1 /* 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 
10 #include <dm.h>
11 #include <log.h>
12 #include <w1.h>
13 #include <linux/delay.h>
14 
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 
28 struct w1_gpio_pdata {
29 	struct gpio_desc	gpio;
30 	u64			search_id;
31 };
32 
w1_gpio_read_bit(struct udevice * dev)33 static bool w1_gpio_read_bit(struct udevice *dev)
34 {
35 	struct w1_gpio_pdata *pdata = dev_get_plat(dev);
36 	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 
w1_gpio_read_byte(struct udevice * dev)52 static 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 
w1_gpio_write_bit(struct udevice * dev,bool bit)63 static void w1_gpio_write_bit(struct udevice *dev, bool bit)
64 {
65 	struct w1_gpio_pdata *pdata = dev_get_plat(dev);
66 
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 
w1_gpio_write_byte(struct udevice * dev,u8 byte)76 static 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 
w1_gpio_reset(struct udevice * dev)84 static bool w1_gpio_reset(struct udevice *dev)
85 {
86 	struct w1_gpio_pdata *pdata = dev_get_plat(dev);
87 	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 
w1_gpio_triplet(struct udevice * dev,bool bdir)124 static 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 
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,
151 };
152 
w1_gpio_of_to_plat(struct udevice * dev)153 static int w1_gpio_of_to_plat(struct udevice *dev)
154 {
155 	struct w1_gpio_pdata *pdata = dev_get_plat(dev);
156 	int ret;
157 
158 	ret = gpio_request_by_name(dev, "gpios", 0, &pdata->gpio, GPIOD_IS_IN);
159 	if (ret < 0)
160 		printf("Error claiming GPIO %d\n", ret);
161 
162 	return ret;
163 };
164 
165 static const struct udevice_id w1_gpio_id[] = {
166 	{ "w1-gpio", 0 },
167 	{ },
168 };
169 
170 U_BOOT_DRIVER(w1_gpio_drv) = {
171 	.id				= UCLASS_W1,
172 	.name				= "w1_gpio_drv",
173 	.of_match			= w1_gpio_id,
174 	.of_to_plat		= w1_gpio_of_to_plat,
175 	.ops				= &w1_gpio_ops,
176 	.plat_auto	= sizeof(struct w1_gpio_pdata),
177 };
178