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