serial_mctrl_gpio.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 <uapi/asm-generic/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. if (IS_ERR_OR_NULL(gpios))
  43. return;
  44. for (i = 0; i < UART_GPIO_MAX; i++)
  45. if (!IS_ERR_OR_NULL(gpios->gpio[i]) &&
  46. mctrl_gpios_desc[i].dir_out)
  47. gpiod_set_value(gpios->gpio[i],
  48. !!(mctrl & mctrl_gpios_desc[i].mctrl));
  49. }
  50. EXPORT_SYMBOL_GPL(mctrl_gpio_set);
  51. struct gpio_desc *mctrl_gpio_to_gpiod(struct mctrl_gpios *gpios,
  52. enum mctrl_gpio_idx gidx)
  53. {
  54. if (!IS_ERR_OR_NULL(gpios) && !IS_ERR_OR_NULL(gpios->gpio[gidx]))
  55. return gpios->gpio[gidx];
  56. else
  57. return NULL;
  58. }
  59. EXPORT_SYMBOL_GPL(mctrl_gpio_to_gpiod);
  60. unsigned int mctrl_gpio_get(struct mctrl_gpios *gpios, unsigned int *mctrl)
  61. {
  62. enum mctrl_gpio_idx i;
  63. /*
  64. * return it unchanged if the structure is not allocated
  65. */
  66. if (IS_ERR_OR_NULL(gpios))
  67. return *mctrl;
  68. for (i = 0; i < UART_GPIO_MAX; i++) {
  69. if (!IS_ERR_OR_NULL(gpios->gpio[i]) &&
  70. !mctrl_gpios_desc[i].dir_out) {
  71. if (gpiod_get_value(gpios->gpio[i]))
  72. *mctrl |= mctrl_gpios_desc[i].mctrl;
  73. else
  74. *mctrl &= ~mctrl_gpios_desc[i].mctrl;
  75. }
  76. }
  77. return *mctrl;
  78. }
  79. EXPORT_SYMBOL_GPL(mctrl_gpio_get);
  80. struct mctrl_gpios *mctrl_gpio_init(struct device *dev, unsigned int idx)
  81. {
  82. struct mctrl_gpios *gpios;
  83. enum mctrl_gpio_idx i;
  84. int err;
  85. gpios = devm_kzalloc(dev, sizeof(*gpios), GFP_KERNEL);
  86. if (!gpios)
  87. return ERR_PTR(-ENOMEM);
  88. for (i = 0; i < UART_GPIO_MAX; i++) {
  89. gpios->gpio[i] = devm_gpiod_get_index(dev,
  90. mctrl_gpios_desc[i].name,
  91. idx);
  92. /*
  93. * The GPIOs are maybe not all filled,
  94. * this is not an error.
  95. */
  96. if (IS_ERR_OR_NULL(gpios->gpio[i]))
  97. continue;
  98. if (mctrl_gpios_desc[i].dir_out)
  99. err = gpiod_direction_output(gpios->gpio[i], 0);
  100. else
  101. err = gpiod_direction_input(gpios->gpio[i]);
  102. if (err) {
  103. dev_dbg(dev, "Unable to set direction for %s GPIO",
  104. mctrl_gpios_desc[i].name);
  105. devm_gpiod_put(dev, gpios->gpio[i]);
  106. gpios->gpio[i] = NULL;
  107. }
  108. }
  109. return gpios;
  110. }
  111. EXPORT_SYMBOL_GPL(mctrl_gpio_init);
  112. void mctrl_gpio_free(struct device *dev, struct mctrl_gpios *gpios)
  113. {
  114. enum mctrl_gpio_idx i;
  115. if (IS_ERR_OR_NULL(gpios))
  116. return;
  117. for (i = 0; i < UART_GPIO_MAX; i++)
  118. if (!IS_ERR_OR_NULL(gpios->gpio[i]))
  119. devm_gpiod_put(dev, gpios->gpio[i]);
  120. devm_kfree(dev, gpios);
  121. }
  122. EXPORT_SYMBOL_GPL(mctrl_gpio_free);