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> 1548c5c68fSGeert Uytterhoeven #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 /* 321b8db07fSThomas Zimmermann * All DisplayLink bulk operations start with 0xaf (UDL_MSG_BULK), followed by 331b8db07fSThomas Zimmermann * a specific command code. All operations are written to a command buffer, which 341b8db07fSThomas Zimmermann * the driver sends to the device. 355320918bSDave Airlie */ 365320918bSDave Airlie static char *udl_set_register(char *buf, u8 reg, u8 val) 375320918bSDave Airlie { 381b8db07fSThomas Zimmermann *buf++ = UDL_MSG_BULK; 391b8db07fSThomas Zimmermann *buf++ = UDL_CMD_WRITEREG; 405320918bSDave Airlie *buf++ = reg; 415320918bSDave Airlie *buf++ = val; 421b8db07fSThomas 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 { 1841b8db07fSThomas Zimmermann *wrptr++ = UDL_MSG_BULK; 1851b8db07fSThomas 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) 2401b8db07fSThomas 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 void udl_crtc_helper_atomic_enable(struct drm_crtc *crtc, struct drm_atomic_state *state) 31572d73dd3SThomas Zimmermann { 31672d73dd3SThomas Zimmermann struct drm_device *dev = crtc->dev; 31772d73dd3SThomas Zimmermann struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc); 31872d73dd3SThomas Zimmermann struct drm_display_mode *mode = &crtc_state->mode; 319890e4de8SThomas Zimmermann struct urb *urb; 3205320918bSDave Airlie char *buf; 321ca2bd373SThomas Zimmermann int idx; 322ca2bd373SThomas Zimmermann 323ca2bd373SThomas Zimmermann if (!drm_dev_enter(dev, &idx)) 324ca2bd373SThomas Zimmermann return; 3255320918bSDave Airlie 326890e4de8SThomas Zimmermann urb = udl_get_urb(dev); 327890e4de8SThomas Zimmermann if (!urb) 328ca2bd373SThomas Zimmermann goto out; 3295320918bSDave Airlie 330890e4de8SThomas Zimmermann buf = (char *)urb->transfer_buffer; 331890e4de8SThomas Zimmermann buf = udl_vidreg_lock(buf); 332ff76e82cSThomas Zimmermann buf = udl_set_color_depth(buf, UDL_COLORDEPTH_16BPP); 3335320918bSDave Airlie /* set base for 16bpp segment to 0 */ 334890e4de8SThomas Zimmermann buf = udl_set_base16bpp(buf, 0); 3355320918bSDave Airlie /* set base for 8bpp segment to end of fb */ 336890e4de8SThomas Zimmermann buf = udl_set_base8bpp(buf, 2 * mode->vdisplay * mode->hdisplay); 3379869e40dSThomas Zimmermann buf = udl_set_display_mode(buf, mode); 338ff76e82cSThomas Zimmermann buf = udl_set_blank_mode(buf, UDL_BLANKMODE_ON); 339890e4de8SThomas Zimmermann buf = udl_vidreg_unlock(buf); 340890e4de8SThomas Zimmermann buf = udl_dummy_render(buf); 3415bd42f69SDave Airlie 342890e4de8SThomas Zimmermann udl_submit_urb(dev, urb, buf - (char *)urb->transfer_buffer); 343ca2bd373SThomas Zimmermann 344ca2bd373SThomas Zimmermann out: 345ca2bd373SThomas Zimmermann drm_dev_exit(idx); 3469fda81e0SThomas Zimmermann } 3479fda81e0SThomas Zimmermann 34872d73dd3SThomas Zimmermann static void udl_crtc_helper_atomic_disable(struct drm_crtc *crtc, struct drm_atomic_state *state) 3499fda81e0SThomas Zimmermann { 350997d33c3SThomas Zimmermann struct drm_device *dev = crtc->dev; 351997d33c3SThomas Zimmermann struct urb *urb; 352997d33c3SThomas Zimmermann char *buf; 353ca2bd373SThomas Zimmermann int idx; 354ca2bd373SThomas Zimmermann 355ca2bd373SThomas Zimmermann if (!drm_dev_enter(dev, &idx)) 356ca2bd373SThomas Zimmermann return; 357997d33c3SThomas Zimmermann 358997d33c3SThomas Zimmermann urb = udl_get_urb(dev); 359997d33c3SThomas Zimmermann if (!urb) 360ca2bd373SThomas Zimmermann goto out; 361997d33c3SThomas Zimmermann 362997d33c3SThomas Zimmermann buf = (char *)urb->transfer_buffer; 363997d33c3SThomas Zimmermann buf = udl_vidreg_lock(buf); 364ff76e82cSThomas Zimmermann buf = udl_set_blank_mode(buf, UDL_BLANKMODE_POWERDOWN); 365997d33c3SThomas Zimmermann buf = udl_vidreg_unlock(buf); 366997d33c3SThomas Zimmermann buf = udl_dummy_render(buf); 367997d33c3SThomas Zimmermann 368997d33c3SThomas Zimmermann udl_submit_urb(dev, urb, buf - (char *)urb->transfer_buffer); 369ca2bd373SThomas Zimmermann 370ca2bd373SThomas Zimmermann out: 371ca2bd373SThomas Zimmermann drm_dev_exit(idx); 3729fda81e0SThomas Zimmermann } 3739fda81e0SThomas Zimmermann 37472d73dd3SThomas Zimmermann static const struct drm_crtc_helper_funcs udl_crtc_helper_funcs = { 37548c5c68fSGeert Uytterhoeven .atomic_check = drm_crtc_helper_atomic_check, 37672d73dd3SThomas Zimmermann .atomic_enable = udl_crtc_helper_atomic_enable, 37772d73dd3SThomas Zimmermann .atomic_disable = udl_crtc_helper_atomic_disable, 37872d73dd3SThomas Zimmermann }; 37940377ef2SStéphane Marchesin 38072d73dd3SThomas Zimmermann static const struct drm_crtc_funcs udl_crtc_funcs = { 38172d73dd3SThomas Zimmermann .reset = drm_atomic_helper_crtc_reset, 38272d73dd3SThomas Zimmermann .destroy = drm_crtc_cleanup, 38372d73dd3SThomas Zimmermann .set_config = drm_atomic_helper_set_config, 38472d73dd3SThomas Zimmermann .page_flip = drm_atomic_helper_page_flip, 38572d73dd3SThomas Zimmermann .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state, 38672d73dd3SThomas Zimmermann .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state, 38772d73dd3SThomas Zimmermann }; 3889fda81e0SThomas Zimmermann 38972d73dd3SThomas Zimmermann /* 39072d73dd3SThomas Zimmermann * Encoder 39172d73dd3SThomas Zimmermann */ 39240377ef2SStéphane Marchesin 39372d73dd3SThomas Zimmermann static const struct drm_encoder_funcs udl_encoder_funcs = { 39472d73dd3SThomas Zimmermann .destroy = drm_encoder_cleanup, 3955320918bSDave Airlie }; 3965320918bSDave Airlie 3979fda81e0SThomas Zimmermann /* 3980862cfd3SThomas Zimmermann * Connector 3990862cfd3SThomas Zimmermann */ 4000862cfd3SThomas Zimmermann 4010862cfd3SThomas Zimmermann static int udl_connector_helper_get_modes(struct drm_connector *connector) 4020862cfd3SThomas Zimmermann { 4030862cfd3SThomas Zimmermann struct udl_connector *udl_connector = to_udl_connector(connector); 4040862cfd3SThomas Zimmermann 4050862cfd3SThomas Zimmermann drm_connector_update_edid_property(connector, udl_connector->edid); 4060862cfd3SThomas Zimmermann if (udl_connector->edid) 4070862cfd3SThomas Zimmermann return drm_add_edid_modes(connector, udl_connector->edid); 4080862cfd3SThomas Zimmermann 4090862cfd3SThomas Zimmermann return 0; 4100862cfd3SThomas Zimmermann } 4110862cfd3SThomas Zimmermann 4120862cfd3SThomas Zimmermann static const struct drm_connector_helper_funcs udl_connector_helper_funcs = { 4130862cfd3SThomas Zimmermann .get_modes = udl_connector_helper_get_modes, 4140862cfd3SThomas Zimmermann }; 4150862cfd3SThomas Zimmermann 4160862cfd3SThomas Zimmermann static int udl_get_edid_block(void *data, u8 *buf, unsigned int block, size_t len) 4170862cfd3SThomas Zimmermann { 4180862cfd3SThomas Zimmermann struct udl_device *udl = data; 4190862cfd3SThomas Zimmermann struct drm_device *dev = &udl->drm; 4200862cfd3SThomas Zimmermann struct usb_device *udev = udl_to_usb_device(udl); 4210862cfd3SThomas Zimmermann u8 *read_buff; 4220862cfd3SThomas Zimmermann int ret; 4230862cfd3SThomas Zimmermann size_t i; 4240862cfd3SThomas Zimmermann 4250862cfd3SThomas Zimmermann read_buff = kmalloc(2, GFP_KERNEL); 4260862cfd3SThomas Zimmermann if (!read_buff) 4270862cfd3SThomas Zimmermann return -ENOMEM; 4280862cfd3SThomas Zimmermann 4290862cfd3SThomas Zimmermann for (i = 0; i < len; i++) { 4300862cfd3SThomas Zimmermann int bval = (i + block * EDID_LENGTH) << 8; 4310862cfd3SThomas Zimmermann 4320862cfd3SThomas Zimmermann ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 4330862cfd3SThomas Zimmermann 0x02, (0x80 | (0x02 << 5)), bval, 4340862cfd3SThomas Zimmermann 0xA1, read_buff, 2, USB_CTRL_GET_TIMEOUT); 4350862cfd3SThomas Zimmermann if (ret < 0) { 4360862cfd3SThomas Zimmermann drm_err(dev, "Read EDID byte %zu failed err %x\n", i, ret); 4370862cfd3SThomas Zimmermann goto err_kfree; 4380862cfd3SThomas Zimmermann } else if (ret < 1) { 4390862cfd3SThomas Zimmermann ret = -EIO; 4400862cfd3SThomas Zimmermann drm_err(dev, "Read EDID byte %zu failed\n", i); 4410862cfd3SThomas Zimmermann goto err_kfree; 4420862cfd3SThomas Zimmermann } 4430862cfd3SThomas Zimmermann 4440862cfd3SThomas Zimmermann buf[i] = read_buff[1]; 4450862cfd3SThomas Zimmermann } 4460862cfd3SThomas Zimmermann 4470862cfd3SThomas Zimmermann kfree(read_buff); 4480862cfd3SThomas Zimmermann 4490862cfd3SThomas Zimmermann return 0; 4500862cfd3SThomas Zimmermann 4510862cfd3SThomas Zimmermann err_kfree: 4520862cfd3SThomas Zimmermann kfree(read_buff); 4530862cfd3SThomas Zimmermann return ret; 4540862cfd3SThomas Zimmermann } 4550862cfd3SThomas Zimmermann 4560862cfd3SThomas Zimmermann static enum drm_connector_status udl_connector_detect(struct drm_connector *connector, bool force) 4570862cfd3SThomas Zimmermann { 458ca2bd373SThomas Zimmermann struct drm_device *dev = connector->dev; 459ca2bd373SThomas Zimmermann struct udl_device *udl = to_udl(dev); 4600862cfd3SThomas Zimmermann struct udl_connector *udl_connector = to_udl_connector(connector); 461ca2bd373SThomas Zimmermann enum drm_connector_status status = connector_status_disconnected; 462ca2bd373SThomas Zimmermann int idx; 4630862cfd3SThomas Zimmermann 4640862cfd3SThomas Zimmermann /* cleanup previous EDID */ 4650862cfd3SThomas Zimmermann kfree(udl_connector->edid); 466ca2bd373SThomas Zimmermann udl_connector->edid = NULL; 4670862cfd3SThomas Zimmermann 468ca2bd373SThomas Zimmermann if (!drm_dev_enter(dev, &idx)) 4690862cfd3SThomas Zimmermann return connector_status_disconnected; 4700862cfd3SThomas Zimmermann 471ca2bd373SThomas Zimmermann udl_connector->edid = drm_do_get_edid(connector, udl_get_edid_block, udl); 472ca2bd373SThomas Zimmermann if (udl_connector->edid) 473ca2bd373SThomas Zimmermann status = connector_status_connected; 474ca2bd373SThomas Zimmermann 475ca2bd373SThomas Zimmermann drm_dev_exit(idx); 476ca2bd373SThomas Zimmermann 477ca2bd373SThomas Zimmermann return status; 4780862cfd3SThomas Zimmermann } 4790862cfd3SThomas Zimmermann 4800862cfd3SThomas Zimmermann static void udl_connector_destroy(struct drm_connector *connector) 4810862cfd3SThomas Zimmermann { 4820862cfd3SThomas Zimmermann struct udl_connector *udl_connector = to_udl_connector(connector); 4830862cfd3SThomas Zimmermann 4840862cfd3SThomas Zimmermann drm_connector_cleanup(connector); 4850862cfd3SThomas Zimmermann kfree(udl_connector->edid); 4860862cfd3SThomas Zimmermann kfree(udl_connector); 4870862cfd3SThomas Zimmermann } 4880862cfd3SThomas Zimmermann 4890862cfd3SThomas Zimmermann static const struct drm_connector_funcs udl_connector_funcs = { 4900862cfd3SThomas Zimmermann .reset = drm_atomic_helper_connector_reset, 4910862cfd3SThomas Zimmermann .detect = udl_connector_detect, 4920862cfd3SThomas Zimmermann .fill_modes = drm_helper_probe_single_connector_modes, 4930862cfd3SThomas Zimmermann .destroy = udl_connector_destroy, 4940862cfd3SThomas Zimmermann .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, 4950862cfd3SThomas Zimmermann .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 4960862cfd3SThomas Zimmermann }; 4970862cfd3SThomas Zimmermann 4980862cfd3SThomas Zimmermann struct drm_connector *udl_connector_init(struct drm_device *dev) 4990862cfd3SThomas Zimmermann { 5000862cfd3SThomas Zimmermann struct udl_connector *udl_connector; 5010862cfd3SThomas Zimmermann struct drm_connector *connector; 5020862cfd3SThomas Zimmermann int ret; 5030862cfd3SThomas Zimmermann 5040862cfd3SThomas Zimmermann udl_connector = kzalloc(sizeof(*udl_connector), GFP_KERNEL); 5050862cfd3SThomas Zimmermann if (!udl_connector) 5060862cfd3SThomas Zimmermann return ERR_PTR(-ENOMEM); 5070862cfd3SThomas Zimmermann 5080862cfd3SThomas Zimmermann connector = &udl_connector->connector; 5090862cfd3SThomas Zimmermann ret = drm_connector_init(dev, connector, &udl_connector_funcs, DRM_MODE_CONNECTOR_VGA); 5100862cfd3SThomas Zimmermann if (ret) 5110862cfd3SThomas Zimmermann goto err_kfree; 5120862cfd3SThomas Zimmermann 5130862cfd3SThomas Zimmermann drm_connector_helper_add(connector, &udl_connector_helper_funcs); 5140862cfd3SThomas Zimmermann 515*b29c910cSThomas Zimmermann connector->polled = DRM_CONNECTOR_POLL_CONNECT | 5160862cfd3SThomas Zimmermann DRM_CONNECTOR_POLL_DISCONNECT; 5170862cfd3SThomas Zimmermann 5180862cfd3SThomas Zimmermann return connector; 5190862cfd3SThomas Zimmermann 5200862cfd3SThomas Zimmermann err_kfree: 5210862cfd3SThomas Zimmermann kfree(udl_connector); 5220862cfd3SThomas Zimmermann return ERR_PTR(ret); 5230862cfd3SThomas Zimmermann } 5240862cfd3SThomas Zimmermann 5250862cfd3SThomas Zimmermann /* 5269fda81e0SThomas Zimmermann * Modesetting 5279fda81e0SThomas Zimmermann */ 5285320918bSDave Airlie 529c020f660SThomas Zimmermann static enum drm_mode_status udl_mode_config_mode_valid(struct drm_device *dev, 530c020f660SThomas Zimmermann const struct drm_display_mode *mode) 531c020f660SThomas Zimmermann { 532c020f660SThomas Zimmermann struct udl_device *udl = to_udl(dev); 533c020f660SThomas Zimmermann 534c020f660SThomas Zimmermann if (udl->sku_pixel_limit) { 535c020f660SThomas Zimmermann if (mode->vdisplay * mode->hdisplay > udl->sku_pixel_limit) 536c020f660SThomas Zimmermann return MODE_MEM; 537c020f660SThomas Zimmermann } 538c020f660SThomas Zimmermann 539c020f660SThomas Zimmermann return MODE_OK; 540c020f660SThomas Zimmermann } 541c020f660SThomas Zimmermann 54272d73dd3SThomas Zimmermann static const struct drm_mode_config_funcs udl_mode_config_funcs = { 543230b8b04SThomas Zimmermann .fb_create = drm_gem_fb_create_with_dirty, 544c020f660SThomas Zimmermann .mode_valid = udl_mode_config_mode_valid, 5459fda81e0SThomas Zimmermann .atomic_check = drm_atomic_helper_check, 5469fda81e0SThomas Zimmermann .atomic_commit = drm_atomic_helper_commit, 5475320918bSDave Airlie }; 5485320918bSDave Airlie 5495320918bSDave Airlie int udl_modeset_init(struct drm_device *dev) 5505320918bSDave Airlie { 5516ae355a2SDaniel Vetter struct udl_device *udl = to_udl(dev); 55272d73dd3SThomas Zimmermann struct drm_plane *primary_plane; 55372d73dd3SThomas Zimmermann struct drm_crtc *crtc; 55472d73dd3SThomas Zimmermann struct drm_encoder *encoder; 555e829cf0bSThomas Zimmermann struct drm_connector *connector; 556e829cf0bSThomas Zimmermann int ret; 557e829cf0bSThomas Zimmermann 558fe5b7c86SDaniel Vetter ret = drmm_mode_config_init(dev); 559fe5b7c86SDaniel Vetter if (ret) 560fe5b7c86SDaniel Vetter return ret; 5615320918bSDave Airlie 5625320918bSDave Airlie dev->mode_config.min_width = 640; 5635320918bSDave Airlie dev->mode_config.min_height = 480; 5645320918bSDave Airlie dev->mode_config.max_width = 2048; 5655320918bSDave Airlie dev->mode_config.max_height = 2048; 566d8177841SThomas Zimmermann dev->mode_config.preferred_depth = 16; 56772d73dd3SThomas Zimmermann dev->mode_config.funcs = &udl_mode_config_funcs; 5685320918bSDave Airlie 56972d73dd3SThomas Zimmermann primary_plane = &udl->primary_plane; 57072d73dd3SThomas Zimmermann ret = drm_universal_plane_init(dev, primary_plane, 0, 57172d73dd3SThomas Zimmermann &udl_primary_plane_funcs, 57272d73dd3SThomas Zimmermann udl_primary_plane_formats, 57372d73dd3SThomas Zimmermann ARRAY_SIZE(udl_primary_plane_formats), 57472d73dd3SThomas Zimmermann udl_primary_plane_fmtmods, 57572d73dd3SThomas Zimmermann DRM_PLANE_TYPE_PRIMARY, NULL); 57672d73dd3SThomas Zimmermann if (ret) 57772d73dd3SThomas Zimmermann return ret; 57872d73dd3SThomas Zimmermann drm_plane_helper_add(primary_plane, &udl_primary_plane_helper_funcs); 57972d73dd3SThomas Zimmermann drm_plane_enable_fb_damage_clips(primary_plane); 58072d73dd3SThomas Zimmermann 58172d73dd3SThomas Zimmermann crtc = &udl->crtc; 58272d73dd3SThomas Zimmermann ret = drm_crtc_init_with_planes(dev, crtc, primary_plane, NULL, 58372d73dd3SThomas Zimmermann &udl_crtc_funcs, NULL); 58472d73dd3SThomas Zimmermann if (ret) 58572d73dd3SThomas Zimmermann return ret; 58672d73dd3SThomas Zimmermann drm_crtc_helper_add(crtc, &udl_crtc_helper_funcs); 58772d73dd3SThomas Zimmermann 58872d73dd3SThomas Zimmermann encoder = &udl->encoder; 58972d73dd3SThomas Zimmermann ret = drm_encoder_init(dev, encoder, &udl_encoder_funcs, DRM_MODE_ENCODER_DAC, NULL); 59072d73dd3SThomas Zimmermann if (ret) 59172d73dd3SThomas Zimmermann return ret; 59272d73dd3SThomas Zimmermann encoder->possible_crtcs = drm_crtc_mask(crtc); 5935320918bSDave Airlie 594e829cf0bSThomas Zimmermann connector = udl_connector_init(dev); 595fe5b7c86SDaniel Vetter if (IS_ERR(connector)) 596fe5b7c86SDaniel Vetter return PTR_ERR(connector); 59772d73dd3SThomas Zimmermann ret = drm_connector_attach_encoder(connector, encoder); 5989fda81e0SThomas Zimmermann if (ret) 599fe5b7c86SDaniel Vetter return ret; 6009fda81e0SThomas Zimmermann 6019fda81e0SThomas Zimmermann drm_mode_config_reset(dev); 6025320918bSDave Airlie 6035320918bSDave Airlie return 0; 6045320918bSDave Airlie } 605