gpio-tps65218.c 5.4 KB

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