xref: /openbmc/linux/drivers/gpu/drm/tegra/gr2d.c (revision 77d84ff8)
1 /*
2  * Copyright (c) 2012-2013, NVIDIA Corporation.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #include <linux/clk.h>
18 
19 #include "drm.h"
20 #include "gem.h"
21 #include "gr2d.h"
22 
23 struct gr2d {
24 	struct tegra_drm_client client;
25 	struct host1x_channel *channel;
26 	struct clk *clk;
27 
28 	DECLARE_BITMAP(addr_regs, GR2D_NUM_REGS);
29 };
30 
31 static inline struct gr2d *to_gr2d(struct tegra_drm_client *client)
32 {
33 	return container_of(client, struct gr2d, client);
34 }
35 
36 static int gr2d_init(struct host1x_client *client)
37 {
38 	struct tegra_drm_client *drm = host1x_to_drm_client(client);
39 	struct tegra_drm *tegra = dev_get_drvdata(client->parent);
40 	unsigned long flags = HOST1X_SYNCPT_HAS_BASE;
41 	struct gr2d *gr2d = to_gr2d(drm);
42 
43 	gr2d->channel = host1x_channel_request(client->dev);
44 	if (!gr2d->channel)
45 		return -ENOMEM;
46 
47 	client->syncpts[0] = host1x_syncpt_request(client->dev, flags);
48 	if (!client->syncpts[0]) {
49 		host1x_channel_free(gr2d->channel);
50 		return -ENOMEM;
51 	}
52 
53 	return tegra_drm_register_client(tegra, drm);
54 }
55 
56 static int gr2d_exit(struct host1x_client *client)
57 {
58 	struct tegra_drm_client *drm = host1x_to_drm_client(client);
59 	struct tegra_drm *tegra = dev_get_drvdata(client->parent);
60 	struct gr2d *gr2d = to_gr2d(drm);
61 	int err;
62 
63 	err = tegra_drm_unregister_client(tegra, drm);
64 	if (err < 0)
65 		return err;
66 
67 	host1x_syncpt_free(client->syncpts[0]);
68 	host1x_channel_free(gr2d->channel);
69 
70 	return 0;
71 }
72 
73 static const struct host1x_client_ops gr2d_client_ops = {
74 	.init = gr2d_init,
75 	.exit = gr2d_exit,
76 };
77 
78 static int gr2d_open_channel(struct tegra_drm_client *client,
79 			     struct tegra_drm_context *context)
80 {
81 	struct gr2d *gr2d = to_gr2d(client);
82 
83 	context->channel = host1x_channel_get(gr2d->channel);
84 	if (!context->channel)
85 		return -ENOMEM;
86 
87 	return 0;
88 }
89 
90 static void gr2d_close_channel(struct tegra_drm_context *context)
91 {
92 	host1x_channel_put(context->channel);
93 }
94 
95 static int gr2d_is_addr_reg(struct device *dev, u32 class, u32 offset)
96 {
97 	struct gr2d *gr2d = dev_get_drvdata(dev);
98 
99 	switch (class) {
100 	case HOST1X_CLASS_HOST1X:
101 		if (offset == 0x2b)
102 			return 1;
103 
104 		break;
105 
106 	case HOST1X_CLASS_GR2D:
107 	case HOST1X_CLASS_GR2D_SB:
108 		if (offset >= GR2D_NUM_REGS)
109 			break;
110 
111 		if (test_bit(offset, gr2d->addr_regs))
112 			return 1;
113 
114 		break;
115 	}
116 
117 	return 0;
118 }
119 
120 static const struct tegra_drm_client_ops gr2d_ops = {
121 	.open_channel = gr2d_open_channel,
122 	.close_channel = gr2d_close_channel,
123 	.is_addr_reg = gr2d_is_addr_reg,
124 	.submit = tegra_drm_submit,
125 };
126 
127 static const struct of_device_id gr2d_match[] = {
128 	{ .compatible = "nvidia,tegra30-gr2d" },
129 	{ .compatible = "nvidia,tegra20-gr2d" },
130 	{ },
131 };
132 
133 static const u32 gr2d_addr_regs[] = {
134 	GR2D_UA_BASE_ADDR,
135 	GR2D_VA_BASE_ADDR,
136 	GR2D_PAT_BASE_ADDR,
137 	GR2D_DSTA_BASE_ADDR,
138 	GR2D_DSTB_BASE_ADDR,
139 	GR2D_DSTC_BASE_ADDR,
140 	GR2D_SRCA_BASE_ADDR,
141 	GR2D_SRCB_BASE_ADDR,
142 	GR2D_SRC_BASE_ADDR_SB,
143 	GR2D_DSTA_BASE_ADDR_SB,
144 	GR2D_DSTB_BASE_ADDR_SB,
145 	GR2D_UA_BASE_ADDR_SB,
146 	GR2D_VA_BASE_ADDR_SB,
147 };
148 
149 static int gr2d_probe(struct platform_device *pdev)
150 {
151 	struct device *dev = &pdev->dev;
152 	struct host1x_syncpt **syncpts;
153 	struct gr2d *gr2d;
154 	unsigned int i;
155 	int err;
156 
157 	gr2d = devm_kzalloc(dev, sizeof(*gr2d), GFP_KERNEL);
158 	if (!gr2d)
159 		return -ENOMEM;
160 
161 	syncpts = devm_kzalloc(dev, sizeof(*syncpts), GFP_KERNEL);
162 	if (!syncpts)
163 		return -ENOMEM;
164 
165 	gr2d->clk = devm_clk_get(dev, NULL);
166 	if (IS_ERR(gr2d->clk)) {
167 		dev_err(dev, "cannot get clock\n");
168 		return PTR_ERR(gr2d->clk);
169 	}
170 
171 	err = clk_prepare_enable(gr2d->clk);
172 	if (err) {
173 		dev_err(dev, "cannot turn on clock\n");
174 		return err;
175 	}
176 
177 	INIT_LIST_HEAD(&gr2d->client.base.list);
178 	gr2d->client.base.ops = &gr2d_client_ops;
179 	gr2d->client.base.dev = dev;
180 	gr2d->client.base.class = HOST1X_CLASS_GR2D;
181 	gr2d->client.base.syncpts = syncpts;
182 	gr2d->client.base.num_syncpts = 1;
183 
184 	INIT_LIST_HEAD(&gr2d->client.list);
185 	gr2d->client.ops = &gr2d_ops;
186 
187 	err = host1x_client_register(&gr2d->client.base);
188 	if (err < 0) {
189 		dev_err(dev, "failed to register host1x client: %d\n", err);
190 		clk_disable_unprepare(gr2d->clk);
191 		return err;
192 	}
193 
194 	/* initialize address register map */
195 	for (i = 0; i < ARRAY_SIZE(gr2d_addr_regs); i++)
196 		set_bit(gr2d_addr_regs[i], gr2d->addr_regs);
197 
198 	platform_set_drvdata(pdev, gr2d);
199 
200 	return 0;
201 }
202 
203 static int gr2d_remove(struct platform_device *pdev)
204 {
205 	struct gr2d *gr2d = platform_get_drvdata(pdev);
206 	int err;
207 
208 	err = host1x_client_unregister(&gr2d->client.base);
209 	if (err < 0) {
210 		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
211 			err);
212 		return err;
213 	}
214 
215 	clk_disable_unprepare(gr2d->clk);
216 
217 	return 0;
218 }
219 
220 struct platform_driver tegra_gr2d_driver = {
221 	.driver = {
222 		.name = "tegra-gr2d",
223 		.of_match_table = gr2d_match,
224 	},
225 	.probe = gr2d_probe,
226 	.remove = gr2d_remove,
227 };
228