gpio-tps6586x.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 __devinit tps6586x_gpio_probe(struct platform_device *pdev)
  70. {
  71. struct tps6586x_platform_data *pdata;
  72. struct tps6586x_gpio *tps6586x_gpio;
  73. int ret;
  74. pdata = dev_get_platdata(pdev->dev.parent);
  75. tps6586x_gpio = devm_kzalloc(&pdev->dev,
  76. sizeof(*tps6586x_gpio), GFP_KERNEL);
  77. if (!tps6586x_gpio) {
  78. dev_err(&pdev->dev, "Could not allocate tps6586x_gpio\n");
  79. return -ENOMEM;
  80. }
  81. tps6586x_gpio->parent = pdev->dev.parent;
  82. tps6586x_gpio->gpio_chip.owner = THIS_MODULE;
  83. tps6586x_gpio->gpio_chip.label = pdev->name;
  84. tps6586x_gpio->gpio_chip.dev = &pdev->dev;
  85. tps6586x_gpio->gpio_chip.ngpio = 4;
  86. tps6586x_gpio->gpio_chip.can_sleep = 1;
  87. /* FIXME: add handling of GPIOs as dedicated inputs */
  88. tps6586x_gpio->gpio_chip.direction_output = tps6586x_gpio_output;
  89. tps6586x_gpio->gpio_chip.set = tps6586x_gpio_set;
  90. tps6586x_gpio->gpio_chip.get = tps6586x_gpio_get;
  91. #ifdef CONFIG_OF_GPIO
  92. tps6586x_gpio->gpio_chip.of_node = pdev->dev.parent->of_node;
  93. #endif
  94. if (pdata && pdata->gpio_base)
  95. tps6586x_gpio->gpio_chip.base = pdata->gpio_base;
  96. else
  97. tps6586x_gpio->gpio_chip.base = -1;
  98. ret = gpiochip_add(&tps6586x_gpio->gpio_chip);
  99. if (ret < 0) {
  100. dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
  101. return ret;
  102. }
  103. platform_set_drvdata(pdev, tps6586x_gpio);
  104. return ret;
  105. }
  106. static int __devexit tps6586x_gpio_remove(struct platform_device *pdev)
  107. {
  108. struct tps6586x_gpio *tps6586x_gpio = platform_get_drvdata(pdev);
  109. return gpiochip_remove(&tps6586x_gpio->gpio_chip);
  110. }
  111. static struct platform_driver tps6586x_gpio_driver = {
  112. .driver.name = "tps6586x-gpio",
  113. .driver.owner = THIS_MODULE,
  114. .probe = tps6586x_gpio_probe,
  115. .remove = __devexit_p(tps6586x_gpio_remove),
  116. };
  117. static int __init tps6586x_gpio_init(void)
  118. {
  119. return platform_driver_register(&tps6586x_gpio_driver);
  120. }
  121. subsys_initcall(tps6586x_gpio_init);
  122. static void __exit tps6586x_gpio_exit(void)
  123. {
  124. platform_driver_unregister(&tps6586x_gpio_driver);
  125. }
  126. module_exit(tps6586x_gpio_exit);
  127. MODULE_ALIAS("platform:tps6586x-gpio");
  128. MODULE_DESCRIPTION("GPIO interface for TPS6586X PMIC");
  129. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  130. MODULE_LICENSE("GPL");