gpio-wm8350.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * gpiolib support for Wolfson WM835x PMICs
  4. *
  5. * Copyright 2009 Wolfson Microelectronics PLC.
  6. *
  7. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  8. *
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/slab.h>
  12. #include <linux/module.h>
  13. #include <linux/gpio/driver.h>
  14. #include <linux/mfd/core.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/mfd/wm8350/core.h>
  18. #include <linux/mfd/wm8350/gpio.h>
  19. struct wm8350_gpio_data {
  20. struct wm8350 *wm8350;
  21. struct gpio_chip gpio_chip;
  22. };
  23. static int wm8350_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
  24. {
  25. struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip);
  26. struct wm8350 *wm8350 = wm8350_gpio->wm8350;
  27. return wm8350_set_bits(wm8350, WM8350_GPIO_CONFIGURATION_I_O,
  28. 1 << offset);
  29. }
  30. static int wm8350_gpio_get(struct gpio_chip *chip, unsigned offset)
  31. {
  32. struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip);
  33. struct wm8350 *wm8350 = wm8350_gpio->wm8350;
  34. int ret;
  35. ret = wm8350_reg_read(wm8350, WM8350_GPIO_LEVEL);
  36. if (ret < 0)
  37. return ret;
  38. if (ret & (1 << offset))
  39. return 1;
  40. else
  41. return 0;
  42. }
  43. static void wm8350_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  44. {
  45. struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip);
  46. struct wm8350 *wm8350 = wm8350_gpio->wm8350;
  47. if (value)
  48. wm8350_set_bits(wm8350, WM8350_GPIO_LEVEL, 1 << offset);
  49. else
  50. wm8350_clear_bits(wm8350, WM8350_GPIO_LEVEL, 1 << offset);
  51. }
  52. static int wm8350_gpio_direction_out(struct gpio_chip *chip,
  53. unsigned offset, int value)
  54. {
  55. struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip);
  56. struct wm8350 *wm8350 = wm8350_gpio->wm8350;
  57. int ret;
  58. ret = wm8350_clear_bits(wm8350, WM8350_GPIO_CONFIGURATION_I_O,
  59. 1 << offset);
  60. if (ret < 0)
  61. return ret;
  62. /* Don't have an atomic direction/value setup */
  63. wm8350_gpio_set(chip, offset, value);
  64. return 0;
  65. }
  66. static int wm8350_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  67. {
  68. struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip);
  69. struct wm8350 *wm8350 = wm8350_gpio->wm8350;
  70. if (!wm8350->irq_base)
  71. return -EINVAL;
  72. return wm8350->irq_base + WM8350_IRQ_GPIO(offset);
  73. }
  74. static const struct gpio_chip template_chip = {
  75. .label = "wm8350",
  76. .owner = THIS_MODULE,
  77. .direction_input = wm8350_gpio_direction_in,
  78. .get = wm8350_gpio_get,
  79. .direction_output = wm8350_gpio_direction_out,
  80. .set = wm8350_gpio_set,
  81. .to_irq = wm8350_gpio_to_irq,
  82. .can_sleep = true,
  83. };
  84. static int wm8350_gpio_probe(struct platform_device *pdev)
  85. {
  86. struct wm8350 *wm8350 = dev_get_drvdata(pdev->dev.parent);
  87. struct wm8350_platform_data *pdata = dev_get_platdata(wm8350->dev);
  88. struct wm8350_gpio_data *wm8350_gpio;
  89. int ret;
  90. wm8350_gpio = devm_kzalloc(&pdev->dev, sizeof(*wm8350_gpio),
  91. GFP_KERNEL);
  92. if (wm8350_gpio == NULL)
  93. return -ENOMEM;
  94. wm8350_gpio->wm8350 = wm8350;
  95. wm8350_gpio->gpio_chip = template_chip;
  96. wm8350_gpio->gpio_chip.ngpio = 13;
  97. wm8350_gpio->gpio_chip.parent = &pdev->dev;
  98. if (pdata && pdata->gpio_base)
  99. wm8350_gpio->gpio_chip.base = pdata->gpio_base;
  100. else
  101. wm8350_gpio->gpio_chip.base = -1;
  102. ret = devm_gpiochip_add_data(&pdev->dev, &wm8350_gpio->gpio_chip,
  103. wm8350_gpio);
  104. if (ret < 0) {
  105. dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
  106. return ret;
  107. }
  108. platform_set_drvdata(pdev, wm8350_gpio);
  109. return ret;
  110. }
  111. static struct platform_driver wm8350_gpio_driver = {
  112. .driver.name = "wm8350-gpio",
  113. .probe = wm8350_gpio_probe,
  114. };
  115. static int __init wm8350_gpio_init(void)
  116. {
  117. return platform_driver_register(&wm8350_gpio_driver);
  118. }
  119. subsys_initcall(wm8350_gpio_init);
  120. static void __exit wm8350_gpio_exit(void)
  121. {
  122. platform_driver_unregister(&wm8350_gpio_driver);
  123. }
  124. module_exit(wm8350_gpio_exit);
  125. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  126. MODULE_DESCRIPTION("GPIO interface for WM8350 PMICs");
  127. MODULE_LICENSE("GPL");
  128. MODULE_ALIAS("platform:wm8350-gpio");