gpio-mpc8xxx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. raw_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. raw_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. raw_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. raw_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. raw_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. raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  116. clrbits32(mm->regs + GPIO_DIR, mpc8xxx_gpio2mask(gpio));
  117. raw_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. raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  127. setbits32(mm->regs + GPIO_DIR, mpc8xxx_gpio2mask(gpio));
  128. raw_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 mpc5125_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  139. {
  140. /* GPIO 0..3 are input only on MPC5125 */
  141. if (gpio <= 3)
  142. return -EINVAL;
  143. return mpc8xxx_gpio_dir_out(gc, gpio, val);
  144. }
  145. static int mpc8xxx_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
  146. {
  147. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  148. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  149. if (mpc8xxx_gc->irq && offset < MPC8XXX_GPIO_PINS)
  150. return irq_create_mapping(mpc8xxx_gc->irq, offset);
  151. else
  152. return -ENXIO;
  153. }
  154. static void mpc8xxx_gpio_irq_cascade(struct irq_desc *desc)
  155. {
  156. struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_desc_get_handler_data(desc);
  157. struct irq_chip *chip = irq_desc_get_chip(desc);
  158. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  159. unsigned int mask;
  160. mask = in_be32(mm->regs + GPIO_IER) & in_be32(mm->regs + GPIO_IMR);
  161. if (mask)
  162. generic_handle_irq(irq_linear_revmap(mpc8xxx_gc->irq,
  163. 32 - ffs(mask)));
  164. if (chip->irq_eoi)
  165. chip->irq_eoi(&desc->irq_data);
  166. }
  167. static void mpc8xxx_irq_unmask(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. raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  173. setbits32(mm->regs + GPIO_IMR, mpc8xxx_gpio2mask(irqd_to_hwirq(d)));
  174. raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  175. }
  176. static void mpc8xxx_irq_mask(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. unsigned long flags;
  181. raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  182. clrbits32(mm->regs + GPIO_IMR, mpc8xxx_gpio2mask(irqd_to_hwirq(d)));
  183. raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  184. }
  185. static void mpc8xxx_irq_ack(struct irq_data *d)
  186. {
  187. struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
  188. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  189. out_be32(mm->regs + GPIO_IER, mpc8xxx_gpio2mask(irqd_to_hwirq(d)));
  190. }
  191. static int mpc8xxx_irq_set_type(struct irq_data *d, unsigned int flow_type)
  192. {
  193. struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
  194. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  195. unsigned long flags;
  196. switch (flow_type) {
  197. case IRQ_TYPE_EDGE_FALLING:
  198. raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  199. setbits32(mm->regs + GPIO_ICR,
  200. mpc8xxx_gpio2mask(irqd_to_hwirq(d)));
  201. raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  202. break;
  203. case IRQ_TYPE_EDGE_BOTH:
  204. raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  205. clrbits32(mm->regs + GPIO_ICR,
  206. mpc8xxx_gpio2mask(irqd_to_hwirq(d)));
  207. raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  208. break;
  209. default:
  210. return -EINVAL;
  211. }
  212. return 0;
  213. }
  214. static int mpc512x_irq_set_type(struct irq_data *d, unsigned int flow_type)
  215. {
  216. struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
  217. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  218. unsigned long gpio = irqd_to_hwirq(d);
  219. void __iomem *reg;
  220. unsigned int shift;
  221. unsigned long flags;
  222. if (gpio < 16) {
  223. reg = mm->regs + GPIO_ICR;
  224. shift = (15 - gpio) * 2;
  225. } else {
  226. reg = mm->regs + GPIO_ICR2;
  227. shift = (15 - (gpio % 16)) * 2;
  228. }
  229. switch (flow_type) {
  230. case IRQ_TYPE_EDGE_FALLING:
  231. case IRQ_TYPE_LEVEL_LOW:
  232. raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  233. clrsetbits_be32(reg, 3 << shift, 2 << shift);
  234. raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  235. break;
  236. case IRQ_TYPE_EDGE_RISING:
  237. case IRQ_TYPE_LEVEL_HIGH:
  238. raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  239. clrsetbits_be32(reg, 3 << shift, 1 << shift);
  240. raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  241. break;
  242. case IRQ_TYPE_EDGE_BOTH:
  243. raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  244. clrbits32(reg, 3 << shift);
  245. raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  246. break;
  247. default:
  248. return -EINVAL;
  249. }
  250. return 0;
  251. }
  252. static struct irq_chip mpc8xxx_irq_chip = {
  253. .name = "mpc8xxx-gpio",
  254. .irq_unmask = mpc8xxx_irq_unmask,
  255. .irq_mask = mpc8xxx_irq_mask,
  256. .irq_ack = mpc8xxx_irq_ack,
  257. /* this might get overwritten in mpc8xxx_probe() */
  258. .irq_set_type = mpc8xxx_irq_set_type,
  259. };
  260. static int mpc8xxx_gpio_irq_map(struct irq_domain *h, unsigned int irq,
  261. irq_hw_number_t hwirq)
  262. {
  263. irq_set_chip_data(irq, h->host_data);
  264. irq_set_chip_and_handler(irq, &mpc8xxx_irq_chip, handle_edge_irq);
  265. return 0;
  266. }
  267. static const struct irq_domain_ops mpc8xxx_gpio_irq_ops = {
  268. .map = mpc8xxx_gpio_irq_map,
  269. .xlate = irq_domain_xlate_twocell,
  270. };
  271. struct mpc8xxx_gpio_devtype {
  272. int (*gpio_dir_out)(struct gpio_chip *, unsigned int, int);
  273. int (*gpio_get)(struct gpio_chip *, unsigned int);
  274. int (*irq_set_type)(struct irq_data *, unsigned int);
  275. };
  276. static const struct mpc8xxx_gpio_devtype mpc512x_gpio_devtype = {
  277. .gpio_dir_out = mpc5121_gpio_dir_out,
  278. .irq_set_type = mpc512x_irq_set_type,
  279. };
  280. static const struct mpc8xxx_gpio_devtype mpc5125_gpio_devtype = {
  281. .gpio_dir_out = mpc5125_gpio_dir_out,
  282. .irq_set_type = mpc512x_irq_set_type,
  283. };
  284. static const struct mpc8xxx_gpio_devtype mpc8572_gpio_devtype = {
  285. .gpio_get = mpc8572_gpio_get,
  286. };
  287. static const struct mpc8xxx_gpio_devtype mpc8xxx_gpio_devtype_default = {
  288. .gpio_dir_out = mpc8xxx_gpio_dir_out,
  289. .gpio_get = mpc8xxx_gpio_get,
  290. .irq_set_type = mpc8xxx_irq_set_type,
  291. };
  292. static const struct of_device_id mpc8xxx_gpio_ids[] = {
  293. { .compatible = "fsl,mpc8349-gpio", },
  294. { .compatible = "fsl,mpc8572-gpio", .data = &mpc8572_gpio_devtype, },
  295. { .compatible = "fsl,mpc8610-gpio", },
  296. { .compatible = "fsl,mpc5121-gpio", .data = &mpc512x_gpio_devtype, },
  297. { .compatible = "fsl,mpc5125-gpio", .data = &mpc5125_gpio_devtype, },
  298. { .compatible = "fsl,pq3-gpio", },
  299. { .compatible = "fsl,qoriq-gpio", },
  300. {}
  301. };
  302. static int mpc8xxx_probe(struct platform_device *pdev)
  303. {
  304. struct device_node *np = pdev->dev.of_node;
  305. struct mpc8xxx_gpio_chip *mpc8xxx_gc;
  306. struct of_mm_gpio_chip *mm_gc;
  307. struct gpio_chip *gc;
  308. const struct of_device_id *id;
  309. const struct mpc8xxx_gpio_devtype *devtype =
  310. of_device_get_match_data(&pdev->dev);
  311. int ret;
  312. mpc8xxx_gc = devm_kzalloc(&pdev->dev, sizeof(*mpc8xxx_gc), GFP_KERNEL);
  313. if (!mpc8xxx_gc)
  314. return -ENOMEM;
  315. platform_set_drvdata(pdev, mpc8xxx_gc);
  316. raw_spin_lock_init(&mpc8xxx_gc->lock);
  317. mm_gc = &mpc8xxx_gc->mm_gc;
  318. gc = &mm_gc->gc;
  319. mm_gc->save_regs = mpc8xxx_gpio_save_regs;
  320. gc->ngpio = MPC8XXX_GPIO_PINS;
  321. gc->direction_input = mpc8xxx_gpio_dir_in;
  322. if (!devtype)
  323. devtype = &mpc8xxx_gpio_devtype_default;
  324. /*
  325. * It's assumed that only a single type of gpio controller is available
  326. * on the current machine, so overwriting global data is fine.
  327. */
  328. mpc8xxx_irq_chip.irq_set_type = devtype->irq_set_type;
  329. gc->direction_output = devtype->gpio_dir_out ?: mpc8xxx_gpio_dir_out;
  330. gc->get = devtype->gpio_get ?: mpc8xxx_gpio_get;
  331. gc->set = mpc8xxx_gpio_set;
  332. gc->set_multiple = mpc8xxx_gpio_set_multiple;
  333. gc->to_irq = mpc8xxx_gpio_to_irq;
  334. ret = of_mm_gpiochip_add(np, mm_gc);
  335. if (ret)
  336. return ret;
  337. mpc8xxx_gc->irqn = irq_of_parse_and_map(np, 0);
  338. if (mpc8xxx_gc->irqn == NO_IRQ)
  339. return 0;
  340. mpc8xxx_gc->irq = irq_domain_add_linear(np, MPC8XXX_GPIO_PINS,
  341. &mpc8xxx_gpio_irq_ops, mpc8xxx_gc);
  342. if (!mpc8xxx_gc->irq)
  343. return 0;
  344. id = of_match_node(mpc8xxx_gpio_ids, np);
  345. if (id)
  346. mpc8xxx_gc->of_dev_id_data = id->data;
  347. /* ack and mask all irqs */
  348. out_be32(mm_gc->regs + GPIO_IER, 0xffffffff);
  349. out_be32(mm_gc->regs + GPIO_IMR, 0);
  350. irq_set_chained_handler_and_data(mpc8xxx_gc->irqn,
  351. mpc8xxx_gpio_irq_cascade, mpc8xxx_gc);
  352. return 0;
  353. }
  354. static int mpc8xxx_remove(struct platform_device *pdev)
  355. {
  356. struct mpc8xxx_gpio_chip *mpc8xxx_gc = platform_get_drvdata(pdev);
  357. if (mpc8xxx_gc->irq) {
  358. irq_set_chained_handler_and_data(mpc8xxx_gc->irqn, NULL, NULL);
  359. irq_domain_remove(mpc8xxx_gc->irq);
  360. }
  361. of_mm_gpiochip_remove(&mpc8xxx_gc->mm_gc);
  362. return 0;
  363. }
  364. static struct platform_driver mpc8xxx_plat_driver = {
  365. .probe = mpc8xxx_probe,
  366. .remove = mpc8xxx_remove,
  367. .driver = {
  368. .name = "gpio-mpc8xxx",
  369. .of_match_table = mpc8xxx_gpio_ids,
  370. },
  371. };
  372. static int __init mpc8xxx_init(void)
  373. {
  374. return platform_driver_register(&mpc8xxx_plat_driver);
  375. }
  376. arch_initcall(mpc8xxx_init);