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