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 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,
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",
36 struct int3400_thermal_priv {
37 struct acpi_device *adev;
38 struct thermal_zone_device *thermal;
46 int current_uuid_index;
49 static ssize_t available_uuids_show(struct device *dev,
50 struct device_attribute *attr,
53 struct platform_device *pdev = to_platform_device(dev);
54 struct int3400_thermal_priv *priv = platform_get_drvdata(pdev);
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],
64 int3400_thermal_uuids[i]);
70 static ssize_t current_uuid_show(struct device *dev,
71 struct device_attribute *devattr, char *buf)
73 struct platform_device *pdev = to_platform_device(dev);
74 struct int3400_thermal_priv *priv = platform_get_drvdata(pdev);
76 if (priv->uuid_bitmap & (1 << priv->current_uuid_index))
77 return sprintf(buf, "%s\n",
78 int3400_thermal_uuids[priv->current_uuid_index]);
80 return sprintf(buf, "INVALID\n");
83 static ssize_t current_uuid_store(struct device *dev,
84 struct device_attribute *attr,
85 const char *buf, size_t count)
87 struct platform_device *pdev = to_platform_device(dev);
88 struct int3400_thermal_priv *priv = platform_get_drvdata(pdev);
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;
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,
111 static struct attribute_group uuid_attribute_group = {
116 static int int3400_thermal_get_uuids(struct int3400_thermal_priv *priv)
118 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL};
119 union acpi_object *obja, *objb;
124 status = acpi_evaluate_object(priv->adev->handle, "IDSP", NULL, &buf);
125 if (ACPI_FAILURE(status))
128 obja = (union acpi_object *)buf.pointer;
129 if (obja->type != ACPI_TYPE_PACKAGE) {
134 for (i = 0; i < obja->package.count; i++) {
135 objb = &obja->package.elements[i];
136 if (objb->type != ACPI_TYPE_BUFFER) {
141 /* UUID must be 16 bytes */
142 if (objb->buffer.length != 16) {
147 for (j = 0; j < INT3400_THERMAL_MAXIMUM_UUID; j++) {
150 acpi_str_to_uuid(int3400_thermal_uuids[j], uuid);
151 if (!strncmp(uuid, objb->buffer.pointer, 16)) {
152 priv->uuid_bitmap |= (1 << j);
163 static int int3400_thermal_run_osc(acpi_handle handle,
164 enum int3400_thermal_uuid uuid, bool enable)
169 struct acpi_osc_context context = {
170 .uuid_str = int3400_thermal_uuids[uuid],
175 buf[OSC_QUERY_DWORD] = 0;
176 buf[OSC_SUPPORT_DWORD] = enable;
178 context.cap.pointer = buf;
180 status = acpi_run_osc(handle, &context);
181 if (ACPI_SUCCESS(status)) {
182 ret = *((u32 *)(context.ret.pointer + 4));
188 kfree(context.ret.pointer);
192 static int int3400_thermal_get_temp(struct thermal_zone_device *thermal,
195 *temp = 20 * 1000; /* faked temp sensor with 20C */
199 static int int3400_thermal_get_mode(struct thermal_zone_device *thermal,
200 enum thermal_device_mode *mode)
202 struct int3400_thermal_priv *priv = thermal->devdata;
212 static int int3400_thermal_set_mode(struct thermal_zone_device *thermal,
213 enum thermal_device_mode mode)
215 struct int3400_thermal_priv *priv = thermal->devdata;
222 if (mode == THERMAL_DEVICE_ENABLED)
224 else if (mode == THERMAL_DEVICE_DISABLED)
229 if (enable != priv->mode) {
231 result = int3400_thermal_run_osc(priv->adev->handle,
232 priv->current_uuid_index,
238 static struct thermal_zone_device_ops int3400_thermal_ops = {
239 .get_temp = int3400_thermal_get_temp,
242 static struct thermal_zone_params int3400_thermal_params = {
243 .governor_name = "user_space",
247 static int int3400_thermal_probe(struct platform_device *pdev)
249 struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
250 struct int3400_thermal_priv *priv;
256 priv = kzalloc(sizeof(struct int3400_thermal_priv), GFP_KERNEL);
262 result = int3400_thermal_get_uuids(priv);
266 result = acpi_parse_art(priv->adev->handle, &priv->art_count,
272 result = acpi_parse_trt(priv->adev->handle, &priv->trt_count,
277 platform_set_drvdata(pdev, priv);
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;
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);
291 priv->rel_misc_dev_res = acpi_thermal_rel_misc_device_add(
294 result = sysfs_create_group(&pdev->dev.kobj, &uuid_attribute_group);
301 thermal_zone_device_unregister(priv->thermal);
311 static int int3400_thermal_remove(struct platform_device *pdev)
313 struct int3400_thermal_priv *priv = platform_get_drvdata(pdev);
315 if (!priv->rel_misc_dev_res)
316 acpi_thermal_rel_misc_device_remove(priv->adev->handle);
318 sysfs_remove_group(&pdev->dev.kobj, &uuid_attribute_group);
319 thermal_zone_device_unregister(priv->thermal);
326 static const struct acpi_device_id int3400_thermal_match[] = {
331 MODULE_DEVICE_TABLE(acpi, int3400_thermal_match);
333 static struct platform_driver int3400_thermal_driver = {
334 .probe = int3400_thermal_probe,
335 .remove = int3400_thermal_remove,
337 .name = "int3400 thermal",
338 .acpi_match_table = ACPI_PTR(int3400_thermal_match),
342 module_platform_driver(int3400_thermal_driver);
344 MODULE_DESCRIPTION("INT3400 Thermal driver");
345 MODULE_AUTHOR("Zhang Rui <rui.zhang@intel.com>");
346 MODULE_LICENSE("GPL");