gpio-nxp-74hc153.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * NXP 74HC153 - Dual 4-input multiplexer GPIO driver
  3. *
  4. * Copyright (C) 2010 Gabor Juhos <[email protected]>
  5. * Copyright (C) 2020 Mauri Sandberg <[email protected]>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * Example device tree definition:
  12. *
  13. * gpio-extender {
  14. * compatible = "nxp,74hc153-gpio";
  15. * gpio-controller;
  16. * #gpio-cells = <2>;
  17. *
  18. * // GPIOs used by this node
  19. * gpio-s0 = <&gpio 9 GPIO_ACTIVE_HIGH>;
  20. * gpio-s1 = <&gpio 11 GPIO_ACTIVE_HIGH>;
  21. * gpio-1y = <&gpio 12 GPIO_ACTIVE_HIGH>;
  22. * gpio-2y = <&gpio 14 GPIO_ACTIVE_HIGH>;
  23. * };
  24. *
  25. */
  26. #include <linux/version.h>
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/gpio.h>
  30. #include <linux/slab.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/of_gpio.h>
  33. #define NXP_74HC153_NUM_GPIOS 8
  34. #define NXP_74HC153_S0_MASK 0x1
  35. #define NXP_74HC153_S1_MASK 0x2
  36. #define NXP_74HC153_BANK_MASK 0x4
  37. #define NXP_74HC153_DRIVER_NAME "nxp-74hc153"
  38. struct nxp_74hc153_config {
  39. unsigned gpio_pin_s0;
  40. unsigned gpio_pin_s1;
  41. unsigned gpio_pin_1y;
  42. unsigned gpio_pin_2y;
  43. };
  44. struct nxp_74hc153_chip {
  45. struct device *parent;
  46. struct gpio_chip gpio_chip;
  47. struct mutex lock;
  48. struct nxp_74hc153_config config;
  49. };
  50. static struct nxp_74hc153_chip *gpio_to_nxp(struct gpio_chip *gc)
  51. {
  52. return container_of(gc, struct nxp_74hc153_chip, gpio_chip);
  53. }
  54. static int nxp_74hc153_direction_input(struct gpio_chip *gc, unsigned offset)
  55. {
  56. return 0;
  57. }
  58. static int nxp_74hc153_direction_output(struct gpio_chip *gc,
  59. unsigned offset, int val)
  60. {
  61. return -EINVAL;
  62. }
  63. static int nxp_74hc153_get_value(struct gpio_chip *gc, unsigned offset)
  64. {
  65. struct nxp_74hc153_chip *nxp;
  66. struct nxp_74hc153_platform_data *pdata;
  67. unsigned s0;
  68. unsigned s1;
  69. unsigned pin;
  70. int ret;
  71. nxp = gpio_to_nxp(gc);
  72. pdata = nxp->parent->platform_data;
  73. s0 = !!(offset & NXP_74HC153_S0_MASK);
  74. s1 = !!(offset & NXP_74HC153_S1_MASK);
  75. pin = (offset & NXP_74HC153_BANK_MASK) ? nxp->config.gpio_pin_2y
  76. : nxp->config.gpio_pin_1y;
  77. mutex_lock(&nxp->lock);
  78. gpio_set_value(nxp->config.gpio_pin_s0, s0);
  79. gpio_set_value(nxp->config.gpio_pin_s1, s1);
  80. ret = gpio_get_value(pin);
  81. mutex_unlock(&nxp->lock);
  82. return ret;
  83. }
  84. static void nxp_74hc153_set_value(struct gpio_chip *gc,
  85. unsigned offset, int val)
  86. {
  87. /* not supported */
  88. }
  89. static int nxp_74hc153_probe(struct platform_device *pdev)
  90. {
  91. struct device_node *np = pdev->dev.of_node;
  92. struct nxp_74hc153_chip *nxp;
  93. struct gpio_chip *gc;
  94. int err;
  95. unsigned gpio_s0;
  96. unsigned gpio_s1;
  97. unsigned gpio_1y;
  98. unsigned gpio_2y;
  99. nxp = kzalloc(sizeof(struct nxp_74hc153_chip), GFP_KERNEL);
  100. if (nxp == NULL) {
  101. dev_err(&pdev->dev, "no memory for private data\n");
  102. return -ENOMEM;
  103. }
  104. gpio_s0 = of_get_named_gpio(np, "gpio-s0", 0);
  105. gpio_s1 = of_get_named_gpio(np, "gpio-s1", 0);
  106. gpio_1y = of_get_named_gpio(np, "gpio-1y", 0);
  107. gpio_2y = of_get_named_gpio(np, "gpio-2y", 0);
  108. if (!gpio_is_valid(gpio_s0) || !gpio_is_valid(gpio_s1) ||
  109. !gpio_is_valid(gpio_1y) || !gpio_is_valid(gpio_2y)) {
  110. dev_err(&pdev->dev, "control GPIO(s) are missing\n");
  111. err = -EINVAL;
  112. goto err_free_nxp;
  113. } else {
  114. nxp->config.gpio_pin_s0 = gpio_s0;
  115. nxp->config.gpio_pin_s1 = gpio_s1;
  116. nxp->config.gpio_pin_1y = gpio_1y;
  117. nxp->config.gpio_pin_2y = gpio_2y;
  118. }
  119. // apply pin configuration
  120. err = gpio_request(nxp->config.gpio_pin_s0, dev_name(&pdev->dev));
  121. if (err) {
  122. dev_err(&pdev->dev, "unable to claim gpio %u, err=%d\n",
  123. nxp->config.gpio_pin_s0, err);
  124. goto err_free_nxp;
  125. }
  126. err = gpio_request(nxp->config.gpio_pin_s1, dev_name(&pdev->dev));
  127. if (err) {
  128. dev_err(&pdev->dev, "unable to claim gpio %u, err=%d\n",
  129. nxp->config.gpio_pin_s1, err);
  130. goto err_free_s0;
  131. }
  132. err = gpio_request(nxp->config.gpio_pin_1y, dev_name(&pdev->dev));
  133. if (err) {
  134. dev_err(&pdev->dev, "unable to claim gpio %u, err=%d\n",
  135. nxp->config.gpio_pin_1y, err);
  136. goto err_free_s1;
  137. }
  138. err = gpio_request(nxp->config.gpio_pin_2y, dev_name(&pdev->dev));
  139. if (err) {
  140. dev_err(&pdev->dev, "unable to claim gpio %u, err=%d\n",
  141. nxp->config.gpio_pin_2y, err);
  142. goto err_free_1y;
  143. }
  144. err = gpio_direction_output(nxp->config.gpio_pin_s0, 0);
  145. if (err) {
  146. dev_err(&pdev->dev,
  147. "unable to set direction of gpio %u, err=%d\n",
  148. nxp->config.gpio_pin_s0, err);
  149. goto err_free_2y;
  150. }
  151. err = gpio_direction_output(nxp->config.gpio_pin_s1, 0);
  152. if (err) {
  153. dev_err(&pdev->dev,
  154. "unable to set direction of gpio %u, err=%d\n",
  155. nxp->config.gpio_pin_s1, err);
  156. goto err_free_2y;
  157. }
  158. err = gpio_direction_input(nxp->config.gpio_pin_1y);
  159. if (err) {
  160. dev_err(&pdev->dev,
  161. "unable to set direction of gpio %u, err=%d\n",
  162. nxp->config.gpio_pin_1y, err);
  163. goto err_free_2y;
  164. }
  165. err = gpio_direction_input(nxp->config.gpio_pin_2y);
  166. if (err) {
  167. dev_err(&pdev->dev,
  168. "unable to set direction of gpio %u, err=%d\n",
  169. nxp->config.gpio_pin_2y, err);
  170. goto err_free_2y;
  171. }
  172. nxp->parent = &pdev->dev;
  173. mutex_init(&nxp->lock);
  174. gc = &nxp->gpio_chip;
  175. gc->direction_input = nxp_74hc153_direction_input;
  176. gc->direction_output = nxp_74hc153_direction_output;
  177. gc->get = nxp_74hc153_get_value;
  178. gc->set = nxp_74hc153_set_value;
  179. gc->can_sleep = 1;
  180. gc->base = -1;
  181. gc->ngpio = NXP_74HC153_NUM_GPIOS;
  182. gc->label = dev_name(nxp->parent);
  183. gc->parent = nxp->parent;
  184. gc->owner = THIS_MODULE;
  185. gc->of_node = np;
  186. err = gpiochip_add(&nxp->gpio_chip);
  187. if (err) {
  188. dev_err(&pdev->dev, "unable to add gpio chip, err=%d\n", err);
  189. goto err_free_2y;
  190. }
  191. platform_set_drvdata(pdev, nxp);
  192. return 0;
  193. err_free_2y:
  194. gpio_free(nxp->config.gpio_pin_2y);
  195. err_free_1y:
  196. gpio_free(nxp->config.gpio_pin_1y);
  197. err_free_s1:
  198. gpio_free(nxp->config.gpio_pin_s1);
  199. err_free_s0:
  200. gpio_free(nxp->config.gpio_pin_s0);
  201. err_free_nxp:
  202. kfree(nxp);
  203. return err;
  204. }
  205. static int nxp_74hc153_remove(struct platform_device *pdev)
  206. {
  207. struct nxp_74hc153_chip *nxp = platform_get_drvdata(pdev);
  208. if (nxp) {
  209. gpiochip_remove(&nxp->gpio_chip);
  210. gpio_free(nxp->config.gpio_pin_2y);
  211. gpio_free(nxp->config.gpio_pin_1y);
  212. gpio_free(nxp->config.gpio_pin_s1);
  213. gpio_free(nxp->config.gpio_pin_s0);
  214. kfree(nxp);
  215. platform_set_drvdata(pdev, NULL);
  216. }
  217. return 0;
  218. }
  219. static struct of_device_id nxp_74hc153_id[] = {
  220. {
  221. .compatible = "nxp,74hc153-gpio",
  222. .data = NULL,
  223. }, { /* sentinel */ }
  224. };
  225. MODULE_DEVICE_TABLE(of, nxp_74hc153_id);
  226. static struct platform_driver nxp_74hc153_driver = {
  227. .probe = nxp_74hc153_probe,
  228. .remove = nxp_74hc153_remove,
  229. .driver = {
  230. .name = NXP_74HC153_DRIVER_NAME,
  231. .owner = THIS_MODULE,
  232. .of_match_table = nxp_74hc153_id,
  233. },
  234. };
  235. static int __init nxp_74hc153_init(void)
  236. {
  237. return platform_driver_register(&nxp_74hc153_driver);
  238. }
  239. subsys_initcall(nxp_74hc153_init);
  240. static void __exit nxp_74hc153_exit(void)
  241. {
  242. platform_driver_unregister(&nxp_74hc153_driver);
  243. }
  244. module_exit(nxp_74hc153_exit);
  245. MODULE_AUTHOR("Gabor Juhos <[email protected]>");
  246. MODULE_DESCRIPTION("GPIO expander driver for NXP 74HC153");
  247. MODULE_LICENSE("GPL v2");
  248. MODULE_ALIAS("platform:" NXP_74HC153_DRIVER_NAME);