gpio-tps65218.c 6.2 KB

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