1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/aperture.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>
13 #include <drm/drm_atomic.h>
14 #include <drm/drm_atomic_state_helper.h>
15 #include <drm/drm_client_setup.h>
16 #include <drm/drm_connector.h>
17 #include <drm/drm_crtc_helper.h>
18 #include <drm/drm_damage_helper.h>
19 #include <drm/drm_device.h>
20 #include <drm/drm_drv.h>
21 #include <drm/drm_fbdev_shmem.h>
22 #include <drm/drm_format_helper.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_vtables.h>
29 #include <drm/drm_panic.h>
30 #include <drm/drm_probe_helper.h>
32 #define DRIVER_NAME "simpledrm"
33 #define DRIVER_DESC "DRM driver for simple-framebuffer platform devices"
34 #define DRIVER_DATE "20200625"
35 #define DRIVER_MAJOR 1
36 #define DRIVER_MINOR 0
39 * Helpers for simplefb
43 simplefb_get_validated_int(struct drm_device *dev, const char *name,
46 if (value > INT_MAX) {
47 drm_err(dev, "simplefb: invalid framebuffer %s of %u\n",
55 simplefb_get_validated_int0(struct drm_device *dev, const char *name,
59 drm_err(dev, "simplefb: invalid framebuffer %s of %u\n",
63 return simplefb_get_validated_int(dev, name, value);
66 static const struct drm_format_info *
67 simplefb_get_validated_format(struct drm_device *dev, const char *format_name)
69 static const struct simplefb_format formats[] = SIMPLEFB_FORMATS;
70 const struct simplefb_format *fmt = formats;
71 const struct simplefb_format *end = fmt + ARRAY_SIZE(formats);
72 const struct drm_format_info *info;
75 drm_err(dev, "simplefb: missing framebuffer format\n");
76 return ERR_PTR(-EINVAL);
80 if (!strcmp(format_name, fmt->name)) {
81 info = drm_format_info(fmt->fourcc);
83 return ERR_PTR(-EINVAL);
89 drm_err(dev, "simplefb: unknown framebuffer format %s\n",
92 return ERR_PTR(-EINVAL);
96 simplefb_get_width_pd(struct drm_device *dev,
97 const struct simplefb_platform_data *pd)
99 return simplefb_get_validated_int0(dev, "width", pd->width);
103 simplefb_get_height_pd(struct drm_device *dev,
104 const struct simplefb_platform_data *pd)
106 return simplefb_get_validated_int0(dev, "height", pd->height);
110 simplefb_get_stride_pd(struct drm_device *dev,
111 const struct simplefb_platform_data *pd)
113 return simplefb_get_validated_int(dev, "stride", pd->stride);
116 static const struct drm_format_info *
117 simplefb_get_format_pd(struct drm_device *dev,
118 const struct simplefb_platform_data *pd)
120 return simplefb_get_validated_format(dev, pd->format);
124 simplefb_read_u32_of(struct drm_device *dev, struct device_node *of_node,
125 const char *name, u32 *value)
127 int ret = of_property_read_u32(of_node, name, value);
130 drm_err(dev, "simplefb: cannot parse framebuffer %s: error %d\n",
136 simplefb_read_string_of(struct drm_device *dev, struct device_node *of_node,
137 const char *name, const char **value)
139 int ret = of_property_read_string(of_node, name, value);
142 drm_err(dev, "simplefb: cannot parse framebuffer %s: error %d\n",
148 simplefb_get_width_of(struct drm_device *dev, struct device_node *of_node)
151 int ret = simplefb_read_u32_of(dev, of_node, "width", &width);
155 return simplefb_get_validated_int0(dev, "width", width);
159 simplefb_get_height_of(struct drm_device *dev, struct device_node *of_node)
162 int ret = simplefb_read_u32_of(dev, of_node, "height", &height);
166 return simplefb_get_validated_int0(dev, "height", height);
170 simplefb_get_stride_of(struct drm_device *dev, struct device_node *of_node)
173 int ret = simplefb_read_u32_of(dev, of_node, "stride", &stride);
177 return simplefb_get_validated_int(dev, "stride", stride);
180 static const struct drm_format_info *
181 simplefb_get_format_of(struct drm_device *dev, struct device_node *of_node)
184 int ret = simplefb_read_string_of(dev, of_node, "format", &format);
188 return simplefb_get_validated_format(dev, format);
191 static struct resource *
192 simplefb_get_memory_of(struct drm_device *dev, struct device_node *of_node)
194 struct device_node *np;
195 struct resource *res;
198 np = of_parse_phandle(of_node, "memory-region", 0);
202 res = devm_kzalloc(dev->dev, sizeof(*res), GFP_KERNEL);
204 return ERR_PTR(-ENOMEM);
206 err = of_address_to_resource(np, 0, res);
210 if (of_property_present(of_node, "reg"))
211 drm_warn(dev, "preferring \"memory-region\" over \"reg\" property\n");
217 * Simple Framebuffer device
220 struct simpledrm_device {
221 struct drm_device dev;
224 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
225 unsigned int clk_count;
229 #if defined CONFIG_OF && defined CONFIG_REGULATOR
230 unsigned int regulator_count;
231 struct regulator **regulators;
234 #if defined CONFIG_OF && defined CONFIG_PM_GENERIC_DOMAINS
236 struct device **pwr_dom_devs;
237 struct device_link **pwr_dom_links;
240 /* simplefb settings */
241 struct drm_display_mode mode;
242 const struct drm_format_info *format;
245 /* memory management */
246 struct iosys_map screen_base;
251 struct drm_plane primary_plane;
252 struct drm_crtc crtc;
253 struct drm_encoder encoder;
254 struct drm_connector connector;
257 static struct simpledrm_device *simpledrm_device_of_dev(struct drm_device *dev)
259 return container_of(dev, struct simpledrm_device, dev);
266 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
268 * Clock handling code.
270 * Here we handle the clocks property of our "simple-framebuffer" dt node.
271 * This is necessary so that we can make sure that any clocks needed by
272 * the display engine that the bootloader set up for us (and for which it
273 * provided a simplefb dt node), stay up, for the life of the simplefb
276 * When the driver unloads, we cleanly disable, and then release the clocks.
278 * We only complain about errors here, no action is taken as the most likely
279 * error can only happen due to a mismatch between the bootloader which set
280 * up simplefb, and the clock definitions in the device tree. Chances are
281 * that there are no adverse effects, and if there are, a clean teardown of
282 * the fb probe will not help us much either. So just complain and carry on,
283 * and hope that the user actually gets a working fb at the end of things.
286 static void simpledrm_device_release_clocks(void *res)
288 struct simpledrm_device *sdev = simpledrm_device_of_dev(res);
291 for (i = 0; i < sdev->clk_count; ++i) {
293 clk_disable_unprepare(sdev->clks[i]);
294 clk_put(sdev->clks[i]);
299 static int simpledrm_device_init_clocks(struct simpledrm_device *sdev)
301 struct drm_device *dev = &sdev->dev;
302 struct platform_device *pdev = to_platform_device(dev->dev);
303 struct device_node *of_node = pdev->dev.of_node;
308 if (dev_get_platdata(&pdev->dev) || !of_node)
311 sdev->clk_count = of_clk_get_parent_count(of_node);
312 if (!sdev->clk_count)
315 sdev->clks = drmm_kzalloc(dev, sdev->clk_count * sizeof(sdev->clks[0]),
320 for (i = 0; i < sdev->clk_count; ++i) {
321 clock = of_clk_get(of_node, i);
323 ret = PTR_ERR(clock);
324 if (ret == -EPROBE_DEFER)
326 drm_err(dev, "clock %u not found: %d\n", i, ret);
329 ret = clk_prepare_enable(clock);
331 drm_err(dev, "failed to enable clock %u: %d\n",
336 sdev->clks[i] = clock;
339 return devm_add_action_or_reset(&pdev->dev,
340 simpledrm_device_release_clocks,
347 clk_disable_unprepare(sdev->clks[i]);
348 clk_put(sdev->clks[i]);
354 static int simpledrm_device_init_clocks(struct simpledrm_device *sdev)
360 #if defined CONFIG_OF && defined CONFIG_REGULATOR
362 #define SUPPLY_SUFFIX "-supply"
365 * Regulator handling code.
367 * Here we handle the num-supplies and vin*-supply properties of our
368 * "simple-framebuffer" dt node. This is necessary so that we can make sure
369 * that any regulators needed by the display hardware that the bootloader
370 * set up for us (and for which it provided a simplefb dt node), stay up,
371 * for the life of the simplefb driver.
373 * When the driver unloads, we cleanly disable, and then release the
376 * We only complain about errors here, no action is taken as the most likely
377 * error can only happen due to a mismatch between the bootloader which set
378 * up simplefb, and the regulator definitions in the device tree. Chances are
379 * that there are no adverse effects, and if there are, a clean teardown of
380 * the fb probe will not help us much either. So just complain and carry on,
381 * and hope that the user actually gets a working fb at the end of things.
384 static void simpledrm_device_release_regulators(void *res)
386 struct simpledrm_device *sdev = simpledrm_device_of_dev(res);
389 for (i = 0; i < sdev->regulator_count; ++i) {
390 if (sdev->regulators[i]) {
391 regulator_disable(sdev->regulators[i]);
392 regulator_put(sdev->regulators[i]);
397 static int simpledrm_device_init_regulators(struct simpledrm_device *sdev)
399 struct drm_device *dev = &sdev->dev;
400 struct platform_device *pdev = to_platform_device(dev->dev);
401 struct device_node *of_node = pdev->dev.of_node;
402 struct property *prop;
403 struct regulator *regulator;
405 unsigned int count = 0, i = 0;
408 if (dev_get_platdata(&pdev->dev) || !of_node)
411 /* Count the number of regulator supplies */
412 for_each_property_of_node(of_node, prop) {
413 p = strstr(prop->name, SUPPLY_SUFFIX);
414 if (p && p != prop->name)
421 sdev->regulators = drmm_kzalloc(dev,
422 count * sizeof(sdev->regulators[0]),
424 if (!sdev->regulators)
427 for_each_property_of_node(of_node, prop) {
428 char name[32]; /* 32 is max size of property name */
431 p = strstr(prop->name, SUPPLY_SUFFIX);
432 if (!p || p == prop->name)
434 len = strlen(prop->name) - strlen(SUPPLY_SUFFIX) + 1;
435 strscpy(name, prop->name, min(sizeof(name), len));
437 regulator = regulator_get_optional(&pdev->dev, name);
438 if (IS_ERR(regulator)) {
439 ret = PTR_ERR(regulator);
440 if (ret == -EPROBE_DEFER)
442 drm_err(dev, "regulator %s not found: %d\n",
447 ret = regulator_enable(regulator);
449 drm_err(dev, "failed to enable regulator %u: %d\n",
451 regulator_put(regulator);
455 sdev->regulators[i++] = regulator;
457 sdev->regulator_count = i;
459 return devm_add_action_or_reset(&pdev->dev,
460 simpledrm_device_release_regulators,
466 if (sdev->regulators[i]) {
467 regulator_disable(sdev->regulators[i]);
468 regulator_put(sdev->regulators[i]);
474 static int simpledrm_device_init_regulators(struct simpledrm_device *sdev)
480 #if defined CONFIG_OF && defined CONFIG_PM_GENERIC_DOMAINS
482 * Generic power domain handling code.
484 * Here we handle the power-domains properties of our "simple-framebuffer"
485 * dt node. This is only necessary if there is more than one power-domain.
486 * A single power-domains is handled automatically by the driver core. Multiple
487 * power-domains have to be handled by drivers since the driver core can't know
488 * the correct power sequencing. Power sequencing is not an issue for simpledrm
489 * since the bootloader has put the power domains already in the correct state.
490 * simpledrm has only to ensure they remain active for its lifetime.
492 * When the driver unloads, we detach from the power-domains.
494 * We only complain about errors here, no action is taken as the most likely
495 * error can only happen due to a mismatch between the bootloader which set
496 * up the "simple-framebuffer" dt node, and the PM domain providers in the
497 * device tree. Chances are that there are no adverse effects, and if there are,
498 * a clean teardown of the fb probe will not help us much either. So just
499 * complain and carry on, and hope that the user actually gets a working fb at
502 static void simpledrm_device_detach_genpd(void *res)
505 struct simpledrm_device *sdev = res;
507 if (sdev->pwr_dom_count <= 1)
510 for (i = sdev->pwr_dom_count - 1; i >= 0; i--) {
511 if (sdev->pwr_dom_links[i])
512 device_link_del(sdev->pwr_dom_links[i]);
513 if (!IS_ERR_OR_NULL(sdev->pwr_dom_devs[i]))
514 dev_pm_domain_detach(sdev->pwr_dom_devs[i], true);
518 static int simpledrm_device_attach_genpd(struct simpledrm_device *sdev)
520 struct device *dev = sdev->dev.dev;
523 sdev->pwr_dom_count = of_count_phandle_with_args(dev->of_node, "power-domains",
524 "#power-domain-cells");
526 * Single power-domain devices are handled by driver core nothing to do
527 * here. The same for device nodes without "power-domains" property.
529 if (sdev->pwr_dom_count <= 1)
532 sdev->pwr_dom_devs = devm_kcalloc(dev, sdev->pwr_dom_count,
533 sizeof(*sdev->pwr_dom_devs),
535 if (!sdev->pwr_dom_devs)
538 sdev->pwr_dom_links = devm_kcalloc(dev, sdev->pwr_dom_count,
539 sizeof(*sdev->pwr_dom_links),
541 if (!sdev->pwr_dom_links)
544 for (i = 0; i < sdev->pwr_dom_count; i++) {
545 sdev->pwr_dom_devs[i] = dev_pm_domain_attach_by_id(dev, i);
546 if (IS_ERR(sdev->pwr_dom_devs[i])) {
547 int ret = PTR_ERR(sdev->pwr_dom_devs[i]);
548 if (ret == -EPROBE_DEFER) {
549 simpledrm_device_detach_genpd(sdev);
553 "pm_domain_attach_by_id(%u) failed: %d\n", i, ret);
557 sdev->pwr_dom_links[i] = device_link_add(dev,
558 sdev->pwr_dom_devs[i],
562 if (!sdev->pwr_dom_links[i])
563 drm_warn(&sdev->dev, "failed to link power-domain %d\n", i);
566 return devm_add_action_or_reset(dev, simpledrm_device_detach_genpd, sdev);
569 static int simpledrm_device_attach_genpd(struct simpledrm_device *sdev)
579 static const uint64_t simpledrm_primary_plane_format_modifiers[] = {
580 DRM_FORMAT_MOD_LINEAR,
581 DRM_FORMAT_MOD_INVALID
584 static int simpledrm_primary_plane_helper_atomic_check(struct drm_plane *plane,
585 struct drm_atomic_state *state)
587 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane);
588 struct drm_shadow_plane_state *new_shadow_plane_state =
589 to_drm_shadow_plane_state(new_plane_state);
590 struct drm_framebuffer *new_fb = new_plane_state->fb;
591 struct drm_crtc *new_crtc = new_plane_state->crtc;
592 struct drm_crtc_state *new_crtc_state = NULL;
593 struct drm_device *dev = plane->dev;
594 struct simpledrm_device *sdev = simpledrm_device_of_dev(dev);
598 new_crtc_state = drm_atomic_get_new_crtc_state(state, new_crtc);
600 ret = drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state,
601 DRM_PLANE_NO_SCALING,
602 DRM_PLANE_NO_SCALING,
606 else if (!new_plane_state->visible)
609 if (new_fb->format != sdev->format) {
612 /* format conversion necessary; reserve buffer */
613 buf = drm_format_conv_state_reserve(&new_shadow_plane_state->fmtcnv_state,
614 sdev->pitch, GFP_KERNEL);
622 static void simpledrm_primary_plane_helper_atomic_update(struct drm_plane *plane,
623 struct drm_atomic_state *state)
625 struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
626 struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
627 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
628 struct drm_framebuffer *fb = plane_state->fb;
629 struct drm_device *dev = plane->dev;
630 struct simpledrm_device *sdev = simpledrm_device_of_dev(dev);
631 struct drm_atomic_helper_damage_iter iter;
632 struct drm_rect damage;
635 ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE);
639 if (!drm_dev_enter(dev, &idx))
640 goto out_drm_gem_fb_end_cpu_access;
642 drm_atomic_helper_damage_iter_init(&iter, old_plane_state, plane_state);
643 drm_atomic_for_each_plane_damage(&iter, &damage) {
644 struct drm_rect dst_clip = plane_state->dst;
645 struct iosys_map dst = sdev->screen_base;
647 if (!drm_rect_intersect(&dst_clip, &damage))
650 iosys_map_incr(&dst, drm_fb_clip_offset(sdev->pitch, sdev->format, &dst_clip));
651 drm_fb_blit(&dst, &sdev->pitch, sdev->format->format, shadow_plane_state->data,
652 fb, &damage, &shadow_plane_state->fmtcnv_state);
656 out_drm_gem_fb_end_cpu_access:
657 drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
660 static void simpledrm_primary_plane_helper_atomic_disable(struct drm_plane *plane,
661 struct drm_atomic_state *state)
663 struct drm_device *dev = plane->dev;
664 struct simpledrm_device *sdev = simpledrm_device_of_dev(dev);
667 if (!drm_dev_enter(dev, &idx))
670 /* Clear screen to black if disabled */
671 iosys_map_memset(&sdev->screen_base, 0, 0, sdev->pitch * sdev->mode.vdisplay);
676 static int simpledrm_primary_plane_helper_get_scanout_buffer(struct drm_plane *plane,
677 struct drm_scanout_buffer *sb)
679 struct simpledrm_device *sdev = simpledrm_device_of_dev(plane->dev);
681 sb->width = sdev->mode.hdisplay;
682 sb->height = sdev->mode.vdisplay;
683 sb->format = sdev->format;
684 sb->pitch[0] = sdev->pitch;
685 sb->map[0] = sdev->screen_base;
690 static const struct drm_plane_helper_funcs simpledrm_primary_plane_helper_funcs = {
691 DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
692 .atomic_check = simpledrm_primary_plane_helper_atomic_check,
693 .atomic_update = simpledrm_primary_plane_helper_atomic_update,
694 .atomic_disable = simpledrm_primary_plane_helper_atomic_disable,
695 .get_scanout_buffer = simpledrm_primary_plane_helper_get_scanout_buffer,
698 static const struct drm_plane_funcs simpledrm_primary_plane_funcs = {
699 .update_plane = drm_atomic_helper_update_plane,
700 .disable_plane = drm_atomic_helper_disable_plane,
701 .destroy = drm_plane_cleanup,
702 DRM_GEM_SHADOW_PLANE_FUNCS,
705 static enum drm_mode_status simpledrm_crtc_helper_mode_valid(struct drm_crtc *crtc,
706 const struct drm_display_mode *mode)
708 struct simpledrm_device *sdev = simpledrm_device_of_dev(crtc->dev);
710 return drm_crtc_helper_mode_valid_fixed(crtc, mode, &sdev->mode);
714 * The CRTC is always enabled. Screen updates are performed by
715 * the primary plane's atomic_update function. Disabling clears
716 * the screen in the primary plane's atomic_disable function.
718 static const struct drm_crtc_helper_funcs simpledrm_crtc_helper_funcs = {
719 .mode_valid = simpledrm_crtc_helper_mode_valid,
720 .atomic_check = drm_crtc_helper_atomic_check,
723 static const struct drm_crtc_funcs simpledrm_crtc_funcs = {
724 .reset = drm_atomic_helper_crtc_reset,
725 .destroy = drm_crtc_cleanup,
726 .set_config = drm_atomic_helper_set_config,
727 .page_flip = drm_atomic_helper_page_flip,
728 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
729 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
732 static const struct drm_encoder_funcs simpledrm_encoder_funcs = {
733 .destroy = drm_encoder_cleanup,
736 static int simpledrm_connector_helper_get_modes(struct drm_connector *connector)
738 struct simpledrm_device *sdev = simpledrm_device_of_dev(connector->dev);
740 return drm_connector_helper_get_modes_fixed(connector, &sdev->mode);
743 static const struct drm_connector_helper_funcs simpledrm_connector_helper_funcs = {
744 .get_modes = simpledrm_connector_helper_get_modes,
747 static const struct drm_connector_funcs simpledrm_connector_funcs = {
748 .reset = drm_atomic_helper_connector_reset,
749 .fill_modes = drm_helper_probe_single_connector_modes,
750 .destroy = drm_connector_cleanup,
751 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
752 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
755 static const struct drm_mode_config_funcs simpledrm_mode_config_funcs = {
756 .fb_create = drm_gem_fb_create_with_dirty,
757 .atomic_check = drm_atomic_helper_check,
758 .atomic_commit = drm_atomic_helper_commit,
765 static struct drm_display_mode simpledrm_mode(unsigned int width,
767 unsigned int width_mm,
768 unsigned int height_mm)
770 const struct drm_display_mode mode = {
771 DRM_MODE_INIT(60, width, height, width_mm, height_mm)
777 static struct simpledrm_device *simpledrm_device_create(struct drm_driver *drv,
778 struct platform_device *pdev)
780 const struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev);
781 struct device_node *of_node = pdev->dev.of_node;
782 struct simpledrm_device *sdev;
783 struct drm_device *dev;
784 int width, height, stride;
785 int width_mm = 0, height_mm = 0;
786 struct device_node *panel_node;
787 const struct drm_format_info *format;
788 struct resource *res, *mem = NULL;
789 struct drm_plane *primary_plane;
790 struct drm_crtc *crtc;
791 struct drm_encoder *encoder;
792 struct drm_connector *connector;
793 unsigned long max_width, max_height;
797 sdev = devm_drm_dev_alloc(&pdev->dev, drv, struct simpledrm_device, dev);
799 return ERR_CAST(sdev);
801 platform_set_drvdata(pdev, sdev);
807 ret = simpledrm_device_init_clocks(sdev);
810 ret = simpledrm_device_init_regulators(sdev);
813 ret = simpledrm_device_attach_genpd(sdev);
818 width = simplefb_get_width_pd(dev, pd);
820 return ERR_PTR(width);
821 height = simplefb_get_height_pd(dev, pd);
823 return ERR_PTR(height);
824 stride = simplefb_get_stride_pd(dev, pd);
826 return ERR_PTR(stride);
827 format = simplefb_get_format_pd(dev, pd);
829 return ERR_CAST(format);
830 } else if (of_node) {
831 width = simplefb_get_width_of(dev, of_node);
833 return ERR_PTR(width);
834 height = simplefb_get_height_of(dev, of_node);
836 return ERR_PTR(height);
837 stride = simplefb_get_stride_of(dev, of_node);
839 return ERR_PTR(stride);
840 format = simplefb_get_format_of(dev, of_node);
842 return ERR_CAST(format);
843 mem = simplefb_get_memory_of(dev, of_node);
845 return ERR_CAST(mem);
846 panel_node = of_parse_phandle(of_node, "panel", 0);
848 simplefb_read_u32_of(dev, panel_node, "width-mm", &width_mm);
849 simplefb_read_u32_of(dev, panel_node, "height-mm", &height_mm);
850 of_node_put(panel_node);
853 drm_err(dev, "no simplefb configuration found\n");
854 return ERR_PTR(-ENODEV);
857 stride = drm_format_info_min_pitch(format, 0, width);
858 if (drm_WARN_ON(dev, !stride))
859 return ERR_PTR(-EINVAL);
863 * Assume a monitor resolution of 96 dpi if physical dimensions
864 * are not specified to get a somewhat reasonable screen size.
867 width_mm = DRM_MODE_RES_MM(width, 96ul);
869 height_mm = DRM_MODE_RES_MM(height, 96ul);
871 sdev->mode = simpledrm_mode(width, height, width_mm, height_mm);
872 sdev->format = format;
873 sdev->pitch = stride;
875 drm_dbg(dev, "display mode={" DRM_MODE_FMT "}\n", DRM_MODE_ARG(&sdev->mode));
876 drm_dbg(dev, "framebuffer format=%p4cc, size=%dx%d, stride=%d byte\n",
877 &format->format, width, height, stride);
886 ret = devm_aperture_acquire_for_platform_device(pdev, mem->start,
889 drm_err(dev, "could not acquire memory range %pr: %d\n", mem, ret);
893 drm_dbg(dev, "using system memory framebuffer at %pr\n", mem);
895 screen_base = devm_memremap(dev->dev, mem->start, resource_size(mem), MEMREMAP_WC);
896 if (IS_ERR(screen_base))
899 iosys_map_set_vaddr(&sdev->screen_base, screen_base);
901 void __iomem *screen_base;
903 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
905 return ERR_PTR(-EINVAL);
907 ret = devm_aperture_acquire_for_platform_device(pdev, res->start,
910 drm_err(dev, "could not acquire memory range %pr: %d\n", res, ret);
914 drm_dbg(dev, "using I/O memory framebuffer at %pr\n", res);
916 mem = devm_request_mem_region(&pdev->dev, res->start, resource_size(res),
920 * We cannot make this fatal. Sometimes this comes from magic
921 * spaces our resource handlers simply don't know about. Use
922 * the I/O-memory resource as-is and try to map that instead.
924 drm_warn(dev, "could not acquire memory region %pr\n", res);
928 screen_base = devm_ioremap_wc(&pdev->dev, mem->start, resource_size(mem));
930 return ERR_PTR(-ENOMEM);
932 iosys_map_set_vaddr_iomem(&sdev->screen_base, screen_base);
939 ret = drmm_mode_config_init(dev);
943 max_width = max_t(unsigned long, width, DRM_SHADOW_PLANE_MAX_WIDTH);
944 max_height = max_t(unsigned long, height, DRM_SHADOW_PLANE_MAX_HEIGHT);
946 dev->mode_config.min_width = width;
947 dev->mode_config.max_width = max_width;
948 dev->mode_config.min_height = height;
949 dev->mode_config.max_height = max_height;
950 dev->mode_config.preferred_depth = format->depth;
951 dev->mode_config.funcs = &simpledrm_mode_config_funcs;
955 nformats = drm_fb_build_fourcc_list(dev, &format->format, 1,
956 sdev->formats, ARRAY_SIZE(sdev->formats));
958 primary_plane = &sdev->primary_plane;
959 ret = drm_universal_plane_init(dev, primary_plane, 0, &simpledrm_primary_plane_funcs,
960 sdev->formats, nformats,
961 simpledrm_primary_plane_format_modifiers,
962 DRM_PLANE_TYPE_PRIMARY, NULL);
965 drm_plane_helper_add(primary_plane, &simpledrm_primary_plane_helper_funcs);
966 drm_plane_enable_fb_damage_clips(primary_plane);
971 ret = drm_crtc_init_with_planes(dev, crtc, primary_plane, NULL,
972 &simpledrm_crtc_funcs, NULL);
975 drm_crtc_helper_add(crtc, &simpledrm_crtc_helper_funcs);
979 encoder = &sdev->encoder;
980 ret = drm_encoder_init(dev, encoder, &simpledrm_encoder_funcs,
981 DRM_MODE_ENCODER_NONE, NULL);
984 encoder->possible_crtcs = drm_crtc_mask(crtc);
988 connector = &sdev->connector;
989 ret = drm_connector_init(dev, connector, &simpledrm_connector_funcs,
990 DRM_MODE_CONNECTOR_Unknown);
993 drm_connector_helper_add(connector, &simpledrm_connector_helper_funcs);
994 drm_connector_set_panel_orientation_with_quirk(connector,
995 DRM_MODE_PANEL_ORIENTATION_UNKNOWN,
998 ret = drm_connector_attach_encoder(connector, encoder);
1000 return ERR_PTR(ret);
1002 drm_mode_config_reset(dev);
1011 DEFINE_DRM_GEM_FOPS(simpledrm_fops);
1013 static struct drm_driver simpledrm_driver = {
1014 DRM_GEM_SHMEM_DRIVER_OPS,
1015 DRM_FBDEV_SHMEM_DRIVER_OPS,
1016 .name = DRIVER_NAME,
1017 .desc = DRIVER_DESC,
1018 .date = DRIVER_DATE,
1019 .major = DRIVER_MAJOR,
1020 .minor = DRIVER_MINOR,
1021 .driver_features = DRIVER_ATOMIC | DRIVER_GEM | DRIVER_MODESET,
1022 .fops = &simpledrm_fops,
1029 static int simpledrm_probe(struct platform_device *pdev)
1031 struct simpledrm_device *sdev;
1032 struct drm_device *dev;
1035 sdev = simpledrm_device_create(&simpledrm_driver, pdev);
1037 return PTR_ERR(sdev);
1040 ret = drm_dev_register(dev, 0);
1044 drm_client_setup(dev, sdev->format);
1049 static void simpledrm_remove(struct platform_device *pdev)
1051 struct simpledrm_device *sdev = platform_get_drvdata(pdev);
1052 struct drm_device *dev = &sdev->dev;
1054 drm_dev_unplug(dev);
1057 static const struct of_device_id simpledrm_of_match_table[] = {
1058 { .compatible = "simple-framebuffer", },
1061 MODULE_DEVICE_TABLE(of, simpledrm_of_match_table);
1063 static struct platform_driver simpledrm_platform_driver = {
1065 .name = "simple-framebuffer", /* connect to sysfb */
1066 .of_match_table = simpledrm_of_match_table,
1068 .probe = simpledrm_probe,
1069 .remove_new = simpledrm_remove,
1072 module_platform_driver(simpledrm_platform_driver);
1074 MODULE_DESCRIPTION(DRIVER_DESC);
1075 MODULE_LICENSE("GPL v2");