gpio-tps65910.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 <[email protected]>
  8. * Author: Jorge Eduardo Candelaria <[email protected]>
  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. regmap_read(tps65910->regmap, 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. regmap_set_bits(tps65910->regmap, TPS65910_GPIO0 + offset,
  39. GPIO_SET_MASK);
  40. else
  41. regmap_clear_bits(tps65910->regmap, 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 regmap_set_bits(tps65910->regmap, 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 regmap_clear_bits(tps65910->regmap, 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. device_set_node(&pdev->dev, dev_fwnode(pdev->dev.parent));
  96. tps65910_gpio = devm_kzalloc(&pdev->dev,
  97. sizeof(*tps65910_gpio), GFP_KERNEL);
  98. if (!tps65910_gpio)
  99. return -ENOMEM;
  100. tps65910_gpio->tps65910 = tps65910;
  101. tps65910_gpio->gpio_chip.owner = THIS_MODULE;
  102. tps65910_gpio->gpio_chip.label = tps65910->i2c_client->name;
  103. switch (tps65910_chip_id(tps65910)) {
  104. case TPS65910:
  105. tps65910_gpio->gpio_chip.ngpio = TPS65910_NUM_GPIO;
  106. break;
  107. case TPS65911:
  108. tps65910_gpio->gpio_chip.ngpio = TPS65911_NUM_GPIO;
  109. break;
  110. default:
  111. return -EINVAL;
  112. }
  113. tps65910_gpio->gpio_chip.can_sleep = true;
  114. tps65910_gpio->gpio_chip.direction_input = tps65910_gpio_input;
  115. tps65910_gpio->gpio_chip.direction_output = tps65910_gpio_output;
  116. tps65910_gpio->gpio_chip.set = tps65910_gpio_set;
  117. tps65910_gpio->gpio_chip.get = tps65910_gpio_get;
  118. tps65910_gpio->gpio_chip.parent = &pdev->dev;
  119. if (pdata && pdata->gpio_base)
  120. tps65910_gpio->gpio_chip.base = pdata->gpio_base;
  121. else
  122. tps65910_gpio->gpio_chip.base = -1;
  123. if (!pdata && tps65910->dev->of_node)
  124. pdata = tps65910_parse_dt_for_gpio(&pdev->dev, tps65910,
  125. tps65910_gpio->gpio_chip.ngpio);
  126. if (!pdata)
  127. goto skip_init;
  128. /* Configure sleep control for gpios if provided */
  129. for (i = 0; i < tps65910_gpio->gpio_chip.ngpio; ++i) {
  130. if (!pdata->en_gpio_sleep[i])
  131. continue;
  132. ret = regmap_set_bits(tps65910->regmap,
  133. TPS65910_GPIO0 + i, GPIO_SLEEP_MASK);
  134. if (ret < 0)
  135. dev_warn(tps65910->dev,
  136. "GPIO Sleep setting failed with err %d\n", ret);
  137. }
  138. skip_init:
  139. return devm_gpiochip_add_data(&pdev->dev, &tps65910_gpio->gpio_chip,
  140. tps65910_gpio);
  141. }
  142. static struct platform_driver tps65910_gpio_driver = {
  143. .driver.name = "tps65910-gpio",
  144. .probe = tps65910_gpio_probe,
  145. };
  146. static int __init tps65910_gpio_init(void)
  147. {
  148. return platform_driver_register(&tps65910_gpio_driver);
  149. }
  150. subsys_initcall(tps65910_gpio_init);