serial_mctrl_gpio.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. for (i = 0; i < UART_GPIO_MAX; i++)
  46. if (!IS_ERR_OR_NULL(gpios->gpio[i]) &&
  47. mctrl_gpios_desc[i].dir_out) {
  48. desc_array[count] = gpios->gpio[i];
  49. value_array[count] = !!(mctrl & mctrl_gpios_desc[i].mctrl);
  50. count++;
  51. }
  52. gpiod_set_array_value(count, desc_array, value_array);
  53. }
  54. EXPORT_SYMBOL_GPL(mctrl_gpio_set);
  55. struct gpio_desc *mctrl_gpio_to_gpiod(struct mctrl_gpios *gpios,
  56. enum mctrl_gpio_idx gidx)
  57. {
  58. return gpios->gpio[gidx];
  59. }
  60. EXPORT_SYMBOL_GPL(mctrl_gpio_to_gpiod);
  61. unsigned int mctrl_gpio_get(struct mctrl_gpios *gpios, unsigned int *mctrl)
  62. {
  63. enum mctrl_gpio_idx i;
  64. for (i = 0; i < UART_GPIO_MAX; i++) {
  65. if (gpios->gpio[i] && !mctrl_gpios_desc[i].dir_out) {
  66. if (gpiod_get_value(gpios->gpio[i]))
  67. *mctrl |= mctrl_gpios_desc[i].mctrl;
  68. else
  69. *mctrl &= ~mctrl_gpios_desc[i].mctrl;
  70. }
  71. }
  72. return *mctrl;
  73. }
  74. EXPORT_SYMBOL_GPL(mctrl_gpio_get);
  75. struct mctrl_gpios *mctrl_gpio_init(struct device *dev, unsigned int idx)
  76. {
  77. struct mctrl_gpios *gpios;
  78. enum mctrl_gpio_idx i;
  79. gpios = devm_kzalloc(dev, sizeof(*gpios), GFP_KERNEL);
  80. if (!gpios)
  81. return ERR_PTR(-ENOMEM);
  82. for (i = 0; i < UART_GPIO_MAX; i++) {
  83. enum gpiod_flags flags;
  84. if (mctrl_gpios_desc[i].dir_out)
  85. flags = GPIOD_OUT_LOW;
  86. else
  87. flags = GPIOD_IN;
  88. gpios->gpio[i] =
  89. devm_gpiod_get_index_optional(dev,
  90. mctrl_gpios_desc[i].name,
  91. idx, flags);
  92. if (IS_ERR(gpios->gpio[i]))
  93. return ERR_CAST(gpios->gpio[i]);
  94. }
  95. return gpios;
  96. }
  97. EXPORT_SYMBOL_GPL(mctrl_gpio_init);
  98. void mctrl_gpio_free(struct device *dev, struct mctrl_gpios *gpios)
  99. {
  100. enum mctrl_gpio_idx i;
  101. for (i = 0; i < UART_GPIO_MAX; i++)
  102. if (!IS_ERR_OR_NULL(gpios->gpio[i]))
  103. devm_gpiod_put(dev, gpios->gpio[i]);
  104. devm_kfree(dev, gpios);
  105. }
  106. EXPORT_SYMBOL_GPL(mctrl_gpio_free);