blob: 4d662d1f47846b61e7d3367476ed0b50c3838215 [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
Johan Hovold978d6fa2017-10-12 15:28:18 +0200361 id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL);
362 if (id < 0) {
363 dev_err(parent,
364 "unable to allocate serdev controller identifier.\n");
365 goto err_free;
366 }
367
368 ctrl->nr = id;
369
Rob Herringcd6484e2017-02-02 13:48:07 -0600370 device_initialize(&ctrl->dev);
371 ctrl->dev.type = &serdev_ctrl_type;
372 ctrl->dev.bus = &serdev_bus_type;
373 ctrl->dev.parent = parent;
374 ctrl->dev.of_node = parent->of_node;
375 serdev_controller_set_drvdata(ctrl, &ctrl[1]);
376
Rob Herringcd6484e2017-02-02 13:48:07 -0600377 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;
Johan Hovold978d6fa2017-10-12 15:28:18 +0200381
382err_free:
383 kfree(ctrl);
384
385 return NULL;
Rob Herringcd6484e2017-02-02 13:48:07 -0600386}
387EXPORT_SYMBOL_GPL(serdev_controller_alloc);
388
389static int of_serdev_register_devices(struct serdev_controller *ctrl)
390{
391 struct device_node *node;
392 struct serdev_device *serdev = NULL;
393 int err;
394 bool found = false;
395
396 for_each_available_child_of_node(ctrl->dev.of_node, node) {
397 if (!of_get_property(node, "compatible", NULL))
398 continue;
399
Rob Herringa73ee842017-07-18 16:43:34 -0500400 dev_dbg(&ctrl->dev, "adding child %pOF\n", node);
Rob Herringcd6484e2017-02-02 13:48:07 -0600401
402 serdev = serdev_device_alloc(ctrl);
403 if (!serdev)
404 continue;
405
406 serdev->dev.of_node = node;
407
408 err = serdev_device_add(serdev);
409 if (err) {
410 dev_err(&serdev->dev,
411 "failure adding device. status %d\n", err);
412 serdev_device_put(serdev);
413 } else
414 found = true;
415 }
416 if (!found)
417 return -ENODEV;
418
419 return 0;
420}
421
Frédéric Danis53c76262017-10-11 10:32:13 +0200422#ifdef CONFIG_ACPI
423static acpi_status acpi_serdev_register_device(struct serdev_controller *ctrl,
424 struct acpi_device *adev)
425{
426 struct serdev_device *serdev = NULL;
427 int err;
428
429 if (acpi_bus_get_status(adev) || !adev->status.present ||
430 acpi_device_enumerated(adev))
431 return AE_OK;
432
433 serdev = serdev_device_alloc(ctrl);
434 if (!serdev) {
435 dev_err(&ctrl->dev, "failed to allocate serdev device for %s\n",
436 dev_name(&adev->dev));
437 return AE_NO_MEMORY;
438 }
439
440 ACPI_COMPANION_SET(&serdev->dev, adev);
441 acpi_device_set_enumerated(adev);
442
443 err = serdev_device_add(serdev);
444 if (err) {
445 dev_err(&serdev->dev,
446 "failure adding ACPI serdev device. status %d\n", err);
447 serdev_device_put(serdev);
448 }
449
450 return AE_OK;
451}
452
453static acpi_status acpi_serdev_add_device(acpi_handle handle, u32 level,
454 void *data, void **return_value)
455{
456 struct serdev_controller *ctrl = data;
457 struct acpi_device *adev;
458
459 if (acpi_bus_get_device(handle, &adev))
460 return AE_OK;
461
462 return acpi_serdev_register_device(ctrl, adev);
463}
464
465static int acpi_serdev_register_devices(struct serdev_controller *ctrl)
466{
467 acpi_status status;
468 acpi_handle handle;
469
470 handle = ACPI_HANDLE(ctrl->dev.parent);
471 if (!handle)
472 return -ENODEV;
473
474 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
475 acpi_serdev_add_device, NULL, ctrl, NULL);
476 if (ACPI_FAILURE(status))
477 dev_dbg(&ctrl->dev, "failed to enumerate serdev slaves\n");
478
479 if (!ctrl->serdev)
480 return -ENODEV;
481
482 return 0;
483}
484#else
485static inline int acpi_serdev_register_devices(struct serdev_controller *ctrl)
486{
487 return -ENODEV;
488}
489#endif /* CONFIG_ACPI */
490
Rob Herringcd6484e2017-02-02 13:48:07 -0600491/**
492 * serdev_controller_add() - Add an serdev controller
493 * @ctrl: controller to be registered.
494 *
495 * Register a controller previously allocated via serdev_controller_alloc() with
496 * the serdev core.
497 */
498int serdev_controller_add(struct serdev_controller *ctrl)
499{
Frédéric Danis53c76262017-10-11 10:32:13 +0200500 int ret_of, ret_acpi, ret;
Rob Herringcd6484e2017-02-02 13:48:07 -0600501
502 /* Can't register until after driver model init */
503 if (WARN_ON(!is_registered))
504 return -EAGAIN;
505
506 ret = device_add(&ctrl->dev);
507 if (ret)
508 return ret;
509
Frédéric Danis53c76262017-10-11 10:32:13 +0200510 ret_of = of_serdev_register_devices(ctrl);
511 ret_acpi = acpi_serdev_register_devices(ctrl);
512 if (ret_of && ret_acpi) {
513 dev_dbg(&ctrl->dev, "no devices registered: of:%d acpi:%d\n",
514 ret_of, ret_acpi);
515 ret = -ENODEV;
Rob Herringcd6484e2017-02-02 13:48:07 -0600516 goto out_dev_del;
Frédéric Danis53c76262017-10-11 10:32:13 +0200517 }
Rob Herringcd6484e2017-02-02 13:48:07 -0600518
519 dev_dbg(&ctrl->dev, "serdev%d registered: dev:%p\n",
520 ctrl->nr, &ctrl->dev);
521 return 0;
522
523out_dev_del:
524 device_del(&ctrl->dev);
525 return ret;
526};
527EXPORT_SYMBOL_GPL(serdev_controller_add);
528
529/* Remove a device associated with a controller */
530static int serdev_remove_device(struct device *dev, void *data)
531{
532 struct serdev_device *serdev = to_serdev_device(dev);
533 if (dev->type == &serdev_device_type)
534 serdev_device_remove(serdev);
535 return 0;
536}
537
538/**
539 * serdev_controller_remove(): remove an serdev controller
540 * @ctrl: controller to remove
541 *
542 * Remove a serdev controller. Caller is responsible for calling
543 * serdev_controller_put() to discard the allocated controller.
544 */
545void serdev_controller_remove(struct serdev_controller *ctrl)
546{
547 int dummy;
548
549 if (!ctrl)
550 return;
551
552 dummy = device_for_each_child(&ctrl->dev, NULL,
553 serdev_remove_device);
554 device_del(&ctrl->dev);
555}
556EXPORT_SYMBOL_GPL(serdev_controller_remove);
557
558/**
559 * serdev_driver_register() - Register client driver with serdev core
560 * @sdrv: client driver to be associated with client-device.
561 *
562 * This API will register the client driver with the serdev framework.
563 * It is typically called from the driver's module-init function.
564 */
565int __serdev_device_driver_register(struct serdev_device_driver *sdrv, struct module *owner)
566{
567 sdrv->driver.bus = &serdev_bus_type;
568 sdrv->driver.owner = owner;
569
570 /* force drivers to async probe so I/O is possible in probe */
571 sdrv->driver.probe_type = PROBE_PREFER_ASYNCHRONOUS;
572
573 return driver_register(&sdrv->driver);
574}
575EXPORT_SYMBOL_GPL(__serdev_device_driver_register);
576
577static void __exit serdev_exit(void)
578{
579 bus_unregister(&serdev_bus_type);
580}
581module_exit(serdev_exit);
582
583static int __init serdev_init(void)
584{
585 int ret;
586
587 ret = bus_register(&serdev_bus_type);
588 if (ret)
589 return ret;
590
591 is_registered = true;
592 return 0;
593}
594/* Must be before serial drivers register */
595postcore_initcall(serdev_init);
596
597MODULE_AUTHOR("Rob Herring <robh@kernel.org>");
598MODULE_LICENSE("GPL v2");
599MODULE_DESCRIPTION("Serial attached device bus");