blob: 360b8218f322cabbe1f363c9fc13c21476d431ff [file] [log] [blame]
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001/*
Fenglin Wu53d296b2017-07-28 12:40:47 +05302 * Copyright (c) 2012-2015, 2017, The Linux Foundation. All rights reserved.
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06003 *
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.
7 *
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.
12 */
Stephen Boyd987a9f12015-11-17 16:13:55 -080013#include <linux/bitmap.h>
Kenneth Heitke39ae93e2014-02-12 13:44:24 -060014#include <linux/delay.h>
15#include <linux/err.h>
16#include <linux/interrupt.h>
17#include <linux/io.h>
Josh Cartwright67b563f2014-02-12 13:44:25 -060018#include <linux/irqchip/chained_irq.h>
19#include <linux/irqdomain.h>
20#include <linux/irq.h>
Kenneth Heitke39ae93e2014-02-12 13:44:24 -060021#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/of.h>
24#include <linux/platform_device.h>
25#include <linux/slab.h>
26#include <linux/spmi.h>
27
28/* PMIC Arbiter configuration registers */
29#define PMIC_ARB_VERSION 0x0000
Gilad Avidovd0c6ae42015-03-25 11:37:32 -060030#define PMIC_ARB_VERSION_V2_MIN 0x20010000
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +053031#define PMIC_ARB_VERSION_V3_MIN 0x30000000
David Collins40f318f2017-07-28 12:40:46 +053032#define PMIC_ARB_VERSION_V5_MIN 0x50000000
Kenneth Heitke39ae93e2014-02-12 13:44:24 -060033#define PMIC_ARB_INT_EN 0x0004
34
Gilad Avidovd0c6ae42015-03-25 11:37:32 -060035/* PMIC Arbiter channel registers offsets */
36#define PMIC_ARB_CMD 0x00
37#define PMIC_ARB_CONFIG 0x04
38#define PMIC_ARB_STATUS 0x08
39#define PMIC_ARB_WDATA0 0x10
40#define PMIC_ARB_WDATA1 0x14
41#define PMIC_ARB_RDATA0 0x18
42#define PMIC_ARB_RDATA1 0x1C
Kenneth Heitke39ae93e2014-02-12 13:44:24 -060043
44/* Mapping Table */
45#define SPMI_MAPPING_TABLE_REG(N) (0x0B00 + (4 * (N)))
46#define SPMI_MAPPING_BIT_INDEX(X) (((X) >> 18) & 0xF)
47#define SPMI_MAPPING_BIT_IS_0_FLAG(X) (((X) >> 17) & 0x1)
48#define SPMI_MAPPING_BIT_IS_0_RESULT(X) (((X) >> 9) & 0xFF)
49#define SPMI_MAPPING_BIT_IS_1_FLAG(X) (((X) >> 8) & 0x1)
50#define SPMI_MAPPING_BIT_IS_1_RESULT(X) (((X) >> 0) & 0xFF)
51
Kenneth Heitke39ae93e2014-02-12 13:44:24 -060052#define SPMI_MAPPING_TABLE_TREE_DEPTH 16 /* Maximum of 16-bits */
Stephen Boyd987a9f12015-11-17 16:13:55 -080053#define PMIC_ARB_MAX_PPID BIT(12) /* PPID is 12bit */
Kiran Gunda02abec32017-07-28 12:40:37 +053054#define PMIC_ARB_APID_VALID BIT(15)
David Collins40f318f2017-07-28 12:40:46 +053055#define PMIC_ARB_CHAN_IS_IRQ_OWNER(reg) ((reg) & BIT(24))
56#define INVALID_EE 0xFF
Kenneth Heitke39ae93e2014-02-12 13:44:24 -060057
58/* Ownership Table */
59#define SPMI_OWNERSHIP_TABLE_REG(N) (0x0700 + (4 * (N)))
60#define SPMI_OWNERSHIP_PERIPH2OWNER(X) ((X) & 0x7)
61
62/* Channel Status fields */
63enum pmic_arb_chnl_status {
Abhijeet Dharmapurikar111a10b2017-05-10 19:55:32 +053064 PMIC_ARB_STATUS_DONE = BIT(0),
65 PMIC_ARB_STATUS_FAILURE = BIT(1),
66 PMIC_ARB_STATUS_DENIED = BIT(2),
67 PMIC_ARB_STATUS_DROPPED = BIT(3),
Kenneth Heitke39ae93e2014-02-12 13:44:24 -060068};
69
70/* Command register fields */
71#define PMIC_ARB_CMD_MAX_BYTE_COUNT 8
72
73/* Command Opcodes */
74enum pmic_arb_cmd_op_code {
75 PMIC_ARB_OP_EXT_WRITEL = 0,
76 PMIC_ARB_OP_EXT_READL = 1,
77 PMIC_ARB_OP_EXT_WRITE = 2,
78 PMIC_ARB_OP_RESET = 3,
79 PMIC_ARB_OP_SLEEP = 4,
80 PMIC_ARB_OP_SHUTDOWN = 5,
81 PMIC_ARB_OP_WAKEUP = 6,
82 PMIC_ARB_OP_AUTHENTICATE = 7,
83 PMIC_ARB_OP_MSTR_READ = 8,
84 PMIC_ARB_OP_MSTR_WRITE = 9,
85 PMIC_ARB_OP_EXT_READ = 13,
86 PMIC_ARB_OP_WRITE = 14,
87 PMIC_ARB_OP_READ = 15,
88 PMIC_ARB_OP_ZERO_WRITE = 16,
89};
90
David Collins40f318f2017-07-28 12:40:46 +053091/*
92 * PMIC arbiter version 5 uses different register offsets for read/write vs
93 * observer channels.
94 */
95enum pmic_arb_channel {
96 PMIC_ARB_CHANNEL_RW,
97 PMIC_ARB_CHANNEL_OBS,
98};
99
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600100/* Maximum number of support PMIC peripherals */
Stephen Boyd987a9f12015-11-17 16:13:55 -0800101#define PMIC_ARB_MAX_PERIPHS 512
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600102#define PMIC_ARB_TIMEOUT_US 100
103#define PMIC_ARB_MAX_TRANS_BYTES (8)
104
105#define PMIC_ARB_APID_MASK 0xFF
106#define PMIC_ARB_PPID_MASK 0xFFF
107
108/* interrupt enable bit */
109#define SPMI_PIC_ACC_ENABLE_BIT BIT(0)
110
Kiran Gunda02abec32017-07-28 12:40:37 +0530111#define spec_to_hwirq(slave_id, periph_id, irq_id, apid) \
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +0530112 ((((slave_id) & 0xF) << 28) | \
113 (((periph_id) & 0xFF) << 20) | \
114 (((irq_id) & 0x7) << 16) | \
115 (((apid) & 0x1FF) << 0))
116
Kiran Gunda02abec32017-07-28 12:40:37 +0530117#define hwirq_to_sid(hwirq) (((hwirq) >> 28) & 0xF)
118#define hwirq_to_per(hwirq) (((hwirq) >> 20) & 0xFF)
119#define hwirq_to_irq(hwirq) (((hwirq) >> 16) & 0x7)
120#define hwirq_to_apid(hwirq) (((hwirq) >> 0) & 0x1FF)
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +0530121
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600122struct pmic_arb_ver_ops;
123
Abhijeet Dharmapurikar6bc546e2017-05-10 19:55:35 +0530124struct apid_data {
125 u16 ppid;
David Collins40f318f2017-07-28 12:40:46 +0530126 u8 write_ee;
127 u8 irq_ee;
Abhijeet Dharmapurikar6bc546e2017-05-10 19:55:35 +0530128};
129
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600130/**
Abhijeet Dharmapurikar111a10b2017-05-10 19:55:32 +0530131 * spmi_pmic_arb - SPMI PMIC Arbiter object
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600132 *
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600133 * @rd_base: on v1 "core", on v2 "observer" register base off DT.
134 * @wr_base: on v1 "core", on v2 "chnls" register base off DT.
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600135 * @intr: address of the SPMI interrupt control registers.
136 * @cnfg: address of the PMIC Arbiter configuration registers.
137 * @lock: lock to synchronize accesses.
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600138 * @channel: execution environment channel to use for accesses.
Josh Cartwright67b563f2014-02-12 13:44:25 -0600139 * @irq: PMIC ARB interrupt.
140 * @ee: the current Execution Environment
141 * @min_apid: minimum APID (used for bounding IRQ search)
142 * @max_apid: maximum APID
143 * @mapping_table: in-memory copy of PPID -> APID mapping table.
144 * @domain: irq domain object for PMIC IRQ domain
145 * @spmic: SPMI controller object
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600146 * @ver_ops: version dependent operations.
Kiran Gunda02abec32017-07-28 12:40:37 +0530147 * @ppid_to_apid in-memory copy of PPID -> APID mapping table.
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600148 */
Abhijeet Dharmapurikar111a10b2017-05-10 19:55:32 +0530149struct spmi_pmic_arb {
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600150 void __iomem *rd_base;
151 void __iomem *wr_base;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600152 void __iomem *intr;
153 void __iomem *cnfg;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800154 void __iomem *core;
155 resource_size_t core_size;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600156 raw_spinlock_t lock;
157 u8 channel;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600158 int irq;
159 u8 ee;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800160 u16 min_apid;
161 u16 max_apid;
162 u32 *mapping_table;
163 DECLARE_BITMAP(mapping_table_valid, PMIC_ARB_MAX_PERIPHS);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600164 struct irq_domain *domain;
165 struct spmi_controller *spmic;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600166 const struct pmic_arb_ver_ops *ver_ops;
Abhijeet Dharmapurikar1ef1ce42017-05-10 19:55:33 +0530167 u16 *ppid_to_apid;
168 u16 last_apid;
Abhijeet Dharmapurikar6bc546e2017-05-10 19:55:35 +0530169 struct apid_data apid_data[PMIC_ARB_MAX_PERIPHS];
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600170};
171
172/**
173 * pmic_arb_ver: version dependent functionality.
174 *
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +0530175 * @ver_str: version string.
176 * @ppid_to_apid: finds the apid for a given ppid.
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600177 * @non_data_cmd: on v1 issues an spmi non-data command.
178 * on v2 no HW support, returns -EOPNOTSUPP.
179 * @offset: on v1 offset of per-ee channel.
180 * on v2 offset of per-ee and per-ppid channel.
181 * @fmt_cmd: formats a GENI/SPMI command.
Kiran Gundae95d0732017-07-28 12:40:44 +0530182 * @owner_acc_status: on v1 address of PMIC_ARB_SPMI_PIC_OWNERm_ACC_STATUSn
183 * on v2 address of SPMI_PIC_OWNERm_ACC_STATUSn.
184 * @acc_enable: on v1 address of PMIC_ARB_SPMI_PIC_ACC_ENABLEn
185 * on v2 address of SPMI_PIC_ACC_ENABLEn.
186 * @irq_status: on v1 address of PMIC_ARB_SPMI_PIC_IRQ_STATUSn
187 * on v2 address of SPMI_PIC_IRQ_STATUSn.
188 * @irq_clear: on v1 address of PMIC_ARB_SPMI_PIC_IRQ_CLEARn
189 * on v2 address of SPMI_PIC_IRQ_CLEARn.
David Collins40f318f2017-07-28 12:40:46 +0530190 * @apid_map_offset: offset of PMIC_ARB_REG_CHNLn
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600191 */
192struct pmic_arb_ver_ops {
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +0530193 const char *ver_str;
Kiran Gundaff615ed2017-07-28 12:40:42 +0530194 int (*ppid_to_apid)(struct spmi_pmic_arb *pmic_arb, u16 ppid);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600195 /* spmi commands (read_cmd, write_cmd, cmd) functionality */
David Collins40f318f2017-07-28 12:40:46 +0530196 int (*offset)(struct spmi_pmic_arb *pmic_arb, u8 sid, u16 addr,
197 enum pmic_arb_channel ch_type);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600198 u32 (*fmt_cmd)(u8 opc, u8 sid, u16 addr, u8 bc);
199 int (*non_data_cmd)(struct spmi_controller *ctrl, u8 opc, u8 sid);
200 /* Interrupts controller functionality (offset of PIC registers) */
Kiran Gundae95d0732017-07-28 12:40:44 +0530201 void __iomem *(*owner_acc_status)(struct spmi_pmic_arb *pmic_arb, u8 m,
202 u16 n);
203 void __iomem *(*acc_enable)(struct spmi_pmic_arb *pmic_arb, u16 n);
204 void __iomem *(*irq_status)(struct spmi_pmic_arb *pmic_arb, u16 n);
205 void __iomem *(*irq_clear)(struct spmi_pmic_arb *pmic_arb, u16 n);
David Collins40f318f2017-07-28 12:40:46 +0530206 u32 (*apid_map_offset)(u16 n);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600207};
208
Kiran Gunda02abec32017-07-28 12:40:37 +0530209static inline void pmic_arb_base_write(struct spmi_pmic_arb *pmic_arb,
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600210 u32 offset, u32 val)
211{
Kiran Gunda02abec32017-07-28 12:40:37 +0530212 writel_relaxed(val, pmic_arb->wr_base + offset);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600213}
214
Kiran Gunda02abec32017-07-28 12:40:37 +0530215static inline void pmic_arb_set_rd_cmd(struct spmi_pmic_arb *pmic_arb,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600216 u32 offset, u32 val)
217{
Kiran Gunda02abec32017-07-28 12:40:37 +0530218 writel_relaxed(val, pmic_arb->rd_base + offset);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600219}
220
221/**
Kiran Gunda02abec32017-07-28 12:40:37 +0530222 * pmic_arb_read_data: reads pmic-arb's register and copy 1..4 bytes to buf
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600223 * @bc: byte count -1. range: 0..3
224 * @reg: register's address
225 * @buf: output parameter, length must be bc + 1
226 */
Kiran Gunda02abec32017-07-28 12:40:37 +0530227static void
228pmic_arb_read_data(struct spmi_pmic_arb *pmic_arb, u8 *buf, u32 reg, u8 bc)
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600229{
Kiran Gunda02abec32017-07-28 12:40:37 +0530230 u32 data = __raw_readl(pmic_arb->rd_base + reg);
Abhijeet Dharmapurikar111a10b2017-05-10 19:55:32 +0530231
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600232 memcpy(buf, &data, (bc & 3) + 1);
233}
234
235/**
Kiran Gunda02abec32017-07-28 12:40:37 +0530236 * pmic_arb_write_data: write 1..4 bytes from buf to pmic-arb's register
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600237 * @bc: byte-count -1. range: 0..3.
238 * @reg: register's address.
239 * @buf: buffer to write. length must be bc + 1.
240 */
Kiran Gunda02abec32017-07-28 12:40:37 +0530241static void pmic_arb_write_data(struct spmi_pmic_arb *pmic_arb, const u8 *buf,
242 u32 reg, u8 bc)
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600243{
244 u32 data = 0;
Abhijeet Dharmapurikar111a10b2017-05-10 19:55:32 +0530245
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600246 memcpy(&data, buf, (bc & 3) + 1);
Kiran Gunda9f7a9a42017-07-28 12:40:41 +0530247 __raw_writel(data, pmic_arb->wr_base + reg);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600248}
249
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600250static int pmic_arb_wait_for_done(struct spmi_controller *ctrl,
David Collins40f318f2017-07-28 12:40:46 +0530251 void __iomem *base, u8 sid, u16 addr,
252 enum pmic_arb_channel ch_type)
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600253{
Kiran Gunda02abec32017-07-28 12:40:37 +0530254 struct spmi_pmic_arb *pmic_arb = spmi_controller_get_drvdata(ctrl);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600255 u32 status = 0;
256 u32 timeout = PMIC_ARB_TIMEOUT_US;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800257 u32 offset;
258 int rc;
259
David Collins40f318f2017-07-28 12:40:46 +0530260 rc = pmic_arb->ver_ops->offset(pmic_arb, sid, addr, ch_type);
Kiran Gundaff615ed2017-07-28 12:40:42 +0530261 if (rc < 0)
Stephen Boyd987a9f12015-11-17 16:13:55 -0800262 return rc;
263
Kiran Gundaff615ed2017-07-28 12:40:42 +0530264 offset = rc;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800265 offset += PMIC_ARB_STATUS;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600266
267 while (timeout--) {
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600268 status = readl_relaxed(base + offset);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600269
270 if (status & PMIC_ARB_STATUS_DONE) {
271 if (status & PMIC_ARB_STATUS_DENIED) {
Kiran Gunda02abec32017-07-28 12:40:37 +0530272 dev_err(&ctrl->dev, "%s: transaction denied (0x%x)\n",
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600273 __func__, status);
274 return -EPERM;
275 }
276
277 if (status & PMIC_ARB_STATUS_FAILURE) {
Kiran Gunda02abec32017-07-28 12:40:37 +0530278 dev_err(&ctrl->dev, "%s: transaction failed (0x%x)\n",
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600279 __func__, status);
280 return -EIO;
281 }
282
283 if (status & PMIC_ARB_STATUS_DROPPED) {
Kiran Gunda02abec32017-07-28 12:40:37 +0530284 dev_err(&ctrl->dev, "%s: transaction dropped (0x%x)\n",
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600285 __func__, status);
286 return -EIO;
287 }
288
289 return 0;
290 }
291 udelay(1);
292 }
293
Kiran Gunda02abec32017-07-28 12:40:37 +0530294 dev_err(&ctrl->dev, "%s: timeout, status 0x%x\n",
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600295 __func__, status);
296 return -ETIMEDOUT;
297}
298
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600299static int
300pmic_arb_non_data_cmd_v1(struct spmi_controller *ctrl, u8 opc, u8 sid)
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600301{
Kiran Gunda02abec32017-07-28 12:40:37 +0530302 struct spmi_pmic_arb *pmic_arb = spmi_controller_get_drvdata(ctrl);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600303 unsigned long flags;
304 u32 cmd;
305 int rc;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800306 u32 offset;
307
David Collins40f318f2017-07-28 12:40:46 +0530308 rc = pmic_arb->ver_ops->offset(pmic_arb, sid, 0, PMIC_ARB_CHANNEL_RW);
Kiran Gundaff615ed2017-07-28 12:40:42 +0530309 if (rc < 0)
Stephen Boyd987a9f12015-11-17 16:13:55 -0800310 return rc;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600311
Kiran Gundaff615ed2017-07-28 12:40:42 +0530312 offset = rc;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600313 cmd = ((opc | 0x40) << 27) | ((sid & 0xf) << 20);
314
Kiran Gunda02abec32017-07-28 12:40:37 +0530315 raw_spin_lock_irqsave(&pmic_arb->lock, flags);
316 pmic_arb_base_write(pmic_arb, offset + PMIC_ARB_CMD, cmd);
David Collins40f318f2017-07-28 12:40:46 +0530317 rc = pmic_arb_wait_for_done(ctrl, pmic_arb->wr_base, sid, 0,
318 PMIC_ARB_CHANNEL_RW);
Kiran Gunda02abec32017-07-28 12:40:37 +0530319 raw_spin_unlock_irqrestore(&pmic_arb->lock, flags);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600320
321 return rc;
322}
323
324static int
325pmic_arb_non_data_cmd_v2(struct spmi_controller *ctrl, u8 opc, u8 sid)
326{
327 return -EOPNOTSUPP;
328}
329
330/* Non-data command */
331static int pmic_arb_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid)
332{
Kiran Gunda02abec32017-07-28 12:40:37 +0530333 struct spmi_pmic_arb *pmic_arb = spmi_controller_get_drvdata(ctrl);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600334
335 dev_dbg(&ctrl->dev, "cmd op:0x%x sid:%d\n", opc, sid);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600336
337 /* Check for valid non-data command */
338 if (opc < SPMI_CMD_RESET || opc > SPMI_CMD_WAKEUP)
339 return -EINVAL;
340
Kiran Gunda02abec32017-07-28 12:40:37 +0530341 return pmic_arb->ver_ops->non_data_cmd(ctrl, opc, sid);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600342}
343
344static int pmic_arb_read_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
345 u16 addr, u8 *buf, size_t len)
346{
Kiran Gunda02abec32017-07-28 12:40:37 +0530347 struct spmi_pmic_arb *pmic_arb = spmi_controller_get_drvdata(ctrl);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600348 unsigned long flags;
349 u8 bc = len - 1;
350 u32 cmd;
351 int rc;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800352 u32 offset;
353
David Collins40f318f2017-07-28 12:40:46 +0530354 rc = pmic_arb->ver_ops->offset(pmic_arb, sid, addr,
355 PMIC_ARB_CHANNEL_OBS);
Kiran Gundaff615ed2017-07-28 12:40:42 +0530356 if (rc < 0)
Stephen Boyd987a9f12015-11-17 16:13:55 -0800357 return rc;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600358
Kiran Gundaff615ed2017-07-28 12:40:42 +0530359 offset = rc;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600360 if (bc >= PMIC_ARB_MAX_TRANS_BYTES) {
Kiran Gunda02abec32017-07-28 12:40:37 +0530361 dev_err(&ctrl->dev, "pmic-arb supports 1..%d bytes per trans, but:%zu requested",
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600362 PMIC_ARB_MAX_TRANS_BYTES, len);
363 return -EINVAL;
364 }
365
366 /* Check the opcode */
367 if (opc >= 0x60 && opc <= 0x7F)
368 opc = PMIC_ARB_OP_READ;
369 else if (opc >= 0x20 && opc <= 0x2F)
370 opc = PMIC_ARB_OP_EXT_READ;
371 else if (opc >= 0x38 && opc <= 0x3F)
372 opc = PMIC_ARB_OP_EXT_READL;
373 else
374 return -EINVAL;
375
Kiran Gunda02abec32017-07-28 12:40:37 +0530376 cmd = pmic_arb->ver_ops->fmt_cmd(opc, sid, addr, bc);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600377
Kiran Gunda02abec32017-07-28 12:40:37 +0530378 raw_spin_lock_irqsave(&pmic_arb->lock, flags);
379 pmic_arb_set_rd_cmd(pmic_arb, offset + PMIC_ARB_CMD, cmd);
David Collins40f318f2017-07-28 12:40:46 +0530380 rc = pmic_arb_wait_for_done(ctrl, pmic_arb->rd_base, sid, addr,
381 PMIC_ARB_CHANNEL_OBS);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600382 if (rc)
383 goto done;
384
Kiran Gunda02abec32017-07-28 12:40:37 +0530385 pmic_arb_read_data(pmic_arb, buf, offset + PMIC_ARB_RDATA0,
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600386 min_t(u8, bc, 3));
387
388 if (bc > 3)
Kiran Gunda02abec32017-07-28 12:40:37 +0530389 pmic_arb_read_data(pmic_arb, buf + 4, offset + PMIC_ARB_RDATA1,
390 bc - 4);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600391
392done:
Kiran Gunda02abec32017-07-28 12:40:37 +0530393 raw_spin_unlock_irqrestore(&pmic_arb->lock, flags);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600394 return rc;
395}
396
397static int pmic_arb_write_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
David Collins40f318f2017-07-28 12:40:46 +0530398 u16 addr, const u8 *buf, size_t len)
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600399{
Kiran Gunda02abec32017-07-28 12:40:37 +0530400 struct spmi_pmic_arb *pmic_arb = spmi_controller_get_drvdata(ctrl);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600401 unsigned long flags;
402 u8 bc = len - 1;
403 u32 cmd;
404 int rc;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800405 u32 offset;
406
David Collins40f318f2017-07-28 12:40:46 +0530407 rc = pmic_arb->ver_ops->offset(pmic_arb, sid, addr,
408 PMIC_ARB_CHANNEL_RW);
Kiran Gundaff615ed2017-07-28 12:40:42 +0530409 if (rc < 0)
Stephen Boyd987a9f12015-11-17 16:13:55 -0800410 return rc;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600411
Kiran Gundaff615ed2017-07-28 12:40:42 +0530412 offset = rc;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600413 if (bc >= PMIC_ARB_MAX_TRANS_BYTES) {
Kiran Gunda02abec32017-07-28 12:40:37 +0530414 dev_err(&ctrl->dev, "pmic-arb supports 1..%d bytes per trans, but:%zu requested",
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600415 PMIC_ARB_MAX_TRANS_BYTES, len);
416 return -EINVAL;
417 }
418
419 /* Check the opcode */
420 if (opc >= 0x40 && opc <= 0x5F)
421 opc = PMIC_ARB_OP_WRITE;
Fenglin Wu53d296b2017-07-28 12:40:47 +0530422 else if (opc <= 0x0F)
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600423 opc = PMIC_ARB_OP_EXT_WRITE;
424 else if (opc >= 0x30 && opc <= 0x37)
425 opc = PMIC_ARB_OP_EXT_WRITEL;
Stephen Boyd9b769682015-08-28 12:31:10 -0700426 else if (opc >= 0x80)
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600427 opc = PMIC_ARB_OP_ZERO_WRITE;
428 else
429 return -EINVAL;
430
Kiran Gunda02abec32017-07-28 12:40:37 +0530431 cmd = pmic_arb->ver_ops->fmt_cmd(opc, sid, addr, bc);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600432
433 /* Write data to FIFOs */
Kiran Gunda02abec32017-07-28 12:40:37 +0530434 raw_spin_lock_irqsave(&pmic_arb->lock, flags);
435 pmic_arb_write_data(pmic_arb, buf, offset + PMIC_ARB_WDATA0,
436 min_t(u8, bc, 3));
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600437 if (bc > 3)
Kiran Gunda02abec32017-07-28 12:40:37 +0530438 pmic_arb_write_data(pmic_arb, buf + 4, offset + PMIC_ARB_WDATA1,
439 bc - 4);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600440
441 /* Start the transaction */
Kiran Gunda02abec32017-07-28 12:40:37 +0530442 pmic_arb_base_write(pmic_arb, offset + PMIC_ARB_CMD, cmd);
David Collins40f318f2017-07-28 12:40:46 +0530443 rc = pmic_arb_wait_for_done(ctrl, pmic_arb->wr_base, sid, addr,
444 PMIC_ARB_CHANNEL_RW);
Kiran Gunda02abec32017-07-28 12:40:37 +0530445 raw_spin_unlock_irqrestore(&pmic_arb->lock, flags);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600446
447 return rc;
448}
449
Josh Cartwright67b563f2014-02-12 13:44:25 -0600450enum qpnpint_regs {
451 QPNPINT_REG_RT_STS = 0x10,
452 QPNPINT_REG_SET_TYPE = 0x11,
453 QPNPINT_REG_POLARITY_HIGH = 0x12,
454 QPNPINT_REG_POLARITY_LOW = 0x13,
455 QPNPINT_REG_LATCHED_CLR = 0x14,
456 QPNPINT_REG_EN_SET = 0x15,
457 QPNPINT_REG_EN_CLR = 0x16,
458 QPNPINT_REG_LATCHED_STS = 0x18,
459};
460
461struct spmi_pmic_arb_qpnpint_type {
462 u8 type; /* 1 -> edge */
463 u8 polarity_high;
464 u8 polarity_low;
465} __packed;
466
467/* Simplified accessor functions for irqchip callbacks */
468static void qpnpint_spmi_write(struct irq_data *d, u8 reg, void *buf,
469 size_t len)
470{
Kiran Gunda02abec32017-07-28 12:40:37 +0530471 struct spmi_pmic_arb *pmic_arb = irq_data_get_irq_chip_data(d);
472 u8 sid = hwirq_to_sid(d->hwirq);
473 u8 per = hwirq_to_per(d->hwirq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600474
Kiran Gunda02abec32017-07-28 12:40:37 +0530475 if (pmic_arb_write_cmd(pmic_arb->spmic, SPMI_CMD_EXT_WRITEL, sid,
Josh Cartwright67b563f2014-02-12 13:44:25 -0600476 (per << 8) + reg, buf, len))
Kiran Gunda02abec32017-07-28 12:40:37 +0530477 dev_err_ratelimited(&pmic_arb->spmic->dev, "failed irqchip transaction on %x\n",
Josh Cartwright67b563f2014-02-12 13:44:25 -0600478 d->irq);
479}
480
481static void qpnpint_spmi_read(struct irq_data *d, u8 reg, void *buf, size_t len)
482{
Kiran Gunda02abec32017-07-28 12:40:37 +0530483 struct spmi_pmic_arb *pmic_arb = irq_data_get_irq_chip_data(d);
484 u8 sid = hwirq_to_sid(d->hwirq);
485 u8 per = hwirq_to_per(d->hwirq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600486
Kiran Gunda02abec32017-07-28 12:40:37 +0530487 if (pmic_arb_read_cmd(pmic_arb->spmic, SPMI_CMD_EXT_READL, sid,
Josh Cartwright67b563f2014-02-12 13:44:25 -0600488 (per << 8) + reg, buf, len))
Kiran Gunda02abec32017-07-28 12:40:37 +0530489 dev_err_ratelimited(&pmic_arb->spmic->dev, "failed irqchip transaction on %x\n",
Josh Cartwright67b563f2014-02-12 13:44:25 -0600490 d->irq);
491}
492
Kiran Gunda02abec32017-07-28 12:40:37 +0530493static void cleanup_irq(struct spmi_pmic_arb *pmic_arb, u16 apid, int id)
Abhijeet Dharmapurikar6bc546e2017-05-10 19:55:35 +0530494{
Kiran Gunda02abec32017-07-28 12:40:37 +0530495 u16 ppid = pmic_arb->apid_data[apid].ppid;
Abhijeet Dharmapurikar6bc546e2017-05-10 19:55:35 +0530496 u8 sid = ppid >> 8;
497 u8 per = ppid & 0xFF;
498 u8 irq_mask = BIT(id);
499
Kiran Gundae95d0732017-07-28 12:40:44 +0530500 writel_relaxed(irq_mask, pmic_arb->ver_ops->irq_clear(pmic_arb, apid));
Abhijeet Dharmapurikar6bc546e2017-05-10 19:55:35 +0530501
Kiran Gunda02abec32017-07-28 12:40:37 +0530502 if (pmic_arb_write_cmd(pmic_arb->spmic, SPMI_CMD_EXT_WRITEL, sid,
Abhijeet Dharmapurikar6bc546e2017-05-10 19:55:35 +0530503 (per << 8) + QPNPINT_REG_LATCHED_CLR, &irq_mask, 1))
Kiran Gunda02abec32017-07-28 12:40:37 +0530504 dev_err_ratelimited(&pmic_arb->spmic->dev, "failed to ack irq_mask = 0x%x for ppid = %x\n",
Abhijeet Dharmapurikar6bc546e2017-05-10 19:55:35 +0530505 irq_mask, ppid);
506
Kiran Gunda02abec32017-07-28 12:40:37 +0530507 if (pmic_arb_write_cmd(pmic_arb->spmic, SPMI_CMD_EXT_WRITEL, sid,
Abhijeet Dharmapurikar6bc546e2017-05-10 19:55:35 +0530508 (per << 8) + QPNPINT_REG_EN_CLR, &irq_mask, 1))
Kiran Gunda02abec32017-07-28 12:40:37 +0530509 dev_err_ratelimited(&pmic_arb->spmic->dev, "failed to ack irq_mask = 0x%x for ppid = %x\n",
Abhijeet Dharmapurikar6bc546e2017-05-10 19:55:35 +0530510 irq_mask, ppid);
511}
512
Kiran Gunda02abec32017-07-28 12:40:37 +0530513static void periph_interrupt(struct spmi_pmic_arb *pmic_arb, u16 apid)
Josh Cartwright67b563f2014-02-12 13:44:25 -0600514{
515 unsigned int irq;
516 u32 status;
517 int id;
Kiran Gunda02abec32017-07-28 12:40:37 +0530518 u8 sid = (pmic_arb->apid_data[apid].ppid >> 8) & 0xF;
519 u8 per = pmic_arb->apid_data[apid].ppid & 0xFF;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600520
Kiran Gundae95d0732017-07-28 12:40:44 +0530521 status = readl_relaxed(pmic_arb->ver_ops->irq_status(pmic_arb, apid));
Josh Cartwright67b563f2014-02-12 13:44:25 -0600522 while (status) {
523 id = ffs(status) - 1;
Abhijeet Dharmapurikar111a10b2017-05-10 19:55:32 +0530524 status &= ~BIT(id);
Kiran Gunda02abec32017-07-28 12:40:37 +0530525 irq = irq_find_mapping(pmic_arb->domain,
526 spec_to_hwirq(sid, per, id, apid));
Abhijeet Dharmapurikar6bc546e2017-05-10 19:55:35 +0530527 if (irq == 0) {
Kiran Gunda02abec32017-07-28 12:40:37 +0530528 cleanup_irq(pmic_arb, apid, id);
Abhijeet Dharmapurikar6bc546e2017-05-10 19:55:35 +0530529 continue;
530 }
Josh Cartwright67b563f2014-02-12 13:44:25 -0600531 generic_handle_irq(irq);
532 }
533}
534
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200535static void pmic_arb_chained_irq(struct irq_desc *desc)
Josh Cartwright67b563f2014-02-12 13:44:25 -0600536{
Kiran Gunda02abec32017-07-28 12:40:37 +0530537 struct spmi_pmic_arb *pmic_arb = irq_desc_get_handler_data(desc);
Kiran Gundae95d0732017-07-28 12:40:44 +0530538 const struct pmic_arb_ver_ops *ver_ops = pmic_arb->ver_ops;
Jiang Liu7fe88f32015-07-13 20:52:25 +0000539 struct irq_chip *chip = irq_desc_get_chip(desc);
Kiran Gunda02abec32017-07-28 12:40:37 +0530540 int first = pmic_arb->min_apid >> 5;
541 int last = pmic_arb->max_apid >> 5;
Kiran Gundae95d0732017-07-28 12:40:44 +0530542 u8 ee = pmic_arb->ee;
Abhijeet Dharmapurikar472eaf82017-05-10 19:55:39 +0530543 u32 status, enable;
544 int i, id, apid;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600545
546 chained_irq_enter(chip, desc);
547
548 for (i = first; i <= last; ++i) {
Kiran Gundae95d0732017-07-28 12:40:44 +0530549 status = readl_relaxed(
550 ver_ops->owner_acc_status(pmic_arb, ee, i));
Josh Cartwright67b563f2014-02-12 13:44:25 -0600551 while (status) {
552 id = ffs(status) - 1;
Abhijeet Dharmapurikar111a10b2017-05-10 19:55:32 +0530553 status &= ~BIT(id);
Abhijeet Dharmapurikar472eaf82017-05-10 19:55:39 +0530554 apid = id + i * 32;
Kiran Gundae95d0732017-07-28 12:40:44 +0530555 enable = readl_relaxed(
556 ver_ops->acc_enable(pmic_arb, apid));
Abhijeet Dharmapurikar472eaf82017-05-10 19:55:39 +0530557 if (enable & SPMI_PIC_ACC_ENABLE_BIT)
Kiran Gunda02abec32017-07-28 12:40:37 +0530558 periph_interrupt(pmic_arb, apid);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600559 }
560 }
561
562 chained_irq_exit(chip, desc);
563}
564
565static void qpnpint_irq_ack(struct irq_data *d)
566{
Kiran Gunda02abec32017-07-28 12:40:37 +0530567 struct spmi_pmic_arb *pmic_arb = irq_data_get_irq_chip_data(d);
568 u8 irq = hwirq_to_irq(d->hwirq);
569 u16 apid = hwirq_to_apid(d->hwirq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600570 u8 data;
571
Kiran Gundae95d0732017-07-28 12:40:44 +0530572 writel_relaxed(BIT(irq), pmic_arb->ver_ops->irq_clear(pmic_arb, apid));
Josh Cartwright67b563f2014-02-12 13:44:25 -0600573
Abhijeet Dharmapurikar111a10b2017-05-10 19:55:32 +0530574 data = BIT(irq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600575 qpnpint_spmi_write(d, QPNPINT_REG_LATCHED_CLR, &data, 1);
576}
577
578static void qpnpint_irq_mask(struct irq_data *d)
579{
Kiran Gunda02abec32017-07-28 12:40:37 +0530580 u8 irq = hwirq_to_irq(d->hwirq);
Abhijeet Dharmapurikar6bc546e2017-05-10 19:55:35 +0530581 u8 data = BIT(irq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600582
Josh Cartwright67b563f2014-02-12 13:44:25 -0600583 qpnpint_spmi_write(d, QPNPINT_REG_EN_CLR, &data, 1);
584}
585
586static void qpnpint_irq_unmask(struct irq_data *d)
587{
Kiran Gunda02abec32017-07-28 12:40:37 +0530588 struct spmi_pmic_arb *pmic_arb = irq_data_get_irq_chip_data(d);
Kiran Gundae95d0732017-07-28 12:40:44 +0530589 const struct pmic_arb_ver_ops *ver_ops = pmic_arb->ver_ops;
Kiran Gunda02abec32017-07-28 12:40:37 +0530590 u8 irq = hwirq_to_irq(d->hwirq);
591 u16 apid = hwirq_to_apid(d->hwirq);
Abhijeet Dharmapurikarcee0fad2017-05-10 19:55:37 +0530592 u8 buf[2];
Josh Cartwright67b563f2014-02-12 13:44:25 -0600593
Abhijeet Dharmapurikar6bc546e2017-05-10 19:55:35 +0530594 writel_relaxed(SPMI_PIC_ACC_ENABLE_BIT,
Kiran Gundae95d0732017-07-28 12:40:44 +0530595 ver_ops->acc_enable(pmic_arb, apid));
Josh Cartwright67b563f2014-02-12 13:44:25 -0600596
Abhijeet Dharmapurikarcee0fad2017-05-10 19:55:37 +0530597 qpnpint_spmi_read(d, QPNPINT_REG_EN_SET, &buf[0], 1);
598 if (!(buf[0] & BIT(irq))) {
599 /*
600 * Since the interrupt is currently disabled, write to both the
601 * LATCHED_CLR and EN_SET registers so that a spurious interrupt
602 * cannot be triggered when the interrupt is enabled
603 */
604 buf[0] = BIT(irq);
605 buf[1] = BIT(irq);
606 qpnpint_spmi_write(d, QPNPINT_REG_LATCHED_CLR, &buf, 2);
607 }
Josh Cartwright67b563f2014-02-12 13:44:25 -0600608}
609
Josh Cartwright67b563f2014-02-12 13:44:25 -0600610static int qpnpint_irq_set_type(struct irq_data *d, unsigned int flow_type)
611{
612 struct spmi_pmic_arb_qpnpint_type type;
Kiran Gunda325255b2017-07-28 12:40:39 +0530613 irq_flow_handler_t flow_handler;
Kiran Gunda02abec32017-07-28 12:40:37 +0530614 u8 irq = hwirq_to_irq(d->hwirq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600615
616 qpnpint_spmi_read(d, QPNPINT_REG_SET_TYPE, &type, sizeof(type));
617
618 if (flow_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)) {
Kiran Gunda325255b2017-07-28 12:40:39 +0530619 type.type |= BIT(irq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600620 if (flow_type & IRQF_TRIGGER_RISING)
Kiran Gunda325255b2017-07-28 12:40:39 +0530621 type.polarity_high |= BIT(irq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600622 if (flow_type & IRQF_TRIGGER_FALLING)
Kiran Gunda325255b2017-07-28 12:40:39 +0530623 type.polarity_low |= BIT(irq);
624
625 flow_handler = handle_edge_irq;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600626 } else {
627 if ((flow_type & (IRQF_TRIGGER_HIGH)) &&
628 (flow_type & (IRQF_TRIGGER_LOW)))
629 return -EINVAL;
630
Kiran Gunda325255b2017-07-28 12:40:39 +0530631 type.type &= ~BIT(irq); /* level trig */
Josh Cartwright67b563f2014-02-12 13:44:25 -0600632 if (flow_type & IRQF_TRIGGER_HIGH)
Kiran Gunda325255b2017-07-28 12:40:39 +0530633 type.polarity_high |= BIT(irq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600634 else
Kiran Gunda325255b2017-07-28 12:40:39 +0530635 type.polarity_low |= BIT(irq);
636
637 flow_handler = handle_level_irq;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600638 }
639
640 qpnpint_spmi_write(d, QPNPINT_REG_SET_TYPE, &type, sizeof(type));
Kiran Gunda325255b2017-07-28 12:40:39 +0530641 irq_set_handler_locked(d, flow_handler);
Abhijeet Dharmapurikar5f9b2ea2017-05-10 19:55:38 +0530642
Josh Cartwright67b563f2014-02-12 13:44:25 -0600643 return 0;
644}
645
Kiran Gundacdeef072017-07-28 12:40:43 +0530646static int qpnpint_irq_set_wake(struct irq_data *d, unsigned int on)
647{
648 struct spmi_pmic_arb *pmic_arb = irq_data_get_irq_chip_data(d);
649
650 return irq_set_irq_wake(pmic_arb->irq, on);
651}
652
Courtney Cavin60be4232015-07-30 10:53:54 -0700653static int qpnpint_get_irqchip_state(struct irq_data *d,
654 enum irqchip_irq_state which,
655 bool *state)
656{
Kiran Gunda02abec32017-07-28 12:40:37 +0530657 u8 irq = hwirq_to_irq(d->hwirq);
Courtney Cavin60be4232015-07-30 10:53:54 -0700658 u8 status = 0;
659
660 if (which != IRQCHIP_STATE_LINE_LEVEL)
661 return -EINVAL;
662
663 qpnpint_spmi_read(d, QPNPINT_REG_RT_STS, &status, 1);
664 *state = !!(status & BIT(irq));
665
666 return 0;
667}
668
Kiran Gunda2fb4f252017-08-23 18:16:26 +0530669static int qpnpint_irq_request_resources(struct irq_data *d)
670{
671 struct spmi_pmic_arb *pmic_arb = irq_data_get_irq_chip_data(d);
672 u16 periph = hwirq_to_per(d->hwirq);
673 u16 apid = hwirq_to_apid(d->hwirq);
674 u16 sid = hwirq_to_sid(d->hwirq);
675 u16 irq = hwirq_to_irq(d->hwirq);
676
677 if (pmic_arb->apid_data[apid].irq_ee != pmic_arb->ee) {
678 dev_err(&pmic_arb->spmic->dev, "failed to xlate sid = %#x, periph = %#x, irq = %u: ee=%u but owner=%u\n",
679 sid, periph, irq, pmic_arb->ee,
680 pmic_arb->apid_data[apid].irq_ee);
681 return -ENODEV;
682 }
683
684 return 0;
685}
686
Josh Cartwright67b563f2014-02-12 13:44:25 -0600687static struct irq_chip pmic_arb_irqchip = {
688 .name = "pmic_arb",
Josh Cartwright67b563f2014-02-12 13:44:25 -0600689 .irq_ack = qpnpint_irq_ack,
690 .irq_mask = qpnpint_irq_mask,
691 .irq_unmask = qpnpint_irq_unmask,
692 .irq_set_type = qpnpint_irq_set_type,
Kiran Gundacdeef072017-07-28 12:40:43 +0530693 .irq_set_wake = qpnpint_irq_set_wake,
Courtney Cavin60be4232015-07-30 10:53:54 -0700694 .irq_get_irqchip_state = qpnpint_get_irqchip_state,
Kiran Gunda2fb4f252017-08-23 18:16:26 +0530695 .irq_request_resources = qpnpint_irq_request_resources,
Kiran Gundacdeef072017-07-28 12:40:43 +0530696 .flags = IRQCHIP_MASK_ON_SUSPEND,
Josh Cartwright67b563f2014-02-12 13:44:25 -0600697};
698
Josh Cartwright67b563f2014-02-12 13:44:25 -0600699static int qpnpint_irq_domain_dt_translate(struct irq_domain *d,
700 struct device_node *controller,
701 const u32 *intspec,
702 unsigned int intsize,
703 unsigned long *out_hwirq,
704 unsigned int *out_type)
705{
Kiran Gunda02abec32017-07-28 12:40:37 +0530706 struct spmi_pmic_arb *pmic_arb = d->host_data;
Kiran Gundaff615ed2017-07-28 12:40:42 +0530707 u16 apid, ppid;
Abhijeet Dharmapurikar7f1d4e52017-05-10 19:55:34 +0530708 int rc;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600709
Kiran Gunda02abec32017-07-28 12:40:37 +0530710 dev_dbg(&pmic_arb->spmic->dev, "intspec[0] 0x%1x intspec[1] 0x%02x intspec[2] 0x%02x\n",
Josh Cartwright67b563f2014-02-12 13:44:25 -0600711 intspec[0], intspec[1], intspec[2]);
712
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100713 if (irq_domain_get_of_node(d) != controller)
Josh Cartwright67b563f2014-02-12 13:44:25 -0600714 return -EINVAL;
715 if (intsize != 4)
716 return -EINVAL;
717 if (intspec[0] > 0xF || intspec[1] > 0xFF || intspec[2] > 0x7)
718 return -EINVAL;
719
Kiran Gundaff615ed2017-07-28 12:40:42 +0530720 ppid = intspec[0] << 8 | intspec[1];
721 rc = pmic_arb->ver_ops->ppid_to_apid(pmic_arb, ppid);
Abhijeet Dharmapurikar7f1d4e52017-05-10 19:55:34 +0530722 if (rc < 0) {
David Collins40f318f2017-07-28 12:40:46 +0530723 dev_err(&pmic_arb->spmic->dev, "failed to xlate sid = %#x, periph = %#x, irq = %u rc = %d\n",
Abhijeet Dharmapurikar7f1d4e52017-05-10 19:55:34 +0530724 intspec[0], intspec[1], intspec[2], rc);
725 return rc;
726 }
Josh Cartwright67b563f2014-02-12 13:44:25 -0600727
Kiran Gundaff615ed2017-07-28 12:40:42 +0530728 apid = rc;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600729 /* Keep track of {max,min}_apid for bounding search during interrupt */
Kiran Gunda02abec32017-07-28 12:40:37 +0530730 if (apid > pmic_arb->max_apid)
731 pmic_arb->max_apid = apid;
732 if (apid < pmic_arb->min_apid)
733 pmic_arb->min_apid = apid;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600734
Kiran Gunda02abec32017-07-28 12:40:37 +0530735 *out_hwirq = spec_to_hwirq(intspec[0], intspec[1], intspec[2], apid);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600736 *out_type = intspec[3] & IRQ_TYPE_SENSE_MASK;
737
Kiran Gunda02abec32017-07-28 12:40:37 +0530738 dev_dbg(&pmic_arb->spmic->dev, "out_hwirq = %lu\n", *out_hwirq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600739
740 return 0;
741}
742
743static int qpnpint_irq_domain_map(struct irq_domain *d,
744 unsigned int virq,
745 irq_hw_number_t hwirq)
746{
Kiran Gunda02abec32017-07-28 12:40:37 +0530747 struct spmi_pmic_arb *pmic_arb = d->host_data;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600748
Kiran Gunda02abec32017-07-28 12:40:37 +0530749 dev_dbg(&pmic_arb->spmic->dev, "virq = %u, hwirq = %lu\n", virq, hwirq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600750
751 irq_set_chip_and_handler(virq, &pmic_arb_irqchip, handle_level_irq);
752 irq_set_chip_data(virq, d->host_data);
753 irq_set_noprobe(virq);
754 return 0;
755}
756
Kiran Gundaff615ed2017-07-28 12:40:42 +0530757static int pmic_arb_ppid_to_apid_v1(struct spmi_pmic_arb *pmic_arb, u16 ppid)
Abhijeet Dharmapurikar7f1d4e52017-05-10 19:55:34 +0530758{
Kiran Gunda02abec32017-07-28 12:40:37 +0530759 u32 *mapping_table = pmic_arb->mapping_table;
Abhijeet Dharmapurikar7f1d4e52017-05-10 19:55:34 +0530760 int index = 0, i;
761 u16 apid_valid;
Kiran Gundaff615ed2017-07-28 12:40:42 +0530762 u16 apid;
Abhijeet Dharmapurikar7f1d4e52017-05-10 19:55:34 +0530763 u32 data;
764
Kiran Gunda02abec32017-07-28 12:40:37 +0530765 apid_valid = pmic_arb->ppid_to_apid[ppid];
766 if (apid_valid & PMIC_ARB_APID_VALID) {
Kiran Gundaff615ed2017-07-28 12:40:42 +0530767 apid = apid_valid & ~PMIC_ARB_APID_VALID;
768 return apid;
Abhijeet Dharmapurikar7f1d4e52017-05-10 19:55:34 +0530769 }
770
771 for (i = 0; i < SPMI_MAPPING_TABLE_TREE_DEPTH; ++i) {
Kiran Gunda02abec32017-07-28 12:40:37 +0530772 if (!test_and_set_bit(index, pmic_arb->mapping_table_valid))
773 mapping_table[index] = readl_relaxed(pmic_arb->cnfg +
Abhijeet Dharmapurikar7f1d4e52017-05-10 19:55:34 +0530774 SPMI_MAPPING_TABLE_REG(index));
775
776 data = mapping_table[index];
777
778 if (ppid & BIT(SPMI_MAPPING_BIT_INDEX(data))) {
779 if (SPMI_MAPPING_BIT_IS_1_FLAG(data)) {
780 index = SPMI_MAPPING_BIT_IS_1_RESULT(data);
781 } else {
Kiran Gundaff615ed2017-07-28 12:40:42 +0530782 apid = SPMI_MAPPING_BIT_IS_1_RESULT(data);
Kiran Gunda02abec32017-07-28 12:40:37 +0530783 pmic_arb->ppid_to_apid[ppid]
Kiran Gundaff615ed2017-07-28 12:40:42 +0530784 = apid | PMIC_ARB_APID_VALID;
785 pmic_arb->apid_data[apid].ppid = ppid;
786 return apid;
Abhijeet Dharmapurikar7f1d4e52017-05-10 19:55:34 +0530787 }
788 } else {
789 if (SPMI_MAPPING_BIT_IS_0_FLAG(data)) {
790 index = SPMI_MAPPING_BIT_IS_0_RESULT(data);
791 } else {
Kiran Gundaff615ed2017-07-28 12:40:42 +0530792 apid = SPMI_MAPPING_BIT_IS_0_RESULT(data);
Kiran Gunda02abec32017-07-28 12:40:37 +0530793 pmic_arb->ppid_to_apid[ppid]
Kiran Gundaff615ed2017-07-28 12:40:42 +0530794 = apid | PMIC_ARB_APID_VALID;
795 pmic_arb->apid_data[apid].ppid = ppid;
796 return apid;
Abhijeet Dharmapurikar7f1d4e52017-05-10 19:55:34 +0530797 }
798 }
799 }
800
801 return -ENODEV;
802}
803
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600804/* v1 offset per ee */
David Collins40f318f2017-07-28 12:40:46 +0530805static int pmic_arb_offset_v1(struct spmi_pmic_arb *pmic_arb, u8 sid, u16 addr,
806 enum pmic_arb_channel ch_type)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600807{
Kiran Gundaff615ed2017-07-28 12:40:42 +0530808 return 0x800 + 0x80 * pmic_arb->channel;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600809}
810
Kiran Gunda02abec32017-07-28 12:40:37 +0530811static u16 pmic_arb_find_apid(struct spmi_pmic_arb *pmic_arb, u16 ppid)
Stephen Boyd987a9f12015-11-17 16:13:55 -0800812{
Kiran Gundaf2f315642017-07-28 12:40:38 +0530813 struct apid_data *apidd = &pmic_arb->apid_data[pmic_arb->last_apid];
Stephen Boyd987a9f12015-11-17 16:13:55 -0800814 u32 regval, offset;
Kiran Gundaf2f315642017-07-28 12:40:38 +0530815 u16 id, apid;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800816
Kiran Gundaf2f315642017-07-28 12:40:38 +0530817 for (apid = pmic_arb->last_apid; ; apid++, apidd++) {
David Collins40f318f2017-07-28 12:40:46 +0530818 offset = pmic_arb->ver_ops->apid_map_offset(apid);
Kiran Gunda02abec32017-07-28 12:40:37 +0530819 if (offset >= pmic_arb->core_size)
Stephen Boyd987a9f12015-11-17 16:13:55 -0800820 break;
821
Kiran Gunda02abec32017-07-28 12:40:37 +0530822 regval = readl_relaxed(pmic_arb->cnfg +
Kiran Gundab319b592017-07-28 12:40:36 +0530823 SPMI_OWNERSHIP_TABLE_REG(apid));
David Collins40f318f2017-07-28 12:40:46 +0530824 apidd->irq_ee = SPMI_OWNERSHIP_PERIPH2OWNER(regval);
825 apidd->write_ee = apidd->irq_ee;
Kiran Gundab319b592017-07-28 12:40:36 +0530826
Kiran Gunda02abec32017-07-28 12:40:37 +0530827 regval = readl_relaxed(pmic_arb->core + offset);
Stephen Boyd987a9f12015-11-17 16:13:55 -0800828 if (!regval)
829 continue;
830
831 id = (regval >> 8) & PMIC_ARB_PPID_MASK;
Kiran Gunda02abec32017-07-28 12:40:37 +0530832 pmic_arb->ppid_to_apid[id] = apid | PMIC_ARB_APID_VALID;
Kiran Gundaf2f315642017-07-28 12:40:38 +0530833 apidd->ppid = id;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800834 if (id == ppid) {
Kiran Gunda02abec32017-07-28 12:40:37 +0530835 apid |= PMIC_ARB_APID_VALID;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800836 break;
837 }
838 }
Kiran Gunda02abec32017-07-28 12:40:37 +0530839 pmic_arb->last_apid = apid & ~PMIC_ARB_APID_VALID;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800840
Abhijeet Dharmapurikar1ef1ce42017-05-10 19:55:33 +0530841 return apid;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800842}
843
Kiran Gundaff615ed2017-07-28 12:40:42 +0530844static int pmic_arb_ppid_to_apid_v2(struct spmi_pmic_arb *pmic_arb, u16 ppid)
Abhijeet Dharmapurikar57102ad2017-05-10 19:55:31 +0530845{
Abhijeet Dharmapurikar7f1d4e52017-05-10 19:55:34 +0530846 u16 apid_valid;
Abhijeet Dharmapurikar57102ad2017-05-10 19:55:31 +0530847
Kiran Gunda02abec32017-07-28 12:40:37 +0530848 apid_valid = pmic_arb->ppid_to_apid[ppid];
849 if (!(apid_valid & PMIC_ARB_APID_VALID))
850 apid_valid = pmic_arb_find_apid(pmic_arb, ppid);
851 if (!(apid_valid & PMIC_ARB_APID_VALID))
Abhijeet Dharmapurikar57102ad2017-05-10 19:55:31 +0530852 return -ENODEV;
853
Kiran Gundaff615ed2017-07-28 12:40:42 +0530854 return apid_valid & ~PMIC_ARB_APID_VALID;
Abhijeet Dharmapurikar7f1d4e52017-05-10 19:55:34 +0530855}
856
David Collins40f318f2017-07-28 12:40:46 +0530857static int pmic_arb_read_apid_map_v5(struct spmi_pmic_arb *pmic_arb)
858{
859 struct apid_data *apidd = pmic_arb->apid_data;
860 struct apid_data *prev_apidd;
861 u16 i, apid, ppid;
862 bool valid, is_irq_ee;
863 u32 regval, offset;
864
865 /*
866 * In order to allow multiple EEs to write to a single PPID in arbiter
867 * version 5, there is more than one APID mapped to each PPID.
868 * The owner field for each of these mappings specifies the EE which is
869 * allowed to write to the APID. The owner of the last (highest) APID
870 * for a given PPID will receive interrupts from the PPID.
871 */
872 for (i = 0; ; i++, apidd++) {
873 offset = pmic_arb->ver_ops->apid_map_offset(i);
874 if (offset >= pmic_arb->core_size)
875 break;
876
877 regval = readl_relaxed(pmic_arb->core + offset);
878 if (!regval)
879 continue;
880 ppid = (regval >> 8) & PMIC_ARB_PPID_MASK;
881 is_irq_ee = PMIC_ARB_CHAN_IS_IRQ_OWNER(regval);
882
883 regval = readl_relaxed(pmic_arb->cnfg +
884 SPMI_OWNERSHIP_TABLE_REG(i));
885 apidd->write_ee = SPMI_OWNERSHIP_PERIPH2OWNER(regval);
886
887 apidd->irq_ee = is_irq_ee ? apidd->write_ee : INVALID_EE;
888
889 valid = pmic_arb->ppid_to_apid[ppid] & PMIC_ARB_APID_VALID;
890 apid = pmic_arb->ppid_to_apid[ppid] & ~PMIC_ARB_APID_VALID;
891 prev_apidd = &pmic_arb->apid_data[apid];
892
893 if (valid && is_irq_ee &&
894 prev_apidd->write_ee == pmic_arb->ee) {
895 /*
896 * Duplicate PPID mapping after the one for this EE;
897 * override the irq owner
898 */
899 prev_apidd->irq_ee = apidd->irq_ee;
900 } else if (!valid || is_irq_ee) {
901 /* First PPID mapping or duplicate for another EE */
902 pmic_arb->ppid_to_apid[ppid] = i | PMIC_ARB_APID_VALID;
903 }
904
905 apidd->ppid = ppid;
906 pmic_arb->last_apid = i;
907 }
908
909 /* Dump the mapping table for debug purposes. */
910 dev_dbg(&pmic_arb->spmic->dev, "PPID APID Write-EE IRQ-EE\n");
911 for (ppid = 0; ppid < PMIC_ARB_MAX_PPID; ppid++) {
912 apid = pmic_arb->ppid_to_apid[ppid];
913 if (apid & PMIC_ARB_APID_VALID) {
914 apid &= ~PMIC_ARB_APID_VALID;
915 apidd = &pmic_arb->apid_data[apid];
916 dev_dbg(&pmic_arb->spmic->dev, "%#03X %3u %2u %2u\n",
917 ppid, apid, apidd->write_ee, apidd->irq_ee);
918 }
919 }
920
921 return 0;
922}
923
924static int pmic_arb_ppid_to_apid_v5(struct spmi_pmic_arb *pmic_arb, u16 ppid)
925{
926 if (!(pmic_arb->ppid_to_apid[ppid] & PMIC_ARB_APID_VALID))
927 return -ENODEV;
928
929 return pmic_arb->ppid_to_apid[ppid] & ~PMIC_ARB_APID_VALID;
930}
931
Abhijeet Dharmapurikar1ef1ce42017-05-10 19:55:33 +0530932/* v2 offset per ppid and per ee */
David Collins40f318f2017-07-28 12:40:46 +0530933static int pmic_arb_offset_v2(struct spmi_pmic_arb *pmic_arb, u8 sid, u16 addr,
934 enum pmic_arb_channel ch_type)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600935{
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +0530936 u16 apid;
Kiran Gundaff615ed2017-07-28 12:40:42 +0530937 u16 ppid;
Abhijeet Dharmapurikar7f1d4e52017-05-10 19:55:34 +0530938 int rc;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600939
Kiran Gundaff615ed2017-07-28 12:40:42 +0530940 ppid = sid << 8 | ((addr >> 8) & 0xFF);
941 rc = pmic_arb_ppid_to_apid_v2(pmic_arb, ppid);
Abhijeet Dharmapurikar7f1d4e52017-05-10 19:55:34 +0530942 if (rc < 0)
943 return rc;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800944
Kiran Gundaff615ed2017-07-28 12:40:42 +0530945 apid = rc;
946 return 0x1000 * pmic_arb->ee + 0x8000 * apid;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600947}
948
David Collins40f318f2017-07-28 12:40:46 +0530949/*
950 * v5 offset per ee and per apid for observer channels and per apid for
951 * read/write channels.
952 */
953static int pmic_arb_offset_v5(struct spmi_pmic_arb *pmic_arb, u8 sid, u16 addr,
954 enum pmic_arb_channel ch_type)
955{
956 u16 apid;
957 int rc;
958 u32 offset = 0;
959 u16 ppid = (sid << 8) | (addr >> 8);
960
961 rc = pmic_arb_ppid_to_apid_v5(pmic_arb, ppid);
962 if (rc < 0)
963 return rc;
964
965 apid = rc;
966 switch (ch_type) {
967 case PMIC_ARB_CHANNEL_OBS:
968 offset = 0x10000 * pmic_arb->ee + 0x80 * apid;
969 break;
970 case PMIC_ARB_CHANNEL_RW:
971 offset = 0x10000 * apid;
972 break;
973 }
974
975 return offset;
976}
977
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600978static u32 pmic_arb_fmt_cmd_v1(u8 opc, u8 sid, u16 addr, u8 bc)
979{
980 return (opc << 27) | ((sid & 0xf) << 20) | (addr << 4) | (bc & 0x7);
981}
982
983static u32 pmic_arb_fmt_cmd_v2(u8 opc, u8 sid, u16 addr, u8 bc)
984{
985 return (opc << 27) | ((addr & 0xff) << 4) | (bc & 0x7);
986}
987
Kiran Gundae95d0732017-07-28 12:40:44 +0530988static void __iomem *
989pmic_arb_owner_acc_status_v1(struct spmi_pmic_arb *pmic_arb, u8 m, u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600990{
Kiran Gundae95d0732017-07-28 12:40:44 +0530991 return pmic_arb->intr + 0x20 * m + 0x4 * n;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600992}
993
Kiran Gundae95d0732017-07-28 12:40:44 +0530994static void __iomem *
995pmic_arb_owner_acc_status_v2(struct spmi_pmic_arb *pmic_arb, u8 m, u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600996{
Kiran Gundae95d0732017-07-28 12:40:44 +0530997 return pmic_arb->intr + 0x100000 + 0x1000 * m + 0x4 * n;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600998}
999
Kiran Gundae95d0732017-07-28 12:40:44 +05301000static void __iomem *
1001pmic_arb_owner_acc_status_v3(struct spmi_pmic_arb *pmic_arb, u8 m, u16 n)
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +05301002{
Kiran Gundae95d0732017-07-28 12:40:44 +05301003 return pmic_arb->intr + 0x200000 + 0x1000 * m + 0x4 * n;
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +05301004}
1005
Kiran Gundae95d0732017-07-28 12:40:44 +05301006static void __iomem *
David Collins40f318f2017-07-28 12:40:46 +05301007pmic_arb_owner_acc_status_v5(struct spmi_pmic_arb *pmic_arb, u8 m, u16 n)
1008{
1009 return pmic_arb->intr + 0x10000 * m + 0x4 * n;
1010}
1011
1012static void __iomem *
Kiran Gundae95d0732017-07-28 12:40:44 +05301013pmic_arb_acc_enable_v1(struct spmi_pmic_arb *pmic_arb, u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001014{
Kiran Gundae95d0732017-07-28 12:40:44 +05301015 return pmic_arb->intr + 0x200 + 0x4 * n;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001016}
1017
Kiran Gundae95d0732017-07-28 12:40:44 +05301018static void __iomem *
1019pmic_arb_acc_enable_v2(struct spmi_pmic_arb *pmic_arb, u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001020{
Kiran Gundae95d0732017-07-28 12:40:44 +05301021 return pmic_arb->intr + 0x1000 * n;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001022}
1023
Kiran Gundae95d0732017-07-28 12:40:44 +05301024static void __iomem *
David Collins40f318f2017-07-28 12:40:46 +05301025pmic_arb_acc_enable_v5(struct spmi_pmic_arb *pmic_arb, u16 n)
1026{
1027 return pmic_arb->wr_base + 0x100 + 0x10000 * n;
1028}
1029
1030static void __iomem *
Kiran Gundae95d0732017-07-28 12:40:44 +05301031pmic_arb_irq_status_v1(struct spmi_pmic_arb *pmic_arb, u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001032{
Kiran Gundae95d0732017-07-28 12:40:44 +05301033 return pmic_arb->intr + 0x600 + 0x4 * n;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001034}
1035
Kiran Gundae95d0732017-07-28 12:40:44 +05301036static void __iomem *
1037pmic_arb_irq_status_v2(struct spmi_pmic_arb *pmic_arb, u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001038{
Kiran Gundae95d0732017-07-28 12:40:44 +05301039 return pmic_arb->intr + 0x4 + 0x1000 * n;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001040}
1041
Kiran Gundae95d0732017-07-28 12:40:44 +05301042static void __iomem *
David Collins40f318f2017-07-28 12:40:46 +05301043pmic_arb_irq_status_v5(struct spmi_pmic_arb *pmic_arb, u16 n)
1044{
1045 return pmic_arb->wr_base + 0x104 + 0x10000 * n;
1046}
1047
1048static void __iomem *
Kiran Gundae95d0732017-07-28 12:40:44 +05301049pmic_arb_irq_clear_v1(struct spmi_pmic_arb *pmic_arb, u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001050{
Kiran Gundae95d0732017-07-28 12:40:44 +05301051 return pmic_arb->intr + 0xA00 + 0x4 * n;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001052}
1053
Kiran Gundae95d0732017-07-28 12:40:44 +05301054static void __iomem *
1055pmic_arb_irq_clear_v2(struct spmi_pmic_arb *pmic_arb, u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001056{
Kiran Gundae95d0732017-07-28 12:40:44 +05301057 return pmic_arb->intr + 0x8 + 0x1000 * n;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001058}
1059
David Collins40f318f2017-07-28 12:40:46 +05301060static void __iomem *
1061pmic_arb_irq_clear_v5(struct spmi_pmic_arb *pmic_arb, u16 n)
1062{
1063 return pmic_arb->wr_base + 0x108 + 0x10000 * n;
1064}
1065
1066static u32 pmic_arb_apid_map_offset_v2(u16 n)
1067{
1068 return 0x800 + 0x4 * n;
1069}
1070
1071static u32 pmic_arb_apid_map_offset_v5(u16 n)
1072{
1073 return 0x900 + 0x4 * n;
1074}
1075
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001076static const struct pmic_arb_ver_ops pmic_arb_v1 = {
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +05301077 .ver_str = "v1",
Abhijeet Dharmapurikar7f1d4e52017-05-10 19:55:34 +05301078 .ppid_to_apid = pmic_arb_ppid_to_apid_v1,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001079 .non_data_cmd = pmic_arb_non_data_cmd_v1,
1080 .offset = pmic_arb_offset_v1,
1081 .fmt_cmd = pmic_arb_fmt_cmd_v1,
1082 .owner_acc_status = pmic_arb_owner_acc_status_v1,
1083 .acc_enable = pmic_arb_acc_enable_v1,
1084 .irq_status = pmic_arb_irq_status_v1,
1085 .irq_clear = pmic_arb_irq_clear_v1,
David Collins40f318f2017-07-28 12:40:46 +05301086 .apid_map_offset = pmic_arb_apid_map_offset_v2,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001087};
1088
1089static const struct pmic_arb_ver_ops pmic_arb_v2 = {
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +05301090 .ver_str = "v2",
Abhijeet Dharmapurikar7f1d4e52017-05-10 19:55:34 +05301091 .ppid_to_apid = pmic_arb_ppid_to_apid_v2,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001092 .non_data_cmd = pmic_arb_non_data_cmd_v2,
1093 .offset = pmic_arb_offset_v2,
1094 .fmt_cmd = pmic_arb_fmt_cmd_v2,
1095 .owner_acc_status = pmic_arb_owner_acc_status_v2,
1096 .acc_enable = pmic_arb_acc_enable_v2,
1097 .irq_status = pmic_arb_irq_status_v2,
1098 .irq_clear = pmic_arb_irq_clear_v2,
David Collins40f318f2017-07-28 12:40:46 +05301099 .apid_map_offset = pmic_arb_apid_map_offset_v2,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001100};
1101
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +05301102static const struct pmic_arb_ver_ops pmic_arb_v3 = {
1103 .ver_str = "v3",
1104 .ppid_to_apid = pmic_arb_ppid_to_apid_v2,
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +05301105 .non_data_cmd = pmic_arb_non_data_cmd_v2,
1106 .offset = pmic_arb_offset_v2,
1107 .fmt_cmd = pmic_arb_fmt_cmd_v2,
1108 .owner_acc_status = pmic_arb_owner_acc_status_v3,
1109 .acc_enable = pmic_arb_acc_enable_v2,
1110 .irq_status = pmic_arb_irq_status_v2,
1111 .irq_clear = pmic_arb_irq_clear_v2,
David Collins40f318f2017-07-28 12:40:46 +05301112 .apid_map_offset = pmic_arb_apid_map_offset_v2,
1113};
1114
1115static const struct pmic_arb_ver_ops pmic_arb_v5 = {
1116 .ver_str = "v5",
1117 .ppid_to_apid = pmic_arb_ppid_to_apid_v5,
1118 .non_data_cmd = pmic_arb_non_data_cmd_v2,
1119 .offset = pmic_arb_offset_v5,
1120 .fmt_cmd = pmic_arb_fmt_cmd_v2,
1121 .owner_acc_status = pmic_arb_owner_acc_status_v5,
1122 .acc_enable = pmic_arb_acc_enable_v5,
1123 .irq_status = pmic_arb_irq_status_v5,
1124 .irq_clear = pmic_arb_irq_clear_v5,
1125 .apid_map_offset = pmic_arb_apid_map_offset_v5,
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +05301126};
1127
Josh Cartwright67b563f2014-02-12 13:44:25 -06001128static const struct irq_domain_ops pmic_arb_irq_domain_ops = {
1129 .map = qpnpint_irq_domain_map,
1130 .xlate = qpnpint_irq_domain_dt_translate,
1131};
1132
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001133static int spmi_pmic_arb_probe(struct platform_device *pdev)
1134{
Kiran Gunda02abec32017-07-28 12:40:37 +05301135 struct spmi_pmic_arb *pmic_arb;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001136 struct spmi_controller *ctrl;
1137 struct resource *res;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001138 void __iomem *core;
Kiran Gunda4788e612017-07-28 12:40:40 +05301139 u32 *mapping_table;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001140 u32 channel, ee, hw_ver;
Stephen Boyd987a9f12015-11-17 16:13:55 -08001141 int err;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001142
Kiran Gunda02abec32017-07-28 12:40:37 +05301143 ctrl = spmi_controller_alloc(&pdev->dev, sizeof(*pmic_arb));
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001144 if (!ctrl)
1145 return -ENOMEM;
1146
Kiran Gunda02abec32017-07-28 12:40:37 +05301147 pmic_arb = spmi_controller_get_drvdata(ctrl);
1148 pmic_arb->spmic = ctrl;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001149
1150 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "core");
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001151 core = devm_ioremap_resource(&ctrl->dev, res);
1152 if (IS_ERR(core)) {
1153 err = PTR_ERR(core);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001154 goto err_put_ctrl;
1155 }
1156
Kiran Gunda000e1a42017-07-28 12:40:45 +05301157 pmic_arb->core_size = resource_size(res);
1158
Kiran Gunda02abec32017-07-28 12:40:37 +05301159 pmic_arb->ppid_to_apid = devm_kcalloc(&ctrl->dev, PMIC_ARB_MAX_PPID,
1160 sizeof(*pmic_arb->ppid_to_apid),
1161 GFP_KERNEL);
1162 if (!pmic_arb->ppid_to_apid) {
Stephen Boydeba97182017-06-26 19:17:46 -07001163 err = -ENOMEM;
1164 goto err_put_ctrl;
1165 }
1166
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001167 hw_ver = readl_relaxed(core + PMIC_ARB_VERSION);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001168
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +05301169 if (hw_ver < PMIC_ARB_VERSION_V2_MIN) {
Kiran Gunda02abec32017-07-28 12:40:37 +05301170 pmic_arb->ver_ops = &pmic_arb_v1;
1171 pmic_arb->wr_base = core;
1172 pmic_arb->rd_base = core;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001173 } else {
Kiran Gunda02abec32017-07-28 12:40:37 +05301174 pmic_arb->core = core;
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +05301175
1176 if (hw_ver < PMIC_ARB_VERSION_V3_MIN)
Kiran Gunda02abec32017-07-28 12:40:37 +05301177 pmic_arb->ver_ops = &pmic_arb_v2;
David Collins40f318f2017-07-28 12:40:46 +05301178 else if (hw_ver < PMIC_ARB_VERSION_V5_MIN)
Kiran Gunda02abec32017-07-28 12:40:37 +05301179 pmic_arb->ver_ops = &pmic_arb_v3;
David Collins40f318f2017-07-28 12:40:46 +05301180 else
1181 pmic_arb->ver_ops = &pmic_arb_v5;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001182
1183 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1184 "obsrvr");
Kiran Gunda02abec32017-07-28 12:40:37 +05301185 pmic_arb->rd_base = devm_ioremap_resource(&ctrl->dev, res);
1186 if (IS_ERR(pmic_arb->rd_base)) {
1187 err = PTR_ERR(pmic_arb->rd_base);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001188 goto err_put_ctrl;
1189 }
1190
1191 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1192 "chnls");
Kiran Gunda02abec32017-07-28 12:40:37 +05301193 pmic_arb->wr_base = devm_ioremap_resource(&ctrl->dev, res);
1194 if (IS_ERR(pmic_arb->wr_base)) {
1195 err = PTR_ERR(pmic_arb->wr_base);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001196 goto err_put_ctrl;
1197 }
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001198 }
1199
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +05301200 dev_info(&ctrl->dev, "PMIC arbiter version %s (0x%x)\n",
Kiran Gunda02abec32017-07-28 12:40:37 +05301201 pmic_arb->ver_ops->ver_str, hw_ver);
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +05301202
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001203 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "intr");
Kiran Gunda02abec32017-07-28 12:40:37 +05301204 pmic_arb->intr = devm_ioremap_resource(&ctrl->dev, res);
1205 if (IS_ERR(pmic_arb->intr)) {
1206 err = PTR_ERR(pmic_arb->intr);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001207 goto err_put_ctrl;
1208 }
1209
1210 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cnfg");
Kiran Gunda02abec32017-07-28 12:40:37 +05301211 pmic_arb->cnfg = devm_ioremap_resource(&ctrl->dev, res);
1212 if (IS_ERR(pmic_arb->cnfg)) {
1213 err = PTR_ERR(pmic_arb->cnfg);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001214 goto err_put_ctrl;
1215 }
1216
Kiran Gunda02abec32017-07-28 12:40:37 +05301217 pmic_arb->irq = platform_get_irq_byname(pdev, "periph_irq");
1218 if (pmic_arb->irq < 0) {
1219 err = pmic_arb->irq;
Josh Cartwright67b563f2014-02-12 13:44:25 -06001220 goto err_put_ctrl;
1221 }
1222
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001223 err = of_property_read_u32(pdev->dev.of_node, "qcom,channel", &channel);
1224 if (err) {
1225 dev_err(&pdev->dev, "channel unspecified.\n");
1226 goto err_put_ctrl;
1227 }
1228
1229 if (channel > 5) {
1230 dev_err(&pdev->dev, "invalid channel (%u) specified.\n",
1231 channel);
Christophe JAILLETe98cc182016-09-26 22:24:46 +02001232 err = -EINVAL;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001233 goto err_put_ctrl;
1234 }
1235
Kiran Gunda02abec32017-07-28 12:40:37 +05301236 pmic_arb->channel = channel;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001237
Josh Cartwright67b563f2014-02-12 13:44:25 -06001238 err = of_property_read_u32(pdev->dev.of_node, "qcom,ee", &ee);
1239 if (err) {
1240 dev_err(&pdev->dev, "EE unspecified.\n");
1241 goto err_put_ctrl;
1242 }
1243
1244 if (ee > 5) {
1245 dev_err(&pdev->dev, "invalid EE (%u) specified\n", ee);
1246 err = -EINVAL;
1247 goto err_put_ctrl;
1248 }
1249
Kiran Gunda02abec32017-07-28 12:40:37 +05301250 pmic_arb->ee = ee;
Kiran Gunda4788e612017-07-28 12:40:40 +05301251 mapping_table = devm_kcalloc(&ctrl->dev, PMIC_ARB_MAX_PERIPHS,
1252 sizeof(*mapping_table), GFP_KERNEL);
1253 if (!mapping_table) {
Stephen Boyd987a9f12015-11-17 16:13:55 -08001254 err = -ENOMEM;
1255 goto err_put_ctrl;
1256 }
Josh Cartwright67b563f2014-02-12 13:44:25 -06001257
Kiran Gunda4788e612017-07-28 12:40:40 +05301258 pmic_arb->mapping_table = mapping_table;
Josh Cartwright67b563f2014-02-12 13:44:25 -06001259 /* Initialize max_apid/min_apid to the opposite bounds, during
1260 * the irq domain translation, we are sure to update these */
Kiran Gunda02abec32017-07-28 12:40:37 +05301261 pmic_arb->max_apid = 0;
1262 pmic_arb->min_apid = PMIC_ARB_MAX_PERIPHS - 1;
Josh Cartwright67b563f2014-02-12 13:44:25 -06001263
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001264 platform_set_drvdata(pdev, ctrl);
Kiran Gunda02abec32017-07-28 12:40:37 +05301265 raw_spin_lock_init(&pmic_arb->lock);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001266
1267 ctrl->cmd = pmic_arb_cmd;
1268 ctrl->read_cmd = pmic_arb_read_cmd;
1269 ctrl->write_cmd = pmic_arb_write_cmd;
1270
David Collins40f318f2017-07-28 12:40:46 +05301271 if (hw_ver >= PMIC_ARB_VERSION_V5_MIN) {
1272 err = pmic_arb_read_apid_map_v5(pmic_arb);
1273 if (err) {
1274 dev_err(&pdev->dev, "could not read APID->PPID mapping table, rc= %d\n",
1275 err);
1276 goto err_put_ctrl;
1277 }
1278 }
1279
Josh Cartwright67b563f2014-02-12 13:44:25 -06001280 dev_dbg(&pdev->dev, "adding irq domain\n");
Kiran Gunda02abec32017-07-28 12:40:37 +05301281 pmic_arb->domain = irq_domain_add_tree(pdev->dev.of_node,
1282 &pmic_arb_irq_domain_ops, pmic_arb);
1283 if (!pmic_arb->domain) {
Josh Cartwright67b563f2014-02-12 13:44:25 -06001284 dev_err(&pdev->dev, "unable to create irq_domain\n");
1285 err = -ENOMEM;
1286 goto err_put_ctrl;
1287 }
1288
Kiran Gunda02abec32017-07-28 12:40:37 +05301289 irq_set_chained_handler_and_data(pmic_arb->irq, pmic_arb_chained_irq,
1290 pmic_arb);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001291 err = spmi_controller_add(ctrl);
1292 if (err)
Josh Cartwright67b563f2014-02-12 13:44:25 -06001293 goto err_domain_remove;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001294
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001295 return 0;
1296
Josh Cartwright67b563f2014-02-12 13:44:25 -06001297err_domain_remove:
Kiran Gunda02abec32017-07-28 12:40:37 +05301298 irq_set_chained_handler_and_data(pmic_arb->irq, NULL, NULL);
1299 irq_domain_remove(pmic_arb->domain);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001300err_put_ctrl:
1301 spmi_controller_put(ctrl);
1302 return err;
1303}
1304
1305static int spmi_pmic_arb_remove(struct platform_device *pdev)
1306{
1307 struct spmi_controller *ctrl = platform_get_drvdata(pdev);
Kiran Gunda02abec32017-07-28 12:40:37 +05301308 struct spmi_pmic_arb *pmic_arb = spmi_controller_get_drvdata(ctrl);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001309 spmi_controller_remove(ctrl);
Kiran Gunda02abec32017-07-28 12:40:37 +05301310 irq_set_chained_handler_and_data(pmic_arb->irq, NULL, NULL);
1311 irq_domain_remove(pmic_arb->domain);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001312 spmi_controller_put(ctrl);
1313 return 0;
1314}
1315
1316static const struct of_device_id spmi_pmic_arb_match_table[] = {
1317 { .compatible = "qcom,spmi-pmic-arb", },
1318 {},
1319};
1320MODULE_DEVICE_TABLE(of, spmi_pmic_arb_match_table);
1321
1322static struct platform_driver spmi_pmic_arb_driver = {
1323 .probe = spmi_pmic_arb_probe,
1324 .remove = spmi_pmic_arb_remove,
1325 .driver = {
1326 .name = "spmi_pmic_arb",
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001327 .of_match_table = spmi_pmic_arb_match_table,
1328 },
1329};
1330module_platform_driver(spmi_pmic_arb_driver);
1331
1332MODULE_LICENSE("GPL v2");
1333MODULE_ALIAS("platform:spmi_pmic_arb");