gpio.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /*
  2. * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
  3. * JZ4740 platform GPIO support
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. *
  10. * You should have received a copy of the GNU General Public License along
  11. * with this program; if not, write to the Free Software Foundation, Inc.,
  12. * 675 Mass Ave, Cambridge, MA 02139, USA.
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/io.h>
  19. #include <linux/gpio.h>
  20. #include <linux/delay.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/bitops.h>
  23. #include <linux/debugfs.h>
  24. #include <linux/seq_file.h>
  25. #include <asm/mach-jz4740/base.h>
  26. #include "irq.h"
  27. #define JZ4740_GPIO_BASE_A (32*0)
  28. #define JZ4740_GPIO_BASE_B (32*1)
  29. #define JZ4740_GPIO_BASE_C (32*2)
  30. #define JZ4740_GPIO_BASE_D (32*3)
  31. #define JZ4740_GPIO_NUM_A 32
  32. #define JZ4740_GPIO_NUM_B 32
  33. #define JZ4740_GPIO_NUM_C 31
  34. #define JZ4740_GPIO_NUM_D 32
  35. #define JZ4740_IRQ_GPIO_BASE_A (JZ4740_IRQ_GPIO(0) + JZ4740_GPIO_BASE_A)
  36. #define JZ4740_IRQ_GPIO_BASE_B (JZ4740_IRQ_GPIO(0) + JZ4740_GPIO_BASE_B)
  37. #define JZ4740_IRQ_GPIO_BASE_C (JZ4740_IRQ_GPIO(0) + JZ4740_GPIO_BASE_C)
  38. #define JZ4740_IRQ_GPIO_BASE_D (JZ4740_IRQ_GPIO(0) + JZ4740_GPIO_BASE_D)
  39. #define JZ_REG_GPIO_PIN 0x00
  40. #define JZ_REG_GPIO_DATA 0x10
  41. #define JZ_REG_GPIO_DATA_SET 0x14
  42. #define JZ_REG_GPIO_DATA_CLEAR 0x18
  43. #define JZ_REG_GPIO_MASK 0x20
  44. #define JZ_REG_GPIO_MASK_SET 0x24
  45. #define JZ_REG_GPIO_MASK_CLEAR 0x28
  46. #define JZ_REG_GPIO_PULL 0x30
  47. #define JZ_REG_GPIO_PULL_SET 0x34
  48. #define JZ_REG_GPIO_PULL_CLEAR 0x38
  49. #define JZ_REG_GPIO_FUNC 0x40
  50. #define JZ_REG_GPIO_FUNC_SET 0x44
  51. #define JZ_REG_GPIO_FUNC_CLEAR 0x48
  52. #define JZ_REG_GPIO_SELECT 0x50
  53. #define JZ_REG_GPIO_SELECT_SET 0x54
  54. #define JZ_REG_GPIO_SELECT_CLEAR 0x58
  55. #define JZ_REG_GPIO_DIRECTION 0x60
  56. #define JZ_REG_GPIO_DIRECTION_SET 0x64
  57. #define JZ_REG_GPIO_DIRECTION_CLEAR 0x68
  58. #define JZ_REG_GPIO_TRIGGER 0x70
  59. #define JZ_REG_GPIO_TRIGGER_SET 0x74
  60. #define JZ_REG_GPIO_TRIGGER_CLEAR 0x78
  61. #define JZ_REG_GPIO_FLAG 0x80
  62. #define JZ_REG_GPIO_FLAG_CLEAR 0x14
  63. #define GPIO_TO_BIT(gpio) BIT(gpio & 0x1f)
  64. #define GPIO_TO_REG(gpio, reg) (gpio_to_jz_gpio_chip(gpio)->base + (reg))
  65. #define CHIP_TO_REG(chip, reg) (gpio_chip_to_jz_gpio_chip(chip)->base + (reg))
  66. struct jz_gpio_chip {
  67. unsigned int irq;
  68. unsigned int irq_base;
  69. uint32_t edge_trigger_both;
  70. void __iomem *base;
  71. struct gpio_chip gpio_chip;
  72. };
  73. static struct jz_gpio_chip jz4740_gpio_chips[];
  74. static inline struct jz_gpio_chip *gpio_to_jz_gpio_chip(unsigned int gpio)
  75. {
  76. return &jz4740_gpio_chips[gpio >> 5];
  77. }
  78. static inline struct jz_gpio_chip *gpio_chip_to_jz_gpio_chip(struct gpio_chip *gpio_chip)
  79. {
  80. return container_of(gpio_chip, struct jz_gpio_chip, gpio_chip);
  81. }
  82. static inline struct jz_gpio_chip *irq_to_jz_gpio_chip(struct irq_data *data)
  83. {
  84. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(data);
  85. return gc->private;
  86. }
  87. static inline void jz_gpio_write_bit(unsigned int gpio, unsigned int reg)
  88. {
  89. writel(GPIO_TO_BIT(gpio), GPIO_TO_REG(gpio, reg));
  90. }
  91. int jz_gpio_set_function(int gpio, enum jz_gpio_function function)
  92. {
  93. if (function == JZ_GPIO_FUNC_NONE) {
  94. jz_gpio_write_bit(gpio, JZ_REG_GPIO_FUNC_CLEAR);
  95. jz_gpio_write_bit(gpio, JZ_REG_GPIO_SELECT_CLEAR);
  96. jz_gpio_write_bit(gpio, JZ_REG_GPIO_TRIGGER_CLEAR);
  97. } else {
  98. jz_gpio_write_bit(gpio, JZ_REG_GPIO_FUNC_SET);
  99. jz_gpio_write_bit(gpio, JZ_REG_GPIO_TRIGGER_CLEAR);
  100. switch (function) {
  101. case JZ_GPIO_FUNC1:
  102. jz_gpio_write_bit(gpio, JZ_REG_GPIO_SELECT_CLEAR);
  103. break;
  104. case JZ_GPIO_FUNC3:
  105. jz_gpio_write_bit(gpio, JZ_REG_GPIO_TRIGGER_SET);
  106. case JZ_GPIO_FUNC2: /* Falltrough */
  107. jz_gpio_write_bit(gpio, JZ_REG_GPIO_SELECT_SET);
  108. break;
  109. default:
  110. BUG();
  111. break;
  112. }
  113. }
  114. return 0;
  115. }
  116. EXPORT_SYMBOL_GPL(jz_gpio_set_function);
  117. int jz_gpio_bulk_request(const struct jz_gpio_bulk_request *request, size_t num)
  118. {
  119. size_t i;
  120. int ret;
  121. for (i = 0; i < num; ++i, ++request) {
  122. ret = gpio_request(request->gpio, request->name);
  123. if (ret)
  124. goto err;
  125. jz_gpio_set_function(request->gpio, request->function);
  126. }
  127. return 0;
  128. err:
  129. for (--request; i > 0; --i, --request) {
  130. gpio_free(request->gpio);
  131. jz_gpio_set_function(request->gpio, JZ_GPIO_FUNC_NONE);
  132. }
  133. return ret;
  134. }
  135. EXPORT_SYMBOL_GPL(jz_gpio_bulk_request);
  136. void jz_gpio_bulk_free(const struct jz_gpio_bulk_request *request, size_t num)
  137. {
  138. size_t i;
  139. for (i = 0; i < num; ++i, ++request) {
  140. gpio_free(request->gpio);
  141. jz_gpio_set_function(request->gpio, JZ_GPIO_FUNC_NONE);
  142. }
  143. }
  144. EXPORT_SYMBOL_GPL(jz_gpio_bulk_free);
  145. void jz_gpio_bulk_suspend(const struct jz_gpio_bulk_request *request, size_t num)
  146. {
  147. size_t i;
  148. for (i = 0; i < num; ++i, ++request) {
  149. jz_gpio_set_function(request->gpio, JZ_GPIO_FUNC_NONE);
  150. jz_gpio_write_bit(request->gpio, JZ_REG_GPIO_DIRECTION_CLEAR);
  151. jz_gpio_write_bit(request->gpio, JZ_REG_GPIO_PULL_SET);
  152. }
  153. }
  154. EXPORT_SYMBOL_GPL(jz_gpio_bulk_suspend);
  155. void jz_gpio_bulk_resume(const struct jz_gpio_bulk_request *request, size_t num)
  156. {
  157. size_t i;
  158. for (i = 0; i < num; ++i, ++request)
  159. jz_gpio_set_function(request->gpio, request->function);
  160. }
  161. EXPORT_SYMBOL_GPL(jz_gpio_bulk_resume);
  162. void jz_gpio_enable_pullup(unsigned gpio)
  163. {
  164. jz_gpio_write_bit(gpio, JZ_REG_GPIO_PULL_CLEAR);
  165. }
  166. EXPORT_SYMBOL_GPL(jz_gpio_enable_pullup);
  167. void jz_gpio_disable_pullup(unsigned gpio)
  168. {
  169. jz_gpio_write_bit(gpio, JZ_REG_GPIO_PULL_SET);
  170. }
  171. EXPORT_SYMBOL_GPL(jz_gpio_disable_pullup);
  172. static int jz_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
  173. {
  174. return !!(readl(CHIP_TO_REG(chip, JZ_REG_GPIO_PIN)) & BIT(gpio));
  175. }
  176. static void jz_gpio_set_value(struct gpio_chip *chip, unsigned gpio, int value)
  177. {
  178. uint32_t __iomem *reg = CHIP_TO_REG(chip, JZ_REG_GPIO_DATA_SET);
  179. reg += !value;
  180. writel(BIT(gpio), reg);
  181. }
  182. static int jz_gpio_direction_output(struct gpio_chip *chip, unsigned gpio,
  183. int value)
  184. {
  185. writel(BIT(gpio), CHIP_TO_REG(chip, JZ_REG_GPIO_DIRECTION_SET));
  186. jz_gpio_set_value(chip, gpio, value);
  187. return 0;
  188. }
  189. static int jz_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
  190. {
  191. writel(BIT(gpio), CHIP_TO_REG(chip, JZ_REG_GPIO_DIRECTION_CLEAR));
  192. return 0;
  193. }
  194. int jz_gpio_port_direction_input(int port, uint32_t mask)
  195. {
  196. writel(mask, GPIO_TO_REG(port, JZ_REG_GPIO_DIRECTION_CLEAR));
  197. return 0;
  198. }
  199. EXPORT_SYMBOL(jz_gpio_port_direction_input);
  200. int jz_gpio_port_direction_output(int port, uint32_t mask)
  201. {
  202. writel(mask, GPIO_TO_REG(port, JZ_REG_GPIO_DIRECTION_SET));
  203. return 0;
  204. }
  205. EXPORT_SYMBOL(jz_gpio_port_direction_output);
  206. void jz_gpio_port_set_value(int port, uint32_t value, uint32_t mask)
  207. {
  208. writel(~value & mask, GPIO_TO_REG(port, JZ_REG_GPIO_DATA_CLEAR));
  209. writel(value & mask, GPIO_TO_REG(port, JZ_REG_GPIO_DATA_SET));
  210. }
  211. EXPORT_SYMBOL(jz_gpio_port_set_value);
  212. uint32_t jz_gpio_port_get_value(int port, uint32_t mask)
  213. {
  214. uint32_t value = readl(GPIO_TO_REG(port, JZ_REG_GPIO_PIN));
  215. return value & mask;
  216. }
  217. EXPORT_SYMBOL(jz_gpio_port_get_value);
  218. int gpio_to_irq(unsigned gpio)
  219. {
  220. return JZ4740_IRQ_GPIO(0) + gpio;
  221. }
  222. EXPORT_SYMBOL_GPL(gpio_to_irq);
  223. int irq_to_gpio(unsigned irq)
  224. {
  225. return irq - JZ4740_IRQ_GPIO(0);
  226. }
  227. EXPORT_SYMBOL_GPL(irq_to_gpio);
  228. #define IRQ_TO_BIT(irq) BIT(irq_to_gpio(irq) & 0x1f)
  229. static void jz_gpio_check_trigger_both(struct jz_gpio_chip *chip, unsigned int irq)
  230. {
  231. uint32_t value;
  232. void __iomem *reg;
  233. uint32_t mask = IRQ_TO_BIT(irq);
  234. if (!(chip->edge_trigger_both & mask))
  235. return;
  236. reg = chip->base;
  237. value = readl(chip->base + JZ_REG_GPIO_PIN);
  238. if (value & mask)
  239. reg += JZ_REG_GPIO_DIRECTION_CLEAR;
  240. else
  241. reg += JZ_REG_GPIO_DIRECTION_SET;
  242. writel(mask, reg);
  243. }
  244. static void jz_gpio_irq_demux_handler(unsigned int irq, struct irq_desc *desc)
  245. {
  246. uint32_t flag;
  247. unsigned int gpio_irq;
  248. unsigned int gpio_bank;
  249. struct jz_gpio_chip *chip = irq_desc_get_handler_data(desc);
  250. gpio_bank = JZ4740_IRQ_GPIO0 - irq;
  251. flag = readl(chip->base + JZ_REG_GPIO_FLAG);
  252. if (!flag)
  253. return;
  254. gpio_irq = __fls(flag);
  255. jz_gpio_check_trigger_both(chip, irq);
  256. gpio_irq += (gpio_bank << 5) + JZ4740_IRQ_GPIO(0);
  257. generic_handle_irq(gpio_irq);
  258. };
  259. static inline void jz_gpio_set_irq_bit(struct irq_data *data, unsigned int reg)
  260. {
  261. struct jz_gpio_chip *chip = irq_to_jz_gpio_chip(data);
  262. writel(IRQ_TO_BIT(data->irq), chip->base + reg);
  263. }
  264. static void jz_gpio_irq_unmask(struct irq_data *data)
  265. {
  266. struct jz_gpio_chip *chip = irq_to_jz_gpio_chip(data);
  267. jz_gpio_check_trigger_both(chip, data->irq);
  268. irq_gc_unmask_enable_reg(data);
  269. };
  270. /* TODO: Check if function is gpio */
  271. static unsigned int jz_gpio_irq_startup(struct irq_data *data)
  272. {
  273. jz_gpio_set_irq_bit(data, JZ_REG_GPIO_SELECT_SET);
  274. jz_gpio_irq_unmask(data);
  275. return 0;
  276. }
  277. static void jz_gpio_irq_shutdown(struct irq_data *data)
  278. {
  279. irq_gc_mask_disable_reg(data);
  280. /* Set direction to input */
  281. jz_gpio_set_irq_bit(data, JZ_REG_GPIO_DIRECTION_CLEAR);
  282. jz_gpio_set_irq_bit(data, JZ_REG_GPIO_SELECT_CLEAR);
  283. }
  284. static int jz_gpio_irq_set_type(struct irq_data *data, unsigned int flow_type)
  285. {
  286. struct jz_gpio_chip *chip = irq_to_jz_gpio_chip(data);
  287. unsigned int irq = data->irq;
  288. if (flow_type == IRQ_TYPE_EDGE_BOTH) {
  289. uint32_t value = readl(chip->base + JZ_REG_GPIO_PIN);
  290. if (value & IRQ_TO_BIT(irq))
  291. flow_type = IRQ_TYPE_EDGE_FALLING;
  292. else
  293. flow_type = IRQ_TYPE_EDGE_RISING;
  294. chip->edge_trigger_both |= IRQ_TO_BIT(irq);
  295. } else {
  296. chip->edge_trigger_both &= ~IRQ_TO_BIT(irq);
  297. }
  298. switch (flow_type) {
  299. case IRQ_TYPE_EDGE_RISING:
  300. jz_gpio_set_irq_bit(data, JZ_REG_GPIO_DIRECTION_SET);
  301. jz_gpio_set_irq_bit(data, JZ_REG_GPIO_TRIGGER_SET);
  302. break;
  303. case IRQ_TYPE_EDGE_FALLING:
  304. jz_gpio_set_irq_bit(data, JZ_REG_GPIO_DIRECTION_CLEAR);
  305. jz_gpio_set_irq_bit(data, JZ_REG_GPIO_TRIGGER_SET);
  306. break;
  307. case IRQ_TYPE_LEVEL_HIGH:
  308. jz_gpio_set_irq_bit(data, JZ_REG_GPIO_DIRECTION_SET);
  309. jz_gpio_set_irq_bit(data, JZ_REG_GPIO_TRIGGER_CLEAR);
  310. break;
  311. case IRQ_TYPE_LEVEL_LOW:
  312. jz_gpio_set_irq_bit(data, JZ_REG_GPIO_DIRECTION_CLEAR);
  313. jz_gpio_set_irq_bit(data, JZ_REG_GPIO_TRIGGER_CLEAR);
  314. break;
  315. default:
  316. return -EINVAL;
  317. }
  318. return 0;
  319. }
  320. static int jz_gpio_irq_set_wake(struct irq_data *data, unsigned int on)
  321. {
  322. struct jz_gpio_chip *chip = irq_to_jz_gpio_chip(data);
  323. irq_gc_set_wake(data, on);
  324. irq_set_irq_wake(chip->irq, on);
  325. return 0;
  326. }
  327. #define JZ4740_GPIO_CHIP(_bank) { \
  328. .irq_base = JZ4740_IRQ_GPIO_BASE_ ## _bank, \
  329. .gpio_chip = { \
  330. .label = "Bank " # _bank, \
  331. .owner = THIS_MODULE, \
  332. .set = jz_gpio_set_value, \
  333. .get = jz_gpio_get_value, \
  334. .direction_output = jz_gpio_direction_output, \
  335. .direction_input = jz_gpio_direction_input, \
  336. .base = JZ4740_GPIO_BASE_ ## _bank, \
  337. .ngpio = JZ4740_GPIO_NUM_ ## _bank, \
  338. }, \
  339. }
  340. static struct jz_gpio_chip jz4740_gpio_chips[] = {
  341. JZ4740_GPIO_CHIP(A),
  342. JZ4740_GPIO_CHIP(B),
  343. JZ4740_GPIO_CHIP(C),
  344. JZ4740_GPIO_CHIP(D),
  345. };
  346. static void jz4740_gpio_chip_init(struct jz_gpio_chip *chip, unsigned int id)
  347. {
  348. struct irq_chip_generic *gc;
  349. struct irq_chip_type *ct;
  350. chip->base = ioremap(JZ4740_GPIO_BASE_ADDR + (id * 0x100), 0x100);
  351. chip->irq = JZ4740_IRQ_INTC_GPIO(id);
  352. irq_set_handler_data(chip->irq, chip);
  353. irq_set_chained_handler(chip->irq, jz_gpio_irq_demux_handler);
  354. gc = irq_alloc_generic_chip(chip->gpio_chip.label, 1, chip->irq_base,
  355. chip->base, handle_level_irq);
  356. gc->wake_enabled = IRQ_MSK(chip->gpio_chip.ngpio);
  357. gc->private = chip;
  358. ct = gc->chip_types;
  359. ct->regs.enable = JZ_REG_GPIO_MASK_CLEAR;
  360. ct->regs.disable = JZ_REG_GPIO_MASK_SET;
  361. ct->regs.ack = JZ_REG_GPIO_FLAG_CLEAR;
  362. ct->chip.name = "GPIO";
  363. ct->chip.irq_mask = irq_gc_mask_disable_reg;
  364. ct->chip.irq_unmask = jz_gpio_irq_unmask;
  365. ct->chip.irq_ack = irq_gc_ack_set_bit;
  366. ct->chip.irq_suspend = jz4740_irq_suspend;
  367. ct->chip.irq_resume = jz4740_irq_resume;
  368. ct->chip.irq_startup = jz_gpio_irq_startup;
  369. ct->chip.irq_shutdown = jz_gpio_irq_shutdown;
  370. ct->chip.irq_set_type = jz_gpio_irq_set_type;
  371. ct->chip.irq_set_wake = jz_gpio_irq_set_wake;
  372. ct->chip.flags = IRQCHIP_SET_TYPE_MASKED;
  373. irq_setup_generic_chip(gc, IRQ_MSK(chip->gpio_chip.ngpio),
  374. IRQ_GC_INIT_NESTED_LOCK, 0, IRQ_NOPROBE | IRQ_LEVEL);
  375. gpiochip_add(&chip->gpio_chip);
  376. }
  377. static int __init jz4740_gpio_init(void)
  378. {
  379. unsigned int i;
  380. for (i = 0; i < ARRAY_SIZE(jz4740_gpio_chips); ++i)
  381. jz4740_gpio_chip_init(&jz4740_gpio_chips[i], i);
  382. printk(KERN_INFO "JZ4740 GPIO initialized\n");
  383. return 0;
  384. }
  385. arch_initcall(jz4740_gpio_init);
  386. #ifdef CONFIG_DEBUG_FS
  387. static inline void gpio_seq_reg(struct seq_file *s, struct jz_gpio_chip *chip,
  388. const char *name, unsigned int reg)
  389. {
  390. seq_printf(s, "\t%s: %08x\n", name, readl(chip->base + reg));
  391. }
  392. static int gpio_regs_show(struct seq_file *s, void *unused)
  393. {
  394. struct jz_gpio_chip *chip = jz4740_gpio_chips;
  395. int i;
  396. for (i = 0; i < ARRAY_SIZE(jz4740_gpio_chips); ++i, ++chip) {
  397. seq_printf(s, "==GPIO %d==\n", i);
  398. gpio_seq_reg(s, chip, "Pin", JZ_REG_GPIO_PIN);
  399. gpio_seq_reg(s, chip, "Data", JZ_REG_GPIO_DATA);
  400. gpio_seq_reg(s, chip, "Mask", JZ_REG_GPIO_MASK);
  401. gpio_seq_reg(s, chip, "Pull", JZ_REG_GPIO_PULL);
  402. gpio_seq_reg(s, chip, "Func", JZ_REG_GPIO_FUNC);
  403. gpio_seq_reg(s, chip, "Select", JZ_REG_GPIO_SELECT);
  404. gpio_seq_reg(s, chip, "Direction", JZ_REG_GPIO_DIRECTION);
  405. gpio_seq_reg(s, chip, "Trigger", JZ_REG_GPIO_TRIGGER);
  406. gpio_seq_reg(s, chip, "Flag", JZ_REG_GPIO_FLAG);
  407. }
  408. return 0;
  409. }
  410. static int gpio_regs_open(struct inode *inode, struct file *file)
  411. {
  412. return single_open(file, gpio_regs_show, NULL);
  413. }
  414. static const struct file_operations gpio_regs_operations = {
  415. .open = gpio_regs_open,
  416. .read = seq_read,
  417. .llseek = seq_lseek,
  418. .release = single_release,
  419. };
  420. static int __init gpio_debugfs_init(void)
  421. {
  422. (void) debugfs_create_file("jz_regs_gpio", S_IFREG | S_IRUGO,
  423. NULL, NULL, &gpio_regs_operations);
  424. return 0;
  425. }
  426. subsys_initcall(gpio_debugfs_init);
  427. #endif