gpio-74xx-mmio.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * 74xx MMIO GPIO driver
  3. *
  4. * Copyright (C) 2014 Alexander Shiyan <shc_work@mail.ru>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/err.h>
  12. #include <linux/module.h>
  13. #include <linux/of_device.h>
  14. #include <linux/gpio/driver.h>
  15. #include <linux/platform_device.h>
  16. #define MMIO_74XX_DIR_IN (0 << 8)
  17. #define MMIO_74XX_DIR_OUT (1 << 8)
  18. #define MMIO_74XX_BIT_CNT(x) ((x) & 0xff)
  19. struct mmio_74xx_gpio_priv {
  20. struct gpio_chip gc;
  21. unsigned flags;
  22. };
  23. static const struct of_device_id mmio_74xx_gpio_ids[] = {
  24. {
  25. .compatible = "ti,741g125",
  26. .data = (const void *)(MMIO_74XX_DIR_IN | 1),
  27. },
  28. {
  29. .compatible = "ti,742g125",
  30. .data = (const void *)(MMIO_74XX_DIR_IN | 2),
  31. },
  32. {
  33. .compatible = "ti,74125",
  34. .data = (const void *)(MMIO_74XX_DIR_IN | 4),
  35. },
  36. {
  37. .compatible = "ti,74365",
  38. .data = (const void *)(MMIO_74XX_DIR_IN | 6),
  39. },
  40. {
  41. .compatible = "ti,74244",
  42. .data = (const void *)(MMIO_74XX_DIR_IN | 8),
  43. },
  44. {
  45. .compatible = "ti,741624",
  46. .data = (const void *)(MMIO_74XX_DIR_IN | 16),
  47. },
  48. {
  49. .compatible = "ti,741g74",
  50. .data = (const void *)(MMIO_74XX_DIR_OUT | 1),
  51. },
  52. {
  53. .compatible = "ti,7474",
  54. .data = (const void *)(MMIO_74XX_DIR_OUT | 2),
  55. },
  56. {
  57. .compatible = "ti,74175",
  58. .data = (const void *)(MMIO_74XX_DIR_OUT | 4),
  59. },
  60. {
  61. .compatible = "ti,74174",
  62. .data = (const void *)(MMIO_74XX_DIR_OUT | 6),
  63. },
  64. {
  65. .compatible = "ti,74273",
  66. .data = (const void *)(MMIO_74XX_DIR_OUT | 8),
  67. },
  68. {
  69. .compatible = "ti,7416374",
  70. .data = (const void *)(MMIO_74XX_DIR_OUT | 16),
  71. },
  72. { }
  73. };
  74. MODULE_DEVICE_TABLE(of, mmio_74xx_gpio_ids);
  75. static int mmio_74xx_get_direction(struct gpio_chip *gc, unsigned offset)
  76. {
  77. struct mmio_74xx_gpio_priv *priv = gpiochip_get_data(gc);
  78. return !(priv->flags & MMIO_74XX_DIR_OUT);
  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. return (priv->flags & MMIO_74XX_DIR_OUT) ? -ENOTSUPP : 0;
  84. }
  85. static int mmio_74xx_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  86. {
  87. struct mmio_74xx_gpio_priv *priv = gpiochip_get_data(gc);
  88. if (priv->flags & MMIO_74XX_DIR_OUT) {
  89. gc->set(gc, gpio, val);
  90. return 0;
  91. }
  92. return -ENOTSUPP;
  93. }
  94. static int mmio_74xx_gpio_probe(struct platform_device *pdev)
  95. {
  96. const struct of_device_id *of_id;
  97. struct mmio_74xx_gpio_priv *priv;
  98. struct resource *res;
  99. void __iomem *dat;
  100. int err;
  101. of_id = of_match_device(mmio_74xx_gpio_ids, &pdev->dev);
  102. if (!of_id)
  103. return -ENODEV;
  104. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  105. if (!priv)
  106. return -ENOMEM;
  107. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  108. dat = devm_ioremap_resource(&pdev->dev, res);
  109. if (IS_ERR(dat))
  110. return PTR_ERR(dat);
  111. priv->flags = (uintptr_t) of_id->data;
  112. err = bgpio_init(&priv->gc, &pdev->dev,
  113. DIV_ROUND_UP(MMIO_74XX_BIT_CNT(priv->flags), 8),
  114. dat, NULL, NULL, NULL, NULL, 0);
  115. if (err)
  116. return err;
  117. priv->gc.direction_input = mmio_74xx_dir_in;
  118. priv->gc.direction_output = mmio_74xx_dir_out;
  119. priv->gc.get_direction = mmio_74xx_get_direction;
  120. priv->gc.ngpio = MMIO_74XX_BIT_CNT(priv->flags);
  121. priv->gc.owner = THIS_MODULE;
  122. platform_set_drvdata(pdev, priv);
  123. return devm_gpiochip_add_data(&pdev->dev, &priv->gc, priv);
  124. }
  125. static struct platform_driver mmio_74xx_gpio_driver = {
  126. .driver = {
  127. .name = "74xx-mmio-gpio",
  128. .of_match_table = mmio_74xx_gpio_ids,
  129. },
  130. .probe = mmio_74xx_gpio_probe,
  131. };
  132. module_platform_driver(mmio_74xx_gpio_driver);
  133. MODULE_LICENSE("GPL");
  134. MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
  135. MODULE_DESCRIPTION("74xx MMIO GPIO driver");