kernel: backport bcma patches queued for 3.20
[openwrt/svn-archive/archive.git] / target / linux / ipq806x / patches / 0056-spmi-Add-MSM-PMIC-Arbiter-SPMI-controller.patch
1 From 1e1f53fbc2848b23af572c16d19e8004f6a7c9c1 Mon Sep 17 00:00:00 2001
2 From: Kenneth Heitke <kheitke@codeaurora.org>
3 Date: Wed, 12 Feb 2014 13:44:24 -0600
4 Subject: [PATCH 056/182] spmi: Add MSM PMIC Arbiter SPMI controller
5
6 Qualcomm's PMIC Arbiter SPMI controller functions as a bus master and
7 is used to communication with one or more PMIC (slave) devices on the
8 SPMI bus. The PMIC Arbiter is actually a hardware wrapper around the
9 SPMI controller that provides concurrent and autonomous PMIC access
10 to various entities that need to communicate with the PMIC.
11
12 The SPMI controller hardware handles all of the SPMI bus activity (bus
13 arbitration, sequence start condition, transmission of frames, etc).
14 This software driver uses the PMIC Arbiter register interface to
15 initiate command sequences on the SPMI bus. The status register is
16 read to determine when the command sequence has completed and whether
17 or not it completed successfully.
18
19 Signed-off-by: Kenneth Heitke <kheitke@codeaurora.org>
20 Signed-off-by: Josh Cartwright <joshc@codeaurora.org>
21 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
22 ---
23 drivers/spmi/Kconfig | 17 ++
24 drivers/spmi/Makefile | 2 +
25 drivers/spmi/spmi-pmic-arb.c | 405 ++++++++++++++++++++++++++++++++++++++++++
26 3 files changed, 424 insertions(+)
27 create mode 100644 drivers/spmi/spmi-pmic-arb.c
28
29 --- a/drivers/spmi/Kconfig
30 +++ b/drivers/spmi/Kconfig
31 @@ -7,3 +7,20 @@ menuconfig SPMI
32 SPMI (System Power Management Interface) is a two-wire
33 serial interface between baseband and application processors
34 and Power Management Integrated Circuits (PMIC).
35 +
36 +if SPMI
37 +
38 +config SPMI_MSM_PMIC_ARB
39 + tristate "Qualcomm MSM SPMI Controller (PMIC Arbiter)"
40 + depends on ARM
41 + depends on ARCH_MSM || COMPILE_TEST
42 + default ARCH_MSM
43 + help
44 + If you say yes to this option, support will be included for the
45 + built-in SPMI PMIC Arbiter interface on Qualcomm MSM family
46 + processors.
47 +
48 + This is required for communicating with Qualcomm PMICs and
49 + other devices that have the SPMI interface.
50 +
51 +endif
52 --- a/drivers/spmi/Makefile
53 +++ b/drivers/spmi/Makefile
54 @@ -2,3 +2,5 @@
55 # Makefile for kernel SPMI framework.
56 #
57 obj-$(CONFIG_SPMI) += spmi.o
58 +
59 +obj-$(CONFIG_SPMI_MSM_PMIC_ARB) += spmi-pmic-arb.o
60 --- /dev/null
61 +++ b/drivers/spmi/spmi-pmic-arb.c
62 @@ -0,0 +1,405 @@
63 +/* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
64 + *
65 + * This program is free software; you can redistribute it and/or modify
66 + * it under the terms of the GNU General Public License version 2 and
67 + * only version 2 as published by the Free Software Foundation.
68 + *
69 + * This program is distributed in the hope that it will be useful,
70 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
71 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
72 + * GNU General Public License for more details.
73 + */
74 +#include <linux/delay.h>
75 +#include <linux/err.h>
76 +#include <linux/interrupt.h>
77 +#include <linux/io.h>
78 +#include <linux/kernel.h>
79 +#include <linux/module.h>
80 +#include <linux/of.h>
81 +#include <linux/platform_device.h>
82 +#include <linux/slab.h>
83 +#include <linux/spmi.h>
84 +
85 +/* PMIC Arbiter configuration registers */
86 +#define PMIC_ARB_VERSION 0x0000
87 +#define PMIC_ARB_INT_EN 0x0004
88 +
89 +/* PMIC Arbiter channel registers */
90 +#define PMIC_ARB_CMD(N) (0x0800 + (0x80 * (N)))
91 +#define PMIC_ARB_CONFIG(N) (0x0804 + (0x80 * (N)))
92 +#define PMIC_ARB_STATUS(N) (0x0808 + (0x80 * (N)))
93 +#define PMIC_ARB_WDATA0(N) (0x0810 + (0x80 * (N)))
94 +#define PMIC_ARB_WDATA1(N) (0x0814 + (0x80 * (N)))
95 +#define PMIC_ARB_RDATA0(N) (0x0818 + (0x80 * (N)))
96 +#define PMIC_ARB_RDATA1(N) (0x081C + (0x80 * (N)))
97 +
98 +/* Interrupt Controller */
99 +#define SPMI_PIC_OWNER_ACC_STATUS(M, N) (0x0000 + ((32 * (M)) + (4 * (N))))
100 +#define SPMI_PIC_ACC_ENABLE(N) (0x0200 + (4 * (N)))
101 +#define SPMI_PIC_IRQ_STATUS(N) (0x0600 + (4 * (N)))
102 +#define SPMI_PIC_IRQ_CLEAR(N) (0x0A00 + (4 * (N)))
103 +
104 +/* Mapping Table */
105 +#define SPMI_MAPPING_TABLE_REG(N) (0x0B00 + (4 * (N)))
106 +#define SPMI_MAPPING_BIT_INDEX(X) (((X) >> 18) & 0xF)
107 +#define SPMI_MAPPING_BIT_IS_0_FLAG(X) (((X) >> 17) & 0x1)
108 +#define SPMI_MAPPING_BIT_IS_0_RESULT(X) (((X) >> 9) & 0xFF)
109 +#define SPMI_MAPPING_BIT_IS_1_FLAG(X) (((X) >> 8) & 0x1)
110 +#define SPMI_MAPPING_BIT_IS_1_RESULT(X) (((X) >> 0) & 0xFF)
111 +
112 +#define SPMI_MAPPING_TABLE_LEN 255
113 +#define SPMI_MAPPING_TABLE_TREE_DEPTH 16 /* Maximum of 16-bits */
114 +
115 +/* Ownership Table */
116 +#define SPMI_OWNERSHIP_TABLE_REG(N) (0x0700 + (4 * (N)))
117 +#define SPMI_OWNERSHIP_PERIPH2OWNER(X) ((X) & 0x7)
118 +
119 +/* Channel Status fields */
120 +enum pmic_arb_chnl_status {
121 + PMIC_ARB_STATUS_DONE = (1 << 0),
122 + PMIC_ARB_STATUS_FAILURE = (1 << 1),
123 + PMIC_ARB_STATUS_DENIED = (1 << 2),
124 + PMIC_ARB_STATUS_DROPPED = (1 << 3),
125 +};
126 +
127 +/* Command register fields */
128 +#define PMIC_ARB_CMD_MAX_BYTE_COUNT 8
129 +
130 +/* Command Opcodes */
131 +enum pmic_arb_cmd_op_code {
132 + PMIC_ARB_OP_EXT_WRITEL = 0,
133 + PMIC_ARB_OP_EXT_READL = 1,
134 + PMIC_ARB_OP_EXT_WRITE = 2,
135 + PMIC_ARB_OP_RESET = 3,
136 + PMIC_ARB_OP_SLEEP = 4,
137 + PMIC_ARB_OP_SHUTDOWN = 5,
138 + PMIC_ARB_OP_WAKEUP = 6,
139 + PMIC_ARB_OP_AUTHENTICATE = 7,
140 + PMIC_ARB_OP_MSTR_READ = 8,
141 + PMIC_ARB_OP_MSTR_WRITE = 9,
142 + PMIC_ARB_OP_EXT_READ = 13,
143 + PMIC_ARB_OP_WRITE = 14,
144 + PMIC_ARB_OP_READ = 15,
145 + PMIC_ARB_OP_ZERO_WRITE = 16,
146 +};
147 +
148 +/* Maximum number of support PMIC peripherals */
149 +#define PMIC_ARB_MAX_PERIPHS 256
150 +#define PMIC_ARB_PERIPH_ID_VALID (1 << 15)
151 +#define PMIC_ARB_TIMEOUT_US 100
152 +#define PMIC_ARB_MAX_TRANS_BYTES (8)
153 +
154 +#define PMIC_ARB_APID_MASK 0xFF
155 +#define PMIC_ARB_PPID_MASK 0xFFF
156 +
157 +/* interrupt enable bit */
158 +#define SPMI_PIC_ACC_ENABLE_BIT BIT(0)
159 +
160 +/**
161 + * spmi_pmic_arb_dev - SPMI PMIC Arbiter object
162 + *
163 + * @base: address of the PMIC Arbiter core registers.
164 + * @intr: address of the SPMI interrupt control registers.
165 + * @cnfg: address of the PMIC Arbiter configuration registers.
166 + * @lock: lock to synchronize accesses.
167 + * @channel: which channel to use for accesses.
168 + */
169 +struct spmi_pmic_arb_dev {
170 + void __iomem *base;
171 + void __iomem *intr;
172 + void __iomem *cnfg;
173 + raw_spinlock_t lock;
174 + u8 channel;
175 +};
176 +
177 +static inline u32 pmic_arb_base_read(struct spmi_pmic_arb_dev *dev, u32 offset)
178 +{
179 + return readl_relaxed(dev->base + offset);
180 +}
181 +
182 +static inline void pmic_arb_base_write(struct spmi_pmic_arb_dev *dev,
183 + u32 offset, u32 val)
184 +{
185 + writel_relaxed(val, dev->base + offset);
186 +}
187 +
188 +/**
189 + * pa_read_data: reads pmic-arb's register and copy 1..4 bytes to buf
190 + * @bc: byte count -1. range: 0..3
191 + * @reg: register's address
192 + * @buf: output parameter, length must be bc + 1
193 + */
194 +static void pa_read_data(struct spmi_pmic_arb_dev *dev, u8 *buf, u32 reg, u8 bc)
195 +{
196 + u32 data = pmic_arb_base_read(dev, reg);
197 + memcpy(buf, &data, (bc & 3) + 1);
198 +}
199 +
200 +/**
201 + * pa_write_data: write 1..4 bytes from buf to pmic-arb's register
202 + * @bc: byte-count -1. range: 0..3.
203 + * @reg: register's address.
204 + * @buf: buffer to write. length must be bc + 1.
205 + */
206 +static void
207 +pa_write_data(struct spmi_pmic_arb_dev *dev, const u8 *buf, u32 reg, u8 bc)
208 +{
209 + u32 data = 0;
210 + memcpy(&data, buf, (bc & 3) + 1);
211 + pmic_arb_base_write(dev, reg, data);
212 +}
213 +
214 +static int pmic_arb_wait_for_done(struct spmi_controller *ctrl)
215 +{
216 + struct spmi_pmic_arb_dev *dev = spmi_controller_get_drvdata(ctrl);
217 + u32 status = 0;
218 + u32 timeout = PMIC_ARB_TIMEOUT_US;
219 + u32 offset = PMIC_ARB_STATUS(dev->channel);
220 +
221 + while (timeout--) {
222 + status = pmic_arb_base_read(dev, offset);
223 +
224 + if (status & PMIC_ARB_STATUS_DONE) {
225 + if (status & PMIC_ARB_STATUS_DENIED) {
226 + dev_err(&ctrl->dev,
227 + "%s: transaction denied (0x%x)\n",
228 + __func__, status);
229 + return -EPERM;
230 + }
231 +
232 + if (status & PMIC_ARB_STATUS_FAILURE) {
233 + dev_err(&ctrl->dev,
234 + "%s: transaction failed (0x%x)\n",
235 + __func__, status);
236 + return -EIO;
237 + }
238 +
239 + if (status & PMIC_ARB_STATUS_DROPPED) {
240 + dev_err(&ctrl->dev,
241 + "%s: transaction dropped (0x%x)\n",
242 + __func__, status);
243 + return -EIO;
244 + }
245 +
246 + return 0;
247 + }
248 + udelay(1);
249 + }
250 +
251 + dev_err(&ctrl->dev,
252 + "%s: timeout, status 0x%x\n",
253 + __func__, status);
254 + return -ETIMEDOUT;
255 +}
256 +
257 +/* Non-data command */
258 +static int pmic_arb_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid)
259 +{
260 + struct spmi_pmic_arb_dev *pmic_arb = spmi_controller_get_drvdata(ctrl);
261 + unsigned long flags;
262 + u32 cmd;
263 + int rc;
264 +
265 + /* Check for valid non-data command */
266 + if (opc < SPMI_CMD_RESET || opc > SPMI_CMD_WAKEUP)
267 + return -EINVAL;
268 +
269 + cmd = ((opc | 0x40) << 27) | ((sid & 0xf) << 20);
270 +
271 + raw_spin_lock_irqsave(&pmic_arb->lock, flags);
272 + pmic_arb_base_write(pmic_arb, PMIC_ARB_CMD(pmic_arb->channel), cmd);
273 + rc = pmic_arb_wait_for_done(ctrl);
274 + raw_spin_unlock_irqrestore(&pmic_arb->lock, flags);
275 +
276 + return rc;
277 +}
278 +
279 +static int pmic_arb_read_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
280 + u16 addr, u8 *buf, size_t len)
281 +{
282 + struct spmi_pmic_arb_dev *pmic_arb = spmi_controller_get_drvdata(ctrl);
283 + unsigned long flags;
284 + u8 bc = len - 1;
285 + u32 cmd;
286 + int rc;
287 +
288 + if (bc >= PMIC_ARB_MAX_TRANS_BYTES) {
289 + dev_err(&ctrl->dev,
290 + "pmic-arb supports 1..%d bytes per trans, but %d requested",
291 + PMIC_ARB_MAX_TRANS_BYTES, len);
292 + return -EINVAL;
293 + }
294 +
295 + /* Check the opcode */
296 + if (opc >= 0x60 && opc <= 0x7F)
297 + opc = PMIC_ARB_OP_READ;
298 + else if (opc >= 0x20 && opc <= 0x2F)
299 + opc = PMIC_ARB_OP_EXT_READ;
300 + else if (opc >= 0x38 && opc <= 0x3F)
301 + opc = PMIC_ARB_OP_EXT_READL;
302 + else
303 + return -EINVAL;
304 +
305 + cmd = (opc << 27) | ((sid & 0xf) << 20) | (addr << 4) | (bc & 0x7);
306 +
307 + raw_spin_lock_irqsave(&pmic_arb->lock, flags);
308 + pmic_arb_base_write(pmic_arb, PMIC_ARB_CMD(pmic_arb->channel), cmd);
309 + rc = pmic_arb_wait_for_done(ctrl);
310 + if (rc)
311 + goto done;
312 +
313 + pa_read_data(pmic_arb, buf, PMIC_ARB_RDATA0(pmic_arb->channel),
314 + min_t(u8, bc, 3));
315 +
316 + if (bc > 3)
317 + pa_read_data(pmic_arb, buf + 4,
318 + PMIC_ARB_RDATA1(pmic_arb->channel), bc - 4);
319 +
320 +done:
321 + raw_spin_unlock_irqrestore(&pmic_arb->lock, flags);
322 + return rc;
323 +}
324 +
325 +static int pmic_arb_write_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
326 + u16 addr, const u8 *buf, size_t len)
327 +{
328 + struct spmi_pmic_arb_dev *pmic_arb = spmi_controller_get_drvdata(ctrl);
329 + unsigned long flags;
330 + u8 bc = len - 1;
331 + u32 cmd;
332 + int rc;
333 +
334 + if (bc >= PMIC_ARB_MAX_TRANS_BYTES) {
335 + dev_err(&ctrl->dev,
336 + "pmic-arb supports 1..%d bytes per trans, but:%d requested",
337 + PMIC_ARB_MAX_TRANS_BYTES, len);
338 + return -EINVAL;
339 + }
340 +
341 + /* Check the opcode */
342 + if (opc >= 0x40 && opc <= 0x5F)
343 + opc = PMIC_ARB_OP_WRITE;
344 + else if (opc >= 0x00 && opc <= 0x0F)
345 + opc = PMIC_ARB_OP_EXT_WRITE;
346 + else if (opc >= 0x30 && opc <= 0x37)
347 + opc = PMIC_ARB_OP_EXT_WRITEL;
348 + else if (opc >= 0x80 && opc <= 0xFF)
349 + opc = PMIC_ARB_OP_ZERO_WRITE;
350 + else
351 + return -EINVAL;
352 +
353 + cmd = (opc << 27) | ((sid & 0xf) << 20) | (addr << 4) | (bc & 0x7);
354 +
355 + /* Write data to FIFOs */
356 + raw_spin_lock_irqsave(&pmic_arb->lock, flags);
357 + pa_write_data(pmic_arb, buf, PMIC_ARB_WDATA0(pmic_arb->channel)
358 + , min_t(u8, bc, 3));
359 + if (bc > 3)
360 + pa_write_data(pmic_arb, buf + 4,
361 + PMIC_ARB_WDATA1(pmic_arb->channel), bc - 4);
362 +
363 + /* Start the transaction */
364 + pmic_arb_base_write(pmic_arb, PMIC_ARB_CMD(pmic_arb->channel), cmd);
365 + rc = pmic_arb_wait_for_done(ctrl);
366 + raw_spin_unlock_irqrestore(&pmic_arb->lock, flags);
367 +
368 + return rc;
369 +}
370 +
371 +static int spmi_pmic_arb_probe(struct platform_device *pdev)
372 +{
373 + struct spmi_pmic_arb_dev *pa;
374 + struct spmi_controller *ctrl;
375 + struct resource *res;
376 + u32 channel;
377 + int err, i;
378 +
379 + ctrl = spmi_controller_alloc(&pdev->dev, sizeof(*pa));
380 + if (!ctrl)
381 + return -ENOMEM;
382 +
383 + pa = spmi_controller_get_drvdata(ctrl);
384 +
385 + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "core");
386 + pa->base = devm_ioremap_resource(&ctrl->dev, res);
387 + if (IS_ERR(pa->base)) {
388 + err = PTR_ERR(pa->base);
389 + goto err_put_ctrl;
390 + }
391 +
392 + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "intr");
393 + pa->intr = devm_ioremap_resource(&ctrl->dev, res);
394 + if (IS_ERR(pa->intr)) {
395 + err = PTR_ERR(pa->intr);
396 + goto err_put_ctrl;
397 + }
398 +
399 + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cnfg");
400 + pa->cnfg = devm_ioremap_resource(&ctrl->dev, res);
401 + if (IS_ERR(pa->cnfg)) {
402 + err = PTR_ERR(pa->cnfg);
403 + goto err_put_ctrl;
404 + }
405 +
406 + err = of_property_read_u32(pdev->dev.of_node, "qcom,channel", &channel);
407 + if (err) {
408 + dev_err(&pdev->dev, "channel unspecified.\n");
409 + goto err_put_ctrl;
410 + }
411 +
412 + if (channel > 5) {
413 + dev_err(&pdev->dev, "invalid channel (%u) specified.\n",
414 + channel);
415 + goto err_put_ctrl;
416 + }
417 +
418 + pa->channel = channel;
419 +
420 + platform_set_drvdata(pdev, ctrl);
421 + raw_spin_lock_init(&pa->lock);
422 +
423 + ctrl->cmd = pmic_arb_cmd;
424 + ctrl->read_cmd = pmic_arb_read_cmd;
425 + ctrl->write_cmd = pmic_arb_write_cmd;
426 +
427 + err = spmi_controller_add(ctrl);
428 + if (err)
429 + goto err_put_ctrl;
430 +
431 + dev_dbg(&ctrl->dev, "PMIC Arb Version 0x%x\n",
432 + pmic_arb_base_read(pa, PMIC_ARB_VERSION));
433 +
434 + return 0;
435 +
436 +err_put_ctrl:
437 + spmi_controller_put(ctrl);
438 + return err;
439 +}
440 +
441 +static int spmi_pmic_arb_remove(struct platform_device *pdev)
442 +{
443 + struct spmi_controller *ctrl = platform_get_drvdata(pdev);
444 + spmi_controller_remove(ctrl);
445 + spmi_controller_put(ctrl);
446 + return 0;
447 +}
448 +
449 +static const struct of_device_id spmi_pmic_arb_match_table[] = {
450 + { .compatible = "qcom,spmi-pmic-arb", },
451 + {},
452 +};
453 +MODULE_DEVICE_TABLE(of, spmi_pmic_arb_match_table);
454 +
455 +static struct platform_driver spmi_pmic_arb_driver = {
456 + .probe = spmi_pmic_arb_probe,
457 + .remove = spmi_pmic_arb_remove,
458 + .driver = {
459 + .name = "spmi_pmic_arb",
460 + .owner = THIS_MODULE,
461 + .of_match_table = spmi_pmic_arb_match_table,
462 + },
463 +};
464 +module_platform_driver(spmi_pmic_arb_driver);
465 +
466 +MODULE_LICENSE("GPL v2");
467 +MODULE_ALIAS("platform:spmi_pmic_arb");