Linux Audio

Check our new training course

Loading...
v5.4
  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
 22static bool is_registered;
 23static DEFINE_IDA(ctrl_ida);
 24
 25static 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}
 36static DEVICE_ATTR_RO(modalias);
 37
 38static struct attribute *serdev_device_attrs[] = {
 39	&dev_attr_modalias.attr,
 40	NULL,
 41};
 42ATTRIBUTE_GROUPS(serdev_device);
 43
 44static 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
 57static void serdev_device_release(struct device *dev)
 58{
 59	struct serdev_device *serdev = to_serdev_device(dev);
 60	kfree(serdev);
 61}
 62
 63static const struct device_type serdev_device_type = {
 64	.groups		= serdev_device_groups,
 65	.uevent		= serdev_device_uevent,
 66	.release	= serdev_device_release,
 67};
 68
 69static bool is_serdev_device(const struct device *dev)
 70{
 71	return dev->type == &serdev_device_type;
 72}
 73
 74static 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
 81static const struct device_type serdev_ctrl_type = {
 82	.release	= serdev_ctrl_release,
 83};
 84
 85static 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 */
101int 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
127err_clear_serdev:
128	ctrl->serdev = NULL;
129	return err;
130}
131EXPORT_SYMBOL_GPL(serdev_device_add);
132
133/**
134 * serdev_device_remove(): remove an serdev device
135 * @serdev:	serdev_device to be removed
136 */
137void 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}
144EXPORT_SYMBOL_GPL(serdev_device_remove);
145
146int 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
166err_close:
167	if (ctrl->ops->close)
168		ctrl->ops->close(ctrl);
169
170	return ret;
171}
172EXPORT_SYMBOL_GPL(serdev_device_open);
173
174void 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}
185EXPORT_SYMBOL_GPL(serdev_device_close);
186
187static void devm_serdev_device_release(struct device *dev, void *dr)
188{
189	serdev_device_close(*(struct serdev_device **)dr);
190}
191
192int 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}
212EXPORT_SYMBOL_GPL(devm_serdev_device_open);
213
214void serdev_device_write_wakeup(struct serdev_device *serdev)
215{
216	complete(&serdev->write_comp);
217}
218EXPORT_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 */
235int 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}
245EXPORT_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 */
269int 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}
315EXPORT_SYMBOL_GPL(serdev_device_write);
316
317void 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}
326EXPORT_SYMBOL_GPL(serdev_device_write_flush);
327
328int 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}
337EXPORT_SYMBOL_GPL(serdev_device_write_room);
338
339unsigned 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}
349EXPORT_SYMBOL_GPL(serdev_device_set_baudrate);
350
351void 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}
360EXPORT_SYMBOL_GPL(serdev_device_set_flow_control);
361
362int 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}
372EXPORT_SYMBOL_GPL(serdev_device_set_parity);
373
374void 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}
383EXPORT_SYMBOL_GPL(serdev_device_wait_until_sent);
384
385int 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}
394EXPORT_SYMBOL_GPL(serdev_device_get_tiocm);
395
396int 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}
405EXPORT_SYMBOL_GPL(serdev_device_set_tiocm);
406
407static 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
423static 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
434static 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 */
448struct 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}
465EXPORT_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 */
477struct 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
514err_free:
515	kfree(ctrl);
516
517	return NULL;
518}
519EXPORT_SYMBOL_GPL(serdev_controller_alloc);
520
521static 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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
555static acpi_status acpi_serdev_register_device(struct serdev_controller *ctrl,
556					    struct acpi_device *adev)
557{
558	struct serdev_device *serdev = NULL;
559	int err;
560
561	if (acpi_bus_get_status(adev) || !adev->status.present ||
562	    acpi_device_enumerated(adev))
563		return AE_OK;
564
565	serdev = serdev_device_alloc(ctrl);
566	if (!serdev) {
567		dev_err(&ctrl->dev, "failed to allocate serdev device for %s\n",
568			dev_name(&adev->dev));
569		return AE_NO_MEMORY;
570	}
571
572	ACPI_COMPANION_SET(&serdev->dev, adev);
573	acpi_device_set_enumerated(adev);
574
575	err = serdev_device_add(serdev);
576	if (err) {
577		dev_err(&serdev->dev,
578			"failure adding ACPI serdev device. status %d\n", err);
 
579		serdev_device_put(serdev);
580	}
581
582	return AE_OK;
583}
584
 
 
 
 
 
 
585static acpi_status acpi_serdev_add_device(acpi_handle handle, u32 level,
586				       void *data, void **return_value)
587{
 
588	struct serdev_controller *ctrl = data;
589	struct acpi_device *adev;
590
591	if (acpi_bus_get_device(handle, &adev))
 
 
 
 
 
 
 
592		return AE_OK;
593
594	return acpi_serdev_register_device(ctrl, adev);
595}
596
 
597static int acpi_serdev_register_devices(struct serdev_controller *ctrl)
598{
599	acpi_status status;
600	acpi_handle handle;
 
601
602	handle = ACPI_HANDLE(ctrl->dev.parent);
603	if (!handle)
604		return -ENODEV;
605
606	status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
 
 
 
 
 
 
 
 
 
 
 
 
 
607				     acpi_serdev_add_device, NULL, ctrl, NULL);
608	if (ACPI_FAILURE(status))
609		dev_dbg(&ctrl->dev, "failed to enumerate serdev slaves\n");
610
611	if (!ctrl->serdev)
612		return -ENODEV;
613
614	return 0;
615}
616#else
617static inline int acpi_serdev_register_devices(struct serdev_controller *ctrl)
618{
619	return -ENODEV;
620}
621#endif /* CONFIG_ACPI */
622
623/**
624 * serdev_controller_add() - Add an serdev controller
625 * @ctrl:	controller to be registered.
626 *
627 * Register a controller previously allocated via serdev_controller_alloc() with
628 * the serdev core.
629 */
630int serdev_controller_add(struct serdev_controller *ctrl)
631{
632	int ret_of, ret_acpi, ret;
633
634	/* Can't register until after driver model init */
635	if (WARN_ON(!is_registered))
636		return -EAGAIN;
637
638	ret = device_add(&ctrl->dev);
639	if (ret)
640		return ret;
641
642	pm_runtime_enable(&ctrl->dev);
643
644	ret_of = of_serdev_register_devices(ctrl);
645	ret_acpi = acpi_serdev_register_devices(ctrl);
646	if (ret_of && ret_acpi) {
647		dev_dbg(&ctrl->dev, "no devices registered: of:%d acpi:%d\n",
648			ret_of, ret_acpi);
649		ret = -ENODEV;
650		goto err_rpm_disable;
651	}
652
653	dev_dbg(&ctrl->dev, "serdev%d registered: dev:%p\n",
654		ctrl->nr, &ctrl->dev);
655	return 0;
656
657err_rpm_disable:
658	pm_runtime_disable(&ctrl->dev);
659	device_del(&ctrl->dev);
660	return ret;
661};
662EXPORT_SYMBOL_GPL(serdev_controller_add);
663
664/* Remove a device associated with a controller */
665static int serdev_remove_device(struct device *dev, void *data)
666{
667	struct serdev_device *serdev = to_serdev_device(dev);
668	if (dev->type == &serdev_device_type)
669		serdev_device_remove(serdev);
670	return 0;
671}
672
673/**
674 * serdev_controller_remove(): remove an serdev controller
675 * @ctrl:	controller to remove
676 *
677 * Remove a serdev controller.  Caller is responsible for calling
678 * serdev_controller_put() to discard the allocated controller.
679 */
680void serdev_controller_remove(struct serdev_controller *ctrl)
681{
682	int dummy;
683
684	if (!ctrl)
685		return;
686
687	dummy = device_for_each_child(&ctrl->dev, NULL,
688				      serdev_remove_device);
689	pm_runtime_disable(&ctrl->dev);
690	device_del(&ctrl->dev);
691}
692EXPORT_SYMBOL_GPL(serdev_controller_remove);
693
694/**
695 * serdev_driver_register() - Register client driver with serdev core
696 * @sdrv:	client driver to be associated with client-device.
 
697 *
698 * This API will register the client driver with the serdev framework.
699 * It is typically called from the driver's module-init function.
700 */
701int __serdev_device_driver_register(struct serdev_device_driver *sdrv, struct module *owner)
702{
703	sdrv->driver.bus = &serdev_bus_type;
704	sdrv->driver.owner = owner;
705
706	/* force drivers to async probe so I/O is possible in probe */
707        sdrv->driver.probe_type = PROBE_PREFER_ASYNCHRONOUS;
708
709	return driver_register(&sdrv->driver);
710}
711EXPORT_SYMBOL_GPL(__serdev_device_driver_register);
712
713static void __exit serdev_exit(void)
714{
715	bus_unregister(&serdev_bus_type);
716	ida_destroy(&ctrl_ida);
717}
718module_exit(serdev_exit);
719
720static int __init serdev_init(void)
721{
722	int ret;
723
724	ret = bus_register(&serdev_bus_type);
725	if (ret)
726		return ret;
727
728	is_registered = true;
729	return 0;
730}
731/* Must be before serial drivers register */
732postcore_initcall(serdev_init);
733
734MODULE_AUTHOR("Rob Herring <robh@kernel.org>");
735MODULE_LICENSE("GPL v2");
736MODULE_DESCRIPTION("Serial attached device bus");
v6.2
  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
 23static bool is_registered;
 24static DEFINE_IDA(ctrl_ida);
 25
 26static 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}
 37static DEVICE_ATTR_RO(modalias);
 38
 39static struct attribute *serdev_device_attrs[] = {
 40	&dev_attr_modalias.attr,
 41	NULL,
 42};
 43ATTRIBUTE_GROUPS(serdev_device);
 44
 45static int serdev_device_uevent(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
 58static void serdev_device_release(struct device *dev)
 59{
 60	struct serdev_device *serdev = to_serdev_device(dev);
 61	kfree(serdev);
 62}
 63
 64static const struct device_type serdev_device_type = {
 65	.groups		= serdev_device_groups,
 66	.uevent		= serdev_device_uevent,
 67	.release	= serdev_device_release,
 68};
 69
 70static bool is_serdev_device(const struct device *dev)
 71{
 72	return dev->type == &serdev_device_type;
 73}
 74
 75static 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
 82static const struct device_type serdev_ctrl_type = {
 83	.release	= serdev_ctrl_release,
 84};
 85
 86static 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 */
102int 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
128err_clear_serdev:
129	ctrl->serdev = NULL;
130	return err;
131}
132EXPORT_SYMBOL_GPL(serdev_device_add);
133
134/**
135 * serdev_device_remove(): remove an serdev device
136 * @serdev:	serdev_device to be removed
137 */
138void 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}
145EXPORT_SYMBOL_GPL(serdev_device_remove);
146
147int 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
167err_close:
168	if (ctrl->ops->close)
169		ctrl->ops->close(ctrl);
170
171	return ret;
172}
173EXPORT_SYMBOL_GPL(serdev_device_open);
174
175void 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}
186EXPORT_SYMBOL_GPL(serdev_device_close);
187
188static void devm_serdev_device_release(struct device *dev, void *dr)
189{
190	serdev_device_close(*(struct serdev_device **)dr);
191}
192
193int 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}
213EXPORT_SYMBOL_GPL(devm_serdev_device_open);
214
215void serdev_device_write_wakeup(struct serdev_device *serdev)
216{
217	complete(&serdev->write_comp);
218}
219EXPORT_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 */
236int 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}
246EXPORT_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 */
270int 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}
316EXPORT_SYMBOL_GPL(serdev_device_write);
317
318void 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}
327EXPORT_SYMBOL_GPL(serdev_device_write_flush);
328
329int 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}
338EXPORT_SYMBOL_GPL(serdev_device_write_room);
339
340unsigned 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}
350EXPORT_SYMBOL_GPL(serdev_device_set_baudrate);
351
352void 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}
361EXPORT_SYMBOL_GPL(serdev_device_set_flow_control);
362
363int 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 -ENOTSUPP;
370
371	return ctrl->ops->set_parity(ctrl, parity);
372}
373EXPORT_SYMBOL_GPL(serdev_device_set_parity);
374
375void 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}
384EXPORT_SYMBOL_GPL(serdev_device_wait_until_sent);
385
386int 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 -ENOTSUPP;
392
393	return ctrl->ops->get_tiocm(ctrl);
394}
395EXPORT_SYMBOL_GPL(serdev_device_get_tiocm);
396
397int 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 -ENOTSUPP;
403
404	return ctrl->ops->set_tiocm(ctrl, set, clear);
405}
406EXPORT_SYMBOL_GPL(serdev_device_set_tiocm);
407
408static int serdev_drv_probe(struct device *dev)
409{
410	const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver);
411	int ret;
412
413	ret = dev_pm_domain_attach(dev, true);
414	if (ret)
415		return ret;
416
417	ret = sdrv->probe(to_serdev_device(dev));
418	if (ret)
419		dev_pm_domain_detach(dev, true);
420
421	return ret;
422}
423
424static void serdev_drv_remove(struct device *dev)
425{
426	const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver);
427	if (sdrv->remove)
428		sdrv->remove(to_serdev_device(dev));
429
430	dev_pm_domain_detach(dev, true);
 
 
431}
432
433static struct bus_type serdev_bus_type = {
434	.name		= "serial",
435	.match		= serdev_device_match,
436	.probe		= serdev_drv_probe,
437	.remove		= serdev_drv_remove,
438};
439
440/**
441 * serdev_device_alloc() - Allocate a new serdev device
442 * @ctrl:	associated controller
443 *
444 * Caller is responsible for either calling serdev_device_add() to add the
445 * newly allocated controller, or calling serdev_device_put() to discard it.
446 */
447struct serdev_device *serdev_device_alloc(struct serdev_controller *ctrl)
448{
449	struct serdev_device *serdev;
450
451	serdev = kzalloc(sizeof(*serdev), GFP_KERNEL);
452	if (!serdev)
453		return NULL;
454
455	serdev->ctrl = ctrl;
456	device_initialize(&serdev->dev);
457	serdev->dev.parent = &ctrl->dev;
458	serdev->dev.bus = &serdev_bus_type;
459	serdev->dev.type = &serdev_device_type;
460	init_completion(&serdev->write_comp);
461	mutex_init(&serdev->write_lock);
462	return serdev;
463}
464EXPORT_SYMBOL_GPL(serdev_device_alloc);
465
466/**
467 * serdev_controller_alloc() - Allocate a new serdev controller
468 * @parent:	parent device
469 * @size:	size of private data
470 *
471 * Caller is responsible for either calling serdev_controller_add() to add the
472 * newly allocated controller, or calling serdev_controller_put() to discard it.
473 * The allocated private data region may be accessed via
474 * serdev_controller_get_drvdata()
475 */
476struct serdev_controller *serdev_controller_alloc(struct device *parent,
477					      size_t size)
478{
479	struct serdev_controller *ctrl;
480	int id;
481
482	if (WARN_ON(!parent))
483		return NULL;
484
485	ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL);
486	if (!ctrl)
487		return NULL;
488
489	id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL);
490	if (id < 0) {
491		dev_err(parent,
492			"unable to allocate serdev controller identifier.\n");
493		goto err_free;
494	}
495
496	ctrl->nr = id;
497
498	device_initialize(&ctrl->dev);
499	ctrl->dev.type = &serdev_ctrl_type;
500	ctrl->dev.bus = &serdev_bus_type;
501	ctrl->dev.parent = parent;
502	ctrl->dev.of_node = parent->of_node;
503	serdev_controller_set_drvdata(ctrl, &ctrl[1]);
504
505	dev_set_name(&ctrl->dev, "serial%d", id);
506
507	pm_runtime_no_callbacks(&ctrl->dev);
508	pm_suspend_ignore_children(&ctrl->dev, true);
509
510	dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id);
511	return ctrl;
512
513err_free:
514	kfree(ctrl);
515
516	return NULL;
517}
518EXPORT_SYMBOL_GPL(serdev_controller_alloc);
519
520static int of_serdev_register_devices(struct serdev_controller *ctrl)
521{
522	struct device_node *node;
523	struct serdev_device *serdev = NULL;
524	int err;
525	bool found = false;
526
527	for_each_available_child_of_node(ctrl->dev.of_node, node) {
528		if (!of_get_property(node, "compatible", NULL))
529			continue;
530
531		dev_dbg(&ctrl->dev, "adding child %pOF\n", node);
532
533		serdev = serdev_device_alloc(ctrl);
534		if (!serdev)
535			continue;
536
537		serdev->dev.of_node = node;
538
539		err = serdev_device_add(serdev);
540		if (err) {
541			dev_err(&serdev->dev,
542				"failure adding device. status %pe\n",
543				ERR_PTR(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
558struct acpi_serdev_lookup {
559	acpi_handle device_handle;
560	acpi_handle controller_handle;
561	int n;
562	int index;
563};
564
565/**
566 * serdev_acpi_get_uart_resource - Gets UARTSerialBus resource if type matches
567 * @ares:	ACPI resource
568 * @uart:	Pointer to UARTSerialBus resource will be returned here
569 *
570 * Checks if the given ACPI resource is of type UARTSerialBus.
571 * In this case, returns a pointer to it to the caller.
572 *
573 * Return: True if resource type is of UARTSerialBus, otherwise false.
574 */
575bool serdev_acpi_get_uart_resource(struct acpi_resource *ares,
576				   struct acpi_resource_uart_serialbus **uart)
577{
578	struct acpi_resource_uart_serialbus *sb;
579
580	if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
581		return false;
582
583	sb = &ares->data.uart_serial_bus;
584	if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_UART)
585		return false;
586
587	*uart = sb;
588	return true;
589}
590EXPORT_SYMBOL_GPL(serdev_acpi_get_uart_resource);
591
592static int acpi_serdev_parse_resource(struct acpi_resource *ares, void *data)
593{
594	struct acpi_serdev_lookup *lookup = data;
595	struct acpi_resource_uart_serialbus *sb;
596	acpi_status status;
597
598	if (!serdev_acpi_get_uart_resource(ares, &sb))
599		return 1;
600
601	if (lookup->index != -1 && lookup->n++ != lookup->index)
602		return 1;
603
604	status = acpi_get_handle(lookup->device_handle,
605				 sb->resource_source.string_ptr,
606				 &lookup->controller_handle);
607	if (ACPI_FAILURE(status))
608		return 1;
609
610	/*
611	 * NOTE: Ideally, we would also want to retrieve other properties here,
612	 * once setting them before opening the device is supported by serdev.
613	 */
614
615	return 1;
616}
617
618static int acpi_serdev_do_lookup(struct acpi_device *adev,
619                                 struct acpi_serdev_lookup *lookup)
620{
621	struct list_head resource_list;
622	int ret;
623
624	lookup->device_handle = acpi_device_handle(adev);
625	lookup->controller_handle = NULL;
626	lookup->n = 0;
627
628	INIT_LIST_HEAD(&resource_list);
629	ret = acpi_dev_get_resources(adev, &resource_list,
630				     acpi_serdev_parse_resource, lookup);
631	acpi_dev_free_resource_list(&resource_list);
632
633	if (ret < 0)
634		return -EINVAL;
635
636	return 0;
637}
638
639static int acpi_serdev_check_resources(struct serdev_controller *ctrl,
640				       struct acpi_device *adev)
641{
642	struct acpi_serdev_lookup lookup;
643	int ret;
644
645	if (acpi_bus_get_status(adev) || !adev->status.present)
646		return -EINVAL;
647
648	/* Look for UARTSerialBusV2 resource */
649	lookup.index = -1;	// we only care for the last device
650
651	ret = acpi_serdev_do_lookup(adev, &lookup);
652	if (ret)
653		return ret;
654
655	/*
656	 * Apple machines provide an empty resource template, so on those
657	 * machines just look for immediate children with a "baud" property
658	 * (from the _DSM method) instead.
659	 */
660	if (!lookup.controller_handle && x86_apple_machine &&
661	    !acpi_dev_get_property(adev, "baud", ACPI_TYPE_BUFFER, NULL))
662		acpi_get_parent(adev->handle, &lookup.controller_handle);
663
664	/* Make sure controller and ResourceSource handle match */
665	if (ACPI_HANDLE(ctrl->dev.parent) != lookup.controller_handle)
666		return -ENODEV;
667
668	return 0;
669}
670
671static acpi_status acpi_serdev_register_device(struct serdev_controller *ctrl,
672					       struct acpi_device *adev)
673{
674	struct serdev_device *serdev;
675	int err;
676
 
 
 
 
677	serdev = serdev_device_alloc(ctrl);
678	if (!serdev) {
679		dev_err(&ctrl->dev, "failed to allocate serdev device for %s\n",
680			dev_name(&adev->dev));
681		return AE_NO_MEMORY;
682	}
683
684	ACPI_COMPANION_SET(&serdev->dev, adev);
685	acpi_device_set_enumerated(adev);
686
687	err = serdev_device_add(serdev);
688	if (err) {
689		dev_err(&serdev->dev,
690			"failure adding ACPI serdev device. status %pe\n",
691			ERR_PTR(err));
692		serdev_device_put(serdev);
693	}
694
695	return AE_OK;
696}
697
698static const struct acpi_device_id serdev_acpi_devices_blacklist[] = {
699	{ "INT3511", 0 },
700	{ "INT3512", 0 },
701	{ },
702};
703
704static acpi_status acpi_serdev_add_device(acpi_handle handle, u32 level,
705					  void *data, void **return_value)
706{
707	struct acpi_device *adev = acpi_fetch_acpi_dev(handle);
708	struct serdev_controller *ctrl = data;
 
709
710	if (!adev || acpi_device_enumerated(adev))
711		return AE_OK;
712
713	/* Skip if black listed */
714	if (!acpi_match_device_ids(adev, serdev_acpi_devices_blacklist))
715		return AE_OK;
716
717	if (acpi_serdev_check_resources(ctrl, adev))
718		return AE_OK;
719
720	return acpi_serdev_register_device(ctrl, adev);
721}
722
723
724static int acpi_serdev_register_devices(struct serdev_controller *ctrl)
725{
726	acpi_status status;
727	bool skip;
728	int ret;
729
730	if (!has_acpi_companion(ctrl->dev.parent))
 
731		return -ENODEV;
732
733	/*
734	 * Skip registration on boards where the ACPI tables are known to
735	 * contain buggy devices. Note serdev_controller_add() must still
736	 * succeed in this case, so that the proper serdev devices can be
737	 * added "manually" later.
738	 */
739	ret = acpi_quirk_skip_serdev_enumeration(ctrl->dev.parent, &skip);
740	if (ret)
741		return ret;
742	if (skip)
743		return 0;
744
745	status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
746				     SERDEV_ACPI_MAX_SCAN_DEPTH,
747				     acpi_serdev_add_device, NULL, ctrl, NULL);
748	if (ACPI_FAILURE(status))
749		dev_warn(&ctrl->dev, "failed to enumerate serdev slaves\n");
750
751	if (!ctrl->serdev)
752		return -ENODEV;
753
754	return 0;
755}
756#else
757static inline int acpi_serdev_register_devices(struct serdev_controller *ctrl)
758{
759	return -ENODEV;
760}
761#endif /* CONFIG_ACPI */
762
763/**
764 * serdev_controller_add() - Add an serdev controller
765 * @ctrl:	controller to be registered.
766 *
767 * Register a controller previously allocated via serdev_controller_alloc() with
768 * the serdev core.
769 */
770int serdev_controller_add(struct serdev_controller *ctrl)
771{
772	int ret_of, ret_acpi, ret;
773
774	/* Can't register until after driver model init */
775	if (WARN_ON(!is_registered))
776		return -EAGAIN;
777
778	ret = device_add(&ctrl->dev);
779	if (ret)
780		return ret;
781
782	pm_runtime_enable(&ctrl->dev);
783
784	ret_of = of_serdev_register_devices(ctrl);
785	ret_acpi = acpi_serdev_register_devices(ctrl);
786	if (ret_of && ret_acpi) {
787		dev_dbg(&ctrl->dev, "no devices registered: of:%pe acpi:%pe\n",
788			ERR_PTR(ret_of), ERR_PTR(ret_acpi));
789		ret = -ENODEV;
790		goto err_rpm_disable;
791	}
792
793	dev_dbg(&ctrl->dev, "serdev%d registered: dev:%p\n",
794		ctrl->nr, &ctrl->dev);
795	return 0;
796
797err_rpm_disable:
798	pm_runtime_disable(&ctrl->dev);
799	device_del(&ctrl->dev);
800	return ret;
801};
802EXPORT_SYMBOL_GPL(serdev_controller_add);
803
804/* Remove a device associated with a controller */
805static int serdev_remove_device(struct device *dev, void *data)
806{
807	struct serdev_device *serdev = to_serdev_device(dev);
808	if (dev->type == &serdev_device_type)
809		serdev_device_remove(serdev);
810	return 0;
811}
812
813/**
814 * serdev_controller_remove(): remove an serdev controller
815 * @ctrl:	controller to remove
816 *
817 * Remove a serdev controller.  Caller is responsible for calling
818 * serdev_controller_put() to discard the allocated controller.
819 */
820void serdev_controller_remove(struct serdev_controller *ctrl)
821{
 
 
822	if (!ctrl)
823		return;
824
825	device_for_each_child(&ctrl->dev, NULL, serdev_remove_device);
 
826	pm_runtime_disable(&ctrl->dev);
827	device_del(&ctrl->dev);
828}
829EXPORT_SYMBOL_GPL(serdev_controller_remove);
830
831/**
832 * __serdev_device_driver_register() - Register client driver with serdev core
833 * @sdrv:	client driver to be associated with client-device.
834 * @owner:	client driver owner to set.
835 *
836 * This API will register the client driver with the serdev framework.
837 * It is typically called from the driver's module-init function.
838 */
839int __serdev_device_driver_register(struct serdev_device_driver *sdrv, struct module *owner)
840{
841	sdrv->driver.bus = &serdev_bus_type;
842	sdrv->driver.owner = owner;
843
844	/* force drivers to async probe so I/O is possible in probe */
845        sdrv->driver.probe_type = PROBE_PREFER_ASYNCHRONOUS;
846
847	return driver_register(&sdrv->driver);
848}
849EXPORT_SYMBOL_GPL(__serdev_device_driver_register);
850
851static void __exit serdev_exit(void)
852{
853	bus_unregister(&serdev_bus_type);
854	ida_destroy(&ctrl_ida);
855}
856module_exit(serdev_exit);
857
858static int __init serdev_init(void)
859{
860	int ret;
861
862	ret = bus_register(&serdev_bus_type);
863	if (ret)
864		return ret;
865
866	is_registered = true;
867	return 0;
868}
869/* Must be before serial drivers register */
870postcore_initcall(serdev_init);
871
872MODULE_AUTHOR("Rob Herring <robh@kernel.org>");
873MODULE_LICENSE("GPL v2");
874MODULE_DESCRIPTION("Serial attached device bus");