1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) STMicroelectronics SA 2014 4 * Authors: Benjamin Gaignard <benjamin.gaignard@st.com> 5 * Fabien Dessenne <fabien.dessenne@st.com> 6 * for STMicroelectronics. 7 */ 8 9 #include <linux/clk.h> 10 11 #include <drm/drmP.h> 12 #include <drm/drm_atomic.h> 13 #include <drm/drm_atomic_helper.h> 14 #include <drm/drm_crtc_helper.h> 15 #include <drm/drm_plane_helper.h> 16 17 #include "sti_compositor.h" 18 #include "sti_crtc.h" 19 #include "sti_drv.h" 20 #include "sti_vid.h" 21 #include "sti_vtg.h" 22 23 static void sti_crtc_atomic_enable(struct drm_crtc *crtc, 24 struct drm_crtc_state *old_state) 25 { 26 struct sti_mixer *mixer = to_sti_mixer(crtc); 27 28 DRM_DEBUG_DRIVER("\n"); 29 30 mixer->status = STI_MIXER_READY; 31 32 drm_crtc_vblank_on(crtc); 33 } 34 35 static void sti_crtc_atomic_disable(struct drm_crtc *crtc, 36 struct drm_crtc_state *old_state) 37 { 38 struct sti_mixer *mixer = to_sti_mixer(crtc); 39 40 DRM_DEBUG_DRIVER("\n"); 41 42 mixer->status = STI_MIXER_DISABLING; 43 44 drm_crtc_wait_one_vblank(crtc); 45 } 46 47 static int 48 sti_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode) 49 { 50 struct sti_mixer *mixer = to_sti_mixer(crtc); 51 struct device *dev = mixer->dev; 52 struct sti_compositor *compo = dev_get_drvdata(dev); 53 struct clk *compo_clk, *pix_clk; 54 int rate = mode->clock * 1000; 55 56 DRM_DEBUG_KMS("CRTC:%d (%s) mode:%d (%s)\n", 57 crtc->base.id, sti_mixer_to_str(mixer), 58 mode->base.id, mode->name); 59 60 DRM_DEBUG_KMS("%d %d %d %d %d %d %d %d %d %d 0x%x 0x%x\n", 61 mode->vrefresh, mode->clock, 62 mode->hdisplay, 63 mode->hsync_start, mode->hsync_end, 64 mode->htotal, 65 mode->vdisplay, 66 mode->vsync_start, mode->vsync_end, 67 mode->vtotal, mode->type, mode->flags); 68 69 if (mixer->id == STI_MIXER_MAIN) { 70 compo_clk = compo->clk_compo_main; 71 pix_clk = compo->clk_pix_main; 72 } else { 73 compo_clk = compo->clk_compo_aux; 74 pix_clk = compo->clk_pix_aux; 75 } 76 77 /* Prepare and enable the compo IP clock */ 78 if (clk_prepare_enable(compo_clk)) { 79 DRM_INFO("Failed to prepare/enable compositor clk\n"); 80 goto compo_error; 81 } 82 83 /* Set rate and prepare/enable pixel clock */ 84 if (clk_set_rate(pix_clk, rate) < 0) { 85 DRM_ERROR("Cannot set rate (%dHz) for pix clk\n", rate); 86 goto pix_error; 87 } 88 if (clk_prepare_enable(pix_clk)) { 89 DRM_ERROR("Failed to prepare/enable pix clk\n"); 90 goto pix_error; 91 } 92 93 sti_vtg_set_config(compo->vtg[mixer->id], &crtc->mode); 94 95 if (sti_mixer_active_video_area(mixer, &crtc->mode)) { 96 DRM_ERROR("Can't set active video area\n"); 97 goto mixer_error; 98 } 99 100 return 0; 101 102 mixer_error: 103 clk_disable_unprepare(pix_clk); 104 pix_error: 105 clk_disable_unprepare(compo_clk); 106 compo_error: 107 return -EINVAL; 108 } 109 110 static void sti_crtc_disable(struct drm_crtc *crtc) 111 { 112 struct sti_mixer *mixer = to_sti_mixer(crtc); 113 struct device *dev = mixer->dev; 114 struct sti_compositor *compo = dev_get_drvdata(dev); 115 116 DRM_DEBUG_KMS("CRTC:%d (%s)\n", crtc->base.id, sti_mixer_to_str(mixer)); 117 118 /* Disable Background */ 119 sti_mixer_set_background_status(mixer, false); 120 121 drm_crtc_vblank_off(crtc); 122 123 /* Disable pixel clock and compo IP clocks */ 124 if (mixer->id == STI_MIXER_MAIN) { 125 clk_disable_unprepare(compo->clk_pix_main); 126 clk_disable_unprepare(compo->clk_compo_main); 127 } else { 128 clk_disable_unprepare(compo->clk_pix_aux); 129 clk_disable_unprepare(compo->clk_compo_aux); 130 } 131 132 mixer->status = STI_MIXER_DISABLED; 133 } 134 135 static void 136 sti_crtc_mode_set_nofb(struct drm_crtc *crtc) 137 { 138 sti_crtc_mode_set(crtc, &crtc->state->adjusted_mode); 139 } 140 141 static void sti_crtc_atomic_flush(struct drm_crtc *crtc, 142 struct drm_crtc_state *old_crtc_state) 143 { 144 struct drm_device *drm_dev = crtc->dev; 145 struct sti_mixer *mixer = to_sti_mixer(crtc); 146 struct sti_compositor *compo = dev_get_drvdata(mixer->dev); 147 struct drm_plane *p; 148 struct drm_pending_vblank_event *event; 149 unsigned long flags; 150 151 DRM_DEBUG_DRIVER("\n"); 152 153 /* perform plane actions */ 154 list_for_each_entry(p, &drm_dev->mode_config.plane_list, head) { 155 struct sti_plane *plane = to_sti_plane(p); 156 157 switch (plane->status) { 158 case STI_PLANE_UPDATED: 159 /* ignore update for other CRTC */ 160 if (p->state->crtc != crtc) 161 continue; 162 163 /* update planes tag as updated */ 164 DRM_DEBUG_DRIVER("update plane %s\n", 165 sti_plane_to_str(plane)); 166 167 if (sti_mixer_set_plane_depth(mixer, plane)) { 168 DRM_ERROR("Cannot set plane %s depth\n", 169 sti_plane_to_str(plane)); 170 break; 171 } 172 173 if (sti_mixer_set_plane_status(mixer, plane, true)) { 174 DRM_ERROR("Cannot enable plane %s at mixer\n", 175 sti_plane_to_str(plane)); 176 break; 177 } 178 179 /* if plane is HQVDP_0 then commit the vid[0] */ 180 if (plane->desc == STI_HQVDP_0) 181 sti_vid_commit(compo->vid[0], p->state); 182 183 plane->status = STI_PLANE_READY; 184 185 break; 186 case STI_PLANE_DISABLING: 187 /* disabling sequence for planes tag as disabling */ 188 DRM_DEBUG_DRIVER("disable plane %s from mixer\n", 189 sti_plane_to_str(plane)); 190 191 if (sti_mixer_set_plane_status(mixer, plane, false)) { 192 DRM_ERROR("Cannot disable plane %s at mixer\n", 193 sti_plane_to_str(plane)); 194 continue; 195 } 196 197 if (plane->desc == STI_CURSOR) 198 /* tag plane status for disabled */ 199 plane->status = STI_PLANE_DISABLED; 200 else 201 /* tag plane status for flushing */ 202 plane->status = STI_PLANE_FLUSHING; 203 204 /* if plane is HQVDP_0 then disable the vid[0] */ 205 if (plane->desc == STI_HQVDP_0) 206 sti_vid_disable(compo->vid[0]); 207 208 break; 209 default: 210 /* Other status case are not handled */ 211 break; 212 } 213 } 214 215 event = crtc->state->event; 216 if (event) { 217 crtc->state->event = NULL; 218 219 spin_lock_irqsave(&crtc->dev->event_lock, flags); 220 if (drm_crtc_vblank_get(crtc) == 0) 221 drm_crtc_arm_vblank_event(crtc, event); 222 else 223 drm_crtc_send_vblank_event(crtc, event); 224 spin_unlock_irqrestore(&crtc->dev->event_lock, flags); 225 } 226 } 227 228 static const struct drm_crtc_helper_funcs sti_crtc_helper_funcs = { 229 .mode_set_nofb = sti_crtc_mode_set_nofb, 230 .atomic_flush = sti_crtc_atomic_flush, 231 .atomic_enable = sti_crtc_atomic_enable, 232 .atomic_disable = sti_crtc_atomic_disable, 233 }; 234 235 static void sti_crtc_destroy(struct drm_crtc *crtc) 236 { 237 DRM_DEBUG_KMS("\n"); 238 drm_crtc_cleanup(crtc); 239 } 240 241 static int sti_crtc_set_property(struct drm_crtc *crtc, 242 struct drm_property *property, 243 uint64_t val) 244 { 245 DRM_DEBUG_KMS("\n"); 246 return 0; 247 } 248 249 int sti_crtc_vblank_cb(struct notifier_block *nb, 250 unsigned long event, void *data) 251 { 252 struct sti_compositor *compo; 253 struct drm_crtc *crtc = data; 254 struct sti_mixer *mixer; 255 unsigned int pipe; 256 257 pipe = drm_crtc_index(crtc); 258 compo = container_of(nb, struct sti_compositor, vtg_vblank_nb[pipe]); 259 mixer = compo->mixer[pipe]; 260 261 if ((event != VTG_TOP_FIELD_EVENT) && 262 (event != VTG_BOTTOM_FIELD_EVENT)) { 263 DRM_ERROR("unknown event: %lu\n", event); 264 return -EINVAL; 265 } 266 267 drm_crtc_handle_vblank(crtc); 268 269 if (mixer->status == STI_MIXER_DISABLING) { 270 struct drm_plane *p; 271 272 /* Disable mixer only if all overlay planes (GDP and VDP) 273 * are disabled */ 274 list_for_each_entry(p, &crtc->dev->mode_config.plane_list, 275 head) { 276 struct sti_plane *plane = to_sti_plane(p); 277 278 if ((plane->desc & STI_PLANE_TYPE_MASK) <= STI_VDP) 279 if (plane->status != STI_PLANE_DISABLED) 280 return 0; 281 } 282 sti_crtc_disable(crtc); 283 } 284 285 return 0; 286 } 287 288 int sti_crtc_enable_vblank(struct drm_device *dev, unsigned int pipe) 289 { 290 struct sti_private *dev_priv = dev->dev_private; 291 struct sti_compositor *compo = dev_priv->compo; 292 struct notifier_block *vtg_vblank_nb = &compo->vtg_vblank_nb[pipe]; 293 struct drm_crtc *crtc = &compo->mixer[pipe]->drm_crtc; 294 struct sti_vtg *vtg = compo->vtg[pipe]; 295 296 DRM_DEBUG_DRIVER("\n"); 297 298 if (sti_vtg_register_client(vtg, vtg_vblank_nb, crtc)) { 299 DRM_ERROR("Cannot register VTG notifier\n"); 300 return -EINVAL; 301 } 302 303 return 0; 304 } 305 306 void sti_crtc_disable_vblank(struct drm_device *drm_dev, unsigned int pipe) 307 { 308 struct sti_private *priv = drm_dev->dev_private; 309 struct sti_compositor *compo = priv->compo; 310 struct notifier_block *vtg_vblank_nb = &compo->vtg_vblank_nb[pipe]; 311 struct sti_vtg *vtg = compo->vtg[pipe]; 312 313 DRM_DEBUG_DRIVER("\n"); 314 315 if (sti_vtg_unregister_client(vtg, vtg_vblank_nb)) 316 DRM_DEBUG_DRIVER("Warning: cannot unregister VTG notifier\n"); 317 } 318 319 static int sti_crtc_late_register(struct drm_crtc *crtc) 320 { 321 struct sti_mixer *mixer = to_sti_mixer(crtc); 322 struct sti_compositor *compo = dev_get_drvdata(mixer->dev); 323 324 if (drm_crtc_index(crtc) == 0) 325 return sti_compositor_debugfs_init(compo, crtc->dev->primary); 326 327 return 0; 328 } 329 330 static const struct drm_crtc_funcs sti_crtc_funcs = { 331 .set_config = drm_atomic_helper_set_config, 332 .page_flip = drm_atomic_helper_page_flip, 333 .destroy = sti_crtc_destroy, 334 .set_property = sti_crtc_set_property, 335 .reset = drm_atomic_helper_crtc_reset, 336 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state, 337 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state, 338 .late_register = sti_crtc_late_register, 339 }; 340 341 bool sti_crtc_is_main(struct drm_crtc *crtc) 342 { 343 struct sti_mixer *mixer = to_sti_mixer(crtc); 344 345 if (mixer->id == STI_MIXER_MAIN) 346 return true; 347 348 return false; 349 } 350 351 int sti_crtc_init(struct drm_device *drm_dev, struct sti_mixer *mixer, 352 struct drm_plane *primary, struct drm_plane *cursor) 353 { 354 struct drm_crtc *crtc = &mixer->drm_crtc; 355 int res; 356 357 res = drm_crtc_init_with_planes(drm_dev, crtc, primary, cursor, 358 &sti_crtc_funcs, NULL); 359 if (res) { 360 DRM_ERROR("Can't initialize CRTC\n"); 361 return -EINVAL; 362 } 363 364 drm_crtc_helper_add(crtc, &sti_crtc_helper_funcs); 365 366 DRM_DEBUG_DRIVER("drm CRTC:%d mapped to %s\n", 367 crtc->base.id, sti_mixer_to_str(mixer)); 368 369 return 0; 370 } 371