112eb90f1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 25320918bSDave Airlie /* 35320918bSDave Airlie * Copyright (C) 2012 Red Hat 45320918bSDave Airlie * 55320918bSDave Airlie * based in parts on udlfb.c: 65320918bSDave Airlie * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it> 75320918bSDave Airlie * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com> 85320918bSDave Airlie * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com> 95320918bSDave Airlie */ 105320918bSDave Airlie 11*44f29ad9SThomas Zimmermann #include <linux/bitfield.h> 12*44f29ad9SThomas Zimmermann 1372d73dd3SThomas Zimmermann #include <drm/drm_atomic.h> 149fda81e0SThomas Zimmermann #include <drm/drm_atomic_helper.h> 15760285e7SDavid Howells #include <drm/drm_crtc_helper.h> 16230b8b04SThomas Zimmermann #include <drm/drm_damage_helper.h> 17ca2bd373SThomas Zimmermann #include <drm/drm_drv.h> 180862cfd3SThomas Zimmermann #include <drm/drm_edid.h> 19a8109f5bSThomas Zimmermann #include <drm/drm_fourcc.h> 205ceeb328SThomas Zimmermann #include <drm/drm_gem_atomic_helper.h> 219fda81e0SThomas Zimmermann #include <drm/drm_gem_framebuffer_helper.h> 22a8109f5bSThomas Zimmermann #include <drm/drm_gem_shmem_helper.h> 23a9dcf380SSam Ravnborg #include <drm/drm_modeset_helper_vtables.h> 2472d73dd3SThomas Zimmermann #include <drm/drm_plane_helper.h> 250862cfd3SThomas Zimmermann #include <drm/drm_probe_helper.h> 26a9dcf380SSam Ravnborg #include <drm/drm_vblank.h> 27a9dcf380SSam Ravnborg 285320918bSDave Airlie #include "udl_drv.h" 29ff76e82cSThomas Zimmermann #include "udl_proto.h" 309fda81e0SThomas Zimmermann 315320918bSDave Airlie /* 325320918bSDave Airlie * All DisplayLink bulk operations start with 0xAF, followed by specific code 335320918bSDave Airlie * All operations are written to buffers which then later get sent to device 345320918bSDave Airlie */ 355320918bSDave Airlie static char *udl_set_register(char *buf, u8 reg, u8 val) 365320918bSDave Airlie { 375320918bSDave Airlie *buf++ = 0xAF; 385320918bSDave Airlie *buf++ = 0x20; 395320918bSDave Airlie *buf++ = reg; 405320918bSDave Airlie *buf++ = val; 415320918bSDave Airlie return buf; 425320918bSDave Airlie } 435320918bSDave Airlie 445320918bSDave Airlie static char *udl_vidreg_lock(char *buf) 455320918bSDave Airlie { 46cb7b995dSThomas Zimmermann return udl_set_register(buf, UDL_REG_VIDREG, UDL_VIDREG_LOCK); 475320918bSDave Airlie } 485320918bSDave Airlie 495320918bSDave Airlie static char *udl_vidreg_unlock(char *buf) 505320918bSDave Airlie { 51cb7b995dSThomas Zimmermann return udl_set_register(buf, UDL_REG_VIDREG, UDL_VIDREG_UNLOCK); 525320918bSDave Airlie } 535320918bSDave Airlie 54997d33c3SThomas Zimmermann static char *udl_set_blank_mode(char *buf, u8 mode) 555320918bSDave Airlie { 56ff76e82cSThomas Zimmermann return udl_set_register(buf, UDL_REG_BLANKMODE, mode); 575320918bSDave Airlie } 585320918bSDave Airlie 595320918bSDave Airlie static char *udl_set_color_depth(char *buf, u8 selection) 605320918bSDave Airlie { 61ed24ed48SThomas Zimmermann return udl_set_register(buf, UDL_REG_COLORDEPTH, selection); 625320918bSDave Airlie } 635320918bSDave Airlie 64*44f29ad9SThomas Zimmermann static char *udl_set_base16bpp(char *buf, u32 base) 655320918bSDave Airlie { 66*44f29ad9SThomas Zimmermann /* the base pointer is 24 bits wide, 0x20 is hi byte. */ 67*44f29ad9SThomas Zimmermann u8 reg20 = FIELD_GET(UDL_BASE_ADDR2_MASK, base); 68*44f29ad9SThomas Zimmermann u8 reg21 = FIELD_GET(UDL_BASE_ADDR1_MASK, base); 69*44f29ad9SThomas Zimmermann u8 reg22 = FIELD_GET(UDL_BASE_ADDR0_MASK, base); 70*44f29ad9SThomas Zimmermann 71*44f29ad9SThomas Zimmermann buf = udl_set_register(buf, UDL_REG_BASE16BPP_ADDR2, reg20); 72*44f29ad9SThomas Zimmermann buf = udl_set_register(buf, UDL_REG_BASE16BPP_ADDR1, reg21); 73*44f29ad9SThomas Zimmermann buf = udl_set_register(buf, UDL_REG_BASE16BPP_ADDR0, reg22); 74*44f29ad9SThomas Zimmermann 75*44f29ad9SThomas Zimmermann return buf; 765320918bSDave Airlie } 775320918bSDave Airlie 785320918bSDave Airlie /* 795320918bSDave Airlie * DisplayLink HW has separate 16bpp and 8bpp framebuffers. 805320918bSDave Airlie * In 24bpp modes, the low 323 RGB bits go in the 8bpp framebuffer 815320918bSDave Airlie */ 82*44f29ad9SThomas Zimmermann static char *udl_set_base8bpp(char *buf, u32 base) 835320918bSDave Airlie { 84*44f29ad9SThomas Zimmermann /* the base pointer is 24 bits wide, 0x26 is hi byte. */ 85*44f29ad9SThomas Zimmermann u8 reg26 = FIELD_GET(UDL_BASE_ADDR2_MASK, base); 86*44f29ad9SThomas Zimmermann u8 reg27 = FIELD_GET(UDL_BASE_ADDR1_MASK, base); 87*44f29ad9SThomas Zimmermann u8 reg28 = FIELD_GET(UDL_BASE_ADDR0_MASK, base); 88*44f29ad9SThomas Zimmermann 89*44f29ad9SThomas Zimmermann buf = udl_set_register(buf, UDL_REG_BASE8BPP_ADDR2, reg26); 90*44f29ad9SThomas Zimmermann buf = udl_set_register(buf, UDL_REG_BASE8BPP_ADDR1, reg27); 91*44f29ad9SThomas Zimmermann buf = udl_set_register(buf, UDL_REG_BASE8BPP_ADDR0, reg28); 92*44f29ad9SThomas Zimmermann 93*44f29ad9SThomas Zimmermann return buf; 945320918bSDave Airlie } 955320918bSDave Airlie 965320918bSDave Airlie static char *udl_set_register_16(char *wrptr, u8 reg, u16 value) 975320918bSDave Airlie { 985320918bSDave Airlie wrptr = udl_set_register(wrptr, reg, value >> 8); 995320918bSDave Airlie return udl_set_register(wrptr, reg+1, value); 1005320918bSDave Airlie } 1015320918bSDave Airlie 1025320918bSDave Airlie /* 1035320918bSDave Airlie * This is kind of weird because the controller takes some 1045320918bSDave Airlie * register values in a different byte order than other registers. 1055320918bSDave Airlie */ 1065320918bSDave Airlie static char *udl_set_register_16be(char *wrptr, u8 reg, u16 value) 1075320918bSDave Airlie { 1085320918bSDave Airlie wrptr = udl_set_register(wrptr, reg, value); 1095320918bSDave Airlie return udl_set_register(wrptr, reg+1, value >> 8); 1105320918bSDave Airlie } 1115320918bSDave Airlie 1125320918bSDave Airlie /* 1135320918bSDave Airlie * LFSR is linear feedback shift register. The reason we have this is 1145320918bSDave Airlie * because the display controller needs to minimize the clock depth of 1155320918bSDave Airlie * various counters used in the display path. So this code reverses the 1165320918bSDave Airlie * provided value into the lfsr16 value by counting backwards to get 1175320918bSDave Airlie * the value that needs to be set in the hardware comparator to get the 1185320918bSDave Airlie * same actual count. This makes sense once you read above a couple of 1195320918bSDave Airlie * times and think about it from a hardware perspective. 1205320918bSDave Airlie */ 1215320918bSDave Airlie static u16 udl_lfsr16(u16 actual_count) 1225320918bSDave Airlie { 1235320918bSDave Airlie u32 lv = 0xFFFF; /* This is the lfsr value that the hw starts with */ 1245320918bSDave Airlie 1255320918bSDave Airlie while (actual_count--) { 1265320918bSDave Airlie lv = ((lv << 1) | 1275320918bSDave Airlie (((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1)) 1285320918bSDave Airlie & 0xFFFF; 1295320918bSDave Airlie } 1305320918bSDave Airlie 1315320918bSDave Airlie return (u16) lv; 1325320918bSDave Airlie } 1335320918bSDave Airlie 1345320918bSDave Airlie /* 1355320918bSDave Airlie * This does LFSR conversion on the value that is to be written. 1365320918bSDave Airlie * See LFSR explanation above for more detail. 1375320918bSDave Airlie */ 1385320918bSDave Airlie static char *udl_set_register_lfsr16(char *wrptr, u8 reg, u16 value) 1395320918bSDave Airlie { 1405320918bSDave Airlie return udl_set_register_16(wrptr, reg, udl_lfsr16(value)); 1415320918bSDave Airlie } 1425320918bSDave Airlie 1435320918bSDave Airlie /* 1449869e40dSThomas Zimmermann * Takes a DRM display mode and converts it into the DisplayLink 1459869e40dSThomas Zimmermann * equivalent register commands. 1465320918bSDave Airlie */ 1479869e40dSThomas Zimmermann static char *udl_set_display_mode(char *buf, struct drm_display_mode *mode) 1485320918bSDave Airlie { 1499869e40dSThomas Zimmermann u16 reg01 = mode->crtc_htotal - mode->crtc_hsync_start; 1509869e40dSThomas Zimmermann u16 reg03 = reg01 + mode->crtc_hdisplay; 1519869e40dSThomas Zimmermann u16 reg05 = mode->crtc_vtotal - mode->crtc_vsync_start; 1529869e40dSThomas Zimmermann u16 reg07 = reg05 + mode->crtc_vdisplay; 1539869e40dSThomas Zimmermann u16 reg09 = mode->crtc_htotal - 1; 1549869e40dSThomas Zimmermann u16 reg0b = 1; /* libdlo hardcodes hsync start to 1 */ 1559869e40dSThomas Zimmermann u16 reg0d = mode->crtc_hsync_end - mode->crtc_hsync_start + 1; 1569869e40dSThomas Zimmermann u16 reg0f = mode->hdisplay; 1579869e40dSThomas Zimmermann u16 reg11 = mode->crtc_vtotal; 1589869e40dSThomas Zimmermann u16 reg13 = 0; /* libdlo hardcodes vsync start to 0 */ 1599869e40dSThomas Zimmermann u16 reg15 = mode->crtc_vsync_end - mode->crtc_vsync_start; 1609869e40dSThomas Zimmermann u16 reg17 = mode->crtc_vdisplay; 1619869e40dSThomas Zimmermann u16 reg1b = mode->clock / 5; 1625320918bSDave Airlie 1639869e40dSThomas Zimmermann buf = udl_set_register_lfsr16(buf, UDL_REG_XDISPLAYSTART, reg01); 1649869e40dSThomas Zimmermann buf = udl_set_register_lfsr16(buf, UDL_REG_XDISPLAYEND, reg03); 1659869e40dSThomas Zimmermann buf = udl_set_register_lfsr16(buf, UDL_REG_YDISPLAYSTART, reg05); 1669869e40dSThomas Zimmermann buf = udl_set_register_lfsr16(buf, UDL_REG_YDISPLAYEND, reg07); 1679869e40dSThomas Zimmermann buf = udl_set_register_lfsr16(buf, UDL_REG_XENDCOUNT, reg09); 1689869e40dSThomas Zimmermann buf = udl_set_register_lfsr16(buf, UDL_REG_HSYNCSTART, reg0b); 1699869e40dSThomas Zimmermann buf = udl_set_register_lfsr16(buf, UDL_REG_HSYNCEND, reg0d); 1709869e40dSThomas Zimmermann buf = udl_set_register_16(buf, UDL_REG_HPIXELS, reg0f); 1719869e40dSThomas Zimmermann buf = udl_set_register_lfsr16(buf, UDL_REG_YENDCOUNT, reg11); 1729869e40dSThomas Zimmermann buf = udl_set_register_lfsr16(buf, UDL_REG_VSYNCSTART, reg13); 1739869e40dSThomas Zimmermann buf = udl_set_register_lfsr16(buf, UDL_REG_VSYNCEND, reg15); 1749869e40dSThomas Zimmermann buf = udl_set_register_16(buf, UDL_REG_VPIXELS, reg17); 1759869e40dSThomas Zimmermann buf = udl_set_register_16be(buf, UDL_REG_PIXELCLOCK5KHZ, reg1b); 1765320918bSDave Airlie 1779869e40dSThomas Zimmermann return buf; 1785320918bSDave Airlie } 1795320918bSDave Airlie 1805bd42f69SDave Airlie static char *udl_dummy_render(char *wrptr) 1815bd42f69SDave Airlie { 1825bd42f69SDave Airlie *wrptr++ = 0xAF; 1835bd42f69SDave Airlie *wrptr++ = 0x6A; /* copy */ 1845bd42f69SDave Airlie *wrptr++ = 0x00; /* from addr */ 1855bd42f69SDave Airlie *wrptr++ = 0x00; 1865bd42f69SDave Airlie *wrptr++ = 0x00; 1875bd42f69SDave Airlie *wrptr++ = 0x01; /* one pixel */ 1885bd42f69SDave Airlie *wrptr++ = 0x00; /* to address */ 1895bd42f69SDave Airlie *wrptr++ = 0x00; 1905bd42f69SDave Airlie *wrptr++ = 0x00; 1915bd42f69SDave Airlie return wrptr; 1925bd42f69SDave Airlie } 1935bd42f69SDave Airlie 194a8109f5bSThomas Zimmermann static long udl_log_cpp(unsigned int cpp) 195a8109f5bSThomas Zimmermann { 196a8109f5bSThomas Zimmermann if (WARN_ON(!is_power_of_2(cpp))) 197a8109f5bSThomas Zimmermann return -EINVAL; 198a8109f5bSThomas Zimmermann return __ffs(cpp); 199a8109f5bSThomas Zimmermann } 200a8109f5bSThomas Zimmermann 2017938f421SLucas De Marchi static int udl_handle_damage(struct drm_framebuffer *fb, 2027938f421SLucas De Marchi const struct iosys_map *map, 203b13fa27aSTakashi Iwai const struct drm_rect *clip) 204a8109f5bSThomas Zimmermann { 205a8109f5bSThomas Zimmermann struct drm_device *dev = fb->dev; 2065ceeb328SThomas Zimmermann void *vaddr = map->vaddr; /* TODO: Use mapping abstraction properly */ 207ce724470SThomas Zimmermann int i, ret; 208a8109f5bSThomas Zimmermann char *cmd; 209a8109f5bSThomas Zimmermann struct urb *urb; 210a8109f5bSThomas Zimmermann int log_bpp; 211a8109f5bSThomas Zimmermann 212a8109f5bSThomas Zimmermann ret = udl_log_cpp(fb->format->cpp[0]); 213a8109f5bSThomas Zimmermann if (ret < 0) 214a8109f5bSThomas Zimmermann return ret; 215a8109f5bSThomas Zimmermann log_bpp = ret; 216a8109f5bSThomas Zimmermann 217a8109f5bSThomas Zimmermann urb = udl_get_urb(dev); 218fcc21447SThomas Zimmermann if (!urb) 219fcc21447SThomas Zimmermann return -ENOMEM; 220a8109f5bSThomas Zimmermann cmd = urb->transfer_buffer; 221a8109f5bSThomas Zimmermann 222b13fa27aSTakashi Iwai for (i = clip->y1; i < clip->y2; i++) { 223a8109f5bSThomas Zimmermann const int line_offset = fb->pitches[0] * i; 224b13fa27aSTakashi Iwai const int byte_offset = line_offset + (clip->x1 << log_bpp); 225b13fa27aSTakashi Iwai const int dev_byte_offset = (fb->width * i + clip->x1) << log_bpp; 226b13fa27aSTakashi Iwai const int byte_width = drm_rect_width(clip) << log_bpp; 227a8109f5bSThomas Zimmermann ret = udl_render_hline(dev, log_bpp, &urb, (char *)vaddr, 228a8109f5bSThomas Zimmermann &cmd, byte_offset, dev_byte_offset, 229a8109f5bSThomas Zimmermann byte_width); 230a8109f5bSThomas Zimmermann if (ret) 231fcc21447SThomas Zimmermann return ret; 232a8109f5bSThomas Zimmermann } 233a8109f5bSThomas Zimmermann 234a8109f5bSThomas Zimmermann if (cmd > (char *)urb->transfer_buffer) { 235a8109f5bSThomas Zimmermann /* Send partial buffer remaining before exiting */ 236a8109f5bSThomas Zimmermann int len; 237a8109f5bSThomas Zimmermann if (cmd < (char *)urb->transfer_buffer + urb->transfer_buffer_length) 238a8109f5bSThomas Zimmermann *cmd++ = 0xAF; 239a8109f5bSThomas Zimmermann len = cmd - (char *)urb->transfer_buffer; 240a8109f5bSThomas Zimmermann ret = udl_submit_urb(dev, urb, len); 241a8109f5bSThomas Zimmermann } else { 242a8109f5bSThomas Zimmermann udl_urb_completion(urb); 243a8109f5bSThomas Zimmermann } 244a8109f5bSThomas Zimmermann 245fcc21447SThomas Zimmermann return 0; 246a8109f5bSThomas Zimmermann } 247a8109f5bSThomas Zimmermann 2489fda81e0SThomas Zimmermann /* 24972d73dd3SThomas Zimmermann * Primary plane 2509fda81e0SThomas Zimmermann */ 2519fda81e0SThomas Zimmermann 25272d73dd3SThomas Zimmermann static const uint32_t udl_primary_plane_formats[] = { 2539fda81e0SThomas Zimmermann DRM_FORMAT_RGB565, 2549fda81e0SThomas Zimmermann DRM_FORMAT_XRGB8888, 2559fda81e0SThomas Zimmermann }; 2569fda81e0SThomas Zimmermann 25772d73dd3SThomas Zimmermann static const uint64_t udl_primary_plane_fmtmods[] = { 25872d73dd3SThomas Zimmermann DRM_FORMAT_MOD_LINEAR, 25972d73dd3SThomas Zimmermann DRM_FORMAT_MOD_INVALID 26072d73dd3SThomas Zimmermann }; 26172d73dd3SThomas Zimmermann 26272d73dd3SThomas Zimmermann static void udl_primary_plane_helper_atomic_update(struct drm_plane *plane, 26372d73dd3SThomas Zimmermann struct drm_atomic_state *state) 2645320918bSDave Airlie { 265ca2bd373SThomas Zimmermann struct drm_device *dev = plane->dev; 26672d73dd3SThomas Zimmermann struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane); 2675ceeb328SThomas Zimmermann struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state); 26872d73dd3SThomas Zimmermann struct drm_framebuffer *fb = plane_state->fb; 26972d73dd3SThomas Zimmermann struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane); 270fcc21447SThomas Zimmermann struct drm_atomic_helper_damage_iter iter; 271fcc21447SThomas Zimmermann struct drm_rect damage; 272fcc21447SThomas Zimmermann int ret, idx; 27372d73dd3SThomas Zimmermann 27472d73dd3SThomas Zimmermann if (!fb) 27572d73dd3SThomas Zimmermann return; /* no framebuffer; plane is disabled */ 27672d73dd3SThomas Zimmermann 277fcc21447SThomas Zimmermann ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE); 278fcc21447SThomas Zimmermann if (ret) 279fcc21447SThomas Zimmermann return; 280fcc21447SThomas Zimmermann 281fcc21447SThomas Zimmermann if (!drm_dev_enter(dev, &idx)) 282fcc21447SThomas Zimmermann goto out_drm_gem_fb_end_cpu_access; 283fcc21447SThomas Zimmermann 284fcc21447SThomas Zimmermann drm_atomic_helper_damage_iter_init(&iter, old_plane_state, plane_state); 285fcc21447SThomas Zimmermann drm_atomic_for_each_plane_damage(&iter, &damage) { 286fcc21447SThomas Zimmermann udl_handle_damage(fb, &shadow_plane_state->data[0], &damage); 287fcc21447SThomas Zimmermann } 288ca2bd373SThomas Zimmermann 289ca2bd373SThomas Zimmermann drm_dev_exit(idx); 290fcc21447SThomas Zimmermann 291fcc21447SThomas Zimmermann out_drm_gem_fb_end_cpu_access: 292fcc21447SThomas Zimmermann drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE); 29372d73dd3SThomas Zimmermann } 29472d73dd3SThomas Zimmermann 29572d73dd3SThomas Zimmermann static const struct drm_plane_helper_funcs udl_primary_plane_helper_funcs = { 29672d73dd3SThomas Zimmermann DRM_GEM_SHADOW_PLANE_HELPER_FUNCS, 29772d73dd3SThomas Zimmermann .atomic_check = drm_plane_helper_atomic_check, 29872d73dd3SThomas Zimmermann .atomic_update = udl_primary_plane_helper_atomic_update, 29972d73dd3SThomas Zimmermann }; 30072d73dd3SThomas Zimmermann 30172d73dd3SThomas Zimmermann static const struct drm_plane_funcs udl_primary_plane_funcs = { 30272d73dd3SThomas Zimmermann .update_plane = drm_atomic_helper_update_plane, 30372d73dd3SThomas Zimmermann .disable_plane = drm_atomic_helper_disable_plane, 30472d73dd3SThomas Zimmermann .destroy = drm_plane_cleanup, 30572d73dd3SThomas Zimmermann DRM_GEM_SHADOW_PLANE_FUNCS, 30672d73dd3SThomas Zimmermann }; 30772d73dd3SThomas Zimmermann 30872d73dd3SThomas Zimmermann /* 30972d73dd3SThomas Zimmermann * CRTC 31072d73dd3SThomas Zimmermann */ 31172d73dd3SThomas Zimmermann 31272d73dd3SThomas Zimmermann static int udl_crtc_helper_atomic_check(struct drm_crtc *crtc, struct drm_atomic_state *state) 31372d73dd3SThomas Zimmermann { 31472d73dd3SThomas Zimmermann struct drm_crtc_state *new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc); 31572d73dd3SThomas Zimmermann 31672d73dd3SThomas Zimmermann return drm_atomic_helper_check_crtc_state(new_crtc_state, false); 31772d73dd3SThomas Zimmermann } 31872d73dd3SThomas Zimmermann 31972d73dd3SThomas Zimmermann static void udl_crtc_helper_atomic_enable(struct drm_crtc *crtc, struct drm_atomic_state *state) 32072d73dd3SThomas Zimmermann { 32172d73dd3SThomas Zimmermann struct drm_device *dev = crtc->dev; 32272d73dd3SThomas Zimmermann struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc); 32372d73dd3SThomas Zimmermann struct drm_display_mode *mode = &crtc_state->mode; 324890e4de8SThomas Zimmermann struct urb *urb; 3255320918bSDave Airlie char *buf; 326ca2bd373SThomas Zimmermann int idx; 327ca2bd373SThomas Zimmermann 328ca2bd373SThomas Zimmermann if (!drm_dev_enter(dev, &idx)) 329ca2bd373SThomas Zimmermann return; 3305320918bSDave Airlie 331890e4de8SThomas Zimmermann urb = udl_get_urb(dev); 332890e4de8SThomas Zimmermann if (!urb) 333ca2bd373SThomas Zimmermann goto out; 3345320918bSDave Airlie 335890e4de8SThomas Zimmermann buf = (char *)urb->transfer_buffer; 336890e4de8SThomas Zimmermann buf = udl_vidreg_lock(buf); 337ff76e82cSThomas Zimmermann buf = udl_set_color_depth(buf, UDL_COLORDEPTH_16BPP); 3385320918bSDave Airlie /* set base for 16bpp segment to 0 */ 339890e4de8SThomas Zimmermann buf = udl_set_base16bpp(buf, 0); 3405320918bSDave Airlie /* set base for 8bpp segment to end of fb */ 341890e4de8SThomas Zimmermann buf = udl_set_base8bpp(buf, 2 * mode->vdisplay * mode->hdisplay); 3429869e40dSThomas Zimmermann buf = udl_set_display_mode(buf, mode); 343ff76e82cSThomas Zimmermann buf = udl_set_blank_mode(buf, UDL_BLANKMODE_ON); 344890e4de8SThomas Zimmermann buf = udl_vidreg_unlock(buf); 345890e4de8SThomas Zimmermann buf = udl_dummy_render(buf); 3465bd42f69SDave Airlie 347890e4de8SThomas Zimmermann udl_submit_urb(dev, urb, buf - (char *)urb->transfer_buffer); 348ca2bd373SThomas Zimmermann 349ca2bd373SThomas Zimmermann out: 350ca2bd373SThomas Zimmermann drm_dev_exit(idx); 3519fda81e0SThomas Zimmermann } 3529fda81e0SThomas Zimmermann 35372d73dd3SThomas Zimmermann static void udl_crtc_helper_atomic_disable(struct drm_crtc *crtc, struct drm_atomic_state *state) 3549fda81e0SThomas Zimmermann { 355997d33c3SThomas Zimmermann struct drm_device *dev = crtc->dev; 356997d33c3SThomas Zimmermann struct urb *urb; 357997d33c3SThomas Zimmermann char *buf; 358ca2bd373SThomas Zimmermann int idx; 359ca2bd373SThomas Zimmermann 360ca2bd373SThomas Zimmermann if (!drm_dev_enter(dev, &idx)) 361ca2bd373SThomas Zimmermann return; 362997d33c3SThomas Zimmermann 363997d33c3SThomas Zimmermann urb = udl_get_urb(dev); 364997d33c3SThomas Zimmermann if (!urb) 365ca2bd373SThomas Zimmermann goto out; 366997d33c3SThomas Zimmermann 367997d33c3SThomas Zimmermann buf = (char *)urb->transfer_buffer; 368997d33c3SThomas Zimmermann buf = udl_vidreg_lock(buf); 369ff76e82cSThomas Zimmermann buf = udl_set_blank_mode(buf, UDL_BLANKMODE_POWERDOWN); 370997d33c3SThomas Zimmermann buf = udl_vidreg_unlock(buf); 371997d33c3SThomas Zimmermann buf = udl_dummy_render(buf); 372997d33c3SThomas Zimmermann 373997d33c3SThomas Zimmermann udl_submit_urb(dev, urb, buf - (char *)urb->transfer_buffer); 374ca2bd373SThomas Zimmermann 375ca2bd373SThomas Zimmermann out: 376ca2bd373SThomas Zimmermann drm_dev_exit(idx); 3779fda81e0SThomas Zimmermann } 3789fda81e0SThomas Zimmermann 37972d73dd3SThomas Zimmermann static const struct drm_crtc_helper_funcs udl_crtc_helper_funcs = { 38072d73dd3SThomas Zimmermann .atomic_check = udl_crtc_helper_atomic_check, 38172d73dd3SThomas Zimmermann .atomic_enable = udl_crtc_helper_atomic_enable, 38272d73dd3SThomas Zimmermann .atomic_disable = udl_crtc_helper_atomic_disable, 38372d73dd3SThomas Zimmermann }; 38440377ef2SStéphane Marchesin 38572d73dd3SThomas Zimmermann static const struct drm_crtc_funcs udl_crtc_funcs = { 38672d73dd3SThomas Zimmermann .reset = drm_atomic_helper_crtc_reset, 38772d73dd3SThomas Zimmermann .destroy = drm_crtc_cleanup, 38872d73dd3SThomas Zimmermann .set_config = drm_atomic_helper_set_config, 38972d73dd3SThomas Zimmermann .page_flip = drm_atomic_helper_page_flip, 39072d73dd3SThomas Zimmermann .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state, 39172d73dd3SThomas Zimmermann .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state, 39272d73dd3SThomas Zimmermann }; 3939fda81e0SThomas Zimmermann 39472d73dd3SThomas Zimmermann /* 39572d73dd3SThomas Zimmermann * Encoder 39672d73dd3SThomas Zimmermann */ 39740377ef2SStéphane Marchesin 39872d73dd3SThomas Zimmermann static const struct drm_encoder_funcs udl_encoder_funcs = { 39972d73dd3SThomas Zimmermann .destroy = drm_encoder_cleanup, 4005320918bSDave Airlie }; 4015320918bSDave Airlie 4029fda81e0SThomas Zimmermann /* 4030862cfd3SThomas Zimmermann * Connector 4040862cfd3SThomas Zimmermann */ 4050862cfd3SThomas Zimmermann 4060862cfd3SThomas Zimmermann static int udl_connector_helper_get_modes(struct drm_connector *connector) 4070862cfd3SThomas Zimmermann { 4080862cfd3SThomas Zimmermann struct udl_connector *udl_connector = to_udl_connector(connector); 4090862cfd3SThomas Zimmermann 4100862cfd3SThomas Zimmermann drm_connector_update_edid_property(connector, udl_connector->edid); 4110862cfd3SThomas Zimmermann if (udl_connector->edid) 4120862cfd3SThomas Zimmermann return drm_add_edid_modes(connector, udl_connector->edid); 4130862cfd3SThomas Zimmermann 4140862cfd3SThomas Zimmermann return 0; 4150862cfd3SThomas Zimmermann } 4160862cfd3SThomas Zimmermann 4170862cfd3SThomas Zimmermann static const struct drm_connector_helper_funcs udl_connector_helper_funcs = { 4180862cfd3SThomas Zimmermann .get_modes = udl_connector_helper_get_modes, 4190862cfd3SThomas Zimmermann }; 4200862cfd3SThomas Zimmermann 4210862cfd3SThomas Zimmermann static int udl_get_edid_block(void *data, u8 *buf, unsigned int block, size_t len) 4220862cfd3SThomas Zimmermann { 4230862cfd3SThomas Zimmermann struct udl_device *udl = data; 4240862cfd3SThomas Zimmermann struct drm_device *dev = &udl->drm; 4250862cfd3SThomas Zimmermann struct usb_device *udev = udl_to_usb_device(udl); 4260862cfd3SThomas Zimmermann u8 *read_buff; 4270862cfd3SThomas Zimmermann int ret; 4280862cfd3SThomas Zimmermann size_t i; 4290862cfd3SThomas Zimmermann 4300862cfd3SThomas Zimmermann read_buff = kmalloc(2, GFP_KERNEL); 4310862cfd3SThomas Zimmermann if (!read_buff) 4320862cfd3SThomas Zimmermann return -ENOMEM; 4330862cfd3SThomas Zimmermann 4340862cfd3SThomas Zimmermann for (i = 0; i < len; i++) { 4350862cfd3SThomas Zimmermann int bval = (i + block * EDID_LENGTH) << 8; 4360862cfd3SThomas Zimmermann 4370862cfd3SThomas Zimmermann ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 4380862cfd3SThomas Zimmermann 0x02, (0x80 | (0x02 << 5)), bval, 4390862cfd3SThomas Zimmermann 0xA1, read_buff, 2, USB_CTRL_GET_TIMEOUT); 4400862cfd3SThomas Zimmermann if (ret < 0) { 4410862cfd3SThomas Zimmermann drm_err(dev, "Read EDID byte %zu failed err %x\n", i, ret); 4420862cfd3SThomas Zimmermann goto err_kfree; 4430862cfd3SThomas Zimmermann } else if (ret < 1) { 4440862cfd3SThomas Zimmermann ret = -EIO; 4450862cfd3SThomas Zimmermann drm_err(dev, "Read EDID byte %zu failed\n", i); 4460862cfd3SThomas Zimmermann goto err_kfree; 4470862cfd3SThomas Zimmermann } 4480862cfd3SThomas Zimmermann 4490862cfd3SThomas Zimmermann buf[i] = read_buff[1]; 4500862cfd3SThomas Zimmermann } 4510862cfd3SThomas Zimmermann 4520862cfd3SThomas Zimmermann kfree(read_buff); 4530862cfd3SThomas Zimmermann 4540862cfd3SThomas Zimmermann return 0; 4550862cfd3SThomas Zimmermann 4560862cfd3SThomas Zimmermann err_kfree: 4570862cfd3SThomas Zimmermann kfree(read_buff); 4580862cfd3SThomas Zimmermann return ret; 4590862cfd3SThomas Zimmermann } 4600862cfd3SThomas Zimmermann 4610862cfd3SThomas Zimmermann static enum drm_connector_status udl_connector_detect(struct drm_connector *connector, bool force) 4620862cfd3SThomas Zimmermann { 463ca2bd373SThomas Zimmermann struct drm_device *dev = connector->dev; 464ca2bd373SThomas Zimmermann struct udl_device *udl = to_udl(dev); 4650862cfd3SThomas Zimmermann struct udl_connector *udl_connector = to_udl_connector(connector); 466ca2bd373SThomas Zimmermann enum drm_connector_status status = connector_status_disconnected; 467ca2bd373SThomas Zimmermann int idx; 4680862cfd3SThomas Zimmermann 4690862cfd3SThomas Zimmermann /* cleanup previous EDID */ 4700862cfd3SThomas Zimmermann kfree(udl_connector->edid); 471ca2bd373SThomas Zimmermann udl_connector->edid = NULL; 4720862cfd3SThomas Zimmermann 473ca2bd373SThomas Zimmermann if (!drm_dev_enter(dev, &idx)) 4740862cfd3SThomas Zimmermann return connector_status_disconnected; 4750862cfd3SThomas Zimmermann 476ca2bd373SThomas Zimmermann udl_connector->edid = drm_do_get_edid(connector, udl_get_edid_block, udl); 477ca2bd373SThomas Zimmermann if (udl_connector->edid) 478ca2bd373SThomas Zimmermann status = connector_status_connected; 479ca2bd373SThomas Zimmermann 480ca2bd373SThomas Zimmermann drm_dev_exit(idx); 481ca2bd373SThomas Zimmermann 482ca2bd373SThomas Zimmermann return status; 4830862cfd3SThomas Zimmermann } 4840862cfd3SThomas Zimmermann 4850862cfd3SThomas Zimmermann static void udl_connector_destroy(struct drm_connector *connector) 4860862cfd3SThomas Zimmermann { 4870862cfd3SThomas Zimmermann struct udl_connector *udl_connector = to_udl_connector(connector); 4880862cfd3SThomas Zimmermann 4890862cfd3SThomas Zimmermann drm_connector_cleanup(connector); 4900862cfd3SThomas Zimmermann kfree(udl_connector->edid); 4910862cfd3SThomas Zimmermann kfree(udl_connector); 4920862cfd3SThomas Zimmermann } 4930862cfd3SThomas Zimmermann 4940862cfd3SThomas Zimmermann static const struct drm_connector_funcs udl_connector_funcs = { 4950862cfd3SThomas Zimmermann .reset = drm_atomic_helper_connector_reset, 4960862cfd3SThomas Zimmermann .detect = udl_connector_detect, 4970862cfd3SThomas Zimmermann .fill_modes = drm_helper_probe_single_connector_modes, 4980862cfd3SThomas Zimmermann .destroy = udl_connector_destroy, 4990862cfd3SThomas Zimmermann .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, 5000862cfd3SThomas Zimmermann .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 5010862cfd3SThomas Zimmermann }; 5020862cfd3SThomas Zimmermann 5030862cfd3SThomas Zimmermann struct drm_connector *udl_connector_init(struct drm_device *dev) 5040862cfd3SThomas Zimmermann { 5050862cfd3SThomas Zimmermann struct udl_connector *udl_connector; 5060862cfd3SThomas Zimmermann struct drm_connector *connector; 5070862cfd3SThomas Zimmermann int ret; 5080862cfd3SThomas Zimmermann 5090862cfd3SThomas Zimmermann udl_connector = kzalloc(sizeof(*udl_connector), GFP_KERNEL); 5100862cfd3SThomas Zimmermann if (!udl_connector) 5110862cfd3SThomas Zimmermann return ERR_PTR(-ENOMEM); 5120862cfd3SThomas Zimmermann 5130862cfd3SThomas Zimmermann connector = &udl_connector->connector; 5140862cfd3SThomas Zimmermann ret = drm_connector_init(dev, connector, &udl_connector_funcs, DRM_MODE_CONNECTOR_VGA); 5150862cfd3SThomas Zimmermann if (ret) 5160862cfd3SThomas Zimmermann goto err_kfree; 5170862cfd3SThomas Zimmermann 5180862cfd3SThomas Zimmermann drm_connector_helper_add(connector, &udl_connector_helper_funcs); 5190862cfd3SThomas Zimmermann 5200862cfd3SThomas Zimmermann connector->polled = DRM_CONNECTOR_POLL_HPD | 5210862cfd3SThomas Zimmermann DRM_CONNECTOR_POLL_CONNECT | 5220862cfd3SThomas Zimmermann DRM_CONNECTOR_POLL_DISCONNECT; 5230862cfd3SThomas Zimmermann 5240862cfd3SThomas Zimmermann return connector; 5250862cfd3SThomas Zimmermann 5260862cfd3SThomas Zimmermann err_kfree: 5270862cfd3SThomas Zimmermann kfree(udl_connector); 5280862cfd3SThomas Zimmermann return ERR_PTR(ret); 5290862cfd3SThomas Zimmermann } 5300862cfd3SThomas Zimmermann 5310862cfd3SThomas Zimmermann /* 5329fda81e0SThomas Zimmermann * Modesetting 5339fda81e0SThomas Zimmermann */ 5345320918bSDave Airlie 535c020f660SThomas Zimmermann static enum drm_mode_status udl_mode_config_mode_valid(struct drm_device *dev, 536c020f660SThomas Zimmermann const struct drm_display_mode *mode) 537c020f660SThomas Zimmermann { 538c020f660SThomas Zimmermann struct udl_device *udl = to_udl(dev); 539c020f660SThomas Zimmermann 540c020f660SThomas Zimmermann if (udl->sku_pixel_limit) { 541c020f660SThomas Zimmermann if (mode->vdisplay * mode->hdisplay > udl->sku_pixel_limit) 542c020f660SThomas Zimmermann return MODE_MEM; 543c020f660SThomas Zimmermann } 544c020f660SThomas Zimmermann 545c020f660SThomas Zimmermann return MODE_OK; 546c020f660SThomas Zimmermann } 547c020f660SThomas Zimmermann 54872d73dd3SThomas Zimmermann static const struct drm_mode_config_funcs udl_mode_config_funcs = { 549230b8b04SThomas Zimmermann .fb_create = drm_gem_fb_create_with_dirty, 550c020f660SThomas Zimmermann .mode_valid = udl_mode_config_mode_valid, 5519fda81e0SThomas Zimmermann .atomic_check = drm_atomic_helper_check, 5529fda81e0SThomas Zimmermann .atomic_commit = drm_atomic_helper_commit, 5535320918bSDave Airlie }; 5545320918bSDave Airlie 5555320918bSDave Airlie int udl_modeset_init(struct drm_device *dev) 5565320918bSDave Airlie { 5576ae355a2SDaniel Vetter struct udl_device *udl = to_udl(dev); 55872d73dd3SThomas Zimmermann struct drm_plane *primary_plane; 55972d73dd3SThomas Zimmermann struct drm_crtc *crtc; 56072d73dd3SThomas Zimmermann struct drm_encoder *encoder; 561e829cf0bSThomas Zimmermann struct drm_connector *connector; 562e829cf0bSThomas Zimmermann int ret; 563e829cf0bSThomas Zimmermann 564fe5b7c86SDaniel Vetter ret = drmm_mode_config_init(dev); 565fe5b7c86SDaniel Vetter if (ret) 566fe5b7c86SDaniel Vetter return ret; 5675320918bSDave Airlie 5685320918bSDave Airlie dev->mode_config.min_width = 640; 5695320918bSDave Airlie dev->mode_config.min_height = 480; 5705320918bSDave Airlie dev->mode_config.max_width = 2048; 5715320918bSDave Airlie dev->mode_config.max_height = 2048; 572d8177841SThomas Zimmermann dev->mode_config.preferred_depth = 16; 57372d73dd3SThomas Zimmermann dev->mode_config.funcs = &udl_mode_config_funcs; 5745320918bSDave Airlie 57572d73dd3SThomas Zimmermann primary_plane = &udl->primary_plane; 57672d73dd3SThomas Zimmermann ret = drm_universal_plane_init(dev, primary_plane, 0, 57772d73dd3SThomas Zimmermann &udl_primary_plane_funcs, 57872d73dd3SThomas Zimmermann udl_primary_plane_formats, 57972d73dd3SThomas Zimmermann ARRAY_SIZE(udl_primary_plane_formats), 58072d73dd3SThomas Zimmermann udl_primary_plane_fmtmods, 58172d73dd3SThomas Zimmermann DRM_PLANE_TYPE_PRIMARY, NULL); 58272d73dd3SThomas Zimmermann if (ret) 58372d73dd3SThomas Zimmermann return ret; 58472d73dd3SThomas Zimmermann drm_plane_helper_add(primary_plane, &udl_primary_plane_helper_funcs); 58572d73dd3SThomas Zimmermann drm_plane_enable_fb_damage_clips(primary_plane); 58672d73dd3SThomas Zimmermann 58772d73dd3SThomas Zimmermann crtc = &udl->crtc; 58872d73dd3SThomas Zimmermann ret = drm_crtc_init_with_planes(dev, crtc, primary_plane, NULL, 58972d73dd3SThomas Zimmermann &udl_crtc_funcs, NULL); 59072d73dd3SThomas Zimmermann if (ret) 59172d73dd3SThomas Zimmermann return ret; 59272d73dd3SThomas Zimmermann drm_crtc_helper_add(crtc, &udl_crtc_helper_funcs); 59372d73dd3SThomas Zimmermann 59472d73dd3SThomas Zimmermann encoder = &udl->encoder; 59572d73dd3SThomas Zimmermann ret = drm_encoder_init(dev, encoder, &udl_encoder_funcs, DRM_MODE_ENCODER_DAC, NULL); 59672d73dd3SThomas Zimmermann if (ret) 59772d73dd3SThomas Zimmermann return ret; 59872d73dd3SThomas Zimmermann encoder->possible_crtcs = drm_crtc_mask(crtc); 5995320918bSDave Airlie 600e829cf0bSThomas Zimmermann connector = udl_connector_init(dev); 601fe5b7c86SDaniel Vetter if (IS_ERR(connector)) 602fe5b7c86SDaniel Vetter return PTR_ERR(connector); 60372d73dd3SThomas Zimmermann ret = drm_connector_attach_encoder(connector, encoder); 6049fda81e0SThomas Zimmermann if (ret) 605fe5b7c86SDaniel Vetter return ret; 6069fda81e0SThomas Zimmermann 6079fda81e0SThomas Zimmermann drm_mode_config_reset(dev); 6085320918bSDave Airlie 6095320918bSDave Airlie return 0; 6105320918bSDave Airlie } 611