Commit | Line | Data |
---|---|---|
816cab93 ZR |
1 | /* |
2 | * INT3400 thermal driver | |
3 | * | |
4 | * Copyright (C) 2014, Intel Corporation | |
5 | * Authors: Zhang Rui <rui.zhang@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 | ||
13 | #include <linux/module.h> | |
14 | #include <linux/platform_device.h> | |
15 | #include <linux/acpi.h> | |
0ab15365 | 16 | #include <linux/thermal.h> |
6306e68a | 17 | #include "acpi_thermal_rel.h" |
816cab93 | 18 | |
c5738ddd ZR |
19 | enum int3400_thermal_uuid { |
20 | INT3400_THERMAL_PASSIVE_1, | |
21 | INT3400_THERMAL_PASSIVE_2, | |
22 | INT3400_THERMAL_ACTIVE, | |
23 | INT3400_THERMAL_CRITICAL, | |
24 | INT3400_THERMAL_COOLING_MODE, | |
25 | INT3400_THERMAL_MAXIMUM_UUID, | |
26 | }; | |
27 | ||
28 | static u8 *int3400_thermal_uuids[INT3400_THERMAL_MAXIMUM_UUID] = { | |
29 | "42A441D6-AE6A-462b-A84B-4A8CE79027D3", | |
30 | "9E04115A-AE87-4D1C-9500-0F3E340BFE75", | |
31 | "3A95C389-E4B8-4629-A526-C52C88626BAE", | |
32 | "97C68AE7-15FA-499c-B8C9-5DA81D606E0A", | |
33 | "16CAF1B7-DD38-40ed-B1C1-1B8A1913D531", | |
34 | }; | |
35 | ||
816cab93 ZR |
36 | struct int3400_thermal_priv { |
37 | struct acpi_device *adev; | |
0ab15365 ZR |
38 | struct thermal_zone_device *thermal; |
39 | int mode; | |
816cab93 ZR |
40 | int art_count; |
41 | struct art *arts; | |
42 | int trt_count; | |
43 | struct trt *trts; | |
c5738ddd | 44 | u8 uuid_bitmap; |
6306e68a | 45 | int rel_misc_dev_res; |
19ecaea2 SP |
46 | int current_uuid_index; |
47 | }; | |
48 | ||
49 | static ssize_t available_uuids_show(struct device *dev, | |
50 | struct device_attribute *attr, | |
51 | char *buf) | |
52 | { | |
53 | struct platform_device *pdev = to_platform_device(dev); | |
54 | struct int3400_thermal_priv *priv = platform_get_drvdata(pdev); | |
55 | int i; | |
56 | int length = 0; | |
57 | ||
58 | for (i = 0; i < INT3400_THERMAL_MAXIMUM_UUID; i++) { | |
59 | if (priv->uuid_bitmap & (1 << i)) | |
60 | if (PAGE_SIZE - length > 0) | |
61 | length += snprintf(&buf[length], | |
62 | PAGE_SIZE - length, | |
63 | "%s\n", | |
64 | int3400_thermal_uuids[i]); | |
65 | } | |
66 | ||
67 | return length; | |
68 | } | |
69 | ||
70 | static ssize_t current_uuid_show(struct device *dev, | |
71 | struct device_attribute *devattr, char *buf) | |
72 | { | |
73 | struct platform_device *pdev = to_platform_device(dev); | |
74 | struct int3400_thermal_priv *priv = platform_get_drvdata(pdev); | |
75 | ||
76 | if (priv->uuid_bitmap & (1 << priv->current_uuid_index)) | |
77 | return sprintf(buf, "%s\n", | |
78 | int3400_thermal_uuids[priv->current_uuid_index]); | |
79 | else | |
80 | return sprintf(buf, "INVALID\n"); | |
81 | } | |
82 | ||
83 | static ssize_t current_uuid_store(struct device *dev, | |
84 | struct device_attribute *attr, | |
85 | const char *buf, size_t count) | |
86 | { | |
87 | struct platform_device *pdev = to_platform_device(dev); | |
88 | struct int3400_thermal_priv *priv = platform_get_drvdata(pdev); | |
89 | int i; | |
90 | ||
91 | for (i = 0; i < INT3400_THERMAL_MAXIMUM_UUID; ++i) { | |
92 | if ((priv->uuid_bitmap & (1 << i)) && | |
93 | !(strncmp(buf, int3400_thermal_uuids[i], | |
94 | sizeof(int3400_thermal_uuids[i]) - 1))) { | |
95 | priv->current_uuid_index = i; | |
96 | return count; | |
97 | } | |
98 | } | |
99 | ||
100 | return -EINVAL; | |
101 | } | |
102 | ||
103 | static DEVICE_ATTR(current_uuid, 0644, current_uuid_show, current_uuid_store); | |
104 | static DEVICE_ATTR_RO(available_uuids); | |
105 | static struct attribute *uuid_attrs[] = { | |
106 | &dev_attr_available_uuids.attr, | |
107 | &dev_attr_current_uuid.attr, | |
108 | NULL | |
109 | }; | |
110 | ||
111 | static struct attribute_group uuid_attribute_group = { | |
112 | .attrs = uuid_attrs, | |
113 | .name = "uuids" | |
816cab93 ZR |
114 | }; |
115 | ||
c5738ddd ZR |
116 | static int int3400_thermal_get_uuids(struct int3400_thermal_priv *priv) |
117 | { | |
118 | struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL}; | |
119 | union acpi_object *obja, *objb; | |
120 | int i, j; | |
121 | int result = 0; | |
122 | acpi_status status; | |
123 | ||
124 | status = acpi_evaluate_object(priv->adev->handle, "IDSP", NULL, &buf); | |
125 | if (ACPI_FAILURE(status)) | |
126 | return -ENODEV; | |
127 | ||
128 | obja = (union acpi_object *)buf.pointer; | |
129 | if (obja->type != ACPI_TYPE_PACKAGE) { | |
130 | result = -EINVAL; | |
131 | goto end; | |
132 | } | |
133 | ||
134 | for (i = 0; i < obja->package.count; i++) { | |
135 | objb = &obja->package.elements[i]; | |
136 | if (objb->type != ACPI_TYPE_BUFFER) { | |
137 | result = -EINVAL; | |
138 | goto end; | |
139 | } | |
140 | ||
141 | /* UUID must be 16 bytes */ | |
142 | if (objb->buffer.length != 16) { | |
143 | result = -EINVAL; | |
144 | goto end; | |
145 | } | |
146 | ||
147 | for (j = 0; j < INT3400_THERMAL_MAXIMUM_UUID; j++) { | |
148 | u8 uuid[16]; | |
149 | ||
150 | acpi_str_to_uuid(int3400_thermal_uuids[j], uuid); | |
151 | if (!strncmp(uuid, objb->buffer.pointer, 16)) { | |
152 | priv->uuid_bitmap |= (1 << j); | |
153 | break; | |
154 | } | |
155 | } | |
156 | } | |
157 | ||
158 | end: | |
159 | kfree(buf.pointer); | |
160 | return result; | |
161 | } | |
162 | ||
0ab15365 ZR |
163 | static int int3400_thermal_run_osc(acpi_handle handle, |
164 | enum int3400_thermal_uuid uuid, bool enable) | |
165 | { | |
166 | u32 ret, buf[2]; | |
167 | acpi_status status; | |
168 | int result = 0; | |
169 | struct acpi_osc_context context = { | |
170 | .uuid_str = int3400_thermal_uuids[uuid], | |
171 | .rev = 1, | |
172 | .cap.length = 8, | |
173 | }; | |
174 | ||
175 | buf[OSC_QUERY_DWORD] = 0; | |
176 | buf[OSC_SUPPORT_DWORD] = enable; | |
177 | ||
178 | context.cap.pointer = buf; | |
179 | ||
180 | status = acpi_run_osc(handle, &context); | |
181 | if (ACPI_SUCCESS(status)) { | |
182 | ret = *((u32 *)(context.ret.pointer + 4)); | |
183 | if (ret != enable) | |
184 | result = -EPERM; | |
185 | } else | |
186 | result = -EPERM; | |
187 | ||
188 | kfree(context.ret.pointer); | |
189 | return result; | |
190 | } | |
191 | ||
0ab15365 ZR |
192 | static int int3400_thermal_get_temp(struct thermal_zone_device *thermal, |
193 | unsigned long *temp) | |
194 | { | |
195 | *temp = 20 * 1000; /* faked temp sensor with 20C */ | |
196 | return 0; | |
197 | } | |
198 | ||
199 | static int int3400_thermal_get_mode(struct thermal_zone_device *thermal, | |
200 | enum thermal_device_mode *mode) | |
201 | { | |
202 | struct int3400_thermal_priv *priv = thermal->devdata; | |
203 | ||
204 | if (!priv) | |
205 | return -EINVAL; | |
206 | ||
207 | *mode = priv->mode; | |
208 | ||
209 | return 0; | |
210 | } | |
211 | ||
212 | static int int3400_thermal_set_mode(struct thermal_zone_device *thermal, | |
213 | enum thermal_device_mode mode) | |
214 | { | |
215 | struct int3400_thermal_priv *priv = thermal->devdata; | |
216 | bool enable; | |
217 | int result = 0; | |
218 | ||
219 | if (!priv) | |
220 | return -EINVAL; | |
221 | ||
222 | if (mode == THERMAL_DEVICE_ENABLED) | |
223 | enable = true; | |
224 | else if (mode == THERMAL_DEVICE_DISABLED) | |
225 | enable = false; | |
226 | else | |
227 | return -EINVAL; | |
228 | ||
229 | if (enable != priv->mode) { | |
230 | priv->mode = enable; | |
0ab15365 | 231 | result = int3400_thermal_run_osc(priv->adev->handle, |
19ecaea2 SP |
232 | priv->current_uuid_index, |
233 | enable); | |
0ab15365 ZR |
234 | } |
235 | return result; | |
236 | } | |
237 | ||
238 | static struct thermal_zone_device_ops int3400_thermal_ops = { | |
239 | .get_temp = int3400_thermal_get_temp, | |
240 | }; | |
241 | ||
242 | static struct thermal_zone_params int3400_thermal_params = { | |
243 | .governor_name = "user_space", | |
244 | .no_hwmon = true, | |
245 | }; | |
246 | ||
816cab93 ZR |
247 | static int int3400_thermal_probe(struct platform_device *pdev) |
248 | { | |
249 | struct acpi_device *adev = ACPI_COMPANION(&pdev->dev); | |
250 | struct int3400_thermal_priv *priv; | |
251 | int result; | |
252 | ||
253 | if (!adev) | |
254 | return -ENODEV; | |
255 | ||
256 | priv = kzalloc(sizeof(struct int3400_thermal_priv), GFP_KERNEL); | |
257 | if (!priv) | |
258 | return -ENOMEM; | |
259 | ||
260 | priv->adev = adev; | |
261 | ||
c5738ddd ZR |
262 | result = int3400_thermal_get_uuids(priv); |
263 | if (result) | |
264 | goto free_priv; | |
265 | ||
6306e68a JP |
266 | result = acpi_parse_art(priv->adev->handle, &priv->art_count, |
267 | &priv->arts, true); | |
816cab93 ZR |
268 | if (result) |
269 | goto free_priv; | |
270 | ||
6306e68a JP |
271 | |
272 | result = acpi_parse_trt(priv->adev->handle, &priv->trt_count, | |
273 | &priv->trts, true); | |
816cab93 ZR |
274 | if (result) |
275 | goto free_art; | |
276 | ||
277 | platform_set_drvdata(pdev, priv); | |
278 | ||
0ab15365 ZR |
279 | if (priv->uuid_bitmap & 1 << INT3400_THERMAL_PASSIVE_1) { |
280 | int3400_thermal_ops.get_mode = int3400_thermal_get_mode; | |
281 | int3400_thermal_ops.set_mode = int3400_thermal_set_mode; | |
282 | } | |
283 | priv->thermal = thermal_zone_device_register("INT3400 Thermal", 0, 0, | |
284 | priv, &int3400_thermal_ops, | |
285 | &int3400_thermal_params, 0, 0); | |
286 | if (IS_ERR(priv->thermal)) { | |
287 | result = PTR_ERR(priv->thermal); | |
288 | goto free_trt; | |
289 | } | |
290 | ||
6306e68a JP |
291 | priv->rel_misc_dev_res = acpi_thermal_rel_misc_device_add( |
292 | priv->adev->handle); | |
293 | ||
19ecaea2 SP |
294 | result = sysfs_create_group(&pdev->dev.kobj, &uuid_attribute_group); |
295 | if (result) | |
296 | goto free_zone; | |
297 | ||
816cab93 | 298 | return 0; |
19ecaea2 SP |
299 | |
300 | free_zone: | |
301 | thermal_zone_device_unregister(priv->thermal); | |
0ab15365 ZR |
302 | free_trt: |
303 | kfree(priv->trts); | |
816cab93 ZR |
304 | free_art: |
305 | kfree(priv->arts); | |
306 | free_priv: | |
307 | kfree(priv); | |
308 | return result; | |
309 | } | |
310 | ||
311 | static int int3400_thermal_remove(struct platform_device *pdev) | |
312 | { | |
313 | struct int3400_thermal_priv *priv = platform_get_drvdata(pdev); | |
314 | ||
6306e68a JP |
315 | if (!priv->rel_misc_dev_res) |
316 | acpi_thermal_rel_misc_device_remove(priv->adev->handle); | |
317 | ||
19ecaea2 | 318 | sysfs_remove_group(&pdev->dev.kobj, &uuid_attribute_group); |
0ab15365 | 319 | thermal_zone_device_unregister(priv->thermal); |
816cab93 ZR |
320 | kfree(priv->trts); |
321 | kfree(priv->arts); | |
322 | kfree(priv); | |
323 | return 0; | |
324 | } | |
325 | ||
326 | static const struct acpi_device_id int3400_thermal_match[] = { | |
327 | {"INT3400", 0}, | |
328 | {} | |
329 | }; | |
330 | ||
331 | MODULE_DEVICE_TABLE(acpi, int3400_thermal_match); | |
332 | ||
333 | static struct platform_driver int3400_thermal_driver = { | |
334 | .probe = int3400_thermal_probe, | |
335 | .remove = int3400_thermal_remove, | |
336 | .driver = { | |
337 | .name = "int3400 thermal", | |
816cab93 ZR |
338 | .acpi_match_table = ACPI_PTR(int3400_thermal_match), |
339 | }, | |
340 | }; | |
341 | ||
342 | module_platform_driver(int3400_thermal_driver); | |
343 | ||
344 | MODULE_DESCRIPTION("INT3400 Thermal driver"); | |
345 | MODULE_AUTHOR("Zhang Rui <rui.zhang@intel.com>"); | |
346 | MODULE_LICENSE("GPL"); |