gpio-shared-proxy.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2025 Linaro Ltd.
  4. */
  5. #include <linux/auxiliary_bus.h>
  6. #include <linux/cleanup.h>
  7. #include <linux/device.h>
  8. #include <linux/err.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/gpio/driver.h>
  11. #include <linux/mod_devicetable.h>
  12. #include <linux/module.h>
  13. #include <linux/string_choices.h>
  14. #include <linux/types.h>
  15. #include "gpiolib-shared.h"
  16. struct gpio_shared_proxy_data {
  17. struct gpio_chip gc;
  18. struct gpio_shared_desc *shared_desc;
  19. struct device *dev;
  20. bool voted_high;
  21. };
  22. static int
  23. gpio_shared_proxy_set_unlocked(struct gpio_shared_proxy_data *proxy,
  24. int (*set_func)(struct gpio_desc *desc, int value),
  25. int value)
  26. {
  27. struct gpio_shared_desc *shared_desc = proxy->shared_desc;
  28. struct gpio_desc *desc = shared_desc->desc;
  29. int ret = 0;
  30. gpio_shared_lockdep_assert(shared_desc);
  31. if (value) {
  32. /* User wants to set value to high. */
  33. if (proxy->voted_high)
  34. /* Already voted for high, nothing to do. */
  35. goto out;
  36. /* Haven't voted for high yet. */
  37. if (!shared_desc->highcnt) {
  38. /*
  39. * Current value is low, need to actually set value
  40. * to high.
  41. */
  42. ret = set_func(desc, 1);
  43. if (ret)
  44. goto out;
  45. }
  46. shared_desc->highcnt++;
  47. proxy->voted_high = true;
  48. goto out;
  49. }
  50. /* Desired value is low. */
  51. if (!proxy->voted_high)
  52. /* We didn't vote for high, nothing to do. */
  53. goto out;
  54. /* We previously voted for high. */
  55. if (shared_desc->highcnt == 1) {
  56. /* This is the last remaining vote for high, set value to low. */
  57. ret = set_func(desc, 0);
  58. if (ret)
  59. goto out;
  60. }
  61. shared_desc->highcnt--;
  62. proxy->voted_high = false;
  63. out:
  64. if (shared_desc->highcnt)
  65. dev_dbg(proxy->dev,
  66. "Voted for value '%s', effective value is 'high', number of votes for 'high': %u\n",
  67. str_high_low(value), shared_desc->highcnt);
  68. else
  69. dev_dbg(proxy->dev, "Voted for value 'low', effective value is 'low'\n");
  70. return ret;
  71. }
  72. static int gpio_shared_proxy_request(struct gpio_chip *gc, unsigned int offset)
  73. {
  74. struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
  75. struct gpio_shared_desc *shared_desc = proxy->shared_desc;
  76. guard(gpio_shared_desc_lock)(shared_desc);
  77. proxy->shared_desc->usecnt++;
  78. dev_dbg(proxy->dev, "Shared GPIO requested, number of users: %u\n",
  79. proxy->shared_desc->usecnt);
  80. return 0;
  81. }
  82. static void gpio_shared_proxy_free(struct gpio_chip *gc, unsigned int offset)
  83. {
  84. struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
  85. struct gpio_shared_desc *shared_desc = proxy->shared_desc;
  86. guard(gpio_shared_desc_lock)(shared_desc);
  87. proxy->shared_desc->usecnt--;
  88. dev_dbg(proxy->dev, "Shared GPIO freed, number of users: %u\n",
  89. proxy->shared_desc->usecnt);
  90. }
  91. static int gpio_shared_proxy_set_config(struct gpio_chip *gc,
  92. unsigned int offset, unsigned long cfg)
  93. {
  94. struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
  95. struct gpio_shared_desc *shared_desc = proxy->shared_desc;
  96. struct gpio_desc *desc = shared_desc->desc;
  97. int ret;
  98. guard(gpio_shared_desc_lock)(shared_desc);
  99. if (shared_desc->usecnt > 1) {
  100. if (shared_desc->cfg != cfg) {
  101. dev_dbg(proxy->dev,
  102. "Shared GPIO's configuration already set, accepting changes but users may conflict!!\n");
  103. } else {
  104. dev_dbg(proxy->dev, "Equal config requested, nothing to do\n");
  105. return 0;
  106. }
  107. }
  108. ret = gpiod_set_config(desc, cfg);
  109. if (ret && ret != -ENOTSUPP)
  110. return ret;
  111. shared_desc->cfg = cfg;
  112. return 0;
  113. }
  114. static int gpio_shared_proxy_direction_input(struct gpio_chip *gc,
  115. unsigned int offset)
  116. {
  117. struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
  118. struct gpio_shared_desc *shared_desc = proxy->shared_desc;
  119. struct gpio_desc *desc = shared_desc->desc;
  120. int dir;
  121. guard(gpio_shared_desc_lock)(shared_desc);
  122. if (shared_desc->usecnt == 1) {
  123. dev_dbg(proxy->dev,
  124. "Only one user of this shared GPIO, allowing to set direction to input\n");
  125. return gpiod_direction_input(desc);
  126. }
  127. dir = gpiod_get_direction(desc);
  128. if (dir < 0)
  129. return dir;
  130. if (dir == GPIO_LINE_DIRECTION_OUT) {
  131. dev_dbg(proxy->dev,
  132. "Shared GPIO's direction already set to output, refusing to change\n");
  133. return -EPERM;
  134. }
  135. return 0;
  136. }
  137. static int gpio_shared_proxy_direction_output(struct gpio_chip *gc,
  138. unsigned int offset, int value)
  139. {
  140. struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
  141. struct gpio_shared_desc *shared_desc = proxy->shared_desc;
  142. struct gpio_desc *desc = shared_desc->desc;
  143. int ret, dir;
  144. guard(gpio_shared_desc_lock)(shared_desc);
  145. if (shared_desc->usecnt == 1) {
  146. dev_dbg(proxy->dev,
  147. "Only one user of this shared GPIO, allowing to set direction to output with value '%s'\n",
  148. str_high_low(value));
  149. ret = gpiod_direction_output(desc, value);
  150. if (ret)
  151. return ret;
  152. if (value) {
  153. proxy->voted_high = true;
  154. shared_desc->highcnt = 1;
  155. } else {
  156. proxy->voted_high = false;
  157. shared_desc->highcnt = 0;
  158. }
  159. return 0;
  160. }
  161. dir = gpiod_get_direction(desc);
  162. if (dir < 0)
  163. return dir;
  164. if (dir == GPIO_LINE_DIRECTION_IN) {
  165. dev_dbg(proxy->dev,
  166. "Shared GPIO's direction already set to input, refusing to change\n");
  167. return -EPERM;
  168. }
  169. return gpio_shared_proxy_set_unlocked(proxy, gpiod_direction_output, value);
  170. }
  171. static int gpio_shared_proxy_get(struct gpio_chip *gc, unsigned int offset)
  172. {
  173. struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
  174. return gpiod_get_value(proxy->shared_desc->desc);
  175. }
  176. static int gpio_shared_proxy_get_cansleep(struct gpio_chip *gc,
  177. unsigned int offset)
  178. {
  179. struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
  180. return gpiod_get_value_cansleep(proxy->shared_desc->desc);
  181. }
  182. static int gpio_shared_proxy_do_set(struct gpio_shared_proxy_data *proxy,
  183. int (*set_func)(struct gpio_desc *desc, int value),
  184. int value)
  185. {
  186. guard(gpio_shared_desc_lock)(proxy->shared_desc);
  187. return gpio_shared_proxy_set_unlocked(proxy, set_func, value);
  188. }
  189. static int gpio_shared_proxy_set(struct gpio_chip *gc, unsigned int offset,
  190. int value)
  191. {
  192. struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
  193. return gpio_shared_proxy_do_set(proxy, gpiod_set_value, value);
  194. }
  195. static int gpio_shared_proxy_set_cansleep(struct gpio_chip *gc,
  196. unsigned int offset, int value)
  197. {
  198. struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
  199. return gpio_shared_proxy_do_set(proxy, gpiod_set_value_cansleep, value);
  200. }
  201. static int gpio_shared_proxy_get_direction(struct gpio_chip *gc,
  202. unsigned int offset)
  203. {
  204. struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
  205. return gpiod_get_direction(proxy->shared_desc->desc);
  206. }
  207. static int gpio_shared_proxy_to_irq(struct gpio_chip *gc, unsigned int offset)
  208. {
  209. struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
  210. return gpiod_to_irq(proxy->shared_desc->desc);
  211. }
  212. static int gpio_shared_proxy_probe(struct auxiliary_device *adev,
  213. const struct auxiliary_device_id *id)
  214. {
  215. struct gpio_shared_proxy_data *proxy;
  216. struct gpio_shared_desc *shared_desc;
  217. struct device *dev = &adev->dev;
  218. struct gpio_chip *gc;
  219. shared_desc = devm_gpiod_shared_get(dev);
  220. if (IS_ERR(shared_desc))
  221. return PTR_ERR(shared_desc);
  222. proxy = devm_kzalloc(dev, sizeof(*proxy), GFP_KERNEL);
  223. if (!proxy)
  224. return -ENOMEM;
  225. proxy->shared_desc = shared_desc;
  226. proxy->dev = dev;
  227. gc = &proxy->gc;
  228. gc->base = -1;
  229. gc->ngpio = 1;
  230. gc->label = dev_name(dev);
  231. gc->parent = dev;
  232. gc->owner = THIS_MODULE;
  233. gc->can_sleep = shared_desc->can_sleep;
  234. gc->request = gpio_shared_proxy_request;
  235. gc->free = gpio_shared_proxy_free;
  236. gc->set_config = gpio_shared_proxy_set_config;
  237. gc->direction_input = gpio_shared_proxy_direction_input;
  238. gc->direction_output = gpio_shared_proxy_direction_output;
  239. if (gc->can_sleep) {
  240. gc->set = gpio_shared_proxy_set_cansleep;
  241. gc->get = gpio_shared_proxy_get_cansleep;
  242. } else {
  243. gc->set = gpio_shared_proxy_set;
  244. gc->get = gpio_shared_proxy_get;
  245. }
  246. gc->get_direction = gpio_shared_proxy_get_direction;
  247. gc->to_irq = gpio_shared_proxy_to_irq;
  248. return devm_gpiochip_add_data(dev, &proxy->gc, proxy);
  249. }
  250. static const struct auxiliary_device_id gpio_shared_proxy_id_table[] = {
  251. { .name = "gpiolib_shared.proxy" },
  252. {},
  253. };
  254. MODULE_DEVICE_TABLE(auxiliary, gpio_shared_proxy_id_table);
  255. static struct auxiliary_driver gpio_shared_proxy_driver = {
  256. .driver = {
  257. .name = "gpio-shared-proxy",
  258. .suppress_bind_attrs = true,
  259. },
  260. .probe = gpio_shared_proxy_probe,
  261. .id_table = gpio_shared_proxy_id_table,
  262. };
  263. module_auxiliary_driver(gpio_shared_proxy_driver);
  264. MODULE_AUTHOR("Bartosz Golaszewski <bartosz.golaszewski@linaro.org>");
  265. MODULE_DESCRIPTION("Shared GPIO mux driver.");
  266. MODULE_LICENSE("GPL");