1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2010 Sascha Hauer <s.hauer@pengutronix.de>
4 * Copyright (C) 2005-2009 Freescale Semiconductor, Inc.
6 #include <linux/export.h>
7 #include <linux/kernel.h>
8 #include <linux/types.h>
9 #include <linux/errno.h>
11 #include <linux/err.h>
13 #include <drm/drm_color_mgmt.h>
14 #include <video/imx-ipu-v3.h>
18 #define DP_ASYNC0 0x60
19 #define DP_ASYNC1 0xBC
21 #define DP_COM_CONF 0x0
22 #define DP_GRAPH_WIND_CTRL 0x0004
23 #define DP_FG_POS 0x0008
24 #define DP_CSC_A_0 0x0044
25 #define DP_CSC_A_1 0x0048
26 #define DP_CSC_A_2 0x004C
27 #define DP_CSC_A_3 0x0050
28 #define DP_CSC_0 0x0054
29 #define DP_CSC_1 0x0058
31 #define DP_COM_CONF_FG_EN (1 << 0)
32 #define DP_COM_CONF_GWSEL (1 << 1)
33 #define DP_COM_CONF_GWAM (1 << 2)
34 #define DP_COM_CONF_GWCKE (1 << 3)
35 #define DP_COM_CONF_CSC_DEF_MASK (3 << 8)
36 #define DP_COM_CONF_CSC_DEF_OFFSET 8
37 #define DP_COM_CONF_CSC_DEF_FG (3 << 8)
38 #define DP_COM_CONF_CSC_DEF_BG (2 << 8)
39 #define DP_COM_CONF_CSC_DEF_BOTH (1 << 8)
41 #define IPUV3_NUM_FLOWS 3
49 enum ipu_color_space in_cs;
53 struct ipu_dp foreground;
54 struct ipu_dp background;
55 enum ipu_color_space out_cs;
57 struct ipu_dp_priv *priv;
64 struct ipu_flow flow[IPUV3_NUM_FLOWS];
69 static u32 ipu_dp_flow_base[] = {DP_SYNC, DP_ASYNC0, DP_ASYNC1};
71 static inline struct ipu_flow *to_flow(struct ipu_dp *dp)
74 return container_of(dp, struct ipu_flow, foreground);
76 return container_of(dp, struct ipu_flow, background);
79 int ipu_dp_set_global_alpha(struct ipu_dp *dp, bool enable,
80 u8 alpha, bool bg_chan)
82 struct ipu_flow *flow = to_flow(dp);
83 struct ipu_dp_priv *priv = flow->priv;
86 mutex_lock(&priv->mutex);
88 reg = readl(flow->base + DP_COM_CONF);
90 reg &= ~DP_COM_CONF_GWSEL;
92 reg |= DP_COM_CONF_GWSEL;
93 writel(reg, flow->base + DP_COM_CONF);
96 reg = readl(flow->base + DP_GRAPH_WIND_CTRL) & 0x00FFFFFFL;
97 writel(reg | ((u32) alpha << 24),
98 flow->base + DP_GRAPH_WIND_CTRL);
100 reg = readl(flow->base + DP_COM_CONF);
101 writel(reg | DP_COM_CONF_GWAM, flow->base + DP_COM_CONF);
103 reg = readl(flow->base + DP_COM_CONF);
104 writel(reg & ~DP_COM_CONF_GWAM, flow->base + DP_COM_CONF);
107 ipu_srm_dp_update(priv->ipu, true);
109 mutex_unlock(&priv->mutex);
113 EXPORT_SYMBOL_GPL(ipu_dp_set_global_alpha);
115 int ipu_dp_set_window_pos(struct ipu_dp *dp, u16 x_pos, u16 y_pos)
117 struct ipu_flow *flow = to_flow(dp);
118 struct ipu_dp_priv *priv = flow->priv;
120 writel((x_pos << 16) | y_pos, flow->base + DP_FG_POS);
122 ipu_srm_dp_update(priv->ipu, true);
126 EXPORT_SYMBOL_GPL(ipu_dp_set_window_pos);
128 static void ipu_dp_csc_init(struct ipu_flow *flow,
129 enum drm_color_encoding ycbcr_enc,
130 enum drm_color_range range,
131 enum ipu_color_space in,
132 enum ipu_color_space out,
137 reg = readl(flow->base + DP_COM_CONF);
138 reg &= ~DP_COM_CONF_CSC_DEF_MASK;
141 writel(reg, flow->base + DP_COM_CONF);
145 if (in == IPUV3_COLORSPACE_RGB && out == IPUV3_COLORSPACE_YUV) {
146 writel(0x099 | (0x12d << 16), flow->base + DP_CSC_A_0);
147 writel(0x03a | (0x3a9 << 16), flow->base + DP_CSC_A_1);
148 writel(0x356 | (0x100 << 16), flow->base + DP_CSC_A_2);
149 writel(0x100 | (0x329 << 16), flow->base + DP_CSC_A_3);
150 writel(0x3d6 | (0x0000 << 16) | (2 << 30),
151 flow->base + DP_CSC_0);
152 writel(0x200 | (2 << 14) | (0x200 << 16) | (2 << 30),
153 flow->base + DP_CSC_1);
154 } else if (ycbcr_enc == DRM_COLOR_YCBCR_BT709) {
155 /* Rec.709 limited range */
156 writel(0x095 | (0x000 << 16), flow->base + DP_CSC_A_0);
157 writel(0x0e5 | (0x095 << 16), flow->base + DP_CSC_A_1);
158 writel(0x3e5 | (0x3bc << 16), flow->base + DP_CSC_A_2);
159 writel(0x095 | (0x10e << 16), flow->base + DP_CSC_A_3);
160 writel(0x000 | (0x3e10 << 16) | (1 << 30),
161 flow->base + DP_CSC_0);
162 writel(0x09a | (1 << 14) | (0x3dbe << 16) | (1 << 30),
163 flow->base + DP_CSC_1);
165 /* BT.601 limited range */
166 writel(0x095 | (0x000 << 16), flow->base + DP_CSC_A_0);
167 writel(0x0cc | (0x095 << 16), flow->base + DP_CSC_A_1);
168 writel(0x3ce | (0x398 << 16), flow->base + DP_CSC_A_2);
169 writel(0x095 | (0x0ff << 16), flow->base + DP_CSC_A_3);
170 writel(0x000 | (0x3e42 << 16) | (1 << 30),
171 flow->base + DP_CSC_0);
172 writel(0x10a | (1 << 14) | (0x3dd6 << 16) | (1 << 30),
173 flow->base + DP_CSC_1);
178 writel(reg, flow->base + DP_COM_CONF);
181 int ipu_dp_setup_channel(struct ipu_dp *dp,
182 enum drm_color_encoding ycbcr_enc,
183 enum drm_color_range range,
184 enum ipu_color_space in,
185 enum ipu_color_space out)
187 struct ipu_flow *flow = to_flow(dp);
188 struct ipu_dp_priv *priv = flow->priv;
190 mutex_lock(&priv->mutex);
197 if (flow->foreground.in_cs == flow->background.in_cs) {
199 * foreground and background are of same colorspace, put
200 * colorspace converter after combining unit.
202 ipu_dp_csc_init(flow, ycbcr_enc, range,
203 flow->foreground.in_cs, flow->out_cs,
204 DP_COM_CONF_CSC_DEF_BOTH);
206 if (flow->foreground.in_cs == IPUV3_COLORSPACE_UNKNOWN ||
207 flow->foreground.in_cs == flow->out_cs)
209 * foreground identical to output, apply color
210 * conversion on background
212 ipu_dp_csc_init(flow, ycbcr_enc, range,
213 flow->background.in_cs,
214 flow->out_cs, DP_COM_CONF_CSC_DEF_BG);
216 ipu_dp_csc_init(flow, ycbcr_enc, range,
217 flow->foreground.in_cs,
218 flow->out_cs, DP_COM_CONF_CSC_DEF_FG);
221 ipu_srm_dp_update(priv->ipu, true);
223 mutex_unlock(&priv->mutex);
227 EXPORT_SYMBOL_GPL(ipu_dp_setup_channel);
229 int ipu_dp_enable(struct ipu_soc *ipu)
231 struct ipu_dp_priv *priv = ipu->dp_priv;
233 mutex_lock(&priv->mutex);
235 if (!priv->use_count)
236 ipu_module_enable(priv->ipu, IPU_CONF_DP_EN);
240 mutex_unlock(&priv->mutex);
244 EXPORT_SYMBOL_GPL(ipu_dp_enable);
246 int ipu_dp_enable_channel(struct ipu_dp *dp)
248 struct ipu_flow *flow = to_flow(dp);
249 struct ipu_dp_priv *priv = flow->priv;
255 mutex_lock(&priv->mutex);
257 reg = readl(flow->base + DP_COM_CONF);
258 reg |= DP_COM_CONF_FG_EN;
259 writel(reg, flow->base + DP_COM_CONF);
261 ipu_srm_dp_update(priv->ipu, true);
263 mutex_unlock(&priv->mutex);
267 EXPORT_SYMBOL_GPL(ipu_dp_enable_channel);
269 void ipu_dp_disable_channel(struct ipu_dp *dp, bool sync)
271 struct ipu_flow *flow = to_flow(dp);
272 struct ipu_dp_priv *priv = flow->priv;
275 dp->in_cs = IPUV3_COLORSPACE_UNKNOWN;
280 mutex_lock(&priv->mutex);
282 reg = readl(flow->base + DP_COM_CONF);
283 csc = reg & DP_COM_CONF_CSC_DEF_MASK;
284 reg &= ~DP_COM_CONF_CSC_DEF_MASK;
285 if (csc == DP_COM_CONF_CSC_DEF_BOTH || csc == DP_COM_CONF_CSC_DEF_BG)
286 reg |= DP_COM_CONF_CSC_DEF_BG;
288 reg &= ~DP_COM_CONF_FG_EN;
289 writel(reg, flow->base + DP_COM_CONF);
291 writel(0, flow->base + DP_FG_POS);
292 ipu_srm_dp_update(priv->ipu, sync);
294 mutex_unlock(&priv->mutex);
296 EXPORT_SYMBOL_GPL(ipu_dp_disable_channel);
298 void ipu_dp_disable(struct ipu_soc *ipu)
300 struct ipu_dp_priv *priv = ipu->dp_priv;
302 mutex_lock(&priv->mutex);
306 if (!priv->use_count)
307 ipu_module_disable(priv->ipu, IPU_CONF_DP_EN);
309 if (priv->use_count < 0)
312 mutex_unlock(&priv->mutex);
314 EXPORT_SYMBOL_GPL(ipu_dp_disable);
316 struct ipu_dp *ipu_dp_get(struct ipu_soc *ipu, unsigned int flow)
318 struct ipu_dp_priv *priv = ipu->dp_priv;
321 if ((flow >> 1) >= IPUV3_NUM_FLOWS)
322 return ERR_PTR(-EINVAL);
325 dp = &priv->flow[flow >> 1].foreground;
327 dp = &priv->flow[flow >> 1].background;
330 return ERR_PTR(-EBUSY);
336 EXPORT_SYMBOL_GPL(ipu_dp_get);
338 void ipu_dp_put(struct ipu_dp *dp)
342 EXPORT_SYMBOL_GPL(ipu_dp_put);
344 int ipu_dp_init(struct ipu_soc *ipu, struct device *dev, unsigned long base)
346 struct ipu_dp_priv *priv;
349 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
357 priv->base = devm_ioremap(dev, base, PAGE_SIZE);
361 mutex_init(&priv->mutex);
363 for (i = 0; i < IPUV3_NUM_FLOWS; i++) {
364 priv->flow[i].background.in_cs = IPUV3_COLORSPACE_UNKNOWN;
365 priv->flow[i].foreground.in_cs = IPUV3_COLORSPACE_UNKNOWN;
366 priv->flow[i].foreground.foreground = true;
367 priv->flow[i].base = priv->base + ipu_dp_flow_base[i];
368 priv->flow[i].priv = priv;
374 void ipu_dp_exit(struct ipu_soc *ipu)