gpio-74xx-mmio.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. struct mmio_74xx_gpio_priv *priv;
  104. struct resource *res;
  105. void __iomem *dat;
  106. int err;
  107. of_id = of_match_device(mmio_74xx_gpio_ids, &pdev->dev);
  108. if (!of_id)
  109. return -ENODEV;
  110. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  111. if (!priv)
  112. return -ENOMEM;
  113. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  114. dat = devm_ioremap_resource(&pdev->dev, res);
  115. if (IS_ERR(dat))
  116. return PTR_ERR(dat);
  117. priv->flags = (uintptr_t) of_id->data;
  118. err = bgpio_init(&priv->bgc, &pdev->dev,
  119. DIV_ROUND_UP(MMIO_74XX_BIT_CNT(priv->flags), 8),
  120. dat, NULL, NULL, NULL, NULL, 0);
  121. if (err)
  122. return err;
  123. priv->bgc.gc.direction_input = mmio_74xx_dir_in;
  124. priv->bgc.gc.direction_output = mmio_74xx_dir_out;
  125. priv->bgc.gc.get_direction = mmio_74xx_get_direction;
  126. priv->bgc.gc.ngpio = MMIO_74XX_BIT_CNT(priv->flags);
  127. priv->bgc.gc.owner = THIS_MODULE;
  128. platform_set_drvdata(pdev, priv);
  129. return gpiochip_add(&priv->bgc.gc);
  130. }
  131. static int mmio_74xx_gpio_remove(struct platform_device *pdev)
  132. {
  133. struct mmio_74xx_gpio_priv *priv = platform_get_drvdata(pdev);
  134. return bgpio_remove(&priv->bgc);
  135. }
  136. static struct platform_driver mmio_74xx_gpio_driver = {
  137. .driver = {
  138. .name = "74xx-mmio-gpio",
  139. .of_match_table = mmio_74xx_gpio_ids,
  140. },
  141. .probe = mmio_74xx_gpio_probe,
  142. .remove = mmio_74xx_gpio_remove,
  143. };
  144. module_platform_driver(mmio_74xx_gpio_driver);
  145. MODULE_LICENSE("GPL");
  146. MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
  147. MODULE_DESCRIPTION("74xx MMIO GPIO driver");