gpio-moxart.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * MOXA ART SoCs GPIO driver.
  3. *
  4. * Copyright (C) 2013 Jonas Jensen
  5. *
  6. * Jonas Jensen <jonas.jensen@gmail.com>
  7. *
  8. * This file is licensed under the terms of the GNU General Public
  9. * License version 2. This program is licensed "as is" without any
  10. * warranty of any kind, whether express or implied.
  11. */
  12. #include <linux/err.h>
  13. #include <linux/init.h>
  14. #include <linux/irq.h>
  15. #include <linux/io.h>
  16. #include <linux/gpio.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/module.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_gpio.h>
  21. #include <linux/pinctrl/consumer.h>
  22. #include <linux/delay.h>
  23. #include <linux/timer.h>
  24. #include <linux/bitops.h>
  25. #define GPIO_DATA_OUT 0x00
  26. #define GPIO_DATA_IN 0x04
  27. #define GPIO_PIN_DIRECTION 0x08
  28. struct moxart_gpio_chip {
  29. struct gpio_chip gpio;
  30. void __iomem *base;
  31. };
  32. static inline struct moxart_gpio_chip *to_moxart_gpio(struct gpio_chip *chip)
  33. {
  34. return container_of(chip, struct moxart_gpio_chip, gpio);
  35. }
  36. static int moxart_gpio_request(struct gpio_chip *chip, unsigned offset)
  37. {
  38. return pinctrl_request_gpio(offset);
  39. }
  40. static void moxart_gpio_free(struct gpio_chip *chip, unsigned offset)
  41. {
  42. pinctrl_free_gpio(offset);
  43. }
  44. static int moxart_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  45. {
  46. struct moxart_gpio_chip *gc = to_moxart_gpio(chip);
  47. void __iomem *ioaddr = gc->base + GPIO_PIN_DIRECTION;
  48. writel(readl(ioaddr) & ~BIT(offset), ioaddr);
  49. return 0;
  50. }
  51. static int moxart_gpio_direction_output(struct gpio_chip *chip,
  52. unsigned offset, int value)
  53. {
  54. struct moxart_gpio_chip *gc = to_moxart_gpio(chip);
  55. void __iomem *ioaddr = gc->base + GPIO_PIN_DIRECTION;
  56. writel(readl(ioaddr) | BIT(offset), ioaddr);
  57. return 0;
  58. }
  59. static void moxart_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  60. {
  61. struct moxart_gpio_chip *gc = to_moxart_gpio(chip);
  62. void __iomem *ioaddr = gc->base + GPIO_DATA_OUT;
  63. u32 reg = readl(ioaddr);
  64. if (value)
  65. reg = reg | BIT(offset);
  66. else
  67. reg = reg & ~BIT(offset);
  68. writel(reg, ioaddr);
  69. }
  70. static int moxart_gpio_get(struct gpio_chip *chip, unsigned offset)
  71. {
  72. struct moxart_gpio_chip *gc = to_moxart_gpio(chip);
  73. u32 ret = readl(gc->base + GPIO_PIN_DIRECTION);
  74. if (ret & BIT(offset))
  75. return !!(readl(gc->base + GPIO_DATA_OUT) & BIT(offset));
  76. else
  77. return !!(readl(gc->base + GPIO_DATA_IN) & BIT(offset));
  78. }
  79. static struct gpio_chip moxart_template_chip = {
  80. .label = "moxart-gpio",
  81. .request = moxart_gpio_request,
  82. .free = moxart_gpio_free,
  83. .direction_input = moxart_gpio_direction_input,
  84. .direction_output = moxart_gpio_direction_output,
  85. .set = moxart_gpio_set,
  86. .get = moxart_gpio_get,
  87. .ngpio = 32,
  88. .owner = THIS_MODULE,
  89. };
  90. static int moxart_gpio_probe(struct platform_device *pdev)
  91. {
  92. struct device *dev = &pdev->dev;
  93. struct resource *res;
  94. struct moxart_gpio_chip *mgc;
  95. int ret;
  96. mgc = devm_kzalloc(dev, sizeof(*mgc), GFP_KERNEL);
  97. if (!mgc) {
  98. dev_err(dev, "can't allocate GPIO chip container\n");
  99. return -ENOMEM;
  100. }
  101. mgc->gpio = moxart_template_chip;
  102. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  103. mgc->base = devm_ioremap_resource(dev, res);
  104. if (IS_ERR(mgc->base))
  105. return PTR_ERR(mgc->base);
  106. mgc->gpio.dev = dev;
  107. ret = gpiochip_add(&mgc->gpio);
  108. if (ret) {
  109. dev_err(dev, "%s: gpiochip_add failed\n",
  110. dev->of_node->full_name);
  111. return ret;
  112. }
  113. return 0;
  114. }
  115. static const struct of_device_id moxart_gpio_match[] = {
  116. { .compatible = "moxa,moxart-gpio" },
  117. { }
  118. };
  119. static struct platform_driver moxart_gpio_driver = {
  120. .driver = {
  121. .name = "moxart-gpio",
  122. .owner = THIS_MODULE,
  123. .of_match_table = moxart_gpio_match,
  124. },
  125. .probe = moxart_gpio_probe,
  126. };
  127. module_platform_driver(moxart_gpio_driver);
  128. MODULE_DESCRIPTION("MOXART GPIO chip driver");
  129. MODULE_LICENSE("GPL");
  130. MODULE_AUTHOR("Jonas Jensen <jonas.jensen@gmail.com>");