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