wm8350-gpiolib.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * wm835x-gpiolib.c -- gpiolib support for Wolfson WM835x 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/wm8350/core.h>
  21. #include <linux/mfd/wm8350/gpio.h>
  22. struct wm8350_gpio_data {
  23. struct wm8350 *wm8350;
  24. struct gpio_chip gpio_chip;
  25. };
  26. static inline struct wm8350_gpio_data *to_wm8350_gpio(struct gpio_chip *chip)
  27. {
  28. return container_of(chip, struct wm8350_gpio_data, gpio_chip);
  29. }
  30. static int wm8350_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
  31. {
  32. struct wm8350_gpio_data *wm8350_gpio = to_wm8350_gpio(chip);
  33. struct wm8350 *wm8350 = wm8350_gpio->wm8350;
  34. return wm8350_set_bits(wm8350, WM8350_GPIO_CONFIGURATION_I_O,
  35. 1 << offset);
  36. }
  37. static int wm8350_gpio_get(struct gpio_chip *chip, unsigned offset)
  38. {
  39. struct wm8350_gpio_data *wm8350_gpio = to_wm8350_gpio(chip);
  40. struct wm8350 *wm8350 = wm8350_gpio->wm8350;
  41. int ret;
  42. ret = wm8350_reg_read(wm8350, WM8350_GPIO_LEVEL);
  43. if (ret < 0)
  44. return ret;
  45. if (ret & (1 << offset))
  46. return 1;
  47. else
  48. return 0;
  49. }
  50. static void wm8350_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  51. {
  52. struct wm8350_gpio_data *wm8350_gpio = to_wm8350_gpio(chip);
  53. struct wm8350 *wm8350 = wm8350_gpio->wm8350;
  54. if (value)
  55. wm8350_set_bits(wm8350, WM8350_GPIO_LEVEL, 1 << offset);
  56. else
  57. wm8350_clear_bits(wm8350, WM8350_GPIO_LEVEL, 1 << offset);
  58. }
  59. static int wm8350_gpio_direction_out(struct gpio_chip *chip,
  60. unsigned offset, int value)
  61. {
  62. struct wm8350_gpio_data *wm8350_gpio = to_wm8350_gpio(chip);
  63. struct wm8350 *wm8350 = wm8350_gpio->wm8350;
  64. int ret;
  65. ret = wm8350_clear_bits(wm8350, WM8350_GPIO_CONFIGURATION_I_O,
  66. 1 << offset);
  67. if (ret < 0)
  68. return ret;
  69. /* Don't have an atomic direction/value setup */
  70. wm8350_gpio_set(chip, offset, value);
  71. return 0;
  72. }
  73. static int wm8350_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  74. {
  75. struct wm8350_gpio_data *wm8350_gpio = to_wm8350_gpio(chip);
  76. struct wm8350 *wm8350 = wm8350_gpio->wm8350;
  77. if (!wm8350->irq_base)
  78. return -EINVAL;
  79. return wm8350->irq_base + WM8350_IRQ_GPIO(offset);
  80. }
  81. static struct gpio_chip template_chip = {
  82. .label = "wm8350",
  83. .owner = THIS_MODULE,
  84. .direction_input = wm8350_gpio_direction_in,
  85. .get = wm8350_gpio_get,
  86. .direction_output = wm8350_gpio_direction_out,
  87. .set = wm8350_gpio_set,
  88. .to_irq = wm8350_gpio_to_irq,
  89. .can_sleep = 1,
  90. };
  91. static int __devinit wm8350_gpio_probe(struct platform_device *pdev)
  92. {
  93. struct wm8350 *wm8350 = dev_get_drvdata(pdev->dev.parent);
  94. struct wm8350_platform_data *pdata = wm8350->dev->platform_data;
  95. struct wm8350_gpio_data *wm8350_gpio;
  96. int ret;
  97. wm8350_gpio = kzalloc(sizeof(*wm8350_gpio), GFP_KERNEL);
  98. if (wm8350_gpio == NULL)
  99. return -ENOMEM;
  100. wm8350_gpio->wm8350 = wm8350;
  101. wm8350_gpio->gpio_chip = template_chip;
  102. wm8350_gpio->gpio_chip.ngpio = 13;
  103. wm8350_gpio->gpio_chip.dev = &pdev->dev;
  104. if (pdata && pdata->gpio_base)
  105. wm8350_gpio->gpio_chip.base = pdata->gpio_base;
  106. else
  107. wm8350_gpio->gpio_chip.base = -1;
  108. ret = gpiochip_add(&wm8350_gpio->gpio_chip);
  109. if (ret < 0) {
  110. dev_err(&pdev->dev, "Could not register gpiochip, %d\n",
  111. ret);
  112. goto err;
  113. }
  114. platform_set_drvdata(pdev, wm8350_gpio);
  115. return ret;
  116. err:
  117. kfree(wm8350_gpio);
  118. return ret;
  119. }
  120. static int __devexit wm8350_gpio_remove(struct platform_device *pdev)
  121. {
  122. struct wm8350_gpio_data *wm8350_gpio = platform_get_drvdata(pdev);
  123. int ret;
  124. ret = gpiochip_remove(&wm8350_gpio->gpio_chip);
  125. if (ret == 0)
  126. kfree(wm8350_gpio);
  127. return ret;
  128. }
  129. static struct platform_driver wm8350_gpio_driver = {
  130. .driver.name = "wm8350-gpio",
  131. .driver.owner = THIS_MODULE,
  132. .probe = wm8350_gpio_probe,
  133. .remove = __devexit_p(wm8350_gpio_remove),
  134. };
  135. static int __init wm8350_gpio_init(void)
  136. {
  137. return platform_driver_register(&wm8350_gpio_driver);
  138. }
  139. subsys_initcall(wm8350_gpio_init);
  140. static void __exit wm8350_gpio_exit(void)
  141. {
  142. platform_driver_unregister(&wm8350_gpio_driver);
  143. }
  144. module_exit(wm8350_gpio_exit);
  145. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  146. MODULE_DESCRIPTION("GPIO interface for WM8350 PMICs");
  147. MODULE_LICENSE("GPL");
  148. MODULE_ALIAS("platform:wm8350-gpio");