gpio-tps65912.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. int ret;
  43. /* Set the initial value */
  44. ret = regmap_update_bits(gpio->tps->regmap, TPS65912_GPIO1 + offset,
  45. GPIO_SET_MASK, value ? GPIO_SET_MASK : 0);
  46. if (ret)
  47. return ret;
  48. return regmap_update_bits(gpio->tps->regmap, TPS65912_GPIO1 + offset,
  49. GPIO_CFG_MASK, GPIO_CFG_MASK);
  50. }
  51. static int tps65912_gpio_get(struct gpio_chip *gc, unsigned offset)
  52. {
  53. struct tps65912_gpio *gpio = gpiochip_get_data(gc);
  54. int ret, val;
  55. ret = regmap_read(gpio->tps->regmap, TPS65912_GPIO1 + offset, &val);
  56. if (ret)
  57. return ret;
  58. if (val & GPIO_STS_MASK)
  59. return 1;
  60. return 0;
  61. }
  62. static void tps65912_gpio_set(struct gpio_chip *gc, unsigned offset,
  63. int value)
  64. {
  65. struct tps65912_gpio *gpio = gpiochip_get_data(gc);
  66. regmap_update_bits(gpio->tps->regmap, TPS65912_GPIO1 + offset,
  67. GPIO_SET_MASK, value ? GPIO_SET_MASK : 0);
  68. }
  69. static const struct gpio_chip template_chip = {
  70. .label = "tps65912-gpio",
  71. .owner = THIS_MODULE,
  72. .get_direction = tps65912_gpio_get_direction,
  73. .direction_input = tps65912_gpio_direction_input,
  74. .direction_output = tps65912_gpio_direction_output,
  75. .get = tps65912_gpio_get,
  76. .set = tps65912_gpio_set,
  77. .base = -1,
  78. .ngpio = 5,
  79. .can_sleep = true,
  80. };
  81. static int tps65912_gpio_probe(struct platform_device *pdev)
  82. {
  83. struct tps65912 *tps = dev_get_drvdata(pdev->dev.parent);
  84. struct tps65912_gpio *gpio;
  85. gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
  86. if (!gpio)
  87. return -ENOMEM;
  88. gpio->tps = dev_get_drvdata(pdev->dev.parent);
  89. gpio->gpio_chip = template_chip;
  90. gpio->gpio_chip.parent = tps->dev;
  91. return devm_gpiochip_add_data(&pdev->dev, &gpio->gpio_chip, gpio);
  92. }
  93. static const struct platform_device_id tps65912_gpio_id_table[] = {
  94. { "tps65912-gpio", },
  95. { /* sentinel */ }
  96. };
  97. MODULE_DEVICE_TABLE(platform, tps65912_gpio_id_table);
  98. static struct platform_driver tps65912_gpio_driver = {
  99. .driver = {
  100. .name = "tps65912-gpio",
  101. },
  102. .probe = tps65912_gpio_probe,
  103. .id_table = tps65912_gpio_id_table,
  104. };
  105. module_platform_driver(tps65912_gpio_driver);
  106. MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
  107. MODULE_DESCRIPTION("TPS65912 GPIO driver");
  108. MODULE_LICENSE("GPL v2");