serial_mctrl_gpio.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. */
  17. #include <linux/err.h>
  18. #include <linux/device.h>
  19. #include <linux/gpio/consumer.h>
  20. #include <linux/termios.h>
  21. #include "serial_mctrl_gpio.h"
  22. struct mctrl_gpios {
  23. struct gpio_desc *gpio[UART_GPIO_MAX];
  24. };
  25. static const struct {
  26. const char *name;
  27. unsigned int mctrl;
  28. bool dir_out;
  29. } mctrl_gpios_desc[UART_GPIO_MAX] = {
  30. { "cts", TIOCM_CTS, false, },
  31. { "dsr", TIOCM_DSR, false, },
  32. { "dcd", TIOCM_CD, false, },
  33. { "rng", TIOCM_RNG, false, },
  34. { "rts", TIOCM_RTS, true, },
  35. { "dtr", TIOCM_DTR, true, },
  36. { "out1", TIOCM_OUT1, true, },
  37. { "out2", TIOCM_OUT2, true, },
  38. };
  39. void mctrl_gpio_set(struct mctrl_gpios *gpios, unsigned int mctrl)
  40. {
  41. enum mctrl_gpio_idx i;
  42. struct gpio_desc *desc_array[UART_GPIO_MAX];
  43. int value_array[UART_GPIO_MAX];
  44. unsigned int count = 0;
  45. if (IS_ERR_OR_NULL(gpios))
  46. return;
  47. for (i = 0; i < UART_GPIO_MAX; i++)
  48. if (!IS_ERR_OR_NULL(gpios->gpio[i]) &&
  49. mctrl_gpios_desc[i].dir_out) {
  50. desc_array[count] = gpios->gpio[i];
  51. value_array[count] = !!(mctrl & mctrl_gpios_desc[i].mctrl);
  52. count++;
  53. }
  54. gpiod_set_array(count, desc_array, value_array);
  55. }
  56. EXPORT_SYMBOL_GPL(mctrl_gpio_set);
  57. struct gpio_desc *mctrl_gpio_to_gpiod(struct mctrl_gpios *gpios,
  58. enum mctrl_gpio_idx gidx)
  59. {
  60. if (!IS_ERR_OR_NULL(gpios) && !IS_ERR_OR_NULL(gpios->gpio[gidx]))
  61. return gpios->gpio[gidx];
  62. else
  63. return NULL;
  64. }
  65. EXPORT_SYMBOL_GPL(mctrl_gpio_to_gpiod);
  66. unsigned int mctrl_gpio_get(struct mctrl_gpios *gpios, unsigned int *mctrl)
  67. {
  68. enum mctrl_gpio_idx i;
  69. /*
  70. * return it unchanged if the structure is not allocated
  71. */
  72. if (IS_ERR_OR_NULL(gpios))
  73. return *mctrl;
  74. for (i = 0; i < UART_GPIO_MAX; i++) {
  75. if (!IS_ERR_OR_NULL(gpios->gpio[i]) &&
  76. !mctrl_gpios_desc[i].dir_out) {
  77. if (gpiod_get_value(gpios->gpio[i]))
  78. *mctrl |= mctrl_gpios_desc[i].mctrl;
  79. else
  80. *mctrl &= ~mctrl_gpios_desc[i].mctrl;
  81. }
  82. }
  83. return *mctrl;
  84. }
  85. EXPORT_SYMBOL_GPL(mctrl_gpio_get);
  86. struct mctrl_gpios *mctrl_gpio_init(struct device *dev, unsigned int idx)
  87. {
  88. struct mctrl_gpios *gpios;
  89. enum mctrl_gpio_idx i;
  90. int err;
  91. gpios = devm_kzalloc(dev, sizeof(*gpios), GFP_KERNEL);
  92. if (!gpios)
  93. return ERR_PTR(-ENOMEM);
  94. for (i = 0; i < UART_GPIO_MAX; i++) {
  95. gpios->gpio[i] = devm_gpiod_get_index(dev,
  96. mctrl_gpios_desc[i].name,
  97. idx);
  98. /*
  99. * The GPIOs are maybe not all filled,
  100. * this is not an error.
  101. */
  102. if (IS_ERR_OR_NULL(gpios->gpio[i]))
  103. continue;
  104. if (mctrl_gpios_desc[i].dir_out)
  105. err = gpiod_direction_output(gpios->gpio[i], 0);
  106. else
  107. err = gpiod_direction_input(gpios->gpio[i]);
  108. if (err) {
  109. dev_dbg(dev, "Unable to set direction for %s GPIO",
  110. mctrl_gpios_desc[i].name);
  111. devm_gpiod_put(dev, gpios->gpio[i]);
  112. gpios->gpio[i] = NULL;
  113. }
  114. }
  115. return gpios;
  116. }
  117. EXPORT_SYMBOL_GPL(mctrl_gpio_init);
  118. void mctrl_gpio_free(struct device *dev, struct mctrl_gpios *gpios)
  119. {
  120. enum mctrl_gpio_idx i;
  121. if (IS_ERR_OR_NULL(gpios))
  122. return;
  123. for (i = 0; i < UART_GPIO_MAX; i++)
  124. if (!IS_ERR_OR_NULL(gpios->gpio[i]))
  125. devm_gpiod_put(dev, gpios->gpio[i]);
  126. devm_kfree(dev, gpios);
  127. }
  128. EXPORT_SYMBOL_GPL(mctrl_gpio_free);