183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2c6202d85SSimon Glass /*
3c6202d85SSimon Glass * Copyright (c) 2014 Google, Inc
4c6202d85SSimon Glass */
5c6202d85SSimon Glass
6c6202d85SSimon Glass #include <common.h>
7c6202d85SSimon Glass #include <dm.h>
8c6202d85SSimon Glass #include <errno.h>
9c6202d85SSimon Glass #include <i2c.h>
10c6202d85SSimon Glass #include <malloc.h>
11c6202d85SSimon Glass #include <dm/device-internal.h>
12c6202d85SSimon Glass #include <dm/lists.h>
13aa54192dSAlexander Kochetkov #include <dm/pinctrl.h>
14aa54192dSAlexander Kochetkov #ifdef CONFIG_DM_GPIO
15aa54192dSAlexander Kochetkov #include <asm/gpio.h>
16aa54192dSAlexander Kochetkov #endif
17c6202d85SSimon Glass
18c6202d85SSimon Glass #define I2C_MAX_OFFSET_LEN 4
19c6202d85SSimon Glass
20aa54192dSAlexander Kochetkov enum {
21aa54192dSAlexander Kochetkov PIN_SDA = 0,
22aa54192dSAlexander Kochetkov PIN_SCL,
23aa54192dSAlexander Kochetkov PIN_COUNT,
24aa54192dSAlexander Kochetkov };
25aa54192dSAlexander Kochetkov
267d7db222SSimon Glass /* Useful debugging function */
i2c_dump_msgs(struct i2c_msg * msg,int nmsgs)277d7db222SSimon Glass void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs)
287d7db222SSimon Glass {
297d7db222SSimon Glass int i;
307d7db222SSimon Glass
317d7db222SSimon Glass for (i = 0; i < nmsgs; i++) {
327d7db222SSimon Glass struct i2c_msg *m = &msg[i];
337d7db222SSimon Glass
347d7db222SSimon Glass printf(" %s %x len=%x", m->flags & I2C_M_RD ? "R" : "W",
357d7db222SSimon Glass msg->addr, msg->len);
367d7db222SSimon Glass if (!(m->flags & I2C_M_RD))
377d7db222SSimon Glass printf(": %x", m->buf[0]);
387d7db222SSimon Glass printf("\n");
397d7db222SSimon Glass }
407d7db222SSimon Glass }
417d7db222SSimon Glass
42c6202d85SSimon Glass /**
43c6202d85SSimon Glass * i2c_setup_offset() - Set up a new message with a chip offset
44c6202d85SSimon Glass *
45c6202d85SSimon Glass * @chip: Chip to use
46c6202d85SSimon Glass * @offset: Byte offset within chip
47c6202d85SSimon Glass * @offset_buf: Place to put byte offset
48c6202d85SSimon Glass * @msg: Message buffer
49c6202d85SSimon Glass * @return 0 if OK, -EADDRNOTAVAIL if the offset length is 0. In that case the
50c6202d85SSimon Glass * message is still set up but will not contain an offset.
51c6202d85SSimon Glass */
i2c_setup_offset(struct dm_i2c_chip * chip,uint offset,uint8_t offset_buf[],struct i2c_msg * msg)52c6202d85SSimon Glass static int i2c_setup_offset(struct dm_i2c_chip *chip, uint offset,
53c6202d85SSimon Glass uint8_t offset_buf[], struct i2c_msg *msg)
54c6202d85SSimon Glass {
55c6202d85SSimon Glass int offset_len;
56c6202d85SSimon Glass
57c6202d85SSimon Glass msg->addr = chip->chip_addr;
58c6202d85SSimon Glass msg->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
59c6202d85SSimon Glass msg->len = chip->offset_len;
60c6202d85SSimon Glass msg->buf = offset_buf;
61c6202d85SSimon Glass if (!chip->offset_len)
62c6202d85SSimon Glass return -EADDRNOTAVAIL;
63c6202d85SSimon Glass assert(chip->offset_len <= I2C_MAX_OFFSET_LEN);
64c6202d85SSimon Glass offset_len = chip->offset_len;
65c6202d85SSimon Glass while (offset_len--)
66c6202d85SSimon Glass *offset_buf++ = offset >> (8 * offset_len);
67c6202d85SSimon Glass
68c6202d85SSimon Glass return 0;
69c6202d85SSimon Glass }
70c6202d85SSimon Glass
i2c_read_bytewise(struct udevice * dev,uint offset,uint8_t * buffer,int len)71c6202d85SSimon Glass static int i2c_read_bytewise(struct udevice *dev, uint offset,
72c6202d85SSimon Glass uint8_t *buffer, int len)
73c6202d85SSimon Glass {
74e6f66ec0SSimon Glass struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
75c6202d85SSimon Glass struct udevice *bus = dev_get_parent(dev);
76c6202d85SSimon Glass struct dm_i2c_ops *ops = i2c_get_ops(bus);
77c6202d85SSimon Glass struct i2c_msg msg[2], *ptr;
78c6202d85SSimon Glass uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
79c6202d85SSimon Glass int ret;
80c6202d85SSimon Glass int i;
81c6202d85SSimon Glass
82c6202d85SSimon Glass for (i = 0; i < len; i++) {
83c6202d85SSimon Glass if (i2c_setup_offset(chip, offset + i, offset_buf, msg))
84c6202d85SSimon Glass return -EINVAL;
85c6202d85SSimon Glass ptr = msg + 1;
86c6202d85SSimon Glass ptr->addr = chip->chip_addr;
87c6202d85SSimon Glass ptr->flags = msg->flags | I2C_M_RD;
88c6202d85SSimon Glass ptr->len = 1;
89c6202d85SSimon Glass ptr->buf = &buffer[i];
90c6202d85SSimon Glass ptr++;
91c6202d85SSimon Glass
92c6202d85SSimon Glass ret = ops->xfer(bus, msg, ptr - msg);
93c6202d85SSimon Glass if (ret)
94c6202d85SSimon Glass return ret;
95c6202d85SSimon Glass }
96c6202d85SSimon Glass
97c6202d85SSimon Glass return 0;
98c6202d85SSimon Glass }
99c6202d85SSimon Glass
i2c_write_bytewise(struct udevice * dev,uint offset,const uint8_t * buffer,int len)100c6202d85SSimon Glass static int i2c_write_bytewise(struct udevice *dev, uint offset,
101c6202d85SSimon Glass const uint8_t *buffer, int len)
102c6202d85SSimon Glass {
103e6f66ec0SSimon Glass struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
104c6202d85SSimon Glass struct udevice *bus = dev_get_parent(dev);
105c6202d85SSimon Glass struct dm_i2c_ops *ops = i2c_get_ops(bus);
106c6202d85SSimon Glass struct i2c_msg msg[1];
107c6202d85SSimon Glass uint8_t buf[I2C_MAX_OFFSET_LEN + 1];
108c6202d85SSimon Glass int ret;
109c6202d85SSimon Glass int i;
110c6202d85SSimon Glass
111c6202d85SSimon Glass for (i = 0; i < len; i++) {
112c6202d85SSimon Glass if (i2c_setup_offset(chip, offset + i, buf, msg))
113c6202d85SSimon Glass return -EINVAL;
114c6202d85SSimon Glass buf[msg->len++] = buffer[i];
115c6202d85SSimon Glass
116c6202d85SSimon Glass ret = ops->xfer(bus, msg, 1);
117c6202d85SSimon Glass if (ret)
118c6202d85SSimon Glass return ret;
119c6202d85SSimon Glass }
120c6202d85SSimon Glass
121c6202d85SSimon Glass return 0;
122c6202d85SSimon Glass }
123c6202d85SSimon Glass
dm_i2c_read(struct udevice * dev,uint offset,uint8_t * buffer,int len)124f9a4c2daSSimon Glass int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len)
125c6202d85SSimon Glass {
126e6f66ec0SSimon Glass struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
127c6202d85SSimon Glass struct udevice *bus = dev_get_parent(dev);
128c6202d85SSimon Glass struct dm_i2c_ops *ops = i2c_get_ops(bus);
129c6202d85SSimon Glass struct i2c_msg msg[2], *ptr;
130c6202d85SSimon Glass uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
131c6202d85SSimon Glass int msg_count;
132c6202d85SSimon Glass
133c6202d85SSimon Glass if (!ops->xfer)
134c6202d85SSimon Glass return -ENOSYS;
135c6202d85SSimon Glass if (chip->flags & DM_I2C_CHIP_RD_ADDRESS)
136c6202d85SSimon Glass return i2c_read_bytewise(dev, offset, buffer, len);
137c6202d85SSimon Glass ptr = msg;
138c6202d85SSimon Glass if (!i2c_setup_offset(chip, offset, offset_buf, ptr))
139c6202d85SSimon Glass ptr++;
140c6202d85SSimon Glass
141c6202d85SSimon Glass if (len) {
142c6202d85SSimon Glass ptr->addr = chip->chip_addr;
143c6202d85SSimon Glass ptr->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
144c6202d85SSimon Glass ptr->flags |= I2C_M_RD;
145c6202d85SSimon Glass ptr->len = len;
146c6202d85SSimon Glass ptr->buf = buffer;
147c6202d85SSimon Glass ptr++;
148c6202d85SSimon Glass }
149c6202d85SSimon Glass msg_count = ptr - msg;
150c6202d85SSimon Glass
151c6202d85SSimon Glass return ops->xfer(bus, msg, msg_count);
152c6202d85SSimon Glass }
153c6202d85SSimon Glass
dm_i2c_write(struct udevice * dev,uint offset,const uint8_t * buffer,int len)154f9a4c2daSSimon Glass int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
155f9a4c2daSSimon Glass int len)
156c6202d85SSimon Glass {
157e6f66ec0SSimon Glass struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
158c6202d85SSimon Glass struct udevice *bus = dev_get_parent(dev);
159c6202d85SSimon Glass struct dm_i2c_ops *ops = i2c_get_ops(bus);
160c6202d85SSimon Glass struct i2c_msg msg[1];
161c6202d85SSimon Glass
162c6202d85SSimon Glass if (!ops->xfer)
163c6202d85SSimon Glass return -ENOSYS;
164c6202d85SSimon Glass
165c6202d85SSimon Glass if (chip->flags & DM_I2C_CHIP_WR_ADDRESS)
166c6202d85SSimon Glass return i2c_write_bytewise(dev, offset, buffer, len);
167c6202d85SSimon Glass /*
168c6202d85SSimon Glass * The simple approach would be to send two messages here: one to
169c6202d85SSimon Glass * set the offset and one to write the bytes. However some drivers
170c6202d85SSimon Glass * will not be expecting this, and some chips won't like how the
171c6202d85SSimon Glass * driver presents this on the I2C bus.
172c6202d85SSimon Glass *
173c6202d85SSimon Glass * The API does not support separate offset and data. We could extend
174c6202d85SSimon Glass * it with a flag indicating that there is data in the next message
175c6202d85SSimon Glass * that needs to be processed in the same transaction. We could
176c6202d85SSimon Glass * instead add an additional buffer to each message. For now, handle
177c6202d85SSimon Glass * this in the uclass since it isn't clear what the impact on drivers
178c6202d85SSimon Glass * would be with this extra complication. Unfortunately this means
179c6202d85SSimon Glass * copying the message.
180c6202d85SSimon Glass *
181c6202d85SSimon Glass * Use the stack for small messages, malloc() for larger ones. We
182c6202d85SSimon Glass * need to allow space for the offset (up to 4 bytes) and the message
183c6202d85SSimon Glass * itself.
184c6202d85SSimon Glass */
185c6202d85SSimon Glass if (len < 64) {
186c6202d85SSimon Glass uint8_t buf[I2C_MAX_OFFSET_LEN + len];
187c6202d85SSimon Glass
188c6202d85SSimon Glass i2c_setup_offset(chip, offset, buf, msg);
189c6202d85SSimon Glass msg->len += len;
190c6202d85SSimon Glass memcpy(buf + chip->offset_len, buffer, len);
191c6202d85SSimon Glass
192c6202d85SSimon Glass return ops->xfer(bus, msg, 1);
193c6202d85SSimon Glass } else {
194c6202d85SSimon Glass uint8_t *buf;
195c6202d85SSimon Glass int ret;
196c6202d85SSimon Glass
197c6202d85SSimon Glass buf = malloc(I2C_MAX_OFFSET_LEN + len);
198c6202d85SSimon Glass if (!buf)
199c6202d85SSimon Glass return -ENOMEM;
200c6202d85SSimon Glass i2c_setup_offset(chip, offset, buf, msg);
201c6202d85SSimon Glass msg->len += len;
202c6202d85SSimon Glass memcpy(buf + chip->offset_len, buffer, len);
203c6202d85SSimon Glass
204c6202d85SSimon Glass ret = ops->xfer(bus, msg, 1);
205c6202d85SSimon Glass free(buf);
206c6202d85SSimon Glass return ret;
207c6202d85SSimon Glass }
208c6202d85SSimon Glass }
209c6202d85SSimon Glass
dm_i2c_xfer(struct udevice * dev,struct i2c_msg * msg,int nmsgs)210df358c6bSSimon Glass int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
211df358c6bSSimon Glass {
212df358c6bSSimon Glass struct udevice *bus = dev_get_parent(dev);
213df358c6bSSimon Glass struct dm_i2c_ops *ops = i2c_get_ops(bus);
214df358c6bSSimon Glass
215df358c6bSSimon Glass if (!ops->xfer)
216df358c6bSSimon Glass return -ENOSYS;
217df358c6bSSimon Glass
218df358c6bSSimon Glass return ops->xfer(bus, msg, nmsgs);
219df358c6bSSimon Glass }
220df358c6bSSimon Glass
dm_i2c_reg_read(struct udevice * dev,uint offset)221ba3864f8SSimon Glass int dm_i2c_reg_read(struct udevice *dev, uint offset)
222ba3864f8SSimon Glass {
223ba3864f8SSimon Glass uint8_t val;
224ba3864f8SSimon Glass int ret;
225ba3864f8SSimon Glass
226ba3864f8SSimon Glass ret = dm_i2c_read(dev, offset, &val, 1);
227ba3864f8SSimon Glass if (ret < 0)
228ba3864f8SSimon Glass return ret;
229ba3864f8SSimon Glass
230ba3864f8SSimon Glass return val;
231ba3864f8SSimon Glass }
232ba3864f8SSimon Glass
dm_i2c_reg_write(struct udevice * dev,uint offset,uint value)233ba3864f8SSimon Glass int dm_i2c_reg_write(struct udevice *dev, uint offset, uint value)
234ba3864f8SSimon Glass {
235ba3864f8SSimon Glass uint8_t val = value;
236ba3864f8SSimon Glass
237ba3864f8SSimon Glass return dm_i2c_write(dev, offset, &val, 1);
238ba3864f8SSimon Glass }
239ba3864f8SSimon Glass
240c6202d85SSimon Glass /**
241c6202d85SSimon Glass * i2c_probe_chip() - probe for a chip on a bus
242c6202d85SSimon Glass *
243c6202d85SSimon Glass * @bus: Bus to probe
244c6202d85SSimon Glass * @chip_addr: Chip address to probe
245c6202d85SSimon Glass * @flags: Flags for the chip
246c6202d85SSimon Glass * @return 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip
247c6202d85SSimon Glass * does not respond to probe
248c6202d85SSimon Glass */
i2c_probe_chip(struct udevice * bus,uint chip_addr,enum dm_i2c_chip_flags chip_flags)249c6202d85SSimon Glass static int i2c_probe_chip(struct udevice *bus, uint chip_addr,
250c6202d85SSimon Glass enum dm_i2c_chip_flags chip_flags)
251c6202d85SSimon Glass {
252c6202d85SSimon Glass struct dm_i2c_ops *ops = i2c_get_ops(bus);
253c6202d85SSimon Glass struct i2c_msg msg[1];
254c6202d85SSimon Glass int ret;
255c6202d85SSimon Glass
256c6202d85SSimon Glass if (ops->probe_chip) {
257c6202d85SSimon Glass ret = ops->probe_chip(bus, chip_addr, chip_flags);
258c6202d85SSimon Glass if (!ret || ret != -ENOSYS)
259c6202d85SSimon Glass return ret;
260c6202d85SSimon Glass }
261c6202d85SSimon Glass
262c6202d85SSimon Glass if (!ops->xfer)
263c6202d85SSimon Glass return -ENOSYS;
264c6202d85SSimon Glass
265c6202d85SSimon Glass /* Probe with a zero-length message */
266c6202d85SSimon Glass msg->addr = chip_addr;
267c6202d85SSimon Glass msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
268c6202d85SSimon Glass msg->len = 0;
269c6202d85SSimon Glass msg->buf = NULL;
270c6202d85SSimon Glass
271c6202d85SSimon Glass return ops->xfer(bus, msg, 1);
272c6202d85SSimon Glass }
273c6202d85SSimon Glass
i2c_bind_driver(struct udevice * bus,uint chip_addr,uint offset_len,struct udevice ** devp)27425ab4b03SSimon Glass static int i2c_bind_driver(struct udevice *bus, uint chip_addr, uint offset_len,
275c6202d85SSimon Glass struct udevice **devp)
276c6202d85SSimon Glass {
277e6f66ec0SSimon Glass struct dm_i2c_chip *chip;
278c6202d85SSimon Glass char name[30], *str;
279c6202d85SSimon Glass struct udevice *dev;
280c6202d85SSimon Glass int ret;
281c6202d85SSimon Glass
282c6202d85SSimon Glass snprintf(name, sizeof(name), "generic_%x", chip_addr);
283c6202d85SSimon Glass str = strdup(name);
2845c2d23bfSSimon Glass if (!str)
2855c2d23bfSSimon Glass return -ENOMEM;
286c6202d85SSimon Glass ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev);
287c6202d85SSimon Glass debug("%s: device_bind_driver: ret=%d\n", __func__, ret);
288c6202d85SSimon Glass if (ret)
289c6202d85SSimon Glass goto err_bind;
290c6202d85SSimon Glass
291c6202d85SSimon Glass /* Tell the device what we know about it */
292e6f66ec0SSimon Glass chip = dev_get_parent_platdata(dev);
293e6f66ec0SSimon Glass chip->chip_addr = chip_addr;
294e6f66ec0SSimon Glass chip->offset_len = offset_len;
295e6f66ec0SSimon Glass ret = device_probe(dev);
296e6f66ec0SSimon Glass debug("%s: device_probe: ret=%d\n", __func__, ret);
297c6202d85SSimon Glass if (ret)
298c6202d85SSimon Glass goto err_probe;
299c6202d85SSimon Glass
300c6202d85SSimon Glass *devp = dev;
301c6202d85SSimon Glass return 0;
302c6202d85SSimon Glass
303c6202d85SSimon Glass err_probe:
304e6f66ec0SSimon Glass /*
305e6f66ec0SSimon Glass * If the device failed to probe, unbind it. There is nothing there
306e6f66ec0SSimon Glass * on the bus so we don't want to leave it lying around
307e6f66ec0SSimon Glass */
308c6202d85SSimon Glass device_unbind(dev);
309c6202d85SSimon Glass err_bind:
310c6202d85SSimon Glass free(str);
311c6202d85SSimon Glass return ret;
312c6202d85SSimon Glass }
313c6202d85SSimon Glass
i2c_get_chip(struct udevice * bus,uint chip_addr,uint offset_len,struct udevice ** devp)31425ab4b03SSimon Glass int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
31525ab4b03SSimon Glass struct udevice **devp)
316c6202d85SSimon Glass {
317c6202d85SSimon Glass struct udevice *dev;
318c6202d85SSimon Glass
319c6202d85SSimon Glass debug("%s: Searching bus '%s' for address %02x: ", __func__,
320c6202d85SSimon Glass bus->name, chip_addr);
321c6202d85SSimon Glass for (device_find_first_child(bus, &dev); dev;
322c6202d85SSimon Glass device_find_next_child(&dev)) {
323e6f66ec0SSimon Glass struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
324c6202d85SSimon Glass int ret;
325c6202d85SSimon Glass
326c6202d85SSimon Glass if (chip->chip_addr == chip_addr) {
327c6202d85SSimon Glass ret = device_probe(dev);
328c6202d85SSimon Glass debug("found, ret=%d\n", ret);
329c6202d85SSimon Glass if (ret)
330c6202d85SSimon Glass return ret;
331c6202d85SSimon Glass *devp = dev;
332c6202d85SSimon Glass return 0;
333c6202d85SSimon Glass }
334c6202d85SSimon Glass }
335c6202d85SSimon Glass debug("not found\n");
33625ab4b03SSimon Glass return i2c_bind_driver(bus, chip_addr, offset_len, devp);
337c6202d85SSimon Glass }
338c6202d85SSimon Glass
i2c_get_chip_for_busnum(int busnum,int chip_addr,uint offset_len,struct udevice ** devp)33925ab4b03SSimon Glass int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
34025ab4b03SSimon Glass struct udevice **devp)
341c6202d85SSimon Glass {
342c6202d85SSimon Glass struct udevice *bus;
343c6202d85SSimon Glass int ret;
344c6202d85SSimon Glass
345c6202d85SSimon Glass ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
346c6202d85SSimon Glass if (ret) {
347c6202d85SSimon Glass debug("Cannot find I2C bus %d\n", busnum);
348c6202d85SSimon Glass return ret;
349c6202d85SSimon Glass }
350f32a8007SJean-Jacques Hiblot
351f32a8007SJean-Jacques Hiblot /* detect the presence of the chip on the bus */
352f32a8007SJean-Jacques Hiblot ret = i2c_probe_chip(bus, chip_addr, 0);
353f32a8007SJean-Jacques Hiblot debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
354f32a8007SJean-Jacques Hiblot chip_addr, ret);
355f32a8007SJean-Jacques Hiblot if (ret) {
356f32a8007SJean-Jacques Hiblot debug("Cannot detect I2C chip %02x on bus %d\n", chip_addr,
357f32a8007SJean-Jacques Hiblot busnum);
358f32a8007SJean-Jacques Hiblot return ret;
359f32a8007SJean-Jacques Hiblot }
360f32a8007SJean-Jacques Hiblot
36125ab4b03SSimon Glass ret = i2c_get_chip(bus, chip_addr, offset_len, devp);
362c6202d85SSimon Glass if (ret) {
363c6202d85SSimon Glass debug("Cannot find I2C chip %02x on bus %d\n", chip_addr,
364c6202d85SSimon Glass busnum);
365c6202d85SSimon Glass return ret;
366c6202d85SSimon Glass }
367c6202d85SSimon Glass
368c6202d85SSimon Glass return 0;
369c6202d85SSimon Glass }
370c6202d85SSimon Glass
dm_i2c_probe(struct udevice * bus,uint chip_addr,uint chip_flags,struct udevice ** devp)371f9a4c2daSSimon Glass int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
372c6202d85SSimon Glass struct udevice **devp)
373c6202d85SSimon Glass {
374c6202d85SSimon Glass int ret;
375c6202d85SSimon Glass
376c6202d85SSimon Glass *devp = NULL;
377c6202d85SSimon Glass
378c6202d85SSimon Glass /* First probe that chip */
379c6202d85SSimon Glass ret = i2c_probe_chip(bus, chip_addr, chip_flags);
380c6202d85SSimon Glass debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
381c6202d85SSimon Glass chip_addr, ret);
382c6202d85SSimon Glass if (ret)
383c6202d85SSimon Glass return ret;
384c6202d85SSimon Glass
385c6202d85SSimon Glass /* The chip was found, see if we have a driver, and probe it */
38625ab4b03SSimon Glass ret = i2c_get_chip(bus, chip_addr, 1, devp);
387c6202d85SSimon Glass debug("%s: i2c_get_chip: ret=%d\n", __func__, ret);
388c6202d85SSimon Glass
389c6202d85SSimon Glass return ret;
390c6202d85SSimon Glass }
391c6202d85SSimon Glass
dm_i2c_set_bus_speed(struct udevice * bus,unsigned int speed)392ca88b9b9SSimon Glass int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
393c6202d85SSimon Glass {
394c6202d85SSimon Glass struct dm_i2c_ops *ops = i2c_get_ops(bus);
395e564f054SSimon Glass struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
396c6202d85SSimon Glass int ret;
397c6202d85SSimon Glass
398c6202d85SSimon Glass /*
399c6202d85SSimon Glass * If we have a method, call it. If not then the driver probably wants
400c6202d85SSimon Glass * to deal with speed changes on the next transfer. It can easily read
401c6202d85SSimon Glass * the current speed from this uclass
402c6202d85SSimon Glass */
403c6202d85SSimon Glass if (ops->set_bus_speed) {
404c6202d85SSimon Glass ret = ops->set_bus_speed(bus, speed);
405c6202d85SSimon Glass if (ret)
406c6202d85SSimon Glass return ret;
407c6202d85SSimon Glass }
408c6202d85SSimon Glass i2c->speed_hz = speed;
409c6202d85SSimon Glass
410c6202d85SSimon Glass return 0;
411c6202d85SSimon Glass }
412c6202d85SSimon Glass
dm_i2c_get_bus_speed(struct udevice * bus)413ca88b9b9SSimon Glass int dm_i2c_get_bus_speed(struct udevice *bus)
414c6202d85SSimon Glass {
415c6202d85SSimon Glass struct dm_i2c_ops *ops = i2c_get_ops(bus);
416e564f054SSimon Glass struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
417c6202d85SSimon Glass
418c6202d85SSimon Glass if (!ops->get_bus_speed)
419c6202d85SSimon Glass return i2c->speed_hz;
420c6202d85SSimon Glass
421c6202d85SSimon Glass return ops->get_bus_speed(bus);
422c6202d85SSimon Glass }
423c6202d85SSimon Glass
i2c_set_chip_flags(struct udevice * dev,uint flags)424c6202d85SSimon Glass int i2c_set_chip_flags(struct udevice *dev, uint flags)
425c6202d85SSimon Glass {
426c6202d85SSimon Glass struct udevice *bus = dev->parent;
427e6f66ec0SSimon Glass struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
428c6202d85SSimon Glass struct dm_i2c_ops *ops = i2c_get_ops(bus);
429c6202d85SSimon Glass int ret;
430c6202d85SSimon Glass
431c6202d85SSimon Glass if (ops->set_flags) {
432c6202d85SSimon Glass ret = ops->set_flags(dev, flags);
433c6202d85SSimon Glass if (ret)
434c6202d85SSimon Glass return ret;
435c6202d85SSimon Glass }
436c6202d85SSimon Glass chip->flags = flags;
437c6202d85SSimon Glass
438c6202d85SSimon Glass return 0;
439c6202d85SSimon Glass }
440c6202d85SSimon Glass
i2c_get_chip_flags(struct udevice * dev,uint * flagsp)441c6202d85SSimon Glass int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
442c6202d85SSimon Glass {
443e6f66ec0SSimon Glass struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
444c6202d85SSimon Glass
445c6202d85SSimon Glass *flagsp = chip->flags;
446c6202d85SSimon Glass
447c6202d85SSimon Glass return 0;
448c6202d85SSimon Glass }
449c6202d85SSimon Glass
i2c_set_chip_offset_len(struct udevice * dev,uint offset_len)450c6202d85SSimon Glass int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
451c6202d85SSimon Glass {
452e6f66ec0SSimon Glass struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
453c6202d85SSimon Glass
454c6202d85SSimon Glass if (offset_len > I2C_MAX_OFFSET_LEN)
455c6202d85SSimon Glass return -EINVAL;
456c6202d85SSimon Glass chip->offset_len = offset_len;
457c6202d85SSimon Glass
458c6202d85SSimon Glass return 0;
459c6202d85SSimon Glass }
460c6202d85SSimon Glass
i2c_get_chip_offset_len(struct udevice * dev)46101501804SSimon Glass int i2c_get_chip_offset_len(struct udevice *dev)
46201501804SSimon Glass {
46301501804SSimon Glass struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
46401501804SSimon Glass
46501501804SSimon Glass return chip->offset_len;
46601501804SSimon Glass }
46701501804SSimon Glass
468aa54192dSAlexander Kochetkov #ifdef CONFIG_DM_GPIO
i2c_gpio_set_pin(struct gpio_desc * pin,int bit)469aa54192dSAlexander Kochetkov static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit)
470aa54192dSAlexander Kochetkov {
471aa54192dSAlexander Kochetkov if (bit)
472aa54192dSAlexander Kochetkov dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
473aa54192dSAlexander Kochetkov else
474aa54192dSAlexander Kochetkov dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT |
475aa54192dSAlexander Kochetkov GPIOD_ACTIVE_LOW |
476aa54192dSAlexander Kochetkov GPIOD_IS_OUT_ACTIVE);
477aa54192dSAlexander Kochetkov }
478aa54192dSAlexander Kochetkov
i2c_gpio_get_pin(struct gpio_desc * pin)479aa54192dSAlexander Kochetkov static int i2c_gpio_get_pin(struct gpio_desc *pin)
480aa54192dSAlexander Kochetkov {
481aa54192dSAlexander Kochetkov return dm_gpio_get_value(pin);
482aa54192dSAlexander Kochetkov }
483aa54192dSAlexander Kochetkov
i2c_deblock_gpio_loop(struct gpio_desc * sda_pin,struct gpio_desc * scl_pin)484aa54192dSAlexander Kochetkov static int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin,
485aa54192dSAlexander Kochetkov struct gpio_desc *scl_pin)
486aa54192dSAlexander Kochetkov {
487aa54192dSAlexander Kochetkov int counter = 9;
488aa54192dSAlexander Kochetkov int ret = 0;
489aa54192dSAlexander Kochetkov
490aa54192dSAlexander Kochetkov i2c_gpio_set_pin(sda_pin, 1);
491aa54192dSAlexander Kochetkov i2c_gpio_set_pin(scl_pin, 1);
492aa54192dSAlexander Kochetkov udelay(5);
493aa54192dSAlexander Kochetkov
494aa54192dSAlexander Kochetkov /* Toggle SCL until slave release SDA */
495aa54192dSAlexander Kochetkov while (counter-- >= 0) {
496aa54192dSAlexander Kochetkov i2c_gpio_set_pin(scl_pin, 1);
497aa54192dSAlexander Kochetkov udelay(5);
498aa54192dSAlexander Kochetkov i2c_gpio_set_pin(scl_pin, 0);
499aa54192dSAlexander Kochetkov udelay(5);
500aa54192dSAlexander Kochetkov if (i2c_gpio_get_pin(sda_pin))
501aa54192dSAlexander Kochetkov break;
502aa54192dSAlexander Kochetkov }
503aa54192dSAlexander Kochetkov
504aa54192dSAlexander Kochetkov /* Then, send I2C stop */
505aa54192dSAlexander Kochetkov i2c_gpio_set_pin(sda_pin, 0);
506aa54192dSAlexander Kochetkov udelay(5);
507aa54192dSAlexander Kochetkov
508aa54192dSAlexander Kochetkov i2c_gpio_set_pin(scl_pin, 1);
509aa54192dSAlexander Kochetkov udelay(5);
510aa54192dSAlexander Kochetkov
511aa54192dSAlexander Kochetkov i2c_gpio_set_pin(sda_pin, 1);
512aa54192dSAlexander Kochetkov udelay(5);
513aa54192dSAlexander Kochetkov
514aa54192dSAlexander Kochetkov if (!i2c_gpio_get_pin(sda_pin) || !i2c_gpio_get_pin(scl_pin))
515aa54192dSAlexander Kochetkov ret = -EREMOTEIO;
516aa54192dSAlexander Kochetkov
517aa54192dSAlexander Kochetkov return ret;
518aa54192dSAlexander Kochetkov }
519aa54192dSAlexander Kochetkov
i2c_deblock_gpio(struct udevice * bus)520aa54192dSAlexander Kochetkov static int i2c_deblock_gpio(struct udevice *bus)
521aa54192dSAlexander Kochetkov {
522aa54192dSAlexander Kochetkov struct gpio_desc gpios[PIN_COUNT];
523aa54192dSAlexander Kochetkov int ret, ret0;
524aa54192dSAlexander Kochetkov
525aa54192dSAlexander Kochetkov ret = gpio_request_list_by_name(bus, "gpios", gpios,
526aa54192dSAlexander Kochetkov ARRAY_SIZE(gpios), GPIOD_IS_IN);
527aa54192dSAlexander Kochetkov if (ret != ARRAY_SIZE(gpios)) {
528aa54192dSAlexander Kochetkov debug("%s: I2C Node '%s' has no 'gpios' property %s\n",
529aa54192dSAlexander Kochetkov __func__, dev_read_name(bus), bus->name);
530aa54192dSAlexander Kochetkov if (ret >= 0) {
531aa54192dSAlexander Kochetkov gpio_free_list(bus, gpios, ret);
532aa54192dSAlexander Kochetkov ret = -ENOENT;
533aa54192dSAlexander Kochetkov }
534aa54192dSAlexander Kochetkov goto out;
535aa54192dSAlexander Kochetkov }
536aa54192dSAlexander Kochetkov
537aa54192dSAlexander Kochetkov ret = pinctrl_select_state(bus, "gpio");
538aa54192dSAlexander Kochetkov if (ret) {
539aa54192dSAlexander Kochetkov debug("%s: I2C Node '%s' has no 'gpio' pinctrl state. %s\n",
540aa54192dSAlexander Kochetkov __func__, dev_read_name(bus), bus->name);
541aa54192dSAlexander Kochetkov goto out_no_pinctrl;
542aa54192dSAlexander Kochetkov }
543aa54192dSAlexander Kochetkov
544aa54192dSAlexander Kochetkov ret0 = i2c_deblock_gpio_loop(&gpios[PIN_SDA], &gpios[PIN_SCL]);
545aa54192dSAlexander Kochetkov
546aa54192dSAlexander Kochetkov ret = pinctrl_select_state(bus, "default");
547aa54192dSAlexander Kochetkov if (ret) {
548aa54192dSAlexander Kochetkov debug("%s: I2C Node '%s' has no 'default' pinctrl state. %s\n",
549aa54192dSAlexander Kochetkov __func__, dev_read_name(bus), bus->name);
550aa54192dSAlexander Kochetkov }
551aa54192dSAlexander Kochetkov
552aa54192dSAlexander Kochetkov ret = !ret ? ret0 : ret;
553aa54192dSAlexander Kochetkov
554aa54192dSAlexander Kochetkov out_no_pinctrl:
555aa54192dSAlexander Kochetkov gpio_free_list(bus, gpios, ARRAY_SIZE(gpios));
556aa54192dSAlexander Kochetkov out:
557aa54192dSAlexander Kochetkov return ret;
558aa54192dSAlexander Kochetkov }
559aa54192dSAlexander Kochetkov #else
i2c_deblock_gpio(struct udevice * bus)560aa54192dSAlexander Kochetkov static int i2c_deblock_gpio(struct udevice *bus)
561aa54192dSAlexander Kochetkov {
562aa54192dSAlexander Kochetkov return -ENOSYS;
563aa54192dSAlexander Kochetkov }
564aa54192dSAlexander Kochetkov #endif // CONFIG_DM_GPIO
565aa54192dSAlexander Kochetkov
i2c_deblock(struct udevice * bus)566c6202d85SSimon Glass int i2c_deblock(struct udevice *bus)
567c6202d85SSimon Glass {
568c6202d85SSimon Glass struct dm_i2c_ops *ops = i2c_get_ops(bus);
569c6202d85SSimon Glass
570c6202d85SSimon Glass if (!ops->deblock)
571aa54192dSAlexander Kochetkov return i2c_deblock_gpio(bus);
572c6202d85SSimon Glass
573c6202d85SSimon Glass return ops->deblock(bus);
574c6202d85SSimon Glass }
575c6202d85SSimon Glass
576afa8cdd6SAdam Ford #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
i2c_chip_ofdata_to_platdata(struct udevice * dev,struct dm_i2c_chip * chip)5771704308eSSimon Glass int i2c_chip_ofdata_to_platdata(struct udevice *dev, struct dm_i2c_chip *chip)
578c6202d85SSimon Glass {
5791704308eSSimon Glass int addr;
5801704308eSSimon Glass
5811704308eSSimon Glass chip->offset_len = dev_read_u32_default(dev, "u-boot,i2c-offset-len",
5821704308eSSimon Glass 1);
583c6202d85SSimon Glass chip->flags = 0;
5841704308eSSimon Glass addr = dev_read_u32_default(dev, "reg", -1);
5851704308eSSimon Glass if (addr == -1) {
5861704308eSSimon Glass debug("%s: I2C Node '%s' has no 'reg' property %s\n", __func__,
5871704308eSSimon Glass dev_read_name(dev), dev->name);
588c6202d85SSimon Glass return -EINVAL;
589c6202d85SSimon Glass }
5901704308eSSimon Glass chip->chip_addr = addr;
591c6202d85SSimon Glass
592c6202d85SSimon Glass return 0;
593c6202d85SSimon Glass }
5945142ac79SMugunthan V N #endif
595c6202d85SSimon Glass
i2c_post_probe(struct udevice * dev)596c6202d85SSimon Glass static int i2c_post_probe(struct udevice *dev)
597c6202d85SSimon Glass {
598afa8cdd6SAdam Ford #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
599e564f054SSimon Glass struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
600c6202d85SSimon Glass
6011704308eSSimon Glass i2c->speed_hz = dev_read_u32_default(dev, "clock-frequency", 100000);
602c6202d85SSimon Glass
603ca88b9b9SSimon Glass return dm_i2c_set_bus_speed(dev, i2c->speed_hz);
6045142ac79SMugunthan V N #else
6055142ac79SMugunthan V N return 0;
6065142ac79SMugunthan V N #endif
607c6202d85SSimon Glass }
608c6202d85SSimon Glass
i2c_child_post_bind(struct udevice * dev)609e6f66ec0SSimon Glass static int i2c_child_post_bind(struct udevice *dev)
610e6f66ec0SSimon Glass {
611afa8cdd6SAdam Ford #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
612e6f66ec0SSimon Glass struct dm_i2c_chip *plat = dev_get_parent_platdata(dev);
613e6f66ec0SSimon Glass
6141704308eSSimon Glass if (!dev_of_valid(dev))
615e6f66ec0SSimon Glass return 0;
6161704308eSSimon Glass return i2c_chip_ofdata_to_platdata(dev, plat);
6175142ac79SMugunthan V N #else
6185142ac79SMugunthan V N return 0;
6195142ac79SMugunthan V N #endif
620e6f66ec0SSimon Glass }
621e6f66ec0SSimon Glass
6226bfacc8aSMichal Simek struct i2c_priv {
6236bfacc8aSMichal Simek int max_id;
6246bfacc8aSMichal Simek };
6256bfacc8aSMichal Simek
i2c_post_bind(struct udevice * dev)626*61607225SMichal Simek static int i2c_post_bind(struct udevice *dev)
627*61607225SMichal Simek {
628*61607225SMichal Simek struct uclass *class = dev->uclass;
629*61607225SMichal Simek struct i2c_priv *priv = class->priv;
630*61607225SMichal Simek int ret = 0;
631*61607225SMichal Simek
632*61607225SMichal Simek /* Just for sure */
633*61607225SMichal Simek if (!priv)
634*61607225SMichal Simek return -ENOMEM;
635*61607225SMichal Simek
636*61607225SMichal Simek debug("%s: %s, req_seq=%d\n", __func__, dev->name, dev->req_seq);
637*61607225SMichal Simek
638*61607225SMichal Simek /* if there is no alias ID, use the first free */
639*61607225SMichal Simek if (dev->req_seq == -1)
640*61607225SMichal Simek dev->req_seq = ++priv->max_id;
641*61607225SMichal Simek
642*61607225SMichal Simek debug("%s: %s, new req_seq=%d\n", __func__, dev->name, dev->req_seq);
643*61607225SMichal Simek
644*61607225SMichal Simek #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
645*61607225SMichal Simek ret = dm_scan_fdt_dev(dev);
646*61607225SMichal Simek #endif
647*61607225SMichal Simek return ret;
648*61607225SMichal Simek }
649*61607225SMichal Simek
i2c_uclass_init(struct uclass * class)6506bfacc8aSMichal Simek int i2c_uclass_init(struct uclass *class)
6516bfacc8aSMichal Simek {
6526bfacc8aSMichal Simek struct i2c_priv *priv = class->priv;
6536bfacc8aSMichal Simek
6546bfacc8aSMichal Simek /* Just for sure */
6556bfacc8aSMichal Simek if (!priv)
6566bfacc8aSMichal Simek return -ENOMEM;
6576bfacc8aSMichal Simek
6586bfacc8aSMichal Simek /* Get the last allocated alias. */
6596bfacc8aSMichal Simek #if CONFIG_IS_ENABLED(OF_CONTROL)
6606bfacc8aSMichal Simek priv->max_id = dev_read_alias_highest_id("i2c");
6616bfacc8aSMichal Simek #else
6626bfacc8aSMichal Simek priv->max_id = -1;
6636bfacc8aSMichal Simek #endif
6646bfacc8aSMichal Simek
6656bfacc8aSMichal Simek debug("%s: highest alias id is %d\n", __func__, priv->max_id);
6666bfacc8aSMichal Simek
6676bfacc8aSMichal Simek return 0;
6686bfacc8aSMichal Simek }
6696bfacc8aSMichal Simek
670c6202d85SSimon Glass UCLASS_DRIVER(i2c) = {
671c6202d85SSimon Glass .id = UCLASS_I2C,
672c6202d85SSimon Glass .name = "i2c",
6739cc36a2bSSimon Glass .flags = DM_UC_FLAG_SEQ_ALIAS,
674*61607225SMichal Simek .post_bind = i2c_post_bind,
6756bfacc8aSMichal Simek .init = i2c_uclass_init,
6766bfacc8aSMichal Simek .priv_auto_alloc_size = sizeof(struct i2c_priv),
677c6202d85SSimon Glass .post_probe = i2c_post_probe,
678e6f66ec0SSimon Glass .per_device_auto_alloc_size = sizeof(struct dm_i2c_bus),
679e6f66ec0SSimon Glass .per_child_platdata_auto_alloc_size = sizeof(struct dm_i2c_chip),
680e6f66ec0SSimon Glass .child_post_bind = i2c_child_post_bind,
681c6202d85SSimon Glass };
682c6202d85SSimon Glass
683c6202d85SSimon Glass UCLASS_DRIVER(i2c_generic) = {
684c6202d85SSimon Glass .id = UCLASS_I2C_GENERIC,
685c6202d85SSimon Glass .name = "i2c_generic",
686c6202d85SSimon Glass };
687c6202d85SSimon Glass
688c6202d85SSimon Glass U_BOOT_DRIVER(i2c_generic_chip_drv) = {
689c6202d85SSimon Glass .name = "i2c_generic_chip_drv",
690c6202d85SSimon Glass .id = UCLASS_I2C_GENERIC,
691c6202d85SSimon Glass };
692