gpio-tps65910.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * TI TPS6591x GPIO driver
  3. *
  4. * Copyright 2010 Texas Instruments Inc.
  5. *
  6. * Author: Graeme Gregory <gg@slimlogic.co.uk>
  7. * Author: Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/errno.h>
  18. #include <linux/gpio.h>
  19. #include <linux/i2c.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/mfd/tps65910.h>
  22. #include <linux/of_device.h>
  23. struct tps65910_gpio {
  24. struct gpio_chip gpio_chip;
  25. struct tps65910 *tps65910;
  26. };
  27. static int tps65910_gpio_get(struct gpio_chip *gc, unsigned offset)
  28. {
  29. struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
  30. struct tps65910 *tps65910 = tps65910_gpio->tps65910;
  31. unsigned int val;
  32. tps65910_reg_read(tps65910, TPS65910_GPIO0 + offset, &val);
  33. if (val & GPIO_STS_MASK)
  34. return 1;
  35. return 0;
  36. }
  37. static void tps65910_gpio_set(struct gpio_chip *gc, unsigned offset,
  38. int value)
  39. {
  40. struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
  41. struct tps65910 *tps65910 = tps65910_gpio->tps65910;
  42. if (value)
  43. tps65910_reg_set_bits(tps65910, TPS65910_GPIO0 + offset,
  44. GPIO_SET_MASK);
  45. else
  46. tps65910_reg_clear_bits(tps65910, TPS65910_GPIO0 + offset,
  47. GPIO_SET_MASK);
  48. }
  49. static int tps65910_gpio_output(struct gpio_chip *gc, unsigned offset,
  50. int value)
  51. {
  52. struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
  53. struct tps65910 *tps65910 = tps65910_gpio->tps65910;
  54. /* Set the initial value */
  55. tps65910_gpio_set(gc, offset, value);
  56. return tps65910_reg_set_bits(tps65910, TPS65910_GPIO0 + offset,
  57. GPIO_CFG_MASK);
  58. }
  59. static int tps65910_gpio_input(struct gpio_chip *gc, unsigned offset)
  60. {
  61. struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
  62. struct tps65910 *tps65910 = tps65910_gpio->tps65910;
  63. return tps65910_reg_clear_bits(tps65910, TPS65910_GPIO0 + offset,
  64. GPIO_CFG_MASK);
  65. }
  66. #ifdef CONFIG_OF
  67. static struct tps65910_board *tps65910_parse_dt_for_gpio(struct device *dev,
  68. struct tps65910 *tps65910, int chip_ngpio)
  69. {
  70. struct tps65910_board *tps65910_board = tps65910->of_plat_data;
  71. unsigned int prop_array[TPS6591X_MAX_NUM_GPIO];
  72. int ngpio = min(chip_ngpio, TPS6591X_MAX_NUM_GPIO);
  73. int ret;
  74. int idx;
  75. tps65910_board->gpio_base = -1;
  76. ret = of_property_read_u32_array(tps65910->dev->of_node,
  77. "ti,en-gpio-sleep", prop_array, ngpio);
  78. if (ret < 0) {
  79. dev_dbg(dev, "ti,en-gpio-sleep not specified\n");
  80. return tps65910_board;
  81. }
  82. for (idx = 0; idx < ngpio; idx++)
  83. tps65910_board->en_gpio_sleep[idx] = (prop_array[idx] != 0);
  84. return tps65910_board;
  85. }
  86. #else
  87. static struct tps65910_board *tps65910_parse_dt_for_gpio(struct device *dev,
  88. struct tps65910 *tps65910, int chip_ngpio)
  89. {
  90. return NULL;
  91. }
  92. #endif
  93. static int tps65910_gpio_probe(struct platform_device *pdev)
  94. {
  95. struct tps65910 *tps65910 = dev_get_drvdata(pdev->dev.parent);
  96. struct tps65910_board *pdata = dev_get_platdata(tps65910->dev);
  97. struct tps65910_gpio *tps65910_gpio;
  98. int ret;
  99. int i;
  100. tps65910_gpio = devm_kzalloc(&pdev->dev,
  101. sizeof(*tps65910_gpio), GFP_KERNEL);
  102. if (!tps65910_gpio)
  103. return -ENOMEM;
  104. tps65910_gpio->tps65910 = tps65910;
  105. tps65910_gpio->gpio_chip.owner = THIS_MODULE;
  106. tps65910_gpio->gpio_chip.label = tps65910->i2c_client->name;
  107. switch (tps65910_chip_id(tps65910)) {
  108. case TPS65910:
  109. tps65910_gpio->gpio_chip.ngpio = TPS65910_NUM_GPIO;
  110. break;
  111. case TPS65911:
  112. tps65910_gpio->gpio_chip.ngpio = TPS65911_NUM_GPIO;
  113. break;
  114. default:
  115. return -EINVAL;
  116. }
  117. tps65910_gpio->gpio_chip.can_sleep = true;
  118. tps65910_gpio->gpio_chip.direction_input = tps65910_gpio_input;
  119. tps65910_gpio->gpio_chip.direction_output = tps65910_gpio_output;
  120. tps65910_gpio->gpio_chip.set = tps65910_gpio_set;
  121. tps65910_gpio->gpio_chip.get = tps65910_gpio_get;
  122. tps65910_gpio->gpio_chip.parent = &pdev->dev;
  123. #ifdef CONFIG_OF_GPIO
  124. tps65910_gpio->gpio_chip.of_node = tps65910->dev->of_node;
  125. #endif
  126. if (pdata && pdata->gpio_base)
  127. tps65910_gpio->gpio_chip.base = pdata->gpio_base;
  128. else
  129. tps65910_gpio->gpio_chip.base = -1;
  130. if (!pdata && tps65910->dev->of_node)
  131. pdata = tps65910_parse_dt_for_gpio(&pdev->dev, tps65910,
  132. tps65910_gpio->gpio_chip.ngpio);
  133. if (!pdata)
  134. goto skip_init;
  135. /* Configure sleep control for gpios if provided */
  136. for (i = 0; i < tps65910_gpio->gpio_chip.ngpio; ++i) {
  137. if (!pdata->en_gpio_sleep[i])
  138. continue;
  139. ret = tps65910_reg_set_bits(tps65910,
  140. TPS65910_GPIO0 + i, GPIO_SLEEP_MASK);
  141. if (ret < 0)
  142. dev_warn(tps65910->dev,
  143. "GPIO Sleep setting failed with err %d\n", ret);
  144. }
  145. skip_init:
  146. ret = devm_gpiochip_add_data(&pdev->dev, &tps65910_gpio->gpio_chip,
  147. tps65910_gpio);
  148. if (ret < 0) {
  149. dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
  150. return ret;
  151. }
  152. platform_set_drvdata(pdev, tps65910_gpio);
  153. return ret;
  154. }
  155. static struct platform_driver tps65910_gpio_driver = {
  156. .driver.name = "tps65910-gpio",
  157. .probe = tps65910_gpio_probe,
  158. };
  159. static int __init tps65910_gpio_init(void)
  160. {
  161. return platform_driver_register(&tps65910_gpio_driver);
  162. }
  163. subsys_initcall(tps65910_gpio_init);