gpio-wm8994.c 7.4 KB

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