blob: 767fb440a79d9c463d8714641da0b6d7ac55308b [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * Copyright (C) 2017 NVIDIA CORPORATION. All rights reserved.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004 */
5
6#include <linux/clk.h>
David Brazdil0f672f62019-12-10 10:32:29 +00007#include <linux/delay.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008#include <linux/host1x.h>
9#include <linux/module.h>
10#include <linux/of.h>
11#include <linux/of_device.h>
12#include <linux/of_graph.h>
13#include <linux/platform_device.h>
14#include <linux/pm_runtime.h>
15#include <linux/reset.h>
16
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000017#include <drm/drm_atomic.h>
18#include <drm/drm_atomic_helper.h>
David Brazdil0f672f62019-12-10 10:32:29 +000019#include <drm/drm_fourcc.h>
20#include <drm/drm_probe_helper.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000021
22#include "drm.h"
23#include "dc.h"
24#include "plane.h"
25
26static const u32 tegra_shared_plane_formats[] = {
27 DRM_FORMAT_ARGB1555,
28 DRM_FORMAT_RGB565,
29 DRM_FORMAT_RGBA5551,
30 DRM_FORMAT_ARGB8888,
31 DRM_FORMAT_ABGR8888,
32 /* new on Tegra114 */
33 DRM_FORMAT_ABGR4444,
34 DRM_FORMAT_ABGR1555,
35 DRM_FORMAT_BGRA5551,
36 DRM_FORMAT_XRGB1555,
37 DRM_FORMAT_RGBX5551,
38 DRM_FORMAT_XBGR1555,
39 DRM_FORMAT_BGRX5551,
40 DRM_FORMAT_BGR565,
41 DRM_FORMAT_XRGB8888,
42 DRM_FORMAT_XBGR8888,
43 /* planar formats */
44 DRM_FORMAT_UYVY,
45 DRM_FORMAT_YUYV,
46 DRM_FORMAT_YUV420,
47 DRM_FORMAT_YUV422,
48};
49
50static const u64 tegra_shared_plane_modifiers[] = {
51 DRM_FORMAT_MOD_LINEAR,
52 DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(0),
53 DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(1),
54 DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(2),
55 DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(3),
56 DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(4),
57 DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(5),
58 DRM_FORMAT_MOD_INVALID
59};
60
61static inline unsigned int tegra_plane_offset(struct tegra_plane *plane,
62 unsigned int offset)
63{
64 if (offset >= 0x500 && offset <= 0x581) {
65 offset = 0x000 + (offset - 0x500);
66 return plane->offset + offset;
67 }
68
69 if (offset >= 0x700 && offset <= 0x73c) {
70 offset = 0x180 + (offset - 0x700);
71 return plane->offset + offset;
72 }
73
74 if (offset >= 0x800 && offset <= 0x83e) {
75 offset = 0x1c0 + (offset - 0x800);
76 return plane->offset + offset;
77 }
78
79 dev_WARN(plane->dc->dev, "invalid offset: %x\n", offset);
80
81 return plane->offset + offset;
82}
83
84static inline u32 tegra_plane_readl(struct tegra_plane *plane,
85 unsigned int offset)
86{
87 return tegra_dc_readl(plane->dc, tegra_plane_offset(plane, offset));
88}
89
90static inline void tegra_plane_writel(struct tegra_plane *plane, u32 value,
91 unsigned int offset)
92{
93 tegra_dc_writel(plane->dc, value, tegra_plane_offset(plane, offset));
94}
95
96static int tegra_windowgroup_enable(struct tegra_windowgroup *wgrp)
97{
98 mutex_lock(&wgrp->lock);
99
100 if (wgrp->usecount == 0) {
101 pm_runtime_get_sync(wgrp->parent);
102 reset_control_deassert(wgrp->rst);
103 }
104
105 wgrp->usecount++;
106 mutex_unlock(&wgrp->lock);
107
108 return 0;
109}
110
111static void tegra_windowgroup_disable(struct tegra_windowgroup *wgrp)
112{
113 int err;
114
115 mutex_lock(&wgrp->lock);
116
117 if (wgrp->usecount == 1) {
118 err = reset_control_assert(wgrp->rst);
119 if (err < 0) {
120 pr_err("failed to assert reset for window group %u\n",
121 wgrp->index);
122 }
123
124 pm_runtime_put(wgrp->parent);
125 }
126
127 wgrp->usecount--;
128 mutex_unlock(&wgrp->lock);
129}
130
131int tegra_display_hub_prepare(struct tegra_display_hub *hub)
132{
133 unsigned int i;
134
135 /*
136 * XXX Enabling/disabling windowgroups needs to happen when the owner
137 * display controller is disabled. There's currently no good point at
138 * which this could be executed, so unconditionally enable all window
139 * groups for now.
140 */
141 for (i = 0; i < hub->soc->num_wgrps; i++) {
142 struct tegra_windowgroup *wgrp = &hub->wgrps[i];
143
Olivier Deprez0e641232021-09-23 10:07:05 +0200144 /* Skip orphaned window group whose parent DC is disabled */
145 if (wgrp->parent)
146 tegra_windowgroup_enable(wgrp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000147 }
148
149 return 0;
150}
151
152void tegra_display_hub_cleanup(struct tegra_display_hub *hub)
153{
154 unsigned int i;
155
156 /*
157 * XXX Remove this once window groups can be more fine-grainedly
158 * enabled and disabled.
159 */
160 for (i = 0; i < hub->soc->num_wgrps; i++) {
161 struct tegra_windowgroup *wgrp = &hub->wgrps[i];
162
Olivier Deprez0e641232021-09-23 10:07:05 +0200163 /* Skip orphaned window group whose parent DC is disabled */
164 if (wgrp->parent)
165 tegra_windowgroup_disable(wgrp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000166 }
167}
168
169static void tegra_shared_plane_update(struct tegra_plane *plane)
170{
171 struct tegra_dc *dc = plane->dc;
172 unsigned long timeout;
173 u32 mask, value;
174
175 mask = COMMON_UPDATE | WIN_A_UPDATE << plane->base.index;
176 tegra_dc_writel(dc, mask, DC_CMD_STATE_CONTROL);
177
178 timeout = jiffies + msecs_to_jiffies(1000);
179
180 while (time_before(jiffies, timeout)) {
181 value = tegra_dc_readl(dc, DC_CMD_STATE_CONTROL);
182 if ((value & mask) == 0)
183 break;
184
185 usleep_range(100, 400);
186 }
187}
188
189static void tegra_shared_plane_activate(struct tegra_plane *plane)
190{
191 struct tegra_dc *dc = plane->dc;
192 unsigned long timeout;
193 u32 mask, value;
194
195 mask = COMMON_ACTREQ | WIN_A_ACT_REQ << plane->base.index;
196 tegra_dc_writel(dc, mask, DC_CMD_STATE_CONTROL);
197
198 timeout = jiffies + msecs_to_jiffies(1000);
199
200 while (time_before(jiffies, timeout)) {
201 value = tegra_dc_readl(dc, DC_CMD_STATE_CONTROL);
202 if ((value & mask) == 0)
203 break;
204
205 usleep_range(100, 400);
206 }
207}
208
209static unsigned int
210tegra_shared_plane_get_owner(struct tegra_plane *plane, struct tegra_dc *dc)
211{
212 unsigned int offset =
213 tegra_plane_offset(plane, DC_WIN_CORE_WINDOWGROUP_SET_CONTROL);
214
215 return tegra_dc_readl(dc, offset) & OWNER_MASK;
216}
217
218static bool tegra_dc_owns_shared_plane(struct tegra_dc *dc,
219 struct tegra_plane *plane)
220{
221 struct device *dev = dc->dev;
222
223 if (tegra_shared_plane_get_owner(plane, dc) == dc->pipe) {
224 if (plane->dc == dc)
225 return true;
226
227 dev_WARN(dev, "head %u owns window %u but is not attached\n",
228 dc->pipe, plane->index);
229 }
230
231 return false;
232}
233
234static int tegra_shared_plane_set_owner(struct tegra_plane *plane,
235 struct tegra_dc *new)
236{
237 unsigned int offset =
238 tegra_plane_offset(plane, DC_WIN_CORE_WINDOWGROUP_SET_CONTROL);
239 struct tegra_dc *old = plane->dc, *dc = new ? new : old;
240 struct device *dev = new ? new->dev : old->dev;
241 unsigned int owner, index = plane->index;
242 u32 value;
243
244 value = tegra_dc_readl(dc, offset);
245 owner = value & OWNER_MASK;
246
247 if (new && (owner != OWNER_MASK && owner != new->pipe)) {
248 dev_WARN(dev, "window %u owned by head %u\n", index, owner);
249 return -EBUSY;
250 }
251
252 /*
253 * This seems to happen whenever the head has been disabled with one
254 * or more windows being active. This is harmless because we'll just
255 * reassign the window to the new head anyway.
256 */
257 if (old && owner == OWNER_MASK)
258 dev_dbg(dev, "window %u not owned by head %u but %u\n", index,
259 old->pipe, owner);
260
261 value &= ~OWNER_MASK;
262
263 if (new)
264 value |= OWNER(new->pipe);
265 else
266 value |= OWNER_MASK;
267
268 tegra_dc_writel(dc, value, offset);
269
270 plane->dc = new;
271
272 return 0;
273}
274
275static void tegra_dc_assign_shared_plane(struct tegra_dc *dc,
276 struct tegra_plane *plane)
277{
278 u32 value;
279 int err;
280
281 if (!tegra_dc_owns_shared_plane(dc, plane)) {
282 err = tegra_shared_plane_set_owner(plane, dc);
283 if (err < 0)
284 return;
285 }
286
287 value = tegra_plane_readl(plane, DC_WIN_CORE_IHUB_LINEBUF_CONFIG);
288 value |= MODE_FOUR_LINES;
289 tegra_plane_writel(plane, value, DC_WIN_CORE_IHUB_LINEBUF_CONFIG);
290
291 value = tegra_plane_readl(plane, DC_WIN_CORE_IHUB_WGRP_FETCH_METER);
292 value = SLOTS(1);
293 tegra_plane_writel(plane, value, DC_WIN_CORE_IHUB_WGRP_FETCH_METER);
294
295 /* disable watermark */
296 value = tegra_plane_readl(plane, DC_WIN_CORE_IHUB_WGRP_LATENCY_CTLA);
297 value &= ~LATENCY_CTL_MODE_ENABLE;
298 tegra_plane_writel(plane, value, DC_WIN_CORE_IHUB_WGRP_LATENCY_CTLA);
299
300 value = tegra_plane_readl(plane, DC_WIN_CORE_IHUB_WGRP_LATENCY_CTLB);
301 value |= WATERMARK_MASK;
302 tegra_plane_writel(plane, value, DC_WIN_CORE_IHUB_WGRP_LATENCY_CTLB);
303
304 /* pipe meter */
305 value = tegra_plane_readl(plane, DC_WIN_CORE_PRECOMP_WGRP_PIPE_METER);
306 value = PIPE_METER_INT(0) | PIPE_METER_FRAC(0);
307 tegra_plane_writel(plane, value, DC_WIN_CORE_PRECOMP_WGRP_PIPE_METER);
308
309 /* mempool entries */
310 value = tegra_plane_readl(plane, DC_WIN_CORE_IHUB_WGRP_POOL_CONFIG);
311 value = MEMPOOL_ENTRIES(0x331);
312 tegra_plane_writel(plane, value, DC_WIN_CORE_IHUB_WGRP_POOL_CONFIG);
313
314 value = tegra_plane_readl(plane, DC_WIN_CORE_IHUB_THREAD_GROUP);
315 value &= ~THREAD_NUM_MASK;
316 value |= THREAD_NUM(plane->base.index);
317 value |= THREAD_GROUP_ENABLE;
318 tegra_plane_writel(plane, value, DC_WIN_CORE_IHUB_THREAD_GROUP);
319
320 tegra_shared_plane_update(plane);
321 tegra_shared_plane_activate(plane);
322}
323
324static void tegra_dc_remove_shared_plane(struct tegra_dc *dc,
325 struct tegra_plane *plane)
326{
327 tegra_shared_plane_set_owner(plane, NULL);
328}
329
330static int tegra_shared_plane_atomic_check(struct drm_plane *plane,
331 struct drm_plane_state *state)
332{
333 struct tegra_plane_state *plane_state = to_tegra_plane_state(state);
334 struct tegra_shared_plane *tegra = to_tegra_shared_plane(plane);
335 struct tegra_bo_tiling *tiling = &plane_state->tiling;
336 struct tegra_dc *dc = to_tegra_dc(state->crtc);
337 int err;
338
339 /* no need for further checks if the plane is being disabled */
340 if (!state->crtc || !state->fb)
341 return 0;
342
343 err = tegra_plane_format(state->fb->format->format,
344 &plane_state->format,
345 &plane_state->swap);
346 if (err < 0)
347 return err;
348
349 err = tegra_fb_get_tiling(state->fb, tiling);
350 if (err < 0)
351 return err;
352
353 if (tiling->mode == TEGRA_BO_TILING_MODE_BLOCK &&
354 !dc->soc->supports_block_linear) {
355 DRM_ERROR("hardware doesn't support block linear mode\n");
356 return -EINVAL;
357 }
358
359 /*
360 * Tegra doesn't support different strides for U and V planes so we
361 * error out if the user tries to display a framebuffer with such a
362 * configuration.
363 */
364 if (state->fb->format->num_planes > 2) {
365 if (state->fb->pitches[2] != state->fb->pitches[1]) {
366 DRM_ERROR("unsupported UV-plane configuration\n");
367 return -EINVAL;
368 }
369 }
370
371 /* XXX scaling is not yet supported, add a check here */
372
373 err = tegra_plane_state_add(&tegra->base, state);
374 if (err < 0)
375 return err;
376
377 return 0;
378}
379
380static void tegra_shared_plane_atomic_disable(struct drm_plane *plane,
381 struct drm_plane_state *old_state)
382{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000383 struct tegra_plane *p = to_tegra_plane(plane);
David Brazdil0f672f62019-12-10 10:32:29 +0000384 struct tegra_dc *dc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000385 u32 value;
386
387 /* rien ne va plus */
388 if (!old_state || !old_state->crtc)
389 return;
390
David Brazdil0f672f62019-12-10 10:32:29 +0000391 dc = to_tegra_dc(old_state->crtc);
392
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000393 /*
394 * XXX Legacy helpers seem to sometimes call ->atomic_disable() even
395 * on planes that are already disabled. Make sure we fallback to the
396 * head for this particular state instead of crashing.
397 */
398 if (WARN_ON(p->dc == NULL))
399 p->dc = dc;
400
401 pm_runtime_get_sync(dc->dev);
402
403 value = tegra_plane_readl(p, DC_WIN_WIN_OPTIONS);
404 value &= ~WIN_ENABLE;
405 tegra_plane_writel(p, value, DC_WIN_WIN_OPTIONS);
406
407 tegra_dc_remove_shared_plane(dc, p);
408
409 pm_runtime_put(dc->dev);
410}
411
412static void tegra_shared_plane_atomic_update(struct drm_plane *plane,
413 struct drm_plane_state *old_state)
414{
415 struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
416 struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
417 unsigned int zpos = plane->state->normalized_zpos;
418 struct drm_framebuffer *fb = plane->state->fb;
419 struct tegra_plane *p = to_tegra_plane(plane);
420 struct tegra_bo *bo;
421 dma_addr_t base;
422 u32 value;
423
424 /* rien ne va plus */
425 if (!plane->state->crtc || !plane->state->fb)
426 return;
427
428 if (!plane->state->visible) {
429 tegra_shared_plane_atomic_disable(plane, old_state);
430 return;
431 }
432
433 pm_runtime_get_sync(dc->dev);
434
435 tegra_dc_assign_shared_plane(dc, p);
436
437 tegra_plane_writel(p, VCOUNTER, DC_WIN_CORE_ACT_CONTROL);
438
439 /* blending */
440 value = BLEND_FACTOR_DST_ALPHA_ZERO | BLEND_FACTOR_SRC_ALPHA_K2 |
441 BLEND_FACTOR_DST_COLOR_NEG_K1_TIMES_SRC |
442 BLEND_FACTOR_SRC_COLOR_K1_TIMES_SRC;
443 tegra_plane_writel(p, value, DC_WIN_BLEND_MATCH_SELECT);
444
445 value = BLEND_FACTOR_DST_ALPHA_ZERO | BLEND_FACTOR_SRC_ALPHA_K2 |
446 BLEND_FACTOR_DST_COLOR_NEG_K1_TIMES_SRC |
447 BLEND_FACTOR_SRC_COLOR_K1_TIMES_SRC;
448 tegra_plane_writel(p, value, DC_WIN_BLEND_NOMATCH_SELECT);
449
450 value = K2(255) | K1(255) | WINDOW_LAYER_DEPTH(255 - zpos);
451 tegra_plane_writel(p, value, DC_WIN_BLEND_LAYER_CONTROL);
452
453 /* bypass scaling */
454 value = HORIZONTAL_TAPS_5 | VERTICAL_TAPS_5;
455 tegra_plane_writel(p, value, DC_WIN_WINDOWGROUP_SET_CONTROL_INPUT_SCALER);
456
457 value = INPUT_SCALER_VBYPASS | INPUT_SCALER_HBYPASS;
458 tegra_plane_writel(p, value, DC_WIN_WINDOWGROUP_SET_INPUT_SCALER_USAGE);
459
460 /* disable compression */
461 tegra_plane_writel(p, 0, DC_WINBUF_CDE_CONTROL);
462
463 bo = tegra_fb_get_plane(fb, 0);
464 base = bo->paddr;
465
466 tegra_plane_writel(p, state->format, DC_WIN_COLOR_DEPTH);
467 tegra_plane_writel(p, 0, DC_WIN_PRECOMP_WGRP_PARAMS);
468
469 value = V_POSITION(plane->state->crtc_y) |
470 H_POSITION(plane->state->crtc_x);
471 tegra_plane_writel(p, value, DC_WIN_POSITION);
472
473 value = V_SIZE(plane->state->crtc_h) | H_SIZE(plane->state->crtc_w);
474 tegra_plane_writel(p, value, DC_WIN_SIZE);
475
476 value = WIN_ENABLE | COLOR_EXPAND;
477 tegra_plane_writel(p, value, DC_WIN_WIN_OPTIONS);
478
479 value = V_SIZE(plane->state->crtc_h) | H_SIZE(plane->state->crtc_w);
480 tegra_plane_writel(p, value, DC_WIN_CROPPED_SIZE);
481
482 tegra_plane_writel(p, upper_32_bits(base), DC_WINBUF_START_ADDR_HI);
483 tegra_plane_writel(p, lower_32_bits(base), DC_WINBUF_START_ADDR);
484
485 value = PITCH(fb->pitches[0]);
486 tegra_plane_writel(p, value, DC_WIN_PLANAR_STORAGE);
487
488 value = CLAMP_BEFORE_BLEND | DEGAMMA_SRGB | INPUT_RANGE_FULL;
489 tegra_plane_writel(p, value, DC_WIN_SET_PARAMS);
490
491 value = OFFSET_X(plane->state->src_y >> 16) |
492 OFFSET_Y(plane->state->src_x >> 16);
493 tegra_plane_writel(p, value, DC_WINBUF_CROPPED_POINT);
494
495 if (dc->soc->supports_block_linear) {
496 unsigned long height = state->tiling.value;
497
498 /* XXX */
499 switch (state->tiling.mode) {
500 case TEGRA_BO_TILING_MODE_PITCH:
501 value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(0) |
502 DC_WINBUF_SURFACE_KIND_PITCH;
503 break;
504
505 /* XXX not supported on Tegra186 and later */
506 case TEGRA_BO_TILING_MODE_TILED:
507 value = DC_WINBUF_SURFACE_KIND_TILED;
508 break;
509
510 case TEGRA_BO_TILING_MODE_BLOCK:
511 value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
512 DC_WINBUF_SURFACE_KIND_BLOCK;
513 break;
514 }
515
516 tegra_plane_writel(p, value, DC_WINBUF_SURFACE_KIND);
517 }
518
519 /* disable gamut CSC */
520 value = tegra_plane_readl(p, DC_WIN_WINDOW_SET_CONTROL);
521 value &= ~CONTROL_CSC_ENABLE;
522 tegra_plane_writel(p, value, DC_WIN_WINDOW_SET_CONTROL);
523
524 pm_runtime_put(dc->dev);
525}
526
527static const struct drm_plane_helper_funcs tegra_shared_plane_helper_funcs = {
528 .atomic_check = tegra_shared_plane_atomic_check,
529 .atomic_update = tegra_shared_plane_atomic_update,
530 .atomic_disable = tegra_shared_plane_atomic_disable,
531};
532
533struct drm_plane *tegra_shared_plane_create(struct drm_device *drm,
534 struct tegra_dc *dc,
535 unsigned int wgrp,
536 unsigned int index)
537{
538 enum drm_plane_type type = DRM_PLANE_TYPE_OVERLAY;
539 struct tegra_drm *tegra = drm->dev_private;
540 struct tegra_display_hub *hub = tegra->hub;
541 /* planes can be assigned to arbitrary CRTCs */
542 unsigned int possible_crtcs = 0x7;
543 struct tegra_shared_plane *plane;
544 unsigned int num_formats;
545 const u64 *modifiers;
546 struct drm_plane *p;
547 const u32 *formats;
548 int err;
549
550 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
551 if (!plane)
552 return ERR_PTR(-ENOMEM);
553
554 plane->base.offset = 0x0a00 + 0x0300 * index;
555 plane->base.index = index;
556
557 plane->wgrp = &hub->wgrps[wgrp];
558 plane->wgrp->parent = dc->dev;
559
560 p = &plane->base.base;
561
562 num_formats = ARRAY_SIZE(tegra_shared_plane_formats);
563 formats = tegra_shared_plane_formats;
564 modifiers = tegra_shared_plane_modifiers;
565
566 err = drm_universal_plane_init(drm, p, possible_crtcs,
567 &tegra_plane_funcs, formats,
568 num_formats, modifiers, type, NULL);
569 if (err < 0) {
570 kfree(plane);
571 return ERR_PTR(err);
572 }
573
574 drm_plane_helper_add(p, &tegra_shared_plane_helper_funcs);
575 drm_plane_create_zpos_property(p, 0, 0, 255);
576
577 return p;
578}
579
580static struct drm_private_state *
581tegra_display_hub_duplicate_state(struct drm_private_obj *obj)
582{
583 struct tegra_display_hub_state *state;
584
585 state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL);
586 if (!state)
587 return NULL;
588
589 __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
590
591 return &state->base;
592}
593
594static void tegra_display_hub_destroy_state(struct drm_private_obj *obj,
595 struct drm_private_state *state)
596{
597 struct tegra_display_hub_state *hub_state =
598 to_tegra_display_hub_state(state);
599
600 kfree(hub_state);
601}
602
603static const struct drm_private_state_funcs tegra_display_hub_state_funcs = {
604 .atomic_duplicate_state = tegra_display_hub_duplicate_state,
605 .atomic_destroy_state = tegra_display_hub_destroy_state,
606};
607
608static struct tegra_display_hub_state *
609tegra_display_hub_get_state(struct tegra_display_hub *hub,
610 struct drm_atomic_state *state)
611{
612 struct drm_device *drm = dev_get_drvdata(hub->client.parent);
613 struct drm_private_state *priv;
614
615 WARN_ON(!drm_modeset_is_locked(&drm->mode_config.connection_mutex));
616
617 priv = drm_atomic_get_private_obj_state(state, &hub->base);
618 if (IS_ERR(priv))
619 return ERR_CAST(priv);
620
621 return to_tegra_display_hub_state(priv);
622}
623
624int tegra_display_hub_atomic_check(struct drm_device *drm,
625 struct drm_atomic_state *state)
626{
627 struct tegra_drm *tegra = drm->dev_private;
628 struct tegra_display_hub_state *hub_state;
629 struct drm_crtc_state *old, *new;
630 struct drm_crtc *crtc;
631 unsigned int i;
632
633 if (!tegra->hub)
634 return 0;
635
636 hub_state = tegra_display_hub_get_state(tegra->hub, state);
637 if (IS_ERR(hub_state))
638 return PTR_ERR(hub_state);
639
640 /*
641 * The display hub display clock needs to be fed by the display clock
642 * with the highest frequency to ensure proper functioning of all the
643 * displays.
644 *
645 * Note that this isn't used before Tegra186, but it doesn't hurt and
646 * conditionalizing it would make the code less clean.
647 */
648 for_each_oldnew_crtc_in_state(state, crtc, old, new, i) {
649 struct tegra_dc_state *dc = to_dc_state(new);
650
651 if (new->active) {
652 if (!hub_state->clk || dc->pclk > hub_state->rate) {
653 hub_state->dc = to_tegra_dc(dc->base.crtc);
654 hub_state->clk = hub_state->dc->clk;
655 hub_state->rate = dc->pclk;
656 }
657 }
658 }
659
660 return 0;
661}
662
663static void tegra_display_hub_update(struct tegra_dc *dc)
664{
665 u32 value;
666
667 pm_runtime_get_sync(dc->dev);
668
669 value = tegra_dc_readl(dc, DC_CMD_IHUB_COMMON_MISC_CTL);
670 value &= ~LATENCY_EVENT;
671 tegra_dc_writel(dc, value, DC_CMD_IHUB_COMMON_MISC_CTL);
672
673 value = tegra_dc_readl(dc, DC_DISP_IHUB_COMMON_DISPLAY_FETCH_METER);
674 value = CURS_SLOTS(1) | WGRP_SLOTS(1);
675 tegra_dc_writel(dc, value, DC_DISP_IHUB_COMMON_DISPLAY_FETCH_METER);
676
677 tegra_dc_writel(dc, COMMON_UPDATE, DC_CMD_STATE_CONTROL);
678 tegra_dc_readl(dc, DC_CMD_STATE_CONTROL);
679 tegra_dc_writel(dc, COMMON_ACTREQ, DC_CMD_STATE_CONTROL);
680 tegra_dc_readl(dc, DC_CMD_STATE_CONTROL);
681
682 pm_runtime_put(dc->dev);
683}
684
685void tegra_display_hub_atomic_commit(struct drm_device *drm,
686 struct drm_atomic_state *state)
687{
688 struct tegra_drm *tegra = drm->dev_private;
689 struct tegra_display_hub *hub = tegra->hub;
690 struct tegra_display_hub_state *hub_state;
691 struct device *dev = hub->client.dev;
692 int err;
693
694 hub_state = to_tegra_display_hub_state(hub->base.state);
695
696 if (hub_state->clk) {
697 err = clk_set_rate(hub_state->clk, hub_state->rate);
698 if (err < 0)
699 dev_err(dev, "failed to set rate of %pC to %lu Hz\n",
700 hub_state->clk, hub_state->rate);
701
702 err = clk_set_parent(hub->clk_disp, hub_state->clk);
703 if (err < 0)
704 dev_err(dev, "failed to set parent of %pC to %pC: %d\n",
705 hub->clk_disp, hub_state->clk, err);
706 }
707
708 if (hub_state->dc)
709 tegra_display_hub_update(hub_state->dc);
710}
711
712static int tegra_display_hub_init(struct host1x_client *client)
713{
714 struct tegra_display_hub *hub = to_tegra_display_hub(client);
715 struct drm_device *drm = dev_get_drvdata(client->parent);
716 struct tegra_drm *tegra = drm->dev_private;
717 struct tegra_display_hub_state *state;
718
719 state = kzalloc(sizeof(*state), GFP_KERNEL);
720 if (!state)
721 return -ENOMEM;
722
David Brazdil0f672f62019-12-10 10:32:29 +0000723 drm_atomic_private_obj_init(drm, &hub->base, &state->base,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000724 &tegra_display_hub_state_funcs);
725
726 tegra->hub = hub;
727
728 return 0;
729}
730
731static int tegra_display_hub_exit(struct host1x_client *client)
732{
733 struct drm_device *drm = dev_get_drvdata(client->parent);
734 struct tegra_drm *tegra = drm->dev_private;
735
736 drm_atomic_private_obj_fini(&tegra->hub->base);
737 tegra->hub = NULL;
738
739 return 0;
740}
741
742static const struct host1x_client_ops tegra_display_hub_ops = {
743 .init = tegra_display_hub_init,
744 .exit = tegra_display_hub_exit,
745};
746
747static int tegra_display_hub_probe(struct platform_device *pdev)
748{
David Brazdil0f672f62019-12-10 10:32:29 +0000749 struct device_node *child = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000750 struct tegra_display_hub *hub;
David Brazdil0f672f62019-12-10 10:32:29 +0000751 struct clk *clk;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000752 unsigned int i;
753 int err;
754
755 hub = devm_kzalloc(&pdev->dev, sizeof(*hub), GFP_KERNEL);
756 if (!hub)
757 return -ENOMEM;
758
759 hub->soc = of_device_get_match_data(&pdev->dev);
760
761 hub->clk_disp = devm_clk_get(&pdev->dev, "disp");
762 if (IS_ERR(hub->clk_disp)) {
763 err = PTR_ERR(hub->clk_disp);
764 return err;
765 }
766
David Brazdil0f672f62019-12-10 10:32:29 +0000767 if (hub->soc->supports_dsc) {
768 hub->clk_dsc = devm_clk_get(&pdev->dev, "dsc");
769 if (IS_ERR(hub->clk_dsc)) {
770 err = PTR_ERR(hub->clk_dsc);
771 return err;
772 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000773 }
774
775 hub->clk_hub = devm_clk_get(&pdev->dev, "hub");
776 if (IS_ERR(hub->clk_hub)) {
777 err = PTR_ERR(hub->clk_hub);
778 return err;
779 }
780
781 hub->rst = devm_reset_control_get(&pdev->dev, "misc");
782 if (IS_ERR(hub->rst)) {
783 err = PTR_ERR(hub->rst);
784 return err;
785 }
786
787 hub->wgrps = devm_kcalloc(&pdev->dev, hub->soc->num_wgrps,
788 sizeof(*hub->wgrps), GFP_KERNEL);
789 if (!hub->wgrps)
790 return -ENOMEM;
791
792 for (i = 0; i < hub->soc->num_wgrps; i++) {
793 struct tegra_windowgroup *wgrp = &hub->wgrps[i];
794 char id[8];
795
796 snprintf(id, sizeof(id), "wgrp%u", i);
797 mutex_init(&wgrp->lock);
798 wgrp->usecount = 0;
799 wgrp->index = i;
800
801 wgrp->rst = devm_reset_control_get(&pdev->dev, id);
802 if (IS_ERR(wgrp->rst))
803 return PTR_ERR(wgrp->rst);
804
805 err = reset_control_assert(wgrp->rst);
806 if (err < 0)
807 return err;
808 }
809
David Brazdil0f672f62019-12-10 10:32:29 +0000810 hub->num_heads = of_get_child_count(pdev->dev.of_node);
811
812 hub->clk_heads = devm_kcalloc(&pdev->dev, hub->num_heads, sizeof(clk),
813 GFP_KERNEL);
814 if (!hub->clk_heads)
815 return -ENOMEM;
816
817 for (i = 0; i < hub->num_heads; i++) {
818 child = of_get_next_child(pdev->dev.of_node, child);
819 if (!child) {
820 dev_err(&pdev->dev, "failed to find node for head %u\n",
821 i);
822 return -ENODEV;
823 }
824
825 clk = devm_get_clk_from_child(&pdev->dev, child, "dc");
826 if (IS_ERR(clk)) {
827 dev_err(&pdev->dev, "failed to get clock for head %u\n",
828 i);
829 of_node_put(child);
830 return PTR_ERR(clk);
831 }
832
833 hub->clk_heads[i] = clk;
834 }
835
836 of_node_put(child);
837
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000838 /* XXX: enable clock across reset? */
839 err = reset_control_assert(hub->rst);
840 if (err < 0)
841 return err;
842
843 platform_set_drvdata(pdev, hub);
844 pm_runtime_enable(&pdev->dev);
845
846 INIT_LIST_HEAD(&hub->client.list);
847 hub->client.ops = &tegra_display_hub_ops;
848 hub->client.dev = &pdev->dev;
849
850 err = host1x_client_register(&hub->client);
851 if (err < 0)
852 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
853 err);
854
855 return err;
856}
857
858static int tegra_display_hub_remove(struct platform_device *pdev)
859{
860 struct tegra_display_hub *hub = platform_get_drvdata(pdev);
861 int err;
862
863 err = host1x_client_unregister(&hub->client);
864 if (err < 0) {
865 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
866 err);
867 }
868
869 pm_runtime_disable(&pdev->dev);
870
871 return err;
872}
873
874static int __maybe_unused tegra_display_hub_suspend(struct device *dev)
875{
876 struct tegra_display_hub *hub = dev_get_drvdata(dev);
David Brazdil0f672f62019-12-10 10:32:29 +0000877 unsigned int i = hub->num_heads;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000878 int err;
879
880 err = reset_control_assert(hub->rst);
881 if (err < 0)
882 return err;
883
David Brazdil0f672f62019-12-10 10:32:29 +0000884 while (i--)
885 clk_disable_unprepare(hub->clk_heads[i]);
886
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000887 clk_disable_unprepare(hub->clk_hub);
888 clk_disable_unprepare(hub->clk_dsc);
889 clk_disable_unprepare(hub->clk_disp);
890
891 return 0;
892}
893
894static int __maybe_unused tegra_display_hub_resume(struct device *dev)
895{
896 struct tegra_display_hub *hub = dev_get_drvdata(dev);
David Brazdil0f672f62019-12-10 10:32:29 +0000897 unsigned int i;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000898 int err;
899
900 err = clk_prepare_enable(hub->clk_disp);
901 if (err < 0)
902 return err;
903
904 err = clk_prepare_enable(hub->clk_dsc);
905 if (err < 0)
906 goto disable_disp;
907
908 err = clk_prepare_enable(hub->clk_hub);
909 if (err < 0)
910 goto disable_dsc;
911
David Brazdil0f672f62019-12-10 10:32:29 +0000912 for (i = 0; i < hub->num_heads; i++) {
913 err = clk_prepare_enable(hub->clk_heads[i]);
914 if (err < 0)
915 goto disable_heads;
916 }
917
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000918 err = reset_control_deassert(hub->rst);
919 if (err < 0)
David Brazdil0f672f62019-12-10 10:32:29 +0000920 goto disable_heads;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000921
922 return 0;
923
David Brazdil0f672f62019-12-10 10:32:29 +0000924disable_heads:
925 while (i--)
926 clk_disable_unprepare(hub->clk_heads[i]);
927
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000928 clk_disable_unprepare(hub->clk_hub);
929disable_dsc:
930 clk_disable_unprepare(hub->clk_dsc);
931disable_disp:
932 clk_disable_unprepare(hub->clk_disp);
933 return err;
934}
935
936static const struct dev_pm_ops tegra_display_hub_pm_ops = {
937 SET_RUNTIME_PM_OPS(tegra_display_hub_suspend,
938 tegra_display_hub_resume, NULL)
939};
940
941static const struct tegra_display_hub_soc tegra186_display_hub = {
942 .num_wgrps = 6,
David Brazdil0f672f62019-12-10 10:32:29 +0000943 .supports_dsc = true,
944};
945
946static const struct tegra_display_hub_soc tegra194_display_hub = {
947 .num_wgrps = 6,
948 .supports_dsc = false,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000949};
950
951static const struct of_device_id tegra_display_hub_of_match[] = {
952 {
David Brazdil0f672f62019-12-10 10:32:29 +0000953 .compatible = "nvidia,tegra194-display",
954 .data = &tegra194_display_hub
955 }, {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000956 .compatible = "nvidia,tegra186-display",
957 .data = &tegra186_display_hub
958 }, {
959 /* sentinel */
960 }
961};
962MODULE_DEVICE_TABLE(of, tegra_display_hub_of_match);
963
964struct platform_driver tegra_display_hub_driver = {
965 .driver = {
966 .name = "tegra-display-hub",
967 .of_match_table = tegra_display_hub_of_match,
968 .pm = &tegra_display_hub_pm_ops,
969 },
970 .probe = tegra_display_hub_probe,
971 .remove = tegra_display_hub_remove,
972};