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 void *base;
23 struct completion complete;
24 unsigned long flags;
25 guid_t guid;
26 };
27
ucsi_acpi_dsm(struct ucsi_acpi * ua,int func)28 static 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
ucsi_acpi_read(struct ucsi * ucsi,unsigned int offset,void * val,size_t val_len)44 static 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, ua->base + offset, val_len);
55
56 return 0;
57 }
58
ucsi_acpi_async_write(struct ucsi * ucsi,unsigned int offset,const void * val,size_t val_len)59 static 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(ua->base + offset, val, val_len);
65
66 return ucsi_acpi_dsm(ua, UCSI_DSM_FUNC_WRITE);
67 }
68
ucsi_acpi_sync_write(struct ucsi * ucsi,unsigned int offset,const void * val,size_t val_len)69 static 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, HZ))
82 ret = -ETIMEDOUT;
83
84 out_clear_bit:
85 clear_bit(COMMAND_PENDING, &ua->flags);
86
87 return ret;
88 }
89
90 static 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
ucsi_acpi_notify(acpi_handle handle,u32 event,void * data)96 static 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 (UCSI_CCI_CONNECTOR(cci))
107 ucsi_connector_change(ua->ucsi, UCSI_CCI_CONNECTOR(cci));
108
109 if (test_bit(COMMAND_PENDING, &ua->flags) &&
110 cci & (UCSI_CCI_ACK_COMPLETE | UCSI_CCI_COMMAND_COMPLETE))
111 complete(&ua->complete);
112 }
113
ucsi_acpi_probe(struct platform_device * pdev)114 static int ucsi_acpi_probe(struct platform_device *pdev)
115 {
116 struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
117 struct ucsi_acpi *ua;
118 struct resource *res;
119 acpi_status status;
120 int ret;
121
122 if (adev->dep_unmet)
123 return -EPROBE_DEFER;
124
125 ua = devm_kzalloc(&pdev->dev, sizeof(*ua), GFP_KERNEL);
126 if (!ua)
127 return -ENOMEM;
128
129 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
130 if (!res) {
131 dev_err(&pdev->dev, "missing memory resource\n");
132 return -ENODEV;
133 }
134
135 ua->base = devm_memremap(&pdev->dev, res->start, resource_size(res), MEMREMAP_WB);
136 if (IS_ERR(ua->base))
137 return PTR_ERR(ua->base);
138
139 ret = guid_parse(UCSI_DSM_UUID, &ua->guid);
140 if (ret)
141 return ret;
142
143 init_completion(&ua->complete);
144 ua->dev = &pdev->dev;
145
146 ua->ucsi = ucsi_create(&pdev->dev, &ucsi_acpi_ops);
147 if (IS_ERR(ua->ucsi))
148 return PTR_ERR(ua->ucsi);
149
150 ucsi_set_drvdata(ua->ucsi, ua);
151
152 status = acpi_install_notify_handler(ACPI_HANDLE(&pdev->dev),
153 ACPI_DEVICE_NOTIFY,
154 ucsi_acpi_notify, ua);
155 if (ACPI_FAILURE(status)) {
156 dev_err(&pdev->dev, "failed to install notify handler\n");
157 ucsi_destroy(ua->ucsi);
158 return -ENODEV;
159 }
160
161 ret = ucsi_register(ua->ucsi);
162 if (ret) {
163 acpi_remove_notify_handler(ACPI_HANDLE(&pdev->dev),
164 ACPI_DEVICE_NOTIFY,
165 ucsi_acpi_notify);
166 ucsi_destroy(ua->ucsi);
167 return ret;
168 }
169
170 platform_set_drvdata(pdev, ua);
171
172 return 0;
173 }
174
ucsi_acpi_remove(struct platform_device * pdev)175 static int ucsi_acpi_remove(struct platform_device *pdev)
176 {
177 struct ucsi_acpi *ua = platform_get_drvdata(pdev);
178
179 ucsi_unregister(ua->ucsi);
180 ucsi_destroy(ua->ucsi);
181
182 acpi_remove_notify_handler(ACPI_HANDLE(&pdev->dev), ACPI_DEVICE_NOTIFY,
183 ucsi_acpi_notify);
184
185 return 0;
186 }
187
188 static const struct acpi_device_id ucsi_acpi_match[] = {
189 { "PNP0CA0", 0 },
190 { },
191 };
192 MODULE_DEVICE_TABLE(acpi, ucsi_acpi_match);
193
194 static struct platform_driver ucsi_acpi_platform_driver = {
195 .driver = {
196 .name = "ucsi_acpi",
197 .acpi_match_table = ACPI_PTR(ucsi_acpi_match),
198 },
199 .probe = ucsi_acpi_probe,
200 .remove = ucsi_acpi_remove,
201 };
202
203 module_platform_driver(ucsi_acpi_platform_driver);
204
205 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
206 MODULE_LICENSE("GPL v2");
207 MODULE_DESCRIPTION("UCSI ACPI driver");
208