blob: 0f0b7ba54fe20e0f19eb1b4451686ebd9975f19d [file] [log] [blame]
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001/*
Kiran Gunda2d5f01d2018-01-02 16:14:44 +05302 * Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06003 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
Stephen Boyd987a9f12015-11-17 16:13:55 -080013#include <linux/bitmap.h>
Kenneth Heitke39ae93e2014-02-12 13:44:24 -060014#include <linux/delay.h>
15#include <linux/err.h>
16#include <linux/interrupt.h>
17#include <linux/io.h>
Josh Cartwright67b563f2014-02-12 13:44:25 -060018#include <linux/irqchip/chained_irq.h>
19#include <linux/irqdomain.h>
20#include <linux/irq.h>
Kenneth Heitke39ae93e2014-02-12 13:44:24 -060021#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/of.h>
24#include <linux/platform_device.h>
25#include <linux/slab.h>
26#include <linux/spmi.h>
27
28/* PMIC Arbiter configuration registers */
29#define PMIC_ARB_VERSION 0x0000
Gilad Avidovd0c6ae42015-03-25 11:37:32 -060030#define PMIC_ARB_VERSION_V2_MIN 0x20010000
Nicholas Troast9c10f8f2016-03-28 10:16:31 -070031#define PMIC_ARB_VERSION_V3_MIN 0x30000000
David Collinsb2d9a402016-07-21 14:42:47 -070032#define PMIC_ARB_VERSION_V5_MIN 0x50000000
Kenneth Heitke39ae93e2014-02-12 13:44:24 -060033#define PMIC_ARB_INT_EN 0x0004
34
Gilad Avidovd0c6ae42015-03-25 11:37:32 -060035/* PMIC Arbiter channel registers offsets */
36#define PMIC_ARB_CMD 0x00
37#define PMIC_ARB_CONFIG 0x04
38#define PMIC_ARB_STATUS 0x08
39#define PMIC_ARB_WDATA0 0x10
40#define PMIC_ARB_WDATA1 0x14
41#define PMIC_ARB_RDATA0 0x18
42#define PMIC_ARB_RDATA1 0x1C
Kenneth Heitke39ae93e2014-02-12 13:44:24 -060043
44/* Mapping Table */
45#define SPMI_MAPPING_TABLE_REG(N) (0x0B00 + (4 * (N)))
46#define SPMI_MAPPING_BIT_INDEX(X) (((X) >> 18) & 0xF)
47#define SPMI_MAPPING_BIT_IS_0_FLAG(X) (((X) >> 17) & 0x1)
48#define SPMI_MAPPING_BIT_IS_0_RESULT(X) (((X) >> 9) & 0xFF)
49#define SPMI_MAPPING_BIT_IS_1_FLAG(X) (((X) >> 8) & 0x1)
50#define SPMI_MAPPING_BIT_IS_1_RESULT(X) (((X) >> 0) & 0xFF)
51
Kenneth Heitke39ae93e2014-02-12 13:44:24 -060052#define SPMI_MAPPING_TABLE_TREE_DEPTH 16 /* Maximum of 16-bits */
Stephen Boyd987a9f12015-11-17 16:13:55 -080053#define PMIC_ARB_MAX_PPID BIT(12) /* PPID is 12bit */
54#define PMIC_ARB_CHAN_VALID BIT(15)
David Collinsb2d9a402016-07-21 14:42:47 -070055#define PMIC_ARB_CHAN_IS_IRQ_OWNER(reg) ((reg) & BIT(24))
56#define INVALID_EE (-1)
Kenneth Heitke39ae93e2014-02-12 13:44:24 -060057
58/* Ownership Table */
59#define SPMI_OWNERSHIP_TABLE_REG(N) (0x0700 + (4 * (N)))
60#define SPMI_OWNERSHIP_PERIPH2OWNER(X) ((X) & 0x7)
61
David Collinsee176e22017-06-20 16:33:04 -070062#define SPMI_PROTOCOL_IRQ_STATUS 0x6000
63
Kenneth Heitke39ae93e2014-02-12 13:44:24 -060064/* Channel Status fields */
65enum pmic_arb_chnl_status {
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -080066 PMIC_ARB_STATUS_DONE = BIT(0),
67 PMIC_ARB_STATUS_FAILURE = BIT(1),
68 PMIC_ARB_STATUS_DENIED = BIT(2),
69 PMIC_ARB_STATUS_DROPPED = BIT(3),
Kenneth Heitke39ae93e2014-02-12 13:44:24 -060070};
71
72/* Command register fields */
73#define PMIC_ARB_CMD_MAX_BYTE_COUNT 8
74
75/* Command Opcodes */
76enum pmic_arb_cmd_op_code {
77 PMIC_ARB_OP_EXT_WRITEL = 0,
78 PMIC_ARB_OP_EXT_READL = 1,
79 PMIC_ARB_OP_EXT_WRITE = 2,
80 PMIC_ARB_OP_RESET = 3,
81 PMIC_ARB_OP_SLEEP = 4,
82 PMIC_ARB_OP_SHUTDOWN = 5,
83 PMIC_ARB_OP_WAKEUP = 6,
84 PMIC_ARB_OP_AUTHENTICATE = 7,
85 PMIC_ARB_OP_MSTR_READ = 8,
86 PMIC_ARB_OP_MSTR_WRITE = 9,
87 PMIC_ARB_OP_EXT_READ = 13,
88 PMIC_ARB_OP_WRITE = 14,
89 PMIC_ARB_OP_READ = 15,
90 PMIC_ARB_OP_ZERO_WRITE = 16,
91};
92
David Collinsb2d9a402016-07-21 14:42:47 -070093/*
94 * PMIC arbiter version 5 uses different register offsets for read/write vs
95 * observer channels.
96 */
97enum pmic_arb_channel {
98 PMIC_ARB_CHANNEL_RW,
99 PMIC_ARB_CHANNEL_OBS,
100};
101
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600102/* Maximum number of support PMIC peripherals */
Stephen Boyd987a9f12015-11-17 16:13:55 -0800103#define PMIC_ARB_MAX_PERIPHS 512
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600104#define PMIC_ARB_TIMEOUT_US 100
105#define PMIC_ARB_MAX_TRANS_BYTES (8)
106
107#define PMIC_ARB_APID_MASK 0xFF
108#define PMIC_ARB_PPID_MASK 0xFFF
109
110/* interrupt enable bit */
111#define SPMI_PIC_ACC_ENABLE_BIT BIT(0)
112
David Collins370a4fa2016-07-21 16:58:29 -0700113#define HWIRQ(slave_id, periph_id, irq_id, apid) \
114 ((((slave_id) & 0xF) << 28) | \
115 (((periph_id) & 0xFF) << 20) | \
116 (((irq_id) & 0x7) << 16) | \
117 (((apid) & 0x1FF) << 0))
118
119#define HWIRQ_SID(hwirq) (((hwirq) >> 28) & 0xF)
120#define HWIRQ_PER(hwirq) (((hwirq) >> 20) & 0xFF)
121#define HWIRQ_IRQ(hwirq) (((hwirq) >> 16) & 0x7)
122#define HWIRQ_APID(hwirq) (((hwirq) >> 0) & 0x1FF)
123
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600124struct pmic_arb_ver_ops;
125
Abhijeet Dharmapurikar8f8ec812016-01-08 12:54:36 -0800126struct apid_data {
127 u16 ppid;
David Collinsb2d9a402016-07-21 14:42:47 -0700128 u8 write_owner;
129 u8 irq_owner;
Abhijeet Dharmapurikar8f8ec812016-01-08 12:54:36 -0800130};
131
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600132/**
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800133 * spmi_pmic_arb - SPMI PMIC Arbiter object
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600134 *
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600135 * @rd_base: on v1 "core", on v2 "observer" register base off DT.
136 * @wr_base: on v1 "core", on v2 "chnls" register base off DT.
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600137 * @intr: address of the SPMI interrupt control registers.
David Collinsb2d9a402016-07-21 14:42:47 -0700138 * @acc_status: address of SPMI ACC interrupt status registers.
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600139 * @cnfg: address of the PMIC Arbiter configuration registers.
140 * @lock: lock to synchronize accesses.
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600141 * @channel: execution environment channel to use for accesses.
Josh Cartwright67b563f2014-02-12 13:44:25 -0600142 * @irq: PMIC ARB interrupt.
143 * @ee: the current Execution Environment
144 * @min_apid: minimum APID (used for bounding IRQ search)
145 * @max_apid: maximum APID
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800146 * @max_periph: maximum number of PMIC peripherals supported by HW.
Josh Cartwright67b563f2014-02-12 13:44:25 -0600147 * @mapping_table: in-memory copy of PPID -> APID mapping table.
148 * @domain: irq domain object for PMIC IRQ domain
149 * @spmic: SPMI controller object
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600150 * @ver_ops: version dependent operations.
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800151 * @ppid_to_apid in-memory copy of PPID -> channel (APID) mapping table.
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600152 * v2 only.
David Collinsee176e22017-06-20 16:33:04 -0700153 * @ahb_bus_wa: Use AHB bus workaround to avoid write transaction
154 * corruption on some PMIC arbiter v5 platforms.
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600155 */
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800156struct spmi_pmic_arb {
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600157 void __iomem *rd_base;
158 void __iomem *wr_base;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600159 void __iomem *intr;
David Collinsb2d9a402016-07-21 14:42:47 -0700160 void __iomem *acc_status;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600161 void __iomem *cnfg;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800162 void __iomem *core;
163 resource_size_t core_size;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600164 raw_spinlock_t lock;
165 u8 channel;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600166 int irq;
167 u8 ee;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800168 u16 min_apid;
169 u16 max_apid;
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800170 u16 max_periph;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800171 u32 *mapping_table;
172 DECLARE_BITMAP(mapping_table_valid, PMIC_ARB_MAX_PERIPHS);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600173 struct irq_domain *domain;
174 struct spmi_controller *spmic;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600175 const struct pmic_arb_ver_ops *ver_ops;
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800176 u16 *ppid_to_apid;
177 u16 last_apid;
Abhijeet Dharmapurikar8f8ec812016-01-08 12:54:36 -0800178 struct apid_data apid_data[PMIC_ARB_MAX_PERIPHS];
David Collinsee176e22017-06-20 16:33:04 -0700179 bool ahb_bus_wa;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600180};
181
182/**
183 * pmic_arb_ver: version dependent functionality.
184 *
Nicholas Troast9c10f8f2016-03-28 10:16:31 -0700185 * @ver_str: version string.
186 * @ppid_to_apid: finds the apid for a given ppid.
187 * @mode: access rights to specified pmic peripheral.
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600188 * @non_data_cmd: on v1 issues an spmi non-data command.
189 * on v2 no HW support, returns -EOPNOTSUPP.
190 * @offset: on v1 offset of per-ee channel.
191 * on v2 offset of per-ee and per-ppid channel.
192 * @fmt_cmd: formats a GENI/SPMI command.
193 * @owner_acc_status: on v1 offset of PMIC_ARB_SPMI_PIC_OWNERm_ACC_STATUSn
194 * on v2 offset of SPMI_PIC_OWNERm_ACC_STATUSn.
195 * @acc_enable: on v1 offset of PMIC_ARB_SPMI_PIC_ACC_ENABLEn
196 * on v2 offset of SPMI_PIC_ACC_ENABLEn.
197 * @irq_status: on v1 offset of PMIC_ARB_SPMI_PIC_IRQ_STATUSn
198 * on v2 offset of SPMI_PIC_IRQ_STATUSn.
199 * @irq_clear: on v1 offset of PMIC_ARB_SPMI_PIC_IRQ_CLEARn
200 * on v2 offset of SPMI_PIC_IRQ_CLEARn.
David Collinsb2d9a402016-07-21 14:42:47 -0700201 * @channel_map_offset: offset of PMIC_ARB_REG_CHNLn
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600202 */
203struct pmic_arb_ver_ops {
Nicholas Troast9c10f8f2016-03-28 10:16:31 -0700204 const char *ver_str;
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800205 int (*ppid_to_apid)(struct spmi_pmic_arb *pa, u8 sid, u16 addr,
David Collins370a4fa2016-07-21 16:58:29 -0700206 u16 *apid);
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800207 int (*mode)(struct spmi_pmic_arb *dev, u8 sid, u16 addr,
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800208 mode_t *mode);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600209 /* spmi commands (read_cmd, write_cmd, cmd) functionality */
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800210 int (*offset)(struct spmi_pmic_arb *dev, u8 sid, u16 addr,
David Collinsb2d9a402016-07-21 14:42:47 -0700211 enum pmic_arb_channel ch_type, u32 *offset);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600212 u32 (*fmt_cmd)(u8 opc, u8 sid, u16 addr, u8 bc);
213 int (*non_data_cmd)(struct spmi_controller *ctrl, u8 opc, u8 sid);
214 /* Interrupts controller functionality (offset of PIC registers) */
David Collins370a4fa2016-07-21 16:58:29 -0700215 u32 (*owner_acc_status)(u8 m, u16 n);
216 u32 (*acc_enable)(u16 n);
217 u32 (*irq_status)(u16 n);
218 u32 (*irq_clear)(u16 n);
David Collinsb2d9a402016-07-21 14:42:47 -0700219 u32 (*channel_map_offset)(u16 n);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600220};
221
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800222static inline void pmic_arb_base_write(struct spmi_pmic_arb *pa,
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600223 u32 offset, u32 val)
224{
David Collinsee176e22017-06-20 16:33:04 -0700225 if (pa->ahb_bus_wa) {
226 /* AHB bus register dummy read for workaround. */
227 readl_relaxed(pa->cnfg + SPMI_PROTOCOL_IRQ_STATUS);
228 /*
229 * Ensure that the read completes before initiating the
230 * subsequent register write.
231 */
232 mb();
233 }
234
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800235 writel_relaxed(val, pa->wr_base + offset);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600236}
237
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800238static inline void pmic_arb_set_rd_cmd(struct spmi_pmic_arb *pa,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600239 u32 offset, u32 val)
240{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800241 writel_relaxed(val, pa->rd_base + offset);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600242}
243
244/**
245 * pa_read_data: reads pmic-arb's register and copy 1..4 bytes to buf
246 * @bc: byte count -1. range: 0..3
247 * @reg: register's address
248 * @buf: output parameter, length must be bc + 1
249 */
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800250static void pa_read_data(struct spmi_pmic_arb *pa, u8 *buf, u32 reg, u8 bc)
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600251{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800252 u32 data = __raw_readl(pa->rd_base + reg);
253
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600254 memcpy(buf, &data, (bc & 3) + 1);
255}
256
257/**
258 * pa_write_data: write 1..4 bytes from buf to pmic-arb's register
259 * @bc: byte-count -1. range: 0..3.
260 * @reg: register's address.
261 * @buf: buffer to write. length must be bc + 1.
262 */
263static void
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800264pa_write_data(struct spmi_pmic_arb *pa, const u8 *buf, u32 reg, u8 bc)
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600265{
266 u32 data = 0;
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800267
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600268 memcpy(&data, buf, (bc & 3) + 1);
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800269 pmic_arb_base_write(pa, reg, data);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600270}
271
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600272static int pmic_arb_wait_for_done(struct spmi_controller *ctrl,
David Collinsb2d9a402016-07-21 14:42:47 -0700273 void __iomem *base, u8 sid, u16 addr,
274 enum pmic_arb_channel ch_type)
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600275{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800276 struct spmi_pmic_arb *pa = spmi_controller_get_drvdata(ctrl);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600277 u32 status = 0;
278 u32 timeout = PMIC_ARB_TIMEOUT_US;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800279 u32 offset;
280 int rc;
281
David Collinsb2d9a402016-07-21 14:42:47 -0700282 rc = pa->ver_ops->offset(pa, sid, addr, ch_type, &offset);
Stephen Boyd987a9f12015-11-17 16:13:55 -0800283 if (rc)
284 return rc;
285
286 offset += PMIC_ARB_STATUS;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600287
288 while (timeout--) {
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600289 status = readl_relaxed(base + offset);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600290
291 if (status & PMIC_ARB_STATUS_DONE) {
292 if (status & PMIC_ARB_STATUS_DENIED) {
293 dev_err(&ctrl->dev,
294 "%s: transaction denied (0x%x)\n",
295 __func__, status);
296 return -EPERM;
297 }
298
299 if (status & PMIC_ARB_STATUS_FAILURE) {
300 dev_err(&ctrl->dev,
301 "%s: transaction failed (0x%x)\n",
302 __func__, status);
303 return -EIO;
304 }
305
306 if (status & PMIC_ARB_STATUS_DROPPED) {
307 dev_err(&ctrl->dev,
308 "%s: transaction dropped (0x%x)\n",
309 __func__, status);
310 return -EIO;
311 }
312
313 return 0;
314 }
315 udelay(1);
316 }
317
318 dev_err(&ctrl->dev,
319 "%s: timeout, status 0x%x\n",
320 __func__, status);
321 return -ETIMEDOUT;
322}
323
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600324static int
325pmic_arb_non_data_cmd_v1(struct spmi_controller *ctrl, u8 opc, u8 sid)
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600326{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800327 struct spmi_pmic_arb *pa = spmi_controller_get_drvdata(ctrl);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600328 unsigned long flags;
329 u32 cmd;
330 int rc;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800331 u32 offset;
332
David Collinsb2d9a402016-07-21 14:42:47 -0700333 rc = pa->ver_ops->offset(pa, sid, 0, PMIC_ARB_CHANNEL_RW, &offset);
Stephen Boyd987a9f12015-11-17 16:13:55 -0800334 if (rc)
335 return rc;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600336
337 cmd = ((opc | 0x40) << 27) | ((sid & 0xf) << 20);
338
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800339 raw_spin_lock_irqsave(&pa->lock, flags);
340 pmic_arb_base_write(pa, offset + PMIC_ARB_CMD, cmd);
David Collinsb2d9a402016-07-21 14:42:47 -0700341 rc = pmic_arb_wait_for_done(ctrl, pa->wr_base, sid, 0,
342 PMIC_ARB_CHANNEL_RW);
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800343 raw_spin_unlock_irqrestore(&pa->lock, flags);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600344
345 return rc;
346}
347
348static int
349pmic_arb_non_data_cmd_v2(struct spmi_controller *ctrl, u8 opc, u8 sid)
350{
351 return -EOPNOTSUPP;
352}
353
354/* Non-data command */
355static int pmic_arb_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid)
356{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800357 struct spmi_pmic_arb *pa = spmi_controller_get_drvdata(ctrl);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600358
359 dev_dbg(&ctrl->dev, "cmd op:0x%x sid:%d\n", opc, sid);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600360
361 /* Check for valid non-data command */
362 if (opc < SPMI_CMD_RESET || opc > SPMI_CMD_WAKEUP)
363 return -EINVAL;
364
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800365 return pa->ver_ops->non_data_cmd(ctrl, opc, sid);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600366}
367
368static int pmic_arb_read_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
369 u16 addr, u8 *buf, size_t len)
370{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800371 struct spmi_pmic_arb *pa = spmi_controller_get_drvdata(ctrl);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600372 unsigned long flags;
373 u8 bc = len - 1;
374 u32 cmd;
375 int rc;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800376 u32 offset;
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800377 mode_t mode;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800378
David Collinsb2d9a402016-07-21 14:42:47 -0700379 rc = pa->ver_ops->offset(pa, sid, addr, PMIC_ARB_CHANNEL_OBS, &offset);
Stephen Boyd987a9f12015-11-17 16:13:55 -0800380 if (rc)
381 return rc;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600382
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800383 rc = pa->ver_ops->mode(pa, sid, addr, &mode);
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800384 if (rc)
385 return rc;
386
387 if (!(mode & 0400)) {
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800388 dev_err(&pa->spmic->dev,
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800389 "error: impermissible read from peripheral sid:%d addr:0x%x\n",
390 sid, addr);
391 return -ENODEV;
392 }
393
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600394 if (bc >= PMIC_ARB_MAX_TRANS_BYTES) {
395 dev_err(&ctrl->dev,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600396 "pmic-arb supports 1..%d bytes per trans, but:%zu requested",
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600397 PMIC_ARB_MAX_TRANS_BYTES, len);
398 return -EINVAL;
399 }
400
401 /* Check the opcode */
402 if (opc >= 0x60 && opc <= 0x7F)
403 opc = PMIC_ARB_OP_READ;
404 else if (opc >= 0x20 && opc <= 0x2F)
405 opc = PMIC_ARB_OP_EXT_READ;
406 else if (opc >= 0x38 && opc <= 0x3F)
407 opc = PMIC_ARB_OP_EXT_READL;
408 else
409 return -EINVAL;
410
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800411 cmd = pa->ver_ops->fmt_cmd(opc, sid, addr, bc);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600412
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800413 raw_spin_lock_irqsave(&pa->lock, flags);
414 pmic_arb_set_rd_cmd(pa, offset + PMIC_ARB_CMD, cmd);
David Collinsb2d9a402016-07-21 14:42:47 -0700415 rc = pmic_arb_wait_for_done(ctrl, pa->rd_base, sid, addr,
416 PMIC_ARB_CHANNEL_OBS);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600417 if (rc)
418 goto done;
419
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800420 pa_read_data(pa, buf, offset + PMIC_ARB_RDATA0,
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600421 min_t(u8, bc, 3));
422
423 if (bc > 3)
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800424 pa_read_data(pa, buf + 4, offset + PMIC_ARB_RDATA1, bc - 4);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600425
426done:
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800427 raw_spin_unlock_irqrestore(&pa->lock, flags);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600428 return rc;
429}
430
431static int pmic_arb_write_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
432 u16 addr, const u8 *buf, size_t len)
433{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800434 struct spmi_pmic_arb *pa = spmi_controller_get_drvdata(ctrl);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600435 unsigned long flags;
436 u8 bc = len - 1;
437 u32 cmd;
438 int rc;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800439 u32 offset;
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800440 mode_t mode;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800441
David Collinsb2d9a402016-07-21 14:42:47 -0700442 rc = pa->ver_ops->offset(pa, sid, addr, PMIC_ARB_CHANNEL_RW, &offset);
Stephen Boyd987a9f12015-11-17 16:13:55 -0800443 if (rc)
444 return rc;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600445
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800446 rc = pa->ver_ops->mode(pa, sid, addr, &mode);
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800447 if (rc)
448 return rc;
449
450 if (!(mode & 0200)) {
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800451 dev_err(&pa->spmic->dev,
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800452 "error: impermissible write to peripheral sid:%d addr:0x%x\n",
453 sid, addr);
454 return -ENODEV;
455 }
456
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600457 if (bc >= PMIC_ARB_MAX_TRANS_BYTES) {
458 dev_err(&ctrl->dev,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600459 "pmic-arb supports 1..%d bytes per trans, but:%zu requested",
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600460 PMIC_ARB_MAX_TRANS_BYTES, len);
461 return -EINVAL;
462 }
463
464 /* Check the opcode */
465 if (opc >= 0x40 && opc <= 0x5F)
466 opc = PMIC_ARB_OP_WRITE;
467 else if (opc >= 0x00 && opc <= 0x0F)
468 opc = PMIC_ARB_OP_EXT_WRITE;
469 else if (opc >= 0x30 && opc <= 0x37)
470 opc = PMIC_ARB_OP_EXT_WRITEL;
Stephen Boyd9b769682015-08-28 12:31:10 -0700471 else if (opc >= 0x80)
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600472 opc = PMIC_ARB_OP_ZERO_WRITE;
473 else
474 return -EINVAL;
475
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800476 cmd = pa->ver_ops->fmt_cmd(opc, sid, addr, bc);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600477
478 /* Write data to FIFOs */
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800479 raw_spin_lock_irqsave(&pa->lock, flags);
480 pa_write_data(pa, buf, offset + PMIC_ARB_WDATA0, min_t(u8, bc, 3));
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600481 if (bc > 3)
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800482 pa_write_data(pa, buf + 4, offset + PMIC_ARB_WDATA1, bc - 4);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600483
484 /* Start the transaction */
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800485 pmic_arb_base_write(pa, offset + PMIC_ARB_CMD, cmd);
David Collinsb2d9a402016-07-21 14:42:47 -0700486 rc = pmic_arb_wait_for_done(ctrl, pa->wr_base, sid, addr,
487 PMIC_ARB_CHANNEL_RW);
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800488 raw_spin_unlock_irqrestore(&pa->lock, flags);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600489
490 return rc;
491}
492
Josh Cartwright67b563f2014-02-12 13:44:25 -0600493enum qpnpint_regs {
494 QPNPINT_REG_RT_STS = 0x10,
495 QPNPINT_REG_SET_TYPE = 0x11,
496 QPNPINT_REG_POLARITY_HIGH = 0x12,
497 QPNPINT_REG_POLARITY_LOW = 0x13,
498 QPNPINT_REG_LATCHED_CLR = 0x14,
499 QPNPINT_REG_EN_SET = 0x15,
500 QPNPINT_REG_EN_CLR = 0x16,
501 QPNPINT_REG_LATCHED_STS = 0x18,
502};
503
504struct spmi_pmic_arb_qpnpint_type {
505 u8 type; /* 1 -> edge */
506 u8 polarity_high;
507 u8 polarity_low;
508} __packed;
509
510/* Simplified accessor functions for irqchip callbacks */
511static void qpnpint_spmi_write(struct irq_data *d, u8 reg, void *buf,
512 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_write_cmd(pa->spmic, SPMI_CMD_EXT_WRITEL, 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
525static void qpnpint_spmi_read(struct irq_data *d, u8 reg, void *buf, size_t len)
526{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800527 struct spmi_pmic_arb *pa = irq_data_get_irq_chip_data(d);
David Collins370a4fa2016-07-21 16:58:29 -0700528 u8 sid = HWIRQ_SID(d->hwirq);
529 u8 per = HWIRQ_PER(d->hwirq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600530
531 if (pmic_arb_read_cmd(pa->spmic, SPMI_CMD_EXT_READL, sid,
532 (per << 8) + reg, buf, len))
533 dev_err_ratelimited(&pa->spmic->dev,
534 "failed irqchip transaction on %x\n",
535 d->irq);
536}
537
David Collins370a4fa2016-07-21 16:58:29 -0700538static void cleanup_irq(struct spmi_pmic_arb *pa, u16 apid, int id)
Abhijeet Dharmapurikarb9b87442016-01-08 10:50:19 -0800539{
Abhijeet Dharmapurikarb9b87442016-01-08 10:50:19 -0800540 u16 ppid = pa->apid_data[apid].ppid;
541 u8 sid = ppid >> 8;
542 u8 per = ppid & 0xFF;
Abhijeet Dharmapurikarb9b87442016-01-08 10:50:19 -0800543 u8 irq_mask = BIT(id);
544
Abhijeet Dharmapurikared44ac12016-04-26 18:31:39 -0700545 dev_err_ratelimited(&pa->spmic->dev,
546 "cleanup_irq apid=%d sid=0x%x per=0x%x irq=%d\n",
547 apid, sid, per, id);
Abhijeet Dharmapurikarb9b87442016-01-08 10:50:19 -0800548 writel_relaxed(irq_mask, pa->intr + pa->ver_ops->irq_clear(apid));
Abhijeet Dharmapurikarb9b87442016-01-08 10:50:19 -0800549}
550
Stephen Boyd51257b72017-08-15 10:27:00 -0700551static void periph_interrupt(struct spmi_pmic_arb *pa, u16 apid)
Josh Cartwright67b563f2014-02-12 13:44:25 -0600552{
553 unsigned int irq;
554 u32 status;
555 int id;
David Collins370a4fa2016-07-21 16:58:29 -0700556 u8 sid = (pa->apid_data[apid].ppid >> 8) & 0xF;
557 u8 per = pa->apid_data[apid].ppid & 0xFF;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600558
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600559 status = readl_relaxed(pa->intr + pa->ver_ops->irq_status(apid));
Josh Cartwright67b563f2014-02-12 13:44:25 -0600560 while (status) {
561 id = ffs(status) - 1;
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800562 status &= ~BIT(id);
David Collins370a4fa2016-07-21 16:58:29 -0700563 irq = irq_find_mapping(pa->domain, HWIRQ(sid, per, id, apid));
Abhijeet Dharmapurikarb9b87442016-01-08 10:50:19 -0800564 if (irq == 0) {
565 cleanup_irq(pa, apid, id);
566 continue;
567 }
Stephen Boyd51257b72017-08-15 10:27:00 -0700568 generic_handle_irq(irq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600569 }
570}
571
Stephen Boyd51257b72017-08-15 10:27:00 -0700572static void pmic_arb_chained_irq(struct irq_desc *desc)
Josh Cartwright67b563f2014-02-12 13:44:25 -0600573{
Stephen Boyd51257b72017-08-15 10:27:00 -0700574 struct spmi_pmic_arb *pa = irq_desc_get_handler_data(desc);
575 struct irq_chip *chip = irq_desc_get_chip(desc);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600576 int first = pa->min_apid >> 5;
577 int last = pa->max_apid >> 5;
Abhijeet Dharmapurikar5e5078b2016-04-27 20:39:46 -0700578 u32 status, enable;
579 int i, id, apid;
Ashay Jaiswal8fbf3582017-02-27 12:33:17 +0530580 /* status based dispatch */
581 bool acc_valid = false;
582 u32 irq_status = 0;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600583
Stephen Boyd51257b72017-08-15 10:27:00 -0700584 chained_irq_enter(chip, desc);
585
Josh Cartwright67b563f2014-02-12 13:44:25 -0600586 for (i = first; i <= last; ++i) {
David Collinsb2d9a402016-07-21 14:42:47 -0700587 status = readl_relaxed(pa->acc_status +
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600588 pa->ver_ops->owner_acc_status(pa->ee, i));
Ashay Jaiswal8fbf3582017-02-27 12:33:17 +0530589 if (status)
590 acc_valid = true;
591
Josh Cartwright67b563f2014-02-12 13:44:25 -0600592 while (status) {
593 id = ffs(status) - 1;
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800594 status &= ~BIT(id);
Abhijeet Dharmapurikar5e5078b2016-04-27 20:39:46 -0700595 apid = id + i * 32;
David Collinsdc817982017-05-12 14:19:20 -0700596 if (apid < pa->min_apid || apid > pa->max_apid) {
597 WARN_ONCE(true, "spurious spmi irq received for apid=%d\n",
598 apid);
599 continue;
600 }
David Collinsb2d9a402016-07-21 14:42:47 -0700601 enable = readl_relaxed(pa->intr +
Abhijeet Dharmapurikar5e5078b2016-04-27 20:39:46 -0700602 pa->ver_ops->acc_enable(apid));
603 if (enable & SPMI_PIC_ACC_ENABLE_BIT)
Stephen Boyd51257b72017-08-15 10:27:00 -0700604 periph_interrupt(pa, apid);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600605 }
606 }
Ashay Jaiswal8fbf3582017-02-27 12:33:17 +0530607
608 /* ACC_STATUS is empty but IRQ fired check IRQ_STATUS */
609 if (!acc_valid) {
610 for (i = pa->min_apid; i <= pa->max_apid; i++) {
611 /* skip if APPS is not irq owner */
612 if (pa->apid_data[i].irq_owner != pa->ee)
613 continue;
614
615 irq_status = readl_relaxed(pa->intr +
616 pa->ver_ops->irq_status(i));
617 if (irq_status) {
618 enable = readl_relaxed(pa->intr +
619 pa->ver_ops->acc_enable(i));
620 if (enable & SPMI_PIC_ACC_ENABLE_BIT) {
621 dev_dbg(&pa->spmic->dev,
622 "Dispatching IRQ for apid=%d status=%x\n",
623 i, irq_status);
Stephen Boyd51257b72017-08-15 10:27:00 -0700624 periph_interrupt(pa, i);
Ashay Jaiswal8fbf3582017-02-27 12:33:17 +0530625 }
626 }
627 }
628 }
Josh Cartwright67b563f2014-02-12 13:44:25 -0600629
630 chained_irq_exit(chip, desc);
631}
632
633static void qpnpint_irq_ack(struct irq_data *d)
634{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800635 struct spmi_pmic_arb *pa = irq_data_get_irq_chip_data(d);
David Collins370a4fa2016-07-21 16:58:29 -0700636 u8 irq = HWIRQ_IRQ(d->hwirq);
637 u16 apid = HWIRQ_APID(d->hwirq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600638 u8 data;
639
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800640 writel_relaxed(BIT(irq), pa->intr + pa->ver_ops->irq_clear(apid));
Josh Cartwright67b563f2014-02-12 13:44:25 -0600641
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800642 data = BIT(irq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600643 qpnpint_spmi_write(d, QPNPINT_REG_LATCHED_CLR, &data, 1);
644}
645
646static void qpnpint_irq_mask(struct irq_data *d)
647{
David Collins370a4fa2016-07-21 16:58:29 -0700648 u8 irq = HWIRQ_IRQ(d->hwirq);
Abhijeet Dharmapurikar8f8ec812016-01-08 12:54:36 -0800649 u8 data = BIT(irq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600650
Josh Cartwright67b563f2014-02-12 13:44:25 -0600651 qpnpint_spmi_write(d, QPNPINT_REG_EN_CLR, &data, 1);
652}
653
654static void qpnpint_irq_unmask(struct irq_data *d)
655{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800656 struct spmi_pmic_arb *pa = irq_data_get_irq_chip_data(d);
David Collins370a4fa2016-07-21 16:58:29 -0700657 u8 irq = HWIRQ_IRQ(d->hwirq);
658 u16 apid = HWIRQ_APID(d->hwirq);
David Collinsa5a32ce2013-11-05 09:31:16 -0800659 u8 buf[2];
Josh Cartwright67b563f2014-02-12 13:44:25 -0600660
Abhijeet Dharmapurikarc27d8632016-02-23 15:56:23 -0800661 writel_relaxed(SPMI_PIC_ACC_ENABLE_BIT,
662 pa->intr + pa->ver_ops->acc_enable(apid));
Josh Cartwright67b563f2014-02-12 13:44:25 -0600663
David Collinsa5a32ce2013-11-05 09:31:16 -0800664 qpnpint_spmi_read(d, QPNPINT_REG_EN_SET, &buf[0], 1);
665 if (!(buf[0] & BIT(irq))) {
666 /*
667 * Since the interrupt is currently disabled, write to both the
668 * LATCHED_CLR and EN_SET registers so that a spurious interrupt
669 * cannot be triggered when the interrupt is enabled
670 */
671 buf[0] = BIT(irq);
672 buf[1] = BIT(irq);
673 qpnpint_spmi_write(d, QPNPINT_REG_LATCHED_CLR, &buf, 2);
674 }
Josh Cartwright67b563f2014-02-12 13:44:25 -0600675}
676
Josh Cartwright67b563f2014-02-12 13:44:25 -0600677static int qpnpint_irq_set_type(struct irq_data *d, unsigned int flow_type)
678{
679 struct spmi_pmic_arb_qpnpint_type type;
David Collins370a4fa2016-07-21 16:58:29 -0700680 u8 irq = HWIRQ_IRQ(d->hwirq);
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800681 u8 bit_mask_irq = BIT(irq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600682
683 qpnpint_spmi_read(d, QPNPINT_REG_SET_TYPE, &type, sizeof(type));
684
685 if (flow_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)) {
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800686 type.type |= bit_mask_irq;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600687 if (flow_type & IRQF_TRIGGER_RISING)
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800688 type.polarity_high |= bit_mask_irq;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600689 if (flow_type & IRQF_TRIGGER_FALLING)
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800690 type.polarity_low |= bit_mask_irq;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600691 } else {
692 if ((flow_type & (IRQF_TRIGGER_HIGH)) &&
693 (flow_type & (IRQF_TRIGGER_LOW)))
694 return -EINVAL;
695
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800696 type.type &= ~bit_mask_irq; /* level trig */
Josh Cartwright67b563f2014-02-12 13:44:25 -0600697 if (flow_type & IRQF_TRIGGER_HIGH)
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800698 type.polarity_high |= bit_mask_irq;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600699 else
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800700 type.polarity_low |= bit_mask_irq;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600701 }
702
703 qpnpint_spmi_write(d, QPNPINT_REG_SET_TYPE, &type, sizeof(type));
Abhijeet Dharmapurikar2464e902016-04-19 20:06:46 -0700704
705 if (flow_type & IRQ_TYPE_EDGE_BOTH)
706 irq_set_handler_locked(d, handle_edge_irq);
707 else
708 irq_set_handler_locked(d, handle_level_irq);
709
Josh Cartwright67b563f2014-02-12 13:44:25 -0600710 return 0;
711}
712
Courtney Cavin60be4232015-07-30 10:53:54 -0700713static int qpnpint_get_irqchip_state(struct irq_data *d,
714 enum irqchip_irq_state which,
715 bool *state)
716{
David Collins370a4fa2016-07-21 16:58:29 -0700717 u8 irq = HWIRQ_IRQ(d->hwirq);
Courtney Cavin60be4232015-07-30 10:53:54 -0700718 u8 status = 0;
719
720 if (which != IRQCHIP_STATE_LINE_LEVEL)
721 return -EINVAL;
722
723 qpnpint_spmi_read(d, QPNPINT_REG_RT_STS, &status, 1);
724 *state = !!(status & BIT(irq));
725
726 return 0;
727}
728
Kiran Gunda2d5f01d2018-01-02 16:14:44 +0530729static int qpnpint_irq_request_resources(struct irq_data *d)
730{
731 struct spmi_pmic_arb *pmic_arb = irq_data_get_irq_chip_data(d);
732 u16 periph = HWIRQ_PER(d->hwirq);
733 u16 apid = HWIRQ_APID(d->hwirq);
734 u16 sid = HWIRQ_SID(d->hwirq);
735 u16 irq = HWIRQ_IRQ(d->hwirq);
736
737 if (pmic_arb->apid_data[apid].irq_owner != pmic_arb->ee) {
738 dev_err(&pmic_arb->spmic->dev, "failed to xlate sid = %#x, periph = %#x, irq = %u: ee=%u but owner=%u\n",
739 sid, periph, irq, pmic_arb->ee,
740 pmic_arb->apid_data[apid].irq_owner);
741 return -ENODEV;
742 }
743
744 return 0;
745}
746
Josh Cartwright67b563f2014-02-12 13:44:25 -0600747static struct irq_chip pmic_arb_irqchip = {
748 .name = "pmic_arb",
Josh Cartwright67b563f2014-02-12 13:44:25 -0600749 .irq_ack = qpnpint_irq_ack,
750 .irq_mask = qpnpint_irq_mask,
751 .irq_unmask = qpnpint_irq_unmask,
752 .irq_set_type = qpnpint_irq_set_type,
Courtney Cavin60be4232015-07-30 10:53:54 -0700753 .irq_get_irqchip_state = qpnpint_get_irqchip_state,
Kiran Gunda2d5f01d2018-01-02 16:14:44 +0530754 .irq_request_resources = qpnpint_irq_request_resources,
Josh Cartwright67b563f2014-02-12 13:44:25 -0600755 .flags = IRQCHIP_MASK_ON_SUSPEND
756 | IRQCHIP_SKIP_SET_WAKE,
757};
758
Subbaraman Narayanamurthyf115a0e2017-01-30 15:26:24 -0800759static void qpnpint_irq_domain_activate(struct irq_domain *domain,
760 struct irq_data *d)
761{
762 u8 irq = HWIRQ_IRQ(d->hwirq);
763 u8 buf;
764
765 buf = BIT(irq);
766 qpnpint_spmi_write(d, QPNPINT_REG_EN_CLR, &buf, 1);
767 qpnpint_spmi_write(d, QPNPINT_REG_LATCHED_CLR, &buf, 1);
768}
769
Josh Cartwright67b563f2014-02-12 13:44:25 -0600770static int qpnpint_irq_domain_dt_translate(struct irq_domain *d,
771 struct device_node *controller,
772 const u32 *intspec,
773 unsigned int intsize,
774 unsigned long *out_hwirq,
775 unsigned int *out_type)
776{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800777 struct spmi_pmic_arb *pa = d->host_data;
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800778 int rc;
David Collins370a4fa2016-07-21 16:58:29 -0700779 u16 apid;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600780
781 dev_dbg(&pa->spmic->dev,
782 "intspec[0] 0x%1x intspec[1] 0x%02x intspec[2] 0x%02x\n",
783 intspec[0], intspec[1], intspec[2]);
784
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100785 if (irq_domain_get_of_node(d) != controller)
Josh Cartwright67b563f2014-02-12 13:44:25 -0600786 return -EINVAL;
787 if (intsize != 4)
788 return -EINVAL;
789 if (intspec[0] > 0xF || intspec[1] > 0xFF || intspec[2] > 0x7)
790 return -EINVAL;
791
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800792 rc = pa->ver_ops->ppid_to_apid(pa, intspec[0],
793 (intspec[1] << 8), &apid);
794 if (rc < 0) {
795 dev_err(&pa->spmic->dev,
David Collinsb2d9a402016-07-21 14:42:47 -0700796 "failed to xlate sid = 0x%x, periph = 0x%x, irq = %u rc = %d\n",
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800797 intspec[0], intspec[1], intspec[2], rc);
798 return rc;
799 }
Josh Cartwright67b563f2014-02-12 13:44:25 -0600800
801 /* Keep track of {max,min}_apid for bounding search during interrupt */
802 if (apid > pa->max_apid)
803 pa->max_apid = apid;
804 if (apid < pa->min_apid)
805 pa->min_apid = apid;
806
David Collins370a4fa2016-07-21 16:58:29 -0700807 *out_hwirq = HWIRQ(intspec[0], intspec[1], intspec[2], apid);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600808 *out_type = intspec[3] & IRQ_TYPE_SENSE_MASK;
809
810 dev_dbg(&pa->spmic->dev, "out_hwirq = %lu\n", *out_hwirq);
811
812 return 0;
813}
814
815static int qpnpint_irq_domain_map(struct irq_domain *d,
816 unsigned int virq,
817 irq_hw_number_t hwirq)
818{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800819 struct spmi_pmic_arb *pa = d->host_data;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600820
821 dev_dbg(&pa->spmic->dev, "virq = %u, hwirq = %lu\n", virq, hwirq);
822
823 irq_set_chip_and_handler(virq, &pmic_arb_irqchip, handle_level_irq);
824 irq_set_chip_data(virq, d->host_data);
825 irq_set_noprobe(virq);
826 return 0;
827}
828
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800829static int
David Collins370a4fa2016-07-21 16:58:29 -0700830pmic_arb_ppid_to_apid_v1(struct spmi_pmic_arb *pa, u8 sid, u16 addr, u16 *apid)
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800831{
832 u16 ppid = sid << 8 | ((addr >> 8) & 0xFF);
833 u32 *mapping_table = pa->mapping_table;
834 int index = 0, i;
835 u16 apid_valid;
836 u32 data;
837
838 apid_valid = pa->ppid_to_apid[ppid];
839 if (apid_valid & PMIC_ARB_CHAN_VALID) {
840 *apid = (apid_valid & ~PMIC_ARB_CHAN_VALID);
841 return 0;
842 }
843
844 for (i = 0; i < SPMI_MAPPING_TABLE_TREE_DEPTH; ++i) {
845 if (!test_and_set_bit(index, pa->mapping_table_valid))
846 mapping_table[index] = readl_relaxed(pa->cnfg +
847 SPMI_MAPPING_TABLE_REG(index));
848
849 data = mapping_table[index];
850
851 if (ppid & BIT(SPMI_MAPPING_BIT_INDEX(data))) {
852 if (SPMI_MAPPING_BIT_IS_1_FLAG(data)) {
853 index = SPMI_MAPPING_BIT_IS_1_RESULT(data);
854 } else {
855 *apid = SPMI_MAPPING_BIT_IS_1_RESULT(data);
856 pa->ppid_to_apid[ppid]
857 = *apid | PMIC_ARB_CHAN_VALID;
Abhijeet Dharmapurikar8f8ec812016-01-08 12:54:36 -0800858 pa->apid_data[*apid].ppid = ppid;
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800859 return 0;
860 }
861 } else {
862 if (SPMI_MAPPING_BIT_IS_0_FLAG(data)) {
863 index = SPMI_MAPPING_BIT_IS_0_RESULT(data);
864 } else {
865 *apid = SPMI_MAPPING_BIT_IS_0_RESULT(data);
866 pa->ppid_to_apid[ppid]
867 = *apid | PMIC_ARB_CHAN_VALID;
Abhijeet Dharmapurikar8f8ec812016-01-08 12:54:36 -0800868 pa->apid_data[*apid].ppid = ppid;
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800869 return 0;
870 }
871 }
872 }
873
874 return -ENODEV;
875}
876
877static int
Abhijeet Dharmapurikar78888862016-07-05 17:54:47 -0700878pmic_arb_mode_v1_v3(struct spmi_pmic_arb *pa, u8 sid, u16 addr, mode_t *mode)
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800879{
880 *mode = 0600;
881 return 0;
882}
883
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600884/* v1 offset per ee */
Stephen Boyd987a9f12015-11-17 16:13:55 -0800885static int
David Collinsb2d9a402016-07-21 14:42:47 -0700886pmic_arb_offset_v1(struct spmi_pmic_arb *pa, u8 sid, u16 addr,
887 enum pmic_arb_channel ch_type, u32 *offset)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600888{
Stephen Boyd987a9f12015-11-17 16:13:55 -0800889 *offset = 0x800 + 0x80 * pa->channel;
890 return 0;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600891}
892
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800893static u16 pmic_arb_find_apid(struct spmi_pmic_arb *pa, u16 ppid)
Stephen Boyd987a9f12015-11-17 16:13:55 -0800894{
895 u32 regval, offset;
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800896 u16 apid;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800897 u16 id;
898
899 /*
900 * PMIC_ARB_REG_CHNL is a table in HW mapping channel to ppid.
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800901 * ppid_to_apid is an in-memory invert of that table.
Stephen Boyd987a9f12015-11-17 16:13:55 -0800902 */
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800903 for (apid = pa->last_apid; apid < pa->max_periph; apid++) {
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800904 regval = readl_relaxed(pa->cnfg +
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800905 SPMI_OWNERSHIP_TABLE_REG(apid));
David Collinsb2d9a402016-07-21 14:42:47 -0700906 pa->apid_data[apid].irq_owner
907 = SPMI_OWNERSHIP_PERIPH2OWNER(regval);
908 pa->apid_data[apid].write_owner = pa->apid_data[apid].irq_owner;
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800909
David Collinsb2d9a402016-07-21 14:42:47 -0700910 offset = pa->ver_ops->channel_map_offset(apid);
Stephen Boyd987a9f12015-11-17 16:13:55 -0800911 if (offset >= pa->core_size)
912 break;
913
914 regval = readl_relaxed(pa->core + offset);
915 if (!regval)
916 continue;
917
918 id = (regval >> 8) & PMIC_ARB_PPID_MASK;
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800919 pa->ppid_to_apid[id] = apid | PMIC_ARB_CHAN_VALID;
Abhijeet Dharmapurikar8f8ec812016-01-08 12:54:36 -0800920 pa->apid_data[apid].ppid = id;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800921 if (id == ppid) {
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800922 apid |= PMIC_ARB_CHAN_VALID;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800923 break;
924 }
925 }
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800926 pa->last_apid = apid & ~PMIC_ARB_CHAN_VALID;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800927
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800928 return apid;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800929}
930
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800931static int
David Collins370a4fa2016-07-21 16:58:29 -0700932pmic_arb_ppid_to_apid_v2(struct spmi_pmic_arb *pa, u8 sid, u16 addr, u16 *apid)
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800933{
934 u16 ppid = (sid << 8) | (addr >> 8);
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800935 u16 apid_valid;
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800936
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800937 apid_valid = pa->ppid_to_apid[ppid];
938 if (!(apid_valid & PMIC_ARB_CHAN_VALID))
939 apid_valid = pmic_arb_find_apid(pa, ppid);
940 if (!(apid_valid & PMIC_ARB_CHAN_VALID))
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800941 return -ENODEV;
942
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800943 *apid = (apid_valid & ~PMIC_ARB_CHAN_VALID);
944 return 0;
945}
946
David Collinsb2d9a402016-07-21 14:42:47 -0700947static int pmic_arb_read_apid_map_v5(struct spmi_pmic_arb *pa)
948{
949 u32 regval, offset;
950 u16 apid, prev_apid, ppid;
951 bool valid, is_irq_owner;
952
953 /*
954 * PMIC_ARB_REG_CHNL is a table in HW mapping APID (channel) to PPID.
955 * ppid_to_apid is an in-memory invert of that table. In order to allow
956 * multiple EE's to write to a single PPID in arbiter version 5, there
957 * is more than one APID mapped to each PPID. The owner field for each
958 * of these mappings specifies the EE which is allowed to write to the
David Collins084e2162017-11-13 18:23:09 -0800959 * APID. The owner of the last (highest) APID which has the IRQ owner
960 * bit set for a given PPID will receive interrupts from the PPID.
David Collinsb2d9a402016-07-21 14:42:47 -0700961 */
962 for (apid = 0; apid < pa->max_periph; apid++) {
963 offset = pa->ver_ops->channel_map_offset(apid);
964 if (offset >= pa->core_size)
965 break;
966
967 regval = readl_relaxed(pa->core + offset);
968 if (!regval)
969 continue;
970 ppid = (regval >> 8) & PMIC_ARB_PPID_MASK;
971 is_irq_owner = PMIC_ARB_CHAN_IS_IRQ_OWNER(regval);
972
973 regval = readl_relaxed(pa->cnfg +
974 SPMI_OWNERSHIP_TABLE_REG(apid));
975 pa->apid_data[apid].write_owner
976 = SPMI_OWNERSHIP_PERIPH2OWNER(regval);
977
978 pa->apid_data[apid].irq_owner = is_irq_owner ?
979 pa->apid_data[apid].write_owner : INVALID_EE;
980
981 valid = pa->ppid_to_apid[ppid] & PMIC_ARB_CHAN_VALID;
982 prev_apid = pa->ppid_to_apid[ppid] & ~PMIC_ARB_CHAN_VALID;
983
David Collins084e2162017-11-13 18:23:09 -0800984 if (!valid || pa->apid_data[apid].write_owner == pa->ee) {
985 /* First PPID mapping or one for this EE */
986 pa->ppid_to_apid[ppid] = apid | PMIC_ARB_CHAN_VALID;
987 } else if (valid && is_irq_owner &&
David Collinsb2d9a402016-07-21 14:42:47 -0700988 pa->apid_data[prev_apid].write_owner == pa->ee) {
989 /*
990 * Duplicate PPID mapping after the one for this EE;
991 * override the irq owner
992 */
993 pa->apid_data[prev_apid].irq_owner
994 = pa->apid_data[apid].irq_owner;
David Collinsb2d9a402016-07-21 14:42:47 -0700995 }
996
997 pa->apid_data[apid].ppid = ppid;
998 pa->last_apid = apid;
999 }
1000
1001 /* Dump the mapping table for debug purposes. */
1002 dev_dbg(&pa->spmic->dev, "PPID APID Write-EE IRQ-EE\n");
1003 for (ppid = 0; ppid < PMIC_ARB_MAX_PPID; ppid++) {
1004 valid = pa->ppid_to_apid[ppid] & PMIC_ARB_CHAN_VALID;
1005 apid = pa->ppid_to_apid[ppid] & ~PMIC_ARB_CHAN_VALID;
1006
1007 if (valid)
1008 dev_dbg(&pa->spmic->dev, "0x%03X %3u %2u %2u\n",
1009 ppid, apid, pa->apid_data[apid].write_owner,
1010 pa->apid_data[apid].irq_owner);
1011 }
1012
1013 return 0;
1014}
1015
1016static int
1017pmic_arb_ppid_to_apid_v5(struct spmi_pmic_arb *pa, u8 sid, u16 addr, u16 *apid)
1018{
1019 u16 ppid = (sid << 8) | (addr >> 8);
1020
1021 if (!(pa->ppid_to_apid[ppid] & PMIC_ARB_CHAN_VALID))
1022 return -ENODEV;
1023
1024 *apid = pa->ppid_to_apid[ppid] & ~PMIC_ARB_CHAN_VALID;
1025
1026 return 0;
1027}
1028
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -08001029static int
1030pmic_arb_mode_v2(struct spmi_pmic_arb *pa, u8 sid, u16 addr, mode_t *mode)
1031{
David Collins370a4fa2016-07-21 16:58:29 -07001032 u16 apid;
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -08001033 int rc;
1034
David Collinsb2d9a402016-07-21 14:42:47 -07001035 rc = pa->ver_ops->ppid_to_apid(pa, sid, addr, &apid);
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -08001036 if (rc < 0)
1037 return rc;
1038
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -08001039 *mode = 0;
1040 *mode |= 0400;
1041
David Collinsb2d9a402016-07-21 14:42:47 -07001042 if (pa->ee == pa->apid_data[apid].write_owner)
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -08001043 *mode |= 0200;
1044 return 0;
1045}
Stephen Boyd987a9f12015-11-17 16:13:55 -08001046
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -08001047/* v2 offset per ppid and per ee */
Stephen Boyd987a9f12015-11-17 16:13:55 -08001048static int
David Collinsb2d9a402016-07-21 14:42:47 -07001049pmic_arb_offset_v2(struct spmi_pmic_arb *pa, u8 sid, u16 addr,
1050 enum pmic_arb_channel ch_type, u32 *offset)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001051{
David Collins370a4fa2016-07-21 16:58:29 -07001052 u16 apid;
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -08001053 int rc;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001054
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -08001055 rc = pmic_arb_ppid_to_apid_v2(pa, sid, addr, &apid);
1056 if (rc < 0)
1057 return rc;
Stephen Boyd987a9f12015-11-17 16:13:55 -08001058
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -08001059 *offset = 0x1000 * pa->ee + 0x8000 * apid;
Stephen Boyd987a9f12015-11-17 16:13:55 -08001060 return 0;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001061}
1062
David Collinsb2d9a402016-07-21 14:42:47 -07001063/*
1064 * v5 offset per ee and per apid for observer channels and per apid for
1065 * read/write channels.
1066 */
1067static int
1068pmic_arb_offset_v5(struct spmi_pmic_arb *pa, u8 sid, u16 addr,
1069 enum pmic_arb_channel ch_type, u32 *offset)
1070{
1071 u16 apid;
1072 int rc;
1073
1074 rc = pmic_arb_ppid_to_apid_v5(pa, sid, addr, &apid);
1075 if (rc < 0)
1076 return rc;
1077
1078 *offset = (ch_type == PMIC_ARB_CHANNEL_OBS)
1079 ? 0x10000 * pa->ee + 0x80 * apid
1080 : 0x10000 * apid;
1081 return 0;
1082}
1083
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001084static u32 pmic_arb_fmt_cmd_v1(u8 opc, u8 sid, u16 addr, u8 bc)
1085{
1086 return (opc << 27) | ((sid & 0xf) << 20) | (addr << 4) | (bc & 0x7);
1087}
1088
1089static u32 pmic_arb_fmt_cmd_v2(u8 opc, u8 sid, u16 addr, u8 bc)
1090{
1091 return (opc << 27) | ((addr & 0xff) << 4) | (bc & 0x7);
1092}
1093
David Collins370a4fa2016-07-21 16:58:29 -07001094static u32 pmic_arb_owner_acc_status_v1(u8 m, u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001095{
1096 return 0x20 * m + 0x4 * n;
1097}
1098
David Collins370a4fa2016-07-21 16:58:29 -07001099static u32 pmic_arb_owner_acc_status_v2(u8 m, u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001100{
1101 return 0x100000 + 0x1000 * m + 0x4 * n;
1102}
1103
David Collins370a4fa2016-07-21 16:58:29 -07001104static u32 pmic_arb_owner_acc_status_v3(u8 m, u16 n)
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001105{
1106 return 0x200000 + 0x1000 * m + 0x4 * n;
1107}
1108
David Collinsb2d9a402016-07-21 14:42:47 -07001109static u32 pmic_arb_owner_acc_status_v5(u8 m, u16 n)
1110{
1111 return 0x10000 * m + 0x4 * n;
1112}
1113
David Collins370a4fa2016-07-21 16:58:29 -07001114static u32 pmic_arb_acc_enable_v1(u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001115{
1116 return 0x200 + 0x4 * n;
1117}
1118
David Collins370a4fa2016-07-21 16:58:29 -07001119static u32 pmic_arb_acc_enable_v2(u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001120{
1121 return 0x1000 * n;
1122}
1123
David Collinsb2d9a402016-07-21 14:42:47 -07001124static u32 pmic_arb_acc_enable_v5(u16 n)
1125{
1126 return 0x100 + 0x10000 * n;
1127}
1128
David Collins370a4fa2016-07-21 16:58:29 -07001129static u32 pmic_arb_irq_status_v1(u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001130{
1131 return 0x600 + 0x4 * n;
1132}
1133
David Collins370a4fa2016-07-21 16:58:29 -07001134static u32 pmic_arb_irq_status_v2(u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001135{
1136 return 0x4 + 0x1000 * n;
1137}
1138
David Collinsb2d9a402016-07-21 14:42:47 -07001139static u32 pmic_arb_irq_status_v5(u16 n)
1140{
1141 return 0x104 + 0x10000 * n;
1142}
1143
David Collins370a4fa2016-07-21 16:58:29 -07001144static u32 pmic_arb_irq_clear_v1(u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001145{
1146 return 0xA00 + 0x4 * n;
1147}
1148
David Collins370a4fa2016-07-21 16:58:29 -07001149static u32 pmic_arb_irq_clear_v2(u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001150{
1151 return 0x8 + 0x1000 * n;
1152}
1153
David Collinsb2d9a402016-07-21 14:42:47 -07001154static u32 pmic_arb_irq_clear_v5(u16 n)
1155{
1156 return 0x108 + 0x10000 * n;
1157}
1158
1159static u32 pmic_arb_channel_map_offset_v2(u16 n)
1160{
1161 return 0x800 + 0x4 * n;
1162}
1163
1164static u32 pmic_arb_channel_map_offset_v5(u16 n)
1165{
1166 return 0x900 + 0x4 * n;
1167}
1168
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001169static const struct pmic_arb_ver_ops pmic_arb_v1 = {
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001170 .ver_str = "v1",
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -08001171 .ppid_to_apid = pmic_arb_ppid_to_apid_v1,
Abhijeet Dharmapurikar78888862016-07-05 17:54:47 -07001172 .mode = pmic_arb_mode_v1_v3,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001173 .non_data_cmd = pmic_arb_non_data_cmd_v1,
1174 .offset = pmic_arb_offset_v1,
1175 .fmt_cmd = pmic_arb_fmt_cmd_v1,
1176 .owner_acc_status = pmic_arb_owner_acc_status_v1,
1177 .acc_enable = pmic_arb_acc_enable_v1,
1178 .irq_status = pmic_arb_irq_status_v1,
1179 .irq_clear = pmic_arb_irq_clear_v1,
David Collinsb2d9a402016-07-21 14:42:47 -07001180 .channel_map_offset = pmic_arb_channel_map_offset_v2,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001181};
1182
1183static const struct pmic_arb_ver_ops pmic_arb_v2 = {
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001184 .ver_str = "v2",
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -08001185 .ppid_to_apid = pmic_arb_ppid_to_apid_v2,
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -08001186 .mode = pmic_arb_mode_v2,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001187 .non_data_cmd = pmic_arb_non_data_cmd_v2,
1188 .offset = pmic_arb_offset_v2,
1189 .fmt_cmd = pmic_arb_fmt_cmd_v2,
1190 .owner_acc_status = pmic_arb_owner_acc_status_v2,
1191 .acc_enable = pmic_arb_acc_enable_v2,
1192 .irq_status = pmic_arb_irq_status_v2,
1193 .irq_clear = pmic_arb_irq_clear_v2,
David Collinsb2d9a402016-07-21 14:42:47 -07001194 .channel_map_offset = pmic_arb_channel_map_offset_v2,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001195};
1196
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001197static const struct pmic_arb_ver_ops pmic_arb_v3 = {
1198 .ver_str = "v3",
1199 .ppid_to_apid = pmic_arb_ppid_to_apid_v2,
Abhijeet Dharmapurikar78888862016-07-05 17:54:47 -07001200 .mode = pmic_arb_mode_v1_v3,
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001201 .non_data_cmd = pmic_arb_non_data_cmd_v2,
1202 .offset = pmic_arb_offset_v2,
1203 .fmt_cmd = pmic_arb_fmt_cmd_v2,
1204 .owner_acc_status = pmic_arb_owner_acc_status_v3,
1205 .acc_enable = pmic_arb_acc_enable_v2,
1206 .irq_status = pmic_arb_irq_status_v2,
1207 .irq_clear = pmic_arb_irq_clear_v2,
David Collinsb2d9a402016-07-21 14:42:47 -07001208 .channel_map_offset = pmic_arb_channel_map_offset_v2,
1209};
1210
1211static const struct pmic_arb_ver_ops pmic_arb_v5 = {
1212 .ver_str = "v5",
1213 .ppid_to_apid = pmic_arb_ppid_to_apid_v5,
1214 .mode = pmic_arb_mode_v2,
1215 .non_data_cmd = pmic_arb_non_data_cmd_v2,
1216 .offset = pmic_arb_offset_v5,
1217 .fmt_cmd = pmic_arb_fmt_cmd_v2,
1218 .owner_acc_status = pmic_arb_owner_acc_status_v5,
1219 .acc_enable = pmic_arb_acc_enable_v5,
1220 .irq_status = pmic_arb_irq_status_v5,
1221 .irq_clear = pmic_arb_irq_clear_v5,
1222 .channel_map_offset = pmic_arb_channel_map_offset_v5,
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001223};
1224
Josh Cartwright67b563f2014-02-12 13:44:25 -06001225static const struct irq_domain_ops pmic_arb_irq_domain_ops = {
1226 .map = qpnpint_irq_domain_map,
1227 .xlate = qpnpint_irq_domain_dt_translate,
Subbaraman Narayanamurthyf115a0e2017-01-30 15:26:24 -08001228 .activate = qpnpint_irq_domain_activate,
Josh Cartwright67b563f2014-02-12 13:44:25 -06001229};
1230
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001231static int spmi_pmic_arb_probe(struct platform_device *pdev)
1232{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -08001233 struct spmi_pmic_arb *pa;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001234 struct spmi_controller *ctrl;
1235 struct resource *res;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001236 void __iomem *core;
1237 u32 channel, ee, hw_ver;
Stephen Boyd987a9f12015-11-17 16:13:55 -08001238 int err;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001239
1240 ctrl = spmi_controller_alloc(&pdev->dev, sizeof(*pa));
1241 if (!ctrl)
1242 return -ENOMEM;
1243
1244 pa = spmi_controller_get_drvdata(ctrl);
Josh Cartwright67b563f2014-02-12 13:44:25 -06001245 pa->spmic = ctrl;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001246
1247 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "core");
Abhijeet Dharmapurikar57132f52016-09-13 11:10:48 -07001248 if (!res) {
1249 dev_err(&pdev->dev, "core resource not specified\n");
1250 err = -EINVAL;
1251 goto err_put_ctrl;
1252 }
1253
Stephen Boyd987a9f12015-11-17 16:13:55 -08001254 pa->core_size = resource_size(res);
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -08001255 if (pa->core_size <= 0x800) {
1256 dev_err(&pdev->dev, "core_size is smaller than 0x800. Failing Probe\n");
1257 err = -EINVAL;
1258 goto err_put_ctrl;
1259 }
1260
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001261 core = devm_ioremap_resource(&ctrl->dev, res);
1262 if (IS_ERR(core)) {
1263 err = PTR_ERR(core);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001264 goto err_put_ctrl;
1265 }
1266
Kiran Gundac6e7a0c2018-01-03 14:53:52 +05301267 pa->ppid_to_apid = devm_kcalloc(&ctrl->dev, PMIC_ARB_MAX_PPID,
1268 sizeof(*pa->ppid_to_apid), GFP_KERNEL);
1269 if (!pa->ppid_to_apid) {
1270 err = -ENOMEM;
1271 goto err_put_ctrl;
1272 }
1273
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001274 hw_ver = readl_relaxed(core + PMIC_ARB_VERSION);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001275
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001276 if (hw_ver < PMIC_ARB_VERSION_V2_MIN) {
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001277 pa->ver_ops = &pmic_arb_v1;
1278 pa->wr_base = core;
1279 pa->rd_base = core;
1280 } else {
Stephen Boyd987a9f12015-11-17 16:13:55 -08001281 pa->core = core;
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001282
1283 if (hw_ver < PMIC_ARB_VERSION_V3_MIN)
1284 pa->ver_ops = &pmic_arb_v2;
David Collinsb2d9a402016-07-21 14:42:47 -07001285 else if (hw_ver < PMIC_ARB_VERSION_V5_MIN)
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001286 pa->ver_ops = &pmic_arb_v3;
David Collinsb2d9a402016-07-21 14:42:47 -07001287 else
1288 pa->ver_ops = &pmic_arb_v5;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001289
David Collinsb2d9a402016-07-21 14:42:47 -07001290 /* the apid to ppid table starts at PMIC_ARB_REG_CHNL0 */
1291 pa->max_periph
1292 = (pa->core_size - pa->ver_ops->channel_map_offset(0)) / 4;
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -08001293
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001294 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1295 "obsrvr");
1296 pa->rd_base = devm_ioremap_resource(&ctrl->dev, res);
1297 if (IS_ERR(pa->rd_base)) {
1298 err = PTR_ERR(pa->rd_base);
1299 goto err_put_ctrl;
1300 }
1301
1302 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1303 "chnls");
1304 pa->wr_base = devm_ioremap_resource(&ctrl->dev, res);
1305 if (IS_ERR(pa->wr_base)) {
1306 err = PTR_ERR(pa->wr_base);
1307 goto err_put_ctrl;
1308 }
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001309 }
1310
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001311 dev_info(&ctrl->dev, "PMIC arbiter version %s (0x%x)\n",
1312 pa->ver_ops->ver_str, hw_ver);
1313
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001314 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "intr");
1315 pa->intr = devm_ioremap_resource(&ctrl->dev, res);
1316 if (IS_ERR(pa->intr)) {
1317 err = PTR_ERR(pa->intr);
1318 goto err_put_ctrl;
1319 }
David Collinsb2d9a402016-07-21 14:42:47 -07001320 pa->acc_status = pa->intr;
1321
1322 /*
1323 * PMIC arbiter v5 groups the IRQ control registers in the same hardware
1324 * module as the read/write channels.
1325 */
1326 if (hw_ver >= PMIC_ARB_VERSION_V5_MIN)
1327 pa->intr = pa->wr_base;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001328
1329 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cnfg");
1330 pa->cnfg = devm_ioremap_resource(&ctrl->dev, res);
1331 if (IS_ERR(pa->cnfg)) {
1332 err = PTR_ERR(pa->cnfg);
1333 goto err_put_ctrl;
1334 }
1335
Josh Cartwright67b563f2014-02-12 13:44:25 -06001336 pa->irq = platform_get_irq_byname(pdev, "periph_irq");
1337 if (pa->irq < 0) {
1338 err = pa->irq;
1339 goto err_put_ctrl;
1340 }
1341
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001342 err = of_property_read_u32(pdev->dev.of_node, "qcom,channel", &channel);
1343 if (err) {
1344 dev_err(&pdev->dev, "channel unspecified.\n");
1345 goto err_put_ctrl;
1346 }
1347
1348 if (channel > 5) {
1349 dev_err(&pdev->dev, "invalid channel (%u) specified.\n",
1350 channel);
Christophe JAILLETe98cc182016-09-26 22:24:46 +02001351 err = -EINVAL;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001352 goto err_put_ctrl;
1353 }
1354
1355 pa->channel = channel;
1356
Josh Cartwright67b563f2014-02-12 13:44:25 -06001357 err = of_property_read_u32(pdev->dev.of_node, "qcom,ee", &ee);
1358 if (err) {
1359 dev_err(&pdev->dev, "EE unspecified.\n");
1360 goto err_put_ctrl;
1361 }
1362
1363 if (ee > 5) {
1364 dev_err(&pdev->dev, "invalid EE (%u) specified\n", ee);
1365 err = -EINVAL;
1366 goto err_put_ctrl;
1367 }
1368
1369 pa->ee = ee;
1370
David Collinsee176e22017-06-20 16:33:04 -07001371 pa->ahb_bus_wa = of_property_read_bool(pdev->dev.of_node,
1372 "qcom,enable-ahb-bus-workaround");
1373
Stephen Boyd987a9f12015-11-17 16:13:55 -08001374 pa->mapping_table = devm_kcalloc(&ctrl->dev, PMIC_ARB_MAX_PERIPHS - 1,
1375 sizeof(*pa->mapping_table), GFP_KERNEL);
1376 if (!pa->mapping_table) {
1377 err = -ENOMEM;
1378 goto err_put_ctrl;
1379 }
Josh Cartwright67b563f2014-02-12 13:44:25 -06001380
1381 /* Initialize max_apid/min_apid to the opposite bounds, during
1382 * the irq domain translation, we are sure to update these */
1383 pa->max_apid = 0;
1384 pa->min_apid = PMIC_ARB_MAX_PERIPHS - 1;
1385
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001386 platform_set_drvdata(pdev, ctrl);
1387 raw_spin_lock_init(&pa->lock);
1388
1389 ctrl->cmd = pmic_arb_cmd;
1390 ctrl->read_cmd = pmic_arb_read_cmd;
1391 ctrl->write_cmd = pmic_arb_write_cmd;
1392
David Collinsb2d9a402016-07-21 14:42:47 -07001393 if (hw_ver >= PMIC_ARB_VERSION_V5_MIN) {
1394 err = pmic_arb_read_apid_map_v5(pa);
1395 if (err) {
1396 dev_err(&pdev->dev, "could not read APID->PPID mapping table, rc= %d\n",
1397 err);
1398 goto err_put_ctrl;
1399 }
1400 }
1401
Josh Cartwright67b563f2014-02-12 13:44:25 -06001402 dev_dbg(&pdev->dev, "adding irq domain\n");
1403 pa->domain = irq_domain_add_tree(pdev->dev.of_node,
1404 &pmic_arb_irq_domain_ops, pa);
1405 if (!pa->domain) {
1406 dev_err(&pdev->dev, "unable to create irq_domain\n");
1407 err = -ENOMEM;
1408 goto err_put_ctrl;
1409 }
1410
Thomas Gleixnerfb68ba62015-07-13 20:52:24 +00001411 irq_set_chained_handler_and_data(pa->irq, pmic_arb_chained_irq, pa);
Nicholas Troast237e9142016-06-14 16:39:38 -07001412 enable_irq_wake(pa->irq);
Josh Cartwright67b563f2014-02-12 13:44:25 -06001413
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001414 err = spmi_controller_add(ctrl);
1415 if (err)
Josh Cartwright67b563f2014-02-12 13:44:25 -06001416 goto err_domain_remove;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001417
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001418 return 0;
1419
Josh Cartwright67b563f2014-02-12 13:44:25 -06001420err_domain_remove:
Thomas Gleixnerfb68ba62015-07-13 20:52:24 +00001421 irq_set_chained_handler_and_data(pa->irq, NULL, NULL);
Josh Cartwright67b563f2014-02-12 13:44:25 -06001422 irq_domain_remove(pa->domain);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001423err_put_ctrl:
1424 spmi_controller_put(ctrl);
1425 return err;
1426}
1427
1428static int spmi_pmic_arb_remove(struct platform_device *pdev)
1429{
1430 struct spmi_controller *ctrl = platform_get_drvdata(pdev);
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -08001431 struct spmi_pmic_arb *pa = spmi_controller_get_drvdata(ctrl);
Abhijeet Dharmapurikar69dc3fc2016-11-07 15:51:24 -08001432
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001433 spmi_controller_remove(ctrl);
Thomas Gleixnerfb68ba62015-07-13 20:52:24 +00001434 irq_set_chained_handler_and_data(pa->irq, NULL, NULL);
Josh Cartwright67b563f2014-02-12 13:44:25 -06001435 irq_domain_remove(pa->domain);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001436 spmi_controller_put(ctrl);
1437 return 0;
1438}
1439
1440static const struct of_device_id spmi_pmic_arb_match_table[] = {
1441 { .compatible = "qcom,spmi-pmic-arb", },
1442 {},
1443};
1444MODULE_DEVICE_TABLE(of, spmi_pmic_arb_match_table);
1445
1446static struct platform_driver spmi_pmic_arb_driver = {
1447 .probe = spmi_pmic_arb_probe,
1448 .remove = spmi_pmic_arb_remove,
1449 .driver = {
1450 .name = "spmi_pmic_arb",
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001451 .of_match_table = spmi_pmic_arb_match_table,
Patrick Dalya118d1d2018-05-11 18:47:45 -07001452 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001453 },
1454};
Abhijeet Dharmapurikardf9bf942015-09-23 11:36:23 -07001455
1456int __init spmi_pmic_arb_init(void)
1457{
1458 return platform_driver_register(&spmi_pmic_arb_driver);
1459}
1460arch_initcall(spmi_pmic_arb_init);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001461
1462MODULE_LICENSE("GPL v2");
1463MODULE_ALIAS("platform:spmi_pmic_arb");