gpio-wm8994.c 7.7 KB

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