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