xref: /linux/drivers/slimbus/core.c (revision e0772de8a48b69d39624cdf99fac8f4a3fcc387b)
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 
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 
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;
117 
118 	if (node)
119 		sbdev->dev.of_node = of_node_get(node);
120 
121 	dev_set_name(&sbdev->dev, "%x:%x:%x:%x",
122 				  sbdev->e_addr.manf_id,
123 				  sbdev->e_addr.prod_code,
124 				  sbdev->e_addr.dev_index,
125 				  sbdev->e_addr.instance);
126 
127 	return device_register(&sbdev->dev);
128 }
129 
130 static struct slim_device *slim_alloc_device(struct slim_controller *ctrl,
131 					     struct slim_eaddr *eaddr,
132 					     struct device_node *node)
133 {
134 	struct slim_device *sbdev;
135 	int ret;
136 
137 	sbdev = kzalloc(sizeof(*sbdev), GFP_KERNEL);
138 	if (!sbdev)
139 		return NULL;
140 
141 	sbdev->e_addr = *eaddr;
142 	ret = slim_add_device(ctrl, sbdev, node);
143 	if (ret) {
144 		put_device(&sbdev->dev);
145 		return NULL;
146 	}
147 
148 	return sbdev;
149 }
150 
151 static void of_register_slim_devices(struct slim_controller *ctrl)
152 {
153 	struct device *dev = ctrl->dev;
154 	struct device_node *node;
155 
156 	if (!ctrl->dev->of_node)
157 		return;
158 
159 	for_each_child_of_node(ctrl->dev->of_node, node) {
160 		struct slim_device *sbdev;
161 		struct slim_eaddr e_addr;
162 		const char *compat = NULL;
163 		int reg[2], ret;
164 		int manf_id, prod_code;
165 
166 		compat = of_get_property(node, "compatible", NULL);
167 		if (!compat)
168 			continue;
169 
170 		ret = sscanf(compat, "slim%x,%x", &manf_id, &prod_code);
171 		if (ret != 2) {
172 			dev_err(dev, "Manf ID & Product code not found %s\n",
173 				compat);
174 			continue;
175 		}
176 
177 		ret = of_property_read_u32_array(node, "reg", reg, 2);
178 		if (ret) {
179 			dev_err(dev, "Device and Instance id not found:%d\n",
180 				ret);
181 			continue;
182 		}
183 
184 		e_addr.dev_index = reg[0];
185 		e_addr.instance = reg[1];
186 		e_addr.manf_id = manf_id;
187 		e_addr.prod_code = prod_code;
188 
189 		sbdev = slim_alloc_device(ctrl, &e_addr, node);
190 		if (!sbdev)
191 			continue;
192 	}
193 }
194 
195 /*
196  * slim_register_controller() - Controller bring-up and registration.
197  *
198  * @ctrl: Controller to be registered.
199  *
200  * A controller is registered with the framework using this API.
201  * If devices on a controller were registered before controller,
202  * this will make sure that they get probed when controller is up
203  */
204 int slim_register_controller(struct slim_controller *ctrl)
205 {
206 	int id;
207 
208 	id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL);
209 	if (id < 0)
210 		return id;
211 
212 	ctrl->id = id;
213 
214 	if (!ctrl->min_cg)
215 		ctrl->min_cg = SLIM_MIN_CLK_GEAR;
216 	if (!ctrl->max_cg)
217 		ctrl->max_cg = SLIM_MAX_CLK_GEAR;
218 
219 	ida_init(&ctrl->laddr_ida);
220 	idr_init(&ctrl->tid_idr);
221 	mutex_init(&ctrl->lock);
222 	mutex_init(&ctrl->sched.m_reconf);
223 	init_completion(&ctrl->sched.pause_comp);
224 
225 	dev_dbg(ctrl->dev, "Bus [%s] registered:dev:%p\n",
226 		ctrl->name, ctrl->dev);
227 
228 	of_register_slim_devices(ctrl);
229 
230 	return 0;
231 }
232 EXPORT_SYMBOL_GPL(slim_register_controller);
233 
234 /* slim_remove_device: Remove the effect of slim_add_device() */
235 static void slim_remove_device(struct slim_device *sbdev)
236 {
237 	device_unregister(&sbdev->dev);
238 }
239 
240 static int slim_ctrl_remove_device(struct device *dev, void *null)
241 {
242 	slim_remove_device(to_slim_device(dev));
243 	return 0;
244 }
245 
246 /**
247  * slim_unregister_controller() - Controller tear-down.
248  *
249  * @ctrl: Controller to tear-down.
250  */
251 int slim_unregister_controller(struct slim_controller *ctrl)
252 {
253 	/* Remove all clients */
254 	device_for_each_child(ctrl->dev, NULL, slim_ctrl_remove_device);
255 	/* Enter Clock Pause */
256 	slim_ctrl_clk_pause(ctrl, false, 0);
257 	ida_simple_remove(&ctrl_ida, ctrl->id);
258 
259 	return 0;
260 }
261 EXPORT_SYMBOL_GPL(slim_unregister_controller);
262 
263 static void slim_device_update_status(struct slim_device *sbdev,
264 				      enum slim_device_status status)
265 {
266 	struct slim_driver *sbdrv;
267 
268 	if (sbdev->status == status)
269 		return;
270 
271 	sbdev->status = status;
272 	if (!sbdev->dev.driver)
273 		return;
274 
275 	sbdrv = to_slim_driver(sbdev->dev.driver);
276 	if (sbdrv->device_status)
277 		sbdrv->device_status(sbdev, sbdev->status);
278 }
279 
280 /**
281  * slim_report_absent() - Controller calls this function when a device
282  *	reports absent, OR when the device cannot be communicated with
283  *
284  * @sbdev: Device that cannot be reached, or sent report absent
285  */
286 void slim_report_absent(struct slim_device *sbdev)
287 {
288 	struct slim_controller *ctrl = sbdev->ctrl;
289 
290 	if (!ctrl)
291 		return;
292 
293 	/* invalidate logical addresses */
294 	mutex_lock(&ctrl->lock);
295 	sbdev->is_laddr_valid = false;
296 	mutex_unlock(&ctrl->lock);
297 
298 	ida_simple_remove(&ctrl->laddr_ida, sbdev->laddr);
299 	slim_device_update_status(sbdev, SLIM_DEVICE_STATUS_DOWN);
300 }
301 EXPORT_SYMBOL_GPL(slim_report_absent);
302 
303 static bool slim_eaddr_equal(struct slim_eaddr *a, struct slim_eaddr *b)
304 {
305 	return (a->manf_id == b->manf_id &&
306 		a->prod_code == b->prod_code &&
307 		a->dev_index == b->dev_index &&
308 		a->instance == b->instance);
309 }
310 
311 static int slim_match_dev(struct device *dev, void *data)
312 {
313 	struct slim_eaddr *e_addr = data;
314 	struct slim_device *sbdev = to_slim_device(dev);
315 
316 	return slim_eaddr_equal(&sbdev->e_addr, e_addr);
317 }
318 
319 static struct slim_device *find_slim_device(struct slim_controller *ctrl,
320 					    struct slim_eaddr *eaddr)
321 {
322 	struct slim_device *sbdev;
323 	struct device *dev;
324 
325 	dev = device_find_child(ctrl->dev, eaddr, slim_match_dev);
326 	if (dev) {
327 		sbdev = to_slim_device(dev);
328 		return sbdev;
329 	}
330 
331 	return NULL;
332 }
333 
334 /**
335  * slim_get_device() - get handle to a device.
336  *
337  * @ctrl: Controller on which this device will be added/queried
338  * @e_addr: Enumeration address of the device to be queried
339  *
340  * Return: pointer to a device if it has already reported. Creates a new
341  * device and returns pointer to it if the device has not yet enumerated.
342  */
343 struct slim_device *slim_get_device(struct slim_controller *ctrl,
344 				    struct slim_eaddr *e_addr)
345 {
346 	struct slim_device *sbdev;
347 
348 	sbdev = find_slim_device(ctrl, e_addr);
349 	if (!sbdev) {
350 		sbdev = slim_alloc_device(ctrl, e_addr, NULL);
351 		if (!sbdev)
352 			return ERR_PTR(-ENOMEM);
353 	}
354 
355 	return sbdev;
356 }
357 EXPORT_SYMBOL_GPL(slim_get_device);
358 
359 static int of_slim_match_dev(struct device *dev, void *data)
360 {
361 	struct device_node *np = data;
362 	struct slim_device *sbdev = to_slim_device(dev);
363 
364 	return (sbdev->dev.of_node == np);
365 }
366 
367 static struct slim_device *of_find_slim_device(struct slim_controller *ctrl,
368 					       struct device_node *np)
369 {
370 	struct slim_device *sbdev;
371 	struct device *dev;
372 
373 	dev = device_find_child(ctrl->dev, np, of_slim_match_dev);
374 	if (dev) {
375 		sbdev = to_slim_device(dev);
376 		return sbdev;
377 	}
378 
379 	return NULL;
380 }
381 
382 /**
383  * of_slim_get_device() - get handle to a device using dt node.
384  *
385  * @ctrl: Controller on which this device will be added/queried
386  * @np: node pointer to device
387  *
388  * Return: pointer to a device if it has already reported. Creates a new
389  * device and returns pointer to it if the device has not yet enumerated.
390  */
391 struct slim_device *of_slim_get_device(struct slim_controller *ctrl,
392 				       struct device_node *np)
393 {
394 	return of_find_slim_device(ctrl, np);
395 }
396 EXPORT_SYMBOL_GPL(of_slim_get_device);
397 
398 static int slim_device_alloc_laddr(struct slim_device *sbdev,
399 				   bool report_present)
400 {
401 	struct slim_controller *ctrl = sbdev->ctrl;
402 	u8 laddr;
403 	int ret;
404 
405 	mutex_lock(&ctrl->lock);
406 	if (ctrl->get_laddr) {
407 		ret = ctrl->get_laddr(ctrl, &sbdev->e_addr, &laddr);
408 		if (ret < 0)
409 			goto err;
410 	} else if (report_present) {
411 		ret = ida_simple_get(&ctrl->laddr_ida,
412 				     0, SLIM_LA_MANAGER - 1, GFP_KERNEL);
413 		if (ret < 0)
414 			goto err;
415 
416 		laddr = ret;
417 	} else {
418 		ret = -EINVAL;
419 		goto err;
420 	}
421 
422 	if (ctrl->set_laddr) {
423 		ret = ctrl->set_laddr(ctrl, &sbdev->e_addr, laddr);
424 		if (ret) {
425 			ret = -EINVAL;
426 			goto err;
427 		}
428 	}
429 
430 	sbdev->laddr = laddr;
431 	sbdev->is_laddr_valid = true;
432 
433 	slim_device_update_status(sbdev, SLIM_DEVICE_STATUS_UP);
434 
435 	dev_dbg(ctrl->dev, "setting slimbus l-addr:%x, ea:%x,%x,%x,%x\n",
436 		laddr, sbdev->e_addr.manf_id, sbdev->e_addr.prod_code,
437 		sbdev->e_addr.dev_index, sbdev->e_addr.instance);
438 
439 err:
440 	mutex_unlock(&ctrl->lock);
441 	return ret;
442 
443 }
444 
445 /**
446  * slim_device_report_present() - Report enumerated device.
447  *
448  * @ctrl: Controller with which device is enumerated.
449  * @e_addr: Enumeration address of the device.
450  * @laddr: Return logical address (if valid flag is false)
451  *
452  * Called by controller in response to REPORT_PRESENT. Framework will assign
453  * a logical address to this enumeration address.
454  * Function returns -EXFULL to indicate that all logical addresses are already
455  * taken.
456  */
457 int slim_device_report_present(struct slim_controller *ctrl,
458 			       struct slim_eaddr *e_addr, u8 *laddr)
459 {
460 	struct slim_device *sbdev;
461 	int ret;
462 
463 	ret = pm_runtime_get_sync(ctrl->dev);
464 
465 	if (ctrl->sched.clk_state != SLIM_CLK_ACTIVE) {
466 		dev_err(ctrl->dev, "slim ctrl not active,state:%d, ret:%d\n",
467 				    ctrl->sched.clk_state, ret);
468 		goto slimbus_not_active;
469 	}
470 
471 	sbdev = slim_get_device(ctrl, e_addr);
472 	if (IS_ERR(sbdev))
473 		return -ENODEV;
474 
475 	if (sbdev->is_laddr_valid) {
476 		*laddr = sbdev->laddr;
477 		return 0;
478 	}
479 
480 	ret = slim_device_alloc_laddr(sbdev, true);
481 
482 slimbus_not_active:
483 	pm_runtime_mark_last_busy(ctrl->dev);
484 	pm_runtime_put_autosuspend(ctrl->dev);
485 	return ret;
486 }
487 EXPORT_SYMBOL_GPL(slim_device_report_present);
488 
489 /**
490  * slim_get_logical_addr() - get/allocate logical address of a SLIMbus device.
491  *
492  * @sbdev: client handle requesting the address.
493  *
494  * Return: zero if a logical address is valid or a new logical address
495  * has been assigned. error code in case of error.
496  */
497 int slim_get_logical_addr(struct slim_device *sbdev)
498 {
499 	if (!sbdev->is_laddr_valid)
500 		return slim_device_alloc_laddr(sbdev, false);
501 
502 	return 0;
503 }
504 EXPORT_SYMBOL_GPL(slim_get_logical_addr);
505 
506 static void __exit slimbus_exit(void)
507 {
508 	bus_unregister(&slimbus_bus);
509 }
510 module_exit(slimbus_exit);
511 
512 static int __init slimbus_init(void)
513 {
514 	return bus_register(&slimbus_bus);
515 }
516 postcore_initcall(slimbus_init);
517 
518 MODULE_LICENSE("GPL v2");
519 MODULE_DESCRIPTION("SLIMbus core");
520