thermal: tegra: therm_est: handle trip_update
[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         int ntemp;
48         long toffset;
49         long polling_period;
50         int polling_enabled;
51         int tc1;
52         int tc2;
53         int ndevs;
54         struct therm_est_subdevice *devs;
55         bool *tripped_info;
56
57         int use_activator;
58 #ifdef CONFIG_PM
59         struct notifier_block pm_nb;
60 #endif
61 };
62
63 static int therm_est_subdev_match(struct thermal_zone_device *thz, void *data)
64 {
65         return strcmp((char *)data, thz->type) == 0;
66 }
67
68 static int therm_est_subdev_get_temp(struct thermal_zone_device *thz,
69                                         long *temp)
70 {
71         if (!thz || !thz->ops->get_temp || thz->ops->get_temp(thz, temp))
72                 *temp = 25000;
73
74         return 0;
75 }
76
77 static void therm_est_update_limits(struct therm_estimator *est)
78 {
79         const int MAX_HIGH_TEMP = 128000;
80         long low_temp = 0, high_temp = MAX_HIGH_TEMP;
81         long trip_temp, passive_low_temp = MAX_HIGH_TEMP;
82         enum thermal_trip_type trip_type;
83         long hysteresis, zone_temp;
84         int i;
85
86         zone_temp = est->thz->temperature;
87         for (i = 0; i < est->thz->trips; i++) {
88                 est->thz->ops->get_trip_temp(est->thz, i, &trip_temp);
89                 est->thz->ops->get_trip_hyst(est->thz, i, &hysteresis);
90                 est->thz->ops->get_trip_type(est->thz, i, &trip_type);
91
92                 if (zone_temp >= trip_temp) {
93                         trip_temp -= hysteresis;
94                         est->tripped_info[i] = true;
95                 } else if (est->tripped_info[i]) {
96                         trip_temp -= hysteresis;
97                         if (zone_temp < trip_temp)
98                                 est->tripped_info[i] = false;
99                 }
100
101                 if (!est->tripped_info[i]) { /* not tripped? update high */
102                         if (trip_temp < high_temp)
103                                 high_temp = trip_temp;
104                 } else { /* tripped? update low */
105                         if (trip_type != THERMAL_TRIP_PASSIVE) {
106                                 /* get highest ACTIVE */
107                                 if (trip_temp > low_temp)
108                                         low_temp = trip_temp;
109                         } else {
110                                 /* get lowest PASSIVE */
111                                 if (trip_temp < passive_low_temp)
112                                         passive_low_temp = trip_temp;
113                         }
114                 }
115         }
116
117         if (passive_low_temp != MAX_HIGH_TEMP)
118                 low_temp = max(low_temp, passive_low_temp);
119
120         est->low_limit = low_temp;
121         est->high_limit = high_temp;
122 }
123
124 static void therm_est_work_func(struct work_struct *work)
125 {
126         int i, j, index, sum = 0;
127         long temp;
128         struct delayed_work *dwork = container_of(work,
129                                         struct delayed_work, work);
130         struct therm_estimator *est = container_of(dwork,
131                                         struct therm_estimator,
132                                         therm_est_work);
133
134         for (i = 0; i < est->ndevs; i++) {
135                 if (therm_est_subdev_get_temp(est->devs[i].sub_thz, &temp))
136                         continue;
137                 est->devs[i].hist[(est->ntemp % HIST_LEN)] = temp;
138         }
139
140         for (i = 0; i < est->ndevs; i++) {
141                 for (j = 0; j < HIST_LEN; j++) {
142                         index = (est->ntemp - j + HIST_LEN) % HIST_LEN;
143                         sum += est->devs[i].hist[index] *
144                                 est->devs[i].coeffs[j];
145                 }
146         }
147
148         est->cur_temp = sum / 100 + est->toffset;
149         est->ntemp++;
150
151         if (est->thz && ((est->cur_temp < est->low_limit) ||
152                         (est->cur_temp >= est->high_limit))) {
153                 thermal_zone_device_update(est->thz);
154                 therm_est_update_limits(est);
155         }
156
157         if (est->polling_enabled > 0 || !est->use_activator) {
158                 queue_delayed_work(est->workqueue, &est->therm_est_work,
159                         msecs_to_jiffies(est->polling_period));
160         }
161 }
162
163 static int therm_est_get_temp(void *of_data, long *temp)
164 {
165         struct therm_estimator *est = (struct therm_estimator *)of_data;
166
167         *temp = est->cur_temp;
168         return 0;
169 }
170
171 static int therm_est_get_trend(void *of_data, long *trend)
172 {
173         struct therm_estimator *est = (struct therm_estimator *)of_data;
174
175         if (est->thz->temperature > est->thz->last_temperature + 100)
176                 *trend = THERMAL_TREND_RAISING;
177         else if (est->thz->temperature < est->thz->last_temperature - 100)
178                 *trend = THERMAL_TREND_DROPPING;
179         else
180                 *trend = THERMAL_TREND_STABLE;
181
182         return 0;
183 }
184
185 static int therm_est_trip_update(void *of_data, int trip)
186 {
187         struct therm_estimator *est = (struct therm_estimator *)of_data;
188
189         thermal_zone_device_update(est->thz);
190         therm_est_update_limits(est);
191
192         return 0;
193 }
194
195 static int therm_est_init_history(struct therm_estimator *est)
196 {
197         int i, j;
198         struct therm_est_subdevice *dev;
199         long temp;
200
201         for (i = 0; i < est->ndevs; i++) {
202                 dev = &est->devs[i];
203
204                 if (therm_est_subdev_get_temp(dev->sub_thz, &temp))
205                         return -EINVAL;
206
207                 for (j = 0; j < HIST_LEN; j++)
208                         dev->hist[j] = temp;
209         }
210
211         return 0;
212 }
213
214 static int therm_est_polling(struct therm_estimator *est,
215                                 int polling)
216 {
217         est->polling_enabled = polling > 0;
218
219         if (est->polling_enabled > 0) {
220                 est->low_limit = 0;
221                 est->high_limit = 0;
222                 therm_est_init_history(est);
223                 queue_delayed_work(est->workqueue,
224                         &est->therm_est_work,
225                         msecs_to_jiffies(est->polling_period));
226         } else {
227                 cancel_delayed_work_sync(&est->therm_est_work);
228                 est->cur_temp = DEFAULT_TSKIN;
229         }
230         return 0;
231 }
232
233 static ssize_t show_coeff(struct device *dev,
234                                 struct device_attribute *da,
235                                 char *buf)
236 {
237         struct therm_estimator *est = dev_get_drvdata(dev);
238         ssize_t len, total_len = 0;
239         int i, j;
240         for (i = 0; i < est->ndevs; i++) {
241                 len = snprintf(buf + total_len,
242                                 PAGE_SIZE - total_len, "[%d]", i);
243                 total_len += len;
244                 for (j = 0; j < HIST_LEN; j++) {
245                         len = snprintf(buf + total_len,
246                                         PAGE_SIZE - total_len, " %ld",
247                                         est->devs[i].coeffs[j]);
248                         total_len += len;
249                 }
250                 len = snprintf(buf + total_len, PAGE_SIZE - total_len, "\n");
251                 total_len += len;
252         }
253         return strlen(buf);
254 }
255
256 static ssize_t set_coeff(struct device *dev,
257                                 struct device_attribute *da,
258                                 const char *buf, size_t count)
259 {
260         struct therm_estimator *est = dev_get_drvdata(dev);
261         int devid, scount;
262         long coeff[20];
263
264         if (HIST_LEN > 20)
265                 return -EINVAL;
266
267         scount = sscanf(buf, "[%d] %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld " \
268                         "%ld %ld %ld %ld %ld %ld %ld %ld %ld %ld",
269                         &devid,
270                         &coeff[0],
271                         &coeff[1],
272                         &coeff[2],
273                         &coeff[3],
274                         &coeff[4],
275                         &coeff[5],
276                         &coeff[6],
277                         &coeff[7],
278                         &coeff[8],
279                         &coeff[9],
280                         &coeff[10],
281                         &coeff[11],
282                         &coeff[12],
283                         &coeff[13],
284                         &coeff[14],
285                         &coeff[15],
286                         &coeff[16],
287                         &coeff[17],
288                         &coeff[18],
289                         &coeff[19]);
290
291         if (scount != HIST_LEN + 1)
292                 return -1;
293
294         if (devid < 0 || devid >= est->ndevs)
295                 return -EINVAL;
296
297         /* This has obvious locking issues but don't worry about it */
298         memcpy(est->devs[devid].coeffs, coeff, sizeof(coeff[0]) * HIST_LEN);
299
300         return count;
301 }
302
303 static ssize_t show_offset(struct device *dev,
304                                 struct device_attribute *da,
305                                 char *buf)
306 {
307         struct therm_estimator *est = dev_get_drvdata(dev);
308         snprintf(buf, PAGE_SIZE, "%ld\n", est->toffset);
309         return strlen(buf);
310 }
311
312 static ssize_t set_offset(struct device *dev,
313                                 struct device_attribute *da,
314                                 const char *buf, size_t count)
315 {
316         struct therm_estimator *est = dev_get_drvdata(dev);
317         int offset;
318
319         if (kstrtoint(buf, 0, &offset))
320                 return -EINVAL;
321
322         est->toffset = offset;
323
324         return count;
325 }
326
327 static ssize_t show_temps(struct device *dev,
328                                 struct device_attribute *da,
329                                 char *buf)
330 {
331         struct therm_estimator *est = dev_get_drvdata(dev);
332         ssize_t total_len = 0;
333         int i, j;
334         int index;
335
336         /* This has obvious locking issues but don't worry about it */
337         for (i = 0; i < est->ndevs; i++) {
338                 total_len += snprintf(buf + total_len,
339                                         PAGE_SIZE - total_len, "[%d]", i);
340                 for (j = 0; j < HIST_LEN; j++) {
341                         index = (est->ntemp - j + HIST_LEN) % HIST_LEN;
342                         total_len += snprintf(buf + total_len,
343                                                 PAGE_SIZE - total_len, " %ld",
344                                                 est->devs[i].hist[index]);
345                 }
346                 total_len += snprintf(buf + total_len,
347                                         PAGE_SIZE - total_len, "\n");
348         }
349         return strlen(buf);
350 }
351
352 static ssize_t show_tc1(struct device *dev,
353                         struct device_attribute *da,
354                         char *buf)
355 {
356         struct therm_estimator *est = dev_get_drvdata(dev);
357         snprintf(buf, PAGE_SIZE, "%d\n", est->tc1);
358         return strlen(buf);
359 }
360
361 static ssize_t set_tc1(struct device *dev,
362                         struct device_attribute *da,
363                         const char *buf, size_t count)
364 {
365         struct therm_estimator *est = dev_get_drvdata(dev);
366         int tc1;
367
368         if (kstrtoint(buf, 0, &tc1))
369                 return -EINVAL;
370
371         est->tc1 = tc1;
372
373         return count;
374 }
375
376 static ssize_t show_tc2(struct device *dev,
377                         struct device_attribute *da,
378                         char *buf)
379 {
380         struct therm_estimator *est = dev_get_drvdata(dev);
381         snprintf(buf, PAGE_SIZE, "%d\n", est->tc2);
382         return strlen(buf);
383 }
384
385 static ssize_t set_tc2(struct device *dev,
386                         struct device_attribute *da,
387                         const char *buf, size_t count)
388 {
389         struct therm_estimator *est = dev_get_drvdata(dev);
390         int tc2;
391
392         if (kstrtoint(buf, 0, &tc2))
393                 return -EINVAL;
394
395         est->tc2 = tc2;
396
397         return count;
398 }
399
400 static struct sensor_device_attribute therm_est_nodes[] = {
401         SENSOR_ATTR(coeff, S_IRUGO | S_IWUSR, show_coeff, set_coeff, 0),
402         SENSOR_ATTR(offset, S_IRUGO | S_IWUSR, show_offset, set_offset, 0),
403         SENSOR_ATTR(tc1, S_IRUGO | S_IWUSR, show_tc1, set_tc1, 0),
404         SENSOR_ATTR(tc2, S_IRUGO | S_IWUSR, show_tc2, set_tc2, 0),
405         SENSOR_ATTR(temps, S_IRUGO, show_temps, 0, 0),
406 };
407
408 #ifdef CONFIG_PM
409 static int therm_est_pm_notify(struct notifier_block *nb,
410                                 unsigned long event, void *data)
411 {
412         struct therm_estimator *est = container_of(
413                                         nb,
414                                         struct therm_estimator,
415                                         pm_nb);
416
417         switch (event) {
418         case PM_SUSPEND_PREPARE:
419                 cancel_delayed_work_sync(&est->therm_est_work);
420                 break;
421         case PM_POST_SUSPEND:
422                 est->low_limit = 0;
423                 est->high_limit = 0;
424                 therm_est_init_history(est);
425                 queue_delayed_work(est->workqueue,
426                                 &est->therm_est_work,
427                                 msecs_to_jiffies(est->polling_period));
428                 break;
429         }
430
431         return NOTIFY_OK;
432 }
433 #endif
434
435 static int
436 thermal_est_activation_get_max_state(struct thermal_cooling_device *cdev,
437                                         unsigned long *max_state)
438 {
439         *max_state = 1;
440         return 0;
441 }
442
443 static int
444 thermal_est_activation_get_cur_state(struct thermal_cooling_device *cdev,
445                                         unsigned long *cur_state)
446 {
447         struct therm_estimator *est = cdev->devdata;
448         *cur_state = est->polling_enabled;
449         return 0;
450 }
451
452 static int
453 thermal_est_activation_set_cur_state(struct thermal_cooling_device *cdev,
454                                         unsigned long cur_state)
455 {
456         struct therm_estimator *est = cdev->devdata;
457         if (est->use_activator)
458                 therm_est_polling(est, cur_state > 0);
459
460         return 0;
461 }
462
463 static struct thermal_cooling_device_ops thermal_est_activation_device_ops = {
464         .get_max_state = thermal_est_activation_get_max_state,
465         .get_cur_state = thermal_est_activation_get_cur_state,
466         .set_cur_state = thermal_est_activation_set_cur_state,
467 };
468
469 struct thermal_cooling_device *thermal_est_activation_device_register(
470                                                 struct therm_estimator *est,
471                                                 char *type)
472 {
473         struct thermal_cooling_device *cdev;
474
475         cdev = thermal_cooling_device_register(
476                 type,
477                 est,
478                 &thermal_est_activation_device_ops);
479
480         if (IS_ERR(cdev))
481                 return NULL;
482
483         pr_debug("Therm_est: Cooling-device REGISTERED\n");
484
485         return cdev;
486 }
487
488 static int __parse_dt_subdev(struct device_node *np,
489                              struct therm_est_subdevice *subdev)
490 {
491         const char *str;
492         char *sbegin;
493         int i = 0;
494         int ret;
495
496         subdev->dev_data = (void *)of_get_property(np, "dev-data", NULL);
497         if (!subdev->dev_data)
498                 return -ENODATA;
499
500         ret = of_property_read_string(np, "coeffs", &str);
501         if (ret < 0)
502                 return ret;
503
504         while (str && (i < HIST_LEN)) {
505                 str = skip_spaces(str);
506                 sbegin = strsep((char **)&str, " ");
507                 if (!sbegin || (kstrtol((const char *)sbegin, 10,
508                                 &subdev->coeffs[i++]) < 0))
509                         break;
510         }
511         if (i != HIST_LEN)
512                 return -EINVAL;
513
514         return 0;
515 }
516
517 static struct therm_est_data *therm_est_get_pdata(struct device *dev)
518 {
519         struct therm_est_data *data;
520         struct device_node *np;
521         struct device_node *ch;
522         u32 val;
523         int num_subdev;
524         int ret;
525
526         np = dev->of_node;
527         if (!np)
528                 return dev->platform_data;
529
530         data = devm_kzalloc(dev, sizeof(struct therm_est_data), GFP_KERNEL);
531         if (!data)
532                 return ERR_PTR(-ENOMEM);
533
534         ret = of_property_read_u32(np, "toffset", &val);
535         if (ret < 0)
536                 return ERR_PTR(ret);
537         data->toffset = val;
538
539         ret = of_property_read_u32(np, "polling-period", &val);
540         if (ret < 0)
541                 return ERR_PTR(ret);
542         data->polling_period = val;
543
544         ret = of_property_read_u32(np, "tc1", &val);
545         if (ret < 0)
546                 return ERR_PTR(ret);
547         data->tc1 = val;
548
549         ret = of_property_read_u32(np, "tc2", &val);
550         if (ret < 0)
551                 return ERR_PTR(ret);
552         data->tc2 = val;
553
554         ret = of_property_read_u32(np, "use_activator", &val);
555         if (ret < 0)
556                 return ERR_PTR(ret);
557         data->use_activator = val;
558
559         num_subdev = 0;
560         ch = np;
561         while ((ch = of_find_node_by_type(ch, "therm-est-subdev")))
562                 num_subdev++;
563
564         /* subdevices are must required data. */
565         if (num_subdev == 0)
566                 return ERR_PTR(-ENOENT);
567
568         data->devs = devm_kzalloc(dev,
569                         sizeof(struct therm_est_subdevice) * num_subdev,
570                         GFP_KERNEL);
571         if (!data->devs)
572                 return ERR_PTR(-ENOMEM);
573
574         num_subdev = 0;
575         ch = np;
576         while ((ch = of_find_node_by_type(ch, "therm-est-subdev"))) {
577                 ret = __parse_dt_subdev(ch, &data->devs[num_subdev++]);
578                 if (ret < 0)
579                         return ERR_PTR(ret);
580                 }
581
582         data->ndevs = num_subdev;
583
584         return data;
585 }
586
587 static int therm_est_probe(struct platform_device *pdev)
588 {
589         int i, ret;
590         struct therm_estimator *est;
591         struct therm_est_data *data;
592         struct thermal_zone_device *thz;
593         struct thermal_of_sensor_ops sops = {
594                 .get_temp = therm_est_get_temp,
595                 .get_trend = therm_est_get_trend,
596                 .trip_update = therm_est_trip_update,
597         };
598
599         est = kzalloc(sizeof(struct therm_estimator), GFP_KERNEL);
600         if (IS_ERR_OR_NULL(est))
601                 return -ENOMEM;
602
603         platform_set_drvdata(pdev, est);
604
605         data = pdev->dev.platform_data;
606         if (!data)
607                 data = therm_est_get_pdata(&pdev->dev);
608
609         for (i = 0; i < data->ndevs; i++) {
610                 thz = thermal_zone_device_find(data->devs[i].dev_data,
611                                                         therm_est_subdev_match);
612                 if (!thz)
613                         goto err;
614                 data->devs[i].sub_thz = thz;
615         }
616
617         est->devs = data->devs;
618         est->ndevs = data->ndevs;
619         est->toffset = data->toffset;
620         est->polling_period = data->polling_period;
621         est->polling_enabled = 0; /* By default polling is switched off */
622         est->tc1 = data->tc1;
623         est->tc2 = data->tc2;
624         est->cur_temp = DEFAULT_TSKIN;
625         est->use_activator = data->use_activator;
626
627         /* initialize history */
628         therm_est_init_history(est);
629
630         est->workqueue = alloc_workqueue(dev_name(&pdev->dev),
631                                     WQ_HIGHPRI | WQ_UNBOUND, 1);
632         if (!est->workqueue)
633                 goto err;
634
635         INIT_DELAYED_WORK(&est->therm_est_work, therm_est_work_func);
636
637         est->cdev = thermal_est_activation_device_register(est,
638                                                         "therm_est_activ");
639
640         est->thz = thermal_zone_of_sensor_register2(&pdev->dev, 0,
641                                         est, &sops);
642         if (IS_ERR(est->thz)) {
643                 ret = PTR_ERR(est->thz);
644                 dev_err(&pdev->dev,
645                         "Device can not register as thermal sensor: %d\n", ret);
646                 goto err;
647         }
648
649         est->tripped_info = devm_kzalloc(&pdev->dev,
650                                 sizeof(bool) * est->thz->trips,
651                                 GFP_KERNEL);
652         if (IS_ERR_OR_NULL(est->thz))
653                 goto err;
654
655         for (i = 0; i < ARRAY_SIZE(therm_est_nodes); i++)
656                 device_create_file(&pdev->dev, &therm_est_nodes[i].dev_attr);
657
658 #ifdef CONFIG_PM
659         est->pm_nb.notifier_call = therm_est_pm_notify,
660         register_pm_notifier(&est->pm_nb);
661 #endif
662
663         if (!est->use_activator)
664                 queue_delayed_work(est->workqueue, &est->therm_est_work,
665                         msecs_to_jiffies(est->polling_period));
666
667         return 0;
668
669 err:
670         if (est->workqueue)
671                 destroy_workqueue(est->workqueue);
672         kfree(est);
673         return -EINVAL;
674 }
675
676 static int therm_est_remove(struct platform_device *pdev)
677 {
678         struct therm_estimator *est = platform_get_drvdata(pdev);
679         int i;
680
681         cancel_delayed_work_sync(&est->therm_est_work);
682
683 #ifdef CONFIG_PM
684         unregister_pm_notifier(&est->pm_nb);
685 #endif
686         for (i = 0; i < ARRAY_SIZE(therm_est_nodes); i++)
687                 device_remove_file(&pdev->dev, &therm_est_nodes[i].dev_attr);
688         thermal_cooling_device_unregister(est->cdev);
689         kfree(est->thz);
690         destroy_workqueue(est->workqueue);
691         kfree(est);
692         return 0;
693 }
694
695 static void therm_est_shutdown(struct platform_device *pdev)
696 {
697         struct therm_estimator *est = platform_get_drvdata(pdev);
698
699         cancel_delayed_work_sync(&est->therm_est_work);
700         thermal_cooling_device_unregister(est->cdev);
701 }
702
703 static struct of_device_id therm_est_of_match[] = {
704         { .compatible = "nvidia,therm-est", },
705         { },
706 };
707 static struct platform_driver therm_est_driver = {
708         .driver = {
709                 .owner = THIS_MODULE,
710                 .name  = "therm_est",
711                 .of_match_table = of_match_ptr(therm_est_of_match),
712         },
713         .probe  = therm_est_probe,
714         .remove = therm_est_remove,
715         .shutdown = therm_est_shutdown,
716 };
717
718 static int __init therm_est_driver_init(void)
719 {
720         return platform_driver_register(&therm_est_driver);
721 }
722 device_initcall_sync(therm_est_driver_init);