wm8994-gpio.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * wm8994-gpio.c -- 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. return wm8994_set_bits(wm8994, WM8994_GPIO_1 + offset,
  79. WM8994_GPN_DIR, 0);
  80. }
  81. static void wm8994_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  82. {
  83. struct wm8994_gpio *wm8994_gpio = to_wm8994_gpio(chip);
  84. struct wm8994 *wm8994 = wm8994_gpio->wm8994;
  85. if (value)
  86. value = WM8994_GPN_LVL;
  87. wm8994_set_bits(wm8994, WM8994_GPIO_1 + offset, WM8994_GPN_LVL, value);
  88. }
  89. static int wm8994_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  90. {
  91. struct wm8994_gpio *wm8994_gpio = to_wm8994_gpio(chip);
  92. struct wm8994 *wm8994 = wm8994_gpio->wm8994;
  93. if (!wm8994->irq_base)
  94. return -EINVAL;
  95. return wm8994->irq_base + offset;
  96. }
  97. #ifdef CONFIG_DEBUG_FS
  98. static void wm8994_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  99. {
  100. struct wm8994_gpio *wm8994_gpio = to_wm8994_gpio(chip);
  101. struct wm8994 *wm8994 = wm8994_gpio->wm8994;
  102. int i;
  103. for (i = 0; i < chip->ngpio; i++) {
  104. int gpio = i + chip->base;
  105. int reg;
  106. const char *label;
  107. /* We report the GPIO even if it's not requested since
  108. * we're also reporting things like alternate
  109. * functions which apply even when the GPIO is not in
  110. * use as a GPIO.
  111. */
  112. label = gpiochip_is_requested(chip, i);
  113. if (!label)
  114. label = "Unrequested";
  115. seq_printf(s, " gpio-%-3d (%-20.20s) ", gpio, label);
  116. reg = wm8994_reg_read(wm8994, WM8994_GPIO_1 + i);
  117. if (reg < 0) {
  118. dev_err(wm8994->dev,
  119. "GPIO control %d read failed: %d\n",
  120. gpio, reg);
  121. seq_printf(s, "\n");
  122. continue;
  123. }
  124. /* No decode yet; note that GPIO2 is special */
  125. seq_printf(s, "(%x)\n", reg);
  126. }
  127. }
  128. #else
  129. #define wm8994_gpio_dbg_show NULL
  130. #endif
  131. static struct gpio_chip template_chip = {
  132. .label = "wm8994",
  133. .owner = THIS_MODULE,
  134. .request = wm8994_gpio_request,
  135. .direction_input = wm8994_gpio_direction_in,
  136. .get = wm8994_gpio_get,
  137. .direction_output = wm8994_gpio_direction_out,
  138. .set = wm8994_gpio_set,
  139. .to_irq = wm8994_gpio_to_irq,
  140. .dbg_show = wm8994_gpio_dbg_show,
  141. .can_sleep = 1,
  142. };
  143. static int __devinit wm8994_gpio_probe(struct platform_device *pdev)
  144. {
  145. struct wm8994 *wm8994 = dev_get_drvdata(pdev->dev.parent);
  146. struct wm8994_pdata *pdata = wm8994->dev->platform_data;
  147. struct wm8994_gpio *wm8994_gpio;
  148. int ret;
  149. wm8994_gpio = kzalloc(sizeof(*wm8994_gpio), GFP_KERNEL);
  150. if (wm8994_gpio == NULL)
  151. return -ENOMEM;
  152. wm8994_gpio->wm8994 = wm8994;
  153. wm8994_gpio->gpio_chip = template_chip;
  154. wm8994_gpio->gpio_chip.ngpio = WM8994_GPIO_MAX;
  155. wm8994_gpio->gpio_chip.dev = &pdev->dev;
  156. if (pdata && pdata->gpio_base)
  157. wm8994_gpio->gpio_chip.base = pdata->gpio_base;
  158. else
  159. wm8994_gpio->gpio_chip.base = -1;
  160. ret = gpiochip_add(&wm8994_gpio->gpio_chip);
  161. if (ret < 0) {
  162. dev_err(&pdev->dev, "Could not register gpiochip, %d\n",
  163. ret);
  164. goto err;
  165. }
  166. platform_set_drvdata(pdev, wm8994_gpio);
  167. return ret;
  168. err:
  169. kfree(wm8994_gpio);
  170. return ret;
  171. }
  172. static int __devexit wm8994_gpio_remove(struct platform_device *pdev)
  173. {
  174. struct wm8994_gpio *wm8994_gpio = platform_get_drvdata(pdev);
  175. int ret;
  176. ret = gpiochip_remove(&wm8994_gpio->gpio_chip);
  177. if (ret == 0)
  178. kfree(wm8994_gpio);
  179. return ret;
  180. }
  181. static struct platform_driver wm8994_gpio_driver = {
  182. .driver.name = "wm8994-gpio",
  183. .driver.owner = THIS_MODULE,
  184. .probe = wm8994_gpio_probe,
  185. .remove = __devexit_p(wm8994_gpio_remove),
  186. };
  187. static int __init wm8994_gpio_init(void)
  188. {
  189. return platform_driver_register(&wm8994_gpio_driver);
  190. }
  191. subsys_initcall(wm8994_gpio_init);
  192. static void __exit wm8994_gpio_exit(void)
  193. {
  194. platform_driver_unregister(&wm8994_gpio_driver);
  195. }
  196. module_exit(wm8994_gpio_exit);
  197. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  198. MODULE_DESCRIPTION("GPIO interface for WM8994");
  199. MODULE_LICENSE("GPL");
  200. MODULE_ALIAS("platform:wm8994-gpio");