1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
|
/*
* drivers/misc/therm_est.c
*
* Copyright (c) 2010-2017, NVIDIA CORPORATION. All rights reserved.
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include <linux/platform_device.h>
#include <linux/kernel.h>
#include <linux/cpufreq.h>
#include <linux/delay.h>
#include <linux/mutex.h>
#include <linux/init.h>
#include <linux/err.h>
#include <linux/clk.h>
#include <linux/debugfs.h>
#include <linux/seq_file.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include <linux/syscalls.h>
#include <linux/therm_est.h>
#include <linux/thermal.h>
#include <linux/module.h>
#include <linux/hwmon-sysfs.h>
#include <linux/suspend.h>
#include <linux/version.h>
#define DEFAULT_TSKIN 25000 /* default tskin in mC */
struct therm_estimator {
struct thermal_zone_device *thz;
struct thermal_cooling_device *cdev; /* activation device */
struct workqueue_struct *workqueue;
struct delayed_work therm_est_work;
long cur_temp;
long low_limit;
long high_limit;
long polling_period;
int polling_enabled;
int tc1;
int tc2;
struct therm_est_subdevice *subdevice;
bool *tripped_info;
int use_activator;
#ifdef CONFIG_PM
struct notifier_block pm_nb;
#endif
};
static int therm_est_subdev_match(struct thermal_zone_device *thz, void *data)
{
return strcmp((char *)data, thz->type) == 0;
}
static int therm_est_subdev_get_temp(struct thermal_zone_device *thz,
int *temp)
{
if (!thz || !thz->ops->get_temp || thz->ops->get_temp(thz, temp))
*temp = 25000;
return 0;
}
static void therm_est_update_limits(struct therm_estimator *est)
{
const int MAX_HIGH_TEMP = 128000;
long low_temp = 0, high_temp = MAX_HIGH_TEMP;
int trip_temp, passive_low_temp = MAX_HIGH_TEMP;
enum thermal_trip_type trip_type;
int hysteresis, zone_temp;
int i;
zone_temp = est->thz->temperature;
for (i = 0; i < est->thz->trips; i++) {
est->thz->ops->get_trip_temp(est->thz, i, &trip_temp);
est->thz->ops->get_trip_hyst(est->thz, i, &hysteresis);
est->thz->ops->get_trip_type(est->thz, i, &trip_type);
if (zone_temp >= trip_temp) {
trip_temp -= hysteresis;
est->tripped_info[i] = true;
} else if (est->tripped_info[i]) {
trip_temp -= hysteresis;
if (zone_temp < trip_temp)
est->tripped_info[i] = false;
}
if (!est->tripped_info[i]) { /* not tripped? update high */
if (trip_temp < high_temp)
high_temp = trip_temp;
} else { /* tripped? update low */
if (trip_type != THERMAL_TRIP_PASSIVE) {
/* get highest ACTIVE */
if (trip_temp > low_temp)
low_temp = trip_temp;
} else {
/* get lowest PASSIVE */
if (trip_temp < passive_low_temp)
passive_low_temp = trip_temp;
}
}
}
if (passive_low_temp != MAX_HIGH_TEMP)
low_temp = max_t(long, low_temp, passive_low_temp);
est->low_limit = low_temp;
est->high_limit = high_temp;
}
static void therm_est_work_func(struct work_struct *work)
{
struct delayed_work *dwork = container_of(work,
struct delayed_work, work);
struct therm_estimator *est = container_of(dwork,
struct therm_estimator,
therm_est_work);
struct therm_est_subdevice *subdevice;
struct therm_est_coeffs *coeffs_set;
s32 *thz_coeffs;
long *hist;
int temp;
int i, j, index, sum = 0;
subdevice = est->subdevice;
coeffs_set = &subdevice->coeffs_set[subdevice->active_coeffs];
subdevice->ntemp = (subdevice->ntemp + 1) % HIST_LEN;
for (i = 0; i < subdevice->num_devs; i++) {
if (therm_est_subdev_get_temp(subdevice->sub_thz[i].thz, &temp))
continue;
subdevice->sub_thz[i].hist[subdevice->ntemp] = temp;
}
for (i = 0; i < subdevice->num_devs; i++) {
hist = subdevice->sub_thz[i].hist;
thz_coeffs = coeffs_set->coeffs[i];
for (j = 0; j < HIST_LEN; j++) {
index = (subdevice->ntemp - j + HIST_LEN) % HIST_LEN;
sum += hist[index] * thz_coeffs[j];
}
}
est->cur_temp = sum / 100 + coeffs_set->toffset;
if (est->thz && ((est->cur_temp < est->low_limit) ||
(est->cur_temp >= est->high_limit))) {
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
thermal_zone_device_update(est->thz, THERMAL_EVENT_UNSPECIFIED);
#else
thermal_zone_device_update(est->thz);
#endif
therm_est_update_limits(est);
}
if (est->polling_enabled > 0 || !est->use_activator) {
queue_delayed_work(est->workqueue, &est->therm_est_work,
msecs_to_jiffies(est->polling_period));
}
}
static int therm_est_get_temp(void *of_data, int *temp)
{
struct therm_estimator *est = (struct therm_estimator *)of_data;
*temp = est->cur_temp;
return 0;
}
static int therm_est_get_trend(void *of_data, int trip,
enum thermal_trend *trend)
{
struct therm_estimator *est = (struct therm_estimator *)of_data;
if (IS_ERR_OR_NULL(est->thz))
*trend = THERMAL_TREND_STABLE;
else if (est->thz->temperature > est->thz->last_temperature + 100)
*trend = THERMAL_TREND_RAISING;
else if (est->thz->temperature < est->thz->last_temperature - 100)
*trend = THERMAL_TREND_DROPPING;
else
*trend = THERMAL_TREND_STABLE;
return 0;
}
static int therm_est_trip_update(void *of_data, int trip)
{
struct therm_estimator *est = (struct therm_estimator *)of_data;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
thermal_zone_device_update(est->thz, THERMAL_EVENT_UNSPECIFIED);
#else
thermal_zone_device_update(est->thz);
#endif
therm_est_update_limits(est);
return 0;
}
static int therm_est_init_history(struct therm_estimator *est)
{
struct therm_est_sub_thz *sub_thz;
int temp;
int i, j;
sub_thz = est->subdevice->sub_thz;
for (i = 0; i < est->subdevice->num_devs; i++) {
if (therm_est_subdev_get_temp(sub_thz[i].thz, &temp))
return -EINVAL;
for (j = 0; j < HIST_LEN; j++)
sub_thz[i].hist[j] = temp;
}
return 0;
}
static int therm_est_polling(struct therm_estimator *est,
int polling)
{
est->polling_enabled = polling > 0;
if (est->polling_enabled > 0) {
est->low_limit = 0;
est->high_limit = 0;
therm_est_init_history(est);
queue_delayed_work(est->workqueue,
&est->therm_est_work,
msecs_to_jiffies(est->polling_period));
} else {
cancel_delayed_work_sync(&est->therm_est_work);
est->cur_temp = DEFAULT_TSKIN;
}
return 0;
}
static int switch_active_coeffs(struct therm_estimator *est, int active_coeffs)
{
struct therm_est_subdevice *subdevice = est->subdevice;
if (active_coeffs < 0 || active_coeffs >= subdevice->num_coeffs)
return -EINVAL;
subdevice->active_coeffs = active_coeffs;
return 0;
}
static ssize_t show_coeff(struct device *dev,
struct device_attribute *da,
char *buf)
{
struct therm_estimator *est = dev_get_drvdata(dev);
struct therm_est_subdevice *subdevice = est->subdevice;
struct therm_est_coeffs *coeffs_set;
s32 *coeffs;
ssize_t total_len = 0;
int i, j, k;
total_len += snprintf(buf + total_len, PAGE_SIZE - total_len,
"Total %02d set(s) are available\n\n",
subdevice->num_coeffs);
for (i = 0; i < subdevice->num_coeffs; i++) {
total_len += snprintf(buf + total_len, PAGE_SIZE - total_len,
"- SET %02d - %s\n", i,
(i == subdevice->active_coeffs)?"active":"");
coeffs_set = subdevice->coeffs_set + i;
for (j = 0; j < subdevice->num_devs; j++) {
total_len += snprintf(buf + total_len,
PAGE_SIZE - total_len,
"%s : ",
subdevice->sub_thz[j].thz->type);
coeffs = coeffs_set->coeffs[j];
for (k = 0; k < HIST_LEN; k++) {
total_len += snprintf(buf + total_len,
PAGE_SIZE - total_len, " %d",
coeffs[k]);
}
total_len += snprintf(buf + total_len,
PAGE_SIZE - total_len, "\n");
}
total_len += snprintf(buf + total_len,
PAGE_SIZE - total_len,
"toffset : %ld\n\n",
coeffs_set->toffset);
}
return strlen(buf);
}
static ssize_t set_coeff(struct device *dev,
struct device_attribute *da,
const char *buf, size_t count)
{
struct therm_estimator *est = dev_get_drvdata(dev);
int coeffs_index, dev_index, scount;
s32 coeff[20];
if (HIST_LEN > 20)
return -EINVAL;
scount = sscanf(buf, "[%d][%d] %d %d %d %d %d %d %d %d %d " \
"%d %d %d %d %d %d %d %d %d %d %d",
&coeffs_index, &dev_index,
&coeff[0],
&coeff[1],
&coeff[2],
&coeff[3],
&coeff[4],
&coeff[5],
&coeff[6],
&coeff[7],
&coeff[8],
&coeff[9],
&coeff[10],
&coeff[11],
&coeff[12],
&coeff[13],
&coeff[14],
&coeff[15],
&coeff[16],
&coeff[17],
&coeff[18],
&coeff[19]);
if (scount != HIST_LEN + 2)
return -1;
if (dev_index < 0 || dev_index >= est->subdevice->num_devs)
return -EINVAL;
if (coeffs_index < 0 || coeffs_index >= est->subdevice->num_coeffs)
return -EINVAL;
/* This has obvious locking issues but don't worry about it */
memcpy(est->subdevice->coeffs_set[coeffs_index].coeffs[dev_index],
coeff,
sizeof(coeff[0]) * HIST_LEN);
return count;
}
static ssize_t show_offset(struct device *dev,
struct device_attribute *da,
char *buf)
{
return show_coeff(dev, da, buf);
}
static ssize_t set_offset(struct device *dev,
struct device_attribute *da,
const char *buf, size_t count)
{
struct therm_estimator *est = dev_get_drvdata(dev);
int offset, scount, coeffs_index;
scount = sscanf(buf, "[%d] %d", &coeffs_index, &offset);
if (scount != 2)
return -EINVAL;
if (coeffs_index < 0 || coeffs_index >= est->subdevice->num_coeffs)
return -EINVAL;
est->subdevice->coeffs_set[coeffs_index].toffset = offset;
return count;
}
static ssize_t show_active_coeffs(struct device *dev,
struct device_attribute *da,
char *buf)
{
struct therm_estimator *est = dev_get_drvdata(dev);
struct therm_est_subdevice *subdevice = est->subdevice;
struct therm_est_coeffs *coeffs_set;
s32 *coeffs;
ssize_t total_len = 0;
int i, j;
total_len += snprintf(buf + total_len, PAGE_SIZE - total_len,
"There are %d set(s) and the active set is [%02d]\n\n",
subdevice->num_coeffs, subdevice->active_coeffs);
coeffs_set = subdevice->coeffs_set + subdevice->active_coeffs;
for (i = 0; i < subdevice->num_devs; i++) {
total_len += snprintf(buf + total_len, PAGE_SIZE - total_len,
"%s : ", subdevice->sub_thz[i].thz->type);
coeffs = coeffs_set->coeffs[i];
for (j = 0; j < HIST_LEN; j++) {
total_len += snprintf(buf + total_len,
PAGE_SIZE - total_len, " %d",
coeffs[j]);
}
total_len += snprintf(buf + total_len,
PAGE_SIZE - total_len, "\n");
}
total_len += snprintf(buf + total_len, PAGE_SIZE - total_len,
"toffset : %ld\n",
coeffs_set->toffset);
return strlen(buf);
}
static ssize_t set_active_coeffs(struct device *dev,
struct device_attribute *da,
const char *buf, size_t count)
{
struct therm_estimator *est = dev_get_drvdata(dev);
int active_coeffs, ret;
if (kstrtoint(buf, 0, &active_coeffs))
return -EINVAL;
ret = switch_active_coeffs(est, active_coeffs);
if (ret)
return ret;
return count;
}
static ssize_t show_temps(struct device *dev,
struct device_attribute *da,
char *buf)
{
struct therm_estimator *est = dev_get_drvdata(dev);
struct therm_est_subdevice *subdevice = est->subdevice;
long *hist;
ssize_t total_len = 0;
int i, j;
int index;
/* This has obvious locking issues but don't worry about it */
for (i = 0; i < subdevice->num_devs; i++) {
total_len += snprintf(buf + total_len,
PAGE_SIZE - total_len, "[%d]", i);
hist = subdevice->sub_thz[i].hist;
for (j = 0; j < HIST_LEN; j++) {
index = (subdevice->ntemp - j + HIST_LEN) % HIST_LEN;
total_len += snprintf(buf + total_len,
PAGE_SIZE - total_len, " %ld",
hist[index]);
}
total_len += snprintf(buf + total_len,
PAGE_SIZE - total_len, "\n");
}
return strlen(buf);
}
static ssize_t show_tc1(struct device *dev,
struct device_attribute *da,
char *buf)
{
struct therm_estimator *est = dev_get_drvdata(dev);
snprintf(buf, PAGE_SIZE, "%d\n", est->tc1);
return strlen(buf);
}
static ssize_t set_tc1(struct device *dev,
struct device_attribute *da,
const char *buf, size_t count)
{
struct therm_estimator *est = dev_get_drvdata(dev);
int tc1;
if (kstrtoint(buf, 0, &tc1))
return -EINVAL;
est->tc1 = tc1;
return count;
}
static ssize_t show_tc2(struct device *dev,
struct device_attribute *da,
char *buf)
{
struct therm_estimator *est = dev_get_drvdata(dev);
snprintf(buf, PAGE_SIZE, "%d\n", est->tc2);
return strlen(buf);
}
static ssize_t set_tc2(struct device *dev,
struct device_attribute *da,
const char *buf, size_t count)
{
struct therm_estimator *est = dev_get_drvdata(dev);
int tc2;
if (kstrtoint(buf, 0, &tc2))
return -EINVAL;
est->tc2 = tc2;
return count;
}
static struct sensor_device_attribute therm_est_nodes[] = {
SENSOR_ATTR(coeff, S_IRUGO | S_IWUSR, show_coeff, set_coeff, 0),
SENSOR_ATTR(offset, S_IRUGO | S_IWUSR, show_offset, set_offset, 0),
SENSOR_ATTR(active_coeffs, S_IRUGO | S_IWUSR, show_active_coeffs,
set_active_coeffs, 0),
SENSOR_ATTR(tc1, S_IRUGO | S_IWUSR, show_tc1, set_tc1, 0),
SENSOR_ATTR(tc2, S_IRUGO | S_IWUSR, show_tc2, set_tc2, 0),
SENSOR_ATTR(temps, S_IRUGO, show_temps, 0, 0),
};
#ifdef CONFIG_PM
static int therm_est_pm_notify(struct notifier_block *nb,
unsigned long event, void *data)
{
struct therm_estimator *est = container_of(
nb,
struct therm_estimator,
pm_nb);
switch (event) {
case PM_SUSPEND_PREPARE:
cancel_delayed_work_sync(&est->therm_est_work);
break;
case PM_POST_SUSPEND:
est->low_limit = 0;
est->high_limit = 0;
therm_est_init_history(est);
queue_delayed_work(est->workqueue,
&est->therm_est_work,
msecs_to_jiffies(est->polling_period));
break;
}
return NOTIFY_OK;
}
#endif
static int
thermal_est_activation_get_max_state(struct thermal_cooling_device *cdev,
unsigned long *max_state)
{
*max_state = 1;
return 0;
}
static int
thermal_est_activation_get_cur_state(struct thermal_cooling_device *cdev,
unsigned long *cur_state)
{
struct therm_estimator *est = cdev->devdata;
*cur_state = est->polling_enabled;
return 0;
}
static int
thermal_est_activation_set_cur_state(struct thermal_cooling_device *cdev,
unsigned long cur_state)
{
struct therm_estimator *est = cdev->devdata;
if (est->use_activator)
therm_est_polling(est, cur_state > 0);
return 0;
}
static struct thermal_cooling_device_ops thermal_est_activation_device_ops = {
.get_max_state = thermal_est_activation_get_max_state,
.get_cur_state = thermal_est_activation_get_cur_state,
.set_cur_state = thermal_est_activation_set_cur_state,
};
struct thermal_cooling_device *thermal_est_activation_device_register(
struct therm_estimator *est,
char *type)
{
struct thermal_cooling_device *cdev;
cdev = thermal_cooling_device_register(
type,
est,
&thermal_est_activation_device_ops);
if (IS_ERR(cdev))
return NULL;
pr_debug("Therm_est: Cooling-device REGISTERED\n");
return cdev;
}
static int therm_est_get_subdev(struct device *dev,
struct device_node *subdev_np,
struct therm_est_subdevice *subdevice)
{
struct thermal_zone_device *thz;
struct device_node *coeffs_np;
char *thz_name;
int num_subdevs;
int num_coeffs;
int i, ret = 0;
s32 val;
num_subdevs = of_property_count_strings(subdev_np, "subdev_names");
if (num_subdevs == 0)
return -ENOENT;
num_coeffs = of_get_child_count(subdev_np);
if (num_coeffs == 0)
return -ENOENT;
subdevice->sub_thz = devm_kzalloc(dev,
sizeof(*subdevice->sub_thz) * num_subdevs, GFP_KERNEL);
if (!subdevice->sub_thz)
return -ENOMEM;
subdevice->coeffs_set = devm_kzalloc(dev,
sizeof(*subdevice->coeffs_set) * num_coeffs,
GFP_KERNEL);
if (!subdevice->coeffs_set)
return -ENOMEM;
for (i = 0; i < num_coeffs; i++) {
subdevice->coeffs_set[i].coeffs = devm_kzalloc(dev,
sizeof(*subdevice->coeffs_set->coeffs) * num_subdevs,
GFP_KERNEL);
if (!subdevice->coeffs_set[i].coeffs)
return -ENOMEM;
}
for (i = 0; i < num_subdevs; i++) {
ret = of_property_read_string_index(subdev_np,
"subdev_names",
i, (const char**)&thz_name);
if (ret)
return -EINVAL;
thz = thermal_zone_device_find(thz_name,
therm_est_subdev_match);
if (!thz)
return -EINVAL;
subdevice->sub_thz[i].thz = thz;
}
i = 0;
for_each_child_of_node(subdev_np, coeffs_np) {
ret = of_property_read_s32(coeffs_np, "toffset", &val);
if (ret)
return ret;
subdevice->coeffs_set[i].toffset = val;
/*
* casting from s32* to u32* is safe here
*/
ret = of_property_read_u32_array(coeffs_np, "coeffs",
(u32 *)subdevice->coeffs_set[i].coeffs,
num_subdevs * HIST_LEN);
i++;
}
of_node_put(coeffs_np);
subdevice->num_coeffs = num_coeffs;
subdevice->num_devs = num_subdevs;
return 0;
}
static struct therm_est_data *therm_est_get_pdata(struct device *dev)
{
struct device_node *np, *subdev_np;
struct therm_est_data *data;
u32 val;
int ret;
np = dev->of_node;
if (!np)
return ERR_PTR(-ENOENT);
data = devm_kzalloc(dev, sizeof(struct therm_est_data), GFP_KERNEL);
if (!data)
return ERR_PTR(-ENOMEM);
ret = of_property_read_u32(np, "polling-period", &val);
if (ret < 0)
return ERR_PTR(ret);
data->polling_period = val;
ret = of_property_read_u32(np, "tc1", &val);
if (ret < 0)
return ERR_PTR(ret);
data->tc1 = val;
ret = of_property_read_u32(np, "tc2", &val);
if (ret < 0)
return ERR_PTR(ret);
data->tc2 = val;
ret = of_property_read_u32(np, "use_activator", &val);
if (ret < 0)
return ERR_PTR(ret);
data->use_activator = val;
subdev_np = of_get_child_by_name(np, "subdev");
if (!subdev_np)
return ERR_PTR(-ENOENT);
ret = therm_est_get_subdev(dev, subdev_np, &data->subdevice);
if (ret)
return ERR_PTR(ret);
of_node_put(subdev_np);
return data;
}
static struct thermal_zone_of_device_ops sops = {
.get_temp = therm_est_get_temp,
.get_trend = therm_est_get_trend,
.trip_update = therm_est_trip_update,
};
static int therm_est_probe(struct platform_device *pdev)
{
int i, ret;
struct therm_estimator *est;
struct therm_est_data *data;
est = kzalloc(sizeof(struct therm_estimator), GFP_KERNEL);
if (IS_ERR_OR_NULL(est))
return -ENOMEM;
platform_set_drvdata(pdev, est);
data = pdev->dev.platform_data;
if (!data) {
data = therm_est_get_pdata(&pdev->dev);
if (IS_ERR_OR_NULL(data))
goto err;
}
est->subdevice = &data->subdevice;
est->polling_period = data->polling_period;
est->tc1 = data->tc1;
est->tc2 = data->tc2;
est->cur_temp = DEFAULT_TSKIN;
est->use_activator = data->use_activator;
/* initialize history */
therm_est_init_history(est);
est->workqueue = alloc_workqueue(dev_name(&pdev->dev),
WQ_HIGHPRI | WQ_UNBOUND, 1);
if (!est->workqueue)
goto err;
INIT_DELAYED_WORK(&est->therm_est_work, therm_est_work_func);
est->cdev = thermal_est_activation_device_register(est,
"therm_est_activ");
est->thz = thermal_zone_of_sensor_register(&pdev->dev, 0, est, &sops);
if (IS_ERR(est->thz)) {
ret = PTR_ERR(est->thz);
dev_err(&pdev->dev,
"Device can not register as thermal sensor: %d\n", ret);
goto err;
}
est->tripped_info = devm_kzalloc(&pdev->dev,
sizeof(bool) * est->thz->trips,
GFP_KERNEL);
if (IS_ERR_OR_NULL(est->thz))
goto err;
for (i = 0; i < ARRAY_SIZE(therm_est_nodes); i++)
device_create_file(&pdev->dev, &therm_est_nodes[i].dev_attr);
#ifdef CONFIG_PM
est->pm_nb.notifier_call = therm_est_pm_notify,
register_pm_notifier(&est->pm_nb);
#endif
if (!est->use_activator)
queue_delayed_work(est->workqueue, &est->therm_est_work,
msecs_to_jiffies(est->polling_period));
return 0;
err:
if (est->workqueue)
destroy_workqueue(est->workqueue);
kfree(est);
return -EINVAL;
}
static int therm_est_remove(struct platform_device *pdev)
{
struct therm_estimator *est = platform_get_drvdata(pdev);
int i;
cancel_delayed_work_sync(&est->therm_est_work);
#ifdef CONFIG_PM
unregister_pm_notifier(&est->pm_nb);
#endif
for (i = 0; i < ARRAY_SIZE(therm_est_nodes); i++)
device_remove_file(&pdev->dev, &therm_est_nodes[i].dev_attr);
thermal_cooling_device_unregister(est->cdev);
kfree(est->thz);
destroy_workqueue(est->workqueue);
kfree(est);
return 0;
}
static void therm_est_shutdown(struct platform_device *pdev)
{
struct therm_estimator *est = platform_get_drvdata(pdev);
cancel_delayed_work_sync(&est->therm_est_work);
thermal_cooling_device_unregister(est->cdev);
}
static struct of_device_id therm_est_of_match[] = {
{ .compatible = "nvidia,therm-est", },
{ },
};
static struct platform_driver therm_est_driver = {
.driver = {
.owner = THIS_MODULE,
.name = "therm_est",
.of_match_table = of_match_ptr(therm_est_of_match),
},
.probe = therm_est_probe,
.remove = therm_est_remove,
.shutdown = therm_est_shutdown,
};
static int __init therm_est_driver_init(void)
{
return platform_driver_register(&therm_est_driver);
}
device_initcall_sync(therm_est_driver_init);
|