Thermal: Allow first cooling device update
[linux-2.6.git] / drivers / thermal / pwm_fan.c
1 /*
2  * pwm_fan.c fan driver that is controlled by pwm
3  *
4  * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
5  *
6  * Author: Anshul Jain <anshulj@nvidia.com>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms and conditions of the GNU General Public License,
10  * version 2, as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <linux/therm_est.h>
22 #include <linux/slab.h>
23 #include <linux/platform_data/pwm_fan.h>
24 #include <linux/thermal.h>
25 #include <linux/mutex.h>
26 #include <linux/debugfs.h>
27 #include <linux/platform_device.h>
28 #include <linux/delay.h>
29 #include <linux/module.h>
30 #include <linux/debugfs.h>
31 #include <linux/uaccess.h>
32 #include <linux/seq_file.h>
33 #include <linux/pwm.h>
34 #include <linux/device.h>
35 #include <linux/sysfs.h>
36
37 struct fan_dev_data {
38         int next_state;
39         int active_steps;
40         int *fan_rpm;
41         int *fan_pwm;
42         int *fan_rru;
43         int *fan_rrd;
44         struct workqueue_struct *workqueue;
45         int fan_temp_control_flag;
46         struct pwm_device *pwm_dev;
47         int fan_cap_pwm;
48         int fan_cur_pwm;
49         int next_target_pwm;
50         struct thermal_cooling_device *cdev;
51         struct delayed_work fan_ramp_work;
52         int step_time;
53         int precision_multiplier;
54         struct mutex fan_state_lock;
55         int pwm_period;
56         struct device *dev;
57 };
58
59 #ifdef CONFIG_DEBUG_FS
60 static struct dentry *fan_debugfs_root;
61
62 static int fan_target_pwm_show(void *data, u64 *val)
63 {
64         struct fan_dev_data *fan_data = (struct fan_dev_data *)data;
65
66         if (!fan_data)
67                 return -EINVAL;
68         mutex_lock(&fan_data->fan_state_lock);
69         *val = ((struct fan_dev_data *)data)->next_target_pwm /
70                                         fan_data->precision_multiplier;
71         mutex_unlock(&fan_data->fan_state_lock);
72         return 0;
73 }
74
75 static int fan_target_pwm_set(void *data, u64 val)
76 {
77         struct fan_dev_data *fan_data = (struct fan_dev_data *)data;
78
79         if (!fan_data)
80                 return -EINVAL;
81         if (val < 0)
82                 val = 0;
83         else if (val > fan_data->pwm_period)
84                 val = fan_data->pwm_period;
85
86         mutex_lock(&fan_data->fan_state_lock);
87         fan_data->next_target_pwm =
88                 min((int)(val * fan_data->precision_multiplier),
89                 fan_data->fan_cap_pwm);
90
91         if (fan_data->next_target_pwm != fan_data->fan_cur_pwm)
92                 queue_delayed_work(fan_data->workqueue,
93                                         &fan_data->fan_ramp_work,
94                                         msecs_to_jiffies(fan_data->step_time));
95         mutex_unlock(&fan_data->fan_state_lock);
96         return 0;
97 }
98
99 static int fan_temp_control_show(void *data, u64 *val)
100 {
101         struct fan_dev_data *fan_data = (struct fan_dev_data *)data;
102
103         if (!fan_data)
104                 return -EINVAL;
105         mutex_lock(&fan_data->fan_state_lock);
106         *val = fan_data->fan_temp_control_flag;
107         mutex_unlock(&fan_data->fan_state_lock);
108         return 0;
109 }
110
111 static int fan_temp_control_set(void *data, u64 val)
112 {
113         struct fan_dev_data *fan_data = (struct fan_dev_data *)data;
114
115         if (!fan_data)
116                 return -EINVAL;
117
118         mutex_lock(&fan_data->fan_state_lock);
119         fan_data->fan_temp_control_flag = val > 0 ? 1 : 0;
120         mutex_unlock(&fan_data->fan_state_lock);
121         return 0;
122 }
123
124 static int fan_cap_pwm_set(void *data, u64 val)
125 {
126         struct fan_dev_data *fan_data = (struct fan_dev_data *)data;
127
128         if (!fan_data)
129                 return -EINVAL;
130         if (val < 0)
131                 val = 0;
132         else if (val > fan_data->pwm_period)
133                 val = fan_data->pwm_period;
134         mutex_lock(&fan_data->fan_state_lock);
135         fan_data->fan_cap_pwm = val * fan_data->precision_multiplier;
136         fan_data->next_target_pwm = min(fan_data->fan_cap_pwm,
137                                         fan_data->next_target_pwm);
138         mutex_unlock(&fan_data->fan_state_lock);
139         return 0;
140 }
141
142 static int fan_cap_pwm_show(void *data, u64 *val)
143 {
144         struct fan_dev_data *fan_data = (struct fan_dev_data *)data;
145
146         if (!fan_data)
147                 return -EINVAL;
148         mutex_lock(&fan_data->fan_state_lock);
149         *val = fan_data->fan_cap_pwm / fan_data->precision_multiplier;
150         mutex_unlock(&fan_data->fan_state_lock);
151         return 0;
152 }
153
154 static int fan_step_time_set(void *data, u64 val)
155 {
156         struct fan_dev_data *fan_data = (struct fan_dev_data *)data;
157
158         if (!fan_data)
159                 return -EINVAL;
160         mutex_lock(&fan_data->fan_state_lock);
161         fan_data->step_time = val;
162         mutex_unlock(&fan_data->fan_state_lock);
163         return 0;
164 }
165
166 static int fan_cur_pwm_show(void *data, u64 *val)
167 {
168         struct fan_dev_data *fan_data = (struct fan_dev_data *)data;
169
170         if (!fan_data)
171                 return -EINVAL;
172         mutex_lock(&fan_data->fan_state_lock);
173         *val = (fan_data->fan_cur_pwm / fan_data->precision_multiplier);
174         mutex_unlock(&fan_data->fan_state_lock);
175         return 0;
176 }
177
178 static int fan_step_time_show(void *data, u64 *val)
179 {
180         struct fan_dev_data *fan_data = (struct fan_dev_data *)data;
181
182         if (!fan_data)
183                 return -EINVAL;
184         mutex_lock(&fan_data->fan_state_lock);
185         *val = fan_data->step_time;
186         mutex_unlock(&fan_data->fan_state_lock);
187         return 0;
188 }
189
190 static int fan_debugfs_show(struct seq_file *s, void *data)
191 {
192         int i;
193         struct fan_dev_data *fan_data = s->private;
194
195         if (!fan_data)
196                 return -EINVAL;
197         seq_printf(s, "(Index, RPM, PWM, RRU*1024, RRD*1024)\n");
198         for (i = 0; i < fan_data->active_steps; i++) {
199                 seq_printf(s, "(%d, %d, %d, %d, %d)\n", i, fan_data->fan_rpm[i],
200                         fan_data->fan_pwm[i]/fan_data->precision_multiplier,
201                         fan_data->fan_rru[i],
202                         fan_data->fan_rrd[i]);
203         }
204         return 0;
205 }
206
207 static int fan_debugfs_open(struct inode *inode, struct file *file)
208 {
209         return single_open(file, fan_debugfs_show, inode->i_private);
210 }
211
212 static const struct file_operations fan_rpm_table_fops = {
213         .open           = fan_debugfs_open,
214         .read           = seq_read,
215         .llseek         = seq_lseek,
216         .release        = single_release,
217 };
218
219 DEFINE_SIMPLE_ATTRIBUTE(fan_cap_pwm_fops,
220                         fan_cap_pwm_show,
221                         fan_cap_pwm_set, "%llu\n");
222 DEFINE_SIMPLE_ATTRIBUTE(fan_temp_control_fops,
223                         fan_temp_control_show,
224                         fan_temp_control_set, "%llu\n");
225 DEFINE_SIMPLE_ATTRIBUTE(fan_target_pwm_fops,
226                         fan_target_pwm_show,
227                         fan_target_pwm_set, "%llu\n");
228 DEFINE_SIMPLE_ATTRIBUTE(fan_cur_pwm_fops,
229                         fan_cur_pwm_show,
230                         NULL, "%llu\n");
231 DEFINE_SIMPLE_ATTRIBUTE(fan_step_time_fops,
232                         fan_step_time_show,
233                         fan_step_time_set, "%llu\n");
234
235 static int pwm_fan_debug_init(struct fan_dev_data *fan_data)
236 {
237         fan_debugfs_root = debugfs_create_dir("tegra_fan", 0);
238
239         if (!fan_debugfs_root)
240                 return -ENOMEM;
241
242         if (!debugfs_create_file("target_pwm", 0644, fan_debugfs_root,
243                 (void *)fan_data,
244                 &fan_target_pwm_fops))
245                 goto err_out;
246
247         if (!debugfs_create_file("temp_control", 0644, fan_debugfs_root,
248                 (void *)fan_data,
249                 &fan_temp_control_fops))
250                 goto err_out;
251
252         if (!debugfs_create_file("pwm_cap", 0644, fan_debugfs_root,
253                 (void *)fan_data,
254                 &fan_cap_pwm_fops))
255                 goto err_out;
256
257         if (!debugfs_create_file("pwm_rpm_table", 0444, fan_debugfs_root,
258                 (void *)fan_data,
259                 &fan_rpm_table_fops))
260                 goto err_out;
261
262         if (!debugfs_create_file("step_time", 0644, fan_debugfs_root,
263                 (void *)fan_data,
264                 &fan_step_time_fops))
265                 goto err_out;
266
267         if (!debugfs_create_file("cur_pwm", 0444, fan_debugfs_root,
268                 (void *)fan_data,
269                 &fan_cur_pwm_fops))
270                 goto err_out;
271         return 0;
272
273 err_out:
274         debugfs_remove_recursive(fan_debugfs_root);
275         return -ENOMEM;
276 }
277 #else
278 static inline int pwm_fan_debug_init(struct fan_dev_data *fan_data)
279 {
280         return 0;
281 }
282 #endif /* DEBUG_FS*/
283
284 static int pwm_fan_get_cur_state(struct thermal_cooling_device *cdev,
285                                                 unsigned long *cur_state)
286 {
287         struct fan_dev_data *fan_data = cdev->devdata;
288
289         if (!fan_data)
290                 return -EINVAL;
291
292         mutex_lock(&fan_data->fan_state_lock);
293         *cur_state = fan_data->next_state;
294         mutex_unlock(&fan_data->fan_state_lock);
295         return 0;
296 }
297
298 static int pwm_fan_set_cur_state(struct thermal_cooling_device *cdev,
299                                                 unsigned long cur_state)
300 {
301         struct fan_dev_data *fan_data = cdev->devdata;
302
303         if (!fan_data)
304                 return -EINVAL;
305 #ifdef CONFIG_DEBUG_FS
306         if (!fan_data->fan_temp_control_flag)
307                 return 0;
308 #endif
309         mutex_lock(&fan_data->fan_state_lock);
310
311         fan_data->next_state = cur_state;
312
313         if (fan_data->next_state <= 0)
314                 fan_data->next_target_pwm = 0;
315         else
316                 fan_data->next_target_pwm = fan_data->fan_pwm[cur_state - 1];
317
318         fan_data->next_target_pwm =
319                 min(fan_data->fan_cap_pwm, fan_data->next_target_pwm);
320         if (fan_data->next_target_pwm != fan_data->fan_cur_pwm)
321                 queue_delayed_work(fan_data->workqueue,
322                                         &(fan_data->fan_ramp_work),
323                                         msecs_to_jiffies(fan_data->step_time));
324
325         mutex_unlock(&fan_data->fan_state_lock);
326         return 0;
327 }
328
329 static int pwm_fan_get_max_state(struct thermal_cooling_device *cdev,
330                                                 unsigned long *max_state)
331 {
332         struct fan_dev_data *fan_data = cdev->devdata;
333
334         *max_state = fan_data->active_steps;
335         return 0;
336 }
337
338 static struct thermal_cooling_device_ops pwm_fan_cooling_ops = {
339         .get_max_state = pwm_fan_get_max_state,
340         .get_cur_state = pwm_fan_get_cur_state,
341         .set_cur_state = pwm_fan_set_cur_state,
342 };
343
344 static int fan_get_rru(int pwm, struct fan_dev_data *fan_data)
345 {
346         int i;
347
348         for (i = 0; i < fan_data->active_steps - 1 ; i++) {
349                 if ((pwm >= fan_data->fan_pwm[i]) &&
350                                 (pwm < fan_data->fan_pwm[i + 1])) {
351                         return fan_data->fan_rru[i];
352                 }
353         }
354         return fan_data->fan_rru[fan_data->active_steps - 1];
355 }
356
357 static int fan_get_rrd(int pwm, struct fan_dev_data *fan_data)
358 {
359         int i;
360
361         for (i = 0; i < fan_data->active_steps - 1 ; i++) {
362                 if ((pwm >= fan_data->fan_pwm[i]) &&
363                                 (pwm < fan_data->fan_pwm[i + 1])) {
364                         return fan_data->fan_rrd[i];
365                 }
366         }
367         return fan_data->fan_rrd[fan_data->active_steps - 1];
368 }
369
370 static void set_pwm_duty_cycle(int pwm, struct fan_dev_data *fan_data)
371 {
372         if (fan_data != NULL && fan_data->pwm_dev != NULL) {
373                 pwm_config(fan_data->pwm_dev, fan_data->pwm_period - pwm,
374                                                         fan_data->pwm_period);
375                 pwm_enable(fan_data->pwm_dev);
376         } else {
377                 dev_err(fan_data->dev,
378                                 "FAN:PWM device or fan data is null\n");
379         }
380 }
381
382 static int get_next_higher_pwm(int pwm, struct fan_dev_data *fan_data)
383 {
384         int i;
385
386         for (i = 0; i < fan_data->active_steps; i++)
387                 if (pwm < fan_data->fan_pwm[i])
388                         return fan_data->fan_pwm[i];
389
390         return fan_data->fan_pwm[fan_data->active_steps - 1];
391 }
392
393 static int get_next_lower_pwm(int pwm, struct fan_dev_data *fan_data)
394 {
395         int i;
396
397         for (i = fan_data->active_steps - 1; i >= 0; i--)
398                 if (pwm > fan_data->fan_pwm[i])
399                         return fan_data->fan_pwm[i];
400
401         return fan_data->fan_pwm[fan_data->active_steps - 1];
402 }
403
404 static void fan_ramping_work_func(struct work_struct *work)
405 {
406         int rru, rrd;
407         int cur_pwm, next_pwm;
408         struct delayed_work *dwork = container_of(work, struct delayed_work,
409                                                                         work);
410         struct fan_dev_data *fan_data = container_of(dwork, struct
411                                                 fan_dev_data, fan_ramp_work);
412
413         if (!fan_data) {
414                 dev_err(fan_data->dev, "Fan data is null\n");
415                 return;
416         }
417         mutex_lock(&fan_data->fan_state_lock);
418         cur_pwm = fan_data->fan_cur_pwm;
419         rru = fan_get_rru(cur_pwm, fan_data);
420         rrd = fan_get_rrd(cur_pwm, fan_data);
421         next_pwm = cur_pwm;
422
423         if (fan_data->next_target_pwm > fan_data->fan_cur_pwm) {
424                 fan_data->fan_cur_pwm = fan_data->fan_cur_pwm + rru;
425                 next_pwm = min(
426                                 get_next_higher_pwm(cur_pwm, fan_data),
427                                 fan_data->fan_cur_pwm);
428                 next_pwm = min(fan_data->next_target_pwm, next_pwm);
429                 next_pwm = min(fan_data->fan_cap_pwm, next_pwm);
430         } else if (fan_data->next_target_pwm < fan_data->fan_cur_pwm) {
431                 fan_data->fan_cur_pwm = fan_data->fan_cur_pwm - rrd;
432                 next_pwm = max(get_next_lower_pwm(cur_pwm, fan_data),
433                                                         fan_data->fan_cur_pwm);
434                 next_pwm = max(next_pwm, fan_data->next_target_pwm);
435                 next_pwm = max(0, next_pwm);
436         }
437         set_pwm_duty_cycle(next_pwm/fan_data->precision_multiplier, fan_data);
438         fan_data->fan_cur_pwm = next_pwm;
439         if (fan_data->next_target_pwm != next_pwm)
440                 queue_delayed_work(fan_data->workqueue,
441                                 &(fan_data->fan_ramp_work),
442                                 msecs_to_jiffies(fan_data->step_time));
443         mutex_unlock(&fan_data->fan_state_lock);
444 }
445
446
447 static ssize_t show_fan_pwm_cap_sysfs(struct device *dev,
448                                 struct device_attribute *attr, char *buf)
449 {
450         struct fan_dev_data *fan_data = dev_get_drvdata(dev);
451         int ret;
452
453         if (!fan_data)
454                 return -EINVAL;
455         mutex_lock(&fan_data->fan_state_lock);
456         ret = sprintf(buf, "%d\n",
457                 (fan_data->fan_cap_pwm / fan_data->precision_multiplier));
458         mutex_unlock(&fan_data->fan_state_lock);
459         return ret;
460 }
461
462 static ssize_t set_fan_pwm_cap_sysfs(struct device *dev,
463         struct device_attribute *attr, const char *buf, size_t count)
464 {
465         struct fan_dev_data *fan_data = dev_get_drvdata(dev);
466         long val;
467         int ret;
468
469         ret = kstrtol(buf, 10, &val);
470
471         if (ret < 0)
472                 return -EINVAL;
473
474         if (!fan_data)
475                 return -EINVAL;
476
477         if (val < 0)
478                 val = 0;
479         else if (val > fan_data->pwm_period)
480                 val = fan_data->pwm_period;
481         mutex_lock(&fan_data->fan_state_lock);
482         fan_data->fan_cap_pwm = val * fan_data->precision_multiplier;
483         fan_data->next_target_pwm = min(fan_data->fan_cap_pwm,
484                                         fan_data->next_target_pwm);
485         mutex_unlock(&fan_data->fan_state_lock);
486         return count;
487 }
488
489 static DEVICE_ATTR(pwm_cap, S_IWUSR | S_IRUGO, show_fan_pwm_cap_sysfs,
490                                                         set_fan_pwm_cap_sysfs);
491 static struct attribute *pwm_fan_attributes[] = {
492         &dev_attr_pwm_cap.attr,
493         NULL
494 };
495
496 static const struct attribute_group pwm_fan_group = {
497         .attrs = pwm_fan_attributes,
498 };
499
500 static int add_sysfs_entry(struct device *dev)
501 {
502         return sysfs_create_group(&dev->kobj, &pwm_fan_group);
503 }
504
505 static void remove_sysfs_entry(struct device *dev)
506 {
507         sysfs_remove_group(&dev->kobj, &pwm_fan_group);
508 }
509
510 static int __devinit pwm_fan_probe(struct platform_device *pdev)
511 {
512         int i;
513         struct pwm_fan_platform_data *data;
514         struct fan_dev_data *fan_data;
515         int *rpm_data;
516         int err = 0;
517
518         data = dev_get_platdata(&pdev->dev);
519         if (!data) {
520                 dev_err(&pdev->dev, "platform data is null\n");
521                 return -EINVAL;
522         }
523
524         fan_data = devm_kzalloc(&pdev->dev,
525                                 sizeof(struct fan_dev_data), GFP_KERNEL);
526         if (!fan_data)
527                 return -ENOMEM;
528
529         rpm_data = devm_kzalloc(&pdev->dev,
530                         4 * sizeof(int) * data->active_steps, GFP_KERNEL);
531         if (!rpm_data)
532                 return -ENOMEM;
533
534         fan_data->fan_rpm = rpm_data;
535         fan_data->fan_pwm = rpm_data + data->active_steps;
536         fan_data->fan_rru = fan_data->fan_pwm + data->active_steps;
537         fan_data->fan_rrd = fan_data->fan_rru + data->active_steps;
538
539         mutex_init(&fan_data->fan_state_lock);
540
541         fan_data->workqueue = alloc_workqueue(dev_name(&pdev->dev),
542                                 WQ_HIGHPRI | WQ_UNBOUND | WQ_RESCUER, 1);
543         if (!fan_data->workqueue)
544                 return -ENOMEM;
545
546         INIT_DELAYED_WORK(&(fan_data->fan_ramp_work), fan_ramping_work_func);
547
548         fan_data->precision_multiplier = data->precision_multiplier;
549         fan_data->fan_cap_pwm = data->pwm_cap * data->precision_multiplier;
550         fan_data->step_time = data->step_time;
551         fan_data->active_steps = data->active_steps;
552         fan_data->pwm_period = data->pwm_period;
553         fan_data->dev = &pdev->dev;
554
555         for (i = 0; i < fan_data->active_steps; i++) {
556                 fan_data->fan_rpm[i] = data->active_rpm[i];
557                 fan_data->fan_pwm[i] = data->active_pwm[i];
558                 fan_data->fan_rru[i] = data->active_rru[i];
559                 fan_data->fan_rrd[i] = data->active_rrd[i];
560                 dev_info(&pdev->dev, "rpm=%d, pwm=%d, rru=%d, rrd=%d\n",
561                                                 fan_data->fan_rpm[i],
562                                                 fan_data->fan_pwm[i],
563                                                 fan_data->fan_rru[i],
564                                                 fan_data->fan_rrd[i]);
565         }
566
567         fan_data->cdev =
568                 thermal_cooling_device_register((char *)dev_name(&pdev->dev),
569                                         fan_data, &pwm_fan_cooling_ops);
570
571         if (IS_ERR_OR_NULL(fan_data->cdev)) {
572                 dev_err(&pdev->dev, "Failed to register cooling device\n");
573                 return -EINVAL;
574         }
575
576         fan_data->pwm_dev = pwm_request(data->pwm_id, dev_name(&pdev->dev));
577         if (IS_ERR_OR_NULL(fan_data->pwm_dev)) {
578                 dev_err(&pdev->dev, "unable to request PWM for fan\n");
579                 err = -ENODEV;
580                 goto pwm_req_fail;
581         } else {
582                 dev_info(&pdev->dev, "got pwm for fan\n");
583         }
584
585         platform_set_drvdata(pdev, fan_data);
586
587         /*turn temp control on*/
588         fan_data->fan_temp_control_flag = 1;
589         set_pwm_duty_cycle(0, fan_data);
590
591         if (add_sysfs_entry(&pdev->dev) < 0) {
592                 dev_err(&pdev->dev, "FAN:Can't create syfs node");
593                 err = -ENOMEM;
594                 goto sysfs_fail;
595         }
596
597         if (pwm_fan_debug_init(fan_data) < 0) {
598                 dev_err(&pdev->dev, "FAN:Can't create debug fs nodes");
599                 /*Just continue without debug fs*/
600         }
601         return err;
602
603 sysfs_fail:
604         pwm_free(fan_data->pwm_dev);
605 pwm_req_fail:
606         thermal_cooling_device_unregister(fan_data->cdev);
607         return err;
608 }
609
610 static int __devexit pwm_fan_remove(struct platform_device *pdev)
611 {
612         struct fan_dev_data *fan_data = platform_get_drvdata(pdev);
613
614         if (!fan_data)
615                 return -EINVAL;
616         pwm_config(fan_data->pwm_dev, 0, fan_data->pwm_period);
617         pwm_disable(fan_data->pwm_dev);
618         pwm_free(fan_data->pwm_dev);
619         thermal_cooling_device_unregister(fan_data->cdev);
620         remove_sysfs_entry(&pdev->dev);
621         return 0;
622 }
623
624 static struct platform_driver pwm_fan_driver = {
625         .driver = {
626                 .owner = THIS_MODULE,
627                 .name = "pwm-fan",
628         },
629         .probe = pwm_fan_probe,
630         .remove = __devexit_p(pwm_fan_remove),
631 };
632
633 module_platform_driver(pwm_fan_driver);
634
635 MODULE_DESCRIPTION("pwm fan driver");
636 MODULE_AUTHOR("Anshul Jain <anshulj@nvidia.com>");
637 MODULE_LICENSE("GPL v2");