gpio-loongson1.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #include <linux/bitops.h>
  14. /* Loongson 1 GPIO Register Definitions */
  15. #define GPIO_CFG 0x0
  16. #define GPIO_DIR 0x10
  17. #define GPIO_DATA 0x20
  18. #define GPIO_OUTPUT 0x30
  19. static void __iomem *gpio_reg_base;
  20. static int ls1x_gpio_request(struct gpio_chip *gc, unsigned int offset)
  21. {
  22. unsigned long flags;
  23. spin_lock_irqsave(&gc->bgpio_lock, flags);
  24. __raw_writel(__raw_readl(gpio_reg_base + GPIO_CFG) | BIT(offset),
  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 flags;
  32. spin_lock_irqsave(&gc->bgpio_lock, flags);
  33. __raw_writel(__raw_readl(gpio_reg_base + GPIO_CFG) & ~BIT(offset),
  34. gpio_reg_base + GPIO_CFG);
  35. spin_unlock_irqrestore(&gc->bgpio_lock, flags);
  36. }
  37. static int ls1x_gpio_probe(struct platform_device *pdev)
  38. {
  39. struct device *dev = &pdev->dev;
  40. struct gpio_chip *gc;
  41. struct resource *res;
  42. int ret;
  43. gc = devm_kzalloc(dev, sizeof(*gc), GFP_KERNEL);
  44. if (!gc)
  45. return -ENOMEM;
  46. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  47. gpio_reg_base = devm_ioremap_resource(dev, res);
  48. if (IS_ERR(gpio_reg_base))
  49. return PTR_ERR(gpio_reg_base);
  50. ret = bgpio_init(gc, dev, 4, gpio_reg_base + GPIO_DATA,
  51. gpio_reg_base + GPIO_OUTPUT, NULL,
  52. NULL, gpio_reg_base + GPIO_DIR, 0);
  53. if (ret)
  54. goto err;
  55. gc->owner = THIS_MODULE;
  56. gc->request = ls1x_gpio_request;
  57. gc->free = ls1x_gpio_free;
  58. gc->base = pdev->id * 32;
  59. ret = devm_gpiochip_add_data(dev, gc, NULL);
  60. if (ret)
  61. goto err;
  62. platform_set_drvdata(pdev, gc);
  63. dev_info(dev, "Loongson1 GPIO driver registered\n");
  64. return 0;
  65. err:
  66. dev_err(dev, "failed to register GPIO device\n");
  67. return ret;
  68. }
  69. static struct platform_driver ls1x_gpio_driver = {
  70. .probe = ls1x_gpio_probe,
  71. .driver = {
  72. .name = "ls1x-gpio",
  73. },
  74. };
  75. module_platform_driver(ls1x_gpio_driver);
  76. MODULE_AUTHOR("Kelvin Cheung <keguang.zhang@gmail.com>");
  77. MODULE_DESCRIPTION("Loongson1 GPIO driver");
  78. MODULE_LICENSE("GPL");