]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/tty/serdev/core.c
Merge tag 'devicetree-for-4.12' of git://git.kernel.org/pub/scm/linux/kernel/git...
[mirror_ubuntu-artful-kernel.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 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 int serdev_device_write_buf(struct serdev_device *serdev,
120 const unsigned char *buf, size_t count)
121 {
122 struct serdev_controller *ctrl = serdev->ctrl;
123
124 if (!ctrl || !ctrl->ops->write_buf)
125 return -EINVAL;
126
127 return ctrl->ops->write_buf(ctrl, buf, count);
128 }
129 EXPORT_SYMBOL_GPL(serdev_device_write_buf);
130
131 void serdev_device_write_flush(struct serdev_device *serdev)
132 {
133 struct serdev_controller *ctrl = serdev->ctrl;
134
135 if (!ctrl || !ctrl->ops->write_flush)
136 return;
137
138 ctrl->ops->write_flush(ctrl);
139 }
140 EXPORT_SYMBOL_GPL(serdev_device_write_flush);
141
142 int serdev_device_write_room(struct serdev_device *serdev)
143 {
144 struct serdev_controller *ctrl = serdev->ctrl;
145
146 if (!ctrl || !ctrl->ops->write_room)
147 return 0;
148
149 return serdev->ctrl->ops->write_room(ctrl);
150 }
151 EXPORT_SYMBOL_GPL(serdev_device_write_room);
152
153 unsigned int serdev_device_set_baudrate(struct serdev_device *serdev, unsigned int speed)
154 {
155 struct serdev_controller *ctrl = serdev->ctrl;
156
157 if (!ctrl || !ctrl->ops->set_baudrate)
158 return 0;
159
160 return ctrl->ops->set_baudrate(ctrl, speed);
161
162 }
163 EXPORT_SYMBOL_GPL(serdev_device_set_baudrate);
164
165 void serdev_device_set_flow_control(struct serdev_device *serdev, bool enable)
166 {
167 struct serdev_controller *ctrl = serdev->ctrl;
168
169 if (!ctrl || !ctrl->ops->set_flow_control)
170 return;
171
172 ctrl->ops->set_flow_control(ctrl, enable);
173 }
174 EXPORT_SYMBOL_GPL(serdev_device_set_flow_control);
175
176 void serdev_device_wait_until_sent(struct serdev_device *serdev, long timeout)
177 {
178 struct serdev_controller *ctrl = serdev->ctrl;
179
180 if (!ctrl || !ctrl->ops->wait_until_sent)
181 return;
182
183 ctrl->ops->wait_until_sent(ctrl, timeout);
184 }
185 EXPORT_SYMBOL_GPL(serdev_device_wait_until_sent);
186
187 int serdev_device_get_tiocm(struct serdev_device *serdev)
188 {
189 struct serdev_controller *ctrl = serdev->ctrl;
190
191 if (!ctrl || !ctrl->ops->get_tiocm)
192 return -ENOTSUPP;
193
194 return ctrl->ops->get_tiocm(ctrl);
195 }
196 EXPORT_SYMBOL_GPL(serdev_device_get_tiocm);
197
198 int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear)
199 {
200 struct serdev_controller *ctrl = serdev->ctrl;
201
202 if (!ctrl || !ctrl->ops->set_tiocm)
203 return -ENOTSUPP;
204
205 return ctrl->ops->set_tiocm(ctrl, set, clear);
206 }
207 EXPORT_SYMBOL_GPL(serdev_device_set_tiocm);
208
209 static int serdev_drv_probe(struct device *dev)
210 {
211 const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver);
212
213 return sdrv->probe(to_serdev_device(dev));
214 }
215
216 static int serdev_drv_remove(struct device *dev)
217 {
218 const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver);
219
220 sdrv->remove(to_serdev_device(dev));
221 return 0;
222 }
223
224 static ssize_t modalias_show(struct device *dev,
225 struct device_attribute *attr, char *buf)
226 {
227 return of_device_modalias(dev, buf, PAGE_SIZE);
228 }
229
230 static struct device_attribute serdev_device_attrs[] = {
231 __ATTR_RO(modalias),
232 __ATTR_NULL
233 };
234
235 static struct bus_type serdev_bus_type = {
236 .name = "serial",
237 .match = serdev_device_match,
238 .probe = serdev_drv_probe,
239 .remove = serdev_drv_remove,
240 .uevent = serdev_uevent,
241 .dev_attrs = serdev_device_attrs,
242 };
243
244 /**
245 * serdev_controller_alloc() - Allocate a new serdev device
246 * @ctrl: associated controller
247 *
248 * Caller is responsible for either calling serdev_device_add() to add the
249 * newly allocated controller, or calling serdev_device_put() to discard it.
250 */
251 struct serdev_device *serdev_device_alloc(struct serdev_controller *ctrl)
252 {
253 struct serdev_device *serdev;
254
255 serdev = kzalloc(sizeof(*serdev), GFP_KERNEL);
256 if (!serdev)
257 return NULL;
258
259 serdev->ctrl = ctrl;
260 ctrl->serdev = serdev;
261 device_initialize(&serdev->dev);
262 serdev->dev.parent = &ctrl->dev;
263 serdev->dev.bus = &serdev_bus_type;
264 serdev->dev.type = &serdev_device_type;
265 return serdev;
266 }
267 EXPORT_SYMBOL_GPL(serdev_device_alloc);
268
269 /**
270 * serdev_controller_alloc() - Allocate a new serdev controller
271 * @parent: parent device
272 * @size: size of private data
273 *
274 * Caller is responsible for either calling serdev_controller_add() to add the
275 * newly allocated controller, or calling serdev_controller_put() to discard it.
276 * The allocated private data region may be accessed via
277 * serdev_controller_get_drvdata()
278 */
279 struct serdev_controller *serdev_controller_alloc(struct device *parent,
280 size_t size)
281 {
282 struct serdev_controller *ctrl;
283 int id;
284
285 if (WARN_ON(!parent))
286 return NULL;
287
288 ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL);
289 if (!ctrl)
290 return NULL;
291
292 device_initialize(&ctrl->dev);
293 ctrl->dev.type = &serdev_ctrl_type;
294 ctrl->dev.bus = &serdev_bus_type;
295 ctrl->dev.parent = parent;
296 ctrl->dev.of_node = parent->of_node;
297 serdev_controller_set_drvdata(ctrl, &ctrl[1]);
298
299 id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL);
300 if (id < 0) {
301 dev_err(parent,
302 "unable to allocate serdev controller identifier.\n");
303 serdev_controller_put(ctrl);
304 return NULL;
305 }
306
307 ctrl->nr = id;
308 dev_set_name(&ctrl->dev, "serial%d", id);
309
310 dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id);
311 return ctrl;
312 }
313 EXPORT_SYMBOL_GPL(serdev_controller_alloc);
314
315 static int of_serdev_register_devices(struct serdev_controller *ctrl)
316 {
317 struct device_node *node;
318 struct serdev_device *serdev = NULL;
319 int err;
320 bool found = false;
321
322 for_each_available_child_of_node(ctrl->dev.of_node, node) {
323 if (!of_get_property(node, "compatible", NULL))
324 continue;
325
326 dev_dbg(&ctrl->dev, "adding child %s\n", node->full_name);
327
328 serdev = serdev_device_alloc(ctrl);
329 if (!serdev)
330 continue;
331
332 serdev->dev.of_node = node;
333
334 err = serdev_device_add(serdev);
335 if (err) {
336 dev_err(&serdev->dev,
337 "failure adding device. status %d\n", err);
338 serdev_device_put(serdev);
339 } else
340 found = true;
341 }
342 if (!found)
343 return -ENODEV;
344
345 return 0;
346 }
347
348 /**
349 * serdev_controller_add() - Add an serdev controller
350 * @ctrl: controller to be registered.
351 *
352 * Register a controller previously allocated via serdev_controller_alloc() with
353 * the serdev core.
354 */
355 int serdev_controller_add(struct serdev_controller *ctrl)
356 {
357 int ret;
358
359 /* Can't register until after driver model init */
360 if (WARN_ON(!is_registered))
361 return -EAGAIN;
362
363 ret = device_add(&ctrl->dev);
364 if (ret)
365 return ret;
366
367 ret = of_serdev_register_devices(ctrl);
368 if (ret)
369 goto out_dev_del;
370
371 dev_dbg(&ctrl->dev, "serdev%d registered: dev:%p\n",
372 ctrl->nr, &ctrl->dev);
373 return 0;
374
375 out_dev_del:
376 device_del(&ctrl->dev);
377 return ret;
378 };
379 EXPORT_SYMBOL_GPL(serdev_controller_add);
380
381 /* Remove a device associated with a controller */
382 static int serdev_remove_device(struct device *dev, void *data)
383 {
384 struct serdev_device *serdev = to_serdev_device(dev);
385 if (dev->type == &serdev_device_type)
386 serdev_device_remove(serdev);
387 return 0;
388 }
389
390 /**
391 * serdev_controller_remove(): remove an serdev controller
392 * @ctrl: controller to remove
393 *
394 * Remove a serdev controller. Caller is responsible for calling
395 * serdev_controller_put() to discard the allocated controller.
396 */
397 void serdev_controller_remove(struct serdev_controller *ctrl)
398 {
399 int dummy;
400
401 if (!ctrl)
402 return;
403
404 dummy = device_for_each_child(&ctrl->dev, NULL,
405 serdev_remove_device);
406 device_del(&ctrl->dev);
407 }
408 EXPORT_SYMBOL_GPL(serdev_controller_remove);
409
410 /**
411 * serdev_driver_register() - Register client driver with serdev core
412 * @sdrv: client driver to be associated with client-device.
413 *
414 * This API will register the client driver with the serdev framework.
415 * It is typically called from the driver's module-init function.
416 */
417 int __serdev_device_driver_register(struct serdev_device_driver *sdrv, struct module *owner)
418 {
419 sdrv->driver.bus = &serdev_bus_type;
420 sdrv->driver.owner = owner;
421
422 /* force drivers to async probe so I/O is possible in probe */
423 sdrv->driver.probe_type = PROBE_PREFER_ASYNCHRONOUS;
424
425 return driver_register(&sdrv->driver);
426 }
427 EXPORT_SYMBOL_GPL(__serdev_device_driver_register);
428
429 static void __exit serdev_exit(void)
430 {
431 bus_unregister(&serdev_bus_type);
432 }
433 module_exit(serdev_exit);
434
435 static int __init serdev_init(void)
436 {
437 int ret;
438
439 ret = bus_register(&serdev_bus_type);
440 if (ret)
441 return ret;
442
443 is_registered = true;
444 return 0;
445 }
446 /* Must be before serial drivers register */
447 postcore_initcall(serdev_init);
448
449 MODULE_AUTHOR("Rob Herring <robh@kernel.org>");
450 MODULE_LICENSE("GPL v2");
451 MODULE_DESCRIPTION("Serial attached device bus");