1
2
3
4
5
6
7
8
9#include <linux/clk.h>
10#include <linux/component.h>
11#include <linux/of_device.h>
12#include <linux/platform_device.h>
13#include <drm/drmP.h>
14#include <drm/drm_crtc_helper.h>
15#include <drm/drm_plane_helper.h>
16#include "armada_crtc.h"
17#include "armada_drm.h"
18#include "armada_fb.h"
19#include "armada_gem.h"
20#include "armada_hw.h"
21
22struct armada_frame_work {
23 struct drm_pending_vblank_event *event;
24 struct armada_regs regs[4];
25 struct drm_framebuffer *old_fb;
26};
27
28enum csc_mode {
29 CSC_AUTO = 0,
30 CSC_YUV_CCIR601 = 1,
31 CSC_YUV_CCIR709 = 2,
32 CSC_RGB_COMPUTER = 1,
33 CSC_RGB_STUDIO = 2,
34};
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90void
91armada_drm_crtc_update_regs(struct armada_crtc *dcrtc, struct armada_regs *regs)
92{
93 while (regs->offset != ~0) {
94 void __iomem *reg = dcrtc->base + regs->offset;
95 uint32_t val;
96
97 val = regs->mask;
98 if (val != 0)
99 val &= readl_relaxed(reg);
100 writel_relaxed(val | regs->val, reg);
101 ++regs;
102 }
103}
104
105#define dpms_blanked(dpms) ((dpms) != DRM_MODE_DPMS_ON)
106
107static void armada_drm_crtc_update(struct armada_crtc *dcrtc)
108{
109 uint32_t dumb_ctrl;
110
111 dumb_ctrl = dcrtc->cfg_dumb_ctrl;
112
113 if (!dpms_blanked(dcrtc->dpms))
114 dumb_ctrl |= CFG_DUMB_ENA;
115
116
117
118
119
120
121
122 if (dpms_blanked(dcrtc->dpms) &&
123 (dumb_ctrl & DUMB_MASK) == DUMB24_RGB888_0) {
124 dumb_ctrl &= ~DUMB_MASK;
125 dumb_ctrl |= DUMB_BLANK;
126 }
127
128
129
130
131
132
133
134
135
136 if (dcrtc->crtc.mode.flags & DRM_MODE_FLAG_NCSYNC)
137 dumb_ctrl |= CFG_INV_CSYNC;
138 if (dcrtc->crtc.mode.flags & DRM_MODE_FLAG_NHSYNC)
139 dumb_ctrl |= CFG_INV_HSYNC;
140 if (dcrtc->crtc.mode.flags & DRM_MODE_FLAG_NVSYNC)
141 dumb_ctrl |= CFG_INV_VSYNC;
142
143 if (dcrtc->dumb_ctrl != dumb_ctrl) {
144 dcrtc->dumb_ctrl = dumb_ctrl;
145 writel_relaxed(dumb_ctrl, dcrtc->base + LCD_SPU_DUMB_CTRL);
146 }
147}
148
149static unsigned armada_drm_crtc_calc_fb(struct drm_framebuffer *fb,
150 int x, int y, struct armada_regs *regs, bool interlaced)
151{
152 struct armada_gem_object *obj = drm_fb_obj(fb);
153 unsigned pitch = fb->pitches[0];
154 unsigned offset = y * pitch + x * fb->bits_per_pixel / 8;
155 uint32_t addr_odd, addr_even;
156 unsigned i = 0;
157
158 DRM_DEBUG_DRIVER("pitch %u x %d y %d bpp %d\n",
159 pitch, x, y, fb->bits_per_pixel);
160
161 addr_odd = addr_even = obj->dev_addr + offset;
162
163 if (interlaced) {
164 addr_even += pitch;
165 pitch *= 2;
166 }
167
168
169 armada_reg_queue_set(regs, i, addr_odd, LCD_CFG_GRA_START_ADDR0);
170 armada_reg_queue_set(regs, i, addr_even, LCD_CFG_GRA_START_ADDR1);
171 armada_reg_queue_mod(regs, i, pitch, 0xffff, LCD_CFG_GRA_PITCH);
172
173 return i;
174}
175
176static int armada_drm_crtc_queue_frame_work(struct armada_crtc *dcrtc,
177 struct armada_frame_work *work)
178{
179 struct drm_device *dev = dcrtc->crtc.dev;
180 unsigned long flags;
181 int ret;
182
183 ret = drm_vblank_get(dev, dcrtc->num);
184 if (ret) {
185 DRM_ERROR("failed to acquire vblank counter\n");
186 return ret;
187 }
188
189 spin_lock_irqsave(&dev->event_lock, flags);
190 if (!dcrtc->frame_work)
191 dcrtc->frame_work = work;
192 else
193 ret = -EBUSY;
194 spin_unlock_irqrestore(&dev->event_lock, flags);
195
196 if (ret)
197 drm_vblank_put(dev, dcrtc->num);
198
199 return ret;
200}
201
202static void armada_drm_crtc_complete_frame_work(struct armada_crtc *dcrtc)
203{
204 struct drm_device *dev = dcrtc->crtc.dev;
205 struct armada_frame_work *work = dcrtc->frame_work;
206
207 dcrtc->frame_work = NULL;
208
209 armada_drm_crtc_update_regs(dcrtc, work->regs);
210
211 if (work->event)
212 drm_send_vblank_event(dev, dcrtc->num, work->event);
213
214 drm_vblank_put(dev, dcrtc->num);
215
216
217 __armada_drm_queue_unref_work(dcrtc->crtc.dev, work->old_fb);
218 kfree(work);
219}
220
221static void armada_drm_crtc_finish_fb(struct armada_crtc *dcrtc,
222 struct drm_framebuffer *fb, bool force)
223{
224 struct armada_frame_work *work;
225
226 if (!fb)
227 return;
228
229 if (force) {
230
231 drm_framebuffer_unreference(fb);
232 return;
233 }
234
235 work = kmalloc(sizeof(*work), GFP_KERNEL);
236 if (work) {
237 int i = 0;
238 work->event = NULL;
239 work->old_fb = fb;
240 armada_reg_queue_end(work->regs, i);
241
242 if (armada_drm_crtc_queue_frame_work(dcrtc, work) == 0)
243 return;
244
245 kfree(work);
246 }
247
248
249
250
251
252
253 drm_framebuffer_unreference(fb);
254}
255
256static void armada_drm_vblank_off(struct armada_crtc *dcrtc)
257{
258 struct drm_device *dev = dcrtc->crtc.dev;
259
260
261
262
263
264 drm_crtc_vblank_off(&dcrtc->crtc);
265
266
267 spin_lock_irq(&dev->event_lock);
268 if (dcrtc->frame_work)
269 armada_drm_crtc_complete_frame_work(dcrtc);
270 spin_unlock_irq(&dev->event_lock);
271}
272
273void armada_drm_crtc_gamma_set(struct drm_crtc *crtc, u16 r, u16 g, u16 b,
274 int idx)
275{
276}
277
278void armada_drm_crtc_gamma_get(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
279 int idx)
280{
281}
282
283
284static void armada_drm_crtc_dpms(struct drm_crtc *crtc, int dpms)
285{
286 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
287
288 if (dcrtc->dpms != dpms) {
289 dcrtc->dpms = dpms;
290 armada_drm_crtc_update(dcrtc);
291 if (dpms_blanked(dpms))
292 armada_drm_vblank_off(dcrtc);
293 else
294 drm_crtc_vblank_on(&dcrtc->crtc);
295 }
296}
297
298
299
300
301
302
303
304
305static void armada_drm_crtc_prepare(struct drm_crtc *crtc)
306{
307 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
308 struct drm_plane *plane;
309
310
311
312
313
314
315 plane = dcrtc->plane;
316 if (plane) {
317 struct drm_framebuffer *fb = plane->fb;
318
319 plane->funcs->disable_plane(plane);
320 plane->fb = NULL;
321 plane->crtc = NULL;
322 drm_framebuffer_unreference(fb);
323 }
324}
325
326
327static void armada_drm_crtc_commit(struct drm_crtc *crtc)
328{
329 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
330
331 if (dcrtc->dpms != DRM_MODE_DPMS_ON) {
332 dcrtc->dpms = DRM_MODE_DPMS_ON;
333 armada_drm_crtc_update(dcrtc);
334 }
335}
336
337
338static bool armada_drm_crtc_mode_fixup(struct drm_crtc *crtc,
339 const struct drm_display_mode *mode, struct drm_display_mode *adj)
340{
341 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
342 int ret;
343
344
345 if (!dcrtc->variant->has_spu_adv_reg &&
346 adj->flags & DRM_MODE_FLAG_INTERLACE)
347 return false;
348
349
350 ret = dcrtc->variant->compute_clock(dcrtc, adj, NULL);
351 if (ret)
352 return false;
353
354 return true;
355}
356
357static void armada_drm_crtc_irq(struct armada_crtc *dcrtc, u32 stat)
358{
359 struct armada_vbl_event *e, *n;
360 void __iomem *base = dcrtc->base;
361
362 if (stat & DMA_FF_UNDERFLOW)
363 DRM_ERROR("video underflow on crtc %u\n", dcrtc->num);
364 if (stat & GRA_FF_UNDERFLOW)
365 DRM_ERROR("graphics underflow on crtc %u\n", dcrtc->num);
366
367 if (stat & VSYNC_IRQ)
368 drm_handle_vblank(dcrtc->crtc.dev, dcrtc->num);
369
370 spin_lock(&dcrtc->irq_lock);
371
372 list_for_each_entry_safe(e, n, &dcrtc->vbl_list, node) {
373 list_del_init(&e->node);
374 drm_vblank_put(dcrtc->crtc.dev, dcrtc->num);
375 e->fn(dcrtc, e->data);
376 }
377
378 if (stat & GRA_FRAME_IRQ && dcrtc->interlaced) {
379 int i = stat & GRA_FRAME_IRQ0 ? 0 : 1;
380 uint32_t val;
381
382 writel_relaxed(dcrtc->v[i].spu_v_porch, base + LCD_SPU_V_PORCH);
383 writel_relaxed(dcrtc->v[i].spu_v_h_total,
384 base + LCD_SPUT_V_H_TOTAL);
385
386 val = readl_relaxed(base + LCD_SPU_ADV_REG);
387 val &= ~(ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF | ADV_VSYNCOFFEN);
388 val |= dcrtc->v[i].spu_adv_reg;
389 writel_relaxed(val, base + LCD_SPU_ADV_REG);
390 }
391
392 if (stat & DUMB_FRAMEDONE && dcrtc->cursor_update) {
393 writel_relaxed(dcrtc->cursor_hw_pos,
394 base + LCD_SPU_HWC_OVSA_HPXL_VLN);
395 writel_relaxed(dcrtc->cursor_hw_sz,
396 base + LCD_SPU_HWC_HPXL_VLN);
397 armada_updatel(CFG_HWC_ENA,
398 CFG_HWC_ENA | CFG_HWC_1BITMOD | CFG_HWC_1BITENA,
399 base + LCD_SPU_DMA_CTRL0);
400 dcrtc->cursor_update = false;
401 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
402 }
403
404 spin_unlock(&dcrtc->irq_lock);
405
406 if (stat & GRA_FRAME_IRQ) {
407 struct drm_device *dev = dcrtc->crtc.dev;
408
409 spin_lock(&dev->event_lock);
410 if (dcrtc->frame_work)
411 armada_drm_crtc_complete_frame_work(dcrtc);
412 spin_unlock(&dev->event_lock);
413
414 wake_up(&dcrtc->frame_wait);
415 }
416}
417
418static irqreturn_t armada_drm_irq(int irq, void *arg)
419{
420 struct armada_crtc *dcrtc = arg;
421 u32 v, stat = readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR);
422
423
424
425
426
427 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
428
429
430 v = stat & dcrtc->irq_ena;
431
432 if (v & (VSYNC_IRQ|GRA_FRAME_IRQ|DUMB_FRAMEDONE)) {
433 armada_drm_crtc_irq(dcrtc, stat);
434 return IRQ_HANDLED;
435 }
436 return IRQ_NONE;
437}
438
439
440void armada_drm_crtc_disable_irq(struct armada_crtc *dcrtc, u32 mask)
441{
442 if (dcrtc->irq_ena & mask) {
443 dcrtc->irq_ena &= ~mask;
444 writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
445 }
446}
447
448void armada_drm_crtc_enable_irq(struct armada_crtc *dcrtc, u32 mask)
449{
450 if ((dcrtc->irq_ena & mask) != mask) {
451 dcrtc->irq_ena |= mask;
452 writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
453 if (readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR) & mask)
454 writel(0, dcrtc->base + LCD_SPU_IRQ_ISR);
455 }
456}
457
458static uint32_t armada_drm_crtc_calculate_csc(struct armada_crtc *dcrtc)
459{
460 struct drm_display_mode *adj = &dcrtc->crtc.mode;
461 uint32_t val = 0;
462
463 if (dcrtc->csc_yuv_mode == CSC_YUV_CCIR709)
464 val |= CFG_CSC_YUV_CCIR709;
465 if (dcrtc->csc_rgb_mode == CSC_RGB_STUDIO)
466 val |= CFG_CSC_RGB_STUDIO;
467
468
469
470
471
472
473
474
475 if ((adj->hdisplay == 1280 && adj->vdisplay == 720 &&
476 !(adj->flags & DRM_MODE_FLAG_INTERLACE)) ||
477 (adj->hdisplay == 1920 && adj->vdisplay == 1080)) {
478 if (dcrtc->csc_yuv_mode == CSC_AUTO)
479 val |= CFG_CSC_YUV_CCIR709;
480 }
481
482
483
484
485
486
487
488 if (dcrtc->csc_rgb_mode == CSC_AUTO)
489 val |= CFG_CSC_RGB_STUDIO;
490
491 return val;
492}
493
494
495static int armada_drm_crtc_mode_set(struct drm_crtc *crtc,
496 struct drm_display_mode *mode, struct drm_display_mode *adj,
497 int x, int y, struct drm_framebuffer *old_fb)
498{
499 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
500 struct armada_regs regs[17];
501 uint32_t lm, rm, tm, bm, val, sclk;
502 unsigned long flags;
503 unsigned i;
504 bool interlaced;
505
506 drm_framebuffer_reference(crtc->primary->fb);
507
508 interlaced = !!(adj->flags & DRM_MODE_FLAG_INTERLACE);
509
510 i = armada_drm_crtc_calc_fb(dcrtc->crtc.primary->fb,
511 x, y, regs, interlaced);
512
513 rm = adj->crtc_hsync_start - adj->crtc_hdisplay;
514 lm = adj->crtc_htotal - adj->crtc_hsync_end;
515 bm = adj->crtc_vsync_start - adj->crtc_vdisplay;
516 tm = adj->crtc_vtotal - adj->crtc_vsync_end;
517
518 DRM_DEBUG_DRIVER("H: %d %d %d %d lm %d rm %d\n",
519 adj->crtc_hdisplay,
520 adj->crtc_hsync_start,
521 adj->crtc_hsync_end,
522 adj->crtc_htotal, lm, rm);
523 DRM_DEBUG_DRIVER("V: %d %d %d %d tm %d bm %d\n",
524 adj->crtc_vdisplay,
525 adj->crtc_vsync_start,
526 adj->crtc_vsync_end,
527 adj->crtc_vtotal, tm, bm);
528
529
530 wait_event(dcrtc->frame_wait, !dcrtc->frame_work);
531
532 drm_crtc_vblank_off(crtc);
533
534 val = dcrtc->dumb_ctrl & ~CFG_DUMB_ENA;
535 if (val != dcrtc->dumb_ctrl) {
536 dcrtc->dumb_ctrl = val;
537 writel_relaxed(val, dcrtc->base + LCD_SPU_DUMB_CTRL);
538 }
539
540
541 dcrtc->variant->compute_clock(dcrtc, adj, &sclk);
542
543
544 armada_reg_queue_mod(regs, i, 0, CFG_PDWN64x66, LCD_SPU_SRAM_PARA1);
545 armada_reg_queue_set(regs, i, sclk, LCD_CFG_SCLK_DIV);
546
547 if (interlaced ^ dcrtc->interlaced) {
548 if (adj->flags & DRM_MODE_FLAG_INTERLACE)
549 drm_vblank_get(dcrtc->crtc.dev, dcrtc->num);
550 else
551 drm_vblank_put(dcrtc->crtc.dev, dcrtc->num);
552 dcrtc->interlaced = interlaced;
553 }
554
555 spin_lock_irqsave(&dcrtc->irq_lock, flags);
556
557
558 dcrtc->v[1].spu_v_h_total = adj->crtc_vtotal << 16 |
559 adj->crtc_htotal;
560 dcrtc->v[1].spu_v_porch = tm << 16 | bm;
561 val = adj->crtc_hsync_start;
562 dcrtc->v[1].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN |
563 dcrtc->variant->spu_adv_reg;
564
565 if (interlaced) {
566
567 dcrtc->v[0].spu_v_h_total = dcrtc->v[1].spu_v_h_total +
568 (1 << 16);
569 dcrtc->v[0].spu_v_porch = dcrtc->v[1].spu_v_porch + 1;
570 val = adj->crtc_hsync_start - adj->crtc_htotal / 2;
571 dcrtc->v[0].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN |
572 dcrtc->variant->spu_adv_reg;
573 } else {
574 dcrtc->v[0] = dcrtc->v[1];
575 }
576
577 val = adj->crtc_vdisplay << 16 | adj->crtc_hdisplay;
578
579 armada_reg_queue_set(regs, i, val, LCD_SPU_V_H_ACTIVE);
580 armada_reg_queue_set(regs, i, val, LCD_SPU_GRA_HPXL_VLN);
581 armada_reg_queue_set(regs, i, val, LCD_SPU_GZM_HPXL_VLN);
582 armada_reg_queue_set(regs, i, (lm << 16) | rm, LCD_SPU_H_PORCH);
583 armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_porch, LCD_SPU_V_PORCH);
584 armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_h_total,
585 LCD_SPUT_V_H_TOTAL);
586
587 if (dcrtc->variant->has_spu_adv_reg) {
588 armada_reg_queue_mod(regs, i, dcrtc->v[0].spu_adv_reg,
589 ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF |
590 ADV_VSYNCOFFEN, LCD_SPU_ADV_REG);
591 }
592
593 val = CFG_GRA_ENA | CFG_GRA_HSMOOTH;
594 val |= CFG_GRA_FMT(drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->fmt);
595 val |= CFG_GRA_MOD(drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->mod);
596
597 if (drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->fmt > CFG_420)
598 val |= CFG_PALETTE_ENA;
599
600 if (interlaced)
601 val |= CFG_GRA_FTOGGLE;
602
603 armada_reg_queue_mod(regs, i, val, CFG_GRAFORMAT |
604 CFG_GRA_MOD(CFG_SWAPRB | CFG_SWAPUV |
605 CFG_SWAPYU | CFG_YUV2RGB) |
606 CFG_PALETTE_ENA | CFG_GRA_FTOGGLE,
607 LCD_SPU_DMA_CTRL0);
608
609 val = adj->flags & DRM_MODE_FLAG_NVSYNC ? CFG_VSYNC_INV : 0;
610 armada_reg_queue_mod(regs, i, val, CFG_VSYNC_INV, LCD_SPU_DMA_CTRL1);
611
612 val = dcrtc->spu_iopad_ctrl | armada_drm_crtc_calculate_csc(dcrtc);
613 armada_reg_queue_set(regs, i, val, LCD_SPU_IOPAD_CONTROL);
614 armada_reg_queue_end(regs, i);
615
616 armada_drm_crtc_update_regs(dcrtc, regs);
617 spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
618
619 armada_drm_crtc_update(dcrtc);
620
621 drm_crtc_vblank_on(crtc);
622 armada_drm_crtc_finish_fb(dcrtc, old_fb, dpms_blanked(dcrtc->dpms));
623
624 return 0;
625}
626
627
628static int armada_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
629 struct drm_framebuffer *old_fb)
630{
631 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
632 struct armada_regs regs[4];
633 unsigned i;
634
635 i = armada_drm_crtc_calc_fb(crtc->primary->fb, crtc->x, crtc->y, regs,
636 dcrtc->interlaced);
637 armada_reg_queue_end(regs, i);
638
639
640 wait_event(dcrtc->frame_wait, !dcrtc->frame_work);
641
642
643 drm_framebuffer_reference(crtc->primary->fb);
644
645
646 armada_drm_crtc_update_regs(dcrtc, regs);
647
648
649 armada_drm_crtc_finish_fb(dcrtc, old_fb, dpms_blanked(dcrtc->dpms));
650
651 return 0;
652}
653
654
655static void armada_drm_crtc_disable(struct drm_crtc *crtc)
656{
657 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
658
659 armada_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
660 armada_drm_crtc_finish_fb(dcrtc, crtc->primary->fb, true);
661
662
663 writel_relaxed(CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 |
664 CFG_PDWN32x32 | CFG_PDWN16x66 | CFG_PDWN32x66 |
665 CFG_PDWN64x66, dcrtc->base + LCD_SPU_SRAM_PARA1);
666}
667
668static const struct drm_crtc_helper_funcs armada_crtc_helper_funcs = {
669 .dpms = armada_drm_crtc_dpms,
670 .prepare = armada_drm_crtc_prepare,
671 .commit = armada_drm_crtc_commit,
672 .mode_fixup = armada_drm_crtc_mode_fixup,
673 .mode_set = armada_drm_crtc_mode_set,
674 .mode_set_base = armada_drm_crtc_mode_set_base,
675 .disable = armada_drm_crtc_disable,
676};
677
678static void armada_load_cursor_argb(void __iomem *base, uint32_t *pix,
679 unsigned stride, unsigned width, unsigned height)
680{
681 uint32_t addr;
682 unsigned y;
683
684 addr = SRAM_HWC32_RAM1;
685 for (y = 0; y < height; y++) {
686 uint32_t *p = &pix[y * stride];
687 unsigned x;
688
689 for (x = 0; x < width; x++, p++) {
690 uint32_t val = *p;
691
692 val = (val & 0xff00ff00) |
693 (val & 0x000000ff) << 16 |
694 (val & 0x00ff0000) >> 16;
695
696 writel_relaxed(val,
697 base + LCD_SPU_SRAM_WRDAT);
698 writel_relaxed(addr | SRAM_WRITE,
699 base + LCD_SPU_SRAM_CTRL);
700 readl_relaxed(base + LCD_SPU_HWC_OVSA_HPXL_VLN);
701 addr += 1;
702 if ((addr & 0x00ff) == 0)
703 addr += 0xf00;
704 if ((addr & 0x30ff) == 0)
705 addr = SRAM_HWC32_RAM2;
706 }
707 }
708}
709
710static void armada_drm_crtc_cursor_tran(void __iomem *base)
711{
712 unsigned addr;
713
714 for (addr = 0; addr < 256; addr++) {
715
716 writel_relaxed(0x55555555, base + LCD_SPU_SRAM_WRDAT);
717 writel_relaxed(addr | SRAM_WRITE | SRAM_HWC32_TRAN,
718 base + LCD_SPU_SRAM_CTRL);
719 }
720}
721
722static int armada_drm_crtc_cursor_update(struct armada_crtc *dcrtc, bool reload)
723{
724 uint32_t xoff, xscr, w = dcrtc->cursor_w, s;
725 uint32_t yoff, yscr, h = dcrtc->cursor_h;
726 uint32_t para1;
727
728
729
730
731
732 if (dcrtc->cursor_x < 0) {
733 xoff = -dcrtc->cursor_x;
734 xscr = 0;
735 w -= min(xoff, w);
736 } else if (dcrtc->cursor_x + w > dcrtc->crtc.mode.hdisplay) {
737 xoff = 0;
738 xscr = dcrtc->cursor_x;
739 w = max_t(int, dcrtc->crtc.mode.hdisplay - dcrtc->cursor_x, 0);
740 } else {
741 xoff = 0;
742 xscr = dcrtc->cursor_x;
743 }
744
745 if (dcrtc->cursor_y < 0) {
746 yoff = -dcrtc->cursor_y;
747 yscr = 0;
748 h -= min(yoff, h);
749 } else if (dcrtc->cursor_y + h > dcrtc->crtc.mode.vdisplay) {
750 yoff = 0;
751 yscr = dcrtc->cursor_y;
752 h = max_t(int, dcrtc->crtc.mode.vdisplay - dcrtc->cursor_y, 0);
753 } else {
754 yoff = 0;
755 yscr = dcrtc->cursor_y;
756 }
757
758
759 s = dcrtc->cursor_w;
760 if (dcrtc->interlaced) {
761 s *= 2;
762 yscr /= 2;
763 h /= 2;
764 }
765
766 if (!dcrtc->cursor_obj || !h || !w) {
767 spin_lock_irq(&dcrtc->irq_lock);
768 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
769 dcrtc->cursor_update = false;
770 armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
771 spin_unlock_irq(&dcrtc->irq_lock);
772 return 0;
773 }
774
775 para1 = readl_relaxed(dcrtc->base + LCD_SPU_SRAM_PARA1);
776 armada_updatel(CFG_CSB_256x32, CFG_CSB_256x32 | CFG_PDWN256x32,
777 dcrtc->base + LCD_SPU_SRAM_PARA1);
778
779
780
781
782
783 if (!(para1 & CFG_CSB_256x32)) {
784 armada_drm_crtc_cursor_tran(dcrtc->base);
785 reload = true;
786 }
787
788 if (dcrtc->cursor_hw_sz != (h << 16 | w)) {
789 spin_lock_irq(&dcrtc->irq_lock);
790 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
791 dcrtc->cursor_update = false;
792 armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
793 spin_unlock_irq(&dcrtc->irq_lock);
794 reload = true;
795 }
796 if (reload) {
797 struct armada_gem_object *obj = dcrtc->cursor_obj;
798 uint32_t *pix;
799
800 pix = obj->addr;
801 pix += yoff * s + xoff;
802 armada_load_cursor_argb(dcrtc->base, pix, s, w, h);
803 }
804
805
806 spin_lock_irq(&dcrtc->irq_lock);
807 dcrtc->cursor_hw_pos = yscr << 16 | xscr;
808 dcrtc->cursor_hw_sz = h << 16 | w;
809 dcrtc->cursor_update = true;
810 armada_drm_crtc_enable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
811 spin_unlock_irq(&dcrtc->irq_lock);
812
813 return 0;
814}
815
816static void cursor_update(void *data)
817{
818 armada_drm_crtc_cursor_update(data, true);
819}
820
821static int armada_drm_crtc_cursor_set(struct drm_crtc *crtc,
822 struct drm_file *file, uint32_t handle, uint32_t w, uint32_t h)
823{
824 struct drm_device *dev = crtc->dev;
825 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
826 struct armada_gem_object *obj = NULL;
827 int ret;
828
829
830 if (!dcrtc->variant->has_spu_adv_reg)
831 return -ENXIO;
832
833 if (handle && w > 0 && h > 0) {
834
835 if (w > 64 || h > 64 || (w > 32 && h > 32))
836 return -ENOMEM;
837
838 obj = armada_gem_object_lookup(dev, file, handle);
839 if (!obj)
840 return -ENOENT;
841
842
843 if (!obj->addr) {
844 drm_gem_object_unreference_unlocked(&obj->obj);
845 return -EINVAL;
846 }
847
848 if (obj->obj.size < w * h * 4) {
849 DRM_ERROR("buffer is too small\n");
850 drm_gem_object_unreference_unlocked(&obj->obj);
851 return -ENOMEM;
852 }
853 }
854
855 mutex_lock(&dev->struct_mutex);
856 if (dcrtc->cursor_obj) {
857 dcrtc->cursor_obj->update = NULL;
858 dcrtc->cursor_obj->update_data = NULL;
859 drm_gem_object_unreference(&dcrtc->cursor_obj->obj);
860 }
861 dcrtc->cursor_obj = obj;
862 dcrtc->cursor_w = w;
863 dcrtc->cursor_h = h;
864 ret = armada_drm_crtc_cursor_update(dcrtc, true);
865 if (obj) {
866 obj->update_data = dcrtc;
867 obj->update = cursor_update;
868 }
869 mutex_unlock(&dev->struct_mutex);
870
871 return ret;
872}
873
874static int armada_drm_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
875{
876 struct drm_device *dev = crtc->dev;
877 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
878 int ret;
879
880
881 if (!dcrtc->variant->has_spu_adv_reg)
882 return -EFAULT;
883
884 mutex_lock(&dev->struct_mutex);
885 dcrtc->cursor_x = x;
886 dcrtc->cursor_y = y;
887 ret = armada_drm_crtc_cursor_update(dcrtc, false);
888 mutex_unlock(&dev->struct_mutex);
889
890 return ret;
891}
892
893static void armada_drm_crtc_destroy(struct drm_crtc *crtc)
894{
895 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
896 struct armada_private *priv = crtc->dev->dev_private;
897
898 if (dcrtc->cursor_obj)
899 drm_gem_object_unreference(&dcrtc->cursor_obj->obj);
900
901 priv->dcrtc[dcrtc->num] = NULL;
902 drm_crtc_cleanup(&dcrtc->crtc);
903
904 if (!IS_ERR(dcrtc->clk))
905 clk_disable_unprepare(dcrtc->clk);
906
907 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ENA);
908
909 of_node_put(dcrtc->crtc.port);
910
911 kfree(dcrtc);
912}
913
914
915
916
917
918static int armada_drm_crtc_page_flip(struct drm_crtc *crtc,
919 struct drm_framebuffer *fb, struct drm_pending_vblank_event *event, uint32_t page_flip_flags)
920{
921 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
922 struct armada_frame_work *work;
923 struct drm_device *dev = crtc->dev;
924 unsigned long flags;
925 unsigned i;
926 int ret;
927
928
929 if (fb->pixel_format != crtc->primary->fb->pixel_format)
930 return -EINVAL;
931
932 work = kmalloc(sizeof(*work), GFP_KERNEL);
933 if (!work)
934 return -ENOMEM;
935
936 work->event = event;
937 work->old_fb = dcrtc->crtc.primary->fb;
938
939 i = armada_drm_crtc_calc_fb(fb, crtc->x, crtc->y, work->regs,
940 dcrtc->interlaced);
941 armada_reg_queue_end(work->regs, i);
942
943
944
945
946
947 drm_framebuffer_reference(fb);
948
949 ret = armada_drm_crtc_queue_frame_work(dcrtc, work);
950 if (ret) {
951
952 drm_framebuffer_unreference(fb);
953 kfree(work);
954 return ret;
955 }
956
957
958
959
960
961
962
963 dcrtc->crtc.primary->fb = fb;
964
965
966
967
968
969 if (dpms_blanked(dcrtc->dpms)) {
970 spin_lock_irqsave(&dev->event_lock, flags);
971 if (dcrtc->frame_work)
972 armada_drm_crtc_complete_frame_work(dcrtc);
973 spin_unlock_irqrestore(&dev->event_lock, flags);
974 }
975
976 return 0;
977}
978
979static int
980armada_drm_crtc_set_property(struct drm_crtc *crtc,
981 struct drm_property *property, uint64_t val)
982{
983 struct armada_private *priv = crtc->dev->dev_private;
984 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
985 bool update_csc = false;
986
987 if (property == priv->csc_yuv_prop) {
988 dcrtc->csc_yuv_mode = val;
989 update_csc = true;
990 } else if (property == priv->csc_rgb_prop) {
991 dcrtc->csc_rgb_mode = val;
992 update_csc = true;
993 }
994
995 if (update_csc) {
996 uint32_t val;
997
998 val = dcrtc->spu_iopad_ctrl |
999 armada_drm_crtc_calculate_csc(dcrtc);
1000 writel_relaxed(val, dcrtc->base + LCD_SPU_IOPAD_CONTROL);
1001 }
1002
1003 return 0;
1004}
1005
1006static struct drm_crtc_funcs armada_crtc_funcs = {
1007 .cursor_set = armada_drm_crtc_cursor_set,
1008 .cursor_move = armada_drm_crtc_cursor_move,
1009 .destroy = armada_drm_crtc_destroy,
1010 .set_config = drm_crtc_helper_set_config,
1011 .page_flip = armada_drm_crtc_page_flip,
1012 .set_property = armada_drm_crtc_set_property,
1013};
1014
1015static struct drm_prop_enum_list armada_drm_csc_yuv_enum_list[] = {
1016 { CSC_AUTO, "Auto" },
1017 { CSC_YUV_CCIR601, "CCIR601" },
1018 { CSC_YUV_CCIR709, "CCIR709" },
1019};
1020
1021static struct drm_prop_enum_list armada_drm_csc_rgb_enum_list[] = {
1022 { CSC_AUTO, "Auto" },
1023 { CSC_RGB_COMPUTER, "Computer system" },
1024 { CSC_RGB_STUDIO, "Studio" },
1025};
1026
1027static int armada_drm_crtc_create_properties(struct drm_device *dev)
1028{
1029 struct armada_private *priv = dev->dev_private;
1030
1031 if (priv->csc_yuv_prop)
1032 return 0;
1033
1034 priv->csc_yuv_prop = drm_property_create_enum(dev, 0,
1035 "CSC_YUV", armada_drm_csc_yuv_enum_list,
1036 ARRAY_SIZE(armada_drm_csc_yuv_enum_list));
1037 priv->csc_rgb_prop = drm_property_create_enum(dev, 0,
1038 "CSC_RGB", armada_drm_csc_rgb_enum_list,
1039 ARRAY_SIZE(armada_drm_csc_rgb_enum_list));
1040
1041 if (!priv->csc_yuv_prop || !priv->csc_rgb_prop)
1042 return -ENOMEM;
1043
1044 return 0;
1045}
1046
1047int armada_drm_crtc_create(struct drm_device *drm, struct device *dev,
1048 struct resource *res, int irq, const struct armada_variant *variant,
1049 struct device_node *port)
1050{
1051 struct armada_private *priv = drm->dev_private;
1052 struct armada_crtc *dcrtc;
1053 void __iomem *base;
1054 int ret;
1055
1056 ret = armada_drm_crtc_create_properties(drm);
1057 if (ret)
1058 return ret;
1059
1060 base = devm_ioremap_resource(dev, res);
1061 if (IS_ERR(base))
1062 return PTR_ERR(base);
1063
1064 dcrtc = kzalloc(sizeof(*dcrtc), GFP_KERNEL);
1065 if (!dcrtc) {
1066 DRM_ERROR("failed to allocate Armada crtc\n");
1067 return -ENOMEM;
1068 }
1069
1070 if (dev != drm->dev)
1071 dev_set_drvdata(dev, dcrtc);
1072
1073 dcrtc->variant = variant;
1074 dcrtc->base = base;
1075 dcrtc->num = drm->mode_config.num_crtc;
1076 dcrtc->clk = ERR_PTR(-EINVAL);
1077 dcrtc->csc_yuv_mode = CSC_AUTO;
1078 dcrtc->csc_rgb_mode = CSC_AUTO;
1079 dcrtc->cfg_dumb_ctrl = DUMB24_RGB888_0;
1080 dcrtc->spu_iopad_ctrl = CFG_VSCALE_LN_EN | CFG_IOPAD_DUMB24;
1081 spin_lock_init(&dcrtc->irq_lock);
1082 dcrtc->irq_ena = CLEAN_SPU_IRQ_ISR;
1083 INIT_LIST_HEAD(&dcrtc->vbl_list);
1084 init_waitqueue_head(&dcrtc->frame_wait);
1085
1086
1087 writel_relaxed(0x00000001, dcrtc->base + LCD_CFG_SCLK_DIV);
1088 writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_BLANKCOLOR);
1089 writel_relaxed(dcrtc->spu_iopad_ctrl,
1090 dcrtc->base + LCD_SPU_IOPAD_CONTROL);
1091 writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_SRAM_PARA0);
1092 writel_relaxed(CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 |
1093 CFG_PDWN32x32 | CFG_PDWN16x66 | CFG_PDWN32x66 |
1094 CFG_PDWN64x66, dcrtc->base + LCD_SPU_SRAM_PARA1);
1095 writel_relaxed(0x2032ff81, dcrtc->base + LCD_SPU_DMA_CTRL1);
1096 writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_GRA_OVSA_HPXL_VLN);
1097 writel_relaxed(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
1098 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
1099
1100 ret = devm_request_irq(dev, irq, armada_drm_irq, 0, "armada_drm_crtc",
1101 dcrtc);
1102 if (ret < 0) {
1103 kfree(dcrtc);
1104 return ret;
1105 }
1106
1107 if (dcrtc->variant->init) {
1108 ret = dcrtc->variant->init(dcrtc, dev);
1109 if (ret) {
1110 kfree(dcrtc);
1111 return ret;
1112 }
1113 }
1114
1115
1116 armada_updatel(CFG_ARBFAST_ENA, 0, dcrtc->base + LCD_SPU_DMA_CTRL0);
1117
1118 priv->dcrtc[dcrtc->num] = dcrtc;
1119
1120 dcrtc->crtc.port = port;
1121 drm_crtc_init(drm, &dcrtc->crtc, &armada_crtc_funcs);
1122 drm_crtc_helper_add(&dcrtc->crtc, &armada_crtc_helper_funcs);
1123
1124 drm_object_attach_property(&dcrtc->crtc.base, priv->csc_yuv_prop,
1125 dcrtc->csc_yuv_mode);
1126 drm_object_attach_property(&dcrtc->crtc.base, priv->csc_rgb_prop,
1127 dcrtc->csc_rgb_mode);
1128
1129 return armada_overlay_plane_create(drm, 1 << dcrtc->num);
1130}
1131
1132static int
1133armada_lcd_bind(struct device *dev, struct device *master, void *data)
1134{
1135 struct platform_device *pdev = to_platform_device(dev);
1136 struct drm_device *drm = data;
1137 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1138 int irq = platform_get_irq(pdev, 0);
1139 const struct armada_variant *variant;
1140 struct device_node *port = NULL;
1141
1142 if (irq < 0)
1143 return irq;
1144
1145 if (!dev->of_node) {
1146 const struct platform_device_id *id;
1147
1148 id = platform_get_device_id(pdev);
1149 if (!id)
1150 return -ENXIO;
1151
1152 variant = (const struct armada_variant *)id->driver_data;
1153 } else {
1154 const struct of_device_id *match;
1155 struct device_node *np, *parent = dev->of_node;
1156
1157 match = of_match_device(dev->driver->of_match_table, dev);
1158 if (!match)
1159 return -ENXIO;
1160
1161 np = of_get_child_by_name(parent, "ports");
1162 if (np)
1163 parent = np;
1164 port = of_get_child_by_name(parent, "port");
1165 of_node_put(np);
1166 if (!port) {
1167 dev_err(dev, "no port node found in %s\n",
1168 parent->full_name);
1169 return -ENXIO;
1170 }
1171
1172 variant = match->data;
1173 }
1174
1175 return armada_drm_crtc_create(drm, dev, res, irq, variant, port);
1176}
1177
1178static void
1179armada_lcd_unbind(struct device *dev, struct device *master, void *data)
1180{
1181 struct armada_crtc *dcrtc = dev_get_drvdata(dev);
1182
1183 armada_drm_crtc_destroy(&dcrtc->crtc);
1184}
1185
1186static const struct component_ops armada_lcd_ops = {
1187 .bind = armada_lcd_bind,
1188 .unbind = armada_lcd_unbind,
1189};
1190
1191static int armada_lcd_probe(struct platform_device *pdev)
1192{
1193 return component_add(&pdev->dev, &armada_lcd_ops);
1194}
1195
1196static int armada_lcd_remove(struct platform_device *pdev)
1197{
1198 component_del(&pdev->dev, &armada_lcd_ops);
1199 return 0;
1200}
1201
1202static struct of_device_id armada_lcd_of_match[] = {
1203 {
1204 .compatible = "marvell,dove-lcd",
1205 .data = &armada510_ops,
1206 },
1207 {}
1208};
1209MODULE_DEVICE_TABLE(of, armada_lcd_of_match);
1210
1211static const struct platform_device_id armada_lcd_platform_ids[] = {
1212 {
1213 .name = "armada-lcd",
1214 .driver_data = (unsigned long)&armada510_ops,
1215 }, {
1216 .name = "armada-510-lcd",
1217 .driver_data = (unsigned long)&armada510_ops,
1218 },
1219 { },
1220};
1221MODULE_DEVICE_TABLE(platform, armada_lcd_platform_ids);
1222
1223struct platform_driver armada_lcd_platform_driver = {
1224 .probe = armada_lcd_probe,
1225 .remove = armada_lcd_remove,
1226 .driver = {
1227 .name = "armada-lcd",
1228 .owner = THIS_MODULE,
1229 .of_match_table = armada_lcd_of_match,
1230 },
1231 .id_table = armada_lcd_platform_ids,
1232};
1233