linux/drivers/usb/typec/ucsi/ucsi_acpi.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * UCSI ACPI driver
   4 *
   5 * Copyright (C) 2017, Intel Corporation
   6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
   7 */
   8
   9#include <linux/platform_device.h>
  10#include <linux/module.h>
  11#include <linux/acpi.h>
  12
  13#include "ucsi.h"
  14
  15#define UCSI_DSM_UUID           "6f8398c2-7ca4-11e4-ad36-631042b5008f"
  16#define UCSI_DSM_FUNC_WRITE     1
  17#define UCSI_DSM_FUNC_READ      2
  18
  19struct ucsi_acpi {
  20        struct device *dev;
  21        struct ucsi *ucsi;
  22        void __iomem *base;
  23        struct completion complete;
  24        unsigned long flags;
  25        guid_t guid;
  26};
  27
  28static int ucsi_acpi_dsm(struct ucsi_acpi *ua, int func)
  29{
  30        union acpi_object *obj;
  31
  32        obj = acpi_evaluate_dsm(ACPI_HANDLE(ua->dev), &ua->guid, 1, func,
  33                                NULL);
  34        if (!obj) {
  35                dev_err(ua->dev, "%s: failed to evaluate _DSM %d\n",
  36                        __func__, func);
  37                return -EIO;
  38        }
  39
  40        ACPI_FREE(obj);
  41        return 0;
  42}
  43
  44static int ucsi_acpi_read(struct ucsi *ucsi, unsigned int offset,
  45                          void *val, size_t val_len)
  46{
  47        struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi);
  48        int ret;
  49
  50        ret = ucsi_acpi_dsm(ua, UCSI_DSM_FUNC_READ);
  51        if (ret)
  52                return ret;
  53
  54        memcpy(val, (const void __force *)(ua->base + offset), val_len);
  55
  56        return 0;
  57}
  58
  59static int ucsi_acpi_async_write(struct ucsi *ucsi, unsigned int offset,
  60                                 const void *val, size_t val_len)
  61{
  62        struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi);
  63
  64        memcpy((void __force *)(ua->base + offset), val, val_len);
  65
  66        return ucsi_acpi_dsm(ua, UCSI_DSM_FUNC_WRITE);
  67}
  68
  69static int ucsi_acpi_sync_write(struct ucsi *ucsi, unsigned int offset,
  70                                const void *val, size_t val_len)
  71{
  72        struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi);
  73        int ret;
  74
  75        set_bit(COMMAND_PENDING, &ua->flags);
  76
  77        ret = ucsi_acpi_async_write(ucsi, offset, val, val_len);
  78        if (ret)
  79                goto out_clear_bit;
  80
  81        if (!wait_for_completion_timeout(&ua->complete, 60 * HZ))
  82                ret = -ETIMEDOUT;
  83
  84out_clear_bit:
  85        clear_bit(COMMAND_PENDING, &ua->flags);
  86
  87        return ret;
  88}
  89
  90static const struct ucsi_operations ucsi_acpi_ops = {
  91        .read = ucsi_acpi_read,
  92        .sync_write = ucsi_acpi_sync_write,
  93        .async_write = ucsi_acpi_async_write
  94};
  95
  96static void ucsi_acpi_notify(acpi_handle handle, u32 event, void *data)
  97{
  98        struct ucsi_acpi *ua = data;
  99        u32 cci;
 100        int ret;
 101
 102        ret = ucsi_acpi_read(ua->ucsi, UCSI_CCI, &cci, sizeof(cci));
 103        if (ret)
 104                return;
 105
 106        if (test_bit(COMMAND_PENDING, &ua->flags) &&
 107            cci & (UCSI_CCI_ACK_COMPLETE | UCSI_CCI_COMMAND_COMPLETE))
 108                complete(&ua->complete);
 109        else if (UCSI_CCI_CONNECTOR(cci))
 110                ucsi_connector_change(ua->ucsi, UCSI_CCI_CONNECTOR(cci));
 111}
 112
 113static int ucsi_acpi_probe(struct platform_device *pdev)
 114{
 115        struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
 116        struct ucsi_acpi *ua;
 117        struct resource *res;
 118        acpi_status status;
 119        int ret;
 120
 121        if (adev->dep_unmet)
 122                return -EPROBE_DEFER;
 123
 124        ua = devm_kzalloc(&pdev->dev, sizeof(*ua), GFP_KERNEL);
 125        if (!ua)
 126                return -ENOMEM;
 127
 128        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 129        if (!res) {
 130                dev_err(&pdev->dev, "missing memory resource\n");
 131                return -ENODEV;
 132        }
 133
 134        /* This will make sure we can use ioremap() */
 135        status = acpi_release_memory(ACPI_HANDLE(&pdev->dev), res, 1);
 136        if (ACPI_FAILURE(status))
 137                return -ENOMEM;
 138
 139        /*
 140         * NOTE: The memory region for the data structures is used also in an
 141         * operation region, which means ACPI has already reserved it. Therefore
 142         * it can not be requested here, and we can not use
 143         * devm_ioremap_resource().
 144         */
 145        ua->base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
 146        if (!ua->base)
 147                return -ENOMEM;
 148
 149        ret = guid_parse(UCSI_DSM_UUID, &ua->guid);
 150        if (ret)
 151                return ret;
 152
 153        init_completion(&ua->complete);
 154        ua->dev = &pdev->dev;
 155
 156        ua->ucsi = ucsi_create(&pdev->dev, &ucsi_acpi_ops);
 157        if (IS_ERR(ua->ucsi))
 158                return PTR_ERR(ua->ucsi);
 159
 160        ucsi_set_drvdata(ua->ucsi, ua);
 161
 162        status = acpi_install_notify_handler(ACPI_HANDLE(&pdev->dev),
 163                                             ACPI_DEVICE_NOTIFY,
 164                                             ucsi_acpi_notify, ua);
 165        if (ACPI_FAILURE(status)) {
 166                dev_err(&pdev->dev, "failed to install notify handler\n");
 167                ucsi_destroy(ua->ucsi);
 168                return -ENODEV;
 169        }
 170
 171        ret = ucsi_register(ua->ucsi);
 172        if (ret) {
 173                acpi_remove_notify_handler(ACPI_HANDLE(&pdev->dev),
 174                                           ACPI_DEVICE_NOTIFY,
 175                                           ucsi_acpi_notify);
 176                ucsi_destroy(ua->ucsi);
 177                return ret;
 178        }
 179
 180        platform_set_drvdata(pdev, ua);
 181
 182        return 0;
 183}
 184
 185static int ucsi_acpi_remove(struct platform_device *pdev)
 186{
 187        struct ucsi_acpi *ua = platform_get_drvdata(pdev);
 188
 189        ucsi_unregister(ua->ucsi);
 190        ucsi_destroy(ua->ucsi);
 191
 192        acpi_remove_notify_handler(ACPI_HANDLE(&pdev->dev), ACPI_DEVICE_NOTIFY,
 193                                   ucsi_acpi_notify);
 194
 195        return 0;
 196}
 197
 198static const struct acpi_device_id ucsi_acpi_match[] = {
 199        { "PNP0CA0", 0 },
 200        { },
 201};
 202MODULE_DEVICE_TABLE(acpi, ucsi_acpi_match);
 203
 204static struct platform_driver ucsi_acpi_platform_driver = {
 205        .driver = {
 206                .name = "ucsi_acpi",
 207                .acpi_match_table = ACPI_PTR(ucsi_acpi_match),
 208        },
 209        .probe = ucsi_acpi_probe,
 210        .remove = ucsi_acpi_remove,
 211};
 212
 213module_platform_driver(ucsi_acpi_platform_driver);
 214
 215MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
 216MODULE_LICENSE("GPL v2");
 217MODULE_DESCRIPTION("UCSI ACPI driver");
 218