wm8994-gpio.c 5.4 KB

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