gpio-axp209.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * AXP20x GPIO driver
  3. *
  4. * Copyright (C) 2016 Maxime Ripard <maxime.ripard@free-electrons.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. */
  11. #include <linux/bitops.h>
  12. #include <linux/device.h>
  13. #include <linux/gpio/driver.h>
  14. #include <linux/init.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/kernel.h>
  17. #include <linux/mfd/axp20x.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/regmap.h>
  22. #include <linux/slab.h>
  23. #define AXP20X_GPIO_FUNCTIONS 0x7
  24. #define AXP20X_GPIO_FUNCTION_OUT_LOW 0
  25. #define AXP20X_GPIO_FUNCTION_OUT_HIGH 1
  26. #define AXP20X_GPIO_FUNCTION_INPUT 2
  27. struct axp20x_gpio {
  28. struct gpio_chip chip;
  29. struct regmap *regmap;
  30. };
  31. static int axp20x_gpio_get_reg(unsigned offset)
  32. {
  33. switch (offset) {
  34. case 0:
  35. return AXP20X_GPIO0_CTRL;
  36. case 1:
  37. return AXP20X_GPIO1_CTRL;
  38. case 2:
  39. return AXP20X_GPIO2_CTRL;
  40. }
  41. return -EINVAL;
  42. }
  43. static int axp20x_gpio_input(struct gpio_chip *chip, unsigned offset)
  44. {
  45. struct axp20x_gpio *gpio = gpiochip_get_data(chip);
  46. int reg;
  47. reg = axp20x_gpio_get_reg(offset);
  48. if (reg < 0)
  49. return reg;
  50. return regmap_update_bits(gpio->regmap, reg,
  51. AXP20X_GPIO_FUNCTIONS,
  52. AXP20X_GPIO_FUNCTION_INPUT);
  53. }
  54. static int axp20x_gpio_get(struct gpio_chip *chip, unsigned offset)
  55. {
  56. struct axp20x_gpio *gpio = gpiochip_get_data(chip);
  57. unsigned int val;
  58. int ret;
  59. ret = regmap_read(gpio->regmap, AXP20X_GPIO20_SS, &val);
  60. if (ret)
  61. return ret;
  62. return !!(val & BIT(offset + 4));
  63. }
  64. static int axp20x_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
  65. {
  66. struct axp20x_gpio *gpio = gpiochip_get_data(chip);
  67. unsigned int val;
  68. int reg, ret;
  69. reg = axp20x_gpio_get_reg(offset);
  70. if (reg < 0)
  71. return reg;
  72. ret = regmap_read(gpio->regmap, reg, &val);
  73. if (ret)
  74. return ret;
  75. /*
  76. * This shouldn't really happen if the pin is in use already,
  77. * or if it's not in use yet, it doesn't matter since we're
  78. * going to change the value soon anyway. Default to output.
  79. */
  80. if ((val & AXP20X_GPIO_FUNCTIONS) > 2)
  81. return 0;
  82. /*
  83. * The GPIO directions are the three lowest values.
  84. * 2 is input, 0 and 1 are output
  85. */
  86. return val & 2;
  87. }
  88. static int axp20x_gpio_output(struct gpio_chip *chip, unsigned offset,
  89. int value)
  90. {
  91. struct axp20x_gpio *gpio = gpiochip_get_data(chip);
  92. int reg;
  93. reg = axp20x_gpio_get_reg(offset);
  94. if (reg < 0)
  95. return reg;
  96. return regmap_update_bits(gpio->regmap, reg,
  97. AXP20X_GPIO_FUNCTIONS,
  98. value ? AXP20X_GPIO_FUNCTION_OUT_HIGH
  99. : AXP20X_GPIO_FUNCTION_OUT_LOW);
  100. }
  101. static void axp20x_gpio_set(struct gpio_chip *chip, unsigned offset,
  102. int value)
  103. {
  104. axp20x_gpio_output(chip, offset, value);
  105. }
  106. static int axp20x_gpio_probe(struct platform_device *pdev)
  107. {
  108. struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
  109. struct axp20x_gpio *gpio;
  110. int ret;
  111. if (!of_device_is_available(pdev->dev.of_node))
  112. return -ENODEV;
  113. if (!axp20x) {
  114. dev_err(&pdev->dev, "Parent drvdata not set\n");
  115. return -EINVAL;
  116. }
  117. gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
  118. if (!gpio)
  119. return -ENOMEM;
  120. gpio->chip.base = -1;
  121. gpio->chip.can_sleep = true;
  122. gpio->chip.parent = &pdev->dev;
  123. gpio->chip.label = dev_name(&pdev->dev);
  124. gpio->chip.owner = THIS_MODULE;
  125. gpio->chip.get = axp20x_gpio_get;
  126. gpio->chip.get_direction = axp20x_gpio_get_direction;
  127. gpio->chip.set = axp20x_gpio_set;
  128. gpio->chip.direction_input = axp20x_gpio_input;
  129. gpio->chip.direction_output = axp20x_gpio_output;
  130. gpio->chip.ngpio = 3;
  131. gpio->regmap = axp20x->regmap;
  132. ret = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
  133. if (ret) {
  134. dev_err(&pdev->dev, "Failed to register GPIO chip\n");
  135. return ret;
  136. }
  137. dev_info(&pdev->dev, "AXP209 GPIO driver loaded\n");
  138. return 0;
  139. }
  140. static const struct of_device_id axp20x_gpio_match[] = {
  141. { .compatible = "x-powers,axp209-gpio" },
  142. { }
  143. };
  144. MODULE_DEVICE_TABLE(of, axp20x_gpio_match);
  145. static struct platform_driver axp20x_gpio_driver = {
  146. .probe = axp20x_gpio_probe,
  147. .driver = {
  148. .name = "axp20x-gpio",
  149. .of_match_table = axp20x_gpio_match,
  150. },
  151. };
  152. module_platform_driver(axp20x_gpio_driver);
  153. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  154. MODULE_DESCRIPTION("AXP20x PMIC GPIO driver");
  155. MODULE_LICENSE("GPL");