1 // SPDX-License-Identifier: GPL-2.0
3 // Ingenic JZ47xx IPU driver
5 // Copyright (C) 2020, Paul Cercueil <paul@crapouillou.net>
6 // Copyright (C) 2020, Daniel Silsby <dansilsby@gmail.com>
8 #include "ingenic-drm.h"
9 #include "ingenic-ipu.h"
11 #include <linux/clk.h>
12 #include <linux/component.h>
13 #include <linux/gcd.h>
14 #include <linux/interrupt.h>
15 #include <linux/module.h>
17 #include <linux/of_device.h>
18 #include <linux/regmap.h>
19 #include <linux/time.h>
21 #include <drm/drm_atomic.h>
22 #include <drm/drm_atomic_helper.h>
23 #include <drm/drm_drv.h>
24 #include <drm/drm_fb_cma_helper.h>
25 #include <drm/drm_fourcc.h>
26 #include <drm/drm_gem_framebuffer_helper.h>
27 #include <drm/drm_plane.h>
28 #include <drm/drm_plane_helper.h>
29 #include <drm/drm_property.h>
30 #include <drm/drm_vblank.h>
40 void (*set_coefs
)(struct ingenic_ipu
*ipu
, unsigned int reg
,
41 unsigned int sharpness
, bool downscale
,
42 unsigned int weight
, unsigned int offset
);
46 struct drm_plane plane
;
47 struct drm_device
*drm
;
48 struct device
*dev
, *master
;
51 const struct soc_info
*soc_info
;
54 unsigned int num_w
, num_h
, denom_w
, denom_h
;
56 dma_addr_t addr_y
, addr_u
, addr_v
;
58 struct drm_property
*sharpness_prop
;
59 unsigned int sharpness
;
62 /* Signed 15.16 fixed-point math (for bicubic scaling coefficients) */
63 #define I2F(i) ((s32)(i) * 65536)
64 #define F2I(f) ((f) / 65536)
65 #define FMUL(fa, fb) ((s32)(((s64)(fa) * (s64)(fb)) / 65536))
66 #define SHARPNESS_INCR (I2F(-1) / 8)
68 static inline struct ingenic_ipu
*plane_to_ingenic_ipu(struct drm_plane
*plane
)
70 return container_of(plane
, struct ingenic_ipu
, plane
);
74 * Apply conventional cubic convolution kernel. Both parameters
75 * and return value are 15.16 signed fixed-point.
77 * @f_a: Sharpness factor, typically in range [-4.0, -0.25].
78 * A larger magnitude increases perceived sharpness, but going past
79 * -2.0 might cause ringing artifacts to outweigh any improvement.
80 * Nice values on a 320x240 LCD are between -0.75 and -2.0.
82 * @f_x: Absolute distance in pixels from 'pixel 0' sample position
83 * along horizontal (or vertical) source axis. Range is [0, +2.0].
85 * returns: Weight of this pixel within 4-pixel sample group. Range is
86 * [-2.0, +2.0]. For moderate (i.e. > -3.0) sharpness factors,
87 * range is within [-1.0, +1.0].
89 static inline s32
cubic_conv(s32 f_a
, s32 f_x
)
91 const s32 f_1
= I2F(1);
92 const s32 f_2
= I2F(2);
93 const s32 f_3
= I2F(3);
94 const s32 f_4
= I2F(4);
95 const s32 f_x2
= FMUL(f_x
, f_x
);
96 const s32 f_x3
= FMUL(f_x
, f_x2
);
99 return FMUL((f_a
+ f_2
), f_x3
) - FMUL((f_a
+ f_3
), f_x2
) + f_1
;
101 return FMUL(f_a
, (f_x3
- 5 * f_x2
+ 8 * f_x
- f_4
));
107 * On entry, "weight" is a coefficient suitable for bilinear mode,
108 * which is converted to a set of four suitable for bicubic mode.
110 * "weight 512" means all of pixel 0;
111 * "weight 256" means half of pixel 0 and half of pixel 1;
112 * "weight 0" means all of pixel 1;
114 * "offset" is increment to next source pixel sample location.
116 static void jz4760_set_coefs(struct ingenic_ipu
*ipu
, unsigned int reg
,
117 unsigned int sharpness
, bool downscale
,
118 unsigned int weight
, unsigned int offset
)
121 s32 w0
, w1
, w2
, w3
; /* Pixel weights at X (or Y) offsets -1,0,1,2 */
123 weight
= clamp_val(weight
, 0, 512);
127 * When sharpness setting is 0, emulate nearest-neighbor.
128 * When sharpness setting is 1, emulate bilinear.
132 weight
= weight
>= 256 ? 512 : 0;
138 const s32 f_a
= SHARPNESS_INCR
* sharpness
;
139 const s32 f_h
= I2F(1) / 2; /* Round up 0.5 */
142 * Note that always rounding towards +infinity here is intended.
143 * The resulting coefficients match a round-to-nearest-int
144 * double floating-point implementation.
147 weight
= 512 - weight
;
148 w0
= F2I(f_h
+ 512 * cubic_conv(f_a
, I2F(512 + weight
) / 512));
149 w1
= F2I(f_h
+ 512 * cubic_conv(f_a
, I2F(0 + weight
) / 512));
150 w2
= F2I(f_h
+ 512 * cubic_conv(f_a
, I2F(512 - weight
) / 512));
151 w3
= F2I(f_h
+ 512 * cubic_conv(f_a
, I2F(1024 - weight
) / 512));
152 w0
= clamp_val(w0
, -1024, 1023);
153 w1
= clamp_val(w1
, -1024, 1023);
154 w2
= clamp_val(w2
, -1024, 1023);
155 w3
= clamp_val(w3
, -1024, 1023);
158 val
= ((w1
& JZ4760_IPU_RSZ_COEF_MASK
) << JZ4760_IPU_RSZ_COEF31_LSB
) |
159 ((w0
& JZ4760_IPU_RSZ_COEF_MASK
) << JZ4760_IPU_RSZ_COEF20_LSB
);
160 regmap_write(ipu
->map
, reg
, val
);
162 val
= ((w3
& JZ4760_IPU_RSZ_COEF_MASK
) << JZ4760_IPU_RSZ_COEF31_LSB
) |
163 ((w2
& JZ4760_IPU_RSZ_COEF_MASK
) << JZ4760_IPU_RSZ_COEF20_LSB
) |
164 ((offset
& JZ4760_IPU_RSZ_OFFSET_MASK
) << JZ4760_IPU_RSZ_OFFSET_LSB
);
165 regmap_write(ipu
->map
, reg
, val
);
168 static void jz4725b_set_coefs(struct ingenic_ipu
*ipu
, unsigned int reg
,
169 unsigned int sharpness
, bool downscale
,
170 unsigned int weight
, unsigned int offset
)
172 u32 val
= JZ4725B_IPU_RSZ_LUT_OUT_EN
;
175 weight
= clamp_val(weight
, 0, 512);
178 weight
= weight
>= 256 ? 512 : 0;
180 val
|= (weight
& JZ4725B_IPU_RSZ_LUT_COEF_MASK
) << JZ4725B_IPU_RSZ_LUT_COEF_LSB
;
181 if (downscale
|| !!offset
)
182 val
|= JZ4725B_IPU_RSZ_LUT_IN_EN
;
184 regmap_write(ipu
->map
, reg
, val
);
187 for (i
= 1; i
< offset
; i
++)
188 regmap_write(ipu
->map
, reg
, JZ4725B_IPU_RSZ_LUT_IN_EN
);
192 static void ingenic_ipu_set_downscale_coefs(struct ingenic_ipu
*ipu
,
197 unsigned int i
, offset
, weight
, weight_num
= denom
;
199 for (i
= 0; i
< num
; i
++) {
200 weight_num
= num
+ (weight_num
- num
) % (num
* 2);
201 weight
= 512 - 512 * (weight_num
- num
) / (num
* 2);
202 weight_num
+= denom
* 2;
203 offset
= (weight_num
- num
) / (num
* 2);
205 ipu
->soc_info
->set_coefs(ipu
, reg
, ipu
->sharpness
,
206 true, weight
, offset
);
210 static void ingenic_ipu_set_integer_upscale_coefs(struct ingenic_ipu
*ipu
,
215 * Force nearest-neighbor scaling and use simple math when upscaling
216 * by an integer ratio. It looks better, and fixes a few problem cases.
220 for (i
= 0; i
< num
; i
++)
221 ipu
->soc_info
->set_coefs(ipu
, reg
, 0, false, 512, i
== num
- 1);
224 static void ingenic_ipu_set_upscale_coefs(struct ingenic_ipu
*ipu
,
229 unsigned int i
, offset
, weight
, weight_num
= 0;
231 for (i
= 0; i
< num
; i
++) {
232 weight
= 512 - 512 * weight_num
/ num
;
234 offset
= weight_num
>= num
;
239 ipu
->soc_info
->set_coefs(ipu
, reg
, ipu
->sharpness
,
240 false, weight
, offset
);
244 static void ingenic_ipu_set_coefs(struct ingenic_ipu
*ipu
, unsigned int reg
,
245 unsigned int num
, unsigned int denom
)
247 /* Begin programming the LUT */
248 regmap_write(ipu
->map
, reg
, -1);
251 ingenic_ipu_set_downscale_coefs(ipu
, reg
, num
, denom
);
253 ingenic_ipu_set_integer_upscale_coefs(ipu
, reg
, num
);
255 ingenic_ipu_set_upscale_coefs(ipu
, reg
, num
, denom
);
258 static int reduce_fraction(unsigned int *num
, unsigned int *denom
)
260 unsigned long d
= gcd(*num
, *denom
);
262 /* The scaling table has only 31 entries */
271 static inline bool osd_changed(struct drm_plane_state
*state
,
272 struct drm_plane_state
*oldstate
)
274 return state
->src_x
!= oldstate
->src_x
||
275 state
->src_y
!= oldstate
->src_y
||
276 state
->src_w
!= oldstate
->src_w
||
277 state
->src_h
!= oldstate
->src_h
||
278 state
->crtc_x
!= oldstate
->crtc_x
||
279 state
->crtc_y
!= oldstate
->crtc_y
||
280 state
->crtc_w
!= oldstate
->crtc_w
||
281 state
->crtc_h
!= oldstate
->crtc_h
;
284 static void ingenic_ipu_plane_atomic_update(struct drm_plane
*plane
,
285 struct drm_plane_state
*oldstate
)
287 struct ingenic_ipu
*ipu
= plane_to_ingenic_ipu(plane
);
288 struct drm_plane_state
*state
= plane
->state
;
289 const struct drm_format_info
*finfo
;
290 u32 ctrl
, stride
= 0, coef_index
= 0, format
= 0;
291 bool needs_modeset
, upscaling_w
, upscaling_h
;
294 if (!state
|| !state
->fb
)
297 finfo
= drm_format_info(state
->fb
->format
->format
);
299 if (!ipu
->clk_enabled
) {
300 err
= clk_enable(ipu
->clk
);
302 dev_err(ipu
->dev
, "Unable to enable clock: %d\n", err
);
306 ipu
->clk_enabled
= true;
309 /* Reset all the registers if needed */
310 needs_modeset
= drm_atomic_crtc_needs_modeset(state
->crtc
->state
);
312 regmap_set_bits(ipu
->map
, JZ_REG_IPU_CTRL
, JZ_IPU_CTRL_RST
);
314 /* Enable the chip */
315 regmap_set_bits(ipu
->map
, JZ_REG_IPU_CTRL
,
316 JZ_IPU_CTRL_CHIP_EN
| JZ_IPU_CTRL_LCDC_SEL
);
319 /* New addresses will be committed in vblank handler... */
320 ipu
->addr_y
= drm_fb_cma_get_gem_addr(state
->fb
, state
, 0);
321 if (finfo
->num_planes
> 1)
322 ipu
->addr_u
= drm_fb_cma_get_gem_addr(state
->fb
, state
, 1);
323 if (finfo
->num_planes
> 2)
324 ipu
->addr_v
= drm_fb_cma_get_gem_addr(state
->fb
, state
, 2);
329 /* Or right here if we're doing a full modeset. */
330 regmap_write(ipu
->map
, JZ_REG_IPU_Y_ADDR
, ipu
->addr_y
);
331 regmap_write(ipu
->map
, JZ_REG_IPU_U_ADDR
, ipu
->addr_u
);
332 regmap_write(ipu
->map
, JZ_REG_IPU_V_ADDR
, ipu
->addr_v
);
334 if (finfo
->num_planes
== 1)
335 regmap_set_bits(ipu
->map
, JZ_REG_IPU_CTRL
, JZ_IPU_CTRL_SPKG_SEL
);
337 ingenic_drm_plane_config(ipu
->master
, plane
, DRM_FORMAT_XRGB8888
);
339 /* Set the input height/width/strides */
340 if (finfo
->num_planes
> 2)
341 stride
= ((state
->src_w
>> 16) * finfo
->cpp
[2] / finfo
->hsub
)
342 << JZ_IPU_UV_STRIDE_V_LSB
;
344 if (finfo
->num_planes
> 1)
345 stride
|= ((state
->src_w
>> 16) * finfo
->cpp
[1] / finfo
->hsub
)
346 << JZ_IPU_UV_STRIDE_U_LSB
;
348 regmap_write(ipu
->map
, JZ_REG_IPU_UV_STRIDE
, stride
);
350 stride
= ((state
->src_w
>> 16) * finfo
->cpp
[0]) << JZ_IPU_Y_STRIDE_Y_LSB
;
351 regmap_write(ipu
->map
, JZ_REG_IPU_Y_STRIDE
, stride
);
353 regmap_write(ipu
->map
, JZ_REG_IPU_IN_GS
,
354 (stride
<< JZ_IPU_IN_GS_W_LSB
) |
355 ((state
->src_h
>> 16) << JZ_IPU_IN_GS_H_LSB
));
357 switch (finfo
->format
) {
358 case DRM_FORMAT_XRGB1555
:
359 format
= JZ_IPU_D_FMT_IN_FMT_RGB555
|
360 JZ_IPU_D_FMT_RGB_OUT_OFT_RGB
;
362 case DRM_FORMAT_XBGR1555
:
363 format
= JZ_IPU_D_FMT_IN_FMT_RGB555
|
364 JZ_IPU_D_FMT_RGB_OUT_OFT_BGR
;
366 case DRM_FORMAT_RGB565
:
367 format
= JZ_IPU_D_FMT_IN_FMT_RGB565
|
368 JZ_IPU_D_FMT_RGB_OUT_OFT_RGB
;
370 case DRM_FORMAT_BGR565
:
371 format
= JZ_IPU_D_FMT_IN_FMT_RGB565
|
372 JZ_IPU_D_FMT_RGB_OUT_OFT_BGR
;
374 case DRM_FORMAT_XRGB8888
:
375 case DRM_FORMAT_XYUV8888
:
376 format
= JZ_IPU_D_FMT_IN_FMT_RGB888
|
377 JZ_IPU_D_FMT_RGB_OUT_OFT_RGB
;
379 case DRM_FORMAT_XBGR8888
:
380 format
= JZ_IPU_D_FMT_IN_FMT_RGB888
|
381 JZ_IPU_D_FMT_RGB_OUT_OFT_BGR
;
383 case DRM_FORMAT_YUYV
:
384 format
= JZ_IPU_D_FMT_IN_FMT_YUV422
|
385 JZ_IPU_D_FMT_YUV_VY1UY0
;
387 case DRM_FORMAT_YVYU
:
388 format
= JZ_IPU_D_FMT_IN_FMT_YUV422
|
389 JZ_IPU_D_FMT_YUV_UY1VY0
;
391 case DRM_FORMAT_UYVY
:
392 format
= JZ_IPU_D_FMT_IN_FMT_YUV422
|
393 JZ_IPU_D_FMT_YUV_Y1VY0U
;
395 case DRM_FORMAT_VYUY
:
396 format
= JZ_IPU_D_FMT_IN_FMT_YUV422
|
397 JZ_IPU_D_FMT_YUV_Y1UY0V
;
399 case DRM_FORMAT_YUV411
:
400 format
= JZ_IPU_D_FMT_IN_FMT_YUV411
;
402 case DRM_FORMAT_YUV420
:
403 format
= JZ_IPU_D_FMT_IN_FMT_YUV420
;
405 case DRM_FORMAT_YUV422
:
406 format
= JZ_IPU_D_FMT_IN_FMT_YUV422
;
408 case DRM_FORMAT_YUV444
:
409 format
= JZ_IPU_D_FMT_IN_FMT_YUV444
;
412 WARN_ONCE(1, "Unsupported format");
416 /* Fix output to RGB888 */
417 format
|= JZ_IPU_D_FMT_OUT_FMT_RGB888
;
419 /* Set pixel format */
420 regmap_write(ipu
->map
, JZ_REG_IPU_D_FMT
, format
);
422 /* Set the output height/width/stride */
423 regmap_write(ipu
->map
, JZ_REG_IPU_OUT_GS
,
424 ((state
->crtc_w
* 4) << JZ_IPU_OUT_GS_W_LSB
)
425 | state
->crtc_h
<< JZ_IPU_OUT_GS_H_LSB
);
426 regmap_write(ipu
->map
, JZ_REG_IPU_OUT_STRIDE
, state
->crtc_w
* 4);
429 regmap_set_bits(ipu
->map
, JZ_REG_IPU_CTRL
, JZ_IPU_CTRL_CSC_EN
);
432 * Offsets for Chroma/Luma.
433 * y = source Y - LUMA,
434 * u = source Cb - CHROMA,
435 * v = source Cr - CHROMA
437 regmap_write(ipu
->map
, JZ_REG_IPU_CSC_OFFSET
,
438 128 << JZ_IPU_CSC_OFFSET_CHROMA_LSB
|
439 0 << JZ_IPU_CSC_OFFSET_LUMA_LSB
);
442 * YUV422 to RGB conversion table.
443 * R = C0 / 0x400 * y + C1 / 0x400 * v
444 * G = C0 / 0x400 * y - C2 / 0x400 * u - C3 / 0x400 * v
445 * B = C0 / 0x400 * y + C4 / 0x400 * u
447 regmap_write(ipu
->map
, JZ_REG_IPU_CSC_C0_COEF
, 0x4a8);
448 regmap_write(ipu
->map
, JZ_REG_IPU_CSC_C1_COEF
, 0x662);
449 regmap_write(ipu
->map
, JZ_REG_IPU_CSC_C2_COEF
, 0x191);
450 regmap_write(ipu
->map
, JZ_REG_IPU_CSC_C3_COEF
, 0x341);
451 regmap_write(ipu
->map
, JZ_REG_IPU_CSC_C4_COEF
, 0x811);
457 * Must set ZOOM_SEL before programming bicubic LUTs.
458 * If the IPU supports bicubic, we enable it unconditionally, since it
459 * can do anything bilinear can and more.
461 if (ipu
->soc_info
->has_bicubic
)
462 ctrl
|= JZ_IPU_CTRL_ZOOM_SEL
;
464 upscaling_w
= ipu
->num_w
> ipu
->denom_w
;
466 ctrl
|= JZ_IPU_CTRL_HSCALE
;
468 if (ipu
->num_w
!= 1 || ipu
->denom_w
!= 1) {
469 if (!ipu
->soc_info
->has_bicubic
&& !upscaling_w
)
470 coef_index
|= (ipu
->denom_w
- 1) << 16;
472 coef_index
|= (ipu
->num_w
- 1) << 16;
473 ctrl
|= JZ_IPU_CTRL_HRSZ_EN
;
476 upscaling_h
= ipu
->num_h
> ipu
->denom_h
;
478 ctrl
|= JZ_IPU_CTRL_VSCALE
;
480 if (ipu
->num_h
!= 1 || ipu
->denom_h
!= 1) {
481 if (!ipu
->soc_info
->has_bicubic
&& !upscaling_h
)
482 coef_index
|= ipu
->denom_h
- 1;
484 coef_index
|= ipu
->num_h
- 1;
485 ctrl
|= JZ_IPU_CTRL_VRSZ_EN
;
488 regmap_update_bits(ipu
->map
, JZ_REG_IPU_CTRL
, JZ_IPU_CTRL_ZOOM_SEL
|
489 JZ_IPU_CTRL_HRSZ_EN
| JZ_IPU_CTRL_VRSZ_EN
|
490 JZ_IPU_CTRL_HSCALE
| JZ_IPU_CTRL_VSCALE
, ctrl
);
492 /* Set the LUT index register */
493 regmap_write(ipu
->map
, JZ_REG_IPU_RSZ_COEF_INDEX
, coef_index
);
495 if (ipu
->num_w
!= 1 || ipu
->denom_w
!= 1)
496 ingenic_ipu_set_coefs(ipu
, JZ_REG_IPU_HRSZ_COEF_LUT
,
497 ipu
->num_w
, ipu
->denom_w
);
499 if (ipu
->num_h
!= 1 || ipu
->denom_h
!= 1)
500 ingenic_ipu_set_coefs(ipu
, JZ_REG_IPU_VRSZ_COEF_LUT
,
501 ipu
->num_h
, ipu
->denom_h
);
503 /* Clear STATUS register */
504 regmap_write(ipu
->map
, JZ_REG_IPU_STATUS
, 0);
507 regmap_set_bits(ipu
->map
, JZ_REG_IPU_CTRL
,
508 JZ_IPU_CTRL_RUN
| JZ_IPU_CTRL_FM_IRQ_EN
);
510 dev_dbg(ipu
->dev
, "Scaling %ux%u to %ux%u (%u:%u horiz, %u:%u vert)\n",
511 state
->src_w
>> 16, state
->src_h
>> 16,
512 state
->crtc_w
, state
->crtc_h
,
513 ipu
->num_w
, ipu
->denom_w
, ipu
->num_h
, ipu
->denom_h
);
516 static int ingenic_ipu_plane_atomic_check(struct drm_plane
*plane
,
517 struct drm_plane_state
*state
)
519 unsigned int num_w
, denom_w
, num_h
, denom_h
, xres
, yres
, max_w
, max_h
;
520 struct ingenic_ipu
*ipu
= plane_to_ingenic_ipu(plane
);
521 struct drm_crtc
*crtc
= state
->crtc
?: plane
->state
->crtc
;
522 struct drm_crtc_state
*crtc_state
;
527 crtc_state
= drm_atomic_get_existing_crtc_state(state
->state
, crtc
);
528 if (WARN_ON(!crtc_state
))
531 /* Request a full modeset if we are enabling or disabling the IPU. */
532 if (!plane
->state
->crtc
^ !state
->crtc
)
533 crtc_state
->mode_changed
= true;
536 !crtc_state
->mode
.hdisplay
|| !crtc_state
->mode
.vdisplay
)
539 /* Plane must be fully visible */
540 if (state
->crtc_x
< 0 || state
->crtc_y
< 0 ||
541 state
->crtc_x
+ state
->crtc_w
> crtc_state
->mode
.hdisplay
||
542 state
->crtc_y
+ state
->crtc_h
> crtc_state
->mode
.vdisplay
)
545 /* Minimum size is 4x4 */
546 if ((state
->src_w
>> 16) < 4 || (state
->src_h
>> 16) < 4)
549 /* Input and output lines must have an even number of pixels. */
550 if (((state
->src_w
>> 16) & 1) || (state
->crtc_w
& 1))
553 if (!osd_changed(state
, plane
->state
))
556 crtc_state
->mode_changed
= true;
558 xres
= state
->src_w
>> 16;
559 yres
= state
->src_h
>> 16;
562 * Increase the scaled image's theorical width/height until we find a
563 * configuration that has valid scaling coefficients, up to 102% of the
564 * screen's resolution. This makes sure that we can scale from almost
565 * every resolution possible at the cost of a very small distorsion.
566 * The CRTC_W / CRTC_H are not modified.
568 max_w
= crtc_state
->mode
.hdisplay
* 102 / 100;
569 max_h
= crtc_state
->mode
.vdisplay
* 102 / 100;
571 for (denom_w
= xres
, num_w
= state
->crtc_w
; num_w
<= max_w
; num_w
++)
572 if (!reduce_fraction(&num_w
, &denom_w
))
577 for (denom_h
= yres
, num_h
= state
->crtc_h
; num_h
<= max_h
; num_h
++)
578 if (!reduce_fraction(&num_h
, &denom_h
))
585 ipu
->denom_w
= denom_w
;
586 ipu
->denom_h
= denom_h
;
591 static void ingenic_ipu_plane_atomic_disable(struct drm_plane
*plane
,
592 struct drm_plane_state
*old_state
)
594 struct ingenic_ipu
*ipu
= plane_to_ingenic_ipu(plane
);
596 regmap_set_bits(ipu
->map
, JZ_REG_IPU_CTRL
, JZ_IPU_CTRL_STOP
);
597 regmap_clear_bits(ipu
->map
, JZ_REG_IPU_CTRL
, JZ_IPU_CTRL_CHIP_EN
);
599 ingenic_drm_plane_disable(ipu
->master
, plane
);
601 if (ipu
->clk_enabled
) {
602 clk_disable(ipu
->clk
);
603 ipu
->clk_enabled
= false;
607 static const struct drm_plane_helper_funcs ingenic_ipu_plane_helper_funcs
= {
608 .atomic_update
= ingenic_ipu_plane_atomic_update
,
609 .atomic_check
= ingenic_ipu_plane_atomic_check
,
610 .atomic_disable
= ingenic_ipu_plane_atomic_disable
,
611 .prepare_fb
= drm_gem_fb_prepare_fb
,
615 ingenic_ipu_plane_atomic_get_property(struct drm_plane
*plane
,
616 const struct drm_plane_state
*state
,
617 struct drm_property
*property
, u64
*val
)
619 struct ingenic_ipu
*ipu
= plane_to_ingenic_ipu(plane
);
621 if (property
!= ipu
->sharpness_prop
)
624 *val
= ipu
->sharpness
;
630 ingenic_ipu_plane_atomic_set_property(struct drm_plane
*plane
,
631 struct drm_plane_state
*state
,
632 struct drm_property
*property
, u64 val
)
634 struct ingenic_ipu
*ipu
= plane_to_ingenic_ipu(plane
);
635 struct drm_crtc_state
*crtc_state
;
637 if (property
!= ipu
->sharpness_prop
)
640 ipu
->sharpness
= val
;
643 crtc_state
= drm_atomic_get_existing_crtc_state(state
->state
, state
->crtc
);
644 if (WARN_ON(!crtc_state
))
647 crtc_state
->mode_changed
= true;
653 static const struct drm_plane_funcs ingenic_ipu_plane_funcs
= {
654 .update_plane
= drm_atomic_helper_update_plane
,
655 .disable_plane
= drm_atomic_helper_disable_plane
,
656 .reset
= drm_atomic_helper_plane_reset
,
657 .destroy
= drm_plane_cleanup
,
659 .atomic_duplicate_state
= drm_atomic_helper_plane_duplicate_state
,
660 .atomic_destroy_state
= drm_atomic_helper_plane_destroy_state
,
662 .atomic_get_property
= ingenic_ipu_plane_atomic_get_property
,
663 .atomic_set_property
= ingenic_ipu_plane_atomic_set_property
,
666 static irqreturn_t
ingenic_ipu_irq_handler(int irq
, void *arg
)
668 struct ingenic_ipu
*ipu
= arg
;
669 struct drm_crtc
*crtc
= drm_crtc_from_index(ipu
->drm
, 0);
672 /* dummy read allows CPU to reconfigure IPU */
673 if (ipu
->soc_info
->manual_restart
)
674 regmap_read(ipu
->map
, JZ_REG_IPU_STATUS
, &dummy
);
677 regmap_write(ipu
->map
, JZ_REG_IPU_STATUS
, 0);
679 /* Set previously cached addresses */
680 regmap_write(ipu
->map
, JZ_REG_IPU_Y_ADDR
, ipu
->addr_y
);
681 regmap_write(ipu
->map
, JZ_REG_IPU_U_ADDR
, ipu
->addr_u
);
682 regmap_write(ipu
->map
, JZ_REG_IPU_V_ADDR
, ipu
->addr_v
);
684 /* Run IPU for the new frame */
685 if (ipu
->soc_info
->manual_restart
)
686 regmap_set_bits(ipu
->map
, JZ_REG_IPU_CTRL
, JZ_IPU_CTRL_RUN
);
688 drm_crtc_handle_vblank(crtc
);
693 static const struct regmap_config ingenic_ipu_regmap_config
= {
698 .max_register
= JZ_REG_IPU_OUT_PHY_T_ADDR
,
701 static int ingenic_ipu_bind(struct device
*dev
, struct device
*master
, void *d
)
703 struct platform_device
*pdev
= to_platform_device(dev
);
704 const struct soc_info
*soc_info
;
705 struct drm_device
*drm
= d
;
706 struct drm_plane
*plane
;
707 struct ingenic_ipu
*ipu
;
709 unsigned int sharpness_max
;
712 ipu
= devm_kzalloc(dev
, sizeof(*ipu
), GFP_KERNEL
);
716 soc_info
= of_device_get_match_data(dev
);
718 dev_err(dev
, "Missing platform data\n");
724 ipu
->master
= master
;
725 ipu
->soc_info
= soc_info
;
727 base
= devm_platform_ioremap_resource(pdev
, 0);
729 dev_err(dev
, "Failed to get memory resource\n");
730 return PTR_ERR(base
);
733 ipu
->map
= devm_regmap_init_mmio(dev
, base
, &ingenic_ipu_regmap_config
);
734 if (IS_ERR(ipu
->map
)) {
735 dev_err(dev
, "Failed to create regmap\n");
736 return PTR_ERR(ipu
->map
);
739 irq
= platform_get_irq(pdev
, 0);
743 ipu
->clk
= devm_clk_get(dev
, "ipu");
744 if (IS_ERR(ipu
->clk
)) {
745 dev_err(dev
, "Failed to get pixel clock\n");
746 return PTR_ERR(ipu
->clk
);
749 err
= devm_request_irq(dev
, irq
, ingenic_ipu_irq_handler
, 0,
752 dev_err(dev
, "Unable to request IRQ\n");
757 dev_set_drvdata(dev
, plane
);
759 drm_plane_helper_add(plane
, &ingenic_ipu_plane_helper_funcs
);
761 err
= drm_universal_plane_init(drm
, plane
, 1, &ingenic_ipu_plane_funcs
,
762 soc_info
->formats
, soc_info
->num_formats
,
763 NULL
, DRM_PLANE_TYPE_PRIMARY
, NULL
);
765 dev_err(dev
, "Failed to init plane: %i\n", err
);
770 * Sharpness settings range is [0,32]
771 * 0 : nearest-neighbor
773 * 2 .. 32 : bicubic (translated to sharpness factor -0.25 .. -4.0)
775 sharpness_max
= soc_info
->has_bicubic
? 32 : 1;
776 ipu
->sharpness_prop
= drm_property_create_range(drm
, 0, "sharpness",
778 if (!ipu
->sharpness_prop
) {
779 dev_err(dev
, "Unable to create sharpness property\n");
783 /* Default sharpness factor: -0.125 * 8 = -1.0 */
784 ipu
->sharpness
= soc_info
->has_bicubic
? 8 : 1;
785 drm_object_attach_property(&plane
->base
, ipu
->sharpness_prop
,
788 err
= clk_prepare(ipu
->clk
);
790 dev_err(dev
, "Unable to prepare clock\n");
797 static void ingenic_ipu_unbind(struct device
*dev
,
798 struct device
*master
, void *d
)
800 struct ingenic_ipu
*ipu
= dev_get_drvdata(dev
);
802 clk_unprepare(ipu
->clk
);
805 static const struct component_ops ingenic_ipu_ops
= {
806 .bind
= ingenic_ipu_bind
,
807 .unbind
= ingenic_ipu_unbind
,
810 static int ingenic_ipu_probe(struct platform_device
*pdev
)
812 return component_add(&pdev
->dev
, &ingenic_ipu_ops
);
815 static int ingenic_ipu_remove(struct platform_device
*pdev
)
817 component_del(&pdev
->dev
, &ingenic_ipu_ops
);
821 static const u32 jz4725b_ipu_formats
[] = {
823 * While officially supported, packed YUV 4:2:2 formats can cause
824 * random hardware crashes on JZ4725B under certain circumstances.
825 * It seems to happen with some specific resize ratios.
826 * Until a proper workaround or fix is found, disable these formats.
838 static const struct soc_info jz4725b_soc_info
= {
839 .formats
= jz4725b_ipu_formats
,
840 .num_formats
= ARRAY_SIZE(jz4725b_ipu_formats
),
841 .has_bicubic
= false,
842 .manual_restart
= true,
843 .set_coefs
= jz4725b_set_coefs
,
846 static const u32 jz4760_ipu_formats
[] = {
864 static const struct soc_info jz4760_soc_info
= {
865 .formats
= jz4760_ipu_formats
,
866 .num_formats
= ARRAY_SIZE(jz4760_ipu_formats
),
868 .manual_restart
= false,
869 .set_coefs
= jz4760_set_coefs
,
872 static const struct of_device_id ingenic_ipu_of_match
[] = {
873 { .compatible
= "ingenic,jz4725b-ipu", .data
= &jz4725b_soc_info
},
874 { .compatible
= "ingenic,jz4760-ipu", .data
= &jz4760_soc_info
},
877 MODULE_DEVICE_TABLE(of
, ingenic_ipu_of_match
);
879 static struct platform_driver ingenic_ipu_driver
= {
881 .name
= "ingenic-ipu",
882 .of_match_table
= ingenic_ipu_of_match
,
884 .probe
= ingenic_ipu_probe
,
885 .remove
= ingenic_ipu_remove
,
888 struct platform_driver
*ingenic_ipu_driver_ptr
= &ingenic_ipu_driver
;