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