gpio-tps65218.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2015 Verifone Int.
  4. *
  5. * Author: Nicolas Saenz Julienne <[email protected]>
  6. *
  7. * This driver is based on the gpio-tps65912 implementation.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/errno.h>
  12. #include <linux/gpio/driver.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/regmap.h>
  15. #include <linux/mfd/tps65218.h>
  16. struct tps65218_gpio {
  17. struct tps65218 *tps65218;
  18. struct gpio_chip gpio_chip;
  19. };
  20. static int tps65218_gpio_get(struct gpio_chip *gc, unsigned offset)
  21. {
  22. struct tps65218_gpio *tps65218_gpio = gpiochip_get_data(gc);
  23. struct tps65218 *tps65218 = tps65218_gpio->tps65218;
  24. unsigned int val;
  25. int ret;
  26. ret = regmap_read(tps65218->regmap, TPS65218_REG_ENABLE2, &val);
  27. if (ret)
  28. return ret;
  29. return !!(val & (TPS65218_ENABLE2_GPIO1 << offset));
  30. }
  31. static void tps65218_gpio_set(struct gpio_chip *gc, unsigned offset,
  32. int value)
  33. {
  34. struct tps65218_gpio *tps65218_gpio = gpiochip_get_data(gc);
  35. struct tps65218 *tps65218 = tps65218_gpio->tps65218;
  36. if (value)
  37. tps65218_set_bits(tps65218, TPS65218_REG_ENABLE2,
  38. TPS65218_ENABLE2_GPIO1 << offset,
  39. TPS65218_ENABLE2_GPIO1 << offset,
  40. TPS65218_PROTECT_L1);
  41. else
  42. tps65218_clear_bits(tps65218, TPS65218_REG_ENABLE2,
  43. TPS65218_ENABLE2_GPIO1 << offset,
  44. TPS65218_PROTECT_L1);
  45. }
  46. static int tps65218_gpio_output(struct gpio_chip *gc, unsigned offset,
  47. int value)
  48. {
  49. /* Only drives GPOs */
  50. tps65218_gpio_set(gc, offset, value);
  51. return 0;
  52. }
  53. static int tps65218_gpio_input(struct gpio_chip *gc, unsigned offset)
  54. {
  55. return -EPERM;
  56. }
  57. static int tps65218_gpio_request(struct gpio_chip *gc, unsigned offset)
  58. {
  59. struct tps65218_gpio *tps65218_gpio = gpiochip_get_data(gc);
  60. struct tps65218 *tps65218 = tps65218_gpio->tps65218;
  61. int ret;
  62. if (gpiochip_line_is_open_source(gc, offset)) {
  63. dev_err(gc->parent, "can't work as open source\n");
  64. return -EINVAL;
  65. }
  66. switch (offset) {
  67. case 0:
  68. if (!gpiochip_line_is_open_drain(gc, offset)) {
  69. dev_err(gc->parent, "GPO1 works only as open drain\n");
  70. return -EINVAL;
  71. }
  72. /* Disable sequencer for GPO1 */
  73. ret = tps65218_clear_bits(tps65218, TPS65218_REG_SEQ7,
  74. TPS65218_SEQ7_GPO1_SEQ_MASK,
  75. TPS65218_PROTECT_L1);
  76. if (ret)
  77. return ret;
  78. /* Setup GPO1 */
  79. ret = tps65218_clear_bits(tps65218, TPS65218_REG_CONFIG1,
  80. TPS65218_CONFIG1_IO1_SEL,
  81. TPS65218_PROTECT_L1);
  82. if (ret)
  83. return ret;
  84. break;
  85. case 1:
  86. /* Setup GPO2 */
  87. ret = tps65218_clear_bits(tps65218, TPS65218_REG_CONFIG1,
  88. TPS65218_CONFIG1_IO1_SEL,
  89. TPS65218_PROTECT_L1);
  90. if (ret)
  91. return ret;
  92. break;
  93. case 2:
  94. if (!gpiochip_line_is_open_drain(gc, offset)) {
  95. dev_err(gc->parent, "GPO3 works only as open drain\n");
  96. return -EINVAL;
  97. }
  98. /* Disable sequencer for GPO3 */
  99. ret = tps65218_clear_bits(tps65218, TPS65218_REG_SEQ7,
  100. TPS65218_SEQ7_GPO3_SEQ_MASK,
  101. TPS65218_PROTECT_L1);
  102. if (ret)
  103. return ret;
  104. /* Setup GPO3 */
  105. ret = tps65218_clear_bits(tps65218, TPS65218_REG_CONFIG2,
  106. TPS65218_CONFIG2_DC12_RST,
  107. TPS65218_PROTECT_L1);
  108. if (ret)
  109. return ret;
  110. break;
  111. default:
  112. return -EINVAL;
  113. }
  114. return 0;
  115. }
  116. static int tps65218_gpio_set_config(struct gpio_chip *gc, unsigned offset,
  117. unsigned long config)
  118. {
  119. struct tps65218_gpio *tps65218_gpio = gpiochip_get_data(gc);
  120. struct tps65218 *tps65218 = tps65218_gpio->tps65218;
  121. enum pin_config_param param = pinconf_to_config_param(config);
  122. switch (offset) {
  123. case 0:
  124. case 2:
  125. /* GPO1 is hardwired to be open drain */
  126. if (param == PIN_CONFIG_DRIVE_OPEN_DRAIN)
  127. return 0;
  128. return -ENOTSUPP;
  129. case 1:
  130. /* GPO2 is push-pull by default, can be set as open drain. */
  131. if (param == PIN_CONFIG_DRIVE_OPEN_DRAIN)
  132. return tps65218_clear_bits(tps65218,
  133. TPS65218_REG_CONFIG1,
  134. TPS65218_CONFIG1_GPO2_BUF,
  135. TPS65218_PROTECT_L1);
  136. if (param == PIN_CONFIG_DRIVE_PUSH_PULL)
  137. return tps65218_set_bits(tps65218,
  138. TPS65218_REG_CONFIG1,
  139. TPS65218_CONFIG1_GPO2_BUF,
  140. TPS65218_CONFIG1_GPO2_BUF,
  141. TPS65218_PROTECT_L1);
  142. return -ENOTSUPP;
  143. default:
  144. break;
  145. }
  146. return -ENOTSUPP;
  147. }
  148. static const struct gpio_chip template_chip = {
  149. .label = "gpio-tps65218",
  150. .owner = THIS_MODULE,
  151. .request = tps65218_gpio_request,
  152. .direction_output = tps65218_gpio_output,
  153. .direction_input = tps65218_gpio_input,
  154. .get = tps65218_gpio_get,
  155. .set = tps65218_gpio_set,
  156. .set_config = tps65218_gpio_set_config,
  157. .can_sleep = true,
  158. .ngpio = 3,
  159. .base = -1,
  160. };
  161. static int tps65218_gpio_probe(struct platform_device *pdev)
  162. {
  163. struct tps65218 *tps65218 = dev_get_drvdata(pdev->dev.parent);
  164. struct tps65218_gpio *tps65218_gpio;
  165. tps65218_gpio = devm_kzalloc(&pdev->dev, sizeof(*tps65218_gpio),
  166. GFP_KERNEL);
  167. if (!tps65218_gpio)
  168. return -ENOMEM;
  169. tps65218_gpio->tps65218 = tps65218;
  170. tps65218_gpio->gpio_chip = template_chip;
  171. tps65218_gpio->gpio_chip.parent = &pdev->dev;
  172. return devm_gpiochip_add_data(&pdev->dev, &tps65218_gpio->gpio_chip,
  173. tps65218_gpio);
  174. }
  175. static const struct of_device_id tps65218_dt_match[] = {
  176. { .compatible = "ti,tps65218-gpio" },
  177. { }
  178. };
  179. MODULE_DEVICE_TABLE(of, tps65218_dt_match);
  180. static const struct platform_device_id tps65218_gpio_id_table[] = {
  181. { "tps65218-gpio", },
  182. { /* sentinel */ }
  183. };
  184. MODULE_DEVICE_TABLE(platform, tps65218_gpio_id_table);
  185. static struct platform_driver tps65218_gpio_driver = {
  186. .driver = {
  187. .name = "tps65218-gpio",
  188. .of_match_table = of_match_ptr(tps65218_dt_match)
  189. },
  190. .probe = tps65218_gpio_probe,
  191. .id_table = tps65218_gpio_id_table,
  192. };
  193. module_platform_driver(tps65218_gpio_driver);
  194. MODULE_AUTHOR("Nicolas Saenz Julienne <[email protected]>");
  195. MODULE_DESCRIPTION("GPO interface for TPS65218 PMICs");
  196. MODULE_LICENSE("GPL v2");