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
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 BIN_ATTR_SIMPLE_RO(data_vault);
78 static ssize_t imok_store(struct device *dev, struct device_attribute *attr,
79 const char *buf, size_t count)
81 struct int3400_thermal_priv *priv = dev_get_drvdata(dev);
85 ret = kstrtouint(buf, 10, &input);
88 status = acpi_execute_simple_method(priv->adev->handle, "IMOK", input);
89 if (ACPI_FAILURE(status))
95 static DEVICE_ATTR_WO(imok);
97 static struct attribute *imok_attr[] = {
102 static const struct attribute_group imok_attribute_group = {
106 static ssize_t available_uuids_show(struct device *dev,
107 struct device_attribute *attr,
110 struct int3400_thermal_priv *priv = dev_get_drvdata(dev);
114 if (!priv->uuid_bitmap)
115 return sprintf(buf, "UNKNOWN\n");
117 for (i = 0; i < INT3400_THERMAL_MAXIMUM_UUID; i++) {
118 if (priv->uuid_bitmap & (1 << i))
119 length += sysfs_emit_at(buf, length, "%s\n", int3400_thermal_uuids[i]);
125 static ssize_t current_uuid_show(struct device *dev,
126 struct device_attribute *devattr, char *buf)
128 struct int3400_thermal_priv *priv = dev_get_drvdata(dev);
131 if (priv->current_uuid_index >= 0)
132 return sprintf(buf, "%s\n",
133 int3400_thermal_uuids[priv->current_uuid_index]);
135 for (i = 0; i <= INT3400_THERMAL_CRITICAL; i++) {
136 if (priv->os_uuid_mask & BIT(i))
137 length += sysfs_emit_at(buf, length, "%s\n", int3400_thermal_uuids[i]);
143 return sprintf(buf, "INVALID\n");
146 static int int3400_thermal_run_osc(acpi_handle handle, char *uuid_str, int *enable)
151 struct acpi_osc_context context = {
152 .uuid_str = uuid_str,
158 buf[OSC_QUERY_DWORD] = 0;
159 buf[OSC_SUPPORT_DWORD] = *enable;
161 status = acpi_run_osc(handle, &context);
162 if (ACPI_SUCCESS(status)) {
163 ret = *((u32 *)(context.ret.pointer + 4));
167 kfree(context.ret.pointer);
174 static int set_os_uuid_mask(struct int3400_thermal_priv *priv, u32 mask)
180 * Bit 0: set to 1 to indicate DPTF is active
181 * Bi1 1: set to 1 to active cooling is supported by user space daemon
182 * Bit 2: set to 1 to passive cooling is supported by user space daemon
183 * Bit 3: set to 1 to critical trip is handled by user space daemon
186 cap = (priv->os_uuid_mask << 1) | 0x01;
188 return int3400_thermal_run_osc(priv->adev->handle,
189 "b23ba85d-c8b7-3542-88de-8de2ffcfd698",
193 static ssize_t current_uuid_store(struct device *dev,
194 struct device_attribute *attr,
195 const char *buf, size_t count)
197 struct int3400_thermal_priv *priv = dev_get_drvdata(dev);
200 for (i = 0; i < INT3400_THERMAL_MAXIMUM_UUID; ++i) {
201 if (!strncmp(buf, int3400_thermal_uuids[i],
202 sizeof(int3400_thermal_uuids[i]) - 1)) {
204 * If we have a list of supported UUIDs, make sure
205 * this one is supported.
207 if (priv->uuid_bitmap & BIT(i)) {
208 priv->current_uuid_index = i;
213 * There is support of only 3 policies via the new
214 * _OSC to inform OS capability:
215 * INT3400_THERMAL_ACTIVE
216 * INT3400_THERMAL_PASSIVE_1
217 * INT3400_THERMAL_CRITICAL
220 if (i > INT3400_THERMAL_CRITICAL)
223 priv->os_uuid_mask |= BIT(i);
229 if (priv->os_uuid_mask) {
230 ret = set_os_uuid_mask(priv, priv->os_uuid_mask);
238 static DEVICE_ATTR_RW(current_uuid);
239 static DEVICE_ATTR_RO(available_uuids);
240 static struct attribute *uuid_attrs[] = {
241 &dev_attr_available_uuids.attr,
242 &dev_attr_current_uuid.attr,
246 static const struct attribute_group uuid_attribute_group = {
251 static int int3400_thermal_get_uuids(struct int3400_thermal_priv *priv)
253 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL};
254 union acpi_object *obja, *objb;
259 status = acpi_evaluate_object(priv->adev->handle, "IDSP", NULL, &buf);
260 if (ACPI_FAILURE(status))
263 obja = (union acpi_object *)buf.pointer;
264 if (obja->type != ACPI_TYPE_PACKAGE) {
269 for (i = 0; i < obja->package.count; i++) {
270 objb = &obja->package.elements[i];
271 if (objb->type != ACPI_TYPE_BUFFER) {
276 /* UUID must be 16 bytes */
277 if (objb->buffer.length != 16) {
282 for (j = 0; j < INT3400_THERMAL_MAXIMUM_UUID; j++) {
285 guid_parse(int3400_thermal_uuids[j], &guid);
286 if (guid_equal((guid_t *)objb->buffer.pointer, &guid)) {
287 priv->uuid_bitmap |= (1 << j);
298 static ssize_t production_mode_show(struct device *dev, struct device_attribute *attr,
301 struct int3400_thermal_priv *priv = dev_get_drvdata(dev);
303 return sysfs_emit(buf, "%d\n", priv->production_mode);
306 static DEVICE_ATTR_RO(production_mode);
308 static int production_mode_init(struct int3400_thermal_priv *priv)
310 unsigned long long mode;
314 priv->production_mode = -1;
316 status = acpi_evaluate_integer(priv->adev->handle, "DCFG", NULL, &mode);
317 /* If the method is not present, this is not an error */
318 if (ACPI_FAILURE(status))
321 ret = sysfs_create_file(&priv->pdev->dev.kobj, &dev_attr_production_mode.attr);
325 priv->production_mode = mode;
330 static void production_mode_exit(struct int3400_thermal_priv *priv)
332 if (priv->production_mode >= 0)
333 sysfs_remove_file(&priv->pdev->dev.kobj, &dev_attr_production_mode.attr);
336 static ssize_t odvp_show(struct device *dev, struct device_attribute *attr,
339 struct odvp_attr *odvp_attr;
341 odvp_attr = container_of(attr, struct odvp_attr, attr);
343 return sprintf(buf, "%d\n", odvp_attr->priv->odvp[odvp_attr->odvp]);
346 static void cleanup_odvp(struct int3400_thermal_priv *priv)
350 if (priv->odvp_attrs) {
351 for (i = 0; i < priv->odvp_count; i++) {
352 sysfs_remove_file(&priv->pdev->dev.kobj,
353 &priv->odvp_attrs[i].attr.attr);
354 kfree(priv->odvp_attrs[i].attr.attr.name);
356 kfree(priv->odvp_attrs);
359 priv->odvp_count = 0;
362 static int evaluate_odvp(struct int3400_thermal_priv *priv)
364 struct acpi_buffer odvp = { ACPI_ALLOCATE_BUFFER, NULL };
365 union acpi_object *obj = NULL;
369 status = acpi_evaluate_object(priv->adev->handle, "ODVP", NULL, &odvp);
370 if (ACPI_FAILURE(status)) {
376 if (obj->type != ACPI_TYPE_PACKAGE) {
381 if (priv->odvp == NULL) {
382 priv->odvp_count = obj->package.count;
383 priv->odvp = kmalloc_array(priv->odvp_count, sizeof(int),
391 if (priv->odvp_attrs == NULL) {
392 priv->odvp_attrs = kcalloc(priv->odvp_count,
393 sizeof(struct odvp_attr),
395 if (!priv->odvp_attrs) {
399 for (i = 0; i < priv->odvp_count; i++) {
400 struct odvp_attr *odvp = &priv->odvp_attrs[i];
402 sysfs_attr_init(&odvp->attr.attr);
405 odvp->attr.attr.name = kasprintf(GFP_KERNEL,
408 if (!odvp->attr.attr.name) {
412 odvp->attr.attr.mode = 0444;
413 odvp->attr.show = odvp_show;
414 odvp->attr.store = NULL;
415 ret = sysfs_create_file(&priv->pdev->dev.kobj,
422 for (i = 0; i < obj->package.count; i++) {
423 if (obj->package.elements[i].type == ACPI_TYPE_INTEGER)
424 priv->odvp[i] = obj->package.elements[i].integer.value;
436 static void int3400_notify(acpi_handle handle,
440 struct int3400_thermal_priv *priv = data;
442 char *thermal_prop[5];
449 case INT3400_THERMAL_TABLE_CHANGED:
450 therm_event = THERMAL_TABLE_CHANGED;
452 case INT3400_KEEP_ALIVE:
453 therm_event = THERMAL_EVENT_KEEP_ALIVE;
455 case INT3400_ODVP_CHANGED:
457 therm_event = THERMAL_DEVICE_POWER_CAPABILITY_CHANGED;
460 /* Ignore unknown notification codes sent to INT3400 device */
464 dev = thermal_zone_device(priv->thermal);
466 thermal_prop[0] = kasprintf(GFP_KERNEL, "NAME=%s", thermal_zone_device_type(priv->thermal));
467 thermal_prop[1] = kasprintf(GFP_KERNEL, "TEMP=%d", INT3400_FAKE_TEMP);
468 thermal_prop[2] = kasprintf(GFP_KERNEL, "TRIP=");
469 thermal_prop[3] = kasprintf(GFP_KERNEL, "EVENT=%d", therm_event);
470 thermal_prop[4] = NULL;
471 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, thermal_prop);
472 kfree(thermal_prop[0]);
473 kfree(thermal_prop[1]);
474 kfree(thermal_prop[2]);
475 kfree(thermal_prop[3]);
478 static int int3400_thermal_get_temp(struct thermal_zone_device *thermal,
481 *temp = INT3400_FAKE_TEMP;
485 static int int3400_thermal_change_mode(struct thermal_zone_device *thermal,
486 enum thermal_device_mode mode)
488 struct int3400_thermal_priv *priv = thermal_zone_device_priv(thermal);
495 enabled = mode == THERMAL_DEVICE_ENABLED;
497 if (priv->os_uuid_mask) {
499 priv->os_uuid_mask = 0;
500 result = set_os_uuid_mask(priv, priv->os_uuid_mask);
505 if (priv->current_uuid_index < 0 ||
506 priv->current_uuid_index >= INT3400_THERMAL_MAXIMUM_UUID)
509 result = int3400_thermal_run_osc(priv->adev->handle,
510 int3400_thermal_uuids[priv->current_uuid_index],
518 static struct thermal_zone_device_ops int3400_thermal_ops = {
519 .get_temp = int3400_thermal_get_temp,
520 .change_mode = int3400_thermal_change_mode,
523 static struct thermal_zone_params int3400_thermal_params = {
524 .governor_name = "user_space",
528 static void int3400_setup_gddv(struct int3400_thermal_priv *priv)
530 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
531 union acpi_object *obj;
534 status = acpi_evaluate_object(priv->adev->handle, "GDDV", NULL,
536 if (ACPI_FAILURE(status) || !buffer.length)
539 obj = buffer.pointer;
540 if (obj->type != ACPI_TYPE_PACKAGE || obj->package.count != 1
541 || obj->package.elements[0].type != ACPI_TYPE_BUFFER)
544 priv->data_vault = kmemdup(obj->package.elements[0].buffer.pointer,
545 obj->package.elements[0].buffer.length,
547 if (ZERO_OR_NULL_PTR(priv->data_vault))
550 bin_attr_data_vault.private = priv->data_vault;
551 bin_attr_data_vault.size = obj->package.elements[0].buffer.length;
553 kfree(buffer.pointer);
556 static int int3400_thermal_probe(struct platform_device *pdev)
558 struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
559 struct int3400_thermal_priv *priv;
565 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
572 result = int3400_thermal_get_uuids(priv);
574 /* Missing IDSP isn't fatal */
575 if (result && result != -ENODEV)
578 priv->current_uuid_index = -1;
580 result = acpi_parse_art(priv->adev->handle, &priv->art_count,
583 dev_dbg(&pdev->dev, "_ART table parsing error\n");
585 result = acpi_parse_trt(priv->adev->handle, &priv->trt_count,
588 dev_dbg(&pdev->dev, "_TRT table parsing error\n");
590 platform_set_drvdata(pdev, priv);
592 int3400_setup_gddv(priv);
596 priv->thermal = thermal_tripless_zone_device_register("INT3400 Thermal", priv,
597 &int3400_thermal_ops,
598 &int3400_thermal_params);
599 if (IS_ERR(priv->thermal)) {
600 result = PTR_ERR(priv->thermal);
604 priv->rel_misc_dev_res = acpi_thermal_rel_misc_device_add(
607 result = sysfs_create_group(&pdev->dev.kobj, &uuid_attribute_group);
611 if (acpi_has_method(priv->adev->handle, "IMOK")) {
612 result = sysfs_create_group(&pdev->dev.kobj, &imok_attribute_group);
617 if (!ZERO_OR_NULL_PTR(priv->data_vault)) {
618 result = device_create_bin_file(&pdev->dev, &bin_attr_data_vault);
623 result = acpi_install_notify_handler(
624 priv->adev->handle, ACPI_DEVICE_NOTIFY, int3400_notify,
629 result = production_mode_init(priv);
636 acpi_remove_notify_handler(priv->adev->handle, ACPI_DEVICE_NOTIFY,
640 if (!ZERO_OR_NULL_PTR(priv->data_vault)) {
641 device_remove_bin_file(&pdev->dev, &bin_attr_data_vault);
642 kfree(priv->data_vault);
645 sysfs_remove_group(&pdev->dev.kobj, &uuid_attribute_group);
647 sysfs_remove_group(&pdev->dev.kobj, &imok_attribute_group);
649 if (!priv->rel_misc_dev_res)
650 acpi_thermal_rel_misc_device_remove(priv->adev->handle);
651 thermal_zone_device_unregister(priv->thermal);
660 static void int3400_thermal_remove(struct platform_device *pdev)
662 struct int3400_thermal_priv *priv = platform_get_drvdata(pdev);
664 production_mode_exit(priv);
666 acpi_remove_notify_handler(
667 priv->adev->handle, ACPI_DEVICE_NOTIFY,
672 if (!priv->rel_misc_dev_res)
673 acpi_thermal_rel_misc_device_remove(priv->adev->handle);
675 if (!ZERO_OR_NULL_PTR(priv->data_vault))
676 device_remove_bin_file(&pdev->dev, &bin_attr_data_vault);
677 sysfs_remove_group(&pdev->dev.kobj, &uuid_attribute_group);
678 sysfs_remove_group(&pdev->dev.kobj, &imok_attribute_group);
679 thermal_zone_device_unregister(priv->thermal);
680 kfree(priv->data_vault);
686 static const struct acpi_device_id int3400_thermal_match[] = {
696 MODULE_DEVICE_TABLE(acpi, int3400_thermal_match);
698 static struct platform_driver int3400_thermal_driver = {
699 .probe = int3400_thermal_probe,
700 .remove = int3400_thermal_remove,
702 .name = "int3400 thermal",
703 .acpi_match_table = ACPI_PTR(int3400_thermal_match),
707 module_platform_driver(int3400_thermal_driver);
709 MODULE_DESCRIPTION("INT3400 Thermal driver");
711 MODULE_LICENSE("GPL");