1 // SPDX-License-Identifier: GPL-2.0
3 // Ingenic JZ47xx IPU driver
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/platform_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_damage_helper.h>
24 #include <drm/drm_drv.h>
25 #include <drm/drm_fb_dma_helper.h>
26 #include <drm/drm_fourcc.h>
27 #include <drm/drm_framebuffer.h>
28 #include <drm/drm_gem_atomic_helper.h>
29 #include <drm/drm_gem_dma_helper.h>
30 #include <drm/drm_gem_framebuffer_helper.h>
31 #include <drm/drm_plane.h>
32 #include <drm/drm_property.h>
33 #include <drm/drm_vblank.h>
43 void (*set_coefs)(struct ingenic_ipu *ipu, unsigned int reg,
44 unsigned int sharpness, bool downscale,
45 unsigned int weight, unsigned int offset);
48 struct ingenic_ipu_private_state {
49 struct drm_private_state base;
51 unsigned int num_w, num_h, denom_w, denom_h;
55 struct drm_plane plane;
56 struct drm_device *drm;
57 struct device *dev, *master;
60 const struct soc_info *soc_info;
63 dma_addr_t addr_y, addr_u, addr_v;
65 struct drm_property *sharpness_prop;
66 unsigned int sharpness;
68 struct drm_private_obj private_obj;
71 /* Signed 15.16 fixed-point math (for bicubic scaling coefficients) */
72 #define I2F(i) ((s32)(i) * 65536)
73 #define F2I(f) ((f) / 65536)
74 #define FMUL(fa, fb) ((s32)(((s64)(fa) * (s64)(fb)) / 65536))
75 #define SHARPNESS_INCR (I2F(-1) / 8)
77 static inline struct ingenic_ipu *plane_to_ingenic_ipu(struct drm_plane *plane)
79 return container_of(plane, struct ingenic_ipu, plane);
82 static inline struct ingenic_ipu_private_state *
83 to_ingenic_ipu_priv_state(struct drm_private_state *state)
85 return container_of(state, struct ingenic_ipu_private_state, base);
88 static struct ingenic_ipu_private_state *
89 ingenic_ipu_get_priv_state(struct ingenic_ipu *priv, struct drm_atomic_state *state)
91 struct drm_private_state *priv_state;
93 priv_state = drm_atomic_get_private_obj_state(state, &priv->private_obj);
94 if (IS_ERR(priv_state))
95 return ERR_CAST(priv_state);
97 return to_ingenic_ipu_priv_state(priv_state);
100 static struct ingenic_ipu_private_state *
101 ingenic_ipu_get_new_priv_state(struct ingenic_ipu *priv, struct drm_atomic_state *state)
103 struct drm_private_state *priv_state;
105 priv_state = drm_atomic_get_new_private_obj_state(state, &priv->private_obj);
109 return to_ingenic_ipu_priv_state(priv_state);
113 * Apply conventional cubic convolution kernel. Both parameters
114 * and return value are 15.16 signed fixed-point.
116 * @f_a: Sharpness factor, typically in range [-4.0, -0.25].
117 * A larger magnitude increases perceived sharpness, but going past
118 * -2.0 might cause ringing artifacts to outweigh any improvement.
119 * Nice values on a 320x240 LCD are between -0.75 and -2.0.
121 * @f_x: Absolute distance in pixels from 'pixel 0' sample position
122 * along horizontal (or vertical) source axis. Range is [0, +2.0].
124 * returns: Weight of this pixel within 4-pixel sample group. Range is
125 * [-2.0, +2.0]. For moderate (i.e. > -3.0) sharpness factors,
126 * range is within [-1.0, +1.0].
128 static inline s32 cubic_conv(s32 f_a, s32 f_x)
130 const s32 f_1 = I2F(1);
131 const s32 f_2 = I2F(2);
132 const s32 f_3 = I2F(3);
133 const s32 f_4 = I2F(4);
134 const s32 f_x2 = FMUL(f_x, f_x);
135 const s32 f_x3 = FMUL(f_x, f_x2);
138 return FMUL((f_a + f_2), f_x3) - FMUL((f_a + f_3), f_x2) + f_1;
140 return FMUL(f_a, (f_x3 - 5 * f_x2 + 8 * f_x - f_4));
146 * On entry, "weight" is a coefficient suitable for bilinear mode,
147 * which is converted to a set of four suitable for bicubic mode.
149 * "weight 512" means all of pixel 0;
150 * "weight 256" means half of pixel 0 and half of pixel 1;
151 * "weight 0" means all of pixel 1;
153 * "offset" is increment to next source pixel sample location.
155 static void jz4760_set_coefs(struct ingenic_ipu *ipu, unsigned int reg,
156 unsigned int sharpness, bool downscale,
157 unsigned int weight, unsigned int offset)
160 s32 w0, w1, w2, w3; /* Pixel weights at X (or Y) offsets -1,0,1,2 */
162 weight = clamp_val(weight, 0, 512);
166 * When sharpness setting is 0, emulate nearest-neighbor.
167 * When sharpness setting is 1, emulate bilinear.
171 weight = weight >= 256 ? 512 : 0;
177 const s32 f_a = SHARPNESS_INCR * sharpness;
178 const s32 f_h = I2F(1) / 2; /* Round up 0.5 */
181 * Note that always rounding towards +infinity here is intended.
182 * The resulting coefficients match a round-to-nearest-int
183 * double floating-point implementation.
186 weight = 512 - weight;
187 w0 = F2I(f_h + 512 * cubic_conv(f_a, I2F(512 + weight) / 512));
188 w1 = F2I(f_h + 512 * cubic_conv(f_a, I2F(0 + weight) / 512));
189 w2 = F2I(f_h + 512 * cubic_conv(f_a, I2F(512 - weight) / 512));
190 w3 = F2I(f_h + 512 * cubic_conv(f_a, I2F(1024 - weight) / 512));
191 w0 = clamp_val(w0, -1024, 1023);
192 w1 = clamp_val(w1, -1024, 1023);
193 w2 = clamp_val(w2, -1024, 1023);
194 w3 = clamp_val(w3, -1024, 1023);
197 val = ((w1 & JZ4760_IPU_RSZ_COEF_MASK) << JZ4760_IPU_RSZ_COEF31_LSB) |
198 ((w0 & JZ4760_IPU_RSZ_COEF_MASK) << JZ4760_IPU_RSZ_COEF20_LSB);
199 regmap_write(ipu->map, reg, val);
201 val = ((w3 & JZ4760_IPU_RSZ_COEF_MASK) << JZ4760_IPU_RSZ_COEF31_LSB) |
202 ((w2 & JZ4760_IPU_RSZ_COEF_MASK) << JZ4760_IPU_RSZ_COEF20_LSB) |
203 ((offset & JZ4760_IPU_RSZ_OFFSET_MASK) << JZ4760_IPU_RSZ_OFFSET_LSB);
204 regmap_write(ipu->map, reg, val);
207 static void jz4725b_set_coefs(struct ingenic_ipu *ipu, unsigned int reg,
208 unsigned int sharpness, bool downscale,
209 unsigned int weight, unsigned int offset)
211 u32 val = JZ4725B_IPU_RSZ_LUT_OUT_EN;
214 weight = clamp_val(weight, 0, 512);
217 weight = weight >= 256 ? 512 : 0;
219 val |= (weight & JZ4725B_IPU_RSZ_LUT_COEF_MASK) << JZ4725B_IPU_RSZ_LUT_COEF_LSB;
220 if (downscale || !!offset)
221 val |= JZ4725B_IPU_RSZ_LUT_IN_EN;
223 regmap_write(ipu->map, reg, val);
226 for (i = 1; i < offset; i++)
227 regmap_write(ipu->map, reg, JZ4725B_IPU_RSZ_LUT_IN_EN);
231 static void ingenic_ipu_set_downscale_coefs(struct ingenic_ipu *ipu,
236 unsigned int i, offset, weight, weight_num = denom;
238 for (i = 0; i < num; i++) {
239 weight_num = num + (weight_num - num) % (num * 2);
240 weight = 512 - 512 * (weight_num - num) / (num * 2);
241 weight_num += denom * 2;
242 offset = (weight_num - num) / (num * 2);
244 ipu->soc_info->set_coefs(ipu, reg, ipu->sharpness,
245 true, weight, offset);
249 static void ingenic_ipu_set_integer_upscale_coefs(struct ingenic_ipu *ipu,
254 * Force nearest-neighbor scaling and use simple math when upscaling
255 * by an integer ratio. It looks better, and fixes a few problem cases.
259 for (i = 0; i < num; i++)
260 ipu->soc_info->set_coefs(ipu, reg, 0, false, 512, i == num - 1);
263 static void ingenic_ipu_set_upscale_coefs(struct ingenic_ipu *ipu,
268 unsigned int i, offset, weight, weight_num = 0;
270 for (i = 0; i < num; i++) {
271 weight = 512 - 512 * weight_num / num;
273 offset = weight_num >= num;
278 ipu->soc_info->set_coefs(ipu, reg, ipu->sharpness,
279 false, weight, offset);
283 static void ingenic_ipu_set_coefs(struct ingenic_ipu *ipu, unsigned int reg,
284 unsigned int num, unsigned int denom)
286 /* Begin programming the LUT */
287 regmap_write(ipu->map, reg, -1);
290 ingenic_ipu_set_downscale_coefs(ipu, reg, num, denom);
292 ingenic_ipu_set_integer_upscale_coefs(ipu, reg, num);
294 ingenic_ipu_set_upscale_coefs(ipu, reg, num, denom);
297 static int reduce_fraction(unsigned int *num, unsigned int *denom)
299 unsigned long d = gcd(*num, *denom);
301 /* The scaling table has only 31 entries */
310 static inline bool osd_changed(struct drm_plane_state *state,
311 struct drm_plane_state *oldstate)
313 return state->src_x != oldstate->src_x ||
314 state->src_y != oldstate->src_y ||
315 state->src_w != oldstate->src_w ||
316 state->src_h != oldstate->src_h ||
317 state->crtc_x != oldstate->crtc_x ||
318 state->crtc_y != oldstate->crtc_y ||
319 state->crtc_w != oldstate->crtc_w ||
320 state->crtc_h != oldstate->crtc_h;
323 static void ingenic_ipu_plane_atomic_update(struct drm_plane *plane,
324 struct drm_atomic_state *state)
326 struct ingenic_ipu *ipu = plane_to_ingenic_ipu(plane);
327 struct drm_plane_state *newstate = drm_atomic_get_new_plane_state(state, plane);
328 struct drm_plane_state *oldstate = drm_atomic_get_old_plane_state(state, plane);
329 const struct drm_format_info *finfo;
330 u32 ctrl, stride = 0, coef_index = 0, format = 0;
331 bool needs_modeset, upscaling_w, upscaling_h;
332 struct ingenic_ipu_private_state *ipu_state;
335 if (!newstate || !newstate->fb)
338 ipu_state = ingenic_ipu_get_new_priv_state(ipu, state);
339 if (WARN_ON(!ipu_state))
342 finfo = drm_format_info(newstate->fb->format->format);
344 if (!ipu->clk_enabled) {
345 err = clk_enable(ipu->clk);
347 dev_err(ipu->dev, "Unable to enable clock: %d\n", err);
351 ipu->clk_enabled = true;
354 /* Reset all the registers if needed */
355 needs_modeset = drm_atomic_crtc_needs_modeset(newstate->crtc->state);
357 regmap_set_bits(ipu->map, JZ_REG_IPU_CTRL, JZ_IPU_CTRL_RST);
359 /* Enable the chip */
360 regmap_set_bits(ipu->map, JZ_REG_IPU_CTRL,
361 JZ_IPU_CTRL_CHIP_EN | JZ_IPU_CTRL_LCDC_SEL);
364 if (ingenic_drm_map_noncoherent(ipu->master))
365 drm_fb_dma_sync_non_coherent(ipu->drm, oldstate, newstate);
367 /* New addresses will be committed in vblank handler... */
368 ipu->addr_y = drm_fb_dma_get_gem_addr(newstate->fb, newstate, 0);
369 if (finfo->num_planes > 1)
370 ipu->addr_u = drm_fb_dma_get_gem_addr(newstate->fb, newstate,
372 if (finfo->num_planes > 2)
373 ipu->addr_v = drm_fb_dma_get_gem_addr(newstate->fb, newstate,
379 /* Or right here if we're doing a full modeset. */
380 regmap_write(ipu->map, JZ_REG_IPU_Y_ADDR, ipu->addr_y);
381 regmap_write(ipu->map, JZ_REG_IPU_U_ADDR, ipu->addr_u);
382 regmap_write(ipu->map, JZ_REG_IPU_V_ADDR, ipu->addr_v);
384 if (finfo->num_planes == 1)
385 regmap_set_bits(ipu->map, JZ_REG_IPU_CTRL, JZ_IPU_CTRL_SPKG_SEL);
387 ingenic_drm_plane_config(ipu->master, plane, DRM_FORMAT_XRGB8888);
389 /* Set the input height/width/strides */
390 if (finfo->num_planes > 2)
391 stride = ((newstate->src_w >> 16) * finfo->cpp[2] / finfo->hsub)
392 << JZ_IPU_UV_STRIDE_V_LSB;
394 if (finfo->num_planes > 1)
395 stride |= ((newstate->src_w >> 16) * finfo->cpp[1] / finfo->hsub)
396 << JZ_IPU_UV_STRIDE_U_LSB;
398 regmap_write(ipu->map, JZ_REG_IPU_UV_STRIDE, stride);
400 stride = ((newstate->src_w >> 16) * finfo->cpp[0]) << JZ_IPU_Y_STRIDE_Y_LSB;
401 regmap_write(ipu->map, JZ_REG_IPU_Y_STRIDE, stride);
403 regmap_write(ipu->map, JZ_REG_IPU_IN_GS,
404 (stride << JZ_IPU_IN_GS_W_LSB) |
405 ((newstate->src_h >> 16) << JZ_IPU_IN_GS_H_LSB));
407 switch (finfo->format) {
408 case DRM_FORMAT_XRGB1555:
409 format = JZ_IPU_D_FMT_IN_FMT_RGB555 |
410 JZ_IPU_D_FMT_RGB_OUT_OFT_RGB;
412 case DRM_FORMAT_XBGR1555:
413 format = JZ_IPU_D_FMT_IN_FMT_RGB555 |
414 JZ_IPU_D_FMT_RGB_OUT_OFT_BGR;
416 case DRM_FORMAT_RGB565:
417 format = JZ_IPU_D_FMT_IN_FMT_RGB565 |
418 JZ_IPU_D_FMT_RGB_OUT_OFT_RGB;
420 case DRM_FORMAT_BGR565:
421 format = JZ_IPU_D_FMT_IN_FMT_RGB565 |
422 JZ_IPU_D_FMT_RGB_OUT_OFT_BGR;
424 case DRM_FORMAT_XRGB8888:
425 case DRM_FORMAT_XYUV8888:
426 format = JZ_IPU_D_FMT_IN_FMT_RGB888 |
427 JZ_IPU_D_FMT_RGB_OUT_OFT_RGB;
429 case DRM_FORMAT_XBGR8888:
430 format = JZ_IPU_D_FMT_IN_FMT_RGB888 |
431 JZ_IPU_D_FMT_RGB_OUT_OFT_BGR;
433 case DRM_FORMAT_YUYV:
434 format = JZ_IPU_D_FMT_IN_FMT_YUV422 |
435 JZ_IPU_D_FMT_YUV_VY1UY0;
437 case DRM_FORMAT_YVYU:
438 format = JZ_IPU_D_FMT_IN_FMT_YUV422 |
439 JZ_IPU_D_FMT_YUV_UY1VY0;
441 case DRM_FORMAT_UYVY:
442 format = JZ_IPU_D_FMT_IN_FMT_YUV422 |
443 JZ_IPU_D_FMT_YUV_Y1VY0U;
445 case DRM_FORMAT_VYUY:
446 format = JZ_IPU_D_FMT_IN_FMT_YUV422 |
447 JZ_IPU_D_FMT_YUV_Y1UY0V;
449 case DRM_FORMAT_YUV411:
450 format = JZ_IPU_D_FMT_IN_FMT_YUV411;
452 case DRM_FORMAT_YUV420:
453 format = JZ_IPU_D_FMT_IN_FMT_YUV420;
455 case DRM_FORMAT_YUV422:
456 format = JZ_IPU_D_FMT_IN_FMT_YUV422;
458 case DRM_FORMAT_YUV444:
459 format = JZ_IPU_D_FMT_IN_FMT_YUV444;
462 WARN_ONCE(1, "Unsupported format");
466 /* Fix output to RGB888 */
467 format |= JZ_IPU_D_FMT_OUT_FMT_RGB888;
469 /* Set pixel format */
470 regmap_write(ipu->map, JZ_REG_IPU_D_FMT, format);
472 /* Set the output height/width/stride */
473 regmap_write(ipu->map, JZ_REG_IPU_OUT_GS,
474 ((newstate->crtc_w * 4) << JZ_IPU_OUT_GS_W_LSB)
475 | newstate->crtc_h << JZ_IPU_OUT_GS_H_LSB);
476 regmap_write(ipu->map, JZ_REG_IPU_OUT_STRIDE, newstate->crtc_w * 4);
479 regmap_set_bits(ipu->map, JZ_REG_IPU_CTRL, JZ_IPU_CTRL_CSC_EN);
482 * Offsets for Chroma/Luma.
483 * y = source Y - LUMA,
484 * u = source Cb - CHROMA,
485 * v = source Cr - CHROMA
487 regmap_write(ipu->map, JZ_REG_IPU_CSC_OFFSET,
488 128 << JZ_IPU_CSC_OFFSET_CHROMA_LSB |
489 0 << JZ_IPU_CSC_OFFSET_LUMA_LSB);
492 * YUV422 to RGB conversion table.
493 * R = C0 / 0x400 * y + C1 / 0x400 * v
494 * G = C0 / 0x400 * y - C2 / 0x400 * u - C3 / 0x400 * v
495 * B = C0 / 0x400 * y + C4 / 0x400 * u
497 regmap_write(ipu->map, JZ_REG_IPU_CSC_C0_COEF, 0x4a8);
498 regmap_write(ipu->map, JZ_REG_IPU_CSC_C1_COEF, 0x662);
499 regmap_write(ipu->map, JZ_REG_IPU_CSC_C2_COEF, 0x191);
500 regmap_write(ipu->map, JZ_REG_IPU_CSC_C3_COEF, 0x341);
501 regmap_write(ipu->map, JZ_REG_IPU_CSC_C4_COEF, 0x811);
507 * Must set ZOOM_SEL before programming bicubic LUTs.
508 * If the IPU supports bicubic, we enable it unconditionally, since it
509 * can do anything bilinear can and more.
511 if (ipu->soc_info->has_bicubic)
512 ctrl |= JZ_IPU_CTRL_ZOOM_SEL;
514 upscaling_w = ipu_state->num_w > ipu_state->denom_w;
516 ctrl |= JZ_IPU_CTRL_HSCALE;
518 if (ipu_state->num_w != 1 || ipu_state->denom_w != 1) {
519 if (!ipu->soc_info->has_bicubic && !upscaling_w)
520 coef_index |= (ipu_state->denom_w - 1) << 16;
522 coef_index |= (ipu_state->num_w - 1) << 16;
523 ctrl |= JZ_IPU_CTRL_HRSZ_EN;
526 upscaling_h = ipu_state->num_h > ipu_state->denom_h;
528 ctrl |= JZ_IPU_CTRL_VSCALE;
530 if (ipu_state->num_h != 1 || ipu_state->denom_h != 1) {
531 if (!ipu->soc_info->has_bicubic && !upscaling_h)
532 coef_index |= ipu_state->denom_h - 1;
534 coef_index |= ipu_state->num_h - 1;
535 ctrl |= JZ_IPU_CTRL_VRSZ_EN;
538 regmap_update_bits(ipu->map, JZ_REG_IPU_CTRL, JZ_IPU_CTRL_ZOOM_SEL |
539 JZ_IPU_CTRL_HRSZ_EN | JZ_IPU_CTRL_VRSZ_EN |
540 JZ_IPU_CTRL_HSCALE | JZ_IPU_CTRL_VSCALE, ctrl);
542 /* Set the LUT index register */
543 regmap_write(ipu->map, JZ_REG_IPU_RSZ_COEF_INDEX, coef_index);
545 if (ipu_state->num_w != 1 || ipu_state->denom_w != 1)
546 ingenic_ipu_set_coefs(ipu, JZ_REG_IPU_HRSZ_COEF_LUT,
547 ipu_state->num_w, ipu_state->denom_w);
549 if (ipu_state->num_h != 1 || ipu_state->denom_h != 1)
550 ingenic_ipu_set_coefs(ipu, JZ_REG_IPU_VRSZ_COEF_LUT,
551 ipu_state->num_h, ipu_state->denom_h);
553 /* Clear STATUS register */
554 regmap_write(ipu->map, JZ_REG_IPU_STATUS, 0);
557 regmap_set_bits(ipu->map, JZ_REG_IPU_CTRL,
558 JZ_IPU_CTRL_RUN | JZ_IPU_CTRL_FM_IRQ_EN);
560 dev_dbg(ipu->dev, "Scaling %ux%u to %ux%u (%u:%u horiz, %u:%u vert)\n",
561 newstate->src_w >> 16, newstate->src_h >> 16,
562 newstate->crtc_w, newstate->crtc_h,
563 ipu_state->num_w, ipu_state->denom_w,
564 ipu_state->num_h, ipu_state->denom_h);
567 static int ingenic_ipu_plane_atomic_check(struct drm_plane *plane,
568 struct drm_atomic_state *state)
570 struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state,
572 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
574 unsigned int num_w, denom_w, num_h, denom_h, xres, yres, max_w, max_h;
575 struct ingenic_ipu *ipu = plane_to_ingenic_ipu(plane);
576 struct drm_crtc *crtc = new_plane_state->crtc ?: old_plane_state->crtc;
577 struct drm_crtc_state *crtc_state;
578 struct ingenic_ipu_private_state *ipu_state;
583 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
584 if (WARN_ON(!crtc_state))
587 ipu_state = ingenic_ipu_get_priv_state(ipu, state);
588 if (IS_ERR(ipu_state))
589 return PTR_ERR(ipu_state);
591 /* Request a full modeset if we are enabling or disabling the IPU. */
592 if (!old_plane_state->crtc ^ !new_plane_state->crtc)
593 crtc_state->mode_changed = true;
595 if (!new_plane_state->crtc ||
596 !crtc_state->mode.hdisplay || !crtc_state->mode.vdisplay)
597 goto out_check_damage;
599 /* Plane must be fully visible */
600 if (new_plane_state->crtc_x < 0 || new_plane_state->crtc_y < 0 ||
601 new_plane_state->crtc_x + new_plane_state->crtc_w > crtc_state->mode.hdisplay ||
602 new_plane_state->crtc_y + new_plane_state->crtc_h > crtc_state->mode.vdisplay)
605 /* Minimum size is 4x4 */
606 if ((new_plane_state->src_w >> 16) < 4 || (new_plane_state->src_h >> 16) < 4)
609 /* Input and output lines must have an even number of pixels. */
610 if (((new_plane_state->src_w >> 16) & 1) || (new_plane_state->crtc_w & 1))
613 if (!osd_changed(new_plane_state, old_plane_state))
614 goto out_check_damage;
616 crtc_state->mode_changed = true;
618 xres = new_plane_state->src_w >> 16;
619 yres = new_plane_state->src_h >> 16;
622 * Increase the scaled image's theorical width/height until we find a
623 * configuration that has valid scaling coefficients, up to 102% of the
624 * screen's resolution. This makes sure that we can scale from almost
625 * every resolution possible at the cost of a very small distorsion.
626 * The CRTC_W / CRTC_H are not modified.
628 max_w = crtc_state->mode.hdisplay * 102 / 100;
629 max_h = crtc_state->mode.vdisplay * 102 / 100;
631 for (denom_w = xres, num_w = new_plane_state->crtc_w; num_w <= max_w; num_w++)
632 if (!reduce_fraction(&num_w, &denom_w))
637 for (denom_h = yres, num_h = new_plane_state->crtc_h; num_h <= max_h; num_h++)
638 if (!reduce_fraction(&num_h, &denom_h))
643 ipu_state->num_w = num_w;
644 ipu_state->num_h = num_h;
645 ipu_state->denom_w = denom_w;
646 ipu_state->denom_h = denom_h;
649 if (ingenic_drm_map_noncoherent(ipu->master))
650 drm_atomic_helper_check_plane_damage(state, new_plane_state);
655 static void ingenic_ipu_plane_atomic_disable(struct drm_plane *plane,
656 struct drm_atomic_state *state)
658 struct ingenic_ipu *ipu = plane_to_ingenic_ipu(plane);
660 regmap_set_bits(ipu->map, JZ_REG_IPU_CTRL, JZ_IPU_CTRL_STOP);
661 regmap_clear_bits(ipu->map, JZ_REG_IPU_CTRL, JZ_IPU_CTRL_CHIP_EN);
663 ingenic_drm_plane_disable(ipu->master, plane);
665 if (ipu->clk_enabled) {
666 clk_disable(ipu->clk);
667 ipu->clk_enabled = false;
671 static const struct drm_plane_helper_funcs ingenic_ipu_plane_helper_funcs = {
672 .atomic_update = ingenic_ipu_plane_atomic_update,
673 .atomic_check = ingenic_ipu_plane_atomic_check,
674 .atomic_disable = ingenic_ipu_plane_atomic_disable,
678 ingenic_ipu_plane_atomic_get_property(struct drm_plane *plane,
679 const struct drm_plane_state *state,
680 struct drm_property *property, u64 *val)
682 struct ingenic_ipu *ipu = plane_to_ingenic_ipu(plane);
684 if (property != ipu->sharpness_prop)
687 *val = ipu->sharpness;
693 ingenic_ipu_plane_atomic_set_property(struct drm_plane *plane,
694 struct drm_plane_state *state,
695 struct drm_property *property, u64 val)
697 struct ingenic_ipu *ipu = plane_to_ingenic_ipu(plane);
698 struct drm_crtc_state *crtc_state;
701 if (property != ipu->sharpness_prop)
704 mode_changed = val != ipu->sharpness;
705 ipu->sharpness = val;
708 crtc_state = drm_atomic_get_existing_crtc_state(state->state, state->crtc);
709 if (WARN_ON(!crtc_state))
712 crtc_state->mode_changed |= mode_changed;
718 static const struct drm_plane_funcs ingenic_ipu_plane_funcs = {
719 .update_plane = drm_atomic_helper_update_plane,
720 .disable_plane = drm_atomic_helper_disable_plane,
721 .reset = drm_atomic_helper_plane_reset,
722 .destroy = drm_plane_cleanup,
724 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
725 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
727 .atomic_get_property = ingenic_ipu_plane_atomic_get_property,
728 .atomic_set_property = ingenic_ipu_plane_atomic_set_property,
731 static struct drm_private_state *
732 ingenic_ipu_duplicate_state(struct drm_private_obj *obj)
734 struct ingenic_ipu_private_state *state = to_ingenic_ipu_priv_state(obj->state);
736 state = kmemdup(state, sizeof(*state), GFP_KERNEL);
740 __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
745 static void ingenic_ipu_destroy_state(struct drm_private_obj *obj,
746 struct drm_private_state *state)
748 struct ingenic_ipu_private_state *priv_state = to_ingenic_ipu_priv_state(state);
753 static const struct drm_private_state_funcs ingenic_ipu_private_state_funcs = {
754 .atomic_duplicate_state = ingenic_ipu_duplicate_state,
755 .atomic_destroy_state = ingenic_ipu_destroy_state,
758 static irqreturn_t ingenic_ipu_irq_handler(int irq, void *arg)
760 struct ingenic_ipu *ipu = arg;
761 struct drm_crtc *crtc = drm_crtc_from_index(ipu->drm, 0);
764 /* dummy read allows CPU to reconfigure IPU */
765 if (ipu->soc_info->manual_restart)
766 regmap_read(ipu->map, JZ_REG_IPU_STATUS, &dummy);
769 regmap_write(ipu->map, JZ_REG_IPU_STATUS, 0);
771 /* Set previously cached addresses */
772 regmap_write(ipu->map, JZ_REG_IPU_Y_ADDR, ipu->addr_y);
773 regmap_write(ipu->map, JZ_REG_IPU_U_ADDR, ipu->addr_u);
774 regmap_write(ipu->map, JZ_REG_IPU_V_ADDR, ipu->addr_v);
776 /* Run IPU for the new frame */
777 if (ipu->soc_info->manual_restart)
778 regmap_set_bits(ipu->map, JZ_REG_IPU_CTRL, JZ_IPU_CTRL_RUN);
780 drm_crtc_handle_vblank(crtc);
785 static const struct regmap_config ingenic_ipu_regmap_config = {
790 .max_register = JZ_REG_IPU_OUT_PHY_T_ADDR,
793 static int ingenic_ipu_bind(struct device *dev, struct device *master, void *d)
795 struct platform_device *pdev = to_platform_device(dev);
796 struct ingenic_ipu_private_state *private_state;
797 const struct soc_info *soc_info;
798 struct drm_device *drm = d;
799 struct drm_plane *plane;
800 struct ingenic_ipu *ipu;
802 unsigned int sharpness_max;
805 ipu = devm_kzalloc(dev, sizeof(*ipu), GFP_KERNEL);
809 soc_info = of_device_get_match_data(dev);
811 dev_err(dev, "Missing platform data\n");
817 ipu->master = master;
818 ipu->soc_info = soc_info;
820 base = devm_platform_ioremap_resource(pdev, 0);
822 dev_err(dev, "Failed to get memory resource\n");
823 return PTR_ERR(base);
826 ipu->map = devm_regmap_init_mmio(dev, base, &ingenic_ipu_regmap_config);
827 if (IS_ERR(ipu->map)) {
828 dev_err(dev, "Failed to create regmap\n");
829 return PTR_ERR(ipu->map);
832 irq = platform_get_irq(pdev, 0);
836 ipu->clk = devm_clk_get(dev, "ipu");
837 if (IS_ERR(ipu->clk)) {
838 dev_err(dev, "Failed to get pixel clock\n");
839 return PTR_ERR(ipu->clk);
842 err = devm_request_irq(dev, irq, ingenic_ipu_irq_handler, 0,
845 dev_err(dev, "Unable to request IRQ\n");
850 dev_set_drvdata(dev, plane);
852 drm_plane_helper_add(plane, &ingenic_ipu_plane_helper_funcs);
854 err = drm_universal_plane_init(drm, plane, 1, &ingenic_ipu_plane_funcs,
855 soc_info->formats, soc_info->num_formats,
856 NULL, DRM_PLANE_TYPE_OVERLAY, NULL);
858 dev_err(dev, "Failed to init plane: %i\n", err);
862 if (ingenic_drm_map_noncoherent(master))
863 drm_plane_enable_fb_damage_clips(plane);
866 * Sharpness settings range is [0,32]
867 * 0 : nearest-neighbor
869 * 2 .. 32 : bicubic (translated to sharpness factor -0.25 .. -4.0)
871 sharpness_max = soc_info->has_bicubic ? 32 : 1;
872 ipu->sharpness_prop = drm_property_create_range(drm, 0, "sharpness",
874 if (!ipu->sharpness_prop) {
875 dev_err(dev, "Unable to create sharpness property\n");
879 /* Default sharpness factor: -0.125 * 8 = -1.0 */
880 ipu->sharpness = soc_info->has_bicubic ? 8 : 1;
881 drm_object_attach_property(&plane->base, ipu->sharpness_prop,
884 err = clk_prepare(ipu->clk);
886 dev_err(dev, "Unable to prepare clock\n");
890 private_state = kzalloc(sizeof(*private_state), GFP_KERNEL);
891 if (!private_state) {
893 goto err_clk_unprepare;
896 drm_atomic_private_obj_init(drm, &ipu->private_obj, &private_state->base,
897 &ingenic_ipu_private_state_funcs);
902 clk_unprepare(ipu->clk);
906 static void ingenic_ipu_unbind(struct device *dev,
907 struct device *master, void *d)
909 struct ingenic_ipu *ipu = dev_get_drvdata(dev);
911 drm_atomic_private_obj_fini(&ipu->private_obj);
912 clk_unprepare(ipu->clk);
915 static const struct component_ops ingenic_ipu_ops = {
916 .bind = ingenic_ipu_bind,
917 .unbind = ingenic_ipu_unbind,
920 static int ingenic_ipu_probe(struct platform_device *pdev)
922 return component_add(&pdev->dev, &ingenic_ipu_ops);
925 static void ingenic_ipu_remove(struct platform_device *pdev)
927 component_del(&pdev->dev, &ingenic_ipu_ops);
930 static const u32 jz4725b_ipu_formats[] = {
932 * While officially supported, packed YUV 4:2:2 formats can cause
933 * random hardware crashes on JZ4725B under certain circumstances.
934 * It seems to happen with some specific resize ratios.
935 * Until a proper workaround or fix is found, disable these formats.
947 static const struct soc_info jz4725b_soc_info = {
948 .formats = jz4725b_ipu_formats,
949 .num_formats = ARRAY_SIZE(jz4725b_ipu_formats),
950 .has_bicubic = false,
951 .manual_restart = true,
952 .set_coefs = jz4725b_set_coefs,
955 static const u32 jz4760_ipu_formats[] = {
973 static const struct soc_info jz4760_soc_info = {
974 .formats = jz4760_ipu_formats,
975 .num_formats = ARRAY_SIZE(jz4760_ipu_formats),
977 .manual_restart = false,
978 .set_coefs = jz4760_set_coefs,
981 static const struct of_device_id ingenic_ipu_of_match[] = {
982 { .compatible = "ingenic,jz4725b-ipu", .data = &jz4725b_soc_info },
983 { .compatible = "ingenic,jz4760-ipu", .data = &jz4760_soc_info },
986 MODULE_DEVICE_TABLE(of, ingenic_ipu_of_match);
988 static struct platform_driver ingenic_ipu_driver = {
990 .name = "ingenic-ipu",
991 .of_match_table = ingenic_ipu_of_match,
993 .probe = ingenic_ipu_probe,
994 .remove_new = ingenic_ipu_remove,
997 struct platform_driver *ingenic_ipu_driver_ptr = &ingenic_ipu_driver;