1 // SPDX-License-Identifier: GPL-2.0-only
4 #include <linux/of_clk.h>
5 #include <linux/minmax.h>
6 #include <linux/platform_data/simplefb.h>
7 #include <linux/platform_device.h>
8 #include <linux/regulator/consumer.h>
10 #include <drm/drm_aperture.h>
11 #include <drm/drm_atomic_state_helper.h>
12 #include <drm/drm_connector.h>
13 #include <drm/drm_damage_helper.h>
14 #include <drm/drm_device.h>
15 #include <drm/drm_drv.h>
16 #include <drm/drm_fb_helper.h>
17 #include <drm/drm_format_helper.h>
18 #include <drm/drm_gem_atomic_helper.h>
19 #include <drm/drm_gem_framebuffer_helper.h>
20 #include <drm/drm_gem_shmem_helper.h>
21 #include <drm/drm_managed.h>
22 #include <drm/drm_modeset_helper_vtables.h>
23 #include <drm/drm_probe_helper.h>
24 #include <drm/drm_simple_kms_helper.h>
26 #define DRIVER_NAME "simpledrm"
27 #define DRIVER_DESC "DRM driver for simple-framebuffer platform devices"
28 #define DRIVER_DATE "20200625"
29 #define DRIVER_MAJOR 1
30 #define DRIVER_MINOR 0
33 * Assume a monitor resolution of 96 dpi to
34 * get a somewhat reasonable screen size.
37 (((d) * 254ul) / (96ul * 10ul))
39 #define SIMPLEDRM_MODE(hd, vd) \
40 DRM_SIMPLE_MODE(hd, vd, RES_MM(hd), RES_MM(vd))
43 * Helpers for simplefb
47 simplefb_get_validated_int(struct drm_device *dev, const char *name,
50 if (value > INT_MAX) {
51 drm_err(dev, "simplefb: invalid framebuffer %s of %u\n",
59 simplefb_get_validated_int0(struct drm_device *dev, const char *name,
63 drm_err(dev, "simplefb: invalid framebuffer %s of %u\n",
67 return simplefb_get_validated_int(dev, name, value);
70 static const struct drm_format_info *
71 simplefb_get_validated_format(struct drm_device *dev, const char *format_name)
73 static const struct simplefb_format formats[] = SIMPLEFB_FORMATS;
74 const struct simplefb_format *fmt = formats;
75 const struct simplefb_format *end = fmt + ARRAY_SIZE(formats);
76 const struct drm_format_info *info;
79 drm_err(dev, "simplefb: missing framebuffer format\n");
80 return ERR_PTR(-EINVAL);
84 if (!strcmp(format_name, fmt->name)) {
85 info = drm_format_info(fmt->fourcc);
87 return ERR_PTR(-EINVAL);
93 drm_err(dev, "simplefb: unknown framebuffer format %s\n",
96 return ERR_PTR(-EINVAL);
100 simplefb_get_width_pd(struct drm_device *dev,
101 const struct simplefb_platform_data *pd)
103 return simplefb_get_validated_int0(dev, "width", pd->width);
107 simplefb_get_height_pd(struct drm_device *dev,
108 const struct simplefb_platform_data *pd)
110 return simplefb_get_validated_int0(dev, "height", pd->height);
114 simplefb_get_stride_pd(struct drm_device *dev,
115 const struct simplefb_platform_data *pd)
117 return simplefb_get_validated_int(dev, "stride", pd->stride);
120 static const struct drm_format_info *
121 simplefb_get_format_pd(struct drm_device *dev,
122 const struct simplefb_platform_data *pd)
124 return simplefb_get_validated_format(dev, pd->format);
128 simplefb_read_u32_of(struct drm_device *dev, struct device_node *of_node,
129 const char *name, u32 *value)
131 int ret = of_property_read_u32(of_node, name, value);
134 drm_err(dev, "simplefb: cannot parse framebuffer %s: error %d\n",
140 simplefb_read_string_of(struct drm_device *dev, struct device_node *of_node,
141 const char *name, const char **value)
143 int ret = of_property_read_string(of_node, name, value);
146 drm_err(dev, "simplefb: cannot parse framebuffer %s: error %d\n",
152 simplefb_get_width_of(struct drm_device *dev, struct device_node *of_node)
155 int ret = simplefb_read_u32_of(dev, of_node, "width", &width);
159 return simplefb_get_validated_int0(dev, "width", width);
163 simplefb_get_height_of(struct drm_device *dev, struct device_node *of_node)
166 int ret = simplefb_read_u32_of(dev, of_node, "height", &height);
170 return simplefb_get_validated_int0(dev, "height", height);
174 simplefb_get_stride_of(struct drm_device *dev, struct device_node *of_node)
177 int ret = simplefb_read_u32_of(dev, of_node, "stride", &stride);
181 return simplefb_get_validated_int(dev, "stride", stride);
184 static const struct drm_format_info *
185 simplefb_get_format_of(struct drm_device *dev, struct device_node *of_node)
188 int ret = simplefb_read_string_of(dev, of_node, "format", &format);
192 return simplefb_get_validated_format(dev, format);
196 * Simple Framebuffer device
199 struct simpledrm_device {
200 struct drm_device dev;
201 struct platform_device *pdev;
204 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
205 unsigned int clk_count;
209 #if defined CONFIG_OF && defined CONFIG_REGULATOR
210 unsigned int regulator_count;
211 struct regulator **regulators;
214 /* simplefb settings */
215 struct drm_display_mode mode;
216 const struct drm_format_info *format;
219 /* memory management */
220 struct resource *mem;
221 void __iomem *screen_base;
226 struct drm_connector connector;
227 struct drm_simple_display_pipe pipe;
230 static struct simpledrm_device *simpledrm_device_of_dev(struct drm_device *dev)
232 return container_of(dev, struct simpledrm_device, dev);
239 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
241 * Clock handling code.
243 * Here we handle the clocks property of our "simple-framebuffer" dt node.
244 * This is necessary so that we can make sure that any clocks needed by
245 * the display engine that the bootloader set up for us (and for which it
246 * provided a simplefb dt node), stay up, for the life of the simplefb
249 * When the driver unloads, we cleanly disable, and then release the clocks.
251 * We only complain about errors here, no action is taken as the most likely
252 * error can only happen due to a mismatch between the bootloader which set
253 * up simplefb, and the clock definitions in the device tree. Chances are
254 * that there are no adverse effects, and if there are, a clean teardown of
255 * the fb probe will not help us much either. So just complain and carry on,
256 * and hope that the user actually gets a working fb at the end of things.
259 static void simpledrm_device_release_clocks(void *res)
261 struct simpledrm_device *sdev = simpledrm_device_of_dev(res);
264 for (i = 0; i < sdev->clk_count; ++i) {
266 clk_disable_unprepare(sdev->clks[i]);
267 clk_put(sdev->clks[i]);
272 static int simpledrm_device_init_clocks(struct simpledrm_device *sdev)
274 struct drm_device *dev = &sdev->dev;
275 struct platform_device *pdev = sdev->pdev;
276 struct device_node *of_node = pdev->dev.of_node;
281 if (dev_get_platdata(&pdev->dev) || !of_node)
284 sdev->clk_count = of_clk_get_parent_count(of_node);
285 if (!sdev->clk_count)
288 sdev->clks = drmm_kzalloc(dev, sdev->clk_count * sizeof(sdev->clks[0]),
293 for (i = 0; i < sdev->clk_count; ++i) {
294 clock = of_clk_get(of_node, i);
296 ret = PTR_ERR(clock);
297 if (ret == -EPROBE_DEFER)
299 drm_err(dev, "clock %u not found: %d\n", i, ret);
302 ret = clk_prepare_enable(clock);
304 drm_err(dev, "failed to enable clock %u: %d\n",
309 sdev->clks[i] = clock;
312 return devm_add_action_or_reset(&pdev->dev,
313 simpledrm_device_release_clocks,
320 clk_disable_unprepare(sdev->clks[i]);
321 clk_put(sdev->clks[i]);
327 static int simpledrm_device_init_clocks(struct simpledrm_device *sdev)
333 #if defined CONFIG_OF && defined CONFIG_REGULATOR
335 #define SUPPLY_SUFFIX "-supply"
338 * Regulator handling code.
340 * Here we handle the num-supplies and vin*-supply properties of our
341 * "simple-framebuffer" dt node. This is necessary so that we can make sure
342 * that any regulators needed by the display hardware that the bootloader
343 * set up for us (and for which it provided a simplefb dt node), stay up,
344 * for the life of the simplefb driver.
346 * When the driver unloads, we cleanly disable, and then release the
349 * We only complain about errors here, no action is taken as the most likely
350 * error can only happen due to a mismatch between the bootloader which set
351 * up simplefb, and the regulator definitions in the device tree. Chances are
352 * that there are no adverse effects, and if there are, a clean teardown of
353 * the fb probe will not help us much either. So just complain and carry on,
354 * and hope that the user actually gets a working fb at the end of things.
357 static void simpledrm_device_release_regulators(void *res)
359 struct simpledrm_device *sdev = simpledrm_device_of_dev(res);
362 for (i = 0; i < sdev->regulator_count; ++i) {
363 if (sdev->regulators[i]) {
364 regulator_disable(sdev->regulators[i]);
365 regulator_put(sdev->regulators[i]);
370 static int simpledrm_device_init_regulators(struct simpledrm_device *sdev)
372 struct drm_device *dev = &sdev->dev;
373 struct platform_device *pdev = sdev->pdev;
374 struct device_node *of_node = pdev->dev.of_node;
375 struct property *prop;
376 struct regulator *regulator;
378 unsigned int count = 0, i = 0;
381 if (dev_get_platdata(&pdev->dev) || !of_node)
384 /* Count the number of regulator supplies */
385 for_each_property_of_node(of_node, prop) {
386 p = strstr(prop->name, SUPPLY_SUFFIX);
387 if (p && p != prop->name)
394 sdev->regulators = drmm_kzalloc(dev,
395 count * sizeof(sdev->regulators[0]),
397 if (!sdev->regulators)
400 for_each_property_of_node(of_node, prop) {
401 char name[32]; /* 32 is max size of property name */
404 p = strstr(prop->name, SUPPLY_SUFFIX);
405 if (!p || p == prop->name)
407 len = strlen(prop->name) - strlen(SUPPLY_SUFFIX) + 1;
408 strscpy(name, prop->name, min(sizeof(name), len));
410 regulator = regulator_get_optional(&pdev->dev, name);
411 if (IS_ERR(regulator)) {
412 ret = PTR_ERR(regulator);
413 if (ret == -EPROBE_DEFER)
415 drm_err(dev, "regulator %s not found: %d\n",
420 ret = regulator_enable(regulator);
422 drm_err(dev, "failed to enable regulator %u: %d\n",
424 regulator_put(regulator);
428 sdev->regulators[i++] = regulator;
430 sdev->regulator_count = i;
432 return devm_add_action_or_reset(&pdev->dev,
433 simpledrm_device_release_regulators,
439 if (sdev->regulators[i]) {
440 regulator_disable(sdev->regulators[i]);
441 regulator_put(sdev->regulators[i]);
447 static int simpledrm_device_init_regulators(struct simpledrm_device *sdev)
457 static struct drm_display_mode simpledrm_mode(unsigned int width,
460 struct drm_display_mode mode = { SIMPLEDRM_MODE(width, height) };
462 mode.clock = mode.hdisplay * mode.vdisplay * 60 / 1000 /* kHz */;
463 drm_mode_set_name(&mode);
468 static int simpledrm_device_init_fb(struct simpledrm_device *sdev)
470 int width, height, stride;
471 const struct drm_format_info *format;
472 struct drm_device *dev = &sdev->dev;
473 struct platform_device *pdev = sdev->pdev;
474 const struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev);
475 struct device_node *of_node = pdev->dev.of_node;
478 width = simplefb_get_width_pd(dev, pd);
481 height = simplefb_get_height_pd(dev, pd);
484 stride = simplefb_get_stride_pd(dev, pd);
487 format = simplefb_get_format_pd(dev, pd);
489 return PTR_ERR(format);
490 } else if (of_node) {
491 width = simplefb_get_width_of(dev, of_node);
494 height = simplefb_get_height_of(dev, of_node);
497 stride = simplefb_get_stride_of(dev, of_node);
500 format = simplefb_get_format_of(dev, of_node);
502 return PTR_ERR(format);
504 drm_err(dev, "no simplefb configuration found\n");
508 sdev->mode = simpledrm_mode(width, height);
509 sdev->format = format;
510 sdev->pitch = stride;
512 drm_dbg_kms(dev, "display mode={" DRM_MODE_FMT "}\n",
513 DRM_MODE_ARG(&sdev->mode));
515 "framebuffer format=%p4cc, size=%dx%d, stride=%d byte\n",
516 &format->format, width, height, stride);
525 static int simpledrm_device_init_mm(struct simpledrm_device *sdev)
527 struct drm_device *dev = &sdev->dev;
528 struct platform_device *pdev = sdev->pdev;
529 struct resource *mem;
530 void __iomem *screen_base;
533 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
537 ret = devm_aperture_acquire_from_firmware(dev, mem->start, resource_size(mem));
539 drm_err(dev, "could not acquire memory range %pr: error %d\n",
544 screen_base = devm_ioremap_wc(&pdev->dev, mem->start,
550 sdev->screen_base = screen_base;
560 * Support all formats of simplefb and maybe more; in order
561 * of preference. The display's update function will do any
562 * conversion necessary.
564 * TODO: Add blit helpers for remaining formats and uncomment
567 static const uint32_t simpledrm_default_formats[] = {
571 //DRM_FORMAT_XRGB1555,
572 //DRM_FORMAT_ARGB1555,
574 DRM_FORMAT_XRGB2101010,
575 DRM_FORMAT_ARGB2101010,
578 static const uint64_t simpledrm_format_modifiers[] = {
579 DRM_FORMAT_MOD_LINEAR,
580 DRM_FORMAT_MOD_INVALID
583 static int simpledrm_connector_helper_get_modes(struct drm_connector *connector)
585 struct simpledrm_device *sdev = simpledrm_device_of_dev(connector->dev);
586 struct drm_display_mode *mode;
588 mode = drm_mode_duplicate(connector->dev, &sdev->mode);
592 if (mode->name[0] == '\0')
593 drm_mode_set_name(mode);
595 mode->type |= DRM_MODE_TYPE_PREFERRED;
596 drm_mode_probed_add(connector, mode);
599 connector->display_info.width_mm = mode->width_mm;
601 connector->display_info.height_mm = mode->height_mm;
606 static const struct drm_connector_helper_funcs simpledrm_connector_helper_funcs = {
607 .get_modes = simpledrm_connector_helper_get_modes,
610 static const struct drm_connector_funcs simpledrm_connector_funcs = {
611 .reset = drm_atomic_helper_connector_reset,
612 .fill_modes = drm_helper_probe_single_connector_modes,
613 .destroy = drm_connector_cleanup,
614 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
615 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
619 simpledrm_simple_display_pipe_mode_valid(struct drm_simple_display_pipe *pipe,
620 const struct drm_display_mode *mode)
622 struct simpledrm_device *sdev = simpledrm_device_of_dev(pipe->crtc.dev);
624 if (mode->hdisplay != sdev->mode.hdisplay &&
625 mode->vdisplay != sdev->mode.vdisplay)
626 return MODE_ONE_SIZE;
627 else if (mode->hdisplay != sdev->mode.hdisplay)
628 return MODE_ONE_WIDTH;
629 else if (mode->vdisplay != sdev->mode.vdisplay)
630 return MODE_ONE_HEIGHT;
636 simpledrm_simple_display_pipe_enable(struct drm_simple_display_pipe *pipe,
637 struct drm_crtc_state *crtc_state,
638 struct drm_plane_state *plane_state)
640 struct simpledrm_device *sdev = simpledrm_device_of_dev(pipe->crtc.dev);
641 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
642 struct drm_framebuffer *fb = plane_state->fb;
643 void *vmap = shadow_plane_state->data[0].vaddr; /* TODO: Use mapping abstraction */
644 struct drm_device *dev = &sdev->dev;
645 void __iomem *dst = sdev->screen_base;
646 struct drm_rect src_clip, dst_clip;
652 drm_rect_fp_to_int(&src_clip, &plane_state->src);
654 dst_clip = plane_state->dst;
655 if (!drm_rect_intersect(&dst_clip, &src_clip))
658 if (!drm_dev_enter(dev, &idx))
661 dst += drm_fb_clip_offset(sdev->pitch, sdev->format, &dst_clip);
662 drm_fb_blit_toio(dst, sdev->pitch, sdev->format->format, vmap, fb, &src_clip);
668 simpledrm_simple_display_pipe_disable(struct drm_simple_display_pipe *pipe)
670 struct simpledrm_device *sdev = simpledrm_device_of_dev(pipe->crtc.dev);
671 struct drm_device *dev = &sdev->dev;
674 if (!drm_dev_enter(dev, &idx))
677 /* Clear screen to black if disabled */
678 memset_io(sdev->screen_base, 0, sdev->pitch * sdev->mode.vdisplay);
684 simpledrm_simple_display_pipe_update(struct drm_simple_display_pipe *pipe,
685 struct drm_plane_state *old_plane_state)
687 struct simpledrm_device *sdev = simpledrm_device_of_dev(pipe->crtc.dev);
688 struct drm_plane_state *plane_state = pipe->plane.state;
689 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
690 void *vmap = shadow_plane_state->data[0].vaddr; /* TODO: Use mapping abstraction */
691 struct drm_framebuffer *fb = plane_state->fb;
692 struct drm_device *dev = &sdev->dev;
693 void __iomem *dst = sdev->screen_base;
694 struct drm_rect src_clip, dst_clip;
700 if (!drm_atomic_helper_damage_merged(old_plane_state, plane_state, &src_clip))
703 dst_clip = plane_state->dst;
704 if (!drm_rect_intersect(&dst_clip, &src_clip))
707 if (!drm_dev_enter(dev, &idx))
710 dst += drm_fb_clip_offset(sdev->pitch, sdev->format, &dst_clip);
711 drm_fb_blit_toio(dst, sdev->pitch, sdev->format->format, vmap, fb, &src_clip);
716 static const struct drm_simple_display_pipe_funcs
717 simpledrm_simple_display_pipe_funcs = {
718 .mode_valid = simpledrm_simple_display_pipe_mode_valid,
719 .enable = simpledrm_simple_display_pipe_enable,
720 .disable = simpledrm_simple_display_pipe_disable,
721 .update = simpledrm_simple_display_pipe_update,
722 DRM_GEM_SIMPLE_DISPLAY_PIPE_SHADOW_PLANE_FUNCS,
725 static const struct drm_mode_config_funcs simpledrm_mode_config_funcs = {
726 .fb_create = drm_gem_fb_create_with_dirty,
727 .atomic_check = drm_atomic_helper_check,
728 .atomic_commit = drm_atomic_helper_commit,
731 static const uint32_t *simpledrm_device_formats(struct simpledrm_device *sdev,
732 size_t *nformats_out)
734 struct drm_device *dev = &sdev->dev;
738 goto out; /* don't rebuild list on recurring calls */
740 /* native format goes first */
741 sdev->formats[0] = sdev->format->format;
744 /* default formats go second */
745 for (i = 0; i < ARRAY_SIZE(simpledrm_default_formats); ++i) {
746 if (simpledrm_default_formats[i] == sdev->format->format)
747 continue; /* native format already went first */
748 sdev->formats[sdev->nformats] = simpledrm_default_formats[i];
753 * TODO: The simpledrm driver converts framebuffers to the native
754 * format when copying them to device memory. If there are more
755 * formats listed than supported by the driver, the native format
756 * is not supported by the conversion helpers. Therefore *only*
757 * support the native format and add a conversion helper ASAP.
759 if (drm_WARN_ONCE(dev, i != sdev->nformats,
760 "format conversion helpers required for %p4cc",
761 &sdev->format->format)) {
766 *nformats_out = sdev->nformats;
767 return sdev->formats;
770 static int simpledrm_device_init_modeset(struct simpledrm_device *sdev)
772 struct drm_device *dev = &sdev->dev;
773 struct drm_display_mode *mode = &sdev->mode;
774 struct drm_connector *connector = &sdev->connector;
775 struct drm_simple_display_pipe *pipe = &sdev->pipe;
776 unsigned long max_width, max_height;
777 const uint32_t *formats;
781 ret = drmm_mode_config_init(dev);
785 max_width = max_t(unsigned long, mode->hdisplay, DRM_SHADOW_PLANE_MAX_WIDTH);
786 max_height = max_t(unsigned long, mode->vdisplay, DRM_SHADOW_PLANE_MAX_HEIGHT);
788 dev->mode_config.min_width = mode->hdisplay;
789 dev->mode_config.max_width = max_width;
790 dev->mode_config.min_height = mode->vdisplay;
791 dev->mode_config.max_height = max_height;
792 dev->mode_config.prefer_shadow_fbdev = true;
793 dev->mode_config.preferred_depth = sdev->format->cpp[0] * 8;
794 dev->mode_config.funcs = &simpledrm_mode_config_funcs;
796 ret = drm_connector_init(dev, connector, &simpledrm_connector_funcs,
797 DRM_MODE_CONNECTOR_Unknown);
800 drm_connector_helper_add(connector, &simpledrm_connector_helper_funcs);
802 formats = simpledrm_device_formats(sdev, &nformats);
804 ret = drm_simple_display_pipe_init(dev, pipe, &simpledrm_simple_display_pipe_funcs,
805 formats, nformats, simpledrm_format_modifiers,
810 drm_plane_enable_fb_damage_clips(&pipe->plane);
812 drm_mode_config_reset(dev);
821 static struct simpledrm_device *
822 simpledrm_device_create(struct drm_driver *drv, struct platform_device *pdev)
824 struct simpledrm_device *sdev;
827 sdev = devm_drm_dev_alloc(&pdev->dev, drv, struct simpledrm_device,
830 return ERR_CAST(sdev);
832 platform_set_drvdata(pdev, sdev);
834 ret = simpledrm_device_init_clocks(sdev);
837 ret = simpledrm_device_init_regulators(sdev);
840 ret = simpledrm_device_init_fb(sdev);
843 ret = simpledrm_device_init_mm(sdev);
846 ret = simpledrm_device_init_modeset(sdev);
857 DEFINE_DRM_GEM_FOPS(simpledrm_fops);
859 static struct drm_driver simpledrm_driver = {
860 DRM_GEM_SHMEM_DRIVER_OPS,
864 .major = DRIVER_MAJOR,
865 .minor = DRIVER_MINOR,
866 .driver_features = DRIVER_ATOMIC | DRIVER_GEM | DRIVER_MODESET,
867 .fops = &simpledrm_fops,
874 static int simpledrm_probe(struct platform_device *pdev)
876 struct simpledrm_device *sdev;
877 struct drm_device *dev;
880 sdev = simpledrm_device_create(&simpledrm_driver, pdev);
882 return PTR_ERR(sdev);
885 ret = drm_dev_register(dev, 0);
889 drm_fbdev_generic_setup(dev, 0);
894 static int simpledrm_remove(struct platform_device *pdev)
896 struct simpledrm_device *sdev = platform_get_drvdata(pdev);
897 struct drm_device *dev = &sdev->dev;
904 static const struct of_device_id simpledrm_of_match_table[] = {
905 { .compatible = "simple-framebuffer", },
908 MODULE_DEVICE_TABLE(of, simpledrm_of_match_table);
910 static struct platform_driver simpledrm_platform_driver = {
912 .name = "simple-framebuffer", /* connect to sysfb */
913 .of_match_table = simpledrm_of_match_table,
915 .probe = simpledrm_probe,
916 .remove = simpledrm_remove,
919 module_platform_driver(simpledrm_platform_driver);
921 MODULE_DESCRIPTION(DRIVER_DESC);
922 MODULE_LICENSE("GPL v2");