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