gpio-tps68470.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * GPIO driver for TPS68470 PMIC
  4. *
  5. * Copyright (C) 2017 Intel Corporation
  6. *
  7. * Authors:
  8. * Antti Laakso <[email protected]>
  9. * Tianshu Qiu <[email protected]>
  10. * Jian Xu Zheng <[email protected]>
  11. * Yuning Pu <[email protected]>
  12. */
  13. #include <linux/gpio/driver.h>
  14. #include <linux/mfd/tps68470.h>
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/regmap.h>
  18. #define TPS68470_N_LOGIC_OUTPUT 3
  19. #define TPS68470_N_REGULAR_GPIO 7
  20. #define TPS68470_N_GPIO (TPS68470_N_LOGIC_OUTPUT + TPS68470_N_REGULAR_GPIO)
  21. struct tps68470_gpio_data {
  22. struct regmap *tps68470_regmap;
  23. struct gpio_chip gc;
  24. };
  25. static int tps68470_gpio_get(struct gpio_chip *gc, unsigned int offset)
  26. {
  27. struct tps68470_gpio_data *tps68470_gpio = gpiochip_get_data(gc);
  28. struct regmap *regmap = tps68470_gpio->tps68470_regmap;
  29. unsigned int reg = TPS68470_REG_GPDO;
  30. int val, ret;
  31. if (offset >= TPS68470_N_REGULAR_GPIO) {
  32. offset -= TPS68470_N_REGULAR_GPIO;
  33. reg = TPS68470_REG_SGPO;
  34. }
  35. ret = regmap_read(regmap, reg, &val);
  36. if (ret) {
  37. dev_err(tps68470_gpio->gc.parent, "reg 0x%x read failed\n",
  38. TPS68470_REG_SGPO);
  39. return ret;
  40. }
  41. return !!(val & BIT(offset));
  42. }
  43. static int tps68470_gpio_get_direction(struct gpio_chip *gc,
  44. unsigned int offset)
  45. {
  46. struct tps68470_gpio_data *tps68470_gpio = gpiochip_get_data(gc);
  47. struct regmap *regmap = tps68470_gpio->tps68470_regmap;
  48. int val, ret;
  49. /* rest are always outputs */
  50. if (offset >= TPS68470_N_REGULAR_GPIO)
  51. return GPIO_LINE_DIRECTION_OUT;
  52. ret = regmap_read(regmap, TPS68470_GPIO_CTL_REG_A(offset), &val);
  53. if (ret) {
  54. dev_err(tps68470_gpio->gc.parent, "reg 0x%x read failed\n",
  55. TPS68470_GPIO_CTL_REG_A(offset));
  56. return ret;
  57. }
  58. val &= TPS68470_GPIO_MODE_MASK;
  59. return val >= TPS68470_GPIO_MODE_OUT_CMOS ? GPIO_LINE_DIRECTION_OUT :
  60. GPIO_LINE_DIRECTION_IN;
  61. }
  62. static void tps68470_gpio_set(struct gpio_chip *gc, unsigned int offset,
  63. int value)
  64. {
  65. struct tps68470_gpio_data *tps68470_gpio = gpiochip_get_data(gc);
  66. struct regmap *regmap = tps68470_gpio->tps68470_regmap;
  67. unsigned int reg = TPS68470_REG_GPDO;
  68. if (offset >= TPS68470_N_REGULAR_GPIO) {
  69. reg = TPS68470_REG_SGPO;
  70. offset -= TPS68470_N_REGULAR_GPIO;
  71. }
  72. regmap_update_bits(regmap, reg, BIT(offset), value ? BIT(offset) : 0);
  73. }
  74. static int tps68470_gpio_output(struct gpio_chip *gc, unsigned int offset,
  75. int value)
  76. {
  77. struct tps68470_gpio_data *tps68470_gpio = gpiochip_get_data(gc);
  78. struct regmap *regmap = tps68470_gpio->tps68470_regmap;
  79. /* Set the initial value */
  80. tps68470_gpio_set(gc, offset, value);
  81. /* rest are always outputs */
  82. if (offset >= TPS68470_N_REGULAR_GPIO)
  83. return 0;
  84. return regmap_update_bits(regmap, TPS68470_GPIO_CTL_REG_A(offset),
  85. TPS68470_GPIO_MODE_MASK,
  86. TPS68470_GPIO_MODE_OUT_CMOS);
  87. }
  88. static int tps68470_gpio_input(struct gpio_chip *gc, unsigned int offset)
  89. {
  90. struct tps68470_gpio_data *tps68470_gpio = gpiochip_get_data(gc);
  91. struct regmap *regmap = tps68470_gpio->tps68470_regmap;
  92. /* rest are always outputs */
  93. if (offset >= TPS68470_N_REGULAR_GPIO)
  94. return -EINVAL;
  95. return regmap_update_bits(regmap, TPS68470_GPIO_CTL_REG_A(offset),
  96. TPS68470_GPIO_MODE_MASK, 0x00);
  97. }
  98. static const char *tps68470_names[TPS68470_N_GPIO] = {
  99. "gpio.0", "gpio.1", "gpio.2", "gpio.3",
  100. "gpio.4", "gpio.5", "gpio.6",
  101. "s_enable", "s_idle", "s_resetn",
  102. };
  103. static int tps68470_gpio_probe(struct platform_device *pdev)
  104. {
  105. struct tps68470_gpio_data *tps68470_gpio;
  106. tps68470_gpio = devm_kzalloc(&pdev->dev, sizeof(*tps68470_gpio),
  107. GFP_KERNEL);
  108. if (!tps68470_gpio)
  109. return -ENOMEM;
  110. tps68470_gpio->tps68470_regmap = dev_get_drvdata(pdev->dev.parent);
  111. tps68470_gpio->gc.label = "tps68470-gpio";
  112. tps68470_gpio->gc.owner = THIS_MODULE;
  113. tps68470_gpio->gc.direction_input = tps68470_gpio_input;
  114. tps68470_gpio->gc.direction_output = tps68470_gpio_output;
  115. tps68470_gpio->gc.get = tps68470_gpio_get;
  116. tps68470_gpio->gc.get_direction = tps68470_gpio_get_direction;
  117. tps68470_gpio->gc.set = tps68470_gpio_set;
  118. tps68470_gpio->gc.can_sleep = true;
  119. tps68470_gpio->gc.names = tps68470_names;
  120. tps68470_gpio->gc.ngpio = TPS68470_N_GPIO;
  121. tps68470_gpio->gc.base = -1;
  122. tps68470_gpio->gc.parent = &pdev->dev;
  123. return devm_gpiochip_add_data(&pdev->dev, &tps68470_gpio->gc, tps68470_gpio);
  124. }
  125. static struct platform_driver tps68470_gpio_driver = {
  126. .driver = {
  127. .name = "tps68470-gpio",
  128. },
  129. .probe = tps68470_gpio_probe,
  130. };
  131. module_platform_driver(tps68470_gpio_driver);
  132. MODULE_ALIAS("platform:tps68470-gpio");
  133. MODULE_DESCRIPTION("GPIO driver for TPS68470 PMIC");
  134. MODULE_LICENSE("GPL v2");