gpio-mpc8xxx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * GPIOs on MPC512x/8349/8572/8610/QorIQ and compatible
  4. *
  5. * Copyright (C) 2008 Peter Korsgaard <[email protected]>
  6. * Copyright (C) 2016 Freescale Semiconductor Inc.
  7. */
  8. #include <linux/acpi.h>
  9. #include <linux/kernel.h>
  10. #include <linux/init.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/io.h>
  13. #include <linux/of.h>
  14. #include <linux/of_gpio.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_irq.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/property.h>
  19. #include <linux/mod_devicetable.h>
  20. #include <linux/slab.h>
  21. #include <linux/irq.h>
  22. #include <linux/gpio/driver.h>
  23. #include <linux/bitops.h>
  24. #include <linux/interrupt.h>
  25. #define MPC8XXX_GPIO_PINS 32
  26. #define GPIO_DIR 0x00
  27. #define GPIO_ODR 0x04
  28. #define GPIO_DAT 0x08
  29. #define GPIO_IER 0x0c
  30. #define GPIO_IMR 0x10
  31. #define GPIO_ICR 0x14
  32. #define GPIO_ICR2 0x18
  33. #define GPIO_IBE 0x18
  34. struct mpc8xxx_gpio_chip {
  35. struct gpio_chip gc;
  36. void __iomem *regs;
  37. raw_spinlock_t lock;
  38. int (*direction_output)(struct gpio_chip *chip,
  39. unsigned offset, int value);
  40. struct irq_domain *irq;
  41. int irqn;
  42. };
  43. /*
  44. * This hardware has a big endian bit assignment such that GPIO line 0 is
  45. * connected to bit 31, line 1 to bit 30 ... line 31 to bit 0.
  46. * This inline helper give the right bitmask for a certain line.
  47. */
  48. static inline u32 mpc_pin2mask(unsigned int offset)
  49. {
  50. return BIT(31 - offset);
  51. }
  52. /* Workaround GPIO 1 errata on MPC8572/MPC8536. The status of GPIOs
  53. * defined as output cannot be determined by reading GPDAT register,
  54. * so we use shadow data register instead. The status of input pins
  55. * is determined by reading GPDAT register.
  56. */
  57. static int mpc8572_gpio_get(struct gpio_chip *gc, unsigned int gpio)
  58. {
  59. u32 val;
  60. struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc);
  61. u32 out_mask, out_shadow;
  62. out_mask = gc->read_reg(mpc8xxx_gc->regs + GPIO_DIR);
  63. val = gc->read_reg(mpc8xxx_gc->regs + GPIO_DAT) & ~out_mask;
  64. out_shadow = gc->bgpio_data & out_mask;
  65. return !!((val | out_shadow) & mpc_pin2mask(gpio));
  66. }
  67. static int mpc5121_gpio_dir_out(struct gpio_chip *gc,
  68. unsigned int gpio, int val)
  69. {
  70. struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc);
  71. /* GPIO 28..31 are input only on MPC5121 */
  72. if (gpio >= 28)
  73. return -EINVAL;
  74. return mpc8xxx_gc->direction_output(gc, gpio, val);
  75. }
  76. static int mpc5125_gpio_dir_out(struct gpio_chip *gc,
  77. unsigned int gpio, int val)
  78. {
  79. struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc);
  80. /* GPIO 0..3 are input only on MPC5125 */
  81. if (gpio <= 3)
  82. return -EINVAL;
  83. return mpc8xxx_gc->direction_output(gc, gpio, val);
  84. }
  85. static int mpc8xxx_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
  86. {
  87. struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc);
  88. if (mpc8xxx_gc->irq && offset < MPC8XXX_GPIO_PINS)
  89. return irq_create_mapping(mpc8xxx_gc->irq, offset);
  90. else
  91. return -ENXIO;
  92. }
  93. static irqreturn_t mpc8xxx_gpio_irq_cascade(int irq, void *data)
  94. {
  95. struct mpc8xxx_gpio_chip *mpc8xxx_gc = data;
  96. struct gpio_chip *gc = &mpc8xxx_gc->gc;
  97. unsigned long mask;
  98. int i;
  99. mask = gc->read_reg(mpc8xxx_gc->regs + GPIO_IER)
  100. & gc->read_reg(mpc8xxx_gc->regs + GPIO_IMR);
  101. for_each_set_bit(i, &mask, 32)
  102. generic_handle_domain_irq(mpc8xxx_gc->irq, 31 - i);
  103. return IRQ_HANDLED;
  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. case IRQ_TYPE_LEVEL_LOW:
  142. raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  143. gc->write_reg(mpc8xxx_gc->regs + GPIO_ICR,
  144. gc->read_reg(mpc8xxx_gc->regs + GPIO_ICR)
  145. | mpc_pin2mask(irqd_to_hwirq(d)));
  146. raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  147. break;
  148. case IRQ_TYPE_EDGE_BOTH:
  149. raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  150. gc->write_reg(mpc8xxx_gc->regs + GPIO_ICR,
  151. gc->read_reg(mpc8xxx_gc->regs + GPIO_ICR)
  152. & ~mpc_pin2mask(irqd_to_hwirq(d)));
  153. raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  154. break;
  155. default:
  156. return -EINVAL;
  157. }
  158. return 0;
  159. }
  160. static int mpc512x_irq_set_type(struct irq_data *d, unsigned int flow_type)
  161. {
  162. struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
  163. struct gpio_chip *gc = &mpc8xxx_gc->gc;
  164. unsigned long gpio = irqd_to_hwirq(d);
  165. void __iomem *reg;
  166. unsigned int shift;
  167. unsigned long flags;
  168. if (gpio < 16) {
  169. reg = mpc8xxx_gc->regs + GPIO_ICR;
  170. shift = (15 - gpio) * 2;
  171. } else {
  172. reg = mpc8xxx_gc->regs + GPIO_ICR2;
  173. shift = (15 - (gpio % 16)) * 2;
  174. }
  175. switch (flow_type) {
  176. case IRQ_TYPE_EDGE_FALLING:
  177. case IRQ_TYPE_LEVEL_LOW:
  178. raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  179. gc->write_reg(reg, (gc->read_reg(reg) & ~(3 << shift))
  180. | (2 << shift));
  181. raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  182. break;
  183. case IRQ_TYPE_EDGE_RISING:
  184. case IRQ_TYPE_LEVEL_HIGH:
  185. raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  186. gc->write_reg(reg, (gc->read_reg(reg) & ~(3 << shift))
  187. | (1 << shift));
  188. raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  189. break;
  190. case IRQ_TYPE_EDGE_BOTH:
  191. raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  192. gc->write_reg(reg, (gc->read_reg(reg) & ~(3 << shift)));
  193. raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  194. break;
  195. default:
  196. return -EINVAL;
  197. }
  198. return 0;
  199. }
  200. static struct irq_chip mpc8xxx_irq_chip = {
  201. .name = "mpc8xxx-gpio",
  202. .irq_unmask = mpc8xxx_irq_unmask,
  203. .irq_mask = mpc8xxx_irq_mask,
  204. .irq_ack = mpc8xxx_irq_ack,
  205. /* this might get overwritten in mpc8xxx_probe() */
  206. .irq_set_type = mpc8xxx_irq_set_type,
  207. };
  208. static int mpc8xxx_gpio_irq_map(struct irq_domain *h, unsigned int irq,
  209. irq_hw_number_t hwirq)
  210. {
  211. irq_set_chip_data(irq, h->host_data);
  212. irq_set_chip_and_handler(irq, &mpc8xxx_irq_chip, handle_edge_irq);
  213. return 0;
  214. }
  215. static const struct irq_domain_ops mpc8xxx_gpio_irq_ops = {
  216. .map = mpc8xxx_gpio_irq_map,
  217. .xlate = irq_domain_xlate_twocell,
  218. };
  219. struct mpc8xxx_gpio_devtype {
  220. int (*gpio_dir_out)(struct gpio_chip *, unsigned int, int);
  221. int (*gpio_get)(struct gpio_chip *, unsigned int);
  222. int (*irq_set_type)(struct irq_data *, unsigned int);
  223. };
  224. static const struct mpc8xxx_gpio_devtype mpc512x_gpio_devtype = {
  225. .gpio_dir_out = mpc5121_gpio_dir_out,
  226. .irq_set_type = mpc512x_irq_set_type,
  227. };
  228. static const struct mpc8xxx_gpio_devtype mpc5125_gpio_devtype = {
  229. .gpio_dir_out = mpc5125_gpio_dir_out,
  230. .irq_set_type = mpc512x_irq_set_type,
  231. };
  232. static const struct mpc8xxx_gpio_devtype mpc8572_gpio_devtype = {
  233. .gpio_get = mpc8572_gpio_get,
  234. };
  235. static const struct mpc8xxx_gpio_devtype mpc8xxx_gpio_devtype_default = {
  236. .irq_set_type = mpc8xxx_irq_set_type,
  237. };
  238. static const struct of_device_id mpc8xxx_gpio_ids[] = {
  239. { .compatible = "fsl,mpc8349-gpio", },
  240. { .compatible = "fsl,mpc8572-gpio", .data = &mpc8572_gpio_devtype, },
  241. { .compatible = "fsl,mpc8610-gpio", },
  242. { .compatible = "fsl,mpc5121-gpio", .data = &mpc512x_gpio_devtype, },
  243. { .compatible = "fsl,mpc5125-gpio", .data = &mpc5125_gpio_devtype, },
  244. { .compatible = "fsl,pq3-gpio", },
  245. { .compatible = "fsl,ls1028a-gpio", },
  246. { .compatible = "fsl,ls1088a-gpio", },
  247. { .compatible = "fsl,qoriq-gpio", },
  248. {}
  249. };
  250. static int mpc8xxx_probe(struct platform_device *pdev)
  251. {
  252. struct device_node *np = pdev->dev.of_node;
  253. struct mpc8xxx_gpio_chip *mpc8xxx_gc;
  254. struct gpio_chip *gc;
  255. const struct mpc8xxx_gpio_devtype *devtype = NULL;
  256. struct fwnode_handle *fwnode;
  257. int ret;
  258. mpc8xxx_gc = devm_kzalloc(&pdev->dev, sizeof(*mpc8xxx_gc), GFP_KERNEL);
  259. if (!mpc8xxx_gc)
  260. return -ENOMEM;
  261. platform_set_drvdata(pdev, mpc8xxx_gc);
  262. raw_spin_lock_init(&mpc8xxx_gc->lock);
  263. mpc8xxx_gc->regs = devm_platform_ioremap_resource(pdev, 0);
  264. if (IS_ERR(mpc8xxx_gc->regs))
  265. return PTR_ERR(mpc8xxx_gc->regs);
  266. gc = &mpc8xxx_gc->gc;
  267. gc->parent = &pdev->dev;
  268. if (device_property_read_bool(&pdev->dev, "little-endian")) {
  269. ret = bgpio_init(gc, &pdev->dev, 4,
  270. mpc8xxx_gc->regs + GPIO_DAT,
  271. NULL, NULL,
  272. mpc8xxx_gc->regs + GPIO_DIR, NULL,
  273. BGPIOF_BIG_ENDIAN);
  274. if (ret)
  275. return ret;
  276. dev_dbg(&pdev->dev, "GPIO registers are LITTLE endian\n");
  277. } else {
  278. ret = bgpio_init(gc, &pdev->dev, 4,
  279. mpc8xxx_gc->regs + GPIO_DAT,
  280. NULL, NULL,
  281. mpc8xxx_gc->regs + GPIO_DIR, NULL,
  282. BGPIOF_BIG_ENDIAN
  283. | BGPIOF_BIG_ENDIAN_BYTE_ORDER);
  284. if (ret)
  285. return ret;
  286. dev_dbg(&pdev->dev, "GPIO registers are BIG endian\n");
  287. }
  288. mpc8xxx_gc->direction_output = gc->direction_output;
  289. devtype = device_get_match_data(&pdev->dev);
  290. if (!devtype)
  291. devtype = &mpc8xxx_gpio_devtype_default;
  292. /*
  293. * It's assumed that only a single type of gpio controller is available
  294. * on the current machine, so overwriting global data is fine.
  295. */
  296. if (devtype->irq_set_type)
  297. mpc8xxx_irq_chip.irq_set_type = devtype->irq_set_type;
  298. if (devtype->gpio_dir_out)
  299. gc->direction_output = devtype->gpio_dir_out;
  300. if (devtype->gpio_get)
  301. gc->get = devtype->gpio_get;
  302. gc->to_irq = mpc8xxx_gpio_to_irq;
  303. /*
  304. * The GPIO Input Buffer Enable register(GPIO_IBE) is used to control
  305. * the input enable of each individual GPIO port. When an individual
  306. * GPIO port’s direction is set to input (GPIO_GPDIR[DRn=0]), the
  307. * associated input enable must be set (GPIOxGPIE[IEn]=1) to propagate
  308. * the port value to the GPIO Data Register.
  309. */
  310. fwnode = dev_fwnode(&pdev->dev);
  311. if (of_device_is_compatible(np, "fsl,qoriq-gpio") ||
  312. of_device_is_compatible(np, "fsl,ls1028a-gpio") ||
  313. of_device_is_compatible(np, "fsl,ls1088a-gpio") ||
  314. is_acpi_node(fwnode))
  315. gc->write_reg(mpc8xxx_gc->regs + GPIO_IBE, 0xffffffff);
  316. ret = devm_gpiochip_add_data(&pdev->dev, gc, mpc8xxx_gc);
  317. if (ret) {
  318. dev_err(&pdev->dev,
  319. "GPIO chip registration failed with status %d\n", ret);
  320. return ret;
  321. }
  322. mpc8xxx_gc->irqn = platform_get_irq(pdev, 0);
  323. if (mpc8xxx_gc->irqn < 0)
  324. return mpc8xxx_gc->irqn;
  325. mpc8xxx_gc->irq = irq_domain_create_linear(fwnode,
  326. MPC8XXX_GPIO_PINS,
  327. &mpc8xxx_gpio_irq_ops,
  328. mpc8xxx_gc);
  329. if (!mpc8xxx_gc->irq)
  330. return 0;
  331. /* ack and mask all irqs */
  332. gc->write_reg(mpc8xxx_gc->regs + GPIO_IER, 0xffffffff);
  333. gc->write_reg(mpc8xxx_gc->regs + GPIO_IMR, 0);
  334. ret = devm_request_irq(&pdev->dev, mpc8xxx_gc->irqn,
  335. mpc8xxx_gpio_irq_cascade,
  336. IRQF_NO_THREAD | IRQF_SHARED, "gpio-cascade",
  337. mpc8xxx_gc);
  338. if (ret) {
  339. dev_err(&pdev->dev,
  340. "failed to devm_request_irq(%d), ret = %d\n",
  341. mpc8xxx_gc->irqn, ret);
  342. goto err;
  343. }
  344. return 0;
  345. err:
  346. irq_domain_remove(mpc8xxx_gc->irq);
  347. return ret;
  348. }
  349. static int mpc8xxx_remove(struct platform_device *pdev)
  350. {
  351. struct mpc8xxx_gpio_chip *mpc8xxx_gc = platform_get_drvdata(pdev);
  352. if (mpc8xxx_gc->irq) {
  353. irq_set_chained_handler_and_data(mpc8xxx_gc->irqn, NULL, NULL);
  354. irq_domain_remove(mpc8xxx_gc->irq);
  355. }
  356. return 0;
  357. }
  358. #ifdef CONFIG_ACPI
  359. static const struct acpi_device_id gpio_acpi_ids[] = {
  360. {"NXP0031",},
  361. { }
  362. };
  363. MODULE_DEVICE_TABLE(acpi, gpio_acpi_ids);
  364. #endif
  365. static struct platform_driver mpc8xxx_plat_driver = {
  366. .probe = mpc8xxx_probe,
  367. .remove = mpc8xxx_remove,
  368. .driver = {
  369. .name = "gpio-mpc8xxx",
  370. .of_match_table = mpc8xxx_gpio_ids,
  371. .acpi_match_table = ACPI_PTR(gpio_acpi_ids),
  372. },
  373. };
  374. static int __init mpc8xxx_init(void)
  375. {
  376. return platform_driver_register(&mpc8xxx_plat_driver);
  377. }
  378. arch_initcall(mpc8xxx_init);