wm831x-gpio.c 6.8 KB

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