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