1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2017, Intel Corporation
6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
9 #include <linux/platform_device.h>
10 #include <linux/module.h>
11 #include <linux/acpi.h>
12 #include <linux/dmi.h>
16 #define UCSI_DSM_UUID "6f8398c2-7ca4-11e4-ad36-631042b5008f"
17 #define UCSI_DSM_FUNC_WRITE 1
18 #define UCSI_DSM_FUNC_READ 2
24 struct completion complete;
28 bool dell_quirk_probed;
29 bool dell_quirk_active;
32 static int ucsi_acpi_dsm(struct ucsi_acpi *ua, int func)
34 union acpi_object *obj;
36 obj = acpi_evaluate_dsm(ACPI_HANDLE(ua->dev), &ua->guid, 1, func,
39 dev_err(ua->dev, "%s: failed to evaluate _DSM %d\n",
48 static int ucsi_acpi_read(struct ucsi *ucsi, unsigned int offset,
49 void *val, size_t val_len)
51 struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi);
54 ret = ucsi_acpi_dsm(ua, UCSI_DSM_FUNC_READ);
58 memcpy(val, ua->base + offset, val_len);
63 static int ucsi_acpi_async_write(struct ucsi *ucsi, unsigned int offset,
64 const void *val, size_t val_len)
66 struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi);
68 memcpy(ua->base + offset, val, val_len);
69 ua->cmd = *(u64 *)val;
71 return ucsi_acpi_dsm(ua, UCSI_DSM_FUNC_WRITE);
74 static int ucsi_acpi_sync_write(struct ucsi *ucsi, unsigned int offset,
75 const void *val, size_t val_len)
77 struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi);
78 bool ack = UCSI_COMMAND(*(u64 *)val) == UCSI_ACK_CC_CI;
82 set_bit(ACK_PENDING, &ua->flags);
84 set_bit(COMMAND_PENDING, &ua->flags);
86 ret = ucsi_acpi_async_write(ucsi, offset, val, val_len);
90 if (!wait_for_completion_timeout(&ua->complete, 5 * HZ))
95 clear_bit(ACK_PENDING, &ua->flags);
97 clear_bit(COMMAND_PENDING, &ua->flags);
102 static const struct ucsi_operations ucsi_acpi_ops = {
103 .read = ucsi_acpi_read,
104 .sync_write = ucsi_acpi_sync_write,
105 .async_write = ucsi_acpi_async_write
109 ucsi_zenbook_read(struct ucsi *ucsi, unsigned int offset, void *val, size_t val_len)
111 struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi);
114 if (offset == UCSI_VERSION || UCSI_COMMAND(ua->cmd) == UCSI_PPM_RESET) {
115 ret = ucsi_acpi_dsm(ua, UCSI_DSM_FUNC_READ);
120 memcpy(val, ua->base + offset, val_len);
125 static const struct ucsi_operations ucsi_zenbook_ops = {
126 .read = ucsi_zenbook_read,
127 .sync_write = ucsi_acpi_sync_write,
128 .async_write = ucsi_acpi_async_write
132 * Some Dell laptops expect that an ACK command with the
133 * UCSI_ACK_CONNECTOR_CHANGE bit set is followed by a (separate)
134 * ACK command that only has the UCSI_ACK_COMMAND_COMPLETE bit set.
135 * If this is not done events are not delivered to OSPM and
136 * subsequent commands will timeout.
139 ucsi_dell_sync_write(struct ucsi *ucsi, unsigned int offset,
140 const void *val, size_t val_len)
142 struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi);
143 u64 cmd = *(u64 *)val, ack = 0;
146 if (UCSI_COMMAND(cmd) == UCSI_ACK_CC_CI &&
147 cmd & UCSI_ACK_CONNECTOR_CHANGE)
148 ack = UCSI_ACK_CC_CI | UCSI_ACK_COMMAND_COMPLETE;
150 ret = ucsi_acpi_sync_write(ucsi, offset, val, val_len);
156 if (!ua->dell_quirk_probed) {
157 ua->dell_quirk_probed = true;
159 cmd = UCSI_GET_CAPABILITY;
160 ret = ucsi_acpi_sync_write(ucsi, UCSI_CONTROL, &cmd,
163 return ucsi_acpi_sync_write(ucsi, UCSI_CONTROL,
165 if (ret != -ETIMEDOUT)
168 ua->dell_quirk_active = true;
169 dev_err(ua->dev, "Firmware bug: Additional ACK required after ACKing a connector change.\n");
170 dev_err(ua->dev, "Firmware bug: Enabling workaround\n");
173 if (!ua->dell_quirk_active)
176 return ucsi_acpi_sync_write(ucsi, UCSI_CONTROL, &ack, sizeof(ack));
179 static const struct ucsi_operations ucsi_dell_ops = {
180 .read = ucsi_acpi_read,
181 .sync_write = ucsi_dell_sync_write,
182 .async_write = ucsi_acpi_async_write
185 static const struct dmi_system_id ucsi_acpi_quirks[] = {
188 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
189 DMI_MATCH(DMI_PRODUCT_NAME, "ZenBook UX325UA_UM325UA"),
191 .driver_data = (void *)&ucsi_zenbook_ops,
195 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
197 .driver_data = (void *)&ucsi_dell_ops,
202 static void ucsi_acpi_notify(acpi_handle handle, u32 event, void *data)
204 struct ucsi_acpi *ua = data;
208 ret = ua->ucsi->ops->read(ua->ucsi, UCSI_CCI, &cci, sizeof(cci));
212 if (UCSI_CCI_CONNECTOR(cci))
213 ucsi_connector_change(ua->ucsi, UCSI_CCI_CONNECTOR(cci));
215 if (cci & UCSI_CCI_ACK_COMPLETE && test_bit(ACK_PENDING, &ua->flags))
216 complete(&ua->complete);
217 if (cci & UCSI_CCI_COMMAND_COMPLETE &&
218 test_bit(COMMAND_PENDING, &ua->flags))
219 complete(&ua->complete);
222 static int ucsi_acpi_probe(struct platform_device *pdev)
224 struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
225 const struct ucsi_operations *ops = &ucsi_acpi_ops;
226 const struct dmi_system_id *id;
227 struct ucsi_acpi *ua;
228 struct resource *res;
233 return -EPROBE_DEFER;
235 ua = devm_kzalloc(&pdev->dev, sizeof(*ua), GFP_KERNEL);
239 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
241 dev_err(&pdev->dev, "missing memory resource\n");
245 ua->base = devm_memremap(&pdev->dev, res->start, resource_size(res), MEMREMAP_WB);
246 if (IS_ERR(ua->base))
247 return PTR_ERR(ua->base);
249 ret = guid_parse(UCSI_DSM_UUID, &ua->guid);
253 init_completion(&ua->complete);
254 ua->dev = &pdev->dev;
256 id = dmi_first_match(ucsi_acpi_quirks);
258 ops = id->driver_data;
260 ua->ucsi = ucsi_create(&pdev->dev, ops);
261 if (IS_ERR(ua->ucsi))
262 return PTR_ERR(ua->ucsi);
264 ucsi_set_drvdata(ua->ucsi, ua);
266 status = acpi_install_notify_handler(ACPI_HANDLE(&pdev->dev),
268 ucsi_acpi_notify, ua);
269 if (ACPI_FAILURE(status)) {
270 dev_err(&pdev->dev, "failed to install notify handler\n");
271 ucsi_destroy(ua->ucsi);
275 ret = ucsi_register(ua->ucsi);
277 acpi_remove_notify_handler(ACPI_HANDLE(&pdev->dev),
280 ucsi_destroy(ua->ucsi);
284 platform_set_drvdata(pdev, ua);
289 static void ucsi_acpi_remove(struct platform_device *pdev)
291 struct ucsi_acpi *ua = platform_get_drvdata(pdev);
293 ucsi_unregister(ua->ucsi);
294 ucsi_destroy(ua->ucsi);
296 acpi_remove_notify_handler(ACPI_HANDLE(&pdev->dev), ACPI_DEVICE_NOTIFY,
300 static int ucsi_acpi_resume(struct device *dev)
302 struct ucsi_acpi *ua = dev_get_drvdata(dev);
304 return ucsi_resume(ua->ucsi);
307 static DEFINE_SIMPLE_DEV_PM_OPS(ucsi_acpi_pm_ops, NULL, ucsi_acpi_resume);
309 static const struct acpi_device_id ucsi_acpi_match[] = {
313 MODULE_DEVICE_TABLE(acpi, ucsi_acpi_match);
315 static struct platform_driver ucsi_acpi_platform_driver = {
318 .pm = pm_ptr(&ucsi_acpi_pm_ops),
319 .acpi_match_table = ACPI_PTR(ucsi_acpi_match),
321 .probe = ucsi_acpi_probe,
322 .remove_new = ucsi_acpi_remove,
325 module_platform_driver(ucsi_acpi_platform_driver);
327 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
328 MODULE_LICENSE("GPL v2");
329 MODULE_DESCRIPTION("UCSI ACPI driver");