gpio-tps65912.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 = 1,
  74. .ngpio = 5,
  75. .base = -1,
  76. };
  77. static int __devinit tps65912_gpio_probe(struct platform_device *pdev)
  78. {
  79. struct tps65912 *tps65912 = dev_get_drvdata(pdev->dev.parent);
  80. struct tps65912_board *pdata = tps65912->dev->platform_data;
  81. struct tps65912_gpio_data *tps65912_gpio;
  82. int ret;
  83. tps65912_gpio = kzalloc(sizeof(*tps65912_gpio), 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.dev = &pdev->dev;
  89. if (pdata && pdata->gpio_base)
  90. tps65912_gpio->gpio_chip.base = pdata->gpio_base;
  91. ret = gpiochip_add(&tps65912_gpio->gpio_chip);
  92. if (ret < 0) {
  93. dev_err(&pdev->dev, "Failed to register gpiochip, %d\n", ret);
  94. goto err;
  95. }
  96. platform_set_drvdata(pdev, tps65912_gpio);
  97. return ret;
  98. err:
  99. kfree(tps65912_gpio);
  100. return ret;
  101. }
  102. static int __devexit tps65912_gpio_remove(struct platform_device *pdev)
  103. {
  104. struct tps65912_gpio_data *tps65912_gpio = platform_get_drvdata(pdev);
  105. int ret;
  106. ret = gpiochip_remove(&tps65912_gpio->gpio_chip);
  107. if (ret == 0)
  108. kfree(tps65912_gpio);
  109. return ret;
  110. }
  111. static struct platform_driver tps65912_gpio_driver = {
  112. .driver = {
  113. .name = "tps65912-gpio",
  114. .owner = THIS_MODULE,
  115. },
  116. .probe = tps65912_gpio_probe,
  117. .remove = __devexit_p(tps65912_gpio_remove),
  118. };
  119. static int __init tps65912_gpio_init(void)
  120. {
  121. return platform_driver_register(&tps65912_gpio_driver);
  122. }
  123. subsys_initcall(tps65912_gpio_init);
  124. static void __exit tps65912_gpio_exit(void)
  125. {
  126. platform_driver_unregister(&tps65912_gpio_driver);
  127. }
  128. module_exit(tps65912_gpio_exit);
  129. MODULE_AUTHOR("Margarita Olaya Cabrera <magi@slimlogic.co.uk>");
  130. MODULE_DESCRIPTION("GPIO interface for TPS65912 PMICs");
  131. MODULE_LICENSE("GPL v2");
  132. MODULE_ALIAS("platform:tps65912-gpio");