gpio-wm8994.c 7.4 KB

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