wm8994-gpio.c 5.1 KB

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