blob: 2f9875a25e00333668004671dcfeb684c36e563c [file] [log] [blame]
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001/*
2 * Copyright (c) 2012-2015, 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
Kenneth Heitke39ae93e2014-02-12 13:44:24 -060032#define PMIC_ARB_INT_EN 0x0004
33
Gilad Avidovd0c6ae42015-03-25 11:37:32 -060034/* PMIC Arbiter channel registers offsets */
35#define PMIC_ARB_CMD 0x00
36#define PMIC_ARB_CONFIG 0x04
37#define PMIC_ARB_STATUS 0x08
38#define PMIC_ARB_WDATA0 0x10
39#define PMIC_ARB_WDATA1 0x14
40#define PMIC_ARB_RDATA0 0x18
41#define PMIC_ARB_RDATA1 0x1C
Kiran Gunda02abec32017-07-28 12:40:37 +053042#define PMIC_ARB_REG_APID(N) (0x800 + 0x4 * (N))
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)
Kenneth Heitke39ae93e2014-02-12 13:44:24 -060055
56/* Ownership Table */
57#define SPMI_OWNERSHIP_TABLE_REG(N) (0x0700 + (4 * (N)))
58#define SPMI_OWNERSHIP_PERIPH2OWNER(X) ((X) & 0x7)
59
60/* Channel Status fields */
61enum pmic_arb_chnl_status {
Abhijeet Dharmapurikar111a10b2017-05-10 19:55:32 +053062 PMIC_ARB_STATUS_DONE = BIT(0),
63 PMIC_ARB_STATUS_FAILURE = BIT(1),
64 PMIC_ARB_STATUS_DENIED = BIT(2),
65 PMIC_ARB_STATUS_DROPPED = BIT(3),
Kenneth Heitke39ae93e2014-02-12 13:44:24 -060066};
67
68/* Command register fields */
69#define PMIC_ARB_CMD_MAX_BYTE_COUNT 8
70
71/* Command Opcodes */
72enum pmic_arb_cmd_op_code {
73 PMIC_ARB_OP_EXT_WRITEL = 0,
74 PMIC_ARB_OP_EXT_READL = 1,
75 PMIC_ARB_OP_EXT_WRITE = 2,
76 PMIC_ARB_OP_RESET = 3,
77 PMIC_ARB_OP_SLEEP = 4,
78 PMIC_ARB_OP_SHUTDOWN = 5,
79 PMIC_ARB_OP_WAKEUP = 6,
80 PMIC_ARB_OP_AUTHENTICATE = 7,
81 PMIC_ARB_OP_MSTR_READ = 8,
82 PMIC_ARB_OP_MSTR_WRITE = 9,
83 PMIC_ARB_OP_EXT_READ = 13,
84 PMIC_ARB_OP_WRITE = 14,
85 PMIC_ARB_OP_READ = 15,
86 PMIC_ARB_OP_ZERO_WRITE = 16,
87};
88
89/* Maximum number of support PMIC peripherals */
Stephen Boyd987a9f12015-11-17 16:13:55 -080090#define PMIC_ARB_MAX_PERIPHS 512
Kenneth Heitke39ae93e2014-02-12 13:44:24 -060091#define PMIC_ARB_TIMEOUT_US 100
92#define PMIC_ARB_MAX_TRANS_BYTES (8)
93
94#define PMIC_ARB_APID_MASK 0xFF
95#define PMIC_ARB_PPID_MASK 0xFFF
96
97/* interrupt enable bit */
98#define SPMI_PIC_ACC_ENABLE_BIT BIT(0)
99
Kiran Gunda02abec32017-07-28 12:40:37 +0530100#define spec_to_hwirq(slave_id, periph_id, irq_id, apid) \
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +0530101 ((((slave_id) & 0xF) << 28) | \
102 (((periph_id) & 0xFF) << 20) | \
103 (((irq_id) & 0x7) << 16) | \
104 (((apid) & 0x1FF) << 0))
105
Kiran Gunda02abec32017-07-28 12:40:37 +0530106#define hwirq_to_sid(hwirq) (((hwirq) >> 28) & 0xF)
107#define hwirq_to_per(hwirq) (((hwirq) >> 20) & 0xFF)
108#define hwirq_to_irq(hwirq) (((hwirq) >> 16) & 0x7)
109#define hwirq_to_apid(hwirq) (((hwirq) >> 0) & 0x1FF)
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +0530110
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600111struct pmic_arb_ver_ops;
112
Abhijeet Dharmapurikar6bc546e2017-05-10 19:55:35 +0530113struct apid_data {
114 u16 ppid;
115 u8 owner;
116};
117
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600118/**
Abhijeet Dharmapurikar111a10b2017-05-10 19:55:32 +0530119 * spmi_pmic_arb - SPMI PMIC Arbiter object
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600120 *
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600121 * @rd_base: on v1 "core", on v2 "observer" register base off DT.
122 * @wr_base: on v1 "core", on v2 "chnls" register base off DT.
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600123 * @intr: address of the SPMI interrupt control registers.
124 * @cnfg: address of the PMIC Arbiter configuration registers.
125 * @lock: lock to synchronize accesses.
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600126 * @channel: execution environment channel to use for accesses.
Josh Cartwright67b563f2014-02-12 13:44:25 -0600127 * @irq: PMIC ARB interrupt.
128 * @ee: the current Execution Environment
129 * @min_apid: minimum APID (used for bounding IRQ search)
130 * @max_apid: maximum APID
131 * @mapping_table: in-memory copy of PPID -> APID mapping table.
132 * @domain: irq domain object for PMIC IRQ domain
133 * @spmic: SPMI controller object
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600134 * @ver_ops: version dependent operations.
Kiran Gunda02abec32017-07-28 12:40:37 +0530135 * @ppid_to_apid in-memory copy of PPID -> APID mapping table.
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600136 */
Abhijeet Dharmapurikar111a10b2017-05-10 19:55:32 +0530137struct spmi_pmic_arb {
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600138 void __iomem *rd_base;
139 void __iomem *wr_base;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600140 void __iomem *intr;
141 void __iomem *cnfg;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800142 void __iomem *core;
143 resource_size_t core_size;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600144 raw_spinlock_t lock;
145 u8 channel;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600146 int irq;
147 u8 ee;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800148 u16 min_apid;
149 u16 max_apid;
150 u32 *mapping_table;
151 DECLARE_BITMAP(mapping_table_valid, PMIC_ARB_MAX_PERIPHS);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600152 struct irq_domain *domain;
153 struct spmi_controller *spmic;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600154 const struct pmic_arb_ver_ops *ver_ops;
Abhijeet Dharmapurikar1ef1ce42017-05-10 19:55:33 +0530155 u16 *ppid_to_apid;
156 u16 last_apid;
Abhijeet Dharmapurikar6bc546e2017-05-10 19:55:35 +0530157 struct apid_data apid_data[PMIC_ARB_MAX_PERIPHS];
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600158};
159
160/**
161 * pmic_arb_ver: version dependent functionality.
162 *
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +0530163 * @ver_str: version string.
164 * @ppid_to_apid: finds the apid for a given ppid.
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600165 * @non_data_cmd: on v1 issues an spmi non-data command.
166 * on v2 no HW support, returns -EOPNOTSUPP.
167 * @offset: on v1 offset of per-ee channel.
168 * on v2 offset of per-ee and per-ppid channel.
169 * @fmt_cmd: formats a GENI/SPMI command.
170 * @owner_acc_status: on v1 offset of PMIC_ARB_SPMI_PIC_OWNERm_ACC_STATUSn
171 * on v2 offset of SPMI_PIC_OWNERm_ACC_STATUSn.
172 * @acc_enable: on v1 offset of PMIC_ARB_SPMI_PIC_ACC_ENABLEn
173 * on v2 offset of SPMI_PIC_ACC_ENABLEn.
174 * @irq_status: on v1 offset of PMIC_ARB_SPMI_PIC_IRQ_STATUSn
175 * on v2 offset of SPMI_PIC_IRQ_STATUSn.
176 * @irq_clear: on v1 offset of PMIC_ARB_SPMI_PIC_IRQ_CLEARn
177 * on v2 offset of SPMI_PIC_IRQ_CLEARn.
178 */
179struct pmic_arb_ver_ops {
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +0530180 const char *ver_str;
Kiran Gundaff615ed2017-07-28 12:40:42 +0530181 int (*ppid_to_apid)(struct spmi_pmic_arb *pmic_arb, u16 ppid);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600182 /* spmi commands (read_cmd, write_cmd, cmd) functionality */
Kiran Gundaff615ed2017-07-28 12:40:42 +0530183 int (*offset)(struct spmi_pmic_arb *pmic_arb, u8 sid, u16 addr);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600184 u32 (*fmt_cmd)(u8 opc, u8 sid, u16 addr, u8 bc);
185 int (*non_data_cmd)(struct spmi_controller *ctrl, u8 opc, u8 sid);
186 /* Interrupts controller functionality (offset of PIC registers) */
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +0530187 u32 (*owner_acc_status)(u8 m, u16 n);
188 u32 (*acc_enable)(u16 n);
189 u32 (*irq_status)(u16 n);
190 u32 (*irq_clear)(u16 n);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600191};
192
Kiran Gunda02abec32017-07-28 12:40:37 +0530193static inline void pmic_arb_base_write(struct spmi_pmic_arb *pmic_arb,
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600194 u32 offset, u32 val)
195{
Kiran Gunda02abec32017-07-28 12:40:37 +0530196 writel_relaxed(val, pmic_arb->wr_base + offset);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600197}
198
Kiran Gunda02abec32017-07-28 12:40:37 +0530199static inline void pmic_arb_set_rd_cmd(struct spmi_pmic_arb *pmic_arb,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600200 u32 offset, u32 val)
201{
Kiran Gunda02abec32017-07-28 12:40:37 +0530202 writel_relaxed(val, pmic_arb->rd_base + offset);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600203}
204
205/**
Kiran Gunda02abec32017-07-28 12:40:37 +0530206 * pmic_arb_read_data: reads pmic-arb's register and copy 1..4 bytes to buf
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600207 * @bc: byte count -1. range: 0..3
208 * @reg: register's address
209 * @buf: output parameter, length must be bc + 1
210 */
Kiran Gunda02abec32017-07-28 12:40:37 +0530211static void
212pmic_arb_read_data(struct spmi_pmic_arb *pmic_arb, u8 *buf, u32 reg, u8 bc)
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600213{
Kiran Gunda02abec32017-07-28 12:40:37 +0530214 u32 data = __raw_readl(pmic_arb->rd_base + reg);
Abhijeet Dharmapurikar111a10b2017-05-10 19:55:32 +0530215
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600216 memcpy(buf, &data, (bc & 3) + 1);
217}
218
219/**
Kiran Gunda02abec32017-07-28 12:40:37 +0530220 * pmic_arb_write_data: write 1..4 bytes from buf to pmic-arb's register
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600221 * @bc: byte-count -1. range: 0..3.
222 * @reg: register's address.
223 * @buf: buffer to write. length must be bc + 1.
224 */
Kiran Gunda02abec32017-07-28 12:40:37 +0530225static void pmic_arb_write_data(struct spmi_pmic_arb *pmic_arb, const u8 *buf,
226 u32 reg, u8 bc)
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600227{
228 u32 data = 0;
Abhijeet Dharmapurikar111a10b2017-05-10 19:55:32 +0530229
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600230 memcpy(&data, buf, (bc & 3) + 1);
Kiran Gunda9f7a9a42017-07-28 12:40:41 +0530231 __raw_writel(data, pmic_arb->wr_base + reg);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600232}
233
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600234static int pmic_arb_wait_for_done(struct spmi_controller *ctrl,
235 void __iomem *base, u8 sid, u16 addr)
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600236{
Kiran Gunda02abec32017-07-28 12:40:37 +0530237 struct spmi_pmic_arb *pmic_arb = spmi_controller_get_drvdata(ctrl);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600238 u32 status = 0;
239 u32 timeout = PMIC_ARB_TIMEOUT_US;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800240 u32 offset;
241 int rc;
242
Kiran Gundaff615ed2017-07-28 12:40:42 +0530243 rc = pmic_arb->ver_ops->offset(pmic_arb, sid, addr);
244 if (rc < 0)
Stephen Boyd987a9f12015-11-17 16:13:55 -0800245 return rc;
246
Kiran Gundaff615ed2017-07-28 12:40:42 +0530247 offset = rc;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800248 offset += PMIC_ARB_STATUS;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600249
250 while (timeout--) {
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600251 status = readl_relaxed(base + offset);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600252
253 if (status & PMIC_ARB_STATUS_DONE) {
254 if (status & PMIC_ARB_STATUS_DENIED) {
Kiran Gunda02abec32017-07-28 12:40:37 +0530255 dev_err(&ctrl->dev, "%s: transaction denied (0x%x)\n",
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600256 __func__, status);
257 return -EPERM;
258 }
259
260 if (status & PMIC_ARB_STATUS_FAILURE) {
Kiran Gunda02abec32017-07-28 12:40:37 +0530261 dev_err(&ctrl->dev, "%s: transaction failed (0x%x)\n",
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600262 __func__, status);
263 return -EIO;
264 }
265
266 if (status & PMIC_ARB_STATUS_DROPPED) {
Kiran Gunda02abec32017-07-28 12:40:37 +0530267 dev_err(&ctrl->dev, "%s: transaction dropped (0x%x)\n",
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600268 __func__, status);
269 return -EIO;
270 }
271
272 return 0;
273 }
274 udelay(1);
275 }
276
Kiran Gunda02abec32017-07-28 12:40:37 +0530277 dev_err(&ctrl->dev, "%s: timeout, status 0x%x\n",
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600278 __func__, status);
279 return -ETIMEDOUT;
280}
281
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600282static int
283pmic_arb_non_data_cmd_v1(struct spmi_controller *ctrl, u8 opc, u8 sid)
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600284{
Kiran Gunda02abec32017-07-28 12:40:37 +0530285 struct spmi_pmic_arb *pmic_arb = spmi_controller_get_drvdata(ctrl);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600286 unsigned long flags;
287 u32 cmd;
288 int rc;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800289 u32 offset;
290
Kiran Gundaff615ed2017-07-28 12:40:42 +0530291 rc = pmic_arb->ver_ops->offset(pmic_arb, sid, 0);
292 if (rc < 0)
Stephen Boyd987a9f12015-11-17 16:13:55 -0800293 return rc;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600294
Kiran Gundaff615ed2017-07-28 12:40:42 +0530295 offset = rc;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600296 cmd = ((opc | 0x40) << 27) | ((sid & 0xf) << 20);
297
Kiran Gunda02abec32017-07-28 12:40:37 +0530298 raw_spin_lock_irqsave(&pmic_arb->lock, flags);
299 pmic_arb_base_write(pmic_arb, offset + PMIC_ARB_CMD, cmd);
300 rc = pmic_arb_wait_for_done(ctrl, pmic_arb->wr_base, sid, 0);
301 raw_spin_unlock_irqrestore(&pmic_arb->lock, flags);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600302
303 return rc;
304}
305
306static int
307pmic_arb_non_data_cmd_v2(struct spmi_controller *ctrl, u8 opc, u8 sid)
308{
309 return -EOPNOTSUPP;
310}
311
312/* Non-data command */
313static int pmic_arb_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid)
314{
Kiran Gunda02abec32017-07-28 12:40:37 +0530315 struct spmi_pmic_arb *pmic_arb = spmi_controller_get_drvdata(ctrl);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600316
317 dev_dbg(&ctrl->dev, "cmd op:0x%x sid:%d\n", opc, sid);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600318
319 /* Check for valid non-data command */
320 if (opc < SPMI_CMD_RESET || opc > SPMI_CMD_WAKEUP)
321 return -EINVAL;
322
Kiran Gunda02abec32017-07-28 12:40:37 +0530323 return pmic_arb->ver_ops->non_data_cmd(ctrl, opc, sid);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600324}
325
326static int pmic_arb_read_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
327 u16 addr, u8 *buf, size_t len)
328{
Kiran Gunda02abec32017-07-28 12:40:37 +0530329 struct spmi_pmic_arb *pmic_arb = spmi_controller_get_drvdata(ctrl);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600330 unsigned long flags;
331 u8 bc = len - 1;
332 u32 cmd;
333 int rc;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800334 u32 offset;
335
Kiran Gundaff615ed2017-07-28 12:40:42 +0530336 rc = pmic_arb->ver_ops->offset(pmic_arb, sid, addr);
337 if (rc < 0)
Stephen Boyd987a9f12015-11-17 16:13:55 -0800338 return rc;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600339
Kiran Gundaff615ed2017-07-28 12:40:42 +0530340 offset = rc;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600341 if (bc >= PMIC_ARB_MAX_TRANS_BYTES) {
Kiran Gunda02abec32017-07-28 12:40:37 +0530342 dev_err(&ctrl->dev, "pmic-arb supports 1..%d bytes per trans, but:%zu requested",
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600343 PMIC_ARB_MAX_TRANS_BYTES, len);
344 return -EINVAL;
345 }
346
347 /* Check the opcode */
348 if (opc >= 0x60 && opc <= 0x7F)
349 opc = PMIC_ARB_OP_READ;
350 else if (opc >= 0x20 && opc <= 0x2F)
351 opc = PMIC_ARB_OP_EXT_READ;
352 else if (opc >= 0x38 && opc <= 0x3F)
353 opc = PMIC_ARB_OP_EXT_READL;
354 else
355 return -EINVAL;
356
Kiran Gunda02abec32017-07-28 12:40:37 +0530357 cmd = pmic_arb->ver_ops->fmt_cmd(opc, sid, addr, bc);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600358
Kiran Gunda02abec32017-07-28 12:40:37 +0530359 raw_spin_lock_irqsave(&pmic_arb->lock, flags);
360 pmic_arb_set_rd_cmd(pmic_arb, offset + PMIC_ARB_CMD, cmd);
361 rc = pmic_arb_wait_for_done(ctrl, pmic_arb->rd_base, sid, addr);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600362 if (rc)
363 goto done;
364
Kiran Gunda02abec32017-07-28 12:40:37 +0530365 pmic_arb_read_data(pmic_arb, buf, offset + PMIC_ARB_RDATA0,
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600366 min_t(u8, bc, 3));
367
368 if (bc > 3)
Kiran Gunda02abec32017-07-28 12:40:37 +0530369 pmic_arb_read_data(pmic_arb, buf + 4, offset + PMIC_ARB_RDATA1,
370 bc - 4);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600371
372done:
Kiran Gunda02abec32017-07-28 12:40:37 +0530373 raw_spin_unlock_irqrestore(&pmic_arb->lock, flags);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600374 return rc;
375}
376
377static int pmic_arb_write_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
378 u16 addr, const u8 *buf, size_t len)
379{
Kiran Gunda02abec32017-07-28 12:40:37 +0530380 struct spmi_pmic_arb *pmic_arb = spmi_controller_get_drvdata(ctrl);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600381 unsigned long flags;
382 u8 bc = len - 1;
383 u32 cmd;
384 int rc;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800385 u32 offset;
386
Kiran Gundaff615ed2017-07-28 12:40:42 +0530387 rc = pmic_arb->ver_ops->offset(pmic_arb, sid, addr);
388 if (rc < 0)
Stephen Boyd987a9f12015-11-17 16:13:55 -0800389 return rc;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600390
Kiran Gundaff615ed2017-07-28 12:40:42 +0530391 offset = rc;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600392 if (bc >= PMIC_ARB_MAX_TRANS_BYTES) {
Kiran Gunda02abec32017-07-28 12:40:37 +0530393 dev_err(&ctrl->dev, "pmic-arb supports 1..%d bytes per trans, but:%zu requested",
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600394 PMIC_ARB_MAX_TRANS_BYTES, len);
395 return -EINVAL;
396 }
397
398 /* Check the opcode */
399 if (opc >= 0x40 && opc <= 0x5F)
400 opc = PMIC_ARB_OP_WRITE;
401 else if (opc >= 0x00 && opc <= 0x0F)
402 opc = PMIC_ARB_OP_EXT_WRITE;
403 else if (opc >= 0x30 && opc <= 0x37)
404 opc = PMIC_ARB_OP_EXT_WRITEL;
Stephen Boyd9b769682015-08-28 12:31:10 -0700405 else if (opc >= 0x80)
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600406 opc = PMIC_ARB_OP_ZERO_WRITE;
407 else
408 return -EINVAL;
409
Kiran Gunda02abec32017-07-28 12:40:37 +0530410 cmd = pmic_arb->ver_ops->fmt_cmd(opc, sid, addr, bc);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600411
412 /* Write data to FIFOs */
Kiran Gunda02abec32017-07-28 12:40:37 +0530413 raw_spin_lock_irqsave(&pmic_arb->lock, flags);
414 pmic_arb_write_data(pmic_arb, buf, offset + PMIC_ARB_WDATA0,
415 min_t(u8, bc, 3));
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600416 if (bc > 3)
Kiran Gunda02abec32017-07-28 12:40:37 +0530417 pmic_arb_write_data(pmic_arb, buf + 4, offset + PMIC_ARB_WDATA1,
418 bc - 4);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600419
420 /* Start the transaction */
Kiran Gunda02abec32017-07-28 12:40:37 +0530421 pmic_arb_base_write(pmic_arb, offset + PMIC_ARB_CMD, cmd);
422 rc = pmic_arb_wait_for_done(ctrl, pmic_arb->wr_base, sid, addr);
423 raw_spin_unlock_irqrestore(&pmic_arb->lock, flags);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600424
425 return rc;
426}
427
Josh Cartwright67b563f2014-02-12 13:44:25 -0600428enum qpnpint_regs {
429 QPNPINT_REG_RT_STS = 0x10,
430 QPNPINT_REG_SET_TYPE = 0x11,
431 QPNPINT_REG_POLARITY_HIGH = 0x12,
432 QPNPINT_REG_POLARITY_LOW = 0x13,
433 QPNPINT_REG_LATCHED_CLR = 0x14,
434 QPNPINT_REG_EN_SET = 0x15,
435 QPNPINT_REG_EN_CLR = 0x16,
436 QPNPINT_REG_LATCHED_STS = 0x18,
437};
438
439struct spmi_pmic_arb_qpnpint_type {
440 u8 type; /* 1 -> edge */
441 u8 polarity_high;
442 u8 polarity_low;
443} __packed;
444
445/* Simplified accessor functions for irqchip callbacks */
446static void qpnpint_spmi_write(struct irq_data *d, u8 reg, void *buf,
447 size_t len)
448{
Kiran Gunda02abec32017-07-28 12:40:37 +0530449 struct spmi_pmic_arb *pmic_arb = irq_data_get_irq_chip_data(d);
450 u8 sid = hwirq_to_sid(d->hwirq);
451 u8 per = hwirq_to_per(d->hwirq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600452
Kiran Gunda02abec32017-07-28 12:40:37 +0530453 if (pmic_arb_write_cmd(pmic_arb->spmic, SPMI_CMD_EXT_WRITEL, sid,
Josh Cartwright67b563f2014-02-12 13:44:25 -0600454 (per << 8) + reg, buf, len))
Kiran Gunda02abec32017-07-28 12:40:37 +0530455 dev_err_ratelimited(&pmic_arb->spmic->dev, "failed irqchip transaction on %x\n",
Josh Cartwright67b563f2014-02-12 13:44:25 -0600456 d->irq);
457}
458
459static void qpnpint_spmi_read(struct irq_data *d, u8 reg, void *buf, size_t len)
460{
Kiran Gunda02abec32017-07-28 12:40:37 +0530461 struct spmi_pmic_arb *pmic_arb = irq_data_get_irq_chip_data(d);
462 u8 sid = hwirq_to_sid(d->hwirq);
463 u8 per = hwirq_to_per(d->hwirq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600464
Kiran Gunda02abec32017-07-28 12:40:37 +0530465 if (pmic_arb_read_cmd(pmic_arb->spmic, SPMI_CMD_EXT_READL, sid,
Josh Cartwright67b563f2014-02-12 13:44:25 -0600466 (per << 8) + reg, buf, len))
Kiran Gunda02abec32017-07-28 12:40:37 +0530467 dev_err_ratelimited(&pmic_arb->spmic->dev, "failed irqchip transaction on %x\n",
Josh Cartwright67b563f2014-02-12 13:44:25 -0600468 d->irq);
469}
470
Kiran Gunda02abec32017-07-28 12:40:37 +0530471static void cleanup_irq(struct spmi_pmic_arb *pmic_arb, u16 apid, int id)
Abhijeet Dharmapurikar6bc546e2017-05-10 19:55:35 +0530472{
Kiran Gunda02abec32017-07-28 12:40:37 +0530473 u16 ppid = pmic_arb->apid_data[apid].ppid;
Abhijeet Dharmapurikar6bc546e2017-05-10 19:55:35 +0530474 u8 sid = ppid >> 8;
475 u8 per = ppid & 0xFF;
476 u8 irq_mask = BIT(id);
477
Kiran Gunda02abec32017-07-28 12:40:37 +0530478 writel_relaxed(irq_mask, pmic_arb->intr +
479 pmic_arb->ver_ops->irq_clear(apid));
Abhijeet Dharmapurikar6bc546e2017-05-10 19:55:35 +0530480
Kiran Gunda02abec32017-07-28 12:40:37 +0530481 if (pmic_arb_write_cmd(pmic_arb->spmic, SPMI_CMD_EXT_WRITEL, sid,
Abhijeet Dharmapurikar6bc546e2017-05-10 19:55:35 +0530482 (per << 8) + QPNPINT_REG_LATCHED_CLR, &irq_mask, 1))
Kiran Gunda02abec32017-07-28 12:40:37 +0530483 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 +0530484 irq_mask, ppid);
485
Kiran Gunda02abec32017-07-28 12:40:37 +0530486 if (pmic_arb_write_cmd(pmic_arb->spmic, SPMI_CMD_EXT_WRITEL, sid,
Abhijeet Dharmapurikar6bc546e2017-05-10 19:55:35 +0530487 (per << 8) + QPNPINT_REG_EN_CLR, &irq_mask, 1))
Kiran Gunda02abec32017-07-28 12:40:37 +0530488 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 +0530489 irq_mask, ppid);
490}
491
Kiran Gunda02abec32017-07-28 12:40:37 +0530492static void periph_interrupt(struct spmi_pmic_arb *pmic_arb, u16 apid)
Josh Cartwright67b563f2014-02-12 13:44:25 -0600493{
494 unsigned int irq;
495 u32 status;
496 int id;
Kiran Gunda02abec32017-07-28 12:40:37 +0530497 u8 sid = (pmic_arb->apid_data[apid].ppid >> 8) & 0xF;
498 u8 per = pmic_arb->apid_data[apid].ppid & 0xFF;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600499
Kiran Gunda02abec32017-07-28 12:40:37 +0530500 status = readl_relaxed(pmic_arb->intr +
501 pmic_arb->ver_ops->irq_status(apid));
Josh Cartwright67b563f2014-02-12 13:44:25 -0600502 while (status) {
503 id = ffs(status) - 1;
Abhijeet Dharmapurikar111a10b2017-05-10 19:55:32 +0530504 status &= ~BIT(id);
Kiran Gunda02abec32017-07-28 12:40:37 +0530505 irq = irq_find_mapping(pmic_arb->domain,
506 spec_to_hwirq(sid, per, id, apid));
Abhijeet Dharmapurikar6bc546e2017-05-10 19:55:35 +0530507 if (irq == 0) {
Kiran Gunda02abec32017-07-28 12:40:37 +0530508 cleanup_irq(pmic_arb, apid, id);
Abhijeet Dharmapurikar6bc546e2017-05-10 19:55:35 +0530509 continue;
510 }
Josh Cartwright67b563f2014-02-12 13:44:25 -0600511 generic_handle_irq(irq);
512 }
513}
514
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200515static void pmic_arb_chained_irq(struct irq_desc *desc)
Josh Cartwright67b563f2014-02-12 13:44:25 -0600516{
Kiran Gunda02abec32017-07-28 12:40:37 +0530517 struct spmi_pmic_arb *pmic_arb = irq_desc_get_handler_data(desc);
Jiang Liu7fe88f32015-07-13 20:52:25 +0000518 struct irq_chip *chip = irq_desc_get_chip(desc);
Kiran Gunda02abec32017-07-28 12:40:37 +0530519 void __iomem *intr = pmic_arb->intr;
520 int first = pmic_arb->min_apid >> 5;
521 int last = pmic_arb->max_apid >> 5;
Abhijeet Dharmapurikar472eaf82017-05-10 19:55:39 +0530522 u32 status, enable;
523 int i, id, apid;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600524
525 chained_irq_enter(chip, desc);
526
527 for (i = first; i <= last; ++i) {
528 status = readl_relaxed(intr +
Kiran Gunda02abec32017-07-28 12:40:37 +0530529 pmic_arb->ver_ops->owner_acc_status(pmic_arb->ee, i));
Josh Cartwright67b563f2014-02-12 13:44:25 -0600530 while (status) {
531 id = ffs(status) - 1;
Abhijeet Dharmapurikar111a10b2017-05-10 19:55:32 +0530532 status &= ~BIT(id);
Abhijeet Dharmapurikar472eaf82017-05-10 19:55:39 +0530533 apid = id + i * 32;
534 enable = readl_relaxed(intr +
Kiran Gunda02abec32017-07-28 12:40:37 +0530535 pmic_arb->ver_ops->acc_enable(apid));
Abhijeet Dharmapurikar472eaf82017-05-10 19:55:39 +0530536 if (enable & SPMI_PIC_ACC_ENABLE_BIT)
Kiran Gunda02abec32017-07-28 12:40:37 +0530537 periph_interrupt(pmic_arb, apid);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600538 }
539 }
540
541 chained_irq_exit(chip, desc);
542}
543
544static void qpnpint_irq_ack(struct irq_data *d)
545{
Kiran Gunda02abec32017-07-28 12:40:37 +0530546 struct spmi_pmic_arb *pmic_arb = irq_data_get_irq_chip_data(d);
547 u8 irq = hwirq_to_irq(d->hwirq);
548 u16 apid = hwirq_to_apid(d->hwirq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600549 u8 data;
550
Kiran Gunda02abec32017-07-28 12:40:37 +0530551 writel_relaxed(BIT(irq), pmic_arb->intr +
552 pmic_arb->ver_ops->irq_clear(apid));
Josh Cartwright67b563f2014-02-12 13:44:25 -0600553
Abhijeet Dharmapurikar111a10b2017-05-10 19:55:32 +0530554 data = BIT(irq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600555 qpnpint_spmi_write(d, QPNPINT_REG_LATCHED_CLR, &data, 1);
556}
557
558static void qpnpint_irq_mask(struct irq_data *d)
559{
Kiran Gunda02abec32017-07-28 12:40:37 +0530560 u8 irq = hwirq_to_irq(d->hwirq);
Abhijeet Dharmapurikar6bc546e2017-05-10 19:55:35 +0530561 u8 data = BIT(irq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600562
Josh Cartwright67b563f2014-02-12 13:44:25 -0600563 qpnpint_spmi_write(d, QPNPINT_REG_EN_CLR, &data, 1);
564}
565
566static void qpnpint_irq_unmask(struct irq_data *d)
567{
Kiran Gunda02abec32017-07-28 12:40:37 +0530568 struct spmi_pmic_arb *pmic_arb = irq_data_get_irq_chip_data(d);
569 u8 irq = hwirq_to_irq(d->hwirq);
570 u16 apid = hwirq_to_apid(d->hwirq);
Abhijeet Dharmapurikarcee0fad2017-05-10 19:55:37 +0530571 u8 buf[2];
Josh Cartwright67b563f2014-02-12 13:44:25 -0600572
Abhijeet Dharmapurikar6bc546e2017-05-10 19:55:35 +0530573 writel_relaxed(SPMI_PIC_ACC_ENABLE_BIT,
Kiran Gunda02abec32017-07-28 12:40:37 +0530574 pmic_arb->intr + pmic_arb->ver_ops->acc_enable(apid));
Josh Cartwright67b563f2014-02-12 13:44:25 -0600575
Abhijeet Dharmapurikarcee0fad2017-05-10 19:55:37 +0530576 qpnpint_spmi_read(d, QPNPINT_REG_EN_SET, &buf[0], 1);
577 if (!(buf[0] & BIT(irq))) {
578 /*
579 * Since the interrupt is currently disabled, write to both the
580 * LATCHED_CLR and EN_SET registers so that a spurious interrupt
581 * cannot be triggered when the interrupt is enabled
582 */
583 buf[0] = BIT(irq);
584 buf[1] = BIT(irq);
585 qpnpint_spmi_write(d, QPNPINT_REG_LATCHED_CLR, &buf, 2);
586 }
Josh Cartwright67b563f2014-02-12 13:44:25 -0600587}
588
Josh Cartwright67b563f2014-02-12 13:44:25 -0600589static int qpnpint_irq_set_type(struct irq_data *d, unsigned int flow_type)
590{
591 struct spmi_pmic_arb_qpnpint_type type;
Kiran Gunda325255b2017-07-28 12:40:39 +0530592 irq_flow_handler_t flow_handler;
Kiran Gunda02abec32017-07-28 12:40:37 +0530593 u8 irq = hwirq_to_irq(d->hwirq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600594
595 qpnpint_spmi_read(d, QPNPINT_REG_SET_TYPE, &type, sizeof(type));
596
597 if (flow_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)) {
Kiran Gunda325255b2017-07-28 12:40:39 +0530598 type.type |= BIT(irq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600599 if (flow_type & IRQF_TRIGGER_RISING)
Kiran Gunda325255b2017-07-28 12:40:39 +0530600 type.polarity_high |= BIT(irq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600601 if (flow_type & IRQF_TRIGGER_FALLING)
Kiran Gunda325255b2017-07-28 12:40:39 +0530602 type.polarity_low |= BIT(irq);
603
604 flow_handler = handle_edge_irq;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600605 } else {
606 if ((flow_type & (IRQF_TRIGGER_HIGH)) &&
607 (flow_type & (IRQF_TRIGGER_LOW)))
608 return -EINVAL;
609
Kiran Gunda325255b2017-07-28 12:40:39 +0530610 type.type &= ~BIT(irq); /* level trig */
Josh Cartwright67b563f2014-02-12 13:44:25 -0600611 if (flow_type & IRQF_TRIGGER_HIGH)
Kiran Gunda325255b2017-07-28 12:40:39 +0530612 type.polarity_high |= BIT(irq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600613 else
Kiran Gunda325255b2017-07-28 12:40:39 +0530614 type.polarity_low |= BIT(irq);
615
616 flow_handler = handle_level_irq;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600617 }
618
619 qpnpint_spmi_write(d, QPNPINT_REG_SET_TYPE, &type, sizeof(type));
Kiran Gunda325255b2017-07-28 12:40:39 +0530620 irq_set_handler_locked(d, flow_handler);
Abhijeet Dharmapurikar5f9b2ea2017-05-10 19:55:38 +0530621
Josh Cartwright67b563f2014-02-12 13:44:25 -0600622 return 0;
623}
624
Courtney Cavin60be4232015-07-30 10:53:54 -0700625static int qpnpint_get_irqchip_state(struct irq_data *d,
626 enum irqchip_irq_state which,
627 bool *state)
628{
Kiran Gunda02abec32017-07-28 12:40:37 +0530629 u8 irq = hwirq_to_irq(d->hwirq);
Courtney Cavin60be4232015-07-30 10:53:54 -0700630 u8 status = 0;
631
632 if (which != IRQCHIP_STATE_LINE_LEVEL)
633 return -EINVAL;
634
635 qpnpint_spmi_read(d, QPNPINT_REG_RT_STS, &status, 1);
636 *state = !!(status & BIT(irq));
637
638 return 0;
639}
640
Josh Cartwright67b563f2014-02-12 13:44:25 -0600641static struct irq_chip pmic_arb_irqchip = {
642 .name = "pmic_arb",
Josh Cartwright67b563f2014-02-12 13:44:25 -0600643 .irq_ack = qpnpint_irq_ack,
644 .irq_mask = qpnpint_irq_mask,
645 .irq_unmask = qpnpint_irq_unmask,
646 .irq_set_type = qpnpint_irq_set_type,
Courtney Cavin60be4232015-07-30 10:53:54 -0700647 .irq_get_irqchip_state = qpnpint_get_irqchip_state,
Josh Cartwright67b563f2014-02-12 13:44:25 -0600648 .flags = IRQCHIP_MASK_ON_SUSPEND
649 | IRQCHIP_SKIP_SET_WAKE,
650};
651
Josh Cartwright67b563f2014-02-12 13:44:25 -0600652static int qpnpint_irq_domain_dt_translate(struct irq_domain *d,
653 struct device_node *controller,
654 const u32 *intspec,
655 unsigned int intsize,
656 unsigned long *out_hwirq,
657 unsigned int *out_type)
658{
Kiran Gunda02abec32017-07-28 12:40:37 +0530659 struct spmi_pmic_arb *pmic_arb = d->host_data;
Kiran Gundaff615ed2017-07-28 12:40:42 +0530660 u16 apid, ppid;
Abhijeet Dharmapurikar7f1d4e52017-05-10 19:55:34 +0530661 int rc;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600662
Kiran Gunda02abec32017-07-28 12:40:37 +0530663 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 -0600664 intspec[0], intspec[1], intspec[2]);
665
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100666 if (irq_domain_get_of_node(d) != controller)
Josh Cartwright67b563f2014-02-12 13:44:25 -0600667 return -EINVAL;
668 if (intsize != 4)
669 return -EINVAL;
670 if (intspec[0] > 0xF || intspec[1] > 0xFF || intspec[2] > 0x7)
671 return -EINVAL;
672
Kiran Gundaff615ed2017-07-28 12:40:42 +0530673 ppid = intspec[0] << 8 | intspec[1];
674 rc = pmic_arb->ver_ops->ppid_to_apid(pmic_arb, ppid);
Abhijeet Dharmapurikar7f1d4e52017-05-10 19:55:34 +0530675 if (rc < 0) {
Kiran Gunda02abec32017-07-28 12:40:37 +0530676 dev_err(&pmic_arb->spmic->dev, "failed to xlate sid = 0x%x, periph = 0x%x, irq = %x rc = %d\n",
Abhijeet Dharmapurikar7f1d4e52017-05-10 19:55:34 +0530677 intspec[0], intspec[1], intspec[2], rc);
678 return rc;
679 }
Josh Cartwright67b563f2014-02-12 13:44:25 -0600680
Kiran Gundaff615ed2017-07-28 12:40:42 +0530681 apid = rc;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600682 /* Keep track of {max,min}_apid for bounding search during interrupt */
Kiran Gunda02abec32017-07-28 12:40:37 +0530683 if (apid > pmic_arb->max_apid)
684 pmic_arb->max_apid = apid;
685 if (apid < pmic_arb->min_apid)
686 pmic_arb->min_apid = apid;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600687
Kiran Gunda02abec32017-07-28 12:40:37 +0530688 *out_hwirq = spec_to_hwirq(intspec[0], intspec[1], intspec[2], apid);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600689 *out_type = intspec[3] & IRQ_TYPE_SENSE_MASK;
690
Kiran Gunda02abec32017-07-28 12:40:37 +0530691 dev_dbg(&pmic_arb->spmic->dev, "out_hwirq = %lu\n", *out_hwirq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600692
693 return 0;
694}
695
696static int qpnpint_irq_domain_map(struct irq_domain *d,
697 unsigned int virq,
698 irq_hw_number_t hwirq)
699{
Kiran Gunda02abec32017-07-28 12:40:37 +0530700 struct spmi_pmic_arb *pmic_arb = d->host_data;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600701
Kiran Gunda02abec32017-07-28 12:40:37 +0530702 dev_dbg(&pmic_arb->spmic->dev, "virq = %u, hwirq = %lu\n", virq, hwirq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600703
704 irq_set_chip_and_handler(virq, &pmic_arb_irqchip, handle_level_irq);
705 irq_set_chip_data(virq, d->host_data);
706 irq_set_noprobe(virq);
707 return 0;
708}
709
Kiran Gundaff615ed2017-07-28 12:40:42 +0530710static int pmic_arb_ppid_to_apid_v1(struct spmi_pmic_arb *pmic_arb, u16 ppid)
Abhijeet Dharmapurikar7f1d4e52017-05-10 19:55:34 +0530711{
Kiran Gunda02abec32017-07-28 12:40:37 +0530712 u32 *mapping_table = pmic_arb->mapping_table;
Abhijeet Dharmapurikar7f1d4e52017-05-10 19:55:34 +0530713 int index = 0, i;
714 u16 apid_valid;
Kiran Gundaff615ed2017-07-28 12:40:42 +0530715 u16 apid;
Abhijeet Dharmapurikar7f1d4e52017-05-10 19:55:34 +0530716 u32 data;
717
Kiran Gunda02abec32017-07-28 12:40:37 +0530718 apid_valid = pmic_arb->ppid_to_apid[ppid];
719 if (apid_valid & PMIC_ARB_APID_VALID) {
Kiran Gundaff615ed2017-07-28 12:40:42 +0530720 apid = apid_valid & ~PMIC_ARB_APID_VALID;
721 return apid;
Abhijeet Dharmapurikar7f1d4e52017-05-10 19:55:34 +0530722 }
723
724 for (i = 0; i < SPMI_MAPPING_TABLE_TREE_DEPTH; ++i) {
Kiran Gunda02abec32017-07-28 12:40:37 +0530725 if (!test_and_set_bit(index, pmic_arb->mapping_table_valid))
726 mapping_table[index] = readl_relaxed(pmic_arb->cnfg +
Abhijeet Dharmapurikar7f1d4e52017-05-10 19:55:34 +0530727 SPMI_MAPPING_TABLE_REG(index));
728
729 data = mapping_table[index];
730
731 if (ppid & BIT(SPMI_MAPPING_BIT_INDEX(data))) {
732 if (SPMI_MAPPING_BIT_IS_1_FLAG(data)) {
733 index = SPMI_MAPPING_BIT_IS_1_RESULT(data);
734 } else {
Kiran Gundaff615ed2017-07-28 12:40:42 +0530735 apid = SPMI_MAPPING_BIT_IS_1_RESULT(data);
Kiran Gunda02abec32017-07-28 12:40:37 +0530736 pmic_arb->ppid_to_apid[ppid]
Kiran Gundaff615ed2017-07-28 12:40:42 +0530737 = apid | PMIC_ARB_APID_VALID;
738 pmic_arb->apid_data[apid].ppid = ppid;
739 return apid;
Abhijeet Dharmapurikar7f1d4e52017-05-10 19:55:34 +0530740 }
741 } else {
742 if (SPMI_MAPPING_BIT_IS_0_FLAG(data)) {
743 index = SPMI_MAPPING_BIT_IS_0_RESULT(data);
744 } else {
Kiran Gundaff615ed2017-07-28 12:40:42 +0530745 apid = SPMI_MAPPING_BIT_IS_0_RESULT(data);
Kiran Gunda02abec32017-07-28 12:40:37 +0530746 pmic_arb->ppid_to_apid[ppid]
Kiran Gundaff615ed2017-07-28 12:40:42 +0530747 = apid | PMIC_ARB_APID_VALID;
748 pmic_arb->apid_data[apid].ppid = ppid;
749 return apid;
Abhijeet Dharmapurikar7f1d4e52017-05-10 19:55:34 +0530750 }
751 }
752 }
753
754 return -ENODEV;
755}
756
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600757/* v1 offset per ee */
Kiran Gundaff615ed2017-07-28 12:40:42 +0530758static int pmic_arb_offset_v1(struct spmi_pmic_arb *pmic_arb, u8 sid, u16 addr)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600759{
Kiran Gundaff615ed2017-07-28 12:40:42 +0530760 return 0x800 + 0x80 * pmic_arb->channel;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600761}
762
Kiran Gunda02abec32017-07-28 12:40:37 +0530763static u16 pmic_arb_find_apid(struct spmi_pmic_arb *pmic_arb, u16 ppid)
Stephen Boyd987a9f12015-11-17 16:13:55 -0800764{
Kiran Gundaf2f315642017-07-28 12:40:38 +0530765 struct apid_data *apidd = &pmic_arb->apid_data[pmic_arb->last_apid];
Stephen Boyd987a9f12015-11-17 16:13:55 -0800766 u32 regval, offset;
Kiran Gundaf2f315642017-07-28 12:40:38 +0530767 u16 id, apid;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800768
769 /*
Kiran Gundaf2f315642017-07-28 12:40:38 +0530770 * PMIC_ARB_REG_APID is a table in HW mapping apid to ppid.
Abhijeet Dharmapurikar1ef1ce42017-05-10 19:55:33 +0530771 * ppid_to_apid is an in-memory invert of that table.
Stephen Boyd987a9f12015-11-17 16:13:55 -0800772 */
Kiran Gundaf2f315642017-07-28 12:40:38 +0530773 for (apid = pmic_arb->last_apid; ; apid++, apidd++) {
Kiran Gunda02abec32017-07-28 12:40:37 +0530774 offset = PMIC_ARB_REG_APID(apid);
775 if (offset >= pmic_arb->core_size)
Stephen Boyd987a9f12015-11-17 16:13:55 -0800776 break;
777
Kiran Gunda02abec32017-07-28 12:40:37 +0530778 regval = readl_relaxed(pmic_arb->cnfg +
Kiran Gundab319b592017-07-28 12:40:36 +0530779 SPMI_OWNERSHIP_TABLE_REG(apid));
Kiran Gundaf2f315642017-07-28 12:40:38 +0530780 apidd->owner = SPMI_OWNERSHIP_PERIPH2OWNER(regval);
Kiran Gundab319b592017-07-28 12:40:36 +0530781
Kiran Gunda02abec32017-07-28 12:40:37 +0530782 regval = readl_relaxed(pmic_arb->core + offset);
Stephen Boyd987a9f12015-11-17 16:13:55 -0800783 if (!regval)
784 continue;
785
786 id = (regval >> 8) & PMIC_ARB_PPID_MASK;
Kiran Gunda02abec32017-07-28 12:40:37 +0530787 pmic_arb->ppid_to_apid[id] = apid | PMIC_ARB_APID_VALID;
Kiran Gundaf2f315642017-07-28 12:40:38 +0530788 apidd->ppid = id;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800789 if (id == ppid) {
Kiran Gunda02abec32017-07-28 12:40:37 +0530790 apid |= PMIC_ARB_APID_VALID;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800791 break;
792 }
793 }
Kiran Gunda02abec32017-07-28 12:40:37 +0530794 pmic_arb->last_apid = apid & ~PMIC_ARB_APID_VALID;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800795
Abhijeet Dharmapurikar1ef1ce42017-05-10 19:55:33 +0530796 return apid;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800797}
798
Kiran Gundaff615ed2017-07-28 12:40:42 +0530799static int pmic_arb_ppid_to_apid_v2(struct spmi_pmic_arb *pmic_arb, u16 ppid)
Abhijeet Dharmapurikar57102ad2017-05-10 19:55:31 +0530800{
Abhijeet Dharmapurikar7f1d4e52017-05-10 19:55:34 +0530801 u16 apid_valid;
Abhijeet Dharmapurikar57102ad2017-05-10 19:55:31 +0530802
Kiran Gunda02abec32017-07-28 12:40:37 +0530803 apid_valid = pmic_arb->ppid_to_apid[ppid];
804 if (!(apid_valid & PMIC_ARB_APID_VALID))
805 apid_valid = pmic_arb_find_apid(pmic_arb, ppid);
806 if (!(apid_valid & PMIC_ARB_APID_VALID))
Abhijeet Dharmapurikar57102ad2017-05-10 19:55:31 +0530807 return -ENODEV;
808
Kiran Gundaff615ed2017-07-28 12:40:42 +0530809 return apid_valid & ~PMIC_ARB_APID_VALID;
Abhijeet Dharmapurikar7f1d4e52017-05-10 19:55:34 +0530810}
811
Abhijeet Dharmapurikar1ef1ce42017-05-10 19:55:33 +0530812/* v2 offset per ppid and per ee */
Kiran Gundaff615ed2017-07-28 12:40:42 +0530813static int pmic_arb_offset_v2(struct spmi_pmic_arb *pmic_arb, u8 sid, u16 addr)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600814{
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +0530815 u16 apid;
Kiran Gundaff615ed2017-07-28 12:40:42 +0530816 u16 ppid;
Abhijeet Dharmapurikar7f1d4e52017-05-10 19:55:34 +0530817 int rc;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600818
Kiran Gundaff615ed2017-07-28 12:40:42 +0530819 ppid = sid << 8 | ((addr >> 8) & 0xFF);
820 rc = pmic_arb_ppid_to_apid_v2(pmic_arb, ppid);
Abhijeet Dharmapurikar7f1d4e52017-05-10 19:55:34 +0530821 if (rc < 0)
822 return rc;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800823
Kiran Gundaff615ed2017-07-28 12:40:42 +0530824 apid = rc;
825 return 0x1000 * pmic_arb->ee + 0x8000 * apid;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600826}
827
828static u32 pmic_arb_fmt_cmd_v1(u8 opc, u8 sid, u16 addr, u8 bc)
829{
830 return (opc << 27) | ((sid & 0xf) << 20) | (addr << 4) | (bc & 0x7);
831}
832
833static u32 pmic_arb_fmt_cmd_v2(u8 opc, u8 sid, u16 addr, u8 bc)
834{
835 return (opc << 27) | ((addr & 0xff) << 4) | (bc & 0x7);
836}
837
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +0530838static u32 pmic_arb_owner_acc_status_v1(u8 m, u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600839{
840 return 0x20 * m + 0x4 * n;
841}
842
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +0530843static u32 pmic_arb_owner_acc_status_v2(u8 m, u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600844{
845 return 0x100000 + 0x1000 * m + 0x4 * n;
846}
847
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +0530848static u32 pmic_arb_owner_acc_status_v3(u8 m, u16 n)
849{
850 return 0x200000 + 0x1000 * m + 0x4 * n;
851}
852
853static u32 pmic_arb_acc_enable_v1(u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600854{
855 return 0x200 + 0x4 * n;
856}
857
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +0530858static u32 pmic_arb_acc_enable_v2(u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600859{
860 return 0x1000 * n;
861}
862
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +0530863static u32 pmic_arb_irq_status_v1(u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600864{
865 return 0x600 + 0x4 * n;
866}
867
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +0530868static u32 pmic_arb_irq_status_v2(u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600869{
870 return 0x4 + 0x1000 * n;
871}
872
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +0530873static u32 pmic_arb_irq_clear_v1(u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600874{
875 return 0xA00 + 0x4 * n;
876}
877
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +0530878static u32 pmic_arb_irq_clear_v2(u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600879{
880 return 0x8 + 0x1000 * n;
881}
882
883static const struct pmic_arb_ver_ops pmic_arb_v1 = {
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +0530884 .ver_str = "v1",
Abhijeet Dharmapurikar7f1d4e52017-05-10 19:55:34 +0530885 .ppid_to_apid = pmic_arb_ppid_to_apid_v1,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600886 .non_data_cmd = pmic_arb_non_data_cmd_v1,
887 .offset = pmic_arb_offset_v1,
888 .fmt_cmd = pmic_arb_fmt_cmd_v1,
889 .owner_acc_status = pmic_arb_owner_acc_status_v1,
890 .acc_enable = pmic_arb_acc_enable_v1,
891 .irq_status = pmic_arb_irq_status_v1,
892 .irq_clear = pmic_arb_irq_clear_v1,
893};
894
895static const struct pmic_arb_ver_ops pmic_arb_v2 = {
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +0530896 .ver_str = "v2",
Abhijeet Dharmapurikar7f1d4e52017-05-10 19:55:34 +0530897 .ppid_to_apid = pmic_arb_ppid_to_apid_v2,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600898 .non_data_cmd = pmic_arb_non_data_cmd_v2,
899 .offset = pmic_arb_offset_v2,
900 .fmt_cmd = pmic_arb_fmt_cmd_v2,
901 .owner_acc_status = pmic_arb_owner_acc_status_v2,
902 .acc_enable = pmic_arb_acc_enable_v2,
903 .irq_status = pmic_arb_irq_status_v2,
904 .irq_clear = pmic_arb_irq_clear_v2,
905};
906
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +0530907static const struct pmic_arb_ver_ops pmic_arb_v3 = {
908 .ver_str = "v3",
909 .ppid_to_apid = pmic_arb_ppid_to_apid_v2,
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +0530910 .non_data_cmd = pmic_arb_non_data_cmd_v2,
911 .offset = pmic_arb_offset_v2,
912 .fmt_cmd = pmic_arb_fmt_cmd_v2,
913 .owner_acc_status = pmic_arb_owner_acc_status_v3,
914 .acc_enable = pmic_arb_acc_enable_v2,
915 .irq_status = pmic_arb_irq_status_v2,
916 .irq_clear = pmic_arb_irq_clear_v2,
917};
918
Josh Cartwright67b563f2014-02-12 13:44:25 -0600919static const struct irq_domain_ops pmic_arb_irq_domain_ops = {
920 .map = qpnpint_irq_domain_map,
921 .xlate = qpnpint_irq_domain_dt_translate,
922};
923
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600924static int spmi_pmic_arb_probe(struct platform_device *pdev)
925{
Kiran Gunda02abec32017-07-28 12:40:37 +0530926 struct spmi_pmic_arb *pmic_arb;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600927 struct spmi_controller *ctrl;
928 struct resource *res;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600929 void __iomem *core;
Kiran Gunda4788e612017-07-28 12:40:40 +0530930 u32 *mapping_table;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600931 u32 channel, ee, hw_ver;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800932 int err;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600933
Kiran Gunda02abec32017-07-28 12:40:37 +0530934 ctrl = spmi_controller_alloc(&pdev->dev, sizeof(*pmic_arb));
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600935 if (!ctrl)
936 return -ENOMEM;
937
Kiran Gunda02abec32017-07-28 12:40:37 +0530938 pmic_arb = spmi_controller_get_drvdata(ctrl);
939 pmic_arb->spmic = ctrl;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600940
941 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "core");
Kiran Gunda02abec32017-07-28 12:40:37 +0530942 pmic_arb->core_size = resource_size(res);
Abhijeet Dharmapurikar57102ad2017-05-10 19:55:31 +0530943
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600944 core = devm_ioremap_resource(&ctrl->dev, res);
945 if (IS_ERR(core)) {
946 err = PTR_ERR(core);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600947 goto err_put_ctrl;
948 }
949
Kiran Gunda02abec32017-07-28 12:40:37 +0530950 pmic_arb->ppid_to_apid = devm_kcalloc(&ctrl->dev, PMIC_ARB_MAX_PPID,
951 sizeof(*pmic_arb->ppid_to_apid),
952 GFP_KERNEL);
953 if (!pmic_arb->ppid_to_apid) {
Stephen Boydeba97182017-06-26 19:17:46 -0700954 err = -ENOMEM;
955 goto err_put_ctrl;
956 }
957
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600958 hw_ver = readl_relaxed(core + PMIC_ARB_VERSION);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600959
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +0530960 if (hw_ver < PMIC_ARB_VERSION_V2_MIN) {
Kiran Gunda02abec32017-07-28 12:40:37 +0530961 pmic_arb->ver_ops = &pmic_arb_v1;
962 pmic_arb->wr_base = core;
963 pmic_arb->rd_base = core;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600964 } else {
Kiran Gunda02abec32017-07-28 12:40:37 +0530965 pmic_arb->core = core;
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +0530966
967 if (hw_ver < PMIC_ARB_VERSION_V3_MIN)
Kiran Gunda02abec32017-07-28 12:40:37 +0530968 pmic_arb->ver_ops = &pmic_arb_v2;
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +0530969 else
Kiran Gunda02abec32017-07-28 12:40:37 +0530970 pmic_arb->ver_ops = &pmic_arb_v3;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600971
972 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
973 "obsrvr");
Kiran Gunda02abec32017-07-28 12:40:37 +0530974 pmic_arb->rd_base = devm_ioremap_resource(&ctrl->dev, res);
975 if (IS_ERR(pmic_arb->rd_base)) {
976 err = PTR_ERR(pmic_arb->rd_base);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600977 goto err_put_ctrl;
978 }
979
980 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
981 "chnls");
Kiran Gunda02abec32017-07-28 12:40:37 +0530982 pmic_arb->wr_base = devm_ioremap_resource(&ctrl->dev, res);
983 if (IS_ERR(pmic_arb->wr_base)) {
984 err = PTR_ERR(pmic_arb->wr_base);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600985 goto err_put_ctrl;
986 }
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600987 }
988
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +0530989 dev_info(&ctrl->dev, "PMIC arbiter version %s (0x%x)\n",
Kiran Gunda02abec32017-07-28 12:40:37 +0530990 pmic_arb->ver_ops->ver_str, hw_ver);
Abhijeet Dharmapurikar319f6882017-05-10 19:55:40 +0530991
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600992 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "intr");
Kiran Gunda02abec32017-07-28 12:40:37 +0530993 pmic_arb->intr = devm_ioremap_resource(&ctrl->dev, res);
994 if (IS_ERR(pmic_arb->intr)) {
995 err = PTR_ERR(pmic_arb->intr);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600996 goto err_put_ctrl;
997 }
998
999 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cnfg");
Kiran Gunda02abec32017-07-28 12:40:37 +05301000 pmic_arb->cnfg = devm_ioremap_resource(&ctrl->dev, res);
1001 if (IS_ERR(pmic_arb->cnfg)) {
1002 err = PTR_ERR(pmic_arb->cnfg);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001003 goto err_put_ctrl;
1004 }
1005
Kiran Gunda02abec32017-07-28 12:40:37 +05301006 pmic_arb->irq = platform_get_irq_byname(pdev, "periph_irq");
1007 if (pmic_arb->irq < 0) {
1008 err = pmic_arb->irq;
Josh Cartwright67b563f2014-02-12 13:44:25 -06001009 goto err_put_ctrl;
1010 }
1011
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001012 err = of_property_read_u32(pdev->dev.of_node, "qcom,channel", &channel);
1013 if (err) {
1014 dev_err(&pdev->dev, "channel unspecified.\n");
1015 goto err_put_ctrl;
1016 }
1017
1018 if (channel > 5) {
1019 dev_err(&pdev->dev, "invalid channel (%u) specified.\n",
1020 channel);
Christophe JAILLETe98cc182016-09-26 22:24:46 +02001021 err = -EINVAL;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001022 goto err_put_ctrl;
1023 }
1024
Kiran Gunda02abec32017-07-28 12:40:37 +05301025 pmic_arb->channel = channel;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001026
Josh Cartwright67b563f2014-02-12 13:44:25 -06001027 err = of_property_read_u32(pdev->dev.of_node, "qcom,ee", &ee);
1028 if (err) {
1029 dev_err(&pdev->dev, "EE unspecified.\n");
1030 goto err_put_ctrl;
1031 }
1032
1033 if (ee > 5) {
1034 dev_err(&pdev->dev, "invalid EE (%u) specified\n", ee);
1035 err = -EINVAL;
1036 goto err_put_ctrl;
1037 }
1038
Kiran Gunda02abec32017-07-28 12:40:37 +05301039 pmic_arb->ee = ee;
Kiran Gunda4788e612017-07-28 12:40:40 +05301040 mapping_table = devm_kcalloc(&ctrl->dev, PMIC_ARB_MAX_PERIPHS,
1041 sizeof(*mapping_table), GFP_KERNEL);
1042 if (!mapping_table) {
Stephen Boyd987a9f12015-11-17 16:13:55 -08001043 err = -ENOMEM;
1044 goto err_put_ctrl;
1045 }
Josh Cartwright67b563f2014-02-12 13:44:25 -06001046
Kiran Gunda4788e612017-07-28 12:40:40 +05301047 pmic_arb->mapping_table = mapping_table;
Josh Cartwright67b563f2014-02-12 13:44:25 -06001048 /* Initialize max_apid/min_apid to the opposite bounds, during
1049 * the irq domain translation, we are sure to update these */
Kiran Gunda02abec32017-07-28 12:40:37 +05301050 pmic_arb->max_apid = 0;
1051 pmic_arb->min_apid = PMIC_ARB_MAX_PERIPHS - 1;
Josh Cartwright67b563f2014-02-12 13:44:25 -06001052
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001053 platform_set_drvdata(pdev, ctrl);
Kiran Gunda02abec32017-07-28 12:40:37 +05301054 raw_spin_lock_init(&pmic_arb->lock);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001055
1056 ctrl->cmd = pmic_arb_cmd;
1057 ctrl->read_cmd = pmic_arb_read_cmd;
1058 ctrl->write_cmd = pmic_arb_write_cmd;
1059
Josh Cartwright67b563f2014-02-12 13:44:25 -06001060 dev_dbg(&pdev->dev, "adding irq domain\n");
Kiran Gunda02abec32017-07-28 12:40:37 +05301061 pmic_arb->domain = irq_domain_add_tree(pdev->dev.of_node,
1062 &pmic_arb_irq_domain_ops, pmic_arb);
1063 if (!pmic_arb->domain) {
Josh Cartwright67b563f2014-02-12 13:44:25 -06001064 dev_err(&pdev->dev, "unable to create irq_domain\n");
1065 err = -ENOMEM;
1066 goto err_put_ctrl;
1067 }
1068
Kiran Gunda02abec32017-07-28 12:40:37 +05301069 irq_set_chained_handler_and_data(pmic_arb->irq, pmic_arb_chained_irq,
1070 pmic_arb);
1071 enable_irq_wake(pmic_arb->irq);
Josh Cartwright67b563f2014-02-12 13:44:25 -06001072
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001073 err = spmi_controller_add(ctrl);
1074 if (err)
Josh Cartwright67b563f2014-02-12 13:44:25 -06001075 goto err_domain_remove;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001076
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001077 return 0;
1078
Josh Cartwright67b563f2014-02-12 13:44:25 -06001079err_domain_remove:
Kiran Gunda02abec32017-07-28 12:40:37 +05301080 irq_set_chained_handler_and_data(pmic_arb->irq, NULL, NULL);
1081 irq_domain_remove(pmic_arb->domain);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001082err_put_ctrl:
1083 spmi_controller_put(ctrl);
1084 return err;
1085}
1086
1087static int spmi_pmic_arb_remove(struct platform_device *pdev)
1088{
1089 struct spmi_controller *ctrl = platform_get_drvdata(pdev);
Kiran Gunda02abec32017-07-28 12:40:37 +05301090 struct spmi_pmic_arb *pmic_arb = spmi_controller_get_drvdata(ctrl);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001091 spmi_controller_remove(ctrl);
Kiran Gunda02abec32017-07-28 12:40:37 +05301092 irq_set_chained_handler_and_data(pmic_arb->irq, NULL, NULL);
1093 irq_domain_remove(pmic_arb->domain);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001094 spmi_controller_put(ctrl);
1095 return 0;
1096}
1097
1098static const struct of_device_id spmi_pmic_arb_match_table[] = {
1099 { .compatible = "qcom,spmi-pmic-arb", },
1100 {},
1101};
1102MODULE_DEVICE_TABLE(of, spmi_pmic_arb_match_table);
1103
1104static struct platform_driver spmi_pmic_arb_driver = {
1105 .probe = spmi_pmic_arb_probe,
1106 .remove = spmi_pmic_arb_remove,
1107 .driver = {
1108 .name = "spmi_pmic_arb",
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001109 .of_match_table = spmi_pmic_arb_match_table,
1110 },
1111};
1112module_platform_driver(spmi_pmic_arb_driver);
1113
1114MODULE_LICENSE("GPL v2");
1115MODULE_ALIAS("platform:spmi_pmic_arb");