gpio-tps65086.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
  4. * Andrew F. 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. int ret;
  69. gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
  70. if (!gpio)
  71. return -ENOMEM;
  72. platform_set_drvdata(pdev, gpio);
  73. gpio->tps = dev_get_drvdata(pdev->dev.parent);
  74. gpio->chip = template_chip;
  75. gpio->chip.parent = gpio->tps->dev;
  76. ret = gpiochip_add_data(&gpio->chip, gpio);
  77. if (ret < 0) {
  78. dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
  79. return ret;
  80. }
  81. return 0;
  82. }
  83. static int tps65086_gpio_remove(struct platform_device *pdev)
  84. {
  85. struct tps65086_gpio *gpio = platform_get_drvdata(pdev);
  86. gpiochip_remove(&gpio->chip);
  87. return 0;
  88. }
  89. static const struct platform_device_id tps65086_gpio_id_table[] = {
  90. { "tps65086-gpio", },
  91. { /* sentinel */ }
  92. };
  93. MODULE_DEVICE_TABLE(platform, tps65086_gpio_id_table);
  94. static struct platform_driver tps65086_gpio_driver = {
  95. .driver = {
  96. .name = "tps65086-gpio",
  97. },
  98. .probe = tps65086_gpio_probe,
  99. .remove = tps65086_gpio_remove,
  100. .id_table = tps65086_gpio_id_table,
  101. };
  102. module_platform_driver(tps65086_gpio_driver);
  103. MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
  104. MODULE_DESCRIPTION("TPS65086 GPIO driver");
  105. MODULE_LICENSE("GPL v2");