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
18 #define INT3400_FAKE_TEMP (20 * 1000) /* faked temp sensor with 20C */
20 enum int3400_thermal_uuid {
21 INT3400_THERMAL_ACTIVE = 0,
22 INT3400_THERMAL_PASSIVE_1,
23 INT3400_THERMAL_CRITICAL,
24 INT3400_THERMAL_ADAPTIVE_PERFORMANCE,
25 INT3400_THERMAL_EMERGENCY_CALL_MODE,
26 INT3400_THERMAL_PASSIVE_2,
27 INT3400_THERMAL_POWER_BOSS,
28 INT3400_THERMAL_VIRTUAL_SENSOR,
29 INT3400_THERMAL_COOLING_MODE,
30 INT3400_THERMAL_HARDWARE_DUTY_CYCLING,
31 INT3400_THERMAL_MAXIMUM_UUID,
34 static char *int3400_thermal_uuids[INT3400_THERMAL_MAXIMUM_UUID] = {
35 "3A95C389-E4B8-4629-A526-C52C88626BAE",
36 "42A441D6-AE6A-462b-A84B-4A8CE79027D3",
37 "97C68AE7-15FA-499c-B8C9-5DA81D606E0A",
38 "63BE270F-1C11-48FD-A6F7-3AF253FF3E2D",
39 "5349962F-71E6-431D-9AE8-0A635B710AEE",
40 "9E04115A-AE87-4D1C-9500-0F3E340BFE75",
41 "F5A35014-C209-46A4-993A-EB56DE7530A1",
42 "6ED722A7-9240-48A5-B479-31EEF723D7CF",
43 "16CAF1B7-DD38-40ED-B1C1-1B8A1913D531",
44 "BE84BABF-C4D4-403D-B495-3128FD44dAC1",
49 struct int3400_thermal_priv {
50 struct acpi_device *adev;
51 struct platform_device *pdev;
52 struct thermal_zone_device *thermal;
59 int current_uuid_index;
65 struct odvp_attr *odvp_attrs;
68 static int evaluate_odvp(struct int3400_thermal_priv *priv);
72 struct int3400_thermal_priv *priv;
73 struct device_attribute attr;
76 static ssize_t data_vault_read(struct file *file, struct kobject *kobj,
77 struct bin_attribute *attr, char *buf, loff_t off, size_t count)
79 memcpy(buf, attr->private + off, count);
83 static BIN_ATTR_RO(data_vault, 0);
85 static struct bin_attribute *data_attributes[] = {
90 static ssize_t imok_store(struct device *dev, struct device_attribute *attr,
91 const char *buf, size_t count)
93 struct int3400_thermal_priv *priv = dev_get_drvdata(dev);
97 ret = kstrtouint(buf, 10, &input);
100 status = acpi_execute_simple_method(priv->adev->handle, "IMOK", input);
101 if (ACPI_FAILURE(status))
107 static DEVICE_ATTR_WO(imok);
109 static struct attribute *imok_attr[] = {
114 static const struct attribute_group imok_attribute_group = {
118 static const struct attribute_group data_attribute_group = {
119 .bin_attrs = data_attributes,
122 static ssize_t available_uuids_show(struct device *dev,
123 struct device_attribute *attr,
126 struct int3400_thermal_priv *priv = dev_get_drvdata(dev);
130 if (!priv->uuid_bitmap)
131 return sprintf(buf, "UNKNOWN\n");
133 for (i = 0; i < INT3400_THERMAL_MAXIMUM_UUID; i++) {
134 if (priv->uuid_bitmap & (1 << i))
135 length += sysfs_emit_at(buf, length, "%s\n", int3400_thermal_uuids[i]);
141 static ssize_t current_uuid_show(struct device *dev,
142 struct device_attribute *devattr, char *buf)
144 struct int3400_thermal_priv *priv = dev_get_drvdata(dev);
147 if (priv->current_uuid_index > 0)
148 return sprintf(buf, "%s\n",
149 int3400_thermal_uuids[priv->current_uuid_index]);
151 for (i = 0; i <= INT3400_THERMAL_CRITICAL; i++) {
152 if (priv->os_uuid_mask & BIT(i))
153 length += sysfs_emit_at(buf, length, "%s\n", int3400_thermal_uuids[i]);
159 return sprintf(buf, "INVALID\n");
162 static int int3400_thermal_run_osc(acpi_handle handle, char *uuid_str, int *enable)
167 struct acpi_osc_context context = {
168 .uuid_str = uuid_str,
174 buf[OSC_QUERY_DWORD] = 0;
175 buf[OSC_SUPPORT_DWORD] = *enable;
177 status = acpi_run_osc(handle, &context);
178 if (ACPI_SUCCESS(status)) {
179 ret = *((u32 *)(context.ret.pointer + 4));
183 kfree(context.ret.pointer);
190 static int set_os_uuid_mask(struct int3400_thermal_priv *priv, u32 mask)
196 * Bit 0: set to 1 to indicate DPTF is active
197 * Bi1 1: set to 1 to active cooling is supported by user space daemon
198 * Bit 2: set to 1 to passive cooling is supported by user space daemon
199 * Bit 3: set to 1 to critical trip is handled by user space daemon
202 cap = (priv->os_uuid_mask << 1) | 0x01;
204 return int3400_thermal_run_osc(priv->adev->handle,
205 "b23ba85d-c8b7-3542-88de-8de2ffcfd698",
209 static ssize_t current_uuid_store(struct device *dev,
210 struct device_attribute *attr,
211 const char *buf, size_t count)
213 struct int3400_thermal_priv *priv = dev_get_drvdata(dev);
216 for (i = 0; i < INT3400_THERMAL_MAXIMUM_UUID; ++i) {
217 if (!strncmp(buf, int3400_thermal_uuids[i],
218 sizeof(int3400_thermal_uuids[i]) - 1)) {
220 * If we have a list of supported UUIDs, make sure
221 * this one is supported.
223 if (priv->uuid_bitmap & BIT(i)) {
224 priv->current_uuid_index = i;
229 * There is support of only 3 policies via the new
230 * _OSC to inform OS capability:
231 * INT3400_THERMAL_ACTIVE
232 * INT3400_THERMAL_PASSIVE_1
233 * INT3400_THERMAL_CRITICAL
236 if (i > INT3400_THERMAL_CRITICAL)
239 priv->os_uuid_mask |= BIT(i);
245 if (priv->os_uuid_mask) {
246 ret = set_os_uuid_mask(priv, priv->os_uuid_mask);
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 production_mode_show(struct device *dev, struct device_attribute *attr,
317 struct int3400_thermal_priv *priv = dev_get_drvdata(dev);
319 return sysfs_emit(buf, "%d\n", priv->production_mode);
322 static DEVICE_ATTR_RO(production_mode);
324 static int production_mode_init(struct int3400_thermal_priv *priv)
326 unsigned long long mode;
330 priv->production_mode = -1;
332 status = acpi_evaluate_integer(priv->adev->handle, "DCFG", NULL, &mode);
333 /* If the method is not present, this is not an error */
334 if (ACPI_FAILURE(status))
337 ret = sysfs_create_file(&priv->pdev->dev.kobj, &dev_attr_production_mode.attr);
341 priv->production_mode = mode;
346 static void production_mode_exit(struct int3400_thermal_priv *priv)
348 if (priv->production_mode >= 0)
349 sysfs_remove_file(&priv->pdev->dev.kobj, &dev_attr_production_mode.attr);
352 static ssize_t odvp_show(struct device *dev, struct device_attribute *attr,
355 struct odvp_attr *odvp_attr;
357 odvp_attr = container_of(attr, struct odvp_attr, attr);
359 return sprintf(buf, "%d\n", odvp_attr->priv->odvp[odvp_attr->odvp]);
362 static void cleanup_odvp(struct int3400_thermal_priv *priv)
366 if (priv->odvp_attrs) {
367 for (i = 0; i < priv->odvp_count; i++) {
368 sysfs_remove_file(&priv->pdev->dev.kobj,
369 &priv->odvp_attrs[i].attr.attr);
370 kfree(priv->odvp_attrs[i].attr.attr.name);
372 kfree(priv->odvp_attrs);
375 priv->odvp_count = 0;
378 static int evaluate_odvp(struct int3400_thermal_priv *priv)
380 struct acpi_buffer odvp = { ACPI_ALLOCATE_BUFFER, NULL };
381 union acpi_object *obj = NULL;
385 status = acpi_evaluate_object(priv->adev->handle, "ODVP", NULL, &odvp);
386 if (ACPI_FAILURE(status)) {
392 if (obj->type != ACPI_TYPE_PACKAGE) {
397 if (priv->odvp == NULL) {
398 priv->odvp_count = obj->package.count;
399 priv->odvp = kmalloc_array(priv->odvp_count, sizeof(int),
407 if (priv->odvp_attrs == NULL) {
408 priv->odvp_attrs = kcalloc(priv->odvp_count,
409 sizeof(struct odvp_attr),
411 if (!priv->odvp_attrs) {
415 for (i = 0; i < priv->odvp_count; i++) {
416 struct odvp_attr *odvp = &priv->odvp_attrs[i];
418 sysfs_attr_init(&odvp->attr.attr);
421 odvp->attr.attr.name = kasprintf(GFP_KERNEL,
424 if (!odvp->attr.attr.name) {
428 odvp->attr.attr.mode = 0444;
429 odvp->attr.show = odvp_show;
430 odvp->attr.store = NULL;
431 ret = sysfs_create_file(&priv->pdev->dev.kobj,
438 for (i = 0; i < obj->package.count; i++) {
439 if (obj->package.elements[i].type == ACPI_TYPE_INTEGER)
440 priv->odvp[i] = obj->package.elements[i].integer.value;
452 static void int3400_notify(acpi_handle handle,
456 struct int3400_thermal_priv *priv = data;
458 char *thermal_prop[5];
465 case INT3400_THERMAL_TABLE_CHANGED:
466 therm_event = THERMAL_TABLE_CHANGED;
468 case INT3400_KEEP_ALIVE:
469 therm_event = THERMAL_EVENT_KEEP_ALIVE;
471 case INT3400_ODVP_CHANGED:
473 therm_event = THERMAL_DEVICE_POWER_CAPABILITY_CHANGED;
476 /* Ignore unknown notification codes sent to INT3400 device */
480 dev = thermal_zone_device(priv->thermal);
482 thermal_prop[0] = kasprintf(GFP_KERNEL, "NAME=%s", thermal_zone_device_type(priv->thermal));
483 thermal_prop[1] = kasprintf(GFP_KERNEL, "TEMP=%d", INT3400_FAKE_TEMP);
484 thermal_prop[2] = kasprintf(GFP_KERNEL, "TRIP=");
485 thermal_prop[3] = kasprintf(GFP_KERNEL, "EVENT=%d", therm_event);
486 thermal_prop[4] = NULL;
487 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, thermal_prop);
488 kfree(thermal_prop[0]);
489 kfree(thermal_prop[1]);
490 kfree(thermal_prop[2]);
491 kfree(thermal_prop[3]);
494 static int int3400_thermal_get_temp(struct thermal_zone_device *thermal,
497 *temp = INT3400_FAKE_TEMP;
501 static int int3400_thermal_change_mode(struct thermal_zone_device *thermal,
502 enum thermal_device_mode mode)
504 struct int3400_thermal_priv *priv = thermal_zone_device_priv(thermal);
511 enabled = mode == THERMAL_DEVICE_ENABLED;
513 if (priv->os_uuid_mask) {
515 priv->os_uuid_mask = 0;
516 result = set_os_uuid_mask(priv, priv->os_uuid_mask);
521 if (priv->current_uuid_index < 0 ||
522 priv->current_uuid_index >= INT3400_THERMAL_MAXIMUM_UUID)
525 result = int3400_thermal_run_osc(priv->adev->handle,
526 int3400_thermal_uuids[priv->current_uuid_index],
534 static struct thermal_zone_device_ops int3400_thermal_ops = {
535 .get_temp = int3400_thermal_get_temp,
536 .change_mode = int3400_thermal_change_mode,
539 static struct thermal_zone_params int3400_thermal_params = {
540 .governor_name = "user_space",
544 static void int3400_setup_gddv(struct int3400_thermal_priv *priv)
546 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
547 union acpi_object *obj;
550 status = acpi_evaluate_object(priv->adev->handle, "GDDV", NULL,
552 if (ACPI_FAILURE(status) || !buffer.length)
555 obj = buffer.pointer;
556 if (obj->type != ACPI_TYPE_PACKAGE || obj->package.count != 1
557 || obj->package.elements[0].type != ACPI_TYPE_BUFFER)
560 priv->data_vault = kmemdup(obj->package.elements[0].buffer.pointer,
561 obj->package.elements[0].buffer.length,
563 if (ZERO_OR_NULL_PTR(priv->data_vault))
566 bin_attr_data_vault.private = priv->data_vault;
567 bin_attr_data_vault.size = obj->package.elements[0].buffer.length;
569 kfree(buffer.pointer);
572 static int int3400_thermal_probe(struct platform_device *pdev)
574 struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
575 struct int3400_thermal_priv *priv;
581 priv = kzalloc(sizeof(struct int3400_thermal_priv), GFP_KERNEL);
588 result = int3400_thermal_get_uuids(priv);
590 /* Missing IDSP isn't fatal */
591 if (result && result != -ENODEV)
594 priv->current_uuid_index = -1;
596 result = acpi_parse_art(priv->adev->handle, &priv->art_count,
599 dev_dbg(&pdev->dev, "_ART table parsing error\n");
601 result = acpi_parse_trt(priv->adev->handle, &priv->trt_count,
604 dev_dbg(&pdev->dev, "_TRT table parsing error\n");
606 platform_set_drvdata(pdev, priv);
608 int3400_setup_gddv(priv);
612 priv->thermal = thermal_tripless_zone_device_register("INT3400 Thermal", priv,
613 &int3400_thermal_ops,
614 &int3400_thermal_params);
615 if (IS_ERR(priv->thermal)) {
616 result = PTR_ERR(priv->thermal);
620 priv->rel_misc_dev_res = acpi_thermal_rel_misc_device_add(
623 result = sysfs_create_group(&pdev->dev.kobj, &uuid_attribute_group);
627 if (acpi_has_method(priv->adev->handle, "IMOK")) {
628 result = sysfs_create_group(&pdev->dev.kobj, &imok_attribute_group);
633 if (!ZERO_OR_NULL_PTR(priv->data_vault)) {
634 result = sysfs_create_group(&pdev->dev.kobj,
635 &data_attribute_group);
640 result = acpi_install_notify_handler(
641 priv->adev->handle, ACPI_DEVICE_NOTIFY, int3400_notify,
646 result = production_mode_init(priv);
653 acpi_remove_notify_handler(priv->adev->handle, ACPI_DEVICE_NOTIFY,
657 if (!ZERO_OR_NULL_PTR(priv->data_vault)) {
658 sysfs_remove_group(&pdev->dev.kobj, &data_attribute_group);
659 kfree(priv->data_vault);
662 sysfs_remove_group(&pdev->dev.kobj, &uuid_attribute_group);
664 sysfs_remove_group(&pdev->dev.kobj, &imok_attribute_group);
666 if (!priv->rel_misc_dev_res)
667 acpi_thermal_rel_misc_device_remove(priv->adev->handle);
668 thermal_zone_device_unregister(priv->thermal);
677 static void int3400_thermal_remove(struct platform_device *pdev)
679 struct int3400_thermal_priv *priv = platform_get_drvdata(pdev);
681 production_mode_exit(priv);
683 acpi_remove_notify_handler(
684 priv->adev->handle, ACPI_DEVICE_NOTIFY,
689 if (!priv->rel_misc_dev_res)
690 acpi_thermal_rel_misc_device_remove(priv->adev->handle);
692 if (!ZERO_OR_NULL_PTR(priv->data_vault))
693 sysfs_remove_group(&pdev->dev.kobj, &data_attribute_group);
694 sysfs_remove_group(&pdev->dev.kobj, &uuid_attribute_group);
695 sysfs_remove_group(&pdev->dev.kobj, &imok_attribute_group);
696 thermal_zone_device_unregister(priv->thermal);
697 kfree(priv->data_vault);
703 static const struct acpi_device_id int3400_thermal_match[] = {
712 MODULE_DEVICE_TABLE(acpi, int3400_thermal_match);
714 static struct platform_driver int3400_thermal_driver = {
715 .probe = int3400_thermal_probe,
716 .remove_new = int3400_thermal_remove,
718 .name = "int3400 thermal",
719 .acpi_match_table = ACPI_PTR(int3400_thermal_match),
723 module_platform_driver(int3400_thermal_driver);
725 MODULE_DESCRIPTION("INT3400 Thermal driver");
726 MODULE_AUTHOR("Zhang Rui <rui.zhang@intel.com>");
727 MODULE_LICENSE("GPL");