gpio-lpc18xx.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * GPIO driver for NXP LPC18xx/43xx.
  3. *
  4. * Copyright (C) 2015 Joachim Eastwood <manabian@gmail.com>
  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 version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/gpio/driver.h>
  13. #include <linux/io.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/of_gpio.h>
  17. #include <linux/pinctrl/consumer.h>
  18. #include <linux/platform_device.h>
  19. /* LPC18xx GPIO register offsets */
  20. #define LPC18XX_REG_DIR(n) (0x2000 + n * sizeof(u32))
  21. #define LPC18XX_MAX_PORTS 8
  22. #define LPC18XX_PINS_PER_PORT 32
  23. struct lpc18xx_gpio_chip {
  24. struct gpio_chip gpio;
  25. void __iomem *base;
  26. struct clk *clk;
  27. spinlock_t lock;
  28. };
  29. static inline struct lpc18xx_gpio_chip *to_lpc18xx_gpio(struct gpio_chip *chip)
  30. {
  31. return container_of(chip, struct lpc18xx_gpio_chip, gpio);
  32. }
  33. static void lpc18xx_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  34. {
  35. struct lpc18xx_gpio_chip *gc = to_lpc18xx_gpio(chip);
  36. writeb(value ? 1 : 0, gc->base + offset);
  37. }
  38. static int lpc18xx_gpio_get(struct gpio_chip *chip, unsigned offset)
  39. {
  40. struct lpc18xx_gpio_chip *gc = to_lpc18xx_gpio(chip);
  41. return !!readb(gc->base + offset);
  42. }
  43. static int lpc18xx_gpio_direction(struct gpio_chip *chip, unsigned offset,
  44. bool out)
  45. {
  46. struct lpc18xx_gpio_chip *gc = to_lpc18xx_gpio(chip);
  47. unsigned long flags;
  48. u32 port, pin, dir;
  49. port = offset / LPC18XX_PINS_PER_PORT;
  50. pin = offset % LPC18XX_PINS_PER_PORT;
  51. spin_lock_irqsave(&gc->lock, flags);
  52. dir = readl(gc->base + LPC18XX_REG_DIR(port));
  53. if (out)
  54. dir |= BIT(pin);
  55. else
  56. dir &= ~BIT(pin);
  57. writel(dir, gc->base + LPC18XX_REG_DIR(port));
  58. spin_unlock_irqrestore(&gc->lock, flags);
  59. return 0;
  60. }
  61. static int lpc18xx_gpio_direction_input(struct gpio_chip *chip,
  62. unsigned offset)
  63. {
  64. return lpc18xx_gpio_direction(chip, offset, false);
  65. }
  66. static int lpc18xx_gpio_direction_output(struct gpio_chip *chip,
  67. unsigned offset, int value)
  68. {
  69. lpc18xx_gpio_set(chip, offset, value);
  70. return lpc18xx_gpio_direction(chip, offset, true);
  71. }
  72. static struct gpio_chip lpc18xx_chip = {
  73. .label = "lpc18xx/43xx-gpio",
  74. .request = gpiochip_generic_request,
  75. .free = gpiochip_generic_free,
  76. .direction_input = lpc18xx_gpio_direction_input,
  77. .direction_output = lpc18xx_gpio_direction_output,
  78. .set = lpc18xx_gpio_set,
  79. .get = lpc18xx_gpio_get,
  80. .ngpio = LPC18XX_MAX_PORTS * LPC18XX_PINS_PER_PORT,
  81. .owner = THIS_MODULE,
  82. };
  83. static int lpc18xx_gpio_probe(struct platform_device *pdev)
  84. {
  85. struct lpc18xx_gpio_chip *gc;
  86. struct resource *res;
  87. int ret;
  88. gc = devm_kzalloc(&pdev->dev, sizeof(*gc), GFP_KERNEL);
  89. if (!gc)
  90. return -ENOMEM;
  91. gc->gpio = lpc18xx_chip;
  92. platform_set_drvdata(pdev, gc);
  93. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  94. gc->base = devm_ioremap_resource(&pdev->dev, res);
  95. if (IS_ERR(gc->base))
  96. return PTR_ERR(gc->base);
  97. gc->clk = devm_clk_get(&pdev->dev, NULL);
  98. if (IS_ERR(gc->clk)) {
  99. dev_err(&pdev->dev, "input clock not found\n");
  100. return PTR_ERR(gc->clk);
  101. }
  102. ret = clk_prepare_enable(gc->clk);
  103. if (ret) {
  104. dev_err(&pdev->dev, "unable to enable clock\n");
  105. return ret;
  106. }
  107. spin_lock_init(&gc->lock);
  108. gc->gpio.dev = &pdev->dev;
  109. ret = gpiochip_add(&gc->gpio);
  110. if (ret) {
  111. dev_err(&pdev->dev, "failed to add gpio chip\n");
  112. clk_disable_unprepare(gc->clk);
  113. return ret;
  114. }
  115. return 0;
  116. }
  117. static int lpc18xx_gpio_remove(struct platform_device *pdev)
  118. {
  119. struct lpc18xx_gpio_chip *gc = platform_get_drvdata(pdev);
  120. gpiochip_remove(&gc->gpio);
  121. clk_disable_unprepare(gc->clk);
  122. return 0;
  123. }
  124. static const struct of_device_id lpc18xx_gpio_match[] = {
  125. { .compatible = "nxp,lpc1850-gpio" },
  126. { }
  127. };
  128. MODULE_DEVICE_TABLE(of, lpc18xx_gpio_match);
  129. static struct platform_driver lpc18xx_gpio_driver = {
  130. .probe = lpc18xx_gpio_probe,
  131. .remove = lpc18xx_gpio_remove,
  132. .driver = {
  133. .name = "lpc18xx-gpio",
  134. .of_match_table = lpc18xx_gpio_match,
  135. },
  136. };
  137. module_platform_driver(lpc18xx_gpio_driver);
  138. MODULE_AUTHOR("Joachim Eastwood <manabian@gmail.com>");
  139. MODULE_DESCRIPTION("GPIO driver for LPC18xx/43xx");
  140. MODULE_LICENSE("GPL v2");