1 // SPDX-License-Identifier: GPL-2.0-only
2
3 #include <linux/aperture.h>
4 #include <linux/clk.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>
12
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_damage_helper.h>
18 #include <drm/drm_device.h>
19 #include <drm/drm_drv.h>
20 #include <drm/drm_fbdev_shmem.h>
21 #include <drm/drm_framebuffer.h>
22 #include <drm/drm_gem_atomic_helper.h>
23 #include <drm/drm_gem_framebuffer_helper.h>
24 #include <drm/drm_gem_shmem_helper.h>
25 #include <drm/drm_managed.h>
26 #include <drm/drm_modeset_helper_vtables.h>
27 #include <drm/drm_probe_helper.h>
28
29 #include "drm_sysfb_helper.h"
30
31 #define DRIVER_NAME "simpledrm"
32 #define DRIVER_DESC "DRM driver for simple-framebuffer platform devices"
33 #define DRIVER_MAJOR 1
34 #define DRIVER_MINOR 0
35
36 /*
37 * Helpers for simplefb
38 */
39
40 static int
simplefb_get_validated_int(struct drm_device * dev,const char * name,uint32_t value)41 simplefb_get_validated_int(struct drm_device *dev, const char *name,
42 uint32_t value)
43 {
44 return drm_sysfb_get_validated_int(dev, name, value, INT_MAX);
45 }
46
47 static int
simplefb_get_validated_int0(struct drm_device * dev,const char * name,uint32_t value)48 simplefb_get_validated_int0(struct drm_device *dev, const char *name,
49 uint32_t value)
50 {
51 return drm_sysfb_get_validated_int0(dev, name, value, INT_MAX);
52 }
53
54 static const struct drm_format_info *
simplefb_get_validated_format(struct drm_device * dev,const char * format_name)55 simplefb_get_validated_format(struct drm_device *dev, const char *format_name)
56 {
57 static const struct simplefb_format formats[] = SIMPLEFB_FORMATS;
58 const struct simplefb_format *fmt = formats;
59 const struct simplefb_format *end = fmt + ARRAY_SIZE(formats);
60 const struct drm_format_info *info;
61
62 if (!format_name) {
63 drm_err(dev, "simplefb: missing framebuffer format\n");
64 return ERR_PTR(-EINVAL);
65 }
66
67 while (fmt < end) {
68 if (!strcmp(format_name, fmt->name)) {
69 info = drm_format_info(fmt->fourcc);
70 if (!info)
71 return ERR_PTR(-EINVAL);
72 return info;
73 }
74 ++fmt;
75 }
76
77 drm_err(dev, "simplefb: unknown framebuffer format %s\n",
78 format_name);
79
80 return ERR_PTR(-EINVAL);
81 }
82
83 static int
simplefb_get_width_pd(struct drm_device * dev,const struct simplefb_platform_data * pd)84 simplefb_get_width_pd(struct drm_device *dev,
85 const struct simplefb_platform_data *pd)
86 {
87 return simplefb_get_validated_int0(dev, "width", pd->width);
88 }
89
90 static int
simplefb_get_height_pd(struct drm_device * dev,const struct simplefb_platform_data * pd)91 simplefb_get_height_pd(struct drm_device *dev,
92 const struct simplefb_platform_data *pd)
93 {
94 return simplefb_get_validated_int0(dev, "height", pd->height);
95 }
96
97 static int
simplefb_get_stride_pd(struct drm_device * dev,const struct simplefb_platform_data * pd)98 simplefb_get_stride_pd(struct drm_device *dev,
99 const struct simplefb_platform_data *pd)
100 {
101 return simplefb_get_validated_int(dev, "stride", pd->stride);
102 }
103
104 static const struct drm_format_info *
simplefb_get_format_pd(struct drm_device * dev,const struct simplefb_platform_data * pd)105 simplefb_get_format_pd(struct drm_device *dev,
106 const struct simplefb_platform_data *pd)
107 {
108 return simplefb_get_validated_format(dev, pd->format);
109 }
110
111 static int
simplefb_read_u32_of(struct drm_device * dev,struct device_node * of_node,const char * name,u32 * value)112 simplefb_read_u32_of(struct drm_device *dev, struct device_node *of_node,
113 const char *name, u32 *value)
114 {
115 int ret = of_property_read_u32(of_node, name, value);
116
117 if (ret)
118 drm_err(dev, "simplefb: cannot parse framebuffer %s: error %d\n",
119 name, ret);
120 return ret;
121 }
122
123 static int
simplefb_read_string_of(struct drm_device * dev,struct device_node * of_node,const char * name,const char ** value)124 simplefb_read_string_of(struct drm_device *dev, struct device_node *of_node,
125 const char *name, const char **value)
126 {
127 int ret = of_property_read_string(of_node, name, value);
128
129 if (ret)
130 drm_err(dev, "simplefb: cannot parse framebuffer %s: error %d\n",
131 name, ret);
132 return ret;
133 }
134
135 static int
simplefb_get_width_of(struct drm_device * dev,struct device_node * of_node)136 simplefb_get_width_of(struct drm_device *dev, struct device_node *of_node)
137 {
138 u32 width;
139 int ret = simplefb_read_u32_of(dev, of_node, "width", &width);
140
141 if (ret)
142 return ret;
143 return simplefb_get_validated_int0(dev, "width", width);
144 }
145
146 static int
simplefb_get_height_of(struct drm_device * dev,struct device_node * of_node)147 simplefb_get_height_of(struct drm_device *dev, struct device_node *of_node)
148 {
149 u32 height;
150 int ret = simplefb_read_u32_of(dev, of_node, "height", &height);
151
152 if (ret)
153 return ret;
154 return simplefb_get_validated_int0(dev, "height", height);
155 }
156
157 static int
simplefb_get_stride_of(struct drm_device * dev,struct device_node * of_node)158 simplefb_get_stride_of(struct drm_device *dev, struct device_node *of_node)
159 {
160 u32 stride;
161 int ret = simplefb_read_u32_of(dev, of_node, "stride", &stride);
162
163 if (ret)
164 return ret;
165 return simplefb_get_validated_int(dev, "stride", stride);
166 }
167
168 static const struct drm_format_info *
simplefb_get_format_of(struct drm_device * dev,struct device_node * of_node)169 simplefb_get_format_of(struct drm_device *dev, struct device_node *of_node)
170 {
171 const char *format;
172 int ret = simplefb_read_string_of(dev, of_node, "format", &format);
173
174 if (ret)
175 return ERR_PTR(ret);
176 return simplefb_get_validated_format(dev, format);
177 }
178
179 static struct resource *
simplefb_get_memory_of(struct drm_device * dev,struct device_node * of_node)180 simplefb_get_memory_of(struct drm_device *dev, struct device_node *of_node)
181 {
182 struct device_node *np;
183 struct resource *res;
184 int err;
185
186 np = of_parse_phandle(of_node, "memory-region", 0);
187 if (!np)
188 return NULL;
189
190 res = devm_kzalloc(dev->dev, sizeof(*res), GFP_KERNEL);
191 if (!res)
192 return ERR_PTR(-ENOMEM);
193
194 err = of_address_to_resource(np, 0, res);
195 if (err)
196 return ERR_PTR(err);
197
198 if (of_property_present(of_node, "reg"))
199 drm_warn(dev, "preferring \"memory-region\" over \"reg\" property\n");
200
201 return res;
202 }
203
204 /*
205 * Simple Framebuffer device
206 */
207
208 struct simpledrm_device {
209 struct drm_sysfb_device sysfb;
210
211 /* clocks */
212 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
213 unsigned int clk_count;
214 struct clk **clks;
215 #endif
216 /* regulators */
217 #if defined CONFIG_OF && defined CONFIG_REGULATOR
218 unsigned int regulator_count;
219 struct regulator **regulators;
220 #endif
221 /* power-domains */
222 #if defined CONFIG_OF && defined CONFIG_PM_GENERIC_DOMAINS
223 int pwr_dom_count;
224 struct device **pwr_dom_devs;
225 struct device_link **pwr_dom_links;
226 #endif
227
228 /* modesetting */
229 u32 formats[DRM_SYSFB_PLANE_NFORMATS(1)];
230 struct drm_plane primary_plane;
231 struct drm_crtc crtc;
232 struct drm_encoder encoder;
233 struct drm_connector connector;
234 };
235
236 /*
237 * Hardware
238 */
239
240 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
241 /*
242 * Clock handling code.
243 *
244 * Here we handle the clocks property of our "simple-framebuffer" dt node.
245 * This is necessary so that we can make sure that any clocks needed by
246 * the display engine that the bootloader set up for us (and for which it
247 * provided a simplefb dt node), stay up, for the life of the simplefb
248 * driver.
249 *
250 * When the driver unloads, we cleanly disable, and then release the clocks.
251 *
252 * We only complain about errors here, no action is taken as the most likely
253 * error can only happen due to a mismatch between the bootloader which set
254 * up simplefb, and the clock definitions in the device tree. Chances are
255 * that there are no adverse effects, and if there are, a clean teardown of
256 * the fb probe will not help us much either. So just complain and carry on,
257 * and hope that the user actually gets a working fb at the end of things.
258 */
259
simpledrm_device_release_clocks(void * res)260 static void simpledrm_device_release_clocks(void *res)
261 {
262 struct simpledrm_device *sdev = res;
263 unsigned int i;
264
265 for (i = 0; i < sdev->clk_count; ++i) {
266 if (sdev->clks[i]) {
267 clk_disable_unprepare(sdev->clks[i]);
268 clk_put(sdev->clks[i]);
269 }
270 }
271 }
272
simpledrm_device_init_clocks(struct simpledrm_device * sdev)273 static int simpledrm_device_init_clocks(struct simpledrm_device *sdev)
274 {
275 struct drm_device *dev = &sdev->sysfb.dev;
276 struct platform_device *pdev = to_platform_device(dev->dev);
277 struct device_node *of_node = pdev->dev.of_node;
278 struct clk *clock;
279 unsigned int i;
280 int ret;
281
282 if (dev_get_platdata(&pdev->dev) || !of_node)
283 return 0;
284
285 sdev->clk_count = of_clk_get_parent_count(of_node);
286 if (!sdev->clk_count)
287 return 0;
288
289 sdev->clks = drmm_kzalloc(dev, sdev->clk_count * sizeof(sdev->clks[0]),
290 GFP_KERNEL);
291 if (!sdev->clks)
292 return -ENOMEM;
293
294 for (i = 0; i < sdev->clk_count; ++i) {
295 clock = of_clk_get(of_node, i);
296 if (IS_ERR(clock)) {
297 ret = PTR_ERR(clock);
298 if (ret == -EPROBE_DEFER)
299 goto err;
300 drm_err(dev, "clock %u not found: %d\n", i, ret);
301 continue;
302 }
303 ret = clk_prepare_enable(clock);
304 if (ret) {
305 drm_err(dev, "failed to enable clock %u: %d\n",
306 i, ret);
307 clk_put(clock);
308 continue;
309 }
310 sdev->clks[i] = clock;
311 }
312
313 return devm_add_action_or_reset(&pdev->dev,
314 simpledrm_device_release_clocks,
315 sdev);
316
317 err:
318 while (i) {
319 --i;
320 if (sdev->clks[i]) {
321 clk_disable_unprepare(sdev->clks[i]);
322 clk_put(sdev->clks[i]);
323 }
324 }
325 return ret;
326 }
327 #else
simpledrm_device_init_clocks(struct simpledrm_device * sdev)328 static int simpledrm_device_init_clocks(struct simpledrm_device *sdev)
329 {
330 return 0;
331 }
332 #endif
333
334 #if defined CONFIG_OF && defined CONFIG_REGULATOR
335
336 #define SUPPLY_SUFFIX "-supply"
337
338 /*
339 * Regulator handling code.
340 *
341 * Here we handle the num-supplies and vin*-supply properties of our
342 * "simple-framebuffer" dt node. This is necessary so that we can make sure
343 * that any regulators needed by the display hardware that the bootloader
344 * set up for us (and for which it provided a simplefb dt node), stay up,
345 * for the life of the simplefb driver.
346 *
347 * When the driver unloads, we cleanly disable, and then release the
348 * regulators.
349 *
350 * We only complain about errors here, no action is taken as the most likely
351 * error can only happen due to a mismatch between the bootloader which set
352 * up simplefb, and the regulator definitions in the device tree. Chances are
353 * that there are no adverse effects, and if there are, a clean teardown of
354 * the fb probe will not help us much either. So just complain and carry on,
355 * and hope that the user actually gets a working fb at the end of things.
356 */
357
simpledrm_device_release_regulators(void * res)358 static void simpledrm_device_release_regulators(void *res)
359 {
360 struct simpledrm_device *sdev = res;
361 unsigned int i;
362
363 for (i = 0; i < sdev->regulator_count; ++i) {
364 if (sdev->regulators[i]) {
365 regulator_disable(sdev->regulators[i]);
366 regulator_put(sdev->regulators[i]);
367 }
368 }
369 }
370
simpledrm_device_init_regulators(struct simpledrm_device * sdev)371 static int simpledrm_device_init_regulators(struct simpledrm_device *sdev)
372 {
373 struct drm_device *dev = &sdev->sysfb.dev;
374 struct platform_device *pdev = to_platform_device(dev->dev);
375 struct device_node *of_node = pdev->dev.of_node;
376 struct property *prop;
377 struct regulator *regulator;
378 const char *p;
379 unsigned int count = 0, i = 0;
380 int ret;
381
382 if (dev_get_platdata(&pdev->dev) || !of_node)
383 return 0;
384
385 /* Count the number of regulator supplies */
386 for_each_property_of_node(of_node, prop) {
387 p = strstr(prop->name, SUPPLY_SUFFIX);
388 if (p && p != prop->name)
389 ++count;
390 }
391
392 if (!count)
393 return 0;
394
395 sdev->regulators = drmm_kzalloc(dev,
396 count * sizeof(sdev->regulators[0]),
397 GFP_KERNEL);
398 if (!sdev->regulators)
399 return -ENOMEM;
400
401 for_each_property_of_node(of_node, prop) {
402 char name[32]; /* 32 is max size of property name */
403 size_t len;
404
405 p = strstr(prop->name, SUPPLY_SUFFIX);
406 if (!p || p == prop->name)
407 continue;
408 len = strlen(prop->name) - strlen(SUPPLY_SUFFIX) + 1;
409 strscpy(name, prop->name, min(sizeof(name), len));
410
411 regulator = regulator_get_optional(&pdev->dev, name);
412 if (IS_ERR(regulator)) {
413 ret = PTR_ERR(regulator);
414 if (ret == -EPROBE_DEFER)
415 goto err;
416 drm_err(dev, "regulator %s not found: %d\n",
417 name, ret);
418 continue;
419 }
420
421 ret = regulator_enable(regulator);
422 if (ret) {
423 drm_err(dev, "failed to enable regulator %u: %d\n",
424 i, ret);
425 regulator_put(regulator);
426 continue;
427 }
428
429 sdev->regulators[i++] = regulator;
430 }
431 sdev->regulator_count = i;
432
433 return devm_add_action_or_reset(&pdev->dev,
434 simpledrm_device_release_regulators,
435 sdev);
436
437 err:
438 while (i) {
439 --i;
440 if (sdev->regulators[i]) {
441 regulator_disable(sdev->regulators[i]);
442 regulator_put(sdev->regulators[i]);
443 }
444 }
445 return ret;
446 }
447 #else
simpledrm_device_init_regulators(struct simpledrm_device * sdev)448 static int simpledrm_device_init_regulators(struct simpledrm_device *sdev)
449 {
450 return 0;
451 }
452 #endif
453
454 #if defined CONFIG_OF && defined CONFIG_PM_GENERIC_DOMAINS
455 /*
456 * Generic power domain handling code.
457 *
458 * Here we handle the power-domains properties of our "simple-framebuffer"
459 * dt node. This is only necessary if there is more than one power-domain.
460 * A single power-domains is handled automatically by the driver core. Multiple
461 * power-domains have to be handled by drivers since the driver core can't know
462 * the correct power sequencing. Power sequencing is not an issue for simpledrm
463 * since the bootloader has put the power domains already in the correct state.
464 * simpledrm has only to ensure they remain active for its lifetime.
465 *
466 * When the driver unloads, we detach from the power-domains.
467 *
468 * We only complain about errors here, no action is taken as the most likely
469 * error can only happen due to a mismatch between the bootloader which set
470 * up the "simple-framebuffer" dt node, and the PM domain providers in the
471 * device tree. Chances are that there are no adverse effects, and if there are,
472 * a clean teardown of the fb probe will not help us much either. So just
473 * complain and carry on, and hope that the user actually gets a working fb at
474 * the end of things.
475 */
simpledrm_device_detach_genpd(void * res)476 static void simpledrm_device_detach_genpd(void *res)
477 {
478 int i;
479 struct simpledrm_device *sdev = res;
480
481 if (sdev->pwr_dom_count <= 1)
482 return;
483
484 for (i = sdev->pwr_dom_count - 1; i >= 0; i--) {
485 if (sdev->pwr_dom_links[i])
486 device_link_del(sdev->pwr_dom_links[i]);
487 if (!IS_ERR_OR_NULL(sdev->pwr_dom_devs[i]))
488 dev_pm_domain_detach(sdev->pwr_dom_devs[i], true);
489 }
490 }
491
simpledrm_device_attach_genpd(struct simpledrm_device * sdev)492 static int simpledrm_device_attach_genpd(struct simpledrm_device *sdev)
493 {
494 struct device *dev = sdev->sysfb.dev.dev;
495 int i;
496
497 sdev->pwr_dom_count = of_count_phandle_with_args(dev->of_node, "power-domains",
498 "#power-domain-cells");
499 /*
500 * Single power-domain devices are handled by driver core nothing to do
501 * here. The same for device nodes without "power-domains" property.
502 */
503 if (sdev->pwr_dom_count <= 1)
504 return 0;
505
506 sdev->pwr_dom_devs = devm_kcalloc(dev, sdev->pwr_dom_count,
507 sizeof(*sdev->pwr_dom_devs),
508 GFP_KERNEL);
509 if (!sdev->pwr_dom_devs)
510 return -ENOMEM;
511
512 sdev->pwr_dom_links = devm_kcalloc(dev, sdev->pwr_dom_count,
513 sizeof(*sdev->pwr_dom_links),
514 GFP_KERNEL);
515 if (!sdev->pwr_dom_links)
516 return -ENOMEM;
517
518 for (i = 0; i < sdev->pwr_dom_count; i++) {
519 sdev->pwr_dom_devs[i] = dev_pm_domain_attach_by_id(dev, i);
520 if (IS_ERR(sdev->pwr_dom_devs[i])) {
521 int ret = PTR_ERR(sdev->pwr_dom_devs[i]);
522 if (ret == -EPROBE_DEFER) {
523 simpledrm_device_detach_genpd(sdev);
524 return ret;
525 }
526 drm_warn(&sdev->sysfb.dev,
527 "pm_domain_attach_by_id(%u) failed: %d\n", i, ret);
528 continue;
529 }
530
531 sdev->pwr_dom_links[i] = device_link_add(dev,
532 sdev->pwr_dom_devs[i],
533 DL_FLAG_STATELESS |
534 DL_FLAG_PM_RUNTIME |
535 DL_FLAG_RPM_ACTIVE);
536 if (!sdev->pwr_dom_links[i])
537 drm_warn(&sdev->sysfb.dev, "failed to link power-domain %d\n", i);
538 }
539
540 return devm_add_action_or_reset(dev, simpledrm_device_detach_genpd, sdev);
541 }
542 #else
simpledrm_device_attach_genpd(struct simpledrm_device * sdev)543 static int simpledrm_device_attach_genpd(struct simpledrm_device *sdev)
544 {
545 return 0;
546 }
547 #endif
548
549 /*
550 * Modesetting
551 */
552
553 static const u64 simpledrm_primary_plane_format_modifiers[] = {
554 DRM_SYSFB_PLANE_FORMAT_MODIFIERS,
555 };
556
557 static const struct drm_plane_helper_funcs simpledrm_primary_plane_helper_funcs = {
558 DRM_SYSFB_PLANE_HELPER_FUNCS,
559 };
560
561 static const struct drm_plane_funcs simpledrm_primary_plane_funcs = {
562 DRM_SYSFB_PLANE_FUNCS,
563 .destroy = drm_plane_cleanup,
564 };
565
566 static const struct drm_crtc_helper_funcs simpledrm_crtc_helper_funcs = {
567 DRM_SYSFB_CRTC_HELPER_FUNCS,
568 };
569
570 static const struct drm_crtc_funcs simpledrm_crtc_funcs = {
571 DRM_SYSFB_CRTC_FUNCS,
572 .destroy = drm_crtc_cleanup,
573 };
574
575 static const struct drm_encoder_funcs simpledrm_encoder_funcs = {
576 .destroy = drm_encoder_cleanup,
577 };
578
579 static const struct drm_connector_helper_funcs simpledrm_connector_helper_funcs = {
580 DRM_SYSFB_CONNECTOR_HELPER_FUNCS,
581 };
582
583 static const struct drm_connector_funcs simpledrm_connector_funcs = {
584 DRM_SYSFB_CONNECTOR_FUNCS,
585 .destroy = drm_connector_cleanup,
586 };
587
588 static const struct drm_mode_config_funcs simpledrm_mode_config_funcs = {
589 DRM_SYSFB_MODE_CONFIG_FUNCS,
590 };
591
592 /*
593 * Init / Cleanup
594 */
595
simpledrm_device_create(struct drm_driver * drv,struct platform_device * pdev)596 static struct simpledrm_device *simpledrm_device_create(struct drm_driver *drv,
597 struct platform_device *pdev)
598 {
599 const struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev);
600 struct device_node *of_node = pdev->dev.of_node;
601 struct simpledrm_device *sdev;
602 struct drm_sysfb_device *sysfb;
603 struct drm_device *dev;
604 int width, height, stride;
605 int width_mm = 0, height_mm = 0;
606 struct device_node *panel_node;
607 const struct drm_format_info *format;
608 struct resource *res, *mem = NULL;
609 struct drm_plane *primary_plane;
610 struct drm_crtc *crtc;
611 struct drm_encoder *encoder;
612 struct drm_connector *connector;
613 unsigned long max_width, max_height;
614 size_t nformats;
615 int ret;
616
617 sdev = devm_drm_dev_alloc(&pdev->dev, drv, struct simpledrm_device, sysfb.dev);
618 if (IS_ERR(sdev))
619 return ERR_CAST(sdev);
620 sysfb = &sdev->sysfb;
621 dev = &sysfb->dev;
622 platform_set_drvdata(pdev, sdev);
623
624 /*
625 * Hardware settings
626 */
627
628 ret = simpledrm_device_init_clocks(sdev);
629 if (ret)
630 return ERR_PTR(ret);
631 ret = simpledrm_device_init_regulators(sdev);
632 if (ret)
633 return ERR_PTR(ret);
634 ret = simpledrm_device_attach_genpd(sdev);
635 if (ret)
636 return ERR_PTR(ret);
637
638 if (pd) {
639 width = simplefb_get_width_pd(dev, pd);
640 if (width < 0)
641 return ERR_PTR(width);
642 height = simplefb_get_height_pd(dev, pd);
643 if (height < 0)
644 return ERR_PTR(height);
645 stride = simplefb_get_stride_pd(dev, pd);
646 if (stride < 0)
647 return ERR_PTR(stride);
648 format = simplefb_get_format_pd(dev, pd);
649 if (IS_ERR(format))
650 return ERR_CAST(format);
651 } else if (of_node) {
652 width = simplefb_get_width_of(dev, of_node);
653 if (width < 0)
654 return ERR_PTR(width);
655 height = simplefb_get_height_of(dev, of_node);
656 if (height < 0)
657 return ERR_PTR(height);
658 stride = simplefb_get_stride_of(dev, of_node);
659 if (stride < 0)
660 return ERR_PTR(stride);
661 format = simplefb_get_format_of(dev, of_node);
662 if (IS_ERR(format))
663 return ERR_CAST(format);
664 mem = simplefb_get_memory_of(dev, of_node);
665 if (IS_ERR(mem))
666 return ERR_CAST(mem);
667 panel_node = of_parse_phandle(of_node, "panel", 0);
668 if (panel_node) {
669 simplefb_read_u32_of(dev, panel_node, "width-mm", &width_mm);
670 simplefb_read_u32_of(dev, panel_node, "height-mm", &height_mm);
671 of_node_put(panel_node);
672 }
673 } else {
674 drm_err(dev, "no simplefb configuration found\n");
675 return ERR_PTR(-ENODEV);
676 }
677 if (!stride) {
678 stride = drm_format_info_min_pitch(format, 0, width);
679 if (drm_WARN_ON(dev, !stride))
680 return ERR_PTR(-EINVAL);
681 }
682
683 sysfb->fb_mode = drm_sysfb_mode(width, height, width_mm, height_mm);
684 sysfb->fb_format = format;
685 sysfb->fb_pitch = stride;
686
687 drm_dbg(dev, "display mode={" DRM_MODE_FMT "}\n", DRM_MODE_ARG(&sysfb->fb_mode));
688 drm_dbg(dev, "framebuffer format=%p4cc, size=%dx%d, stride=%d byte\n",
689 &format->format, width, height, stride);
690
691 /*
692 * Memory management
693 */
694
695 if (mem) {
696 void *screen_base;
697
698 ret = devm_aperture_acquire_for_platform_device(pdev, mem->start,
699 resource_size(mem));
700 if (ret) {
701 drm_err(dev, "could not acquire memory range %pr: %d\n", mem, ret);
702 return ERR_PTR(ret);
703 }
704
705 drm_dbg(dev, "using system memory framebuffer at %pr\n", mem);
706
707 screen_base = devm_memremap(dev->dev, mem->start, resource_size(mem), MEMREMAP_WC);
708 if (IS_ERR(screen_base))
709 return screen_base;
710
711 iosys_map_set_vaddr(&sysfb->fb_addr, screen_base);
712 } else {
713 void __iomem *screen_base;
714
715 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
716 if (!res)
717 return ERR_PTR(-EINVAL);
718
719 ret = devm_aperture_acquire_for_platform_device(pdev, res->start,
720 resource_size(res));
721 if (ret) {
722 drm_err(dev, "could not acquire memory range %pr: %d\n", res, ret);
723 return ERR_PTR(ret);
724 }
725
726 drm_dbg(dev, "using I/O memory framebuffer at %pr\n", res);
727
728 mem = devm_request_mem_region(&pdev->dev, res->start, resource_size(res),
729 drv->name);
730 if (!mem) {
731 /*
732 * We cannot make this fatal. Sometimes this comes from magic
733 * spaces our resource handlers simply don't know about. Use
734 * the I/O-memory resource as-is and try to map that instead.
735 */
736 drm_warn(dev, "could not acquire memory region %pr\n", res);
737 mem = res;
738 }
739
740 screen_base = devm_ioremap_wc(&pdev->dev, mem->start, resource_size(mem));
741 if (!screen_base)
742 return ERR_PTR(-ENOMEM);
743
744 iosys_map_set_vaddr_iomem(&sysfb->fb_addr, screen_base);
745 }
746
747 /*
748 * Modesetting
749 */
750
751 ret = drmm_mode_config_init(dev);
752 if (ret)
753 return ERR_PTR(ret);
754
755 max_width = max_t(unsigned long, width, DRM_SHADOW_PLANE_MAX_WIDTH);
756 max_height = max_t(unsigned long, height, DRM_SHADOW_PLANE_MAX_HEIGHT);
757
758 dev->mode_config.min_width = width;
759 dev->mode_config.max_width = max_width;
760 dev->mode_config.min_height = height;
761 dev->mode_config.max_height = max_height;
762 dev->mode_config.preferred_depth = format->depth;
763 dev->mode_config.funcs = &simpledrm_mode_config_funcs;
764
765 /* Primary plane */
766
767 nformats = drm_sysfb_build_fourcc_list(dev, &format->format, 1,
768 sdev->formats, ARRAY_SIZE(sdev->formats));
769
770 primary_plane = &sdev->primary_plane;
771 ret = drm_universal_plane_init(dev, primary_plane, 0, &simpledrm_primary_plane_funcs,
772 sdev->formats, nformats,
773 simpledrm_primary_plane_format_modifiers,
774 DRM_PLANE_TYPE_PRIMARY, NULL);
775 if (ret)
776 return ERR_PTR(ret);
777 drm_plane_helper_add(primary_plane, &simpledrm_primary_plane_helper_funcs);
778 drm_plane_enable_fb_damage_clips(primary_plane);
779
780 /* CRTC */
781
782 crtc = &sdev->crtc;
783 ret = drm_crtc_init_with_planes(dev, crtc, primary_plane, NULL,
784 &simpledrm_crtc_funcs, NULL);
785 if (ret)
786 return ERR_PTR(ret);
787 drm_crtc_helper_add(crtc, &simpledrm_crtc_helper_funcs);
788
789 /* Encoder */
790
791 encoder = &sdev->encoder;
792 ret = drm_encoder_init(dev, encoder, &simpledrm_encoder_funcs,
793 DRM_MODE_ENCODER_NONE, NULL);
794 if (ret)
795 return ERR_PTR(ret);
796 encoder->possible_crtcs = drm_crtc_mask(crtc);
797
798 /* Connector */
799
800 connector = &sdev->connector;
801 ret = drm_connector_init(dev, connector, &simpledrm_connector_funcs,
802 DRM_MODE_CONNECTOR_Unknown);
803 if (ret)
804 return ERR_PTR(ret);
805 drm_connector_helper_add(connector, &simpledrm_connector_helper_funcs);
806 drm_connector_set_panel_orientation_with_quirk(connector,
807 DRM_MODE_PANEL_ORIENTATION_UNKNOWN,
808 width, height);
809
810 ret = drm_connector_attach_encoder(connector, encoder);
811 if (ret)
812 return ERR_PTR(ret);
813
814 drm_mode_config_reset(dev);
815
816 return sdev;
817 }
818
819 /*
820 * DRM driver
821 */
822
823 DEFINE_DRM_GEM_FOPS(simpledrm_fops);
824
825 static struct drm_driver simpledrm_driver = {
826 DRM_GEM_SHMEM_DRIVER_OPS,
827 DRM_FBDEV_SHMEM_DRIVER_OPS,
828 .name = DRIVER_NAME,
829 .desc = DRIVER_DESC,
830 .major = DRIVER_MAJOR,
831 .minor = DRIVER_MINOR,
832 .driver_features = DRIVER_ATOMIC | DRIVER_GEM | DRIVER_MODESET,
833 .fops = &simpledrm_fops,
834 };
835
836 /*
837 * Platform driver
838 */
839
simpledrm_probe(struct platform_device * pdev)840 static int simpledrm_probe(struct platform_device *pdev)
841 {
842 struct simpledrm_device *sdev;
843 struct drm_sysfb_device *sysfb;
844 struct drm_device *dev;
845 int ret;
846
847 sdev = simpledrm_device_create(&simpledrm_driver, pdev);
848 if (IS_ERR(sdev))
849 return PTR_ERR(sdev);
850 sysfb = &sdev->sysfb;
851 dev = &sysfb->dev;
852
853 ret = drm_dev_register(dev, 0);
854 if (ret)
855 return ret;
856
857 drm_client_setup(dev, sdev->sysfb.fb_format);
858
859 return 0;
860 }
861
simpledrm_remove(struct platform_device * pdev)862 static void simpledrm_remove(struct platform_device *pdev)
863 {
864 struct simpledrm_device *sdev = platform_get_drvdata(pdev);
865 struct drm_device *dev = &sdev->sysfb.dev;
866
867 drm_dev_unplug(dev);
868 }
869
870 static const struct of_device_id simpledrm_of_match_table[] = {
871 { .compatible = "simple-framebuffer", },
872 { },
873 };
874 MODULE_DEVICE_TABLE(of, simpledrm_of_match_table);
875
876 static struct platform_driver simpledrm_platform_driver = {
877 .driver = {
878 .name = "simple-framebuffer", /* connect to sysfb */
879 .of_match_table = simpledrm_of_match_table,
880 },
881 .probe = simpledrm_probe,
882 .remove = simpledrm_remove,
883 };
884
885 module_platform_driver(simpledrm_platform_driver);
886
887 MODULE_DESCRIPTION(DRIVER_DESC);
888 MODULE_LICENSE("GPL v2");
889