gpio-arizona.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. return ret;
  63. }
  64. /* Register is cached, drop it to ensure a physical read */
  65. ret = regcache_drop_region(arizona->regmap, reg, reg);
  66. if (ret < 0) {
  67. dev_err(chip->parent, "Failed to drop cache: %d\n",
  68. ret);
  69. return ret;
  70. }
  71. ret = regmap_read(arizona->regmap, reg, &val);
  72. if (ret < 0)
  73. return ret;
  74. pm_runtime_mark_last_busy(chip->parent);
  75. pm_runtime_put_autosuspend(chip->parent);
  76. }
  77. if (val & ARIZONA_GPN_LVL)
  78. return 1;
  79. else
  80. return 0;
  81. }
  82. static int arizona_gpio_direction_out(struct gpio_chip *chip,
  83. unsigned offset, int value)
  84. {
  85. struct arizona_gpio *arizona_gpio = gpiochip_get_data(chip);
  86. struct arizona *arizona = arizona_gpio->arizona;
  87. bool persistent = gpiochip_line_is_persistent(chip, offset);
  88. unsigned int val;
  89. int ret;
  90. ret = regmap_read(arizona->regmap, ARIZONA_GPIO1_CTRL + offset, &val);
  91. if (ret < 0)
  92. return ret;
  93. if ((val & ARIZONA_GPN_DIR) && persistent) {
  94. ret = pm_runtime_get_sync(chip->parent);
  95. if (ret < 0) {
  96. dev_err(chip->parent, "Failed to resume: %d\n", ret);
  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 = dev_get_platdata(arizona->dev);
  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 && 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. dev_err(&pdev->dev, "Could not register gpiochip, %d\n",
  166. ret);
  167. return ret;
  168. }
  169. return 0;
  170. }
  171. static struct platform_driver arizona_gpio_driver = {
  172. .driver.name = "arizona-gpio",
  173. .probe = arizona_gpio_probe,
  174. };
  175. module_platform_driver(arizona_gpio_driver);
  176. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  177. MODULE_DESCRIPTION("GPIO interface for Arizona devices");
  178. MODULE_LICENSE("GPL");
  179. MODULE_ALIAS("platform:arizona-gpio");