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 1144f29ad9SThomas Zimmermann #include <linux/bitfield.h> 1244f29ad9SThomas 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 /* 32*1b8db07fSThomas Zimmermann * All DisplayLink bulk operations start with 0xaf (UDL_MSG_BULK), followed by 33*1b8db07fSThomas Zimmermann * a specific command code. All operations are written to a command buffer, which 34*1b8db07fSThomas Zimmermann * the driver sends to the device. 355320918bSDave Airlie */ 365320918bSDave Airlie static char *udl_set_register(char *buf, u8 reg, u8 val) 375320918bSDave Airlie { 38*1b8db07fSThomas Zimmermann *buf++ = UDL_MSG_BULK; 39*1b8db07fSThomas Zimmermann *buf++ = UDL_CMD_WRITEREG; 405320918bSDave Airlie *buf++ = reg; 415320918bSDave Airlie *buf++ = val; 42*1b8db07fSThomas Zimmermann 435320918bSDave Airlie return buf; 445320918bSDave Airlie } 455320918bSDave Airlie 465320918bSDave Airlie static char *udl_vidreg_lock(char *buf) 475320918bSDave Airlie { 48cb7b995dSThomas Zimmermann return udl_set_register(buf, UDL_REG_VIDREG, UDL_VIDREG_LOCK); 495320918bSDave Airlie } 505320918bSDave Airlie 515320918bSDave Airlie static char *udl_vidreg_unlock(char *buf) 525320918bSDave Airlie { 53cb7b995dSThomas Zimmermann return udl_set_register(buf, UDL_REG_VIDREG, UDL_VIDREG_UNLOCK); 545320918bSDave Airlie } 555320918bSDave Airlie 56997d33c3SThomas Zimmermann static char *udl_set_blank_mode(char *buf, u8 mode) 575320918bSDave Airlie { 58ff76e82cSThomas Zimmermann return udl_set_register(buf, UDL_REG_BLANKMODE, mode); 595320918bSDave Airlie } 605320918bSDave Airlie 615320918bSDave Airlie static char *udl_set_color_depth(char *buf, u8 selection) 625320918bSDave Airlie { 63ed24ed48SThomas Zimmermann return udl_set_register(buf, UDL_REG_COLORDEPTH, selection); 645320918bSDave Airlie } 655320918bSDave Airlie 6644f29ad9SThomas Zimmermann static char *udl_set_base16bpp(char *buf, u32 base) 675320918bSDave Airlie { 6844f29ad9SThomas Zimmermann /* the base pointer is 24 bits wide, 0x20 is hi byte. */ 6944f29ad9SThomas Zimmermann u8 reg20 = FIELD_GET(UDL_BASE_ADDR2_MASK, base); 7044f29ad9SThomas Zimmermann u8 reg21 = FIELD_GET(UDL_BASE_ADDR1_MASK, base); 7144f29ad9SThomas Zimmermann u8 reg22 = FIELD_GET(UDL_BASE_ADDR0_MASK, base); 7244f29ad9SThomas Zimmermann 7344f29ad9SThomas Zimmermann buf = udl_set_register(buf, UDL_REG_BASE16BPP_ADDR2, reg20); 7444f29ad9SThomas Zimmermann buf = udl_set_register(buf, UDL_REG_BASE16BPP_ADDR1, reg21); 7544f29ad9SThomas Zimmermann buf = udl_set_register(buf, UDL_REG_BASE16BPP_ADDR0, reg22); 7644f29ad9SThomas Zimmermann 7744f29ad9SThomas Zimmermann return buf; 785320918bSDave Airlie } 795320918bSDave Airlie 805320918bSDave Airlie /* 815320918bSDave Airlie * DisplayLink HW has separate 16bpp and 8bpp framebuffers. 825320918bSDave Airlie * In 24bpp modes, the low 323 RGB bits go in the 8bpp framebuffer 835320918bSDave Airlie */ 8444f29ad9SThomas Zimmermann static char *udl_set_base8bpp(char *buf, u32 base) 855320918bSDave Airlie { 8644f29ad9SThomas Zimmermann /* the base pointer is 24 bits wide, 0x26 is hi byte. */ 8744f29ad9SThomas Zimmermann u8 reg26 = FIELD_GET(UDL_BASE_ADDR2_MASK, base); 8844f29ad9SThomas Zimmermann u8 reg27 = FIELD_GET(UDL_BASE_ADDR1_MASK, base); 8944f29ad9SThomas Zimmermann u8 reg28 = FIELD_GET(UDL_BASE_ADDR0_MASK, base); 9044f29ad9SThomas Zimmermann 9144f29ad9SThomas Zimmermann buf = udl_set_register(buf, UDL_REG_BASE8BPP_ADDR2, reg26); 9244f29ad9SThomas Zimmermann buf = udl_set_register(buf, UDL_REG_BASE8BPP_ADDR1, reg27); 9344f29ad9SThomas Zimmermann buf = udl_set_register(buf, UDL_REG_BASE8BPP_ADDR0, reg28); 9444f29ad9SThomas Zimmermann 9544f29ad9SThomas Zimmermann return buf; 965320918bSDave Airlie } 975320918bSDave Airlie 985320918bSDave Airlie static char *udl_set_register_16(char *wrptr, u8 reg, u16 value) 995320918bSDave Airlie { 1005320918bSDave Airlie wrptr = udl_set_register(wrptr, reg, value >> 8); 1015320918bSDave Airlie return udl_set_register(wrptr, reg+1, value); 1025320918bSDave Airlie } 1035320918bSDave Airlie 1045320918bSDave Airlie /* 1055320918bSDave Airlie * This is kind of weird because the controller takes some 1065320918bSDave Airlie * register values in a different byte order than other registers. 1075320918bSDave Airlie */ 1085320918bSDave Airlie static char *udl_set_register_16be(char *wrptr, u8 reg, u16 value) 1095320918bSDave Airlie { 1105320918bSDave Airlie wrptr = udl_set_register(wrptr, reg, value); 1115320918bSDave Airlie return udl_set_register(wrptr, reg+1, value >> 8); 1125320918bSDave Airlie } 1135320918bSDave Airlie 1145320918bSDave Airlie /* 1155320918bSDave Airlie * LFSR is linear feedback shift register. The reason we have this is 1165320918bSDave Airlie * because the display controller needs to minimize the clock depth of 1175320918bSDave Airlie * various counters used in the display path. So this code reverses the 1185320918bSDave Airlie * provided value into the lfsr16 value by counting backwards to get 1195320918bSDave Airlie * the value that needs to be set in the hardware comparator to get the 1205320918bSDave Airlie * same actual count. This makes sense once you read above a couple of 1215320918bSDave Airlie * times and think about it from a hardware perspective. 1225320918bSDave Airlie */ 1235320918bSDave Airlie static u16 udl_lfsr16(u16 actual_count) 1245320918bSDave Airlie { 1255320918bSDave Airlie u32 lv = 0xFFFF; /* This is the lfsr value that the hw starts with */ 1265320918bSDave Airlie 1275320918bSDave Airlie while (actual_count--) { 1285320918bSDave Airlie lv = ((lv << 1) | 1295320918bSDave Airlie (((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1)) 1305320918bSDave Airlie & 0xFFFF; 1315320918bSDave Airlie } 1325320918bSDave Airlie 1335320918bSDave Airlie return (u16) lv; 1345320918bSDave Airlie } 1355320918bSDave Airlie 1365320918bSDave Airlie /* 1375320918bSDave Airlie * This does LFSR conversion on the value that is to be written. 1385320918bSDave Airlie * See LFSR explanation above for more detail. 1395320918bSDave Airlie */ 1405320918bSDave Airlie static char *udl_set_register_lfsr16(char *wrptr, u8 reg, u16 value) 1415320918bSDave Airlie { 1425320918bSDave Airlie return udl_set_register_16(wrptr, reg, udl_lfsr16(value)); 1435320918bSDave Airlie } 1445320918bSDave Airlie 1455320918bSDave Airlie /* 1469869e40dSThomas Zimmermann * Takes a DRM display mode and converts it into the DisplayLink 1479869e40dSThomas Zimmermann * equivalent register commands. 1485320918bSDave Airlie */ 1499869e40dSThomas Zimmermann static char *udl_set_display_mode(char *buf, struct drm_display_mode *mode) 1505320918bSDave Airlie { 1519869e40dSThomas Zimmermann u16 reg01 = mode->crtc_htotal - mode->crtc_hsync_start; 1529869e40dSThomas Zimmermann u16 reg03 = reg01 + mode->crtc_hdisplay; 1539869e40dSThomas Zimmermann u16 reg05 = mode->crtc_vtotal - mode->crtc_vsync_start; 1549869e40dSThomas Zimmermann u16 reg07 = reg05 + mode->crtc_vdisplay; 1559869e40dSThomas Zimmermann u16 reg09 = mode->crtc_htotal - 1; 1569869e40dSThomas Zimmermann u16 reg0b = 1; /* libdlo hardcodes hsync start to 1 */ 1579869e40dSThomas Zimmermann u16 reg0d = mode->crtc_hsync_end - mode->crtc_hsync_start + 1; 1589869e40dSThomas Zimmermann u16 reg0f = mode->hdisplay; 1599869e40dSThomas Zimmermann u16 reg11 = mode->crtc_vtotal; 1609869e40dSThomas Zimmermann u16 reg13 = 0; /* libdlo hardcodes vsync start to 0 */ 1619869e40dSThomas Zimmermann u16 reg15 = mode->crtc_vsync_end - mode->crtc_vsync_start; 1629869e40dSThomas Zimmermann u16 reg17 = mode->crtc_vdisplay; 1639869e40dSThomas Zimmermann u16 reg1b = mode->clock / 5; 1645320918bSDave Airlie 1659869e40dSThomas Zimmermann buf = udl_set_register_lfsr16(buf, UDL_REG_XDISPLAYSTART, reg01); 1669869e40dSThomas Zimmermann buf = udl_set_register_lfsr16(buf, UDL_REG_XDISPLAYEND, reg03); 1679869e40dSThomas Zimmermann buf = udl_set_register_lfsr16(buf, UDL_REG_YDISPLAYSTART, reg05); 1689869e40dSThomas Zimmermann buf = udl_set_register_lfsr16(buf, UDL_REG_YDISPLAYEND, reg07); 1699869e40dSThomas Zimmermann buf = udl_set_register_lfsr16(buf, UDL_REG_XENDCOUNT, reg09); 1709869e40dSThomas Zimmermann buf = udl_set_register_lfsr16(buf, UDL_REG_HSYNCSTART, reg0b); 1719869e40dSThomas Zimmermann buf = udl_set_register_lfsr16(buf, UDL_REG_HSYNCEND, reg0d); 1729869e40dSThomas Zimmermann buf = udl_set_register_16(buf, UDL_REG_HPIXELS, reg0f); 1739869e40dSThomas Zimmermann buf = udl_set_register_lfsr16(buf, UDL_REG_YENDCOUNT, reg11); 1749869e40dSThomas Zimmermann buf = udl_set_register_lfsr16(buf, UDL_REG_VSYNCSTART, reg13); 1759869e40dSThomas Zimmermann buf = udl_set_register_lfsr16(buf, UDL_REG_VSYNCEND, reg15); 1769869e40dSThomas Zimmermann buf = udl_set_register_16(buf, UDL_REG_VPIXELS, reg17); 1779869e40dSThomas Zimmermann buf = udl_set_register_16be(buf, UDL_REG_PIXELCLOCK5KHZ, reg1b); 1785320918bSDave Airlie 1799869e40dSThomas Zimmermann return buf; 1805320918bSDave Airlie } 1815320918bSDave Airlie 1825bd42f69SDave Airlie static char *udl_dummy_render(char *wrptr) 1835bd42f69SDave Airlie { 184*1b8db07fSThomas Zimmermann *wrptr++ = UDL_MSG_BULK; 185*1b8db07fSThomas Zimmermann *wrptr++ = UDL_CMD_WRITECOPY16; 1865bd42f69SDave Airlie *wrptr++ = 0x00; /* from addr */ 1875bd42f69SDave Airlie *wrptr++ = 0x00; 1885bd42f69SDave Airlie *wrptr++ = 0x00; 1895bd42f69SDave Airlie *wrptr++ = 0x01; /* one pixel */ 1905bd42f69SDave Airlie *wrptr++ = 0x00; /* to address */ 1915bd42f69SDave Airlie *wrptr++ = 0x00; 1925bd42f69SDave Airlie *wrptr++ = 0x00; 1935bd42f69SDave Airlie return wrptr; 1945bd42f69SDave Airlie } 1955bd42f69SDave Airlie 196a8109f5bSThomas Zimmermann static long udl_log_cpp(unsigned int cpp) 197a8109f5bSThomas Zimmermann { 198a8109f5bSThomas Zimmermann if (WARN_ON(!is_power_of_2(cpp))) 199a8109f5bSThomas Zimmermann return -EINVAL; 200a8109f5bSThomas Zimmermann return __ffs(cpp); 201a8109f5bSThomas Zimmermann } 202a8109f5bSThomas Zimmermann 2037938f421SLucas De Marchi static int udl_handle_damage(struct drm_framebuffer *fb, 2047938f421SLucas De Marchi const struct iosys_map *map, 205b13fa27aSTakashi Iwai const struct drm_rect *clip) 206a8109f5bSThomas Zimmermann { 207a8109f5bSThomas Zimmermann struct drm_device *dev = fb->dev; 2085ceeb328SThomas Zimmermann void *vaddr = map->vaddr; /* TODO: Use mapping abstraction properly */ 209ce724470SThomas Zimmermann int i, ret; 210a8109f5bSThomas Zimmermann char *cmd; 211a8109f5bSThomas Zimmermann struct urb *urb; 212a8109f5bSThomas Zimmermann int log_bpp; 213a8109f5bSThomas Zimmermann 214a8109f5bSThomas Zimmermann ret = udl_log_cpp(fb->format->cpp[0]); 215a8109f5bSThomas Zimmermann if (ret < 0) 216a8109f5bSThomas Zimmermann return ret; 217a8109f5bSThomas Zimmermann log_bpp = ret; 218a8109f5bSThomas Zimmermann 219a8109f5bSThomas Zimmermann urb = udl_get_urb(dev); 220fcc21447SThomas Zimmermann if (!urb) 221fcc21447SThomas Zimmermann return -ENOMEM; 222a8109f5bSThomas Zimmermann cmd = urb->transfer_buffer; 223a8109f5bSThomas Zimmermann 224b13fa27aSTakashi Iwai for (i = clip->y1; i < clip->y2; i++) { 225a8109f5bSThomas Zimmermann const int line_offset = fb->pitches[0] * i; 226b13fa27aSTakashi Iwai const int byte_offset = line_offset + (clip->x1 << log_bpp); 227b13fa27aSTakashi Iwai const int dev_byte_offset = (fb->width * i + clip->x1) << log_bpp; 228b13fa27aSTakashi Iwai const int byte_width = drm_rect_width(clip) << log_bpp; 229a8109f5bSThomas Zimmermann ret = udl_render_hline(dev, log_bpp, &urb, (char *)vaddr, 230a8109f5bSThomas Zimmermann &cmd, byte_offset, dev_byte_offset, 231a8109f5bSThomas Zimmermann byte_width); 232a8109f5bSThomas Zimmermann if (ret) 233fcc21447SThomas Zimmermann return ret; 234a8109f5bSThomas Zimmermann } 235a8109f5bSThomas Zimmermann 236a8109f5bSThomas Zimmermann if (cmd > (char *)urb->transfer_buffer) { 237a8109f5bSThomas Zimmermann /* Send partial buffer remaining before exiting */ 238a8109f5bSThomas Zimmermann int len; 239a8109f5bSThomas Zimmermann if (cmd < (char *)urb->transfer_buffer + urb->transfer_buffer_length) 240*1b8db07fSThomas Zimmermann *cmd++ = UDL_MSG_BULK; 241a8109f5bSThomas Zimmermann len = cmd - (char *)urb->transfer_buffer; 242a8109f5bSThomas Zimmermann ret = udl_submit_urb(dev, urb, len); 243a8109f5bSThomas Zimmermann } else { 244a8109f5bSThomas Zimmermann udl_urb_completion(urb); 245a8109f5bSThomas Zimmermann } 246a8109f5bSThomas Zimmermann 247fcc21447SThomas Zimmermann return 0; 248a8109f5bSThomas Zimmermann } 249a8109f5bSThomas Zimmermann 2509fda81e0SThomas Zimmermann /* 25172d73dd3SThomas Zimmermann * Primary plane 2529fda81e0SThomas Zimmermann */ 2539fda81e0SThomas Zimmermann 25472d73dd3SThomas Zimmermann static const uint32_t udl_primary_plane_formats[] = { 2559fda81e0SThomas Zimmermann DRM_FORMAT_RGB565, 2569fda81e0SThomas Zimmermann DRM_FORMAT_XRGB8888, 2579fda81e0SThomas Zimmermann }; 2589fda81e0SThomas Zimmermann 25972d73dd3SThomas Zimmermann static const uint64_t udl_primary_plane_fmtmods[] = { 26072d73dd3SThomas Zimmermann DRM_FORMAT_MOD_LINEAR, 26172d73dd3SThomas Zimmermann DRM_FORMAT_MOD_INVALID 26272d73dd3SThomas Zimmermann }; 26372d73dd3SThomas Zimmermann 26472d73dd3SThomas Zimmermann static void udl_primary_plane_helper_atomic_update(struct drm_plane *plane, 26572d73dd3SThomas Zimmermann struct drm_atomic_state *state) 2665320918bSDave Airlie { 267ca2bd373SThomas Zimmermann struct drm_device *dev = plane->dev; 26872d73dd3SThomas Zimmermann struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane); 2695ceeb328SThomas Zimmermann struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state); 27072d73dd3SThomas Zimmermann struct drm_framebuffer *fb = plane_state->fb; 27172d73dd3SThomas Zimmermann struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane); 272fcc21447SThomas Zimmermann struct drm_atomic_helper_damage_iter iter; 273fcc21447SThomas Zimmermann struct drm_rect damage; 274fcc21447SThomas Zimmermann int ret, idx; 27572d73dd3SThomas Zimmermann 27672d73dd3SThomas Zimmermann if (!fb) 27772d73dd3SThomas Zimmermann return; /* no framebuffer; plane is disabled */ 27872d73dd3SThomas Zimmermann 279fcc21447SThomas Zimmermann ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE); 280fcc21447SThomas Zimmermann if (ret) 281fcc21447SThomas Zimmermann return; 282fcc21447SThomas Zimmermann 283fcc21447SThomas Zimmermann if (!drm_dev_enter(dev, &idx)) 284fcc21447SThomas Zimmermann goto out_drm_gem_fb_end_cpu_access; 285fcc21447SThomas Zimmermann 286fcc21447SThomas Zimmermann drm_atomic_helper_damage_iter_init(&iter, old_plane_state, plane_state); 287fcc21447SThomas Zimmermann drm_atomic_for_each_plane_damage(&iter, &damage) { 288fcc21447SThomas Zimmermann udl_handle_damage(fb, &shadow_plane_state->data[0], &damage); 289fcc21447SThomas Zimmermann } 290ca2bd373SThomas Zimmermann 291ca2bd373SThomas Zimmermann drm_dev_exit(idx); 292fcc21447SThomas Zimmermann 293fcc21447SThomas Zimmermann out_drm_gem_fb_end_cpu_access: 294fcc21447SThomas Zimmermann drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE); 29572d73dd3SThomas Zimmermann } 29672d73dd3SThomas Zimmermann 29772d73dd3SThomas Zimmermann static const struct drm_plane_helper_funcs udl_primary_plane_helper_funcs = { 29872d73dd3SThomas Zimmermann DRM_GEM_SHADOW_PLANE_HELPER_FUNCS, 29972d73dd3SThomas Zimmermann .atomic_check = drm_plane_helper_atomic_check, 30072d73dd3SThomas Zimmermann .atomic_update = udl_primary_plane_helper_atomic_update, 30172d73dd3SThomas Zimmermann }; 30272d73dd3SThomas Zimmermann 30372d73dd3SThomas Zimmermann static const struct drm_plane_funcs udl_primary_plane_funcs = { 30472d73dd3SThomas Zimmermann .update_plane = drm_atomic_helper_update_plane, 30572d73dd3SThomas Zimmermann .disable_plane = drm_atomic_helper_disable_plane, 30672d73dd3SThomas Zimmermann .destroy = drm_plane_cleanup, 30772d73dd3SThomas Zimmermann DRM_GEM_SHADOW_PLANE_FUNCS, 30872d73dd3SThomas Zimmermann }; 30972d73dd3SThomas Zimmermann 31072d73dd3SThomas Zimmermann /* 31172d73dd3SThomas Zimmermann * CRTC 31272d73dd3SThomas Zimmermann */ 31372d73dd3SThomas Zimmermann 31472d73dd3SThomas Zimmermann static int udl_crtc_helper_atomic_check(struct drm_crtc *crtc, struct drm_atomic_state *state) 31572d73dd3SThomas Zimmermann { 31672d73dd3SThomas Zimmermann struct drm_crtc_state *new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc); 31772d73dd3SThomas Zimmermann 31872d73dd3SThomas Zimmermann return drm_atomic_helper_check_crtc_state(new_crtc_state, false); 31972d73dd3SThomas Zimmermann } 32072d73dd3SThomas Zimmermann 32172d73dd3SThomas Zimmermann static void udl_crtc_helper_atomic_enable(struct drm_crtc *crtc, struct drm_atomic_state *state) 32272d73dd3SThomas Zimmermann { 32372d73dd3SThomas Zimmermann struct drm_device *dev = crtc->dev; 32472d73dd3SThomas Zimmermann struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc); 32572d73dd3SThomas Zimmermann struct drm_display_mode *mode = &crtc_state->mode; 326890e4de8SThomas Zimmermann struct urb *urb; 3275320918bSDave Airlie char *buf; 328ca2bd373SThomas Zimmermann int idx; 329ca2bd373SThomas Zimmermann 330ca2bd373SThomas Zimmermann if (!drm_dev_enter(dev, &idx)) 331ca2bd373SThomas Zimmermann return; 3325320918bSDave Airlie 333890e4de8SThomas Zimmermann urb = udl_get_urb(dev); 334890e4de8SThomas Zimmermann if (!urb) 335ca2bd373SThomas Zimmermann goto out; 3365320918bSDave Airlie 337890e4de8SThomas Zimmermann buf = (char *)urb->transfer_buffer; 338890e4de8SThomas Zimmermann buf = udl_vidreg_lock(buf); 339ff76e82cSThomas Zimmermann buf = udl_set_color_depth(buf, UDL_COLORDEPTH_16BPP); 3405320918bSDave Airlie /* set base for 16bpp segment to 0 */ 341890e4de8SThomas Zimmermann buf = udl_set_base16bpp(buf, 0); 3425320918bSDave Airlie /* set base for 8bpp segment to end of fb */ 343890e4de8SThomas Zimmermann buf = udl_set_base8bpp(buf, 2 * mode->vdisplay * mode->hdisplay); 3449869e40dSThomas Zimmermann buf = udl_set_display_mode(buf, mode); 345ff76e82cSThomas Zimmermann buf = udl_set_blank_mode(buf, UDL_BLANKMODE_ON); 346890e4de8SThomas Zimmermann buf = udl_vidreg_unlock(buf); 347890e4de8SThomas Zimmermann buf = udl_dummy_render(buf); 3485bd42f69SDave Airlie 349890e4de8SThomas Zimmermann udl_submit_urb(dev, urb, buf - (char *)urb->transfer_buffer); 350ca2bd373SThomas Zimmermann 351ca2bd373SThomas Zimmermann out: 352ca2bd373SThomas Zimmermann drm_dev_exit(idx); 3539fda81e0SThomas Zimmermann } 3549fda81e0SThomas Zimmermann 35572d73dd3SThomas Zimmermann static void udl_crtc_helper_atomic_disable(struct drm_crtc *crtc, struct drm_atomic_state *state) 3569fda81e0SThomas Zimmermann { 357997d33c3SThomas Zimmermann struct drm_device *dev = crtc->dev; 358997d33c3SThomas Zimmermann struct urb *urb; 359997d33c3SThomas Zimmermann char *buf; 360ca2bd373SThomas Zimmermann int idx; 361ca2bd373SThomas Zimmermann 362ca2bd373SThomas Zimmermann if (!drm_dev_enter(dev, &idx)) 363ca2bd373SThomas Zimmermann return; 364997d33c3SThomas Zimmermann 365997d33c3SThomas Zimmermann urb = udl_get_urb(dev); 366997d33c3SThomas Zimmermann if (!urb) 367ca2bd373SThomas Zimmermann goto out; 368997d33c3SThomas Zimmermann 369997d33c3SThomas Zimmermann buf = (char *)urb->transfer_buffer; 370997d33c3SThomas Zimmermann buf = udl_vidreg_lock(buf); 371ff76e82cSThomas Zimmermann buf = udl_set_blank_mode(buf, UDL_BLANKMODE_POWERDOWN); 372997d33c3SThomas Zimmermann buf = udl_vidreg_unlock(buf); 373997d33c3SThomas Zimmermann buf = udl_dummy_render(buf); 374997d33c3SThomas Zimmermann 375997d33c3SThomas Zimmermann udl_submit_urb(dev, urb, buf - (char *)urb->transfer_buffer); 376ca2bd373SThomas Zimmermann 377ca2bd373SThomas Zimmermann out: 378ca2bd373SThomas Zimmermann drm_dev_exit(idx); 3799fda81e0SThomas Zimmermann } 3809fda81e0SThomas Zimmermann 38172d73dd3SThomas Zimmermann static const struct drm_crtc_helper_funcs udl_crtc_helper_funcs = { 38272d73dd3SThomas Zimmermann .atomic_check = udl_crtc_helper_atomic_check, 38372d73dd3SThomas Zimmermann .atomic_enable = udl_crtc_helper_atomic_enable, 38472d73dd3SThomas Zimmermann .atomic_disable = udl_crtc_helper_atomic_disable, 38572d73dd3SThomas Zimmermann }; 38640377ef2SStéphane Marchesin 38772d73dd3SThomas Zimmermann static const struct drm_crtc_funcs udl_crtc_funcs = { 38872d73dd3SThomas Zimmermann .reset = drm_atomic_helper_crtc_reset, 38972d73dd3SThomas Zimmermann .destroy = drm_crtc_cleanup, 39072d73dd3SThomas Zimmermann .set_config = drm_atomic_helper_set_config, 39172d73dd3SThomas Zimmermann .page_flip = drm_atomic_helper_page_flip, 39272d73dd3SThomas Zimmermann .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state, 39372d73dd3SThomas Zimmermann .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state, 39472d73dd3SThomas Zimmermann }; 3959fda81e0SThomas Zimmermann 39672d73dd3SThomas Zimmermann /* 39772d73dd3SThomas Zimmermann * Encoder 39872d73dd3SThomas Zimmermann */ 39940377ef2SStéphane Marchesin 40072d73dd3SThomas Zimmermann static const struct drm_encoder_funcs udl_encoder_funcs = { 40172d73dd3SThomas Zimmermann .destroy = drm_encoder_cleanup, 4025320918bSDave Airlie }; 4035320918bSDave Airlie 4049fda81e0SThomas Zimmermann /* 4050862cfd3SThomas Zimmermann * Connector 4060862cfd3SThomas Zimmermann */ 4070862cfd3SThomas Zimmermann 4080862cfd3SThomas Zimmermann static int udl_connector_helper_get_modes(struct drm_connector *connector) 4090862cfd3SThomas Zimmermann { 4100862cfd3SThomas Zimmermann struct udl_connector *udl_connector = to_udl_connector(connector); 4110862cfd3SThomas Zimmermann 4120862cfd3SThomas Zimmermann drm_connector_update_edid_property(connector, udl_connector->edid); 4130862cfd3SThomas Zimmermann if (udl_connector->edid) 4140862cfd3SThomas Zimmermann return drm_add_edid_modes(connector, udl_connector->edid); 4150862cfd3SThomas Zimmermann 4160862cfd3SThomas Zimmermann return 0; 4170862cfd3SThomas Zimmermann } 4180862cfd3SThomas Zimmermann 4190862cfd3SThomas Zimmermann static const struct drm_connector_helper_funcs udl_connector_helper_funcs = { 4200862cfd3SThomas Zimmermann .get_modes = udl_connector_helper_get_modes, 4210862cfd3SThomas Zimmermann }; 4220862cfd3SThomas Zimmermann 4230862cfd3SThomas Zimmermann static int udl_get_edid_block(void *data, u8 *buf, unsigned int block, size_t len) 4240862cfd3SThomas Zimmermann { 4250862cfd3SThomas Zimmermann struct udl_device *udl = data; 4260862cfd3SThomas Zimmermann struct drm_device *dev = &udl->drm; 4270862cfd3SThomas Zimmermann struct usb_device *udev = udl_to_usb_device(udl); 4280862cfd3SThomas Zimmermann u8 *read_buff; 4290862cfd3SThomas Zimmermann int ret; 4300862cfd3SThomas Zimmermann size_t i; 4310862cfd3SThomas Zimmermann 4320862cfd3SThomas Zimmermann read_buff = kmalloc(2, GFP_KERNEL); 4330862cfd3SThomas Zimmermann if (!read_buff) 4340862cfd3SThomas Zimmermann return -ENOMEM; 4350862cfd3SThomas Zimmermann 4360862cfd3SThomas Zimmermann for (i = 0; i < len; i++) { 4370862cfd3SThomas Zimmermann int bval = (i + block * EDID_LENGTH) << 8; 4380862cfd3SThomas Zimmermann 4390862cfd3SThomas Zimmermann ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 4400862cfd3SThomas Zimmermann 0x02, (0x80 | (0x02 << 5)), bval, 4410862cfd3SThomas Zimmermann 0xA1, read_buff, 2, USB_CTRL_GET_TIMEOUT); 4420862cfd3SThomas Zimmermann if (ret < 0) { 4430862cfd3SThomas Zimmermann drm_err(dev, "Read EDID byte %zu failed err %x\n", i, ret); 4440862cfd3SThomas Zimmermann goto err_kfree; 4450862cfd3SThomas Zimmermann } else if (ret < 1) { 4460862cfd3SThomas Zimmermann ret = -EIO; 4470862cfd3SThomas Zimmermann drm_err(dev, "Read EDID byte %zu failed\n", i); 4480862cfd3SThomas Zimmermann goto err_kfree; 4490862cfd3SThomas Zimmermann } 4500862cfd3SThomas Zimmermann 4510862cfd3SThomas Zimmermann buf[i] = read_buff[1]; 4520862cfd3SThomas Zimmermann } 4530862cfd3SThomas Zimmermann 4540862cfd3SThomas Zimmermann kfree(read_buff); 4550862cfd3SThomas Zimmermann 4560862cfd3SThomas Zimmermann return 0; 4570862cfd3SThomas Zimmermann 4580862cfd3SThomas Zimmermann err_kfree: 4590862cfd3SThomas Zimmermann kfree(read_buff); 4600862cfd3SThomas Zimmermann return ret; 4610862cfd3SThomas Zimmermann } 4620862cfd3SThomas Zimmermann 4630862cfd3SThomas Zimmermann static enum drm_connector_status udl_connector_detect(struct drm_connector *connector, bool force) 4640862cfd3SThomas Zimmermann { 465ca2bd373SThomas Zimmermann struct drm_device *dev = connector->dev; 466ca2bd373SThomas Zimmermann struct udl_device *udl = to_udl(dev); 4670862cfd3SThomas Zimmermann struct udl_connector *udl_connector = to_udl_connector(connector); 468ca2bd373SThomas Zimmermann enum drm_connector_status status = connector_status_disconnected; 469ca2bd373SThomas Zimmermann int idx; 4700862cfd3SThomas Zimmermann 4710862cfd3SThomas Zimmermann /* cleanup previous EDID */ 4720862cfd3SThomas Zimmermann kfree(udl_connector->edid); 473ca2bd373SThomas Zimmermann udl_connector->edid = NULL; 4740862cfd3SThomas Zimmermann 475ca2bd373SThomas Zimmermann if (!drm_dev_enter(dev, &idx)) 4760862cfd3SThomas Zimmermann return connector_status_disconnected; 4770862cfd3SThomas Zimmermann 478ca2bd373SThomas Zimmermann udl_connector->edid = drm_do_get_edid(connector, udl_get_edid_block, udl); 479ca2bd373SThomas Zimmermann if (udl_connector->edid) 480ca2bd373SThomas Zimmermann status = connector_status_connected; 481ca2bd373SThomas Zimmermann 482ca2bd373SThomas Zimmermann drm_dev_exit(idx); 483ca2bd373SThomas Zimmermann 484ca2bd373SThomas Zimmermann return status; 4850862cfd3SThomas Zimmermann } 4860862cfd3SThomas Zimmermann 4870862cfd3SThomas Zimmermann static void udl_connector_destroy(struct drm_connector *connector) 4880862cfd3SThomas Zimmermann { 4890862cfd3SThomas Zimmermann struct udl_connector *udl_connector = to_udl_connector(connector); 4900862cfd3SThomas Zimmermann 4910862cfd3SThomas Zimmermann drm_connector_cleanup(connector); 4920862cfd3SThomas Zimmermann kfree(udl_connector->edid); 4930862cfd3SThomas Zimmermann kfree(udl_connector); 4940862cfd3SThomas Zimmermann } 4950862cfd3SThomas Zimmermann 4960862cfd3SThomas Zimmermann static const struct drm_connector_funcs udl_connector_funcs = { 4970862cfd3SThomas Zimmermann .reset = drm_atomic_helper_connector_reset, 4980862cfd3SThomas Zimmermann .detect = udl_connector_detect, 4990862cfd3SThomas Zimmermann .fill_modes = drm_helper_probe_single_connector_modes, 5000862cfd3SThomas Zimmermann .destroy = udl_connector_destroy, 5010862cfd3SThomas Zimmermann .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, 5020862cfd3SThomas Zimmermann .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 5030862cfd3SThomas Zimmermann }; 5040862cfd3SThomas Zimmermann 5050862cfd3SThomas Zimmermann struct drm_connector *udl_connector_init(struct drm_device *dev) 5060862cfd3SThomas Zimmermann { 5070862cfd3SThomas Zimmermann struct udl_connector *udl_connector; 5080862cfd3SThomas Zimmermann struct drm_connector *connector; 5090862cfd3SThomas Zimmermann int ret; 5100862cfd3SThomas Zimmermann 5110862cfd3SThomas Zimmermann udl_connector = kzalloc(sizeof(*udl_connector), GFP_KERNEL); 5120862cfd3SThomas Zimmermann if (!udl_connector) 5130862cfd3SThomas Zimmermann return ERR_PTR(-ENOMEM); 5140862cfd3SThomas Zimmermann 5150862cfd3SThomas Zimmermann connector = &udl_connector->connector; 5160862cfd3SThomas Zimmermann ret = drm_connector_init(dev, connector, &udl_connector_funcs, DRM_MODE_CONNECTOR_VGA); 5170862cfd3SThomas Zimmermann if (ret) 5180862cfd3SThomas Zimmermann goto err_kfree; 5190862cfd3SThomas Zimmermann 5200862cfd3SThomas Zimmermann drm_connector_helper_add(connector, &udl_connector_helper_funcs); 5210862cfd3SThomas Zimmermann 5220862cfd3SThomas Zimmermann connector->polled = DRM_CONNECTOR_POLL_HPD | 5230862cfd3SThomas Zimmermann DRM_CONNECTOR_POLL_CONNECT | 5240862cfd3SThomas Zimmermann DRM_CONNECTOR_POLL_DISCONNECT; 5250862cfd3SThomas Zimmermann 5260862cfd3SThomas Zimmermann return connector; 5270862cfd3SThomas Zimmermann 5280862cfd3SThomas Zimmermann err_kfree: 5290862cfd3SThomas Zimmermann kfree(udl_connector); 5300862cfd3SThomas Zimmermann return ERR_PTR(ret); 5310862cfd3SThomas Zimmermann } 5320862cfd3SThomas Zimmermann 5330862cfd3SThomas Zimmermann /* 5349fda81e0SThomas Zimmermann * Modesetting 5359fda81e0SThomas Zimmermann */ 5365320918bSDave Airlie 537c020f660SThomas Zimmermann static enum drm_mode_status udl_mode_config_mode_valid(struct drm_device *dev, 538c020f660SThomas Zimmermann const struct drm_display_mode *mode) 539c020f660SThomas Zimmermann { 540c020f660SThomas Zimmermann struct udl_device *udl = to_udl(dev); 541c020f660SThomas Zimmermann 542c020f660SThomas Zimmermann if (udl->sku_pixel_limit) { 543c020f660SThomas Zimmermann if (mode->vdisplay * mode->hdisplay > udl->sku_pixel_limit) 544c020f660SThomas Zimmermann return MODE_MEM; 545c020f660SThomas Zimmermann } 546c020f660SThomas Zimmermann 547c020f660SThomas Zimmermann return MODE_OK; 548c020f660SThomas Zimmermann } 549c020f660SThomas Zimmermann 55072d73dd3SThomas Zimmermann static const struct drm_mode_config_funcs udl_mode_config_funcs = { 551230b8b04SThomas Zimmermann .fb_create = drm_gem_fb_create_with_dirty, 552c020f660SThomas Zimmermann .mode_valid = udl_mode_config_mode_valid, 5539fda81e0SThomas Zimmermann .atomic_check = drm_atomic_helper_check, 5549fda81e0SThomas Zimmermann .atomic_commit = drm_atomic_helper_commit, 5555320918bSDave Airlie }; 5565320918bSDave Airlie 5575320918bSDave Airlie int udl_modeset_init(struct drm_device *dev) 5585320918bSDave Airlie { 5596ae355a2SDaniel Vetter struct udl_device *udl = to_udl(dev); 56072d73dd3SThomas Zimmermann struct drm_plane *primary_plane; 56172d73dd3SThomas Zimmermann struct drm_crtc *crtc; 56272d73dd3SThomas Zimmermann struct drm_encoder *encoder; 563e829cf0bSThomas Zimmermann struct drm_connector *connector; 564e829cf0bSThomas Zimmermann int ret; 565e829cf0bSThomas Zimmermann 566fe5b7c86SDaniel Vetter ret = drmm_mode_config_init(dev); 567fe5b7c86SDaniel Vetter if (ret) 568fe5b7c86SDaniel Vetter return ret; 5695320918bSDave Airlie 5705320918bSDave Airlie dev->mode_config.min_width = 640; 5715320918bSDave Airlie dev->mode_config.min_height = 480; 5725320918bSDave Airlie dev->mode_config.max_width = 2048; 5735320918bSDave Airlie dev->mode_config.max_height = 2048; 574d8177841SThomas Zimmermann dev->mode_config.preferred_depth = 16; 57572d73dd3SThomas Zimmermann dev->mode_config.funcs = &udl_mode_config_funcs; 5765320918bSDave Airlie 57772d73dd3SThomas Zimmermann primary_plane = &udl->primary_plane; 57872d73dd3SThomas Zimmermann ret = drm_universal_plane_init(dev, primary_plane, 0, 57972d73dd3SThomas Zimmermann &udl_primary_plane_funcs, 58072d73dd3SThomas Zimmermann udl_primary_plane_formats, 58172d73dd3SThomas Zimmermann ARRAY_SIZE(udl_primary_plane_formats), 58272d73dd3SThomas Zimmermann udl_primary_plane_fmtmods, 58372d73dd3SThomas Zimmermann DRM_PLANE_TYPE_PRIMARY, NULL); 58472d73dd3SThomas Zimmermann if (ret) 58572d73dd3SThomas Zimmermann return ret; 58672d73dd3SThomas Zimmermann drm_plane_helper_add(primary_plane, &udl_primary_plane_helper_funcs); 58772d73dd3SThomas Zimmermann drm_plane_enable_fb_damage_clips(primary_plane); 58872d73dd3SThomas Zimmermann 58972d73dd3SThomas Zimmermann crtc = &udl->crtc; 59072d73dd3SThomas Zimmermann ret = drm_crtc_init_with_planes(dev, crtc, primary_plane, NULL, 59172d73dd3SThomas Zimmermann &udl_crtc_funcs, NULL); 59272d73dd3SThomas Zimmermann if (ret) 59372d73dd3SThomas Zimmermann return ret; 59472d73dd3SThomas Zimmermann drm_crtc_helper_add(crtc, &udl_crtc_helper_funcs); 59572d73dd3SThomas Zimmermann 59672d73dd3SThomas Zimmermann encoder = &udl->encoder; 59772d73dd3SThomas Zimmermann ret = drm_encoder_init(dev, encoder, &udl_encoder_funcs, DRM_MODE_ENCODER_DAC, NULL); 59872d73dd3SThomas Zimmermann if (ret) 59972d73dd3SThomas Zimmermann return ret; 60072d73dd3SThomas Zimmermann encoder->possible_crtcs = drm_crtc_mask(crtc); 6015320918bSDave Airlie 602e829cf0bSThomas Zimmermann connector = udl_connector_init(dev); 603fe5b7c86SDaniel Vetter if (IS_ERR(connector)) 604fe5b7c86SDaniel Vetter return PTR_ERR(connector); 60572d73dd3SThomas Zimmermann ret = drm_connector_attach_encoder(connector, encoder); 6069fda81e0SThomas Zimmermann if (ret) 607fe5b7c86SDaniel Vetter return ret; 6089fda81e0SThomas Zimmermann 6099fda81e0SThomas Zimmermann drm_mode_config_reset(dev); 6105320918bSDave Airlie 6115320918bSDave Airlie return 0; 6125320918bSDave Airlie } 613