| #include "util.h" |
| #include "i2c_driver.h" |
| |
| #define LP5018_DEVICE_CONFIG0 0x00 |
| #define LP5018_DEVICE_CONFIG1 0x01 |
| #define LP5018_GLOBAL_DIMMING 0x03 |
| #define LP5018_BRIGHTNESS_BASE 0x07 |
| #define LP5018_COLOR_BASE 0x0F |
| #define LP5018_MAX_LED_CHANNELS 24 |
| #define LP5018_MAX_BANKS 3 |
| #define LP5018_MAX_LEDS 8 |
| |
| extern int lgpl_printf(const char *format, ...); |
| extern int i2c_master_write_and_read(int id, int target_addr, unsigned char* send_buff, int send_len, unsigned char* recv_buff, int recv_len); |
| |
| int diag_i2c_led_lp5018_detect_driver(int master_id, unsigned char slave_addr) { |
| unsigned char tx_buf = LP5018_DEVICE_CONFIG1; |
| unsigned char rx_buf = 0xFF; |
| int ret; |
| |
| lgpl_printf("%s: master_id: %d slave_addr: 0x%x\n", __func__, master_id, slave_addr); |
| |
| ret = i2c_master_write_and_read(master_id, slave_addr, &tx_buf, 1, &rx_buf, 1); |
| if (ret != 0) { |
| lgpl_printf("%s: failed to set Config1 register.\n", __func__); |
| return ret; |
| } |
| |
| if (rx_buf != 0x3C) { |
| lgpl_printf("%s: Config1 register value is incorrect: 0x%x\n", __func__, rx_buf); |
| return -1; |
| } |
| |
| return 0; |
| } |
| |
| // Initializes the LP5018 LED driver at I2C address |slave_addr|. |
| int diag_i2c_led_lp5018_init(int master_id, unsigned char slave_addr) { |
| unsigned char buff[2]; |
| unsigned char rx_buf = 0xFF; |
| int ret; |
| |
| lgpl_printf("%s: master_id: %d slave_addr: 0x%x\n", __func__, master_id, slave_addr); |
| |
| /* Initialize LED driver */ |
| /* Set Chip_EN */ |
| buff[0] = LP5018_DEVICE_CONFIG0; |
| buff[1] = 0x40; |
| ret = i2c_master_write_and_read(master_id, slave_addr, buff, 2, 0, 0); |
| if (ret != 0) { |
| lgpl_printf("%s: failed to set Config0 register.\n", __func__); |
| return ret; |
| } |
| |
| /* Set Log_Scale_EN, Power_save_EN, Auto_incr_EN and PWm_Dithering_EN */ |
| buff[0] = LP5018_DEVICE_CONFIG1; |
| buff[1] = 0x3C; |
| ret = i2c_master_write_and_read(master_id, slave_addr, buff, 2, &rx_buf, 1); |
| if (ret != 0) { |
| lgpl_printf("%s: failed to set Config1 register.\n", __func__); |
| return ret; |
| } |
| |
| /* Now set all the leds to their first value */ |
| unsigned char val[LP5018_MAX_LED_CHANNELS + 1]; |
| size_t i; |
| |
| val[0] = LP5018_COLOR_BASE; // Register address |
| for (i = 1; i < LP5018_MAX_LED_CHANNELS + 1; ++i) { |
| val[i] = 0x00; |
| } |
| ret = i2c_master_write_and_read(master_id, slave_addr, val, sizeof(val), |
| 0, 0); |
| if (ret < 0) { |
| lgpl_printf("%s: failed on initialization LED.\n", __func__); |
| return ret; |
| } |
| |
| return 0; |
| } |
| |
| // Sets the first |size|/3 LEDs of LP5018 with address |slave_addr| to |frame|. |
| int diag_i2c_led_lp5018_set_frame(int master_id, unsigned char slave_addr, |
| unsigned char *frame, size_t size) { |
| int ret; |
| unsigned char val[LP5018_MAX_LED_CHANNELS + LP5018_MAX_LEDS + 1]; |
| size_t i; |
| |
| if (size < LP5018_MAX_LED_CHANNELS + LP5018_MAX_LEDS){ |
| lgpl_printf("%s: LED frame is too small!.\n", __func__); |
| return 0; |
| } |
| |
| val[0] = LP5018_BRIGHTNESS_BASE; // Register address |
| for (i = 1; i < LP5018_MAX_LED_CHANNELS + LP5018_MAX_LEDS + 1; ++i) { |
| val[i] = frame[i-1]; |
| } |
| ret = i2c_master_write_and_read(master_id, slave_addr, val, sizeof(val), |
| 0, 0); |
| if (ret < 0) { |
| lgpl_printf("%s: failed on initialization LED.\n", __func__); |
| return ret; |
| } |
| |
| return 0; |
| } |