blob: 422e99e07dec401dc41d01c92aad2fa9344c6874 [file] [log] [blame]
Kenneth Heitke65a5ad22012-02-08 14:00:04 -07001/* Copyright (c) 2012, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#define pr_fmt(fmt) "%s: " fmt, __func__
14
15#include <linux/delay.h>
16#include <linux/err.h>
17#include <linux/io.h>
18#include <linux/kernel.h>
19#include <linux/platform_device.h>
20#include <linux/slab.h>
21#include <linux/spmi.h>
22#include <linux/of.h>
23#include <linux/interrupt.h>
24#include <linux/of_spmi.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070025#include <linux/module.h>
Kenneth Heitke65a5ad22012-02-08 14:00:04 -070026#include <mach/qpnp-int.h>
27
28#define SPMI_PMIC_ARB_NAME "spmi_pmic_arb"
29
30/* PMIC Arbiter configuration registers */
31#define PMIC_ARB_VERSION 0x0000
32#define PMIC_ARB_INT_EN 0x0004
33
34/* PMIC Arbiter channel registers */
35#define PMIC_ARB_CMD(N) (0x0800 + (0x80 * (N)))
36#define PMIC_ARB_CONFIG(N) (0x0804 + (0x80 * (N)))
37#define PMIC_ARB_STATUS(N) (0x0808 + (0x80 * (N)))
38#define PMIC_ARB_WDATA0(N) (0x0810 + (0x80 * (N)))
39#define PMIC_ARB_WDATA1(N) (0x0814 + (0x80 * (N)))
40#define PMIC_ARB_RDATA0(N) (0x0818 + (0x80 * (N)))
41#define PMIC_ARB_RDATA1(N) (0x081C + (0x80 * (N)))
42
43/* Interrupt Controller */
44#define SPMI_PIC_OWNER_ACC_STATUS(M, N) (0x0000 + ((32 * (M)) + (4 * (N))))
45#define SPMI_PIC_ACC_ENABLE(N) (0x0200 + (4 * (N)))
46#define SPMI_PIC_IRQ_STATUS(N) (0x0600 + (4 * (N)))
47#define SPMI_PIC_IRQ_CLEAR(N) (0x0A00 + (4 * (N)))
48
49/* Channel Status fields */
50enum pmic_arb_chnl_status {
51 PMIC_ARB_STATUS_DONE = (1 << 0),
52 PMIC_ARB_STATUS_FAILURE = (1 << 1),
53 PMIC_ARB_STATUS_DENIED = (1 << 2),
54 PMIC_ARB_STATUS_DROPPED = (1 << 3),
55};
56
57/* Command register fields */
58#define PMIC_ARB_CMD_MAX_BYTE_COUNT 8
59
60/* Command Opcodes */
61enum pmic_arb_cmd_op_code {
62 PMIC_ARB_OP_EXT_WRITEL = 0,
63 PMIC_ARB_OP_EXT_READL = 1,
64 PMIC_ARB_OP_EXT_WRITE = 2,
65 PMIC_ARB_OP_RESET = 3,
66 PMIC_ARB_OP_SLEEP = 4,
67 PMIC_ARB_OP_SHUTDOWN = 5,
68 PMIC_ARB_OP_WAKEUP = 6,
69 PMIC_ARB_OP_AUTHENTICATE = 7,
70 PMIC_ARB_OP_MSTR_READ = 8,
71 PMIC_ARB_OP_MSTR_WRITE = 9,
72 PMIC_ARB_OP_EXT_READ = 13,
73 PMIC_ARB_OP_WRITE = 14,
74 PMIC_ARB_OP_READ = 15,
75 PMIC_ARB_OP_ZERO_WRITE = 16,
76};
77
78/* Maximum number of support PMIC peripherals */
79#define PMIC_ARB_MAX_PERIPHS 256
80#define PMIC_ARB_PERIPH_ID_VALID (1 << 15)
81#define PMIC_ARB_TIMEOUT_US 100
Gilad Avidova11c0b52012-02-15 15:30:49 -070082
83#define PMIC_ARB_APID_MASK 0xFF
84#define PMIC_ARB_PPID_MASK 0xFFF
85/* extract PPID and APID from interrupt map in .dts config file format */
86#define PMIC_ARB_DEV_TRE_2_PPID(MAP_COMPRS_VAL) \
87 ((MAP_COMPRS_VAL) >> (20))
88#define PMIC_ARB_DEV_TRE_2_APID(MAP_COMPRS_VAL) \
89 ((MAP_COMPRS_VAL) & PMIC_ARB_APID_MASK)
Kenneth Heitke65a5ad22012-02-08 14:00:04 -070090
91/**
92 * base - base address of the PMIC Arbiter core registers.
93 * intr - base address of the SPMI interrupt control registers
94 */
95struct spmi_pmic_arb_dev {
96 struct spmi_controller controller;
97 struct device *dev;
98 struct device *slave;
99 void __iomem *base;
100 void __iomem *intr;
101 int pic_irq;
Kenneth Heitke65a5ad22012-02-08 14:00:04 -0700102 spinlock_t lock;
103 u8 owner;
104 u8 channel;
105 u8 min_apid;
106 u8 max_apid;
107 u16 periph_id_map[PMIC_ARB_MAX_PERIPHS];
108};
109
110static u32 pmic_arb_read(struct spmi_pmic_arb_dev *dev, u32 offset)
111{
112 u32 val = readl_relaxed(dev->base + offset);
113 pr_debug("address 0x%p, val 0x%x\n", dev->base + offset, val);
114 return val;
115}
116
117static void pmic_arb_write(struct spmi_pmic_arb_dev *dev, u32 offset, u32 val)
118{
119 pr_debug("address 0x%p, val 0x%x\n", dev->base + offset, val);
120 writel_relaxed(val, dev->base + offset);
121}
122
123static int pmic_arb_wait_for_done(struct spmi_pmic_arb_dev *dev)
124{
125 u32 status = 0;
126 u32 timeout = PMIC_ARB_TIMEOUT_US;
127 u32 offset = PMIC_ARB_STATUS(dev->channel);
128
129 while (timeout--) {
130 status = pmic_arb_read(dev, offset);
131
132 if (status & PMIC_ARB_STATUS_DONE) {
133 if (status & PMIC_ARB_STATUS_DENIED) {
134 dev_err(dev->dev,
135 "%s: transaction denied (0x%x)\n",
136 __func__, status);
137 return -EPERM;
138 }
139
140 if (status & PMIC_ARB_STATUS_FAILURE) {
141 dev_err(dev->dev,
142 "%s: transaction failed (0x%x)\n",
143 __func__, status);
144 return -EIO;
145 }
146
147 if (status & PMIC_ARB_STATUS_DROPPED) {
148 dev_err(dev->dev,
149 "%s: transaction dropped (0x%x)\n",
150 __func__, status);
151 return -EIO;
152 }
153
154 return 0;
155 }
156 udelay(1);
157 }
158
159 dev_err(dev->dev, "%s: timeout, status 0x%x\n", __func__, status);
160 return -ETIMEDOUT;
161}
162
163static void pa_read_data(struct spmi_pmic_arb_dev *dev, u8 *buf, u32 reg, u8 bc)
164{
165 u32 data = pmic_arb_read(dev, reg);
166
167 switch (bc & 0x3) {
168 case 3:
169 *buf++ = data & 0xff;
170 data >>= 8;
171 case 2:
172 *buf++ = data & 0xff;
173 data >>= 8;
174 case 1:
175 *buf++ = data & 0xff;
176 data >>= 8;
177 case 0:
178 *buf++ = data & 0xff;
179 default:
180 break;
181 }
182}
183
184static void
185pa_write_data(struct spmi_pmic_arb_dev *dev, u8 *buf, u32 reg, u8 bc)
186{
187 u32 data = 0;
188
189 switch (bc & 0x3) {
190 case 3:
191 data = (buf[0]|buf[1]<<8|buf[2]<<16|buf[3]<<24);
192 break;
193 case 2:
194 data = (buf[0]|buf[1]<<8|buf[2]<<16);
195 break;
196 case 1:
197 data = (buf[0]|buf[1]<<8);
198 break;
199 case 0:
200 data = (buf[0]);
201 break;
202 default:
203 break;
204 }
205
206 pmic_arb_write(dev, reg, data);
207}
208
209/* Non-data command */
210static int pmic_arb_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid)
211{
212 struct spmi_pmic_arb_dev *pmic_arb = spmi_get_ctrldata(ctrl);
213 unsigned long flags;
214 u32 cmd;
215 int rc;
216
217 pr_debug("op:0x%x sid:%d\n", opc, sid);
218
219 /* Check for valid non-data command */
220 if (opc < SPMI_CMD_RESET || opc > SPMI_CMD_WAKEUP)
221 return -EINVAL;
222
223 cmd = ((opc | 0x40) << 27) | ((sid & 0xf) << 20);
224
225 spin_lock_irqsave(&pmic_arb->lock, flags);
226 pmic_arb_write(pmic_arb, PMIC_ARB_CMD(pmic_arb->channel), cmd);
227 rc = pmic_arb_wait_for_done(pmic_arb);
228 spin_unlock_irqrestore(&pmic_arb->lock, flags);
229
230 return rc;
231}
232
233static int pmic_arb_read_cmd(struct spmi_controller *ctrl,
234 u8 opc, u8 sid, u16 addr, u8 bc, u8 *buf)
235{
236 struct spmi_pmic_arb_dev *pmic_arb = spmi_get_ctrldata(ctrl);
237 unsigned long flags;
238 u32 cmd;
239 int rc;
240
241 pr_debug("op:0x%x sid:%d bc:%d addr:0x%x\n", opc, sid, bc, addr);
242
243 /* Check the opcode */
244 if (opc >= 0x60 && opc <= 0x7F)
245 opc = PMIC_ARB_OP_READ;
246 else if (opc >= 0x20 && opc <= 0x2F)
247 opc = PMIC_ARB_OP_EXT_READ;
248 else if (opc >= 0x38 && opc <= 0x3F)
249 opc = PMIC_ARB_OP_EXT_READL;
250 else
251 return -EINVAL;
252
253 cmd = (opc << 27) | ((sid & 0xf) << 20) | (addr << 4) | (bc & 0x7);
254
255 spin_lock_irqsave(&pmic_arb->lock, flags);
256 pmic_arb_write(pmic_arb, PMIC_ARB_CMD(pmic_arb->channel), cmd);
257 rc = pmic_arb_wait_for_done(pmic_arb);
258 if (rc)
259 goto done;
260
261 /* Read from FIFO, note 'bc' is actually number of bytes minus 1 */
262 pa_read_data(pmic_arb, buf, PMIC_ARB_RDATA0(pmic_arb->channel), bc);
263
264 if (bc > 3)
265 pa_read_data(pmic_arb, buf + 4,
266 PMIC_ARB_RDATA1(pmic_arb->channel), bc);
267
268done:
269 spin_unlock_irqrestore(&pmic_arb->lock, flags);
270 return rc;
271}
272
273static int pmic_arb_write_cmd(struct spmi_controller *ctrl,
274 u8 opc, u8 sid, u16 addr, u8 bc, u8 *buf)
275{
276 struct spmi_pmic_arb_dev *pmic_arb = spmi_get_ctrldata(ctrl);
277 unsigned long flags;
278 u32 cmd;
279 int rc;
280
281 pr_debug("op:0x%x sid:%d bc:%d addr:0x%x\n", opc, sid, bc, addr);
282
283 /* Check the opcode */
284 if (opc >= 0x40 && opc <= 0x5F)
285 opc = PMIC_ARB_OP_WRITE;
286 else if (opc >= 0x00 && opc <= 0x0F)
287 opc = PMIC_ARB_OP_EXT_WRITE;
288 else if (opc >= 0x30 && opc <= 0x37)
289 opc = PMIC_ARB_OP_EXT_WRITEL;
290 else if (opc >= 0x80 && opc <= 0xFF)
291 opc = PMIC_ARB_OP_ZERO_WRITE;
292 else
293 return -EINVAL;
294
295 cmd = (opc << 27) | ((sid & 0xf) << 20) | (addr << 4) | (bc & 0x7);
296
297 /* Write data to FIFOs */
298 spin_lock_irqsave(&pmic_arb->lock, flags);
299 pa_write_data(pmic_arb, buf, PMIC_ARB_WDATA0(pmic_arb->channel), bc);
300
301 if (bc > 3)
302 pa_write_data(pmic_arb, buf + 4,
303 PMIC_ARB_WDATA1(pmic_arb->channel), bc);
304
305 /* Start the transaction */
306 pmic_arb_write(pmic_arb, PMIC_ARB_CMD(pmic_arb->channel), cmd);
307 rc = pmic_arb_wait_for_done(pmic_arb);
308 spin_unlock_irqrestore(&pmic_arb->lock, flags);
309
310 return rc;
311}
312
313/* APID to PPID */
314static u16 get_peripheral_id(struct spmi_pmic_arb_dev *pmic_arb, u8 apid)
315{
316 return pmic_arb->periph_id_map[apid] & PMIC_ARB_PPID_MASK;
317}
318
319/* APID to PPID, returns valid flag */
320static int is_apid_valid(struct spmi_pmic_arb_dev *pmic_arb, u8 apid)
321{
322 return pmic_arb->periph_id_map[apid] & PMIC_ARB_PERIPH_ID_VALID;
323}
324
325/* PPID to APID */
326static uint32_t map_peripheral_id(struct spmi_pmic_arb_dev *pmic_arb, u16 ppid)
327{
328 int first = pmic_arb->min_apid;
329 int last = pmic_arb->max_apid;
330 int i;
331
332 /* Search table for a matching PPID */
333 for (i = first; i <= last; ++i) {
334 if ((pmic_arb->periph_id_map[i] & PMIC_ARB_PPID_MASK) == ppid)
335 return i;
336 }
337
338 dev_err(pmic_arb->dev, "Unknown ppid 0x%x\n", ppid);
339 return PMIC_ARB_MAX_PERIPHS;
340}
341
342/* Enable interrupt at the PMIC Arbiter PIC */
343static int pmic_arb_pic_enable(struct spmi_controller *ctrl,
344 struct qpnp_irq_spec *spec, uint32_t data)
345{
346 struct spmi_pmic_arb_dev *pmic_arb = spmi_get_ctrldata(ctrl);
347 u8 apid = data & PMIC_ARB_APID_MASK;
348 unsigned long flags;
349 u32 status;
350
351 dev_dbg(pmic_arb->dev, "PIC enable, apid:0x%x, sid:0x%x, pid:0x%x\n",
352 apid, spec->slave, spec->per);
353
354 if (data < pmic_arb->min_apid || data > pmic_arb->max_apid) {
355 dev_err(pmic_arb->dev, "int enable: invalid APID %d\n", data);
356 return -EINVAL;
357 }
358
359 if (!is_apid_valid(pmic_arb, apid)) {
360 dev_err(pmic_arb->dev, "int enable: int not supported\n");
361 return -EINVAL;
362 }
363
364 spin_lock_irqsave(&pmic_arb->lock, flags);
365 status = readl_relaxed(pmic_arb->intr + SPMI_PIC_ACC_ENABLE(apid));
366 if (!status) {
Kenneth Heitke65a5ad22012-02-08 14:00:04 -0700367 writel_relaxed(0x1, pmic_arb->intr + SPMI_PIC_ACC_ENABLE(apid));
368 /* Interrupt needs to be enabled before returning to caller */
369 wmb();
370 }
371 spin_unlock_irqrestore(&pmic_arb->lock, flags);
372 return 0;
373}
374
375/* Disable interrupt at the PMIC Arbiter PIC */
376static int pmic_arb_pic_disable(struct spmi_controller *ctrl,
377 struct qpnp_irq_spec *spec, uint32_t data)
378{
379 struct spmi_pmic_arb_dev *pmic_arb = spmi_get_ctrldata(ctrl);
380 u8 apid = data & PMIC_ARB_APID_MASK;
381 unsigned long flags;
382 u32 status;
383
384 dev_dbg(pmic_arb->dev, "PIC disable, apid:0x%x, sid:0x%x, pid:0x%x\n",
385 apid, spec->slave, spec->per);
386
387 if (data < pmic_arb->min_apid || data > pmic_arb->max_apid) {
388 dev_err(pmic_arb->dev, "int disable: invalid APID %d\n", data);
389 return -EINVAL;
390 }
391
392 if (!is_apid_valid(pmic_arb, apid)) {
393 dev_err(pmic_arb->dev, "int disable: int not supported\n");
394 return -EINVAL;
395 }
396
397 spin_lock_irqsave(&pmic_arb->lock, flags);
398 status = readl_relaxed(pmic_arb->intr + SPMI_PIC_ACC_ENABLE(apid));
399 if (status) {
Kenneth Heitke65a5ad22012-02-08 14:00:04 -0700400 writel_relaxed(0x0, pmic_arb->intr + SPMI_PIC_ACC_ENABLE(apid));
401 /* Interrupt needs to be disabled before returning to caller */
402 wmb();
403 }
404 spin_unlock_irqrestore(&pmic_arb->lock, flags);
405 return 0;
406}
407
408static irqreturn_t
409periph_interrupt(struct spmi_pmic_arb_dev *pmic_arb, u8 apid)
410{
411 u16 ppid = get_peripheral_id(pmic_arb, apid);
412 void __iomem *base = pmic_arb->intr;
413 u8 sid = (ppid >> 8) & 0x0F;
414 u8 pid = ppid & 0xFF;
415 u32 status;
416 int i;
417
418 if (!is_apid_valid(pmic_arb, apid)) {
419 dev_err(pmic_arb->dev, "unknown peripheral id 0x%x\n", ppid);
420 /* return IRQ_NONE; */
421 }
422
423 /* Read the peripheral specific interrupt bits */
424 status = readl_relaxed(base + SPMI_PIC_IRQ_STATUS(apid));
425
426 /* Clear the peripheral interrupts */
427 writel_relaxed(status, base + SPMI_PIC_IRQ_CLEAR(apid));
428 /* Interrupt needs to be cleared/acknowledged before exiting ISR */
429 mb();
430
431 dev_dbg(pmic_arb->dev,
432 "interrupt, apid:0x%x, sid:0x%x, pid:0x%x, intr:0x%x\n",
433 apid, sid, pid, status);
434
435 /* Send interrupt notification */
436 for (i = 0; status && i < 8; ++i, status >>= 1) {
437 if (status & 0x1) {
438 struct qpnp_irq_spec irq_spec = {
439 .slave = sid,
440 .per = pid,
441 .irq = i,
442 };
443 qpnpint_handle_irq(&pmic_arb->controller, &irq_spec);
444 }
445 }
446 return IRQ_HANDLED;
447}
448
449/* Peripheral interrupt handler */
450static irqreturn_t pmic_arb_periph_irq(int irq, void *dev_id)
451{
452 struct spmi_pmic_arb_dev *pmic_arb = dev_id;
453 void __iomem *intr = pmic_arb->intr;
454 u8 ee = pmic_arb->owner;
455 u32 ret = IRQ_NONE;
456 u32 status;
457
458 int first = pmic_arb->min_apid >> 5;
459 int last = pmic_arb->max_apid >> 5;
460 int i, j;
461
462 dev_dbg(pmic_arb->dev, "Peripheral interrupt detected\n");
463
464 /* Check the accumulated interrupt status */
465 for (i = first; i <= last; ++i) {
466 status = readl_relaxed(intr + SPMI_PIC_OWNER_ACC_STATUS(ee, i));
467
468 for (j = 0; status && j < 32; ++j, status >>= 1) {
469 if (status & 0x1) {
470 u8 id = (i * 32) + j;
471 ret |= periph_interrupt(pmic_arb, id);
472 }
473 }
474 }
475
476 return ret;
477}
478
479/* Callback to register an APID for specific slave/peripheral */
480static int pmic_arb_intr_priv_data(struct spmi_controller *ctrl,
481 struct qpnp_irq_spec *spec, uint32_t *data)
482{
483 struct spmi_pmic_arb_dev *pmic_arb = spmi_get_ctrldata(ctrl);
484 u16 ppid = ((spec->slave & 0x0F) << 8) | (spec->per & 0xFF);
485 *data = map_peripheral_id(pmic_arb, ppid);
486 return 0;
487}
488
489static int __devinit
490spmi_pmic_arb_get_property(struct platform_device *pdev, char *pname, u32 *prop)
491{
492 int ret = of_property_read_u32(pdev->dev.of_node, pname, prop);
493
494 if (ret)
495 dev_err(&pdev->dev, "missing property: %s\n", pname);
496 else
497 pr_debug("%s = 0x%x\n", pname, *prop);
498
499 return ret;
500}
501
502static int __devinit spmi_pmic_arb_get_map_data(struct platform_device *pdev,
503 struct spmi_pmic_arb_dev *pmic_arb)
504{
505 int i;
506 int ret;
507 int map_size;
508 u32 *map_data;
Gilad Avidova11c0b52012-02-15 15:30:49 -0700509 const int map_width = sizeof(*map_data);
Kenneth Heitke65a5ad22012-02-08 14:00:04 -0700510 const struct device_node *of_node = pdev->dev.of_node;
511
512 /* Get size of the mapping table (in bytes) */
513 if (!of_get_property(of_node, "qcom,pmic-arb-ppid-map", &map_size)) {
514 dev_err(&pdev->dev, "missing ppid mapping table\n");
515 return -ENODEV;
516 }
517
518 /* Map size can't exceed the maximum number of peripherals */
Gilad Avidova11c0b52012-02-15 15:30:49 -0700519 if (map_size == 0 || map_size > map_width * PMIC_ARB_MAX_PERIPHS) {
Kenneth Heitke65a5ad22012-02-08 14:00:04 -0700520 dev_err(&pdev->dev, "map size of %d is not valid\n", map_size);
521 return -ENODEV;
522 }
523
524 map_data = kzalloc(map_size, GFP_KERNEL);
525 if (!map_data) {
526 dev_err(&pdev->dev, "can not allocate map data\n");
527 return -ENOMEM;
528 }
529
530 ret = of_property_read_u32_array(of_node,
531 "qcom,pmic-arb-ppid-map", map_data, map_size/sizeof(u32));
532 if (ret) {
533 dev_err(&pdev->dev, "invalid or missing property: ppid-map\n");
534 goto err;
535 };
536
537 pmic_arb->max_apid = 0;
538 pmic_arb->min_apid = PMIC_ARB_MAX_PERIPHS - 1;
539
540 /* Build the mapping table from the data */
541 for (i = 0; i < map_size/sizeof(u32);) {
Gilad Avidova11c0b52012-02-15 15:30:49 -0700542 u32 map_compressed_val = map_data[i++];
543 u32 ppid = PMIC_ARB_DEV_TRE_2_PPID(map_compressed_val) ;
544 u32 apid = PMIC_ARB_DEV_TRE_2_APID(map_compressed_val) ;
Kenneth Heitke65a5ad22012-02-08 14:00:04 -0700545
546 if (pmic_arb->periph_id_map[apid] & PMIC_ARB_PERIPH_ID_VALID)
547 dev_warn(&pdev->dev, "duplicate APID 0x%x\n", apid);
548
549 pmic_arb->periph_id_map[apid] = ppid | PMIC_ARB_PERIPH_ID_VALID;
550
551 if (apid > pmic_arb->max_apid)
552 pmic_arb->max_apid = apid;
553
554 if (apid < pmic_arb->min_apid)
555 pmic_arb->min_apid = apid;
556 }
557
558 pr_debug("%d value(s) mapped, min:%d, max:%d\n",
559 map_size/map_width, pmic_arb->min_apid, pmic_arb->max_apid);
560
561err:
562 kfree(map_data);
563 return ret;
564}
565
566static struct qpnp_local_int spmi_pmic_arb_intr_cb = {
567 .mask = pmic_arb_pic_disable,
568 .unmask = pmic_arb_pic_enable,
569 .register_priv_data = pmic_arb_intr_priv_data,
570};
571
572static int __devinit spmi_pmic_arb_probe(struct platform_device *pdev)
573{
574 struct spmi_pmic_arb_dev *pmic_arb;
575 struct resource *mem_res;
576 u32 cell_index;
577 u32 prop;
578 int ret = 0;
579
580 pr_debug("SPMI PMIC Arbiter\n");
581
582 pmic_arb = devm_kzalloc(&pdev->dev,
583 sizeof(struct spmi_pmic_arb_dev), GFP_KERNEL);
584 if (!pmic_arb) {
585 dev_err(&pdev->dev, "can not allocate pmic_arb data\n");
586 return -ENOMEM;
587 }
588
589 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
590 if (!mem_res) {
591 dev_err(&pdev->dev, "missing base memory resource\n");
592 return -ENODEV;
593 }
594
595 pmic_arb->base = devm_ioremap(&pdev->dev,
596 mem_res->start, resource_size(mem_res));
597 if (!pmic_arb->base) {
598 dev_err(&pdev->dev, "ioremap of 'base' failed\n");
599 return -ENOMEM;
600 }
601
602 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
603 if (!mem_res) {
604 dev_err(&pdev->dev, "missing mem resource (interrupts)\n");
605 return -ENODEV;
606 }
607
608 pmic_arb->intr = devm_ioremap(&pdev->dev,
609 mem_res->start, resource_size(mem_res));
610 if (!pmic_arb->intr) {
611 dev_err(&pdev->dev, "ioremap of 'intr' failed\n");
612 return -ENOMEM;
613 }
614
615 pmic_arb->pic_irq = platform_get_irq(pdev, 0);
616 if (!pmic_arb->pic_irq) {
617 dev_err(&pdev->dev, "missing IRQ resource\n");
618 return -ENODEV;
619 }
620
621 ret = devm_request_irq(&pdev->dev, pmic_arb->pic_irq,
622 pmic_arb_periph_irq, IRQF_TRIGGER_HIGH, pdev->name, pmic_arb);
623 if (ret) {
624 dev_err(&pdev->dev, "request IRQ failed\n");
625 return ret;
626 }
Kenneth Heitke65a5ad22012-02-08 14:00:04 -0700627
628 /* Get properties from the device tree */
629 ret = spmi_pmic_arb_get_property(pdev, "cell-index", &cell_index);
630 if (ret)
631 return -ENODEV;
632
633 ret = spmi_pmic_arb_get_map_data(pdev, pmic_arb);
634 if (ret)
635 return ret;
636
637 ret = spmi_pmic_arb_get_property(pdev, "qcom,pmic-arb-ee", &prop);
638 if (ret)
639 return -ENODEV;
640 pmic_arb->owner = (u8)prop;
641
642 ret = spmi_pmic_arb_get_property(pdev, "qcom,pmic-arb-channel", &prop);
643 if (ret)
644 return -ENODEV;
645 pmic_arb->channel = (u8)prop;
646
647 pmic_arb->dev = &pdev->dev;
648 platform_set_drvdata(pdev, pmic_arb);
649 spmi_set_ctrldata(&pmic_arb->controller, pmic_arb);
650
651 spin_lock_init(&pmic_arb->lock);
652
653 pmic_arb->controller.nr = cell_index;
654 pmic_arb->controller.dev.parent = pdev->dev.parent;
655 pmic_arb->controller.dev.of_node = of_node_get(pdev->dev.of_node);
656
657 /* Callbacks */
658 pmic_arb->controller.cmd = pmic_arb_cmd;
659 pmic_arb->controller.read_cmd = pmic_arb_read_cmd;
660 pmic_arb->controller.write_cmd = pmic_arb_write_cmd;
661
662 ret = spmi_add_controller(&pmic_arb->controller);
663 if (ret)
664 goto err_add_controller;
665
666 /* Register the interrupt enable/disable functions */
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700667 ret = qpnpint_register_controller(pmic_arb->controller.dev.of_node,
668 &pmic_arb->controller,
669 &spmi_pmic_arb_intr_cb);
670 if (ret) {
671 dev_err(&pdev->dev, "Unable to register controller %d\n",
672 cell_index);
673 goto err_reg_controller;
674 }
Kenneth Heitke65a5ad22012-02-08 14:00:04 -0700675
676 /* Register device(s) from the device tree */
677 of_spmi_register_devices(&pmic_arb->controller);
678
679 pr_debug("PMIC Arb Version 0x%x\n",
680 pmic_arb_read(pmic_arb, PMIC_ARB_VERSION));
681
682 return 0;
683
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700684err_reg_controller:
685 spmi_del_controller(&pmic_arb->controller);
Kenneth Heitke65a5ad22012-02-08 14:00:04 -0700686err_add_controller:
687 platform_set_drvdata(pdev, NULL);
688 return ret;
689}
690
691static int __devexit spmi_pmic_arb_remove(struct platform_device *pdev)
692{
693 struct spmi_pmic_arb_dev *pmic_arb = platform_get_drvdata(pdev);
694
695 free_irq(pmic_arb->pic_irq, pmic_arb);
696 platform_set_drvdata(pdev, NULL);
697 spmi_del_controller(&pmic_arb->controller);
698 return 0;
699}
700
701static struct of_device_id spmi_pmic_arb_match_table[] = {
702 { .compatible = "qcom,spmi-pmic-arb",
703 },
704 {}
705};
706
707static struct platform_driver spmi_pmic_arb_driver = {
708 .probe = spmi_pmic_arb_probe,
709 .remove = __exit_p(spmi_pmic_arb_remove),
710 .driver = {
711 .name = SPMI_PMIC_ARB_NAME,
712 .owner = THIS_MODULE,
713 .of_match_table = spmi_pmic_arb_match_table,
714 },
715};
716
717static int __init spmi_pmic_arb_init(void)
718{
719 return platform_driver_register(&spmi_pmic_arb_driver);
720}
721postcore_initcall(spmi_pmic_arb_init);
722
723static void __exit spmi_pmic_arb_exit(void)
724{
725 platform_driver_unregister(&spmi_pmic_arb_driver);
726}
727module_exit(spmi_pmic_arb_exit);
728
729MODULE_LICENSE("GPL v2");
730MODULE_VERSION("1.0");
731MODULE_ALIAS("platform:spmi_pmic_arb");