gpio-wm8994.c 7.4 KB

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