4 * Copyright (C) 2017, Intel Corporation
5 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/platform_device.h>
13 #include <linux/module.h>
14 #include <linux/acpi.h>
18 #define UCSI_DSM_UUID "6f8398c2-7ca4-11e4-ad36-631042b5008f"
19 #define UCSI_DSM_FUNC_WRITE 1
20 #define UCSI_DSM_FUNC_READ 2
29 static int ucsi_acpi_dsm(struct ucsi_acpi *ua, int func)
31 union acpi_object *obj;
33 obj = acpi_evaluate_dsm(ACPI_HANDLE(ua->dev), &ua->guid, 1, func,
36 dev_err(ua->dev, "%s: failed to evaluate _DSM %d\n",
45 static int ucsi_acpi_cmd(struct ucsi_ppm *ppm, struct ucsi_control *ctrl)
47 struct ucsi_acpi *ua = container_of(ppm, struct ucsi_acpi, ppm);
49 ppm->data->ctrl.raw_cmd = ctrl->raw_cmd;
51 return ucsi_acpi_dsm(ua, UCSI_DSM_FUNC_WRITE);
54 static int ucsi_acpi_sync(struct ucsi_ppm *ppm)
56 struct ucsi_acpi *ua = container_of(ppm, struct ucsi_acpi, ppm);
58 return ucsi_acpi_dsm(ua, UCSI_DSM_FUNC_READ);
61 static void ucsi_acpi_notify(acpi_handle handle, u32 event, void *data)
63 struct ucsi_acpi *ua = data;
65 ucsi_notify(ua->ucsi);
68 static int ucsi_acpi_probe(struct platform_device *pdev)
70 struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
79 ua = devm_kzalloc(&pdev->dev, sizeof(*ua), GFP_KERNEL);
83 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
85 dev_err(&pdev->dev, "missing memory resource\n");
89 /* This will make sure we can use ioremap_nocache() */
90 status = acpi_release_memory(ACPI_HANDLE(&pdev->dev), res, 1);
91 if (ACPI_FAILURE(status))
95 * NOTE: The memory region for the data structures is used also in an
96 * operation region, which means ACPI has already reserved it. Therefore
97 * it can not be requested here, and we can not use
98 * devm_ioremap_resource().
100 ua->ppm.data = devm_ioremap(&pdev->dev, res->start, resource_size(res));
104 if (!ua->ppm.data->version)
107 ret = guid_parse(UCSI_DSM_UUID, &ua->guid);
111 ua->ppm.cmd = ucsi_acpi_cmd;
112 ua->ppm.sync = ucsi_acpi_sync;
113 ua->dev = &pdev->dev;
115 status = acpi_install_notify_handler(ACPI_HANDLE(&pdev->dev),
117 ucsi_acpi_notify, ua);
118 if (ACPI_FAILURE(status)) {
119 dev_err(&pdev->dev, "failed to install notify handler\n");
123 ua->ucsi = ucsi_register_ppm(&pdev->dev, &ua->ppm);
124 if (IS_ERR(ua->ucsi)) {
125 acpi_remove_notify_handler(ACPI_HANDLE(&pdev->dev),
128 return PTR_ERR(ua->ucsi);
131 platform_set_drvdata(pdev, ua);
136 static int ucsi_acpi_remove(struct platform_device *pdev)
138 struct ucsi_acpi *ua = platform_get_drvdata(pdev);
140 ucsi_unregister_ppm(ua->ucsi);
142 acpi_remove_notify_handler(ACPI_HANDLE(&pdev->dev), ACPI_DEVICE_NOTIFY,
148 static const struct acpi_device_id ucsi_acpi_match[] = {
152 MODULE_DEVICE_TABLE(acpi, ucsi_acpi_match);
154 static struct platform_driver ucsi_acpi_platform_driver = {
157 .acpi_match_table = ACPI_PTR(ucsi_acpi_match),
159 .probe = ucsi_acpi_probe,
160 .remove = ucsi_acpi_remove,
163 module_platform_driver(ucsi_acpi_platform_driver);
165 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
166 MODULE_LICENSE("GPL v2");
167 MODULE_DESCRIPTION("UCSI ACPI driver");