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