gpio-mpc8xxx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*
  2. * GPIOs on MPC512x/8349/8572/8610 and compatible
  3. *
  4. * Copyright (C) 2008 Peter Korsgaard <jacmet@sunsite.dk>
  5. *
  6. * This file is licensed under the terms of the GNU General Public License
  7. * version 2. This program is licensed "as is" without any warranty of any
  8. * kind, whether express or implied.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/io.h>
  14. #include <linux/of.h>
  15. #include <linux/of_gpio.h>
  16. #include <linux/of_irq.h>
  17. #include <linux/gpio.h>
  18. #include <linux/slab.h>
  19. #include <linux/irq.h>
  20. #define MPC8XXX_GPIO_PINS 32
  21. #define GPIO_DIR 0x00
  22. #define GPIO_ODR 0x04
  23. #define GPIO_DAT 0x08
  24. #define GPIO_IER 0x0c
  25. #define GPIO_IMR 0x10
  26. #define GPIO_ICR 0x14
  27. #define GPIO_ICR2 0x18
  28. struct mpc8xxx_gpio_chip {
  29. struct of_mm_gpio_chip mm_gc;
  30. spinlock_t lock;
  31. /*
  32. * shadowed data register to be able to clear/set output pins in
  33. * open drain mode safely
  34. */
  35. u32 data;
  36. struct irq_domain *irq;
  37. const void *of_dev_id_data;
  38. };
  39. static inline u32 mpc8xxx_gpio2mask(unsigned int gpio)
  40. {
  41. return 1u << (MPC8XXX_GPIO_PINS - 1 - gpio);
  42. }
  43. static inline struct mpc8xxx_gpio_chip *
  44. to_mpc8xxx_gpio_chip(struct of_mm_gpio_chip *mm)
  45. {
  46. return container_of(mm, struct mpc8xxx_gpio_chip, mm_gc);
  47. }
  48. static void mpc8xxx_gpio_save_regs(struct of_mm_gpio_chip *mm)
  49. {
  50. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  51. mpc8xxx_gc->data = in_be32(mm->regs + GPIO_DAT);
  52. }
  53. /* Workaround GPIO 1 errata on MPC8572/MPC8536. The status of GPIOs
  54. * defined as output cannot be determined by reading GPDAT register,
  55. * so we use shadow data register instead. The status of input pins
  56. * is determined by reading GPDAT register.
  57. */
  58. static int mpc8572_gpio_get(struct gpio_chip *gc, unsigned int gpio)
  59. {
  60. u32 val;
  61. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  62. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  63. u32 out_mask, out_shadow;
  64. out_mask = in_be32(mm->regs + GPIO_DIR);
  65. val = in_be32(mm->regs + GPIO_DAT) & ~out_mask;
  66. out_shadow = mpc8xxx_gc->data & out_mask;
  67. return (val | out_shadow) & mpc8xxx_gpio2mask(gpio);
  68. }
  69. static int mpc8xxx_gpio_get(struct gpio_chip *gc, unsigned int gpio)
  70. {
  71. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  72. return in_be32(mm->regs + GPIO_DAT) & mpc8xxx_gpio2mask(gpio);
  73. }
  74. static void mpc8xxx_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  75. {
  76. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  77. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  78. unsigned long flags;
  79. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  80. if (val)
  81. mpc8xxx_gc->data |= mpc8xxx_gpio2mask(gpio);
  82. else
  83. mpc8xxx_gc->data &= ~mpc8xxx_gpio2mask(gpio);
  84. out_be32(mm->regs + GPIO_DAT, mpc8xxx_gc->data);
  85. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  86. }
  87. static void mpc8xxx_gpio_set_multiple(struct gpio_chip *gc,
  88. unsigned long *mask, unsigned long *bits)
  89. {
  90. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  91. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  92. unsigned long flags;
  93. int i;
  94. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  95. for (i = 0; i < gc->ngpio; i++) {
  96. if (*mask == 0)
  97. break;
  98. if (__test_and_clear_bit(i, mask)) {
  99. if (test_bit(i, bits))
  100. mpc8xxx_gc->data |= mpc8xxx_gpio2mask(i);
  101. else
  102. mpc8xxx_gc->data &= ~mpc8xxx_gpio2mask(i);
  103. }
  104. }
  105. out_be32(mm->regs + GPIO_DAT, mpc8xxx_gc->data);
  106. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  107. }
  108. static int mpc8xxx_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
  109. {
  110. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  111. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  112. unsigned long flags;
  113. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  114. clrbits32(mm->regs + GPIO_DIR, mpc8xxx_gpio2mask(gpio));
  115. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  116. return 0;
  117. }
  118. static int mpc8xxx_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  119. {
  120. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  121. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  122. unsigned long flags;
  123. mpc8xxx_gpio_set(gc, gpio, val);
  124. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  125. setbits32(mm->regs + GPIO_DIR, mpc8xxx_gpio2mask(gpio));
  126. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  127. return 0;
  128. }
  129. static int mpc5121_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  130. {
  131. /* GPIO 28..31 are input only on MPC5121 */
  132. if (gpio >= 28)
  133. return -EINVAL;
  134. return mpc8xxx_gpio_dir_out(gc, gpio, val);
  135. }
  136. static int mpc8xxx_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
  137. {
  138. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  139. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  140. if (mpc8xxx_gc->irq && offset < MPC8XXX_GPIO_PINS)
  141. return irq_create_mapping(mpc8xxx_gc->irq, offset);
  142. else
  143. return -ENXIO;
  144. }
  145. static void mpc8xxx_gpio_irq_cascade(unsigned int irq, struct irq_desc *desc)
  146. {
  147. struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_desc_get_handler_data(desc);
  148. struct irq_chip *chip = irq_desc_get_chip(desc);
  149. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  150. unsigned int mask;
  151. mask = in_be32(mm->regs + GPIO_IER) & in_be32(mm->regs + GPIO_IMR);
  152. if (mask)
  153. generic_handle_irq(irq_linear_revmap(mpc8xxx_gc->irq,
  154. 32 - ffs(mask)));
  155. if (chip->irq_eoi)
  156. chip->irq_eoi(&desc->irq_data);
  157. }
  158. static void mpc8xxx_irq_unmask(struct irq_data *d)
  159. {
  160. struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
  161. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  162. unsigned long flags;
  163. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  164. setbits32(mm->regs + GPIO_IMR, mpc8xxx_gpio2mask(irqd_to_hwirq(d)));
  165. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  166. }
  167. static void mpc8xxx_irq_mask(struct irq_data *d)
  168. {
  169. struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
  170. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  171. unsigned long flags;
  172. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  173. clrbits32(mm->regs + GPIO_IMR, mpc8xxx_gpio2mask(irqd_to_hwirq(d)));
  174. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  175. }
  176. static void mpc8xxx_irq_ack(struct irq_data *d)
  177. {
  178. struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
  179. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  180. out_be32(mm->regs + GPIO_IER, mpc8xxx_gpio2mask(irqd_to_hwirq(d)));
  181. }
  182. static int mpc8xxx_irq_set_type(struct irq_data *d, unsigned int flow_type)
  183. {
  184. struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
  185. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  186. unsigned long flags;
  187. switch (flow_type) {
  188. case IRQ_TYPE_EDGE_FALLING:
  189. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  190. setbits32(mm->regs + GPIO_ICR,
  191. mpc8xxx_gpio2mask(irqd_to_hwirq(d)));
  192. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  193. break;
  194. case IRQ_TYPE_EDGE_BOTH:
  195. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  196. clrbits32(mm->regs + GPIO_ICR,
  197. mpc8xxx_gpio2mask(irqd_to_hwirq(d)));
  198. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  199. break;
  200. default:
  201. return -EINVAL;
  202. }
  203. return 0;
  204. }
  205. static int mpc512x_irq_set_type(struct irq_data *d, unsigned int flow_type)
  206. {
  207. struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
  208. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  209. unsigned long gpio = irqd_to_hwirq(d);
  210. void __iomem *reg;
  211. unsigned int shift;
  212. unsigned long flags;
  213. if (gpio < 16) {
  214. reg = mm->regs + GPIO_ICR;
  215. shift = (15 - gpio) * 2;
  216. } else {
  217. reg = mm->regs + GPIO_ICR2;
  218. shift = (15 - (gpio % 16)) * 2;
  219. }
  220. switch (flow_type) {
  221. case IRQ_TYPE_EDGE_FALLING:
  222. case IRQ_TYPE_LEVEL_LOW:
  223. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  224. clrsetbits_be32(reg, 3 << shift, 2 << shift);
  225. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  226. break;
  227. case IRQ_TYPE_EDGE_RISING:
  228. case IRQ_TYPE_LEVEL_HIGH:
  229. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  230. clrsetbits_be32(reg, 3 << shift, 1 << shift);
  231. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  232. break;
  233. case IRQ_TYPE_EDGE_BOTH:
  234. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  235. clrbits32(reg, 3 << shift);
  236. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  237. break;
  238. default:
  239. return -EINVAL;
  240. }
  241. return 0;
  242. }
  243. static struct irq_chip mpc8xxx_irq_chip = {
  244. .name = "mpc8xxx-gpio",
  245. .irq_unmask = mpc8xxx_irq_unmask,
  246. .irq_mask = mpc8xxx_irq_mask,
  247. .irq_ack = mpc8xxx_irq_ack,
  248. .irq_set_type = mpc8xxx_irq_set_type,
  249. };
  250. static int mpc8xxx_gpio_irq_map(struct irq_domain *h, unsigned int irq,
  251. irq_hw_number_t hwirq)
  252. {
  253. struct mpc8xxx_gpio_chip *mpc8xxx_gc = h->host_data;
  254. if (mpc8xxx_gc->of_dev_id_data)
  255. mpc8xxx_irq_chip.irq_set_type = mpc8xxx_gc->of_dev_id_data;
  256. irq_set_chip_data(irq, h->host_data);
  257. irq_set_chip_and_handler(irq, &mpc8xxx_irq_chip, handle_level_irq);
  258. return 0;
  259. }
  260. static struct irq_domain_ops mpc8xxx_gpio_irq_ops = {
  261. .map = mpc8xxx_gpio_irq_map,
  262. .xlate = irq_domain_xlate_twocell,
  263. };
  264. static struct of_device_id mpc8xxx_gpio_ids[] __initdata = {
  265. { .compatible = "fsl,mpc8349-gpio", },
  266. { .compatible = "fsl,mpc8572-gpio", },
  267. { .compatible = "fsl,mpc8610-gpio", },
  268. { .compatible = "fsl,mpc5121-gpio", .data = mpc512x_irq_set_type, },
  269. { .compatible = "fsl,pq3-gpio", },
  270. { .compatible = "fsl,qoriq-gpio", },
  271. {}
  272. };
  273. static void __init mpc8xxx_add_controller(struct device_node *np)
  274. {
  275. struct mpc8xxx_gpio_chip *mpc8xxx_gc;
  276. struct of_mm_gpio_chip *mm_gc;
  277. struct gpio_chip *gc;
  278. const struct of_device_id *id;
  279. unsigned hwirq;
  280. int ret;
  281. mpc8xxx_gc = kzalloc(sizeof(*mpc8xxx_gc), GFP_KERNEL);
  282. if (!mpc8xxx_gc) {
  283. ret = -ENOMEM;
  284. goto err;
  285. }
  286. spin_lock_init(&mpc8xxx_gc->lock);
  287. mm_gc = &mpc8xxx_gc->mm_gc;
  288. gc = &mm_gc->gc;
  289. mm_gc->save_regs = mpc8xxx_gpio_save_regs;
  290. gc->ngpio = MPC8XXX_GPIO_PINS;
  291. gc->direction_input = mpc8xxx_gpio_dir_in;
  292. gc->direction_output = of_device_is_compatible(np, "fsl,mpc5121-gpio") ?
  293. mpc5121_gpio_dir_out : mpc8xxx_gpio_dir_out;
  294. gc->get = of_device_is_compatible(np, "fsl,mpc8572-gpio") ?
  295. mpc8572_gpio_get : mpc8xxx_gpio_get;
  296. gc->set = mpc8xxx_gpio_set;
  297. gc->set_multiple = mpc8xxx_gpio_set_multiple;
  298. gc->to_irq = mpc8xxx_gpio_to_irq;
  299. ret = of_mm_gpiochip_add(np, mm_gc);
  300. if (ret)
  301. goto err;
  302. hwirq = irq_of_parse_and_map(np, 0);
  303. if (hwirq == NO_IRQ)
  304. goto skip_irq;
  305. mpc8xxx_gc->irq = irq_domain_add_linear(np, MPC8XXX_GPIO_PINS,
  306. &mpc8xxx_gpio_irq_ops, mpc8xxx_gc);
  307. if (!mpc8xxx_gc->irq)
  308. goto skip_irq;
  309. id = of_match_node(mpc8xxx_gpio_ids, np);
  310. if (id)
  311. mpc8xxx_gc->of_dev_id_data = id->data;
  312. /* ack and mask all irqs */
  313. out_be32(mm_gc->regs + GPIO_IER, 0xffffffff);
  314. out_be32(mm_gc->regs + GPIO_IMR, 0);
  315. irq_set_handler_data(hwirq, mpc8xxx_gc);
  316. irq_set_chained_handler(hwirq, mpc8xxx_gpio_irq_cascade);
  317. skip_irq:
  318. return;
  319. err:
  320. pr_err("%s: registration failed with status %d\n",
  321. np->full_name, ret);
  322. kfree(mpc8xxx_gc);
  323. return;
  324. }
  325. static int __init mpc8xxx_add_gpiochips(void)
  326. {
  327. struct device_node *np;
  328. for_each_matching_node(np, mpc8xxx_gpio_ids)
  329. mpc8xxx_add_controller(np);
  330. return 0;
  331. }
  332. arch_initcall(mpc8xxx_add_gpiochips);