1 /* SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Free Electrons
4 * Copyright (c) 2015 NextThing Co
6 * Maxime Ripard <maxime.ripard@free-electrons.com>
18 #define W1_TIMING_B 64
19 #define W1_TIMING_C 60
20 #define W1_TIMING_D 10
22 #define W1_TIMING_F 55
24 #define W1_TIMING_H 480
25 #define W1_TIMING_I 70
26 #define W1_TIMING_J 410
28 struct w1_gpio_pdata {
29 struct gpio_desc gpio;
33 static bool w1_gpio_read_bit(struct udevice *dev)
35 struct w1_gpio_pdata *pdata = dev_get_platdata(dev);
38 dm_gpio_set_dir_flags(&pdata->gpio, GPIOD_IS_OUT);
41 dm_gpio_set_dir_flags(&pdata->gpio, GPIOD_IS_IN);
44 val = dm_gpio_get_value(&pdata->gpio);
46 debug("error in retrieving GPIO value");
52 static u8 w1_gpio_read_byte(struct udevice *dev)
57 for (i = 0; i < 8; ++i)
58 ret |= (w1_gpio_read_bit(dev) ? 1 : 0) << i;
63 static void w1_gpio_write_bit(struct udevice *dev, bool bit)
65 struct w1_gpio_pdata *pdata = dev_get_platdata(dev);
67 dm_gpio_set_dir_flags(&pdata->gpio, GPIOD_IS_OUT);
69 bit ? udelay(W1_TIMING_A) : udelay(W1_TIMING_C);
71 dm_gpio_set_value(&pdata->gpio, 1);
73 bit ? udelay(W1_TIMING_B) : udelay(W1_TIMING_D);
76 static void w1_gpio_write_byte(struct udevice *dev, u8 byte)
80 for (i = 0; i < 8; ++i)
81 w1_gpio_write_bit(dev, (byte >> i) & 0x1);
84 static bool w1_gpio_reset(struct udevice *dev)
86 struct w1_gpio_pdata *pdata = dev_get_platdata(dev);
89 /* initiate the reset pulse. first we must pull the bus to low */
90 dm_gpio_set_dir_flags(&pdata->gpio, GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
93 dm_gpio_set_value(&pdata->gpio, 0);
94 /* wait for the specified time with the bus kept low */
97 /* now we must read the presence pulse */
98 dm_gpio_set_dir_flags(&pdata->gpio, GPIOD_IS_IN);
101 val = dm_gpio_get_value(&pdata->gpio);
103 debug("error in retrieving GPIO value");
105 /* if nobody pulled the bus down , it means nobody is on the bus */
108 /* we have the bus pulled down, let's wait for the specified presence time */
111 /* read again, the other end should leave the bus free */
112 val = dm_gpio_get_value(&pdata->gpio);
114 debug("error in retrieving GPIO value");
116 /* bus is not going up again, so we have an error */
120 /* all good, presence detected */
124 static u8 w1_gpio_triplet(struct udevice *dev, bool bdir)
126 u8 id_bit = w1_gpio_read_bit(dev);
127 u8 comp_bit = w1_gpio_read_bit(dev);
130 if (id_bit && comp_bit)
131 return 0x03; /* error */
133 if (!id_bit && !comp_bit) {
134 /* Both bits are valid, take the direction given */
135 retval = bdir ? 0x04 : 0;
137 /* Only one bit is valid, take that direction */
139 retval = id_bit ? 0x05 : 0x02;
142 w1_gpio_write_bit(dev, bdir);
146 static const struct w1_ops w1_gpio_ops = {
147 .read_byte = w1_gpio_read_byte,
148 .reset = w1_gpio_reset,
149 .triplet = w1_gpio_triplet,
150 .write_byte = w1_gpio_write_byte,
153 static int w1_gpio_ofdata_to_platdata(struct udevice *dev)
155 struct w1_gpio_pdata *pdata = dev_get_platdata(dev);
158 ret = gpio_request_by_name(dev, "gpios", 0, &pdata->gpio, 0);
160 printf("Error claiming GPIO %d\n", ret);
165 static const struct udevice_id w1_gpio_id[] = {
170 U_BOOT_DRIVER(w1_gpio_drv) = {
172 .name = "w1_gpio_drv",
173 .of_match = w1_gpio_id,
174 .ofdata_to_platdata = w1_gpio_ofdata_to_platdata,
176 .platdata_auto_alloc_size = sizeof(struct w1_gpio_pdata),