wm8350-gpiolib.c 4.5 KB

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