gpio-arizona.c 4.6 KB

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