gpio-wm831x.c 7.1 KB

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