• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: (GPL-2.0+ OR MIT)
2 /*
3  * Copyright (c) 2021 Rockchip Electronics Co., Ltd.
4  * Author: Sandy Huang <hjc@rock-chips.com>
5  */
6 #include <linux/memblock.h>
7 #include <linux/of_address.h>
8 #include <linux/of_platform.h>
9 #include <linux/clk.h>
10 #include <linux/clk-provider.h>
11 #include <linux/iommu.h>
12 
13 #include <drm/drm_atomic_uapi.h>
14 #include <drm/drm_drv.h>
15 #include <drm/drm_gem_cma_helper.h>
16 #include <drm/drm_of.h>
17 #include <drm/drm_probe_helper.h>
18 
19 #include "rockchip_drm_drv.h"
20 #include "rockchip_drm_fb.h"
21 #include "rockchip_drm_logo.h"
22 
is_support_hotplug(uint32_t output_type)23 static bool is_support_hotplug(uint32_t output_type)
24 {
25 	switch (output_type) {
26 	case DRM_MODE_CONNECTOR_DVII:
27 	case DRM_MODE_CONNECTOR_DVID:
28 	case DRM_MODE_CONNECTOR_DVIA:
29 	case DRM_MODE_CONNECTOR_DisplayPort:
30 	case DRM_MODE_CONNECTOR_HDMIA:
31 	case DRM_MODE_CONNECTOR_HDMIB:
32 	case DRM_MODE_CONNECTOR_TV:
33 		return true;
34 	default:
35 		return false;
36 	}
37 }
38 
39 static struct drm_crtc *
find_crtc_by_node(struct drm_device * drm_dev,struct device_node * node)40 find_crtc_by_node(struct drm_device *drm_dev, struct device_node *node)
41 {
42 	struct device_node *np_crtc;
43 	struct drm_crtc *crtc;
44 
45 	np_crtc = of_get_parent(node);
46 	if (!np_crtc || !of_device_is_available(np_crtc))
47 		return NULL;
48 
49 	drm_for_each_crtc(crtc, drm_dev) {
50 		if (crtc->port == np_crtc)
51 			return crtc;
52 	}
53 
54 	return NULL;
55 }
56 
57 static struct rockchip_drm_sub_dev *
find_sub_dev_by_node(struct drm_device * drm_dev,struct device_node * node)58 find_sub_dev_by_node(struct drm_device *drm_dev, struct device_node *node)
59 {
60 	struct device_node *np_connector;
61 	struct rockchip_drm_sub_dev *sub_dev;
62 
63 	np_connector = of_graph_get_remote_port_parent(node);
64 	if (!np_connector || !of_device_is_available(np_connector))
65 		return NULL;
66 
67 	sub_dev = rockchip_drm_get_sub_dev(np_connector);
68 	if (!sub_dev)
69 		return NULL;
70 
71 	return sub_dev;
72 }
73 
74 static struct rockchip_drm_sub_dev *
find_sub_dev_by_bridge(struct drm_device * drm_dev,struct device_node * node)75 find_sub_dev_by_bridge(struct drm_device *drm_dev, struct device_node *node)
76 {
77 	struct device_node *np_encoder, *np_connector = NULL;
78 	struct rockchip_drm_sub_dev *sub_dev = NULL;
79 	struct device_node *port, *endpoint;
80 
81 	np_encoder = of_graph_get_remote_port_parent(node);
82 	if (!np_encoder || !of_device_is_available(np_encoder))
83 		goto err_put_encoder;
84 
85 	port = of_graph_get_port_by_id(np_encoder, 1);
86 	if (!port) {
87 		dev_err(drm_dev->dev, "can't found port point!\n");
88 		goto err_put_encoder;
89 	}
90 
91 	for_each_child_of_node(port, endpoint) {
92 		np_connector = of_graph_get_remote_port_parent(endpoint);
93 		if (!np_connector) {
94 			dev_err(drm_dev->dev,
95 				"can't found connector node, please init!\n");
96 			goto err_put_port;
97 		}
98 		if (!of_device_is_available(np_connector)) {
99 			of_node_put(np_connector);
100 			np_connector = NULL;
101 			continue;
102 		} else {
103 			break;
104 		}
105 	}
106 	if (!np_connector) {
107 		dev_err(drm_dev->dev, "can't found available connector node!\n");
108 		goto err_put_port;
109 	}
110 
111 sub_dev = rockchip_drm_get_sub_dev(np_connector);
112 	if (!sub_dev)
113 		goto err_put_port;
114 
115 	of_node_put(np_connector);
116 err_put_port:
117 	of_node_put(port);
118 err_put_encoder:
119 	of_node_put(np_encoder);
120 
121 	return sub_dev;
122 }
123 
124 static unsigned long
rockchip_drm_free_reserved_area(void * start,void * end,int poison,const char * s)125 rockchip_drm_free_reserved_area(void *start, void *end, int poison, const char *s)
126 {
127 	void *pos;
128 	unsigned long pages = 0;
129 
130 	start = (void *)PAGE_ALIGN((unsigned long)start);
131 	end = (void *)((unsigned long)end & PAGE_MASK);
132 	for (pos = start; pos < end; pos += PAGE_SIZE, pages++) {
133 		struct page *page = virt_to_page(pos);
134 		void *direct_map_addr;
135 
136 		/*
137 		 * 'direct_map_addr' might be different from 'pos'
138 		 * because some architectures' virt_to_page()
139 		 * work with aliases.  Getting the direct map
140 		 * address ensures that we get a _writeable_
141 		 * alias for the memset().
142 		 */
143 		direct_map_addr = page_address(page);
144 		/*
145 		 * Perform a kasan-unchecked memset() since this memory
146 		 * has not been initialized.
147 		 */
148 		direct_map_addr = kasan_reset_tag(direct_map_addr);
149 		if ((unsigned int)poison <= 0xFF)
150 			memset(direct_map_addr, poison, PAGE_SIZE);
151 
152 		free_reserved_page(page);
153 	}
154 
155 	if (pages && s)
156 		pr_info("Freeing %s memory: %ldK\n", s, pages << (PAGE_SHIFT - 10));
157 
158 	return pages;
159 }
160 
rockchip_free_loader_memory(struct drm_device * drm)161 void rockchip_free_loader_memory(struct drm_device *drm)
162 {
163 	struct rockchip_drm_private *private = drm->dev_private;
164 	struct rockchip_logo *logo;
165 	void *start, *end;
166 
167 	if (!private || !private->logo || --private->logo->count)
168 		return;
169 
170 	logo = private->logo;
171 	start = phys_to_virt(logo->dma_addr);
172 	end = phys_to_virt(logo->dma_addr + logo->size);
173 
174 	if (private->domain) {
175 		u32 pg_size = 1UL << __ffs(private->domain->pgsize_bitmap);
176 
177 		iommu_unmap(private->domain, logo->dma_addr, ALIGN(logo->size, pg_size));
178 	}
179 
180 	memblock_free(logo->start, logo->size);
181 	rockchip_drm_free_reserved_area(start, end, -1, "drm_logo");
182 	kfree(logo);
183 	private->logo = NULL;
184 	private->loader_protect = false;
185 }
186 
init_loader_memory(struct drm_device * drm_dev)187 static int init_loader_memory(struct drm_device *drm_dev)
188 {
189 	struct rockchip_drm_private *private = drm_dev->dev_private;
190 	struct rockchip_logo *logo;
191 	struct device_node *np = drm_dev->dev->of_node;
192 	struct device_node *node;
193 	phys_addr_t start, size;
194 	u32 pg_size = PAGE_SIZE;
195 	struct resource res;
196 	int ret, idx;
197 
198 	idx = of_property_match_string(np, "memory-region-names", "drm-logo");
199 	if (idx >= 0)
200 		node = of_parse_phandle(np, "memory-region", idx);
201 	else
202 		node = of_parse_phandle(np, "logo-memory-region", 0);
203 	if (!node)
204 		return -ENOMEM;
205 
206 	ret = of_address_to_resource(node, 0, &res);
207 	if (ret)
208 		return ret;
209 	if (private->domain)
210 		pg_size = 1UL << __ffs(private->domain->pgsize_bitmap);
211 	start = ALIGN_DOWN(res.start, pg_size);
212 	size = resource_size(&res);
213 	if (!size)
214 		return -ENOMEM;
215 
216 	logo = kmalloc(sizeof(*logo), GFP_KERNEL);
217 	if (!logo)
218 		return -ENOMEM;
219 
220 	logo->kvaddr = phys_to_virt(start);
221 
222 	if (private->domain) {
223 		ret = iommu_map(private->domain, start, start, ALIGN(size, pg_size),
224 				IOMMU_WRITE | IOMMU_READ);
225 		if (ret) {
226 			dev_err(drm_dev->dev, "failed to create 1v1 mapping\n");
227 			goto err_free_logo;
228 		}
229 	}
230 
231 	logo->dma_addr = start;
232 	logo->size = size;
233 	logo->count = 1;
234 	private->logo = logo;
235 
236 	idx = of_property_match_string(np, "memory-region-names", "drm-cubic-lut");
237 	if (idx < 0)
238 		return 0;
239 
240 	node = of_parse_phandle(np, "memory-region", idx);
241 	if (!node)
242 		return -ENOMEM;
243 
244 	ret = of_address_to_resource(node, 0, &res);
245 	if (ret)
246 		return ret;
247 	start = ALIGN_DOWN(res.start, pg_size);
248 	size = resource_size(&res);
249 	if (!size)
250 		return 0;
251 
252 	private->cubic_lut_kvaddr = phys_to_virt(start);
253 	if (private->domain) {
254 		ret = iommu_map(private->domain, start, start, ALIGN(size, pg_size),
255 				IOMMU_WRITE | IOMMU_READ);
256 		if (ret) {
257 			dev_err(drm_dev->dev, "failed to create 1v1 mapping for cubic lut\n");
258 			goto err_free_logo;
259 		}
260 	}
261 	private->cubic_lut_dma_addr = start;
262 
263 	return 0;
264 
265 err_free_logo:
266 	kfree(logo);
267 
268 	return ret;
269 }
270 
271 static struct drm_framebuffer *
get_framebuffer_by_node(struct drm_device * drm_dev,struct device_node * node)272 get_framebuffer_by_node(struct drm_device *drm_dev, struct device_node *node)
273 {
274 	struct rockchip_drm_private *private = drm_dev->dev_private;
275 	struct drm_mode_fb_cmd2 mode_cmd = { 0 };
276 	u32 val;
277 	int bpp;
278 
279 	if (WARN_ON(!private->logo))
280 		return NULL;
281 
282 	if (of_property_read_u32(node, "logo,offset", &val)) {
283 		pr_err("%s: failed to get logo,offset\n", __func__);
284 		return NULL;
285 	}
286 	mode_cmd.offsets[0] = val;
287 
288 	if (of_property_read_u32(node, "logo,width", &val)) {
289 		pr_err("%s: failed to get logo,width\n", __func__);
290 		return NULL;
291 	}
292 	mode_cmd.width = val;
293 
294 	if (of_property_read_u32(node, "logo,height", &val)) {
295 		pr_err("%s: failed to get logo,height\n", __func__);
296 		return NULL;
297 	}
298 	mode_cmd.height = val;
299 
300 	if (of_property_read_u32(node, "logo,bpp", &val)) {
301 		pr_err("%s: failed to get logo,bpp\n", __func__);
302 		return NULL;
303 	}
304 	bpp = val;
305 
306 	mode_cmd.pitches[0] = ALIGN(mode_cmd.width * bpp, 32) / 8;
307 
308 	switch (bpp) {
309 	case 16:
310 		mode_cmd.pixel_format = DRM_FORMAT_RGB565;
311 		break;
312 	case 24:
313 		mode_cmd.pixel_format = DRM_FORMAT_RGB888;
314 		break;
315 	case 32:
316 		mode_cmd.pixel_format = DRM_FORMAT_XRGB8888;
317 		break;
318 	default:
319 		pr_err("%s: unsupported to logo bpp %d\n", __func__, bpp);
320 		return NULL;
321 	}
322 
323 	return rockchip_drm_logo_fb_alloc(drm_dev, &mode_cmd, private->logo);
324 }
325 
326 static struct rockchip_drm_mode_set *
of_parse_display_resource(struct drm_device * drm_dev,struct device_node * route)327 of_parse_display_resource(struct drm_device *drm_dev, struct device_node *route)
328 {
329 	struct rockchip_drm_private *private = drm_dev->dev_private;
330 	struct rockchip_drm_mode_set *set;
331 	struct device_node *connect;
332 	struct drm_framebuffer *fb;
333 	struct rockchip_drm_sub_dev *sub_dev;
334 	struct drm_crtc *crtc;
335 	const char *string;
336 	u32 val;
337 
338 	connect = of_parse_phandle(route, "connect", 0);
339 	if (!connect)
340 		return NULL;
341 
342 	fb = get_framebuffer_by_node(drm_dev, route);
343 	if (IS_ERR_OR_NULL(fb))
344 		return NULL;
345 
346 	crtc = find_crtc_by_node(drm_dev, connect);
347 
348 	sub_dev = find_sub_dev_by_node(drm_dev, connect);
349 
350 	if (!sub_dev)
351 		sub_dev = find_sub_dev_by_bridge(drm_dev, connect);
352 
353 	if (!crtc || !sub_dev) {
354 		dev_warn(drm_dev->dev,
355 			 "No available crtc or connector for display");
356 		drm_framebuffer_put(fb);
357 		return NULL;
358 	}
359 
360 	set = kzalloc(sizeof(*set), GFP_KERNEL);
361 	if (!set)
362 		return NULL;
363 
364 	if (!of_property_read_u32(route, "video,clock", &val))
365 		set->clock = val;
366 
367 	if (!of_property_read_u32(route, "video,hdisplay", &val))
368 		set->hdisplay = val;
369 
370 	if (!of_property_read_u32(route, "video,vdisplay", &val))
371 		set->vdisplay = val;
372 
373 	if (!of_property_read_u32(route, "video,crtc_hsync_end", &val))
374 		set->crtc_hsync_end = val;
375 
376 	if (!of_property_read_u32(route, "video,crtc_vsync_end", &val))
377 		set->crtc_vsync_end = val;
378 
379 	if (!of_property_read_u32(route, "video,vrefresh", &val))
380 		set->vrefresh = val;
381 
382 	if (!of_property_read_u32(route, "video,flags", &val))
383 		set->flags = val;
384 
385 	if (!of_property_read_u32(route, "video,aspect_ratio", &val))
386 		set->picture_aspect_ratio = val;
387 
388 	if (!of_property_read_u32(route, "overscan,left_margin", &val))
389 		set->left_margin = val;
390 
391 	if (!of_property_read_u32(route, "overscan,right_margin", &val))
392 		set->right_margin = val;
393 
394 	if (!of_property_read_u32(route, "overscan,top_margin", &val))
395 		set->top_margin = val;
396 
397 	if (!of_property_read_u32(route, "overscan,bottom_margin", &val))
398 		set->bottom_margin = val;
399 
400 	if (!of_property_read_u32(route, "bcsh,brightness", &val))
401 		set->brightness = val;
402 	else
403 		set->brightness = 50;
404 
405 	if (!of_property_read_u32(route, "bcsh,contrast", &val))
406 		set->contrast = val;
407 	else
408 		set->contrast = 50;
409 
410 	if (!of_property_read_u32(route, "bcsh,saturation", &val))
411 		set->saturation = val;
412 	else
413 		set->saturation = 50;
414 
415 	if (!of_property_read_u32(route, "bcsh,hue", &val))
416 		set->hue = val;
417 	else
418 		set->hue = 50;
419 
420 	if (!of_property_read_u32(route, "cubic_lut,offset", &val)) {
421 		private->cubic_lut[crtc->index].enable = true;
422 		private->cubic_lut[crtc->index].offset = val;
423 	}
424 
425 	set->ratio = 1;
426 	if (!of_property_read_string(route, "logo,mode", &string) &&
427 	    !strcmp(string, "fullscreen"))
428 		set->ratio = 0;
429 
430 	set->fb = fb;
431 	set->crtc = crtc;
432 	set->sub_dev = sub_dev;
433 
434 	return set;
435 }
436 
rockchip_drm_fill_connector_modes(struct drm_connector * connector,uint32_t maxX,uint32_t maxY)437 static int rockchip_drm_fill_connector_modes(struct drm_connector *connector,
438 					     uint32_t maxX, uint32_t maxY)
439 {
440 	struct drm_device *dev = connector->dev;
441 	struct drm_display_mode *mode;
442 	const struct drm_connector_helper_funcs *connector_funcs =
443 		connector->helper_private;
444 	int count = 0;
445 	bool verbose_prune = true;
446 	enum drm_connector_status old_status;
447 
448 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
449 
450 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
451 		      connector->name);
452 	/* set all modes to the unverified state */
453 	list_for_each_entry(mode, &connector->modes, head)
454 		mode->status = MODE_STALE;
455 
456 	if (connector->force) {
457 		if (connector->force == DRM_FORCE_ON ||
458 		    connector->force == DRM_FORCE_ON_DIGITAL)
459 			connector->status = connector_status_connected;
460 		else
461 			connector->status = connector_status_disconnected;
462 		if (connector->funcs->force)
463 			connector->funcs->force(connector);
464 	} else {
465 		old_status = connector->status;
466 
467 		if (connector->funcs->detect)
468 			connector->status = connector->funcs->detect(connector, true);
469 		else
470 			connector->status  = connector_status_connected;
471 		/*
472 		 * Normally either the driver's hpd code or the poll loop should
473 		 * pick up any changes and fire the hotplug event. But if
474 		 * userspace sneaks in a probe, we might miss a change. Hence
475 		 * check here, and if anything changed start the hotplug code.
476 		 */
477 		if (old_status != connector->status) {
478 			DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n",
479 				      connector->base.id,
480 				      connector->name,
481 				      old_status, connector->status);
482 
483 			/*
484 			 * The hotplug event code might call into the fb
485 			 * helpers, and so expects that we do not hold any
486 			 * locks. Fire up the poll struct instead, it will
487 			 * disable itself again.
488 			 */
489 			dev->mode_config.delayed_event = true;
490 			if (dev->mode_config.poll_enabled)
491 				schedule_delayed_work(&dev->mode_config.output_poll_work,
492 						      0);
493 		}
494 	}
495 
496 	/* Re-enable polling in case the global poll config changed. */
497 	if (!dev->mode_config.poll_running)
498 		drm_kms_helper_poll_enable(dev);
499 
500 	dev->mode_config.poll_running = true;
501 
502 	if (connector->status == connector_status_disconnected) {
503 		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
504 			      connector->base.id, connector->name);
505 		drm_connector_update_edid_property(connector, NULL);
506 		verbose_prune = false;
507 		goto prune;
508 	}
509 
510 	count = (*connector_funcs->get_modes)(connector);
511 
512 	if (count == 0 && connector->status == connector_status_connected)
513 		count = drm_add_modes_noedid(connector, 1024, 768);
514 	if (count == 0)
515 		goto prune;
516 
517 	drm_connector_list_update(connector);
518 
519 	list_for_each_entry(mode, &connector->modes, head) {
520 		if (mode->status == MODE_OK)
521 			mode->status = drm_mode_validate_driver(dev, mode);
522 
523 		if (mode->status == MODE_OK)
524 			mode->status = drm_mode_validate_size(mode, maxX, maxY);
525 
526 		/**
527 		 * if (mode->status == MODE_OK)
528 		 *	mode->status = drm_mode_validate_flag(mode, mode_flags);
529 		 */
530 		if (mode->status == MODE_OK && connector_funcs->mode_valid)
531 			mode->status = connector_funcs->mode_valid(connector,
532 								   mode);
533 		if (mode->status == MODE_OK)
534 			mode->status = drm_mode_validate_ycbcr420(mode,
535 								  connector);
536 	}
537 
538 prune:
539 	drm_mode_prune_invalid(dev, &connector->modes, verbose_prune);
540 
541 	if (list_empty(&connector->modes))
542 		return 0;
543 
544 	drm_mode_sort(&connector->modes);
545 
546 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
547 		      connector->name);
548 	list_for_each_entry(mode, &connector->modes, head) {
549 		drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
550 		drm_mode_debug_printmodeline(mode);
551 	}
552 
553 	return count;
554 }
555 
556 /*
557  * For connectors that support multiple encoders, either the
558  * .atomic_best_encoder() or .best_encoder() operation must be implemented.
559  */
560 static struct drm_encoder *
rockchip_drm_connector_get_single_encoder(struct drm_connector * connector)561 rockchip_drm_connector_get_single_encoder(struct drm_connector *connector)
562 {
563 	struct drm_encoder *encoder;
564 
565 	WARN_ON(hweight32(connector->possible_encoders) > 1);
566 	drm_connector_for_each_possible_encoder(connector, encoder)
567 		return encoder;
568 
569 	return NULL;
570 }
571 
setup_initial_state(struct drm_device * drm_dev,struct drm_atomic_state * state,struct rockchip_drm_mode_set * set)572 static int setup_initial_state(struct drm_device *drm_dev,
573 			       struct drm_atomic_state *state,
574 			       struct rockchip_drm_mode_set *set)
575 {
576 	struct rockchip_drm_private *priv = drm_dev->dev_private;
577 	struct drm_connector *connector = set->sub_dev->connector;
578 	struct drm_crtc *crtc = set->crtc;
579 	struct drm_crtc_state *crtc_state;
580 	struct drm_connector_state *conn_state;
581 	struct drm_plane_state *primary_state;
582 	struct drm_display_mode *mode = NULL;
583 	const struct drm_connector_helper_funcs *funcs;
584 	int pipe = drm_crtc_index(crtc);
585 	bool is_crtc_enabled = true;
586 	int hdisplay, vdisplay;
587 	int fb_width, fb_height;
588 	int found = 0, match = 0;
589 	int num_modes;
590 	int ret = 0;
591 	struct rockchip_crtc_state *s = NULL;
592 
593 	if (!set->hdisplay || !set->vdisplay || !set->vrefresh)
594 		is_crtc_enabled = false;
595 
596 	conn_state = drm_atomic_get_connector_state(state, connector);
597 	if (IS_ERR(conn_state))
598 		return PTR_ERR(conn_state);
599 
600 	funcs = connector->helper_private;
601 
602 	if (funcs->best_encoder)
603 		conn_state->best_encoder = funcs->best_encoder(connector);
604 	else
605 		conn_state->best_encoder = rockchip_drm_connector_get_single_encoder(connector);
606 
607 	if (set->sub_dev->loader_protect)
608 		set->sub_dev->loader_protect(conn_state->best_encoder, true);
609 	num_modes = rockchip_drm_fill_connector_modes(connector, 4096, 4096);
610 	if (!num_modes) {
611 		dev_err(drm_dev->dev, "connector[%s] can't found any modes\n",
612 			connector->name);
613 		ret = -EINVAL;
614 		goto error_conn;
615 	}
616 
617 	list_for_each_entry(mode, &connector->modes, head) {
618 		if (mode->clock == set->clock &&
619 		    mode->hdisplay == set->hdisplay &&
620 		    mode->vdisplay == set->vdisplay &&
621 		    mode->crtc_hsync_end == set->crtc_hsync_end &&
622 		    mode->crtc_vsync_end == set->crtc_vsync_end &&
623 		    drm_mode_vrefresh(mode) == set->vrefresh &&
624 		    /* we just need to focus on DRM_MODE_FLAG_ALL flag, so here
625 		     * we compare mode->flags with set->flags & DRM_MODE_FLAG_ALL.
626 		     */
627 		    mode->flags == (set->flags & DRM_MODE_FLAG_ALL) &&
628 		    mode->picture_aspect_ratio == set->picture_aspect_ratio) {
629 			found = 1;
630 			match = 1;
631 			break;
632 		}
633 	}
634 
635 	if (!found) {
636 		ret = -EINVAL;
637 		connector->status = connector_status_disconnected;
638 		dev_err(drm_dev->dev, "connector[%s] can't found any match mode\n",
639 			connector->name);
640 		DRM_INFO("%s support modes:\n\n", connector->name);
641 		list_for_each_entry(mode, &connector->modes, head) {
642 			DRM_INFO(DRM_MODE_FMT "\n", DRM_MODE_ARG(mode));
643 		}
644 		DRM_INFO("uboot set mode: h/v display[%d,%d] h/v sync_end[%d,%d] vfresh[%d], flags[0x%x], aspect_ratio[%d]\n",
645 			 set->hdisplay, set->vdisplay, set->crtc_hsync_end, set->crtc_vsync_end,
646 			 set->vrefresh, set->flags, set->picture_aspect_ratio);
647 		goto error_conn;
648 	}
649 
650 	conn_state->tv.brightness = set->brightness;
651 	conn_state->tv.contrast = set->contrast;
652 	conn_state->tv.saturation = set->saturation;
653 	conn_state->tv.hue = set->hue;
654 	set->mode = mode;
655 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
656 	if (IS_ERR(crtc_state)) {
657 		ret = PTR_ERR(crtc_state);
658 		goto error_conn;
659 	}
660 
661 	drm_mode_copy(&crtc_state->adjusted_mode, mode);
662 	if (!match || !is_crtc_enabled) {
663 		set->mode_changed = true;
664 	} else {
665 		ret = drm_atomic_set_crtc_for_connector(conn_state, crtc);
666 		if (ret)
667 			goto error_conn;
668 
669 		mode->picture_aspect_ratio = HDMI_PICTURE_ASPECT_NONE;
670 		ret = drm_atomic_set_mode_for_crtc(crtc_state, mode);
671 		if (ret)
672 			goto error_conn;
673 
674 		crtc_state->active = true;
675 
676 		if (priv->crtc_funcs[pipe] &&
677 		    priv->crtc_funcs[pipe]->loader_protect)
678 			priv->crtc_funcs[pipe]->loader_protect(crtc, true);
679 	}
680 
681 	if (!set->fb) {
682 		ret = 0;
683 		goto error_crtc;
684 	}
685 	primary_state = drm_atomic_get_plane_state(state, crtc->primary);
686 	if (IS_ERR(primary_state)) {
687 		ret = PTR_ERR(primary_state);
688 		goto error_crtc;
689 	}
690 
691 	hdisplay = mode->hdisplay;
692 	vdisplay = mode->vdisplay;
693 	fb_width = set->fb->width;
694 	fb_height = set->fb->height;
695 
696 	primary_state->crtc = crtc;
697 	primary_state->src_x = 0;
698 	primary_state->src_y = 0;
699 	primary_state->src_w = fb_width << 16;
700 	primary_state->src_h = fb_height << 16;
701 	if (set->ratio) {
702 		if (set->fb->width >= hdisplay) {
703 			primary_state->crtc_x = 0;
704 			primary_state->crtc_w = hdisplay;
705 		} else {
706 			primary_state->crtc_x = (hdisplay - fb_width) / 2;
707 			primary_state->crtc_w = set->fb->width;
708 		}
709 
710 		if (set->fb->height >= vdisplay) {
711 			primary_state->crtc_y = 0;
712 			primary_state->crtc_h = vdisplay;
713 		} else {
714 			primary_state->crtc_y = (vdisplay - fb_height) / 2;
715 			primary_state->crtc_h = fb_height;
716 		}
717 	} else {
718 		primary_state->crtc_x = 0;
719 		primary_state->crtc_y = 0;
720 		primary_state->crtc_w = hdisplay;
721 		primary_state->crtc_h = vdisplay;
722 	}
723 	s = to_rockchip_crtc_state(crtc->state);
724 	s->output_type = connector->connector_type;
725 
726 	return 0;
727 
728 error_crtc:
729 	if (priv->crtc_funcs[pipe] && priv->crtc_funcs[pipe]->loader_protect)
730 		priv->crtc_funcs[pipe]->loader_protect(crtc, false);
731 error_conn:
732 	if (set->sub_dev->loader_protect)
733 		set->sub_dev->loader_protect(conn_state->best_encoder, false);
734 
735 	return ret;
736 }
737 
update_state(struct drm_device * drm_dev,struct drm_atomic_state * state,struct rockchip_drm_mode_set * set,unsigned int * plane_mask)738 static int update_state(struct drm_device *drm_dev,
739 			struct drm_atomic_state *state,
740 			struct rockchip_drm_mode_set *set,
741 			unsigned int *plane_mask)
742 {
743 	struct drm_crtc *crtc = set->crtc;
744 	struct drm_connector *connector = set->sub_dev->connector;
745 	struct drm_display_mode *mode = set->mode;
746 	struct drm_plane_state *primary_state;
747 	struct drm_crtc_state *crtc_state;
748 	struct drm_connector_state *conn_state;
749 	int ret;
750 	struct rockchip_crtc_state *s;
751 
752 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
753 	if (IS_ERR(crtc_state))
754 		return PTR_ERR(crtc_state);
755 	conn_state = drm_atomic_get_connector_state(state, connector);
756 	if (IS_ERR(conn_state))
757 		return PTR_ERR(conn_state);
758 	s = to_rockchip_crtc_state(crtc_state);
759 	s->left_margin = set->left_margin;
760 	s->right_margin = set->right_margin;
761 	s->top_margin = set->top_margin;
762 	s->bottom_margin = set->bottom_margin;
763 
764 	if (set->mode_changed) {
765 		ret = drm_atomic_set_crtc_for_connector(conn_state, crtc);
766 		if (ret)
767 			return ret;
768 
769 		ret = drm_atomic_set_mode_for_crtc(crtc_state, mode);
770 		if (ret)
771 			return ret;
772 
773 		crtc_state->active = true;
774 	} else {
775 		const struct drm_encoder_helper_funcs *encoder_helper_funcs;
776 		const struct drm_connector_helper_funcs *connector_helper_funcs;
777 		struct drm_encoder *encoder;
778 
779 		connector_helper_funcs = connector->helper_private;
780 		if (!connector_helper_funcs)
781 			return -ENXIO;
782 		if (connector_helper_funcs->best_encoder)
783 			encoder = connector_helper_funcs->best_encoder(connector);
784 		else
785 			encoder = rockchip_drm_connector_get_single_encoder(connector);
786 		if (!encoder)
787 			return -ENXIO;
788 		encoder_helper_funcs = encoder->helper_private;
789 		if (!encoder_helper_funcs->atomic_check)
790 			return -ENXIO;
791 		ret = encoder_helper_funcs->atomic_check(encoder, crtc->state,
792 							 conn_state);
793 		if (ret)
794 			return ret;
795 
796 		if (encoder_helper_funcs->atomic_mode_set)
797 			encoder_helper_funcs->atomic_mode_set(encoder,
798 							      crtc_state,
799 							      conn_state);
800 		else if (encoder_helper_funcs->mode_set)
801 			encoder_helper_funcs->mode_set(encoder, mode, mode);
802 	}
803 
804 	primary_state = drm_atomic_get_plane_state(state, crtc->primary);
805 	if (IS_ERR(primary_state))
806 		return PTR_ERR(primary_state);
807 
808 	crtc_state->plane_mask = 1 << drm_plane_index(crtc->primary);
809 	*plane_mask |= crtc_state->plane_mask;
810 
811 
812 	drm_atomic_set_fb_for_plane(primary_state, set->fb);
813 	drm_framebuffer_put(set->fb);
814 	ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
815 
816 	return ret;
817 }
818 
rockchip_drm_show_logo(struct drm_device * drm_dev)819 void rockchip_drm_show_logo(struct drm_device *drm_dev)
820 {
821 	struct drm_atomic_state *state, *old_state;
822 	struct device_node *np = drm_dev->dev->of_node;
823 	struct drm_mode_config *mode_config = &drm_dev->mode_config;
824 	struct rockchip_drm_private *private = drm_dev->dev_private;
825 	struct device_node *root, *route;
826 	struct rockchip_drm_mode_set *set, *tmp, *unset;
827 	struct list_head mode_set_list;
828 	struct list_head mode_unset_list;
829 	unsigned int plane_mask = 0;
830 	struct drm_crtc *crtc;
831 	int ret, i;
832 
833 	root = of_get_child_by_name(np, "route");
834 	if (!root) {
835 		dev_warn(drm_dev->dev, "failed to parse resources for logo display\n");
836 		return;
837 	}
838 
839 	if (init_loader_memory(drm_dev)) {
840 		dev_warn(drm_dev->dev, "failed to parse loader memory\n");
841 		return;
842 	}
843 
844 	INIT_LIST_HEAD(&mode_set_list);
845 	INIT_LIST_HEAD(&mode_unset_list);
846 	drm_modeset_lock_all(drm_dev);
847 	state = drm_atomic_state_alloc(drm_dev);
848 	if (!state) {
849 		dev_err(drm_dev->dev, "failed to alloc atomic state for logo display\n");
850 		ret = -ENOMEM;
851 		goto err_unlock;
852 	}
853 
854 	state->acquire_ctx = mode_config->acquire_ctx;
855 
856 	for_each_child_of_node(root, route) {
857 		if (!of_device_is_available(route))
858 			continue;
859 
860 		set = of_parse_display_resource(drm_dev, route);
861 		if (!set)
862 			continue;
863 
864 		if (setup_initial_state(drm_dev, state, set)) {
865 			drm_framebuffer_put(set->fb);
866 			INIT_LIST_HEAD(&set->head);
867 			list_add_tail(&set->head, &mode_unset_list);
868 			continue;
869 		}
870 
871 		INIT_LIST_HEAD(&set->head);
872 		list_add_tail(&set->head, &mode_set_list);
873 	}
874 
875 	/*
876 	 * the mode_unset_list store the unconnected route, if route's crtc
877 	 * isn't used, we should close it.
878 	 */
879 	list_for_each_entry_safe(unset, tmp, &mode_unset_list, head) {
880 		struct rockchip_drm_mode_set *tmp_set;
881 		int find_used_crtc = 0;
882 
883 		list_for_each_entry_safe(set, tmp_set, &mode_set_list, head) {
884 			if (set->crtc == unset->crtc) {
885 				find_used_crtc = 1;
886 				continue;
887 			}
888 		}
889 
890 		if (!find_used_crtc) {
891 			struct drm_crtc *crtc = unset->crtc;
892 			int pipe = drm_crtc_index(crtc);
893 			struct rockchip_drm_private *priv =
894 							drm_dev->dev_private;
895 
896 			if (unset->hdisplay && unset->vdisplay) {
897 				if (priv->crtc_funcs[pipe] &&
898 				    priv->crtc_funcs[pipe]->loader_protect)
899 					priv->crtc_funcs[pipe]->loader_protect(crtc, true);
900 				priv->crtc_funcs[pipe]->crtc_close(crtc);
901 				if (priv->crtc_funcs[pipe] &&
902 				    priv->crtc_funcs[pipe]->loader_protect)
903 					priv->crtc_funcs[pipe]->loader_protect(crtc, false);
904 			}
905 		}
906 
907 		list_del(&unset->head);
908 		kfree(unset);
909 	}
910 
911 	if (list_empty(&mode_set_list)) {
912 		dev_warn(drm_dev->dev, "can't not find any logo display\n");
913 		ret = -ENXIO;
914 		goto err_free_state;
915 	}
916 
917 	/*
918 	 * The state save initial devices status, swap the state into
919 	 * drm devices as old state, so if new state come, can compare
920 	 * with this state to judge which status need to update.
921 	 */
922 	WARN_ON(drm_atomic_helper_swap_state(state, false));
923 	drm_atomic_state_put(state);
924 	old_state = drm_atomic_helper_duplicate_state(drm_dev,
925 						      mode_config->acquire_ctx);
926 	if (IS_ERR(old_state)) {
927 		dev_err(drm_dev->dev, "failed to duplicate atomic state for logo display\n");
928 		ret = PTR_ERR_OR_ZERO(old_state);
929 		goto err_free_state;
930 	}
931 
932 	state = drm_atomic_helper_duplicate_state(drm_dev,
933 						  mode_config->acquire_ctx);
934 	if (IS_ERR(state)) {
935 		dev_err(drm_dev->dev, "failed to duplicate atomic state for logo display\n");
936 		ret = PTR_ERR_OR_ZERO(state);
937 		goto err_free_old_state;
938 	}
939 	state->acquire_ctx = mode_config->acquire_ctx;
940 
941 	list_for_each_entry(set, &mode_set_list, head)
942 		/*
943 		 * We don't want to see any fail on update_state.
944 		 */
945 		WARN_ON(update_state(drm_dev, state, set, &plane_mask));
946 
947 	for (i = 0; i < state->num_connector; i++) {
948 		if (state->connectors[i].new_state->connector->status !=
949 		    connector_status_connected)
950 			state->connectors[i].new_state->best_encoder = NULL;
951 	}
952 
953 	ret = drm_atomic_commit(state);
954 	/**
955 	 * todo
956 	 * drm_atomic_clean_old_fb(drm_dev, plane_mask, ret);
957 	 */
958 
959 	list_for_each_entry_safe(set, tmp, &mode_set_list, head) {
960 		list_del(&set->head);
961 		kfree(set);
962 	}
963 
964 	/*
965 	 * Is possible get deadlock here?
966 	 */
967 	WARN_ON(ret == -EDEADLK);
968 
969 	if (ret) {
970 		/*
971 		 * restore display status if atomic commit failed.
972 		 */
973 		WARN_ON(drm_atomic_helper_swap_state(old_state, false));
974 		goto err_free_state;
975 	}
976 
977 	rockchip_free_loader_memory(drm_dev);
978 	drm_atomic_state_put(old_state);
979 	drm_atomic_state_put(state);
980 
981 	private->loader_protect = true;
982 	drm_modeset_unlock_all(drm_dev);
983 
984 	drm_for_each_crtc(crtc, drm_dev) {
985 		struct drm_fb_helper *helper = private->fbdev_helper;
986 		struct rockchip_crtc_state *s = NULL;
987 
988 		if (!helper)
989 			break;
990 
991 		s = to_rockchip_crtc_state(crtc->state);
992 		if (is_support_hotplug(s->output_type))
993 			drm_framebuffer_get(helper->fb);
994 	}
995 
996 	return;
997 err_free_old_state:
998 	drm_atomic_state_put(old_state);
999 err_free_state:
1000 	drm_atomic_state_put(state);
1001 err_unlock:
1002 	drm_modeset_unlock_all(drm_dev);
1003 	if (ret)
1004 		dev_err(drm_dev->dev, "failed to show kernel logo\n");
1005 }
1006 
1007 #ifndef MODULE
1008 static const char *const loader_protect_clocks[] __initconst = {
1009 	"hclk_vio",
1010 	"hclk_vop",
1011 	"hclk_vopb",
1012 	"hclk_vopl",
1013 	"aclk_vio",
1014 	"aclk_vio0",
1015 	"aclk_vio1",
1016 	"aclk_vop",
1017 	"aclk_vopb",
1018 	"aclk_vopl",
1019 	"aclk_vo_pre",
1020 	"aclk_vio_pre",
1021 	"dclk_vop",
1022 	"dclk_vop0",
1023 	"dclk_vop1",
1024 	"dclk_vopb",
1025 	"dclk_vopl",
1026 };
1027 
1028 static struct clk **loader_clocks __initdata;
rockchip_clocks_loader_protect(void)1029 static int __init rockchip_clocks_loader_protect(void)
1030 {
1031 	int nclocks = ARRAY_SIZE(loader_protect_clocks);
1032 	struct clk *clk;
1033 	int i;
1034 
1035 	loader_clocks = kcalloc(nclocks, sizeof(void *), GFP_KERNEL);
1036 	if (!loader_clocks)
1037 		return -ENOMEM;
1038 
1039 	for (i = 0; i < nclocks; i++) {
1040 		clk = __clk_lookup(loader_protect_clocks[i]);
1041 
1042 		if (clk) {
1043 			loader_clocks[i] = clk;
1044 			clk_prepare_enable(clk);
1045 		}
1046 	}
1047 
1048 	return 0;
1049 }
1050 arch_initcall_sync(rockchip_clocks_loader_protect);
1051 
rockchip_clocks_loader_unprotect(void)1052 static int __init rockchip_clocks_loader_unprotect(void)
1053 {
1054 	int i;
1055 
1056 	if (!loader_clocks)
1057 		return -ENODEV;
1058 
1059 	for (i = 0; i < ARRAY_SIZE(loader_protect_clocks); i++) {
1060 		struct clk *clk = loader_clocks[i];
1061 
1062 		if (clk)
1063 			clk_disable_unprepare(clk);
1064 	}
1065 	kfree(loader_clocks);
1066 
1067 	return 0;
1068 }
1069 late_initcall_sync(rockchip_clocks_loader_unprotect);
1070 #endif
1071