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