blob: 0a1e50c540ad4fd872d8f3d6a38fc0cacea0ebde [file] [log] [blame]
Benjamin Gaignardcfd8d742014-07-28 10:30:02 +02001/*
2 * Copyright (C) STMicroelectronics SA 2014
3 * Author: Fabien Dessenne <fabien.dessenne@st.com> for STMicroelectronics.
4 * License terms: GNU General Public License (GPL), version 2
5 */
6
7#include <drm/drmP.h>
8
Vincent Abriou9e1f05b2015-07-31 11:32:34 +02009#include "sti_plane.h"
Benjamin Gaignardcfd8d742014-07-28 10:30:02 +020010#include "sti_vid.h"
11#include "sti_vtg.h"
12
13/* Registers */
14#define VID_CTL 0x00
15#define VID_ALP 0x04
16#define VID_CLF 0x08
17#define VID_VPO 0x0C
18#define VID_VPS 0x10
19#define VID_KEY1 0x28
20#define VID_KEY2 0x2C
21#define VID_MPR0 0x30
22#define VID_MPR1 0x34
23#define VID_MPR2 0x38
24#define VID_MPR3 0x3C
25#define VID_MST 0x68
26#define VID_BC 0x70
27#define VID_TINT 0x74
28#define VID_CSAT 0x78
29
30/* Registers values */
31#define VID_CTL_IGNORE (BIT(31) | BIT(30))
32#define VID_CTL_PSI_ENABLE (BIT(2) | BIT(1) | BIT(0))
33#define VID_ALP_OPAQUE 0x00000080
34#define VID_BC_DFLT 0x00008000
35#define VID_TINT_DFLT 0x00000000
36#define VID_CSAT_DFLT 0x00000080
37/* YCbCr to RGB BT709:
38 * R = Y+1.5391Cr
39 * G = Y-0.4590Cr-0.1826Cb
40 * B = Y+1.8125Cb */
41#define VID_MPR0_BT709 0x0A800000
42#define VID_MPR1_BT709 0x0AC50000
43#define VID_MPR2_BT709 0x07150545
44#define VID_MPR3_BT709 0x00000AE8
Bich Hemon05a142c2016-02-10 10:39:23 +010045/* YCbCr to RGB BT709:
46 * R = Y+1.3711Cr
47 * G = Y-0.6992Cr-0.3359Cb
48 * B = Y+1.7344Cb
49 */
50#define VID_MPR0_BT601 0x0A800000
51#define VID_MPR1_BT601 0x0AAF0000
52#define VID_MPR2_BT601 0x094E0754
53#define VID_MPR3_BT601 0x00000ADD
54
55#define VID_MIN_HD_HEIGHT 720
Benjamin Gaignardcfd8d742014-07-28 10:30:02 +020056
Vincent Abriou29d1dc62015-08-03 14:22:16 +020057void sti_vid_commit(struct sti_vid *vid,
58 struct drm_plane_state *state)
Benjamin Gaignardcfd8d742014-07-28 10:30:02 +020059{
Vincent Abriou29d1dc62015-08-03 14:22:16 +020060 struct drm_crtc *crtc = state->crtc;
61 struct drm_display_mode *mode = &crtc->mode;
62 int dst_x = state->crtc_x;
63 int dst_y = state->crtc_y;
64 int dst_w = clamp_val(state->crtc_w, 0, mode->crtc_hdisplay - dst_x);
65 int dst_h = clamp_val(state->crtc_h, 0, mode->crtc_vdisplay - dst_y);
Bich Hemon05a142c2016-02-10 10:39:23 +010066 int src_h = state->src_h >> 16;
Vincent Abriou871bcdf2015-07-31 11:32:13 +020067 u32 val, ydo, xdo, yds, xds;
Benjamin Gaignardcfd8d742014-07-28 10:30:02 +020068
Vincent Abriou29d1dc62015-08-03 14:22:16 +020069 /* Input / output size
70 * Align to upper even value */
71 dst_w = ALIGN(dst_w, 2);
72 dst_h = ALIGN(dst_h, 2);
73
Benjamin Gaignardcfd8d742014-07-28 10:30:02 +020074 /* Unmask */
75 val = readl(vid->regs + VID_CTL);
76 val &= ~VID_CTL_IGNORE;
77 writel(val, vid->regs + VID_CTL);
78
Vincent Abriou29d1dc62015-08-03 14:22:16 +020079 ydo = sti_vtg_get_line_number(*mode, dst_y);
80 yds = sti_vtg_get_line_number(*mode, dst_y + dst_h - 1);
81 xdo = sti_vtg_get_pixel_number(*mode, dst_x);
82 xds = sti_vtg_get_pixel_number(*mode, dst_x + dst_w - 1);
Benjamin Gaignardcfd8d742014-07-28 10:30:02 +020083
84 writel((ydo << 16) | xdo, vid->regs + VID_VPO);
85 writel((yds << 16) | xds, vid->regs + VID_VPS);
Bich Hemon05a142c2016-02-10 10:39:23 +010086
87 /* Color conversion parameters */
88 if (src_h >= VID_MIN_HD_HEIGHT) {
89 writel(VID_MPR0_BT709, vid->regs + VID_MPR0);
90 writel(VID_MPR1_BT709, vid->regs + VID_MPR1);
91 writel(VID_MPR2_BT709, vid->regs + VID_MPR2);
92 writel(VID_MPR3_BT709, vid->regs + VID_MPR3);
93 } else {
94 writel(VID_MPR0_BT601, vid->regs + VID_MPR0);
95 writel(VID_MPR1_BT601, vid->regs + VID_MPR1);
96 writel(VID_MPR2_BT601, vid->regs + VID_MPR2);
97 writel(VID_MPR3_BT601, vid->regs + VID_MPR3);
98 }
Benjamin Gaignardcfd8d742014-07-28 10:30:02 +020099}
100
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200101void sti_vid_disable(struct sti_vid *vid)
Benjamin Gaignardcfd8d742014-07-28 10:30:02 +0200102{
103 u32 val;
104
105 /* Mask */
106 val = readl(vid->regs + VID_CTL);
107 val |= VID_CTL_IGNORE;
108 writel(val, vid->regs + VID_CTL);
Benjamin Gaignardcfd8d742014-07-28 10:30:02 +0200109}
110
Vincent Abriou871bcdf2015-07-31 11:32:13 +0200111static void sti_vid_init(struct sti_vid *vid)
Benjamin Gaignardcfd8d742014-07-28 10:30:02 +0200112{
113 /* Enable PSI, Mask layer */
114 writel(VID_CTL_PSI_ENABLE | VID_CTL_IGNORE, vid->regs + VID_CTL);
115
116 /* Opaque */
117 writel(VID_ALP_OPAQUE, vid->regs + VID_ALP);
118
Benjamin Gaignardcfd8d742014-07-28 10:30:02 +0200119 /* Brightness, contrast, tint, saturation */
120 writel(VID_BC_DFLT, vid->regs + VID_BC);
121 writel(VID_TINT_DFLT, vid->regs + VID_TINT);
122 writel(VID_CSAT_DFLT, vid->regs + VID_CSAT);
123}
124
Vincent Abriou871bcdf2015-07-31 11:32:13 +0200125struct sti_vid *sti_vid_create(struct device *dev, int id,
126 void __iomem *baseaddr)
Benjamin Gaignardcfd8d742014-07-28 10:30:02 +0200127{
Vincent Abriou871bcdf2015-07-31 11:32:13 +0200128 struct sti_vid *vid;
Benjamin Gaignardcfd8d742014-07-28 10:30:02 +0200129
130 vid = devm_kzalloc(dev, sizeof(*vid), GFP_KERNEL);
131 if (!vid) {
132 DRM_ERROR("Failed to allocate memory for VID\n");
133 return NULL;
134 }
135
Vincent Abriou871bcdf2015-07-31 11:32:13 +0200136 vid->dev = dev;
137 vid->regs = baseaddr;
138 vid->id = id;
139
140 sti_vid_init(vid);
Benjamin Gaignardcfd8d742014-07-28 10:30:02 +0200141
142 return vid;
143}