gpio-tps6586x.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/init.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 int tps6586x_gpio_get(struct gpio_chip *gc, unsigned offset)
  38. {
  39. struct tps6586x_gpio *tps6586x_gpio = gpiochip_get_data(gc);
  40. uint8_t val;
  41. int ret;
  42. ret = tps6586x_read(tps6586x_gpio->parent, TPS6586X_GPIOSET2, &val);
  43. if (ret)
  44. return ret;
  45. return !!(val & (1 << offset));
  46. }
  47. static void tps6586x_gpio_set(struct gpio_chip *gc, unsigned offset,
  48. int value)
  49. {
  50. struct tps6586x_gpio *tps6586x_gpio = gpiochip_get_data(gc);
  51. tps6586x_update(tps6586x_gpio->parent, TPS6586X_GPIOSET2,
  52. value << offset, 1 << offset);
  53. }
  54. static int tps6586x_gpio_output(struct gpio_chip *gc, unsigned offset,
  55. int value)
  56. {
  57. struct tps6586x_gpio *tps6586x_gpio = gpiochip_get_data(gc);
  58. uint8_t val, mask;
  59. tps6586x_gpio_set(gc, offset, value);
  60. val = 0x1 << (offset * 2);
  61. mask = 0x3 << (offset * 2);
  62. return tps6586x_update(tps6586x_gpio->parent, TPS6586X_GPIOSET1,
  63. val, mask);
  64. }
  65. static int tps6586x_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
  66. {
  67. struct tps6586x_gpio *tps6586x_gpio = gpiochip_get_data(gc);
  68. return tps6586x_irq_get_virq(tps6586x_gpio->parent,
  69. TPS6586X_INT_PLDO_0 + offset);
  70. }
  71. static int tps6586x_gpio_probe(struct platform_device *pdev)
  72. {
  73. struct tps6586x_platform_data *pdata;
  74. struct tps6586x_gpio *tps6586x_gpio;
  75. int ret;
  76. pdata = dev_get_platdata(pdev->dev.parent);
  77. tps6586x_gpio = devm_kzalloc(&pdev->dev,
  78. sizeof(*tps6586x_gpio), GFP_KERNEL);
  79. if (!tps6586x_gpio)
  80. return -ENOMEM;
  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.parent = &pdev->dev;
  85. tps6586x_gpio->gpio_chip.ngpio = 4;
  86. tps6586x_gpio->gpio_chip.can_sleep = true;
  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. tps6586x_gpio->gpio_chip.to_irq = tps6586x_gpio_to_irq;
  92. #ifdef CONFIG_OF_GPIO
  93. tps6586x_gpio->gpio_chip.of_node = pdev->dev.parent->of_node;
  94. #endif
  95. if (pdata && pdata->gpio_base)
  96. tps6586x_gpio->gpio_chip.base = pdata->gpio_base;
  97. else
  98. tps6586x_gpio->gpio_chip.base = -1;
  99. ret = devm_gpiochip_add_data(&pdev->dev, &tps6586x_gpio->gpio_chip,
  100. tps6586x_gpio);
  101. if (ret < 0) {
  102. dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
  103. return ret;
  104. }
  105. platform_set_drvdata(pdev, tps6586x_gpio);
  106. return ret;
  107. }
  108. static struct platform_driver tps6586x_gpio_driver = {
  109. .driver.name = "tps6586x-gpio",
  110. .probe = tps6586x_gpio_probe,
  111. };
  112. static int __init tps6586x_gpio_init(void)
  113. {
  114. return platform_driver_register(&tps6586x_gpio_driver);
  115. }
  116. subsys_initcall(tps6586x_gpio_init);