serial_mctrl_gpio.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Helpers for controlling modem lines via GPIO
  3. *
  4. * Copyright (C) 2014 Paratronic S.A.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/err.h>
  17. #include <linux/device.h>
  18. #include <linux/irq.h>
  19. #include <linux/gpio/consumer.h>
  20. #include <linux/termios.h>
  21. #include <linux/serial_core.h>
  22. #include <linux/module.h>
  23. #include "serial_mctrl_gpio.h"
  24. struct mctrl_gpios {
  25. struct uart_port *port;
  26. struct gpio_desc *gpio[UART_GPIO_MAX];
  27. int irq[UART_GPIO_MAX];
  28. unsigned int mctrl_prev;
  29. bool mctrl_on;
  30. };
  31. static const struct {
  32. const char *name;
  33. unsigned int mctrl;
  34. bool dir_out;
  35. } mctrl_gpios_desc[UART_GPIO_MAX] = {
  36. { "cts", TIOCM_CTS, false, },
  37. { "dsr", TIOCM_DSR, false, },
  38. { "dcd", TIOCM_CD, false, },
  39. { "rng", TIOCM_RNG, false, },
  40. { "rts", TIOCM_RTS, true, },
  41. { "dtr", TIOCM_DTR, true, },
  42. };
  43. void mctrl_gpio_set(struct mctrl_gpios *gpios, unsigned int mctrl)
  44. {
  45. enum mctrl_gpio_idx i;
  46. struct gpio_desc *desc_array[UART_GPIO_MAX];
  47. int value_array[UART_GPIO_MAX];
  48. unsigned int count = 0;
  49. if (gpios == NULL)
  50. return;
  51. for (i = 0; i < UART_GPIO_MAX; i++)
  52. if (gpios->gpio[i] && mctrl_gpios_desc[i].dir_out) {
  53. desc_array[count] = gpios->gpio[i];
  54. value_array[count] = !!(mctrl & mctrl_gpios_desc[i].mctrl);
  55. count++;
  56. }
  57. gpiod_set_array_value(count, desc_array, value_array);
  58. }
  59. EXPORT_SYMBOL_GPL(mctrl_gpio_set);
  60. struct gpio_desc *mctrl_gpio_to_gpiod(struct mctrl_gpios *gpios,
  61. enum mctrl_gpio_idx gidx)
  62. {
  63. if (gpios == NULL)
  64. return NULL;
  65. return gpios->gpio[gidx];
  66. }
  67. EXPORT_SYMBOL_GPL(mctrl_gpio_to_gpiod);
  68. unsigned int mctrl_gpio_get(struct mctrl_gpios *gpios, unsigned int *mctrl)
  69. {
  70. enum mctrl_gpio_idx i;
  71. if (gpios == NULL)
  72. return *mctrl;
  73. for (i = 0; i < UART_GPIO_MAX; i++) {
  74. if (gpios->gpio[i] && !mctrl_gpios_desc[i].dir_out) {
  75. if (gpiod_get_value(gpios->gpio[i]))
  76. *mctrl |= mctrl_gpios_desc[i].mctrl;
  77. else
  78. *mctrl &= ~mctrl_gpios_desc[i].mctrl;
  79. }
  80. }
  81. return *mctrl;
  82. }
  83. EXPORT_SYMBOL_GPL(mctrl_gpio_get);
  84. unsigned int
  85. mctrl_gpio_get_outputs(struct mctrl_gpios *gpios, unsigned int *mctrl)
  86. {
  87. enum mctrl_gpio_idx i;
  88. if (gpios == NULL)
  89. return *mctrl;
  90. for (i = 0; i < UART_GPIO_MAX; i++) {
  91. if (gpios->gpio[i] && mctrl_gpios_desc[i].dir_out) {
  92. if (gpiod_get_value(gpios->gpio[i]))
  93. *mctrl |= mctrl_gpios_desc[i].mctrl;
  94. else
  95. *mctrl &= ~mctrl_gpios_desc[i].mctrl;
  96. }
  97. }
  98. return *mctrl;
  99. }
  100. EXPORT_SYMBOL_GPL(mctrl_gpio_get_outputs);
  101. struct mctrl_gpios *mctrl_gpio_init_noauto(struct device *dev, unsigned int idx)
  102. {
  103. struct mctrl_gpios *gpios;
  104. enum mctrl_gpio_idx i;
  105. gpios = devm_kzalloc(dev, sizeof(*gpios), GFP_KERNEL);
  106. if (!gpios)
  107. return ERR_PTR(-ENOMEM);
  108. for (i = 0; i < UART_GPIO_MAX; i++) {
  109. enum gpiod_flags flags;
  110. if (mctrl_gpios_desc[i].dir_out)
  111. flags = GPIOD_OUT_LOW;
  112. else
  113. flags = GPIOD_IN;
  114. gpios->gpio[i] =
  115. devm_gpiod_get_index_optional(dev,
  116. mctrl_gpios_desc[i].name,
  117. idx, flags);
  118. if (IS_ERR(gpios->gpio[i]))
  119. return ERR_CAST(gpios->gpio[i]);
  120. }
  121. return gpios;
  122. }
  123. EXPORT_SYMBOL_GPL(mctrl_gpio_init_noauto);
  124. #define MCTRL_ANY_DELTA (TIOCM_RI | TIOCM_DSR | TIOCM_CD | TIOCM_CTS)
  125. static irqreturn_t mctrl_gpio_irq_handle(int irq, void *context)
  126. {
  127. struct mctrl_gpios *gpios = context;
  128. struct uart_port *port = gpios->port;
  129. u32 mctrl = gpios->mctrl_prev;
  130. u32 mctrl_diff;
  131. unsigned long flags;
  132. mctrl_gpio_get(gpios, &mctrl);
  133. spin_lock_irqsave(&port->lock, flags);
  134. mctrl_diff = mctrl ^ gpios->mctrl_prev;
  135. gpios->mctrl_prev = mctrl;
  136. if (mctrl_diff & MCTRL_ANY_DELTA && port->state != NULL) {
  137. if ((mctrl_diff & mctrl) & TIOCM_RI)
  138. port->icount.rng++;
  139. if ((mctrl_diff & mctrl) & TIOCM_DSR)
  140. port->icount.dsr++;
  141. if (mctrl_diff & TIOCM_CD)
  142. uart_handle_dcd_change(port, mctrl & TIOCM_CD);
  143. if (mctrl_diff & TIOCM_CTS)
  144. uart_handle_cts_change(port, mctrl & TIOCM_CTS);
  145. wake_up_interruptible(&port->state->port.delta_msr_wait);
  146. }
  147. spin_unlock_irqrestore(&port->lock, flags);
  148. return IRQ_HANDLED;
  149. }
  150. struct mctrl_gpios *mctrl_gpio_init(struct uart_port *port, unsigned int idx)
  151. {
  152. struct mctrl_gpios *gpios;
  153. enum mctrl_gpio_idx i;
  154. gpios = mctrl_gpio_init_noauto(port->dev, idx);
  155. if (IS_ERR(gpios))
  156. return gpios;
  157. gpios->port = port;
  158. for (i = 0; i < UART_GPIO_MAX; ++i) {
  159. int ret;
  160. if (!gpios->gpio[i] || mctrl_gpios_desc[i].dir_out)
  161. continue;
  162. ret = gpiod_to_irq(gpios->gpio[i]);
  163. if (ret <= 0) {
  164. dev_err(port->dev,
  165. "failed to find corresponding irq for %s (idx=%d, err=%d)\n",
  166. mctrl_gpios_desc[i].name, idx, ret);
  167. return ERR_PTR(ret);
  168. }
  169. gpios->irq[i] = ret;
  170. /* irqs should only be enabled in .enable_ms */
  171. irq_set_status_flags(gpios->irq[i], IRQ_NOAUTOEN);
  172. ret = devm_request_irq(port->dev, gpios->irq[i],
  173. mctrl_gpio_irq_handle,
  174. IRQ_TYPE_EDGE_BOTH, dev_name(port->dev),
  175. gpios);
  176. if (ret) {
  177. /* alternatively implement polling */
  178. dev_err(port->dev,
  179. "failed to request irq for %s (idx=%d, err=%d)\n",
  180. mctrl_gpios_desc[i].name, idx, ret);
  181. return ERR_PTR(ret);
  182. }
  183. }
  184. return gpios;
  185. }
  186. EXPORT_SYMBOL_GPL(mctrl_gpio_init);
  187. void mctrl_gpio_free(struct device *dev, struct mctrl_gpios *gpios)
  188. {
  189. enum mctrl_gpio_idx i;
  190. if (gpios == NULL)
  191. return;
  192. for (i = 0; i < UART_GPIO_MAX; i++) {
  193. if (gpios->irq[i])
  194. devm_free_irq(gpios->port->dev, gpios->irq[i], gpios);
  195. if (gpios->gpio[i])
  196. devm_gpiod_put(dev, gpios->gpio[i]);
  197. }
  198. devm_kfree(dev, gpios);
  199. }
  200. EXPORT_SYMBOL_GPL(mctrl_gpio_free);
  201. void mctrl_gpio_enable_ms(struct mctrl_gpios *gpios)
  202. {
  203. enum mctrl_gpio_idx i;
  204. if (gpios == NULL)
  205. return;
  206. /* .enable_ms may be called multiple times */
  207. if (gpios->mctrl_on)
  208. return;
  209. gpios->mctrl_on = true;
  210. /* get initial status of modem lines GPIOs */
  211. mctrl_gpio_get(gpios, &gpios->mctrl_prev);
  212. for (i = 0; i < UART_GPIO_MAX; ++i) {
  213. if (!gpios->irq[i])
  214. continue;
  215. enable_irq(gpios->irq[i]);
  216. }
  217. }
  218. EXPORT_SYMBOL_GPL(mctrl_gpio_enable_ms);
  219. void mctrl_gpio_disable_ms(struct mctrl_gpios *gpios)
  220. {
  221. enum mctrl_gpio_idx i;
  222. if (gpios == NULL)
  223. return;
  224. if (!gpios->mctrl_on)
  225. return;
  226. gpios->mctrl_on = false;
  227. for (i = 0; i < UART_GPIO_MAX; ++i) {
  228. if (!gpios->irq[i])
  229. continue;
  230. disable_irq(gpios->irq[i]);
  231. }
  232. }
  233. EXPORT_SYMBOL_GPL(mctrl_gpio_disable_ms);
  234. MODULE_LICENSE("GPL");