xref: /openbmc/linux/drivers/tty/serdev/core.c (revision 37b9b9a5)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2016-2017 Linaro Ltd., Rob Herring <robh@kernel.org>
4  *
5  * Based on drivers/spmi/spmi.c:
6  * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
7  */
8 
9 #include <linux/acpi.h>
10 #include <linux/errno.h>
11 #include <linux/idr.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/of_device.h>
16 #include <linux/pm_domain.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/sched.h>
19 #include <linux/serdev.h>
20 #include <linux/slab.h>
21 
22 static bool is_registered;
23 static DEFINE_IDA(ctrl_ida);
24 
25 static ssize_t modalias_show(struct device *dev,
26 			     struct device_attribute *attr, char *buf)
27 {
28 	int len;
29 
30 	len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
31 	if (len != -ENODEV)
32 		return len;
33 
34 	return of_device_modalias(dev, buf, PAGE_SIZE);
35 }
36 static DEVICE_ATTR_RO(modalias);
37 
38 static struct attribute *serdev_device_attrs[] = {
39 	&dev_attr_modalias.attr,
40 	NULL,
41 };
42 ATTRIBUTE_GROUPS(serdev_device);
43 
44 static int serdev_device_uevent(struct device *dev, struct kobj_uevent_env *env)
45 {
46 	int rc;
47 
48 	/* TODO: platform modalias */
49 
50 	rc = acpi_device_uevent_modalias(dev, env);
51 	if (rc != -ENODEV)
52 		return rc;
53 
54 	return of_device_uevent_modalias(dev, env);
55 }
56 
57 static void serdev_device_release(struct device *dev)
58 {
59 	struct serdev_device *serdev = to_serdev_device(dev);
60 	kfree(serdev);
61 }
62 
63 static const struct device_type serdev_device_type = {
64 	.groups		= serdev_device_groups,
65 	.uevent		= serdev_device_uevent,
66 	.release	= serdev_device_release,
67 };
68 
69 static bool is_serdev_device(const struct device *dev)
70 {
71 	return dev->type == &serdev_device_type;
72 }
73 
74 static void serdev_ctrl_release(struct device *dev)
75 {
76 	struct serdev_controller *ctrl = to_serdev_controller(dev);
77 	ida_simple_remove(&ctrl_ida, ctrl->nr);
78 	kfree(ctrl);
79 }
80 
81 static const struct device_type serdev_ctrl_type = {
82 	.release	= serdev_ctrl_release,
83 };
84 
85 static int serdev_device_match(struct device *dev, struct device_driver *drv)
86 {
87 	if (!is_serdev_device(dev))
88 		return 0;
89 
90 	/* TODO: platform matching */
91 	if (acpi_driver_match_device(dev, drv))
92 		return 1;
93 
94 	return of_driver_match_device(dev, drv);
95 }
96 
97 /**
98  * serdev_device_add() - add a device previously constructed via serdev_device_alloc()
99  * @serdev:	serdev_device to be added
100  */
101 int serdev_device_add(struct serdev_device *serdev)
102 {
103 	struct serdev_controller *ctrl = serdev->ctrl;
104 	struct device *parent = serdev->dev.parent;
105 	int err;
106 
107 	dev_set_name(&serdev->dev, "%s-%d", dev_name(parent), serdev->nr);
108 
109 	/* Only a single slave device is currently supported. */
110 	if (ctrl->serdev) {
111 		dev_err(&serdev->dev, "controller busy\n");
112 		return -EBUSY;
113 	}
114 	ctrl->serdev = serdev;
115 
116 	err = device_add(&serdev->dev);
117 	if (err < 0) {
118 		dev_err(&serdev->dev, "Can't add %s, status %d\n",
119 			dev_name(&serdev->dev), err);
120 		goto err_clear_serdev;
121 	}
122 
123 	dev_dbg(&serdev->dev, "device %s registered\n", dev_name(&serdev->dev));
124 
125 	return 0;
126 
127 err_clear_serdev:
128 	ctrl->serdev = NULL;
129 	return err;
130 }
131 EXPORT_SYMBOL_GPL(serdev_device_add);
132 
133 /**
134  * serdev_device_remove(): remove an serdev device
135  * @serdev:	serdev_device to be removed
136  */
137 void serdev_device_remove(struct serdev_device *serdev)
138 {
139 	struct serdev_controller *ctrl = serdev->ctrl;
140 
141 	device_unregister(&serdev->dev);
142 	ctrl->serdev = NULL;
143 }
144 EXPORT_SYMBOL_GPL(serdev_device_remove);
145 
146 int serdev_device_open(struct serdev_device *serdev)
147 {
148 	struct serdev_controller *ctrl = serdev->ctrl;
149 	int ret;
150 
151 	if (!ctrl || !ctrl->ops->open)
152 		return -EINVAL;
153 
154 	ret = ctrl->ops->open(ctrl);
155 	if (ret)
156 		return ret;
157 
158 	ret = pm_runtime_get_sync(&ctrl->dev);
159 	if (ret < 0) {
160 		pm_runtime_put_noidle(&ctrl->dev);
161 		goto err_close;
162 	}
163 
164 	return 0;
165 
166 err_close:
167 	if (ctrl->ops->close)
168 		ctrl->ops->close(ctrl);
169 
170 	return ret;
171 }
172 EXPORT_SYMBOL_GPL(serdev_device_open);
173 
174 void serdev_device_close(struct serdev_device *serdev)
175 {
176 	struct serdev_controller *ctrl = serdev->ctrl;
177 
178 	if (!ctrl || !ctrl->ops->close)
179 		return;
180 
181 	pm_runtime_put(&ctrl->dev);
182 
183 	ctrl->ops->close(ctrl);
184 }
185 EXPORT_SYMBOL_GPL(serdev_device_close);
186 
187 static void devm_serdev_device_release(struct device *dev, void *dr)
188 {
189 	serdev_device_close(*(struct serdev_device **)dr);
190 }
191 
192 int devm_serdev_device_open(struct device *dev, struct serdev_device *serdev)
193 {
194 	struct serdev_device **dr;
195 	int ret;
196 
197 	dr = devres_alloc(devm_serdev_device_release, sizeof(*dr), GFP_KERNEL);
198 	if (!dr)
199 		return -ENOMEM;
200 
201 	ret = serdev_device_open(serdev);
202 	if (ret) {
203 		devres_free(dr);
204 		return ret;
205 	}
206 
207 	*dr = serdev;
208 	devres_add(dev, dr);
209 
210 	return 0;
211 }
212 EXPORT_SYMBOL_GPL(devm_serdev_device_open);
213 
214 void serdev_device_write_wakeup(struct serdev_device *serdev)
215 {
216 	complete(&serdev->write_comp);
217 }
218 EXPORT_SYMBOL_GPL(serdev_device_write_wakeup);
219 
220 /**
221  * serdev_device_write_buf() - write data asynchronously
222  * @serdev:	serdev device
223  * @buf:	data to be written
224  * @count:	number of bytes to write
225  *
226  * Write data to the device asynchronously.
227  *
228  * Note that any accepted data has only been buffered by the controller; use
229  * serdev_device_wait_until_sent() to make sure the controller write buffer
230  * has actually been emptied.
231  *
232  * Return: The number of bytes written (less than count if not enough room in
233  * the write buffer), or a negative errno on errors.
234  */
235 int serdev_device_write_buf(struct serdev_device *serdev,
236 			    const unsigned char *buf, size_t count)
237 {
238 	struct serdev_controller *ctrl = serdev->ctrl;
239 
240 	if (!ctrl || !ctrl->ops->write_buf)
241 		return -EINVAL;
242 
243 	return ctrl->ops->write_buf(ctrl, buf, count);
244 }
245 EXPORT_SYMBOL_GPL(serdev_device_write_buf);
246 
247 /**
248  * serdev_device_write() - write data synchronously
249  * @serdev:	serdev device
250  * @buf:	data to be written
251  * @count:	number of bytes to write
252  * @timeout:	timeout in jiffies, or 0 to wait indefinitely
253  *
254  * Write data to the device synchronously by repeatedly calling
255  * serdev_device_write() until the controller has accepted all data (unless
256  * interrupted by a timeout or a signal).
257  *
258  * Note that any accepted data has only been buffered by the controller; use
259  * serdev_device_wait_until_sent() to make sure the controller write buffer
260  * has actually been emptied.
261  *
262  * Note that this function depends on serdev_device_write_wakeup() being
263  * called in the serdev driver write_wakeup() callback.
264  *
265  * Return: The number of bytes written (less than count if interrupted),
266  * -ETIMEDOUT or -ERESTARTSYS if interrupted before any bytes were written, or
267  * a negative errno on errors.
268  */
269 int serdev_device_write(struct serdev_device *serdev,
270 			const unsigned char *buf, size_t count,
271 			long timeout)
272 {
273 	struct serdev_controller *ctrl = serdev->ctrl;
274 	int written = 0;
275 	int ret;
276 
277 	if (!ctrl || !ctrl->ops->write_buf || !serdev->ops->write_wakeup)
278 		return -EINVAL;
279 
280 	if (timeout == 0)
281 		timeout = MAX_SCHEDULE_TIMEOUT;
282 
283 	mutex_lock(&serdev->write_lock);
284 	do {
285 		reinit_completion(&serdev->write_comp);
286 
287 		ret = ctrl->ops->write_buf(ctrl, buf, count);
288 		if (ret < 0)
289 			break;
290 
291 		written += ret;
292 		buf += ret;
293 		count -= ret;
294 
295 		if (count == 0)
296 			break;
297 
298 		timeout = wait_for_completion_interruptible_timeout(&serdev->write_comp,
299 								    timeout);
300 	} while (timeout > 0);
301 	mutex_unlock(&serdev->write_lock);
302 
303 	if (ret < 0)
304 		return ret;
305 
306 	if (timeout <= 0 && written == 0) {
307 		if (timeout == -ERESTARTSYS)
308 			return -ERESTARTSYS;
309 		else
310 			return -ETIMEDOUT;
311 	}
312 
313 	return written;
314 }
315 EXPORT_SYMBOL_GPL(serdev_device_write);
316 
317 void serdev_device_write_flush(struct serdev_device *serdev)
318 {
319 	struct serdev_controller *ctrl = serdev->ctrl;
320 
321 	if (!ctrl || !ctrl->ops->write_flush)
322 		return;
323 
324 	ctrl->ops->write_flush(ctrl);
325 }
326 EXPORT_SYMBOL_GPL(serdev_device_write_flush);
327 
328 int serdev_device_write_room(struct serdev_device *serdev)
329 {
330 	struct serdev_controller *ctrl = serdev->ctrl;
331 
332 	if (!ctrl || !ctrl->ops->write_room)
333 		return 0;
334 
335 	return serdev->ctrl->ops->write_room(ctrl);
336 }
337 EXPORT_SYMBOL_GPL(serdev_device_write_room);
338 
339 unsigned int serdev_device_set_baudrate(struct serdev_device *serdev, unsigned int speed)
340 {
341 	struct serdev_controller *ctrl = serdev->ctrl;
342 
343 	if (!ctrl || !ctrl->ops->set_baudrate)
344 		return 0;
345 
346 	return ctrl->ops->set_baudrate(ctrl, speed);
347 
348 }
349 EXPORT_SYMBOL_GPL(serdev_device_set_baudrate);
350 
351 void serdev_device_set_flow_control(struct serdev_device *serdev, bool enable)
352 {
353 	struct serdev_controller *ctrl = serdev->ctrl;
354 
355 	if (!ctrl || !ctrl->ops->set_flow_control)
356 		return;
357 
358 	ctrl->ops->set_flow_control(ctrl, enable);
359 }
360 EXPORT_SYMBOL_GPL(serdev_device_set_flow_control);
361 
362 int serdev_device_set_parity(struct serdev_device *serdev,
363 			     enum serdev_parity parity)
364 {
365 	struct serdev_controller *ctrl = serdev->ctrl;
366 
367 	if (!ctrl || !ctrl->ops->set_parity)
368 		return -ENOTSUPP;
369 
370 	return ctrl->ops->set_parity(ctrl, parity);
371 }
372 EXPORT_SYMBOL_GPL(serdev_device_set_parity);
373 
374 void serdev_device_wait_until_sent(struct serdev_device *serdev, long timeout)
375 {
376 	struct serdev_controller *ctrl = serdev->ctrl;
377 
378 	if (!ctrl || !ctrl->ops->wait_until_sent)
379 		return;
380 
381 	ctrl->ops->wait_until_sent(ctrl, timeout);
382 }
383 EXPORT_SYMBOL_GPL(serdev_device_wait_until_sent);
384 
385 int serdev_device_get_tiocm(struct serdev_device *serdev)
386 {
387 	struct serdev_controller *ctrl = serdev->ctrl;
388 
389 	if (!ctrl || !ctrl->ops->get_tiocm)
390 		return -ENOTSUPP;
391 
392 	return ctrl->ops->get_tiocm(ctrl);
393 }
394 EXPORT_SYMBOL_GPL(serdev_device_get_tiocm);
395 
396 int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear)
397 {
398 	struct serdev_controller *ctrl = serdev->ctrl;
399 
400 	if (!ctrl || !ctrl->ops->set_tiocm)
401 		return -ENOTSUPP;
402 
403 	return ctrl->ops->set_tiocm(ctrl, set, clear);
404 }
405 EXPORT_SYMBOL_GPL(serdev_device_set_tiocm);
406 
407 static int serdev_drv_probe(struct device *dev)
408 {
409 	const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver);
410 	int ret;
411 
412 	ret = dev_pm_domain_attach(dev, true);
413 	if (ret)
414 		return ret;
415 
416 	ret = sdrv->probe(to_serdev_device(dev));
417 	if (ret)
418 		dev_pm_domain_detach(dev, true);
419 
420 	return ret;
421 }
422 
423 static int serdev_drv_remove(struct device *dev)
424 {
425 	const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver);
426 	if (sdrv->remove)
427 		sdrv->remove(to_serdev_device(dev));
428 
429 	dev_pm_domain_detach(dev, true);
430 
431 	return 0;
432 }
433 
434 static struct bus_type serdev_bus_type = {
435 	.name		= "serial",
436 	.match		= serdev_device_match,
437 	.probe		= serdev_drv_probe,
438 	.remove		= serdev_drv_remove,
439 };
440 
441 /**
442  * serdev_device_alloc() - Allocate a new serdev device
443  * @ctrl:	associated controller
444  *
445  * Caller is responsible for either calling serdev_device_add() to add the
446  * newly allocated controller, or calling serdev_device_put() to discard it.
447  */
448 struct serdev_device *serdev_device_alloc(struct serdev_controller *ctrl)
449 {
450 	struct serdev_device *serdev;
451 
452 	serdev = kzalloc(sizeof(*serdev), GFP_KERNEL);
453 	if (!serdev)
454 		return NULL;
455 
456 	serdev->ctrl = ctrl;
457 	device_initialize(&serdev->dev);
458 	serdev->dev.parent = &ctrl->dev;
459 	serdev->dev.bus = &serdev_bus_type;
460 	serdev->dev.type = &serdev_device_type;
461 	init_completion(&serdev->write_comp);
462 	mutex_init(&serdev->write_lock);
463 	return serdev;
464 }
465 EXPORT_SYMBOL_GPL(serdev_device_alloc);
466 
467 /**
468  * serdev_controller_alloc() - Allocate a new serdev controller
469  * @parent:	parent device
470  * @size:	size of private data
471  *
472  * Caller is responsible for either calling serdev_controller_add() to add the
473  * newly allocated controller, or calling serdev_controller_put() to discard it.
474  * The allocated private data region may be accessed via
475  * serdev_controller_get_drvdata()
476  */
477 struct serdev_controller *serdev_controller_alloc(struct device *parent,
478 					      size_t size)
479 {
480 	struct serdev_controller *ctrl;
481 	int id;
482 
483 	if (WARN_ON(!parent))
484 		return NULL;
485 
486 	ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL);
487 	if (!ctrl)
488 		return NULL;
489 
490 	id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL);
491 	if (id < 0) {
492 		dev_err(parent,
493 			"unable to allocate serdev controller identifier.\n");
494 		goto err_free;
495 	}
496 
497 	ctrl->nr = id;
498 
499 	device_initialize(&ctrl->dev);
500 	ctrl->dev.type = &serdev_ctrl_type;
501 	ctrl->dev.bus = &serdev_bus_type;
502 	ctrl->dev.parent = parent;
503 	ctrl->dev.of_node = parent->of_node;
504 	serdev_controller_set_drvdata(ctrl, &ctrl[1]);
505 
506 	dev_set_name(&ctrl->dev, "serial%d", id);
507 
508 	pm_runtime_no_callbacks(&ctrl->dev);
509 	pm_suspend_ignore_children(&ctrl->dev, true);
510 
511 	dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id);
512 	return ctrl;
513 
514 err_free:
515 	kfree(ctrl);
516 
517 	return NULL;
518 }
519 EXPORT_SYMBOL_GPL(serdev_controller_alloc);
520 
521 static int of_serdev_register_devices(struct serdev_controller *ctrl)
522 {
523 	struct device_node *node;
524 	struct serdev_device *serdev = NULL;
525 	int err;
526 	bool found = false;
527 
528 	for_each_available_child_of_node(ctrl->dev.of_node, node) {
529 		if (!of_get_property(node, "compatible", NULL))
530 			continue;
531 
532 		dev_dbg(&ctrl->dev, "adding child %pOF\n", node);
533 
534 		serdev = serdev_device_alloc(ctrl);
535 		if (!serdev)
536 			continue;
537 
538 		serdev->dev.of_node = node;
539 
540 		err = serdev_device_add(serdev);
541 		if (err) {
542 			dev_err(&serdev->dev,
543 				"failure adding device. status %d\n", err);
544 			serdev_device_put(serdev);
545 		} else
546 			found = true;
547 	}
548 	if (!found)
549 		return -ENODEV;
550 
551 	return 0;
552 }
553 
554 #ifdef CONFIG_ACPI
555 
556 #define SERDEV_ACPI_MAX_SCAN_DEPTH 32
557 
558 struct acpi_serdev_lookup {
559 	acpi_handle device_handle;
560 	acpi_handle controller_handle;
561 	int n;
562 	int index;
563 };
564 
565 static int acpi_serdev_parse_resource(struct acpi_resource *ares, void *data)
566 {
567 	struct acpi_serdev_lookup *lookup = data;
568 	struct acpi_resource_uart_serialbus *sb;
569 	acpi_status status;
570 
571 	if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
572 		return 1;
573 
574 	if (ares->data.common_serial_bus.type != ACPI_RESOURCE_SERIAL_TYPE_UART)
575 		return 1;
576 
577 	if (lookup->index != -1 && lookup->n++ != lookup->index)
578 		return 1;
579 
580 	sb = &ares->data.uart_serial_bus;
581 
582 	status = acpi_get_handle(lookup->device_handle,
583 				 sb->resource_source.string_ptr,
584 				 &lookup->controller_handle);
585 	if (ACPI_FAILURE(status))
586 		return 1;
587 
588 	/*
589 	 * NOTE: Ideally, we would also want to retreive other properties here,
590 	 * once setting them before opening the device is supported by serdev.
591 	 */
592 
593 	return 1;
594 }
595 
596 static int acpi_serdev_do_lookup(struct acpi_device *adev,
597                                  struct acpi_serdev_lookup *lookup)
598 {
599 	struct list_head resource_list;
600 	int ret;
601 
602 	lookup->device_handle = acpi_device_handle(adev);
603 	lookup->controller_handle = NULL;
604 	lookup->n = 0;
605 
606 	INIT_LIST_HEAD(&resource_list);
607 	ret = acpi_dev_get_resources(adev, &resource_list,
608 				     acpi_serdev_parse_resource, lookup);
609 	acpi_dev_free_resource_list(&resource_list);
610 
611 	if (ret < 0)
612 		return -EINVAL;
613 
614 	return 0;
615 }
616 
617 static int acpi_serdev_check_resources(struct serdev_controller *ctrl,
618 				       struct acpi_device *adev)
619 {
620 	struct acpi_serdev_lookup lookup;
621 	int ret;
622 
623 	if (acpi_bus_get_status(adev) || !adev->status.present)
624 		return -EINVAL;
625 
626 	/* Look for UARTSerialBusV2 resource */
627 	lookup.index = -1;	// we only care for the last device
628 
629 	ret = acpi_serdev_do_lookup(adev, &lookup);
630 	if (ret)
631 		return ret;
632 
633 	/* Make sure controller and ResourceSource handle match */
634 	if (ACPI_HANDLE(ctrl->dev.parent) != lookup.controller_handle)
635 		return -ENODEV;
636 
637 	return 0;
638 }
639 
640 static acpi_status acpi_serdev_register_device(struct serdev_controller *ctrl,
641 					       struct acpi_device *adev)
642 {
643 	struct serdev_device *serdev;
644 	int err;
645 
646 	serdev = serdev_device_alloc(ctrl);
647 	if (!serdev) {
648 		dev_err(&ctrl->dev, "failed to allocate serdev device for %s\n",
649 			dev_name(&adev->dev));
650 		return AE_NO_MEMORY;
651 	}
652 
653 	ACPI_COMPANION_SET(&serdev->dev, adev);
654 	acpi_device_set_enumerated(adev);
655 
656 	err = serdev_device_add(serdev);
657 	if (err) {
658 		dev_err(&serdev->dev,
659 			"failure adding ACPI serdev device. status %d\n", err);
660 		serdev_device_put(serdev);
661 	}
662 
663 	return AE_OK;
664 }
665 
666 static acpi_status acpi_serdev_add_device(acpi_handle handle, u32 level,
667 					  void *data, void **return_value)
668 {
669 	struct serdev_controller *ctrl = data;
670 	struct acpi_device *adev;
671 
672 	if (acpi_bus_get_device(handle, &adev))
673 		return AE_OK;
674 
675 	if (acpi_device_enumerated(adev))
676 		return AE_OK;
677 
678 	if (acpi_serdev_check_resources(ctrl, adev))
679 		return AE_OK;
680 
681 	return acpi_serdev_register_device(ctrl, adev);
682 }
683 
684 
685 static int acpi_serdev_register_devices(struct serdev_controller *ctrl)
686 {
687 	acpi_status status;
688 
689 	if (!has_acpi_companion(ctrl->dev.parent))
690 		return -ENODEV;
691 
692 	status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
693 				     SERDEV_ACPI_MAX_SCAN_DEPTH,
694 				     acpi_serdev_add_device, NULL, ctrl, NULL);
695 	if (ACPI_FAILURE(status))
696 		dev_warn(&ctrl->dev, "failed to enumerate serdev slaves\n");
697 
698 	if (!ctrl->serdev)
699 		return -ENODEV;
700 
701 	return 0;
702 }
703 #else
704 static inline int acpi_serdev_register_devices(struct serdev_controller *ctrl)
705 {
706 	return -ENODEV;
707 }
708 #endif /* CONFIG_ACPI */
709 
710 /**
711  * serdev_controller_add() - Add an serdev controller
712  * @ctrl:	controller to be registered.
713  *
714  * Register a controller previously allocated via serdev_controller_alloc() with
715  * the serdev core.
716  */
717 int serdev_controller_add(struct serdev_controller *ctrl)
718 {
719 	int ret_of, ret_acpi, ret;
720 
721 	/* Can't register until after driver model init */
722 	if (WARN_ON(!is_registered))
723 		return -EAGAIN;
724 
725 	ret = device_add(&ctrl->dev);
726 	if (ret)
727 		return ret;
728 
729 	pm_runtime_enable(&ctrl->dev);
730 
731 	ret_of = of_serdev_register_devices(ctrl);
732 	ret_acpi = acpi_serdev_register_devices(ctrl);
733 	if (ret_of && ret_acpi) {
734 		dev_dbg(&ctrl->dev, "no devices registered: of:%d acpi:%d\n",
735 			ret_of, ret_acpi);
736 		ret = -ENODEV;
737 		goto err_rpm_disable;
738 	}
739 
740 	dev_dbg(&ctrl->dev, "serdev%d registered: dev:%p\n",
741 		ctrl->nr, &ctrl->dev);
742 	return 0;
743 
744 err_rpm_disable:
745 	pm_runtime_disable(&ctrl->dev);
746 	device_del(&ctrl->dev);
747 	return ret;
748 };
749 EXPORT_SYMBOL_GPL(serdev_controller_add);
750 
751 /* Remove a device associated with a controller */
752 static int serdev_remove_device(struct device *dev, void *data)
753 {
754 	struct serdev_device *serdev = to_serdev_device(dev);
755 	if (dev->type == &serdev_device_type)
756 		serdev_device_remove(serdev);
757 	return 0;
758 }
759 
760 /**
761  * serdev_controller_remove(): remove an serdev controller
762  * @ctrl:	controller to remove
763  *
764  * Remove a serdev controller.  Caller is responsible for calling
765  * serdev_controller_put() to discard the allocated controller.
766  */
767 void serdev_controller_remove(struct serdev_controller *ctrl)
768 {
769 	int dummy;
770 
771 	if (!ctrl)
772 		return;
773 
774 	dummy = device_for_each_child(&ctrl->dev, NULL,
775 				      serdev_remove_device);
776 	pm_runtime_disable(&ctrl->dev);
777 	device_del(&ctrl->dev);
778 }
779 EXPORT_SYMBOL_GPL(serdev_controller_remove);
780 
781 /**
782  * serdev_driver_register() - Register client driver with serdev core
783  * @sdrv:	client driver to be associated with client-device.
784  *
785  * This API will register the client driver with the serdev framework.
786  * It is typically called from the driver's module-init function.
787  */
788 int __serdev_device_driver_register(struct serdev_device_driver *sdrv, struct module *owner)
789 {
790 	sdrv->driver.bus = &serdev_bus_type;
791 	sdrv->driver.owner = owner;
792 
793 	/* force drivers to async probe so I/O is possible in probe */
794         sdrv->driver.probe_type = PROBE_PREFER_ASYNCHRONOUS;
795 
796 	return driver_register(&sdrv->driver);
797 }
798 EXPORT_SYMBOL_GPL(__serdev_device_driver_register);
799 
800 static void __exit serdev_exit(void)
801 {
802 	bus_unregister(&serdev_bus_type);
803 	ida_destroy(&ctrl_ida);
804 }
805 module_exit(serdev_exit);
806 
807 static int __init serdev_init(void)
808 {
809 	int ret;
810 
811 	ret = bus_register(&serdev_bus_type);
812 	if (ret)
813 		return ret;
814 
815 	is_registered = true;
816 	return 0;
817 }
818 /* Must be before serial drivers register */
819 postcore_initcall(serdev_init);
820 
821 MODULE_AUTHOR("Rob Herring <robh@kernel.org>");
822 MODULE_LICENSE("GPL v2");
823 MODULE_DESCRIPTION("Serial attached device bus");
824