gpio-lpc18xx.c 4.0 KB

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