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