gpio-tps65910.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * TI TPS6591x GPIO driver
  4. *
  5. * Copyright 2010 Texas Instruments Inc.
  6. *
  7. * Author: Graeme Gregory <gg@slimlogic.co.uk>
  8. * Author: Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/errno.h>
  13. #include <linux/gpio/driver.h>
  14. #include <linux/i2c.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/mfd/tps65910.h>
  17. #include <linux/of_device.h>
  18. struct tps65910_gpio {
  19. struct gpio_chip gpio_chip;
  20. struct tps65910 *tps65910;
  21. };
  22. static int tps65910_gpio_get(struct gpio_chip *gc, unsigned offset)
  23. {
  24. struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
  25. struct tps65910 *tps65910 = tps65910_gpio->tps65910;
  26. unsigned int val;
  27. tps65910_reg_read(tps65910, TPS65910_GPIO0 + offset, &val);
  28. if (val & GPIO_STS_MASK)
  29. return 1;
  30. return 0;
  31. }
  32. static void tps65910_gpio_set(struct gpio_chip *gc, unsigned offset,
  33. int value)
  34. {
  35. struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
  36. struct tps65910 *tps65910 = tps65910_gpio->tps65910;
  37. if (value)
  38. tps65910_reg_set_bits(tps65910, TPS65910_GPIO0 + offset,
  39. GPIO_SET_MASK);
  40. else
  41. tps65910_reg_clear_bits(tps65910, TPS65910_GPIO0 + offset,
  42. GPIO_SET_MASK);
  43. }
  44. static int tps65910_gpio_output(struct gpio_chip *gc, unsigned offset,
  45. int value)
  46. {
  47. struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
  48. struct tps65910 *tps65910 = tps65910_gpio->tps65910;
  49. /* Set the initial value */
  50. tps65910_gpio_set(gc, offset, value);
  51. return tps65910_reg_set_bits(tps65910, TPS65910_GPIO0 + offset,
  52. GPIO_CFG_MASK);
  53. }
  54. static int tps65910_gpio_input(struct gpio_chip *gc, unsigned offset)
  55. {
  56. struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
  57. struct tps65910 *tps65910 = tps65910_gpio->tps65910;
  58. return tps65910_reg_clear_bits(tps65910, TPS65910_GPIO0 + offset,
  59. GPIO_CFG_MASK);
  60. }
  61. #ifdef CONFIG_OF
  62. static struct tps65910_board *tps65910_parse_dt_for_gpio(struct device *dev,
  63. struct tps65910 *tps65910, int chip_ngpio)
  64. {
  65. struct tps65910_board *tps65910_board = tps65910->of_plat_data;
  66. unsigned int prop_array[TPS6591X_MAX_NUM_GPIO];
  67. int ngpio = min(chip_ngpio, TPS6591X_MAX_NUM_GPIO);
  68. int ret;
  69. int idx;
  70. tps65910_board->gpio_base = -1;
  71. ret = of_property_read_u32_array(tps65910->dev->of_node,
  72. "ti,en-gpio-sleep", prop_array, ngpio);
  73. if (ret < 0) {
  74. dev_dbg(dev, "ti,en-gpio-sleep not specified\n");
  75. return tps65910_board;
  76. }
  77. for (idx = 0; idx < ngpio; idx++)
  78. tps65910_board->en_gpio_sleep[idx] = (prop_array[idx] != 0);
  79. return tps65910_board;
  80. }
  81. #else
  82. static struct tps65910_board *tps65910_parse_dt_for_gpio(struct device *dev,
  83. struct tps65910 *tps65910, int chip_ngpio)
  84. {
  85. return NULL;
  86. }
  87. #endif
  88. static int tps65910_gpio_probe(struct platform_device *pdev)
  89. {
  90. struct tps65910 *tps65910 = dev_get_drvdata(pdev->dev.parent);
  91. struct tps65910_board *pdata = dev_get_platdata(tps65910->dev);
  92. struct tps65910_gpio *tps65910_gpio;
  93. int ret;
  94. int i;
  95. tps65910_gpio = devm_kzalloc(&pdev->dev,
  96. sizeof(*tps65910_gpio), GFP_KERNEL);
  97. if (!tps65910_gpio)
  98. return -ENOMEM;
  99. tps65910_gpio->tps65910 = tps65910;
  100. tps65910_gpio->gpio_chip.owner = THIS_MODULE;
  101. tps65910_gpio->gpio_chip.label = tps65910->i2c_client->name;
  102. switch (tps65910_chip_id(tps65910)) {
  103. case TPS65910:
  104. tps65910_gpio->gpio_chip.ngpio = TPS65910_NUM_GPIO;
  105. break;
  106. case TPS65911:
  107. tps65910_gpio->gpio_chip.ngpio = TPS65911_NUM_GPIO;
  108. break;
  109. default:
  110. return -EINVAL;
  111. }
  112. tps65910_gpio->gpio_chip.can_sleep = true;
  113. tps65910_gpio->gpio_chip.direction_input = tps65910_gpio_input;
  114. tps65910_gpio->gpio_chip.direction_output = tps65910_gpio_output;
  115. tps65910_gpio->gpio_chip.set = tps65910_gpio_set;
  116. tps65910_gpio->gpio_chip.get = tps65910_gpio_get;
  117. tps65910_gpio->gpio_chip.parent = &pdev->dev;
  118. #ifdef CONFIG_OF_GPIO
  119. tps65910_gpio->gpio_chip.of_node = tps65910->dev->of_node;
  120. #endif
  121. if (pdata && pdata->gpio_base)
  122. tps65910_gpio->gpio_chip.base = pdata->gpio_base;
  123. else
  124. tps65910_gpio->gpio_chip.base = -1;
  125. if (!pdata && tps65910->dev->of_node)
  126. pdata = tps65910_parse_dt_for_gpio(&pdev->dev, tps65910,
  127. tps65910_gpio->gpio_chip.ngpio);
  128. if (!pdata)
  129. goto skip_init;
  130. /* Configure sleep control for gpios if provided */
  131. for (i = 0; i < tps65910_gpio->gpio_chip.ngpio; ++i) {
  132. if (!pdata->en_gpio_sleep[i])
  133. continue;
  134. ret = tps65910_reg_set_bits(tps65910,
  135. TPS65910_GPIO0 + i, GPIO_SLEEP_MASK);
  136. if (ret < 0)
  137. dev_warn(tps65910->dev,
  138. "GPIO Sleep setting failed with err %d\n", ret);
  139. }
  140. skip_init:
  141. ret = devm_gpiochip_add_data(&pdev->dev, &tps65910_gpio->gpio_chip,
  142. tps65910_gpio);
  143. if (ret < 0) {
  144. dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
  145. return ret;
  146. }
  147. platform_set_drvdata(pdev, tps65910_gpio);
  148. return ret;
  149. }
  150. static struct platform_driver tps65910_gpio_driver = {
  151. .driver.name = "tps65910-gpio",
  152. .probe = tps65910_gpio_probe,
  153. };
  154. static int __init tps65910_gpio_init(void)
  155. {
  156. return platform_driver_register(&tps65910_gpio_driver);
  157. }
  158. subsys_initcall(tps65910_gpio_init);