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