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