Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 Free Electrons |
| 3 | * Copyright (C) 2015 NextThing Co |
| 4 | * |
| 5 | * Maxime Ripard <maxime.ripard@free-electrons.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License as |
| 9 | * published by the Free Software Foundation; either version 2 of |
| 10 | * the License, or (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | #include <drm/drmP.h> |
| 14 | #include <drm/drm_atomic_helper.h> |
| 15 | #include <drm/drm_crtc.h> |
| 16 | #include <drm/drm_crtc_helper.h> |
| 17 | #include <drm/drm_fb_cma_helper.h> |
| 18 | #include <drm/drm_gem_cma_helper.h> |
| 19 | #include <drm/drm_plane_helper.h> |
| 20 | |
| 21 | #include <linux/component.h> |
Chen-Yu Tsai | 80a5824 | 2017-04-21 16:38:50 +0800 | [diff] [blame] | 22 | #include <linux/list.h> |
Chen-Yu Tsai | da3a1c3 | 2017-04-21 16:38:52 +0800 | [diff] [blame] | 23 | #include <linux/of_graph.h> |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 24 | #include <linux/reset.h> |
| 25 | |
| 26 | #include "sun4i_backend.h" |
| 27 | #include "sun4i_drv.h" |
Icenowy Zheng | 8796933 | 2017-05-17 22:47:17 +0800 | [diff] [blame] | 28 | #include "sun4i_layer.h" |
| 29 | #include "sunxi_engine.h" |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 30 | |
Chen-Yu Tsai | a6fbffb | 2017-02-23 16:05:33 +0800 | [diff] [blame] | 31 | static const u32 sunxi_rgb2yuv_coef[12] = { |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 32 | 0x00000107, 0x00000204, 0x00000064, 0x00000108, |
| 33 | 0x00003f69, 0x00003ed6, 0x000001c1, 0x00000808, |
| 34 | 0x000001c1, 0x00003e88, 0x00003fb8, 0x00000808 |
| 35 | }; |
| 36 | |
Icenowy Zheng | 8796933 | 2017-05-17 22:47:17 +0800 | [diff] [blame] | 37 | static void sun4i_backend_apply_color_correction(struct sunxi_engine *engine) |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 38 | { |
| 39 | int i; |
| 40 | |
| 41 | DRM_DEBUG_DRIVER("Applying RGB to YUV color correction\n"); |
| 42 | |
| 43 | /* Set color correction */ |
Icenowy Zheng | 8796933 | 2017-05-17 22:47:17 +0800 | [diff] [blame] | 44 | regmap_write(engine->regs, SUN4I_BACKEND_OCCTL_REG, |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 45 | SUN4I_BACKEND_OCCTL_ENABLE); |
| 46 | |
| 47 | for (i = 0; i < 12; i++) |
Icenowy Zheng | 8796933 | 2017-05-17 22:47:17 +0800 | [diff] [blame] | 48 | regmap_write(engine->regs, SUN4I_BACKEND_OCRCOEF_REG(i), |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 49 | sunxi_rgb2yuv_coef[i]); |
| 50 | } |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 51 | |
Icenowy Zheng | 8796933 | 2017-05-17 22:47:17 +0800 | [diff] [blame] | 52 | static void sun4i_backend_disable_color_correction(struct sunxi_engine *engine) |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 53 | { |
| 54 | DRM_DEBUG_DRIVER("Disabling color correction\n"); |
| 55 | |
| 56 | /* Disable color correction */ |
Icenowy Zheng | 8796933 | 2017-05-17 22:47:17 +0800 | [diff] [blame] | 57 | regmap_update_bits(engine->regs, SUN4I_BACKEND_OCCTL_REG, |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 58 | SUN4I_BACKEND_OCCTL_ENABLE, 0); |
| 59 | } |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 60 | |
Icenowy Zheng | 8796933 | 2017-05-17 22:47:17 +0800 | [diff] [blame] | 61 | static void sun4i_backend_commit(struct sunxi_engine *engine) |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 62 | { |
| 63 | DRM_DEBUG_DRIVER("Committing changes\n"); |
| 64 | |
Icenowy Zheng | 8796933 | 2017-05-17 22:47:17 +0800 | [diff] [blame] | 65 | regmap_write(engine->regs, SUN4I_BACKEND_REGBUFFCTL_REG, |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 66 | SUN4I_BACKEND_REGBUFFCTL_AUTOLOAD_DIS | |
| 67 | SUN4I_BACKEND_REGBUFFCTL_LOADCTL); |
| 68 | } |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 69 | |
| 70 | void sun4i_backend_layer_enable(struct sun4i_backend *backend, |
| 71 | int layer, bool enable) |
| 72 | { |
| 73 | u32 val; |
| 74 | |
Chen-Yu Tsai | cf80aee | 2017-04-25 23:25:05 +0800 | [diff] [blame] | 75 | DRM_DEBUG_DRIVER("%sabling layer %d\n", enable ? "En" : "Dis", |
| 76 | layer); |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 77 | |
| 78 | if (enable) |
| 79 | val = SUN4I_BACKEND_MODCTL_LAY_EN(layer); |
| 80 | else |
| 81 | val = 0; |
| 82 | |
Icenowy Zheng | 8796933 | 2017-05-17 22:47:17 +0800 | [diff] [blame] | 83 | regmap_update_bits(backend->engine.regs, SUN4I_BACKEND_MODCTL_REG, |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 84 | SUN4I_BACKEND_MODCTL_LAY_EN(layer), val); |
| 85 | } |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 86 | |
Maxime Ripard | c222f39 | 2016-09-19 22:17:50 +0200 | [diff] [blame] | 87 | static int sun4i_backend_drm_format_to_layer(struct drm_plane *plane, |
| 88 | u32 format, u32 *mode) |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 89 | { |
Maxime Ripard | c222f39 | 2016-09-19 22:17:50 +0200 | [diff] [blame] | 90 | if ((plane->type == DRM_PLANE_TYPE_PRIMARY) && |
| 91 | (format == DRM_FORMAT_ARGB8888)) |
| 92 | format = DRM_FORMAT_XRGB8888; |
| 93 | |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 94 | switch (format) { |
| 95 | case DRM_FORMAT_ARGB8888: |
| 96 | *mode = SUN4I_BACKEND_LAY_FBFMT_ARGB8888; |
| 97 | break; |
| 98 | |
Maxime Ripard | 47d7fbb | 2016-10-18 10:46:14 +0200 | [diff] [blame] | 99 | case DRM_FORMAT_ARGB4444: |
| 100 | *mode = SUN4I_BACKEND_LAY_FBFMT_ARGB4444; |
| 101 | break; |
| 102 | |
| 103 | case DRM_FORMAT_ARGB1555: |
| 104 | *mode = SUN4I_BACKEND_LAY_FBFMT_ARGB1555; |
| 105 | break; |
| 106 | |
| 107 | case DRM_FORMAT_RGBA5551: |
| 108 | *mode = SUN4I_BACKEND_LAY_FBFMT_RGBA5551; |
| 109 | break; |
| 110 | |
| 111 | case DRM_FORMAT_RGBA4444: |
| 112 | *mode = SUN4I_BACKEND_LAY_FBFMT_RGBA4444; |
| 113 | break; |
| 114 | |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 115 | case DRM_FORMAT_XRGB8888: |
| 116 | *mode = SUN4I_BACKEND_LAY_FBFMT_XRGB8888; |
| 117 | break; |
| 118 | |
| 119 | case DRM_FORMAT_RGB888: |
| 120 | *mode = SUN4I_BACKEND_LAY_FBFMT_RGB888; |
| 121 | break; |
| 122 | |
Maxime Ripard | 47d7fbb | 2016-10-18 10:46:14 +0200 | [diff] [blame] | 123 | case DRM_FORMAT_RGB565: |
| 124 | *mode = SUN4I_BACKEND_LAY_FBFMT_RGB565; |
| 125 | break; |
| 126 | |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 127 | default: |
| 128 | return -EINVAL; |
| 129 | } |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | int sun4i_backend_update_layer_coord(struct sun4i_backend *backend, |
| 135 | int layer, struct drm_plane *plane) |
| 136 | { |
| 137 | struct drm_plane_state *state = plane->state; |
| 138 | struct drm_framebuffer *fb = state->fb; |
| 139 | |
| 140 | DRM_DEBUG_DRIVER("Updating layer %d\n", layer); |
| 141 | |
| 142 | if (plane->type == DRM_PLANE_TYPE_PRIMARY) { |
| 143 | DRM_DEBUG_DRIVER("Primary layer, updating global size W: %u H: %u\n", |
| 144 | state->crtc_w, state->crtc_h); |
Icenowy Zheng | 8796933 | 2017-05-17 22:47:17 +0800 | [diff] [blame] | 145 | regmap_write(backend->engine.regs, SUN4I_BACKEND_DISSIZE_REG, |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 146 | SUN4I_BACKEND_DISSIZE(state->crtc_w, |
| 147 | state->crtc_h)); |
| 148 | } |
| 149 | |
| 150 | /* Set the line width */ |
| 151 | DRM_DEBUG_DRIVER("Layer line width: %d bits\n", fb->pitches[0] * 8); |
Icenowy Zheng | 8796933 | 2017-05-17 22:47:17 +0800 | [diff] [blame] | 152 | regmap_write(backend->engine.regs, |
| 153 | SUN4I_BACKEND_LAYLINEWIDTH_REG(layer), |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 154 | fb->pitches[0] * 8); |
| 155 | |
| 156 | /* Set height and width */ |
| 157 | DRM_DEBUG_DRIVER("Layer size W: %u H: %u\n", |
| 158 | state->crtc_w, state->crtc_h); |
Icenowy Zheng | 8796933 | 2017-05-17 22:47:17 +0800 | [diff] [blame] | 159 | regmap_write(backend->engine.regs, SUN4I_BACKEND_LAYSIZE_REG(layer), |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 160 | SUN4I_BACKEND_LAYSIZE(state->crtc_w, |
| 161 | state->crtc_h)); |
| 162 | |
| 163 | /* Set base coordinates */ |
| 164 | DRM_DEBUG_DRIVER("Layer coordinates X: %d Y: %d\n", |
| 165 | state->crtc_x, state->crtc_y); |
Icenowy Zheng | 8796933 | 2017-05-17 22:47:17 +0800 | [diff] [blame] | 166 | regmap_write(backend->engine.regs, SUN4I_BACKEND_LAYCOOR_REG(layer), |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 167 | SUN4I_BACKEND_LAYCOOR(state->crtc_x, |
| 168 | state->crtc_y)); |
| 169 | |
| 170 | return 0; |
| 171 | } |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 172 | |
| 173 | int sun4i_backend_update_layer_formats(struct sun4i_backend *backend, |
| 174 | int layer, struct drm_plane *plane) |
| 175 | { |
| 176 | struct drm_plane_state *state = plane->state; |
| 177 | struct drm_framebuffer *fb = state->fb; |
| 178 | bool interlaced = false; |
| 179 | u32 val; |
| 180 | int ret; |
| 181 | |
| 182 | if (plane->state->crtc) |
| 183 | interlaced = plane->state->crtc->state->adjusted_mode.flags |
| 184 | & DRM_MODE_FLAG_INTERLACE; |
| 185 | |
Icenowy Zheng | 8796933 | 2017-05-17 22:47:17 +0800 | [diff] [blame] | 186 | regmap_update_bits(backend->engine.regs, SUN4I_BACKEND_MODCTL_REG, |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 187 | SUN4I_BACKEND_MODCTL_ITLMOD_EN, |
| 188 | interlaced ? SUN4I_BACKEND_MODCTL_ITLMOD_EN : 0); |
| 189 | |
| 190 | DRM_DEBUG_DRIVER("Switching display backend interlaced mode %s\n", |
| 191 | interlaced ? "on" : "off"); |
| 192 | |
Ville Syrjälä | 438b74a | 2016-12-14 23:32:55 +0200 | [diff] [blame] | 193 | ret = sun4i_backend_drm_format_to_layer(plane, fb->format->format, |
| 194 | &val); |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 195 | if (ret) { |
| 196 | DRM_DEBUG_DRIVER("Invalid format\n"); |
Christophe JAILLET | 0f0861e | 2016-11-18 19:18:47 +0100 | [diff] [blame] | 197 | return ret; |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 198 | } |
| 199 | |
Icenowy Zheng | 8796933 | 2017-05-17 22:47:17 +0800 | [diff] [blame] | 200 | regmap_update_bits(backend->engine.regs, |
| 201 | SUN4I_BACKEND_ATTCTL_REG1(layer), |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 202 | SUN4I_BACKEND_ATTCTL_REG1_LAY_FBFMT, val); |
| 203 | |
| 204 | return 0; |
| 205 | } |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 206 | |
| 207 | int sun4i_backend_update_layer_buffer(struct sun4i_backend *backend, |
| 208 | int layer, struct drm_plane *plane) |
| 209 | { |
| 210 | struct drm_plane_state *state = plane->state; |
| 211 | struct drm_framebuffer *fb = state->fb; |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 212 | u32 lo_paddr, hi_paddr; |
| 213 | dma_addr_t paddr; |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 214 | |
Chen-Yu Tsai | cff2192 | 2017-10-14 12:02:48 +0800 | [diff] [blame] | 215 | /* Get the start of the displayed memory */ |
| 216 | paddr = drm_fb_cma_get_gem_addr(fb, state, 0); |
Arnd Bergmann | f1b78f0 | 2016-05-03 17:23:28 +0200 | [diff] [blame] | 217 | DRM_DEBUG_DRIVER("Setting buffer address to %pad\n", &paddr); |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 218 | |
Chen-Yu Tsai | 4690803 | 2017-10-17 12:23:47 +0800 | [diff] [blame^] | 219 | /* |
| 220 | * backend DMA accesses DRAM directly, bypassing the system |
| 221 | * bus. As such, the address range is different and the buffer |
| 222 | * address needs to be corrected. |
| 223 | */ |
| 224 | paddr -= PHYS_OFFSET; |
| 225 | |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 226 | /* Write the 32 lower bits of the address (in bits) */ |
| 227 | lo_paddr = paddr << 3; |
| 228 | DRM_DEBUG_DRIVER("Setting address lower bits to 0x%x\n", lo_paddr); |
Icenowy Zheng | 8796933 | 2017-05-17 22:47:17 +0800 | [diff] [blame] | 229 | regmap_write(backend->engine.regs, |
| 230 | SUN4I_BACKEND_LAYFB_L32ADD_REG(layer), |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 231 | lo_paddr); |
| 232 | |
| 233 | /* And the upper bits */ |
| 234 | hi_paddr = paddr >> 29; |
| 235 | DRM_DEBUG_DRIVER("Setting address high bits to 0x%x\n", hi_paddr); |
Icenowy Zheng | 8796933 | 2017-05-17 22:47:17 +0800 | [diff] [blame] | 236 | regmap_update_bits(backend->engine.regs, SUN4I_BACKEND_LAYFB_H4ADD_REG, |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 237 | SUN4I_BACKEND_LAYFB_H4ADD_MSK(layer), |
| 238 | SUN4I_BACKEND_LAYFB_H4ADD(layer, hi_paddr)); |
| 239 | |
| 240 | return 0; |
| 241 | } |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 242 | |
Maxime Ripard | 440d2c7 | 2016-09-06 15:23:03 +0200 | [diff] [blame] | 243 | static int sun4i_backend_init_sat(struct device *dev) { |
| 244 | struct sun4i_backend *backend = dev_get_drvdata(dev); |
| 245 | int ret; |
| 246 | |
| 247 | backend->sat_reset = devm_reset_control_get(dev, "sat"); |
| 248 | if (IS_ERR(backend->sat_reset)) { |
| 249 | dev_err(dev, "Couldn't get the SAT reset line\n"); |
| 250 | return PTR_ERR(backend->sat_reset); |
| 251 | } |
| 252 | |
| 253 | ret = reset_control_deassert(backend->sat_reset); |
| 254 | if (ret) { |
| 255 | dev_err(dev, "Couldn't deassert the SAT reset line\n"); |
| 256 | return ret; |
| 257 | } |
| 258 | |
| 259 | backend->sat_clk = devm_clk_get(dev, "sat"); |
| 260 | if (IS_ERR(backend->sat_clk)) { |
| 261 | dev_err(dev, "Couldn't get our SAT clock\n"); |
| 262 | ret = PTR_ERR(backend->sat_clk); |
| 263 | goto err_assert_reset; |
| 264 | } |
| 265 | |
| 266 | ret = clk_prepare_enable(backend->sat_clk); |
| 267 | if (ret) { |
| 268 | dev_err(dev, "Couldn't enable the SAT clock\n"); |
| 269 | return ret; |
| 270 | } |
| 271 | |
| 272 | return 0; |
| 273 | |
| 274 | err_assert_reset: |
| 275 | reset_control_assert(backend->sat_reset); |
| 276 | return ret; |
| 277 | } |
| 278 | |
| 279 | static int sun4i_backend_free_sat(struct device *dev) { |
| 280 | struct sun4i_backend *backend = dev_get_drvdata(dev); |
| 281 | |
| 282 | clk_disable_unprepare(backend->sat_clk); |
| 283 | reset_control_assert(backend->sat_reset); |
| 284 | |
| 285 | return 0; |
| 286 | } |
| 287 | |
Chen-Yu Tsai | da3a1c3 | 2017-04-21 16:38:52 +0800 | [diff] [blame] | 288 | /* |
| 289 | * The display backend can take video output from the display frontend, or |
| 290 | * the display enhancement unit on the A80, as input for one it its layers. |
| 291 | * This relationship within the display pipeline is encoded in the device |
| 292 | * tree with of_graph, and we use it here to figure out which backend, if |
| 293 | * there are 2 or more, we are currently probing. The number would be in |
| 294 | * the "reg" property of the upstream output port endpoint. |
| 295 | */ |
| 296 | static int sun4i_backend_of_get_id(struct device_node *node) |
| 297 | { |
| 298 | struct device_node *port, *ep; |
| 299 | int ret = -EINVAL; |
| 300 | |
| 301 | /* input is port 0 */ |
| 302 | port = of_graph_get_port_by_id(node, 0); |
| 303 | if (!port) |
| 304 | return -EINVAL; |
| 305 | |
| 306 | /* try finding an upstream endpoint */ |
| 307 | for_each_available_child_of_node(port, ep) { |
| 308 | struct device_node *remote; |
| 309 | u32 reg; |
| 310 | |
Kuninori Morimoto | 0bd46d7 | 2017-08-10 04:36:43 +0000 | [diff] [blame] | 311 | remote = of_graph_get_remote_endpoint(ep); |
Chen-Yu Tsai | da3a1c3 | 2017-04-21 16:38:52 +0800 | [diff] [blame] | 312 | if (!remote) |
| 313 | continue; |
| 314 | |
| 315 | ret = of_property_read_u32(remote, "reg", ®); |
| 316 | if (ret) |
| 317 | continue; |
| 318 | |
| 319 | ret = reg; |
| 320 | } |
| 321 | |
| 322 | of_node_put(port); |
| 323 | |
| 324 | return ret; |
| 325 | } |
| 326 | |
Icenowy Zheng | 8796933 | 2017-05-17 22:47:17 +0800 | [diff] [blame] | 327 | static const struct sunxi_engine_ops sun4i_backend_engine_ops = { |
| 328 | .commit = sun4i_backend_commit, |
| 329 | .layers_init = sun4i_layers_init, |
| 330 | .apply_color_correction = sun4i_backend_apply_color_correction, |
| 331 | .disable_color_correction = sun4i_backend_disable_color_correction, |
| 332 | }; |
| 333 | |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 334 | static struct regmap_config sun4i_backend_regmap_config = { |
| 335 | .reg_bits = 32, |
| 336 | .val_bits = 32, |
| 337 | .reg_stride = 4, |
| 338 | .max_register = 0x5800, |
| 339 | }; |
| 340 | |
| 341 | static int sun4i_backend_bind(struct device *dev, struct device *master, |
| 342 | void *data) |
| 343 | { |
| 344 | struct platform_device *pdev = to_platform_device(dev); |
| 345 | struct drm_device *drm = data; |
| 346 | struct sun4i_drv *drv = drm->dev_private; |
| 347 | struct sun4i_backend *backend; |
| 348 | struct resource *res; |
| 349 | void __iomem *regs; |
| 350 | int i, ret; |
| 351 | |
| 352 | backend = devm_kzalloc(dev, sizeof(*backend), GFP_KERNEL); |
| 353 | if (!backend) |
| 354 | return -ENOMEM; |
| 355 | dev_set_drvdata(dev, backend); |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 356 | |
Icenowy Zheng | 8796933 | 2017-05-17 22:47:17 +0800 | [diff] [blame] | 357 | backend->engine.node = dev->of_node; |
| 358 | backend->engine.ops = &sun4i_backend_engine_ops; |
| 359 | backend->engine.id = sun4i_backend_of_get_id(dev->of_node); |
| 360 | if (backend->engine.id < 0) |
| 361 | return backend->engine.id; |
Chen-Yu Tsai | da3a1c3 | 2017-04-21 16:38:52 +0800 | [diff] [blame] | 362 | |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 363 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 364 | regs = devm_ioremap_resource(dev, res); |
Wei Yongjun | 9a8aa93 | 2016-09-15 03:25:58 +0000 | [diff] [blame] | 365 | if (IS_ERR(regs)) |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 366 | return PTR_ERR(regs); |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 367 | |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 368 | backend->reset = devm_reset_control_get(dev, NULL); |
| 369 | if (IS_ERR(backend->reset)) { |
| 370 | dev_err(dev, "Couldn't get our reset line\n"); |
| 371 | return PTR_ERR(backend->reset); |
| 372 | } |
| 373 | |
| 374 | ret = reset_control_deassert(backend->reset); |
| 375 | if (ret) { |
| 376 | dev_err(dev, "Couldn't deassert our reset line\n"); |
| 377 | return ret; |
| 378 | } |
| 379 | |
| 380 | backend->bus_clk = devm_clk_get(dev, "ahb"); |
| 381 | if (IS_ERR(backend->bus_clk)) { |
| 382 | dev_err(dev, "Couldn't get the backend bus clock\n"); |
| 383 | ret = PTR_ERR(backend->bus_clk); |
| 384 | goto err_assert_reset; |
| 385 | } |
| 386 | clk_prepare_enable(backend->bus_clk); |
| 387 | |
| 388 | backend->mod_clk = devm_clk_get(dev, "mod"); |
| 389 | if (IS_ERR(backend->mod_clk)) { |
| 390 | dev_err(dev, "Couldn't get the backend module clock\n"); |
| 391 | ret = PTR_ERR(backend->mod_clk); |
| 392 | goto err_disable_bus_clk; |
| 393 | } |
| 394 | clk_prepare_enable(backend->mod_clk); |
| 395 | |
| 396 | backend->ram_clk = devm_clk_get(dev, "ram"); |
| 397 | if (IS_ERR(backend->ram_clk)) { |
| 398 | dev_err(dev, "Couldn't get the backend RAM clock\n"); |
| 399 | ret = PTR_ERR(backend->ram_clk); |
| 400 | goto err_disable_mod_clk; |
| 401 | } |
| 402 | clk_prepare_enable(backend->ram_clk); |
| 403 | |
Maxime Ripard | 440d2c7 | 2016-09-06 15:23:03 +0200 | [diff] [blame] | 404 | if (of_device_is_compatible(dev->of_node, |
| 405 | "allwinner,sun8i-a33-display-backend")) { |
| 406 | ret = sun4i_backend_init_sat(dev); |
| 407 | if (ret) { |
| 408 | dev_err(dev, "Couldn't init SAT resources\n"); |
| 409 | goto err_disable_ram_clk; |
| 410 | } |
| 411 | } |
| 412 | |
Chen-Yu Tsai | 8270249 | 2017-10-14 12:02:47 +0800 | [diff] [blame] | 413 | backend->engine.regs = devm_regmap_init_mmio(dev, regs, |
| 414 | &sun4i_backend_regmap_config); |
| 415 | if (IS_ERR(backend->engine.regs)) { |
| 416 | dev_err(dev, "Couldn't create the backend regmap\n"); |
| 417 | return PTR_ERR(backend->engine.regs); |
| 418 | } |
| 419 | |
Icenowy Zheng | 8796933 | 2017-05-17 22:47:17 +0800 | [diff] [blame] | 420 | list_add_tail(&backend->engine.list, &drv->engine_list); |
Chen-Yu Tsai | 80a5824 | 2017-04-21 16:38:50 +0800 | [diff] [blame] | 421 | |
Chen-Yu Tsai | 936598d | 2017-10-14 12:02:49 +0800 | [diff] [blame] | 422 | /* |
| 423 | * Many of the backend's layer configuration registers have |
| 424 | * undefined default values. This poses a risk as we use |
| 425 | * regmap_update_bits in some places, and don't overwrite |
| 426 | * the whole register. |
| 427 | * |
| 428 | * Clear the registers here to have something predictable. |
| 429 | */ |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 430 | for (i = 0x800; i < 0x1000; i += 4) |
Icenowy Zheng | 8796933 | 2017-05-17 22:47:17 +0800 | [diff] [blame] | 431 | regmap_write(backend->engine.regs, i, 0); |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 432 | |
| 433 | /* Disable registers autoloading */ |
Icenowy Zheng | 8796933 | 2017-05-17 22:47:17 +0800 | [diff] [blame] | 434 | regmap_write(backend->engine.regs, SUN4I_BACKEND_REGBUFFCTL_REG, |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 435 | SUN4I_BACKEND_REGBUFFCTL_AUTOLOAD_DIS); |
| 436 | |
| 437 | /* Enable the backend */ |
Icenowy Zheng | 8796933 | 2017-05-17 22:47:17 +0800 | [diff] [blame] | 438 | regmap_write(backend->engine.regs, SUN4I_BACKEND_MODCTL_REG, |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 439 | SUN4I_BACKEND_MODCTL_DEBE_EN | |
| 440 | SUN4I_BACKEND_MODCTL_START_CTL); |
| 441 | |
| 442 | return 0; |
| 443 | |
Maxime Ripard | 440d2c7 | 2016-09-06 15:23:03 +0200 | [diff] [blame] | 444 | err_disable_ram_clk: |
| 445 | clk_disable_unprepare(backend->ram_clk); |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 446 | err_disable_mod_clk: |
| 447 | clk_disable_unprepare(backend->mod_clk); |
| 448 | err_disable_bus_clk: |
| 449 | clk_disable_unprepare(backend->bus_clk); |
| 450 | err_assert_reset: |
| 451 | reset_control_assert(backend->reset); |
| 452 | return ret; |
| 453 | } |
| 454 | |
| 455 | static void sun4i_backend_unbind(struct device *dev, struct device *master, |
| 456 | void *data) |
| 457 | { |
| 458 | struct sun4i_backend *backend = dev_get_drvdata(dev); |
| 459 | |
Icenowy Zheng | 8796933 | 2017-05-17 22:47:17 +0800 | [diff] [blame] | 460 | list_del(&backend->engine.list); |
Chen-Yu Tsai | 80a5824 | 2017-04-21 16:38:50 +0800 | [diff] [blame] | 461 | |
Maxime Ripard | 440d2c7 | 2016-09-06 15:23:03 +0200 | [diff] [blame] | 462 | if (of_device_is_compatible(dev->of_node, |
| 463 | "allwinner,sun8i-a33-display-backend")) |
| 464 | sun4i_backend_free_sat(dev); |
| 465 | |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 466 | clk_disable_unprepare(backend->ram_clk); |
| 467 | clk_disable_unprepare(backend->mod_clk); |
| 468 | clk_disable_unprepare(backend->bus_clk); |
| 469 | reset_control_assert(backend->reset); |
| 470 | } |
| 471 | |
Julia Lawall | dfeb693 | 2016-11-12 18:19:58 +0100 | [diff] [blame] | 472 | static const struct component_ops sun4i_backend_ops = { |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 473 | .bind = sun4i_backend_bind, |
| 474 | .unbind = sun4i_backend_unbind, |
| 475 | }; |
| 476 | |
| 477 | static int sun4i_backend_probe(struct platform_device *pdev) |
| 478 | { |
| 479 | return component_add(&pdev->dev, &sun4i_backend_ops); |
| 480 | } |
| 481 | |
| 482 | static int sun4i_backend_remove(struct platform_device *pdev) |
| 483 | { |
| 484 | component_del(&pdev->dev, &sun4i_backend_ops); |
| 485 | |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | static const struct of_device_id sun4i_backend_of_table[] = { |
| 490 | { .compatible = "allwinner,sun5i-a13-display-backend" }, |
Chen-Yu Tsai | 49c440e | 2016-10-20 11:43:41 +0800 | [diff] [blame] | 491 | { .compatible = "allwinner,sun6i-a31-display-backend" }, |
Maxime Ripard | 4a408f1 | 2016-01-07 12:32:25 +0100 | [diff] [blame] | 492 | { .compatible = "allwinner,sun8i-a33-display-backend" }, |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 493 | { } |
| 494 | }; |
| 495 | MODULE_DEVICE_TABLE(of, sun4i_backend_of_table); |
| 496 | |
| 497 | static struct platform_driver sun4i_backend_platform_driver = { |
| 498 | .probe = sun4i_backend_probe, |
| 499 | .remove = sun4i_backend_remove, |
| 500 | .driver = { |
| 501 | .name = "sun4i-backend", |
| 502 | .of_match_table = sun4i_backend_of_table, |
| 503 | }, |
| 504 | }; |
| 505 | module_platform_driver(sun4i_backend_platform_driver); |
| 506 | |
| 507 | MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>"); |
| 508 | MODULE_DESCRIPTION("Allwinner A10 Display Backend Driver"); |
| 509 | MODULE_LICENSE("GPL"); |