• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
16 #include <linux/thermal.h>
17 #include "acpi_thermal_rel.h"
18 
19 #define INT3400_THERMAL_TABLE_CHANGED 0x83
20 
21 enum int3400_thermal_uuid {
22 	INT3400_THERMAL_PASSIVE_1,
23 	INT3400_THERMAL_ACTIVE,
24 	INT3400_THERMAL_CRITICAL,
25 	INT3400_THERMAL_ADAPTIVE_PERFORMANCE,
26 	INT3400_THERMAL_EMERGENCY_CALL_MODE,
27 	INT3400_THERMAL_PASSIVE_2,
28 	INT3400_THERMAL_POWER_BOSS,
29 	INT3400_THERMAL_VIRTUAL_SENSOR,
30 	INT3400_THERMAL_COOLING_MODE,
31 	INT3400_THERMAL_HARDWARE_DUTY_CYCLING,
32 	INT3400_THERMAL_MAXIMUM_UUID,
33 };
34 
35 static char *int3400_thermal_uuids[INT3400_THERMAL_MAXIMUM_UUID] = {
36 	"42A441D6-AE6A-462b-A84B-4A8CE79027D3",
37 	"3A95C389-E4B8-4629-A526-C52C88626BAE",
38 	"97C68AE7-15FA-499c-B8C9-5DA81D606E0A",
39 	"63BE270F-1C11-48FD-A6F7-3AF253FF3E2D",
40 	"5349962F-71E6-431D-9AE8-0A635B710AEE",
41 	"9E04115A-AE87-4D1C-9500-0F3E340BFE75",
42 	"F5A35014-C209-46A4-993A-EB56DE7530A1",
43 	"6ED722A7-9240-48A5-B479-31EEF723D7CF",
44 	"16CAF1B7-DD38-40ED-B1C1-1B8A1913D531",
45 	"BE84BABF-C4D4-403D-B495-3128FD44dAC1",
46 };
47 
48 struct int3400_thermal_priv {
49 	struct acpi_device *adev;
50 	struct thermal_zone_device *thermal;
51 	int mode;
52 	int art_count;
53 	struct art *arts;
54 	int trt_count;
55 	struct trt *trts;
56 	u8 uuid_bitmap;
57 	int rel_misc_dev_res;
58 	int current_uuid_index;
59 };
60 
available_uuids_show(struct device * dev,struct device_attribute * attr,char * buf)61 static ssize_t available_uuids_show(struct device *dev,
62 				    struct device_attribute *attr,
63 				    char *buf)
64 {
65 	struct platform_device *pdev = to_platform_device(dev);
66 	struct int3400_thermal_priv *priv = platform_get_drvdata(pdev);
67 	int i;
68 	int length = 0;
69 
70 	for (i = 0; i < INT3400_THERMAL_MAXIMUM_UUID; i++) {
71 		if (priv->uuid_bitmap & (1 << i))
72 			if (PAGE_SIZE - length > 0)
73 				length += snprintf(&buf[length],
74 						   PAGE_SIZE - length,
75 						   "%s\n",
76 						   int3400_thermal_uuids[i]);
77 	}
78 
79 	return length;
80 }
81 
current_uuid_show(struct device * dev,struct device_attribute * devattr,char * buf)82 static ssize_t current_uuid_show(struct device *dev,
83 				 struct device_attribute *devattr, char *buf)
84 {
85 	struct platform_device *pdev = to_platform_device(dev);
86 	struct int3400_thermal_priv *priv = platform_get_drvdata(pdev);
87 
88 	if (priv->uuid_bitmap & (1 << priv->current_uuid_index))
89 		return sprintf(buf, "%s\n",
90 			       int3400_thermal_uuids[priv->current_uuid_index]);
91 	else
92 		return sprintf(buf, "INVALID\n");
93 }
94 
current_uuid_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)95 static ssize_t current_uuid_store(struct device *dev,
96 				  struct device_attribute *attr,
97 				  const char *buf, size_t count)
98 {
99 	struct platform_device *pdev = to_platform_device(dev);
100 	struct int3400_thermal_priv *priv = platform_get_drvdata(pdev);
101 	int i;
102 
103 	for (i = 0; i < INT3400_THERMAL_MAXIMUM_UUID; ++i) {
104 		if ((priv->uuid_bitmap & (1 << i)) &&
105 		    !(strncmp(buf, int3400_thermal_uuids[i],
106 			      sizeof(int3400_thermal_uuids[i]) - 1))) {
107 			priv->current_uuid_index = i;
108 			return count;
109 		}
110 	}
111 
112 	return -EINVAL;
113 }
114 
115 static DEVICE_ATTR_RW(current_uuid);
116 static DEVICE_ATTR_RO(available_uuids);
117 static struct attribute *uuid_attrs[] = {
118 	&dev_attr_available_uuids.attr,
119 	&dev_attr_current_uuid.attr,
120 	NULL
121 };
122 
123 static const struct attribute_group uuid_attribute_group = {
124 	.attrs = uuid_attrs,
125 	.name = "uuids"
126 };
127 
int3400_thermal_get_uuids(struct int3400_thermal_priv * priv)128 static int int3400_thermal_get_uuids(struct int3400_thermal_priv *priv)
129 {
130 	struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL};
131 	union acpi_object *obja, *objb;
132 	int i, j;
133 	int result = 0;
134 	acpi_status status;
135 
136 	status = acpi_evaluate_object(priv->adev->handle, "IDSP", NULL, &buf);
137 	if (ACPI_FAILURE(status))
138 		return -ENODEV;
139 
140 	obja = (union acpi_object *)buf.pointer;
141 	if (obja->type != ACPI_TYPE_PACKAGE) {
142 		result = -EINVAL;
143 		goto end;
144 	}
145 
146 	for (i = 0; i < obja->package.count; i++) {
147 		objb = &obja->package.elements[i];
148 		if (objb->type != ACPI_TYPE_BUFFER) {
149 			result = -EINVAL;
150 			goto end;
151 		}
152 
153 		/* UUID must be 16 bytes */
154 		if (objb->buffer.length != 16) {
155 			result = -EINVAL;
156 			goto end;
157 		}
158 
159 		for (j = 0; j < INT3400_THERMAL_MAXIMUM_UUID; j++) {
160 			guid_t guid;
161 
162 			guid_parse(int3400_thermal_uuids[j], &guid);
163 			if (guid_equal((guid_t *)objb->buffer.pointer, &guid)) {
164 				priv->uuid_bitmap |= (1 << j);
165 				break;
166 			}
167 		}
168 	}
169 
170 end:
171 	kfree(buf.pointer);
172 	return result;
173 }
174 
int3400_thermal_run_osc(acpi_handle handle,enum int3400_thermal_uuid uuid,bool enable)175 static int int3400_thermal_run_osc(acpi_handle handle,
176 				enum int3400_thermal_uuid uuid, bool enable)
177 {
178 	u32 ret, buf[2];
179 	acpi_status status;
180 	int result = 0;
181 	struct acpi_osc_context context = {
182 		.uuid_str = int3400_thermal_uuids[uuid],
183 		.rev = 1,
184 		.cap.length = 8,
185 	};
186 
187 	buf[OSC_QUERY_DWORD] = 0;
188 	buf[OSC_SUPPORT_DWORD] = enable;
189 
190 	context.cap.pointer = buf;
191 
192 	status = acpi_run_osc(handle, &context);
193 	if (ACPI_SUCCESS(status)) {
194 		ret = *((u32 *)(context.ret.pointer + 4));
195 		if (ret != enable)
196 			result = -EPERM;
197 	} else
198 		result = -EPERM;
199 
200 	kfree(context.ret.pointer);
201 	return result;
202 }
203 
int3400_notify(acpi_handle handle,u32 event,void * data)204 static void int3400_notify(acpi_handle handle,
205 			u32 event,
206 			void *data)
207 {
208 	struct int3400_thermal_priv *priv = data;
209 	char *thermal_prop[5];
210 
211 	if (!priv)
212 		return;
213 
214 	switch (event) {
215 	case INT3400_THERMAL_TABLE_CHANGED:
216 		thermal_prop[0] = kasprintf(GFP_KERNEL, "NAME=%s",
217 				priv->thermal->type);
218 		thermal_prop[1] = kasprintf(GFP_KERNEL, "TEMP=%d",
219 				priv->thermal->temperature);
220 		thermal_prop[2] = kasprintf(GFP_KERNEL, "TRIP=");
221 		thermal_prop[3] = kasprintf(GFP_KERNEL, "EVENT=%d",
222 				THERMAL_TABLE_CHANGED);
223 		thermal_prop[4] = NULL;
224 		kobject_uevent_env(&priv->thermal->device.kobj, KOBJ_CHANGE,
225 				thermal_prop);
226 		break;
227 	default:
228 		dev_err(&priv->adev->dev, "Unsupported event [0x%x]\n", event);
229 		break;
230 	}
231 }
232 
int3400_thermal_get_temp(struct thermal_zone_device * thermal,int * temp)233 static int int3400_thermal_get_temp(struct thermal_zone_device *thermal,
234 			int *temp)
235 {
236 	*temp = 20 * 1000; /* faked temp sensor with 20C */
237 	return 0;
238 }
239 
int3400_thermal_get_mode(struct thermal_zone_device * thermal,enum thermal_device_mode * mode)240 static int int3400_thermal_get_mode(struct thermal_zone_device *thermal,
241 				enum thermal_device_mode *mode)
242 {
243 	struct int3400_thermal_priv *priv = thermal->devdata;
244 
245 	if (!priv)
246 		return -EINVAL;
247 
248 	*mode = priv->mode;
249 
250 	return 0;
251 }
252 
int3400_thermal_set_mode(struct thermal_zone_device * thermal,enum thermal_device_mode mode)253 static int int3400_thermal_set_mode(struct thermal_zone_device *thermal,
254 				enum thermal_device_mode mode)
255 {
256 	struct int3400_thermal_priv *priv = thermal->devdata;
257 	bool enable;
258 	int result = 0;
259 
260 	if (!priv)
261 		return -EINVAL;
262 
263 	if (mode == THERMAL_DEVICE_ENABLED)
264 		enable = true;
265 	else if (mode == THERMAL_DEVICE_DISABLED)
266 		enable = false;
267 	else
268 		return -EINVAL;
269 
270 	if (enable != priv->mode) {
271 		priv->mode = enable;
272 		result = int3400_thermal_run_osc(priv->adev->handle,
273 						 priv->current_uuid_index,
274 						 enable);
275 	}
276 	return result;
277 }
278 
279 static struct thermal_zone_device_ops int3400_thermal_ops = {
280 	.get_temp = int3400_thermal_get_temp,
281 };
282 
283 static struct thermal_zone_params int3400_thermal_params = {
284 	.governor_name = "user_space",
285 	.no_hwmon = true,
286 };
287 
int3400_thermal_probe(struct platform_device * pdev)288 static int int3400_thermal_probe(struct platform_device *pdev)
289 {
290 	struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
291 	struct int3400_thermal_priv *priv;
292 	int result;
293 
294 	if (!adev)
295 		return -ENODEV;
296 
297 	priv = kzalloc(sizeof(struct int3400_thermal_priv), GFP_KERNEL);
298 	if (!priv)
299 		return -ENOMEM;
300 
301 	priv->adev = adev;
302 
303 	result = int3400_thermal_get_uuids(priv);
304 	if (result)
305 		goto free_priv;
306 
307 	result = acpi_parse_art(priv->adev->handle, &priv->art_count,
308 				&priv->arts, true);
309 	if (result)
310 		dev_dbg(&pdev->dev, "_ART table parsing error\n");
311 
312 	result = acpi_parse_trt(priv->adev->handle, &priv->trt_count,
313 				&priv->trts, true);
314 	if (result)
315 		dev_dbg(&pdev->dev, "_TRT table parsing error\n");
316 
317 	platform_set_drvdata(pdev, priv);
318 
319 	int3400_thermal_ops.get_mode = int3400_thermal_get_mode;
320 	int3400_thermal_ops.set_mode = int3400_thermal_set_mode;
321 
322 	priv->thermal = thermal_zone_device_register("INT3400 Thermal", 0, 0,
323 						priv, &int3400_thermal_ops,
324 						&int3400_thermal_params, 0, 0);
325 	if (IS_ERR(priv->thermal)) {
326 		result = PTR_ERR(priv->thermal);
327 		goto free_art_trt;
328 	}
329 
330 	priv->rel_misc_dev_res = acpi_thermal_rel_misc_device_add(
331 							priv->adev->handle);
332 
333 	result = sysfs_create_group(&pdev->dev.kobj, &uuid_attribute_group);
334 	if (result)
335 		goto free_rel_misc;
336 
337 	result = acpi_install_notify_handler(
338 			priv->adev->handle, ACPI_DEVICE_NOTIFY, int3400_notify,
339 			(void *)priv);
340 	if (result)
341 		goto free_sysfs;
342 
343 	return 0;
344 
345 free_sysfs:
346 	sysfs_remove_group(&pdev->dev.kobj, &uuid_attribute_group);
347 free_rel_misc:
348 	if (!priv->rel_misc_dev_res)
349 		acpi_thermal_rel_misc_device_remove(priv->adev->handle);
350 	thermal_zone_device_unregister(priv->thermal);
351 free_art_trt:
352 	kfree(priv->trts);
353 	kfree(priv->arts);
354 free_priv:
355 	kfree(priv);
356 	return result;
357 }
358 
int3400_thermal_remove(struct platform_device * pdev)359 static int int3400_thermal_remove(struct platform_device *pdev)
360 {
361 	struct int3400_thermal_priv *priv = platform_get_drvdata(pdev);
362 
363 	acpi_remove_notify_handler(
364 			priv->adev->handle, ACPI_DEVICE_NOTIFY,
365 			int3400_notify);
366 
367 	if (!priv->rel_misc_dev_res)
368 		acpi_thermal_rel_misc_device_remove(priv->adev->handle);
369 
370 	sysfs_remove_group(&pdev->dev.kobj, &uuid_attribute_group);
371 	thermal_zone_device_unregister(priv->thermal);
372 	kfree(priv->trts);
373 	kfree(priv->arts);
374 	kfree(priv);
375 	return 0;
376 }
377 
378 static const struct acpi_device_id int3400_thermal_match[] = {
379 	{"INT3400", 0},
380 	{}
381 };
382 
383 MODULE_DEVICE_TABLE(acpi, int3400_thermal_match);
384 
385 static struct platform_driver int3400_thermal_driver = {
386 	.probe = int3400_thermal_probe,
387 	.remove = int3400_thermal_remove,
388 	.driver = {
389 		   .name = "int3400 thermal",
390 		   .acpi_match_table = ACPI_PTR(int3400_thermal_match),
391 		   },
392 };
393 
394 module_platform_driver(int3400_thermal_driver);
395 
396 MODULE_DESCRIPTION("INT3400 Thermal driver");
397 MODULE_AUTHOR("Zhang Rui <rui.zhang@intel.com>");
398 MODULE_LICENSE("GPL");
399