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