xref: /openbmc/linux/drivers/gpu/drm/tiny/simpledrm.c (revision 82806c25)
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #include <linux/clk.h>
4 #include <linux/of_clk.h>
5 #include <linux/minmax.h>
6 #include <linux/platform_data/simplefb.h>
7 #include <linux/platform_device.h>
8 #include <linux/regulator/consumer.h>
9 
10 #include <drm/drm_aperture.h>
11 #include <drm/drm_atomic.h>
12 #include <drm/drm_atomic_state_helper.h>
13 #include <drm/drm_connector.h>
14 #include <drm/drm_damage_helper.h>
15 #include <drm/drm_device.h>
16 #include <drm/drm_drv.h>
17 #include <drm/drm_fb_helper.h>
18 #include <drm/drm_format_helper.h>
19 #include <drm/drm_gem_atomic_helper.h>
20 #include <drm/drm_gem_framebuffer_helper.h>
21 #include <drm/drm_gem_shmem_helper.h>
22 #include <drm/drm_managed.h>
23 #include <drm/drm_modeset_helper_vtables.h>
24 #include <drm/drm_plane_helper.h>
25 #include <drm/drm_probe_helper.h>
26 
27 #define DRIVER_NAME	"simpledrm"
28 #define DRIVER_DESC	"DRM driver for simple-framebuffer platform devices"
29 #define DRIVER_DATE	"20200625"
30 #define DRIVER_MAJOR	1
31 #define DRIVER_MINOR	0
32 
33 /*
34  * Assume a monitor resolution of 96 dpi to
35  * get a somewhat reasonable screen size.
36  */
37 #define RES_MM(d)	\
38 	(((d) * 254ul) / (96ul * 10ul))
39 
40 #define SIMPLEDRM_MODE(hd, vd)	\
41 	DRM_SIMPLE_MODE(hd, vd, RES_MM(hd), RES_MM(vd))
42 
43 /*
44  * Helpers for simplefb
45  */
46 
47 static int
48 simplefb_get_validated_int(struct drm_device *dev, const char *name,
49 			   uint32_t value)
50 {
51 	if (value > INT_MAX) {
52 		drm_err(dev, "simplefb: invalid framebuffer %s of %u\n",
53 			name, value);
54 		return -EINVAL;
55 	}
56 	return (int)value;
57 }
58 
59 static int
60 simplefb_get_validated_int0(struct drm_device *dev, const char *name,
61 			    uint32_t value)
62 {
63 	if (!value) {
64 		drm_err(dev, "simplefb: invalid framebuffer %s of %u\n",
65 			name, value);
66 		return -EINVAL;
67 	}
68 	return simplefb_get_validated_int(dev, name, value);
69 }
70 
71 static const struct drm_format_info *
72 simplefb_get_validated_format(struct drm_device *dev, const char *format_name)
73 {
74 	static const struct simplefb_format formats[] = SIMPLEFB_FORMATS;
75 	const struct simplefb_format *fmt = formats;
76 	const struct simplefb_format *end = fmt + ARRAY_SIZE(formats);
77 	const struct drm_format_info *info;
78 
79 	if (!format_name) {
80 		drm_err(dev, "simplefb: missing framebuffer format\n");
81 		return ERR_PTR(-EINVAL);
82 	}
83 
84 	while (fmt < end) {
85 		if (!strcmp(format_name, fmt->name)) {
86 			info = drm_format_info(fmt->fourcc);
87 			if (!info)
88 				return ERR_PTR(-EINVAL);
89 			return info;
90 		}
91 		++fmt;
92 	}
93 
94 	drm_err(dev, "simplefb: unknown framebuffer format %s\n",
95 		format_name);
96 
97 	return ERR_PTR(-EINVAL);
98 }
99 
100 static int
101 simplefb_get_width_pd(struct drm_device *dev,
102 		      const struct simplefb_platform_data *pd)
103 {
104 	return simplefb_get_validated_int0(dev, "width", pd->width);
105 }
106 
107 static int
108 simplefb_get_height_pd(struct drm_device *dev,
109 		       const struct simplefb_platform_data *pd)
110 {
111 	return simplefb_get_validated_int0(dev, "height", pd->height);
112 }
113 
114 static int
115 simplefb_get_stride_pd(struct drm_device *dev,
116 		       const struct simplefb_platform_data *pd)
117 {
118 	return simplefb_get_validated_int(dev, "stride", pd->stride);
119 }
120 
121 static const struct drm_format_info *
122 simplefb_get_format_pd(struct drm_device *dev,
123 		       const struct simplefb_platform_data *pd)
124 {
125 	return simplefb_get_validated_format(dev, pd->format);
126 }
127 
128 static int
129 simplefb_read_u32_of(struct drm_device *dev, struct device_node *of_node,
130 		     const char *name, u32 *value)
131 {
132 	int ret = of_property_read_u32(of_node, name, value);
133 
134 	if (ret)
135 		drm_err(dev, "simplefb: cannot parse framebuffer %s: error %d\n",
136 			name, ret);
137 	return ret;
138 }
139 
140 static int
141 simplefb_read_string_of(struct drm_device *dev, struct device_node *of_node,
142 			const char *name, const char **value)
143 {
144 	int ret = of_property_read_string(of_node, name, value);
145 
146 	if (ret)
147 		drm_err(dev, "simplefb: cannot parse framebuffer %s: error %d\n",
148 			name, ret);
149 	return ret;
150 }
151 
152 static int
153 simplefb_get_width_of(struct drm_device *dev, struct device_node *of_node)
154 {
155 	u32 width;
156 	int ret = simplefb_read_u32_of(dev, of_node, "width", &width);
157 
158 	if (ret)
159 		return ret;
160 	return simplefb_get_validated_int0(dev, "width", width);
161 }
162 
163 static int
164 simplefb_get_height_of(struct drm_device *dev, struct device_node *of_node)
165 {
166 	u32 height;
167 	int ret = simplefb_read_u32_of(dev, of_node, "height", &height);
168 
169 	if (ret)
170 		return ret;
171 	return simplefb_get_validated_int0(dev, "height", height);
172 }
173 
174 static int
175 simplefb_get_stride_of(struct drm_device *dev, struct device_node *of_node)
176 {
177 	u32 stride;
178 	int ret = simplefb_read_u32_of(dev, of_node, "stride", &stride);
179 
180 	if (ret)
181 		return ret;
182 	return simplefb_get_validated_int(dev, "stride", stride);
183 }
184 
185 static const struct drm_format_info *
186 simplefb_get_format_of(struct drm_device *dev, struct device_node *of_node)
187 {
188 	const char *format;
189 	int ret = simplefb_read_string_of(dev, of_node, "format", &format);
190 
191 	if (ret)
192 		return ERR_PTR(ret);
193 	return simplefb_get_validated_format(dev, format);
194 }
195 
196 /*
197  * Simple Framebuffer device
198  */
199 
200 struct simpledrm_device {
201 	struct drm_device dev;
202 
203 	/* clocks */
204 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
205 	unsigned int clk_count;
206 	struct clk **clks;
207 #endif
208 	/* regulators */
209 #if defined CONFIG_OF && defined CONFIG_REGULATOR
210 	unsigned int regulator_count;
211 	struct regulator **regulators;
212 #endif
213 
214 	/* simplefb settings */
215 	struct drm_display_mode mode;
216 	const struct drm_format_info *format;
217 	unsigned int pitch;
218 
219 	/* memory management */
220 	void __iomem *screen_base;
221 
222 	/* modesetting */
223 	uint32_t formats[8];
224 	size_t nformats;
225 	struct drm_plane primary_plane;
226 	struct drm_crtc crtc;
227 	struct drm_encoder encoder;
228 	struct drm_connector connector;
229 };
230 
231 static struct simpledrm_device *simpledrm_device_of_dev(struct drm_device *dev)
232 {
233 	return container_of(dev, struct simpledrm_device, dev);
234 }
235 
236 /*
237  * Hardware
238  */
239 
240 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
241 /*
242  * Clock handling code.
243  *
244  * Here we handle the clocks property of our "simple-framebuffer" dt node.
245  * This is necessary so that we can make sure that any clocks needed by
246  * the display engine that the bootloader set up for us (and for which it
247  * provided a simplefb dt node), stay up, for the life of the simplefb
248  * driver.
249  *
250  * When the driver unloads, we cleanly disable, and then release the clocks.
251  *
252  * We only complain about errors here, no action is taken as the most likely
253  * error can only happen due to a mismatch between the bootloader which set
254  * up simplefb, and the clock definitions in the device tree. Chances are
255  * that there are no adverse effects, and if there are, a clean teardown of
256  * the fb probe will not help us much either. So just complain and carry on,
257  * and hope that the user actually gets a working fb at the end of things.
258  */
259 
260 static void simpledrm_device_release_clocks(void *res)
261 {
262 	struct simpledrm_device *sdev = simpledrm_device_of_dev(res);
263 	unsigned int i;
264 
265 	for (i = 0; i < sdev->clk_count; ++i) {
266 		if (sdev->clks[i]) {
267 			clk_disable_unprepare(sdev->clks[i]);
268 			clk_put(sdev->clks[i]);
269 		}
270 	}
271 }
272 
273 static int simpledrm_device_init_clocks(struct simpledrm_device *sdev)
274 {
275 	struct drm_device *dev = &sdev->dev;
276 	struct platform_device *pdev = to_platform_device(dev->dev);
277 	struct device_node *of_node = pdev->dev.of_node;
278 	struct clk *clock;
279 	unsigned int i;
280 	int ret;
281 
282 	if (dev_get_platdata(&pdev->dev) || !of_node)
283 		return 0;
284 
285 	sdev->clk_count = of_clk_get_parent_count(of_node);
286 	if (!sdev->clk_count)
287 		return 0;
288 
289 	sdev->clks = drmm_kzalloc(dev, sdev->clk_count * sizeof(sdev->clks[0]),
290 				  GFP_KERNEL);
291 	if (!sdev->clks)
292 		return -ENOMEM;
293 
294 	for (i = 0; i < sdev->clk_count; ++i) {
295 		clock = of_clk_get(of_node, i);
296 		if (IS_ERR(clock)) {
297 			ret = PTR_ERR(clock);
298 			if (ret == -EPROBE_DEFER)
299 				goto err;
300 			drm_err(dev, "clock %u not found: %d\n", i, ret);
301 			continue;
302 		}
303 		ret = clk_prepare_enable(clock);
304 		if (ret) {
305 			drm_err(dev, "failed to enable clock %u: %d\n",
306 				i, ret);
307 			clk_put(clock);
308 			continue;
309 		}
310 		sdev->clks[i] = clock;
311 	}
312 
313 	return devm_add_action_or_reset(&pdev->dev,
314 					simpledrm_device_release_clocks,
315 					sdev);
316 
317 err:
318 	while (i) {
319 		--i;
320 		if (sdev->clks[i]) {
321 			clk_disable_unprepare(sdev->clks[i]);
322 			clk_put(sdev->clks[i]);
323 		}
324 	}
325 	return ret;
326 }
327 #else
328 static int simpledrm_device_init_clocks(struct simpledrm_device *sdev)
329 {
330 	return 0;
331 }
332 #endif
333 
334 #if defined CONFIG_OF && defined CONFIG_REGULATOR
335 
336 #define SUPPLY_SUFFIX "-supply"
337 
338 /*
339  * Regulator handling code.
340  *
341  * Here we handle the num-supplies and vin*-supply properties of our
342  * "simple-framebuffer" dt node. This is necessary so that we can make sure
343  * that any regulators needed by the display hardware that the bootloader
344  * set up for us (and for which it provided a simplefb dt node), stay up,
345  * for the life of the simplefb driver.
346  *
347  * When the driver unloads, we cleanly disable, and then release the
348  * regulators.
349  *
350  * We only complain about errors here, no action is taken as the most likely
351  * error can only happen due to a mismatch between the bootloader which set
352  * up simplefb, and the regulator definitions in the device tree. Chances are
353  * that there are no adverse effects, and if there are, a clean teardown of
354  * the fb probe will not help us much either. So just complain and carry on,
355  * and hope that the user actually gets a working fb at the end of things.
356  */
357 
358 static void simpledrm_device_release_regulators(void *res)
359 {
360 	struct simpledrm_device *sdev = simpledrm_device_of_dev(res);
361 	unsigned int i;
362 
363 	for (i = 0; i < sdev->regulator_count; ++i) {
364 		if (sdev->regulators[i]) {
365 			regulator_disable(sdev->regulators[i]);
366 			regulator_put(sdev->regulators[i]);
367 		}
368 	}
369 }
370 
371 static int simpledrm_device_init_regulators(struct simpledrm_device *sdev)
372 {
373 	struct drm_device *dev = &sdev->dev;
374 	struct platform_device *pdev = to_platform_device(dev->dev);
375 	struct device_node *of_node = pdev->dev.of_node;
376 	struct property *prop;
377 	struct regulator *regulator;
378 	const char *p;
379 	unsigned int count = 0, i = 0;
380 	int ret;
381 
382 	if (dev_get_platdata(&pdev->dev) || !of_node)
383 		return 0;
384 
385 	/* Count the number of regulator supplies */
386 	for_each_property_of_node(of_node, prop) {
387 		p = strstr(prop->name, SUPPLY_SUFFIX);
388 		if (p && p != prop->name)
389 			++count;
390 	}
391 
392 	if (!count)
393 		return 0;
394 
395 	sdev->regulators = drmm_kzalloc(dev,
396 					count * sizeof(sdev->regulators[0]),
397 					GFP_KERNEL);
398 	if (!sdev->regulators)
399 		return -ENOMEM;
400 
401 	for_each_property_of_node(of_node, prop) {
402 		char name[32]; /* 32 is max size of property name */
403 		size_t len;
404 
405 		p = strstr(prop->name, SUPPLY_SUFFIX);
406 		if (!p || p == prop->name)
407 			continue;
408 		len = strlen(prop->name) - strlen(SUPPLY_SUFFIX) + 1;
409 		strscpy(name, prop->name, min(sizeof(name), len));
410 
411 		regulator = regulator_get_optional(&pdev->dev, name);
412 		if (IS_ERR(regulator)) {
413 			ret = PTR_ERR(regulator);
414 			if (ret == -EPROBE_DEFER)
415 				goto err;
416 			drm_err(dev, "regulator %s not found: %d\n",
417 				name, ret);
418 			continue;
419 		}
420 
421 		ret = regulator_enable(regulator);
422 		if (ret) {
423 			drm_err(dev, "failed to enable regulator %u: %d\n",
424 				i, ret);
425 			regulator_put(regulator);
426 			continue;
427 		}
428 
429 		sdev->regulators[i++] = regulator;
430 	}
431 	sdev->regulator_count = i;
432 
433 	return devm_add_action_or_reset(&pdev->dev,
434 					simpledrm_device_release_regulators,
435 					sdev);
436 
437 err:
438 	while (i) {
439 		--i;
440 		if (sdev->regulators[i]) {
441 			regulator_disable(sdev->regulators[i]);
442 			regulator_put(sdev->regulators[i]);
443 		}
444 	}
445 	return ret;
446 }
447 #else
448 static int simpledrm_device_init_regulators(struct simpledrm_device *sdev)
449 {
450 	return 0;
451 }
452 #endif
453 
454 /*
455  * Modesetting
456  */
457 
458 /*
459  * Support all formats of simplefb and maybe more; in order
460  * of preference. The display's update function will do any
461  * conversion necessary.
462  *
463  * TODO: Add blit helpers for remaining formats and uncomment
464  *       constants.
465  */
466 static const uint32_t simpledrm_primary_plane_formats[] = {
467 	DRM_FORMAT_XRGB8888,
468 	DRM_FORMAT_ARGB8888,
469 	DRM_FORMAT_RGB565,
470 	//DRM_FORMAT_XRGB1555,
471 	//DRM_FORMAT_ARGB1555,
472 	DRM_FORMAT_RGB888,
473 	DRM_FORMAT_XRGB2101010,
474 	DRM_FORMAT_ARGB2101010,
475 };
476 
477 static const uint64_t simpledrm_primary_plane_format_modifiers[] = {
478 	DRM_FORMAT_MOD_LINEAR,
479 	DRM_FORMAT_MOD_INVALID
480 };
481 
482 static int simpledrm_primary_plane_helper_atomic_check(struct drm_plane *plane,
483 						       struct drm_atomic_state *new_state)
484 {
485 	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(new_state, plane);
486 	struct drm_crtc *new_crtc = new_plane_state->crtc;
487 	struct drm_crtc_state *new_crtc_state = NULL;
488 	int ret;
489 
490 	if (new_crtc)
491 		new_crtc_state = drm_atomic_get_new_crtc_state(new_state, new_crtc);
492 
493 	ret = drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state,
494 						  DRM_PLANE_NO_SCALING,
495 						  DRM_PLANE_NO_SCALING,
496 						  false, false);
497 	if (ret)
498 		return ret;
499 	else if (!new_plane_state->visible)
500 		return 0;
501 
502 	return 0;
503 }
504 
505 static void simpledrm_primary_plane_helper_atomic_update(struct drm_plane *plane,
506 							 struct drm_atomic_state *old_state)
507 {
508 	struct drm_plane_state *plane_state = plane->state;
509 	struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(old_state, plane);
510 	struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
511 	struct drm_framebuffer *fb = plane_state->fb;
512 	struct drm_device *dev = plane->dev;
513 	struct simpledrm_device *sdev = simpledrm_device_of_dev(dev);
514 	struct iosys_map dst = IOSYS_MAP_INIT_VADDR(sdev->screen_base);
515 	struct drm_rect src_clip, dst_clip;
516 	int idx;
517 
518 	if (!fb)
519 		return;
520 
521 	if (!drm_atomic_helper_damage_merged(old_plane_state, plane_state, &src_clip))
522 		return;
523 
524 	dst_clip = plane_state->dst;
525 	if (!drm_rect_intersect(&dst_clip, &src_clip))
526 		return;
527 
528 	if (!drm_dev_enter(dev, &idx))
529 		return;
530 
531 	iosys_map_incr(&dst, drm_fb_clip_offset(sdev->pitch, sdev->format, &dst_clip));
532 	drm_fb_blit(&dst, &sdev->pitch, sdev->format->format, shadow_plane_state->data, fb,
533 		    &src_clip);
534 
535 	drm_dev_exit(idx);
536 }
537 
538 static void simpledrm_primary_plane_helper_atomic_disable(struct drm_plane *plane,
539 							  struct drm_atomic_state *old_state)
540 {
541 	struct drm_device *dev = plane->dev;
542 	struct simpledrm_device *sdev = simpledrm_device_of_dev(dev);
543 	int idx;
544 
545 	if (!drm_dev_enter(dev, &idx))
546 		return;
547 
548 	/* Clear screen to black if disabled */
549 	memset_io(sdev->screen_base, 0, sdev->pitch * sdev->mode.vdisplay);
550 
551 	drm_dev_exit(idx);
552 }
553 
554 static const struct drm_plane_helper_funcs simpledrm_primary_plane_helper_funcs = {
555 	DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
556 	.atomic_check = simpledrm_primary_plane_helper_atomic_check,
557 	.atomic_update = simpledrm_primary_plane_helper_atomic_update,
558 	.atomic_disable = simpledrm_primary_plane_helper_atomic_disable,
559 };
560 
561 static const struct drm_plane_funcs simpledrm_primary_plane_funcs = {
562 	.update_plane = drm_atomic_helper_update_plane,
563 	.disable_plane = drm_atomic_helper_disable_plane,
564 	.destroy = drm_plane_cleanup,
565 	DRM_GEM_SHADOW_PLANE_FUNCS,
566 };
567 
568 static enum drm_mode_status simpledrm_crtc_helper_mode_valid(struct drm_crtc *crtc,
569 							     const struct drm_display_mode *mode)
570 {
571 	struct simpledrm_device *sdev = simpledrm_device_of_dev(crtc->dev);
572 
573 	if (mode->hdisplay != sdev->mode.hdisplay &&
574 	    mode->vdisplay != sdev->mode.vdisplay)
575 		return MODE_ONE_SIZE;
576 	else if (mode->hdisplay != sdev->mode.hdisplay)
577 		return MODE_ONE_WIDTH;
578 	else if (mode->vdisplay != sdev->mode.vdisplay)
579 		return MODE_ONE_HEIGHT;
580 
581 	return MODE_OK;
582 }
583 
584 static int simpledrm_crtc_helper_atomic_check(struct drm_crtc *crtc,
585 					      struct drm_atomic_state *new_state)
586 {
587 	struct drm_crtc_state *new_crtc_state = drm_atomic_get_new_crtc_state(new_state, crtc);
588 	int ret;
589 
590 	ret = drm_atomic_helper_check_crtc_state(new_crtc_state, false);
591 	if (ret)
592 		return ret;
593 
594 	return drm_atomic_add_affected_planes(new_state, crtc);
595 }
596 
597 /*
598  * The CRTC is always enabled. Screen updates are performed by
599  * the primary plane's atomic_update function. Disabling clears
600  * the screen in the primary plane's atomic_disable function.
601  */
602 static const struct drm_crtc_helper_funcs simpledrm_crtc_helper_funcs = {
603 	.mode_valid = simpledrm_crtc_helper_mode_valid,
604 	.atomic_check = simpledrm_crtc_helper_atomic_check,
605 };
606 
607 static const struct drm_crtc_funcs simpledrm_crtc_funcs = {
608 	.reset = drm_atomic_helper_crtc_reset,
609 	.destroy = drm_crtc_cleanup,
610 	.set_config = drm_atomic_helper_set_config,
611 	.page_flip = drm_atomic_helper_page_flip,
612 	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
613 	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
614 };
615 
616 static const struct drm_encoder_funcs simpledrm_encoder_funcs = {
617 	.destroy = drm_encoder_cleanup,
618 };
619 
620 static int simpledrm_connector_helper_get_modes(struct drm_connector *connector)
621 {
622 	struct simpledrm_device *sdev = simpledrm_device_of_dev(connector->dev);
623 	struct drm_display_mode *mode;
624 
625 	mode = drm_mode_duplicate(connector->dev, &sdev->mode);
626 	if (!mode)
627 		return 0;
628 
629 	if (mode->name[0] == '\0')
630 		drm_mode_set_name(mode);
631 
632 	mode->type |= DRM_MODE_TYPE_PREFERRED;
633 	drm_mode_probed_add(connector, mode);
634 
635 	if (mode->width_mm)
636 		connector->display_info.width_mm = mode->width_mm;
637 	if (mode->height_mm)
638 		connector->display_info.height_mm = mode->height_mm;
639 
640 	return 1;
641 }
642 
643 static const struct drm_connector_helper_funcs simpledrm_connector_helper_funcs = {
644 	.get_modes = simpledrm_connector_helper_get_modes,
645 };
646 
647 static const struct drm_connector_funcs simpledrm_connector_funcs = {
648 	.reset = drm_atomic_helper_connector_reset,
649 	.fill_modes = drm_helper_probe_single_connector_modes,
650 	.destroy = drm_connector_cleanup,
651 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
652 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
653 };
654 
655 static const struct drm_mode_config_funcs simpledrm_mode_config_funcs = {
656 	.fb_create = drm_gem_fb_create_with_dirty,
657 	.atomic_check = drm_atomic_helper_check,
658 	.atomic_commit = drm_atomic_helper_commit,
659 };
660 
661 /*
662  * Init / Cleanup
663  */
664 
665 static struct drm_display_mode simpledrm_mode(unsigned int width,
666 					      unsigned int height)
667 {
668 	struct drm_display_mode mode = { SIMPLEDRM_MODE(width, height) };
669 
670 	mode.clock = mode.hdisplay * mode.vdisplay * 60 / 1000 /* kHz */;
671 	drm_mode_set_name(&mode);
672 
673 	return mode;
674 }
675 
676 static const uint32_t *simpledrm_device_formats(struct simpledrm_device *sdev,
677 						size_t *nformats_out)
678 {
679 	struct drm_device *dev = &sdev->dev;
680 	size_t i;
681 
682 	if (sdev->nformats)
683 		goto out; /* don't rebuild list on recurring calls */
684 
685 	/* native format goes first */
686 	sdev->formats[0] = sdev->format->format;
687 	sdev->nformats = 1;
688 
689 	/* default formats go second */
690 	for (i = 0; i < ARRAY_SIZE(simpledrm_primary_plane_formats); ++i) {
691 		if (simpledrm_primary_plane_formats[i] == sdev->format->format)
692 			continue; /* native format already went first */
693 		sdev->formats[sdev->nformats] = simpledrm_primary_plane_formats[i];
694 		sdev->nformats++;
695 	}
696 
697 	/*
698 	 * TODO: The simpledrm driver converts framebuffers to the native
699 	 * format when copying them to device memory. If there are more
700 	 * formats listed than supported by the driver, the native format
701 	 * is not supported by the conversion helpers. Therefore *only*
702 	 * support the native format and add a conversion helper ASAP.
703 	 */
704 	if (drm_WARN_ONCE(dev, i != sdev->nformats,
705 			  "format conversion helpers required for %p4cc",
706 			  &sdev->format->format)) {
707 		sdev->nformats = 1;
708 	}
709 
710 out:
711 	*nformats_out = sdev->nformats;
712 	return sdev->formats;
713 }
714 
715 static struct simpledrm_device *simpledrm_device_create(struct drm_driver *drv,
716 							struct platform_device *pdev)
717 {
718 	const struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev);
719 	struct device_node *of_node = pdev->dev.of_node;
720 	struct simpledrm_device *sdev;
721 	struct drm_device *dev;
722 	int width, height, stride;
723 	const struct drm_format_info *format;
724 	struct resource *res, *mem;
725 	void __iomem *screen_base;
726 	struct drm_plane *primary_plane;
727 	struct drm_crtc *crtc;
728 	struct drm_encoder *encoder;
729 	struct drm_connector *connector;
730 	unsigned long max_width, max_height;
731 	const uint32_t *formats;
732 	size_t nformats;
733 	int ret;
734 
735 	sdev = devm_drm_dev_alloc(&pdev->dev, drv, struct simpledrm_device, dev);
736 	if (IS_ERR(sdev))
737 		return ERR_CAST(sdev);
738 	dev = &sdev->dev;
739 	platform_set_drvdata(pdev, sdev);
740 
741 	/*
742 	 * Hardware settings
743 	 */
744 
745 	ret = simpledrm_device_init_clocks(sdev);
746 	if (ret)
747 		return ERR_PTR(ret);
748 	ret = simpledrm_device_init_regulators(sdev);
749 	if (ret)
750 		return ERR_PTR(ret);
751 
752 	if (pd) {
753 		width = simplefb_get_width_pd(dev, pd);
754 		if (width < 0)
755 			return ERR_PTR(width);
756 		height = simplefb_get_height_pd(dev, pd);
757 		if (height < 0)
758 			return ERR_PTR(height);
759 		stride = simplefb_get_stride_pd(dev, pd);
760 		if (stride < 0)
761 			return ERR_PTR(stride);
762 		format = simplefb_get_format_pd(dev, pd);
763 		if (IS_ERR(format))
764 			return ERR_CAST(format);
765 	} else if (of_node) {
766 		width = simplefb_get_width_of(dev, of_node);
767 		if (width < 0)
768 			return ERR_PTR(width);
769 		height = simplefb_get_height_of(dev, of_node);
770 		if (height < 0)
771 			return ERR_PTR(height);
772 		stride = simplefb_get_stride_of(dev, of_node);
773 		if (stride < 0)
774 			return ERR_PTR(stride);
775 		format = simplefb_get_format_of(dev, of_node);
776 		if (IS_ERR(format))
777 			return ERR_CAST(format);
778 	} else {
779 		drm_err(dev, "no simplefb configuration found\n");
780 		return ERR_PTR(-ENODEV);
781 	}
782 	if (!stride)
783 		stride = DIV_ROUND_UP(drm_format_info_bpp(format, 0) * width, 8);
784 
785 	sdev->mode = simpledrm_mode(width, height);
786 	sdev->format = format;
787 	sdev->pitch = stride;
788 
789 	drm_dbg(dev, "display mode={" DRM_MODE_FMT "}\n", DRM_MODE_ARG(&sdev->mode));
790 	drm_dbg(dev, "framebuffer format=%p4cc, size=%dx%d, stride=%d byte\n",
791 		&format->format, width, height, stride);
792 
793 	/*
794 	 * Memory management
795 	 */
796 
797 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
798 	if (!res)
799 		return ERR_PTR(-EINVAL);
800 
801 	ret = devm_aperture_acquire_from_firmware(dev, res->start, resource_size(res));
802 	if (ret) {
803 		drm_err(dev, "could not acquire memory range %pr: error %d\n", res, ret);
804 		return ERR_PTR(ret);
805 	}
806 
807 	mem = devm_request_mem_region(&pdev->dev, res->start, resource_size(res), drv->name);
808 	if (!mem) {
809 		/*
810 		 * We cannot make this fatal. Sometimes this comes from magic
811 		 * spaces our resource handlers simply don't know about. Use
812 		 * the I/O-memory resource as-is and try to map that instead.
813 		 */
814 		drm_warn(dev, "could not acquire memory region %pr\n", res);
815 		mem = res;
816 	}
817 
818 	screen_base = devm_ioremap_wc(&pdev->dev, mem->start, resource_size(mem));
819 	if (!screen_base)
820 		return ERR_PTR(-ENOMEM);
821 	sdev->screen_base = screen_base;
822 
823 	/*
824 	 * Modesetting
825 	 */
826 
827 	ret = drmm_mode_config_init(dev);
828 	if (ret)
829 		return ERR_PTR(ret);
830 
831 	max_width = max_t(unsigned long, width, DRM_SHADOW_PLANE_MAX_WIDTH);
832 	max_height = max_t(unsigned long, height, DRM_SHADOW_PLANE_MAX_HEIGHT);
833 
834 	dev->mode_config.min_width = width;
835 	dev->mode_config.max_width = max_width;
836 	dev->mode_config.min_height = height;
837 	dev->mode_config.max_height = max_height;
838 	dev->mode_config.preferred_depth = format->cpp[0] * 8;
839 	dev->mode_config.funcs = &simpledrm_mode_config_funcs;
840 
841 	/* Primary plane */
842 
843 	formats = simpledrm_device_formats(sdev, &nformats);
844 
845 	primary_plane = &sdev->primary_plane;
846 	ret = drm_universal_plane_init(dev, primary_plane, 0, &simpledrm_primary_plane_funcs,
847 				       formats, nformats,
848 				       simpledrm_primary_plane_format_modifiers,
849 				       DRM_PLANE_TYPE_PRIMARY, NULL);
850 	if (ret)
851 		return ERR_PTR(ret);
852 	drm_plane_helper_add(primary_plane, &simpledrm_primary_plane_helper_funcs);
853 	drm_plane_enable_fb_damage_clips(primary_plane);
854 
855 	/* CRTC */
856 
857 	crtc = &sdev->crtc;
858 	ret = drm_crtc_init_with_planes(dev, crtc, primary_plane, NULL,
859 					&simpledrm_crtc_funcs, NULL);
860 	if (ret)
861 		return ERR_PTR(ret);
862 	drm_crtc_helper_add(crtc, &simpledrm_crtc_helper_funcs);
863 
864 	/* Encoder */
865 
866 	encoder = &sdev->encoder;
867 	ret = drm_encoder_init(dev, encoder, &simpledrm_encoder_funcs,
868 			       DRM_MODE_ENCODER_NONE, NULL);
869 	if (ret)
870 		return ERR_PTR(ret);
871 	encoder->possible_crtcs = drm_crtc_mask(crtc);
872 
873 	/* Connector */
874 
875 	connector = &sdev->connector;
876 	ret = drm_connector_init(dev, connector, &simpledrm_connector_funcs,
877 				 DRM_MODE_CONNECTOR_Unknown);
878 	if (ret)
879 		return ERR_PTR(ret);
880 	drm_connector_helper_add(connector, &simpledrm_connector_helper_funcs);
881 	drm_connector_set_panel_orientation_with_quirk(connector,
882 						       DRM_MODE_PANEL_ORIENTATION_UNKNOWN,
883 						       width, height);
884 
885 	ret = drm_connector_attach_encoder(connector, encoder);
886 	if (ret)
887 		return ERR_PTR(ret);
888 
889 	drm_mode_config_reset(dev);
890 
891 	return sdev;
892 }
893 
894 /*
895  * DRM driver
896  */
897 
898 DEFINE_DRM_GEM_FOPS(simpledrm_fops);
899 
900 static struct drm_driver simpledrm_driver = {
901 	DRM_GEM_SHMEM_DRIVER_OPS,
902 	.name			= DRIVER_NAME,
903 	.desc			= DRIVER_DESC,
904 	.date			= DRIVER_DATE,
905 	.major			= DRIVER_MAJOR,
906 	.minor			= DRIVER_MINOR,
907 	.driver_features	= DRIVER_ATOMIC | DRIVER_GEM | DRIVER_MODESET,
908 	.fops			= &simpledrm_fops,
909 };
910 
911 /*
912  * Platform driver
913  */
914 
915 static int simpledrm_probe(struct platform_device *pdev)
916 {
917 	struct simpledrm_device *sdev;
918 	struct drm_device *dev;
919 	int ret;
920 
921 	sdev = simpledrm_device_create(&simpledrm_driver, pdev);
922 	if (IS_ERR(sdev))
923 		return PTR_ERR(sdev);
924 	dev = &sdev->dev;
925 
926 	ret = drm_dev_register(dev, 0);
927 	if (ret)
928 		return ret;
929 
930 	drm_fbdev_generic_setup(dev, 0);
931 
932 	return 0;
933 }
934 
935 static int simpledrm_remove(struct platform_device *pdev)
936 {
937 	struct simpledrm_device *sdev = platform_get_drvdata(pdev);
938 	struct drm_device *dev = &sdev->dev;
939 
940 	drm_dev_unplug(dev);
941 
942 	return 0;
943 }
944 
945 static const struct of_device_id simpledrm_of_match_table[] = {
946 	{ .compatible = "simple-framebuffer", },
947 	{ },
948 };
949 MODULE_DEVICE_TABLE(of, simpledrm_of_match_table);
950 
951 static struct platform_driver simpledrm_platform_driver = {
952 	.driver = {
953 		.name = "simple-framebuffer", /* connect to sysfb */
954 		.of_match_table = simpledrm_of_match_table,
955 	},
956 	.probe = simpledrm_probe,
957 	.remove = simpledrm_remove,
958 };
959 
960 module_platform_driver(simpledrm_platform_driver);
961 
962 MODULE_DESCRIPTION(DRIVER_DESC);
963 MODULE_LICENSE("GPL v2");
964