1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
5 #include <linux/kernel.h>
6 #include <linux/errno.h>
8 #include <linux/slab.h>
9 #include <linux/module.h>
11 #include <linux/of_device.h>
12 #include <linux/platform_device.h>
13 #include <linux/spmi.h>
14 #include <linux/pm_runtime.h>
16 #include <dt-bindings/spmi/spmi.h>
17 #define CREATE_TRACE_POINTS
18 #include <trace/events/spmi.h>
20 static bool is_registered
;
21 static DEFINE_IDA(ctrl_ida
);
23 static void spmi_dev_release(struct device
*dev
)
25 struct spmi_device
*sdev
= to_spmi_device(dev
);
30 static const struct device_type spmi_dev_type
= {
31 .release
= spmi_dev_release
,
34 static void spmi_ctrl_release(struct device
*dev
)
36 struct spmi_controller
*ctrl
= to_spmi_controller(dev
);
38 ida_free(&ctrl_ida
, ctrl
->nr
);
42 static const struct device_type spmi_ctrl_type
= {
43 .release
= spmi_ctrl_release
,
46 static int spmi_device_match(struct device
*dev
, const struct device_driver
*drv
)
48 if (of_driver_match_device(dev
, drv
))
52 return strncmp(dev_name(dev
), drv
->name
,
59 * spmi_device_add() - add a device previously constructed via spmi_device_alloc()
60 * @sdev: spmi_device to be added
62 int spmi_device_add(struct spmi_device
*sdev
)
64 struct spmi_controller
*ctrl
= sdev
->ctrl
;
67 dev_set_name(&sdev
->dev
, "%d-%02x", ctrl
->nr
, sdev
->usid
);
69 err
= device_add(&sdev
->dev
);
71 dev_err(&sdev
->dev
, "Can't add %s, status %d\n",
72 dev_name(&sdev
->dev
), err
);
76 dev_dbg(&sdev
->dev
, "device %s registered\n", dev_name(&sdev
->dev
));
81 EXPORT_SYMBOL_GPL(spmi_device_add
);
84 * spmi_device_remove(): remove an SPMI device
85 * @sdev: spmi_device to be removed
87 void spmi_device_remove(struct spmi_device
*sdev
)
89 device_unregister(&sdev
->dev
);
91 EXPORT_SYMBOL_GPL(spmi_device_remove
);
94 spmi_cmd(struct spmi_controller
*ctrl
, u8 opcode
, u8 sid
)
98 if (!ctrl
|| !ctrl
->cmd
|| ctrl
->dev
.type
!= &spmi_ctrl_type
)
101 ret
= ctrl
->cmd(ctrl
, opcode
, sid
);
102 trace_spmi_cmd(opcode
, sid
, ret
);
106 static inline int spmi_read_cmd(struct spmi_controller
*ctrl
, u8 opcode
,
107 u8 sid
, u16 addr
, u8
*buf
, size_t len
)
111 if (!ctrl
|| !ctrl
->read_cmd
|| ctrl
->dev
.type
!= &spmi_ctrl_type
)
114 trace_spmi_read_begin(opcode
, sid
, addr
);
115 ret
= ctrl
->read_cmd(ctrl
, opcode
, sid
, addr
, buf
, len
);
116 trace_spmi_read_end(opcode
, sid
, addr
, ret
, len
, buf
);
120 static inline int spmi_write_cmd(struct spmi_controller
*ctrl
, u8 opcode
,
121 u8 sid
, u16 addr
, const u8
*buf
, size_t len
)
125 if (!ctrl
|| !ctrl
->write_cmd
|| ctrl
->dev
.type
!= &spmi_ctrl_type
)
128 trace_spmi_write_begin(opcode
, sid
, addr
, len
, buf
);
129 ret
= ctrl
->write_cmd(ctrl
, opcode
, sid
, addr
, buf
, len
);
130 trace_spmi_write_end(opcode
, sid
, addr
, ret
);
135 * spmi_register_read() - register read
136 * @sdev: SPMI device.
137 * @addr: slave register address (5-bit address).
138 * @buf: buffer to be populated with data from the Slave.
140 * Reads 1 byte of data from a Slave device register.
142 int spmi_register_read(struct spmi_device
*sdev
, u8 addr
, u8
*buf
)
144 /* 5-bit register address */
148 return spmi_read_cmd(sdev
->ctrl
, SPMI_CMD_READ
, sdev
->usid
, addr
,
151 EXPORT_SYMBOL_GPL(spmi_register_read
);
154 * spmi_ext_register_read() - extended register read
155 * @sdev: SPMI device.
156 * @addr: slave register address (8-bit address).
157 * @buf: buffer to be populated with data from the Slave.
158 * @len: the request number of bytes to read (up to 16 bytes).
160 * Reads up to 16 bytes of data from the extended register space on a
163 int spmi_ext_register_read(struct spmi_device
*sdev
, u8 addr
, u8
*buf
,
166 /* 8-bit register address, up to 16 bytes */
167 if (len
== 0 || len
> 16)
170 return spmi_read_cmd(sdev
->ctrl
, SPMI_CMD_EXT_READ
, sdev
->usid
, addr
,
173 EXPORT_SYMBOL_GPL(spmi_ext_register_read
);
176 * spmi_ext_register_readl() - extended register read long
177 * @sdev: SPMI device.
178 * @addr: slave register address (16-bit address).
179 * @buf: buffer to be populated with data from the Slave.
180 * @len: the request number of bytes to read (up to 8 bytes).
182 * Reads up to 8 bytes of data from the extended register space on a
183 * Slave device using 16-bit address.
185 int spmi_ext_register_readl(struct spmi_device
*sdev
, u16 addr
, u8
*buf
,
188 /* 16-bit register address, up to 8 bytes */
189 if (len
== 0 || len
> 8)
192 return spmi_read_cmd(sdev
->ctrl
, SPMI_CMD_EXT_READL
, sdev
->usid
, addr
,
195 EXPORT_SYMBOL_GPL(spmi_ext_register_readl
);
198 * spmi_register_write() - register write
200 * @addr: slave register address (5-bit address).
201 * @data: buffer containing the data to be transferred to the Slave.
203 * Writes 1 byte of data to a Slave device register.
205 int spmi_register_write(struct spmi_device
*sdev
, u8 addr
, u8 data
)
207 /* 5-bit register address */
211 return spmi_write_cmd(sdev
->ctrl
, SPMI_CMD_WRITE
, sdev
->usid
, addr
,
214 EXPORT_SYMBOL_GPL(spmi_register_write
);
217 * spmi_register_zero_write() - register zero write
218 * @sdev: SPMI device.
219 * @data: the data to be written to register 0 (7-bits).
221 * Writes data to register 0 of the Slave device.
223 int spmi_register_zero_write(struct spmi_device
*sdev
, u8 data
)
225 return spmi_write_cmd(sdev
->ctrl
, SPMI_CMD_ZERO_WRITE
, sdev
->usid
, 0,
228 EXPORT_SYMBOL_GPL(spmi_register_zero_write
);
231 * spmi_ext_register_write() - extended register write
232 * @sdev: SPMI device.
233 * @addr: slave register address (8-bit address).
234 * @buf: buffer containing the data to be transferred to the Slave.
235 * @len: the request number of bytes to read (up to 16 bytes).
237 * Writes up to 16 bytes of data to the extended register space of a
240 int spmi_ext_register_write(struct spmi_device
*sdev
, u8 addr
, const u8
*buf
,
243 /* 8-bit register address, up to 16 bytes */
244 if (len
== 0 || len
> 16)
247 return spmi_write_cmd(sdev
->ctrl
, SPMI_CMD_EXT_WRITE
, sdev
->usid
, addr
,
250 EXPORT_SYMBOL_GPL(spmi_ext_register_write
);
253 * spmi_ext_register_writel() - extended register write long
254 * @sdev: SPMI device.
255 * @addr: slave register address (16-bit address).
256 * @buf: buffer containing the data to be transferred to the Slave.
257 * @len: the request number of bytes to read (up to 8 bytes).
259 * Writes up to 8 bytes of data to the extended register space of a
260 * Slave device using 16-bit address.
262 int spmi_ext_register_writel(struct spmi_device
*sdev
, u16 addr
, const u8
*buf
,
265 /* 4-bit Slave Identifier, 16-bit register address, up to 8 bytes */
266 if (len
== 0 || len
> 8)
269 return spmi_write_cmd(sdev
->ctrl
, SPMI_CMD_EXT_WRITEL
, sdev
->usid
,
272 EXPORT_SYMBOL_GPL(spmi_ext_register_writel
);
275 * spmi_command_reset() - sends RESET command to the specified slave
276 * @sdev: SPMI device.
278 * The Reset command initializes the Slave and forces all registers to
279 * their reset values. The Slave shall enter the STARTUP state after
280 * receiving a Reset command.
282 int spmi_command_reset(struct spmi_device
*sdev
)
284 return spmi_cmd(sdev
->ctrl
, SPMI_CMD_RESET
, sdev
->usid
);
286 EXPORT_SYMBOL_GPL(spmi_command_reset
);
289 * spmi_command_sleep() - sends SLEEP command to the specified SPMI device
290 * @sdev: SPMI device.
292 * The Sleep command causes the Slave to enter the user defined SLEEP state.
294 int spmi_command_sleep(struct spmi_device
*sdev
)
296 return spmi_cmd(sdev
->ctrl
, SPMI_CMD_SLEEP
, sdev
->usid
);
298 EXPORT_SYMBOL_GPL(spmi_command_sleep
);
301 * spmi_command_wakeup() - sends WAKEUP command to the specified SPMI device
302 * @sdev: SPMI device.
304 * The Wakeup command causes the Slave to move from the SLEEP state to
307 int spmi_command_wakeup(struct spmi_device
*sdev
)
309 return spmi_cmd(sdev
->ctrl
, SPMI_CMD_WAKEUP
, sdev
->usid
);
311 EXPORT_SYMBOL_GPL(spmi_command_wakeup
);
314 * spmi_command_shutdown() - sends SHUTDOWN command to the specified SPMI device
315 * @sdev: SPMI device.
317 * The Shutdown command causes the Slave to enter the SHUTDOWN state.
319 int spmi_command_shutdown(struct spmi_device
*sdev
)
321 return spmi_cmd(sdev
->ctrl
, SPMI_CMD_SHUTDOWN
, sdev
->usid
);
323 EXPORT_SYMBOL_GPL(spmi_command_shutdown
);
325 static int spmi_drv_probe(struct device
*dev
)
327 const struct spmi_driver
*sdrv
= to_spmi_driver(dev
->driver
);
328 struct spmi_device
*sdev
= to_spmi_device(dev
);
331 pm_runtime_get_noresume(dev
);
332 pm_runtime_set_active(dev
);
333 pm_runtime_enable(dev
);
335 err
= sdrv
->probe(sdev
);
342 pm_runtime_disable(dev
);
343 pm_runtime_set_suspended(dev
);
344 pm_runtime_put_noidle(dev
);
348 static void spmi_drv_remove(struct device
*dev
)
350 const struct spmi_driver
*sdrv
= to_spmi_driver(dev
->driver
);
352 pm_runtime_get_sync(dev
);
354 sdrv
->remove(to_spmi_device(dev
));
355 pm_runtime_put_noidle(dev
);
357 pm_runtime_disable(dev
);
358 pm_runtime_set_suspended(dev
);
359 pm_runtime_put_noidle(dev
);
362 static void spmi_drv_shutdown(struct device
*dev
)
364 const struct spmi_driver
*sdrv
= to_spmi_driver(dev
->driver
);
366 if (sdrv
&& sdrv
->shutdown
)
367 sdrv
->shutdown(to_spmi_device(dev
));
370 static int spmi_drv_uevent(const struct device
*dev
, struct kobj_uevent_env
*env
)
374 ret
= of_device_uevent_modalias(dev
, env
);
381 static const struct bus_type spmi_bus_type
= {
383 .match
= spmi_device_match
,
384 .probe
= spmi_drv_probe
,
385 .remove
= spmi_drv_remove
,
386 .shutdown
= spmi_drv_shutdown
,
387 .uevent
= spmi_drv_uevent
,
391 * spmi_find_device_by_of_node() - look up an SPMI device from a device node
395 * Takes a reference to the embedded struct device which needs to be dropped
398 * Returns the struct spmi_device associated with a device node or NULL.
400 struct spmi_device
*spmi_find_device_by_of_node(struct device_node
*np
)
402 struct device
*dev
= bus_find_device_by_of_node(&spmi_bus_type
, np
);
405 return to_spmi_device(dev
);
408 EXPORT_SYMBOL_GPL(spmi_find_device_by_of_node
);
411 * spmi_device_alloc() - Allocate a new SPMI device
412 * @ctrl: associated controller
414 * Caller is responsible for either calling spmi_device_add() to add the
415 * newly allocated controller, or calling spmi_device_put() to discard it.
417 struct spmi_device
*spmi_device_alloc(struct spmi_controller
*ctrl
)
419 struct spmi_device
*sdev
;
421 sdev
= kzalloc(sizeof(*sdev
), GFP_KERNEL
);
426 device_initialize(&sdev
->dev
);
427 sdev
->dev
.parent
= &ctrl
->dev
;
428 sdev
->dev
.bus
= &spmi_bus_type
;
429 sdev
->dev
.type
= &spmi_dev_type
;
432 EXPORT_SYMBOL_GPL(spmi_device_alloc
);
435 * spmi_controller_alloc() - Allocate a new SPMI controller
436 * @parent: parent device
437 * @size: size of private data
439 * Caller is responsible for either calling spmi_controller_add() to add the
440 * newly allocated controller, or calling spmi_controller_put() to discard it.
441 * The allocated private data region may be accessed via
442 * spmi_controller_get_drvdata()
444 struct spmi_controller
*spmi_controller_alloc(struct device
*parent
,
447 struct spmi_controller
*ctrl
;
450 if (WARN_ON(!parent
))
451 return ERR_PTR(-EINVAL
);
453 ctrl
= kzalloc(sizeof(*ctrl
) + size
, GFP_KERNEL
);
455 return ERR_PTR(-ENOMEM
);
457 device_initialize(&ctrl
->dev
);
458 ctrl
->dev
.type
= &spmi_ctrl_type
;
459 ctrl
->dev
.bus
= &spmi_bus_type
;
460 ctrl
->dev
.parent
= parent
;
461 ctrl
->dev
.of_node
= parent
->of_node
;
462 spmi_controller_set_drvdata(ctrl
, &ctrl
[1]);
464 id
= ida_alloc(&ctrl_ida
, GFP_KERNEL
);
467 "unable to allocate SPMI controller identifier.\n");
468 spmi_controller_put(ctrl
);
473 dev_set_name(&ctrl
->dev
, "spmi-%d", id
);
475 dev_dbg(&ctrl
->dev
, "allocated controller 0x%p id %d\n", ctrl
, id
);
478 EXPORT_SYMBOL_GPL(spmi_controller_alloc
);
480 static void of_spmi_register_devices(struct spmi_controller
*ctrl
)
482 struct device_node
*node
;
485 if (!ctrl
->dev
.of_node
)
488 for_each_available_child_of_node(ctrl
->dev
.of_node
, node
) {
489 struct spmi_device
*sdev
;
492 dev_dbg(&ctrl
->dev
, "adding child %pOF\n", node
);
494 err
= of_property_read_u32_array(node
, "reg", reg
, 2);
497 "node %pOF err (%d) does not have 'reg' property\n",
502 if (reg
[1] != SPMI_USID
) {
504 "node %pOF contains unsupported 'reg' entry\n",
509 if (reg
[0] >= SPMI_MAX_SLAVE_ID
) {
510 dev_err(&ctrl
->dev
, "invalid usid on node %pOF\n", node
);
514 dev_dbg(&ctrl
->dev
, "read usid %02x\n", reg
[0]);
516 sdev
= spmi_device_alloc(ctrl
);
520 sdev
->dev
.of_node
= node
;
521 sdev
->usid
= (u8
)reg
[0];
523 err
= spmi_device_add(sdev
);
526 "failure adding device. status %d\n", err
);
527 spmi_device_put(sdev
);
533 * spmi_controller_add() - Add an SPMI controller
534 * @ctrl: controller to be registered.
536 * Register a controller previously allocated via spmi_controller_alloc() with
539 int spmi_controller_add(struct spmi_controller
*ctrl
)
543 /* Can't register until after driver model init */
544 if (WARN_ON(!is_registered
))
547 ret
= device_add(&ctrl
->dev
);
551 if (IS_ENABLED(CONFIG_OF
))
552 of_spmi_register_devices(ctrl
);
554 dev_dbg(&ctrl
->dev
, "spmi-%d registered: dev:%p\n",
555 ctrl
->nr
, &ctrl
->dev
);
559 EXPORT_SYMBOL_GPL(spmi_controller_add
);
561 /* Remove a device associated with a controller */
562 static int spmi_ctrl_remove_device(struct device
*dev
, void *data
)
564 struct spmi_device
*spmidev
= to_spmi_device(dev
);
566 if (dev
->type
== &spmi_dev_type
)
567 spmi_device_remove(spmidev
);
572 * spmi_controller_remove(): remove an SPMI controller
573 * @ctrl: controller to remove
575 * Remove a SPMI controller. Caller is responsible for calling
576 * spmi_controller_put() to discard the allocated controller.
578 void spmi_controller_remove(struct spmi_controller
*ctrl
)
583 device_for_each_child(&ctrl
->dev
, NULL
, spmi_ctrl_remove_device
);
584 device_del(&ctrl
->dev
);
586 EXPORT_SYMBOL_GPL(spmi_controller_remove
);
589 * __spmi_driver_register() - Register client driver with SPMI core
590 * @sdrv: client driver to be associated with client-device.
591 * @owner: module owner
593 * This API will register the client driver with the SPMI framework.
594 * It is typically called from the driver's module-init function.
596 int __spmi_driver_register(struct spmi_driver
*sdrv
, struct module
*owner
)
598 sdrv
->driver
.bus
= &spmi_bus_type
;
599 sdrv
->driver
.owner
= owner
;
600 return driver_register(&sdrv
->driver
);
602 EXPORT_SYMBOL_GPL(__spmi_driver_register
);
604 static void __exit
spmi_exit(void)
606 bus_unregister(&spmi_bus_type
);
608 module_exit(spmi_exit
);
610 static int __init
spmi_init(void)
614 ret
= bus_register(&spmi_bus_type
);
618 is_registered
= true;
621 postcore_initcall(spmi_init
);
623 MODULE_LICENSE("GPL v2");
624 MODULE_DESCRIPTION("SPMI module");
625 MODULE_ALIAS("platform:spmi");