gpio-tps6586x.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * TI TPS6586x GPIO driver
  3. *
  4. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  5. * Author: Laxman dewangan <ldewangan@nvidia.com>
  6. *
  7. * Based on tps6586x.c
  8. * Copyright (c) 2010 CompuLab Ltd.
  9. * Mike Rapoport <mike@compulab.co.il>
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms and conditions of the GNU General Public License,
  13. * version 2, as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  17. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  18. * more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. #include <linux/errno.h>
  24. #include <linux/gpio.h>
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/mfd/tps6586x.h>
  28. #include <linux/of_device.h>
  29. #include <linux/platform_device.h>
  30. /* GPIO control registers */
  31. #define TPS6586X_GPIOSET1 0x5d
  32. #define TPS6586X_GPIOSET2 0x5e
  33. struct tps6586x_gpio {
  34. struct gpio_chip gpio_chip;
  35. struct device *parent;
  36. };
  37. static inline struct tps6586x_gpio *to_tps6586x_gpio(struct gpio_chip *chip)
  38. {
  39. return container_of(chip, struct tps6586x_gpio, gpio_chip);
  40. }
  41. static int tps6586x_gpio_get(struct gpio_chip *gc, unsigned offset)
  42. {
  43. struct tps6586x_gpio *tps6586x_gpio = to_tps6586x_gpio(gc);
  44. uint8_t val;
  45. int ret;
  46. ret = tps6586x_read(tps6586x_gpio->parent, TPS6586X_GPIOSET2, &val);
  47. if (ret)
  48. return ret;
  49. return !!(val & (1 << offset));
  50. }
  51. static void tps6586x_gpio_set(struct gpio_chip *gc, unsigned offset,
  52. int value)
  53. {
  54. struct tps6586x_gpio *tps6586x_gpio = to_tps6586x_gpio(gc);
  55. tps6586x_update(tps6586x_gpio->parent, TPS6586X_GPIOSET2,
  56. value << offset, 1 << offset);
  57. }
  58. static int tps6586x_gpio_output(struct gpio_chip *gc, unsigned offset,
  59. int value)
  60. {
  61. struct tps6586x_gpio *tps6586x_gpio = to_tps6586x_gpio(gc);
  62. uint8_t val, mask;
  63. tps6586x_gpio_set(gc, offset, value);
  64. val = 0x1 << (offset * 2);
  65. mask = 0x3 << (offset * 2);
  66. return tps6586x_update(tps6586x_gpio->parent, TPS6586X_GPIOSET1,
  67. val, mask);
  68. }
  69. static int tps6586x_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
  70. {
  71. struct tps6586x_gpio *tps6586x_gpio = to_tps6586x_gpio(gc);
  72. return tps6586x_irq_get_virq(tps6586x_gpio->parent,
  73. TPS6586X_INT_PLDO_0 + offset);
  74. }
  75. static int tps6586x_gpio_probe(struct platform_device *pdev)
  76. {
  77. struct tps6586x_platform_data *pdata;
  78. struct tps6586x_gpio *tps6586x_gpio;
  79. int ret;
  80. pdata = dev_get_platdata(pdev->dev.parent);
  81. tps6586x_gpio = devm_kzalloc(&pdev->dev,
  82. sizeof(*tps6586x_gpio), GFP_KERNEL);
  83. if (!tps6586x_gpio)
  84. return -ENOMEM;
  85. tps6586x_gpio->parent = pdev->dev.parent;
  86. tps6586x_gpio->gpio_chip.owner = THIS_MODULE;
  87. tps6586x_gpio->gpio_chip.label = pdev->name;
  88. tps6586x_gpio->gpio_chip.dev = &pdev->dev;
  89. tps6586x_gpio->gpio_chip.ngpio = 4;
  90. tps6586x_gpio->gpio_chip.can_sleep = true;
  91. /* FIXME: add handling of GPIOs as dedicated inputs */
  92. tps6586x_gpio->gpio_chip.direction_output = tps6586x_gpio_output;
  93. tps6586x_gpio->gpio_chip.set = tps6586x_gpio_set;
  94. tps6586x_gpio->gpio_chip.get = tps6586x_gpio_get;
  95. tps6586x_gpio->gpio_chip.to_irq = tps6586x_gpio_to_irq;
  96. #ifdef CONFIG_OF_GPIO
  97. tps6586x_gpio->gpio_chip.of_node = pdev->dev.parent->of_node;
  98. #endif
  99. if (pdata && pdata->gpio_base)
  100. tps6586x_gpio->gpio_chip.base = pdata->gpio_base;
  101. else
  102. tps6586x_gpio->gpio_chip.base = -1;
  103. ret = gpiochip_add(&tps6586x_gpio->gpio_chip);
  104. if (ret < 0) {
  105. dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
  106. return ret;
  107. }
  108. platform_set_drvdata(pdev, tps6586x_gpio);
  109. return ret;
  110. }
  111. static int tps6586x_gpio_remove(struct platform_device *pdev)
  112. {
  113. struct tps6586x_gpio *tps6586x_gpio = platform_get_drvdata(pdev);
  114. gpiochip_remove(&tps6586x_gpio->gpio_chip);
  115. return 0;
  116. }
  117. static struct platform_driver tps6586x_gpio_driver = {
  118. .driver.name = "tps6586x-gpio",
  119. .driver.owner = THIS_MODULE,
  120. .probe = tps6586x_gpio_probe,
  121. .remove = tps6586x_gpio_remove,
  122. };
  123. static int __init tps6586x_gpio_init(void)
  124. {
  125. return platform_driver_register(&tps6586x_gpio_driver);
  126. }
  127. subsys_initcall(tps6586x_gpio_init);
  128. static void __exit tps6586x_gpio_exit(void)
  129. {
  130. platform_driver_unregister(&tps6586x_gpio_driver);
  131. }
  132. module_exit(tps6586x_gpio_exit);
  133. MODULE_ALIAS("platform:tps6586x-gpio");
  134. MODULE_DESCRIPTION("GPIO interface for TPS6586X PMIC");
  135. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  136. MODULE_LICENSE("GPL");