1 /* 2 * Copyright (C) 2014 Traphandler 3 * Copyright (C) 2014 Free Electrons 4 * Copyright (C) 2014 Atmel 5 * 6 * Author: Jean-Jacques Hiblot <jjhiblot@traphandler.com> 7 * Author: Boris BREZILLON <boris.brezillon@free-electrons.com> 8 * 9 * This program is free software; you can redistribute it and/or modify it 10 * under the terms of the GNU General Public License version 2 as published by 11 * the Free Software Foundation. 12 * 13 * This program is distributed in the hope that it will be useful, but WITHOUT 14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 16 * more details. 17 * 18 * You should have received a copy of the GNU General Public License along with 19 * this program. If not, see <http://www.gnu.org/licenses/>. 20 */ 21 22 #ifndef DRM_ATMEL_HLCDC_H 23 #define DRM_ATMEL_HLCDC_H 24 25 #include <linux/clk.h> 26 #include <linux/dmapool.h> 27 #include <linux/irqdomain.h> 28 #include <linux/mfd/atmel-hlcdc.h> 29 #include <linux/pwm.h> 30 31 #include <drm/drm_atomic.h> 32 #include <drm/drm_atomic_helper.h> 33 #include <drm/drm_crtc.h> 34 #include <drm/drm_crtc_helper.h> 35 #include <drm/drm_fb_cma_helper.h> 36 #include <drm/drm_gem_cma_helper.h> 37 #include <drm/drm_gem_framebuffer_helper.h> 38 #include <drm/drm_panel.h> 39 #include <drm/drm_plane_helper.h> 40 #include <drm/drmP.h> 41 42 #define ATMEL_HLCDC_LAYER_CHER 0x0 43 #define ATMEL_HLCDC_LAYER_CHDR 0x4 44 #define ATMEL_HLCDC_LAYER_CHSR 0x8 45 #define ATMEL_HLCDC_LAYER_EN BIT(0) 46 #define ATMEL_HLCDC_LAYER_UPDATE BIT(1) 47 #define ATMEL_HLCDC_LAYER_A2Q BIT(2) 48 #define ATMEL_HLCDC_LAYER_RST BIT(8) 49 50 #define ATMEL_HLCDC_LAYER_IER 0xc 51 #define ATMEL_HLCDC_LAYER_IDR 0x10 52 #define ATMEL_HLCDC_LAYER_IMR 0x14 53 #define ATMEL_HLCDC_LAYER_ISR 0x18 54 #define ATMEL_HLCDC_LAYER_DFETCH BIT(0) 55 #define ATMEL_HLCDC_LAYER_LFETCH BIT(1) 56 #define ATMEL_HLCDC_LAYER_DMA_IRQ(p) BIT(2 + (8 * (p))) 57 #define ATMEL_HLCDC_LAYER_DSCR_IRQ(p) BIT(3 + (8 * (p))) 58 #define ATMEL_HLCDC_LAYER_ADD_IRQ(p) BIT(4 + (8 * (p))) 59 #define ATMEL_HLCDC_LAYER_DONE_IRQ(p) BIT(5 + (8 * (p))) 60 #define ATMEL_HLCDC_LAYER_OVR_IRQ(p) BIT(6 + (8 * (p))) 61 62 #define ATMEL_HLCDC_LAYER_PLANE_HEAD(p) (((p) * 0x10) + 0x1c) 63 #define ATMEL_HLCDC_LAYER_PLANE_ADDR(p) (((p) * 0x10) + 0x20) 64 #define ATMEL_HLCDC_LAYER_PLANE_CTRL(p) (((p) * 0x10) + 0x24) 65 #define ATMEL_HLCDC_LAYER_PLANE_NEXT(p) (((p) * 0x10) + 0x28) 66 67 #define ATMEL_HLCDC_LAYER_DMA_CFG 0 68 #define ATMEL_HLCDC_LAYER_DMA_SIF BIT(0) 69 #define ATMEL_HLCDC_LAYER_DMA_BLEN_MASK GENMASK(5, 4) 70 #define ATMEL_HLCDC_LAYER_DMA_BLEN_SINGLE (0 << 4) 71 #define ATMEL_HLCDC_LAYER_DMA_BLEN_INCR4 (1 << 4) 72 #define ATMEL_HLCDC_LAYER_DMA_BLEN_INCR8 (2 << 4) 73 #define ATMEL_HLCDC_LAYER_DMA_BLEN_INCR16 (3 << 4) 74 #define ATMEL_HLCDC_LAYER_DMA_DLBO BIT(8) 75 #define ATMEL_HLCDC_LAYER_DMA_ROTDIS BIT(12) 76 #define ATMEL_HLCDC_LAYER_DMA_LOCKDIS BIT(13) 77 78 #define ATMEL_HLCDC_LAYER_FORMAT_CFG 1 79 #define ATMEL_HLCDC_LAYER_RGB (0 << 0) 80 #define ATMEL_HLCDC_LAYER_CLUT (1 << 0) 81 #define ATMEL_HLCDC_LAYER_YUV (2 << 0) 82 #define ATMEL_HLCDC_RGB_MODE(m) \ 83 (ATMEL_HLCDC_LAYER_RGB | (((m) & 0xf) << 4)) 84 #define ATMEL_HLCDC_CLUT_MODE(m) \ 85 (ATMEL_HLCDC_LAYER_CLUT | (((m) & 0x3) << 8)) 86 #define ATMEL_HLCDC_YUV_MODE(m) \ 87 (ATMEL_HLCDC_LAYER_YUV | (((m) & 0xf) << 12)) 88 #define ATMEL_HLCDC_YUV422ROT BIT(16) 89 #define ATMEL_HLCDC_YUV422SWP BIT(17) 90 #define ATMEL_HLCDC_DSCALEOPT BIT(20) 91 92 #define ATMEL_HLCDC_C1_MODE ATMEL_HLCDC_CLUT_MODE(0) 93 #define ATMEL_HLCDC_C2_MODE ATMEL_HLCDC_CLUT_MODE(1) 94 #define ATMEL_HLCDC_C4_MODE ATMEL_HLCDC_CLUT_MODE(2) 95 #define ATMEL_HLCDC_C8_MODE ATMEL_HLCDC_CLUT_MODE(3) 96 97 #define ATMEL_HLCDC_XRGB4444_MODE ATMEL_HLCDC_RGB_MODE(0) 98 #define ATMEL_HLCDC_ARGB4444_MODE ATMEL_HLCDC_RGB_MODE(1) 99 #define ATMEL_HLCDC_RGBA4444_MODE ATMEL_HLCDC_RGB_MODE(2) 100 #define ATMEL_HLCDC_RGB565_MODE ATMEL_HLCDC_RGB_MODE(3) 101 #define ATMEL_HLCDC_ARGB1555_MODE ATMEL_HLCDC_RGB_MODE(4) 102 #define ATMEL_HLCDC_XRGB8888_MODE ATMEL_HLCDC_RGB_MODE(9) 103 #define ATMEL_HLCDC_RGB888_MODE ATMEL_HLCDC_RGB_MODE(10) 104 #define ATMEL_HLCDC_ARGB8888_MODE ATMEL_HLCDC_RGB_MODE(12) 105 #define ATMEL_HLCDC_RGBA8888_MODE ATMEL_HLCDC_RGB_MODE(13) 106 107 #define ATMEL_HLCDC_AYUV_MODE ATMEL_HLCDC_YUV_MODE(0) 108 #define ATMEL_HLCDC_YUYV_MODE ATMEL_HLCDC_YUV_MODE(1) 109 #define ATMEL_HLCDC_UYVY_MODE ATMEL_HLCDC_YUV_MODE(2) 110 #define ATMEL_HLCDC_YVYU_MODE ATMEL_HLCDC_YUV_MODE(3) 111 #define ATMEL_HLCDC_VYUY_MODE ATMEL_HLCDC_YUV_MODE(4) 112 #define ATMEL_HLCDC_NV61_MODE ATMEL_HLCDC_YUV_MODE(5) 113 #define ATMEL_HLCDC_YUV422_MODE ATMEL_HLCDC_YUV_MODE(6) 114 #define ATMEL_HLCDC_NV21_MODE ATMEL_HLCDC_YUV_MODE(7) 115 #define ATMEL_HLCDC_YUV420_MODE ATMEL_HLCDC_YUV_MODE(8) 116 117 #define ATMEL_HLCDC_LAYER_POS(x, y) ((x) | ((y) << 16)) 118 #define ATMEL_HLCDC_LAYER_SIZE(w, h) (((w) - 1) | (((h) - 1) << 16)) 119 120 #define ATMEL_HLCDC_LAYER_CRKEY BIT(0) 121 #define ATMEL_HLCDC_LAYER_INV BIT(1) 122 #define ATMEL_HLCDC_LAYER_ITER2BL BIT(2) 123 #define ATMEL_HLCDC_LAYER_ITER BIT(3) 124 #define ATMEL_HLCDC_LAYER_REVALPHA BIT(4) 125 #define ATMEL_HLCDC_LAYER_GAEN BIT(5) 126 #define ATMEL_HLCDC_LAYER_LAEN BIT(6) 127 #define ATMEL_HLCDC_LAYER_OVR BIT(7) 128 #define ATMEL_HLCDC_LAYER_DMA BIT(8) 129 #define ATMEL_HLCDC_LAYER_REP BIT(9) 130 #define ATMEL_HLCDC_LAYER_DSTKEY BIT(10) 131 #define ATMEL_HLCDC_LAYER_DISCEN BIT(11) 132 #define ATMEL_HLCDC_LAYER_GA_SHIFT 16 133 #define ATMEL_HLCDC_LAYER_GA_MASK \ 134 GENMASK(23, ATMEL_HLCDC_LAYER_GA_SHIFT) 135 #define ATMEL_HLCDC_LAYER_GA(x) \ 136 ((x) << ATMEL_HLCDC_LAYER_GA_SHIFT) 137 138 #define ATMEL_HLCDC_LAYER_DISC_POS(x, y) ((x) | ((y) << 16)) 139 #define ATMEL_HLCDC_LAYER_DISC_SIZE(w, h) (((w) - 1) | (((h) - 1) << 16)) 140 141 #define ATMEL_HLCDC_LAYER_SCALER_FACTORS(x, y) ((x) | ((y) << 16)) 142 #define ATMEL_HLCDC_LAYER_SCALER_ENABLE BIT(31) 143 144 #define ATMEL_HLCDC_LAYER_MAX_PLANES 3 145 146 #define ATMEL_HLCDC_DMA_CHANNEL_DSCR_RESERVED BIT(0) 147 #define ATMEL_HLCDC_DMA_CHANNEL_DSCR_LOADED BIT(1) 148 #define ATMEL_HLCDC_DMA_CHANNEL_DSCR_DONE BIT(2) 149 #define ATMEL_HLCDC_DMA_CHANNEL_DSCR_OVERRUN BIT(3) 150 151 #define ATMEL_HLCDC_CLUT_SIZE 256 152 153 #define ATMEL_HLCDC_MAX_LAYERS 6 154 155 /** 156 * Atmel HLCDC Layer registers layout structure 157 * 158 * Each HLCDC layer has its own register organization and a given register 159 * can be placed differently on 2 different layers depending on its 160 * capabilities. 161 * This structure stores common registers layout for a given layer and is 162 * used by HLCDC layer code to choose the appropriate register to write to 163 * or to read from. 164 * 165 * For all fields, a value of zero means "unsupported". 166 * 167 * See Atmel's datasheet for a detailled description of these registers. 168 * 169 * @xstride: xstride registers 170 * @pstride: pstride registers 171 * @pos: position register 172 * @size: displayed size register 173 * @memsize: memory size register 174 * @default_color: default color register 175 * @chroma_key: chroma key register 176 * @chroma_key_mask: chroma key mask register 177 * @general_config: general layer config register 178 * @sacler_config: scaler factors register 179 * @phicoeffs: X/Y PHI coefficient registers 180 * @disc_pos: discard area position register 181 * @disc_size: discard area size register 182 * @csc: color space conversion register 183 */ 184 struct atmel_hlcdc_layer_cfg_layout { 185 int xstride[ATMEL_HLCDC_LAYER_MAX_PLANES]; 186 int pstride[ATMEL_HLCDC_LAYER_MAX_PLANES]; 187 int pos; 188 int size; 189 int memsize; 190 int default_color; 191 int chroma_key; 192 int chroma_key_mask; 193 int general_config; 194 int scaler_config; 195 struct { 196 int x; 197 int y; 198 } phicoeffs; 199 int disc_pos; 200 int disc_size; 201 int csc; 202 }; 203 204 /** 205 * Atmel HLCDC DMA descriptor structure 206 * 207 * This structure is used by the HLCDC DMA engine to schedule a DMA transfer. 208 * 209 * The structure fields must remain in this specific order, because they're 210 * used by the HLCDC DMA engine, which expect them in this order. 211 * HLCDC DMA descriptors must be aligned on 64 bits. 212 * 213 * @addr: buffer DMA address 214 * @ctrl: DMA transfer options 215 * @next: next DMA descriptor to fetch 216 * @self: descriptor DMA address 217 */ 218 struct atmel_hlcdc_dma_channel_dscr { 219 dma_addr_t addr; 220 u32 ctrl; 221 dma_addr_t next; 222 dma_addr_t self; 223 } __aligned(sizeof(u64)); 224 225 /** 226 * Atmel HLCDC layer types 227 */ 228 enum atmel_hlcdc_layer_type { 229 ATMEL_HLCDC_NO_LAYER, 230 ATMEL_HLCDC_BASE_LAYER, 231 ATMEL_HLCDC_OVERLAY_LAYER, 232 ATMEL_HLCDC_CURSOR_LAYER, 233 ATMEL_HLCDC_PP_LAYER, 234 }; 235 236 /** 237 * Atmel HLCDC Supported formats structure 238 * 239 * This structure list all the formats supported by a given layer. 240 * 241 * @nformats: number of supported formats 242 * @formats: supported formats 243 */ 244 struct atmel_hlcdc_formats { 245 int nformats; 246 u32 *formats; 247 }; 248 249 /** 250 * Atmel HLCDC Layer description structure 251 * 252 * This structure describes the capabilities provided by a given layer. 253 * 254 * @name: layer name 255 * @type: layer type 256 * @id: layer id 257 * @regs_offset: offset of the layer registers from the HLCDC registers base 258 * @cfgs_offset: CFGX registers offset from the layer registers base 259 * @formats: supported formats 260 * @layout: config registers layout 261 * @max_width: maximum width supported by this layer (0 means unlimited) 262 * @max_height: maximum height supported by this layer (0 means unlimited) 263 */ 264 struct atmel_hlcdc_layer_desc { 265 const char *name; 266 enum atmel_hlcdc_layer_type type; 267 int id; 268 int regs_offset; 269 int cfgs_offset; 270 int clut_offset; 271 struct atmel_hlcdc_formats *formats; 272 struct atmel_hlcdc_layer_cfg_layout layout; 273 int max_width; 274 int max_height; 275 }; 276 277 /** 278 * Atmel HLCDC Layer. 279 * 280 * A layer can be a DRM plane of a post processing layer used to render 281 * HLCDC composition into memory. 282 * 283 * @desc: layer description 284 * @regmap: pointer to the HLCDC regmap 285 */ 286 struct atmel_hlcdc_layer { 287 const struct atmel_hlcdc_layer_desc *desc; 288 struct regmap *regmap; 289 }; 290 291 /** 292 * Atmel HLCDC Plane. 293 * 294 * @base: base DRM plane structure 295 * @layer: HLCDC layer structure 296 * @properties: pointer to the property definitions structure 297 */ 298 struct atmel_hlcdc_plane { 299 struct drm_plane base; 300 struct atmel_hlcdc_layer layer; 301 struct atmel_hlcdc_plane_properties *properties; 302 }; 303 304 static inline struct atmel_hlcdc_plane * 305 drm_plane_to_atmel_hlcdc_plane(struct drm_plane *p) 306 { 307 return container_of(p, struct atmel_hlcdc_plane, base); 308 } 309 310 static inline struct atmel_hlcdc_plane * 311 atmel_hlcdc_layer_to_plane(struct atmel_hlcdc_layer *layer) 312 { 313 return container_of(layer, struct atmel_hlcdc_plane, layer); 314 } 315 316 /** 317 * Atmel HLCDC Display Controller description structure. 318 * 319 * This structure describes the HLCDC IP capabilities and depends on the 320 * HLCDC IP version (or Atmel SoC family). 321 * 322 * @min_width: minimum width supported by the Display Controller 323 * @min_height: minimum height supported by the Display Controller 324 * @max_width: maximum width supported by the Display Controller 325 * @max_height: maximum height supported by the Display Controller 326 * @max_spw: maximum vertical/horizontal pulse width 327 * @max_vpw: maximum vertical back/front porch width 328 * @max_hpw: maximum horizontal back/front porch width 329 * @conflicting_output_formats: true if RGBXXX output formats conflict with 330 * each other. 331 * @layers: a layer description table describing available layers 332 * @nlayers: layer description table size 333 */ 334 struct atmel_hlcdc_dc_desc { 335 int min_width; 336 int min_height; 337 int max_width; 338 int max_height; 339 int max_spw; 340 int max_vpw; 341 int max_hpw; 342 bool conflicting_output_formats; 343 const struct atmel_hlcdc_layer_desc *layers; 344 int nlayers; 345 }; 346 347 /** 348 * Atmel HLCDC Plane properties. 349 * 350 * This structure stores plane property definitions. 351 * 352 * @alpha: alpha blending (or transparency) property 353 * @rotation: rotation property 354 */ 355 struct atmel_hlcdc_plane_properties { 356 struct drm_property *alpha; 357 }; 358 359 /** 360 * Atmel HLCDC Display Controller. 361 * 362 * @desc: HLCDC Display Controller description 363 * @dscrpool: DMA coherent pool used to allocate DMA descriptors 364 * @hlcdc: pointer to the atmel_hlcdc structure provided by the MFD device 365 * @fbdev: framebuffer device attached to the Display Controller 366 * @crtc: CRTC provided by the display controller 367 * @planes: instantiated planes 368 * @layers: active HLCDC layers 369 * @wq: display controller workqueue 370 * @suspend: used to store the HLCDC state when entering suspend 371 * @commit: used for async commit handling 372 */ 373 struct atmel_hlcdc_dc { 374 const struct atmel_hlcdc_dc_desc *desc; 375 struct dma_pool *dscrpool; 376 struct atmel_hlcdc *hlcdc; 377 struct drm_fbdev_cma *fbdev; 378 struct drm_crtc *crtc; 379 struct atmel_hlcdc_layer *layers[ATMEL_HLCDC_MAX_LAYERS]; 380 struct workqueue_struct *wq; 381 struct { 382 u32 imr; 383 struct drm_atomic_state *state; 384 } suspend; 385 struct { 386 wait_queue_head_t wait; 387 bool pending; 388 } commit; 389 }; 390 391 extern struct atmel_hlcdc_formats atmel_hlcdc_plane_rgb_formats; 392 extern struct atmel_hlcdc_formats atmel_hlcdc_plane_rgb_and_yuv_formats; 393 394 static inline void atmel_hlcdc_layer_write_reg(struct atmel_hlcdc_layer *layer, 395 unsigned int reg, u32 val) 396 { 397 regmap_write(layer->regmap, layer->desc->regs_offset + reg, val); 398 } 399 400 static inline u32 atmel_hlcdc_layer_read_reg(struct atmel_hlcdc_layer *layer, 401 unsigned int reg) 402 { 403 u32 val; 404 405 regmap_read(layer->regmap, layer->desc->regs_offset + reg, &val); 406 407 return val; 408 } 409 410 static inline void atmel_hlcdc_layer_write_cfg(struct atmel_hlcdc_layer *layer, 411 unsigned int cfgid, u32 val) 412 { 413 atmel_hlcdc_layer_write_reg(layer, 414 layer->desc->cfgs_offset + 415 (cfgid * sizeof(u32)), val); 416 } 417 418 static inline u32 atmel_hlcdc_layer_read_cfg(struct atmel_hlcdc_layer *layer, 419 unsigned int cfgid) 420 { 421 return atmel_hlcdc_layer_read_reg(layer, 422 layer->desc->cfgs_offset + 423 (cfgid * sizeof(u32))); 424 } 425 426 static inline void atmel_hlcdc_layer_write_clut(struct atmel_hlcdc_layer *layer, 427 unsigned int c, u32 val) 428 { 429 regmap_write(layer->regmap, 430 layer->desc->clut_offset + c * sizeof(u32), 431 val); 432 } 433 434 static inline void atmel_hlcdc_layer_init(struct atmel_hlcdc_layer *layer, 435 const struct atmel_hlcdc_layer_desc *desc, 436 struct regmap *regmap) 437 { 438 layer->desc = desc; 439 layer->regmap = regmap; 440 } 441 442 enum drm_mode_status 443 atmel_hlcdc_dc_mode_valid(struct atmel_hlcdc_dc *dc, 444 const struct drm_display_mode *mode); 445 446 int atmel_hlcdc_create_planes(struct drm_device *dev); 447 void atmel_hlcdc_plane_irq(struct atmel_hlcdc_plane *plane); 448 449 int atmel_hlcdc_plane_prepare_disc_area(struct drm_crtc_state *c_state); 450 int atmel_hlcdc_plane_prepare_ahb_routing(struct drm_crtc_state *c_state); 451 452 void atmel_hlcdc_crtc_irq(struct drm_crtc *c); 453 454 int atmel_hlcdc_crtc_create(struct drm_device *dev); 455 456 int atmel_hlcdc_create_outputs(struct drm_device *dev); 457 458 #endif /* DRM_ATMEL_HLCDC_H */ 459