blob: a5bfeab596ac61d23fa4854057dc0a827808b4b3 [file] [log] [blame]
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001/*
David Collinsb2d9a402016-07-21 14:42:47 -07002 * Copyright (c) 2012-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>
Abhijeet Dharmapurikar69dc3fc2016-11-07 15:51:24 -080027#include <linux/syscore_ops.h>
Kenneth Heitke39ae93e2014-02-12 13:44:24 -060028
29/* PMIC Arbiter configuration registers */
30#define PMIC_ARB_VERSION 0x0000
Gilad Avidovd0c6ae42015-03-25 11:37:32 -060031#define PMIC_ARB_VERSION_V2_MIN 0x20010000
Nicholas Troast9c10f8f2016-03-28 10:16:31 -070032#define PMIC_ARB_VERSION_V3_MIN 0x30000000
David Collinsb2d9a402016-07-21 14:42:47 -070033#define PMIC_ARB_VERSION_V5_MIN 0x50000000
Kenneth Heitke39ae93e2014-02-12 13:44:24 -060034#define PMIC_ARB_INT_EN 0x0004
35
Gilad Avidovd0c6ae42015-03-25 11:37:32 -060036/* PMIC Arbiter channel registers offsets */
37#define PMIC_ARB_CMD 0x00
38#define PMIC_ARB_CONFIG 0x04
39#define PMIC_ARB_STATUS 0x08
40#define PMIC_ARB_WDATA0 0x10
41#define PMIC_ARB_WDATA1 0x14
42#define PMIC_ARB_RDATA0 0x18
43#define PMIC_ARB_RDATA1 0x1C
Kenneth Heitke39ae93e2014-02-12 13:44:24 -060044
45/* Mapping Table */
46#define SPMI_MAPPING_TABLE_REG(N) (0x0B00 + (4 * (N)))
47#define SPMI_MAPPING_BIT_INDEX(X) (((X) >> 18) & 0xF)
48#define SPMI_MAPPING_BIT_IS_0_FLAG(X) (((X) >> 17) & 0x1)
49#define SPMI_MAPPING_BIT_IS_0_RESULT(X) (((X) >> 9) & 0xFF)
50#define SPMI_MAPPING_BIT_IS_1_FLAG(X) (((X) >> 8) & 0x1)
51#define SPMI_MAPPING_BIT_IS_1_RESULT(X) (((X) >> 0) & 0xFF)
52
Kenneth Heitke39ae93e2014-02-12 13:44:24 -060053#define SPMI_MAPPING_TABLE_TREE_DEPTH 16 /* Maximum of 16-bits */
Stephen Boyd987a9f12015-11-17 16:13:55 -080054#define PMIC_ARB_MAX_PPID BIT(12) /* PPID is 12bit */
55#define PMIC_ARB_CHAN_VALID BIT(15)
David Collinsb2d9a402016-07-21 14:42:47 -070056#define PMIC_ARB_CHAN_IS_IRQ_OWNER(reg) ((reg) & BIT(24))
57#define INVALID_EE (-1)
Kenneth Heitke39ae93e2014-02-12 13:44:24 -060058
59/* Ownership Table */
60#define SPMI_OWNERSHIP_TABLE_REG(N) (0x0700 + (4 * (N)))
61#define SPMI_OWNERSHIP_PERIPH2OWNER(X) ((X) & 0x7)
62
63/* Channel Status fields */
64enum pmic_arb_chnl_status {
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -080065 PMIC_ARB_STATUS_DONE = BIT(0),
66 PMIC_ARB_STATUS_FAILURE = BIT(1),
67 PMIC_ARB_STATUS_DENIED = BIT(2),
68 PMIC_ARB_STATUS_DROPPED = BIT(3),
Kenneth Heitke39ae93e2014-02-12 13:44:24 -060069};
70
71/* Command register fields */
72#define PMIC_ARB_CMD_MAX_BYTE_COUNT 8
73
74/* Command Opcodes */
75enum pmic_arb_cmd_op_code {
76 PMIC_ARB_OP_EXT_WRITEL = 0,
77 PMIC_ARB_OP_EXT_READL = 1,
78 PMIC_ARB_OP_EXT_WRITE = 2,
79 PMIC_ARB_OP_RESET = 3,
80 PMIC_ARB_OP_SLEEP = 4,
81 PMIC_ARB_OP_SHUTDOWN = 5,
82 PMIC_ARB_OP_WAKEUP = 6,
83 PMIC_ARB_OP_AUTHENTICATE = 7,
84 PMIC_ARB_OP_MSTR_READ = 8,
85 PMIC_ARB_OP_MSTR_WRITE = 9,
86 PMIC_ARB_OP_EXT_READ = 13,
87 PMIC_ARB_OP_WRITE = 14,
88 PMIC_ARB_OP_READ = 15,
89 PMIC_ARB_OP_ZERO_WRITE = 16,
90};
91
David Collinsb2d9a402016-07-21 14:42:47 -070092/*
93 * PMIC arbiter version 5 uses different register offsets for read/write vs
94 * observer channels.
95 */
96enum pmic_arb_channel {
97 PMIC_ARB_CHANNEL_RW,
98 PMIC_ARB_CHANNEL_OBS,
99};
100
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600101/* Maximum number of support PMIC peripherals */
Stephen Boyd987a9f12015-11-17 16:13:55 -0800102#define PMIC_ARB_MAX_PERIPHS 512
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600103#define PMIC_ARB_TIMEOUT_US 100
104#define PMIC_ARB_MAX_TRANS_BYTES (8)
105
106#define PMIC_ARB_APID_MASK 0xFF
107#define PMIC_ARB_PPID_MASK 0xFFF
108
109/* interrupt enable bit */
110#define SPMI_PIC_ACC_ENABLE_BIT BIT(0)
111
David Collins370a4fa2016-07-21 16:58:29 -0700112#define HWIRQ(slave_id, periph_id, irq_id, apid) \
113 ((((slave_id) & 0xF) << 28) | \
114 (((periph_id) & 0xFF) << 20) | \
115 (((irq_id) & 0x7) << 16) | \
116 (((apid) & 0x1FF) << 0))
117
118#define HWIRQ_SID(hwirq) (((hwirq) >> 28) & 0xF)
119#define HWIRQ_PER(hwirq) (((hwirq) >> 20) & 0xFF)
120#define HWIRQ_IRQ(hwirq) (((hwirq) >> 16) & 0x7)
121#define HWIRQ_APID(hwirq) (((hwirq) >> 0) & 0x1FF)
122
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600123struct pmic_arb_ver_ops;
124
Abhijeet Dharmapurikar8f8ec812016-01-08 12:54:36 -0800125struct apid_data {
126 u16 ppid;
David Collinsb2d9a402016-07-21 14:42:47 -0700127 u8 write_owner;
128 u8 irq_owner;
Abhijeet Dharmapurikar8f8ec812016-01-08 12:54:36 -0800129};
130
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600131/**
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800132 * spmi_pmic_arb - SPMI PMIC Arbiter object
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600133 *
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600134 * @rd_base: on v1 "core", on v2 "observer" register base off DT.
135 * @wr_base: on v1 "core", on v2 "chnls" register base off DT.
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600136 * @intr: address of the SPMI interrupt control registers.
David Collinsb2d9a402016-07-21 14:42:47 -0700137 * @acc_status: address of SPMI ACC interrupt status registers.
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600138 * @cnfg: address of the PMIC Arbiter configuration registers.
139 * @lock: lock to synchronize accesses.
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600140 * @channel: execution environment channel to use for accesses.
Josh Cartwright67b563f2014-02-12 13:44:25 -0600141 * @irq: PMIC ARB interrupt.
142 * @ee: the current Execution Environment
143 * @min_apid: minimum APID (used for bounding IRQ search)
144 * @max_apid: maximum APID
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800145 * @max_periph: maximum number of PMIC peripherals supported by HW.
Josh Cartwright67b563f2014-02-12 13:44:25 -0600146 * @mapping_table: in-memory copy of PPID -> APID mapping table.
147 * @domain: irq domain object for PMIC IRQ domain
148 * @spmic: SPMI controller object
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600149 * @ver_ops: version dependent operations.
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800150 * @ppid_to_apid in-memory copy of PPID -> channel (APID) mapping table.
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600151 * v2 only.
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600152 */
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800153struct spmi_pmic_arb {
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600154 void __iomem *rd_base;
155 void __iomem *wr_base;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600156 void __iomem *intr;
David Collinsb2d9a402016-07-21 14:42:47 -0700157 void __iomem *acc_status;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600158 void __iomem *cnfg;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800159 void __iomem *core;
160 resource_size_t core_size;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600161 raw_spinlock_t lock;
162 u8 channel;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600163 int irq;
164 u8 ee;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800165 u16 min_apid;
166 u16 max_apid;
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800167 u16 max_periph;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800168 u32 *mapping_table;
169 DECLARE_BITMAP(mapping_table_valid, PMIC_ARB_MAX_PERIPHS);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600170 struct irq_domain *domain;
171 struct spmi_controller *spmic;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600172 const struct pmic_arb_ver_ops *ver_ops;
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800173 u16 *ppid_to_apid;
174 u16 last_apid;
Abhijeet Dharmapurikar8f8ec812016-01-08 12:54:36 -0800175 struct apid_data apid_data[PMIC_ARB_MAX_PERIPHS];
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600176};
Abhijeet Dharmapurikar69dc3fc2016-11-07 15:51:24 -0800177static struct spmi_pmic_arb *the_pa;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600178
179/**
180 * pmic_arb_ver: version dependent functionality.
181 *
Nicholas Troast9c10f8f2016-03-28 10:16:31 -0700182 * @ver_str: version string.
183 * @ppid_to_apid: finds the apid for a given ppid.
184 * @mode: access rights to specified pmic peripheral.
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600185 * @non_data_cmd: on v1 issues an spmi non-data command.
186 * on v2 no HW support, returns -EOPNOTSUPP.
187 * @offset: on v1 offset of per-ee channel.
188 * on v2 offset of per-ee and per-ppid channel.
189 * @fmt_cmd: formats a GENI/SPMI command.
190 * @owner_acc_status: on v1 offset of PMIC_ARB_SPMI_PIC_OWNERm_ACC_STATUSn
191 * on v2 offset of SPMI_PIC_OWNERm_ACC_STATUSn.
192 * @acc_enable: on v1 offset of PMIC_ARB_SPMI_PIC_ACC_ENABLEn
193 * on v2 offset of SPMI_PIC_ACC_ENABLEn.
194 * @irq_status: on v1 offset of PMIC_ARB_SPMI_PIC_IRQ_STATUSn
195 * on v2 offset of SPMI_PIC_IRQ_STATUSn.
196 * @irq_clear: on v1 offset of PMIC_ARB_SPMI_PIC_IRQ_CLEARn
197 * on v2 offset of SPMI_PIC_IRQ_CLEARn.
David Collinsb2d9a402016-07-21 14:42:47 -0700198 * @channel_map_offset: offset of PMIC_ARB_REG_CHNLn
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600199 */
200struct pmic_arb_ver_ops {
Nicholas Troast9c10f8f2016-03-28 10:16:31 -0700201 const char *ver_str;
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800202 int (*ppid_to_apid)(struct spmi_pmic_arb *pa, u8 sid, u16 addr,
David Collins370a4fa2016-07-21 16:58:29 -0700203 u16 *apid);
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800204 int (*mode)(struct spmi_pmic_arb *dev, u8 sid, u16 addr,
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800205 mode_t *mode);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600206 /* spmi commands (read_cmd, write_cmd, cmd) functionality */
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800207 int (*offset)(struct spmi_pmic_arb *dev, u8 sid, u16 addr,
David Collinsb2d9a402016-07-21 14:42:47 -0700208 enum pmic_arb_channel ch_type, u32 *offset);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600209 u32 (*fmt_cmd)(u8 opc, u8 sid, u16 addr, u8 bc);
210 int (*non_data_cmd)(struct spmi_controller *ctrl, u8 opc, u8 sid);
211 /* Interrupts controller functionality (offset of PIC registers) */
David Collins370a4fa2016-07-21 16:58:29 -0700212 u32 (*owner_acc_status)(u8 m, u16 n);
213 u32 (*acc_enable)(u16 n);
214 u32 (*irq_status)(u16 n);
215 u32 (*irq_clear)(u16 n);
David Collinsb2d9a402016-07-21 14:42:47 -0700216 u32 (*channel_map_offset)(u16 n);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600217};
218
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800219static inline void pmic_arb_base_write(struct spmi_pmic_arb *pa,
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600220 u32 offset, u32 val)
221{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800222 writel_relaxed(val, pa->wr_base + offset);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600223}
224
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800225static inline void pmic_arb_set_rd_cmd(struct spmi_pmic_arb *pa,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600226 u32 offset, u32 val)
227{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800228 writel_relaxed(val, pa->rd_base + offset);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600229}
230
231/**
232 * pa_read_data: reads pmic-arb's register and copy 1..4 bytes to buf
233 * @bc: byte count -1. range: 0..3
234 * @reg: register's address
235 * @buf: output parameter, length must be bc + 1
236 */
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800237static void pa_read_data(struct spmi_pmic_arb *pa, u8 *buf, u32 reg, u8 bc)
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600238{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800239 u32 data = __raw_readl(pa->rd_base + reg);
240
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600241 memcpy(buf, &data, (bc & 3) + 1);
242}
243
244/**
245 * pa_write_data: write 1..4 bytes from buf to pmic-arb's register
246 * @bc: byte-count -1. range: 0..3.
247 * @reg: register's address.
248 * @buf: buffer to write. length must be bc + 1.
249 */
250static void
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800251pa_write_data(struct spmi_pmic_arb *pa, const u8 *buf, u32 reg, u8 bc)
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600252{
253 u32 data = 0;
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800254
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600255 memcpy(&data, buf, (bc & 3) + 1);
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800256 pmic_arb_base_write(pa, reg, data);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600257}
258
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600259static int pmic_arb_wait_for_done(struct spmi_controller *ctrl,
David Collinsb2d9a402016-07-21 14:42:47 -0700260 void __iomem *base, u8 sid, u16 addr,
261 enum pmic_arb_channel ch_type)
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600262{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800263 struct spmi_pmic_arb *pa = spmi_controller_get_drvdata(ctrl);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600264 u32 status = 0;
265 u32 timeout = PMIC_ARB_TIMEOUT_US;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800266 u32 offset;
267 int rc;
268
David Collinsb2d9a402016-07-21 14:42:47 -0700269 rc = pa->ver_ops->offset(pa, sid, addr, ch_type, &offset);
Stephen Boyd987a9f12015-11-17 16:13:55 -0800270 if (rc)
271 return rc;
272
273 offset += PMIC_ARB_STATUS;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600274
275 while (timeout--) {
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600276 status = readl_relaxed(base + offset);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600277
278 if (status & PMIC_ARB_STATUS_DONE) {
279 if (status & PMIC_ARB_STATUS_DENIED) {
280 dev_err(&ctrl->dev,
281 "%s: transaction denied (0x%x)\n",
282 __func__, status);
283 return -EPERM;
284 }
285
286 if (status & PMIC_ARB_STATUS_FAILURE) {
287 dev_err(&ctrl->dev,
288 "%s: transaction failed (0x%x)\n",
289 __func__, status);
290 return -EIO;
291 }
292
293 if (status & PMIC_ARB_STATUS_DROPPED) {
294 dev_err(&ctrl->dev,
295 "%s: transaction dropped (0x%x)\n",
296 __func__, status);
297 return -EIO;
298 }
299
300 return 0;
301 }
302 udelay(1);
303 }
304
305 dev_err(&ctrl->dev,
306 "%s: timeout, status 0x%x\n",
307 __func__, status);
308 return -ETIMEDOUT;
309}
310
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600311static int
312pmic_arb_non_data_cmd_v1(struct spmi_controller *ctrl, u8 opc, u8 sid)
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600313{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800314 struct spmi_pmic_arb *pa = spmi_controller_get_drvdata(ctrl);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600315 unsigned long flags;
316 u32 cmd;
317 int rc;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800318 u32 offset;
319
David Collinsb2d9a402016-07-21 14:42:47 -0700320 rc = pa->ver_ops->offset(pa, sid, 0, PMIC_ARB_CHANNEL_RW, &offset);
Stephen Boyd987a9f12015-11-17 16:13:55 -0800321 if (rc)
322 return rc;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600323
324 cmd = ((opc | 0x40) << 27) | ((sid & 0xf) << 20);
325
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800326 raw_spin_lock_irqsave(&pa->lock, flags);
327 pmic_arb_base_write(pa, offset + PMIC_ARB_CMD, cmd);
David Collinsb2d9a402016-07-21 14:42:47 -0700328 rc = pmic_arb_wait_for_done(ctrl, pa->wr_base, sid, 0,
329 PMIC_ARB_CHANNEL_RW);
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800330 raw_spin_unlock_irqrestore(&pa->lock, flags);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600331
332 return rc;
333}
334
335static int
336pmic_arb_non_data_cmd_v2(struct spmi_controller *ctrl, u8 opc, u8 sid)
337{
338 return -EOPNOTSUPP;
339}
340
341/* Non-data command */
342static int pmic_arb_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid)
343{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800344 struct spmi_pmic_arb *pa = spmi_controller_get_drvdata(ctrl);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600345
346 dev_dbg(&ctrl->dev, "cmd op:0x%x sid:%d\n", opc, sid);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600347
348 /* Check for valid non-data command */
349 if (opc < SPMI_CMD_RESET || opc > SPMI_CMD_WAKEUP)
350 return -EINVAL;
351
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800352 return pa->ver_ops->non_data_cmd(ctrl, opc, sid);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600353}
354
355static int pmic_arb_read_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
356 u16 addr, u8 *buf, size_t len)
357{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800358 struct spmi_pmic_arb *pa = spmi_controller_get_drvdata(ctrl);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600359 unsigned long flags;
360 u8 bc = len - 1;
361 u32 cmd;
362 int rc;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800363 u32 offset;
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800364 mode_t mode;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800365
David Collinsb2d9a402016-07-21 14:42:47 -0700366 rc = pa->ver_ops->offset(pa, sid, addr, PMIC_ARB_CHANNEL_OBS, &offset);
Stephen Boyd987a9f12015-11-17 16:13:55 -0800367 if (rc)
368 return rc;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600369
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800370 rc = pa->ver_ops->mode(pa, sid, addr, &mode);
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800371 if (rc)
372 return rc;
373
374 if (!(mode & 0400)) {
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800375 dev_err(&pa->spmic->dev,
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800376 "error: impermissible read from peripheral sid:%d addr:0x%x\n",
377 sid, addr);
378 return -ENODEV;
379 }
380
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600381 if (bc >= PMIC_ARB_MAX_TRANS_BYTES) {
382 dev_err(&ctrl->dev,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600383 "pmic-arb supports 1..%d bytes per trans, but:%zu requested",
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600384 PMIC_ARB_MAX_TRANS_BYTES, len);
385 return -EINVAL;
386 }
387
388 /* Check the opcode */
389 if (opc >= 0x60 && opc <= 0x7F)
390 opc = PMIC_ARB_OP_READ;
391 else if (opc >= 0x20 && opc <= 0x2F)
392 opc = PMIC_ARB_OP_EXT_READ;
393 else if (opc >= 0x38 && opc <= 0x3F)
394 opc = PMIC_ARB_OP_EXT_READL;
395 else
396 return -EINVAL;
397
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800398 cmd = pa->ver_ops->fmt_cmd(opc, sid, addr, bc);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600399
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800400 raw_spin_lock_irqsave(&pa->lock, flags);
401 pmic_arb_set_rd_cmd(pa, offset + PMIC_ARB_CMD, cmd);
David Collinsb2d9a402016-07-21 14:42:47 -0700402 rc = pmic_arb_wait_for_done(ctrl, pa->rd_base, sid, addr,
403 PMIC_ARB_CHANNEL_OBS);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600404 if (rc)
405 goto done;
406
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800407 pa_read_data(pa, buf, offset + PMIC_ARB_RDATA0,
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600408 min_t(u8, bc, 3));
409
410 if (bc > 3)
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800411 pa_read_data(pa, buf + 4, offset + PMIC_ARB_RDATA1, bc - 4);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600412
413done:
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800414 raw_spin_unlock_irqrestore(&pa->lock, flags);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600415 return rc;
416}
417
418static int pmic_arb_write_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
419 u16 addr, const u8 *buf, size_t len)
420{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800421 struct spmi_pmic_arb *pa = spmi_controller_get_drvdata(ctrl);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600422 unsigned long flags;
423 u8 bc = len - 1;
424 u32 cmd;
425 int rc;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800426 u32 offset;
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800427 mode_t mode;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800428
David Collinsb2d9a402016-07-21 14:42:47 -0700429 rc = pa->ver_ops->offset(pa, sid, addr, PMIC_ARB_CHANNEL_RW, &offset);
Stephen Boyd987a9f12015-11-17 16:13:55 -0800430 if (rc)
431 return rc;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600432
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800433 rc = pa->ver_ops->mode(pa, sid, addr, &mode);
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800434 if (rc)
435 return rc;
436
437 if (!(mode & 0200)) {
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800438 dev_err(&pa->spmic->dev,
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800439 "error: impermissible write to peripheral sid:%d addr:0x%x\n",
440 sid, addr);
441 return -ENODEV;
442 }
443
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600444 if (bc >= PMIC_ARB_MAX_TRANS_BYTES) {
445 dev_err(&ctrl->dev,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600446 "pmic-arb supports 1..%d bytes per trans, but:%zu requested",
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600447 PMIC_ARB_MAX_TRANS_BYTES, len);
448 return -EINVAL;
449 }
450
451 /* Check the opcode */
452 if (opc >= 0x40 && opc <= 0x5F)
453 opc = PMIC_ARB_OP_WRITE;
454 else if (opc >= 0x00 && opc <= 0x0F)
455 opc = PMIC_ARB_OP_EXT_WRITE;
456 else if (opc >= 0x30 && opc <= 0x37)
457 opc = PMIC_ARB_OP_EXT_WRITEL;
Stephen Boyd9b769682015-08-28 12:31:10 -0700458 else if (opc >= 0x80)
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600459 opc = PMIC_ARB_OP_ZERO_WRITE;
460 else
461 return -EINVAL;
462
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800463 cmd = pa->ver_ops->fmt_cmd(opc, sid, addr, bc);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600464
465 /* Write data to FIFOs */
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800466 raw_spin_lock_irqsave(&pa->lock, flags);
467 pa_write_data(pa, buf, offset + PMIC_ARB_WDATA0, min_t(u8, bc, 3));
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600468 if (bc > 3)
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800469 pa_write_data(pa, buf + 4, offset + PMIC_ARB_WDATA1, bc - 4);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600470
471 /* Start the transaction */
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800472 pmic_arb_base_write(pa, offset + PMIC_ARB_CMD, cmd);
David Collinsb2d9a402016-07-21 14:42:47 -0700473 rc = pmic_arb_wait_for_done(ctrl, pa->wr_base, sid, addr,
474 PMIC_ARB_CHANNEL_RW);
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800475 raw_spin_unlock_irqrestore(&pa->lock, flags);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600476
477 return rc;
478}
479
Josh Cartwright67b563f2014-02-12 13:44:25 -0600480enum qpnpint_regs {
481 QPNPINT_REG_RT_STS = 0x10,
482 QPNPINT_REG_SET_TYPE = 0x11,
483 QPNPINT_REG_POLARITY_HIGH = 0x12,
484 QPNPINT_REG_POLARITY_LOW = 0x13,
485 QPNPINT_REG_LATCHED_CLR = 0x14,
486 QPNPINT_REG_EN_SET = 0x15,
487 QPNPINT_REG_EN_CLR = 0x16,
488 QPNPINT_REG_LATCHED_STS = 0x18,
489};
490
491struct spmi_pmic_arb_qpnpint_type {
492 u8 type; /* 1 -> edge */
493 u8 polarity_high;
494 u8 polarity_low;
495} __packed;
496
497/* Simplified accessor functions for irqchip callbacks */
498static void qpnpint_spmi_write(struct irq_data *d, u8 reg, void *buf,
499 size_t len)
500{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800501 struct spmi_pmic_arb *pa = irq_data_get_irq_chip_data(d);
David Collins370a4fa2016-07-21 16:58:29 -0700502 u8 sid = HWIRQ_SID(d->hwirq);
503 u8 per = HWIRQ_PER(d->hwirq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600504
505 if (pmic_arb_write_cmd(pa->spmic, SPMI_CMD_EXT_WRITEL, sid,
506 (per << 8) + reg, buf, len))
507 dev_err_ratelimited(&pa->spmic->dev,
508 "failed irqchip transaction on %x\n",
509 d->irq);
510}
511
512static void qpnpint_spmi_read(struct irq_data *d, u8 reg, void *buf, size_t len)
513{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800514 struct spmi_pmic_arb *pa = irq_data_get_irq_chip_data(d);
David Collins370a4fa2016-07-21 16:58:29 -0700515 u8 sid = HWIRQ_SID(d->hwirq);
516 u8 per = HWIRQ_PER(d->hwirq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600517
518 if (pmic_arb_read_cmd(pa->spmic, SPMI_CMD_EXT_READL, sid,
519 (per << 8) + reg, buf, len))
520 dev_err_ratelimited(&pa->spmic->dev,
521 "failed irqchip transaction on %x\n",
522 d->irq);
523}
524
David Collins370a4fa2016-07-21 16:58:29 -0700525static void cleanup_irq(struct spmi_pmic_arb *pa, u16 apid, int id)
Abhijeet Dharmapurikarb9b87442016-01-08 10:50:19 -0800526{
Abhijeet Dharmapurikarb9b87442016-01-08 10:50:19 -0800527 u16 ppid = pa->apid_data[apid].ppid;
528 u8 sid = ppid >> 8;
529 u8 per = ppid & 0xFF;
Abhijeet Dharmapurikarb9b87442016-01-08 10:50:19 -0800530 u8 irq_mask = BIT(id);
531
Abhijeet Dharmapurikared44ac12016-04-26 18:31:39 -0700532 dev_err_ratelimited(&pa->spmic->dev,
533 "cleanup_irq apid=%d sid=0x%x per=0x%x irq=%d\n",
534 apid, sid, per, id);
Abhijeet Dharmapurikarb9b87442016-01-08 10:50:19 -0800535 writel_relaxed(irq_mask, pa->intr + pa->ver_ops->irq_clear(apid));
Abhijeet Dharmapurikarb9b87442016-01-08 10:50:19 -0800536}
537
Abhijeet Dharmapurikar69dc3fc2016-11-07 15:51:24 -0800538static void periph_interrupt(struct spmi_pmic_arb *pa, u16 apid, bool show)
Josh Cartwright67b563f2014-02-12 13:44:25 -0600539{
540 unsigned int irq;
541 u32 status;
542 int id;
David Collins370a4fa2016-07-21 16:58:29 -0700543 u8 sid = (pa->apid_data[apid].ppid >> 8) & 0xF;
544 u8 per = pa->apid_data[apid].ppid & 0xFF;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600545
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600546 status = readl_relaxed(pa->intr + pa->ver_ops->irq_status(apid));
Josh Cartwright67b563f2014-02-12 13:44:25 -0600547 while (status) {
548 id = ffs(status) - 1;
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800549 status &= ~BIT(id);
David Collins370a4fa2016-07-21 16:58:29 -0700550 irq = irq_find_mapping(pa->domain, HWIRQ(sid, per, id, apid));
Abhijeet Dharmapurikarb9b87442016-01-08 10:50:19 -0800551 if (irq == 0) {
552 cleanup_irq(pa, apid, id);
553 continue;
554 }
Abhijeet Dharmapurikar69dc3fc2016-11-07 15:51:24 -0800555 if (show) {
556 struct irq_desc *desc;
557 const char *name = "null";
558
559 desc = irq_to_desc(irq);
560 if (desc == NULL)
561 name = "stray irq";
562 else if (desc->action && desc->action->name)
563 name = desc->action->name;
564
565 pr_warn("spmi_show_resume_irq: %d triggered [0x%01x, 0x%02x, 0x%01x] %s\n",
566 irq, sid, per, id, name);
567 } else {
568 generic_handle_irq(irq);
569 }
Josh Cartwright67b563f2014-02-12 13:44:25 -0600570 }
571}
572
Abhijeet Dharmapurikar69dc3fc2016-11-07 15:51:24 -0800573static void __pmic_arb_chained_irq(struct spmi_pmic_arb *pa, bool show)
Josh Cartwright67b563f2014-02-12 13:44:25 -0600574{
Josh Cartwright67b563f2014-02-12 13:44:25 -0600575 int first = pa->min_apid >> 5;
576 int last = pa->max_apid >> 5;
Abhijeet Dharmapurikar5e5078b2016-04-27 20:39:46 -0700577 u32 status, enable;
578 int i, id, apid;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600579
Josh Cartwright67b563f2014-02-12 13:44:25 -0600580 for (i = first; i <= last; ++i) {
David Collinsb2d9a402016-07-21 14:42:47 -0700581 status = readl_relaxed(pa->acc_status +
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600582 pa->ver_ops->owner_acc_status(pa->ee, i));
Josh Cartwright67b563f2014-02-12 13:44:25 -0600583 while (status) {
584 id = ffs(status) - 1;
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800585 status &= ~BIT(id);
Abhijeet Dharmapurikar5e5078b2016-04-27 20:39:46 -0700586 apid = id + i * 32;
David Collinsb2d9a402016-07-21 14:42:47 -0700587 enable = readl_relaxed(pa->intr +
Abhijeet Dharmapurikar5e5078b2016-04-27 20:39:46 -0700588 pa->ver_ops->acc_enable(apid));
589 if (enable & SPMI_PIC_ACC_ENABLE_BIT)
Abhijeet Dharmapurikar69dc3fc2016-11-07 15:51:24 -0800590 periph_interrupt(pa, apid, show);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600591 }
592 }
Abhijeet Dharmapurikar69dc3fc2016-11-07 15:51:24 -0800593}
Josh Cartwright67b563f2014-02-12 13:44:25 -0600594
Abhijeet Dharmapurikar69dc3fc2016-11-07 15:51:24 -0800595static void pmic_arb_chained_irq(struct irq_desc *desc)
596{
597 struct spmi_pmic_arb *pa = irq_desc_get_handler_data(desc);
598 struct irq_chip *chip = irq_desc_get_chip(desc);
599
600 chained_irq_enter(chip, desc);
601 __pmic_arb_chained_irq(pa, false);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600602 chained_irq_exit(chip, desc);
603}
604
605static void qpnpint_irq_ack(struct irq_data *d)
606{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800607 struct spmi_pmic_arb *pa = irq_data_get_irq_chip_data(d);
David Collins370a4fa2016-07-21 16:58:29 -0700608 u8 irq = HWIRQ_IRQ(d->hwirq);
609 u16 apid = HWIRQ_APID(d->hwirq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600610 u8 data;
611
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800612 writel_relaxed(BIT(irq), pa->intr + pa->ver_ops->irq_clear(apid));
Josh Cartwright67b563f2014-02-12 13:44:25 -0600613
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800614 data = BIT(irq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600615 qpnpint_spmi_write(d, QPNPINT_REG_LATCHED_CLR, &data, 1);
616}
617
618static void qpnpint_irq_mask(struct irq_data *d)
619{
David Collins370a4fa2016-07-21 16:58:29 -0700620 u8 irq = HWIRQ_IRQ(d->hwirq);
Abhijeet Dharmapurikar8f8ec812016-01-08 12:54:36 -0800621 u8 data = BIT(irq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600622
Josh Cartwright67b563f2014-02-12 13:44:25 -0600623 qpnpint_spmi_write(d, QPNPINT_REG_EN_CLR, &data, 1);
624}
625
626static void qpnpint_irq_unmask(struct irq_data *d)
627{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800628 struct spmi_pmic_arb *pa = irq_data_get_irq_chip_data(d);
David Collins370a4fa2016-07-21 16:58:29 -0700629 u8 irq = HWIRQ_IRQ(d->hwirq);
630 u16 apid = HWIRQ_APID(d->hwirq);
David Collinsa5a32ce2013-11-05 09:31:16 -0800631 u8 buf[2];
Josh Cartwright67b563f2014-02-12 13:44:25 -0600632
Abhijeet Dharmapurikarc27d8632016-02-23 15:56:23 -0800633 writel_relaxed(SPMI_PIC_ACC_ENABLE_BIT,
634 pa->intr + pa->ver_ops->acc_enable(apid));
Josh Cartwright67b563f2014-02-12 13:44:25 -0600635
David Collinsa5a32ce2013-11-05 09:31:16 -0800636 qpnpint_spmi_read(d, QPNPINT_REG_EN_SET, &buf[0], 1);
637 if (!(buf[0] & BIT(irq))) {
638 /*
639 * Since the interrupt is currently disabled, write to both the
640 * LATCHED_CLR and EN_SET registers so that a spurious interrupt
641 * cannot be triggered when the interrupt is enabled
642 */
643 buf[0] = BIT(irq);
644 buf[1] = BIT(irq);
645 qpnpint_spmi_write(d, QPNPINT_REG_LATCHED_CLR, &buf, 2);
646 }
Josh Cartwright67b563f2014-02-12 13:44:25 -0600647}
648
Josh Cartwright67b563f2014-02-12 13:44:25 -0600649static int qpnpint_irq_set_type(struct irq_data *d, unsigned int flow_type)
650{
651 struct spmi_pmic_arb_qpnpint_type type;
David Collins370a4fa2016-07-21 16:58:29 -0700652 u8 irq = HWIRQ_IRQ(d->hwirq);
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800653 u8 bit_mask_irq = BIT(irq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600654
655 qpnpint_spmi_read(d, QPNPINT_REG_SET_TYPE, &type, sizeof(type));
656
657 if (flow_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)) {
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800658 type.type |= bit_mask_irq;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600659 if (flow_type & IRQF_TRIGGER_RISING)
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800660 type.polarity_high |= bit_mask_irq;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600661 if (flow_type & IRQF_TRIGGER_FALLING)
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800662 type.polarity_low |= bit_mask_irq;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600663 } else {
664 if ((flow_type & (IRQF_TRIGGER_HIGH)) &&
665 (flow_type & (IRQF_TRIGGER_LOW)))
666 return -EINVAL;
667
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800668 type.type &= ~bit_mask_irq; /* level trig */
Josh Cartwright67b563f2014-02-12 13:44:25 -0600669 if (flow_type & IRQF_TRIGGER_HIGH)
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800670 type.polarity_high |= bit_mask_irq;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600671 else
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800672 type.polarity_low |= bit_mask_irq;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600673 }
674
675 qpnpint_spmi_write(d, QPNPINT_REG_SET_TYPE, &type, sizeof(type));
Abhijeet Dharmapurikar2464e902016-04-19 20:06:46 -0700676
677 if (flow_type & IRQ_TYPE_EDGE_BOTH)
678 irq_set_handler_locked(d, handle_edge_irq);
679 else
680 irq_set_handler_locked(d, handle_level_irq);
681
Josh Cartwright67b563f2014-02-12 13:44:25 -0600682 return 0;
683}
684
Courtney Cavin60be4232015-07-30 10:53:54 -0700685static int qpnpint_get_irqchip_state(struct irq_data *d,
686 enum irqchip_irq_state which,
687 bool *state)
688{
David Collins370a4fa2016-07-21 16:58:29 -0700689 u8 irq = HWIRQ_IRQ(d->hwirq);
Courtney Cavin60be4232015-07-30 10:53:54 -0700690 u8 status = 0;
691
692 if (which != IRQCHIP_STATE_LINE_LEVEL)
693 return -EINVAL;
694
695 qpnpint_spmi_read(d, QPNPINT_REG_RT_STS, &status, 1);
696 *state = !!(status & BIT(irq));
697
698 return 0;
699}
700
Josh Cartwright67b563f2014-02-12 13:44:25 -0600701static struct irq_chip pmic_arb_irqchip = {
702 .name = "pmic_arb",
Josh Cartwright67b563f2014-02-12 13:44:25 -0600703 .irq_ack = qpnpint_irq_ack,
704 .irq_mask = qpnpint_irq_mask,
705 .irq_unmask = qpnpint_irq_unmask,
706 .irq_set_type = qpnpint_irq_set_type,
Courtney Cavin60be4232015-07-30 10:53:54 -0700707 .irq_get_irqchip_state = qpnpint_get_irqchip_state,
Josh Cartwright67b563f2014-02-12 13:44:25 -0600708 .flags = IRQCHIP_MASK_ON_SUSPEND
709 | IRQCHIP_SKIP_SET_WAKE,
710};
711
Subbaraman Narayanamurthyf115a0e2017-01-30 15:26:24 -0800712static void qpnpint_irq_domain_activate(struct irq_domain *domain,
713 struct irq_data *d)
714{
715 u8 irq = HWIRQ_IRQ(d->hwirq);
716 u8 buf;
717
718 buf = BIT(irq);
719 qpnpint_spmi_write(d, QPNPINT_REG_EN_CLR, &buf, 1);
720 qpnpint_spmi_write(d, QPNPINT_REG_LATCHED_CLR, &buf, 1);
721}
722
Josh Cartwright67b563f2014-02-12 13:44:25 -0600723static int qpnpint_irq_domain_dt_translate(struct irq_domain *d,
724 struct device_node *controller,
725 const u32 *intspec,
726 unsigned int intsize,
727 unsigned long *out_hwirq,
728 unsigned int *out_type)
729{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800730 struct spmi_pmic_arb *pa = d->host_data;
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800731 int rc;
David Collins370a4fa2016-07-21 16:58:29 -0700732 u16 apid;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600733
734 dev_dbg(&pa->spmic->dev,
735 "intspec[0] 0x%1x intspec[1] 0x%02x intspec[2] 0x%02x\n",
736 intspec[0], intspec[1], intspec[2]);
737
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100738 if (irq_domain_get_of_node(d) != controller)
Josh Cartwright67b563f2014-02-12 13:44:25 -0600739 return -EINVAL;
740 if (intsize != 4)
741 return -EINVAL;
742 if (intspec[0] > 0xF || intspec[1] > 0xFF || intspec[2] > 0x7)
743 return -EINVAL;
744
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800745 rc = pa->ver_ops->ppid_to_apid(pa, intspec[0],
746 (intspec[1] << 8), &apid);
747 if (rc < 0) {
748 dev_err(&pa->spmic->dev,
David Collinsb2d9a402016-07-21 14:42:47 -0700749 "failed to xlate sid = 0x%x, periph = 0x%x, irq = %u rc = %d\n",
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800750 intspec[0], intspec[1], intspec[2], rc);
751 return rc;
752 }
Josh Cartwright67b563f2014-02-12 13:44:25 -0600753
David Collinsb2d9a402016-07-21 14:42:47 -0700754 if (pa->apid_data[apid].irq_owner != pa->ee) {
755 dev_err(&pa->spmic->dev, "failed to xlate sid = 0x%x, periph = 0x%x, irq = %u: ee=%u but owner=%u\n",
756 intspec[0], intspec[1], intspec[2], pa->ee,
757 pa->apid_data[apid].irq_owner);
758 return -ENODEV;
759 }
760
Josh Cartwright67b563f2014-02-12 13:44:25 -0600761 /* Keep track of {max,min}_apid for bounding search during interrupt */
762 if (apid > pa->max_apid)
763 pa->max_apid = apid;
764 if (apid < pa->min_apid)
765 pa->min_apid = apid;
766
David Collins370a4fa2016-07-21 16:58:29 -0700767 *out_hwirq = HWIRQ(intspec[0], intspec[1], intspec[2], apid);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600768 *out_type = intspec[3] & IRQ_TYPE_SENSE_MASK;
769
770 dev_dbg(&pa->spmic->dev, "out_hwirq = %lu\n", *out_hwirq);
771
772 return 0;
773}
774
775static int qpnpint_irq_domain_map(struct irq_domain *d,
776 unsigned int virq,
777 irq_hw_number_t hwirq)
778{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800779 struct spmi_pmic_arb *pa = d->host_data;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600780
781 dev_dbg(&pa->spmic->dev, "virq = %u, hwirq = %lu\n", virq, hwirq);
782
783 irq_set_chip_and_handler(virq, &pmic_arb_irqchip, handle_level_irq);
784 irq_set_chip_data(virq, d->host_data);
785 irq_set_noprobe(virq);
786 return 0;
787}
788
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800789static int
David Collins370a4fa2016-07-21 16:58:29 -0700790pmic_arb_ppid_to_apid_v1(struct spmi_pmic_arb *pa, u8 sid, u16 addr, u16 *apid)
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800791{
792 u16 ppid = sid << 8 | ((addr >> 8) & 0xFF);
793 u32 *mapping_table = pa->mapping_table;
794 int index = 0, i;
795 u16 apid_valid;
796 u32 data;
797
798 apid_valid = pa->ppid_to_apid[ppid];
799 if (apid_valid & PMIC_ARB_CHAN_VALID) {
800 *apid = (apid_valid & ~PMIC_ARB_CHAN_VALID);
801 return 0;
802 }
803
804 for (i = 0; i < SPMI_MAPPING_TABLE_TREE_DEPTH; ++i) {
805 if (!test_and_set_bit(index, pa->mapping_table_valid))
806 mapping_table[index] = readl_relaxed(pa->cnfg +
807 SPMI_MAPPING_TABLE_REG(index));
808
809 data = mapping_table[index];
810
811 if (ppid & BIT(SPMI_MAPPING_BIT_INDEX(data))) {
812 if (SPMI_MAPPING_BIT_IS_1_FLAG(data)) {
813 index = SPMI_MAPPING_BIT_IS_1_RESULT(data);
814 } else {
815 *apid = SPMI_MAPPING_BIT_IS_1_RESULT(data);
816 pa->ppid_to_apid[ppid]
817 = *apid | PMIC_ARB_CHAN_VALID;
Abhijeet Dharmapurikar8f8ec812016-01-08 12:54:36 -0800818 pa->apid_data[*apid].ppid = ppid;
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800819 return 0;
820 }
821 } else {
822 if (SPMI_MAPPING_BIT_IS_0_FLAG(data)) {
823 index = SPMI_MAPPING_BIT_IS_0_RESULT(data);
824 } else {
825 *apid = SPMI_MAPPING_BIT_IS_0_RESULT(data);
826 pa->ppid_to_apid[ppid]
827 = *apid | PMIC_ARB_CHAN_VALID;
Abhijeet Dharmapurikar8f8ec812016-01-08 12:54:36 -0800828 pa->apid_data[*apid].ppid = ppid;
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800829 return 0;
830 }
831 }
832 }
833
834 return -ENODEV;
835}
836
837static int
Abhijeet Dharmapurikar78888862016-07-05 17:54:47 -0700838pmic_arb_mode_v1_v3(struct spmi_pmic_arb *pa, u8 sid, u16 addr, mode_t *mode)
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800839{
840 *mode = 0600;
841 return 0;
842}
843
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600844/* v1 offset per ee */
Stephen Boyd987a9f12015-11-17 16:13:55 -0800845static int
David Collinsb2d9a402016-07-21 14:42:47 -0700846pmic_arb_offset_v1(struct spmi_pmic_arb *pa, u8 sid, u16 addr,
847 enum pmic_arb_channel ch_type, u32 *offset)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600848{
Stephen Boyd987a9f12015-11-17 16:13:55 -0800849 *offset = 0x800 + 0x80 * pa->channel;
850 return 0;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600851}
852
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800853static u16 pmic_arb_find_apid(struct spmi_pmic_arb *pa, u16 ppid)
Stephen Boyd987a9f12015-11-17 16:13:55 -0800854{
855 u32 regval, offset;
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800856 u16 apid;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800857 u16 id;
858
859 /*
860 * PMIC_ARB_REG_CHNL is a table in HW mapping channel to ppid.
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800861 * ppid_to_apid is an in-memory invert of that table.
Stephen Boyd987a9f12015-11-17 16:13:55 -0800862 */
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800863 for (apid = pa->last_apid; apid < pa->max_periph; apid++) {
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800864 regval = readl_relaxed(pa->cnfg +
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800865 SPMI_OWNERSHIP_TABLE_REG(apid));
David Collinsb2d9a402016-07-21 14:42:47 -0700866 pa->apid_data[apid].irq_owner
867 = SPMI_OWNERSHIP_PERIPH2OWNER(regval);
868 pa->apid_data[apid].write_owner = pa->apid_data[apid].irq_owner;
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800869
David Collinsb2d9a402016-07-21 14:42:47 -0700870 offset = pa->ver_ops->channel_map_offset(apid);
Stephen Boyd987a9f12015-11-17 16:13:55 -0800871 if (offset >= pa->core_size)
872 break;
873
874 regval = readl_relaxed(pa->core + offset);
875 if (!regval)
876 continue;
877
878 id = (regval >> 8) & PMIC_ARB_PPID_MASK;
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800879 pa->ppid_to_apid[id] = apid | PMIC_ARB_CHAN_VALID;
Abhijeet Dharmapurikar8f8ec812016-01-08 12:54:36 -0800880 pa->apid_data[apid].ppid = id;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800881 if (id == ppid) {
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800882 apid |= PMIC_ARB_CHAN_VALID;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800883 break;
884 }
885 }
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800886 pa->last_apid = apid & ~PMIC_ARB_CHAN_VALID;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800887
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800888 return apid;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800889}
890
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800891static int
David Collins370a4fa2016-07-21 16:58:29 -0700892pmic_arb_ppid_to_apid_v2(struct spmi_pmic_arb *pa, u8 sid, u16 addr, u16 *apid)
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800893{
894 u16 ppid = (sid << 8) | (addr >> 8);
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800895 u16 apid_valid;
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800896
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800897 apid_valid = pa->ppid_to_apid[ppid];
898 if (!(apid_valid & PMIC_ARB_CHAN_VALID))
899 apid_valid = pmic_arb_find_apid(pa, ppid);
900 if (!(apid_valid & PMIC_ARB_CHAN_VALID))
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800901 return -ENODEV;
902
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800903 *apid = (apid_valid & ~PMIC_ARB_CHAN_VALID);
904 return 0;
905}
906
David Collinsb2d9a402016-07-21 14:42:47 -0700907static int pmic_arb_read_apid_map_v5(struct spmi_pmic_arb *pa)
908{
909 u32 regval, offset;
910 u16 apid, prev_apid, ppid;
911 bool valid, is_irq_owner;
912
913 /*
914 * PMIC_ARB_REG_CHNL is a table in HW mapping APID (channel) to PPID.
915 * ppid_to_apid is an in-memory invert of that table. In order to allow
916 * multiple EE's to write to a single PPID in arbiter version 5, there
917 * is more than one APID mapped to each PPID. The owner field for each
918 * of these mappings specifies the EE which is allowed to write to the
919 * APID. The owner of the last (highest) APID for a given PPID will
920 * receive interrupts from the PPID.
921 */
922 for (apid = 0; apid < pa->max_periph; apid++) {
923 offset = pa->ver_ops->channel_map_offset(apid);
924 if (offset >= pa->core_size)
925 break;
926
927 regval = readl_relaxed(pa->core + offset);
928 if (!regval)
929 continue;
930 ppid = (regval >> 8) & PMIC_ARB_PPID_MASK;
931 is_irq_owner = PMIC_ARB_CHAN_IS_IRQ_OWNER(regval);
932
933 regval = readl_relaxed(pa->cnfg +
934 SPMI_OWNERSHIP_TABLE_REG(apid));
935 pa->apid_data[apid].write_owner
936 = SPMI_OWNERSHIP_PERIPH2OWNER(regval);
937
938 pa->apid_data[apid].irq_owner = is_irq_owner ?
939 pa->apid_data[apid].write_owner : INVALID_EE;
940
941 valid = pa->ppid_to_apid[ppid] & PMIC_ARB_CHAN_VALID;
942 prev_apid = pa->ppid_to_apid[ppid] & ~PMIC_ARB_CHAN_VALID;
943
944 if (valid && is_irq_owner &&
945 pa->apid_data[prev_apid].write_owner == pa->ee) {
946 /*
947 * Duplicate PPID mapping after the one for this EE;
948 * override the irq owner
949 */
950 pa->apid_data[prev_apid].irq_owner
951 = pa->apid_data[apid].irq_owner;
952 } else if (!valid || is_irq_owner) {
953 /* First PPID mapping or duplicate for another EE */
954 pa->ppid_to_apid[ppid] = apid | PMIC_ARB_CHAN_VALID;
955 }
956
957 pa->apid_data[apid].ppid = ppid;
958 pa->last_apid = apid;
959 }
960
961 /* Dump the mapping table for debug purposes. */
962 dev_dbg(&pa->spmic->dev, "PPID APID Write-EE IRQ-EE\n");
963 for (ppid = 0; ppid < PMIC_ARB_MAX_PPID; ppid++) {
964 valid = pa->ppid_to_apid[ppid] & PMIC_ARB_CHAN_VALID;
965 apid = pa->ppid_to_apid[ppid] & ~PMIC_ARB_CHAN_VALID;
966
967 if (valid)
968 dev_dbg(&pa->spmic->dev, "0x%03X %3u %2u %2u\n",
969 ppid, apid, pa->apid_data[apid].write_owner,
970 pa->apid_data[apid].irq_owner);
971 }
972
973 return 0;
974}
975
976static int
977pmic_arb_ppid_to_apid_v5(struct spmi_pmic_arb *pa, u8 sid, u16 addr, u16 *apid)
978{
979 u16 ppid = (sid << 8) | (addr >> 8);
980
981 if (!(pa->ppid_to_apid[ppid] & PMIC_ARB_CHAN_VALID))
982 return -ENODEV;
983
984 *apid = pa->ppid_to_apid[ppid] & ~PMIC_ARB_CHAN_VALID;
985
986 return 0;
987}
988
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800989static int
990pmic_arb_mode_v2(struct spmi_pmic_arb *pa, u8 sid, u16 addr, mode_t *mode)
991{
David Collins370a4fa2016-07-21 16:58:29 -0700992 u16 apid;
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800993 int rc;
994
David Collinsb2d9a402016-07-21 14:42:47 -0700995 rc = pa->ver_ops->ppid_to_apid(pa, sid, addr, &apid);
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800996 if (rc < 0)
997 return rc;
998
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800999 *mode = 0;
1000 *mode |= 0400;
1001
David Collinsb2d9a402016-07-21 14:42:47 -07001002 if (pa->ee == pa->apid_data[apid].write_owner)
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -08001003 *mode |= 0200;
1004 return 0;
1005}
Stephen Boyd987a9f12015-11-17 16:13:55 -08001006
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -08001007/* v2 offset per ppid and per ee */
Stephen Boyd987a9f12015-11-17 16:13:55 -08001008static int
David Collinsb2d9a402016-07-21 14:42:47 -07001009pmic_arb_offset_v2(struct spmi_pmic_arb *pa, u8 sid, u16 addr,
1010 enum pmic_arb_channel ch_type, u32 *offset)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001011{
David Collins370a4fa2016-07-21 16:58:29 -07001012 u16 apid;
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -08001013 int rc;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001014
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -08001015 rc = pmic_arb_ppid_to_apid_v2(pa, sid, addr, &apid);
1016 if (rc < 0)
1017 return rc;
Stephen Boyd987a9f12015-11-17 16:13:55 -08001018
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -08001019 *offset = 0x1000 * pa->ee + 0x8000 * apid;
Stephen Boyd987a9f12015-11-17 16:13:55 -08001020 return 0;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001021}
1022
David Collinsb2d9a402016-07-21 14:42:47 -07001023/*
1024 * v5 offset per ee and per apid for observer channels and per apid for
1025 * read/write channels.
1026 */
1027static int
1028pmic_arb_offset_v5(struct spmi_pmic_arb *pa, u8 sid, u16 addr,
1029 enum pmic_arb_channel ch_type, u32 *offset)
1030{
1031 u16 apid;
1032 int rc;
1033
1034 rc = pmic_arb_ppid_to_apid_v5(pa, sid, addr, &apid);
1035 if (rc < 0)
1036 return rc;
1037
1038 *offset = (ch_type == PMIC_ARB_CHANNEL_OBS)
1039 ? 0x10000 * pa->ee + 0x80 * apid
1040 : 0x10000 * apid;
1041 return 0;
1042}
1043
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001044static u32 pmic_arb_fmt_cmd_v1(u8 opc, u8 sid, u16 addr, u8 bc)
1045{
1046 return (opc << 27) | ((sid & 0xf) << 20) | (addr << 4) | (bc & 0x7);
1047}
1048
1049static u32 pmic_arb_fmt_cmd_v2(u8 opc, u8 sid, u16 addr, u8 bc)
1050{
1051 return (opc << 27) | ((addr & 0xff) << 4) | (bc & 0x7);
1052}
1053
David Collins370a4fa2016-07-21 16:58:29 -07001054static u32 pmic_arb_owner_acc_status_v1(u8 m, u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001055{
1056 return 0x20 * m + 0x4 * n;
1057}
1058
David Collins370a4fa2016-07-21 16:58:29 -07001059static u32 pmic_arb_owner_acc_status_v2(u8 m, u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001060{
1061 return 0x100000 + 0x1000 * m + 0x4 * n;
1062}
1063
David Collins370a4fa2016-07-21 16:58:29 -07001064static u32 pmic_arb_owner_acc_status_v3(u8 m, u16 n)
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001065{
1066 return 0x200000 + 0x1000 * m + 0x4 * n;
1067}
1068
David Collinsb2d9a402016-07-21 14:42:47 -07001069static u32 pmic_arb_owner_acc_status_v5(u8 m, u16 n)
1070{
1071 return 0x10000 * m + 0x4 * n;
1072}
1073
David Collins370a4fa2016-07-21 16:58:29 -07001074static u32 pmic_arb_acc_enable_v1(u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001075{
1076 return 0x200 + 0x4 * n;
1077}
1078
David Collins370a4fa2016-07-21 16:58:29 -07001079static u32 pmic_arb_acc_enable_v2(u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001080{
1081 return 0x1000 * n;
1082}
1083
David Collinsb2d9a402016-07-21 14:42:47 -07001084static u32 pmic_arb_acc_enable_v5(u16 n)
1085{
1086 return 0x100 + 0x10000 * n;
1087}
1088
David Collins370a4fa2016-07-21 16:58:29 -07001089static u32 pmic_arb_irq_status_v1(u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001090{
1091 return 0x600 + 0x4 * n;
1092}
1093
David Collins370a4fa2016-07-21 16:58:29 -07001094static u32 pmic_arb_irq_status_v2(u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001095{
1096 return 0x4 + 0x1000 * n;
1097}
1098
David Collinsb2d9a402016-07-21 14:42:47 -07001099static u32 pmic_arb_irq_status_v5(u16 n)
1100{
1101 return 0x104 + 0x10000 * n;
1102}
1103
David Collins370a4fa2016-07-21 16:58:29 -07001104static u32 pmic_arb_irq_clear_v1(u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001105{
1106 return 0xA00 + 0x4 * n;
1107}
1108
David Collins370a4fa2016-07-21 16:58:29 -07001109static u32 pmic_arb_irq_clear_v2(u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001110{
1111 return 0x8 + 0x1000 * n;
1112}
1113
David Collinsb2d9a402016-07-21 14:42:47 -07001114static u32 pmic_arb_irq_clear_v5(u16 n)
1115{
1116 return 0x108 + 0x10000 * n;
1117}
1118
1119static u32 pmic_arb_channel_map_offset_v2(u16 n)
1120{
1121 return 0x800 + 0x4 * n;
1122}
1123
1124static u32 pmic_arb_channel_map_offset_v5(u16 n)
1125{
1126 return 0x900 + 0x4 * n;
1127}
1128
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001129static const struct pmic_arb_ver_ops pmic_arb_v1 = {
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001130 .ver_str = "v1",
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -08001131 .ppid_to_apid = pmic_arb_ppid_to_apid_v1,
Abhijeet Dharmapurikar78888862016-07-05 17:54:47 -07001132 .mode = pmic_arb_mode_v1_v3,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001133 .non_data_cmd = pmic_arb_non_data_cmd_v1,
1134 .offset = pmic_arb_offset_v1,
1135 .fmt_cmd = pmic_arb_fmt_cmd_v1,
1136 .owner_acc_status = pmic_arb_owner_acc_status_v1,
1137 .acc_enable = pmic_arb_acc_enable_v1,
1138 .irq_status = pmic_arb_irq_status_v1,
1139 .irq_clear = pmic_arb_irq_clear_v1,
David Collinsb2d9a402016-07-21 14:42:47 -07001140 .channel_map_offset = pmic_arb_channel_map_offset_v2,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001141};
1142
1143static const struct pmic_arb_ver_ops pmic_arb_v2 = {
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001144 .ver_str = "v2",
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -08001145 .ppid_to_apid = pmic_arb_ppid_to_apid_v2,
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -08001146 .mode = pmic_arb_mode_v2,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001147 .non_data_cmd = pmic_arb_non_data_cmd_v2,
1148 .offset = pmic_arb_offset_v2,
1149 .fmt_cmd = pmic_arb_fmt_cmd_v2,
1150 .owner_acc_status = pmic_arb_owner_acc_status_v2,
1151 .acc_enable = pmic_arb_acc_enable_v2,
1152 .irq_status = pmic_arb_irq_status_v2,
1153 .irq_clear = pmic_arb_irq_clear_v2,
David Collinsb2d9a402016-07-21 14:42:47 -07001154 .channel_map_offset = pmic_arb_channel_map_offset_v2,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001155};
1156
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001157static const struct pmic_arb_ver_ops pmic_arb_v3 = {
1158 .ver_str = "v3",
1159 .ppid_to_apid = pmic_arb_ppid_to_apid_v2,
Abhijeet Dharmapurikar78888862016-07-05 17:54:47 -07001160 .mode = pmic_arb_mode_v1_v3,
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001161 .non_data_cmd = pmic_arb_non_data_cmd_v2,
1162 .offset = pmic_arb_offset_v2,
1163 .fmt_cmd = pmic_arb_fmt_cmd_v2,
1164 .owner_acc_status = pmic_arb_owner_acc_status_v3,
1165 .acc_enable = pmic_arb_acc_enable_v2,
1166 .irq_status = pmic_arb_irq_status_v2,
1167 .irq_clear = pmic_arb_irq_clear_v2,
David Collinsb2d9a402016-07-21 14:42:47 -07001168 .channel_map_offset = pmic_arb_channel_map_offset_v2,
1169};
1170
1171static const struct pmic_arb_ver_ops pmic_arb_v5 = {
1172 .ver_str = "v5",
1173 .ppid_to_apid = pmic_arb_ppid_to_apid_v5,
1174 .mode = pmic_arb_mode_v2,
1175 .non_data_cmd = pmic_arb_non_data_cmd_v2,
1176 .offset = pmic_arb_offset_v5,
1177 .fmt_cmd = pmic_arb_fmt_cmd_v2,
1178 .owner_acc_status = pmic_arb_owner_acc_status_v5,
1179 .acc_enable = pmic_arb_acc_enable_v5,
1180 .irq_status = pmic_arb_irq_status_v5,
1181 .irq_clear = pmic_arb_irq_clear_v5,
1182 .channel_map_offset = pmic_arb_channel_map_offset_v5,
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001183};
1184
Josh Cartwright67b563f2014-02-12 13:44:25 -06001185static const struct irq_domain_ops pmic_arb_irq_domain_ops = {
1186 .map = qpnpint_irq_domain_map,
1187 .xlate = qpnpint_irq_domain_dt_translate,
Subbaraman Narayanamurthyf115a0e2017-01-30 15:26:24 -08001188 .activate = qpnpint_irq_domain_activate,
Josh Cartwright67b563f2014-02-12 13:44:25 -06001189};
1190
Abhijeet Dharmapurikar69dc3fc2016-11-07 15:51:24 -08001191static void spmi_pmic_arb_resume(void)
1192{
1193 if (spmi_show_resume_irq())
1194 __pmic_arb_chained_irq(the_pa, true);
1195}
1196
1197static struct syscore_ops spmi_pmic_arb_syscore_ops = {
1198 .resume = spmi_pmic_arb_resume,
1199};
1200
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001201static int spmi_pmic_arb_probe(struct platform_device *pdev)
1202{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -08001203 struct spmi_pmic_arb *pa;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001204 struct spmi_controller *ctrl;
1205 struct resource *res;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001206 void __iomem *core;
1207 u32 channel, ee, hw_ver;
Stephen Boyd987a9f12015-11-17 16:13:55 -08001208 int err;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001209
1210 ctrl = spmi_controller_alloc(&pdev->dev, sizeof(*pa));
1211 if (!ctrl)
1212 return -ENOMEM;
1213
1214 pa = spmi_controller_get_drvdata(ctrl);
Josh Cartwright67b563f2014-02-12 13:44:25 -06001215 pa->spmic = ctrl;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001216
1217 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "core");
Abhijeet Dharmapurikar57132f52016-09-13 11:10:48 -07001218 if (!res) {
1219 dev_err(&pdev->dev, "core resource not specified\n");
1220 err = -EINVAL;
1221 goto err_put_ctrl;
1222 }
1223
Stephen Boyd987a9f12015-11-17 16:13:55 -08001224 pa->core_size = resource_size(res);
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -08001225 if (pa->core_size <= 0x800) {
1226 dev_err(&pdev->dev, "core_size is smaller than 0x800. Failing Probe\n");
1227 err = -EINVAL;
1228 goto err_put_ctrl;
1229 }
1230
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001231 core = devm_ioremap_resource(&ctrl->dev, res);
1232 if (IS_ERR(core)) {
1233 err = PTR_ERR(core);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001234 goto err_put_ctrl;
1235 }
1236
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001237 hw_ver = readl_relaxed(core + PMIC_ARB_VERSION);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001238
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001239 if (hw_ver < PMIC_ARB_VERSION_V2_MIN) {
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001240 pa->ver_ops = &pmic_arb_v1;
1241 pa->wr_base = core;
1242 pa->rd_base = core;
1243 } else {
Stephen Boyd987a9f12015-11-17 16:13:55 -08001244 pa->core = core;
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001245
1246 if (hw_ver < PMIC_ARB_VERSION_V3_MIN)
1247 pa->ver_ops = &pmic_arb_v2;
David Collinsb2d9a402016-07-21 14:42:47 -07001248 else if (hw_ver < PMIC_ARB_VERSION_V5_MIN)
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001249 pa->ver_ops = &pmic_arb_v3;
David Collinsb2d9a402016-07-21 14:42:47 -07001250 else
1251 pa->ver_ops = &pmic_arb_v5;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001252
David Collinsb2d9a402016-07-21 14:42:47 -07001253 /* the apid to ppid table starts at PMIC_ARB_REG_CHNL0 */
1254 pa->max_periph
1255 = (pa->core_size - pa->ver_ops->channel_map_offset(0)) / 4;
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -08001256
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001257 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1258 "obsrvr");
1259 pa->rd_base = devm_ioremap_resource(&ctrl->dev, res);
1260 if (IS_ERR(pa->rd_base)) {
1261 err = PTR_ERR(pa->rd_base);
1262 goto err_put_ctrl;
1263 }
1264
1265 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1266 "chnls");
1267 pa->wr_base = devm_ioremap_resource(&ctrl->dev, res);
1268 if (IS_ERR(pa->wr_base)) {
1269 err = PTR_ERR(pa->wr_base);
1270 goto err_put_ctrl;
1271 }
1272
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -08001273 pa->ppid_to_apid = devm_kcalloc(&ctrl->dev,
Stephen Boyd987a9f12015-11-17 16:13:55 -08001274 PMIC_ARB_MAX_PPID,
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -08001275 sizeof(*pa->ppid_to_apid),
Stephen Boyd987a9f12015-11-17 16:13:55 -08001276 GFP_KERNEL);
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -08001277 if (!pa->ppid_to_apid) {
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001278 err = -ENOMEM;
1279 goto err_put_ctrl;
1280 }
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001281 }
1282
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001283 dev_info(&ctrl->dev, "PMIC arbiter version %s (0x%x)\n",
1284 pa->ver_ops->ver_str, hw_ver);
1285
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001286 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "intr");
1287 pa->intr = devm_ioremap_resource(&ctrl->dev, res);
1288 if (IS_ERR(pa->intr)) {
1289 err = PTR_ERR(pa->intr);
1290 goto err_put_ctrl;
1291 }
David Collinsb2d9a402016-07-21 14:42:47 -07001292 pa->acc_status = pa->intr;
1293
1294 /*
1295 * PMIC arbiter v5 groups the IRQ control registers in the same hardware
1296 * module as the read/write channels.
1297 */
1298 if (hw_ver >= PMIC_ARB_VERSION_V5_MIN)
1299 pa->intr = pa->wr_base;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001300
1301 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cnfg");
1302 pa->cnfg = devm_ioremap_resource(&ctrl->dev, res);
1303 if (IS_ERR(pa->cnfg)) {
1304 err = PTR_ERR(pa->cnfg);
1305 goto err_put_ctrl;
1306 }
1307
Josh Cartwright67b563f2014-02-12 13:44:25 -06001308 pa->irq = platform_get_irq_byname(pdev, "periph_irq");
1309 if (pa->irq < 0) {
1310 err = pa->irq;
1311 goto err_put_ctrl;
1312 }
1313
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001314 err = of_property_read_u32(pdev->dev.of_node, "qcom,channel", &channel);
1315 if (err) {
1316 dev_err(&pdev->dev, "channel unspecified.\n");
1317 goto err_put_ctrl;
1318 }
1319
1320 if (channel > 5) {
1321 dev_err(&pdev->dev, "invalid channel (%u) specified.\n",
1322 channel);
Christophe JAILLETe98cc182016-09-26 22:24:46 +02001323 err = -EINVAL;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001324 goto err_put_ctrl;
1325 }
1326
1327 pa->channel = channel;
1328
Josh Cartwright67b563f2014-02-12 13:44:25 -06001329 err = of_property_read_u32(pdev->dev.of_node, "qcom,ee", &ee);
1330 if (err) {
1331 dev_err(&pdev->dev, "EE unspecified.\n");
1332 goto err_put_ctrl;
1333 }
1334
1335 if (ee > 5) {
1336 dev_err(&pdev->dev, "invalid EE (%u) specified\n", ee);
1337 err = -EINVAL;
1338 goto err_put_ctrl;
1339 }
1340
1341 pa->ee = ee;
1342
Stephen Boyd987a9f12015-11-17 16:13:55 -08001343 pa->mapping_table = devm_kcalloc(&ctrl->dev, PMIC_ARB_MAX_PERIPHS - 1,
1344 sizeof(*pa->mapping_table), GFP_KERNEL);
1345 if (!pa->mapping_table) {
1346 err = -ENOMEM;
1347 goto err_put_ctrl;
1348 }
Josh Cartwright67b563f2014-02-12 13:44:25 -06001349
1350 /* Initialize max_apid/min_apid to the opposite bounds, during
1351 * the irq domain translation, we are sure to update these */
1352 pa->max_apid = 0;
1353 pa->min_apid = PMIC_ARB_MAX_PERIPHS - 1;
1354
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001355 platform_set_drvdata(pdev, ctrl);
1356 raw_spin_lock_init(&pa->lock);
1357
1358 ctrl->cmd = pmic_arb_cmd;
1359 ctrl->read_cmd = pmic_arb_read_cmd;
1360 ctrl->write_cmd = pmic_arb_write_cmd;
1361
David Collinsb2d9a402016-07-21 14:42:47 -07001362 if (hw_ver >= PMIC_ARB_VERSION_V5_MIN) {
1363 err = pmic_arb_read_apid_map_v5(pa);
1364 if (err) {
1365 dev_err(&pdev->dev, "could not read APID->PPID mapping table, rc= %d\n",
1366 err);
1367 goto err_put_ctrl;
1368 }
1369 }
1370
Josh Cartwright67b563f2014-02-12 13:44:25 -06001371 dev_dbg(&pdev->dev, "adding irq domain\n");
1372 pa->domain = irq_domain_add_tree(pdev->dev.of_node,
1373 &pmic_arb_irq_domain_ops, pa);
1374 if (!pa->domain) {
1375 dev_err(&pdev->dev, "unable to create irq_domain\n");
1376 err = -ENOMEM;
1377 goto err_put_ctrl;
1378 }
1379
Thomas Gleixnerfb68ba62015-07-13 20:52:24 +00001380 irq_set_chained_handler_and_data(pa->irq, pmic_arb_chained_irq, pa);
Nicholas Troast237e9142016-06-14 16:39:38 -07001381 enable_irq_wake(pa->irq);
Josh Cartwright67b563f2014-02-12 13:44:25 -06001382
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001383 err = spmi_controller_add(ctrl);
1384 if (err)
Josh Cartwright67b563f2014-02-12 13:44:25 -06001385 goto err_domain_remove;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001386
Abhijeet Dharmapurikar69dc3fc2016-11-07 15:51:24 -08001387 the_pa = pa;
1388 register_syscore_ops(&spmi_pmic_arb_syscore_ops);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001389 return 0;
1390
Josh Cartwright67b563f2014-02-12 13:44:25 -06001391err_domain_remove:
Thomas Gleixnerfb68ba62015-07-13 20:52:24 +00001392 irq_set_chained_handler_and_data(pa->irq, NULL, NULL);
Josh Cartwright67b563f2014-02-12 13:44:25 -06001393 irq_domain_remove(pa->domain);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001394err_put_ctrl:
1395 spmi_controller_put(ctrl);
1396 return err;
1397}
1398
1399static int spmi_pmic_arb_remove(struct platform_device *pdev)
1400{
1401 struct spmi_controller *ctrl = platform_get_drvdata(pdev);
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -08001402 struct spmi_pmic_arb *pa = spmi_controller_get_drvdata(ctrl);
Abhijeet Dharmapurikar69dc3fc2016-11-07 15:51:24 -08001403
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001404 spmi_controller_remove(ctrl);
Thomas Gleixnerfb68ba62015-07-13 20:52:24 +00001405 irq_set_chained_handler_and_data(pa->irq, NULL, NULL);
Abhijeet Dharmapurikar69dc3fc2016-11-07 15:51:24 -08001406 unregister_syscore_ops(&spmi_pmic_arb_syscore_ops);
1407 the_pa = NULL;
Josh Cartwright67b563f2014-02-12 13:44:25 -06001408 irq_domain_remove(pa->domain);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001409 spmi_controller_put(ctrl);
1410 return 0;
1411}
1412
1413static const struct of_device_id spmi_pmic_arb_match_table[] = {
1414 { .compatible = "qcom,spmi-pmic-arb", },
1415 {},
1416};
1417MODULE_DEVICE_TABLE(of, spmi_pmic_arb_match_table);
1418
1419static struct platform_driver spmi_pmic_arb_driver = {
1420 .probe = spmi_pmic_arb_probe,
1421 .remove = spmi_pmic_arb_remove,
1422 .driver = {
1423 .name = "spmi_pmic_arb",
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001424 .of_match_table = spmi_pmic_arb_match_table,
1425 },
1426};
Abhijeet Dharmapurikardf9bf942015-09-23 11:36:23 -07001427
1428int __init spmi_pmic_arb_init(void)
1429{
1430 return platform_driver_register(&spmi_pmic_arb_driver);
1431}
1432arch_initcall(spmi_pmic_arb_init);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001433
1434MODULE_LICENSE("GPL v2");
1435MODULE_ALIAS("platform:spmi_pmic_arb");