1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * w1-uart - UART 1-Wire bus driver
5 * Uses the UART interface (via Serial Device Bus) to create the 1-Wire
6 * timing patterns. Implements the following 1-Wire master interface:
8 * - reset_bus: requests baud-rate 9600
10 * - touch_bit: requests baud-rate 115200
12 * Author: Christoph Winklhofer <cj.winklhofer@gmail.com>
15 #include <linux/completion.h>
16 #include <linux/delay.h>
17 #include <linux/jiffies.h>
18 #include <linux/module.h>
19 #include <linux/mutex.h>
21 #include <linux/serdev.h>
24 /* UART packet contains start and stop bit */
25 #define W1_UART_BITS_PER_PACKET (BITS_PER_BYTE + 2)
27 /* Timeout to wait for completion of serdev-receive */
28 #define W1_UART_TIMEOUT msecs_to_jiffies(500)
31 * struct w1_uart_config - configuration for 1-Wire operation
32 * @baudrate: baud-rate returned from serdev
33 * @delay_us: delay to complete a 1-Wire cycle (in us)
34 * @tx_byte: byte to generate 1-Wire timing pattern
36 struct w1_uart_config
{
37 unsigned int baudrate
;
38 unsigned int delay_us
;
43 * struct w1_uart_device - 1-Wire UART device structure
44 * @serdev: serial device
46 * @cfg_reset: config for 1-Wire reset
47 * @cfg_touch_0: config for 1-Wire write-0 cycle
48 * @cfg_touch_1: config for 1-Wire write-1 and read cycle
49 * @rx_byte_received: completion for serdev receive
50 * @rx_mutex: mutex to protect rx_err and rx_byte
51 * @rx_err: indicates an error in serdev-receive
52 * @rx_byte: result byte from serdev-receive
54 struct w1_uart_device
{
55 struct serdev_device
*serdev
;
56 struct w1_bus_master bus
;
58 struct w1_uart_config cfg_reset
;
59 struct w1_uart_config cfg_touch_0
;
60 struct w1_uart_config cfg_touch_1
;
62 struct completion rx_byte_received
;
64 * protect rx_err and rx_byte from concurrent access in
65 * w1-callbacks and serdev-receive.
67 struct mutex rx_mutex
;
73 * struct w1_uart_limits - limits for 1-Wire operations
74 * @baudrate: Requested baud-rate to create 1-Wire timing pattern
75 * @bit_min_us: minimum time for a bit (in us)
76 * @bit_max_us: maximum time for a bit (in us)
77 * @sample_us: timespan to sample 1-Wire response
78 * @cycle_us: duration of the 1-Wire cycle
80 struct w1_uart_limits
{
81 unsigned int baudrate
;
82 unsigned int bit_min_us
;
83 unsigned int bit_max_us
;
84 unsigned int sample_us
;
85 unsigned int cycle_us
;
88 static inline unsigned int baud_to_bit_ns(unsigned int baud
)
90 return NSEC_PER_SEC
/ baud
;
93 static inline unsigned int to_ns(unsigned int us
)
95 return us
* NSEC_PER_USEC
;
99 * Set baud-rate, delay and tx-byte to create a 1-Wire pulse and adapt
100 * the tx-byte according to the actual baud-rate.
103 * - time for a bit outside min/max range
104 * - a 1-Wire response is not detectable for sent byte
106 static int w1_uart_set_config(struct serdev_device
*serdev
,
107 const struct w1_uart_limits
*limits
,
108 struct w1_uart_config
*w1cfg
)
110 unsigned int packet_ns
;
111 unsigned int bits_low
;
115 w1cfg
->baudrate
= serdev_device_set_baudrate(serdev
, limits
->baudrate
);
116 if (w1cfg
->baudrate
== 0)
119 /* Compute in nanoseconds for accuracy */
120 bit_ns
= baud_to_bit_ns(w1cfg
->baudrate
);
121 bits_low
= to_ns(limits
->bit_min_us
) / bit_ns
;
122 /* start bit is always low */
123 low_ns
= bit_ns
* (bits_low
+ 1);
125 if (low_ns
< to_ns(limits
->bit_min_us
))
128 if (low_ns
> to_ns(limits
->bit_max_us
))
131 /* 1-Wire response detectable for sent byte */
132 if (limits
->sample_us
> 0 &&
133 bit_ns
* BITS_PER_BYTE
< low_ns
+ to_ns(limits
->sample_us
))
136 /* delay: 1-Wire cycle takes longer than the UART packet */
137 packet_ns
= bit_ns
* W1_UART_BITS_PER_PACKET
;
139 if (to_ns(limits
->cycle_us
) > packet_ns
)
141 (to_ns(limits
->cycle_us
) - packet_ns
) / NSEC_PER_USEC
;
143 /* byte to create 1-Wire pulse */
144 w1cfg
->tx_byte
= 0xff << bits_low
;
150 * Configuration for reset and presence detect
151 * - bit_min_us is 480us, add margin and use 485us
152 * - limits for sample time 60us-75us, use 65us
154 static int w1_uart_set_config_reset(struct w1_uart_device
*w1dev
)
156 struct serdev_device
*serdev
= w1dev
->serdev
;
157 struct device_node
*np
= serdev
->dev
.of_node
;
159 struct w1_uart_limits limits
= { .baudrate
= 9600,
165 of_property_read_u32(np
, "reset-bps", &limits
.baudrate
);
167 return w1_uart_set_config(serdev
, &limits
, &w1dev
->cfg_reset
);
171 * Configuration for write-0 cycle (touch bit 0)
172 * - bit_min_us is 60us, add margin and use 65us
173 * - no sampling required, sample_us = 0
175 static int w1_uart_set_config_touch_0(struct w1_uart_device
*w1dev
)
177 struct serdev_device
*serdev
= w1dev
->serdev
;
178 struct device_node
*np
= serdev
->dev
.of_node
;
180 struct w1_uart_limits limits
= { .baudrate
= 115200,
186 of_property_read_u32(np
, "write-0-bps", &limits
.baudrate
);
188 return w1_uart_set_config(serdev
, &limits
, &w1dev
->cfg_touch_0
);
192 * Configuration for write-1 and read cycle (touch bit 1)
193 * - bit_min_us is 5us, add margin and use 6us
194 * - limits for sample time 5us-15us, use 15us
196 static int w1_uart_set_config_touch_1(struct w1_uart_device
*w1dev
)
198 struct serdev_device
*serdev
= w1dev
->serdev
;
199 struct device_node
*np
= serdev
->dev
.of_node
;
201 struct w1_uart_limits limits
= { .baudrate
= 115200,
207 of_property_read_u32(np
, "write-1-bps", &limits
.baudrate
);
209 return w1_uart_set_config(serdev
, &limits
, &w1dev
->cfg_touch_1
);
213 * Configure and open the serial device
215 static int w1_uart_serdev_open(struct w1_uart_device
*w1dev
)
217 struct serdev_device
*serdev
= w1dev
->serdev
;
218 struct device
*dev
= &serdev
->dev
;
221 ret
= devm_serdev_device_open(dev
, serdev
);
225 ret
= serdev_device_set_parity(serdev
, SERDEV_PARITY_NONE
);
227 dev_err(dev
, "set parity failed\n");
231 ret
= w1_uart_set_config_reset(w1dev
);
233 dev_err(dev
, "config for reset failed\n");
237 ret
= w1_uart_set_config_touch_0(w1dev
);
239 dev_err(dev
, "config for touch-0 failed\n");
243 ret
= w1_uart_set_config_touch_1(w1dev
);
245 dev_err(dev
, "config for touch-1 failed\n");
249 serdev_device_set_flow_control(serdev
, false);
255 * Send one byte (tx_byte) and read one byte (rx_byte) via serdev.
257 static int w1_uart_serdev_tx_rx(struct w1_uart_device
*w1dev
,
258 const struct w1_uart_config
*w1cfg
, u8
*rx_byte
)
260 struct serdev_device
*serdev
= w1dev
->serdev
;
263 serdev_device_write_flush(serdev
);
264 serdev_device_set_baudrate(serdev
, w1cfg
->baudrate
);
266 /* write and immediately read one byte */
267 reinit_completion(&w1dev
->rx_byte_received
);
268 ret
= serdev_device_write_buf(serdev
, &w1cfg
->tx_byte
, 1);
271 ret
= wait_for_completion_interruptible_timeout(
272 &w1dev
->rx_byte_received
, W1_UART_TIMEOUT
);
276 /* locking could fail when serdev is unexpectedly receiving. */
277 if (!mutex_trylock(&w1dev
->rx_mutex
))
282 *rx_byte
= w1dev
->rx_byte
;
284 mutex_unlock(&w1dev
->rx_mutex
);
286 if (w1cfg
->delay_us
> 0)
287 fsleep(w1cfg
->delay_us
);
292 static size_t w1_uart_serdev_receive_buf(struct serdev_device
*serdev
,
293 const u8
*buf
, size_t count
)
295 struct w1_uart_device
*w1dev
= serdev_device_get_drvdata(serdev
);
297 mutex_lock(&w1dev
->rx_mutex
);
299 /* sent a single byte and receive one single byte */
301 w1dev
->rx_byte
= buf
[0];
304 w1dev
->rx_err
= -EIO
;
307 mutex_unlock(&w1dev
->rx_mutex
);
308 complete(&w1dev
->rx_byte_received
);
313 static const struct serdev_device_ops w1_uart_serdev_ops
= {
314 .receive_buf
= w1_uart_serdev_receive_buf
,
315 .write_wakeup
= serdev_device_write_wakeup
,
319 * 1-wire reset and presence detect: A present slave will manipulate
320 * the received byte by pulling the 1-Wire low.
322 static u8
w1_uart_reset_bus(void *data
)
324 struct w1_uart_device
*w1dev
= data
;
325 const struct w1_uart_config
*w1cfg
= &w1dev
->cfg_reset
;
329 ret
= w1_uart_serdev_tx_rx(w1dev
, w1cfg
, &val
);
333 /* Device present (0) or no device (1) */
334 return val
!= w1cfg
->tx_byte
? 0 : 1;
338 * 1-Wire read and write cycle: Only the read-0 manipulates the
339 * received byte, all others left the line untouched.
341 static u8
w1_uart_touch_bit(void *data
, u8 bit
)
343 struct w1_uart_device
*w1dev
= data
;
344 const struct w1_uart_config
*w1cfg
= bit
? &w1dev
->cfg_touch_1
:
349 ret
= w1_uart_serdev_tx_rx(w1dev
, w1cfg
, &val
);
351 /* return inactive bus state on error */
355 return val
== w1cfg
->tx_byte
? 1 : 0;
358 static int w1_uart_probe(struct serdev_device
*serdev
)
360 struct device
*dev
= &serdev
->dev
;
361 struct w1_uart_device
*w1dev
;
364 w1dev
= devm_kzalloc(dev
, sizeof(*w1dev
), GFP_KERNEL
);
367 w1dev
->bus
.data
= w1dev
;
368 w1dev
->bus
.reset_bus
= w1_uart_reset_bus
;
369 w1dev
->bus
.touch_bit
= w1_uart_touch_bit
;
370 w1dev
->serdev
= serdev
;
372 init_completion(&w1dev
->rx_byte_received
);
373 mutex_init(&w1dev
->rx_mutex
);
375 ret
= w1_uart_serdev_open(w1dev
);
378 serdev_device_set_drvdata(serdev
, w1dev
);
379 serdev_device_set_client_ops(serdev
, &w1_uart_serdev_ops
);
381 return w1_add_master_device(&w1dev
->bus
);
384 static void w1_uart_remove(struct serdev_device
*serdev
)
386 struct w1_uart_device
*w1dev
= serdev_device_get_drvdata(serdev
);
389 * Waits until w1-uart callbacks are finished, serdev is closed
390 * and its device data released automatically by devres (waits
391 * until serdev-receive is finished).
393 w1_remove_master_device(&w1dev
->bus
);
396 static const struct of_device_id w1_uart_of_match
[] = {
397 { .compatible
= "w1-uart" },
400 MODULE_DEVICE_TABLE(of
, w1_uart_of_match
);
402 static struct serdev_device_driver w1_uart_driver
= {
405 .of_match_table
= w1_uart_of_match
,
407 .probe
= w1_uart_probe
,
408 .remove
= w1_uart_remove
,
411 module_serdev_device_driver(w1_uart_driver
);
413 MODULE_DESCRIPTION("UART w1 bus driver");
414 MODULE_AUTHOR("Christoph Winklhofer <cj.winklhofer@gmail.com>");
415 MODULE_LICENSE("GPL");