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