gpio-74xx-mmio.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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/gpio.h>
  13. #include <linux/module.h>
  14. #include <linux/of_device.h>
  15. #include <linux/basic_mmio_gpio.h>
  16. #include <linux/platform_device.h>
  17. #define MMIO_74XX_DIR_IN (0 << 8)
  18. #define MMIO_74XX_DIR_OUT (1 << 8)
  19. #define MMIO_74XX_BIT_CNT(x) ((x) & 0xff)
  20. struct mmio_74xx_gpio_priv {
  21. struct bgpio_chip bgc;
  22. unsigned flags;
  23. };
  24. static const struct of_device_id mmio_74xx_gpio_ids[] = {
  25. {
  26. .compatible = "ti,741g125",
  27. .data = (const void *)(MMIO_74XX_DIR_IN | 1),
  28. },
  29. {
  30. .compatible = "ti,742g125",
  31. .data = (const void *)(MMIO_74XX_DIR_IN | 2),
  32. },
  33. {
  34. .compatible = "ti,74125",
  35. .data = (const void *)(MMIO_74XX_DIR_IN | 4),
  36. },
  37. {
  38. .compatible = "ti,74365",
  39. .data = (const void *)(MMIO_74XX_DIR_IN | 6),
  40. },
  41. {
  42. .compatible = "ti,74244",
  43. .data = (const void *)(MMIO_74XX_DIR_IN | 8),
  44. },
  45. {
  46. .compatible = "ti,741624",
  47. .data = (const void *)(MMIO_74XX_DIR_IN | 16),
  48. },
  49. {
  50. .compatible = "ti,741g74",
  51. .data = (const void *)(MMIO_74XX_DIR_OUT | 1),
  52. },
  53. {
  54. .compatible = "ti,7474",
  55. .data = (const void *)(MMIO_74XX_DIR_OUT | 2),
  56. },
  57. {
  58. .compatible = "ti,74175",
  59. .data = (const void *)(MMIO_74XX_DIR_OUT | 4),
  60. },
  61. {
  62. .compatible = "ti,74174",
  63. .data = (const void *)(MMIO_74XX_DIR_OUT | 6),
  64. },
  65. {
  66. .compatible = "ti,74273",
  67. .data = (const void *)(MMIO_74XX_DIR_OUT | 8),
  68. },
  69. {
  70. .compatible = "ti,7416374",
  71. .data = (const void *)(MMIO_74XX_DIR_OUT | 16),
  72. },
  73. { }
  74. };
  75. MODULE_DEVICE_TABLE(of, mmio_74xx_gpio_ids);
  76. static inline struct mmio_74xx_gpio_priv *to_74xx_gpio(struct gpio_chip *gc)
  77. {
  78. struct bgpio_chip *bgc = to_bgpio_chip(gc);
  79. return container_of(bgc, struct mmio_74xx_gpio_priv, bgc);
  80. }
  81. static int mmio_74xx_get_direction(struct gpio_chip *gc, unsigned offset)
  82. {
  83. struct mmio_74xx_gpio_priv *priv = to_74xx_gpio(gc);
  84. return (priv->flags & MMIO_74XX_DIR_OUT) ? GPIOF_DIR_OUT : GPIOF_DIR_IN;
  85. }
  86. static int mmio_74xx_dir_in(struct gpio_chip *gc, unsigned int gpio)
  87. {
  88. struct mmio_74xx_gpio_priv *priv = to_74xx_gpio(gc);
  89. return (priv->flags & MMIO_74XX_DIR_OUT) ? -ENOTSUPP : 0;
  90. }
  91. static int mmio_74xx_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  92. {
  93. struct mmio_74xx_gpio_priv *priv = to_74xx_gpio(gc);
  94. if (priv->flags & MMIO_74XX_DIR_OUT) {
  95. gc->set(gc, gpio, val);
  96. return 0;
  97. }
  98. return -ENOTSUPP;
  99. }
  100. static int mmio_74xx_gpio_probe(struct platform_device *pdev)
  101. {
  102. const struct of_device_id *of_id =
  103. of_match_device(mmio_74xx_gpio_ids, &pdev->dev);
  104. struct mmio_74xx_gpio_priv *priv;
  105. struct resource *res;
  106. void __iomem *dat;
  107. int err;
  108. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  109. if (!priv)
  110. return -ENOMEM;
  111. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  112. dat = devm_ioremap_resource(&pdev->dev, res);
  113. if (IS_ERR(dat))
  114. return PTR_ERR(dat);
  115. priv->flags = (unsigned)of_id->data;
  116. err = bgpio_init(&priv->bgc, &pdev->dev,
  117. DIV_ROUND_UP(MMIO_74XX_BIT_CNT(priv->flags), 8),
  118. dat, NULL, NULL, NULL, NULL, 0);
  119. if (err)
  120. return err;
  121. priv->bgc.gc.direction_input = mmio_74xx_dir_in;
  122. priv->bgc.gc.direction_output = mmio_74xx_dir_out;
  123. priv->bgc.gc.get_direction = mmio_74xx_get_direction;
  124. priv->bgc.gc.ngpio = MMIO_74XX_BIT_CNT(priv->flags);
  125. priv->bgc.gc.owner = THIS_MODULE;
  126. platform_set_drvdata(pdev, priv);
  127. return gpiochip_add(&priv->bgc.gc);
  128. }
  129. static int mmio_74xx_gpio_remove(struct platform_device *pdev)
  130. {
  131. struct mmio_74xx_gpio_priv *priv = platform_get_drvdata(pdev);
  132. return bgpio_remove(&priv->bgc);
  133. }
  134. static struct platform_driver mmio_74xx_gpio_driver = {
  135. .driver = {
  136. .name = "74xx-mmio-gpio",
  137. .of_match_table = mmio_74xx_gpio_ids,
  138. },
  139. .probe = mmio_74xx_gpio_probe,
  140. .remove = mmio_74xx_gpio_remove,
  141. };
  142. module_platform_driver(mmio_74xx_gpio_driver);
  143. MODULE_LICENSE("GPL");
  144. MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
  145. MODULE_DESCRIPTION("74xx MMIO GPIO driver");