gpio-twl6040.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. return GPIO_LINE_DIRECTION_OUT;
  32. }
  33. static int twl6040gpo_direction_out(struct gpio_chip *chip, unsigned offset,
  34. int value)
  35. {
  36. /* This only drives GPOs, and can't change direction */
  37. return 0;
  38. }
  39. static void twl6040gpo_set(struct gpio_chip *chip, unsigned offset, int value)
  40. {
  41. struct twl6040 *twl6040 = dev_get_drvdata(chip->parent->parent);
  42. int ret;
  43. u8 gpoctl;
  44. ret = twl6040_reg_read(twl6040, TWL6040_REG_GPOCTL);
  45. if (ret < 0)
  46. return;
  47. if (value)
  48. gpoctl = ret | BIT(offset);
  49. else
  50. gpoctl = ret & ~BIT(offset);
  51. twl6040_reg_write(twl6040, TWL6040_REG_GPOCTL, gpoctl);
  52. }
  53. static struct gpio_chip twl6040gpo_chip = {
  54. .label = "twl6040",
  55. .owner = THIS_MODULE,
  56. .get = twl6040gpo_get,
  57. .direction_output = twl6040gpo_direction_out,
  58. .get_direction = twl6040gpo_get_direction,
  59. .set = twl6040gpo_set,
  60. .can_sleep = true,
  61. };
  62. /*----------------------------------------------------------------------*/
  63. static int gpo_twl6040_probe(struct platform_device *pdev)
  64. {
  65. struct device *twl6040_core_dev = pdev->dev.parent;
  66. struct twl6040 *twl6040 = dev_get_drvdata(twl6040_core_dev);
  67. int ret;
  68. twl6040gpo_chip.base = -1;
  69. if (twl6040_get_revid(twl6040) < TWL6041_REV_ES2_0)
  70. twl6040gpo_chip.ngpio = 3; /* twl6040 have 3 GPO */
  71. else
  72. twl6040gpo_chip.ngpio = 1; /* twl6041 have 1 GPO */
  73. twl6040gpo_chip.parent = &pdev->dev;
  74. #ifdef CONFIG_OF_GPIO
  75. twl6040gpo_chip.of_node = twl6040_core_dev->of_node;
  76. #endif
  77. ret = devm_gpiochip_add_data(&pdev->dev, &twl6040gpo_chip, NULL);
  78. if (ret < 0) {
  79. dev_err(&pdev->dev, "could not register gpiochip, %d\n", ret);
  80. twl6040gpo_chip.ngpio = 0;
  81. }
  82. return ret;
  83. }
  84. /* Note: this hardware lives inside an I2C-based multi-function device. */
  85. MODULE_ALIAS("platform:twl6040-gpo");
  86. static struct platform_driver gpo_twl6040_driver = {
  87. .driver = {
  88. .name = "twl6040-gpo",
  89. },
  90. .probe = gpo_twl6040_probe,
  91. };
  92. module_platform_driver(gpo_twl6040_driver);
  93. MODULE_AUTHOR("Texas Instruments, Inc.");
  94. MODULE_DESCRIPTION("GPO interface for TWL6040");
  95. MODULE_LICENSE("GPL");