thermal: devfreq_cooling: refactor code and add get_voltage function
[linux-2.6-microblaze.git] / drivers / thermal / devfreq_cooling.c
1 /*
2  * devfreq_cooling: Thermal cooling device implementation for devices using
3  *                  devfreq
4  *
5  * Copyright (C) 2014-2015 ARM Limited
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
12  * kind, whether express or implied; without even the implied warranty
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * TODO:
17  *    - If OPPs are added or removed after devfreq cooling has
18  *      registered, the devfreq cooling won't react to it.
19  */
20
21 #include <linux/devfreq.h>
22 #include <linux/devfreq_cooling.h>
23 #include <linux/export.h>
24 #include <linux/idr.h>
25 #include <linux/slab.h>
26 #include <linux/pm_opp.h>
27 #include <linux/thermal.h>
28
29 #include <trace/events/thermal.h>
30
31 static DEFINE_IDA(devfreq_ida);
32
33 /**
34  * struct devfreq_cooling_device - Devfreq cooling device
35  * @id:         unique integer value corresponding to each
36  *              devfreq_cooling_device registered.
37  * @cdev:       Pointer to associated thermal cooling device.
38  * @devfreq:    Pointer to associated devfreq device.
39  * @cooling_state:      Current cooling state.
40  * @power_table:        Pointer to table with maximum power draw for each
41  *                      cooling state. State is the index into the table, and
42  *                      the power is in mW.
43  * @freq_table: Pointer to a table with the frequencies sorted in descending
44  *              order.  You can index the table by cooling device state
45  * @freq_table_size:    Size of the @freq_table and @power_table
46  * @power_ops:  Pointer to devfreq_cooling_power, used to generate the
47  *              @power_table.
48  */
49 struct devfreq_cooling_device {
50         int id;
51         struct thermal_cooling_device *cdev;
52         struct devfreq *devfreq;
53         unsigned long cooling_state;
54         u32 *power_table;
55         u32 *freq_table;
56         size_t freq_table_size;
57         struct devfreq_cooling_power *power_ops;
58 };
59
60 /**
61  * partition_enable_opps() - disable all opps above a given state
62  * @dfc:        Pointer to devfreq we are operating on
63  * @cdev_state: cooling device state we're setting
64  *
65  * Go through the OPPs of the device, enabling all OPPs until
66  * @cdev_state and disabling those frequencies above it.
67  */
68 static int partition_enable_opps(struct devfreq_cooling_device *dfc,
69                                  unsigned long cdev_state)
70 {
71         int i;
72         struct device *dev = dfc->devfreq->dev.parent;
73
74         for (i = 0; i < dfc->freq_table_size; i++) {
75                 struct dev_pm_opp *opp;
76                 int ret = 0;
77                 unsigned int freq = dfc->freq_table[i];
78                 bool want_enable = i >= cdev_state ? true : false;
79
80                 opp = dev_pm_opp_find_freq_exact(dev, freq, !want_enable);
81
82                 if (PTR_ERR(opp) == -ERANGE)
83                         continue;
84                 else if (IS_ERR(opp))
85                         return PTR_ERR(opp);
86
87                 dev_pm_opp_put(opp);
88
89                 if (want_enable)
90                         ret = dev_pm_opp_enable(dev, freq);
91                 else
92                         ret = dev_pm_opp_disable(dev, freq);
93
94                 if (ret)
95                         return ret;
96         }
97
98         return 0;
99 }
100
101 static int devfreq_cooling_get_max_state(struct thermal_cooling_device *cdev,
102                                          unsigned long *state)
103 {
104         struct devfreq_cooling_device *dfc = cdev->devdata;
105
106         *state = dfc->freq_table_size - 1;
107
108         return 0;
109 }
110
111 static int devfreq_cooling_get_cur_state(struct thermal_cooling_device *cdev,
112                                          unsigned long *state)
113 {
114         struct devfreq_cooling_device *dfc = cdev->devdata;
115
116         *state = dfc->cooling_state;
117
118         return 0;
119 }
120
121 static int devfreq_cooling_set_cur_state(struct thermal_cooling_device *cdev,
122                                          unsigned long state)
123 {
124         struct devfreq_cooling_device *dfc = cdev->devdata;
125         struct devfreq *df = dfc->devfreq;
126         struct device *dev = df->dev.parent;
127         int ret;
128
129         if (state == dfc->cooling_state)
130                 return 0;
131
132         dev_dbg(dev, "Setting cooling state %lu\n", state);
133
134         if (state >= dfc->freq_table_size)
135                 return -EINVAL;
136
137         ret = partition_enable_opps(dfc, state);
138         if (ret)
139                 return ret;
140
141         dfc->cooling_state = state;
142
143         return 0;
144 }
145
146 /**
147  * freq_get_state() - get the cooling state corresponding to a frequency
148  * @dfc:        Pointer to devfreq cooling device
149  * @freq:       frequency in Hz
150  *
151  * Return: the cooling state associated with the @freq, or
152  * THERMAL_CSTATE_INVALID if it wasn't found.
153  */
154 static unsigned long
155 freq_get_state(struct devfreq_cooling_device *dfc, unsigned long freq)
156 {
157         int i;
158
159         for (i = 0; i < dfc->freq_table_size; i++) {
160                 if (dfc->freq_table[i] == freq)
161                         return i;
162         }
163
164         return THERMAL_CSTATE_INVALID;
165 }
166
167 static unsigned long get_voltage(struct devfreq *df, unsigned long freq)
168 {
169         struct device *dev = df->dev.parent;
170         unsigned long voltage;
171         struct dev_pm_opp *opp;
172
173         opp = dev_pm_opp_find_freq_exact(dev, freq, true);
174         if (PTR_ERR(opp) == -ERANGE)
175                 opp = dev_pm_opp_find_freq_exact(dev, freq, false);
176
177         if (IS_ERR(opp)) {
178                 dev_err_ratelimited(dev, "Failed to find OPP for frequency %lu: %ld\n",
179                                     freq, PTR_ERR(opp));
180                 return 0;
181         }
182
183         voltage = dev_pm_opp_get_voltage(opp) / 1000; /* mV */
184         dev_pm_opp_put(opp);
185
186         if (voltage == 0) {
187                 dev_err_ratelimited(dev,
188                                     "Failed to get voltage for frequency %lu\n",
189                                     freq);
190         }
191
192         return voltage;
193 }
194
195 /**
196  * get_static_power() - calculate the static power
197  * @dfc:        Pointer to devfreq cooling device
198  * @freq:       Frequency in Hz
199  *
200  * Calculate the static power in milliwatts using the supplied
201  * get_static_power().  The current voltage is calculated using the
202  * OPP library.  If no get_static_power() was supplied, assume the
203  * static power is negligible.
204  */
205 static unsigned long
206 get_static_power(struct devfreq_cooling_device *dfc, unsigned long freq)
207 {
208         struct devfreq *df = dfc->devfreq;
209         unsigned long voltage;
210
211         if (!dfc->power_ops->get_static_power)
212                 return 0;
213
214         voltage = get_voltage(df, freq);
215
216         if (voltage == 0)
217                 return 0;
218
219         return dfc->power_ops->get_static_power(df, voltage);
220 }
221
222 /**
223  * get_dynamic_power - calculate the dynamic power
224  * @dfc:        Pointer to devfreq cooling device
225  * @freq:       Frequency in Hz
226  * @voltage:    Voltage in millivolts
227  *
228  * Calculate the dynamic power in milliwatts consumed by the device at
229  * frequency @freq and voltage @voltage.  If the get_dynamic_power()
230  * was supplied as part of the devfreq_cooling_power struct, then that
231  * function is used.  Otherwise, a simple power model (Pdyn = Coeff *
232  * Voltage^2 * Frequency) is used.
233  */
234 static unsigned long
235 get_dynamic_power(struct devfreq_cooling_device *dfc, unsigned long freq,
236                   unsigned long voltage)
237 {
238         u64 power;
239         u32 freq_mhz;
240         struct devfreq_cooling_power *dfc_power = dfc->power_ops;
241
242         if (dfc_power->get_dynamic_power)
243                 return dfc_power->get_dynamic_power(dfc->devfreq, freq,
244                                                     voltage);
245
246         freq_mhz = freq / 1000000;
247         power = (u64)dfc_power->dyn_power_coeff * freq_mhz * voltage * voltage;
248         do_div(power, 1000000000);
249
250         return power;
251 }
252
253 static int devfreq_cooling_get_requested_power(struct thermal_cooling_device *cdev,
254                                                struct thermal_zone_device *tz,
255                                                u32 *power)
256 {
257         struct devfreq_cooling_device *dfc = cdev->devdata;
258         struct devfreq *df = dfc->devfreq;
259         struct devfreq_dev_status *status = &df->last_status;
260         unsigned long state;
261         unsigned long freq = status->current_frequency;
262         u32 dyn_power, static_power;
263
264         /* Get dynamic power for state */
265         state = freq_get_state(dfc, freq);
266         if (state == THERMAL_CSTATE_INVALID)
267                 return -EAGAIN;
268
269         dyn_power = dfc->power_table[state];
270
271         /* Scale dynamic power for utilization */
272         dyn_power = (dyn_power * status->busy_time) / status->total_time;
273
274         /* Get static power */
275         static_power = get_static_power(dfc, freq);
276
277         trace_thermal_power_devfreq_get_power(cdev, status, freq, dyn_power,
278                                               static_power);
279
280         *power = dyn_power + static_power;
281
282         return 0;
283 }
284
285 static int devfreq_cooling_state2power(struct thermal_cooling_device *cdev,
286                                        struct thermal_zone_device *tz,
287                                        unsigned long state,
288                                        u32 *power)
289 {
290         struct devfreq_cooling_device *dfc = cdev->devdata;
291         unsigned long freq;
292         u32 static_power;
293
294         if (state >= dfc->freq_table_size)
295                 return -EINVAL;
296
297         freq = dfc->freq_table[state];
298         static_power = get_static_power(dfc, freq);
299
300         *power = dfc->power_table[state] + static_power;
301         return 0;
302 }
303
304 static int devfreq_cooling_power2state(struct thermal_cooling_device *cdev,
305                                        struct thermal_zone_device *tz,
306                                        u32 power, unsigned long *state)
307 {
308         struct devfreq_cooling_device *dfc = cdev->devdata;
309         struct devfreq *df = dfc->devfreq;
310         struct devfreq_dev_status *status = &df->last_status;
311         unsigned long freq = status->current_frequency;
312         unsigned long busy_time;
313         s32 dyn_power;
314         u32 static_power;
315         int i;
316
317         static_power = get_static_power(dfc, freq);
318
319         dyn_power = power - static_power;
320         dyn_power = dyn_power > 0 ? dyn_power : 0;
321
322         /* Scale dynamic power for utilization */
323         busy_time = status->busy_time ?: 1;
324         dyn_power = (dyn_power * status->total_time) / busy_time;
325
326         /*
327          * Find the first cooling state that is within the power
328          * budget for dynamic power.
329          */
330         for (i = 0; i < dfc->freq_table_size - 1; i++)
331                 if (dyn_power >= dfc->power_table[i])
332                         break;
333
334         *state = i;
335         trace_thermal_power_devfreq_limit(cdev, freq, *state, power);
336         return 0;
337 }
338
339 static struct thermal_cooling_device_ops devfreq_cooling_ops = {
340         .get_max_state = devfreq_cooling_get_max_state,
341         .get_cur_state = devfreq_cooling_get_cur_state,
342         .set_cur_state = devfreq_cooling_set_cur_state,
343 };
344
345 /**
346  * devfreq_cooling_gen_tables() - Generate power and freq tables.
347  * @dfc: Pointer to devfreq cooling device.
348  *
349  * Generate power and frequency tables: the power table hold the
350  * device's maximum power usage at each cooling state (OPP).  The
351  * static and dynamic power using the appropriate voltage and
352  * frequency for the state, is acquired from the struct
353  * devfreq_cooling_power, and summed to make the maximum power draw.
354  *
355  * The frequency table holds the frequencies in descending order.
356  * That way its indexed by cooling device state.
357  *
358  * The tables are malloced, and pointers put in dfc.  They must be
359  * freed when unregistering the devfreq cooling device.
360  *
361  * Return: 0 on success, negative error code on failure.
362  */
363 static int devfreq_cooling_gen_tables(struct devfreq_cooling_device *dfc)
364 {
365         struct devfreq *df = dfc->devfreq;
366         struct device *dev = df->dev.parent;
367         int ret, num_opps;
368         unsigned long freq;
369         u32 *power_table = NULL;
370         u32 *freq_table;
371         int i;
372
373         num_opps = dev_pm_opp_get_opp_count(dev);
374
375         if (dfc->power_ops) {
376                 power_table = kcalloc(num_opps, sizeof(*power_table),
377                                       GFP_KERNEL);
378                 if (!power_table)
379                         return -ENOMEM;
380         }
381
382         freq_table = kcalloc(num_opps, sizeof(*freq_table),
383                              GFP_KERNEL);
384         if (!freq_table) {
385                 ret = -ENOMEM;
386                 goto free_power_table;
387         }
388
389         for (i = 0, freq = ULONG_MAX; i < num_opps; i++, freq--) {
390                 unsigned long power_dyn, voltage;
391                 struct dev_pm_opp *opp;
392
393                 opp = dev_pm_opp_find_freq_floor(dev, &freq);
394                 if (IS_ERR(opp)) {
395                         ret = PTR_ERR(opp);
396                         goto free_tables;
397                 }
398
399                 voltage = dev_pm_opp_get_voltage(opp) / 1000; /* mV */
400                 dev_pm_opp_put(opp);
401
402                 if (dfc->power_ops) {
403                         power_dyn = get_dynamic_power(dfc, freq, voltage);
404
405                         dev_dbg(dev, "Dynamic power table: %lu MHz @ %lu mV: %lu = %lu mW\n",
406                                 freq / 1000000, voltage, power_dyn, power_dyn);
407
408                         power_table[i] = power_dyn;
409                 }
410
411                 freq_table[i] = freq;
412         }
413
414         if (dfc->power_ops)
415                 dfc->power_table = power_table;
416
417         dfc->freq_table = freq_table;
418         dfc->freq_table_size = num_opps;
419
420         return 0;
421
422 free_tables:
423         kfree(freq_table);
424 free_power_table:
425         kfree(power_table);
426
427         return ret;
428 }
429
430 /**
431  * of_devfreq_cooling_register_power() - Register devfreq cooling device,
432  *                                      with OF and power information.
433  * @np: Pointer to OF device_node.
434  * @df: Pointer to devfreq device.
435  * @dfc_power:  Pointer to devfreq_cooling_power.
436  *
437  * Register a devfreq cooling device.  The available OPPs must be
438  * registered on the device.
439  *
440  * If @dfc_power is provided, the cooling device is registered with the
441  * power extensions.  For the power extensions to work correctly,
442  * devfreq should use the simple_ondemand governor, other governors
443  * are not currently supported.
444  */
445 struct thermal_cooling_device *
446 of_devfreq_cooling_register_power(struct device_node *np, struct devfreq *df,
447                                   struct devfreq_cooling_power *dfc_power)
448 {
449         struct thermal_cooling_device *cdev;
450         struct devfreq_cooling_device *dfc;
451         char dev_name[THERMAL_NAME_LENGTH];
452         int err;
453
454         dfc = kzalloc(sizeof(*dfc), GFP_KERNEL);
455         if (!dfc)
456                 return ERR_PTR(-ENOMEM);
457
458         dfc->devfreq = df;
459
460         if (dfc_power) {
461                 dfc->power_ops = dfc_power;
462
463                 devfreq_cooling_ops.get_requested_power =
464                         devfreq_cooling_get_requested_power;
465                 devfreq_cooling_ops.state2power = devfreq_cooling_state2power;
466                 devfreq_cooling_ops.power2state = devfreq_cooling_power2state;
467         }
468
469         err = devfreq_cooling_gen_tables(dfc);
470         if (err)
471                 goto free_dfc;
472
473         err = ida_simple_get(&devfreq_ida, 0, 0, GFP_KERNEL);
474         if (err < 0)
475                 goto free_tables;
476         dfc->id = err;
477
478         snprintf(dev_name, sizeof(dev_name), "thermal-devfreq-%d", dfc->id);
479
480         cdev = thermal_of_cooling_device_register(np, dev_name, dfc,
481                                                   &devfreq_cooling_ops);
482         if (IS_ERR(cdev)) {
483                 err = PTR_ERR(cdev);
484                 dev_err(df->dev.parent,
485                         "Failed to register devfreq cooling device (%d)\n",
486                         err);
487                 goto release_ida;
488         }
489
490         dfc->cdev = cdev;
491
492         return cdev;
493
494 release_ida:
495         ida_simple_remove(&devfreq_ida, dfc->id);
496 free_tables:
497         kfree(dfc->power_table);
498         kfree(dfc->freq_table);
499 free_dfc:
500         kfree(dfc);
501
502         return ERR_PTR(err);
503 }
504 EXPORT_SYMBOL_GPL(of_devfreq_cooling_register_power);
505
506 /**
507  * of_devfreq_cooling_register() - Register devfreq cooling device,
508  *                                with OF information.
509  * @np: Pointer to OF device_node.
510  * @df: Pointer to devfreq device.
511  */
512 struct thermal_cooling_device *
513 of_devfreq_cooling_register(struct device_node *np, struct devfreq *df)
514 {
515         return of_devfreq_cooling_register_power(np, df, NULL);
516 }
517 EXPORT_SYMBOL_GPL(of_devfreq_cooling_register);
518
519 /**
520  * devfreq_cooling_register() - Register devfreq cooling device.
521  * @df: Pointer to devfreq device.
522  */
523 struct thermal_cooling_device *devfreq_cooling_register(struct devfreq *df)
524 {
525         return of_devfreq_cooling_register(NULL, df);
526 }
527 EXPORT_SYMBOL_GPL(devfreq_cooling_register);
528
529 /**
530  * devfreq_cooling_unregister() - Unregister devfreq cooling device.
531  * @dfc: Pointer to devfreq cooling device to unregister.
532  */
533 void devfreq_cooling_unregister(struct thermal_cooling_device *cdev)
534 {
535         struct devfreq_cooling_device *dfc;
536
537         if (!cdev)
538                 return;
539
540         dfc = cdev->devdata;
541
542         thermal_cooling_device_unregister(dfc->cdev);
543         ida_simple_remove(&devfreq_ida, dfc->id);
544         kfree(dfc->power_table);
545         kfree(dfc->freq_table);
546
547         kfree(dfc);
548 }
549 EXPORT_SYMBOL_GPL(devfreq_cooling_unregister);