GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / usb / typec / ucsi / ucsi_acpi.c
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
19 struct ucsi_acpi {
20         struct device *dev;
21         struct ucsi *ucsi;
22         struct ucsi_ppm ppm;
23         guid_t guid;
24 };
25
26 static int ucsi_acpi_dsm(struct ucsi_acpi *ua, int func)
27 {
28         union acpi_object *obj;
29
30         obj = acpi_evaluate_dsm(ACPI_HANDLE(ua->dev), &ua->guid, 1, func,
31                                 NULL);
32         if (!obj) {
33                 dev_err(ua->dev, "%s: failed to evaluate _DSM %d\n",
34                         __func__, func);
35                 return -EIO;
36         }
37
38         ACPI_FREE(obj);
39         return 0;
40 }
41
42 static int ucsi_acpi_cmd(struct ucsi_ppm *ppm, struct ucsi_control *ctrl)
43 {
44         struct ucsi_acpi *ua = container_of(ppm, struct ucsi_acpi, ppm);
45
46         ppm->data->ctrl.raw_cmd = ctrl->raw_cmd;
47
48         return ucsi_acpi_dsm(ua, UCSI_DSM_FUNC_WRITE);
49 }
50
51 static int ucsi_acpi_sync(struct ucsi_ppm *ppm)
52 {
53         struct ucsi_acpi *ua = container_of(ppm, struct ucsi_acpi, ppm);
54
55         return ucsi_acpi_dsm(ua, UCSI_DSM_FUNC_READ);
56 }
57
58 static void ucsi_acpi_notify(acpi_handle handle, u32 event, void *data)
59 {
60         struct ucsi_acpi *ua = data;
61
62         ucsi_notify(ua->ucsi);
63 }
64
65 static int ucsi_acpi_probe(struct platform_device *pdev)
66 {
67         struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
68         struct ucsi_acpi *ua;
69         struct resource *res;
70         acpi_status status;
71         int ret;
72
73         if (adev->dep_unmet)
74                 return -EPROBE_DEFER;
75
76         ua = devm_kzalloc(&pdev->dev, sizeof(*ua), GFP_KERNEL);
77         if (!ua)
78                 return -ENOMEM;
79
80         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
81         if (!res) {
82                 dev_err(&pdev->dev, "missing memory resource\n");
83                 return -ENODEV;
84         }
85
86         /* This will make sure we can use ioremap_nocache() */
87         status = acpi_release_memory(ACPI_HANDLE(&pdev->dev), res, 1);
88         if (ACPI_FAILURE(status))
89                 return -ENOMEM;
90
91         /*
92          * NOTE: The memory region for the data structures is used also in an
93          * operation region, which means ACPI has already reserved it. Therefore
94          * it can not be requested here, and we can not use
95          * devm_ioremap_resource().
96          */
97         ua->ppm.data = devm_ioremap(&pdev->dev, res->start, resource_size(res));
98         if (!ua->ppm.data)
99                 return -ENOMEM;
100
101         if (!ua->ppm.data->version)
102                 return -ENODEV;
103
104         ret = guid_parse(UCSI_DSM_UUID, &ua->guid);
105         if (ret)
106                 return ret;
107
108         ua->ppm.cmd = ucsi_acpi_cmd;
109         ua->ppm.sync = ucsi_acpi_sync;
110         ua->dev = &pdev->dev;
111
112         status = acpi_install_notify_handler(ACPI_HANDLE(&pdev->dev),
113                                              ACPI_DEVICE_NOTIFY,
114                                              ucsi_acpi_notify, ua);
115         if (ACPI_FAILURE(status)) {
116                 dev_err(&pdev->dev, "failed to install notify handler\n");
117                 return -ENODEV;
118         }
119
120         ua->ucsi = ucsi_register_ppm(&pdev->dev, &ua->ppm);
121         if (IS_ERR(ua->ucsi)) {
122                 acpi_remove_notify_handler(ACPI_HANDLE(&pdev->dev),
123                                            ACPI_DEVICE_NOTIFY,
124                                            ucsi_acpi_notify);
125                 return PTR_ERR(ua->ucsi);
126         }
127
128         platform_set_drvdata(pdev, ua);
129
130         return 0;
131 }
132
133 static int ucsi_acpi_remove(struct platform_device *pdev)
134 {
135         struct ucsi_acpi *ua = platform_get_drvdata(pdev);
136
137         ucsi_unregister_ppm(ua->ucsi);
138
139         acpi_remove_notify_handler(ACPI_HANDLE(&pdev->dev), ACPI_DEVICE_NOTIFY,
140                                    ucsi_acpi_notify);
141
142         return 0;
143 }
144
145 static const struct acpi_device_id ucsi_acpi_match[] = {
146         { "PNP0CA0", 0 },
147         { },
148 };
149 MODULE_DEVICE_TABLE(acpi, ucsi_acpi_match);
150
151 static struct platform_driver ucsi_acpi_platform_driver = {
152         .driver = {
153                 .name = "ucsi_acpi",
154                 .acpi_match_table = ACPI_PTR(ucsi_acpi_match),
155         },
156         .probe = ucsi_acpi_probe,
157         .remove = ucsi_acpi_remove,
158 };
159
160 module_platform_driver(ucsi_acpi_platform_driver);
161
162 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
163 MODULE_LICENSE("GPL v2");
164 MODULE_DESCRIPTION("UCSI ACPI driver");