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