gpio-wm8350.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * 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 = true,
  91. };
  92. static int wm8350_gpio_probe(struct platform_device *pdev)
  93. {
  94. struct wm8350 *wm8350 = dev_get_drvdata(pdev->dev.parent);
  95. struct wm8350_platform_data *pdata = dev_get_platdata(wm8350->dev);
  96. struct wm8350_gpio_data *wm8350_gpio;
  97. int ret;
  98. wm8350_gpio = devm_kzalloc(&pdev->dev, sizeof(*wm8350_gpio),
  99. GFP_KERNEL);
  100. if (wm8350_gpio == NULL)
  101. return -ENOMEM;
  102. wm8350_gpio->wm8350 = wm8350;
  103. wm8350_gpio->gpio_chip = template_chip;
  104. wm8350_gpio->gpio_chip.ngpio = 13;
  105. wm8350_gpio->gpio_chip.dev = &pdev->dev;
  106. if (pdata && pdata->gpio_base)
  107. wm8350_gpio->gpio_chip.base = pdata->gpio_base;
  108. else
  109. wm8350_gpio->gpio_chip.base = -1;
  110. ret = gpiochip_add(&wm8350_gpio->gpio_chip);
  111. if (ret < 0) {
  112. dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
  113. return ret;
  114. }
  115. platform_set_drvdata(pdev, wm8350_gpio);
  116. return ret;
  117. }
  118. static int wm8350_gpio_remove(struct platform_device *pdev)
  119. {
  120. struct wm8350_gpio_data *wm8350_gpio = platform_get_drvdata(pdev);
  121. gpiochip_remove(&wm8350_gpio->gpio_chip);
  122. return 0;
  123. }
  124. static struct platform_driver wm8350_gpio_driver = {
  125. .driver.name = "wm8350-gpio",
  126. .driver.owner = THIS_MODULE,
  127. .probe = wm8350_gpio_probe,
  128. .remove = wm8350_gpio_remove,
  129. };
  130. static int __init wm8350_gpio_init(void)
  131. {
  132. return platform_driver_register(&wm8350_gpio_driver);
  133. }
  134. subsys_initcall(wm8350_gpio_init);
  135. static void __exit wm8350_gpio_exit(void)
  136. {
  137. platform_driver_unregister(&wm8350_gpio_driver);
  138. }
  139. module_exit(wm8350_gpio_exit);
  140. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  141. MODULE_DESCRIPTION("GPIO interface for WM8350 PMICs");
  142. MODULE_LICENSE("GPL");
  143. MODULE_ALIAS("platform:wm8350-gpio");