]> git.ipfire.org Git - people/ms/u-boot.git/commitdiff
dm: i2c: Add a compatbility layer
authorSimon Glass <sjg@chromium.org>
Tue, 13 Jan 2015 01:02:08 +0000 (18:02 -0700)
committerSimon Glass <sjg@chromium.org>
Fri, 30 Jan 2015 00:09:53 +0000 (17:09 -0700)
For boards which use multiple I2C devices, or for SOCs which support
multiple boards, we might want to convert these to driver model at different
times. At present this is difficult because we need to either use
CONFIG_DM_I2C for a board or not.

Add a compatibility layer which implements the old API, thus allowing a
board to move to driver model for I2C without requiring that everything it
uses is moved in the same commit.

Signed-off-by: Simon Glass <sjg@chromium.org>
Makefile
drivers/i2c/Makefile
drivers/i2c/i2c-uclass-compat.c [new file with mode: 0644]
include/i2c.h

index ea5ae8fac72f3b0c8f1c7807b4fc4dfaeca15ea1..9b406c844700f9674f4a787cb95019e6d7d4c115 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -776,6 +776,13 @@ ifneq ($(CONFIG_SYS_GENERIC_BOARD),y)
        @echo "See doc/README.generic-board for further information"
        @echo "===================================================="
 endif
+ifeq ($(CONFIG_DM_I2C_COMPAT),y)
+       @echo "===================== WARNING ======================"
+       @echo "This board uses CONFIG_DM_I2C_COMPAT. Please remove"
+       @echo "(possibly in a subsequent patch in your series)"
+       @echo "before sending patches to the mailing list."
+       @echo "===================================================="
+endif
 
 PHONY += dtbs
 dtbs dts/dt.dtb: checkdtc u-boot
index 0e4c9f466a145e2fdbaf578867b59f6adc3d9c73..774bc94a4a7a864acfbdb6a8a5214f34637f7fe7 100644 (file)
@@ -5,6 +5,7 @@
 # SPDX-License-Identifier:     GPL-2.0+
 #
 obj-$(CONFIG_DM_I2C) += i2c-uclass.o
+obj-$(CONFIG_DM_I2C_COMPAT) += i2c-uclass-compat.o
 
 obj-$(CONFIG_SYS_I2C_ADI) += adi_i2c.o
 obj-$(CONFIG_I2C_MV) += mv_i2c.o
diff --git a/drivers/i2c/i2c-uclass-compat.c b/drivers/i2c/i2c-uclass-compat.c
new file mode 100644 (file)
index 0000000..c29fc89
--- /dev/null
@@ -0,0 +1,98 @@
+/*
+ * Copyright (c) 2014 Google, Inc
+ *
+ * SPDX-License-Identifier:    GPL-2.0+
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <errno.h>
+#include <i2c.h>
+
+static int cur_busnum;
+
+static int i2c_compat_get_device(uint chip_addr, int alen,
+                                struct udevice **devp)
+{
+       struct dm_i2c_chip *chip;
+       int ret;
+
+       ret = i2c_get_chip_for_busnum(cur_busnum, chip_addr, devp);
+       if (ret)
+               return ret;
+       chip = dev_get_parentdata(*devp);
+       if (chip->offset_len != alen) {
+               printf("Requested alen %d does not match chip offset_len %d\n",
+                      alen, chip->offset_len);
+               return -EADDRNOTAVAIL;
+       }
+
+       return 0;
+}
+
+int i2c_probe(uint8_t chip_addr)
+{
+       struct udevice *bus, *dev;
+       int ret;
+
+       ret = uclass_get_device_by_seq(UCLASS_I2C, cur_busnum, &bus);
+       if (ret) {
+               debug("Cannot find I2C bus %d: err=%d\n", cur_busnum, ret);
+               return ret;
+       }
+
+       if (!bus)
+               return -ENOENT;
+
+       return dm_i2c_probe(bus, chip_addr, 0, &dev);
+}
+
+int i2c_read(uint8_t chip_addr, unsigned int addr, int alen, uint8_t *buffer,
+            int len)
+{
+       struct udevice *dev;
+       int ret;
+
+       ret = i2c_compat_get_device(chip_addr, alen, &dev);
+       if (ret)
+               return ret;
+
+       return dm_i2c_read(dev, addr, buffer, len);
+}
+
+int i2c_write(uint8_t chip_addr, unsigned int addr, int alen, uint8_t *buffer,
+             int len)
+{
+       struct udevice *dev;
+       int ret;
+
+       ret = i2c_compat_get_device(chip_addr, alen, &dev);
+       if (ret)
+               return ret;
+
+       return dm_i2c_write(dev, addr, buffer, len);
+}
+
+int i2c_get_bus_num_fdt(int node)
+{
+       struct udevice *bus;
+       int ret;
+
+       ret = uclass_get_device_by_of_offset(UCLASS_I2C, node, &bus);
+       if (ret)
+               return ret;
+
+       return bus->seq;
+}
+
+unsigned int i2c_get_bus_num(void)
+{
+       return cur_busnum;
+}
+
+int i2c_set_bus_num(unsigned int bus)
+{
+       cur_busnum = bus;
+
+       return 0;
+}
index 30d53c8a9df4cb38ca0d97e2c47aa8ea2f231a1c..47529f4b283fe1b1c73157148cb615480f46f15a 100644 (file)
@@ -184,6 +184,65 @@ int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len);
  */
 int i2c_deblock(struct udevice *bus);
 
+#ifdef CONFIG_DM_I2C_COMPAT
+/**
+ * i2c_probe() - Compatibility function for driver model
+ *
+ * Calls dm_i2c_probe() on the current bus
+ */
+int i2c_probe(uint8_t chip_addr);
+
+/**
+ * i2c_read() - Compatibility function for driver model
+ *
+ * Calls dm_i2c_read() with the device corresponding to @chip_addr, and offset
+ * set to @addr. @alen must match the current setting for the device.
+ */
+int i2c_read(uint8_t chip_addr, unsigned int addr, int alen, uint8_t *buffer,
+            int len);
+
+/**
+ * i2c_write() - Compatibility function for driver model
+ *
+ * Calls dm_i2c_write() with the device corresponding to @chip_addr, and offset
+ * set to @addr. @alen must match the current setting for the device.
+ */
+int i2c_write(uint8_t chip_addr, unsigned int addr, int alen, uint8_t *buffer,
+             int len);
+
+/**
+ * i2c_get_bus_num_fdt() - Compatibility function for driver model
+ *
+ * @return the bus number associated with the given device tree node
+ */
+int i2c_get_bus_num_fdt(int node);
+
+/**
+ * i2c_get_bus_num() - Compatibility function for driver model
+ *
+ * @return the 'current' bus number
+ */
+unsigned int i2c_get_bus_num(void);
+
+/**
+ * i2c_set_bus_num(): Compatibility function for driver model
+ *
+ * Sets the 'current' bus
+ */
+int i2c_set_bus_num(unsigned int bus);
+
+static inline void I2C_SET_BUS(unsigned int bus)
+{
+       i2c_set_bus_num(bus);
+}
+
+static inline unsigned int I2C_GET_BUS(void)
+{
+       return i2c_get_bus_num();
+}
+
+#endif
+
 /*
  * Not all of these flags are implemented in the U-Boot API
  */