kernel: backport bcma patches queued for 3.20
[openwrt/svn-archive/archive.git] / target / linux / ipq806x / patches / 0055-spmi-Linux-driver-framework-for-SPMI.patch
1 From 1b0018dfd6295cbcc87738601b84bf49f3004419 Mon Sep 17 00:00:00 2001
2 From: Kenneth Heitke <kheitke@codeaurora.org>
3 Date: Wed, 12 Feb 2014 13:44:22 -0600
4 Subject: [PATCH 055/182] spmi: Linux driver framework for SPMI
5
6 System Power Management Interface (SPMI) is a specification
7 developed by the MIPI (Mobile Industry Process Interface) Alliance
8 optimized for the real time control of Power Management ICs (PMIC).
9
10 SPMI is a two-wire serial interface that supports up to 4 master
11 devices and up to 16 logical slaves.
12
13 The framework supports message APIs, multiple busses (1 controller
14 per bus) and multiple clients/slave devices per controller.
15
16 Signed-off-by: Kenneth Heitke <kheitke@codeaurora.org>
17 Signed-off-by: Michael Bohan <mbohan@codeaurora.org>
18 Signed-off-by: Josh Cartwright <joshc@codeaurora.org>
19 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
20 ---
21 drivers/Kconfig | 2 +
22 drivers/Makefile | 1 +
23 drivers/spmi/Kconfig | 9 +
24 drivers/spmi/Makefile | 4 +
25 drivers/spmi/spmi.c | 609 +++++++++++++++++++++++++++++++++++++++
26 include/dt-bindings/spmi/spmi.h | 18 ++
27 include/linux/mod_devicetable.h | 8 +
28 include/linux/spmi.h | 191 ++++++++++++
29 8 files changed, 842 insertions(+)
30 create mode 100644 drivers/spmi/Kconfig
31 create mode 100644 drivers/spmi/Makefile
32 create mode 100644 drivers/spmi/spmi.c
33 create mode 100644 include/dt-bindings/spmi/spmi.h
34 create mode 100644 include/linux/spmi.h
35
36 --- a/drivers/Kconfig
37 +++ b/drivers/Kconfig
38 @@ -52,6 +52,8 @@ source "drivers/i2c/Kconfig"
39
40 source "drivers/spi/Kconfig"
41
42 +source "drivers/spmi/Kconfig"
43 +
44 source "drivers/hsi/Kconfig"
45
46 source "drivers/pps/Kconfig"
47 --- a/drivers/Makefile
48 +++ b/drivers/Makefile
49 @@ -66,6 +66,7 @@ obj-$(CONFIG_ATA) += ata/
50 obj-$(CONFIG_TARGET_CORE) += target/
51 obj-$(CONFIG_MTD) += mtd/
52 obj-$(CONFIG_SPI) += spi/
53 +obj-$(CONFIG_SPMI) += spmi/
54 obj-y += hsi/
55 obj-y += net/
56 obj-$(CONFIG_ATM) += atm/
57 --- /dev/null
58 +++ b/drivers/spmi/Kconfig
59 @@ -0,0 +1,9 @@
60 +#
61 +# SPMI driver configuration
62 +#
63 +menuconfig SPMI
64 + tristate "SPMI support"
65 + help
66 + SPMI (System Power Management Interface) is a two-wire
67 + serial interface between baseband and application processors
68 + and Power Management Integrated Circuits (PMIC).
69 --- /dev/null
70 +++ b/drivers/spmi/Makefile
71 @@ -0,0 +1,4 @@
72 +#
73 +# Makefile for kernel SPMI framework.
74 +#
75 +obj-$(CONFIG_SPMI) += spmi.o
76 --- /dev/null
77 +++ b/drivers/spmi/spmi.c
78 @@ -0,0 +1,609 @@
79 +/* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
80 + *
81 + * This program is free software; you can redistribute it and/or modify
82 + * it under the terms of the GNU General Public License version 2 and
83 + * only version 2 as published by the Free Software Foundation.
84 + *
85 + * This program is distributed in the hope that it will be useful,
86 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
87 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
88 + * GNU General Public License for more details.
89 + */
90 +#include <linux/kernel.h>
91 +#include <linux/errno.h>
92 +#include <linux/idr.h>
93 +#include <linux/slab.h>
94 +#include <linux/module.h>
95 +#include <linux/of.h>
96 +#include <linux/of_device.h>
97 +#include <linux/platform_device.h>
98 +#include <linux/spmi.h>
99 +#include <linux/module.h>
100 +#include <linux/pm_runtime.h>
101 +
102 +#include <dt-bindings/spmi/spmi.h>
103 +
104 +static DEFINE_IDA(ctrl_ida);
105 +
106 +static void spmi_dev_release(struct device *dev)
107 +{
108 + struct spmi_device *sdev = to_spmi_device(dev);
109 + kfree(sdev);
110 +}
111 +
112 +static const struct device_type spmi_dev_type = {
113 + .release = spmi_dev_release,
114 +};
115 +
116 +static void spmi_ctrl_release(struct device *dev)
117 +{
118 + struct spmi_controller *ctrl = to_spmi_controller(dev);
119 + ida_simple_remove(&ctrl_ida, ctrl->nr);
120 + kfree(ctrl);
121 +}
122 +
123 +static const struct device_type spmi_ctrl_type = {
124 + .release = spmi_ctrl_release,
125 +};
126 +
127 +#ifdef CONFIG_PM_RUNTIME
128 +static int spmi_runtime_suspend(struct device *dev)
129 +{
130 + struct spmi_device *sdev = to_spmi_device(dev);
131 + int err;
132 +
133 + err = pm_generic_runtime_suspend(dev);
134 + if (err)
135 + return err;
136 +
137 + return spmi_command_sleep(sdev);
138 +}
139 +
140 +static int spmi_runtime_resume(struct device *dev)
141 +{
142 + struct spmi_device *sdev = to_spmi_device(dev);
143 + int err;
144 +
145 + err = spmi_command_wakeup(sdev);
146 + if (err)
147 + return err;
148 +
149 + return pm_generic_runtime_resume(dev);
150 +}
151 +#endif
152 +
153 +static const struct dev_pm_ops spmi_pm_ops = {
154 + SET_RUNTIME_PM_OPS(
155 + spmi_runtime_suspend,
156 + spmi_runtime_resume,
157 + NULL
158 + )
159 +};
160 +
161 +static int spmi_device_match(struct device *dev, struct device_driver *drv)
162 +{
163 + if (of_driver_match_device(dev, drv))
164 + return 1;
165 +
166 + if (drv->name)
167 + return strncmp(dev_name(dev), drv->name,
168 + SPMI_NAME_SIZE) == 0;
169 +
170 + return 0;
171 +}
172 +
173 +/**
174 + * spmi_device_add() - add a device previously constructed via spmi_device_alloc()
175 + * @sdev: spmi_device to be added
176 + */
177 +int spmi_device_add(struct spmi_device *sdev)
178 +{
179 + struct spmi_controller *ctrl = sdev->ctrl;
180 + int err;
181 +
182 + dev_set_name(&sdev->dev, "%d-%02x", ctrl->nr, sdev->usid);
183 +
184 + err = device_add(&sdev->dev);
185 + if (err < 0) {
186 + dev_err(&sdev->dev, "Can't add %s, status %d\n",
187 + dev_name(&sdev->dev), err);
188 + goto err_device_add;
189 + }
190 +
191 + dev_dbg(&sdev->dev, "device %s registered\n", dev_name(&sdev->dev));
192 +
193 +err_device_add:
194 + return err;
195 +}
196 +EXPORT_SYMBOL_GPL(spmi_device_add);
197 +
198 +/**
199 + * spmi_device_remove(): remove an SPMI device
200 + * @sdev: spmi_device to be removed
201 + */
202 +void spmi_device_remove(struct spmi_device *sdev)
203 +{
204 + device_unregister(&sdev->dev);
205 +}
206 +EXPORT_SYMBOL_GPL(spmi_device_remove);
207 +
208 +static inline int
209 +spmi_cmd(struct spmi_controller *ctrl, u8 opcode, u8 sid)
210 +{
211 + if (!ctrl || !ctrl->cmd || ctrl->dev.type != &spmi_ctrl_type)
212 + return -EINVAL;
213 +
214 + return ctrl->cmd(ctrl, opcode, sid);
215 +}
216 +
217 +static inline int spmi_read_cmd(struct spmi_controller *ctrl, u8 opcode,
218 + u8 sid, u16 addr, u8 *buf, size_t len)
219 +{
220 + if (!ctrl || !ctrl->read_cmd || ctrl->dev.type != &spmi_ctrl_type)
221 + return -EINVAL;
222 +
223 + return ctrl->read_cmd(ctrl, opcode, sid, addr, buf, len);
224 +}
225 +
226 +static inline int spmi_write_cmd(struct spmi_controller *ctrl, u8 opcode,
227 + u8 sid, u16 addr, const u8 *buf, size_t len)
228 +{
229 + if (!ctrl || !ctrl->write_cmd || ctrl->dev.type != &spmi_ctrl_type)
230 + return -EINVAL;
231 +
232 + return ctrl->write_cmd(ctrl, opcode, sid, addr, buf, len);
233 +}
234 +
235 +/**
236 + * spmi_register_read() - register read
237 + * @sdev: SPMI device.
238 + * @addr: slave register address (5-bit address).
239 + * @buf: buffer to be populated with data from the Slave.
240 + *
241 + * Reads 1 byte of data from a Slave device register.
242 + */
243 +int spmi_register_read(struct spmi_device *sdev, u8 addr, u8 *buf)
244 +{
245 + /* 5-bit register address */
246 + if (addr > 0x1F)
247 + return -EINVAL;
248 +
249 + return spmi_read_cmd(sdev->ctrl, SPMI_CMD_READ, sdev->usid, addr,
250 + buf, 1);
251 +}
252 +EXPORT_SYMBOL_GPL(spmi_register_read);
253 +
254 +/**
255 + * spmi_ext_register_read() - extended register read
256 + * @sdev: SPMI device.
257 + * @addr: slave register address (8-bit address).
258 + * @buf: buffer to be populated with data from the Slave.
259 + * @len: the request number of bytes to read (up to 16 bytes).
260 + *
261 + * Reads up to 16 bytes of data from the extended register space on a
262 + * Slave device.
263 + */
264 +int spmi_ext_register_read(struct spmi_device *sdev, u8 addr, u8 *buf,
265 + size_t len)
266 +{
267 + /* 8-bit register address, up to 16 bytes */
268 + if (len == 0 || len > 16)
269 + return -EINVAL;
270 +
271 + return spmi_read_cmd(sdev->ctrl, SPMI_CMD_EXT_READ, sdev->usid, addr,
272 + buf, len);
273 +}
274 +EXPORT_SYMBOL_GPL(spmi_ext_register_read);
275 +
276 +/**
277 + * spmi_ext_register_readl() - extended register read long
278 + * @sdev: SPMI device.
279 + * @addr: slave register address (16-bit address).
280 + * @buf: buffer to be populated with data from the Slave.
281 + * @len: the request number of bytes to read (up to 8 bytes).
282 + *
283 + * Reads up to 8 bytes of data from the extended register space on a
284 + * Slave device using 16-bit address.
285 + */
286 +int spmi_ext_register_readl(struct spmi_device *sdev, u16 addr, u8 *buf,
287 + size_t len)
288 +{
289 + /* 16-bit register address, up to 8 bytes */
290 + if (len == 0 || len > 8)
291 + return -EINVAL;
292 +
293 + return spmi_read_cmd(sdev->ctrl, SPMI_CMD_EXT_READL, sdev->usid, addr,
294 + buf, len);
295 +}
296 +EXPORT_SYMBOL_GPL(spmi_ext_register_readl);
297 +
298 +/**
299 + * spmi_register_write() - register write
300 + * @sdev: SPMI device
301 + * @addr: slave register address (5-bit address).
302 + * @data: buffer containing the data to be transferred to the Slave.
303 + *
304 + * Writes 1 byte of data to a Slave device register.
305 + */
306 +int spmi_register_write(struct spmi_device *sdev, u8 addr, u8 data)
307 +{
308 + /* 5-bit register address */
309 + if (addr > 0x1F)
310 + return -EINVAL;
311 +
312 + return spmi_write_cmd(sdev->ctrl, SPMI_CMD_WRITE, sdev->usid, addr,
313 + &data, 1);
314 +}
315 +EXPORT_SYMBOL_GPL(spmi_register_write);
316 +
317 +/**
318 + * spmi_register_zero_write() - register zero write
319 + * @sdev: SPMI device.
320 + * @data: the data to be written to register 0 (7-bits).
321 + *
322 + * Writes data to register 0 of the Slave device.
323 + */
324 +int spmi_register_zero_write(struct spmi_device *sdev, u8 data)
325 +{
326 + return spmi_write_cmd(sdev->ctrl, SPMI_CMD_ZERO_WRITE, sdev->usid, 0,
327 + &data, 1);
328 +}
329 +EXPORT_SYMBOL_GPL(spmi_register_zero_write);
330 +
331 +/**
332 + * spmi_ext_register_write() - extended register write
333 + * @sdev: SPMI device.
334 + * @addr: slave register address (8-bit address).
335 + * @buf: buffer containing the data to be transferred to the Slave.
336 + * @len: the request number of bytes to read (up to 16 bytes).
337 + *
338 + * Writes up to 16 bytes of data to the extended register space of a
339 + * Slave device.
340 + */
341 +int spmi_ext_register_write(struct spmi_device *sdev, u8 addr, const u8 *buf,
342 + size_t len)
343 +{
344 + /* 8-bit register address, up to 16 bytes */
345 + if (len == 0 || len > 16)
346 + return -EINVAL;
347 +
348 + return spmi_write_cmd(sdev->ctrl, SPMI_CMD_EXT_WRITE, sdev->usid, addr,
349 + buf, len);
350 +}
351 +EXPORT_SYMBOL_GPL(spmi_ext_register_write);
352 +
353 +/**
354 + * spmi_ext_register_writel() - extended register write long
355 + * @sdev: SPMI device.
356 + * @addr: slave register address (16-bit address).
357 + * @buf: buffer containing the data to be transferred to the Slave.
358 + * @len: the request number of bytes to read (up to 8 bytes).
359 + *
360 + * Writes up to 8 bytes of data to the extended register space of a
361 + * Slave device using 16-bit address.
362 + */
363 +int spmi_ext_register_writel(struct spmi_device *sdev, u16 addr, const u8 *buf,
364 + size_t len)
365 +{
366 + /* 4-bit Slave Identifier, 16-bit register address, up to 8 bytes */
367 + if (len == 0 || len > 8)
368 + return -EINVAL;
369 +
370 + return spmi_write_cmd(sdev->ctrl, SPMI_CMD_EXT_WRITEL, sdev->usid,
371 + addr, buf, len);
372 +}
373 +EXPORT_SYMBOL_GPL(spmi_ext_register_writel);
374 +
375 +/**
376 + * spmi_command_reset() - sends RESET command to the specified slave
377 + * @sdev: SPMI device.
378 + *
379 + * The Reset command initializes the Slave and forces all registers to
380 + * their reset values. The Slave shall enter the STARTUP state after
381 + * receiving a Reset command.
382 + */
383 +int spmi_command_reset(struct spmi_device *sdev)
384 +{
385 + return spmi_cmd(sdev->ctrl, SPMI_CMD_RESET, sdev->usid);
386 +}
387 +EXPORT_SYMBOL_GPL(spmi_command_reset);
388 +
389 +/**
390 + * spmi_command_sleep() - sends SLEEP command to the specified SPMI device
391 + * @sdev: SPMI device.
392 + *
393 + * The Sleep command causes the Slave to enter the user defined SLEEP state.
394 + */
395 +int spmi_command_sleep(struct spmi_device *sdev)
396 +{
397 + return spmi_cmd(sdev->ctrl, SPMI_CMD_SLEEP, sdev->usid);
398 +}
399 +EXPORT_SYMBOL_GPL(spmi_command_sleep);
400 +
401 +/**
402 + * spmi_command_wakeup() - sends WAKEUP command to the specified SPMI device
403 + * @sdev: SPMI device.
404 + *
405 + * The Wakeup command causes the Slave to move from the SLEEP state to
406 + * the ACTIVE state.
407 + */
408 +int spmi_command_wakeup(struct spmi_device *sdev)
409 +{
410 + return spmi_cmd(sdev->ctrl, SPMI_CMD_WAKEUP, sdev->usid);
411 +}
412 +EXPORT_SYMBOL_GPL(spmi_command_wakeup);
413 +
414 +/**
415 + * spmi_command_shutdown() - sends SHUTDOWN command to the specified SPMI device
416 + * @sdev: SPMI device.
417 + *
418 + * The Shutdown command causes the Slave to enter the SHUTDOWN state.
419 + */
420 +int spmi_command_shutdown(struct spmi_device *sdev)
421 +{
422 + return spmi_cmd(sdev->ctrl, SPMI_CMD_SHUTDOWN, sdev->usid);
423 +}
424 +EXPORT_SYMBOL_GPL(spmi_command_shutdown);
425 +
426 +static int spmi_drv_probe(struct device *dev)
427 +{
428 + const struct spmi_driver *sdrv = to_spmi_driver(dev->driver);
429 + struct spmi_device *sdev = to_spmi_device(dev);
430 + int err;
431 +
432 + /* Ensure the slave is in ACTIVE state */
433 + err = spmi_command_wakeup(sdev);
434 + if (err)
435 + goto fail_wakeup;
436 +
437 + pm_runtime_get_noresume(dev);
438 + pm_runtime_set_active(dev);
439 + pm_runtime_enable(dev);
440 +
441 + err = sdrv->probe(sdev);
442 + if (err)
443 + goto fail_probe;
444 +
445 + return 0;
446 +
447 +fail_probe:
448 + pm_runtime_disable(dev);
449 + pm_runtime_set_suspended(dev);
450 + pm_runtime_put_noidle(dev);
451 +fail_wakeup:
452 + return err;
453 +}
454 +
455 +static int spmi_drv_remove(struct device *dev)
456 +{
457 + const struct spmi_driver *sdrv = to_spmi_driver(dev->driver);
458 +
459 + pm_runtime_get_sync(dev);
460 + sdrv->remove(to_spmi_device(dev));
461 + pm_runtime_put_noidle(dev);
462 +
463 + pm_runtime_disable(dev);
464 + pm_runtime_set_suspended(dev);
465 + pm_runtime_put_noidle(dev);
466 + return 0;
467 +}
468 +
469 +static struct bus_type spmi_bus_type = {
470 + .name = "spmi",
471 + .match = spmi_device_match,
472 + .pm = &spmi_pm_ops,
473 + .probe = spmi_drv_probe,
474 + .remove = spmi_drv_remove,
475 +};
476 +
477 +/**
478 + * spmi_controller_alloc() - Allocate a new SPMI device
479 + * @ctrl: associated controller
480 + *
481 + * Caller is responsible for either calling spmi_device_add() to add the
482 + * newly allocated controller, or calling spmi_device_put() to discard it.
483 + */
484 +struct spmi_device *spmi_device_alloc(struct spmi_controller *ctrl)
485 +{
486 + struct spmi_device *sdev;
487 +
488 + sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
489 + if (!sdev)
490 + return NULL;
491 +
492 + sdev->ctrl = ctrl;
493 + device_initialize(&sdev->dev);
494 + sdev->dev.parent = &ctrl->dev;
495 + sdev->dev.bus = &spmi_bus_type;
496 + sdev->dev.type = &spmi_dev_type;
497 + return sdev;
498 +}
499 +EXPORT_SYMBOL_GPL(spmi_device_alloc);
500 +
501 +/**
502 + * spmi_controller_alloc() - Allocate a new SPMI controller
503 + * @parent: parent device
504 + * @size: size of private data
505 + *
506 + * Caller is responsible for either calling spmi_controller_add() to add the
507 + * newly allocated controller, or calling spmi_controller_put() to discard it.
508 + * The allocated private data region may be accessed via
509 + * spmi_controller_get_drvdata()
510 + */
511 +struct spmi_controller *spmi_controller_alloc(struct device *parent,
512 + size_t size)
513 +{
514 + struct spmi_controller *ctrl;
515 + int id;
516 +
517 + if (WARN_ON(!parent))
518 + return NULL;
519 +
520 + ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL);
521 + if (!ctrl)
522 + return NULL;
523 +
524 + device_initialize(&ctrl->dev);
525 + ctrl->dev.type = &spmi_ctrl_type;
526 + ctrl->dev.bus = &spmi_bus_type;
527 + ctrl->dev.parent = parent;
528 + ctrl->dev.of_node = parent->of_node;
529 + spmi_controller_set_drvdata(ctrl, &ctrl[1]);
530 +
531 + id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL);
532 + if (id < 0) {
533 + dev_err(parent,
534 + "unable to allocate SPMI controller identifier.\n");
535 + spmi_controller_put(ctrl);
536 + return NULL;
537 + }
538 +
539 + ctrl->nr = id;
540 + dev_set_name(&ctrl->dev, "spmi-%d", id);
541 +
542 + dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id);
543 + return ctrl;
544 +}
545 +EXPORT_SYMBOL_GPL(spmi_controller_alloc);
546 +
547 +static void of_spmi_register_devices(struct spmi_controller *ctrl)
548 +{
549 + struct device_node *node;
550 + int err;
551 +
552 + if (!ctrl->dev.of_node)
553 + return;
554 +
555 + for_each_available_child_of_node(ctrl->dev.of_node, node) {
556 + struct spmi_device *sdev;
557 + u32 reg[2];
558 +
559 + dev_dbg(&ctrl->dev, "adding child %s\n", node->full_name);
560 +
561 + err = of_property_read_u32_array(node, "reg", reg, 2);
562 + if (err) {
563 + dev_err(&ctrl->dev,
564 + "node %s err (%d) does not have 'reg' property\n",
565 + node->full_name, err);
566 + continue;
567 + }
568 +
569 + if (reg[1] != SPMI_USID) {
570 + dev_err(&ctrl->dev,
571 + "node %s contains unsupported 'reg' entry\n",
572 + node->full_name);
573 + continue;
574 + }
575 +
576 + if (reg[0] >= SPMI_MAX_SLAVE_ID) {
577 + dev_err(&ctrl->dev,
578 + "invalid usid on node %s\n",
579 + node->full_name);
580 + continue;
581 + }
582 +
583 + dev_dbg(&ctrl->dev, "read usid %02x\n", reg[0]);
584 +
585 + sdev = spmi_device_alloc(ctrl);
586 + if (!sdev)
587 + continue;
588 +
589 + sdev->dev.of_node = node;
590 + sdev->usid = (u8) reg[0];
591 +
592 + err = spmi_device_add(sdev);
593 + if (err) {
594 + dev_err(&sdev->dev,
595 + "failure adding device. status %d\n", err);
596 + spmi_device_put(sdev);
597 + }
598 + }
599 +}
600 +
601 +/**
602 + * spmi_controller_add() - Add an SPMI controller
603 + * @ctrl: controller to be registered.
604 + *
605 + * Register a controller previously allocated via spmi_controller_alloc() with
606 + * the SPMI core.
607 + */
608 +int spmi_controller_add(struct spmi_controller *ctrl)
609 +{
610 + int ret;
611 +
612 + /* Can't register until after driver model init */
613 + if (WARN_ON(!spmi_bus_type.p))
614 + return -EAGAIN;
615 +
616 + ret = device_add(&ctrl->dev);
617 + if (ret)
618 + return ret;
619 +
620 + if (IS_ENABLED(CONFIG_OF))
621 + of_spmi_register_devices(ctrl);
622 +
623 + dev_dbg(&ctrl->dev, "spmi-%d registered: dev:%p\n",
624 + ctrl->nr, &ctrl->dev);
625 +
626 + return 0;
627 +};
628 +EXPORT_SYMBOL_GPL(spmi_controller_add);
629 +
630 +/* Remove a device associated with a controller */
631 +static int spmi_ctrl_remove_device(struct device *dev, void *data)
632 +{
633 + struct spmi_device *spmidev = to_spmi_device(dev);
634 + if (dev->type == &spmi_dev_type)
635 + spmi_device_remove(spmidev);
636 + return 0;
637 +}
638 +
639 +/**
640 + * spmi_controller_remove(): remove an SPMI controller
641 + * @ctrl: controller to remove
642 + *
643 + * Remove a SPMI controller. Caller is responsible for calling
644 + * spmi_controller_put() to discard the allocated controller.
645 + */
646 +void spmi_controller_remove(struct spmi_controller *ctrl)
647 +{
648 + int dummy;
649 +
650 + if (!ctrl)
651 + return;
652 +
653 + dummy = device_for_each_child(&ctrl->dev, NULL,
654 + spmi_ctrl_remove_device);
655 + device_del(&ctrl->dev);
656 +}
657 +EXPORT_SYMBOL_GPL(spmi_controller_remove);
658 +
659 +/**
660 + * spmi_driver_register() - Register client driver with SPMI core
661 + * @sdrv: client driver to be associated with client-device.
662 + *
663 + * This API will register the client driver with the SPMI framework.
664 + * It is typically called from the driver's module-init function.
665 + */
666 +int spmi_driver_register(struct spmi_driver *sdrv)
667 +{
668 + sdrv->driver.bus = &spmi_bus_type;
669 + return driver_register(&sdrv->driver);
670 +}
671 +EXPORT_SYMBOL_GPL(spmi_driver_register);
672 +
673 +static void __exit spmi_exit(void)
674 +{
675 + bus_unregister(&spmi_bus_type);
676 +}
677 +module_exit(spmi_exit);
678 +
679 +static int __init spmi_init(void)
680 +{
681 + return bus_register(&spmi_bus_type);
682 +}
683 +postcore_initcall(spmi_init);
684 +
685 +MODULE_LICENSE("GPL v2");
686 +MODULE_DESCRIPTION("SPMI module");
687 +MODULE_ALIAS("platform:spmi");
688 --- /dev/null
689 +++ b/include/dt-bindings/spmi/spmi.h
690 @@ -0,0 +1,18 @@
691 +/* Copyright (c) 2013, The Linux Foundation. All rights reserved.
692 + *
693 + * This program is free software; you can redistribute it and/or modify
694 + * it under the terms of the GNU General Public License version 2 and
695 + * only version 2 as published by the Free Software Foundation.
696 + *
697 + * This program is distributed in the hope that it will be useful,
698 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
699 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
700 + * GNU General Public License for more details.
701 + */
702 +#ifndef __DT_BINDINGS_SPMI_H
703 +#define __DT_BINDINGS_SPMI_H
704 +
705 +#define SPMI_USID 0
706 +#define SPMI_GSID 1
707 +
708 +#endif
709 --- a/include/linux/mod_devicetable.h
710 +++ b/include/linux/mod_devicetable.h
711 @@ -432,6 +432,14 @@ struct spi_device_id {
712 kernel_ulong_t driver_data; /* Data private to the driver */
713 };
714
715 +#define SPMI_NAME_SIZE 32
716 +#define SPMI_MODULE_PREFIX "spmi:"
717 +
718 +struct spmi_device_id {
719 + char name[SPMI_NAME_SIZE];
720 + kernel_ulong_t driver_data; /* Data private to the driver */
721 +};
722 +
723 /* dmi */
724 enum dmi_field {
725 DMI_NONE,
726 --- /dev/null
727 +++ b/include/linux/spmi.h
728 @@ -0,0 +1,191 @@
729 +/* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
730 + *
731 + * This program is free software; you can redistribute it and/or modify
732 + * it under the terms of the GNU General Public License version 2 and
733 + * only version 2 as published by the Free Software Foundation.
734 + *
735 + * This program is distributed in the hope that it will be useful,
736 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
737 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
738 + * GNU General Public License for more details.
739 + */
740 +#ifndef _LINUX_SPMI_H
741 +#define _LINUX_SPMI_H
742 +
743 +#include <linux/types.h>
744 +#include <linux/device.h>
745 +#include <linux/mod_devicetable.h>
746 +
747 +/* Maximum slave identifier */
748 +#define SPMI_MAX_SLAVE_ID 16
749 +
750 +/* SPMI Commands */
751 +#define SPMI_CMD_EXT_WRITE 0x00
752 +#define SPMI_CMD_RESET 0x10
753 +#define SPMI_CMD_SLEEP 0x11
754 +#define SPMI_CMD_SHUTDOWN 0x12
755 +#define SPMI_CMD_WAKEUP 0x13
756 +#define SPMI_CMD_AUTHENTICATE 0x14
757 +#define SPMI_CMD_MSTR_READ 0x15
758 +#define SPMI_CMD_MSTR_WRITE 0x16
759 +#define SPMI_CMD_TRANSFER_BUS_OWNERSHIP 0x1A
760 +#define SPMI_CMD_DDB_MASTER_READ 0x1B
761 +#define SPMI_CMD_DDB_SLAVE_READ 0x1C
762 +#define SPMI_CMD_EXT_READ 0x20
763 +#define SPMI_CMD_EXT_WRITEL 0x30
764 +#define SPMI_CMD_EXT_READL 0x38
765 +#define SPMI_CMD_WRITE 0x40
766 +#define SPMI_CMD_READ 0x60
767 +#define SPMI_CMD_ZERO_WRITE 0x80
768 +
769 +/**
770 + * struct spmi_device - Basic representation of an SPMI device
771 + * @dev: Driver model representation of the device.
772 + * @ctrl: SPMI controller managing the bus hosting this device.
773 + * @usid: This devices' Unique Slave IDentifier.
774 + */
775 +struct spmi_device {
776 + struct device dev;
777 + struct spmi_controller *ctrl;
778 + u8 usid;
779 +};
780 +
781 +static inline struct spmi_device *to_spmi_device(struct device *d)
782 +{
783 + return container_of(d, struct spmi_device, dev);
784 +}
785 +
786 +static inline void *spmi_device_get_drvdata(const struct spmi_device *sdev)
787 +{
788 + return dev_get_drvdata(&sdev->dev);
789 +}
790 +
791 +static inline void spmi_device_set_drvdata(struct spmi_device *sdev, void *data)
792 +{
793 + dev_set_drvdata(&sdev->dev, data);
794 +}
795 +
796 +struct spmi_device *spmi_device_alloc(struct spmi_controller *ctrl);
797 +
798 +static inline void spmi_device_put(struct spmi_device *sdev)
799 +{
800 + if (sdev)
801 + put_device(&sdev->dev);
802 +}
803 +
804 +int spmi_device_add(struct spmi_device *sdev);
805 +
806 +void spmi_device_remove(struct spmi_device *sdev);
807 +
808 +/**
809 + * struct spmi_controller - interface to the SPMI master controller
810 + * @dev: Driver model representation of the device.
811 + * @nr: board-specific number identifier for this controller/bus
812 + * @cmd: sends a non-data command sequence on the SPMI bus.
813 + * @read_cmd: sends a register read command sequence on the SPMI bus.
814 + * @write_cmd: sends a register write command sequence on the SPMI bus.
815 + */
816 +struct spmi_controller {
817 + struct device dev;
818 + unsigned int nr;
819 + int (*cmd)(struct spmi_controller *ctrl, u8 opcode, u8 sid);
820 + int (*read_cmd)(struct spmi_controller *ctrl, u8 opcode,
821 + u8 sid, u16 addr, u8 *buf, size_t len);
822 + int (*write_cmd)(struct spmi_controller *ctrl, u8 opcode,
823 + u8 sid, u16 addr, const u8 *buf, size_t len);
824 +};
825 +
826 +static inline struct spmi_controller *to_spmi_controller(struct device *d)
827 +{
828 + return container_of(d, struct spmi_controller, dev);
829 +}
830 +
831 +static inline
832 +void *spmi_controller_get_drvdata(const struct spmi_controller *ctrl)
833 +{
834 + return dev_get_drvdata(&ctrl->dev);
835 +}
836 +
837 +static inline void spmi_controller_set_drvdata(struct spmi_controller *ctrl,
838 + void *data)
839 +{
840 + dev_set_drvdata(&ctrl->dev, data);
841 +}
842 +
843 +struct spmi_controller *spmi_controller_alloc(struct device *parent,
844 + size_t size);
845 +
846 +/**
847 + * spmi_controller_put() - decrement controller refcount
848 + * @ctrl SPMI controller.
849 + */
850 +static inline void spmi_controller_put(struct spmi_controller *ctrl)
851 +{
852 + if (ctrl)
853 + put_device(&ctrl->dev);
854 +}
855 +
856 +int spmi_controller_add(struct spmi_controller *ctrl);
857 +void spmi_controller_remove(struct spmi_controller *ctrl);
858 +
859 +/**
860 + * struct spmi_driver - SPMI slave device driver
861 + * @driver: SPMI device drivers should initialize name and owner field of
862 + * this structure.
863 + * @probe: binds this driver to a SPMI device.
864 + * @remove: unbinds this driver from the SPMI device.
865 + * @shutdown: standard shutdown callback used during powerdown/halt.
866 + * @suspend: standard suspend callback used during system suspend.
867 + * @resume: standard resume callback used during system resume.
868 + *
869 + * If PM runtime support is desired for a slave, a device driver can call
870 + * pm_runtime_put() from their probe() routine (and a balancing
871 + * pm_runtime_get() in remove()). PM runtime support for a slave is
872 + * implemented by issuing a SLEEP command to the slave on runtime_suspend(),
873 + * transitioning the slave into the SLEEP state. On runtime_resume(), a WAKEUP
874 + * command is sent to the slave to bring it back to ACTIVE.
875 + */
876 +struct spmi_driver {
877 + struct device_driver driver;
878 + int (*probe)(struct spmi_device *sdev);
879 + void (*remove)(struct spmi_device *sdev);
880 +};
881 +
882 +static inline struct spmi_driver *to_spmi_driver(struct device_driver *d)
883 +{
884 + return container_of(d, struct spmi_driver, driver);
885 +}
886 +
887 +int spmi_driver_register(struct spmi_driver *sdrv);
888 +
889 +/**
890 + * spmi_driver_unregister() - unregister an SPMI client driver
891 + * @sdrv: the driver to unregister
892 + */
893 +static inline void spmi_driver_unregister(struct spmi_driver *sdrv)
894 +{
895 + if (sdrv)
896 + driver_unregister(&sdrv->driver);
897 +}
898 +
899 +#define module_spmi_driver(__spmi_driver) \
900 + module_driver(__spmi_driver, spmi_driver_register, \
901 + spmi_driver_unregister)
902 +
903 +int spmi_register_read(struct spmi_device *sdev, u8 addr, u8 *buf);
904 +int spmi_ext_register_read(struct spmi_device *sdev, u8 addr, u8 *buf,
905 + size_t len);
906 +int spmi_ext_register_readl(struct spmi_device *sdev, u16 addr, u8 *buf,
907 + size_t len);
908 +int spmi_register_write(struct spmi_device *sdev, u8 addr, u8 data);
909 +int spmi_register_zero_write(struct spmi_device *sdev, u8 data);
910 +int spmi_ext_register_write(struct spmi_device *sdev, u8 addr,
911 + const u8 *buf, size_t len);
912 +int spmi_ext_register_writel(struct spmi_device *sdev, u16 addr,
913 + const u8 *buf, size_t len);
914 +int spmi_command_reset(struct spmi_device *sdev);
915 +int spmi_command_sleep(struct spmi_device *sdev);
916 +int spmi_command_wakeup(struct spmi_device *sdev);
917 +int spmi_command_shutdown(struct spmi_device *sdev);
918 +
919 +#endif