1 // SPDX-License-Identifier: GPL-2.0-only
3 * INT3400 thermal driver
5 * Copyright (C) 2014, Intel Corporation
6 * Authors: Zhang Rui <rui.zhang@intel.com>
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/acpi.h>
12 #include <linux/thermal.h>
13 #include "acpi_thermal_rel.h"
15 #define INT3400_THERMAL_TABLE_CHANGED 0x83
16 #define INT3400_ODVP_CHANGED 0x88
17 #define INT3400_KEEP_ALIVE 0xA0
19 enum int3400_thermal_uuid {
20 INT3400_THERMAL_ACTIVE = 0,
21 INT3400_THERMAL_PASSIVE_1,
22 INT3400_THERMAL_CRITICAL,
23 INT3400_THERMAL_ADAPTIVE_PERFORMANCE,
24 INT3400_THERMAL_EMERGENCY_CALL_MODE,
25 INT3400_THERMAL_PASSIVE_2,
26 INT3400_THERMAL_POWER_BOSS,
27 INT3400_THERMAL_VIRTUAL_SENSOR,
28 INT3400_THERMAL_COOLING_MODE,
29 INT3400_THERMAL_HARDWARE_DUTY_CYCLING,
30 INT3400_THERMAL_MAXIMUM_UUID,
33 static char *int3400_thermal_uuids[INT3400_THERMAL_MAXIMUM_UUID] = {
34 "3A95C389-E4B8-4629-A526-C52C88626BAE",
35 "42A441D6-AE6A-462b-A84B-4A8CE79027D3",
36 "97C68AE7-15FA-499c-B8C9-5DA81D606E0A",
37 "63BE270F-1C11-48FD-A6F7-3AF253FF3E2D",
38 "5349962F-71E6-431D-9AE8-0A635B710AEE",
39 "9E04115A-AE87-4D1C-9500-0F3E340BFE75",
40 "F5A35014-C209-46A4-993A-EB56DE7530A1",
41 "6ED722A7-9240-48A5-B479-31EEF723D7CF",
42 "16CAF1B7-DD38-40ED-B1C1-1B8A1913D531",
43 "BE84BABF-C4D4-403D-B495-3128FD44dAC1",
48 struct int3400_thermal_priv {
49 struct acpi_device *adev;
50 struct platform_device *pdev;
51 struct thermal_zone_device *thermal;
58 int current_uuid_index;
63 struct odvp_attr *odvp_attrs;
66 static int evaluate_odvp(struct int3400_thermal_priv *priv);
70 struct int3400_thermal_priv *priv;
71 struct kobj_attribute attr;
74 static ssize_t data_vault_read(struct file *file, struct kobject *kobj,
75 struct bin_attribute *attr, char *buf, loff_t off, size_t count)
77 memcpy(buf, attr->private + off, count);
81 static BIN_ATTR_RO(data_vault, 0);
83 static struct bin_attribute *data_attributes[] = {
88 static ssize_t imok_store(struct device *dev, struct device_attribute *attr,
89 const char *buf, size_t count)
91 struct int3400_thermal_priv *priv = dev_get_drvdata(dev);
95 ret = kstrtouint(buf, 10, &input);
98 status = acpi_execute_simple_method(priv->adev->handle, "IMOK", input);
99 if (ACPI_FAILURE(status))
105 static DEVICE_ATTR_WO(imok);
107 static struct attribute *imok_attr[] = {
112 static const struct attribute_group imok_attribute_group = {
116 static const struct attribute_group data_attribute_group = {
117 .bin_attrs = data_attributes,
120 static ssize_t available_uuids_show(struct device *dev,
121 struct device_attribute *attr,
124 struct int3400_thermal_priv *priv = dev_get_drvdata(dev);
128 if (!priv->uuid_bitmap)
129 return sprintf(buf, "UNKNOWN\n");
131 for (i = 0; i < INT3400_THERMAL_MAXIMUM_UUID; i++) {
132 if (priv->uuid_bitmap & (1 << i))
133 length += scnprintf(&buf[length],
136 int3400_thermal_uuids[i]);
142 static ssize_t current_uuid_show(struct device *dev,
143 struct device_attribute *devattr, char *buf)
145 struct int3400_thermal_priv *priv = dev_get_drvdata(dev);
148 if (priv->current_uuid_index > 0)
149 return sprintf(buf, "%s\n",
150 int3400_thermal_uuids[priv->current_uuid_index]);
152 for (i = 0; i <= INT3400_THERMAL_CRITICAL; i++) {
153 if (priv->os_uuid_mask & BIT(i))
154 length += scnprintf(&buf[length],
157 int3400_thermal_uuids[i]);
163 return sprintf(buf, "INVALID\n");
166 static int int3400_thermal_run_osc(acpi_handle handle, char *uuid_str, int *enable)
171 struct acpi_osc_context context = {
177 context.uuid_str = uuid_str;
179 buf[OSC_QUERY_DWORD] = 0;
180 buf[OSC_SUPPORT_DWORD] = *enable;
182 context.cap.pointer = buf;
184 status = acpi_run_osc(handle, &context);
185 if (ACPI_SUCCESS(status)) {
186 ret = *((u32 *)(context.ret.pointer + 4));
192 kfree(context.ret.pointer);
197 static ssize_t current_uuid_store(struct device *dev,
198 struct device_attribute *attr,
199 const char *buf, size_t count)
201 struct int3400_thermal_priv *priv = dev_get_drvdata(dev);
204 for (i = 0; i < INT3400_THERMAL_MAXIMUM_UUID; ++i) {
205 if (!strncmp(buf, int3400_thermal_uuids[i],
206 sizeof(int3400_thermal_uuids[i]) - 1)) {
208 * If we have a list of supported UUIDs, make sure
209 * this one is supported.
211 if (priv->uuid_bitmap & BIT(i)) {
212 priv->current_uuid_index = i;
217 * There is support of only 3 policies via the new
218 * _OSC to inform OS capability:
219 * INT3400_THERMAL_ACTIVE
220 * INT3400_THERMAL_PASSIVE_1
221 * INT3400_THERMAL_CRITICAL
224 if (i > INT3400_THERMAL_CRITICAL)
227 priv->os_uuid_mask |= BIT(i);
233 if (priv->os_uuid_mask) {
238 * Bit 0: set to 1 to indicate DPTF is active
239 * Bi1 1: set to 1 to active cooling is supported by user space daemon
240 * Bit 2: set to 1 to passive cooling is supported by user space daemon
241 * Bit 3: set to 1 to critical trip is handled by user space daemon
243 cap = ((priv->os_uuid_mask << 1) | 0x01);
244 ret = int3400_thermal_run_osc(priv->adev->handle,
245 "b23ba85d-c8b7-3542-88de-8de2ffcfd698",
254 static DEVICE_ATTR_RW(current_uuid);
255 static DEVICE_ATTR_RO(available_uuids);
256 static struct attribute *uuid_attrs[] = {
257 &dev_attr_available_uuids.attr,
258 &dev_attr_current_uuid.attr,
262 static const struct attribute_group uuid_attribute_group = {
267 static int int3400_thermal_get_uuids(struct int3400_thermal_priv *priv)
269 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL};
270 union acpi_object *obja, *objb;
275 status = acpi_evaluate_object(priv->adev->handle, "IDSP", NULL, &buf);
276 if (ACPI_FAILURE(status))
279 obja = (union acpi_object *)buf.pointer;
280 if (obja->type != ACPI_TYPE_PACKAGE) {
285 for (i = 0; i < obja->package.count; i++) {
286 objb = &obja->package.elements[i];
287 if (objb->type != ACPI_TYPE_BUFFER) {
292 /* UUID must be 16 bytes */
293 if (objb->buffer.length != 16) {
298 for (j = 0; j < INT3400_THERMAL_MAXIMUM_UUID; j++) {
301 guid_parse(int3400_thermal_uuids[j], &guid);
302 if (guid_equal((guid_t *)objb->buffer.pointer, &guid)) {
303 priv->uuid_bitmap |= (1 << j);
314 static ssize_t odvp_show(struct kobject *kobj, struct kobj_attribute *attr,
317 struct odvp_attr *odvp_attr;
319 odvp_attr = container_of(attr, struct odvp_attr, attr);
321 return sprintf(buf, "%d\n", odvp_attr->priv->odvp[odvp_attr->odvp]);
324 static void cleanup_odvp(struct int3400_thermal_priv *priv)
328 if (priv->odvp_attrs) {
329 for (i = 0; i < priv->odvp_count; i++) {
330 sysfs_remove_file(&priv->pdev->dev.kobj,
331 &priv->odvp_attrs[i].attr.attr);
332 kfree(priv->odvp_attrs[i].attr.attr.name);
334 kfree(priv->odvp_attrs);
337 priv->odvp_count = 0;
340 static int evaluate_odvp(struct int3400_thermal_priv *priv)
342 struct acpi_buffer odvp = { ACPI_ALLOCATE_BUFFER, NULL };
343 union acpi_object *obj = NULL;
347 status = acpi_evaluate_object(priv->adev->handle, "ODVP", NULL, &odvp);
348 if (ACPI_FAILURE(status)) {
354 if (obj->type != ACPI_TYPE_PACKAGE) {
359 if (priv->odvp == NULL) {
360 priv->odvp_count = obj->package.count;
361 priv->odvp = kmalloc_array(priv->odvp_count, sizeof(int),
369 if (priv->odvp_attrs == NULL) {
370 priv->odvp_attrs = kcalloc(priv->odvp_count,
371 sizeof(struct odvp_attr),
373 if (!priv->odvp_attrs) {
377 for (i = 0; i < priv->odvp_count; i++) {
378 struct odvp_attr *odvp = &priv->odvp_attrs[i];
380 sysfs_attr_init(&odvp->attr.attr);
383 odvp->attr.attr.name = kasprintf(GFP_KERNEL,
386 if (!odvp->attr.attr.name) {
390 odvp->attr.attr.mode = 0444;
391 odvp->attr.show = odvp_show;
392 odvp->attr.store = NULL;
393 ret = sysfs_create_file(&priv->pdev->dev.kobj,
400 for (i = 0; i < obj->package.count; i++) {
401 if (obj->package.elements[i].type == ACPI_TYPE_INTEGER)
402 priv->odvp[i] = obj->package.elements[i].integer.value;
414 static void int3400_notify(acpi_handle handle,
418 struct int3400_thermal_priv *priv = data;
419 char *thermal_prop[5];
426 case INT3400_THERMAL_TABLE_CHANGED:
427 therm_event = THERMAL_TABLE_CHANGED;
429 case INT3400_KEEP_ALIVE:
430 therm_event = THERMAL_EVENT_KEEP_ALIVE;
432 case INT3400_ODVP_CHANGED:
434 therm_event = THERMAL_DEVICE_POWER_CAPABILITY_CHANGED;
437 /* Ignore unknown notification codes sent to INT3400 device */
441 thermal_prop[0] = kasprintf(GFP_KERNEL, "NAME=%s", priv->thermal->type);
442 thermal_prop[1] = kasprintf(GFP_KERNEL, "TEMP=%d", priv->thermal->temperature);
443 thermal_prop[2] = kasprintf(GFP_KERNEL, "TRIP=");
444 thermal_prop[3] = kasprintf(GFP_KERNEL, "EVENT=%d", therm_event);
445 thermal_prop[4] = NULL;
446 kobject_uevent_env(&priv->thermal->device.kobj, KOBJ_CHANGE, thermal_prop);
447 kfree(thermal_prop[0]);
448 kfree(thermal_prop[1]);
449 kfree(thermal_prop[2]);
450 kfree(thermal_prop[3]);
453 static int int3400_thermal_get_temp(struct thermal_zone_device *thermal,
456 *temp = 20 * 1000; /* faked temp sensor with 20C */
460 static int int3400_thermal_change_mode(struct thermal_zone_device *thermal,
461 enum thermal_device_mode mode)
463 struct int3400_thermal_priv *priv = thermal->devdata;
469 if (mode != thermal->mode) {
472 if (priv->current_uuid_index < 0 ||
473 priv->current_uuid_index >= INT3400_THERMAL_MAXIMUM_UUID)
476 enabled = (mode == THERMAL_DEVICE_ENABLED);
477 result = int3400_thermal_run_osc(priv->adev->handle,
478 int3400_thermal_uuids[priv->current_uuid_index],
488 static struct thermal_zone_device_ops int3400_thermal_ops = {
489 .get_temp = int3400_thermal_get_temp,
490 .change_mode = int3400_thermal_change_mode,
493 static struct thermal_zone_params int3400_thermal_params = {
494 .governor_name = "user_space",
498 static void int3400_setup_gddv(struct int3400_thermal_priv *priv)
500 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
501 union acpi_object *obj;
504 status = acpi_evaluate_object(priv->adev->handle, "GDDV", NULL,
506 if (ACPI_FAILURE(status) || !buffer.length)
509 obj = buffer.pointer;
510 if (obj->type != ACPI_TYPE_PACKAGE || obj->package.count != 1
511 || obj->package.elements[0].type != ACPI_TYPE_BUFFER) {
512 kfree(buffer.pointer);
516 priv->data_vault = kmemdup(obj->package.elements[0].buffer.pointer,
517 obj->package.elements[0].buffer.length,
519 if (!priv->data_vault) {
520 kfree(buffer.pointer);
524 bin_attr_data_vault.private = priv->data_vault;
525 bin_attr_data_vault.size = obj->package.elements[0].buffer.length;
526 kfree(buffer.pointer);
529 static int int3400_thermal_probe(struct platform_device *pdev)
531 struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
532 struct int3400_thermal_priv *priv;
538 priv = kzalloc(sizeof(struct int3400_thermal_priv), GFP_KERNEL);
545 result = int3400_thermal_get_uuids(priv);
547 /* Missing IDSP isn't fatal */
548 if (result && result != -ENODEV)
551 priv->current_uuid_index = -1;
553 result = acpi_parse_art(priv->adev->handle, &priv->art_count,
556 dev_dbg(&pdev->dev, "_ART table parsing error\n");
558 result = acpi_parse_trt(priv->adev->handle, &priv->trt_count,
561 dev_dbg(&pdev->dev, "_TRT table parsing error\n");
563 platform_set_drvdata(pdev, priv);
565 int3400_setup_gddv(priv);
569 priv->thermal = thermal_zone_device_register("INT3400 Thermal", 0, 0,
570 priv, &int3400_thermal_ops,
571 &int3400_thermal_params, 0, 0);
572 if (IS_ERR(priv->thermal)) {
573 result = PTR_ERR(priv->thermal);
577 priv->rel_misc_dev_res = acpi_thermal_rel_misc_device_add(
580 result = sysfs_create_group(&pdev->dev.kobj, &uuid_attribute_group);
584 if (acpi_has_method(priv->adev->handle, "IMOK")) {
585 result = sysfs_create_group(&pdev->dev.kobj, &imok_attribute_group);
590 if (priv->data_vault) {
591 result = sysfs_create_group(&pdev->dev.kobj,
592 &data_attribute_group);
597 result = acpi_install_notify_handler(
598 priv->adev->handle, ACPI_DEVICE_NOTIFY, int3400_notify,
607 if (priv->data_vault) {
608 sysfs_remove_group(&pdev->dev.kobj, &data_attribute_group);
609 kfree(priv->data_vault);
612 sysfs_remove_group(&pdev->dev.kobj, &uuid_attribute_group);
614 sysfs_remove_group(&pdev->dev.kobj, &imok_attribute_group);
616 if (!priv->rel_misc_dev_res)
617 acpi_thermal_rel_misc_device_remove(priv->adev->handle);
618 thermal_zone_device_unregister(priv->thermal);
627 static int int3400_thermal_remove(struct platform_device *pdev)
629 struct int3400_thermal_priv *priv = platform_get_drvdata(pdev);
631 acpi_remove_notify_handler(
632 priv->adev->handle, ACPI_DEVICE_NOTIFY,
637 if (!priv->rel_misc_dev_res)
638 acpi_thermal_rel_misc_device_remove(priv->adev->handle);
640 if (priv->data_vault)
641 sysfs_remove_group(&pdev->dev.kobj, &data_attribute_group);
642 sysfs_remove_group(&pdev->dev.kobj, &uuid_attribute_group);
643 sysfs_remove_group(&pdev->dev.kobj, &imok_attribute_group);
644 thermal_zone_device_unregister(priv->thermal);
645 kfree(priv->data_vault);
652 static const struct acpi_device_id int3400_thermal_match[] = {
660 MODULE_DEVICE_TABLE(acpi, int3400_thermal_match);
662 static struct platform_driver int3400_thermal_driver = {
663 .probe = int3400_thermal_probe,
664 .remove = int3400_thermal_remove,
666 .name = "int3400 thermal",
667 .acpi_match_table = ACPI_PTR(int3400_thermal_match),
671 module_platform_driver(int3400_thermal_driver);
673 MODULE_DESCRIPTION("INT3400 Thermal driver");
674 MODULE_AUTHOR("Zhang Rui <rui.zhang@intel.com>");
675 MODULE_LICENSE("GPL");