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
);
29 static const struct device_type spmi_dev_type
= {
30 .release
= spmi_dev_release
,
33 static void spmi_ctrl_release(struct device
*dev
)
35 struct spmi_controller
*ctrl
= to_spmi_controller(dev
);
36 ida_simple_remove(&ctrl_ida
, ctrl
->nr
);
40 static const struct device_type spmi_ctrl_type
= {
41 .release
= spmi_ctrl_release
,
44 static int spmi_device_match(struct device
*dev
, struct device_driver
*drv
)
46 if (of_driver_match_device(dev
, drv
))
50 return strncmp(dev_name(dev
), drv
->name
,
57 * spmi_device_add() - add a device previously constructed via spmi_device_alloc()
58 * @sdev: spmi_device to be added
60 int spmi_device_add(struct spmi_device
*sdev
)
62 struct spmi_controller
*ctrl
= sdev
->ctrl
;
65 dev_set_name(&sdev
->dev
, "%d-%02x", ctrl
->nr
, sdev
->usid
);
67 err
= device_add(&sdev
->dev
);
69 dev_err(&sdev
->dev
, "Can't add %s, status %d\n",
70 dev_name(&sdev
->dev
), err
);
74 dev_dbg(&sdev
->dev
, "device %s registered\n", dev_name(&sdev
->dev
));
79 EXPORT_SYMBOL_GPL(spmi_device_add
);
82 * spmi_device_remove(): remove an SPMI device
83 * @sdev: spmi_device to be removed
85 void spmi_device_remove(struct spmi_device
*sdev
)
87 device_unregister(&sdev
->dev
);
89 EXPORT_SYMBOL_GPL(spmi_device_remove
);
92 spmi_cmd(struct spmi_controller
*ctrl
, u8 opcode
, u8 sid
)
96 if (!ctrl
|| !ctrl
->cmd
|| ctrl
->dev
.type
!= &spmi_ctrl_type
)
99 ret
= ctrl
->cmd(ctrl
, opcode
, sid
);
100 trace_spmi_cmd(opcode
, sid
, ret
);
104 static inline int spmi_read_cmd(struct spmi_controller
*ctrl
, u8 opcode
,
105 u8 sid
, u16 addr
, u8
*buf
, size_t len
)
109 if (!ctrl
|| !ctrl
->read_cmd
|| ctrl
->dev
.type
!= &spmi_ctrl_type
)
112 trace_spmi_read_begin(opcode
, sid
, addr
);
113 ret
= ctrl
->read_cmd(ctrl
, opcode
, sid
, addr
, buf
, len
);
114 trace_spmi_read_end(opcode
, sid
, addr
, ret
, len
, buf
);
118 static inline int spmi_write_cmd(struct spmi_controller
*ctrl
, u8 opcode
,
119 u8 sid
, u16 addr
, const u8
*buf
, size_t len
)
123 if (!ctrl
|| !ctrl
->write_cmd
|| ctrl
->dev
.type
!= &spmi_ctrl_type
)
126 trace_spmi_write_begin(opcode
, sid
, addr
, len
, buf
);
127 ret
= ctrl
->write_cmd(ctrl
, opcode
, sid
, addr
, buf
, len
);
128 trace_spmi_write_end(opcode
, sid
, addr
, ret
);
133 * spmi_register_read() - register read
134 * @sdev: SPMI device.
135 * @addr: slave register address (5-bit address).
136 * @buf: buffer to be populated with data from the Slave.
138 * Reads 1 byte of data from a Slave device register.
140 int spmi_register_read(struct spmi_device
*sdev
, u8 addr
, u8
*buf
)
142 /* 5-bit register address */
146 return spmi_read_cmd(sdev
->ctrl
, SPMI_CMD_READ
, sdev
->usid
, addr
,
149 EXPORT_SYMBOL_GPL(spmi_register_read
);
152 * spmi_ext_register_read() - extended register read
153 * @sdev: SPMI device.
154 * @addr: slave register address (8-bit address).
155 * @buf: buffer to be populated with data from the Slave.
156 * @len: the request number of bytes to read (up to 16 bytes).
158 * Reads up to 16 bytes of data from the extended register space on a
161 int spmi_ext_register_read(struct spmi_device
*sdev
, u8 addr
, u8
*buf
,
164 /* 8-bit register address, up to 16 bytes */
165 if (len
== 0 || len
> 16)
168 return spmi_read_cmd(sdev
->ctrl
, SPMI_CMD_EXT_READ
, sdev
->usid
, addr
,
171 EXPORT_SYMBOL_GPL(spmi_ext_register_read
);
174 * spmi_ext_register_readl() - extended register read long
175 * @sdev: SPMI device.
176 * @addr: slave register address (16-bit address).
177 * @buf: buffer to be populated with data from the Slave.
178 * @len: the request number of bytes to read (up to 8 bytes).
180 * Reads up to 8 bytes of data from the extended register space on a
181 * Slave device using 16-bit address.
183 int spmi_ext_register_readl(struct spmi_device
*sdev
, u16 addr
, u8
*buf
,
186 /* 16-bit register address, up to 8 bytes */
187 if (len
== 0 || len
> 8)
190 return spmi_read_cmd(sdev
->ctrl
, SPMI_CMD_EXT_READL
, sdev
->usid
, addr
,
193 EXPORT_SYMBOL_GPL(spmi_ext_register_readl
);
196 * spmi_register_write() - register write
198 * @addr: slave register address (5-bit address).
199 * @data: buffer containing the data to be transferred to the Slave.
201 * Writes 1 byte of data to a Slave device register.
203 int spmi_register_write(struct spmi_device
*sdev
, u8 addr
, u8 data
)
205 /* 5-bit register address */
209 return spmi_write_cmd(sdev
->ctrl
, SPMI_CMD_WRITE
, sdev
->usid
, addr
,
212 EXPORT_SYMBOL_GPL(spmi_register_write
);
215 * spmi_register_zero_write() - register zero write
216 * @sdev: SPMI device.
217 * @data: the data to be written to register 0 (7-bits).
219 * Writes data to register 0 of the Slave device.
221 int spmi_register_zero_write(struct spmi_device
*sdev
, u8 data
)
223 return spmi_write_cmd(sdev
->ctrl
, SPMI_CMD_ZERO_WRITE
, sdev
->usid
, 0,
226 EXPORT_SYMBOL_GPL(spmi_register_zero_write
);
229 * spmi_ext_register_write() - extended register write
230 * @sdev: SPMI device.
231 * @addr: slave register address (8-bit address).
232 * @buf: buffer containing the data to be transferred to the Slave.
233 * @len: the request number of bytes to read (up to 16 bytes).
235 * Writes up to 16 bytes of data to the extended register space of a
238 int spmi_ext_register_write(struct spmi_device
*sdev
, u8 addr
, const u8
*buf
,
241 /* 8-bit register address, up to 16 bytes */
242 if (len
== 0 || len
> 16)
245 return spmi_write_cmd(sdev
->ctrl
, SPMI_CMD_EXT_WRITE
, sdev
->usid
, addr
,
248 EXPORT_SYMBOL_GPL(spmi_ext_register_write
);
251 * spmi_ext_register_writel() - extended register write long
252 * @sdev: SPMI device.
253 * @addr: slave register address (16-bit address).
254 * @buf: buffer containing the data to be transferred to the Slave.
255 * @len: the request number of bytes to read (up to 8 bytes).
257 * Writes up to 8 bytes of data to the extended register space of a
258 * Slave device using 16-bit address.
260 int spmi_ext_register_writel(struct spmi_device
*sdev
, u16 addr
, const u8
*buf
,
263 /* 4-bit Slave Identifier, 16-bit register address, up to 8 bytes */
264 if (len
== 0 || len
> 8)
267 return spmi_write_cmd(sdev
->ctrl
, SPMI_CMD_EXT_WRITEL
, sdev
->usid
,
270 EXPORT_SYMBOL_GPL(spmi_ext_register_writel
);
273 * spmi_command_reset() - sends RESET command to the specified slave
274 * @sdev: SPMI device.
276 * The Reset command initializes the Slave and forces all registers to
277 * their reset values. The Slave shall enter the STARTUP state after
278 * receiving a Reset command.
280 int spmi_command_reset(struct spmi_device
*sdev
)
282 return spmi_cmd(sdev
->ctrl
, SPMI_CMD_RESET
, sdev
->usid
);
284 EXPORT_SYMBOL_GPL(spmi_command_reset
);
287 * spmi_command_sleep() - sends SLEEP command to the specified SPMI device
288 * @sdev: SPMI device.
290 * The Sleep command causes the Slave to enter the user defined SLEEP state.
292 int spmi_command_sleep(struct spmi_device
*sdev
)
294 return spmi_cmd(sdev
->ctrl
, SPMI_CMD_SLEEP
, sdev
->usid
);
296 EXPORT_SYMBOL_GPL(spmi_command_sleep
);
299 * spmi_command_wakeup() - sends WAKEUP command to the specified SPMI device
300 * @sdev: SPMI device.
302 * The Wakeup command causes the Slave to move from the SLEEP state to
305 int spmi_command_wakeup(struct spmi_device
*sdev
)
307 return spmi_cmd(sdev
->ctrl
, SPMI_CMD_WAKEUP
, sdev
->usid
);
309 EXPORT_SYMBOL_GPL(spmi_command_wakeup
);
312 * spmi_command_shutdown() - sends SHUTDOWN command to the specified SPMI device
313 * @sdev: SPMI device.
315 * The Shutdown command causes the Slave to enter the SHUTDOWN state.
317 int spmi_command_shutdown(struct spmi_device
*sdev
)
319 return spmi_cmd(sdev
->ctrl
, SPMI_CMD_SHUTDOWN
, sdev
->usid
);
321 EXPORT_SYMBOL_GPL(spmi_command_shutdown
);
323 static int spmi_drv_probe(struct device
*dev
)
325 const struct spmi_driver
*sdrv
= to_spmi_driver(dev
->driver
);
326 struct spmi_device
*sdev
= to_spmi_device(dev
);
329 pm_runtime_get_noresume(dev
);
330 pm_runtime_set_active(dev
);
331 pm_runtime_enable(dev
);
333 err
= sdrv
->probe(sdev
);
340 pm_runtime_disable(dev
);
341 pm_runtime_set_suspended(dev
);
342 pm_runtime_put_noidle(dev
);
346 static int spmi_drv_remove(struct device
*dev
)
348 const struct spmi_driver
*sdrv
= to_spmi_driver(dev
->driver
);
350 pm_runtime_get_sync(dev
);
351 sdrv
->remove(to_spmi_device(dev
));
352 pm_runtime_put_noidle(dev
);
354 pm_runtime_disable(dev
);
355 pm_runtime_set_suspended(dev
);
356 pm_runtime_put_noidle(dev
);
360 static int spmi_drv_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
364 ret
= of_device_uevent_modalias(dev
, env
);
371 static struct bus_type spmi_bus_type
= {
373 .match
= spmi_device_match
,
374 .probe
= spmi_drv_probe
,
375 .remove
= spmi_drv_remove
,
376 .uevent
= spmi_drv_uevent
,
380 * spmi_controller_alloc() - Allocate a new SPMI device
381 * @ctrl: associated controller
383 * Caller is responsible for either calling spmi_device_add() to add the
384 * newly allocated controller, or calling spmi_device_put() to discard it.
386 struct spmi_device
*spmi_device_alloc(struct spmi_controller
*ctrl
)
388 struct spmi_device
*sdev
;
390 sdev
= kzalloc(sizeof(*sdev
), GFP_KERNEL
);
395 device_initialize(&sdev
->dev
);
396 sdev
->dev
.parent
= &ctrl
->dev
;
397 sdev
->dev
.bus
= &spmi_bus_type
;
398 sdev
->dev
.type
= &spmi_dev_type
;
401 EXPORT_SYMBOL_GPL(spmi_device_alloc
);
404 * spmi_controller_alloc() - Allocate a new SPMI controller
405 * @parent: parent device
406 * @size: size of private data
408 * Caller is responsible for either calling spmi_controller_add() to add the
409 * newly allocated controller, or calling spmi_controller_put() to discard it.
410 * The allocated private data region may be accessed via
411 * spmi_controller_get_drvdata()
413 struct spmi_controller
*spmi_controller_alloc(struct device
*parent
,
416 struct spmi_controller
*ctrl
;
419 if (WARN_ON(!parent
))
422 ctrl
= kzalloc(sizeof(*ctrl
) + size
, GFP_KERNEL
);
426 device_initialize(&ctrl
->dev
);
427 ctrl
->dev
.type
= &spmi_ctrl_type
;
428 ctrl
->dev
.bus
= &spmi_bus_type
;
429 ctrl
->dev
.parent
= parent
;
430 ctrl
->dev
.of_node
= parent
->of_node
;
431 spmi_controller_set_drvdata(ctrl
, &ctrl
[1]);
433 id
= ida_simple_get(&ctrl_ida
, 0, 0, GFP_KERNEL
);
436 "unable to allocate SPMI controller identifier.\n");
437 spmi_controller_put(ctrl
);
442 dev_set_name(&ctrl
->dev
, "spmi-%d", id
);
444 dev_dbg(&ctrl
->dev
, "allocated controller 0x%p id %d\n", ctrl
, id
);
447 EXPORT_SYMBOL_GPL(spmi_controller_alloc
);
449 static void of_spmi_register_devices(struct spmi_controller
*ctrl
)
451 struct device_node
*node
;
454 if (!ctrl
->dev
.of_node
)
457 for_each_available_child_of_node(ctrl
->dev
.of_node
, node
) {
458 struct spmi_device
*sdev
;
461 dev_dbg(&ctrl
->dev
, "adding child %pOF\n", node
);
463 err
= of_property_read_u32_array(node
, "reg", reg
, 2);
466 "node %pOF err (%d) does not have 'reg' property\n",
471 if (reg
[1] != SPMI_USID
) {
473 "node %pOF contains unsupported 'reg' entry\n",
478 if (reg
[0] >= SPMI_MAX_SLAVE_ID
) {
479 dev_err(&ctrl
->dev
, "invalid usid on node %pOF\n", node
);
483 dev_dbg(&ctrl
->dev
, "read usid %02x\n", reg
[0]);
485 sdev
= spmi_device_alloc(ctrl
);
489 sdev
->dev
.of_node
= node
;
490 sdev
->usid
= (u8
) reg
[0];
492 err
= spmi_device_add(sdev
);
495 "failure adding device. status %d\n", err
);
496 spmi_device_put(sdev
);
502 * spmi_controller_add() - Add an SPMI controller
503 * @ctrl: controller to be registered.
505 * Register a controller previously allocated via spmi_controller_alloc() with
508 int spmi_controller_add(struct spmi_controller
*ctrl
)
512 /* Can't register until after driver model init */
513 if (WARN_ON(!is_registered
))
516 ret
= device_add(&ctrl
->dev
);
520 if (IS_ENABLED(CONFIG_OF
))
521 of_spmi_register_devices(ctrl
);
523 dev_dbg(&ctrl
->dev
, "spmi-%d registered: dev:%p\n",
524 ctrl
->nr
, &ctrl
->dev
);
528 EXPORT_SYMBOL_GPL(spmi_controller_add
);
530 /* Remove a device associated with a controller */
531 static int spmi_ctrl_remove_device(struct device
*dev
, void *data
)
533 struct spmi_device
*spmidev
= to_spmi_device(dev
);
534 if (dev
->type
== &spmi_dev_type
)
535 spmi_device_remove(spmidev
);
540 * spmi_controller_remove(): remove an SPMI controller
541 * @ctrl: controller to remove
543 * Remove a SPMI controller. Caller is responsible for calling
544 * spmi_controller_put() to discard the allocated controller.
546 void spmi_controller_remove(struct spmi_controller
*ctrl
)
553 dummy
= device_for_each_child(&ctrl
->dev
, NULL
,
554 spmi_ctrl_remove_device
);
555 device_del(&ctrl
->dev
);
557 EXPORT_SYMBOL_GPL(spmi_controller_remove
);
560 * spmi_driver_register() - Register client driver with SPMI core
561 * @sdrv: client driver to be associated with client-device.
563 * This API will register the client driver with the SPMI framework.
564 * It is typically called from the driver's module-init function.
566 int __spmi_driver_register(struct spmi_driver
*sdrv
, struct module
*owner
)
568 sdrv
->driver
.bus
= &spmi_bus_type
;
569 sdrv
->driver
.owner
= owner
;
570 return driver_register(&sdrv
->driver
);
572 EXPORT_SYMBOL_GPL(__spmi_driver_register
);
574 static void __exit
spmi_exit(void)
576 bus_unregister(&spmi_bus_type
);
578 module_exit(spmi_exit
);
580 static int __init
spmi_init(void)
584 ret
= bus_register(&spmi_bus_type
);
588 is_registered
= true;
591 postcore_initcall(spmi_init
);
593 MODULE_LICENSE("GPL v2");
594 MODULE_DESCRIPTION("SPMI module");
595 MODULE_ALIAS("platform:spmi");