gpio-mpc8xxx.c 11 KB

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