gpio-tps68470.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 <antti.laakso@intel.com>
  9. * Tianshu Qiu <tian.shu.qiu@intel.com>
  10. * Jian Xu Zheng <jian.xu.zheng@intel.com>
  11. * Yuning Pu <yuning.pu@intel.com>
  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. /* rest are always outputs */
  80. if (offset >= TPS68470_N_REGULAR_GPIO)
  81. return 0;
  82. /* Set the initial value */
  83. tps68470_gpio_set(gc, offset, value);
  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. int ret;
  107. tps68470_gpio = devm_kzalloc(&pdev->dev, sizeof(*tps68470_gpio),
  108. GFP_KERNEL);
  109. if (!tps68470_gpio)
  110. return -ENOMEM;
  111. tps68470_gpio->tps68470_regmap = dev_get_drvdata(pdev->dev.parent);
  112. tps68470_gpio->gc.label = "tps68470-gpio";
  113. tps68470_gpio->gc.owner = THIS_MODULE;
  114. tps68470_gpio->gc.direction_input = tps68470_gpio_input;
  115. tps68470_gpio->gc.direction_output = tps68470_gpio_output;
  116. tps68470_gpio->gc.get = tps68470_gpio_get;
  117. tps68470_gpio->gc.get_direction = tps68470_gpio_get_direction;
  118. tps68470_gpio->gc.set = tps68470_gpio_set;
  119. tps68470_gpio->gc.can_sleep = true;
  120. tps68470_gpio->gc.names = tps68470_names;
  121. tps68470_gpio->gc.ngpio = TPS68470_N_GPIO;
  122. tps68470_gpio->gc.base = -1;
  123. tps68470_gpio->gc.parent = &pdev->dev;
  124. ret = devm_gpiochip_add_data(&pdev->dev, &tps68470_gpio->gc,
  125. tps68470_gpio);
  126. if (ret < 0) {
  127. dev_err(&pdev->dev, "Failed to register gpio_chip: %d\n", ret);
  128. return ret;
  129. }
  130. platform_set_drvdata(pdev, tps68470_gpio);
  131. return ret;
  132. }
  133. static struct platform_driver tps68470_gpio_driver = {
  134. .driver = {
  135. .name = "tps68470-gpio",
  136. },
  137. .probe = tps68470_gpio_probe,
  138. };
  139. builtin_platform_driver(tps68470_gpio_driver)