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.
7 #include <linux/export.h>
8 #include <linux/module.h>
9 #include <linux/types.h>
10 #include <linux/errno.h>
11 #include <linux/delay.h>
12 #include <linux/interrupt.h>
15 #include <video/imx-ipu-v3.h>
18 #define DC_MAP_CONF_PTR(n) (0x108 + ((n) & ~0x1) * 2)
19 #define DC_MAP_CONF_VAL(n) (0x144 + ((n) & ~0x1) * 2)
24 #define DC_EVT_NFIELD 3
26 #define DC_EVT_EOFIELD 5
27 #define DC_EVT_NEW_ADDR 6
28 #define DC_EVT_NEW_CHAN 7
29 #define DC_EVT_NEW_DATA 8
31 #define DC_EVT_NEW_ADDR_W_0 0
32 #define DC_EVT_NEW_ADDR_W_1 1
33 #define DC_EVT_NEW_CHAN_W_0 2
34 #define DC_EVT_NEW_CHAN_W_1 3
35 #define DC_EVT_NEW_DATA_W_0 4
36 #define DC_EVT_NEW_DATA_W_1 5
37 #define DC_EVT_NEW_ADDR_R_0 6
38 #define DC_EVT_NEW_ADDR_R_1 7
39 #define DC_EVT_NEW_CHAN_R_0 8
40 #define DC_EVT_NEW_CHAN_R_1 9
41 #define DC_EVT_NEW_DATA_R_0 10
42 #define DC_EVT_NEW_DATA_R_1 11
44 #define DC_WR_CH_CONF 0x0
45 #define DC_WR_CH_ADDR 0x4
46 #define DC_RL_CH(evt) (8 + ((evt) & ~0x1) * 2)
49 #define DC_DISP_CONF1(disp) (0xd8 + (disp) * 4)
50 #define DC_DISP_CONF2(disp) (0xe8 + (disp) * 4)
53 #define WROD(lf) (0x18 | ((lf) << 1))
58 #define NULL_WAVE (-1)
60 #define DC_GEN_SYNC_1_6_SYNC (2 << 1)
61 #define DC_GEN_SYNC_PRIORITY_1 (1 << 7)
63 #define DC_WR_CH_CONF_WORD_SIZE_8 (0 << 0)
64 #define DC_WR_CH_CONF_WORD_SIZE_16 (1 << 0)
65 #define DC_WR_CH_CONF_WORD_SIZE_24 (2 << 0)
66 #define DC_WR_CH_CONF_WORD_SIZE_32 (3 << 0)
67 #define DC_WR_CH_CONF_DISP_ID_PARALLEL(i) (((i) & 0x1) << 3)
68 #define DC_WR_CH_CONF_DISP_ID_SERIAL (2 << 3)
69 #define DC_WR_CH_CONF_DISP_ID_ASYNC (3 << 4)
70 #define DC_WR_CH_CONF_FIELD_MODE (1 << 9)
71 #define DC_WR_CH_CONF_PROG_TYPE_NORMAL (4 << 5)
72 #define DC_WR_CH_CONF_PROG_TYPE_MASK (7 << 5)
73 #define DC_WR_CH_CONF_PROG_DI_ID (1 << 2)
74 #define DC_WR_CH_CONF_PROG_DISP_ID(i) (((i) & 0x1) << 3)
76 #define IPU_DC_NUM_CHANNELS 10
83 IPU_DC_MAP_GBR24
, /* TVEv2 */
90 /* The display interface number assigned to this dc channel */
93 struct ipu_dc_priv
*priv
;
100 void __iomem
*dc_tmpl_reg
;
103 struct ipu_dc channels
[IPU_DC_NUM_CHANNELS
];
105 struct completion comp
;
109 static void dc_link_event(struct ipu_dc
*dc
, int event
, int addr
, int priority
)
113 reg
= readl(dc
->base
+ DC_RL_CH(event
));
114 reg
&= ~(0xffff << (16 * (event
& 0x1)));
115 reg
|= ((addr
<< 8) | priority
) << (16 * (event
& 0x1));
116 writel(reg
, dc
->base
+ DC_RL_CH(event
));
119 static void dc_write_tmpl(struct ipu_dc
*dc
, int word
, u32 opcode
, u32 operand
,
120 int map
, int wave
, int glue
, int sync
, int stop
)
122 struct ipu_dc_priv
*priv
= dc
->priv
;
125 if (opcode
== WCLK
) {
126 reg1
= (operand
<< 20) & 0xfff00000;
127 reg2
= operand
>> 12 | opcode
<< 1 | stop
<< 9;
128 } else if (opcode
== WRG
) {
129 reg1
= sync
| glue
<< 4 | ++wave
<< 11 | ((operand
<< 15) & 0xffff8000);
130 reg2
= operand
>> 17 | opcode
<< 7 | stop
<< 9;
132 reg1
= sync
| glue
<< 4 | ++wave
<< 11 | ++map
<< 15 | ((operand
<< 20) & 0xfff00000);
133 reg2
= operand
>> 12 | opcode
<< 4 | stop
<< 9;
135 writel(reg1
, priv
->dc_tmpl_reg
+ word
* 8);
136 writel(reg2
, priv
->dc_tmpl_reg
+ word
* 8 + 4);
139 static int ipu_bus_format_to_map(u32 fmt
)
145 case MEDIA_BUS_FMT_RGB888_1X24
:
146 return IPU_DC_MAP_RGB24
;
147 case MEDIA_BUS_FMT_RGB565_1X16
:
148 return IPU_DC_MAP_RGB565
;
149 case MEDIA_BUS_FMT_GBR888_1X24
:
150 return IPU_DC_MAP_GBR24
;
151 case MEDIA_BUS_FMT_RGB666_1X18
:
152 return IPU_DC_MAP_BGR666
;
153 case MEDIA_BUS_FMT_RGB666_1X24_CPADHI
:
154 return IPU_DC_MAP_LVDS666
;
155 case MEDIA_BUS_FMT_BGR888_1X24
:
156 return IPU_DC_MAP_BGR24
;
160 int ipu_dc_init_sync(struct ipu_dc
*dc
, struct ipu_di
*di
, bool interlaced
,
161 u32 bus_format
, u32 width
)
163 struct ipu_dc_priv
*priv
= dc
->priv
;
168 dc
->di
= ipu_di_get_num(di
);
170 map
= ipu_bus_format_to_map(bus_format
);
173 * In interlaced mode we need more counters to create the asymmetric
174 * per-field VSYNC signals. The pixel active signal synchronising DC
175 * to DI moves to signal generator #6 (see ipu-di.c). In progressive
176 * mode counter #5 is used.
178 sync
= interlaced
? 6 : 5;
180 /* Reserve 5 microcode template words for each DI */
187 dc_link_event(dc
, DC_EVT_NL
, addr
, 3);
188 dc_link_event(dc
, DC_EVT_EOL
, addr
, 2);
189 dc_link_event(dc
, DC_EVT_NEW_DATA
, addr
, 1);
191 /* Init template microcode */
192 dc_write_tmpl(dc
, addr
, WROD(0), 0, map
, SYNC_WAVE
, 0, sync
, 1);
194 dc_link_event(dc
, DC_EVT_NL
, addr
+ 2, 3);
195 dc_link_event(dc
, DC_EVT_EOL
, addr
+ 3, 2);
196 dc_link_event(dc
, DC_EVT_NEW_DATA
, addr
+ 1, 1);
198 /* Init template microcode */
199 dc_write_tmpl(dc
, addr
+ 2, WROD(0), 0, map
, SYNC_WAVE
, 8, sync
, 1);
200 dc_write_tmpl(dc
, addr
+ 3, WROD(0), 0, map
, SYNC_WAVE
, 4, sync
, 0);
201 dc_write_tmpl(dc
, addr
+ 4, WRG
, 0, map
, NULL_WAVE
, 0, 0, 1);
202 dc_write_tmpl(dc
, addr
+ 1, WROD(0), 0, map
, SYNC_WAVE
, 0, sync
, 1);
205 dc_link_event(dc
, DC_EVT_NF
, 0, 0);
206 dc_link_event(dc
, DC_EVT_NFIELD
, 0, 0);
207 dc_link_event(dc
, DC_EVT_EOF
, 0, 0);
208 dc_link_event(dc
, DC_EVT_EOFIELD
, 0, 0);
209 dc_link_event(dc
, DC_EVT_NEW_CHAN
, 0, 0);
210 dc_link_event(dc
, DC_EVT_NEW_ADDR
, 0, 0);
212 reg
= readl(dc
->base
+ DC_WR_CH_CONF
);
214 reg
|= DC_WR_CH_CONF_FIELD_MODE
;
216 reg
&= ~DC_WR_CH_CONF_FIELD_MODE
;
217 writel(reg
, dc
->base
+ DC_WR_CH_CONF
);
219 writel(0x0, dc
->base
+ DC_WR_CH_ADDR
);
220 writel(width
, priv
->dc_reg
+ DC_DISP_CONF2(dc
->di
));
224 EXPORT_SYMBOL_GPL(ipu_dc_init_sync
);
226 void ipu_dc_enable(struct ipu_soc
*ipu
)
228 struct ipu_dc_priv
*priv
= ipu
->dc_priv
;
230 mutex_lock(&priv
->mutex
);
232 if (!priv
->use_count
)
233 ipu_module_enable(priv
->ipu
, IPU_CONF_DC_EN
);
237 mutex_unlock(&priv
->mutex
);
239 EXPORT_SYMBOL_GPL(ipu_dc_enable
);
241 void ipu_dc_enable_channel(struct ipu_dc
*dc
)
245 reg
= readl(dc
->base
+ DC_WR_CH_CONF
);
246 reg
|= DC_WR_CH_CONF_PROG_TYPE_NORMAL
;
247 writel(reg
, dc
->base
+ DC_WR_CH_CONF
);
249 EXPORT_SYMBOL_GPL(ipu_dc_enable_channel
);
251 void ipu_dc_disable_channel(struct ipu_dc
*dc
)
255 val
= readl(dc
->base
+ DC_WR_CH_CONF
);
256 val
&= ~DC_WR_CH_CONF_PROG_TYPE_MASK
;
257 writel(val
, dc
->base
+ DC_WR_CH_CONF
);
259 EXPORT_SYMBOL_GPL(ipu_dc_disable_channel
);
261 void ipu_dc_disable(struct ipu_soc
*ipu
)
263 struct ipu_dc_priv
*priv
= ipu
->dc_priv
;
265 mutex_lock(&priv
->mutex
);
268 if (!priv
->use_count
)
269 ipu_module_disable(priv
->ipu
, IPU_CONF_DC_EN
);
271 if (priv
->use_count
< 0)
274 mutex_unlock(&priv
->mutex
);
276 EXPORT_SYMBOL_GPL(ipu_dc_disable
);
278 static void ipu_dc_map_config(struct ipu_dc_priv
*priv
, enum ipu_dc_map map
,
279 int byte_num
, int offset
, int mask
)
281 int ptr
= map
* 3 + byte_num
;
284 reg
= readl(priv
->dc_reg
+ DC_MAP_CONF_VAL(ptr
));
285 reg
&= ~(0xffff << (16 * (ptr
& 0x1)));
286 reg
|= ((offset
<< 8) | mask
) << (16 * (ptr
& 0x1));
287 writel(reg
, priv
->dc_reg
+ DC_MAP_CONF_VAL(ptr
));
289 reg
= readl(priv
->dc_reg
+ DC_MAP_CONF_PTR(map
));
290 reg
&= ~(0x1f << ((16 * (map
& 0x1)) + (5 * byte_num
)));
291 reg
|= ptr
<< ((16 * (map
& 0x1)) + (5 * byte_num
));
292 writel(reg
, priv
->dc_reg
+ DC_MAP_CONF_PTR(map
));
295 static void ipu_dc_map_clear(struct ipu_dc_priv
*priv
, int map
)
297 u32 reg
= readl(priv
->dc_reg
+ DC_MAP_CONF_PTR(map
));
299 writel(reg
& ~(0xffff << (16 * (map
& 0x1))),
300 priv
->dc_reg
+ DC_MAP_CONF_PTR(map
));
303 struct ipu_dc
*ipu_dc_get(struct ipu_soc
*ipu
, int channel
)
305 struct ipu_dc_priv
*priv
= ipu
->dc_priv
;
308 if (channel
>= IPU_DC_NUM_CHANNELS
)
309 return ERR_PTR(-ENODEV
);
311 dc
= &priv
->channels
[channel
];
313 mutex_lock(&priv
->mutex
);
316 mutex_unlock(&priv
->mutex
);
317 return ERR_PTR(-EBUSY
);
322 mutex_unlock(&priv
->mutex
);
326 EXPORT_SYMBOL_GPL(ipu_dc_get
);
328 void ipu_dc_put(struct ipu_dc
*dc
)
330 struct ipu_dc_priv
*priv
= dc
->priv
;
332 mutex_lock(&priv
->mutex
);
334 mutex_unlock(&priv
->mutex
);
336 EXPORT_SYMBOL_GPL(ipu_dc_put
);
338 int ipu_dc_init(struct ipu_soc
*ipu
, struct device
*dev
,
339 unsigned long base
, unsigned long template_base
)
341 struct ipu_dc_priv
*priv
;
342 static int channel_offsets
[] = { 0, 0x1c, 0x38, 0x54, 0x58, 0x5c,
343 0x78, 0, 0x94, 0xb4};
346 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
350 mutex_init(&priv
->mutex
);
354 priv
->dc_reg
= devm_ioremap(dev
, base
, PAGE_SIZE
);
355 priv
->dc_tmpl_reg
= devm_ioremap(dev
, template_base
, PAGE_SIZE
);
356 if (!priv
->dc_reg
|| !priv
->dc_tmpl_reg
)
359 for (i
= 0; i
< IPU_DC_NUM_CHANNELS
; i
++) {
360 priv
->channels
[i
].chno
= i
;
361 priv
->channels
[i
].priv
= priv
;
362 priv
->channels
[i
].base
= priv
->dc_reg
+ channel_offsets
[i
];
365 writel(DC_WR_CH_CONF_WORD_SIZE_24
| DC_WR_CH_CONF_DISP_ID_PARALLEL(1) |
366 DC_WR_CH_CONF_PROG_DI_ID
,
367 priv
->channels
[1].base
+ DC_WR_CH_CONF
);
368 writel(DC_WR_CH_CONF_WORD_SIZE_24
| DC_WR_CH_CONF_DISP_ID_PARALLEL(0),
369 priv
->channels
[5].base
+ DC_WR_CH_CONF
);
371 writel(DC_GEN_SYNC_1_6_SYNC
| DC_GEN_SYNC_PRIORITY_1
,
372 priv
->dc_reg
+ DC_GEN
);
376 dev_dbg(dev
, "DC base: 0x%08lx template base: 0x%08lx\n",
377 base
, template_base
);
380 ipu_dc_map_clear(priv
, IPU_DC_MAP_RGB24
);
381 ipu_dc_map_config(priv
, IPU_DC_MAP_RGB24
, 0, 7, 0xff); /* blue */
382 ipu_dc_map_config(priv
, IPU_DC_MAP_RGB24
, 1, 15, 0xff); /* green */
383 ipu_dc_map_config(priv
, IPU_DC_MAP_RGB24
, 2, 23, 0xff); /* red */
386 ipu_dc_map_clear(priv
, IPU_DC_MAP_RGB565
);
387 ipu_dc_map_config(priv
, IPU_DC_MAP_RGB565
, 0, 4, 0xf8); /* blue */
388 ipu_dc_map_config(priv
, IPU_DC_MAP_RGB565
, 1, 10, 0xfc); /* green */
389 ipu_dc_map_config(priv
, IPU_DC_MAP_RGB565
, 2, 15, 0xf8); /* red */
392 ipu_dc_map_clear(priv
, IPU_DC_MAP_GBR24
);
393 ipu_dc_map_config(priv
, IPU_DC_MAP_GBR24
, 2, 15, 0xff); /* green */
394 ipu_dc_map_config(priv
, IPU_DC_MAP_GBR24
, 1, 7, 0xff); /* blue */
395 ipu_dc_map_config(priv
, IPU_DC_MAP_GBR24
, 0, 23, 0xff); /* red */
398 ipu_dc_map_clear(priv
, IPU_DC_MAP_BGR666
);
399 ipu_dc_map_config(priv
, IPU_DC_MAP_BGR666
, 0, 5, 0xfc); /* blue */
400 ipu_dc_map_config(priv
, IPU_DC_MAP_BGR666
, 1, 11, 0xfc); /* green */
401 ipu_dc_map_config(priv
, IPU_DC_MAP_BGR666
, 2, 17, 0xfc); /* red */
404 ipu_dc_map_clear(priv
, IPU_DC_MAP_LVDS666
);
405 ipu_dc_map_config(priv
, IPU_DC_MAP_LVDS666
, 0, 5, 0xfc); /* blue */
406 ipu_dc_map_config(priv
, IPU_DC_MAP_LVDS666
, 1, 13, 0xfc); /* green */
407 ipu_dc_map_config(priv
, IPU_DC_MAP_LVDS666
, 2, 21, 0xfc); /* red */
410 ipu_dc_map_clear(priv
, IPU_DC_MAP_BGR24
);
411 ipu_dc_map_config(priv
, IPU_DC_MAP_BGR24
, 2, 7, 0xff); /* red */
412 ipu_dc_map_config(priv
, IPU_DC_MAP_BGR24
, 1, 15, 0xff); /* green */
413 ipu_dc_map_config(priv
, IPU_DC_MAP_BGR24
, 0, 23, 0xff); /* blue */
418 void ipu_dc_exit(struct ipu_soc
*ipu
)