gpio-rc5t583.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 inline struct rc5t583_gpio *to_rc5t583_gpio(struct gpio_chip *chip)
  36. {
  37. return container_of(chip, struct rc5t583_gpio, gpio_chip);
  38. }
  39. static int rc5t583_gpio_get(struct gpio_chip *gc, unsigned int offset)
  40. {
  41. struct rc5t583_gpio *rc5t583_gpio = to_rc5t583_gpio(gc);
  42. struct device *parent = rc5t583_gpio->rc5t583->dev;
  43. uint8_t val = 0;
  44. int ret;
  45. ret = rc5t583_read(parent, RC5T583_GPIO_MON_IOIN, &val);
  46. if (ret < 0)
  47. return ret;
  48. return !!(val & BIT(offset));
  49. }
  50. static void rc5t583_gpio_set(struct gpio_chip *gc, unsigned int offset, int val)
  51. {
  52. struct rc5t583_gpio *rc5t583_gpio = to_rc5t583_gpio(gc);
  53. struct device *parent = rc5t583_gpio->rc5t583->dev;
  54. if (val)
  55. rc5t583_set_bits(parent, RC5T583_GPIO_IOOUT, BIT(offset));
  56. else
  57. rc5t583_clear_bits(parent, RC5T583_GPIO_IOOUT, BIT(offset));
  58. }
  59. static int rc5t583_gpio_dir_input(struct gpio_chip *gc, unsigned int offset)
  60. {
  61. struct rc5t583_gpio *rc5t583_gpio = to_rc5t583_gpio(gc);
  62. struct device *parent = rc5t583_gpio->rc5t583->dev;
  63. int ret;
  64. ret = rc5t583_clear_bits(parent, RC5T583_GPIO_IOSEL, BIT(offset));
  65. if (ret < 0)
  66. return ret;
  67. /* Set pin to gpio mode */
  68. return rc5t583_clear_bits(parent, RC5T583_GPIO_PGSEL, BIT(offset));
  69. }
  70. static int rc5t583_gpio_dir_output(struct gpio_chip *gc, unsigned offset,
  71. int value)
  72. {
  73. struct rc5t583_gpio *rc5t583_gpio = to_rc5t583_gpio(gc);
  74. struct device *parent = rc5t583_gpio->rc5t583->dev;
  75. int ret;
  76. rc5t583_gpio_set(gc, offset, value);
  77. ret = rc5t583_set_bits(parent, RC5T583_GPIO_IOSEL, BIT(offset));
  78. if (ret < 0)
  79. return ret;
  80. /* Set pin to gpio mode */
  81. return rc5t583_clear_bits(parent, RC5T583_GPIO_PGSEL, BIT(offset));
  82. }
  83. static int rc5t583_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
  84. {
  85. struct rc5t583_gpio *rc5t583_gpio = to_rc5t583_gpio(gc);
  86. if (offset < RC5T583_MAX_GPIO)
  87. return rc5t583_gpio->rc5t583->irq_base +
  88. RC5T583_IRQ_GPIO0 + offset;
  89. return -EINVAL;
  90. }
  91. static void rc5t583_gpio_free(struct gpio_chip *gc, unsigned offset)
  92. {
  93. struct rc5t583_gpio *rc5t583_gpio = to_rc5t583_gpio(gc);
  94. struct device *parent = rc5t583_gpio->rc5t583->dev;
  95. rc5t583_set_bits(parent, RC5T583_GPIO_PGSEL, BIT(offset));
  96. }
  97. static int rc5t583_gpio_probe(struct platform_device *pdev)
  98. {
  99. struct rc5t583 *rc5t583 = dev_get_drvdata(pdev->dev.parent);
  100. struct rc5t583_platform_data *pdata = dev_get_platdata(rc5t583->dev);
  101. struct rc5t583_gpio *rc5t583_gpio;
  102. rc5t583_gpio = devm_kzalloc(&pdev->dev, sizeof(*rc5t583_gpio),
  103. GFP_KERNEL);
  104. if (!rc5t583_gpio)
  105. return -ENOMEM;
  106. rc5t583_gpio->gpio_chip.label = "gpio-rc5t583",
  107. rc5t583_gpio->gpio_chip.owner = THIS_MODULE,
  108. rc5t583_gpio->gpio_chip.free = rc5t583_gpio_free,
  109. rc5t583_gpio->gpio_chip.direction_input = rc5t583_gpio_dir_input,
  110. rc5t583_gpio->gpio_chip.direction_output = rc5t583_gpio_dir_output,
  111. rc5t583_gpio->gpio_chip.set = rc5t583_gpio_set,
  112. rc5t583_gpio->gpio_chip.get = rc5t583_gpio_get,
  113. rc5t583_gpio->gpio_chip.to_irq = rc5t583_gpio_to_irq,
  114. rc5t583_gpio->gpio_chip.ngpio = RC5T583_MAX_GPIO,
  115. rc5t583_gpio->gpio_chip.can_sleep = true,
  116. rc5t583_gpio->gpio_chip.dev = &pdev->dev;
  117. rc5t583_gpio->gpio_chip.base = -1;
  118. rc5t583_gpio->rc5t583 = rc5t583;
  119. if (pdata && pdata->gpio_base)
  120. rc5t583_gpio->gpio_chip.base = pdata->gpio_base;
  121. platform_set_drvdata(pdev, rc5t583_gpio);
  122. return gpiochip_add(&rc5t583_gpio->gpio_chip);
  123. }
  124. static int rc5t583_gpio_remove(struct platform_device *pdev)
  125. {
  126. struct rc5t583_gpio *rc5t583_gpio = platform_get_drvdata(pdev);
  127. gpiochip_remove(&rc5t583_gpio->gpio_chip);
  128. return 0;
  129. }
  130. static struct platform_driver rc5t583_gpio_driver = {
  131. .driver = {
  132. .name = "rc5t583-gpio",
  133. },
  134. .probe = rc5t583_gpio_probe,
  135. .remove = rc5t583_gpio_remove,
  136. };
  137. static int __init rc5t583_gpio_init(void)
  138. {
  139. return platform_driver_register(&rc5t583_gpio_driver);
  140. }
  141. subsys_initcall(rc5t583_gpio_init);
  142. static void __exit rc5t583_gpio_exit(void)
  143. {
  144. platform_driver_unregister(&rc5t583_gpio_driver);
  145. }
  146. module_exit(rc5t583_gpio_exit);
  147. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  148. MODULE_DESCRIPTION("GPIO interface for RC5T583");
  149. MODULE_LICENSE("GPL v2");
  150. MODULE_ALIAS("platform:rc5t583-gpio");