xref: /openbmc/u-boot/drivers/i2c/i2c-uclass.c (revision ca88b9b93916f66c6737527aa955d2c1b4758080)
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 
189c6202d85SSimon Glass /**
190c6202d85SSimon Glass  * i2c_probe_chip() - probe for a chip on a bus
191c6202d85SSimon Glass  *
192c6202d85SSimon Glass  * @bus:	Bus to probe
193c6202d85SSimon Glass  * @chip_addr:	Chip address to probe
194c6202d85SSimon Glass  * @flags:	Flags for the chip
195c6202d85SSimon Glass  * @return 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip
196c6202d85SSimon Glass  * does not respond to probe
197c6202d85SSimon Glass  */
198c6202d85SSimon Glass static int i2c_probe_chip(struct udevice *bus, uint chip_addr,
199c6202d85SSimon Glass 			  enum dm_i2c_chip_flags chip_flags)
200c6202d85SSimon Glass {
201c6202d85SSimon Glass 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
202c6202d85SSimon Glass 	struct i2c_msg msg[1];
203c6202d85SSimon Glass 	int ret;
204c6202d85SSimon Glass 
205c6202d85SSimon Glass 	if (ops->probe_chip) {
206c6202d85SSimon Glass 		ret = ops->probe_chip(bus, chip_addr, chip_flags);
207c6202d85SSimon Glass 		if (!ret || ret != -ENOSYS)
208c6202d85SSimon Glass 			return ret;
209c6202d85SSimon Glass 	}
210c6202d85SSimon Glass 
211c6202d85SSimon Glass 	if (!ops->xfer)
212c6202d85SSimon Glass 		return -ENOSYS;
213c6202d85SSimon Glass 
214c6202d85SSimon Glass 	/* Probe with a zero-length message */
215c6202d85SSimon Glass 	msg->addr = chip_addr;
216c6202d85SSimon Glass 	msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
217c6202d85SSimon Glass 	msg->len = 0;
218c6202d85SSimon Glass 	msg->buf = NULL;
219c6202d85SSimon Glass 
220c6202d85SSimon Glass 	return ops->xfer(bus, msg, 1);
221c6202d85SSimon Glass }
222c6202d85SSimon Glass 
22325ab4b03SSimon Glass static int i2c_bind_driver(struct udevice *bus, uint chip_addr, uint offset_len,
224c6202d85SSimon Glass 			   struct udevice **devp)
225c6202d85SSimon Glass {
226e6f66ec0SSimon Glass 	struct dm_i2c_chip *chip;
227c6202d85SSimon Glass 	char name[30], *str;
228c6202d85SSimon Glass 	struct udevice *dev;
229c6202d85SSimon Glass 	int ret;
230c6202d85SSimon Glass 
231c6202d85SSimon Glass 	snprintf(name, sizeof(name), "generic_%x", chip_addr);
232c6202d85SSimon Glass 	str = strdup(name);
233c6202d85SSimon Glass 	ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev);
234c6202d85SSimon Glass 	debug("%s:  device_bind_driver: ret=%d\n", __func__, ret);
235c6202d85SSimon Glass 	if (ret)
236c6202d85SSimon Glass 		goto err_bind;
237c6202d85SSimon Glass 
238c6202d85SSimon Glass 	/* Tell the device what we know about it */
239e6f66ec0SSimon Glass 	chip = dev_get_parent_platdata(dev);
240e6f66ec0SSimon Glass 	chip->chip_addr = chip_addr;
241e6f66ec0SSimon Glass 	chip->offset_len = offset_len;
242e6f66ec0SSimon Glass 	ret = device_probe(dev);
243e6f66ec0SSimon Glass 	debug("%s:  device_probe: ret=%d\n", __func__, ret);
244c6202d85SSimon Glass 	if (ret)
245c6202d85SSimon Glass 		goto err_probe;
246c6202d85SSimon Glass 
247c6202d85SSimon Glass 	*devp = dev;
248c6202d85SSimon Glass 	return 0;
249c6202d85SSimon Glass 
250c6202d85SSimon Glass err_probe:
251e6f66ec0SSimon Glass 	/*
252e6f66ec0SSimon Glass 	 * If the device failed to probe, unbind it. There is nothing there
253e6f66ec0SSimon Glass 	 * on the bus so we don't want to leave it lying around
254e6f66ec0SSimon Glass 	 */
255c6202d85SSimon Glass 	device_unbind(dev);
256c6202d85SSimon Glass err_bind:
257c6202d85SSimon Glass 	free(str);
258c6202d85SSimon Glass 	return ret;
259c6202d85SSimon Glass }
260c6202d85SSimon Glass 
26125ab4b03SSimon Glass int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
26225ab4b03SSimon Glass 		 struct udevice **devp)
263c6202d85SSimon Glass {
264c6202d85SSimon Glass 	struct udevice *dev;
265c6202d85SSimon Glass 
266c6202d85SSimon Glass 	debug("%s: Searching bus '%s' for address %02x: ", __func__,
267c6202d85SSimon Glass 	      bus->name, chip_addr);
268c6202d85SSimon Glass 	for (device_find_first_child(bus, &dev); dev;
269c6202d85SSimon Glass 			device_find_next_child(&dev)) {
270e6f66ec0SSimon Glass 		struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
271c6202d85SSimon Glass 		int ret;
272c6202d85SSimon Glass 
273c6202d85SSimon Glass 		if (chip->chip_addr == chip_addr) {
274c6202d85SSimon Glass 			ret = device_probe(dev);
275c6202d85SSimon Glass 			debug("found, ret=%d\n", ret);
276c6202d85SSimon Glass 			if (ret)
277c6202d85SSimon Glass 				return ret;
278c6202d85SSimon Glass 			*devp = dev;
279c6202d85SSimon Glass 			return 0;
280c6202d85SSimon Glass 		}
281c6202d85SSimon Glass 	}
282c6202d85SSimon Glass 	debug("not found\n");
28325ab4b03SSimon Glass 	return i2c_bind_driver(bus, chip_addr, offset_len, devp);
284c6202d85SSimon Glass }
285c6202d85SSimon Glass 
28625ab4b03SSimon Glass int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
28725ab4b03SSimon Glass 			    struct udevice **devp)
288c6202d85SSimon Glass {
289c6202d85SSimon Glass 	struct udevice *bus;
290c6202d85SSimon Glass 	int ret;
291c6202d85SSimon Glass 
292c6202d85SSimon Glass 	ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
293c6202d85SSimon Glass 	if (ret) {
294c6202d85SSimon Glass 		debug("Cannot find I2C bus %d\n", busnum);
295c6202d85SSimon Glass 		return ret;
296c6202d85SSimon Glass 	}
29725ab4b03SSimon Glass 	ret = i2c_get_chip(bus, chip_addr, offset_len, devp);
298c6202d85SSimon Glass 	if (ret) {
299c6202d85SSimon Glass 		debug("Cannot find I2C chip %02x on bus %d\n", chip_addr,
300c6202d85SSimon Glass 		      busnum);
301c6202d85SSimon Glass 		return ret;
302c6202d85SSimon Glass 	}
303c6202d85SSimon Glass 
304c6202d85SSimon Glass 	return 0;
305c6202d85SSimon Glass }
306c6202d85SSimon Glass 
307f9a4c2daSSimon Glass int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
308c6202d85SSimon Glass 		 struct udevice **devp)
309c6202d85SSimon Glass {
310c6202d85SSimon Glass 	int ret;
311c6202d85SSimon Glass 
312c6202d85SSimon Glass 	*devp = NULL;
313c6202d85SSimon Glass 
314c6202d85SSimon Glass 	/* First probe that chip */
315c6202d85SSimon Glass 	ret = i2c_probe_chip(bus, chip_addr, chip_flags);
316c6202d85SSimon Glass 	debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
317c6202d85SSimon Glass 	      chip_addr, ret);
318c6202d85SSimon Glass 	if (ret)
319c6202d85SSimon Glass 		return ret;
320c6202d85SSimon Glass 
321c6202d85SSimon Glass 	/* The chip was found, see if we have a driver, and probe it */
32225ab4b03SSimon Glass 	ret = i2c_get_chip(bus, chip_addr, 1, devp);
323c6202d85SSimon Glass 	debug("%s:  i2c_get_chip: ret=%d\n", __func__, ret);
324c6202d85SSimon Glass 
325c6202d85SSimon Glass 	return ret;
326c6202d85SSimon Glass }
327c6202d85SSimon Glass 
328*ca88b9b9SSimon Glass int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
329c6202d85SSimon Glass {
330c6202d85SSimon Glass 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
331c6202d85SSimon Glass 	struct dm_i2c_bus *i2c = bus->uclass_priv;
332c6202d85SSimon Glass 	int ret;
333c6202d85SSimon Glass 
334c6202d85SSimon Glass 	/*
335c6202d85SSimon Glass 	 * If we have a method, call it. If not then the driver probably wants
336c6202d85SSimon Glass 	 * to deal with speed changes on the next transfer. It can easily read
337c6202d85SSimon Glass 	 * the current speed from this uclass
338c6202d85SSimon Glass 	 */
339c6202d85SSimon Glass 	if (ops->set_bus_speed) {
340c6202d85SSimon Glass 		ret = ops->set_bus_speed(bus, speed);
341c6202d85SSimon Glass 		if (ret)
342c6202d85SSimon Glass 			return ret;
343c6202d85SSimon Glass 	}
344c6202d85SSimon Glass 	i2c->speed_hz = speed;
345c6202d85SSimon Glass 
346c6202d85SSimon Glass 	return 0;
347c6202d85SSimon Glass }
348c6202d85SSimon Glass 
349*ca88b9b9SSimon Glass int dm_i2c_get_bus_speed(struct udevice *bus)
350c6202d85SSimon Glass {
351c6202d85SSimon Glass 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
352c6202d85SSimon Glass 	struct dm_i2c_bus *i2c = bus->uclass_priv;
353c6202d85SSimon Glass 
354c6202d85SSimon Glass 	if (!ops->get_bus_speed)
355c6202d85SSimon Glass 		return i2c->speed_hz;
356c6202d85SSimon Glass 
357c6202d85SSimon Glass 	return ops->get_bus_speed(bus);
358c6202d85SSimon Glass }
359c6202d85SSimon Glass 
360c6202d85SSimon Glass int i2c_set_chip_flags(struct udevice *dev, uint flags)
361c6202d85SSimon Glass {
362c6202d85SSimon Glass 	struct udevice *bus = dev->parent;
363e6f66ec0SSimon Glass 	struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
364c6202d85SSimon Glass 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
365c6202d85SSimon Glass 	int ret;
366c6202d85SSimon Glass 
367c6202d85SSimon Glass 	if (ops->set_flags) {
368c6202d85SSimon Glass 		ret = ops->set_flags(dev, flags);
369c6202d85SSimon Glass 		if (ret)
370c6202d85SSimon Glass 			return ret;
371c6202d85SSimon Glass 	}
372c6202d85SSimon Glass 	chip->flags = flags;
373c6202d85SSimon Glass 
374c6202d85SSimon Glass 	return 0;
375c6202d85SSimon Glass }
376c6202d85SSimon Glass 
377c6202d85SSimon Glass int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
378c6202d85SSimon Glass {
379e6f66ec0SSimon Glass 	struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
380c6202d85SSimon Glass 
381c6202d85SSimon Glass 	*flagsp = chip->flags;
382c6202d85SSimon Glass 
383c6202d85SSimon Glass 	return 0;
384c6202d85SSimon Glass }
385c6202d85SSimon Glass 
386c6202d85SSimon Glass int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
387c6202d85SSimon Glass {
388e6f66ec0SSimon Glass 	struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
389c6202d85SSimon Glass 
390c6202d85SSimon Glass 	if (offset_len > I2C_MAX_OFFSET_LEN)
391c6202d85SSimon Glass 		return -EINVAL;
392c6202d85SSimon Glass 	chip->offset_len = offset_len;
393c6202d85SSimon Glass 
394c6202d85SSimon Glass 	return 0;
395c6202d85SSimon Glass }
396c6202d85SSimon Glass 
397c6202d85SSimon Glass int i2c_deblock(struct udevice *bus)
398c6202d85SSimon Glass {
399c6202d85SSimon Glass 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
400c6202d85SSimon Glass 
401c6202d85SSimon Glass 	/*
402c6202d85SSimon Glass 	 * We could implement a software deblocking here if we could get
403c6202d85SSimon Glass 	 * access to the GPIOs used by I2C, and switch them to GPIO mode
404c6202d85SSimon Glass 	 * and then back to I2C. This is somewhat beyond our powers in
405c6202d85SSimon Glass 	 * driver model at present, so for now just fail.
406c6202d85SSimon Glass 	 *
407c6202d85SSimon Glass 	 * See https://patchwork.ozlabs.org/patch/399040/
408c6202d85SSimon Glass 	 */
409c6202d85SSimon Glass 	if (!ops->deblock)
410c6202d85SSimon Glass 		return -ENOSYS;
411c6202d85SSimon Glass 
412c6202d85SSimon Glass 	return ops->deblock(bus);
413c6202d85SSimon Glass }
414c6202d85SSimon Glass 
415c6202d85SSimon Glass int i2c_chip_ofdata_to_platdata(const void *blob, int node,
416c6202d85SSimon Glass 				struct dm_i2c_chip *chip)
417c6202d85SSimon Glass {
4187132b9fdSSimon Glass 	chip->offset_len = fdtdec_get_int(gd->fdt_blob, node,
4197132b9fdSSimon Glass 					  "u-boot,i2c-offset-len", 1);
420c6202d85SSimon Glass 	chip->flags = 0;
421c6202d85SSimon Glass 	chip->chip_addr = fdtdec_get_int(gd->fdt_blob, node, "reg", -1);
422c6202d85SSimon Glass 	if (chip->chip_addr == -1) {
423c6202d85SSimon Glass 		debug("%s: I2C Node '%s' has no 'reg' property\n", __func__,
424c6202d85SSimon Glass 		      fdt_get_name(blob, node, NULL));
425c6202d85SSimon Glass 		return -EINVAL;
426c6202d85SSimon Glass 	}
427c6202d85SSimon Glass 
428c6202d85SSimon Glass 	return 0;
429c6202d85SSimon Glass }
430c6202d85SSimon Glass 
431c6202d85SSimon Glass static int i2c_post_probe(struct udevice *dev)
432c6202d85SSimon Glass {
433c6202d85SSimon Glass 	struct dm_i2c_bus *i2c = dev->uclass_priv;
434c6202d85SSimon Glass 
435c6202d85SSimon Glass 	i2c->speed_hz = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
436c6202d85SSimon Glass 				     "clock-frequency", 100000);
437c6202d85SSimon Glass 
438*ca88b9b9SSimon Glass 	return dm_i2c_set_bus_speed(dev, i2c->speed_hz);
439c6202d85SSimon Glass }
440c6202d85SSimon Glass 
441e6f66ec0SSimon Glass static int i2c_post_bind(struct udevice *dev)
442c6202d85SSimon Glass {
443c6202d85SSimon Glass 	/* Scan the bus for devices */
444c6202d85SSimon Glass 	return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
445c6202d85SSimon Glass }
446c6202d85SSimon Glass 
447e6f66ec0SSimon Glass static int i2c_child_post_bind(struct udevice *dev)
448e6f66ec0SSimon Glass {
449e6f66ec0SSimon Glass 	struct dm_i2c_chip *plat = dev_get_parent_platdata(dev);
450e6f66ec0SSimon Glass 
451e6f66ec0SSimon Glass 	if (dev->of_offset == -1)
452e6f66ec0SSimon Glass 		return 0;
453e6f66ec0SSimon Glass 
454e6f66ec0SSimon Glass 	return i2c_chip_ofdata_to_platdata(gd->fdt_blob, dev->of_offset, plat);
455e6f66ec0SSimon Glass }
456e6f66ec0SSimon Glass 
457c6202d85SSimon Glass UCLASS_DRIVER(i2c) = {
458c6202d85SSimon Glass 	.id		= UCLASS_I2C,
459c6202d85SSimon Glass 	.name		= "i2c",
4609cc36a2bSSimon Glass 	.flags		= DM_UC_FLAG_SEQ_ALIAS,
461c6202d85SSimon Glass 	.post_bind	= i2c_post_bind,
462c6202d85SSimon Glass 	.post_probe	= i2c_post_probe,
463e6f66ec0SSimon Glass 	.per_device_auto_alloc_size = sizeof(struct dm_i2c_bus),
464e6f66ec0SSimon Glass 	.per_child_platdata_auto_alloc_size = sizeof(struct dm_i2c_chip),
465e6f66ec0SSimon Glass 	.child_post_bind = i2c_child_post_bind,
466c6202d85SSimon Glass };
467c6202d85SSimon Glass 
468c6202d85SSimon Glass UCLASS_DRIVER(i2c_generic) = {
469c6202d85SSimon Glass 	.id		= UCLASS_I2C_GENERIC,
470c6202d85SSimon Glass 	.name		= "i2c_generic",
471c6202d85SSimon Glass };
472c6202d85SSimon Glass 
473c6202d85SSimon Glass U_BOOT_DRIVER(i2c_generic_chip_drv) = {
474c6202d85SSimon Glass 	.name		= "i2c_generic_chip_drv",
475c6202d85SSimon Glass 	.id		= UCLASS_I2C_GENERIC,
476c6202d85SSimon Glass };
477