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