gpio-arizona.c 5.5 KB

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