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