blob: f7254ab7a5c709666c67eddf92a515786aadf16e [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 Reding9eb9b222013-09-24 16:32:47 +020011#include <linux/debugfs.h>
Thierry Redingdf06b752014-06-26 21:41:53 +020012#include <linux/iommu.h>
Stephen Warrenca480802013-11-06 16:20:54 -070013#include <linux/reset.h>
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000014
Thierry Reding9c012702014-07-07 15:32:53 +020015#include <soc/tegra/pmc.h>
16
Arto Merilainende2ba662013-03-22 16:34:08 +020017#include "dc.h"
18#include "drm.h"
19#include "gem.h"
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000020
Thierry Reding4aa3df72014-11-24 16:27:13 +010021#include <drm/drm_atomic_helper.h>
Daniel Vetter3cb9ae42014-10-29 10:03:57 +010022#include <drm/drm_plane_helper.h>
23
Thierry Reding8620fc62013-12-12 11:03:59 +010024struct tegra_dc_soc_info {
Thierry Reding42d06592014-12-08 15:45:39 +010025 bool supports_border_color;
Thierry Reding8620fc62013-12-12 11:03:59 +010026 bool supports_interlacing;
Thierry Redinge6876512013-12-20 13:58:33 +010027 bool supports_cursor;
Thierry Redingc134f012014-06-03 14:48:12 +020028 bool supports_block_linear;
Thierry Redingd1f3e1e2014-07-11 08:29:14 +020029 unsigned int pitch_align;
Thierry Reding9c012702014-07-07 15:32:53 +020030 bool has_powergate;
Thierry Reding8620fc62013-12-12 11:03:59 +010031};
32
Thierry Redingf34bc782012-11-04 21:47:13 +010033struct tegra_plane {
34 struct drm_plane base;
35 unsigned int index;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000036};
37
Thierry Redingf34bc782012-11-04 21:47:13 +010038static inline struct tegra_plane *to_tegra_plane(struct drm_plane *plane)
39{
40 return container_of(plane, struct tegra_plane, base);
41}
42
Thierry Reding205d48e2014-10-21 13:41:46 +020043static void tegra_dc_window_commit(struct tegra_dc *dc, unsigned int index)
44{
45 u32 value = WIN_A_ACT_REQ << index;
46
47 tegra_dc_writel(dc, value << 8, DC_CMD_STATE_CONTROL);
48 tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
49}
50
51static void tegra_dc_cursor_commit(struct tegra_dc *dc)
52{
53 tegra_dc_writel(dc, CURSOR_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
54 tegra_dc_writel(dc, CURSOR_ACT_REQ, DC_CMD_STATE_CONTROL);
55}
56
Thierry Redingd700ba72014-12-08 15:50:04 +010057/*
Thierry Reding86df2562014-12-08 16:03:53 +010058 * Reads the active copy of a register. This takes the dc->lock spinlock to
59 * prevent races with the VBLANK processing which also needs access to the
60 * active copy of some registers.
61 */
62static u32 tegra_dc_readl_active(struct tegra_dc *dc, unsigned long offset)
63{
64 unsigned long flags;
65 u32 value;
66
67 spin_lock_irqsave(&dc->lock, flags);
68
69 tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
70 value = tegra_dc_readl(dc, offset);
71 tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
72
73 spin_unlock_irqrestore(&dc->lock, flags);
74 return value;
75}
76
77/*
Thierry Redingd700ba72014-12-08 15:50:04 +010078 * Double-buffered registers have two copies: ASSEMBLY and ACTIVE. When the
79 * *_ACT_REQ bits are set the ASSEMBLY copy is latched into the ACTIVE copy.
80 * Latching happens mmediately if the display controller is in STOP mode or
81 * on the next frame boundary otherwise.
82 *
83 * Triple-buffered registers have three copies: ASSEMBLY, ARM and ACTIVE. The
84 * ASSEMBLY copy is latched into the ARM copy immediately after *_UPDATE bits
85 * are written. When the *_ACT_REQ bits are written, the ARM copy is latched
86 * into the ACTIVE copy, either immediately if the display controller is in
87 * STOP mode, or at the next frame boundary otherwise.
88 */
Thierry Reding62b9e062014-11-21 17:33:33 +010089void tegra_dc_commit(struct tegra_dc *dc)
Thierry Reding205d48e2014-10-21 13:41:46 +020090{
91 tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
92 tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
93}
94
Thierry Reding10288ee2014-03-14 09:54:58 +010095static unsigned int tegra_dc_format(uint32_t format, uint32_t *swap)
96{
97 /* assume no swapping of fetched data */
98 if (swap)
99 *swap = BYTE_SWAP_NOSWAP;
100
101 switch (format) {
102 case DRM_FORMAT_XBGR8888:
103 return WIN_COLOR_DEPTH_R8G8B8A8;
104
105 case DRM_FORMAT_XRGB8888:
106 return WIN_COLOR_DEPTH_B8G8R8A8;
107
108 case DRM_FORMAT_RGB565:
109 return WIN_COLOR_DEPTH_B5G6R5;
110
111 case DRM_FORMAT_UYVY:
112 return WIN_COLOR_DEPTH_YCbCr422;
113
114 case DRM_FORMAT_YUYV:
115 if (swap)
116 *swap = BYTE_SWAP_SWAP2;
117
118 return WIN_COLOR_DEPTH_YCbCr422;
119
120 case DRM_FORMAT_YUV420:
121 return WIN_COLOR_DEPTH_YCbCr420P;
122
123 case DRM_FORMAT_YUV422:
124 return WIN_COLOR_DEPTH_YCbCr422P;
125
126 default:
127 break;
128 }
129
130 WARN(1, "unsupported pixel format %u, using default\n", format);
131 return WIN_COLOR_DEPTH_B8G8R8A8;
132}
133
134static bool tegra_dc_format_is_yuv(unsigned int format, bool *planar)
135{
136 switch (format) {
137 case WIN_COLOR_DEPTH_YCbCr422:
138 case WIN_COLOR_DEPTH_YUV422:
139 if (planar)
140 *planar = false;
141
142 return true;
143
144 case WIN_COLOR_DEPTH_YCbCr420P:
145 case WIN_COLOR_DEPTH_YUV420P:
146 case WIN_COLOR_DEPTH_YCbCr422P:
147 case WIN_COLOR_DEPTH_YUV422P:
148 case WIN_COLOR_DEPTH_YCbCr422R:
149 case WIN_COLOR_DEPTH_YUV422R:
150 case WIN_COLOR_DEPTH_YCbCr422RA:
151 case WIN_COLOR_DEPTH_YUV422RA:
152 if (planar)
153 *planar = true;
154
155 return true;
156 }
157
Thierry Redingfb35c6b2014-12-08 15:55:28 +0100158 if (planar)
159 *planar = false;
160
Thierry Reding10288ee2014-03-14 09:54:58 +0100161 return false;
162}
163
164static inline u32 compute_dda_inc(unsigned int in, unsigned int out, bool v,
165 unsigned int bpp)
166{
167 fixed20_12 outf = dfixed_init(out);
168 fixed20_12 inf = dfixed_init(in);
169 u32 dda_inc;
170 int max;
171
172 if (v)
173 max = 15;
174 else {
175 switch (bpp) {
176 case 2:
177 max = 8;
178 break;
179
180 default:
181 WARN_ON_ONCE(1);
182 /* fallthrough */
183 case 4:
184 max = 4;
185 break;
186 }
187 }
188
189 outf.full = max_t(u32, outf.full - dfixed_const(1), dfixed_const(1));
190 inf.full -= dfixed_const(1);
191
192 dda_inc = dfixed_div(inf, outf);
193 dda_inc = min_t(u32, dda_inc, dfixed_const(max));
194
195 return dda_inc;
196}
197
198static inline u32 compute_initial_dda(unsigned int in)
199{
200 fixed20_12 inf = dfixed_init(in);
201 return dfixed_frac(inf);
202}
203
Thierry Reding4aa3df72014-11-24 16:27:13 +0100204static void tegra_dc_setup_window(struct tegra_dc *dc, unsigned int index,
205 const struct tegra_dc_window *window)
Thierry Reding10288ee2014-03-14 09:54:58 +0100206{
207 unsigned h_offset, v_offset, h_size, v_size, h_dda, v_dda, bpp;
Sean Paul93396d02014-11-19 13:04:49 -0500208 unsigned long value, flags;
Thierry Reding10288ee2014-03-14 09:54:58 +0100209 bool yuv, planar;
210
211 /*
212 * For YUV planar modes, the number of bytes per pixel takes into
213 * account only the luma component and therefore is 1.
214 */
215 yuv = tegra_dc_format_is_yuv(window->format, &planar);
216 if (!yuv)
217 bpp = window->bits_per_pixel / 8;
218 else
219 bpp = planar ? 1 : 2;
220
Sean Paul93396d02014-11-19 13:04:49 -0500221 spin_lock_irqsave(&dc->lock, flags);
222
Thierry Reding10288ee2014-03-14 09:54:58 +0100223 value = WINDOW_A_SELECT << index;
224 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
225
226 tegra_dc_writel(dc, window->format, DC_WIN_COLOR_DEPTH);
227 tegra_dc_writel(dc, window->swap, DC_WIN_BYTE_SWAP);
228
229 value = V_POSITION(window->dst.y) | H_POSITION(window->dst.x);
230 tegra_dc_writel(dc, value, DC_WIN_POSITION);
231
232 value = V_SIZE(window->dst.h) | H_SIZE(window->dst.w);
233 tegra_dc_writel(dc, value, DC_WIN_SIZE);
234
235 h_offset = window->src.x * bpp;
236 v_offset = window->src.y;
237 h_size = window->src.w * bpp;
238 v_size = window->src.h;
239
240 value = V_PRESCALED_SIZE(v_size) | H_PRESCALED_SIZE(h_size);
241 tegra_dc_writel(dc, value, DC_WIN_PRESCALED_SIZE);
242
243 /*
244 * For DDA computations the number of bytes per pixel for YUV planar
245 * modes needs to take into account all Y, U and V components.
246 */
247 if (yuv && planar)
248 bpp = 2;
249
250 h_dda = compute_dda_inc(window->src.w, window->dst.w, false, bpp);
251 v_dda = compute_dda_inc(window->src.h, window->dst.h, true, bpp);
252
253 value = V_DDA_INC(v_dda) | H_DDA_INC(h_dda);
254 tegra_dc_writel(dc, value, DC_WIN_DDA_INC);
255
256 h_dda = compute_initial_dda(window->src.x);
257 v_dda = compute_initial_dda(window->src.y);
258
259 tegra_dc_writel(dc, h_dda, DC_WIN_H_INITIAL_DDA);
260 tegra_dc_writel(dc, v_dda, DC_WIN_V_INITIAL_DDA);
261
262 tegra_dc_writel(dc, 0, DC_WIN_UV_BUF_STRIDE);
263 tegra_dc_writel(dc, 0, DC_WIN_BUF_STRIDE);
264
265 tegra_dc_writel(dc, window->base[0], DC_WINBUF_START_ADDR);
266
267 if (yuv && planar) {
268 tegra_dc_writel(dc, window->base[1], DC_WINBUF_START_ADDR_U);
269 tegra_dc_writel(dc, window->base[2], DC_WINBUF_START_ADDR_V);
270 value = window->stride[1] << 16 | window->stride[0];
271 tegra_dc_writel(dc, value, DC_WIN_LINE_STRIDE);
272 } else {
273 tegra_dc_writel(dc, window->stride[0], DC_WIN_LINE_STRIDE);
274 }
275
276 if (window->bottom_up)
277 v_offset += window->src.h - 1;
278
279 tegra_dc_writel(dc, h_offset, DC_WINBUF_ADDR_H_OFFSET);
280 tegra_dc_writel(dc, v_offset, DC_WINBUF_ADDR_V_OFFSET);
281
Thierry Redingc134f012014-06-03 14:48:12 +0200282 if (dc->soc->supports_block_linear) {
283 unsigned long height = window->tiling.value;
Thierry Reding10288ee2014-03-14 09:54:58 +0100284
Thierry Redingc134f012014-06-03 14:48:12 +0200285 switch (window->tiling.mode) {
286 case TEGRA_BO_TILING_MODE_PITCH:
287 value = DC_WINBUF_SURFACE_KIND_PITCH;
288 break;
289
290 case TEGRA_BO_TILING_MODE_TILED:
291 value = DC_WINBUF_SURFACE_KIND_TILED;
292 break;
293
294 case TEGRA_BO_TILING_MODE_BLOCK:
295 value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
296 DC_WINBUF_SURFACE_KIND_BLOCK;
297 break;
298 }
299
300 tegra_dc_writel(dc, value, DC_WINBUF_SURFACE_KIND);
301 } else {
302 switch (window->tiling.mode) {
303 case TEGRA_BO_TILING_MODE_PITCH:
304 value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
305 DC_WIN_BUFFER_ADDR_MODE_LINEAR;
306 break;
307
308 case TEGRA_BO_TILING_MODE_TILED:
309 value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
310 DC_WIN_BUFFER_ADDR_MODE_TILE;
311 break;
312
313 case TEGRA_BO_TILING_MODE_BLOCK:
Thierry Reding4aa3df72014-11-24 16:27:13 +0100314 /*
315 * No need to handle this here because ->atomic_check
316 * will already have filtered it out.
317 */
318 break;
Thierry Redingc134f012014-06-03 14:48:12 +0200319 }
320
321 tegra_dc_writel(dc, value, DC_WIN_BUFFER_ADDR_MODE);
322 }
Thierry Reding10288ee2014-03-14 09:54:58 +0100323
324 value = WIN_ENABLE;
325
326 if (yuv) {
327 /* setup default colorspace conversion coefficients */
328 tegra_dc_writel(dc, 0x00f0, DC_WIN_CSC_YOF);
329 tegra_dc_writel(dc, 0x012a, DC_WIN_CSC_KYRGB);
330 tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KUR);
331 tegra_dc_writel(dc, 0x0198, DC_WIN_CSC_KVR);
332 tegra_dc_writel(dc, 0x039b, DC_WIN_CSC_KUG);
333 tegra_dc_writel(dc, 0x032f, DC_WIN_CSC_KVG);
334 tegra_dc_writel(dc, 0x0204, DC_WIN_CSC_KUB);
335 tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KVB);
336
337 value |= CSC_ENABLE;
338 } else if (window->bits_per_pixel < 24) {
339 value |= COLOR_EXPAND;
340 }
341
342 if (window->bottom_up)
343 value |= V_DIRECTION;
344
345 tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
346
347 /*
348 * Disable blending and assume Window A is the bottom-most window,
349 * Window C is the top-most window and Window B is in the middle.
350 */
351 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_NOKEY);
352 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_1WIN);
353
354 switch (index) {
355 case 0:
356 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_X);
357 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
358 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
359 break;
360
361 case 1:
362 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
363 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
364 tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
365 break;
366
367 case 2:
368 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
369 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_Y);
370 tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_3WIN_XY);
371 break;
372 }
373
Thierry Reding205d48e2014-10-21 13:41:46 +0200374 tegra_dc_window_commit(dc, index);
Thierry Reding10288ee2014-03-14 09:54:58 +0100375
Sean Paul93396d02014-11-19 13:04:49 -0500376 spin_unlock_irqrestore(&dc->lock, flags);
Thierry Redingc7679302014-10-21 13:51:53 +0200377}
378
379static void tegra_plane_destroy(struct drm_plane *plane)
380{
381 struct tegra_plane *p = to_tegra_plane(plane);
382
383 drm_plane_cleanup(plane);
384 kfree(p);
385}
386
387static const u32 tegra_primary_plane_formats[] = {
388 DRM_FORMAT_XBGR8888,
389 DRM_FORMAT_XRGB8888,
390 DRM_FORMAT_RGB565,
391};
392
Thierry Reding4aa3df72014-11-24 16:27:13 +0100393static void tegra_primary_plane_destroy(struct drm_plane *plane)
Thierry Redingc7679302014-10-21 13:51:53 +0200394{
Thierry Reding4aa3df72014-11-24 16:27:13 +0100395 tegra_plane_destroy(plane);
396}
397
398static const struct drm_plane_funcs tegra_primary_plane_funcs = {
399 .update_plane = drm_plane_helper_update,
400 .disable_plane = drm_plane_helper_disable,
401 .destroy = tegra_primary_plane_destroy,
402 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
403};
404
405static int tegra_plane_prepare_fb(struct drm_plane *plane,
406 struct drm_framebuffer *fb)
407{
408 return 0;
409}
410
411static void tegra_plane_cleanup_fb(struct drm_plane *plane,
412 struct drm_framebuffer *fb)
413{
414}
415
416static int tegra_plane_atomic_check(struct drm_plane *plane,
417 struct drm_plane_state *state)
418{
419 struct tegra_dc *dc = to_tegra_dc(state->crtc);
420 struct tegra_bo_tiling tiling;
Thierry Redingc7679302014-10-21 13:51:53 +0200421 int err;
422
Thierry Reding4aa3df72014-11-24 16:27:13 +0100423 /* no need for further checks if the plane is being disabled */
424 if (!state->crtc)
425 return 0;
426
427 err = tegra_fb_get_tiling(state->fb, &tiling);
428 if (err < 0)
429 return err;
430
431 if (tiling.mode == TEGRA_BO_TILING_MODE_BLOCK &&
432 !dc->soc->supports_block_linear) {
433 DRM_ERROR("hardware doesn't support block linear mode\n");
434 return -EINVAL;
435 }
436
437 /*
438 * Tegra doesn't support different strides for U and V planes so we
439 * error out if the user tries to display a framebuffer with such a
440 * configuration.
441 */
442 if (drm_format_num_planes(state->fb->pixel_format) > 2) {
443 if (state->fb->pitches[2] != state->fb->pitches[1]) {
444 DRM_ERROR("unsupported UV-plane configuration\n");
445 return -EINVAL;
446 }
447 }
448
449 return 0;
450}
451
452static void tegra_plane_atomic_update(struct drm_plane *plane,
453 struct drm_plane_state *old_state)
454{
455 struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
456 struct drm_framebuffer *fb = plane->state->fb;
457 struct tegra_plane *p = to_tegra_plane(plane);
458 struct tegra_dc_window window;
459 unsigned int i;
460 int err;
461
462 /* rien ne va plus */
463 if (!plane->state->crtc || !plane->state->fb)
464 return;
465
Thierry Redingc7679302014-10-21 13:51:53 +0200466 memset(&window, 0, sizeof(window));
Thierry Reding4aa3df72014-11-24 16:27:13 +0100467 window.src.x = plane->state->src_x >> 16;
468 window.src.y = plane->state->src_y >> 16;
469 window.src.w = plane->state->src_w >> 16;
470 window.src.h = plane->state->src_h >> 16;
471 window.dst.x = plane->state->crtc_x;
472 window.dst.y = plane->state->crtc_y;
473 window.dst.w = plane->state->crtc_w;
474 window.dst.h = plane->state->crtc_h;
Thierry Redingc7679302014-10-21 13:51:53 +0200475 window.format = tegra_dc_format(fb->pixel_format, &window.swap);
476 window.bits_per_pixel = fb->bits_per_pixel;
477 window.bottom_up = tegra_fb_is_bottom_up(fb);
478
479 err = tegra_fb_get_tiling(fb, &window.tiling);
Thierry Reding4aa3df72014-11-24 16:27:13 +0100480 WARN_ON(err < 0);
Thierry Redingc7679302014-10-21 13:51:53 +0200481
Thierry Reding4aa3df72014-11-24 16:27:13 +0100482 for (i = 0; i < drm_format_num_planes(fb->pixel_format); i++) {
483 struct tegra_bo *bo = tegra_fb_get_plane(fb, i);
Thierry Redingc7679302014-10-21 13:51:53 +0200484
Thierry Reding4aa3df72014-11-24 16:27:13 +0100485 window.base[i] = bo->paddr + fb->offsets[i];
486 window.stride[i] = fb->pitches[i];
487 }
Thierry Redingc7679302014-10-21 13:51:53 +0200488
Thierry Reding4aa3df72014-11-24 16:27:13 +0100489 tegra_dc_setup_window(dc, p->index, &window);
Thierry Redingc7679302014-10-21 13:51:53 +0200490}
491
Thierry Reding4aa3df72014-11-24 16:27:13 +0100492static void tegra_plane_atomic_disable(struct drm_plane *plane,
493 struct drm_plane_state *old_state)
Thierry Redingc7679302014-10-21 13:51:53 +0200494{
Thierry Reding4aa3df72014-11-24 16:27:13 +0100495 struct tegra_plane *p = to_tegra_plane(plane);
496 struct tegra_dc *dc;
497 unsigned long flags;
498 u32 value;
499
500 /* rien ne va plus */
501 if (!old_state || !old_state->crtc)
502 return;
503
504 dc = to_tegra_dc(old_state->crtc);
505
506 spin_lock_irqsave(&dc->lock, flags);
507
508 value = WINDOW_A_SELECT << p->index;
509 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
510
511 value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
512 value &= ~WIN_ENABLE;
513 tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
514
515 tegra_dc_window_commit(dc, p->index);
516
517 spin_unlock_irqrestore(&dc->lock, flags);
Thierry Redingc7679302014-10-21 13:51:53 +0200518}
519
Thierry Reding4aa3df72014-11-24 16:27:13 +0100520static const struct drm_plane_helper_funcs tegra_primary_plane_helper_funcs = {
521 .prepare_fb = tegra_plane_prepare_fb,
522 .cleanup_fb = tegra_plane_cleanup_fb,
523 .atomic_check = tegra_plane_atomic_check,
524 .atomic_update = tegra_plane_atomic_update,
525 .atomic_disable = tegra_plane_atomic_disable,
Thierry Redingc7679302014-10-21 13:51:53 +0200526};
527
528static struct drm_plane *tegra_dc_primary_plane_create(struct drm_device *drm,
529 struct tegra_dc *dc)
530{
Thierry Reding518e6222014-12-16 18:04:08 +0100531 /*
532 * Ideally this would use drm_crtc_mask(), but that would require the
533 * CRTC to already be in the mode_config's list of CRTCs. However, it
534 * will only be added to that list in the drm_crtc_init_with_planes()
535 * (in tegra_dc_init()), which in turn requires registration of these
536 * planes. So we have ourselves a nice little chicken and egg problem
537 * here.
538 *
539 * We work around this by manually creating the mask from the number
540 * of CRTCs that have been registered, and should therefore always be
541 * the same as drm_crtc_index() after registration.
542 */
543 unsigned long possible_crtcs = 1 << drm->mode_config.num_crtc;
Thierry Redingc7679302014-10-21 13:51:53 +0200544 struct tegra_plane *plane;
545 unsigned int num_formats;
546 const u32 *formats;
547 int err;
548
549 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
550 if (!plane)
551 return ERR_PTR(-ENOMEM);
552
553 num_formats = ARRAY_SIZE(tegra_primary_plane_formats);
554 formats = tegra_primary_plane_formats;
555
Thierry Reding518e6222014-12-16 18:04:08 +0100556 err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
Thierry Redingc7679302014-10-21 13:51:53 +0200557 &tegra_primary_plane_funcs, formats,
558 num_formats, DRM_PLANE_TYPE_PRIMARY);
559 if (err < 0) {
560 kfree(plane);
561 return ERR_PTR(err);
562 }
563
Thierry Reding4aa3df72014-11-24 16:27:13 +0100564 drm_plane_helper_add(&plane->base, &tegra_primary_plane_helper_funcs);
565
Thierry Redingc7679302014-10-21 13:51:53 +0200566 return &plane->base;
567}
568
569static const u32 tegra_cursor_plane_formats[] = {
570 DRM_FORMAT_RGBA8888,
571};
572
Thierry Reding4aa3df72014-11-24 16:27:13 +0100573static int tegra_cursor_atomic_check(struct drm_plane *plane,
574 struct drm_plane_state *state)
Thierry Redingc7679302014-10-21 13:51:53 +0200575{
Thierry Reding4aa3df72014-11-24 16:27:13 +0100576 /* no need for further checks if the plane is being disabled */
577 if (!state->crtc)
578 return 0;
Thierry Redingc7679302014-10-21 13:51:53 +0200579
580 /* scaling not supported for cursor */
Thierry Reding4aa3df72014-11-24 16:27:13 +0100581 if ((state->src_w >> 16 != state->crtc_w) ||
582 (state->src_h >> 16 != state->crtc_h))
Thierry Redingc7679302014-10-21 13:51:53 +0200583 return -EINVAL;
584
585 /* only square cursors supported */
Thierry Reding4aa3df72014-11-24 16:27:13 +0100586 if (state->src_w != state->src_h)
Thierry Redingc7679302014-10-21 13:51:53 +0200587 return -EINVAL;
588
Thierry Reding4aa3df72014-11-24 16:27:13 +0100589 if (state->crtc_w != 32 && state->crtc_w != 64 &&
590 state->crtc_w != 128 && state->crtc_w != 256)
591 return -EINVAL;
592
593 return 0;
594}
595
596static void tegra_cursor_atomic_update(struct drm_plane *plane,
597 struct drm_plane_state *old_state)
598{
599 struct tegra_bo *bo = tegra_fb_get_plane(plane->state->fb, 0);
600 struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
601 struct drm_plane_state *state = plane->state;
602 u32 value = CURSOR_CLIP_DISPLAY;
603
604 /* rien ne va plus */
605 if (!plane->state->crtc || !plane->state->fb)
606 return;
607
608 switch (state->crtc_w) {
Thierry Redingc7679302014-10-21 13:51:53 +0200609 case 32:
610 value |= CURSOR_SIZE_32x32;
611 break;
612
613 case 64:
614 value |= CURSOR_SIZE_64x64;
615 break;
616
617 case 128:
618 value |= CURSOR_SIZE_128x128;
619 break;
620
621 case 256:
622 value |= CURSOR_SIZE_256x256;
623 break;
624
625 default:
Thierry Reding4aa3df72014-11-24 16:27:13 +0100626 WARN(1, "cursor size %ux%u not supported\n", state->crtc_w,
627 state->crtc_h);
628 return;
Thierry Redingc7679302014-10-21 13:51:53 +0200629 }
630
631 value |= (bo->paddr >> 10) & 0x3fffff;
632 tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR);
633
634#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
635 value = (bo->paddr >> 32) & 0x3;
636 tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR_HI);
637#endif
638
639 /* enable cursor and set blend mode */
640 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
641 value |= CURSOR_ENABLE;
642 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
643
644 value = tegra_dc_readl(dc, DC_DISP_BLEND_CURSOR_CONTROL);
645 value &= ~CURSOR_DST_BLEND_MASK;
646 value &= ~CURSOR_SRC_BLEND_MASK;
647 value |= CURSOR_MODE_NORMAL;
648 value |= CURSOR_DST_BLEND_NEG_K1_TIMES_SRC;
649 value |= CURSOR_SRC_BLEND_K1_TIMES_SRC;
650 value |= CURSOR_ALPHA;
651 tegra_dc_writel(dc, value, DC_DISP_BLEND_CURSOR_CONTROL);
652
653 /* position the cursor */
Thierry Reding4aa3df72014-11-24 16:27:13 +0100654 value = (state->crtc_y & 0x3fff) << 16 | (state->crtc_x & 0x3fff);
Thierry Redingc7679302014-10-21 13:51:53 +0200655 tegra_dc_writel(dc, value, DC_DISP_CURSOR_POSITION);
656
657 /* apply changes */
658 tegra_dc_cursor_commit(dc);
659 tegra_dc_commit(dc);
Thierry Redingc7679302014-10-21 13:51:53 +0200660}
661
Thierry Reding4aa3df72014-11-24 16:27:13 +0100662static void tegra_cursor_atomic_disable(struct drm_plane *plane,
663 struct drm_plane_state *old_state)
Thierry Redingc7679302014-10-21 13:51:53 +0200664{
Thierry Reding4aa3df72014-11-24 16:27:13 +0100665 struct tegra_dc *dc;
Thierry Redingc7679302014-10-21 13:51:53 +0200666 u32 value;
667
Thierry Reding4aa3df72014-11-24 16:27:13 +0100668 /* rien ne va plus */
669 if (!old_state || !old_state->crtc)
670 return;
671
672 dc = to_tegra_dc(old_state->crtc);
Thierry Redingc7679302014-10-21 13:51:53 +0200673
674 value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
675 value &= ~CURSOR_ENABLE;
676 tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
677
678 tegra_dc_cursor_commit(dc);
679 tegra_dc_commit(dc);
Thierry Redingc7679302014-10-21 13:51:53 +0200680}
681
682static const struct drm_plane_funcs tegra_cursor_plane_funcs = {
Thierry Reding4aa3df72014-11-24 16:27:13 +0100683 .update_plane = drm_plane_helper_update,
684 .disable_plane = drm_plane_helper_disable,
Thierry Redingc7679302014-10-21 13:51:53 +0200685 .destroy = tegra_plane_destroy,
Thierry Reding4aa3df72014-11-24 16:27:13 +0100686 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
687};
688
689static const struct drm_plane_helper_funcs tegra_cursor_plane_helper_funcs = {
690 .prepare_fb = tegra_plane_prepare_fb,
691 .cleanup_fb = tegra_plane_cleanup_fb,
692 .atomic_check = tegra_cursor_atomic_check,
693 .atomic_update = tegra_cursor_atomic_update,
694 .atomic_disable = tegra_cursor_atomic_disable,
Thierry Redingc7679302014-10-21 13:51:53 +0200695};
696
697static struct drm_plane *tegra_dc_cursor_plane_create(struct drm_device *drm,
698 struct tegra_dc *dc)
699{
700 struct tegra_plane *plane;
701 unsigned int num_formats;
702 const u32 *formats;
703 int err;
704
705 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
706 if (!plane)
707 return ERR_PTR(-ENOMEM);
708
709 num_formats = ARRAY_SIZE(tegra_cursor_plane_formats);
710 formats = tegra_cursor_plane_formats;
711
712 err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
713 &tegra_cursor_plane_funcs, formats,
714 num_formats, DRM_PLANE_TYPE_CURSOR);
715 if (err < 0) {
716 kfree(plane);
717 return ERR_PTR(err);
718 }
719
Thierry Reding4aa3df72014-11-24 16:27:13 +0100720 drm_plane_helper_add(&plane->base, &tegra_cursor_plane_helper_funcs);
721
Thierry Redingc7679302014-10-21 13:51:53 +0200722 return &plane->base;
723}
724
Thierry Redingc7679302014-10-21 13:51:53 +0200725static void tegra_overlay_plane_destroy(struct drm_plane *plane)
Thierry Redingf34bc782012-11-04 21:47:13 +0100726{
Thierry Redingc7679302014-10-21 13:51:53 +0200727 tegra_plane_destroy(plane);
Thierry Redingf34bc782012-11-04 21:47:13 +0100728}
729
Thierry Redingc7679302014-10-21 13:51:53 +0200730static const struct drm_plane_funcs tegra_overlay_plane_funcs = {
Thierry Reding4aa3df72014-11-24 16:27:13 +0100731 .update_plane = drm_plane_helper_update,
732 .disable_plane = drm_plane_helper_disable,
Thierry Redingc7679302014-10-21 13:51:53 +0200733 .destroy = tegra_overlay_plane_destroy,
Thierry Reding4aa3df72014-11-24 16:27:13 +0100734 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
Thierry Redingf34bc782012-11-04 21:47:13 +0100735};
736
Thierry Redingc7679302014-10-21 13:51:53 +0200737static const uint32_t tegra_overlay_plane_formats[] = {
Thierry Redingdbe4d9a2013-03-22 15:37:30 +0100738 DRM_FORMAT_XBGR8888,
Thierry Redingf34bc782012-11-04 21:47:13 +0100739 DRM_FORMAT_XRGB8888,
Thierry Redingdbe4d9a2013-03-22 15:37:30 +0100740 DRM_FORMAT_RGB565,
Thierry Redingf34bc782012-11-04 21:47:13 +0100741 DRM_FORMAT_UYVY,
Thierry Redingf9253902014-01-29 20:31:17 +0100742 DRM_FORMAT_YUYV,
Thierry Redingf34bc782012-11-04 21:47:13 +0100743 DRM_FORMAT_YUV420,
744 DRM_FORMAT_YUV422,
745};
746
Thierry Reding4aa3df72014-11-24 16:27:13 +0100747static const struct drm_plane_helper_funcs tegra_overlay_plane_helper_funcs = {
748 .prepare_fb = tegra_plane_prepare_fb,
749 .cleanup_fb = tegra_plane_cleanup_fb,
750 .atomic_check = tegra_plane_atomic_check,
751 .atomic_update = tegra_plane_atomic_update,
752 .atomic_disable = tegra_plane_atomic_disable,
753};
754
Thierry Redingc7679302014-10-21 13:51:53 +0200755static struct drm_plane *tegra_dc_overlay_plane_create(struct drm_device *drm,
756 struct tegra_dc *dc,
757 unsigned int index)
758{
759 struct tegra_plane *plane;
760 unsigned int num_formats;
761 const u32 *formats;
762 int err;
763
764 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
765 if (!plane)
766 return ERR_PTR(-ENOMEM);
767
768 plane->index = index;
769
770 num_formats = ARRAY_SIZE(tegra_overlay_plane_formats);
771 formats = tegra_overlay_plane_formats;
772
773 err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
774 &tegra_overlay_plane_funcs, formats,
775 num_formats, DRM_PLANE_TYPE_OVERLAY);
776 if (err < 0) {
777 kfree(plane);
778 return ERR_PTR(err);
779 }
780
Thierry Reding4aa3df72014-11-24 16:27:13 +0100781 drm_plane_helper_add(&plane->base, &tegra_overlay_plane_helper_funcs);
782
Thierry Redingc7679302014-10-21 13:51:53 +0200783 return &plane->base;
784}
785
Thierry Redingf34bc782012-11-04 21:47:13 +0100786static int tegra_dc_add_planes(struct drm_device *drm, struct tegra_dc *dc)
787{
Thierry Redingc7679302014-10-21 13:51:53 +0200788 struct drm_plane *plane;
Thierry Redingf34bc782012-11-04 21:47:13 +0100789 unsigned int i;
Thierry Redingf34bc782012-11-04 21:47:13 +0100790
791 for (i = 0; i < 2; i++) {
Thierry Redingc7679302014-10-21 13:51:53 +0200792 plane = tegra_dc_overlay_plane_create(drm, dc, 1 + i);
793 if (IS_ERR(plane))
794 return PTR_ERR(plane);
Thierry Redingf34bc782012-11-04 21:47:13 +0100795 }
796
797 return 0;
798}
799
Thierry Reding23fb4742012-11-28 11:38:24 +0100800static int tegra_dc_set_base(struct tegra_dc *dc, int x, int y,
801 struct drm_framebuffer *fb)
802{
Arto Merilainende2ba662013-03-22 16:34:08 +0200803 struct tegra_bo *bo = tegra_fb_get_plane(fb, 0);
Thierry Redingdb7fbdf2013-10-07 09:47:58 +0200804 unsigned int h_offset = 0, v_offset = 0;
Thierry Redingc134f012014-06-03 14:48:12 +0200805 struct tegra_bo_tiling tiling;
Sean Paul93396d02014-11-19 13:04:49 -0500806 unsigned long value, flags;
Thierry Redingf9253902014-01-29 20:31:17 +0100807 unsigned int format, swap;
Thierry Redingc134f012014-06-03 14:48:12 +0200808 int err;
809
810 err = tegra_fb_get_tiling(fb, &tiling);
811 if (err < 0)
812 return err;
Thierry Reding23fb4742012-11-28 11:38:24 +0100813
Sean Paul93396d02014-11-19 13:04:49 -0500814 spin_lock_irqsave(&dc->lock, flags);
815
Thierry Reding23fb4742012-11-28 11:38:24 +0100816 tegra_dc_writel(dc, WINDOW_A_SELECT, DC_CMD_DISPLAY_WINDOW_HEADER);
817
818 value = fb->offsets[0] + y * fb->pitches[0] +
819 x * fb->bits_per_pixel / 8;
820
Arto Merilainende2ba662013-03-22 16:34:08 +0200821 tegra_dc_writel(dc, bo->paddr + value, DC_WINBUF_START_ADDR);
Thierry Reding23fb4742012-11-28 11:38:24 +0100822 tegra_dc_writel(dc, fb->pitches[0], DC_WIN_LINE_STRIDE);
Thierry Redingf9253902014-01-29 20:31:17 +0100823
824 format = tegra_dc_format(fb->pixel_format, &swap);
Thierry Redinged683ae2013-04-22 21:31:15 +0200825 tegra_dc_writel(dc, format, DC_WIN_COLOR_DEPTH);
Thierry Redingf9253902014-01-29 20:31:17 +0100826 tegra_dc_writel(dc, swap, DC_WIN_BYTE_SWAP);
Thierry Reding23fb4742012-11-28 11:38:24 +0100827
Thierry Redingc134f012014-06-03 14:48:12 +0200828 if (dc->soc->supports_block_linear) {
829 unsigned long height = tiling.value;
Thierry Reding773af772013-10-04 22:34:01 +0200830
Thierry Redingc134f012014-06-03 14:48:12 +0200831 switch (tiling.mode) {
832 case TEGRA_BO_TILING_MODE_PITCH:
833 value = DC_WINBUF_SURFACE_KIND_PITCH;
834 break;
835
836 case TEGRA_BO_TILING_MODE_TILED:
837 value = DC_WINBUF_SURFACE_KIND_TILED;
838 break;
839
840 case TEGRA_BO_TILING_MODE_BLOCK:
841 value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
842 DC_WINBUF_SURFACE_KIND_BLOCK;
843 break;
844 }
845
846 tegra_dc_writel(dc, value, DC_WINBUF_SURFACE_KIND);
847 } else {
848 switch (tiling.mode) {
849 case TEGRA_BO_TILING_MODE_PITCH:
850 value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
851 DC_WIN_BUFFER_ADDR_MODE_LINEAR;
852 break;
853
854 case TEGRA_BO_TILING_MODE_TILED:
855 value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
856 DC_WIN_BUFFER_ADDR_MODE_TILE;
857 break;
858
859 case TEGRA_BO_TILING_MODE_BLOCK:
860 DRM_ERROR("hardware doesn't support block linear mode\n");
Sean Paul93396d02014-11-19 13:04:49 -0500861 spin_unlock_irqrestore(&dc->lock, flags);
Thierry Redingc134f012014-06-03 14:48:12 +0200862 return -EINVAL;
863 }
864
865 tegra_dc_writel(dc, value, DC_WIN_BUFFER_ADDR_MODE);
866 }
Thierry Reding773af772013-10-04 22:34:01 +0200867
Thierry Redingdb7fbdf2013-10-07 09:47:58 +0200868 /* make sure bottom-up buffers are properly displayed */
869 if (tegra_fb_is_bottom_up(fb)) {
870 value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
Thierry Redingeba66502014-02-25 12:04:06 +0100871 value |= V_DIRECTION;
Thierry Redingdb7fbdf2013-10-07 09:47:58 +0200872 tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
873
874 v_offset += fb->height - 1;
875 } else {
876 value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
Thierry Redingeba66502014-02-25 12:04:06 +0100877 value &= ~V_DIRECTION;
Thierry Redingdb7fbdf2013-10-07 09:47:58 +0200878 tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
879 }
880
881 tegra_dc_writel(dc, h_offset, DC_WINBUF_ADDR_H_OFFSET);
882 tegra_dc_writel(dc, v_offset, DC_WINBUF_ADDR_V_OFFSET);
883
Thierry Reding23fb4742012-11-28 11:38:24 +0100884 value = GENERAL_ACT_REQ | WIN_A_ACT_REQ;
Thierry Reding205d48e2014-10-21 13:41:46 +0200885 tegra_dc_writel(dc, value << 8, DC_CMD_STATE_CONTROL);
Thierry Reding23fb4742012-11-28 11:38:24 +0100886 tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
887
Sean Paul93396d02014-11-19 13:04:49 -0500888 spin_unlock_irqrestore(&dc->lock, flags);
889
Thierry Reding23fb4742012-11-28 11:38:24 +0100890 return 0;
891}
892
Thierry Reding6e5ff992012-11-28 11:45:47 +0100893void tegra_dc_enable_vblank(struct tegra_dc *dc)
894{
895 unsigned long value, flags;
896
897 spin_lock_irqsave(&dc->lock, flags);
898
899 value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
900 value |= VBLANK_INT;
901 tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
902
903 spin_unlock_irqrestore(&dc->lock, flags);
904}
905
906void tegra_dc_disable_vblank(struct tegra_dc *dc)
907{
908 unsigned long value, flags;
909
910 spin_lock_irqsave(&dc->lock, flags);
911
912 value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
913 value &= ~VBLANK_INT;
914 tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
915
916 spin_unlock_irqrestore(&dc->lock, flags);
917}
918
Thierry Reding3c03c462012-11-28 12:00:18 +0100919static void tegra_dc_finish_page_flip(struct tegra_dc *dc)
920{
921 struct drm_device *drm = dc->base.dev;
922 struct drm_crtc *crtc = &dc->base;
Thierry Reding3c03c462012-11-28 12:00:18 +0100923 unsigned long flags, base;
Arto Merilainende2ba662013-03-22 16:34:08 +0200924 struct tegra_bo *bo;
Thierry Reding3c03c462012-11-28 12:00:18 +0100925
Thierry Reding6b59cc12014-12-16 16:33:27 +0100926 spin_lock_irqsave(&drm->event_lock, flags);
927
928 if (!dc->event) {
929 spin_unlock_irqrestore(&drm->event_lock, flags);
Thierry Reding3c03c462012-11-28 12:00:18 +0100930 return;
Thierry Reding6b59cc12014-12-16 16:33:27 +0100931 }
Thierry Reding3c03c462012-11-28 12:00:18 +0100932
Matt Roperf4510a22014-04-01 15:22:40 -0700933 bo = tegra_fb_get_plane(crtc->primary->fb, 0);
Thierry Reding3c03c462012-11-28 12:00:18 +0100934
Dan Carpenter8643bc62015-01-07 14:01:26 +0300935 spin_lock(&dc->lock);
Sean Paul93396d02014-11-19 13:04:49 -0500936
Thierry Reding3c03c462012-11-28 12:00:18 +0100937 /* check if new start address has been latched */
Sean Paul93396d02014-11-19 13:04:49 -0500938 tegra_dc_writel(dc, WINDOW_A_SELECT, DC_CMD_DISPLAY_WINDOW_HEADER);
Thierry Reding3c03c462012-11-28 12:00:18 +0100939 tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
940 base = tegra_dc_readl(dc, DC_WINBUF_START_ADDR);
941 tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
942
Dan Carpenter8643bc62015-01-07 14:01:26 +0300943 spin_unlock(&dc->lock);
Sean Paul93396d02014-11-19 13:04:49 -0500944
Matt Roperf4510a22014-04-01 15:22:40 -0700945 if (base == bo->paddr + crtc->primary->fb->offsets[0]) {
Thierry Redinged7dae52014-12-16 16:03:13 +0100946 drm_crtc_send_vblank_event(crtc, dc->event);
947 drm_crtc_vblank_put(crtc);
Thierry Reding3c03c462012-11-28 12:00:18 +0100948 dc->event = NULL;
Thierry Reding3c03c462012-11-28 12:00:18 +0100949 }
Thierry Reding6b59cc12014-12-16 16:33:27 +0100950
951 spin_unlock_irqrestore(&drm->event_lock, flags);
Thierry Reding3c03c462012-11-28 12:00:18 +0100952}
953
954void tegra_dc_cancel_page_flip(struct drm_crtc *crtc, struct drm_file *file)
955{
956 struct tegra_dc *dc = to_tegra_dc(crtc);
957 struct drm_device *drm = crtc->dev;
958 unsigned long flags;
959
960 spin_lock_irqsave(&drm->event_lock, flags);
961
962 if (dc->event && dc->event->base.file_priv == file) {
963 dc->event->base.destroy(&dc->event->base);
Thierry Redinged7dae52014-12-16 16:03:13 +0100964 drm_crtc_vblank_put(crtc);
Thierry Reding3c03c462012-11-28 12:00:18 +0100965 dc->event = NULL;
966 }
967
968 spin_unlock_irqrestore(&drm->event_lock, flags);
969}
970
971static int tegra_dc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
Dave Airliea5b6f742013-09-02 09:47:56 +1000972 struct drm_pending_vblank_event *event, uint32_t page_flip_flags)
Thierry Reding3c03c462012-11-28 12:00:18 +0100973{
Thierry Redinged7dae52014-12-16 16:03:13 +0100974 unsigned int pipe = drm_crtc_index(crtc);
Thierry Reding3c03c462012-11-28 12:00:18 +0100975 struct tegra_dc *dc = to_tegra_dc(crtc);
Thierry Reding3c03c462012-11-28 12:00:18 +0100976
977 if (dc->event)
978 return -EBUSY;
979
980 if (event) {
Thierry Redinged7dae52014-12-16 16:03:13 +0100981 event->pipe = pipe;
Thierry Reding3c03c462012-11-28 12:00:18 +0100982 dc->event = event;
Thierry Redinged7dae52014-12-16 16:03:13 +0100983 drm_crtc_vblank_get(crtc);
Thierry Reding3c03c462012-11-28 12:00:18 +0100984 }
985
986 tegra_dc_set_base(dc, 0, 0, fb);
Matt Roperf4510a22014-04-01 15:22:40 -0700987 crtc->primary->fb = fb;
Thierry Reding3c03c462012-11-28 12:00:18 +0100988
989 return 0;
990}
991
Thierry Redingf002abc2013-10-14 14:06:02 +0200992static void tegra_dc_destroy(struct drm_crtc *crtc)
993{
994 drm_crtc_cleanup(crtc);
Thierry Redingf002abc2013-10-14 14:06:02 +0200995}
996
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000997static const struct drm_crtc_funcs tegra_crtc_funcs = {
Thierry Reding3c03c462012-11-28 12:00:18 +0100998 .page_flip = tegra_dc_page_flip,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000999 .set_config = drm_crtc_helper_set_config,
Thierry Redingf002abc2013-10-14 14:06:02 +02001000 .destroy = tegra_dc_destroy,
Thierry Reding4aa3df72014-11-24 16:27:13 +01001001 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001002};
1003
Thierry Reding86df2562014-12-08 16:03:53 +01001004static void tegra_dc_stop(struct tegra_dc *dc)
1005{
1006 u32 value;
1007
1008 /* stop the display controller */
1009 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
1010 value &= ~DISP_CTRL_MODE_MASK;
1011 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
1012
1013 tegra_dc_commit(dc);
1014}
1015
1016static bool tegra_dc_idle(struct tegra_dc *dc)
1017{
1018 u32 value;
1019
1020 value = tegra_dc_readl_active(dc, DC_CMD_DISPLAY_COMMAND);
1021
1022 return (value & DISP_CTRL_MODE_MASK) == 0;
1023}
1024
1025static int tegra_dc_wait_idle(struct tegra_dc *dc, unsigned long timeout)
1026{
1027 timeout = jiffies + msecs_to_jiffies(timeout);
1028
1029 while (time_before(jiffies, timeout)) {
1030 if (tegra_dc_idle(dc))
1031 return 0;
1032
1033 usleep_range(1000, 2000);
1034 }
1035
1036 dev_dbg(dc->dev, "timeout waiting for DC to become idle\n");
1037 return -ETIMEDOUT;
1038}
1039
Thierry Redingf34bc782012-11-04 21:47:13 +01001040static void tegra_crtc_disable(struct drm_crtc *crtc)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001041{
Thierry Redingf002abc2013-10-14 14:06:02 +02001042 struct tegra_dc *dc = to_tegra_dc(crtc);
Thierry Reding3b0e5852014-12-16 18:30:16 +01001043 u32 value;
Thierry Redingf002abc2013-10-14 14:06:02 +02001044
Thierry Reding86df2562014-12-08 16:03:53 +01001045 if (!tegra_dc_idle(dc)) {
1046 tegra_dc_stop(dc);
1047
1048 /*
1049 * Ignore the return value, there isn't anything useful to do
1050 * in case this fails.
1051 */
1052 tegra_dc_wait_idle(dc, 100);
1053 }
Thierry Reding36904ad2014-11-21 17:35:54 +01001054
Thierry Reding3b0e5852014-12-16 18:30:16 +01001055 /*
1056 * This should really be part of the RGB encoder driver, but clearing
1057 * these bits has the side-effect of stopping the display controller.
1058 * When that happens no VBLANK interrupts will be raised. At the same
1059 * time the encoder is disabled before the display controller, so the
1060 * above code is always going to timeout waiting for the controller
1061 * to go idle.
1062 *
1063 * Given the close coupling between the RGB encoder and the display
1064 * controller doing it here is still kind of okay. None of the other
1065 * encoder drivers require these bits to be cleared.
1066 *
1067 * XXX: Perhaps given that the display controller is switched off at
1068 * this point anyway maybe clearing these bits isn't even useful for
1069 * the RGB encoder?
1070 */
1071 if (dc->rgb) {
1072 value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
1073 value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
1074 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE);
1075 tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
1076 }
1077
Thierry Reding8ff64c12014-10-08 14:48:51 +02001078 drm_crtc_vblank_off(crtc);
Thierry Redingc7679302014-10-21 13:51:53 +02001079 tegra_dc_commit(dc);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001080}
1081
1082static bool tegra_crtc_mode_fixup(struct drm_crtc *crtc,
1083 const struct drm_display_mode *mode,
1084 struct drm_display_mode *adjusted)
1085{
1086 return true;
1087}
1088
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001089static int tegra_dc_set_timings(struct tegra_dc *dc,
1090 struct drm_display_mode *mode)
1091{
Thierry Reding0444c0f2014-04-16 09:22:38 +02001092 unsigned int h_ref_to_sync = 1;
1093 unsigned int v_ref_to_sync = 1;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001094 unsigned long value;
1095
1096 tegra_dc_writel(dc, 0x0, DC_DISP_DISP_TIMING_OPTIONS);
1097
1098 value = (v_ref_to_sync << 16) | h_ref_to_sync;
1099 tegra_dc_writel(dc, value, DC_DISP_REF_TO_SYNC);
1100
1101 value = ((mode->vsync_end - mode->vsync_start) << 16) |
1102 ((mode->hsync_end - mode->hsync_start) << 0);
1103 tegra_dc_writel(dc, value, DC_DISP_SYNC_WIDTH);
1104
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001105 value = ((mode->vtotal - mode->vsync_end) << 16) |
1106 ((mode->htotal - mode->hsync_end) << 0);
Lucas Stach40495082012-12-19 21:38:52 +00001107 tegra_dc_writel(dc, value, DC_DISP_BACK_PORCH);
1108
1109 value = ((mode->vsync_start - mode->vdisplay) << 16) |
1110 ((mode->hsync_start - mode->hdisplay) << 0);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001111 tegra_dc_writel(dc, value, DC_DISP_FRONT_PORCH);
1112
1113 value = (mode->vdisplay << 16) | mode->hdisplay;
1114 tegra_dc_writel(dc, value, DC_DISP_ACTIVE);
1115
1116 return 0;
1117}
1118
Thierry Redingc5a107d2014-12-02 15:15:06 +01001119int tegra_dc_setup_clock(struct tegra_dc *dc, struct clk *parent,
1120 unsigned long pclk, unsigned int div)
1121{
1122 u32 value;
1123 int err;
1124
1125 err = clk_set_parent(dc->clk, parent);
1126 if (err < 0) {
1127 dev_err(dc->dev, "failed to set parent clock: %d\n", err);
1128 return err;
1129 }
1130
1131 DRM_DEBUG_KMS("rate: %lu, div: %u\n", clk_get_rate(dc->clk), div);
1132
1133 value = SHIFT_CLK_DIVIDER(div) | PIXEL_CLK_DIVIDER_PCD1;
1134 tegra_dc_writel(dc, value, DC_DISP_DISP_CLOCK_CONTROL);
1135
1136 return 0;
1137}
1138
Thierry Reding4aa3df72014-11-24 16:27:13 +01001139static void tegra_crtc_mode_set_nofb(struct drm_crtc *crtc)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001140{
Thierry Reding4aa3df72014-11-24 16:27:13 +01001141 struct drm_display_mode *mode = &crtc->state->adjusted_mode;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001142 struct tegra_dc *dc = to_tegra_dc(crtc);
Thierry Redingdbb3f2f2014-03-26 12:32:14 +01001143 u32 value;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001144
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001145 /* program display mode */
1146 tegra_dc_set_timings(dc, mode);
1147
Thierry Reding42d06592014-12-08 15:45:39 +01001148 if (dc->soc->supports_border_color)
1149 tegra_dc_writel(dc, 0, DC_DISP_BORDER_COLOR);
1150
Thierry Reding8620fc62013-12-12 11:03:59 +01001151 /* interlacing isn't supported yet, so disable it */
1152 if (dc->soc->supports_interlacing) {
1153 value = tegra_dc_readl(dc, DC_DISP_INTERLACE_CONTROL);
1154 value &= ~INTERLACE_ENABLE;
1155 tegra_dc_writel(dc, value, DC_DISP_INTERLACE_CONTROL);
1156 }
Thierry Reding23fb4742012-11-28 11:38:24 +01001157}
1158
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001159static void tegra_crtc_prepare(struct drm_crtc *crtc)
1160{
1161 struct tegra_dc *dc = to_tegra_dc(crtc);
1162 unsigned int syncpt;
1163 unsigned long value;
1164
Thierry Reding8ff64c12014-10-08 14:48:51 +02001165 drm_crtc_vblank_off(crtc);
1166
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001167 if (dc->pipe)
1168 syncpt = SYNCPT_VBLANK1;
1169 else
1170 syncpt = SYNCPT_VBLANK0;
1171
1172 /* initialize display controller */
1173 tegra_dc_writel(dc, 0x00000100, DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
1174 tegra_dc_writel(dc, 0x100 | syncpt, DC_CMD_CONT_SYNCPT_VSYNC);
1175
1176 value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT | WIN_A_OF_INT;
1177 tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
1178
1179 value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
1180 WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
1181 tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
1182
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001183 /* initialize timer */
1184 value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(0x20) |
1185 WINDOW_B_THRESHOLD(0x20) | WINDOW_C_THRESHOLD(0x20);
1186 tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY);
1187
1188 value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(1) |
1189 WINDOW_B_THRESHOLD(1) | WINDOW_C_THRESHOLD(1);
1190 tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
1191
1192 value = VBLANK_INT | WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001193 tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
Thierry Reding6e5ff992012-11-28 11:45:47 +01001194
1195 value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT;
1196 tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001197}
1198
1199static void tegra_crtc_commit(struct drm_crtc *crtc)
1200{
1201 struct tegra_dc *dc = to_tegra_dc(crtc);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001202
Thierry Reding8ff64c12014-10-08 14:48:51 +02001203 drm_crtc_vblank_on(crtc);
Thierry Reding205d48e2014-10-21 13:41:46 +02001204 tegra_dc_commit(dc);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001205}
1206
Thierry Reding4aa3df72014-11-24 16:27:13 +01001207static int tegra_crtc_atomic_check(struct drm_crtc *crtc,
1208 struct drm_crtc_state *state)
1209{
1210 return 0;
1211}
1212
1213static void tegra_crtc_atomic_begin(struct drm_crtc *crtc)
1214{
1215}
1216
1217static void tegra_crtc_atomic_flush(struct drm_crtc *crtc)
1218{
1219}
1220
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001221static const struct drm_crtc_helper_funcs tegra_crtc_helper_funcs = {
Thierry Redingf34bc782012-11-04 21:47:13 +01001222 .disable = tegra_crtc_disable,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001223 .mode_fixup = tegra_crtc_mode_fixup,
Thierry Reding4aa3df72014-11-24 16:27:13 +01001224 .mode_set = drm_helper_crtc_mode_set,
1225 .mode_set_nofb = tegra_crtc_mode_set_nofb,
1226 .mode_set_base = drm_helper_crtc_mode_set_base,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001227 .prepare = tegra_crtc_prepare,
1228 .commit = tegra_crtc_commit,
Thierry Reding4aa3df72014-11-24 16:27:13 +01001229 .atomic_check = tegra_crtc_atomic_check,
1230 .atomic_begin = tegra_crtc_atomic_begin,
1231 .atomic_flush = tegra_crtc_atomic_flush,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001232};
1233
Thierry Reding6e5ff992012-11-28 11:45:47 +01001234static irqreturn_t tegra_dc_irq(int irq, void *data)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001235{
1236 struct tegra_dc *dc = data;
1237 unsigned long status;
1238
1239 status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
1240 tegra_dc_writel(dc, status, DC_CMD_INT_STATUS);
1241
1242 if (status & FRAME_END_INT) {
1243 /*
1244 dev_dbg(dc->dev, "%s(): frame end\n", __func__);
1245 */
1246 }
1247
1248 if (status & VBLANK_INT) {
1249 /*
1250 dev_dbg(dc->dev, "%s(): vertical blank\n", __func__);
1251 */
Thierry Redinged7dae52014-12-16 16:03:13 +01001252 drm_crtc_handle_vblank(&dc->base);
Thierry Reding3c03c462012-11-28 12:00:18 +01001253 tegra_dc_finish_page_flip(dc);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001254 }
1255
1256 if (status & (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT)) {
1257 /*
1258 dev_dbg(dc->dev, "%s(): underflow\n", __func__);
1259 */
1260 }
1261
1262 return IRQ_HANDLED;
1263}
1264
1265static int tegra_dc_show_regs(struct seq_file *s, void *data)
1266{
1267 struct drm_info_node *node = s->private;
1268 struct tegra_dc *dc = node->info_ent->data;
1269
1270#define DUMP_REG(name) \
Thierry Reding03a60562014-10-21 13:48:48 +02001271 seq_printf(s, "%-40s %#05x %08x\n", #name, name, \
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001272 tegra_dc_readl(dc, name))
1273
1274 DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT);
1275 DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
1276 DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_ERROR);
1277 DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT);
1278 DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_CNTRL);
1279 DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_ERROR);
1280 DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT);
1281 DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_CNTRL);
1282 DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_ERROR);
1283 DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT);
1284 DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_CNTRL);
1285 DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_ERROR);
1286 DUMP_REG(DC_CMD_CONT_SYNCPT_VSYNC);
1287 DUMP_REG(DC_CMD_DISPLAY_COMMAND_OPTION0);
1288 DUMP_REG(DC_CMD_DISPLAY_COMMAND);
1289 DUMP_REG(DC_CMD_SIGNAL_RAISE);
1290 DUMP_REG(DC_CMD_DISPLAY_POWER_CONTROL);
1291 DUMP_REG(DC_CMD_INT_STATUS);
1292 DUMP_REG(DC_CMD_INT_MASK);
1293 DUMP_REG(DC_CMD_INT_ENABLE);
1294 DUMP_REG(DC_CMD_INT_TYPE);
1295 DUMP_REG(DC_CMD_INT_POLARITY);
1296 DUMP_REG(DC_CMD_SIGNAL_RAISE1);
1297 DUMP_REG(DC_CMD_SIGNAL_RAISE2);
1298 DUMP_REG(DC_CMD_SIGNAL_RAISE3);
1299 DUMP_REG(DC_CMD_STATE_ACCESS);
1300 DUMP_REG(DC_CMD_STATE_CONTROL);
1301 DUMP_REG(DC_CMD_DISPLAY_WINDOW_HEADER);
1302 DUMP_REG(DC_CMD_REG_ACT_CONTROL);
1303 DUMP_REG(DC_COM_CRC_CONTROL);
1304 DUMP_REG(DC_COM_CRC_CHECKSUM);
1305 DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(0));
1306 DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(1));
1307 DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(2));
1308 DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(3));
1309 DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(0));
1310 DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(1));
1311 DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(2));
1312 DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(3));
1313 DUMP_REG(DC_COM_PIN_OUTPUT_DATA(0));
1314 DUMP_REG(DC_COM_PIN_OUTPUT_DATA(1));
1315 DUMP_REG(DC_COM_PIN_OUTPUT_DATA(2));
1316 DUMP_REG(DC_COM_PIN_OUTPUT_DATA(3));
1317 DUMP_REG(DC_COM_PIN_INPUT_ENABLE(0));
1318 DUMP_REG(DC_COM_PIN_INPUT_ENABLE(1));
1319 DUMP_REG(DC_COM_PIN_INPUT_ENABLE(2));
1320 DUMP_REG(DC_COM_PIN_INPUT_ENABLE(3));
1321 DUMP_REG(DC_COM_PIN_INPUT_DATA(0));
1322 DUMP_REG(DC_COM_PIN_INPUT_DATA(1));
1323 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(0));
1324 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(1));
1325 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(2));
1326 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(3));
1327 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(4));
1328 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(5));
1329 DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(6));
1330 DUMP_REG(DC_COM_PIN_MISC_CONTROL);
1331 DUMP_REG(DC_COM_PIN_PM0_CONTROL);
1332 DUMP_REG(DC_COM_PIN_PM0_DUTY_CYCLE);
1333 DUMP_REG(DC_COM_PIN_PM1_CONTROL);
1334 DUMP_REG(DC_COM_PIN_PM1_DUTY_CYCLE);
1335 DUMP_REG(DC_COM_SPI_CONTROL);
1336 DUMP_REG(DC_COM_SPI_START_BYTE);
1337 DUMP_REG(DC_COM_HSPI_WRITE_DATA_AB);
1338 DUMP_REG(DC_COM_HSPI_WRITE_DATA_CD);
1339 DUMP_REG(DC_COM_HSPI_CS_DC);
1340 DUMP_REG(DC_COM_SCRATCH_REGISTER_A);
1341 DUMP_REG(DC_COM_SCRATCH_REGISTER_B);
1342 DUMP_REG(DC_COM_GPIO_CTRL);
1343 DUMP_REG(DC_COM_GPIO_DEBOUNCE_COUNTER);
1344 DUMP_REG(DC_COM_CRC_CHECKSUM_LATCHED);
1345 DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS0);
1346 DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS1);
1347 DUMP_REG(DC_DISP_DISP_WIN_OPTIONS);
1348 DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY);
1349 DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
1350 DUMP_REG(DC_DISP_DISP_TIMING_OPTIONS);
1351 DUMP_REG(DC_DISP_REF_TO_SYNC);
1352 DUMP_REG(DC_DISP_SYNC_WIDTH);
1353 DUMP_REG(DC_DISP_BACK_PORCH);
1354 DUMP_REG(DC_DISP_ACTIVE);
1355 DUMP_REG(DC_DISP_FRONT_PORCH);
1356 DUMP_REG(DC_DISP_H_PULSE0_CONTROL);
1357 DUMP_REG(DC_DISP_H_PULSE0_POSITION_A);
1358 DUMP_REG(DC_DISP_H_PULSE0_POSITION_B);
1359 DUMP_REG(DC_DISP_H_PULSE0_POSITION_C);
1360 DUMP_REG(DC_DISP_H_PULSE0_POSITION_D);
1361 DUMP_REG(DC_DISP_H_PULSE1_CONTROL);
1362 DUMP_REG(DC_DISP_H_PULSE1_POSITION_A);
1363 DUMP_REG(DC_DISP_H_PULSE1_POSITION_B);
1364 DUMP_REG(DC_DISP_H_PULSE1_POSITION_C);
1365 DUMP_REG(DC_DISP_H_PULSE1_POSITION_D);
1366 DUMP_REG(DC_DISP_H_PULSE2_CONTROL);
1367 DUMP_REG(DC_DISP_H_PULSE2_POSITION_A);
1368 DUMP_REG(DC_DISP_H_PULSE2_POSITION_B);
1369 DUMP_REG(DC_DISP_H_PULSE2_POSITION_C);
1370 DUMP_REG(DC_DISP_H_PULSE2_POSITION_D);
1371 DUMP_REG(DC_DISP_V_PULSE0_CONTROL);
1372 DUMP_REG(DC_DISP_V_PULSE0_POSITION_A);
1373 DUMP_REG(DC_DISP_V_PULSE0_POSITION_B);
1374 DUMP_REG(DC_DISP_V_PULSE0_POSITION_C);
1375 DUMP_REG(DC_DISP_V_PULSE1_CONTROL);
1376 DUMP_REG(DC_DISP_V_PULSE1_POSITION_A);
1377 DUMP_REG(DC_DISP_V_PULSE1_POSITION_B);
1378 DUMP_REG(DC_DISP_V_PULSE1_POSITION_C);
1379 DUMP_REG(DC_DISP_V_PULSE2_CONTROL);
1380 DUMP_REG(DC_DISP_V_PULSE2_POSITION_A);
1381 DUMP_REG(DC_DISP_V_PULSE3_CONTROL);
1382 DUMP_REG(DC_DISP_V_PULSE3_POSITION_A);
1383 DUMP_REG(DC_DISP_M0_CONTROL);
1384 DUMP_REG(DC_DISP_M1_CONTROL);
1385 DUMP_REG(DC_DISP_DI_CONTROL);
1386 DUMP_REG(DC_DISP_PP_CONTROL);
1387 DUMP_REG(DC_DISP_PP_SELECT_A);
1388 DUMP_REG(DC_DISP_PP_SELECT_B);
1389 DUMP_REG(DC_DISP_PP_SELECT_C);
1390 DUMP_REG(DC_DISP_PP_SELECT_D);
1391 DUMP_REG(DC_DISP_DISP_CLOCK_CONTROL);
1392 DUMP_REG(DC_DISP_DISP_INTERFACE_CONTROL);
1393 DUMP_REG(DC_DISP_DISP_COLOR_CONTROL);
1394 DUMP_REG(DC_DISP_SHIFT_CLOCK_OPTIONS);
1395 DUMP_REG(DC_DISP_DATA_ENABLE_OPTIONS);
1396 DUMP_REG(DC_DISP_SERIAL_INTERFACE_OPTIONS);
1397 DUMP_REG(DC_DISP_LCD_SPI_OPTIONS);
1398 DUMP_REG(DC_DISP_BORDER_COLOR);
1399 DUMP_REG(DC_DISP_COLOR_KEY0_LOWER);
1400 DUMP_REG(DC_DISP_COLOR_KEY0_UPPER);
1401 DUMP_REG(DC_DISP_COLOR_KEY1_LOWER);
1402 DUMP_REG(DC_DISP_COLOR_KEY1_UPPER);
1403 DUMP_REG(DC_DISP_CURSOR_FOREGROUND);
1404 DUMP_REG(DC_DISP_CURSOR_BACKGROUND);
1405 DUMP_REG(DC_DISP_CURSOR_START_ADDR);
1406 DUMP_REG(DC_DISP_CURSOR_START_ADDR_NS);
1407 DUMP_REG(DC_DISP_CURSOR_POSITION);
1408 DUMP_REG(DC_DISP_CURSOR_POSITION_NS);
1409 DUMP_REG(DC_DISP_INIT_SEQ_CONTROL);
1410 DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_A);
1411 DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_B);
1412 DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_C);
1413 DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_D);
1414 DUMP_REG(DC_DISP_DC_MCCIF_FIFOCTRL);
1415 DUMP_REG(DC_DISP_MCCIF_DISPLAY0A_HYST);
1416 DUMP_REG(DC_DISP_MCCIF_DISPLAY0B_HYST);
1417 DUMP_REG(DC_DISP_MCCIF_DISPLAY1A_HYST);
1418 DUMP_REG(DC_DISP_MCCIF_DISPLAY1B_HYST);
1419 DUMP_REG(DC_DISP_DAC_CRT_CTRL);
1420 DUMP_REG(DC_DISP_DISP_MISC_CONTROL);
1421 DUMP_REG(DC_DISP_SD_CONTROL);
1422 DUMP_REG(DC_DISP_SD_CSC_COEFF);
1423 DUMP_REG(DC_DISP_SD_LUT(0));
1424 DUMP_REG(DC_DISP_SD_LUT(1));
1425 DUMP_REG(DC_DISP_SD_LUT(2));
1426 DUMP_REG(DC_DISP_SD_LUT(3));
1427 DUMP_REG(DC_DISP_SD_LUT(4));
1428 DUMP_REG(DC_DISP_SD_LUT(5));
1429 DUMP_REG(DC_DISP_SD_LUT(6));
1430 DUMP_REG(DC_DISP_SD_LUT(7));
1431 DUMP_REG(DC_DISP_SD_LUT(8));
1432 DUMP_REG(DC_DISP_SD_FLICKER_CONTROL);
1433 DUMP_REG(DC_DISP_DC_PIXEL_COUNT);
1434 DUMP_REG(DC_DISP_SD_HISTOGRAM(0));
1435 DUMP_REG(DC_DISP_SD_HISTOGRAM(1));
1436 DUMP_REG(DC_DISP_SD_HISTOGRAM(2));
1437 DUMP_REG(DC_DISP_SD_HISTOGRAM(3));
1438 DUMP_REG(DC_DISP_SD_HISTOGRAM(4));
1439 DUMP_REG(DC_DISP_SD_HISTOGRAM(5));
1440 DUMP_REG(DC_DISP_SD_HISTOGRAM(6));
1441 DUMP_REG(DC_DISP_SD_HISTOGRAM(7));
1442 DUMP_REG(DC_DISP_SD_BL_TF(0));
1443 DUMP_REG(DC_DISP_SD_BL_TF(1));
1444 DUMP_REG(DC_DISP_SD_BL_TF(2));
1445 DUMP_REG(DC_DISP_SD_BL_TF(3));
1446 DUMP_REG(DC_DISP_SD_BL_CONTROL);
1447 DUMP_REG(DC_DISP_SD_HW_K_VALUES);
1448 DUMP_REG(DC_DISP_SD_MAN_K_VALUES);
Thierry Redinge6876512013-12-20 13:58:33 +01001449 DUMP_REG(DC_DISP_CURSOR_START_ADDR_HI);
1450 DUMP_REG(DC_DISP_BLEND_CURSOR_CONTROL);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001451 DUMP_REG(DC_WIN_WIN_OPTIONS);
1452 DUMP_REG(DC_WIN_BYTE_SWAP);
1453 DUMP_REG(DC_WIN_BUFFER_CONTROL);
1454 DUMP_REG(DC_WIN_COLOR_DEPTH);
1455 DUMP_REG(DC_WIN_POSITION);
1456 DUMP_REG(DC_WIN_SIZE);
1457 DUMP_REG(DC_WIN_PRESCALED_SIZE);
1458 DUMP_REG(DC_WIN_H_INITIAL_DDA);
1459 DUMP_REG(DC_WIN_V_INITIAL_DDA);
1460 DUMP_REG(DC_WIN_DDA_INC);
1461 DUMP_REG(DC_WIN_LINE_STRIDE);
1462 DUMP_REG(DC_WIN_BUF_STRIDE);
1463 DUMP_REG(DC_WIN_UV_BUF_STRIDE);
1464 DUMP_REG(DC_WIN_BUFFER_ADDR_MODE);
1465 DUMP_REG(DC_WIN_DV_CONTROL);
1466 DUMP_REG(DC_WIN_BLEND_NOKEY);
1467 DUMP_REG(DC_WIN_BLEND_1WIN);
1468 DUMP_REG(DC_WIN_BLEND_2WIN_X);
1469 DUMP_REG(DC_WIN_BLEND_2WIN_Y);
Thierry Redingf34bc782012-11-04 21:47:13 +01001470 DUMP_REG(DC_WIN_BLEND_3WIN_XY);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001471 DUMP_REG(DC_WIN_HP_FETCH_CONTROL);
1472 DUMP_REG(DC_WINBUF_START_ADDR);
1473 DUMP_REG(DC_WINBUF_START_ADDR_NS);
1474 DUMP_REG(DC_WINBUF_START_ADDR_U);
1475 DUMP_REG(DC_WINBUF_START_ADDR_U_NS);
1476 DUMP_REG(DC_WINBUF_START_ADDR_V);
1477 DUMP_REG(DC_WINBUF_START_ADDR_V_NS);
1478 DUMP_REG(DC_WINBUF_ADDR_H_OFFSET);
1479 DUMP_REG(DC_WINBUF_ADDR_H_OFFSET_NS);
1480 DUMP_REG(DC_WINBUF_ADDR_V_OFFSET);
1481 DUMP_REG(DC_WINBUF_ADDR_V_OFFSET_NS);
1482 DUMP_REG(DC_WINBUF_UFLOW_STATUS);
1483 DUMP_REG(DC_WINBUF_AD_UFLOW_STATUS);
1484 DUMP_REG(DC_WINBUF_BD_UFLOW_STATUS);
1485 DUMP_REG(DC_WINBUF_CD_UFLOW_STATUS);
1486
1487#undef DUMP_REG
1488
1489 return 0;
1490}
1491
1492static struct drm_info_list debugfs_files[] = {
1493 { "regs", tegra_dc_show_regs, 0, NULL },
1494};
1495
1496static int tegra_dc_debugfs_init(struct tegra_dc *dc, struct drm_minor *minor)
1497{
1498 unsigned int i;
1499 char *name;
1500 int err;
1501
1502 name = kasprintf(GFP_KERNEL, "dc.%d", dc->pipe);
1503 dc->debugfs = debugfs_create_dir(name, minor->debugfs_root);
1504 kfree(name);
1505
1506 if (!dc->debugfs)
1507 return -ENOMEM;
1508
1509 dc->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
1510 GFP_KERNEL);
1511 if (!dc->debugfs_files) {
1512 err = -ENOMEM;
1513 goto remove;
1514 }
1515
1516 for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
1517 dc->debugfs_files[i].data = dc;
1518
1519 err = drm_debugfs_create_files(dc->debugfs_files,
1520 ARRAY_SIZE(debugfs_files),
1521 dc->debugfs, minor);
1522 if (err < 0)
1523 goto free;
1524
1525 dc->minor = minor;
1526
1527 return 0;
1528
1529free:
1530 kfree(dc->debugfs_files);
1531 dc->debugfs_files = NULL;
1532remove:
1533 debugfs_remove(dc->debugfs);
1534 dc->debugfs = NULL;
1535
1536 return err;
1537}
1538
1539static int tegra_dc_debugfs_exit(struct tegra_dc *dc)
1540{
1541 drm_debugfs_remove_files(dc->debugfs_files, ARRAY_SIZE(debugfs_files),
1542 dc->minor);
1543 dc->minor = NULL;
1544
1545 kfree(dc->debugfs_files);
1546 dc->debugfs_files = NULL;
1547
1548 debugfs_remove(dc->debugfs);
1549 dc->debugfs = NULL;
1550
1551 return 0;
1552}
1553
Thierry Reding53fa7f72013-09-24 15:35:40 +02001554static int tegra_dc_init(struct host1x_client *client)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001555{
Thierry Reding9910f5c2014-05-22 09:57:15 +02001556 struct drm_device *drm = dev_get_drvdata(client->parent);
Thierry Reding776dc382013-10-14 14:43:22 +02001557 struct tegra_dc *dc = host1x_client_to_dc(client);
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001558 struct tegra_drm *tegra = drm->dev_private;
Thierry Redingc7679302014-10-21 13:51:53 +02001559 struct drm_plane *primary = NULL;
1560 struct drm_plane *cursor = NULL;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001561 int err;
1562
Thierry Redingdf06b752014-06-26 21:41:53 +02001563 if (tegra->domain) {
1564 err = iommu_attach_device(tegra->domain, dc->dev);
1565 if (err < 0) {
1566 dev_err(dc->dev, "failed to attach to domain: %d\n",
1567 err);
1568 return err;
1569 }
1570
1571 dc->domain = tegra->domain;
1572 }
1573
Thierry Redingc7679302014-10-21 13:51:53 +02001574 primary = tegra_dc_primary_plane_create(drm, dc);
1575 if (IS_ERR(primary)) {
1576 err = PTR_ERR(primary);
1577 goto cleanup;
1578 }
1579
1580 if (dc->soc->supports_cursor) {
1581 cursor = tegra_dc_cursor_plane_create(drm, dc);
1582 if (IS_ERR(cursor)) {
1583 err = PTR_ERR(cursor);
1584 goto cleanup;
1585 }
1586 }
1587
1588 err = drm_crtc_init_with_planes(drm, &dc->base, primary, cursor,
1589 &tegra_crtc_funcs);
1590 if (err < 0)
1591 goto cleanup;
1592
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001593 drm_mode_crtc_set_gamma_size(&dc->base, 256);
1594 drm_crtc_helper_add(&dc->base, &tegra_crtc_helper_funcs);
1595
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001596 /*
1597 * Keep track of the minimum pitch alignment across all display
1598 * controllers.
1599 */
1600 if (dc->soc->pitch_align > tegra->pitch_align)
1601 tegra->pitch_align = dc->soc->pitch_align;
1602
Thierry Reding9910f5c2014-05-22 09:57:15 +02001603 err = tegra_dc_rgb_init(drm, dc);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001604 if (err < 0 && err != -ENODEV) {
1605 dev_err(dc->dev, "failed to initialize RGB output: %d\n", err);
Thierry Redingc7679302014-10-21 13:51:53 +02001606 goto cleanup;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001607 }
1608
Thierry Reding9910f5c2014-05-22 09:57:15 +02001609 err = tegra_dc_add_planes(drm, dc);
Thierry Redingf34bc782012-11-04 21:47:13 +01001610 if (err < 0)
Thierry Redingc7679302014-10-21 13:51:53 +02001611 goto cleanup;
Thierry Redingf34bc782012-11-04 21:47:13 +01001612
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001613 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
Thierry Reding9910f5c2014-05-22 09:57:15 +02001614 err = tegra_dc_debugfs_init(dc, drm->primary);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001615 if (err < 0)
1616 dev_err(dc->dev, "debugfs setup failed: %d\n", err);
1617 }
1618
Thierry Reding6e5ff992012-11-28 11:45:47 +01001619 err = devm_request_irq(dc->dev, dc->irq, tegra_dc_irq, 0,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001620 dev_name(dc->dev), dc);
1621 if (err < 0) {
1622 dev_err(dc->dev, "failed to request IRQ#%u: %d\n", dc->irq,
1623 err);
Thierry Redingc7679302014-10-21 13:51:53 +02001624 goto cleanup;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001625 }
1626
1627 return 0;
Thierry Redingc7679302014-10-21 13:51:53 +02001628
1629cleanup:
1630 if (cursor)
1631 drm_plane_cleanup(cursor);
1632
1633 if (primary)
1634 drm_plane_cleanup(primary);
1635
1636 if (tegra->domain) {
1637 iommu_detach_device(tegra->domain, dc->dev);
1638 dc->domain = NULL;
1639 }
1640
1641 return err;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001642}
1643
Thierry Reding53fa7f72013-09-24 15:35:40 +02001644static int tegra_dc_exit(struct host1x_client *client)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001645{
Thierry Reding776dc382013-10-14 14:43:22 +02001646 struct tegra_dc *dc = host1x_client_to_dc(client);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001647 int err;
1648
1649 devm_free_irq(dc->dev, dc->irq, dc);
1650
1651 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1652 err = tegra_dc_debugfs_exit(dc);
1653 if (err < 0)
1654 dev_err(dc->dev, "debugfs cleanup failed: %d\n", err);
1655 }
1656
1657 err = tegra_dc_rgb_exit(dc);
1658 if (err) {
1659 dev_err(dc->dev, "failed to shutdown RGB output: %d\n", err);
1660 return err;
1661 }
1662
Thierry Redingdf06b752014-06-26 21:41:53 +02001663 if (dc->domain) {
1664 iommu_detach_device(dc->domain, dc->dev);
1665 dc->domain = NULL;
1666 }
1667
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001668 return 0;
1669}
1670
1671static const struct host1x_client_ops dc_client_ops = {
Thierry Reding53fa7f72013-09-24 15:35:40 +02001672 .init = tegra_dc_init,
1673 .exit = tegra_dc_exit,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001674};
1675
Thierry Reding8620fc62013-12-12 11:03:59 +01001676static const struct tegra_dc_soc_info tegra20_dc_soc_info = {
Thierry Reding42d06592014-12-08 15:45:39 +01001677 .supports_border_color = true,
Thierry Reding8620fc62013-12-12 11:03:59 +01001678 .supports_interlacing = false,
Thierry Redinge6876512013-12-20 13:58:33 +01001679 .supports_cursor = false,
Thierry Redingc134f012014-06-03 14:48:12 +02001680 .supports_block_linear = false,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001681 .pitch_align = 8,
Thierry Reding9c012702014-07-07 15:32:53 +02001682 .has_powergate = false,
Thierry Reding8620fc62013-12-12 11:03:59 +01001683};
1684
1685static const struct tegra_dc_soc_info tegra30_dc_soc_info = {
Thierry Reding42d06592014-12-08 15:45:39 +01001686 .supports_border_color = true,
Thierry Reding8620fc62013-12-12 11:03:59 +01001687 .supports_interlacing = false,
Thierry Redinge6876512013-12-20 13:58:33 +01001688 .supports_cursor = false,
Thierry Redingc134f012014-06-03 14:48:12 +02001689 .supports_block_linear = false,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001690 .pitch_align = 8,
Thierry Reding9c012702014-07-07 15:32:53 +02001691 .has_powergate = false,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001692};
1693
1694static const struct tegra_dc_soc_info tegra114_dc_soc_info = {
Thierry Reding42d06592014-12-08 15:45:39 +01001695 .supports_border_color = true,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001696 .supports_interlacing = false,
1697 .supports_cursor = false,
1698 .supports_block_linear = false,
1699 .pitch_align = 64,
Thierry Reding9c012702014-07-07 15:32:53 +02001700 .has_powergate = true,
Thierry Reding8620fc62013-12-12 11:03:59 +01001701};
1702
1703static const struct tegra_dc_soc_info tegra124_dc_soc_info = {
Thierry Reding42d06592014-12-08 15:45:39 +01001704 .supports_border_color = false,
Thierry Reding8620fc62013-12-12 11:03:59 +01001705 .supports_interlacing = true,
Thierry Redinge6876512013-12-20 13:58:33 +01001706 .supports_cursor = true,
Thierry Redingc134f012014-06-03 14:48:12 +02001707 .supports_block_linear = true,
Thierry Redingd1f3e1e2014-07-11 08:29:14 +02001708 .pitch_align = 64,
Thierry Reding9c012702014-07-07 15:32:53 +02001709 .has_powergate = true,
Thierry Reding8620fc62013-12-12 11:03:59 +01001710};
1711
1712static const struct of_device_id tegra_dc_of_match[] = {
1713 {
1714 .compatible = "nvidia,tegra124-dc",
1715 .data = &tegra124_dc_soc_info,
1716 }, {
Thierry Reding9c012702014-07-07 15:32:53 +02001717 .compatible = "nvidia,tegra114-dc",
1718 .data = &tegra114_dc_soc_info,
1719 }, {
Thierry Reding8620fc62013-12-12 11:03:59 +01001720 .compatible = "nvidia,tegra30-dc",
1721 .data = &tegra30_dc_soc_info,
1722 }, {
1723 .compatible = "nvidia,tegra20-dc",
1724 .data = &tegra20_dc_soc_info,
1725 }, {
1726 /* sentinel */
1727 }
1728};
Stephen Warrenef707282014-06-18 16:21:55 -06001729MODULE_DEVICE_TABLE(of, tegra_dc_of_match);
Thierry Reding8620fc62013-12-12 11:03:59 +01001730
Thierry Reding13411dd2014-01-09 17:08:36 +01001731static int tegra_dc_parse_dt(struct tegra_dc *dc)
1732{
1733 struct device_node *np;
1734 u32 value = 0;
1735 int err;
1736
1737 err = of_property_read_u32(dc->dev->of_node, "nvidia,head", &value);
1738 if (err < 0) {
1739 dev_err(dc->dev, "missing \"nvidia,head\" property\n");
1740
1741 /*
1742 * If the nvidia,head property isn't present, try to find the
1743 * correct head number by looking up the position of this
1744 * display controller's node within the device tree. Assuming
1745 * that the nodes are ordered properly in the DTS file and
1746 * that the translation into a flattened device tree blob
1747 * preserves that ordering this will actually yield the right
1748 * head number.
1749 *
1750 * If those assumptions don't hold, this will still work for
1751 * cases where only a single display controller is used.
1752 */
1753 for_each_matching_node(np, tegra_dc_of_match) {
1754 if (np == dc->dev->of_node)
1755 break;
1756
1757 value++;
1758 }
1759 }
1760
1761 dc->pipe = value;
1762
1763 return 0;
1764}
1765
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001766static int tegra_dc_probe(struct platform_device *pdev)
1767{
Thierry Reding8620fc62013-12-12 11:03:59 +01001768 const struct of_device_id *id;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001769 struct resource *regs;
1770 struct tegra_dc *dc;
1771 int err;
1772
1773 dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
1774 if (!dc)
1775 return -ENOMEM;
1776
Thierry Reding8620fc62013-12-12 11:03:59 +01001777 id = of_match_node(tegra_dc_of_match, pdev->dev.of_node);
1778 if (!id)
1779 return -ENODEV;
1780
Thierry Reding6e5ff992012-11-28 11:45:47 +01001781 spin_lock_init(&dc->lock);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001782 INIT_LIST_HEAD(&dc->list);
1783 dc->dev = &pdev->dev;
Thierry Reding8620fc62013-12-12 11:03:59 +01001784 dc->soc = id->data;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001785
Thierry Reding13411dd2014-01-09 17:08:36 +01001786 err = tegra_dc_parse_dt(dc);
1787 if (err < 0)
1788 return err;
1789
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001790 dc->clk = devm_clk_get(&pdev->dev, NULL);
1791 if (IS_ERR(dc->clk)) {
1792 dev_err(&pdev->dev, "failed to get clock\n");
1793 return PTR_ERR(dc->clk);
1794 }
1795
Stephen Warrenca480802013-11-06 16:20:54 -07001796 dc->rst = devm_reset_control_get(&pdev->dev, "dc");
1797 if (IS_ERR(dc->rst)) {
1798 dev_err(&pdev->dev, "failed to get reset\n");
1799 return PTR_ERR(dc->rst);
1800 }
1801
Thierry Reding9c012702014-07-07 15:32:53 +02001802 if (dc->soc->has_powergate) {
1803 if (dc->pipe == 0)
1804 dc->powergate = TEGRA_POWERGATE_DIS;
1805 else
1806 dc->powergate = TEGRA_POWERGATE_DISB;
1807
1808 err = tegra_powergate_sequence_power_up(dc->powergate, dc->clk,
1809 dc->rst);
1810 if (err < 0) {
1811 dev_err(&pdev->dev, "failed to power partition: %d\n",
1812 err);
1813 return err;
1814 }
1815 } else {
1816 err = clk_prepare_enable(dc->clk);
1817 if (err < 0) {
1818 dev_err(&pdev->dev, "failed to enable clock: %d\n",
1819 err);
1820 return err;
1821 }
1822
1823 err = reset_control_deassert(dc->rst);
1824 if (err < 0) {
1825 dev_err(&pdev->dev, "failed to deassert reset: %d\n",
1826 err);
1827 return err;
1828 }
1829 }
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001830
1831 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Redingd4ed6022013-01-21 11:09:02 +01001832 dc->regs = devm_ioremap_resource(&pdev->dev, regs);
1833 if (IS_ERR(dc->regs))
1834 return PTR_ERR(dc->regs);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001835
1836 dc->irq = platform_get_irq(pdev, 0);
1837 if (dc->irq < 0) {
1838 dev_err(&pdev->dev, "failed to get IRQ\n");
1839 return -ENXIO;
1840 }
1841
Thierry Reding776dc382013-10-14 14:43:22 +02001842 INIT_LIST_HEAD(&dc->client.list);
1843 dc->client.ops = &dc_client_ops;
1844 dc->client.dev = &pdev->dev;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001845
1846 err = tegra_dc_rgb_probe(dc);
1847 if (err < 0 && err != -ENODEV) {
1848 dev_err(&pdev->dev, "failed to probe RGB output: %d\n", err);
1849 return err;
1850 }
1851
Thierry Reding776dc382013-10-14 14:43:22 +02001852 err = host1x_client_register(&dc->client);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001853 if (err < 0) {
1854 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
1855 err);
1856 return err;
1857 }
1858
1859 platform_set_drvdata(pdev, dc);
1860
1861 return 0;
1862}
1863
1864static int tegra_dc_remove(struct platform_device *pdev)
1865{
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001866 struct tegra_dc *dc = platform_get_drvdata(pdev);
1867 int err;
1868
Thierry Reding776dc382013-10-14 14:43:22 +02001869 err = host1x_client_unregister(&dc->client);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001870 if (err < 0) {
1871 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
1872 err);
1873 return err;
1874 }
1875
Thierry Reding59d29c02013-10-14 14:26:42 +02001876 err = tegra_dc_rgb_remove(dc);
1877 if (err < 0) {
1878 dev_err(&pdev->dev, "failed to remove RGB output: %d\n", err);
1879 return err;
1880 }
1881
Thierry Reding5482d752014-07-11 08:39:03 +02001882 reset_control_assert(dc->rst);
Thierry Reding9c012702014-07-07 15:32:53 +02001883
1884 if (dc->soc->has_powergate)
1885 tegra_powergate_power_off(dc->powergate);
1886
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001887 clk_disable_unprepare(dc->clk);
1888
1889 return 0;
1890}
1891
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001892struct platform_driver tegra_dc_driver = {
1893 .driver = {
1894 .name = "tegra-dc",
1895 .owner = THIS_MODULE,
1896 .of_match_table = tegra_dc_of_match,
1897 },
1898 .probe = tegra_dc_probe,
1899 .remove = tegra_dc_remove,
1900};