1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2014 Google, Inc
11 static int cur_busnum __attribute__((section(".data")));
13 static int i2c_compat_get_device(uint chip_addr, int alen,
14 struct udevice **devp)
16 struct dm_i2c_chip *chip;
19 ret = i2c_get_chip_for_busnum(cur_busnum, chip_addr, alen, devp);
22 chip = dev_get_parent_platdata(*devp);
23 if (chip->offset_len != alen) {
24 printf("I2C chip %x: requested alen %d does not match chip offset_len %d\n",
25 chip_addr, alen, chip->offset_len);
26 return -EADDRNOTAVAIL;
32 int i2c_probe(uint8_t chip_addr)
34 struct udevice *bus, *dev;
37 ret = uclass_get_device_by_seq(UCLASS_I2C, cur_busnum, &bus);
39 debug("Cannot find I2C bus %d: err=%d\n", cur_busnum, ret);
46 return dm_i2c_probe(bus, chip_addr, 0, &dev);
49 int i2c_read(uint8_t chip_addr, unsigned int addr, int alen, uint8_t *buffer,
55 ret = i2c_compat_get_device(chip_addr, alen, &dev);
59 return dm_i2c_read(dev, addr, buffer, len);
62 int i2c_write(uint8_t chip_addr, unsigned int addr, int alen, uint8_t *buffer,
68 ret = i2c_compat_get_device(chip_addr, alen, &dev);
72 return dm_i2c_write(dev, addr, buffer, len);
75 int i2c_get_bus_num_fdt(int node)
80 ret = uclass_get_device_by_of_offset(UCLASS_I2C, node, &bus);
87 unsigned int i2c_get_bus_num(void)
92 int i2c_set_bus_num(unsigned int bus)
99 void i2c_init(int speed, int slaveaddr)
101 /* Nothing to do here - the init happens through driver model */
104 void board_i2c_init(const void *blob)
106 /* Nothing to do here - the init happens through driver model */
109 uint8_t i2c_reg_read(uint8_t chip_addr, uint8_t offset)
114 ret = i2c_compat_get_device(chip_addr, 1, &dev);
117 return dm_i2c_reg_read(dev, offset);
120 void i2c_reg_write(uint8_t chip_addr, uint8_t offset, uint8_t val)
125 ret = i2c_compat_get_device(chip_addr, 1, &dev);
127 dm_i2c_reg_write(dev, offset, val);