wm831x-gpio.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * wm831x-gpio.c -- gpiolib support for Wolfson WM831x PMICs
  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/module.h>
  16. #include <linux/gpio.h>
  17. #include <linux/mfd/core.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/seq_file.h>
  20. #include <linux/mfd/wm831x/core.h>
  21. #include <linux/mfd/wm831x/pdata.h>
  22. #include <linux/mfd/wm831x/gpio.h>
  23. #include <linux/mfd/wm831x/irq.h>
  24. struct wm831x_gpio {
  25. struct wm831x *wm831x;
  26. struct gpio_chip gpio_chip;
  27. };
  28. static inline struct wm831x_gpio *to_wm831x_gpio(struct gpio_chip *chip)
  29. {
  30. return container_of(chip, struct wm831x_gpio, gpio_chip);
  31. }
  32. static int wm831x_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
  33. {
  34. struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
  35. struct wm831x *wm831x = wm831x_gpio->wm831x;
  36. return wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset,
  37. WM831X_GPN_DIR | WM831X_GPN_TRI,
  38. WM831X_GPN_DIR);
  39. }
  40. static int wm831x_gpio_get(struct gpio_chip *chip, unsigned offset)
  41. {
  42. struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
  43. struct wm831x *wm831x = wm831x_gpio->wm831x;
  44. int ret;
  45. ret = wm831x_reg_read(wm831x, WM831X_GPIO_LEVEL);
  46. if (ret < 0)
  47. return ret;
  48. if (ret & 1 << offset)
  49. return 1;
  50. else
  51. return 0;
  52. }
  53. static int wm831x_gpio_direction_out(struct gpio_chip *chip,
  54. unsigned offset, int value)
  55. {
  56. struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
  57. struct wm831x *wm831x = wm831x_gpio->wm831x;
  58. return wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset,
  59. WM831X_GPN_DIR | WM831X_GPN_TRI, 0);
  60. }
  61. static void wm831x_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  62. {
  63. struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
  64. struct wm831x *wm831x = wm831x_gpio->wm831x;
  65. wm831x_set_bits(wm831x, WM831X_GPIO_LEVEL, 1 << offset,
  66. value << offset);
  67. }
  68. static int wm831x_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  69. {
  70. struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
  71. struct wm831x *wm831x = wm831x_gpio->wm831x;
  72. if (!wm831x->irq_base)
  73. return -EINVAL;
  74. return wm831x->irq_base + WM831X_IRQ_GPIO_1 + offset;
  75. }
  76. #ifdef CONFIG_DEBUG_FS
  77. static void wm831x_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  78. {
  79. struct wm831x_gpio *wm831x_gpio = to_wm831x_gpio(chip);
  80. struct wm831x *wm831x = wm831x_gpio->wm831x;
  81. int i;
  82. for (i = 0; i < chip->ngpio; i++) {
  83. int gpio = i + chip->base;
  84. int reg;
  85. const char *label, *pull, *powerdomain;
  86. /* We report the GPIO even if it's not requested since
  87. * we're also reporting things like alternate
  88. * functions which apply even when the GPIO is not in
  89. * use as a GPIO.
  90. */
  91. label = gpiochip_is_requested(chip, i);
  92. if (!label)
  93. label = "Unrequested";
  94. seq_printf(s, " gpio-%-3d (%-20.20s) ", gpio, label);
  95. reg = wm831x_reg_read(wm831x, WM831X_GPIO1_CONTROL + i);
  96. if (reg < 0) {
  97. dev_err(wm831x->dev,
  98. "GPIO control %d read failed: %d\n",
  99. gpio, reg);
  100. seq_printf(s, "\n");
  101. continue;
  102. }
  103. switch (reg & WM831X_GPN_PULL_MASK) {
  104. case WM831X_GPIO_PULL_NONE:
  105. pull = "nopull";
  106. break;
  107. case WM831X_GPIO_PULL_DOWN:
  108. pull = "pulldown";
  109. break;
  110. case WM831X_GPIO_PULL_UP:
  111. pull = "pullup";
  112. default:
  113. pull = "INVALID PULL";
  114. break;
  115. }
  116. switch (i + 1) {
  117. case 1 ... 3:
  118. case 7 ... 9:
  119. if (reg & WM831X_GPN_PWR_DOM)
  120. powerdomain = "VPMIC";
  121. else
  122. powerdomain = "DBVDD";
  123. break;
  124. case 4 ... 6:
  125. case 10 ... 12:
  126. if (reg & WM831X_GPN_PWR_DOM)
  127. powerdomain = "SYSVDD";
  128. else
  129. powerdomain = "DBVDD";
  130. break;
  131. case 13 ... 16:
  132. powerdomain = "TPVDD";
  133. break;
  134. default:
  135. BUG();
  136. break;
  137. }
  138. seq_printf(s, " %s %s %s %s%s\n"
  139. " %s%s (0x%4x)\n",
  140. reg & WM831X_GPN_DIR ? "in" : "out",
  141. wm831x_gpio_get(chip, i) ? "high" : "low",
  142. pull,
  143. powerdomain,
  144. reg & WM831X_GPN_POL ? " inverted" : "",
  145. reg & WM831X_GPN_OD ? "open-drain" : "CMOS",
  146. reg & WM831X_GPN_TRI ? " tristated" : "",
  147. reg);
  148. }
  149. }
  150. #else
  151. #define wm831x_gpio_dbg_show NULL
  152. #endif
  153. static struct gpio_chip template_chip = {
  154. .label = "wm831x",
  155. .owner = THIS_MODULE,
  156. .direction_input = wm831x_gpio_direction_in,
  157. .get = wm831x_gpio_get,
  158. .direction_output = wm831x_gpio_direction_out,
  159. .set = wm831x_gpio_set,
  160. .to_irq = wm831x_gpio_to_irq,
  161. .dbg_show = wm831x_gpio_dbg_show,
  162. .can_sleep = 1,
  163. };
  164. static int __devinit wm831x_gpio_probe(struct platform_device *pdev)
  165. {
  166. struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
  167. struct wm831x_pdata *pdata = wm831x->dev->platform_data;
  168. struct wm831x_gpio *wm831x_gpio;
  169. int ret;
  170. wm831x_gpio = kzalloc(sizeof(*wm831x_gpio), GFP_KERNEL);
  171. if (wm831x_gpio == NULL)
  172. return -ENOMEM;
  173. wm831x_gpio->wm831x = wm831x;
  174. wm831x_gpio->gpio_chip = template_chip;
  175. wm831x_gpio->gpio_chip.ngpio = wm831x->num_gpio;
  176. wm831x_gpio->gpio_chip.dev = &pdev->dev;
  177. if (pdata && pdata->gpio_base)
  178. wm831x_gpio->gpio_chip.base = pdata->gpio_base;
  179. else
  180. wm831x_gpio->gpio_chip.base = -1;
  181. ret = gpiochip_add(&wm831x_gpio->gpio_chip);
  182. if (ret < 0) {
  183. dev_err(&pdev->dev, "Could not register gpiochip, %d\n",
  184. ret);
  185. goto err;
  186. }
  187. platform_set_drvdata(pdev, wm831x_gpio);
  188. return ret;
  189. err:
  190. kfree(wm831x_gpio);
  191. return ret;
  192. }
  193. static int __devexit wm831x_gpio_remove(struct platform_device *pdev)
  194. {
  195. struct wm831x_gpio *wm831x_gpio = platform_get_drvdata(pdev);
  196. int ret;
  197. ret = gpiochip_remove(&wm831x_gpio->gpio_chip);
  198. if (ret == 0)
  199. kfree(wm831x_gpio);
  200. return ret;
  201. }
  202. static struct platform_driver wm831x_gpio_driver = {
  203. .driver.name = "wm831x-gpio",
  204. .driver.owner = THIS_MODULE,
  205. .probe = wm831x_gpio_probe,
  206. .remove = __devexit_p(wm831x_gpio_remove),
  207. };
  208. static int __init wm831x_gpio_init(void)
  209. {
  210. return platform_driver_register(&wm831x_gpio_driver);
  211. }
  212. subsys_initcall(wm831x_gpio_init);
  213. static void __exit wm831x_gpio_exit(void)
  214. {
  215. platform_driver_unregister(&wm831x_gpio_driver);
  216. }
  217. module_exit(wm831x_gpio_exit);
  218. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  219. MODULE_DESCRIPTION("GPIO interface for WM831x PMICs");
  220. MODULE_LICENSE("GPL");
  221. MODULE_ALIAS("platform:wm831x-gpio");