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");
v4.17
  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/serdev.h>
 17#include <linux/slab.h>
 18
 19static bool is_registered;
 20static DEFINE_IDA(ctrl_ida);
 21
 22static ssize_t modalias_show(struct device *dev,
 23			     struct device_attribute *attr, char *buf)
 24{
 25	int len;
 26
 27	len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
 28	if (len != -ENODEV)
 29		return len;
 30
 31	return of_device_modalias(dev, buf, PAGE_SIZE);
 32}
 33static DEVICE_ATTR_RO(modalias);
 34
 35static struct attribute *serdev_device_attrs[] = {
 36	&dev_attr_modalias.attr,
 37	NULL,
 38};
 39ATTRIBUTE_GROUPS(serdev_device);
 40
 41static int serdev_device_uevent(struct device *dev, struct kobj_uevent_env *env)
 42{
 43	int rc;
 44
 45	/* TODO: platform modalias */
 46
 47	rc = acpi_device_uevent_modalias(dev, env);
 48	if (rc != -ENODEV)
 49		return rc;
 50
 51	return of_device_uevent_modalias(dev, env);
 52}
 53
 54static void serdev_device_release(struct device *dev)
 55{
 56	struct serdev_device *serdev = to_serdev_device(dev);
 57	kfree(serdev);
 58}
 59
 60static const struct device_type serdev_device_type = {
 61	.groups		= serdev_device_groups,
 62	.uevent		= serdev_device_uevent,
 63	.release	= serdev_device_release,
 64};
 65
 66static bool is_serdev_device(const struct device *dev)
 67{
 68	return dev->type == &serdev_device_type;
 69}
 70
 71static void serdev_ctrl_release(struct device *dev)
 72{
 73	struct serdev_controller *ctrl = to_serdev_controller(dev);
 74	ida_simple_remove(&ctrl_ida, ctrl->nr);
 75	kfree(ctrl);
 76}
 77
 78static const struct device_type serdev_ctrl_type = {
 79	.release	= serdev_ctrl_release,
 80};
 81
 82static int serdev_device_match(struct device *dev, struct device_driver *drv)
 83{
 84	if (!is_serdev_device(dev))
 85		return 0;
 86
 87	/* TODO: platform matching */
 88	if (acpi_driver_match_device(dev, drv))
 89		return 1;
 90
 91	return of_driver_match_device(dev, drv);
 92}
 93
 94/**
 95 * serdev_device_add() - add a device previously constructed via serdev_device_alloc()
 96 * @serdev:	serdev_device to be added
 97 */
 98int serdev_device_add(struct serdev_device *serdev)
 99{
100	struct serdev_controller *ctrl = serdev->ctrl;
101	struct device *parent = serdev->dev.parent;
102	int err;
103
104	dev_set_name(&serdev->dev, "%s-%d", dev_name(parent), serdev->nr);
105
106	/* Only a single slave device is currently supported. */
107	if (ctrl->serdev) {
108		dev_err(&serdev->dev, "controller busy\n");
109		return -EBUSY;
110	}
111	ctrl->serdev = serdev;
112
113	err = device_add(&serdev->dev);
114	if (err < 0) {
115		dev_err(&serdev->dev, "Can't add %s, status %d\n",
116			dev_name(&serdev->dev), err);
117		goto err_clear_serdev;
118	}
119
120	dev_dbg(&serdev->dev, "device %s registered\n", dev_name(&serdev->dev));
121
122	return 0;
123
124err_clear_serdev:
125	ctrl->serdev = NULL;
126	return err;
127}
128EXPORT_SYMBOL_GPL(serdev_device_add);
129
130/**
131 * serdev_device_remove(): remove an serdev device
132 * @serdev:	serdev_device to be removed
133 */
134void serdev_device_remove(struct serdev_device *serdev)
135{
136	struct serdev_controller *ctrl = serdev->ctrl;
137
138	device_unregister(&serdev->dev);
139	ctrl->serdev = NULL;
140}
141EXPORT_SYMBOL_GPL(serdev_device_remove);
142
143int serdev_device_open(struct serdev_device *serdev)
144{
145	struct serdev_controller *ctrl = serdev->ctrl;
 
146
147	if (!ctrl || !ctrl->ops->open)
148		return -EINVAL;
149
150	return ctrl->ops->open(ctrl);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151}
152EXPORT_SYMBOL_GPL(serdev_device_open);
153
154void serdev_device_close(struct serdev_device *serdev)
155{
156	struct serdev_controller *ctrl = serdev->ctrl;
157
158	if (!ctrl || !ctrl->ops->close)
159		return;
160
 
 
161	ctrl->ops->close(ctrl);
162}
163EXPORT_SYMBOL_GPL(serdev_device_close);
164
165static void devm_serdev_device_release(struct device *dev, void *dr)
166{
167	serdev_device_close(*(struct serdev_device **)dr);
168}
169
170int devm_serdev_device_open(struct device *dev, struct serdev_device *serdev)
171{
172	struct serdev_device **dr;
173	int ret;
174
175	dr = devres_alloc(devm_serdev_device_release, sizeof(*dr), GFP_KERNEL);
176	if (!dr)
177		return -ENOMEM;
178
179	ret = serdev_device_open(serdev);
180	if (ret) {
181		devres_free(dr);
182		return ret;
183	}
184
185	*dr = serdev;
186	devres_add(dev, dr);
187
188	return 0;
189}
190EXPORT_SYMBOL_GPL(devm_serdev_device_open);
191
192void serdev_device_write_wakeup(struct serdev_device *serdev)
193{
194	complete(&serdev->write_comp);
195}
196EXPORT_SYMBOL_GPL(serdev_device_write_wakeup);
197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198int serdev_device_write_buf(struct serdev_device *serdev,
199			    const unsigned char *buf, size_t count)
200{
201	struct serdev_controller *ctrl = serdev->ctrl;
202
203	if (!ctrl || !ctrl->ops->write_buf)
204		return -EINVAL;
205
206	return ctrl->ops->write_buf(ctrl, buf, count);
207}
208EXPORT_SYMBOL_GPL(serdev_device_write_buf);
209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210int serdev_device_write(struct serdev_device *serdev,
211			const unsigned char *buf, size_t count,
212			unsigned long timeout)
213{
214	struct serdev_controller *ctrl = serdev->ctrl;
 
215	int ret;
216
217	if (!ctrl || !ctrl->ops->write_buf ||
218	    (timeout && !serdev->ops->write_wakeup))
219		return -EINVAL;
220
 
 
 
221	mutex_lock(&serdev->write_lock);
222	do {
223		reinit_completion(&serdev->write_comp);
224
225		ret = ctrl->ops->write_buf(ctrl, buf, count);
226		if (ret < 0)
227			break;
228
 
229		buf += ret;
230		count -= ret;
231
232	} while (count &&
233		 (timeout = wait_for_completion_timeout(&serdev->write_comp,
234							timeout)));
 
 
 
235	mutex_unlock(&serdev->write_lock);
236	return ret < 0 ? ret : (count ? -ETIMEDOUT : 0);
 
 
 
 
 
 
 
 
 
 
 
237}
238EXPORT_SYMBOL_GPL(serdev_device_write);
239
240void serdev_device_write_flush(struct serdev_device *serdev)
241{
242	struct serdev_controller *ctrl = serdev->ctrl;
243
244	if (!ctrl || !ctrl->ops->write_flush)
245		return;
246
247	ctrl->ops->write_flush(ctrl);
248}
249EXPORT_SYMBOL_GPL(serdev_device_write_flush);
250
251int serdev_device_write_room(struct serdev_device *serdev)
252{
253	struct serdev_controller *ctrl = serdev->ctrl;
254
255	if (!ctrl || !ctrl->ops->write_room)
256		return 0;
257
258	return serdev->ctrl->ops->write_room(ctrl);
259}
260EXPORT_SYMBOL_GPL(serdev_device_write_room);
261
262unsigned int serdev_device_set_baudrate(struct serdev_device *serdev, unsigned int speed)
263{
264	struct serdev_controller *ctrl = serdev->ctrl;
265
266	if (!ctrl || !ctrl->ops->set_baudrate)
267		return 0;
268
269	return ctrl->ops->set_baudrate(ctrl, speed);
270
271}
272EXPORT_SYMBOL_GPL(serdev_device_set_baudrate);
273
274void serdev_device_set_flow_control(struct serdev_device *serdev, bool enable)
275{
276	struct serdev_controller *ctrl = serdev->ctrl;
277
278	if (!ctrl || !ctrl->ops->set_flow_control)
279		return;
280
281	ctrl->ops->set_flow_control(ctrl, enable);
282}
283EXPORT_SYMBOL_GPL(serdev_device_set_flow_control);
284
285int serdev_device_set_parity(struct serdev_device *serdev,
286			     enum serdev_parity parity)
287{
288	struct serdev_controller *ctrl = serdev->ctrl;
289
290	if (!ctrl || !ctrl->ops->set_parity)
291		return -ENOTSUPP;
292
293	return ctrl->ops->set_parity(ctrl, parity);
294}
295EXPORT_SYMBOL_GPL(serdev_device_set_parity);
296
297void serdev_device_wait_until_sent(struct serdev_device *serdev, long timeout)
298{
299	struct serdev_controller *ctrl = serdev->ctrl;
300
301	if (!ctrl || !ctrl->ops->wait_until_sent)
302		return;
303
304	ctrl->ops->wait_until_sent(ctrl, timeout);
305}
306EXPORT_SYMBOL_GPL(serdev_device_wait_until_sent);
307
308int serdev_device_get_tiocm(struct serdev_device *serdev)
309{
310	struct serdev_controller *ctrl = serdev->ctrl;
311
312	if (!ctrl || !ctrl->ops->get_tiocm)
313		return -ENOTSUPP;
314
315	return ctrl->ops->get_tiocm(ctrl);
316}
317EXPORT_SYMBOL_GPL(serdev_device_get_tiocm);
318
319int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear)
320{
321	struct serdev_controller *ctrl = serdev->ctrl;
322
323	if (!ctrl || !ctrl->ops->set_tiocm)
324		return -ENOTSUPP;
325
326	return ctrl->ops->set_tiocm(ctrl, set, clear);
327}
328EXPORT_SYMBOL_GPL(serdev_device_set_tiocm);
329
330static int serdev_drv_probe(struct device *dev)
331{
332	const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver);
 
333
334	return sdrv->probe(to_serdev_device(dev));
 
 
 
 
 
 
 
 
335}
336
337static int serdev_drv_remove(struct device *dev)
338{
339	const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver);
340	if (sdrv->remove)
341		sdrv->remove(to_serdev_device(dev));
 
 
 
342	return 0;
343}
344
345static struct bus_type serdev_bus_type = {
346	.name		= "serial",
347	.match		= serdev_device_match,
348	.probe		= serdev_drv_probe,
349	.remove		= serdev_drv_remove,
350};
351
352/**
353 * serdev_device_alloc() - Allocate a new serdev device
354 * @ctrl:	associated controller
355 *
356 * Caller is responsible for either calling serdev_device_add() to add the
357 * newly allocated controller, or calling serdev_device_put() to discard it.
358 */
359struct serdev_device *serdev_device_alloc(struct serdev_controller *ctrl)
360{
361	struct serdev_device *serdev;
362
363	serdev = kzalloc(sizeof(*serdev), GFP_KERNEL);
364	if (!serdev)
365		return NULL;
366
367	serdev->ctrl = ctrl;
368	device_initialize(&serdev->dev);
369	serdev->dev.parent = &ctrl->dev;
370	serdev->dev.bus = &serdev_bus_type;
371	serdev->dev.type = &serdev_device_type;
372	init_completion(&serdev->write_comp);
373	mutex_init(&serdev->write_lock);
374	return serdev;
375}
376EXPORT_SYMBOL_GPL(serdev_device_alloc);
377
378/**
379 * serdev_controller_alloc() - Allocate a new serdev controller
380 * @parent:	parent device
381 * @size:	size of private data
382 *
383 * Caller is responsible for either calling serdev_controller_add() to add the
384 * newly allocated controller, or calling serdev_controller_put() to discard it.
385 * The allocated private data region may be accessed via
386 * serdev_controller_get_drvdata()
387 */
388struct serdev_controller *serdev_controller_alloc(struct device *parent,
389					      size_t size)
390{
391	struct serdev_controller *ctrl;
392	int id;
393
394	if (WARN_ON(!parent))
395		return NULL;
396
397	ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL);
398	if (!ctrl)
399		return NULL;
400
401	id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL);
402	if (id < 0) {
403		dev_err(parent,
404			"unable to allocate serdev controller identifier.\n");
405		goto err_free;
406	}
407
408	ctrl->nr = id;
409
410	device_initialize(&ctrl->dev);
411	ctrl->dev.type = &serdev_ctrl_type;
412	ctrl->dev.bus = &serdev_bus_type;
413	ctrl->dev.parent = parent;
414	ctrl->dev.of_node = parent->of_node;
415	serdev_controller_set_drvdata(ctrl, &ctrl[1]);
416
417	dev_set_name(&ctrl->dev, "serial%d", id);
418
 
 
 
419	dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id);
420	return ctrl;
421
422err_free:
423	kfree(ctrl);
424
425	return NULL;
426}
427EXPORT_SYMBOL_GPL(serdev_controller_alloc);
428
429static int of_serdev_register_devices(struct serdev_controller *ctrl)
430{
431	struct device_node *node;
432	struct serdev_device *serdev = NULL;
433	int err;
434	bool found = false;
435
436	for_each_available_child_of_node(ctrl->dev.of_node, node) {
437		if (!of_get_property(node, "compatible", NULL))
438			continue;
439
440		dev_dbg(&ctrl->dev, "adding child %pOF\n", node);
441
442		serdev = serdev_device_alloc(ctrl);
443		if (!serdev)
444			continue;
445
446		serdev->dev.of_node = node;
447
448		err = serdev_device_add(serdev);
449		if (err) {
450			dev_err(&serdev->dev,
451				"failure adding device. status %d\n", err);
452			serdev_device_put(serdev);
453		} else
454			found = true;
455	}
456	if (!found)
457		return -ENODEV;
458
459	return 0;
460}
461
462#ifdef CONFIG_ACPI
463static acpi_status acpi_serdev_register_device(struct serdev_controller *ctrl,
464					    struct acpi_device *adev)
465{
466	struct serdev_device *serdev = NULL;
467	int err;
468
469	if (acpi_bus_get_status(adev) || !adev->status.present ||
470	    acpi_device_enumerated(adev))
471		return AE_OK;
472
473	serdev = serdev_device_alloc(ctrl);
474	if (!serdev) {
475		dev_err(&ctrl->dev, "failed to allocate serdev device for %s\n",
476			dev_name(&adev->dev));
477		return AE_NO_MEMORY;
478	}
479
480	ACPI_COMPANION_SET(&serdev->dev, adev);
481	acpi_device_set_enumerated(adev);
482
483	err = serdev_device_add(serdev);
484	if (err) {
485		dev_err(&serdev->dev,
486			"failure adding ACPI serdev device. status %d\n", err);
487		serdev_device_put(serdev);
488	}
489
490	return AE_OK;
491}
492
493static acpi_status acpi_serdev_add_device(acpi_handle handle, u32 level,
494				       void *data, void **return_value)
495{
496	struct serdev_controller *ctrl = data;
497	struct acpi_device *adev;
498
499	if (acpi_bus_get_device(handle, &adev))
500		return AE_OK;
501
502	return acpi_serdev_register_device(ctrl, adev);
503}
504
505static int acpi_serdev_register_devices(struct serdev_controller *ctrl)
506{
507	acpi_status status;
508	acpi_handle handle;
509
510	handle = ACPI_HANDLE(ctrl->dev.parent);
511	if (!handle)
512		return -ENODEV;
513
514	status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
515				     acpi_serdev_add_device, NULL, ctrl, NULL);
516	if (ACPI_FAILURE(status))
517		dev_dbg(&ctrl->dev, "failed to enumerate serdev slaves\n");
518
519	if (!ctrl->serdev)
520		return -ENODEV;
521
522	return 0;
523}
524#else
525static inline int acpi_serdev_register_devices(struct serdev_controller *ctrl)
526{
527	return -ENODEV;
528}
529#endif /* CONFIG_ACPI */
530
531/**
532 * serdev_controller_add() - Add an serdev controller
533 * @ctrl:	controller to be registered.
534 *
535 * Register a controller previously allocated via serdev_controller_alloc() with
536 * the serdev core.
537 */
538int serdev_controller_add(struct serdev_controller *ctrl)
539{
540	int ret_of, ret_acpi, ret;
541
542	/* Can't register until after driver model init */
543	if (WARN_ON(!is_registered))
544		return -EAGAIN;
545
546	ret = device_add(&ctrl->dev);
547	if (ret)
548		return ret;
549
 
 
550	ret_of = of_serdev_register_devices(ctrl);
551	ret_acpi = acpi_serdev_register_devices(ctrl);
552	if (ret_of && ret_acpi) {
553		dev_dbg(&ctrl->dev, "no devices registered: of:%d acpi:%d\n",
554			ret_of, ret_acpi);
555		ret = -ENODEV;
556		goto out_dev_del;
557	}
558
559	dev_dbg(&ctrl->dev, "serdev%d registered: dev:%p\n",
560		ctrl->nr, &ctrl->dev);
561	return 0;
562
563out_dev_del:
 
564	device_del(&ctrl->dev);
565	return ret;
566};
567EXPORT_SYMBOL_GPL(serdev_controller_add);
568
569/* Remove a device associated with a controller */
570static int serdev_remove_device(struct device *dev, void *data)
571{
572	struct serdev_device *serdev = to_serdev_device(dev);
573	if (dev->type == &serdev_device_type)
574		serdev_device_remove(serdev);
575	return 0;
576}
577
578/**
579 * serdev_controller_remove(): remove an serdev controller
580 * @ctrl:	controller to remove
581 *
582 * Remove a serdev controller.  Caller is responsible for calling
583 * serdev_controller_put() to discard the allocated controller.
584 */
585void serdev_controller_remove(struct serdev_controller *ctrl)
586{
587	int dummy;
588
589	if (!ctrl)
590		return;
591
592	dummy = device_for_each_child(&ctrl->dev, NULL,
593				      serdev_remove_device);
 
594	device_del(&ctrl->dev);
595}
596EXPORT_SYMBOL_GPL(serdev_controller_remove);
597
598/**
599 * serdev_driver_register() - Register client driver with serdev core
600 * @sdrv:	client driver to be associated with client-device.
601 *
602 * This API will register the client driver with the serdev framework.
603 * It is typically called from the driver's module-init function.
604 */
605int __serdev_device_driver_register(struct serdev_device_driver *sdrv, struct module *owner)
606{
607	sdrv->driver.bus = &serdev_bus_type;
608	sdrv->driver.owner = owner;
609
610	/* force drivers to async probe so I/O is possible in probe */
611        sdrv->driver.probe_type = PROBE_PREFER_ASYNCHRONOUS;
612
613	return driver_register(&sdrv->driver);
614}
615EXPORT_SYMBOL_GPL(__serdev_device_driver_register);
616
617static void __exit serdev_exit(void)
618{
619	bus_unregister(&serdev_bus_type);
 
620}
621module_exit(serdev_exit);
622
623static int __init serdev_init(void)
624{
625	int ret;
626
627	ret = bus_register(&serdev_bus_type);
628	if (ret)
629		return ret;
630
631	is_registered = true;
632	return 0;
633}
634/* Must be before serial drivers register */
635postcore_initcall(serdev_init);
636
637MODULE_AUTHOR("Rob Herring <robh@kernel.org>");
638MODULE_LICENSE("GPL v2");
639MODULE_DESCRIPTION("Serial attached device bus");