blob: 5ec3a595dc7d500bf0c217729c5a6a7669327db2 [file] [log] [blame]
Gilad Avidovd0c6ae42015-03-25 11:37:32 -06001/*
2 * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06003 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
Stephen Boyd987a9f12015-11-17 16:13:55 -080013#include <linux/bitmap.h>
Kenneth Heitke39ae93e2014-02-12 13:44:24 -060014#include <linux/delay.h>
15#include <linux/err.h>
16#include <linux/interrupt.h>
17#include <linux/io.h>
Josh Cartwright67b563f2014-02-12 13:44:25 -060018#include <linux/irqchip/chained_irq.h>
19#include <linux/irqdomain.h>
20#include <linux/irq.h>
Kenneth Heitke39ae93e2014-02-12 13:44:24 -060021#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/of.h>
24#include <linux/platform_device.h>
25#include <linux/slab.h>
26#include <linux/spmi.h>
27
28/* PMIC Arbiter configuration registers */
29#define PMIC_ARB_VERSION 0x0000
Gilad Avidovd0c6ae42015-03-25 11:37:32 -060030#define PMIC_ARB_VERSION_V2_MIN 0x20010000
Kenneth Heitke39ae93e2014-02-12 13:44:24 -060031#define PMIC_ARB_INT_EN 0x0004
32
Gilad Avidovd0c6ae42015-03-25 11:37:32 -060033/* PMIC Arbiter channel registers offsets */
34#define PMIC_ARB_CMD 0x00
35#define PMIC_ARB_CONFIG 0x04
36#define PMIC_ARB_STATUS 0x08
37#define PMIC_ARB_WDATA0 0x10
38#define PMIC_ARB_WDATA1 0x14
39#define PMIC_ARB_RDATA0 0x18
40#define PMIC_ARB_RDATA1 0x1C
41#define PMIC_ARB_REG_CHNL(N) (0x800 + 0x4 * (N))
Kenneth Heitke39ae93e2014-02-12 13:44:24 -060042
43/* Mapping Table */
44#define SPMI_MAPPING_TABLE_REG(N) (0x0B00 + (4 * (N)))
45#define SPMI_MAPPING_BIT_INDEX(X) (((X) >> 18) & 0xF)
46#define SPMI_MAPPING_BIT_IS_0_FLAG(X) (((X) >> 17) & 0x1)
47#define SPMI_MAPPING_BIT_IS_0_RESULT(X) (((X) >> 9) & 0xFF)
48#define SPMI_MAPPING_BIT_IS_1_FLAG(X) (((X) >> 8) & 0x1)
49#define SPMI_MAPPING_BIT_IS_1_RESULT(X) (((X) >> 0) & 0xFF)
50
Kenneth Heitke39ae93e2014-02-12 13:44:24 -060051#define SPMI_MAPPING_TABLE_TREE_DEPTH 16 /* Maximum of 16-bits */
Stephen Boyd987a9f12015-11-17 16:13:55 -080052#define PMIC_ARB_MAX_PPID BIT(12) /* PPID is 12bit */
53#define PMIC_ARB_CHAN_VALID BIT(15)
Kenneth Heitke39ae93e2014-02-12 13:44:24 -060054
55/* Ownership Table */
56#define SPMI_OWNERSHIP_TABLE_REG(N) (0x0700 + (4 * (N)))
57#define SPMI_OWNERSHIP_PERIPH2OWNER(X) ((X) & 0x7)
58
59/* Channel Status fields */
60enum pmic_arb_chnl_status {
61 PMIC_ARB_STATUS_DONE = (1 << 0),
62 PMIC_ARB_STATUS_FAILURE = (1 << 1),
63 PMIC_ARB_STATUS_DENIED = (1 << 2),
64 PMIC_ARB_STATUS_DROPPED = (1 << 3),
65};
66
67/* Command register fields */
68#define PMIC_ARB_CMD_MAX_BYTE_COUNT 8
69
70/* Command Opcodes */
71enum pmic_arb_cmd_op_code {
72 PMIC_ARB_OP_EXT_WRITEL = 0,
73 PMIC_ARB_OP_EXT_READL = 1,
74 PMIC_ARB_OP_EXT_WRITE = 2,
75 PMIC_ARB_OP_RESET = 3,
76 PMIC_ARB_OP_SLEEP = 4,
77 PMIC_ARB_OP_SHUTDOWN = 5,
78 PMIC_ARB_OP_WAKEUP = 6,
79 PMIC_ARB_OP_AUTHENTICATE = 7,
80 PMIC_ARB_OP_MSTR_READ = 8,
81 PMIC_ARB_OP_MSTR_WRITE = 9,
82 PMIC_ARB_OP_EXT_READ = 13,
83 PMIC_ARB_OP_WRITE = 14,
84 PMIC_ARB_OP_READ = 15,
85 PMIC_ARB_OP_ZERO_WRITE = 16,
86};
87
88/* Maximum number of support PMIC peripherals */
Stephen Boyd987a9f12015-11-17 16:13:55 -080089#define PMIC_ARB_MAX_PERIPHS 512
Kenneth Heitke39ae93e2014-02-12 13:44:24 -060090#define PMIC_ARB_TIMEOUT_US 100
91#define PMIC_ARB_MAX_TRANS_BYTES (8)
92
93#define PMIC_ARB_APID_MASK 0xFF
94#define PMIC_ARB_PPID_MASK 0xFFF
95
96/* interrupt enable bit */
97#define SPMI_PIC_ACC_ENABLE_BIT BIT(0)
98
Gilad Avidovd0c6ae42015-03-25 11:37:32 -060099struct pmic_arb_ver_ops;
100
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600101/**
102 * spmi_pmic_arb_dev - SPMI PMIC Arbiter object
103 *
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600104 * @rd_base: on v1 "core", on v2 "observer" register base off DT.
105 * @wr_base: on v1 "core", on v2 "chnls" register base off DT.
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600106 * @intr: address of the SPMI interrupt control registers.
107 * @cnfg: address of the PMIC Arbiter configuration registers.
108 * @lock: lock to synchronize accesses.
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600109 * @channel: execution environment channel to use for accesses.
Josh Cartwright67b563f2014-02-12 13:44:25 -0600110 * @irq: PMIC ARB interrupt.
111 * @ee: the current Execution Environment
112 * @min_apid: minimum APID (used for bounding IRQ search)
113 * @max_apid: maximum APID
114 * @mapping_table: in-memory copy of PPID -> APID mapping table.
115 * @domain: irq domain object for PMIC IRQ domain
116 * @spmic: SPMI controller object
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600117 * @apid_to_ppid: in-memory copy of APID -> PPID mapping table.
118 * @ver_ops: version dependent operations.
119 * @ppid_to_chan in-memory copy of PPID -> channel (APID) mapping table.
120 * v2 only.
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600121 */
122struct spmi_pmic_arb_dev {
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600123 void __iomem *rd_base;
124 void __iomem *wr_base;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600125 void __iomem *intr;
126 void __iomem *cnfg;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800127 void __iomem *core;
128 resource_size_t core_size;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600129 raw_spinlock_t lock;
130 u8 channel;
Josh Cartwright67b563f2014-02-12 13:44:25 -0600131 int irq;
132 u8 ee;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800133 u16 min_apid;
134 u16 max_apid;
135 u32 *mapping_table;
136 DECLARE_BITMAP(mapping_table_valid, PMIC_ARB_MAX_PERIPHS);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600137 struct irq_domain *domain;
138 struct spmi_controller *spmic;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800139 u16 *apid_to_ppid;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600140 const struct pmic_arb_ver_ops *ver_ops;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800141 u16 *ppid_to_chan;
142 u16 last_channel;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600143};
144
145/**
146 * pmic_arb_ver: version dependent functionality.
147 *
148 * @non_data_cmd: on v1 issues an spmi non-data command.
149 * on v2 no HW support, returns -EOPNOTSUPP.
150 * @offset: on v1 offset of per-ee channel.
151 * on v2 offset of per-ee and per-ppid channel.
152 * @fmt_cmd: formats a GENI/SPMI command.
153 * @owner_acc_status: on v1 offset of PMIC_ARB_SPMI_PIC_OWNERm_ACC_STATUSn
154 * on v2 offset of SPMI_PIC_OWNERm_ACC_STATUSn.
155 * @acc_enable: on v1 offset of PMIC_ARB_SPMI_PIC_ACC_ENABLEn
156 * on v2 offset of SPMI_PIC_ACC_ENABLEn.
157 * @irq_status: on v1 offset of PMIC_ARB_SPMI_PIC_IRQ_STATUSn
158 * on v2 offset of SPMI_PIC_IRQ_STATUSn.
159 * @irq_clear: on v1 offset of PMIC_ARB_SPMI_PIC_IRQ_CLEARn
160 * on v2 offset of SPMI_PIC_IRQ_CLEARn.
161 */
162struct pmic_arb_ver_ops {
163 /* spmi commands (read_cmd, write_cmd, cmd) functionality */
Stephen Boyd987a9f12015-11-17 16:13:55 -0800164 int (*offset)(struct spmi_pmic_arb_dev *dev, u8 sid, u16 addr,
165 u32 *offset);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600166 u32 (*fmt_cmd)(u8 opc, u8 sid, u16 addr, u8 bc);
167 int (*non_data_cmd)(struct spmi_controller *ctrl, u8 opc, u8 sid);
168 /* Interrupts controller functionality (offset of PIC registers) */
169 u32 (*owner_acc_status)(u8 m, u8 n);
170 u32 (*acc_enable)(u8 n);
171 u32 (*irq_status)(u8 n);
172 u32 (*irq_clear)(u8 n);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600173};
174
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600175static inline void pmic_arb_base_write(struct spmi_pmic_arb_dev *dev,
176 u32 offset, u32 val)
177{
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600178 writel_relaxed(val, dev->wr_base + offset);
179}
180
181static inline void pmic_arb_set_rd_cmd(struct spmi_pmic_arb_dev *dev,
182 u32 offset, u32 val)
183{
184 writel_relaxed(val, dev->rd_base + offset);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600185}
186
187/**
188 * pa_read_data: reads pmic-arb's register and copy 1..4 bytes to buf
189 * @bc: byte count -1. range: 0..3
190 * @reg: register's address
191 * @buf: output parameter, length must be bc + 1
192 */
193static void pa_read_data(struct spmi_pmic_arb_dev *dev, u8 *buf, u32 reg, u8 bc)
194{
Stephen Boydd5144792015-08-28 12:21:42 -0700195 u32 data = __raw_readl(dev->rd_base + reg);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600196 memcpy(buf, &data, (bc & 3) + 1);
197}
198
199/**
200 * pa_write_data: write 1..4 bytes from buf to pmic-arb's register
201 * @bc: byte-count -1. range: 0..3.
202 * @reg: register's address.
203 * @buf: buffer to write. length must be bc + 1.
204 */
205static void
206pa_write_data(struct spmi_pmic_arb_dev *dev, const u8 *buf, u32 reg, u8 bc)
207{
208 u32 data = 0;
209 memcpy(&data, buf, (bc & 3) + 1);
Stephen Boydd5144792015-08-28 12:21:42 -0700210 __raw_writel(data, dev->wr_base + reg);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600211}
212
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600213static int pmic_arb_wait_for_done(struct spmi_controller *ctrl,
214 void __iomem *base, u8 sid, u16 addr)
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600215{
216 struct spmi_pmic_arb_dev *dev = spmi_controller_get_drvdata(ctrl);
217 u32 status = 0;
218 u32 timeout = PMIC_ARB_TIMEOUT_US;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800219 u32 offset;
220 int rc;
221
222 rc = dev->ver_ops->offset(dev, sid, addr, &offset);
223 if (rc)
224 return rc;
225
226 offset += PMIC_ARB_STATUS;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600227
228 while (timeout--) {
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600229 status = readl_relaxed(base + offset);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600230
231 if (status & PMIC_ARB_STATUS_DONE) {
232 if (status & PMIC_ARB_STATUS_DENIED) {
233 dev_err(&ctrl->dev,
234 "%s: transaction denied (0x%x)\n",
235 __func__, status);
236 return -EPERM;
237 }
238
239 if (status & PMIC_ARB_STATUS_FAILURE) {
240 dev_err(&ctrl->dev,
241 "%s: transaction failed (0x%x)\n",
242 __func__, status);
243 return -EIO;
244 }
245
246 if (status & PMIC_ARB_STATUS_DROPPED) {
247 dev_err(&ctrl->dev,
248 "%s: transaction dropped (0x%x)\n",
249 __func__, status);
250 return -EIO;
251 }
252
253 return 0;
254 }
255 udelay(1);
256 }
257
258 dev_err(&ctrl->dev,
259 "%s: timeout, status 0x%x\n",
260 __func__, status);
261 return -ETIMEDOUT;
262}
263
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600264static int
265pmic_arb_non_data_cmd_v1(struct spmi_controller *ctrl, u8 opc, u8 sid)
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600266{
267 struct spmi_pmic_arb_dev *pmic_arb = spmi_controller_get_drvdata(ctrl);
268 unsigned long flags;
269 u32 cmd;
270 int rc;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800271 u32 offset;
272
273 rc = pmic_arb->ver_ops->offset(pmic_arb, sid, 0, &offset);
274 if (rc)
275 return rc;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600276
277 cmd = ((opc | 0x40) << 27) | ((sid & 0xf) << 20);
278
279 raw_spin_lock_irqsave(&pmic_arb->lock, flags);
280 pmic_arb_base_write(pmic_arb, offset + PMIC_ARB_CMD, cmd);
281 rc = pmic_arb_wait_for_done(ctrl, pmic_arb->wr_base, sid, 0);
282 raw_spin_unlock_irqrestore(&pmic_arb->lock, flags);
283
284 return rc;
285}
286
287static int
288pmic_arb_non_data_cmd_v2(struct spmi_controller *ctrl, u8 opc, u8 sid)
289{
290 return -EOPNOTSUPP;
291}
292
293/* Non-data command */
294static int pmic_arb_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid)
295{
296 struct spmi_pmic_arb_dev *pmic_arb = spmi_controller_get_drvdata(ctrl);
297
298 dev_dbg(&ctrl->dev, "cmd op:0x%x sid:%d\n", opc, sid);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600299
300 /* Check for valid non-data command */
301 if (opc < SPMI_CMD_RESET || opc > SPMI_CMD_WAKEUP)
302 return -EINVAL;
303
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600304 return pmic_arb->ver_ops->non_data_cmd(ctrl, opc, sid);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600305}
306
307static int pmic_arb_read_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
308 u16 addr, u8 *buf, size_t len)
309{
310 struct spmi_pmic_arb_dev *pmic_arb = spmi_controller_get_drvdata(ctrl);
311 unsigned long flags;
312 u8 bc = len - 1;
313 u32 cmd;
314 int rc;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800315 u32 offset;
316
317 rc = pmic_arb->ver_ops->offset(pmic_arb, sid, addr, &offset);
318 if (rc)
319 return rc;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600320
321 if (bc >= PMIC_ARB_MAX_TRANS_BYTES) {
322 dev_err(&ctrl->dev,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600323 "pmic-arb supports 1..%d bytes per trans, but:%zu requested",
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600324 PMIC_ARB_MAX_TRANS_BYTES, len);
325 return -EINVAL;
326 }
327
328 /* Check the opcode */
329 if (opc >= 0x60 && opc <= 0x7F)
330 opc = PMIC_ARB_OP_READ;
331 else if (opc >= 0x20 && opc <= 0x2F)
332 opc = PMIC_ARB_OP_EXT_READ;
333 else if (opc >= 0x38 && opc <= 0x3F)
334 opc = PMIC_ARB_OP_EXT_READL;
335 else
336 return -EINVAL;
337
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600338 cmd = pmic_arb->ver_ops->fmt_cmd(opc, sid, addr, bc);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600339
340 raw_spin_lock_irqsave(&pmic_arb->lock, flags);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600341 pmic_arb_set_rd_cmd(pmic_arb, offset + PMIC_ARB_CMD, cmd);
342 rc = pmic_arb_wait_for_done(ctrl, pmic_arb->rd_base, sid, addr);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600343 if (rc)
344 goto done;
345
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600346 pa_read_data(pmic_arb, buf, offset + PMIC_ARB_RDATA0,
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600347 min_t(u8, bc, 3));
348
349 if (bc > 3)
350 pa_read_data(pmic_arb, buf + 4,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600351 offset + PMIC_ARB_RDATA1, bc - 4);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600352
353done:
354 raw_spin_unlock_irqrestore(&pmic_arb->lock, flags);
355 return rc;
356}
357
358static int pmic_arb_write_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
359 u16 addr, const u8 *buf, size_t len)
360{
361 struct spmi_pmic_arb_dev *pmic_arb = spmi_controller_get_drvdata(ctrl);
362 unsigned long flags;
363 u8 bc = len - 1;
364 u32 cmd;
365 int rc;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800366 u32 offset;
367
368 rc = pmic_arb->ver_ops->offset(pmic_arb, sid, addr, &offset);
369 if (rc)
370 return rc;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600371
372 if (bc >= PMIC_ARB_MAX_TRANS_BYTES) {
373 dev_err(&ctrl->dev,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600374 "pmic-arb supports 1..%d bytes per trans, but:%zu requested",
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600375 PMIC_ARB_MAX_TRANS_BYTES, len);
376 return -EINVAL;
377 }
378
379 /* Check the opcode */
380 if (opc >= 0x40 && opc <= 0x5F)
381 opc = PMIC_ARB_OP_WRITE;
382 else if (opc >= 0x00 && opc <= 0x0F)
383 opc = PMIC_ARB_OP_EXT_WRITE;
384 else if (opc >= 0x30 && opc <= 0x37)
385 opc = PMIC_ARB_OP_EXT_WRITEL;
Stephen Boyd9b769682015-08-28 12:31:10 -0700386 else if (opc >= 0x80)
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600387 opc = PMIC_ARB_OP_ZERO_WRITE;
388 else
389 return -EINVAL;
390
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600391 cmd = pmic_arb->ver_ops->fmt_cmd(opc, sid, addr, bc);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600392
393 /* Write data to FIFOs */
394 raw_spin_lock_irqsave(&pmic_arb->lock, flags);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600395 pa_write_data(pmic_arb, buf, offset + PMIC_ARB_WDATA0,
396 min_t(u8, bc, 3));
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600397 if (bc > 3)
398 pa_write_data(pmic_arb, buf + 4,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600399 offset + PMIC_ARB_WDATA1, bc - 4);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600400
401 /* Start the transaction */
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600402 pmic_arb_base_write(pmic_arb, offset + PMIC_ARB_CMD, cmd);
403 rc = pmic_arb_wait_for_done(ctrl, pmic_arb->wr_base, sid, addr);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600404 raw_spin_unlock_irqrestore(&pmic_arb->lock, flags);
405
406 return rc;
407}
408
Josh Cartwright67b563f2014-02-12 13:44:25 -0600409enum qpnpint_regs {
410 QPNPINT_REG_RT_STS = 0x10,
411 QPNPINT_REG_SET_TYPE = 0x11,
412 QPNPINT_REG_POLARITY_HIGH = 0x12,
413 QPNPINT_REG_POLARITY_LOW = 0x13,
414 QPNPINT_REG_LATCHED_CLR = 0x14,
415 QPNPINT_REG_EN_SET = 0x15,
416 QPNPINT_REG_EN_CLR = 0x16,
417 QPNPINT_REG_LATCHED_STS = 0x18,
418};
419
420struct spmi_pmic_arb_qpnpint_type {
421 u8 type; /* 1 -> edge */
422 u8 polarity_high;
423 u8 polarity_low;
424} __packed;
425
426/* Simplified accessor functions for irqchip callbacks */
427static void qpnpint_spmi_write(struct irq_data *d, u8 reg, void *buf,
428 size_t len)
429{
430 struct spmi_pmic_arb_dev *pa = irq_data_get_irq_chip_data(d);
431 u8 sid = d->hwirq >> 24;
432 u8 per = d->hwirq >> 16;
433
434 if (pmic_arb_write_cmd(pa->spmic, SPMI_CMD_EXT_WRITEL, sid,
435 (per << 8) + reg, buf, len))
436 dev_err_ratelimited(&pa->spmic->dev,
437 "failed irqchip transaction on %x\n",
438 d->irq);
439}
440
441static void qpnpint_spmi_read(struct irq_data *d, u8 reg, void *buf, size_t len)
442{
443 struct spmi_pmic_arb_dev *pa = irq_data_get_irq_chip_data(d);
444 u8 sid = d->hwirq >> 24;
445 u8 per = d->hwirq >> 16;
446
447 if (pmic_arb_read_cmd(pa->spmic, SPMI_CMD_EXT_READL, sid,
448 (per << 8) + reg, buf, len))
449 dev_err_ratelimited(&pa->spmic->dev,
450 "failed irqchip transaction on %x\n",
451 d->irq);
452}
453
454static void periph_interrupt(struct spmi_pmic_arb_dev *pa, u8 apid)
455{
456 unsigned int irq;
457 u32 status;
458 int id;
459
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600460 status = readl_relaxed(pa->intr + pa->ver_ops->irq_status(apid));
Josh Cartwright67b563f2014-02-12 13:44:25 -0600461 while (status) {
462 id = ffs(status) - 1;
463 status &= ~(1 << id);
464 irq = irq_find_mapping(pa->domain,
465 pa->apid_to_ppid[apid] << 16
466 | id << 8
467 | apid);
468 generic_handle_irq(irq);
469 }
470}
471
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200472static void pmic_arb_chained_irq(struct irq_desc *desc)
Josh Cartwright67b563f2014-02-12 13:44:25 -0600473{
Jiang Liu7fe88f32015-07-13 20:52:25 +0000474 struct spmi_pmic_arb_dev *pa = irq_desc_get_handler_data(desc);
475 struct irq_chip *chip = irq_desc_get_chip(desc);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600476 void __iomem *intr = pa->intr;
477 int first = pa->min_apid >> 5;
478 int last = pa->max_apid >> 5;
479 u32 status;
480 int i, id;
481
482 chained_irq_enter(chip, desc);
483
484 for (i = first; i <= last; ++i) {
485 status = readl_relaxed(intr +
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600486 pa->ver_ops->owner_acc_status(pa->ee, i));
Josh Cartwright67b563f2014-02-12 13:44:25 -0600487 while (status) {
488 id = ffs(status) - 1;
489 status &= ~(1 << id);
490 periph_interrupt(pa, id + i * 32);
491 }
492 }
493
494 chained_irq_exit(chip, desc);
495}
496
497static void qpnpint_irq_ack(struct irq_data *d)
498{
499 struct spmi_pmic_arb_dev *pa = irq_data_get_irq_chip_data(d);
500 u8 irq = d->hwirq >> 8;
501 u8 apid = d->hwirq;
502 unsigned long flags;
503 u8 data;
504
505 raw_spin_lock_irqsave(&pa->lock, flags);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600506 writel_relaxed(1 << irq, pa->intr + pa->ver_ops->irq_clear(apid));
Josh Cartwright67b563f2014-02-12 13:44:25 -0600507 raw_spin_unlock_irqrestore(&pa->lock, flags);
508
509 data = 1 << irq;
510 qpnpint_spmi_write(d, QPNPINT_REG_LATCHED_CLR, &data, 1);
511}
512
513static void qpnpint_irq_mask(struct irq_data *d)
514{
515 struct spmi_pmic_arb_dev *pa = irq_data_get_irq_chip_data(d);
516 u8 irq = d->hwirq >> 8;
517 u8 apid = d->hwirq;
518 unsigned long flags;
519 u32 status;
520 u8 data;
521
522 raw_spin_lock_irqsave(&pa->lock, flags);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600523 status = readl_relaxed(pa->intr + pa->ver_ops->acc_enable(apid));
Josh Cartwright67b563f2014-02-12 13:44:25 -0600524 if (status & SPMI_PIC_ACC_ENABLE_BIT) {
525 status = status & ~SPMI_PIC_ACC_ENABLE_BIT;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600526 writel_relaxed(status, pa->intr +
527 pa->ver_ops->acc_enable(apid));
Josh Cartwright67b563f2014-02-12 13:44:25 -0600528 }
529 raw_spin_unlock_irqrestore(&pa->lock, flags);
530
531 data = 1 << irq;
532 qpnpint_spmi_write(d, QPNPINT_REG_EN_CLR, &data, 1);
533}
534
535static void qpnpint_irq_unmask(struct irq_data *d)
536{
537 struct spmi_pmic_arb_dev *pa = irq_data_get_irq_chip_data(d);
538 u8 irq = d->hwirq >> 8;
539 u8 apid = d->hwirq;
540 unsigned long flags;
541 u32 status;
542 u8 data;
543
544 raw_spin_lock_irqsave(&pa->lock, flags);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600545 status = readl_relaxed(pa->intr + pa->ver_ops->acc_enable(apid));
Josh Cartwright67b563f2014-02-12 13:44:25 -0600546 if (!(status & SPMI_PIC_ACC_ENABLE_BIT)) {
547 writel_relaxed(status | SPMI_PIC_ACC_ENABLE_BIT,
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600548 pa->intr + pa->ver_ops->acc_enable(apid));
Josh Cartwright67b563f2014-02-12 13:44:25 -0600549 }
550 raw_spin_unlock_irqrestore(&pa->lock, flags);
551
552 data = 1 << irq;
553 qpnpint_spmi_write(d, QPNPINT_REG_EN_SET, &data, 1);
554}
555
556static void qpnpint_irq_enable(struct irq_data *d)
557{
558 u8 irq = d->hwirq >> 8;
559 u8 data;
560
561 qpnpint_irq_unmask(d);
562
563 data = 1 << irq;
564 qpnpint_spmi_write(d, QPNPINT_REG_LATCHED_CLR, &data, 1);
565}
566
567static int qpnpint_irq_set_type(struct irq_data *d, unsigned int flow_type)
568{
569 struct spmi_pmic_arb_qpnpint_type type;
570 u8 irq = d->hwirq >> 8;
571
572 qpnpint_spmi_read(d, QPNPINT_REG_SET_TYPE, &type, sizeof(type));
573
574 if (flow_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)) {
575 type.type |= 1 << irq;
576 if (flow_type & IRQF_TRIGGER_RISING)
577 type.polarity_high |= 1 << irq;
578 if (flow_type & IRQF_TRIGGER_FALLING)
579 type.polarity_low |= 1 << irq;
580 } else {
581 if ((flow_type & (IRQF_TRIGGER_HIGH)) &&
582 (flow_type & (IRQF_TRIGGER_LOW)))
583 return -EINVAL;
584
585 type.type &= ~(1 << irq); /* level trig */
586 if (flow_type & IRQF_TRIGGER_HIGH)
587 type.polarity_high |= 1 << irq;
588 else
589 type.polarity_low |= 1 << irq;
590 }
591
592 qpnpint_spmi_write(d, QPNPINT_REG_SET_TYPE, &type, sizeof(type));
593 return 0;
594}
595
Courtney Cavin60be4232015-07-30 10:53:54 -0700596static int qpnpint_get_irqchip_state(struct irq_data *d,
597 enum irqchip_irq_state which,
598 bool *state)
599{
600 u8 irq = d->hwirq >> 8;
601 u8 status = 0;
602
603 if (which != IRQCHIP_STATE_LINE_LEVEL)
604 return -EINVAL;
605
606 qpnpint_spmi_read(d, QPNPINT_REG_RT_STS, &status, 1);
607 *state = !!(status & BIT(irq));
608
609 return 0;
610}
611
Josh Cartwright67b563f2014-02-12 13:44:25 -0600612static struct irq_chip pmic_arb_irqchip = {
613 .name = "pmic_arb",
614 .irq_enable = qpnpint_irq_enable,
615 .irq_ack = qpnpint_irq_ack,
616 .irq_mask = qpnpint_irq_mask,
617 .irq_unmask = qpnpint_irq_unmask,
618 .irq_set_type = qpnpint_irq_set_type,
Courtney Cavin60be4232015-07-30 10:53:54 -0700619 .irq_get_irqchip_state = qpnpint_get_irqchip_state,
Josh Cartwright67b563f2014-02-12 13:44:25 -0600620 .flags = IRQCHIP_MASK_ON_SUSPEND
621 | IRQCHIP_SKIP_SET_WAKE,
622};
623
624struct spmi_pmic_arb_irq_spec {
625 unsigned slave:4;
626 unsigned per:8;
627 unsigned irq:3;
628};
629
630static int search_mapping_table(struct spmi_pmic_arb_dev *pa,
631 struct spmi_pmic_arb_irq_spec *spec,
632 u8 *apid)
633{
634 u16 ppid = spec->slave << 8 | spec->per;
635 u32 *mapping_table = pa->mapping_table;
636 int index = 0, i;
637 u32 data;
638
639 for (i = 0; i < SPMI_MAPPING_TABLE_TREE_DEPTH; ++i) {
Stephen Boyd987a9f12015-11-17 16:13:55 -0800640 if (!test_and_set_bit(index, pa->mapping_table_valid))
641 mapping_table[index] = readl_relaxed(pa->cnfg +
642 SPMI_MAPPING_TABLE_REG(index));
643
Josh Cartwright67b563f2014-02-12 13:44:25 -0600644 data = mapping_table[index];
645
646 if (ppid & (1 << SPMI_MAPPING_BIT_INDEX(data))) {
647 if (SPMI_MAPPING_BIT_IS_1_FLAG(data)) {
648 index = SPMI_MAPPING_BIT_IS_1_RESULT(data);
649 } else {
650 *apid = SPMI_MAPPING_BIT_IS_1_RESULT(data);
651 return 0;
652 }
653 } else {
654 if (SPMI_MAPPING_BIT_IS_0_FLAG(data)) {
655 index = SPMI_MAPPING_BIT_IS_0_RESULT(data);
656 } else {
657 *apid = SPMI_MAPPING_BIT_IS_0_RESULT(data);
658 return 0;
659 }
660 }
661 }
662
663 return -ENODEV;
664}
665
666static int qpnpint_irq_domain_dt_translate(struct irq_domain *d,
667 struct device_node *controller,
668 const u32 *intspec,
669 unsigned int intsize,
670 unsigned long *out_hwirq,
671 unsigned int *out_type)
672{
673 struct spmi_pmic_arb_dev *pa = d->host_data;
674 struct spmi_pmic_arb_irq_spec spec;
675 int err;
676 u8 apid;
677
678 dev_dbg(&pa->spmic->dev,
679 "intspec[0] 0x%1x intspec[1] 0x%02x intspec[2] 0x%02x\n",
680 intspec[0], intspec[1], intspec[2]);
681
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100682 if (irq_domain_get_of_node(d) != controller)
Josh Cartwright67b563f2014-02-12 13:44:25 -0600683 return -EINVAL;
684 if (intsize != 4)
685 return -EINVAL;
686 if (intspec[0] > 0xF || intspec[1] > 0xFF || intspec[2] > 0x7)
687 return -EINVAL;
688
689 spec.slave = intspec[0];
690 spec.per = intspec[1];
691 spec.irq = intspec[2];
692
693 err = search_mapping_table(pa, &spec, &apid);
694 if (err)
695 return err;
696
697 pa->apid_to_ppid[apid] = spec.slave << 8 | spec.per;
698
699 /* Keep track of {max,min}_apid for bounding search during interrupt */
700 if (apid > pa->max_apid)
701 pa->max_apid = apid;
702 if (apid < pa->min_apid)
703 pa->min_apid = apid;
704
705 *out_hwirq = spec.slave << 24
706 | spec.per << 16
707 | spec.irq << 8
708 | apid;
709 *out_type = intspec[3] & IRQ_TYPE_SENSE_MASK;
710
711 dev_dbg(&pa->spmic->dev, "out_hwirq = %lu\n", *out_hwirq);
712
713 return 0;
714}
715
716static int qpnpint_irq_domain_map(struct irq_domain *d,
717 unsigned int virq,
718 irq_hw_number_t hwirq)
719{
720 struct spmi_pmic_arb_dev *pa = d->host_data;
721
722 dev_dbg(&pa->spmic->dev, "virq = %u, hwirq = %lu\n", virq, hwirq);
723
724 irq_set_chip_and_handler(virq, &pmic_arb_irqchip, handle_level_irq);
725 irq_set_chip_data(virq, d->host_data);
726 irq_set_noprobe(virq);
727 return 0;
728}
729
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600730/* v1 offset per ee */
Stephen Boyd987a9f12015-11-17 16:13:55 -0800731static int
732pmic_arb_offset_v1(struct spmi_pmic_arb_dev *pa, u8 sid, u16 addr, u32 *offset)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600733{
Stephen Boyd987a9f12015-11-17 16:13:55 -0800734 *offset = 0x800 + 0x80 * pa->channel;
735 return 0;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600736}
737
Stephen Boyd987a9f12015-11-17 16:13:55 -0800738static u16 pmic_arb_find_chan(struct spmi_pmic_arb_dev *pa, u16 ppid)
739{
740 u32 regval, offset;
741 u16 chan;
742 u16 id;
743
744 /*
745 * PMIC_ARB_REG_CHNL is a table in HW mapping channel to ppid.
746 * ppid_to_chan is an in-memory invert of that table.
747 */
748 for (chan = pa->last_channel; ; chan++) {
749 offset = PMIC_ARB_REG_CHNL(chan);
750 if (offset >= pa->core_size)
751 break;
752
753 regval = readl_relaxed(pa->core + offset);
754 if (!regval)
755 continue;
756
757 id = (regval >> 8) & PMIC_ARB_PPID_MASK;
758 pa->ppid_to_chan[id] = chan | PMIC_ARB_CHAN_VALID;
759 if (id == ppid) {
760 chan |= PMIC_ARB_CHAN_VALID;
761 break;
762 }
763 }
764 pa->last_channel = chan & ~PMIC_ARB_CHAN_VALID;
765
766 return chan;
767}
768
769
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600770/* v2 offset per ppid (chan) and per ee */
Stephen Boyd987a9f12015-11-17 16:13:55 -0800771static int
772pmic_arb_offset_v2(struct spmi_pmic_arb_dev *pa, u8 sid, u16 addr, u32 *offset)
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600773{
774 u16 ppid = (sid << 8) | (addr >> 8);
Stephen Boyd987a9f12015-11-17 16:13:55 -0800775 u16 chan;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600776
Stephen Boyd987a9f12015-11-17 16:13:55 -0800777 chan = pa->ppid_to_chan[ppid];
778 if (!(chan & PMIC_ARB_CHAN_VALID))
779 chan = pmic_arb_find_chan(pa, ppid);
780 if (!(chan & PMIC_ARB_CHAN_VALID))
781 return -ENODEV;
782 chan &= ~PMIC_ARB_CHAN_VALID;
783
784 *offset = 0x1000 * pa->ee + 0x8000 * chan;
785 return 0;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600786}
787
788static u32 pmic_arb_fmt_cmd_v1(u8 opc, u8 sid, u16 addr, u8 bc)
789{
790 return (opc << 27) | ((sid & 0xf) << 20) | (addr << 4) | (bc & 0x7);
791}
792
793static u32 pmic_arb_fmt_cmd_v2(u8 opc, u8 sid, u16 addr, u8 bc)
794{
795 return (opc << 27) | ((addr & 0xff) << 4) | (bc & 0x7);
796}
797
798static u32 pmic_arb_owner_acc_status_v1(u8 m, u8 n)
799{
800 return 0x20 * m + 0x4 * n;
801}
802
803static u32 pmic_arb_owner_acc_status_v2(u8 m, u8 n)
804{
805 return 0x100000 + 0x1000 * m + 0x4 * n;
806}
807
808static u32 pmic_arb_acc_enable_v1(u8 n)
809{
810 return 0x200 + 0x4 * n;
811}
812
813static u32 pmic_arb_acc_enable_v2(u8 n)
814{
815 return 0x1000 * n;
816}
817
818static u32 pmic_arb_irq_status_v1(u8 n)
819{
820 return 0x600 + 0x4 * n;
821}
822
823static u32 pmic_arb_irq_status_v2(u8 n)
824{
825 return 0x4 + 0x1000 * n;
826}
827
828static u32 pmic_arb_irq_clear_v1(u8 n)
829{
830 return 0xA00 + 0x4 * n;
831}
832
833static u32 pmic_arb_irq_clear_v2(u8 n)
834{
835 return 0x8 + 0x1000 * n;
836}
837
838static const struct pmic_arb_ver_ops pmic_arb_v1 = {
839 .non_data_cmd = pmic_arb_non_data_cmd_v1,
840 .offset = pmic_arb_offset_v1,
841 .fmt_cmd = pmic_arb_fmt_cmd_v1,
842 .owner_acc_status = pmic_arb_owner_acc_status_v1,
843 .acc_enable = pmic_arb_acc_enable_v1,
844 .irq_status = pmic_arb_irq_status_v1,
845 .irq_clear = pmic_arb_irq_clear_v1,
846};
847
848static const struct pmic_arb_ver_ops pmic_arb_v2 = {
849 .non_data_cmd = pmic_arb_non_data_cmd_v2,
850 .offset = pmic_arb_offset_v2,
851 .fmt_cmd = pmic_arb_fmt_cmd_v2,
852 .owner_acc_status = pmic_arb_owner_acc_status_v2,
853 .acc_enable = pmic_arb_acc_enable_v2,
854 .irq_status = pmic_arb_irq_status_v2,
855 .irq_clear = pmic_arb_irq_clear_v2,
856};
857
Josh Cartwright67b563f2014-02-12 13:44:25 -0600858static const struct irq_domain_ops pmic_arb_irq_domain_ops = {
859 .map = qpnpint_irq_domain_map,
860 .xlate = qpnpint_irq_domain_dt_translate,
861};
862
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600863static int spmi_pmic_arb_probe(struct platform_device *pdev)
864{
865 struct spmi_pmic_arb_dev *pa;
866 struct spmi_controller *ctrl;
867 struct resource *res;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600868 void __iomem *core;
869 u32 channel, ee, hw_ver;
Stephen Boyd987a9f12015-11-17 16:13:55 -0800870 int err;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600871 bool is_v1;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600872
873 ctrl = spmi_controller_alloc(&pdev->dev, sizeof(*pa));
874 if (!ctrl)
875 return -ENOMEM;
876
877 pa = spmi_controller_get_drvdata(ctrl);
Josh Cartwright67b563f2014-02-12 13:44:25 -0600878 pa->spmic = ctrl;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600879
880 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "core");
Stephen Boyd987a9f12015-11-17 16:13:55 -0800881 pa->core_size = resource_size(res);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600882 core = devm_ioremap_resource(&ctrl->dev, res);
883 if (IS_ERR(core)) {
884 err = PTR_ERR(core);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600885 goto err_put_ctrl;
886 }
887
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600888 hw_ver = readl_relaxed(core + PMIC_ARB_VERSION);
889 is_v1 = (hw_ver < PMIC_ARB_VERSION_V2_MIN);
890
891 dev_info(&ctrl->dev, "PMIC Arb Version-%d (0x%x)\n", (is_v1 ? 1 : 2),
892 hw_ver);
893
894 if (is_v1) {
895 pa->ver_ops = &pmic_arb_v1;
896 pa->wr_base = core;
897 pa->rd_base = core;
898 } else {
Stephen Boyd987a9f12015-11-17 16:13:55 -0800899 pa->core = core;
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600900 pa->ver_ops = &pmic_arb_v2;
901
902 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
903 "obsrvr");
904 pa->rd_base = devm_ioremap_resource(&ctrl->dev, res);
905 if (IS_ERR(pa->rd_base)) {
906 err = PTR_ERR(pa->rd_base);
907 goto err_put_ctrl;
908 }
909
910 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
911 "chnls");
912 pa->wr_base = devm_ioremap_resource(&ctrl->dev, res);
913 if (IS_ERR(pa->wr_base)) {
914 err = PTR_ERR(pa->wr_base);
915 goto err_put_ctrl;
916 }
917
Stephen Boyd987a9f12015-11-17 16:13:55 -0800918 pa->ppid_to_chan = devm_kcalloc(&ctrl->dev,
919 PMIC_ARB_MAX_PPID,
920 sizeof(*pa->ppid_to_chan),
921 GFP_KERNEL);
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600922 if (!pa->ppid_to_chan) {
923 err = -ENOMEM;
924 goto err_put_ctrl;
925 }
Gilad Avidovd0c6ae42015-03-25 11:37:32 -0600926 }
927
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600928 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "intr");
929 pa->intr = devm_ioremap_resource(&ctrl->dev, res);
930 if (IS_ERR(pa->intr)) {
931 err = PTR_ERR(pa->intr);
932 goto err_put_ctrl;
933 }
934
935 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cnfg");
936 pa->cnfg = devm_ioremap_resource(&ctrl->dev, res);
937 if (IS_ERR(pa->cnfg)) {
938 err = PTR_ERR(pa->cnfg);
939 goto err_put_ctrl;
940 }
941
Josh Cartwright67b563f2014-02-12 13:44:25 -0600942 pa->irq = platform_get_irq_byname(pdev, "periph_irq");
943 if (pa->irq < 0) {
944 err = pa->irq;
945 goto err_put_ctrl;
946 }
947
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600948 err = of_property_read_u32(pdev->dev.of_node, "qcom,channel", &channel);
949 if (err) {
950 dev_err(&pdev->dev, "channel unspecified.\n");
951 goto err_put_ctrl;
952 }
953
954 if (channel > 5) {
955 dev_err(&pdev->dev, "invalid channel (%u) specified.\n",
956 channel);
Christophe JAILLETe98cc182016-09-26 22:24:46 +0200957 err = -EINVAL;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600958 goto err_put_ctrl;
959 }
960
961 pa->channel = channel;
962
Josh Cartwright67b563f2014-02-12 13:44:25 -0600963 err = of_property_read_u32(pdev->dev.of_node, "qcom,ee", &ee);
964 if (err) {
965 dev_err(&pdev->dev, "EE unspecified.\n");
966 goto err_put_ctrl;
967 }
968
969 if (ee > 5) {
970 dev_err(&pdev->dev, "invalid EE (%u) specified\n", ee);
971 err = -EINVAL;
972 goto err_put_ctrl;
973 }
974
975 pa->ee = ee;
976
Stephen Boyd987a9f12015-11-17 16:13:55 -0800977 pa->apid_to_ppid = devm_kcalloc(&ctrl->dev, PMIC_ARB_MAX_PERIPHS,
978 sizeof(*pa->apid_to_ppid),
979 GFP_KERNEL);
980 if (!pa->apid_to_ppid) {
981 err = -ENOMEM;
982 goto err_put_ctrl;
983 }
984
985 pa->mapping_table = devm_kcalloc(&ctrl->dev, PMIC_ARB_MAX_PERIPHS - 1,
986 sizeof(*pa->mapping_table), GFP_KERNEL);
987 if (!pa->mapping_table) {
988 err = -ENOMEM;
989 goto err_put_ctrl;
990 }
Josh Cartwright67b563f2014-02-12 13:44:25 -0600991
992 /* Initialize max_apid/min_apid to the opposite bounds, during
993 * the irq domain translation, we are sure to update these */
994 pa->max_apid = 0;
995 pa->min_apid = PMIC_ARB_MAX_PERIPHS - 1;
996
Kenneth Heitke39ae93e2014-02-12 13:44:24 -0600997 platform_set_drvdata(pdev, ctrl);
998 raw_spin_lock_init(&pa->lock);
999
1000 ctrl->cmd = pmic_arb_cmd;
1001 ctrl->read_cmd = pmic_arb_read_cmd;
1002 ctrl->write_cmd = pmic_arb_write_cmd;
1003
Josh Cartwright67b563f2014-02-12 13:44:25 -06001004 dev_dbg(&pdev->dev, "adding irq domain\n");
1005 pa->domain = irq_domain_add_tree(pdev->dev.of_node,
1006 &pmic_arb_irq_domain_ops, pa);
1007 if (!pa->domain) {
1008 dev_err(&pdev->dev, "unable to create irq_domain\n");
1009 err = -ENOMEM;
1010 goto err_put_ctrl;
1011 }
1012
Thomas Gleixnerfb68ba62015-07-13 20:52:24 +00001013 irq_set_chained_handler_and_data(pa->irq, pmic_arb_chained_irq, pa);
Josh Cartwright67b563f2014-02-12 13:44:25 -06001014
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001015 err = spmi_controller_add(ctrl);
1016 if (err)
Josh Cartwright67b563f2014-02-12 13:44:25 -06001017 goto err_domain_remove;
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001018
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001019 return 0;
1020
Josh Cartwright67b563f2014-02-12 13:44:25 -06001021err_domain_remove:
Thomas Gleixnerfb68ba62015-07-13 20:52:24 +00001022 irq_set_chained_handler_and_data(pa->irq, NULL, NULL);
Josh Cartwright67b563f2014-02-12 13:44:25 -06001023 irq_domain_remove(pa->domain);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001024err_put_ctrl:
1025 spmi_controller_put(ctrl);
1026 return err;
1027}
1028
1029static int spmi_pmic_arb_remove(struct platform_device *pdev)
1030{
1031 struct spmi_controller *ctrl = platform_get_drvdata(pdev);
Josh Cartwright67b563f2014-02-12 13:44:25 -06001032 struct spmi_pmic_arb_dev *pa = spmi_controller_get_drvdata(ctrl);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001033 spmi_controller_remove(ctrl);
Thomas Gleixnerfb68ba62015-07-13 20:52:24 +00001034 irq_set_chained_handler_and_data(pa->irq, NULL, NULL);
Josh Cartwright67b563f2014-02-12 13:44:25 -06001035 irq_domain_remove(pa->domain);
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001036 spmi_controller_put(ctrl);
1037 return 0;
1038}
1039
1040static const struct of_device_id spmi_pmic_arb_match_table[] = {
1041 { .compatible = "qcom,spmi-pmic-arb", },
1042 {},
1043};
1044MODULE_DEVICE_TABLE(of, spmi_pmic_arb_match_table);
1045
1046static struct platform_driver spmi_pmic_arb_driver = {
1047 .probe = spmi_pmic_arb_probe,
1048 .remove = spmi_pmic_arb_remove,
1049 .driver = {
1050 .name = "spmi_pmic_arb",
Kenneth Heitke39ae93e2014-02-12 13:44:24 -06001051 .of_match_table = spmi_pmic_arb_match_table,
1052 },
1053};
1054module_platform_driver(spmi_pmic_arb_driver);
1055
1056MODULE_LICENSE("GPL v2");
1057MODULE_ALIAS("platform:spmi_pmic_arb");