gpio-tps6586x.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * TI TPS6586x GPIO driver
  4. *
  5. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  6. * Author: Laxman dewangan <ldewangan@nvidia.com>
  7. *
  8. * Based on tps6586x.c
  9. * Copyright (c) 2010 CompuLab Ltd.
  10. * Mike Rapoport <mike@compulab.co.il>
  11. */
  12. #include <linux/errno.h>
  13. #include <linux/gpio/driver.h>
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/mfd/tps6586x.h>
  17. #include <linux/of_device.h>
  18. #include <linux/platform_device.h>
  19. /* GPIO control registers */
  20. #define TPS6586X_GPIOSET1 0x5d
  21. #define TPS6586X_GPIOSET2 0x5e
  22. struct tps6586x_gpio {
  23. struct gpio_chip gpio_chip;
  24. struct device *parent;
  25. };
  26. static int tps6586x_gpio_get(struct gpio_chip *gc, unsigned offset)
  27. {
  28. struct tps6586x_gpio *tps6586x_gpio = gpiochip_get_data(gc);
  29. uint8_t val;
  30. int ret;
  31. ret = tps6586x_read(tps6586x_gpio->parent, TPS6586X_GPIOSET2, &val);
  32. if (ret)
  33. return ret;
  34. return !!(val & (1 << offset));
  35. }
  36. static void tps6586x_gpio_set(struct gpio_chip *gc, unsigned offset,
  37. int value)
  38. {
  39. struct tps6586x_gpio *tps6586x_gpio = gpiochip_get_data(gc);
  40. tps6586x_update(tps6586x_gpio->parent, TPS6586X_GPIOSET2,
  41. value << offset, 1 << offset);
  42. }
  43. static int tps6586x_gpio_output(struct gpio_chip *gc, unsigned offset,
  44. int value)
  45. {
  46. struct tps6586x_gpio *tps6586x_gpio = gpiochip_get_data(gc);
  47. uint8_t val, mask;
  48. tps6586x_gpio_set(gc, offset, value);
  49. val = 0x1 << (offset * 2);
  50. mask = 0x3 << (offset * 2);
  51. return tps6586x_update(tps6586x_gpio->parent, TPS6586X_GPIOSET1,
  52. val, mask);
  53. }
  54. static int tps6586x_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
  55. {
  56. struct tps6586x_gpio *tps6586x_gpio = gpiochip_get_data(gc);
  57. return tps6586x_irq_get_virq(tps6586x_gpio->parent,
  58. TPS6586X_INT_PLDO_0 + offset);
  59. }
  60. static int tps6586x_gpio_probe(struct platform_device *pdev)
  61. {
  62. struct tps6586x_platform_data *pdata;
  63. struct tps6586x_gpio *tps6586x_gpio;
  64. int ret;
  65. pdata = dev_get_platdata(pdev->dev.parent);
  66. tps6586x_gpio = devm_kzalloc(&pdev->dev,
  67. sizeof(*tps6586x_gpio), GFP_KERNEL);
  68. if (!tps6586x_gpio)
  69. return -ENOMEM;
  70. tps6586x_gpio->parent = pdev->dev.parent;
  71. tps6586x_gpio->gpio_chip.owner = THIS_MODULE;
  72. tps6586x_gpio->gpio_chip.label = pdev->name;
  73. tps6586x_gpio->gpio_chip.parent = &pdev->dev;
  74. tps6586x_gpio->gpio_chip.ngpio = 4;
  75. tps6586x_gpio->gpio_chip.can_sleep = true;
  76. /* FIXME: add handling of GPIOs as dedicated inputs */
  77. tps6586x_gpio->gpio_chip.direction_output = tps6586x_gpio_output;
  78. tps6586x_gpio->gpio_chip.set = tps6586x_gpio_set;
  79. tps6586x_gpio->gpio_chip.get = tps6586x_gpio_get;
  80. tps6586x_gpio->gpio_chip.to_irq = tps6586x_gpio_to_irq;
  81. #ifdef CONFIG_OF_GPIO
  82. tps6586x_gpio->gpio_chip.of_node = pdev->dev.parent->of_node;
  83. #endif
  84. if (pdata && pdata->gpio_base)
  85. tps6586x_gpio->gpio_chip.base = pdata->gpio_base;
  86. else
  87. tps6586x_gpio->gpio_chip.base = -1;
  88. ret = devm_gpiochip_add_data(&pdev->dev, &tps6586x_gpio->gpio_chip,
  89. tps6586x_gpio);
  90. if (ret < 0) {
  91. dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
  92. return ret;
  93. }
  94. platform_set_drvdata(pdev, tps6586x_gpio);
  95. return ret;
  96. }
  97. static struct platform_driver tps6586x_gpio_driver = {
  98. .driver.name = "tps6586x-gpio",
  99. .probe = tps6586x_gpio_probe,
  100. };
  101. static int __init tps6586x_gpio_init(void)
  102. {
  103. return platform_driver_register(&tps6586x_gpio_driver);
  104. }
  105. subsys_initcall(tps6586x_gpio_init);