xref: /openbmc/u-boot/drivers/i2c/i2c-uclass.c (revision 7d7db2225c5e63a389ee04d63919f012e7ba880d)
1c6202d85SSimon Glass /*
2c6202d85SSimon Glass  * Copyright (c) 2014 Google, Inc
3c6202d85SSimon Glass  *
4c6202d85SSimon Glass  * SPDX-License-Identifier:	GPL-2.0+
5c6202d85SSimon Glass  */
6c6202d85SSimon Glass 
7c6202d85SSimon Glass #include <common.h>
8c6202d85SSimon Glass #include <dm.h>
9c6202d85SSimon Glass #include <errno.h>
10c6202d85SSimon Glass #include <fdtdec.h>
11c6202d85SSimon Glass #include <i2c.h>
12c6202d85SSimon Glass #include <malloc.h>
13c6202d85SSimon Glass #include <dm/device-internal.h>
14c6202d85SSimon Glass #include <dm/lists.h>
15c6202d85SSimon Glass #include <dm/root.h>
16c6202d85SSimon Glass 
17c6202d85SSimon Glass DECLARE_GLOBAL_DATA_PTR;
18c6202d85SSimon Glass 
19c6202d85SSimon Glass #define I2C_MAX_OFFSET_LEN	4
20c6202d85SSimon Glass 
21*7d7db222SSimon Glass /* Useful debugging function */
22*7d7db222SSimon Glass void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs)
23*7d7db222SSimon Glass {
24*7d7db222SSimon Glass 	int i;
25*7d7db222SSimon Glass 
26*7d7db222SSimon Glass 	for (i = 0; i < nmsgs; i++) {
27*7d7db222SSimon Glass 		struct i2c_msg *m = &msg[i];
28*7d7db222SSimon Glass 
29*7d7db222SSimon Glass 		printf("   %s %x len=%x", m->flags & I2C_M_RD ? "R" : "W",
30*7d7db222SSimon Glass 		       msg->addr, msg->len);
31*7d7db222SSimon Glass 		if (!(m->flags & I2C_M_RD))
32*7d7db222SSimon Glass 			printf(": %x", m->buf[0]);
33*7d7db222SSimon Glass 		printf("\n");
34*7d7db222SSimon Glass 	}
35*7d7db222SSimon Glass }
36*7d7db222SSimon Glass 
37c6202d85SSimon Glass /**
38c6202d85SSimon Glass  * i2c_setup_offset() - Set up a new message with a chip offset
39c6202d85SSimon Glass  *
40c6202d85SSimon Glass  * @chip:	Chip to use
41c6202d85SSimon Glass  * @offset:	Byte offset within chip
42c6202d85SSimon Glass  * @offset_buf:	Place to put byte offset
43c6202d85SSimon Glass  * @msg:	Message buffer
44c6202d85SSimon Glass  * @return 0 if OK, -EADDRNOTAVAIL if the offset length is 0. In that case the
45c6202d85SSimon Glass  * message is still set up but will not contain an offset.
46c6202d85SSimon Glass  */
47c6202d85SSimon Glass static int i2c_setup_offset(struct dm_i2c_chip *chip, uint offset,
48c6202d85SSimon Glass 			    uint8_t offset_buf[], struct i2c_msg *msg)
49c6202d85SSimon Glass {
50c6202d85SSimon Glass 	int offset_len;
51c6202d85SSimon Glass 
52c6202d85SSimon Glass 	msg->addr = chip->chip_addr;
53c6202d85SSimon Glass 	msg->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
54c6202d85SSimon Glass 	msg->len = chip->offset_len;
55c6202d85SSimon Glass 	msg->buf = offset_buf;
56c6202d85SSimon Glass 	if (!chip->offset_len)
57c6202d85SSimon Glass 		return -EADDRNOTAVAIL;
58c6202d85SSimon Glass 	assert(chip->offset_len <= I2C_MAX_OFFSET_LEN);
59c6202d85SSimon Glass 	offset_len = chip->offset_len;
60c6202d85SSimon Glass 	while (offset_len--)
61c6202d85SSimon Glass 		*offset_buf++ = offset >> (8 * offset_len);
62c6202d85SSimon Glass 
63c6202d85SSimon Glass 	return 0;
64c6202d85SSimon Glass }
65c6202d85SSimon Glass 
66c6202d85SSimon Glass static int i2c_read_bytewise(struct udevice *dev, uint offset,
67c6202d85SSimon Glass 			     uint8_t *buffer, int len)
68c6202d85SSimon Glass {
69e6f66ec0SSimon Glass 	struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
70c6202d85SSimon Glass 	struct udevice *bus = dev_get_parent(dev);
71c6202d85SSimon Glass 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
72c6202d85SSimon Glass 	struct i2c_msg msg[2], *ptr;
73c6202d85SSimon Glass 	uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
74c6202d85SSimon Glass 	int ret;
75c6202d85SSimon Glass 	int i;
76c6202d85SSimon Glass 
77c6202d85SSimon Glass 	for (i = 0; i < len; i++) {
78c6202d85SSimon Glass 		if (i2c_setup_offset(chip, offset + i, offset_buf, msg))
79c6202d85SSimon Glass 			return -EINVAL;
80c6202d85SSimon Glass 		ptr = msg + 1;
81c6202d85SSimon Glass 		ptr->addr = chip->chip_addr;
82c6202d85SSimon Glass 		ptr->flags = msg->flags | I2C_M_RD;
83c6202d85SSimon Glass 		ptr->len = 1;
84c6202d85SSimon Glass 		ptr->buf = &buffer[i];
85c6202d85SSimon Glass 		ptr++;
86c6202d85SSimon Glass 
87c6202d85SSimon Glass 		ret = ops->xfer(bus, msg, ptr - msg);
88c6202d85SSimon Glass 		if (ret)
89c6202d85SSimon Glass 			return ret;
90c6202d85SSimon Glass 	}
91c6202d85SSimon Glass 
92c6202d85SSimon Glass 	return 0;
93c6202d85SSimon Glass }
94c6202d85SSimon Glass 
95c6202d85SSimon Glass static int i2c_write_bytewise(struct udevice *dev, uint offset,
96c6202d85SSimon Glass 			     const uint8_t *buffer, int len)
97c6202d85SSimon Glass {
98e6f66ec0SSimon Glass 	struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
99c6202d85SSimon Glass 	struct udevice *bus = dev_get_parent(dev);
100c6202d85SSimon Glass 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
101c6202d85SSimon Glass 	struct i2c_msg msg[1];
102c6202d85SSimon Glass 	uint8_t buf[I2C_MAX_OFFSET_LEN + 1];
103c6202d85SSimon Glass 	int ret;
104c6202d85SSimon Glass 	int i;
105c6202d85SSimon Glass 
106c6202d85SSimon Glass 	for (i = 0; i < len; i++) {
107c6202d85SSimon Glass 		if (i2c_setup_offset(chip, offset + i, buf, msg))
108c6202d85SSimon Glass 			return -EINVAL;
109c6202d85SSimon Glass 		buf[msg->len++] = buffer[i];
110c6202d85SSimon Glass 
111c6202d85SSimon Glass 		ret = ops->xfer(bus, msg, 1);
112c6202d85SSimon Glass 		if (ret)
113c6202d85SSimon Glass 			return ret;
114c6202d85SSimon Glass 	}
115c6202d85SSimon Glass 
116c6202d85SSimon Glass 	return 0;
117c6202d85SSimon Glass }
118c6202d85SSimon Glass 
119f9a4c2daSSimon Glass int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len)
120c6202d85SSimon Glass {
121e6f66ec0SSimon Glass 	struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
122c6202d85SSimon Glass 	struct udevice *bus = dev_get_parent(dev);
123c6202d85SSimon Glass 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
124c6202d85SSimon Glass 	struct i2c_msg msg[2], *ptr;
125c6202d85SSimon Glass 	uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
126c6202d85SSimon Glass 	int msg_count;
127c6202d85SSimon Glass 
128c6202d85SSimon Glass 	if (!ops->xfer)
129c6202d85SSimon Glass 		return -ENOSYS;
130c6202d85SSimon Glass 	if (chip->flags & DM_I2C_CHIP_RD_ADDRESS)
131c6202d85SSimon Glass 		return i2c_read_bytewise(dev, offset, buffer, len);
132c6202d85SSimon Glass 	ptr = msg;
133c6202d85SSimon Glass 	if (!i2c_setup_offset(chip, offset, offset_buf, ptr))
134c6202d85SSimon Glass 		ptr++;
135c6202d85SSimon Glass 
136c6202d85SSimon Glass 	if (len) {
137c6202d85SSimon Glass 		ptr->addr = chip->chip_addr;
138c6202d85SSimon Glass 		ptr->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
139c6202d85SSimon Glass 		ptr->flags |= I2C_M_RD;
140c6202d85SSimon Glass 		ptr->len = len;
141c6202d85SSimon Glass 		ptr->buf = buffer;
142c6202d85SSimon Glass 		ptr++;
143c6202d85SSimon Glass 	}
144c6202d85SSimon Glass 	msg_count = ptr - msg;
145c6202d85SSimon Glass 
146c6202d85SSimon Glass 	return ops->xfer(bus, msg, msg_count);
147c6202d85SSimon Glass }
148c6202d85SSimon Glass 
149f9a4c2daSSimon Glass int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
150f9a4c2daSSimon Glass 		 int len)
151c6202d85SSimon Glass {
152e6f66ec0SSimon Glass 	struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
153c6202d85SSimon Glass 	struct udevice *bus = dev_get_parent(dev);
154c6202d85SSimon Glass 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
155c6202d85SSimon Glass 	struct i2c_msg msg[1];
156c6202d85SSimon Glass 
157c6202d85SSimon Glass 	if (!ops->xfer)
158c6202d85SSimon Glass 		return -ENOSYS;
159c6202d85SSimon Glass 
160c6202d85SSimon Glass 	if (chip->flags & DM_I2C_CHIP_WR_ADDRESS)
161c6202d85SSimon Glass 		return i2c_write_bytewise(dev, offset, buffer, len);
162c6202d85SSimon Glass 	/*
163c6202d85SSimon Glass 	 * The simple approach would be to send two messages here: one to
164c6202d85SSimon Glass 	 * set the offset and one to write the bytes. However some drivers
165c6202d85SSimon Glass 	 * will not be expecting this, and some chips won't like how the
166c6202d85SSimon Glass 	 * driver presents this on the I2C bus.
167c6202d85SSimon Glass 	 *
168c6202d85SSimon Glass 	 * The API does not support separate offset and data. We could extend
169c6202d85SSimon Glass 	 * it with a flag indicating that there is data in the next message
170c6202d85SSimon Glass 	 * that needs to be processed in the same transaction. We could
171c6202d85SSimon Glass 	 * instead add an additional buffer to each message. For now, handle
172c6202d85SSimon Glass 	 * this in the uclass since it isn't clear what the impact on drivers
173c6202d85SSimon Glass 	 * would be with this extra complication. Unfortunately this means
174c6202d85SSimon Glass 	 * copying the message.
175c6202d85SSimon Glass 	 *
176c6202d85SSimon Glass 	 * Use the stack for small messages, malloc() for larger ones. We
177c6202d85SSimon Glass 	 * need to allow space for the offset (up to 4 bytes) and the message
178c6202d85SSimon Glass 	 * itself.
179c6202d85SSimon Glass 	 */
180c6202d85SSimon Glass 	if (len < 64) {
181c6202d85SSimon Glass 		uint8_t buf[I2C_MAX_OFFSET_LEN + len];
182c6202d85SSimon Glass 
183c6202d85SSimon Glass 		i2c_setup_offset(chip, offset, buf, msg);
184c6202d85SSimon Glass 		msg->len += len;
185c6202d85SSimon Glass 		memcpy(buf + chip->offset_len, buffer, len);
186c6202d85SSimon Glass 
187c6202d85SSimon Glass 		return ops->xfer(bus, msg, 1);
188c6202d85SSimon Glass 	} else {
189c6202d85SSimon Glass 		uint8_t *buf;
190c6202d85SSimon Glass 		int ret;
191c6202d85SSimon Glass 
192c6202d85SSimon Glass 		buf = malloc(I2C_MAX_OFFSET_LEN + len);
193c6202d85SSimon Glass 		if (!buf)
194c6202d85SSimon Glass 			return -ENOMEM;
195c6202d85SSimon Glass 		i2c_setup_offset(chip, offset, buf, msg);
196c6202d85SSimon Glass 		msg->len += len;
197c6202d85SSimon Glass 		memcpy(buf + chip->offset_len, buffer, len);
198c6202d85SSimon Glass 
199c6202d85SSimon Glass 		ret = ops->xfer(bus, msg, 1);
200c6202d85SSimon Glass 		free(buf);
201c6202d85SSimon Glass 		return ret;
202c6202d85SSimon Glass 	}
203c6202d85SSimon Glass }
204c6202d85SSimon Glass 
205ba3864f8SSimon Glass int dm_i2c_reg_read(struct udevice *dev, uint offset)
206ba3864f8SSimon Glass {
207ba3864f8SSimon Glass 	uint8_t val;
208ba3864f8SSimon Glass 	int ret;
209ba3864f8SSimon Glass 
210ba3864f8SSimon Glass 	ret = dm_i2c_read(dev, offset, &val, 1);
211ba3864f8SSimon Glass 	if (ret < 0)
212ba3864f8SSimon Glass 		return ret;
213ba3864f8SSimon Glass 
214ba3864f8SSimon Glass 	return val;
215ba3864f8SSimon Glass }
216ba3864f8SSimon Glass 
217ba3864f8SSimon Glass int dm_i2c_reg_write(struct udevice *dev, uint offset, uint value)
218ba3864f8SSimon Glass {
219ba3864f8SSimon Glass 	uint8_t val = value;
220ba3864f8SSimon Glass 
221ba3864f8SSimon Glass 	return dm_i2c_write(dev, offset, &val, 1);
222ba3864f8SSimon Glass }
223ba3864f8SSimon Glass 
224c6202d85SSimon Glass /**
225c6202d85SSimon Glass  * i2c_probe_chip() - probe for a chip on a bus
226c6202d85SSimon Glass  *
227c6202d85SSimon Glass  * @bus:	Bus to probe
228c6202d85SSimon Glass  * @chip_addr:	Chip address to probe
229c6202d85SSimon Glass  * @flags:	Flags for the chip
230c6202d85SSimon Glass  * @return 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip
231c6202d85SSimon Glass  * does not respond to probe
232c6202d85SSimon Glass  */
233c6202d85SSimon Glass static int i2c_probe_chip(struct udevice *bus, uint chip_addr,
234c6202d85SSimon Glass 			  enum dm_i2c_chip_flags chip_flags)
235c6202d85SSimon Glass {
236c6202d85SSimon Glass 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
237c6202d85SSimon Glass 	struct i2c_msg msg[1];
238c6202d85SSimon Glass 	int ret;
239c6202d85SSimon Glass 
240c6202d85SSimon Glass 	if (ops->probe_chip) {
241c6202d85SSimon Glass 		ret = ops->probe_chip(bus, chip_addr, chip_flags);
242c6202d85SSimon Glass 		if (!ret || ret != -ENOSYS)
243c6202d85SSimon Glass 			return ret;
244c6202d85SSimon Glass 	}
245c6202d85SSimon Glass 
246c6202d85SSimon Glass 	if (!ops->xfer)
247c6202d85SSimon Glass 		return -ENOSYS;
248c6202d85SSimon Glass 
249c6202d85SSimon Glass 	/* Probe with a zero-length message */
250c6202d85SSimon Glass 	msg->addr = chip_addr;
251c6202d85SSimon Glass 	msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
252c6202d85SSimon Glass 	msg->len = 0;
253c6202d85SSimon Glass 	msg->buf = NULL;
254c6202d85SSimon Glass 
255c6202d85SSimon Glass 	return ops->xfer(bus, msg, 1);
256c6202d85SSimon Glass }
257c6202d85SSimon Glass 
25825ab4b03SSimon Glass static int i2c_bind_driver(struct udevice *bus, uint chip_addr, uint offset_len,
259c6202d85SSimon Glass 			   struct udevice **devp)
260c6202d85SSimon Glass {
261e6f66ec0SSimon Glass 	struct dm_i2c_chip *chip;
262c6202d85SSimon Glass 	char name[30], *str;
263c6202d85SSimon Glass 	struct udevice *dev;
264c6202d85SSimon Glass 	int ret;
265c6202d85SSimon Glass 
266c6202d85SSimon Glass 	snprintf(name, sizeof(name), "generic_%x", chip_addr);
267c6202d85SSimon Glass 	str = strdup(name);
2685c2d23bfSSimon Glass 	if (!str)
2695c2d23bfSSimon Glass 		return -ENOMEM;
270c6202d85SSimon Glass 	ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev);
271c6202d85SSimon Glass 	debug("%s:  device_bind_driver: ret=%d\n", __func__, ret);
272c6202d85SSimon Glass 	if (ret)
273c6202d85SSimon Glass 		goto err_bind;
274c6202d85SSimon Glass 
275c6202d85SSimon Glass 	/* Tell the device what we know about it */
276e6f66ec0SSimon Glass 	chip = dev_get_parent_platdata(dev);
277e6f66ec0SSimon Glass 	chip->chip_addr = chip_addr;
278e6f66ec0SSimon Glass 	chip->offset_len = offset_len;
279e6f66ec0SSimon Glass 	ret = device_probe(dev);
280e6f66ec0SSimon Glass 	debug("%s:  device_probe: ret=%d\n", __func__, ret);
281c6202d85SSimon Glass 	if (ret)
282c6202d85SSimon Glass 		goto err_probe;
283c6202d85SSimon Glass 
284c6202d85SSimon Glass 	*devp = dev;
285c6202d85SSimon Glass 	return 0;
286c6202d85SSimon Glass 
287c6202d85SSimon Glass err_probe:
288e6f66ec0SSimon Glass 	/*
289e6f66ec0SSimon Glass 	 * If the device failed to probe, unbind it. There is nothing there
290e6f66ec0SSimon Glass 	 * on the bus so we don't want to leave it lying around
291e6f66ec0SSimon Glass 	 */
292c6202d85SSimon Glass 	device_unbind(dev);
293c6202d85SSimon Glass err_bind:
294c6202d85SSimon Glass 	free(str);
295c6202d85SSimon Glass 	return ret;
296c6202d85SSimon Glass }
297c6202d85SSimon Glass 
29825ab4b03SSimon Glass int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
29925ab4b03SSimon Glass 		 struct udevice **devp)
300c6202d85SSimon Glass {
301c6202d85SSimon Glass 	struct udevice *dev;
302c6202d85SSimon Glass 
303c6202d85SSimon Glass 	debug("%s: Searching bus '%s' for address %02x: ", __func__,
304c6202d85SSimon Glass 	      bus->name, chip_addr);
305c6202d85SSimon Glass 	for (device_find_first_child(bus, &dev); dev;
306c6202d85SSimon Glass 			device_find_next_child(&dev)) {
307e6f66ec0SSimon Glass 		struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
308c6202d85SSimon Glass 		int ret;
309c6202d85SSimon Glass 
310c6202d85SSimon Glass 		if (chip->chip_addr == chip_addr) {
311c6202d85SSimon Glass 			ret = device_probe(dev);
312c6202d85SSimon Glass 			debug("found, ret=%d\n", ret);
313c6202d85SSimon Glass 			if (ret)
314c6202d85SSimon Glass 				return ret;
315c6202d85SSimon Glass 			*devp = dev;
316c6202d85SSimon Glass 			return 0;
317c6202d85SSimon Glass 		}
318c6202d85SSimon Glass 	}
319c6202d85SSimon Glass 	debug("not found\n");
32025ab4b03SSimon Glass 	return i2c_bind_driver(bus, chip_addr, offset_len, devp);
321c6202d85SSimon Glass }
322c6202d85SSimon Glass 
32325ab4b03SSimon Glass int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
32425ab4b03SSimon Glass 			    struct udevice **devp)
325c6202d85SSimon Glass {
326c6202d85SSimon Glass 	struct udevice *bus;
327c6202d85SSimon Glass 	int ret;
328c6202d85SSimon Glass 
329c6202d85SSimon Glass 	ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
330c6202d85SSimon Glass 	if (ret) {
331c6202d85SSimon Glass 		debug("Cannot find I2C bus %d\n", busnum);
332c6202d85SSimon Glass 		return ret;
333c6202d85SSimon Glass 	}
33425ab4b03SSimon Glass 	ret = i2c_get_chip(bus, chip_addr, offset_len, devp);
335c6202d85SSimon Glass 	if (ret) {
336c6202d85SSimon Glass 		debug("Cannot find I2C chip %02x on bus %d\n", chip_addr,
337c6202d85SSimon Glass 		      busnum);
338c6202d85SSimon Glass 		return ret;
339c6202d85SSimon Glass 	}
340c6202d85SSimon Glass 
341c6202d85SSimon Glass 	return 0;
342c6202d85SSimon Glass }
343c6202d85SSimon Glass 
344f9a4c2daSSimon Glass int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
345c6202d85SSimon Glass 		 struct udevice **devp)
346c6202d85SSimon Glass {
347c6202d85SSimon Glass 	int ret;
348c6202d85SSimon Glass 
349c6202d85SSimon Glass 	*devp = NULL;
350c6202d85SSimon Glass 
351c6202d85SSimon Glass 	/* First probe that chip */
352c6202d85SSimon Glass 	ret = i2c_probe_chip(bus, chip_addr, chip_flags);
353c6202d85SSimon Glass 	debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
354c6202d85SSimon Glass 	      chip_addr, ret);
355c6202d85SSimon Glass 	if (ret)
356c6202d85SSimon Glass 		return ret;
357c6202d85SSimon Glass 
358c6202d85SSimon Glass 	/* The chip was found, see if we have a driver, and probe it */
35925ab4b03SSimon Glass 	ret = i2c_get_chip(bus, chip_addr, 1, devp);
360c6202d85SSimon Glass 	debug("%s:  i2c_get_chip: ret=%d\n", __func__, ret);
361c6202d85SSimon Glass 
362c6202d85SSimon Glass 	return ret;
363c6202d85SSimon Glass }
364c6202d85SSimon Glass 
365ca88b9b9SSimon Glass int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
366c6202d85SSimon Glass {
367c6202d85SSimon Glass 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
368e564f054SSimon Glass 	struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
369c6202d85SSimon Glass 	int ret;
370c6202d85SSimon Glass 
371c6202d85SSimon Glass 	/*
372c6202d85SSimon Glass 	 * If we have a method, call it. If not then the driver probably wants
373c6202d85SSimon Glass 	 * to deal with speed changes on the next transfer. It can easily read
374c6202d85SSimon Glass 	 * the current speed from this uclass
375c6202d85SSimon Glass 	 */
376c6202d85SSimon Glass 	if (ops->set_bus_speed) {
377c6202d85SSimon Glass 		ret = ops->set_bus_speed(bus, speed);
378c6202d85SSimon Glass 		if (ret)
379c6202d85SSimon Glass 			return ret;
380c6202d85SSimon Glass 	}
381c6202d85SSimon Glass 	i2c->speed_hz = speed;
382c6202d85SSimon Glass 
383c6202d85SSimon Glass 	return 0;
384c6202d85SSimon Glass }
385c6202d85SSimon Glass 
386ca88b9b9SSimon Glass int dm_i2c_get_bus_speed(struct udevice *bus)
387c6202d85SSimon Glass {
388c6202d85SSimon Glass 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
389e564f054SSimon Glass 	struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
390c6202d85SSimon Glass 
391c6202d85SSimon Glass 	if (!ops->get_bus_speed)
392c6202d85SSimon Glass 		return i2c->speed_hz;
393c6202d85SSimon Glass 
394c6202d85SSimon Glass 	return ops->get_bus_speed(bus);
395c6202d85SSimon Glass }
396c6202d85SSimon Glass 
397c6202d85SSimon Glass int i2c_set_chip_flags(struct udevice *dev, uint flags)
398c6202d85SSimon Glass {
399c6202d85SSimon Glass 	struct udevice *bus = dev->parent;
400e6f66ec0SSimon Glass 	struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
401c6202d85SSimon Glass 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
402c6202d85SSimon Glass 	int ret;
403c6202d85SSimon Glass 
404c6202d85SSimon Glass 	if (ops->set_flags) {
405c6202d85SSimon Glass 		ret = ops->set_flags(dev, flags);
406c6202d85SSimon Glass 		if (ret)
407c6202d85SSimon Glass 			return ret;
408c6202d85SSimon Glass 	}
409c6202d85SSimon Glass 	chip->flags = flags;
410c6202d85SSimon Glass 
411c6202d85SSimon Glass 	return 0;
412c6202d85SSimon Glass }
413c6202d85SSimon Glass 
414c6202d85SSimon Glass int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
415c6202d85SSimon Glass {
416e6f66ec0SSimon Glass 	struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
417c6202d85SSimon Glass 
418c6202d85SSimon Glass 	*flagsp = chip->flags;
419c6202d85SSimon Glass 
420c6202d85SSimon Glass 	return 0;
421c6202d85SSimon Glass }
422c6202d85SSimon Glass 
423c6202d85SSimon Glass int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
424c6202d85SSimon Glass {
425e6f66ec0SSimon Glass 	struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
426c6202d85SSimon Glass 
427c6202d85SSimon Glass 	if (offset_len > I2C_MAX_OFFSET_LEN)
428c6202d85SSimon Glass 		return -EINVAL;
429c6202d85SSimon Glass 	chip->offset_len = offset_len;
430c6202d85SSimon Glass 
431c6202d85SSimon Glass 	return 0;
432c6202d85SSimon Glass }
433c6202d85SSimon Glass 
43401501804SSimon Glass int i2c_get_chip_offset_len(struct udevice *dev)
43501501804SSimon Glass {
43601501804SSimon Glass 	struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
43701501804SSimon Glass 
43801501804SSimon Glass 	return chip->offset_len;
43901501804SSimon Glass }
44001501804SSimon Glass 
441c6202d85SSimon Glass int i2c_deblock(struct udevice *bus)
442c6202d85SSimon Glass {
443c6202d85SSimon Glass 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
444c6202d85SSimon Glass 
445c6202d85SSimon Glass 	/*
446c6202d85SSimon Glass 	 * We could implement a software deblocking here if we could get
447c6202d85SSimon Glass 	 * access to the GPIOs used by I2C, and switch them to GPIO mode
448c6202d85SSimon Glass 	 * and then back to I2C. This is somewhat beyond our powers in
449c6202d85SSimon Glass 	 * driver model at present, so for now just fail.
450c6202d85SSimon Glass 	 *
451c6202d85SSimon Glass 	 * See https://patchwork.ozlabs.org/patch/399040/
452c6202d85SSimon Glass 	 */
453c6202d85SSimon Glass 	if (!ops->deblock)
454c6202d85SSimon Glass 		return -ENOSYS;
455c6202d85SSimon Glass 
456c6202d85SSimon Glass 	return ops->deblock(bus);
457c6202d85SSimon Glass }
458c6202d85SSimon Glass 
459c6202d85SSimon Glass int i2c_chip_ofdata_to_platdata(const void *blob, int node,
460c6202d85SSimon Glass 				struct dm_i2c_chip *chip)
461c6202d85SSimon Glass {
4627132b9fdSSimon Glass 	chip->offset_len = fdtdec_get_int(gd->fdt_blob, node,
4637132b9fdSSimon Glass 					  "u-boot,i2c-offset-len", 1);
464c6202d85SSimon Glass 	chip->flags = 0;
465c6202d85SSimon Glass 	chip->chip_addr = fdtdec_get_int(gd->fdt_blob, node, "reg", -1);
466c6202d85SSimon Glass 	if (chip->chip_addr == -1) {
467c6202d85SSimon Glass 		debug("%s: I2C Node '%s' has no 'reg' property\n", __func__,
468c6202d85SSimon Glass 		      fdt_get_name(blob, node, NULL));
469c6202d85SSimon Glass 		return -EINVAL;
470c6202d85SSimon Glass 	}
471c6202d85SSimon Glass 
472c6202d85SSimon Glass 	return 0;
473c6202d85SSimon Glass }
474c6202d85SSimon Glass 
475c6202d85SSimon Glass static int i2c_post_probe(struct udevice *dev)
476c6202d85SSimon Glass {
477e564f054SSimon Glass 	struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
478c6202d85SSimon Glass 
479c6202d85SSimon Glass 	i2c->speed_hz = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
480c6202d85SSimon Glass 				     "clock-frequency", 100000);
481c6202d85SSimon Glass 
482ca88b9b9SSimon Glass 	return dm_i2c_set_bus_speed(dev, i2c->speed_hz);
483c6202d85SSimon Glass }
484c6202d85SSimon Glass 
485e6f66ec0SSimon Glass static int i2c_post_bind(struct udevice *dev)
486c6202d85SSimon Glass {
487c6202d85SSimon Glass 	/* Scan the bus for devices */
488c6202d85SSimon Glass 	return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
489c6202d85SSimon Glass }
490c6202d85SSimon Glass 
491e6f66ec0SSimon Glass static int i2c_child_post_bind(struct udevice *dev)
492e6f66ec0SSimon Glass {
493e6f66ec0SSimon Glass 	struct dm_i2c_chip *plat = dev_get_parent_platdata(dev);
494e6f66ec0SSimon Glass 
495e6f66ec0SSimon Glass 	if (dev->of_offset == -1)
496e6f66ec0SSimon Glass 		return 0;
497e6f66ec0SSimon Glass 
498e6f66ec0SSimon Glass 	return i2c_chip_ofdata_to_platdata(gd->fdt_blob, dev->of_offset, plat);
499e6f66ec0SSimon Glass }
500e6f66ec0SSimon Glass 
501c6202d85SSimon Glass UCLASS_DRIVER(i2c) = {
502c6202d85SSimon Glass 	.id		= UCLASS_I2C,
503c6202d85SSimon Glass 	.name		= "i2c",
5049cc36a2bSSimon Glass 	.flags		= DM_UC_FLAG_SEQ_ALIAS,
505c6202d85SSimon Glass 	.post_bind	= i2c_post_bind,
506c6202d85SSimon Glass 	.post_probe	= i2c_post_probe,
507e6f66ec0SSimon Glass 	.per_device_auto_alloc_size = sizeof(struct dm_i2c_bus),
508e6f66ec0SSimon Glass 	.per_child_platdata_auto_alloc_size = sizeof(struct dm_i2c_chip),
509e6f66ec0SSimon Glass 	.child_post_bind = i2c_child_post_bind,
510c6202d85SSimon Glass };
511c6202d85SSimon Glass 
512c6202d85SSimon Glass UCLASS_DRIVER(i2c_generic) = {
513c6202d85SSimon Glass 	.id		= UCLASS_I2C_GENERIC,
514c6202d85SSimon Glass 	.name		= "i2c_generic",
515c6202d85SSimon Glass };
516c6202d85SSimon Glass 
517c6202d85SSimon Glass U_BOOT_DRIVER(i2c_generic_chip_drv) = {
518c6202d85SSimon Glass 	.name		= "i2c_generic_chip_drv",
519c6202d85SSimon Glass 	.id		= UCLASS_I2C_GENERIC,
520c6202d85SSimon Glass };
521