gpio-wm831x.c 7.5 KB

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