treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / spmi / spmi.c
blobc16b60f645a4de6100a043143fb42386cbc65a7a
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
4 */
5 #include <linux/kernel.h>
6 #include <linux/errno.h>
7 #include <linux/idr.h>
8 #include <linux/slab.h>
9 #include <linux/module.h>
10 #include <linux/of.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);
26 kfree(sdev);
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);
37 kfree(ctrl);
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))
47 return 1;
49 if (drv->name)
50 return strncmp(dev_name(dev), drv->name,
51 SPMI_NAME_SIZE) == 0;
53 return 0;
56 /**
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;
63 int err;
65 dev_set_name(&sdev->dev, "%d-%02x", ctrl->nr, sdev->usid);
67 err = device_add(&sdev->dev);
68 if (err < 0) {
69 dev_err(&sdev->dev, "Can't add %s, status %d\n",
70 dev_name(&sdev->dev), err);
71 goto err_device_add;
74 dev_dbg(&sdev->dev, "device %s registered\n", dev_name(&sdev->dev));
76 err_device_add:
77 return err;
79 EXPORT_SYMBOL_GPL(spmi_device_add);
81 /**
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);
91 static inline int
92 spmi_cmd(struct spmi_controller *ctrl, u8 opcode, u8 sid)
94 int ret;
96 if (!ctrl || !ctrl->cmd || ctrl->dev.type != &spmi_ctrl_type)
97 return -EINVAL;
99 ret = ctrl->cmd(ctrl, opcode, sid);
100 trace_spmi_cmd(opcode, sid, ret);
101 return ret;
104 static inline int spmi_read_cmd(struct spmi_controller *ctrl, u8 opcode,
105 u8 sid, u16 addr, u8 *buf, size_t len)
107 int ret;
109 if (!ctrl || !ctrl->read_cmd || ctrl->dev.type != &spmi_ctrl_type)
110 return -EINVAL;
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);
115 return ret;
118 static inline int spmi_write_cmd(struct spmi_controller *ctrl, u8 opcode,
119 u8 sid, u16 addr, const u8 *buf, size_t len)
121 int ret;
123 if (!ctrl || !ctrl->write_cmd || ctrl->dev.type != &spmi_ctrl_type)
124 return -EINVAL;
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);
129 return 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 */
143 if (addr > 0x1F)
144 return -EINVAL;
146 return spmi_read_cmd(sdev->ctrl, SPMI_CMD_READ, sdev->usid, addr,
147 buf, 1);
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
159 * Slave device.
161 int spmi_ext_register_read(struct spmi_device *sdev, u8 addr, u8 *buf,
162 size_t len)
164 /* 8-bit register address, up to 16 bytes */
165 if (len == 0 || len > 16)
166 return -EINVAL;
168 return spmi_read_cmd(sdev->ctrl, SPMI_CMD_EXT_READ, sdev->usid, addr,
169 buf, len);
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,
184 size_t len)
186 /* 16-bit register address, up to 8 bytes */
187 if (len == 0 || len > 8)
188 return -EINVAL;
190 return spmi_read_cmd(sdev->ctrl, SPMI_CMD_EXT_READL, sdev->usid, addr,
191 buf, len);
193 EXPORT_SYMBOL_GPL(spmi_ext_register_readl);
196 * spmi_register_write() - register write
197 * @sdev: SPMI device
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 */
206 if (addr > 0x1F)
207 return -EINVAL;
209 return spmi_write_cmd(sdev->ctrl, SPMI_CMD_WRITE, sdev->usid, addr,
210 &data, 1);
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,
224 &data, 1);
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
236 * Slave device.
238 int spmi_ext_register_write(struct spmi_device *sdev, u8 addr, const u8 *buf,
239 size_t len)
241 /* 8-bit register address, up to 16 bytes */
242 if (len == 0 || len > 16)
243 return -EINVAL;
245 return spmi_write_cmd(sdev->ctrl, SPMI_CMD_EXT_WRITE, sdev->usid, addr,
246 buf, len);
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,
261 size_t len)
263 /* 4-bit Slave Identifier, 16-bit register address, up to 8 bytes */
264 if (len == 0 || len > 8)
265 return -EINVAL;
267 return spmi_write_cmd(sdev->ctrl, SPMI_CMD_EXT_WRITEL, sdev->usid,
268 addr, buf, len);
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
303 * the ACTIVE state.
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);
327 int err;
329 pm_runtime_get_noresume(dev);
330 pm_runtime_set_active(dev);
331 pm_runtime_enable(dev);
333 err = sdrv->probe(sdev);
334 if (err)
335 goto fail_probe;
337 return 0;
339 fail_probe:
340 pm_runtime_disable(dev);
341 pm_runtime_set_suspended(dev);
342 pm_runtime_put_noidle(dev);
343 return err;
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);
357 return 0;
360 static int spmi_drv_uevent(struct device *dev, struct kobj_uevent_env *env)
362 int ret;
364 ret = of_device_uevent_modalias(dev, env);
365 if (ret != -ENODEV)
366 return ret;
368 return 0;
371 static struct bus_type spmi_bus_type = {
372 .name = "spmi",
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);
391 if (!sdev)
392 return NULL;
394 sdev->ctrl = ctrl;
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;
399 return sdev;
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,
414 size_t size)
416 struct spmi_controller *ctrl;
417 int id;
419 if (WARN_ON(!parent))
420 return NULL;
422 ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL);
423 if (!ctrl)
424 return NULL;
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);
434 if (id < 0) {
435 dev_err(parent,
436 "unable to allocate SPMI controller identifier.\n");
437 spmi_controller_put(ctrl);
438 return NULL;
441 ctrl->nr = id;
442 dev_set_name(&ctrl->dev, "spmi-%d", id);
444 dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id);
445 return ctrl;
447 EXPORT_SYMBOL_GPL(spmi_controller_alloc);
449 static void of_spmi_register_devices(struct spmi_controller *ctrl)
451 struct device_node *node;
452 int err;
454 if (!ctrl->dev.of_node)
455 return;
457 for_each_available_child_of_node(ctrl->dev.of_node, node) {
458 struct spmi_device *sdev;
459 u32 reg[2];
461 dev_dbg(&ctrl->dev, "adding child %pOF\n", node);
463 err = of_property_read_u32_array(node, "reg", reg, 2);
464 if (err) {
465 dev_err(&ctrl->dev,
466 "node %pOF err (%d) does not have 'reg' property\n",
467 node, err);
468 continue;
471 if (reg[1] != SPMI_USID) {
472 dev_err(&ctrl->dev,
473 "node %pOF contains unsupported 'reg' entry\n",
474 node);
475 continue;
478 if (reg[0] >= SPMI_MAX_SLAVE_ID) {
479 dev_err(&ctrl->dev, "invalid usid on node %pOF\n", node);
480 continue;
483 dev_dbg(&ctrl->dev, "read usid %02x\n", reg[0]);
485 sdev = spmi_device_alloc(ctrl);
486 if (!sdev)
487 continue;
489 sdev->dev.of_node = node;
490 sdev->usid = (u8) reg[0];
492 err = spmi_device_add(sdev);
493 if (err) {
494 dev_err(&sdev->dev,
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
506 * the SPMI core.
508 int spmi_controller_add(struct spmi_controller *ctrl)
510 int ret;
512 /* Can't register until after driver model init */
513 if (WARN_ON(!is_registered))
514 return -EAGAIN;
516 ret = device_add(&ctrl->dev);
517 if (ret)
518 return ret;
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);
526 return 0;
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);
536 return 0;
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)
548 int dummy;
550 if (!ctrl)
551 return;
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)
582 int ret;
584 ret = bus_register(&spmi_bus_type);
585 if (ret)
586 return ret;
588 is_registered = true;
589 return 0;
591 postcore_initcall(spmi_init);
593 MODULE_LICENSE("GPL v2");
594 MODULE_DESCRIPTION("SPMI module");
595 MODULE_ALIAS("platform:spmi");