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