gpio-tps65218.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. /* Setup GPO2 */
  90. ret = tps65218_clear_bits(tps65218, TPS65218_REG_CONFIG1,
  91. TPS65218_CONFIG1_IO1_SEL,
  92. TPS65218_PROTECT_L1);
  93. if (ret)
  94. return ret;
  95. break;
  96. case 2:
  97. if (!gpiochip_line_is_open_drain(gc, offset)) {
  98. dev_err(gc->parent, "GPO3 works only as open drain\n");
  99. return -EINVAL;
  100. }
  101. /* Disable sequencer for GPO3 */
  102. ret = tps65218_clear_bits(tps65218, TPS65218_REG_SEQ7,
  103. TPS65218_SEQ7_GPO3_SEQ_MASK,
  104. TPS65218_PROTECT_L1);
  105. if (ret)
  106. return ret;
  107. /* Setup GPO3 */
  108. ret = tps65218_clear_bits(tps65218, TPS65218_REG_CONFIG2,
  109. TPS65218_CONFIG2_DC12_RST,
  110. TPS65218_PROTECT_L1);
  111. if (ret)
  112. return ret;
  113. break;
  114. default:
  115. return -EINVAL;
  116. }
  117. return 0;
  118. }
  119. static int tps65218_gpio_set_single_ended(struct gpio_chip *gc,
  120. unsigned offset,
  121. enum single_ended_mode mode)
  122. {
  123. struct tps65218_gpio *tps65218_gpio = gpiochip_get_data(gc);
  124. struct tps65218 *tps65218 = tps65218_gpio->tps65218;
  125. switch (offset) {
  126. case 0:
  127. case 2:
  128. /* GPO1 is hardwired to be open drain */
  129. if (mode == LINE_MODE_OPEN_DRAIN)
  130. return 0;
  131. return -ENOTSUPP;
  132. case 1:
  133. /* GPO2 is push-pull by default, can be set as open drain. */
  134. if (mode == LINE_MODE_OPEN_DRAIN)
  135. return tps65218_clear_bits(tps65218,
  136. TPS65218_REG_CONFIG1,
  137. TPS65218_CONFIG1_GPO2_BUF,
  138. TPS65218_PROTECT_L1);
  139. if (mode == LINE_MODE_PUSH_PULL)
  140. return tps65218_set_bits(tps65218,
  141. TPS65218_REG_CONFIG1,
  142. TPS65218_CONFIG1_GPO2_BUF,
  143. TPS65218_CONFIG1_GPO2_BUF,
  144. TPS65218_PROTECT_L1);
  145. return -ENOTSUPP;
  146. default:
  147. break;
  148. }
  149. return -ENOTSUPP;
  150. }
  151. static const struct gpio_chip template_chip = {
  152. .label = "gpio-tps65218",
  153. .owner = THIS_MODULE,
  154. .request = tps65218_gpio_request,
  155. .direction_output = tps65218_gpio_output,
  156. .direction_input = tps65218_gpio_input,
  157. .get = tps65218_gpio_get,
  158. .set = tps65218_gpio_set,
  159. .set_single_ended = tps65218_gpio_set_single_ended,
  160. .can_sleep = true,
  161. .ngpio = 3,
  162. .base = -1,
  163. };
  164. static int tps65218_gpio_probe(struct platform_device *pdev)
  165. {
  166. struct tps65218 *tps65218 = dev_get_drvdata(pdev->dev.parent);
  167. struct tps65218_gpio *tps65218_gpio;
  168. int ret;
  169. tps65218_gpio = devm_kzalloc(&pdev->dev, sizeof(*tps65218_gpio),
  170. GFP_KERNEL);
  171. if (!tps65218_gpio)
  172. return -ENOMEM;
  173. tps65218_gpio->tps65218 = tps65218;
  174. tps65218_gpio->gpio_chip = template_chip;
  175. tps65218_gpio->gpio_chip.parent = &pdev->dev;
  176. #ifdef CONFIG_OF_GPIO
  177. tps65218_gpio->gpio_chip.of_node = pdev->dev.of_node;
  178. #endif
  179. ret = devm_gpiochip_add_data(&pdev->dev, &tps65218_gpio->gpio_chip,
  180. tps65218_gpio);
  181. if (ret < 0) {
  182. dev_err(&pdev->dev, "Failed to register gpiochip, %d\n", ret);
  183. return ret;
  184. }
  185. platform_set_drvdata(pdev, tps65218_gpio);
  186. return ret;
  187. }
  188. static const struct of_device_id tps65218_dt_match[] = {
  189. { .compatible = "ti,tps65218-gpio" },
  190. { }
  191. };
  192. MODULE_DEVICE_TABLE(of, tps65218_dt_match);
  193. static const struct platform_device_id tps65218_gpio_id_table[] = {
  194. { "tps65218-gpio", },
  195. { /* sentinel */ }
  196. };
  197. MODULE_DEVICE_TABLE(platform, tps65218_gpio_id_table);
  198. static struct platform_driver tps65218_gpio_driver = {
  199. .driver = {
  200. .name = "tps65218-gpio",
  201. .of_match_table = of_match_ptr(tps65218_dt_match)
  202. },
  203. .probe = tps65218_gpio_probe,
  204. .id_table = tps65218_gpio_id_table,
  205. };
  206. module_platform_driver(tps65218_gpio_driver);
  207. MODULE_AUTHOR("Nicolas Saenz Julienne <nicolassaenzj@gmail.com>");
  208. MODULE_DESCRIPTION("GPO interface for TPS65218 PMICs");
  209. MODULE_LICENSE("GPL v2");
  210. MODULE_ALIAS("platform:tps65218-gpio");