gpio-palmas.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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/module.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 inline struct palmas_gpio *to_palmas_gpio(struct gpio_chip *chip)
  35. {
  36. return container_of(chip, struct palmas_gpio, gpio_chip);
  37. }
  38. static int palmas_gpio_get(struct gpio_chip *gc, unsigned offset)
  39. {
  40. struct palmas_gpio *pg = to_palmas_gpio(gc);
  41. struct palmas *palmas = pg->palmas;
  42. unsigned int val;
  43. int ret;
  44. unsigned int reg;
  45. int gpio16 = (offset/8);
  46. offset %= 8;
  47. reg = (gpio16) ? PALMAS_GPIO_DATA_DIR2 : PALMAS_GPIO_DATA_DIR;
  48. ret = palmas_read(palmas, PALMAS_GPIO_BASE, reg, &val);
  49. if (ret < 0) {
  50. dev_err(gc->dev, "Reg 0x%02x read failed, %d\n", reg, ret);
  51. return ret;
  52. }
  53. if (val & BIT(offset))
  54. reg = (gpio16) ? PALMAS_GPIO_DATA_OUT2 : PALMAS_GPIO_DATA_OUT;
  55. else
  56. reg = (gpio16) ? PALMAS_GPIO_DATA_IN2 : PALMAS_GPIO_DATA_IN;
  57. ret = palmas_read(palmas, PALMAS_GPIO_BASE, reg, &val);
  58. if (ret < 0) {
  59. dev_err(gc->dev, "Reg 0x%02x read failed, %d\n", reg, ret);
  60. return ret;
  61. }
  62. return !!(val & BIT(offset));
  63. }
  64. static void palmas_gpio_set(struct gpio_chip *gc, unsigned offset,
  65. int value)
  66. {
  67. struct palmas_gpio *pg = to_palmas_gpio(gc);
  68. struct palmas *palmas = pg->palmas;
  69. int ret;
  70. unsigned int reg;
  71. int gpio16 = (offset/8);
  72. offset %= 8;
  73. if (gpio16)
  74. reg = (value) ?
  75. PALMAS_GPIO_SET_DATA_OUT2 : PALMAS_GPIO_CLEAR_DATA_OUT2;
  76. else
  77. reg = (value) ?
  78. PALMAS_GPIO_SET_DATA_OUT : PALMAS_GPIO_CLEAR_DATA_OUT;
  79. ret = palmas_write(palmas, PALMAS_GPIO_BASE, reg, BIT(offset));
  80. if (ret < 0)
  81. dev_err(gc->dev, "Reg 0x%02x write failed, %d\n", reg, ret);
  82. }
  83. static int palmas_gpio_output(struct gpio_chip *gc, unsigned offset,
  84. int value)
  85. {
  86. struct palmas_gpio *pg = to_palmas_gpio(gc);
  87. struct palmas *palmas = pg->palmas;
  88. int ret;
  89. unsigned int reg;
  90. int gpio16 = (offset/8);
  91. offset %= 8;
  92. reg = (gpio16) ? PALMAS_GPIO_DATA_DIR2 : PALMAS_GPIO_DATA_DIR;
  93. /* Set the initial value */
  94. palmas_gpio_set(gc, offset, value);
  95. ret = palmas_update_bits(palmas, PALMAS_GPIO_BASE, reg,
  96. BIT(offset), BIT(offset));
  97. if (ret < 0)
  98. dev_err(gc->dev, "Reg 0x%02x update failed, %d\n", reg, ret);
  99. return ret;
  100. }
  101. static int palmas_gpio_input(struct gpio_chip *gc, unsigned offset)
  102. {
  103. struct palmas_gpio *pg = to_palmas_gpio(gc);
  104. struct palmas *palmas = pg->palmas;
  105. int ret;
  106. unsigned int reg;
  107. int gpio16 = (offset/8);
  108. offset %= 8;
  109. reg = (gpio16) ? PALMAS_GPIO_DATA_DIR2 : PALMAS_GPIO_DATA_DIR;
  110. ret = palmas_update_bits(palmas, PALMAS_GPIO_BASE, reg, BIT(offset), 0);
  111. if (ret < 0)
  112. dev_err(gc->dev, "Reg 0x%02x update failed, %d\n", reg, ret);
  113. return ret;
  114. }
  115. static int palmas_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
  116. {
  117. struct palmas_gpio *pg = to_palmas_gpio(gc);
  118. struct palmas *palmas = pg->palmas;
  119. return palmas_irq_get_virq(palmas, PALMAS_GPIO_0_IRQ + offset);
  120. }
  121. static const struct palmas_device_data palmas_dev_data = {
  122. .ngpio = 8,
  123. };
  124. static const struct palmas_device_data tps80036_dev_data = {
  125. .ngpio = 16,
  126. };
  127. static const struct of_device_id of_palmas_gpio_match[] = {
  128. { .compatible = "ti,palmas-gpio", .data = &palmas_dev_data,},
  129. { .compatible = "ti,tps65913-gpio", .data = &palmas_dev_data,},
  130. { .compatible = "ti,tps65914-gpio", .data = &palmas_dev_data,},
  131. { .compatible = "ti,tps80036-gpio", .data = &tps80036_dev_data,},
  132. { },
  133. };
  134. MODULE_DEVICE_TABLE(of, of_palmas_gpio_match);
  135. static int palmas_gpio_probe(struct platform_device *pdev)
  136. {
  137. struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
  138. struct palmas_platform_data *palmas_pdata;
  139. struct palmas_gpio *palmas_gpio;
  140. int ret;
  141. const struct of_device_id *match;
  142. const struct palmas_device_data *dev_data;
  143. match = of_match_device(of_palmas_gpio_match, &pdev->dev);
  144. if (!match)
  145. return -ENODEV;
  146. dev_data = match->data;
  147. if (!dev_data)
  148. dev_data = &palmas_dev_data;
  149. palmas_gpio = devm_kzalloc(&pdev->dev,
  150. sizeof(*palmas_gpio), GFP_KERNEL);
  151. if (!palmas_gpio)
  152. return -ENOMEM;
  153. palmas_gpio->palmas = palmas;
  154. palmas_gpio->gpio_chip.owner = THIS_MODULE;
  155. palmas_gpio->gpio_chip.label = dev_name(&pdev->dev);
  156. palmas_gpio->gpio_chip.ngpio = dev_data->ngpio;
  157. palmas_gpio->gpio_chip.can_sleep = true;
  158. palmas_gpio->gpio_chip.direction_input = palmas_gpio_input;
  159. palmas_gpio->gpio_chip.direction_output = palmas_gpio_output;
  160. palmas_gpio->gpio_chip.to_irq = palmas_gpio_to_irq;
  161. palmas_gpio->gpio_chip.set = palmas_gpio_set;
  162. palmas_gpio->gpio_chip.get = palmas_gpio_get;
  163. palmas_gpio->gpio_chip.dev = &pdev->dev;
  164. #ifdef CONFIG_OF_GPIO
  165. palmas_gpio->gpio_chip.of_node = pdev->dev.of_node;
  166. #endif
  167. palmas_pdata = dev_get_platdata(palmas->dev);
  168. if (palmas_pdata && palmas_pdata->gpio_base)
  169. palmas_gpio->gpio_chip.base = palmas_pdata->gpio_base;
  170. else
  171. palmas_gpio->gpio_chip.base = -1;
  172. ret = gpiochip_add(&palmas_gpio->gpio_chip);
  173. if (ret < 0) {
  174. dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
  175. return ret;
  176. }
  177. platform_set_drvdata(pdev, palmas_gpio);
  178. return ret;
  179. }
  180. static int palmas_gpio_remove(struct platform_device *pdev)
  181. {
  182. struct palmas_gpio *palmas_gpio = platform_get_drvdata(pdev);
  183. gpiochip_remove(&palmas_gpio->gpio_chip);
  184. return 0;
  185. }
  186. static struct platform_driver palmas_gpio_driver = {
  187. .driver.name = "palmas-gpio",
  188. .driver.owner = THIS_MODULE,
  189. .driver.of_match_table = of_palmas_gpio_match,
  190. .probe = palmas_gpio_probe,
  191. .remove = palmas_gpio_remove,
  192. };
  193. static int __init palmas_gpio_init(void)
  194. {
  195. return platform_driver_register(&palmas_gpio_driver);
  196. }
  197. subsys_initcall(palmas_gpio_init);
  198. static void __exit palmas_gpio_exit(void)
  199. {
  200. platform_driver_unregister(&palmas_gpio_driver);
  201. }
  202. module_exit(palmas_gpio_exit);
  203. MODULE_ALIAS("platform:palmas-gpio");
  204. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  205. MODULE_DESCRIPTION("GPIO driver for TI Palmas series PMICs");
  206. MODULE_LICENSE("GPL v2");