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