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