gpio.c 8.0 KB

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