1 // SPDX-License-Identifier: GPL-2.0-only
4 #include <linux/of_clk.h>
5 #include <linux/minmax.h>
6 #include <linux/of_address.h>
7 #include <linux/platform_data/simplefb.h>
8 #include <linux/platform_device.h>
9 #include <linux/pm_domain.h>
10 #include <linux/regulator/consumer.h>
12 #include <drm/drm_aperture.h>
13 #include <drm/drm_atomic.h>
14 #include <drm/drm_atomic_state_helper.h>
15 #include <drm/drm_connector.h>
16 #include <drm/drm_crtc_helper.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_generic.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>
30 #define DRIVER_NAME "simpledrm"
31 #define DRIVER_DESC "DRM driver for simple-framebuffer platform devices"
32 #define DRIVER_DATE "20200625"
33 #define DRIVER_MAJOR 1
34 #define DRIVER_MINOR 0
37 * Helpers for simplefb
41 simplefb_get_validated_int(struct drm_device *dev, const char *name,
44 if (value > INT_MAX) {
45 drm_err(dev, "simplefb: invalid framebuffer %s of %u\n",
53 simplefb_get_validated_int0(struct drm_device *dev, const char *name,
57 drm_err(dev, "simplefb: invalid framebuffer %s of %u\n",
61 return simplefb_get_validated_int(dev, name, value);
64 static const struct drm_format_info *
65 simplefb_get_validated_format(struct drm_device *dev, const char *format_name)
67 static const struct simplefb_format formats[] = SIMPLEFB_FORMATS;
68 const struct simplefb_format *fmt = formats;
69 const struct simplefb_format *end = fmt + ARRAY_SIZE(formats);
70 const struct drm_format_info *info;
73 drm_err(dev, "simplefb: missing framebuffer format\n");
74 return ERR_PTR(-EINVAL);
78 if (!strcmp(format_name, fmt->name)) {
79 info = drm_format_info(fmt->fourcc);
81 return ERR_PTR(-EINVAL);
87 drm_err(dev, "simplefb: unknown framebuffer format %s\n",
90 return ERR_PTR(-EINVAL);
94 simplefb_get_width_pd(struct drm_device *dev,
95 const struct simplefb_platform_data *pd)
97 return simplefb_get_validated_int0(dev, "width", pd->width);
101 simplefb_get_height_pd(struct drm_device *dev,
102 const struct simplefb_platform_data *pd)
104 return simplefb_get_validated_int0(dev, "height", pd->height);
108 simplefb_get_stride_pd(struct drm_device *dev,
109 const struct simplefb_platform_data *pd)
111 return simplefb_get_validated_int(dev, "stride", pd->stride);
114 static const struct drm_format_info *
115 simplefb_get_format_pd(struct drm_device *dev,
116 const struct simplefb_platform_data *pd)
118 return simplefb_get_validated_format(dev, pd->format);
122 simplefb_read_u32_of(struct drm_device *dev, struct device_node *of_node,
123 const char *name, u32 *value)
125 int ret = of_property_read_u32(of_node, name, value);
128 drm_err(dev, "simplefb: cannot parse framebuffer %s: error %d\n",
134 simplefb_read_string_of(struct drm_device *dev, struct device_node *of_node,
135 const char *name, const char **value)
137 int ret = of_property_read_string(of_node, name, value);
140 drm_err(dev, "simplefb: cannot parse framebuffer %s: error %d\n",
146 simplefb_get_width_of(struct drm_device *dev, struct device_node *of_node)
149 int ret = simplefb_read_u32_of(dev, of_node, "width", &width);
153 return simplefb_get_validated_int0(dev, "width", width);
157 simplefb_get_height_of(struct drm_device *dev, struct device_node *of_node)
160 int ret = simplefb_read_u32_of(dev, of_node, "height", &height);
164 return simplefb_get_validated_int0(dev, "height", height);
168 simplefb_get_stride_of(struct drm_device *dev, struct device_node *of_node)
171 int ret = simplefb_read_u32_of(dev, of_node, "stride", &stride);
175 return simplefb_get_validated_int(dev, "stride", stride);
178 static const struct drm_format_info *
179 simplefb_get_format_of(struct drm_device *dev, struct device_node *of_node)
182 int ret = simplefb_read_string_of(dev, of_node, "format", &format);
186 return simplefb_get_validated_format(dev, format);
189 static struct resource *
190 simplefb_get_memory_of(struct drm_device *dev, struct device_node *of_node)
192 struct device_node *np;
193 struct resource *res;
196 np = of_parse_phandle(of_node, "memory-region", 0);
200 res = devm_kzalloc(dev->dev, sizeof(*res), GFP_KERNEL);
202 return ERR_PTR(-ENOMEM);
204 err = of_address_to_resource(np, 0, res);
208 if (of_property_present(of_node, "reg"))
209 drm_warn(dev, "preferring \"memory-region\" over \"reg\" property\n");
215 * Simple Framebuffer device
218 struct simpledrm_device {
219 struct drm_device dev;
222 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
223 unsigned int clk_count;
227 #if defined CONFIG_OF && defined CONFIG_REGULATOR
228 unsigned int regulator_count;
229 struct regulator **regulators;
232 #if defined CONFIG_OF && defined CONFIG_PM_GENERIC_DOMAINS
234 struct device **pwr_dom_devs;
235 struct device_link **pwr_dom_links;
238 /* simplefb settings */
239 struct drm_display_mode mode;
240 const struct drm_format_info *format;
243 /* memory management */
244 struct iosys_map screen_base;
249 struct drm_plane primary_plane;
250 struct drm_crtc crtc;
251 struct drm_encoder encoder;
252 struct drm_connector connector;
255 static struct simpledrm_device *simpledrm_device_of_dev(struct drm_device *dev)
257 return container_of(dev, struct simpledrm_device, dev);
264 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
266 * Clock handling code.
268 * Here we handle the clocks property of our "simple-framebuffer" dt node.
269 * This is necessary so that we can make sure that any clocks needed by
270 * the display engine that the bootloader set up for us (and for which it
271 * provided a simplefb dt node), stay up, for the life of the simplefb
274 * When the driver unloads, we cleanly disable, and then release the clocks.
276 * We only complain about errors here, no action is taken as the most likely
277 * error can only happen due to a mismatch between the bootloader which set
278 * up simplefb, and the clock definitions in the device tree. Chances are
279 * that there are no adverse effects, and if there are, a clean teardown of
280 * the fb probe will not help us much either. So just complain and carry on,
281 * and hope that the user actually gets a working fb at the end of things.
284 static void simpledrm_device_release_clocks(void *res)
286 struct simpledrm_device *sdev = simpledrm_device_of_dev(res);
289 for (i = 0; i < sdev->clk_count; ++i) {
291 clk_disable_unprepare(sdev->clks[i]);
292 clk_put(sdev->clks[i]);
297 static int simpledrm_device_init_clocks(struct simpledrm_device *sdev)
299 struct drm_device *dev = &sdev->dev;
300 struct platform_device *pdev = to_platform_device(dev->dev);
301 struct device_node *of_node = pdev->dev.of_node;
306 if (dev_get_platdata(&pdev->dev) || !of_node)
309 sdev->clk_count = of_clk_get_parent_count(of_node);
310 if (!sdev->clk_count)
313 sdev->clks = drmm_kzalloc(dev, sdev->clk_count * sizeof(sdev->clks[0]),
318 for (i = 0; i < sdev->clk_count; ++i) {
319 clock = of_clk_get(of_node, i);
321 ret = PTR_ERR(clock);
322 if (ret == -EPROBE_DEFER)
324 drm_err(dev, "clock %u not found: %d\n", i, ret);
327 ret = clk_prepare_enable(clock);
329 drm_err(dev, "failed to enable clock %u: %d\n",
334 sdev->clks[i] = clock;
337 return devm_add_action_or_reset(&pdev->dev,
338 simpledrm_device_release_clocks,
345 clk_disable_unprepare(sdev->clks[i]);
346 clk_put(sdev->clks[i]);
352 static int simpledrm_device_init_clocks(struct simpledrm_device *sdev)
358 #if defined CONFIG_OF && defined CONFIG_REGULATOR
360 #define SUPPLY_SUFFIX "-supply"
363 * Regulator handling code.
365 * Here we handle the num-supplies and vin*-supply properties of our
366 * "simple-framebuffer" dt node. This is necessary so that we can make sure
367 * that any regulators needed by the display hardware that the bootloader
368 * set up for us (and for which it provided a simplefb dt node), stay up,
369 * for the life of the simplefb driver.
371 * When the driver unloads, we cleanly disable, and then release the
374 * We only complain about errors here, no action is taken as the most likely
375 * error can only happen due to a mismatch between the bootloader which set
376 * up simplefb, and the regulator definitions in the device tree. Chances are
377 * that there are no adverse effects, and if there are, a clean teardown of
378 * the fb probe will not help us much either. So just complain and carry on,
379 * and hope that the user actually gets a working fb at the end of things.
382 static void simpledrm_device_release_regulators(void *res)
384 struct simpledrm_device *sdev = simpledrm_device_of_dev(res);
387 for (i = 0; i < sdev->regulator_count; ++i) {
388 if (sdev->regulators[i]) {
389 regulator_disable(sdev->regulators[i]);
390 regulator_put(sdev->regulators[i]);
395 static int simpledrm_device_init_regulators(struct simpledrm_device *sdev)
397 struct drm_device *dev = &sdev->dev;
398 struct platform_device *pdev = to_platform_device(dev->dev);
399 struct device_node *of_node = pdev->dev.of_node;
400 struct property *prop;
401 struct regulator *regulator;
403 unsigned int count = 0, i = 0;
406 if (dev_get_platdata(&pdev->dev) || !of_node)
409 /* Count the number of regulator supplies */
410 for_each_property_of_node(of_node, prop) {
411 p = strstr(prop->name, SUPPLY_SUFFIX);
412 if (p && p != prop->name)
419 sdev->regulators = drmm_kzalloc(dev,
420 count * sizeof(sdev->regulators[0]),
422 if (!sdev->regulators)
425 for_each_property_of_node(of_node, prop) {
426 char name[32]; /* 32 is max size of property name */
429 p = strstr(prop->name, SUPPLY_SUFFIX);
430 if (!p || p == prop->name)
432 len = strlen(prop->name) - strlen(SUPPLY_SUFFIX) + 1;
433 strscpy(name, prop->name, min(sizeof(name), len));
435 regulator = regulator_get_optional(&pdev->dev, name);
436 if (IS_ERR(regulator)) {
437 ret = PTR_ERR(regulator);
438 if (ret == -EPROBE_DEFER)
440 drm_err(dev, "regulator %s not found: %d\n",
445 ret = regulator_enable(regulator);
447 drm_err(dev, "failed to enable regulator %u: %d\n",
449 regulator_put(regulator);
453 sdev->regulators[i++] = regulator;
455 sdev->regulator_count = i;
457 return devm_add_action_or_reset(&pdev->dev,
458 simpledrm_device_release_regulators,
464 if (sdev->regulators[i]) {
465 regulator_disable(sdev->regulators[i]);
466 regulator_put(sdev->regulators[i]);
472 static int simpledrm_device_init_regulators(struct simpledrm_device *sdev)
478 #if defined CONFIG_OF && defined CONFIG_PM_GENERIC_DOMAINS
480 * Generic power domain handling code.
482 * Here we handle the power-domains properties of our "simple-framebuffer"
483 * dt node. This is only necessary if there is more than one power-domain.
484 * A single power-domains is handled automatically by the driver core. Multiple
485 * power-domains have to be handled by drivers since the driver core can't know
486 * the correct power sequencing. Power sequencing is not an issue for simpledrm
487 * since the bootloader has put the power domains already in the correct state.
488 * simpledrm has only to ensure they remain active for its lifetime.
490 * When the driver unloads, we detach from the power-domains.
492 * We only complain about errors here, no action is taken as the most likely
493 * error can only happen due to a mismatch between the bootloader which set
494 * up the "simple-framebuffer" dt node, and the PM domain providers in the
495 * device tree. Chances are that there are no adverse effects, and if there are,
496 * a clean teardown of the fb probe will not help us much either. So just
497 * complain and carry on, and hope that the user actually gets a working fb at
500 static void simpledrm_device_detach_genpd(void *res)
503 struct simpledrm_device *sdev = res;
505 if (sdev->pwr_dom_count <= 1)
508 for (i = sdev->pwr_dom_count - 1; i >= 0; i--) {
509 if (sdev->pwr_dom_links[i])
510 device_link_del(sdev->pwr_dom_links[i]);
511 if (!IS_ERR_OR_NULL(sdev->pwr_dom_devs[i]))
512 dev_pm_domain_detach(sdev->pwr_dom_devs[i], true);
516 static int simpledrm_device_attach_genpd(struct simpledrm_device *sdev)
518 struct device *dev = sdev->dev.dev;
521 sdev->pwr_dom_count = of_count_phandle_with_args(dev->of_node, "power-domains",
522 "#power-domain-cells");
524 * Single power-domain devices are handled by driver core nothing to do
525 * here. The same for device nodes without "power-domains" property.
527 if (sdev->pwr_dom_count <= 1)
530 sdev->pwr_dom_devs = devm_kcalloc(dev, sdev->pwr_dom_count,
531 sizeof(*sdev->pwr_dom_devs),
533 if (!sdev->pwr_dom_devs)
536 sdev->pwr_dom_links = devm_kcalloc(dev, sdev->pwr_dom_count,
537 sizeof(*sdev->pwr_dom_links),
539 if (!sdev->pwr_dom_links)
542 for (i = 0; i < sdev->pwr_dom_count; i++) {
543 sdev->pwr_dom_devs[i] = dev_pm_domain_attach_by_id(dev, i);
544 if (IS_ERR(sdev->pwr_dom_devs[i])) {
545 int ret = PTR_ERR(sdev->pwr_dom_devs[i]);
546 if (ret == -EPROBE_DEFER) {
547 simpledrm_device_detach_genpd(sdev);
551 "pm_domain_attach_by_id(%u) failed: %d\n", i, ret);
555 sdev->pwr_dom_links[i] = device_link_add(dev,
556 sdev->pwr_dom_devs[i],
560 if (!sdev->pwr_dom_links[i])
561 drm_warn(&sdev->dev, "failed to link power-domain %d\n", i);
564 return devm_add_action_or_reset(dev, simpledrm_device_detach_genpd, sdev);
567 static int simpledrm_device_attach_genpd(struct simpledrm_device *sdev)
577 static const uint64_t simpledrm_primary_plane_format_modifiers[] = {
578 DRM_FORMAT_MOD_LINEAR,
579 DRM_FORMAT_MOD_INVALID
582 static int simpledrm_primary_plane_helper_atomic_check(struct drm_plane *plane,
583 struct drm_atomic_state *state)
585 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane);
586 struct drm_shadow_plane_state *new_shadow_plane_state =
587 to_drm_shadow_plane_state(new_plane_state);
588 struct drm_framebuffer *new_fb = new_plane_state->fb;
589 struct drm_crtc *new_crtc = new_plane_state->crtc;
590 struct drm_crtc_state *new_crtc_state = NULL;
591 struct drm_device *dev = plane->dev;
592 struct simpledrm_device *sdev = simpledrm_device_of_dev(dev);
596 new_crtc_state = drm_atomic_get_new_crtc_state(state, new_crtc);
598 ret = drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state,
599 DRM_PLANE_NO_SCALING,
600 DRM_PLANE_NO_SCALING,
604 else if (!new_plane_state->visible)
607 if (new_fb->format != sdev->format) {
610 /* format conversion necessary; reserve buffer */
611 buf = drm_format_conv_state_reserve(&new_shadow_plane_state->fmtcnv_state,
612 sdev->pitch, GFP_KERNEL);
620 static void simpledrm_primary_plane_helper_atomic_update(struct drm_plane *plane,
621 struct drm_atomic_state *state)
623 struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
624 struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
625 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
626 struct drm_framebuffer *fb = plane_state->fb;
627 struct drm_device *dev = plane->dev;
628 struct simpledrm_device *sdev = simpledrm_device_of_dev(dev);
629 struct drm_atomic_helper_damage_iter iter;
630 struct drm_rect damage;
633 ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE);
637 if (!drm_dev_enter(dev, &idx))
638 goto out_drm_gem_fb_end_cpu_access;
640 drm_atomic_helper_damage_iter_init(&iter, old_plane_state, plane_state);
641 drm_atomic_for_each_plane_damage(&iter, &damage) {
642 struct drm_rect dst_clip = plane_state->dst;
643 struct iosys_map dst = sdev->screen_base;
645 if (!drm_rect_intersect(&dst_clip, &damage))
648 iosys_map_incr(&dst, drm_fb_clip_offset(sdev->pitch, sdev->format, &dst_clip));
649 drm_fb_blit(&dst, &sdev->pitch, sdev->format->format, shadow_plane_state->data,
650 fb, &damage, &shadow_plane_state->fmtcnv_state);
654 out_drm_gem_fb_end_cpu_access:
655 drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
658 static void simpledrm_primary_plane_helper_atomic_disable(struct drm_plane *plane,
659 struct drm_atomic_state *state)
661 struct drm_device *dev = plane->dev;
662 struct simpledrm_device *sdev = simpledrm_device_of_dev(dev);
665 if (!drm_dev_enter(dev, &idx))
668 /* Clear screen to black if disabled */
669 iosys_map_memset(&sdev->screen_base, 0, 0, sdev->pitch * sdev->mode.vdisplay);
674 static const struct drm_plane_helper_funcs simpledrm_primary_plane_helper_funcs = {
675 DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
676 .atomic_check = simpledrm_primary_plane_helper_atomic_check,
677 .atomic_update = simpledrm_primary_plane_helper_atomic_update,
678 .atomic_disable = simpledrm_primary_plane_helper_atomic_disable,
681 static const struct drm_plane_funcs simpledrm_primary_plane_funcs = {
682 .update_plane = drm_atomic_helper_update_plane,
683 .disable_plane = drm_atomic_helper_disable_plane,
684 .destroy = drm_plane_cleanup,
685 DRM_GEM_SHADOW_PLANE_FUNCS,
688 static enum drm_mode_status simpledrm_crtc_helper_mode_valid(struct drm_crtc *crtc,
689 const struct drm_display_mode *mode)
691 struct simpledrm_device *sdev = simpledrm_device_of_dev(crtc->dev);
693 return drm_crtc_helper_mode_valid_fixed(crtc, mode, &sdev->mode);
697 * The CRTC is always enabled. Screen updates are performed by
698 * the primary plane's atomic_update function. Disabling clears
699 * the screen in the primary plane's atomic_disable function.
701 static const struct drm_crtc_helper_funcs simpledrm_crtc_helper_funcs = {
702 .mode_valid = simpledrm_crtc_helper_mode_valid,
703 .atomic_check = drm_crtc_helper_atomic_check,
706 static const struct drm_crtc_funcs simpledrm_crtc_funcs = {
707 .reset = drm_atomic_helper_crtc_reset,
708 .destroy = drm_crtc_cleanup,
709 .set_config = drm_atomic_helper_set_config,
710 .page_flip = drm_atomic_helper_page_flip,
711 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
712 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
715 static const struct drm_encoder_funcs simpledrm_encoder_funcs = {
716 .destroy = drm_encoder_cleanup,
719 static int simpledrm_connector_helper_get_modes(struct drm_connector *connector)
721 struct simpledrm_device *sdev = simpledrm_device_of_dev(connector->dev);
723 return drm_connector_helper_get_modes_fixed(connector, &sdev->mode);
726 static const struct drm_connector_helper_funcs simpledrm_connector_helper_funcs = {
727 .get_modes = simpledrm_connector_helper_get_modes,
730 static const struct drm_connector_funcs simpledrm_connector_funcs = {
731 .reset = drm_atomic_helper_connector_reset,
732 .fill_modes = drm_helper_probe_single_connector_modes,
733 .destroy = drm_connector_cleanup,
734 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
735 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
738 static const struct drm_mode_config_funcs simpledrm_mode_config_funcs = {
739 .fb_create = drm_gem_fb_create_with_dirty,
740 .atomic_check = drm_atomic_helper_check,
741 .atomic_commit = drm_atomic_helper_commit,
748 static struct drm_display_mode simpledrm_mode(unsigned int width,
750 unsigned int width_mm,
751 unsigned int height_mm)
753 const struct drm_display_mode mode = {
754 DRM_MODE_INIT(60, width, height, width_mm, height_mm)
760 static struct simpledrm_device *simpledrm_device_create(struct drm_driver *drv,
761 struct platform_device *pdev)
763 const struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev);
764 struct device_node *of_node = pdev->dev.of_node;
765 struct simpledrm_device *sdev;
766 struct drm_device *dev;
767 int width, height, stride;
768 int width_mm = 0, height_mm = 0;
769 struct device_node *panel_node;
770 const struct drm_format_info *format;
771 struct resource *res, *mem = NULL;
772 struct drm_plane *primary_plane;
773 struct drm_crtc *crtc;
774 struct drm_encoder *encoder;
775 struct drm_connector *connector;
776 unsigned long max_width, max_height;
780 sdev = devm_drm_dev_alloc(&pdev->dev, drv, struct simpledrm_device, dev);
782 return ERR_CAST(sdev);
784 platform_set_drvdata(pdev, sdev);
790 ret = simpledrm_device_init_clocks(sdev);
793 ret = simpledrm_device_init_regulators(sdev);
796 ret = simpledrm_device_attach_genpd(sdev);
801 width = simplefb_get_width_pd(dev, pd);
803 return ERR_PTR(width);
804 height = simplefb_get_height_pd(dev, pd);
806 return ERR_PTR(height);
807 stride = simplefb_get_stride_pd(dev, pd);
809 return ERR_PTR(stride);
810 format = simplefb_get_format_pd(dev, pd);
812 return ERR_CAST(format);
813 } else if (of_node) {
814 width = simplefb_get_width_of(dev, of_node);
816 return ERR_PTR(width);
817 height = simplefb_get_height_of(dev, of_node);
819 return ERR_PTR(height);
820 stride = simplefb_get_stride_of(dev, of_node);
822 return ERR_PTR(stride);
823 format = simplefb_get_format_of(dev, of_node);
825 return ERR_CAST(format);
826 mem = simplefb_get_memory_of(dev, of_node);
828 return ERR_CAST(mem);
829 panel_node = of_parse_phandle(of_node, "panel", 0);
831 simplefb_read_u32_of(dev, panel_node, "width-mm", &width_mm);
832 simplefb_read_u32_of(dev, panel_node, "height-mm", &height_mm);
833 of_node_put(panel_node);
836 drm_err(dev, "no simplefb configuration found\n");
837 return ERR_PTR(-ENODEV);
840 stride = drm_format_info_min_pitch(format, 0, width);
841 if (drm_WARN_ON(dev, !stride))
842 return ERR_PTR(-EINVAL);
846 * Assume a monitor resolution of 96 dpi if physical dimensions
847 * are not specified to get a somewhat reasonable screen size.
850 width_mm = DRM_MODE_RES_MM(width, 96ul);
852 height_mm = DRM_MODE_RES_MM(height, 96ul);
854 sdev->mode = simpledrm_mode(width, height, width_mm, height_mm);
855 sdev->format = format;
856 sdev->pitch = stride;
858 drm_dbg(dev, "display mode={" DRM_MODE_FMT "}\n", DRM_MODE_ARG(&sdev->mode));
859 drm_dbg(dev, "framebuffer format=%p4cc, size=%dx%d, stride=%d byte\n",
860 &format->format, width, height, stride);
869 ret = devm_aperture_acquire_from_firmware(dev, mem->start, resource_size(mem));
871 drm_err(dev, "could not acquire memory range %pr: %d\n", mem, ret);
875 drm_dbg(dev, "using system memory framebuffer at %pr\n", mem);
877 screen_base = devm_memremap(dev->dev, mem->start, resource_size(mem), MEMREMAP_WC);
878 if (IS_ERR(screen_base))
881 iosys_map_set_vaddr(&sdev->screen_base, screen_base);
883 void __iomem *screen_base;
885 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
887 return ERR_PTR(-EINVAL);
889 ret = devm_aperture_acquire_from_firmware(dev, res->start, resource_size(res));
891 drm_err(dev, "could not acquire memory range %pr: %d\n", res, ret);
895 drm_dbg(dev, "using I/O memory framebuffer at %pr\n", res);
897 mem = devm_request_mem_region(&pdev->dev, res->start, resource_size(res),
901 * We cannot make this fatal. Sometimes this comes from magic
902 * spaces our resource handlers simply don't know about. Use
903 * the I/O-memory resource as-is and try to map that instead.
905 drm_warn(dev, "could not acquire memory region %pr\n", res);
909 screen_base = devm_ioremap_wc(&pdev->dev, mem->start, resource_size(mem));
911 return ERR_PTR(-ENOMEM);
913 iosys_map_set_vaddr_iomem(&sdev->screen_base, screen_base);
920 ret = drmm_mode_config_init(dev);
924 max_width = max_t(unsigned long, width, DRM_SHADOW_PLANE_MAX_WIDTH);
925 max_height = max_t(unsigned long, height, DRM_SHADOW_PLANE_MAX_HEIGHT);
927 dev->mode_config.min_width = width;
928 dev->mode_config.max_width = max_width;
929 dev->mode_config.min_height = height;
930 dev->mode_config.max_height = max_height;
931 dev->mode_config.preferred_depth = format->depth;
932 dev->mode_config.funcs = &simpledrm_mode_config_funcs;
936 nformats = drm_fb_build_fourcc_list(dev, &format->format, 1,
937 sdev->formats, ARRAY_SIZE(sdev->formats));
939 primary_plane = &sdev->primary_plane;
940 ret = drm_universal_plane_init(dev, primary_plane, 0, &simpledrm_primary_plane_funcs,
941 sdev->formats, nformats,
942 simpledrm_primary_plane_format_modifiers,
943 DRM_PLANE_TYPE_PRIMARY, NULL);
946 drm_plane_helper_add(primary_plane, &simpledrm_primary_plane_helper_funcs);
947 drm_plane_enable_fb_damage_clips(primary_plane);
952 ret = drm_crtc_init_with_planes(dev, crtc, primary_plane, NULL,
953 &simpledrm_crtc_funcs, NULL);
956 drm_crtc_helper_add(crtc, &simpledrm_crtc_helper_funcs);
960 encoder = &sdev->encoder;
961 ret = drm_encoder_init(dev, encoder, &simpledrm_encoder_funcs,
962 DRM_MODE_ENCODER_NONE, NULL);
965 encoder->possible_crtcs = drm_crtc_mask(crtc);
969 connector = &sdev->connector;
970 ret = drm_connector_init(dev, connector, &simpledrm_connector_funcs,
971 DRM_MODE_CONNECTOR_Unknown);
974 drm_connector_helper_add(connector, &simpledrm_connector_helper_funcs);
975 drm_connector_set_panel_orientation_with_quirk(connector,
976 DRM_MODE_PANEL_ORIENTATION_UNKNOWN,
979 ret = drm_connector_attach_encoder(connector, encoder);
983 drm_mode_config_reset(dev);
992 DEFINE_DRM_GEM_FOPS(simpledrm_fops);
994 static struct drm_driver simpledrm_driver = {
995 DRM_GEM_SHMEM_DRIVER_OPS,
999 .major = DRIVER_MAJOR,
1000 .minor = DRIVER_MINOR,
1001 .driver_features = DRIVER_ATOMIC | DRIVER_GEM | DRIVER_MODESET,
1002 .fops = &simpledrm_fops,
1009 static int simpledrm_probe(struct platform_device *pdev)
1011 struct simpledrm_device *sdev;
1012 struct drm_device *dev;
1013 unsigned int color_mode;
1016 sdev = simpledrm_device_create(&simpledrm_driver, pdev);
1018 return PTR_ERR(sdev);
1021 ret = drm_dev_register(dev, 0);
1025 color_mode = drm_format_info_bpp(sdev->format, 0);
1026 if (color_mode == 16)
1027 color_mode = sdev->format->depth; // can be 15 or 16
1029 drm_fbdev_generic_setup(dev, color_mode);
1034 static void simpledrm_remove(struct platform_device *pdev)
1036 struct simpledrm_device *sdev = platform_get_drvdata(pdev);
1037 struct drm_device *dev = &sdev->dev;
1039 drm_dev_unplug(dev);
1042 static const struct of_device_id simpledrm_of_match_table[] = {
1043 { .compatible = "simple-framebuffer", },
1046 MODULE_DEVICE_TABLE(of, simpledrm_of_match_table);
1048 static struct platform_driver simpledrm_platform_driver = {
1050 .name = "simple-framebuffer", /* connect to sysfb */
1051 .of_match_table = simpledrm_of_match_table,
1053 .probe = simpledrm_probe,
1054 .remove_new = simpledrm_remove,
1057 module_platform_driver(simpledrm_platform_driver);
1059 MODULE_DESCRIPTION(DRIVER_DESC);
1060 MODULE_LICENSE("GPL v2");