gpio-tps65912.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #define to_tgd(gc) container_of(gc, struct tps65912_gpio_data, gpio_chip)
  27. static int tps65912_gpio_get(struct gpio_chip *gc, unsigned offset)
  28. {
  29. struct tps65912_gpio_data *tps65912_gpio = to_tgd(gc);
  30. struct tps65912 *tps65912 = tps65912_gpio->tps65912;
  31. int val;
  32. val = tps65912_reg_read(tps65912, TPS65912_GPIO1 + offset);
  33. if (val & GPIO_STS_MASK)
  34. return 1;
  35. return 0;
  36. }
  37. static void tps65912_gpio_set(struct gpio_chip *gc, unsigned offset,
  38. int value)
  39. {
  40. struct tps65912_gpio_data *tps65912_gpio = to_tgd(gc);
  41. struct tps65912 *tps65912 = tps65912_gpio->tps65912;
  42. if (value)
  43. tps65912_set_bits(tps65912, TPS65912_GPIO1 + offset,
  44. GPIO_SET_MASK);
  45. else
  46. tps65912_clear_bits(tps65912, TPS65912_GPIO1 + offset,
  47. GPIO_SET_MASK);
  48. }
  49. static int tps65912_gpio_output(struct gpio_chip *gc, unsigned offset,
  50. int value)
  51. {
  52. struct tps65912_gpio_data *tps65912_gpio = to_tgd(gc);
  53. struct tps65912 *tps65912 = tps65912_gpio->tps65912;
  54. /* Set the initial value */
  55. tps65912_gpio_set(gc, offset, value);
  56. return tps65912_set_bits(tps65912, TPS65912_GPIO1 + offset,
  57. GPIO_CFG_MASK);
  58. }
  59. static int tps65912_gpio_input(struct gpio_chip *gc, unsigned offset)
  60. {
  61. struct tps65912_gpio_data *tps65912_gpio = to_tgd(gc);
  62. struct tps65912 *tps65912 = tps65912_gpio->tps65912;
  63. return tps65912_clear_bits(tps65912, TPS65912_GPIO1 + offset,
  64. GPIO_CFG_MASK);
  65. }
  66. static struct gpio_chip template_chip = {
  67. .label = "tps65912",
  68. .owner = THIS_MODULE,
  69. .direction_input = tps65912_gpio_input,
  70. .direction_output = tps65912_gpio_output,
  71. .get = tps65912_gpio_get,
  72. .set = tps65912_gpio_set,
  73. .can_sleep = true,
  74. .ngpio = 5,
  75. .base = -1,
  76. };
  77. static int tps65912_gpio_probe(struct platform_device *pdev)
  78. {
  79. struct tps65912 *tps65912 = dev_get_drvdata(pdev->dev.parent);
  80. struct tps65912_board *pdata = dev_get_platdata(tps65912->dev);
  81. struct tps65912_gpio_data *tps65912_gpio;
  82. int ret;
  83. tps65912_gpio = devm_kzalloc(&pdev->dev, sizeof(*tps65912_gpio),
  84. GFP_KERNEL);
  85. if (tps65912_gpio == NULL)
  86. return -ENOMEM;
  87. tps65912_gpio->tps65912 = tps65912;
  88. tps65912_gpio->gpio_chip = template_chip;
  89. tps65912_gpio->gpio_chip.dev = &pdev->dev;
  90. if (pdata && pdata->gpio_base)
  91. tps65912_gpio->gpio_chip.base = pdata->gpio_base;
  92. ret = gpiochip_add(&tps65912_gpio->gpio_chip);
  93. if (ret < 0) {
  94. dev_err(&pdev->dev, "Failed to register gpiochip, %d\n", ret);
  95. return ret;
  96. }
  97. platform_set_drvdata(pdev, tps65912_gpio);
  98. return ret;
  99. }
  100. static int tps65912_gpio_remove(struct platform_device *pdev)
  101. {
  102. struct tps65912_gpio_data *tps65912_gpio = platform_get_drvdata(pdev);
  103. gpiochip_remove(&tps65912_gpio->gpio_chip);
  104. return 0;
  105. }
  106. static struct platform_driver tps65912_gpio_driver = {
  107. .driver = {
  108. .name = "tps65912-gpio",
  109. },
  110. .probe = tps65912_gpio_probe,
  111. .remove = tps65912_gpio_remove,
  112. };
  113. static int __init tps65912_gpio_init(void)
  114. {
  115. return platform_driver_register(&tps65912_gpio_driver);
  116. }
  117. subsys_initcall(tps65912_gpio_init);
  118. static void __exit tps65912_gpio_exit(void)
  119. {
  120. platform_driver_unregister(&tps65912_gpio_driver);
  121. }
  122. module_exit(tps65912_gpio_exit);
  123. MODULE_AUTHOR("Margarita Olaya Cabrera <magi@slimlogic.co.uk>");
  124. MODULE_DESCRIPTION("GPIO interface for TPS65912 PMICs");
  125. MODULE_LICENSE("GPL v2");
  126. MODULE_ALIAS("platform:tps65912-gpio");