video: tegra: host: handle !PM_RUNTIME
[linux-3.10.git] / drivers / video / tegra / host / gr2d / gr2d.c
1 /*
2  * drivers/video/tegra/host/gr2d/gr2d.c
3  *
4  * Tegra Graphics 2D
5  *
6  * Copyright (c) 2012-2013, 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 <linux/export.h>
22 #include <linux/module.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/of.h>
25 #include <linux/of_device.h>
26 #include <linux/of_platform.h>
27
28 #include <mach/pm_domains.h>
29
30 #include "dev.h"
31 #include "bus_client.h"
32 #include "nvhost_acm.h"
33 #include "gr2d_t30.h"
34 #include "gr2d_t114.h"
35 #include "t20/t20.h"
36 #include "t30/t30.h"
37 #include "t114/t114.h"
38 #include "t148/t148.h"
39
40 static struct of_device_id tegra_gr2d_of_match[] = {
41 #ifdef TEGRA_2X_OR_HIGHER_CONFIG
42         { .compatible = "nvidia,tegra20-gr2d",
43                 .data = (struct nvhost_device_data *)&t20_gr2d_info },
44 #endif
45 #ifdef TEGRA_3X_OR_HIGHER_CONFIG
46         { .compatible = "nvidia,tegra30-gr2d",
47                 .data = (struct nvhost_device_data *)&t30_gr2d_info },
48 #endif
49 #ifdef TEGRA_11X_OR_HIGHER_CONFIG
50         { .compatible = "nvidia,tegra114-gr2d",
51                 .data = (struct nvhost_device_data *)&t11_gr2d_info },
52 #endif
53 #ifdef TEGRA_14X_OR_HIGHER_CONFIG
54         { .compatible = "nvidia,tegra148-gr2d",
55                 .data = (struct nvhost_device_data *)&t14_gr2d_info },
56 #endif
57         { },
58 };
59
60 #ifdef CONFIG_PM_GENERIC_DOMAINS
61 static int gr2d_unpowergate(struct generic_pm_domain *domain)
62 {
63         struct nvhost_device_data *pdata;
64
65         pdata = container_of(domain, struct nvhost_device_data, pd);
66         return nvhost_module_power_on(pdata->pdev);
67 }
68
69 static int gr2d_powergate(struct generic_pm_domain *domain)
70 {
71         struct nvhost_device_data *pdata;
72
73         pdata = container_of(domain, struct nvhost_device_data, pd);
74         return nvhost_module_power_off(pdata->pdev);
75 }
76 #endif
77
78 static int gr2d_probe(struct platform_device *dev)
79 {
80         int err = 0;
81         struct nvhost_device_data *pdata = NULL;
82
83         if (dev->dev.of_node) {
84                 const struct of_device_id *match;
85
86                 match = of_match_device(tegra_gr2d_of_match, &dev->dev);
87                 if (match)
88                         pdata = (struct nvhost_device_data *)match->data;
89         } else
90                 pdata = (struct nvhost_device_data *)dev->dev.platform_data;
91
92         WARN_ON(!pdata);
93         if (!pdata) {
94                 dev_info(&dev->dev, "no platform data\n");
95                 return -ENODATA;
96         }
97
98         pdata->pdev = dev;
99         mutex_init(&pdata->lock);
100         platform_set_drvdata(dev, pdata);
101         nvhost_module_init(dev);
102
103 #ifdef CONFIG_PM_GENERIC_DOMAINS
104         pdata->pd.name = "gr2d";
105         pdata->pd.power_off = gr2d_powergate;
106         pdata->pd.power_on = gr2d_unpowergate;
107         pdata->pd.dev_ops.start = nvhost_module_enable_clk;
108         pdata->pd.dev_ops.stop = nvhost_module_disable_clk;
109
110         err = nvhost_module_add_domain(&pdata->pd, dev);
111
112         /* overwrite save/restore fptrs set by pm_genpd_init */
113         pdata->pd.domain.ops.suspend = nvhost_client_device_suspend;
114         pdata->pd.domain.ops.resume = nvhost_client_device_resume;
115         pdata->pd.dev_ops.restore_state = nvhost_module_finalize_poweron;
116 #endif
117
118 #ifdef CONFIG_PM_RUNTIME
119         if (pdata->clockgate_delay) {
120                 pm_runtime_set_autosuspend_delay(&dev->dev,
121                         pdata->clockgate_delay);
122                 pm_runtime_use_autosuspend(&dev->dev);
123         }
124         pm_runtime_enable(&dev->dev);
125         pm_runtime_get_sync(&dev->dev);
126 #else
127         nvhost_module_enable_clk(&dev->dev);
128 #endif
129
130         err = nvhost_client_device_init(dev);
131
132 #ifdef CONFIG_PM_RUNTIME
133         if (pdata->clockgate_delay)
134                 pm_runtime_put_sync_autosuspend(&dev->dev);
135         else
136                 pm_runtime_put(&dev->dev);
137         if (err)
138                 return err;
139 #endif
140
141         return 0;
142 }
143
144 static int __exit gr2d_remove(struct platform_device *dev)
145 {
146 #ifdef CONFIG_PM_RUNTIME
147         pm_runtime_put(&dev->dev);
148         pm_runtime_disable(&dev->dev);
149 #else
150         nvhost_module_disable_clk(&dev->dev);
151 #endif
152         return 0;
153 }
154
155 static struct platform_driver gr2d_driver = {
156         .probe = gr2d_probe,
157         .remove = __exit_p(gr2d_remove),
158         .driver = {
159                 .owner = THIS_MODULE,
160                 .name = "gr2d",
161 #ifdef CONFIG_OF
162                 .of_match_table = tegra_gr2d_of_match,
163 #endif
164 #ifdef CONFIG_PM
165                 .pm = &nvhost_module_pm_ops,
166 #endif
167         },
168 };
169
170 static int __init gr2d_init(void)
171 {
172         return platform_driver_register(&gr2d_driver);
173 }
174
175 static void __exit gr2d_exit(void)
176 {
177         platform_driver_unregister(&gr2d_driver);
178 }
179
180 module_init(gr2d_init);
181 module_exit(gr2d_exit);