blob: 49a27c70a60b373f1dd2b49097d9287baa6130ac [file] [log] [blame]
Duy Truong790f06d2013-02-13 16:38:12 -08001/* Copyright (c) 2012, The Linux Foundation. All rights reserved.
Kenneth Heitkeee44ade2012-02-08 13:45:33 -07002 *
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/kernel.h>
16#include <linux/errno.h>
17#include <linux/idr.h>
18#include <linux/slab.h>
19#include <linux/of_device.h>
20#include <linux/platform_device.h>
21#include <linux/spmi.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070022#include <linux/module.h>
Mahesh Sivasubramanian006b4582012-05-29 15:55:46 -060023#include <linux/pm_runtime.h>
Kenneth Heitkeee44ade2012-02-08 13:45:33 -070024
Kenneth Heitke98661912012-09-19 18:51:40 -060025#include "spmi-dbgfs.h"
26
Kenneth Heitkeee44ade2012-02-08 13:45:33 -070027struct spmii_boardinfo {
28 struct list_head list;
29 struct spmi_boardinfo board_info;
30};
31
32static DEFINE_MUTEX(board_lock);
33static LIST_HEAD(board_list);
34static LIST_HEAD(spmi_ctrl_list);
35static DEFINE_IDR(ctrl_idr);
36static struct device_type spmi_ctrl_type = { 0 };
37
38#define to_spmi(dev) platform_get_drvdata(to_platform_device(dev))
39
40/* Forward declarations */
41struct bus_type spmi_bus_type;
42static int spmi_register_controller(struct spmi_controller *ctrl);
43
44/**
45 * spmi_busnum_to_ctrl: Map bus number to controller
46 * @busnum: bus number
47 * Returns controller representing this bus number
48 */
49struct spmi_controller *spmi_busnum_to_ctrl(u32 bus_num)
50{
51 struct spmi_controller *ctrl;
52
53 mutex_lock(&board_lock);
54 list_for_each_entry(ctrl, &spmi_ctrl_list, list) {
55 if (bus_num == ctrl->nr) {
56 mutex_unlock(&board_lock);
57 return ctrl;
58 }
59 }
60 mutex_unlock(&board_lock);
61 return NULL;
62}
63EXPORT_SYMBOL_GPL(spmi_busnum_to_ctrl);
64
65/**
66 * spmi_add_controller: Controller bring-up.
67 * @ctrl: controller to be registered.
68 * A controller is registered with the framework using this API. ctrl->nr is the
69 * desired number with which SPMI framework registers the controller.
70 * Function will return -EBUSY if the number is in use.
71 */
72int spmi_add_controller(struct spmi_controller *ctrl)
73{
74 int id;
75 int status;
76
77 pr_debug("adding controller for bus %d (0x%p)\n", ctrl->nr, ctrl);
78
79 if (ctrl->nr & ~MAX_ID_MASK) {
80 pr_err("invalid bus identifier %d\n", ctrl->nr);
81 return -EINVAL;
82 }
83
84retry:
85 if (idr_pre_get(&ctrl_idr, GFP_KERNEL) == 0) {
86 pr_err("no free memory for idr\n");
87 return -ENOMEM;
88 }
89
90 mutex_lock(&board_lock);
91 status = idr_get_new_above(&ctrl_idr, ctrl, ctrl->nr, &id);
92 if (status == 0 && id != ctrl->nr) {
93 status = -EAGAIN;
94 idr_remove(&ctrl_idr, id);
95 }
96 mutex_unlock(&board_lock);
97 if (status == -EAGAIN)
98 goto retry;
99
100 if (status == 0)
101 status = spmi_register_controller(ctrl);
102 return status;
103}
104EXPORT_SYMBOL_GPL(spmi_add_controller);
105
106/**
107 * spmi_del_controller: Controller tear-down.
108 * @ctrl: controller to which this device is to be added to.
109 *
110 * Controller added with the above API is torn down using this API.
111 */
112int spmi_del_controller(struct spmi_controller *ctrl)
113{
114 return -ENXIO;
115}
116EXPORT_SYMBOL_GPL(spmi_del_controller);
117
118#define spmi_device_attr_gr NULL
119#define spmi_device_uevent NULL
120static void spmi_dev_release(struct device *dev)
121{
122 struct spmi_device *spmidev = to_spmi_device(dev);
123 kfree(spmidev);
124}
125
126static struct device_type spmi_dev_type = {
127 .groups = spmi_device_attr_gr,
128 .uevent = spmi_device_uevent,
129 .release = spmi_dev_release,
130};
131
132/**
133 * spmi_alloc_device: Allocate a new SPMI devices.
134 * @ctrl: controller to which this device is to be added to.
135 * Context: can sleep
136 *
137 * Allows a driver to allocate and initialize a SPMI device without
138 * registering it immediately. This allows a driver to directly fill
139 * the spmi_device structure before calling spmi_add_device().
140 *
141 * Caller is responsible to call spmi_add_device() on the returned
142 * spmi_device. If the caller needs to discard the spmi_device without
143 * adding it, then spmi_dev_put() should be called.
144 */
145struct spmi_device *spmi_alloc_device(struct spmi_controller *ctrl)
146{
147 struct spmi_device *spmidev;
148
149 if (!ctrl) {
150 pr_err("Missing SPMI controller\n");
151 return NULL;
152 }
153
154 spmidev = kzalloc(sizeof(*spmidev), GFP_KERNEL);
155 if (!spmidev) {
156 dev_err(&ctrl->dev, "unable to allocate spmi_device\n");
157 return NULL;
158 }
159
160 spmidev->ctrl = ctrl;
161 spmidev->dev.parent = ctrl->dev.parent;
162 spmidev->dev.bus = &spmi_bus_type;
163 spmidev->dev.type = &spmi_dev_type;
164 device_initialize(&spmidev->dev);
165
166 return spmidev;
167}
168EXPORT_SYMBOL_GPL(spmi_alloc_device);
169
170/* Validate the SPMI device structure */
171static struct device *get_valid_device(struct spmi_device *spmidev)
172{
173 struct device *dev;
174
175 if (!spmidev)
176 return NULL;
177
178 dev = &spmidev->dev;
179 if (dev->bus != &spmi_bus_type || dev->type != &spmi_dev_type)
180 return NULL;
181
182 return dev;
183}
184
185/**
186 * spmi_add_device: Add a new device without register board info.
187 * @ctrl: controller to which this device is to be added to.
188 *
189 * Called when device doesn't have an explicit client-driver to be probed, or
190 * the client-driver is a module installed dynamically.
191 */
192int spmi_add_device(struct spmi_device *spmidev)
193{
194 int rc;
195 struct device *dev = get_valid_device(spmidev);
196
197 if (!dev) {
198 pr_err("%s: invalid SPMI device\n", __func__);
199 return -EINVAL;
200 }
201
202 /* Set the device name */
203 dev_set_name(dev, "%s-%p", spmidev->name, spmidev);
204
205 /* Device may be bound to an active driver when this returns */
206 rc = device_add(dev);
207
208 if (rc < 0)
209 dev_err(dev, "Can't add %s, status %d\n", dev_name(dev), rc);
210 else
211 dev_dbg(dev, "device %s registered\n", dev_name(dev));
212
213 return rc;
214}
215EXPORT_SYMBOL_GPL(spmi_add_device);
216
217/**
218 * spmi_new_device: Instantiates a new SPMI device
219 * @ctrl: controller to which this device is to be added to.
220 * @info: board information for this device.
221 *
222 * Returns the new device or NULL.
223 */
224struct spmi_device *spmi_new_device(struct spmi_controller *ctrl,
225 struct spmi_boardinfo const *info)
226{
227 struct spmi_device *spmidev;
228 int rc;
229
230 if (!ctrl || !info)
231 return NULL;
232
233 spmidev = spmi_alloc_device(ctrl);
234 if (!spmidev)
235 return NULL;
236
237 spmidev->name = info->name;
238 spmidev->sid = info->slave_id;
239 spmidev->dev.of_node = info->of_node;
Kenneth Heitkeee44ade2012-02-08 13:45:33 -0700240 spmidev->dev.platform_data = (void *)info->platform_data;
Michael Bohan86622b32012-02-08 16:59:00 -0800241 spmidev->num_dev_node = info->num_dev_node;
242 spmidev->dev_node = info->dev_node;
Michael Bohan978d3352012-05-25 15:02:38 -0700243 spmidev->res = info->res;
Kenneth Heitkeee44ade2012-02-08 13:45:33 -0700244
245 rc = spmi_add_device(spmidev);
246 if (rc < 0) {
247 spmi_dev_put(spmidev);
248 return NULL;
249 }
250
251 return spmidev;
252}
253EXPORT_SYMBOL_GPL(spmi_new_device);
254
255/* spmi_remove_device: Remove the effect of spmi_add_device() */
256void spmi_remove_device(struct spmi_device *spmi_dev)
257{
258 device_unregister(&spmi_dev->dev);
259}
260EXPORT_SYMBOL_GPL(spmi_remove_device);
261
262/* If controller is not present, only add to boards list */
263static void spmi_match_ctrl_to_boardinfo(struct spmi_controller *ctrl,
264 struct spmi_boardinfo *bi)
265{
266 struct spmi_device *spmidev;
267
268 spmidev = spmi_new_device(ctrl, bi);
269 if (!spmidev)
270 dev_err(ctrl->dev.parent, "can't create new device for %s\n",
271 bi->name);
272}
273
274/**
275 * spmi_register_board_info: Board-initialization routine.
276 * @bus_num: controller number (bus) on which this device will sit.
277 * @info: list of all devices on all controllers present on the board.
278 * @n: number of entries.
279 * API enumerates respective devices on corresponding controller.
280 * Called from board-init function.
281 */
282int spmi_register_board_info(int busnum,
283 struct spmi_boardinfo const *info, unsigned n)
284{
285 int i;
286 struct spmii_boardinfo *bi;
287
288 bi = kzalloc(n * sizeof(*bi), GFP_KERNEL);
289 if (!bi)
290 return -ENOMEM;
291
292 for (i = 0; i < n; i++, bi++, info++) {
293 struct spmi_controller *ctrl;
294
295 memcpy(&bi->board_info, info, sizeof(*info));
296 mutex_lock(&board_lock);
297 list_add_tail(&bi->list, &board_list);
298 list_for_each_entry(ctrl, &spmi_ctrl_list, list)
299 if (ctrl->nr == busnum)
300 spmi_match_ctrl_to_boardinfo(ctrl,
301 &bi->board_info);
302 mutex_unlock(&board_lock);
303 }
304 return 0;
305}
306EXPORT_SYMBOL_GPL(spmi_register_board_info);
307
308/* ------------------------------------------------------------------------- */
309
310static inline int
311spmi_cmd(struct spmi_controller *ctrl, u8 opcode, u8 sid)
312{
313 BUG_ON(!ctrl || !ctrl->cmd);
314 return ctrl->cmd(ctrl, opcode, sid);
315}
316
317static inline int spmi_read_cmd(struct spmi_controller *ctrl,
318 u8 opcode, u8 sid, u16 addr, u8 bc, u8 *buf)
319{
320 BUG_ON(!ctrl || !ctrl->read_cmd);
321 return ctrl->read_cmd(ctrl, opcode, sid, addr, bc, buf);
322}
323
324static inline int spmi_write_cmd(struct spmi_controller *ctrl,
325 u8 opcode, u8 sid, u16 addr, u8 bc, u8 *buf)
326{
327 BUG_ON(!ctrl || !ctrl->write_cmd);
328 return ctrl->write_cmd(ctrl, opcode, sid, addr, bc, buf);
329}
330
331/*
332 * register read/write: 5-bit address, 1 byte of data
333 * extended register read/write: 8-bit address, up to 16 bytes of data
334 * extended register read/write long: 16-bit address, up to 8 bytes of data
335 */
336
337/**
338 * spmi_register_read() - register read
339 * @dev: SPMI device.
340 * @sid: slave identifier.
341 * @ad: slave register address (5-bit address).
342 * @buf: buffer to be populated with data from the Slave.
343 *
344 * Reads 1 byte of data from a Slave device register.
345 */
346int spmi_register_read(struct spmi_controller *ctrl, u8 sid, u8 addr, u8 *buf)
347{
348 /* 4-bit Slave Identifier, 5-bit register address */
349 if (sid > SPMI_MAX_SLAVE_ID || addr > 0x1F)
350 return -EINVAL;
351
352 return spmi_read_cmd(ctrl, SPMI_CMD_READ, sid, addr, 0, buf);
353}
354EXPORT_SYMBOL_GPL(spmi_register_read);
355
356/**
357 * spmi_ext_register_read() - extended register read
358 * @dev: SPMI device.
359 * @sid: slave identifier.
360 * @ad: slave register address (8-bit address).
361 * @len: the request number of bytes to read (up to 16 bytes).
362 * @buf: buffer to be populated with data from the Slave.
363 *
364 * Reads up to 16 bytes of data from the extended register space on a
365 * Slave device.
366 */
367int spmi_ext_register_read(struct spmi_controller *ctrl,
368 u8 sid, u8 addr, u8 *buf, int len)
369{
370 /* 4-bit Slave Identifier, 8-bit register address, up to 16 bytes */
371 if (sid > SPMI_MAX_SLAVE_ID || len <= 0 || len > 16)
372 return -EINVAL;
373
374 return spmi_read_cmd(ctrl, SPMI_CMD_EXT_READ, sid, addr, len - 1, buf);
375}
376EXPORT_SYMBOL_GPL(spmi_ext_register_read);
377
378/**
379 * spmi_ext_register_readl() - extended register read long
380 * @dev: SPMI device.
381 * @sid: slave identifier.
382 * @ad: slave register address (16-bit address).
383 * @len: the request number of bytes to read (up to 8 bytes).
384 * @buf: buffer to be populated with data from the Slave.
385 *
386 * Reads up to 8 bytes of data from the extended register space on a
387 * Slave device using 16-bit address.
388 */
389int spmi_ext_register_readl(struct spmi_controller *ctrl,
390 u8 sid, u16 addr, u8 *buf, int len)
391{
392 /* 4-bit Slave Identifier, 16-bit register address, up to 8 bytes */
393 if (sid > SPMI_MAX_SLAVE_ID || len <= 0 || len > 8)
394 return -EINVAL;
395
396 return spmi_read_cmd(ctrl, SPMI_CMD_EXT_READL, sid, addr, len - 1, buf);
397}
398EXPORT_SYMBOL_GPL(spmi_ext_register_readl);
399
400/**
401 * spmi_register_write() - register write
402 * @dev: SPMI device.
403 * @sid: slave identifier.
404 * @ad: slave register address (5-bit address).
405 * @buf: buffer containing the data to be transferred to the Slave.
406 *
407 * Writes 1 byte of data to a Slave device register.
408 */
409int spmi_register_write(struct spmi_controller *ctrl, u8 sid, u8 addr, u8 *buf)
410{
411 u8 op = SPMI_CMD_WRITE;
412
413 /* 4-bit Slave Identifier, 5-bit register address */
414 if (sid > SPMI_MAX_SLAVE_ID || addr > 0x1F)
415 return -EINVAL;
416
417 return spmi_write_cmd(ctrl, op, sid, addr, 0, buf);
418}
419EXPORT_SYMBOL_GPL(spmi_register_write);
420
421/**
422 * spmi_register_zero_write() - register zero write
423 * @dev: SPMI device.
424 * @sid: slave identifier.
425 * @data: the data to be written to register 0 (7-bits).
426 *
427 * Writes data to register 0 of the Slave device.
428 */
429int spmi_register_zero_write(struct spmi_controller *ctrl, u8 sid, u8 data)
430{
431 u8 op = SPMI_CMD_ZERO_WRITE;
432
433 /* 4-bit Slave Identifier, 5-bit register address */
434 if (sid > SPMI_MAX_SLAVE_ID)
435 return -EINVAL;
436
437 return spmi_write_cmd(ctrl, op, sid, 0, 0, &data);
438}
439EXPORT_SYMBOL_GPL(spmi_register_zero_write);
440
441/**
442 * spmi_ext_register_write() - extended register write
443 * @dev: SPMI device.
444 * @sid: slave identifier.
445 * @ad: slave register address (8-bit address).
446 * @buf: buffer containing the data to be transferred to the Slave.
447 * @len: the request number of bytes to read (up to 16 bytes).
448 *
449 * Writes up to 16 bytes of data to the extended register space of a
450 * Slave device.
451 */
452int spmi_ext_register_write(struct spmi_controller *ctrl,
453 u8 sid, u8 addr, u8 *buf, int len)
454{
455 u8 op = SPMI_CMD_EXT_WRITE;
456
457 /* 4-bit Slave Identifier, 8-bit register address, up to 16 bytes */
458 if (sid > SPMI_MAX_SLAVE_ID || len <= 0 || len > 16)
459 return -EINVAL;
460
461 return spmi_write_cmd(ctrl, op, sid, addr, len - 1, buf);
462}
463EXPORT_SYMBOL_GPL(spmi_ext_register_write);
464
465/**
466 * spmi_ext_register_writel() - extended register write long
467 * @dev: SPMI device.
468 * @sid: slave identifier.
469 * @ad: slave register address (16-bit address).
470 * @buf: buffer containing the data to be transferred to the Slave.
471 * @len: the request number of bytes to read (up to 8 bytes).
472 *
473 * Writes up to 8 bytes of data to the extended register space of a
474 * Slave device using 16-bit address.
475 */
476int spmi_ext_register_writel(struct spmi_controller *ctrl,
477 u8 sid, u16 addr, u8 *buf, int len)
478{
479 u8 op = SPMI_CMD_EXT_WRITEL;
480
481 /* 4-bit Slave Identifier, 16-bit register address, up to 8 bytes */
482 if (sid > SPMI_MAX_SLAVE_ID || len <= 0 || len > 8)
483 return -EINVAL;
484
485 return spmi_write_cmd(ctrl, op, sid, addr, len - 1, buf);
486}
487EXPORT_SYMBOL_GPL(spmi_ext_register_writel);
488
489/**
490 * spmi_command_reset() - sends RESET command to the specified slave
491 * @dev: SPMI device.
492 * @sid: slave identifier.
493 *
494 * The Reset command initializes the Slave and forces all registers to
495 * their reset values. The Slave shall enter the STARTUP state after
496 * receiving a Reset command.
497 *
498 * Returns
499 * -EINVAL for invalid Slave Identifier.
500 * -EPERM if the SPMI transaction is denied due to permission issues.
501 * -EIO if the SPMI transaction fails (parity errors, etc).
502 * -ETIMEDOUT if the SPMI transaction times out.
503 */
504int spmi_command_reset(struct spmi_controller *ctrl, u8 sid)
505{
506 if (sid > SPMI_MAX_SLAVE_ID)
507 return -EINVAL;
508 return spmi_cmd(ctrl, SPMI_CMD_RESET, sid);
509}
510EXPORT_SYMBOL_GPL(spmi_command_reset);
511
512/**
513 * spmi_command_sleep() - sends SLEEP command to the specified slave
514 * @dev: SPMI device.
515 * @sid: slave identifier.
516 *
517 * The Sleep command causes the Slave to enter the user defined SLEEP state.
518 *
519 * Returns
520 * -EINVAL for invalid Slave Identifier.
521 * -EPERM if the SPMI transaction is denied due to permission issues.
522 * -EIO if the SPMI transaction fails (parity errors, etc).
523 * -ETIMEDOUT if the SPMI transaction times out.
524 */
525int spmi_command_sleep(struct spmi_controller *ctrl, u8 sid)
526{
527 if (sid > SPMI_MAX_SLAVE_ID)
528 return -EINVAL;
529 return spmi_cmd(ctrl, SPMI_CMD_SLEEP, sid);
530}
531EXPORT_SYMBOL_GPL(spmi_command_sleep);
532
533/**
534 * spmi_command_wakeup() - sends WAKEUP command to the specified slave
535 * @dev: SPMI device.
536 * @sid: slave identifier.
537 *
538 * The Wakeup command causes the Slave to move from the SLEEP state to
539 * the ACTIVE state.
540 *
541 * Returns
542 * -EINVAL for invalid Slave Identifier.
543 * -EPERM if the SPMI transaction is denied due to permission issues.
544 * -EIO if the SPMI transaction fails (parity errors, etc).
545 * -ETIMEDOUT if the SPMI transaction times out.
546 */
547int spmi_command_wakeup(struct spmi_controller *ctrl, u8 sid)
548{
549 if (sid > SPMI_MAX_SLAVE_ID)
550 return -EINVAL;
551 return spmi_cmd(ctrl, SPMI_CMD_WAKEUP, sid);
552}
553EXPORT_SYMBOL_GPL(spmi_command_wakeup);
554
555/**
556 * spmi_command_shutdown() - sends SHUTDOWN command to the specified slave
557 * @dev: SPMI device.
558 * @sid: slave identifier.
559 *
560 * The Shutdown command causes the Slave to enter the SHUTDOWN state.
561 *
562 * Returns
563 * -EINVAL for invalid Slave Identifier.
564 * -EPERM if the SPMI transaction is denied due to permission issues.
565 * -EIO if the SPMI transaction fails (parity errors, etc).
566 * -ETIMEDOUT if the SPMI transaction times out.
567 */
568int spmi_command_shutdown(struct spmi_controller *ctrl, u8 sid)
569{
570 if (sid > SPMI_MAX_SLAVE_ID)
571 return -EINVAL;
572 return spmi_cmd(ctrl, SPMI_CMD_SHUTDOWN, sid);
573}
574EXPORT_SYMBOL_GPL(spmi_command_shutdown);
575
576/* ------------------------------------------------------------------------- */
577
578static const struct spmi_device_id *spmi_match(const struct spmi_device_id *id,
579 const struct spmi_device *spmi_dev)
580{
581 while (id->name[0]) {
582 if (strncmp(spmi_dev->name, id->name, SPMI_NAME_SIZE) == 0)
583 return id;
584 id++;
585 }
586 return NULL;
587}
588
589static int spmi_device_match(struct device *dev, struct device_driver *drv)
590{
591 struct spmi_device *spmi_dev;
592 struct spmi_driver *sdrv = to_spmi_driver(drv);
593
594 if (dev->type == &spmi_dev_type)
595 spmi_dev = to_spmi_device(dev);
596 else
597 return 0;
598
599 /* Attempt an OF style match */
600 if (of_driver_match_device(dev, drv))
601 return 1;
602
603 if (sdrv->id_table)
604 return spmi_match(sdrv->id_table, spmi_dev) != NULL;
605
606 if (drv->name)
607 return strncmp(spmi_dev->name, drv->name, SPMI_NAME_SIZE) == 0;
608 return 0;
609}
610
611#ifdef CONFIG_PM_SLEEP
612static int spmi_legacy_suspend(struct device *dev, pm_message_t mesg)
613{
614 struct spmi_device *spmi_dev = NULL;
615 struct spmi_driver *driver;
616 if (dev->type == &spmi_dev_type)
617 spmi_dev = to_spmi_device(dev);
618
619 if (!spmi_dev || !dev->driver)
620 return 0;
621
622 driver = to_spmi_driver(dev->driver);
623 if (!driver->suspend)
624 return 0;
625
626 return driver->suspend(spmi_dev, mesg);
627}
628
629static int spmi_legacy_resume(struct device *dev)
630{
631 struct spmi_device *spmi_dev = NULL;
632 struct spmi_driver *driver;
633 if (dev->type == &spmi_dev_type)
634 spmi_dev = to_spmi_device(dev);
635
636 if (!spmi_dev || !dev->driver)
637 return 0;
638
639 driver = to_spmi_driver(dev->driver);
640 if (!driver->resume)
641 return 0;
642
643 return driver->resume(spmi_dev);
644}
645
646static int spmi_pm_suspend(struct device *dev)
647{
648 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
649
650 if (pm)
651 return pm_generic_suspend(dev);
652 else
653 return spmi_legacy_suspend(dev, PMSG_SUSPEND);
654}
655
656static int spmi_pm_resume(struct device *dev)
657{
658 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
659
660 if (pm)
661 return pm_generic_resume(dev);
662 else
663 return spmi_legacy_resume(dev);
664}
665
666#else
667#define spmi_pm_suspend NULL
668#define spmi_pm_resume NULL
669#endif
670
671static const struct dev_pm_ops spmi_pm_ops = {
672 .suspend = spmi_pm_suspend,
673 .resume = spmi_pm_resume,
674 SET_RUNTIME_PM_OPS(
675 pm_generic_suspend,
676 pm_generic_resume,
677 pm_generic_runtime_idle
678 )
679};
680struct bus_type spmi_bus_type = {
681 .name = "spmi",
682 .match = spmi_device_match,
683 .pm = &spmi_pm_ops,
684};
685EXPORT_SYMBOL_GPL(spmi_bus_type);
686
687struct device spmi_dev = {
688 .init_name = "spmi",
689};
690
691static int spmi_drv_probe(struct device *dev)
692{
693 const struct spmi_driver *sdrv = to_spmi_driver(dev->driver);
694
695 return sdrv->probe(to_spmi_device(dev));
696}
697
698static int spmi_drv_remove(struct device *dev)
699{
700 const struct spmi_driver *sdrv = to_spmi_driver(dev->driver);
701
702 return sdrv->remove(to_spmi_device(dev));
703}
704
705static void spmi_drv_shutdown(struct device *dev)
706{
707 const struct spmi_driver *sdrv = to_spmi_driver(dev->driver);
708
709 sdrv->shutdown(to_spmi_device(dev));
710}
711
712/**
713 * spmi_driver_register: Client driver registration with SPMI framework.
714 * @drv: client driver to be associated with client-device.
715 *
716 * This API will register the client driver with the SPMI framework.
717 * It is called from the driver's module-init function.
718 */
719int spmi_driver_register(struct spmi_driver *drv)
720{
721 drv->driver.bus = &spmi_bus_type;
722
723 if (drv->probe)
724 drv->driver.probe = spmi_drv_probe;
725
726 if (drv->remove)
727 drv->driver.remove = spmi_drv_remove;
728
729 if (drv->shutdown)
730 drv->driver.shutdown = spmi_drv_shutdown;
731
732 return driver_register(&drv->driver);
733}
734EXPORT_SYMBOL_GPL(spmi_driver_register);
735
736static int spmi_register_controller(struct spmi_controller *ctrl)
737{
738 int ret = 0;
739
740 /* Can't register until after driver model init */
741 if (WARN_ON(!spmi_bus_type.p)) {
742 ret = -EAGAIN;
743 goto exit;
744 }
745
746 dev_set_name(&ctrl->dev, "spmi-%d", ctrl->nr);
747 ctrl->dev.bus = &spmi_bus_type;
748 ctrl->dev.type = &spmi_ctrl_type;
749 ret = device_register(&ctrl->dev);
750 if (ret)
751 goto exit;
752
753 dev_dbg(&ctrl->dev, "Bus spmi-%d registered: dev:%x\n",
754 ctrl->nr, (u32)&ctrl->dev);
755
756 mutex_lock(&board_lock);
757 list_add_tail(&ctrl->list, &spmi_ctrl_list);
758 mutex_unlock(&board_lock);
759
Kenneth Heitke98661912012-09-19 18:51:40 -0600760 spmi_dfs_add_controller(ctrl);
Kenneth Heitkeee44ade2012-02-08 13:45:33 -0700761 return 0;
762
763exit:
764 mutex_lock(&board_lock);
765 idr_remove(&ctrl_idr, ctrl->nr);
766 mutex_unlock(&board_lock);
767 return ret;
768}
769
770static void __exit spmi_exit(void)
771{
772 device_unregister(&spmi_dev);
773 bus_unregister(&spmi_bus_type);
774}
775
776static int __init spmi_init(void)
777{
778 int retval;
779
780 retval = bus_register(&spmi_bus_type);
781 if (!retval)
782 retval = device_register(&spmi_dev);
783
784 if (retval)
785 bus_unregister(&spmi_bus_type);
786
787 return retval;
788}
789postcore_initcall(spmi_init);
790module_exit(spmi_exit);
791
792MODULE_LICENSE("GPL v2");
793MODULE_VERSION("1.0");
794MODULE_DESCRIPTION("SPMI module");
795MODULE_ALIAS("platform:spmi");