gpio-palmas.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * TI Palma series PMIC's GPIO driver.
  3. *
  4. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  5. *
  6. * Author: Laxman Dewangan <ldewangan@nvidia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <linux/gpio.h>
  21. #include <linux/kernel.h>
  22. #include <linux/init.h>
  23. #include <linux/mfd/palmas.h>
  24. #include <linux/of.h>
  25. #include <linux/of_device.h>
  26. #include <linux/platform_device.h>
  27. struct palmas_gpio {
  28. struct gpio_chip gpio_chip;
  29. struct palmas *palmas;
  30. };
  31. struct palmas_device_data {
  32. int ngpio;
  33. };
  34. static int palmas_gpio_get(struct gpio_chip *gc, unsigned offset)
  35. {
  36. struct palmas_gpio *pg = gpiochip_get_data(gc);
  37. struct palmas *palmas = pg->palmas;
  38. unsigned int val;
  39. int ret;
  40. unsigned int reg;
  41. int gpio16 = (offset/8);
  42. offset %= 8;
  43. reg = (gpio16) ? PALMAS_GPIO_DATA_DIR2 : PALMAS_GPIO_DATA_DIR;
  44. ret = palmas_read(palmas, PALMAS_GPIO_BASE, reg, &val);
  45. if (ret < 0) {
  46. dev_err(gc->parent, "Reg 0x%02x read failed, %d\n", reg, ret);
  47. return ret;
  48. }
  49. if (val & BIT(offset))
  50. reg = (gpio16) ? PALMAS_GPIO_DATA_OUT2 : PALMAS_GPIO_DATA_OUT;
  51. else
  52. reg = (gpio16) ? PALMAS_GPIO_DATA_IN2 : PALMAS_GPIO_DATA_IN;
  53. ret = palmas_read(palmas, PALMAS_GPIO_BASE, reg, &val);
  54. if (ret < 0) {
  55. dev_err(gc->parent, "Reg 0x%02x read failed, %d\n", reg, ret);
  56. return ret;
  57. }
  58. return !!(val & BIT(offset));
  59. }
  60. static void palmas_gpio_set(struct gpio_chip *gc, unsigned offset,
  61. int value)
  62. {
  63. struct palmas_gpio *pg = gpiochip_get_data(gc);
  64. struct palmas *palmas = pg->palmas;
  65. int ret;
  66. unsigned int reg;
  67. int gpio16 = (offset/8);
  68. offset %= 8;
  69. if (gpio16)
  70. reg = (value) ?
  71. PALMAS_GPIO_SET_DATA_OUT2 : PALMAS_GPIO_CLEAR_DATA_OUT2;
  72. else
  73. reg = (value) ?
  74. PALMAS_GPIO_SET_DATA_OUT : PALMAS_GPIO_CLEAR_DATA_OUT;
  75. ret = palmas_write(palmas, PALMAS_GPIO_BASE, reg, BIT(offset));
  76. if (ret < 0)
  77. dev_err(gc->parent, "Reg 0x%02x write failed, %d\n", reg, ret);
  78. }
  79. static int palmas_gpio_output(struct gpio_chip *gc, unsigned offset,
  80. int value)
  81. {
  82. struct palmas_gpio *pg = gpiochip_get_data(gc);
  83. struct palmas *palmas = pg->palmas;
  84. int ret;
  85. unsigned int reg;
  86. int gpio16 = (offset/8);
  87. offset %= 8;
  88. reg = (gpio16) ? PALMAS_GPIO_DATA_DIR2 : PALMAS_GPIO_DATA_DIR;
  89. /* Set the initial value */
  90. palmas_gpio_set(gc, offset, value);
  91. ret = palmas_update_bits(palmas, PALMAS_GPIO_BASE, reg,
  92. BIT(offset), BIT(offset));
  93. if (ret < 0)
  94. dev_err(gc->parent, "Reg 0x%02x update failed, %d\n", reg,
  95. ret);
  96. return ret;
  97. }
  98. static int palmas_gpio_input(struct gpio_chip *gc, unsigned offset)
  99. {
  100. struct palmas_gpio *pg = gpiochip_get_data(gc);
  101. struct palmas *palmas = pg->palmas;
  102. int ret;
  103. unsigned int reg;
  104. int gpio16 = (offset/8);
  105. offset %= 8;
  106. reg = (gpio16) ? PALMAS_GPIO_DATA_DIR2 : PALMAS_GPIO_DATA_DIR;
  107. ret = palmas_update_bits(palmas, PALMAS_GPIO_BASE, reg, BIT(offset), 0);
  108. if (ret < 0)
  109. dev_err(gc->parent, "Reg 0x%02x update failed, %d\n", reg,
  110. ret);
  111. return ret;
  112. }
  113. static int palmas_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
  114. {
  115. struct palmas_gpio *pg = gpiochip_get_data(gc);
  116. struct palmas *palmas = pg->palmas;
  117. return palmas_irq_get_virq(palmas, PALMAS_GPIO_0_IRQ + offset);
  118. }
  119. static const struct palmas_device_data palmas_dev_data = {
  120. .ngpio = 8,
  121. };
  122. static const struct palmas_device_data tps80036_dev_data = {
  123. .ngpio = 16,
  124. };
  125. static const struct of_device_id of_palmas_gpio_match[] = {
  126. { .compatible = "ti,palmas-gpio", .data = &palmas_dev_data,},
  127. { .compatible = "ti,tps65913-gpio", .data = &palmas_dev_data,},
  128. { .compatible = "ti,tps65914-gpio", .data = &palmas_dev_data,},
  129. { .compatible = "ti,tps80036-gpio", .data = &tps80036_dev_data,},
  130. { },
  131. };
  132. static int palmas_gpio_probe(struct platform_device *pdev)
  133. {
  134. struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
  135. struct palmas_platform_data *palmas_pdata;
  136. struct palmas_gpio *palmas_gpio;
  137. int ret;
  138. const struct of_device_id *match;
  139. const struct palmas_device_data *dev_data;
  140. match = of_match_device(of_palmas_gpio_match, &pdev->dev);
  141. if (!match)
  142. return -ENODEV;
  143. dev_data = match->data;
  144. if (!dev_data)
  145. dev_data = &palmas_dev_data;
  146. palmas_gpio = devm_kzalloc(&pdev->dev,
  147. sizeof(*palmas_gpio), GFP_KERNEL);
  148. if (!palmas_gpio)
  149. return -ENOMEM;
  150. palmas_gpio->palmas = palmas;
  151. palmas_gpio->gpio_chip.owner = THIS_MODULE;
  152. palmas_gpio->gpio_chip.label = dev_name(&pdev->dev);
  153. palmas_gpio->gpio_chip.ngpio = dev_data->ngpio;
  154. palmas_gpio->gpio_chip.can_sleep = true;
  155. palmas_gpio->gpio_chip.direction_input = palmas_gpio_input;
  156. palmas_gpio->gpio_chip.direction_output = palmas_gpio_output;
  157. palmas_gpio->gpio_chip.to_irq = palmas_gpio_to_irq;
  158. palmas_gpio->gpio_chip.set = palmas_gpio_set;
  159. palmas_gpio->gpio_chip.get = palmas_gpio_get;
  160. palmas_gpio->gpio_chip.parent = &pdev->dev;
  161. #ifdef CONFIG_OF_GPIO
  162. palmas_gpio->gpio_chip.of_node = pdev->dev.of_node;
  163. #endif
  164. palmas_pdata = dev_get_platdata(palmas->dev);
  165. if (palmas_pdata && palmas_pdata->gpio_base)
  166. palmas_gpio->gpio_chip.base = palmas_pdata->gpio_base;
  167. else
  168. palmas_gpio->gpio_chip.base = -1;
  169. ret = devm_gpiochip_add_data(&pdev->dev, &palmas_gpio->gpio_chip,
  170. palmas_gpio);
  171. if (ret < 0) {
  172. dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
  173. return ret;
  174. }
  175. platform_set_drvdata(pdev, palmas_gpio);
  176. return ret;
  177. }
  178. static struct platform_driver palmas_gpio_driver = {
  179. .driver.name = "palmas-gpio",
  180. .driver.of_match_table = of_palmas_gpio_match,
  181. .probe = palmas_gpio_probe,
  182. };
  183. static int __init palmas_gpio_init(void)
  184. {
  185. return platform_driver_register(&palmas_gpio_driver);
  186. }
  187. subsys_initcall(palmas_gpio_init);