Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

drm/ingenic: Add support for the IPU

Add support for the Image Processing Unit (IPU) found in all Ingenic
SoCs.

The IPU can upscale and downscale a source frame of arbitrary size
ranging from 4x4 to 4096x4096 on newer SoCs, with bicubic filtering
on newer SoCs, bilinear filtering on older SoCs. Nearest-neighbour can
also be obtained with proper coefficients.

Starting from the JZ4725B, the IPU supports a mode where its output is
sent directly to the LCDC, without having to be written to RAM first.
This makes it possible to use the IPU as a DRM plane on the compatible
SoCs, and have it convert and scale anything the userspace asks for to
what's available for the display.

Regarding pixel formats, older SoCs support packed YUV 4:2:2 and various
planar YUV formats. Newer SoCs introduced support for RGB.

Since the IPU is a separate hardware block, to make it work properly the
Ingenic DRM driver will now register itself as a component master in
case the IPU driver has been enabled in the config.

When enabled in the config, the CRTC will see the IPU as a second primary
plane. It cannot be enabled at the same time as the regular primary
plane. It has the same priority, which means that it will also display
below the overlay plane.

v2: - ingenic-ipu is no longer its own module. It will be built
into the ingenic-drm module.
- If enabled in the config, both the core driver and the IPU
driver will register as components; otherwise the core
driver will bypass that and call the ingenic_drm_bind()
function directly.
- Since both files now build into the same module, the
symbols previously exported as GPL are not exported anymore,
since they are only used internally.
- Fix SPDX license header in ingenic-ipu.h
- Avoid using 'for(;;);' loops without trailing statement(s)

v3: - Pass priv structure to IRQ handler; that way we don't hardcode
the expectation that the IPU plane is at index #0.
- Rework osd_changed() to account for src_* changes
- Add multiplanar YUV 4:4:4 support
- Commit fb addresses to HW at vblank, since addr registers are
not shadow registers
- Probe IPU component later so that IPU plane is last
- Fix driver not working on IPU-less hardware
- Use IPU driver's name as the IRQ name to avoid having two
'ingenic-drm' in /proc/interrupts
- Fix IPU only working for still images on JZ4725B
- Add a bit more code comments

Signed-off-by: Paul Cercueil <paul@crapouillou.net>
Reviewed-by: Sam Ravnborg <sam@ravnborg.org>
Link: https://patchwork.freedesktop.org/patch/msgid/20200716163846.174790-10-paul@crapouillou.net

+1143 -16
+11
drivers/gpu/drm/ingenic/Kconfig
··· 14 14 Choose this option for DRM support for the Ingenic SoCs. 15 15 16 16 If M is selected the module will be called ingenic-drm. 17 + 18 + if DRM_INGENIC 19 + 20 + config DRM_INGENIC_IPU 21 + bool "IPU support for Ingenic SoCs" 22 + help 23 + Choose this option to enable support for the IPU found in Ingenic SoCs. 24 + 25 + The Image Processing Unit (IPU) will appear as a second primary plane. 26 + 27 + endif
+2 -1
drivers/gpu/drm/ingenic/Makefile
··· 1 1 obj-$(CONFIG_DRM_INGENIC) += ingenic-drm.o 2 - ingenic-drm-y += ingenic-drm-drv.o 2 + ingenic-drm-y = ingenic-drm-drv.o 3 + ingenic-drm-$(CONFIG_DRM_INGENIC_IPU) += ingenic-ipu.o
+156 -15
drivers/gpu/drm/ingenic/ingenic-drm-drv.c
··· 6 6 7 7 #include "ingenic-drm.h" 8 8 9 + #include <linux/component.h> 9 10 #include <linux/clk.h> 10 11 #include <linux/dma-mapping.h> 11 12 #include <linux/module.h> ··· 55 54 * f0 (aka. foreground0) can be overlayed. Z-order is fixed in 56 55 * hardware and cannot be changed. 57 56 */ 58 - struct drm_plane f0, f1; 57 + struct drm_plane f0, f1, *ipu_plane; 59 58 struct drm_crtc crtc; 60 59 struct drm_encoder encoder; 61 60 ··· 191 190 192 191 regmap_set_bits(priv->map, JZ_REG_LCD_CTRL, 193 192 JZ_LCD_CTRL_OFUP | JZ_LCD_CTRL_BURST_16); 193 + 194 + /* 195 + * IPU restart - specify how much time the LCDC will wait before 196 + * transferring a new frame from the IPU. The value is the one 197 + * suggested in the programming manual. 198 + */ 199 + regmap_write(priv->map, JZ_REG_LCD_IPUR, JZ_LCD_IPUR_IPUREN | 200 + (ht * vpe / 3) << JZ_LCD_IPUR_IPUR_LSB); 194 201 } 195 202 196 203 static int ingenic_drm_crtc_atomic_check(struct drm_crtc *crtc, 197 204 struct drm_crtc_state *state) 198 205 { 199 206 struct ingenic_drm *priv = drm_crtc_get_priv(crtc); 200 - struct drm_plane_state *f1_state, *f0_state; 207 + struct drm_plane_state *f1_state, *f0_state, *ipu_state; 201 208 long rate; 202 209 203 210 if (!drm_atomic_crtc_needs_modeset(state)) ··· 224 215 f1_state = drm_atomic_get_plane_state(state->state, &priv->f1); 225 216 f0_state = drm_atomic_get_plane_state(state->state, &priv->f0); 226 217 218 + if (IS_ENABLED(CONFIG_DRM_INGENIC_IPU) && priv->ipu_plane) { 219 + ipu_state = drm_atomic_get_plane_state(state->state, priv->ipu_plane); 220 + 221 + /* IPU and F1 planes cannot be enabled at the same time. */ 222 + if (f1_state->fb && ipu_state->fb) { 223 + dev_dbg(priv->dev, "Cannot enable both F1 and IPU\n"); 224 + return -EINVAL; 225 + } 226 + } 227 + 227 228 /* If all the planes are disabled, we won't get a VBLANK IRQ */ 228 - priv->no_vblank = !f1_state->fb && !f0_state->fb; 229 + priv->no_vblank = !f1_state->fb && !f0_state->fb && 230 + !(priv->ipu_plane && ipu_state->fb); 229 231 } 230 232 231 233 return 0; 234 + } 235 + 236 + static void ingenic_drm_crtc_atomic_begin(struct drm_crtc *crtc, 237 + struct drm_crtc_state *oldstate) 238 + { 239 + struct ingenic_drm *priv = drm_crtc_get_priv(crtc); 240 + u32 ctrl = 0; 241 + 242 + if (priv->soc_info->has_osd && 243 + drm_atomic_crtc_needs_modeset(crtc->state)) { 244 + /* 245 + * If IPU plane is enabled, enable IPU as source for the F1 246 + * plane; otherwise use regular DMA. 247 + */ 248 + if (priv->ipu_plane && priv->ipu_plane->state->fb) 249 + ctrl |= JZ_LCD_OSDCTRL_IPU; 250 + 251 + regmap_update_bits(priv->map, JZ_REG_LCD_OSDCTRL, 252 + JZ_LCD_OSDCTRL_IPU, ctrl); 253 + } 232 254 } 233 255 234 256 static void ingenic_drm_crtc_atomic_flush(struct drm_crtc *crtc, ··· 351 311 } 352 312 } 353 313 354 - static void ingenic_drm_plane_atomic_disable(struct drm_plane *plane, 355 - struct drm_plane_state *old_state) 314 + void ingenic_drm_plane_disable(struct device *dev, struct drm_plane *plane) 356 315 { 357 - struct ingenic_drm *priv = drm_device_get_priv(plane->dev); 316 + struct ingenic_drm *priv = dev_get_drvdata(dev); 358 317 unsigned int en_bit; 359 318 360 319 if (priv->soc_info->has_osd) { ··· 366 327 } 367 328 } 368 329 369 - static void ingenic_drm_plane_config(struct ingenic_drm *priv, 370 - struct drm_plane *plane, u32 fourcc) 330 + static void ingenic_drm_plane_atomic_disable(struct drm_plane *plane, 331 + struct drm_plane_state *old_state) 371 332 { 333 + struct ingenic_drm *priv = drm_device_get_priv(plane->dev); 334 + 335 + ingenic_drm_plane_disable(priv->dev, plane); 336 + } 337 + 338 + void ingenic_drm_plane_config(struct device *dev, 339 + struct drm_plane *plane, u32 fourcc) 340 + { 341 + struct ingenic_drm *priv = dev_get_drvdata(dev); 372 342 struct drm_plane_state *state = plane->state; 373 343 unsigned int xy_reg, size_reg; 374 344 unsigned int ctrl = 0; ··· 459 411 hwdesc->cmd = JZ_LCD_CMD_EOF_IRQ | (width * height * cpp / 4); 460 412 461 413 if (drm_atomic_crtc_needs_modeset(state->crtc->state)) 462 - ingenic_drm_plane_config(priv, plane, 414 + ingenic_drm_plane_config(priv->dev, plane, 463 415 state->fb->format->format); 464 416 } 465 417 } ··· 652 604 static const struct drm_crtc_helper_funcs ingenic_drm_crtc_helper_funcs = { 653 605 .atomic_enable = ingenic_drm_crtc_atomic_enable, 654 606 .atomic_disable = ingenic_drm_crtc_atomic_disable, 607 + .atomic_begin = ingenic_drm_crtc_atomic_begin, 655 608 .atomic_flush = ingenic_drm_crtc_atomic_flush, 656 609 .atomic_check = ingenic_drm_crtc_atomic_check, 657 610 }; ··· 673 624 .atomic_commit_tail = ingenic_drm_atomic_helper_commit_tail, 674 625 }; 675 626 676 - static int ingenic_drm_probe(struct platform_device *pdev) 627 + static void ingenic_drm_unbind_all(void *d) 677 628 { 629 + struct ingenic_drm *priv = d; 630 + 631 + component_unbind_all(priv->dev, &priv->drm); 632 + } 633 + 634 + static int ingenic_drm_bind(struct device *dev) 635 + { 636 + struct platform_device *pdev = to_platform_device(dev); 678 637 const struct jz_soc_info *soc_info; 679 - struct device *dev = &pdev->dev; 680 638 struct ingenic_drm *priv; 681 639 struct clk *parent_clk; 682 640 struct drm_bridge *bridge; ··· 784 728 priv->dma_hwdesc_f0->id = 0xf0; 785 729 } 786 730 731 + if (soc_info->has_osd) 732 + priv->ipu_plane = drm_plane_from_index(drm, 0); 733 + 787 734 drm_plane_helper_add(&priv->f1, &ingenic_drm_plane_helper_funcs); 788 735 789 736 ret = drm_universal_plane_init(drm, &priv->f1, 1, ··· 795 736 ARRAY_SIZE(ingenic_drm_primary_formats), 796 737 NULL, DRM_PLANE_TYPE_PRIMARY, NULL); 797 738 if (ret) { 798 - dev_err(dev, "Failed to register primary plane: %i\n", ret); 739 + dev_err(dev, "Failed to register plane: %i\n", ret); 799 740 return ret; 800 741 } 801 742 ··· 822 763 dev_err(dev, "Failed to register overlay plane: %i\n", 823 764 ret); 824 765 return ret; 766 + } 767 + 768 + if (IS_ENABLED(CONFIG_DRM_INGENIC_IPU)) { 769 + ret = component_bind_all(dev, drm); 770 + if (ret) { 771 + if (ret != -EPROBE_DEFER) 772 + dev_err(dev, "Failed to bind components: %i\n", ret); 773 + return ret; 774 + } 775 + 776 + ret = devm_add_action_or_reset(dev, ingenic_drm_unbind_all, priv); 777 + if (ret) 778 + return ret; 779 + 780 + priv->ipu_plane = drm_plane_from_index(drm, 2); 781 + if (!priv->ipu_plane) { 782 + dev_err(dev, "Failed to retrieve IPU plane\n"); 783 + return -EINVAL; 784 + } 825 785 } 826 786 } 827 787 ··· 930 852 return ret; 931 853 } 932 854 933 - static int ingenic_drm_remove(struct platform_device *pdev) 855 + static int compare_of(struct device *dev, void *data) 934 856 { 935 - struct ingenic_drm *priv = platform_get_drvdata(pdev); 857 + return dev->of_node == data; 858 + } 859 + 860 + static void ingenic_drm_unbind(struct device *dev) 861 + { 862 + struct ingenic_drm *priv = dev_get_drvdata(dev); 936 863 937 864 if (priv->lcd_clk) 938 865 clk_disable_unprepare(priv->lcd_clk); ··· 945 862 946 863 drm_dev_unregister(&priv->drm); 947 864 drm_atomic_helper_shutdown(&priv->drm); 865 + } 866 + 867 + static const struct component_master_ops ingenic_master_ops = { 868 + .bind = ingenic_drm_bind, 869 + .unbind = ingenic_drm_unbind, 870 + }; 871 + 872 + static int ingenic_drm_probe(struct platform_device *pdev) 873 + { 874 + struct device *dev = &pdev->dev; 875 + struct component_match *match = NULL; 876 + struct device_node *np; 877 + 878 + if (!IS_ENABLED(CONFIG_DRM_INGENIC_IPU)) 879 + return ingenic_drm_bind(dev); 880 + 881 + /* IPU is at port address 8 */ 882 + np = of_graph_get_remote_node(dev->of_node, 8, 0); 883 + if (!np) { 884 + dev_err(dev, "Unable to get IPU node\n"); 885 + return -EINVAL; 886 + } 887 + 888 + drm_of_component_match_add(dev, &match, compare_of, np); 889 + 890 + return component_master_add_with_match(dev, &ingenic_master_ops, match); 891 + } 892 + 893 + static int ingenic_drm_remove(struct platform_device *pdev) 894 + { 895 + struct device *dev = &pdev->dev; 896 + 897 + if (!IS_ENABLED(CONFIG_DRM_INGENIC_IPU)) 898 + ingenic_drm_unbind(dev); 899 + else 900 + component_master_del(dev, &ingenic_master_ops); 948 901 949 902 return 0; 950 903 } ··· 1022 903 .probe = ingenic_drm_probe, 1023 904 .remove = ingenic_drm_remove, 1024 905 }; 1025 - module_platform_driver(ingenic_drm_driver); 906 + 907 + static int ingenic_drm_init(void) 908 + { 909 + int err; 910 + 911 + if (IS_ENABLED(CONFIG_DRM_INGENIC_IPU)) { 912 + err = platform_driver_register(ingenic_ipu_driver_ptr); 913 + if (err) 914 + return err; 915 + } 916 + 917 + return platform_driver_register(&ingenic_drm_driver); 918 + } 919 + module_init(ingenic_drm_init); 920 + 921 + static void ingenic_drm_exit(void) 922 + { 923 + platform_driver_unregister(&ingenic_drm_driver); 924 + 925 + if (IS_ENABLED(CONFIG_DRM_INGENIC_IPU)) 926 + platform_driver_unregister(ingenic_ipu_driver_ptr); 927 + } 928 + module_exit(ingenic_drm_exit); 1026 929 1027 930 MODULE_AUTHOR("Paul Cercueil <paul@crapouillou.net>"); 1028 931 MODULE_DESCRIPTION("DRM driver for the Ingenic SoCs\n");
+12
drivers/gpu/drm/ingenic/ingenic-drm.h
··· 8 8 #define DRIVERS_GPU_DRM_INGENIC_INGENIC_DRM_H 9 9 10 10 #include <linux/bitops.h> 11 + #include <linux/types.h> 11 12 12 13 #define JZ_REG_LCD_CFG 0x00 13 14 #define JZ_REG_LCD_VSYNC 0x04 ··· 158 157 159 158 #define JZ_LCD_SIZE01_WIDTH_LSB 0 160 159 #define JZ_LCD_SIZE01_HEIGHT_LSB 16 160 + 161 + struct device; 162 + struct drm_plane; 163 + struct drm_plane_state; 164 + struct platform_driver; 165 + 166 + void ingenic_drm_plane_config(struct device *dev, 167 + struct drm_plane *plane, u32 fourcc); 168 + void ingenic_drm_plane_disable(struct device *dev, struct drm_plane *plane); 169 + 170 + extern struct platform_driver *ingenic_ipu_driver_ptr; 161 171 162 172 #endif /* DRIVERS_GPU_DRM_INGENIC_INGENIC_DRM_H */
+853
drivers/gpu/drm/ingenic/ingenic-ipu.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + // 3 + // Ingenic JZ47xx IPU driver 4 + // 5 + // Copyright (C) 2020, Paul Cercueil <paul@crapouillou.net> 6 + // Copyright (C) 2020, Daniel Silsby <dansilsby@gmail.com> 7 + 8 + #include "ingenic-drm.h" 9 + #include "ingenic-ipu.h" 10 + 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> 16 + #include <linux/of.h> 17 + #include <linux/of_device.h> 18 + #include <linux/regmap.h> 19 + #include <linux/time.h> 20 + 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> 31 + 32 + struct ingenic_ipu; 33 + 34 + struct soc_info { 35 + const u32 *formats; 36 + size_t num_formats; 37 + bool has_bicubic; 38 + 39 + void (*set_coefs)(struct ingenic_ipu *ipu, unsigned int reg, 40 + unsigned int sharpness, bool downscale, 41 + unsigned int weight, unsigned int offset); 42 + }; 43 + 44 + struct ingenic_ipu { 45 + struct drm_plane plane; 46 + struct drm_device *drm; 47 + struct device *dev, *master; 48 + struct regmap *map; 49 + struct clk *clk; 50 + const struct soc_info *soc_info; 51 + 52 + unsigned int num_w, num_h, denom_w, denom_h; 53 + 54 + dma_addr_t addr_y, addr_u, addr_v; 55 + 56 + struct drm_property *sharpness_prop; 57 + unsigned int sharpness; 58 + }; 59 + 60 + /* Signed 15.16 fixed-point math (for bicubic scaling coefficients) */ 61 + #define I2F(i) ((s32)(i) * 65536) 62 + #define F2I(f) ((f) / 65536) 63 + #define FMUL(fa, fb) ((s32)(((s64)(fa) * (s64)(fb)) / 65536)) 64 + #define SHARPNESS_INCR (I2F(-1) / 8) 65 + 66 + static inline struct ingenic_ipu *plane_to_ingenic_ipu(struct drm_plane *plane) 67 + { 68 + return container_of(plane, struct ingenic_ipu, plane); 69 + } 70 + 71 + /* 72 + * Apply conventional cubic convolution kernel. Both parameters 73 + * and return value are 15.16 signed fixed-point. 74 + * 75 + * @f_a: Sharpness factor, typically in range [-4.0, -0.25]. 76 + * A larger magnitude increases perceived sharpness, but going past 77 + * -2.0 might cause ringing artifacts to outweigh any improvement. 78 + * Nice values on a 320x240 LCD are between -0.75 and -2.0. 79 + * 80 + * @f_x: Absolute distance in pixels from 'pixel 0' sample position 81 + * along horizontal (or vertical) source axis. Range is [0, +2.0]. 82 + * 83 + * returns: Weight of this pixel within 4-pixel sample group. Range is 84 + * [-2.0, +2.0]. For moderate (i.e. > -3.0) sharpness factors, 85 + * range is within [-1.0, +1.0]. 86 + */ 87 + static inline s32 cubic_conv(s32 f_a, s32 f_x) 88 + { 89 + const s32 f_1 = I2F(1); 90 + const s32 f_2 = I2F(2); 91 + const s32 f_3 = I2F(3); 92 + const s32 f_4 = I2F(4); 93 + const s32 f_x2 = FMUL(f_x, f_x); 94 + const s32 f_x3 = FMUL(f_x, f_x2); 95 + 96 + if (f_x <= f_1) 97 + return FMUL((f_a + f_2), f_x3) - FMUL((f_a + f_3), f_x2) + f_1; 98 + else if (f_x <= f_2) 99 + return FMUL(f_a, (f_x3 - 5 * f_x2 + 8 * f_x - f_4)); 100 + else 101 + return 0; 102 + } 103 + 104 + /* 105 + * On entry, "weight" is a coefficient suitable for bilinear mode, 106 + * which is converted to a set of four suitable for bicubic mode. 107 + * 108 + * "weight 512" means all of pixel 0; 109 + * "weight 256" means half of pixel 0 and half of pixel 1; 110 + * "weight 0" means all of pixel 1; 111 + * 112 + * "offset" is increment to next source pixel sample location. 113 + */ 114 + static void jz4760_set_coefs(struct ingenic_ipu *ipu, unsigned int reg, 115 + unsigned int sharpness, bool downscale, 116 + unsigned int weight, unsigned int offset) 117 + { 118 + u32 val; 119 + s32 w0, w1, w2, w3; /* Pixel weights at X (or Y) offsets -1,0,1,2 */ 120 + 121 + weight = clamp_val(weight, 0, 512); 122 + 123 + if (sharpness < 2) { 124 + /* 125 + * When sharpness setting is 0, emulate nearest-neighbor. 126 + * When sharpness setting is 1, emulate bilinear. 127 + */ 128 + 129 + if (sharpness == 0) 130 + weight = weight >= 256 ? 512 : 0; 131 + w0 = 0; 132 + w1 = weight; 133 + w2 = 512 - weight; 134 + w3 = 0; 135 + } else { 136 + const s32 f_a = SHARPNESS_INCR * sharpness; 137 + const s32 f_h = I2F(1) / 2; /* Round up 0.5 */ 138 + 139 + /* 140 + * Note that always rounding towards +infinity here is intended. 141 + * The resulting coefficients match a round-to-nearest-int 142 + * double floating-point implementation. 143 + */ 144 + 145 + weight = 512 - weight; 146 + w0 = F2I(f_h + 512 * cubic_conv(f_a, I2F(512 + weight) / 512)); 147 + w1 = F2I(f_h + 512 * cubic_conv(f_a, I2F(0 + weight) / 512)); 148 + w2 = F2I(f_h + 512 * cubic_conv(f_a, I2F(512 - weight) / 512)); 149 + w3 = F2I(f_h + 512 * cubic_conv(f_a, I2F(1024 - weight) / 512)); 150 + w0 = clamp_val(w0, -1024, 1023); 151 + w1 = clamp_val(w1, -1024, 1023); 152 + w2 = clamp_val(w2, -1024, 1023); 153 + w3 = clamp_val(w3, -1024, 1023); 154 + } 155 + 156 + val = ((w1 & JZ4760_IPU_RSZ_COEF_MASK) << JZ4760_IPU_RSZ_COEF31_LSB) | 157 + ((w0 & JZ4760_IPU_RSZ_COEF_MASK) << JZ4760_IPU_RSZ_COEF20_LSB); 158 + regmap_write(ipu->map, reg, val); 159 + 160 + val = ((w3 & JZ4760_IPU_RSZ_COEF_MASK) << JZ4760_IPU_RSZ_COEF31_LSB) | 161 + ((w2 & JZ4760_IPU_RSZ_COEF_MASK) << JZ4760_IPU_RSZ_COEF20_LSB) | 162 + ((offset & JZ4760_IPU_RSZ_OFFSET_MASK) << JZ4760_IPU_RSZ_OFFSET_LSB); 163 + regmap_write(ipu->map, reg, val); 164 + } 165 + 166 + static void jz4725b_set_coefs(struct ingenic_ipu *ipu, unsigned int reg, 167 + unsigned int sharpness, bool downscale, 168 + unsigned int weight, unsigned int offset) 169 + { 170 + u32 val = JZ4725B_IPU_RSZ_LUT_OUT_EN; 171 + unsigned int i; 172 + 173 + weight = clamp_val(weight, 0, 512); 174 + 175 + if (sharpness == 0) 176 + weight = weight >= 256 ? 512 : 0; 177 + 178 + val |= (weight & JZ4725B_IPU_RSZ_LUT_COEF_MASK) << JZ4725B_IPU_RSZ_LUT_COEF_LSB; 179 + if (downscale || !!offset) 180 + val |= JZ4725B_IPU_RSZ_LUT_IN_EN; 181 + 182 + regmap_write(ipu->map, reg, val); 183 + 184 + if (downscale) { 185 + for (i = 1; i < offset; i++) 186 + regmap_write(ipu->map, reg, JZ4725B_IPU_RSZ_LUT_IN_EN); 187 + } 188 + } 189 + 190 + static void ingenic_ipu_set_downscale_coefs(struct ingenic_ipu *ipu, 191 + unsigned int reg, 192 + unsigned int num, 193 + unsigned int denom) 194 + { 195 + unsigned int i, offset, weight, weight_num = denom; 196 + 197 + for (i = 0; i < num; i++) { 198 + weight_num = num + (weight_num - num) % (num * 2); 199 + weight = 512 - 512 * (weight_num - num) / (num * 2); 200 + weight_num += denom * 2; 201 + offset = (weight_num - num) / (num * 2); 202 + 203 + ipu->soc_info->set_coefs(ipu, reg, ipu->sharpness, 204 + true, weight, offset); 205 + } 206 + } 207 + 208 + static void ingenic_ipu_set_integer_upscale_coefs(struct ingenic_ipu *ipu, 209 + unsigned int reg, 210 + unsigned int num) 211 + { 212 + /* 213 + * Force nearest-neighbor scaling and use simple math when upscaling 214 + * by an integer ratio. It looks better, and fixes a few problem cases. 215 + */ 216 + unsigned int i; 217 + 218 + for (i = 0; i < num; i++) 219 + ipu->soc_info->set_coefs(ipu, reg, 0, false, 512, i == num - 1); 220 + } 221 + 222 + static void ingenic_ipu_set_upscale_coefs(struct ingenic_ipu *ipu, 223 + unsigned int reg, 224 + unsigned int num, 225 + unsigned int denom) 226 + { 227 + unsigned int i, offset, weight, weight_num = 0; 228 + 229 + for (i = 0; i < num; i++) { 230 + weight = 512 - 512 * weight_num / num; 231 + weight_num += denom; 232 + offset = weight_num >= num; 233 + 234 + if (offset) 235 + weight_num -= num; 236 + 237 + ipu->soc_info->set_coefs(ipu, reg, ipu->sharpness, 238 + false, weight, offset); 239 + } 240 + } 241 + 242 + static void ingenic_ipu_set_coefs(struct ingenic_ipu *ipu, unsigned int reg, 243 + unsigned int num, unsigned int denom) 244 + { 245 + /* Begin programming the LUT */ 246 + regmap_write(ipu->map, reg, -1); 247 + 248 + if (denom > num) 249 + ingenic_ipu_set_downscale_coefs(ipu, reg, num, denom); 250 + else if (denom == 1) 251 + ingenic_ipu_set_integer_upscale_coefs(ipu, reg, num); 252 + else 253 + ingenic_ipu_set_upscale_coefs(ipu, reg, num, denom); 254 + } 255 + 256 + static int reduce_fraction(unsigned int *num, unsigned int *denom) 257 + { 258 + unsigned long d = gcd(*num, *denom); 259 + 260 + /* The scaling table has only 31 entries */ 261 + if (*num > 31 * d) 262 + return -EINVAL; 263 + 264 + *num /= d; 265 + *denom /= d; 266 + return 0; 267 + } 268 + 269 + static inline bool osd_changed(struct drm_plane_state *state, 270 + struct drm_plane_state *oldstate) 271 + { 272 + return state->src_x != oldstate->src_x || 273 + state->src_y != oldstate->src_y || 274 + state->src_w != oldstate->src_w || 275 + state->src_h != oldstate->src_h || 276 + state->crtc_x != oldstate->crtc_x || 277 + state->crtc_y != oldstate->crtc_y || 278 + state->crtc_w != oldstate->crtc_w || 279 + state->crtc_h != oldstate->crtc_h; 280 + } 281 + 282 + static void ingenic_ipu_plane_atomic_update(struct drm_plane *plane, 283 + struct drm_plane_state *oldstate) 284 + { 285 + struct ingenic_ipu *ipu = plane_to_ingenic_ipu(plane); 286 + struct drm_plane_state *state = plane->state; 287 + const struct drm_format_info *finfo; 288 + u32 ctrl, stride = 0, coef_index = 0, format = 0; 289 + bool needs_modeset, upscaling_w, upscaling_h; 290 + 291 + if (!state || !state->fb) 292 + return; 293 + 294 + finfo = drm_format_info(state->fb->format->format); 295 + 296 + /* Reset all the registers if needed */ 297 + needs_modeset = drm_atomic_crtc_needs_modeset(state->crtc->state); 298 + if (needs_modeset) { 299 + regmap_set_bits(ipu->map, JZ_REG_IPU_CTRL, JZ_IPU_CTRL_RST); 300 + 301 + /* Enable the chip */ 302 + regmap_set_bits(ipu->map, JZ_REG_IPU_CTRL, 303 + JZ_IPU_CTRL_CHIP_EN | JZ_IPU_CTRL_LCDC_SEL); 304 + } 305 + 306 + /* New addresses will be committed in vblank handler... */ 307 + ipu->addr_y = drm_fb_cma_get_gem_addr(state->fb, state, 0); 308 + if (finfo->num_planes > 1) 309 + ipu->addr_u = drm_fb_cma_get_gem_addr(state->fb, state, 1); 310 + if (finfo->num_planes > 2) 311 + ipu->addr_v = drm_fb_cma_get_gem_addr(state->fb, state, 2); 312 + 313 + if (!needs_modeset) 314 + return; 315 + 316 + /* Or right here if we're doing a full modeset. */ 317 + regmap_write(ipu->map, JZ_REG_IPU_Y_ADDR, ipu->addr_y); 318 + regmap_write(ipu->map, JZ_REG_IPU_U_ADDR, ipu->addr_u); 319 + regmap_write(ipu->map, JZ_REG_IPU_V_ADDR, ipu->addr_v); 320 + 321 + if (finfo->num_planes == 1) 322 + regmap_set_bits(ipu->map, JZ_REG_IPU_CTRL, JZ_IPU_CTRL_SPKG_SEL); 323 + 324 + ingenic_drm_plane_config(ipu->master, plane, DRM_FORMAT_XRGB8888); 325 + 326 + /* Set the input height/width/strides */ 327 + if (finfo->num_planes > 2) 328 + stride = ((state->src_w >> 16) * finfo->cpp[2] / finfo->hsub) 329 + << JZ_IPU_UV_STRIDE_V_LSB; 330 + 331 + if (finfo->num_planes > 1) 332 + stride |= ((state->src_w >> 16) * finfo->cpp[1] / finfo->hsub) 333 + << JZ_IPU_UV_STRIDE_U_LSB; 334 + 335 + regmap_write(ipu->map, JZ_REG_IPU_UV_STRIDE, stride); 336 + 337 + stride = ((state->src_w >> 16) * finfo->cpp[0]) << JZ_IPU_Y_STRIDE_Y_LSB; 338 + regmap_write(ipu->map, JZ_REG_IPU_Y_STRIDE, stride); 339 + 340 + regmap_write(ipu->map, JZ_REG_IPU_IN_GS, 341 + (stride << JZ_IPU_IN_GS_W_LSB) | 342 + ((state->src_h >> 16) << JZ_IPU_IN_GS_H_LSB)); 343 + 344 + switch (finfo->format) { 345 + case DRM_FORMAT_XRGB1555: 346 + format = JZ_IPU_D_FMT_IN_FMT_RGB555 | 347 + JZ_IPU_D_FMT_RGB_OUT_OFT_RGB; 348 + break; 349 + case DRM_FORMAT_XBGR1555: 350 + format = JZ_IPU_D_FMT_IN_FMT_RGB555 | 351 + JZ_IPU_D_FMT_RGB_OUT_OFT_BGR; 352 + break; 353 + case DRM_FORMAT_RGB565: 354 + format = JZ_IPU_D_FMT_IN_FMT_RGB565 | 355 + JZ_IPU_D_FMT_RGB_OUT_OFT_RGB; 356 + break; 357 + case DRM_FORMAT_BGR565: 358 + format = JZ_IPU_D_FMT_IN_FMT_RGB565 | 359 + JZ_IPU_D_FMT_RGB_OUT_OFT_BGR; 360 + break; 361 + case DRM_FORMAT_XRGB8888: 362 + case DRM_FORMAT_XYUV8888: 363 + format = JZ_IPU_D_FMT_IN_FMT_RGB888 | 364 + JZ_IPU_D_FMT_RGB_OUT_OFT_RGB; 365 + break; 366 + case DRM_FORMAT_XBGR8888: 367 + format = JZ_IPU_D_FMT_IN_FMT_RGB888 | 368 + JZ_IPU_D_FMT_RGB_OUT_OFT_BGR; 369 + break; 370 + case DRM_FORMAT_YUYV: 371 + format = JZ_IPU_D_FMT_IN_FMT_YUV422 | 372 + JZ_IPU_D_FMT_YUV_VY1UY0; 373 + break; 374 + case DRM_FORMAT_YVYU: 375 + format = JZ_IPU_D_FMT_IN_FMT_YUV422 | 376 + JZ_IPU_D_FMT_YUV_UY1VY0; 377 + break; 378 + case DRM_FORMAT_UYVY: 379 + format = JZ_IPU_D_FMT_IN_FMT_YUV422 | 380 + JZ_IPU_D_FMT_YUV_Y1VY0U; 381 + break; 382 + case DRM_FORMAT_VYUY: 383 + format = JZ_IPU_D_FMT_IN_FMT_YUV422 | 384 + JZ_IPU_D_FMT_YUV_Y1UY0V; 385 + break; 386 + case DRM_FORMAT_YUV411: 387 + format = JZ_IPU_D_FMT_IN_FMT_YUV411; 388 + break; 389 + case DRM_FORMAT_YUV420: 390 + format = JZ_IPU_D_FMT_IN_FMT_YUV420; 391 + break; 392 + case DRM_FORMAT_YUV422: 393 + format = JZ_IPU_D_FMT_IN_FMT_YUV422; 394 + break; 395 + case DRM_FORMAT_YUV444: 396 + format = JZ_IPU_D_FMT_IN_FMT_YUV444; 397 + break; 398 + default: 399 + WARN_ONCE(1, "Unsupported format"); 400 + break; 401 + } 402 + 403 + /* Fix output to RGB888 */ 404 + format |= JZ_IPU_D_FMT_OUT_FMT_RGB888; 405 + 406 + /* Set pixel format */ 407 + regmap_write(ipu->map, JZ_REG_IPU_D_FMT, format); 408 + 409 + /* Set the output height/width/stride */ 410 + regmap_write(ipu->map, JZ_REG_IPU_OUT_GS, 411 + ((state->crtc_w * 4) << JZ_IPU_OUT_GS_W_LSB) 412 + | state->crtc_h << JZ_IPU_OUT_GS_H_LSB); 413 + regmap_write(ipu->map, JZ_REG_IPU_OUT_STRIDE, state->crtc_w * 4); 414 + 415 + if (finfo->is_yuv) { 416 + regmap_set_bits(ipu->map, JZ_REG_IPU_CTRL, JZ_IPU_CTRL_CSC_EN); 417 + 418 + /* 419 + * Offsets for Chroma/Luma. 420 + * y = source Y - LUMA, 421 + * u = source Cb - CHROMA, 422 + * v = source Cr - CHROMA 423 + */ 424 + regmap_write(ipu->map, JZ_REG_IPU_CSC_OFFSET, 425 + 128 << JZ_IPU_CSC_OFFSET_CHROMA_LSB | 426 + 0 << JZ_IPU_CSC_OFFSET_LUMA_LSB); 427 + 428 + /* 429 + * YUV422 to RGB conversion table. 430 + * R = C0 / 0x400 * y + C1 / 0x400 * v 431 + * G = C0 / 0x400 * y - C2 / 0x400 * u - C3 / 0x400 * v 432 + * B = C0 / 0x400 * y + C4 / 0x400 * u 433 + */ 434 + regmap_write(ipu->map, JZ_REG_IPU_CSC_C0_COEF, 0x4a8); 435 + regmap_write(ipu->map, JZ_REG_IPU_CSC_C1_COEF, 0x662); 436 + regmap_write(ipu->map, JZ_REG_IPU_CSC_C2_COEF, 0x191); 437 + regmap_write(ipu->map, JZ_REG_IPU_CSC_C3_COEF, 0x341); 438 + regmap_write(ipu->map, JZ_REG_IPU_CSC_C4_COEF, 0x811); 439 + } 440 + 441 + ctrl = 0; 442 + 443 + /* 444 + * Must set ZOOM_SEL before programming bicubic LUTs. 445 + * If the IPU supports bicubic, we enable it unconditionally, since it 446 + * can do anything bilinear can and more. 447 + */ 448 + if (ipu->soc_info->has_bicubic) 449 + ctrl |= JZ_IPU_CTRL_ZOOM_SEL; 450 + 451 + upscaling_w = ipu->num_w > ipu->denom_w; 452 + if (upscaling_w) 453 + ctrl |= JZ_IPU_CTRL_HSCALE; 454 + 455 + if (ipu->num_w != 1 || ipu->denom_w != 1) { 456 + if (!ipu->soc_info->has_bicubic && !upscaling_w) 457 + coef_index |= (ipu->denom_w - 1) << 16; 458 + else 459 + coef_index |= (ipu->num_w - 1) << 16; 460 + ctrl |= JZ_IPU_CTRL_HRSZ_EN; 461 + } 462 + 463 + upscaling_h = ipu->num_h > ipu->denom_h; 464 + if (upscaling_h) 465 + ctrl |= JZ_IPU_CTRL_VSCALE; 466 + 467 + if (ipu->num_h != 1 || ipu->denom_h != 1) { 468 + if (!ipu->soc_info->has_bicubic && !upscaling_h) 469 + coef_index |= ipu->denom_h - 1; 470 + else 471 + coef_index |= ipu->num_h - 1; 472 + ctrl |= JZ_IPU_CTRL_VRSZ_EN; 473 + } 474 + 475 + regmap_update_bits(ipu->map, JZ_REG_IPU_CTRL, JZ_IPU_CTRL_ZOOM_SEL | 476 + JZ_IPU_CTRL_HRSZ_EN | JZ_IPU_CTRL_VRSZ_EN | 477 + JZ_IPU_CTRL_HSCALE | JZ_IPU_CTRL_VSCALE, ctrl); 478 + 479 + /* Set the LUT index register */ 480 + regmap_write(ipu->map, JZ_REG_IPU_RSZ_COEF_INDEX, coef_index); 481 + 482 + if (ipu->num_w != 1 || ipu->denom_w != 1) 483 + ingenic_ipu_set_coefs(ipu, JZ_REG_IPU_HRSZ_COEF_LUT, 484 + ipu->num_w, ipu->denom_w); 485 + 486 + if (ipu->num_h != 1 || ipu->denom_h != 1) 487 + ingenic_ipu_set_coefs(ipu, JZ_REG_IPU_VRSZ_COEF_LUT, 488 + ipu->num_h, ipu->denom_h); 489 + 490 + /* Clear STATUS register */ 491 + regmap_write(ipu->map, JZ_REG_IPU_STATUS, 0); 492 + 493 + /* Start IPU */ 494 + regmap_set_bits(ipu->map, JZ_REG_IPU_CTRL, 495 + JZ_IPU_CTRL_RUN | JZ_IPU_CTRL_FM_IRQ_EN); 496 + 497 + dev_dbg(ipu->dev, "Scaling %ux%u to %ux%u (%u:%u horiz, %u:%u vert)\n", 498 + state->src_w >> 16, state->src_h >> 16, 499 + state->crtc_w, state->crtc_h, 500 + ipu->num_w, ipu->denom_w, ipu->num_h, ipu->denom_h); 501 + } 502 + 503 + static int ingenic_ipu_plane_atomic_check(struct drm_plane *plane, 504 + struct drm_plane_state *state) 505 + { 506 + unsigned int num_w, denom_w, num_h, denom_h, xres, yres; 507 + struct ingenic_ipu *ipu = plane_to_ingenic_ipu(plane); 508 + struct drm_crtc *crtc = state->crtc ?: plane->state->crtc; 509 + struct drm_crtc_state *crtc_state; 510 + 511 + if (!crtc) 512 + return 0; 513 + 514 + crtc_state = drm_atomic_get_existing_crtc_state(state->state, crtc); 515 + if (WARN_ON(!crtc_state)) 516 + return -EINVAL; 517 + 518 + /* Request a full modeset if we are enabling or disabling the IPU. */ 519 + if (!plane->state->crtc ^ !state->crtc) 520 + crtc_state->mode_changed = true; 521 + 522 + if (!state->crtc || 523 + !crtc_state->mode.hdisplay || !crtc_state->mode.vdisplay) 524 + return 0; 525 + 526 + /* Plane must be fully visible */ 527 + if (state->crtc_x < 0 || state->crtc_y < 0 || 528 + state->crtc_x + state->crtc_w > crtc_state->mode.hdisplay || 529 + state->crtc_y + state->crtc_h > crtc_state->mode.vdisplay) 530 + return -EINVAL; 531 + 532 + /* Minimum size is 4x4 */ 533 + if ((state->src_w >> 16) < 4 || (state->src_h >> 16) < 4) 534 + return -EINVAL; 535 + 536 + /* Input and output lines must have an even number of pixels. */ 537 + if (((state->src_w >> 16) & 1) || (state->crtc_w & 1)) 538 + return -EINVAL; 539 + 540 + if (!osd_changed(state, plane->state)) 541 + return 0; 542 + 543 + crtc_state->mode_changed = true; 544 + 545 + xres = state->src_w >> 16; 546 + yres = state->src_h >> 16; 547 + 548 + /* Adjust the coefficients until we find a valid configuration */ 549 + for (denom_w = xres, num_w = state->crtc_w; 550 + num_w <= crtc_state->mode.hdisplay; num_w++) 551 + if (!reduce_fraction(&num_w, &denom_w)) 552 + break; 553 + if (num_w > crtc_state->mode.hdisplay) 554 + return -EINVAL; 555 + 556 + for (denom_h = yres, num_h = state->crtc_h; 557 + num_h <= crtc_state->mode.vdisplay; num_h++) 558 + if (!reduce_fraction(&num_h, &denom_h)) 559 + break; 560 + if (num_h > crtc_state->mode.vdisplay) 561 + return -EINVAL; 562 + 563 + ipu->num_w = num_w; 564 + ipu->num_h = num_h; 565 + ipu->denom_w = denom_w; 566 + ipu->denom_h = denom_h; 567 + 568 + return 0; 569 + } 570 + 571 + static void ingenic_ipu_plane_atomic_disable(struct drm_plane *plane, 572 + struct drm_plane_state *old_state) 573 + { 574 + struct ingenic_ipu *ipu = plane_to_ingenic_ipu(plane); 575 + 576 + regmap_set_bits(ipu->map, JZ_REG_IPU_CTRL, JZ_IPU_CTRL_STOP); 577 + regmap_clear_bits(ipu->map, JZ_REG_IPU_CTRL, JZ_IPU_CTRL_CHIP_EN); 578 + 579 + ingenic_drm_plane_disable(ipu->master, plane); 580 + } 581 + 582 + static const struct drm_plane_helper_funcs ingenic_ipu_plane_helper_funcs = { 583 + .atomic_update = ingenic_ipu_plane_atomic_update, 584 + .atomic_check = ingenic_ipu_plane_atomic_check, 585 + .atomic_disable = ingenic_ipu_plane_atomic_disable, 586 + .prepare_fb = drm_gem_fb_prepare_fb, 587 + }; 588 + 589 + static int 590 + ingenic_ipu_plane_atomic_get_property(struct drm_plane *plane, 591 + const struct drm_plane_state *state, 592 + struct drm_property *property, u64 *val) 593 + { 594 + struct ingenic_ipu *ipu = plane_to_ingenic_ipu(plane); 595 + 596 + if (property != ipu->sharpness_prop) 597 + return -EINVAL; 598 + 599 + *val = ipu->sharpness; 600 + 601 + return 0; 602 + } 603 + 604 + static int 605 + ingenic_ipu_plane_atomic_set_property(struct drm_plane *plane, 606 + struct drm_plane_state *state, 607 + struct drm_property *property, u64 val) 608 + { 609 + struct ingenic_ipu *ipu = plane_to_ingenic_ipu(plane); 610 + struct drm_crtc_state *crtc_state; 611 + 612 + if (property != ipu->sharpness_prop) 613 + return -EINVAL; 614 + 615 + ipu->sharpness = val; 616 + 617 + if (state->crtc) { 618 + crtc_state = drm_atomic_get_existing_crtc_state(state->state, state->crtc); 619 + if (WARN_ON(!crtc_state)) 620 + return -EINVAL; 621 + 622 + crtc_state->mode_changed = true; 623 + } 624 + 625 + return 0; 626 + } 627 + 628 + static const struct drm_plane_funcs ingenic_ipu_plane_funcs = { 629 + .update_plane = drm_atomic_helper_update_plane, 630 + .disable_plane = drm_atomic_helper_disable_plane, 631 + .reset = drm_atomic_helper_plane_reset, 632 + .destroy = drm_plane_cleanup, 633 + 634 + .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state, 635 + .atomic_destroy_state = drm_atomic_helper_plane_destroy_state, 636 + 637 + .atomic_get_property = ingenic_ipu_plane_atomic_get_property, 638 + .atomic_set_property = ingenic_ipu_plane_atomic_set_property, 639 + }; 640 + 641 + static irqreturn_t ingenic_ipu_irq_handler(int irq, void *arg) 642 + { 643 + struct ingenic_ipu *ipu = arg; 644 + struct drm_crtc *crtc = drm_crtc_from_index(ipu->drm, 0); 645 + unsigned int dummy; 646 + 647 + /* dummy read allows CPU to reconfigure IPU */ 648 + regmap_read(ipu->map, JZ_REG_IPU_STATUS, &dummy); 649 + 650 + /* ACK interrupt */ 651 + regmap_write(ipu->map, JZ_REG_IPU_STATUS, 0); 652 + 653 + /* Set previously cached addresses */ 654 + regmap_write(ipu->map, JZ_REG_IPU_Y_ADDR, ipu->addr_y); 655 + regmap_write(ipu->map, JZ_REG_IPU_U_ADDR, ipu->addr_u); 656 + regmap_write(ipu->map, JZ_REG_IPU_V_ADDR, ipu->addr_v); 657 + 658 + /* Run IPU for the new frame */ 659 + regmap_set_bits(ipu->map, JZ_REG_IPU_CTRL, JZ_IPU_CTRL_RUN); 660 + 661 + drm_crtc_handle_vblank(crtc); 662 + 663 + return IRQ_HANDLED; 664 + } 665 + 666 + static const struct regmap_config ingenic_ipu_regmap_config = { 667 + .reg_bits = 32, 668 + .val_bits = 32, 669 + .reg_stride = 4, 670 + 671 + .max_register = JZ_REG_IPU_OUT_PHY_T_ADDR, 672 + }; 673 + 674 + static int ingenic_ipu_bind(struct device *dev, struct device *master, void *d) 675 + { 676 + struct platform_device *pdev = to_platform_device(dev); 677 + const struct soc_info *soc_info; 678 + struct drm_device *drm = d; 679 + struct drm_plane *plane; 680 + struct ingenic_ipu *ipu; 681 + void __iomem *base; 682 + unsigned int sharpness_max; 683 + int err, irq; 684 + 685 + ipu = devm_kzalloc(dev, sizeof(*ipu), GFP_KERNEL); 686 + if (!ipu) 687 + return -ENOMEM; 688 + 689 + soc_info = of_device_get_match_data(dev); 690 + if (!soc_info) { 691 + dev_err(dev, "Missing platform data\n"); 692 + return -EINVAL; 693 + } 694 + 695 + ipu->dev = dev; 696 + ipu->drm = drm; 697 + ipu->master = master; 698 + ipu->soc_info = soc_info; 699 + 700 + base = devm_platform_ioremap_resource(pdev, 0); 701 + if (IS_ERR(base)) { 702 + dev_err(dev, "Failed to get memory resource\n"); 703 + return PTR_ERR(base); 704 + } 705 + 706 + ipu->map = devm_regmap_init_mmio(dev, base, &ingenic_ipu_regmap_config); 707 + if (IS_ERR(ipu->map)) { 708 + dev_err(dev, "Failed to create regmap\n"); 709 + return PTR_ERR(ipu->map); 710 + } 711 + 712 + irq = platform_get_irq(pdev, 0); 713 + if (irq < 0) 714 + return irq; 715 + 716 + ipu->clk = devm_clk_get(dev, "ipu"); 717 + if (IS_ERR(ipu->clk)) { 718 + dev_err(dev, "Failed to get pixel clock\n"); 719 + return PTR_ERR(ipu->clk); 720 + } 721 + 722 + err = devm_request_irq(dev, irq, ingenic_ipu_irq_handler, 0, 723 + dev_name(dev), ipu); 724 + if (err) { 725 + dev_err(dev, "Unable to request IRQ\n"); 726 + return err; 727 + } 728 + 729 + plane = &ipu->plane; 730 + dev_set_drvdata(dev, plane); 731 + 732 + drm_plane_helper_add(plane, &ingenic_ipu_plane_helper_funcs); 733 + 734 + err = drm_universal_plane_init(drm, plane, 1, &ingenic_ipu_plane_funcs, 735 + soc_info->formats, soc_info->num_formats, 736 + NULL, DRM_PLANE_TYPE_PRIMARY, NULL); 737 + if (err) { 738 + dev_err(dev, "Failed to init plane: %i\n", err); 739 + return err; 740 + } 741 + 742 + /* 743 + * Sharpness settings range is [0,32] 744 + * 0 : nearest-neighbor 745 + * 1 : bilinear 746 + * 2 .. 32 : bicubic (translated to sharpness factor -0.25 .. -4.0) 747 + */ 748 + sharpness_max = soc_info->has_bicubic ? 32 : 1; 749 + ipu->sharpness_prop = drm_property_create_range(drm, 0, "sharpness", 750 + 0, sharpness_max); 751 + if (!ipu->sharpness_prop) { 752 + dev_err(dev, "Unable to create sharpness property\n"); 753 + return -ENOMEM; 754 + } 755 + 756 + /* Default sharpness factor: -0.125 * 8 = -1.0 */ 757 + ipu->sharpness = soc_info->has_bicubic ? 8 : 1; 758 + drm_object_attach_property(&plane->base, ipu->sharpness_prop, 759 + ipu->sharpness); 760 + 761 + err = clk_prepare_enable(ipu->clk); 762 + if (err) { 763 + dev_err(dev, "Unable to enable clock\n"); 764 + return err; 765 + } 766 + 767 + return 0; 768 + } 769 + 770 + static void ingenic_ipu_unbind(struct device *dev, 771 + struct device *master, void *d) 772 + { 773 + struct ingenic_ipu *ipu = dev_get_drvdata(dev); 774 + 775 + clk_disable_unprepare(ipu->clk); 776 + } 777 + 778 + static const struct component_ops ingenic_ipu_ops = { 779 + .bind = ingenic_ipu_bind, 780 + .unbind = ingenic_ipu_unbind, 781 + }; 782 + 783 + static int ingenic_ipu_probe(struct platform_device *pdev) 784 + { 785 + return component_add(&pdev->dev, &ingenic_ipu_ops); 786 + } 787 + 788 + static int ingenic_ipu_remove(struct platform_device *pdev) 789 + { 790 + component_del(&pdev->dev, &ingenic_ipu_ops); 791 + return 0; 792 + } 793 + 794 + static const u32 jz4725b_ipu_formats[] = { 795 + DRM_FORMAT_YUYV, 796 + DRM_FORMAT_YVYU, 797 + DRM_FORMAT_UYVY, 798 + DRM_FORMAT_VYUY, 799 + DRM_FORMAT_YUV411, 800 + DRM_FORMAT_YUV420, 801 + DRM_FORMAT_YUV422, 802 + DRM_FORMAT_YUV444, 803 + }; 804 + 805 + static const struct soc_info jz4725b_soc_info = { 806 + .formats = jz4725b_ipu_formats, 807 + .num_formats = ARRAY_SIZE(jz4725b_ipu_formats), 808 + .has_bicubic = false, 809 + .set_coefs = jz4725b_set_coefs, 810 + }; 811 + 812 + static const u32 jz4760_ipu_formats[] = { 813 + DRM_FORMAT_XRGB1555, 814 + DRM_FORMAT_XBGR1555, 815 + DRM_FORMAT_RGB565, 816 + DRM_FORMAT_BGR565, 817 + DRM_FORMAT_XRGB8888, 818 + DRM_FORMAT_XBGR8888, 819 + DRM_FORMAT_YUYV, 820 + DRM_FORMAT_YVYU, 821 + DRM_FORMAT_UYVY, 822 + DRM_FORMAT_VYUY, 823 + DRM_FORMAT_YUV411, 824 + DRM_FORMAT_YUV420, 825 + DRM_FORMAT_YUV422, 826 + DRM_FORMAT_YUV444, 827 + DRM_FORMAT_XYUV8888, 828 + }; 829 + 830 + static const struct soc_info jz4760_soc_info = { 831 + .formats = jz4760_ipu_formats, 832 + .num_formats = ARRAY_SIZE(jz4760_ipu_formats), 833 + .has_bicubic = true, 834 + .set_coefs = jz4760_set_coefs, 835 + }; 836 + 837 + static const struct of_device_id ingenic_ipu_of_match[] = { 838 + { .compatible = "ingenic,jz4725b-ipu", .data = &jz4725b_soc_info }, 839 + { .compatible = "ingenic,jz4760-ipu", .data = &jz4760_soc_info }, 840 + { /* sentinel */ }, 841 + }; 842 + MODULE_DEVICE_TABLE(of, ingenic_ipu_of_match); 843 + 844 + static struct platform_driver ingenic_ipu_driver = { 845 + .driver = { 846 + .name = "ingenic-ipu", 847 + .of_match_table = ingenic_ipu_of_match, 848 + }, 849 + .probe = ingenic_ipu_probe, 850 + .remove = ingenic_ipu_remove, 851 + }; 852 + 853 + struct platform_driver *ingenic_ipu_driver_ptr = &ingenic_ipu_driver;
+109
drivers/gpu/drm/ingenic/ingenic-ipu.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + // 3 + // Ingenic JZ47xx IPU - Register definitions and private API 4 + // 5 + // Copyright (C) 2020, Paul Cercueil <paul@crapouillou.net> 6 + 7 + #ifndef DRIVERS_GPU_DRM_INGENIC_INGENIC_IPU_H 8 + #define DRIVERS_GPU_DRM_INGENIC_INGENIC_IPU_H 9 + 10 + #include <linux/bitops.h> 11 + 12 + #define JZ_REG_IPU_CTRL 0x00 13 + #define JZ_REG_IPU_STATUS 0x04 14 + #define JZ_REG_IPU_D_FMT 0x08 15 + #define JZ_REG_IPU_Y_ADDR 0x0c 16 + #define JZ_REG_IPU_U_ADDR 0x10 17 + #define JZ_REG_IPU_V_ADDR 0x14 18 + #define JZ_REG_IPU_IN_GS 0x18 19 + #define JZ_REG_IPU_Y_STRIDE 0x1c 20 + #define JZ_REG_IPU_UV_STRIDE 0x20 21 + #define JZ_REG_IPU_OUT_ADDR 0x24 22 + #define JZ_REG_IPU_OUT_GS 0x28 23 + #define JZ_REG_IPU_OUT_STRIDE 0x2c 24 + #define JZ_REG_IPU_RSZ_COEF_INDEX 0x30 25 + #define JZ_REG_IPU_CSC_C0_COEF 0x34 26 + #define JZ_REG_IPU_CSC_C1_COEF 0x38 27 + #define JZ_REG_IPU_CSC_C2_COEF 0x3c 28 + #define JZ_REG_IPU_CSC_C3_COEF 0x40 29 + #define JZ_REG_IPU_CSC_C4_COEF 0x44 30 + #define JZ_REG_IPU_HRSZ_COEF_LUT 0x48 31 + #define JZ_REG_IPU_VRSZ_COEF_LUT 0x4c 32 + #define JZ_REG_IPU_CSC_OFFSET 0x50 33 + #define JZ_REG_IPU_Y_PHY_T_ADDR 0x54 34 + #define JZ_REG_IPU_U_PHY_T_ADDR 0x58 35 + #define JZ_REG_IPU_V_PHY_T_ADDR 0x5c 36 + #define JZ_REG_IPU_OUT_PHY_T_ADDR 0x60 37 + 38 + #define JZ_IPU_CTRL_ADDR_SEL BIT(20) 39 + #define JZ_IPU_CTRL_ZOOM_SEL BIT(18) 40 + #define JZ_IPU_CTRL_DFIX_SEL BIT(17) 41 + #define JZ_IPU_CTRL_LCDC_SEL BIT(11) 42 + #define JZ_IPU_CTRL_SPKG_SEL BIT(10) 43 + #define JZ_IPU_CTRL_VSCALE BIT(9) 44 + #define JZ_IPU_CTRL_HSCALE BIT(8) 45 + #define JZ_IPU_CTRL_STOP BIT(7) 46 + #define JZ_IPU_CTRL_RST BIT(6) 47 + #define JZ_IPU_CTRL_FM_IRQ_EN BIT(5) 48 + #define JZ_IPU_CTRL_CSC_EN BIT(4) 49 + #define JZ_IPU_CTRL_VRSZ_EN BIT(3) 50 + #define JZ_IPU_CTRL_HRSZ_EN BIT(2) 51 + #define JZ_IPU_CTRL_RUN BIT(1) 52 + #define JZ_IPU_CTRL_CHIP_EN BIT(0) 53 + 54 + #define JZ_IPU_STATUS_OUT_END BIT(0) 55 + 56 + #define JZ_IPU_IN_GS_H_LSB 0x0 57 + #define JZ_IPU_IN_GS_W_LSB 0x10 58 + #define JZ_IPU_OUT_GS_H_LSB 0x0 59 + #define JZ_IPU_OUT_GS_W_LSB 0x10 60 + 61 + #define JZ_IPU_Y_STRIDE_Y_LSB 0 62 + #define JZ_IPU_UV_STRIDE_U_LSB 16 63 + #define JZ_IPU_UV_STRIDE_V_LSB 0 64 + 65 + #define JZ_IPU_D_FMT_IN_FMT_LSB 0 66 + #define JZ_IPU_D_FMT_IN_FMT_RGB555 (0x0 << JZ_IPU_D_FMT_IN_FMT_LSB) 67 + #define JZ_IPU_D_FMT_IN_FMT_YUV420 (0x0 << JZ_IPU_D_FMT_IN_FMT_LSB) 68 + #define JZ_IPU_D_FMT_IN_FMT_YUV422 (0x1 << JZ_IPU_D_FMT_IN_FMT_LSB) 69 + #define JZ_IPU_D_FMT_IN_FMT_RGB888 (0x2 << JZ_IPU_D_FMT_IN_FMT_LSB) 70 + #define JZ_IPU_D_FMT_IN_FMT_YUV444 (0x2 << JZ_IPU_D_FMT_IN_FMT_LSB) 71 + #define JZ_IPU_D_FMT_IN_FMT_RGB565 (0x3 << JZ_IPU_D_FMT_IN_FMT_LSB) 72 + 73 + #define JZ_IPU_D_FMT_YUV_FMT_LSB 2 74 + #define JZ_IPU_D_FMT_YUV_Y1UY0V (0x0 << JZ_IPU_D_FMT_YUV_FMT_LSB) 75 + #define JZ_IPU_D_FMT_YUV_Y1VY0U (0x1 << JZ_IPU_D_FMT_YUV_FMT_LSB) 76 + #define JZ_IPU_D_FMT_YUV_UY1VY0 (0x2 << JZ_IPU_D_FMT_YUV_FMT_LSB) 77 + #define JZ_IPU_D_FMT_YUV_VY1UY0 (0x3 << JZ_IPU_D_FMT_YUV_FMT_LSB) 78 + #define JZ_IPU_D_FMT_IN_FMT_YUV411 (0x3 << JZ_IPU_D_FMT_IN_FMT_LSB) 79 + 80 + #define JZ_IPU_D_FMT_OUT_FMT_LSB 19 81 + #define JZ_IPU_D_FMT_OUT_FMT_RGB555 (0x0 << JZ_IPU_D_FMT_OUT_FMT_LSB) 82 + #define JZ_IPU_D_FMT_OUT_FMT_RGB565 (0x1 << JZ_IPU_D_FMT_OUT_FMT_LSB) 83 + #define JZ_IPU_D_FMT_OUT_FMT_RGB888 (0x2 << JZ_IPU_D_FMT_OUT_FMT_LSB) 84 + #define JZ_IPU_D_FMT_OUT_FMT_YUV422 (0x3 << JZ_IPU_D_FMT_OUT_FMT_LSB) 85 + #define JZ_IPU_D_FMT_OUT_FMT_RGBAAA (0x4 << JZ_IPU_D_FMT_OUT_FMT_LSB) 86 + 87 + #define JZ_IPU_D_FMT_RGB_OUT_OFT_LSB 22 88 + #define JZ_IPU_D_FMT_RGB_OUT_OFT_RGB (0x0 << JZ_IPU_D_FMT_RGB_OUT_OFT_LSB) 89 + #define JZ_IPU_D_FMT_RGB_OUT_OFT_RBG (0x1 << JZ_IPU_D_FMT_RGB_OUT_OFT_LSB) 90 + #define JZ_IPU_D_FMT_RGB_OUT_OFT_GBR (0x2 << JZ_IPU_D_FMT_RGB_OUT_OFT_LSB) 91 + #define JZ_IPU_D_FMT_RGB_OUT_OFT_GRB (0x3 << JZ_IPU_D_FMT_RGB_OUT_OFT_LSB) 92 + #define JZ_IPU_D_FMT_RGB_OUT_OFT_BRG (0x4 << JZ_IPU_D_FMT_RGB_OUT_OFT_LSB) 93 + #define JZ_IPU_D_FMT_RGB_OUT_OFT_BGR (0x5 << JZ_IPU_D_FMT_RGB_OUT_OFT_LSB) 94 + 95 + #define JZ4725B_IPU_RSZ_LUT_COEF_LSB 2 96 + #define JZ4725B_IPU_RSZ_LUT_COEF_MASK 0x7ff 97 + #define JZ4725B_IPU_RSZ_LUT_IN_EN BIT(1) 98 + #define JZ4725B_IPU_RSZ_LUT_OUT_EN BIT(0) 99 + 100 + #define JZ4760_IPU_RSZ_COEF20_LSB 6 101 + #define JZ4760_IPU_RSZ_COEF31_LSB 17 102 + #define JZ4760_IPU_RSZ_COEF_MASK 0x7ff 103 + #define JZ4760_IPU_RSZ_OFFSET_LSB 1 104 + #define JZ4760_IPU_RSZ_OFFSET_MASK 0x1f 105 + 106 + #define JZ_IPU_CSC_OFFSET_CHROMA_LSB 16 107 + #define JZ_IPU_CSC_OFFSET_LUMA_LSB 16 108 + 109 + #endif /* DRIVERS_GPU_DRM_INGENIC_INGENIC_IPU_H */