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