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/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_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_MAJOR 1
35 #define DRIVER_MINOR 0
38 * Helpers for simplefb
42 simplefb_get_validated_int(struct drm_device *dev, const char *name,
45 if (value > INT_MAX) {
46 drm_err(dev, "simplefb: invalid framebuffer %s of %u\n",
54 simplefb_get_validated_int0(struct drm_device *dev, const char *name,
58 drm_err(dev, "simplefb: invalid framebuffer %s of %u\n",
62 return simplefb_get_validated_int(dev, name, value);
65 static const struct drm_format_info *
66 simplefb_get_validated_format(struct drm_device *dev, const char *format_name)
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;
74 drm_err(dev, "simplefb: missing framebuffer format\n");
75 return ERR_PTR(-EINVAL);
79 if (!strcmp(format_name, fmt->name)) {
80 info = drm_format_info(fmt->fourcc);
82 return ERR_PTR(-EINVAL);
88 drm_err(dev, "simplefb: unknown framebuffer format %s\n",
91 return ERR_PTR(-EINVAL);
95 simplefb_get_width_pd(struct drm_device *dev,
96 const struct simplefb_platform_data *pd)
98 return simplefb_get_validated_int0(dev, "width", pd->width);
102 simplefb_get_height_pd(struct drm_device *dev,
103 const struct simplefb_platform_data *pd)
105 return simplefb_get_validated_int0(dev, "height", pd->height);
109 simplefb_get_stride_pd(struct drm_device *dev,
110 const struct simplefb_platform_data *pd)
112 return simplefb_get_validated_int(dev, "stride", pd->stride);
115 static const struct drm_format_info *
116 simplefb_get_format_pd(struct drm_device *dev,
117 const struct simplefb_platform_data *pd)
119 return simplefb_get_validated_format(dev, pd->format);
123 simplefb_read_u32_of(struct drm_device *dev, struct device_node *of_node,
124 const char *name, u32 *value)
126 int ret = of_property_read_u32(of_node, name, value);
129 drm_err(dev, "simplefb: cannot parse framebuffer %s: error %d\n",
135 simplefb_read_string_of(struct drm_device *dev, struct device_node *of_node,
136 const char *name, const char **value)
138 int ret = of_property_read_string(of_node, name, value);
141 drm_err(dev, "simplefb: cannot parse framebuffer %s: error %d\n",
147 simplefb_get_width_of(struct drm_device *dev, struct device_node *of_node)
150 int ret = simplefb_read_u32_of(dev, of_node, "width", &width);
154 return simplefb_get_validated_int0(dev, "width", width);
158 simplefb_get_height_of(struct drm_device *dev, struct device_node *of_node)
161 int ret = simplefb_read_u32_of(dev, of_node, "height", &height);
165 return simplefb_get_validated_int0(dev, "height", height);
169 simplefb_get_stride_of(struct drm_device *dev, struct device_node *of_node)
172 int ret = simplefb_read_u32_of(dev, of_node, "stride", &stride);
176 return simplefb_get_validated_int(dev, "stride", stride);
179 static const struct drm_format_info *
180 simplefb_get_format_of(struct drm_device *dev, struct device_node *of_node)
183 int ret = simplefb_read_string_of(dev, of_node, "format", &format);
187 return simplefb_get_validated_format(dev, format);
190 static struct resource *
191 simplefb_get_memory_of(struct drm_device *dev, struct device_node *of_node)
193 struct device_node *np;
194 struct resource *res;
197 np = of_parse_phandle(of_node, "memory-region", 0);
201 res = devm_kzalloc(dev->dev, sizeof(*res), GFP_KERNEL);
203 return ERR_PTR(-ENOMEM);
205 err = of_address_to_resource(np, 0, res);
209 if (of_property_present(of_node, "reg"))
210 drm_warn(dev, "preferring \"memory-region\" over \"reg\" property\n");
216 * Simple Framebuffer device
219 struct simpledrm_device {
220 struct drm_device dev;
223 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
224 unsigned int clk_count;
228 #if defined CONFIG_OF && defined CONFIG_REGULATOR
229 unsigned int regulator_count;
230 struct regulator **regulators;
233 #if defined CONFIG_OF && defined CONFIG_PM_GENERIC_DOMAINS
235 struct device **pwr_dom_devs;
236 struct device_link **pwr_dom_links;
239 /* simplefb settings */
240 struct drm_display_mode mode;
241 const struct drm_format_info *format;
244 /* memory management */
245 struct iosys_map screen_base;
250 struct drm_plane primary_plane;
251 struct drm_crtc crtc;
252 struct drm_encoder encoder;
253 struct drm_connector connector;
256 static struct simpledrm_device *simpledrm_device_of_dev(struct drm_device *dev)
258 return container_of(dev, struct simpledrm_device, dev);
265 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
267 * Clock handling code.
269 * Here we handle the clocks property of our "simple-framebuffer" dt node.
270 * This is necessary so that we can make sure that any clocks needed by
271 * the display engine that the bootloader set up for us (and for which it
272 * provided a simplefb dt node), stay up, for the life of the simplefb
275 * When the driver unloads, we cleanly disable, and then release the clocks.
277 * We only complain about errors here, no action is taken as the most likely
278 * error can only happen due to a mismatch between the bootloader which set
279 * up simplefb, and the clock definitions in the device tree. Chances are
280 * that there are no adverse effects, and if there are, a clean teardown of
281 * the fb probe will not help us much either. So just complain and carry on,
282 * and hope that the user actually gets a working fb at the end of things.
285 static void simpledrm_device_release_clocks(void *res)
287 struct simpledrm_device *sdev = simpledrm_device_of_dev(res);
290 for (i = 0; i < sdev->clk_count; ++i) {
292 clk_disable_unprepare(sdev->clks[i]);
293 clk_put(sdev->clks[i]);
298 static int simpledrm_device_init_clocks(struct simpledrm_device *sdev)
300 struct drm_device *dev = &sdev->dev;
301 struct platform_device *pdev = to_platform_device(dev->dev);
302 struct device_node *of_node = pdev->dev.of_node;
307 if (dev_get_platdata(&pdev->dev) || !of_node)
310 sdev->clk_count = of_clk_get_parent_count(of_node);
311 if (!sdev->clk_count)
314 sdev->clks = drmm_kzalloc(dev, sdev->clk_count * sizeof(sdev->clks[0]),
319 for (i = 0; i < sdev->clk_count; ++i) {
320 clock = of_clk_get(of_node, i);
322 ret = PTR_ERR(clock);
323 if (ret == -EPROBE_DEFER)
325 drm_err(dev, "clock %u not found: %d\n", i, ret);
328 ret = clk_prepare_enable(clock);
330 drm_err(dev, "failed to enable clock %u: %d\n",
335 sdev->clks[i] = clock;
338 return devm_add_action_or_reset(&pdev->dev,
339 simpledrm_device_release_clocks,
346 clk_disable_unprepare(sdev->clks[i]);
347 clk_put(sdev->clks[i]);
353 static int simpledrm_device_init_clocks(struct simpledrm_device *sdev)
359 #if defined CONFIG_OF && defined CONFIG_REGULATOR
361 #define SUPPLY_SUFFIX "-supply"
364 * Regulator handling code.
366 * Here we handle the num-supplies and vin*-supply properties of our
367 * "simple-framebuffer" dt node. This is necessary so that we can make sure
368 * that any regulators needed by the display hardware that the bootloader
369 * set up for us (and for which it provided a simplefb dt node), stay up,
370 * for the life of the simplefb driver.
372 * When the driver unloads, we cleanly disable, and then release the
375 * We only complain about errors here, no action is taken as the most likely
376 * error can only happen due to a mismatch between the bootloader which set
377 * up simplefb, and the regulator definitions in the device tree. Chances are
378 * that there are no adverse effects, and if there are, a clean teardown of
379 * the fb probe will not help us much either. So just complain and carry on,
380 * and hope that the user actually gets a working fb at the end of things.
383 static void simpledrm_device_release_regulators(void *res)
385 struct simpledrm_device *sdev = simpledrm_device_of_dev(res);
388 for (i = 0; i < sdev->regulator_count; ++i) {
389 if (sdev->regulators[i]) {
390 regulator_disable(sdev->regulators[i]);
391 regulator_put(sdev->regulators[i]);
396 static int simpledrm_device_init_regulators(struct simpledrm_device *sdev)
398 struct drm_device *dev = &sdev->dev;
399 struct platform_device *pdev = to_platform_device(dev->dev);
400 struct device_node *of_node = pdev->dev.of_node;
401 struct property *prop;
402 struct regulator *regulator;
404 unsigned int count = 0, i = 0;
407 if (dev_get_platdata(&pdev->dev) || !of_node)
410 /* Count the number of regulator supplies */
411 for_each_property_of_node(of_node, prop) {
412 p = strstr(prop->name, SUPPLY_SUFFIX);
413 if (p && p != prop->name)
420 sdev->regulators = drmm_kzalloc(dev,
421 count * sizeof(sdev->regulators[0]),
423 if (!sdev->regulators)
426 for_each_property_of_node(of_node, prop) {
427 char name[32]; /* 32 is max size of property name */
430 p = strstr(prop->name, SUPPLY_SUFFIX);
431 if (!p || p == prop->name)
433 len = strlen(prop->name) - strlen(SUPPLY_SUFFIX) + 1;
434 strscpy(name, prop->name, min(sizeof(name), len));
436 regulator = regulator_get_optional(&pdev->dev, name);
437 if (IS_ERR(regulator)) {
438 ret = PTR_ERR(regulator);
439 if (ret == -EPROBE_DEFER)
441 drm_err(dev, "regulator %s not found: %d\n",
446 ret = regulator_enable(regulator);
448 drm_err(dev, "failed to enable regulator %u: %d\n",
450 regulator_put(regulator);
454 sdev->regulators[i++] = regulator;
456 sdev->regulator_count = i;
458 return devm_add_action_or_reset(&pdev->dev,
459 simpledrm_device_release_regulators,
465 if (sdev->regulators[i]) {
466 regulator_disable(sdev->regulators[i]);
467 regulator_put(sdev->regulators[i]);
473 static int simpledrm_device_init_regulators(struct simpledrm_device *sdev)
479 #if defined CONFIG_OF && defined CONFIG_PM_GENERIC_DOMAINS
481 * Generic power domain handling code.
483 * Here we handle the power-domains properties of our "simple-framebuffer"
484 * dt node. This is only necessary if there is more than one power-domain.
485 * A single power-domains is handled automatically by the driver core. Multiple
486 * power-domains have to be handled by drivers since the driver core can't know
487 * the correct power sequencing. Power sequencing is not an issue for simpledrm
488 * since the bootloader has put the power domains already in the correct state.
489 * simpledrm has only to ensure they remain active for its lifetime.
491 * When the driver unloads, we detach from the power-domains.
493 * We only complain about errors here, no action is taken as the most likely
494 * error can only happen due to a mismatch between the bootloader which set
495 * up the "simple-framebuffer" dt node, and the PM domain providers in the
496 * device tree. Chances are that there are no adverse effects, and if there are,
497 * a clean teardown of the fb probe will not help us much either. So just
498 * complain and carry on, and hope that the user actually gets a working fb at
501 static void simpledrm_device_detach_genpd(void *res)
504 struct simpledrm_device *sdev = res;
506 if (sdev->pwr_dom_count <= 1)
509 for (i = sdev->pwr_dom_count - 1; i >= 0; i--) {
510 if (sdev->pwr_dom_links[i])
511 device_link_del(sdev->pwr_dom_links[i]);
512 if (!IS_ERR_OR_NULL(sdev->pwr_dom_devs[i]))
513 dev_pm_domain_detach(sdev->pwr_dom_devs[i], true);
517 static int simpledrm_device_attach_genpd(struct simpledrm_device *sdev)
519 struct device *dev = sdev->dev.dev;
522 sdev->pwr_dom_count = of_count_phandle_with_args(dev->of_node, "power-domains",
523 "#power-domain-cells");
525 * Single power-domain devices are handled by driver core nothing to do
526 * here. The same for device nodes without "power-domains" property.
528 if (sdev->pwr_dom_count <= 1)
531 sdev->pwr_dom_devs = devm_kcalloc(dev, sdev->pwr_dom_count,
532 sizeof(*sdev->pwr_dom_devs),
534 if (!sdev->pwr_dom_devs)
537 sdev->pwr_dom_links = devm_kcalloc(dev, sdev->pwr_dom_count,
538 sizeof(*sdev->pwr_dom_links),
540 if (!sdev->pwr_dom_links)
543 for (i = 0; i < sdev->pwr_dom_count; i++) {
544 sdev->pwr_dom_devs[i] = dev_pm_domain_attach_by_id(dev, i);
545 if (IS_ERR(sdev->pwr_dom_devs[i])) {
546 int ret = PTR_ERR(sdev->pwr_dom_devs[i]);
547 if (ret == -EPROBE_DEFER) {
548 simpledrm_device_detach_genpd(sdev);
552 "pm_domain_attach_by_id(%u) failed: %d\n", i, ret);
556 sdev->pwr_dom_links[i] = device_link_add(dev,
557 sdev->pwr_dom_devs[i],
561 if (!sdev->pwr_dom_links[i])
562 drm_warn(&sdev->dev, "failed to link power-domain %d\n", i);
565 return devm_add_action_or_reset(dev, simpledrm_device_detach_genpd, sdev);
568 static int simpledrm_device_attach_genpd(struct simpledrm_device *sdev)
578 static const uint64_t simpledrm_primary_plane_format_modifiers[] = {
579 DRM_FORMAT_MOD_LINEAR,
580 DRM_FORMAT_MOD_INVALID
583 static int simpledrm_primary_plane_helper_atomic_check(struct drm_plane *plane,
584 struct drm_atomic_state *state)
586 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane);
587 struct drm_shadow_plane_state *new_shadow_plane_state =
588 to_drm_shadow_plane_state(new_plane_state);
589 struct drm_framebuffer *new_fb = new_plane_state->fb;
590 struct drm_crtc *new_crtc = new_plane_state->crtc;
591 struct drm_crtc_state *new_crtc_state = NULL;
592 struct drm_device *dev = plane->dev;
593 struct simpledrm_device *sdev = simpledrm_device_of_dev(dev);
597 new_crtc_state = drm_atomic_get_new_crtc_state(state, new_crtc);
599 ret = drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state,
600 DRM_PLANE_NO_SCALING,
601 DRM_PLANE_NO_SCALING,
605 else if (!new_plane_state->visible)
608 if (new_fb->format != sdev->format) {
611 /* format conversion necessary; reserve buffer */
612 buf = drm_format_conv_state_reserve(&new_shadow_plane_state->fmtcnv_state,
613 sdev->pitch, GFP_KERNEL);
621 static void simpledrm_primary_plane_helper_atomic_update(struct drm_plane *plane,
622 struct drm_atomic_state *state)
624 struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
625 struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
626 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
627 struct drm_framebuffer *fb = plane_state->fb;
628 struct drm_device *dev = plane->dev;
629 struct simpledrm_device *sdev = simpledrm_device_of_dev(dev);
630 struct drm_atomic_helper_damage_iter iter;
631 struct drm_rect damage;
634 ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE);
638 if (!drm_dev_enter(dev, &idx))
639 goto out_drm_gem_fb_end_cpu_access;
641 drm_atomic_helper_damage_iter_init(&iter, old_plane_state, plane_state);
642 drm_atomic_for_each_plane_damage(&iter, &damage) {
643 struct drm_rect dst_clip = plane_state->dst;
644 struct iosys_map dst = sdev->screen_base;
646 if (!drm_rect_intersect(&dst_clip, &damage))
649 iosys_map_incr(&dst, drm_fb_clip_offset(sdev->pitch, sdev->format, &dst_clip));
650 drm_fb_blit(&dst, &sdev->pitch, sdev->format->format, shadow_plane_state->data,
651 fb, &damage, &shadow_plane_state->fmtcnv_state);
655 out_drm_gem_fb_end_cpu_access:
656 drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
659 static void simpledrm_primary_plane_helper_atomic_disable(struct drm_plane *plane,
660 struct drm_atomic_state *state)
662 struct drm_device *dev = plane->dev;
663 struct simpledrm_device *sdev = simpledrm_device_of_dev(dev);
666 if (!drm_dev_enter(dev, &idx))
669 /* Clear screen to black if disabled */
670 iosys_map_memset(&sdev->screen_base, 0, 0, sdev->pitch * sdev->mode.vdisplay);
675 static int simpledrm_primary_plane_helper_get_scanout_buffer(struct drm_plane *plane,
676 struct drm_scanout_buffer *sb)
678 struct simpledrm_device *sdev = simpledrm_device_of_dev(plane->dev);
680 sb->width = sdev->mode.hdisplay;
681 sb->height = sdev->mode.vdisplay;
682 sb->format = sdev->format;
683 sb->pitch[0] = sdev->pitch;
684 sb->map[0] = sdev->screen_base;
689 static const struct drm_plane_helper_funcs simpledrm_primary_plane_helper_funcs = {
690 DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
691 .atomic_check = simpledrm_primary_plane_helper_atomic_check,
692 .atomic_update = simpledrm_primary_plane_helper_atomic_update,
693 .atomic_disable = simpledrm_primary_plane_helper_atomic_disable,
694 .get_scanout_buffer = simpledrm_primary_plane_helper_get_scanout_buffer,
697 static const struct drm_plane_funcs simpledrm_primary_plane_funcs = {
698 .update_plane = drm_atomic_helper_update_plane,
699 .disable_plane = drm_atomic_helper_disable_plane,
700 .destroy = drm_plane_cleanup,
701 DRM_GEM_SHADOW_PLANE_FUNCS,
704 static enum drm_mode_status simpledrm_crtc_helper_mode_valid(struct drm_crtc *crtc,
705 const struct drm_display_mode *mode)
707 struct simpledrm_device *sdev = simpledrm_device_of_dev(crtc->dev);
709 return drm_crtc_helper_mode_valid_fixed(crtc, mode, &sdev->mode);
713 * The CRTC is always enabled. Screen updates are performed by
714 * the primary plane's atomic_update function. Disabling clears
715 * the screen in the primary plane's atomic_disable function.
717 static const struct drm_crtc_helper_funcs simpledrm_crtc_helper_funcs = {
718 .mode_valid = simpledrm_crtc_helper_mode_valid,
719 .atomic_check = drm_crtc_helper_atomic_check,
722 static const struct drm_crtc_funcs simpledrm_crtc_funcs = {
723 .reset = drm_atomic_helper_crtc_reset,
724 .destroy = drm_crtc_cleanup,
725 .set_config = drm_atomic_helper_set_config,
726 .page_flip = drm_atomic_helper_page_flip,
727 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
728 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
731 static const struct drm_encoder_funcs simpledrm_encoder_funcs = {
732 .destroy = drm_encoder_cleanup,
735 static int simpledrm_connector_helper_get_modes(struct drm_connector *connector)
737 struct simpledrm_device *sdev = simpledrm_device_of_dev(connector->dev);
739 return drm_connector_helper_get_modes_fixed(connector, &sdev->mode);
742 static const struct drm_connector_helper_funcs simpledrm_connector_helper_funcs = {
743 .get_modes = simpledrm_connector_helper_get_modes,
746 static const struct drm_connector_funcs simpledrm_connector_funcs = {
747 .reset = drm_atomic_helper_connector_reset,
748 .fill_modes = drm_helper_probe_single_connector_modes,
749 .destroy = drm_connector_cleanup,
750 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
751 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
754 static const struct drm_mode_config_funcs simpledrm_mode_config_funcs = {
755 .fb_create = drm_gem_fb_create_with_dirty,
756 .atomic_check = drm_atomic_helper_check,
757 .atomic_commit = drm_atomic_helper_commit,
764 static struct drm_display_mode simpledrm_mode(unsigned int width,
766 unsigned int width_mm,
767 unsigned int height_mm)
769 const struct drm_display_mode mode = {
770 DRM_MODE_INIT(60, width, height, width_mm, height_mm)
776 static struct simpledrm_device *simpledrm_device_create(struct drm_driver *drv,
777 struct platform_device *pdev)
779 const struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev);
780 struct device_node *of_node = pdev->dev.of_node;
781 struct simpledrm_device *sdev;
782 struct drm_device *dev;
783 int width, height, stride;
784 int width_mm = 0, height_mm = 0;
785 struct device_node *panel_node;
786 const struct drm_format_info *format;
787 struct resource *res, *mem = NULL;
788 struct drm_plane *primary_plane;
789 struct drm_crtc *crtc;
790 struct drm_encoder *encoder;
791 struct drm_connector *connector;
792 unsigned long max_width, max_height;
796 sdev = devm_drm_dev_alloc(&pdev->dev, drv, struct simpledrm_device, dev);
798 return ERR_CAST(sdev);
800 platform_set_drvdata(pdev, sdev);
806 ret = simpledrm_device_init_clocks(sdev);
809 ret = simpledrm_device_init_regulators(sdev);
812 ret = simpledrm_device_attach_genpd(sdev);
817 width = simplefb_get_width_pd(dev, pd);
819 return ERR_PTR(width);
820 height = simplefb_get_height_pd(dev, pd);
822 return ERR_PTR(height);
823 stride = simplefb_get_stride_pd(dev, pd);
825 return ERR_PTR(stride);
826 format = simplefb_get_format_pd(dev, pd);
828 return ERR_CAST(format);
829 } else if (of_node) {
830 width = simplefb_get_width_of(dev, of_node);
832 return ERR_PTR(width);
833 height = simplefb_get_height_of(dev, of_node);
835 return ERR_PTR(height);
836 stride = simplefb_get_stride_of(dev, of_node);
838 return ERR_PTR(stride);
839 format = simplefb_get_format_of(dev, of_node);
841 return ERR_CAST(format);
842 mem = simplefb_get_memory_of(dev, of_node);
844 return ERR_CAST(mem);
845 panel_node = of_parse_phandle(of_node, "panel", 0);
847 simplefb_read_u32_of(dev, panel_node, "width-mm", &width_mm);
848 simplefb_read_u32_of(dev, panel_node, "height-mm", &height_mm);
849 of_node_put(panel_node);
852 drm_err(dev, "no simplefb configuration found\n");
853 return ERR_PTR(-ENODEV);
856 stride = drm_format_info_min_pitch(format, 0, width);
857 if (drm_WARN_ON(dev, !stride))
858 return ERR_PTR(-EINVAL);
862 * Assume a monitor resolution of 96 dpi if physical dimensions
863 * are not specified to get a somewhat reasonable screen size.
866 width_mm = DRM_MODE_RES_MM(width, 96ul);
868 height_mm = DRM_MODE_RES_MM(height, 96ul);
870 sdev->mode = simpledrm_mode(width, height, width_mm, height_mm);
871 sdev->format = format;
872 sdev->pitch = stride;
874 drm_dbg(dev, "display mode={" DRM_MODE_FMT "}\n", DRM_MODE_ARG(&sdev->mode));
875 drm_dbg(dev, "framebuffer format=%p4cc, size=%dx%d, stride=%d byte\n",
876 &format->format, width, height, stride);
885 ret = devm_aperture_acquire_for_platform_device(pdev, mem->start,
888 drm_err(dev, "could not acquire memory range %pr: %d\n", mem, ret);
892 drm_dbg(dev, "using system memory framebuffer at %pr\n", mem);
894 screen_base = devm_memremap(dev->dev, mem->start, resource_size(mem), MEMREMAP_WC);
895 if (IS_ERR(screen_base))
898 iosys_map_set_vaddr(&sdev->screen_base, screen_base);
900 void __iomem *screen_base;
902 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
904 return ERR_PTR(-EINVAL);
906 ret = devm_aperture_acquire_for_platform_device(pdev, res->start,
909 drm_err(dev, "could not acquire memory range %pr: %d\n", res, ret);
913 drm_dbg(dev, "using I/O memory framebuffer at %pr\n", res);
915 mem = devm_request_mem_region(&pdev->dev, res->start, resource_size(res),
919 * We cannot make this fatal. Sometimes this comes from magic
920 * spaces our resource handlers simply don't know about. Use
921 * the I/O-memory resource as-is and try to map that instead.
923 drm_warn(dev, "could not acquire memory region %pr\n", res);
927 screen_base = devm_ioremap_wc(&pdev->dev, mem->start, resource_size(mem));
929 return ERR_PTR(-ENOMEM);
931 iosys_map_set_vaddr_iomem(&sdev->screen_base, screen_base);
938 ret = drmm_mode_config_init(dev);
942 max_width = max_t(unsigned long, width, DRM_SHADOW_PLANE_MAX_WIDTH);
943 max_height = max_t(unsigned long, height, DRM_SHADOW_PLANE_MAX_HEIGHT);
945 dev->mode_config.min_width = width;
946 dev->mode_config.max_width = max_width;
947 dev->mode_config.min_height = height;
948 dev->mode_config.max_height = max_height;
949 dev->mode_config.preferred_depth = format->depth;
950 dev->mode_config.funcs = &simpledrm_mode_config_funcs;
954 nformats = drm_fb_build_fourcc_list(dev, &format->format, 1,
955 sdev->formats, ARRAY_SIZE(sdev->formats));
957 primary_plane = &sdev->primary_plane;
958 ret = drm_universal_plane_init(dev, primary_plane, 0, &simpledrm_primary_plane_funcs,
959 sdev->formats, nformats,
960 simpledrm_primary_plane_format_modifiers,
961 DRM_PLANE_TYPE_PRIMARY, NULL);
964 drm_plane_helper_add(primary_plane, &simpledrm_primary_plane_helper_funcs);
965 drm_plane_enable_fb_damage_clips(primary_plane);
970 ret = drm_crtc_init_with_planes(dev, crtc, primary_plane, NULL,
971 &simpledrm_crtc_funcs, NULL);
974 drm_crtc_helper_add(crtc, &simpledrm_crtc_helper_funcs);
978 encoder = &sdev->encoder;
979 ret = drm_encoder_init(dev, encoder, &simpledrm_encoder_funcs,
980 DRM_MODE_ENCODER_NONE, NULL);
983 encoder->possible_crtcs = drm_crtc_mask(crtc);
987 connector = &sdev->connector;
988 ret = drm_connector_init(dev, connector, &simpledrm_connector_funcs,
989 DRM_MODE_CONNECTOR_Unknown);
992 drm_connector_helper_add(connector, &simpledrm_connector_helper_funcs);
993 drm_connector_set_panel_orientation_with_quirk(connector,
994 DRM_MODE_PANEL_ORIENTATION_UNKNOWN,
997 ret = drm_connector_attach_encoder(connector, encoder);
1001 drm_mode_config_reset(dev);
1010 DEFINE_DRM_GEM_FOPS(simpledrm_fops);
1012 static struct drm_driver simpledrm_driver = {
1013 DRM_GEM_SHMEM_DRIVER_OPS,
1014 DRM_FBDEV_SHMEM_DRIVER_OPS,
1015 .name = DRIVER_NAME,
1016 .desc = DRIVER_DESC,
1017 .major = DRIVER_MAJOR,
1018 .minor = DRIVER_MINOR,
1019 .driver_features = DRIVER_ATOMIC | DRIVER_GEM | DRIVER_MODESET,
1020 .fops = &simpledrm_fops,
1027 static int simpledrm_probe(struct platform_device *pdev)
1029 struct simpledrm_device *sdev;
1030 struct drm_device *dev;
1033 sdev = simpledrm_device_create(&simpledrm_driver, pdev);
1035 return PTR_ERR(sdev);
1038 ret = drm_dev_register(dev, 0);
1042 drm_client_setup(dev, sdev->format);
1047 static void simpledrm_remove(struct platform_device *pdev)
1049 struct simpledrm_device *sdev = platform_get_drvdata(pdev);
1050 struct drm_device *dev = &sdev->dev;
1052 drm_dev_unplug(dev);
1055 static const struct of_device_id simpledrm_of_match_table[] = {
1056 { .compatible = "simple-framebuffer", },
1059 MODULE_DEVICE_TABLE(of, simpledrm_of_match_table);
1061 static struct platform_driver simpledrm_platform_driver = {
1063 .name = "simple-framebuffer", /* connect to sysfb */
1064 .of_match_table = simpledrm_of_match_table,
1066 .probe = simpledrm_probe,
1067 .remove = simpledrm_remove,
1070 module_platform_driver(simpledrm_platform_driver);
1072 MODULE_DESCRIPTION(DRIVER_DESC);
1073 MODULE_LICENSE("GPL v2");