2 * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 #include <linux/bitmap.h>
14 #include <linux/delay.h>
15 #include <linux/err.h>
16 #include <linux/interrupt.h>
18 #include <linux/irqchip/chained_irq.h>
19 #include <linux/irqdomain.h>
20 #include <linux/irq.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/slab.h>
26 #include <linux/spmi.h>
28 /* PMIC Arbiter configuration registers */
29 #define PMIC_ARB_VERSION 0x0000
30 #define PMIC_ARB_VERSION_V2_MIN 0x20010000
31 #define PMIC_ARB_INT_EN 0x0004
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
41 #define PMIC_ARB_REG_CHNL(N) (0x800 + 0x4 * (N))
44 #define SPMI_MAPPING_TABLE_REG(N) (0x0B00 + (4 * (N)))
45 #define SPMI_MAPPING_BIT_INDEX(X) (((X) >> 18) & 0xF)
46 #define SPMI_MAPPING_BIT_IS_0_FLAG(X) (((X) >> 17) & 0x1)
47 #define SPMI_MAPPING_BIT_IS_0_RESULT(X) (((X) >> 9) & 0xFF)
48 #define SPMI_MAPPING_BIT_IS_1_FLAG(X) (((X) >> 8) & 0x1)
49 #define SPMI_MAPPING_BIT_IS_1_RESULT(X) (((X) >> 0) & 0xFF)
51 #define SPMI_MAPPING_TABLE_TREE_DEPTH 16 /* Maximum of 16-bits */
52 #define PMIC_ARB_MAX_PPID BIT(12) /* PPID is 12bit */
53 #define PMIC_ARB_CHAN_VALID BIT(15)
56 #define SPMI_OWNERSHIP_TABLE_REG(N) (0x0700 + (4 * (N)))
57 #define SPMI_OWNERSHIP_PERIPH2OWNER(X) ((X) & 0x7)
59 /* Channel Status fields */
60 enum pmic_arb_chnl_status
{
61 PMIC_ARB_STATUS_DONE
= (1 << 0),
62 PMIC_ARB_STATUS_FAILURE
= (1 << 1),
63 PMIC_ARB_STATUS_DENIED
= (1 << 2),
64 PMIC_ARB_STATUS_DROPPED
= (1 << 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,
88 /* Maximum number of support PMIC peripherals */
89 #define PMIC_ARB_MAX_PERIPHS 512
90 #define PMIC_ARB_TIMEOUT_US 100
91 #define PMIC_ARB_MAX_TRANS_BYTES (8)
93 #define PMIC_ARB_APID_MASK 0xFF
94 #define PMIC_ARB_PPID_MASK 0xFFF
96 /* interrupt enable bit */
97 #define SPMI_PIC_ACC_ENABLE_BIT BIT(0)
99 struct pmic_arb_ver_ops
;
102 * spmi_pmic_arb_dev - SPMI PMIC Arbiter object
104 * @rd_base: on v1 "core", on v2 "observer" register base off DT.
105 * @wr_base: on v1 "core", on v2 "chnls" register base off DT.
106 * @intr: address of the SPMI interrupt control registers.
107 * @cnfg: address of the PMIC Arbiter configuration registers.
108 * @lock: lock to synchronize accesses.
109 * @channel: execution environment channel to use for accesses.
110 * @irq: PMIC ARB interrupt.
111 * @ee: the current Execution Environment
112 * @min_apid: minimum APID (used for bounding IRQ search)
113 * @max_apid: maximum APID
114 * @mapping_table: in-memory copy of PPID -> APID mapping table.
115 * @domain: irq domain object for PMIC IRQ domain
116 * @spmic: SPMI controller object
117 * @apid_to_ppid: in-memory copy of APID -> PPID mapping table.
118 * @ver_ops: version dependent operations.
119 * @ppid_to_chan in-memory copy of PPID -> channel (APID) mapping table.
122 struct spmi_pmic_arb_dev
{
123 void __iomem
*rd_base
;
124 void __iomem
*wr_base
;
128 resource_size_t core_size
;
136 DECLARE_BITMAP(mapping_table_valid
, PMIC_ARB_MAX_PERIPHS
);
137 struct irq_domain
*domain
;
138 struct spmi_controller
*spmic
;
140 const struct pmic_arb_ver_ops
*ver_ops
;
146 * pmic_arb_ver: version dependent functionality.
148 * @non_data_cmd: on v1 issues an spmi non-data command.
149 * on v2 no HW support, returns -EOPNOTSUPP.
150 * @offset: on v1 offset of per-ee channel.
151 * on v2 offset of per-ee and per-ppid channel.
152 * @fmt_cmd: formats a GENI/SPMI command.
153 * @owner_acc_status: on v1 offset of PMIC_ARB_SPMI_PIC_OWNERm_ACC_STATUSn
154 * on v2 offset of SPMI_PIC_OWNERm_ACC_STATUSn.
155 * @acc_enable: on v1 offset of PMIC_ARB_SPMI_PIC_ACC_ENABLEn
156 * on v2 offset of SPMI_PIC_ACC_ENABLEn.
157 * @irq_status: on v1 offset of PMIC_ARB_SPMI_PIC_IRQ_STATUSn
158 * on v2 offset of SPMI_PIC_IRQ_STATUSn.
159 * @irq_clear: on v1 offset of PMIC_ARB_SPMI_PIC_IRQ_CLEARn
160 * on v2 offset of SPMI_PIC_IRQ_CLEARn.
162 struct pmic_arb_ver_ops
{
163 /* spmi commands (read_cmd, write_cmd, cmd) functionality */
164 int (*offset
)(struct spmi_pmic_arb_dev
*dev
, u8 sid
, u16 addr
,
166 u32 (*fmt_cmd
)(u8 opc
, u8 sid
, u16 addr
, u8 bc
);
167 int (*non_data_cmd
)(struct spmi_controller
*ctrl
, u8 opc
, u8 sid
);
168 /* Interrupts controller functionality (offset of PIC registers) */
169 u32 (*owner_acc_status
)(u8 m
, u8 n
);
170 u32 (*acc_enable
)(u8 n
);
171 u32 (*irq_status
)(u8 n
);
172 u32 (*irq_clear
)(u8 n
);
175 static inline void pmic_arb_base_write(struct spmi_pmic_arb_dev
*dev
,
178 writel_relaxed(val
, dev
->wr_base
+ offset
);
181 static inline void pmic_arb_set_rd_cmd(struct spmi_pmic_arb_dev
*dev
,
184 writel_relaxed(val
, dev
->rd_base
+ offset
);
188 * pa_read_data: reads pmic-arb's register and copy 1..4 bytes to buf
189 * @bc: byte count -1. range: 0..3
190 * @reg: register's address
191 * @buf: output parameter, length must be bc + 1
193 static void pa_read_data(struct spmi_pmic_arb_dev
*dev
, u8
*buf
, u32 reg
, u8 bc
)
195 u32 data
= __raw_readl(dev
->rd_base
+ reg
);
196 memcpy(buf
, &data
, (bc
& 3) + 1);
200 * pa_write_data: write 1..4 bytes from buf to pmic-arb's register
201 * @bc: byte-count -1. range: 0..3.
202 * @reg: register's address.
203 * @buf: buffer to write. length must be bc + 1.
206 pa_write_data(struct spmi_pmic_arb_dev
*dev
, const u8
*buf
, u32 reg
, u8 bc
)
209 memcpy(&data
, buf
, (bc
& 3) + 1);
210 __raw_writel(data
, dev
->wr_base
+ reg
);
213 static int pmic_arb_wait_for_done(struct spmi_controller
*ctrl
,
214 void __iomem
*base
, u8 sid
, u16 addr
)
216 struct spmi_pmic_arb_dev
*dev
= spmi_controller_get_drvdata(ctrl
);
218 u32 timeout
= PMIC_ARB_TIMEOUT_US
;
222 rc
= dev
->ver_ops
->offset(dev
, sid
, addr
, &offset
);
226 offset
+= PMIC_ARB_STATUS
;
229 status
= readl_relaxed(base
+ offset
);
231 if (status
& PMIC_ARB_STATUS_DONE
) {
232 if (status
& PMIC_ARB_STATUS_DENIED
) {
234 "%s: transaction denied (0x%x)\n",
239 if (status
& PMIC_ARB_STATUS_FAILURE
) {
241 "%s: transaction failed (0x%x)\n",
246 if (status
& PMIC_ARB_STATUS_DROPPED
) {
248 "%s: transaction dropped (0x%x)\n",
259 "%s: timeout, status 0x%x\n",
265 pmic_arb_non_data_cmd_v1(struct spmi_controller
*ctrl
, u8 opc
, u8 sid
)
267 struct spmi_pmic_arb_dev
*pmic_arb
= spmi_controller_get_drvdata(ctrl
);
273 rc
= pmic_arb
->ver_ops
->offset(pmic_arb
, sid
, 0, &offset
);
277 cmd
= ((opc
| 0x40) << 27) | ((sid
& 0xf) << 20);
279 raw_spin_lock_irqsave(&pmic_arb
->lock
, flags
);
280 pmic_arb_base_write(pmic_arb
, offset
+ PMIC_ARB_CMD
, cmd
);
281 rc
= pmic_arb_wait_for_done(ctrl
, pmic_arb
->wr_base
, sid
, 0);
282 raw_spin_unlock_irqrestore(&pmic_arb
->lock
, flags
);
288 pmic_arb_non_data_cmd_v2(struct spmi_controller
*ctrl
, u8 opc
, u8 sid
)
293 /* Non-data command */
294 static int pmic_arb_cmd(struct spmi_controller
*ctrl
, u8 opc
, u8 sid
)
296 struct spmi_pmic_arb_dev
*pmic_arb
= spmi_controller_get_drvdata(ctrl
);
298 dev_dbg(&ctrl
->dev
, "cmd op:0x%x sid:%d\n", opc
, sid
);
300 /* Check for valid non-data command */
301 if (opc
< SPMI_CMD_RESET
|| opc
> SPMI_CMD_WAKEUP
)
304 return pmic_arb
->ver_ops
->non_data_cmd(ctrl
, opc
, sid
);
307 static int pmic_arb_read_cmd(struct spmi_controller
*ctrl
, u8 opc
, u8 sid
,
308 u16 addr
, u8
*buf
, size_t len
)
310 struct spmi_pmic_arb_dev
*pmic_arb
= spmi_controller_get_drvdata(ctrl
);
317 rc
= pmic_arb
->ver_ops
->offset(pmic_arb
, sid
, addr
, &offset
);
321 if (bc
>= PMIC_ARB_MAX_TRANS_BYTES
) {
323 "pmic-arb supports 1..%d bytes per trans, but:%zu requested",
324 PMIC_ARB_MAX_TRANS_BYTES
, len
);
328 /* Check the opcode */
329 if (opc
>= 0x60 && opc
<= 0x7F)
330 opc
= PMIC_ARB_OP_READ
;
331 else if (opc
>= 0x20 && opc
<= 0x2F)
332 opc
= PMIC_ARB_OP_EXT_READ
;
333 else if (opc
>= 0x38 && opc
<= 0x3F)
334 opc
= PMIC_ARB_OP_EXT_READL
;
338 cmd
= pmic_arb
->ver_ops
->fmt_cmd(opc
, sid
, addr
, bc
);
340 raw_spin_lock_irqsave(&pmic_arb
->lock
, flags
);
341 pmic_arb_set_rd_cmd(pmic_arb
, offset
+ PMIC_ARB_CMD
, cmd
);
342 rc
= pmic_arb_wait_for_done(ctrl
, pmic_arb
->rd_base
, sid
, addr
);
346 pa_read_data(pmic_arb
, buf
, offset
+ PMIC_ARB_RDATA0
,
350 pa_read_data(pmic_arb
, buf
+ 4,
351 offset
+ PMIC_ARB_RDATA1
, bc
- 4);
354 raw_spin_unlock_irqrestore(&pmic_arb
->lock
, flags
);
358 static int pmic_arb_write_cmd(struct spmi_controller
*ctrl
, u8 opc
, u8 sid
,
359 u16 addr
, const u8
*buf
, size_t len
)
361 struct spmi_pmic_arb_dev
*pmic_arb
= spmi_controller_get_drvdata(ctrl
);
368 rc
= pmic_arb
->ver_ops
->offset(pmic_arb
, sid
, addr
, &offset
);
372 if (bc
>= PMIC_ARB_MAX_TRANS_BYTES
) {
374 "pmic-arb supports 1..%d bytes per trans, but:%zu requested",
375 PMIC_ARB_MAX_TRANS_BYTES
, len
);
379 /* Check the opcode */
380 if (opc
>= 0x40 && opc
<= 0x5F)
381 opc
= PMIC_ARB_OP_WRITE
;
382 else if (opc
>= 0x00 && opc
<= 0x0F)
383 opc
= PMIC_ARB_OP_EXT_WRITE
;
384 else if (opc
>= 0x30 && opc
<= 0x37)
385 opc
= PMIC_ARB_OP_EXT_WRITEL
;
386 else if (opc
>= 0x80)
387 opc
= PMIC_ARB_OP_ZERO_WRITE
;
391 cmd
= pmic_arb
->ver_ops
->fmt_cmd(opc
, sid
, addr
, bc
);
393 /* Write data to FIFOs */
394 raw_spin_lock_irqsave(&pmic_arb
->lock
, flags
);
395 pa_write_data(pmic_arb
, buf
, offset
+ PMIC_ARB_WDATA0
,
398 pa_write_data(pmic_arb
, buf
+ 4,
399 offset
+ PMIC_ARB_WDATA1
, bc
- 4);
401 /* Start the transaction */
402 pmic_arb_base_write(pmic_arb
, offset
+ PMIC_ARB_CMD
, cmd
);
403 rc
= pmic_arb_wait_for_done(ctrl
, pmic_arb
->wr_base
, sid
, addr
);
404 raw_spin_unlock_irqrestore(&pmic_arb
->lock
, flags
);
410 QPNPINT_REG_RT_STS
= 0x10,
411 QPNPINT_REG_SET_TYPE
= 0x11,
412 QPNPINT_REG_POLARITY_HIGH
= 0x12,
413 QPNPINT_REG_POLARITY_LOW
= 0x13,
414 QPNPINT_REG_LATCHED_CLR
= 0x14,
415 QPNPINT_REG_EN_SET
= 0x15,
416 QPNPINT_REG_EN_CLR
= 0x16,
417 QPNPINT_REG_LATCHED_STS
= 0x18,
420 struct spmi_pmic_arb_qpnpint_type
{
421 u8 type
; /* 1 -> edge */
426 /* Simplified accessor functions for irqchip callbacks */
427 static void qpnpint_spmi_write(struct irq_data
*d
, u8 reg
, void *buf
,
430 struct spmi_pmic_arb_dev
*pa
= irq_data_get_irq_chip_data(d
);
431 u8 sid
= d
->hwirq
>> 24;
432 u8 per
= d
->hwirq
>> 16;
434 if (pmic_arb_write_cmd(pa
->spmic
, SPMI_CMD_EXT_WRITEL
, sid
,
435 (per
<< 8) + reg
, buf
, len
))
436 dev_err_ratelimited(&pa
->spmic
->dev
,
437 "failed irqchip transaction on %x\n",
441 static void qpnpint_spmi_read(struct irq_data
*d
, u8 reg
, void *buf
, size_t len
)
443 struct spmi_pmic_arb_dev
*pa
= irq_data_get_irq_chip_data(d
);
444 u8 sid
= d
->hwirq
>> 24;
445 u8 per
= d
->hwirq
>> 16;
447 if (pmic_arb_read_cmd(pa
->spmic
, SPMI_CMD_EXT_READL
, sid
,
448 (per
<< 8) + reg
, buf
, len
))
449 dev_err_ratelimited(&pa
->spmic
->dev
,
450 "failed irqchip transaction on %x\n",
454 static void periph_interrupt(struct spmi_pmic_arb_dev
*pa
, u8 apid
)
460 status
= readl_relaxed(pa
->intr
+ pa
->ver_ops
->irq_status(apid
));
462 id
= ffs(status
) - 1;
463 status
&= ~(1 << id
);
464 irq
= irq_find_mapping(pa
->domain
,
465 pa
->apid_to_ppid
[apid
] << 16
468 generic_handle_irq(irq
);
472 static void pmic_arb_chained_irq(struct irq_desc
*desc
)
474 struct spmi_pmic_arb_dev
*pa
= irq_desc_get_handler_data(desc
);
475 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
476 void __iomem
*intr
= pa
->intr
;
477 int first
= pa
->min_apid
>> 5;
478 int last
= pa
->max_apid
>> 5;
482 chained_irq_enter(chip
, desc
);
484 for (i
= first
; i
<= last
; ++i
) {
485 status
= readl_relaxed(intr
+
486 pa
->ver_ops
->owner_acc_status(pa
->ee
, i
));
488 id
= ffs(status
) - 1;
489 status
&= ~(1 << id
);
490 periph_interrupt(pa
, id
+ i
* 32);
494 chained_irq_exit(chip
, desc
);
497 static void qpnpint_irq_ack(struct irq_data
*d
)
499 struct spmi_pmic_arb_dev
*pa
= irq_data_get_irq_chip_data(d
);
500 u8 irq
= d
->hwirq
>> 8;
505 raw_spin_lock_irqsave(&pa
->lock
, flags
);
506 writel_relaxed(1 << irq
, pa
->intr
+ pa
->ver_ops
->irq_clear(apid
));
507 raw_spin_unlock_irqrestore(&pa
->lock
, flags
);
510 qpnpint_spmi_write(d
, QPNPINT_REG_LATCHED_CLR
, &data
, 1);
513 static void qpnpint_irq_mask(struct irq_data
*d
)
515 struct spmi_pmic_arb_dev
*pa
= irq_data_get_irq_chip_data(d
);
516 u8 irq
= d
->hwirq
>> 8;
522 raw_spin_lock_irqsave(&pa
->lock
, flags
);
523 status
= readl_relaxed(pa
->intr
+ pa
->ver_ops
->acc_enable(apid
));
524 if (status
& SPMI_PIC_ACC_ENABLE_BIT
) {
525 status
= status
& ~SPMI_PIC_ACC_ENABLE_BIT
;
526 writel_relaxed(status
, pa
->intr
+
527 pa
->ver_ops
->acc_enable(apid
));
529 raw_spin_unlock_irqrestore(&pa
->lock
, flags
);
532 qpnpint_spmi_write(d
, QPNPINT_REG_EN_CLR
, &data
, 1);
535 static void qpnpint_irq_unmask(struct irq_data
*d
)
537 struct spmi_pmic_arb_dev
*pa
= irq_data_get_irq_chip_data(d
);
538 u8 irq
= d
->hwirq
>> 8;
544 raw_spin_lock_irqsave(&pa
->lock
, flags
);
545 status
= readl_relaxed(pa
->intr
+ pa
->ver_ops
->acc_enable(apid
));
546 if (!(status
& SPMI_PIC_ACC_ENABLE_BIT
)) {
547 writel_relaxed(status
| SPMI_PIC_ACC_ENABLE_BIT
,
548 pa
->intr
+ pa
->ver_ops
->acc_enable(apid
));
550 raw_spin_unlock_irqrestore(&pa
->lock
, flags
);
553 qpnpint_spmi_write(d
, QPNPINT_REG_EN_SET
, &data
, 1);
556 static void qpnpint_irq_enable(struct irq_data
*d
)
558 u8 irq
= d
->hwirq
>> 8;
561 qpnpint_irq_unmask(d
);
564 qpnpint_spmi_write(d
, QPNPINT_REG_LATCHED_CLR
, &data
, 1);
567 static int qpnpint_irq_set_type(struct irq_data
*d
, unsigned int flow_type
)
569 struct spmi_pmic_arb_qpnpint_type type
;
570 u8 irq
= d
->hwirq
>> 8;
572 qpnpint_spmi_read(d
, QPNPINT_REG_SET_TYPE
, &type
, sizeof(type
));
574 if (flow_type
& (IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
)) {
575 type
.type
|= 1 << irq
;
576 if (flow_type
& IRQF_TRIGGER_RISING
)
577 type
.polarity_high
|= 1 << irq
;
578 if (flow_type
& IRQF_TRIGGER_FALLING
)
579 type
.polarity_low
|= 1 << irq
;
581 if ((flow_type
& (IRQF_TRIGGER_HIGH
)) &&
582 (flow_type
& (IRQF_TRIGGER_LOW
)))
585 type
.type
&= ~(1 << irq
); /* level trig */
586 if (flow_type
& IRQF_TRIGGER_HIGH
)
587 type
.polarity_high
|= 1 << irq
;
589 type
.polarity_low
|= 1 << irq
;
592 qpnpint_spmi_write(d
, QPNPINT_REG_SET_TYPE
, &type
, sizeof(type
));
596 static int qpnpint_get_irqchip_state(struct irq_data
*d
,
597 enum irqchip_irq_state which
,
600 u8 irq
= d
->hwirq
>> 8;
603 if (which
!= IRQCHIP_STATE_LINE_LEVEL
)
606 qpnpint_spmi_read(d
, QPNPINT_REG_RT_STS
, &status
, 1);
607 *state
= !!(status
& BIT(irq
));
612 static struct irq_chip pmic_arb_irqchip
= {
614 .irq_enable
= qpnpint_irq_enable
,
615 .irq_ack
= qpnpint_irq_ack
,
616 .irq_mask
= qpnpint_irq_mask
,
617 .irq_unmask
= qpnpint_irq_unmask
,
618 .irq_set_type
= qpnpint_irq_set_type
,
619 .irq_get_irqchip_state
= qpnpint_get_irqchip_state
,
620 .flags
= IRQCHIP_MASK_ON_SUSPEND
621 | IRQCHIP_SKIP_SET_WAKE
,
624 struct spmi_pmic_arb_irq_spec
{
630 static int search_mapping_table(struct spmi_pmic_arb_dev
*pa
,
631 struct spmi_pmic_arb_irq_spec
*spec
,
634 u16 ppid
= spec
->slave
<< 8 | spec
->per
;
635 u32
*mapping_table
= pa
->mapping_table
;
639 for (i
= 0; i
< SPMI_MAPPING_TABLE_TREE_DEPTH
; ++i
) {
640 if (!test_and_set_bit(index
, pa
->mapping_table_valid
))
641 mapping_table
[index
] = readl_relaxed(pa
->cnfg
+
642 SPMI_MAPPING_TABLE_REG(index
));
644 data
= mapping_table
[index
];
646 if (ppid
& (1 << SPMI_MAPPING_BIT_INDEX(data
))) {
647 if (SPMI_MAPPING_BIT_IS_1_FLAG(data
)) {
648 index
= SPMI_MAPPING_BIT_IS_1_RESULT(data
);
650 *apid
= SPMI_MAPPING_BIT_IS_1_RESULT(data
);
654 if (SPMI_MAPPING_BIT_IS_0_FLAG(data
)) {
655 index
= SPMI_MAPPING_BIT_IS_0_RESULT(data
);
657 *apid
= SPMI_MAPPING_BIT_IS_0_RESULT(data
);
666 static int qpnpint_irq_domain_dt_translate(struct irq_domain
*d
,
667 struct device_node
*controller
,
669 unsigned int intsize
,
670 unsigned long *out_hwirq
,
671 unsigned int *out_type
)
673 struct spmi_pmic_arb_dev
*pa
= d
->host_data
;
674 struct spmi_pmic_arb_irq_spec spec
;
678 dev_dbg(&pa
->spmic
->dev
,
679 "intspec[0] 0x%1x intspec[1] 0x%02x intspec[2] 0x%02x\n",
680 intspec
[0], intspec
[1], intspec
[2]);
682 if (irq_domain_get_of_node(d
) != controller
)
686 if (intspec
[0] > 0xF || intspec
[1] > 0xFF || intspec
[2] > 0x7)
689 spec
.slave
= intspec
[0];
690 spec
.per
= intspec
[1];
691 spec
.irq
= intspec
[2];
693 err
= search_mapping_table(pa
, &spec
, &apid
);
697 pa
->apid_to_ppid
[apid
] = spec
.slave
<< 8 | spec
.per
;
699 /* Keep track of {max,min}_apid for bounding search during interrupt */
700 if (apid
> pa
->max_apid
)
702 if (apid
< pa
->min_apid
)
705 *out_hwirq
= spec
.slave
<< 24
709 *out_type
= intspec
[3] & IRQ_TYPE_SENSE_MASK
;
711 dev_dbg(&pa
->spmic
->dev
, "out_hwirq = %lu\n", *out_hwirq
);
716 static int qpnpint_irq_domain_map(struct irq_domain
*d
,
718 irq_hw_number_t hwirq
)
720 struct spmi_pmic_arb_dev
*pa
= d
->host_data
;
722 dev_dbg(&pa
->spmic
->dev
, "virq = %u, hwirq = %lu\n", virq
, hwirq
);
724 irq_set_chip_and_handler(virq
, &pmic_arb_irqchip
, handle_level_irq
);
725 irq_set_chip_data(virq
, d
->host_data
);
726 irq_set_noprobe(virq
);
730 /* v1 offset per ee */
732 pmic_arb_offset_v1(struct spmi_pmic_arb_dev
*pa
, u8 sid
, u16 addr
, u32
*offset
)
734 *offset
= 0x800 + 0x80 * pa
->channel
;
738 static u16
pmic_arb_find_chan(struct spmi_pmic_arb_dev
*pa
, u16 ppid
)
745 * PMIC_ARB_REG_CHNL is a table in HW mapping channel to ppid.
746 * ppid_to_chan is an in-memory invert of that table.
748 for (chan
= pa
->last_channel
; ; chan
++) {
749 offset
= PMIC_ARB_REG_CHNL(chan
);
750 if (offset
>= pa
->core_size
)
753 regval
= readl_relaxed(pa
->core
+ offset
);
757 id
= (regval
>> 8) & PMIC_ARB_PPID_MASK
;
758 pa
->ppid_to_chan
[id
] = chan
| PMIC_ARB_CHAN_VALID
;
760 chan
|= PMIC_ARB_CHAN_VALID
;
764 pa
->last_channel
= chan
& ~PMIC_ARB_CHAN_VALID
;
770 /* v2 offset per ppid (chan) and per ee */
772 pmic_arb_offset_v2(struct spmi_pmic_arb_dev
*pa
, u8 sid
, u16 addr
, u32
*offset
)
774 u16 ppid
= (sid
<< 8) | (addr
>> 8);
777 chan
= pa
->ppid_to_chan
[ppid
];
778 if (!(chan
& PMIC_ARB_CHAN_VALID
))
779 chan
= pmic_arb_find_chan(pa
, ppid
);
780 if (!(chan
& PMIC_ARB_CHAN_VALID
))
782 chan
&= ~PMIC_ARB_CHAN_VALID
;
784 *offset
= 0x1000 * pa
->ee
+ 0x8000 * chan
;
788 static u32
pmic_arb_fmt_cmd_v1(u8 opc
, u8 sid
, u16 addr
, u8 bc
)
790 return (opc
<< 27) | ((sid
& 0xf) << 20) | (addr
<< 4) | (bc
& 0x7);
793 static u32
pmic_arb_fmt_cmd_v2(u8 opc
, u8 sid
, u16 addr
, u8 bc
)
795 return (opc
<< 27) | ((addr
& 0xff) << 4) | (bc
& 0x7);
798 static u32
pmic_arb_owner_acc_status_v1(u8 m
, u8 n
)
800 return 0x20 * m
+ 0x4 * n
;
803 static u32
pmic_arb_owner_acc_status_v2(u8 m
, u8 n
)
805 return 0x100000 + 0x1000 * m
+ 0x4 * n
;
808 static u32
pmic_arb_acc_enable_v1(u8 n
)
810 return 0x200 + 0x4 * n
;
813 static u32
pmic_arb_acc_enable_v2(u8 n
)
818 static u32
pmic_arb_irq_status_v1(u8 n
)
820 return 0x600 + 0x4 * n
;
823 static u32
pmic_arb_irq_status_v2(u8 n
)
825 return 0x4 + 0x1000 * n
;
828 static u32
pmic_arb_irq_clear_v1(u8 n
)
830 return 0xA00 + 0x4 * n
;
833 static u32
pmic_arb_irq_clear_v2(u8 n
)
835 return 0x8 + 0x1000 * n
;
838 static const struct pmic_arb_ver_ops pmic_arb_v1
= {
839 .non_data_cmd
= pmic_arb_non_data_cmd_v1
,
840 .offset
= pmic_arb_offset_v1
,
841 .fmt_cmd
= pmic_arb_fmt_cmd_v1
,
842 .owner_acc_status
= pmic_arb_owner_acc_status_v1
,
843 .acc_enable
= pmic_arb_acc_enable_v1
,
844 .irq_status
= pmic_arb_irq_status_v1
,
845 .irq_clear
= pmic_arb_irq_clear_v1
,
848 static const struct pmic_arb_ver_ops pmic_arb_v2
= {
849 .non_data_cmd
= pmic_arb_non_data_cmd_v2
,
850 .offset
= pmic_arb_offset_v2
,
851 .fmt_cmd
= pmic_arb_fmt_cmd_v2
,
852 .owner_acc_status
= pmic_arb_owner_acc_status_v2
,
853 .acc_enable
= pmic_arb_acc_enable_v2
,
854 .irq_status
= pmic_arb_irq_status_v2
,
855 .irq_clear
= pmic_arb_irq_clear_v2
,
858 static const struct irq_domain_ops pmic_arb_irq_domain_ops
= {
859 .map
= qpnpint_irq_domain_map
,
860 .xlate
= qpnpint_irq_domain_dt_translate
,
863 static int spmi_pmic_arb_probe(struct platform_device
*pdev
)
865 struct spmi_pmic_arb_dev
*pa
;
866 struct spmi_controller
*ctrl
;
867 struct resource
*res
;
869 u32 channel
, ee
, hw_ver
;
873 ctrl
= spmi_controller_alloc(&pdev
->dev
, sizeof(*pa
));
877 pa
= spmi_controller_get_drvdata(ctrl
);
880 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "core");
881 pa
->core_size
= resource_size(res
);
882 core
= devm_ioremap_resource(&ctrl
->dev
, res
);
888 hw_ver
= readl_relaxed(core
+ PMIC_ARB_VERSION
);
889 is_v1
= (hw_ver
< PMIC_ARB_VERSION_V2_MIN
);
891 dev_info(&ctrl
->dev
, "PMIC Arb Version-%d (0x%x)\n", (is_v1
? 1 : 2),
895 pa
->ver_ops
= &pmic_arb_v1
;
900 pa
->ver_ops
= &pmic_arb_v2
;
902 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
,
904 pa
->rd_base
= devm_ioremap_resource(&ctrl
->dev
, res
);
905 if (IS_ERR(pa
->rd_base
)) {
906 err
= PTR_ERR(pa
->rd_base
);
910 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
,
912 pa
->wr_base
= devm_ioremap_resource(&ctrl
->dev
, res
);
913 if (IS_ERR(pa
->wr_base
)) {
914 err
= PTR_ERR(pa
->wr_base
);
918 pa
->ppid_to_chan
= devm_kcalloc(&ctrl
->dev
,
920 sizeof(*pa
->ppid_to_chan
),
922 if (!pa
->ppid_to_chan
) {
928 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "intr");
929 pa
->intr
= devm_ioremap_resource(&ctrl
->dev
, res
);
930 if (IS_ERR(pa
->intr
)) {
931 err
= PTR_ERR(pa
->intr
);
935 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "cnfg");
936 pa
->cnfg
= devm_ioremap_resource(&ctrl
->dev
, res
);
937 if (IS_ERR(pa
->cnfg
)) {
938 err
= PTR_ERR(pa
->cnfg
);
942 pa
->irq
= platform_get_irq_byname(pdev
, "periph_irq");
948 err
= of_property_read_u32(pdev
->dev
.of_node
, "qcom,channel", &channel
);
950 dev_err(&pdev
->dev
, "channel unspecified.\n");
955 dev_err(&pdev
->dev
, "invalid channel (%u) specified.\n",
960 pa
->channel
= channel
;
962 err
= of_property_read_u32(pdev
->dev
.of_node
, "qcom,ee", &ee
);
964 dev_err(&pdev
->dev
, "EE unspecified.\n");
969 dev_err(&pdev
->dev
, "invalid EE (%u) specified\n", ee
);
976 pa
->apid_to_ppid
= devm_kcalloc(&ctrl
->dev
, PMIC_ARB_MAX_PERIPHS
,
977 sizeof(*pa
->apid_to_ppid
),
979 if (!pa
->apid_to_ppid
) {
984 pa
->mapping_table
= devm_kcalloc(&ctrl
->dev
, PMIC_ARB_MAX_PERIPHS
- 1,
985 sizeof(*pa
->mapping_table
), GFP_KERNEL
);
986 if (!pa
->mapping_table
) {
991 /* Initialize max_apid/min_apid to the opposite bounds, during
992 * the irq domain translation, we are sure to update these */
994 pa
->min_apid
= PMIC_ARB_MAX_PERIPHS
- 1;
996 platform_set_drvdata(pdev
, ctrl
);
997 raw_spin_lock_init(&pa
->lock
);
999 ctrl
->cmd
= pmic_arb_cmd
;
1000 ctrl
->read_cmd
= pmic_arb_read_cmd
;
1001 ctrl
->write_cmd
= pmic_arb_write_cmd
;
1003 dev_dbg(&pdev
->dev
, "adding irq domain\n");
1004 pa
->domain
= irq_domain_add_tree(pdev
->dev
.of_node
,
1005 &pmic_arb_irq_domain_ops
, pa
);
1007 dev_err(&pdev
->dev
, "unable to create irq_domain\n");
1012 irq_set_chained_handler_and_data(pa
->irq
, pmic_arb_chained_irq
, pa
);
1014 err
= spmi_controller_add(ctrl
);
1016 goto err_domain_remove
;
1021 irq_set_chained_handler_and_data(pa
->irq
, NULL
, NULL
);
1022 irq_domain_remove(pa
->domain
);
1024 spmi_controller_put(ctrl
);
1028 static int spmi_pmic_arb_remove(struct platform_device
*pdev
)
1030 struct spmi_controller
*ctrl
= platform_get_drvdata(pdev
);
1031 struct spmi_pmic_arb_dev
*pa
= spmi_controller_get_drvdata(ctrl
);
1032 spmi_controller_remove(ctrl
);
1033 irq_set_chained_handler_and_data(pa
->irq
, NULL
, NULL
);
1034 irq_domain_remove(pa
->domain
);
1035 spmi_controller_put(ctrl
);
1039 static const struct of_device_id spmi_pmic_arb_match_table
[] = {
1040 { .compatible
= "qcom,spmi-pmic-arb", },
1043 MODULE_DEVICE_TABLE(of
, spmi_pmic_arb_match_table
);
1045 static struct platform_driver spmi_pmic_arb_driver
= {
1046 .probe
= spmi_pmic_arb_probe
,
1047 .remove
= spmi_pmic_arb_remove
,
1049 .name
= "spmi_pmic_arb",
1050 .of_match_table
= spmi_pmic_arb_match_table
,
1053 module_platform_driver(spmi_pmic_arb_driver
);
1055 MODULE_LICENSE("GPL v2");
1056 MODULE_ALIAS("platform:spmi_pmic_arb");