gpio-tps65912.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * GPIO driver for TI TPS65912x PMICs
  4. *
  5. * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
  6. * Andrew F. Davis <afd@ti.com>
  7. *
  8. * Based on the Arizona GPIO driver and the previous TPS65912 driver by
  9. * Margarita Olaya Cabrera <magi@slimlogic.co.uk>
  10. */
  11. #include <linux/gpio/driver.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/mfd/tps65912.h>
  15. struct tps65912_gpio {
  16. struct gpio_chip gpio_chip;
  17. struct tps65912 *tps;
  18. };
  19. static int tps65912_gpio_get_direction(struct gpio_chip *gc,
  20. unsigned offset)
  21. {
  22. struct tps65912_gpio *gpio = gpiochip_get_data(gc);
  23. int ret, val;
  24. ret = regmap_read(gpio->tps->regmap, TPS65912_GPIO1 + offset, &val);
  25. if (ret)
  26. return ret;
  27. if (val & GPIO_CFG_MASK)
  28. return GPIO_LINE_DIRECTION_OUT;
  29. else
  30. return GPIO_LINE_DIRECTION_IN;
  31. }
  32. static int tps65912_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
  33. {
  34. struct tps65912_gpio *gpio = gpiochip_get_data(gc);
  35. return regmap_update_bits(gpio->tps->regmap, TPS65912_GPIO1 + offset,
  36. GPIO_CFG_MASK, 0);
  37. }
  38. static int tps65912_gpio_direction_output(struct gpio_chip *gc,
  39. unsigned offset, int value)
  40. {
  41. struct tps65912_gpio *gpio = gpiochip_get_data(gc);
  42. /* Set the initial value */
  43. regmap_update_bits(gpio->tps->regmap, TPS65912_GPIO1 + offset,
  44. GPIO_SET_MASK, value ? GPIO_SET_MASK : 0);
  45. return regmap_update_bits(gpio->tps->regmap, TPS65912_GPIO1 + offset,
  46. GPIO_CFG_MASK, GPIO_CFG_MASK);
  47. }
  48. static int tps65912_gpio_get(struct gpio_chip *gc, unsigned offset)
  49. {
  50. struct tps65912_gpio *gpio = gpiochip_get_data(gc);
  51. int ret, val;
  52. ret = regmap_read(gpio->tps->regmap, TPS65912_GPIO1 + offset, &val);
  53. if (ret)
  54. return ret;
  55. if (val & GPIO_STS_MASK)
  56. return 1;
  57. return 0;
  58. }
  59. static void tps65912_gpio_set(struct gpio_chip *gc, unsigned offset,
  60. int value)
  61. {
  62. struct tps65912_gpio *gpio = gpiochip_get_data(gc);
  63. regmap_update_bits(gpio->tps->regmap, TPS65912_GPIO1 + offset,
  64. GPIO_SET_MASK, value ? GPIO_SET_MASK : 0);
  65. }
  66. static const struct gpio_chip template_chip = {
  67. .label = "tps65912-gpio",
  68. .owner = THIS_MODULE,
  69. .get_direction = tps65912_gpio_get_direction,
  70. .direction_input = tps65912_gpio_direction_input,
  71. .direction_output = tps65912_gpio_direction_output,
  72. .get = tps65912_gpio_get,
  73. .set = tps65912_gpio_set,
  74. .base = -1,
  75. .ngpio = 5,
  76. .can_sleep = true,
  77. };
  78. static int tps65912_gpio_probe(struct platform_device *pdev)
  79. {
  80. struct tps65912 *tps = dev_get_drvdata(pdev->dev.parent);
  81. struct tps65912_gpio *gpio;
  82. int ret;
  83. gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
  84. if (!gpio)
  85. return -ENOMEM;
  86. gpio->tps = dev_get_drvdata(pdev->dev.parent);
  87. gpio->gpio_chip = template_chip;
  88. gpio->gpio_chip.parent = tps->dev;
  89. ret = devm_gpiochip_add_data(&pdev->dev, &gpio->gpio_chip,
  90. gpio);
  91. if (ret < 0) {
  92. dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
  93. return ret;
  94. }
  95. platform_set_drvdata(pdev, gpio);
  96. return 0;
  97. }
  98. static const struct platform_device_id tps65912_gpio_id_table[] = {
  99. { "tps65912-gpio", },
  100. { /* sentinel */ }
  101. };
  102. MODULE_DEVICE_TABLE(platform, tps65912_gpio_id_table);
  103. static struct platform_driver tps65912_gpio_driver = {
  104. .driver = {
  105. .name = "tps65912-gpio",
  106. },
  107. .probe = tps65912_gpio_probe,
  108. .id_table = tps65912_gpio_id_table,
  109. };
  110. module_platform_driver(tps65912_gpio_driver);
  111. MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
  112. MODULE_DESCRIPTION("TPS65912 GPIO driver");
  113. MODULE_LICENSE("GPL v2");