wm831x-gpio.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. static int wm831x_gpio_set_debounce(struct gpio_chip *chip, unsigned offset,
  91. unsigned debounce)
  92. {
  93. struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
  94. struct wm831x *wm831x = wm831x_gpio->wm831x;
  95. int reg = WM831X_GPIO1_CONTROL + offset;
  96. int ret, fn;
  97. ret = wm831x_reg_read(wm831x, reg);
  98. if (ret < 0)
  99. return ret;
  100. switch (ret & WM831X_GPN_FN_MASK) {
  101. case 0:
  102. case 1:
  103. break;
  104. default:
  105. /* Not in GPIO mode */
  106. return -EBUSY;
  107. }
  108. if (debounce >= 32 && debounce <= 64)
  109. fn = 0;
  110. else if (debounce >= 4000 && debounce <= 8000)
  111. fn = 1;
  112. else
  113. return -EINVAL;
  114. return wm831x_set_bits(wm831x, reg, WM831X_GPN_FN_MASK, fn);
  115. }
  116. #ifdef CONFIG_DEBUG_FS
  117. static void wm831x_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  118. {
  119. struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
  120. struct wm831x *wm831x = wm831x_gpio->wm831x;
  121. int i, tristated;
  122. for (i = 0; i < chip->ngpio; i++) {
  123. int gpio = i + chip->base;
  124. int reg;
  125. const char *label, *pull, *powerdomain;
  126. /* We report the GPIO even if it's not requested since
  127. * we're also reporting things like alternate
  128. * functions which apply even when the GPIO is not in
  129. * use as a GPIO.
  130. */
  131. label = gpiochip_is_requested(chip, i);
  132. if (!label)
  133. label = "Unrequested";
  134. seq_printf(s, " gpio-%-3d (%-20.20s) ", gpio, label);
  135. reg = wm831x_reg_read(wm831x, WM831X_GPIO1_CONTROL + i);
  136. if (reg < 0) {
  137. dev_err(wm831x->dev,
  138. "GPIO control %d read failed: %d\n",
  139. gpio, reg);
  140. seq_printf(s, "\n");
  141. continue;
  142. }
  143. switch (reg & WM831X_GPN_PULL_MASK) {
  144. case WM831X_GPIO_PULL_NONE:
  145. pull = "nopull";
  146. break;
  147. case WM831X_GPIO_PULL_DOWN:
  148. pull = "pulldown";
  149. break;
  150. case WM831X_GPIO_PULL_UP:
  151. pull = "pullup";
  152. break;
  153. default:
  154. pull = "INVALID PULL";
  155. break;
  156. }
  157. switch (i + 1) {
  158. case 1 ... 3:
  159. case 7 ... 9:
  160. if (reg & WM831X_GPN_PWR_DOM)
  161. powerdomain = "VPMIC";
  162. else
  163. powerdomain = "DBVDD";
  164. break;
  165. case 4 ... 6:
  166. case 10 ... 12:
  167. if (reg & WM831X_GPN_PWR_DOM)
  168. powerdomain = "SYSVDD";
  169. else
  170. powerdomain = "DBVDD";
  171. break;
  172. case 13 ... 16:
  173. powerdomain = "TPVDD";
  174. break;
  175. default:
  176. BUG();
  177. break;
  178. }
  179. tristated = reg & WM831X_GPN_TRI;
  180. if (wm831x->has_gpio_ena)
  181. tristated = !tristated;
  182. seq_printf(s, " %s %s %s %s%s\n"
  183. " %s%s (0x%4x)\n",
  184. reg & WM831X_GPN_DIR ? "in" : "out",
  185. wm831x_gpio_get(chip, i) ? "high" : "low",
  186. pull,
  187. powerdomain,
  188. reg & WM831X_GPN_POL ? "" : " inverted",
  189. reg & WM831X_GPN_OD ? "open-drain" : "CMOS",
  190. tristated ? " tristated" : "",
  191. reg);
  192. }
  193. }
  194. #else
  195. #define wm831x_gpio_dbg_show NULL
  196. #endif
  197. static struct gpio_chip template_chip = {
  198. .label = "wm831x",
  199. .owner = THIS_MODULE,
  200. .direction_input = wm831x_gpio_direction_in,
  201. .get = wm831x_gpio_get,
  202. .direction_output = wm831x_gpio_direction_out,
  203. .set = wm831x_gpio_set,
  204. .to_irq = wm831x_gpio_to_irq,
  205. .set_debounce = wm831x_gpio_set_debounce,
  206. .dbg_show = wm831x_gpio_dbg_show,
  207. .can_sleep = 1,
  208. };
  209. static int __devinit wm831x_gpio_probe(struct platform_device *pdev)
  210. {
  211. struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
  212. struct wm831x_pdata *pdata = wm831x->dev->platform_data;
  213. struct wm831x_gpio *wm831x_gpio;
  214. int ret;
  215. wm831x_gpio = kzalloc(sizeof(*wm831x_gpio), GFP_KERNEL);
  216. if (wm831x_gpio == NULL)
  217. return -ENOMEM;
  218. wm831x_gpio->wm831x = wm831x;
  219. wm831x_gpio->gpio_chip = template_chip;
  220. wm831x_gpio->gpio_chip.ngpio = wm831x->num_gpio;
  221. wm831x_gpio->gpio_chip.dev = &pdev->dev;
  222. if (pdata && pdata->gpio_base)
  223. wm831x_gpio->gpio_chip.base = pdata->gpio_base;
  224. else
  225. wm831x_gpio->gpio_chip.base = -1;
  226. ret = gpiochip_add(&wm831x_gpio->gpio_chip);
  227. if (ret < 0) {
  228. dev_err(&pdev->dev, "Could not register gpiochip, %d\n",
  229. ret);
  230. goto err;
  231. }
  232. platform_set_drvdata(pdev, wm831x_gpio);
  233. return ret;
  234. err:
  235. kfree(wm831x_gpio);
  236. return ret;
  237. }
  238. static int __devexit wm831x_gpio_remove(struct platform_device *pdev)
  239. {
  240. struct wm831x_gpio *wm831x_gpio = platform_get_drvdata(pdev);
  241. int ret;
  242. ret = gpiochip_remove(&wm831x_gpio->gpio_chip);
  243. if (ret == 0)
  244. kfree(wm831x_gpio);
  245. return ret;
  246. }
  247. static struct platform_driver wm831x_gpio_driver = {
  248. .driver.name = "wm831x-gpio",
  249. .driver.owner = THIS_MODULE,
  250. .probe = wm831x_gpio_probe,
  251. .remove = __devexit_p(wm831x_gpio_remove),
  252. };
  253. static int __init wm831x_gpio_init(void)
  254. {
  255. return platform_driver_register(&wm831x_gpio_driver);
  256. }
  257. subsys_initcall(wm831x_gpio_init);
  258. static void __exit wm831x_gpio_exit(void)
  259. {
  260. platform_driver_unregister(&wm831x_gpio_driver);
  261. }
  262. module_exit(wm831x_gpio_exit);
  263. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  264. MODULE_DESCRIPTION("GPIO interface for WM831x PMICs");
  265. MODULE_LICENSE("GPL");
  266. MODULE_ALIAS("platform:wm831x-gpio");