gpio-realtek-otto.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/gpio/driver.h>
  3. #include <linux/cpumask.h>
  4. #include <linux/irq.h>
  5. #include <linux/minmax.h>
  6. #include <linux/mod_devicetable.h>
  7. #include <linux/module.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/property.h>
  10. /*
  11. * Total register block size is 0x1C for one bank of four ports (A, B, C, D).
  12. * An optional second bank, with ports E, F, G, and H, may be present, starting
  13. * at register offset 0x1C.
  14. */
  15. /*
  16. * Pin select: (0) "normal", (1) "dedicate peripheral"
  17. * Not used on RTL8380/RTL8390, peripheral selection is managed by control bits
  18. * in the peripheral registers.
  19. */
  20. #define REALTEK_GPIO_REG_CNR 0x00
  21. /* Clear bit (0) for input, set bit (1) for output */
  22. #define REALTEK_GPIO_REG_DIR 0x08
  23. #define REALTEK_GPIO_REG_DATA 0x0C
  24. /* Read bit for IRQ status, write 1 to clear IRQ */
  25. #define REALTEK_GPIO_REG_ISR 0x10
  26. /* Two bits per GPIO in IMR registers */
  27. #define REALTEK_GPIO_REG_IMR 0x14
  28. #define REALTEK_GPIO_REG_IMR_AB 0x14
  29. #define REALTEK_GPIO_REG_IMR_CD 0x18
  30. #define REALTEK_GPIO_IMR_LINE_MASK GENMASK(1, 0)
  31. #define REALTEK_GPIO_IRQ_EDGE_FALLING 1
  32. #define REALTEK_GPIO_IRQ_EDGE_RISING 2
  33. #define REALTEK_GPIO_IRQ_EDGE_BOTH 3
  34. #define REALTEK_GPIO_MAX 32
  35. #define REALTEK_GPIO_PORTS_PER_BANK 4
  36. /**
  37. * realtek_gpio_ctrl - Realtek Otto GPIO driver data
  38. *
  39. * @gc: Associated gpio_chip instance
  40. * @base: Base address of the register block for a GPIO bank
  41. * @lock: Lock for accessing the IRQ registers and values
  42. * @intr_mask: Mask for interrupts lines
  43. * @intr_type: Interrupt type selection
  44. * @bank_read: Read a bank setting as a single 32-bit value
  45. * @bank_write: Write a bank setting as a single 32-bit value
  46. * @imr_line_pos: Bit shift of an IRQ line's IMR value.
  47. *
  48. * The DIR, DATA, and ISR registers consist of four 8-bit port values, packed
  49. * into a single 32-bit register. Use @bank_read (@bank_write) to get (assign)
  50. * a value from (to) these registers. The IMR register consists of four 16-bit
  51. * port values, packed into two 32-bit registers. Use @imr_line_pos to get the
  52. * bit shift of the 2-bit field for a line's IMR settings. Shifts larger than
  53. * 32 overflow into the second register.
  54. *
  55. * Because the interrupt mask register (IMR) combines the function of IRQ type
  56. * selection and masking, two extra values are stored. @intr_mask is used to
  57. * mask/unmask the interrupts for a GPIO line, and @intr_type is used to store
  58. * the selected interrupt types. The logical AND of these values is written to
  59. * IMR on changes.
  60. */
  61. struct realtek_gpio_ctrl {
  62. struct gpio_chip gc;
  63. void __iomem *base;
  64. void __iomem *cpumask_base;
  65. struct cpumask cpu_irq_maskable;
  66. raw_spinlock_t lock;
  67. u8 intr_mask[REALTEK_GPIO_MAX];
  68. u8 intr_type[REALTEK_GPIO_MAX];
  69. u32 (*bank_read)(void __iomem *reg);
  70. void (*bank_write)(void __iomem *reg, u32 value);
  71. unsigned int (*line_imr_pos)(unsigned int line);
  72. };
  73. /* Expand with more flags as devices with other quirks are added */
  74. enum realtek_gpio_flags {
  75. /*
  76. * Allow disabling interrupts, for cases where the port order is
  77. * unknown. This may result in a port mismatch between ISR and IMR.
  78. * An interrupt would appear to come from a different line than the
  79. * line the IRQ handler was assigned to, causing uncaught interrupts.
  80. */
  81. GPIO_INTERRUPTS_DISABLED = BIT(0),
  82. /*
  83. * Port order is reversed, meaning DCBA register layout for 1-bit
  84. * fields, and [BA, DC] for 2-bit fields.
  85. */
  86. GPIO_PORTS_REVERSED = BIT(1),
  87. /*
  88. * Interrupts can be enabled per cpu. This requires a secondary IO
  89. * range, where the per-cpu enable masks are located.
  90. */
  91. GPIO_INTERRUPTS_PER_CPU = BIT(2),
  92. };
  93. static struct realtek_gpio_ctrl *irq_data_to_ctrl(struct irq_data *data)
  94. {
  95. struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
  96. return container_of(gc, struct realtek_gpio_ctrl, gc);
  97. }
  98. /*
  99. * Normal port order register access
  100. *
  101. * Port information is stored with the first port at offset 0, followed by the
  102. * second, etc. Most registers store one bit per GPIO and use a u8 value per
  103. * port. The two interrupt mask registers store two bits per GPIO, so use u16
  104. * values.
  105. */
  106. static u32 realtek_gpio_bank_read_swapped(void __iomem *reg)
  107. {
  108. return ioread32be(reg);
  109. }
  110. static void realtek_gpio_bank_write_swapped(void __iomem *reg, u32 value)
  111. {
  112. iowrite32be(value, reg);
  113. }
  114. static unsigned int realtek_gpio_line_imr_pos_swapped(unsigned int line)
  115. {
  116. unsigned int port_pin = line % 8;
  117. unsigned int port = line / 8;
  118. return 2 * (8 * (port ^ 1) + port_pin);
  119. }
  120. /*
  121. * Reversed port order register access
  122. *
  123. * For registers with one bit per GPIO, all ports are stored as u8-s in one
  124. * register in reversed order. The two interrupt mask registers store two bits
  125. * per GPIO, so use u16 values. The first register contains ports 1 and 0, the
  126. * second ports 3 and 2.
  127. */
  128. static u32 realtek_gpio_bank_read(void __iomem *reg)
  129. {
  130. return ioread32(reg);
  131. }
  132. static void realtek_gpio_bank_write(void __iomem *reg, u32 value)
  133. {
  134. iowrite32(value, reg);
  135. }
  136. static unsigned int realtek_gpio_line_imr_pos(unsigned int line)
  137. {
  138. return 2 * line;
  139. }
  140. static void realtek_gpio_clear_isr(struct realtek_gpio_ctrl *ctrl, u32 mask)
  141. {
  142. ctrl->bank_write(ctrl->base + REALTEK_GPIO_REG_ISR, mask);
  143. }
  144. static u32 realtek_gpio_read_isr(struct realtek_gpio_ctrl *ctrl)
  145. {
  146. return ctrl->bank_read(ctrl->base + REALTEK_GPIO_REG_ISR);
  147. }
  148. /* Set the rising and falling edge mask bits for a GPIO pin */
  149. static void realtek_gpio_update_line_imr(struct realtek_gpio_ctrl *ctrl, unsigned int line)
  150. {
  151. void __iomem *reg = ctrl->base + REALTEK_GPIO_REG_IMR;
  152. unsigned int line_shift = ctrl->line_imr_pos(line);
  153. unsigned int shift = line_shift % 32;
  154. u32 irq_type = ctrl->intr_type[line];
  155. u32 irq_mask = ctrl->intr_mask[line];
  156. u32 reg_val;
  157. reg += 4 * (line_shift / 32);
  158. reg_val = ioread32(reg);
  159. reg_val &= ~(REALTEK_GPIO_IMR_LINE_MASK << shift);
  160. reg_val |= (irq_type & irq_mask & REALTEK_GPIO_IMR_LINE_MASK) << shift;
  161. iowrite32(reg_val, reg);
  162. }
  163. static void realtek_gpio_irq_ack(struct irq_data *data)
  164. {
  165. struct realtek_gpio_ctrl *ctrl = irq_data_to_ctrl(data);
  166. irq_hw_number_t line = irqd_to_hwirq(data);
  167. realtek_gpio_clear_isr(ctrl, BIT(line));
  168. }
  169. static void realtek_gpio_irq_unmask(struct irq_data *data)
  170. {
  171. struct realtek_gpio_ctrl *ctrl = irq_data_to_ctrl(data);
  172. unsigned int line = irqd_to_hwirq(data);
  173. unsigned long flags;
  174. gpiochip_enable_irq(&ctrl->gc, line);
  175. raw_spin_lock_irqsave(&ctrl->lock, flags);
  176. ctrl->intr_mask[line] = REALTEK_GPIO_IMR_LINE_MASK;
  177. realtek_gpio_update_line_imr(ctrl, line);
  178. raw_spin_unlock_irqrestore(&ctrl->lock, flags);
  179. }
  180. static void realtek_gpio_irq_mask(struct irq_data *data)
  181. {
  182. struct realtek_gpio_ctrl *ctrl = irq_data_to_ctrl(data);
  183. unsigned int line = irqd_to_hwirq(data);
  184. unsigned long flags;
  185. raw_spin_lock_irqsave(&ctrl->lock, flags);
  186. ctrl->intr_mask[line] = 0;
  187. realtek_gpio_update_line_imr(ctrl, line);
  188. raw_spin_unlock_irqrestore(&ctrl->lock, flags);
  189. gpiochip_disable_irq(&ctrl->gc, line);
  190. }
  191. static int realtek_gpio_irq_set_type(struct irq_data *data, unsigned int flow_type)
  192. {
  193. struct realtek_gpio_ctrl *ctrl = irq_data_to_ctrl(data);
  194. unsigned int line = irqd_to_hwirq(data);
  195. unsigned long flags;
  196. u8 type;
  197. switch (flow_type & IRQ_TYPE_SENSE_MASK) {
  198. case IRQ_TYPE_EDGE_FALLING:
  199. type = REALTEK_GPIO_IRQ_EDGE_FALLING;
  200. break;
  201. case IRQ_TYPE_EDGE_RISING:
  202. type = REALTEK_GPIO_IRQ_EDGE_RISING;
  203. break;
  204. case IRQ_TYPE_EDGE_BOTH:
  205. type = REALTEK_GPIO_IRQ_EDGE_BOTH;
  206. break;
  207. default:
  208. return -EINVAL;
  209. }
  210. irq_set_handler_locked(data, handle_edge_irq);
  211. raw_spin_lock_irqsave(&ctrl->lock, flags);
  212. ctrl->intr_type[line] = type;
  213. realtek_gpio_update_line_imr(ctrl, line);
  214. raw_spin_unlock_irqrestore(&ctrl->lock, flags);
  215. return 0;
  216. }
  217. static void realtek_gpio_irq_handler(struct irq_desc *desc)
  218. {
  219. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  220. struct realtek_gpio_ctrl *ctrl = gpiochip_get_data(gc);
  221. struct irq_chip *irq_chip = irq_desc_get_chip(desc);
  222. unsigned long status;
  223. int offset;
  224. chained_irq_enter(irq_chip, desc);
  225. status = realtek_gpio_read_isr(ctrl);
  226. for_each_set_bit(offset, &status, gc->ngpio)
  227. generic_handle_domain_irq(gc->irq.domain, offset);
  228. chained_irq_exit(irq_chip, desc);
  229. }
  230. static inline void __iomem *realtek_gpio_irq_cpu_mask(struct realtek_gpio_ctrl *ctrl, int cpu)
  231. {
  232. return ctrl->cpumask_base + REALTEK_GPIO_PORTS_PER_BANK * cpu;
  233. }
  234. static int realtek_gpio_irq_set_affinity(struct irq_data *data,
  235. const struct cpumask *dest, bool force)
  236. {
  237. struct realtek_gpio_ctrl *ctrl = irq_data_to_ctrl(data);
  238. unsigned int line = irqd_to_hwirq(data);
  239. void __iomem *irq_cpu_mask;
  240. unsigned long flags;
  241. int cpu;
  242. u32 v;
  243. if (!ctrl->cpumask_base)
  244. return -ENXIO;
  245. raw_spin_lock_irqsave(&ctrl->lock, flags);
  246. for_each_cpu(cpu, &ctrl->cpu_irq_maskable) {
  247. irq_cpu_mask = realtek_gpio_irq_cpu_mask(ctrl, cpu);
  248. v = ctrl->bank_read(irq_cpu_mask);
  249. if (cpumask_test_cpu(cpu, dest))
  250. v |= BIT(line);
  251. else
  252. v &= ~BIT(line);
  253. ctrl->bank_write(irq_cpu_mask, v);
  254. }
  255. raw_spin_unlock_irqrestore(&ctrl->lock, flags);
  256. irq_data_update_effective_affinity(data, dest);
  257. return 0;
  258. }
  259. static int realtek_gpio_irq_init(struct gpio_chip *gc)
  260. {
  261. struct realtek_gpio_ctrl *ctrl = gpiochip_get_data(gc);
  262. u32 mask_all = GENMASK(gc->ngpio - 1, 0);
  263. unsigned int line;
  264. int cpu;
  265. for (line = 0; line < gc->ngpio; line++)
  266. realtek_gpio_update_line_imr(ctrl, line);
  267. realtek_gpio_clear_isr(ctrl, mask_all);
  268. for_each_cpu(cpu, &ctrl->cpu_irq_maskable)
  269. ctrl->bank_write(realtek_gpio_irq_cpu_mask(ctrl, cpu), mask_all);
  270. return 0;
  271. }
  272. static const struct irq_chip realtek_gpio_irq_chip = {
  273. .name = "realtek-otto-gpio",
  274. .irq_ack = realtek_gpio_irq_ack,
  275. .irq_mask = realtek_gpio_irq_mask,
  276. .irq_unmask = realtek_gpio_irq_unmask,
  277. .irq_set_type = realtek_gpio_irq_set_type,
  278. .irq_set_affinity = realtek_gpio_irq_set_affinity,
  279. .flags = IRQCHIP_IMMUTABLE,
  280. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  281. };
  282. static const struct of_device_id realtek_gpio_of_match[] = {
  283. {
  284. .compatible = "realtek,otto-gpio",
  285. .data = (void *)GPIO_INTERRUPTS_DISABLED,
  286. },
  287. {
  288. .compatible = "realtek,rtl8380-gpio",
  289. },
  290. {
  291. .compatible = "realtek,rtl8390-gpio",
  292. },
  293. {
  294. .compatible = "realtek,rtl9300-gpio",
  295. .data = (void *)(GPIO_PORTS_REVERSED | GPIO_INTERRUPTS_PER_CPU)
  296. },
  297. {
  298. .compatible = "realtek,rtl9310-gpio",
  299. },
  300. {}
  301. };
  302. MODULE_DEVICE_TABLE(of, realtek_gpio_of_match);
  303. static int realtek_gpio_probe(struct platform_device *pdev)
  304. {
  305. struct device *dev = &pdev->dev;
  306. unsigned long bgpio_flags;
  307. unsigned int dev_flags;
  308. struct gpio_irq_chip *girq;
  309. struct realtek_gpio_ctrl *ctrl;
  310. struct resource *res;
  311. u32 ngpios;
  312. unsigned int nr_cpus;
  313. int cpu, err, irq;
  314. ctrl = devm_kzalloc(dev, sizeof(*ctrl), GFP_KERNEL);
  315. if (!ctrl)
  316. return -ENOMEM;
  317. dev_flags = (unsigned int) device_get_match_data(dev);
  318. ngpios = REALTEK_GPIO_MAX;
  319. device_property_read_u32(dev, "ngpios", &ngpios);
  320. if (ngpios > REALTEK_GPIO_MAX) {
  321. dev_err(&pdev->dev, "invalid ngpios (max. %d)\n",
  322. REALTEK_GPIO_MAX);
  323. return -EINVAL;
  324. }
  325. ctrl->base = devm_platform_ioremap_resource(pdev, 0);
  326. if (IS_ERR(ctrl->base))
  327. return PTR_ERR(ctrl->base);
  328. raw_spin_lock_init(&ctrl->lock);
  329. if (dev_flags & GPIO_PORTS_REVERSED) {
  330. bgpio_flags = 0;
  331. ctrl->bank_read = realtek_gpio_bank_read;
  332. ctrl->bank_write = realtek_gpio_bank_write;
  333. ctrl->line_imr_pos = realtek_gpio_line_imr_pos;
  334. } else {
  335. bgpio_flags = BGPIOF_BIG_ENDIAN_BYTE_ORDER;
  336. ctrl->bank_read = realtek_gpio_bank_read_swapped;
  337. ctrl->bank_write = realtek_gpio_bank_write_swapped;
  338. ctrl->line_imr_pos = realtek_gpio_line_imr_pos_swapped;
  339. }
  340. err = bgpio_init(&ctrl->gc, dev, 4,
  341. ctrl->base + REALTEK_GPIO_REG_DATA, NULL, NULL,
  342. ctrl->base + REALTEK_GPIO_REG_DIR, NULL,
  343. bgpio_flags);
  344. if (err) {
  345. dev_err(dev, "unable to init generic GPIO");
  346. return err;
  347. }
  348. ctrl->gc.ngpio = ngpios;
  349. ctrl->gc.owner = THIS_MODULE;
  350. irq = platform_get_irq_optional(pdev, 0);
  351. if (!(dev_flags & GPIO_INTERRUPTS_DISABLED) && irq > 0) {
  352. girq = &ctrl->gc.irq;
  353. gpio_irq_chip_set_chip(girq, &realtek_gpio_irq_chip);
  354. girq->default_type = IRQ_TYPE_NONE;
  355. girq->handler = handle_bad_irq;
  356. girq->parent_handler = realtek_gpio_irq_handler;
  357. girq->num_parents = 1;
  358. girq->parents = devm_kcalloc(dev, girq->num_parents,
  359. sizeof(*girq->parents), GFP_KERNEL);
  360. if (!girq->parents)
  361. return -ENOMEM;
  362. girq->parents[0] = irq;
  363. girq->init_hw = realtek_gpio_irq_init;
  364. }
  365. cpumask_clear(&ctrl->cpu_irq_maskable);
  366. if ((dev_flags & GPIO_INTERRUPTS_PER_CPU) && irq > 0) {
  367. ctrl->cpumask_base = devm_platform_get_and_ioremap_resource(pdev, 1, &res);
  368. if (IS_ERR(ctrl->cpumask_base))
  369. return dev_err_probe(dev, PTR_ERR(ctrl->cpumask_base),
  370. "missing CPU IRQ mask registers");
  371. nr_cpus = resource_size(res) / REALTEK_GPIO_PORTS_PER_BANK;
  372. nr_cpus = min(nr_cpus, num_present_cpus());
  373. for (cpu = 0; cpu < nr_cpus; cpu++)
  374. cpumask_set_cpu(cpu, &ctrl->cpu_irq_maskable);
  375. }
  376. return devm_gpiochip_add_data(dev, &ctrl->gc, ctrl);
  377. }
  378. static struct platform_driver realtek_gpio_driver = {
  379. .driver = {
  380. .name = "realtek-otto-gpio",
  381. .of_match_table = realtek_gpio_of_match,
  382. },
  383. .probe = realtek_gpio_probe,
  384. };
  385. module_platform_driver(realtek_gpio_driver);
  386. MODULE_DESCRIPTION("Realtek Otto GPIO support");
  387. MODULE_AUTHOR("Sander Vanheule <[email protected]>");
  388. MODULE_LICENSE("GPL v2");