xref: /openbmc/u-boot/drivers/i2c/i2c-uclass.c (revision 25ab4b0303f2df5e6b94ed92e37875a7c98f4de3)
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 {
53c6202d85SSimon Glass 	struct dm_i2c_chip *chip = dev_get_parentdata(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 {
82c6202d85SSimon Glass 	struct dm_i2c_chip *chip = dev_get_parentdata(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 {
105c6202d85SSimon Glass 	struct dm_i2c_chip *chip = dev_get_parentdata(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 {
136c6202d85SSimon Glass 	struct dm_i2c_chip *chip = dev_get_parentdata(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 
223*25ab4b03SSimon Glass static int i2c_bind_driver(struct udevice *bus, uint chip_addr, uint offset_len,
224c6202d85SSimon Glass 			   struct udevice **devp)
225c6202d85SSimon Glass {
226c6202d85SSimon 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 */
239c6202d85SSimon Glass 	memset(&chip, '\0', sizeof(chip));
240c6202d85SSimon Glass 	chip.chip_addr = chip_addr;
241*25ab4b03SSimon Glass 	chip.offset_len = offset_len;
242c6202d85SSimon Glass 	ret = device_probe_child(dev, &chip);
243c6202d85SSimon Glass 	debug("%s:  device_probe_child: 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:
251c6202d85SSimon Glass 	device_unbind(dev);
252c6202d85SSimon Glass err_bind:
253c6202d85SSimon Glass 	free(str);
254c6202d85SSimon Glass 	return ret;
255c6202d85SSimon Glass }
256c6202d85SSimon Glass 
257*25ab4b03SSimon Glass int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
258*25ab4b03SSimon Glass 		 struct udevice **devp)
259c6202d85SSimon Glass {
260c6202d85SSimon Glass 	struct udevice *dev;
261c6202d85SSimon Glass 
262c6202d85SSimon Glass 	debug("%s: Searching bus '%s' for address %02x: ", __func__,
263c6202d85SSimon Glass 	      bus->name, chip_addr);
264c6202d85SSimon Glass 	for (device_find_first_child(bus, &dev); dev;
265c6202d85SSimon Glass 			device_find_next_child(&dev)) {
266c6202d85SSimon Glass 		struct dm_i2c_chip store;
267c6202d85SSimon Glass 		struct dm_i2c_chip *chip = dev_get_parentdata(dev);
268c6202d85SSimon Glass 		int ret;
269c6202d85SSimon Glass 
270c6202d85SSimon Glass 		if (!chip) {
271c6202d85SSimon Glass 			chip = &store;
272c6202d85SSimon Glass 			i2c_chip_ofdata_to_platdata(gd->fdt_blob,
273c6202d85SSimon Glass 						    dev->of_offset, chip);
274c6202d85SSimon Glass 		}
275c6202d85SSimon Glass 		if (chip->chip_addr == chip_addr) {
276c6202d85SSimon Glass 			ret = device_probe(dev);
277c6202d85SSimon Glass 			debug("found, ret=%d\n", ret);
278c6202d85SSimon Glass 			if (ret)
279c6202d85SSimon Glass 				return ret;
280c6202d85SSimon Glass 			*devp = dev;
281c6202d85SSimon Glass 			return 0;
282c6202d85SSimon Glass 		}
283c6202d85SSimon Glass 	}
284c6202d85SSimon Glass 	debug("not found\n");
285*25ab4b03SSimon Glass 	return i2c_bind_driver(bus, chip_addr, offset_len, devp);
286c6202d85SSimon Glass }
287c6202d85SSimon Glass 
288*25ab4b03SSimon Glass int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
289*25ab4b03SSimon Glass 			    struct udevice **devp)
290c6202d85SSimon Glass {
291c6202d85SSimon Glass 	struct udevice *bus;
292c6202d85SSimon Glass 	int ret;
293c6202d85SSimon Glass 
294c6202d85SSimon Glass 	ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
295c6202d85SSimon Glass 	if (ret) {
296c6202d85SSimon Glass 		debug("Cannot find I2C bus %d\n", busnum);
297c6202d85SSimon Glass 		return ret;
298c6202d85SSimon Glass 	}
299*25ab4b03SSimon Glass 	ret = i2c_get_chip(bus, chip_addr, offset_len, devp);
300c6202d85SSimon Glass 	if (ret) {
301c6202d85SSimon Glass 		debug("Cannot find I2C chip %02x on bus %d\n", chip_addr,
302c6202d85SSimon Glass 		      busnum);
303c6202d85SSimon Glass 		return ret;
304c6202d85SSimon Glass 	}
305c6202d85SSimon Glass 
306c6202d85SSimon Glass 	return 0;
307c6202d85SSimon Glass }
308c6202d85SSimon Glass 
309f9a4c2daSSimon Glass int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
310c6202d85SSimon Glass 		 struct udevice **devp)
311c6202d85SSimon Glass {
312c6202d85SSimon Glass 	int ret;
313c6202d85SSimon Glass 
314c6202d85SSimon Glass 	*devp = NULL;
315c6202d85SSimon Glass 
316c6202d85SSimon Glass 	/* First probe that chip */
317c6202d85SSimon Glass 	ret = i2c_probe_chip(bus, chip_addr, chip_flags);
318c6202d85SSimon Glass 	debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
319c6202d85SSimon Glass 	      chip_addr, ret);
320c6202d85SSimon Glass 	if (ret)
321c6202d85SSimon Glass 		return ret;
322c6202d85SSimon Glass 
323c6202d85SSimon Glass 	/* The chip was found, see if we have a driver, and probe it */
324*25ab4b03SSimon Glass 	ret = i2c_get_chip(bus, chip_addr, 1, devp);
325c6202d85SSimon Glass 	debug("%s:  i2c_get_chip: ret=%d\n", __func__, ret);
326c6202d85SSimon Glass 
327c6202d85SSimon Glass 	return ret;
328c6202d85SSimon Glass }
329c6202d85SSimon Glass 
330c6202d85SSimon Glass int i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
331c6202d85SSimon Glass {
332c6202d85SSimon Glass 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
333c6202d85SSimon Glass 	struct dm_i2c_bus *i2c = bus->uclass_priv;
334c6202d85SSimon Glass 	int ret;
335c6202d85SSimon Glass 
336c6202d85SSimon Glass 	/*
337c6202d85SSimon Glass 	 * If we have a method, call it. If not then the driver probably wants
338c6202d85SSimon Glass 	 * to deal with speed changes on the next transfer. It can easily read
339c6202d85SSimon Glass 	 * the current speed from this uclass
340c6202d85SSimon Glass 	 */
341c6202d85SSimon Glass 	if (ops->set_bus_speed) {
342c6202d85SSimon Glass 		ret = ops->set_bus_speed(bus, speed);
343c6202d85SSimon Glass 		if (ret)
344c6202d85SSimon Glass 			return ret;
345c6202d85SSimon Glass 	}
346c6202d85SSimon Glass 	i2c->speed_hz = speed;
347c6202d85SSimon Glass 
348c6202d85SSimon Glass 	return 0;
349c6202d85SSimon Glass }
350c6202d85SSimon Glass 
351c6202d85SSimon Glass /*
352c6202d85SSimon Glass  * i2c_get_bus_speed:
353c6202d85SSimon Glass  *
354c6202d85SSimon Glass  *  Returns speed of selected I2C bus in Hz
355c6202d85SSimon Glass  */
356c6202d85SSimon Glass int i2c_get_bus_speed(struct udevice *bus)
357c6202d85SSimon Glass {
358c6202d85SSimon Glass 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
359c6202d85SSimon Glass 	struct dm_i2c_bus *i2c = bus->uclass_priv;
360c6202d85SSimon Glass 
361c6202d85SSimon Glass 	if (!ops->get_bus_speed)
362c6202d85SSimon Glass 		return i2c->speed_hz;
363c6202d85SSimon Glass 
364c6202d85SSimon Glass 	return ops->get_bus_speed(bus);
365c6202d85SSimon Glass }
366c6202d85SSimon Glass 
367c6202d85SSimon Glass int i2c_set_chip_flags(struct udevice *dev, uint flags)
368c6202d85SSimon Glass {
369c6202d85SSimon Glass 	struct udevice *bus = dev->parent;
370c6202d85SSimon Glass 	struct dm_i2c_chip *chip = dev_get_parentdata(dev);
371c6202d85SSimon Glass 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
372c6202d85SSimon Glass 	int ret;
373c6202d85SSimon Glass 
374c6202d85SSimon Glass 	if (ops->set_flags) {
375c6202d85SSimon Glass 		ret = ops->set_flags(dev, flags);
376c6202d85SSimon Glass 		if (ret)
377c6202d85SSimon Glass 			return ret;
378c6202d85SSimon Glass 	}
379c6202d85SSimon Glass 	chip->flags = flags;
380c6202d85SSimon Glass 
381c6202d85SSimon Glass 	return 0;
382c6202d85SSimon Glass }
383c6202d85SSimon Glass 
384c6202d85SSimon Glass int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
385c6202d85SSimon Glass {
386c6202d85SSimon Glass 	struct dm_i2c_chip *chip = dev_get_parentdata(dev);
387c6202d85SSimon Glass 
388c6202d85SSimon Glass 	*flagsp = chip->flags;
389c6202d85SSimon Glass 
390c6202d85SSimon Glass 	return 0;
391c6202d85SSimon Glass }
392c6202d85SSimon Glass 
393c6202d85SSimon Glass int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
394c6202d85SSimon Glass {
395c6202d85SSimon Glass 	struct dm_i2c_chip *chip = dev_get_parentdata(dev);
396c6202d85SSimon Glass 
397c6202d85SSimon Glass 	if (offset_len > I2C_MAX_OFFSET_LEN)
398c6202d85SSimon Glass 		return -EINVAL;
399c6202d85SSimon Glass 	chip->offset_len = offset_len;
400c6202d85SSimon Glass 
401c6202d85SSimon Glass 	return 0;
402c6202d85SSimon Glass }
403c6202d85SSimon Glass 
404c6202d85SSimon Glass int i2c_deblock(struct udevice *bus)
405c6202d85SSimon Glass {
406c6202d85SSimon Glass 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
407c6202d85SSimon Glass 
408c6202d85SSimon Glass 	/*
409c6202d85SSimon Glass 	 * We could implement a software deblocking here if we could get
410c6202d85SSimon Glass 	 * access to the GPIOs used by I2C, and switch them to GPIO mode
411c6202d85SSimon Glass 	 * and then back to I2C. This is somewhat beyond our powers in
412c6202d85SSimon Glass 	 * driver model at present, so for now just fail.
413c6202d85SSimon Glass 	 *
414c6202d85SSimon Glass 	 * See https://patchwork.ozlabs.org/patch/399040/
415c6202d85SSimon Glass 	 */
416c6202d85SSimon Glass 	if (!ops->deblock)
417c6202d85SSimon Glass 		return -ENOSYS;
418c6202d85SSimon Glass 
419c6202d85SSimon Glass 	return ops->deblock(bus);
420c6202d85SSimon Glass }
421c6202d85SSimon Glass 
422c6202d85SSimon Glass int i2c_chip_ofdata_to_platdata(const void *blob, int node,
423c6202d85SSimon Glass 				struct dm_i2c_chip *chip)
424c6202d85SSimon Glass {
425c6202d85SSimon Glass 	chip->offset_len = 1;	/* default */
426c6202d85SSimon Glass 	chip->flags = 0;
427c6202d85SSimon Glass 	chip->chip_addr = fdtdec_get_int(gd->fdt_blob, node, "reg", -1);
428c6202d85SSimon Glass 	if (chip->chip_addr == -1) {
429c6202d85SSimon Glass 		debug("%s: I2C Node '%s' has no 'reg' property\n", __func__,
430c6202d85SSimon Glass 		      fdt_get_name(blob, node, NULL));
431c6202d85SSimon Glass 		return -EINVAL;
432c6202d85SSimon Glass 	}
433c6202d85SSimon Glass 
434c6202d85SSimon Glass 	return 0;
435c6202d85SSimon Glass }
436c6202d85SSimon Glass 
437c6202d85SSimon Glass static int i2c_post_probe(struct udevice *dev)
438c6202d85SSimon Glass {
439c6202d85SSimon Glass 	struct dm_i2c_bus *i2c = dev->uclass_priv;
440c6202d85SSimon Glass 
441c6202d85SSimon Glass 	i2c->speed_hz = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
442c6202d85SSimon Glass 				     "clock-frequency", 100000);
443c6202d85SSimon Glass 
444c6202d85SSimon Glass 	return i2c_set_bus_speed(dev, i2c->speed_hz);
445c6202d85SSimon Glass }
446c6202d85SSimon Glass 
447c6202d85SSimon Glass int i2c_post_bind(struct udevice *dev)
448c6202d85SSimon Glass {
449c6202d85SSimon Glass 	/* Scan the bus for devices */
450c6202d85SSimon Glass 	return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
451c6202d85SSimon Glass }
452c6202d85SSimon Glass 
453c6202d85SSimon Glass UCLASS_DRIVER(i2c) = {
454c6202d85SSimon Glass 	.id		= UCLASS_I2C,
455c6202d85SSimon Glass 	.name		= "i2c",
456c6202d85SSimon Glass 	.per_device_auto_alloc_size = sizeof(struct dm_i2c_bus),
457c6202d85SSimon Glass 	.post_bind	= i2c_post_bind,
458c6202d85SSimon Glass 	.post_probe	= i2c_post_probe,
459c6202d85SSimon Glass };
460c6202d85SSimon Glass 
461c6202d85SSimon Glass UCLASS_DRIVER(i2c_generic) = {
462c6202d85SSimon Glass 	.id		= UCLASS_I2C_GENERIC,
463c6202d85SSimon Glass 	.name		= "i2c_generic",
464c6202d85SSimon Glass };
465c6202d85SSimon Glass 
466c6202d85SSimon Glass U_BOOT_DRIVER(i2c_generic_chip_drv) = {
467c6202d85SSimon Glass 	.name		= "i2c_generic_chip_drv",
468c6202d85SSimon Glass 	.id		= UCLASS_I2C_GENERIC,
469c6202d85SSimon Glass };
470