1 // SPDX-License-Identifier:	GPL-2.0+
2 /*
3  *
4  * Copyright (c) 2015 Free Electrons
5  * Copyright (c) 2015 NextThing Co.
6  * Copyright (c) 2018 Microchip Technology, Inc.
7  *
8  * Maxime Ripard <maxime.ripard@free-electrons.com>
9  * Eugen Hristev <eugen.hristev@microchip.com>
10  *
11  */
12 
13 #include <common.h>
14 #include <dm.h>
15 #include <log.h>
16 #include <w1.h>
17 #include <w1-eeprom.h>
18 
19 #include <dm/device-internal.h>
20 
w1_eeprom_read_buf(struct udevice * dev,unsigned int offset,u8 * buf,unsigned int count)21 int w1_eeprom_read_buf(struct udevice *dev, unsigned int offset,
22 		       u8 *buf, unsigned int count)
23 {
24 	const struct w1_eeprom_ops *ops = device_get_ops(dev);
25 	u64 id = 0;
26 	int ret;
27 
28 	if (!ops->read_buf)
29 		return -ENOSYS;
30 
31 	ret = w1_eeprom_get_id(dev, &id);
32 	if (ret)
33 		return ret;
34 	if (!id)
35 		return -ENODEV;
36 
37 	return ops->read_buf(dev, offset, buf, count);
38 }
39 
w1_eeprom_register_new_device(u64 id)40 int w1_eeprom_register_new_device(u64 id)
41 {
42 	u8 family = id & 0xff;
43 	int ret;
44 	struct udevice *dev;
45 
46 	for (ret = uclass_first_device(UCLASS_W1_EEPROM, &dev);
47 	     !ret && dev;
48 	     uclass_next_device(&dev)) {
49 		if (ret || !dev) {
50 			debug("cannot find w1 eeprom dev\n");
51 			return ret;
52 		}
53 		if (dev_get_driver_data(dev) == family) {
54 			struct w1_device *w1;
55 
56 			w1 = dev_get_parent_plat(dev);
57 			if (w1->id) /* device already in use */
58 				continue;
59 			w1->id = id;
60 			debug("%s: Match found: %s:%s %llx\n", __func__,
61 			      dev->name, dev->driver->name, id);
62 			return 0;
63 		}
64 	}
65 
66 	debug("%s: No matches found: error %d\n", __func__, ret);
67 
68 	return ret;
69 }
70 
w1_eeprom_get_id(struct udevice * dev,u64 * id)71 int w1_eeprom_get_id(struct udevice *dev, u64 *id)
72 {
73 	struct w1_device *w1 = dev_get_parent_plat(dev);
74 
75 	if (!w1)
76 		return -ENODEV;
77 	*id = w1->id;
78 
79 	return 0;
80 }
81 
82 UCLASS_DRIVER(w1_eeprom) = {
83 	.name		= "w1_eeprom",
84 	.id		= UCLASS_W1_EEPROM,
85 	.flags		= DM_UC_FLAG_SEQ_ALIAS,
86 #if CONFIG_IS_ENABLED(OF_CONTROL)
87 	.post_bind	= dm_scan_fdt_dev,
88 #endif
89 };
90 
w1_eeprom_dm_init(void)91 int w1_eeprom_dm_init(void)
92 {
93 	struct udevice *dev;
94 	struct uclass *uc;
95 	int ret;
96 
97 	ret = uclass_get(UCLASS_W1_EEPROM, &uc);
98 	if (ret) {
99 		debug("W1_EEPROM uclass not available\n");
100 		return ret;
101 	}
102 
103 	uclass_foreach_dev(dev, uc) {
104 		ret = device_probe(dev);
105 		if (ret == -ENODEV) {	/* No such device. */
106 			debug("W1_EEPROM not available.\n");
107 			continue;
108 		}
109 
110 		if (ret) {		/* Other error. */
111 			printf("W1_EEPROM probe failed, error %d\n", ret);
112 			continue;
113 		}
114 	}
115 
116 	return 0;
117 }
118