Commit 73845350b6281a7afeeb279475e6eb613d7a89f9

Authored by Simon Glass
1 parent f9a4c2da72

dm: i2c: Add a compatbility layer

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>

Showing 4 changed files with 165 additions and 0 deletions Side-by-side Diff

... ... @@ -776,6 +776,13 @@
776 776 @echo "See doc/README.generic-board for further information"
777 777 @echo "===================================================="
778 778 endif
  779 +ifeq ($(CONFIG_DM_I2C_COMPAT),y)
  780 + @echo "===================== WARNING ======================"
  781 + @echo "This board uses CONFIG_DM_I2C_COMPAT. Please remove"
  782 + @echo "(possibly in a subsequent patch in your series)"
  783 + @echo "before sending patches to the mailing list."
  784 + @echo "===================================================="
  785 +endif
779 786  
780 787 PHONY += dtbs
781 788 dtbs dts/dt.dtb: checkdtc u-boot
drivers/i2c/Makefile
... ... @@ -5,6 +5,7 @@
5 5 # SPDX-License-Identifier: GPL-2.0+
6 6 #
7 7 obj-$(CONFIG_DM_I2C) += i2c-uclass.o
  8 +obj-$(CONFIG_DM_I2C_COMPAT) += i2c-uclass-compat.o
8 9  
9 10 obj-$(CONFIG_SYS_I2C_ADI) += adi_i2c.o
10 11 obj-$(CONFIG_I2C_MV) += mv_i2c.o
drivers/i2c/i2c-uclass-compat.c
  1 +/*
  2 + * Copyright (c) 2014 Google, Inc
  3 + *
  4 + * SPDX-License-Identifier: GPL-2.0+
  5 + */
  6 +
  7 +#include <common.h>
  8 +#include <dm.h>
  9 +#include <errno.h>
  10 +#include <i2c.h>
  11 +
  12 +static int cur_busnum;
  13 +
  14 +static int i2c_compat_get_device(uint chip_addr, int alen,
  15 + struct udevice **devp)
  16 +{
  17 + struct dm_i2c_chip *chip;
  18 + int ret;
  19 +
  20 + ret = i2c_get_chip_for_busnum(cur_busnum, chip_addr, devp);
  21 + if (ret)
  22 + return ret;
  23 + chip = dev_get_parentdata(*devp);
  24 + if (chip->offset_len != alen) {
  25 + printf("Requested alen %d does not match chip offset_len %d\n",
  26 + alen, chip->offset_len);
  27 + return -EADDRNOTAVAIL;
  28 + }
  29 +
  30 + return 0;
  31 +}
  32 +
  33 +int i2c_probe(uint8_t chip_addr)
  34 +{
  35 + struct udevice *bus, *dev;
  36 + int ret;
  37 +
  38 + ret = uclass_get_device_by_seq(UCLASS_I2C, cur_busnum, &bus);
  39 + if (ret) {
  40 + debug("Cannot find I2C bus %d: err=%d\n", cur_busnum, ret);
  41 + return ret;
  42 + }
  43 +
  44 + if (!bus)
  45 + return -ENOENT;
  46 +
  47 + return dm_i2c_probe(bus, chip_addr, 0, &dev);
  48 +}
  49 +
  50 +int i2c_read(uint8_t chip_addr, unsigned int addr, int alen, uint8_t *buffer,
  51 + int len)
  52 +{
  53 + struct udevice *dev;
  54 + int ret;
  55 +
  56 + ret = i2c_compat_get_device(chip_addr, alen, &dev);
  57 + if (ret)
  58 + return ret;
  59 +
  60 + return dm_i2c_read(dev, addr, buffer, len);
  61 +}
  62 +
  63 +int i2c_write(uint8_t chip_addr, unsigned int addr, int alen, uint8_t *buffer,
  64 + int len)
  65 +{
  66 + struct udevice *dev;
  67 + int ret;
  68 +
  69 + ret = i2c_compat_get_device(chip_addr, alen, &dev);
  70 + if (ret)
  71 + return ret;
  72 +
  73 + return dm_i2c_write(dev, addr, buffer, len);
  74 +}
  75 +
  76 +int i2c_get_bus_num_fdt(int node)
  77 +{
  78 + struct udevice *bus;
  79 + int ret;
  80 +
  81 + ret = uclass_get_device_by_of_offset(UCLASS_I2C, node, &bus);
  82 + if (ret)
  83 + return ret;
  84 +
  85 + return bus->seq;
  86 +}
  87 +
  88 +unsigned int i2c_get_bus_num(void)
  89 +{
  90 + return cur_busnum;
  91 +}
  92 +
  93 +int i2c_set_bus_num(unsigned int bus)
  94 +{
  95 + cur_busnum = bus;
  96 +
  97 + return 0;
  98 +}
... ... @@ -184,6 +184,65 @@
184 184 */
185 185 int i2c_deblock(struct udevice *bus);
186 186  
  187 +#ifdef CONFIG_DM_I2C_COMPAT
  188 +/**
  189 + * i2c_probe() - Compatibility function for driver model
  190 + *
  191 + * Calls dm_i2c_probe() on the current bus
  192 + */
  193 +int i2c_probe(uint8_t chip_addr);
  194 +
  195 +/**
  196 + * i2c_read() - Compatibility function for driver model
  197 + *
  198 + * Calls dm_i2c_read() with the device corresponding to @chip_addr, and offset
  199 + * set to @addr. @alen must match the current setting for the device.
  200 + */
  201 +int i2c_read(uint8_t chip_addr, unsigned int addr, int alen, uint8_t *buffer,
  202 + int len);
  203 +
  204 +/**
  205 + * i2c_write() - Compatibility function for driver model
  206 + *
  207 + * Calls dm_i2c_write() with the device corresponding to @chip_addr, and offset
  208 + * set to @addr. @alen must match the current setting for the device.
  209 + */
  210 +int i2c_write(uint8_t chip_addr, unsigned int addr, int alen, uint8_t *buffer,
  211 + int len);
  212 +
  213 +/**
  214 + * i2c_get_bus_num_fdt() - Compatibility function for driver model
  215 + *
  216 + * @return the bus number associated with the given device tree node
  217 + */
  218 +int i2c_get_bus_num_fdt(int node);
  219 +
  220 +/**
  221 + * i2c_get_bus_num() - Compatibility function for driver model
  222 + *
  223 + * @return the 'current' bus number
  224 + */
  225 +unsigned int i2c_get_bus_num(void);
  226 +
  227 +/**
  228 + * i2c_set_bus_num(): Compatibility function for driver model
  229 + *
  230 + * Sets the 'current' bus
  231 + */
  232 +int i2c_set_bus_num(unsigned int bus);
  233 +
  234 +static inline void I2C_SET_BUS(unsigned int bus)
  235 +{
  236 + i2c_set_bus_num(bus);
  237 +}
  238 +
  239 +static inline unsigned int I2C_GET_BUS(void)
  240 +{
  241 + return i2c_get_bus_num();
  242 +}
  243 +
  244 +#endif
  245 +
187 246 /*
188 247 * Not all of these flags are implemented in the U-Boot API
189 248 */