gpio-wm8994.c 7.7 KB

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