gpio.c 8.2 KB

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