gpio-moxart.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 void moxart_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  45. {
  46. struct moxart_gpio_chip *gc = to_moxart_gpio(chip);
  47. void __iomem *ioaddr = gc->base + GPIO_DATA_OUT;
  48. u32 reg = readl(ioaddr);
  49. if (value)
  50. reg = reg | BIT(offset);
  51. else
  52. reg = reg & ~BIT(offset);
  53. writel(reg, ioaddr);
  54. }
  55. static int moxart_gpio_get(struct gpio_chip *chip, unsigned offset)
  56. {
  57. struct moxart_gpio_chip *gc = to_moxart_gpio(chip);
  58. u32 ret = readl(gc->base + GPIO_PIN_DIRECTION);
  59. if (ret & BIT(offset))
  60. return !!(readl(gc->base + GPIO_DATA_OUT) & BIT(offset));
  61. else
  62. return !!(readl(gc->base + GPIO_DATA_IN) & BIT(offset));
  63. }
  64. static int moxart_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  65. {
  66. struct moxart_gpio_chip *gc = to_moxart_gpio(chip);
  67. void __iomem *ioaddr = gc->base + GPIO_PIN_DIRECTION;
  68. writel(readl(ioaddr) & ~BIT(offset), ioaddr);
  69. return 0;
  70. }
  71. static int moxart_gpio_direction_output(struct gpio_chip *chip,
  72. unsigned offset, int value)
  73. {
  74. struct moxart_gpio_chip *gc = to_moxart_gpio(chip);
  75. void __iomem *ioaddr = gc->base + GPIO_PIN_DIRECTION;
  76. moxart_gpio_set(chip, offset, value);
  77. writel(readl(ioaddr) | BIT(offset), ioaddr);
  78. return 0;
  79. }
  80. static struct gpio_chip moxart_template_chip = {
  81. .label = "moxart-gpio",
  82. .request = moxart_gpio_request,
  83. .free = moxart_gpio_free,
  84. .direction_input = moxart_gpio_direction_input,
  85. .direction_output = moxart_gpio_direction_output,
  86. .set = moxart_gpio_set,
  87. .get = moxart_gpio_get,
  88. .ngpio = 32,
  89. .owner = THIS_MODULE,
  90. };
  91. static int moxart_gpio_probe(struct platform_device *pdev)
  92. {
  93. struct device *dev = &pdev->dev;
  94. struct resource *res;
  95. struct moxart_gpio_chip *mgc;
  96. int ret;
  97. mgc = devm_kzalloc(dev, sizeof(*mgc), GFP_KERNEL);
  98. if (!mgc)
  99. return -ENOMEM;
  100. mgc->gpio = moxart_template_chip;
  101. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  102. mgc->base = devm_ioremap_resource(dev, res);
  103. if (IS_ERR(mgc->base))
  104. return PTR_ERR(mgc->base);
  105. mgc->gpio.dev = dev;
  106. ret = gpiochip_add(&mgc->gpio);
  107. if (ret) {
  108. dev_err(dev, "%s: gpiochip_add failed\n",
  109. dev->of_node->full_name);
  110. return ret;
  111. }
  112. return 0;
  113. }
  114. static const struct of_device_id moxart_gpio_match[] = {
  115. { .compatible = "moxa,moxart-gpio" },
  116. { }
  117. };
  118. static struct platform_driver moxart_gpio_driver = {
  119. .driver = {
  120. .name = "moxart-gpio",
  121. .owner = THIS_MODULE,
  122. .of_match_table = moxart_gpio_match,
  123. },
  124. .probe = moxart_gpio_probe,
  125. };
  126. module_platform_driver(moxart_gpio_driver);
  127. MODULE_DESCRIPTION("MOXART GPIO chip driver");
  128. MODULE_LICENSE("GPL");
  129. MODULE_AUTHOR("Jonas Jensen <jonas.jensen@gmail.com>");