gpio-loongson1.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * GPIO Driver for Loongson 1 SoC
  3. *
  4. * Copyright (C) 2015-2016 Zhang, Keguang <keguang.zhang@gmail.com>
  5. *
  6. * This file is licensed under the terms of the GNU General Public
  7. * License version 2. This program is licensed "as is" without any
  8. * warranty of any kind, whether express or implied.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/gpio/driver.h>
  12. #include <linux/platform_device.h>
  13. /* Loongson 1 GPIO Register Definitions */
  14. #define GPIO_CFG 0x0
  15. #define GPIO_DIR 0x10
  16. #define GPIO_DATA 0x20
  17. #define GPIO_OUTPUT 0x30
  18. static void __iomem *gpio_reg_base;
  19. static int ls1x_gpio_request(struct gpio_chip *gc, unsigned int offset)
  20. {
  21. unsigned long pinmask = gc->pin2mask(gc, offset);
  22. unsigned long flags;
  23. spin_lock_irqsave(&gc->bgpio_lock, flags);
  24. __raw_writel(__raw_readl(gpio_reg_base + GPIO_CFG) | pinmask,
  25. gpio_reg_base + GPIO_CFG);
  26. spin_unlock_irqrestore(&gc->bgpio_lock, flags);
  27. return 0;
  28. }
  29. static void ls1x_gpio_free(struct gpio_chip *gc, unsigned int offset)
  30. {
  31. unsigned long pinmask = gc->pin2mask(gc, offset);
  32. unsigned long flags;
  33. spin_lock_irqsave(&gc->bgpio_lock, flags);
  34. __raw_writel(__raw_readl(gpio_reg_base + GPIO_CFG) & ~pinmask,
  35. gpio_reg_base + GPIO_CFG);
  36. spin_unlock_irqrestore(&gc->bgpio_lock, flags);
  37. }
  38. static int ls1x_gpio_probe(struct platform_device *pdev)
  39. {
  40. struct device *dev = &pdev->dev;
  41. struct gpio_chip *gc;
  42. struct resource *res;
  43. int ret;
  44. gc = devm_kzalloc(dev, sizeof(*gc), GFP_KERNEL);
  45. if (!gc)
  46. return -ENOMEM;
  47. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  48. gpio_reg_base = devm_ioremap_resource(dev, res);
  49. if (IS_ERR(gpio_reg_base))
  50. return PTR_ERR(gpio_reg_base);
  51. ret = bgpio_init(gc, dev, 4, gpio_reg_base + GPIO_DATA,
  52. gpio_reg_base + GPIO_OUTPUT, NULL,
  53. NULL, gpio_reg_base + GPIO_DIR, 0);
  54. if (ret)
  55. goto err;
  56. gc->owner = THIS_MODULE;
  57. gc->request = ls1x_gpio_request;
  58. gc->free = ls1x_gpio_free;
  59. gc->base = pdev->id * 32;
  60. ret = devm_gpiochip_add_data(dev, gc, NULL);
  61. if (ret)
  62. goto err;
  63. platform_set_drvdata(pdev, gc);
  64. dev_info(dev, "Loongson1 GPIO driver registered\n");
  65. return 0;
  66. err:
  67. dev_err(dev, "failed to register GPIO device\n");
  68. return ret;
  69. }
  70. static struct platform_driver ls1x_gpio_driver = {
  71. .probe = ls1x_gpio_probe,
  72. .driver = {
  73. .name = "ls1x-gpio",
  74. },
  75. };
  76. module_platform_driver(ls1x_gpio_driver);
  77. MODULE_AUTHOR("Kelvin Cheung <keguang.zhang@gmail.com>");
  78. MODULE_DESCRIPTION("Loongson1 GPIO driver");
  79. MODULE_LICENSE("GPL");