1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2012-2015, 2017, 2021, The Linux Foundation. All rights reserved.
5 #include <linux/bitmap.h>
6 #include <linux/delay.h>
8 #include <linux/interrupt.h>
10 #include <linux/irqchip/chained_irq.h>
11 #include <linux/irqdomain.h>
12 #include <linux/irq.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
18 #include <linux/spmi.h>
20 /* PMIC Arbiter configuration registers */
21 #define PMIC_ARB_VERSION 0x0000
22 #define PMIC_ARB_VERSION_V2_MIN 0x20010000
23 #define PMIC_ARB_VERSION_V3_MIN 0x30000000
24 #define PMIC_ARB_VERSION_V5_MIN 0x50000000
25 #define PMIC_ARB_VERSION_V7_MIN 0x70000000
26 #define PMIC_ARB_INT_EN 0x0004
28 #define PMIC_ARB_FEATURES 0x0004
29 #define PMIC_ARB_FEATURES_PERIPH_MASK GENMASK(10, 0)
31 #define PMIC_ARB_FEATURES1 0x0008
33 /* PMIC Arbiter channel registers offsets */
34 #define PMIC_ARB_CMD 0x00
35 #define PMIC_ARB_CONFIG 0x04
36 #define PMIC_ARB_STATUS 0x08
37 #define PMIC_ARB_WDATA0 0x10
38 #define PMIC_ARB_WDATA1 0x14
39 #define PMIC_ARB_RDATA0 0x18
40 #define PMIC_ARB_RDATA1 0x1C
43 #define SPMI_MAPPING_TABLE_REG(N) (0x0B00 + (4 * (N)))
44 #define SPMI_MAPPING_BIT_INDEX(X) (((X) >> 18) & 0xF)
45 #define SPMI_MAPPING_BIT_IS_0_FLAG(X) (((X) >> 17) & 0x1)
46 #define SPMI_MAPPING_BIT_IS_0_RESULT(X) (((X) >> 9) & 0xFF)
47 #define SPMI_MAPPING_BIT_IS_1_FLAG(X) (((X) >> 8) & 0x1)
48 #define SPMI_MAPPING_BIT_IS_1_RESULT(X) (((X) >> 0) & 0xFF)
50 #define SPMI_MAPPING_TABLE_TREE_DEPTH 16 /* Maximum of 16-bits */
51 #define PMIC_ARB_MAX_PPID BIT(12) /* PPID is 12bit */
52 #define PMIC_ARB_APID_VALID BIT(15)
53 #define PMIC_ARB_CHAN_IS_IRQ_OWNER(reg) ((reg) & BIT(24))
54 #define INVALID_EE 0xFF
57 #define SPMI_OWNERSHIP_PERIPH2OWNER(X) ((X) & 0x7)
59 /* Channel Status fields */
60 enum pmic_arb_chnl_status {
61 PMIC_ARB_STATUS_DONE = BIT(0),
62 PMIC_ARB_STATUS_FAILURE = BIT(1),
63 PMIC_ARB_STATUS_DENIED = BIT(2),
64 PMIC_ARB_STATUS_DROPPED = BIT(3),
67 /* Command register fields */
68 #define PMIC_ARB_CMD_MAX_BYTE_COUNT 8
71 enum pmic_arb_cmd_op_code {
72 PMIC_ARB_OP_EXT_WRITEL = 0,
73 PMIC_ARB_OP_EXT_READL = 1,
74 PMIC_ARB_OP_EXT_WRITE = 2,
75 PMIC_ARB_OP_RESET = 3,
76 PMIC_ARB_OP_SLEEP = 4,
77 PMIC_ARB_OP_SHUTDOWN = 5,
78 PMIC_ARB_OP_WAKEUP = 6,
79 PMIC_ARB_OP_AUTHENTICATE = 7,
80 PMIC_ARB_OP_MSTR_READ = 8,
81 PMIC_ARB_OP_MSTR_WRITE = 9,
82 PMIC_ARB_OP_EXT_READ = 13,
83 PMIC_ARB_OP_WRITE = 14,
84 PMIC_ARB_OP_READ = 15,
85 PMIC_ARB_OP_ZERO_WRITE = 16,
89 * PMIC arbiter version 5 uses different register offsets for read/write vs
92 enum pmic_arb_channel {
97 /* Maximum number of support PMIC peripherals */
98 #define PMIC_ARB_MAX_PERIPHS 512
99 #define PMIC_ARB_MAX_PERIPHS_V7 1024
100 #define PMIC_ARB_TIMEOUT_US 1000
101 #define PMIC_ARB_MAX_TRANS_BYTES (8)
103 #define PMIC_ARB_APID_MASK 0xFF
104 #define PMIC_ARB_PPID_MASK 0xFFF
106 /* interrupt enable bit */
107 #define SPMI_PIC_ACC_ENABLE_BIT BIT(0)
109 #define spec_to_hwirq(slave_id, periph_id, irq_id, apid) \
110 ((((slave_id) & 0xF) << 28) | \
111 (((periph_id) & 0xFF) << 20) | \
112 (((irq_id) & 0x7) << 16) | \
113 (((apid) & 0x3FF) << 0))
115 #define hwirq_to_sid(hwirq) (((hwirq) >> 28) & 0xF)
116 #define hwirq_to_per(hwirq) (((hwirq) >> 20) & 0xFF)
117 #define hwirq_to_irq(hwirq) (((hwirq) >> 16) & 0x7)
118 #define hwirq_to_apid(hwirq) (((hwirq) >> 0) & 0x3FF)
120 struct pmic_arb_ver_ops;
129 * struct spmi_pmic_arb - SPMI PMIC Arbiter object
131 * @rd_base: on v1 "core", on v2 "observer" register base off DT.
132 * @wr_base: on v1 "core", on v2 "chnls" register base off DT.
133 * @intr: address of the SPMI interrupt control registers.
134 * @cnfg: address of the PMIC Arbiter configuration registers.
135 * @lock: lock to synchronize accesses.
136 * @channel: execution environment channel to use for accesses.
137 * @irq: PMIC ARB interrupt.
138 * @ee: the current Execution Environment
139 * @bus_instance: on v7: 0 = primary SPMI bus, 1 = secondary SPMI bus
140 * @min_apid: minimum APID (used for bounding IRQ search)
141 * @max_apid: maximum APID
142 * @base_apid: on v7: minimum APID associated with the particular SPMI
144 * @apid_count: on v5 and v7: number of APIDs associated with the
145 * particular SPMI bus instance
146 * @mapping_table: in-memory copy of PPID -> APID mapping table.
147 * @domain: irq domain object for PMIC IRQ domain
148 * @spmic: SPMI controller object
149 * @ver_ops: version dependent operations.
150 * @ppid_to_apid: in-memory copy of PPID -> APID mapping table.
151 * @last_apid: Highest value APID in use
152 * @apid_data: Table of data for all APIDs
153 * @max_periphs: Number of elements in apid_data[]
155 struct spmi_pmic_arb {
156 void __iomem *rd_base;
157 void __iomem *wr_base;
161 resource_size_t core_size;
172 DECLARE_BITMAP(mapping_table_valid, PMIC_ARB_MAX_PERIPHS);
173 struct irq_domain *domain;
174 struct spmi_controller *spmic;
175 const struct pmic_arb_ver_ops *ver_ops;
178 struct apid_data *apid_data;
183 * struct pmic_arb_ver_ops - version dependent functionality.
185 * @ver_str: version string.
186 * @ppid_to_apid: finds the apid for a given ppid.
187 * @non_data_cmd: on v1 issues an spmi non-data command.
188 * on v2 no HW support, returns -EOPNOTSUPP.
189 * @offset: on v1 offset of per-ee channel.
190 * on v2 offset of per-ee and per-ppid channel.
191 * @fmt_cmd: formats a GENI/SPMI command.
192 * @owner_acc_status: on v1 address of PMIC_ARB_SPMI_PIC_OWNERm_ACC_STATUSn
193 * on v2 address of SPMI_PIC_OWNERm_ACC_STATUSn.
194 * @acc_enable: on v1 address of PMIC_ARB_SPMI_PIC_ACC_ENABLEn
195 * on v2 address of SPMI_PIC_ACC_ENABLEn.
196 * @irq_status: on v1 address of PMIC_ARB_SPMI_PIC_IRQ_STATUSn
197 * on v2 address of SPMI_PIC_IRQ_STATUSn.
198 * @irq_clear: on v1 address of PMIC_ARB_SPMI_PIC_IRQ_CLEARn
199 * on v2 address of SPMI_PIC_IRQ_CLEARn.
200 * @apid_map_offset: offset of PMIC_ARB_REG_CHNLn
201 * @apid_owner: on v2 and later address of SPMI_PERIPHn_2OWNER_TABLE_REG
203 struct pmic_arb_ver_ops {
205 int (*ppid_to_apid)(struct spmi_pmic_arb *pmic_arb, u16 ppid);
206 /* spmi commands (read_cmd, write_cmd, cmd) functionality */
207 int (*offset)(struct spmi_pmic_arb *pmic_arb, u8 sid, u16 addr,
208 enum pmic_arb_channel ch_type);
209 u32 (*fmt_cmd)(u8 opc, u8 sid, u16 addr, u8 bc);
210 int (*non_data_cmd)(struct spmi_controller *ctrl, u8 opc, u8 sid);
211 /* Interrupts controller functionality (offset of PIC registers) */
212 void __iomem *(*owner_acc_status)(struct spmi_pmic_arb *pmic_arb, u8 m,
214 void __iomem *(*acc_enable)(struct spmi_pmic_arb *pmic_arb, u16 n);
215 void __iomem *(*irq_status)(struct spmi_pmic_arb *pmic_arb, u16 n);
216 void __iomem *(*irq_clear)(struct spmi_pmic_arb *pmic_arb, u16 n);
217 u32 (*apid_map_offset)(u16 n);
218 void __iomem *(*apid_owner)(struct spmi_pmic_arb *pmic_arb, u16 n);
221 static inline void pmic_arb_base_write(struct spmi_pmic_arb *pmic_arb,
224 writel_relaxed(val, pmic_arb->wr_base + offset);
227 static inline void pmic_arb_set_rd_cmd(struct spmi_pmic_arb *pmic_arb,
230 writel_relaxed(val, pmic_arb->rd_base + offset);
234 * pmic_arb_read_data: reads pmic-arb's register and copy 1..4 bytes to buf
235 * @bc: byte count -1. range: 0..3
236 * @reg: register's address
237 * @buf: output parameter, length must be bc + 1
240 pmic_arb_read_data(struct spmi_pmic_arb *pmic_arb, u8 *buf, u32 reg, u8 bc)
242 u32 data = __raw_readl(pmic_arb->rd_base + reg);
244 memcpy(buf, &data, (bc & 3) + 1);
248 * pmic_arb_write_data: write 1..4 bytes from buf to pmic-arb's register
249 * @bc: byte-count -1. range: 0..3.
250 * @reg: register's address.
251 * @buf: buffer to write. length must be bc + 1.
253 static void pmic_arb_write_data(struct spmi_pmic_arb *pmic_arb, const u8 *buf,
258 memcpy(&data, buf, (bc & 3) + 1);
259 __raw_writel(data, pmic_arb->wr_base + reg);
262 static int pmic_arb_wait_for_done(struct spmi_controller *ctrl,
263 void __iomem *base, u8 sid, u16 addr,
264 enum pmic_arb_channel ch_type)
266 struct spmi_pmic_arb *pmic_arb = spmi_controller_get_drvdata(ctrl);
268 u32 timeout = PMIC_ARB_TIMEOUT_US;
272 rc = pmic_arb->ver_ops->offset(pmic_arb, sid, addr, ch_type);
277 offset += PMIC_ARB_STATUS;
280 status = readl_relaxed(base + offset);
282 if (status & PMIC_ARB_STATUS_DONE) {
283 if (status & PMIC_ARB_STATUS_DENIED) {
284 dev_err(&ctrl->dev, "%s: %#x %#x: transaction denied (%#x)\n",
285 __func__, sid, addr, status);
289 if (status & PMIC_ARB_STATUS_FAILURE) {
290 dev_err(&ctrl->dev, "%s: %#x %#x: transaction failed (%#x)\n",
291 __func__, sid, addr, status);
296 if (status & PMIC_ARB_STATUS_DROPPED) {
297 dev_err(&ctrl->dev, "%s: %#x %#x: transaction dropped (%#x)\n",
298 __func__, sid, addr, status);
307 dev_err(&ctrl->dev, "%s: %#x %#x: timeout, status %#x\n",
308 __func__, sid, addr, status);
313 pmic_arb_non_data_cmd_v1(struct spmi_controller *ctrl, u8 opc, u8 sid)
315 struct spmi_pmic_arb *pmic_arb = spmi_controller_get_drvdata(ctrl);
321 rc = pmic_arb->ver_ops->offset(pmic_arb, sid, 0, PMIC_ARB_CHANNEL_RW);
326 cmd = ((opc | 0x40) << 27) | ((sid & 0xf) << 20);
328 raw_spin_lock_irqsave(&pmic_arb->lock, flags);
329 pmic_arb_base_write(pmic_arb, offset + PMIC_ARB_CMD, cmd);
330 rc = pmic_arb_wait_for_done(ctrl, pmic_arb->wr_base, sid, 0,
331 PMIC_ARB_CHANNEL_RW);
332 raw_spin_unlock_irqrestore(&pmic_arb->lock, flags);
338 pmic_arb_non_data_cmd_v2(struct spmi_controller *ctrl, u8 opc, u8 sid)
343 /* Non-data command */
344 static int pmic_arb_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid)
346 struct spmi_pmic_arb *pmic_arb = spmi_controller_get_drvdata(ctrl);
348 dev_dbg(&ctrl->dev, "cmd op:0x%x sid:%d\n", opc, sid);
350 /* Check for valid non-data command */
351 if (opc < SPMI_CMD_RESET || opc > SPMI_CMD_WAKEUP)
354 return pmic_arb->ver_ops->non_data_cmd(ctrl, opc, sid);
357 static int pmic_arb_fmt_read_cmd(struct spmi_pmic_arb *pmic_arb, u8 opc, u8 sid,
358 u16 addr, size_t len, u32 *cmd, u32 *offset)
363 rc = pmic_arb->ver_ops->offset(pmic_arb, sid, addr,
364 PMIC_ARB_CHANNEL_OBS);
369 if (bc >= PMIC_ARB_MAX_TRANS_BYTES) {
370 dev_err(&pmic_arb->spmic->dev, "pmic-arb supports 1..%d bytes per trans, but:%zu requested",
371 PMIC_ARB_MAX_TRANS_BYTES, len);
375 /* Check the opcode */
376 if (opc >= 0x60 && opc <= 0x7F)
377 opc = PMIC_ARB_OP_READ;
378 else if (opc >= 0x20 && opc <= 0x2F)
379 opc = PMIC_ARB_OP_EXT_READ;
380 else if (opc >= 0x38 && opc <= 0x3F)
381 opc = PMIC_ARB_OP_EXT_READL;
385 *cmd = pmic_arb->ver_ops->fmt_cmd(opc, sid, addr, bc);
390 static int pmic_arb_read_cmd_unlocked(struct spmi_controller *ctrl, u32 cmd,
391 u32 offset, u8 sid, u16 addr, u8 *buf,
394 struct spmi_pmic_arb *pmic_arb = spmi_controller_get_drvdata(ctrl);
398 pmic_arb_set_rd_cmd(pmic_arb, offset + PMIC_ARB_CMD, cmd);
399 rc = pmic_arb_wait_for_done(ctrl, pmic_arb->rd_base, sid, addr,
400 PMIC_ARB_CHANNEL_OBS);
404 pmic_arb_read_data(pmic_arb, buf, offset + PMIC_ARB_RDATA0,
408 pmic_arb_read_data(pmic_arb, buf + 4, offset + PMIC_ARB_RDATA1,
413 static int pmic_arb_read_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
414 u16 addr, u8 *buf, size_t len)
416 struct spmi_pmic_arb *pmic_arb = spmi_controller_get_drvdata(ctrl);
421 rc = pmic_arb_fmt_read_cmd(pmic_arb, opc, sid, addr, len, &cmd,
426 raw_spin_lock_irqsave(&pmic_arb->lock, flags);
427 rc = pmic_arb_read_cmd_unlocked(ctrl, cmd, offset, sid, addr, buf, len);
428 raw_spin_unlock_irqrestore(&pmic_arb->lock, flags);
433 static int pmic_arb_fmt_write_cmd(struct spmi_pmic_arb *pmic_arb, u8 opc,
434 u8 sid, u16 addr, size_t len, u32 *cmd,
440 rc = pmic_arb->ver_ops->offset(pmic_arb, sid, addr,
441 PMIC_ARB_CHANNEL_RW);
446 if (bc >= PMIC_ARB_MAX_TRANS_BYTES) {
447 dev_err(&pmic_arb->spmic->dev, "pmic-arb supports 1..%d bytes per trans, but:%zu requested",
448 PMIC_ARB_MAX_TRANS_BYTES, len);
452 /* Check the opcode */
453 if (opc >= 0x40 && opc <= 0x5F)
454 opc = PMIC_ARB_OP_WRITE;
455 else if (opc <= 0x0F)
456 opc = PMIC_ARB_OP_EXT_WRITE;
457 else if (opc >= 0x30 && opc <= 0x37)
458 opc = PMIC_ARB_OP_EXT_WRITEL;
459 else if (opc >= 0x80)
460 opc = PMIC_ARB_OP_ZERO_WRITE;
464 *cmd = pmic_arb->ver_ops->fmt_cmd(opc, sid, addr, bc);
469 static int pmic_arb_write_cmd_unlocked(struct spmi_controller *ctrl, u32 cmd,
470 u32 offset, u8 sid, u16 addr,
471 const u8 *buf, size_t len)
473 struct spmi_pmic_arb *pmic_arb = spmi_controller_get_drvdata(ctrl);
476 /* Write data to FIFOs */
477 pmic_arb_write_data(pmic_arb, buf, offset + PMIC_ARB_WDATA0,
480 pmic_arb_write_data(pmic_arb, buf + 4, offset + PMIC_ARB_WDATA1,
483 /* Start the transaction */
484 pmic_arb_base_write(pmic_arb, offset + PMIC_ARB_CMD, cmd);
485 return pmic_arb_wait_for_done(ctrl, pmic_arb->wr_base, sid, addr,
486 PMIC_ARB_CHANNEL_RW);
489 static int pmic_arb_write_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
490 u16 addr, const u8 *buf, size_t len)
492 struct spmi_pmic_arb *pmic_arb = spmi_controller_get_drvdata(ctrl);
497 rc = pmic_arb_fmt_write_cmd(pmic_arb, opc, sid, addr, len, &cmd,
502 raw_spin_lock_irqsave(&pmic_arb->lock, flags);
503 rc = pmic_arb_write_cmd_unlocked(ctrl, cmd, offset, sid, addr, buf,
505 raw_spin_unlock_irqrestore(&pmic_arb->lock, flags);
510 static int pmic_arb_masked_write(struct spmi_controller *ctrl, u8 sid, u16 addr,
511 const u8 *buf, const u8 *mask, size_t len)
513 struct spmi_pmic_arb *pmic_arb = spmi_controller_get_drvdata(ctrl);
514 u32 read_cmd, read_offset, write_cmd, write_offset;
515 u8 temp[PMIC_ARB_MAX_TRANS_BYTES];
519 rc = pmic_arb_fmt_read_cmd(pmic_arb, SPMI_CMD_EXT_READL, sid, addr, len,
520 &read_cmd, &read_offset);
524 rc = pmic_arb_fmt_write_cmd(pmic_arb, SPMI_CMD_EXT_WRITEL, sid, addr,
525 len, &write_cmd, &write_offset);
529 raw_spin_lock_irqsave(&pmic_arb->lock, flags);
530 rc = pmic_arb_read_cmd_unlocked(ctrl, read_cmd, read_offset, sid, addr,
535 for (i = 0; i < len; i++)
536 temp[i] = (temp[i] & ~mask[i]) | (buf[i] & mask[i]);
538 rc = pmic_arb_write_cmd_unlocked(ctrl, write_cmd, write_offset, sid,
541 raw_spin_unlock_irqrestore(&pmic_arb->lock, flags);
547 QPNPINT_REG_RT_STS = 0x10,
548 QPNPINT_REG_SET_TYPE = 0x11,
549 QPNPINT_REG_POLARITY_HIGH = 0x12,
550 QPNPINT_REG_POLARITY_LOW = 0x13,
551 QPNPINT_REG_LATCHED_CLR = 0x14,
552 QPNPINT_REG_EN_SET = 0x15,
553 QPNPINT_REG_EN_CLR = 0x16,
554 QPNPINT_REG_LATCHED_STS = 0x18,
557 struct spmi_pmic_arb_qpnpint_type {
558 u8 type; /* 1 -> edge */
563 /* Simplified accessor functions for irqchip callbacks */
564 static void qpnpint_spmi_write(struct irq_data *d, u8 reg, void *buf,
567 struct spmi_pmic_arb *pmic_arb = irq_data_get_irq_chip_data(d);
568 u8 sid = hwirq_to_sid(d->hwirq);
569 u8 per = hwirq_to_per(d->hwirq);
571 if (pmic_arb_write_cmd(pmic_arb->spmic, SPMI_CMD_EXT_WRITEL, sid,
572 (per << 8) + reg, buf, len))
573 dev_err_ratelimited(&pmic_arb->spmic->dev, "failed irqchip transaction on %x\n",
577 static void qpnpint_spmi_read(struct irq_data *d, u8 reg, void *buf, size_t len)
579 struct spmi_pmic_arb *pmic_arb = irq_data_get_irq_chip_data(d);
580 u8 sid = hwirq_to_sid(d->hwirq);
581 u8 per = hwirq_to_per(d->hwirq);
583 if (pmic_arb_read_cmd(pmic_arb->spmic, SPMI_CMD_EXT_READL, sid,
584 (per << 8) + reg, buf, len))
585 dev_err_ratelimited(&pmic_arb->spmic->dev, "failed irqchip transaction on %x\n",
589 static int qpnpint_spmi_masked_write(struct irq_data *d, u8 reg,
590 const void *buf, const void *mask,
593 struct spmi_pmic_arb *pmic_arb = irq_data_get_irq_chip_data(d);
594 u8 sid = hwirq_to_sid(d->hwirq);
595 u8 per = hwirq_to_per(d->hwirq);
598 rc = pmic_arb_masked_write(pmic_arb->spmic, sid, (per << 8) + reg, buf,
601 dev_err_ratelimited(&pmic_arb->spmic->dev, "failed irqchip transaction on %x rc=%d\n",
606 static void cleanup_irq(struct spmi_pmic_arb *pmic_arb, u16 apid, int id)
608 u16 ppid = pmic_arb->apid_data[apid].ppid;
610 u8 per = ppid & 0xFF;
611 u8 irq_mask = BIT(id);
613 dev_err_ratelimited(&pmic_arb->spmic->dev, "%s apid=%d sid=0x%x per=0x%x irq=%d\n",
614 __func__, apid, sid, per, id);
615 writel_relaxed(irq_mask, pmic_arb->ver_ops->irq_clear(pmic_arb, apid));
618 static int periph_interrupt(struct spmi_pmic_arb *pmic_arb, u16 apid)
623 u8 sid = (pmic_arb->apid_data[apid].ppid >> 8) & 0xF;
624 u8 per = pmic_arb->apid_data[apid].ppid & 0xFF;
626 status = readl_relaxed(pmic_arb->ver_ops->irq_status(pmic_arb, apid));
628 id = ffs(status) - 1;
630 irq = irq_find_mapping(pmic_arb->domain,
631 spec_to_hwirq(sid, per, id, apid));
633 cleanup_irq(pmic_arb, apid, id);
636 generic_handle_irq(irq);
643 static void pmic_arb_chained_irq(struct irq_desc *desc)
645 struct spmi_pmic_arb *pmic_arb = irq_desc_get_handler_data(desc);
646 const struct pmic_arb_ver_ops *ver_ops = pmic_arb->ver_ops;
647 struct irq_chip *chip = irq_desc_get_chip(desc);
648 int first = pmic_arb->min_apid;
649 int last = pmic_arb->max_apid;
651 * acc_offset will be non-zero for the secondary SPMI bus instance on
654 int acc_offset = pmic_arb->base_apid >> 5;
655 u8 ee = pmic_arb->ee;
656 u32 status, enable, handled = 0;
658 /* status based dispatch */
659 bool acc_valid = false;
662 chained_irq_enter(chip, desc);
664 for (i = first >> 5; i <= last >> 5; ++i) {
665 status = readl_relaxed(ver_ops->owner_acc_status(pmic_arb, ee, i - acc_offset));
670 id = ffs(status) - 1;
673 if (apid < first || apid > last) {
674 WARN_ONCE(true, "spurious spmi irq received for apid=%d\n",
678 enable = readl_relaxed(
679 ver_ops->acc_enable(pmic_arb, apid));
680 if (enable & SPMI_PIC_ACC_ENABLE_BIT)
681 if (periph_interrupt(pmic_arb, apid) != 0)
686 /* ACC_STATUS is empty but IRQ fired check IRQ_STATUS */
688 for (i = first; i <= last; i++) {
689 /* skip if APPS is not irq owner */
690 if (pmic_arb->apid_data[i].irq_ee != pmic_arb->ee)
693 irq_status = readl_relaxed(
694 ver_ops->irq_status(pmic_arb, i));
696 enable = readl_relaxed(
697 ver_ops->acc_enable(pmic_arb, i));
698 if (enable & SPMI_PIC_ACC_ENABLE_BIT) {
699 dev_dbg(&pmic_arb->spmic->dev,
700 "Dispatching IRQ for apid=%d status=%x\n",
702 if (periph_interrupt(pmic_arb, i) != 0)
710 handle_bad_irq(desc);
712 chained_irq_exit(chip, desc);
715 static void qpnpint_irq_ack(struct irq_data *d)
717 struct spmi_pmic_arb *pmic_arb = irq_data_get_irq_chip_data(d);
718 u8 irq = hwirq_to_irq(d->hwirq);
719 u16 apid = hwirq_to_apid(d->hwirq);
722 writel_relaxed(BIT(irq), pmic_arb->ver_ops->irq_clear(pmic_arb, apid));
725 qpnpint_spmi_write(d, QPNPINT_REG_LATCHED_CLR, &data, 1);
728 static void qpnpint_irq_mask(struct irq_data *d)
730 u8 irq = hwirq_to_irq(d->hwirq);
733 qpnpint_spmi_write(d, QPNPINT_REG_EN_CLR, &data, 1);
736 static void qpnpint_irq_unmask(struct irq_data *d)
738 struct spmi_pmic_arb *pmic_arb = irq_data_get_irq_chip_data(d);
739 const struct pmic_arb_ver_ops *ver_ops = pmic_arb->ver_ops;
740 u8 irq = hwirq_to_irq(d->hwirq);
741 u16 apid = hwirq_to_apid(d->hwirq);
744 writel_relaxed(SPMI_PIC_ACC_ENABLE_BIT,
745 ver_ops->acc_enable(pmic_arb, apid));
747 qpnpint_spmi_read(d, QPNPINT_REG_EN_SET, &buf[0], 1);
748 if (!(buf[0] & BIT(irq))) {
750 * Since the interrupt is currently disabled, write to both the
751 * LATCHED_CLR and EN_SET registers so that a spurious interrupt
752 * cannot be triggered when the interrupt is enabled
756 qpnpint_spmi_write(d, QPNPINT_REG_LATCHED_CLR, &buf, 2);
760 static int qpnpint_irq_set_type(struct irq_data *d, unsigned int flow_type)
762 struct spmi_pmic_arb_qpnpint_type type = {0};
763 struct spmi_pmic_arb_qpnpint_type mask;
764 irq_flow_handler_t flow_handler;
765 u8 irq_bit = BIT(hwirq_to_irq(d->hwirq));
768 if (flow_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)) {
770 if (flow_type & IRQF_TRIGGER_RISING)
771 type.polarity_high = irq_bit;
772 if (flow_type & IRQF_TRIGGER_FALLING)
773 type.polarity_low = irq_bit;
775 flow_handler = handle_edge_irq;
777 if ((flow_type & (IRQF_TRIGGER_HIGH)) &&
778 (flow_type & (IRQF_TRIGGER_LOW)))
781 if (flow_type & IRQF_TRIGGER_HIGH)
782 type.polarity_high = irq_bit;
784 type.polarity_low = irq_bit;
786 flow_handler = handle_level_irq;
790 mask.polarity_high = irq_bit;
791 mask.polarity_low = irq_bit;
793 rc = qpnpint_spmi_masked_write(d, QPNPINT_REG_SET_TYPE, &type, &mask,
795 irq_set_handler_locked(d, flow_handler);
800 static int qpnpint_irq_set_wake(struct irq_data *d, unsigned int on)
802 struct spmi_pmic_arb *pmic_arb = irq_data_get_irq_chip_data(d);
804 return irq_set_irq_wake(pmic_arb->irq, on);
807 static int qpnpint_get_irqchip_state(struct irq_data *d,
808 enum irqchip_irq_state which,
811 u8 irq = hwirq_to_irq(d->hwirq);
814 if (which != IRQCHIP_STATE_LINE_LEVEL)
817 qpnpint_spmi_read(d, QPNPINT_REG_RT_STS, &status, 1);
818 *state = !!(status & BIT(irq));
823 static int qpnpint_irq_domain_activate(struct irq_domain *domain,
824 struct irq_data *d, bool reserve)
826 struct spmi_pmic_arb *pmic_arb = irq_data_get_irq_chip_data(d);
827 u16 periph = hwirq_to_per(d->hwirq);
828 u16 apid = hwirq_to_apid(d->hwirq);
829 u16 sid = hwirq_to_sid(d->hwirq);
830 u16 irq = hwirq_to_irq(d->hwirq);
833 if (pmic_arb->apid_data[apid].irq_ee != pmic_arb->ee) {
834 dev_err(&pmic_arb->spmic->dev, "failed to xlate sid = %#x, periph = %#x, irq = %u: ee=%u but owner=%u\n",
835 sid, periph, irq, pmic_arb->ee,
836 pmic_arb->apid_data[apid].irq_ee);
841 qpnpint_spmi_write(d, QPNPINT_REG_EN_CLR, &buf, 1);
842 qpnpint_spmi_write(d, QPNPINT_REG_LATCHED_CLR, &buf, 1);
847 static struct irq_chip pmic_arb_irqchip = {
849 .irq_ack = qpnpint_irq_ack,
850 .irq_mask = qpnpint_irq_mask,
851 .irq_unmask = qpnpint_irq_unmask,
852 .irq_set_type = qpnpint_irq_set_type,
853 .irq_set_wake = qpnpint_irq_set_wake,
854 .irq_get_irqchip_state = qpnpint_get_irqchip_state,
855 .flags = IRQCHIP_MASK_ON_SUSPEND,
858 static int qpnpint_irq_domain_translate(struct irq_domain *d,
859 struct irq_fwspec *fwspec,
860 unsigned long *out_hwirq,
861 unsigned int *out_type)
863 struct spmi_pmic_arb *pmic_arb = d->host_data;
864 u32 *intspec = fwspec->param;
868 dev_dbg(&pmic_arb->spmic->dev, "intspec[0] 0x%1x intspec[1] 0x%02x intspec[2] 0x%02x\n",
869 intspec[0], intspec[1], intspec[2]);
871 if (irq_domain_get_of_node(d) != pmic_arb->spmic->dev.of_node)
873 if (fwspec->param_count != 4)
875 if (intspec[0] > 0xF || intspec[1] > 0xFF || intspec[2] > 0x7)
878 ppid = intspec[0] << 8 | intspec[1];
879 rc = pmic_arb->ver_ops->ppid_to_apid(pmic_arb, ppid);
881 dev_err(&pmic_arb->spmic->dev, "failed to xlate sid = %#x, periph = %#x, irq = %u rc = %d\n",
882 intspec[0], intspec[1], intspec[2], rc);
887 /* Keep track of {max,min}_apid for bounding search during interrupt */
888 if (apid > pmic_arb->max_apid)
889 pmic_arb->max_apid = apid;
890 if (apid < pmic_arb->min_apid)
891 pmic_arb->min_apid = apid;
893 *out_hwirq = spec_to_hwirq(intspec[0], intspec[1], intspec[2], apid);
894 *out_type = intspec[3] & IRQ_TYPE_SENSE_MASK;
896 dev_dbg(&pmic_arb->spmic->dev, "out_hwirq = %lu\n", *out_hwirq);
901 static struct lock_class_key qpnpint_irq_lock_class, qpnpint_irq_request_class;
903 static void qpnpint_irq_domain_map(struct spmi_pmic_arb *pmic_arb,
904 struct irq_domain *domain, unsigned int virq,
905 irq_hw_number_t hwirq, unsigned int type)
907 irq_flow_handler_t handler;
909 dev_dbg(&pmic_arb->spmic->dev, "virq = %u, hwirq = %lu, type = %u\n",
912 if (type & IRQ_TYPE_EDGE_BOTH)
913 handler = handle_edge_irq;
915 handler = handle_level_irq;
918 irq_set_lockdep_class(virq, &qpnpint_irq_lock_class,
919 &qpnpint_irq_request_class);
920 irq_domain_set_info(domain, virq, hwirq, &pmic_arb_irqchip, pmic_arb,
921 handler, NULL, NULL);
924 static int qpnpint_irq_domain_alloc(struct irq_domain *domain,
925 unsigned int virq, unsigned int nr_irqs,
928 struct spmi_pmic_arb *pmic_arb = domain->host_data;
929 struct irq_fwspec *fwspec = data;
930 irq_hw_number_t hwirq;
934 ret = qpnpint_irq_domain_translate(domain, fwspec, &hwirq, &type);
938 for (i = 0; i < nr_irqs; i++)
939 qpnpint_irq_domain_map(pmic_arb, domain, virq + i, hwirq + i,
945 static int pmic_arb_ppid_to_apid_v1(struct spmi_pmic_arb *pmic_arb, u16 ppid)
947 u32 *mapping_table = pmic_arb->mapping_table;
953 apid_valid = pmic_arb->ppid_to_apid[ppid];
954 if (apid_valid & PMIC_ARB_APID_VALID) {
955 apid = apid_valid & ~PMIC_ARB_APID_VALID;
959 for (i = 0; i < SPMI_MAPPING_TABLE_TREE_DEPTH; ++i) {
960 if (!test_and_set_bit(index, pmic_arb->mapping_table_valid))
961 mapping_table[index] = readl_relaxed(pmic_arb->cnfg +
962 SPMI_MAPPING_TABLE_REG(index));
964 data = mapping_table[index];
966 if (ppid & BIT(SPMI_MAPPING_BIT_INDEX(data))) {
967 if (SPMI_MAPPING_BIT_IS_1_FLAG(data)) {
968 index = SPMI_MAPPING_BIT_IS_1_RESULT(data);
970 apid = SPMI_MAPPING_BIT_IS_1_RESULT(data);
971 pmic_arb->ppid_to_apid[ppid]
972 = apid | PMIC_ARB_APID_VALID;
973 pmic_arb->apid_data[apid].ppid = ppid;
977 if (SPMI_MAPPING_BIT_IS_0_FLAG(data)) {
978 index = SPMI_MAPPING_BIT_IS_0_RESULT(data);
980 apid = SPMI_MAPPING_BIT_IS_0_RESULT(data);
981 pmic_arb->ppid_to_apid[ppid]
982 = apid | PMIC_ARB_APID_VALID;
983 pmic_arb->apid_data[apid].ppid = ppid;
992 /* v1 offset per ee */
993 static int pmic_arb_offset_v1(struct spmi_pmic_arb *pmic_arb, u8 sid, u16 addr,
994 enum pmic_arb_channel ch_type)
996 return 0x800 + 0x80 * pmic_arb->channel;
999 static u16 pmic_arb_find_apid(struct spmi_pmic_arb *pmic_arb, u16 ppid)
1001 struct apid_data *apidd = &pmic_arb->apid_data[pmic_arb->last_apid];
1005 for (apid = pmic_arb->last_apid; ; apid++, apidd++) {
1006 offset = pmic_arb->ver_ops->apid_map_offset(apid);
1007 if (offset >= pmic_arb->core_size)
1010 regval = readl_relaxed(pmic_arb->ver_ops->apid_owner(pmic_arb,
1012 apidd->irq_ee = SPMI_OWNERSHIP_PERIPH2OWNER(regval);
1013 apidd->write_ee = apidd->irq_ee;
1015 regval = readl_relaxed(pmic_arb->core + offset);
1019 id = (regval >> 8) & PMIC_ARB_PPID_MASK;
1020 pmic_arb->ppid_to_apid[id] = apid | PMIC_ARB_APID_VALID;
1023 apid |= PMIC_ARB_APID_VALID;
1027 pmic_arb->last_apid = apid & ~PMIC_ARB_APID_VALID;
1032 static int pmic_arb_ppid_to_apid_v2(struct spmi_pmic_arb *pmic_arb, u16 ppid)
1036 apid_valid = pmic_arb->ppid_to_apid[ppid];
1037 if (!(apid_valid & PMIC_ARB_APID_VALID))
1038 apid_valid = pmic_arb_find_apid(pmic_arb, ppid);
1039 if (!(apid_valid & PMIC_ARB_APID_VALID))
1042 return apid_valid & ~PMIC_ARB_APID_VALID;
1045 static int pmic_arb_read_apid_map_v5(struct spmi_pmic_arb *pmic_arb)
1047 struct apid_data *apidd;
1048 struct apid_data *prev_apidd;
1049 u16 i, apid, ppid, apid_max;
1050 bool valid, is_irq_ee;
1054 * In order to allow multiple EEs to write to a single PPID in arbiter
1055 * version 5 and 7, there is more than one APID mapped to each PPID.
1056 * The owner field for each of these mappings specifies the EE which is
1057 * allowed to write to the APID. The owner of the last (highest) APID
1058 * which has the IRQ owner bit set for a given PPID will receive
1059 * interrupts from the PPID.
1061 * In arbiter version 7, the APID numbering space is divided between
1062 * the primary bus (0) and secondary bus (1) such that:
1063 * APID = 0 to N-1 are assigned to the primary bus
1064 * APID = N to N+M-1 are assigned to the secondary bus
1065 * where N = number of APIDs supported by the primary bus and
1066 * M = number of APIDs supported by the secondary bus
1068 apidd = &pmic_arb->apid_data[pmic_arb->base_apid];
1069 apid_max = pmic_arb->base_apid + pmic_arb->apid_count;
1070 for (i = pmic_arb->base_apid; i < apid_max; i++, apidd++) {
1071 offset = pmic_arb->ver_ops->apid_map_offset(i);
1072 if (offset >= pmic_arb->core_size)
1075 regval = readl_relaxed(pmic_arb->core + offset);
1078 ppid = (regval >> 8) & PMIC_ARB_PPID_MASK;
1079 is_irq_ee = PMIC_ARB_CHAN_IS_IRQ_OWNER(regval);
1081 regval = readl_relaxed(pmic_arb->ver_ops->apid_owner(pmic_arb,
1083 apidd->write_ee = SPMI_OWNERSHIP_PERIPH2OWNER(regval);
1085 apidd->irq_ee = is_irq_ee ? apidd->write_ee : INVALID_EE;
1087 valid = pmic_arb->ppid_to_apid[ppid] & PMIC_ARB_APID_VALID;
1088 apid = pmic_arb->ppid_to_apid[ppid] & ~PMIC_ARB_APID_VALID;
1089 prev_apidd = &pmic_arb->apid_data[apid];
1091 if (!valid || apidd->write_ee == pmic_arb->ee) {
1092 /* First PPID mapping or one for this EE */
1093 pmic_arb->ppid_to_apid[ppid] = i | PMIC_ARB_APID_VALID;
1094 } else if (valid && is_irq_ee &&
1095 prev_apidd->write_ee == pmic_arb->ee) {
1097 * Duplicate PPID mapping after the one for this EE;
1098 * override the irq owner
1100 prev_apidd->irq_ee = apidd->irq_ee;
1104 pmic_arb->last_apid = i;
1107 /* Dump the mapping table for debug purposes. */
1108 dev_dbg(&pmic_arb->spmic->dev, "PPID APID Write-EE IRQ-EE\n");
1109 for (ppid = 0; ppid < PMIC_ARB_MAX_PPID; ppid++) {
1110 apid = pmic_arb->ppid_to_apid[ppid];
1111 if (apid & PMIC_ARB_APID_VALID) {
1112 apid &= ~PMIC_ARB_APID_VALID;
1113 apidd = &pmic_arb->apid_data[apid];
1114 dev_dbg(&pmic_arb->spmic->dev, "%#03X %3u %2u %2u\n",
1115 ppid, apid, apidd->write_ee, apidd->irq_ee);
1122 static int pmic_arb_ppid_to_apid_v5(struct spmi_pmic_arb *pmic_arb, u16 ppid)
1124 if (!(pmic_arb->ppid_to_apid[ppid] & PMIC_ARB_APID_VALID))
1127 return pmic_arb->ppid_to_apid[ppid] & ~PMIC_ARB_APID_VALID;
1130 /* v2 offset per ppid and per ee */
1131 static int pmic_arb_offset_v2(struct spmi_pmic_arb *pmic_arb, u8 sid, u16 addr,
1132 enum pmic_arb_channel ch_type)
1138 ppid = sid << 8 | ((addr >> 8) & 0xFF);
1139 rc = pmic_arb_ppid_to_apid_v2(pmic_arb, ppid);
1144 return 0x1000 * pmic_arb->ee + 0x8000 * apid;
1148 * v5 offset per ee and per apid for observer channels and per apid for
1149 * read/write channels.
1151 static int pmic_arb_offset_v5(struct spmi_pmic_arb *pmic_arb, u8 sid, u16 addr,
1152 enum pmic_arb_channel ch_type)
1157 u16 ppid = (sid << 8) | (addr >> 8);
1159 rc = pmic_arb_ppid_to_apid_v5(pmic_arb, ppid);
1165 case PMIC_ARB_CHANNEL_OBS:
1166 offset = 0x10000 * pmic_arb->ee + 0x80 * apid;
1168 case PMIC_ARB_CHANNEL_RW:
1169 if (pmic_arb->apid_data[apid].write_ee != pmic_arb->ee) {
1170 dev_err(&pmic_arb->spmic->dev, "disallowed SPMI write to sid=%u, addr=0x%04X\n",
1174 offset = 0x10000 * apid;
1182 * v7 offset per ee and per apid for observer channels and per apid for
1183 * read/write channels.
1185 static int pmic_arb_offset_v7(struct spmi_pmic_arb *pmic_arb, u8 sid, u16 addr,
1186 enum pmic_arb_channel ch_type)
1191 u16 ppid = (sid << 8) | (addr >> 8);
1193 rc = pmic_arb->ver_ops->ppid_to_apid(pmic_arb, ppid);
1199 case PMIC_ARB_CHANNEL_OBS:
1200 offset = 0x8000 * pmic_arb->ee + 0x20 * apid;
1202 case PMIC_ARB_CHANNEL_RW:
1203 if (pmic_arb->apid_data[apid].write_ee != pmic_arb->ee) {
1204 dev_err(&pmic_arb->spmic->dev, "disallowed SPMI write to sid=%u, addr=0x%04X\n",
1208 offset = 0x1000 * apid;
1215 static u32 pmic_arb_fmt_cmd_v1(u8 opc, u8 sid, u16 addr, u8 bc)
1217 return (opc << 27) | ((sid & 0xf) << 20) | (addr << 4) | (bc & 0x7);
1220 static u32 pmic_arb_fmt_cmd_v2(u8 opc, u8 sid, u16 addr, u8 bc)
1222 return (opc << 27) | ((addr & 0xff) << 4) | (bc & 0x7);
1225 static void __iomem *
1226 pmic_arb_owner_acc_status_v1(struct spmi_pmic_arb *pmic_arb, u8 m, u16 n)
1228 return pmic_arb->intr + 0x20 * m + 0x4 * n;
1231 static void __iomem *
1232 pmic_arb_owner_acc_status_v2(struct spmi_pmic_arb *pmic_arb, u8 m, u16 n)
1234 return pmic_arb->intr + 0x100000 + 0x1000 * m + 0x4 * n;
1237 static void __iomem *
1238 pmic_arb_owner_acc_status_v3(struct spmi_pmic_arb *pmic_arb, u8 m, u16 n)
1240 return pmic_arb->intr + 0x200000 + 0x1000 * m + 0x4 * n;
1243 static void __iomem *
1244 pmic_arb_owner_acc_status_v5(struct spmi_pmic_arb *pmic_arb, u8 m, u16 n)
1246 return pmic_arb->intr + 0x10000 * m + 0x4 * n;
1249 static void __iomem *
1250 pmic_arb_owner_acc_status_v7(struct spmi_pmic_arb *pmic_arb, u8 m, u16 n)
1252 return pmic_arb->intr + 0x1000 * m + 0x4 * n;
1255 static void __iomem *
1256 pmic_arb_acc_enable_v1(struct spmi_pmic_arb *pmic_arb, u16 n)
1258 return pmic_arb->intr + 0x200 + 0x4 * n;
1261 static void __iomem *
1262 pmic_arb_acc_enable_v2(struct spmi_pmic_arb *pmic_arb, u16 n)
1264 return pmic_arb->intr + 0x1000 * n;
1267 static void __iomem *
1268 pmic_arb_acc_enable_v5(struct spmi_pmic_arb *pmic_arb, u16 n)
1270 return pmic_arb->wr_base + 0x100 + 0x10000 * n;
1273 static void __iomem *
1274 pmic_arb_acc_enable_v7(struct spmi_pmic_arb *pmic_arb, u16 n)
1276 return pmic_arb->wr_base + 0x100 + 0x1000 * n;
1279 static void __iomem *
1280 pmic_arb_irq_status_v1(struct spmi_pmic_arb *pmic_arb, u16 n)
1282 return pmic_arb->intr + 0x600 + 0x4 * n;
1285 static void __iomem *
1286 pmic_arb_irq_status_v2(struct spmi_pmic_arb *pmic_arb, u16 n)
1288 return pmic_arb->intr + 0x4 + 0x1000 * n;
1291 static void __iomem *
1292 pmic_arb_irq_status_v5(struct spmi_pmic_arb *pmic_arb, u16 n)
1294 return pmic_arb->wr_base + 0x104 + 0x10000 * n;
1297 static void __iomem *
1298 pmic_arb_irq_status_v7(struct spmi_pmic_arb *pmic_arb, u16 n)
1300 return pmic_arb->wr_base + 0x104 + 0x1000 * n;
1303 static void __iomem *
1304 pmic_arb_irq_clear_v1(struct spmi_pmic_arb *pmic_arb, u16 n)
1306 return pmic_arb->intr + 0xA00 + 0x4 * n;
1309 static void __iomem *
1310 pmic_arb_irq_clear_v2(struct spmi_pmic_arb *pmic_arb, u16 n)
1312 return pmic_arb->intr + 0x8 + 0x1000 * n;
1315 static void __iomem *
1316 pmic_arb_irq_clear_v5(struct spmi_pmic_arb *pmic_arb, u16 n)
1318 return pmic_arb->wr_base + 0x108 + 0x10000 * n;
1321 static void __iomem *
1322 pmic_arb_irq_clear_v7(struct spmi_pmic_arb *pmic_arb, u16 n)
1324 return pmic_arb->wr_base + 0x108 + 0x1000 * n;
1327 static u32 pmic_arb_apid_map_offset_v2(u16 n)
1329 return 0x800 + 0x4 * n;
1332 static u32 pmic_arb_apid_map_offset_v5(u16 n)
1334 return 0x900 + 0x4 * n;
1337 static u32 pmic_arb_apid_map_offset_v7(u16 n)
1339 return 0x2000 + 0x4 * n;
1342 static void __iomem *
1343 pmic_arb_apid_owner_v2(struct spmi_pmic_arb *pmic_arb, u16 n)
1345 return pmic_arb->cnfg + 0x700 + 0x4 * n;
1349 * For arbiter version 7, APID ownership table registers have independent
1350 * numbering space for each SPMI bus instance, so each is indexed starting from
1353 static void __iomem *
1354 pmic_arb_apid_owner_v7(struct spmi_pmic_arb *pmic_arb, u16 n)
1356 return pmic_arb->cnfg + 0x4 * (n - pmic_arb->base_apid);
1359 static const struct pmic_arb_ver_ops pmic_arb_v1 = {
1361 .ppid_to_apid = pmic_arb_ppid_to_apid_v1,
1362 .non_data_cmd = pmic_arb_non_data_cmd_v1,
1363 .offset = pmic_arb_offset_v1,
1364 .fmt_cmd = pmic_arb_fmt_cmd_v1,
1365 .owner_acc_status = pmic_arb_owner_acc_status_v1,
1366 .acc_enable = pmic_arb_acc_enable_v1,
1367 .irq_status = pmic_arb_irq_status_v1,
1368 .irq_clear = pmic_arb_irq_clear_v1,
1369 .apid_map_offset = pmic_arb_apid_map_offset_v2,
1370 .apid_owner = pmic_arb_apid_owner_v2,
1373 static const struct pmic_arb_ver_ops pmic_arb_v2 = {
1375 .ppid_to_apid = pmic_arb_ppid_to_apid_v2,
1376 .non_data_cmd = pmic_arb_non_data_cmd_v2,
1377 .offset = pmic_arb_offset_v2,
1378 .fmt_cmd = pmic_arb_fmt_cmd_v2,
1379 .owner_acc_status = pmic_arb_owner_acc_status_v2,
1380 .acc_enable = pmic_arb_acc_enable_v2,
1381 .irq_status = pmic_arb_irq_status_v2,
1382 .irq_clear = pmic_arb_irq_clear_v2,
1383 .apid_map_offset = pmic_arb_apid_map_offset_v2,
1384 .apid_owner = pmic_arb_apid_owner_v2,
1387 static const struct pmic_arb_ver_ops pmic_arb_v3 = {
1389 .ppid_to_apid = pmic_arb_ppid_to_apid_v2,
1390 .non_data_cmd = pmic_arb_non_data_cmd_v2,
1391 .offset = pmic_arb_offset_v2,
1392 .fmt_cmd = pmic_arb_fmt_cmd_v2,
1393 .owner_acc_status = pmic_arb_owner_acc_status_v3,
1394 .acc_enable = pmic_arb_acc_enable_v2,
1395 .irq_status = pmic_arb_irq_status_v2,
1396 .irq_clear = pmic_arb_irq_clear_v2,
1397 .apid_map_offset = pmic_arb_apid_map_offset_v2,
1398 .apid_owner = pmic_arb_apid_owner_v2,
1401 static const struct pmic_arb_ver_ops pmic_arb_v5 = {
1403 .ppid_to_apid = pmic_arb_ppid_to_apid_v5,
1404 .non_data_cmd = pmic_arb_non_data_cmd_v2,
1405 .offset = pmic_arb_offset_v5,
1406 .fmt_cmd = pmic_arb_fmt_cmd_v2,
1407 .owner_acc_status = pmic_arb_owner_acc_status_v5,
1408 .acc_enable = pmic_arb_acc_enable_v5,
1409 .irq_status = pmic_arb_irq_status_v5,
1410 .irq_clear = pmic_arb_irq_clear_v5,
1411 .apid_map_offset = pmic_arb_apid_map_offset_v5,
1412 .apid_owner = pmic_arb_apid_owner_v2,
1415 static const struct pmic_arb_ver_ops pmic_arb_v7 = {
1417 .ppid_to_apid = pmic_arb_ppid_to_apid_v5,
1418 .non_data_cmd = pmic_arb_non_data_cmd_v2,
1419 .offset = pmic_arb_offset_v7,
1420 .fmt_cmd = pmic_arb_fmt_cmd_v2,
1421 .owner_acc_status = pmic_arb_owner_acc_status_v7,
1422 .acc_enable = pmic_arb_acc_enable_v7,
1423 .irq_status = pmic_arb_irq_status_v7,
1424 .irq_clear = pmic_arb_irq_clear_v7,
1425 .apid_map_offset = pmic_arb_apid_map_offset_v7,
1426 .apid_owner = pmic_arb_apid_owner_v7,
1429 static const struct irq_domain_ops pmic_arb_irq_domain_ops = {
1430 .activate = qpnpint_irq_domain_activate,
1431 .alloc = qpnpint_irq_domain_alloc,
1432 .free = irq_domain_free_irqs_common,
1433 .translate = qpnpint_irq_domain_translate,
1436 static int spmi_pmic_arb_probe(struct platform_device *pdev)
1438 struct spmi_pmic_arb *pmic_arb;
1439 struct spmi_controller *ctrl;
1440 struct resource *res;
1443 u32 channel, ee, hw_ver;
1446 ctrl = spmi_controller_alloc(&pdev->dev, sizeof(*pmic_arb));
1450 pmic_arb = spmi_controller_get_drvdata(ctrl);
1451 pmic_arb->spmic = ctrl;
1454 * Please don't replace this with devm_platform_ioremap_resource() or
1455 * devm_ioremap_resource(). These both result in a call to
1456 * devm_request_mem_region() which prevents multiple mappings of this
1457 * register address range. SoCs with PMIC arbiter v7 may define two
1458 * arbiter devices, for the two physical SPMI interfaces, which share
1459 * some register address ranges (i.e. "core", "obsrvr", and "chnls").
1460 * Ensure that both devices probe successfully by calling devm_ioremap()
1461 * which does not result in a devm_request_mem_region() call.
1463 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "core");
1464 core = devm_ioremap(&ctrl->dev, res->start, resource_size(res));
1466 err = PTR_ERR(core);
1470 pmic_arb->core_size = resource_size(res);
1472 pmic_arb->ppid_to_apid = devm_kcalloc(&ctrl->dev, PMIC_ARB_MAX_PPID,
1473 sizeof(*pmic_arb->ppid_to_apid),
1475 if (!pmic_arb->ppid_to_apid) {
1480 hw_ver = readl_relaxed(core + PMIC_ARB_VERSION);
1482 if (hw_ver < PMIC_ARB_VERSION_V2_MIN) {
1483 pmic_arb->ver_ops = &pmic_arb_v1;
1484 pmic_arb->wr_base = core;
1485 pmic_arb->rd_base = core;
1487 pmic_arb->core = core;
1489 if (hw_ver < PMIC_ARB_VERSION_V3_MIN)
1490 pmic_arb->ver_ops = &pmic_arb_v2;
1491 else if (hw_ver < PMIC_ARB_VERSION_V5_MIN)
1492 pmic_arb->ver_ops = &pmic_arb_v3;
1493 else if (hw_ver < PMIC_ARB_VERSION_V7_MIN)
1494 pmic_arb->ver_ops = &pmic_arb_v5;
1496 pmic_arb->ver_ops = &pmic_arb_v7;
1498 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1500 pmic_arb->rd_base = devm_ioremap(&ctrl->dev, res->start,
1501 resource_size(res));
1502 if (IS_ERR(pmic_arb->rd_base)) {
1503 err = PTR_ERR(pmic_arb->rd_base);
1507 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1509 pmic_arb->wr_base = devm_ioremap(&ctrl->dev, res->start,
1510 resource_size(res));
1511 if (IS_ERR(pmic_arb->wr_base)) {
1512 err = PTR_ERR(pmic_arb->wr_base);
1517 pmic_arb->max_periphs = PMIC_ARB_MAX_PERIPHS;
1519 if (hw_ver >= PMIC_ARB_VERSION_V7_MIN) {
1520 pmic_arb->max_periphs = PMIC_ARB_MAX_PERIPHS_V7;
1521 /* Optional property for v7: */
1522 of_property_read_u32(pdev->dev.of_node, "qcom,bus-id",
1523 &pmic_arb->bus_instance);
1524 if (pmic_arb->bus_instance > 1) {
1526 dev_err(&pdev->dev, "invalid bus instance (%u) specified\n",
1527 pmic_arb->bus_instance);
1531 if (pmic_arb->bus_instance == 0) {
1532 pmic_arb->base_apid = 0;
1533 pmic_arb->apid_count =
1534 readl_relaxed(core + PMIC_ARB_FEATURES) &
1535 PMIC_ARB_FEATURES_PERIPH_MASK;
1537 pmic_arb->base_apid =
1538 readl_relaxed(core + PMIC_ARB_FEATURES) &
1539 PMIC_ARB_FEATURES_PERIPH_MASK;
1540 pmic_arb->apid_count =
1541 readl_relaxed(core + PMIC_ARB_FEATURES1) &
1542 PMIC_ARB_FEATURES_PERIPH_MASK;
1545 if (pmic_arb->base_apid + pmic_arb->apid_count > pmic_arb->max_periphs) {
1547 dev_err(&pdev->dev, "Unsupported APID count %d detected\n",
1548 pmic_arb->base_apid + pmic_arb->apid_count);
1551 } else if (hw_ver >= PMIC_ARB_VERSION_V5_MIN) {
1552 pmic_arb->base_apid = 0;
1553 pmic_arb->apid_count = readl_relaxed(core + PMIC_ARB_FEATURES) &
1554 PMIC_ARB_FEATURES_PERIPH_MASK;
1556 if (pmic_arb->apid_count > pmic_arb->max_periphs) {
1558 dev_err(&pdev->dev, "Unsupported APID count %d detected\n",
1559 pmic_arb->apid_count);
1564 pmic_arb->apid_data = devm_kcalloc(&ctrl->dev, pmic_arb->max_periphs,
1565 sizeof(*pmic_arb->apid_data),
1567 if (!pmic_arb->apid_data) {
1572 dev_info(&ctrl->dev, "PMIC arbiter version %s (0x%x)\n",
1573 pmic_arb->ver_ops->ver_str, hw_ver);
1575 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "intr");
1576 pmic_arb->intr = devm_ioremap_resource(&ctrl->dev, res);
1577 if (IS_ERR(pmic_arb->intr)) {
1578 err = PTR_ERR(pmic_arb->intr);
1582 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cnfg");
1583 pmic_arb->cnfg = devm_ioremap_resource(&ctrl->dev, res);
1584 if (IS_ERR(pmic_arb->cnfg)) {
1585 err = PTR_ERR(pmic_arb->cnfg);
1589 pmic_arb->irq = platform_get_irq_byname(pdev, "periph_irq");
1590 if (pmic_arb->irq < 0) {
1591 err = pmic_arb->irq;
1595 err = of_property_read_u32(pdev->dev.of_node, "qcom,channel", &channel);
1597 dev_err(&pdev->dev, "channel unspecified.\n");
1602 dev_err(&pdev->dev, "invalid channel (%u) specified.\n",
1608 pmic_arb->channel = channel;
1610 err = of_property_read_u32(pdev->dev.of_node, "qcom,ee", &ee);
1612 dev_err(&pdev->dev, "EE unspecified.\n");
1617 dev_err(&pdev->dev, "invalid EE (%u) specified\n", ee);
1623 mapping_table = devm_kcalloc(&ctrl->dev, pmic_arb->max_periphs,
1624 sizeof(*mapping_table), GFP_KERNEL);
1625 if (!mapping_table) {
1630 pmic_arb->mapping_table = mapping_table;
1631 /* Initialize max_apid/min_apid to the opposite bounds, during
1632 * the irq domain translation, we are sure to update these */
1633 pmic_arb->max_apid = 0;
1634 pmic_arb->min_apid = pmic_arb->max_periphs - 1;
1636 platform_set_drvdata(pdev, ctrl);
1637 raw_spin_lock_init(&pmic_arb->lock);
1639 ctrl->cmd = pmic_arb_cmd;
1640 ctrl->read_cmd = pmic_arb_read_cmd;
1641 ctrl->write_cmd = pmic_arb_write_cmd;
1643 if (hw_ver >= PMIC_ARB_VERSION_V5_MIN) {
1644 err = pmic_arb_read_apid_map_v5(pmic_arb);
1646 dev_err(&pdev->dev, "could not read APID->PPID mapping table, rc= %d\n",
1652 dev_dbg(&pdev->dev, "adding irq domain\n");
1653 pmic_arb->domain = irq_domain_add_tree(pdev->dev.of_node,
1654 &pmic_arb_irq_domain_ops, pmic_arb);
1655 if (!pmic_arb->domain) {
1656 dev_err(&pdev->dev, "unable to create irq_domain\n");
1661 irq_set_chained_handler_and_data(pmic_arb->irq, pmic_arb_chained_irq,
1663 err = spmi_controller_add(ctrl);
1665 goto err_domain_remove;
1670 irq_set_chained_handler_and_data(pmic_arb->irq, NULL, NULL);
1671 irq_domain_remove(pmic_arb->domain);
1673 spmi_controller_put(ctrl);
1677 static void spmi_pmic_arb_remove(struct platform_device *pdev)
1679 struct spmi_controller *ctrl = platform_get_drvdata(pdev);
1680 struct spmi_pmic_arb *pmic_arb = spmi_controller_get_drvdata(ctrl);
1681 spmi_controller_remove(ctrl);
1682 irq_set_chained_handler_and_data(pmic_arb->irq, NULL, NULL);
1683 irq_domain_remove(pmic_arb->domain);
1684 spmi_controller_put(ctrl);
1687 static const struct of_device_id spmi_pmic_arb_match_table[] = {
1688 { .compatible = "qcom,spmi-pmic-arb", },
1691 MODULE_DEVICE_TABLE(of, spmi_pmic_arb_match_table);
1693 static struct platform_driver spmi_pmic_arb_driver = {
1694 .probe = spmi_pmic_arb_probe,
1695 .remove_new = spmi_pmic_arb_remove,
1697 .name = "spmi_pmic_arb",
1698 .of_match_table = spmi_pmic_arb_match_table,
1701 module_platform_driver(spmi_pmic_arb_driver);
1703 MODULE_LICENSE("GPL v2");
1704 MODULE_ALIAS("platform:spmi_pmic_arb");