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