gpio-mpc8xxx.c 11 KB

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