Merge tag 'mm-hotfixes-stable-2025-07-11-16-16' of git://git.kernel.org/pub/scm/linux...
[linux-2.6-block.git] / drivers / slimbus / core.c
CommitLineData
3648e78e
SD
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2011-2017, The Linux Foundation
4 */
5
6#include <linux/kernel.h>
7#include <linux/errno.h>
8#include <linux/slab.h>
9#include <linux/init.h>
46a2bb5a 10#include <linux/idr.h>
7588a511 11#include <linux/of.h>
9e663f48 12#include <linux/of_device.h>
4b14e62a 13#include <linux/pm_runtime.h>
3648e78e 14#include <linux/slimbus.h>
46a2bb5a
SD
15#include "slimbus.h"
16
17static DEFINE_IDA(ctrl_ida);
3648e78e
SD
18
19static const struct slim_device_id *slim_match(const struct slim_device_id *id,
20 const struct slim_device *sbdev)
21{
22 while (id->manf_id != 0 || id->prod_code != 0) {
23 if (id->manf_id == sbdev->e_addr.manf_id &&
8631f940
SK
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)
3648e78e
SD
27 return id;
28 id++;
29 }
30 return NULL;
31}
32
d69d8048 33static int slim_device_match(struct device *dev, const struct device_driver *drv)
3648e78e
SD
34{
35 struct slim_device *sbdev = to_slim_device(dev);
d69d8048 36 const struct slim_driver *sbdrv = to_slim_driver(drv);
3648e78e 37
14a649d3
SK
38 /* Attempt an OF style match first */
39 if (of_driver_match_device(dev, drv))
40 return 1;
41
3648e78e
SD
42 return !!slim_match(sbdrv->id_table, sbdev);
43}
44
380c6246
SK
45static void slim_device_update_status(struct slim_device *sbdev,
46 enum slim_device_status status)
47{
48 struct slim_driver *sbdrv;
49
50 if (sbdev->status == status)
51 return;
52
53 sbdev->status = status;
54 if (!sbdev->dev.driver)
55 return;
56
57 sbdrv = to_slim_driver(sbdev->dev.driver);
58 if (sbdrv->device_status)
59 sbdrv->device_status(sbdev, sbdev->status);
60}
61
3648e78e
SD
62static int slim_device_probe(struct device *dev)
63{
64 struct slim_device *sbdev = to_slim_device(dev);
65 struct slim_driver *sbdrv = to_slim_driver(dev->driver);
8f3d5fcd 66 int ret;
3648e78e 67
8f3d5fcd
SK
68 ret = sbdrv->probe(sbdev);
69 if (ret)
70 return ret;
71
72 /* try getting the logical address after probe */
73 ret = slim_get_logical_addr(sbdev);
74 if (!ret) {
380c6246 75 slim_device_update_status(sbdev, SLIM_DEVICE_STATUS_UP);
8f3d5fcd
SK
76 } else {
77 dev_err(&sbdev->dev, "Failed to get logical address\n");
78 ret = -EPROBE_DEFER;
79 }
80
81 return ret;
3648e78e
SD
82}
83
fc7a6209 84static void slim_device_remove(struct device *dev)
3648e78e
SD
85{
86 struct slim_device *sbdev = to_slim_device(dev);
87 struct slim_driver *sbdrv;
88
89 if (dev->driver) {
90 sbdrv = to_slim_driver(dev->driver);
91 if (sbdrv->remove)
92 sbdrv->remove(sbdev);
93 }
3648e78e
SD
94}
95
2a81ada3 96static int slim_device_uevent(const struct device *dev, struct kobj_uevent_env *env)
9e663f48 97{
2a81ada3 98 const struct slim_device *sbdev = to_slim_device(dev);
9e663f48
SK
99
100 return add_uevent_var(env, "MODALIAS=slim:%s", dev_name(&sbdev->dev));
101}
102
ab23f1bf 103const struct bus_type slimbus_bus = {
3648e78e
SD
104 .name = "slimbus",
105 .match = slim_device_match,
106 .probe = slim_device_probe,
107 .remove = slim_device_remove,
9e663f48 108 .uevent = slim_device_uevent,
3648e78e
SD
109};
110EXPORT_SYMBOL_GPL(slimbus_bus);
111
112/*
113 * __slim_driver_register() - Client driver registration with SLIMbus
114 *
115 * @drv:Client driver to be associated with client-device.
116 * @owner: owning module/driver
117 *
118 * This API will register the client driver with the SLIMbus
119 * It is called from the driver's module-init function.
120 */
121int __slim_driver_register(struct slim_driver *drv, struct module *owner)
122{
123 /* ID table and probe are mandatory */
14a649d3 124 if (!(drv->driver.of_match_table || drv->id_table) || !drv->probe)
3648e78e
SD
125 return -EINVAL;
126
127 drv->driver.bus = &slimbus_bus;
128 drv->driver.owner = owner;
129
130 return driver_register(&drv->driver);
131}
132EXPORT_SYMBOL_GPL(__slim_driver_register);
133
134/*
135 * slim_driver_unregister() - Undo effect of slim_driver_register
136 *
137 * @drv: Client driver to be unregistered
138 */
139void slim_driver_unregister(struct slim_driver *drv)
140{
141 driver_unregister(&drv->driver);
142}
143EXPORT_SYMBOL_GPL(slim_driver_unregister);
144
46a2bb5a
SD
145static void slim_dev_release(struct device *dev)
146{
147 struct slim_device *sbdev = to_slim_device(dev);
148
149 kfree(sbdev);
150}
151
152static int slim_add_device(struct slim_controller *ctrl,
153 struct slim_device *sbdev,
154 struct device_node *node)
155{
156 sbdev->dev.bus = &slimbus_bus;
157 sbdev->dev.parent = ctrl->dev;
158 sbdev->dev.release = slim_dev_release;
159 sbdev->dev.driver = NULL;
160 sbdev->ctrl = ctrl;
abb9c9b8
SK
161 INIT_LIST_HEAD(&sbdev->stream_list);
162 spin_lock_init(&sbdev->stream_list_lock);
dbf4d133
SK
163 sbdev->dev.of_node = of_node_get(node);
164 sbdev->dev.fwnode = of_fwnode_handle(node);
7588a511 165
46a2bb5a
SD
166 dev_set_name(&sbdev->dev, "%x:%x:%x:%x",
167 sbdev->e_addr.manf_id,
168 sbdev->e_addr.prod_code,
169 sbdev->e_addr.dev_index,
170 sbdev->e_addr.instance);
171
172 return device_register(&sbdev->dev);
173}
174
175static struct slim_device *slim_alloc_device(struct slim_controller *ctrl,
176 struct slim_eaddr *eaddr,
177 struct device_node *node)
178{
179 struct slim_device *sbdev;
180 int ret;
181
182 sbdev = kzalloc(sizeof(*sbdev), GFP_KERNEL);
183 if (!sbdev)
184 return NULL;
185
186 sbdev->e_addr = *eaddr;
187 ret = slim_add_device(ctrl, sbdev, node);
188 if (ret) {
bd329f02 189 put_device(&sbdev->dev);
46a2bb5a
SD
190 return NULL;
191 }
192
193 return sbdev;
194}
195
7588a511
SK
196static void of_register_slim_devices(struct slim_controller *ctrl)
197{
198 struct device *dev = ctrl->dev;
199 struct device_node *node;
200
201 if (!ctrl->dev->of_node)
202 return;
203
204 for_each_child_of_node(ctrl->dev->of_node, node) {
205 struct slim_device *sbdev;
206 struct slim_eaddr e_addr;
207 const char *compat = NULL;
208 int reg[2], ret;
209 int manf_id, prod_code;
210
211 compat = of_get_property(node, "compatible", NULL);
212 if (!compat)
213 continue;
214
215 ret = sscanf(compat, "slim%x,%x", &manf_id, &prod_code);
216 if (ret != 2) {
217 dev_err(dev, "Manf ID & Product code not found %s\n",
218 compat);
219 continue;
220 }
221
222 ret = of_property_read_u32_array(node, "reg", reg, 2);
223 if (ret) {
224 dev_err(dev, "Device and Instance id not found:%d\n",
225 ret);
226 continue;
227 }
228
229 e_addr.dev_index = reg[0];
230 e_addr.instance = reg[1];
231 e_addr.manf_id = manf_id;
232 e_addr.prod_code = prod_code;
233
234 sbdev = slim_alloc_device(ctrl, &e_addr, node);
235 if (!sbdev)
236 continue;
237 }
238}
239
46a2bb5a
SD
240/*
241 * slim_register_controller() - Controller bring-up and registration.
242 *
243 * @ctrl: Controller to be registered.
244 *
245 * A controller is registered with the framework using this API.
246 * If devices on a controller were registered before controller,
247 * this will make sure that they get probed when controller is up
248 */
249int slim_register_controller(struct slim_controller *ctrl)
250{
251 int id;
252
ad804659 253 id = ida_alloc(&ctrl_ida, GFP_KERNEL);
46a2bb5a
SD
254 if (id < 0)
255 return id;
256
257 ctrl->id = id;
258
259 if (!ctrl->min_cg)
260 ctrl->min_cg = SLIM_MIN_CLK_GEAR;
261 if (!ctrl->max_cg)
262 ctrl->max_cg = SLIM_MAX_CLK_GEAR;
263
264 ida_init(&ctrl->laddr_ida);
265 idr_init(&ctrl->tid_idr);
266 mutex_init(&ctrl->lock);
4b14e62a
SD
267 mutex_init(&ctrl->sched.m_reconf);
268 init_completion(&ctrl->sched.pause_comp);
8147760d 269 spin_lock_init(&ctrl->txn_lock);
46a2bb5a
SD
270
271 dev_dbg(ctrl->dev, "Bus [%s] registered:dev:%p\n",
272 ctrl->name, ctrl->dev);
273
7588a511
SK
274 of_register_slim_devices(ctrl);
275
46a2bb5a
SD
276 return 0;
277}
278EXPORT_SYMBOL_GPL(slim_register_controller);
279
280/* slim_remove_device: Remove the effect of slim_add_device() */
281static void slim_remove_device(struct slim_device *sbdev)
282{
01360857 283 of_node_put(sbdev->dev.of_node);
46a2bb5a
SD
284 device_unregister(&sbdev->dev);
285}
286
287static int slim_ctrl_remove_device(struct device *dev, void *null)
288{
289 slim_remove_device(to_slim_device(dev));
290 return 0;
291}
292
293/**
294 * slim_unregister_controller() - Controller tear-down.
295 *
296 * @ctrl: Controller to tear-down.
297 */
298int slim_unregister_controller(struct slim_controller *ctrl)
299{
300 /* Remove all clients */
301 device_for_each_child(ctrl->dev, NULL, slim_ctrl_remove_device);
ad804659 302 ida_free(&ctrl_ida, ctrl->id);
46a2bb5a
SD
303
304 return 0;
305}
306EXPORT_SYMBOL_GPL(slim_unregister_controller);
307
46a2bb5a
SD
308/**
309 * slim_report_absent() - Controller calls this function when a device
310 * reports absent, OR when the device cannot be communicated with
311 *
312 * @sbdev: Device that cannot be reached, or sent report absent
313 */
314void slim_report_absent(struct slim_device *sbdev)
315{
316 struct slim_controller *ctrl = sbdev->ctrl;
317
318 if (!ctrl)
319 return;
320
321 /* invalidate logical addresses */
322 mutex_lock(&ctrl->lock);
323 sbdev->is_laddr_valid = false;
324 mutex_unlock(&ctrl->lock);
f97769fd 325 if (!ctrl->get_laddr)
ad804659 326 ida_free(&ctrl->laddr_ida, sbdev->laddr);
46a2bb5a
SD
327 slim_device_update_status(sbdev, SLIM_DEVICE_STATUS_DOWN);
328}
329EXPORT_SYMBOL_GPL(slim_report_absent);
330
064aa528
ZH
331static bool slim_eaddr_equal(const struct slim_eaddr *a,
332 const struct slim_eaddr *b)
46a2bb5a
SD
333{
334 return (a->manf_id == b->manf_id &&
335 a->prod_code == b->prod_code &&
336 a->dev_index == b->dev_index &&
337 a->instance == b->instance);
338}
339
f1e8bf56 340static int slim_match_dev(struct device *dev, const void *data)
46a2bb5a 341{
f1e8bf56 342 const struct slim_eaddr *e_addr = data;
46a2bb5a
SD
343 struct slim_device *sbdev = to_slim_device(dev);
344
345 return slim_eaddr_equal(&sbdev->e_addr, e_addr);
346}
347
348static struct slim_device *find_slim_device(struct slim_controller *ctrl,
349 struct slim_eaddr *eaddr)
350{
351 struct slim_device *sbdev;
352 struct device *dev;
353
354 dev = device_find_child(ctrl->dev, eaddr, slim_match_dev);
355 if (dev) {
356 sbdev = to_slim_device(dev);
357 return sbdev;
358 }
359
360 return NULL;
361}
362
363/**
364 * slim_get_device() - get handle to a device.
365 *
366 * @ctrl: Controller on which this device will be added/queried
367 * @e_addr: Enumeration address of the device to be queried
368 *
369 * Return: pointer to a device if it has already reported. Creates a new
370 * device and returns pointer to it if the device has not yet enumerated.
371 */
372struct slim_device *slim_get_device(struct slim_controller *ctrl,
373 struct slim_eaddr *e_addr)
374{
375 struct slim_device *sbdev;
376
377 sbdev = find_slim_device(ctrl, e_addr);
378 if (!sbdev) {
379 sbdev = slim_alloc_device(ctrl, e_addr, NULL);
380 if (!sbdev)
381 return ERR_PTR(-ENOMEM);
382 }
383
384 return sbdev;
385}
386EXPORT_SYMBOL_GPL(slim_get_device);
387
e0772de8
SK
388static struct slim_device *of_find_slim_device(struct slim_controller *ctrl,
389 struct device_node *np)
390{
391 struct slim_device *sbdev;
392 struct device *dev;
393
989e2b35 394 dev = device_find_child(ctrl->dev, np, device_match_of_node);
e0772de8
SK
395 if (dev) {
396 sbdev = to_slim_device(dev);
397 return sbdev;
398 }
399
400 return NULL;
401}
402
403/**
404 * of_slim_get_device() - get handle to a device using dt node.
405 *
406 * @ctrl: Controller on which this device will be added/queried
407 * @np: node pointer to device
408 *
409 * Return: pointer to a device if it has already reported. Creates a new
410 * device and returns pointer to it if the device has not yet enumerated.
411 */
412struct slim_device *of_slim_get_device(struct slim_controller *ctrl,
413 struct device_node *np)
414{
415 return of_find_slim_device(ctrl, np);
416}
417EXPORT_SYMBOL_GPL(of_slim_get_device);
418
46a2bb5a
SD
419static int slim_device_alloc_laddr(struct slim_device *sbdev,
420 bool report_present)
421{
422 struct slim_controller *ctrl = sbdev->ctrl;
423 u8 laddr;
424 int ret;
425
426 mutex_lock(&ctrl->lock);
427 if (ctrl->get_laddr) {
428 ret = ctrl->get_laddr(ctrl, &sbdev->e_addr, &laddr);
429 if (ret < 0)
430 goto err;
431 } else if (report_present) {
89ffa4cc
CJ
432 ret = ida_alloc_max(&ctrl->laddr_ida,
433 SLIM_LA_MANAGER - 1, GFP_KERNEL);
46a2bb5a
SD
434 if (ret < 0)
435 goto err;
436
437 laddr = ret;
438 } else {
439 ret = -EINVAL;
440 goto err;
441 }
442
443 if (ctrl->set_laddr) {
444 ret = ctrl->set_laddr(ctrl, &sbdev->e_addr, laddr);
445 if (ret) {
446 ret = -EINVAL;
447 goto err;
448 }
449 }
450
451 sbdev->laddr = laddr;
452 sbdev->is_laddr_valid = true;
cfb32101 453 mutex_unlock(&ctrl->lock);
46a2bb5a
SD
454
455 slim_device_update_status(sbdev, SLIM_DEVICE_STATUS_UP);
456
457 dev_dbg(ctrl->dev, "setting slimbus l-addr:%x, ea:%x,%x,%x,%x\n",
458 laddr, sbdev->e_addr.manf_id, sbdev->e_addr.prod_code,
459 sbdev->e_addr.dev_index, sbdev->e_addr.instance);
460
cfb32101
SK
461 return 0;
462
46a2bb5a
SD
463err:
464 mutex_unlock(&ctrl->lock);
465 return ret;
466
467}
468
469/**
470 * slim_device_report_present() - Report enumerated device.
471 *
472 * @ctrl: Controller with which device is enumerated.
473 * @e_addr: Enumeration address of the device.
474 * @laddr: Return logical address (if valid flag is false)
475 *
476 * Called by controller in response to REPORT_PRESENT. Framework will assign
477 * a logical address to this enumeration address.
478 * Function returns -EXFULL to indicate that all logical addresses are already
479 * taken.
480 */
481int slim_device_report_present(struct slim_controller *ctrl,
482 struct slim_eaddr *e_addr, u8 *laddr)
483{
484 struct slim_device *sbdev;
485 int ret;
486
4b14e62a
SD
487 ret = pm_runtime_get_sync(ctrl->dev);
488
489 if (ctrl->sched.clk_state != SLIM_CLK_ACTIVE) {
490 dev_err(ctrl->dev, "slim ctrl not active,state:%d, ret:%d\n",
491 ctrl->sched.clk_state, ret);
492 goto slimbus_not_active;
493 }
494
46a2bb5a
SD
495 sbdev = slim_get_device(ctrl, e_addr);
496 if (IS_ERR(sbdev))
497 return -ENODEV;
498
499 if (sbdev->is_laddr_valid) {
500 *laddr = sbdev->laddr;
501 return 0;
502 }
503
504 ret = slim_device_alloc_laddr(sbdev, true);
505
4b14e62a
SD
506slimbus_not_active:
507 pm_runtime_mark_last_busy(ctrl->dev);
508 pm_runtime_put_autosuspend(ctrl->dev);
46a2bb5a
SD
509 return ret;
510}
511EXPORT_SYMBOL_GPL(slim_device_report_present);
512
513/**
514 * slim_get_logical_addr() - get/allocate logical address of a SLIMbus device.
515 *
516 * @sbdev: client handle requesting the address.
517 *
518 * Return: zero if a logical address is valid or a new logical address
519 * has been assigned. error code in case of error.
520 */
521int slim_get_logical_addr(struct slim_device *sbdev)
522{
523 if (!sbdev->is_laddr_valid)
524 return slim_device_alloc_laddr(sbdev, false);
525
526 return 0;
527}
528EXPORT_SYMBOL_GPL(slim_get_logical_addr);
529
3648e78e
SD
530static void __exit slimbus_exit(void)
531{
532 bus_unregister(&slimbus_bus);
533}
534module_exit(slimbus_exit);
535
536static int __init slimbus_init(void)
537{
538 return bus_register(&slimbus_bus);
539}
540postcore_initcall(slimbus_init);
541
542MODULE_LICENSE("GPL v2");
543MODULE_DESCRIPTION("SLIMbus core");