blob: 7d9a2e8fde7ac73e9820a913b8f3e2375b5b0f70 [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>
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
62/* Channel Status fields */
63enum pmic_arb_chnl_status {
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -080064 PMIC_ARB_STATUS_DONE = BIT(0),
65 PMIC_ARB_STATUS_FAILURE = BIT(1),
66 PMIC_ARB_STATUS_DENIED = BIT(2),
67 PMIC_ARB_STATUS_DROPPED = BIT(3),
Kenneth Heitke39ae93e2014-02-12 13:44:24 -060068};
69
70/* Command register fields */
71#define PMIC_ARB_CMD_MAX_BYTE_COUNT 8
72
73/* Command Opcodes */
74enum pmic_arb_cmd_op_code {
75 PMIC_ARB_OP_EXT_WRITEL = 0,
76 PMIC_ARB_OP_EXT_READL = 1,
77 PMIC_ARB_OP_EXT_WRITE = 2,
78 PMIC_ARB_OP_RESET = 3,
79 PMIC_ARB_OP_SLEEP = 4,
80 PMIC_ARB_OP_SHUTDOWN = 5,
81 PMIC_ARB_OP_WAKEUP = 6,
82 PMIC_ARB_OP_AUTHENTICATE = 7,
83 PMIC_ARB_OP_MSTR_READ = 8,
84 PMIC_ARB_OP_MSTR_WRITE = 9,
85 PMIC_ARB_OP_EXT_READ = 13,
86 PMIC_ARB_OP_WRITE = 14,
87 PMIC_ARB_OP_READ = 15,
88 PMIC_ARB_OP_ZERO_WRITE = 16,
89};
90
David Collinsb2d9a402016-07-21 14:42:47 -070091/*
92 * PMIC arbiter version 5 uses different register offsets for read/write vs
93 * observer channels.
94 */
95enum pmic_arb_channel {
96 PMIC_ARB_CHANNEL_RW,
97 PMIC_ARB_CHANNEL_OBS,
98};
99
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600100/* Maximum number of support PMIC peripherals */
Stephen Boyd987a9f12015-11-17 16:13:55 -0800101#define PMIC_ARB_MAX_PERIPHS 512
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600102#define PMIC_ARB_TIMEOUT_US 100
103#define PMIC_ARB_MAX_TRANS_BYTES (8)
104
105#define PMIC_ARB_APID_MASK 0xFF
106#define PMIC_ARB_PPID_MASK 0xFFF
107
108/* interrupt enable bit */
109#define SPMI_PIC_ACC_ENABLE_BIT BIT(0)
110
David Collins370a4fa2016-07-21 16:58:29 -0700111#define HWIRQ(slave_id, periph_id, irq_id, apid) \
112 ((((slave_id) & 0xF) << 28) | \
113 (((periph_id) & 0xFF) << 20) | \
114 (((irq_id) & 0x7) << 16) | \
115 (((apid) & 0x1FF) << 0))
116
117#define HWIRQ_SID(hwirq) (((hwirq) >> 28) & 0xF)
118#define HWIRQ_PER(hwirq) (((hwirq) >> 20) & 0xFF)
119#define HWIRQ_IRQ(hwirq) (((hwirq) >> 16) & 0x7)
120#define HWIRQ_APID(hwirq) (((hwirq) >> 0) & 0x1FF)
121
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600122struct pmic_arb_ver_ops;
123
Abhijeet Dharmapurikar8f8ec812016-01-08 12:54:36 -0800124struct apid_data {
125 u16 ppid;
David Collinsb2d9a402016-07-21 14:42:47 -0700126 u8 write_owner;
127 u8 irq_owner;
Abhijeet Dharmapurikar8f8ec812016-01-08 12:54:36 -0800128};
129
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600130/**
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800131 * spmi_pmic_arb - SPMI PMIC Arbiter object
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600132 *
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600133 * @rd_base: on v1 "core", on v2 "observer" register base off DT.
134 * @wr_base: on v1 "core", on v2 "chnls" register base off DT.
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600135 * @intr: address of the SPMI interrupt control registers.
David Collinsb2d9a402016-07-21 14:42:47 -0700136 * @acc_status: address of SPMI ACC interrupt status registers.
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600137 * @cnfg: address of the PMIC Arbiter configuration registers.
138 * @lock: lock to synchronize accesses.
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600139 * @channel: execution environment channel to use for accesses.
Josh Cartwright67b563f2014-02-12 13:44:25 -0600140 * @irq: PMIC ARB interrupt.
141 * @ee: the current Execution Environment
142 * @min_apid: minimum APID (used for bounding IRQ search)
143 * @max_apid: maximum APID
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800144 * @max_periph: maximum number of PMIC peripherals supported by HW.
Josh Cartwright67b563f2014-02-12 13:44:25 -0600145 * @mapping_table: in-memory copy of PPID -> APID mapping table.
146 * @domain: irq domain object for PMIC IRQ domain
147 * @spmic: SPMI controller object
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600148 * @ver_ops: version dependent operations.
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800149 * @ppid_to_apid in-memory copy of PPID -> channel (APID) mapping table.
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600150 * v2 only.
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600151 */
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800152struct spmi_pmic_arb {
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600153 void __iomem *rd_base;
154 void __iomem *wr_base;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600155 void __iomem *intr;
David Collinsb2d9a402016-07-21 14:42:47 -0700156 void __iomem *acc_status;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600157 void __iomem *cnfg;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800158 void __iomem *core;
159 resource_size_t core_size;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600160 raw_spinlock_t lock;
161 u8 channel;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600162 int irq;
163 u8 ee;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800164 u16 min_apid;
165 u16 max_apid;
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800166 u16 max_periph;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800167 u32 *mapping_table;
168 DECLARE_BITMAP(mapping_table_valid, PMIC_ARB_MAX_PERIPHS);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600169 struct irq_domain *domain;
170 struct spmi_controller *spmic;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600171 const struct pmic_arb_ver_ops *ver_ops;
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800172 u16 *ppid_to_apid;
173 u16 last_apid;
Abhijeet Dharmapurikar8f8ec812016-01-08 12:54:36 -0800174 struct apid_data apid_data[PMIC_ARB_MAX_PERIPHS];
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600175};
176
177/**
178 * pmic_arb_ver: version dependent functionality.
179 *
Nicholas Troast9c10f8f2016-03-28 10:16:31 -0700180 * @ver_str: version string.
181 * @ppid_to_apid: finds the apid for a given ppid.
182 * @mode: access rights to specified pmic peripheral.
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600183 * @non_data_cmd: on v1 issues an spmi non-data command.
184 * on v2 no HW support, returns -EOPNOTSUPP.
185 * @offset: on v1 offset of per-ee channel.
186 * on v2 offset of per-ee and per-ppid channel.
187 * @fmt_cmd: formats a GENI/SPMI command.
188 * @owner_acc_status: on v1 offset of PMIC_ARB_SPMI_PIC_OWNERm_ACC_STATUSn
189 * on v2 offset of SPMI_PIC_OWNERm_ACC_STATUSn.
190 * @acc_enable: on v1 offset of PMIC_ARB_SPMI_PIC_ACC_ENABLEn
191 * on v2 offset of SPMI_PIC_ACC_ENABLEn.
192 * @irq_status: on v1 offset of PMIC_ARB_SPMI_PIC_IRQ_STATUSn
193 * on v2 offset of SPMI_PIC_IRQ_STATUSn.
194 * @irq_clear: on v1 offset of PMIC_ARB_SPMI_PIC_IRQ_CLEARn
195 * on v2 offset of SPMI_PIC_IRQ_CLEARn.
David Collinsb2d9a402016-07-21 14:42:47 -0700196 * @channel_map_offset: offset of PMIC_ARB_REG_CHNLn
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600197 */
198struct pmic_arb_ver_ops {
Nicholas Troast9c10f8f2016-03-28 10:16:31 -0700199 const char *ver_str;
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800200 int (*ppid_to_apid)(struct spmi_pmic_arb *pa, u8 sid, u16 addr,
David Collins370a4fa2016-07-21 16:58:29 -0700201 u16 *apid);
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800202 int (*mode)(struct spmi_pmic_arb *dev, u8 sid, u16 addr,
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800203 mode_t *mode);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600204 /* spmi commands (read_cmd, write_cmd, cmd) functionality */
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800205 int (*offset)(struct spmi_pmic_arb *dev, u8 sid, u16 addr,
David Collinsb2d9a402016-07-21 14:42:47 -0700206 enum pmic_arb_channel ch_type, u32 *offset);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600207 u32 (*fmt_cmd)(u8 opc, u8 sid, u16 addr, u8 bc);
208 int (*non_data_cmd)(struct spmi_controller *ctrl, u8 opc, u8 sid);
209 /* Interrupts controller functionality (offset of PIC registers) */
David Collins370a4fa2016-07-21 16:58:29 -0700210 u32 (*owner_acc_status)(u8 m, u16 n);
211 u32 (*acc_enable)(u16 n);
212 u32 (*irq_status)(u16 n);
213 u32 (*irq_clear)(u16 n);
David Collinsb2d9a402016-07-21 14:42:47 -0700214 u32 (*channel_map_offset)(u16 n);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600215};
216
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800217static inline void pmic_arb_base_write(struct spmi_pmic_arb *pa,
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600218 u32 offset, u32 val)
219{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800220 writel_relaxed(val, pa->wr_base + offset);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600221}
222
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800223static inline void pmic_arb_set_rd_cmd(struct spmi_pmic_arb *pa,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600224 u32 offset, u32 val)
225{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800226 writel_relaxed(val, pa->rd_base + offset);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600227}
228
229/**
230 * pa_read_data: reads pmic-arb's register and copy 1..4 bytes to buf
231 * @bc: byte count -1. range: 0..3
232 * @reg: register's address
233 * @buf: output parameter, length must be bc + 1
234 */
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800235static void pa_read_data(struct spmi_pmic_arb *pa, u8 *buf, u32 reg, u8 bc)
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600236{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800237 u32 data = __raw_readl(pa->rd_base + reg);
238
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600239 memcpy(buf, &data, (bc & 3) + 1);
240}
241
242/**
243 * pa_write_data: write 1..4 bytes from buf to pmic-arb's register
244 * @bc: byte-count -1. range: 0..3.
245 * @reg: register's address.
246 * @buf: buffer to write. length must be bc + 1.
247 */
248static void
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800249pa_write_data(struct spmi_pmic_arb *pa, const u8 *buf, u32 reg, u8 bc)
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600250{
251 u32 data = 0;
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800252
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600253 memcpy(&data, buf, (bc & 3) + 1);
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800254 pmic_arb_base_write(pa, reg, data);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600255}
256
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600257static int pmic_arb_wait_for_done(struct spmi_controller *ctrl,
David Collinsb2d9a402016-07-21 14:42:47 -0700258 void __iomem *base, u8 sid, u16 addr,
259 enum pmic_arb_channel ch_type)
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600260{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800261 struct spmi_pmic_arb *pa = spmi_controller_get_drvdata(ctrl);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600262 u32 status = 0;
263 u32 timeout = PMIC_ARB_TIMEOUT_US;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800264 u32 offset;
265 int rc;
266
David Collinsb2d9a402016-07-21 14:42:47 -0700267 rc = pa->ver_ops->offset(pa, sid, addr, ch_type, &offset);
Stephen Boyd987a9f12015-11-17 16:13:55 -0800268 if (rc)
269 return rc;
270
271 offset += PMIC_ARB_STATUS;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600272
273 while (timeout--) {
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600274 status = readl_relaxed(base + offset);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600275
276 if (status & PMIC_ARB_STATUS_DONE) {
277 if (status & PMIC_ARB_STATUS_DENIED) {
278 dev_err(&ctrl->dev,
279 "%s: transaction denied (0x%x)\n",
280 __func__, status);
281 return -EPERM;
282 }
283
284 if (status & PMIC_ARB_STATUS_FAILURE) {
285 dev_err(&ctrl->dev,
286 "%s: transaction failed (0x%x)\n",
287 __func__, status);
288 return -EIO;
289 }
290
291 if (status & PMIC_ARB_STATUS_DROPPED) {
292 dev_err(&ctrl->dev,
293 "%s: transaction dropped (0x%x)\n",
294 __func__, status);
295 return -EIO;
296 }
297
298 return 0;
299 }
300 udelay(1);
301 }
302
303 dev_err(&ctrl->dev,
304 "%s: timeout, status 0x%x\n",
305 __func__, status);
306 return -ETIMEDOUT;
307}
308
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600309static int
310pmic_arb_non_data_cmd_v1(struct spmi_controller *ctrl, u8 opc, u8 sid)
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600311{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800312 struct spmi_pmic_arb *pa = spmi_controller_get_drvdata(ctrl);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600313 unsigned long flags;
314 u32 cmd;
315 int rc;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800316 u32 offset;
317
David Collinsb2d9a402016-07-21 14:42:47 -0700318 rc = pa->ver_ops->offset(pa, sid, 0, PMIC_ARB_CHANNEL_RW, &offset);
Stephen Boyd987a9f12015-11-17 16:13:55 -0800319 if (rc)
320 return rc;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600321
322 cmd = ((opc | 0x40) << 27) | ((sid & 0xf) << 20);
323
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800324 raw_spin_lock_irqsave(&pa->lock, flags);
325 pmic_arb_base_write(pa, offset + PMIC_ARB_CMD, cmd);
David Collinsb2d9a402016-07-21 14:42:47 -0700326 rc = pmic_arb_wait_for_done(ctrl, pa->wr_base, sid, 0,
327 PMIC_ARB_CHANNEL_RW);
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800328 raw_spin_unlock_irqrestore(&pa->lock, flags);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600329
330 return rc;
331}
332
333static int
334pmic_arb_non_data_cmd_v2(struct spmi_controller *ctrl, u8 opc, u8 sid)
335{
336 return -EOPNOTSUPP;
337}
338
339/* Non-data command */
340static int pmic_arb_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid)
341{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800342 struct spmi_pmic_arb *pa = spmi_controller_get_drvdata(ctrl);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600343
344 dev_dbg(&ctrl->dev, "cmd op:0x%x sid:%d\n", opc, sid);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600345
346 /* Check for valid non-data command */
347 if (opc < SPMI_CMD_RESET || opc > SPMI_CMD_WAKEUP)
348 return -EINVAL;
349
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800350 return pa->ver_ops->non_data_cmd(ctrl, opc, sid);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600351}
352
353static int pmic_arb_read_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
354 u16 addr, u8 *buf, size_t len)
355{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800356 struct spmi_pmic_arb *pa = spmi_controller_get_drvdata(ctrl);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600357 unsigned long flags;
358 u8 bc = len - 1;
359 u32 cmd;
360 int rc;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800361 u32 offset;
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800362 mode_t mode;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800363
David Collinsb2d9a402016-07-21 14:42:47 -0700364 rc = pa->ver_ops->offset(pa, sid, addr, PMIC_ARB_CHANNEL_OBS, &offset);
Stephen Boyd987a9f12015-11-17 16:13:55 -0800365 if (rc)
366 return rc;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600367
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800368 rc = pa->ver_ops->mode(pa, sid, addr, &mode);
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800369 if (rc)
370 return rc;
371
372 if (!(mode & 0400)) {
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800373 dev_err(&pa->spmic->dev,
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800374 "error: impermissible read from peripheral sid:%d addr:0x%x\n",
375 sid, addr);
376 return -ENODEV;
377 }
378
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600379 if (bc >= PMIC_ARB_MAX_TRANS_BYTES) {
380 dev_err(&ctrl->dev,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600381 "pmic-arb supports 1..%d bytes per trans, but:%zu requested",
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600382 PMIC_ARB_MAX_TRANS_BYTES, len);
383 return -EINVAL;
384 }
385
386 /* Check the opcode */
387 if (opc >= 0x60 && opc <= 0x7F)
388 opc = PMIC_ARB_OP_READ;
389 else if (opc >= 0x20 && opc <= 0x2F)
390 opc = PMIC_ARB_OP_EXT_READ;
391 else if (opc >= 0x38 && opc <= 0x3F)
392 opc = PMIC_ARB_OP_EXT_READL;
393 else
394 return -EINVAL;
395
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800396 cmd = pa->ver_ops->fmt_cmd(opc, sid, addr, bc);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600397
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800398 raw_spin_lock_irqsave(&pa->lock, flags);
399 pmic_arb_set_rd_cmd(pa, offset + PMIC_ARB_CMD, cmd);
David Collinsb2d9a402016-07-21 14:42:47 -0700400 rc = pmic_arb_wait_for_done(ctrl, pa->rd_base, sid, addr,
401 PMIC_ARB_CHANNEL_OBS);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600402 if (rc)
403 goto done;
404
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800405 pa_read_data(pa, buf, offset + PMIC_ARB_RDATA0,
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600406 min_t(u8, bc, 3));
407
408 if (bc > 3)
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800409 pa_read_data(pa, buf + 4, offset + PMIC_ARB_RDATA1, bc - 4);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600410
411done:
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800412 raw_spin_unlock_irqrestore(&pa->lock, flags);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600413 return rc;
414}
415
416static int pmic_arb_write_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
417 u16 addr, const u8 *buf, size_t len)
418{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800419 struct spmi_pmic_arb *pa = spmi_controller_get_drvdata(ctrl);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600420 unsigned long flags;
421 u8 bc = len - 1;
422 u32 cmd;
423 int rc;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800424 u32 offset;
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800425 mode_t mode;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800426
David Collinsb2d9a402016-07-21 14:42:47 -0700427 rc = pa->ver_ops->offset(pa, sid, addr, PMIC_ARB_CHANNEL_RW, &offset);
Stephen Boyd987a9f12015-11-17 16:13:55 -0800428 if (rc)
429 return rc;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600430
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800431 rc = pa->ver_ops->mode(pa, sid, addr, &mode);
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800432 if (rc)
433 return rc;
434
435 if (!(mode & 0200)) {
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800436 dev_err(&pa->spmic->dev,
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800437 "error: impermissible write to peripheral sid:%d addr:0x%x\n",
438 sid, addr);
439 return -ENODEV;
440 }
441
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600442 if (bc >= PMIC_ARB_MAX_TRANS_BYTES) {
443 dev_err(&ctrl->dev,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600444 "pmic-arb supports 1..%d bytes per trans, but:%zu requested",
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600445 PMIC_ARB_MAX_TRANS_BYTES, len);
446 return -EINVAL;
447 }
448
449 /* Check the opcode */
450 if (opc >= 0x40 && opc <= 0x5F)
451 opc = PMIC_ARB_OP_WRITE;
452 else if (opc >= 0x00 && opc <= 0x0F)
453 opc = PMIC_ARB_OP_EXT_WRITE;
454 else if (opc >= 0x30 && opc <= 0x37)
455 opc = PMIC_ARB_OP_EXT_WRITEL;
Stephen Boyd9b769682015-08-28 12:31:10 -0700456 else if (opc >= 0x80)
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600457 opc = PMIC_ARB_OP_ZERO_WRITE;
458 else
459 return -EINVAL;
460
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800461 cmd = pa->ver_ops->fmt_cmd(opc, sid, addr, bc);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600462
463 /* Write data to FIFOs */
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800464 raw_spin_lock_irqsave(&pa->lock, flags);
465 pa_write_data(pa, buf, offset + PMIC_ARB_WDATA0, min_t(u8, bc, 3));
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600466 if (bc > 3)
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800467 pa_write_data(pa, buf + 4, offset + PMIC_ARB_WDATA1, bc - 4);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600468
469 /* Start the transaction */
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800470 pmic_arb_base_write(pa, offset + PMIC_ARB_CMD, cmd);
David Collinsb2d9a402016-07-21 14:42:47 -0700471 rc = pmic_arb_wait_for_done(ctrl, pa->wr_base, sid, addr,
472 PMIC_ARB_CHANNEL_RW);
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800473 raw_spin_unlock_irqrestore(&pa->lock, flags);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600474
475 return rc;
476}
477
Josh Cartwright67b563f2014-02-12 13:44:25 -0600478enum qpnpint_regs {
479 QPNPINT_REG_RT_STS = 0x10,
480 QPNPINT_REG_SET_TYPE = 0x11,
481 QPNPINT_REG_POLARITY_HIGH = 0x12,
482 QPNPINT_REG_POLARITY_LOW = 0x13,
483 QPNPINT_REG_LATCHED_CLR = 0x14,
484 QPNPINT_REG_EN_SET = 0x15,
485 QPNPINT_REG_EN_CLR = 0x16,
486 QPNPINT_REG_LATCHED_STS = 0x18,
487};
488
489struct spmi_pmic_arb_qpnpint_type {
490 u8 type; /* 1 -> edge */
491 u8 polarity_high;
492 u8 polarity_low;
493} __packed;
494
495/* Simplified accessor functions for irqchip callbacks */
496static void qpnpint_spmi_write(struct irq_data *d, u8 reg, void *buf,
497 size_t len)
498{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800499 struct spmi_pmic_arb *pa = irq_data_get_irq_chip_data(d);
David Collins370a4fa2016-07-21 16:58:29 -0700500 u8 sid = HWIRQ_SID(d->hwirq);
501 u8 per = HWIRQ_PER(d->hwirq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600502
503 if (pmic_arb_write_cmd(pa->spmic, SPMI_CMD_EXT_WRITEL, sid,
504 (per << 8) + reg, buf, len))
505 dev_err_ratelimited(&pa->spmic->dev,
506 "failed irqchip transaction on %x\n",
507 d->irq);
508}
509
510static void qpnpint_spmi_read(struct irq_data *d, u8 reg, void *buf, size_t len)
511{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800512 struct spmi_pmic_arb *pa = irq_data_get_irq_chip_data(d);
David Collins370a4fa2016-07-21 16:58:29 -0700513 u8 sid = HWIRQ_SID(d->hwirq);
514 u8 per = HWIRQ_PER(d->hwirq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600515
516 if (pmic_arb_read_cmd(pa->spmic, SPMI_CMD_EXT_READL, sid,
517 (per << 8) + reg, buf, len))
518 dev_err_ratelimited(&pa->spmic->dev,
519 "failed irqchip transaction on %x\n",
520 d->irq);
521}
522
David Collins370a4fa2016-07-21 16:58:29 -0700523static void cleanup_irq(struct spmi_pmic_arb *pa, u16 apid, int id)
Abhijeet Dharmapurikarb9b87442016-01-08 10:50:19 -0800524{
Abhijeet Dharmapurikarb9b87442016-01-08 10:50:19 -0800525 u16 ppid = pa->apid_data[apid].ppid;
526 u8 sid = ppid >> 8;
527 u8 per = ppid & 0xFF;
Abhijeet Dharmapurikarb9b87442016-01-08 10:50:19 -0800528 u8 irq_mask = BIT(id);
529
Abhijeet Dharmapurikared44ac12016-04-26 18:31:39 -0700530 dev_err_ratelimited(&pa->spmic->dev,
531 "cleanup_irq apid=%d sid=0x%x per=0x%x irq=%d\n",
532 apid, sid, per, id);
Abhijeet Dharmapurikarb9b87442016-01-08 10:50:19 -0800533 writel_relaxed(irq_mask, pa->intr + pa->ver_ops->irq_clear(apid));
Abhijeet Dharmapurikarb9b87442016-01-08 10:50:19 -0800534}
535
Stephen Boyd51257b72017-08-15 10:27:00 -0700536static void periph_interrupt(struct spmi_pmic_arb *pa, u16 apid)
Josh Cartwright67b563f2014-02-12 13:44:25 -0600537{
538 unsigned int irq;
539 u32 status;
540 int id;
David Collins370a4fa2016-07-21 16:58:29 -0700541 u8 sid = (pa->apid_data[apid].ppid >> 8) & 0xF;
542 u8 per = pa->apid_data[apid].ppid & 0xFF;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600543
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600544 status = readl_relaxed(pa->intr + pa->ver_ops->irq_status(apid));
Josh Cartwright67b563f2014-02-12 13:44:25 -0600545 while (status) {
546 id = ffs(status) - 1;
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800547 status &= ~BIT(id);
David Collins370a4fa2016-07-21 16:58:29 -0700548 irq = irq_find_mapping(pa->domain, HWIRQ(sid, per, id, apid));
Abhijeet Dharmapurikarb9b87442016-01-08 10:50:19 -0800549 if (irq == 0) {
550 cleanup_irq(pa, apid, id);
551 continue;
552 }
Stephen Boyd51257b72017-08-15 10:27:00 -0700553 generic_handle_irq(irq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600554 }
555}
556
Stephen Boyd51257b72017-08-15 10:27:00 -0700557static void pmic_arb_chained_irq(struct irq_desc *desc)
Josh Cartwright67b563f2014-02-12 13:44:25 -0600558{
Stephen Boyd51257b72017-08-15 10:27:00 -0700559 struct spmi_pmic_arb *pa = irq_desc_get_handler_data(desc);
560 struct irq_chip *chip = irq_desc_get_chip(desc);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600561 int first = pa->min_apid >> 5;
562 int last = pa->max_apid >> 5;
Abhijeet Dharmapurikar5e5078b2016-04-27 20:39:46 -0700563 u32 status, enable;
564 int i, id, apid;
Ashay Jaiswal8fbf3582017-02-27 12:33:17 +0530565 /* status based dispatch */
566 bool acc_valid = false;
567 u32 irq_status = 0;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600568
Stephen Boyd51257b72017-08-15 10:27:00 -0700569 chained_irq_enter(chip, desc);
570
Josh Cartwright67b563f2014-02-12 13:44:25 -0600571 for (i = first; i <= last; ++i) {
David Collinsb2d9a402016-07-21 14:42:47 -0700572 status = readl_relaxed(pa->acc_status +
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600573 pa->ver_ops->owner_acc_status(pa->ee, i));
Ashay Jaiswal8fbf3582017-02-27 12:33:17 +0530574 if (status)
575 acc_valid = true;
576
Josh Cartwright67b563f2014-02-12 13:44:25 -0600577 while (status) {
578 id = ffs(status) - 1;
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800579 status &= ~BIT(id);
Abhijeet Dharmapurikar5e5078b2016-04-27 20:39:46 -0700580 apid = id + i * 32;
David Collinsdc817982017-05-12 14:19:20 -0700581 if (apid < pa->min_apid || apid > pa->max_apid) {
582 WARN_ONCE(true, "spurious spmi irq received for apid=%d\n",
583 apid);
584 continue;
585 }
David Collinsb2d9a402016-07-21 14:42:47 -0700586 enable = readl_relaxed(pa->intr +
Abhijeet Dharmapurikar5e5078b2016-04-27 20:39:46 -0700587 pa->ver_ops->acc_enable(apid));
588 if (enable & SPMI_PIC_ACC_ENABLE_BIT)
Stephen Boyd51257b72017-08-15 10:27:00 -0700589 periph_interrupt(pa, apid);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600590 }
591 }
Ashay Jaiswal8fbf3582017-02-27 12:33:17 +0530592
593 /* ACC_STATUS is empty but IRQ fired check IRQ_STATUS */
594 if (!acc_valid) {
595 for (i = pa->min_apid; i <= pa->max_apid; i++) {
596 /* skip if APPS is not irq owner */
597 if (pa->apid_data[i].irq_owner != pa->ee)
598 continue;
599
600 irq_status = readl_relaxed(pa->intr +
601 pa->ver_ops->irq_status(i));
602 if (irq_status) {
603 enable = readl_relaxed(pa->intr +
604 pa->ver_ops->acc_enable(i));
605 if (enable & SPMI_PIC_ACC_ENABLE_BIT) {
606 dev_dbg(&pa->spmic->dev,
607 "Dispatching IRQ for apid=%d status=%x\n",
608 i, irq_status);
Stephen Boyd51257b72017-08-15 10:27:00 -0700609 periph_interrupt(pa, i);
Ashay Jaiswal8fbf3582017-02-27 12:33:17 +0530610 }
611 }
612 }
613 }
Josh Cartwright67b563f2014-02-12 13:44:25 -0600614
615 chained_irq_exit(chip, desc);
616}
617
618static void qpnpint_irq_ack(struct irq_data *d)
619{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800620 struct spmi_pmic_arb *pa = irq_data_get_irq_chip_data(d);
David Collins370a4fa2016-07-21 16:58:29 -0700621 u8 irq = HWIRQ_IRQ(d->hwirq);
622 u16 apid = HWIRQ_APID(d->hwirq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600623 u8 data;
624
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800625 writel_relaxed(BIT(irq), pa->intr + pa->ver_ops->irq_clear(apid));
Josh Cartwright67b563f2014-02-12 13:44:25 -0600626
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800627 data = BIT(irq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600628 qpnpint_spmi_write(d, QPNPINT_REG_LATCHED_CLR, &data, 1);
629}
630
631static void qpnpint_irq_mask(struct irq_data *d)
632{
David Collins370a4fa2016-07-21 16:58:29 -0700633 u8 irq = HWIRQ_IRQ(d->hwirq);
Abhijeet Dharmapurikar8f8ec812016-01-08 12:54:36 -0800634 u8 data = BIT(irq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600635
Josh Cartwright67b563f2014-02-12 13:44:25 -0600636 qpnpint_spmi_write(d, QPNPINT_REG_EN_CLR, &data, 1);
637}
638
639static void qpnpint_irq_unmask(struct irq_data *d)
640{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800641 struct spmi_pmic_arb *pa = irq_data_get_irq_chip_data(d);
David Collins370a4fa2016-07-21 16:58:29 -0700642 u8 irq = HWIRQ_IRQ(d->hwirq);
643 u16 apid = HWIRQ_APID(d->hwirq);
David Collinsa5a32ce2013-11-05 09:31:16 -0800644 u8 buf[2];
Josh Cartwright67b563f2014-02-12 13:44:25 -0600645
Abhijeet Dharmapurikarc27d8632016-02-23 15:56:23 -0800646 writel_relaxed(SPMI_PIC_ACC_ENABLE_BIT,
647 pa->intr + pa->ver_ops->acc_enable(apid));
Josh Cartwright67b563f2014-02-12 13:44:25 -0600648
David Collinsa5a32ce2013-11-05 09:31:16 -0800649 qpnpint_spmi_read(d, QPNPINT_REG_EN_SET, &buf[0], 1);
650 if (!(buf[0] & BIT(irq))) {
651 /*
652 * Since the interrupt is currently disabled, write to both the
653 * LATCHED_CLR and EN_SET registers so that a spurious interrupt
654 * cannot be triggered when the interrupt is enabled
655 */
656 buf[0] = BIT(irq);
657 buf[1] = BIT(irq);
658 qpnpint_spmi_write(d, QPNPINT_REG_LATCHED_CLR, &buf, 2);
659 }
Josh Cartwright67b563f2014-02-12 13:44:25 -0600660}
661
Josh Cartwright67b563f2014-02-12 13:44:25 -0600662static int qpnpint_irq_set_type(struct irq_data *d, unsigned int flow_type)
663{
664 struct spmi_pmic_arb_qpnpint_type type;
David Collins370a4fa2016-07-21 16:58:29 -0700665 u8 irq = HWIRQ_IRQ(d->hwirq);
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800666 u8 bit_mask_irq = BIT(irq);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600667
668 qpnpint_spmi_read(d, QPNPINT_REG_SET_TYPE, &type, sizeof(type));
669
670 if (flow_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)) {
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800671 type.type |= bit_mask_irq;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600672 if (flow_type & IRQF_TRIGGER_RISING)
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800673 type.polarity_high |= bit_mask_irq;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600674 if (flow_type & IRQF_TRIGGER_FALLING)
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800675 type.polarity_low |= bit_mask_irq;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600676 } else {
677 if ((flow_type & (IRQF_TRIGGER_HIGH)) &&
678 (flow_type & (IRQF_TRIGGER_LOW)))
679 return -EINVAL;
680
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800681 type.type &= ~bit_mask_irq; /* level trig */
Josh Cartwright67b563f2014-02-12 13:44:25 -0600682 if (flow_type & IRQF_TRIGGER_HIGH)
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800683 type.polarity_high |= bit_mask_irq;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600684 else
Abhijeet Dharmapurikar469f2c32016-01-05 16:52:38 -0800685 type.polarity_low |= bit_mask_irq;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600686 }
687
688 qpnpint_spmi_write(d, QPNPINT_REG_SET_TYPE, &type, sizeof(type));
Abhijeet Dharmapurikar2464e902016-04-19 20:06:46 -0700689
690 if (flow_type & IRQ_TYPE_EDGE_BOTH)
691 irq_set_handler_locked(d, handle_edge_irq);
692 else
693 irq_set_handler_locked(d, handle_level_irq);
694
Josh Cartwright67b563f2014-02-12 13:44:25 -0600695 return 0;
696}
697
Courtney Cavin60be4232015-07-30 10:53:54 -0700698static int qpnpint_get_irqchip_state(struct irq_data *d,
699 enum irqchip_irq_state which,
700 bool *state)
701{
David Collins370a4fa2016-07-21 16:58:29 -0700702 u8 irq = HWIRQ_IRQ(d->hwirq);
Courtney Cavin60be4232015-07-30 10:53:54 -0700703 u8 status = 0;
704
705 if (which != IRQCHIP_STATE_LINE_LEVEL)
706 return -EINVAL;
707
708 qpnpint_spmi_read(d, QPNPINT_REG_RT_STS, &status, 1);
709 *state = !!(status & BIT(irq));
710
711 return 0;
712}
713
Josh Cartwright67b563f2014-02-12 13:44:25 -0600714static struct irq_chip pmic_arb_irqchip = {
715 .name = "pmic_arb",
Josh Cartwright67b563f2014-02-12 13:44:25 -0600716 .irq_ack = qpnpint_irq_ack,
717 .irq_mask = qpnpint_irq_mask,
718 .irq_unmask = qpnpint_irq_unmask,
719 .irq_set_type = qpnpint_irq_set_type,
Courtney Cavin60be4232015-07-30 10:53:54 -0700720 .irq_get_irqchip_state = qpnpint_get_irqchip_state,
Josh Cartwright67b563f2014-02-12 13:44:25 -0600721 .flags = IRQCHIP_MASK_ON_SUSPEND
722 | IRQCHIP_SKIP_SET_WAKE,
723};
724
Subbaraman Narayanamurthyf115a0e2017-01-30 15:26:24 -0800725static void qpnpint_irq_domain_activate(struct irq_domain *domain,
726 struct irq_data *d)
727{
728 u8 irq = HWIRQ_IRQ(d->hwirq);
729 u8 buf;
730
731 buf = BIT(irq);
732 qpnpint_spmi_write(d, QPNPINT_REG_EN_CLR, &buf, 1);
733 qpnpint_spmi_write(d, QPNPINT_REG_LATCHED_CLR, &buf, 1);
734}
735
Josh Cartwright67b563f2014-02-12 13:44:25 -0600736static int qpnpint_irq_domain_dt_translate(struct irq_domain *d,
737 struct device_node *controller,
738 const u32 *intspec,
739 unsigned int intsize,
740 unsigned long *out_hwirq,
741 unsigned int *out_type)
742{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800743 struct spmi_pmic_arb *pa = d->host_data;
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800744 int rc;
David Collins370a4fa2016-07-21 16:58:29 -0700745 u16 apid;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600746
747 dev_dbg(&pa->spmic->dev,
748 "intspec[0] 0x%1x intspec[1] 0x%02x intspec[2] 0x%02x\n",
749 intspec[0], intspec[1], intspec[2]);
750
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100751 if (irq_domain_get_of_node(d) != controller)
Josh Cartwright67b563f2014-02-12 13:44:25 -0600752 return -EINVAL;
753 if (intsize != 4)
754 return -EINVAL;
755 if (intspec[0] > 0xF || intspec[1] > 0xFF || intspec[2] > 0x7)
756 return -EINVAL;
757
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800758 rc = pa->ver_ops->ppid_to_apid(pa, intspec[0],
759 (intspec[1] << 8), &apid);
760 if (rc < 0) {
761 dev_err(&pa->spmic->dev,
David Collinsb2d9a402016-07-21 14:42:47 -0700762 "failed to xlate sid = 0x%x, periph = 0x%x, irq = %u rc = %d\n",
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800763 intspec[0], intspec[1], intspec[2], rc);
764 return rc;
765 }
Josh Cartwright67b563f2014-02-12 13:44:25 -0600766
David Collinsb2d9a402016-07-21 14:42:47 -0700767 if (pa->apid_data[apid].irq_owner != pa->ee) {
768 dev_err(&pa->spmic->dev, "failed to xlate sid = 0x%x, periph = 0x%x, irq = %u: ee=%u but owner=%u\n",
769 intspec[0], intspec[1], intspec[2], pa->ee,
770 pa->apid_data[apid].irq_owner);
771 return -ENODEV;
772 }
773
Josh Cartwright67b563f2014-02-12 13:44:25 -0600774 /* Keep track of {max,min}_apid for bounding search during interrupt */
775 if (apid > pa->max_apid)
776 pa->max_apid = apid;
777 if (apid < pa->min_apid)
778 pa->min_apid = apid;
779
David Collins370a4fa2016-07-21 16:58:29 -0700780 *out_hwirq = HWIRQ(intspec[0], intspec[1], intspec[2], apid);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600781 *out_type = intspec[3] & IRQ_TYPE_SENSE_MASK;
782
783 dev_dbg(&pa->spmic->dev, "out_hwirq = %lu\n", *out_hwirq);
784
785 return 0;
786}
787
788static int qpnpint_irq_domain_map(struct irq_domain *d,
789 unsigned int virq,
790 irq_hw_number_t hwirq)
791{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -0800792 struct spmi_pmic_arb *pa = d->host_data;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600793
794 dev_dbg(&pa->spmic->dev, "virq = %u, hwirq = %lu\n", virq, hwirq);
795
796 irq_set_chip_and_handler(virq, &pmic_arb_irqchip, handle_level_irq);
797 irq_set_chip_data(virq, d->host_data);
798 irq_set_noprobe(virq);
799 return 0;
800}
801
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800802static int
David Collins370a4fa2016-07-21 16:58:29 -0700803pmic_arb_ppid_to_apid_v1(struct spmi_pmic_arb *pa, u8 sid, u16 addr, u16 *apid)
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800804{
805 u16 ppid = sid << 8 | ((addr >> 8) & 0xFF);
806 u32 *mapping_table = pa->mapping_table;
807 int index = 0, i;
808 u16 apid_valid;
809 u32 data;
810
811 apid_valid = pa->ppid_to_apid[ppid];
812 if (apid_valid & PMIC_ARB_CHAN_VALID) {
813 *apid = (apid_valid & ~PMIC_ARB_CHAN_VALID);
814 return 0;
815 }
816
817 for (i = 0; i < SPMI_MAPPING_TABLE_TREE_DEPTH; ++i) {
818 if (!test_and_set_bit(index, pa->mapping_table_valid))
819 mapping_table[index] = readl_relaxed(pa->cnfg +
820 SPMI_MAPPING_TABLE_REG(index));
821
822 data = mapping_table[index];
823
824 if (ppid & BIT(SPMI_MAPPING_BIT_INDEX(data))) {
825 if (SPMI_MAPPING_BIT_IS_1_FLAG(data)) {
826 index = SPMI_MAPPING_BIT_IS_1_RESULT(data);
827 } else {
828 *apid = SPMI_MAPPING_BIT_IS_1_RESULT(data);
829 pa->ppid_to_apid[ppid]
830 = *apid | PMIC_ARB_CHAN_VALID;
Abhijeet Dharmapurikar8f8ec812016-01-08 12:54:36 -0800831 pa->apid_data[*apid].ppid = ppid;
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800832 return 0;
833 }
834 } else {
835 if (SPMI_MAPPING_BIT_IS_0_FLAG(data)) {
836 index = SPMI_MAPPING_BIT_IS_0_RESULT(data);
837 } else {
838 *apid = SPMI_MAPPING_BIT_IS_0_RESULT(data);
839 pa->ppid_to_apid[ppid]
840 = *apid | PMIC_ARB_CHAN_VALID;
Abhijeet Dharmapurikar8f8ec812016-01-08 12:54:36 -0800841 pa->apid_data[*apid].ppid = ppid;
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800842 return 0;
843 }
844 }
845 }
846
847 return -ENODEV;
848}
849
850static int
Abhijeet Dharmapurikar78888862016-07-05 17:54:47 -0700851pmic_arb_mode_v1_v3(struct spmi_pmic_arb *pa, u8 sid, u16 addr, mode_t *mode)
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800852{
853 *mode = 0600;
854 return 0;
855}
856
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600857/* v1 offset per ee */
Stephen Boyd987a9f12015-11-17 16:13:55 -0800858static int
David Collinsb2d9a402016-07-21 14:42:47 -0700859pmic_arb_offset_v1(struct spmi_pmic_arb *pa, u8 sid, u16 addr,
860 enum pmic_arb_channel ch_type, u32 *offset)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600861{
Stephen Boyd987a9f12015-11-17 16:13:55 -0800862 *offset = 0x800 + 0x80 * pa->channel;
863 return 0;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600864}
865
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800866static u16 pmic_arb_find_apid(struct spmi_pmic_arb *pa, u16 ppid)
Stephen Boyd987a9f12015-11-17 16:13:55 -0800867{
868 u32 regval, offset;
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800869 u16 apid;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800870 u16 id;
871
872 /*
873 * PMIC_ARB_REG_CHNL is a table in HW mapping channel to ppid.
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800874 * ppid_to_apid is an in-memory invert of that table.
Stephen Boyd987a9f12015-11-17 16:13:55 -0800875 */
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800876 for (apid = pa->last_apid; apid < pa->max_periph; apid++) {
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800877 regval = readl_relaxed(pa->cnfg +
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800878 SPMI_OWNERSHIP_TABLE_REG(apid));
David Collinsb2d9a402016-07-21 14:42:47 -0700879 pa->apid_data[apid].irq_owner
880 = SPMI_OWNERSHIP_PERIPH2OWNER(regval);
881 pa->apid_data[apid].write_owner = pa->apid_data[apid].irq_owner;
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800882
David Collinsb2d9a402016-07-21 14:42:47 -0700883 offset = pa->ver_ops->channel_map_offset(apid);
Stephen Boyd987a9f12015-11-17 16:13:55 -0800884 if (offset >= pa->core_size)
885 break;
886
887 regval = readl_relaxed(pa->core + offset);
888 if (!regval)
889 continue;
890
891 id = (regval >> 8) & PMIC_ARB_PPID_MASK;
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800892 pa->ppid_to_apid[id] = apid | PMIC_ARB_CHAN_VALID;
Abhijeet Dharmapurikar8f8ec812016-01-08 12:54:36 -0800893 pa->apid_data[apid].ppid = id;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800894 if (id == ppid) {
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800895 apid |= PMIC_ARB_CHAN_VALID;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800896 break;
897 }
898 }
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800899 pa->last_apid = apid & ~PMIC_ARB_CHAN_VALID;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800900
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -0800901 return apid;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800902}
903
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800904static int
David Collins370a4fa2016-07-21 16:58:29 -0700905pmic_arb_ppid_to_apid_v2(struct spmi_pmic_arb *pa, u8 sid, u16 addr, u16 *apid)
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800906{
907 u16 ppid = (sid << 8) | (addr >> 8);
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800908 u16 apid_valid;
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800909
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800910 apid_valid = pa->ppid_to_apid[ppid];
911 if (!(apid_valid & PMIC_ARB_CHAN_VALID))
912 apid_valid = pmic_arb_find_apid(pa, ppid);
913 if (!(apid_valid & PMIC_ARB_CHAN_VALID))
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -0800914 return -ENODEV;
915
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -0800916 *apid = (apid_valid & ~PMIC_ARB_CHAN_VALID);
917 return 0;
918}
919
David Collinsb2d9a402016-07-21 14:42:47 -0700920static int pmic_arb_read_apid_map_v5(struct spmi_pmic_arb *pa)
921{
922 u32 regval, offset;
923 u16 apid, prev_apid, ppid;
924 bool valid, is_irq_owner;
925
926 /*
927 * PMIC_ARB_REG_CHNL is a table in HW mapping APID (channel) to PPID.
928 * ppid_to_apid is an in-memory invert of that table. In order to allow
929 * multiple EE's to write to a single PPID in arbiter version 5, there
930 * is more than one APID mapped to each PPID. The owner field for each
931 * of these mappings specifies the EE which is allowed to write to the
932 * APID. The owner of the last (highest) APID for a given PPID will
933 * receive interrupts from the PPID.
934 */
935 for (apid = 0; apid < pa->max_periph; apid++) {
936 offset = pa->ver_ops->channel_map_offset(apid);
937 if (offset >= pa->core_size)
938 break;
939
940 regval = readl_relaxed(pa->core + offset);
941 if (!regval)
942 continue;
943 ppid = (regval >> 8) & PMIC_ARB_PPID_MASK;
944 is_irq_owner = PMIC_ARB_CHAN_IS_IRQ_OWNER(regval);
945
946 regval = readl_relaxed(pa->cnfg +
947 SPMI_OWNERSHIP_TABLE_REG(apid));
948 pa->apid_data[apid].write_owner
949 = SPMI_OWNERSHIP_PERIPH2OWNER(regval);
950
951 pa->apid_data[apid].irq_owner = is_irq_owner ?
952 pa->apid_data[apid].write_owner : INVALID_EE;
953
954 valid = pa->ppid_to_apid[ppid] & PMIC_ARB_CHAN_VALID;
955 prev_apid = pa->ppid_to_apid[ppid] & ~PMIC_ARB_CHAN_VALID;
956
957 if (valid && is_irq_owner &&
958 pa->apid_data[prev_apid].write_owner == pa->ee) {
959 /*
960 * Duplicate PPID mapping after the one for this EE;
961 * override the irq owner
962 */
963 pa->apid_data[prev_apid].irq_owner
964 = pa->apid_data[apid].irq_owner;
965 } else if (!valid || is_irq_owner) {
966 /* First PPID mapping or duplicate for another EE */
967 pa->ppid_to_apid[ppid] = apid | PMIC_ARB_CHAN_VALID;
968 }
969
970 pa->apid_data[apid].ppid = ppid;
971 pa->last_apid = apid;
972 }
973
974 /* Dump the mapping table for debug purposes. */
975 dev_dbg(&pa->spmic->dev, "PPID APID Write-EE IRQ-EE\n");
976 for (ppid = 0; ppid < PMIC_ARB_MAX_PPID; ppid++) {
977 valid = pa->ppid_to_apid[ppid] & PMIC_ARB_CHAN_VALID;
978 apid = pa->ppid_to_apid[ppid] & ~PMIC_ARB_CHAN_VALID;
979
980 if (valid)
981 dev_dbg(&pa->spmic->dev, "0x%03X %3u %2u %2u\n",
982 ppid, apid, pa->apid_data[apid].write_owner,
983 pa->apid_data[apid].irq_owner);
984 }
985
986 return 0;
987}
988
989static int
990pmic_arb_ppid_to_apid_v5(struct spmi_pmic_arb *pa, u8 sid, u16 addr, u16 *apid)
991{
992 u16 ppid = (sid << 8) | (addr >> 8);
993
994 if (!(pa->ppid_to_apid[ppid] & PMIC_ARB_CHAN_VALID))
995 return -ENODEV;
996
997 *apid = pa->ppid_to_apid[ppid] & ~PMIC_ARB_CHAN_VALID;
998
999 return 0;
1000}
1001
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -08001002static int
1003pmic_arb_mode_v2(struct spmi_pmic_arb *pa, u8 sid, u16 addr, mode_t *mode)
1004{
David Collins370a4fa2016-07-21 16:58:29 -07001005 u16 apid;
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -08001006 int rc;
1007
David Collinsb2d9a402016-07-21 14:42:47 -07001008 rc = pa->ver_ops->ppid_to_apid(pa, sid, addr, &apid);
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -08001009 if (rc < 0)
1010 return rc;
1011
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -08001012 *mode = 0;
1013 *mode |= 0400;
1014
David Collinsb2d9a402016-07-21 14:42:47 -07001015 if (pa->ee == pa->apid_data[apid].write_owner)
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -08001016 *mode |= 0200;
1017 return 0;
1018}
Stephen Boyd987a9f12015-11-17 16:13:55 -08001019
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -08001020/* v2 offset per ppid and per ee */
Stephen Boyd987a9f12015-11-17 16:13:55 -08001021static int
David Collinsb2d9a402016-07-21 14:42:47 -07001022pmic_arb_offset_v2(struct spmi_pmic_arb *pa, u8 sid, u16 addr,
1023 enum pmic_arb_channel ch_type, u32 *offset)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001024{
David Collins370a4fa2016-07-21 16:58:29 -07001025 u16 apid;
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -08001026 int rc;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001027
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -08001028 rc = pmic_arb_ppid_to_apid_v2(pa, sid, addr, &apid);
1029 if (rc < 0)
1030 return rc;
Stephen Boyd987a9f12015-11-17 16:13:55 -08001031
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -08001032 *offset = 0x1000 * pa->ee + 0x8000 * apid;
Stephen Boyd987a9f12015-11-17 16:13:55 -08001033 return 0;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001034}
1035
David Collinsb2d9a402016-07-21 14:42:47 -07001036/*
1037 * v5 offset per ee and per apid for observer channels and per apid for
1038 * read/write channels.
1039 */
1040static int
1041pmic_arb_offset_v5(struct spmi_pmic_arb *pa, u8 sid, u16 addr,
1042 enum pmic_arb_channel ch_type, u32 *offset)
1043{
1044 u16 apid;
1045 int rc;
1046
1047 rc = pmic_arb_ppid_to_apid_v5(pa, sid, addr, &apid);
1048 if (rc < 0)
1049 return rc;
1050
1051 *offset = (ch_type == PMIC_ARB_CHANNEL_OBS)
1052 ? 0x10000 * pa->ee + 0x80 * apid
1053 : 0x10000 * apid;
1054 return 0;
1055}
1056
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001057static u32 pmic_arb_fmt_cmd_v1(u8 opc, u8 sid, u16 addr, u8 bc)
1058{
1059 return (opc << 27) | ((sid & 0xf) << 20) | (addr << 4) | (bc & 0x7);
1060}
1061
1062static u32 pmic_arb_fmt_cmd_v2(u8 opc, u8 sid, u16 addr, u8 bc)
1063{
1064 return (opc << 27) | ((addr & 0xff) << 4) | (bc & 0x7);
1065}
1066
David Collins370a4fa2016-07-21 16:58:29 -07001067static u32 pmic_arb_owner_acc_status_v1(u8 m, u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001068{
1069 return 0x20 * m + 0x4 * n;
1070}
1071
David Collins370a4fa2016-07-21 16:58:29 -07001072static u32 pmic_arb_owner_acc_status_v2(u8 m, u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001073{
1074 return 0x100000 + 0x1000 * m + 0x4 * n;
1075}
1076
David Collins370a4fa2016-07-21 16:58:29 -07001077static u32 pmic_arb_owner_acc_status_v3(u8 m, u16 n)
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001078{
1079 return 0x200000 + 0x1000 * m + 0x4 * n;
1080}
1081
David Collinsb2d9a402016-07-21 14:42:47 -07001082static u32 pmic_arb_owner_acc_status_v5(u8 m, u16 n)
1083{
1084 return 0x10000 * m + 0x4 * n;
1085}
1086
David Collins370a4fa2016-07-21 16:58:29 -07001087static u32 pmic_arb_acc_enable_v1(u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001088{
1089 return 0x200 + 0x4 * n;
1090}
1091
David Collins370a4fa2016-07-21 16:58:29 -07001092static u32 pmic_arb_acc_enable_v2(u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001093{
1094 return 0x1000 * n;
1095}
1096
David Collinsb2d9a402016-07-21 14:42:47 -07001097static u32 pmic_arb_acc_enable_v5(u16 n)
1098{
1099 return 0x100 + 0x10000 * n;
1100}
1101
David Collins370a4fa2016-07-21 16:58:29 -07001102static u32 pmic_arb_irq_status_v1(u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001103{
1104 return 0x600 + 0x4 * n;
1105}
1106
David Collins370a4fa2016-07-21 16:58:29 -07001107static u32 pmic_arb_irq_status_v2(u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001108{
1109 return 0x4 + 0x1000 * n;
1110}
1111
David Collinsb2d9a402016-07-21 14:42:47 -07001112static u32 pmic_arb_irq_status_v5(u16 n)
1113{
1114 return 0x104 + 0x10000 * n;
1115}
1116
David Collins370a4fa2016-07-21 16:58:29 -07001117static u32 pmic_arb_irq_clear_v1(u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001118{
1119 return 0xA00 + 0x4 * n;
1120}
1121
David Collins370a4fa2016-07-21 16:58:29 -07001122static u32 pmic_arb_irq_clear_v2(u16 n)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001123{
1124 return 0x8 + 0x1000 * n;
1125}
1126
David Collinsb2d9a402016-07-21 14:42:47 -07001127static u32 pmic_arb_irq_clear_v5(u16 n)
1128{
1129 return 0x108 + 0x10000 * n;
1130}
1131
1132static u32 pmic_arb_channel_map_offset_v2(u16 n)
1133{
1134 return 0x800 + 0x4 * n;
1135}
1136
1137static u32 pmic_arb_channel_map_offset_v5(u16 n)
1138{
1139 return 0x900 + 0x4 * n;
1140}
1141
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001142static const struct pmic_arb_ver_ops pmic_arb_v1 = {
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001143 .ver_str = "v1",
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -08001144 .ppid_to_apid = pmic_arb_ppid_to_apid_v1,
Abhijeet Dharmapurikar78888862016-07-05 17:54:47 -07001145 .mode = pmic_arb_mode_v1_v3,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001146 .non_data_cmd = pmic_arb_non_data_cmd_v1,
1147 .offset = pmic_arb_offset_v1,
1148 .fmt_cmd = pmic_arb_fmt_cmd_v1,
1149 .owner_acc_status = pmic_arb_owner_acc_status_v1,
1150 .acc_enable = pmic_arb_acc_enable_v1,
1151 .irq_status = pmic_arb_irq_status_v1,
1152 .irq_clear = pmic_arb_irq_clear_v1,
David Collinsb2d9a402016-07-21 14:42:47 -07001153 .channel_map_offset = pmic_arb_channel_map_offset_v2,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001154};
1155
1156static const struct pmic_arb_ver_ops pmic_arb_v2 = {
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001157 .ver_str = "v2",
Abhijeet Dharmapurikar6e9eb382016-01-08 12:50:24 -08001158 .ppid_to_apid = pmic_arb_ppid_to_apid_v2,
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -08001159 .mode = pmic_arb_mode_v2,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001160 .non_data_cmd = pmic_arb_non_data_cmd_v2,
1161 .offset = pmic_arb_offset_v2,
1162 .fmt_cmd = pmic_arb_fmt_cmd_v2,
1163 .owner_acc_status = pmic_arb_owner_acc_status_v2,
1164 .acc_enable = pmic_arb_acc_enable_v2,
1165 .irq_status = pmic_arb_irq_status_v2,
1166 .irq_clear = pmic_arb_irq_clear_v2,
David Collinsb2d9a402016-07-21 14:42:47 -07001167 .channel_map_offset = pmic_arb_channel_map_offset_v2,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001168};
1169
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001170static const struct pmic_arb_ver_ops pmic_arb_v3 = {
1171 .ver_str = "v3",
1172 .ppid_to_apid = pmic_arb_ppid_to_apid_v2,
Abhijeet Dharmapurikar78888862016-07-05 17:54:47 -07001173 .mode = pmic_arb_mode_v1_v3,
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001174 .non_data_cmd = pmic_arb_non_data_cmd_v2,
1175 .offset = pmic_arb_offset_v2,
1176 .fmt_cmd = pmic_arb_fmt_cmd_v2,
1177 .owner_acc_status = pmic_arb_owner_acc_status_v3,
1178 .acc_enable = pmic_arb_acc_enable_v2,
1179 .irq_status = pmic_arb_irq_status_v2,
1180 .irq_clear = pmic_arb_irq_clear_v2,
David Collinsb2d9a402016-07-21 14:42:47 -07001181 .channel_map_offset = pmic_arb_channel_map_offset_v2,
1182};
1183
1184static const struct pmic_arb_ver_ops pmic_arb_v5 = {
1185 .ver_str = "v5",
1186 .ppid_to_apid = pmic_arb_ppid_to_apid_v5,
1187 .mode = pmic_arb_mode_v2,
1188 .non_data_cmd = pmic_arb_non_data_cmd_v2,
1189 .offset = pmic_arb_offset_v5,
1190 .fmt_cmd = pmic_arb_fmt_cmd_v2,
1191 .owner_acc_status = pmic_arb_owner_acc_status_v5,
1192 .acc_enable = pmic_arb_acc_enable_v5,
1193 .irq_status = pmic_arb_irq_status_v5,
1194 .irq_clear = pmic_arb_irq_clear_v5,
1195 .channel_map_offset = pmic_arb_channel_map_offset_v5,
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001196};
1197
Josh Cartwright67b563f2014-02-12 13:44:25 -06001198static const struct irq_domain_ops pmic_arb_irq_domain_ops = {
1199 .map = qpnpint_irq_domain_map,
1200 .xlate = qpnpint_irq_domain_dt_translate,
Subbaraman Narayanamurthyf115a0e2017-01-30 15:26:24 -08001201 .activate = qpnpint_irq_domain_activate,
Josh Cartwright67b563f2014-02-12 13:44:25 -06001202};
1203
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001204static int spmi_pmic_arb_probe(struct platform_device *pdev)
1205{
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -08001206 struct spmi_pmic_arb *pa;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001207 struct spmi_controller *ctrl;
1208 struct resource *res;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001209 void __iomem *core;
1210 u32 channel, ee, hw_ver;
Stephen Boyd987a9f12015-11-17 16:13:55 -08001211 int err;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001212
1213 ctrl = spmi_controller_alloc(&pdev->dev, sizeof(*pa));
1214 if (!ctrl)
1215 return -ENOMEM;
1216
1217 pa = spmi_controller_get_drvdata(ctrl);
Josh Cartwright67b563f2014-02-12 13:44:25 -06001218 pa->spmic = ctrl;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001219
1220 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "core");
Abhijeet Dharmapurikar57132f52016-09-13 11:10:48 -07001221 if (!res) {
1222 dev_err(&pdev->dev, "core resource not specified\n");
1223 err = -EINVAL;
1224 goto err_put_ctrl;
1225 }
1226
Stephen Boyd987a9f12015-11-17 16:13:55 -08001227 pa->core_size = resource_size(res);
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -08001228 if (pa->core_size <= 0x800) {
1229 dev_err(&pdev->dev, "core_size is smaller than 0x800. Failing Probe\n");
1230 err = -EINVAL;
1231 goto err_put_ctrl;
1232 }
1233
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001234 core = devm_ioremap_resource(&ctrl->dev, res);
1235 if (IS_ERR(core)) {
1236 err = PTR_ERR(core);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001237 goto err_put_ctrl;
1238 }
1239
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001240 hw_ver = readl_relaxed(core + PMIC_ARB_VERSION);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001241
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001242 if (hw_ver < PMIC_ARB_VERSION_V2_MIN) {
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001243 pa->ver_ops = &pmic_arb_v1;
1244 pa->wr_base = core;
1245 pa->rd_base = core;
1246 } else {
Stephen Boyd987a9f12015-11-17 16:13:55 -08001247 pa->core = core;
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001248
1249 if (hw_ver < PMIC_ARB_VERSION_V3_MIN)
1250 pa->ver_ops = &pmic_arb_v2;
David Collinsb2d9a402016-07-21 14:42:47 -07001251 else if (hw_ver < PMIC_ARB_VERSION_V5_MIN)
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001252 pa->ver_ops = &pmic_arb_v3;
David Collinsb2d9a402016-07-21 14:42:47 -07001253 else
1254 pa->ver_ops = &pmic_arb_v5;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001255
David Collinsb2d9a402016-07-21 14:42:47 -07001256 /* the apid to ppid table starts at PMIC_ARB_REG_CHNL0 */
1257 pa->max_periph
1258 = (pa->core_size - pa->ver_ops->channel_map_offset(0)) / 4;
Abhijeet Dharmapurikarea64f7f2016-01-18 22:00:33 -08001259
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001260 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1261 "obsrvr");
1262 pa->rd_base = devm_ioremap_resource(&ctrl->dev, res);
1263 if (IS_ERR(pa->rd_base)) {
1264 err = PTR_ERR(pa->rd_base);
1265 goto err_put_ctrl;
1266 }
1267
1268 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1269 "chnls");
1270 pa->wr_base = devm_ioremap_resource(&ctrl->dev, res);
1271 if (IS_ERR(pa->wr_base)) {
1272 err = PTR_ERR(pa->wr_base);
1273 goto err_put_ctrl;
1274 }
1275
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -08001276 pa->ppid_to_apid = devm_kcalloc(&ctrl->dev,
Stephen Boyd987a9f12015-11-17 16:13:55 -08001277 PMIC_ARB_MAX_PPID,
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -08001278 sizeof(*pa->ppid_to_apid),
Stephen Boyd987a9f12015-11-17 16:13:55 -08001279 GFP_KERNEL);
Abhijeet Dharmapurikar39155b62016-01-06 19:55:21 -08001280 if (!pa->ppid_to_apid) {
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001281 err = -ENOMEM;
1282 goto err_put_ctrl;
1283 }
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001284 }
1285
Nicholas Troast9c10f8f2016-03-28 10:16:31 -07001286 dev_info(&ctrl->dev, "PMIC arbiter version %s (0x%x)\n",
1287 pa->ver_ops->ver_str, hw_ver);
1288
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001289 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "intr");
1290 pa->intr = devm_ioremap_resource(&ctrl->dev, res);
1291 if (IS_ERR(pa->intr)) {
1292 err = PTR_ERR(pa->intr);
1293 goto err_put_ctrl;
1294 }
David Collinsb2d9a402016-07-21 14:42:47 -07001295 pa->acc_status = pa->intr;
1296
1297 /*
1298 * PMIC arbiter v5 groups the IRQ control registers in the same hardware
1299 * module as the read/write channels.
1300 */
1301 if (hw_ver >= PMIC_ARB_VERSION_V5_MIN)
1302 pa->intr = pa->wr_base;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001303
1304 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cnfg");
1305 pa->cnfg = devm_ioremap_resource(&ctrl->dev, res);
1306 if (IS_ERR(pa->cnfg)) {
1307 err = PTR_ERR(pa->cnfg);
1308 goto err_put_ctrl;
1309 }
1310
Josh Cartwright67b563f2014-02-12 13:44:25 -06001311 pa->irq = platform_get_irq_byname(pdev, "periph_irq");
1312 if (pa->irq < 0) {
1313 err = pa->irq;
1314 goto err_put_ctrl;
1315 }
1316
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001317 err = of_property_read_u32(pdev->dev.of_node, "qcom,channel", &channel);
1318 if (err) {
1319 dev_err(&pdev->dev, "channel unspecified.\n");
1320 goto err_put_ctrl;
1321 }
1322
1323 if (channel > 5) {
1324 dev_err(&pdev->dev, "invalid channel (%u) specified.\n",
1325 channel);
Christophe JAILLETe98cc182016-09-26 22:24:46 +02001326 err = -EINVAL;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001327 goto err_put_ctrl;
1328 }
1329
1330 pa->channel = channel;
1331
Josh Cartwright67b563f2014-02-12 13:44:25 -06001332 err = of_property_read_u32(pdev->dev.of_node, "qcom,ee", &ee);
1333 if (err) {
1334 dev_err(&pdev->dev, "EE unspecified.\n");
1335 goto err_put_ctrl;
1336 }
1337
1338 if (ee > 5) {
1339 dev_err(&pdev->dev, "invalid EE (%u) specified\n", ee);
1340 err = -EINVAL;
1341 goto err_put_ctrl;
1342 }
1343
1344 pa->ee = ee;
1345
Stephen Boyd987a9f12015-11-17 16:13:55 -08001346 pa->mapping_table = devm_kcalloc(&ctrl->dev, PMIC_ARB_MAX_PERIPHS - 1,
1347 sizeof(*pa->mapping_table), GFP_KERNEL);
1348 if (!pa->mapping_table) {
1349 err = -ENOMEM;
1350 goto err_put_ctrl;
1351 }
Josh Cartwright67b563f2014-02-12 13:44:25 -06001352
1353 /* Initialize max_apid/min_apid to the opposite bounds, during
1354 * the irq domain translation, we are sure to update these */
1355 pa->max_apid = 0;
1356 pa->min_apid = PMIC_ARB_MAX_PERIPHS - 1;
1357
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001358 platform_set_drvdata(pdev, ctrl);
1359 raw_spin_lock_init(&pa->lock);
1360
1361 ctrl->cmd = pmic_arb_cmd;
1362 ctrl->read_cmd = pmic_arb_read_cmd;
1363 ctrl->write_cmd = pmic_arb_write_cmd;
1364
David Collinsb2d9a402016-07-21 14:42:47 -07001365 if (hw_ver >= PMIC_ARB_VERSION_V5_MIN) {
1366 err = pmic_arb_read_apid_map_v5(pa);
1367 if (err) {
1368 dev_err(&pdev->dev, "could not read APID->PPID mapping table, rc= %d\n",
1369 err);
1370 goto err_put_ctrl;
1371 }
1372 }
1373
Josh Cartwright67b563f2014-02-12 13:44:25 -06001374 dev_dbg(&pdev->dev, "adding irq domain\n");
1375 pa->domain = irq_domain_add_tree(pdev->dev.of_node,
1376 &pmic_arb_irq_domain_ops, pa);
1377 if (!pa->domain) {
1378 dev_err(&pdev->dev, "unable to create irq_domain\n");
1379 err = -ENOMEM;
1380 goto err_put_ctrl;
1381 }
1382
Thomas Gleixnerfb68ba62015-07-13 20:52:24 +00001383 irq_set_chained_handler_and_data(pa->irq, pmic_arb_chained_irq, pa);
Nicholas Troast237e9142016-06-14 16:39:38 -07001384 enable_irq_wake(pa->irq);
Josh Cartwright67b563f2014-02-12 13:44:25 -06001385
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001386 err = spmi_controller_add(ctrl);
1387 if (err)
Josh Cartwright67b563f2014-02-12 13:44:25 -06001388 goto err_domain_remove;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001389
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001390 return 0;
1391
Josh Cartwright67b563f2014-02-12 13:44:25 -06001392err_domain_remove:
Thomas Gleixnerfb68ba62015-07-13 20:52:24 +00001393 irq_set_chained_handler_and_data(pa->irq, NULL, NULL);
Josh Cartwright67b563f2014-02-12 13:44:25 -06001394 irq_domain_remove(pa->domain);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001395err_put_ctrl:
1396 spmi_controller_put(ctrl);
1397 return err;
1398}
1399
1400static int spmi_pmic_arb_remove(struct platform_device *pdev)
1401{
1402 struct spmi_controller *ctrl = platform_get_drvdata(pdev);
Abhijeet Dharmapurikarfbcfb7e2016-01-08 12:45:20 -08001403 struct spmi_pmic_arb *pa = spmi_controller_get_drvdata(ctrl);
Abhijeet Dharmapurikar69dc3fc2016-11-07 15:51:24 -08001404
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001405 spmi_controller_remove(ctrl);
Thomas Gleixnerfb68ba62015-07-13 20:52:24 +00001406 irq_set_chained_handler_and_data(pa->irq, NULL, NULL);
Josh Cartwright67b563f2014-02-12 13:44:25 -06001407 irq_domain_remove(pa->domain);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001408 spmi_controller_put(ctrl);
1409 return 0;
1410}
1411
1412static const struct of_device_id spmi_pmic_arb_match_table[] = {
1413 { .compatible = "qcom,spmi-pmic-arb", },
1414 {},
1415};
1416MODULE_DEVICE_TABLE(of, spmi_pmic_arb_match_table);
1417
1418static struct platform_driver spmi_pmic_arb_driver = {
1419 .probe = spmi_pmic_arb_probe,
1420 .remove = spmi_pmic_arb_remove,
1421 .driver = {
1422 .name = "spmi_pmic_arb",
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001423 .of_match_table = spmi_pmic_arb_match_table,
1424 },
1425};
Abhijeet Dharmapurikardf9bf942015-09-23 11:36:23 -07001426
1427int __init spmi_pmic_arb_init(void)
1428{
1429 return platform_driver_register(&spmi_pmic_arb_driver);
1430}
1431arch_initcall(spmi_pmic_arb_init);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001432
1433MODULE_LICENSE("GPL v2");
1434MODULE_ALIAS("platform:spmi_pmic_arb");