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.h>
15 #include <drm/drm_atomic_helper.h>
16 #include <drm/drm_crtc.h>
17 #include <drm/drm_crtc_helper.h>
18 #include <drm/drm_fb_cma_helper.h>
19 #include <drm/drm_gem_cma_helper.h>
20 #include <drm/drm_plane_helper.h>
21 
22 #include <linux/component.h>
23 #include <linux/list.h>
24 #include <linux/of_device.h>
25 #include <linux/of_graph.h>
26 #include <linux/reset.h>
27 
28 #include "sun4i_backend.h"
29 #include "sun4i_drv.h"
30 #include "sun4i_frontend.h"
31 #include "sun4i_layer.h"
32 #include "sunxi_engine.h"
33 
34 struct sun4i_backend_quirks {
35 	/* backend <-> TCON muxing selection done in backend */
36 	bool needs_output_muxing;
37 };
38 
39 static const u32 sunxi_rgb2yuv_coef[12] = {
40 	0x00000107, 0x00000204, 0x00000064, 0x00000108,
41 	0x00003f69, 0x00003ed6, 0x000001c1, 0x00000808,
42 	0x000001c1, 0x00003e88, 0x00003fb8, 0x00000808
43 };
44 
45 static void sun4i_backend_apply_color_correction(struct sunxi_engine *engine)
46 {
47 	int i;
48 
49 	DRM_DEBUG_DRIVER("Applying RGB to YUV color correction\n");
50 
51 	/* Set color correction */
52 	regmap_write(engine->regs, SUN4I_BACKEND_OCCTL_REG,
53 		     SUN4I_BACKEND_OCCTL_ENABLE);
54 
55 	for (i = 0; i < 12; i++)
56 		regmap_write(engine->regs, SUN4I_BACKEND_OCRCOEF_REG(i),
57 			     sunxi_rgb2yuv_coef[i]);
58 }
59 
60 static void sun4i_backend_disable_color_correction(struct sunxi_engine *engine)
61 {
62 	DRM_DEBUG_DRIVER("Disabling color correction\n");
63 
64 	/* Disable color correction */
65 	regmap_update_bits(engine->regs, SUN4I_BACKEND_OCCTL_REG,
66 			   SUN4I_BACKEND_OCCTL_ENABLE, 0);
67 }
68 
69 static void sun4i_backend_commit(struct sunxi_engine *engine)
70 {
71 	DRM_DEBUG_DRIVER("Committing changes\n");
72 
73 	regmap_write(engine->regs, SUN4I_BACKEND_REGBUFFCTL_REG,
74 		     SUN4I_BACKEND_REGBUFFCTL_AUTOLOAD_DIS |
75 		     SUN4I_BACKEND_REGBUFFCTL_LOADCTL);
76 }
77 
78 void sun4i_backend_layer_enable(struct sun4i_backend *backend,
79 				int layer, bool enable)
80 {
81 	u32 val;
82 
83 	DRM_DEBUG_DRIVER("%sabling layer %d\n", enable ? "En" : "Dis",
84 			 layer);
85 
86 	if (enable)
87 		val = SUN4I_BACKEND_MODCTL_LAY_EN(layer);
88 	else
89 		val = 0;
90 
91 	regmap_update_bits(backend->engine.regs, SUN4I_BACKEND_MODCTL_REG,
92 			   SUN4I_BACKEND_MODCTL_LAY_EN(layer), val);
93 }
94 
95 static int sun4i_backend_drm_format_to_layer(u32 format, u32 *mode)
96 {
97 	switch (format) {
98 	case DRM_FORMAT_ARGB8888:
99 		*mode = SUN4I_BACKEND_LAY_FBFMT_ARGB8888;
100 		break;
101 
102 	case DRM_FORMAT_ARGB4444:
103 		*mode = SUN4I_BACKEND_LAY_FBFMT_ARGB4444;
104 		break;
105 
106 	case DRM_FORMAT_ARGB1555:
107 		*mode = SUN4I_BACKEND_LAY_FBFMT_ARGB1555;
108 		break;
109 
110 	case DRM_FORMAT_RGBA5551:
111 		*mode = SUN4I_BACKEND_LAY_FBFMT_RGBA5551;
112 		break;
113 
114 	case DRM_FORMAT_RGBA4444:
115 		*mode = SUN4I_BACKEND_LAY_FBFMT_RGBA4444;
116 		break;
117 
118 	case DRM_FORMAT_XRGB8888:
119 		*mode = SUN4I_BACKEND_LAY_FBFMT_XRGB8888;
120 		break;
121 
122 	case DRM_FORMAT_RGB888:
123 		*mode = SUN4I_BACKEND_LAY_FBFMT_RGB888;
124 		break;
125 
126 	case DRM_FORMAT_RGB565:
127 		*mode = SUN4I_BACKEND_LAY_FBFMT_RGB565;
128 		break;
129 
130 	default:
131 		return -EINVAL;
132 	}
133 
134 	return 0;
135 }
136 
137 int sun4i_backend_update_layer_coord(struct sun4i_backend *backend,
138 				     int layer, struct drm_plane *plane)
139 {
140 	struct drm_plane_state *state = plane->state;
141 
142 	DRM_DEBUG_DRIVER("Updating layer %d\n", layer);
143 
144 	if (plane->type == DRM_PLANE_TYPE_PRIMARY) {
145 		DRM_DEBUG_DRIVER("Primary layer, updating global size W: %u H: %u\n",
146 				 state->crtc_w, state->crtc_h);
147 		regmap_write(backend->engine.regs, SUN4I_BACKEND_DISSIZE_REG,
148 			     SUN4I_BACKEND_DISSIZE(state->crtc_w,
149 						   state->crtc_h));
150 	}
151 
152 	/* Set height and width */
153 	DRM_DEBUG_DRIVER("Layer size W: %u H: %u\n",
154 			 state->crtc_w, state->crtc_h);
155 	regmap_write(backend->engine.regs, SUN4I_BACKEND_LAYSIZE_REG(layer),
156 		     SUN4I_BACKEND_LAYSIZE(state->crtc_w,
157 					   state->crtc_h));
158 
159 	/* Set base coordinates */
160 	DRM_DEBUG_DRIVER("Layer coordinates X: %d Y: %d\n",
161 			 state->crtc_x, state->crtc_y);
162 	regmap_write(backend->engine.regs, SUN4I_BACKEND_LAYCOOR_REG(layer),
163 		     SUN4I_BACKEND_LAYCOOR(state->crtc_x,
164 					   state->crtc_y));
165 
166 	return 0;
167 }
168 
169 int sun4i_backend_update_layer_formats(struct sun4i_backend *backend,
170 				       int layer, struct drm_plane *plane)
171 {
172 	struct drm_plane_state *state = plane->state;
173 	struct drm_framebuffer *fb = state->fb;
174 	bool interlaced = false;
175 	u32 val;
176 	int ret;
177 
178 	if (plane->state->crtc)
179 		interlaced = plane->state->crtc->state->adjusted_mode.flags
180 			& DRM_MODE_FLAG_INTERLACE;
181 
182 	regmap_update_bits(backend->engine.regs, SUN4I_BACKEND_MODCTL_REG,
183 			   SUN4I_BACKEND_MODCTL_ITLMOD_EN,
184 			   interlaced ? SUN4I_BACKEND_MODCTL_ITLMOD_EN : 0);
185 
186 	DRM_DEBUG_DRIVER("Switching display backend interlaced mode %s\n",
187 			 interlaced ? "on" : "off");
188 
189 	ret = sun4i_backend_drm_format_to_layer(fb->format->format, &val);
190 	if (ret) {
191 		DRM_DEBUG_DRIVER("Invalid format\n");
192 		return ret;
193 	}
194 
195 	regmap_update_bits(backend->engine.regs,
196 			   SUN4I_BACKEND_ATTCTL_REG1(layer),
197 			   SUN4I_BACKEND_ATTCTL_REG1_LAY_FBFMT, val);
198 
199 	return 0;
200 }
201 
202 int sun4i_backend_update_layer_frontend(struct sun4i_backend *backend,
203 					int layer, uint32_t fmt)
204 {
205 	u32 val;
206 	int ret;
207 
208 	ret = sun4i_backend_drm_format_to_layer(fmt, &val);
209 	if (ret) {
210 		DRM_DEBUG_DRIVER("Invalid format\n");
211 		return ret;
212 	}
213 
214 	regmap_update_bits(backend->engine.regs,
215 			   SUN4I_BACKEND_ATTCTL_REG0(layer),
216 			   SUN4I_BACKEND_ATTCTL_REG0_LAY_VDOEN,
217 			   SUN4I_BACKEND_ATTCTL_REG0_LAY_VDOEN);
218 
219 	regmap_update_bits(backend->engine.regs,
220 			   SUN4I_BACKEND_ATTCTL_REG1(layer),
221 			   SUN4I_BACKEND_ATTCTL_REG1_LAY_FBFMT, val);
222 
223 	return 0;
224 }
225 
226 int sun4i_backend_update_layer_buffer(struct sun4i_backend *backend,
227 				      int layer, struct drm_plane *plane)
228 {
229 	struct drm_plane_state *state = plane->state;
230 	struct drm_framebuffer *fb = state->fb;
231 	u32 lo_paddr, hi_paddr;
232 	dma_addr_t paddr;
233 
234 	/* Set the line width */
235 	DRM_DEBUG_DRIVER("Layer line width: %d bits\n", fb->pitches[0] * 8);
236 	regmap_write(backend->engine.regs,
237 		     SUN4I_BACKEND_LAYLINEWIDTH_REG(layer),
238 		     fb->pitches[0] * 8);
239 
240 	/* Get the start of the displayed memory */
241 	paddr = drm_fb_cma_get_gem_addr(fb, state, 0);
242 	DRM_DEBUG_DRIVER("Setting buffer address to %pad\n", &paddr);
243 
244 	/*
245 	 * backend DMA accesses DRAM directly, bypassing the system
246 	 * bus. As such, the address range is different and the buffer
247 	 * address needs to be corrected.
248 	 */
249 	paddr -= PHYS_OFFSET;
250 
251 	/* Write the 32 lower bits of the address (in bits) */
252 	lo_paddr = paddr << 3;
253 	DRM_DEBUG_DRIVER("Setting address lower bits to 0x%x\n", lo_paddr);
254 	regmap_write(backend->engine.regs,
255 		     SUN4I_BACKEND_LAYFB_L32ADD_REG(layer),
256 		     lo_paddr);
257 
258 	/* And the upper bits */
259 	hi_paddr = paddr >> 29;
260 	DRM_DEBUG_DRIVER("Setting address high bits to 0x%x\n", hi_paddr);
261 	regmap_update_bits(backend->engine.regs, SUN4I_BACKEND_LAYFB_H4ADD_REG,
262 			   SUN4I_BACKEND_LAYFB_H4ADD_MSK(layer),
263 			   SUN4I_BACKEND_LAYFB_H4ADD(layer, hi_paddr));
264 
265 	return 0;
266 }
267 
268 int sun4i_backend_update_layer_zpos(struct sun4i_backend *backend, int layer,
269 				    struct drm_plane *plane)
270 {
271 	struct drm_plane_state *state = plane->state;
272 	struct sun4i_layer_state *p_state = state_to_sun4i_layer_state(state);
273 	unsigned int priority = state->normalized_zpos;
274 	unsigned int pipe = p_state->pipe;
275 
276 	DRM_DEBUG_DRIVER("Setting layer %d's priority to %d and pipe %d\n",
277 			 layer, priority, pipe);
278 	regmap_update_bits(backend->engine.regs, SUN4I_BACKEND_ATTCTL_REG0(layer),
279 			   SUN4I_BACKEND_ATTCTL_REG0_LAY_PIPESEL_MASK |
280 			   SUN4I_BACKEND_ATTCTL_REG0_LAY_PRISEL_MASK,
281 			   SUN4I_BACKEND_ATTCTL_REG0_LAY_PIPESEL(p_state->pipe) |
282 			   SUN4I_BACKEND_ATTCTL_REG0_LAY_PRISEL(priority));
283 
284 	return 0;
285 }
286 
287 static bool sun4i_backend_plane_uses_scaler(struct drm_plane_state *state)
288 {
289 	u16 src_h = state->src_h >> 16;
290 	u16 src_w = state->src_w >> 16;
291 
292 	DRM_DEBUG_DRIVER("Input size %dx%d, output size %dx%d\n",
293 			 src_w, src_h, state->crtc_w, state->crtc_h);
294 
295 	if ((state->crtc_h != src_h) || (state->crtc_w != src_w))
296 		return true;
297 
298 	return false;
299 }
300 
301 static bool sun4i_backend_plane_uses_frontend(struct drm_plane_state *state)
302 {
303 	struct sun4i_layer *layer = plane_to_sun4i_layer(state->plane);
304 	struct sun4i_backend *backend = layer->backend;
305 
306 	if (IS_ERR(backend->frontend))
307 		return false;
308 
309 	return sun4i_backend_plane_uses_scaler(state);
310 }
311 
312 static void sun4i_backend_atomic_begin(struct sunxi_engine *engine,
313 				       struct drm_crtc_state *old_state)
314 {
315 	u32 val;
316 
317 	WARN_ON(regmap_read_poll_timeout(engine->regs,
318 					 SUN4I_BACKEND_REGBUFFCTL_REG,
319 					 val, !(val & SUN4I_BACKEND_REGBUFFCTL_LOADCTL),
320 					 100, 50000));
321 }
322 
323 static int sun4i_backend_atomic_check(struct sunxi_engine *engine,
324 				      struct drm_crtc_state *crtc_state)
325 {
326 	struct drm_plane_state *plane_states[SUN4I_BACKEND_NUM_LAYERS] = { 0 };
327 	struct drm_atomic_state *state = crtc_state->state;
328 	struct drm_device *drm = state->dev;
329 	struct drm_plane *plane;
330 	unsigned int num_planes = 0;
331 	unsigned int num_alpha_planes = 0;
332 	unsigned int num_frontend_planes = 0;
333 	unsigned int current_pipe = 0;
334 	unsigned int i;
335 
336 	DRM_DEBUG_DRIVER("Starting checking our planes\n");
337 
338 	if (!crtc_state->planes_changed)
339 		return 0;
340 
341 	drm_for_each_plane_mask(plane, drm, crtc_state->plane_mask) {
342 		struct drm_plane_state *plane_state =
343 			drm_atomic_get_plane_state(state, plane);
344 		struct sun4i_layer_state *layer_state =
345 			state_to_sun4i_layer_state(plane_state);
346 		struct drm_framebuffer *fb = plane_state->fb;
347 		struct drm_format_name_buf format_name;
348 
349 		if (sun4i_backend_plane_uses_frontend(plane_state)) {
350 			DRM_DEBUG_DRIVER("Using the frontend for plane %d\n",
351 					 plane->index);
352 
353 			layer_state->uses_frontend = true;
354 			num_frontend_planes++;
355 		} else {
356 			layer_state->uses_frontend = false;
357 		}
358 
359 		DRM_DEBUG_DRIVER("Plane FB format is %s\n",
360 				 drm_get_format_name(fb->format->format,
361 						     &format_name));
362 		if (fb->format->has_alpha)
363 			num_alpha_planes++;
364 
365 		DRM_DEBUG_DRIVER("Plane zpos is %d\n",
366 				 plane_state->normalized_zpos);
367 
368 		/* Sort our planes by Zpos */
369 		plane_states[plane_state->normalized_zpos] = plane_state;
370 
371 		num_planes++;
372 	}
373 
374 	/* All our planes were disabled, bail out */
375 	if (!num_planes)
376 		return 0;
377 
378 	/*
379 	 * The hardware is a bit unusual here.
380 	 *
381 	 * Even though it supports 4 layers, it does the composition
382 	 * in two separate steps.
383 	 *
384 	 * The first one is assigning a layer to one of its two
385 	 * pipes. If more that 1 layer is assigned to the same pipe,
386 	 * and if pixels overlaps, the pipe will take the pixel from
387 	 * the layer with the highest priority.
388 	 *
389 	 * The second step is the actual alpha blending, that takes
390 	 * the two pipes as input, and uses the eventual alpha
391 	 * component to do the transparency between the two.
392 	 *
393 	 * This two steps scenario makes us unable to guarantee a
394 	 * robust alpha blending between the 4 layers in all
395 	 * situations, since this means that we need to have one layer
396 	 * with alpha at the lowest position of our two pipes.
397 	 *
398 	 * However, we cannot even do that, since the hardware has a
399 	 * bug where the lowest plane of the lowest pipe (pipe 0,
400 	 * priority 0), if it has any alpha, will discard the pixel
401 	 * entirely and just display the pixels in the background
402 	 * color (black by default).
403 	 *
404 	 * This means that we effectively have only three valid
405 	 * configurations with alpha, all of them with the alpha being
406 	 * on pipe1 with the lowest position, which can be 1, 2 or 3
407 	 * depending on the number of planes and their zpos.
408 	 */
409 	if (num_alpha_planes > SUN4I_BACKEND_NUM_ALPHA_LAYERS) {
410 		DRM_DEBUG_DRIVER("Too many planes with alpha, rejecting...\n");
411 		return -EINVAL;
412 	}
413 
414 	/* We can't have an alpha plane at the lowest position */
415 	if (plane_states[0]->fb->format->has_alpha)
416 		return -EINVAL;
417 
418 	for (i = 1; i < num_planes; i++) {
419 		struct drm_plane_state *p_state = plane_states[i];
420 		struct drm_framebuffer *fb = p_state->fb;
421 		struct sun4i_layer_state *s_state = state_to_sun4i_layer_state(p_state);
422 
423 		/*
424 		 * The only alpha position is the lowest plane of the
425 		 * second pipe.
426 		 */
427 		if (fb->format->has_alpha)
428 			current_pipe++;
429 
430 		s_state->pipe = current_pipe;
431 	}
432 
433 	if (num_frontend_planes > SUN4I_BACKEND_NUM_FRONTEND_LAYERS) {
434 		DRM_DEBUG_DRIVER("Too many planes going through the frontend, rejecting\n");
435 		return -EINVAL;
436 	}
437 
438 	DRM_DEBUG_DRIVER("State valid with %u planes, %u alpha, %u video\n",
439 			 num_planes, num_alpha_planes, num_frontend_planes);
440 
441 	return 0;
442 }
443 
444 static void sun4i_backend_vblank_quirk(struct sunxi_engine *engine)
445 {
446 	struct sun4i_backend *backend = engine_to_sun4i_backend(engine);
447 	struct sun4i_frontend *frontend = backend->frontend;
448 
449 	if (!frontend)
450 		return;
451 
452 	/*
453 	 * In a teardown scenario with the frontend involved, we have
454 	 * to keep the frontend enabled until the next vblank, and
455 	 * only then disable it.
456 	 *
457 	 * This is due to the fact that the backend will not take into
458 	 * account the new configuration (with the plane that used to
459 	 * be fed by the frontend now disabled) until we write to the
460 	 * commit bit and the hardware fetches the new configuration
461 	 * during the next vblank.
462 	 *
463 	 * So we keep the frontend around in order to prevent any
464 	 * visual artifacts.
465 	 */
466 	spin_lock(&backend->frontend_lock);
467 	if (backend->frontend_teardown) {
468 		sun4i_frontend_exit(frontend);
469 		backend->frontend_teardown = false;
470 	}
471 	spin_unlock(&backend->frontend_lock);
472 };
473 
474 static int sun4i_backend_init_sat(struct device *dev) {
475 	struct sun4i_backend *backend = dev_get_drvdata(dev);
476 	int ret;
477 
478 	backend->sat_reset = devm_reset_control_get(dev, "sat");
479 	if (IS_ERR(backend->sat_reset)) {
480 		dev_err(dev, "Couldn't get the SAT reset line\n");
481 		return PTR_ERR(backend->sat_reset);
482 	}
483 
484 	ret = reset_control_deassert(backend->sat_reset);
485 	if (ret) {
486 		dev_err(dev, "Couldn't deassert the SAT reset line\n");
487 		return ret;
488 	}
489 
490 	backend->sat_clk = devm_clk_get(dev, "sat");
491 	if (IS_ERR(backend->sat_clk)) {
492 		dev_err(dev, "Couldn't get our SAT clock\n");
493 		ret = PTR_ERR(backend->sat_clk);
494 		goto err_assert_reset;
495 	}
496 
497 	ret = clk_prepare_enable(backend->sat_clk);
498 	if (ret) {
499 		dev_err(dev, "Couldn't enable the SAT clock\n");
500 		return ret;
501 	}
502 
503 	return 0;
504 
505 err_assert_reset:
506 	reset_control_assert(backend->sat_reset);
507 	return ret;
508 }
509 
510 static int sun4i_backend_free_sat(struct device *dev) {
511 	struct sun4i_backend *backend = dev_get_drvdata(dev);
512 
513 	clk_disable_unprepare(backend->sat_clk);
514 	reset_control_assert(backend->sat_reset);
515 
516 	return 0;
517 }
518 
519 /*
520  * The display backend can take video output from the display frontend, or
521  * the display enhancement unit on the A80, as input for one it its layers.
522  * This relationship within the display pipeline is encoded in the device
523  * tree with of_graph, and we use it here to figure out which backend, if
524  * there are 2 or more, we are currently probing. The number would be in
525  * the "reg" property of the upstream output port endpoint.
526  */
527 static int sun4i_backend_of_get_id(struct device_node *node)
528 {
529 	struct device_node *port, *ep;
530 	int ret = -EINVAL;
531 
532 	/* input is port 0 */
533 	port = of_graph_get_port_by_id(node, 0);
534 	if (!port)
535 		return -EINVAL;
536 
537 	/* try finding an upstream endpoint */
538 	for_each_available_child_of_node(port, ep) {
539 		struct device_node *remote;
540 		u32 reg;
541 
542 		remote = of_graph_get_remote_endpoint(ep);
543 		if (!remote)
544 			continue;
545 
546 		ret = of_property_read_u32(remote, "reg", &reg);
547 		if (ret)
548 			continue;
549 
550 		ret = reg;
551 	}
552 
553 	of_node_put(port);
554 
555 	return ret;
556 }
557 
558 /* TODO: This needs to take multiple pipelines into account */
559 static struct sun4i_frontend *sun4i_backend_find_frontend(struct sun4i_drv *drv,
560 							  struct device_node *node)
561 {
562 	struct device_node *port, *ep, *remote;
563 	struct sun4i_frontend *frontend;
564 
565 	port = of_graph_get_port_by_id(node, 0);
566 	if (!port)
567 		return ERR_PTR(-EINVAL);
568 
569 	for_each_available_child_of_node(port, ep) {
570 		remote = of_graph_get_remote_port_parent(ep);
571 		if (!remote)
572 			continue;
573 
574 		/* does this node match any registered engines? */
575 		list_for_each_entry(frontend, &drv->frontend_list, list) {
576 			if (remote == frontend->node) {
577 				of_node_put(remote);
578 				of_node_put(port);
579 				return frontend;
580 			}
581 		}
582 	}
583 
584 	return ERR_PTR(-EINVAL);
585 }
586 
587 static const struct sunxi_engine_ops sun4i_backend_engine_ops = {
588 	.atomic_begin			= sun4i_backend_atomic_begin,
589 	.atomic_check			= sun4i_backend_atomic_check,
590 	.commit				= sun4i_backend_commit,
591 	.layers_init			= sun4i_layers_init,
592 	.apply_color_correction		= sun4i_backend_apply_color_correction,
593 	.disable_color_correction	= sun4i_backend_disable_color_correction,
594 	.vblank_quirk			= sun4i_backend_vblank_quirk,
595 };
596 
597 static struct regmap_config sun4i_backend_regmap_config = {
598 	.reg_bits	= 32,
599 	.val_bits	= 32,
600 	.reg_stride	= 4,
601 	.max_register	= 0x5800,
602 };
603 
604 static int sun4i_backend_bind(struct device *dev, struct device *master,
605 			      void *data)
606 {
607 	struct platform_device *pdev = to_platform_device(dev);
608 	struct drm_device *drm = data;
609 	struct sun4i_drv *drv = drm->dev_private;
610 	struct sun4i_backend *backend;
611 	const struct sun4i_backend_quirks *quirks;
612 	struct resource *res;
613 	void __iomem *regs;
614 	int i, ret;
615 
616 	backend = devm_kzalloc(dev, sizeof(*backend), GFP_KERNEL);
617 	if (!backend)
618 		return -ENOMEM;
619 	dev_set_drvdata(dev, backend);
620 	spin_lock_init(&backend->frontend_lock);
621 
622 	backend->engine.node = dev->of_node;
623 	backend->engine.ops = &sun4i_backend_engine_ops;
624 	backend->engine.id = sun4i_backend_of_get_id(dev->of_node);
625 	if (backend->engine.id < 0)
626 		return backend->engine.id;
627 
628 	backend->frontend = sun4i_backend_find_frontend(drv, dev->of_node);
629 	if (IS_ERR(backend->frontend))
630 		dev_warn(dev, "Couldn't find matching frontend, frontend features disabled\n");
631 
632 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
633 	regs = devm_ioremap_resource(dev, res);
634 	if (IS_ERR(regs))
635 		return PTR_ERR(regs);
636 
637 	backend->reset = devm_reset_control_get(dev, NULL);
638 	if (IS_ERR(backend->reset)) {
639 		dev_err(dev, "Couldn't get our reset line\n");
640 		return PTR_ERR(backend->reset);
641 	}
642 
643 	ret = reset_control_deassert(backend->reset);
644 	if (ret) {
645 		dev_err(dev, "Couldn't deassert our reset line\n");
646 		return ret;
647 	}
648 
649 	backend->bus_clk = devm_clk_get(dev, "ahb");
650 	if (IS_ERR(backend->bus_clk)) {
651 		dev_err(dev, "Couldn't get the backend bus clock\n");
652 		ret = PTR_ERR(backend->bus_clk);
653 		goto err_assert_reset;
654 	}
655 	clk_prepare_enable(backend->bus_clk);
656 
657 	backend->mod_clk = devm_clk_get(dev, "mod");
658 	if (IS_ERR(backend->mod_clk)) {
659 		dev_err(dev, "Couldn't get the backend module clock\n");
660 		ret = PTR_ERR(backend->mod_clk);
661 		goto err_disable_bus_clk;
662 	}
663 	clk_prepare_enable(backend->mod_clk);
664 
665 	backend->ram_clk = devm_clk_get(dev, "ram");
666 	if (IS_ERR(backend->ram_clk)) {
667 		dev_err(dev, "Couldn't get the backend RAM clock\n");
668 		ret = PTR_ERR(backend->ram_clk);
669 		goto err_disable_mod_clk;
670 	}
671 	clk_prepare_enable(backend->ram_clk);
672 
673 	if (of_device_is_compatible(dev->of_node,
674 				    "allwinner,sun8i-a33-display-backend")) {
675 		ret = sun4i_backend_init_sat(dev);
676 		if (ret) {
677 			dev_err(dev, "Couldn't init SAT resources\n");
678 			goto err_disable_ram_clk;
679 		}
680 	}
681 
682 	backend->engine.regs = devm_regmap_init_mmio(dev, regs,
683 						     &sun4i_backend_regmap_config);
684 	if (IS_ERR(backend->engine.regs)) {
685 		dev_err(dev, "Couldn't create the backend regmap\n");
686 		return PTR_ERR(backend->engine.regs);
687 	}
688 
689 	list_add_tail(&backend->engine.list, &drv->engine_list);
690 
691 	/*
692 	 * Many of the backend's layer configuration registers have
693 	 * undefined default values. This poses a risk as we use
694 	 * regmap_update_bits in some places, and don't overwrite
695 	 * the whole register.
696 	 *
697 	 * Clear the registers here to have something predictable.
698 	 */
699 	for (i = 0x800; i < 0x1000; i += 4)
700 		regmap_write(backend->engine.regs, i, 0);
701 
702 	/* Disable registers autoloading */
703 	regmap_write(backend->engine.regs, SUN4I_BACKEND_REGBUFFCTL_REG,
704 		     SUN4I_BACKEND_REGBUFFCTL_AUTOLOAD_DIS);
705 
706 	/* Enable the backend */
707 	regmap_write(backend->engine.regs, SUN4I_BACKEND_MODCTL_REG,
708 		     SUN4I_BACKEND_MODCTL_DEBE_EN |
709 		     SUN4I_BACKEND_MODCTL_START_CTL);
710 
711 	/* Set output selection if needed */
712 	quirks = of_device_get_match_data(dev);
713 	if (quirks->needs_output_muxing) {
714 		/*
715 		 * We assume there is no dynamic muxing of backends
716 		 * and TCONs, so we select the backend with same ID.
717 		 *
718 		 * While dynamic selection might be interesting, since
719 		 * the CRTC is tied to the TCON, while the layers are
720 		 * tied to the backends, this means, we will need to
721 		 * switch between groups of layers. There might not be
722 		 * a way to represent this constraint in DRM.
723 		 */
724 		regmap_update_bits(backend->engine.regs,
725 				   SUN4I_BACKEND_MODCTL_REG,
726 				   SUN4I_BACKEND_MODCTL_OUT_SEL,
727 				   (backend->engine.id
728 				    ? SUN4I_BACKEND_MODCTL_OUT_LCD1
729 				    : SUN4I_BACKEND_MODCTL_OUT_LCD0));
730 	}
731 
732 	return 0;
733 
734 err_disable_ram_clk:
735 	clk_disable_unprepare(backend->ram_clk);
736 err_disable_mod_clk:
737 	clk_disable_unprepare(backend->mod_clk);
738 err_disable_bus_clk:
739 	clk_disable_unprepare(backend->bus_clk);
740 err_assert_reset:
741 	reset_control_assert(backend->reset);
742 	return ret;
743 }
744 
745 static void sun4i_backend_unbind(struct device *dev, struct device *master,
746 				 void *data)
747 {
748 	struct sun4i_backend *backend = dev_get_drvdata(dev);
749 
750 	list_del(&backend->engine.list);
751 
752 	if (of_device_is_compatible(dev->of_node,
753 				    "allwinner,sun8i-a33-display-backend"))
754 		sun4i_backend_free_sat(dev);
755 
756 	clk_disable_unprepare(backend->ram_clk);
757 	clk_disable_unprepare(backend->mod_clk);
758 	clk_disable_unprepare(backend->bus_clk);
759 	reset_control_assert(backend->reset);
760 }
761 
762 static const struct component_ops sun4i_backend_ops = {
763 	.bind	= sun4i_backend_bind,
764 	.unbind	= sun4i_backend_unbind,
765 };
766 
767 static int sun4i_backend_probe(struct platform_device *pdev)
768 {
769 	return component_add(&pdev->dev, &sun4i_backend_ops);
770 }
771 
772 static int sun4i_backend_remove(struct platform_device *pdev)
773 {
774 	component_del(&pdev->dev, &sun4i_backend_ops);
775 
776 	return 0;
777 }
778 
779 static const struct sun4i_backend_quirks sun4i_backend_quirks = {
780 	.needs_output_muxing = true,
781 };
782 
783 static const struct sun4i_backend_quirks sun5i_backend_quirks = {
784 };
785 
786 static const struct sun4i_backend_quirks sun6i_backend_quirks = {
787 };
788 
789 static const struct sun4i_backend_quirks sun7i_backend_quirks = {
790 	.needs_output_muxing = true,
791 };
792 
793 static const struct sun4i_backend_quirks sun8i_a33_backend_quirks = {
794 };
795 
796 static const struct of_device_id sun4i_backend_of_table[] = {
797 	{
798 		.compatible = "allwinner,sun4i-a10-display-backend",
799 		.data = &sun4i_backend_quirks,
800 	},
801 	{
802 		.compatible = "allwinner,sun5i-a13-display-backend",
803 		.data = &sun5i_backend_quirks,
804 	},
805 	{
806 		.compatible = "allwinner,sun6i-a31-display-backend",
807 		.data = &sun6i_backend_quirks,
808 	},
809 	{
810 		.compatible = "allwinner,sun7i-a20-display-backend",
811 		.data = &sun7i_backend_quirks,
812 	},
813 	{
814 		.compatible = "allwinner,sun8i-a33-display-backend",
815 		.data = &sun8i_a33_backend_quirks,
816 	},
817 	{ }
818 };
819 MODULE_DEVICE_TABLE(of, sun4i_backend_of_table);
820 
821 static struct platform_driver sun4i_backend_platform_driver = {
822 	.probe		= sun4i_backend_probe,
823 	.remove		= sun4i_backend_remove,
824 	.driver		= {
825 		.name		= "sun4i-backend",
826 		.of_match_table	= sun4i_backend_of_table,
827 	},
828 };
829 module_platform_driver(sun4i_backend_platform_driver);
830 
831 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
832 MODULE_DESCRIPTION("Allwinner A10 Display Backend Driver");
833 MODULE_LICENSE("GPL");
834