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