blob: 9cc85eea8cef0f61401f26150f0d989f8d84e404 [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 Collinsdc817982017-05-12 14:19:20 -0700587 if (apid < pa->min_apid || apid > pa->max_apid) {
588 WARN_ONCE(true, "spurious spmi irq received for apid=%d\n",
589 apid);
590 continue;
591 }
David Collinsb2d9a402016-07-21 14:42:47 -0700592 enable = readl_relaxed(pa->intr +
Abhijeet Dharmapurikar5e5078b2016-04-27 20:39:46 -0700593 pa->ver_ops->acc_enable(apid));
594 if (enable & SPMI_PIC_ACC_ENABLE_BIT)
Abhijeet Dharmapurikar69dc3fc2016-11-07 15:51:24 -0800595 periph_interrupt(pa, apid, show);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600596 }
597 }
Abhijeet Dharmapurikar69dc3fc2016-11-07 15:51:24 -0800598}
Josh Cartwright67b563f2014-02-12 13:44:25 -0600599
Abhijeet Dharmapurikar69dc3fc2016-11-07 15:51:24 -0800600static void pmic_arb_chained_irq(struct irq_desc *desc)
601{
602 struct spmi_pmic_arb *pa = irq_desc_get_handler_data(desc);
603 struct irq_chip *chip = irq_desc_get_chip(desc);
604
605 chained_irq_enter(chip, desc);
606 __pmic_arb_chained_irq(pa, false);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600607 chained_irq_exit(chip, desc);
608}
609
610static void qpnpint_irq_ack(struct irq_data *d)
611{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800612 struct spmi_pmic_arb *pa = irq_data_get_irq_chip_data(d);
David Collins370a4fa2016-07-21 16:58:29 -0700613 u8 irq = HWIRQ_IRQ(d->hwirq);
614 u16 apid = HWIRQ_APID(d->hwirq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600615 u8 data;
616
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800617 writel_relaxed(BIT(irq), pa->intr + pa->ver_ops->irq_clear(apid));
Josh Cartwright67b563f2014-02-12 13:44:25 -0600618
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800619 data = BIT(irq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600620 qpnpint_spmi_write(d, QPNPINT_REG_LATCHED_CLR, &data, 1);
621}
622
623static void qpnpint_irq_mask(struct irq_data *d)
624{
David Collins370a4fa2016-07-21 16:58:29 -0700625 u8 irq = HWIRQ_IRQ(d->hwirq);
Abhijeet Dharmapurikar8f8ec812016-01-08 12:54:36 -0800626 u8 data = BIT(irq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600627
Josh Cartwright67b563f2014-02-12 13:44:25 -0600628 qpnpint_spmi_write(d, QPNPINT_REG_EN_CLR, &data, 1);
629}
630
631static void qpnpint_irq_unmask(struct irq_data *d)
632{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800633 struct spmi_pmic_arb *pa = irq_data_get_irq_chip_data(d);
David Collins370a4fa2016-07-21 16:58:29 -0700634 u8 irq = HWIRQ_IRQ(d->hwirq);
635 u16 apid = HWIRQ_APID(d->hwirq);
David Collinsa5a32ce2013-11-05 09:31:16 -0800636 u8 buf[2];
Josh Cartwright67b563f2014-02-12 13:44:25 -0600637
Abhijeet Dharmapurikarc27d8632016-02-23 15:56:23 -0800638 writel_relaxed(SPMI_PIC_ACC_ENABLE_BIT,
639 pa->intr + pa->ver_ops->acc_enable(apid));
Josh Cartwright67b563f2014-02-12 13:44:25 -0600640
David Collinsa5a32ce2013-11-05 09:31:16 -0800641 qpnpint_spmi_read(d, QPNPINT_REG_EN_SET, &buf[0], 1);
642 if (!(buf[0] & BIT(irq))) {
643 /*
644 * Since the interrupt is currently disabled, write to both the
645 * LATCHED_CLR and EN_SET registers so that a spurious interrupt
646 * cannot be triggered when the interrupt is enabled
647 */
648 buf[0] = BIT(irq);
649 buf[1] = BIT(irq);
650 qpnpint_spmi_write(d, QPNPINT_REG_LATCHED_CLR, &buf, 2);
651 }
Josh Cartwright67b563f2014-02-12 13:44:25 -0600652}
653
Josh Cartwright67b563f2014-02-12 13:44:25 -0600654static int qpnpint_irq_set_type(struct irq_data *d, unsigned int flow_type)
655{
656 struct spmi_pmic_arb_qpnpint_type type;
David Collins370a4fa2016-07-21 16:58:29 -0700657 u8 irq = HWIRQ_IRQ(d->hwirq);
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800658 u8 bit_mask_irq = BIT(irq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600659
660 qpnpint_spmi_read(d, QPNPINT_REG_SET_TYPE, &type, sizeof(type));
661
662 if (flow_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)) {
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800663 type.type |= bit_mask_irq;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600664 if (flow_type & IRQF_TRIGGER_RISING)
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800665 type.polarity_high |= bit_mask_irq;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600666 if (flow_type & IRQF_TRIGGER_FALLING)
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800667 type.polarity_low |= bit_mask_irq;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600668 } else {
669 if ((flow_type & (IRQF_TRIGGER_HIGH)) &&
670 (flow_type & (IRQF_TRIGGER_LOW)))
671 return -EINVAL;
672
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800673 type.type &= ~bit_mask_irq; /* level trig */
Josh Cartwright67b563f2014-02-12 13:44:25 -0600674 if (flow_type & IRQF_TRIGGER_HIGH)
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800675 type.polarity_high |= bit_mask_irq;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600676 else
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800677 type.polarity_low |= bit_mask_irq;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600678 }
679
680 qpnpint_spmi_write(d, QPNPINT_REG_SET_TYPE, &type, sizeof(type));
Abhijeet Dharmapurikar2464e902016-04-19 20:06:46 -0700681
682 if (flow_type & IRQ_TYPE_EDGE_BOTH)
683 irq_set_handler_locked(d, handle_edge_irq);
684 else
685 irq_set_handler_locked(d, handle_level_irq);
686
Josh Cartwright67b563f2014-02-12 13:44:25 -0600687 return 0;
688}
689
Courtney Cavin60be4232015-07-30 10:53:54 -0700690static int qpnpint_get_irqchip_state(struct irq_data *d,
691 enum irqchip_irq_state which,
692 bool *state)
693{
David Collins370a4fa2016-07-21 16:58:29 -0700694 u8 irq = HWIRQ_IRQ(d->hwirq);
Courtney Cavin60be4232015-07-30 10:53:54 -0700695 u8 status = 0;
696
697 if (which != IRQCHIP_STATE_LINE_LEVEL)
698 return -EINVAL;
699
700 qpnpint_spmi_read(d, QPNPINT_REG_RT_STS, &status, 1);
701 *state = !!(status & BIT(irq));
702
703 return 0;
704}
705
Josh Cartwright67b563f2014-02-12 13:44:25 -0600706static struct irq_chip pmic_arb_irqchip = {
707 .name = "pmic_arb",
Josh Cartwright67b563f2014-02-12 13:44:25 -0600708 .irq_ack = qpnpint_irq_ack,
709 .irq_mask = qpnpint_irq_mask,
710 .irq_unmask = qpnpint_irq_unmask,
711 .irq_set_type = qpnpint_irq_set_type,
Courtney Cavin60be4232015-07-30 10:53:54 -0700712 .irq_get_irqchip_state = qpnpint_get_irqchip_state,
Josh Cartwright67b563f2014-02-12 13:44:25 -0600713 .flags = IRQCHIP_MASK_ON_SUSPEND
714 | IRQCHIP_SKIP_SET_WAKE,
715};
716
Subbaraman Narayanamurthyf115a0e2017-01-30 15:26:24 -0800717static void qpnpint_irq_domain_activate(struct irq_domain *domain,
718 struct irq_data *d)
719{
720 u8 irq = HWIRQ_IRQ(d->hwirq);
721 u8 buf;
722
723 buf = BIT(irq);
724 qpnpint_spmi_write(d, QPNPINT_REG_EN_CLR, &buf, 1);
725 qpnpint_spmi_write(d, QPNPINT_REG_LATCHED_CLR, &buf, 1);
726}
727
Josh Cartwright67b563f2014-02-12 13:44:25 -0600728static int qpnpint_irq_domain_dt_translate(struct irq_domain *d,
729 struct device_node *controller,
730 const u32 *intspec,
731 unsigned int intsize,
732 unsigned long *out_hwirq,
733 unsigned int *out_type)
734{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800735 struct spmi_pmic_arb *pa = d->host_data;
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800736 int rc;
David Collins370a4fa2016-07-21 16:58:29 -0700737 u16 apid;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600738
739 dev_dbg(&pa->spmic->dev,
740 "intspec[0] 0x%1x intspec[1] 0x%02x intspec[2] 0x%02x\n",
741 intspec[0], intspec[1], intspec[2]);
742
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100743 if (irq_domain_get_of_node(d) != controller)
Josh Cartwright67b563f2014-02-12 13:44:25 -0600744 return -EINVAL;
745 if (intsize != 4)
746 return -EINVAL;
747 if (intspec[0] > 0xF || intspec[1] > 0xFF || intspec[2] > 0x7)
748 return -EINVAL;
749
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800750 rc = pa->ver_ops->ppid_to_apid(pa, intspec[0],
751 (intspec[1] << 8), &apid);
752 if (rc < 0) {
753 dev_err(&pa->spmic->dev,
David Collinsb2d9a402016-07-21 14:42:47 -0700754 "failed to xlate sid = 0x%x, periph = 0x%x, irq = %u rc = %d\n",
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800755 intspec[0], intspec[1], intspec[2], rc);
756 return rc;
757 }
Josh Cartwright67b563f2014-02-12 13:44:25 -0600758
David Collinsb2d9a402016-07-21 14:42:47 -0700759 if (pa->apid_data[apid].irq_owner != pa->ee) {
760 dev_err(&pa->spmic->dev, "failed to xlate sid = 0x%x, periph = 0x%x, irq = %u: ee=%u but owner=%u\n",
761 intspec[0], intspec[1], intspec[2], pa->ee,
762 pa->apid_data[apid].irq_owner);
763 return -ENODEV;
764 }
765
Josh Cartwright67b563f2014-02-12 13:44:25 -0600766 /* Keep track of {max,min}_apid for bounding search during interrupt */
767 if (apid > pa->max_apid)
768 pa->max_apid = apid;
769 if (apid < pa->min_apid)
770 pa->min_apid = apid;
771
David Collins370a4fa2016-07-21 16:58:29 -0700772 *out_hwirq = HWIRQ(intspec[0], intspec[1], intspec[2], apid);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600773 *out_type = intspec[3] & IRQ_TYPE_SENSE_MASK;
774
775 dev_dbg(&pa->spmic->dev, "out_hwirq = %lu\n", *out_hwirq);
776
777 return 0;
778}
779
780static int qpnpint_irq_domain_map(struct irq_domain *d,
781 unsigned int virq,
782 irq_hw_number_t hwirq)
783{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800784 struct spmi_pmic_arb *pa = d->host_data;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600785
786 dev_dbg(&pa->spmic->dev, "virq = %u, hwirq = %lu\n", virq, hwirq);
787
788 irq_set_chip_and_handler(virq, &pmic_arb_irqchip, handle_level_irq);
789 irq_set_chip_data(virq, d->host_data);
790 irq_set_noprobe(virq);
791 return 0;
792}
793
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800794static int
David Collins370a4fa2016-07-21 16:58:29 -0700795pmic_arb_ppid_to_apid_v1(struct spmi_pmic_arb *pa, u8 sid, u16 addr, u16 *apid)
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800796{
797 u16 ppid = sid << 8 | ((addr >> 8) & 0xFF);
798 u32 *mapping_table = pa->mapping_table;
799 int index = 0, i;
800 u16 apid_valid;
801 u32 data;
802
803 apid_valid = pa->ppid_to_apid[ppid];
804 if (apid_valid & PMIC_ARB_CHAN_VALID) {
805 *apid = (apid_valid & ~PMIC_ARB_CHAN_VALID);
806 return 0;
807 }
808
809 for (i = 0; i < SPMI_MAPPING_TABLE_TREE_DEPTH; ++i) {
810 if (!test_and_set_bit(index, pa->mapping_table_valid))
811 mapping_table[index] = readl_relaxed(pa->cnfg +
812 SPMI_MAPPING_TABLE_REG(index));
813
814 data = mapping_table[index];
815
816 if (ppid & BIT(SPMI_MAPPING_BIT_INDEX(data))) {
817 if (SPMI_MAPPING_BIT_IS_1_FLAG(data)) {
818 index = SPMI_MAPPING_BIT_IS_1_RESULT(data);
819 } else {
820 *apid = SPMI_MAPPING_BIT_IS_1_RESULT(data);
821 pa->ppid_to_apid[ppid]
822 = *apid | PMIC_ARB_CHAN_VALID;
Abhijeet Dharmapurikar8f8ec812016-01-08 12:54:36 -0800823 pa->apid_data[*apid].ppid = ppid;
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800824 return 0;
825 }
826 } else {
827 if (SPMI_MAPPING_BIT_IS_0_FLAG(data)) {
828 index = SPMI_MAPPING_BIT_IS_0_RESULT(data);
829 } else {
830 *apid = SPMI_MAPPING_BIT_IS_0_RESULT(data);
831 pa->ppid_to_apid[ppid]
832 = *apid | PMIC_ARB_CHAN_VALID;
Abhijeet Dharmapurikar8f8ec812016-01-08 12:54:36 -0800833 pa->apid_data[*apid].ppid = ppid;
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800834 return 0;
835 }
836 }
837 }
838
839 return -ENODEV;
840}
841
842static int
Abhijeet Dharmapurikar78888862016-07-05 17:54:47 -0700843pmic_arb_mode_v1_v3(struct spmi_pmic_arb *pa, u8 sid, u16 addr, mode_t *mode)
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800844{
845 *mode = 0600;
846 return 0;
847}
848
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600849/* v1 offset per ee */
Stephen Boyd987a9f12015-11-17 16:13:55 -0800850static int
David Collinsb2d9a402016-07-21 14:42:47 -0700851pmic_arb_offset_v1(struct spmi_pmic_arb *pa, u8 sid, u16 addr,
852 enum pmic_arb_channel ch_type, u32 *offset)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600853{
Stephen Boyd987a9f12015-11-17 16:13:55 -0800854 *offset = 0x800 + 0x80 * pa->channel;
855 return 0;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600856}
857
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800858static u16 pmic_arb_find_apid(struct spmi_pmic_arb *pa, u16 ppid)
Stephen Boyd987a9f12015-11-17 16:13:55 -0800859{
860 u32 regval, offset;
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800861 u16 apid;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800862 u16 id;
863
864 /*
865 * PMIC_ARB_REG_CHNL is a table in HW mapping channel to ppid.
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800866 * ppid_to_apid is an in-memory invert of that table.
Stephen Boyd987a9f12015-11-17 16:13:55 -0800867 */
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800868 for (apid = pa->last_apid; apid < pa->max_periph; apid++) {
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800869 regval = readl_relaxed(pa->cnfg +
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800870 SPMI_OWNERSHIP_TABLE_REG(apid));
David Collinsb2d9a402016-07-21 14:42:47 -0700871 pa->apid_data[apid].irq_owner
872 = SPMI_OWNERSHIP_PERIPH2OWNER(regval);
873 pa->apid_data[apid].write_owner = pa->apid_data[apid].irq_owner;
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800874
David Collinsb2d9a402016-07-21 14:42:47 -0700875 offset = pa->ver_ops->channel_map_offset(apid);
Stephen Boyd987a9f12015-11-17 16:13:55 -0800876 if (offset >= pa->core_size)
877 break;
878
879 regval = readl_relaxed(pa->core + offset);
880 if (!regval)
881 continue;
882
883 id = (regval >> 8) & PMIC_ARB_PPID_MASK;
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800884 pa->ppid_to_apid[id] = apid | PMIC_ARB_CHAN_VALID;
Abhijeet Dharmapurikar8f8ec812016-01-08 12:54:36 -0800885 pa->apid_data[apid].ppid = id;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800886 if (id == ppid) {
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800887 apid |= PMIC_ARB_CHAN_VALID;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800888 break;
889 }
890 }
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800891 pa->last_apid = apid & ~PMIC_ARB_CHAN_VALID;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800892
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800893 return apid;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800894}
895
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800896static int
David Collins370a4fa2016-07-21 16:58:29 -0700897pmic_arb_ppid_to_apid_v2(struct spmi_pmic_arb *pa, u8 sid, u16 addr, u16 *apid)
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800898{
899 u16 ppid = (sid << 8) | (addr >> 8);
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800900 u16 apid_valid;
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800901
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800902 apid_valid = pa->ppid_to_apid[ppid];
903 if (!(apid_valid & PMIC_ARB_CHAN_VALID))
904 apid_valid = pmic_arb_find_apid(pa, ppid);
905 if (!(apid_valid & PMIC_ARB_CHAN_VALID))
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800906 return -ENODEV;
907
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800908 *apid = (apid_valid & ~PMIC_ARB_CHAN_VALID);
909 return 0;
910}
911
David Collinsb2d9a402016-07-21 14:42:47 -0700912static int pmic_arb_read_apid_map_v5(struct spmi_pmic_arb *pa)
913{
914 u32 regval, offset;
915 u16 apid, prev_apid, ppid;
916 bool valid, is_irq_owner;
917
918 /*
919 * PMIC_ARB_REG_CHNL is a table in HW mapping APID (channel) to PPID.
920 * ppid_to_apid is an in-memory invert of that table. In order to allow
921 * multiple EE's to write to a single PPID in arbiter version 5, there
922 * is more than one APID mapped to each PPID. The owner field for each
923 * of these mappings specifies the EE which is allowed to write to the
924 * APID. The owner of the last (highest) APID for a given PPID will
925 * receive interrupts from the PPID.
926 */
927 for (apid = 0; apid < pa->max_periph; apid++) {
928 offset = pa->ver_ops->channel_map_offset(apid);
929 if (offset >= pa->core_size)
930 break;
931
932 regval = readl_relaxed(pa->core + offset);
933 if (!regval)
934 continue;
935 ppid = (regval >> 8) & PMIC_ARB_PPID_MASK;
936 is_irq_owner = PMIC_ARB_CHAN_IS_IRQ_OWNER(regval);
937
938 regval = readl_relaxed(pa->cnfg +
939 SPMI_OWNERSHIP_TABLE_REG(apid));
940 pa->apid_data[apid].write_owner
941 = SPMI_OWNERSHIP_PERIPH2OWNER(regval);
942
943 pa->apid_data[apid].irq_owner = is_irq_owner ?
944 pa->apid_data[apid].write_owner : INVALID_EE;
945
946 valid = pa->ppid_to_apid[ppid] & PMIC_ARB_CHAN_VALID;
947 prev_apid = pa->ppid_to_apid[ppid] & ~PMIC_ARB_CHAN_VALID;
948
949 if (valid && is_irq_owner &&
950 pa->apid_data[prev_apid].write_owner == pa->ee) {
951 /*
952 * Duplicate PPID mapping after the one for this EE;
953 * override the irq owner
954 */
955 pa->apid_data[prev_apid].irq_owner
956 = pa->apid_data[apid].irq_owner;
957 } else if (!valid || is_irq_owner) {
958 /* First PPID mapping or duplicate for another EE */
959 pa->ppid_to_apid[ppid] = apid | PMIC_ARB_CHAN_VALID;
960 }
961
962 pa->apid_data[apid].ppid = ppid;
963 pa->last_apid = apid;
964 }
965
966 /* Dump the mapping table for debug purposes. */
967 dev_dbg(&pa->spmic->dev, "PPID APID Write-EE IRQ-EE\n");
968 for (ppid = 0; ppid < PMIC_ARB_MAX_PPID; ppid++) {
969 valid = pa->ppid_to_apid[ppid] & PMIC_ARB_CHAN_VALID;
970 apid = pa->ppid_to_apid[ppid] & ~PMIC_ARB_CHAN_VALID;
971
972 if (valid)
973 dev_dbg(&pa->spmic->dev, "0x%03X %3u %2u %2u\n",
974 ppid, apid, pa->apid_data[apid].write_owner,
975 pa->apid_data[apid].irq_owner);
976 }
977
978 return 0;
979}
980
981static int
982pmic_arb_ppid_to_apid_v5(struct spmi_pmic_arb *pa, u8 sid, u16 addr, u16 *apid)
983{
984 u16 ppid = (sid << 8) | (addr >> 8);
985
986 if (!(pa->ppid_to_apid[ppid] & PMIC_ARB_CHAN_VALID))
987 return -ENODEV;
988
989 *apid = pa->ppid_to_apid[ppid] & ~PMIC_ARB_CHAN_VALID;
990
991 return 0;
992}
993
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800994static int
995pmic_arb_mode_v2(struct spmi_pmic_arb *pa, u8 sid, u16 addr, mode_t *mode)
996{
David Collins370a4fa2016-07-21 16:58:29 -0700997 u16 apid;
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800998 int rc;
999
David Collinsb2d9a402016-07-21 14:42:47 -07001000 rc = pa->ver_ops->ppid_to_apid(pa, sid, addr, &apid);
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -08001001 if (rc < 0)
1002 return rc;
1003
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -08001004 *mode = 0;
1005 *mode |= 0400;
1006
David Collinsb2d9a402016-07-21 14:42:47 -07001007 if (pa->ee == pa->apid_data[apid].write_owner)
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -08001008 *mode |= 0200;
1009 return 0;
1010}
Stephen Boyd987a9f12015-11-17 16:13:55 -08001011
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -08001012/* v2 offset per ppid and per ee */
Stephen Boyd987a9f12015-11-17 16:13:55 -08001013static int
David Collinsb2d9a402016-07-21 14:42:47 -07001014pmic_arb_offset_v2(struct spmi_pmic_arb *pa, u8 sid, u16 addr,
1015 enum pmic_arb_channel ch_type, u32 *offset)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001016{
David Collins370a4fa2016-07-21 16:58:29 -07001017 u16 apid;
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -08001018 int rc;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001019
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -08001020 rc = pmic_arb_ppid_to_apid_v2(pa, sid, addr, &apid);
1021 if (rc < 0)
1022 return rc;
Stephen Boyd987a9f12015-11-17 16:13:55 -08001023
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -08001024 *offset = 0x1000 * pa->ee + 0x8000 * apid;
Stephen Boyd987a9f12015-11-17 16:13:55 -08001025 return 0;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001026}
1027
David Collinsb2d9a402016-07-21 14:42:47 -07001028/*
1029 * v5 offset per ee and per apid for observer channels and per apid for
1030 * read/write channels.
1031 */
1032static int
1033pmic_arb_offset_v5(struct spmi_pmic_arb *pa, u8 sid, u16 addr,
1034 enum pmic_arb_channel ch_type, u32 *offset)
1035{
1036 u16 apid;
1037 int rc;
1038
1039 rc = pmic_arb_ppid_to_apid_v5(pa, sid, addr, &apid);
1040 if (rc < 0)
1041 return rc;
1042
1043 *offset = (ch_type == PMIC_ARB_CHANNEL_OBS)
1044 ? 0x10000 * pa->ee + 0x80 * apid
1045 : 0x10000 * apid;
1046 return 0;
1047}
1048
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001049static u32 pmic_arb_fmt_cmd_v1(u8 opc, u8 sid, u16 addr, u8 bc)
1050{
1051 return (opc << 27) | ((sid & 0xf) << 20) | (addr << 4) | (bc & 0x7);
1052}
1053
1054static u32 pmic_arb_fmt_cmd_v2(u8 opc, u8 sid, u16 addr, u8 bc)
1055{
1056 return (opc << 27) | ((addr & 0xff) << 4) | (bc & 0x7);
1057}
1058
David Collins370a4fa2016-07-21 16:58:29 -07001059static u32 pmic_arb_owner_acc_status_v1(u8 m, u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001060{
1061 return 0x20 * m + 0x4 * n;
1062}
1063
David Collins370a4fa2016-07-21 16:58:29 -07001064static u32 pmic_arb_owner_acc_status_v2(u8 m, u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001065{
1066 return 0x100000 + 0x1000 * m + 0x4 * n;
1067}
1068
David Collins370a4fa2016-07-21 16:58:29 -07001069static u32 pmic_arb_owner_acc_status_v3(u8 m, u16 n)
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001070{
1071 return 0x200000 + 0x1000 * m + 0x4 * n;
1072}
1073
David Collinsb2d9a402016-07-21 14:42:47 -07001074static u32 pmic_arb_owner_acc_status_v5(u8 m, u16 n)
1075{
1076 return 0x10000 * m + 0x4 * n;
1077}
1078
David Collins370a4fa2016-07-21 16:58:29 -07001079static u32 pmic_arb_acc_enable_v1(u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001080{
1081 return 0x200 + 0x4 * n;
1082}
1083
David Collins370a4fa2016-07-21 16:58:29 -07001084static u32 pmic_arb_acc_enable_v2(u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001085{
1086 return 0x1000 * n;
1087}
1088
David Collinsb2d9a402016-07-21 14:42:47 -07001089static u32 pmic_arb_acc_enable_v5(u16 n)
1090{
1091 return 0x100 + 0x10000 * n;
1092}
1093
David Collins370a4fa2016-07-21 16:58:29 -07001094static u32 pmic_arb_irq_status_v1(u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001095{
1096 return 0x600 + 0x4 * n;
1097}
1098
David Collins370a4fa2016-07-21 16:58:29 -07001099static u32 pmic_arb_irq_status_v2(u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001100{
1101 return 0x4 + 0x1000 * n;
1102}
1103
David Collinsb2d9a402016-07-21 14:42:47 -07001104static u32 pmic_arb_irq_status_v5(u16 n)
1105{
1106 return 0x104 + 0x10000 * n;
1107}
1108
David Collins370a4fa2016-07-21 16:58:29 -07001109static u32 pmic_arb_irq_clear_v1(u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001110{
1111 return 0xA00 + 0x4 * n;
1112}
1113
David Collins370a4fa2016-07-21 16:58:29 -07001114static u32 pmic_arb_irq_clear_v2(u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001115{
1116 return 0x8 + 0x1000 * n;
1117}
1118
David Collinsb2d9a402016-07-21 14:42:47 -07001119static u32 pmic_arb_irq_clear_v5(u16 n)
1120{
1121 return 0x108 + 0x10000 * n;
1122}
1123
1124static u32 pmic_arb_channel_map_offset_v2(u16 n)
1125{
1126 return 0x800 + 0x4 * n;
1127}
1128
1129static u32 pmic_arb_channel_map_offset_v5(u16 n)
1130{
1131 return 0x900 + 0x4 * n;
1132}
1133
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001134static const struct pmic_arb_ver_ops pmic_arb_v1 = {
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001135 .ver_str = "v1",
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -08001136 .ppid_to_apid = pmic_arb_ppid_to_apid_v1,
Abhijeet Dharmapurikar78888862016-07-05 17:54:47 -07001137 .mode = pmic_arb_mode_v1_v3,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001138 .non_data_cmd = pmic_arb_non_data_cmd_v1,
1139 .offset = pmic_arb_offset_v1,
1140 .fmt_cmd = pmic_arb_fmt_cmd_v1,
1141 .owner_acc_status = pmic_arb_owner_acc_status_v1,
1142 .acc_enable = pmic_arb_acc_enable_v1,
1143 .irq_status = pmic_arb_irq_status_v1,
1144 .irq_clear = pmic_arb_irq_clear_v1,
David Collinsb2d9a402016-07-21 14:42:47 -07001145 .channel_map_offset = pmic_arb_channel_map_offset_v2,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001146};
1147
1148static const struct pmic_arb_ver_ops pmic_arb_v2 = {
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001149 .ver_str = "v2",
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -08001150 .ppid_to_apid = pmic_arb_ppid_to_apid_v2,
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -08001151 .mode = pmic_arb_mode_v2,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001152 .non_data_cmd = pmic_arb_non_data_cmd_v2,
1153 .offset = pmic_arb_offset_v2,
1154 .fmt_cmd = pmic_arb_fmt_cmd_v2,
1155 .owner_acc_status = pmic_arb_owner_acc_status_v2,
1156 .acc_enable = pmic_arb_acc_enable_v2,
1157 .irq_status = pmic_arb_irq_status_v2,
1158 .irq_clear = pmic_arb_irq_clear_v2,
David Collinsb2d9a402016-07-21 14:42:47 -07001159 .channel_map_offset = pmic_arb_channel_map_offset_v2,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001160};
1161
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001162static const struct pmic_arb_ver_ops pmic_arb_v3 = {
1163 .ver_str = "v3",
1164 .ppid_to_apid = pmic_arb_ppid_to_apid_v2,
Abhijeet Dharmapurikar78888862016-07-05 17:54:47 -07001165 .mode = pmic_arb_mode_v1_v3,
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001166 .non_data_cmd = pmic_arb_non_data_cmd_v2,
1167 .offset = pmic_arb_offset_v2,
1168 .fmt_cmd = pmic_arb_fmt_cmd_v2,
1169 .owner_acc_status = pmic_arb_owner_acc_status_v3,
1170 .acc_enable = pmic_arb_acc_enable_v2,
1171 .irq_status = pmic_arb_irq_status_v2,
1172 .irq_clear = pmic_arb_irq_clear_v2,
David Collinsb2d9a402016-07-21 14:42:47 -07001173 .channel_map_offset = pmic_arb_channel_map_offset_v2,
1174};
1175
1176static const struct pmic_arb_ver_ops pmic_arb_v5 = {
1177 .ver_str = "v5",
1178 .ppid_to_apid = pmic_arb_ppid_to_apid_v5,
1179 .mode = pmic_arb_mode_v2,
1180 .non_data_cmd = pmic_arb_non_data_cmd_v2,
1181 .offset = pmic_arb_offset_v5,
1182 .fmt_cmd = pmic_arb_fmt_cmd_v2,
1183 .owner_acc_status = pmic_arb_owner_acc_status_v5,
1184 .acc_enable = pmic_arb_acc_enable_v5,
1185 .irq_status = pmic_arb_irq_status_v5,
1186 .irq_clear = pmic_arb_irq_clear_v5,
1187 .channel_map_offset = pmic_arb_channel_map_offset_v5,
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001188};
1189
Josh Cartwright67b563f2014-02-12 13:44:25 -06001190static const struct irq_domain_ops pmic_arb_irq_domain_ops = {
1191 .map = qpnpint_irq_domain_map,
1192 .xlate = qpnpint_irq_domain_dt_translate,
Subbaraman Narayanamurthyf115a0e2017-01-30 15:26:24 -08001193 .activate = qpnpint_irq_domain_activate,
Josh Cartwright67b563f2014-02-12 13:44:25 -06001194};
1195
Abhijeet Dharmapurikar69dc3fc2016-11-07 15:51:24 -08001196static void spmi_pmic_arb_resume(void)
1197{
1198 if (spmi_show_resume_irq())
1199 __pmic_arb_chained_irq(the_pa, true);
1200}
1201
1202static struct syscore_ops spmi_pmic_arb_syscore_ops = {
1203 .resume = spmi_pmic_arb_resume,
1204};
1205
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001206static int spmi_pmic_arb_probe(struct platform_device *pdev)
1207{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -08001208 struct spmi_pmic_arb *pa;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001209 struct spmi_controller *ctrl;
1210 struct resource *res;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001211 void __iomem *core;
1212 u32 channel, ee, hw_ver;
Stephen Boyd987a9f12015-11-17 16:13:55 -08001213 int err;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001214
1215 ctrl = spmi_controller_alloc(&pdev->dev, sizeof(*pa));
1216 if (!ctrl)
1217 return -ENOMEM;
1218
1219 pa = spmi_controller_get_drvdata(ctrl);
Josh Cartwright67b563f2014-02-12 13:44:25 -06001220 pa->spmic = ctrl;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001221
1222 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "core");
Abhijeet Dharmapurikar57132f52016-09-13 11:10:48 -07001223 if (!res) {
1224 dev_err(&pdev->dev, "core resource not specified\n");
1225 err = -EINVAL;
1226 goto err_put_ctrl;
1227 }
1228
Stephen Boyd987a9f12015-11-17 16:13:55 -08001229 pa->core_size = resource_size(res);
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -08001230 if (pa->core_size <= 0x800) {
1231 dev_err(&pdev->dev, "core_size is smaller than 0x800. Failing Probe\n");
1232 err = -EINVAL;
1233 goto err_put_ctrl;
1234 }
1235
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001236 core = devm_ioremap_resource(&ctrl->dev, res);
1237 if (IS_ERR(core)) {
1238 err = PTR_ERR(core);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001239 goto err_put_ctrl;
1240 }
1241
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001242 hw_ver = readl_relaxed(core + PMIC_ARB_VERSION);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001243
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001244 if (hw_ver < PMIC_ARB_VERSION_V2_MIN) {
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001245 pa->ver_ops = &pmic_arb_v1;
1246 pa->wr_base = core;
1247 pa->rd_base = core;
1248 } else {
Stephen Boyd987a9f12015-11-17 16:13:55 -08001249 pa->core = core;
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001250
1251 if (hw_ver < PMIC_ARB_VERSION_V3_MIN)
1252 pa->ver_ops = &pmic_arb_v2;
David Collinsb2d9a402016-07-21 14:42:47 -07001253 else if (hw_ver < PMIC_ARB_VERSION_V5_MIN)
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001254 pa->ver_ops = &pmic_arb_v3;
David Collinsb2d9a402016-07-21 14:42:47 -07001255 else
1256 pa->ver_ops = &pmic_arb_v5;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001257
David Collinsb2d9a402016-07-21 14:42:47 -07001258 /* the apid to ppid table starts at PMIC_ARB_REG_CHNL0 */
1259 pa->max_periph
1260 = (pa->core_size - pa->ver_ops->channel_map_offset(0)) / 4;
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -08001261
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001262 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1263 "obsrvr");
1264 pa->rd_base = devm_ioremap_resource(&ctrl->dev, res);
1265 if (IS_ERR(pa->rd_base)) {
1266 err = PTR_ERR(pa->rd_base);
1267 goto err_put_ctrl;
1268 }
1269
1270 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1271 "chnls");
1272 pa->wr_base = devm_ioremap_resource(&ctrl->dev, res);
1273 if (IS_ERR(pa->wr_base)) {
1274 err = PTR_ERR(pa->wr_base);
1275 goto err_put_ctrl;
1276 }
1277
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -08001278 pa->ppid_to_apid = devm_kcalloc(&ctrl->dev,
Stephen Boyd987a9f12015-11-17 16:13:55 -08001279 PMIC_ARB_MAX_PPID,
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -08001280 sizeof(*pa->ppid_to_apid),
Stephen Boyd987a9f12015-11-17 16:13:55 -08001281 GFP_KERNEL);
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -08001282 if (!pa->ppid_to_apid) {
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001283 err = -ENOMEM;
1284 goto err_put_ctrl;
1285 }
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001286 }
1287
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001288 dev_info(&ctrl->dev, "PMIC arbiter version %s (0x%x)\n",
1289 pa->ver_ops->ver_str, hw_ver);
1290
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001291 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "intr");
1292 pa->intr = devm_ioremap_resource(&ctrl->dev, res);
1293 if (IS_ERR(pa->intr)) {
1294 err = PTR_ERR(pa->intr);
1295 goto err_put_ctrl;
1296 }
David Collinsb2d9a402016-07-21 14:42:47 -07001297 pa->acc_status = pa->intr;
1298
1299 /*
1300 * PMIC arbiter v5 groups the IRQ control registers in the same hardware
1301 * module as the read/write channels.
1302 */
1303 if (hw_ver >= PMIC_ARB_VERSION_V5_MIN)
1304 pa->intr = pa->wr_base;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001305
1306 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cnfg");
1307 pa->cnfg = devm_ioremap_resource(&ctrl->dev, res);
1308 if (IS_ERR(pa->cnfg)) {
1309 err = PTR_ERR(pa->cnfg);
1310 goto err_put_ctrl;
1311 }
1312
Josh Cartwright67b563f2014-02-12 13:44:25 -06001313 pa->irq = platform_get_irq_byname(pdev, "periph_irq");
1314 if (pa->irq < 0) {
1315 err = pa->irq;
1316 goto err_put_ctrl;
1317 }
1318
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001319 err = of_property_read_u32(pdev->dev.of_node, "qcom,channel", &channel);
1320 if (err) {
1321 dev_err(&pdev->dev, "channel unspecified.\n");
1322 goto err_put_ctrl;
1323 }
1324
1325 if (channel > 5) {
1326 dev_err(&pdev->dev, "invalid channel (%u) specified.\n",
1327 channel);
Christophe JAILLETe98cc182016-09-26 22:24:46 +02001328 err = -EINVAL;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001329 goto err_put_ctrl;
1330 }
1331
1332 pa->channel = channel;
1333
Josh Cartwright67b563f2014-02-12 13:44:25 -06001334 err = of_property_read_u32(pdev->dev.of_node, "qcom,ee", &ee);
1335 if (err) {
1336 dev_err(&pdev->dev, "EE unspecified.\n");
1337 goto err_put_ctrl;
1338 }
1339
1340 if (ee > 5) {
1341 dev_err(&pdev->dev, "invalid EE (%u) specified\n", ee);
1342 err = -EINVAL;
1343 goto err_put_ctrl;
1344 }
1345
1346 pa->ee = ee;
1347
Stephen Boyd987a9f12015-11-17 16:13:55 -08001348 pa->mapping_table = devm_kcalloc(&ctrl->dev, PMIC_ARB_MAX_PERIPHS - 1,
1349 sizeof(*pa->mapping_table), GFP_KERNEL);
1350 if (!pa->mapping_table) {
1351 err = -ENOMEM;
1352 goto err_put_ctrl;
1353 }
Josh Cartwright67b563f2014-02-12 13:44:25 -06001354
1355 /* Initialize max_apid/min_apid to the opposite bounds, during
1356 * the irq domain translation, we are sure to update these */
1357 pa->max_apid = 0;
1358 pa->min_apid = PMIC_ARB_MAX_PERIPHS - 1;
1359
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001360 platform_set_drvdata(pdev, ctrl);
1361 raw_spin_lock_init(&pa->lock);
1362
1363 ctrl->cmd = pmic_arb_cmd;
1364 ctrl->read_cmd = pmic_arb_read_cmd;
1365 ctrl->write_cmd = pmic_arb_write_cmd;
1366
David Collinsb2d9a402016-07-21 14:42:47 -07001367 if (hw_ver >= PMIC_ARB_VERSION_V5_MIN) {
1368 err = pmic_arb_read_apid_map_v5(pa);
1369 if (err) {
1370 dev_err(&pdev->dev, "could not read APID->PPID mapping table, rc= %d\n",
1371 err);
1372 goto err_put_ctrl;
1373 }
1374 }
1375
Josh Cartwright67b563f2014-02-12 13:44:25 -06001376 dev_dbg(&pdev->dev, "adding irq domain\n");
1377 pa->domain = irq_domain_add_tree(pdev->dev.of_node,
1378 &pmic_arb_irq_domain_ops, pa);
1379 if (!pa->domain) {
1380 dev_err(&pdev->dev, "unable to create irq_domain\n");
1381 err = -ENOMEM;
1382 goto err_put_ctrl;
1383 }
1384
Thomas Gleixnerfb68ba62015-07-13 20:52:24 +00001385 irq_set_chained_handler_and_data(pa->irq, pmic_arb_chained_irq, pa);
Nicholas Troast237e9142016-06-14 16:39:38 -07001386 enable_irq_wake(pa->irq);
Josh Cartwright67b563f2014-02-12 13:44:25 -06001387
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001388 err = spmi_controller_add(ctrl);
1389 if (err)
Josh Cartwright67b563f2014-02-12 13:44:25 -06001390 goto err_domain_remove;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001391
Abhijeet Dharmapurikar69dc3fc2016-11-07 15:51:24 -08001392 the_pa = pa;
1393 register_syscore_ops(&spmi_pmic_arb_syscore_ops);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001394 return 0;
1395
Josh Cartwright67b563f2014-02-12 13:44:25 -06001396err_domain_remove:
Thomas Gleixnerfb68ba62015-07-13 20:52:24 +00001397 irq_set_chained_handler_and_data(pa->irq, NULL, NULL);
Josh Cartwright67b563f2014-02-12 13:44:25 -06001398 irq_domain_remove(pa->domain);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001399err_put_ctrl:
1400 spmi_controller_put(ctrl);
1401 return err;
1402}
1403
1404static int spmi_pmic_arb_remove(struct platform_device *pdev)
1405{
1406 struct spmi_controller *ctrl = platform_get_drvdata(pdev);
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -08001407 struct spmi_pmic_arb *pa = spmi_controller_get_drvdata(ctrl);
Abhijeet Dharmapurikar69dc3fc2016-11-07 15:51:24 -08001408
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001409 spmi_controller_remove(ctrl);
Thomas Gleixnerfb68ba62015-07-13 20:52:24 +00001410 irq_set_chained_handler_and_data(pa->irq, NULL, NULL);
Abhijeet Dharmapurikar69dc3fc2016-11-07 15:51:24 -08001411 unregister_syscore_ops(&spmi_pmic_arb_syscore_ops);
1412 the_pa = NULL;
Josh Cartwright67b563f2014-02-12 13:44:25 -06001413 irq_domain_remove(pa->domain);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001414 spmi_controller_put(ctrl);
1415 return 0;
1416}
1417
1418static const struct of_device_id spmi_pmic_arb_match_table[] = {
1419 { .compatible = "qcom,spmi-pmic-arb", },
1420 {},
1421};
1422MODULE_DEVICE_TABLE(of, spmi_pmic_arb_match_table);
1423
1424static struct platform_driver spmi_pmic_arb_driver = {
1425 .probe = spmi_pmic_arb_probe,
1426 .remove = spmi_pmic_arb_remove,
1427 .driver = {
1428 .name = "spmi_pmic_arb",
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001429 .of_match_table = spmi_pmic_arb_match_table,
1430 },
1431};
Abhijeet Dharmapurikardf9bf942015-09-23 11:36:23 -07001432
1433int __init spmi_pmic_arb_init(void)
1434{
1435 return platform_driver_register(&spmi_pmic_arb_driver);
1436}
1437arch_initcall(spmi_pmic_arb_init);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001438
1439MODULE_LICENSE("GPL v2");
1440MODULE_ALIAS("platform:spmi_pmic_arb");