video: tegra: host: expose power management paramters via sysfs
[linux-2.6.git] / drivers / video / tegra / host / nvhost_acm.c
1 /*
2  * drivers/video/tegra/host/nvhost_acm.c
3  *
4  * Tegra Graphics Host Automatic Clock Management
5  *
6  * Copyright (c) 2010-2012, NVIDIA Corporation. All rights reserved.
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 "nvhost_acm.h"
22 #include "dev.h"
23 #include <linux/slab.h>
24 #include <linux/string.h>
25 #include <linux/sched.h>
26 #include <linux/err.h>
27 #include <linux/device.h>
28 #include <linux/delay.h>
29 #include <linux/platform_device.h>
30 #include <mach/powergate.h>
31 #include <mach/clk.h>
32 #include <mach/hardware.h>
33
34 #define ACM_SUSPEND_WAIT_FOR_IDLE_TIMEOUT (2 * HZ)
35 #define POWERGATE_DELAY 10
36 #define MAX_DEVID_LENGTH 16
37
38 DEFINE_MUTEX(client_list_lock);
39
40 struct nvhost_module_client {
41         struct list_head node;
42         unsigned long rate[NVHOST_MODULE_MAX_CLOCKS];
43         void *priv;
44 };
45
46 static void do_powergate_locked(int id)
47 {
48         if (id != -1 && tegra_powergate_is_powered(id))
49                 tegra_powergate_partition(id);
50 }
51
52 static void do_unpowergate_locked(int id)
53 {
54         if (id != -1)
55                 tegra_unpowergate_partition(id);
56 }
57
58 static void do_module_reset_locked(struct nvhost_device *dev)
59 {
60         /* assert module and mc client reset */
61         if (dev->powergate_ids[0] != -1) {
62                 tegra_powergate_mc_disable(dev->powergate_ids[0]);
63                 tegra_periph_reset_assert(dev->clk[0]);
64                 tegra_powergate_mc_flush(dev->powergate_ids[0]);
65         }
66         if (dev->powergate_ids[1] != -1) {
67                 tegra_powergate_mc_disable(dev->powergate_ids[1]);
68                 tegra_periph_reset_assert(dev->clk[1]);
69                 tegra_powergate_mc_flush(dev->powergate_ids[1]);
70         }
71
72         udelay(POWERGATE_DELAY);
73
74         /* deassert reset */
75         if (dev->powergate_ids[0] != -1) {
76                 tegra_powergate_mc_flush_done(dev->powergate_ids[0]);
77                 tegra_periph_reset_deassert(dev->clk[0]);
78                 tegra_powergate_mc_enable(dev->powergate_ids[0]);
79         }
80         if (dev->powergate_ids[1] != -1) {
81                 tegra_powergate_mc_flush_done(dev->powergate_ids[1]);
82                 tegra_periph_reset_deassert(dev->clk[1]);
83                 tegra_powergate_mc_enable(dev->powergate_ids[1]);
84         }
85 }
86
87 void nvhost_module_reset(struct nvhost_device *dev)
88 {
89         dev_dbg(&dev->dev,
90                 "%s: asserting %s module reset (id %d, id2 %d)\n",
91                 __func__, dev->name,
92                 dev->powergate_ids[0], dev->powergate_ids[1]);
93
94         mutex_lock(&dev->lock);
95         do_module_reset_locked(dev);
96         mutex_unlock(&dev->lock);
97
98         dev_dbg(&dev->dev, "%s: module %s out of reset\n",
99                 __func__, dev->name);
100 }
101
102 static void to_state_clockgated_locked(struct nvhost_device *dev)
103 {
104         if (dev->powerstate == NVHOST_POWER_STATE_RUNNING) {
105                 int i;
106                 for (i = 0; i < dev->num_clks; i++)
107                         clk_disable(dev->clk[i]);
108                 if (dev->dev.parent)
109                         nvhost_module_idle(to_nvhost_device(dev->dev.parent));
110         } else if (dev->powerstate == NVHOST_POWER_STATE_POWERGATED
111                         && dev->can_powergate) {
112                 do_unpowergate_locked(dev->powergate_ids[0]);
113                 do_unpowergate_locked(dev->powergate_ids[1]);
114
115                 if (dev->powerup_reset)
116                         do_module_reset_locked(dev);
117         }
118         dev->powerstate = NVHOST_POWER_STATE_CLOCKGATED;
119 }
120
121 static void to_state_running_locked(struct nvhost_device *dev)
122 {
123         struct nvhost_driver *drv = to_nvhost_driver(dev->dev.driver);
124         int prev_state = dev->powerstate;
125
126         if (dev->powerstate == NVHOST_POWER_STATE_POWERGATED)
127                 to_state_clockgated_locked(dev);
128
129         if (dev->powerstate == NVHOST_POWER_STATE_CLOCKGATED) {
130                 int i;
131
132                 if (dev->dev.parent)
133                         nvhost_module_busy(to_nvhost_device(dev->dev.parent));
134
135                 for (i = 0; i < dev->num_clks; i++) {
136                         int err = clk_enable(dev->clk[i]);
137                         if (err) {
138                                 dev_err(&dev->dev, "Cannot turn on clock %s",
139                                         dev->clocks[i].name);
140                                 return;
141                         }
142                 }
143
144                 if (prev_state == NVHOST_POWER_STATE_POWERGATED
145                                 && drv->finalize_poweron)
146                         drv->finalize_poweron(dev);
147         }
148         dev->powerstate = NVHOST_POWER_STATE_RUNNING;
149 }
150
151 /* This gets called from powergate_handler() and from module suspend.
152  * Module suspend is done for all modules, runtime power gating only
153  * for modules with can_powergate set.
154  */
155 static int to_state_powergated_locked(struct nvhost_device *dev)
156 {
157         int err = 0;
158         struct nvhost_driver *drv = to_nvhost_driver(dev->dev.driver);
159
160         if (drv->prepare_poweroff
161                         && dev->powerstate != NVHOST_POWER_STATE_POWERGATED) {
162                 /* Clock needs to be on in prepare_poweroff */
163                 to_state_running_locked(dev);
164                 err = drv->prepare_poweroff(dev);
165                 if (err)
166                         return err;
167         }
168
169         if (dev->powerstate == NVHOST_POWER_STATE_RUNNING)
170                 to_state_clockgated_locked(dev);
171
172         if (dev->can_powergate) {
173                 do_powergate_locked(dev->powergate_ids[0]);
174                 do_powergate_locked(dev->powergate_ids[1]);
175         }
176
177         dev->powerstate = NVHOST_POWER_STATE_POWERGATED;
178         return 0;
179 }
180
181 static void schedule_powergating_locked(struct nvhost_device *dev)
182 {
183         if (dev->can_powergate)
184                 schedule_delayed_work(&dev->powerstate_down,
185                                 msecs_to_jiffies(dev->powergate_delay));
186 }
187
188 static void schedule_clockgating_locked(struct nvhost_device *dev)
189 {
190         schedule_delayed_work(&dev->powerstate_down,
191                         msecs_to_jiffies(dev->clockgate_delay));
192 }
193
194 void nvhost_module_busy(struct nvhost_device *dev)
195 {
196         struct nvhost_driver *drv = to_nvhost_driver(dev->dev.driver);
197
198         if (drv->busy)
199                 drv->busy(dev);
200
201         mutex_lock(&dev->lock);
202         cancel_delayed_work(&dev->powerstate_down);
203
204         dev->refcount++;
205         if (dev->refcount > 0 && !nvhost_module_powered(dev))
206                 to_state_running_locked(dev);
207         mutex_unlock(&dev->lock);
208 }
209
210 static void powerstate_down_handler(struct work_struct *work)
211 {
212         struct nvhost_device *dev;
213
214         dev = container_of(to_delayed_work(work),
215                         struct nvhost_device,
216                         powerstate_down);
217
218         mutex_lock(&dev->lock);
219         if (dev->refcount == 0) {
220                 switch (dev->powerstate) {
221                 case NVHOST_POWER_STATE_RUNNING:
222                         to_state_clockgated_locked(dev);
223                         schedule_powergating_locked(dev);
224                         break;
225                 case NVHOST_POWER_STATE_CLOCKGATED:
226                         if (to_state_powergated_locked(dev))
227                                 schedule_powergating_locked(dev);
228                         break;
229                 default:
230                         break;
231                 }
232         }
233         mutex_unlock(&dev->lock);
234 }
235
236 void nvhost_module_idle_mult(struct nvhost_device *dev, int refs)
237 {
238         struct nvhost_driver *drv = to_nvhost_driver(dev->dev.driver);
239         bool kick = false;
240
241         mutex_lock(&dev->lock);
242         dev->refcount -= refs;
243         if (dev->refcount == 0) {
244                 if (nvhost_module_powered(dev))
245                         schedule_clockgating_locked(dev);
246                 kick = true;
247         }
248         mutex_unlock(&dev->lock);
249
250         if (kick) {
251                 wake_up(&dev->idle_wq);
252
253                 if (drv->idle)
254                         drv->idle(dev);
255         }
256 }
257
258 int nvhost_module_get_rate(struct nvhost_device *dev, unsigned long *rate,
259                 int index)
260 {
261         struct clk *c;
262
263         c = dev->clk[index];
264         if (IS_ERR_OR_NULL(c))
265                 return -EINVAL;
266
267         /* Need to enable client to get correct rate */
268         nvhost_module_busy(dev);
269         *rate = clk_get_rate(c);
270         nvhost_module_idle(dev);
271         return 0;
272
273 }
274
275 static int nvhost_module_update_rate(struct nvhost_device *dev, int index)
276 {
277         unsigned long rate = 0;
278         struct nvhost_module_client *m;
279
280         if (!dev->clk[index])
281                 return -EINVAL;
282
283         list_for_each_entry(m, &dev->client_list, node) {
284                 rate = max(m->rate[index], rate);
285         }
286         if (!rate)
287                 rate = clk_round_rate(dev->clk[index],
288                                 dev->clocks[index].default_rate);
289
290         return clk_set_rate(dev->clk[index], rate);
291 }
292
293 int nvhost_module_set_rate(struct nvhost_device *dev, void *priv,
294                 unsigned long rate, int index)
295 {
296         struct nvhost_module_client *m;
297         int i, ret = 0;
298
299         mutex_lock(&client_list_lock);
300         list_for_each_entry(m, &dev->client_list, node) {
301                 if (m->priv == priv) {
302                         for (i = 0; i < dev->num_clks; i++)
303                                 m->rate[i] = clk_round_rate(dev->clk[i], rate);
304                         break;
305                 }
306         }
307
308         for (i = 0; i < dev->num_clks; i++) {
309                 ret = nvhost_module_update_rate(dev, i);
310                 if (ret < 0)
311                         break;
312         }
313         mutex_unlock(&client_list_lock);
314         return ret;
315
316 }
317
318 int nvhost_module_add_client(struct nvhost_device *dev, void *priv)
319 {
320         int i;
321         unsigned long rate;
322         struct nvhost_module_client *client;
323
324         client = kzalloc(sizeof(*client), GFP_KERNEL);
325         if (!client)
326                 return -ENOMEM;
327
328         INIT_LIST_HEAD(&client->node);
329         client->priv = priv;
330
331         for (i = 0; i < dev->num_clks; i++) {
332                 rate = clk_round_rate(dev->clk[i],
333                                 dev->clocks[i].default_rate);
334                 client->rate[i] = rate;
335         }
336         mutex_lock(&client_list_lock);
337         list_add_tail(&client->node, &dev->client_list);
338         mutex_unlock(&client_list_lock);
339         return 0;
340 }
341
342 void nvhost_module_remove_client(struct nvhost_device *dev, void *priv)
343 {
344         int i;
345         struct nvhost_module_client *m;
346
347         mutex_lock(&client_list_lock);
348         list_for_each_entry(m, &dev->client_list, node) {
349                 if (priv == m->priv) {
350                         list_del(&m->node);
351                         break;
352                 }
353         }
354         if (m) {
355                 kfree(m);
356                 for (i = 0; i < dev->num_clks; i++)
357                         nvhost_module_update_rate(dev, i);
358         }
359         mutex_unlock(&client_list_lock);
360 }
361
362 static ssize_t refcount_show(struct kobject *kobj,
363         struct kobj_attribute *attr, char *buf)
364 {
365         int ret;
366         struct nvhost_device_power_attr *power_attribute =
367                 container_of(attr, struct nvhost_device_power_attr, \
368                         power_attr[NVHOST_POWER_SYSFS_ATTRIB_REFCOUNT]);
369         struct nvhost_device *dev = power_attribute->ndev;
370
371         mutex_lock(&dev->lock);
372         ret = sprintf(buf, "%d\n", dev->refcount);
373         mutex_unlock(&dev->lock);
374
375         return ret;
376 }
377
378 static ssize_t powergate_delay_store(struct kobject *kobj,
379         struct kobj_attribute *attr, const char *buf, size_t count)
380 {
381         int powergate_delay = 0, ret = 0;
382         struct nvhost_device_power_attr *power_attribute =
383                 container_of(attr, struct nvhost_device_power_attr, \
384                         power_attr[NVHOST_POWER_SYSFS_ATTRIB_POWERGATE_DELAY]);
385         struct nvhost_device *dev = power_attribute->ndev;
386
387         if (!dev->can_powergate) {
388                 dev_info(&dev->dev, "does not support power-gating\n");
389                 return count;
390         }
391
392         mutex_lock(&dev->lock);
393         ret = sscanf(buf, "%d", &powergate_delay);
394         if (ret == 1 && powergate_delay >= 0)
395                 dev->powergate_delay = powergate_delay;
396         else
397                 dev_err(&dev->dev, "Invalid powergate delay\n");
398         mutex_unlock(&dev->lock);
399
400         return count;
401 }
402
403 static ssize_t powergate_delay_show(struct kobject *kobj,
404         struct kobj_attribute *attr, char *buf)
405 {
406         int ret;
407         struct nvhost_device_power_attr *power_attribute =
408                 container_of(attr, struct nvhost_device_power_attr, \
409                         power_attr[NVHOST_POWER_SYSFS_ATTRIB_POWERGATE_DELAY]);
410         struct nvhost_device *dev = power_attribute->ndev;
411
412         mutex_lock(&dev->lock);
413         ret = sprintf(buf, "%d\n", dev->powergate_delay);
414         mutex_unlock(&dev->lock);
415
416         return ret;
417 }
418
419 static ssize_t clockgate_delay_store(struct kobject *kobj,
420         struct kobj_attribute *attr, const char *buf, size_t count)
421 {
422         int clockgate_delay = 0, ret = 0;
423         struct nvhost_device_power_attr *power_attribute =
424                 container_of(attr, struct nvhost_device_power_attr, \
425                         power_attr[NVHOST_POWER_SYSFS_ATTRIB_CLOCKGATE_DELAY]);
426         struct nvhost_device *dev = power_attribute->ndev;
427
428         mutex_lock(&dev->lock);
429         ret = sscanf(buf, "%d", &clockgate_delay);
430         if (ret == 1 && clockgate_delay >= 0)
431                 dev->clockgate_delay = clockgate_delay;
432         else
433                 dev_err(&dev->dev, "Invalid clockgate delay\n");
434         mutex_unlock(&dev->lock);
435
436         return count;
437 }
438
439 static ssize_t clockgate_delay_show(struct kobject *kobj,
440         struct kobj_attribute *attr, char *buf)
441 {
442         int ret;
443         struct nvhost_device_power_attr *power_attribute =
444                 container_of(attr, struct nvhost_device_power_attr, \
445                         power_attr[NVHOST_POWER_SYSFS_ATTRIB_CLOCKGATE_DELAY]);
446         struct nvhost_device *dev = power_attribute->ndev;
447
448         mutex_lock(&dev->lock);
449         ret = sprintf(buf, "%d\n", dev->clockgate_delay);
450         mutex_unlock(&dev->lock);
451
452         return ret;
453 }
454
455 int nvhost_module_init(struct nvhost_device *dev)
456 {
457         int i = 0, err = 0;
458         struct kobj_attribute *attr = NULL;
459
460         /* initialize clocks to known state */
461         INIT_LIST_HEAD(&dev->client_list);
462         while (dev->clocks[i].name && i < NVHOST_MODULE_MAX_CLOCKS) {
463                 char devname[MAX_DEVID_LENGTH];
464                 long rate = dev->clocks[i].default_rate;
465                 struct clk *c;
466
467                 snprintf(devname, MAX_DEVID_LENGTH, "tegra_%s", dev->name);
468                 c = clk_get_sys(devname, dev->clocks[i].name);
469                 if (IS_ERR_OR_NULL(c)) {
470                         dev_err(&dev->dev, "Cannot get clock %s\n",
471                                         dev->clocks[i].name);
472                         continue;
473                 }
474
475                 rate = clk_round_rate(c, rate);
476                 clk_enable(c);
477                 clk_set_rate(c, rate);
478                 clk_disable(c);
479                 dev->clk[i] = c;
480                 i++;
481         }
482         dev->num_clks = i;
483
484         mutex_init(&dev->lock);
485         init_waitqueue_head(&dev->idle_wq);
486         INIT_DELAYED_WORK(&dev->powerstate_down, powerstate_down_handler);
487
488         /* power gate units that we can power gate */
489         if (dev->can_powergate) {
490                 do_powergate_locked(dev->powergate_ids[0]);
491                 do_powergate_locked(dev->powergate_ids[1]);
492                 dev->powerstate = NVHOST_POWER_STATE_POWERGATED;
493         } else {
494                 do_unpowergate_locked(dev->powergate_ids[0]);
495                 do_unpowergate_locked(dev->powergate_ids[1]);
496                 dev->powerstate = NVHOST_POWER_STATE_CLOCKGATED;
497         }
498
499         /* Init the power sysfs attributes for this device */
500         dev->power_attrib = kzalloc(sizeof(struct nvhost_device_power_attr),
501                 GFP_KERNEL);
502         if (!dev->power_attrib) {
503                 dev_err(&dev->dev, "Unable to allocate sysfs attributes\n");
504                 return -ENOMEM;
505         }
506         dev->power_attrib->ndev = dev;
507
508         dev->power_kobj = kobject_create_and_add("acm", &dev->dev.kobj);
509         if (!dev->power_kobj) {
510                 dev_err(&dev->dev, "Could not add dir 'power'\n");
511                 err = -EIO;
512                 goto fail_attrib_alloc;
513         }
514
515         attr = &dev->power_attrib->power_attr[NVHOST_POWER_SYSFS_ATTRIB_CLOCKGATE_DELAY];
516         attr->attr.name = "clockgate_delay";
517         attr->attr.mode = S_IWUSR | S_IRUGO;
518         attr->show = clockgate_delay_show;
519         attr->store = clockgate_delay_store;
520         if (sysfs_create_file(dev->power_kobj, &attr->attr)) {
521                 dev_err(&dev->dev, "Could not create sysfs attribute clockgate_delay\n");
522                 err = -EIO;
523                 goto fail_clockdelay;
524         }
525
526         attr = &dev->power_attrib->power_attr[NVHOST_POWER_SYSFS_ATTRIB_POWERGATE_DELAY];
527         attr->attr.name = "powergate_delay";
528         attr->attr.mode = S_IWUSR | S_IRUGO;
529         attr->show = powergate_delay_show;
530         attr->store = powergate_delay_store;
531         if (sysfs_create_file(dev->power_kobj, &attr->attr)) {
532                 dev_err(&dev->dev, "Could not create sysfs attribute powergate_delay\n");
533                 err = -EIO;
534                 goto fail_powergatedelay;
535         }
536
537         attr = &dev->power_attrib->power_attr[NVHOST_POWER_SYSFS_ATTRIB_REFCOUNT];
538         attr->attr.name = "refcount";
539         attr->attr.mode = S_IRUGO;
540         attr->show = refcount_show;
541         if (sysfs_create_file(dev->power_kobj, &attr->attr)) {
542                 dev_err(&dev->dev, "Could not create sysfs attribute refcount\n");
543                 err = -EIO;
544                 goto fail_refcount;
545         }
546
547         return 0;
548
549 fail_refcount:
550         attr = &dev->power_attrib->power_attr[NVHOST_POWER_SYSFS_ATTRIB_POWERGATE_DELAY];
551         sysfs_remove_file(dev->power_kobj, &attr->attr);
552
553 fail_powergatedelay:
554         attr = &dev->power_attrib->power_attr[NVHOST_POWER_SYSFS_ATTRIB_CLOCKGATE_DELAY];
555         sysfs_remove_file(dev->power_kobj, &attr->attr);
556
557 fail_clockdelay:
558         kobject_put(dev->power_kobj);
559
560 fail_attrib_alloc:
561         kfree(dev->power_attrib);
562
563         return err;
564 }
565
566 static int is_module_idle(struct nvhost_device *dev)
567 {
568         int count;
569         mutex_lock(&dev->lock);
570         count = dev->refcount;
571         mutex_unlock(&dev->lock);
572         return (count == 0);
573 }
574
575 int nvhost_module_suspend(struct nvhost_device *dev)
576 {
577         int ret;
578         struct nvhost_driver *drv = to_nvhost_driver(dev->dev.driver);
579
580         ret = wait_event_timeout(dev->idle_wq, is_module_idle(dev),
581                         ACM_SUSPEND_WAIT_FOR_IDLE_TIMEOUT);
582         if (ret == 0) {
583                 dev_info(&dev->dev, "%s prevented suspend\n",
584                                 dev->name);
585                 return -EBUSY;
586         }
587
588         mutex_lock(&dev->lock);
589         cancel_delayed_work(&dev->powerstate_down);
590         to_state_powergated_locked(dev);
591         mutex_unlock(&dev->lock);
592
593         if (drv->suspend_ndev)
594                 drv->suspend_ndev(dev);
595
596         return 0;
597 }
598
599 void nvhost_module_deinit(struct nvhost_device *dev)
600 {
601         int i;
602         struct nvhost_driver *drv = to_nvhost_driver(dev->dev.driver);
603
604         if (drv->deinit)
605                 drv->deinit(dev);
606
607         nvhost_module_suspend(dev);
608         for (i = 0; i < dev->num_clks; i++)
609                 clk_put(dev->clk[i]);
610         dev->powerstate = NVHOST_POWER_STATE_DEINIT;
611 }
612