1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2011-2017, The Linux Foundation
6 #include <linux/kernel.h>
7 #include <linux/errno.h>
8 #include <linux/slab.h>
9 #include <linux/init.h>
10 #include <linux/idr.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/slimbus.h>
16 static DEFINE_IDA(ctrl_ida
);
18 static const struct slim_device_id
*slim_match(const struct slim_device_id
*id
,
19 const struct slim_device
*sbdev
)
21 while (id
->manf_id
!= 0 || id
->prod_code
!= 0) {
22 if (id
->manf_id
== sbdev
->e_addr
.manf_id
&&
23 id
->prod_code
== sbdev
->e_addr
.prod_code
)
30 static int slim_device_match(struct device
*dev
, struct device_driver
*drv
)
32 struct slim_device
*sbdev
= to_slim_device(dev
);
33 struct slim_driver
*sbdrv
= to_slim_driver(drv
);
35 return !!slim_match(sbdrv
->id_table
, sbdev
);
38 static int slim_device_probe(struct device
*dev
)
40 struct slim_device
*sbdev
= to_slim_device(dev
);
41 struct slim_driver
*sbdrv
= to_slim_driver(dev
->driver
);
43 return sbdrv
->probe(sbdev
);
46 static int slim_device_remove(struct device
*dev
)
48 struct slim_device
*sbdev
= to_slim_device(dev
);
49 struct slim_driver
*sbdrv
;
52 sbdrv
= to_slim_driver(dev
->driver
);
60 struct bus_type slimbus_bus
= {
62 .match
= slim_device_match
,
63 .probe
= slim_device_probe
,
64 .remove
= slim_device_remove
,
66 EXPORT_SYMBOL_GPL(slimbus_bus
);
69 * __slim_driver_register() - Client driver registration with SLIMbus
71 * @drv:Client driver to be associated with client-device.
72 * @owner: owning module/driver
74 * This API will register the client driver with the SLIMbus
75 * It is called from the driver's module-init function.
77 int __slim_driver_register(struct slim_driver
*drv
, struct module
*owner
)
79 /* ID table and probe are mandatory */
80 if (!drv
->id_table
|| !drv
->probe
)
83 drv
->driver
.bus
= &slimbus_bus
;
84 drv
->driver
.owner
= owner
;
86 return driver_register(&drv
->driver
);
88 EXPORT_SYMBOL_GPL(__slim_driver_register
);
91 * slim_driver_unregister() - Undo effect of slim_driver_register
93 * @drv: Client driver to be unregistered
95 void slim_driver_unregister(struct slim_driver
*drv
)
97 driver_unregister(&drv
->driver
);
99 EXPORT_SYMBOL_GPL(slim_driver_unregister
);
101 static void slim_dev_release(struct device
*dev
)
103 struct slim_device
*sbdev
= to_slim_device(dev
);
108 static int slim_add_device(struct slim_controller
*ctrl
,
109 struct slim_device
*sbdev
,
110 struct device_node
*node
)
112 sbdev
->dev
.bus
= &slimbus_bus
;
113 sbdev
->dev
.parent
= ctrl
->dev
;
114 sbdev
->dev
.release
= slim_dev_release
;
115 sbdev
->dev
.driver
= NULL
;
117 INIT_LIST_HEAD(&sbdev
->stream_list
);
118 spin_lock_init(&sbdev
->stream_list_lock
);
121 sbdev
->dev
.of_node
= of_node_get(node
);
123 dev_set_name(&sbdev
->dev
, "%x:%x:%x:%x",
124 sbdev
->e_addr
.manf_id
,
125 sbdev
->e_addr
.prod_code
,
126 sbdev
->e_addr
.dev_index
,
127 sbdev
->e_addr
.instance
);
129 return device_register(&sbdev
->dev
);
132 static struct slim_device
*slim_alloc_device(struct slim_controller
*ctrl
,
133 struct slim_eaddr
*eaddr
,
134 struct device_node
*node
)
136 struct slim_device
*sbdev
;
139 sbdev
= kzalloc(sizeof(*sbdev
), GFP_KERNEL
);
143 sbdev
->e_addr
= *eaddr
;
144 ret
= slim_add_device(ctrl
, sbdev
, node
);
146 put_device(&sbdev
->dev
);
153 static void of_register_slim_devices(struct slim_controller
*ctrl
)
155 struct device
*dev
= ctrl
->dev
;
156 struct device_node
*node
;
158 if (!ctrl
->dev
->of_node
)
161 for_each_child_of_node(ctrl
->dev
->of_node
, node
) {
162 struct slim_device
*sbdev
;
163 struct slim_eaddr e_addr
;
164 const char *compat
= NULL
;
166 int manf_id
, prod_code
;
168 compat
= of_get_property(node
, "compatible", NULL
);
172 ret
= sscanf(compat
, "slim%x,%x", &manf_id
, &prod_code
);
174 dev_err(dev
, "Manf ID & Product code not found %s\n",
179 ret
= of_property_read_u32_array(node
, "reg", reg
, 2);
181 dev_err(dev
, "Device and Instance id not found:%d\n",
186 e_addr
.dev_index
= reg
[0];
187 e_addr
.instance
= reg
[1];
188 e_addr
.manf_id
= manf_id
;
189 e_addr
.prod_code
= prod_code
;
191 sbdev
= slim_alloc_device(ctrl
, &e_addr
, node
);
198 * slim_register_controller() - Controller bring-up and registration.
200 * @ctrl: Controller to be registered.
202 * A controller is registered with the framework using this API.
203 * If devices on a controller were registered before controller,
204 * this will make sure that they get probed when controller is up
206 int slim_register_controller(struct slim_controller
*ctrl
)
210 id
= ida_simple_get(&ctrl_ida
, 0, 0, GFP_KERNEL
);
217 ctrl
->min_cg
= SLIM_MIN_CLK_GEAR
;
219 ctrl
->max_cg
= SLIM_MAX_CLK_GEAR
;
221 ida_init(&ctrl
->laddr_ida
);
222 idr_init(&ctrl
->tid_idr
);
223 mutex_init(&ctrl
->lock
);
224 mutex_init(&ctrl
->sched
.m_reconf
);
225 init_completion(&ctrl
->sched
.pause_comp
);
227 dev_dbg(ctrl
->dev
, "Bus [%s] registered:dev:%p\n",
228 ctrl
->name
, ctrl
->dev
);
230 of_register_slim_devices(ctrl
);
234 EXPORT_SYMBOL_GPL(slim_register_controller
);
236 /* slim_remove_device: Remove the effect of slim_add_device() */
237 static void slim_remove_device(struct slim_device
*sbdev
)
239 device_unregister(&sbdev
->dev
);
242 static int slim_ctrl_remove_device(struct device
*dev
, void *null
)
244 slim_remove_device(to_slim_device(dev
));
249 * slim_unregister_controller() - Controller tear-down.
251 * @ctrl: Controller to tear-down.
253 int slim_unregister_controller(struct slim_controller
*ctrl
)
255 /* Remove all clients */
256 device_for_each_child(ctrl
->dev
, NULL
, slim_ctrl_remove_device
);
257 /* Enter Clock Pause */
258 slim_ctrl_clk_pause(ctrl
, false, 0);
259 ida_simple_remove(&ctrl_ida
, ctrl
->id
);
263 EXPORT_SYMBOL_GPL(slim_unregister_controller
);
265 static void slim_device_update_status(struct slim_device
*sbdev
,
266 enum slim_device_status status
)
268 struct slim_driver
*sbdrv
;
270 if (sbdev
->status
== status
)
273 sbdev
->status
= status
;
274 if (!sbdev
->dev
.driver
)
277 sbdrv
= to_slim_driver(sbdev
->dev
.driver
);
278 if (sbdrv
->device_status
)
279 sbdrv
->device_status(sbdev
, sbdev
->status
);
283 * slim_report_absent() - Controller calls this function when a device
284 * reports absent, OR when the device cannot be communicated with
286 * @sbdev: Device that cannot be reached, or sent report absent
288 void slim_report_absent(struct slim_device
*sbdev
)
290 struct slim_controller
*ctrl
= sbdev
->ctrl
;
295 /* invalidate logical addresses */
296 mutex_lock(&ctrl
->lock
);
297 sbdev
->is_laddr_valid
= false;
298 mutex_unlock(&ctrl
->lock
);
300 ida_simple_remove(&ctrl
->laddr_ida
, sbdev
->laddr
);
301 slim_device_update_status(sbdev
, SLIM_DEVICE_STATUS_DOWN
);
303 EXPORT_SYMBOL_GPL(slim_report_absent
);
305 static bool slim_eaddr_equal(struct slim_eaddr
*a
, struct slim_eaddr
*b
)
307 return (a
->manf_id
== b
->manf_id
&&
308 a
->prod_code
== b
->prod_code
&&
309 a
->dev_index
== b
->dev_index
&&
310 a
->instance
== b
->instance
);
313 static int slim_match_dev(struct device
*dev
, void *data
)
315 struct slim_eaddr
*e_addr
= data
;
316 struct slim_device
*sbdev
= to_slim_device(dev
);
318 return slim_eaddr_equal(&sbdev
->e_addr
, e_addr
);
321 static struct slim_device
*find_slim_device(struct slim_controller
*ctrl
,
322 struct slim_eaddr
*eaddr
)
324 struct slim_device
*sbdev
;
327 dev
= device_find_child(ctrl
->dev
, eaddr
, slim_match_dev
);
329 sbdev
= to_slim_device(dev
);
337 * slim_get_device() - get handle to a device.
339 * @ctrl: Controller on which this device will be added/queried
340 * @e_addr: Enumeration address of the device to be queried
342 * Return: pointer to a device if it has already reported. Creates a new
343 * device and returns pointer to it if the device has not yet enumerated.
345 struct slim_device
*slim_get_device(struct slim_controller
*ctrl
,
346 struct slim_eaddr
*e_addr
)
348 struct slim_device
*sbdev
;
350 sbdev
= find_slim_device(ctrl
, e_addr
);
352 sbdev
= slim_alloc_device(ctrl
, e_addr
, NULL
);
354 return ERR_PTR(-ENOMEM
);
359 EXPORT_SYMBOL_GPL(slim_get_device
);
361 static int of_slim_match_dev(struct device
*dev
, void *data
)
363 struct device_node
*np
= data
;
364 struct slim_device
*sbdev
= to_slim_device(dev
);
366 return (sbdev
->dev
.of_node
== np
);
369 static struct slim_device
*of_find_slim_device(struct slim_controller
*ctrl
,
370 struct device_node
*np
)
372 struct slim_device
*sbdev
;
375 dev
= device_find_child(ctrl
->dev
, np
, of_slim_match_dev
);
377 sbdev
= to_slim_device(dev
);
385 * of_slim_get_device() - get handle to a device using dt node.
387 * @ctrl: Controller on which this device will be added/queried
388 * @np: node pointer to device
390 * Return: pointer to a device if it has already reported. Creates a new
391 * device and returns pointer to it if the device has not yet enumerated.
393 struct slim_device
*of_slim_get_device(struct slim_controller
*ctrl
,
394 struct device_node
*np
)
396 return of_find_slim_device(ctrl
, np
);
398 EXPORT_SYMBOL_GPL(of_slim_get_device
);
400 static int slim_device_alloc_laddr(struct slim_device
*sbdev
,
403 struct slim_controller
*ctrl
= sbdev
->ctrl
;
407 mutex_lock(&ctrl
->lock
);
408 if (ctrl
->get_laddr
) {
409 ret
= ctrl
->get_laddr(ctrl
, &sbdev
->e_addr
, &laddr
);
412 } else if (report_present
) {
413 ret
= ida_simple_get(&ctrl
->laddr_ida
,
414 0, SLIM_LA_MANAGER
- 1, GFP_KERNEL
);
424 if (ctrl
->set_laddr
) {
425 ret
= ctrl
->set_laddr(ctrl
, &sbdev
->e_addr
, laddr
);
432 sbdev
->laddr
= laddr
;
433 sbdev
->is_laddr_valid
= true;
435 slim_device_update_status(sbdev
, SLIM_DEVICE_STATUS_UP
);
437 dev_dbg(ctrl
->dev
, "setting slimbus l-addr:%x, ea:%x,%x,%x,%x\n",
438 laddr
, sbdev
->e_addr
.manf_id
, sbdev
->e_addr
.prod_code
,
439 sbdev
->e_addr
.dev_index
, sbdev
->e_addr
.instance
);
442 mutex_unlock(&ctrl
->lock
);
448 * slim_device_report_present() - Report enumerated device.
450 * @ctrl: Controller with which device is enumerated.
451 * @e_addr: Enumeration address of the device.
452 * @laddr: Return logical address (if valid flag is false)
454 * Called by controller in response to REPORT_PRESENT. Framework will assign
455 * a logical address to this enumeration address.
456 * Function returns -EXFULL to indicate that all logical addresses are already
459 int slim_device_report_present(struct slim_controller
*ctrl
,
460 struct slim_eaddr
*e_addr
, u8
*laddr
)
462 struct slim_device
*sbdev
;
465 ret
= pm_runtime_get_sync(ctrl
->dev
);
467 if (ctrl
->sched
.clk_state
!= SLIM_CLK_ACTIVE
) {
468 dev_err(ctrl
->dev
, "slim ctrl not active,state:%d, ret:%d\n",
469 ctrl
->sched
.clk_state
, ret
);
470 goto slimbus_not_active
;
473 sbdev
= slim_get_device(ctrl
, e_addr
);
477 if (sbdev
->is_laddr_valid
) {
478 *laddr
= sbdev
->laddr
;
482 ret
= slim_device_alloc_laddr(sbdev
, true);
485 pm_runtime_mark_last_busy(ctrl
->dev
);
486 pm_runtime_put_autosuspend(ctrl
->dev
);
489 EXPORT_SYMBOL_GPL(slim_device_report_present
);
492 * slim_get_logical_addr() - get/allocate logical address of a SLIMbus device.
494 * @sbdev: client handle requesting the address.
496 * Return: zero if a logical address is valid or a new logical address
497 * has been assigned. error code in case of error.
499 int slim_get_logical_addr(struct slim_device
*sbdev
)
501 if (!sbdev
->is_laddr_valid
)
502 return slim_device_alloc_laddr(sbdev
, false);
506 EXPORT_SYMBOL_GPL(slim_get_logical_addr
);
508 static void __exit
slimbus_exit(void)
510 bus_unregister(&slimbus_bus
);
512 module_exit(slimbus_exit
);
514 static int __init
slimbus_init(void)
516 return bus_register(&slimbus_bus
);
518 postcore_initcall(slimbus_init
);
520 MODULE_LICENSE("GPL v2");
521 MODULE_DESCRIPTION("SLIMbus core");