gpio-arizona.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * gpiolib support for Wolfson Arizona class devices
  4. *
  5. * Copyright 2012 Wolfson Microelectronics PLC.
  6. *
  7. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <linux/module.h>
  12. #include <linux/gpio/driver.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/seq_file.h>
  16. #include <linux/mfd/arizona/core.h>
  17. #include <linux/mfd/arizona/pdata.h>
  18. #include <linux/mfd/arizona/registers.h>
  19. struct arizona_gpio {
  20. struct arizona *arizona;
  21. struct gpio_chip gpio_chip;
  22. };
  23. static int arizona_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
  24. {
  25. struct arizona_gpio *arizona_gpio = gpiochip_get_data(chip);
  26. struct arizona *arizona = arizona_gpio->arizona;
  27. bool persistent = gpiochip_line_is_persistent(chip, offset);
  28. bool change;
  29. int ret;
  30. ret = regmap_update_bits_check(arizona->regmap,
  31. ARIZONA_GPIO1_CTRL + offset,
  32. ARIZONA_GPN_DIR, ARIZONA_GPN_DIR,
  33. &change);
  34. if (ret < 0)
  35. return ret;
  36. if (change && persistent) {
  37. pm_runtime_mark_last_busy(chip->parent);
  38. pm_runtime_put_autosuspend(chip->parent);
  39. }
  40. return 0;
  41. }
  42. static int arizona_gpio_get(struct gpio_chip *chip, unsigned offset)
  43. {
  44. struct arizona_gpio *arizona_gpio = gpiochip_get_data(chip);
  45. struct arizona *arizona = arizona_gpio->arizona;
  46. unsigned int reg, val;
  47. int ret;
  48. reg = ARIZONA_GPIO1_CTRL + offset;
  49. ret = regmap_read(arizona->regmap, reg, &val);
  50. if (ret < 0)
  51. return ret;
  52. /* Resume to read actual registers for input pins */
  53. if (val & ARIZONA_GPN_DIR) {
  54. ret = pm_runtime_get_sync(chip->parent);
  55. if (ret < 0) {
  56. dev_err(chip->parent, "Failed to resume: %d\n", ret);
  57. pm_runtime_put_autosuspend(chip->parent);
  58. return ret;
  59. }
  60. /* Register is cached, drop it to ensure a physical read */
  61. ret = regcache_drop_region(arizona->regmap, reg, reg);
  62. if (ret < 0) {
  63. dev_err(chip->parent, "Failed to drop cache: %d\n",
  64. ret);
  65. pm_runtime_put_autosuspend(chip->parent);
  66. return ret;
  67. }
  68. ret = regmap_read(arizona->regmap, reg, &val);
  69. if (ret < 0) {
  70. pm_runtime_put_autosuspend(chip->parent);
  71. return ret;
  72. }
  73. pm_runtime_mark_last_busy(chip->parent);
  74. pm_runtime_put_autosuspend(chip->parent);
  75. }
  76. if (val & ARIZONA_GPN_LVL)
  77. return 1;
  78. else
  79. return 0;
  80. }
  81. static int arizona_gpio_direction_out(struct gpio_chip *chip,
  82. unsigned offset, int value)
  83. {
  84. struct arizona_gpio *arizona_gpio = gpiochip_get_data(chip);
  85. struct arizona *arizona = arizona_gpio->arizona;
  86. bool persistent = gpiochip_line_is_persistent(chip, offset);
  87. unsigned int val;
  88. int ret;
  89. ret = regmap_read(arizona->regmap, ARIZONA_GPIO1_CTRL + offset, &val);
  90. if (ret < 0)
  91. return ret;
  92. if ((val & ARIZONA_GPN_DIR) && persistent) {
  93. ret = pm_runtime_get_sync(chip->parent);
  94. if (ret < 0) {
  95. dev_err(chip->parent, "Failed to resume: %d\n", ret);
  96. pm_runtime_put(chip->parent);
  97. return ret;
  98. }
  99. }
  100. if (value)
  101. value = ARIZONA_GPN_LVL;
  102. return regmap_update_bits(arizona->regmap, ARIZONA_GPIO1_CTRL + offset,
  103. ARIZONA_GPN_DIR | ARIZONA_GPN_LVL, value);
  104. }
  105. static void arizona_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  106. {
  107. struct arizona_gpio *arizona_gpio = gpiochip_get_data(chip);
  108. struct arizona *arizona = arizona_gpio->arizona;
  109. if (value)
  110. value = ARIZONA_GPN_LVL;
  111. regmap_update_bits(arizona->regmap, ARIZONA_GPIO1_CTRL + offset,
  112. ARIZONA_GPN_LVL, value);
  113. }
  114. static const struct gpio_chip template_chip = {
  115. .label = "arizona",
  116. .owner = THIS_MODULE,
  117. .direction_input = arizona_gpio_direction_in,
  118. .get = arizona_gpio_get,
  119. .direction_output = arizona_gpio_direction_out,
  120. .set = arizona_gpio_set,
  121. .can_sleep = true,
  122. };
  123. static int arizona_gpio_probe(struct platform_device *pdev)
  124. {
  125. struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
  126. struct arizona_pdata *pdata = &arizona->pdata;
  127. struct arizona_gpio *arizona_gpio;
  128. int ret;
  129. arizona_gpio = devm_kzalloc(&pdev->dev, sizeof(*arizona_gpio),
  130. GFP_KERNEL);
  131. if (!arizona_gpio)
  132. return -ENOMEM;
  133. arizona_gpio->arizona = arizona;
  134. arizona_gpio->gpio_chip = template_chip;
  135. arizona_gpio->gpio_chip.parent = &pdev->dev;
  136. #ifdef CONFIG_OF_GPIO
  137. arizona_gpio->gpio_chip.of_node = arizona->dev->of_node;
  138. #endif
  139. switch (arizona->type) {
  140. case WM5102:
  141. case WM5110:
  142. case WM8280:
  143. case WM8997:
  144. case WM8998:
  145. case WM1814:
  146. arizona_gpio->gpio_chip.ngpio = 5;
  147. break;
  148. case WM1831:
  149. case CS47L24:
  150. arizona_gpio->gpio_chip.ngpio = 2;
  151. break;
  152. default:
  153. dev_err(&pdev->dev, "Unknown chip variant %d\n",
  154. arizona->type);
  155. return -EINVAL;
  156. }
  157. if (pdata->gpio_base)
  158. arizona_gpio->gpio_chip.base = pdata->gpio_base;
  159. else
  160. arizona_gpio->gpio_chip.base = -1;
  161. pm_runtime_enable(&pdev->dev);
  162. ret = devm_gpiochip_add_data(&pdev->dev, &arizona_gpio->gpio_chip,
  163. arizona_gpio);
  164. if (ret < 0) {
  165. pm_runtime_disable(&pdev->dev);
  166. dev_err(&pdev->dev, "Could not register gpiochip, %d\n",
  167. ret);
  168. return ret;
  169. }
  170. return 0;
  171. }
  172. static struct platform_driver arizona_gpio_driver = {
  173. .driver.name = "arizona-gpio",
  174. .probe = arizona_gpio_probe,
  175. };
  176. module_platform_driver(arizona_gpio_driver);
  177. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  178. MODULE_DESCRIPTION("GPIO interface for Arizona devices");
  179. MODULE_LICENSE("GPL");
  180. MODULE_ALIAS("platform:arizona-gpio");