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/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_damage_helper.h>
24 #include <drm/drm_drv.h>
25 #include <drm/drm_fb_cma_helper.h>
26 #include <drm/drm_fourcc.h>
27 #include <drm/drm_gem_atomic_helper.h>
28 #include <drm/drm_gem_cma_helper.h>
29 #include <drm/drm_gem_framebuffer_helper.h>
30 #include <drm/drm_plane.h>
31 #include <drm/drm_plane_helper.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);
49 struct drm_plane plane;
50 struct drm_device *drm;
51 struct device *dev, *master;
54 const struct soc_info *soc_info;
57 unsigned int num_w, num_h, denom_w, denom_h;
59 dma_addr_t addr_y, addr_u, addr_v;
61 struct drm_property *sharpness_prop;
62 unsigned int sharpness;
65 /* Signed 15.16 fixed-point math (for bicubic scaling coefficients) */
66 #define I2F(i) ((s32)(i) * 65536)
67 #define F2I(f) ((f) / 65536)
68 #define FMUL(fa, fb) ((s32)(((s64)(fa) * (s64)(fb)) / 65536))
69 #define SHARPNESS_INCR (I2F(-1) / 8)
71 static inline struct ingenic_ipu *plane_to_ingenic_ipu(struct drm_plane *plane)
73 return container_of(plane, struct ingenic_ipu, plane);
77 * Apply conventional cubic convolution kernel. Both parameters
78 * and return value are 15.16 signed fixed-point.
80 * @f_a: Sharpness factor, typically in range [-4.0, -0.25].
81 * A larger magnitude increases perceived sharpness, but going past
82 * -2.0 might cause ringing artifacts to outweigh any improvement.
83 * Nice values on a 320x240 LCD are between -0.75 and -2.0.
85 * @f_x: Absolute distance in pixels from 'pixel 0' sample position
86 * along horizontal (or vertical) source axis. Range is [0, +2.0].
88 * returns: Weight of this pixel within 4-pixel sample group. Range is
89 * [-2.0, +2.0]. For moderate (i.e. > -3.0) sharpness factors,
90 * range is within [-1.0, +1.0].
92 static inline s32 cubic_conv(s32 f_a, s32 f_x)
94 const s32 f_1 = I2F(1);
95 const s32 f_2 = I2F(2);
96 const s32 f_3 = I2F(3);
97 const s32 f_4 = I2F(4);
98 const s32 f_x2 = FMUL(f_x, f_x);
99 const s32 f_x3 = FMUL(f_x, f_x2);
102 return FMUL((f_a + f_2), f_x3) - FMUL((f_a + f_3), f_x2) + f_1;
104 return FMUL(f_a, (f_x3 - 5 * f_x2 + 8 * f_x - f_4));
110 * On entry, "weight" is a coefficient suitable for bilinear mode,
111 * which is converted to a set of four suitable for bicubic mode.
113 * "weight 512" means all of pixel 0;
114 * "weight 256" means half of pixel 0 and half of pixel 1;
115 * "weight 0" means all of pixel 1;
117 * "offset" is increment to next source pixel sample location.
119 static void jz4760_set_coefs(struct ingenic_ipu *ipu, unsigned int reg,
120 unsigned int sharpness, bool downscale,
121 unsigned int weight, unsigned int offset)
124 s32 w0, w1, w2, w3; /* Pixel weights at X (or Y) offsets -1,0,1,2 */
126 weight = clamp_val(weight, 0, 512);
130 * When sharpness setting is 0, emulate nearest-neighbor.
131 * When sharpness setting is 1, emulate bilinear.
135 weight = weight >= 256 ? 512 : 0;
141 const s32 f_a = SHARPNESS_INCR * sharpness;
142 const s32 f_h = I2F(1) / 2; /* Round up 0.5 */
145 * Note that always rounding towards +infinity here is intended.
146 * The resulting coefficients match a round-to-nearest-int
147 * double floating-point implementation.
150 weight = 512 - weight;
151 w0 = F2I(f_h + 512 * cubic_conv(f_a, I2F(512 + weight) / 512));
152 w1 = F2I(f_h + 512 * cubic_conv(f_a, I2F(0 + weight) / 512));
153 w2 = F2I(f_h + 512 * cubic_conv(f_a, I2F(512 - weight) / 512));
154 w3 = F2I(f_h + 512 * cubic_conv(f_a, I2F(1024 - weight) / 512));
155 w0 = clamp_val(w0, -1024, 1023);
156 w1 = clamp_val(w1, -1024, 1023);
157 w2 = clamp_val(w2, -1024, 1023);
158 w3 = clamp_val(w3, -1024, 1023);
161 val = ((w1 & JZ4760_IPU_RSZ_COEF_MASK) << JZ4760_IPU_RSZ_COEF31_LSB) |
162 ((w0 & JZ4760_IPU_RSZ_COEF_MASK) << JZ4760_IPU_RSZ_COEF20_LSB);
163 regmap_write(ipu->map, reg, val);
165 val = ((w3 & JZ4760_IPU_RSZ_COEF_MASK) << JZ4760_IPU_RSZ_COEF31_LSB) |
166 ((w2 & JZ4760_IPU_RSZ_COEF_MASK) << JZ4760_IPU_RSZ_COEF20_LSB) |
167 ((offset & JZ4760_IPU_RSZ_OFFSET_MASK) << JZ4760_IPU_RSZ_OFFSET_LSB);
168 regmap_write(ipu->map, reg, val);
171 static void jz4725b_set_coefs(struct ingenic_ipu *ipu, unsigned int reg,
172 unsigned int sharpness, bool downscale,
173 unsigned int weight, unsigned int offset)
175 u32 val = JZ4725B_IPU_RSZ_LUT_OUT_EN;
178 weight = clamp_val(weight, 0, 512);
181 weight = weight >= 256 ? 512 : 0;
183 val |= (weight & JZ4725B_IPU_RSZ_LUT_COEF_MASK) << JZ4725B_IPU_RSZ_LUT_COEF_LSB;
184 if (downscale || !!offset)
185 val |= JZ4725B_IPU_RSZ_LUT_IN_EN;
187 regmap_write(ipu->map, reg, val);
190 for (i = 1; i < offset; i++)
191 regmap_write(ipu->map, reg, JZ4725B_IPU_RSZ_LUT_IN_EN);
195 static void ingenic_ipu_set_downscale_coefs(struct ingenic_ipu *ipu,
200 unsigned int i, offset, weight, weight_num = denom;
202 for (i = 0; i < num; i++) {
203 weight_num = num + (weight_num - num) % (num * 2);
204 weight = 512 - 512 * (weight_num - num) / (num * 2);
205 weight_num += denom * 2;
206 offset = (weight_num - num) / (num * 2);
208 ipu->soc_info->set_coefs(ipu, reg, ipu->sharpness,
209 true, weight, offset);
213 static void ingenic_ipu_set_integer_upscale_coefs(struct ingenic_ipu *ipu,
218 * Force nearest-neighbor scaling and use simple math when upscaling
219 * by an integer ratio. It looks better, and fixes a few problem cases.
223 for (i = 0; i < num; i++)
224 ipu->soc_info->set_coefs(ipu, reg, 0, false, 512, i == num - 1);
227 static void ingenic_ipu_set_upscale_coefs(struct ingenic_ipu *ipu,
232 unsigned int i, offset, weight, weight_num = 0;
234 for (i = 0; i < num; i++) {
235 weight = 512 - 512 * weight_num / num;
237 offset = weight_num >= num;
242 ipu->soc_info->set_coefs(ipu, reg, ipu->sharpness,
243 false, weight, offset);
247 static void ingenic_ipu_set_coefs(struct ingenic_ipu *ipu, unsigned int reg,
248 unsigned int num, unsigned int denom)
250 /* Begin programming the LUT */
251 regmap_write(ipu->map, reg, -1);
254 ingenic_ipu_set_downscale_coefs(ipu, reg, num, denom);
256 ingenic_ipu_set_integer_upscale_coefs(ipu, reg, num);
258 ingenic_ipu_set_upscale_coefs(ipu, reg, num, denom);
261 static int reduce_fraction(unsigned int *num, unsigned int *denom)
263 unsigned long d = gcd(*num, *denom);
265 /* The scaling table has only 31 entries */
274 static inline bool osd_changed(struct drm_plane_state *state,
275 struct drm_plane_state *oldstate)
277 return state->src_x != oldstate->src_x ||
278 state->src_y != oldstate->src_y ||
279 state->src_w != oldstate->src_w ||
280 state->src_h != oldstate->src_h ||
281 state->crtc_x != oldstate->crtc_x ||
282 state->crtc_y != oldstate->crtc_y ||
283 state->crtc_w != oldstate->crtc_w ||
284 state->crtc_h != oldstate->crtc_h;
287 static void ingenic_ipu_plane_atomic_update(struct drm_plane *plane,
288 struct drm_atomic_state *state)
290 struct ingenic_ipu *ipu = plane_to_ingenic_ipu(plane);
291 struct drm_plane_state *newstate = drm_atomic_get_new_plane_state(state, plane);
292 struct drm_plane_state *oldstate = drm_atomic_get_old_plane_state(state, plane);
293 const struct drm_format_info *finfo;
294 u32 ctrl, stride = 0, coef_index = 0, format = 0;
295 bool needs_modeset, upscaling_w, upscaling_h;
298 if (!newstate || !newstate->fb)
301 finfo = drm_format_info(newstate->fb->format->format);
303 if (!ipu->clk_enabled) {
304 err = clk_enable(ipu->clk);
306 dev_err(ipu->dev, "Unable to enable clock: %d\n", err);
310 ipu->clk_enabled = true;
313 /* Reset all the registers if needed */
314 needs_modeset = drm_atomic_crtc_needs_modeset(newstate->crtc->state);
316 regmap_set_bits(ipu->map, JZ_REG_IPU_CTRL, JZ_IPU_CTRL_RST);
318 /* Enable the chip */
319 regmap_set_bits(ipu->map, JZ_REG_IPU_CTRL,
320 JZ_IPU_CTRL_CHIP_EN | JZ_IPU_CTRL_LCDC_SEL);
323 if (ingenic_drm_map_noncoherent(ipu->master))
324 drm_fb_cma_sync_non_coherent(ipu->drm, oldstate, newstate);
326 /* New addresses will be committed in vblank handler... */
327 ipu->addr_y = drm_fb_cma_get_gem_addr(newstate->fb, newstate, 0);
328 if (finfo->num_planes > 1)
329 ipu->addr_u = drm_fb_cma_get_gem_addr(newstate->fb, newstate,
331 if (finfo->num_planes > 2)
332 ipu->addr_v = drm_fb_cma_get_gem_addr(newstate->fb, newstate,
338 /* Or right here if we're doing a full modeset. */
339 regmap_write(ipu->map, JZ_REG_IPU_Y_ADDR, ipu->addr_y);
340 regmap_write(ipu->map, JZ_REG_IPU_U_ADDR, ipu->addr_u);
341 regmap_write(ipu->map, JZ_REG_IPU_V_ADDR, ipu->addr_v);
343 if (finfo->num_planes == 1)
344 regmap_set_bits(ipu->map, JZ_REG_IPU_CTRL, JZ_IPU_CTRL_SPKG_SEL);
346 ingenic_drm_plane_config(ipu->master, plane, DRM_FORMAT_XRGB8888);
348 /* Set the input height/width/strides */
349 if (finfo->num_planes > 2)
350 stride = ((newstate->src_w >> 16) * finfo->cpp[2] / finfo->hsub)
351 << JZ_IPU_UV_STRIDE_V_LSB;
353 if (finfo->num_planes > 1)
354 stride |= ((newstate->src_w >> 16) * finfo->cpp[1] / finfo->hsub)
355 << JZ_IPU_UV_STRIDE_U_LSB;
357 regmap_write(ipu->map, JZ_REG_IPU_UV_STRIDE, stride);
359 stride = ((newstate->src_w >> 16) * finfo->cpp[0]) << JZ_IPU_Y_STRIDE_Y_LSB;
360 regmap_write(ipu->map, JZ_REG_IPU_Y_STRIDE, stride);
362 regmap_write(ipu->map, JZ_REG_IPU_IN_GS,
363 (stride << JZ_IPU_IN_GS_W_LSB) |
364 ((newstate->src_h >> 16) << JZ_IPU_IN_GS_H_LSB));
366 switch (finfo->format) {
367 case DRM_FORMAT_XRGB1555:
368 format = JZ_IPU_D_FMT_IN_FMT_RGB555 |
369 JZ_IPU_D_FMT_RGB_OUT_OFT_RGB;
371 case DRM_FORMAT_XBGR1555:
372 format = JZ_IPU_D_FMT_IN_FMT_RGB555 |
373 JZ_IPU_D_FMT_RGB_OUT_OFT_BGR;
375 case DRM_FORMAT_RGB565:
376 format = JZ_IPU_D_FMT_IN_FMT_RGB565 |
377 JZ_IPU_D_FMT_RGB_OUT_OFT_RGB;
379 case DRM_FORMAT_BGR565:
380 format = JZ_IPU_D_FMT_IN_FMT_RGB565 |
381 JZ_IPU_D_FMT_RGB_OUT_OFT_BGR;
383 case DRM_FORMAT_XRGB8888:
384 case DRM_FORMAT_XYUV8888:
385 format = JZ_IPU_D_FMT_IN_FMT_RGB888 |
386 JZ_IPU_D_FMT_RGB_OUT_OFT_RGB;
388 case DRM_FORMAT_XBGR8888:
389 format = JZ_IPU_D_FMT_IN_FMT_RGB888 |
390 JZ_IPU_D_FMT_RGB_OUT_OFT_BGR;
392 case DRM_FORMAT_YUYV:
393 format = JZ_IPU_D_FMT_IN_FMT_YUV422 |
394 JZ_IPU_D_FMT_YUV_VY1UY0;
396 case DRM_FORMAT_YVYU:
397 format = JZ_IPU_D_FMT_IN_FMT_YUV422 |
398 JZ_IPU_D_FMT_YUV_UY1VY0;
400 case DRM_FORMAT_UYVY:
401 format = JZ_IPU_D_FMT_IN_FMT_YUV422 |
402 JZ_IPU_D_FMT_YUV_Y1VY0U;
404 case DRM_FORMAT_VYUY:
405 format = JZ_IPU_D_FMT_IN_FMT_YUV422 |
406 JZ_IPU_D_FMT_YUV_Y1UY0V;
408 case DRM_FORMAT_YUV411:
409 format = JZ_IPU_D_FMT_IN_FMT_YUV411;
411 case DRM_FORMAT_YUV420:
412 format = JZ_IPU_D_FMT_IN_FMT_YUV420;
414 case DRM_FORMAT_YUV422:
415 format = JZ_IPU_D_FMT_IN_FMT_YUV422;
417 case DRM_FORMAT_YUV444:
418 format = JZ_IPU_D_FMT_IN_FMT_YUV444;
421 WARN_ONCE(1, "Unsupported format");
425 /* Fix output to RGB888 */
426 format |= JZ_IPU_D_FMT_OUT_FMT_RGB888;
428 /* Set pixel format */
429 regmap_write(ipu->map, JZ_REG_IPU_D_FMT, format);
431 /* Set the output height/width/stride */
432 regmap_write(ipu->map, JZ_REG_IPU_OUT_GS,
433 ((newstate->crtc_w * 4) << JZ_IPU_OUT_GS_W_LSB)
434 | newstate->crtc_h << JZ_IPU_OUT_GS_H_LSB);
435 regmap_write(ipu->map, JZ_REG_IPU_OUT_STRIDE, newstate->crtc_w * 4);
438 regmap_set_bits(ipu->map, JZ_REG_IPU_CTRL, JZ_IPU_CTRL_CSC_EN);
441 * Offsets for Chroma/Luma.
442 * y = source Y - LUMA,
443 * u = source Cb - CHROMA,
444 * v = source Cr - CHROMA
446 regmap_write(ipu->map, JZ_REG_IPU_CSC_OFFSET,
447 128 << JZ_IPU_CSC_OFFSET_CHROMA_LSB |
448 0 << JZ_IPU_CSC_OFFSET_LUMA_LSB);
451 * YUV422 to RGB conversion table.
452 * R = C0 / 0x400 * y + C1 / 0x400 * v
453 * G = C0 / 0x400 * y - C2 / 0x400 * u - C3 / 0x400 * v
454 * B = C0 / 0x400 * y + C4 / 0x400 * u
456 regmap_write(ipu->map, JZ_REG_IPU_CSC_C0_COEF, 0x4a8);
457 regmap_write(ipu->map, JZ_REG_IPU_CSC_C1_COEF, 0x662);
458 regmap_write(ipu->map, JZ_REG_IPU_CSC_C2_COEF, 0x191);
459 regmap_write(ipu->map, JZ_REG_IPU_CSC_C3_COEF, 0x341);
460 regmap_write(ipu->map, JZ_REG_IPU_CSC_C4_COEF, 0x811);
466 * Must set ZOOM_SEL before programming bicubic LUTs.
467 * If the IPU supports bicubic, we enable it unconditionally, since it
468 * can do anything bilinear can and more.
470 if (ipu->soc_info->has_bicubic)
471 ctrl |= JZ_IPU_CTRL_ZOOM_SEL;
473 upscaling_w = ipu->num_w > ipu->denom_w;
475 ctrl |= JZ_IPU_CTRL_HSCALE;
477 if (ipu->num_w != 1 || ipu->denom_w != 1) {
478 if (!ipu->soc_info->has_bicubic && !upscaling_w)
479 coef_index |= (ipu->denom_w - 1) << 16;
481 coef_index |= (ipu->num_w - 1) << 16;
482 ctrl |= JZ_IPU_CTRL_HRSZ_EN;
485 upscaling_h = ipu->num_h > ipu->denom_h;
487 ctrl |= JZ_IPU_CTRL_VSCALE;
489 if (ipu->num_h != 1 || ipu->denom_h != 1) {
490 if (!ipu->soc_info->has_bicubic && !upscaling_h)
491 coef_index |= ipu->denom_h - 1;
493 coef_index |= ipu->num_h - 1;
494 ctrl |= JZ_IPU_CTRL_VRSZ_EN;
497 regmap_update_bits(ipu->map, JZ_REG_IPU_CTRL, JZ_IPU_CTRL_ZOOM_SEL |
498 JZ_IPU_CTRL_HRSZ_EN | JZ_IPU_CTRL_VRSZ_EN |
499 JZ_IPU_CTRL_HSCALE | JZ_IPU_CTRL_VSCALE, ctrl);
501 /* Set the LUT index register */
502 regmap_write(ipu->map, JZ_REG_IPU_RSZ_COEF_INDEX, coef_index);
504 if (ipu->num_w != 1 || ipu->denom_w != 1)
505 ingenic_ipu_set_coefs(ipu, JZ_REG_IPU_HRSZ_COEF_LUT,
506 ipu->num_w, ipu->denom_w);
508 if (ipu->num_h != 1 || ipu->denom_h != 1)
509 ingenic_ipu_set_coefs(ipu, JZ_REG_IPU_VRSZ_COEF_LUT,
510 ipu->num_h, ipu->denom_h);
512 /* Clear STATUS register */
513 regmap_write(ipu->map, JZ_REG_IPU_STATUS, 0);
516 regmap_set_bits(ipu->map, JZ_REG_IPU_CTRL,
517 JZ_IPU_CTRL_RUN | JZ_IPU_CTRL_FM_IRQ_EN);
519 dev_dbg(ipu->dev, "Scaling %ux%u to %ux%u (%u:%u horiz, %u:%u vert)\n",
520 newstate->src_w >> 16, newstate->src_h >> 16,
521 newstate->crtc_w, newstate->crtc_h,
522 ipu->num_w, ipu->denom_w, ipu->num_h, ipu->denom_h);
525 static int ingenic_ipu_plane_atomic_check(struct drm_plane *plane,
526 struct drm_atomic_state *state)
528 struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state,
530 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
532 unsigned int num_w, denom_w, num_h, denom_h, xres, yres, max_w, max_h;
533 struct ingenic_ipu *ipu = plane_to_ingenic_ipu(plane);
534 struct drm_crtc *crtc = new_plane_state->crtc ?: old_plane_state->crtc;
535 struct drm_crtc_state *crtc_state;
540 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
541 if (WARN_ON(!crtc_state))
544 /* Request a full modeset if we are enabling or disabling the IPU. */
545 if (!old_plane_state->crtc ^ !new_plane_state->crtc)
546 crtc_state->mode_changed = true;
548 if (!new_plane_state->crtc ||
549 !crtc_state->mode.hdisplay || !crtc_state->mode.vdisplay)
550 goto out_check_damage;
552 /* Plane must be fully visible */
553 if (new_plane_state->crtc_x < 0 || new_plane_state->crtc_y < 0 ||
554 new_plane_state->crtc_x + new_plane_state->crtc_w > crtc_state->mode.hdisplay ||
555 new_plane_state->crtc_y + new_plane_state->crtc_h > crtc_state->mode.vdisplay)
558 /* Minimum size is 4x4 */
559 if ((new_plane_state->src_w >> 16) < 4 || (new_plane_state->src_h >> 16) < 4)
562 /* Input and output lines must have an even number of pixels. */
563 if (((new_plane_state->src_w >> 16) & 1) || (new_plane_state->crtc_w & 1))
566 if (!osd_changed(new_plane_state, old_plane_state))
567 goto out_check_damage;
569 crtc_state->mode_changed = true;
571 xres = new_plane_state->src_w >> 16;
572 yres = new_plane_state->src_h >> 16;
575 * Increase the scaled image's theorical width/height until we find a
576 * configuration that has valid scaling coefficients, up to 102% of the
577 * screen's resolution. This makes sure that we can scale from almost
578 * every resolution possible at the cost of a very small distorsion.
579 * The CRTC_W / CRTC_H are not modified.
581 max_w = crtc_state->mode.hdisplay * 102 / 100;
582 max_h = crtc_state->mode.vdisplay * 102 / 100;
584 for (denom_w = xres, num_w = new_plane_state->crtc_w; num_w <= max_w; num_w++)
585 if (!reduce_fraction(&num_w, &denom_w))
590 for (denom_h = yres, num_h = new_plane_state->crtc_h; num_h <= max_h; num_h++)
591 if (!reduce_fraction(&num_h, &denom_h))
598 ipu->denom_w = denom_w;
599 ipu->denom_h = denom_h;
602 if (ingenic_drm_map_noncoherent(ipu->master))
603 drm_atomic_helper_check_plane_damage(state, new_plane_state);
608 static void ingenic_ipu_plane_atomic_disable(struct drm_plane *plane,
609 struct drm_atomic_state *state)
611 struct ingenic_ipu *ipu = plane_to_ingenic_ipu(plane);
613 regmap_set_bits(ipu->map, JZ_REG_IPU_CTRL, JZ_IPU_CTRL_STOP);
614 regmap_clear_bits(ipu->map, JZ_REG_IPU_CTRL, JZ_IPU_CTRL_CHIP_EN);
616 ingenic_drm_plane_disable(ipu->master, plane);
618 if (ipu->clk_enabled) {
619 clk_disable(ipu->clk);
620 ipu->clk_enabled = false;
624 static const struct drm_plane_helper_funcs ingenic_ipu_plane_helper_funcs = {
625 .atomic_update = ingenic_ipu_plane_atomic_update,
626 .atomic_check = ingenic_ipu_plane_atomic_check,
627 .atomic_disable = ingenic_ipu_plane_atomic_disable,
628 .prepare_fb = drm_gem_plane_helper_prepare_fb,
632 ingenic_ipu_plane_atomic_get_property(struct drm_plane *plane,
633 const struct drm_plane_state *state,
634 struct drm_property *property, u64 *val)
636 struct ingenic_ipu *ipu = plane_to_ingenic_ipu(plane);
638 if (property != ipu->sharpness_prop)
641 *val = ipu->sharpness;
647 ingenic_ipu_plane_atomic_set_property(struct drm_plane *plane,
648 struct drm_plane_state *state,
649 struct drm_property *property, u64 val)
651 struct ingenic_ipu *ipu = plane_to_ingenic_ipu(plane);
652 struct drm_crtc_state *crtc_state;
654 if (property != ipu->sharpness_prop)
657 ipu->sharpness = val;
660 crtc_state = drm_atomic_get_existing_crtc_state(state->state, state->crtc);
661 if (WARN_ON(!crtc_state))
664 crtc_state->mode_changed = true;
670 static const struct drm_plane_funcs ingenic_ipu_plane_funcs = {
671 .update_plane = drm_atomic_helper_update_plane,
672 .disable_plane = drm_atomic_helper_disable_plane,
673 .reset = drm_atomic_helper_plane_reset,
674 .destroy = drm_plane_cleanup,
676 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
677 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
679 .atomic_get_property = ingenic_ipu_plane_atomic_get_property,
680 .atomic_set_property = ingenic_ipu_plane_atomic_set_property,
683 static irqreturn_t ingenic_ipu_irq_handler(int irq, void *arg)
685 struct ingenic_ipu *ipu = arg;
686 struct drm_crtc *crtc = drm_crtc_from_index(ipu->drm, 0);
689 /* dummy read allows CPU to reconfigure IPU */
690 if (ipu->soc_info->manual_restart)
691 regmap_read(ipu->map, JZ_REG_IPU_STATUS, &dummy);
694 regmap_write(ipu->map, JZ_REG_IPU_STATUS, 0);
696 /* Set previously cached addresses */
697 regmap_write(ipu->map, JZ_REG_IPU_Y_ADDR, ipu->addr_y);
698 regmap_write(ipu->map, JZ_REG_IPU_U_ADDR, ipu->addr_u);
699 regmap_write(ipu->map, JZ_REG_IPU_V_ADDR, ipu->addr_v);
701 /* Run IPU for the new frame */
702 if (ipu->soc_info->manual_restart)
703 regmap_set_bits(ipu->map, JZ_REG_IPU_CTRL, JZ_IPU_CTRL_RUN);
705 drm_crtc_handle_vblank(crtc);
710 static const struct regmap_config ingenic_ipu_regmap_config = {
715 .max_register = JZ_REG_IPU_OUT_PHY_T_ADDR,
718 static int ingenic_ipu_bind(struct device *dev, struct device *master, void *d)
720 struct platform_device *pdev = to_platform_device(dev);
721 const struct soc_info *soc_info;
722 struct drm_device *drm = d;
723 struct drm_plane *plane;
724 struct ingenic_ipu *ipu;
726 unsigned int sharpness_max;
729 ipu = devm_kzalloc(dev, sizeof(*ipu), GFP_KERNEL);
733 soc_info = of_device_get_match_data(dev);
735 dev_err(dev, "Missing platform data\n");
741 ipu->master = master;
742 ipu->soc_info = soc_info;
744 base = devm_platform_ioremap_resource(pdev, 0);
746 dev_err(dev, "Failed to get memory resource\n");
747 return PTR_ERR(base);
750 ipu->map = devm_regmap_init_mmio(dev, base, &ingenic_ipu_regmap_config);
751 if (IS_ERR(ipu->map)) {
752 dev_err(dev, "Failed to create regmap\n");
753 return PTR_ERR(ipu->map);
756 irq = platform_get_irq(pdev, 0);
760 ipu->clk = devm_clk_get(dev, "ipu");
761 if (IS_ERR(ipu->clk)) {
762 dev_err(dev, "Failed to get pixel clock\n");
763 return PTR_ERR(ipu->clk);
766 err = devm_request_irq(dev, irq, ingenic_ipu_irq_handler, 0,
769 dev_err(dev, "Unable to request IRQ\n");
774 dev_set_drvdata(dev, plane);
776 drm_plane_helper_add(plane, &ingenic_ipu_plane_helper_funcs);
778 err = drm_universal_plane_init(drm, plane, 1, &ingenic_ipu_plane_funcs,
779 soc_info->formats, soc_info->num_formats,
780 NULL, DRM_PLANE_TYPE_OVERLAY, NULL);
782 dev_err(dev, "Failed to init plane: %i\n", err);
786 if (ingenic_drm_map_noncoherent(master))
787 drm_plane_enable_fb_damage_clips(plane);
790 * Sharpness settings range is [0,32]
791 * 0 : nearest-neighbor
793 * 2 .. 32 : bicubic (translated to sharpness factor -0.25 .. -4.0)
795 sharpness_max = soc_info->has_bicubic ? 32 : 1;
796 ipu->sharpness_prop = drm_property_create_range(drm, 0, "sharpness",
798 if (!ipu->sharpness_prop) {
799 dev_err(dev, "Unable to create sharpness property\n");
803 /* Default sharpness factor: -0.125 * 8 = -1.0 */
804 ipu->sharpness = soc_info->has_bicubic ? 8 : 1;
805 drm_object_attach_property(&plane->base, ipu->sharpness_prop,
808 err = clk_prepare(ipu->clk);
810 dev_err(dev, "Unable to prepare clock\n");
817 static void ingenic_ipu_unbind(struct device *dev,
818 struct device *master, void *d)
820 struct ingenic_ipu *ipu = dev_get_drvdata(dev);
822 clk_unprepare(ipu->clk);
825 static const struct component_ops ingenic_ipu_ops = {
826 .bind = ingenic_ipu_bind,
827 .unbind = ingenic_ipu_unbind,
830 static int ingenic_ipu_probe(struct platform_device *pdev)
832 return component_add(&pdev->dev, &ingenic_ipu_ops);
835 static int ingenic_ipu_remove(struct platform_device *pdev)
837 component_del(&pdev->dev, &ingenic_ipu_ops);
841 static const u32 jz4725b_ipu_formats[] = {
843 * While officially supported, packed YUV 4:2:2 formats can cause
844 * random hardware crashes on JZ4725B under certain circumstances.
845 * It seems to happen with some specific resize ratios.
846 * Until a proper workaround or fix is found, disable these formats.
858 static const struct soc_info jz4725b_soc_info = {
859 .formats = jz4725b_ipu_formats,
860 .num_formats = ARRAY_SIZE(jz4725b_ipu_formats),
861 .has_bicubic = false,
862 .manual_restart = true,
863 .set_coefs = jz4725b_set_coefs,
866 static const u32 jz4760_ipu_formats[] = {
884 static const struct soc_info jz4760_soc_info = {
885 .formats = jz4760_ipu_formats,
886 .num_formats = ARRAY_SIZE(jz4760_ipu_formats),
888 .manual_restart = false,
889 .set_coefs = jz4760_set_coefs,
892 static const struct of_device_id ingenic_ipu_of_match[] = {
893 { .compatible = "ingenic,jz4725b-ipu", .data = &jz4725b_soc_info },
894 { .compatible = "ingenic,jz4760-ipu", .data = &jz4760_soc_info },
897 MODULE_DEVICE_TABLE(of, ingenic_ipu_of_match);
899 static struct platform_driver ingenic_ipu_driver = {
901 .name = "ingenic-ipu",
902 .of_match_table = ingenic_ipu_of_match,
904 .probe = ingenic_ipu_probe,
905 .remove = ingenic_ipu_remove,
908 struct platform_driver *ingenic_ipu_driver_ptr = &ingenic_ipu_driver;