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 static int spmi_device_match(struct device
*dev
, struct device_driver
*drv
)
51 if (of_driver_match_device(dev
, drv
))
55 return strncmp(dev_name(dev
), drv
->name
,
62 * spmi_device_add() - add a device previously constructed via spmi_device_alloc()
63 * @sdev: spmi_device to be added
65 int spmi_device_add(struct spmi_device
*sdev
)
67 struct spmi_controller
*ctrl
= sdev
->ctrl
;
70 dev_set_name(&sdev
->dev
, "%d-%02x", ctrl
->nr
, sdev
->usid
);
72 err
= device_add(&sdev
->dev
);
74 dev_err(&sdev
->dev
, "Can't add %s, status %d\n",
75 dev_name(&sdev
->dev
), err
);
79 dev_dbg(&sdev
->dev
, "device %s registered\n", dev_name(&sdev
->dev
));
84 EXPORT_SYMBOL_GPL(spmi_device_add
);
87 * spmi_device_remove(): remove an SPMI device
88 * @sdev: spmi_device to be removed
90 void spmi_device_remove(struct spmi_device
*sdev
)
92 device_unregister(&sdev
->dev
);
94 EXPORT_SYMBOL_GPL(spmi_device_remove
);
97 spmi_cmd(struct spmi_controller
*ctrl
, u8 opcode
, u8 sid
)
99 if (!ctrl
|| !ctrl
->cmd
|| ctrl
->dev
.type
!= &spmi_ctrl_type
)
102 return ctrl
->cmd(ctrl
, opcode
, sid
);
105 static inline int spmi_read_cmd(struct spmi_controller
*ctrl
, u8 opcode
,
106 u8 sid
, u16 addr
, u8
*buf
, size_t len
)
108 if (!ctrl
|| !ctrl
->read_cmd
|| ctrl
->dev
.type
!= &spmi_ctrl_type
)
111 return ctrl
->read_cmd(ctrl
, opcode
, sid
, addr
, buf
, len
);
114 static inline int spmi_write_cmd(struct spmi_controller
*ctrl
, u8 opcode
,
115 u8 sid
, u16 addr
, const u8
*buf
, size_t len
)
117 if (!ctrl
|| !ctrl
->write_cmd
|| ctrl
->dev
.type
!= &spmi_ctrl_type
)
120 return ctrl
->write_cmd(ctrl
, opcode
, sid
, addr
, buf
, len
);
124 * spmi_register_read() - register read
125 * @sdev: SPMI device.
126 * @addr: slave register address (5-bit address).
127 * @buf: buffer to be populated with data from the Slave.
129 * Reads 1 byte of data from a Slave device register.
131 int spmi_register_read(struct spmi_device
*sdev
, u8 addr
, u8
*buf
)
133 /* 5-bit register address */
137 return spmi_read_cmd(sdev
->ctrl
, SPMI_CMD_READ
, sdev
->usid
, addr
,
140 EXPORT_SYMBOL_GPL(spmi_register_read
);
143 * spmi_ext_register_read() - extended register read
144 * @sdev: SPMI device.
145 * @addr: slave register address (8-bit address).
146 * @buf: buffer to be populated with data from the Slave.
147 * @len: the request number of bytes to read (up to 16 bytes).
149 * Reads up to 16 bytes of data from the extended register space on a
152 int spmi_ext_register_read(struct spmi_device
*sdev
, u8 addr
, u8
*buf
,
155 /* 8-bit register address, up to 16 bytes */
156 if (len
== 0 || len
> 16)
159 return spmi_read_cmd(sdev
->ctrl
, SPMI_CMD_EXT_READ
, sdev
->usid
, addr
,
162 EXPORT_SYMBOL_GPL(spmi_ext_register_read
);
165 * spmi_ext_register_readl() - extended register read long
166 * @sdev: SPMI device.
167 * @addr: slave register address (16-bit address).
168 * @buf: buffer to be populated with data from the Slave.
169 * @len: the request number of bytes to read (up to 8 bytes).
171 * Reads up to 8 bytes of data from the extended register space on a
172 * Slave device using 16-bit address.
174 int spmi_ext_register_readl(struct spmi_device
*sdev
, u16 addr
, u8
*buf
,
177 /* 16-bit register address, up to 8 bytes */
178 if (len
== 0 || len
> 8)
181 return spmi_read_cmd(sdev
->ctrl
, SPMI_CMD_EXT_READL
, sdev
->usid
, addr
,
184 EXPORT_SYMBOL_GPL(spmi_ext_register_readl
);
187 * spmi_register_write() - register write
189 * @addr: slave register address (5-bit address).
190 * @data: buffer containing the data to be transferred to the Slave.
192 * Writes 1 byte of data to a Slave device register.
194 int spmi_register_write(struct spmi_device
*sdev
, u8 addr
, u8 data
)
196 /* 5-bit register address */
200 return spmi_write_cmd(sdev
->ctrl
, SPMI_CMD_WRITE
, sdev
->usid
, addr
,
203 EXPORT_SYMBOL_GPL(spmi_register_write
);
206 * spmi_register_zero_write() - register zero write
207 * @sdev: SPMI device.
208 * @data: the data to be written to register 0 (7-bits).
210 * Writes data to register 0 of the Slave device.
212 int spmi_register_zero_write(struct spmi_device
*sdev
, u8 data
)
214 return spmi_write_cmd(sdev
->ctrl
, SPMI_CMD_ZERO_WRITE
, sdev
->usid
, 0,
217 EXPORT_SYMBOL_GPL(spmi_register_zero_write
);
220 * spmi_ext_register_write() - extended register write
221 * @sdev: SPMI device.
222 * @addr: slave register address (8-bit address).
223 * @buf: buffer containing the data to be transferred to the Slave.
224 * @len: the request number of bytes to read (up to 16 bytes).
226 * Writes up to 16 bytes of data to the extended register space of a
229 int spmi_ext_register_write(struct spmi_device
*sdev
, u8 addr
, const u8
*buf
,
232 /* 8-bit register address, up to 16 bytes */
233 if (len
== 0 || len
> 16)
236 return spmi_write_cmd(sdev
->ctrl
, SPMI_CMD_EXT_WRITE
, sdev
->usid
, addr
,
239 EXPORT_SYMBOL_GPL(spmi_ext_register_write
);
242 * spmi_ext_register_writel() - extended register write long
243 * @sdev: SPMI device.
244 * @addr: slave register address (16-bit address).
245 * @buf: buffer containing the data to be transferred to the Slave.
246 * @len: the request number of bytes to read (up to 8 bytes).
248 * Writes up to 8 bytes of data to the extended register space of a
249 * Slave device using 16-bit address.
251 int spmi_ext_register_writel(struct spmi_device
*sdev
, u16 addr
, const u8
*buf
,
254 /* 4-bit Slave Identifier, 16-bit register address, up to 8 bytes */
255 if (len
== 0 || len
> 8)
258 return spmi_write_cmd(sdev
->ctrl
, SPMI_CMD_EXT_WRITEL
, sdev
->usid
,
261 EXPORT_SYMBOL_GPL(spmi_ext_register_writel
);
264 * spmi_command_reset() - sends RESET command to the specified slave
265 * @sdev: SPMI device.
267 * The Reset command initializes the Slave and forces all registers to
268 * their reset values. The Slave shall enter the STARTUP state after
269 * receiving a Reset command.
271 int spmi_command_reset(struct spmi_device
*sdev
)
273 return spmi_cmd(sdev
->ctrl
, SPMI_CMD_RESET
, sdev
->usid
);
275 EXPORT_SYMBOL_GPL(spmi_command_reset
);
278 * spmi_command_sleep() - sends SLEEP command to the specified SPMI device
279 * @sdev: SPMI device.
281 * The Sleep command causes the Slave to enter the user defined SLEEP state.
283 int spmi_command_sleep(struct spmi_device
*sdev
)
285 return spmi_cmd(sdev
->ctrl
, SPMI_CMD_SLEEP
, sdev
->usid
);
287 EXPORT_SYMBOL_GPL(spmi_command_sleep
);
290 * spmi_command_wakeup() - sends WAKEUP command to the specified SPMI device
291 * @sdev: SPMI device.
293 * The Wakeup command causes the Slave to move from the SLEEP state to
296 int spmi_command_wakeup(struct spmi_device
*sdev
)
298 return spmi_cmd(sdev
->ctrl
, SPMI_CMD_WAKEUP
, sdev
->usid
);
300 EXPORT_SYMBOL_GPL(spmi_command_wakeup
);
303 * spmi_command_shutdown() - sends SHUTDOWN command to the specified SPMI device
304 * @sdev: SPMI device.
306 * The Shutdown command causes the Slave to enter the SHUTDOWN state.
308 int spmi_command_shutdown(struct spmi_device
*sdev
)
310 return spmi_cmd(sdev
->ctrl
, SPMI_CMD_SHUTDOWN
, sdev
->usid
);
312 EXPORT_SYMBOL_GPL(spmi_command_shutdown
);
314 static int spmi_drv_probe(struct device
*dev
)
316 const struct spmi_driver
*sdrv
= to_spmi_driver(dev
->driver
);
317 struct spmi_device
*sdev
= to_spmi_device(dev
);
320 /* Ensure the slave is in ACTIVE state */
321 err
= spmi_command_wakeup(sdev
);
325 pm_runtime_get_noresume(dev
);
326 pm_runtime_set_active(dev
);
327 pm_runtime_enable(dev
);
329 err
= sdrv
->probe(sdev
);
336 pm_runtime_disable(dev
);
337 pm_runtime_set_suspended(dev
);
338 pm_runtime_put_noidle(dev
);
343 static int spmi_drv_remove(struct device
*dev
)
345 const struct spmi_driver
*sdrv
= to_spmi_driver(dev
->driver
);
347 pm_runtime_get_sync(dev
);
348 sdrv
->remove(to_spmi_device(dev
));
349 pm_runtime_put_noidle(dev
);
351 pm_runtime_disable(dev
);
352 pm_runtime_set_suspended(dev
);
353 pm_runtime_put_noidle(dev
);
357 static struct bus_type spmi_bus_type
= {
359 .match
= spmi_device_match
,
360 .probe
= spmi_drv_probe
,
361 .remove
= spmi_drv_remove
,
365 * spmi_controller_alloc() - Allocate a new SPMI device
366 * @ctrl: associated controller
368 * Caller is responsible for either calling spmi_device_add() to add the
369 * newly allocated controller, or calling spmi_device_put() to discard it.
371 struct spmi_device
*spmi_device_alloc(struct spmi_controller
*ctrl
)
373 struct spmi_device
*sdev
;
375 sdev
= kzalloc(sizeof(*sdev
), GFP_KERNEL
);
380 device_initialize(&sdev
->dev
);
381 sdev
->dev
.parent
= &ctrl
->dev
;
382 sdev
->dev
.bus
= &spmi_bus_type
;
383 sdev
->dev
.type
= &spmi_dev_type
;
386 EXPORT_SYMBOL_GPL(spmi_device_alloc
);
389 * spmi_controller_alloc() - Allocate a new SPMI controller
390 * @parent: parent device
391 * @size: size of private data
393 * Caller is responsible for either calling spmi_controller_add() to add the
394 * newly allocated controller, or calling spmi_controller_put() to discard it.
395 * The allocated private data region may be accessed via
396 * spmi_controller_get_drvdata()
398 struct spmi_controller
*spmi_controller_alloc(struct device
*parent
,
401 struct spmi_controller
*ctrl
;
404 if (WARN_ON(!parent
))
407 ctrl
= kzalloc(sizeof(*ctrl
) + size
, GFP_KERNEL
);
411 device_initialize(&ctrl
->dev
);
412 ctrl
->dev
.type
= &spmi_ctrl_type
;
413 ctrl
->dev
.bus
= &spmi_bus_type
;
414 ctrl
->dev
.parent
= parent
;
415 ctrl
->dev
.of_node
= parent
->of_node
;
416 spmi_controller_set_drvdata(ctrl
, &ctrl
[1]);
418 id
= ida_simple_get(&ctrl_ida
, 0, 0, GFP_KERNEL
);
421 "unable to allocate SPMI controller identifier.\n");
422 spmi_controller_put(ctrl
);
427 dev_set_name(&ctrl
->dev
, "spmi-%d", id
);
429 dev_dbg(&ctrl
->dev
, "allocated controller 0x%p id %d\n", ctrl
, id
);
432 EXPORT_SYMBOL_GPL(spmi_controller_alloc
);
434 static void of_spmi_register_devices(struct spmi_controller
*ctrl
)
436 struct device_node
*node
;
439 if (!ctrl
->dev
.of_node
)
442 for_each_available_child_of_node(ctrl
->dev
.of_node
, node
) {
443 struct spmi_device
*sdev
;
446 dev_dbg(&ctrl
->dev
, "adding child %s\n", node
->full_name
);
448 err
= of_property_read_u32_array(node
, "reg", reg
, 2);
451 "node %s err (%d) does not have 'reg' property\n",
452 node
->full_name
, err
);
456 if (reg
[1] != SPMI_USID
) {
458 "node %s contains unsupported 'reg' entry\n",
463 if (reg
[0] >= SPMI_MAX_SLAVE_ID
) {
465 "invalid usid on node %s\n",
470 dev_dbg(&ctrl
->dev
, "read usid %02x\n", reg
[0]);
472 sdev
= spmi_device_alloc(ctrl
);
476 sdev
->dev
.of_node
= node
;
477 sdev
->usid
= (u8
) reg
[0];
479 err
= spmi_device_add(sdev
);
482 "failure adding device. status %d\n", err
);
483 spmi_device_put(sdev
);
489 * spmi_controller_add() - Add an SPMI controller
490 * @ctrl: controller to be registered.
492 * Register a controller previously allocated via spmi_controller_alloc() with
495 int spmi_controller_add(struct spmi_controller
*ctrl
)
499 /* Can't register until after driver model init */
500 if (WARN_ON(!spmi_bus_type
.p
))
503 ret
= device_add(&ctrl
->dev
);
507 if (IS_ENABLED(CONFIG_OF
))
508 of_spmi_register_devices(ctrl
);
510 dev_dbg(&ctrl
->dev
, "spmi-%d registered: dev:%p\n",
511 ctrl
->nr
, &ctrl
->dev
);
515 EXPORT_SYMBOL_GPL(spmi_controller_add
);
517 /* Remove a device associated with a controller */
518 static int spmi_ctrl_remove_device(struct device
*dev
, void *data
)
520 struct spmi_device
*spmidev
= to_spmi_device(dev
);
521 if (dev
->type
== &spmi_dev_type
)
522 spmi_device_remove(spmidev
);
527 * spmi_controller_remove(): remove an SPMI controller
528 * @ctrl: controller to remove
530 * Remove a SPMI controller. Caller is responsible for calling
531 * spmi_controller_put() to discard the allocated controller.
533 void spmi_controller_remove(struct spmi_controller
*ctrl
)
540 dummy
= device_for_each_child(&ctrl
->dev
, NULL
,
541 spmi_ctrl_remove_device
);
542 device_del(&ctrl
->dev
);
544 EXPORT_SYMBOL_GPL(spmi_controller_remove
);
547 * spmi_driver_register() - Register client driver with SPMI core
548 * @sdrv: client driver to be associated with client-device.
550 * This API will register the client driver with the SPMI framework.
551 * It is typically called from the driver's module-init function.
553 int spmi_driver_register(struct spmi_driver
*sdrv
)
555 sdrv
->driver
.bus
= &spmi_bus_type
;
556 return driver_register(&sdrv
->driver
);
558 EXPORT_SYMBOL_GPL(spmi_driver_register
);
560 static void __exit
spmi_exit(void)
562 bus_unregister(&spmi_bus_type
);
564 module_exit(spmi_exit
);
566 static int __init
spmi_init(void)
568 return bus_register(&spmi_bus_type
);
570 postcore_initcall(spmi_init
);
572 MODULE_LICENSE("GPL v2");
573 MODULE_DESCRIPTION("SPMI module");
574 MODULE_ALIAS("platform:spmi");