gpio-loongson1.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. int ret;
  42. gc = devm_kzalloc(dev, sizeof(*gc), GFP_KERNEL);
  43. if (!gc)
  44. return -ENOMEM;
  45. gpio_reg_base = devm_platform_ioremap_resource(pdev, 0);
  46. if (IS_ERR(gpio_reg_base))
  47. return PTR_ERR(gpio_reg_base);
  48. ret = bgpio_init(gc, dev, 4, gpio_reg_base + GPIO_DATA,
  49. gpio_reg_base + GPIO_OUTPUT, NULL,
  50. NULL, gpio_reg_base + GPIO_DIR, 0);
  51. if (ret)
  52. goto err;
  53. gc->owner = THIS_MODULE;
  54. gc->request = ls1x_gpio_request;
  55. gc->free = ls1x_gpio_free;
  56. gc->base = pdev->id * 32;
  57. ret = devm_gpiochip_add_data(dev, gc, NULL);
  58. if (ret)
  59. goto err;
  60. platform_set_drvdata(pdev, gc);
  61. dev_info(dev, "Loongson1 GPIO driver registered\n");
  62. return 0;
  63. err:
  64. dev_err(dev, "failed to register GPIO device\n");
  65. return ret;
  66. }
  67. static struct platform_driver ls1x_gpio_driver = {
  68. .probe = ls1x_gpio_probe,
  69. .driver = {
  70. .name = "ls1x-gpio",
  71. },
  72. };
  73. module_platform_driver(ls1x_gpio_driver);
  74. MODULE_AUTHOR("Kelvin Cheung <keguang.zhang@gmail.com>");
  75. MODULE_DESCRIPTION("Loongson1 GPIO driver");
  76. MODULE_LICENSE("GPL");