gpio.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * Copyright (C) 2007 Felix Fietkau <nbd@openwrt.org>
  3. * Copyright (C) 2007 Eugene Konev <ejka@openwrt.org>
  4. * Copyright (C) 2009-2010 Florian Fainelli <florian@openwrt.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <linux/module.h>
  21. #include <linux/gpio.h>
  22. #include <asm/mach-ar7/ar7.h>
  23. #define AR7_GPIO_MAX 32
  24. #define TITAN_GPIO_MAX 51
  25. struct ar7_gpio_chip {
  26. void __iomem *regs;
  27. struct gpio_chip chip;
  28. };
  29. static int ar7_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
  30. {
  31. struct ar7_gpio_chip *gpch = gpiochip_get_data(chip);
  32. void __iomem *gpio_in = gpch->regs + AR7_GPIO_INPUT;
  33. return !!(readl(gpio_in) & (1 << gpio));
  34. }
  35. static int titan_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
  36. {
  37. struct ar7_gpio_chip *gpch = gpiochip_get_data(chip);
  38. void __iomem *gpio_in0 = gpch->regs + TITAN_GPIO_INPUT_0;
  39. void __iomem *gpio_in1 = gpch->regs + TITAN_GPIO_INPUT_1;
  40. return readl(gpio >> 5 ? gpio_in1 : gpio_in0) & (1 << (gpio & 0x1f));
  41. }
  42. static void ar7_gpio_set_value(struct gpio_chip *chip,
  43. unsigned gpio, int value)
  44. {
  45. struct ar7_gpio_chip *gpch = gpiochip_get_data(chip);
  46. void __iomem *gpio_out = gpch->regs + AR7_GPIO_OUTPUT;
  47. unsigned tmp;
  48. tmp = readl(gpio_out) & ~(1 << gpio);
  49. if (value)
  50. tmp |= 1 << gpio;
  51. writel(tmp, gpio_out);
  52. }
  53. static void titan_gpio_set_value(struct gpio_chip *chip,
  54. unsigned gpio, int value)
  55. {
  56. struct ar7_gpio_chip *gpch = gpiochip_get_data(chip);
  57. void __iomem *gpio_out0 = gpch->regs + TITAN_GPIO_OUTPUT_0;
  58. void __iomem *gpio_out1 = gpch->regs + TITAN_GPIO_OUTPUT_1;
  59. unsigned tmp;
  60. tmp = readl(gpio >> 5 ? gpio_out1 : gpio_out0) & ~(1 << (gpio & 0x1f));
  61. if (value)
  62. tmp |= 1 << (gpio & 0x1f);
  63. writel(tmp, gpio >> 5 ? gpio_out1 : gpio_out0);
  64. }
  65. static int ar7_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
  66. {
  67. struct ar7_gpio_chip *gpch = gpiochip_get_data(chip);
  68. void __iomem *gpio_dir = gpch->regs + AR7_GPIO_DIR;
  69. writel(readl(gpio_dir) | (1 << gpio), gpio_dir);
  70. return 0;
  71. }
  72. static int titan_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
  73. {
  74. struct ar7_gpio_chip *gpch = gpiochip_get_data(chip);
  75. void __iomem *gpio_dir0 = gpch->regs + TITAN_GPIO_DIR_0;
  76. void __iomem *gpio_dir1 = gpch->regs + TITAN_GPIO_DIR_1;
  77. if (gpio >= TITAN_GPIO_MAX)
  78. return -EINVAL;
  79. writel(readl(gpio >> 5 ? gpio_dir1 : gpio_dir0) | (1 << (gpio & 0x1f)),
  80. gpio >> 5 ? gpio_dir1 : gpio_dir0);
  81. return 0;
  82. }
  83. static int ar7_gpio_direction_output(struct gpio_chip *chip,
  84. unsigned gpio, int value)
  85. {
  86. struct ar7_gpio_chip *gpch = gpiochip_get_data(chip);
  87. void __iomem *gpio_dir = gpch->regs + AR7_GPIO_DIR;
  88. ar7_gpio_set_value(chip, gpio, value);
  89. writel(readl(gpio_dir) & ~(1 << gpio), gpio_dir);
  90. return 0;
  91. }
  92. static int titan_gpio_direction_output(struct gpio_chip *chip,
  93. unsigned gpio, int value)
  94. {
  95. struct ar7_gpio_chip *gpch = gpiochip_get_data(chip);
  96. void __iomem *gpio_dir0 = gpch->regs + TITAN_GPIO_DIR_0;
  97. void __iomem *gpio_dir1 = gpch->regs + TITAN_GPIO_DIR_1;
  98. if (gpio >= TITAN_GPIO_MAX)
  99. return -EINVAL;
  100. titan_gpio_set_value(chip, gpio, value);
  101. writel(readl(gpio >> 5 ? gpio_dir1 : gpio_dir0) & ~(1 <<
  102. (gpio & 0x1f)), gpio >> 5 ? gpio_dir1 : gpio_dir0);
  103. return 0;
  104. }
  105. static struct ar7_gpio_chip ar7_gpio_chip = {
  106. .chip = {
  107. .label = "ar7-gpio",
  108. .direction_input = ar7_gpio_direction_input,
  109. .direction_output = ar7_gpio_direction_output,
  110. .set = ar7_gpio_set_value,
  111. .get = ar7_gpio_get_value,
  112. .base = 0,
  113. .ngpio = AR7_GPIO_MAX,
  114. }
  115. };
  116. static struct ar7_gpio_chip titan_gpio_chip = {
  117. .chip = {
  118. .label = "titan-gpio",
  119. .direction_input = titan_gpio_direction_input,
  120. .direction_output = titan_gpio_direction_output,
  121. .set = titan_gpio_set_value,
  122. .get = titan_gpio_get_value,
  123. .base = 0,
  124. .ngpio = TITAN_GPIO_MAX,
  125. }
  126. };
  127. static inline int ar7_gpio_enable_ar7(unsigned gpio)
  128. {
  129. void __iomem *gpio_en = ar7_gpio_chip.regs + AR7_GPIO_ENABLE;
  130. writel(readl(gpio_en) | (1 << gpio), gpio_en);
  131. return 0;
  132. }
  133. static inline int ar7_gpio_enable_titan(unsigned gpio)
  134. {
  135. void __iomem *gpio_en0 = titan_gpio_chip.regs + TITAN_GPIO_ENBL_0;
  136. void __iomem *gpio_en1 = titan_gpio_chip.regs + TITAN_GPIO_ENBL_1;
  137. writel(readl(gpio >> 5 ? gpio_en1 : gpio_en0) | (1 << (gpio & 0x1f)),
  138. gpio >> 5 ? gpio_en1 : gpio_en0);
  139. return 0;
  140. }
  141. int ar7_gpio_enable(unsigned gpio)
  142. {
  143. return ar7_is_titan() ? ar7_gpio_enable_titan(gpio) :
  144. ar7_gpio_enable_ar7(gpio);
  145. }
  146. EXPORT_SYMBOL(ar7_gpio_enable);
  147. static inline int ar7_gpio_disable_ar7(unsigned gpio)
  148. {
  149. void __iomem *gpio_en = ar7_gpio_chip.regs + AR7_GPIO_ENABLE;
  150. writel(readl(gpio_en) & ~(1 << gpio), gpio_en);
  151. return 0;
  152. }
  153. static inline int ar7_gpio_disable_titan(unsigned gpio)
  154. {
  155. void __iomem *gpio_en0 = titan_gpio_chip.regs + TITAN_GPIO_ENBL_0;
  156. void __iomem *gpio_en1 = titan_gpio_chip.regs + TITAN_GPIO_ENBL_1;
  157. writel(readl(gpio >> 5 ? gpio_en1 : gpio_en0) & ~(1 << (gpio & 0x1f)),
  158. gpio >> 5 ? gpio_en1 : gpio_en0);
  159. return 0;
  160. }
  161. int ar7_gpio_disable(unsigned gpio)
  162. {
  163. return ar7_is_titan() ? ar7_gpio_disable_titan(gpio) :
  164. ar7_gpio_disable_ar7(gpio);
  165. }
  166. EXPORT_SYMBOL(ar7_gpio_disable);
  167. struct titan_gpio_cfg {
  168. u32 reg;
  169. u32 shift;
  170. u32 func;
  171. };
  172. static const struct titan_gpio_cfg titan_gpio_table[] = {
  173. /* reg, start bit, mux value */
  174. {4, 24, 1},
  175. {4, 26, 1},
  176. {4, 28, 1},
  177. {4, 30, 1},
  178. {5, 6, 1},
  179. {5, 8, 1},
  180. {5, 10, 1},
  181. {5, 12, 1},
  182. {7, 14, 3},
  183. {7, 16, 3},
  184. {7, 18, 3},
  185. {7, 20, 3},
  186. {7, 22, 3},
  187. {7, 26, 3},
  188. {7, 28, 3},
  189. {7, 30, 3},
  190. {8, 0, 3},
  191. {8, 2, 3},
  192. {8, 4, 3},
  193. {8, 10, 3},
  194. {8, 14, 3},
  195. {8, 16, 3},
  196. {8, 18, 3},
  197. {8, 20, 3},
  198. {9, 8, 3},
  199. {9, 10, 3},
  200. {9, 12, 3},
  201. {9, 14, 3},
  202. {9, 18, 3},
  203. {9, 20, 3},
  204. {9, 24, 3},
  205. {9, 26, 3},
  206. {9, 28, 3},
  207. {9, 30, 3},
  208. {10, 0, 3},
  209. {10, 2, 3},
  210. {10, 8, 3},
  211. {10, 10, 3},
  212. {10, 12, 3},
  213. {10, 14, 3},
  214. {13, 12, 3},
  215. {13, 14, 3},
  216. {13, 16, 3},
  217. {13, 18, 3},
  218. {13, 24, 3},
  219. {13, 26, 3},
  220. {13, 28, 3},
  221. {13, 30, 3},
  222. {14, 2, 3},
  223. {14, 6, 3},
  224. {14, 8, 3},
  225. {14, 12, 3}
  226. };
  227. static int titan_gpio_pinsel(unsigned gpio)
  228. {
  229. struct titan_gpio_cfg gpio_cfg;
  230. u32 mux_status, pin_sel_reg, tmp;
  231. void __iomem *pin_sel = (void __iomem *)KSEG1ADDR(AR7_REGS_PINSEL);
  232. if (gpio >= ARRAY_SIZE(titan_gpio_table))
  233. return -EINVAL;
  234. gpio_cfg = titan_gpio_table[gpio];
  235. pin_sel_reg = gpio_cfg.reg - 1;
  236. mux_status = (readl(pin_sel + pin_sel_reg) >> gpio_cfg.shift) & 0x3;
  237. /* Check the mux status */
  238. if (!((mux_status == 0) || (mux_status == gpio_cfg.func)))
  239. return 0;
  240. /* Set the pin sel value */
  241. tmp = readl(pin_sel + pin_sel_reg);
  242. tmp |= ((gpio_cfg.func & 0x3) << gpio_cfg.shift);
  243. writel(tmp, pin_sel + pin_sel_reg);
  244. return 0;
  245. }
  246. /* Perform minimal Titan GPIO configuration */
  247. static void titan_gpio_init(void)
  248. {
  249. unsigned i;
  250. for (i = 44; i < 48; i++) {
  251. titan_gpio_pinsel(i);
  252. ar7_gpio_enable_titan(i);
  253. titan_gpio_direction_input(&titan_gpio_chip.chip, i);
  254. }
  255. }
  256. int __init ar7_gpio_init(void)
  257. {
  258. int ret;
  259. struct ar7_gpio_chip *gpch;
  260. unsigned size;
  261. if (!ar7_is_titan()) {
  262. gpch = &ar7_gpio_chip;
  263. size = 0x10;
  264. } else {
  265. gpch = &titan_gpio_chip;
  266. size = 0x1f;
  267. }
  268. gpch->regs = ioremap_nocache(AR7_REGS_GPIO, size);
  269. if (!gpch->regs) {
  270. printk(KERN_ERR "%s: failed to ioremap regs\n",
  271. gpch->chip.label);
  272. return -ENOMEM;
  273. }
  274. ret = gpiochip_add_data(&gpch->chip, gpch);
  275. if (ret) {
  276. printk(KERN_ERR "%s: failed to add gpiochip\n",
  277. gpch->chip.label);
  278. return ret;
  279. }
  280. printk(KERN_INFO "%s: registered %d GPIOs\n",
  281. gpch->chip.label, gpch->chip.ngpio);
  282. if (ar7_is_titan())
  283. titan_gpio_init();
  284. return ret;
  285. }