treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / tty / serdev / core.c
blob42345e79920c229285cf95cf4c150690a5dfb04c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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.
7 */
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>
14 #include <linux/of.h>
15 #include <linux/of_device.h>
16 #include <linux/pm_domain.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/sched.h>
19 #include <linux/serdev.h>
20 #include <linux/slab.h>
22 static bool is_registered;
23 static DEFINE_IDA(ctrl_ida);
25 static ssize_t modalias_show(struct device *dev,
26 struct device_attribute *attr, char *buf)
28 int len;
30 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
31 if (len != -ENODEV)
32 return len;
34 return of_device_modalias(dev, buf, PAGE_SIZE);
36 static DEVICE_ATTR_RO(modalias);
38 static struct attribute *serdev_device_attrs[] = {
39 &dev_attr_modalias.attr,
40 NULL,
42 ATTRIBUTE_GROUPS(serdev_device);
44 static int serdev_device_uevent(struct device *dev, struct kobj_uevent_env *env)
46 int rc;
48 /* TODO: platform modalias */
50 rc = acpi_device_uevent_modalias(dev, env);
51 if (rc != -ENODEV)
52 return rc;
54 return of_device_uevent_modalias(dev, env);
57 static void serdev_device_release(struct device *dev)
59 struct serdev_device *serdev = to_serdev_device(dev);
60 kfree(serdev);
63 static const struct device_type serdev_device_type = {
64 .groups = serdev_device_groups,
65 .uevent = serdev_device_uevent,
66 .release = serdev_device_release,
69 static bool is_serdev_device(const struct device *dev)
71 return dev->type == &serdev_device_type;
74 static void serdev_ctrl_release(struct device *dev)
76 struct serdev_controller *ctrl = to_serdev_controller(dev);
77 ida_simple_remove(&ctrl_ida, ctrl->nr);
78 kfree(ctrl);
81 static const struct device_type serdev_ctrl_type = {
82 .release = serdev_ctrl_release,
85 static int serdev_device_match(struct device *dev, struct device_driver *drv)
87 if (!is_serdev_device(dev))
88 return 0;
90 /* TODO: platform matching */
91 if (acpi_driver_match_device(dev, drv))
92 return 1;
94 return of_driver_match_device(dev, drv);
97 /**
98 * serdev_device_add() - add a device previously constructed via serdev_device_alloc()
99 * @serdev: serdev_device to be added
101 int serdev_device_add(struct serdev_device *serdev)
103 struct serdev_controller *ctrl = serdev->ctrl;
104 struct device *parent = serdev->dev.parent;
105 int err;
107 dev_set_name(&serdev->dev, "%s-%d", dev_name(parent), serdev->nr);
109 /* Only a single slave device is currently supported. */
110 if (ctrl->serdev) {
111 dev_err(&serdev->dev, "controller busy\n");
112 return -EBUSY;
114 ctrl->serdev = serdev;
116 err = device_add(&serdev->dev);
117 if (err < 0) {
118 dev_err(&serdev->dev, "Can't add %s, status %pe\n",
119 dev_name(&serdev->dev), ERR_PTR(err));
120 goto err_clear_serdev;
123 dev_dbg(&serdev->dev, "device %s registered\n", dev_name(&serdev->dev));
125 return 0;
127 err_clear_serdev:
128 ctrl->serdev = NULL;
129 return err;
131 EXPORT_SYMBOL_GPL(serdev_device_add);
134 * serdev_device_remove(): remove an serdev device
135 * @serdev: serdev_device to be removed
137 void serdev_device_remove(struct serdev_device *serdev)
139 struct serdev_controller *ctrl = serdev->ctrl;
141 device_unregister(&serdev->dev);
142 ctrl->serdev = NULL;
144 EXPORT_SYMBOL_GPL(serdev_device_remove);
146 int serdev_device_open(struct serdev_device *serdev)
148 struct serdev_controller *ctrl = serdev->ctrl;
149 int ret;
151 if (!ctrl || !ctrl->ops->open)
152 return -EINVAL;
154 ret = ctrl->ops->open(ctrl);
155 if (ret)
156 return ret;
158 ret = pm_runtime_get_sync(&ctrl->dev);
159 if (ret < 0) {
160 pm_runtime_put_noidle(&ctrl->dev);
161 goto err_close;
164 return 0;
166 err_close:
167 if (ctrl->ops->close)
168 ctrl->ops->close(ctrl);
170 return ret;
172 EXPORT_SYMBOL_GPL(serdev_device_open);
174 void serdev_device_close(struct serdev_device *serdev)
176 struct serdev_controller *ctrl = serdev->ctrl;
178 if (!ctrl || !ctrl->ops->close)
179 return;
181 pm_runtime_put(&ctrl->dev);
183 ctrl->ops->close(ctrl);
185 EXPORT_SYMBOL_GPL(serdev_device_close);
187 static void devm_serdev_device_release(struct device *dev, void *dr)
189 serdev_device_close(*(struct serdev_device **)dr);
192 int devm_serdev_device_open(struct device *dev, struct serdev_device *serdev)
194 struct serdev_device **dr;
195 int ret;
197 dr = devres_alloc(devm_serdev_device_release, sizeof(*dr), GFP_KERNEL);
198 if (!dr)
199 return -ENOMEM;
201 ret = serdev_device_open(serdev);
202 if (ret) {
203 devres_free(dr);
204 return ret;
207 *dr = serdev;
208 devres_add(dev, dr);
210 return 0;
212 EXPORT_SYMBOL_GPL(devm_serdev_device_open);
214 void serdev_device_write_wakeup(struct serdev_device *serdev)
216 complete(&serdev->write_comp);
218 EXPORT_SYMBOL_GPL(serdev_device_write_wakeup);
221 * serdev_device_write_buf() - write data asynchronously
222 * @serdev: serdev device
223 * @buf: data to be written
224 * @count: number of bytes to write
226 * Write data to the device asynchronously.
228 * Note that any accepted data has only been buffered by the controller; use
229 * serdev_device_wait_until_sent() to make sure the controller write buffer
230 * has actually been emptied.
232 * Return: The number of bytes written (less than count if not enough room in
233 * the write buffer), or a negative errno on errors.
235 int serdev_device_write_buf(struct serdev_device *serdev,
236 const unsigned char *buf, size_t count)
238 struct serdev_controller *ctrl = serdev->ctrl;
240 if (!ctrl || !ctrl->ops->write_buf)
241 return -EINVAL;
243 return ctrl->ops->write_buf(ctrl, buf, count);
245 EXPORT_SYMBOL_GPL(serdev_device_write_buf);
248 * serdev_device_write() - write data synchronously
249 * @serdev: serdev device
250 * @buf: data to be written
251 * @count: number of bytes to write
252 * @timeout: timeout in jiffies, or 0 to wait indefinitely
254 * Write data to the device synchronously by repeatedly calling
255 * serdev_device_write() until the controller has accepted all data (unless
256 * interrupted by a timeout or a signal).
258 * Note that any accepted data has only been buffered by the controller; use
259 * serdev_device_wait_until_sent() to make sure the controller write buffer
260 * has actually been emptied.
262 * Note that this function depends on serdev_device_write_wakeup() being
263 * called in the serdev driver write_wakeup() callback.
265 * Return: The number of bytes written (less than count if interrupted),
266 * -ETIMEDOUT or -ERESTARTSYS if interrupted before any bytes were written, or
267 * a negative errno on errors.
269 int serdev_device_write(struct serdev_device *serdev,
270 const unsigned char *buf, size_t count,
271 long timeout)
273 struct serdev_controller *ctrl = serdev->ctrl;
274 int written = 0;
275 int ret;
277 if (!ctrl || !ctrl->ops->write_buf || !serdev->ops->write_wakeup)
278 return -EINVAL;
280 if (timeout == 0)
281 timeout = MAX_SCHEDULE_TIMEOUT;
283 mutex_lock(&serdev->write_lock);
284 do {
285 reinit_completion(&serdev->write_comp);
287 ret = ctrl->ops->write_buf(ctrl, buf, count);
288 if (ret < 0)
289 break;
291 written += ret;
292 buf += ret;
293 count -= ret;
295 if (count == 0)
296 break;
298 timeout = wait_for_completion_interruptible_timeout(&serdev->write_comp,
299 timeout);
300 } while (timeout > 0);
301 mutex_unlock(&serdev->write_lock);
303 if (ret < 0)
304 return ret;
306 if (timeout <= 0 && written == 0) {
307 if (timeout == -ERESTARTSYS)
308 return -ERESTARTSYS;
309 else
310 return -ETIMEDOUT;
313 return written;
315 EXPORT_SYMBOL_GPL(serdev_device_write);
317 void serdev_device_write_flush(struct serdev_device *serdev)
319 struct serdev_controller *ctrl = serdev->ctrl;
321 if (!ctrl || !ctrl->ops->write_flush)
322 return;
324 ctrl->ops->write_flush(ctrl);
326 EXPORT_SYMBOL_GPL(serdev_device_write_flush);
328 int serdev_device_write_room(struct serdev_device *serdev)
330 struct serdev_controller *ctrl = serdev->ctrl;
332 if (!ctrl || !ctrl->ops->write_room)
333 return 0;
335 return serdev->ctrl->ops->write_room(ctrl);
337 EXPORT_SYMBOL_GPL(serdev_device_write_room);
339 unsigned int serdev_device_set_baudrate(struct serdev_device *serdev, unsigned int speed)
341 struct serdev_controller *ctrl = serdev->ctrl;
343 if (!ctrl || !ctrl->ops->set_baudrate)
344 return 0;
346 return ctrl->ops->set_baudrate(ctrl, speed);
349 EXPORT_SYMBOL_GPL(serdev_device_set_baudrate);
351 void serdev_device_set_flow_control(struct serdev_device *serdev, bool enable)
353 struct serdev_controller *ctrl = serdev->ctrl;
355 if (!ctrl || !ctrl->ops->set_flow_control)
356 return;
358 ctrl->ops->set_flow_control(ctrl, enable);
360 EXPORT_SYMBOL_GPL(serdev_device_set_flow_control);
362 int serdev_device_set_parity(struct serdev_device *serdev,
363 enum serdev_parity parity)
365 struct serdev_controller *ctrl = serdev->ctrl;
367 if (!ctrl || !ctrl->ops->set_parity)
368 return -ENOTSUPP;
370 return ctrl->ops->set_parity(ctrl, parity);
372 EXPORT_SYMBOL_GPL(serdev_device_set_parity);
374 void serdev_device_wait_until_sent(struct serdev_device *serdev, long timeout)
376 struct serdev_controller *ctrl = serdev->ctrl;
378 if (!ctrl || !ctrl->ops->wait_until_sent)
379 return;
381 ctrl->ops->wait_until_sent(ctrl, timeout);
383 EXPORT_SYMBOL_GPL(serdev_device_wait_until_sent);
385 int serdev_device_get_tiocm(struct serdev_device *serdev)
387 struct serdev_controller *ctrl = serdev->ctrl;
389 if (!ctrl || !ctrl->ops->get_tiocm)
390 return -ENOTSUPP;
392 return ctrl->ops->get_tiocm(ctrl);
394 EXPORT_SYMBOL_GPL(serdev_device_get_tiocm);
396 int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear)
398 struct serdev_controller *ctrl = serdev->ctrl;
400 if (!ctrl || !ctrl->ops->set_tiocm)
401 return -ENOTSUPP;
403 return ctrl->ops->set_tiocm(ctrl, set, clear);
405 EXPORT_SYMBOL_GPL(serdev_device_set_tiocm);
407 static int serdev_drv_probe(struct device *dev)
409 const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver);
410 int ret;
412 ret = dev_pm_domain_attach(dev, true);
413 if (ret)
414 return ret;
416 ret = sdrv->probe(to_serdev_device(dev));
417 if (ret)
418 dev_pm_domain_detach(dev, true);
420 return ret;
423 static int serdev_drv_remove(struct device *dev)
425 const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver);
426 if (sdrv->remove)
427 sdrv->remove(to_serdev_device(dev));
429 dev_pm_domain_detach(dev, true);
431 return 0;
434 static struct bus_type serdev_bus_type = {
435 .name = "serial",
436 .match = serdev_device_match,
437 .probe = serdev_drv_probe,
438 .remove = serdev_drv_remove,
442 * serdev_device_alloc() - Allocate a new serdev device
443 * @ctrl: associated controller
445 * Caller is responsible for either calling serdev_device_add() to add the
446 * newly allocated controller, or calling serdev_device_put() to discard it.
448 struct serdev_device *serdev_device_alloc(struct serdev_controller *ctrl)
450 struct serdev_device *serdev;
452 serdev = kzalloc(sizeof(*serdev), GFP_KERNEL);
453 if (!serdev)
454 return NULL;
456 serdev->ctrl = ctrl;
457 device_initialize(&serdev->dev);
458 serdev->dev.parent = &ctrl->dev;
459 serdev->dev.bus = &serdev_bus_type;
460 serdev->dev.type = &serdev_device_type;
461 init_completion(&serdev->write_comp);
462 mutex_init(&serdev->write_lock);
463 return serdev;
465 EXPORT_SYMBOL_GPL(serdev_device_alloc);
468 * serdev_controller_alloc() - Allocate a new serdev controller
469 * @parent: parent device
470 * @size: size of private data
472 * Caller is responsible for either calling serdev_controller_add() to add the
473 * newly allocated controller, or calling serdev_controller_put() to discard it.
474 * The allocated private data region may be accessed via
475 * serdev_controller_get_drvdata()
477 struct serdev_controller *serdev_controller_alloc(struct device *parent,
478 size_t size)
480 struct serdev_controller *ctrl;
481 int id;
483 if (WARN_ON(!parent))
484 return NULL;
486 ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL);
487 if (!ctrl)
488 return NULL;
490 id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL);
491 if (id < 0) {
492 dev_err(parent,
493 "unable to allocate serdev controller identifier.\n");
494 goto err_free;
497 ctrl->nr = id;
499 device_initialize(&ctrl->dev);
500 ctrl->dev.type = &serdev_ctrl_type;
501 ctrl->dev.bus = &serdev_bus_type;
502 ctrl->dev.parent = parent;
503 ctrl->dev.of_node = parent->of_node;
504 serdev_controller_set_drvdata(ctrl, &ctrl[1]);
506 dev_set_name(&ctrl->dev, "serial%d", id);
508 pm_runtime_no_callbacks(&ctrl->dev);
509 pm_suspend_ignore_children(&ctrl->dev, true);
511 dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id);
512 return ctrl;
514 err_free:
515 kfree(ctrl);
517 return NULL;
519 EXPORT_SYMBOL_GPL(serdev_controller_alloc);
521 static int of_serdev_register_devices(struct serdev_controller *ctrl)
523 struct device_node *node;
524 struct serdev_device *serdev = NULL;
525 int err;
526 bool found = false;
528 for_each_available_child_of_node(ctrl->dev.of_node, node) {
529 if (!of_get_property(node, "compatible", NULL))
530 continue;
532 dev_dbg(&ctrl->dev, "adding child %pOF\n", node);
534 serdev = serdev_device_alloc(ctrl);
535 if (!serdev)
536 continue;
538 serdev->dev.of_node = node;
540 err = serdev_device_add(serdev);
541 if (err) {
542 dev_err(&serdev->dev,
543 "failure adding device. status %pe\n",
544 ERR_PTR(err));
545 serdev_device_put(serdev);
546 } else
547 found = true;
549 if (!found)
550 return -ENODEV;
552 return 0;
555 #ifdef CONFIG_ACPI
557 #define SERDEV_ACPI_MAX_SCAN_DEPTH 32
559 struct acpi_serdev_lookup {
560 acpi_handle device_handle;
561 acpi_handle controller_handle;
562 int n;
563 int index;
566 static int acpi_serdev_parse_resource(struct acpi_resource *ares, void *data)
568 struct acpi_serdev_lookup *lookup = data;
569 struct acpi_resource_uart_serialbus *sb;
570 acpi_status status;
572 if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
573 return 1;
575 if (ares->data.common_serial_bus.type != ACPI_RESOURCE_SERIAL_TYPE_UART)
576 return 1;
578 if (lookup->index != -1 && lookup->n++ != lookup->index)
579 return 1;
581 sb = &ares->data.uart_serial_bus;
583 status = acpi_get_handle(lookup->device_handle,
584 sb->resource_source.string_ptr,
585 &lookup->controller_handle);
586 if (ACPI_FAILURE(status))
587 return 1;
590 * NOTE: Ideally, we would also want to retreive other properties here,
591 * once setting them before opening the device is supported by serdev.
594 return 1;
597 static int acpi_serdev_do_lookup(struct acpi_device *adev,
598 struct acpi_serdev_lookup *lookup)
600 struct list_head resource_list;
601 int ret;
603 lookup->device_handle = acpi_device_handle(adev);
604 lookup->controller_handle = NULL;
605 lookup->n = 0;
607 INIT_LIST_HEAD(&resource_list);
608 ret = acpi_dev_get_resources(adev, &resource_list,
609 acpi_serdev_parse_resource, lookup);
610 acpi_dev_free_resource_list(&resource_list);
612 if (ret < 0)
613 return -EINVAL;
615 return 0;
618 static int acpi_serdev_check_resources(struct serdev_controller *ctrl,
619 struct acpi_device *adev)
621 struct acpi_serdev_lookup lookup;
622 int ret;
624 if (acpi_bus_get_status(adev) || !adev->status.present)
625 return -EINVAL;
627 /* Look for UARTSerialBusV2 resource */
628 lookup.index = -1; // we only care for the last device
630 ret = acpi_serdev_do_lookup(adev, &lookup);
631 if (ret)
632 return ret;
634 /* Make sure controller and ResourceSource handle match */
635 if (ACPI_HANDLE(ctrl->dev.parent) != lookup.controller_handle)
636 return -ENODEV;
638 return 0;
641 static acpi_status acpi_serdev_register_device(struct serdev_controller *ctrl,
642 struct acpi_device *adev)
644 struct serdev_device *serdev;
645 int err;
647 serdev = serdev_device_alloc(ctrl);
648 if (!serdev) {
649 dev_err(&ctrl->dev, "failed to allocate serdev device for %s\n",
650 dev_name(&adev->dev));
651 return AE_NO_MEMORY;
654 ACPI_COMPANION_SET(&serdev->dev, adev);
655 acpi_device_set_enumerated(adev);
657 err = serdev_device_add(serdev);
658 if (err) {
659 dev_err(&serdev->dev,
660 "failure adding ACPI serdev device. status %pe\n",
661 ERR_PTR(err));
662 serdev_device_put(serdev);
665 return AE_OK;
668 static const struct acpi_device_id serdev_acpi_devices_blacklist[] = {
669 { "INT3511", 0 },
670 { "INT3512", 0 },
671 { },
674 static acpi_status acpi_serdev_add_device(acpi_handle handle, u32 level,
675 void *data, void **return_value)
677 struct serdev_controller *ctrl = data;
678 struct acpi_device *adev;
680 if (acpi_bus_get_device(handle, &adev))
681 return AE_OK;
683 if (acpi_device_enumerated(adev))
684 return AE_OK;
686 /* Skip if black listed */
687 if (!acpi_match_device_ids(adev, serdev_acpi_devices_blacklist))
688 return AE_OK;
690 if (acpi_serdev_check_resources(ctrl, adev))
691 return AE_OK;
693 return acpi_serdev_register_device(ctrl, adev);
697 static int acpi_serdev_register_devices(struct serdev_controller *ctrl)
699 acpi_status status;
701 if (!has_acpi_companion(ctrl->dev.parent))
702 return -ENODEV;
704 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
705 SERDEV_ACPI_MAX_SCAN_DEPTH,
706 acpi_serdev_add_device, NULL, ctrl, NULL);
707 if (ACPI_FAILURE(status))
708 dev_warn(&ctrl->dev, "failed to enumerate serdev slaves\n");
710 if (!ctrl->serdev)
711 return -ENODEV;
713 return 0;
715 #else
716 static inline int acpi_serdev_register_devices(struct serdev_controller *ctrl)
718 return -ENODEV;
720 #endif /* CONFIG_ACPI */
723 * serdev_controller_add() - Add an serdev controller
724 * @ctrl: controller to be registered.
726 * Register a controller previously allocated via serdev_controller_alloc() with
727 * the serdev core.
729 int serdev_controller_add(struct serdev_controller *ctrl)
731 int ret_of, ret_acpi, ret;
733 /* Can't register until after driver model init */
734 if (WARN_ON(!is_registered))
735 return -EAGAIN;
737 ret = device_add(&ctrl->dev);
738 if (ret)
739 return ret;
741 pm_runtime_enable(&ctrl->dev);
743 ret_of = of_serdev_register_devices(ctrl);
744 ret_acpi = acpi_serdev_register_devices(ctrl);
745 if (ret_of && ret_acpi) {
746 dev_dbg(&ctrl->dev, "no devices registered: of:%pe acpi:%pe\n",
747 ERR_PTR(ret_of), ERR_PTR(ret_acpi));
748 ret = -ENODEV;
749 goto err_rpm_disable;
752 dev_dbg(&ctrl->dev, "serdev%d registered: dev:%p\n",
753 ctrl->nr, &ctrl->dev);
754 return 0;
756 err_rpm_disable:
757 pm_runtime_disable(&ctrl->dev);
758 device_del(&ctrl->dev);
759 return ret;
761 EXPORT_SYMBOL_GPL(serdev_controller_add);
763 /* Remove a device associated with a controller */
764 static int serdev_remove_device(struct device *dev, void *data)
766 struct serdev_device *serdev = to_serdev_device(dev);
767 if (dev->type == &serdev_device_type)
768 serdev_device_remove(serdev);
769 return 0;
773 * serdev_controller_remove(): remove an serdev controller
774 * @ctrl: controller to remove
776 * Remove a serdev controller. Caller is responsible for calling
777 * serdev_controller_put() to discard the allocated controller.
779 void serdev_controller_remove(struct serdev_controller *ctrl)
781 int dummy;
783 if (!ctrl)
784 return;
786 dummy = device_for_each_child(&ctrl->dev, NULL,
787 serdev_remove_device);
788 pm_runtime_disable(&ctrl->dev);
789 device_del(&ctrl->dev);
791 EXPORT_SYMBOL_GPL(serdev_controller_remove);
794 * serdev_driver_register() - Register client driver with serdev core
795 * @sdrv: client driver to be associated with client-device.
797 * This API will register the client driver with the serdev framework.
798 * It is typically called from the driver's module-init function.
800 int __serdev_device_driver_register(struct serdev_device_driver *sdrv, struct module *owner)
802 sdrv->driver.bus = &serdev_bus_type;
803 sdrv->driver.owner = owner;
805 /* force drivers to async probe so I/O is possible in probe */
806 sdrv->driver.probe_type = PROBE_PREFER_ASYNCHRONOUS;
808 return driver_register(&sdrv->driver);
810 EXPORT_SYMBOL_GPL(__serdev_device_driver_register);
812 static void __exit serdev_exit(void)
814 bus_unregister(&serdev_bus_type);
815 ida_destroy(&ctrl_ida);
817 module_exit(serdev_exit);
819 static int __init serdev_init(void)
821 int ret;
823 ret = bus_register(&serdev_bus_type);
824 if (ret)
825 return ret;
827 is_registered = true;
828 return 0;
830 /* Must be before serial drivers register */
831 postcore_initcall(serdev_init);
833 MODULE_AUTHOR("Rob Herring <robh@kernel.org>");
834 MODULE_LICENSE("GPL v2");
835 MODULE_DESCRIPTION("Serial attached device bus");