gpio-loongson.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Loongson-2F/3A/3B GPIO Support
  4. *
  5. * Copyright (c) 2008 Richard Liu, STMicroelectronics <richard.liu@st.com>
  6. * Copyright (c) 2008-2010 Arnaud Patard <apatard@mandriva.com>
  7. * Copyright (c) 2013 Hongbing Hu <huhb@lemote.com>
  8. * Copyright (c) 2014 Huacai Chen <chenhc@lemote.com>
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/err.h>
  15. #include <linux/gpio/driver.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/bitops.h>
  18. #include <asm/types.h>
  19. #include <loongson.h>
  20. #define STLS2F_N_GPIO 4
  21. #define STLS3A_N_GPIO 16
  22. #ifdef CONFIG_CPU_LOONGSON64
  23. #define LOONGSON_N_GPIO STLS3A_N_GPIO
  24. #else
  25. #define LOONGSON_N_GPIO STLS2F_N_GPIO
  26. #endif
  27. /*
  28. * Offset into the register where we read lines, we write them from offset 0.
  29. * This offset is the only thing that stand between us and using
  30. * GPIO_GENERIC.
  31. */
  32. #define LOONGSON_GPIO_IN_OFFSET 16
  33. static DEFINE_SPINLOCK(gpio_lock);
  34. static int loongson_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
  35. {
  36. u32 val;
  37. spin_lock(&gpio_lock);
  38. val = LOONGSON_GPIODATA;
  39. spin_unlock(&gpio_lock);
  40. return !!(val & BIT(gpio + LOONGSON_GPIO_IN_OFFSET));
  41. }
  42. static void loongson_gpio_set_value(struct gpio_chip *chip,
  43. unsigned gpio, int value)
  44. {
  45. u32 val;
  46. spin_lock(&gpio_lock);
  47. val = LOONGSON_GPIODATA;
  48. if (value)
  49. val |= BIT(gpio);
  50. else
  51. val &= ~BIT(gpio);
  52. LOONGSON_GPIODATA = val;
  53. spin_unlock(&gpio_lock);
  54. }
  55. static int loongson_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
  56. {
  57. u32 temp;
  58. spin_lock(&gpio_lock);
  59. temp = LOONGSON_GPIOIE;
  60. temp |= BIT(gpio);
  61. LOONGSON_GPIOIE = temp;
  62. spin_unlock(&gpio_lock);
  63. return 0;
  64. }
  65. static int loongson_gpio_direction_output(struct gpio_chip *chip,
  66. unsigned gpio, int level)
  67. {
  68. u32 temp;
  69. loongson_gpio_set_value(chip, gpio, level);
  70. spin_lock(&gpio_lock);
  71. temp = LOONGSON_GPIOIE;
  72. temp &= ~BIT(gpio);
  73. LOONGSON_GPIOIE = temp;
  74. spin_unlock(&gpio_lock);
  75. return 0;
  76. }
  77. static int loongson_gpio_probe(struct platform_device *pdev)
  78. {
  79. struct gpio_chip *gc;
  80. struct device *dev = &pdev->dev;
  81. gc = devm_kzalloc(dev, sizeof(*gc), GFP_KERNEL);
  82. if (!gc)
  83. return -ENOMEM;
  84. gc->label = "loongson-gpio-chip";
  85. gc->base = 0;
  86. gc->ngpio = LOONGSON_N_GPIO;
  87. gc->get = loongson_gpio_get_value;
  88. gc->set = loongson_gpio_set_value;
  89. gc->direction_input = loongson_gpio_direction_input;
  90. gc->direction_output = loongson_gpio_direction_output;
  91. return gpiochip_add_data(gc, NULL);
  92. }
  93. static struct platform_driver loongson_gpio_driver = {
  94. .driver = {
  95. .name = "loongson-gpio",
  96. },
  97. .probe = loongson_gpio_probe,
  98. };
  99. static int __init loongson_gpio_setup(void)
  100. {
  101. struct platform_device *pdev;
  102. int ret;
  103. ret = platform_driver_register(&loongson_gpio_driver);
  104. if (ret) {
  105. pr_err("error registering loongson GPIO driver\n");
  106. return ret;
  107. }
  108. pdev = platform_device_register_simple("loongson-gpio", -1, NULL, 0);
  109. return PTR_ERR_OR_ZERO(pdev);
  110. }
  111. postcore_initcall(loongson_gpio_setup);