Loading...
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>
15
16#include <dt-bindings/spmi/spmi.h>
17#define CREATE_TRACE_POINTS
18#include <trace/events/spmi.h>
19
20static bool is_registered;
21static DEFINE_IDA(ctrl_ida);
22
23static void spmi_dev_release(struct device *dev)
24{
25 struct spmi_device *sdev = to_spmi_device(dev);
26
27 kfree(sdev);
28}
29
30static const struct device_type spmi_dev_type = {
31 .release = spmi_dev_release,
32};
33
34static void spmi_ctrl_release(struct device *dev)
35{
36 struct spmi_controller *ctrl = to_spmi_controller(dev);
37
38 ida_free(&ctrl_ida, ctrl->nr);
39 kfree(ctrl);
40}
41
42static const struct device_type spmi_ctrl_type = {
43 .release = spmi_ctrl_release,
44};
45
46static int spmi_device_match(struct device *dev, struct device_driver *drv)
47{
48 if (of_driver_match_device(dev, drv))
49 return 1;
50
51 if (drv->name)
52 return strncmp(dev_name(dev), drv->name,
53 SPMI_NAME_SIZE) == 0;
54
55 return 0;
56}
57
58/**
59 * spmi_device_add() - add a device previously constructed via spmi_device_alloc()
60 * @sdev: spmi_device to be added
61 */
62int spmi_device_add(struct spmi_device *sdev)
63{
64 struct spmi_controller *ctrl = sdev->ctrl;
65 int err;
66
67 dev_set_name(&sdev->dev, "%d-%02x", ctrl->nr, sdev->usid);
68
69 err = device_add(&sdev->dev);
70 if (err < 0) {
71 dev_err(&sdev->dev, "Can't add %s, status %d\n",
72 dev_name(&sdev->dev), err);
73 goto err_device_add;
74 }
75
76 dev_dbg(&sdev->dev, "device %s registered\n", dev_name(&sdev->dev));
77
78err_device_add:
79 return err;
80}
81EXPORT_SYMBOL_GPL(spmi_device_add);
82
83/**
84 * spmi_device_remove(): remove an SPMI device
85 * @sdev: spmi_device to be removed
86 */
87void spmi_device_remove(struct spmi_device *sdev)
88{
89 device_unregister(&sdev->dev);
90}
91EXPORT_SYMBOL_GPL(spmi_device_remove);
92
93static inline int
94spmi_cmd(struct spmi_controller *ctrl, u8 opcode, u8 sid)
95{
96 int ret;
97
98 if (!ctrl || !ctrl->cmd || ctrl->dev.type != &spmi_ctrl_type)
99 return -EINVAL;
100
101 ret = ctrl->cmd(ctrl, opcode, sid);
102 trace_spmi_cmd(opcode, sid, ret);
103 return ret;
104}
105
106static inline int spmi_read_cmd(struct spmi_controller *ctrl, u8 opcode,
107 u8 sid, u16 addr, u8 *buf, size_t len)
108{
109 int ret;
110
111 if (!ctrl || !ctrl->read_cmd || ctrl->dev.type != &spmi_ctrl_type)
112 return -EINVAL;
113
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);
117 return ret;
118}
119
120static inline int spmi_write_cmd(struct spmi_controller *ctrl, u8 opcode,
121 u8 sid, u16 addr, const u8 *buf, size_t len)
122{
123 int ret;
124
125 if (!ctrl || !ctrl->write_cmd || ctrl->dev.type != &spmi_ctrl_type)
126 return -EINVAL;
127
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);
131 return ret;
132}
133
134/**
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.
139 *
140 * Reads 1 byte of data from a Slave device register.
141 */
142int spmi_register_read(struct spmi_device *sdev, u8 addr, u8 *buf)
143{
144 /* 5-bit register address */
145 if (addr > 0x1F)
146 return -EINVAL;
147
148 return spmi_read_cmd(sdev->ctrl, SPMI_CMD_READ, sdev->usid, addr,
149 buf, 1);
150}
151EXPORT_SYMBOL_GPL(spmi_register_read);
152
153/**
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).
159 *
160 * Reads up to 16 bytes of data from the extended register space on a
161 * Slave device.
162 */
163int spmi_ext_register_read(struct spmi_device *sdev, u8 addr, u8 *buf,
164 size_t len)
165{
166 /* 8-bit register address, up to 16 bytes */
167 if (len == 0 || len > 16)
168 return -EINVAL;
169
170 return spmi_read_cmd(sdev->ctrl, SPMI_CMD_EXT_READ, sdev->usid, addr,
171 buf, len);
172}
173EXPORT_SYMBOL_GPL(spmi_ext_register_read);
174
175/**
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).
181 *
182 * Reads up to 8 bytes of data from the extended register space on a
183 * Slave device using 16-bit address.
184 */
185int spmi_ext_register_readl(struct spmi_device *sdev, u16 addr, u8 *buf,
186 size_t len)
187{
188 /* 16-bit register address, up to 8 bytes */
189 if (len == 0 || len > 8)
190 return -EINVAL;
191
192 return spmi_read_cmd(sdev->ctrl, SPMI_CMD_EXT_READL, sdev->usid, addr,
193 buf, len);
194}
195EXPORT_SYMBOL_GPL(spmi_ext_register_readl);
196
197/**
198 * spmi_register_write() - register write
199 * @sdev: SPMI device
200 * @addr: slave register address (5-bit address).
201 * @data: buffer containing the data to be transferred to the Slave.
202 *
203 * Writes 1 byte of data to a Slave device register.
204 */
205int spmi_register_write(struct spmi_device *sdev, u8 addr, u8 data)
206{
207 /* 5-bit register address */
208 if (addr > 0x1F)
209 return -EINVAL;
210
211 return spmi_write_cmd(sdev->ctrl, SPMI_CMD_WRITE, sdev->usid, addr,
212 &data, 1);
213}
214EXPORT_SYMBOL_GPL(spmi_register_write);
215
216/**
217 * spmi_register_zero_write() - register zero write
218 * @sdev: SPMI device.
219 * @data: the data to be written to register 0 (7-bits).
220 *
221 * Writes data to register 0 of the Slave device.
222 */
223int spmi_register_zero_write(struct spmi_device *sdev, u8 data)
224{
225 return spmi_write_cmd(sdev->ctrl, SPMI_CMD_ZERO_WRITE, sdev->usid, 0,
226 &data, 1);
227}
228EXPORT_SYMBOL_GPL(spmi_register_zero_write);
229
230/**
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).
236 *
237 * Writes up to 16 bytes of data to the extended register space of a
238 * Slave device.
239 */
240int spmi_ext_register_write(struct spmi_device *sdev, u8 addr, const u8 *buf,
241 size_t len)
242{
243 /* 8-bit register address, up to 16 bytes */
244 if (len == 0 || len > 16)
245 return -EINVAL;
246
247 return spmi_write_cmd(sdev->ctrl, SPMI_CMD_EXT_WRITE, sdev->usid, addr,
248 buf, len);
249}
250EXPORT_SYMBOL_GPL(spmi_ext_register_write);
251
252/**
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).
258 *
259 * Writes up to 8 bytes of data to the extended register space of a
260 * Slave device using 16-bit address.
261 */
262int spmi_ext_register_writel(struct spmi_device *sdev, u16 addr, const u8 *buf,
263 size_t len)
264{
265 /* 4-bit Slave Identifier, 16-bit register address, up to 8 bytes */
266 if (len == 0 || len > 8)
267 return -EINVAL;
268
269 return spmi_write_cmd(sdev->ctrl, SPMI_CMD_EXT_WRITEL, sdev->usid,
270 addr, buf, len);
271}
272EXPORT_SYMBOL_GPL(spmi_ext_register_writel);
273
274/**
275 * spmi_command_reset() - sends RESET command to the specified slave
276 * @sdev: SPMI device.
277 *
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.
281 */
282int spmi_command_reset(struct spmi_device *sdev)
283{
284 return spmi_cmd(sdev->ctrl, SPMI_CMD_RESET, sdev->usid);
285}
286EXPORT_SYMBOL_GPL(spmi_command_reset);
287
288/**
289 * spmi_command_sleep() - sends SLEEP command to the specified SPMI device
290 * @sdev: SPMI device.
291 *
292 * The Sleep command causes the Slave to enter the user defined SLEEP state.
293 */
294int spmi_command_sleep(struct spmi_device *sdev)
295{
296 return spmi_cmd(sdev->ctrl, SPMI_CMD_SLEEP, sdev->usid);
297}
298EXPORT_SYMBOL_GPL(spmi_command_sleep);
299
300/**
301 * spmi_command_wakeup() - sends WAKEUP command to the specified SPMI device
302 * @sdev: SPMI device.
303 *
304 * The Wakeup command causes the Slave to move from the SLEEP state to
305 * the ACTIVE state.
306 */
307int spmi_command_wakeup(struct spmi_device *sdev)
308{
309 return spmi_cmd(sdev->ctrl, SPMI_CMD_WAKEUP, sdev->usid);
310}
311EXPORT_SYMBOL_GPL(spmi_command_wakeup);
312
313/**
314 * spmi_command_shutdown() - sends SHUTDOWN command to the specified SPMI device
315 * @sdev: SPMI device.
316 *
317 * The Shutdown command causes the Slave to enter the SHUTDOWN state.
318 */
319int spmi_command_shutdown(struct spmi_device *sdev)
320{
321 return spmi_cmd(sdev->ctrl, SPMI_CMD_SHUTDOWN, sdev->usid);
322}
323EXPORT_SYMBOL_GPL(spmi_command_shutdown);
324
325static int spmi_drv_probe(struct device *dev)
326{
327 const struct spmi_driver *sdrv = to_spmi_driver(dev->driver);
328 struct spmi_device *sdev = to_spmi_device(dev);
329 int err;
330
331 pm_runtime_get_noresume(dev);
332 pm_runtime_set_active(dev);
333 pm_runtime_enable(dev);
334
335 err = sdrv->probe(sdev);
336 if (err)
337 goto fail_probe;
338
339 return 0;
340
341fail_probe:
342 pm_runtime_disable(dev);
343 pm_runtime_set_suspended(dev);
344 pm_runtime_put_noidle(dev);
345 return err;
346}
347
348static void spmi_drv_remove(struct device *dev)
349{
350 const struct spmi_driver *sdrv = to_spmi_driver(dev->driver);
351
352 pm_runtime_get_sync(dev);
353 if (sdrv->remove)
354 sdrv->remove(to_spmi_device(dev));
355 pm_runtime_put_noidle(dev);
356
357 pm_runtime_disable(dev);
358 pm_runtime_set_suspended(dev);
359 pm_runtime_put_noidle(dev);
360}
361
362static void spmi_drv_shutdown(struct device *dev)
363{
364 const struct spmi_driver *sdrv = to_spmi_driver(dev->driver);
365
366 if (sdrv && sdrv->shutdown)
367 sdrv->shutdown(to_spmi_device(dev));
368}
369
370static int spmi_drv_uevent(const struct device *dev, struct kobj_uevent_env *env)
371{
372 int ret;
373
374 ret = of_device_uevent_modalias(dev, env);
375 if (ret != -ENODEV)
376 return ret;
377
378 return 0;
379}
380
381static struct bus_type spmi_bus_type = {
382 .name = "spmi",
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,
388};
389
390/**
391 * spmi_find_device_by_of_node() - look up an SPMI device from a device node
392 *
393 * @np: device node
394 *
395 * Takes a reference to the embedded struct device which needs to be dropped
396 * after use.
397 *
398 * Returns the struct spmi_device associated with a device node or NULL.
399 */
400struct spmi_device *spmi_find_device_by_of_node(struct device_node *np)
401{
402 struct device *dev = bus_find_device_by_of_node(&spmi_bus_type, np);
403
404 if (dev)
405 return to_spmi_device(dev);
406 return NULL;
407}
408EXPORT_SYMBOL_GPL(spmi_find_device_by_of_node);
409
410/**
411 * spmi_device_alloc() - Allocate a new SPMI device
412 * @ctrl: associated controller
413 *
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.
416 */
417struct spmi_device *spmi_device_alloc(struct spmi_controller *ctrl)
418{
419 struct spmi_device *sdev;
420
421 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
422 if (!sdev)
423 return NULL;
424
425 sdev->ctrl = ctrl;
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;
430 return sdev;
431}
432EXPORT_SYMBOL_GPL(spmi_device_alloc);
433
434/**
435 * spmi_controller_alloc() - Allocate a new SPMI controller
436 * @parent: parent device
437 * @size: size of private data
438 *
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()
443 */
444struct spmi_controller *spmi_controller_alloc(struct device *parent,
445 size_t size)
446{
447 struct spmi_controller *ctrl;
448 int id;
449
450 if (WARN_ON(!parent))
451 return ERR_PTR(-EINVAL);
452
453 ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL);
454 if (!ctrl)
455 return ERR_PTR(-ENOMEM);
456
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]);
463
464 id = ida_alloc(&ctrl_ida, GFP_KERNEL);
465 if (id < 0) {
466 dev_err(parent,
467 "unable to allocate SPMI controller identifier.\n");
468 spmi_controller_put(ctrl);
469 return ERR_PTR(id);
470 }
471
472 ctrl->nr = id;
473 dev_set_name(&ctrl->dev, "spmi-%d", id);
474
475 dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id);
476 return ctrl;
477}
478EXPORT_SYMBOL_GPL(spmi_controller_alloc);
479
480static void of_spmi_register_devices(struct spmi_controller *ctrl)
481{
482 struct device_node *node;
483 int err;
484
485 if (!ctrl->dev.of_node)
486 return;
487
488 for_each_available_child_of_node(ctrl->dev.of_node, node) {
489 struct spmi_device *sdev;
490 u32 reg[2];
491
492 dev_dbg(&ctrl->dev, "adding child %pOF\n", node);
493
494 err = of_property_read_u32_array(node, "reg", reg, 2);
495 if (err) {
496 dev_err(&ctrl->dev,
497 "node %pOF err (%d) does not have 'reg' property\n",
498 node, err);
499 continue;
500 }
501
502 if (reg[1] != SPMI_USID) {
503 dev_err(&ctrl->dev,
504 "node %pOF contains unsupported 'reg' entry\n",
505 node);
506 continue;
507 }
508
509 if (reg[0] >= SPMI_MAX_SLAVE_ID) {
510 dev_err(&ctrl->dev, "invalid usid on node %pOF\n", node);
511 continue;
512 }
513
514 dev_dbg(&ctrl->dev, "read usid %02x\n", reg[0]);
515
516 sdev = spmi_device_alloc(ctrl);
517 if (!sdev)
518 continue;
519
520 sdev->dev.of_node = node;
521 sdev->usid = (u8)reg[0];
522
523 err = spmi_device_add(sdev);
524 if (err) {
525 dev_err(&sdev->dev,
526 "failure adding device. status %d\n", err);
527 spmi_device_put(sdev);
528 }
529 }
530}
531
532/**
533 * spmi_controller_add() - Add an SPMI controller
534 * @ctrl: controller to be registered.
535 *
536 * Register a controller previously allocated via spmi_controller_alloc() with
537 * the SPMI core.
538 */
539int spmi_controller_add(struct spmi_controller *ctrl)
540{
541 int ret;
542
543 /* Can't register until after driver model init */
544 if (WARN_ON(!is_registered))
545 return -EAGAIN;
546
547 ret = device_add(&ctrl->dev);
548 if (ret)
549 return ret;
550
551 if (IS_ENABLED(CONFIG_OF))
552 of_spmi_register_devices(ctrl);
553
554 dev_dbg(&ctrl->dev, "spmi-%d registered: dev:%p\n",
555 ctrl->nr, &ctrl->dev);
556
557 return 0;
558};
559EXPORT_SYMBOL_GPL(spmi_controller_add);
560
561/* Remove a device associated with a controller */
562static int spmi_ctrl_remove_device(struct device *dev, void *data)
563{
564 struct spmi_device *spmidev = to_spmi_device(dev);
565
566 if (dev->type == &spmi_dev_type)
567 spmi_device_remove(spmidev);
568 return 0;
569}
570
571/**
572 * spmi_controller_remove(): remove an SPMI controller
573 * @ctrl: controller to remove
574 *
575 * Remove a SPMI controller. Caller is responsible for calling
576 * spmi_controller_put() to discard the allocated controller.
577 */
578void spmi_controller_remove(struct spmi_controller *ctrl)
579{
580 if (!ctrl)
581 return;
582
583 device_for_each_child(&ctrl->dev, NULL, spmi_ctrl_remove_device);
584 device_del(&ctrl->dev);
585}
586EXPORT_SYMBOL_GPL(spmi_controller_remove);
587
588/**
589 * __spmi_driver_register() - Register client driver with SPMI core
590 * @sdrv: client driver to be associated with client-device.
591 * @owner: module owner
592 *
593 * This API will register the client driver with the SPMI framework.
594 * It is typically called from the driver's module-init function.
595 */
596int __spmi_driver_register(struct spmi_driver *sdrv, struct module *owner)
597{
598 sdrv->driver.bus = &spmi_bus_type;
599 sdrv->driver.owner = owner;
600 return driver_register(&sdrv->driver);
601}
602EXPORT_SYMBOL_GPL(__spmi_driver_register);
603
604static void __exit spmi_exit(void)
605{
606 bus_unregister(&spmi_bus_type);
607}
608module_exit(spmi_exit);
609
610static int __init spmi_init(void)
611{
612 int ret;
613
614 ret = bus_register(&spmi_bus_type);
615 if (ret)
616 return ret;
617
618 is_registered = true;
619 return 0;
620}
621postcore_initcall(spmi_init);
622
623MODULE_LICENSE("GPL v2");
624MODULE_DESCRIPTION("SPMI module");
625MODULE_ALIAS("platform:spmi");
1/* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
2 *
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.
6 *
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.
11 */
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>
17#include <linux/of.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>
23
24#include <dt-bindings/spmi/spmi.h>
25
26static DEFINE_IDA(ctrl_ida);
27
28static void spmi_dev_release(struct device *dev)
29{
30 struct spmi_device *sdev = to_spmi_device(dev);
31 kfree(sdev);
32}
33
34static const struct device_type spmi_dev_type = {
35 .release = spmi_dev_release,
36};
37
38static void spmi_ctrl_release(struct device *dev)
39{
40 struct spmi_controller *ctrl = to_spmi_controller(dev);
41 ida_simple_remove(&ctrl_ida, ctrl->nr);
42 kfree(ctrl);
43}
44
45static const struct device_type spmi_ctrl_type = {
46 .release = spmi_ctrl_release,
47};
48
49static int spmi_device_match(struct device *dev, struct device_driver *drv)
50{
51 if (of_driver_match_device(dev, drv))
52 return 1;
53
54 if (drv->name)
55 return strncmp(dev_name(dev), drv->name,
56 SPMI_NAME_SIZE) == 0;
57
58 return 0;
59}
60
61/**
62 * spmi_device_add() - add a device previously constructed via spmi_device_alloc()
63 * @sdev: spmi_device to be added
64 */
65int spmi_device_add(struct spmi_device *sdev)
66{
67 struct spmi_controller *ctrl = sdev->ctrl;
68 int err;
69
70 dev_set_name(&sdev->dev, "%d-%02x", ctrl->nr, sdev->usid);
71
72 err = device_add(&sdev->dev);
73 if (err < 0) {
74 dev_err(&sdev->dev, "Can't add %s, status %d\n",
75 dev_name(&sdev->dev), err);
76 goto err_device_add;
77 }
78
79 dev_dbg(&sdev->dev, "device %s registered\n", dev_name(&sdev->dev));
80
81err_device_add:
82 return err;
83}
84EXPORT_SYMBOL_GPL(spmi_device_add);
85
86/**
87 * spmi_device_remove(): remove an SPMI device
88 * @sdev: spmi_device to be removed
89 */
90void spmi_device_remove(struct spmi_device *sdev)
91{
92 device_unregister(&sdev->dev);
93}
94EXPORT_SYMBOL_GPL(spmi_device_remove);
95
96static inline int
97spmi_cmd(struct spmi_controller *ctrl, u8 opcode, u8 sid)
98{
99 if (!ctrl || !ctrl->cmd || ctrl->dev.type != &spmi_ctrl_type)
100 return -EINVAL;
101
102 return ctrl->cmd(ctrl, opcode, sid);
103}
104
105static inline int spmi_read_cmd(struct spmi_controller *ctrl, u8 opcode,
106 u8 sid, u16 addr, u8 *buf, size_t len)
107{
108 if (!ctrl || !ctrl->read_cmd || ctrl->dev.type != &spmi_ctrl_type)
109 return -EINVAL;
110
111 return ctrl->read_cmd(ctrl, opcode, sid, addr, buf, len);
112}
113
114static inline int spmi_write_cmd(struct spmi_controller *ctrl, u8 opcode,
115 u8 sid, u16 addr, const u8 *buf, size_t len)
116{
117 if (!ctrl || !ctrl->write_cmd || ctrl->dev.type != &spmi_ctrl_type)
118 return -EINVAL;
119
120 return ctrl->write_cmd(ctrl, opcode, sid, addr, buf, len);
121}
122
123/**
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.
128 *
129 * Reads 1 byte of data from a Slave device register.
130 */
131int spmi_register_read(struct spmi_device *sdev, u8 addr, u8 *buf)
132{
133 /* 5-bit register address */
134 if (addr > 0x1F)
135 return -EINVAL;
136
137 return spmi_read_cmd(sdev->ctrl, SPMI_CMD_READ, sdev->usid, addr,
138 buf, 1);
139}
140EXPORT_SYMBOL_GPL(spmi_register_read);
141
142/**
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).
148 *
149 * Reads up to 16 bytes of data from the extended register space on a
150 * Slave device.
151 */
152int spmi_ext_register_read(struct spmi_device *sdev, u8 addr, u8 *buf,
153 size_t len)
154{
155 /* 8-bit register address, up to 16 bytes */
156 if (len == 0 || len > 16)
157 return -EINVAL;
158
159 return spmi_read_cmd(sdev->ctrl, SPMI_CMD_EXT_READ, sdev->usid, addr,
160 buf, len);
161}
162EXPORT_SYMBOL_GPL(spmi_ext_register_read);
163
164/**
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).
170 *
171 * Reads up to 8 bytes of data from the extended register space on a
172 * Slave device using 16-bit address.
173 */
174int spmi_ext_register_readl(struct spmi_device *sdev, u16 addr, u8 *buf,
175 size_t len)
176{
177 /* 16-bit register address, up to 8 bytes */
178 if (len == 0 || len > 8)
179 return -EINVAL;
180
181 return spmi_read_cmd(sdev->ctrl, SPMI_CMD_EXT_READL, sdev->usid, addr,
182 buf, len);
183}
184EXPORT_SYMBOL_GPL(spmi_ext_register_readl);
185
186/**
187 * spmi_register_write() - register write
188 * @sdev: SPMI device
189 * @addr: slave register address (5-bit address).
190 * @data: buffer containing the data to be transferred to the Slave.
191 *
192 * Writes 1 byte of data to a Slave device register.
193 */
194int spmi_register_write(struct spmi_device *sdev, u8 addr, u8 data)
195{
196 /* 5-bit register address */
197 if (addr > 0x1F)
198 return -EINVAL;
199
200 return spmi_write_cmd(sdev->ctrl, SPMI_CMD_WRITE, sdev->usid, addr,
201 &data, 1);
202}
203EXPORT_SYMBOL_GPL(spmi_register_write);
204
205/**
206 * spmi_register_zero_write() - register zero write
207 * @sdev: SPMI device.
208 * @data: the data to be written to register 0 (7-bits).
209 *
210 * Writes data to register 0 of the Slave device.
211 */
212int spmi_register_zero_write(struct spmi_device *sdev, u8 data)
213{
214 return spmi_write_cmd(sdev->ctrl, SPMI_CMD_ZERO_WRITE, sdev->usid, 0,
215 &data, 1);
216}
217EXPORT_SYMBOL_GPL(spmi_register_zero_write);
218
219/**
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).
225 *
226 * Writes up to 16 bytes of data to the extended register space of a
227 * Slave device.
228 */
229int spmi_ext_register_write(struct spmi_device *sdev, u8 addr, const u8 *buf,
230 size_t len)
231{
232 /* 8-bit register address, up to 16 bytes */
233 if (len == 0 || len > 16)
234 return -EINVAL;
235
236 return spmi_write_cmd(sdev->ctrl, SPMI_CMD_EXT_WRITE, sdev->usid, addr,
237 buf, len);
238}
239EXPORT_SYMBOL_GPL(spmi_ext_register_write);
240
241/**
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).
247 *
248 * Writes up to 8 bytes of data to the extended register space of a
249 * Slave device using 16-bit address.
250 */
251int spmi_ext_register_writel(struct spmi_device *sdev, u16 addr, const u8 *buf,
252 size_t len)
253{
254 /* 4-bit Slave Identifier, 16-bit register address, up to 8 bytes */
255 if (len == 0 || len > 8)
256 return -EINVAL;
257
258 return spmi_write_cmd(sdev->ctrl, SPMI_CMD_EXT_WRITEL, sdev->usid,
259 addr, buf, len);
260}
261EXPORT_SYMBOL_GPL(spmi_ext_register_writel);
262
263/**
264 * spmi_command_reset() - sends RESET command to the specified slave
265 * @sdev: SPMI device.
266 *
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.
270 */
271int spmi_command_reset(struct spmi_device *sdev)
272{
273 return spmi_cmd(sdev->ctrl, SPMI_CMD_RESET, sdev->usid);
274}
275EXPORT_SYMBOL_GPL(spmi_command_reset);
276
277/**
278 * spmi_command_sleep() - sends SLEEP command to the specified SPMI device
279 * @sdev: SPMI device.
280 *
281 * The Sleep command causes the Slave to enter the user defined SLEEP state.
282 */
283int spmi_command_sleep(struct spmi_device *sdev)
284{
285 return spmi_cmd(sdev->ctrl, SPMI_CMD_SLEEP, sdev->usid);
286}
287EXPORT_SYMBOL_GPL(spmi_command_sleep);
288
289/**
290 * spmi_command_wakeup() - sends WAKEUP command to the specified SPMI device
291 * @sdev: SPMI device.
292 *
293 * The Wakeup command causes the Slave to move from the SLEEP state to
294 * the ACTIVE state.
295 */
296int spmi_command_wakeup(struct spmi_device *sdev)
297{
298 return spmi_cmd(sdev->ctrl, SPMI_CMD_WAKEUP, sdev->usid);
299}
300EXPORT_SYMBOL_GPL(spmi_command_wakeup);
301
302/**
303 * spmi_command_shutdown() - sends SHUTDOWN command to the specified SPMI device
304 * @sdev: SPMI device.
305 *
306 * The Shutdown command causes the Slave to enter the SHUTDOWN state.
307 */
308int spmi_command_shutdown(struct spmi_device *sdev)
309{
310 return spmi_cmd(sdev->ctrl, SPMI_CMD_SHUTDOWN, sdev->usid);
311}
312EXPORT_SYMBOL_GPL(spmi_command_shutdown);
313
314static int spmi_drv_probe(struct device *dev)
315{
316 const struct spmi_driver *sdrv = to_spmi_driver(dev->driver);
317 struct spmi_device *sdev = to_spmi_device(dev);
318 int err;
319
320 /* Ensure the slave is in ACTIVE state */
321 err = spmi_command_wakeup(sdev);
322 if (err)
323 goto fail_wakeup;
324
325 pm_runtime_get_noresume(dev);
326 pm_runtime_set_active(dev);
327 pm_runtime_enable(dev);
328
329 err = sdrv->probe(sdev);
330 if (err)
331 goto fail_probe;
332
333 return 0;
334
335fail_probe:
336 pm_runtime_disable(dev);
337 pm_runtime_set_suspended(dev);
338 pm_runtime_put_noidle(dev);
339fail_wakeup:
340 return err;
341}
342
343static int spmi_drv_remove(struct device *dev)
344{
345 const struct spmi_driver *sdrv = to_spmi_driver(dev->driver);
346
347 pm_runtime_get_sync(dev);
348 sdrv->remove(to_spmi_device(dev));
349 pm_runtime_put_noidle(dev);
350
351 pm_runtime_disable(dev);
352 pm_runtime_set_suspended(dev);
353 pm_runtime_put_noidle(dev);
354 return 0;
355}
356
357static struct bus_type spmi_bus_type = {
358 .name = "spmi",
359 .match = spmi_device_match,
360 .probe = spmi_drv_probe,
361 .remove = spmi_drv_remove,
362};
363
364/**
365 * spmi_controller_alloc() - Allocate a new SPMI device
366 * @ctrl: associated controller
367 *
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.
370 */
371struct spmi_device *spmi_device_alloc(struct spmi_controller *ctrl)
372{
373 struct spmi_device *sdev;
374
375 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
376 if (!sdev)
377 return NULL;
378
379 sdev->ctrl = ctrl;
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;
384 return sdev;
385}
386EXPORT_SYMBOL_GPL(spmi_device_alloc);
387
388/**
389 * spmi_controller_alloc() - Allocate a new SPMI controller
390 * @parent: parent device
391 * @size: size of private data
392 *
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()
397 */
398struct spmi_controller *spmi_controller_alloc(struct device *parent,
399 size_t size)
400{
401 struct spmi_controller *ctrl;
402 int id;
403
404 if (WARN_ON(!parent))
405 return NULL;
406
407 ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL);
408 if (!ctrl)
409 return NULL;
410
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]);
417
418 id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL);
419 if (id < 0) {
420 dev_err(parent,
421 "unable to allocate SPMI controller identifier.\n");
422 spmi_controller_put(ctrl);
423 return NULL;
424 }
425
426 ctrl->nr = id;
427 dev_set_name(&ctrl->dev, "spmi-%d", id);
428
429 dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id);
430 return ctrl;
431}
432EXPORT_SYMBOL_GPL(spmi_controller_alloc);
433
434static void of_spmi_register_devices(struct spmi_controller *ctrl)
435{
436 struct device_node *node;
437 int err;
438
439 if (!ctrl->dev.of_node)
440 return;
441
442 for_each_available_child_of_node(ctrl->dev.of_node, node) {
443 struct spmi_device *sdev;
444 u32 reg[2];
445
446 dev_dbg(&ctrl->dev, "adding child %s\n", node->full_name);
447
448 err = of_property_read_u32_array(node, "reg", reg, 2);
449 if (err) {
450 dev_err(&ctrl->dev,
451 "node %s err (%d) does not have 'reg' property\n",
452 node->full_name, err);
453 continue;
454 }
455
456 if (reg[1] != SPMI_USID) {
457 dev_err(&ctrl->dev,
458 "node %s contains unsupported 'reg' entry\n",
459 node->full_name);
460 continue;
461 }
462
463 if (reg[0] >= SPMI_MAX_SLAVE_ID) {
464 dev_err(&ctrl->dev,
465 "invalid usid on node %s\n",
466 node->full_name);
467 continue;
468 }
469
470 dev_dbg(&ctrl->dev, "read usid %02x\n", reg[0]);
471
472 sdev = spmi_device_alloc(ctrl);
473 if (!sdev)
474 continue;
475
476 sdev->dev.of_node = node;
477 sdev->usid = (u8) reg[0];
478
479 err = spmi_device_add(sdev);
480 if (err) {
481 dev_err(&sdev->dev,
482 "failure adding device. status %d\n", err);
483 spmi_device_put(sdev);
484 }
485 }
486}
487
488/**
489 * spmi_controller_add() - Add an SPMI controller
490 * @ctrl: controller to be registered.
491 *
492 * Register a controller previously allocated via spmi_controller_alloc() with
493 * the SPMI core.
494 */
495int spmi_controller_add(struct spmi_controller *ctrl)
496{
497 int ret;
498
499 /* Can't register until after driver model init */
500 if (WARN_ON(!spmi_bus_type.p))
501 return -EAGAIN;
502
503 ret = device_add(&ctrl->dev);
504 if (ret)
505 return ret;
506
507 if (IS_ENABLED(CONFIG_OF))
508 of_spmi_register_devices(ctrl);
509
510 dev_dbg(&ctrl->dev, "spmi-%d registered: dev:%p\n",
511 ctrl->nr, &ctrl->dev);
512
513 return 0;
514};
515EXPORT_SYMBOL_GPL(spmi_controller_add);
516
517/* Remove a device associated with a controller */
518static int spmi_ctrl_remove_device(struct device *dev, void *data)
519{
520 struct spmi_device *spmidev = to_spmi_device(dev);
521 if (dev->type == &spmi_dev_type)
522 spmi_device_remove(spmidev);
523 return 0;
524}
525
526/**
527 * spmi_controller_remove(): remove an SPMI controller
528 * @ctrl: controller to remove
529 *
530 * Remove a SPMI controller. Caller is responsible for calling
531 * spmi_controller_put() to discard the allocated controller.
532 */
533void spmi_controller_remove(struct spmi_controller *ctrl)
534{
535 int dummy;
536
537 if (!ctrl)
538 return;
539
540 dummy = device_for_each_child(&ctrl->dev, NULL,
541 spmi_ctrl_remove_device);
542 device_del(&ctrl->dev);
543}
544EXPORT_SYMBOL_GPL(spmi_controller_remove);
545
546/**
547 * spmi_driver_register() - Register client driver with SPMI core
548 * @sdrv: client driver to be associated with client-device.
549 *
550 * This API will register the client driver with the SPMI framework.
551 * It is typically called from the driver's module-init function.
552 */
553int spmi_driver_register(struct spmi_driver *sdrv)
554{
555 sdrv->driver.bus = &spmi_bus_type;
556 return driver_register(&sdrv->driver);
557}
558EXPORT_SYMBOL_GPL(spmi_driver_register);
559
560static void __exit spmi_exit(void)
561{
562 bus_unregister(&spmi_bus_type);
563}
564module_exit(spmi_exit);
565
566static int __init spmi_init(void)
567{
568 return bus_register(&spmi_bus_type);
569}
570postcore_initcall(spmi_init);
571
572MODULE_LICENSE("GPL v2");
573MODULE_DESCRIPTION("SPMI module");
574MODULE_ALIAS("platform:spmi");