gpio.c 8.2 KB

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