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
;
119 sbdev
->dev
.of_node
= of_node_get(node
);
121 dev_set_name(&sbdev
->dev
, "%x:%x:%x:%x",
122 sbdev
->e_addr
.manf_id
,
123 sbdev
->e_addr
.prod_code
,
124 sbdev
->e_addr
.dev_index
,
125 sbdev
->e_addr
.instance
);
127 return device_register(&sbdev
->dev
);
130 static struct slim_device
*slim_alloc_device(struct slim_controller
*ctrl
,
131 struct slim_eaddr
*eaddr
,
132 struct device_node
*node
)
134 struct slim_device
*sbdev
;
137 sbdev
= kzalloc(sizeof(*sbdev
), GFP_KERNEL
);
141 sbdev
->e_addr
= *eaddr
;
142 ret
= slim_add_device(ctrl
, sbdev
, node
);
144 put_device(&sbdev
->dev
);
151 static void of_register_slim_devices(struct slim_controller
*ctrl
)
153 struct device
*dev
= ctrl
->dev
;
154 struct device_node
*node
;
156 if (!ctrl
->dev
->of_node
)
159 for_each_child_of_node(ctrl
->dev
->of_node
, node
) {
160 struct slim_device
*sbdev
;
161 struct slim_eaddr e_addr
;
162 const char *compat
= NULL
;
164 int manf_id
, prod_code
;
166 compat
= of_get_property(node
, "compatible", NULL
);
170 ret
= sscanf(compat
, "slim%x,%x", &manf_id
, &prod_code
);
172 dev_err(dev
, "Manf ID & Product code not found %s\n",
177 ret
= of_property_read_u32_array(node
, "reg", reg
, 2);
179 dev_err(dev
, "Device and Instance id not found:%d\n",
184 e_addr
.dev_index
= reg
[0];
185 e_addr
.instance
= reg
[1];
186 e_addr
.manf_id
= manf_id
;
187 e_addr
.prod_code
= prod_code
;
189 sbdev
= slim_alloc_device(ctrl
, &e_addr
, node
);
196 * slim_register_controller() - Controller bring-up and registration.
198 * @ctrl: Controller to be registered.
200 * A controller is registered with the framework using this API.
201 * If devices on a controller were registered before controller,
202 * this will make sure that they get probed when controller is up
204 int slim_register_controller(struct slim_controller
*ctrl
)
208 id
= ida_simple_get(&ctrl_ida
, 0, 0, GFP_KERNEL
);
215 ctrl
->min_cg
= SLIM_MIN_CLK_GEAR
;
217 ctrl
->max_cg
= SLIM_MAX_CLK_GEAR
;
219 ida_init(&ctrl
->laddr_ida
);
220 idr_init(&ctrl
->tid_idr
);
221 mutex_init(&ctrl
->lock
);
222 mutex_init(&ctrl
->sched
.m_reconf
);
223 init_completion(&ctrl
->sched
.pause_comp
);
225 dev_dbg(ctrl
->dev
, "Bus [%s] registered:dev:%p\n",
226 ctrl
->name
, ctrl
->dev
);
228 of_register_slim_devices(ctrl
);
232 EXPORT_SYMBOL_GPL(slim_register_controller
);
234 /* slim_remove_device: Remove the effect of slim_add_device() */
235 static void slim_remove_device(struct slim_device
*sbdev
)
237 device_unregister(&sbdev
->dev
);
240 static int slim_ctrl_remove_device(struct device
*dev
, void *null
)
242 slim_remove_device(to_slim_device(dev
));
247 * slim_unregister_controller() - Controller tear-down.
249 * @ctrl: Controller to tear-down.
251 int slim_unregister_controller(struct slim_controller
*ctrl
)
253 /* Remove all clients */
254 device_for_each_child(ctrl
->dev
, NULL
, slim_ctrl_remove_device
);
255 /* Enter Clock Pause */
256 slim_ctrl_clk_pause(ctrl
, false, 0);
257 ida_simple_remove(&ctrl_ida
, ctrl
->id
);
261 EXPORT_SYMBOL_GPL(slim_unregister_controller
);
263 static void slim_device_update_status(struct slim_device
*sbdev
,
264 enum slim_device_status status
)
266 struct slim_driver
*sbdrv
;
268 if (sbdev
->status
== status
)
271 sbdev
->status
= status
;
272 if (!sbdev
->dev
.driver
)
275 sbdrv
= to_slim_driver(sbdev
->dev
.driver
);
276 if (sbdrv
->device_status
)
277 sbdrv
->device_status(sbdev
, sbdev
->status
);
281 * slim_report_absent() - Controller calls this function when a device
282 * reports absent, OR when the device cannot be communicated with
284 * @sbdev: Device that cannot be reached, or sent report absent
286 void slim_report_absent(struct slim_device
*sbdev
)
288 struct slim_controller
*ctrl
= sbdev
->ctrl
;
293 /* invalidate logical addresses */
294 mutex_lock(&ctrl
->lock
);
295 sbdev
->is_laddr_valid
= false;
296 mutex_unlock(&ctrl
->lock
);
298 ida_simple_remove(&ctrl
->laddr_ida
, sbdev
->laddr
);
299 slim_device_update_status(sbdev
, SLIM_DEVICE_STATUS_DOWN
);
301 EXPORT_SYMBOL_GPL(slim_report_absent
);
303 static bool slim_eaddr_equal(struct slim_eaddr
*a
, struct slim_eaddr
*b
)
305 return (a
->manf_id
== b
->manf_id
&&
306 a
->prod_code
== b
->prod_code
&&
307 a
->dev_index
== b
->dev_index
&&
308 a
->instance
== b
->instance
);
311 static int slim_match_dev(struct device
*dev
, void *data
)
313 struct slim_eaddr
*e_addr
= data
;
314 struct slim_device
*sbdev
= to_slim_device(dev
);
316 return slim_eaddr_equal(&sbdev
->e_addr
, e_addr
);
319 static struct slim_device
*find_slim_device(struct slim_controller
*ctrl
,
320 struct slim_eaddr
*eaddr
)
322 struct slim_device
*sbdev
;
325 dev
= device_find_child(ctrl
->dev
, eaddr
, slim_match_dev
);
327 sbdev
= to_slim_device(dev
);
335 * slim_get_device() - get handle to a device.
337 * @ctrl: Controller on which this device will be added/queried
338 * @e_addr: Enumeration address of the device to be queried
340 * Return: pointer to a device if it has already reported. Creates a new
341 * device and returns pointer to it if the device has not yet enumerated.
343 struct slim_device
*slim_get_device(struct slim_controller
*ctrl
,
344 struct slim_eaddr
*e_addr
)
346 struct slim_device
*sbdev
;
348 sbdev
= find_slim_device(ctrl
, e_addr
);
350 sbdev
= slim_alloc_device(ctrl
, e_addr
, NULL
);
352 return ERR_PTR(-ENOMEM
);
357 EXPORT_SYMBOL_GPL(slim_get_device
);
359 static int slim_device_alloc_laddr(struct slim_device
*sbdev
,
362 struct slim_controller
*ctrl
= sbdev
->ctrl
;
366 mutex_lock(&ctrl
->lock
);
367 if (ctrl
->get_laddr
) {
368 ret
= ctrl
->get_laddr(ctrl
, &sbdev
->e_addr
, &laddr
);
371 } else if (report_present
) {
372 ret
= ida_simple_get(&ctrl
->laddr_ida
,
373 0, SLIM_LA_MANAGER
- 1, GFP_KERNEL
);
383 if (ctrl
->set_laddr
) {
384 ret
= ctrl
->set_laddr(ctrl
, &sbdev
->e_addr
, laddr
);
391 sbdev
->laddr
= laddr
;
392 sbdev
->is_laddr_valid
= true;
394 slim_device_update_status(sbdev
, SLIM_DEVICE_STATUS_UP
);
396 dev_dbg(ctrl
->dev
, "setting slimbus l-addr:%x, ea:%x,%x,%x,%x\n",
397 laddr
, sbdev
->e_addr
.manf_id
, sbdev
->e_addr
.prod_code
,
398 sbdev
->e_addr
.dev_index
, sbdev
->e_addr
.instance
);
401 mutex_unlock(&ctrl
->lock
);
407 * slim_device_report_present() - Report enumerated device.
409 * @ctrl: Controller with which device is enumerated.
410 * @e_addr: Enumeration address of the device.
411 * @laddr: Return logical address (if valid flag is false)
413 * Called by controller in response to REPORT_PRESENT. Framework will assign
414 * a logical address to this enumeration address.
415 * Function returns -EXFULL to indicate that all logical addresses are already
418 int slim_device_report_present(struct slim_controller
*ctrl
,
419 struct slim_eaddr
*e_addr
, u8
*laddr
)
421 struct slim_device
*sbdev
;
424 ret
= pm_runtime_get_sync(ctrl
->dev
);
426 if (ctrl
->sched
.clk_state
!= SLIM_CLK_ACTIVE
) {
427 dev_err(ctrl
->dev
, "slim ctrl not active,state:%d, ret:%d\n",
428 ctrl
->sched
.clk_state
, ret
);
429 goto slimbus_not_active
;
432 sbdev
= slim_get_device(ctrl
, e_addr
);
436 if (sbdev
->is_laddr_valid
) {
437 *laddr
= sbdev
->laddr
;
441 ret
= slim_device_alloc_laddr(sbdev
, true);
444 pm_runtime_mark_last_busy(ctrl
->dev
);
445 pm_runtime_put_autosuspend(ctrl
->dev
);
448 EXPORT_SYMBOL_GPL(slim_device_report_present
);
451 * slim_get_logical_addr() - get/allocate logical address of a SLIMbus device.
453 * @sbdev: client handle requesting the address.
455 * Return: zero if a logical address is valid or a new logical address
456 * has been assigned. error code in case of error.
458 int slim_get_logical_addr(struct slim_device
*sbdev
)
460 if (!sbdev
->is_laddr_valid
)
461 return slim_device_alloc_laddr(sbdev
, false);
465 EXPORT_SYMBOL_GPL(slim_get_logical_addr
);
467 static void __exit
slimbus_exit(void)
469 bus_unregister(&slimbus_bus
);
471 module_exit(slimbus_exit
);
473 static int __init
slimbus_init(void)
475 return bus_register(&slimbus_bus
);
477 postcore_initcall(slimbus_init
);
479 MODULE_LICENSE("GPL v2");
480 MODULE_DESCRIPTION("SLIMbus core");