1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2016-2017 Linaro Ltd., Rob Herring <robh@kernel.org>
5 * Based on drivers/spmi/spmi.c:
6 * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
9 #include <linux/acpi.h>
10 #include <linux/errno.h>
11 #include <linux/idr.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
15 #include <linux/of_device.h>
16 #include <linux/pm_domain.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/serdev.h>
19 #include <linux/slab.h>
21 static bool is_registered;
22 static DEFINE_IDA(ctrl_ida);
24 static ssize_t modalias_show(struct device *dev,
25 struct device_attribute *attr, char *buf)
29 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
33 return of_device_modalias(dev, buf, PAGE_SIZE);
35 static DEVICE_ATTR_RO(modalias);
37 static struct attribute *serdev_device_attrs[] = {
38 &dev_attr_modalias.attr,
41 ATTRIBUTE_GROUPS(serdev_device);
43 static int serdev_device_uevent(struct device *dev, struct kobj_uevent_env *env)
47 /* TODO: platform modalias */
49 rc = acpi_device_uevent_modalias(dev, env);
53 return of_device_uevent_modalias(dev, env);
56 static void serdev_device_release(struct device *dev)
58 struct serdev_device *serdev = to_serdev_device(dev);
62 static const struct device_type serdev_device_type = {
63 .groups = serdev_device_groups,
64 .uevent = serdev_device_uevent,
65 .release = serdev_device_release,
68 static bool is_serdev_device(const struct device *dev)
70 return dev->type == &serdev_device_type;
73 static void serdev_ctrl_release(struct device *dev)
75 struct serdev_controller *ctrl = to_serdev_controller(dev);
76 ida_simple_remove(&ctrl_ida, ctrl->nr);
80 static const struct device_type serdev_ctrl_type = {
81 .release = serdev_ctrl_release,
84 static int serdev_device_match(struct device *dev, struct device_driver *drv)
86 if (!is_serdev_device(dev))
89 /* TODO: platform matching */
90 if (acpi_driver_match_device(dev, drv))
93 return of_driver_match_device(dev, drv);
97 * serdev_device_add() - add a device previously constructed via serdev_device_alloc()
98 * @serdev: serdev_device to be added
100 int serdev_device_add(struct serdev_device *serdev)
102 struct serdev_controller *ctrl = serdev->ctrl;
103 struct device *parent = serdev->dev.parent;
106 dev_set_name(&serdev->dev, "%s-%d", dev_name(parent), serdev->nr);
108 /* Only a single slave device is currently supported. */
110 dev_err(&serdev->dev, "controller busy\n");
113 ctrl->serdev = serdev;
115 err = device_add(&serdev->dev);
117 dev_err(&serdev->dev, "Can't add %s, status %d\n",
118 dev_name(&serdev->dev), err);
119 goto err_clear_serdev;
122 dev_dbg(&serdev->dev, "device %s registered\n", dev_name(&serdev->dev));
130 EXPORT_SYMBOL_GPL(serdev_device_add);
133 * serdev_device_remove(): remove an serdev device
134 * @serdev: serdev_device to be removed
136 void serdev_device_remove(struct serdev_device *serdev)
138 struct serdev_controller *ctrl = serdev->ctrl;
140 device_unregister(&serdev->dev);
143 EXPORT_SYMBOL_GPL(serdev_device_remove);
145 int serdev_device_open(struct serdev_device *serdev)
147 struct serdev_controller *ctrl = serdev->ctrl;
150 if (!ctrl || !ctrl->ops->open)
153 ret = ctrl->ops->open(ctrl);
157 ret = pm_runtime_get_sync(&ctrl->dev);
159 pm_runtime_put_noidle(&ctrl->dev);
166 if (ctrl->ops->close)
167 ctrl->ops->close(ctrl);
171 EXPORT_SYMBOL_GPL(serdev_device_open);
173 void serdev_device_close(struct serdev_device *serdev)
175 struct serdev_controller *ctrl = serdev->ctrl;
177 if (!ctrl || !ctrl->ops->close)
180 pm_runtime_put(&ctrl->dev);
182 ctrl->ops->close(ctrl);
184 EXPORT_SYMBOL_GPL(serdev_device_close);
186 static void devm_serdev_device_release(struct device *dev, void *dr)
188 serdev_device_close(*(struct serdev_device **)dr);
191 int devm_serdev_device_open(struct device *dev, struct serdev_device *serdev)
193 struct serdev_device **dr;
196 dr = devres_alloc(devm_serdev_device_release, sizeof(*dr), GFP_KERNEL);
200 ret = serdev_device_open(serdev);
211 EXPORT_SYMBOL_GPL(devm_serdev_device_open);
213 void serdev_device_write_wakeup(struct serdev_device *serdev)
215 complete(&serdev->write_comp);
217 EXPORT_SYMBOL_GPL(serdev_device_write_wakeup);
219 int serdev_device_write_buf(struct serdev_device *serdev,
220 const unsigned char *buf, size_t count)
222 struct serdev_controller *ctrl = serdev->ctrl;
224 if (!ctrl || !ctrl->ops->write_buf)
227 return ctrl->ops->write_buf(ctrl, buf, count);
229 EXPORT_SYMBOL_GPL(serdev_device_write_buf);
231 int serdev_device_write(struct serdev_device *serdev,
232 const unsigned char *buf, size_t count,
233 unsigned long timeout)
235 struct serdev_controller *ctrl = serdev->ctrl;
238 if (!ctrl || !ctrl->ops->write_buf ||
239 (timeout && !serdev->ops->write_wakeup))
242 mutex_lock(&serdev->write_lock);
244 reinit_completion(&serdev->write_comp);
246 ret = ctrl->ops->write_buf(ctrl, buf, count);
254 (timeout = wait_for_completion_timeout(&serdev->write_comp,
256 mutex_unlock(&serdev->write_lock);
257 return ret < 0 ? ret : (count ? -ETIMEDOUT : 0);
259 EXPORT_SYMBOL_GPL(serdev_device_write);
261 void serdev_device_write_flush(struct serdev_device *serdev)
263 struct serdev_controller *ctrl = serdev->ctrl;
265 if (!ctrl || !ctrl->ops->write_flush)
268 ctrl->ops->write_flush(ctrl);
270 EXPORT_SYMBOL_GPL(serdev_device_write_flush);
272 int serdev_device_write_room(struct serdev_device *serdev)
274 struct serdev_controller *ctrl = serdev->ctrl;
276 if (!ctrl || !ctrl->ops->write_room)
279 return serdev->ctrl->ops->write_room(ctrl);
281 EXPORT_SYMBOL_GPL(serdev_device_write_room);
283 unsigned int serdev_device_set_baudrate(struct serdev_device *serdev, unsigned int speed)
285 struct serdev_controller *ctrl = serdev->ctrl;
287 if (!ctrl || !ctrl->ops->set_baudrate)
290 return ctrl->ops->set_baudrate(ctrl, speed);
293 EXPORT_SYMBOL_GPL(serdev_device_set_baudrate);
295 void serdev_device_set_flow_control(struct serdev_device *serdev, bool enable)
297 struct serdev_controller *ctrl = serdev->ctrl;
299 if (!ctrl || !ctrl->ops->set_flow_control)
302 ctrl->ops->set_flow_control(ctrl, enable);
304 EXPORT_SYMBOL_GPL(serdev_device_set_flow_control);
306 int serdev_device_set_parity(struct serdev_device *serdev,
307 enum serdev_parity parity)
309 struct serdev_controller *ctrl = serdev->ctrl;
311 if (!ctrl || !ctrl->ops->set_parity)
314 return ctrl->ops->set_parity(ctrl, parity);
316 EXPORT_SYMBOL_GPL(serdev_device_set_parity);
318 void serdev_device_wait_until_sent(struct serdev_device *serdev, long timeout)
320 struct serdev_controller *ctrl = serdev->ctrl;
322 if (!ctrl || !ctrl->ops->wait_until_sent)
325 ctrl->ops->wait_until_sent(ctrl, timeout);
327 EXPORT_SYMBOL_GPL(serdev_device_wait_until_sent);
329 int serdev_device_get_tiocm(struct serdev_device *serdev)
331 struct serdev_controller *ctrl = serdev->ctrl;
333 if (!ctrl || !ctrl->ops->get_tiocm)
336 return ctrl->ops->get_tiocm(ctrl);
338 EXPORT_SYMBOL_GPL(serdev_device_get_tiocm);
340 int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear)
342 struct serdev_controller *ctrl = serdev->ctrl;
344 if (!ctrl || !ctrl->ops->set_tiocm)
347 return ctrl->ops->set_tiocm(ctrl, set, clear);
349 EXPORT_SYMBOL_GPL(serdev_device_set_tiocm);
351 static int serdev_drv_probe(struct device *dev)
353 const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver);
356 ret = dev_pm_domain_attach(dev, true);
360 ret = sdrv->probe(to_serdev_device(dev));
362 dev_pm_domain_detach(dev, true);
367 static int serdev_drv_remove(struct device *dev)
369 const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver);
371 sdrv->remove(to_serdev_device(dev));
373 dev_pm_domain_detach(dev, true);
378 static struct bus_type serdev_bus_type = {
380 .match = serdev_device_match,
381 .probe = serdev_drv_probe,
382 .remove = serdev_drv_remove,
386 * serdev_device_alloc() - Allocate a new serdev device
387 * @ctrl: associated controller
389 * Caller is responsible for either calling serdev_device_add() to add the
390 * newly allocated controller, or calling serdev_device_put() to discard it.
392 struct serdev_device *serdev_device_alloc(struct serdev_controller *ctrl)
394 struct serdev_device *serdev;
396 serdev = kzalloc(sizeof(*serdev), GFP_KERNEL);
401 device_initialize(&serdev->dev);
402 serdev->dev.parent = &ctrl->dev;
403 serdev->dev.bus = &serdev_bus_type;
404 serdev->dev.type = &serdev_device_type;
405 init_completion(&serdev->write_comp);
406 mutex_init(&serdev->write_lock);
409 EXPORT_SYMBOL_GPL(serdev_device_alloc);
412 * serdev_controller_alloc() - Allocate a new serdev controller
413 * @parent: parent device
414 * @size: size of private data
416 * Caller is responsible for either calling serdev_controller_add() to add the
417 * newly allocated controller, or calling serdev_controller_put() to discard it.
418 * The allocated private data region may be accessed via
419 * serdev_controller_get_drvdata()
421 struct serdev_controller *serdev_controller_alloc(struct device *parent,
424 struct serdev_controller *ctrl;
427 if (WARN_ON(!parent))
430 ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL);
434 id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL);
437 "unable to allocate serdev controller identifier.\n");
443 device_initialize(&ctrl->dev);
444 ctrl->dev.type = &serdev_ctrl_type;
445 ctrl->dev.bus = &serdev_bus_type;
446 ctrl->dev.parent = parent;
447 ctrl->dev.of_node = parent->of_node;
448 serdev_controller_set_drvdata(ctrl, &ctrl[1]);
450 dev_set_name(&ctrl->dev, "serial%d", id);
452 pm_runtime_no_callbacks(&ctrl->dev);
453 pm_suspend_ignore_children(&ctrl->dev, true);
455 dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id);
463 EXPORT_SYMBOL_GPL(serdev_controller_alloc);
465 static int of_serdev_register_devices(struct serdev_controller *ctrl)
467 struct device_node *node;
468 struct serdev_device *serdev = NULL;
472 for_each_available_child_of_node(ctrl->dev.of_node, node) {
473 if (!of_get_property(node, "compatible", NULL))
476 dev_dbg(&ctrl->dev, "adding child %pOF\n", node);
478 serdev = serdev_device_alloc(ctrl);
482 serdev->dev.of_node = node;
484 err = serdev_device_add(serdev);
486 dev_err(&serdev->dev,
487 "failure adding device. status %d\n", err);
488 serdev_device_put(serdev);
499 static acpi_status acpi_serdev_register_device(struct serdev_controller *ctrl,
500 struct acpi_device *adev)
502 struct serdev_device *serdev = NULL;
505 if (acpi_bus_get_status(adev) || !adev->status.present ||
506 acpi_device_enumerated(adev))
509 serdev = serdev_device_alloc(ctrl);
511 dev_err(&ctrl->dev, "failed to allocate serdev device for %s\n",
512 dev_name(&adev->dev));
516 ACPI_COMPANION_SET(&serdev->dev, adev);
517 acpi_device_set_enumerated(adev);
519 err = serdev_device_add(serdev);
521 dev_err(&serdev->dev,
522 "failure adding ACPI serdev device. status %d\n", err);
523 serdev_device_put(serdev);
529 static const struct acpi_device_id serdev_acpi_devices_blacklist[] = {
535 static acpi_status acpi_serdev_add_device(acpi_handle handle, u32 level,
536 void *data, void **return_value)
538 struct serdev_controller *ctrl = data;
539 struct acpi_device *adev;
541 if (acpi_bus_get_device(handle, &adev))
544 /* Skip if black listed */
545 if (!acpi_match_device_ids(adev, serdev_acpi_devices_blacklist))
548 return acpi_serdev_register_device(ctrl, adev);
551 static int acpi_serdev_register_devices(struct serdev_controller *ctrl)
556 handle = ACPI_HANDLE(ctrl->dev.parent);
560 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
561 acpi_serdev_add_device, NULL, ctrl, NULL);
562 if (ACPI_FAILURE(status))
563 dev_dbg(&ctrl->dev, "failed to enumerate serdev slaves\n");
571 static inline int acpi_serdev_register_devices(struct serdev_controller *ctrl)
575 #endif /* CONFIG_ACPI */
578 * serdev_controller_add() - Add an serdev controller
579 * @ctrl: controller to be registered.
581 * Register a controller previously allocated via serdev_controller_alloc() with
584 int serdev_controller_add(struct serdev_controller *ctrl)
586 int ret_of, ret_acpi, ret;
588 /* Can't register until after driver model init */
589 if (WARN_ON(!is_registered))
592 ret = device_add(&ctrl->dev);
596 pm_runtime_enable(&ctrl->dev);
598 ret_of = of_serdev_register_devices(ctrl);
599 ret_acpi = acpi_serdev_register_devices(ctrl);
600 if (ret_of && ret_acpi) {
601 dev_dbg(&ctrl->dev, "no devices registered: of:%d acpi:%d\n",
604 goto err_rpm_disable;
607 dev_dbg(&ctrl->dev, "serdev%d registered: dev:%p\n",
608 ctrl->nr, &ctrl->dev);
612 pm_runtime_disable(&ctrl->dev);
613 device_del(&ctrl->dev);
616 EXPORT_SYMBOL_GPL(serdev_controller_add);
618 /* Remove a device associated with a controller */
619 static int serdev_remove_device(struct device *dev, void *data)
621 struct serdev_device *serdev = to_serdev_device(dev);
622 if (dev->type == &serdev_device_type)
623 serdev_device_remove(serdev);
628 * serdev_controller_remove(): remove an serdev controller
629 * @ctrl: controller to remove
631 * Remove a serdev controller. Caller is responsible for calling
632 * serdev_controller_put() to discard the allocated controller.
634 void serdev_controller_remove(struct serdev_controller *ctrl)
641 dummy = device_for_each_child(&ctrl->dev, NULL,
642 serdev_remove_device);
643 pm_runtime_disable(&ctrl->dev);
644 device_del(&ctrl->dev);
646 EXPORT_SYMBOL_GPL(serdev_controller_remove);
649 * serdev_driver_register() - Register client driver with serdev core
650 * @sdrv: client driver to be associated with client-device.
652 * This API will register the client driver with the serdev framework.
653 * It is typically called from the driver's module-init function.
655 int __serdev_device_driver_register(struct serdev_device_driver *sdrv, struct module *owner)
657 sdrv->driver.bus = &serdev_bus_type;
658 sdrv->driver.owner = owner;
660 /* force drivers to async probe so I/O is possible in probe */
661 sdrv->driver.probe_type = PROBE_PREFER_ASYNCHRONOUS;
663 return driver_register(&sdrv->driver);
665 EXPORT_SYMBOL_GPL(__serdev_device_driver_register);
667 static void __exit serdev_exit(void)
669 bus_unregister(&serdev_bus_type);
670 ida_destroy(&ctrl_ida);
672 module_exit(serdev_exit);
674 static int __init serdev_init(void)
678 ret = bus_register(&serdev_bus_type);
682 is_registered = true;
685 /* Must be before serial drivers register */
686 postcore_initcall(serdev_init);
688 MODULE_AUTHOR("Rob Herring <robh@kernel.org>");
689 MODULE_LICENSE("GPL v2");
690 MODULE_DESCRIPTION("Serial attached device bus");