gpio-nxp-74hc153.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * NXP 74HC153 - Dual 4-input multiplexer GPIO driver
  3. *
  4. * Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
  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 version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/version.h>
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/gpio.h>
  14. #include <linux/slab.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/nxp_74hc153.h>
  17. #define NXP_74HC153_NUM_GPIOS 8
  18. #define NXP_74HC153_S0_MASK 0x1
  19. #define NXP_74HC153_S1_MASK 0x2
  20. #define NXP_74HC153_BANK_MASK 0x4
  21. struct nxp_74hc153_chip {
  22. struct device *parent;
  23. struct gpio_chip gpio_chip;
  24. struct mutex lock;
  25. };
  26. static struct nxp_74hc153_chip *gpio_to_nxp(struct gpio_chip *gc)
  27. {
  28. return container_of(gc, struct nxp_74hc153_chip, gpio_chip);
  29. }
  30. static int nxp_74hc153_direction_input(struct gpio_chip *gc, unsigned offset)
  31. {
  32. return 0;
  33. }
  34. static int nxp_74hc153_direction_output(struct gpio_chip *gc,
  35. unsigned offset, int val)
  36. {
  37. return -EINVAL;
  38. }
  39. static int nxp_74hc153_get_value(struct gpio_chip *gc, unsigned offset)
  40. {
  41. struct nxp_74hc153_chip *nxp;
  42. struct nxp_74hc153_platform_data *pdata;
  43. unsigned s0;
  44. unsigned s1;
  45. unsigned pin;
  46. int ret;
  47. nxp = gpio_to_nxp(gc);
  48. pdata = nxp->parent->platform_data;
  49. s0 = !!(offset & NXP_74HC153_S0_MASK);
  50. s1 = !!(offset & NXP_74HC153_S1_MASK);
  51. pin = (offset & NXP_74HC153_BANK_MASK) ? pdata->gpio_pin_2y
  52. : pdata->gpio_pin_1y;
  53. mutex_lock(&nxp->lock);
  54. gpio_set_value(pdata->gpio_pin_s0, s0);
  55. gpio_set_value(pdata->gpio_pin_s1, s1);
  56. ret = gpio_get_value(pin);
  57. mutex_unlock(&nxp->lock);
  58. return ret;
  59. }
  60. static void nxp_74hc153_set_value(struct gpio_chip *gc,
  61. unsigned offset, int val)
  62. {
  63. /* not supported */
  64. }
  65. static int nxp_74hc153_probe(struct platform_device *pdev)
  66. {
  67. struct nxp_74hc153_platform_data *pdata;
  68. struct nxp_74hc153_chip *nxp;
  69. struct gpio_chip *gc;
  70. int err;
  71. pdata = pdev->dev.platform_data;
  72. if (pdata == NULL) {
  73. dev_dbg(&pdev->dev, "no platform data specified\n");
  74. return -EINVAL;
  75. }
  76. nxp = kzalloc(sizeof(struct nxp_74hc153_chip), GFP_KERNEL);
  77. if (nxp == NULL) {
  78. dev_err(&pdev->dev, "no memory for private data\n");
  79. return -ENOMEM;
  80. }
  81. err = gpio_request(pdata->gpio_pin_s0, dev_name(&pdev->dev));
  82. if (err) {
  83. dev_err(&pdev->dev, "unable to claim gpio %u, err=%d\n",
  84. pdata->gpio_pin_s0, err);
  85. goto err_free_nxp;
  86. }
  87. err = gpio_request(pdata->gpio_pin_s1, dev_name(&pdev->dev));
  88. if (err) {
  89. dev_err(&pdev->dev, "unable to claim gpio %u, err=%d\n",
  90. pdata->gpio_pin_s1, err);
  91. goto err_free_s0;
  92. }
  93. err = gpio_request(pdata->gpio_pin_1y, dev_name(&pdev->dev));
  94. if (err) {
  95. dev_err(&pdev->dev, "unable to claim gpio %u, err=%d\n",
  96. pdata->gpio_pin_1y, err);
  97. goto err_free_s1;
  98. }
  99. err = gpio_request(pdata->gpio_pin_2y, dev_name(&pdev->dev));
  100. if (err) {
  101. dev_err(&pdev->dev, "unable to claim gpio %u, err=%d\n",
  102. pdata->gpio_pin_2y, err);
  103. goto err_free_1y;
  104. }
  105. err = gpio_direction_output(pdata->gpio_pin_s0, 0);
  106. if (err) {
  107. dev_err(&pdev->dev,
  108. "unable to set direction of gpio %u, err=%d\n",
  109. pdata->gpio_pin_s0, err);
  110. goto err_free_2y;
  111. }
  112. err = gpio_direction_output(pdata->gpio_pin_s1, 0);
  113. if (err) {
  114. dev_err(&pdev->dev,
  115. "unable to set direction of gpio %u, err=%d\n",
  116. pdata->gpio_pin_s1, err);
  117. goto err_free_2y;
  118. }
  119. err = gpio_direction_input(pdata->gpio_pin_1y);
  120. if (err) {
  121. dev_err(&pdev->dev,
  122. "unable to set direction of gpio %u, err=%d\n",
  123. pdata->gpio_pin_1y, err);
  124. goto err_free_2y;
  125. }
  126. err = gpio_direction_input(pdata->gpio_pin_2y);
  127. if (err) {
  128. dev_err(&pdev->dev,
  129. "unable to set direction of gpio %u, err=%d\n",
  130. pdata->gpio_pin_2y, err);
  131. goto err_free_2y;
  132. }
  133. nxp->parent = &pdev->dev;
  134. mutex_init(&nxp->lock);
  135. gc = &nxp->gpio_chip;
  136. gc->direction_input = nxp_74hc153_direction_input;
  137. gc->direction_output = nxp_74hc153_direction_output;
  138. gc->get = nxp_74hc153_get_value;
  139. gc->set = nxp_74hc153_set_value;
  140. gc->can_sleep = 1;
  141. gc->base = pdata->gpio_base;
  142. gc->ngpio = NXP_74HC153_NUM_GPIOS;
  143. gc->label = dev_name(nxp->parent);
  144. gc->dev = nxp->parent;
  145. gc->owner = THIS_MODULE;
  146. err = gpiochip_add(&nxp->gpio_chip);
  147. if (err) {
  148. dev_err(&pdev->dev, "unable to add gpio chip, err=%d\n", err);
  149. goto err_free_2y;
  150. }
  151. platform_set_drvdata(pdev, nxp);
  152. return 0;
  153. err_free_2y:
  154. gpio_free(pdata->gpio_pin_2y);
  155. err_free_1y:
  156. gpio_free(pdata->gpio_pin_1y);
  157. err_free_s1:
  158. gpio_free(pdata->gpio_pin_s1);
  159. err_free_s0:
  160. gpio_free(pdata->gpio_pin_s0);
  161. err_free_nxp:
  162. kfree(nxp);
  163. return err;
  164. }
  165. static int nxp_74hc153_remove(struct platform_device *pdev)
  166. {
  167. struct nxp_74hc153_chip *nxp = platform_get_drvdata(pdev);
  168. struct nxp_74hc153_platform_data *pdata = pdev->dev.platform_data;
  169. if (nxp) {
  170. #if LINUX_VERSION_CODE < KERNEL_VERSION(3,15,0)
  171. int err;
  172. err = gpiochip_remove(&nxp->gpio_chip);
  173. if (err) {
  174. dev_err(&pdev->dev,
  175. "unable to remove gpio chip, err=%d\n",
  176. err);
  177. return err;
  178. }
  179. #else
  180. gpiochip_remove(&nxp->gpio_chip);
  181. #endif
  182. gpio_free(pdata->gpio_pin_2y);
  183. gpio_free(pdata->gpio_pin_1y);
  184. gpio_free(pdata->gpio_pin_s1);
  185. gpio_free(pdata->gpio_pin_s0);
  186. kfree(nxp);
  187. platform_set_drvdata(pdev, NULL);
  188. }
  189. return 0;
  190. }
  191. static struct platform_driver nxp_74hc153_driver = {
  192. .probe = nxp_74hc153_probe,
  193. .remove = nxp_74hc153_remove,
  194. .driver = {
  195. .name = NXP_74HC153_DRIVER_NAME,
  196. .owner = THIS_MODULE,
  197. },
  198. };
  199. static int __init nxp_74hc153_init(void)
  200. {
  201. return platform_driver_register(&nxp_74hc153_driver);
  202. }
  203. subsys_initcall(nxp_74hc153_init);
  204. static void __exit nxp_74hc153_exit(void)
  205. {
  206. platform_driver_unregister(&nxp_74hc153_driver);
  207. }
  208. module_exit(nxp_74hc153_exit);
  209. MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
  210. MODULE_DESCRIPTION("GPIO expander driver for NXP 74HC153");
  211. MODULE_LICENSE("GPL v2");
  212. MODULE_ALIAS("platform:" NXP_74HC153_DRIVER_NAME);