1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2019-2020 NXP
6 #include <linux/delay.h>
7 #include <linux/device.h>
9 #include <linux/types.h>
11 #include "imx8-isi-core.h"
12 #include "imx8-isi-regs.h"
14 #define ISI_DOWNSCALE_THRESHOLD 0x4000
16 static inline u32 mxc_isi_read(struct mxc_isi_pipe *pipe, u32 reg)
18 return readl(pipe->regs + reg);
21 static inline void mxc_isi_write(struct mxc_isi_pipe *pipe, u32 reg, u32 val)
23 writel(val, pipe->regs + reg);
26 /* -----------------------------------------------------------------------------
27 * Buffers & M2M operation
30 void mxc_isi_channel_set_inbuf(struct mxc_isi_pipe *pipe, dma_addr_t dma_addr)
32 mxc_isi_write(pipe, CHNL_IN_BUF_ADDR, lower_32_bits(dma_addr));
33 if (pipe->isi->pdata->has_36bit_dma)
34 mxc_isi_write(pipe, CHNL_IN_BUF_XTND_ADDR,
35 upper_32_bits(dma_addr));
38 void mxc_isi_channel_set_outbuf(struct mxc_isi_pipe *pipe,
39 const dma_addr_t dma_addrs[3],
40 enum mxc_isi_buf_id buf_id)
44 val = mxc_isi_read(pipe, CHNL_OUT_BUF_CTRL);
46 if (buf_id == MXC_ISI_BUF1) {
47 mxc_isi_write(pipe, CHNL_OUT_BUF1_ADDR_Y,
48 lower_32_bits(dma_addrs[0]));
49 mxc_isi_write(pipe, CHNL_OUT_BUF1_ADDR_U,
50 lower_32_bits(dma_addrs[1]));
51 mxc_isi_write(pipe, CHNL_OUT_BUF1_ADDR_V,
52 lower_32_bits(dma_addrs[2]));
53 if (pipe->isi->pdata->has_36bit_dma) {
54 mxc_isi_write(pipe, CHNL_Y_BUF1_XTND_ADDR,
55 upper_32_bits(dma_addrs[0]));
56 mxc_isi_write(pipe, CHNL_U_BUF1_XTND_ADDR,
57 upper_32_bits(dma_addrs[1]));
58 mxc_isi_write(pipe, CHNL_V_BUF1_XTND_ADDR,
59 upper_32_bits(dma_addrs[2]));
61 val ^= CHNL_OUT_BUF_CTRL_LOAD_BUF1_ADDR;
63 mxc_isi_write(pipe, CHNL_OUT_BUF2_ADDR_Y,
64 lower_32_bits(dma_addrs[0]));
65 mxc_isi_write(pipe, CHNL_OUT_BUF2_ADDR_U,
66 lower_32_bits(dma_addrs[1]));
67 mxc_isi_write(pipe, CHNL_OUT_BUF2_ADDR_V,
68 lower_32_bits(dma_addrs[2]));
69 if (pipe->isi->pdata->has_36bit_dma) {
70 mxc_isi_write(pipe, CHNL_Y_BUF2_XTND_ADDR,
71 upper_32_bits(dma_addrs[0]));
72 mxc_isi_write(pipe, CHNL_U_BUF2_XTND_ADDR,
73 upper_32_bits(dma_addrs[1]));
74 mxc_isi_write(pipe, CHNL_V_BUF2_XTND_ADDR,
75 upper_32_bits(dma_addrs[2]));
77 val ^= CHNL_OUT_BUF_CTRL_LOAD_BUF2_ADDR;
80 mxc_isi_write(pipe, CHNL_OUT_BUF_CTRL, val);
83 void mxc_isi_channel_m2m_start(struct mxc_isi_pipe *pipe)
87 val = mxc_isi_read(pipe, CHNL_MEM_RD_CTRL);
88 val &= ~CHNL_MEM_RD_CTRL_READ_MEM;
89 mxc_isi_write(pipe, CHNL_MEM_RD_CTRL, val);
93 val |= CHNL_MEM_RD_CTRL_READ_MEM;
94 mxc_isi_write(pipe, CHNL_MEM_RD_CTRL, val);
97 /* -----------------------------------------------------------------------------
98 * Pipeline configuration
101 static u32 mxc_isi_channel_scaling_ratio(unsigned int from, unsigned int to,
104 unsigned int ratio = from / to;
115 return min_t(u32, from * 0x1000 / (to * *dec), ISI_DOWNSCALE_THRESHOLD);
118 static void mxc_isi_channel_set_scaling(struct mxc_isi_pipe *pipe,
119 enum mxc_isi_encoding encoding,
120 const struct v4l2_area *in_size,
121 const struct v4l2_area *out_size,
128 dev_dbg(pipe->isi->dev, "input %ux%u, output %ux%u\n",
129 in_size->width, in_size->height,
130 out_size->width, out_size->height);
132 xscale = mxc_isi_channel_scaling_ratio(in_size->width, out_size->width,
134 yscale = mxc_isi_channel_scaling_ratio(in_size->height, out_size->height,
137 val = mxc_isi_read(pipe, CHNL_IMG_CTRL);
138 val &= ~(CHNL_IMG_CTRL_DEC_X_MASK | CHNL_IMG_CTRL_DEC_Y_MASK |
139 CHNL_IMG_CTRL_YCBCR_MODE);
141 val |= CHNL_IMG_CTRL_DEC_X(ilog2(decx))
142 | CHNL_IMG_CTRL_DEC_Y(ilog2(decy));
145 * Contrary to what the documentation states, YCBCR_MODE does not
146 * control conversion between YCbCr and RGB, but whether the scaler
147 * operates in YUV mode or in RGB mode. It must be set when the scaler
150 if (encoding == MXC_ISI_ENC_YUV)
151 val |= CHNL_IMG_CTRL_YCBCR_MODE;
153 mxc_isi_write(pipe, CHNL_IMG_CTRL, val);
155 mxc_isi_write(pipe, CHNL_SCALE_FACTOR,
156 CHNL_SCALE_FACTOR_Y_SCALE(yscale) |
157 CHNL_SCALE_FACTOR_X_SCALE(xscale));
159 mxc_isi_write(pipe, CHNL_SCALE_OFFSET, 0);
161 mxc_isi_write(pipe, CHNL_SCL_IMG_CFG,
162 CHNL_SCL_IMG_CFG_HEIGHT(out_size->height) |
163 CHNL_SCL_IMG_CFG_WIDTH(out_size->width));
165 *bypass = in_size->height == out_size->height &&
166 in_size->width == out_size->width;
169 static void mxc_isi_channel_set_crop(struct mxc_isi_pipe *pipe,
170 const struct v4l2_area *src,
171 const struct v4l2_rect *dst)
175 val = mxc_isi_read(pipe, CHNL_IMG_CTRL);
176 val &= ~CHNL_IMG_CTRL_CROP_EN;
178 if (src->height == dst->height && src->width == dst->width) {
179 mxc_isi_write(pipe, CHNL_IMG_CTRL, val);
183 val |= CHNL_IMG_CTRL_CROP_EN;
184 val0 = CHNL_CROP_ULC_X(dst->left) | CHNL_CROP_ULC_Y(dst->top);
185 val1 = CHNL_CROP_LRC_X(dst->width) | CHNL_CROP_LRC_Y(dst->height);
187 mxc_isi_write(pipe, CHNL_CROP_ULC, val0);
188 mxc_isi_write(pipe, CHNL_CROP_LRC, val1 + val0);
189 mxc_isi_write(pipe, CHNL_IMG_CTRL, val);
193 * A2,A1, B1, A3, B3, B2,
194 * C2, C1, D1, C3, D3, D2
196 static const u32 mxc_isi_yuv2rgb_coeffs[6] = {
198 0x0000012a, 0x012a0198, 0x0730079c,
199 0x0204012a, 0x01f00000, 0x01800180
202 static const u32 mxc_isi_rgb2yuv_coeffs[6] = {
204 0x00810041, 0x07db0019, 0x007007b6,
205 0x07a20070, 0x001007ee, 0x00800080
208 static void mxc_isi_channel_set_csc(struct mxc_isi_pipe *pipe,
209 enum mxc_isi_encoding in_encoding,
210 enum mxc_isi_encoding out_encoding,
213 static const char * const encodings[] = {
214 [MXC_ISI_ENC_RAW] = "RAW",
215 [MXC_ISI_ENC_RGB] = "RGB",
216 [MXC_ISI_ENC_YUV] = "YUV",
222 val = mxc_isi_read(pipe, CHNL_IMG_CTRL);
223 val &= ~(CHNL_IMG_CTRL_CSC_BYPASS | CHNL_IMG_CTRL_CSC_MODE_MASK);
225 if (in_encoding == MXC_ISI_ENC_YUV &&
226 out_encoding == MXC_ISI_ENC_RGB) {
228 coeffs = mxc_isi_yuv2rgb_coeffs;
229 /* YCbCr enable??? */
230 val |= CHNL_IMG_CTRL_CSC_MODE(CHNL_IMG_CTRL_CSC_MODE_YCBCR2RGB);
231 } else if (in_encoding == MXC_ISI_ENC_RGB &&
232 out_encoding == MXC_ISI_ENC_YUV) {
234 coeffs = mxc_isi_rgb2yuv_coeffs;
235 val |= CHNL_IMG_CTRL_CSC_MODE(CHNL_IMG_CTRL_CSC_MODE_RGB2YCBCR);
239 val |= CHNL_IMG_CTRL_CSC_BYPASS;
242 dev_dbg(pipe->isi->dev, "CSC: %s -> %s\n",
243 encodings[in_encoding], encodings[out_encoding]);
246 mxc_isi_write(pipe, CHNL_CSC_COEFF0, coeffs[0]);
247 mxc_isi_write(pipe, CHNL_CSC_COEFF1, coeffs[1]);
248 mxc_isi_write(pipe, CHNL_CSC_COEFF2, coeffs[2]);
249 mxc_isi_write(pipe, CHNL_CSC_COEFF3, coeffs[3]);
250 mxc_isi_write(pipe, CHNL_CSC_COEFF4, coeffs[4]);
251 mxc_isi_write(pipe, CHNL_CSC_COEFF5, coeffs[5]);
254 mxc_isi_write(pipe, CHNL_IMG_CTRL, val);
259 void mxc_isi_channel_set_alpha(struct mxc_isi_pipe *pipe, u8 alpha)
263 val = mxc_isi_read(pipe, CHNL_IMG_CTRL);
264 val &= ~CHNL_IMG_CTRL_GBL_ALPHA_VAL_MASK;
265 val |= CHNL_IMG_CTRL_GBL_ALPHA_VAL(alpha) |
266 CHNL_IMG_CTRL_GBL_ALPHA_EN;
267 mxc_isi_write(pipe, CHNL_IMG_CTRL, val);
270 void mxc_isi_channel_set_flip(struct mxc_isi_pipe *pipe, bool hflip, bool vflip)
274 val = mxc_isi_read(pipe, CHNL_IMG_CTRL);
275 val &= ~(CHNL_IMG_CTRL_VFLIP_EN | CHNL_IMG_CTRL_HFLIP_EN);
278 val |= CHNL_IMG_CTRL_VFLIP_EN;
280 val |= CHNL_IMG_CTRL_HFLIP_EN;
282 mxc_isi_write(pipe, CHNL_IMG_CTRL, val);
285 static void mxc_isi_channel_set_panic_threshold(struct mxc_isi_pipe *pipe)
287 const struct mxc_isi_set_thd *set_thd = pipe->isi->pdata->set_thd;
290 val = mxc_isi_read(pipe, CHNL_OUT_BUF_CTRL);
292 val &= ~(set_thd->panic_set_thd_y.mask);
293 val |= set_thd->panic_set_thd_y.threshold << set_thd->panic_set_thd_y.offset;
295 val &= ~(set_thd->panic_set_thd_u.mask);
296 val |= set_thd->panic_set_thd_u.threshold << set_thd->panic_set_thd_u.offset;
298 val &= ~(set_thd->panic_set_thd_v.mask);
299 val |= set_thd->panic_set_thd_v.threshold << set_thd->panic_set_thd_v.offset;
301 mxc_isi_write(pipe, CHNL_OUT_BUF_CTRL, val);
304 static void mxc_isi_channel_set_control(struct mxc_isi_pipe *pipe,
305 enum mxc_isi_input_id input,
310 mutex_lock(&pipe->lock);
312 val = mxc_isi_read(pipe, CHNL_CTRL);
313 val &= ~(CHNL_CTRL_CHNL_BYPASS | CHNL_CTRL_CHAIN_BUF_MASK |
314 CHNL_CTRL_BLANK_PXL_MASK | CHNL_CTRL_SRC_TYPE_MASK |
315 CHNL_CTRL_MIPI_VC_ID_MASK | CHNL_CTRL_SRC_INPUT_MASK);
318 * If no scaling or color space conversion is needed, bypass the
322 val |= CHNL_CTRL_CHNL_BYPASS;
324 /* Chain line buffers if needed. */
326 val |= CHNL_CTRL_CHAIN_BUF(CHNL_CTRL_CHAIN_BUF_2_CHAIN);
328 val |= CHNL_CTRL_BLANK_PXL(0xff);
330 /* Input source (including VC configuration for CSI-2) */
331 if (input == MXC_ISI_INPUT_MEM) {
333 * The memory input is connected to the last port of the
334 * crossbar switch, after all pixel link inputs. The SRC_INPUT
335 * field controls the input selection and must be set
336 * accordingly, despite being documented as ignored when using
337 * the memory input in the i.MX8MP reference manual, and
338 * reserved in the i.MX8MN reference manual.
340 val |= CHNL_CTRL_SRC_TYPE(CHNL_CTRL_SRC_TYPE_MEMORY);
341 val |= CHNL_CTRL_SRC_INPUT(pipe->isi->pdata->num_ports);
343 val |= CHNL_CTRL_SRC_TYPE(CHNL_CTRL_SRC_TYPE_DEVICE);
344 val |= CHNL_CTRL_SRC_INPUT(input);
345 val |= CHNL_CTRL_MIPI_VC_ID(0); /* FIXME: For CSI-2 only */
348 mxc_isi_write(pipe, CHNL_CTRL, val);
350 mutex_unlock(&pipe->lock);
353 void mxc_isi_channel_config(struct mxc_isi_pipe *pipe,
354 enum mxc_isi_input_id input,
355 const struct v4l2_area *in_size,
356 const struct v4l2_area *scale,
357 const struct v4l2_rect *crop,
358 enum mxc_isi_encoding in_encoding,
359 enum mxc_isi_encoding out_encoding)
364 /* Input frame size */
365 mxc_isi_write(pipe, CHNL_IMG_CFG,
366 CHNL_IMG_CFG_HEIGHT(in_size->height) |
367 CHNL_IMG_CFG_WIDTH(in_size->width));
370 mxc_isi_channel_set_scaling(pipe, in_encoding, in_size, scale,
372 mxc_isi_channel_set_crop(pipe, scale, crop);
375 mxc_isi_channel_set_csc(pipe, in_encoding, out_encoding, &csc_bypass);
377 /* Output buffer management */
378 mxc_isi_channel_set_panic_threshold(pipe);
380 /* Channel control */
381 mxc_isi_channel_set_control(pipe, input, csc_bypass && scaler_bypass);
384 void mxc_isi_channel_set_input_format(struct mxc_isi_pipe *pipe,
385 const struct mxc_isi_format_info *info,
386 const struct v4l2_pix_format_mplane *format)
388 unsigned int bpl = format->plane_fmt[0].bytesperline;
390 mxc_isi_write(pipe, CHNL_MEM_RD_CTRL,
391 CHNL_MEM_RD_CTRL_IMG_TYPE(info->isi_in_format));
392 mxc_isi_write(pipe, CHNL_IN_BUF_PITCH,
393 CHNL_IN_BUF_PITCH_LINE_PITCH(bpl));
396 void mxc_isi_channel_set_output_format(struct mxc_isi_pipe *pipe,
397 const struct mxc_isi_format_info *info,
398 struct v4l2_pix_format_mplane *format)
402 /* set outbuf format */
403 dev_dbg(pipe->isi->dev, "output format %p4cc", &format->pixelformat);
405 val = mxc_isi_read(pipe, CHNL_IMG_CTRL);
406 val &= ~CHNL_IMG_CTRL_FORMAT_MASK;
407 val |= CHNL_IMG_CTRL_FORMAT(info->isi_out_format);
408 mxc_isi_write(pipe, CHNL_IMG_CTRL, val);
411 mxc_isi_write(pipe, CHNL_OUT_BUF_PITCH,
412 format->plane_fmt[0].bytesperline);
415 /* -----------------------------------------------------------------------------
419 u32 mxc_isi_channel_irq_status(struct mxc_isi_pipe *pipe, bool clear)
423 status = mxc_isi_read(pipe, CHNL_STS);
425 mxc_isi_write(pipe, CHNL_STS, status);
430 void mxc_isi_channel_irq_clear(struct mxc_isi_pipe *pipe)
432 mxc_isi_write(pipe, CHNL_STS, 0xffffffff);
435 static void mxc_isi_channel_irq_enable(struct mxc_isi_pipe *pipe)
437 const struct mxc_isi_ier_reg *ier_reg = pipe->isi->pdata->ier_reg;
440 val = CHNL_IER_FRM_RCVD_EN |
441 CHNL_IER_AXI_WR_ERR_U_EN |
442 CHNL_IER_AXI_WR_ERR_V_EN |
443 CHNL_IER_AXI_WR_ERR_Y_EN;
445 /* Y/U/V overflow enable */
446 val |= ier_reg->oflw_y_buf_en.mask |
447 ier_reg->oflw_u_buf_en.mask |
448 ier_reg->oflw_v_buf_en.mask;
450 /* Y/U/V excess overflow enable */
451 val |= ier_reg->excs_oflw_y_buf_en.mask |
452 ier_reg->excs_oflw_u_buf_en.mask |
453 ier_reg->excs_oflw_v_buf_en.mask;
455 /* Y/U/V panic enable */
456 val |= ier_reg->panic_y_buf_en.mask |
457 ier_reg->panic_u_buf_en.mask |
458 ier_reg->panic_v_buf_en.mask;
460 mxc_isi_channel_irq_clear(pipe);
461 mxc_isi_write(pipe, CHNL_IER, val);
464 static void mxc_isi_channel_irq_disable(struct mxc_isi_pipe *pipe)
466 mxc_isi_write(pipe, CHNL_IER, 0);
469 /* -----------------------------------------------------------------------------
470 * Init, deinit, enable, disable
473 static void mxc_isi_channel_sw_reset(struct mxc_isi_pipe *pipe, bool enable_clk)
475 mxc_isi_write(pipe, CHNL_CTRL, CHNL_CTRL_SW_RST);
477 mxc_isi_write(pipe, CHNL_CTRL, enable_clk ? CHNL_CTRL_CLK_EN : 0);
480 static void __mxc_isi_channel_get(struct mxc_isi_pipe *pipe)
482 if (!pipe->use_count++)
483 mxc_isi_channel_sw_reset(pipe, true);
486 void mxc_isi_channel_get(struct mxc_isi_pipe *pipe)
488 mutex_lock(&pipe->lock);
489 __mxc_isi_channel_get(pipe);
490 mutex_unlock(&pipe->lock);
493 static void __mxc_isi_channel_put(struct mxc_isi_pipe *pipe)
495 if (!--pipe->use_count)
496 mxc_isi_channel_sw_reset(pipe, false);
499 void mxc_isi_channel_put(struct mxc_isi_pipe *pipe)
501 mutex_lock(&pipe->lock);
502 __mxc_isi_channel_put(pipe);
503 mutex_unlock(&pipe->lock);
506 void mxc_isi_channel_enable(struct mxc_isi_pipe *pipe)
510 mxc_isi_channel_irq_enable(pipe);
512 mutex_lock(&pipe->lock);
514 val = mxc_isi_read(pipe, CHNL_CTRL);
515 val |= CHNL_CTRL_CHNL_EN;
516 mxc_isi_write(pipe, CHNL_CTRL, val);
518 mutex_unlock(&pipe->lock);
521 void mxc_isi_channel_disable(struct mxc_isi_pipe *pipe)
525 mxc_isi_channel_irq_disable(pipe);
527 mutex_lock(&pipe->lock);
529 val = mxc_isi_read(pipe, CHNL_CTRL);
530 val &= ~CHNL_CTRL_CHNL_EN;
531 mxc_isi_write(pipe, CHNL_CTRL, val);
533 mutex_unlock(&pipe->lock);
536 /* -----------------------------------------------------------------------------
537 * Resource management & chaining
539 int mxc_isi_channel_acquire(struct mxc_isi_pipe *pipe,
540 mxc_isi_pipe_irq_t irq_handler, bool bypass)
545 mutex_lock(&pipe->lock);
547 if (pipe->irq_handler) {
553 * Make sure the resources we need are available. The output buffer is
554 * always needed to operate the channel, the line buffer is needed only
555 * when the channel isn't in bypass mode.
557 resources = MXC_ISI_CHANNEL_RES_OUTPUT_BUF
558 | (!bypass ? MXC_ISI_CHANNEL_RES_LINE_BUF : 0);
559 if ((pipe->available_res & resources) != resources) {
564 /* Acquire the channel resources. */
565 pipe->acquired_res = resources;
566 pipe->available_res &= ~resources;
567 pipe->irq_handler = irq_handler;
570 mutex_unlock(&pipe->lock);
575 void mxc_isi_channel_release(struct mxc_isi_pipe *pipe)
577 mutex_lock(&pipe->lock);
579 pipe->irq_handler = NULL;
580 pipe->available_res |= pipe->acquired_res;
581 pipe->acquired_res = 0;
583 mutex_unlock(&pipe->lock);
587 * We currently support line buffer chaining only, for handling images with a
588 * width larger than 2048 pixels.
590 * TODO: Support secondary line buffer for downscaling YUV420 images.
592 int mxc_isi_channel_chain(struct mxc_isi_pipe *pipe, bool bypass)
594 /* Channel chaining requires both line and output buffer. */
595 const u8 resources = MXC_ISI_CHANNEL_RES_OUTPUT_BUF
596 | MXC_ISI_CHANNEL_RES_LINE_BUF;
597 struct mxc_isi_pipe *chained_pipe = pipe + 1;
601 * If buffer chaining is required, make sure this channel is not the
602 * last one, otherwise there's no 'next' channel to chain with. This
603 * should be prevented by checks in the set format handlers, but let's
606 if (WARN_ON(pipe->id == pipe->isi->pdata->num_channels - 1))
609 mutex_lock(&chained_pipe->lock);
612 if (WARN_ON(pipe->chained || chained_pipe->chained_res)) {
617 if ((chained_pipe->available_res & resources) != resources) {
622 pipe->chained = true;
623 chained_pipe->chained_res |= resources;
624 chained_pipe->available_res &= ~resources;
626 __mxc_isi_channel_get(chained_pipe);
629 mutex_unlock(&chained_pipe->lock);
634 void mxc_isi_channel_unchain(struct mxc_isi_pipe *pipe)
636 struct mxc_isi_pipe *chained_pipe = pipe + 1;
641 pipe->chained = false;
643 mutex_lock(&chained_pipe->lock);
645 chained_pipe->available_res |= chained_pipe->chained_res;
646 chained_pipe->chained_res = 0;
648 __mxc_isi_channel_put(chained_pipe);
650 mutex_unlock(&chained_pipe->lock);