drm: simpledrm: print resource info using '%pr'
[linux-2.6-microblaze.git] / drivers / gpu / drm / tiny / simpledrm.c
1 // SPDX-License-Identifier: GPL-2.0-only
2
3 #include <linux/clk.h>
4 #include <linux/of_clk.h>
5 #include <linux/platform_data/simplefb.h>
6 #include <linux/platform_device.h>
7 #include <linux/regulator/consumer.h>
8
9 #include <drm/drm_aperture.h>
10 #include <drm/drm_atomic_state_helper.h>
11 #include <drm/drm_connector.h>
12 #include <drm/drm_damage_helper.h>
13 #include <drm/drm_device.h>
14 #include <drm/drm_drv.h>
15 #include <drm/drm_fb_helper.h>
16 #include <drm/drm_format_helper.h>
17 #include <drm/drm_gem_atomic_helper.h>
18 #include <drm/drm_gem_framebuffer_helper.h>
19 #include <drm/drm_gem_shmem_helper.h>
20 #include <drm/drm_managed.h>
21 #include <drm/drm_modeset_helper_vtables.h>
22 #include <drm/drm_probe_helper.h>
23 #include <drm/drm_simple_kms_helper.h>
24
25 #define DRIVER_NAME     "simpledrm"
26 #define DRIVER_DESC     "DRM driver for simple-framebuffer platform devices"
27 #define DRIVER_DATE     "20200625"
28 #define DRIVER_MAJOR    1
29 #define DRIVER_MINOR    0
30
31 /*
32  * Assume a monitor resolution of 96 dpi to
33  * get a somewhat reasonable screen size.
34  */
35 #define RES_MM(d)       \
36         (((d) * 254ul) / (96ul * 10ul))
37
38 #define SIMPLEDRM_MODE(hd, vd)  \
39         DRM_SIMPLE_MODE(hd, vd, RES_MM(hd), RES_MM(vd))
40
41 /*
42  * Helpers for simplefb
43  */
44
45 static int
46 simplefb_get_validated_int(struct drm_device *dev, const char *name,
47                            uint32_t value)
48 {
49         if (value > INT_MAX) {
50                 drm_err(dev, "simplefb: invalid framebuffer %s of %u\n",
51                         name, value);
52                 return -EINVAL;
53         }
54         return (int)value;
55 }
56
57 static int
58 simplefb_get_validated_int0(struct drm_device *dev, const char *name,
59                             uint32_t value)
60 {
61         if (!value) {
62                 drm_err(dev, "simplefb: invalid framebuffer %s of %u\n",
63                         name, value);
64                 return -EINVAL;
65         }
66         return simplefb_get_validated_int(dev, name, value);
67 }
68
69 static const struct drm_format_info *
70 simplefb_get_validated_format(struct drm_device *dev, const char *format_name)
71 {
72         static const struct simplefb_format formats[] = SIMPLEFB_FORMATS;
73         const struct simplefb_format *fmt = formats;
74         const struct simplefb_format *end = fmt + ARRAY_SIZE(formats);
75
76         if (!format_name) {
77                 drm_err(dev, "simplefb: missing framebuffer format\n");
78                 return ERR_PTR(-EINVAL);
79         }
80
81         while (fmt < end) {
82                 if (!strcmp(format_name, fmt->name))
83                         return drm_format_info(fmt->fourcc);
84                 ++fmt;
85         }
86
87         drm_err(dev, "simplefb: unknown framebuffer format %s\n",
88                 format_name);
89
90         return ERR_PTR(-EINVAL);
91 }
92
93 static int
94 simplefb_get_width_pd(struct drm_device *dev,
95                       const struct simplefb_platform_data *pd)
96 {
97         return simplefb_get_validated_int0(dev, "width", pd->width);
98 }
99
100 static int
101 simplefb_get_height_pd(struct drm_device *dev,
102                        const struct simplefb_platform_data *pd)
103 {
104         return simplefb_get_validated_int0(dev, "height", pd->height);
105 }
106
107 static int
108 simplefb_get_stride_pd(struct drm_device *dev,
109                        const struct simplefb_platform_data *pd)
110 {
111         return simplefb_get_validated_int(dev, "stride", pd->stride);
112 }
113
114 static const struct drm_format_info *
115 simplefb_get_format_pd(struct drm_device *dev,
116                        const struct simplefb_platform_data *pd)
117 {
118         return simplefb_get_validated_format(dev, pd->format);
119 }
120
121 static int
122 simplefb_read_u32_of(struct drm_device *dev, struct device_node *of_node,
123                      const char *name, u32 *value)
124 {
125         int ret = of_property_read_u32(of_node, name, value);
126
127         if (ret)
128                 drm_err(dev, "simplefb: cannot parse framebuffer %s: error %d\n",
129                         name, ret);
130         return ret;
131 }
132
133 static int
134 simplefb_read_string_of(struct drm_device *dev, struct device_node *of_node,
135                         const char *name, const char **value)
136 {
137         int ret = of_property_read_string(of_node, name, value);
138
139         if (ret)
140                 drm_err(dev, "simplefb: cannot parse framebuffer %s: error %d\n",
141                         name, ret);
142         return ret;
143 }
144
145 static int
146 simplefb_get_width_of(struct drm_device *dev, struct device_node *of_node)
147 {
148         u32 width;
149         int ret = simplefb_read_u32_of(dev, of_node, "width", &width);
150
151         if (ret)
152                 return ret;
153         return simplefb_get_validated_int0(dev, "width", width);
154 }
155
156 static int
157 simplefb_get_height_of(struct drm_device *dev, struct device_node *of_node)
158 {
159         u32 height;
160         int ret = simplefb_read_u32_of(dev, of_node, "height", &height);
161
162         if (ret)
163                 return ret;
164         return simplefb_get_validated_int0(dev, "height", height);
165 }
166
167 static int
168 simplefb_get_stride_of(struct drm_device *dev, struct device_node *of_node)
169 {
170         u32 stride;
171         int ret = simplefb_read_u32_of(dev, of_node, "stride", &stride);
172
173         if (ret)
174                 return ret;
175         return simplefb_get_validated_int(dev, "stride", stride);
176 }
177
178 static const struct drm_format_info *
179 simplefb_get_format_of(struct drm_device *dev, struct device_node *of_node)
180 {
181         const char *format;
182         int ret = simplefb_read_string_of(dev, of_node, "format", &format);
183
184         if (ret)
185                 return ERR_PTR(ret);
186         return simplefb_get_validated_format(dev, format);
187 }
188
189 /*
190  * Simple Framebuffer device
191  */
192
193 struct simpledrm_device {
194         struct drm_device dev;
195         struct platform_device *pdev;
196
197         /* clocks */
198 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
199         unsigned int clk_count;
200         struct clk **clks;
201 #endif
202         /* regulators */
203 #if defined CONFIG_OF && defined CONFIG_REGULATOR
204         unsigned int regulator_count;
205         struct regulator **regulators;
206 #endif
207
208         /* simplefb settings */
209         struct drm_display_mode mode;
210         const struct drm_format_info *format;
211         unsigned int pitch;
212
213         /* memory management */
214         struct resource *mem;
215         void __iomem *screen_base;
216
217         /* modesetting */
218         uint32_t formats[8];
219         size_t nformats;
220         struct drm_connector connector;
221         struct drm_simple_display_pipe pipe;
222 };
223
224 static struct simpledrm_device *simpledrm_device_of_dev(struct drm_device *dev)
225 {
226         return container_of(dev, struct simpledrm_device, dev);
227 }
228
229 /*
230  * Hardware
231  */
232
233 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
234 /*
235  * Clock handling code.
236  *
237  * Here we handle the clocks property of our "simple-framebuffer" dt node.
238  * This is necessary so that we can make sure that any clocks needed by
239  * the display engine that the bootloader set up for us (and for which it
240  * provided a simplefb dt node), stay up, for the life of the simplefb
241  * driver.
242  *
243  * When the driver unloads, we cleanly disable, and then release the clocks.
244  *
245  * We only complain about errors here, no action is taken as the most likely
246  * error can only happen due to a mismatch between the bootloader which set
247  * up simplefb, and the clock definitions in the device tree. Chances are
248  * that there are no adverse effects, and if there are, a clean teardown of
249  * the fb probe will not help us much either. So just complain and carry on,
250  * and hope that the user actually gets a working fb at the end of things.
251  */
252
253 static void simpledrm_device_release_clocks(void *res)
254 {
255         struct simpledrm_device *sdev = simpledrm_device_of_dev(res);
256         unsigned int i;
257
258         for (i = 0; i < sdev->clk_count; ++i) {
259                 if (sdev->clks[i]) {
260                         clk_disable_unprepare(sdev->clks[i]);
261                         clk_put(sdev->clks[i]);
262                 }
263         }
264 }
265
266 static int simpledrm_device_init_clocks(struct simpledrm_device *sdev)
267 {
268         struct drm_device *dev = &sdev->dev;
269         struct platform_device *pdev = sdev->pdev;
270         struct device_node *of_node = pdev->dev.of_node;
271         struct clk *clock;
272         unsigned int i;
273         int ret;
274
275         if (dev_get_platdata(&pdev->dev) || !of_node)
276                 return 0;
277
278         sdev->clk_count = of_clk_get_parent_count(of_node);
279         if (!sdev->clk_count)
280                 return 0;
281
282         sdev->clks = drmm_kzalloc(dev, sdev->clk_count * sizeof(sdev->clks[0]),
283                                   GFP_KERNEL);
284         if (!sdev->clks)
285                 return -ENOMEM;
286
287         for (i = 0; i < sdev->clk_count; ++i) {
288                 clock = of_clk_get(of_node, i);
289                 if (IS_ERR(clock)) {
290                         ret = PTR_ERR(clock);
291                         if (ret == -EPROBE_DEFER)
292                                 goto err;
293                         drm_err(dev, "clock %u not found: %d\n", i, ret);
294                         continue;
295                 }
296                 ret = clk_prepare_enable(clock);
297                 if (ret) {
298                         drm_err(dev, "failed to enable clock %u: %d\n",
299                                 i, ret);
300                         clk_put(clock);
301                 }
302                 sdev->clks[i] = clock;
303         }
304
305         return devm_add_action_or_reset(&pdev->dev,
306                                         simpledrm_device_release_clocks,
307                                         sdev);
308
309 err:
310         while (i) {
311                 --i;
312                 if (sdev->clks[i]) {
313                         clk_disable_unprepare(sdev->clks[i]);
314                         clk_put(sdev->clks[i]);
315                 }
316         }
317         return ret;
318 }
319 #else
320 static int simpledrm_device_init_clocks(struct simpledrm_device *sdev)
321 {
322         return 0;
323 }
324 #endif
325
326 #if defined CONFIG_OF && defined CONFIG_REGULATOR
327
328 #define SUPPLY_SUFFIX "-supply"
329
330 /*
331  * Regulator handling code.
332  *
333  * Here we handle the num-supplies and vin*-supply properties of our
334  * "simple-framebuffer" dt node. This is necessary so that we can make sure
335  * that any regulators needed by the display hardware that the bootloader
336  * set up for us (and for which it provided a simplefb dt node), stay up,
337  * for the life of the simplefb driver.
338  *
339  * When the driver unloads, we cleanly disable, and then release the
340  * regulators.
341  *
342  * We only complain about errors here, no action is taken as the most likely
343  * error can only happen due to a mismatch between the bootloader which set
344  * up simplefb, and the regulator definitions in the device tree. Chances are
345  * that there are no adverse effects, and if there are, a clean teardown of
346  * the fb probe will not help us much either. So just complain and carry on,
347  * and hope that the user actually gets a working fb at the end of things.
348  */
349
350 static void simpledrm_device_release_regulators(void *res)
351 {
352         struct simpledrm_device *sdev = simpledrm_device_of_dev(res);
353         unsigned int i;
354
355         for (i = 0; i < sdev->regulator_count; ++i) {
356                 if (sdev->regulators[i]) {
357                         regulator_disable(sdev->regulators[i]);
358                         regulator_put(sdev->regulators[i]);
359                 }
360         }
361 }
362
363 static int simpledrm_device_init_regulators(struct simpledrm_device *sdev)
364 {
365         struct drm_device *dev = &sdev->dev;
366         struct platform_device *pdev = sdev->pdev;
367         struct device_node *of_node = pdev->dev.of_node;
368         struct property *prop;
369         struct regulator *regulator;
370         const char *p;
371         unsigned int count = 0, i = 0;
372         int ret;
373
374         if (dev_get_platdata(&pdev->dev) || !of_node)
375                 return 0;
376
377         /* Count the number of regulator supplies */
378         for_each_property_of_node(of_node, prop) {
379                 p = strstr(prop->name, SUPPLY_SUFFIX);
380                 if (p && p != prop->name)
381                         ++count;
382         }
383
384         if (!count)
385                 return 0;
386
387         sdev->regulators = drmm_kzalloc(dev,
388                                         count * sizeof(sdev->regulators[0]),
389                                         GFP_KERNEL);
390         if (!sdev->regulators)
391                 return -ENOMEM;
392
393         for_each_property_of_node(of_node, prop) {
394                 char name[32]; /* 32 is max size of property name */
395                 size_t len;
396
397                 p = strstr(prop->name, SUPPLY_SUFFIX);
398                 if (!p || p == prop->name)
399                         continue;
400                 len = strlen(prop->name) - strlen(SUPPLY_SUFFIX) + 1;
401                 strscpy(name, prop->name, min(sizeof(name), len));
402
403                 regulator = regulator_get_optional(&pdev->dev, name);
404                 if (IS_ERR(regulator)) {
405                         ret = PTR_ERR(regulator);
406                         if (ret == -EPROBE_DEFER)
407                                 goto err;
408                         drm_err(dev, "regulator %s not found: %d\n",
409                                 name, ret);
410                         continue;
411                 }
412
413                 ret = regulator_enable(regulator);
414                 if (ret) {
415                         drm_err(dev, "failed to enable regulator %u: %d\n",
416                                 i, ret);
417                         regulator_put(regulator);
418                 }
419
420                 sdev->regulators[i++] = regulator;
421         }
422         sdev->regulator_count = i;
423
424         return devm_add_action_or_reset(&pdev->dev,
425                                         simpledrm_device_release_regulators,
426                                         sdev);
427
428 err:
429         while (i) {
430                 --i;
431                 if (sdev->regulators[i]) {
432                         regulator_disable(sdev->regulators[i]);
433                         regulator_put(sdev->regulators[i]);
434                 }
435         }
436         return ret;
437 }
438 #else
439 static int simpledrm_device_init_regulators(struct simpledrm_device *sdev)
440 {
441         return 0;
442 }
443 #endif
444
445 /*
446  *  Simplefb settings
447  */
448
449 static struct drm_display_mode simpledrm_mode(unsigned int width,
450                                               unsigned int height)
451 {
452         struct drm_display_mode mode = { SIMPLEDRM_MODE(width, height) };
453
454         mode.clock = 60 /* Hz */ * mode.hdisplay * mode.vdisplay;
455         drm_mode_set_name(&mode);
456
457         return mode;
458 }
459
460 static int simpledrm_device_init_fb(struct simpledrm_device *sdev)
461 {
462         int width, height, stride;
463         const struct drm_format_info *format;
464         struct drm_format_name_buf buf;
465         struct drm_device *dev = &sdev->dev;
466         struct platform_device *pdev = sdev->pdev;
467         const struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev);
468         struct device_node *of_node = pdev->dev.of_node;
469
470         if (pd) {
471                 width = simplefb_get_width_pd(dev, pd);
472                 if (width < 0)
473                         return width;
474                 height = simplefb_get_height_pd(dev, pd);
475                 if (height < 0)
476                         return height;
477                 stride = simplefb_get_stride_pd(dev, pd);
478                 if (stride < 0)
479                         return stride;
480                 format = simplefb_get_format_pd(dev, pd);
481                 if (IS_ERR(format))
482                         return PTR_ERR(format);
483         } else if (of_node) {
484                 width = simplefb_get_width_of(dev, of_node);
485                 if (width < 0)
486                         return width;
487                 height = simplefb_get_height_of(dev, of_node);
488                 if (height < 0)
489                         return height;
490                 stride = simplefb_get_stride_of(dev, of_node);
491                 if (stride < 0)
492                         return stride;
493                 format = simplefb_get_format_of(dev, of_node);
494                 if (IS_ERR(format))
495                         return PTR_ERR(format);
496         } else {
497                 drm_err(dev, "no simplefb configuration found\n");
498                 return -ENODEV;
499         }
500
501         sdev->mode = simpledrm_mode(width, height);
502         sdev->format = format;
503         sdev->pitch = stride;
504
505         drm_dbg_kms(dev, "display mode={" DRM_MODE_FMT "}\n",
506                     DRM_MODE_ARG(&sdev->mode));
507         drm_dbg_kms(dev,
508                     "framebuffer format=\"%s\", size=%dx%d, stride=%d byte\n",
509                     drm_get_format_name(format->format, &buf), width,
510                     height, stride);
511
512         return 0;
513 }
514
515 /*
516  * Memory management
517  */
518
519 static int simpledrm_device_init_mm(struct simpledrm_device *sdev)
520 {
521         struct drm_device *dev = &sdev->dev;
522         struct platform_device *pdev = sdev->pdev;
523         struct resource *mem;
524         void __iomem *screen_base;
525         int ret;
526
527         mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
528         if (!mem)
529                 return -EINVAL;
530
531         ret = devm_aperture_acquire_from_firmware(dev, mem->start, resource_size(mem));
532         if (ret) {
533                 drm_err(dev, "could not acquire memory range %pr: error %d\n",
534                         mem, ret);
535                 return ret;
536         }
537
538         screen_base = devm_ioremap_wc(&pdev->dev, mem->start,
539                                       resource_size(mem));
540         if (!screen_base)
541                 return -ENOMEM;
542
543         sdev->mem = mem;
544         sdev->screen_base = screen_base;
545
546         return 0;
547 }
548
549 /*
550  * Modesetting
551  */
552
553 /*
554  * Support all formats of simplefb and maybe more; in order
555  * of preference. The display's update function will do any
556  * conversion necessary.
557  *
558  * TODO: Add blit helpers for remaining formats and uncomment
559  *       constants.
560  */
561 static const uint32_t simpledrm_default_formats[] = {
562         DRM_FORMAT_XRGB8888,
563         DRM_FORMAT_ARGB8888,
564         DRM_FORMAT_RGB565,
565         //DRM_FORMAT_XRGB1555,
566         //DRM_FORMAT_ARGB1555,
567         DRM_FORMAT_RGB888,
568         //DRM_FORMAT_XRGB2101010,
569         //DRM_FORMAT_ARGB2101010,
570 };
571
572 static const uint64_t simpledrm_format_modifiers[] = {
573         DRM_FORMAT_MOD_LINEAR,
574         DRM_FORMAT_MOD_INVALID
575 };
576
577 static int simpledrm_connector_helper_get_modes(struct drm_connector *connector)
578 {
579         struct simpledrm_device *sdev = simpledrm_device_of_dev(connector->dev);
580         struct drm_display_mode *mode;
581
582         mode = drm_mode_duplicate(connector->dev, &sdev->mode);
583         if (!mode)
584                 return 0;
585
586         if (mode->name[0] == '\0')
587                 drm_mode_set_name(mode);
588
589         mode->type |= DRM_MODE_TYPE_PREFERRED;
590         drm_mode_probed_add(connector, mode);
591
592         if (mode->width_mm)
593                 connector->display_info.width_mm = mode->width_mm;
594         if (mode->height_mm)
595                 connector->display_info.height_mm = mode->height_mm;
596
597         return 1;
598 }
599
600 static const struct drm_connector_helper_funcs simpledrm_connector_helper_funcs = {
601         .get_modes = simpledrm_connector_helper_get_modes,
602 };
603
604 static const struct drm_connector_funcs simpledrm_connector_funcs = {
605         .reset = drm_atomic_helper_connector_reset,
606         .fill_modes = drm_helper_probe_single_connector_modes,
607         .destroy = drm_connector_cleanup,
608         .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
609         .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
610 };
611
612 static int
613 simpledrm_simple_display_pipe_mode_valid(struct drm_simple_display_pipe *pipe,
614                                     const struct drm_display_mode *mode)
615 {
616         struct simpledrm_device *sdev = simpledrm_device_of_dev(pipe->crtc.dev);
617
618         if (mode->hdisplay != sdev->mode.hdisplay &&
619             mode->vdisplay != sdev->mode.vdisplay)
620                 return MODE_ONE_SIZE;
621         else if (mode->hdisplay != sdev->mode.hdisplay)
622                 return MODE_ONE_WIDTH;
623         else if (mode->vdisplay != sdev->mode.vdisplay)
624                 return MODE_ONE_HEIGHT;
625
626         return MODE_OK;
627 }
628
629 static void
630 simpledrm_simple_display_pipe_enable(struct drm_simple_display_pipe *pipe,
631                                      struct drm_crtc_state *crtc_state,
632                                      struct drm_plane_state *plane_state)
633 {
634         struct simpledrm_device *sdev = simpledrm_device_of_dev(pipe->crtc.dev);
635         struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
636         struct drm_framebuffer *fb = plane_state->fb;
637         void *vmap = shadow_plane_state->map[0].vaddr; /* TODO: Use mapping abstraction properly */
638         struct drm_device *dev = &sdev->dev;
639         int idx;
640
641         if (!fb)
642                 return;
643
644         if (!drm_dev_enter(dev, &idx))
645                 return;
646
647         drm_fb_blit_dstclip(sdev->screen_base, sdev->pitch,
648                             sdev->format->format, vmap, fb);
649         drm_dev_exit(idx);
650 }
651
652 static void
653 simpledrm_simple_display_pipe_disable(struct drm_simple_display_pipe *pipe)
654 {
655         struct simpledrm_device *sdev = simpledrm_device_of_dev(pipe->crtc.dev);
656         struct drm_device *dev = &sdev->dev;
657         int idx;
658
659         if (!drm_dev_enter(dev, &idx))
660                 return;
661
662         /* Clear screen to black if disabled */
663         memset_io(sdev->screen_base, 0, sdev->pitch * sdev->mode.vdisplay);
664
665         drm_dev_exit(idx);
666 }
667
668 static void
669 simpledrm_simple_display_pipe_update(struct drm_simple_display_pipe *pipe,
670                                      struct drm_plane_state *old_plane_state)
671 {
672         struct simpledrm_device *sdev = simpledrm_device_of_dev(pipe->crtc.dev);
673         struct drm_plane_state *plane_state = pipe->plane.state;
674         struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
675         void *vmap = shadow_plane_state->map[0].vaddr; /* TODO: Use mapping abstraction properly */
676         struct drm_framebuffer *fb = plane_state->fb;
677         struct drm_device *dev = &sdev->dev;
678         struct drm_rect clip;
679         int idx;
680
681         if (!fb)
682                 return;
683
684         if (!drm_atomic_helper_damage_merged(old_plane_state, plane_state, &clip))
685                 return;
686
687         if (!drm_dev_enter(dev, &idx))
688                 return;
689
690         drm_fb_blit_rect_dstclip(sdev->screen_base, sdev->pitch,
691                                  sdev->format->format, vmap, fb, &clip);
692
693         drm_dev_exit(idx);
694 }
695
696 static const struct drm_simple_display_pipe_funcs
697 simpledrm_simple_display_pipe_funcs = {
698         .mode_valid = simpledrm_simple_display_pipe_mode_valid,
699         .enable = simpledrm_simple_display_pipe_enable,
700         .disable = simpledrm_simple_display_pipe_disable,
701         .update = simpledrm_simple_display_pipe_update,
702         DRM_GEM_SIMPLE_DISPLAY_PIPE_SHADOW_PLANE_FUNCS,
703 };
704
705 static const struct drm_mode_config_funcs simpledrm_mode_config_funcs = {
706         .fb_create = drm_gem_fb_create_with_dirty,
707         .atomic_check = drm_atomic_helper_check,
708         .atomic_commit = drm_atomic_helper_commit,
709 };
710
711 static const uint32_t *simpledrm_device_formats(struct simpledrm_device *sdev,
712                                                 size_t *nformats_out)
713 {
714         struct drm_device *dev = &sdev->dev;
715         size_t i;
716
717         if (sdev->nformats)
718                 goto out; /* don't rebuild list on recurring calls */
719
720         /* native format goes first */
721         sdev->formats[0] = sdev->format->format;
722         sdev->nformats = 1;
723
724         /* default formats go second */
725         for (i = 0; i < ARRAY_SIZE(simpledrm_default_formats); ++i) {
726                 if (simpledrm_default_formats[i] == sdev->format->format)
727                         continue; /* native format already went first */
728                 sdev->formats[sdev->nformats] = simpledrm_default_formats[i];
729                 sdev->nformats++;
730         }
731
732         /*
733          * TODO: The simpledrm driver converts framebuffers to the native
734          * format when copying them to device memory. If there are more
735          * formats listed than supported by the driver, the native format
736          * is not supported by the conversion helpers. Therefore *only*
737          * support the native format and add a conversion helper ASAP.
738          */
739         if (drm_WARN_ONCE(dev, i != sdev->nformats,
740                           "format conversion helpers required for %p4cc",
741                           &sdev->format->format)) {
742                 sdev->nformats = 1;
743         }
744
745 out:
746         *nformats_out = sdev->nformats;
747         return sdev->formats;
748 }
749
750 static int simpledrm_device_init_modeset(struct simpledrm_device *sdev)
751 {
752         struct drm_device *dev = &sdev->dev;
753         struct drm_display_mode *mode = &sdev->mode;
754         struct drm_connector *connector = &sdev->connector;
755         struct drm_simple_display_pipe *pipe = &sdev->pipe;
756         const uint32_t *formats;
757         size_t nformats;
758         int ret;
759
760         ret = drmm_mode_config_init(dev);
761         if (ret)
762                 return ret;
763
764         dev->mode_config.min_width = mode->hdisplay;
765         dev->mode_config.max_width = mode->hdisplay;
766         dev->mode_config.min_height = mode->vdisplay;
767         dev->mode_config.max_height = mode->vdisplay;
768         dev->mode_config.prefer_shadow_fbdev = true;
769         dev->mode_config.preferred_depth = sdev->format->cpp[0] * 8;
770         dev->mode_config.funcs = &simpledrm_mode_config_funcs;
771
772         ret = drm_connector_init(dev, connector, &simpledrm_connector_funcs,
773                                  DRM_MODE_CONNECTOR_Unknown);
774         if (ret)
775                 return ret;
776         drm_connector_helper_add(connector, &simpledrm_connector_helper_funcs);
777
778         formats = simpledrm_device_formats(sdev, &nformats);
779
780         ret = drm_simple_display_pipe_init(dev, pipe, &simpledrm_simple_display_pipe_funcs,
781                                            formats, nformats, simpledrm_format_modifiers,
782                                            connector);
783         if (ret)
784                 return ret;
785
786         drm_mode_config_reset(dev);
787
788         return 0;
789 }
790
791 /*
792  * Init / Cleanup
793  */
794
795 static struct simpledrm_device *
796 simpledrm_device_create(struct drm_driver *drv, struct platform_device *pdev)
797 {
798         struct simpledrm_device *sdev;
799         int ret;
800
801         sdev = devm_drm_dev_alloc(&pdev->dev, drv, struct simpledrm_device,
802                                   dev);
803         if (IS_ERR(sdev))
804                 return ERR_CAST(sdev);
805         sdev->pdev = pdev;
806         platform_set_drvdata(pdev, sdev);
807
808         ret = simpledrm_device_init_clocks(sdev);
809         if (ret)
810                 return ERR_PTR(ret);
811         ret = simpledrm_device_init_regulators(sdev);
812         if (ret)
813                 return ERR_PTR(ret);
814         ret = simpledrm_device_init_fb(sdev);
815         if (ret)
816                 return ERR_PTR(ret);
817         ret = simpledrm_device_init_mm(sdev);
818         if (ret)
819                 return ERR_PTR(ret);
820         ret = simpledrm_device_init_modeset(sdev);
821         if (ret)
822                 return ERR_PTR(ret);
823
824         return sdev;
825 }
826
827 /*
828  * DRM driver
829  */
830
831 DEFINE_DRM_GEM_FOPS(simpledrm_fops);
832
833 static struct drm_driver simpledrm_driver = {
834         DRM_GEM_SHMEM_DRIVER_OPS,
835         .name                   = DRIVER_NAME,
836         .desc                   = DRIVER_DESC,
837         .date                   = DRIVER_DATE,
838         .major                  = DRIVER_MAJOR,
839         .minor                  = DRIVER_MINOR,
840         .driver_features        = DRIVER_ATOMIC | DRIVER_GEM | DRIVER_MODESET,
841         .fops                   = &simpledrm_fops,
842 };
843
844 /*
845  * Platform driver
846  */
847
848 static int simpledrm_probe(struct platform_device *pdev)
849 {
850         struct simpledrm_device *sdev;
851         struct drm_device *dev;
852         int ret;
853
854         sdev = simpledrm_device_create(&simpledrm_driver, pdev);
855         if (IS_ERR(sdev))
856                 return PTR_ERR(sdev);
857         dev = &sdev->dev;
858
859         ret = drm_dev_register(dev, 0);
860         if (ret)
861                 return ret;
862
863         drm_fbdev_generic_setup(dev, 0);
864
865         return 0;
866 }
867
868 static int simpledrm_remove(struct platform_device *pdev)
869 {
870         struct simpledrm_device *sdev = platform_get_drvdata(pdev);
871         struct drm_device *dev = &sdev->dev;
872
873         drm_dev_unplug(dev);
874
875         return 0;
876 }
877
878 static const struct of_device_id simpledrm_of_match_table[] = {
879         { .compatible = "simple-framebuffer", },
880         { },
881 };
882 MODULE_DEVICE_TABLE(of, simpledrm_of_match_table);
883
884 static struct platform_driver simpledrm_platform_driver = {
885         .driver = {
886                 .name = "simple-framebuffer", /* connect to sysfb */
887                 .of_match_table = simpledrm_of_match_table,
888         },
889         .probe = simpledrm_probe,
890         .remove = simpledrm_remove,
891 };
892
893 module_platform_driver(simpledrm_platform_driver);
894
895 MODULE_DESCRIPTION(DRIVER_DESC);
896 MODULE_LICENSE("GPL v2");