You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by an...@apache.org on 2018/12/03 12:42:22 UTC

[mynewt-core] 11/15: hw/drivers: Add bus driver support to ADP5061

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

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

commit 14d33a05af4e7a1fc21eb9fef9f1c51a968fcb37
Author: Andrzej Kaczmarek <an...@codecoup.pl>
AuthorDate: Fri Nov 23 17:40:23 2018 +0100

    hw/drivers: Add bus driver support to ADP5061
---
 .../chg_ctrl/adp5061/include/adp5061/adp5061.h     | 23 +++++++
 hw/drivers/chg_ctrl/adp5061/src/adp5061.c          | 78 +++++++++++++++++-----
 2 files changed, 86 insertions(+), 15 deletions(-)

diff --git a/hw/drivers/chg_ctrl/adp5061/include/adp5061/adp5061.h b/hw/drivers/chg_ctrl/adp5061/include/adp5061/adp5061.h
index 0e7e999..242ed0c 100644
--- a/hw/drivers/chg_ctrl/adp5061/include/adp5061/adp5061.h
+++ b/hw/drivers/chg_ctrl/adp5061/include/adp5061/adp5061.h
@@ -25,6 +25,10 @@
 #include "syscfg/syscfg.h"
 #include "os/os_time.h"
 #include "charge-control/charge_control.h"
+#if MYNEWT_VAL(BUS_DRIVER_PRESENT)
+#include "bus/bus_driver.h"
+#include "bus/i2c.h"
+#endif
 
 /**
 * Struct for ADP50961 configuration
@@ -43,7 +47,11 @@ struct adp5061_config {
 };
 
 struct adp5061_dev {
+#if MYNEWT_VAL(BUS_DRIVER_PRESENT)
+    struct bus_i2c_node     a_node;
+#else
     struct os_dev           a_dev;
+#endif
     struct charge_control   a_chg_ctrl;
     struct adp5061_config   a_cfg;
     os_time_t               a_last_read_time;
@@ -979,4 +987,19 @@ int adp5061_set_regs(struct adp5061_dev *dev, uint8_t addr,
 #define ADP5061_SYS_EN_SET(a)           ((a & ((1 << ADP5061_SYS_EN_LEN)-1)) \
                                             << ADP5061_SYS_EN_OFFSET)
 
+#if MYNEWT_VAL(BUS_DRIVER_PRESENT)
+/**
+ * Create I2C bus node for ADP5061
+ *
+ * @param node  Bus node
+ * @param name  Device name
+ * @param cfg   I2C node configuration
+ *
+ * @return 0 on success, non-zero on failure
+ */
+int
+adp5061_create_i2c_dev(struct bus_i2c_node *node, const char *name,
+                       const struct bus_i2c_node_cfg *cfg);
+#endif
+
 #endif /* _ADP5061_H */
diff --git a/hw/drivers/chg_ctrl/adp5061/src/adp5061.c b/hw/drivers/chg_ctrl/adp5061/src/adp5061.c
index 6af79d1..8175070 100644
--- a/hw/drivers/chg_ctrl/adp5061/src/adp5061.c
+++ b/hw/drivers/chg_ctrl/adp5061/src/adp5061.c
@@ -23,13 +23,18 @@
 
 #include <os/mynewt.h>
 #include <console/console.h>
-#include <hal/hal_i2c.h>
 #include <mcu/nrf52_hal.h>
 #include <hal/hal_gpio.h>
+#if MYNEWT_VAL(BUS_DRIVER_PRESENT)
+#include "bus/bus.h"
+#include "bus/i2c.h"
+#else
+#include "hal/hal_i2c.h"
+#include "i2cn/i2cn.h"
+#endif
 #include <adp5061/adp5061.h>
 #include <bsp/bsp.h>
 #include <charge-control/charge_control.h>
-#include <i2cn/i2cn.h>
 #include "adp5061_priv.h"
 
 /**
@@ -139,6 +144,7 @@ adp5061_set_config(struct adp5061_dev *dev,
     return rc;
 }
 
+#if !MYNEWT_VAL(BUS_DRIVER_PRESENT)
 /**
  * Lock access to the charge_control_itf specified by cci. Blocks until lock acquired.
  *
@@ -186,11 +192,17 @@ adp5061_itf_unlock(struct charge_control_itf *cci)
 
     os_mutex_release(cci->cci_lock);
 }
+#endif
 
 int
 adp5061_get_reg(struct adp5061_dev *dev, uint8_t addr, uint8_t *value)
 {
-    int rc = 0;
+    int rc;
+
+#if MYNEWT_VAL(BUS_DRIVER_PRESENT)
+    rc = bus_node_simple_write_read_transact((struct os_dev *)&dev->a_node,
+                                             &addr, 1, value, 1);
+#else
     uint8_t payload;
     struct hal_i2c_master_data data_struct = {
         .address = dev->a_chg_ctrl.cc_itf.cci_addr,
@@ -219,6 +231,7 @@ adp5061_get_reg(struct adp5061_dev *dev, uint8_t addr, uint8_t *value)
 
 err:
     adp5061_itf_unlock(&dev->a_chg_ctrl.cc_itf);
+#endif
 
     return rc;
 }
@@ -226,8 +239,12 @@ err:
 int
 adp5061_set_reg(struct adp5061_dev *dev, uint8_t addr, uint8_t value)
 {
-    int rc = 0;
+    int rc;
     uint8_t payload[2] = { addr, value };
+
+#if MYNEWT_VAL(BUS_DRIVER_PRESENT)
+    rc = bus_node_simple_write((struct os_dev *)&dev->a_node, payload, 2);
+#else
     struct hal_i2c_master_data data_struct = {
         .address = dev->a_chg_ctrl.cc_itf.cci_addr,
         .len = 2,
@@ -243,6 +260,7 @@ adp5061_set_reg(struct adp5061_dev *dev, uint8_t addr, uint8_t value)
             MYNEWT_VAL(ADP5061_I2C_TIMEOUT_TICKS), 1, MYNEWT_VAL(ADP5061_I2C_RETRIES));
 
     adp5061_itf_unlock(&dev->a_chg_ctrl.cc_itf);
+#endif
 
     return rc;
 }
@@ -251,20 +269,24 @@ int
 adp5061_set_regs(struct adp5061_dev *dev, uint8_t addr,
         const uint8_t *values, int count)
 {
-    int rc = 0;
-    int i;
     uint8_t payload[1 + count];
-    struct hal_i2c_master_data data_struct = {
-        .address = dev->a_chg_ctrl.cc_itf.cci_addr,
-        .len = count + 1,
-        .buffer = payload
-    };
+    int i;
+    int rc;
 
     payload[0] = addr;
     for (i = 0; i < count; ++i) {
         payload[i + 1] = values[i];
     }
 
+#if MYNEWT_VAL(BUS_DRIVER_PRESENT)
+    rc = bus_node_simple_write((struct os_dev *)&dev->a_node, payload, count + 1);
+#else
+    struct hal_i2c_master_data data_struct = {
+        .address = dev->a_chg_ctrl.cc_itf.cci_addr,
+        .len = count + 1,
+        .buffer = payload
+    };
+
     rc = ad5061_itf_lock(&dev->a_chg_ctrl.cc_itf, OS_TIMEOUT_NEVER);
     if (rc) {
         return rc;
@@ -274,6 +296,7 @@ adp5061_set_regs(struct adp5061_dev *dev, uint8_t addr,
             MYNEWT_VAL(ADP5061_I2C_TIMEOUT_TICKS), 1, MYNEWT_VAL(ADP5061_I2C_RETRIES));
 
     adp5061_itf_unlock(&dev->a_chg_ctrl.cc_itf);
+#endif
 
     return rc;
 }
@@ -585,7 +608,6 @@ adp5061_init(struct os_dev *dev, void *arg)
 {
     struct adp5061_dev *adp5061 = (struct adp5061_dev *)dev;
     struct charge_control *cc;
-    struct charge_control_itf *cci;
     const struct adp5061_config *cfg;
     uint8_t device_id;
     int rc;
@@ -597,14 +619,14 @@ adp5061_init(struct os_dev *dev, void *arg)
 
     cc = &adp5061->a_chg_ctrl;
 
-    cci = (struct charge_control_itf *)arg;
-
     rc = charge_control_init(cc, dev);
     if (rc) {
         goto err;
     }
 
-    charge_control_set_interface(cc, cci);
+#if !MYNEWT_VAL(BUS_DRIVER_PRESENT)
+    charge_control_set_interface(cc, (struct charge_control_itf *)arg);
+#endif
 
     /* Add the driver with all the supported types */
     rc = charge_control_set_driver(cc, CHARGE_CONTROL_TYPE_STATUS,
@@ -649,3 +671,29 @@ adp5061_init(struct os_dev *dev, void *arg)
 err:
     return rc;
 }
+
+#if MYNEWT_VAL(BUS_DRIVER_PRESENT)
+static void
+init_node_cb(struct bus_node *bnode, void *arg)
+{
+    assert(arg == NULL);
+
+    adp5061_init((struct os_dev *)bnode, NULL);
+}
+
+int
+adp5061_create_i2c_dev(struct bus_i2c_node *node, const char *name,
+                       const struct bus_i2c_node_cfg *cfg)
+{
+    struct bus_node_callbacks cbs = {
+        .init = init_node_cb,
+    };
+    int rc;
+
+    bus_node_set_callbacks((struct os_dev *)node, &cbs);
+
+    rc = bus_i2c_node_create(name, node, cfg, NULL);
+
+    return rc;
+}
+#endif