gpio-loongson.c 3.2 KB

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