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