wm831x-gpio.c 6.0 KB

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