gpio-74xx-mmio.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * 74xx MMIO GPIO driver
  4. *
  5. * Copyright (C) 2014 Alexander Shiyan <[email protected]>
  6. */
  7. #include <linux/bits.h>
  8. #include <linux/err.h>
  9. #include <linux/gpio/driver.h>
  10. #include <linux/mod_devicetable.h>
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/property.h>
  14. #define MMIO_74XX_DIR_IN BIT(8)
  15. #define MMIO_74XX_DIR_OUT BIT(9)
  16. #define MMIO_74XX_BIT_CNT(x) ((x) & GENMASK(7, 0))
  17. struct mmio_74xx_gpio_priv {
  18. struct gpio_chip gc;
  19. unsigned flags;
  20. };
  21. static const struct of_device_id mmio_74xx_gpio_ids[] = {
  22. {
  23. .compatible = "ti,741g125",
  24. .data = (const void *)(MMIO_74XX_DIR_IN | 1),
  25. },
  26. {
  27. .compatible = "ti,742g125",
  28. .data = (const void *)(MMIO_74XX_DIR_IN | 2),
  29. },
  30. {
  31. .compatible = "ti,74125",
  32. .data = (const void *)(MMIO_74XX_DIR_IN | 4),
  33. },
  34. {
  35. .compatible = "ti,74365",
  36. .data = (const void *)(MMIO_74XX_DIR_IN | 6),
  37. },
  38. {
  39. .compatible = "ti,74244",
  40. .data = (const void *)(MMIO_74XX_DIR_IN | 8),
  41. },
  42. {
  43. .compatible = "ti,741624",
  44. .data = (const void *)(MMIO_74XX_DIR_IN | 16),
  45. },
  46. {
  47. .compatible = "ti,741g74",
  48. .data = (const void *)(MMIO_74XX_DIR_OUT | 1),
  49. },
  50. {
  51. .compatible = "ti,7474",
  52. .data = (const void *)(MMIO_74XX_DIR_OUT | 2),
  53. },
  54. {
  55. .compatible = "ti,74175",
  56. .data = (const void *)(MMIO_74XX_DIR_OUT | 4),
  57. },
  58. {
  59. .compatible = "ti,74174",
  60. .data = (const void *)(MMIO_74XX_DIR_OUT | 6),
  61. },
  62. {
  63. .compatible = "ti,74273",
  64. .data = (const void *)(MMIO_74XX_DIR_OUT | 8),
  65. },
  66. {
  67. .compatible = "ti,7416374",
  68. .data = (const void *)(MMIO_74XX_DIR_OUT | 16),
  69. },
  70. { }
  71. };
  72. MODULE_DEVICE_TABLE(of, mmio_74xx_gpio_ids);
  73. static int mmio_74xx_get_direction(struct gpio_chip *gc, unsigned offset)
  74. {
  75. struct mmio_74xx_gpio_priv *priv = gpiochip_get_data(gc);
  76. if (priv->flags & MMIO_74XX_DIR_OUT)
  77. return GPIO_LINE_DIRECTION_OUT;
  78. return GPIO_LINE_DIRECTION_IN;
  79. }
  80. static int mmio_74xx_dir_in(struct gpio_chip *gc, unsigned int gpio)
  81. {
  82. struct mmio_74xx_gpio_priv *priv = gpiochip_get_data(gc);
  83. if (priv->flags & MMIO_74XX_DIR_IN)
  84. return 0;
  85. return -ENOTSUPP;
  86. }
  87. static int mmio_74xx_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  88. {
  89. struct mmio_74xx_gpio_priv *priv = gpiochip_get_data(gc);
  90. if (priv->flags & MMIO_74XX_DIR_OUT) {
  91. gc->set(gc, gpio, val);
  92. return 0;
  93. }
  94. return -ENOTSUPP;
  95. }
  96. static int mmio_74xx_gpio_probe(struct platform_device *pdev)
  97. {
  98. struct mmio_74xx_gpio_priv *priv;
  99. void __iomem *dat;
  100. int err;
  101. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  102. if (!priv)
  103. return -ENOMEM;
  104. priv->flags = (uintptr_t)device_get_match_data(&pdev->dev);
  105. dat = devm_platform_ioremap_resource(pdev, 0);
  106. if (IS_ERR(dat))
  107. return PTR_ERR(dat);
  108. err = bgpio_init(&priv->gc, &pdev->dev,
  109. DIV_ROUND_UP(MMIO_74XX_BIT_CNT(priv->flags), 8),
  110. dat, NULL, NULL, NULL, NULL, 0);
  111. if (err)
  112. return err;
  113. priv->gc.direction_input = mmio_74xx_dir_in;
  114. priv->gc.direction_output = mmio_74xx_dir_out;
  115. priv->gc.get_direction = mmio_74xx_get_direction;
  116. priv->gc.ngpio = MMIO_74XX_BIT_CNT(priv->flags);
  117. priv->gc.owner = THIS_MODULE;
  118. platform_set_drvdata(pdev, priv);
  119. return devm_gpiochip_add_data(&pdev->dev, &priv->gc, priv);
  120. }
  121. static struct platform_driver mmio_74xx_gpio_driver = {
  122. .driver = {
  123. .name = "74xx-mmio-gpio",
  124. .of_match_table = mmio_74xx_gpio_ids,
  125. },
  126. .probe = mmio_74xx_gpio_probe,
  127. };
  128. module_platform_driver(mmio_74xx_gpio_driver);
  129. MODULE_LICENSE("GPL");
  130. MODULE_AUTHOR("Alexander Shiyan <[email protected]>");
  131. MODULE_DESCRIPTION("74xx MMIO GPIO driver");