gpio-axp209.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * AXP20x GPIO driver
  3. *
  4. * Copyright (C) 2016 Maxime Ripard <[email protected]>
  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 reg, ret;
  59. reg = axp20x_gpio_get_reg(offset);
  60. if (reg < 0)
  61. return reg;
  62. ret = regmap_read(gpio->regmap, reg, &val);
  63. if (ret)
  64. return ret;
  65. return !!(val & BIT(offset + 4));
  66. }
  67. static int axp20x_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
  68. {
  69. struct axp20x_gpio *gpio = gpiochip_get_data(chip);
  70. unsigned int val;
  71. int reg, ret;
  72. reg = axp20x_gpio_get_reg(offset);
  73. if (reg < 0)
  74. return reg;
  75. ret = regmap_read(gpio->regmap, reg, &val);
  76. if (ret)
  77. return ret;
  78. /*
  79. * This shouldn't really happen if the pin is in use already,
  80. * or if it's not in use yet, it doesn't matter since we're
  81. * going to change the value soon anyway. Default to output.
  82. */
  83. if ((val & AXP20X_GPIO_FUNCTIONS) > 2)
  84. return 0;
  85. /*
  86. * The GPIO directions are the three lowest values.
  87. * 2 is input, 0 and 1 are output
  88. */
  89. return val & 2;
  90. }
  91. static int axp20x_gpio_output(struct gpio_chip *chip, unsigned offset,
  92. int value)
  93. {
  94. struct axp20x_gpio *gpio = gpiochip_get_data(chip);
  95. int reg;
  96. reg = axp20x_gpio_get_reg(offset);
  97. if (reg < 0)
  98. return reg;
  99. return regmap_update_bits(gpio->regmap, reg,
  100. AXP20X_GPIO_FUNCTIONS,
  101. value ? AXP20X_GPIO_FUNCTION_OUT_HIGH
  102. : AXP20X_GPIO_FUNCTION_OUT_LOW);
  103. }
  104. static void axp20x_gpio_set(struct gpio_chip *chip, unsigned offset,
  105. int value)
  106. {
  107. axp20x_gpio_output(chip, offset, value);
  108. }
  109. static int axp20x_gpio_probe(struct platform_device *pdev)
  110. {
  111. struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
  112. struct axp20x_gpio *gpio;
  113. int ret;
  114. if (!of_device_is_available(pdev->dev.of_node))
  115. return -ENODEV;
  116. if (!axp20x) {
  117. dev_err(&pdev->dev, "Parent drvdata not set\n");
  118. return -EINVAL;
  119. }
  120. gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
  121. if (!gpio)
  122. return -ENOMEM;
  123. gpio->chip.base = -1;
  124. gpio->chip.can_sleep = true;
  125. gpio->chip.parent = &pdev->dev;
  126. gpio->chip.label = dev_name(&pdev->dev);
  127. gpio->chip.owner = THIS_MODULE;
  128. gpio->chip.get = axp20x_gpio_get;
  129. gpio->chip.get_direction = axp20x_gpio_get_direction;
  130. gpio->chip.set = axp20x_gpio_set;
  131. gpio->chip.direction_input = axp20x_gpio_input;
  132. gpio->chip.direction_output = axp20x_gpio_output;
  133. gpio->chip.ngpio = 3;
  134. gpio->regmap = axp20x->regmap;
  135. ret = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
  136. if (ret) {
  137. dev_err(&pdev->dev, "Failed to register GPIO chip\n");
  138. return ret;
  139. }
  140. dev_info(&pdev->dev, "AXP209 GPIO driver loaded\n");
  141. return 0;
  142. }
  143. static const struct of_device_id axp20x_gpio_match[] = {
  144. { .compatible = "x-powers,axp209-gpio" },
  145. { }
  146. };
  147. MODULE_DEVICE_TABLE(of, axp20x_gpio_match);
  148. static struct platform_driver axp20x_gpio_driver = {
  149. .probe = axp20x_gpio_probe,
  150. .driver = {
  151. .name = "axp20x-gpio",
  152. .of_match_table = axp20x_gpio_match,
  153. },
  154. };
  155. module_platform_driver(axp20x_gpio_driver);
  156. MODULE_AUTHOR("Maxime Ripard <[email protected]>");
  157. MODULE_DESCRIPTION("AXP20x PMIC GPIO driver");
  158. MODULE_LICENSE("GPL");