2 * Copyright 2012 Red Hat Inc.
3 * Parts based on xf86-video-ast
4 * Copyright (c) 2005 ASPEED Technology Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
31 #include <linux/export.h>
32 #include <linux/pci.h>
34 #include <drm/drm_atomic.h>
35 #include <drm/drm_atomic_helper.h>
36 #include <drm/drm_atomic_state_helper.h>
37 #include <drm/drm_crtc.h>
38 #include <drm/drm_damage_helper.h>
39 #include <drm/drm_edid.h>
40 #include <drm/drm_format_helper.h>
41 #include <drm/drm_fourcc.h>
42 #include <drm/drm_gem_atomic_helper.h>
43 #include <drm/drm_gem_framebuffer_helper.h>
44 #include <drm/drm_gem_shmem_helper.h>
45 #include <drm/drm_managed.h>
46 #include <drm/drm_panic.h>
47 #include <drm/drm_probe_helper.h>
51 #include "ast_tables.h"
53 #define AST_LUT_SIZE 256
55 static inline void ast_load_palette_index(struct ast_device *ast,
56 u8 index, u8 red, u8 green,
59 ast_io_write8(ast, AST_IO_VGADWR, index);
60 ast_io_read8(ast, AST_IO_VGASRI);
61 ast_io_write8(ast, AST_IO_VGAPDR, red);
62 ast_io_read8(ast, AST_IO_VGASRI);
63 ast_io_write8(ast, AST_IO_VGAPDR, green);
64 ast_io_read8(ast, AST_IO_VGASRI);
65 ast_io_write8(ast, AST_IO_VGAPDR, blue);
66 ast_io_read8(ast, AST_IO_VGASRI);
69 static void ast_crtc_set_gamma_linear(struct ast_device *ast,
70 const struct drm_format_info *format)
74 switch (format->format) {
75 case DRM_FORMAT_C8: /* In this case, gamma table is used as color palette */
76 case DRM_FORMAT_RGB565:
77 case DRM_FORMAT_XRGB8888:
78 for (i = 0; i < AST_LUT_SIZE; i++)
79 ast_load_palette_index(ast, i, i, i, i);
82 drm_warn_once(&ast->base, "Unsupported format %p4cc for gamma correction\n",
88 static void ast_crtc_set_gamma(struct ast_device *ast,
89 const struct drm_format_info *format,
90 struct drm_color_lut *lut)
94 switch (format->format) {
95 case DRM_FORMAT_C8: /* In this case, gamma table is used as color palette */
96 case DRM_FORMAT_RGB565:
97 case DRM_FORMAT_XRGB8888:
98 for (i = 0; i < AST_LUT_SIZE; i++)
99 ast_load_palette_index(ast, i,
105 drm_warn_once(&ast->base, "Unsupported format %p4cc for gamma correction\n",
111 static bool ast_get_vbios_mode_info(const struct drm_format_info *format,
112 const struct drm_display_mode *mode,
113 struct drm_display_mode *adjusted_mode,
114 struct ast_vbios_mode_info *vbios_mode)
116 u32 refresh_rate_index = 0, refresh_rate;
117 const struct ast_vbios_enhtable *best = NULL;
118 u32 hborder, vborder;
121 switch (format->cpp[0] * 8) {
123 vbios_mode->std_table = &vbios_stdtable[VGAModeIndex];
126 vbios_mode->std_table = &vbios_stdtable[HiCModeIndex];
130 vbios_mode->std_table = &vbios_stdtable[TrueCModeIndex];
136 switch (mode->crtc_hdisplay) {
138 vbios_mode->enh_table = &res_640x480[refresh_rate_index];
141 vbios_mode->enh_table = &res_800x600[refresh_rate_index];
144 vbios_mode->enh_table = &res_1024x768[refresh_rate_index];
147 vbios_mode->enh_table = &res_1152x864[refresh_rate_index];
150 if (mode->crtc_vdisplay == 800)
151 vbios_mode->enh_table = &res_1280x800[refresh_rate_index];
153 vbios_mode->enh_table = &res_1280x1024[refresh_rate_index];
156 vbios_mode->enh_table = &res_1360x768[refresh_rate_index];
159 vbios_mode->enh_table = &res_1440x900[refresh_rate_index];
162 if (mode->crtc_vdisplay == 900)
163 vbios_mode->enh_table = &res_1600x900[refresh_rate_index];
165 vbios_mode->enh_table = &res_1600x1200[refresh_rate_index];
168 vbios_mode->enh_table = &res_1680x1050[refresh_rate_index];
171 if (mode->crtc_vdisplay == 1080)
172 vbios_mode->enh_table = &res_1920x1080[refresh_rate_index];
174 vbios_mode->enh_table = &res_1920x1200[refresh_rate_index];
180 refresh_rate = drm_mode_vrefresh(mode);
181 check_sync = vbios_mode->enh_table->flags & WideScreenMode;
184 const struct ast_vbios_enhtable *loop = vbios_mode->enh_table;
186 while (loop->refresh_rate != 0xff) {
188 (((mode->flags & DRM_MODE_FLAG_NVSYNC) &&
189 (loop->flags & PVSync)) ||
190 ((mode->flags & DRM_MODE_FLAG_PVSYNC) &&
191 (loop->flags & NVSync)) ||
192 ((mode->flags & DRM_MODE_FLAG_NHSYNC) &&
193 (loop->flags & PHSync)) ||
194 ((mode->flags & DRM_MODE_FLAG_PHSYNC) &&
195 (loop->flags & NHSync)))) {
199 if (loop->refresh_rate <= refresh_rate
200 && (!best || loop->refresh_rate > best->refresh_rate))
204 if (best || !check_sync)
210 vbios_mode->enh_table = best;
212 hborder = (vbios_mode->enh_table->flags & HBorder) ? 8 : 0;
213 vborder = (vbios_mode->enh_table->flags & VBorder) ? 8 : 0;
215 adjusted_mode->crtc_htotal = vbios_mode->enh_table->ht;
216 adjusted_mode->crtc_hblank_start = vbios_mode->enh_table->hde + hborder;
217 adjusted_mode->crtc_hblank_end = vbios_mode->enh_table->ht - hborder;
218 adjusted_mode->crtc_hsync_start = vbios_mode->enh_table->hde + hborder +
219 vbios_mode->enh_table->hfp;
220 adjusted_mode->crtc_hsync_end = (vbios_mode->enh_table->hde + hborder +
221 vbios_mode->enh_table->hfp +
222 vbios_mode->enh_table->hsync);
224 adjusted_mode->crtc_vtotal = vbios_mode->enh_table->vt;
225 adjusted_mode->crtc_vblank_start = vbios_mode->enh_table->vde + vborder;
226 adjusted_mode->crtc_vblank_end = vbios_mode->enh_table->vt - vborder;
227 adjusted_mode->crtc_vsync_start = vbios_mode->enh_table->vde + vborder +
228 vbios_mode->enh_table->vfp;
229 adjusted_mode->crtc_vsync_end = (vbios_mode->enh_table->vde + vborder +
230 vbios_mode->enh_table->vfp +
231 vbios_mode->enh_table->vsync);
236 static void ast_set_vbios_color_reg(struct ast_device *ast,
237 const struct drm_format_info *format,
238 const struct ast_vbios_mode_info *vbios_mode)
242 switch (format->cpp[0]) {
244 color_index = VGAModeIndex - 1;
247 color_index = HiCModeIndex;
251 color_index = TrueCModeIndex;
257 ast_set_index_reg(ast, AST_IO_VGACRI, 0x8c, (u8)((color_index & 0x0f) << 4));
259 ast_set_index_reg(ast, AST_IO_VGACRI, 0x91, 0x00);
261 if (vbios_mode->enh_table->flags & NewModeInfo) {
262 ast_set_index_reg(ast, AST_IO_VGACRI, 0x91, 0xa8);
263 ast_set_index_reg(ast, AST_IO_VGACRI, 0x92, format->cpp[0] * 8);
267 static void ast_set_vbios_mode_reg(struct ast_device *ast,
268 const struct drm_display_mode *adjusted_mode,
269 const struct ast_vbios_mode_info *vbios_mode)
271 u32 refresh_rate_index, mode_id;
273 refresh_rate_index = vbios_mode->enh_table->refresh_rate_index;
274 mode_id = vbios_mode->enh_table->mode_id;
276 ast_set_index_reg(ast, AST_IO_VGACRI, 0x8d, refresh_rate_index & 0xff);
277 ast_set_index_reg(ast, AST_IO_VGACRI, 0x8e, mode_id & 0xff);
279 ast_set_index_reg(ast, AST_IO_VGACRI, 0x91, 0x00);
281 if (vbios_mode->enh_table->flags & NewModeInfo) {
282 ast_set_index_reg(ast, AST_IO_VGACRI, 0x91, 0xa8);
283 ast_set_index_reg(ast, AST_IO_VGACRI, 0x93, adjusted_mode->clock / 1000);
284 ast_set_index_reg(ast, AST_IO_VGACRI, 0x94, adjusted_mode->crtc_hdisplay);
285 ast_set_index_reg(ast, AST_IO_VGACRI, 0x95, adjusted_mode->crtc_hdisplay >> 8);
286 ast_set_index_reg(ast, AST_IO_VGACRI, 0x96, adjusted_mode->crtc_vdisplay);
287 ast_set_index_reg(ast, AST_IO_VGACRI, 0x97, adjusted_mode->crtc_vdisplay >> 8);
291 static void ast_set_std_reg(struct ast_device *ast,
292 struct drm_display_mode *mode,
293 struct ast_vbios_mode_info *vbios_mode)
295 const struct ast_vbios_stdtable *stdtable;
299 stdtable = vbios_mode->std_table;
301 jreg = stdtable->misc;
302 ast_io_write8(ast, AST_IO_VGAMR_W, jreg);
304 /* Set SEQ; except Screen Disable field */
305 ast_set_index_reg(ast, AST_IO_VGASRI, 0x00, 0x03);
306 ast_set_index_reg_mask(ast, AST_IO_VGASRI, 0x01, 0x20, stdtable->seq[0]);
307 for (i = 1; i < 4; i++) {
308 jreg = stdtable->seq[i];
309 ast_set_index_reg(ast, AST_IO_VGASRI, (i + 1), jreg);
312 /* Set CRTC; except base address and offset */
313 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x11, 0x7f, 0x00);
314 for (i = 0; i < 12; i++)
315 ast_set_index_reg(ast, AST_IO_VGACRI, i, stdtable->crtc[i]);
316 for (i = 14; i < 19; i++)
317 ast_set_index_reg(ast, AST_IO_VGACRI, i, stdtable->crtc[i]);
318 for (i = 20; i < 25; i++)
319 ast_set_index_reg(ast, AST_IO_VGACRI, i, stdtable->crtc[i]);
322 jreg = ast_io_read8(ast, AST_IO_VGAIR1_R);
323 for (i = 0; i < 20; i++) {
324 jreg = stdtable->ar[i];
325 ast_io_write8(ast, AST_IO_VGAARI_W, (u8)i);
326 ast_io_write8(ast, AST_IO_VGAARI_W, jreg);
328 ast_io_write8(ast, AST_IO_VGAARI_W, 0x14);
329 ast_io_write8(ast, AST_IO_VGAARI_W, 0x00);
331 jreg = ast_io_read8(ast, AST_IO_VGAIR1_R);
332 ast_io_write8(ast, AST_IO_VGAARI_W, 0x20);
335 for (i = 0; i < 9; i++)
336 ast_set_index_reg(ast, AST_IO_VGAGRI, i, stdtable->gr[i]);
339 static void ast_set_crtc_reg(struct ast_device *ast,
340 struct drm_display_mode *mode,
341 struct ast_vbios_mode_info *vbios_mode)
343 u8 jreg05 = 0, jreg07 = 0, jreg09 = 0, jregAC = 0, jregAD = 0, jregAE = 0;
344 u16 temp, precache = 0;
346 if ((IS_AST_GEN6(ast) || IS_AST_GEN7(ast)) &&
347 (vbios_mode->enh_table->flags & AST2500PreCatchCRT))
350 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x11, 0x7f, 0x00);
352 temp = (mode->crtc_htotal >> 3) - 5;
354 jregAC |= 0x01; /* HT D[8] */
355 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x00, 0x00, temp);
357 temp = (mode->crtc_hdisplay >> 3) - 1;
359 jregAC |= 0x04; /* HDE D[8] */
360 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x01, 0x00, temp);
362 temp = (mode->crtc_hblank_start >> 3) - 1;
364 jregAC |= 0x10; /* HBS D[8] */
365 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x02, 0x00, temp);
367 temp = ((mode->crtc_hblank_end >> 3) - 1) & 0x7f;
369 jreg05 |= 0x80; /* HBE D[5] */
371 jregAD |= 0x01; /* HBE D[5] */
372 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x03, 0xE0, (temp & 0x1f));
374 temp = ((mode->crtc_hsync_start-precache) >> 3) - 1;
376 jregAC |= 0x40; /* HRS D[5] */
377 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x04, 0x00, temp);
379 temp = (((mode->crtc_hsync_end-precache) >> 3) - 1) & 0x3f;
381 jregAD |= 0x04; /* HRE D[5] */
382 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x05, 0x60, (u8)((temp & 0x1f) | jreg05));
384 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xAC, 0x00, jregAC);
385 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xAD, 0x00, jregAD);
387 // Workaround for HSync Time non octave pixels (1920x1080@60Hz HSync 44 pixels);
388 if (IS_AST_GEN7(ast) && (mode->crtc_vdisplay == 1080))
389 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xFC, 0xFD, 0x02);
391 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xFC, 0xFD, 0x00);
394 temp = (mode->crtc_vtotal) - 2;
401 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x06, 0x00, temp);
403 temp = (mode->crtc_vsync_start) - 1;
410 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x10, 0x00, temp);
412 temp = (mode->crtc_vsync_end - 1) & 0x3f;
417 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x11, 0x70, temp & 0xf);
419 temp = mode->crtc_vdisplay - 1;
426 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x12, 0x00, temp);
428 temp = mode->crtc_vblank_start - 1;
435 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x15, 0x00, temp);
437 temp = mode->crtc_vblank_end - 1;
440 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x16, 0x00, temp);
442 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x07, 0x00, jreg07);
443 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x09, 0xdf, jreg09);
444 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xAE, 0x00, (jregAE | 0x80));
447 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xb6, 0x3f, 0x80);
449 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xb6, 0x3f, 0x00);
451 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x11, 0x7f, 0x80);
454 static void ast_set_offset_reg(struct ast_device *ast,
455 struct drm_framebuffer *fb)
459 offset = fb->pitches[0] >> 3;
460 ast_set_index_reg(ast, AST_IO_VGACRI, 0x13, (offset & 0xff));
461 ast_set_index_reg(ast, AST_IO_VGACRI, 0xb0, (offset >> 8) & 0x3f);
464 static void ast_set_dclk_reg(struct ast_device *ast,
465 struct drm_display_mode *mode,
466 struct ast_vbios_mode_info *vbios_mode)
468 const struct ast_vbios_dclk_info *clk_info;
470 if (IS_AST_GEN6(ast) || IS_AST_GEN7(ast))
471 clk_info = &dclk_table_ast2500[vbios_mode->enh_table->dclk_index];
473 clk_info = &dclk_table[vbios_mode->enh_table->dclk_index];
475 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xc0, 0x00, clk_info->param1);
476 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xc1, 0x00, clk_info->param2);
477 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xbb, 0x0f,
478 (clk_info->param3 & 0xc0) |
479 ((clk_info->param3 & 0x3) << 4));
482 static void ast_set_color_reg(struct ast_device *ast,
483 const struct drm_format_info *format)
485 u8 jregA0 = 0, jregA3 = 0, jregA8 = 0;
487 switch (format->cpp[0] * 8) {
506 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xa0, 0x8f, jregA0);
507 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xa3, 0xf0, jregA3);
508 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xa8, 0xfd, jregA8);
511 static void ast_set_crtthd_reg(struct ast_device *ast)
514 if (IS_AST_GEN7(ast)) {
515 ast_set_index_reg(ast, AST_IO_VGACRI, 0xa7, 0xe0);
516 ast_set_index_reg(ast, AST_IO_VGACRI, 0xa6, 0xa0);
517 } else if (IS_AST_GEN6(ast) || IS_AST_GEN5(ast) || IS_AST_GEN4(ast)) {
518 ast_set_index_reg(ast, AST_IO_VGACRI, 0xa7, 0x78);
519 ast_set_index_reg(ast, AST_IO_VGACRI, 0xa6, 0x60);
520 } else if (IS_AST_GEN3(ast) || IS_AST_GEN2(ast)) {
521 ast_set_index_reg(ast, AST_IO_VGACRI, 0xa7, 0x3f);
522 ast_set_index_reg(ast, AST_IO_VGACRI, 0xa6, 0x2f);
524 ast_set_index_reg(ast, AST_IO_VGACRI, 0xa7, 0x2f);
525 ast_set_index_reg(ast, AST_IO_VGACRI, 0xa6, 0x1f);
529 static void ast_set_sync_reg(struct ast_device *ast,
530 struct drm_display_mode *mode,
531 struct ast_vbios_mode_info *vbios_mode)
535 jreg = ast_io_read8(ast, AST_IO_VGAMR_R);
537 if (vbios_mode->enh_table->flags & NVSync)
539 if (vbios_mode->enh_table->flags & NHSync)
541 ast_io_write8(ast, AST_IO_VGAMR_W, jreg);
544 static void ast_set_start_address_crt1(struct ast_device *ast,
550 ast_set_index_reg(ast, AST_IO_VGACRI, 0x0d, (u8)(addr & 0xff));
551 ast_set_index_reg(ast, AST_IO_VGACRI, 0x0c, (u8)((addr >> 8) & 0xff));
552 ast_set_index_reg(ast, AST_IO_VGACRI, 0xaf, (u8)((addr >> 16) & 0xff));
556 static void ast_wait_for_vretrace(struct ast_device *ast)
558 unsigned long timeout = jiffies + HZ;
562 vgair1 = ast_io_read8(ast, AST_IO_VGAIR1_R);
563 } while (!(vgair1 & AST_IO_VGAIR1_VREFRESH) && time_before(jiffies, timeout));
570 static int ast_plane_init(struct drm_device *dev, struct ast_plane *ast_plane,
571 void __iomem *vaddr, u64 offset, unsigned long size,
572 uint32_t possible_crtcs,
573 const struct drm_plane_funcs *funcs,
574 const uint32_t *formats, unsigned int format_count,
575 const uint64_t *format_modifiers,
576 enum drm_plane_type type)
578 struct drm_plane *plane = &ast_plane->base;
580 ast_plane->vaddr = vaddr;
581 ast_plane->offset = offset;
582 ast_plane->size = size;
584 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
585 formats, format_count, format_modifiers,
593 static const uint32_t ast_primary_plane_formats[] = {
599 static int ast_primary_plane_helper_atomic_check(struct drm_plane *plane,
600 struct drm_atomic_state *state)
602 struct drm_device *dev = plane->dev;
603 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane);
604 struct drm_crtc_state *new_crtc_state = NULL;
605 struct ast_crtc_state *new_ast_crtc_state;
608 if (new_plane_state->crtc)
609 new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_state->crtc);
611 ret = drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state,
612 DRM_PLANE_NO_SCALING,
613 DRM_PLANE_NO_SCALING,
617 } else if (!new_plane_state->visible) {
618 if (drm_WARN_ON(dev, new_plane_state->crtc)) /* cannot legally happen */
624 new_ast_crtc_state = to_ast_crtc_state(new_crtc_state);
626 new_ast_crtc_state->format = new_plane_state->fb->format;
631 static void ast_handle_damage(struct ast_plane *ast_plane, struct iosys_map *src,
632 struct drm_framebuffer *fb,
633 const struct drm_rect *clip)
635 struct iosys_map dst = IOSYS_MAP_INIT_VADDR_IOMEM(ast_plane->vaddr);
637 iosys_map_incr(&dst, drm_fb_clip_offset(fb->pitches[0], fb->format, clip));
638 drm_fb_memcpy(&dst, fb->pitches, src, fb, clip);
641 static void ast_primary_plane_helper_atomic_update(struct drm_plane *plane,
642 struct drm_atomic_state *state)
644 struct drm_device *dev = plane->dev;
645 struct ast_device *ast = to_ast_device(dev);
646 struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
647 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
648 struct drm_framebuffer *fb = plane_state->fb;
649 struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
650 struct drm_framebuffer *old_fb = old_plane_state->fb;
651 struct ast_plane *ast_plane = to_ast_plane(plane);
652 struct drm_crtc *crtc = plane_state->crtc;
653 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
654 struct drm_rect damage;
655 struct drm_atomic_helper_damage_iter iter;
657 if (!old_fb || (fb->format != old_fb->format) || crtc_state->mode_changed) {
658 struct ast_crtc_state *ast_crtc_state = to_ast_crtc_state(crtc_state);
659 struct ast_vbios_mode_info *vbios_mode_info = &ast_crtc_state->vbios_mode_info;
661 ast_set_color_reg(ast, fb->format);
662 ast_set_vbios_color_reg(ast, fb->format, vbios_mode_info);
665 drm_atomic_helper_damage_iter_init(&iter, old_plane_state, plane_state);
666 drm_atomic_for_each_plane_damage(&iter, &damage) {
667 ast_handle_damage(ast_plane, shadow_plane_state->data, fb, &damage);
671 * Some BMCs stop scanning out the video signal after the driver
672 * reprogrammed the offset. This stalls display output for several
673 * seconds and makes the display unusable. Therefore only update
674 * the offset if it changes.
676 if (!old_fb || old_fb->pitches[0] != fb->pitches[0])
677 ast_set_offset_reg(ast, fb);
680 static void ast_primary_plane_helper_atomic_enable(struct drm_plane *plane,
681 struct drm_atomic_state *state)
683 struct ast_device *ast = to_ast_device(plane->dev);
684 struct ast_plane *ast_plane = to_ast_plane(plane);
687 * Some BMCs stop scanning out the video signal after the driver
688 * reprogrammed the scanout address. This stalls display
689 * output for several seconds and makes the display unusable.
690 * Therefore only reprogram the address after enabling the plane.
692 ast_set_start_address_crt1(ast, (u32)ast_plane->offset);
695 static void ast_primary_plane_helper_atomic_disable(struct drm_plane *plane,
696 struct drm_atomic_state *state)
699 * Keep this empty function to avoid calling
700 * atomic_update when disabling the plane.
704 static int ast_primary_plane_helper_get_scanout_buffer(struct drm_plane *plane,
705 struct drm_scanout_buffer *sb)
707 struct ast_plane *ast_plane = to_ast_plane(plane);
709 if (plane->state && plane->state->fb && ast_plane->vaddr) {
710 sb->format = plane->state->fb->format;
711 sb->width = plane->state->fb->width;
712 sb->height = plane->state->fb->height;
713 sb->pitch[0] = plane->state->fb->pitches[0];
714 iosys_map_set_vaddr_iomem(&sb->map[0], ast_plane->vaddr);
720 static const struct drm_plane_helper_funcs ast_primary_plane_helper_funcs = {
721 DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
722 .atomic_check = ast_primary_plane_helper_atomic_check,
723 .atomic_update = ast_primary_plane_helper_atomic_update,
724 .atomic_enable = ast_primary_plane_helper_atomic_enable,
725 .atomic_disable = ast_primary_plane_helper_atomic_disable,
726 .get_scanout_buffer = ast_primary_plane_helper_get_scanout_buffer,
729 static const struct drm_plane_funcs ast_primary_plane_funcs = {
730 .update_plane = drm_atomic_helper_update_plane,
731 .disable_plane = drm_atomic_helper_disable_plane,
732 .destroy = drm_plane_cleanup,
733 DRM_GEM_SHADOW_PLANE_FUNCS,
736 static int ast_primary_plane_init(struct ast_device *ast)
738 struct drm_device *dev = &ast->base;
739 struct ast_plane *ast_primary_plane = &ast->primary_plane;
740 struct drm_plane *primary_plane = &ast_primary_plane->base;
741 void __iomem *vaddr = ast->vram;
742 u64 offset = 0; /* with shmem, the primary plane is always at offset 0 */
743 unsigned long cursor_size = roundup(AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE, PAGE_SIZE);
744 unsigned long size = ast->vram_fb_available - cursor_size;
747 ret = ast_plane_init(dev, ast_primary_plane, vaddr, offset, size,
748 0x01, &ast_primary_plane_funcs,
749 ast_primary_plane_formats, ARRAY_SIZE(ast_primary_plane_formats),
750 NULL, DRM_PLANE_TYPE_PRIMARY);
752 drm_err(dev, "ast_plane_init() failed: %d\n", ret);
755 drm_plane_helper_add(primary_plane, &ast_primary_plane_helper_funcs);
756 drm_plane_enable_fb_damage_clips(primary_plane);
765 static void ast_update_cursor_image(u8 __iomem *dst, const u8 *src, int width, int height)
770 } srcdata32[2], data32;
776 s32 alpha_dst_delta, last_alpha_dst_delta;
780 u32 per_pixel_copy, two_pixel_copy;
782 alpha_dst_delta = AST_MAX_HWC_WIDTH << 1;
783 last_alpha_dst_delta = alpha_dst_delta - (width << 1);
786 dstxor = (u8 *)dst + last_alpha_dst_delta + (AST_MAX_HWC_HEIGHT - height) * alpha_dst_delta;
787 per_pixel_copy = width & 1;
788 two_pixel_copy = width >> 1;
790 for (j = 0; j < height; j++) {
791 for (i = 0; i < two_pixel_copy; i++) {
792 srcdata32[0].ul = *((u32 *)srcxor) & 0xf0f0f0f0;
793 srcdata32[1].ul = *((u32 *)(srcxor + 4)) & 0xf0f0f0f0;
794 data32.b[0] = srcdata32[0].b[1] | (srcdata32[0].b[0] >> 4);
795 data32.b[1] = srcdata32[0].b[3] | (srcdata32[0].b[2] >> 4);
796 data32.b[2] = srcdata32[1].b[1] | (srcdata32[1].b[0] >> 4);
797 data32.b[3] = srcdata32[1].b[3] | (srcdata32[1].b[2] >> 4);
799 writel(data32.ul, dstxor);
807 for (i = 0; i < per_pixel_copy; i++) {
808 srcdata32[0].ul = *((u32 *)srcxor) & 0xf0f0f0f0;
809 data16.b[0] = srcdata32[0].b[1] | (srcdata32[0].b[0] >> 4);
810 data16.b[1] = srcdata32[0].b[3] | (srcdata32[0].b[2] >> 4);
811 writew(data16.us, dstxor);
812 csum += (u32)data16.us;
817 dstxor += last_alpha_dst_delta;
820 /* write checksum + signature */
823 writel(width, dst + AST_HWC_SIGNATURE_SizeX);
824 writel(height, dst + AST_HWC_SIGNATURE_SizeY);
825 writel(0, dst + AST_HWC_SIGNATURE_HOTSPOTX);
826 writel(0, dst + AST_HWC_SIGNATURE_HOTSPOTY);
829 static void ast_set_cursor_base(struct ast_device *ast, u64 address)
831 u8 addr0 = (address >> 3) & 0xff;
832 u8 addr1 = (address >> 11) & 0xff;
833 u8 addr2 = (address >> 19) & 0xff;
835 ast_set_index_reg(ast, AST_IO_VGACRI, 0xc8, addr0);
836 ast_set_index_reg(ast, AST_IO_VGACRI, 0xc9, addr1);
837 ast_set_index_reg(ast, AST_IO_VGACRI, 0xca, addr2);
840 static void ast_set_cursor_location(struct ast_device *ast, u16 x, u16 y,
841 u8 x_offset, u8 y_offset)
843 u8 x0 = (x & 0x00ff);
844 u8 x1 = (x & 0x0f00) >> 8;
845 u8 y0 = (y & 0x00ff);
846 u8 y1 = (y & 0x0700) >> 8;
848 ast_set_index_reg(ast, AST_IO_VGACRI, 0xc2, x_offset);
849 ast_set_index_reg(ast, AST_IO_VGACRI, 0xc3, y_offset);
850 ast_set_index_reg(ast, AST_IO_VGACRI, 0xc4, x0);
851 ast_set_index_reg(ast, AST_IO_VGACRI, 0xc5, x1);
852 ast_set_index_reg(ast, AST_IO_VGACRI, 0xc6, y0);
853 ast_set_index_reg(ast, AST_IO_VGACRI, 0xc7, y1);
856 static void ast_set_cursor_enabled(struct ast_device *ast, bool enabled)
858 static const u8 mask = (u8)~(AST_IO_VGACRCB_HWC_16BPP |
859 AST_IO_VGACRCB_HWC_ENABLED);
861 u8 vgacrcb = AST_IO_VGACRCB_HWC_16BPP;
864 vgacrcb |= AST_IO_VGACRCB_HWC_ENABLED;
866 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xcb, mask, vgacrcb);
869 static const uint32_t ast_cursor_plane_formats[] = {
873 static int ast_cursor_plane_helper_atomic_check(struct drm_plane *plane,
874 struct drm_atomic_state *state)
876 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane);
877 struct drm_framebuffer *new_fb = new_plane_state->fb;
878 struct drm_crtc_state *new_crtc_state = NULL;
881 if (new_plane_state->crtc)
882 new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_state->crtc);
884 ret = drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state,
885 DRM_PLANE_NO_SCALING,
886 DRM_PLANE_NO_SCALING,
888 if (ret || !new_plane_state->visible)
891 if (new_fb->width > AST_MAX_HWC_WIDTH || new_fb->height > AST_MAX_HWC_HEIGHT)
897 static void ast_cursor_plane_helper_atomic_update(struct drm_plane *plane,
898 struct drm_atomic_state *state)
900 struct ast_plane *ast_plane = to_ast_plane(plane);
901 struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
902 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
903 struct drm_framebuffer *fb = plane_state->fb;
904 struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
905 struct ast_device *ast = to_ast_device(plane->dev);
906 struct iosys_map src_map = shadow_plane_state->data[0];
907 struct drm_rect damage;
908 const u8 *src = src_map.vaddr; /* TODO: Use mapping abstraction properly */
909 u64 dst_off = ast_plane->offset;
910 u8 __iomem *dst = ast_plane->vaddr; /* TODO: Use mapping abstraction properly */
911 u8 __iomem *sig = dst + AST_HWC_SIZE; /* TODO: Use mapping abstraction properly */
912 unsigned int offset_x, offset_y;
914 u8 x_offset, y_offset;
917 * Do data transfer to hardware buffer and point the scanout
918 * engine to the offset.
921 if (drm_atomic_helper_damage_merged(old_plane_state, plane_state, &damage)) {
922 ast_update_cursor_image(dst, src, fb->width, fb->height);
923 ast_set_cursor_base(ast, dst_off);
927 * Update location in HWC signature and registers.
930 writel(plane_state->crtc_x, sig + AST_HWC_SIGNATURE_X);
931 writel(plane_state->crtc_y, sig + AST_HWC_SIGNATURE_Y);
933 offset_x = AST_MAX_HWC_WIDTH - fb->width;
934 offset_y = AST_MAX_HWC_HEIGHT - fb->height;
936 if (plane_state->crtc_x < 0) {
937 x_offset = (-plane_state->crtc_x) + offset_x;
941 x = plane_state->crtc_x;
943 if (plane_state->crtc_y < 0) {
944 y_offset = (-plane_state->crtc_y) + offset_y;
948 y = plane_state->crtc_y;
951 ast_set_cursor_location(ast, x, y, x_offset, y_offset);
953 /* Dummy write to enable HWC and make the HW pick-up the changes. */
954 ast_set_cursor_enabled(ast, true);
957 static void ast_cursor_plane_helper_atomic_disable(struct drm_plane *plane,
958 struct drm_atomic_state *state)
960 struct ast_device *ast = to_ast_device(plane->dev);
962 ast_set_cursor_enabled(ast, false);
965 static const struct drm_plane_helper_funcs ast_cursor_plane_helper_funcs = {
966 DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
967 .atomic_check = ast_cursor_plane_helper_atomic_check,
968 .atomic_update = ast_cursor_plane_helper_atomic_update,
969 .atomic_disable = ast_cursor_plane_helper_atomic_disable,
972 static const struct drm_plane_funcs ast_cursor_plane_funcs = {
973 .update_plane = drm_atomic_helper_update_plane,
974 .disable_plane = drm_atomic_helper_disable_plane,
975 .destroy = drm_plane_cleanup,
976 DRM_GEM_SHADOW_PLANE_FUNCS,
979 static int ast_cursor_plane_init(struct ast_device *ast)
981 struct drm_device *dev = &ast->base;
982 struct ast_plane *ast_cursor_plane = &ast->cursor_plane;
983 struct drm_plane *cursor_plane = &ast_cursor_plane->base;
990 * Allocate backing storage for cursors. The BOs are permanently
991 * pinned to the top end of the VRAM.
994 size = roundup(AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE, PAGE_SIZE);
996 if (ast->vram_fb_available < size)
999 vaddr = ast->vram + ast->vram_fb_available - size;
1000 offset = ast->vram_fb_available - size;
1002 ret = ast_plane_init(dev, ast_cursor_plane, vaddr, offset, size,
1003 0x01, &ast_cursor_plane_funcs,
1004 ast_cursor_plane_formats, ARRAY_SIZE(ast_cursor_plane_formats),
1005 NULL, DRM_PLANE_TYPE_CURSOR);
1007 drm_err(dev, "ast_plane_init() failed: %d\n", ret);
1010 drm_plane_helper_add(cursor_plane, &ast_cursor_plane_helper_funcs);
1011 drm_plane_enable_fb_damage_clips(cursor_plane);
1013 ast->vram_fb_available -= size;
1022 static enum drm_mode_status
1023 ast_crtc_helper_mode_valid(struct drm_crtc *crtc, const struct drm_display_mode *mode)
1025 struct ast_device *ast = to_ast_device(crtc->dev);
1026 enum drm_mode_status status;
1029 if (ast->support_wide_screen) {
1030 if ((mode->hdisplay == 1680) && (mode->vdisplay == 1050))
1032 if ((mode->hdisplay == 1280) && (mode->vdisplay == 800))
1034 if ((mode->hdisplay == 1440) && (mode->vdisplay == 900))
1036 if ((mode->hdisplay == 1360) && (mode->vdisplay == 768))
1038 if ((mode->hdisplay == 1600) && (mode->vdisplay == 900))
1040 if ((mode->hdisplay == 1152) && (mode->vdisplay == 864))
1043 if ((ast->chip == AST2100) || // GEN2, but not AST1100 (?)
1044 (ast->chip == AST2200) || // GEN3, but not AST2150 (?)
1045 IS_AST_GEN4(ast) || IS_AST_GEN5(ast) ||
1046 IS_AST_GEN6(ast) || IS_AST_GEN7(ast)) {
1047 if ((mode->hdisplay == 1920) && (mode->vdisplay == 1080))
1050 if ((mode->hdisplay == 1920) && (mode->vdisplay == 1200)) {
1051 jtemp = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xd1, 0xff);
1060 status = MODE_NOMODE;
1062 switch (mode->hdisplay) {
1064 if (mode->vdisplay == 480)
1068 if (mode->vdisplay == 600)
1072 if (mode->vdisplay == 768)
1076 if (mode->vdisplay == 864)
1080 if (mode->vdisplay == 1024)
1084 if (mode->vdisplay == 1200)
1094 static void ast_crtc_helper_mode_set_nofb(struct drm_crtc *crtc)
1096 struct drm_device *dev = crtc->dev;
1097 struct ast_device *ast = to_ast_device(dev);
1098 struct drm_crtc_state *crtc_state = crtc->state;
1099 struct ast_crtc_state *ast_crtc_state = to_ast_crtc_state(crtc_state);
1100 struct ast_vbios_mode_info *vbios_mode_info =
1101 &ast_crtc_state->vbios_mode_info;
1102 struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
1105 * Ensure that no scanout takes place before reprogramming mode
1106 * and format registers.
1108 * TODO: Get vblank interrupts working and remove this line.
1110 ast_wait_for_vretrace(ast);
1112 ast_set_vbios_mode_reg(ast, adjusted_mode, vbios_mode_info);
1113 ast_set_index_reg(ast, AST_IO_VGACRI, 0xa1, 0x06);
1114 ast_set_std_reg(ast, adjusted_mode, vbios_mode_info);
1115 ast_set_crtc_reg(ast, adjusted_mode, vbios_mode_info);
1116 ast_set_dclk_reg(ast, adjusted_mode, vbios_mode_info);
1117 ast_set_crtthd_reg(ast);
1118 ast_set_sync_reg(ast, adjusted_mode, vbios_mode_info);
1121 static int ast_crtc_helper_atomic_check(struct drm_crtc *crtc,
1122 struct drm_atomic_state *state)
1124 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1125 struct drm_crtc_state *old_crtc_state = drm_atomic_get_old_crtc_state(state, crtc);
1126 struct ast_crtc_state *old_ast_crtc_state = to_ast_crtc_state(old_crtc_state);
1127 struct drm_device *dev = crtc->dev;
1128 struct ast_crtc_state *ast_state;
1129 const struct drm_format_info *format;
1133 if (!crtc_state->enable)
1136 ret = drm_atomic_helper_check_crtc_primary_plane(crtc_state);
1140 ast_state = to_ast_crtc_state(crtc_state);
1142 format = ast_state->format;
1143 if (drm_WARN_ON_ONCE(dev, !format))
1144 return -EINVAL; /* BUG: We didn't set format in primary check(). */
1147 * The gamma LUT has to be reloaded after changing the primary
1148 * plane's color format.
1150 if (old_ast_crtc_state->format != format)
1151 crtc_state->color_mgmt_changed = true;
1153 if (crtc_state->color_mgmt_changed && crtc_state->gamma_lut) {
1154 if (crtc_state->gamma_lut->length !=
1155 AST_LUT_SIZE * sizeof(struct drm_color_lut)) {
1156 drm_err(dev, "Wrong size for gamma_lut %zu\n",
1157 crtc_state->gamma_lut->length);
1162 succ = ast_get_vbios_mode_info(format, &crtc_state->mode,
1163 &crtc_state->adjusted_mode,
1164 &ast_state->vbios_mode_info);
1172 ast_crtc_helper_atomic_flush(struct drm_crtc *crtc,
1173 struct drm_atomic_state *state)
1175 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
1177 struct drm_device *dev = crtc->dev;
1178 struct ast_device *ast = to_ast_device(dev);
1179 struct ast_crtc_state *ast_crtc_state = to_ast_crtc_state(crtc_state);
1182 * The gamma LUT has to be reloaded after changing the primary
1183 * plane's color format.
1185 if (crtc_state->enable && crtc_state->color_mgmt_changed) {
1186 if (crtc_state->gamma_lut)
1187 ast_crtc_set_gamma(ast,
1188 ast_crtc_state->format,
1189 crtc_state->gamma_lut->data);
1191 ast_crtc_set_gamma_linear(ast, ast_crtc_state->format);
1195 static void ast_crtc_helper_atomic_enable(struct drm_crtc *crtc, struct drm_atomic_state *state)
1197 struct ast_device *ast = to_ast_device(crtc->dev);
1199 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xb6, 0xfc, 0x00);
1200 ast_set_index_reg_mask(ast, AST_IO_VGASRI, 0x01, 0xdf, 0x00);
1203 static void ast_crtc_helper_atomic_disable(struct drm_crtc *crtc, struct drm_atomic_state *state)
1205 struct drm_crtc_state *old_crtc_state = drm_atomic_get_old_crtc_state(state, crtc);
1206 struct ast_device *ast = to_ast_device(crtc->dev);
1209 ast_set_index_reg_mask(ast, AST_IO_VGASRI, 0x01, 0xdf, AST_IO_VGASR1_SD);
1211 vgacrb6 = AST_IO_VGACRB6_VSYNC_OFF |
1212 AST_IO_VGACRB6_HSYNC_OFF;
1213 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xb6, 0xfc, vgacrb6);
1216 * HW cursors require the underlying primary plane and CRTC to
1217 * display a valid mode and image. This is not the case during
1218 * full modeset operations. So we temporarily disable any active
1219 * plane, including the HW cursor. Each plane's atomic_update()
1220 * helper will re-enable it if necessary.
1222 * We only do this during *full* modesets. It does not affect
1223 * simple pageflips on the planes.
1225 drm_atomic_helper_disable_planes_on_crtc(old_crtc_state, false);
1228 static const struct drm_crtc_helper_funcs ast_crtc_helper_funcs = {
1229 .mode_valid = ast_crtc_helper_mode_valid,
1230 .mode_set_nofb = ast_crtc_helper_mode_set_nofb,
1231 .atomic_check = ast_crtc_helper_atomic_check,
1232 .atomic_flush = ast_crtc_helper_atomic_flush,
1233 .atomic_enable = ast_crtc_helper_atomic_enable,
1234 .atomic_disable = ast_crtc_helper_atomic_disable,
1237 static void ast_crtc_reset(struct drm_crtc *crtc)
1239 struct ast_crtc_state *ast_state =
1240 kzalloc(sizeof(*ast_state), GFP_KERNEL);
1243 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
1246 __drm_atomic_helper_crtc_reset(crtc, &ast_state->base);
1248 __drm_atomic_helper_crtc_reset(crtc, NULL);
1251 static struct drm_crtc_state *
1252 ast_crtc_atomic_duplicate_state(struct drm_crtc *crtc)
1254 struct ast_crtc_state *new_ast_state, *ast_state;
1255 struct drm_device *dev = crtc->dev;
1257 if (drm_WARN_ON(dev, !crtc->state))
1260 new_ast_state = kmalloc(sizeof(*new_ast_state), GFP_KERNEL);
1263 __drm_atomic_helper_crtc_duplicate_state(crtc, &new_ast_state->base);
1265 ast_state = to_ast_crtc_state(crtc->state);
1267 new_ast_state->format = ast_state->format;
1268 memcpy(&new_ast_state->vbios_mode_info, &ast_state->vbios_mode_info,
1269 sizeof(new_ast_state->vbios_mode_info));
1271 return &new_ast_state->base;
1274 static void ast_crtc_atomic_destroy_state(struct drm_crtc *crtc,
1275 struct drm_crtc_state *state)
1277 struct ast_crtc_state *ast_state = to_ast_crtc_state(state);
1279 __drm_atomic_helper_crtc_destroy_state(&ast_state->base);
1283 static const struct drm_crtc_funcs ast_crtc_funcs = {
1284 .reset = ast_crtc_reset,
1285 .destroy = drm_crtc_cleanup,
1286 .set_config = drm_atomic_helper_set_config,
1287 .page_flip = drm_atomic_helper_page_flip,
1288 .atomic_duplicate_state = ast_crtc_atomic_duplicate_state,
1289 .atomic_destroy_state = ast_crtc_atomic_destroy_state,
1292 static int ast_crtc_init(struct drm_device *dev)
1294 struct ast_device *ast = to_ast_device(dev);
1295 struct drm_crtc *crtc = &ast->crtc;
1298 ret = drm_crtc_init_with_planes(dev, crtc, &ast->primary_plane.base,
1299 &ast->cursor_plane.base, &ast_crtc_funcs,
1304 drm_mode_crtc_set_gamma_size(crtc, AST_LUT_SIZE);
1305 drm_crtc_enable_color_mgmt(crtc, 0, false, AST_LUT_SIZE);
1307 drm_crtc_helper_add(crtc, &ast_crtc_helper_funcs);
1316 static const struct drm_encoder_funcs ast_vga_encoder_funcs = {
1317 .destroy = drm_encoder_cleanup,
1324 static const struct drm_connector_helper_funcs ast_vga_connector_helper_funcs = {
1325 .get_modes = drm_connector_helper_get_modes,
1326 .detect_ctx = drm_connector_helper_detect_from_ddc,
1329 static const struct drm_connector_funcs ast_vga_connector_funcs = {
1330 .reset = drm_atomic_helper_connector_reset,
1331 .fill_modes = drm_helper_probe_single_connector_modes,
1332 .destroy = drm_connector_cleanup,
1333 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1334 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1337 static int ast_vga_connector_init(struct drm_device *dev, struct drm_connector *connector)
1339 struct ast_device *ast = to_ast_device(dev);
1340 struct i2c_adapter *ddc;
1343 ddc = ast_ddc_create(ast);
1346 drm_err(dev, "failed to add DDC bus for connector; ret=%d\n", ret);
1350 ret = drm_connector_init_with_ddc(dev, connector, &ast_vga_connector_funcs,
1351 DRM_MODE_CONNECTOR_VGA, ddc);
1355 drm_connector_helper_add(connector, &ast_vga_connector_helper_funcs);
1357 connector->interlace_allowed = 0;
1358 connector->doublescan_allowed = 0;
1360 connector->polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
1365 static int ast_vga_output_init(struct ast_device *ast)
1367 struct drm_device *dev = &ast->base;
1368 struct drm_crtc *crtc = &ast->crtc;
1369 struct drm_encoder *encoder = &ast->output.vga.encoder;
1370 struct drm_connector *connector = &ast->output.vga.connector;
1373 ret = drm_encoder_init(dev, encoder, &ast_vga_encoder_funcs,
1374 DRM_MODE_ENCODER_DAC, NULL);
1377 encoder->possible_crtcs = drm_crtc_mask(crtc);
1379 ret = ast_vga_connector_init(dev, connector);
1383 ret = drm_connector_attach_encoder(connector, encoder);
1394 static const struct drm_encoder_funcs ast_sil164_encoder_funcs = {
1395 .destroy = drm_encoder_cleanup,
1402 static const struct drm_connector_helper_funcs ast_sil164_connector_helper_funcs = {
1403 .get_modes = drm_connector_helper_get_modes,
1404 .detect_ctx = drm_connector_helper_detect_from_ddc,
1407 static const struct drm_connector_funcs ast_sil164_connector_funcs = {
1408 .reset = drm_atomic_helper_connector_reset,
1409 .fill_modes = drm_helper_probe_single_connector_modes,
1410 .destroy = drm_connector_cleanup,
1411 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1412 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1415 static int ast_sil164_connector_init(struct drm_device *dev, struct drm_connector *connector)
1417 struct ast_device *ast = to_ast_device(dev);
1418 struct i2c_adapter *ddc;
1421 ddc = ast_ddc_create(ast);
1424 drm_err(dev, "failed to add DDC bus for connector; ret=%d\n", ret);
1428 ret = drm_connector_init_with_ddc(dev, connector, &ast_sil164_connector_funcs,
1429 DRM_MODE_CONNECTOR_DVII, ddc);
1433 drm_connector_helper_add(connector, &ast_sil164_connector_helper_funcs);
1435 connector->interlace_allowed = 0;
1436 connector->doublescan_allowed = 0;
1438 connector->polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
1443 static int ast_sil164_output_init(struct ast_device *ast)
1445 struct drm_device *dev = &ast->base;
1446 struct drm_crtc *crtc = &ast->crtc;
1447 struct drm_encoder *encoder = &ast->output.sil164.encoder;
1448 struct drm_connector *connector = &ast->output.sil164.connector;
1451 ret = drm_encoder_init(dev, encoder, &ast_sil164_encoder_funcs,
1452 DRM_MODE_ENCODER_TMDS, NULL);
1455 encoder->possible_crtcs = drm_crtc_mask(crtc);
1457 ret = ast_sil164_connector_init(dev, connector);
1461 ret = drm_connector_attach_encoder(connector, encoder);
1472 static const struct drm_encoder_funcs ast_dp501_encoder_funcs = {
1473 .destroy = drm_encoder_cleanup,
1476 static void ast_dp501_encoder_helper_atomic_enable(struct drm_encoder *encoder,
1477 struct drm_atomic_state *state)
1479 struct drm_device *dev = encoder->dev;
1481 ast_set_dp501_video_output(dev, 1);
1484 static void ast_dp501_encoder_helper_atomic_disable(struct drm_encoder *encoder,
1485 struct drm_atomic_state *state)
1487 struct drm_device *dev = encoder->dev;
1489 ast_set_dp501_video_output(dev, 0);
1492 static const struct drm_encoder_helper_funcs ast_dp501_encoder_helper_funcs = {
1493 .atomic_enable = ast_dp501_encoder_helper_atomic_enable,
1494 .atomic_disable = ast_dp501_encoder_helper_atomic_disable,
1501 static int ast_dp501_connector_helper_get_modes(struct drm_connector *connector)
1507 edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
1509 goto err_drm_connector_update_edid_property;
1511 succ = ast_dp501_read_edid(connector->dev, edid);
1515 drm_connector_update_edid_property(connector, edid);
1516 count = drm_add_edid_modes(connector, edid);
1523 err_drm_connector_update_edid_property:
1524 drm_connector_update_edid_property(connector, NULL);
1528 static int ast_dp501_connector_helper_detect_ctx(struct drm_connector *connector,
1529 struct drm_modeset_acquire_ctx *ctx,
1532 struct ast_device *ast = to_ast_device(connector->dev);
1534 if (ast_dp501_is_connected(ast))
1535 return connector_status_connected;
1536 return connector_status_disconnected;
1539 static const struct drm_connector_helper_funcs ast_dp501_connector_helper_funcs = {
1540 .get_modes = ast_dp501_connector_helper_get_modes,
1541 .detect_ctx = ast_dp501_connector_helper_detect_ctx,
1544 static const struct drm_connector_funcs ast_dp501_connector_funcs = {
1545 .reset = drm_atomic_helper_connector_reset,
1546 .fill_modes = drm_helper_probe_single_connector_modes,
1547 .destroy = drm_connector_cleanup,
1548 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1549 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1552 static int ast_dp501_connector_init(struct drm_device *dev, struct drm_connector *connector)
1556 ret = drm_connector_init(dev, connector, &ast_dp501_connector_funcs,
1557 DRM_MODE_CONNECTOR_DisplayPort);
1561 drm_connector_helper_add(connector, &ast_dp501_connector_helper_funcs);
1563 connector->interlace_allowed = 0;
1564 connector->doublescan_allowed = 0;
1566 connector->polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
1571 static int ast_dp501_output_init(struct ast_device *ast)
1573 struct drm_device *dev = &ast->base;
1574 struct drm_crtc *crtc = &ast->crtc;
1575 struct drm_encoder *encoder = &ast->output.dp501.encoder;
1576 struct drm_connector *connector = &ast->output.dp501.connector;
1579 ret = drm_encoder_init(dev, encoder, &ast_dp501_encoder_funcs,
1580 DRM_MODE_ENCODER_TMDS, NULL);
1583 drm_encoder_helper_add(encoder, &ast_dp501_encoder_helper_funcs);
1585 encoder->possible_crtcs = drm_crtc_mask(crtc);
1587 ret = ast_dp501_connector_init(dev, connector);
1591 ret = drm_connector_attach_encoder(connector, encoder);
1599 * ASPEED Display-Port Encoder
1602 static const struct drm_encoder_funcs ast_astdp_encoder_funcs = {
1603 .destroy = drm_encoder_cleanup,
1606 static void ast_astdp_encoder_helper_atomic_mode_set(struct drm_encoder *encoder,
1607 struct drm_crtc_state *crtc_state,
1608 struct drm_connector_state *conn_state)
1610 struct drm_crtc *crtc = crtc_state->crtc;
1611 struct ast_crtc_state *ast_crtc_state = to_ast_crtc_state(crtc_state);
1612 struct ast_vbios_mode_info *vbios_mode_info = &ast_crtc_state->vbios_mode_info;
1614 ast_dp_set_mode(crtc, vbios_mode_info);
1617 static void ast_astdp_encoder_helper_atomic_enable(struct drm_encoder *encoder,
1618 struct drm_atomic_state *state)
1620 struct drm_device *dev = encoder->dev;
1621 struct ast_device *ast = to_ast_device(dev);
1623 ast_dp_power_on_off(dev, AST_DP_POWER_ON);
1624 ast_wait_for_vretrace(ast);
1625 ast_dp_set_on_off(dev, 1);
1628 static void ast_astdp_encoder_helper_atomic_disable(struct drm_encoder *encoder,
1629 struct drm_atomic_state *state)
1631 struct drm_device *dev = encoder->dev;
1633 ast_dp_set_on_off(dev, 0);
1634 ast_dp_power_on_off(dev, AST_DP_POWER_OFF);
1637 static const struct drm_encoder_helper_funcs ast_astdp_encoder_helper_funcs = {
1638 .atomic_mode_set = ast_astdp_encoder_helper_atomic_mode_set,
1639 .atomic_enable = ast_astdp_encoder_helper_atomic_enable,
1640 .atomic_disable = ast_astdp_encoder_helper_atomic_disable,
1644 * ASPEED Display-Port Connector
1647 static int ast_astdp_connector_helper_get_modes(struct drm_connector *connector)
1650 struct drm_device *dev = connector->dev;
1651 struct ast_device *ast = to_ast_device(dev);
1656 edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
1658 goto err_drm_connector_update_edid_property;
1661 * Protect access to I/O registers from concurrent modesetting
1662 * by acquiring the I/O-register lock.
1664 mutex_lock(&ast->modeset_lock);
1666 succ = ast_astdp_read_edid(connector->dev, edid);
1668 goto err_mutex_unlock;
1670 mutex_unlock(&ast->modeset_lock);
1672 drm_connector_update_edid_property(connector, edid);
1673 count = drm_add_edid_modes(connector, edid);
1679 mutex_unlock(&ast->modeset_lock);
1681 err_drm_connector_update_edid_property:
1682 drm_connector_update_edid_property(connector, NULL);
1686 static int ast_astdp_connector_helper_detect_ctx(struct drm_connector *connector,
1687 struct drm_modeset_acquire_ctx *ctx,
1690 struct ast_device *ast = to_ast_device(connector->dev);
1692 if (ast_astdp_is_connected(ast))
1693 return connector_status_connected;
1694 return connector_status_disconnected;
1697 static const struct drm_connector_helper_funcs ast_astdp_connector_helper_funcs = {
1698 .get_modes = ast_astdp_connector_helper_get_modes,
1699 .detect_ctx = ast_astdp_connector_helper_detect_ctx,
1702 static const struct drm_connector_funcs ast_astdp_connector_funcs = {
1703 .reset = drm_atomic_helper_connector_reset,
1704 .fill_modes = drm_helper_probe_single_connector_modes,
1705 .destroy = drm_connector_cleanup,
1706 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1707 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1710 static int ast_astdp_connector_init(struct drm_device *dev, struct drm_connector *connector)
1714 ret = drm_connector_init(dev, connector, &ast_astdp_connector_funcs,
1715 DRM_MODE_CONNECTOR_DisplayPort);
1719 drm_connector_helper_add(connector, &ast_astdp_connector_helper_funcs);
1721 connector->interlace_allowed = 0;
1722 connector->doublescan_allowed = 0;
1724 connector->polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
1729 static int ast_astdp_output_init(struct ast_device *ast)
1731 struct drm_device *dev = &ast->base;
1732 struct drm_crtc *crtc = &ast->crtc;
1733 struct drm_encoder *encoder = &ast->output.astdp.encoder;
1734 struct drm_connector *connector = &ast->output.astdp.connector;
1737 ret = drm_encoder_init(dev, encoder, &ast_astdp_encoder_funcs,
1738 DRM_MODE_ENCODER_TMDS, NULL);
1741 drm_encoder_helper_add(encoder, &ast_astdp_encoder_helper_funcs);
1743 encoder->possible_crtcs = drm_crtc_mask(crtc);
1745 ret = ast_astdp_connector_init(dev, connector);
1749 ret = drm_connector_attach_encoder(connector, encoder);
1757 * BMC virtual Connector
1760 static const struct drm_encoder_funcs ast_bmc_encoder_funcs = {
1761 .destroy = drm_encoder_cleanup,
1764 static int ast_bmc_connector_helper_detect_ctx(struct drm_connector *connector,
1765 struct drm_modeset_acquire_ctx *ctx,
1768 struct ast_bmc_connector *bmc_connector = to_ast_bmc_connector(connector);
1769 struct drm_connector *physical_connector = bmc_connector->physical_connector;
1772 * Most user-space compositors cannot handle more than one connected
1773 * connector per CRTC. Hence, we only mark the BMC as connected if the
1774 * physical connector is disconnected. If the physical connector's status
1775 * is connected or unknown, the BMC remains disconnected. This has no
1776 * effect on the output of the BMC.
1778 * FIXME: Remove this logic once user-space compositors can handle more
1779 * than one connector per CRTC. The BMC should always be connected.
1782 if (physical_connector && physical_connector->status == connector_status_disconnected)
1783 return connector_status_connected;
1785 return connector_status_disconnected;
1788 static int ast_bmc_connector_helper_get_modes(struct drm_connector *connector)
1790 return drm_add_modes_noedid(connector, 4096, 4096);
1793 static const struct drm_connector_helper_funcs ast_bmc_connector_helper_funcs = {
1794 .get_modes = ast_bmc_connector_helper_get_modes,
1795 .detect_ctx = ast_bmc_connector_helper_detect_ctx,
1798 static const struct drm_connector_funcs ast_bmc_connector_funcs = {
1799 .reset = drm_atomic_helper_connector_reset,
1800 .fill_modes = drm_helper_probe_single_connector_modes,
1801 .destroy = drm_connector_cleanup,
1802 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1803 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1806 static int ast_bmc_connector_init(struct drm_device *dev,
1807 struct ast_bmc_connector *bmc_connector,
1808 struct drm_connector *physical_connector)
1810 struct drm_connector *connector = &bmc_connector->base;
1813 ret = drm_connector_init(dev, connector, &ast_bmc_connector_funcs,
1814 DRM_MODE_CONNECTOR_VIRTUAL);
1818 drm_connector_helper_add(connector, &ast_bmc_connector_helper_funcs);
1820 bmc_connector->physical_connector = physical_connector;
1825 static int ast_bmc_output_init(struct ast_device *ast,
1826 struct drm_connector *physical_connector)
1828 struct drm_device *dev = &ast->base;
1829 struct drm_crtc *crtc = &ast->crtc;
1830 struct drm_encoder *encoder = &ast->output.bmc.encoder;
1831 struct ast_bmc_connector *bmc_connector = &ast->output.bmc.bmc_connector;
1832 struct drm_connector *connector = &bmc_connector->base;
1835 ret = drm_encoder_init(dev, encoder,
1836 &ast_bmc_encoder_funcs,
1837 DRM_MODE_ENCODER_VIRTUAL, "ast_bmc");
1840 encoder->possible_crtcs = drm_crtc_mask(crtc);
1842 ret = ast_bmc_connector_init(dev, bmc_connector, physical_connector);
1846 ret = drm_connector_attach_encoder(connector, encoder);
1857 static void ast_mode_config_helper_atomic_commit_tail(struct drm_atomic_state *state)
1859 struct ast_device *ast = to_ast_device(state->dev);
1862 * Concurrent operations could possibly trigger a call to
1863 * drm_connector_helper_funcs.get_modes by trying to read the
1864 * display modes. Protect access to I/O registers by acquiring
1865 * the I/O-register lock. Released in atomic_flush().
1867 mutex_lock(&ast->modeset_lock);
1868 drm_atomic_helper_commit_tail(state);
1869 mutex_unlock(&ast->modeset_lock);
1872 static const struct drm_mode_config_helper_funcs ast_mode_config_helper_funcs = {
1873 .atomic_commit_tail = ast_mode_config_helper_atomic_commit_tail,
1876 static enum drm_mode_status ast_mode_config_mode_valid(struct drm_device *dev,
1877 const struct drm_display_mode *mode)
1879 static const unsigned long max_bpp = 4; /* DRM_FORMAT_XRGB8888 */
1880 struct ast_device *ast = to_ast_device(dev);
1881 unsigned long fbsize, fbpages, max_fbpages;
1883 max_fbpages = (ast->vram_fb_available) >> PAGE_SHIFT;
1885 fbsize = mode->hdisplay * mode->vdisplay * max_bpp;
1886 fbpages = DIV_ROUND_UP(fbsize, PAGE_SIZE);
1888 if (fbpages > max_fbpages)
1894 static const struct drm_mode_config_funcs ast_mode_config_funcs = {
1895 .fb_create = drm_gem_fb_create_with_dirty,
1896 .mode_valid = ast_mode_config_mode_valid,
1897 .atomic_check = drm_atomic_helper_check,
1898 .atomic_commit = drm_atomic_helper_commit,
1901 int ast_mode_config_init(struct ast_device *ast)
1903 struct drm_device *dev = &ast->base;
1904 struct drm_connector *physical_connector = NULL;
1907 ret = drmm_mutex_init(dev, &ast->modeset_lock);
1911 ret = drmm_mode_config_init(dev);
1915 dev->mode_config.funcs = &ast_mode_config_funcs;
1916 dev->mode_config.min_width = 0;
1917 dev->mode_config.min_height = 0;
1918 dev->mode_config.preferred_depth = 24;
1920 if (ast->chip == AST2100 || // GEN2, but not AST1100 (?)
1921 ast->chip == AST2200 || // GEN3, but not AST2150 (?)
1926 dev->mode_config.max_width = 1920;
1927 dev->mode_config.max_height = 2048;
1929 dev->mode_config.max_width = 1600;
1930 dev->mode_config.max_height = 1200;
1933 dev->mode_config.helper_private = &ast_mode_config_helper_funcs;
1935 ret = ast_primary_plane_init(ast);
1939 ret = ast_cursor_plane_init(ast);
1945 if (ast->tx_chip_types & AST_TX_NONE_BIT) {
1946 ret = ast_vga_output_init(ast);
1949 physical_connector = &ast->output.vga.connector;
1951 if (ast->tx_chip_types & AST_TX_SIL164_BIT) {
1952 ret = ast_sil164_output_init(ast);
1955 physical_connector = &ast->output.sil164.connector;
1957 if (ast->tx_chip_types & AST_TX_DP501_BIT) {
1958 ret = ast_dp501_output_init(ast);
1961 physical_connector = &ast->output.dp501.connector;
1963 if (ast->tx_chip_types & AST_TX_ASTDP_BIT) {
1964 ret = ast_astdp_output_init(ast);
1967 physical_connector = &ast->output.astdp.connector;
1969 ret = ast_bmc_output_init(ast, physical_connector);
1973 drm_mode_config_reset(dev);
1975 ret = drmm_kms_helper_poll_init(dev);