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>
17 #define W1_TIMING_B 64
18 #define W1_TIMING_C 60
19 #define W1_TIMING_D 10
21 #define W1_TIMING_F 55
23 #define W1_TIMING_H 480
24 #define W1_TIMING_I 70
25 #define W1_TIMING_J 410
27 struct w1_gpio_pdata {
28 struct gpio_desc gpio;
32 static bool w1_gpio_read_bit(struct udevice *dev)
34 struct w1_gpio_pdata *pdata = dev_get_platdata(dev);
37 dm_gpio_set_dir_flags(&pdata->gpio, GPIOD_IS_OUT);
40 dm_gpio_set_dir_flags(&pdata->gpio, GPIOD_IS_IN);
43 val = dm_gpio_get_value(&pdata->gpio);
45 debug("error in retrieving GPIO value");
51 static u8 w1_gpio_read_byte(struct udevice *dev)
56 for (i = 0; i < 8; ++i)
57 ret |= (w1_gpio_read_bit(dev) ? 1 : 0) << i;
62 static void w1_gpio_write_bit(struct udevice *dev, bool bit)
64 struct w1_gpio_pdata *pdata = dev_get_platdata(dev);
66 dm_gpio_set_dir_flags(&pdata->gpio, GPIOD_IS_OUT);
68 bit ? udelay(W1_TIMING_A) : udelay(W1_TIMING_C);
70 dm_gpio_set_value(&pdata->gpio, 1);
72 bit ? udelay(W1_TIMING_B) : udelay(W1_TIMING_D);
75 static void w1_gpio_write_byte(struct udevice *dev, u8 byte)
79 for (i = 0; i < 8; ++i)
80 w1_gpio_write_bit(dev, (byte >> i) & 0x1);
83 static bool w1_gpio_reset(struct udevice *dev)
85 struct w1_gpio_pdata *pdata = dev_get_platdata(dev);
88 /* initiate the reset pulse. first we must pull the bus to low */
89 dm_gpio_set_dir_flags(&pdata->gpio, GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
92 dm_gpio_set_value(&pdata->gpio, 0);
93 /* wait for the specified time with the bus kept low */
96 /* now we must read the presence pulse */
97 dm_gpio_set_dir_flags(&pdata->gpio, GPIOD_IS_IN);
100 val = dm_gpio_get_value(&pdata->gpio);
102 debug("error in retrieving GPIO value");
104 /* if nobody pulled the bus down , it means nobody is on the bus */
107 /* we have the bus pulled down, let's wait for the specified presence time */
110 /* read again, the other end should leave the bus free */
111 val = dm_gpio_get_value(&pdata->gpio);
113 debug("error in retrieving GPIO value");
115 /* bus is not going up again, so we have an error */
119 /* all good, presence detected */
123 static u8 w1_gpio_triplet(struct udevice *dev, bool bdir)
125 u8 id_bit = w1_gpio_read_bit(dev);
126 u8 comp_bit = w1_gpio_read_bit(dev);
129 if (id_bit && comp_bit)
130 return 0x03; /* error */
132 if (!id_bit && !comp_bit) {
133 /* Both bits are valid, take the direction given */
134 retval = bdir ? 0x04 : 0;
136 /* Only one bit is valid, take that direction */
138 retval = id_bit ? 0x05 : 0x02;
141 w1_gpio_write_bit(dev, bdir);
145 static const struct w1_ops w1_gpio_ops = {
146 .read_byte = w1_gpio_read_byte,
147 .reset = w1_gpio_reset,
148 .triplet = w1_gpio_triplet,
149 .write_byte = w1_gpio_write_byte,
152 static int w1_gpio_ofdata_to_platdata(struct udevice *dev)
154 struct w1_gpio_pdata *pdata = dev_get_platdata(dev);
157 ret = gpio_request_by_name(dev, "gpios", 0, &pdata->gpio, 0);
159 printf("Error claiming GPIO %d\n", ret);
164 static const struct udevice_id w1_gpio_id[] = {
169 U_BOOT_DRIVER(w1_gpio_drv) = {
171 .name = "w1_gpio_drv",
172 .of_match = w1_gpio_id,
173 .ofdata_to_platdata = w1_gpio_ofdata_to_platdata,
175 .platdata_auto_alloc_size = sizeof(struct w1_gpio_pdata),