gpio.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * SuperH Pin Function Controller GPIO driver.
  3. *
  4. * Copyright (C) 2008 Magnus Damm
  5. * Copyright (C) 2009 - 2012 Paul Mundt
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License. See the file "COPYING" in the main directory of this archive
  9. * for more details.
  10. */
  11. #define pr_fmt(fmt) "sh_pfc " KBUILD_MODNAME ": " fmt
  12. #include <linux/init.h>
  13. #include <linux/gpio.h>
  14. #include <linux/slab.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/pinctrl/consumer.h>
  19. struct sh_pfc_chip {
  20. struct sh_pfc *pfc;
  21. struct gpio_chip gpio_chip;
  22. };
  23. static struct sh_pfc_chip *gpio_to_pfc_chip(struct gpio_chip *gc)
  24. {
  25. return container_of(gc, struct sh_pfc_chip, gpio_chip);
  26. }
  27. static struct sh_pfc *gpio_to_pfc(struct gpio_chip *gc)
  28. {
  29. return gpio_to_pfc_chip(gc)->pfc;
  30. }
  31. static int sh_gpio_request(struct gpio_chip *gc, unsigned offset)
  32. {
  33. return pinctrl_request_gpio(offset);
  34. }
  35. static void sh_gpio_free(struct gpio_chip *gc, unsigned offset)
  36. {
  37. pinctrl_free_gpio(offset);
  38. }
  39. static void sh_gpio_set_value(struct sh_pfc *pfc, unsigned gpio, int value)
  40. {
  41. struct pinmux_data_reg *dr = NULL;
  42. int bit = 0;
  43. if (!pfc || sh_pfc_get_data_reg(pfc, gpio, &dr, &bit) != 0)
  44. BUG();
  45. else
  46. sh_pfc_write_bit(dr, bit, value);
  47. }
  48. static int sh_gpio_get_value(struct sh_pfc *pfc, unsigned gpio)
  49. {
  50. struct pinmux_data_reg *dr = NULL;
  51. int bit = 0;
  52. if (!pfc || sh_pfc_get_data_reg(pfc, gpio, &dr, &bit) != 0)
  53. return -EINVAL;
  54. return sh_pfc_read_bit(dr, bit);
  55. }
  56. static int sh_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
  57. {
  58. return pinctrl_gpio_direction_input(offset);
  59. }
  60. static int sh_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
  61. int value)
  62. {
  63. sh_gpio_set_value(gpio_to_pfc(gc), offset, value);
  64. return pinctrl_gpio_direction_output(offset);
  65. }
  66. static int sh_gpio_get(struct gpio_chip *gc, unsigned offset)
  67. {
  68. return sh_gpio_get_value(gpio_to_pfc(gc), offset);
  69. }
  70. static void sh_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
  71. {
  72. sh_gpio_set_value(gpio_to_pfc(gc), offset, value);
  73. }
  74. static int sh_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
  75. {
  76. struct sh_pfc *pfc = gpio_to_pfc(gc);
  77. pinmux_enum_t enum_id;
  78. pinmux_enum_t *enum_ids;
  79. int i, k, pos;
  80. pos = 0;
  81. enum_id = 0;
  82. while (1) {
  83. pos = sh_pfc_gpio_to_enum(pfc, offset, pos, &enum_id);
  84. if (pos <= 0 || !enum_id)
  85. break;
  86. for (i = 0; i < pfc->gpio_irq_size; i++) {
  87. enum_ids = pfc->gpio_irq[i].enum_ids;
  88. for (k = 0; enum_ids[k]; k++) {
  89. if (enum_ids[k] == enum_id)
  90. return pfc->gpio_irq[i].irq;
  91. }
  92. }
  93. }
  94. return -ENOSYS;
  95. }
  96. static void sh_pfc_gpio_setup(struct sh_pfc_chip *chip)
  97. {
  98. struct sh_pfc *pfc = chip->pfc;
  99. struct gpio_chip *gc = &chip->gpio_chip;
  100. gc->request = sh_gpio_request;
  101. gc->free = sh_gpio_free;
  102. gc->direction_input = sh_gpio_direction_input;
  103. gc->get = sh_gpio_get;
  104. gc->direction_output = sh_gpio_direction_output;
  105. gc->set = sh_gpio_set;
  106. gc->to_irq = sh_gpio_to_irq;
  107. WARN_ON(pfc->first_gpio != 0); /* needs testing */
  108. gc->label = pfc->name;
  109. gc->owner = THIS_MODULE;
  110. gc->base = pfc->first_gpio;
  111. gc->ngpio = (pfc->last_gpio - pfc->first_gpio) + 1;
  112. }
  113. int sh_pfc_register_gpiochip(struct sh_pfc *pfc)
  114. {
  115. struct sh_pfc_chip *chip;
  116. int ret;
  117. chip = kzalloc(sizeof(struct sh_pfc_chip), GFP_KERNEL);
  118. if (unlikely(!chip))
  119. return -ENOMEM;
  120. chip->pfc = pfc;
  121. sh_pfc_gpio_setup(chip);
  122. ret = gpiochip_add(&chip->gpio_chip);
  123. if (unlikely(ret < 0))
  124. kfree(chip);
  125. pr_info("%s handling gpio %d -> %d\n",
  126. pfc->name, pfc->first_gpio, pfc->last_gpio);
  127. return ret;
  128. }
  129. EXPORT_SYMBOL_GPL(sh_pfc_register_gpiochip);
  130. static int sh_pfc_gpio_match(struct gpio_chip *gc, void *data)
  131. {
  132. return !!strstr(gc->label, data);
  133. }
  134. static int __devinit sh_pfc_gpio_probe(struct platform_device *pdev)
  135. {
  136. struct sh_pfc_chip *chip;
  137. struct gpio_chip *gc;
  138. gc = gpiochip_find("_pfc", sh_pfc_gpio_match);
  139. if (unlikely(!gc)) {
  140. pr_err("Cant find gpio chip\n");
  141. return -ENODEV;
  142. }
  143. chip = gpio_to_pfc_chip(gc);
  144. platform_set_drvdata(pdev, chip);
  145. pr_info("attaching to GPIO chip %s\n", chip->pfc->name);
  146. return 0;
  147. }
  148. static int __devexit sh_pfc_gpio_remove(struct platform_device *pdev)
  149. {
  150. struct sh_pfc_chip *chip = platform_get_drvdata(pdev);
  151. int ret;
  152. ret = gpiochip_remove(&chip->gpio_chip);
  153. if (unlikely(ret < 0))
  154. return ret;
  155. kfree(chip);
  156. return 0;
  157. }
  158. static struct platform_driver sh_pfc_gpio_driver = {
  159. .probe = sh_pfc_gpio_probe,
  160. .remove = __devexit_p(sh_pfc_gpio_remove),
  161. .driver = {
  162. .name = KBUILD_MODNAME,
  163. .owner = THIS_MODULE,
  164. },
  165. };
  166. static struct platform_device sh_pfc_gpio_device = {
  167. .name = KBUILD_MODNAME,
  168. .id = -1,
  169. };
  170. static int __init sh_pfc_gpio_init(void)
  171. {
  172. int rc;
  173. rc = platform_driver_register(&sh_pfc_gpio_driver);
  174. if (likely(!rc)) {
  175. rc = platform_device_register(&sh_pfc_gpio_device);
  176. if (unlikely(rc))
  177. platform_driver_unregister(&sh_pfc_gpio_driver);
  178. }
  179. return rc;
  180. }
  181. static void __exit sh_pfc_gpio_exit(void)
  182. {
  183. platform_device_unregister(&sh_pfc_gpio_device);
  184. platform_driver_unregister(&sh_pfc_gpio_driver);
  185. }
  186. module_init(sh_pfc_gpio_init);
  187. module_exit(sh_pfc_gpio_exit);
  188. MODULE_AUTHOR("Magnus Damm, Paul Mundt");
  189. MODULE_DESCRIPTION("GPIO driver for SuperH pin function controller");
  190. MODULE_LICENSE("GPL v2");
  191. MODULE_ALIAS("platform:pfc-gpio");