gpio.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * STLS2F 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. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/err.h>
  17. #include <asm/types.h>
  18. #include <loongson.h>
  19. #include <linux/gpio.h>
  20. #define STLS2F_N_GPIO 4
  21. #define STLS2F_GPIO_IN_OFFSET 16
  22. static DEFINE_SPINLOCK(gpio_lock);
  23. int gpio_get_value(unsigned gpio)
  24. {
  25. u32 val;
  26. u32 mask;
  27. if (gpio >= STLS2F_N_GPIO)
  28. return __gpio_get_value(gpio);
  29. mask = 1 << (gpio + STLS2F_GPIO_IN_OFFSET);
  30. spin_lock(&gpio_lock);
  31. val = LOONGSON_GPIODATA;
  32. spin_unlock(&gpio_lock);
  33. return ((val & mask) != 0);
  34. }
  35. EXPORT_SYMBOL(gpio_get_value);
  36. void gpio_set_value(unsigned gpio, int state)
  37. {
  38. u32 val;
  39. u32 mask;
  40. if (gpio >= STLS2F_N_GPIO) {
  41. __gpio_set_value(gpio, state);
  42. return ;
  43. }
  44. mask = 1 << gpio;
  45. spin_lock(&gpio_lock);
  46. val = LOONGSON_GPIODATA;
  47. if (state)
  48. val |= mask;
  49. else
  50. val &= (~mask);
  51. LOONGSON_GPIODATA = val;
  52. spin_unlock(&gpio_lock);
  53. }
  54. EXPORT_SYMBOL(gpio_set_value);
  55. int gpio_cansleep(unsigned gpio)
  56. {
  57. if (gpio < STLS2F_N_GPIO)
  58. return 0;
  59. else
  60. return __gpio_cansleep(gpio);
  61. }
  62. EXPORT_SYMBOL(gpio_cansleep);
  63. static int ls2f_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
  64. {
  65. u32 temp;
  66. u32 mask;
  67. if (gpio >= STLS2F_N_GPIO)
  68. return -EINVAL;
  69. spin_lock(&gpio_lock);
  70. mask = 1 << gpio;
  71. temp = LOONGSON_GPIOIE;
  72. temp |= mask;
  73. LOONGSON_GPIOIE = temp;
  74. spin_unlock(&gpio_lock);
  75. return 0;
  76. }
  77. static int ls2f_gpio_direction_output(struct gpio_chip *chip,
  78. unsigned gpio, int level)
  79. {
  80. u32 temp;
  81. u32 mask;
  82. if (gpio >= STLS2F_N_GPIO)
  83. return -EINVAL;
  84. gpio_set_value(gpio, level);
  85. spin_lock(&gpio_lock);
  86. mask = 1 << gpio;
  87. temp = LOONGSON_GPIOIE;
  88. temp &= (~mask);
  89. LOONGSON_GPIOIE = temp;
  90. spin_unlock(&gpio_lock);
  91. return 0;
  92. }
  93. static int ls2f_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
  94. {
  95. return gpio_get_value(gpio);
  96. }
  97. static void ls2f_gpio_set_value(struct gpio_chip *chip,
  98. unsigned gpio, int value)
  99. {
  100. gpio_set_value(gpio, value);
  101. }
  102. static struct gpio_chip ls2f_chip = {
  103. .label = "ls2f",
  104. .direction_input = ls2f_gpio_direction_input,
  105. .get = ls2f_gpio_get_value,
  106. .direction_output = ls2f_gpio_direction_output,
  107. .set = ls2f_gpio_set_value,
  108. .base = 0,
  109. .ngpio = STLS2F_N_GPIO,
  110. };
  111. static int __init ls2f_gpio_setup(void)
  112. {
  113. return gpiochip_add(&ls2f_chip);
  114. }
  115. arch_initcall(ls2f_gpio_setup);