2 * INT3400 thermal driver
4 * Copyright (C) 2014, Intel Corporation
5 * Authors: Zhang Rui <rui.zhang@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.
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"
19 #define INT3400_THERMAL_TABLE_CHANGED 0x83
21 enum int3400_thermal_uuid {
22 INT3400_THERMAL_PASSIVE_1,
23 INT3400_THERMAL_ACTIVE,
24 INT3400_THERMAL_CRITICAL,
25 INT3400_THERMAL_MAXIMUM_UUID,
28 static char *int3400_thermal_uuids[INT3400_THERMAL_MAXIMUM_UUID] = {
29 "42A441D6-AE6A-462b-A84B-4A8CE79027D3",
30 "3A95C389-E4B8-4629-A526-C52C88626BAE",
31 "97C68AE7-15FA-499c-B8C9-5DA81D606E0A",
34 struct int3400_thermal_priv {
35 struct acpi_device *adev;
36 struct thermal_zone_device *thermal;
44 int current_uuid_index;
47 static ssize_t available_uuids_show(struct device *dev,
48 struct device_attribute *attr,
51 struct int3400_thermal_priv *priv = dev_get_drvdata(dev);
55 for (i = 0; i < INT3400_THERMAL_MAXIMUM_UUID; i++) {
56 if (priv->uuid_bitmap & (1 << i))
57 if (PAGE_SIZE - length > 0)
58 length += snprintf(&buf[length],
61 int3400_thermal_uuids[i]);
67 static ssize_t current_uuid_show(struct device *dev,
68 struct device_attribute *devattr, char *buf)
70 struct int3400_thermal_priv *priv = dev_get_drvdata(dev);
72 if (priv->uuid_bitmap & (1 << priv->current_uuid_index))
73 return sprintf(buf, "%s\n",
74 int3400_thermal_uuids[priv->current_uuid_index]);
76 return sprintf(buf, "INVALID\n");
79 static ssize_t current_uuid_store(struct device *dev,
80 struct device_attribute *attr,
81 const char *buf, size_t count)
83 struct int3400_thermal_priv *priv = dev_get_drvdata(dev);
86 for (i = 0; i < INT3400_THERMAL_MAXIMUM_UUID; ++i) {
87 if ((priv->uuid_bitmap & (1 << i)) &&
88 !(strncmp(buf, int3400_thermal_uuids[i],
89 sizeof(int3400_thermal_uuids[i]) - 1))) {
90 priv->current_uuid_index = i;
98 static DEVICE_ATTR_RW(current_uuid);
99 static DEVICE_ATTR_RO(available_uuids);
100 static struct attribute *uuid_attrs[] = {
101 &dev_attr_available_uuids.attr,
102 &dev_attr_current_uuid.attr,
106 static const struct attribute_group uuid_attribute_group = {
111 static int int3400_thermal_get_uuids(struct int3400_thermal_priv *priv)
113 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL};
114 union acpi_object *obja, *objb;
119 status = acpi_evaluate_object(priv->adev->handle, "IDSP", NULL, &buf);
120 if (ACPI_FAILURE(status))
123 obja = (union acpi_object *)buf.pointer;
124 if (obja->type != ACPI_TYPE_PACKAGE) {
129 for (i = 0; i < obja->package.count; i++) {
130 objb = &obja->package.elements[i];
131 if (objb->type != ACPI_TYPE_BUFFER) {
136 /* UUID must be 16 bytes */
137 if (objb->buffer.length != 16) {
142 for (j = 0; j < INT3400_THERMAL_MAXIMUM_UUID; j++) {
145 guid_parse(int3400_thermal_uuids[j], &guid);
146 if (guid_equal((guid_t *)objb->buffer.pointer, &guid)) {
147 priv->uuid_bitmap |= (1 << j);
158 static int int3400_thermal_run_osc(acpi_handle handle,
159 enum int3400_thermal_uuid uuid, bool enable)
164 struct acpi_osc_context context = {
165 .uuid_str = int3400_thermal_uuids[uuid],
170 buf[OSC_QUERY_DWORD] = 0;
171 buf[OSC_SUPPORT_DWORD] = enable;
173 context.cap.pointer = buf;
175 status = acpi_run_osc(handle, &context);
176 if (ACPI_SUCCESS(status)) {
177 ret = *((u32 *)(context.ret.pointer + 4));
183 kfree(context.ret.pointer);
187 static void int3400_notify(acpi_handle handle,
191 struct int3400_thermal_priv *priv = data;
192 char *thermal_prop[5];
198 case INT3400_THERMAL_TABLE_CHANGED:
199 thermal_prop[0] = kasprintf(GFP_KERNEL, "NAME=%s",
200 priv->thermal->type);
201 thermal_prop[1] = kasprintf(GFP_KERNEL, "TEMP=%d",
202 priv->thermal->temperature);
203 thermal_prop[2] = kasprintf(GFP_KERNEL, "TRIP=");
204 thermal_prop[3] = kasprintf(GFP_KERNEL, "EVENT=%d",
205 THERMAL_TABLE_CHANGED);
206 thermal_prop[4] = NULL;
207 kobject_uevent_env(&priv->thermal->device.kobj, KOBJ_CHANGE,
211 /* Ignore unknown notification codes sent to INT3400 device */
216 static int int3400_thermal_get_temp(struct thermal_zone_device *thermal,
219 *temp = 20 * 1000; /* faked temp sensor with 20C */
223 static int int3400_thermal_get_mode(struct thermal_zone_device *thermal,
224 enum thermal_device_mode *mode)
226 struct int3400_thermal_priv *priv = thermal->devdata;
236 static int int3400_thermal_set_mode(struct thermal_zone_device *thermal,
237 enum thermal_device_mode mode)
239 struct int3400_thermal_priv *priv = thermal->devdata;
246 if (mode == THERMAL_DEVICE_ENABLED)
248 else if (mode == THERMAL_DEVICE_DISABLED)
253 if (enable != priv->mode) {
255 result = int3400_thermal_run_osc(priv->adev->handle,
256 priv->current_uuid_index,
262 static struct thermal_zone_device_ops int3400_thermal_ops = {
263 .get_temp = int3400_thermal_get_temp,
266 static struct thermal_zone_params int3400_thermal_params = {
267 .governor_name = "user_space",
271 static int int3400_thermal_probe(struct platform_device *pdev)
273 struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
274 struct int3400_thermal_priv *priv;
280 priv = kzalloc(sizeof(struct int3400_thermal_priv), GFP_KERNEL);
286 result = int3400_thermal_get_uuids(priv);
290 result = acpi_parse_art(priv->adev->handle, &priv->art_count,
293 dev_dbg(&pdev->dev, "_ART table parsing error\n");
295 result = acpi_parse_trt(priv->adev->handle, &priv->trt_count,
298 dev_dbg(&pdev->dev, "_TRT table parsing error\n");
300 platform_set_drvdata(pdev, priv);
302 if (priv->uuid_bitmap & 1 << INT3400_THERMAL_PASSIVE_1) {
303 int3400_thermal_ops.get_mode = int3400_thermal_get_mode;
304 int3400_thermal_ops.set_mode = int3400_thermal_set_mode;
306 priv->thermal = thermal_zone_device_register("INT3400 Thermal", 0, 0,
307 priv, &int3400_thermal_ops,
308 &int3400_thermal_params, 0, 0);
309 if (IS_ERR(priv->thermal)) {
310 result = PTR_ERR(priv->thermal);
314 priv->rel_misc_dev_res = acpi_thermal_rel_misc_device_add(
317 result = sysfs_create_group(&pdev->dev.kobj, &uuid_attribute_group);
321 result = acpi_install_notify_handler(
322 priv->adev->handle, ACPI_DEVICE_NOTIFY, int3400_notify,
330 sysfs_remove_group(&pdev->dev.kobj, &uuid_attribute_group);
332 if (!priv->rel_misc_dev_res)
333 acpi_thermal_rel_misc_device_remove(priv->adev->handle);
334 thermal_zone_device_unregister(priv->thermal);
343 static int int3400_thermal_remove(struct platform_device *pdev)
345 struct int3400_thermal_priv *priv = platform_get_drvdata(pdev);
347 acpi_remove_notify_handler(
348 priv->adev->handle, ACPI_DEVICE_NOTIFY,
351 if (!priv->rel_misc_dev_res)
352 acpi_thermal_rel_misc_device_remove(priv->adev->handle);
354 sysfs_remove_group(&pdev->dev.kobj, &uuid_attribute_group);
355 thermal_zone_device_unregister(priv->thermal);
362 static const struct acpi_device_id int3400_thermal_match[] = {
367 MODULE_DEVICE_TABLE(acpi, int3400_thermal_match);
369 static struct platform_driver int3400_thermal_driver = {
370 .probe = int3400_thermal_probe,
371 .remove = int3400_thermal_remove,
373 .name = "int3400 thermal",
374 .acpi_match_table = ACPI_PTR(int3400_thermal_match),
378 module_platform_driver(int3400_thermal_driver);
380 MODULE_DESCRIPTION("INT3400 Thermal driver");
381 MODULE_AUTHOR("Zhang Rui <rui.zhang@intel.com>");
382 MODULE_LICENSE("GPL");