gpio-rc5t583.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * GPIO driver for RICOH583 power management chip.
  3. *
  4. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  5. * Author: Laxman dewangan <ldewangan@nvidia.com>
  6. *
  7. * Based on code
  8. * Copyright (C) 2011 RICOH COMPANY,LTD
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms and conditions of the GNU General Public License,
  12. * version 2, as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  17. * more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. #include <linux/init.h>
  24. #include <linux/kernel.h>
  25. #include <linux/slab.h>
  26. #include <linux/module.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/device.h>
  29. #include <linux/gpio.h>
  30. #include <linux/mfd/rc5t583.h>
  31. struct rc5t583_gpio {
  32. struct gpio_chip gpio_chip;
  33. struct rc5t583 *rc5t583;
  34. };
  35. static int rc5t583_gpio_get(struct gpio_chip *gc, unsigned int offset)
  36. {
  37. struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
  38. struct device *parent = rc5t583_gpio->rc5t583->dev;
  39. uint8_t val = 0;
  40. int ret;
  41. ret = rc5t583_read(parent, RC5T583_GPIO_MON_IOIN, &val);
  42. if (ret < 0)
  43. return ret;
  44. return !!(val & BIT(offset));
  45. }
  46. static void rc5t583_gpio_set(struct gpio_chip *gc, unsigned int offset, int val)
  47. {
  48. struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
  49. struct device *parent = rc5t583_gpio->rc5t583->dev;
  50. if (val)
  51. rc5t583_set_bits(parent, RC5T583_GPIO_IOOUT, BIT(offset));
  52. else
  53. rc5t583_clear_bits(parent, RC5T583_GPIO_IOOUT, BIT(offset));
  54. }
  55. static int rc5t583_gpio_dir_input(struct gpio_chip *gc, unsigned int offset)
  56. {
  57. struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
  58. struct device *parent = rc5t583_gpio->rc5t583->dev;
  59. int ret;
  60. ret = rc5t583_clear_bits(parent, RC5T583_GPIO_IOSEL, BIT(offset));
  61. if (ret < 0)
  62. return ret;
  63. /* Set pin to gpio mode */
  64. return rc5t583_clear_bits(parent, RC5T583_GPIO_PGSEL, BIT(offset));
  65. }
  66. static int rc5t583_gpio_dir_output(struct gpio_chip *gc, unsigned offset,
  67. int value)
  68. {
  69. struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
  70. struct device *parent = rc5t583_gpio->rc5t583->dev;
  71. int ret;
  72. rc5t583_gpio_set(gc, offset, value);
  73. ret = rc5t583_set_bits(parent, RC5T583_GPIO_IOSEL, BIT(offset));
  74. if (ret < 0)
  75. return ret;
  76. /* Set pin to gpio mode */
  77. return rc5t583_clear_bits(parent, RC5T583_GPIO_PGSEL, BIT(offset));
  78. }
  79. static int rc5t583_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
  80. {
  81. struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
  82. if (offset < RC5T583_MAX_GPIO)
  83. return rc5t583_gpio->rc5t583->irq_base +
  84. RC5T583_IRQ_GPIO0 + offset;
  85. return -EINVAL;
  86. }
  87. static void rc5t583_gpio_free(struct gpio_chip *gc, unsigned offset)
  88. {
  89. struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
  90. struct device *parent = rc5t583_gpio->rc5t583->dev;
  91. rc5t583_set_bits(parent, RC5T583_GPIO_PGSEL, BIT(offset));
  92. }
  93. static int rc5t583_gpio_probe(struct platform_device *pdev)
  94. {
  95. struct rc5t583 *rc5t583 = dev_get_drvdata(pdev->dev.parent);
  96. struct rc5t583_platform_data *pdata = dev_get_platdata(rc5t583->dev);
  97. struct rc5t583_gpio *rc5t583_gpio;
  98. rc5t583_gpio = devm_kzalloc(&pdev->dev, sizeof(*rc5t583_gpio),
  99. GFP_KERNEL);
  100. if (!rc5t583_gpio)
  101. return -ENOMEM;
  102. rc5t583_gpio->gpio_chip.label = "gpio-rc5t583",
  103. rc5t583_gpio->gpio_chip.owner = THIS_MODULE,
  104. rc5t583_gpio->gpio_chip.free = rc5t583_gpio_free,
  105. rc5t583_gpio->gpio_chip.direction_input = rc5t583_gpio_dir_input,
  106. rc5t583_gpio->gpio_chip.direction_output = rc5t583_gpio_dir_output,
  107. rc5t583_gpio->gpio_chip.set = rc5t583_gpio_set,
  108. rc5t583_gpio->gpio_chip.get = rc5t583_gpio_get,
  109. rc5t583_gpio->gpio_chip.to_irq = rc5t583_gpio_to_irq,
  110. rc5t583_gpio->gpio_chip.ngpio = RC5T583_MAX_GPIO,
  111. rc5t583_gpio->gpio_chip.can_sleep = true,
  112. rc5t583_gpio->gpio_chip.parent = &pdev->dev;
  113. rc5t583_gpio->gpio_chip.base = -1;
  114. rc5t583_gpio->rc5t583 = rc5t583;
  115. if (pdata && pdata->gpio_base)
  116. rc5t583_gpio->gpio_chip.base = pdata->gpio_base;
  117. platform_set_drvdata(pdev, rc5t583_gpio);
  118. return devm_gpiochip_add_data(&pdev->dev, &rc5t583_gpio->gpio_chip,
  119. rc5t583_gpio);
  120. }
  121. static struct platform_driver rc5t583_gpio_driver = {
  122. .driver = {
  123. .name = "rc5t583-gpio",
  124. },
  125. .probe = rc5t583_gpio_probe,
  126. };
  127. static int __init rc5t583_gpio_init(void)
  128. {
  129. return platform_driver_register(&rc5t583_gpio_driver);
  130. }
  131. subsys_initcall(rc5t583_gpio_init);
  132. static void __exit rc5t583_gpio_exit(void)
  133. {
  134. platform_driver_unregister(&rc5t583_gpio_driver);
  135. }
  136. module_exit(rc5t583_gpio_exit);
  137. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  138. MODULE_DESCRIPTION("GPIO interface for RC5T583");
  139. MODULE_LICENSE("GPL v2");
  140. MODULE_ALIAS("platform:rc5t583-gpio");