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/of_device.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/slimbus.h>
17 static DEFINE_IDA(ctrl_ida);
19 static const struct slim_device_id *slim_match(const struct slim_device_id *id,
20 const struct slim_device *sbdev)
22 while (id->manf_id != 0 || id->prod_code != 0) {
23 if (id->manf_id == sbdev->e_addr.manf_id &&
24 id->prod_code == sbdev->e_addr.prod_code &&
25 id->dev_index == sbdev->e_addr.dev_index &&
26 id->instance == sbdev->e_addr.instance)
33 static int slim_device_match(struct device *dev, struct device_driver *drv)
35 struct slim_device *sbdev = to_slim_device(dev);
36 struct slim_driver *sbdrv = to_slim_driver(drv);
38 /* Attempt an OF style match first */
39 if (of_driver_match_device(dev, drv))
42 return !!slim_match(sbdrv->id_table, sbdev);
45 static void slim_device_update_status(struct slim_device *sbdev,
46 enum slim_device_status status)
48 struct slim_driver *sbdrv;
50 if (sbdev->status == status)
53 sbdev->status = status;
54 if (!sbdev->dev.driver)
57 sbdrv = to_slim_driver(sbdev->dev.driver);
58 if (sbdrv->device_status)
59 sbdrv->device_status(sbdev, sbdev->status);
62 static int slim_device_probe(struct device *dev)
64 struct slim_device *sbdev = to_slim_device(dev);
65 struct slim_driver *sbdrv = to_slim_driver(dev->driver);
68 ret = sbdrv->probe(sbdev);
72 /* try getting the logical address after probe */
73 ret = slim_get_logical_addr(sbdev);
75 slim_device_update_status(sbdev, SLIM_DEVICE_STATUS_UP);
77 dev_err(&sbdev->dev, "Failed to get logical address\n");
84 static int slim_device_remove(struct device *dev)
86 struct slim_device *sbdev = to_slim_device(dev);
87 struct slim_driver *sbdrv;
90 sbdrv = to_slim_driver(dev->driver);
98 static int slim_device_uevent(struct device *dev, struct kobj_uevent_env *env)
100 struct slim_device *sbdev = to_slim_device(dev);
103 ret = of_device_uevent_modalias(dev, env);
107 return add_uevent_var(env, "MODALIAS=slim:%s", dev_name(&sbdev->dev));
110 struct bus_type slimbus_bus = {
112 .match = slim_device_match,
113 .probe = slim_device_probe,
114 .remove = slim_device_remove,
115 .uevent = slim_device_uevent,
117 EXPORT_SYMBOL_GPL(slimbus_bus);
120 * __slim_driver_register() - Client driver registration with SLIMbus
122 * @drv:Client driver to be associated with client-device.
123 * @owner: owning module/driver
125 * This API will register the client driver with the SLIMbus
126 * It is called from the driver's module-init function.
128 int __slim_driver_register(struct slim_driver *drv, struct module *owner)
130 /* ID table and probe are mandatory */
131 if (!(drv->driver.of_match_table || drv->id_table) || !drv->probe)
134 drv->driver.bus = &slimbus_bus;
135 drv->driver.owner = owner;
137 return driver_register(&drv->driver);
139 EXPORT_SYMBOL_GPL(__slim_driver_register);
142 * slim_driver_unregister() - Undo effect of slim_driver_register
144 * @drv: Client driver to be unregistered
146 void slim_driver_unregister(struct slim_driver *drv)
148 driver_unregister(&drv->driver);
150 EXPORT_SYMBOL_GPL(slim_driver_unregister);
152 static void slim_dev_release(struct device *dev)
154 struct slim_device *sbdev = to_slim_device(dev);
159 static int slim_add_device(struct slim_controller *ctrl,
160 struct slim_device *sbdev,
161 struct device_node *node)
163 sbdev->dev.bus = &slimbus_bus;
164 sbdev->dev.parent = ctrl->dev;
165 sbdev->dev.release = slim_dev_release;
166 sbdev->dev.driver = NULL;
168 INIT_LIST_HEAD(&sbdev->stream_list);
169 spin_lock_init(&sbdev->stream_list_lock);
172 sbdev->dev.of_node = of_node_get(node);
174 dev_set_name(&sbdev->dev, "%x:%x:%x:%x",
175 sbdev->e_addr.manf_id,
176 sbdev->e_addr.prod_code,
177 sbdev->e_addr.dev_index,
178 sbdev->e_addr.instance);
180 return device_register(&sbdev->dev);
183 static struct slim_device *slim_alloc_device(struct slim_controller *ctrl,
184 struct slim_eaddr *eaddr,
185 struct device_node *node)
187 struct slim_device *sbdev;
190 sbdev = kzalloc(sizeof(*sbdev), GFP_KERNEL);
194 sbdev->e_addr = *eaddr;
195 ret = slim_add_device(ctrl, sbdev, node);
197 put_device(&sbdev->dev);
204 static void of_register_slim_devices(struct slim_controller *ctrl)
206 struct device *dev = ctrl->dev;
207 struct device_node *node;
209 if (!ctrl->dev->of_node)
212 for_each_child_of_node(ctrl->dev->of_node, node) {
213 struct slim_device *sbdev;
214 struct slim_eaddr e_addr;
215 const char *compat = NULL;
217 int manf_id, prod_code;
219 compat = of_get_property(node, "compatible", NULL);
223 ret = sscanf(compat, "slim%x,%x", &manf_id, &prod_code);
225 dev_err(dev, "Manf ID & Product code not found %s\n",
230 ret = of_property_read_u32_array(node, "reg", reg, 2);
232 dev_err(dev, "Device and Instance id not found:%d\n",
237 e_addr.dev_index = reg[0];
238 e_addr.instance = reg[1];
239 e_addr.manf_id = manf_id;
240 e_addr.prod_code = prod_code;
242 sbdev = slim_alloc_device(ctrl, &e_addr, node);
249 * slim_register_controller() - Controller bring-up and registration.
251 * @ctrl: Controller to be registered.
253 * A controller is registered with the framework using this API.
254 * If devices on a controller were registered before controller,
255 * this will make sure that they get probed when controller is up
257 int slim_register_controller(struct slim_controller *ctrl)
261 id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL);
268 ctrl->min_cg = SLIM_MIN_CLK_GEAR;
270 ctrl->max_cg = SLIM_MAX_CLK_GEAR;
272 ida_init(&ctrl->laddr_ida);
273 idr_init(&ctrl->tid_idr);
274 mutex_init(&ctrl->lock);
275 mutex_init(&ctrl->sched.m_reconf);
276 init_completion(&ctrl->sched.pause_comp);
277 spin_lock_init(&ctrl->txn_lock);
279 dev_dbg(ctrl->dev, "Bus [%s] registered:dev:%p\n",
280 ctrl->name, ctrl->dev);
282 of_register_slim_devices(ctrl);
286 EXPORT_SYMBOL_GPL(slim_register_controller);
288 /* slim_remove_device: Remove the effect of slim_add_device() */
289 static void slim_remove_device(struct slim_device *sbdev)
291 device_unregister(&sbdev->dev);
294 static int slim_ctrl_remove_device(struct device *dev, void *null)
296 slim_remove_device(to_slim_device(dev));
301 * slim_unregister_controller() - Controller tear-down.
303 * @ctrl: Controller to tear-down.
305 int slim_unregister_controller(struct slim_controller *ctrl)
307 /* Remove all clients */
308 device_for_each_child(ctrl->dev, NULL, slim_ctrl_remove_device);
309 /* Enter Clock Pause */
310 slim_ctrl_clk_pause(ctrl, false, 0);
311 ida_simple_remove(&ctrl_ida, ctrl->id);
315 EXPORT_SYMBOL_GPL(slim_unregister_controller);
318 * slim_report_absent() - Controller calls this function when a device
319 * reports absent, OR when the device cannot be communicated with
321 * @sbdev: Device that cannot be reached, or sent report absent
323 void slim_report_absent(struct slim_device *sbdev)
325 struct slim_controller *ctrl = sbdev->ctrl;
330 /* invalidate logical addresses */
331 mutex_lock(&ctrl->lock);
332 sbdev->is_laddr_valid = false;
333 mutex_unlock(&ctrl->lock);
335 ida_simple_remove(&ctrl->laddr_ida, sbdev->laddr);
336 slim_device_update_status(sbdev, SLIM_DEVICE_STATUS_DOWN);
338 EXPORT_SYMBOL_GPL(slim_report_absent);
340 static bool slim_eaddr_equal(struct slim_eaddr *a, struct slim_eaddr *b)
342 return (a->manf_id == b->manf_id &&
343 a->prod_code == b->prod_code &&
344 a->dev_index == b->dev_index &&
345 a->instance == b->instance);
348 static int slim_match_dev(struct device *dev, void *data)
350 struct slim_eaddr *e_addr = data;
351 struct slim_device *sbdev = to_slim_device(dev);
353 return slim_eaddr_equal(&sbdev->e_addr, e_addr);
356 static struct slim_device *find_slim_device(struct slim_controller *ctrl,
357 struct slim_eaddr *eaddr)
359 struct slim_device *sbdev;
362 dev = device_find_child(ctrl->dev, eaddr, slim_match_dev);
364 sbdev = to_slim_device(dev);
372 * slim_get_device() - get handle to a device.
374 * @ctrl: Controller on which this device will be added/queried
375 * @e_addr: Enumeration address of the device to be queried
377 * Return: pointer to a device if it has already reported. Creates a new
378 * device and returns pointer to it if the device has not yet enumerated.
380 struct slim_device *slim_get_device(struct slim_controller *ctrl,
381 struct slim_eaddr *e_addr)
383 struct slim_device *sbdev;
385 sbdev = find_slim_device(ctrl, e_addr);
387 sbdev = slim_alloc_device(ctrl, e_addr, NULL);
389 return ERR_PTR(-ENOMEM);
394 EXPORT_SYMBOL_GPL(slim_get_device);
396 static int of_slim_match_dev(struct device *dev, void *data)
398 struct device_node *np = data;
399 struct slim_device *sbdev = to_slim_device(dev);
401 return (sbdev->dev.of_node == np);
404 static struct slim_device *of_find_slim_device(struct slim_controller *ctrl,
405 struct device_node *np)
407 struct slim_device *sbdev;
410 dev = device_find_child(ctrl->dev, np, of_slim_match_dev);
412 sbdev = to_slim_device(dev);
420 * of_slim_get_device() - get handle to a device using dt node.
422 * @ctrl: Controller on which this device will be added/queried
423 * @np: node pointer to device
425 * Return: pointer to a device if it has already reported. Creates a new
426 * device and returns pointer to it if the device has not yet enumerated.
428 struct slim_device *of_slim_get_device(struct slim_controller *ctrl,
429 struct device_node *np)
431 return of_find_slim_device(ctrl, np);
433 EXPORT_SYMBOL_GPL(of_slim_get_device);
435 static int slim_device_alloc_laddr(struct slim_device *sbdev,
438 struct slim_controller *ctrl = sbdev->ctrl;
442 mutex_lock(&ctrl->lock);
443 if (ctrl->get_laddr) {
444 ret = ctrl->get_laddr(ctrl, &sbdev->e_addr, &laddr);
447 } else if (report_present) {
448 ret = ida_simple_get(&ctrl->laddr_ida,
449 0, SLIM_LA_MANAGER - 1, GFP_KERNEL);
459 if (ctrl->set_laddr) {
460 ret = ctrl->set_laddr(ctrl, &sbdev->e_addr, laddr);
467 sbdev->laddr = laddr;
468 sbdev->is_laddr_valid = true;
469 mutex_unlock(&ctrl->lock);
471 slim_device_update_status(sbdev, SLIM_DEVICE_STATUS_UP);
473 dev_dbg(ctrl->dev, "setting slimbus l-addr:%x, ea:%x,%x,%x,%x\n",
474 laddr, sbdev->e_addr.manf_id, sbdev->e_addr.prod_code,
475 sbdev->e_addr.dev_index, sbdev->e_addr.instance);
480 mutex_unlock(&ctrl->lock);
486 * slim_device_report_present() - Report enumerated device.
488 * @ctrl: Controller with which device is enumerated.
489 * @e_addr: Enumeration address of the device.
490 * @laddr: Return logical address (if valid flag is false)
492 * Called by controller in response to REPORT_PRESENT. Framework will assign
493 * a logical address to this enumeration address.
494 * Function returns -EXFULL to indicate that all logical addresses are already
497 int slim_device_report_present(struct slim_controller *ctrl,
498 struct slim_eaddr *e_addr, u8 *laddr)
500 struct slim_device *sbdev;
503 ret = pm_runtime_get_sync(ctrl->dev);
505 if (ctrl->sched.clk_state != SLIM_CLK_ACTIVE) {
506 dev_err(ctrl->dev, "slim ctrl not active,state:%d, ret:%d\n",
507 ctrl->sched.clk_state, ret);
508 goto slimbus_not_active;
511 sbdev = slim_get_device(ctrl, e_addr);
515 if (sbdev->is_laddr_valid) {
516 *laddr = sbdev->laddr;
520 ret = slim_device_alloc_laddr(sbdev, true);
523 pm_runtime_mark_last_busy(ctrl->dev);
524 pm_runtime_put_autosuspend(ctrl->dev);
527 EXPORT_SYMBOL_GPL(slim_device_report_present);
530 * slim_get_logical_addr() - get/allocate logical address of a SLIMbus device.
532 * @sbdev: client handle requesting the address.
534 * Return: zero if a logical address is valid or a new logical address
535 * has been assigned. error code in case of error.
537 int slim_get_logical_addr(struct slim_device *sbdev)
539 if (!sbdev->is_laddr_valid)
540 return slim_device_alloc_laddr(sbdev, false);
544 EXPORT_SYMBOL_GPL(slim_get_logical_addr);
546 static void __exit slimbus_exit(void)
548 bus_unregister(&slimbus_bus);
550 module_exit(slimbus_exit);
552 static int __init slimbus_init(void)
554 return bus_register(&slimbus_bus);
556 postcore_initcall(slimbus_init);
558 MODULE_LICENSE("GPL v2");
559 MODULE_DESCRIPTION("SLIMbus core");