pfc-gpio.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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) 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. struct sh_pfc_chip {
  19. struct sh_pfc *pfc;
  20. struct gpio_chip gpio_chip;
  21. };
  22. static struct sh_pfc_chip *gpio_to_pfc_chip(struct gpio_chip *gc)
  23. {
  24. return container_of(gc, struct sh_pfc_chip, gpio_chip);
  25. }
  26. static struct sh_pfc *gpio_to_pfc(struct gpio_chip *gc)
  27. {
  28. return gpio_to_pfc_chip(gc)->pfc;
  29. }
  30. static int sh_gpio_request(struct gpio_chip *gc, unsigned offset)
  31. {
  32. struct sh_pfc *pfc = gpio_to_pfc(gc);
  33. struct pinmux_data_reg *dummy;
  34. unsigned long flags;
  35. int i, ret, pinmux_type;
  36. ret = -EINVAL;
  37. if (!pfc)
  38. goto err_out;
  39. spin_lock_irqsave(&pfc->lock, flags);
  40. if ((pfc->gpios[offset].flags & PINMUX_FLAG_TYPE) != PINMUX_TYPE_NONE)
  41. goto err_unlock;
  42. /* setup pin function here if no data is associated with pin */
  43. if (sh_pfc_get_data_reg(pfc, offset, &dummy, &i) != 0)
  44. pinmux_type = PINMUX_TYPE_FUNCTION;
  45. else
  46. pinmux_type = PINMUX_TYPE_GPIO;
  47. if (pinmux_type == PINMUX_TYPE_FUNCTION) {
  48. if (sh_pfc_config_gpio(pfc, offset,
  49. pinmux_type,
  50. GPIO_CFG_DRYRUN) != 0)
  51. goto err_unlock;
  52. if (sh_pfc_config_gpio(pfc, offset,
  53. pinmux_type,
  54. GPIO_CFG_REQ) != 0)
  55. BUG();
  56. }
  57. pfc->gpios[offset].flags &= ~PINMUX_FLAG_TYPE;
  58. pfc->gpios[offset].flags |= pinmux_type;
  59. ret = 0;
  60. err_unlock:
  61. spin_unlock_irqrestore(&pfc->lock, flags);
  62. err_out:
  63. return ret;
  64. }
  65. static void sh_gpio_free(struct gpio_chip *gc, unsigned offset)
  66. {
  67. struct sh_pfc *pfc = gpio_to_pfc(gc);
  68. unsigned long flags;
  69. int pinmux_type;
  70. if (!pfc)
  71. return;
  72. spin_lock_irqsave(&pfc->lock, flags);
  73. pinmux_type = pfc->gpios[offset].flags & PINMUX_FLAG_TYPE;
  74. sh_pfc_config_gpio(pfc, offset, pinmux_type, GPIO_CFG_FREE);
  75. pfc->gpios[offset].flags &= ~PINMUX_FLAG_TYPE;
  76. pfc->gpios[offset].flags |= PINMUX_TYPE_NONE;
  77. spin_unlock_irqrestore(&pfc->lock, flags);
  78. }
  79. static int sh_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
  80. {
  81. struct sh_pfc *pfc = gpio_to_pfc(gc);
  82. unsigned long flags;
  83. int ret;
  84. spin_lock_irqsave(&pfc->lock, flags);
  85. ret = sh_pfc_set_direction(pfc, offset, PINMUX_TYPE_INPUT);
  86. spin_unlock_irqrestore(&pfc->lock, flags);
  87. return ret;
  88. }
  89. static void sh_gpio_set_value(struct sh_pfc *pfc, unsigned gpio, int value)
  90. {
  91. struct pinmux_data_reg *dr = NULL;
  92. int bit = 0;
  93. if (!pfc || sh_pfc_get_data_reg(pfc, gpio, &dr, &bit) != 0)
  94. BUG();
  95. else
  96. sh_pfc_write_bit(dr, bit, value);
  97. }
  98. static int sh_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
  99. int value)
  100. {
  101. struct sh_pfc *pfc = gpio_to_pfc(gc);
  102. unsigned long flags;
  103. int ret;
  104. sh_gpio_set_value(pfc, offset, value);
  105. spin_lock_irqsave(&pfc->lock, flags);
  106. ret = sh_pfc_set_direction(pfc, offset, PINMUX_TYPE_OUTPUT);
  107. spin_unlock_irqrestore(&pfc->lock, flags);
  108. return ret;
  109. }
  110. static int sh_gpio_get_value(struct sh_pfc *pfc, unsigned gpio)
  111. {
  112. struct pinmux_data_reg *dr = NULL;
  113. int bit = 0;
  114. if (!pfc || sh_pfc_get_data_reg(pfc, gpio, &dr, &bit) != 0)
  115. return -EINVAL;
  116. return sh_pfc_read_bit(dr, bit);
  117. }
  118. static int sh_gpio_get(struct gpio_chip *gc, unsigned offset)
  119. {
  120. return sh_gpio_get_value(gpio_to_pfc(gc), offset);
  121. }
  122. static void sh_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
  123. {
  124. sh_gpio_set_value(gpio_to_pfc(gc), offset, value);
  125. }
  126. static int sh_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
  127. {
  128. struct sh_pfc *pfc = gpio_to_pfc(gc);
  129. pinmux_enum_t enum_id;
  130. pinmux_enum_t *enum_ids;
  131. int i, k, pos;
  132. pos = 0;
  133. enum_id = 0;
  134. while (1) {
  135. pos = sh_pfc_gpio_to_enum(pfc, offset, pos, &enum_id);
  136. if (pos <= 0 || !enum_id)
  137. break;
  138. for (i = 0; i < pfc->gpio_irq_size; i++) {
  139. enum_ids = pfc->gpio_irq[i].enum_ids;
  140. for (k = 0; enum_ids[k]; k++) {
  141. if (enum_ids[k] == enum_id)
  142. return pfc->gpio_irq[i].irq;
  143. }
  144. }
  145. }
  146. return -ENOSYS;
  147. }
  148. static void sh_pfc_gpio_setup(struct sh_pfc_chip *chip)
  149. {
  150. struct sh_pfc *pfc = chip->pfc;
  151. struct gpio_chip *gc = &chip->gpio_chip;
  152. gc->request = sh_gpio_request;
  153. gc->free = sh_gpio_free;
  154. gc->direction_input = sh_gpio_direction_input;
  155. gc->get = sh_gpio_get;
  156. gc->direction_output = sh_gpio_direction_output;
  157. gc->set = sh_gpio_set;
  158. gc->to_irq = sh_gpio_to_irq;
  159. WARN_ON(pfc->first_gpio != 0); /* needs testing */
  160. gc->label = pfc->name;
  161. gc->owner = THIS_MODULE;
  162. gc->base = pfc->first_gpio;
  163. gc->ngpio = (pfc->last_gpio - pfc->first_gpio) + 1;
  164. }
  165. int sh_pfc_register_gpiochip(struct sh_pfc *pfc)
  166. {
  167. struct sh_pfc_chip *chip;
  168. int ret;
  169. chip = kzalloc(sizeof(struct sh_pfc_chip), GFP_KERNEL);
  170. if (unlikely(!chip))
  171. return -ENOMEM;
  172. chip->pfc = pfc;
  173. sh_pfc_gpio_setup(chip);
  174. ret = gpiochip_add(&chip->gpio_chip);
  175. if (unlikely(ret < 0))
  176. kfree(chip);
  177. pr_info("%s handling gpio %d -> %d\n",
  178. pfc->name, pfc->first_gpio, pfc->last_gpio);
  179. return ret;
  180. }
  181. EXPORT_SYMBOL_GPL(sh_pfc_register_gpiochip);
  182. static int sh_pfc_gpio_match(struct gpio_chip *gc, void *data)
  183. {
  184. return !!strstr(gc->label, data);
  185. }
  186. static int __devinit sh_pfc_gpio_probe(struct platform_device *pdev)
  187. {
  188. struct sh_pfc_chip *chip;
  189. struct gpio_chip *gc;
  190. gc = gpiochip_find("_pfc", sh_pfc_gpio_match);
  191. if (unlikely(!gc)) {
  192. pr_err("Cant find gpio chip\n");
  193. return -ENODEV;
  194. }
  195. chip = gpio_to_pfc_chip(gc);
  196. platform_set_drvdata(pdev, chip);
  197. pr_info("attaching to GPIO chip %s\n", chip->pfc->name);
  198. return 0;
  199. }
  200. static int __devexit sh_pfc_gpio_remove(struct platform_device *pdev)
  201. {
  202. struct sh_pfc_chip *chip = platform_get_drvdata(pdev);
  203. int ret;
  204. ret = gpiochip_remove(&chip->gpio_chip);
  205. if (unlikely(ret < 0))
  206. return ret;
  207. kfree(chip);
  208. return 0;
  209. }
  210. static struct platform_driver sh_pfc_gpio_driver = {
  211. .probe = sh_pfc_gpio_probe,
  212. .remove = __devexit_p(sh_pfc_gpio_remove),
  213. .driver = {
  214. .name = KBUILD_MODNAME,
  215. .owner = THIS_MODULE,
  216. },
  217. };
  218. static struct platform_device sh_pfc_gpio_device = {
  219. .name = KBUILD_MODNAME,
  220. .id = -1,
  221. };
  222. static int __init sh_pfc_gpio_init(void)
  223. {
  224. int rc;
  225. rc = platform_driver_register(&sh_pfc_gpio_driver);
  226. if (likely(!rc)) {
  227. rc = platform_device_register(&sh_pfc_gpio_device);
  228. if (unlikely(rc))
  229. platform_driver_unregister(&sh_pfc_gpio_driver);
  230. }
  231. return rc;
  232. }
  233. static void __exit sh_pfc_gpio_exit(void)
  234. {
  235. platform_device_unregister(&sh_pfc_gpio_device);
  236. platform_driver_unregister(&sh_pfc_gpio_driver);
  237. }
  238. module_init(sh_pfc_gpio_init);
  239. module_exit(sh_pfc_gpio_exit);
  240. MODULE_AUTHOR("Magnus Damm, Paul Mundt");
  241. MODULE_DESCRIPTION("GPIO driver for SuperH pin function controller");
  242. MODULE_LICENSE("GPL v2");
  243. MODULE_ALIAS("platform:pfc-gpio");