1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 Red Hat
5 * based in parts on udlfb.c:
6 * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it>
7 * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com>
8 * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com>
11 #include <linux/bitfield.h>
13 #include <drm/drm_atomic.h>
14 #include <drm/drm_atomic_helper.h>
15 #include <drm/drm_crtc_helper.h>
16 #include <drm/drm_damage_helper.h>
17 #include <drm/drm_drv.h>
18 #include <drm/drm_edid.h>
19 #include <drm/drm_fourcc.h>
20 #include <drm/drm_gem_atomic_helper.h>
21 #include <drm/drm_gem_framebuffer_helper.h>
22 #include <drm/drm_gem_shmem_helper.h>
23 #include <drm/drm_modeset_helper_vtables.h>
24 #include <drm/drm_plane_helper.h>
25 #include <drm/drm_probe_helper.h>
26 #include <drm/drm_vblank.h>
29 #include "udl_proto.h"
32 * All DisplayLink bulk operations start with 0xaf (UDL_MSG_BULK), followed by
33 * a specific command code. All operations are written to a command buffer, which
34 * the driver sends to the device.
36 static char *udl_set_register(char *buf, u8 reg, u8 val)
38 *buf++ = UDL_MSG_BULK;
39 *buf++ = UDL_CMD_WRITEREG;
46 static char *udl_vidreg_lock(char *buf)
48 return udl_set_register(buf, UDL_REG_VIDREG, UDL_VIDREG_LOCK);
51 static char *udl_vidreg_unlock(char *buf)
53 return udl_set_register(buf, UDL_REG_VIDREG, UDL_VIDREG_UNLOCK);
56 static char *udl_set_blank_mode(char *buf, u8 mode)
58 return udl_set_register(buf, UDL_REG_BLANKMODE, mode);
61 static char *udl_set_color_depth(char *buf, u8 selection)
63 return udl_set_register(buf, UDL_REG_COLORDEPTH, selection);
66 static char *udl_set_base16bpp(char *buf, u32 base)
68 /* the base pointer is 24 bits wide, 0x20 is hi byte. */
69 u8 reg20 = FIELD_GET(UDL_BASE_ADDR2_MASK, base);
70 u8 reg21 = FIELD_GET(UDL_BASE_ADDR1_MASK, base);
71 u8 reg22 = FIELD_GET(UDL_BASE_ADDR0_MASK, base);
73 buf = udl_set_register(buf, UDL_REG_BASE16BPP_ADDR2, reg20);
74 buf = udl_set_register(buf, UDL_REG_BASE16BPP_ADDR1, reg21);
75 buf = udl_set_register(buf, UDL_REG_BASE16BPP_ADDR0, reg22);
81 * DisplayLink HW has separate 16bpp and 8bpp framebuffers.
82 * In 24bpp modes, the low 323 RGB bits go in the 8bpp framebuffer
84 static char *udl_set_base8bpp(char *buf, u32 base)
86 /* the base pointer is 24 bits wide, 0x26 is hi byte. */
87 u8 reg26 = FIELD_GET(UDL_BASE_ADDR2_MASK, base);
88 u8 reg27 = FIELD_GET(UDL_BASE_ADDR1_MASK, base);
89 u8 reg28 = FIELD_GET(UDL_BASE_ADDR0_MASK, base);
91 buf = udl_set_register(buf, UDL_REG_BASE8BPP_ADDR2, reg26);
92 buf = udl_set_register(buf, UDL_REG_BASE8BPP_ADDR1, reg27);
93 buf = udl_set_register(buf, UDL_REG_BASE8BPP_ADDR0, reg28);
98 static char *udl_set_register_16(char *wrptr, u8 reg, u16 value)
100 wrptr = udl_set_register(wrptr, reg, value >> 8);
101 return udl_set_register(wrptr, reg+1, value);
105 * This is kind of weird because the controller takes some
106 * register values in a different byte order than other registers.
108 static char *udl_set_register_16be(char *wrptr, u8 reg, u16 value)
110 wrptr = udl_set_register(wrptr, reg, value);
111 return udl_set_register(wrptr, reg+1, value >> 8);
115 * LFSR is linear feedback shift register. The reason we have this is
116 * because the display controller needs to minimize the clock depth of
117 * various counters used in the display path. So this code reverses the
118 * provided value into the lfsr16 value by counting backwards to get
119 * the value that needs to be set in the hardware comparator to get the
120 * same actual count. This makes sense once you read above a couple of
121 * times and think about it from a hardware perspective.
123 static u16 udl_lfsr16(u16 actual_count)
125 u32 lv = 0xFFFF; /* This is the lfsr value that the hw starts with */
127 while (actual_count--) {
129 (((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1))
137 * This does LFSR conversion on the value that is to be written.
138 * See LFSR explanation above for more detail.
140 static char *udl_set_register_lfsr16(char *wrptr, u8 reg, u16 value)
142 return udl_set_register_16(wrptr, reg, udl_lfsr16(value));
146 * Takes a DRM display mode and converts it into the DisplayLink
147 * equivalent register commands.
149 static char *udl_set_display_mode(char *buf, struct drm_display_mode *mode)
151 u16 reg01 = mode->crtc_htotal - mode->crtc_hsync_start;
152 u16 reg03 = reg01 + mode->crtc_hdisplay;
153 u16 reg05 = mode->crtc_vtotal - mode->crtc_vsync_start;
154 u16 reg07 = reg05 + mode->crtc_vdisplay;
155 u16 reg09 = mode->crtc_htotal - 1;
156 u16 reg0b = 1; /* libdlo hardcodes hsync start to 1 */
157 u16 reg0d = mode->crtc_hsync_end - mode->crtc_hsync_start + 1;
158 u16 reg0f = mode->hdisplay;
159 u16 reg11 = mode->crtc_vtotal;
160 u16 reg13 = 0; /* libdlo hardcodes vsync start to 0 */
161 u16 reg15 = mode->crtc_vsync_end - mode->crtc_vsync_start;
162 u16 reg17 = mode->crtc_vdisplay;
163 u16 reg1b = mode->clock / 5;
165 buf = udl_set_register_lfsr16(buf, UDL_REG_XDISPLAYSTART, reg01);
166 buf = udl_set_register_lfsr16(buf, UDL_REG_XDISPLAYEND, reg03);
167 buf = udl_set_register_lfsr16(buf, UDL_REG_YDISPLAYSTART, reg05);
168 buf = udl_set_register_lfsr16(buf, UDL_REG_YDISPLAYEND, reg07);
169 buf = udl_set_register_lfsr16(buf, UDL_REG_XENDCOUNT, reg09);
170 buf = udl_set_register_lfsr16(buf, UDL_REG_HSYNCSTART, reg0b);
171 buf = udl_set_register_lfsr16(buf, UDL_REG_HSYNCEND, reg0d);
172 buf = udl_set_register_16(buf, UDL_REG_HPIXELS, reg0f);
173 buf = udl_set_register_lfsr16(buf, UDL_REG_YENDCOUNT, reg11);
174 buf = udl_set_register_lfsr16(buf, UDL_REG_VSYNCSTART, reg13);
175 buf = udl_set_register_lfsr16(buf, UDL_REG_VSYNCEND, reg15);
176 buf = udl_set_register_16(buf, UDL_REG_VPIXELS, reg17);
177 buf = udl_set_register_16be(buf, UDL_REG_PIXELCLOCK5KHZ, reg1b);
182 static char *udl_dummy_render(char *wrptr)
184 *wrptr++ = UDL_MSG_BULK;
185 *wrptr++ = UDL_CMD_WRITECOPY16;
186 *wrptr++ = 0x00; /* from addr */
189 *wrptr++ = 0x01; /* one pixel */
190 *wrptr++ = 0x00; /* to address */
196 static long udl_log_cpp(unsigned int cpp)
198 if (WARN_ON(!is_power_of_2(cpp)))
203 static int udl_handle_damage(struct drm_framebuffer *fb,
204 const struct iosys_map *map,
205 const struct drm_rect *clip)
207 struct drm_device *dev = fb->dev;
208 void *vaddr = map->vaddr; /* TODO: Use mapping abstraction properly */
214 ret = udl_log_cpp(fb->format->cpp[0]);
219 urb = udl_get_urb(dev);
222 cmd = urb->transfer_buffer;
224 for (i = clip->y1; i < clip->y2; i++) {
225 const int line_offset = fb->pitches[0] * i;
226 const int byte_offset = line_offset + (clip->x1 << log_bpp);
227 const int dev_byte_offset = (fb->width * i + clip->x1) << log_bpp;
228 const int byte_width = drm_rect_width(clip) << log_bpp;
229 ret = udl_render_hline(dev, log_bpp, &urb, (char *)vaddr,
230 &cmd, byte_offset, dev_byte_offset,
236 if (cmd > (char *)urb->transfer_buffer) {
237 /* Send partial buffer remaining before exiting */
239 if (cmd < (char *)urb->transfer_buffer + urb->transfer_buffer_length)
240 *cmd++ = UDL_MSG_BULK;
241 len = cmd - (char *)urb->transfer_buffer;
242 ret = udl_submit_urb(dev, urb, len);
244 udl_urb_completion(urb);
254 static const uint32_t udl_primary_plane_formats[] = {
259 static const uint64_t udl_primary_plane_fmtmods[] = {
260 DRM_FORMAT_MOD_LINEAR,
261 DRM_FORMAT_MOD_INVALID
264 static void udl_primary_plane_helper_atomic_update(struct drm_plane *plane,
265 struct drm_atomic_state *state)
267 struct drm_device *dev = plane->dev;
268 struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
269 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
270 struct drm_framebuffer *fb = plane_state->fb;
271 struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
272 struct drm_atomic_helper_damage_iter iter;
273 struct drm_rect damage;
277 return; /* no framebuffer; plane is disabled */
279 ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE);
283 if (!drm_dev_enter(dev, &idx))
284 goto out_drm_gem_fb_end_cpu_access;
286 drm_atomic_helper_damage_iter_init(&iter, old_plane_state, plane_state);
287 drm_atomic_for_each_plane_damage(&iter, &damage) {
288 udl_handle_damage(fb, &shadow_plane_state->data[0], &damage);
293 out_drm_gem_fb_end_cpu_access:
294 drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
297 static const struct drm_plane_helper_funcs udl_primary_plane_helper_funcs = {
298 DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
299 .atomic_check = drm_plane_helper_atomic_check,
300 .atomic_update = udl_primary_plane_helper_atomic_update,
303 static const struct drm_plane_funcs udl_primary_plane_funcs = {
304 .update_plane = drm_atomic_helper_update_plane,
305 .disable_plane = drm_atomic_helper_disable_plane,
306 .destroy = drm_plane_cleanup,
307 DRM_GEM_SHADOW_PLANE_FUNCS,
314 static void udl_crtc_helper_atomic_enable(struct drm_crtc *crtc, struct drm_atomic_state *state)
316 struct drm_device *dev = crtc->dev;
317 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
318 struct drm_display_mode *mode = &crtc_state->mode;
323 if (!drm_dev_enter(dev, &idx))
326 urb = udl_get_urb(dev);
330 buf = (char *)urb->transfer_buffer;
331 buf = udl_vidreg_lock(buf);
332 buf = udl_set_color_depth(buf, UDL_COLORDEPTH_16BPP);
333 /* set base for 16bpp segment to 0 */
334 buf = udl_set_base16bpp(buf, 0);
335 /* set base for 8bpp segment to end of fb */
336 buf = udl_set_base8bpp(buf, 2 * mode->vdisplay * mode->hdisplay);
337 buf = udl_set_display_mode(buf, mode);
338 buf = udl_set_blank_mode(buf, UDL_BLANKMODE_ON);
339 buf = udl_vidreg_unlock(buf);
340 buf = udl_dummy_render(buf);
342 udl_submit_urb(dev, urb, buf - (char *)urb->transfer_buffer);
348 static void udl_crtc_helper_atomic_disable(struct drm_crtc *crtc, struct drm_atomic_state *state)
350 struct drm_device *dev = crtc->dev;
355 if (!drm_dev_enter(dev, &idx))
358 urb = udl_get_urb(dev);
362 buf = (char *)urb->transfer_buffer;
363 buf = udl_vidreg_lock(buf);
364 buf = udl_set_blank_mode(buf, UDL_BLANKMODE_POWERDOWN);
365 buf = udl_vidreg_unlock(buf);
366 buf = udl_dummy_render(buf);
368 udl_submit_urb(dev, urb, buf - (char *)urb->transfer_buffer);
374 static const struct drm_crtc_helper_funcs udl_crtc_helper_funcs = {
375 .atomic_check = drm_crtc_helper_atomic_check,
376 .atomic_enable = udl_crtc_helper_atomic_enable,
377 .atomic_disable = udl_crtc_helper_atomic_disable,
380 static const struct drm_crtc_funcs udl_crtc_funcs = {
381 .reset = drm_atomic_helper_crtc_reset,
382 .destroy = drm_crtc_cleanup,
383 .set_config = drm_atomic_helper_set_config,
384 .page_flip = drm_atomic_helper_page_flip,
385 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
386 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
393 static const struct drm_encoder_funcs udl_encoder_funcs = {
394 .destroy = drm_encoder_cleanup,
401 static int udl_connector_helper_get_modes(struct drm_connector *connector)
403 struct udl_connector *udl_connector = to_udl_connector(connector);
405 drm_connector_update_edid_property(connector, udl_connector->edid);
406 if (udl_connector->edid)
407 return drm_add_edid_modes(connector, udl_connector->edid);
412 static const struct drm_connector_helper_funcs udl_connector_helper_funcs = {
413 .get_modes = udl_connector_helper_get_modes,
416 static int udl_get_edid_block(void *data, u8 *buf, unsigned int block, size_t len)
418 struct udl_device *udl = data;
419 struct drm_device *dev = &udl->drm;
420 struct usb_device *udev = udl_to_usb_device(udl);
425 read_buff = kmalloc(2, GFP_KERNEL);
429 for (i = 0; i < len; i++) {
430 int bval = (i + block * EDID_LENGTH) << 8;
432 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
433 0x02, (0x80 | (0x02 << 5)), bval,
434 0xA1, read_buff, 2, USB_CTRL_GET_TIMEOUT);
436 drm_err(dev, "Read EDID byte %zu failed err %x\n", i, ret);
438 } else if (ret < 1) {
440 drm_err(dev, "Read EDID byte %zu failed\n", i);
444 buf[i] = read_buff[1];
456 static enum drm_connector_status udl_connector_detect(struct drm_connector *connector, bool force)
458 struct drm_device *dev = connector->dev;
459 struct udl_device *udl = to_udl(dev);
460 struct udl_connector *udl_connector = to_udl_connector(connector);
461 enum drm_connector_status status = connector_status_disconnected;
464 /* cleanup previous EDID */
465 kfree(udl_connector->edid);
466 udl_connector->edid = NULL;
468 if (!drm_dev_enter(dev, &idx))
469 return connector_status_disconnected;
471 udl_connector->edid = drm_do_get_edid(connector, udl_get_edid_block, udl);
472 if (udl_connector->edid)
473 status = connector_status_connected;
480 static void udl_connector_destroy(struct drm_connector *connector)
482 struct udl_connector *udl_connector = to_udl_connector(connector);
484 drm_connector_cleanup(connector);
485 kfree(udl_connector->edid);
486 kfree(udl_connector);
489 static const struct drm_connector_funcs udl_connector_funcs = {
490 .reset = drm_atomic_helper_connector_reset,
491 .detect = udl_connector_detect,
492 .fill_modes = drm_helper_probe_single_connector_modes,
493 .destroy = udl_connector_destroy,
494 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
495 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
498 struct drm_connector *udl_connector_init(struct drm_device *dev)
500 struct udl_connector *udl_connector;
501 struct drm_connector *connector;
504 udl_connector = kzalloc(sizeof(*udl_connector), GFP_KERNEL);
506 return ERR_PTR(-ENOMEM);
508 connector = &udl_connector->connector;
509 ret = drm_connector_init(dev, connector, &udl_connector_funcs, DRM_MODE_CONNECTOR_VGA);
513 drm_connector_helper_add(connector, &udl_connector_helper_funcs);
515 connector->polled = DRM_CONNECTOR_POLL_HPD |
516 DRM_CONNECTOR_POLL_CONNECT |
517 DRM_CONNECTOR_POLL_DISCONNECT;
522 kfree(udl_connector);
530 static enum drm_mode_status udl_mode_config_mode_valid(struct drm_device *dev,
531 const struct drm_display_mode *mode)
533 struct udl_device *udl = to_udl(dev);
535 if (udl->sku_pixel_limit) {
536 if (mode->vdisplay * mode->hdisplay > udl->sku_pixel_limit)
543 static const struct drm_mode_config_funcs udl_mode_config_funcs = {
544 .fb_create = drm_gem_fb_create_with_dirty,
545 .mode_valid = udl_mode_config_mode_valid,
546 .atomic_check = drm_atomic_helper_check,
547 .atomic_commit = drm_atomic_helper_commit,
550 int udl_modeset_init(struct drm_device *dev)
552 struct udl_device *udl = to_udl(dev);
553 struct drm_plane *primary_plane;
554 struct drm_crtc *crtc;
555 struct drm_encoder *encoder;
556 struct drm_connector *connector;
559 ret = drmm_mode_config_init(dev);
563 dev->mode_config.min_width = 640;
564 dev->mode_config.min_height = 480;
565 dev->mode_config.max_width = 2048;
566 dev->mode_config.max_height = 2048;
567 dev->mode_config.preferred_depth = 16;
568 dev->mode_config.funcs = &udl_mode_config_funcs;
570 primary_plane = &udl->primary_plane;
571 ret = drm_universal_plane_init(dev, primary_plane, 0,
572 &udl_primary_plane_funcs,
573 udl_primary_plane_formats,
574 ARRAY_SIZE(udl_primary_plane_formats),
575 udl_primary_plane_fmtmods,
576 DRM_PLANE_TYPE_PRIMARY, NULL);
579 drm_plane_helper_add(primary_plane, &udl_primary_plane_helper_funcs);
580 drm_plane_enable_fb_damage_clips(primary_plane);
583 ret = drm_crtc_init_with_planes(dev, crtc, primary_plane, NULL,
584 &udl_crtc_funcs, NULL);
587 drm_crtc_helper_add(crtc, &udl_crtc_helper_funcs);
589 encoder = &udl->encoder;
590 ret = drm_encoder_init(dev, encoder, &udl_encoder_funcs, DRM_MODE_ENCODER_DAC, NULL);
593 encoder->possible_crtcs = drm_crtc_mask(crtc);
595 connector = udl_connector_init(dev);
596 if (IS_ERR(connector))
597 return PTR_ERR(connector);
598 ret = drm_connector_attach_encoder(connector, encoder);
602 drm_mode_config_reset(dev);