dfd84328307baea0db0facfc41d9758be657e47b
[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.
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
237 void nvhost_module_idle_mult(struct nvhost_device *dev, int refs)
238 {
239         struct nvhost_driver *drv = to_nvhost_driver(dev->dev.driver);
240         bool kick = false;
241
242         mutex_lock(&dev->lock);
243         dev->refcount -= refs;
244         if (dev->refcount == 0) {
245                 if (nvhost_module_powered(dev))
246                         schedule_clockgating_locked(dev);
247                 kick = true;
248         }
249         mutex_unlock(&dev->lock);
250
251         if (kick) {
252                 wake_up(&dev->idle_wq);
253
254                 if (drv->idle)
255                         drv->idle(dev);
256         }
257 }
258
259 int nvhost_module_get_rate(struct nvhost_device *dev, unsigned long *rate,
260                 int index)
261 {
262         struct clk *c;
263
264         c = dev->clk[index];
265         if (IS_ERR_OR_NULL(c))
266                 return -EINVAL;
267
268         /* Need to enable client to get correct rate */
269         nvhost_module_busy(dev);
270         *rate = clk_get_rate(c);
271         nvhost_module_idle(dev);
272         return 0;
273
274 }
275
276 static int nvhost_module_update_rate(struct nvhost_device *dev, int index)
277 {
278         unsigned long rate = 0;
279         struct nvhost_module_client *m;
280
281         if (!dev->clk[index])
282                 return -EINVAL;
283
284         list_for_each_entry(m, &dev->client_list, node) {
285                 rate = max(m->rate[index], rate);
286         }
287         if (!rate)
288                 rate = clk_round_rate(dev->clk[index],
289                                 dev->clocks[index].default_rate);
290
291         return clk_set_rate(dev->clk[index], rate);
292 }
293
294 int nvhost_module_set_rate(struct nvhost_device *dev, void *priv,
295                 unsigned long rate, int index)
296 {
297         struct nvhost_module_client *m;
298         int i, ret = 0;
299
300         mutex_lock(&client_list_lock);
301         list_for_each_entry(m, &dev->client_list, node) {
302                 if (m->priv == priv) {
303                         for (i = 0; i < dev->num_clks; i++)
304                                 m->rate[i] = clk_round_rate(dev->clk[i], rate);
305                         break;
306                 }
307         }
308
309         for (i = 0; i < dev->num_clks; i++) {
310                 ret = nvhost_module_update_rate(dev, i);
311                 if (ret < 0)
312                         break;
313         }
314         mutex_unlock(&client_list_lock);
315         return ret;
316
317 }
318
319 int nvhost_module_add_client(struct nvhost_device *dev, void *priv)
320 {
321         int i;
322         unsigned long rate;
323         struct nvhost_module_client *client;
324
325         client = kzalloc(sizeof(*client), GFP_KERNEL);
326         if (!client)
327                 return -ENOMEM;
328
329         INIT_LIST_HEAD(&client->node);
330         client->priv = priv;
331
332         for (i = 0; i < dev->num_clks; i++) {
333                 rate = clk_round_rate(dev->clk[i],
334                                 dev->clocks[i].default_rate);
335                 client->rate[i] = rate;
336         }
337         mutex_lock(&client_list_lock);
338         list_add_tail(&client->node, &dev->client_list);
339         mutex_unlock(&client_list_lock);
340         return 0;
341 }
342
343 void nvhost_module_remove_client(struct nvhost_device *dev, void *priv)
344 {
345         int i;
346         struct nvhost_module_client *m;
347
348         mutex_lock(&client_list_lock);
349         list_for_each_entry(m, &dev->client_list, node) {
350                 if (priv == m->priv) {
351                         list_del(&m->node);
352                         break;
353                 }
354         }
355         if (m) {
356                 kfree(m);
357                 for (i = 0; i < dev->num_clks; i++)
358                         nvhost_module_update_rate(dev, i);
359         }
360         mutex_unlock(&client_list_lock);
361 }
362
363 int nvhost_module_init(struct nvhost_device *dev)
364 {
365         int i = 0;
366
367         /* initialize clocks to known state */
368         INIT_LIST_HEAD(&dev->client_list);
369         while (dev->clocks[i].name && i < NVHOST_MODULE_MAX_CLOCKS) {
370                 char devname[MAX_DEVID_LENGTH];
371                 long rate = dev->clocks[i].default_rate;
372                 struct clk *c;
373
374                 snprintf(devname, MAX_DEVID_LENGTH, "tegra_%s", dev->name);
375                 c = clk_get_sys(devname, dev->clocks[i].name);
376                 if (IS_ERR_OR_NULL(c)) {
377                         dev_err(&dev->dev, "Cannot get clock %s\n",
378                                         dev->clocks[i].name);
379                         continue;
380                 }
381
382                 rate = clk_round_rate(c, rate);
383                 clk_enable(c);
384                 clk_set_rate(c, rate);
385                 clk_disable(c);
386                 dev->clk[i] = c;
387                 i++;
388         }
389         dev->num_clks = i;
390
391         mutex_init(&dev->lock);
392         init_waitqueue_head(&dev->idle_wq);
393         INIT_DELAYED_WORK(&dev->powerstate_down, powerstate_down_handler);
394
395         /* power gate units that we can power gate */
396         if (dev->can_powergate) {
397                 do_powergate_locked(dev->powergate_ids[0]);
398                 do_powergate_locked(dev->powergate_ids[1]);
399                 dev->powerstate = NVHOST_POWER_STATE_POWERGATED;
400         } else {
401                 do_unpowergate_locked(dev->powergate_ids[0]);
402                 do_unpowergate_locked(dev->powergate_ids[1]);
403                 dev->powerstate = NVHOST_POWER_STATE_CLOCKGATED;
404         }
405
406         return 0;
407 }
408
409 static int is_module_idle(struct nvhost_device *dev)
410 {
411         int count;
412         mutex_lock(&dev->lock);
413         count = dev->refcount;
414         mutex_unlock(&dev->lock);
415         return (count == 0);
416 }
417
418 int nvhost_module_suspend(struct nvhost_device *dev)
419 {
420         int ret;
421         struct nvhost_driver *drv = to_nvhost_driver(dev->dev.driver);
422
423         ret = wait_event_timeout(dev->idle_wq, is_module_idle(dev),
424                         ACM_SUSPEND_WAIT_FOR_IDLE_TIMEOUT);
425         if (ret == 0) {
426                 dev_info(&dev->dev, "%s prevented suspend\n",
427                                 dev->name);
428                 return -EBUSY;
429         }
430
431         mutex_lock(&dev->lock);
432         cancel_delayed_work(&dev->powerstate_down);
433         to_state_powergated_locked(dev);
434         mutex_unlock(&dev->lock);
435
436         if (drv->suspend_ndev)
437                 drv->suspend_ndev(dev);
438
439         return 0;
440 }
441
442 void nvhost_module_deinit(struct nvhost_device *dev)
443 {
444         int i;
445         struct nvhost_driver *drv = to_nvhost_driver(dev->dev.driver);
446
447         if (drv->deinit)
448                 drv->deinit(dev);
449
450         nvhost_module_suspend(dev);
451         for (i = 0; i < dev->num_clks; i++)
452                 clk_put(dev->clk[i]);
453         dev->powerstate = NVHOST_POWER_STATE_DEINIT;
454 }
455