gpio-lpc18xx.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 int lpc18xx_gpio_request(struct gpio_chip *chip, unsigned offset)
  34. {
  35. return pinctrl_request_gpio(offset);
  36. }
  37. static void lpc18xx_gpio_free(struct gpio_chip *chip, unsigned offset)
  38. {
  39. pinctrl_free_gpio(offset);
  40. }
  41. static void lpc18xx_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  42. {
  43. struct lpc18xx_gpio_chip *gc = to_lpc18xx_gpio(chip);
  44. writeb(value ? 1 : 0, gc->base + offset);
  45. }
  46. static int lpc18xx_gpio_get(struct gpio_chip *chip, unsigned offset)
  47. {
  48. struct lpc18xx_gpio_chip *gc = to_lpc18xx_gpio(chip);
  49. return !!readb(gc->base + offset);
  50. }
  51. static int lpc18xx_gpio_direction(struct gpio_chip *chip, unsigned offset,
  52. bool out)
  53. {
  54. struct lpc18xx_gpio_chip *gc = to_lpc18xx_gpio(chip);
  55. unsigned long flags;
  56. u32 port, pin, dir;
  57. port = offset / LPC18XX_PINS_PER_PORT;
  58. pin = offset % LPC18XX_PINS_PER_PORT;
  59. spin_lock_irqsave(&gc->lock, flags);
  60. dir = readl(gc->base + LPC18XX_REG_DIR(port));
  61. if (out)
  62. dir |= BIT(pin);
  63. else
  64. dir &= ~BIT(pin);
  65. writel(dir, gc->base + LPC18XX_REG_DIR(port));
  66. spin_unlock_irqrestore(&gc->lock, flags);
  67. return 0;
  68. }
  69. static int lpc18xx_gpio_direction_input(struct gpio_chip *chip,
  70. unsigned offset)
  71. {
  72. return lpc18xx_gpio_direction(chip, offset, false);
  73. }
  74. static int lpc18xx_gpio_direction_output(struct gpio_chip *chip,
  75. unsigned offset, int value)
  76. {
  77. lpc18xx_gpio_set(chip, offset, value);
  78. return lpc18xx_gpio_direction(chip, offset, true);
  79. }
  80. static struct gpio_chip lpc18xx_chip = {
  81. .label = "lpc18xx/43xx-gpio",
  82. .request = lpc18xx_gpio_request,
  83. .free = lpc18xx_gpio_free,
  84. .direction_input = lpc18xx_gpio_direction_input,
  85. .direction_output = lpc18xx_gpio_direction_output,
  86. .set = lpc18xx_gpio_set,
  87. .get = lpc18xx_gpio_get,
  88. .ngpio = LPC18XX_MAX_PORTS * LPC18XX_PINS_PER_PORT,
  89. .owner = THIS_MODULE,
  90. };
  91. static int lpc18xx_gpio_probe(struct platform_device *pdev)
  92. {
  93. struct lpc18xx_gpio_chip *gc;
  94. struct resource *res;
  95. int ret;
  96. gc = devm_kzalloc(&pdev->dev, sizeof(*gc), GFP_KERNEL);
  97. if (!gc)
  98. return -ENOMEM;
  99. gc->gpio = lpc18xx_chip;
  100. platform_set_drvdata(pdev, gc);
  101. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  102. gc->base = devm_ioremap_resource(&pdev->dev, res);
  103. if (IS_ERR(gc->base))
  104. return PTR_ERR(gc->base);
  105. gc->clk = devm_clk_get(&pdev->dev, NULL);
  106. if (IS_ERR(gc->clk)) {
  107. dev_err(&pdev->dev, "input clock not found\n");
  108. return PTR_ERR(gc->clk);
  109. }
  110. ret = clk_prepare_enable(gc->clk);
  111. if (ret) {
  112. dev_err(&pdev->dev, "unable to enable clock\n");
  113. return ret;
  114. }
  115. spin_lock_init(&gc->lock);
  116. gc->gpio.dev = &pdev->dev;
  117. ret = gpiochip_add(&gc->gpio);
  118. if (ret) {
  119. dev_err(&pdev->dev, "failed to add gpio chip\n");
  120. clk_disable_unprepare(gc->clk);
  121. return ret;
  122. }
  123. return 0;
  124. }
  125. static int lpc18xx_gpio_remove(struct platform_device *pdev)
  126. {
  127. struct lpc18xx_gpio_chip *gc = platform_get_drvdata(pdev);
  128. gpiochip_remove(&gc->gpio);
  129. clk_disable_unprepare(gc->clk);
  130. return 0;
  131. }
  132. static const struct of_device_id lpc18xx_gpio_match[] = {
  133. { .compatible = "nxp,lpc1850-gpio" },
  134. { }
  135. };
  136. MODULE_DEVICE_TABLE(of, lpc18xx_gpio_match);
  137. static struct platform_driver lpc18xx_gpio_driver = {
  138. .probe = lpc18xx_gpio_probe,
  139. .remove = lpc18xx_gpio_remove,
  140. .driver = {
  141. .name = "lpc18xx-gpio",
  142. .of_match_table = lpc18xx_gpio_match,
  143. },
  144. };
  145. module_platform_driver(lpc18xx_gpio_driver);
  146. MODULE_AUTHOR("Joachim Eastwood <manabian@gmail.com>");
  147. MODULE_DESCRIPTION("GPIO driver for LPC18xx/43xx");
  148. MODULE_LICENSE("GPL v2");