• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 __iomem *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, (const void __force *)(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((void __force *)(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 	bool ack = UCSI_COMMAND(*(u64 *)val) == UCSI_ACK_CC_CI;
74 	int ret;
75 
76 	if (ack)
77 		set_bit(ACK_PENDING, &ua->flags);
78 	else
79 		set_bit(COMMAND_PENDING, &ua->flags);
80 
81 	ret = ucsi_acpi_async_write(ucsi, offset, val, val_len);
82 	if (ret)
83 		goto out_clear_bit;
84 
85 	if (!wait_for_completion_timeout(&ua->complete, 60 * HZ))
86 		ret = -ETIMEDOUT;
87 
88 out_clear_bit:
89 	if (ack)
90 		clear_bit(ACK_PENDING, &ua->flags);
91 	else
92 		clear_bit(COMMAND_PENDING, &ua->flags);
93 
94 	return ret;
95 }
96 
97 static const struct ucsi_operations ucsi_acpi_ops = {
98 	.read = ucsi_acpi_read,
99 	.sync_write = ucsi_acpi_sync_write,
100 	.async_write = ucsi_acpi_async_write
101 };
102 
ucsi_acpi_notify(acpi_handle handle,u32 event,void * data)103 static void ucsi_acpi_notify(acpi_handle handle, u32 event, void *data)
104 {
105 	struct ucsi_acpi *ua = data;
106 	u32 cci;
107 	int ret;
108 
109 	ret = ucsi_acpi_read(ua->ucsi, UCSI_CCI, &cci, sizeof(cci));
110 	if (ret)
111 		return;
112 
113 	if (UCSI_CCI_CONNECTOR(cci))
114 		ucsi_connector_change(ua->ucsi, UCSI_CCI_CONNECTOR(cci));
115 
116 	if (cci & UCSI_CCI_ACK_COMPLETE && test_bit(ACK_PENDING, &ua->flags))
117 		complete(&ua->complete);
118 	if (cci & UCSI_CCI_COMMAND_COMPLETE &&
119 	    test_bit(COMMAND_PENDING, &ua->flags))
120 		complete(&ua->complete);
121 }
122 
ucsi_acpi_probe(struct platform_device * pdev)123 static int ucsi_acpi_probe(struct platform_device *pdev)
124 {
125 	struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
126 	struct ucsi_acpi *ua;
127 	struct resource *res;
128 	acpi_status status;
129 	int ret;
130 
131 	if (adev->dep_unmet)
132 		return -EPROBE_DEFER;
133 
134 	ua = devm_kzalloc(&pdev->dev, sizeof(*ua), GFP_KERNEL);
135 	if (!ua)
136 		return -ENOMEM;
137 
138 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
139 	if (!res) {
140 		dev_err(&pdev->dev, "missing memory resource\n");
141 		return -ENODEV;
142 	}
143 
144 	/* This will make sure we can use ioremap() */
145 	status = acpi_release_memory(ACPI_HANDLE(&pdev->dev), res, 1);
146 	if (ACPI_FAILURE(status))
147 		return -ENOMEM;
148 
149 	/*
150 	 * NOTE: The memory region for the data structures is used also in an
151 	 * operation region, which means ACPI has already reserved it. Therefore
152 	 * it can not be requested here, and we can not use
153 	 * devm_ioremap_resource().
154 	 */
155 	ua->base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
156 	if (!ua->base)
157 		return -ENOMEM;
158 
159 	ret = guid_parse(UCSI_DSM_UUID, &ua->guid);
160 	if (ret)
161 		return ret;
162 
163 	init_completion(&ua->complete);
164 	ua->dev = &pdev->dev;
165 
166 	ua->ucsi = ucsi_create(&pdev->dev, &ucsi_acpi_ops);
167 	if (IS_ERR(ua->ucsi))
168 		return PTR_ERR(ua->ucsi);
169 
170 	ucsi_set_drvdata(ua->ucsi, ua);
171 
172 	status = acpi_install_notify_handler(ACPI_HANDLE(&pdev->dev),
173 					     ACPI_DEVICE_NOTIFY,
174 					     ucsi_acpi_notify, ua);
175 	if (ACPI_FAILURE(status)) {
176 		dev_err(&pdev->dev, "failed to install notify handler\n");
177 		ucsi_destroy(ua->ucsi);
178 		return -ENODEV;
179 	}
180 
181 	ret = ucsi_register(ua->ucsi);
182 	if (ret) {
183 		acpi_remove_notify_handler(ACPI_HANDLE(&pdev->dev),
184 					   ACPI_DEVICE_NOTIFY,
185 					   ucsi_acpi_notify);
186 		ucsi_destroy(ua->ucsi);
187 		return ret;
188 	}
189 
190 	platform_set_drvdata(pdev, ua);
191 
192 	return 0;
193 }
194 
ucsi_acpi_remove(struct platform_device * pdev)195 static int ucsi_acpi_remove(struct platform_device *pdev)
196 {
197 	struct ucsi_acpi *ua = platform_get_drvdata(pdev);
198 
199 	ucsi_unregister(ua->ucsi);
200 	ucsi_destroy(ua->ucsi);
201 
202 	acpi_remove_notify_handler(ACPI_HANDLE(&pdev->dev), ACPI_DEVICE_NOTIFY,
203 				   ucsi_acpi_notify);
204 
205 	return 0;
206 }
207 
208 static const struct acpi_device_id ucsi_acpi_match[] = {
209 	{ "PNP0CA0", 0 },
210 	{ },
211 };
212 MODULE_DEVICE_TABLE(acpi, ucsi_acpi_match);
213 
214 static struct platform_driver ucsi_acpi_platform_driver = {
215 	.driver = {
216 		.name = "ucsi_acpi",
217 		.acpi_match_table = ACPI_PTR(ucsi_acpi_match),
218 	},
219 	.probe = ucsi_acpi_probe,
220 	.remove = ucsi_acpi_remove,
221 };
222 
223 module_platform_driver(ucsi_acpi_platform_driver);
224 
225 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
226 MODULE_LICENSE("GPL v2");
227 MODULE_DESCRIPTION("UCSI ACPI driver");
228