GNU Linux-libre 4.14.303-gnu1
[releases.git] / drivers / tty / serdev / core.c
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 serdev_controller *ctrl = serdev->ctrl;
69         struct device *parent = serdev->dev.parent;
70         int err;
71
72         dev_set_name(&serdev->dev, "%s-%d", dev_name(parent), serdev->nr);
73
74         /* Only a single slave device is currently supported. */
75         if (ctrl->serdev) {
76                 dev_err(&serdev->dev, "controller busy\n");
77                 return -EBUSY;
78         }
79         ctrl->serdev = serdev;
80
81         err = device_add(&serdev->dev);
82         if (err < 0) {
83                 dev_err(&serdev->dev, "Can't add %s, status %d\n",
84                         dev_name(&serdev->dev), err);
85                 goto err_clear_serdev;
86         }
87
88         dev_dbg(&serdev->dev, "device %s registered\n", dev_name(&serdev->dev));
89
90         return 0;
91
92 err_clear_serdev:
93         ctrl->serdev = NULL;
94         return err;
95 }
96 EXPORT_SYMBOL_GPL(serdev_device_add);
97
98 /**
99  * serdev_device_remove(): remove an serdev device
100  * @serdev:     serdev_device to be removed
101  */
102 void serdev_device_remove(struct serdev_device *serdev)
103 {
104         struct serdev_controller *ctrl = serdev->ctrl;
105
106         device_unregister(&serdev->dev);
107         ctrl->serdev = NULL;
108 }
109 EXPORT_SYMBOL_GPL(serdev_device_remove);
110
111 int serdev_device_open(struct serdev_device *serdev)
112 {
113         struct serdev_controller *ctrl = serdev->ctrl;
114
115         if (!ctrl || !ctrl->ops->open)
116                 return -EINVAL;
117
118         return ctrl->ops->open(ctrl);
119 }
120 EXPORT_SYMBOL_GPL(serdev_device_open);
121
122 void serdev_device_close(struct serdev_device *serdev)
123 {
124         struct serdev_controller *ctrl = serdev->ctrl;
125
126         if (!ctrl || !ctrl->ops->close)
127                 return;
128
129         ctrl->ops->close(ctrl);
130 }
131 EXPORT_SYMBOL_GPL(serdev_device_close);
132
133 void serdev_device_write_wakeup(struct serdev_device *serdev)
134 {
135         complete(&serdev->write_comp);
136 }
137 EXPORT_SYMBOL_GPL(serdev_device_write_wakeup);
138
139 int serdev_device_write_buf(struct serdev_device *serdev,
140                             const unsigned char *buf, size_t count)
141 {
142         struct serdev_controller *ctrl = serdev->ctrl;
143
144         if (!ctrl || !ctrl->ops->write_buf)
145                 return -EINVAL;
146
147         return ctrl->ops->write_buf(ctrl, buf, count);
148 }
149 EXPORT_SYMBOL_GPL(serdev_device_write_buf);
150
151 int serdev_device_write(struct serdev_device *serdev,
152                         const unsigned char *buf, size_t count,
153                         unsigned long timeout)
154 {
155         struct serdev_controller *ctrl = serdev->ctrl;
156         int ret;
157
158         if (!ctrl || !ctrl->ops->write_buf ||
159             (timeout && !serdev->ops->write_wakeup))
160                 return -EINVAL;
161
162         mutex_lock(&serdev->write_lock);
163         do {
164                 reinit_completion(&serdev->write_comp);
165
166                 ret = ctrl->ops->write_buf(ctrl, buf, count);
167                 if (ret < 0)
168                         break;
169
170                 buf += ret;
171                 count -= ret;
172
173         } while (count &&
174                  (timeout = wait_for_completion_timeout(&serdev->write_comp,
175                                                         timeout)));
176         mutex_unlock(&serdev->write_lock);
177         return ret < 0 ? ret : (count ? -ETIMEDOUT : 0);
178 }
179 EXPORT_SYMBOL_GPL(serdev_device_write);
180
181 void serdev_device_write_flush(struct serdev_device *serdev)
182 {
183         struct serdev_controller *ctrl = serdev->ctrl;
184
185         if (!ctrl || !ctrl->ops->write_flush)
186                 return;
187
188         ctrl->ops->write_flush(ctrl);
189 }
190 EXPORT_SYMBOL_GPL(serdev_device_write_flush);
191
192 int serdev_device_write_room(struct serdev_device *serdev)
193 {
194         struct serdev_controller *ctrl = serdev->ctrl;
195
196         if (!ctrl || !ctrl->ops->write_room)
197                 return 0;
198
199         return serdev->ctrl->ops->write_room(ctrl);
200 }
201 EXPORT_SYMBOL_GPL(serdev_device_write_room);
202
203 unsigned int serdev_device_set_baudrate(struct serdev_device *serdev, unsigned int speed)
204 {
205         struct serdev_controller *ctrl = serdev->ctrl;
206
207         if (!ctrl || !ctrl->ops->set_baudrate)
208                 return 0;
209
210         return ctrl->ops->set_baudrate(ctrl, speed);
211
212 }
213 EXPORT_SYMBOL_GPL(serdev_device_set_baudrate);
214
215 void serdev_device_set_flow_control(struct serdev_device *serdev, bool enable)
216 {
217         struct serdev_controller *ctrl = serdev->ctrl;
218
219         if (!ctrl || !ctrl->ops->set_flow_control)
220                 return;
221
222         ctrl->ops->set_flow_control(ctrl, enable);
223 }
224 EXPORT_SYMBOL_GPL(serdev_device_set_flow_control);
225
226 void serdev_device_wait_until_sent(struct serdev_device *serdev, long timeout)
227 {
228         struct serdev_controller *ctrl = serdev->ctrl;
229
230         if (!ctrl || !ctrl->ops->wait_until_sent)
231                 return;
232
233         ctrl->ops->wait_until_sent(ctrl, timeout);
234 }
235 EXPORT_SYMBOL_GPL(serdev_device_wait_until_sent);
236
237 int serdev_device_get_tiocm(struct serdev_device *serdev)
238 {
239         struct serdev_controller *ctrl = serdev->ctrl;
240
241         if (!ctrl || !ctrl->ops->get_tiocm)
242                 return -ENOTSUPP;
243
244         return ctrl->ops->get_tiocm(ctrl);
245 }
246 EXPORT_SYMBOL_GPL(serdev_device_get_tiocm);
247
248 int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear)
249 {
250         struct serdev_controller *ctrl = serdev->ctrl;
251
252         if (!ctrl || !ctrl->ops->set_tiocm)
253                 return -ENOTSUPP;
254
255         return ctrl->ops->set_tiocm(ctrl, set, clear);
256 }
257 EXPORT_SYMBOL_GPL(serdev_device_set_tiocm);
258
259 static int serdev_drv_probe(struct device *dev)
260 {
261         const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver);
262
263         return sdrv->probe(to_serdev_device(dev));
264 }
265
266 static int serdev_drv_remove(struct device *dev)
267 {
268         const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver);
269
270         sdrv->remove(to_serdev_device(dev));
271         return 0;
272 }
273
274 static ssize_t modalias_show(struct device *dev,
275                              struct device_attribute *attr, char *buf)
276 {
277         return of_device_modalias(dev, buf, PAGE_SIZE);
278 }
279 DEVICE_ATTR_RO(modalias);
280
281 static struct attribute *serdev_device_attrs[] = {
282         &dev_attr_modalias.attr,
283         NULL,
284 };
285 ATTRIBUTE_GROUPS(serdev_device);
286
287 static struct bus_type serdev_bus_type = {
288         .name           = "serial",
289         .match          = serdev_device_match,
290         .probe          = serdev_drv_probe,
291         .remove         = serdev_drv_remove,
292         .uevent         = serdev_uevent,
293         .dev_groups     = serdev_device_groups,
294 };
295
296 /**
297  * serdev_controller_alloc() - Allocate a new serdev device
298  * @ctrl:       associated controller
299  *
300  * Caller is responsible for either calling serdev_device_add() to add the
301  * newly allocated controller, or calling serdev_device_put() to discard it.
302  */
303 struct serdev_device *serdev_device_alloc(struct serdev_controller *ctrl)
304 {
305         struct serdev_device *serdev;
306
307         serdev = kzalloc(sizeof(*serdev), GFP_KERNEL);
308         if (!serdev)
309                 return NULL;
310
311         serdev->ctrl = ctrl;
312         device_initialize(&serdev->dev);
313         serdev->dev.parent = &ctrl->dev;
314         serdev->dev.bus = &serdev_bus_type;
315         serdev->dev.type = &serdev_device_type;
316         init_completion(&serdev->write_comp);
317         mutex_init(&serdev->write_lock);
318         return serdev;
319 }
320 EXPORT_SYMBOL_GPL(serdev_device_alloc);
321
322 /**
323  * serdev_controller_alloc() - Allocate a new serdev controller
324  * @parent:     parent device
325  * @size:       size of private data
326  *
327  * Caller is responsible for either calling serdev_controller_add() to add the
328  * newly allocated controller, or calling serdev_controller_put() to discard it.
329  * The allocated private data region may be accessed via
330  * serdev_controller_get_drvdata()
331  */
332 struct serdev_controller *serdev_controller_alloc(struct device *parent,
333                                               size_t size)
334 {
335         struct serdev_controller *ctrl;
336         int id;
337
338         if (WARN_ON(!parent))
339                 return NULL;
340
341         ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL);
342         if (!ctrl)
343                 return NULL;
344
345         device_initialize(&ctrl->dev);
346         ctrl->dev.type = &serdev_ctrl_type;
347         ctrl->dev.bus = &serdev_bus_type;
348         ctrl->dev.parent = parent;
349         ctrl->dev.of_node = parent->of_node;
350         serdev_controller_set_drvdata(ctrl, &ctrl[1]);
351
352         id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL);
353         if (id < 0) {
354                 dev_err(parent,
355                         "unable to allocate serdev controller identifier.\n");
356                 serdev_controller_put(ctrl);
357                 return NULL;
358         }
359
360         ctrl->nr = id;
361         dev_set_name(&ctrl->dev, "serial%d", id);
362
363         dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id);
364         return ctrl;
365 }
366 EXPORT_SYMBOL_GPL(serdev_controller_alloc);
367
368 static int of_serdev_register_devices(struct serdev_controller *ctrl)
369 {
370         struct device_node *node;
371         struct serdev_device *serdev = NULL;
372         int err;
373         bool found = false;
374
375         for_each_available_child_of_node(ctrl->dev.of_node, node) {
376                 if (!of_get_property(node, "compatible", NULL))
377                         continue;
378
379                 dev_dbg(&ctrl->dev, "adding child %pOF\n", node);
380
381                 serdev = serdev_device_alloc(ctrl);
382                 if (!serdev)
383                         continue;
384
385                 serdev->dev.of_node = node;
386
387                 err = serdev_device_add(serdev);
388                 if (err) {
389                         dev_err(&serdev->dev,
390                                 "failure adding device. status %d\n", err);
391                         serdev_device_put(serdev);
392                 } else
393                         found = true;
394         }
395         if (!found)
396                 return -ENODEV;
397
398         return 0;
399 }
400
401 /**
402  * serdev_controller_add() - Add an serdev controller
403  * @ctrl:       controller to be registered.
404  *
405  * Register a controller previously allocated via serdev_controller_alloc() with
406  * the serdev core.
407  */
408 int serdev_controller_add(struct serdev_controller *ctrl)
409 {
410         int ret;
411
412         /* Can't register until after driver model init */
413         if (WARN_ON(!is_registered))
414                 return -EAGAIN;
415
416         ret = device_add(&ctrl->dev);
417         if (ret)
418                 return ret;
419
420         ret = of_serdev_register_devices(ctrl);
421         if (ret)
422                 goto out_dev_del;
423
424         dev_dbg(&ctrl->dev, "serdev%d registered: dev:%p\n",
425                 ctrl->nr, &ctrl->dev);
426         return 0;
427
428 out_dev_del:
429         device_del(&ctrl->dev);
430         return ret;
431 };
432 EXPORT_SYMBOL_GPL(serdev_controller_add);
433
434 /* Remove a device associated with a controller */
435 static int serdev_remove_device(struct device *dev, void *data)
436 {
437         struct serdev_device *serdev = to_serdev_device(dev);
438         if (dev->type == &serdev_device_type)
439                 serdev_device_remove(serdev);
440         return 0;
441 }
442
443 /**
444  * serdev_controller_remove(): remove an serdev controller
445  * @ctrl:       controller to remove
446  *
447  * Remove a serdev controller.  Caller is responsible for calling
448  * serdev_controller_put() to discard the allocated controller.
449  */
450 void serdev_controller_remove(struct serdev_controller *ctrl)
451 {
452         int dummy;
453
454         if (!ctrl)
455                 return;
456
457         dummy = device_for_each_child(&ctrl->dev, NULL,
458                                       serdev_remove_device);
459         device_del(&ctrl->dev);
460 }
461 EXPORT_SYMBOL_GPL(serdev_controller_remove);
462
463 /**
464  * serdev_driver_register() - Register client driver with serdev core
465  * @sdrv:       client driver to be associated with client-device.
466  *
467  * This API will register the client driver with the serdev framework.
468  * It is typically called from the driver's module-init function.
469  */
470 int __serdev_device_driver_register(struct serdev_device_driver *sdrv, struct module *owner)
471 {
472         sdrv->driver.bus = &serdev_bus_type;
473         sdrv->driver.owner = owner;
474
475         /* force drivers to async probe so I/O is possible in probe */
476         sdrv->driver.probe_type = PROBE_PREFER_ASYNCHRONOUS;
477
478         return driver_register(&sdrv->driver);
479 }
480 EXPORT_SYMBOL_GPL(__serdev_device_driver_register);
481
482 static void __exit serdev_exit(void)
483 {
484         bus_unregister(&serdev_bus_type);
485         ida_destroy(&ctrl_ida);
486 }
487 module_exit(serdev_exit);
488
489 static int __init serdev_init(void)
490 {
491         int ret;
492
493         ret = bus_register(&serdev_bus_type);
494         if (ret)
495                 return ret;
496
497         is_registered = true;
498         return 0;
499 }
500 /* Must be before serial drivers register */
501 postcore_initcall(serdev_init);
502
503 MODULE_AUTHOR("Rob Herring <robh@kernel.org>");
504 MODULE_LICENSE("GPL v2");
505 MODULE_DESCRIPTION("Serial attached device bus");