gpio-tps65912.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright 2011 Texas Instruments Inc.
  3. *
  4. * Author: Margarita Olaya <magi@slimlogic.co.uk>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This driver is based on wm8350 implementation.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/errno.h>
  16. #include <linux/gpio.h>
  17. #include <linux/mfd/core.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/seq_file.h>
  20. #include <linux/slab.h>
  21. #include <linux/mfd/tps65912.h>
  22. struct tps65912_gpio_data {
  23. struct tps65912 *tps65912;
  24. struct gpio_chip gpio_chip;
  25. };
  26. static int tps65912_gpio_get(struct gpio_chip *gc, unsigned offset)
  27. {
  28. struct tps65912_gpio_data *tps65912_gpio = gpiochip_get_data(gc);
  29. struct tps65912 *tps65912 = tps65912_gpio->tps65912;
  30. int val;
  31. val = tps65912_reg_read(tps65912, TPS65912_GPIO1 + offset);
  32. if (val & GPIO_STS_MASK)
  33. return 1;
  34. return 0;
  35. }
  36. static void tps65912_gpio_set(struct gpio_chip *gc, unsigned offset,
  37. int value)
  38. {
  39. struct tps65912_gpio_data *tps65912_gpio = gpiochip_get_data(gc);
  40. struct tps65912 *tps65912 = tps65912_gpio->tps65912;
  41. if (value)
  42. tps65912_set_bits(tps65912, TPS65912_GPIO1 + offset,
  43. GPIO_SET_MASK);
  44. else
  45. tps65912_clear_bits(tps65912, TPS65912_GPIO1 + offset,
  46. GPIO_SET_MASK);
  47. }
  48. static int tps65912_gpio_output(struct gpio_chip *gc, unsigned offset,
  49. int value)
  50. {
  51. struct tps65912_gpio_data *tps65912_gpio = gpiochip_get_data(gc);
  52. struct tps65912 *tps65912 = tps65912_gpio->tps65912;
  53. /* Set the initial value */
  54. tps65912_gpio_set(gc, offset, value);
  55. return tps65912_set_bits(tps65912, TPS65912_GPIO1 + offset,
  56. GPIO_CFG_MASK);
  57. }
  58. static int tps65912_gpio_input(struct gpio_chip *gc, unsigned offset)
  59. {
  60. struct tps65912_gpio_data *tps65912_gpio = gpiochip_get_data(gc);
  61. struct tps65912 *tps65912 = tps65912_gpio->tps65912;
  62. return tps65912_clear_bits(tps65912, TPS65912_GPIO1 + offset,
  63. GPIO_CFG_MASK);
  64. }
  65. static struct gpio_chip template_chip = {
  66. .label = "tps65912",
  67. .owner = THIS_MODULE,
  68. .direction_input = tps65912_gpio_input,
  69. .direction_output = tps65912_gpio_output,
  70. .get = tps65912_gpio_get,
  71. .set = tps65912_gpio_set,
  72. .can_sleep = true,
  73. .ngpio = 5,
  74. .base = -1,
  75. };
  76. static int tps65912_gpio_probe(struct platform_device *pdev)
  77. {
  78. struct tps65912 *tps65912 = dev_get_drvdata(pdev->dev.parent);
  79. struct tps65912_board *pdata = dev_get_platdata(tps65912->dev);
  80. struct tps65912_gpio_data *tps65912_gpio;
  81. int ret;
  82. tps65912_gpio = devm_kzalloc(&pdev->dev, sizeof(*tps65912_gpio),
  83. GFP_KERNEL);
  84. if (tps65912_gpio == NULL)
  85. return -ENOMEM;
  86. tps65912_gpio->tps65912 = tps65912;
  87. tps65912_gpio->gpio_chip = template_chip;
  88. tps65912_gpio->gpio_chip.parent = &pdev->dev;
  89. if (pdata && pdata->gpio_base)
  90. tps65912_gpio->gpio_chip.base = pdata->gpio_base;
  91. ret = gpiochip_add_data(&tps65912_gpio->gpio_chip, tps65912_gpio);
  92. if (ret < 0) {
  93. dev_err(&pdev->dev, "Failed to register gpiochip, %d\n", ret);
  94. return ret;
  95. }
  96. platform_set_drvdata(pdev, tps65912_gpio);
  97. return ret;
  98. }
  99. static int tps65912_gpio_remove(struct platform_device *pdev)
  100. {
  101. struct tps65912_gpio_data *tps65912_gpio = platform_get_drvdata(pdev);
  102. gpiochip_remove(&tps65912_gpio->gpio_chip);
  103. return 0;
  104. }
  105. static struct platform_driver tps65912_gpio_driver = {
  106. .driver = {
  107. .name = "tps65912-gpio",
  108. },
  109. .probe = tps65912_gpio_probe,
  110. .remove = tps65912_gpio_remove,
  111. };
  112. static int __init tps65912_gpio_init(void)
  113. {
  114. return platform_driver_register(&tps65912_gpio_driver);
  115. }
  116. subsys_initcall(tps65912_gpio_init);
  117. static void __exit tps65912_gpio_exit(void)
  118. {
  119. platform_driver_unregister(&tps65912_gpio_driver);
  120. }
  121. module_exit(tps65912_gpio_exit);
  122. MODULE_AUTHOR("Margarita Olaya Cabrera <magi@slimlogic.co.uk>");
  123. MODULE_DESCRIPTION("GPIO interface for TPS65912 PMICs");
  124. MODULE_LICENSE("GPL v2");
  125. MODULE_ALIAS("platform:tps65912-gpio");