1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * INT3400 thermal driver
4  *
5  * Copyright (C) 2014, Intel Corporation
6  * Authors: Zhang Rui <rui.zhang@intel.com>
7  */
8 
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"
14 
15 #define INT3400_THERMAL_TABLE_CHANGED 0x83
16 #define INT3400_ODVP_CHANGED 0x88
17 #define INT3400_KEEP_ALIVE 0xA0
18 
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,
31 };
32 
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",
44 };
45 
46 struct odvp_attr;
47 
48 struct int3400_thermal_priv {
49 	struct acpi_device *adev;
50 	struct platform_device *pdev;
51 	struct thermal_zone_device *thermal;
52 	int art_count;
53 	struct art *arts;
54 	int trt_count;
55 	struct trt *trts;
56 	u32 uuid_bitmap;
57 	int rel_misc_dev_res;
58 	int current_uuid_index;
59 	char *data_vault;
60 	int odvp_count;
61 	int *odvp;
62 	u32 os_uuid_mask;
63 	struct odvp_attr *odvp_attrs;
64 };
65 
66 static int evaluate_odvp(struct int3400_thermal_priv *priv);
67 
68 struct odvp_attr {
69 	int odvp;
70 	struct int3400_thermal_priv *priv;
71 	struct kobj_attribute attr;
72 };
73 
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)
76 {
77 	memcpy(buf, attr->private + off, count);
78 	return count;
79 }
80 
81 static BIN_ATTR_RO(data_vault, 0);
82 
83 static struct bin_attribute *data_attributes[] = {
84 	&bin_attr_data_vault,
85 	NULL,
86 };
87 
88 static ssize_t imok_store(struct device *dev, struct device_attribute *attr,
89 			  const char *buf, size_t count)
90 {
91 	struct int3400_thermal_priv *priv = dev_get_drvdata(dev);
92 	acpi_status status;
93 	int input, ret;
94 
95 	ret = kstrtouint(buf, 10, &input);
96 	if (ret)
97 		return ret;
98 	status = acpi_execute_simple_method(priv->adev->handle, "IMOK", input);
99 	if (ACPI_FAILURE(status))
100 		return -EIO;
101 
102 	return count;
103 }
104 
105 static DEVICE_ATTR_WO(imok);
106 
107 static struct attribute *imok_attr[] = {
108 	&dev_attr_imok.attr,
109 	NULL
110 };
111 
112 static const struct attribute_group imok_attribute_group = {
113 	.attrs = imok_attr,
114 };
115 
116 static const struct attribute_group data_attribute_group = {
117 	.bin_attrs = data_attributes,
118 };
119 
120 static ssize_t available_uuids_show(struct device *dev,
121 				    struct device_attribute *attr,
122 				    char *buf)
123 {
124 	struct int3400_thermal_priv *priv = dev_get_drvdata(dev);
125 	int i;
126 	int length = 0;
127 
128 	if (!priv->uuid_bitmap)
129 		return sprintf(buf, "UNKNOWN\n");
130 
131 	for (i = 0; i < INT3400_THERMAL_MAXIMUM_UUID; i++) {
132 		if (priv->uuid_bitmap & (1 << i))
133 			length += scnprintf(&buf[length],
134 					    PAGE_SIZE - length,
135 					    "%s\n",
136 					    int3400_thermal_uuids[i]);
137 	}
138 
139 	return length;
140 }
141 
142 static ssize_t current_uuid_show(struct device *dev,
143 				 struct device_attribute *devattr, char *buf)
144 {
145 	struct int3400_thermal_priv *priv = dev_get_drvdata(dev);
146 	int i, length = 0;
147 
148 	if (priv->current_uuid_index > 0)
149 		return sprintf(buf, "%s\n",
150 			       int3400_thermal_uuids[priv->current_uuid_index]);
151 
152 	for (i = 0; i <= INT3400_THERMAL_CRITICAL; i++) {
153 		if (priv->os_uuid_mask & BIT(i))
154 			length += scnprintf(&buf[length],
155 					    PAGE_SIZE - length,
156 					    "%s\n",
157 					    int3400_thermal_uuids[i]);
158 	}
159 
160 	if (length)
161 		return length;
162 
163 	return sprintf(buf, "INVALID\n");
164 }
165 
166 static int int3400_thermal_run_osc(acpi_handle handle, char *uuid_str, int *enable)
167 {
168 	u32 ret, buf[2];
169 	acpi_status status;
170 	int result = 0;
171 	struct acpi_osc_context context = {
172 		.uuid_str = uuid_str,
173 		.rev = 1,
174 		.cap.length = 8,
175 		.cap.pointer = buf,
176 	};
177 
178 	buf[OSC_QUERY_DWORD] = 0;
179 	buf[OSC_SUPPORT_DWORD] = *enable;
180 
181 	status = acpi_run_osc(handle, &context);
182 	if (ACPI_SUCCESS(status)) {
183 		ret = *((u32 *)(context.ret.pointer + 4));
184 		if (ret != *enable)
185 			result = -EPERM;
186 
187 		kfree(context.ret.pointer);
188 	} else
189 		result = -EPERM;
190 
191 	return result;
192 }
193 
194 static ssize_t current_uuid_store(struct device *dev,
195 				  struct device_attribute *attr,
196 				  const char *buf, size_t count)
197 {
198 	struct int3400_thermal_priv *priv = dev_get_drvdata(dev);
199 	int i;
200 
201 	for (i = 0; i < INT3400_THERMAL_MAXIMUM_UUID; ++i) {
202 		if (!strncmp(buf, int3400_thermal_uuids[i],
203 			     sizeof(int3400_thermal_uuids[i]) - 1)) {
204 			/*
205 			 * If we have a list of supported UUIDs, make sure
206 			 * this one is supported.
207 			 */
208 			if (priv->uuid_bitmap & BIT(i)) {
209 				priv->current_uuid_index = i;
210 				return count;
211 			}
212 
213 			/*
214 			 * There is support of only 3 policies via the new
215 			 * _OSC to inform OS capability:
216 			 * INT3400_THERMAL_ACTIVE
217 			 * INT3400_THERMAL_PASSIVE_1
218 			 * INT3400_THERMAL_CRITICAL
219 			 */
220 
221 			if (i > INT3400_THERMAL_CRITICAL)
222 				return -EINVAL;
223 
224 			priv->os_uuid_mask |= BIT(i);
225 
226 			break;
227 		}
228 	}
229 
230 	if (priv->os_uuid_mask) {
231 		int cap, ret;
232 
233 		/*
234 		 * Capability bits:
235 		 * Bit 0: set to 1 to indicate DPTF is active
236 		 * Bi1 1: set to 1 to active cooling is supported by user space daemon
237 		 * Bit 2: set to 1 to passive cooling is supported by user space daemon
238 		 * Bit 3: set to 1 to critical trip is handled by user space daemon
239 		 */
240 		cap = ((priv->os_uuid_mask << 1) | 0x01);
241 		ret = int3400_thermal_run_osc(priv->adev->handle,
242 					      "b23ba85d-c8b7-3542-88de-8de2ffcfd698",
243 					      &cap);
244 		if (ret)
245 			return ret;
246 	}
247 
248 	return count;
249 }
250 
251 static DEVICE_ATTR_RW(current_uuid);
252 static DEVICE_ATTR_RO(available_uuids);
253 static struct attribute *uuid_attrs[] = {
254 	&dev_attr_available_uuids.attr,
255 	&dev_attr_current_uuid.attr,
256 	NULL
257 };
258 
259 static const struct attribute_group uuid_attribute_group = {
260 	.attrs = uuid_attrs,
261 	.name = "uuids"
262 };
263 
264 static int int3400_thermal_get_uuids(struct int3400_thermal_priv *priv)
265 {
266 	struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL};
267 	union acpi_object *obja, *objb;
268 	int i, j;
269 	int result = 0;
270 	acpi_status status;
271 
272 	status = acpi_evaluate_object(priv->adev->handle, "IDSP", NULL, &buf);
273 	if (ACPI_FAILURE(status))
274 		return -ENODEV;
275 
276 	obja = (union acpi_object *)buf.pointer;
277 	if (obja->type != ACPI_TYPE_PACKAGE) {
278 		result = -EINVAL;
279 		goto end;
280 	}
281 
282 	for (i = 0; i < obja->package.count; i++) {
283 		objb = &obja->package.elements[i];
284 		if (objb->type != ACPI_TYPE_BUFFER) {
285 			result = -EINVAL;
286 			goto end;
287 		}
288 
289 		/* UUID must be 16 bytes */
290 		if (objb->buffer.length != 16) {
291 			result = -EINVAL;
292 			goto end;
293 		}
294 
295 		for (j = 0; j < INT3400_THERMAL_MAXIMUM_UUID; j++) {
296 			guid_t guid;
297 
298 			guid_parse(int3400_thermal_uuids[j], &guid);
299 			if (guid_equal((guid_t *)objb->buffer.pointer, &guid)) {
300 				priv->uuid_bitmap |= (1 << j);
301 				break;
302 			}
303 		}
304 	}
305 
306 end:
307 	kfree(buf.pointer);
308 	return result;
309 }
310 
311 static ssize_t odvp_show(struct kobject *kobj, struct kobj_attribute *attr,
312 			 char *buf)
313 {
314 	struct odvp_attr *odvp_attr;
315 
316 	odvp_attr = container_of(attr, struct odvp_attr, attr);
317 
318 	return sprintf(buf, "%d\n", odvp_attr->priv->odvp[odvp_attr->odvp]);
319 }
320 
321 static void cleanup_odvp(struct int3400_thermal_priv *priv)
322 {
323 	int i;
324 
325 	if (priv->odvp_attrs) {
326 		for (i = 0; i < priv->odvp_count; i++) {
327 			sysfs_remove_file(&priv->pdev->dev.kobj,
328 					  &priv->odvp_attrs[i].attr.attr);
329 			kfree(priv->odvp_attrs[i].attr.attr.name);
330 		}
331 		kfree(priv->odvp_attrs);
332 	}
333 	kfree(priv->odvp);
334 	priv->odvp_count = 0;
335 }
336 
337 static int evaluate_odvp(struct int3400_thermal_priv *priv)
338 {
339 	struct acpi_buffer odvp = { ACPI_ALLOCATE_BUFFER, NULL };
340 	union acpi_object *obj = NULL;
341 	acpi_status status;
342 	int i, ret;
343 
344 	status = acpi_evaluate_object(priv->adev->handle, "ODVP", NULL, &odvp);
345 	if (ACPI_FAILURE(status)) {
346 		ret = -EINVAL;
347 		goto out_err;
348 	}
349 
350 	obj = odvp.pointer;
351 	if (obj->type != ACPI_TYPE_PACKAGE) {
352 		ret = -EINVAL;
353 		goto out_err;
354 	}
355 
356 	if (priv->odvp == NULL) {
357 		priv->odvp_count = obj->package.count;
358 		priv->odvp = kmalloc_array(priv->odvp_count, sizeof(int),
359 				     GFP_KERNEL);
360 		if (!priv->odvp) {
361 			ret = -ENOMEM;
362 			goto out_err;
363 		}
364 	}
365 
366 	if (priv->odvp_attrs == NULL) {
367 		priv->odvp_attrs = kcalloc(priv->odvp_count,
368 					   sizeof(struct odvp_attr),
369 					   GFP_KERNEL);
370 		if (!priv->odvp_attrs) {
371 			ret = -ENOMEM;
372 			goto out_err;
373 		}
374 		for (i = 0; i < priv->odvp_count; i++) {
375 			struct odvp_attr *odvp = &priv->odvp_attrs[i];
376 
377 			sysfs_attr_init(&odvp->attr.attr);
378 			odvp->priv = priv;
379 			odvp->odvp = i;
380 			odvp->attr.attr.name = kasprintf(GFP_KERNEL,
381 							 "odvp%d", i);
382 
383 			if (!odvp->attr.attr.name) {
384 				ret = -ENOMEM;
385 				goto out_err;
386 			}
387 			odvp->attr.attr.mode = 0444;
388 			odvp->attr.show = odvp_show;
389 			odvp->attr.store = NULL;
390 			ret = sysfs_create_file(&priv->pdev->dev.kobj,
391 						&odvp->attr.attr);
392 			if (ret)
393 				goto out_err;
394 		}
395 	}
396 
397 	for (i = 0; i < obj->package.count; i++) {
398 		if (obj->package.elements[i].type == ACPI_TYPE_INTEGER)
399 			priv->odvp[i] = obj->package.elements[i].integer.value;
400 	}
401 
402 	kfree(obj);
403 	return 0;
404 
405 out_err:
406 	cleanup_odvp(priv);
407 	kfree(obj);
408 	return ret;
409 }
410 
411 static void int3400_notify(acpi_handle handle,
412 			u32 event,
413 			void *data)
414 {
415 	struct int3400_thermal_priv *priv = data;
416 	char *thermal_prop[5];
417 	int therm_event;
418 
419 	if (!priv)
420 		return;
421 
422 	switch (event) {
423 	case INT3400_THERMAL_TABLE_CHANGED:
424 		therm_event = THERMAL_TABLE_CHANGED;
425 		break;
426 	case INT3400_KEEP_ALIVE:
427 		therm_event = THERMAL_EVENT_KEEP_ALIVE;
428 		break;
429 	case INT3400_ODVP_CHANGED:
430 		evaluate_odvp(priv);
431 		therm_event = THERMAL_DEVICE_POWER_CAPABILITY_CHANGED;
432 		break;
433 	default:
434 		/* Ignore unknown notification codes sent to INT3400 device */
435 		return;
436 	}
437 
438 	thermal_prop[0] = kasprintf(GFP_KERNEL, "NAME=%s", priv->thermal->type);
439 	thermal_prop[1] = kasprintf(GFP_KERNEL, "TEMP=%d", priv->thermal->temperature);
440 	thermal_prop[2] = kasprintf(GFP_KERNEL, "TRIP=");
441 	thermal_prop[3] = kasprintf(GFP_KERNEL, "EVENT=%d", therm_event);
442 	thermal_prop[4] = NULL;
443 	kobject_uevent_env(&priv->thermal->device.kobj, KOBJ_CHANGE, thermal_prop);
444 	kfree(thermal_prop[0]);
445 	kfree(thermal_prop[1]);
446 	kfree(thermal_prop[2]);
447 	kfree(thermal_prop[3]);
448 }
449 
450 static int int3400_thermal_get_temp(struct thermal_zone_device *thermal,
451 			int *temp)
452 {
453 	*temp = 20 * 1000; /* faked temp sensor with 20C */
454 	return 0;
455 }
456 
457 static int int3400_thermal_change_mode(struct thermal_zone_device *thermal,
458 				       enum thermal_device_mode mode)
459 {
460 	struct int3400_thermal_priv *priv = thermal->devdata;
461 	int result = 0;
462 
463 	if (!priv)
464 		return -EINVAL;
465 
466 	if (mode != thermal->mode) {
467 		int enabled;
468 
469 		if (priv->current_uuid_index < 0 ||
470 		    priv->current_uuid_index >= INT3400_THERMAL_MAXIMUM_UUID)
471 			return -EINVAL;
472 
473 		enabled = (mode == THERMAL_DEVICE_ENABLED);
474 		result = int3400_thermal_run_osc(priv->adev->handle,
475 						 int3400_thermal_uuids[priv->current_uuid_index],
476 						 &enabled);
477 	}
478 
479 
480 	evaluate_odvp(priv);
481 
482 	return result;
483 }
484 
485 static struct thermal_zone_device_ops int3400_thermal_ops = {
486 	.get_temp = int3400_thermal_get_temp,
487 	.change_mode = int3400_thermal_change_mode,
488 };
489 
490 static struct thermal_zone_params int3400_thermal_params = {
491 	.governor_name = "user_space",
492 	.no_hwmon = true,
493 };
494 
495 static void int3400_setup_gddv(struct int3400_thermal_priv *priv)
496 {
497 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
498 	union acpi_object *obj;
499 	acpi_status status;
500 
501 	status = acpi_evaluate_object(priv->adev->handle, "GDDV", NULL,
502 				      &buffer);
503 	if (ACPI_FAILURE(status) || !buffer.length)
504 		return;
505 
506 	obj = buffer.pointer;
507 	if (obj->type != ACPI_TYPE_PACKAGE || obj->package.count != 1
508 	    || obj->package.elements[0].type != ACPI_TYPE_BUFFER)
509 		goto out_free;
510 
511 	priv->data_vault = kmemdup(obj->package.elements[0].buffer.pointer,
512 				   obj->package.elements[0].buffer.length,
513 				   GFP_KERNEL);
514 	if (!priv->data_vault)
515 		goto out_free;
516 
517 	bin_attr_data_vault.private = priv->data_vault;
518 	bin_attr_data_vault.size = obj->package.elements[0].buffer.length;
519 out_free:
520 	kfree(buffer.pointer);
521 }
522 
523 static int int3400_thermal_probe(struct platform_device *pdev)
524 {
525 	struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
526 	struct int3400_thermal_priv *priv;
527 	int result;
528 
529 	if (!adev)
530 		return -ENODEV;
531 
532 	priv = kzalloc(sizeof(struct int3400_thermal_priv), GFP_KERNEL);
533 	if (!priv)
534 		return -ENOMEM;
535 
536 	priv->pdev = pdev;
537 	priv->adev = adev;
538 
539 	result = int3400_thermal_get_uuids(priv);
540 
541 	/* Missing IDSP isn't fatal */
542 	if (result && result != -ENODEV)
543 		goto free_priv;
544 
545 	priv->current_uuid_index = -1;
546 
547 	result = acpi_parse_art(priv->adev->handle, &priv->art_count,
548 				&priv->arts, true);
549 	if (result)
550 		dev_dbg(&pdev->dev, "_ART table parsing error\n");
551 
552 	result = acpi_parse_trt(priv->adev->handle, &priv->trt_count,
553 				&priv->trts, true);
554 	if (result)
555 		dev_dbg(&pdev->dev, "_TRT table parsing error\n");
556 
557 	platform_set_drvdata(pdev, priv);
558 
559 	int3400_setup_gddv(priv);
560 
561 	evaluate_odvp(priv);
562 
563 	priv->thermal = thermal_zone_device_register("INT3400 Thermal", 0, 0,
564 						priv, &int3400_thermal_ops,
565 						&int3400_thermal_params, 0, 0);
566 	if (IS_ERR(priv->thermal)) {
567 		result = PTR_ERR(priv->thermal);
568 		goto free_art_trt;
569 	}
570 
571 	priv->rel_misc_dev_res = acpi_thermal_rel_misc_device_add(
572 							priv->adev->handle);
573 
574 	result = sysfs_create_group(&pdev->dev.kobj, &uuid_attribute_group);
575 	if (result)
576 		goto free_rel_misc;
577 
578 	if (acpi_has_method(priv->adev->handle, "IMOK")) {
579 		result = sysfs_create_group(&pdev->dev.kobj, &imok_attribute_group);
580 		if (result)
581 			goto free_imok;
582 	}
583 
584 	if (priv->data_vault) {
585 		result = sysfs_create_group(&pdev->dev.kobj,
586 					    &data_attribute_group);
587 		if (result)
588 			goto free_uuid;
589 	}
590 
591 	result = acpi_install_notify_handler(
592 			priv->adev->handle, ACPI_DEVICE_NOTIFY, int3400_notify,
593 			(void *)priv);
594 	if (result)
595 		goto free_sysfs;
596 
597 	return 0;
598 
599 free_sysfs:
600 	cleanup_odvp(priv);
601 	if (priv->data_vault) {
602 		sysfs_remove_group(&pdev->dev.kobj, &data_attribute_group);
603 		kfree(priv->data_vault);
604 	}
605 free_uuid:
606 	sysfs_remove_group(&pdev->dev.kobj, &uuid_attribute_group);
607 free_imok:
608 	sysfs_remove_group(&pdev->dev.kobj, &imok_attribute_group);
609 free_rel_misc:
610 	if (!priv->rel_misc_dev_res)
611 		acpi_thermal_rel_misc_device_remove(priv->adev->handle);
612 	thermal_zone_device_unregister(priv->thermal);
613 free_art_trt:
614 	kfree(priv->trts);
615 	kfree(priv->arts);
616 free_priv:
617 	kfree(priv);
618 	return result;
619 }
620 
621 static int int3400_thermal_remove(struct platform_device *pdev)
622 {
623 	struct int3400_thermal_priv *priv = platform_get_drvdata(pdev);
624 
625 	acpi_remove_notify_handler(
626 			priv->adev->handle, ACPI_DEVICE_NOTIFY,
627 			int3400_notify);
628 
629 	cleanup_odvp(priv);
630 
631 	if (!priv->rel_misc_dev_res)
632 		acpi_thermal_rel_misc_device_remove(priv->adev->handle);
633 
634 	if (priv->data_vault)
635 		sysfs_remove_group(&pdev->dev.kobj, &data_attribute_group);
636 	sysfs_remove_group(&pdev->dev.kobj, &uuid_attribute_group);
637 	sysfs_remove_group(&pdev->dev.kobj, &imok_attribute_group);
638 	thermal_zone_device_unregister(priv->thermal);
639 	kfree(priv->data_vault);
640 	kfree(priv->trts);
641 	kfree(priv->arts);
642 	kfree(priv);
643 	return 0;
644 }
645 
646 static const struct acpi_device_id int3400_thermal_match[] = {
647 	{"INT3400", 0},
648 	{"INTC1040", 0},
649 	{"INTC1041", 0},
650 	{"INTC10A0", 0},
651 	{}
652 };
653 
654 MODULE_DEVICE_TABLE(acpi, int3400_thermal_match);
655 
656 static struct platform_driver int3400_thermal_driver = {
657 	.probe = int3400_thermal_probe,
658 	.remove = int3400_thermal_remove,
659 	.driver = {
660 		   .name = "int3400 thermal",
661 		   .acpi_match_table = ACPI_PTR(int3400_thermal_match),
662 		   },
663 };
664 
665 module_platform_driver(int3400_thermal_driver);
666 
667 MODULE_DESCRIPTION("INT3400 Thermal driver");
668 MODULE_AUTHOR("Zhang Rui <rui.zhang@intel.com>");
669 MODULE_LICENSE("GPL");
670