Liviu Dudau | 8e22d79 | 2015-04-02 19:48:39 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013-2015 ARM Limited |
| 3 | * Author: Liviu Dudau <Liviu.Dudau@arm.com> |
| 4 | * |
| 5 | * This file is subject to the terms and conditions of the GNU General Public |
| 6 | * License. See the file COPYING in the main directory of this archive |
| 7 | * for more details. |
| 8 | * |
| 9 | * Implementation of a CRTC class for the HDLCD driver. |
| 10 | */ |
| 11 | |
| 12 | #include <drm/drmP.h> |
Liviu Dudau | 1de3cd4 | 2017-03-08 16:10:19 +0000 | [diff] [blame^] | 13 | #include <drm/drm_atomic.h> |
Liviu Dudau | 8e22d79 | 2015-04-02 19:48:39 +0100 | [diff] [blame] | 14 | #include <drm/drm_atomic_helper.h> |
| 15 | #include <drm/drm_crtc.h> |
| 16 | #include <drm/drm_crtc_helper.h> |
| 17 | #include <drm/drm_fb_helper.h> |
| 18 | #include <drm/drm_fb_cma_helper.h> |
| 19 | #include <drm/drm_gem_cma_helper.h> |
| 20 | #include <drm/drm_of.h> |
| 21 | #include <drm/drm_plane_helper.h> |
| 22 | #include <linux/clk.h> |
| 23 | #include <linux/of_graph.h> |
| 24 | #include <linux/platform_data/simplefb.h> |
| 25 | #include <video/videomode.h> |
| 26 | |
| 27 | #include "hdlcd_drv.h" |
| 28 | #include "hdlcd_regs.h" |
| 29 | |
| 30 | /* |
| 31 | * The HDLCD controller is a dumb RGB streamer that gets connected to |
| 32 | * a single HDMI transmitter or in the case of the ARM Models it gets |
| 33 | * emulated by the software that does the actual rendering. |
| 34 | * |
| 35 | */ |
| 36 | |
Liviu Dudau | a95acec | 2016-05-17 10:06:54 +0100 | [diff] [blame] | 37 | static void hdlcd_crtc_cleanup(struct drm_crtc *crtc) |
| 38 | { |
| 39 | struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc); |
| 40 | |
| 41 | /* stop the controller on cleanup */ |
| 42 | hdlcd_write(hdlcd, HDLCD_REG_COMMAND, 0); |
| 43 | drm_crtc_cleanup(crtc); |
| 44 | } |
| 45 | |
Liviu Dudau | 8e22d79 | 2015-04-02 19:48:39 +0100 | [diff] [blame] | 46 | static const struct drm_crtc_funcs hdlcd_crtc_funcs = { |
Liviu Dudau | a95acec | 2016-05-17 10:06:54 +0100 | [diff] [blame] | 47 | .destroy = hdlcd_crtc_cleanup, |
Liviu Dudau | 8e22d79 | 2015-04-02 19:48:39 +0100 | [diff] [blame] | 48 | .set_config = drm_atomic_helper_set_config, |
| 49 | .page_flip = drm_atomic_helper_page_flip, |
| 50 | .reset = drm_atomic_helper_crtc_reset, |
| 51 | .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state, |
| 52 | .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state, |
| 53 | }; |
| 54 | |
| 55 | static struct simplefb_format supported_formats[] = SIMPLEFB_FORMATS; |
| 56 | |
| 57 | /* |
| 58 | * Setup the HDLCD registers for decoding the pixels out of the framebuffer |
| 59 | */ |
| 60 | static int hdlcd_set_pxl_fmt(struct drm_crtc *crtc) |
| 61 | { |
| 62 | unsigned int btpp; |
| 63 | struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc); |
Ville Syrjälä | 59477fa | 2016-11-18 21:52:45 +0200 | [diff] [blame] | 64 | const struct drm_framebuffer *fb = crtc->primary->state->fb; |
Liviu Dudau | 8e22d79 | 2015-04-02 19:48:39 +0100 | [diff] [blame] | 65 | uint32_t pixel_format; |
| 66 | struct simplefb_format *format = NULL; |
| 67 | int i; |
| 68 | |
Ville Syrjälä | 438b74a | 2016-12-14 23:32:55 +0200 | [diff] [blame] | 69 | pixel_format = fb->format->format; |
Liviu Dudau | 8e22d79 | 2015-04-02 19:48:39 +0100 | [diff] [blame] | 70 | |
| 71 | for (i = 0; i < ARRAY_SIZE(supported_formats); i++) { |
| 72 | if (supported_formats[i].fourcc == pixel_format) |
| 73 | format = &supported_formats[i]; |
| 74 | } |
| 75 | |
| 76 | if (WARN_ON(!format)) |
| 77 | return 0; |
| 78 | |
| 79 | /* HDLCD uses 'bytes per pixel', zero means 1 byte */ |
| 80 | btpp = (format->bits_per_pixel + 7) / 8; |
| 81 | hdlcd_write(hdlcd, HDLCD_REG_PIXEL_FORMAT, (btpp - 1) << 3); |
| 82 | |
| 83 | /* |
| 84 | * The format of the HDLCD_REG_<color>_SELECT register is: |
| 85 | * - bits[23:16] - default value for that color component |
| 86 | * - bits[11:8] - number of bits to extract for each color component |
| 87 | * - bits[4:0] - index of the lowest bit to extract |
| 88 | * |
| 89 | * The default color value is used when bits[11:8] are zero, when the |
| 90 | * pixel is outside the visible frame area or when there is a |
| 91 | * buffer underrun. |
| 92 | */ |
| 93 | hdlcd_write(hdlcd, HDLCD_REG_RED_SELECT, format->red.offset | |
| 94 | #ifdef CONFIG_DRM_HDLCD_SHOW_UNDERRUN |
| 95 | 0x00ff0000 | /* show underruns in red */ |
| 96 | #endif |
| 97 | ((format->red.length & 0xf) << 8)); |
| 98 | hdlcd_write(hdlcd, HDLCD_REG_GREEN_SELECT, format->green.offset | |
| 99 | ((format->green.length & 0xf) << 8)); |
| 100 | hdlcd_write(hdlcd, HDLCD_REG_BLUE_SELECT, format->blue.offset | |
| 101 | ((format->blue.length & 0xf) << 8)); |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static void hdlcd_crtc_mode_set_nofb(struct drm_crtc *crtc) |
| 107 | { |
| 108 | struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc); |
| 109 | struct drm_display_mode *m = &crtc->state->adjusted_mode; |
| 110 | struct videomode vm; |
Liviu Dudau | 96ebb1f | 2016-06-01 15:00:15 +0100 | [diff] [blame] | 111 | unsigned int polarities, err; |
Liviu Dudau | 8e22d79 | 2015-04-02 19:48:39 +0100 | [diff] [blame] | 112 | |
| 113 | vm.vfront_porch = m->crtc_vsync_start - m->crtc_vdisplay; |
| 114 | vm.vback_porch = m->crtc_vtotal - m->crtc_vsync_end; |
| 115 | vm.vsync_len = m->crtc_vsync_end - m->crtc_vsync_start; |
| 116 | vm.hfront_porch = m->crtc_hsync_start - m->crtc_hdisplay; |
| 117 | vm.hback_porch = m->crtc_htotal - m->crtc_hsync_end; |
| 118 | vm.hsync_len = m->crtc_hsync_end - m->crtc_hsync_start; |
| 119 | |
| 120 | polarities = HDLCD_POLARITY_DATAEN | HDLCD_POLARITY_DATA; |
| 121 | |
| 122 | if (m->flags & DRM_MODE_FLAG_PHSYNC) |
| 123 | polarities |= HDLCD_POLARITY_HSYNC; |
| 124 | if (m->flags & DRM_MODE_FLAG_PVSYNC) |
| 125 | polarities |= HDLCD_POLARITY_VSYNC; |
| 126 | |
Liviu Dudau | 8e22d79 | 2015-04-02 19:48:39 +0100 | [diff] [blame] | 127 | /* Allow max number of outstanding requests and largest burst size */ |
| 128 | hdlcd_write(hdlcd, HDLCD_REG_BUS_OPTIONS, |
| 129 | HDLCD_BUS_MAX_OUTSTAND | HDLCD_BUS_BURST_16); |
| 130 | |
Liviu Dudau | 8e22d79 | 2015-04-02 19:48:39 +0100 | [diff] [blame] | 131 | hdlcd_write(hdlcd, HDLCD_REG_V_DATA, m->crtc_vdisplay - 1); |
| 132 | hdlcd_write(hdlcd, HDLCD_REG_V_BACK_PORCH, vm.vback_porch - 1); |
| 133 | hdlcd_write(hdlcd, HDLCD_REG_V_FRONT_PORCH, vm.vfront_porch - 1); |
| 134 | hdlcd_write(hdlcd, HDLCD_REG_V_SYNC, vm.vsync_len - 1); |
Liviu Dudau | 96ebb1f | 2016-06-01 15:00:15 +0100 | [diff] [blame] | 135 | hdlcd_write(hdlcd, HDLCD_REG_H_DATA, m->crtc_hdisplay - 1); |
Liviu Dudau | 8e22d79 | 2015-04-02 19:48:39 +0100 | [diff] [blame] | 136 | hdlcd_write(hdlcd, HDLCD_REG_H_BACK_PORCH, vm.hback_porch - 1); |
| 137 | hdlcd_write(hdlcd, HDLCD_REG_H_FRONT_PORCH, vm.hfront_porch - 1); |
| 138 | hdlcd_write(hdlcd, HDLCD_REG_H_SYNC, vm.hsync_len - 1); |
Liviu Dudau | 8e22d79 | 2015-04-02 19:48:39 +0100 | [diff] [blame] | 139 | hdlcd_write(hdlcd, HDLCD_REG_POLARITIES, polarities); |
| 140 | |
| 141 | err = hdlcd_set_pxl_fmt(crtc); |
| 142 | if (err) |
| 143 | return; |
| 144 | |
| 145 | clk_set_rate(hdlcd->clk, m->crtc_clock * 1000); |
| 146 | } |
| 147 | |
| 148 | static void hdlcd_crtc_enable(struct drm_crtc *crtc) |
| 149 | { |
| 150 | struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc); |
| 151 | |
| 152 | clk_prepare_enable(hdlcd->clk); |
Liviu Dudau | 96ebb1f | 2016-06-01 15:00:15 +0100 | [diff] [blame] | 153 | hdlcd_crtc_mode_set_nofb(crtc); |
Liviu Dudau | 8e22d79 | 2015-04-02 19:48:39 +0100 | [diff] [blame] | 154 | hdlcd_write(hdlcd, HDLCD_REG_COMMAND, 1); |
Russell King | 7a79279 | 2016-11-22 13:56:54 +0000 | [diff] [blame] | 155 | drm_crtc_vblank_on(crtc); |
Liviu Dudau | 8e22d79 | 2015-04-02 19:48:39 +0100 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static void hdlcd_crtc_disable(struct drm_crtc *crtc) |
| 159 | { |
| 160 | struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc); |
| 161 | |
Russell King | 7a79279 | 2016-11-22 13:56:54 +0000 | [diff] [blame] | 162 | drm_crtc_vblank_off(crtc); |
Liviu Dudau | 8e22d79 | 2015-04-02 19:48:39 +0100 | [diff] [blame] | 163 | hdlcd_write(hdlcd, HDLCD_REG_COMMAND, 0); |
Liviu Dudau | a95acec | 2016-05-17 10:06:54 +0100 | [diff] [blame] | 164 | clk_disable_unprepare(hdlcd->clk); |
Liviu Dudau | 8e22d79 | 2015-04-02 19:48:39 +0100 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | static int hdlcd_crtc_atomic_check(struct drm_crtc *crtc, |
| 168 | struct drm_crtc_state *state) |
| 169 | { |
| 170 | struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc); |
| 171 | struct drm_display_mode *mode = &state->adjusted_mode; |
| 172 | long rate, clk_rate = mode->clock * 1000; |
| 173 | |
| 174 | rate = clk_round_rate(hdlcd->clk, clk_rate); |
| 175 | if (rate != clk_rate) { |
| 176 | /* clock required by mode not supported by hardware */ |
| 177 | return -EINVAL; |
| 178 | } |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | static void hdlcd_crtc_atomic_begin(struct drm_crtc *crtc, |
| 184 | struct drm_crtc_state *state) |
| 185 | { |
Daniel Vetter | 38c8c22c | 2016-05-31 18:21:13 +0200 | [diff] [blame] | 186 | struct drm_pending_vblank_event *event = crtc->state->event; |
Liviu Dudau | 8e22d79 | 2015-04-02 19:48:39 +0100 | [diff] [blame] | 187 | |
Daniel Vetter | 38c8c22c | 2016-05-31 18:21:13 +0200 | [diff] [blame] | 188 | if (event) { |
Liviu Dudau | 8e22d79 | 2015-04-02 19:48:39 +0100 | [diff] [blame] | 189 | crtc->state->event = NULL; |
Liviu Dudau | 8e22d79 | 2015-04-02 19:48:39 +0100 | [diff] [blame] | 190 | |
Daniel Vetter | 38c8c22c | 2016-05-31 18:21:13 +0200 | [diff] [blame] | 191 | spin_lock_irq(&crtc->dev->event_lock); |
| 192 | if (drm_crtc_vblank_get(crtc) == 0) |
| 193 | drm_crtc_arm_vblank_event(crtc, event); |
| 194 | else |
| 195 | drm_crtc_send_vblank_event(crtc, event); |
| 196 | spin_unlock_irq(&crtc->dev->event_lock); |
Liviu Dudau | 8e22d79 | 2015-04-02 19:48:39 +0100 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
Liviu Dudau | 8e22d79 | 2015-04-02 19:48:39 +0100 | [diff] [blame] | 200 | static const struct drm_crtc_helper_funcs hdlcd_crtc_helper_funcs = { |
Liviu Dudau | 8e22d79 | 2015-04-02 19:48:39 +0100 | [diff] [blame] | 201 | .enable = hdlcd_crtc_enable, |
| 202 | .disable = hdlcd_crtc_disable, |
Liviu Dudau | 8e22d79 | 2015-04-02 19:48:39 +0100 | [diff] [blame] | 203 | .atomic_check = hdlcd_crtc_atomic_check, |
| 204 | .atomic_begin = hdlcd_crtc_atomic_begin, |
Liviu Dudau | 8e22d79 | 2015-04-02 19:48:39 +0100 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | static int hdlcd_plane_atomic_check(struct drm_plane *plane, |
| 208 | struct drm_plane_state *state) |
| 209 | { |
Liviu Dudau | 1de3cd4 | 2017-03-08 16:10:19 +0000 | [diff] [blame^] | 210 | struct drm_rect clip = { 0 }; |
| 211 | struct drm_crtc_state *crtc_state; |
| 212 | u32 src_h = state->src_h >> 16; |
Liviu Dudau | 96ebb1f | 2016-06-01 15:00:15 +0100 | [diff] [blame] | 213 | |
Liviu Dudau | 1de3cd4 | 2017-03-08 16:10:19 +0000 | [diff] [blame^] | 214 | /* only the HDLCD_REG_FB_LINE_COUNT register has a limit */ |
| 215 | if (src_h >= HDLCD_MAX_YRES) { |
| 216 | DRM_DEBUG_KMS("Invalid source width: %d\n", src_h); |
Liviu Dudau | 96ebb1f | 2016-06-01 15:00:15 +0100 | [diff] [blame] | 217 | return -EINVAL; |
Liviu Dudau | 1de3cd4 | 2017-03-08 16:10:19 +0000 | [diff] [blame^] | 218 | } |
Liviu Dudau | 96ebb1f | 2016-06-01 15:00:15 +0100 | [diff] [blame] | 219 | |
Liviu Dudau | 1de3cd4 | 2017-03-08 16:10:19 +0000 | [diff] [blame^] | 220 | if (!state->fb || !state->crtc) |
| 221 | return 0; |
| 222 | |
| 223 | crtc_state = drm_atomic_get_existing_crtc_state(state->state, |
| 224 | state->crtc); |
| 225 | if (!crtc_state) { |
| 226 | DRM_DEBUG_KMS("Invalid crtc state\n"); |
| 227 | return -EINVAL; |
| 228 | } |
| 229 | |
| 230 | clip.x2 = crtc_state->adjusted_mode.hdisplay; |
| 231 | clip.y2 = crtc_state->adjusted_mode.vdisplay; |
| 232 | |
| 233 | return drm_plane_helper_check_state(state, &clip, |
| 234 | DRM_PLANE_HELPER_NO_SCALING, |
| 235 | DRM_PLANE_HELPER_NO_SCALING, |
| 236 | false, true); |
Liviu Dudau | 8e22d79 | 2015-04-02 19:48:39 +0100 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | static void hdlcd_plane_atomic_update(struct drm_plane *plane, |
| 240 | struct drm_plane_state *state) |
| 241 | { |
Ville Syrjälä | 59477fa | 2016-11-18 21:52:45 +0200 | [diff] [blame] | 242 | struct drm_framebuffer *fb = plane->state->fb; |
Liviu Dudau | 8e22d79 | 2015-04-02 19:48:39 +0100 | [diff] [blame] | 243 | struct hdlcd_drm_private *hdlcd; |
| 244 | struct drm_gem_cma_object *gem; |
Liviu Dudau | 1de3cd4 | 2017-03-08 16:10:19 +0000 | [diff] [blame^] | 245 | u32 src_x, src_y, dest_h; |
Liviu Dudau | 8e22d79 | 2015-04-02 19:48:39 +0100 | [diff] [blame] | 246 | dma_addr_t scanout_start; |
| 247 | |
Ville Syrjälä | 59477fa | 2016-11-18 21:52:45 +0200 | [diff] [blame] | 248 | if (!fb) |
Liviu Dudau | 8e22d79 | 2015-04-02 19:48:39 +0100 | [diff] [blame] | 249 | return; |
| 250 | |
Liviu Dudau | 1de3cd4 | 2017-03-08 16:10:19 +0000 | [diff] [blame^] | 251 | src_x = plane->state->src.x1 >> 16; |
| 252 | src_y = plane->state->src.y1 >> 16; |
| 253 | dest_h = drm_rect_height(&plane->state->dst); |
Ville Syrjälä | 59477fa | 2016-11-18 21:52:45 +0200 | [diff] [blame] | 254 | gem = drm_fb_cma_get_gem_obj(fb, 0); |
Liviu Dudau | 1de3cd4 | 2017-03-08 16:10:19 +0000 | [diff] [blame^] | 255 | |
Ville Syrjälä | 59477fa | 2016-11-18 21:52:45 +0200 | [diff] [blame] | 256 | scanout_start = gem->paddr + fb->offsets[0] + |
Liviu Dudau | 1de3cd4 | 2017-03-08 16:10:19 +0000 | [diff] [blame^] | 257 | src_y * fb->pitches[0] + |
| 258 | src_x * fb->format->cpp[0]; |
Liviu Dudau | 96ebb1f | 2016-06-01 15:00:15 +0100 | [diff] [blame] | 259 | |
| 260 | hdlcd = plane->dev->dev_private; |
Ville Syrjälä | 59477fa | 2016-11-18 21:52:45 +0200 | [diff] [blame] | 261 | hdlcd_write(hdlcd, HDLCD_REG_FB_LINE_LENGTH, fb->pitches[0]); |
| 262 | hdlcd_write(hdlcd, HDLCD_REG_FB_LINE_PITCH, fb->pitches[0]); |
Liviu Dudau | 96ebb1f | 2016-06-01 15:00:15 +0100 | [diff] [blame] | 263 | hdlcd_write(hdlcd, HDLCD_REG_FB_LINE_COUNT, dest_h - 1); |
Liviu Dudau | 8e22d79 | 2015-04-02 19:48:39 +0100 | [diff] [blame] | 264 | hdlcd_write(hdlcd, HDLCD_REG_FB_BASE, scanout_start); |
| 265 | } |
| 266 | |
| 267 | static const struct drm_plane_helper_funcs hdlcd_plane_helper_funcs = { |
Liviu Dudau | 8e22d79 | 2015-04-02 19:48:39 +0100 | [diff] [blame] | 268 | .atomic_check = hdlcd_plane_atomic_check, |
| 269 | .atomic_update = hdlcd_plane_atomic_update, |
| 270 | }; |
| 271 | |
| 272 | static void hdlcd_plane_destroy(struct drm_plane *plane) |
| 273 | { |
| 274 | drm_plane_helper_disable(plane); |
| 275 | drm_plane_cleanup(plane); |
| 276 | } |
| 277 | |
| 278 | static const struct drm_plane_funcs hdlcd_plane_funcs = { |
| 279 | .update_plane = drm_atomic_helper_update_plane, |
| 280 | .disable_plane = drm_atomic_helper_disable_plane, |
| 281 | .destroy = hdlcd_plane_destroy, |
| 282 | .reset = drm_atomic_helper_plane_reset, |
| 283 | .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state, |
| 284 | .atomic_destroy_state = drm_atomic_helper_plane_destroy_state, |
| 285 | }; |
| 286 | |
| 287 | static struct drm_plane *hdlcd_plane_init(struct drm_device *drm) |
| 288 | { |
| 289 | struct hdlcd_drm_private *hdlcd = drm->dev_private; |
| 290 | struct drm_plane *plane = NULL; |
| 291 | u32 formats[ARRAY_SIZE(supported_formats)], i; |
| 292 | int ret; |
| 293 | |
| 294 | plane = devm_kzalloc(drm->dev, sizeof(*plane), GFP_KERNEL); |
| 295 | if (!plane) |
| 296 | return ERR_PTR(-ENOMEM); |
| 297 | |
| 298 | for (i = 0; i < ARRAY_SIZE(supported_formats); i++) |
| 299 | formats[i] = supported_formats[i].fourcc; |
| 300 | |
| 301 | ret = drm_universal_plane_init(drm, plane, 0xff, &hdlcd_plane_funcs, |
| 302 | formats, ARRAY_SIZE(formats), |
| 303 | DRM_PLANE_TYPE_PRIMARY, NULL); |
| 304 | if (ret) { |
Liviu Dudau | 8e22d79 | 2015-04-02 19:48:39 +0100 | [diff] [blame] | 305 | return ERR_PTR(ret); |
| 306 | } |
| 307 | |
| 308 | drm_plane_helper_add(plane, &hdlcd_plane_helper_funcs); |
| 309 | hdlcd->plane = plane; |
| 310 | |
| 311 | return plane; |
| 312 | } |
| 313 | |
Liviu Dudau | 8e22d79 | 2015-04-02 19:48:39 +0100 | [diff] [blame] | 314 | int hdlcd_setup_crtc(struct drm_device *drm) |
| 315 | { |
| 316 | struct hdlcd_drm_private *hdlcd = drm->dev_private; |
| 317 | struct drm_plane *primary; |
| 318 | int ret; |
| 319 | |
| 320 | primary = hdlcd_plane_init(drm); |
| 321 | if (IS_ERR(primary)) |
| 322 | return PTR_ERR(primary); |
| 323 | |
| 324 | ret = drm_crtc_init_with_planes(drm, &hdlcd->crtc, primary, NULL, |
| 325 | &hdlcd_crtc_funcs, NULL); |
| 326 | if (ret) { |
| 327 | hdlcd_plane_destroy(primary); |
Liviu Dudau | 8e22d79 | 2015-04-02 19:48:39 +0100 | [diff] [blame] | 328 | return ret; |
| 329 | } |
| 330 | |
| 331 | drm_crtc_helper_add(&hdlcd->crtc, &hdlcd_crtc_helper_funcs); |
| 332 | return 0; |
| 333 | } |