1 // SPDX-License-Identifier: GPL-2.0-only
3 * INT3400 thermal driver
5 * Copyright (C) 2014, Intel Corporation
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;
64 struct odvp_attr *odvp_attrs;
67 static int evaluate_odvp(struct int3400_thermal_priv *priv);
71 struct int3400_thermal_priv *priv;
72 struct device_attribute attr;
75 static ssize_t data_vault_read(struct file *file, struct kobject *kobj,
76 struct bin_attribute *attr, char *buf, loff_t off, size_t count)
78 memcpy(buf, attr->private + off, count);
82 static BIN_ATTR_RO(data_vault, 0);
84 static struct bin_attribute *data_attributes[] = {
89 static ssize_t imok_store(struct device *dev, struct device_attribute *attr,
90 const char *buf, size_t count)
92 struct int3400_thermal_priv *priv = dev_get_drvdata(dev);
96 ret = kstrtouint(buf, 10, &input);
99 status = acpi_execute_simple_method(priv->adev->handle, "IMOK", input);
100 if (ACPI_FAILURE(status))
106 static DEVICE_ATTR_WO(imok);
108 static struct attribute *imok_attr[] = {
113 static const struct attribute_group imok_attribute_group = {
117 static const struct attribute_group data_attribute_group = {
118 .bin_attrs = data_attributes,
121 static ssize_t available_uuids_show(struct device *dev,
122 struct device_attribute *attr,
125 struct int3400_thermal_priv *priv = dev_get_drvdata(dev);
129 if (!priv->uuid_bitmap)
130 return sprintf(buf, "UNKNOWN\n");
132 for (i = 0; i < INT3400_THERMAL_MAXIMUM_UUID; i++) {
133 if (priv->uuid_bitmap & (1 << i))
134 length += sysfs_emit_at(buf, length, int3400_thermal_uuids[i]);
140 static ssize_t current_uuid_show(struct device *dev,
141 struct device_attribute *devattr, char *buf)
143 struct int3400_thermal_priv *priv = dev_get_drvdata(dev);
146 if (priv->current_uuid_index > 0)
147 return sprintf(buf, "%s\n",
148 int3400_thermal_uuids[priv->current_uuid_index]);
150 for (i = 0; i <= INT3400_THERMAL_CRITICAL; i++) {
151 if (priv->os_uuid_mask & BIT(i))
152 length += sysfs_emit_at(buf, length, int3400_thermal_uuids[i]);
158 return sprintf(buf, "INVALID\n");
161 static int int3400_thermal_run_osc(acpi_handle handle, char *uuid_str, int *enable)
166 struct acpi_osc_context context = {
167 .uuid_str = uuid_str,
173 buf[OSC_QUERY_DWORD] = 0;
174 buf[OSC_SUPPORT_DWORD] = *enable;
176 status = acpi_run_osc(handle, &context);
177 if (ACPI_SUCCESS(status)) {
178 ret = *((u32 *)(context.ret.pointer + 4));
182 kfree(context.ret.pointer);
189 static int set_os_uuid_mask(struct int3400_thermal_priv *priv, u32 mask)
195 * Bit 0: set to 1 to indicate DPTF is active
196 * Bi1 1: set to 1 to active cooling is supported by user space daemon
197 * Bit 2: set to 1 to passive cooling is supported by user space daemon
198 * Bit 3: set to 1 to critical trip is handled by user space daemon
201 cap = (priv->os_uuid_mask << 1) | 0x01;
203 return int3400_thermal_run_osc(priv->adev->handle,
204 "b23ba85d-c8b7-3542-88de-8de2ffcfd698",
208 static ssize_t current_uuid_store(struct device *dev,
209 struct device_attribute *attr,
210 const char *buf, size_t count)
212 struct int3400_thermal_priv *priv = dev_get_drvdata(dev);
215 for (i = 0; i < INT3400_THERMAL_MAXIMUM_UUID; ++i) {
216 if (!strncmp(buf, int3400_thermal_uuids[i],
217 sizeof(int3400_thermal_uuids[i]) - 1)) {
219 * If we have a list of supported UUIDs, make sure
220 * this one is supported.
222 if (priv->uuid_bitmap & BIT(i)) {
223 priv->current_uuid_index = i;
228 * There is support of only 3 policies via the new
229 * _OSC to inform OS capability:
230 * INT3400_THERMAL_ACTIVE
231 * INT3400_THERMAL_PASSIVE_1
232 * INT3400_THERMAL_CRITICAL
235 if (i > INT3400_THERMAL_CRITICAL)
238 priv->os_uuid_mask |= BIT(i);
244 if (priv->os_uuid_mask) {
245 ret = set_os_uuid_mask(priv, priv->os_uuid_mask);
253 static DEVICE_ATTR_RW(current_uuid);
254 static DEVICE_ATTR_RO(available_uuids);
255 static struct attribute *uuid_attrs[] = {
256 &dev_attr_available_uuids.attr,
257 &dev_attr_current_uuid.attr,
261 static const struct attribute_group uuid_attribute_group = {
266 static int int3400_thermal_get_uuids(struct int3400_thermal_priv *priv)
268 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL};
269 union acpi_object *obja, *objb;
274 status = acpi_evaluate_object(priv->adev->handle, "IDSP", NULL, &buf);
275 if (ACPI_FAILURE(status))
278 obja = (union acpi_object *)buf.pointer;
279 if (obja->type != ACPI_TYPE_PACKAGE) {
284 for (i = 0; i < obja->package.count; i++) {
285 objb = &obja->package.elements[i];
286 if (objb->type != ACPI_TYPE_BUFFER) {
291 /* UUID must be 16 bytes */
292 if (objb->buffer.length != 16) {
297 for (j = 0; j < INT3400_THERMAL_MAXIMUM_UUID; j++) {
300 guid_parse(int3400_thermal_uuids[j], &guid);
301 if (guid_equal((guid_t *)objb->buffer.pointer, &guid)) {
302 priv->uuid_bitmap |= (1 << j);
313 static ssize_t production_mode_show(struct device *dev, struct device_attribute *attr,
316 struct int3400_thermal_priv *priv = dev_get_drvdata(dev);
318 return sysfs_emit(buf, "%d\n", priv->production_mode);
321 static DEVICE_ATTR_RO(production_mode);
323 static int production_mode_init(struct int3400_thermal_priv *priv)
325 unsigned long long mode;
329 priv->production_mode = -1;
331 status = acpi_evaluate_integer(priv->adev->handle, "DCFG", NULL, &mode);
332 /* If the method is not present, this is not an error */
333 if (ACPI_FAILURE(status))
336 ret = sysfs_create_file(&priv->pdev->dev.kobj, &dev_attr_production_mode.attr);
340 priv->production_mode = mode;
345 static void production_mode_exit(struct int3400_thermal_priv *priv)
347 if (priv->production_mode >= 0)
348 sysfs_remove_file(&priv->pdev->dev.kobj, &dev_attr_production_mode.attr);
351 static ssize_t odvp_show(struct device *dev, struct device_attribute *attr,
354 struct odvp_attr *odvp_attr;
356 odvp_attr = container_of(attr, struct odvp_attr, attr);
358 return sprintf(buf, "%d\n", odvp_attr->priv->odvp[odvp_attr->odvp]);
361 static void cleanup_odvp(struct int3400_thermal_priv *priv)
365 if (priv->odvp_attrs) {
366 for (i = 0; i < priv->odvp_count; i++) {
367 sysfs_remove_file(&priv->pdev->dev.kobj,
368 &priv->odvp_attrs[i].attr.attr);
369 kfree(priv->odvp_attrs[i].attr.attr.name);
371 kfree(priv->odvp_attrs);
374 priv->odvp_count = 0;
377 static int evaluate_odvp(struct int3400_thermal_priv *priv)
379 struct acpi_buffer odvp = { ACPI_ALLOCATE_BUFFER, NULL };
380 union acpi_object *obj = NULL;
384 status = acpi_evaluate_object(priv->adev->handle, "ODVP", NULL, &odvp);
385 if (ACPI_FAILURE(status)) {
391 if (obj->type != ACPI_TYPE_PACKAGE) {
396 if (priv->odvp == NULL) {
397 priv->odvp_count = obj->package.count;
398 priv->odvp = kmalloc_array(priv->odvp_count, sizeof(int),
406 if (priv->odvp_attrs == NULL) {
407 priv->odvp_attrs = kcalloc(priv->odvp_count,
408 sizeof(struct odvp_attr),
410 if (!priv->odvp_attrs) {
414 for (i = 0; i < priv->odvp_count; i++) {
415 struct odvp_attr *odvp = &priv->odvp_attrs[i];
417 sysfs_attr_init(&odvp->attr.attr);
420 odvp->attr.attr.name = kasprintf(GFP_KERNEL,
423 if (!odvp->attr.attr.name) {
427 odvp->attr.attr.mode = 0444;
428 odvp->attr.show = odvp_show;
429 odvp->attr.store = NULL;
430 ret = sysfs_create_file(&priv->pdev->dev.kobj,
437 for (i = 0; i < obj->package.count; i++) {
438 if (obj->package.elements[i].type == ACPI_TYPE_INTEGER)
439 priv->odvp[i] = obj->package.elements[i].integer.value;
451 static void int3400_notify(acpi_handle handle,
455 struct int3400_thermal_priv *priv = data;
456 char *thermal_prop[5];
463 case INT3400_THERMAL_TABLE_CHANGED:
464 therm_event = THERMAL_TABLE_CHANGED;
466 case INT3400_KEEP_ALIVE:
467 therm_event = THERMAL_EVENT_KEEP_ALIVE;
469 case INT3400_ODVP_CHANGED:
471 therm_event = THERMAL_DEVICE_POWER_CAPABILITY_CHANGED;
474 /* Ignore unknown notification codes sent to INT3400 device */
478 thermal_prop[0] = kasprintf(GFP_KERNEL, "NAME=%s", priv->thermal->type);
479 thermal_prop[1] = kasprintf(GFP_KERNEL, "TEMP=%d", priv->thermal->temperature);
480 thermal_prop[2] = kasprintf(GFP_KERNEL, "TRIP=");
481 thermal_prop[3] = kasprintf(GFP_KERNEL, "EVENT=%d", therm_event);
482 thermal_prop[4] = NULL;
483 kobject_uevent_env(&priv->thermal->device.kobj, KOBJ_CHANGE, thermal_prop);
484 kfree(thermal_prop[0]);
485 kfree(thermal_prop[1]);
486 kfree(thermal_prop[2]);
487 kfree(thermal_prop[3]);
490 static int int3400_thermal_get_temp(struct thermal_zone_device *thermal,
493 *temp = 20 * 1000; /* faked temp sensor with 20C */
497 static int int3400_thermal_change_mode(struct thermal_zone_device *thermal,
498 enum thermal_device_mode mode)
500 struct int3400_thermal_priv *priv = thermal_zone_device_priv(thermal);
506 if (mode != thermal->mode) {
509 enabled = mode == THERMAL_DEVICE_ENABLED;
511 if (priv->os_uuid_mask) {
513 priv->os_uuid_mask = 0;
514 result = set_os_uuid_mask(priv, priv->os_uuid_mask);
519 if (priv->current_uuid_index < 0 ||
520 priv->current_uuid_index >= INT3400_THERMAL_MAXIMUM_UUID)
523 result = int3400_thermal_run_osc(priv->adev->handle,
524 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_zone_device_register("INT3400 Thermal", 0, 0,
613 priv, &int3400_thermal_ops,
614 &int3400_thermal_params, 0, 0);
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 int 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);
704 static const struct acpi_device_id int3400_thermal_match[] = {
713 MODULE_DEVICE_TABLE(acpi, int3400_thermal_match);
715 static struct platform_driver int3400_thermal_driver = {
716 .probe = int3400_thermal_probe,
717 .remove = int3400_thermal_remove,
719 .name = "int3400 thermal",
720 .acpi_match_table = ACPI_PTR(int3400_thermal_match),
724 module_platform_driver(int3400_thermal_driver);
726 MODULE_DESCRIPTION("INT3400 Thermal driver");
728 MODULE_LICENSE("GPL");