gpio-tps65086.c 3.4 KB

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