gpio-wm8994.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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_to_irq(struct gpio_chip *chip, unsigned offset)
  89. {
  90. struct wm8994_gpio *wm8994_gpio = gpiochip_get_data(chip);
  91. struct wm8994 *wm8994 = wm8994_gpio->wm8994;
  92. return regmap_irq_get_virq(wm8994->irq_data, offset);
  93. }
  94. #ifdef CONFIG_DEBUG_FS
  95. static const char *wm8994_gpio_fn(u16 fn)
  96. {
  97. switch (fn) {
  98. case WM8994_GP_FN_PIN_SPECIFIC:
  99. return "pin-specific";
  100. case WM8994_GP_FN_GPIO:
  101. return "GPIO";
  102. case WM8994_GP_FN_SDOUT:
  103. return "SDOUT";
  104. case WM8994_GP_FN_IRQ:
  105. return "IRQ";
  106. case WM8994_GP_FN_TEMPERATURE:
  107. return "Temperature";
  108. case WM8994_GP_FN_MICBIAS1_DET:
  109. return "MICBIAS1 detect";
  110. case WM8994_GP_FN_MICBIAS1_SHORT:
  111. return "MICBIAS1 short";
  112. case WM8994_GP_FN_MICBIAS2_DET:
  113. return "MICBIAS2 detect";
  114. case WM8994_GP_FN_MICBIAS2_SHORT:
  115. return "MICBIAS2 short";
  116. case WM8994_GP_FN_FLL1_LOCK:
  117. return "FLL1 lock";
  118. case WM8994_GP_FN_FLL2_LOCK:
  119. return "FLL2 lock";
  120. case WM8994_GP_FN_SRC1_LOCK:
  121. return "SRC1 lock";
  122. case WM8994_GP_FN_SRC2_LOCK:
  123. return "SRC2 lock";
  124. case WM8994_GP_FN_DRC1_ACT:
  125. return "DRC1 activity";
  126. case WM8994_GP_FN_DRC2_ACT:
  127. return "DRC2 activity";
  128. case WM8994_GP_FN_DRC3_ACT:
  129. return "DRC3 activity";
  130. case WM8994_GP_FN_WSEQ_STATUS:
  131. return "Write sequencer";
  132. case WM8994_GP_FN_FIFO_ERROR:
  133. return "FIFO error";
  134. case WM8994_GP_FN_OPCLK:
  135. return "OPCLK";
  136. case WM8994_GP_FN_THW:
  137. return "Thermal warning";
  138. case WM8994_GP_FN_DCS_DONE:
  139. return "DC servo";
  140. case WM8994_GP_FN_FLL1_OUT:
  141. return "FLL1 output";
  142. case WM8994_GP_FN_FLL2_OUT:
  143. return "FLL1 output";
  144. default:
  145. return "Unknown";
  146. }
  147. }
  148. static void wm8994_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  149. {
  150. struct wm8994_gpio *wm8994_gpio = gpiochip_get_data(chip);
  151. struct wm8994 *wm8994 = wm8994_gpio->wm8994;
  152. int i;
  153. for (i = 0; i < chip->ngpio; i++) {
  154. int gpio = i + chip->base;
  155. int reg;
  156. const char *label;
  157. /* We report the GPIO even if it's not requested since
  158. * we're also reporting things like alternate
  159. * functions which apply even when the GPIO is not in
  160. * use as a GPIO.
  161. */
  162. label = gpiochip_is_requested(chip, i);
  163. if (!label)
  164. label = "Unrequested";
  165. seq_printf(s, " gpio-%-3d (%-20.20s) ", gpio, label);
  166. reg = wm8994_reg_read(wm8994, WM8994_GPIO_1 + i);
  167. if (reg < 0) {
  168. dev_err(wm8994->dev,
  169. "GPIO control %d read failed: %d\n",
  170. gpio, reg);
  171. seq_printf(s, "\n");
  172. continue;
  173. }
  174. if (reg & WM8994_GPN_DIR)
  175. seq_printf(s, "in ");
  176. else
  177. seq_printf(s, "out ");
  178. if (reg & WM8994_GPN_PU)
  179. seq_printf(s, "pull up ");
  180. if (reg & WM8994_GPN_PD)
  181. seq_printf(s, "pull down ");
  182. if (reg & WM8994_GPN_POL)
  183. seq_printf(s, "inverted ");
  184. else
  185. seq_printf(s, "noninverted ");
  186. if (reg & WM8994_GPN_OP_CFG)
  187. seq_printf(s, "open drain ");
  188. else
  189. seq_printf(s, "CMOS ");
  190. seq_printf(s, "%s (%x)\n",
  191. wm8994_gpio_fn(reg & WM8994_GPN_FN_MASK), reg);
  192. }
  193. }
  194. #else
  195. #define wm8994_gpio_dbg_show NULL
  196. #endif
  197. static struct gpio_chip template_chip = {
  198. .label = "wm8994",
  199. .owner = THIS_MODULE,
  200. .request = wm8994_gpio_request,
  201. .direction_input = wm8994_gpio_direction_in,
  202. .get = wm8994_gpio_get,
  203. .direction_output = wm8994_gpio_direction_out,
  204. .set = wm8994_gpio_set,
  205. .to_irq = wm8994_gpio_to_irq,
  206. .dbg_show = wm8994_gpio_dbg_show,
  207. .can_sleep = true,
  208. };
  209. static int wm8994_gpio_probe(struct platform_device *pdev)
  210. {
  211. struct wm8994 *wm8994 = dev_get_drvdata(pdev->dev.parent);
  212. struct wm8994_pdata *pdata = dev_get_platdata(wm8994->dev);
  213. struct wm8994_gpio *wm8994_gpio;
  214. int ret;
  215. wm8994_gpio = devm_kzalloc(&pdev->dev, sizeof(*wm8994_gpio),
  216. GFP_KERNEL);
  217. if (wm8994_gpio == NULL)
  218. return -ENOMEM;
  219. wm8994_gpio->wm8994 = wm8994;
  220. wm8994_gpio->gpio_chip = template_chip;
  221. wm8994_gpio->gpio_chip.ngpio = WM8994_GPIO_MAX;
  222. wm8994_gpio->gpio_chip.parent = &pdev->dev;
  223. if (pdata && pdata->gpio_base)
  224. wm8994_gpio->gpio_chip.base = pdata->gpio_base;
  225. else
  226. wm8994_gpio->gpio_chip.base = -1;
  227. ret = devm_gpiochip_add_data(&pdev->dev, &wm8994_gpio->gpio_chip,
  228. wm8994_gpio);
  229. if (ret < 0) {
  230. dev_err(&pdev->dev, "Could not register gpiochip, %d\n",
  231. ret);
  232. return ret;
  233. }
  234. platform_set_drvdata(pdev, wm8994_gpio);
  235. return ret;
  236. }
  237. static struct platform_driver wm8994_gpio_driver = {
  238. .driver.name = "wm8994-gpio",
  239. .driver.owner = THIS_MODULE,
  240. .probe = wm8994_gpio_probe,
  241. };
  242. static int __init wm8994_gpio_init(void)
  243. {
  244. return platform_driver_register(&wm8994_gpio_driver);
  245. }
  246. subsys_initcall(wm8994_gpio_init);
  247. static void __exit wm8994_gpio_exit(void)
  248. {
  249. platform_driver_unregister(&wm8994_gpio_driver);
  250. }
  251. module_exit(wm8994_gpio_exit);
  252. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  253. MODULE_DESCRIPTION("GPIO interface for WM8994");
  254. MODULE_LICENSE("GPL");
  255. MODULE_ALIAS("platform:wm8994-gpio");