wm831x-gpio.c 6.7 KB

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