This is an automated email from the ASF dual-hosted git repository.

vipulrahane pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git


The following commit(s) were added to refs/heads/master by this push:
     new ff02eb6  Lp5523 improvements (#1122)
ff02eb6 is described below

commit ff02eb6f028a7248b8090e24a5ff4568286c72c8
Author: Vipul Rahane <[email protected]>
AuthorDate: Mon May 28 14:52:52 2018 -0700

    Lp5523 improvements (#1122)
    
    * lp5523 improvements
    - Add led_itf
    - Add APIs that were missing earlier for configs
    - Add configs
    * lp5523: Add conditional led_itf & multi byte trans
    - Add led_itf if abstraction is enabled
    - Change lp5523_set_2_regs() to lp5523_set_n_regs() to work for multiple
      bytes
    * lp5523 shell: use led_itf instead of sensor_itf
    * lp5523: make the changes more efficient
---
 hw/drivers/lp5523/include/lp5523/lp5523.h | 245 ++++++++++++++---
 hw/drivers/lp5523/src/lp5523.c            | 441 +++++++++++++++++++++---------
 hw/drivers/lp5523/src/lp5523_shell.c      |   8 +-
 hw/drivers/lp5523/syscfg.yml              |   5 +-
 4 files changed, 516 insertions(+), 183 deletions(-)

diff --git a/hw/drivers/lp5523/include/lp5523/lp5523.h 
b/hw/drivers/lp5523/include/lp5523/lp5523.h
index 4658174..4f64779 100644
--- a/hw/drivers/lp5523/include/lp5523/lp5523.h
+++ b/hw/drivers/lp5523/include/lp5523/lp5523.h
@@ -26,6 +26,12 @@ extern "C" {
 
 #include <sensor/sensor.h>
 
+#if MYNEWT_VAL(LED_ENABLE_ABSTRACTION)
+#include <led/led.h>
+#endif
+
+#define LP5523_MAX_PAYLOAD   (10)
+
 #define LP5523_I2C_BASE_ADDR (0x32)
 
 /* Engine control mask */
@@ -49,26 +55,109 @@ extern "C" {
 #define LP5523_LED2 (2)
 #define LP5523_LED1 (1)
 
+struct per_led_cfg {
+    /* Mapping */
+    uint8_t mapping:2;
+    /* Enable Log dimming */
+    uint8_t log_dim_en:1;
+    /* Enable temperature compensation */
+    uint8_t temp_comp:5;
+    /* Output On/Off */
+    uint8_t output_on:1;
+    /* In steps of 100 microamps */
+    uint8_t current_ctrl;
+};
+
 struct lp5523_cfg {
     /* The 2 LSBs of this represent ASEL1 and ASEL0 */
-    uint8_t asel;
-    uint8_t clk_det_en;
-    uint8_t int_clk_en;
-    uint8_t cp_mode;
-    uint8_t int_conf;
+    uint8_t asel:2;
+    /* Enable clock detect enable */
+    uint8_t clk_det_en:1;
+    /* Enable Int clock */
+    uint8_t int_clk_en:1;
+    /* Select CP Mode */
+    uint8_t cp_mode:2;
+    /* Enable VAR_D_SEL */
+    uint8_t var_d_sel:1;
+    /* Enable Power Save */
+    uint8_t ps_en:1;
+    /* Enable PWM power save */
+    uint8_t pwm_ps_en:1;
+    /* Enable auto increment */
+    uint8_t auto_inc_en:1;
+    /* INT conf */
+    uint8_t int_conf:1;
+    /* INT GPO */
+
+    /****** Gain change control settings ******/
+    uint8_t int_gpo:1;
+    /* Threshold Voltage */
+    uint8_t thresh_volt:2;
+    /* Enable adaptive threshold */
+    uint8_t adapt_thresh_en:1;
+    /* Timer value */
+    uint8_t timer:2;
+    /* Force_1x enbale */
+    uint8_t force_1x:1;
+
+    /* All per LED configs go here - 0: D1 8: D9 */
+    struct per_led_cfg per_led_cfg[MYNEWT_VAL(LP5523_LEDS_PER_DRIVER)];
 };
 
 struct lp5523 {
     struct os_dev dev;
-    struct sensor sensor;
+#if MYNEWT_VAL(LED_ENABLE_ABSTRACTION)
+    struct led_dev led_dev;
+#endif
     struct lp5523_cfg cfg;
 };
 
+#if MYNEWT_VAL(LED_ENABLE_ABSTRACTION) == 0
+/*
+ * LED interface
+ */
+struct led_itf {
+
+    /* LED interface type */
+    uint8_t li_type;
+
+    /* interface number */
+    uint8_t li_num;
+
+    /* CS pin - optional, only needed for SPI */
+    uint8_t li_cs_pin;
+
+    /* LED chip address, only needed for I2C */
+    uint16_t li_addr;
+};
+#endif
+
+/**** Config Values ****/
+#define LP5523_ASEL00_ADDR_32h            0x00
+#define LP5523_ASEL01_ADDR_33h            0x01
+#define LP5523_ASEL10_ADDR_34h            0x02
+#define LP5523_ASEL11_ADDR_35h            0x03
+
+#define LP5523_CP_MODE_OFF                0x00
+#define LP5523_CP_MODE_FORCE_TO_BYPASS    0x01
+#define LP5523_CP_MODE_FORCE_TO_1_5X      0x02
+#define LP5523_CP_MODE_AUTO               0x03
+
+#define LP5523_THRESH_VOLT_400MV          0x00
+#define LP5523_THRESH_VOLT_300MV          0x01
+#define LP5523_THRESH_VOLT_200MV          0x02
+#define LP5523_THRESH_VOLT_100MV          0x03
+
+#define LP5523_TIMER_5MS                  0x00
+#define LP5523_TIMER_10MS                 0x01
+#define LP5523_TIMER_50MS                 0x02
+#define LP5523_TIMER_INF                  0x03
+
 /* register addresses */
 
 enum lp5523_bitfield_registers {
     LP5523_OUTPUT_RATIOMETRIC = 0x02,
-    LP5523_OUTPUT_CONTROL = 0x04,
+    LP5523_OUTPUT_CTRL_MSB = 0x04,
     LP5523_ENG_MAPPING = 0x70
 };
 
@@ -92,7 +181,7 @@ enum lp5523_engine_control_registers {
 
 enum lp5523_registers {
     LP5523_ENABLE = 0x00,
-    LP5523_LED_OUTPUT_CTRL = 0x05,
+    LP5523_OUTPUT_CTRL_LSB = 0x05,
     LP5523_LED_CONTROL_BASE = 0x06,
     LP5523_PWM_BASE = 0x16,
     LP5523_MISC = 0x36,
@@ -227,7 +316,7 @@ LP5523_REGISTER_VALUE(LP5523_GAIN_CHANGE_CTRL, 
LP5523_FORCE_1X, 2, 0x04);
  *
  * @return 0 on success, non-zero error on failure.
  */
-int lp5523_set_reg(struct sensor_itf *itf, enum lp5523_registers addr,
+int lp5523_set_reg(struct led_itf *itf, enum lp5523_registers addr,
     uint8_t value);
 
 /**
@@ -239,7 +328,7 @@ int lp5523_set_reg(struct sensor_itf *itf, enum 
lp5523_registers addr,
  *
  * @return 0 on success, non-zero error on failure.
  */
-int lp5523_get_reg(struct sensor_itf *itf, enum lp5523_registers addr,
+int lp5523_get_reg(struct led_itf *itf, enum lp5523_registers addr,
     uint8_t *value);
 
 /**
@@ -264,7 +353,7 @@ int lp5523_apply_value(struct lp5523_register_value addr, 
uint8_t value,
  *
  * @return 0 on success, non-zero error on failure.
  */
-int lp5523_set_value(struct sensor_itf *itf, struct lp5523_register_value addr,
+int lp5523_set_value(struct led_itf *itf, struct lp5523_register_value addr,
     uint8_t value);
 
 /**
@@ -277,7 +366,7 @@ int lp5523_set_value(struct sensor_itf *itf, struct 
lp5523_register_value addr,
  *
  * @return 0 on success, non-zero error on failure.
  */
-int lp5523_get_value(struct sensor_itf *itf, struct lp5523_register_value addr,
+int lp5523_get_value(struct led_itf *itf, struct lp5523_register_value addr,
     uint8_t *value);
 
 /**
@@ -294,7 +383,7 @@ int lp5523_get_value(struct sensor_itf *itf, struct 
lp5523_register_value addr,
  *
  * @return 0 on success, non-zero error on failure.
  */
-int lp5523_set_bitfield(struct sensor_itf *itf,
+int lp5523_set_bitfield(struct led_itf *itf,
     enum lp5523_bitfield_registers addr, uint16_t outputs);
 
 /**
@@ -310,7 +399,7 @@ int lp5523_set_bitfield(struct sensor_itf *itf,
  *
  * @return 0 on success, non-zero error on failure.
  */
-int lp5523_get_bitfield(struct sensor_itf *itf,
+int lp5523_get_bitfield(struct led_itf *itf,
     enum lp5523_bitfield_registers addr, uint16_t* outputs);
 
 /**
@@ -320,14 +409,14 @@ int lp5523_get_bitfield(struct sensor_itf *itf,
  * LP5523_PWM
  * LP5523_CURRENT_CONTROL
  *
- * @param The sensor interface.
+ * @param The LED interface.
  * @param The register address to write to (the MSB register).
  * @param Output ID 1-9.
  * @param Value to write.
  *
  * @return 0 on success, non-zero error on failure.
  */
-int lp5523_set_output_reg(struct sensor_itf *itf,
+int lp5523_set_output_reg(struct led_itf *itf,
     enum lp5523_output_registers addr, uint8_t output, uint8_t value);
 
 /**
@@ -344,7 +433,7 @@ int lp5523_set_output_reg(struct sensor_itf *itf,
  *
  * @return 0 on success, non-zero error on failure.
  */
-int lp5523_get_output_reg(struct sensor_itf *itf,
+int lp5523_get_output_reg(struct led_itf *itf,
     enum lp5523_output_registers addr, uint8_t output, uint8_t *value);
 
 /**
@@ -361,7 +450,7 @@ int lp5523_get_output_reg(struct sensor_itf *itf,
  *
  * @return 0 on success, non-zero error on failure.
  */
-int lp5523_set_engine_reg(struct sensor_itf *itf,
+int lp5523_set_engine_reg(struct led_itf *itf,
     enum lp5523_engine_registers addr, uint8_t engine, uint8_t value);
 
 /**
@@ -378,7 +467,7 @@ int lp5523_set_engine_reg(struct sensor_itf *itf,
  *
  * @return 0 on success, non-zero error on failure.
  */
-int lp5523_get_engine_reg(struct sensor_itf *itf,
+int lp5523_get_engine_reg(struct led_itf *itf,
     enum lp5523_engine_registers addr, uint8_t engine, uint8_t *value);
 
 /**
@@ -389,7 +478,7 @@ int lp5523_get_engine_reg(struct sensor_itf *itf,
  *
  * @return 0 on success, non-zero error on failure.
  */
-int lp5523_set_enable(struct sensor_itf *itf, uint8_t enable);
+int lp5523_set_enable(struct led_itf *itf, uint8_t enable);
 
 /**
  * Sets the ENGINEX_MODE and ENGINEX_EXEC bits in the ENGINE CNTRLX registers.
@@ -403,7 +492,7 @@ int lp5523_set_enable(struct sensor_itf *itf, uint8_t 
enable);
  *
  * @return 0 on success, non-zero error on failure.
  */
-int lp5523_set_engine_control(struct sensor_itf *itf,
+int lp5523_set_engine_control(struct led_itf *itf,
     enum lp5523_engine_control_registers addr, uint8_t engine_mask,
     uint8_t values);
 
@@ -416,7 +505,7 @@ int lp5523_set_engine_control(struct sensor_itf *itf,
  *
  * @return 0 on success, non-zero error on failure.
  */
-int lp5523_set_output_mapping(struct sensor_itf *itf, uint8_t output,
+int lp5523_set_output_mapping(struct led_itf *itf, uint8_t output,
     uint8_t mapping);
 
 /**
@@ -428,7 +517,7 @@ int lp5523_set_output_mapping(struct sensor_itf *itf, 
uint8_t output,
  *
  * @return 0 on success, non-zero error on failure.
  */
-int lp5523_get_output_mapping(struct sensor_itf *itf, uint8_t output,
+int lp5523_get_output_mapping(struct led_itf *itf, uint8_t output,
     uint8_t *mapping);
 
 /**
@@ -440,7 +529,7 @@ int lp5523_get_output_mapping(struct sensor_itf *itf, 
uint8_t output,
  *
  * @return 0 on success, non-zero error on failure.
  */
-int lp5523_set_output_log_dim(struct sensor_itf *itf, uint8_t output,
+int lp5523_set_output_log_dim(struct led_itf *itf, uint8_t output,
     uint8_t enable);
 
 /**
@@ -452,7 +541,7 @@ int lp5523_set_output_log_dim(struct sensor_itf *itf, 
uint8_t output,
  *
  * @return 0 on success, non-zero error on failure.
  */
-int lp5523_get_output_log_dim(struct sensor_itf *itf, uint8_t output,
+int lp5523_get_output_log_dim(struct led_itf *itf, uint8_t output,
     uint8_t *enable);
 
 /**
@@ -464,7 +553,7 @@ int lp5523_get_output_log_dim(struct sensor_itf *itf, 
uint8_t output,
  *
  * @return 0 on success, non-zero error on failure.
  */
-int lp5523_set_output_temp_comp(struct sensor_itf *itf, uint8_t output,
+int lp5523_set_output_temp_comp(struct led_itf *itf, uint8_t output,
     uint8_t value);
 
 /**
@@ -476,7 +565,7 @@ int lp5523_set_output_temp_comp(struct sensor_itf *itf, 
uint8_t output,
  *
  * @return 0 on success, non-zero error on failure.
  */
-int lp5523_get_output_temp_comp(struct sensor_itf *itf, uint8_t output,
+int lp5523_get_output_temp_comp(struct led_itf *itf, uint8_t output,
     uint8_t *value);
 
 /**
@@ -488,7 +577,7 @@ int lp5523_get_output_temp_comp(struct sensor_itf *itf, 
uint8_t output,
  *
  * @return 0 on success, non-zero error on failure.
  */
-int lp5523_get_engine_int(struct sensor_itf *itf, uint8_t engine,
+int lp5523_get_engine_int(struct led_itf *itf, uint8_t engine,
     uint8_t *flag);
 
 /**
@@ -498,7 +587,7 @@ int lp5523_get_engine_int(struct sensor_itf *itf, uint8_t 
engine,
  *
  * @return 0 on success, non-zero error on failure.
  */
-int lp5523_reset(struct sensor_itf *itf);
+int lp5523_reset(struct led_itf *itf);
 
 /**
  * Sets the page used for program memory reads and writes.
@@ -508,7 +597,7 @@ int lp5523_reset(struct sensor_itf *itf);
  *
  * @return 0 on success, non-zero error on failure.
  */
-int lp5523_set_page_sel(struct sensor_itf *itf, uint8_t page);
+int lp5523_set_page_sel(struct led_itf *itf, uint8_t page);
 
 /**
  * Sets or clears the relevant DX bit in the ENGX MAPPING registers.
@@ -520,7 +609,7 @@ int lp5523_set_page_sel(struct sensor_itf *itf, uint8_t 
page);
  *
  * @return 0 on success, non-zero error on failure.
  */
-int lp5523_set_engine_mapping(struct sensor_itf *itf, uint8_t engine,
+int lp5523_set_engine_mapping(struct led_itf *itf, uint8_t engine,
     uint16_t output);
 
 /**
@@ -534,7 +623,7 @@ int lp5523_set_engine_mapping(struct sensor_itf *itf, 
uint8_t engine,
  *
  * @return 0 on success, non-zero error on failure.
  */
-int lp5523_get_engine_mapping(struct sensor_itf *itf, uint8_t engine,
+int lp5523_get_engine_mapping(struct led_itf *itf, uint8_t engine,
     uint16_t *output);
 
 /**
@@ -546,7 +635,7 @@ int lp5523_get_engine_mapping(struct sensor_itf *itf, 
uint8_t engine,
  *
  * @return 0 on success, non-zero error on failure.
  */
-int lp5523_set_instruction(struct sensor_itf *itf, uint8_t addr, uint16_t ins);
+int lp5523_set_instruction(struct led_itf *itf, uint8_t addr, uint16_t ins);
 
 /**
  * gets an instruction from program memory.
@@ -557,7 +646,7 @@ int lp5523_set_instruction(struct sensor_itf *itf, uint8_t 
addr, uint16_t ins);
  *
  * @return 0 on success, non-zero error on failure.
  */
-int lp5523_get_instruction(struct sensor_itf *itf, uint8_t addr, uint16_t 
*ins);
+int lp5523_get_instruction(struct led_itf *itf, uint8_t addr, uint16_t *ins);
 
 /**
  * Writes a program to memory.
@@ -571,7 +660,7 @@ int lp5523_get_instruction(struct sensor_itf *itf, uint8_t 
addr, uint16_t *ins);
  *
  * @return 0 on success, non-zero error on failure.
  */
-int lp5523_set_program(struct sensor_itf *itf, uint8_t engines, uint16_t *pgm,
+int lp5523_set_program(struct led_itf *itf, uint8_t engines, uint16_t *pgm,
     uint8_t start, uint8_t size);
 
 /**
@@ -584,7 +673,7 @@ int lp5523_set_program(struct sensor_itf *itf, uint8_t 
engines, uint16_t *pgm,
  *
  * @return 0 on success, non-zero error on failure.
  */
-int lp5523_get_program(struct sensor_itf *itf, uint16_t *pgm, uint8_t start,
+int lp5523_get_program(struct led_itf *itf, uint16_t *pgm, uint8_t start,
     uint8_t size);
 
 /**
@@ -598,7 +687,7 @@ int lp5523_get_program(struct sensor_itf *itf, uint16_t 
*pgm, uint8_t start,
  * @return 0 on success, 1 on verify failure, other non-zero error on other
  * failure.
  */
-int lp5523_verify_program(struct sensor_itf *itf, uint16_t *pgm, uint8_t start,
+int lp5523_verify_program(struct led_itf *itf, uint16_t *pgm, uint8_t start,
     uint8_t size);
 
 /**
@@ -610,7 +699,7 @@ int lp5523_verify_program(struct sensor_itf *itf, uint16_t 
*pgm, uint8_t start,
  *
  * @return 0 on success, non-zero error on failure.
  */
-int lp5523_engines_run(struct sensor_itf *itf, uint8_t engines);
+int lp5523_engines_run(struct led_itf *itf, uint8_t engines);
 
 /**
  * Sets the specified engines to hold execution of their respective programs.
@@ -621,7 +710,7 @@ int lp5523_engines_run(struct sensor_itf *itf, uint8_t 
engines);
  *
  * @return 0 on success, non-zero error on failure.
  */
-int lp5523_engines_hold(struct sensor_itf *itf, uint8_t engines);
+int lp5523_engines_hold(struct led_itf *itf, uint8_t engines);
 
 /**
  * Sets the specified engines to execute the next instruction of their
@@ -633,7 +722,7 @@ int lp5523_engines_hold(struct sensor_itf *itf, uint8_t 
engines);
  *
  * @return 0 on success, non-zero error on failure.
  */
-int lp5523_engines_step(struct sensor_itf *itf, uint8_t engines);
+int lp5523_engines_step(struct led_itf *itf, uint8_t engines);
 
 /**
  * Disables the specified engines.
@@ -644,7 +733,7 @@ int lp5523_engines_step(struct sensor_itf *itf, uint8_t 
engines);
  *
  * @return 0 on success, non-zero error on failure.
  */
-int lp5523_engines_disable(struct sensor_itf *itf, uint8_t engines);
+int lp5523_engines_disable(struct led_itf *itf, uint8_t engines);
 
 /**
  * Reads the ADC on a pin.
@@ -655,7 +744,7 @@ int lp5523_engines_disable(struct sensor_itf *itf, uint8_t 
engines);
  *
  * @return 0 on success, non-zero error on failure.
  */
-int lp5523_read_adc(struct sensor_itf *itf, uint8_t pin, uint8_t *v);
+int lp5523_read_adc(struct led_itf *itf, uint8_t pin, uint8_t *v);
 
 /**
  * Tests the Device, checks for a clock if necessary and checks the ADC values
@@ -665,7 +754,7 @@ int lp5523_read_adc(struct sensor_itf *itf, uint8_t pin, 
uint8_t *v);
  *
  * @return 0 on success, non-zero error on failure.
  */
-int lp5523_self_test(struct sensor_itf *itf);
+int lp5523_self_test(struct led_itf *itf);
 
 /**
  * Expects to be called back through os_dev_create().
@@ -678,6 +767,76 @@ int lp5523_self_test(struct sensor_itf *itf);
 int lp5523_init(struct os_dev *dev, void *arg);
 int lp5523_config(struct lp5523 *lp, struct lp5523_cfg *cfg);
 
+/**
+ * Calculate Temp comp bits from correction factor
+ *
+ * @param corr_factor Correction factor -1.5 < corr_factor < +1.5
+ * @param temp_comp Temperature compensation bits 5 bits 11111 - 01111
+ *
+ * @return 0 on success, non-zero on failure
+ */
+int
+lp5523_calc_temp_comp(float corr_factor, uint8_t *temp_comp);
+
+/**
+ * Set output ON for particular output
+ *
+ * @param itf Ptr to LED itf
+ * @param output Number of the output
+ * @param on Output 1-ON/0-OFF
+ *
+ * @return 0 on success, non-zero on failure
+ */
+int
+lp5523_set_output_on(struct led_itf *itf, uint8_t output, uint8_t on);
+
+/**
+ * Get output ON for particular output
+ *
+ * @param itf Ptr to LED itf
+ * @param output Number of the output
+ * @param on Ptr to output variable Output 1-ON/0-OFF
+ *
+ * @return 0 on success, non-zero on failure
+ */
+int
+lp5523_get_output_on(struct led_itf *itf, uint8_t output, uint8_t *on);
+
+/**
+ *
+ * Get status
+ *
+ * @param LED interface
+ * @param Ptr to status
+ *
+ * @return 0 on success, non-zero on failure
+ */
+int
+lp5523_get_status(struct led_itf *itf, uint8_t *status);
+
+/**
+ * Get Output Current control
+ *
+ * @param itf LED interface
+ * @param output Number of the output
+ * @param curr_ctrl Ptr to Current control
+ *
+ * @return 0 on success, non-zero on failure
+ */
+int
+lp5523_get_output_curr_ctrl(struct led_itf *itf, uint8_t output, uint8_t 
*curr_ctrl);
+
+/**
+ *
+ * Set output Current control
+ *
+ * @param itf LED interface
+ * @param output Number of the output
+ * @param curr_ctrl Current control value to set
+ */
+int
+lp5523_set_output_curr_ctrl(struct led_itf *itf, uint8_t output, uint8_t 
curr_ctrl);
+
 #if MYNEWT_VAL(LP5523_CLI)
 int lp5523_shell_init(void);
 #endif
diff --git a/hw/drivers/lp5523/src/lp5523.c b/hw/drivers/lp5523/src/lp5523.c
index 55cccc9..c9da96a 100644
--- a/hw/drivers/lp5523/src/lp5523.c
+++ b/hw/drivers/lp5523/src/lp5523.c
@@ -24,6 +24,10 @@
 
 #include "lp5523/lp5523.h"
 
+#if MYNEWT_VAL(LED_ABSTRACTION_LAYER)
+#include "led/led.h"
+#endif
+
 /* Define the stats section and records */
 STATS_SECT_START(lp5523_stat_section)
     STATS_SECT_ENTRY(read_errors)
@@ -45,24 +49,24 @@ STATS_SECT_DECL(lp5523_stat_section) g_lp5523stats;
 static struct log _log;
 
 int
-lp5523_set_reg(struct sensor_itf *itf, enum lp5523_registers addr,
+lp5523_set_reg(struct led_itf *itf, enum lp5523_registers addr,
     uint8_t value)
 {
     int rc;
     uint8_t payload[2] = { addr, value };
 
     struct hal_i2c_master_data data_struct = {
-        .address = itf->si_addr,
+        .address = itf->li_addr,
         .len = 2,
         .buffer = payload
     };
 
-    rc = hal_i2c_master_write(itf->si_num, &data_struct,
+    rc = hal_i2c_master_write(itf->li_num, &data_struct,
                               OS_TICKS_PER_SEC / 10, 1);
 
-    if (rc != 0) {
+    if (rc) {
         LP5523_ERR("Failed to write to 0x%02X:0x%02X with value 0x%02X\n",
-                       itf->si_addr, addr, value);
+                       itf->li_addr, addr, value);
         STATS_INC(g_lp5523stats, read_errors);
     }
 
@@ -70,101 +74,124 @@ lp5523_set_reg(struct sensor_itf *itf, enum 
lp5523_registers addr,
 }
 
 int
-lp5523_get_reg(struct sensor_itf *itf, enum lp5523_registers addr,
+lp5523_get_reg(struct led_itf *itf, enum lp5523_registers addr,
     uint8_t *value)
 {
     int rc;
 
     struct hal_i2c_master_data data_struct = {
-        .address = itf->si_addr,
+        .address = itf->li_addr,
         .len = 1,
         .buffer = &addr
     };
 
     /* Register write */
-    rc = hal_i2c_master_write(itf->si_num, &data_struct,
+    rc = hal_i2c_master_write(itf->li_num, &data_struct,
                               OS_TICKS_PER_SEC / 10, 0);
-    if (rc != 0) {
-        LP5523_ERR("I2C access failed at address 0x%02X\n", itf->si_addr);
+    if (rc) {
+        LP5523_ERR("I2C access failed at address 0x%02X\n", itf->li_addr);
         STATS_INC(g_lp5523stats, write_errors);
-        return rc;
+        goto err;
     }
 
     /* Read one byte back */
     data_struct.buffer = value;
-    rc = hal_i2c_master_read(itf->si_num, &data_struct,
+    rc = hal_i2c_master_read(itf->li_num, &data_struct,
                              OS_TICKS_PER_SEC / 10, 1);
 
-    if (rc != 0) {
-         LP5523_ERR("Failed to read from 0x%02X:0x%02X\n", itf->si_addr, addr);
+    if (rc) {
+         LP5523_ERR("Failed to read from 0x%02X:0x%02X\n", itf->li_addr, addr);
          STATS_INC(g_lp5523stats, read_errors);
     }
+
+err:
     return rc;
 }
 
 static int
-lp5523_set_2_regs(struct sensor_itf *itf, enum lp5523_registers addr,
-    uint8_t vals[2])
+lp5523_set_n_regs(struct led_itf *itf, enum lp5523_registers addr,
+    uint8_t *vals, uint8_t len)
 {
     int rc;
-    uint8_t regs[3];
-
-    regs[0] = addr ;
-    regs[1] = vals[0];
-    regs[2] = vals[1];
+    uint8_t regs[LP5523_MAX_PAYLOAD] = {0};
 
     struct hal_i2c_master_data data_struct = {
-        .address = itf->si_addr,
-        .len = 3,
+        .address = itf->li_addr,
+        .len = len + 1,
         .buffer = regs
     };
 
-    rc = hal_i2c_master_write(itf->si_num, &data_struct,
+    memcpy(regs, vals, len + 1);
+
+    regs[0] = addr;
+
+    rc = hal_i2c_master_write(itf->li_num, &data_struct,
                               (OS_TICKS_PER_SEC / 5), 1);
 
-    if (rc != 0) {
-        LP5523_ERR("Failed to write to 0x%02X:0x%02X\n", itf->si_addr, 
regs[0]);
+    if (rc) {
+        LP5523_ERR("Failed to write to 0x%02X:0x%02X\n", itf->li_addr, 
regs[0]);
         STATS_INC(g_lp5523stats, read_errors);
     }
+
     return rc;
 }
 
 /* XXX: Not sure if ai-read is supported, seems to work for reads of length 2 
*/
 static int
-lp5523_get_2_regs(struct sensor_itf *itf, enum lp5523_registers addr,
-    uint8_t vals[2])
+lp5523_get_n_regs(struct led_itf *itf, enum lp5523_registers addr,
+    uint8_t *vals, uint8_t len)
 {
     int rc;
     uint8_t addr_b = (uint8_t) addr;
 
     struct hal_i2c_master_data data_struct = {
-        .address = itf->si_addr,
+        .address = itf->li_addr,
         .len = 1,
         .buffer = &addr_b
     };
 
-    rc = hal_i2c_master_write(itf->si_num, &data_struct,
+    rc = hal_i2c_master_write(itf->li_num, &data_struct,
         (OS_TICKS_PER_SEC / 10), 0);
 
-    if (rc != 0) {
-        LP5523_ERR("Failed to write to 0x%02X:0x%02X\n", itf->si_addr, addr_b);
+    if (rc) {
+        LP5523_ERR("Failed to write to 0x%02X:0x%02X\n", itf->li_addr, addr_b);
         STATS_INC(g_lp5523stats, read_errors);
         return rc;
     }
 
-    data_struct.len = 2;
+    data_struct.len = len;
     data_struct.buffer = vals;
-    rc = hal_i2c_master_read(itf->si_num, &data_struct,
+    rc = hal_i2c_master_read(itf->li_num, &data_struct,
         OS_TICKS_PER_SEC / 5, 1);
 
-    if (rc != 0) {
-         LP5523_ERR("Failed to read from 0x%02X:0x%02X\n", itf->si_addr,
+    if (rc) {
+         LP5523_ERR("Failed to read from 0x%02X:0x%02X\n", itf->li_addr,
             addr_b);
          STATS_INC(g_lp5523stats, read_errors);
     }
     return rc;
 }
 
+int
+lp5523_calc_temp_comp(float corr_factor, uint8_t *temp_comp)
+{
+    uint8_t val;
+
+    /* To deactivate compensation set temp_comp to 0 */
+    if (corr_factor < -1.5 || corr_factor > 1.5) {
+        return SYS_EINVAL;
+    }
+
+    val = abs(corr_factor/0.1);
+
+    if (corr_factor < 0) {
+        val &= 0x10 & val;
+    }
+
+    return 0;
+}
+
+
 static void
 lp5523_wait(uint32_t ms)
 {
@@ -187,19 +214,19 @@ int lp5523_apply_value(struct lp5523_register_value addr, 
uint8_t value,
 }
 
 int
-lp5523_set_value(struct sensor_itf *itf, struct lp5523_register_value addr,
+lp5523_set_value(struct led_itf *itf, struct lp5523_register_value addr,
     uint8_t value)
 {
     int rc;
     uint8_t reg;
 
     rc = lp5523_get_reg(itf, addr.reg, &reg);
-    if (rc != 0) {
+    if (rc) {
         return rc;
     }
 
     rc = lp5523_apply_value(addr, value, &reg);
-    if (rc != 0) {
+    if (rc) {
         return rc;
     }
 
@@ -207,7 +234,7 @@ lp5523_set_value(struct sensor_itf *itf, struct 
lp5523_register_value addr,
 }
 
 int
-lp5523_get_value(struct sensor_itf *itf, struct lp5523_register_value addr,
+lp5523_get_value(struct led_itf *itf, struct lp5523_register_value addr,
     uint8_t *value)
 {
     int rc;
@@ -221,22 +248,22 @@ lp5523_get_value(struct sensor_itf *itf, struct 
lp5523_register_value addr,
 }
 
 int
-lp5523_set_bitfield(struct sensor_itf *itf, enum lp5523_bitfield_registers 
addr,
+lp5523_set_bitfield(struct led_itf *itf, enum lp5523_bitfield_registers addr,
     uint16_t outputs)
 {
     uint8_t vals[2] = { (outputs >> 8) & 0x01, outputs & 0xff };
 
-    return lp5523_set_2_regs(itf, addr, vals);
+    return lp5523_set_n_regs(itf, addr, vals, 2);
 }
 
 int
-lp5523_get_bitfield(struct sensor_itf *itf, enum lp5523_bitfield_registers 
addr,
+lp5523_get_bitfield(struct led_itf *itf, enum lp5523_bitfield_registers addr,
     uint16_t* outputs)
 {
     int rc;
     uint8_t vals[2];
 
-    rc = lp5523_get_2_regs(itf, addr, vals);
+    rc = lp5523_get_n_regs(itf, addr, vals, 2);
 
     *outputs = ((vals[0] & 0x01) << 8) | vals[1];
 
@@ -244,17 +271,75 @@ lp5523_get_bitfield(struct sensor_itf *itf, enum 
lp5523_bitfield_registers addr,
 }
 
 int
-lp5523_set_output_reg(struct sensor_itf *itf, enum lp5523_output_registers 
addr,
+lp5523_set_output_on(struct led_itf *itf, uint8_t output, uint8_t on)
+{
+    int rc;
+    uint16_t outputs;
+
+    if ((output < 1) || (output > 9)) {
+        rc = -1;
+        goto err;
+    }
+
+    rc = lp5523_get_bitfield(itf, LP5523_OUTPUT_CTRL_MSB,
+                             &outputs);
+    if (rc) {
+        goto err;
+    }
+
+    if (!on) {
+        outputs &= ~(0x1 << (output - 1));
+    } else {
+        outputs |= (0x1 << (output - 1));
+    }
+
+    rc = lp5523_set_bitfield(itf, LP5523_OUTPUT_CTRL_MSB, outputs);
+    if (rc) {
+        goto err;
+    }
+
+    return 0;
+err:
+    return rc;
+}
+
+int
+lp5523_get_output_on(struct led_itf *itf, uint8_t output, uint8_t *on)
+{
+    int rc;
+    uint16_t outputs;
+
+    if ((output < 1) || (output > 9)) {
+        rc = -1;
+        goto err;
+    }
+
+    rc = lp5523_get_bitfield(itf, LP5523_OUTPUT_CTRL_MSB,
+                             &outputs);
+    if (rc) {
+        goto err;
+    }
+
+    *on = (outputs & (0x1 << (output - 1)));
+
+    return 0;
+err:
+    return rc;
+}
+
+int
+lp5523_set_output_reg(struct led_itf *itf, enum lp5523_output_registers addr,
     uint8_t output, uint8_t value)
 {
     if ((output < 1) || (output > 9)) {
         return -1;
     }
+
     return lp5523_set_reg(itf, addr + (output - 1), value);
 }
 
 int
-lp5523_get_output_reg(struct sensor_itf *itf, enum lp5523_output_registers 
addr,
+lp5523_get_output_reg(struct led_itf *itf, enum lp5523_output_registers addr,
     uint8_t output, uint8_t *value)
 {
     if ((output < 1) || (output > 9)) {
@@ -263,8 +348,9 @@ lp5523_get_output_reg(struct sensor_itf *itf, enum 
lp5523_output_registers addr,
     return lp5523_get_reg(itf, addr + (output - 1), value);
 }
 
+
 int
-lp5523_set_engine_reg(struct sensor_itf *itf, enum lp5523_engine_registers 
addr,
+lp5523_set_engine_reg(struct led_itf *itf, enum lp5523_engine_registers addr,
     uint8_t engine, uint8_t value)
 {
     if ((engine < 1) || (engine > 3)) {
@@ -274,7 +360,7 @@ lp5523_set_engine_reg(struct sensor_itf *itf, enum 
lp5523_engine_registers addr,
 }
 
 int
-lp5523_get_engine_reg(struct sensor_itf *itf, enum lp5523_engine_registers 
addr,
+lp5523_get_engine_reg(struct led_itf *itf, enum lp5523_engine_registers addr,
     uint8_t engine, uint8_t *value)
 {
     if ((engine < 1) || (engine > 3)) {
@@ -284,7 +370,7 @@ lp5523_get_engine_reg(struct sensor_itf *itf, enum 
lp5523_engine_registers addr,
 }
 
 int
-lp5523_set_enable(struct sensor_itf *itf, uint8_t enable)
+lp5523_set_enable(struct led_itf *itf, uint8_t enable)
 {
     int rc;
 
@@ -297,7 +383,7 @@ lp5523_set_enable(struct sensor_itf *itf, uint8_t enable)
 }
 
 int
-lp5523_set_engine_control(struct sensor_itf *itf,
+lp5523_set_engine_control(struct led_itf *itf,
     enum lp5523_engine_control_registers addr, uint8_t engine_mask, uint8_t 
values)
 {
     uint8_t reg;
@@ -310,7 +396,7 @@ lp5523_set_engine_control(struct sensor_itf *itf,
     }
 
     rc = lp5523_get_reg(itf, addr, &reg);
-    if (rc != 0) {
+    if (rc) {
         return rc;
     }
 
@@ -321,7 +407,7 @@ lp5523_set_engine_control(struct sensor_itf *itf,
 }
 
 int
-lp5523_set_output_mapping(struct sensor_itf *itf, uint8_t output,
+lp5523_set_output_mapping(struct led_itf *itf, uint8_t output,
     uint8_t mapping)
 {
     struct lp5523_register_value reg;
@@ -337,7 +423,7 @@ lp5523_set_output_mapping(struct sensor_itf *itf, uint8_t 
output,
 }
 
 int
-lp5523_get_output_mapping(struct sensor_itf *itf, uint8_t output,
+lp5523_get_output_mapping(struct led_itf *itf, uint8_t output,
     uint8_t *mapping)
 {
     struct lp5523_register_value reg;
@@ -353,7 +439,7 @@ lp5523_get_output_mapping(struct sensor_itf *itf, uint8_t 
output,
 }
 
 int
-lp5523_set_output_log_dim(struct sensor_itf *itf, uint8_t output,
+lp5523_set_output_log_dim(struct led_itf *itf, uint8_t output,
     uint8_t enable)
 {
     struct lp5523_register_value reg;
@@ -369,7 +455,7 @@ lp5523_set_output_log_dim(struct sensor_itf *itf, uint8_t 
output,
 }
 
 int
-lp5523_get_output_log_dim(struct sensor_itf *itf, uint8_t output,
+lp5523_get_output_log_dim(struct led_itf *itf, uint8_t output,
     uint8_t *enable)
 {
     struct lp5523_register_value reg;
@@ -385,7 +471,7 @@ lp5523_get_output_log_dim(struct sensor_itf *itf, uint8_t 
output,
 }
 
 int
-lp5523_set_output_temp_comp(struct sensor_itf *itf, uint8_t output,
+lp5523_set_output_temp_comp(struct led_itf *itf, uint8_t output,
     uint8_t value)
 {
     struct lp5523_register_value reg;
@@ -401,7 +487,7 @@ lp5523_set_output_temp_comp(struct sensor_itf *itf, uint8_t 
output,
 }
 
 int
-lp5523_get_output_temp_comp(struct sensor_itf *itf, uint8_t output,
+lp5523_get_output_temp_comp(struct led_itf *itf, uint8_t output,
     uint8_t *value)
 {
     struct lp5523_register_value reg;
@@ -417,7 +503,7 @@ lp5523_get_output_temp_comp(struct sensor_itf *itf, uint8_t 
output,
 }
 
 int
-lp5523_get_engine_int(struct sensor_itf *itf, uint8_t engine, uint8_t *flag)
+lp5523_get_engine_int(struct led_itf *itf, uint8_t engine, uint8_t *flag)
 {
     struct lp5523_register_value reg;
 
@@ -432,13 +518,13 @@ lp5523_get_engine_int(struct sensor_itf *itf, uint8_t 
engine, uint8_t *flag)
 }
 
 int
-lp5523_reset(struct sensor_itf *itf)
+lp5523_reset(struct led_itf *itf)
 {
     return lp5523_set_reg(itf, LP5523_RESET, 0xff);
 }
 
 int
-lp5523_set_page_sel(struct sensor_itf *itf, uint8_t page)
+lp5523_set_page_sel(struct led_itf *itf, uint8_t page)
 {
     if (page > 5) {
         return -1;
@@ -447,7 +533,7 @@ lp5523_set_page_sel(struct sensor_itf *itf, uint8_t page)
 }
 
 int
-lp5523_set_engine_mapping(struct sensor_itf *itf, uint8_t engine,
+lp5523_set_engine_mapping(struct led_itf *itf, uint8_t engine,
     uint16_t outputs)
 {
     if ((engine < 1) || (engine > 3)) {
@@ -459,7 +545,7 @@ lp5523_set_engine_mapping(struct sensor_itf *itf, uint8_t 
engine,
 }
 
 int
-lp5523_get_engine_mapping(struct sensor_itf *itf, uint8_t engine,
+lp5523_get_engine_mapping(struct led_itf *itf, uint8_t engine,
     uint16_t *outputs)
 {
     if ((engine < 1) || (engine > 3)) {
@@ -471,20 +557,20 @@ lp5523_get_engine_mapping(struct sensor_itf *itf, uint8_t 
engine,
 }
 
 static int
-lp5523_set_pr_instruction(struct sensor_itf *itf, uint8_t addr, uint16_t *ins)
+lp5523_set_pr_instruction(struct led_itf *itf, uint8_t addr, uint16_t *ins)
 {
     uint8_t mem[2] = { ((*ins) >> 8) & 0x00ff, (*ins) & 0x00ff };
 
-    return lp5523_set_2_regs(itf, LP5523_PROGRAM_MEMORY + (addr << 1), mem);
+    return lp5523_set_n_regs(itf, LP5523_PROGRAM_MEMORY + (addr << 1), mem, 2);
 }
 
 static int
-lp5523_get_pr_instruction(struct sensor_itf *itf, uint8_t addr, uint16_t *ins)
+lp5523_get_pr_instruction(struct led_itf *itf, uint8_t addr, uint16_t *ins)
 {
     int rc;
     uint8_t mem[2];
 
-    rc = lp5523_get_2_regs(itf, LP5523_PROGRAM_MEMORY + (addr << 1), mem);
+    rc = lp5523_get_n_regs(itf, LP5523_PROGRAM_MEMORY + (addr << 1), mem, 2);
 
     *ins = (((uint16_t)mem[0]) << 8) | mem[1];
 
@@ -492,15 +578,15 @@ lp5523_get_pr_instruction(struct sensor_itf *itf, uint8_t 
addr, uint16_t *ins)
 }
 
 static int
-lp5523_verify_pr_instruction(struct sensor_itf *itf, uint8_t addr,
+lp5523_verify_pr_instruction(struct led_itf *itf, uint8_t addr,
     uint16_t *ins)
 {
     int rc;
     uint8_t mem[2];
     uint16_t ver;
 
-    rc = lp5523_get_2_regs(itf, LP5523_PROGRAM_MEMORY + (addr << 1), mem);
-    if (rc != 0) {
+    rc = lp5523_get_n_regs(itf, LP5523_PROGRAM_MEMORY + (addr << 1), mem, 2);
+    if (rc) {
         return rc;
     }
 
@@ -511,7 +597,7 @@ lp5523_verify_pr_instruction(struct sensor_itf *itf, 
uint8_t addr,
 
 
 static int
-lp5523_rwv_page(struct sensor_itf *itf, int(*irwv)(struct sensor_itf *,
+lp5523_rwv_page(struct led_itf *itf, int(*irwv)(struct led_itf *,
     uint8_t, uint16_t *), uint8_t page, uint16_t* pgm,
     uint8_t start, uint8_t size)
 {
@@ -519,14 +605,14 @@ lp5523_rwv_page(struct sensor_itf *itf, int(*irwv)(struct 
sensor_itf *,
     uint8_t i;
 
     rc = lp5523_set_page_sel(itf, page);
-    if (rc != 0) {
+    if (rc) {
         return rc;
     }
 
     i = 0;
     while (i < size) {
         rc = irwv(itf, start, pgm + i);
-        if (rc != 0) {
+        if (rc) {
             return rc;
         }
         ++start;
@@ -536,7 +622,7 @@ lp5523_rwv_page(struct sensor_itf *itf, int(*irwv)(struct 
sensor_itf *,
 }
 
 static int
-lp5523_set_page(struct sensor_itf *itf, uint8_t page, uint16_t* pgm,
+lp5523_set_page(struct led_itf *itf, uint8_t page, uint16_t* pgm,
     uint8_t start, uint8_t size)
 {
     return lp5523_rwv_page(itf, &lp5523_set_pr_instruction, page, pgm, start,
@@ -544,7 +630,7 @@ lp5523_set_page(struct sensor_itf *itf, uint8_t page, 
uint16_t* pgm,
 }
 
 static int
-lp5523_get_page(struct sensor_itf *itf, uint8_t page, uint16_t* pgm,
+lp5523_get_page(struct led_itf *itf, uint8_t page, uint16_t* pgm,
     uint8_t start, uint8_t size)
 {
     return lp5523_rwv_page(itf, &lp5523_get_pr_instruction, page, pgm, start,
@@ -552,7 +638,7 @@ lp5523_get_page(struct sensor_itf *itf, uint8_t page, 
uint16_t* pgm,
 }
 
 static int
-lp5523_verify_page(struct sensor_itf *itf, uint8_t page, uint16_t* pgm,
+lp5523_verify_page(struct led_itf *itf, uint8_t page, uint16_t* pgm,
     uint8_t start, uint8_t size)
 {
     return lp5523_rwv_page(itf, &lp5523_verify_pr_instruction, page, pgm, 
start,
@@ -560,7 +646,7 @@ lp5523_verify_page(struct sensor_itf *itf, uint8_t page, 
uint16_t* pgm,
 }
 
 static int
-lp5523_rwv_program(struct sensor_itf *itf, int(*prw)(struct sensor_itf *,
+lp5523_rwv_program(struct led_itf *itf, int(*prw)(struct led_itf *,
     uint8_t, uint16_t *, uint8_t, uint8_t), uint16_t* pgm, uint8_t start,
     uint8_t size)
 {
@@ -581,7 +667,7 @@ lp5523_rwv_program(struct sensor_itf *itf, int(*prw)(struct 
sensor_itf *,
 
     if (page == last_page) {
         rc = prw(itf, page, pgm, start_rel, size);
-        if (rc != 0) {
+        if (rc) {
             return rc;
         }
     } else {
@@ -590,7 +676,7 @@ lp5523_rwv_program(struct sensor_itf *itf, int(*prw)(struct 
sensor_itf *,
 
         write_size = LP5523_PAGE_SIZE - start_rel;
         rc = prw(itf, page, pgm, start_rel, write_size);
-        if (rc != 0) {
+        if (rc) {
             return rc;
         }
         ++page;
@@ -598,7 +684,7 @@ lp5523_rwv_program(struct sensor_itf *itf, int(*prw)(struct 
sensor_itf *,
 
         while (page < last_page) {
             rc = prw(itf, page, pgm, 0, LP5523_PAGE_SIZE);
-            if (rc != 0) {
+            if (rc) {
                 return rc;
             }
             ++page;
@@ -613,27 +699,27 @@ lp5523_rwv_program(struct sensor_itf *itf, 
int(*prw)(struct sensor_itf *,
 }
 
 int
-lp5523_set_instruction(struct sensor_itf *itf, uint8_t addr, uint16_t ins)
+lp5523_set_instruction(struct led_itf *itf, uint8_t addr, uint16_t ins)
 {
     int rc;
 
     rc = lp5523_set_page_sel(itf, addr / LP5523_PAGE_SIZE);
-    return (rc != 0) ? rc : lp5523_set_pr_instruction(itf,
+    return (rc) ? rc : lp5523_set_pr_instruction(itf,
         addr % LP5523_PAGE_SIZE, &ins);
 }
 
 int
-lp5523_get_instruction(struct sensor_itf *itf, uint8_t addr, uint16_t *ins)
+lp5523_get_instruction(struct led_itf *itf, uint8_t addr, uint16_t *ins)
 {
     int rc;
 
     rc = lp5523_set_page_sel(itf, addr / LP5523_PAGE_SIZE);
-    return (rc != 0) ? rc : lp5523_get_pr_instruction(itf,
+    return (rc) ? rc : lp5523_get_pr_instruction(itf,
         addr % LP5523_PAGE_SIZE, ins);
 }
 
 int
-lp5523_set_program(struct sensor_itf *itf, uint8_t engine_mask, uint16_t *pgm,
+lp5523_set_program(struct led_itf *itf, uint8_t engine_mask, uint16_t *pgm,
     uint8_t start, uint8_t size)
 {
     int rc;
@@ -645,14 +731,14 @@ lp5523_set_program(struct sensor_itf *itf, uint8_t 
engine_mask, uint16_t *pgm,
     /* disable engines */
     rc = lp5523_set_engine_control(itf, LP5523_ENGINE_CNTRL2, engine_mask,
         LP5523_ENGINES_DISABLED);
-    if (rc != 0) {
+    if (rc) {
         return rc;
     }
 
     /* put engines into load program state */
     rc = lp5523_set_engine_control(itf, LP5523_ENGINE_CNTRL2, engine_mask,
         LP5523_ENGINES_LOAD_PROGRAM);
-    if (rc != 0) {
+    if (rc) {
         return rc;
     }
 
@@ -663,53 +749,53 @@ lp5523_set_program(struct sensor_itf *itf, uint8_t 
engine_mask, uint16_t *pgm,
 }
 
 int
-lp5523_get_program(struct sensor_itf *itf, uint16_t *pgm, uint8_t start,
+lp5523_get_program(struct led_itf *itf, uint16_t *pgm, uint8_t start,
     uint8_t size)
 {
     return lp5523_rwv_program(itf, &lp5523_get_page, pgm, start, size);
 }
 
 int
-lp5523_verify_program(struct sensor_itf *itf, uint16_t *pgm, uint8_t start,
+lp5523_verify_program(struct led_itf *itf, uint16_t *pgm, uint8_t start,
     uint8_t size)
 {
     return lp5523_rwv_program(itf, &lp5523_verify_page, pgm, start, size);
 }
 
 int
-lp5523_engines_run(struct sensor_itf *itf, uint8_t engine_mask)
+lp5523_engines_run(struct led_itf *itf, uint8_t engine_mask)
 {
     int rc;
 
     rc = lp5523_set_engine_control(itf, LP5523_ENGINE_CNTRL1, engine_mask,
         LP5523_ENGINES_FREE_RUN);
-    return (rc != 0) ? rc : lp5523_set_engine_control(itf, 
LP5523_ENGINE_CNTRL2,
+    return (rc) ? rc : lp5523_set_engine_control(itf, LP5523_ENGINE_CNTRL2,
         engine_mask, LP5523_ENGINES_RUN_PROGRAM);
 }
 
 int
-lp5523_engines_hold(struct sensor_itf *itf, uint8_t engine_mask)
+lp5523_engines_hold(struct led_itf *itf, uint8_t engine_mask)
 {
     return lp5523_set_engine_control(itf, LP5523_ENGINE_CNTRL1, engine_mask,
         LP5523_ENGINES_HOLD);
 }
 
 int
-lp5523_engines_step(struct sensor_itf *itf, uint8_t engine_mask)
+lp5523_engines_step(struct led_itf *itf, uint8_t engine_mask)
 {
     return lp5523_set_engine_control(itf, LP5523_ENGINE_CNTRL1, engine_mask,
         LP5523_ENGINES_STEP);
 }
 
 int
-lp5523_engines_disable(struct sensor_itf *itf, uint8_t engine_mask)
+lp5523_engines_disable(struct led_itf *itf, uint8_t engine_mask)
 {
     return lp5523_set_engine_control(itf, LP5523_ENGINE_CNTRL2, engine_mask,
         LP5523_ENGINES_DISABLED);
 }
 
 int
-lp5523_read_adc(struct sensor_itf *itf, uint8_t pin, uint8_t *v) {
+lp5523_read_adc(struct led_itf *itf, uint8_t pin, uint8_t *v) {
     int rc;
 
     if (pin > 0x1f) {
@@ -718,7 +804,7 @@ lp5523_read_adc(struct sensor_itf *itf, uint8_t pin, 
uint8_t *v) {
 
     rc = lp5523_set_reg(itf, LP5523_LED_TEST_CONTROL,
         pin | LP5523_EN_LED_TEST_ADC.mask);
-    if (rc != 0) {
+    if (rc) {
         return rc;
     }
 
@@ -728,7 +814,23 @@ lp5523_read_adc(struct sensor_itf *itf, uint8_t pin, 
uint8_t *v) {
 }
 
 int
-lp5523_self_test(struct sensor_itf *itf) {
+lp5523_get_status(struct led_itf *itf, uint8_t *status)
+{
+    uint8_t reg;
+    int rc;
+
+    rc = lp5523_get_reg(itf, LP5523_STATUS, &reg);
+    if (rc) {
+        return rc;
+    }
+
+    *status = reg;
+
+    return 0;
+}
+
+int
+lp5523_self_test(struct led_itf *itf) {
     int rc;
     uint8_t status;
     uint8_t misc;
@@ -736,13 +838,13 @@ lp5523_self_test(struct sensor_itf *itf) {
     uint8_t adc;
     uint8_t i;
 
-    rc = lp5523_get_reg(itf, LP5523_STATUS, &status);
-    if (rc != 0) {
+    rc = lp5523_get_status(itf, &status);
+    if (rc) {
         return rc;
     }
 
     rc = lp5523_get_reg(itf, LP5523_MISC, &misc);
-    if (rc != 0) {
+    if (rc) {
         return rc;
     }
 
@@ -754,7 +856,7 @@ lp5523_self_test(struct sensor_itf *itf) {
 
     /* measure VDD */
     rc = lp5523_read_adc(itf, LP5523_LED_TEST_VDD, &vdd);
-    if (rc != 0) {
+    if (rc) {
         return rc;
     }
 
@@ -762,7 +864,7 @@ lp5523_self_test(struct sensor_itf *itf) {
     while (i <= 9) {
         /* set LED PWM to 0xff */
         rc = lp5523_set_output_reg(itf, LP5523_PWM, i, 0xff);
-        if (rc != 0) {
+        if (rc) {
             return rc;
         }
 
@@ -771,7 +873,7 @@ lp5523_self_test(struct sensor_itf *itf) {
 
         /* read the ADC */
         rc = lp5523_read_adc(itf, i - 1, &adc);
-        if (rc != 0) {
+        if (rc) {
             return rc;
         }
         if ((adc > vdd) || (adc < LP5523_LED_TEST_SC_LIM)) {
@@ -779,7 +881,7 @@ lp5523_self_test(struct sensor_itf *itf) {
         }
 
         rc = lp5523_set_output_reg(itf, LP5523_PWM, i, 0x00);
-        if (rc != 0) {
+        if (rc) {
             return rc;
         }
         ++i;
@@ -791,21 +893,15 @@ lp5523_self_test(struct sensor_itf *itf) {
 int
 lp5523_init(struct os_dev *dev, void *arg)
 {
-    struct lp5523 *lp;
-    struct sensor *sensor;
     int rc;
 
     if (!arg || !dev) {
         return SYS_ENODEV;
     }
 
-    lp = (struct lp5523 *)dev;
-
     log_register(dev->od_name, &_log, &log_console_handler, NULL,
         LOG_SYSLEVEL);
 
-    sensor = &lp->sensor;
-
     /* Initialise the stats entry */
     rc = stats_init(
         STATS_HDR(g_lp5523stats),
@@ -816,62 +912,137 @@ lp5523_init(struct os_dev *dev, void *arg)
     rc = stats_register(dev->od_name, STATS_HDR(g_lp5523stats));
     SYSINIT_PANIC_ASSERT(rc == 0);
 
-    rc = sensor_init(sensor, dev);
-    if (rc != 0) {
-        return rc;
-    }
+    return 0;
+}
 
-    rc = sensor_set_interface(sensor, arg);
-    if (rc != 0) {
-        return rc;
-    }
+int
+lp5523_get_output_curr_ctrl(struct led_itf *itf, uint8_t output, uint8_t 
*curr_ctrl)
+{
+      int rc;
+      uint8_t reg;
+
+      rc = lp5523_get_output_reg(itf, LP5523_CURRENT_CONTROL, output, &reg);
+      if (rc) {
+          goto err;
+      }
+
+      *curr_ctrl = reg;
+err:
+      return rc;
+}
+
+int
+lp5523_set_output_curr_ctrl(struct led_itf *itf, uint8_t output, uint8_t 
curr_ctrl)
+{
 
-    return sensor_mgr_register(sensor);
+      return lp5523_set_output_reg(itf, LP5523_CURRENT_CONTROL, output, 
curr_ctrl);
 }
 
 int
-lp5523_config(struct lp5523 *lp, struct lp5523_cfg *cfg)
+lp5523_config(struct lp5523 *lp5523, struct lp5523_cfg *cfg)
 {
     int rc;
-    struct sensor_itf *itf;
+    int i;
+    struct led_itf *itf;
     uint8_t misc_val;
 
-    itf = SENSOR_GET_ITF(&(lp->sensor));
+    itf = LED_GET_ITF(&(lp5523->led_dev));
 
-    itf->si_addr = LP5523_I2C_BASE_ADDR + cfg->asel;
+    itf->li_addr = LP5523_I2C_BASE_ADDR + cfg->asel;
 
     rc = lp5523_reset(itf);
-    if (rc != 0) {
+    if (rc) {
         return rc;
     }
 
     /* chip enable */
     rc = lp5523_set_enable(itf, 1);
-    if (rc != 0) {
-        return rc;
+    if (rc) {
+        goto err;
     }
 
-    misc_val = LP5523_EN_AUTO_INCR.mask;
+    misc_val = 0;
+    if (cfg->auto_inc_en) {
+        misc_val = LP5523_EN_AUTO_INCR.mask;
+    } else {
+        misc_val = LP5523_CLK_DET_EN.mask;
+    }
 
     rc = lp5523_apply_value(LP5523_CLK_DET_EN, cfg->clk_det_en, &misc_val);
-    if (rc != 0) {
-        return rc;
+    if (rc) {
+        goto err;
     }
 
     rc = lp5523_apply_value(LP5523_INT_CLK_EN, cfg->int_clk_en, &misc_val);
-    if (rc != 0) {
-        return rc;
+    if (rc) {
+        goto err;
+    }
+
+    rc = lp5523_apply_value(LP5523_VARIABLE_D_SEL, cfg->var_d_sel, &misc_val);
+    if (rc) {
+        goto err;
+    }
+
+    rc = lp5523_apply_value(LP5523_POWERSAVE_EN, cfg->ps_en, &misc_val);
+    if (rc) {
+        goto err;
+    }
+
+    rc = lp5523_apply_value(LP5523_PWM_PS_EN, cfg->pwm_ps_en, &misc_val);
+    if (rc) {
+        goto err;
     }
 
     rc = lp5523_apply_value(LP5523_CP_MODE, cfg->cp_mode, &misc_val);
-    if (rc != 0) {
-        return rc;
+    if (rc) {
+        goto err;
     }
 
     rc = lp5523_set_reg(itf, LP5523_MISC, misc_val);
-    if (rc != 0) {
-        return rc;
+    if (rc) {
+        goto err;
     }
 
-    return lp5523_set_value(itf, LP5523_INT_CONF, cfg->int_conf);
+    rc = lp5523_set_value(itf, LP5523_INT_CONF, cfg->int_conf);
+    if (rc) {
+        goto err;
+    }
+
+    rc = lp5523_set_value(itf, LP5523_INT_GPO, cfg->int_gpo);
+    if (rc) {
+        goto err;
+    }
+
+    for (i = 1; i <= MYNEWT_VAL(LEDS_PER_DRIVER); i++) {
+        rc = lp5523_set_output_curr_ctrl(itf, i, cfg->per_led_cfg[i - 
1].current_ctrl);
+        if (rc) {
+            goto err;
+        }
+
+        lp5523_wait(1);
+
+        rc = lp5523_set_output_log_dim(itf, i, cfg->per_led_cfg[i - 
1].log_dim_en);
+        if (rc) {
+            goto err;
+        }
+
+        lp5523_wait(1);
+
+        rc = lp5523_set_output_temp_comp(itf, i, cfg->per_led_cfg[i - 
1].temp_comp);
+        if (rc) {
+            goto err;
+        }
+
+        lp5523_wait(1);
+
+        rc = lp5523_set_output_on(itf, i, cfg->per_led_cfg[i - 1].output_on);
+        if (rc) {
+            goto err;
+        }
+
+        lp5523_wait(1);
+    }
+
+err:
+    return rc;
 }
diff --git a/hw/drivers/lp5523/src/lp5523_shell.c 
b/hw/drivers/lp5523/src/lp5523_shell.c
index 991414a..43cd5cd 100644
--- a/hw/drivers/lp5523/src/lp5523_shell.c
+++ b/hw/drivers/lp5523/src/lp5523_shell.c
@@ -36,10 +36,10 @@ static struct shell_cmd lp5523_shell_cmd_struct = {
     .sc_cmd_func = lp5523_shell_cmd
 };
 
-static struct sensor_itf lp5523_itf = {
-    .si_type = MYNEWT_VAL(LP5523_SHELL_ITF_TYPE),
-    .si_num = MYNEWT_VAL(LP5523_SHELL_ITF_NUM),
-    .si_addr = MYNEWT_VAL(LP5523_SHELL_ITF_ADDR)
+static struct led_itf lp5523_itf = {
+    .li_type = MYNEWT_VAL(LP5523_SHELL_ITF_TYPE),
+    .li_num = MYNEWT_VAL(LP5523_SHELL_ITF_NUM),
+    .li_addr = MYNEWT_VAL(LP5523_SHELL_ITF_ADDR)
 };
 
 static int
diff --git a/hw/drivers/lp5523/syscfg.yml b/hw/drivers/lp5523/syscfg.yml
index 416f0a4..01df753 100644
--- a/hw/drivers/lp5523/syscfg.yml
+++ b/hw/drivers/lp5523/syscfg.yml
@@ -29,4 +29,7 @@ syscfg.defs:
         value: 0
     LP5523_SHELL_ITF_ADDR:
         description: 'LP5523 I2C Address'
-        value: 0x32
\ No newline at end of file
+        value: 0x32
+    LP5523_LEDS_PER_DRIVER:
+        description: 'LP5523 LEDs per driver'
+        value: 5

-- 
To stop receiving notification emails like this one, please contact
[email protected].

Reply via email to