gpio-tps65086.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2015-2023 Texas Instruments Incorporated - https://www.ti.com/
  4. * Andrew Davis <afd@ti.com>
  5. *
  6. * Based on the TPS65912 driver
  7. */
  8. #include <linux/gpio/driver.h>
  9. #include <linux/module.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/mfd/tps65086.h>
  12. struct tps65086_gpio {
  13. struct gpio_chip chip;
  14. struct tps65086 *tps;
  15. };
  16. static int tps65086_gpio_get_direction(struct gpio_chip *chip,
  17. unsigned offset)
  18. {
  19. /* This device is output only */
  20. return GPIO_LINE_DIRECTION_OUT;
  21. }
  22. static int tps65086_gpio_direction_input(struct gpio_chip *chip,
  23. unsigned offset)
  24. {
  25. /* This device is output only */
  26. return -EINVAL;
  27. }
  28. static int tps65086_gpio_direction_output(struct gpio_chip *chip,
  29. unsigned offset, int value)
  30. {
  31. struct tps65086_gpio *gpio = gpiochip_get_data(chip);
  32. /* Set the initial value */
  33. regmap_update_bits(gpio->tps->regmap, TPS65086_GPOCTRL,
  34. BIT(4 + offset), value ? BIT(4 + offset) : 0);
  35. return 0;
  36. }
  37. static int tps65086_gpio_get(struct gpio_chip *chip, unsigned offset)
  38. {
  39. struct tps65086_gpio *gpio = gpiochip_get_data(chip);
  40. int ret, val;
  41. ret = regmap_read(gpio->tps->regmap, TPS65086_GPOCTRL, &val);
  42. if (ret < 0)
  43. return ret;
  44. return val & BIT(4 + offset);
  45. }
  46. static void tps65086_gpio_set(struct gpio_chip *chip, unsigned offset,
  47. int value)
  48. {
  49. struct tps65086_gpio *gpio = gpiochip_get_data(chip);
  50. regmap_update_bits(gpio->tps->regmap, TPS65086_GPOCTRL,
  51. BIT(4 + offset), value ? BIT(4 + offset) : 0);
  52. }
  53. static const struct gpio_chip template_chip = {
  54. .label = "tps65086-gpio",
  55. .owner = THIS_MODULE,
  56. .get_direction = tps65086_gpio_get_direction,
  57. .direction_input = tps65086_gpio_direction_input,
  58. .direction_output = tps65086_gpio_direction_output,
  59. .get = tps65086_gpio_get,
  60. .set = tps65086_gpio_set,
  61. .base = -1,
  62. .ngpio = 4,
  63. .can_sleep = true,
  64. };
  65. static int tps65086_gpio_probe(struct platform_device *pdev)
  66. {
  67. struct tps65086_gpio *gpio;
  68. gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
  69. if (!gpio)
  70. return -ENOMEM;
  71. gpio->tps = dev_get_drvdata(pdev->dev.parent);
  72. gpio->chip = template_chip;
  73. gpio->chip.parent = gpio->tps->dev;
  74. return devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
  75. }
  76. static const struct platform_device_id tps65086_gpio_id_table[] = {
  77. { "tps65086-gpio", },
  78. { /* sentinel */ }
  79. };
  80. MODULE_DEVICE_TABLE(platform, tps65086_gpio_id_table);
  81. static struct platform_driver tps65086_gpio_driver = {
  82. .driver = {
  83. .name = "tps65086-gpio",
  84. },
  85. .probe = tps65086_gpio_probe,
  86. .id_table = tps65086_gpio_id_table,
  87. };
  88. module_platform_driver(tps65086_gpio_driver);
  89. MODULE_AUTHOR("Andrew Davis <afd@ti.com>");
  90. MODULE_DESCRIPTION("TPS65086 GPIO driver");
  91. MODULE_LICENSE("GPL v2");