gpio-twl6040.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Access to GPOs on TWL6040 chip
  4. *
  5. * Copyright (C) 2012 Texas Instruments, Inc.
  6. *
  7. * Authors:
  8. * Sergio Aguirre <saaguirre@ti.com>
  9. * Peter Ujfalusi <peter.ujfalusi@ti.com>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/kthread.h>
  14. #include <linux/irq.h>
  15. #include <linux/gpio/driver.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/bitops.h>
  18. #include <linux/of.h>
  19. #include <linux/mfd/twl6040.h>
  20. static int twl6040gpo_get(struct gpio_chip *chip, unsigned offset)
  21. {
  22. struct twl6040 *twl6040 = dev_get_drvdata(chip->parent->parent);
  23. int ret = 0;
  24. ret = twl6040_reg_read(twl6040, TWL6040_REG_GPOCTL);
  25. if (ret < 0)
  26. return ret;
  27. return !!(ret & BIT(offset));
  28. }
  29. static int twl6040gpo_get_direction(struct gpio_chip *chip, unsigned offset)
  30. {
  31. /* This means "out" */
  32. return 0;
  33. }
  34. static int twl6040gpo_direction_out(struct gpio_chip *chip, unsigned offset,
  35. int value)
  36. {
  37. /* This only drives GPOs, and can't change direction */
  38. return 0;
  39. }
  40. static void twl6040gpo_set(struct gpio_chip *chip, unsigned offset, int value)
  41. {
  42. struct twl6040 *twl6040 = dev_get_drvdata(chip->parent->parent);
  43. int ret;
  44. u8 gpoctl;
  45. ret = twl6040_reg_read(twl6040, TWL6040_REG_GPOCTL);
  46. if (ret < 0)
  47. return;
  48. if (value)
  49. gpoctl = ret | BIT(offset);
  50. else
  51. gpoctl = ret & ~BIT(offset);
  52. twl6040_reg_write(twl6040, TWL6040_REG_GPOCTL, gpoctl);
  53. }
  54. static struct gpio_chip twl6040gpo_chip = {
  55. .label = "twl6040",
  56. .owner = THIS_MODULE,
  57. .get = twl6040gpo_get,
  58. .direction_output = twl6040gpo_direction_out,
  59. .get_direction = twl6040gpo_get_direction,
  60. .set = twl6040gpo_set,
  61. .can_sleep = true,
  62. };
  63. /*----------------------------------------------------------------------*/
  64. static int gpo_twl6040_probe(struct platform_device *pdev)
  65. {
  66. struct device *twl6040_core_dev = pdev->dev.parent;
  67. struct twl6040 *twl6040 = dev_get_drvdata(twl6040_core_dev);
  68. int ret;
  69. twl6040gpo_chip.base = -1;
  70. if (twl6040_get_revid(twl6040) < TWL6041_REV_ES2_0)
  71. twl6040gpo_chip.ngpio = 3; /* twl6040 have 3 GPO */
  72. else
  73. twl6040gpo_chip.ngpio = 1; /* twl6041 have 1 GPO */
  74. twl6040gpo_chip.parent = &pdev->dev;
  75. #ifdef CONFIG_OF_GPIO
  76. twl6040gpo_chip.of_node = twl6040_core_dev->of_node;
  77. #endif
  78. ret = devm_gpiochip_add_data(&pdev->dev, &twl6040gpo_chip, NULL);
  79. if (ret < 0) {
  80. dev_err(&pdev->dev, "could not register gpiochip, %d\n", ret);
  81. twl6040gpo_chip.ngpio = 0;
  82. }
  83. return ret;
  84. }
  85. /* Note: this hardware lives inside an I2C-based multi-function device. */
  86. MODULE_ALIAS("platform:twl6040-gpo");
  87. static struct platform_driver gpo_twl6040_driver = {
  88. .driver = {
  89. .name = "twl6040-gpo",
  90. },
  91. .probe = gpo_twl6040_probe,
  92. };
  93. module_platform_driver(gpo_twl6040_driver);
  94. MODULE_AUTHOR("Texas Instruments, Inc.");
  95. MODULE_DESCRIPTION("GPO interface for TWL6040");
  96. MODULE_LICENSE("GPL");