wm831x-gpio.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * wm831x-gpio.c -- gpiolib support for Wolfson WM831x PMICs
  3. *
  4. * Copyright 2009 Wolfson Microelectronics PLC.
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/slab.h>
  16. #include <linux/module.h>
  17. #include <linux/gpio.h>
  18. #include <linux/mfd/core.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/seq_file.h>
  21. #include <linux/mfd/wm831x/core.h>
  22. #include <linux/mfd/wm831x/pdata.h>
  23. #include <linux/mfd/wm831x/gpio.h>
  24. #include <linux/mfd/wm831x/irq.h>
  25. struct wm831x_gpio {
  26. struct wm831x *wm831x;
  27. struct gpio_chip gpio_chip;
  28. };
  29. static inline struct wm831x_gpio *to_wm831x_gpio(struct gpio_chip *chip)
  30. {
  31. return container_of(chip, struct wm831x_gpio, gpio_chip);
  32. }
  33. static int wm831x_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
  34. {
  35. struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
  36. struct wm831x *wm831x = wm831x_gpio->wm831x;
  37. int val = WM831X_GPN_DIR;
  38. if (wm831x->has_gpio_ena)
  39. val |= WM831X_GPN_TRI;
  40. return wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset,
  41. WM831X_GPN_DIR | WM831X_GPN_TRI |
  42. WM831X_GPN_FN_MASK, val);
  43. }
  44. static int wm831x_gpio_get(struct gpio_chip *chip, unsigned offset)
  45. {
  46. struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
  47. struct wm831x *wm831x = wm831x_gpio->wm831x;
  48. int ret;
  49. ret = wm831x_reg_read(wm831x, WM831X_GPIO_LEVEL);
  50. if (ret < 0)
  51. return ret;
  52. if (ret & 1 << offset)
  53. return 1;
  54. else
  55. return 0;
  56. }
  57. static void wm831x_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  58. {
  59. struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
  60. struct wm831x *wm831x = wm831x_gpio->wm831x;
  61. wm831x_set_bits(wm831x, WM831X_GPIO_LEVEL, 1 << offset,
  62. value << offset);
  63. }
  64. static int wm831x_gpio_direction_out(struct gpio_chip *chip,
  65. unsigned offset, int value)
  66. {
  67. struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
  68. struct wm831x *wm831x = wm831x_gpio->wm831x;
  69. int val = 0;
  70. int ret;
  71. if (wm831x->has_gpio_ena)
  72. val |= WM831X_GPN_TRI;
  73. ret = wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset,
  74. WM831X_GPN_DIR | WM831X_GPN_TRI |
  75. WM831X_GPN_FN_MASK, val);
  76. if (ret < 0)
  77. return ret;
  78. /* Can only set GPIO state once it's in output mode */
  79. wm831x_gpio_set(chip, offset, value);
  80. return 0;
  81. }
  82. static int wm831x_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  83. {
  84. struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
  85. struct wm831x *wm831x = wm831x_gpio->wm831x;
  86. if (!wm831x->irq_base)
  87. return -EINVAL;
  88. return wm831x->irq_base + WM831X_IRQ_GPIO_1 + offset;
  89. }
  90. #ifdef CONFIG_DEBUG_FS
  91. static void wm831x_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  92. {
  93. struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
  94. struct wm831x *wm831x = wm831x_gpio->wm831x;
  95. int i, tristated;
  96. for (i = 0; i < chip->ngpio; i++) {
  97. int gpio = i + chip->base;
  98. int reg;
  99. const char *label, *pull, *powerdomain;
  100. /* We report the GPIO even if it's not requested since
  101. * we're also reporting things like alternate
  102. * functions which apply even when the GPIO is not in
  103. * use as a GPIO.
  104. */
  105. label = gpiochip_is_requested(chip, i);
  106. if (!label)
  107. label = "Unrequested";
  108. seq_printf(s, " gpio-%-3d (%-20.20s) ", gpio, label);
  109. reg = wm831x_reg_read(wm831x, WM831X_GPIO1_CONTROL + i);
  110. if (reg < 0) {
  111. dev_err(wm831x->dev,
  112. "GPIO control %d read failed: %d\n",
  113. gpio, reg);
  114. seq_printf(s, "\n");
  115. continue;
  116. }
  117. switch (reg & WM831X_GPN_PULL_MASK) {
  118. case WM831X_GPIO_PULL_NONE:
  119. pull = "nopull";
  120. break;
  121. case WM831X_GPIO_PULL_DOWN:
  122. pull = "pulldown";
  123. break;
  124. case WM831X_GPIO_PULL_UP:
  125. pull = "pullup";
  126. default:
  127. pull = "INVALID PULL";
  128. break;
  129. }
  130. switch (i + 1) {
  131. case 1 ... 3:
  132. case 7 ... 9:
  133. if (reg & WM831X_GPN_PWR_DOM)
  134. powerdomain = "VPMIC";
  135. else
  136. powerdomain = "DBVDD";
  137. break;
  138. case 4 ... 6:
  139. case 10 ... 12:
  140. if (reg & WM831X_GPN_PWR_DOM)
  141. powerdomain = "SYSVDD";
  142. else
  143. powerdomain = "DBVDD";
  144. break;
  145. case 13 ... 16:
  146. powerdomain = "TPVDD";
  147. break;
  148. default:
  149. BUG();
  150. break;
  151. }
  152. tristated = reg & WM831X_GPN_TRI;
  153. if (wm831x->has_gpio_ena)
  154. tristated = !tristated;
  155. seq_printf(s, " %s %s %s %s%s\n"
  156. " %s%s (0x%4x)\n",
  157. reg & WM831X_GPN_DIR ? "in" : "out",
  158. wm831x_gpio_get(chip, i) ? "high" : "low",
  159. pull,
  160. powerdomain,
  161. reg & WM831X_GPN_POL ? "" : " inverted",
  162. reg & WM831X_GPN_OD ? "open-drain" : "CMOS",
  163. tristated ? " tristated" : "",
  164. reg);
  165. }
  166. }
  167. #else
  168. #define wm831x_gpio_dbg_show NULL
  169. #endif
  170. static struct gpio_chip template_chip = {
  171. .label = "wm831x",
  172. .owner = THIS_MODULE,
  173. .direction_input = wm831x_gpio_direction_in,
  174. .get = wm831x_gpio_get,
  175. .direction_output = wm831x_gpio_direction_out,
  176. .set = wm831x_gpio_set,
  177. .to_irq = wm831x_gpio_to_irq,
  178. .dbg_show = wm831x_gpio_dbg_show,
  179. .can_sleep = 1,
  180. };
  181. static int __devinit wm831x_gpio_probe(struct platform_device *pdev)
  182. {
  183. struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
  184. struct wm831x_pdata *pdata = wm831x->dev->platform_data;
  185. struct wm831x_gpio *wm831x_gpio;
  186. int ret;
  187. wm831x_gpio = kzalloc(sizeof(*wm831x_gpio), GFP_KERNEL);
  188. if (wm831x_gpio == NULL)
  189. return -ENOMEM;
  190. wm831x_gpio->wm831x = wm831x;
  191. wm831x_gpio->gpio_chip = template_chip;
  192. wm831x_gpio->gpio_chip.ngpio = wm831x->num_gpio;
  193. wm831x_gpio->gpio_chip.dev = &pdev->dev;
  194. if (pdata && pdata->gpio_base)
  195. wm831x_gpio->gpio_chip.base = pdata->gpio_base;
  196. else
  197. wm831x_gpio->gpio_chip.base = -1;
  198. ret = gpiochip_add(&wm831x_gpio->gpio_chip);
  199. if (ret < 0) {
  200. dev_err(&pdev->dev, "Could not register gpiochip, %d\n",
  201. ret);
  202. goto err;
  203. }
  204. platform_set_drvdata(pdev, wm831x_gpio);
  205. return ret;
  206. err:
  207. kfree(wm831x_gpio);
  208. return ret;
  209. }
  210. static int __devexit wm831x_gpio_remove(struct platform_device *pdev)
  211. {
  212. struct wm831x_gpio *wm831x_gpio = platform_get_drvdata(pdev);
  213. int ret;
  214. ret = gpiochip_remove(&wm831x_gpio->gpio_chip);
  215. if (ret == 0)
  216. kfree(wm831x_gpio);
  217. return ret;
  218. }
  219. static struct platform_driver wm831x_gpio_driver = {
  220. .driver.name = "wm831x-gpio",
  221. .driver.owner = THIS_MODULE,
  222. .probe = wm831x_gpio_probe,
  223. .remove = __devexit_p(wm831x_gpio_remove),
  224. };
  225. static int __init wm831x_gpio_init(void)
  226. {
  227. return platform_driver_register(&wm831x_gpio_driver);
  228. }
  229. subsys_initcall(wm831x_gpio_init);
  230. static void __exit wm831x_gpio_exit(void)
  231. {
  232. platform_driver_unregister(&wm831x_gpio_driver);
  233. }
  234. module_exit(wm831x_gpio_exit);
  235. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  236. MODULE_DESCRIPTION("GPIO interface for WM831x PMICs");
  237. MODULE_LICENSE("GPL");
  238. MODULE_ALIAS("platform:wm831x-gpio");