blob: 3b851abbccaebc50cde152dc1b36eba2a3ff3d69 [file] [log] [blame]
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001/*
2 * Copyright (C) 2012 Avionic Design GmbH
3 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9
10#include <linux/clk.h>
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000011
12#include "drm.h"
13#include "dc.h"
14
15struct tegra_rgb {
16 struct tegra_output output;
Thierry Reding7602fa12013-10-30 09:55:33 +010017 struct tegra_dc *dc;
Dmitry Osipenkob1891532014-02-11 21:12:27 +040018 bool enabled;
Thierry Reding7602fa12013-10-30 09:55:33 +010019
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000020 struct clk *clk_parent;
21 struct clk *clk;
22};
23
24static inline struct tegra_rgb *to_rgb(struct tegra_output *output)
25{
26 return container_of(output, struct tegra_rgb, output);
27}
28
29struct reg_entry {
30 unsigned long offset;
31 unsigned long value;
32};
33
34static const struct reg_entry rgb_enable[] = {
35 { DC_COM_PIN_OUTPUT_ENABLE(0), 0x00000000 },
36 { DC_COM_PIN_OUTPUT_ENABLE(1), 0x00000000 },
37 { DC_COM_PIN_OUTPUT_ENABLE(2), 0x00000000 },
38 { DC_COM_PIN_OUTPUT_ENABLE(3), 0x00000000 },
39 { DC_COM_PIN_OUTPUT_POLARITY(0), 0x00000000 },
40 { DC_COM_PIN_OUTPUT_POLARITY(1), 0x01000000 },
41 { DC_COM_PIN_OUTPUT_POLARITY(2), 0x00000000 },
42 { DC_COM_PIN_OUTPUT_POLARITY(3), 0x00000000 },
43 { DC_COM_PIN_OUTPUT_DATA(0), 0x00000000 },
44 { DC_COM_PIN_OUTPUT_DATA(1), 0x00000000 },
45 { DC_COM_PIN_OUTPUT_DATA(2), 0x00000000 },
46 { DC_COM_PIN_OUTPUT_DATA(3), 0x00000000 },
47 { DC_COM_PIN_OUTPUT_SELECT(0), 0x00000000 },
48 { DC_COM_PIN_OUTPUT_SELECT(1), 0x00000000 },
49 { DC_COM_PIN_OUTPUT_SELECT(2), 0x00000000 },
50 { DC_COM_PIN_OUTPUT_SELECT(3), 0x00000000 },
51 { DC_COM_PIN_OUTPUT_SELECT(4), 0x00210222 },
52 { DC_COM_PIN_OUTPUT_SELECT(5), 0x00002200 },
53 { DC_COM_PIN_OUTPUT_SELECT(6), 0x00020000 },
54};
55
56static const struct reg_entry rgb_disable[] = {
57 { DC_COM_PIN_OUTPUT_SELECT(6), 0x00000000 },
58 { DC_COM_PIN_OUTPUT_SELECT(5), 0x00000000 },
59 { DC_COM_PIN_OUTPUT_SELECT(4), 0x00000000 },
60 { DC_COM_PIN_OUTPUT_SELECT(3), 0x00000000 },
61 { DC_COM_PIN_OUTPUT_SELECT(2), 0x00000000 },
62 { DC_COM_PIN_OUTPUT_SELECT(1), 0x00000000 },
63 { DC_COM_PIN_OUTPUT_SELECT(0), 0x00000000 },
64 { DC_COM_PIN_OUTPUT_DATA(3), 0xaaaaaaaa },
65 { DC_COM_PIN_OUTPUT_DATA(2), 0xaaaaaaaa },
66 { DC_COM_PIN_OUTPUT_DATA(1), 0xaaaaaaaa },
67 { DC_COM_PIN_OUTPUT_DATA(0), 0xaaaaaaaa },
68 { DC_COM_PIN_OUTPUT_POLARITY(3), 0x00000000 },
69 { DC_COM_PIN_OUTPUT_POLARITY(2), 0x00000000 },
70 { DC_COM_PIN_OUTPUT_POLARITY(1), 0x00000000 },
71 { DC_COM_PIN_OUTPUT_POLARITY(0), 0x00000000 },
72 { DC_COM_PIN_OUTPUT_ENABLE(3), 0x55555555 },
73 { DC_COM_PIN_OUTPUT_ENABLE(2), 0x55555555 },
74 { DC_COM_PIN_OUTPUT_ENABLE(1), 0x55150005 },
75 { DC_COM_PIN_OUTPUT_ENABLE(0), 0x55555555 },
76};
77
78static void tegra_dc_write_regs(struct tegra_dc *dc,
79 const struct reg_entry *table,
80 unsigned int num)
81{
82 unsigned int i;
83
84 for (i = 0; i < num; i++)
85 tegra_dc_writel(dc, table[i].value, table[i].offset);
86}
87
88static int tegra_output_rgb_enable(struct tegra_output *output)
89{
Thierry Reding7602fa12013-10-30 09:55:33 +010090 struct tegra_rgb *rgb = to_rgb(output);
Thierry Reding72d30282013-12-12 11:06:55 +010091 unsigned long value;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000092
Dmitry Osipenkob1891532014-02-11 21:12:27 +040093 if (rgb->enabled)
94 return 0;
95
Thierry Reding7602fa12013-10-30 09:55:33 +010096 tegra_dc_write_regs(rgb->dc, rgb_enable, ARRAY_SIZE(rgb_enable));
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000097
Thierry Reding72d30282013-12-12 11:06:55 +010098 value = DE_SELECT_ACTIVE | DE_CONTROL_NORMAL;
99 tegra_dc_writel(rgb->dc, value, DC_DISP_DATA_ENABLE_OPTIONS);
100
101 /* XXX: parameterize? */
102 value = tegra_dc_readl(rgb->dc, DC_COM_PIN_OUTPUT_POLARITY(1));
103 value &= ~LVS_OUTPUT_POLARITY_LOW;
104 value &= ~LHS_OUTPUT_POLARITY_LOW;
105 tegra_dc_writel(rgb->dc, value, DC_COM_PIN_OUTPUT_POLARITY(1));
106
107 /* XXX: parameterize? */
108 value = DISP_DATA_FORMAT_DF1P1C | DISP_ALIGNMENT_MSB |
109 DISP_ORDER_RED_BLUE;
110 tegra_dc_writel(rgb->dc, value, DC_DISP_DISP_INTERFACE_CONTROL);
111
112 /* XXX: parameterize? */
113 value = SC0_H_QUALIFIER_NONE | SC1_H_QUALIFIER_NONE;
114 tegra_dc_writel(rgb->dc, value, DC_DISP_SHIFT_CLOCK_OPTIONS);
115
116 value = tegra_dc_readl(rgb->dc, DC_CMD_DISPLAY_COMMAND);
117 value &= ~DISP_CTRL_MODE_MASK;
118 value |= DISP_CTRL_MODE_C_DISPLAY;
119 tegra_dc_writel(rgb->dc, value, DC_CMD_DISPLAY_COMMAND);
120
121 value = tegra_dc_readl(rgb->dc, DC_CMD_DISPLAY_POWER_CONTROL);
122 value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
123 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
124 tegra_dc_writel(rgb->dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
125
Thierry Reding62b9e062014-11-21 17:33:33 +0100126 tegra_dc_commit(rgb->dc);
Thierry Reding72d30282013-12-12 11:06:55 +0100127
Dmitry Osipenkob1891532014-02-11 21:12:27 +0400128 rgb->enabled = true;
129
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000130 return 0;
131}
132
133static int tegra_output_rgb_disable(struct tegra_output *output)
134{
Thierry Reding7602fa12013-10-30 09:55:33 +0100135 struct tegra_rgb *rgb = to_rgb(output);
Thierry Reding72d30282013-12-12 11:06:55 +0100136 unsigned long value;
137
Dmitry Osipenkob1891532014-02-11 21:12:27 +0400138 if (!rgb->enabled)
139 return 0;
140
Thierry Reding72d30282013-12-12 11:06:55 +0100141 value = tegra_dc_readl(rgb->dc, DC_CMD_DISPLAY_POWER_CONTROL);
142 value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
143 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE);
144 tegra_dc_writel(rgb->dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
145
146 value = tegra_dc_readl(rgb->dc, DC_CMD_DISPLAY_COMMAND);
147 value &= ~DISP_CTRL_MODE_MASK;
148 tegra_dc_writel(rgb->dc, value, DC_CMD_DISPLAY_COMMAND);
149
Thierry Reding7602fa12013-10-30 09:55:33 +0100150 tegra_dc_write_regs(rgb->dc, rgb_disable, ARRAY_SIZE(rgb_disable));
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000151
Thierry Reding62b9e062014-11-21 17:33:33 +0100152 tegra_dc_commit(rgb->dc);
153
Dmitry Osipenkob1891532014-02-11 21:12:27 +0400154 rgb->enabled = false;
155
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000156 return 0;
157}
158
159static int tegra_output_rgb_setup_clock(struct tegra_output *output,
Thierry Reding91eded92014-03-26 13:32:21 +0100160 struct clk *clk, unsigned long pclk,
161 unsigned int *div)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000162{
163 struct tegra_rgb *rgb = to_rgb(output);
Thierry Reding91eded92014-03-26 13:32:21 +0100164 int err;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000165
Thierry Reding91eded92014-03-26 13:32:21 +0100166 err = clk_set_parent(clk, rgb->clk_parent);
167 if (err < 0) {
168 dev_err(output->dev, "failed to set parent: %d\n", err);
169 return err;
170 }
171
172 /*
173 * We may not want to change the frequency of the parent clock, since
174 * it may be a parent for other peripherals. This is due to the fact
175 * that on Tegra20 there's only a single clock dedicated to display
176 * (pll_d_out0), whereas later generations have a second one that can
177 * be used to independently drive a second output (pll_d2_out0).
178 *
179 * As a way to support multiple outputs on Tegra20 as well, pll_p is
180 * typically used as the parent clock for the display controllers.
181 * But this comes at a cost: pll_p is the parent of several other
182 * peripherals, so its frequency shouldn't change out of the blue.
183 *
184 * The best we can do at this point is to use the shift clock divider
185 * and hope that the desired frequency can be matched (or at least
186 * matched sufficiently close that the panel will still work).
187 */
188
189 *div = ((clk_get_rate(clk) * 2) / pclk) - 2;
190
191 return 0;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000192}
193
194static int tegra_output_rgb_check_mode(struct tegra_output *output,
195 struct drm_display_mode *mode,
196 enum drm_mode_status *status)
197{
198 /*
199 * FIXME: For now, always assume that the mode is okay. There are
200 * unresolved issues with clk_round_rate(), which doesn't always
201 * reliably report whether a frequency can be set or not.
202 */
203
204 *status = MODE_OK;
205
206 return 0;
207}
208
209static const struct tegra_output_ops rgb_ops = {
210 .enable = tegra_output_rgb_enable,
211 .disable = tegra_output_rgb_disable,
212 .setup_clock = tegra_output_rgb_setup_clock,
213 .check_mode = tegra_output_rgb_check_mode,
214};
215
216int tegra_dc_rgb_probe(struct tegra_dc *dc)
217{
218 struct device_node *np;
219 struct tegra_rgb *rgb;
220 int err;
221
222 np = of_get_child_by_name(dc->dev->of_node, "rgb");
223 if (!np || !of_device_is_available(np))
224 return -ENODEV;
225
226 rgb = devm_kzalloc(dc->dev, sizeof(*rgb), GFP_KERNEL);
227 if (!rgb)
228 return -ENOMEM;
229
Thierry Reding03da0e72013-08-30 15:27:16 +0200230 rgb->output.dev = dc->dev;
231 rgb->output.of_node = np;
Thierry Reding7602fa12013-10-30 09:55:33 +0100232 rgb->dc = dc;
Thierry Reding03da0e72013-08-30 15:27:16 +0200233
Thierry Reding59d29c02013-10-14 14:26:42 +0200234 err = tegra_output_probe(&rgb->output);
Thierry Reding03da0e72013-08-30 15:27:16 +0200235 if (err < 0)
236 return err;
237
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000238 rgb->clk = devm_clk_get(dc->dev, NULL);
239 if (IS_ERR(rgb->clk)) {
240 dev_err(dc->dev, "failed to get clock\n");
241 return PTR_ERR(rgb->clk);
242 }
243
244 rgb->clk_parent = devm_clk_get(dc->dev, "parent");
245 if (IS_ERR(rgb->clk_parent)) {
246 dev_err(dc->dev, "failed to get parent clock\n");
247 return PTR_ERR(rgb->clk_parent);
248 }
249
250 err = clk_set_parent(rgb->clk, rgb->clk_parent);
251 if (err < 0) {
252 dev_err(dc->dev, "failed to set parent clock: %d\n", err);
253 return err;
254 }
255
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000256 dc->rgb = &rgb->output;
257
258 return 0;
259}
260
Thierry Reding59d29c02013-10-14 14:26:42 +0200261int tegra_dc_rgb_remove(struct tegra_dc *dc)
262{
263 int err;
264
265 if (!dc->rgb)
266 return 0;
267
268 err = tegra_output_remove(dc->rgb);
269 if (err < 0)
270 return err;
271
272 return 0;
273}
274
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000275int tegra_dc_rgb_init(struct drm_device *drm, struct tegra_dc *dc)
276{
277 struct tegra_rgb *rgb = to_rgb(dc->rgb);
278 int err;
279
280 if (!dc->rgb)
281 return -ENODEV;
282
283 rgb->output.type = TEGRA_OUTPUT_RGB;
284 rgb->output.ops = &rgb_ops;
285
286 err = tegra_output_init(dc->base.dev, &rgb->output);
287 if (err < 0) {
288 dev_err(dc->dev, "output setup failed: %d\n", err);
289 return err;
290 }
291
292 /*
293 * By default, outputs can be associated with each display controller.
294 * RGB outputs are an exception, so we make sure they can be attached
295 * to only their parent display controller.
296 */
Thierry Reding456ac562014-01-10 16:56:06 +0100297 rgb->output.encoder.possible_crtcs = drm_crtc_mask(&dc->base);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000298
299 return 0;
300}
301
302int tegra_dc_rgb_exit(struct tegra_dc *dc)
303{
304 if (dc->rgb) {
305 int err;
306
307 err = tegra_output_disable(dc->rgb);
308 if (err < 0) {
309 dev_err(dc->dev, "output failed to disable: %d\n", err);
310 return err;
311 }
312
313 err = tegra_output_exit(dc->rgb);
314 if (err < 0) {
315 dev_err(dc->dev, "output cleanup failed: %d\n", err);
316 return err;
317 }
318
319 dc->rgb = NULL;
320 }
321
322 return 0;
323}