1 /* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
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.
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.
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/idr.h>
15 #include <linux/slab.h>
16 #include <linux/module.h>
18 #include <linux/of_device.h>
19 #include <linux/platform_device.h>
20 #include <linux/spmi.h>
21 #include <linux/module.h>
22 #include <linux/pm_runtime.h>
24 #include <dt-bindings/spmi/spmi.h>
26 static DEFINE_IDA(ctrl_ida);
28 static void spmi_dev_release(struct device *dev)
30 struct spmi_device *sdev = to_spmi_device(dev);
34 static const struct device_type spmi_dev_type = {
35 .release = spmi_dev_release,
38 static void spmi_ctrl_release(struct device *dev)
40 struct spmi_controller *ctrl = to_spmi_controller(dev);
41 ida_simple_remove(&ctrl_ida, ctrl->nr);
45 static const struct device_type spmi_ctrl_type = {
46 .release = spmi_ctrl_release,
49 #ifdef CONFIG_PM_RUNTIME
50 static int spmi_runtime_suspend(struct device *dev)
52 struct spmi_device *sdev = to_spmi_device(dev);
55 err = pm_generic_runtime_suspend(dev);
59 return spmi_command_sleep(sdev);
62 static int spmi_runtime_resume(struct device *dev)
64 struct spmi_device *sdev = to_spmi_device(dev);
67 err = spmi_command_wakeup(sdev);
71 return pm_generic_runtime_resume(dev);
75 static const struct dev_pm_ops spmi_pm_ops = {
83 static int spmi_device_match(struct device *dev, struct device_driver *drv)
85 if (of_driver_match_device(dev, drv))
89 return strncmp(dev_name(dev), drv->name,
96 * spmi_device_add() - add a device previously constructed via spmi_device_alloc()
97 * @sdev: spmi_device to be added
99 int spmi_device_add(struct spmi_device *sdev)
101 struct spmi_controller *ctrl = sdev->ctrl;
104 dev_set_name(&sdev->dev, "%d-%02x", ctrl->nr, sdev->usid);
106 err = device_add(&sdev->dev);
108 dev_err(&sdev->dev, "Can't add %s, status %d\n",
109 dev_name(&sdev->dev), err);
113 dev_dbg(&sdev->dev, "device %s registered\n", dev_name(&sdev->dev));
118 EXPORT_SYMBOL_GPL(spmi_device_add);
121 * spmi_device_remove(): remove an SPMI device
122 * @sdev: spmi_device to be removed
124 void spmi_device_remove(struct spmi_device *sdev)
126 device_unregister(&sdev->dev);
128 EXPORT_SYMBOL_GPL(spmi_device_remove);
131 spmi_cmd(struct spmi_controller *ctrl, u8 opcode, u8 sid)
133 if (!ctrl || !ctrl->cmd || ctrl->dev.type != &spmi_ctrl_type)
136 return ctrl->cmd(ctrl, opcode, sid);
139 static inline int spmi_read_cmd(struct spmi_controller *ctrl, u8 opcode,
140 u8 sid, u16 addr, u8 *buf, size_t len)
142 if (!ctrl || !ctrl->read_cmd || ctrl->dev.type != &spmi_ctrl_type)
145 return ctrl->read_cmd(ctrl, opcode, sid, addr, buf, len);
148 static inline int spmi_write_cmd(struct spmi_controller *ctrl, u8 opcode,
149 u8 sid, u16 addr, const u8 *buf, size_t len)
151 if (!ctrl || !ctrl->write_cmd || ctrl->dev.type != &spmi_ctrl_type)
154 return ctrl->write_cmd(ctrl, opcode, sid, addr, buf, len);
158 * spmi_register_read() - register read
159 * @sdev: SPMI device.
160 * @addr: slave register address (5-bit address).
161 * @buf: buffer to be populated with data from the Slave.
163 * Reads 1 byte of data from a Slave device register.
165 int spmi_register_read(struct spmi_device *sdev, u8 addr, u8 *buf)
167 /* 5-bit register address */
171 return spmi_read_cmd(sdev->ctrl, SPMI_CMD_READ, sdev->usid, addr,
174 EXPORT_SYMBOL_GPL(spmi_register_read);
177 * spmi_ext_register_read() - extended register read
178 * @sdev: SPMI device.
179 * @addr: slave register address (8-bit address).
180 * @buf: buffer to be populated with data from the Slave.
181 * @len: the request number of bytes to read (up to 16 bytes).
183 * Reads up to 16 bytes of data from the extended register space on a
186 int spmi_ext_register_read(struct spmi_device *sdev, u8 addr, u8 *buf,
189 /* 8-bit register address, up to 16 bytes */
190 if (len == 0 || len > 16)
193 return spmi_read_cmd(sdev->ctrl, SPMI_CMD_EXT_READ, sdev->usid, addr,
196 EXPORT_SYMBOL_GPL(spmi_ext_register_read);
199 * spmi_ext_register_readl() - extended register read long
200 * @sdev: SPMI device.
201 * @addr: slave register address (16-bit address).
202 * @buf: buffer to be populated with data from the Slave.
203 * @len: the request number of bytes to read (up to 8 bytes).
205 * Reads up to 8 bytes of data from the extended register space on a
206 * Slave device using 16-bit address.
208 int spmi_ext_register_readl(struct spmi_device *sdev, u16 addr, u8 *buf,
211 /* 16-bit register address, up to 8 bytes */
212 if (len == 0 || len > 8)
215 return spmi_read_cmd(sdev->ctrl, SPMI_CMD_EXT_READL, sdev->usid, addr,
218 EXPORT_SYMBOL_GPL(spmi_ext_register_readl);
221 * spmi_register_write() - register write
223 * @addr: slave register address (5-bit address).
224 * @data: buffer containing the data to be transferred to the Slave.
226 * Writes 1 byte of data to a Slave device register.
228 int spmi_register_write(struct spmi_device *sdev, u8 addr, u8 data)
230 /* 5-bit register address */
234 return spmi_write_cmd(sdev->ctrl, SPMI_CMD_WRITE, sdev->usid, addr,
237 EXPORT_SYMBOL_GPL(spmi_register_write);
240 * spmi_register_zero_write() - register zero write
241 * @sdev: SPMI device.
242 * @data: the data to be written to register 0 (7-bits).
244 * Writes data to register 0 of the Slave device.
246 int spmi_register_zero_write(struct spmi_device *sdev, u8 data)
248 return spmi_write_cmd(sdev->ctrl, SPMI_CMD_ZERO_WRITE, sdev->usid, 0,
251 EXPORT_SYMBOL_GPL(spmi_register_zero_write);
254 * spmi_ext_register_write() - extended register write
255 * @sdev: SPMI device.
256 * @addr: slave register address (8-bit address).
257 * @buf: buffer containing the data to be transferred to the Slave.
258 * @len: the request number of bytes to read (up to 16 bytes).
260 * Writes up to 16 bytes of data to the extended register space of a
263 int spmi_ext_register_write(struct spmi_device *sdev, u8 addr, const u8 *buf,
266 /* 8-bit register address, up to 16 bytes */
267 if (len == 0 || len > 16)
270 return spmi_write_cmd(sdev->ctrl, SPMI_CMD_EXT_WRITE, sdev->usid, addr,
273 EXPORT_SYMBOL_GPL(spmi_ext_register_write);
276 * spmi_ext_register_writel() - extended register write long
277 * @sdev: SPMI device.
278 * @addr: slave register address (16-bit address).
279 * @buf: buffer containing the data to be transferred to the Slave.
280 * @len: the request number of bytes to read (up to 8 bytes).
282 * Writes up to 8 bytes of data to the extended register space of a
283 * Slave device using 16-bit address.
285 int spmi_ext_register_writel(struct spmi_device *sdev, u16 addr, const u8 *buf,
288 /* 4-bit Slave Identifier, 16-bit register address, up to 8 bytes */
289 if (len == 0 || len > 8)
292 return spmi_write_cmd(sdev->ctrl, SPMI_CMD_EXT_WRITEL, sdev->usid,
295 EXPORT_SYMBOL_GPL(spmi_ext_register_writel);
298 * spmi_command_reset() - sends RESET command to the specified slave
299 * @sdev: SPMI device.
301 * The Reset command initializes the Slave and forces all registers to
302 * their reset values. The Slave shall enter the STARTUP state after
303 * receiving a Reset command.
305 int spmi_command_reset(struct spmi_device *sdev)
307 return spmi_cmd(sdev->ctrl, SPMI_CMD_RESET, sdev->usid);
309 EXPORT_SYMBOL_GPL(spmi_command_reset);
312 * spmi_command_sleep() - sends SLEEP command to the specified SPMI device
313 * @sdev: SPMI device.
315 * The Sleep command causes the Slave to enter the user defined SLEEP state.
317 int spmi_command_sleep(struct spmi_device *sdev)
319 return spmi_cmd(sdev->ctrl, SPMI_CMD_SLEEP, sdev->usid);
321 EXPORT_SYMBOL_GPL(spmi_command_sleep);
324 * spmi_command_wakeup() - sends WAKEUP command to the specified SPMI device
325 * @sdev: SPMI device.
327 * The Wakeup command causes the Slave to move from the SLEEP state to
330 int spmi_command_wakeup(struct spmi_device *sdev)
332 return spmi_cmd(sdev->ctrl, SPMI_CMD_WAKEUP, sdev->usid);
334 EXPORT_SYMBOL_GPL(spmi_command_wakeup);
337 * spmi_command_shutdown() - sends SHUTDOWN command to the specified SPMI device
338 * @sdev: SPMI device.
340 * The Shutdown command causes the Slave to enter the SHUTDOWN state.
342 int spmi_command_shutdown(struct spmi_device *sdev)
344 return spmi_cmd(sdev->ctrl, SPMI_CMD_SHUTDOWN, sdev->usid);
346 EXPORT_SYMBOL_GPL(spmi_command_shutdown);
348 static int spmi_drv_probe(struct device *dev)
350 const struct spmi_driver *sdrv = to_spmi_driver(dev->driver);
351 struct spmi_device *sdev = to_spmi_device(dev);
354 /* Ensure the slave is in ACTIVE state */
355 err = spmi_command_wakeup(sdev);
359 pm_runtime_get_noresume(dev);
360 pm_runtime_set_active(dev);
361 pm_runtime_enable(dev);
363 err = sdrv->probe(sdev);
370 pm_runtime_disable(dev);
371 pm_runtime_set_suspended(dev);
372 pm_runtime_put_noidle(dev);
377 static int spmi_drv_remove(struct device *dev)
379 const struct spmi_driver *sdrv = to_spmi_driver(dev->driver);
381 pm_runtime_get_sync(dev);
382 sdrv->remove(to_spmi_device(dev));
383 pm_runtime_put_noidle(dev);
385 pm_runtime_disable(dev);
386 pm_runtime_set_suspended(dev);
387 pm_runtime_put_noidle(dev);
391 static struct bus_type spmi_bus_type = {
393 .match = spmi_device_match,
395 .probe = spmi_drv_probe,
396 .remove = spmi_drv_remove,
400 * spmi_controller_alloc() - Allocate a new SPMI device
401 * @ctrl: associated controller
403 * Caller is responsible for either calling spmi_device_add() to add the
404 * newly allocated controller, or calling spmi_device_put() to discard it.
406 struct spmi_device *spmi_device_alloc(struct spmi_controller *ctrl)
408 struct spmi_device *sdev;
410 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
415 device_initialize(&sdev->dev);
416 sdev->dev.parent = &ctrl->dev;
417 sdev->dev.bus = &spmi_bus_type;
418 sdev->dev.type = &spmi_dev_type;
421 EXPORT_SYMBOL_GPL(spmi_device_alloc);
424 * spmi_controller_alloc() - Allocate a new SPMI controller
425 * @parent: parent device
426 * @size: size of private data
428 * Caller is responsible for either calling spmi_controller_add() to add the
429 * newly allocated controller, or calling spmi_controller_put() to discard it.
430 * The allocated private data region may be accessed via
431 * spmi_controller_get_drvdata()
433 struct spmi_controller *spmi_controller_alloc(struct device *parent,
436 struct spmi_controller *ctrl;
439 if (WARN_ON(!parent))
442 ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL);
446 device_initialize(&ctrl->dev);
447 ctrl->dev.type = &spmi_ctrl_type;
448 ctrl->dev.bus = &spmi_bus_type;
449 ctrl->dev.parent = parent;
450 ctrl->dev.of_node = parent->of_node;
451 spmi_controller_set_drvdata(ctrl, &ctrl[1]);
453 id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL);
456 "unable to allocate SPMI controller identifier.\n");
457 spmi_controller_put(ctrl);
462 dev_set_name(&ctrl->dev, "spmi-%d", id);
464 dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id);
467 EXPORT_SYMBOL_GPL(spmi_controller_alloc);
469 static void of_spmi_register_devices(struct spmi_controller *ctrl)
471 struct device_node *node;
474 if (!ctrl->dev.of_node)
477 for_each_available_child_of_node(ctrl->dev.of_node, node) {
478 struct spmi_device *sdev;
481 dev_dbg(&ctrl->dev, "adding child %s\n", node->full_name);
483 err = of_property_read_u32_array(node, "reg", reg, 2);
486 "node %s err (%d) does not have 'reg' property\n",
487 node->full_name, err);
491 if (reg[1] != SPMI_USID) {
493 "node %s contains unsupported 'reg' entry\n",
498 if (reg[0] >= SPMI_MAX_SLAVE_ID) {
500 "invalid usid on node %s\n",
505 dev_dbg(&ctrl->dev, "read usid %02x\n", reg[0]);
507 sdev = spmi_device_alloc(ctrl);
511 sdev->dev.of_node = node;
512 sdev->usid = (u8) reg[0];
514 err = spmi_device_add(sdev);
517 "failure adding device. status %d\n", err);
518 spmi_device_put(sdev);
524 * spmi_controller_add() - Add an SPMI controller
525 * @ctrl: controller to be registered.
527 * Register a controller previously allocated via spmi_controller_alloc() with
530 int spmi_controller_add(struct spmi_controller *ctrl)
534 /* Can't register until after driver model init */
535 if (WARN_ON(!spmi_bus_type.p))
538 ret = device_add(&ctrl->dev);
542 if (IS_ENABLED(CONFIG_OF))
543 of_spmi_register_devices(ctrl);
545 dev_dbg(&ctrl->dev, "spmi-%d registered: dev:%p\n",
546 ctrl->nr, &ctrl->dev);
550 EXPORT_SYMBOL_GPL(spmi_controller_add);
552 /* Remove a device associated with a controller */
553 static int spmi_ctrl_remove_device(struct device *dev, void *data)
555 struct spmi_device *spmidev = to_spmi_device(dev);
556 if (dev->type == &spmi_dev_type)
557 spmi_device_remove(spmidev);
562 * spmi_controller_remove(): remove an SPMI controller
563 * @ctrl: controller to remove
565 * Remove a SPMI controller. Caller is responsible for calling
566 * spmi_controller_put() to discard the allocated controller.
568 void spmi_controller_remove(struct spmi_controller *ctrl)
575 dummy = device_for_each_child(&ctrl->dev, NULL,
576 spmi_ctrl_remove_device);
577 device_del(&ctrl->dev);
579 EXPORT_SYMBOL_GPL(spmi_controller_remove);
582 * spmi_driver_register() - Register client driver with SPMI core
583 * @sdrv: client driver to be associated with client-device.
585 * This API will register the client driver with the SPMI framework.
586 * It is typically called from the driver's module-init function.
588 int spmi_driver_register(struct spmi_driver *sdrv)
590 sdrv->driver.bus = &spmi_bus_type;
591 return driver_register(&sdrv->driver);
593 EXPORT_SYMBOL_GPL(spmi_driver_register);
595 static void __exit spmi_exit(void)
597 bus_unregister(&spmi_bus_type);
599 module_exit(spmi_exit);
601 static int __init spmi_init(void)
603 return bus_register(&spmi_bus_type);
605 postcore_initcall(spmi_init);
607 MODULE_LICENSE("GPL v2");
608 MODULE_DESCRIPTION("SPMI module");
609 MODULE_ALIAS("platform:spmi");