5bde55ad2ff524b29a38765ebcf94c559628ac72
[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         struct nvhost_driver *drv = to_nvhost_driver(dev->dev.driver);
105
106         if (dev->powerstate == NVHOST_POWER_STATE_RUNNING) {
107                 int i, err;
108                 if (drv->prepare_clockoff) {
109                         err = drv->prepare_clockoff(dev);
110                         if (err) {
111                                 dev_err(&dev->dev, "error clock gating");
112                                 return;
113                         }
114                 }
115                 for (i = 0; i < dev->num_clks; i++)
116                         clk_disable(dev->clk[i]);
117                 if (dev->dev.parent)
118                         nvhost_module_idle(to_nvhost_device(dev->dev.parent));
119         } else if (dev->powerstate == NVHOST_POWER_STATE_POWERGATED
120                         && dev->can_powergate) {
121                 do_unpowergate_locked(dev->powergate_ids[0]);
122                 do_unpowergate_locked(dev->powergate_ids[1]);
123
124                 if (dev->powerup_reset)
125                         do_module_reset_locked(dev);
126         }
127         dev->powerstate = NVHOST_POWER_STATE_CLOCKGATED;
128 }
129
130 static void to_state_running_locked(struct nvhost_device *dev)
131 {
132         struct nvhost_driver *drv = to_nvhost_driver(dev->dev.driver);
133         int prev_state = dev->powerstate;
134
135         if (dev->powerstate == NVHOST_POWER_STATE_POWERGATED)
136                 to_state_clockgated_locked(dev);
137
138         if (dev->powerstate == NVHOST_POWER_STATE_CLOCKGATED) {
139                 int i;
140
141                 if (dev->dev.parent)
142                         nvhost_module_busy(to_nvhost_device(dev->dev.parent));
143
144                 for (i = 0; i < dev->num_clks; i++) {
145                         int err = clk_enable(dev->clk[i]);
146                         if (err) {
147                                 dev_err(&dev->dev, "Cannot turn on clock %s",
148                                         dev->clocks[i].name);
149                                 return;
150                         }
151                 }
152
153                 /* Invoke callback after enabling clock. This is used for
154                  * re-enabling host1x interrupts. */
155                 if (prev_state == NVHOST_POWER_STATE_CLOCKGATED
156                                 && drv->finalize_clockon)
157                         drv->finalize_clockon(dev);
158
159                 /* Invoke callback after power un-gating. This is used for
160                  * restoring context. */
161                 if (prev_state == NVHOST_POWER_STATE_POWERGATED
162                                 && drv->finalize_poweron)
163                         drv->finalize_poweron(dev);
164         }
165         dev->powerstate = NVHOST_POWER_STATE_RUNNING;
166 }
167
168 /* This gets called from powergate_handler() and from module suspend.
169  * Module suspend is done for all modules, runtime power gating only
170  * for modules with can_powergate set.
171  */
172 static int to_state_powergated_locked(struct nvhost_device *dev)
173 {
174         int err = 0;
175         struct nvhost_driver *drv = to_nvhost_driver(dev->dev.driver);
176
177         if (drv->prepare_poweroff
178                         && dev->powerstate != NVHOST_POWER_STATE_POWERGATED) {
179                 /* Clock needs to be on in prepare_poweroff */
180                 to_state_running_locked(dev);
181                 err = drv->prepare_poweroff(dev);
182                 if (err)
183                         return err;
184         }
185
186         if (dev->powerstate == NVHOST_POWER_STATE_RUNNING)
187                 to_state_clockgated_locked(dev);
188
189         if (dev->can_powergate) {
190                 do_powergate_locked(dev->powergate_ids[0]);
191                 do_powergate_locked(dev->powergate_ids[1]);
192         }
193
194         dev->powerstate = NVHOST_POWER_STATE_POWERGATED;
195         return 0;
196 }
197
198 static void schedule_powergating_locked(struct nvhost_device *dev)
199 {
200         if (dev->can_powergate)
201                 schedule_delayed_work(&dev->powerstate_down,
202                                 msecs_to_jiffies(dev->powergate_delay));
203 }
204
205 static void schedule_clockgating_locked(struct nvhost_device *dev)
206 {
207         schedule_delayed_work(&dev->powerstate_down,
208                         msecs_to_jiffies(dev->clockgate_delay));
209 }
210
211 void nvhost_module_busy(struct nvhost_device *dev)
212 {
213         struct nvhost_driver *drv = to_nvhost_driver(dev->dev.driver);
214
215         if (drv->busy)
216                 drv->busy(dev);
217
218         mutex_lock(&dev->lock);
219         cancel_delayed_work(&dev->powerstate_down);
220
221         dev->refcount++;
222         if (dev->refcount > 0 && !nvhost_module_powered(dev))
223                 to_state_running_locked(dev);
224         mutex_unlock(&dev->lock);
225 }
226
227 static void powerstate_down_handler(struct work_struct *work)
228 {
229         struct nvhost_device *dev;
230
231         dev = container_of(to_delayed_work(work),
232                         struct nvhost_device,
233                         powerstate_down);
234
235         mutex_lock(&dev->lock);
236         if (dev->refcount == 0) {
237                 switch (dev->powerstate) {
238                 case NVHOST_POWER_STATE_RUNNING:
239                         to_state_clockgated_locked(dev);
240                         schedule_powergating_locked(dev);
241                         break;
242                 case NVHOST_POWER_STATE_CLOCKGATED:
243                         if (to_state_powergated_locked(dev))
244                                 schedule_powergating_locked(dev);
245                         break;
246                 default:
247                         break;
248                 }
249         }
250         mutex_unlock(&dev->lock);
251 }
252
253 void nvhost_module_idle_mult(struct nvhost_device *dev, int refs)
254 {
255         struct nvhost_driver *drv = to_nvhost_driver(dev->dev.driver);
256         bool kick = false;
257
258         mutex_lock(&dev->lock);
259         dev->refcount -= refs;
260         if (dev->refcount == 0) {
261                 if (nvhost_module_powered(dev))
262                         schedule_clockgating_locked(dev);
263                 kick = true;
264         }
265         mutex_unlock(&dev->lock);
266
267         if (kick) {
268                 wake_up(&dev->idle_wq);
269
270                 if (drv->idle)
271                         drv->idle(dev);
272         }
273 }
274
275 int nvhost_module_get_rate(struct nvhost_device *dev, unsigned long *rate,
276                 int index)
277 {
278         struct clk *c;
279
280         c = dev->clk[index];
281         if (IS_ERR_OR_NULL(c))
282                 return -EINVAL;
283
284         /* Need to enable client to get correct rate */
285         nvhost_module_busy(dev);
286         *rate = clk_get_rate(c);
287         nvhost_module_idle(dev);
288         return 0;
289
290 }
291
292 static int nvhost_module_update_rate(struct nvhost_device *dev, int index)
293 {
294         unsigned long rate = 0;
295         struct nvhost_module_client *m;
296
297         if (!dev->clk[index])
298                 return -EINVAL;
299
300         list_for_each_entry(m, &dev->client_list, node) {
301                 rate = max(m->rate[index], rate);
302         }
303         if (!rate)
304                 rate = clk_round_rate(dev->clk[index],
305                                 dev->clocks[index].default_rate);
306
307         return clk_set_rate(dev->clk[index], rate);
308 }
309
310 int nvhost_module_set_rate(struct nvhost_device *dev, void *priv,
311                 unsigned long rate, int index)
312 {
313         struct nvhost_module_client *m;
314         int i, ret = 0;
315
316         mutex_lock(&client_list_lock);
317         list_for_each_entry(m, &dev->client_list, node) {
318                 if (m->priv == priv) {
319                         for (i = 0; i < dev->num_clks; i++)
320                                 m->rate[i] = clk_round_rate(dev->clk[i], rate);
321                         break;
322                 }
323         }
324
325         for (i = 0; i < dev->num_clks; i++) {
326                 ret = nvhost_module_update_rate(dev, i);
327                 if (ret < 0)
328                         break;
329         }
330         mutex_unlock(&client_list_lock);
331         return ret;
332
333 }
334
335 int nvhost_module_add_client(struct nvhost_device *dev, void *priv)
336 {
337         int i;
338         unsigned long rate;
339         struct nvhost_module_client *client;
340
341         client = kzalloc(sizeof(*client), GFP_KERNEL);
342         if (!client)
343                 return -ENOMEM;
344
345         INIT_LIST_HEAD(&client->node);
346         client->priv = priv;
347
348         for (i = 0; i < dev->num_clks; i++) {
349                 rate = clk_round_rate(dev->clk[i],
350                                 dev->clocks[i].default_rate);
351                 client->rate[i] = rate;
352         }
353         mutex_lock(&client_list_lock);
354         list_add_tail(&client->node, &dev->client_list);
355         mutex_unlock(&client_list_lock);
356         return 0;
357 }
358
359 void nvhost_module_remove_client(struct nvhost_device *dev, void *priv)
360 {
361         int i;
362         struct nvhost_module_client *m;
363         int found = 0;
364
365         mutex_lock(&client_list_lock);
366         list_for_each_entry(m, &dev->client_list, node) {
367                 if (priv == m->priv) {
368                         list_del(&m->node);
369                         found = 1;
370                         break;
371                 }
372         }
373         if (found) {
374                 kfree(m);
375                 for (i = 0; i < dev->num_clks; i++)
376                         nvhost_module_update_rate(dev, i);
377         }
378         mutex_unlock(&client_list_lock);
379 }
380
381 static ssize_t refcount_show(struct kobject *kobj,
382         struct kobj_attribute *attr, char *buf)
383 {
384         int ret;
385         struct nvhost_device_power_attr *power_attribute =
386                 container_of(attr, struct nvhost_device_power_attr, \
387                         power_attr[NVHOST_POWER_SYSFS_ATTRIB_REFCOUNT]);
388         struct nvhost_device *dev = power_attribute->ndev;
389
390         mutex_lock(&dev->lock);
391         ret = sprintf(buf, "%d\n", dev->refcount);
392         mutex_unlock(&dev->lock);
393
394         return ret;
395 }
396
397 static ssize_t powergate_delay_store(struct kobject *kobj,
398         struct kobj_attribute *attr, const char *buf, size_t count)
399 {
400         int powergate_delay = 0, ret = 0;
401         struct nvhost_device_power_attr *power_attribute =
402                 container_of(attr, struct nvhost_device_power_attr, \
403                         power_attr[NVHOST_POWER_SYSFS_ATTRIB_POWERGATE_DELAY]);
404         struct nvhost_device *dev = power_attribute->ndev;
405
406         if (!dev->can_powergate) {
407                 dev_info(&dev->dev, "does not support power-gating\n");
408                 return count;
409         }
410
411         mutex_lock(&dev->lock);
412         ret = sscanf(buf, "%d", &powergate_delay);
413         if (ret == 1 && powergate_delay >= 0)
414                 dev->powergate_delay = powergate_delay;
415         else
416                 dev_err(&dev->dev, "Invalid powergate delay\n");
417         mutex_unlock(&dev->lock);
418
419         return count;
420 }
421
422 static ssize_t powergate_delay_show(struct kobject *kobj,
423         struct kobj_attribute *attr, char *buf)
424 {
425         int ret;
426         struct nvhost_device_power_attr *power_attribute =
427                 container_of(attr, struct nvhost_device_power_attr, \
428                         power_attr[NVHOST_POWER_SYSFS_ATTRIB_POWERGATE_DELAY]);
429         struct nvhost_device *dev = power_attribute->ndev;
430
431         mutex_lock(&dev->lock);
432         ret = sprintf(buf, "%d\n", dev->powergate_delay);
433         mutex_unlock(&dev->lock);
434
435         return ret;
436 }
437
438 static ssize_t clockgate_delay_store(struct kobject *kobj,
439         struct kobj_attribute *attr, const char *buf, size_t count)
440 {
441         int clockgate_delay = 0, ret = 0;
442         struct nvhost_device_power_attr *power_attribute =
443                 container_of(attr, struct nvhost_device_power_attr, \
444                         power_attr[NVHOST_POWER_SYSFS_ATTRIB_CLOCKGATE_DELAY]);
445         struct nvhost_device *dev = power_attribute->ndev;
446
447         mutex_lock(&dev->lock);
448         ret = sscanf(buf, "%d", &clockgate_delay);
449         if (ret == 1 && clockgate_delay >= 0)
450                 dev->clockgate_delay = clockgate_delay;
451         else
452                 dev_err(&dev->dev, "Invalid clockgate delay\n");
453         mutex_unlock(&dev->lock);
454
455         return count;
456 }
457
458 static ssize_t clockgate_delay_show(struct kobject *kobj,
459         struct kobj_attribute *attr, char *buf)
460 {
461         int ret;
462         struct nvhost_device_power_attr *power_attribute =
463                 container_of(attr, struct nvhost_device_power_attr, \
464                         power_attr[NVHOST_POWER_SYSFS_ATTRIB_CLOCKGATE_DELAY]);
465         struct nvhost_device *dev = power_attribute->ndev;
466
467         mutex_lock(&dev->lock);
468         ret = sprintf(buf, "%d\n", dev->clockgate_delay);
469         mutex_unlock(&dev->lock);
470
471         return ret;
472 }
473
474 int nvhost_module_init(struct nvhost_device *dev)
475 {
476         int i = 0, err = 0;
477         struct kobj_attribute *attr = NULL;
478
479         /* initialize clocks to known state */
480         INIT_LIST_HEAD(&dev->client_list);
481         while (dev->clocks[i].name && i < NVHOST_MODULE_MAX_CLOCKS) {
482                 char devname[MAX_DEVID_LENGTH];
483                 long rate = dev->clocks[i].default_rate;
484                 struct clk *c;
485
486                 snprintf(devname, MAX_DEVID_LENGTH, "tegra_%s", dev->name);
487                 c = clk_get_sys(devname, dev->clocks[i].name);
488                 if (IS_ERR_OR_NULL(c)) {
489                         dev_err(&dev->dev, "Cannot get clock %s\n",
490                                         dev->clocks[i].name);
491                         continue;
492                 }
493
494                 rate = clk_round_rate(c, rate);
495                 clk_enable(c);
496                 clk_set_rate(c, rate);
497                 clk_disable(c);
498                 dev->clk[i] = c;
499                 i++;
500         }
501         dev->num_clks = i;
502
503         mutex_init(&dev->lock);
504         init_waitqueue_head(&dev->idle_wq);
505         INIT_DELAYED_WORK(&dev->powerstate_down, powerstate_down_handler);
506
507         /* power gate units that we can power gate */
508         if (dev->can_powergate) {
509                 do_powergate_locked(dev->powergate_ids[0]);
510                 do_powergate_locked(dev->powergate_ids[1]);
511                 dev->powerstate = NVHOST_POWER_STATE_POWERGATED;
512         } else {
513                 do_unpowergate_locked(dev->powergate_ids[0]);
514                 do_unpowergate_locked(dev->powergate_ids[1]);
515                 dev->powerstate = NVHOST_POWER_STATE_CLOCKGATED;
516         }
517
518         /* Init the power sysfs attributes for this device */
519         dev->power_attrib = kzalloc(sizeof(struct nvhost_device_power_attr),
520                 GFP_KERNEL);
521         if (!dev->power_attrib) {
522                 dev_err(&dev->dev, "Unable to allocate sysfs attributes\n");
523                 return -ENOMEM;
524         }
525         dev->power_attrib->ndev = dev;
526
527         dev->power_kobj = kobject_create_and_add("acm", &dev->dev.kobj);
528         if (!dev->power_kobj) {
529                 dev_err(&dev->dev, "Could not add dir 'power'\n");
530                 err = -EIO;
531                 goto fail_attrib_alloc;
532         }
533
534         attr = &dev->power_attrib->power_attr[NVHOST_POWER_SYSFS_ATTRIB_CLOCKGATE_DELAY];
535         attr->attr.name = "clockgate_delay";
536         attr->attr.mode = S_IWUSR | S_IRUGO;
537         attr->show = clockgate_delay_show;
538         attr->store = clockgate_delay_store;
539         if (sysfs_create_file(dev->power_kobj, &attr->attr)) {
540                 dev_err(&dev->dev, "Could not create sysfs attribute clockgate_delay\n");
541                 err = -EIO;
542                 goto fail_clockdelay;
543         }
544
545         attr = &dev->power_attrib->power_attr[NVHOST_POWER_SYSFS_ATTRIB_POWERGATE_DELAY];
546         attr->attr.name = "powergate_delay";
547         attr->attr.mode = S_IWUSR | S_IRUGO;
548         attr->show = powergate_delay_show;
549         attr->store = powergate_delay_store;
550         if (sysfs_create_file(dev->power_kobj, &attr->attr)) {
551                 dev_err(&dev->dev, "Could not create sysfs attribute powergate_delay\n");
552                 err = -EIO;
553                 goto fail_powergatedelay;
554         }
555
556         attr = &dev->power_attrib->power_attr[NVHOST_POWER_SYSFS_ATTRIB_REFCOUNT];
557         attr->attr.name = "refcount";
558         attr->attr.mode = S_IRUGO;
559         attr->show = refcount_show;
560         if (sysfs_create_file(dev->power_kobj, &attr->attr)) {
561                 dev_err(&dev->dev, "Could not create sysfs attribute refcount\n");
562                 err = -EIO;
563                 goto fail_refcount;
564         }
565
566         return 0;
567
568 fail_refcount:
569         attr = &dev->power_attrib->power_attr[NVHOST_POWER_SYSFS_ATTRIB_POWERGATE_DELAY];
570         sysfs_remove_file(dev->power_kobj, &attr->attr);
571
572 fail_powergatedelay:
573         attr = &dev->power_attrib->power_attr[NVHOST_POWER_SYSFS_ATTRIB_CLOCKGATE_DELAY];
574         sysfs_remove_file(dev->power_kobj, &attr->attr);
575
576 fail_clockdelay:
577         kobject_put(dev->power_kobj);
578
579 fail_attrib_alloc:
580         kfree(dev->power_attrib);
581
582         return err;
583 }
584
585 static int is_module_idle(struct nvhost_device *dev)
586 {
587         int count;
588         mutex_lock(&dev->lock);
589         count = dev->refcount;
590         mutex_unlock(&dev->lock);
591         return (count == 0);
592 }
593
594 int nvhost_module_suspend(struct nvhost_device *dev)
595 {
596         int ret;
597         struct nvhost_driver *drv = to_nvhost_driver(dev->dev.driver);
598
599         ret = wait_event_timeout(dev->idle_wq, is_module_idle(dev),
600                         ACM_SUSPEND_WAIT_FOR_IDLE_TIMEOUT);
601         if (ret == 0) {
602                 dev_info(&dev->dev, "%s prevented suspend\n",
603                                 dev->name);
604                 return -EBUSY;
605         }
606
607         mutex_lock(&dev->lock);
608         cancel_delayed_work(&dev->powerstate_down);
609         to_state_powergated_locked(dev);
610         mutex_unlock(&dev->lock);
611
612         if (drv->suspend_ndev)
613                 drv->suspend_ndev(dev);
614
615         return 0;
616 }
617
618 void nvhost_module_deinit(struct nvhost_device *dev)
619 {
620         int i;
621         struct nvhost_driver *drv = to_nvhost_driver(dev->dev.driver);
622
623         if (drv->deinit)
624                 drv->deinit(dev);
625
626         nvhost_module_suspend(dev);
627         for (i = 0; i < dev->num_clks; i++)
628                 clk_put(dev->clk[i]);
629         dev->powerstate = NVHOST_POWER_STATE_DEINIT;
630 }
631
632 /* public host1x power management APIs */
633 bool nvhost_module_powered_ext(struct nvhost_device *dev)
634 {
635         return nvhost_module_powered(dev);
636 }
637
638 void nvhost_module_busy_ext(struct nvhost_device *dev)
639 {
640         nvhost_module_busy(dev);
641 }
642
643 void nvhost_module_idle_ext(struct nvhost_device *dev)
644 {
645         nvhost_module_idle(dev);
646 }