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