gpio-wm831x.c 7.3 KB

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