blob: c8c43834477b562659d732c7a890ba318edd0f60 [file] [log] [blame]
Greg Kroah-Hartmane3b3d0f2017-11-06 18:11:51 +01001// SPDX-License-Identifier: GPL-2.0
Rob Herringcd6484e2017-02-02 13:48:07 -06002/*
3 * Copyright (C) 2016-2017 Linaro Ltd., Rob Herring <robh@kernel.org>
4 *
5 * Based on drivers/spmi/spmi.c:
6 * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
Rob Herringcd6484e2017-02-02 13:48:07 -06007 */
8
Frédéric Danis53c76262017-10-11 10:32:13 +02009#include <linux/acpi.h>
Rob Herringcd6484e2017-02-02 13:48:07 -060010#include <linux/errno.h>
11#include <linux/idr.h>
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/of.h>
15#include <linux/of_device.h>
16#include <linux/serdev.h>
17#include <linux/slab.h>
18
19static bool is_registered;
20static DEFINE_IDA(ctrl_ida);
21
Johan Hovold24609422018-01-09 17:09:16 +010022static ssize_t modalias_show(struct device *dev,
23 struct device_attribute *attr, char *buf)
24{
25 int len;
26
27 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
28 if (len != -ENODEV)
29 return len;
30
31 return of_device_modalias(dev, buf, PAGE_SIZE);
32}
33static DEVICE_ATTR_RO(modalias);
34
35static struct attribute *serdev_device_attrs[] = {
36 &dev_attr_modalias.attr,
37 NULL,
38};
39ATTRIBUTE_GROUPS(serdev_device);
40
41static int serdev_device_uevent(struct device *dev, struct kobj_uevent_env *env)
42{
43 int rc;
44
45 /* TODO: platform modalias */
46
47 rc = acpi_device_uevent_modalias(dev, env);
48 if (rc != -ENODEV)
49 return rc;
50
51 return of_device_uevent_modalias(dev, env);
52}
53
Rob Herringcd6484e2017-02-02 13:48:07 -060054static void serdev_device_release(struct device *dev)
55{
56 struct serdev_device *serdev = to_serdev_device(dev);
57 kfree(serdev);
58}
59
60static const struct device_type serdev_device_type = {
Johan Hovold24609422018-01-09 17:09:16 +010061 .groups = serdev_device_groups,
62 .uevent = serdev_device_uevent,
Rob Herringcd6484e2017-02-02 13:48:07 -060063 .release = serdev_device_release,
64};
65
66static void serdev_ctrl_release(struct device *dev)
67{
68 struct serdev_controller *ctrl = to_serdev_controller(dev);
69 ida_simple_remove(&ctrl_ida, ctrl->nr);
70 kfree(ctrl);
71}
72
73static const struct device_type serdev_ctrl_type = {
74 .release = serdev_ctrl_release,
75};
76
77static int serdev_device_match(struct device *dev, struct device_driver *drv)
78{
Frédéric Danis53c76262017-10-11 10:32:13 +020079 /* TODO: platform matching */
80 if (acpi_driver_match_device(dev, drv))
81 return 1;
82
Rob Herringcd6484e2017-02-02 13:48:07 -060083 return of_driver_match_device(dev, drv);
84}
85
Rob Herringcd6484e2017-02-02 13:48:07 -060086/**
87 * serdev_device_add() - add a device previously constructed via serdev_device_alloc()
88 * @serdev: serdev_device to be added
89 */
90int serdev_device_add(struct serdev_device *serdev)
91{
Johan Hovold08fcee22017-10-10 18:09:49 +020092 struct serdev_controller *ctrl = serdev->ctrl;
Rob Herringcd6484e2017-02-02 13:48:07 -060093 struct device *parent = serdev->dev.parent;
94 int err;
95
96 dev_set_name(&serdev->dev, "%s-%d", dev_name(parent), serdev->nr);
97
Johan Hovold08fcee22017-10-10 18:09:49 +020098 /* Only a single slave device is currently supported. */
99 if (ctrl->serdev) {
100 dev_err(&serdev->dev, "controller busy\n");
101 return -EBUSY;
102 }
103 ctrl->serdev = serdev;
104
Rob Herringcd6484e2017-02-02 13:48:07 -0600105 err = device_add(&serdev->dev);
106 if (err < 0) {
107 dev_err(&serdev->dev, "Can't add %s, status %d\n",
108 dev_name(&serdev->dev), err);
Johan Hovold08fcee22017-10-10 18:09:49 +0200109 goto err_clear_serdev;
Rob Herringcd6484e2017-02-02 13:48:07 -0600110 }
111
112 dev_dbg(&serdev->dev, "device %s registered\n", dev_name(&serdev->dev));
113
Johan Hovold08fcee22017-10-10 18:09:49 +0200114 return 0;
115
116err_clear_serdev:
117 ctrl->serdev = NULL;
Rob Herringcd6484e2017-02-02 13:48:07 -0600118 return err;
119}
120EXPORT_SYMBOL_GPL(serdev_device_add);
121
122/**
123 * serdev_device_remove(): remove an serdev device
124 * @serdev: serdev_device to be removed
125 */
126void serdev_device_remove(struct serdev_device *serdev)
127{
Johan Hovold08fcee22017-10-10 18:09:49 +0200128 struct serdev_controller *ctrl = serdev->ctrl;
129
Rob Herringcd6484e2017-02-02 13:48:07 -0600130 device_unregister(&serdev->dev);
Johan Hovold08fcee22017-10-10 18:09:49 +0200131 ctrl->serdev = NULL;
Rob Herringcd6484e2017-02-02 13:48:07 -0600132}
133EXPORT_SYMBOL_GPL(serdev_device_remove);
134
135int serdev_device_open(struct serdev_device *serdev)
136{
137 struct serdev_controller *ctrl = serdev->ctrl;
138
139 if (!ctrl || !ctrl->ops->open)
140 return -EINVAL;
141
142 return ctrl->ops->open(ctrl);
143}
144EXPORT_SYMBOL_GPL(serdev_device_open);
145
146void serdev_device_close(struct serdev_device *serdev)
147{
148 struct serdev_controller *ctrl = serdev->ctrl;
149
150 if (!ctrl || !ctrl->ops->close)
151 return;
152
153 ctrl->ops->close(ctrl);
154}
155EXPORT_SYMBOL_GPL(serdev_device_close);
156
Andrey Smirnov525ba622017-11-09 08:05:53 -0800157static void devm_serdev_device_release(struct device *dev, void *dr)
158{
159 serdev_device_close(*(struct serdev_device **)dr);
160}
161
162int devm_serdev_device_open(struct device *dev, struct serdev_device *serdev)
163{
164 struct serdev_device **dr;
165 int ret;
166
167 dr = devres_alloc(devm_serdev_device_release, sizeof(*dr), GFP_KERNEL);
168 if (!dr)
169 return -ENOMEM;
170
171 ret = serdev_device_open(serdev);
172 if (ret) {
173 devres_free(dr);
174 return ret;
175 }
176
177 *dr = serdev;
178 devres_add(dev, dr);
179
180 return 0;
181}
182EXPORT_SYMBOL_GPL(devm_serdev_device_open);
183
Andrey Smirnov6fe729c2017-04-04 07:22:33 -0700184void serdev_device_write_wakeup(struct serdev_device *serdev)
185{
186 complete(&serdev->write_comp);
187}
188EXPORT_SYMBOL_GPL(serdev_device_write_wakeup);
189
Stefan Wahren6bdc00d2017-04-28 13:47:21 +0200190int serdev_device_write_buf(struct serdev_device *serdev,
191 const unsigned char *buf, size_t count)
192{
193 struct serdev_controller *ctrl = serdev->ctrl;
194
195 if (!ctrl || !ctrl->ops->write_buf)
196 return -EINVAL;
197
198 return ctrl->ops->write_buf(ctrl, buf, count);
199}
200EXPORT_SYMBOL_GPL(serdev_device_write_buf);
201
Andrey Smirnov6fe729c2017-04-04 07:22:33 -0700202int serdev_device_write(struct serdev_device *serdev,
203 const unsigned char *buf, size_t count,
204 unsigned long timeout)
Rob Herringcd6484e2017-02-02 13:48:07 -0600205{
206 struct serdev_controller *ctrl = serdev->ctrl;
Andrey Smirnov6fe729c2017-04-04 07:22:33 -0700207 int ret;
Rob Herringcd6484e2017-02-02 13:48:07 -0600208
Andrey Smirnov6fe729c2017-04-04 07:22:33 -0700209 if (!ctrl || !ctrl->ops->write_buf ||
210 (timeout && !serdev->ops->write_wakeup))
Rob Herringcd6484e2017-02-02 13:48:07 -0600211 return -EINVAL;
212
Andrey Smirnov6fe729c2017-04-04 07:22:33 -0700213 mutex_lock(&serdev->write_lock);
214 do {
215 reinit_completion(&serdev->write_comp);
216
217 ret = ctrl->ops->write_buf(ctrl, buf, count);
218 if (ret < 0)
219 break;
220
221 buf += ret;
222 count -= ret;
223
224 } while (count &&
225 (timeout = wait_for_completion_timeout(&serdev->write_comp,
226 timeout)));
227 mutex_unlock(&serdev->write_lock);
228 return ret < 0 ? ret : (count ? -ETIMEDOUT : 0);
Rob Herringcd6484e2017-02-02 13:48:07 -0600229}
Andrey Smirnov6fe729c2017-04-04 07:22:33 -0700230EXPORT_SYMBOL_GPL(serdev_device_write);
Rob Herringcd6484e2017-02-02 13:48:07 -0600231
232void serdev_device_write_flush(struct serdev_device *serdev)
233{
234 struct serdev_controller *ctrl = serdev->ctrl;
235
236 if (!ctrl || !ctrl->ops->write_flush)
237 return;
238
239 ctrl->ops->write_flush(ctrl);
240}
241EXPORT_SYMBOL_GPL(serdev_device_write_flush);
242
243int serdev_device_write_room(struct serdev_device *serdev)
244{
245 struct serdev_controller *ctrl = serdev->ctrl;
246
247 if (!ctrl || !ctrl->ops->write_room)
248 return 0;
249
250 return serdev->ctrl->ops->write_room(ctrl);
251}
252EXPORT_SYMBOL_GPL(serdev_device_write_room);
253
254unsigned int serdev_device_set_baudrate(struct serdev_device *serdev, unsigned int speed)
255{
256 struct serdev_controller *ctrl = serdev->ctrl;
257
258 if (!ctrl || !ctrl->ops->set_baudrate)
259 return 0;
260
261 return ctrl->ops->set_baudrate(ctrl, speed);
262
263}
264EXPORT_SYMBOL_GPL(serdev_device_set_baudrate);
265
266void serdev_device_set_flow_control(struct serdev_device *serdev, bool enable)
267{
268 struct serdev_controller *ctrl = serdev->ctrl;
269
270 if (!ctrl || !ctrl->ops->set_flow_control)
271 return;
272
273 ctrl->ops->set_flow_control(ctrl, enable);
274}
275EXPORT_SYMBOL_GPL(serdev_device_set_flow_control);
276
Sebastian Reichelb3f80c82017-03-28 17:59:31 +0200277void serdev_device_wait_until_sent(struct serdev_device *serdev, long timeout)
278{
279 struct serdev_controller *ctrl = serdev->ctrl;
280
281 if (!ctrl || !ctrl->ops->wait_until_sent)
282 return;
283
284 ctrl->ops->wait_until_sent(ctrl, timeout);
285}
286EXPORT_SYMBOL_GPL(serdev_device_wait_until_sent);
287
Sebastian Reichel5659dab2017-03-28 17:59:32 +0200288int serdev_device_get_tiocm(struct serdev_device *serdev)
289{
290 struct serdev_controller *ctrl = serdev->ctrl;
291
292 if (!ctrl || !ctrl->ops->get_tiocm)
293 return -ENOTSUPP;
294
295 return ctrl->ops->get_tiocm(ctrl);
296}
297EXPORT_SYMBOL_GPL(serdev_device_get_tiocm);
298
299int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear)
300{
301 struct serdev_controller *ctrl = serdev->ctrl;
302
303 if (!ctrl || !ctrl->ops->set_tiocm)
304 return -ENOTSUPP;
305
306 return ctrl->ops->set_tiocm(ctrl, set, clear);
307}
308EXPORT_SYMBOL_GPL(serdev_device_set_tiocm);
309
Rob Herringcd6484e2017-02-02 13:48:07 -0600310static int serdev_drv_probe(struct device *dev)
311{
312 const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver);
313
314 return sdrv->probe(to_serdev_device(dev));
315}
316
317static int serdev_drv_remove(struct device *dev)
318{
319 const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver);
Andrey Smirnov98869f92017-11-09 08:05:52 -0800320 if (sdrv->remove)
321 sdrv->remove(to_serdev_device(dev));
Rob Herringcd6484e2017-02-02 13:48:07 -0600322 return 0;
323}
324
Rob Herringcd6484e2017-02-02 13:48:07 -0600325static struct bus_type serdev_bus_type = {
326 .name = "serial",
327 .match = serdev_device_match,
328 .probe = serdev_drv_probe,
329 .remove = serdev_drv_remove,
Rob Herringcd6484e2017-02-02 13:48:07 -0600330};
331
332/**
333 * serdev_controller_alloc() - Allocate a new serdev device
334 * @ctrl: associated controller
335 *
336 * Caller is responsible for either calling serdev_device_add() to add the
337 * newly allocated controller, or calling serdev_device_put() to discard it.
338 */
339struct serdev_device *serdev_device_alloc(struct serdev_controller *ctrl)
340{
341 struct serdev_device *serdev;
342
343 serdev = kzalloc(sizeof(*serdev), GFP_KERNEL);
344 if (!serdev)
345 return NULL;
346
347 serdev->ctrl = ctrl;
Rob Herringcd6484e2017-02-02 13:48:07 -0600348 device_initialize(&serdev->dev);
349 serdev->dev.parent = &ctrl->dev;
350 serdev->dev.bus = &serdev_bus_type;
351 serdev->dev.type = &serdev_device_type;
Andrey Smirnov6fe729c2017-04-04 07:22:33 -0700352 init_completion(&serdev->write_comp);
353 mutex_init(&serdev->write_lock);
Rob Herringcd6484e2017-02-02 13:48:07 -0600354 return serdev;
355}
356EXPORT_SYMBOL_GPL(serdev_device_alloc);
357
358/**
359 * serdev_controller_alloc() - Allocate a new serdev controller
360 * @parent: parent device
361 * @size: size of private data
362 *
363 * Caller is responsible for either calling serdev_controller_add() to add the
364 * newly allocated controller, or calling serdev_controller_put() to discard it.
365 * The allocated private data region may be accessed via
366 * serdev_controller_get_drvdata()
367 */
368struct serdev_controller *serdev_controller_alloc(struct device *parent,
369 size_t size)
370{
371 struct serdev_controller *ctrl;
372 int id;
373
374 if (WARN_ON(!parent))
375 return NULL;
376
377 ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL);
378 if (!ctrl)
379 return NULL;
380
Johan Hovold978d6fa2017-10-12 15:28:18 +0200381 id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL);
382 if (id < 0) {
383 dev_err(parent,
384 "unable to allocate serdev controller identifier.\n");
385 goto err_free;
386 }
387
388 ctrl->nr = id;
389
Rob Herringcd6484e2017-02-02 13:48:07 -0600390 device_initialize(&ctrl->dev);
391 ctrl->dev.type = &serdev_ctrl_type;
392 ctrl->dev.bus = &serdev_bus_type;
393 ctrl->dev.parent = parent;
394 ctrl->dev.of_node = parent->of_node;
395 serdev_controller_set_drvdata(ctrl, &ctrl[1]);
396
Rob Herringcd6484e2017-02-02 13:48:07 -0600397 dev_set_name(&ctrl->dev, "serial%d", id);
398
399 dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id);
400 return ctrl;
Johan Hovold978d6fa2017-10-12 15:28:18 +0200401
402err_free:
403 kfree(ctrl);
404
405 return NULL;
Rob Herringcd6484e2017-02-02 13:48:07 -0600406}
407EXPORT_SYMBOL_GPL(serdev_controller_alloc);
408
409static int of_serdev_register_devices(struct serdev_controller *ctrl)
410{
411 struct device_node *node;
412 struct serdev_device *serdev = NULL;
413 int err;
414 bool found = false;
415
416 for_each_available_child_of_node(ctrl->dev.of_node, node) {
417 if (!of_get_property(node, "compatible", NULL))
418 continue;
419
Rob Herringa73ee842017-07-18 16:43:34 -0500420 dev_dbg(&ctrl->dev, "adding child %pOF\n", node);
Rob Herringcd6484e2017-02-02 13:48:07 -0600421
422 serdev = serdev_device_alloc(ctrl);
423 if (!serdev)
424 continue;
425
426 serdev->dev.of_node = node;
427
428 err = serdev_device_add(serdev);
429 if (err) {
430 dev_err(&serdev->dev,
431 "failure adding device. status %d\n", err);
432 serdev_device_put(serdev);
433 } else
434 found = true;
435 }
436 if (!found)
437 return -ENODEV;
438
439 return 0;
440}
441
Frédéric Danis53c76262017-10-11 10:32:13 +0200442#ifdef CONFIG_ACPI
443static acpi_status acpi_serdev_register_device(struct serdev_controller *ctrl,
444 struct acpi_device *adev)
445{
446 struct serdev_device *serdev = NULL;
447 int err;
448
449 if (acpi_bus_get_status(adev) || !adev->status.present ||
450 acpi_device_enumerated(adev))
451 return AE_OK;
452
453 serdev = serdev_device_alloc(ctrl);
454 if (!serdev) {
455 dev_err(&ctrl->dev, "failed to allocate serdev device for %s\n",
456 dev_name(&adev->dev));
457 return AE_NO_MEMORY;
458 }
459
460 ACPI_COMPANION_SET(&serdev->dev, adev);
461 acpi_device_set_enumerated(adev);
462
463 err = serdev_device_add(serdev);
464 if (err) {
465 dev_err(&serdev->dev,
466 "failure adding ACPI serdev device. status %d\n", err);
467 serdev_device_put(serdev);
468 }
469
470 return AE_OK;
471}
472
473static acpi_status acpi_serdev_add_device(acpi_handle handle, u32 level,
474 void *data, void **return_value)
475{
476 struct serdev_controller *ctrl = data;
477 struct acpi_device *adev;
478
479 if (acpi_bus_get_device(handle, &adev))
480 return AE_OK;
481
482 return acpi_serdev_register_device(ctrl, adev);
483}
484
485static int acpi_serdev_register_devices(struct serdev_controller *ctrl)
486{
487 acpi_status status;
488 acpi_handle handle;
489
490 handle = ACPI_HANDLE(ctrl->dev.parent);
491 if (!handle)
492 return -ENODEV;
493
494 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
495 acpi_serdev_add_device, NULL, ctrl, NULL);
496 if (ACPI_FAILURE(status))
497 dev_dbg(&ctrl->dev, "failed to enumerate serdev slaves\n");
498
499 if (!ctrl->serdev)
500 return -ENODEV;
501
502 return 0;
503}
504#else
505static inline int acpi_serdev_register_devices(struct serdev_controller *ctrl)
506{
507 return -ENODEV;
508}
509#endif /* CONFIG_ACPI */
510
Rob Herringcd6484e2017-02-02 13:48:07 -0600511/**
512 * serdev_controller_add() - Add an serdev controller
513 * @ctrl: controller to be registered.
514 *
515 * Register a controller previously allocated via serdev_controller_alloc() with
516 * the serdev core.
517 */
518int serdev_controller_add(struct serdev_controller *ctrl)
519{
Frédéric Danis53c76262017-10-11 10:32:13 +0200520 int ret_of, ret_acpi, ret;
Rob Herringcd6484e2017-02-02 13:48:07 -0600521
522 /* Can't register until after driver model init */
523 if (WARN_ON(!is_registered))
524 return -EAGAIN;
525
526 ret = device_add(&ctrl->dev);
527 if (ret)
528 return ret;
529
Frédéric Danis53c76262017-10-11 10:32:13 +0200530 ret_of = of_serdev_register_devices(ctrl);
531 ret_acpi = acpi_serdev_register_devices(ctrl);
532 if (ret_of && ret_acpi) {
533 dev_dbg(&ctrl->dev, "no devices registered: of:%d acpi:%d\n",
534 ret_of, ret_acpi);
535 ret = -ENODEV;
Rob Herringcd6484e2017-02-02 13:48:07 -0600536 goto out_dev_del;
Frédéric Danis53c76262017-10-11 10:32:13 +0200537 }
Rob Herringcd6484e2017-02-02 13:48:07 -0600538
539 dev_dbg(&ctrl->dev, "serdev%d registered: dev:%p\n",
540 ctrl->nr, &ctrl->dev);
541 return 0;
542
543out_dev_del:
544 device_del(&ctrl->dev);
545 return ret;
546};
547EXPORT_SYMBOL_GPL(serdev_controller_add);
548
549/* Remove a device associated with a controller */
550static int serdev_remove_device(struct device *dev, void *data)
551{
552 struct serdev_device *serdev = to_serdev_device(dev);
553 if (dev->type == &serdev_device_type)
554 serdev_device_remove(serdev);
555 return 0;
556}
557
558/**
559 * serdev_controller_remove(): remove an serdev controller
560 * @ctrl: controller to remove
561 *
562 * Remove a serdev controller. Caller is responsible for calling
563 * serdev_controller_put() to discard the allocated controller.
564 */
565void serdev_controller_remove(struct serdev_controller *ctrl)
566{
567 int dummy;
568
569 if (!ctrl)
570 return;
571
572 dummy = device_for_each_child(&ctrl->dev, NULL,
573 serdev_remove_device);
574 device_del(&ctrl->dev);
575}
576EXPORT_SYMBOL_GPL(serdev_controller_remove);
577
578/**
579 * serdev_driver_register() - Register client driver with serdev core
580 * @sdrv: client driver to be associated with client-device.
581 *
582 * This API will register the client driver with the serdev framework.
583 * It is typically called from the driver's module-init function.
584 */
585int __serdev_device_driver_register(struct serdev_device_driver *sdrv, struct module *owner)
586{
587 sdrv->driver.bus = &serdev_bus_type;
588 sdrv->driver.owner = owner;
589
590 /* force drivers to async probe so I/O is possible in probe */
591 sdrv->driver.probe_type = PROBE_PREFER_ASYNCHRONOUS;
592
593 return driver_register(&sdrv->driver);
594}
595EXPORT_SYMBOL_GPL(__serdev_device_driver_register);
596
597static void __exit serdev_exit(void)
598{
599 bus_unregister(&serdev_bus_type);
600}
601module_exit(serdev_exit);
602
603static int __init serdev_init(void)
604{
605 int ret;
606
607 ret = bus_register(&serdev_bus_type);
608 if (ret)
609 return ret;
610
611 is_registered = true;
612 return 0;
613}
614/* Must be before serial drivers register */
615postcore_initcall(serdev_init);
616
617MODULE_AUTHOR("Rob Herring <robh@kernel.org>");
618MODULE_LICENSE("GPL v2");
619MODULE_DESCRIPTION("Serial attached device bus");