gpio.c 5.2 KB

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