Commit | Line | Data |
---|---|---|
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> |
4b14e62a | 12 | #include <linux/pm_runtime.h> |
3648e78e | 13 | #include <linux/slimbus.h> |
46a2bb5a SD |
14 | #include "slimbus.h" |
15 | ||
16 | static DEFINE_IDA(ctrl_ida); | |
3648e78e SD |
17 | |
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 | ||
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 | ||
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 | ||
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 | */ | |
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 | */ | |
95 | void slim_driver_unregister(struct slim_driver *drv) | |
96 | { | |
97 | driver_unregister(&drv->driver); | |
98 | } | |
99 | EXPORT_SYMBOL_GPL(slim_driver_unregister); | |
100 | ||
46a2bb5a SD |
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 | ||
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; | |
abb9c9b8 SK |
117 | INIT_LIST_HEAD(&sbdev->stream_list); |
118 | spin_lock_init(&sbdev->stream_list_lock); | |
46a2bb5a | 119 | |
7588a511 SK |
120 | if (node) |
121 | sbdev->dev.of_node = of_node_get(node); | |
122 | ||
46a2bb5a SD |
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 | ||
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) { | |
bd329f02 | 146 | put_device(&sbdev->dev); |
46a2bb5a SD |
147 | return NULL; |
148 | } | |
149 | ||
150 | return sbdev; | |
151 | } | |
152 | ||
7588a511 SK |
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 | ||
46a2bb5a SD |
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 | */ | |
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); | |
4b14e62a SD |
224 | mutex_init(&ctrl->sched.m_reconf); |
225 | init_completion(&ctrl->sched.pause_comp); | |
46a2bb5a SD |
226 | |
227 | dev_dbg(ctrl->dev, "Bus [%s] registered:dev:%p\n", | |
228 | ctrl->name, ctrl->dev); | |
229 | ||
7588a511 SK |
230 | of_register_slim_devices(ctrl); |
231 | ||
46a2bb5a SD |
232 | return 0; |
233 | } | |
234 | EXPORT_SYMBOL_GPL(slim_register_controller); | |
235 | ||
236 | /* slim_remove_device: Remove the effect of slim_add_device() */ | |
237 | static void slim_remove_device(struct slim_device *sbdev) | |
238 | { | |
239 | device_unregister(&sbdev->dev); | |
240 | } | |
241 | ||
242 | static int slim_ctrl_remove_device(struct device *dev, void *null) | |
243 | { | |
244 | slim_remove_device(to_slim_device(dev)); | |
245 | return 0; | |
246 | } | |
247 | ||
248 | /** | |
249 | * slim_unregister_controller() - Controller tear-down. | |
250 | * | |
251 | * @ctrl: Controller to tear-down. | |
252 | */ | |
253 | int slim_unregister_controller(struct slim_controller *ctrl) | |
254 | { | |
255 | /* Remove all clients */ | |
256 | device_for_each_child(ctrl->dev, NULL, slim_ctrl_remove_device); | |
4b14e62a SD |
257 | /* Enter Clock Pause */ |
258 | slim_ctrl_clk_pause(ctrl, false, 0); | |
46a2bb5a SD |
259 | ida_simple_remove(&ctrl_ida, ctrl->id); |
260 | ||
261 | return 0; | |
262 | } | |
263 | EXPORT_SYMBOL_GPL(slim_unregister_controller); | |
264 | ||
265 | static void slim_device_update_status(struct slim_device *sbdev, | |
266 | enum slim_device_status status) | |
267 | { | |
268 | struct slim_driver *sbdrv; | |
269 | ||
270 | if (sbdev->status == status) | |
271 | return; | |
272 | ||
273 | sbdev->status = status; | |
274 | if (!sbdev->dev.driver) | |
275 | return; | |
276 | ||
277 | sbdrv = to_slim_driver(sbdev->dev.driver); | |
278 | if (sbdrv->device_status) | |
279 | sbdrv->device_status(sbdev, sbdev->status); | |
280 | } | |
281 | ||
282 | /** | |
283 | * slim_report_absent() - Controller calls this function when a device | |
284 | * reports absent, OR when the device cannot be communicated with | |
285 | * | |
286 | * @sbdev: Device that cannot be reached, or sent report absent | |
287 | */ | |
288 | void slim_report_absent(struct slim_device *sbdev) | |
289 | { | |
290 | struct slim_controller *ctrl = sbdev->ctrl; | |
291 | ||
292 | if (!ctrl) | |
293 | return; | |
294 | ||
295 | /* invalidate logical addresses */ | |
296 | mutex_lock(&ctrl->lock); | |
297 | sbdev->is_laddr_valid = false; | |
298 | mutex_unlock(&ctrl->lock); | |
299 | ||
300 | ida_simple_remove(&ctrl->laddr_ida, sbdev->laddr); | |
301 | slim_device_update_status(sbdev, SLIM_DEVICE_STATUS_DOWN); | |
302 | } | |
303 | EXPORT_SYMBOL_GPL(slim_report_absent); | |
304 | ||
305 | static bool slim_eaddr_equal(struct slim_eaddr *a, struct slim_eaddr *b) | |
306 | { | |
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); | |
311 | } | |
312 | ||
313 | static int slim_match_dev(struct device *dev, void *data) | |
314 | { | |
315 | struct slim_eaddr *e_addr = data; | |
316 | struct slim_device *sbdev = to_slim_device(dev); | |
317 | ||
318 | return slim_eaddr_equal(&sbdev->e_addr, e_addr); | |
319 | } | |
320 | ||
321 | static struct slim_device *find_slim_device(struct slim_controller *ctrl, | |
322 | struct slim_eaddr *eaddr) | |
323 | { | |
324 | struct slim_device *sbdev; | |
325 | struct device *dev; | |
326 | ||
327 | dev = device_find_child(ctrl->dev, eaddr, slim_match_dev); | |
328 | if (dev) { | |
329 | sbdev = to_slim_device(dev); | |
330 | return sbdev; | |
331 | } | |
332 | ||
333 | return NULL; | |
334 | } | |
335 | ||
336 | /** | |
337 | * slim_get_device() - get handle to a device. | |
338 | * | |
339 | * @ctrl: Controller on which this device will be added/queried | |
340 | * @e_addr: Enumeration address of the device to be queried | |
341 | * | |
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. | |
344 | */ | |
345 | struct slim_device *slim_get_device(struct slim_controller *ctrl, | |
346 | struct slim_eaddr *e_addr) | |
347 | { | |
348 | struct slim_device *sbdev; | |
349 | ||
350 | sbdev = find_slim_device(ctrl, e_addr); | |
351 | if (!sbdev) { | |
352 | sbdev = slim_alloc_device(ctrl, e_addr, NULL); | |
353 | if (!sbdev) | |
354 | return ERR_PTR(-ENOMEM); | |
355 | } | |
356 | ||
357 | return sbdev; | |
358 | } | |
359 | EXPORT_SYMBOL_GPL(slim_get_device); | |
360 | ||
e0772de8 SK |
361 | static int of_slim_match_dev(struct device *dev, void *data) |
362 | { | |
363 | struct device_node *np = data; | |
364 | struct slim_device *sbdev = to_slim_device(dev); | |
365 | ||
366 | return (sbdev->dev.of_node == np); | |
367 | } | |
368 | ||
369 | static struct slim_device *of_find_slim_device(struct slim_controller *ctrl, | |
370 | struct device_node *np) | |
371 | { | |
372 | struct slim_device *sbdev; | |
373 | struct device *dev; | |
374 | ||
375 | dev = device_find_child(ctrl->dev, np, of_slim_match_dev); | |
376 | if (dev) { | |
377 | sbdev = to_slim_device(dev); | |
378 | return sbdev; | |
379 | } | |
380 | ||
381 | return NULL; | |
382 | } | |
383 | ||
384 | /** | |
385 | * of_slim_get_device() - get handle to a device using dt node. | |
386 | * | |
387 | * @ctrl: Controller on which this device will be added/queried | |
388 | * @np: node pointer to device | |
389 | * | |
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. | |
392 | */ | |
393 | struct slim_device *of_slim_get_device(struct slim_controller *ctrl, | |
394 | struct device_node *np) | |
395 | { | |
396 | return of_find_slim_device(ctrl, np); | |
397 | } | |
398 | EXPORT_SYMBOL_GPL(of_slim_get_device); | |
399 | ||
46a2bb5a SD |
400 | static int slim_device_alloc_laddr(struct slim_device *sbdev, |
401 | bool report_present) | |
402 | { | |
403 | struct slim_controller *ctrl = sbdev->ctrl; | |
404 | u8 laddr; | |
405 | int ret; | |
406 | ||
407 | mutex_lock(&ctrl->lock); | |
408 | if (ctrl->get_laddr) { | |
409 | ret = ctrl->get_laddr(ctrl, &sbdev->e_addr, &laddr); | |
410 | if (ret < 0) | |
411 | goto err; | |
412 | } else if (report_present) { | |
413 | ret = ida_simple_get(&ctrl->laddr_ida, | |
414 | 0, SLIM_LA_MANAGER - 1, GFP_KERNEL); | |
415 | if (ret < 0) | |
416 | goto err; | |
417 | ||
418 | laddr = ret; | |
419 | } else { | |
420 | ret = -EINVAL; | |
421 | goto err; | |
422 | } | |
423 | ||
424 | if (ctrl->set_laddr) { | |
425 | ret = ctrl->set_laddr(ctrl, &sbdev->e_addr, laddr); | |
426 | if (ret) { | |
427 | ret = -EINVAL; | |
428 | goto err; | |
429 | } | |
430 | } | |
431 | ||
432 | sbdev->laddr = laddr; | |
433 | sbdev->is_laddr_valid = true; | |
434 | ||
435 | slim_device_update_status(sbdev, SLIM_DEVICE_STATUS_UP); | |
436 | ||
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); | |
440 | ||
441 | err: | |
442 | mutex_unlock(&ctrl->lock); | |
443 | return ret; | |
444 | ||
445 | } | |
446 | ||
447 | /** | |
448 | * slim_device_report_present() - Report enumerated device. | |
449 | * | |
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) | |
453 | * | |
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 | |
457 | * taken. | |
458 | */ | |
459 | int slim_device_report_present(struct slim_controller *ctrl, | |
460 | struct slim_eaddr *e_addr, u8 *laddr) | |
461 | { | |
462 | struct slim_device *sbdev; | |
463 | int ret; | |
464 | ||
4b14e62a SD |
465 | ret = pm_runtime_get_sync(ctrl->dev); |
466 | ||
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; | |
471 | } | |
472 | ||
46a2bb5a SD |
473 | sbdev = slim_get_device(ctrl, e_addr); |
474 | if (IS_ERR(sbdev)) | |
475 | return -ENODEV; | |
476 | ||
477 | if (sbdev->is_laddr_valid) { | |
478 | *laddr = sbdev->laddr; | |
479 | return 0; | |
480 | } | |
481 | ||
482 | ret = slim_device_alloc_laddr(sbdev, true); | |
483 | ||
4b14e62a SD |
484 | slimbus_not_active: |
485 | pm_runtime_mark_last_busy(ctrl->dev); | |
486 | pm_runtime_put_autosuspend(ctrl->dev); | |
46a2bb5a SD |
487 | return ret; |
488 | } | |
489 | EXPORT_SYMBOL_GPL(slim_device_report_present); | |
490 | ||
491 | /** | |
492 | * slim_get_logical_addr() - get/allocate logical address of a SLIMbus device. | |
493 | * | |
494 | * @sbdev: client handle requesting the address. | |
495 | * | |
496 | * Return: zero if a logical address is valid or a new logical address | |
497 | * has been assigned. error code in case of error. | |
498 | */ | |
499 | int slim_get_logical_addr(struct slim_device *sbdev) | |
500 | { | |
501 | if (!sbdev->is_laddr_valid) | |
502 | return slim_device_alloc_laddr(sbdev, false); | |
503 | ||
504 | return 0; | |
505 | } | |
506 | EXPORT_SYMBOL_GPL(slim_get_logical_addr); | |
507 | ||
3648e78e SD |
508 | static void __exit slimbus_exit(void) |
509 | { | |
510 | bus_unregister(&slimbus_bus); | |
511 | } | |
512 | module_exit(slimbus_exit); | |
513 | ||
514 | static int __init slimbus_init(void) | |
515 | { | |
516 | return bus_register(&slimbus_bus); | |
517 | } | |
518 | postcore_initcall(slimbus_init); | |
519 | ||
520 | MODULE_LICENSE("GPL v2"); | |
521 | MODULE_DESCRIPTION("SLIMbus core"); |