wm8994-gpio.c 5.1 KB

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