Merge branch 'stable-3.2' into pandora-3.2
[pandora-kernel.git] / drivers / misc / omap_overclocking.c
1 /*
2  * OMAP CPU overclocking hacks
3  *
4  * Licensed under the GPL-2 or later.
5  */
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/proc_fs.h>
9 #include <linux/opp.h>
10 #include <linux/clk.h>
11 #include <linux/uaccess.h>
12 #include <linux/regulator/consumer.h>
13
14 #include <plat/omap_device.h>
15
16 #define PROC_DIR        "pandora"
17 #define PROC_CPUMHZ     "pandora/cpu_mhz_max"
18 #define PROC_DSPMHZ     "pandora/dsp_mhz_max"
19 #define PROC_CPUOPP     "pandora/cpu_opp_max"
20 #define PROC_SYSMHZ     "pandora/sys_mhz_max"
21
22 static struct device *mpu_dev;
23
24 static struct device *iva_dev;
25 static struct regulator *iva_reg;
26 static struct clk *iva_clk;
27 static DEFINE_MUTEX(iva_lock);
28 static struct delayed_work iva_work;
29 static int iva_mhz_max;
30 static int iva_opp_min;
31 static int iva_active;
32
33 /* XXX: could use opp3xxx_data.c, but that's initdata.. */
34 static const unsigned long nominal_f_mpu_35xx[] = {
35         125000000, 250000000, 500000000, 550000000, 600000000,
36 };
37
38 static const unsigned long nominal_f_mpu_36xx[] = {
39         300000000, 600000000, 800000000, 1000000000,
40 };
41
42 static const unsigned long nominal_f_iva_35xx[] = {
43         90000000,  180000000, 360000000, 400000000, 430000000,
44 };
45
46 static const unsigned long nominal_f_iva_36xx[] = {
47         260000000, 520000000, 660000000, 800000000,
48 };
49
50 static const unsigned long *nominal_freqs_mpu;
51 static const unsigned long *nominal_freqs_iva;
52
53 /* IVA voltages (MPU ones are managed by cpufreq) */
54 static unsigned long iva_voltages[5];
55
56 static int opp_max_avail, opp_max_now, opp_max_ceil;
57
58 static int set_mpu_opp_max(int new_opp_max)
59 {
60         int i, ret;
61
62         if (new_opp_max == opp_max_now)
63                 return 0;
64
65         for (i = 1; i < new_opp_max; i++) {
66                 ret = opp_enable_i(mpu_dev, i);
67                 if (ret != 0)
68                         dev_err(mpu_dev, "%s: mpu opp_enable returned %d\n",
69                                 __func__, ret);
70         }
71
72         for (i = new_opp_max; i < opp_max_avail; i++) {
73                 ret = opp_disable_i(mpu_dev, i);
74                 if (ret != 0)
75                         dev_err(mpu_dev, "%s: mpu opp_disable returned %d\n",
76                                 __func__, ret);
77         }
78
79         dev_info(mpu_dev, "max MPU OPP set to %d\n", new_opp_max);
80         opp_max_now = new_opp_max;
81
82         return 0;
83 }
84
85 static int set_opp_max_ceil(int new_opp_max)
86 {
87         opp_max_ceil = new_opp_max;
88         return set_mpu_opp_max(new_opp_max);
89 }
90
91 static int set_mpu_mhz_max(unsigned long new_mhz_max)
92 {
93         unsigned long cur_mhz_max = 0;
94         int index, ret;
95
96         new_mhz_max *= 1000000;
97
98         if (opp_max_ceil < 1 || opp_max_ceil > opp_max_avail) {
99                 pr_err("%s: corrupt opp_max_ceil: %d\n",
100                         __func__, opp_max_ceil);
101                 return -EINVAL;
102         }
103
104         /* determine minimum OPP needed for given MPU clock limit,
105          * and limit that opp as maximum OPP.
106          * This is for cpufreq governors only. */
107         index = opp_max_ceil - 1;
108         while (index > 0 && new_mhz_max <= nominal_freqs_mpu[index - 1])
109                 index--;
110
111         set_mpu_opp_max(index + 1);
112
113         opp_hack_get_freq(mpu_dev, index, &cur_mhz_max);
114         if (cur_mhz_max == new_mhz_max)
115                 return 0;
116
117         ret = opp_hack_set_freq(mpu_dev, index, new_mhz_max);
118         if (ret != 0) {
119                 dev_err(mpu_dev, "%s: opp_hack_set_freq returned %d\n",
120                         __func__, ret);
121                 return ret;
122         }
123
124         return 0;
125 }
126
127 static int get_mpu_mhz_max(void)
128 {
129         unsigned long cur_mhz_max = 0;
130
131         if (opp_max_now < 1 || opp_max_now > opp_max_avail) {
132                 pr_err("%s: corrupt opp_max: %d\n", __func__, opp_max_now);
133                 return -EINVAL;
134         }
135
136         opp_hack_get_freq(mpu_dev, opp_max_now - 1, &cur_mhz_max);
137
138         return cur_mhz_max / 1000000;
139 }
140
141 static void update_iva_opp_limit(int target_mhz)
142 {
143         int volt_max;
144         int i, ret;
145
146         for (i = 0; i < opp_max_ceil - 1; i++) {
147                 if (target_mhz * 1000000 <= nominal_freqs_iva[i])
148                         break;
149         }
150
151         if (iva_opp_min == i + 1)
152                 return;
153
154         //dev_info(iva_dev, "new IVA OPP %d for clock %d\n",
155         //      i + 1, target_mhz);
156
157         volt_max = iva_voltages[opp_max_avail - 1];
158         volt_max += volt_max * 4 / 100;
159
160         ret = regulator_set_voltage(iva_reg, iva_voltages[i], volt_max);
161         if (ret < 0)
162                 dev_warn(iva_dev, "unable to set IVA OPP limits: %d\n", ret);
163         else
164                 iva_opp_min = i + 1;
165 }
166
167 static int set_dsp_mhz_max(unsigned long new_mhz_max)
168 {
169         int ret;
170
171         mutex_lock(&iva_lock);
172
173         if (iva_active && new_mhz_max > iva_mhz_max)
174                 /* going up.. */
175                 update_iva_opp_limit(new_mhz_max);
176
177         ret = clk_set_rate(iva_clk, new_mhz_max * 1000000);
178         if (ret != 0) {
179                 dev_warn(iva_dev, "unable to change IVA clock to %lu: %d\n",
180                         new_mhz_max, ret);
181                 goto out;
182         }
183
184         if (iva_active && new_mhz_max < iva_mhz_max)
185                 /* going down.. */
186                 update_iva_opp_limit(new_mhz_max);
187
188         iva_mhz_max = new_mhz_max;
189 out:
190         mutex_unlock(&iva_lock);
191
192         return ret;
193 }
194
195 static int get_dsp_mhz_max(void)
196 {
197         return iva_mhz_max;
198 }
199
200 static void iva_unneeded_work(struct work_struct *work)
201 {
202         mutex_lock(&iva_lock);
203
204         update_iva_opp_limit(0);
205         iva_active = 0;
206
207         mutex_unlock(&iva_lock);
208 }
209
210 /* called from c64_tools */
211 void dsp_power_notify(int enable)
212 {
213         if (enable) {
214                 cancel_delayed_work_sync(&iva_work);
215
216                 mutex_lock(&iva_lock);
217
218                 if (iva_active) {
219                         mutex_unlock(&iva_lock);
220                         return;
221                 }
222
223                 /* apply the OPP limit */
224                 update_iva_opp_limit(iva_mhz_max);
225                 iva_active = 1;
226
227                 mutex_unlock(&iva_lock);
228         }
229         else {
230                 if (!iva_active)
231                         return;
232
233                 cancel_delayed_work_sync(&iva_work);
234                 schedule_delayed_work(&iva_work, HZ * 2);
235         }
236 }
237 EXPORT_SYMBOL(dsp_power_notify);
238
239 static int init_opp_hacks(void)
240 {
241         int iva_init_freq;
242         struct opp *opp;
243         int i, ret;
244
245         if (cpu_is_omap3630()) {
246                 nominal_freqs_mpu = nominal_f_mpu_36xx;
247                 nominal_freqs_iva = nominal_f_iva_36xx;
248                 opp_max_avail = sizeof(nominal_f_mpu_36xx) / sizeof(nominal_f_mpu_36xx[0]);
249                 opp_max_ceil = 2;
250         } else if (cpu_is_omap34xx()) {
251                 nominal_freqs_mpu = nominal_f_mpu_35xx;
252                 nominal_freqs_iva = nominal_f_iva_35xx;
253                 opp_max_avail = sizeof(nominal_f_mpu_35xx) / sizeof(nominal_f_mpu_35xx[0]);
254                 opp_max_ceil = opp_max_avail;
255         } else {
256                 dev_err(mpu_dev, "%s: unsupported CPU\n", __func__);
257                 return -ENODEV;
258         }
259         opp_max_now = opp_max_ceil;
260
261         for (i = 0; i < opp_max_avail; i++) {
262                 /* enable all OPPs for MPU so that cpufreq can find out
263                  * maximum voltage to supply to regulator as max */
264                 ret = opp_enable_i(mpu_dev, i);
265                 if (ret != 0) {
266                         dev_err(mpu_dev, "opp_enable returned %d\n", ret);
267                         return ret;
268                 }
269
270                 ret = opp_enable_i(iva_dev, i);
271                 if (ret != 0) {
272                         dev_err(iva_dev, "opp_enable returned %d\n", ret);
273                         return ret;
274                 }
275
276                 opp = opp_find_freq_exact(iva_dev, nominal_freqs_iva[i], true);
277                 if (IS_ERR(opp)) {
278                         dev_err(iva_dev, "mising opp %d, %lu\n",
279                                 i, nominal_freqs_iva[i]);
280                         return PTR_ERR(opp);
281                 }
282                 iva_voltages[i] = opp_get_voltage(opp);
283         }
284
285         iva_init_freq = nominal_freqs_iva[(i + 1) / 2];
286         ret = clk_set_rate(iva_clk, iva_init_freq);
287         if (ret == 0) {
288                 iva_mhz_max = iva_init_freq / 1000000;
289                 dev_info(iva_dev, "IVA freq set to %dMHz\n", iva_mhz_max);
290         }
291         else
292                 dev_err(iva_dev, "IVA freq set failed: %d\n", ret);
293
294         return 0;
295 }
296
297 static int set_sys_mhz_max(unsigned long rate)
298 {
299         struct clk *dpll3_m2_ck;
300         int ret;
301
302         rate *= 1000000;
303
304         dpll3_m2_ck = clk_get(NULL, "dpll3_m2_ck");
305         if (IS_ERR(dpll3_m2_ck)) {
306                 pr_err("%s: dpll3_m2_clk not available: %ld\n",
307                         __func__, PTR_ERR(dpll3_m2_ck));
308                 return -ENODEV;
309         }
310
311         pr_info("Reprogramming CORE clock to %luHz\n", rate);
312         ret = clk_set_rate(dpll3_m2_ck, rate);
313         if (ret)
314                 pr_err("dpll3_m2_clk rate change failed: %d\n", ret);
315
316         clk_put(dpll3_m2_ck);
317
318         return ret;
319 }
320
321 static int get_sys_mhz_max(void)
322 {
323         struct clk *dpll3_m2_ck;
324         int ret;
325
326         dpll3_m2_ck = clk_get(NULL, "dpll3_m2_ck");
327         if (IS_ERR(dpll3_m2_ck)) {
328                 pr_err("%s: dpll3_m2_clk not available: %ld\n",
329                         __func__, PTR_ERR(dpll3_m2_ck));
330                 return -ENODEV;
331         }
332
333         ret = clk_get_rate(dpll3_m2_ck);
334         clk_put(dpll3_m2_ck);
335
336         return ret / 1000000;
337 }
338
339 static int proc_read_val(char *page, char **start, off_t off, int count,
340                 int *eof, int val)
341 {
342         char *p = page;
343         int len;
344
345         p += sprintf(p, "%d\n", val);
346
347         len = (p - page) - off;
348         if (len < 0)
349                 len = 0;
350
351         *eof = (len <= count) ? 1 : 0;
352         *start = page + off;
353
354         return len;
355 }
356
357 static int proc_write_val(struct file *file, const char __user *buffer,
358                 unsigned long count, unsigned long *val)
359 {
360         char buff[32];
361         int ret;
362
363         count = strncpy_from_user(buff, buffer,
364                         count < sizeof(buff) ? count : sizeof(buff) - 1);
365         buff[count] = 0;
366
367         ret = strict_strtoul(buff, 0, val);
368         if (ret < 0) {
369                 pr_err("error %i parsing %s\n", ret, buff);
370                 return ret;
371         }
372
373         return count;
374 }
375
376 static int cpu_clk_read(char *page, char **start, off_t off, int count,
377                 int *eof, void *data)
378 {
379         return proc_read_val(page, start, off, count, eof, get_mpu_mhz_max());
380 }
381
382 static int cpu_clk_write(struct file *file, const char __user *buffer,
383                 unsigned long count, void *data)
384 {
385         unsigned long val;
386         int ret, retval;
387
388         retval = proc_write_val(file, buffer, count, &val);
389         if (retval < 0)
390                 return retval;
391
392         ret = set_mpu_mhz_max(val);
393         if (ret < 0)
394                 return ret;
395
396         return retval;
397 }
398
399 static int dsp_clk_read(char *page, char **start, off_t off, int count,
400                 int *eof, void *data)
401 {
402         return proc_read_val(page, start, off, count, eof, get_dsp_mhz_max());
403 }
404
405 static int dsp_clk_write(struct file *file, const char __user *buffer,
406                 unsigned long count, void *data)
407 {
408         unsigned long val;
409         int ret, retval;
410
411         retval = proc_write_val(file, buffer, count, &val);
412         if (retval < 0)
413                 return retval;
414
415         ret = set_dsp_mhz_max(val);
416         if (ret < 0)
417                 return ret;
418
419         return retval;
420 }
421
422 static int cpu_maxopp_read(char *page, char **start, off_t off, int count,
423                 int *eof, void *data)
424 {
425         return proc_read_val(page, start, off, count, eof, opp_max_ceil);
426 }
427
428 static int cpu_maxopp_write(struct file *file, const char __user *buffer,
429                 unsigned long count, void *data)
430 {
431         unsigned long val;
432         int ret, retval;
433
434         retval = proc_write_val(file, buffer, count, &val);
435         if (retval < 0)
436                 return retval;
437
438         if (val > opp_max_avail)
439                 val = opp_max_avail;
440
441         if (val < 1)
442                 return -EINVAL;
443
444         ret = set_opp_max_ceil(val);
445         if (ret != 0)
446                 return ret;
447
448         return retval;
449 }
450
451 static int sys_clk_read(char *page, char **start, off_t off, int count,
452                 int *eof, void *data)
453 {
454         return proc_read_val(page, start, off, count, eof, get_sys_mhz_max());
455 }
456
457 static int sys_clk_write(struct file *file, const char __user *buffer,
458                 unsigned long count, void *data)
459 {
460         unsigned long val;
461         int ret, retval;
462
463         retval = proc_write_val(file, buffer, count, &val);
464         if (retval < 0)
465                 return retval;
466
467         ret = set_sys_mhz_max(val);
468         if (ret < 0)
469                 return ret;
470
471         return retval;
472 }
473
474 static void proc_create_rw(const char *name, void *pdata,
475                            read_proc_t *read_proc, write_proc_t *write_proc)
476 {
477         struct proc_dir_entry *pret;
478
479         pret = create_proc_entry(name, S_IWUSR | S_IRUGO, NULL);
480         if (pret == NULL) {
481                 proc_mkdir(PROC_DIR, NULL);
482                 pret = create_proc_entry(name, S_IWUSR | S_IRUGO, NULL);
483                 if (pret == NULL) {
484                         pr_err("%s: failed to create proc file %s\n",
485                                 __func__, name);
486                         return;
487                 }
488         }
489
490         pret->data = pdata;
491         pret->read_proc = read_proc;
492         pret->write_proc = write_proc;
493 }
494
495 static int pndctrl_init(void)
496 {
497         int ret;
498
499         INIT_DELAYED_WORK(&iva_work, iva_unneeded_work);
500
501         mpu_dev = omap_device_get_by_hwmod_name("mpu");
502         if (IS_ERR(mpu_dev)) {
503                 pr_err("%s: mpu device not available (%ld)\n",
504                         __func__, PTR_ERR(mpu_dev));
505                 return -ENODEV;
506         }
507
508         iva_dev = omap_device_get_by_hwmod_name("iva");
509         if (IS_ERR(iva_dev)) {
510                 pr_err("%s: iva device not available (%ld)\n",
511                         __func__, PTR_ERR(iva_dev));
512                 return -ENODEV;
513         }
514
515         /* regulator to constrain OPPs while DSP is running */
516         iva_reg = regulator_get(iva_dev, "vcc");
517         if (IS_ERR(iva_reg)) {
518                 dev_err(iva_dev, "unable to get MPU regulator\n");
519                 return -ENODEV;
520         }
521
522         /* 
523          * Ensure physical regulator is present.
524          * (e.g. could be dummy regulator.)
525          */
526         if (regulator_get_voltage(iva_reg) < 0) {
527                 dev_err(iva_dev, "IVA regulator is not physical?\n");
528                 ret = -ENODEV;
529                 goto fail_reg;
530         }
531
532         iva_clk = clk_get(NULL, "dpll2_ck");
533         if (IS_ERR(iva_clk)) {
534                 dev_err(iva_dev, "IVA clock not available.\n");
535                 ret = PTR_ERR(iva_clk);
536                 goto fail_reg;
537         }
538
539         ret = init_opp_hacks();
540         if (ret != 0) {
541                 pr_err("init_opp_hacks failed: %d\n", ret);
542                 goto fail_opp;
543         }
544
545         proc_create_rw(PROC_CPUMHZ, NULL, cpu_clk_read, cpu_clk_write);
546         proc_create_rw(PROC_DSPMHZ, NULL, dsp_clk_read, dsp_clk_write);
547         proc_create_rw(PROC_CPUOPP, NULL, cpu_maxopp_read, cpu_maxopp_write);
548         proc_create_rw(PROC_SYSMHZ, NULL, sys_clk_read, sys_clk_write);
549
550         pr_info("OMAP overclocker loaded.\n");
551         return 0;
552
553 fail_opp:
554         clk_put(iva_clk);
555 fail_reg:
556         regulator_put(iva_reg);
557         return ret;
558 }
559
560
561 static void pndctrl_cleanup(void)
562 {
563         remove_proc_entry(PROC_SYSMHZ, NULL);
564         remove_proc_entry(PROC_CPUOPP, NULL);
565         remove_proc_entry(PROC_DSPMHZ, NULL);
566         remove_proc_entry(PROC_CPUMHZ, NULL);
567
568         cancel_delayed_work_sync(&iva_work);
569         regulator_put(iva_reg);
570         clk_put(iva_clk);
571 }
572
573 module_init(pndctrl_init);
574 module_exit(pndctrl_cleanup);
575
576 MODULE_AUTHOR("GraÅžvydas Ignotas");
577 MODULE_LICENSE("GPL");
578 MODULE_DESCRIPTION("OMAP overclocking support");