gpio-wm831x.c 7.6 KB

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