2 * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/idr.h>
16 #include <linux/slab.h>
17 #include <linux/module.h>
19 #include <linux/of_device.h>
20 #include <linux/platform_device.h>
21 #include <linux/spmi.h>
22 #include <linux/pm_runtime.h>
24 #include <dt-bindings/spmi/spmi.h>
25 #define CREATE_TRACE_POINTS
26 #include <trace/events/spmi.h>
28 static bool is_registered
;
29 static DEFINE_IDA(ctrl_ida
);
31 static void spmi_dev_release(struct device
*dev
)
33 struct spmi_device
*sdev
= to_spmi_device(dev
);
37 static const struct device_type spmi_dev_type
= {
38 .release
= spmi_dev_release
,
41 static void spmi_ctrl_release(struct device
*dev
)
43 struct spmi_controller
*ctrl
= to_spmi_controller(dev
);
44 ida_simple_remove(&ctrl_ida
, ctrl
->nr
);
48 static const struct device_type spmi_ctrl_type
= {
49 .release
= spmi_ctrl_release
,
52 static int spmi_device_match(struct device
*dev
, struct device_driver
*drv
)
54 if (of_driver_match_device(dev
, drv
))
58 return strncmp(dev_name(dev
), drv
->name
,
65 * spmi_device_add() - add a device previously constructed via spmi_device_alloc()
66 * @sdev: spmi_device to be added
68 int spmi_device_add(struct spmi_device
*sdev
)
70 struct spmi_controller
*ctrl
= sdev
->ctrl
;
73 dev_set_name(&sdev
->dev
, "%d-%02x", ctrl
->nr
, sdev
->usid
);
75 err
= device_add(&sdev
->dev
);
77 dev_err(&sdev
->dev
, "Can't add %s, status %d\n",
78 dev_name(&sdev
->dev
), err
);
82 dev_dbg(&sdev
->dev
, "device %s registered\n", dev_name(&sdev
->dev
));
87 EXPORT_SYMBOL_GPL(spmi_device_add
);
90 * spmi_device_remove(): remove an SPMI device
91 * @sdev: spmi_device to be removed
93 void spmi_device_remove(struct spmi_device
*sdev
)
95 device_unregister(&sdev
->dev
);
97 EXPORT_SYMBOL_GPL(spmi_device_remove
);
100 spmi_cmd(struct spmi_controller
*ctrl
, u8 opcode
, u8 sid
)
104 if (!ctrl
|| !ctrl
->cmd
|| ctrl
->dev
.type
!= &spmi_ctrl_type
)
107 ret
= ctrl
->cmd(ctrl
, opcode
, sid
);
108 trace_spmi_cmd(opcode
, sid
, ret
);
112 static inline int spmi_read_cmd(struct spmi_controller
*ctrl
, u8 opcode
,
113 u8 sid
, u16 addr
, u8
*buf
, size_t len
)
117 if (!ctrl
|| !ctrl
->read_cmd
|| ctrl
->dev
.type
!= &spmi_ctrl_type
)
120 trace_spmi_read_begin(opcode
, sid
, addr
);
121 ret
= ctrl
->read_cmd(ctrl
, opcode
, sid
, addr
, buf
, len
);
122 trace_spmi_read_end(opcode
, sid
, addr
, ret
, len
, buf
);
126 static inline int spmi_write_cmd(struct spmi_controller
*ctrl
, u8 opcode
,
127 u8 sid
, u16 addr
, const u8
*buf
, size_t len
)
131 if (!ctrl
|| !ctrl
->write_cmd
|| ctrl
->dev
.type
!= &spmi_ctrl_type
)
134 trace_spmi_write_begin(opcode
, sid
, addr
, len
, buf
);
135 ret
= ctrl
->write_cmd(ctrl
, opcode
, sid
, addr
, buf
, len
);
136 trace_spmi_write_end(opcode
, sid
, addr
, ret
);
141 * spmi_register_read() - register read
142 * @sdev: SPMI device.
143 * @addr: slave register address (5-bit address).
144 * @buf: buffer to be populated with data from the Slave.
146 * Reads 1 byte of data from a Slave device register.
148 int spmi_register_read(struct spmi_device
*sdev
, u8 addr
, u8
*buf
)
150 /* 5-bit register address */
154 return spmi_read_cmd(sdev
->ctrl
, SPMI_CMD_READ
, sdev
->usid
, addr
,
157 EXPORT_SYMBOL_GPL(spmi_register_read
);
160 * spmi_ext_register_read() - extended register read
161 * @sdev: SPMI device.
162 * @addr: slave register address (8-bit address).
163 * @buf: buffer to be populated with data from the Slave.
164 * @len: the request number of bytes to read (up to 16 bytes).
166 * Reads up to 16 bytes of data from the extended register space on a
169 int spmi_ext_register_read(struct spmi_device
*sdev
, u8 addr
, u8
*buf
,
172 /* 8-bit register address, up to 16 bytes */
173 if (len
== 0 || len
> 16)
176 return spmi_read_cmd(sdev
->ctrl
, SPMI_CMD_EXT_READ
, sdev
->usid
, addr
,
179 EXPORT_SYMBOL_GPL(spmi_ext_register_read
);
182 * spmi_ext_register_readl() - extended register read long
183 * @sdev: SPMI device.
184 * @addr: slave register address (16-bit address).
185 * @buf: buffer to be populated with data from the Slave.
186 * @len: the request number of bytes to read (up to 8 bytes).
188 * Reads up to 8 bytes of data from the extended register space on a
189 * Slave device using 16-bit address.
191 int spmi_ext_register_readl(struct spmi_device
*sdev
, u16 addr
, u8
*buf
,
194 /* 16-bit register address, up to 8 bytes */
195 if (len
== 0 || len
> 8)
198 return spmi_read_cmd(sdev
->ctrl
, SPMI_CMD_EXT_READL
, sdev
->usid
, addr
,
201 EXPORT_SYMBOL_GPL(spmi_ext_register_readl
);
204 * spmi_register_write() - register write
206 * @addr: slave register address (5-bit address).
207 * @data: buffer containing the data to be transferred to the Slave.
209 * Writes 1 byte of data to a Slave device register.
211 int spmi_register_write(struct spmi_device
*sdev
, u8 addr
, u8 data
)
213 /* 5-bit register address */
217 return spmi_write_cmd(sdev
->ctrl
, SPMI_CMD_WRITE
, sdev
->usid
, addr
,
220 EXPORT_SYMBOL_GPL(spmi_register_write
);
223 * spmi_register_zero_write() - register zero write
224 * @sdev: SPMI device.
225 * @data: the data to be written to register 0 (7-bits).
227 * Writes data to register 0 of the Slave device.
229 int spmi_register_zero_write(struct spmi_device
*sdev
, u8 data
)
231 return spmi_write_cmd(sdev
->ctrl
, SPMI_CMD_ZERO_WRITE
, sdev
->usid
, 0,
234 EXPORT_SYMBOL_GPL(spmi_register_zero_write
);
237 * spmi_ext_register_write() - extended register write
238 * @sdev: SPMI device.
239 * @addr: slave register address (8-bit address).
240 * @buf: buffer containing the data to be transferred to the Slave.
241 * @len: the request number of bytes to read (up to 16 bytes).
243 * Writes up to 16 bytes of data to the extended register space of a
246 int spmi_ext_register_write(struct spmi_device
*sdev
, u8 addr
, const u8
*buf
,
249 /* 8-bit register address, up to 16 bytes */
250 if (len
== 0 || len
> 16)
253 return spmi_write_cmd(sdev
->ctrl
, SPMI_CMD_EXT_WRITE
, sdev
->usid
, addr
,
256 EXPORT_SYMBOL_GPL(spmi_ext_register_write
);
259 * spmi_ext_register_writel() - extended register write long
260 * @sdev: SPMI device.
261 * @addr: slave register address (16-bit address).
262 * @buf: buffer containing the data to be transferred to the Slave.
263 * @len: the request number of bytes to read (up to 8 bytes).
265 * Writes up to 8 bytes of data to the extended register space of a
266 * Slave device using 16-bit address.
268 int spmi_ext_register_writel(struct spmi_device
*sdev
, u16 addr
, const u8
*buf
,
271 /* 4-bit Slave Identifier, 16-bit register address, up to 8 bytes */
272 if (len
== 0 || len
> 8)
275 return spmi_write_cmd(sdev
->ctrl
, SPMI_CMD_EXT_WRITEL
, sdev
->usid
,
278 EXPORT_SYMBOL_GPL(spmi_ext_register_writel
);
281 * spmi_command_reset() - sends RESET command to the specified slave
282 * @sdev: SPMI device.
284 * The Reset command initializes the Slave and forces all registers to
285 * their reset values. The Slave shall enter the STARTUP state after
286 * receiving a Reset command.
288 int spmi_command_reset(struct spmi_device
*sdev
)
290 return spmi_cmd(sdev
->ctrl
, SPMI_CMD_RESET
, sdev
->usid
);
292 EXPORT_SYMBOL_GPL(spmi_command_reset
);
295 * spmi_command_sleep() - sends SLEEP command to the specified SPMI device
296 * @sdev: SPMI device.
298 * The Sleep command causes the Slave to enter the user defined SLEEP state.
300 int spmi_command_sleep(struct spmi_device
*sdev
)
302 return spmi_cmd(sdev
->ctrl
, SPMI_CMD_SLEEP
, sdev
->usid
);
304 EXPORT_SYMBOL_GPL(spmi_command_sleep
);
307 * spmi_command_wakeup() - sends WAKEUP command to the specified SPMI device
308 * @sdev: SPMI device.
310 * The Wakeup command causes the Slave to move from the SLEEP state to
313 int spmi_command_wakeup(struct spmi_device
*sdev
)
315 return spmi_cmd(sdev
->ctrl
, SPMI_CMD_WAKEUP
, sdev
->usid
);
317 EXPORT_SYMBOL_GPL(spmi_command_wakeup
);
320 * spmi_command_shutdown() - sends SHUTDOWN command to the specified SPMI device
321 * @sdev: SPMI device.
323 * The Shutdown command causes the Slave to enter the SHUTDOWN state.
325 int spmi_command_shutdown(struct spmi_device
*sdev
)
327 return spmi_cmd(sdev
->ctrl
, SPMI_CMD_SHUTDOWN
, sdev
->usid
);
329 EXPORT_SYMBOL_GPL(spmi_command_shutdown
);
331 static int spmi_drv_probe(struct device
*dev
)
333 const struct spmi_driver
*sdrv
= to_spmi_driver(dev
->driver
);
334 struct spmi_device
*sdev
= to_spmi_device(dev
);
337 pm_runtime_get_noresume(dev
);
338 pm_runtime_set_active(dev
);
339 pm_runtime_enable(dev
);
341 err
= sdrv
->probe(sdev
);
348 pm_runtime_disable(dev
);
349 pm_runtime_set_suspended(dev
);
350 pm_runtime_put_noidle(dev
);
354 static int spmi_drv_remove(struct device
*dev
)
356 const struct spmi_driver
*sdrv
= to_spmi_driver(dev
->driver
);
358 pm_runtime_get_sync(dev
);
359 sdrv
->remove(to_spmi_device(dev
));
360 pm_runtime_put_noidle(dev
);
362 pm_runtime_disable(dev
);
363 pm_runtime_set_suspended(dev
);
364 pm_runtime_put_noidle(dev
);
368 static struct bus_type spmi_bus_type
= {
370 .match
= spmi_device_match
,
371 .probe
= spmi_drv_probe
,
372 .remove
= spmi_drv_remove
,
376 * spmi_controller_alloc() - Allocate a new SPMI device
377 * @ctrl: associated controller
379 * Caller is responsible for either calling spmi_device_add() to add the
380 * newly allocated controller, or calling spmi_device_put() to discard it.
382 struct spmi_device
*spmi_device_alloc(struct spmi_controller
*ctrl
)
384 struct spmi_device
*sdev
;
386 sdev
= kzalloc(sizeof(*sdev
), GFP_KERNEL
);
391 device_initialize(&sdev
->dev
);
392 sdev
->dev
.parent
= &ctrl
->dev
;
393 sdev
->dev
.bus
= &spmi_bus_type
;
394 sdev
->dev
.type
= &spmi_dev_type
;
397 EXPORT_SYMBOL_GPL(spmi_device_alloc
);
400 * spmi_controller_alloc() - Allocate a new SPMI controller
401 * @parent: parent device
402 * @size: size of private data
404 * Caller is responsible for either calling spmi_controller_add() to add the
405 * newly allocated controller, or calling spmi_controller_put() to discard it.
406 * The allocated private data region may be accessed via
407 * spmi_controller_get_drvdata()
409 struct spmi_controller
*spmi_controller_alloc(struct device
*parent
,
412 struct spmi_controller
*ctrl
;
415 if (WARN_ON(!parent
))
418 ctrl
= kzalloc(sizeof(*ctrl
) + size
, GFP_KERNEL
);
422 device_initialize(&ctrl
->dev
);
423 ctrl
->dev
.type
= &spmi_ctrl_type
;
424 ctrl
->dev
.bus
= &spmi_bus_type
;
425 ctrl
->dev
.parent
= parent
;
426 ctrl
->dev
.of_node
= parent
->of_node
;
427 spmi_controller_set_drvdata(ctrl
, &ctrl
[1]);
429 id
= ida_simple_get(&ctrl_ida
, 0, 0, GFP_KERNEL
);
432 "unable to allocate SPMI controller identifier.\n");
433 spmi_controller_put(ctrl
);
438 dev_set_name(&ctrl
->dev
, "spmi-%d", id
);
440 dev_dbg(&ctrl
->dev
, "allocated controller 0x%p id %d\n", ctrl
, id
);
443 EXPORT_SYMBOL_GPL(spmi_controller_alloc
);
445 static void of_spmi_register_devices(struct spmi_controller
*ctrl
)
447 struct device_node
*node
;
450 if (!ctrl
->dev
.of_node
)
453 for_each_available_child_of_node(ctrl
->dev
.of_node
, node
) {
454 struct spmi_device
*sdev
;
457 dev_dbg(&ctrl
->dev
, "adding child %s\n", node
->full_name
);
459 err
= of_property_read_u32_array(node
, "reg", reg
, 2);
462 "node %s err (%d) does not have 'reg' property\n",
463 node
->full_name
, err
);
467 if (reg
[1] != SPMI_USID
) {
469 "node %s contains unsupported 'reg' entry\n",
474 if (reg
[0] >= SPMI_MAX_SLAVE_ID
) {
476 "invalid usid on node %s\n",
481 dev_dbg(&ctrl
->dev
, "read usid %02x\n", reg
[0]);
483 sdev
= spmi_device_alloc(ctrl
);
487 sdev
->dev
.of_node
= node
;
488 sdev
->usid
= (u8
) reg
[0];
490 err
= spmi_device_add(sdev
);
493 "failure adding device. status %d\n", err
);
494 spmi_device_put(sdev
);
500 * spmi_controller_add() - Add an SPMI controller
501 * @ctrl: controller to be registered.
503 * Register a controller previously allocated via spmi_controller_alloc() with
506 int spmi_controller_add(struct spmi_controller
*ctrl
)
510 /* Can't register until after driver model init */
511 if (WARN_ON(!is_registered
))
514 ret
= device_add(&ctrl
->dev
);
518 if (IS_ENABLED(CONFIG_OF
))
519 of_spmi_register_devices(ctrl
);
521 dev_dbg(&ctrl
->dev
, "spmi-%d registered: dev:%p\n",
522 ctrl
->nr
, &ctrl
->dev
);
526 EXPORT_SYMBOL_GPL(spmi_controller_add
);
528 /* Remove a device associated with a controller */
529 static int spmi_ctrl_remove_device(struct device
*dev
, void *data
)
531 struct spmi_device
*spmidev
= to_spmi_device(dev
);
532 if (dev
->type
== &spmi_dev_type
)
533 spmi_device_remove(spmidev
);
538 * spmi_controller_remove(): remove an SPMI controller
539 * @ctrl: controller to remove
541 * Remove a SPMI controller. Caller is responsible for calling
542 * spmi_controller_put() to discard the allocated controller.
544 void spmi_controller_remove(struct spmi_controller
*ctrl
)
551 dummy
= device_for_each_child(&ctrl
->dev
, NULL
,
552 spmi_ctrl_remove_device
);
553 device_del(&ctrl
->dev
);
555 EXPORT_SYMBOL_GPL(spmi_controller_remove
);
558 * spmi_driver_register() - Register client driver with SPMI core
559 * @sdrv: client driver to be associated with client-device.
561 * This API will register the client driver with the SPMI framework.
562 * It is typically called from the driver's module-init function.
564 int __spmi_driver_register(struct spmi_driver
*sdrv
, struct module
*owner
)
566 sdrv
->driver
.bus
= &spmi_bus_type
;
567 sdrv
->driver
.owner
= owner
;
568 return driver_register(&sdrv
->driver
);
570 EXPORT_SYMBOL_GPL(__spmi_driver_register
);
572 static void __exit
spmi_exit(void)
574 bus_unregister(&spmi_bus_type
);
576 module_exit(spmi_exit
);
578 static int __init
spmi_init(void)
582 ret
= bus_register(&spmi_bus_type
);
586 is_registered
= true;
589 postcore_initcall(spmi_init
);
591 MODULE_LICENSE("GPL v2");
592 MODULE_DESCRIPTION("SPMI module");
593 MODULE_ALIAS("platform:spmi");