gpio-twl6040.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Access to GPOs on TWL6040 chip
  3. *
  4. * Copyright (C) 2012 Texas Instruments, Inc.
  5. *
  6. * Authors:
  7. * Sergio Aguirre <saaguirre@ti.com>
  8. * Peter Ujfalusi <peter.ujfalusi@ti.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/kthread.h>
  27. #include <linux/irq.h>
  28. #include <linux/gpio.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/of.h>
  31. #include <linux/mfd/twl6040.h>
  32. static struct gpio_chip twl6040gpo_chip;
  33. static int twl6040gpo_get(struct gpio_chip *chip, unsigned offset)
  34. {
  35. struct twl6040 *twl6040 = dev_get_drvdata(chip->dev->parent);
  36. int ret = 0;
  37. ret = twl6040_reg_read(twl6040, TWL6040_REG_GPOCTL);
  38. if (ret < 0)
  39. return ret;
  40. return (ret >> offset) & 1;
  41. }
  42. static int twl6040gpo_direction_out(struct gpio_chip *chip, unsigned offset,
  43. int value)
  44. {
  45. /* This only drives GPOs, and can't change direction */
  46. return 0;
  47. }
  48. static void twl6040gpo_set(struct gpio_chip *chip, unsigned offset, int value)
  49. {
  50. struct twl6040 *twl6040 = dev_get_drvdata(chip->dev->parent);
  51. int ret;
  52. u8 gpoctl;
  53. ret = twl6040_reg_read(twl6040, TWL6040_REG_GPOCTL);
  54. if (ret < 0)
  55. return;
  56. if (value)
  57. gpoctl = ret | (1 << offset);
  58. else
  59. gpoctl = ret & ~(1 << offset);
  60. twl6040_reg_write(twl6040, TWL6040_REG_GPOCTL, gpoctl);
  61. }
  62. static struct gpio_chip twl6040gpo_chip = {
  63. .label = "twl6040",
  64. .owner = THIS_MODULE,
  65. .get = twl6040gpo_get,
  66. .direction_output = twl6040gpo_direction_out,
  67. .set = twl6040gpo_set,
  68. .can_sleep = 1,
  69. };
  70. /*----------------------------------------------------------------------*/
  71. static int __devinit gpo_twl6040_probe(struct platform_device *pdev)
  72. {
  73. struct twl6040_gpo_data *pdata = pdev->dev.platform_data;
  74. struct device *twl6040_core_dev = pdev->dev.parent;
  75. struct twl6040 *twl6040 = dev_get_drvdata(twl6040_core_dev);
  76. int ret;
  77. if (pdata)
  78. twl6040gpo_chip.base = pdata->gpio_base;
  79. else
  80. twl6040gpo_chip.base = -1;
  81. if (twl6040_get_revid(twl6040) < TWL6041_REV_ES2_0)
  82. twl6040gpo_chip.ngpio = 3; /* twl6040 have 3 GPO */
  83. else
  84. twl6040gpo_chip.ngpio = 1; /* twl6041 have 1 GPO */
  85. twl6040gpo_chip.dev = &pdev->dev;
  86. #ifdef CONFIG_OF_GPIO
  87. twl6040gpo_chip.of_node = twl6040_core_dev->of_node;
  88. #endif
  89. ret = gpiochip_add(&twl6040gpo_chip);
  90. if (ret < 0) {
  91. dev_err(&pdev->dev, "could not register gpiochip, %d\n", ret);
  92. twl6040gpo_chip.ngpio = 0;
  93. }
  94. return ret;
  95. }
  96. static int __devexit gpo_twl6040_remove(struct platform_device *pdev)
  97. {
  98. return gpiochip_remove(&twl6040gpo_chip);
  99. }
  100. /* Note: this hardware lives inside an I2C-based multi-function device. */
  101. MODULE_ALIAS("platform:twl6040-gpo");
  102. static struct platform_driver gpo_twl6040_driver = {
  103. .driver = {
  104. .name = "twl6040-gpo",
  105. .owner = THIS_MODULE,
  106. },
  107. .probe = gpo_twl6040_probe,
  108. .remove = gpo_twl6040_remove,
  109. };
  110. module_platform_driver(gpo_twl6040_driver);
  111. MODULE_AUTHOR("Texas Instruments, Inc.");
  112. MODULE_DESCRIPTION("GPO interface for TWL6040");
  113. MODULE_LICENSE("GPL");