Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) 2016-2017 Linaro Ltd., Rob Herring <robh@kernel.org>
0004  *
0005  * Based on drivers/spmi/spmi.c:
0006  * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
0007  */
0008 
0009 #include <linux/acpi.h>
0010 #include <linux/errno.h>
0011 #include <linux/idr.h>
0012 #include <linux/kernel.h>
0013 #include <linux/module.h>
0014 #include <linux/of.h>
0015 #include <linux/of_device.h>
0016 #include <linux/pm_domain.h>
0017 #include <linux/pm_runtime.h>
0018 #include <linux/sched.h>
0019 #include <linux/serdev.h>
0020 #include <linux/slab.h>
0021 #include <linux/platform_data/x86/apple.h>
0022 
0023 static bool is_registered;
0024 static DEFINE_IDA(ctrl_ida);
0025 
0026 static ssize_t modalias_show(struct device *dev,
0027                  struct device_attribute *attr, char *buf)
0028 {
0029     int len;
0030 
0031     len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
0032     if (len != -ENODEV)
0033         return len;
0034 
0035     return of_device_modalias(dev, buf, PAGE_SIZE);
0036 }
0037 static DEVICE_ATTR_RO(modalias);
0038 
0039 static struct attribute *serdev_device_attrs[] = {
0040     &dev_attr_modalias.attr,
0041     NULL,
0042 };
0043 ATTRIBUTE_GROUPS(serdev_device);
0044 
0045 static int serdev_device_uevent(struct device *dev, struct kobj_uevent_env *env)
0046 {
0047     int rc;
0048 
0049     /* TODO: platform modalias */
0050 
0051     rc = acpi_device_uevent_modalias(dev, env);
0052     if (rc != -ENODEV)
0053         return rc;
0054 
0055     return of_device_uevent_modalias(dev, env);
0056 }
0057 
0058 static void serdev_device_release(struct device *dev)
0059 {
0060     struct serdev_device *serdev = to_serdev_device(dev);
0061     kfree(serdev);
0062 }
0063 
0064 static const struct device_type serdev_device_type = {
0065     .groups     = serdev_device_groups,
0066     .uevent     = serdev_device_uevent,
0067     .release    = serdev_device_release,
0068 };
0069 
0070 static bool is_serdev_device(const struct device *dev)
0071 {
0072     return dev->type == &serdev_device_type;
0073 }
0074 
0075 static void serdev_ctrl_release(struct device *dev)
0076 {
0077     struct serdev_controller *ctrl = to_serdev_controller(dev);
0078     ida_simple_remove(&ctrl_ida, ctrl->nr);
0079     kfree(ctrl);
0080 }
0081 
0082 static const struct device_type serdev_ctrl_type = {
0083     .release    = serdev_ctrl_release,
0084 };
0085 
0086 static int serdev_device_match(struct device *dev, struct device_driver *drv)
0087 {
0088     if (!is_serdev_device(dev))
0089         return 0;
0090 
0091     /* TODO: platform matching */
0092     if (acpi_driver_match_device(dev, drv))
0093         return 1;
0094 
0095     return of_driver_match_device(dev, drv);
0096 }
0097 
0098 /**
0099  * serdev_device_add() - add a device previously constructed via serdev_device_alloc()
0100  * @serdev: serdev_device to be added
0101  */
0102 int serdev_device_add(struct serdev_device *serdev)
0103 {
0104     struct serdev_controller *ctrl = serdev->ctrl;
0105     struct device *parent = serdev->dev.parent;
0106     int err;
0107 
0108     dev_set_name(&serdev->dev, "%s-%d", dev_name(parent), serdev->nr);
0109 
0110     /* Only a single slave device is currently supported. */
0111     if (ctrl->serdev) {
0112         dev_err(&serdev->dev, "controller busy\n");
0113         return -EBUSY;
0114     }
0115     ctrl->serdev = serdev;
0116 
0117     err = device_add(&serdev->dev);
0118     if (err < 0) {
0119         dev_err(&serdev->dev, "Can't add %s, status %pe\n",
0120             dev_name(&serdev->dev), ERR_PTR(err));
0121         goto err_clear_serdev;
0122     }
0123 
0124     dev_dbg(&serdev->dev, "device %s registered\n", dev_name(&serdev->dev));
0125 
0126     return 0;
0127 
0128 err_clear_serdev:
0129     ctrl->serdev = NULL;
0130     return err;
0131 }
0132 EXPORT_SYMBOL_GPL(serdev_device_add);
0133 
0134 /**
0135  * serdev_device_remove(): remove an serdev device
0136  * @serdev: serdev_device to be removed
0137  */
0138 void serdev_device_remove(struct serdev_device *serdev)
0139 {
0140     struct serdev_controller *ctrl = serdev->ctrl;
0141 
0142     device_unregister(&serdev->dev);
0143     ctrl->serdev = NULL;
0144 }
0145 EXPORT_SYMBOL_GPL(serdev_device_remove);
0146 
0147 int serdev_device_open(struct serdev_device *serdev)
0148 {
0149     struct serdev_controller *ctrl = serdev->ctrl;
0150     int ret;
0151 
0152     if (!ctrl || !ctrl->ops->open)
0153         return -EINVAL;
0154 
0155     ret = ctrl->ops->open(ctrl);
0156     if (ret)
0157         return ret;
0158 
0159     ret = pm_runtime_get_sync(&ctrl->dev);
0160     if (ret < 0) {
0161         pm_runtime_put_noidle(&ctrl->dev);
0162         goto err_close;
0163     }
0164 
0165     return 0;
0166 
0167 err_close:
0168     if (ctrl->ops->close)
0169         ctrl->ops->close(ctrl);
0170 
0171     return ret;
0172 }
0173 EXPORT_SYMBOL_GPL(serdev_device_open);
0174 
0175 void serdev_device_close(struct serdev_device *serdev)
0176 {
0177     struct serdev_controller *ctrl = serdev->ctrl;
0178 
0179     if (!ctrl || !ctrl->ops->close)
0180         return;
0181 
0182     pm_runtime_put(&ctrl->dev);
0183 
0184     ctrl->ops->close(ctrl);
0185 }
0186 EXPORT_SYMBOL_GPL(serdev_device_close);
0187 
0188 static void devm_serdev_device_release(struct device *dev, void *dr)
0189 {
0190     serdev_device_close(*(struct serdev_device **)dr);
0191 }
0192 
0193 int devm_serdev_device_open(struct device *dev, struct serdev_device *serdev)
0194 {
0195     struct serdev_device **dr;
0196     int ret;
0197 
0198     dr = devres_alloc(devm_serdev_device_release, sizeof(*dr), GFP_KERNEL);
0199     if (!dr)
0200         return -ENOMEM;
0201 
0202     ret = serdev_device_open(serdev);
0203     if (ret) {
0204         devres_free(dr);
0205         return ret;
0206     }
0207 
0208     *dr = serdev;
0209     devres_add(dev, dr);
0210 
0211     return 0;
0212 }
0213 EXPORT_SYMBOL_GPL(devm_serdev_device_open);
0214 
0215 void serdev_device_write_wakeup(struct serdev_device *serdev)
0216 {
0217     complete(&serdev->write_comp);
0218 }
0219 EXPORT_SYMBOL_GPL(serdev_device_write_wakeup);
0220 
0221 /**
0222  * serdev_device_write_buf() - write data asynchronously
0223  * @serdev: serdev device
0224  * @buf:    data to be written
0225  * @count:  number of bytes to write
0226  *
0227  * Write data to the device asynchronously.
0228  *
0229  * Note that any accepted data has only been buffered by the controller; use
0230  * serdev_device_wait_until_sent() to make sure the controller write buffer
0231  * has actually been emptied.
0232  *
0233  * Return: The number of bytes written (less than count if not enough room in
0234  * the write buffer), or a negative errno on errors.
0235  */
0236 int serdev_device_write_buf(struct serdev_device *serdev,
0237                 const unsigned char *buf, size_t count)
0238 {
0239     struct serdev_controller *ctrl = serdev->ctrl;
0240 
0241     if (!ctrl || !ctrl->ops->write_buf)
0242         return -EINVAL;
0243 
0244     return ctrl->ops->write_buf(ctrl, buf, count);
0245 }
0246 EXPORT_SYMBOL_GPL(serdev_device_write_buf);
0247 
0248 /**
0249  * serdev_device_write() - write data synchronously
0250  * @serdev: serdev device
0251  * @buf:    data to be written
0252  * @count:  number of bytes to write
0253  * @timeout:    timeout in jiffies, or 0 to wait indefinitely
0254  *
0255  * Write data to the device synchronously by repeatedly calling
0256  * serdev_device_write() until the controller has accepted all data (unless
0257  * interrupted by a timeout or a signal).
0258  *
0259  * Note that any accepted data has only been buffered by the controller; use
0260  * serdev_device_wait_until_sent() to make sure the controller write buffer
0261  * has actually been emptied.
0262  *
0263  * Note that this function depends on serdev_device_write_wakeup() being
0264  * called in the serdev driver write_wakeup() callback.
0265  *
0266  * Return: The number of bytes written (less than count if interrupted),
0267  * -ETIMEDOUT or -ERESTARTSYS if interrupted before any bytes were written, or
0268  * a negative errno on errors.
0269  */
0270 int serdev_device_write(struct serdev_device *serdev,
0271             const unsigned char *buf, size_t count,
0272             long timeout)
0273 {
0274     struct serdev_controller *ctrl = serdev->ctrl;
0275     int written = 0;
0276     int ret;
0277 
0278     if (!ctrl || !ctrl->ops->write_buf || !serdev->ops->write_wakeup)
0279         return -EINVAL;
0280 
0281     if (timeout == 0)
0282         timeout = MAX_SCHEDULE_TIMEOUT;
0283 
0284     mutex_lock(&serdev->write_lock);
0285     do {
0286         reinit_completion(&serdev->write_comp);
0287 
0288         ret = ctrl->ops->write_buf(ctrl, buf, count);
0289         if (ret < 0)
0290             break;
0291 
0292         written += ret;
0293         buf += ret;
0294         count -= ret;
0295 
0296         if (count == 0)
0297             break;
0298 
0299         timeout = wait_for_completion_interruptible_timeout(&serdev->write_comp,
0300                                     timeout);
0301     } while (timeout > 0);
0302     mutex_unlock(&serdev->write_lock);
0303 
0304     if (ret < 0)
0305         return ret;
0306 
0307     if (timeout <= 0 && written == 0) {
0308         if (timeout == -ERESTARTSYS)
0309             return -ERESTARTSYS;
0310         else
0311             return -ETIMEDOUT;
0312     }
0313 
0314     return written;
0315 }
0316 EXPORT_SYMBOL_GPL(serdev_device_write);
0317 
0318 void serdev_device_write_flush(struct serdev_device *serdev)
0319 {
0320     struct serdev_controller *ctrl = serdev->ctrl;
0321 
0322     if (!ctrl || !ctrl->ops->write_flush)
0323         return;
0324 
0325     ctrl->ops->write_flush(ctrl);
0326 }
0327 EXPORT_SYMBOL_GPL(serdev_device_write_flush);
0328 
0329 int serdev_device_write_room(struct serdev_device *serdev)
0330 {
0331     struct serdev_controller *ctrl = serdev->ctrl;
0332 
0333     if (!ctrl || !ctrl->ops->write_room)
0334         return 0;
0335 
0336     return serdev->ctrl->ops->write_room(ctrl);
0337 }
0338 EXPORT_SYMBOL_GPL(serdev_device_write_room);
0339 
0340 unsigned int serdev_device_set_baudrate(struct serdev_device *serdev, unsigned int speed)
0341 {
0342     struct serdev_controller *ctrl = serdev->ctrl;
0343 
0344     if (!ctrl || !ctrl->ops->set_baudrate)
0345         return 0;
0346 
0347     return ctrl->ops->set_baudrate(ctrl, speed);
0348 
0349 }
0350 EXPORT_SYMBOL_GPL(serdev_device_set_baudrate);
0351 
0352 void serdev_device_set_flow_control(struct serdev_device *serdev, bool enable)
0353 {
0354     struct serdev_controller *ctrl = serdev->ctrl;
0355 
0356     if (!ctrl || !ctrl->ops->set_flow_control)
0357         return;
0358 
0359     ctrl->ops->set_flow_control(ctrl, enable);
0360 }
0361 EXPORT_SYMBOL_GPL(serdev_device_set_flow_control);
0362 
0363 int serdev_device_set_parity(struct serdev_device *serdev,
0364                  enum serdev_parity parity)
0365 {
0366     struct serdev_controller *ctrl = serdev->ctrl;
0367 
0368     if (!ctrl || !ctrl->ops->set_parity)
0369         return -ENOTSUPP;
0370 
0371     return ctrl->ops->set_parity(ctrl, parity);
0372 }
0373 EXPORT_SYMBOL_GPL(serdev_device_set_parity);
0374 
0375 void serdev_device_wait_until_sent(struct serdev_device *serdev, long timeout)
0376 {
0377     struct serdev_controller *ctrl = serdev->ctrl;
0378 
0379     if (!ctrl || !ctrl->ops->wait_until_sent)
0380         return;
0381 
0382     ctrl->ops->wait_until_sent(ctrl, timeout);
0383 }
0384 EXPORT_SYMBOL_GPL(serdev_device_wait_until_sent);
0385 
0386 int serdev_device_get_tiocm(struct serdev_device *serdev)
0387 {
0388     struct serdev_controller *ctrl = serdev->ctrl;
0389 
0390     if (!ctrl || !ctrl->ops->get_tiocm)
0391         return -ENOTSUPP;
0392 
0393     return ctrl->ops->get_tiocm(ctrl);
0394 }
0395 EXPORT_SYMBOL_GPL(serdev_device_get_tiocm);
0396 
0397 int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear)
0398 {
0399     struct serdev_controller *ctrl = serdev->ctrl;
0400 
0401     if (!ctrl || !ctrl->ops->set_tiocm)
0402         return -ENOTSUPP;
0403 
0404     return ctrl->ops->set_tiocm(ctrl, set, clear);
0405 }
0406 EXPORT_SYMBOL_GPL(serdev_device_set_tiocm);
0407 
0408 static int serdev_drv_probe(struct device *dev)
0409 {
0410     const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver);
0411     int ret;
0412 
0413     ret = dev_pm_domain_attach(dev, true);
0414     if (ret)
0415         return ret;
0416 
0417     ret = sdrv->probe(to_serdev_device(dev));
0418     if (ret)
0419         dev_pm_domain_detach(dev, true);
0420 
0421     return ret;
0422 }
0423 
0424 static void serdev_drv_remove(struct device *dev)
0425 {
0426     const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver);
0427     if (sdrv->remove)
0428         sdrv->remove(to_serdev_device(dev));
0429 
0430     dev_pm_domain_detach(dev, true);
0431 }
0432 
0433 static struct bus_type serdev_bus_type = {
0434     .name       = "serial",
0435     .match      = serdev_device_match,
0436     .probe      = serdev_drv_probe,
0437     .remove     = serdev_drv_remove,
0438 };
0439 
0440 /**
0441  * serdev_device_alloc() - Allocate a new serdev device
0442  * @ctrl:   associated controller
0443  *
0444  * Caller is responsible for either calling serdev_device_add() to add the
0445  * newly allocated controller, or calling serdev_device_put() to discard it.
0446  */
0447 struct serdev_device *serdev_device_alloc(struct serdev_controller *ctrl)
0448 {
0449     struct serdev_device *serdev;
0450 
0451     serdev = kzalloc(sizeof(*serdev), GFP_KERNEL);
0452     if (!serdev)
0453         return NULL;
0454 
0455     serdev->ctrl = ctrl;
0456     device_initialize(&serdev->dev);
0457     serdev->dev.parent = &ctrl->dev;
0458     serdev->dev.bus = &serdev_bus_type;
0459     serdev->dev.type = &serdev_device_type;
0460     init_completion(&serdev->write_comp);
0461     mutex_init(&serdev->write_lock);
0462     return serdev;
0463 }
0464 EXPORT_SYMBOL_GPL(serdev_device_alloc);
0465 
0466 /**
0467  * serdev_controller_alloc() - Allocate a new serdev controller
0468  * @parent: parent device
0469  * @size:   size of private data
0470  *
0471  * Caller is responsible for either calling serdev_controller_add() to add the
0472  * newly allocated controller, or calling serdev_controller_put() to discard it.
0473  * The allocated private data region may be accessed via
0474  * serdev_controller_get_drvdata()
0475  */
0476 struct serdev_controller *serdev_controller_alloc(struct device *parent,
0477                           size_t size)
0478 {
0479     struct serdev_controller *ctrl;
0480     int id;
0481 
0482     if (WARN_ON(!parent))
0483         return NULL;
0484 
0485     ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL);
0486     if (!ctrl)
0487         return NULL;
0488 
0489     id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL);
0490     if (id < 0) {
0491         dev_err(parent,
0492             "unable to allocate serdev controller identifier.\n");
0493         goto err_free;
0494     }
0495 
0496     ctrl->nr = id;
0497 
0498     device_initialize(&ctrl->dev);
0499     ctrl->dev.type = &serdev_ctrl_type;
0500     ctrl->dev.bus = &serdev_bus_type;
0501     ctrl->dev.parent = parent;
0502     ctrl->dev.of_node = parent->of_node;
0503     serdev_controller_set_drvdata(ctrl, &ctrl[1]);
0504 
0505     dev_set_name(&ctrl->dev, "serial%d", id);
0506 
0507     pm_runtime_no_callbacks(&ctrl->dev);
0508     pm_suspend_ignore_children(&ctrl->dev, true);
0509 
0510     dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id);
0511     return ctrl;
0512 
0513 err_free:
0514     kfree(ctrl);
0515 
0516     return NULL;
0517 }
0518 EXPORT_SYMBOL_GPL(serdev_controller_alloc);
0519 
0520 static int of_serdev_register_devices(struct serdev_controller *ctrl)
0521 {
0522     struct device_node *node;
0523     struct serdev_device *serdev = NULL;
0524     int err;
0525     bool found = false;
0526 
0527     for_each_available_child_of_node(ctrl->dev.of_node, node) {
0528         if (!of_get_property(node, "compatible", NULL))
0529             continue;
0530 
0531         dev_dbg(&ctrl->dev, "adding child %pOF\n", node);
0532 
0533         serdev = serdev_device_alloc(ctrl);
0534         if (!serdev)
0535             continue;
0536 
0537         serdev->dev.of_node = node;
0538 
0539         err = serdev_device_add(serdev);
0540         if (err) {
0541             dev_err(&serdev->dev,
0542                 "failure adding device. status %pe\n",
0543                 ERR_PTR(err));
0544             serdev_device_put(serdev);
0545         } else
0546             found = true;
0547     }
0548     if (!found)
0549         return -ENODEV;
0550 
0551     return 0;
0552 }
0553 
0554 #ifdef CONFIG_ACPI
0555 
0556 #define SERDEV_ACPI_MAX_SCAN_DEPTH 32
0557 
0558 struct acpi_serdev_lookup {
0559     acpi_handle device_handle;
0560     acpi_handle controller_handle;
0561     int n;
0562     int index;
0563 };
0564 
0565 /**
0566  * serdev_acpi_get_uart_resource - Gets UARTSerialBus resource if type matches
0567  * @ares:   ACPI resource
0568  * @uart:   Pointer to UARTSerialBus resource will be returned here
0569  *
0570  * Checks if the given ACPI resource is of type UARTSerialBus.
0571  * In this case, returns a pointer to it to the caller.
0572  *
0573  * Return: True if resource type is of UARTSerialBus, otherwise false.
0574  */
0575 bool serdev_acpi_get_uart_resource(struct acpi_resource *ares,
0576                    struct acpi_resource_uart_serialbus **uart)
0577 {
0578     struct acpi_resource_uart_serialbus *sb;
0579 
0580     if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
0581         return false;
0582 
0583     sb = &ares->data.uart_serial_bus;
0584     if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_UART)
0585         return false;
0586 
0587     *uart = sb;
0588     return true;
0589 }
0590 EXPORT_SYMBOL_GPL(serdev_acpi_get_uart_resource);
0591 
0592 static int acpi_serdev_parse_resource(struct acpi_resource *ares, void *data)
0593 {
0594     struct acpi_serdev_lookup *lookup = data;
0595     struct acpi_resource_uart_serialbus *sb;
0596     acpi_status status;
0597 
0598     if (!serdev_acpi_get_uart_resource(ares, &sb))
0599         return 1;
0600 
0601     if (lookup->index != -1 && lookup->n++ != lookup->index)
0602         return 1;
0603 
0604     status = acpi_get_handle(lookup->device_handle,
0605                  sb->resource_source.string_ptr,
0606                  &lookup->controller_handle);
0607     if (ACPI_FAILURE(status))
0608         return 1;
0609 
0610     /*
0611      * NOTE: Ideally, we would also want to retrieve other properties here,
0612      * once setting them before opening the device is supported by serdev.
0613      */
0614 
0615     return 1;
0616 }
0617 
0618 static int acpi_serdev_do_lookup(struct acpi_device *adev,
0619                                  struct acpi_serdev_lookup *lookup)
0620 {
0621     struct list_head resource_list;
0622     int ret;
0623 
0624     lookup->device_handle = acpi_device_handle(adev);
0625     lookup->controller_handle = NULL;
0626     lookup->n = 0;
0627 
0628     INIT_LIST_HEAD(&resource_list);
0629     ret = acpi_dev_get_resources(adev, &resource_list,
0630                      acpi_serdev_parse_resource, lookup);
0631     acpi_dev_free_resource_list(&resource_list);
0632 
0633     if (ret < 0)
0634         return -EINVAL;
0635 
0636     return 0;
0637 }
0638 
0639 static int acpi_serdev_check_resources(struct serdev_controller *ctrl,
0640                        struct acpi_device *adev)
0641 {
0642     struct acpi_serdev_lookup lookup;
0643     int ret;
0644 
0645     if (acpi_bus_get_status(adev) || !adev->status.present)
0646         return -EINVAL;
0647 
0648     /* Look for UARTSerialBusV2 resource */
0649     lookup.index = -1;  // we only care for the last device
0650 
0651     ret = acpi_serdev_do_lookup(adev, &lookup);
0652     if (ret)
0653         return ret;
0654 
0655     /*
0656      * Apple machines provide an empty resource template, so on those
0657      * machines just look for immediate children with a "baud" property
0658      * (from the _DSM method) instead.
0659      */
0660     if (!lookup.controller_handle && x86_apple_machine &&
0661         !acpi_dev_get_property(adev, "baud", ACPI_TYPE_BUFFER, NULL))
0662         acpi_get_parent(adev->handle, &lookup.controller_handle);
0663 
0664     /* Make sure controller and ResourceSource handle match */
0665     if (ACPI_HANDLE(ctrl->dev.parent) != lookup.controller_handle)
0666         return -ENODEV;
0667 
0668     return 0;
0669 }
0670 
0671 static acpi_status acpi_serdev_register_device(struct serdev_controller *ctrl,
0672                            struct acpi_device *adev)
0673 {
0674     struct serdev_device *serdev;
0675     int err;
0676 
0677     serdev = serdev_device_alloc(ctrl);
0678     if (!serdev) {
0679         dev_err(&ctrl->dev, "failed to allocate serdev device for %s\n",
0680             dev_name(&adev->dev));
0681         return AE_NO_MEMORY;
0682     }
0683 
0684     ACPI_COMPANION_SET(&serdev->dev, adev);
0685     acpi_device_set_enumerated(adev);
0686 
0687     err = serdev_device_add(serdev);
0688     if (err) {
0689         dev_err(&serdev->dev,
0690             "failure adding ACPI serdev device. status %pe\n",
0691             ERR_PTR(err));
0692         serdev_device_put(serdev);
0693     }
0694 
0695     return AE_OK;
0696 }
0697 
0698 static const struct acpi_device_id serdev_acpi_devices_blacklist[] = {
0699     { "INT3511", 0 },
0700     { "INT3512", 0 },
0701     { },
0702 };
0703 
0704 static acpi_status acpi_serdev_add_device(acpi_handle handle, u32 level,
0705                       void *data, void **return_value)
0706 {
0707     struct acpi_device *adev = acpi_fetch_acpi_dev(handle);
0708     struct serdev_controller *ctrl = data;
0709 
0710     if (!adev || acpi_device_enumerated(adev))
0711         return AE_OK;
0712 
0713     /* Skip if black listed */
0714     if (!acpi_match_device_ids(adev, serdev_acpi_devices_blacklist))
0715         return AE_OK;
0716 
0717     if (acpi_serdev_check_resources(ctrl, adev))
0718         return AE_OK;
0719 
0720     return acpi_serdev_register_device(ctrl, adev);
0721 }
0722 
0723 
0724 static int acpi_serdev_register_devices(struct serdev_controller *ctrl)
0725 {
0726     acpi_status status;
0727     bool skip;
0728     int ret;
0729 
0730     if (!has_acpi_companion(ctrl->dev.parent))
0731         return -ENODEV;
0732 
0733     /*
0734      * Skip registration on boards where the ACPI tables are known to
0735      * contain buggy devices. Note serdev_controller_add() must still
0736      * succeed in this case, so that the proper serdev devices can be
0737      * added "manually" later.
0738      */
0739     ret = acpi_quirk_skip_serdev_enumeration(ctrl->dev.parent, &skip);
0740     if (ret)
0741         return ret;
0742     if (skip)
0743         return 0;
0744 
0745     status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
0746                      SERDEV_ACPI_MAX_SCAN_DEPTH,
0747                      acpi_serdev_add_device, NULL, ctrl, NULL);
0748     if (ACPI_FAILURE(status))
0749         dev_warn(&ctrl->dev, "failed to enumerate serdev slaves\n");
0750 
0751     if (!ctrl->serdev)
0752         return -ENODEV;
0753 
0754     return 0;
0755 }
0756 #else
0757 static inline int acpi_serdev_register_devices(struct serdev_controller *ctrl)
0758 {
0759     return -ENODEV;
0760 }
0761 #endif /* CONFIG_ACPI */
0762 
0763 /**
0764  * serdev_controller_add() - Add an serdev controller
0765  * @ctrl:   controller to be registered.
0766  *
0767  * Register a controller previously allocated via serdev_controller_alloc() with
0768  * the serdev core.
0769  */
0770 int serdev_controller_add(struct serdev_controller *ctrl)
0771 {
0772     int ret_of, ret_acpi, ret;
0773 
0774     /* Can't register until after driver model init */
0775     if (WARN_ON(!is_registered))
0776         return -EAGAIN;
0777 
0778     ret = device_add(&ctrl->dev);
0779     if (ret)
0780         return ret;
0781 
0782     pm_runtime_enable(&ctrl->dev);
0783 
0784     ret_of = of_serdev_register_devices(ctrl);
0785     ret_acpi = acpi_serdev_register_devices(ctrl);
0786     if (ret_of && ret_acpi) {
0787         dev_dbg(&ctrl->dev, "no devices registered: of:%pe acpi:%pe\n",
0788             ERR_PTR(ret_of), ERR_PTR(ret_acpi));
0789         ret = -ENODEV;
0790         goto err_rpm_disable;
0791     }
0792 
0793     dev_dbg(&ctrl->dev, "serdev%d registered: dev:%p\n",
0794         ctrl->nr, &ctrl->dev);
0795     return 0;
0796 
0797 err_rpm_disable:
0798     pm_runtime_disable(&ctrl->dev);
0799     device_del(&ctrl->dev);
0800     return ret;
0801 };
0802 EXPORT_SYMBOL_GPL(serdev_controller_add);
0803 
0804 /* Remove a device associated with a controller */
0805 static int serdev_remove_device(struct device *dev, void *data)
0806 {
0807     struct serdev_device *serdev = to_serdev_device(dev);
0808     if (dev->type == &serdev_device_type)
0809         serdev_device_remove(serdev);
0810     return 0;
0811 }
0812 
0813 /**
0814  * serdev_controller_remove(): remove an serdev controller
0815  * @ctrl:   controller to remove
0816  *
0817  * Remove a serdev controller.  Caller is responsible for calling
0818  * serdev_controller_put() to discard the allocated controller.
0819  */
0820 void serdev_controller_remove(struct serdev_controller *ctrl)
0821 {
0822     if (!ctrl)
0823         return;
0824 
0825     device_for_each_child(&ctrl->dev, NULL, serdev_remove_device);
0826     pm_runtime_disable(&ctrl->dev);
0827     device_del(&ctrl->dev);
0828 }
0829 EXPORT_SYMBOL_GPL(serdev_controller_remove);
0830 
0831 /**
0832  * __serdev_device_driver_register() - Register client driver with serdev core
0833  * @sdrv:   client driver to be associated with client-device.
0834  * @owner:  client driver owner to set.
0835  *
0836  * This API will register the client driver with the serdev framework.
0837  * It is typically called from the driver's module-init function.
0838  */
0839 int __serdev_device_driver_register(struct serdev_device_driver *sdrv, struct module *owner)
0840 {
0841     sdrv->driver.bus = &serdev_bus_type;
0842     sdrv->driver.owner = owner;
0843 
0844     /* force drivers to async probe so I/O is possible in probe */
0845         sdrv->driver.probe_type = PROBE_PREFER_ASYNCHRONOUS;
0846 
0847     return driver_register(&sdrv->driver);
0848 }
0849 EXPORT_SYMBOL_GPL(__serdev_device_driver_register);
0850 
0851 static void __exit serdev_exit(void)
0852 {
0853     bus_unregister(&serdev_bus_type);
0854     ida_destroy(&ctrl_ida);
0855 }
0856 module_exit(serdev_exit);
0857 
0858 static int __init serdev_init(void)
0859 {
0860     int ret;
0861 
0862     ret = bus_register(&serdev_bus_type);
0863     if (ret)
0864         return ret;
0865 
0866     is_registered = true;
0867     return 0;
0868 }
0869 /* Must be before serial drivers register */
0870 postcore_initcall(serdev_init);
0871 
0872 MODULE_AUTHOR("Rob Herring <robh@kernel.org>");
0873 MODULE_LICENSE("GPL v2");
0874 MODULE_DESCRIPTION("Serial attached device bus");