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