gpio-mpc8xxx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * GPIOs on MPC512x/8349/8572/8610/QorIQ and compatible
  3. *
  4. * Copyright (C) 2008 Peter Korsgaard <jacmet@sunsite.dk>
  5. * Copyright (C) 2016 Freescale Semiconductor Inc.
  6. *
  7. * This file is licensed under the terms of the GNU General Public License
  8. * version 2. This program is licensed "as is" without any warranty of any
  9. * kind, whether express or implied.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/io.h>
  15. #include <linux/of.h>
  16. #include <linux/of_gpio.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/of_platform.h>
  20. #include <linux/slab.h>
  21. #include <linux/irq.h>
  22. #include <linux/gpio/driver.h>
  23. #define MPC8XXX_GPIO_PINS 32
  24. #define GPIO_DIR 0x00
  25. #define GPIO_ODR 0x04
  26. #define GPIO_DAT 0x08
  27. #define GPIO_IER 0x0c
  28. #define GPIO_IMR 0x10
  29. #define GPIO_ICR 0x14
  30. #define GPIO_ICR2 0x18
  31. struct mpc8xxx_gpio_chip {
  32. struct gpio_chip gc;
  33. void __iomem *regs;
  34. raw_spinlock_t lock;
  35. int (*direction_output)(struct gpio_chip *chip,
  36. unsigned offset, int value);
  37. struct irq_domain *irq;
  38. unsigned int irqn;
  39. };
  40. /* Workaround GPIO 1 errata on MPC8572/MPC8536. The status of GPIOs
  41. * defined as output cannot be determined by reading GPDAT register,
  42. * so we use shadow data register instead. The status of input pins
  43. * is determined by reading GPDAT register.
  44. */
  45. static int mpc8572_gpio_get(struct gpio_chip *gc, unsigned int gpio)
  46. {
  47. u32 val;
  48. struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc);
  49. u32 out_mask, out_shadow;
  50. out_mask = gc->read_reg(mpc8xxx_gc->regs + GPIO_DIR);
  51. val = gc->read_reg(mpc8xxx_gc->regs + GPIO_DAT) & ~out_mask;
  52. out_shadow = gc->bgpio_data & out_mask;
  53. return !!((val | out_shadow) & gc->pin2mask(gc, gpio));
  54. }
  55. static int mpc5121_gpio_dir_out(struct gpio_chip *gc,
  56. unsigned int gpio, int val)
  57. {
  58. struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc);
  59. /* GPIO 28..31 are input only on MPC5121 */
  60. if (gpio >= 28)
  61. return -EINVAL;
  62. return mpc8xxx_gc->direction_output(gc, gpio, val);
  63. }
  64. static int mpc5125_gpio_dir_out(struct gpio_chip *gc,
  65. unsigned int gpio, int val)
  66. {
  67. struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc);
  68. /* GPIO 0..3 are input only on MPC5125 */
  69. if (gpio <= 3)
  70. return -EINVAL;
  71. return mpc8xxx_gc->direction_output(gc, gpio, val);
  72. }
  73. static int mpc8xxx_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
  74. {
  75. struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc);
  76. if (mpc8xxx_gc->irq && offset < MPC8XXX_GPIO_PINS)
  77. return irq_create_mapping(mpc8xxx_gc->irq, offset);
  78. else
  79. return -ENXIO;
  80. }
  81. static void mpc8xxx_gpio_irq_cascade(struct irq_desc *desc)
  82. {
  83. struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_desc_get_handler_data(desc);
  84. struct irq_chip *chip = irq_desc_get_chip(desc);
  85. struct gpio_chip *gc = &mpc8xxx_gc->gc;
  86. unsigned int mask;
  87. mask = gc->read_reg(mpc8xxx_gc->regs + GPIO_IER)
  88. & gc->read_reg(mpc8xxx_gc->regs + GPIO_IMR);
  89. if (mask)
  90. generic_handle_irq(irq_linear_revmap(mpc8xxx_gc->irq,
  91. 32 - ffs(mask)));
  92. if (chip->irq_eoi)
  93. chip->irq_eoi(&desc->irq_data);
  94. }
  95. static void mpc8xxx_irq_unmask(struct irq_data *d)
  96. {
  97. struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
  98. struct gpio_chip *gc = &mpc8xxx_gc->gc;
  99. unsigned long flags;
  100. raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  101. gc->write_reg(mpc8xxx_gc->regs + GPIO_IMR,
  102. gc->read_reg(mpc8xxx_gc->regs + GPIO_IMR)
  103. | gc->pin2mask(gc, irqd_to_hwirq(d)));
  104. raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  105. }
  106. static void mpc8xxx_irq_mask(struct irq_data *d)
  107. {
  108. struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
  109. struct gpio_chip *gc = &mpc8xxx_gc->gc;
  110. unsigned long flags;
  111. raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  112. gc->write_reg(mpc8xxx_gc->regs + GPIO_IMR,
  113. gc->read_reg(mpc8xxx_gc->regs + GPIO_IMR)
  114. & ~(gc->pin2mask(gc, irqd_to_hwirq(d))));
  115. raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  116. }
  117. static void mpc8xxx_irq_ack(struct irq_data *d)
  118. {
  119. struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
  120. struct gpio_chip *gc = &mpc8xxx_gc->gc;
  121. gc->write_reg(mpc8xxx_gc->regs + GPIO_IER,
  122. gc->pin2mask(gc, irqd_to_hwirq(d)));
  123. }
  124. static int mpc8xxx_irq_set_type(struct irq_data *d, unsigned int flow_type)
  125. {
  126. struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
  127. struct gpio_chip *gc = &mpc8xxx_gc->gc;
  128. unsigned long flags;
  129. switch (flow_type) {
  130. case IRQ_TYPE_EDGE_FALLING:
  131. raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  132. gc->write_reg(mpc8xxx_gc->regs + GPIO_ICR,
  133. gc->read_reg(mpc8xxx_gc->regs + GPIO_ICR)
  134. | gc->pin2mask(gc, irqd_to_hwirq(d)));
  135. raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  136. break;
  137. case IRQ_TYPE_EDGE_BOTH:
  138. raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  139. gc->write_reg(mpc8xxx_gc->regs + GPIO_ICR,
  140. gc->read_reg(mpc8xxx_gc->regs + GPIO_ICR)
  141. & ~(gc->pin2mask(gc, irqd_to_hwirq(d))));
  142. raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  143. break;
  144. default:
  145. return -EINVAL;
  146. }
  147. return 0;
  148. }
  149. static int mpc512x_irq_set_type(struct irq_data *d, unsigned int flow_type)
  150. {
  151. struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
  152. struct gpio_chip *gc = &mpc8xxx_gc->gc;
  153. unsigned long gpio = irqd_to_hwirq(d);
  154. void __iomem *reg;
  155. unsigned int shift;
  156. unsigned long flags;
  157. if (gpio < 16) {
  158. reg = mpc8xxx_gc->regs + GPIO_ICR;
  159. shift = (15 - gpio) * 2;
  160. } else {
  161. reg = mpc8xxx_gc->regs + GPIO_ICR2;
  162. shift = (15 - (gpio % 16)) * 2;
  163. }
  164. switch (flow_type) {
  165. case IRQ_TYPE_EDGE_FALLING:
  166. case IRQ_TYPE_LEVEL_LOW:
  167. raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  168. gc->write_reg(reg, (gc->read_reg(reg) & ~(3 << shift))
  169. | (2 << shift));
  170. raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  171. break;
  172. case IRQ_TYPE_EDGE_RISING:
  173. case IRQ_TYPE_LEVEL_HIGH:
  174. raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  175. gc->write_reg(reg, (gc->read_reg(reg) & ~(3 << shift))
  176. | (1 << shift));
  177. raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  178. break;
  179. case IRQ_TYPE_EDGE_BOTH:
  180. raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  181. gc->write_reg(reg, (gc->read_reg(reg) & ~(3 << shift)));
  182. raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  183. break;
  184. default:
  185. return -EINVAL;
  186. }
  187. return 0;
  188. }
  189. static struct irq_chip mpc8xxx_irq_chip = {
  190. .name = "mpc8xxx-gpio",
  191. .irq_unmask = mpc8xxx_irq_unmask,
  192. .irq_mask = mpc8xxx_irq_mask,
  193. .irq_ack = mpc8xxx_irq_ack,
  194. /* this might get overwritten in mpc8xxx_probe() */
  195. .irq_set_type = mpc8xxx_irq_set_type,
  196. };
  197. static int mpc8xxx_gpio_irq_map(struct irq_domain *h, unsigned int irq,
  198. irq_hw_number_t hwirq)
  199. {
  200. irq_set_chip_data(irq, h->host_data);
  201. irq_set_chip_and_handler(irq, &mpc8xxx_irq_chip, handle_edge_irq);
  202. return 0;
  203. }
  204. static const struct irq_domain_ops mpc8xxx_gpio_irq_ops = {
  205. .map = mpc8xxx_gpio_irq_map,
  206. .xlate = irq_domain_xlate_twocell,
  207. };
  208. struct mpc8xxx_gpio_devtype {
  209. int (*gpio_dir_out)(struct gpio_chip *, unsigned int, int);
  210. int (*gpio_get)(struct gpio_chip *, unsigned int);
  211. int (*irq_set_type)(struct irq_data *, unsigned int);
  212. };
  213. static const struct mpc8xxx_gpio_devtype mpc512x_gpio_devtype = {
  214. .gpio_dir_out = mpc5121_gpio_dir_out,
  215. .irq_set_type = mpc512x_irq_set_type,
  216. };
  217. static const struct mpc8xxx_gpio_devtype mpc5125_gpio_devtype = {
  218. .gpio_dir_out = mpc5125_gpio_dir_out,
  219. .irq_set_type = mpc512x_irq_set_type,
  220. };
  221. static const struct mpc8xxx_gpio_devtype mpc8572_gpio_devtype = {
  222. .gpio_get = mpc8572_gpio_get,
  223. };
  224. static const struct mpc8xxx_gpio_devtype mpc8xxx_gpio_devtype_default = {
  225. .irq_set_type = mpc8xxx_irq_set_type,
  226. };
  227. static const struct of_device_id mpc8xxx_gpio_ids[] = {
  228. { .compatible = "fsl,mpc8349-gpio", },
  229. { .compatible = "fsl,mpc8572-gpio", .data = &mpc8572_gpio_devtype, },
  230. { .compatible = "fsl,mpc8610-gpio", },
  231. { .compatible = "fsl,mpc5121-gpio", .data = &mpc512x_gpio_devtype, },
  232. { .compatible = "fsl,mpc5125-gpio", .data = &mpc5125_gpio_devtype, },
  233. { .compatible = "fsl,pq3-gpio", },
  234. { .compatible = "fsl,qoriq-gpio", },
  235. {}
  236. };
  237. static int mpc8xxx_probe(struct platform_device *pdev)
  238. {
  239. struct device_node *np = pdev->dev.of_node;
  240. struct mpc8xxx_gpio_chip *mpc8xxx_gc;
  241. struct gpio_chip *gc;
  242. const struct mpc8xxx_gpio_devtype *devtype =
  243. of_device_get_match_data(&pdev->dev);
  244. int ret;
  245. mpc8xxx_gc = devm_kzalloc(&pdev->dev, sizeof(*mpc8xxx_gc), GFP_KERNEL);
  246. if (!mpc8xxx_gc)
  247. return -ENOMEM;
  248. platform_set_drvdata(pdev, mpc8xxx_gc);
  249. raw_spin_lock_init(&mpc8xxx_gc->lock);
  250. mpc8xxx_gc->regs = of_iomap(np, 0);
  251. if (!mpc8xxx_gc->regs)
  252. return -ENOMEM;
  253. gc = &mpc8xxx_gc->gc;
  254. if (of_property_read_bool(np, "little-endian")) {
  255. ret = bgpio_init(gc, &pdev->dev, 4,
  256. mpc8xxx_gc->regs + GPIO_DAT,
  257. NULL, NULL,
  258. mpc8xxx_gc->regs + GPIO_DIR, NULL,
  259. BGPIOF_BIG_ENDIAN);
  260. if (ret)
  261. goto err;
  262. dev_dbg(&pdev->dev, "GPIO registers are LITTLE endian\n");
  263. } else {
  264. ret = bgpio_init(gc, &pdev->dev, 4,
  265. mpc8xxx_gc->regs + GPIO_DAT,
  266. NULL, NULL,
  267. mpc8xxx_gc->regs + GPIO_DIR, NULL,
  268. BGPIOF_BIG_ENDIAN
  269. | BGPIOF_BIG_ENDIAN_BYTE_ORDER);
  270. if (ret)
  271. goto err;
  272. dev_dbg(&pdev->dev, "GPIO registers are BIG endian\n");
  273. }
  274. mpc8xxx_gc->direction_output = gc->direction_output;
  275. if (!devtype)
  276. devtype = &mpc8xxx_gpio_devtype_default;
  277. /*
  278. * It's assumed that only a single type of gpio controller is available
  279. * on the current machine, so overwriting global data is fine.
  280. */
  281. mpc8xxx_irq_chip.irq_set_type = devtype->irq_set_type;
  282. if (devtype->gpio_dir_out)
  283. gc->direction_output = devtype->gpio_dir_out;
  284. if (devtype->gpio_get)
  285. gc->get = devtype->gpio_get;
  286. gc->to_irq = mpc8xxx_gpio_to_irq;
  287. ret = gpiochip_add_data(gc, mpc8xxx_gc);
  288. if (ret) {
  289. pr_err("%s: GPIO chip registration failed with status %d\n",
  290. np->full_name, ret);
  291. goto err;
  292. }
  293. mpc8xxx_gc->irqn = irq_of_parse_and_map(np, 0);
  294. if (!mpc8xxx_gc->irqn)
  295. return 0;
  296. mpc8xxx_gc->irq = irq_domain_add_linear(np, MPC8XXX_GPIO_PINS,
  297. &mpc8xxx_gpio_irq_ops, mpc8xxx_gc);
  298. if (!mpc8xxx_gc->irq)
  299. return 0;
  300. /* ack and mask all irqs */
  301. gc->write_reg(mpc8xxx_gc->regs + GPIO_IER, 0xffffffff);
  302. gc->write_reg(mpc8xxx_gc->regs + GPIO_IMR, 0);
  303. irq_set_chained_handler_and_data(mpc8xxx_gc->irqn,
  304. mpc8xxx_gpio_irq_cascade, mpc8xxx_gc);
  305. return 0;
  306. err:
  307. iounmap(mpc8xxx_gc->regs);
  308. return ret;
  309. }
  310. static int mpc8xxx_remove(struct platform_device *pdev)
  311. {
  312. struct mpc8xxx_gpio_chip *mpc8xxx_gc = platform_get_drvdata(pdev);
  313. if (mpc8xxx_gc->irq) {
  314. irq_set_chained_handler_and_data(mpc8xxx_gc->irqn, NULL, NULL);
  315. irq_domain_remove(mpc8xxx_gc->irq);
  316. }
  317. gpiochip_remove(&mpc8xxx_gc->gc);
  318. iounmap(mpc8xxx_gc->regs);
  319. return 0;
  320. }
  321. static struct platform_driver mpc8xxx_plat_driver = {
  322. .probe = mpc8xxx_probe,
  323. .remove = mpc8xxx_remove,
  324. .driver = {
  325. .name = "gpio-mpc8xxx",
  326. .of_match_table = mpc8xxx_gpio_ids,
  327. },
  328. };
  329. static int __init mpc8xxx_init(void)
  330. {
  331. return platform_driver_register(&mpc8xxx_plat_driver);
  332. }
  333. arch_initcall(mpc8xxx_init);