2 * Copyright (c) 2010 Sascha Hauer <s.hauer@pengutronix.de>
3 * Copyright (C) 2005-2009 Freescale Semiconductor, Inc.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 #include <linux/module.h>
16 #include <linux/export.h>
17 #include <linux/types.h>
18 #include <linux/reset.h>
19 #include <linux/platform_device.h>
20 #include <linux/err.h>
21 #include <linux/spinlock.h>
22 #include <linux/delay.h>
23 #include <linux/interrupt.h>
25 #include <linux/clk.h>
26 #include <linux/list.h>
27 #include <linux/irq.h>
28 #include <linux/irqchip/chained_irq.h>
29 #include <linux/irqdomain.h>
30 #include <linux/of_device.h>
32 #include <drm/drm_fourcc.h>
34 #include <video/imx-ipu-v3.h>
37 static inline u32
ipu_cm_read(struct ipu_soc
*ipu
, unsigned offset
)
39 return readl(ipu
->cm_reg
+ offset
);
42 static inline void ipu_cm_write(struct ipu_soc
*ipu
, u32 value
, unsigned offset
)
44 writel(value
, ipu
->cm_reg
+ offset
);
47 void ipu_srm_dp_sync_update(struct ipu_soc
*ipu
)
51 val
= ipu_cm_read(ipu
, IPU_SRM_PRI2
);
53 ipu_cm_write(ipu
, val
, IPU_SRM_PRI2
);
55 EXPORT_SYMBOL_GPL(ipu_srm_dp_sync_update
);
57 enum ipu_color_space
ipu_drm_fourcc_to_colorspace(u32 drm_fourcc
)
60 case DRM_FORMAT_ARGB1555
:
61 case DRM_FORMAT_ABGR1555
:
62 case DRM_FORMAT_RGBA5551
:
63 case DRM_FORMAT_BGRA5551
:
64 case DRM_FORMAT_RGB565
:
65 case DRM_FORMAT_BGR565
:
66 case DRM_FORMAT_RGB888
:
67 case DRM_FORMAT_BGR888
:
68 case DRM_FORMAT_XRGB8888
:
69 case DRM_FORMAT_XBGR8888
:
70 case DRM_FORMAT_RGBX8888
:
71 case DRM_FORMAT_BGRX8888
:
72 case DRM_FORMAT_ARGB8888
:
73 case DRM_FORMAT_ABGR8888
:
74 case DRM_FORMAT_RGBA8888
:
75 case DRM_FORMAT_BGRA8888
:
76 return IPUV3_COLORSPACE_RGB
;
79 case DRM_FORMAT_YUV420
:
80 case DRM_FORMAT_YVU420
:
81 case DRM_FORMAT_YUV422
:
82 case DRM_FORMAT_YVU422
:
87 return IPUV3_COLORSPACE_YUV
;
89 return IPUV3_COLORSPACE_UNKNOWN
;
92 EXPORT_SYMBOL_GPL(ipu_drm_fourcc_to_colorspace
);
94 enum ipu_color_space
ipu_pixelformat_to_colorspace(u32 pixelformat
)
96 switch (pixelformat
) {
97 case V4L2_PIX_FMT_YUV420
:
98 case V4L2_PIX_FMT_YVU420
:
99 case V4L2_PIX_FMT_YUV422P
:
100 case V4L2_PIX_FMT_UYVY
:
101 case V4L2_PIX_FMT_YUYV
:
102 case V4L2_PIX_FMT_NV12
:
103 case V4L2_PIX_FMT_NV21
:
104 case V4L2_PIX_FMT_NV16
:
105 case V4L2_PIX_FMT_NV61
:
106 return IPUV3_COLORSPACE_YUV
;
107 case V4L2_PIX_FMT_RGB32
:
108 case V4L2_PIX_FMT_BGR32
:
109 case V4L2_PIX_FMT_RGB24
:
110 case V4L2_PIX_FMT_BGR24
:
111 case V4L2_PIX_FMT_RGB565
:
112 return IPUV3_COLORSPACE_RGB
;
114 return IPUV3_COLORSPACE_UNKNOWN
;
117 EXPORT_SYMBOL_GPL(ipu_pixelformat_to_colorspace
);
119 bool ipu_pixelformat_is_planar(u32 pixelformat
)
121 switch (pixelformat
) {
122 case V4L2_PIX_FMT_YUV420
:
123 case V4L2_PIX_FMT_YVU420
:
124 case V4L2_PIX_FMT_YUV422P
:
125 case V4L2_PIX_FMT_NV12
:
126 case V4L2_PIX_FMT_NV21
:
127 case V4L2_PIX_FMT_NV16
:
128 case V4L2_PIX_FMT_NV61
:
134 EXPORT_SYMBOL_GPL(ipu_pixelformat_is_planar
);
136 enum ipu_color_space
ipu_mbus_code_to_colorspace(u32 mbus_code
)
138 switch (mbus_code
& 0xf000) {
140 return IPUV3_COLORSPACE_RGB
;
142 return IPUV3_COLORSPACE_YUV
;
144 return IPUV3_COLORSPACE_UNKNOWN
;
147 EXPORT_SYMBOL_GPL(ipu_mbus_code_to_colorspace
);
149 int ipu_stride_to_bytes(u32 pixel_stride
, u32 pixelformat
)
151 switch (pixelformat
) {
152 case V4L2_PIX_FMT_YUV420
:
153 case V4L2_PIX_FMT_YVU420
:
154 case V4L2_PIX_FMT_YUV422P
:
155 case V4L2_PIX_FMT_NV12
:
156 case V4L2_PIX_FMT_NV21
:
157 case V4L2_PIX_FMT_NV16
:
158 case V4L2_PIX_FMT_NV61
:
160 * for the planar YUV formats, the stride passed to
161 * cpmem must be the stride in bytes of the Y plane.
162 * And all the planar YUV formats have an 8-bit
165 return (8 * pixel_stride
) >> 3;
166 case V4L2_PIX_FMT_RGB565
:
167 case V4L2_PIX_FMT_YUYV
:
168 case V4L2_PIX_FMT_UYVY
:
169 return (16 * pixel_stride
) >> 3;
170 case V4L2_PIX_FMT_BGR24
:
171 case V4L2_PIX_FMT_RGB24
:
172 return (24 * pixel_stride
) >> 3;
173 case V4L2_PIX_FMT_BGR32
:
174 case V4L2_PIX_FMT_RGB32
:
175 return (32 * pixel_stride
) >> 3;
182 EXPORT_SYMBOL_GPL(ipu_stride_to_bytes
);
184 int ipu_degrees_to_rot_mode(enum ipu_rotate_mode
*mode
, int degrees
,
185 bool hflip
, bool vflip
)
211 *mode
= (enum ipu_rotate_mode
)((r90
<< 2) | (hf
<< 1) | vf
);
214 EXPORT_SYMBOL_GPL(ipu_degrees_to_rot_mode
);
216 int ipu_rot_mode_to_degrees(int *degrees
, enum ipu_rotate_mode mode
,
217 bool hflip
, bool vflip
)
221 r90
= ((u32
)mode
>> 2) & 0x1;
222 hf
= ((u32
)mode
>> 1) & 0x1;
223 vf
= ((u32
)mode
>> 0) & 0x1;
227 switch ((enum ipu_rotate_mode
)((r90
<< 2) | (hf
<< 1) | vf
)) {
228 case IPU_ROTATE_NONE
:
231 case IPU_ROTATE_90_RIGHT
:
237 case IPU_ROTATE_90_LEFT
:
246 EXPORT_SYMBOL_GPL(ipu_rot_mode_to_degrees
);
248 struct ipuv3_channel
*ipu_idmac_get(struct ipu_soc
*ipu
, unsigned num
)
250 struct ipuv3_channel
*channel
;
252 dev_dbg(ipu
->dev
, "%s %d\n", __func__
, num
);
255 return ERR_PTR(-ENODEV
);
257 mutex_lock(&ipu
->channel_lock
);
259 channel
= &ipu
->channel
[num
];
262 channel
= ERR_PTR(-EBUSY
);
266 channel
->busy
= true;
270 mutex_unlock(&ipu
->channel_lock
);
274 EXPORT_SYMBOL_GPL(ipu_idmac_get
);
276 void ipu_idmac_put(struct ipuv3_channel
*channel
)
278 struct ipu_soc
*ipu
= channel
->ipu
;
280 dev_dbg(ipu
->dev
, "%s %d\n", __func__
, channel
->num
);
282 mutex_lock(&ipu
->channel_lock
);
284 channel
->busy
= false;
286 mutex_unlock(&ipu
->channel_lock
);
288 EXPORT_SYMBOL_GPL(ipu_idmac_put
);
290 #define idma_mask(ch) (1 << ((ch) & 0x1f))
293 * This is an undocumented feature, a write one to a channel bit in
294 * IPU_CHA_CUR_BUF and IPU_CHA_TRIPLE_CUR_BUF will reset the channel's
295 * internal current buffer pointer so that transfers start from buffer
296 * 0 on the next channel enable (that's the theory anyway, the imx6 TRM
297 * only says these are read-only registers). This operation is required
298 * for channel linking to work correctly, for instance video capture
299 * pipelines that carry out image rotations will fail after the first
300 * streaming unless this function is called for each channel before
301 * re-enabling the channels.
303 static void __ipu_idmac_reset_current_buffer(struct ipuv3_channel
*channel
)
305 struct ipu_soc
*ipu
= channel
->ipu
;
306 unsigned int chno
= channel
->num
;
308 ipu_cm_write(ipu
, idma_mask(chno
), IPU_CHA_CUR_BUF(chno
));
311 void ipu_idmac_set_double_buffer(struct ipuv3_channel
*channel
,
314 struct ipu_soc
*ipu
= channel
->ipu
;
318 spin_lock_irqsave(&ipu
->lock
, flags
);
320 reg
= ipu_cm_read(ipu
, IPU_CHA_DB_MODE_SEL(channel
->num
));
322 reg
|= idma_mask(channel
->num
);
324 reg
&= ~idma_mask(channel
->num
);
325 ipu_cm_write(ipu
, reg
, IPU_CHA_DB_MODE_SEL(channel
->num
));
327 __ipu_idmac_reset_current_buffer(channel
);
329 spin_unlock_irqrestore(&ipu
->lock
, flags
);
331 EXPORT_SYMBOL_GPL(ipu_idmac_set_double_buffer
);
333 static const struct {
337 } idmac_lock_en_info
[] = {
338 { .chnum
= 5, .reg
= IDMAC_CH_LOCK_EN_1
, .shift
= 0, },
339 { .chnum
= 11, .reg
= IDMAC_CH_LOCK_EN_1
, .shift
= 2, },
340 { .chnum
= 12, .reg
= IDMAC_CH_LOCK_EN_1
, .shift
= 4, },
341 { .chnum
= 14, .reg
= IDMAC_CH_LOCK_EN_1
, .shift
= 6, },
342 { .chnum
= 15, .reg
= IDMAC_CH_LOCK_EN_1
, .shift
= 8, },
343 { .chnum
= 20, .reg
= IDMAC_CH_LOCK_EN_1
, .shift
= 10, },
344 { .chnum
= 21, .reg
= IDMAC_CH_LOCK_EN_1
, .shift
= 12, },
345 { .chnum
= 22, .reg
= IDMAC_CH_LOCK_EN_1
, .shift
= 14, },
346 { .chnum
= 23, .reg
= IDMAC_CH_LOCK_EN_1
, .shift
= 16, },
347 { .chnum
= 27, .reg
= IDMAC_CH_LOCK_EN_1
, .shift
= 18, },
348 { .chnum
= 28, .reg
= IDMAC_CH_LOCK_EN_1
, .shift
= 20, },
349 { .chnum
= 45, .reg
= IDMAC_CH_LOCK_EN_2
, .shift
= 0, },
350 { .chnum
= 46, .reg
= IDMAC_CH_LOCK_EN_2
, .shift
= 2, },
351 { .chnum
= 47, .reg
= IDMAC_CH_LOCK_EN_2
, .shift
= 4, },
352 { .chnum
= 48, .reg
= IDMAC_CH_LOCK_EN_2
, .shift
= 6, },
353 { .chnum
= 49, .reg
= IDMAC_CH_LOCK_EN_2
, .shift
= 8, },
354 { .chnum
= 50, .reg
= IDMAC_CH_LOCK_EN_2
, .shift
= 10, },
357 int ipu_idmac_lock_enable(struct ipuv3_channel
*channel
, int num_bursts
)
359 struct ipu_soc
*ipu
= channel
->ipu
;
364 switch (num_bursts
) {
367 bursts
= 0x00; /* locking disabled */
382 for (i
= 0; i
< ARRAY_SIZE(idmac_lock_en_info
); i
++) {
383 if (channel
->num
== idmac_lock_en_info
[i
].chnum
)
386 if (i
>= ARRAY_SIZE(idmac_lock_en_info
))
389 spin_lock_irqsave(&ipu
->lock
, flags
);
391 regval
= ipu_idmac_read(ipu
, idmac_lock_en_info
[i
].reg
);
392 regval
&= ~(0x03 << idmac_lock_en_info
[i
].shift
);
393 regval
|= (bursts
<< idmac_lock_en_info
[i
].shift
);
394 ipu_idmac_write(ipu
, regval
, idmac_lock_en_info
[i
].reg
);
396 spin_unlock_irqrestore(&ipu
->lock
, flags
);
400 EXPORT_SYMBOL_GPL(ipu_idmac_lock_enable
);
402 int ipu_module_enable(struct ipu_soc
*ipu
, u32 mask
)
404 unsigned long lock_flags
;
407 spin_lock_irqsave(&ipu
->lock
, lock_flags
);
409 val
= ipu_cm_read(ipu
, IPU_DISP_GEN
);
411 if (mask
& IPU_CONF_DI0_EN
)
412 val
|= IPU_DI0_COUNTER_RELEASE
;
413 if (mask
& IPU_CONF_DI1_EN
)
414 val
|= IPU_DI1_COUNTER_RELEASE
;
416 ipu_cm_write(ipu
, val
, IPU_DISP_GEN
);
418 val
= ipu_cm_read(ipu
, IPU_CONF
);
420 ipu_cm_write(ipu
, val
, IPU_CONF
);
422 spin_unlock_irqrestore(&ipu
->lock
, lock_flags
);
426 EXPORT_SYMBOL_GPL(ipu_module_enable
);
428 int ipu_module_disable(struct ipu_soc
*ipu
, u32 mask
)
430 unsigned long lock_flags
;
433 spin_lock_irqsave(&ipu
->lock
, lock_flags
);
435 val
= ipu_cm_read(ipu
, IPU_CONF
);
437 ipu_cm_write(ipu
, val
, IPU_CONF
);
439 val
= ipu_cm_read(ipu
, IPU_DISP_GEN
);
441 if (mask
& IPU_CONF_DI0_EN
)
442 val
&= ~IPU_DI0_COUNTER_RELEASE
;
443 if (mask
& IPU_CONF_DI1_EN
)
444 val
&= ~IPU_DI1_COUNTER_RELEASE
;
446 ipu_cm_write(ipu
, val
, IPU_DISP_GEN
);
448 spin_unlock_irqrestore(&ipu
->lock
, lock_flags
);
452 EXPORT_SYMBOL_GPL(ipu_module_disable
);
454 int ipu_idmac_get_current_buffer(struct ipuv3_channel
*channel
)
456 struct ipu_soc
*ipu
= channel
->ipu
;
457 unsigned int chno
= channel
->num
;
459 return (ipu_cm_read(ipu
, IPU_CHA_CUR_BUF(chno
)) & idma_mask(chno
)) ? 1 : 0;
461 EXPORT_SYMBOL_GPL(ipu_idmac_get_current_buffer
);
463 bool ipu_idmac_buffer_is_ready(struct ipuv3_channel
*channel
, u32 buf_num
)
465 struct ipu_soc
*ipu
= channel
->ipu
;
469 spin_lock_irqsave(&ipu
->lock
, flags
);
472 reg
= ipu_cm_read(ipu
, IPU_CHA_BUF0_RDY(channel
->num
));
475 reg
= ipu_cm_read(ipu
, IPU_CHA_BUF1_RDY(channel
->num
));
478 reg
= ipu_cm_read(ipu
, IPU_CHA_BUF2_RDY(channel
->num
));
481 spin_unlock_irqrestore(&ipu
->lock
, flags
);
483 return ((reg
& idma_mask(channel
->num
)) != 0);
485 EXPORT_SYMBOL_GPL(ipu_idmac_buffer_is_ready
);
487 void ipu_idmac_select_buffer(struct ipuv3_channel
*channel
, u32 buf_num
)
489 struct ipu_soc
*ipu
= channel
->ipu
;
490 unsigned int chno
= channel
->num
;
493 spin_lock_irqsave(&ipu
->lock
, flags
);
495 /* Mark buffer as ready. */
497 ipu_cm_write(ipu
, idma_mask(chno
), IPU_CHA_BUF0_RDY(chno
));
499 ipu_cm_write(ipu
, idma_mask(chno
), IPU_CHA_BUF1_RDY(chno
));
501 spin_unlock_irqrestore(&ipu
->lock
, flags
);
503 EXPORT_SYMBOL_GPL(ipu_idmac_select_buffer
);
505 void ipu_idmac_clear_buffer(struct ipuv3_channel
*channel
, u32 buf_num
)
507 struct ipu_soc
*ipu
= channel
->ipu
;
508 unsigned int chno
= channel
->num
;
511 spin_lock_irqsave(&ipu
->lock
, flags
);
513 ipu_cm_write(ipu
, 0xF0300000, IPU_GPR
); /* write one to clear */
516 ipu_cm_write(ipu
, idma_mask(chno
), IPU_CHA_BUF0_RDY(chno
));
519 ipu_cm_write(ipu
, idma_mask(chno
), IPU_CHA_BUF1_RDY(chno
));
522 ipu_cm_write(ipu
, idma_mask(chno
), IPU_CHA_BUF2_RDY(chno
));
527 ipu_cm_write(ipu
, 0x0, IPU_GPR
); /* write one to set */
529 spin_unlock_irqrestore(&ipu
->lock
, flags
);
531 EXPORT_SYMBOL_GPL(ipu_idmac_clear_buffer
);
533 int ipu_idmac_enable_channel(struct ipuv3_channel
*channel
)
535 struct ipu_soc
*ipu
= channel
->ipu
;
539 spin_lock_irqsave(&ipu
->lock
, flags
);
541 val
= ipu_idmac_read(ipu
, IDMAC_CHA_EN(channel
->num
));
542 val
|= idma_mask(channel
->num
);
543 ipu_idmac_write(ipu
, val
, IDMAC_CHA_EN(channel
->num
));
545 spin_unlock_irqrestore(&ipu
->lock
, flags
);
549 EXPORT_SYMBOL_GPL(ipu_idmac_enable_channel
);
551 bool ipu_idmac_channel_busy(struct ipu_soc
*ipu
, unsigned int chno
)
553 return (ipu_idmac_read(ipu
, IDMAC_CHA_BUSY(chno
)) & idma_mask(chno
));
555 EXPORT_SYMBOL_GPL(ipu_idmac_channel_busy
);
557 int ipu_idmac_wait_busy(struct ipuv3_channel
*channel
, int ms
)
559 struct ipu_soc
*ipu
= channel
->ipu
;
560 unsigned long timeout
;
562 timeout
= jiffies
+ msecs_to_jiffies(ms
);
563 while (ipu_idmac_read(ipu
, IDMAC_CHA_BUSY(channel
->num
)) &
564 idma_mask(channel
->num
)) {
565 if (time_after(jiffies
, timeout
))
572 EXPORT_SYMBOL_GPL(ipu_idmac_wait_busy
);
574 int ipu_wait_interrupt(struct ipu_soc
*ipu
, int irq
, int ms
)
576 unsigned long timeout
;
578 timeout
= jiffies
+ msecs_to_jiffies(ms
);
579 ipu_cm_write(ipu
, BIT(irq
% 32), IPU_INT_STAT(irq
/ 32));
580 while (!(ipu_cm_read(ipu
, IPU_INT_STAT(irq
/ 32) & BIT(irq
% 32)))) {
581 if (time_after(jiffies
, timeout
))
588 EXPORT_SYMBOL_GPL(ipu_wait_interrupt
);
590 int ipu_idmac_disable_channel(struct ipuv3_channel
*channel
)
592 struct ipu_soc
*ipu
= channel
->ipu
;
596 spin_lock_irqsave(&ipu
->lock
, flags
);
598 /* Disable DMA channel(s) */
599 val
= ipu_idmac_read(ipu
, IDMAC_CHA_EN(channel
->num
));
600 val
&= ~idma_mask(channel
->num
);
601 ipu_idmac_write(ipu
, val
, IDMAC_CHA_EN(channel
->num
));
603 __ipu_idmac_reset_current_buffer(channel
);
605 /* Set channel buffers NOT to be ready */
606 ipu_cm_write(ipu
, 0xf0000000, IPU_GPR
); /* write one to clear */
608 if (ipu_cm_read(ipu
, IPU_CHA_BUF0_RDY(channel
->num
)) &
609 idma_mask(channel
->num
)) {
610 ipu_cm_write(ipu
, idma_mask(channel
->num
),
611 IPU_CHA_BUF0_RDY(channel
->num
));
614 if (ipu_cm_read(ipu
, IPU_CHA_BUF1_RDY(channel
->num
)) &
615 idma_mask(channel
->num
)) {
616 ipu_cm_write(ipu
, idma_mask(channel
->num
),
617 IPU_CHA_BUF1_RDY(channel
->num
));
620 ipu_cm_write(ipu
, 0x0, IPU_GPR
); /* write one to set */
622 /* Reset the double buffer */
623 val
= ipu_cm_read(ipu
, IPU_CHA_DB_MODE_SEL(channel
->num
));
624 val
&= ~idma_mask(channel
->num
);
625 ipu_cm_write(ipu
, val
, IPU_CHA_DB_MODE_SEL(channel
->num
));
627 spin_unlock_irqrestore(&ipu
->lock
, flags
);
631 EXPORT_SYMBOL_GPL(ipu_idmac_disable_channel
);
634 * The imx6 rev. D TRM says that enabling the WM feature will increase
635 * a channel's priority. Refer to Table 36-8 Calculated priority value.
636 * The sub-module that is the sink or source for the channel must enable
637 * watermark signal for this to take effect (SMFC_WM for instance).
639 void ipu_idmac_enable_watermark(struct ipuv3_channel
*channel
, bool enable
)
641 struct ipu_soc
*ipu
= channel
->ipu
;
645 spin_lock_irqsave(&ipu
->lock
, flags
);
647 val
= ipu_idmac_read(ipu
, IDMAC_WM_EN(channel
->num
));
649 val
|= 1 << (channel
->num
% 32);
651 val
&= ~(1 << (channel
->num
% 32));
652 ipu_idmac_write(ipu
, val
, IDMAC_WM_EN(channel
->num
));
654 spin_unlock_irqrestore(&ipu
->lock
, flags
);
656 EXPORT_SYMBOL_GPL(ipu_idmac_enable_watermark
);
658 static int ipu_memory_reset(struct ipu_soc
*ipu
)
660 unsigned long timeout
;
662 ipu_cm_write(ipu
, 0x807FFFFF, IPU_MEM_RST
);
664 timeout
= jiffies
+ msecs_to_jiffies(1000);
665 while (ipu_cm_read(ipu
, IPU_MEM_RST
) & 0x80000000) {
666 if (time_after(jiffies
, timeout
))
675 * Set the source mux for the given CSI. Selects either parallel or
678 void ipu_set_csi_src_mux(struct ipu_soc
*ipu
, int csi_id
, bool mipi_csi2
)
683 mask
= (csi_id
== 1) ? IPU_CONF_CSI1_DATA_SOURCE
:
684 IPU_CONF_CSI0_DATA_SOURCE
;
686 spin_lock_irqsave(&ipu
->lock
, flags
);
688 val
= ipu_cm_read(ipu
, IPU_CONF
);
693 ipu_cm_write(ipu
, val
, IPU_CONF
);
695 spin_unlock_irqrestore(&ipu
->lock
, flags
);
697 EXPORT_SYMBOL_GPL(ipu_set_csi_src_mux
);
700 * Set the source mux for the IC. Selects either CSI[01] or the VDI.
702 void ipu_set_ic_src_mux(struct ipu_soc
*ipu
, int csi_id
, bool vdi
)
707 spin_lock_irqsave(&ipu
->lock
, flags
);
709 val
= ipu_cm_read(ipu
, IPU_CONF
);
711 val
|= IPU_CONF_IC_INPUT
;
713 val
&= ~IPU_CONF_IC_INPUT
;
715 val
|= IPU_CONF_CSI_SEL
;
717 val
&= ~IPU_CONF_CSI_SEL
;
719 ipu_cm_write(ipu
, val
, IPU_CONF
);
721 spin_unlock_irqrestore(&ipu
->lock
, flags
);
723 EXPORT_SYMBOL_GPL(ipu_set_ic_src_mux
);
727 unsigned long cm_ofs
;
728 unsigned long cpmem_ofs
;
729 unsigned long srm_ofs
;
730 unsigned long tpm_ofs
;
731 unsigned long csi0_ofs
;
732 unsigned long csi1_ofs
;
733 unsigned long ic_ofs
;
734 unsigned long disp0_ofs
;
735 unsigned long disp1_ofs
;
736 unsigned long dc_tmpl_ofs
;
737 unsigned long vdi_ofs
;
738 enum ipuv3_type type
;
741 static struct ipu_devtype ipu_type_imx51
= {
743 .cm_ofs
= 0x1e000000,
744 .cpmem_ofs
= 0x1f000000,
745 .srm_ofs
= 0x1f040000,
746 .tpm_ofs
= 0x1f060000,
747 .csi0_ofs
= 0x1f030000,
748 .csi1_ofs
= 0x1f038000,
749 .ic_ofs
= 0x1e020000,
750 .disp0_ofs
= 0x1e040000,
751 .disp1_ofs
= 0x1e048000,
752 .dc_tmpl_ofs
= 0x1f080000,
753 .vdi_ofs
= 0x1e068000,
757 static struct ipu_devtype ipu_type_imx53
= {
759 .cm_ofs
= 0x06000000,
760 .cpmem_ofs
= 0x07000000,
761 .srm_ofs
= 0x07040000,
762 .tpm_ofs
= 0x07060000,
763 .csi0_ofs
= 0x07030000,
764 .csi1_ofs
= 0x07038000,
765 .ic_ofs
= 0x06020000,
766 .disp0_ofs
= 0x06040000,
767 .disp1_ofs
= 0x06048000,
768 .dc_tmpl_ofs
= 0x07080000,
769 .vdi_ofs
= 0x06068000,
773 static struct ipu_devtype ipu_type_imx6q
= {
775 .cm_ofs
= 0x00200000,
776 .cpmem_ofs
= 0x00300000,
777 .srm_ofs
= 0x00340000,
778 .tpm_ofs
= 0x00360000,
779 .csi0_ofs
= 0x00230000,
780 .csi1_ofs
= 0x00238000,
781 .ic_ofs
= 0x00220000,
782 .disp0_ofs
= 0x00240000,
783 .disp1_ofs
= 0x00248000,
784 .dc_tmpl_ofs
= 0x00380000,
785 .vdi_ofs
= 0x00268000,
789 static const struct of_device_id imx_ipu_dt_ids
[] = {
790 { .compatible
= "fsl,imx51-ipu", .data
= &ipu_type_imx51
, },
791 { .compatible
= "fsl,imx53-ipu", .data
= &ipu_type_imx53
, },
792 { .compatible
= "fsl,imx6q-ipu", .data
= &ipu_type_imx6q
, },
795 MODULE_DEVICE_TABLE(of
, imx_ipu_dt_ids
);
797 static int ipu_submodules_init(struct ipu_soc
*ipu
,
798 struct platform_device
*pdev
, unsigned long ipu_base
,
803 struct device
*dev
= &pdev
->dev
;
804 const struct ipu_devtype
*devtype
= ipu
->devtype
;
806 ret
= ipu_cpmem_init(ipu
, dev
, ipu_base
+ devtype
->cpmem_ofs
);
812 ret
= ipu_csi_init(ipu
, dev
, 0, ipu_base
+ devtype
->csi0_ofs
,
813 IPU_CONF_CSI0_EN
, ipu_clk
);
819 ret
= ipu_csi_init(ipu
, dev
, 1, ipu_base
+ devtype
->csi1_ofs
,
820 IPU_CONF_CSI1_EN
, ipu_clk
);
826 ret
= ipu_ic_init(ipu
, dev
,
827 ipu_base
+ devtype
->ic_ofs
,
828 ipu_base
+ devtype
->tpm_ofs
);
834 ret
= ipu_di_init(ipu
, dev
, 0, ipu_base
+ devtype
->disp0_ofs
,
835 IPU_CONF_DI0_EN
, ipu_clk
);
841 ret
= ipu_di_init(ipu
, dev
, 1, ipu_base
+ devtype
->disp1_ofs
,
842 IPU_CONF_DI1_EN
, ipu_clk
);
848 ret
= ipu_dc_init(ipu
, dev
, ipu_base
+ devtype
->cm_ofs
+
849 IPU_CM_DC_REG_OFS
, ipu_base
+ devtype
->dc_tmpl_ofs
);
851 unit
= "dc_template";
855 ret
= ipu_dmfc_init(ipu
, dev
, ipu_base
+
856 devtype
->cm_ofs
+ IPU_CM_DMFC_REG_OFS
, ipu_clk
);
862 ret
= ipu_dp_init(ipu
, dev
, ipu_base
+ devtype
->srm_ofs
);
868 ret
= ipu_smfc_init(ipu
, dev
, ipu_base
+
869 devtype
->cm_ofs
+ IPU_CM_SMFC_REG_OFS
);
890 ipu_csi_exit(ipu
, 1);
892 ipu_csi_exit(ipu
, 0);
896 dev_err(&pdev
->dev
, "init %s failed with %d\n", unit
, ret
);
900 static void ipu_irq_handle(struct ipu_soc
*ipu
, const int *regs
, int num_regs
)
902 unsigned long status
;
905 for (i
= 0; i
< num_regs
; i
++) {
907 status
= ipu_cm_read(ipu
, IPU_INT_STAT(regs
[i
]));
908 status
&= ipu_cm_read(ipu
, IPU_INT_CTRL(regs
[i
]));
910 for_each_set_bit(bit
, &status
, 32) {
911 irq
= irq_linear_revmap(ipu
->domain
,
914 generic_handle_irq(irq
);
919 static void ipu_irq_handler(struct irq_desc
*desc
)
921 struct ipu_soc
*ipu
= irq_desc_get_handler_data(desc
);
922 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
923 const int int_reg
[] = { 0, 1, 2, 3, 10, 11, 12, 13, 14};
925 chained_irq_enter(chip
, desc
);
927 ipu_irq_handle(ipu
, int_reg
, ARRAY_SIZE(int_reg
));
929 chained_irq_exit(chip
, desc
);
932 static void ipu_err_irq_handler(struct irq_desc
*desc
)
934 struct ipu_soc
*ipu
= irq_desc_get_handler_data(desc
);
935 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
936 const int int_reg
[] = { 4, 5, 8, 9};
938 chained_irq_enter(chip
, desc
);
940 ipu_irq_handle(ipu
, int_reg
, ARRAY_SIZE(int_reg
));
942 chained_irq_exit(chip
, desc
);
945 int ipu_map_irq(struct ipu_soc
*ipu
, int irq
)
949 virq
= irq_linear_revmap(ipu
->domain
, irq
);
951 virq
= irq_create_mapping(ipu
->domain
, irq
);
955 EXPORT_SYMBOL_GPL(ipu_map_irq
);
957 int ipu_idmac_channel_irq(struct ipu_soc
*ipu
, struct ipuv3_channel
*channel
,
958 enum ipu_channel_irq irq_type
)
960 return ipu_map_irq(ipu
, irq_type
+ channel
->num
);
962 EXPORT_SYMBOL_GPL(ipu_idmac_channel_irq
);
964 static void ipu_submodules_exit(struct ipu_soc
*ipu
)
973 ipu_csi_exit(ipu
, 1);
974 ipu_csi_exit(ipu
, 0);
978 static int platform_remove_devices_fn(struct device
*dev
, void *unused
)
980 struct platform_device
*pdev
= to_platform_device(dev
);
982 platform_device_unregister(pdev
);
987 static void platform_device_unregister_children(struct platform_device
*pdev
)
989 device_for_each_child(&pdev
->dev
, NULL
, platform_remove_devices_fn
);
992 struct ipu_platform_reg
{
993 struct ipu_client_platformdata pdata
;
998 static const struct ipu_platform_reg client_reg
[] = {
1003 .dp
= IPU_DP_FLOW_SYNC_BG
,
1004 .dma
[0] = IPUV3_CHANNEL_MEM_BG_SYNC
,
1005 .dma
[1] = IPUV3_CHANNEL_MEM_FG_SYNC
,
1007 .name
= "imx-ipuv3-crtc",
1013 .dma
[0] = IPUV3_CHANNEL_MEM_DC_SYNC
,
1016 .name
= "imx-ipuv3-crtc",
1020 .dma
[0] = IPUV3_CHANNEL_CSI0
,
1023 .reg_offset
= IPU_CM_CSI0_REG_OFS
,
1024 .name
= "imx-ipuv3-camera",
1028 .dma
[0] = IPUV3_CHANNEL_CSI1
,
1031 .reg_offset
= IPU_CM_CSI1_REG_OFS
,
1032 .name
= "imx-ipuv3-camera",
1036 static DEFINE_MUTEX(ipu_client_id_mutex
);
1037 static int ipu_client_id
;
1039 static int ipu_add_client_devices(struct ipu_soc
*ipu
, unsigned long ipu_base
)
1041 struct device
*dev
= ipu
->dev
;
1045 mutex_lock(&ipu_client_id_mutex
);
1047 ipu_client_id
+= ARRAY_SIZE(client_reg
);
1048 mutex_unlock(&ipu_client_id_mutex
);
1050 for (i
= 0; i
< ARRAY_SIZE(client_reg
); i
++) {
1051 const struct ipu_platform_reg
*reg
= &client_reg
[i
];
1052 struct platform_device
*pdev
;
1053 struct resource res
;
1055 if (reg
->reg_offset
) {
1056 memset(&res
, 0, sizeof(res
));
1057 res
.flags
= IORESOURCE_MEM
;
1058 res
.start
= ipu_base
+ ipu
->devtype
->cm_ofs
+ reg
->reg_offset
;
1059 res
.end
= res
.start
+ PAGE_SIZE
- 1;
1060 pdev
= platform_device_register_resndata(dev
, reg
->name
,
1061 id
++, &res
, 1, ®
->pdata
, sizeof(reg
->pdata
));
1063 pdev
= platform_device_register_data(dev
, reg
->name
,
1064 id
++, ®
->pdata
, sizeof(reg
->pdata
));
1068 ret
= PTR_ERR(pdev
);
1076 platform_device_unregister_children(to_platform_device(dev
));
1082 static int ipu_irq_init(struct ipu_soc
*ipu
)
1084 struct irq_chip_generic
*gc
;
1085 struct irq_chip_type
*ct
;
1086 unsigned long unused
[IPU_NUM_IRQS
/ 32] = {
1087 0x400100d0, 0xffe000fd,
1088 0x400100d0, 0xffe000fd,
1089 0x400100d0, 0xffe000fd,
1090 0x4077ffff, 0xffe7e1fd,
1091 0x23fffffe, 0x8880fff0,
1092 0xf98fe7d0, 0xfff81fff,
1093 0x400100d0, 0xffe000fd,
1098 ipu
->domain
= irq_domain_add_linear(ipu
->dev
->of_node
, IPU_NUM_IRQS
,
1099 &irq_generic_chip_ops
, ipu
);
1101 dev_err(ipu
->dev
, "failed to add irq domain\n");
1105 ret
= irq_alloc_domain_generic_chips(ipu
->domain
, 32, 1, "IPU",
1106 handle_level_irq
, 0, 0, 0);
1108 dev_err(ipu
->dev
, "failed to alloc generic irq chips\n");
1109 irq_domain_remove(ipu
->domain
);
1113 for (i
= 0; i
< IPU_NUM_IRQS
; i
+= 32)
1114 ipu_cm_write(ipu
, 0, IPU_INT_CTRL(i
/ 32));
1116 for (i
= 0; i
< IPU_NUM_IRQS
; i
+= 32) {
1117 gc
= irq_get_domain_generic_chip(ipu
->domain
, i
);
1118 gc
->reg_base
= ipu
->cm_reg
;
1119 gc
->unused
= unused
[i
/ 32];
1120 ct
= gc
->chip_types
;
1121 ct
->chip
.irq_ack
= irq_gc_ack_set_bit
;
1122 ct
->chip
.irq_mask
= irq_gc_mask_clr_bit
;
1123 ct
->chip
.irq_unmask
= irq_gc_mask_set_bit
;
1124 ct
->regs
.ack
= IPU_INT_STAT(i
/ 32);
1125 ct
->regs
.mask
= IPU_INT_CTRL(i
/ 32);
1128 irq_set_chained_handler_and_data(ipu
->irq_sync
, ipu_irq_handler
, ipu
);
1129 irq_set_chained_handler_and_data(ipu
->irq_err
, ipu_err_irq_handler
,
1135 static void ipu_irq_exit(struct ipu_soc
*ipu
)
1139 irq_set_chained_handler_and_data(ipu
->irq_err
, NULL
, NULL
);
1140 irq_set_chained_handler_and_data(ipu
->irq_sync
, NULL
, NULL
);
1142 /* TODO: remove irq_domain_generic_chips */
1144 for (i
= 0; i
< IPU_NUM_IRQS
; i
++) {
1145 irq
= irq_linear_revmap(ipu
->domain
, i
);
1147 irq_dispose_mapping(irq
);
1150 irq_domain_remove(ipu
->domain
);
1153 void ipu_dump(struct ipu_soc
*ipu
)
1157 dev_dbg(ipu
->dev
, "IPU_CONF = \t0x%08X\n",
1158 ipu_cm_read(ipu
, IPU_CONF
));
1159 dev_dbg(ipu
->dev
, "IDMAC_CONF = \t0x%08X\n",
1160 ipu_idmac_read(ipu
, IDMAC_CONF
));
1161 dev_dbg(ipu
->dev
, "IDMAC_CHA_EN1 = \t0x%08X\n",
1162 ipu_idmac_read(ipu
, IDMAC_CHA_EN(0)));
1163 dev_dbg(ipu
->dev
, "IDMAC_CHA_EN2 = \t0x%08X\n",
1164 ipu_idmac_read(ipu
, IDMAC_CHA_EN(32)));
1165 dev_dbg(ipu
->dev
, "IDMAC_CHA_PRI1 = \t0x%08X\n",
1166 ipu_idmac_read(ipu
, IDMAC_CHA_PRI(0)));
1167 dev_dbg(ipu
->dev
, "IDMAC_CHA_PRI2 = \t0x%08X\n",
1168 ipu_idmac_read(ipu
, IDMAC_CHA_PRI(32)));
1169 dev_dbg(ipu
->dev
, "IDMAC_BAND_EN1 = \t0x%08X\n",
1170 ipu_idmac_read(ipu
, IDMAC_BAND_EN(0)));
1171 dev_dbg(ipu
->dev
, "IDMAC_BAND_EN2 = \t0x%08X\n",
1172 ipu_idmac_read(ipu
, IDMAC_BAND_EN(32)));
1173 dev_dbg(ipu
->dev
, "IPU_CHA_DB_MODE_SEL0 = \t0x%08X\n",
1174 ipu_cm_read(ipu
, IPU_CHA_DB_MODE_SEL(0)));
1175 dev_dbg(ipu
->dev
, "IPU_CHA_DB_MODE_SEL1 = \t0x%08X\n",
1176 ipu_cm_read(ipu
, IPU_CHA_DB_MODE_SEL(32)));
1177 dev_dbg(ipu
->dev
, "IPU_FS_PROC_FLOW1 = \t0x%08X\n",
1178 ipu_cm_read(ipu
, IPU_FS_PROC_FLOW1
));
1179 dev_dbg(ipu
->dev
, "IPU_FS_PROC_FLOW2 = \t0x%08X\n",
1180 ipu_cm_read(ipu
, IPU_FS_PROC_FLOW2
));
1181 dev_dbg(ipu
->dev
, "IPU_FS_PROC_FLOW3 = \t0x%08X\n",
1182 ipu_cm_read(ipu
, IPU_FS_PROC_FLOW3
));
1183 dev_dbg(ipu
->dev
, "IPU_FS_DISP_FLOW1 = \t0x%08X\n",
1184 ipu_cm_read(ipu
, IPU_FS_DISP_FLOW1
));
1185 for (i
= 0; i
< 15; i
++)
1186 dev_dbg(ipu
->dev
, "IPU_INT_CTRL(%d) = \t%08X\n", i
,
1187 ipu_cm_read(ipu
, IPU_INT_CTRL(i
)));
1189 EXPORT_SYMBOL_GPL(ipu_dump
);
1191 static int ipu_probe(struct platform_device
*pdev
)
1193 const struct of_device_id
*of_id
=
1194 of_match_device(imx_ipu_dt_ids
, &pdev
->dev
);
1195 struct ipu_soc
*ipu
;
1196 struct resource
*res
;
1197 unsigned long ipu_base
;
1198 int i
, ret
, irq_sync
, irq_err
;
1199 const struct ipu_devtype
*devtype
;
1201 devtype
= of_id
->data
;
1203 irq_sync
= platform_get_irq(pdev
, 0);
1204 irq_err
= platform_get_irq(pdev
, 1);
1205 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1207 dev_dbg(&pdev
->dev
, "irq_sync: %d irq_err: %d\n",
1210 if (!res
|| irq_sync
< 0 || irq_err
< 0)
1213 ipu_base
= res
->start
;
1215 ipu
= devm_kzalloc(&pdev
->dev
, sizeof(*ipu
), GFP_KERNEL
);
1219 for (i
= 0; i
< 64; i
++)
1220 ipu
->channel
[i
].ipu
= ipu
;
1221 ipu
->devtype
= devtype
;
1222 ipu
->ipu_type
= devtype
->type
;
1224 spin_lock_init(&ipu
->lock
);
1225 mutex_init(&ipu
->channel_lock
);
1227 dev_dbg(&pdev
->dev
, "cm_reg: 0x%08lx\n",
1228 ipu_base
+ devtype
->cm_ofs
);
1229 dev_dbg(&pdev
->dev
, "idmac: 0x%08lx\n",
1230 ipu_base
+ devtype
->cm_ofs
+ IPU_CM_IDMAC_REG_OFS
);
1231 dev_dbg(&pdev
->dev
, "cpmem: 0x%08lx\n",
1232 ipu_base
+ devtype
->cpmem_ofs
);
1233 dev_dbg(&pdev
->dev
, "csi0: 0x%08lx\n",
1234 ipu_base
+ devtype
->csi0_ofs
);
1235 dev_dbg(&pdev
->dev
, "csi1: 0x%08lx\n",
1236 ipu_base
+ devtype
->csi1_ofs
);
1237 dev_dbg(&pdev
->dev
, "ic: 0x%08lx\n",
1238 ipu_base
+ devtype
->ic_ofs
);
1239 dev_dbg(&pdev
->dev
, "disp0: 0x%08lx\n",
1240 ipu_base
+ devtype
->disp0_ofs
);
1241 dev_dbg(&pdev
->dev
, "disp1: 0x%08lx\n",
1242 ipu_base
+ devtype
->disp1_ofs
);
1243 dev_dbg(&pdev
->dev
, "srm: 0x%08lx\n",
1244 ipu_base
+ devtype
->srm_ofs
);
1245 dev_dbg(&pdev
->dev
, "tpm: 0x%08lx\n",
1246 ipu_base
+ devtype
->tpm_ofs
);
1247 dev_dbg(&pdev
->dev
, "dc: 0x%08lx\n",
1248 ipu_base
+ devtype
->cm_ofs
+ IPU_CM_DC_REG_OFS
);
1249 dev_dbg(&pdev
->dev
, "ic: 0x%08lx\n",
1250 ipu_base
+ devtype
->cm_ofs
+ IPU_CM_IC_REG_OFS
);
1251 dev_dbg(&pdev
->dev
, "dmfc: 0x%08lx\n",
1252 ipu_base
+ devtype
->cm_ofs
+ IPU_CM_DMFC_REG_OFS
);
1253 dev_dbg(&pdev
->dev
, "vdi: 0x%08lx\n",
1254 ipu_base
+ devtype
->vdi_ofs
);
1256 ipu
->cm_reg
= devm_ioremap(&pdev
->dev
,
1257 ipu_base
+ devtype
->cm_ofs
, PAGE_SIZE
);
1258 ipu
->idmac_reg
= devm_ioremap(&pdev
->dev
,
1259 ipu_base
+ devtype
->cm_ofs
+ IPU_CM_IDMAC_REG_OFS
,
1262 if (!ipu
->cm_reg
|| !ipu
->idmac_reg
)
1265 ipu
->clk
= devm_clk_get(&pdev
->dev
, "bus");
1266 if (IS_ERR(ipu
->clk
)) {
1267 ret
= PTR_ERR(ipu
->clk
);
1268 dev_err(&pdev
->dev
, "clk_get failed with %d", ret
);
1272 platform_set_drvdata(pdev
, ipu
);
1274 ret
= clk_prepare_enable(ipu
->clk
);
1276 dev_err(&pdev
->dev
, "clk_prepare_enable failed: %d\n", ret
);
1280 ipu
->dev
= &pdev
->dev
;
1281 ipu
->irq_sync
= irq_sync
;
1282 ipu
->irq_err
= irq_err
;
1284 ret
= ipu_irq_init(ipu
);
1286 goto out_failed_irq
;
1288 ret
= device_reset(&pdev
->dev
);
1290 dev_err(&pdev
->dev
, "failed to reset: %d\n", ret
);
1291 goto out_failed_reset
;
1293 ret
= ipu_memory_reset(ipu
);
1295 goto out_failed_reset
;
1297 /* Set MCU_T to divide MCU access window into 2 */
1298 ipu_cm_write(ipu
, 0x00400000L
| (IPU_MCU_T_DEFAULT
<< 18),
1301 ret
= ipu_submodules_init(ipu
, pdev
, ipu_base
, ipu
->clk
);
1303 goto failed_submodules_init
;
1305 ret
= ipu_add_client_devices(ipu
, ipu_base
);
1307 dev_err(&pdev
->dev
, "adding client devices failed with %d\n",
1309 goto failed_add_clients
;
1312 dev_info(&pdev
->dev
, "%s probed\n", devtype
->name
);
1317 ipu_submodules_exit(ipu
);
1318 failed_submodules_init
:
1322 clk_disable_unprepare(ipu
->clk
);
1326 static int ipu_remove(struct platform_device
*pdev
)
1328 struct ipu_soc
*ipu
= platform_get_drvdata(pdev
);
1330 platform_device_unregister_children(pdev
);
1331 ipu_submodules_exit(ipu
);
1334 clk_disable_unprepare(ipu
->clk
);
1339 static struct platform_driver imx_ipu_driver
= {
1341 .name
= "imx-ipuv3",
1342 .of_match_table
= imx_ipu_dt_ids
,
1345 .remove
= ipu_remove
,
1348 module_platform_driver(imx_ipu_driver
);
1350 MODULE_ALIAS("platform:imx-ipuv3");
1351 MODULE_DESCRIPTION("i.MX IPU v3 driver");
1352 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
1353 MODULE_LICENSE("GPL");