1 /* SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Free Electrons
4 * Copyright (c) 2015 NextThing Co
14 #include <linux/delay.h>
19 #define W1_TIMING_B 64
20 #define W1_TIMING_C 60
21 #define W1_TIMING_D 10
23 #define W1_TIMING_F 55
25 #define W1_TIMING_H 480
26 #define W1_TIMING_I 70
27 #define W1_TIMING_J 410
29 struct w1_gpio_pdata {
30 struct gpio_desc gpio;
34 static bool w1_gpio_read_bit(struct udevice *dev)
36 struct w1_gpio_pdata *pdata = dev_get_platdata(dev);
39 dm_gpio_set_dir_flags(&pdata->gpio, GPIOD_IS_OUT);
42 dm_gpio_set_dir_flags(&pdata->gpio, GPIOD_IS_IN);
45 val = dm_gpio_get_value(&pdata->gpio);
47 debug("error in retrieving GPIO value");
53 static u8 w1_gpio_read_byte(struct udevice *dev)
58 for (i = 0; i < 8; ++i)
59 ret |= (w1_gpio_read_bit(dev) ? 1 : 0) << i;
64 static void w1_gpio_write_bit(struct udevice *dev, bool bit)
66 struct w1_gpio_pdata *pdata = dev_get_platdata(dev);
68 dm_gpio_set_dir_flags(&pdata->gpio, GPIOD_IS_OUT);
70 bit ? udelay(W1_TIMING_A) : udelay(W1_TIMING_C);
72 dm_gpio_set_value(&pdata->gpio, 1);
74 bit ? udelay(W1_TIMING_B) : udelay(W1_TIMING_D);
77 static void w1_gpio_write_byte(struct udevice *dev, u8 byte)
81 for (i = 0; i < 8; ++i)
82 w1_gpio_write_bit(dev, (byte >> i) & 0x1);
85 static bool w1_gpio_reset(struct udevice *dev)
87 struct w1_gpio_pdata *pdata = dev_get_platdata(dev);
90 /* initiate the reset pulse. first we must pull the bus to low */
91 dm_gpio_set_dir_flags(&pdata->gpio, GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
94 dm_gpio_set_value(&pdata->gpio, 0);
95 /* wait for the specified time with the bus kept low */
98 /* now we must read the presence pulse */
99 dm_gpio_set_dir_flags(&pdata->gpio, GPIOD_IS_IN);
102 val = dm_gpio_get_value(&pdata->gpio);
104 debug("error in retrieving GPIO value");
106 /* if nobody pulled the bus down , it means nobody is on the bus */
109 /* we have the bus pulled down, let's wait for the specified presence time */
112 /* read again, the other end should leave the bus free */
113 val = dm_gpio_get_value(&pdata->gpio);
115 debug("error in retrieving GPIO value");
117 /* bus is not going up again, so we have an error */
121 /* all good, presence detected */
125 static u8 w1_gpio_triplet(struct udevice *dev, bool bdir)
127 u8 id_bit = w1_gpio_read_bit(dev);
128 u8 comp_bit = w1_gpio_read_bit(dev);
131 if (id_bit && comp_bit)
132 return 0x03; /* error */
134 if (!id_bit && !comp_bit) {
135 /* Both bits are valid, take the direction given */
136 retval = bdir ? 0x04 : 0;
138 /* Only one bit is valid, take that direction */
140 retval = id_bit ? 0x05 : 0x02;
143 w1_gpio_write_bit(dev, bdir);
147 static const struct w1_ops w1_gpio_ops = {
148 .read_byte = w1_gpio_read_byte,
149 .reset = w1_gpio_reset,
150 .triplet = w1_gpio_triplet,
151 .write_byte = w1_gpio_write_byte,
154 static int w1_gpio_ofdata_to_platdata(struct udevice *dev)
156 struct w1_gpio_pdata *pdata = dev_get_platdata(dev);
159 ret = gpio_request_by_name(dev, "gpios", 0, &pdata->gpio, 0);
161 printf("Error claiming GPIO %d\n", ret);
166 static const struct udevice_id w1_gpio_id[] = {
171 U_BOOT_DRIVER(w1_gpio_drv) = {
173 .name = "w1_gpio_drv",
174 .of_match = w1_gpio_id,
175 .ofdata_to_platdata = w1_gpio_ofdata_to_platdata,
177 .platdata_auto_alloc_size = sizeof(struct w1_gpio_pdata),