gpio-wm8994.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * gpiolib support for Wolfson WM8994
  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/regmap.h>
  18. #include <linux/mfd/wm8994/core.h>
  19. #include <linux/mfd/wm8994/pdata.h>
  20. #include <linux/mfd/wm8994/gpio.h>
  21. #include <linux/mfd/wm8994/registers.h>
  22. struct wm8994_gpio {
  23. struct wm8994 *wm8994;
  24. struct gpio_chip gpio_chip;
  25. };
  26. static int wm8994_gpio_request(struct gpio_chip *chip, unsigned offset)
  27. {
  28. struct wm8994_gpio *wm8994_gpio = gpiochip_get_data(chip);
  29. struct wm8994 *wm8994 = wm8994_gpio->wm8994;
  30. switch (wm8994->type) {
  31. case WM8958:
  32. switch (offset) {
  33. case 1:
  34. case 2:
  35. case 3:
  36. case 4:
  37. case 6:
  38. return -EINVAL;
  39. }
  40. break;
  41. default:
  42. break;
  43. }
  44. return 0;
  45. }
  46. static int wm8994_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
  47. {
  48. struct wm8994_gpio *wm8994_gpio = gpiochip_get_data(chip);
  49. struct wm8994 *wm8994 = wm8994_gpio->wm8994;
  50. return wm8994_set_bits(wm8994, WM8994_GPIO_1 + offset,
  51. WM8994_GPN_DIR, WM8994_GPN_DIR);
  52. }
  53. static int wm8994_gpio_get(struct gpio_chip *chip, unsigned offset)
  54. {
  55. struct wm8994_gpio *wm8994_gpio = gpiochip_get_data(chip);
  56. struct wm8994 *wm8994 = wm8994_gpio->wm8994;
  57. int ret;
  58. ret = wm8994_reg_read(wm8994, WM8994_GPIO_1 + offset);
  59. if (ret < 0)
  60. return ret;
  61. if (ret & WM8994_GPN_LVL)
  62. return 1;
  63. else
  64. return 0;
  65. }
  66. static int wm8994_gpio_direction_out(struct gpio_chip *chip,
  67. unsigned offset, int value)
  68. {
  69. struct wm8994_gpio *wm8994_gpio = gpiochip_get_data(chip);
  70. struct wm8994 *wm8994 = wm8994_gpio->wm8994;
  71. if (value)
  72. value = WM8994_GPN_LVL;
  73. return wm8994_set_bits(wm8994, WM8994_GPIO_1 + offset,
  74. WM8994_GPN_DIR | WM8994_GPN_LVL, value);
  75. }
  76. static void wm8994_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  77. {
  78. struct wm8994_gpio *wm8994_gpio = gpiochip_get_data(chip);
  79. struct wm8994 *wm8994 = wm8994_gpio->wm8994;
  80. if (value)
  81. value = WM8994_GPN_LVL;
  82. wm8994_set_bits(wm8994, WM8994_GPIO_1 + offset, WM8994_GPN_LVL, value);
  83. }
  84. static int wm8994_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
  85. unsigned long config)
  86. {
  87. struct wm8994_gpio *wm8994_gpio = gpiochip_get_data(chip);
  88. struct wm8994 *wm8994 = wm8994_gpio->wm8994;
  89. switch (pinconf_to_config_param(config)) {
  90. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  91. return wm8994_set_bits(wm8994, WM8994_GPIO_1 + offset,
  92. WM8994_GPN_OP_CFG_MASK,
  93. WM8994_GPN_OP_CFG);
  94. case PIN_CONFIG_DRIVE_PUSH_PULL:
  95. return wm8994_set_bits(wm8994, WM8994_GPIO_1 + offset,
  96. WM8994_GPN_OP_CFG_MASK, 0);
  97. default:
  98. break;
  99. }
  100. return -ENOTSUPP;
  101. }
  102. static int wm8994_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  103. {
  104. struct wm8994_gpio *wm8994_gpio = gpiochip_get_data(chip);
  105. struct wm8994 *wm8994 = wm8994_gpio->wm8994;
  106. return regmap_irq_get_virq(wm8994->irq_data, offset);
  107. }
  108. #ifdef CONFIG_DEBUG_FS
  109. static const char *wm8994_gpio_fn(u16 fn)
  110. {
  111. switch (fn) {
  112. case WM8994_GP_FN_PIN_SPECIFIC:
  113. return "pin-specific";
  114. case WM8994_GP_FN_GPIO:
  115. return "GPIO";
  116. case WM8994_GP_FN_SDOUT:
  117. return "SDOUT";
  118. case WM8994_GP_FN_IRQ:
  119. return "IRQ";
  120. case WM8994_GP_FN_TEMPERATURE:
  121. return "Temperature";
  122. case WM8994_GP_FN_MICBIAS1_DET:
  123. return "MICBIAS1 detect";
  124. case WM8994_GP_FN_MICBIAS1_SHORT:
  125. return "MICBIAS1 short";
  126. case WM8994_GP_FN_MICBIAS2_DET:
  127. return "MICBIAS2 detect";
  128. case WM8994_GP_FN_MICBIAS2_SHORT:
  129. return "MICBIAS2 short";
  130. case WM8994_GP_FN_FLL1_LOCK:
  131. return "FLL1 lock";
  132. case WM8994_GP_FN_FLL2_LOCK:
  133. return "FLL2 lock";
  134. case WM8994_GP_FN_SRC1_LOCK:
  135. return "SRC1 lock";
  136. case WM8994_GP_FN_SRC2_LOCK:
  137. return "SRC2 lock";
  138. case WM8994_GP_FN_DRC1_ACT:
  139. return "DRC1 activity";
  140. case WM8994_GP_FN_DRC2_ACT:
  141. return "DRC2 activity";
  142. case WM8994_GP_FN_DRC3_ACT:
  143. return "DRC3 activity";
  144. case WM8994_GP_FN_WSEQ_STATUS:
  145. return "Write sequencer";
  146. case WM8994_GP_FN_FIFO_ERROR:
  147. return "FIFO error";
  148. case WM8994_GP_FN_OPCLK:
  149. return "OPCLK";
  150. case WM8994_GP_FN_THW:
  151. return "Thermal warning";
  152. case WM8994_GP_FN_DCS_DONE:
  153. return "DC servo";
  154. case WM8994_GP_FN_FLL1_OUT:
  155. return "FLL1 output";
  156. case WM8994_GP_FN_FLL2_OUT:
  157. return "FLL1 output";
  158. default:
  159. return "Unknown";
  160. }
  161. }
  162. static void wm8994_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  163. {
  164. struct wm8994_gpio *wm8994_gpio = gpiochip_get_data(chip);
  165. struct wm8994 *wm8994 = wm8994_gpio->wm8994;
  166. int i;
  167. for (i = 0; i < chip->ngpio; i++) {
  168. int gpio = i + chip->base;
  169. int reg;
  170. const char *label;
  171. /* We report the GPIO even if it's not requested since
  172. * we're also reporting things like alternate
  173. * functions which apply even when the GPIO is not in
  174. * use as a GPIO.
  175. */
  176. label = gpiochip_is_requested(chip, i);
  177. if (!label)
  178. label = "Unrequested";
  179. seq_printf(s, " gpio-%-3d (%-20.20s) ", gpio, label);
  180. reg = wm8994_reg_read(wm8994, WM8994_GPIO_1 + i);
  181. if (reg < 0) {
  182. dev_err(wm8994->dev,
  183. "GPIO control %d read failed: %d\n",
  184. gpio, reg);
  185. seq_printf(s, "\n");
  186. continue;
  187. }
  188. if (reg & WM8994_GPN_DIR)
  189. seq_printf(s, "in ");
  190. else
  191. seq_printf(s, "out ");
  192. if (reg & WM8994_GPN_PU)
  193. seq_printf(s, "pull up ");
  194. if (reg & WM8994_GPN_PD)
  195. seq_printf(s, "pull down ");
  196. if (reg & WM8994_GPN_POL)
  197. seq_printf(s, "inverted ");
  198. else
  199. seq_printf(s, "noninverted ");
  200. if (reg & WM8994_GPN_OP_CFG)
  201. seq_printf(s, "open drain ");
  202. else
  203. seq_printf(s, "push-pull ");
  204. seq_printf(s, "%s (%x)\n",
  205. wm8994_gpio_fn(reg & WM8994_GPN_FN_MASK), reg);
  206. }
  207. }
  208. #else
  209. #define wm8994_gpio_dbg_show NULL
  210. #endif
  211. static const struct gpio_chip template_chip = {
  212. .label = "wm8994",
  213. .owner = THIS_MODULE,
  214. .request = wm8994_gpio_request,
  215. .direction_input = wm8994_gpio_direction_in,
  216. .get = wm8994_gpio_get,
  217. .direction_output = wm8994_gpio_direction_out,
  218. .set = wm8994_gpio_set,
  219. .set_config = wm8994_gpio_set_config,
  220. .to_irq = wm8994_gpio_to_irq,
  221. .dbg_show = wm8994_gpio_dbg_show,
  222. .can_sleep = true,
  223. };
  224. static int wm8994_gpio_probe(struct platform_device *pdev)
  225. {
  226. struct wm8994 *wm8994 = dev_get_drvdata(pdev->dev.parent);
  227. struct wm8994_pdata *pdata = dev_get_platdata(wm8994->dev);
  228. struct wm8994_gpio *wm8994_gpio;
  229. int ret;
  230. wm8994_gpio = devm_kzalloc(&pdev->dev, sizeof(*wm8994_gpio),
  231. GFP_KERNEL);
  232. if (wm8994_gpio == NULL)
  233. return -ENOMEM;
  234. wm8994_gpio->wm8994 = wm8994;
  235. wm8994_gpio->gpio_chip = template_chip;
  236. wm8994_gpio->gpio_chip.ngpio = WM8994_GPIO_MAX;
  237. wm8994_gpio->gpio_chip.parent = &pdev->dev;
  238. if (pdata && pdata->gpio_base)
  239. wm8994_gpio->gpio_chip.base = pdata->gpio_base;
  240. else
  241. wm8994_gpio->gpio_chip.base = -1;
  242. ret = devm_gpiochip_add_data(&pdev->dev, &wm8994_gpio->gpio_chip,
  243. wm8994_gpio);
  244. if (ret < 0) {
  245. dev_err(&pdev->dev, "Could not register gpiochip, %d\n",
  246. ret);
  247. return ret;
  248. }
  249. platform_set_drvdata(pdev, wm8994_gpio);
  250. return ret;
  251. }
  252. static struct platform_driver wm8994_gpio_driver = {
  253. .driver.name = "wm8994-gpio",
  254. .probe = wm8994_gpio_probe,
  255. };
  256. static int __init wm8994_gpio_init(void)
  257. {
  258. return platform_driver_register(&wm8994_gpio_driver);
  259. }
  260. subsys_initcall(wm8994_gpio_init);
  261. static void __exit wm8994_gpio_exit(void)
  262. {
  263. platform_driver_unregister(&wm8994_gpio_driver);
  264. }
  265. module_exit(wm8994_gpio_exit);
  266. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  267. MODULE_DESCRIPTION("GPIO interface for WM8994");
  268. MODULE_LICENSE("GPL");
  269. MODULE_ALIAS("platform:wm8994-gpio");