1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2014 Google, Inc
4  */
5 
6 #define LOG_CATEGORY UCLASS_I2C
7 
8 #include <common.h>
9 #include <dm.h>
10 #include <errno.h>
11 #include <i2c.h>
12 #include <log.h>
13 #include <malloc.h>
14 #include <acpi/acpi_device.h>
15 #include <dm/acpi.h>
16 #include <dm/device-internal.h>
17 #include <dm/lists.h>
18 #include <dm/pinctrl.h>
19 #if CONFIG_IS_ENABLED(DM_GPIO)
20 #include <asm/gpio.h>
21 #endif
22 #include <linux/delay.h>
23 #include "acpi_i2c.h"
24 
25 #define I2C_MAX_OFFSET_LEN	4
26 
27 enum {
28 	PIN_SDA = 0,
29 	PIN_SCL,
30 	PIN_COUNT,
31 };
32 
33 /* Useful debugging function */
i2c_dump_msgs(struct i2c_msg * msg,int nmsgs)34 void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs)
35 {
36 	int i;
37 
38 	for (i = 0; i < nmsgs; i++) {
39 		struct i2c_msg *m = &msg[i];
40 
41 		printf("   %s %x len=%x", m->flags & I2C_M_RD ? "R" : "W",
42 		       msg->addr, msg->len);
43 		if (!(m->flags & I2C_M_RD))
44 			printf(": %x", m->buf[0]);
45 		printf("\n");
46 	}
47 }
48 
49 /**
50  * i2c_setup_offset() - Set up a new message with a chip offset
51  *
52  * @chip:	Chip to use
53  * @offset:	Byte offset within chip
54  * @offset_buf:	Place to put byte offset
55  * @msg:	Message buffer
56  * @return 0 if OK, -EADDRNOTAVAIL if the offset length is 0. In that case the
57  * message is still set up but will not contain an offset.
58  */
i2c_setup_offset(struct dm_i2c_chip * chip,uint offset,uint8_t offset_buf[],struct i2c_msg * msg)59 static int i2c_setup_offset(struct dm_i2c_chip *chip, uint offset,
60 			    uint8_t offset_buf[], struct i2c_msg *msg)
61 {
62 	int offset_len = chip->offset_len;
63 
64 	msg->addr = chip->chip_addr;
65 	if (chip->chip_addr_offset_mask)
66 		msg->addr |= (offset >> (8 * offset_len)) &
67 			chip->chip_addr_offset_mask;
68 	msg->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
69 	msg->len = chip->offset_len;
70 	msg->buf = offset_buf;
71 	if (!offset_len)
72 		return -EADDRNOTAVAIL;
73 	assert(offset_len <= I2C_MAX_OFFSET_LEN);
74 
75 	while (offset_len--)
76 		*offset_buf++ = offset >> (8 * offset_len);
77 
78 	return 0;
79 }
80 
i2c_read_bytewise(struct udevice * dev,uint offset,uint8_t * buffer,int len)81 static int i2c_read_bytewise(struct udevice *dev, uint offset,
82 			     uint8_t *buffer, int len)
83 {
84 	struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
85 	struct udevice *bus = dev_get_parent(dev);
86 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
87 	struct i2c_msg msg[2], *ptr;
88 	uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
89 	int ret;
90 	int i;
91 
92 	for (i = 0; i < len; i++) {
93 		if (i2c_setup_offset(chip, offset + i, offset_buf, msg))
94 			return -EINVAL;
95 		ptr = msg + 1;
96 		ptr->addr = msg->addr;
97 		ptr->flags = msg->flags | I2C_M_RD;
98 		ptr->len = 1;
99 		ptr->buf = &buffer[i];
100 		ptr++;
101 
102 		ret = ops->xfer(bus, msg, ptr - msg);
103 		if (ret)
104 			return ret;
105 	}
106 
107 	return 0;
108 }
109 
i2c_write_bytewise(struct udevice * dev,uint offset,const uint8_t * buffer,int len)110 static int i2c_write_bytewise(struct udevice *dev, uint offset,
111 			     const uint8_t *buffer, int len)
112 {
113 	struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
114 	struct udevice *bus = dev_get_parent(dev);
115 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
116 	struct i2c_msg msg[1];
117 	uint8_t buf[I2C_MAX_OFFSET_LEN + 1];
118 	int ret;
119 	int i;
120 
121 	for (i = 0; i < len; i++) {
122 		if (i2c_setup_offset(chip, offset + i, buf, msg))
123 			return -EINVAL;
124 		buf[msg->len++] = buffer[i];
125 
126 		ret = ops->xfer(bus, msg, 1);
127 		if (ret)
128 			return ret;
129 	}
130 
131 	return 0;
132 }
133 
dm_i2c_read(struct udevice * dev,uint offset,uint8_t * buffer,int len)134 int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len)
135 {
136 	struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
137 	struct udevice *bus = dev_get_parent(dev);
138 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
139 	struct i2c_msg msg[2], *ptr;
140 	uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
141 	int msg_count;
142 
143 	if (!ops->xfer)
144 		return -ENOSYS;
145 	if (chip->flags & DM_I2C_CHIP_RD_ADDRESS)
146 		return i2c_read_bytewise(dev, offset, buffer, len);
147 	ptr = msg;
148 	if (!i2c_setup_offset(chip, offset, offset_buf, ptr))
149 		ptr++;
150 
151 	if (len) {
152 		ptr->addr = msg->addr;
153 		ptr->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
154 		ptr->flags |= I2C_M_RD;
155 		ptr->len = len;
156 		ptr->buf = buffer;
157 		ptr++;
158 	}
159 	msg_count = ptr - msg;
160 
161 	return ops->xfer(bus, msg, msg_count);
162 }
163 
dm_i2c_write(struct udevice * dev,uint offset,const uint8_t * buffer,int len)164 int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
165 		 int len)
166 {
167 	struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
168 	struct udevice *bus = dev_get_parent(dev);
169 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
170 	struct i2c_msg msg[1];
171 
172 	if (!ops->xfer)
173 		return -ENOSYS;
174 
175 	if (chip->flags & DM_I2C_CHIP_WR_ADDRESS)
176 		return i2c_write_bytewise(dev, offset, buffer, len);
177 	/*
178 	 * The simple approach would be to send two messages here: one to
179 	 * set the offset and one to write the bytes. However some drivers
180 	 * will not be expecting this, and some chips won't like how the
181 	 * driver presents this on the I2C bus.
182 	 *
183 	 * The API does not support separate offset and data. We could extend
184 	 * it with a flag indicating that there is data in the next message
185 	 * that needs to be processed in the same transaction. We could
186 	 * instead add an additional buffer to each message. For now, handle
187 	 * this in the uclass since it isn't clear what the impact on drivers
188 	 * would be with this extra complication. Unfortunately this means
189 	 * copying the message.
190 	 *
191 	 * Use the stack for small messages, malloc() for larger ones. We
192 	 * need to allow space for the offset (up to 4 bytes) and the message
193 	 * itself.
194 	 */
195 	if (len < 64) {
196 		uint8_t buf[I2C_MAX_OFFSET_LEN + len];
197 
198 		i2c_setup_offset(chip, offset, buf, msg);
199 		msg->len += len;
200 		memcpy(buf + chip->offset_len, buffer, len);
201 
202 		return ops->xfer(bus, msg, 1);
203 	} else {
204 		uint8_t *buf;
205 		int ret;
206 
207 		buf = malloc(I2C_MAX_OFFSET_LEN + len);
208 		if (!buf)
209 			return -ENOMEM;
210 		i2c_setup_offset(chip, offset, buf, msg);
211 		msg->len += len;
212 		memcpy(buf + chip->offset_len, buffer, len);
213 
214 		ret = ops->xfer(bus, msg, 1);
215 		free(buf);
216 		return ret;
217 	}
218 }
219 
dm_i2c_xfer(struct udevice * dev,struct i2c_msg * msg,int nmsgs)220 int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
221 {
222 	struct udevice *bus = dev_get_parent(dev);
223 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
224 
225 	if (!ops->xfer)
226 		return -ENOSYS;
227 
228 	return ops->xfer(bus, msg, nmsgs);
229 }
230 
dm_i2c_reg_read(struct udevice * dev,uint offset)231 int dm_i2c_reg_read(struct udevice *dev, uint offset)
232 {
233 	uint8_t val;
234 	int ret;
235 
236 	ret = dm_i2c_read(dev, offset, &val, 1);
237 	if (ret < 0)
238 		return ret;
239 
240 	return val;
241 }
242 
dm_i2c_reg_write(struct udevice * dev,uint offset,uint value)243 int dm_i2c_reg_write(struct udevice *dev, uint offset, uint value)
244 {
245 	uint8_t val = value;
246 
247 	return dm_i2c_write(dev, offset, &val, 1);
248 }
249 
dm_i2c_reg_clrset(struct udevice * dev,uint offset,u32 clr,u32 set)250 int dm_i2c_reg_clrset(struct udevice *dev, uint offset, u32 clr, u32 set)
251 {
252 	uint8_t val;
253 	int ret;
254 
255 	ret = dm_i2c_read(dev, offset, &val, 1);
256 	if (ret < 0)
257 		return ret;
258 
259 	val &= ~clr;
260 	val |= set;
261 
262 	return dm_i2c_write(dev, offset, &val, 1);
263 }
264 
265 /**
266  * i2c_probe_chip() - probe for a chip on a bus
267  *
268  * @bus:	Bus to probe
269  * @chip_addr:	Chip address to probe
270  * @flags:	Flags for the chip
271  * @return 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip
272  * does not respond to probe
273  */
i2c_probe_chip(struct udevice * bus,uint chip_addr,enum dm_i2c_chip_flags chip_flags)274 static int i2c_probe_chip(struct udevice *bus, uint chip_addr,
275 			  enum dm_i2c_chip_flags chip_flags)
276 {
277 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
278 	struct i2c_msg msg[1];
279 	int ret;
280 
281 	if (ops->probe_chip) {
282 		ret = ops->probe_chip(bus, chip_addr, chip_flags);
283 		if (!ret || ret != -ENOSYS)
284 			return ret;
285 	}
286 
287 	if (!ops->xfer)
288 		return -ENOSYS;
289 
290 	/* Probe with a zero-length message */
291 	msg->addr = chip_addr;
292 	msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
293 	msg->len = 0;
294 	msg->buf = NULL;
295 
296 	return ops->xfer(bus, msg, 1);
297 }
298 
i2c_bind_driver(struct udevice * bus,uint chip_addr,uint offset_len,struct udevice ** devp)299 static int i2c_bind_driver(struct udevice *bus, uint chip_addr, uint offset_len,
300 			   struct udevice **devp)
301 {
302 	struct dm_i2c_chip *chip;
303 	char name[30], *str;
304 	struct udevice *dev;
305 	int ret;
306 
307 	snprintf(name, sizeof(name), "generic_%x", chip_addr);
308 	str = strdup(name);
309 	if (!str)
310 		return -ENOMEM;
311 	ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev);
312 	debug("%s:  device_bind_driver: ret=%d\n", __func__, ret);
313 	if (ret)
314 		goto err_bind;
315 
316 	/* Tell the device what we know about it */
317 	chip = dev_get_parent_plat(dev);
318 	chip->chip_addr = chip_addr;
319 	chip->offset_len = offset_len;
320 	ret = device_probe(dev);
321 	debug("%s:  device_probe: ret=%d\n", __func__, ret);
322 	if (ret)
323 		goto err_probe;
324 
325 	*devp = dev;
326 	return 0;
327 
328 err_probe:
329 	/*
330 	 * If the device failed to probe, unbind it. There is nothing there
331 	 * on the bus so we don't want to leave it lying around
332 	 */
333 	device_unbind(dev);
334 err_bind:
335 	free(str);
336 	return ret;
337 }
338 
i2c_get_chip(struct udevice * bus,uint chip_addr,uint offset_len,struct udevice ** devp)339 int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
340 		 struct udevice **devp)
341 {
342 	struct udevice *dev;
343 
344 	debug("%s: Searching bus '%s' for address %02x: ", __func__,
345 	      bus->name, chip_addr);
346 	for (device_find_first_child(bus, &dev); dev;
347 			device_find_next_child(&dev)) {
348 		struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
349 		int ret;
350 
351 		if (chip->chip_addr == (chip_addr &
352 					~chip->chip_addr_offset_mask)) {
353 			ret = device_probe(dev);
354 			debug("found, ret=%d\n", ret);
355 			if (ret)
356 				return ret;
357 			*devp = dev;
358 			return 0;
359 		}
360 	}
361 	debug("not found\n");
362 	return i2c_bind_driver(bus, chip_addr, offset_len, devp);
363 }
364 
i2c_get_chip_for_busnum(int busnum,int chip_addr,uint offset_len,struct udevice ** devp)365 int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
366 			    struct udevice **devp)
367 {
368 	struct udevice *bus;
369 	int ret;
370 
371 	ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
372 	if (ret) {
373 		debug("Cannot find I2C bus %d\n", busnum);
374 		return ret;
375 	}
376 
377 	/* detect the presence of the chip on the bus */
378 	ret = i2c_probe_chip(bus, chip_addr, 0);
379 	debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
380 	      chip_addr, ret);
381 	if (ret) {
382 		debug("Cannot detect I2C chip %02x on bus %d\n", chip_addr,
383 		      busnum);
384 		return ret;
385 	}
386 
387 	ret = i2c_get_chip(bus, chip_addr, offset_len, devp);
388 	if (ret) {
389 		debug("Cannot find I2C chip %02x on bus %d\n", chip_addr,
390 		      busnum);
391 		return ret;
392 	}
393 
394 	return 0;
395 }
396 
dm_i2c_probe(struct udevice * bus,uint chip_addr,uint chip_flags,struct udevice ** devp)397 int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
398 		 struct udevice **devp)
399 {
400 	int ret;
401 
402 	*devp = NULL;
403 
404 	/* First probe that chip */
405 	ret = i2c_probe_chip(bus, chip_addr, chip_flags);
406 	debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
407 	      chip_addr, ret);
408 	if (ret)
409 		return ret;
410 
411 	/* The chip was found, see if we have a driver, and probe it */
412 	ret = i2c_get_chip(bus, chip_addr, 1, devp);
413 	debug("%s:  i2c_get_chip: ret=%d\n", __func__, ret);
414 
415 	return ret;
416 }
417 
dm_i2c_set_bus_speed(struct udevice * bus,unsigned int speed)418 int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
419 {
420 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
421 	struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
422 	int ret;
423 
424 	/*
425 	 * If we have a method, call it. If not then the driver probably wants
426 	 * to deal with speed changes on the next transfer. It can easily read
427 	 * the current speed from this uclass
428 	 */
429 	if (ops->set_bus_speed) {
430 		ret = ops->set_bus_speed(bus, speed);
431 		if (ret)
432 			return ret;
433 	}
434 	i2c->speed_hz = speed;
435 
436 	return 0;
437 }
438 
dm_i2c_get_bus_speed(struct udevice * bus)439 int dm_i2c_get_bus_speed(struct udevice *bus)
440 {
441 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
442 	struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
443 
444 	if (!ops->get_bus_speed)
445 		return i2c->speed_hz;
446 
447 	return ops->get_bus_speed(bus);
448 }
449 
i2c_set_chip_flags(struct udevice * dev,uint flags)450 int i2c_set_chip_flags(struct udevice *dev, uint flags)
451 {
452 	struct udevice *bus = dev->parent;
453 	struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
454 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
455 	int ret;
456 
457 	if (ops->set_flags) {
458 		ret = ops->set_flags(dev, flags);
459 		if (ret)
460 			return ret;
461 	}
462 	chip->flags = flags;
463 
464 	return 0;
465 }
466 
i2c_get_chip_flags(struct udevice * dev,uint * flagsp)467 int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
468 {
469 	struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
470 
471 	*flagsp = chip->flags;
472 
473 	return 0;
474 }
475 
i2c_set_chip_offset_len(struct udevice * dev,uint offset_len)476 int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
477 {
478 	struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
479 
480 	if (offset_len > I2C_MAX_OFFSET_LEN)
481 		return log_ret(-EINVAL);
482 	chip->offset_len = offset_len;
483 
484 	return 0;
485 }
486 
i2c_get_chip_offset_len(struct udevice * dev)487 int i2c_get_chip_offset_len(struct udevice *dev)
488 {
489 	struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
490 
491 	return chip->offset_len;
492 }
493 
i2c_set_chip_addr_offset_mask(struct udevice * dev,uint mask)494 int i2c_set_chip_addr_offset_mask(struct udevice *dev, uint mask)
495 {
496 	struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
497 
498 	chip->chip_addr_offset_mask = mask;
499 
500 	return 0;
501 }
502 
i2c_get_chip_addr_offset_mask(struct udevice * dev)503 uint i2c_get_chip_addr_offset_mask(struct udevice *dev)
504 {
505 	struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
506 
507 	return chip->chip_addr_offset_mask;
508 }
509 
510 #if CONFIG_IS_ENABLED(DM_GPIO)
i2c_gpio_set_pin(struct gpio_desc * pin,int bit)511 static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit)
512 {
513 	if (bit)
514 		dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
515 	else
516 		dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT |
517 					   GPIOD_ACTIVE_LOW |
518 					   GPIOD_IS_OUT_ACTIVE);
519 }
520 
i2c_gpio_get_pin(struct gpio_desc * pin)521 static int i2c_gpio_get_pin(struct gpio_desc *pin)
522 {
523 	return dm_gpio_get_value(pin);
524 }
525 
i2c_deblock_gpio_loop(struct gpio_desc * sda_pin,struct gpio_desc * scl_pin,unsigned int scl_count,unsigned int start_count,unsigned int delay)526 int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin,
527 			  struct gpio_desc *scl_pin,
528 			  unsigned int scl_count,
529 			  unsigned int start_count,
530 			  unsigned int delay)
531 {
532 	int i, ret = -EREMOTEIO;
533 
534 	i2c_gpio_set_pin(sda_pin, 1);
535 	i2c_gpio_set_pin(scl_pin, 1);
536 	udelay(delay);
537 
538 	/*  Toggle SCL until slave release SDA */
539 	for (; scl_count; --scl_count) {
540 		i2c_gpio_set_pin(scl_pin, 1);
541 		udelay(delay);
542 		i2c_gpio_set_pin(scl_pin, 0);
543 		udelay(delay);
544 		if (i2c_gpio_get_pin(sda_pin)) {
545 			ret = 0;
546 			break;
547 		}
548 	}
549 
550 	if (!ret && start_count) {
551 		for (i = 0; i < start_count; i++) {
552 			/* Send start condition */
553 			udelay(delay);
554 			i2c_gpio_set_pin(sda_pin, 1);
555 			udelay(delay);
556 			i2c_gpio_set_pin(scl_pin, 1);
557 			udelay(delay);
558 			i2c_gpio_set_pin(sda_pin, 0);
559 			udelay(delay);
560 			i2c_gpio_set_pin(scl_pin, 0);
561 		}
562 	}
563 
564 	/* Then, send I2C stop */
565 	i2c_gpio_set_pin(sda_pin, 0);
566 	udelay(delay);
567 
568 	i2c_gpio_set_pin(scl_pin, 1);
569 	udelay(delay);
570 
571 	i2c_gpio_set_pin(sda_pin, 1);
572 	udelay(delay);
573 
574 	if (!i2c_gpio_get_pin(sda_pin) || !i2c_gpio_get_pin(scl_pin))
575 		ret = -EREMOTEIO;
576 
577 	return ret;
578 }
579 
i2c_deblock_gpio(struct udevice * bus)580 static int i2c_deblock_gpio(struct udevice *bus)
581 {
582 	struct gpio_desc gpios[PIN_COUNT];
583 	int ret, ret0;
584 
585 	ret = gpio_request_list_by_name(bus, "gpios", gpios,
586 					ARRAY_SIZE(gpios), GPIOD_IS_IN);
587 	if (ret != ARRAY_SIZE(gpios)) {
588 		debug("%s: I2C Node '%s' has no 'gpios' property %s\n",
589 		      __func__, dev_read_name(bus), bus->name);
590 		if (ret >= 0) {
591 			gpio_free_list(bus, gpios, ret);
592 			ret = -ENOENT;
593 		}
594 		goto out;
595 	}
596 
597 	ret = pinctrl_select_state(bus, "gpio");
598 	if (ret) {
599 		debug("%s: I2C Node '%s' has no 'gpio' pinctrl state. %s\n",
600 		      __func__, dev_read_name(bus), bus->name);
601 		goto out_no_pinctrl;
602 	}
603 
604 	ret0 = i2c_deblock_gpio_loop(&gpios[PIN_SDA], &gpios[PIN_SCL], 9, 0, 5);
605 
606 	ret = pinctrl_select_state(bus, "default");
607 	if (ret) {
608 		debug("%s: I2C Node '%s' has no 'default' pinctrl state. %s\n",
609 		      __func__, dev_read_name(bus), bus->name);
610 	}
611 
612 	ret = !ret ? ret0 : ret;
613 
614 out_no_pinctrl:
615 	gpio_free_list(bus, gpios, ARRAY_SIZE(gpios));
616 out:
617 	return ret;
618 }
619 #else
i2c_deblock_gpio(struct udevice * bus)620 static int i2c_deblock_gpio(struct udevice *bus)
621 {
622 	return -ENOSYS;
623 }
624 #endif /* DM_GPIO */
625 
i2c_deblock(struct udevice * bus)626 int i2c_deblock(struct udevice *bus)
627 {
628 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
629 
630 	if (!ops->deblock)
631 		return i2c_deblock_gpio(bus);
632 
633 	return ops->deblock(bus);
634 }
635 
636 #if CONFIG_IS_ENABLED(OF_REAL)
i2c_chip_of_to_plat(struct udevice * dev,struct dm_i2c_chip * chip)637 int i2c_chip_of_to_plat(struct udevice *dev, struct dm_i2c_chip *chip)
638 {
639 	int addr;
640 
641 	chip->offset_len = dev_read_u32_default(dev, "u-boot,i2c-offset-len",
642 						1);
643 	chip->flags = 0;
644 	addr = dev_read_u32_default(dev, "reg", -1);
645 	if (addr == -1) {
646 		debug("%s: I2C Node '%s' has no 'reg' property %s\n", __func__,
647 		      dev_read_name(dev), dev->name);
648 		return log_ret(-EINVAL);
649 	}
650 	chip->chip_addr = addr;
651 
652 	return 0;
653 }
654 #endif
655 
i2c_pre_probe(struct udevice * dev)656 static int i2c_pre_probe(struct udevice *dev)
657 {
658 #if CONFIG_IS_ENABLED(OF_REAL)
659 	struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
660 	unsigned int max = 0;
661 	ofnode node;
662 	int ret;
663 
664 	i2c->max_transaction_bytes = 0;
665 	dev_for_each_subnode(node, dev) {
666 		ret = ofnode_read_u32(node,
667 				      "u-boot,i2c-transaction-bytes",
668 				      &max);
669 		if (!ret && max > i2c->max_transaction_bytes)
670 			i2c->max_transaction_bytes = max;
671 	}
672 
673 	debug("%s: I2C bus: %s max transaction bytes: %d\n", __func__,
674 	      dev->name, i2c->max_transaction_bytes);
675 #endif
676 	return 0;
677 }
678 
i2c_post_probe(struct udevice * dev)679 static int i2c_post_probe(struct udevice *dev)
680 {
681 #if CONFIG_IS_ENABLED(OF_REAL)
682 	struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
683 
684 	i2c->speed_hz = dev_read_u32_default(dev, "clock-frequency",
685 					     I2C_SPEED_STANDARD_RATE);
686 
687 	return dm_i2c_set_bus_speed(dev, i2c->speed_hz);
688 #else
689 	return 0;
690 #endif
691 }
692 
i2c_child_post_bind(struct udevice * dev)693 static int i2c_child_post_bind(struct udevice *dev)
694 {
695 #if CONFIG_IS_ENABLED(OF_REAL)
696 	struct dm_i2c_chip *plat = dev_get_parent_plat(dev);
697 
698 	if (!dev_has_ofnode(dev))
699 		return 0;
700 	return i2c_chip_of_to_plat(dev, plat);
701 #else
702 	return 0;
703 #endif
704 }
705 
i2c_post_bind(struct udevice * dev)706 static int i2c_post_bind(struct udevice *dev)
707 {
708 	int ret = 0;
709 
710 	debug("%s: %s, seq=%d\n", __func__, dev->name, dev_seq(dev));
711 
712 #if CONFIG_IS_ENABLED(OF_REAL)
713 	ret = dm_scan_fdt_dev(dev);
714 #endif
715 	return ret;
716 }
717 
718 UCLASS_DRIVER(i2c) = {
719 	.id		= UCLASS_I2C,
720 	.name		= "i2c",
721 	.flags		= DM_UC_FLAG_SEQ_ALIAS,
722 	.post_bind	= i2c_post_bind,
723 	.pre_probe      = i2c_pre_probe,
724 	.post_probe	= i2c_post_probe,
725 	.per_device_auto	= sizeof(struct dm_i2c_bus),
726 	.per_child_plat_auto	= sizeof(struct dm_i2c_chip),
727 	.child_post_bind = i2c_child_post_bind,
728 };
729 
730 UCLASS_DRIVER(i2c_generic) = {
731 	.id		= UCLASS_I2C_GENERIC,
732 	.name		= "i2c_generic",
733 };
734 
735 static const struct udevice_id generic_chip_i2c_ids[] = {
736 	{ .compatible = "i2c-chip", .data = I2C_DEVICE_GENERIC },
737 #if CONFIG_IS_ENABLED(ACPIGEN)
738 	{ .compatible = "hid-over-i2c", .data = I2C_DEVICE_HID_OVER_I2C },
739 #endif
740 	{ }
741 };
742 
743 U_BOOT_DRIVER(i2c_generic_chip_drv) = {
744 	.name		= "i2c_generic_chip_drv",
745 	.id		= UCLASS_I2C_GENERIC,
746 	.of_match	= generic_chip_i2c_ids,
747 #if CONFIG_IS_ENABLED(ACPIGEN)
748 	.of_to_plat	= acpi_i2c_of_to_plat,
749 	.priv_auto	= sizeof(struct acpi_i2c_priv),
750 #endif
751 	ACPI_OPS_PTR(&acpi_i2c_ops)
752 };
753