gpio-latch-mikrotik.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * GPIO latch driver
  4. *
  5. * Copyright (C) 2014 Gabor Juhos <[email protected]>
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/init.h>
  9. #include <linux/module.h>
  10. #include <linux/types.h>
  11. #include <linux/gpio/consumer.h>
  12. #include <linux/gpio/driver.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/of_gpio.h>
  16. #define GPIO_LATCH_DRIVER_NAME "gpio-latch-mikrotik"
  17. #define GPIO_LATCH_LINES 9
  18. struct gpio_latch_chip {
  19. struct gpio_chip gc;
  20. struct mutex mutex;
  21. struct mutex latch_mutex;
  22. bool latch_enabled;
  23. int le_gpio;
  24. bool le_active_low;
  25. struct gpio_desc *gpios[GPIO_LATCH_LINES];
  26. };
  27. static void gpio_latch_lock(struct gpio_latch_chip *glc, bool enable)
  28. {
  29. mutex_lock(&glc->mutex);
  30. if (enable)
  31. glc->latch_enabled = true;
  32. if (glc->latch_enabled)
  33. mutex_lock(&glc->latch_mutex);
  34. }
  35. static void gpio_latch_unlock(struct gpio_latch_chip *glc, bool disable)
  36. {
  37. if (glc->latch_enabled)
  38. mutex_unlock(&glc->latch_mutex);
  39. if (disable)
  40. glc->latch_enabled = true;
  41. mutex_unlock(&glc->mutex);
  42. }
  43. static int
  44. gpio_latch_get(struct gpio_chip *gc, unsigned offset)
  45. {
  46. struct gpio_latch_chip *glc = gpiochip_get_data(gc);
  47. int ret;
  48. gpio_latch_lock(glc, false);
  49. ret = gpiod_get_raw_value_cansleep(glc->gpios[offset]);
  50. gpio_latch_unlock(glc, false);
  51. return ret;
  52. }
  53. static void
  54. gpio_latch_set(struct gpio_chip *gc, unsigned offset, int value)
  55. {
  56. struct gpio_latch_chip *glc = gpiochip_get_data(gc);
  57. bool enable_latch = false;
  58. bool disable_latch = false;
  59. if (offset == glc->le_gpio) {
  60. enable_latch = value ^ glc->le_active_low;
  61. disable_latch = !enable_latch;
  62. }
  63. gpio_latch_lock(glc, enable_latch);
  64. gpiod_set_raw_value_cansleep(glc->gpios[offset], value);
  65. gpio_latch_unlock(glc, disable_latch);
  66. }
  67. static int
  68. gpio_latch_direction_output(struct gpio_chip *gc, unsigned offset, int value)
  69. {
  70. struct gpio_latch_chip *glc = gpiochip_get_data(gc);
  71. bool enable_latch = false;
  72. bool disable_latch = false;
  73. int ret;
  74. if (offset == glc->le_gpio) {
  75. enable_latch = value ^ glc->le_active_low;
  76. disable_latch = !enable_latch;
  77. }
  78. gpio_latch_lock(glc, enable_latch);
  79. ret = gpiod_direction_output_raw(glc->gpios[offset], value);
  80. gpio_latch_unlock(glc, disable_latch);
  81. return ret;
  82. }
  83. static int gpio_latch_probe(struct platform_device *pdev)
  84. {
  85. struct gpio_latch_chip *glc;
  86. struct gpio_chip *gc;
  87. struct device *dev = &pdev->dev;
  88. int err, i, n;
  89. glc = devm_kzalloc(dev, sizeof(*glc), GFP_KERNEL);
  90. if (!glc)
  91. return -ENOMEM;
  92. err = devm_mutex_init(&pdev->dev, &glc->mutex);
  93. if (err)
  94. return err;
  95. err = devm_mutex_init(&pdev->dev, &glc->latch_mutex);
  96. if (err)
  97. return err;
  98. n = gpiod_count(dev, NULL);
  99. if (n <= 0)
  100. return dev_err_probe(dev, n, "failed to get gpios");
  101. if (n != GPIO_LATCH_LINES) {
  102. dev_err(dev, "expected %d gpios", GPIO_LATCH_LINES);
  103. return -EINVAL;
  104. }
  105. for (i = 0; i < n; i++) {
  106. glc->gpios[i] = devm_gpiod_get_index_optional(dev, NULL, i,
  107. GPIOD_OUT_LOW);
  108. if (IS_ERR(glc->gpios[i]))
  109. return dev_err_probe(dev, PTR_ERR(glc->gpios[i]),
  110. "failed to get gpio %d", i);
  111. }
  112. glc->le_gpio = 8;
  113. glc->le_active_low = gpiod_is_active_low(glc->gpios[glc->le_gpio]);
  114. if (!glc->gpios[glc->le_gpio]) {
  115. dev_err(dev, "missing required latch-enable gpio %d\n",
  116. glc->le_gpio);
  117. return -EINVAL;
  118. }
  119. gc = &glc->gc;
  120. gc->label = GPIO_LATCH_DRIVER_NAME;
  121. gc->parent = dev;
  122. gc->can_sleep = true;
  123. gc->base = -1;
  124. gc->ngpio = GPIO_LATCH_LINES;
  125. gc->get = gpio_latch_get;
  126. gc->set = gpio_latch_set;
  127. gc->direction_output = gpio_latch_direction_output;
  128. return devm_gpiochip_add_data(dev, gc, glc);
  129. }
  130. static const struct of_device_id gpio_latch_match[] = {
  131. { .compatible = GPIO_LATCH_DRIVER_NAME },
  132. {},
  133. };
  134. MODULE_DEVICE_TABLE(of, gpio_latch_match);
  135. static struct platform_driver gpio_latch_driver = {
  136. .probe = gpio_latch_probe,
  137. .driver = {
  138. .name = GPIO_LATCH_DRIVER_NAME,
  139. .of_match_table = gpio_latch_match,
  140. },
  141. };
  142. module_platform_driver(gpio_latch_driver);
  143. MODULE_DESCRIPTION("GPIO latch driver");
  144. MODULE_AUTHOR("Gabor Juhos <[email protected]>");
  145. MODULE_AUTHOR("Denis Kalashnikov <[email protected]>");
  146. MODULE_LICENSE("GPL v2");
  147. MODULE_ALIAS("platform:" GPIO_LATCH_DRIVER_NAME);