w1-uart.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * w1-uart - UART 1-Wire bus driver
  4. *
  5. * Uses the UART interface (via Serial Device Bus) to create the 1-Wire
  6. * timing patterns. Implements the following 1-Wire master interface:
  7. *
  8. * - reset_bus: requests baud-rate 9600
  9. *
  10. * - touch_bit: requests baud-rate 115200
  11. *
  12. * Author: Christoph Winklhofer <cj.winklhofer@gmail.com>
  13. */
  14. #include <linux/completion.h>
  15. #include <linux/delay.h>
  16. #include <linux/jiffies.h>
  17. #include <linux/module.h>
  18. #include <linux/mutex.h>
  19. #include <linux/of.h>
  20. #include <linux/serdev.h>
  21. #include <linux/w1.h>
  22. /* UART packet contains start and stop bit */
  23. #define W1_UART_BITS_PER_PACKET (BITS_PER_BYTE + 2)
  24. /* Timeout to wait for completion of serdev-receive */
  25. #define W1_UART_TIMEOUT msecs_to_jiffies(500)
  26. /**
  27. * struct w1_uart_config - configuration for 1-Wire operation
  28. * @baudrate: baud-rate returned from serdev
  29. * @delay_us: delay to complete a 1-Wire cycle (in us)
  30. * @tx_byte: byte to generate 1-Wire timing pattern
  31. */
  32. struct w1_uart_config {
  33. unsigned int baudrate;
  34. unsigned int delay_us;
  35. u8 tx_byte;
  36. };
  37. /**
  38. * struct w1_uart_device - 1-Wire UART device structure
  39. * @serdev: serial device
  40. * @bus: w1-bus master
  41. * @cfg_reset: config for 1-Wire reset
  42. * @cfg_touch_0: config for 1-Wire write-0 cycle
  43. * @cfg_touch_1: config for 1-Wire write-1 and read cycle
  44. * @rx_byte_received: completion for serdev receive
  45. * @rx_mutex: mutex to protect rx_err and rx_byte
  46. * @rx_err: indicates an error in serdev-receive
  47. * @rx_byte: result byte from serdev-receive
  48. */
  49. struct w1_uart_device {
  50. struct serdev_device *serdev;
  51. struct w1_bus_master bus;
  52. struct w1_uart_config cfg_reset;
  53. struct w1_uart_config cfg_touch_0;
  54. struct w1_uart_config cfg_touch_1;
  55. struct completion rx_byte_received;
  56. /*
  57. * protect rx_err and rx_byte from concurrent access in
  58. * w1-callbacks and serdev-receive.
  59. */
  60. struct mutex rx_mutex;
  61. int rx_err;
  62. u8 rx_byte;
  63. };
  64. /**
  65. * struct w1_uart_limits - limits for 1-Wire operations
  66. * @baudrate: Requested baud-rate to create 1-Wire timing pattern
  67. * @bit_min_us: minimum time for a bit (in us)
  68. * @bit_max_us: maximum time for a bit (in us)
  69. * @sample_us: timespan to sample 1-Wire response
  70. * @cycle_us: duration of the 1-Wire cycle
  71. */
  72. struct w1_uart_limits {
  73. unsigned int baudrate;
  74. unsigned int bit_min_us;
  75. unsigned int bit_max_us;
  76. unsigned int sample_us;
  77. unsigned int cycle_us;
  78. };
  79. static inline unsigned int baud_to_bit_ns(unsigned int baud)
  80. {
  81. return NSEC_PER_SEC / baud;
  82. }
  83. static inline unsigned int to_ns(unsigned int us)
  84. {
  85. return us * NSEC_PER_USEC;
  86. }
  87. /*
  88. * Set baud-rate, delay and tx-byte to create a 1-Wire pulse and adapt
  89. * the tx-byte according to the actual baud-rate.
  90. *
  91. * Reject when:
  92. * - time for a bit outside min/max range
  93. * - a 1-Wire response is not detectable for sent byte
  94. */
  95. static int w1_uart_set_config(struct serdev_device *serdev,
  96. const struct w1_uart_limits *limits,
  97. struct w1_uart_config *w1cfg)
  98. {
  99. unsigned int packet_ns;
  100. unsigned int bits_low;
  101. unsigned int bit_ns;
  102. unsigned int low_ns;
  103. w1cfg->baudrate = serdev_device_set_baudrate(serdev, limits->baudrate);
  104. if (w1cfg->baudrate == 0)
  105. return -EINVAL;
  106. /* Compute in nanoseconds for accuracy */
  107. bit_ns = baud_to_bit_ns(w1cfg->baudrate);
  108. bits_low = to_ns(limits->bit_min_us) / bit_ns;
  109. /* start bit is always low */
  110. low_ns = bit_ns * (bits_low + 1);
  111. if (low_ns < to_ns(limits->bit_min_us))
  112. return -EINVAL;
  113. if (low_ns > to_ns(limits->bit_max_us))
  114. return -EINVAL;
  115. /* 1-Wire response detectable for sent byte */
  116. if (limits->sample_us > 0 &&
  117. bit_ns * BITS_PER_BYTE < low_ns + to_ns(limits->sample_us))
  118. return -EINVAL;
  119. /* delay: 1-Wire cycle takes longer than the UART packet */
  120. packet_ns = bit_ns * W1_UART_BITS_PER_PACKET;
  121. w1cfg->delay_us = 0;
  122. if (to_ns(limits->cycle_us) > packet_ns)
  123. w1cfg->delay_us =
  124. (to_ns(limits->cycle_us) - packet_ns) / NSEC_PER_USEC;
  125. /* byte to create 1-Wire pulse */
  126. w1cfg->tx_byte = 0xff << bits_low;
  127. return 0;
  128. }
  129. /*
  130. * Configuration for reset and presence detect
  131. * - bit_min_us is 480us, add margin and use 485us
  132. * - limits for sample time 60us-75us, use 65us
  133. */
  134. static int w1_uart_set_config_reset(struct w1_uart_device *w1dev)
  135. {
  136. struct serdev_device *serdev = w1dev->serdev;
  137. struct device_node *np = serdev->dev.of_node;
  138. struct w1_uart_limits limits = { .baudrate = 9600,
  139. .bit_min_us = 485,
  140. .bit_max_us = 640,
  141. .sample_us = 65,
  142. .cycle_us = 960 };
  143. of_property_read_u32(np, "reset-bps", &limits.baudrate);
  144. return w1_uart_set_config(serdev, &limits, &w1dev->cfg_reset);
  145. }
  146. /*
  147. * Configuration for write-0 cycle (touch bit 0)
  148. * - bit_min_us is 60us, add margin and use 65us
  149. * - no sampling required, sample_us = 0
  150. */
  151. static int w1_uart_set_config_touch_0(struct w1_uart_device *w1dev)
  152. {
  153. struct serdev_device *serdev = w1dev->serdev;
  154. struct device_node *np = serdev->dev.of_node;
  155. struct w1_uart_limits limits = { .baudrate = 115200,
  156. .bit_min_us = 65,
  157. .bit_max_us = 120,
  158. .sample_us = 0,
  159. .cycle_us = 70 };
  160. of_property_read_u32(np, "write-0-bps", &limits.baudrate);
  161. return w1_uart_set_config(serdev, &limits, &w1dev->cfg_touch_0);
  162. }
  163. /*
  164. * Configuration for write-1 and read cycle (touch bit 1)
  165. * - bit_min_us is 5us, add margin and use 6us
  166. * - limits for sample time 5us-15us, use 15us
  167. */
  168. static int w1_uart_set_config_touch_1(struct w1_uart_device *w1dev)
  169. {
  170. struct serdev_device *serdev = w1dev->serdev;
  171. struct device_node *np = serdev->dev.of_node;
  172. struct w1_uart_limits limits = { .baudrate = 115200,
  173. .bit_min_us = 6,
  174. .bit_max_us = 15,
  175. .sample_us = 15,
  176. .cycle_us = 70 };
  177. of_property_read_u32(np, "write-1-bps", &limits.baudrate);
  178. return w1_uart_set_config(serdev, &limits, &w1dev->cfg_touch_1);
  179. }
  180. /*
  181. * Configure and open the serial device
  182. */
  183. static int w1_uart_serdev_open(struct w1_uart_device *w1dev)
  184. {
  185. struct serdev_device *serdev = w1dev->serdev;
  186. struct device *dev = &serdev->dev;
  187. int ret;
  188. ret = devm_serdev_device_open(dev, serdev);
  189. if (ret < 0)
  190. return ret;
  191. ret = serdev_device_set_parity(serdev, SERDEV_PARITY_NONE);
  192. if (ret < 0) {
  193. dev_err(dev, "set parity failed\n");
  194. return ret;
  195. }
  196. ret = w1_uart_set_config_reset(w1dev);
  197. if (ret < 0) {
  198. dev_err(dev, "config for reset failed\n");
  199. return ret;
  200. }
  201. ret = w1_uart_set_config_touch_0(w1dev);
  202. if (ret < 0) {
  203. dev_err(dev, "config for touch-0 failed\n");
  204. return ret;
  205. }
  206. ret = w1_uart_set_config_touch_1(w1dev);
  207. if (ret < 0) {
  208. dev_err(dev, "config for touch-1 failed\n");
  209. return ret;
  210. }
  211. serdev_device_set_flow_control(serdev, false);
  212. return 0;
  213. }
  214. /*
  215. * Send one byte (tx_byte) and read one byte (rx_byte) via serdev.
  216. */
  217. static int w1_uart_serdev_tx_rx(struct w1_uart_device *w1dev,
  218. const struct w1_uart_config *w1cfg, u8 *rx_byte)
  219. {
  220. struct serdev_device *serdev = w1dev->serdev;
  221. int ret;
  222. serdev_device_write_flush(serdev);
  223. serdev_device_set_baudrate(serdev, w1cfg->baudrate);
  224. /* write and immediately read one byte */
  225. reinit_completion(&w1dev->rx_byte_received);
  226. ret = serdev_device_write_buf(serdev, &w1cfg->tx_byte, 1);
  227. if (ret != 1)
  228. return -EIO;
  229. ret = wait_for_completion_interruptible_timeout(
  230. &w1dev->rx_byte_received, W1_UART_TIMEOUT);
  231. if (ret <= 0)
  232. return -EIO;
  233. /* locking could fail when serdev is unexpectedly receiving. */
  234. if (!mutex_trylock(&w1dev->rx_mutex))
  235. return -EIO;
  236. ret = w1dev->rx_err;
  237. if (ret == 0)
  238. *rx_byte = w1dev->rx_byte;
  239. mutex_unlock(&w1dev->rx_mutex);
  240. if (w1cfg->delay_us > 0)
  241. fsleep(w1cfg->delay_us);
  242. return ret;
  243. }
  244. static size_t w1_uart_serdev_receive_buf(struct serdev_device *serdev,
  245. const u8 *buf, size_t count)
  246. {
  247. struct w1_uart_device *w1dev = serdev_device_get_drvdata(serdev);
  248. mutex_lock(&w1dev->rx_mutex);
  249. /* sent a single byte and receive one single byte */
  250. if (count == 1) {
  251. w1dev->rx_byte = buf[0];
  252. w1dev->rx_err = 0;
  253. } else {
  254. w1dev->rx_err = -EIO;
  255. }
  256. mutex_unlock(&w1dev->rx_mutex);
  257. complete(&w1dev->rx_byte_received);
  258. return count;
  259. }
  260. static const struct serdev_device_ops w1_uart_serdev_ops = {
  261. .receive_buf = w1_uart_serdev_receive_buf,
  262. .write_wakeup = serdev_device_write_wakeup,
  263. };
  264. /*
  265. * 1-wire reset and presence detect: A present slave will manipulate
  266. * the received byte by pulling the 1-Wire low.
  267. */
  268. static u8 w1_uart_reset_bus(void *data)
  269. {
  270. struct w1_uart_device *w1dev = data;
  271. const struct w1_uart_config *w1cfg = &w1dev->cfg_reset;
  272. int ret;
  273. u8 val;
  274. ret = w1_uart_serdev_tx_rx(w1dev, w1cfg, &val);
  275. if (ret < 0)
  276. return -1;
  277. /* Device present (0) or no device (1) */
  278. return val != w1cfg->tx_byte ? 0 : 1;
  279. }
  280. /*
  281. * 1-Wire read and write cycle: Only the read-0 manipulates the
  282. * received byte, all others left the line untouched.
  283. */
  284. static u8 w1_uart_touch_bit(void *data, u8 bit)
  285. {
  286. struct w1_uart_device *w1dev = data;
  287. const struct w1_uart_config *w1cfg = bit ? &w1dev->cfg_touch_1 :
  288. &w1dev->cfg_touch_0;
  289. int ret;
  290. u8 val;
  291. ret = w1_uart_serdev_tx_rx(w1dev, w1cfg, &val);
  292. /* return inactive bus state on error */
  293. if (ret < 0)
  294. return 1;
  295. return val == w1cfg->tx_byte ? 1 : 0;
  296. }
  297. static int w1_uart_probe(struct serdev_device *serdev)
  298. {
  299. struct device *dev = &serdev->dev;
  300. struct w1_uart_device *w1dev;
  301. int ret;
  302. w1dev = devm_kzalloc(dev, sizeof(*w1dev), GFP_KERNEL);
  303. if (!w1dev)
  304. return -ENOMEM;
  305. w1dev->bus.data = w1dev;
  306. w1dev->bus.reset_bus = w1_uart_reset_bus;
  307. w1dev->bus.touch_bit = w1_uart_touch_bit;
  308. w1dev->serdev = serdev;
  309. init_completion(&w1dev->rx_byte_received);
  310. mutex_init(&w1dev->rx_mutex);
  311. serdev_device_set_drvdata(serdev, w1dev);
  312. serdev_device_set_client_ops(serdev, &w1_uart_serdev_ops);
  313. ret = w1_uart_serdev_open(w1dev);
  314. if (ret < 0)
  315. return ret;
  316. return w1_add_master_device(&w1dev->bus);
  317. }
  318. static void w1_uart_remove(struct serdev_device *serdev)
  319. {
  320. struct w1_uart_device *w1dev = serdev_device_get_drvdata(serdev);
  321. /*
  322. * Waits until w1-uart callbacks are finished, serdev is closed
  323. * and its device data released automatically by devres (waits
  324. * until serdev-receive is finished).
  325. */
  326. w1_remove_master_device(&w1dev->bus);
  327. }
  328. static const struct of_device_id w1_uart_of_match[] = {
  329. { .compatible = "w1-uart" },
  330. {},
  331. };
  332. MODULE_DEVICE_TABLE(of, w1_uart_of_match);
  333. static struct serdev_device_driver w1_uart_driver = {
  334. .driver = {
  335. .name = "w1-uart",
  336. .of_match_table = w1_uart_of_match,
  337. },
  338. .probe = w1_uart_probe,
  339. .remove = w1_uart_remove,
  340. };
  341. module_serdev_device_driver(w1_uart_driver);
  342. MODULE_DESCRIPTION("UART w1 bus driver");
  343. MODULE_AUTHOR("Christoph Winklhofer <cj.winklhofer@gmail.com>");
  344. MODULE_LICENSE("GPL");