gpio-tps65912.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * GPIO driver for TI TPS65912x PMICs
  3. *
  4. * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
  5. * Andrew F. Davis <afd@ti.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  12. * kind, whether expressed or implied; without even the implied warranty
  13. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License version 2 for more details.
  15. *
  16. * Based on the Arizona GPIO driver and the previous TPS65912 driver by
  17. * Margarita Olaya Cabrera <magi@slimlogic.co.uk>
  18. */
  19. #include <linux/gpio.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/mfd/tps65912.h>
  23. struct tps65912_gpio {
  24. struct gpio_chip gpio_chip;
  25. struct tps65912 *tps;
  26. };
  27. static int tps65912_gpio_get_direction(struct gpio_chip *gc,
  28. unsigned offset)
  29. {
  30. struct tps65912_gpio *gpio = gpiochip_get_data(gc);
  31. int ret, val;
  32. ret = regmap_read(gpio->tps->regmap, TPS65912_GPIO1 + offset, &val);
  33. if (ret)
  34. return ret;
  35. if (val & GPIO_CFG_MASK)
  36. return GPIOF_DIR_OUT;
  37. else
  38. return GPIOF_DIR_IN;
  39. }
  40. static int tps65912_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
  41. {
  42. struct tps65912_gpio *gpio = gpiochip_get_data(gc);
  43. return regmap_update_bits(gpio->tps->regmap, TPS65912_GPIO1 + offset,
  44. GPIO_CFG_MASK, 0);
  45. }
  46. static int tps65912_gpio_direction_output(struct gpio_chip *gc,
  47. unsigned offset, int value)
  48. {
  49. struct tps65912_gpio *gpio = gpiochip_get_data(gc);
  50. /* Set the initial value */
  51. regmap_update_bits(gpio->tps->regmap, TPS65912_GPIO1 + offset,
  52. GPIO_SET_MASK, value ? GPIO_SET_MASK : 0);
  53. return regmap_update_bits(gpio->tps->regmap, TPS65912_GPIO1 + offset,
  54. GPIO_CFG_MASK, GPIO_CFG_MASK);
  55. }
  56. static int tps65912_gpio_get(struct gpio_chip *gc, unsigned offset)
  57. {
  58. struct tps65912_gpio *gpio = gpiochip_get_data(gc);
  59. int ret, val;
  60. ret = regmap_read(gpio->tps->regmap, TPS65912_GPIO1 + offset, &val);
  61. if (ret)
  62. return ret;
  63. if (val & GPIO_STS_MASK)
  64. return 1;
  65. return 0;
  66. }
  67. static void tps65912_gpio_set(struct gpio_chip *gc, unsigned offset,
  68. int value)
  69. {
  70. struct tps65912_gpio *gpio = gpiochip_get_data(gc);
  71. regmap_update_bits(gpio->tps->regmap, TPS65912_GPIO1 + offset,
  72. GPIO_SET_MASK, value ? GPIO_SET_MASK : 0);
  73. }
  74. static const struct gpio_chip template_chip = {
  75. .label = "tps65912-gpio",
  76. .owner = THIS_MODULE,
  77. .get_direction = tps65912_gpio_get_direction,
  78. .direction_input = tps65912_gpio_direction_input,
  79. .direction_output = tps65912_gpio_direction_output,
  80. .get = tps65912_gpio_get,
  81. .set = tps65912_gpio_set,
  82. .base = -1,
  83. .ngpio = 5,
  84. .can_sleep = true,
  85. };
  86. static int tps65912_gpio_probe(struct platform_device *pdev)
  87. {
  88. struct tps65912 *tps = dev_get_drvdata(pdev->dev.parent);
  89. struct tps65912_gpio *gpio;
  90. int ret;
  91. gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
  92. if (!gpio)
  93. return -ENOMEM;
  94. gpio->tps = dev_get_drvdata(pdev->dev.parent);
  95. gpio->gpio_chip = template_chip;
  96. gpio->gpio_chip.parent = tps->dev;
  97. ret = devm_gpiochip_add_data(&pdev->dev, &gpio->gpio_chip,
  98. gpio);
  99. if (ret < 0) {
  100. dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
  101. return ret;
  102. }
  103. platform_set_drvdata(pdev, gpio);
  104. return 0;
  105. }
  106. static const struct platform_device_id tps65912_gpio_id_table[] = {
  107. { "tps65912-gpio", },
  108. { /* sentinel */ }
  109. };
  110. MODULE_DEVICE_TABLE(platform, tps65912_gpio_id_table);
  111. static struct platform_driver tps65912_gpio_driver = {
  112. .driver = {
  113. .name = "tps65912-gpio",
  114. },
  115. .probe = tps65912_gpio_probe,
  116. .id_table = tps65912_gpio_id_table,
  117. };
  118. module_platform_driver(tps65912_gpio_driver);
  119. MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
  120. MODULE_DESCRIPTION("TPS65912 GPIO driver");
  121. MODULE_LICENSE("GPL v2");