serial_mctrl_gpio.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Helpers for controlling modem lines via GPIO
  4. *
  5. * Copyright (C) 2014 Paratronic S.A.
  6. */
  7. #include <linux/err.h>
  8. #include <linux/device.h>
  9. #include <linux/irq.h>
  10. #include <linux/gpio/consumer.h>
  11. #include <linux/termios.h>
  12. #include <linux/serial_core.h>
  13. #include <linux/module.h>
  14. #include <linux/property.h>
  15. #include "serial_mctrl_gpio.h"
  16. struct mctrl_gpios {
  17. struct uart_port *port;
  18. struct gpio_desc *gpio[UART_GPIO_MAX];
  19. int irq[UART_GPIO_MAX];
  20. unsigned int mctrl_prev;
  21. bool mctrl_on;
  22. };
  23. static const struct {
  24. const char *name;
  25. unsigned int mctrl;
  26. enum gpiod_flags flags;
  27. } mctrl_gpios_desc[UART_GPIO_MAX] = {
  28. { "cts", TIOCM_CTS, GPIOD_IN, },
  29. { "dsr", TIOCM_DSR, GPIOD_IN, },
  30. { "dcd", TIOCM_CD, GPIOD_IN, },
  31. { "rng", TIOCM_RNG, GPIOD_IN, },
  32. { "rts", TIOCM_RTS, GPIOD_OUT_LOW, },
  33. { "dtr", TIOCM_DTR, GPIOD_OUT_LOW, },
  34. };
  35. static bool mctrl_gpio_flags_is_dir_out(unsigned int idx)
  36. {
  37. return mctrl_gpios_desc[idx].flags & GPIOD_FLAGS_BIT_DIR_OUT;
  38. }
  39. /**
  40. * mctrl_gpio_set - set gpios according to mctrl state
  41. * @gpios: gpios to set
  42. * @mctrl: state to set
  43. *
  44. * Set the gpios according to the mctrl state.
  45. */
  46. void mctrl_gpio_set(struct mctrl_gpios *gpios, unsigned int mctrl)
  47. {
  48. enum mctrl_gpio_idx i;
  49. struct gpio_desc *desc_array[UART_GPIO_MAX];
  50. DECLARE_BITMAP(values, UART_GPIO_MAX);
  51. unsigned int count = 0;
  52. if (gpios == NULL)
  53. return;
  54. for (i = 0; i < UART_GPIO_MAX; i++)
  55. if (gpios->gpio[i] && mctrl_gpio_flags_is_dir_out(i)) {
  56. desc_array[count] = gpios->gpio[i];
  57. __assign_bit(count, values,
  58. mctrl & mctrl_gpios_desc[i].mctrl);
  59. count++;
  60. }
  61. gpiod_set_array_value(count, desc_array, NULL, values);
  62. }
  63. EXPORT_SYMBOL_GPL(mctrl_gpio_set);
  64. /**
  65. * mctrl_gpio_to_gpiod - obtain gpio_desc of modem line index
  66. * @gpios: gpios to look into
  67. * @gidx: index of the modem line
  68. * Returns: the gpio_desc structure associated to the modem line index
  69. */
  70. struct gpio_desc *mctrl_gpio_to_gpiod(struct mctrl_gpios *gpios,
  71. enum mctrl_gpio_idx gidx)
  72. {
  73. if (gpios == NULL)
  74. return NULL;
  75. return gpios->gpio[gidx];
  76. }
  77. EXPORT_SYMBOL_GPL(mctrl_gpio_to_gpiod);
  78. /**
  79. * mctrl_gpio_get - update mctrl with the gpios values.
  80. * @gpios: gpios to get the info from
  81. * @mctrl: mctrl to set
  82. * Returns: modified mctrl (the same value as in @mctrl)
  83. *
  84. * Update mctrl with the gpios values.
  85. */
  86. unsigned int mctrl_gpio_get(struct mctrl_gpios *gpios, unsigned int *mctrl)
  87. {
  88. enum mctrl_gpio_idx i;
  89. if (gpios == NULL)
  90. return *mctrl;
  91. for (i = 0; i < UART_GPIO_MAX; i++) {
  92. if (gpios->gpio[i] && !mctrl_gpio_flags_is_dir_out(i)) {
  93. if (gpiod_get_value(gpios->gpio[i]))
  94. *mctrl |= mctrl_gpios_desc[i].mctrl;
  95. else
  96. *mctrl &= ~mctrl_gpios_desc[i].mctrl;
  97. }
  98. }
  99. return *mctrl;
  100. }
  101. EXPORT_SYMBOL_GPL(mctrl_gpio_get);
  102. unsigned int
  103. mctrl_gpio_get_outputs(struct mctrl_gpios *gpios, unsigned int *mctrl)
  104. {
  105. enum mctrl_gpio_idx i;
  106. if (gpios == NULL)
  107. return *mctrl;
  108. for (i = 0; i < UART_GPIO_MAX; i++) {
  109. if (gpios->gpio[i] && mctrl_gpio_flags_is_dir_out(i)) {
  110. if (gpiod_get_value(gpios->gpio[i]))
  111. *mctrl |= mctrl_gpios_desc[i].mctrl;
  112. else
  113. *mctrl &= ~mctrl_gpios_desc[i].mctrl;
  114. }
  115. }
  116. return *mctrl;
  117. }
  118. EXPORT_SYMBOL_GPL(mctrl_gpio_get_outputs);
  119. struct mctrl_gpios *mctrl_gpio_init_noauto(struct device *dev, unsigned int idx)
  120. {
  121. struct mctrl_gpios *gpios;
  122. enum mctrl_gpio_idx i;
  123. gpios = devm_kzalloc(dev, sizeof(*gpios), GFP_KERNEL);
  124. if (!gpios)
  125. return ERR_PTR(-ENOMEM);
  126. for (i = 0; i < UART_GPIO_MAX; i++) {
  127. char *gpio_str;
  128. bool present;
  129. /* Check if GPIO property exists and continue if not */
  130. gpio_str = kasprintf(GFP_KERNEL, "%s-gpios",
  131. mctrl_gpios_desc[i].name);
  132. if (!gpio_str)
  133. continue;
  134. present = device_property_present(dev, gpio_str);
  135. kfree(gpio_str);
  136. if (!present)
  137. continue;
  138. gpios->gpio[i] =
  139. devm_gpiod_get_index_optional(dev,
  140. mctrl_gpios_desc[i].name,
  141. idx,
  142. mctrl_gpios_desc[i].flags);
  143. if (IS_ERR(gpios->gpio[i]))
  144. return ERR_CAST(gpios->gpio[i]);
  145. }
  146. return gpios;
  147. }
  148. EXPORT_SYMBOL_GPL(mctrl_gpio_init_noauto);
  149. #define MCTRL_ANY_DELTA (TIOCM_RI | TIOCM_DSR | TIOCM_CD | TIOCM_CTS)
  150. static irqreturn_t mctrl_gpio_irq_handle(int irq, void *context)
  151. {
  152. struct mctrl_gpios *gpios = context;
  153. struct uart_port *port = gpios->port;
  154. u32 mctrl = gpios->mctrl_prev;
  155. u32 mctrl_diff;
  156. unsigned long flags;
  157. mctrl_gpio_get(gpios, &mctrl);
  158. spin_lock_irqsave(&port->lock, flags);
  159. mctrl_diff = mctrl ^ gpios->mctrl_prev;
  160. gpios->mctrl_prev = mctrl;
  161. if (mctrl_diff & MCTRL_ANY_DELTA && port->state != NULL) {
  162. if ((mctrl_diff & mctrl) & TIOCM_RI)
  163. port->icount.rng++;
  164. if ((mctrl_diff & mctrl) & TIOCM_DSR)
  165. port->icount.dsr++;
  166. if (mctrl_diff & TIOCM_CD)
  167. uart_handle_dcd_change(port, mctrl & TIOCM_CD);
  168. if (mctrl_diff & TIOCM_CTS)
  169. uart_handle_cts_change(port, mctrl & TIOCM_CTS);
  170. wake_up_interruptible(&port->state->port.delta_msr_wait);
  171. }
  172. spin_unlock_irqrestore(&port->lock, flags);
  173. return IRQ_HANDLED;
  174. }
  175. /**
  176. * mctrl_gpio_init - initialize uart gpios
  177. * @port: port to initialize gpios for
  178. * @idx: index of the gpio in the @port's device
  179. *
  180. * This will get the {cts,rts,...}-gpios from device tree if they are present
  181. * and request them, set direction etc, and return an allocated structure.
  182. * `devm_*` functions are used, so there's no need to call mctrl_gpio_free().
  183. * As this sets up the irq handling, make sure to not handle changes to the
  184. * gpio input lines in your driver, too.
  185. */
  186. struct mctrl_gpios *mctrl_gpio_init(struct uart_port *port, unsigned int idx)
  187. {
  188. struct mctrl_gpios *gpios;
  189. enum mctrl_gpio_idx i;
  190. gpios = mctrl_gpio_init_noauto(port->dev, idx);
  191. if (IS_ERR(gpios))
  192. return gpios;
  193. gpios->port = port;
  194. for (i = 0; i < UART_GPIO_MAX; ++i) {
  195. int ret;
  196. if (!gpios->gpio[i] || mctrl_gpio_flags_is_dir_out(i))
  197. continue;
  198. ret = gpiod_to_irq(gpios->gpio[i]);
  199. if (ret < 0) {
  200. dev_err(port->dev,
  201. "failed to find corresponding irq for %s (idx=%d, err=%d)\n",
  202. mctrl_gpios_desc[i].name, idx, ret);
  203. return ERR_PTR(ret);
  204. }
  205. gpios->irq[i] = ret;
  206. /* irqs should only be enabled in .enable_ms */
  207. irq_set_status_flags(gpios->irq[i], IRQ_NOAUTOEN);
  208. ret = devm_request_irq(port->dev, gpios->irq[i],
  209. mctrl_gpio_irq_handle,
  210. IRQ_TYPE_EDGE_BOTH, dev_name(port->dev),
  211. gpios);
  212. if (ret) {
  213. /* alternatively implement polling */
  214. dev_err(port->dev,
  215. "failed to request irq for %s (idx=%d, err=%d)\n",
  216. mctrl_gpios_desc[i].name, idx, ret);
  217. return ERR_PTR(ret);
  218. }
  219. }
  220. return gpios;
  221. }
  222. EXPORT_SYMBOL_GPL(mctrl_gpio_init);
  223. /**
  224. * mctrl_gpio_free - explicitly free uart gpios
  225. * @dev: uart port's device
  226. * @gpios: gpios structure to be freed
  227. *
  228. * This will free the requested gpios in mctrl_gpio_init(). As `devm_*`
  229. * functions are used, there's generally no need to call this function.
  230. */
  231. void mctrl_gpio_free(struct device *dev, struct mctrl_gpios *gpios)
  232. {
  233. enum mctrl_gpio_idx i;
  234. if (gpios == NULL)
  235. return;
  236. for (i = 0; i < UART_GPIO_MAX; i++) {
  237. if (gpios->irq[i])
  238. devm_free_irq(gpios->port->dev, gpios->irq[i], gpios);
  239. if (gpios->gpio[i])
  240. devm_gpiod_put(dev, gpios->gpio[i]);
  241. }
  242. devm_kfree(dev, gpios);
  243. }
  244. EXPORT_SYMBOL_GPL(mctrl_gpio_free);
  245. /**
  246. * mctrl_gpio_enable_ms - enable irqs and handling of changes to the ms lines
  247. * @gpios: gpios to enable
  248. */
  249. void mctrl_gpio_enable_ms(struct mctrl_gpios *gpios)
  250. {
  251. enum mctrl_gpio_idx i;
  252. if (gpios == NULL)
  253. return;
  254. /* .enable_ms may be called multiple times */
  255. if (gpios->mctrl_on)
  256. return;
  257. gpios->mctrl_on = true;
  258. /* get initial status of modem lines GPIOs */
  259. mctrl_gpio_get(gpios, &gpios->mctrl_prev);
  260. for (i = 0; i < UART_GPIO_MAX; ++i) {
  261. if (!gpios->irq[i])
  262. continue;
  263. enable_irq(gpios->irq[i]);
  264. }
  265. }
  266. EXPORT_SYMBOL_GPL(mctrl_gpio_enable_ms);
  267. /**
  268. * mctrl_gpio_disable_ms - disable irqs and handling of changes to the ms lines
  269. * @gpios: gpios to disable
  270. */
  271. void mctrl_gpio_disable_ms(struct mctrl_gpios *gpios)
  272. {
  273. enum mctrl_gpio_idx i;
  274. if (gpios == NULL)
  275. return;
  276. if (!gpios->mctrl_on)
  277. return;
  278. gpios->mctrl_on = false;
  279. for (i = 0; i < UART_GPIO_MAX; ++i) {
  280. if (!gpios->irq[i])
  281. continue;
  282. disable_irq(gpios->irq[i]);
  283. }
  284. }
  285. EXPORT_SYMBOL_GPL(mctrl_gpio_disable_ms);
  286. void mctrl_gpio_enable_irq_wake(struct mctrl_gpios *gpios)
  287. {
  288. enum mctrl_gpio_idx i;
  289. if (!gpios)
  290. return;
  291. if (!gpios->mctrl_on)
  292. return;
  293. for (i = 0; i < UART_GPIO_MAX; ++i) {
  294. if (!gpios->irq[i])
  295. continue;
  296. enable_irq_wake(gpios->irq[i]);
  297. }
  298. }
  299. EXPORT_SYMBOL_GPL(mctrl_gpio_enable_irq_wake);
  300. void mctrl_gpio_disable_irq_wake(struct mctrl_gpios *gpios)
  301. {
  302. enum mctrl_gpio_idx i;
  303. if (!gpios)
  304. return;
  305. if (!gpios->mctrl_on)
  306. return;
  307. for (i = 0; i < UART_GPIO_MAX; ++i) {
  308. if (!gpios->irq[i])
  309. continue;
  310. disable_irq_wake(gpios->irq[i]);
  311. }
  312. }
  313. EXPORT_SYMBOL_GPL(mctrl_gpio_disable_irq_wake);
  314. MODULE_LICENSE("GPL");