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