blob: 759f94e224e4e52f626b02cd49c3c56a4f0714e9 [file] [log] [blame]
Maxime Ripard73aea282018-09-18 10:35:25 +03001/* SPDX-License-Identifier: GPL-2.0+
2 *
3 * Copyright (c) 2015 Free Electrons
4 * Copyright (c) 2015 NextThing Co
5 *
6 * Maxime Ripard <maxime.ripard@free-electrons.com>
7 *
8 */
9
Maxime Ripard73aea282018-09-18 10:35:25 +030010#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Maxime Ripard73aea282018-09-18 10:35:25 +030012#include <w1.h>
Simon Glassc05ed002020-05-10 11:40:11 -060013#include <linux/delay.h>
Maxime Ripard73aea282018-09-18 10:35:25 +030014
15#include <asm/gpio.h>
16
17#define W1_TIMING_A 6
18#define W1_TIMING_B 64
19#define W1_TIMING_C 60
20#define W1_TIMING_D 10
21#define W1_TIMING_E 9
22#define W1_TIMING_F 55
23#define W1_TIMING_G 0
24#define W1_TIMING_H 480
25#define W1_TIMING_I 70
26#define W1_TIMING_J 410
27
28struct w1_gpio_pdata {
29 struct gpio_desc gpio;
30 u64 search_id;
31};
32
33static bool w1_gpio_read_bit(struct udevice *dev)
34{
Simon Glassc69cda22020-12-03 16:55:20 -070035 struct w1_gpio_pdata *pdata = dev_get_plat(dev);
Maxime Ripard73aea282018-09-18 10:35:25 +030036 int val;
37
38 dm_gpio_set_dir_flags(&pdata->gpio, GPIOD_IS_OUT);
39 udelay(W1_TIMING_A);
40
41 dm_gpio_set_dir_flags(&pdata->gpio, GPIOD_IS_IN);
42 udelay(W1_TIMING_E);
43
44 val = dm_gpio_get_value(&pdata->gpio);
45 if (val < 0)
46 debug("error in retrieving GPIO value");
47 udelay(W1_TIMING_F);
48
49 return val;
50}
51
52static u8 w1_gpio_read_byte(struct udevice *dev)
53{
54 int i;
55 u8 ret = 0;
56
57 for (i = 0; i < 8; ++i)
58 ret |= (w1_gpio_read_bit(dev) ? 1 : 0) << i;
59
60 return ret;
61}
62
63static void w1_gpio_write_bit(struct udevice *dev, bool bit)
64{
Simon Glassc69cda22020-12-03 16:55:20 -070065 struct w1_gpio_pdata *pdata = dev_get_plat(dev);
Maxime Ripard73aea282018-09-18 10:35:25 +030066
67 dm_gpio_set_dir_flags(&pdata->gpio, GPIOD_IS_OUT);
68
69 bit ? udelay(W1_TIMING_A) : udelay(W1_TIMING_C);
70
71 dm_gpio_set_value(&pdata->gpio, 1);
72
73 bit ? udelay(W1_TIMING_B) : udelay(W1_TIMING_D);
74}
75
76static void w1_gpio_write_byte(struct udevice *dev, u8 byte)
77{
78 int i;
79
80 for (i = 0; i < 8; ++i)
81 w1_gpio_write_bit(dev, (byte >> i) & 0x1);
82}
83
84static bool w1_gpio_reset(struct udevice *dev)
85{
Simon Glassc69cda22020-12-03 16:55:20 -070086 struct w1_gpio_pdata *pdata = dev_get_plat(dev);
Maxime Ripard73aea282018-09-18 10:35:25 +030087 int val;
88
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);
91 udelay(W1_TIMING_G);
92
93 dm_gpio_set_value(&pdata->gpio, 0);
94 /* wait for the specified time with the bus kept low */
95 udelay(W1_TIMING_H);
96
97 /* now we must read the presence pulse */
98 dm_gpio_set_dir_flags(&pdata->gpio, GPIOD_IS_IN);
99 udelay(W1_TIMING_I);
100
101 val = dm_gpio_get_value(&pdata->gpio);
102 if (val < 0)
103 debug("error in retrieving GPIO value");
104
105 /* if nobody pulled the bus down , it means nobody is on the bus */
106 if (val != 0)
107 return 1;
108 /* we have the bus pulled down, let's wait for the specified presence time */
109 udelay(W1_TIMING_J);
110
111 /* read again, the other end should leave the bus free */
112 val = dm_gpio_get_value(&pdata->gpio);
113 if (val < 0)
114 debug("error in retrieving GPIO value");
115
116 /* bus is not going up again, so we have an error */
117 if (val != 1)
118 return 1;
119
120 /* all good, presence detected */
121 return 0;
122}
123
124static u8 w1_gpio_triplet(struct udevice *dev, bool bdir)
125{
126 u8 id_bit = w1_gpio_read_bit(dev);
127 u8 comp_bit = w1_gpio_read_bit(dev);
128 u8 retval;
129
130 if (id_bit && comp_bit)
131 return 0x03; /* error */
132
133 if (!id_bit && !comp_bit) {
134 /* Both bits are valid, take the direction given */
135 retval = bdir ? 0x04 : 0;
136 } else {
137 /* Only one bit is valid, take that direction */
138 bdir = id_bit;
139 retval = id_bit ? 0x05 : 0x02;
140 }
141
142 w1_gpio_write_bit(dev, bdir);
143 return retval;
144}
145
146static 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,
151};
152
Simon Glassd1998a92020-12-03 16:55:21 -0700153static int w1_gpio_of_to_plat(struct udevice *dev)
Maxime Ripard73aea282018-09-18 10:35:25 +0300154{
Simon Glassc69cda22020-12-03 16:55:20 -0700155 struct w1_gpio_pdata *pdata = dev_get_plat(dev);
Maxime Ripard73aea282018-09-18 10:35:25 +0300156 int ret;
157
Eugen Hristevdffea442021-06-23 16:11:58 +0300158 ret = gpio_request_by_name(dev, "gpios", 0, &pdata->gpio, GPIOD_IS_IN);
Maxime Ripard73aea282018-09-18 10:35:25 +0300159 if (ret < 0)
160 printf("Error claiming GPIO %d\n", ret);
161
162 return ret;
163};
164
165static const struct udevice_id w1_gpio_id[] = {
166 { "w1-gpio", 0 },
167 { },
168};
169
170U_BOOT_DRIVER(w1_gpio_drv) = {
171 .id = UCLASS_W1,
172 .name = "w1_gpio_drv",
173 .of_match = w1_gpio_id,
Simon Glassd1998a92020-12-03 16:55:21 -0700174 .of_to_plat = w1_gpio_of_to_plat,
Maxime Ripard73aea282018-09-18 10:35:25 +0300175 .ops = &w1_gpio_ops,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700176 .plat_auto = sizeof(struct w1_gpio_pdata),
Maxime Ripard73aea282018-09-18 10:35:25 +0300177};