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>
15 #define UCSI_DSM_UUID "6f8398c2-7ca4-11e4-ad36-631042b5008f"
16 #define UCSI_DSM_FUNC_WRITE 1
17 #define UCSI_DSM_FUNC_READ 2
26 static int ucsi_acpi_dsm(struct ucsi_acpi *ua, int func)
28 union acpi_object *obj;
30 obj = acpi_evaluate_dsm(ACPI_HANDLE(ua->dev), &ua->guid, 1, func,
33 dev_err(ua->dev, "%s: failed to evaluate _DSM %d\n",
42 static int ucsi_acpi_cmd(struct ucsi_ppm *ppm, struct ucsi_control *ctrl)
44 struct ucsi_acpi *ua = container_of(ppm, struct ucsi_acpi, ppm);
46 ppm->data->ctrl.raw_cmd = ctrl->raw_cmd;
48 return ucsi_acpi_dsm(ua, UCSI_DSM_FUNC_WRITE);
51 static int ucsi_acpi_sync(struct ucsi_ppm *ppm)
53 struct ucsi_acpi *ua = container_of(ppm, struct ucsi_acpi, ppm);
55 return ucsi_acpi_dsm(ua, UCSI_DSM_FUNC_READ);
58 static void ucsi_acpi_notify(acpi_handle handle, u32 event, void *data)
60 struct ucsi_acpi *ua = data;
62 ucsi_notify(ua->ucsi);
65 static int ucsi_acpi_probe(struct platform_device *pdev)
67 struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
76 ua = devm_kzalloc(&pdev->dev, sizeof(*ua), GFP_KERNEL);
80 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
82 dev_err(&pdev->dev, "missing memory resource\n");
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))
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().
97 ua->ppm.data = devm_ioremap(&pdev->dev, res->start, resource_size(res));
101 if (!ua->ppm.data->version)
104 ret = guid_parse(UCSI_DSM_UUID, &ua->guid);
108 ua->ppm.cmd = ucsi_acpi_cmd;
109 ua->ppm.sync = ucsi_acpi_sync;
110 ua->dev = &pdev->dev;
112 status = acpi_install_notify_handler(ACPI_HANDLE(&pdev->dev),
114 ucsi_acpi_notify, ua);
115 if (ACPI_FAILURE(status)) {
116 dev_err(&pdev->dev, "failed to install notify handler\n");
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),
125 return PTR_ERR(ua->ucsi);
128 platform_set_drvdata(pdev, ua);
133 static int ucsi_acpi_remove(struct platform_device *pdev)
135 struct ucsi_acpi *ua = platform_get_drvdata(pdev);
137 ucsi_unregister_ppm(ua->ucsi);
139 acpi_remove_notify_handler(ACPI_HANDLE(&pdev->dev), ACPI_DEVICE_NOTIFY,
145 static const struct acpi_device_id ucsi_acpi_match[] = {
149 MODULE_DEVICE_TABLE(acpi, ucsi_acpi_match);
151 static struct platform_driver ucsi_acpi_platform_driver = {
154 .acpi_match_table = ACPI_PTR(ucsi_acpi_match),
156 .probe = ucsi_acpi_probe,
157 .remove = ucsi_acpi_remove,
160 module_platform_driver(ucsi_acpi_platform_driver);
162 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
163 MODULE_LICENSE("GPL v2");
164 MODULE_DESCRIPTION("UCSI ACPI driver");