gpio-rc5t583.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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/platform_device.h>
  27. #include <linux/device.h>
  28. #include <linux/gpio.h>
  29. #include <linux/mfd/rc5t583.h>
  30. struct rc5t583_gpio {
  31. struct gpio_chip gpio_chip;
  32. struct rc5t583 *rc5t583;
  33. };
  34. static int rc5t583_gpio_get(struct gpio_chip *gc, unsigned int offset)
  35. {
  36. struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
  37. struct device *parent = rc5t583_gpio->rc5t583->dev;
  38. uint8_t val = 0;
  39. int ret;
  40. ret = rc5t583_read(parent, RC5T583_GPIO_MON_IOIN, &val);
  41. if (ret < 0)
  42. return ret;
  43. return !!(val & BIT(offset));
  44. }
  45. static void rc5t583_gpio_set(struct gpio_chip *gc, unsigned int offset, int val)
  46. {
  47. struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
  48. struct device *parent = rc5t583_gpio->rc5t583->dev;
  49. if (val)
  50. rc5t583_set_bits(parent, RC5T583_GPIO_IOOUT, BIT(offset));
  51. else
  52. rc5t583_clear_bits(parent, RC5T583_GPIO_IOOUT, BIT(offset));
  53. }
  54. static int rc5t583_gpio_dir_input(struct gpio_chip *gc, unsigned int offset)
  55. {
  56. struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
  57. struct device *parent = rc5t583_gpio->rc5t583->dev;
  58. int ret;
  59. ret = rc5t583_clear_bits(parent, RC5T583_GPIO_IOSEL, BIT(offset));
  60. if (ret < 0)
  61. return ret;
  62. /* Set pin to gpio mode */
  63. return rc5t583_clear_bits(parent, RC5T583_GPIO_PGSEL, BIT(offset));
  64. }
  65. static int rc5t583_gpio_dir_output(struct gpio_chip *gc, unsigned offset,
  66. int value)
  67. {
  68. struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
  69. struct device *parent = rc5t583_gpio->rc5t583->dev;
  70. int ret;
  71. rc5t583_gpio_set(gc, offset, value);
  72. ret = rc5t583_set_bits(parent, RC5T583_GPIO_IOSEL, BIT(offset));
  73. if (ret < 0)
  74. return ret;
  75. /* Set pin to gpio mode */
  76. return rc5t583_clear_bits(parent, RC5T583_GPIO_PGSEL, BIT(offset));
  77. }
  78. static int rc5t583_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
  79. {
  80. struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
  81. if (offset < RC5T583_MAX_GPIO)
  82. return rc5t583_gpio->rc5t583->irq_base +
  83. RC5T583_IRQ_GPIO0 + offset;
  84. return -EINVAL;
  85. }
  86. static void rc5t583_gpio_free(struct gpio_chip *gc, unsigned offset)
  87. {
  88. struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
  89. struct device *parent = rc5t583_gpio->rc5t583->dev;
  90. rc5t583_set_bits(parent, RC5T583_GPIO_PGSEL, BIT(offset));
  91. }
  92. static int rc5t583_gpio_probe(struct platform_device *pdev)
  93. {
  94. struct rc5t583 *rc5t583 = dev_get_drvdata(pdev->dev.parent);
  95. struct rc5t583_platform_data *pdata = dev_get_platdata(rc5t583->dev);
  96. struct rc5t583_gpio *rc5t583_gpio;
  97. rc5t583_gpio = devm_kzalloc(&pdev->dev, sizeof(*rc5t583_gpio),
  98. GFP_KERNEL);
  99. if (!rc5t583_gpio)
  100. return -ENOMEM;
  101. rc5t583_gpio->gpio_chip.label = "gpio-rc5t583",
  102. rc5t583_gpio->gpio_chip.owner = THIS_MODULE,
  103. rc5t583_gpio->gpio_chip.free = rc5t583_gpio_free,
  104. rc5t583_gpio->gpio_chip.direction_input = rc5t583_gpio_dir_input,
  105. rc5t583_gpio->gpio_chip.direction_output = rc5t583_gpio_dir_output,
  106. rc5t583_gpio->gpio_chip.set = rc5t583_gpio_set,
  107. rc5t583_gpio->gpio_chip.get = rc5t583_gpio_get,
  108. rc5t583_gpio->gpio_chip.to_irq = rc5t583_gpio_to_irq,
  109. rc5t583_gpio->gpio_chip.ngpio = RC5T583_MAX_GPIO,
  110. rc5t583_gpio->gpio_chip.can_sleep = true,
  111. rc5t583_gpio->gpio_chip.parent = &pdev->dev;
  112. rc5t583_gpio->gpio_chip.base = -1;
  113. rc5t583_gpio->rc5t583 = rc5t583;
  114. if (pdata && pdata->gpio_base)
  115. rc5t583_gpio->gpio_chip.base = pdata->gpio_base;
  116. platform_set_drvdata(pdev, rc5t583_gpio);
  117. return devm_gpiochip_add_data(&pdev->dev, &rc5t583_gpio->gpio_chip,
  118. rc5t583_gpio);
  119. }
  120. static struct platform_driver rc5t583_gpio_driver = {
  121. .driver = {
  122. .name = "rc5t583-gpio",
  123. },
  124. .probe = rc5t583_gpio_probe,
  125. };
  126. static int __init rc5t583_gpio_init(void)
  127. {
  128. return platform_driver_register(&rc5t583_gpio_driver);
  129. }
  130. subsys_initcall(rc5t583_gpio_init);