1 // SPDX-License-Identifier: GPL-2.0+
4 * Copyright (c) 2015 Free Electrons
5 * Copyright (c) 2015 NextThing Co.
6 * Copyright (c) 2018 Microchip Technology, Inc.
7 * Copyright (c) 2021 Bootlin
9 * Maxime Ripard <maxime.ripard@free-electrons.com>
10 * Eugen Hristev <eugen.hristev@microchip.com>
11 * Kory Maincent <kory.maincent@bootlin.com>
15 #define LOG_CATEGORY UCLASS_W1
21 #include <w1-eeprom.h>
23 #include <dm/device-internal.h>
25 #define W1_MATCH_ROM 0x55
26 #define W1_SKIP_ROM 0xcc
27 #define W1_SEARCH 0xf0
33 int w1_bus_find_dev(const struct udevice *bus, u64 id, struct udevice
37 u8 family = id & 0xff;
40 for (ret = uclass_first_device(UCLASS_W1_EEPROM, &dev);
42 uclass_next_device(&dev)) {
44 debug("cannot find w1 eeprom dev\n");
48 if (dev_get_driver_data(dev) == family) {
57 int w1_register_new_device(u64 id, struct udevice *bus)
59 u8 family = id & 0xff;
63 struct w1_driver_entry *start, *entry;
65 start = ll_entry_start(struct w1_driver_entry, w1_driver_entry);
66 n_ents = ll_entry_count(struct w1_driver_entry, w1_driver_entry);
68 for (entry = start; entry != start + n_ents; entry++) {
69 const u8 *match_family;
70 const struct driver *drv;
73 for (match_family = entry->family; match_family;
75 if (*match_family != family)
78 ret = w1_bus_find_dev(bus, id, &dev);
80 /* If nothing in the device tree, bind a device */
83 ret = device_bind(bus, drv, drv->name,
84 NULL, ofnode_null(), &dev);
91 w1 = dev_get_parent_plat(dev);
98 debug("%s: No matches found: error %d\n", __func__, ret);
103 static int w1_enumerate(struct udevice *bus)
105 const struct w1_ops *ops = device_get_ops(bus);
106 struct w1_bus *w1 = dev_get_uclass_priv(bus);
107 u64 last_rn, rn = w1->search_id, tmp64;
108 bool last_device = false;
109 int search_bit, desc_bit = 64;
114 if (!ops->reset || !ops->write_byte || !ops->triplet)
117 while (!last_device) {
122 * Reset bus and all 1-wire device state machines
123 * so they can respond to our requests.
125 * Return 0 - device(s) present, 1 - no devices present.
127 if (ops->reset(bus)) {
128 debug("%s: No devices present on the wire.\n",
133 /* Start the search */
134 ops->write_byte(bus, W1_SEARCH);
135 for (i = 0; i < 64; ++i) {
136 /* Determine the direction/search bit */
138 /* took the 0 path last time, so take the 1 path */
140 else if (i > desc_bit)
141 /* take the 0 path on the next branch */
144 search_bit = ((last_rn >> i) & 0x1);
146 /* Read two bits and write one bit */
147 triplet_ret = ops->triplet(bus, search_bit);
149 /* quit if no device responded */
150 if ((triplet_ret & 0x03) == 0x03)
153 /* If both directions were valid, and we took the 0 path... */
154 if (triplet_ret == 0)
157 /* extract the direction taken & update the device number */
158 tmp64 = (triplet_ret >> 2);
162 if ((triplet_ret & 0x03) != 0x03) {
163 if (desc_bit == last_zero || last_zero < 0) {
169 desc_bit = last_zero;
171 debug("%s: Detected new device 0x%llx (family 0x%x)\n",
172 bus->name, rn, (u8)(rn & 0xff));
174 /* attempt to register as w1 device */
175 w1_register_new_device(rn, bus);
182 int w1_get_bus(int busnum, struct udevice **busp)
188 for (ret = uclass_first_device(UCLASS_W1, &dev);
190 ret = uclass_next_device(&dev), i++) {
198 debug("Cannot find w1 bus %d\n", busnum);
205 u8 w1_get_device_family(struct udevice *dev)
207 struct w1_device *w1 = dev_get_parent_plat(dev);
209 return w1->id & 0xff;
212 int w1_reset_select(struct udevice *dev)
214 struct w1_device *w1 = dev_get_parent_plat(dev);
215 struct udevice *bus = dev_get_parent(dev);
216 const struct w1_ops *ops = device_get_ops(bus);
219 if (!ops->reset || !ops->write_byte)
224 ops->write_byte(bus, W1_MATCH_ROM);
226 for (i = 0; i < sizeof(w1->id); i++)
227 ops->write_byte(bus, (w1->id >> (i * 8)) & 0xff);
232 int w1_read_byte(struct udevice *dev)
234 struct udevice *bus = dev_get_parent(dev);
235 const struct w1_ops *ops = device_get_ops(bus);
240 return ops->read_byte(bus);
243 int w1_read_buf(struct udevice *dev, u8 *buf, unsigned int count)
247 for (i = 0; i < count; i++) {
248 ret = w1_read_byte(dev);
258 int w1_write_byte(struct udevice *dev, u8 byte)
260 struct udevice *bus = dev_get_parent(dev);
261 const struct w1_ops *ops = device_get_ops(bus);
263 if (!ops->write_byte)
266 ops->write_byte(bus, byte);
271 static int w1_post_probe(struct udevice *bus)
284 ret = uclass_get(UCLASS_W1, &uc);
288 uclass_foreach_dev(bus, uc) {
289 ret = device_probe(bus);
290 if (ret == -ENODEV) { /* No such device. */
291 printf("W1 controller not available.\n");
295 if (ret) { /* Other error. */
296 printf("W1 controller probe failed.\n");
303 UCLASS_DRIVER(w1) = {
306 .flags = DM_UC_FLAG_SEQ_ALIAS,
307 .per_device_auto = sizeof(struct w1_bus),
308 .post_probe = w1_post_probe,
309 #if CONFIG_IS_ENABLED(OF_CONTROL)
310 .post_bind = dm_scan_fdt_dev,
312 .per_child_plat_auto = sizeof(struct w1_device),