Loading...
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/drm_atomic.h>
14#include <drm/drm_atomic_state_helper.h>
15#include <drm/drm_client_setup.h>
16#include <drm/drm_connector.h>
17#include <drm/drm_crtc_helper.h>
18#include <drm/drm_damage_helper.h>
19#include <drm/drm_device.h>
20#include <drm/drm_drv.h>
21#include <drm/drm_fbdev_shmem.h>
22#include <drm/drm_format_helper.h>
23#include <drm/drm_framebuffer.h>
24#include <drm/drm_gem_atomic_helper.h>
25#include <drm/drm_gem_framebuffer_helper.h>
26#include <drm/drm_gem_shmem_helper.h>
27#include <drm/drm_managed.h>
28#include <drm/drm_modeset_helper_vtables.h>
29#include <drm/drm_panic.h>
30#include <drm/drm_probe_helper.h>
31
32#define DRIVER_NAME "simpledrm"
33#define DRIVER_DESC "DRM driver for simple-framebuffer platform devices"
34#define DRIVER_DATE "20200625"
35#define DRIVER_MAJOR 1
36#define DRIVER_MINOR 0
37
38/*
39 * Helpers for simplefb
40 */
41
42static int
43simplefb_get_validated_int(struct drm_device *dev, const char *name,
44 uint32_t value)
45{
46 if (value > INT_MAX) {
47 drm_err(dev, "simplefb: invalid framebuffer %s of %u\n",
48 name, value);
49 return -EINVAL;
50 }
51 return (int)value;
52}
53
54static int
55simplefb_get_validated_int0(struct drm_device *dev, const char *name,
56 uint32_t value)
57{
58 if (!value) {
59 drm_err(dev, "simplefb: invalid framebuffer %s of %u\n",
60 name, value);
61 return -EINVAL;
62 }
63 return simplefb_get_validated_int(dev, name, value);
64}
65
66static const struct drm_format_info *
67simplefb_get_validated_format(struct drm_device *dev, const char *format_name)
68{
69 static const struct simplefb_format formats[] = SIMPLEFB_FORMATS;
70 const struct simplefb_format *fmt = formats;
71 const struct simplefb_format *end = fmt + ARRAY_SIZE(formats);
72 const struct drm_format_info *info;
73
74 if (!format_name) {
75 drm_err(dev, "simplefb: missing framebuffer format\n");
76 return ERR_PTR(-EINVAL);
77 }
78
79 while (fmt < end) {
80 if (!strcmp(format_name, fmt->name)) {
81 info = drm_format_info(fmt->fourcc);
82 if (!info)
83 return ERR_PTR(-EINVAL);
84 return info;
85 }
86 ++fmt;
87 }
88
89 drm_err(dev, "simplefb: unknown framebuffer format %s\n",
90 format_name);
91
92 return ERR_PTR(-EINVAL);
93}
94
95static int
96simplefb_get_width_pd(struct drm_device *dev,
97 const struct simplefb_platform_data *pd)
98{
99 return simplefb_get_validated_int0(dev, "width", pd->width);
100}
101
102static int
103simplefb_get_height_pd(struct drm_device *dev,
104 const struct simplefb_platform_data *pd)
105{
106 return simplefb_get_validated_int0(dev, "height", pd->height);
107}
108
109static int
110simplefb_get_stride_pd(struct drm_device *dev,
111 const struct simplefb_platform_data *pd)
112{
113 return simplefb_get_validated_int(dev, "stride", pd->stride);
114}
115
116static const struct drm_format_info *
117simplefb_get_format_pd(struct drm_device *dev,
118 const struct simplefb_platform_data *pd)
119{
120 return simplefb_get_validated_format(dev, pd->format);
121}
122
123static int
124simplefb_read_u32_of(struct drm_device *dev, struct device_node *of_node,
125 const char *name, u32 *value)
126{
127 int ret = of_property_read_u32(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
135static int
136simplefb_read_string_of(struct drm_device *dev, struct device_node *of_node,
137 const char *name, const char **value)
138{
139 int ret = of_property_read_string(of_node, name, value);
140
141 if (ret)
142 drm_err(dev, "simplefb: cannot parse framebuffer %s: error %d\n",
143 name, ret);
144 return ret;
145}
146
147static int
148simplefb_get_width_of(struct drm_device *dev, struct device_node *of_node)
149{
150 u32 width;
151 int ret = simplefb_read_u32_of(dev, of_node, "width", &width);
152
153 if (ret)
154 return ret;
155 return simplefb_get_validated_int0(dev, "width", width);
156}
157
158static int
159simplefb_get_height_of(struct drm_device *dev, struct device_node *of_node)
160{
161 u32 height;
162 int ret = simplefb_read_u32_of(dev, of_node, "height", &height);
163
164 if (ret)
165 return ret;
166 return simplefb_get_validated_int0(dev, "height", height);
167}
168
169static int
170simplefb_get_stride_of(struct drm_device *dev, struct device_node *of_node)
171{
172 u32 stride;
173 int ret = simplefb_read_u32_of(dev, of_node, "stride", &stride);
174
175 if (ret)
176 return ret;
177 return simplefb_get_validated_int(dev, "stride", stride);
178}
179
180static const struct drm_format_info *
181simplefb_get_format_of(struct drm_device *dev, struct device_node *of_node)
182{
183 const char *format;
184 int ret = simplefb_read_string_of(dev, of_node, "format", &format);
185
186 if (ret)
187 return ERR_PTR(ret);
188 return simplefb_get_validated_format(dev, format);
189}
190
191static struct resource *
192simplefb_get_memory_of(struct drm_device *dev, struct device_node *of_node)
193{
194 struct device_node *np;
195 struct resource *res;
196 int err;
197
198 np = of_parse_phandle(of_node, "memory-region", 0);
199 if (!np)
200 return NULL;
201
202 res = devm_kzalloc(dev->dev, sizeof(*res), GFP_KERNEL);
203 if (!res)
204 return ERR_PTR(-ENOMEM);
205
206 err = of_address_to_resource(np, 0, res);
207 if (err)
208 return ERR_PTR(err);
209
210 if (of_property_present(of_node, "reg"))
211 drm_warn(dev, "preferring \"memory-region\" over \"reg\" property\n");
212
213 return res;
214}
215
216/*
217 * Simple Framebuffer device
218 */
219
220struct simpledrm_device {
221 struct drm_device dev;
222
223 /* clocks */
224#if defined CONFIG_OF && defined CONFIG_COMMON_CLK
225 unsigned int clk_count;
226 struct clk **clks;
227#endif
228 /* regulators */
229#if defined CONFIG_OF && defined CONFIG_REGULATOR
230 unsigned int regulator_count;
231 struct regulator **regulators;
232#endif
233 /* power-domains */
234#if defined CONFIG_OF && defined CONFIG_PM_GENERIC_DOMAINS
235 int pwr_dom_count;
236 struct device **pwr_dom_devs;
237 struct device_link **pwr_dom_links;
238#endif
239
240 /* simplefb settings */
241 struct drm_display_mode mode;
242 const struct drm_format_info *format;
243 unsigned int pitch;
244
245 /* memory management */
246 struct iosys_map screen_base;
247
248 /* modesetting */
249 uint32_t formats[8];
250 size_t nformats;
251 struct drm_plane primary_plane;
252 struct drm_crtc crtc;
253 struct drm_encoder encoder;
254 struct drm_connector connector;
255};
256
257static struct simpledrm_device *simpledrm_device_of_dev(struct drm_device *dev)
258{
259 return container_of(dev, struct simpledrm_device, dev);
260}
261
262/*
263 * Hardware
264 */
265
266#if defined CONFIG_OF && defined CONFIG_COMMON_CLK
267/*
268 * Clock handling code.
269 *
270 * Here we handle the clocks property of our "simple-framebuffer" dt node.
271 * This is necessary so that we can make sure that any clocks needed by
272 * the display engine that the bootloader set up for us (and for which it
273 * provided a simplefb dt node), stay up, for the life of the simplefb
274 * driver.
275 *
276 * When the driver unloads, we cleanly disable, and then release the clocks.
277 *
278 * We only complain about errors here, no action is taken as the most likely
279 * error can only happen due to a mismatch between the bootloader which set
280 * up simplefb, and the clock definitions in the device tree. Chances are
281 * that there are no adverse effects, and if there are, a clean teardown of
282 * the fb probe will not help us much either. So just complain and carry on,
283 * and hope that the user actually gets a working fb at the end of things.
284 */
285
286static void simpledrm_device_release_clocks(void *res)
287{
288 struct simpledrm_device *sdev = simpledrm_device_of_dev(res);
289 unsigned int i;
290
291 for (i = 0; i < sdev->clk_count; ++i) {
292 if (sdev->clks[i]) {
293 clk_disable_unprepare(sdev->clks[i]);
294 clk_put(sdev->clks[i]);
295 }
296 }
297}
298
299static int simpledrm_device_init_clocks(struct simpledrm_device *sdev)
300{
301 struct drm_device *dev = &sdev->dev;
302 struct platform_device *pdev = to_platform_device(dev->dev);
303 struct device_node *of_node = pdev->dev.of_node;
304 struct clk *clock;
305 unsigned int i;
306 int ret;
307
308 if (dev_get_platdata(&pdev->dev) || !of_node)
309 return 0;
310
311 sdev->clk_count = of_clk_get_parent_count(of_node);
312 if (!sdev->clk_count)
313 return 0;
314
315 sdev->clks = drmm_kzalloc(dev, sdev->clk_count * sizeof(sdev->clks[0]),
316 GFP_KERNEL);
317 if (!sdev->clks)
318 return -ENOMEM;
319
320 for (i = 0; i < sdev->clk_count; ++i) {
321 clock = of_clk_get(of_node, i);
322 if (IS_ERR(clock)) {
323 ret = PTR_ERR(clock);
324 if (ret == -EPROBE_DEFER)
325 goto err;
326 drm_err(dev, "clock %u not found: %d\n", i, ret);
327 continue;
328 }
329 ret = clk_prepare_enable(clock);
330 if (ret) {
331 drm_err(dev, "failed to enable clock %u: %d\n",
332 i, ret);
333 clk_put(clock);
334 continue;
335 }
336 sdev->clks[i] = clock;
337 }
338
339 return devm_add_action_or_reset(&pdev->dev,
340 simpledrm_device_release_clocks,
341 sdev);
342
343err:
344 while (i) {
345 --i;
346 if (sdev->clks[i]) {
347 clk_disable_unprepare(sdev->clks[i]);
348 clk_put(sdev->clks[i]);
349 }
350 }
351 return ret;
352}
353#else
354static int simpledrm_device_init_clocks(struct simpledrm_device *sdev)
355{
356 return 0;
357}
358#endif
359
360#if defined CONFIG_OF && defined CONFIG_REGULATOR
361
362#define SUPPLY_SUFFIX "-supply"
363
364/*
365 * Regulator handling code.
366 *
367 * Here we handle the num-supplies and vin*-supply properties of our
368 * "simple-framebuffer" dt node. This is necessary so that we can make sure
369 * that any regulators needed by the display hardware that the bootloader
370 * set up for us (and for which it provided a simplefb dt node), stay up,
371 * for the life of the simplefb driver.
372 *
373 * When the driver unloads, we cleanly disable, and then release the
374 * regulators.
375 *
376 * We only complain about errors here, no action is taken as the most likely
377 * error can only happen due to a mismatch between the bootloader which set
378 * up simplefb, and the regulator definitions in the device tree. Chances are
379 * that there are no adverse effects, and if there are, a clean teardown of
380 * the fb probe will not help us much either. So just complain and carry on,
381 * and hope that the user actually gets a working fb at the end of things.
382 */
383
384static void simpledrm_device_release_regulators(void *res)
385{
386 struct simpledrm_device *sdev = simpledrm_device_of_dev(res);
387 unsigned int i;
388
389 for (i = 0; i < sdev->regulator_count; ++i) {
390 if (sdev->regulators[i]) {
391 regulator_disable(sdev->regulators[i]);
392 regulator_put(sdev->regulators[i]);
393 }
394 }
395}
396
397static int simpledrm_device_init_regulators(struct simpledrm_device *sdev)
398{
399 struct drm_device *dev = &sdev->dev;
400 struct platform_device *pdev = to_platform_device(dev->dev);
401 struct device_node *of_node = pdev->dev.of_node;
402 struct property *prop;
403 struct regulator *regulator;
404 const char *p;
405 unsigned int count = 0, i = 0;
406 int ret;
407
408 if (dev_get_platdata(&pdev->dev) || !of_node)
409 return 0;
410
411 /* Count the number of regulator supplies */
412 for_each_property_of_node(of_node, prop) {
413 p = strstr(prop->name, SUPPLY_SUFFIX);
414 if (p && p != prop->name)
415 ++count;
416 }
417
418 if (!count)
419 return 0;
420
421 sdev->regulators = drmm_kzalloc(dev,
422 count * sizeof(sdev->regulators[0]),
423 GFP_KERNEL);
424 if (!sdev->regulators)
425 return -ENOMEM;
426
427 for_each_property_of_node(of_node, prop) {
428 char name[32]; /* 32 is max size of property name */
429 size_t len;
430
431 p = strstr(prop->name, SUPPLY_SUFFIX);
432 if (!p || p == prop->name)
433 continue;
434 len = strlen(prop->name) - strlen(SUPPLY_SUFFIX) + 1;
435 strscpy(name, prop->name, min(sizeof(name), len));
436
437 regulator = regulator_get_optional(&pdev->dev, name);
438 if (IS_ERR(regulator)) {
439 ret = PTR_ERR(regulator);
440 if (ret == -EPROBE_DEFER)
441 goto err;
442 drm_err(dev, "regulator %s not found: %d\n",
443 name, ret);
444 continue;
445 }
446
447 ret = regulator_enable(regulator);
448 if (ret) {
449 drm_err(dev, "failed to enable regulator %u: %d\n",
450 i, ret);
451 regulator_put(regulator);
452 continue;
453 }
454
455 sdev->regulators[i++] = regulator;
456 }
457 sdev->regulator_count = i;
458
459 return devm_add_action_or_reset(&pdev->dev,
460 simpledrm_device_release_regulators,
461 sdev);
462
463err:
464 while (i) {
465 --i;
466 if (sdev->regulators[i]) {
467 regulator_disable(sdev->regulators[i]);
468 regulator_put(sdev->regulators[i]);
469 }
470 }
471 return ret;
472}
473#else
474static int simpledrm_device_init_regulators(struct simpledrm_device *sdev)
475{
476 return 0;
477}
478#endif
479
480#if defined CONFIG_OF && defined CONFIG_PM_GENERIC_DOMAINS
481/*
482 * Generic power domain handling code.
483 *
484 * Here we handle the power-domains properties of our "simple-framebuffer"
485 * dt node. This is only necessary if there is more than one power-domain.
486 * A single power-domains is handled automatically by the driver core. Multiple
487 * power-domains have to be handled by drivers since the driver core can't know
488 * the correct power sequencing. Power sequencing is not an issue for simpledrm
489 * since the bootloader has put the power domains already in the correct state.
490 * simpledrm has only to ensure they remain active for its lifetime.
491 *
492 * When the driver unloads, we detach from the power-domains.
493 *
494 * We only complain about errors here, no action is taken as the most likely
495 * error can only happen due to a mismatch between the bootloader which set
496 * up the "simple-framebuffer" dt node, and the PM domain providers in the
497 * device tree. Chances are that there are no adverse effects, and if there are,
498 * a clean teardown of the fb probe will not help us much either. So just
499 * complain and carry on, and hope that the user actually gets a working fb at
500 * the end of things.
501 */
502static void simpledrm_device_detach_genpd(void *res)
503{
504 int i;
505 struct simpledrm_device *sdev = res;
506
507 if (sdev->pwr_dom_count <= 1)
508 return;
509
510 for (i = sdev->pwr_dom_count - 1; i >= 0; i--) {
511 if (sdev->pwr_dom_links[i])
512 device_link_del(sdev->pwr_dom_links[i]);
513 if (!IS_ERR_OR_NULL(sdev->pwr_dom_devs[i]))
514 dev_pm_domain_detach(sdev->pwr_dom_devs[i], true);
515 }
516}
517
518static int simpledrm_device_attach_genpd(struct simpledrm_device *sdev)
519{
520 struct device *dev = sdev->dev.dev;
521 int i;
522
523 sdev->pwr_dom_count = of_count_phandle_with_args(dev->of_node, "power-domains",
524 "#power-domain-cells");
525 /*
526 * Single power-domain devices are handled by driver core nothing to do
527 * here. The same for device nodes without "power-domains" property.
528 */
529 if (sdev->pwr_dom_count <= 1)
530 return 0;
531
532 sdev->pwr_dom_devs = devm_kcalloc(dev, sdev->pwr_dom_count,
533 sizeof(*sdev->pwr_dom_devs),
534 GFP_KERNEL);
535 if (!sdev->pwr_dom_devs)
536 return -ENOMEM;
537
538 sdev->pwr_dom_links = devm_kcalloc(dev, sdev->pwr_dom_count,
539 sizeof(*sdev->pwr_dom_links),
540 GFP_KERNEL);
541 if (!sdev->pwr_dom_links)
542 return -ENOMEM;
543
544 for (i = 0; i < sdev->pwr_dom_count; i++) {
545 sdev->pwr_dom_devs[i] = dev_pm_domain_attach_by_id(dev, i);
546 if (IS_ERR(sdev->pwr_dom_devs[i])) {
547 int ret = PTR_ERR(sdev->pwr_dom_devs[i]);
548 if (ret == -EPROBE_DEFER) {
549 simpledrm_device_detach_genpd(sdev);
550 return ret;
551 }
552 drm_warn(&sdev->dev,
553 "pm_domain_attach_by_id(%u) failed: %d\n", i, ret);
554 continue;
555 }
556
557 sdev->pwr_dom_links[i] = device_link_add(dev,
558 sdev->pwr_dom_devs[i],
559 DL_FLAG_STATELESS |
560 DL_FLAG_PM_RUNTIME |
561 DL_FLAG_RPM_ACTIVE);
562 if (!sdev->pwr_dom_links[i])
563 drm_warn(&sdev->dev, "failed to link power-domain %d\n", i);
564 }
565
566 return devm_add_action_or_reset(dev, simpledrm_device_detach_genpd, sdev);
567}
568#else
569static int simpledrm_device_attach_genpd(struct simpledrm_device *sdev)
570{
571 return 0;
572}
573#endif
574
575/*
576 * Modesetting
577 */
578
579static const uint64_t simpledrm_primary_plane_format_modifiers[] = {
580 DRM_FORMAT_MOD_LINEAR,
581 DRM_FORMAT_MOD_INVALID
582};
583
584static int simpledrm_primary_plane_helper_atomic_check(struct drm_plane *plane,
585 struct drm_atomic_state *state)
586{
587 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane);
588 struct drm_shadow_plane_state *new_shadow_plane_state =
589 to_drm_shadow_plane_state(new_plane_state);
590 struct drm_framebuffer *new_fb = new_plane_state->fb;
591 struct drm_crtc *new_crtc = new_plane_state->crtc;
592 struct drm_crtc_state *new_crtc_state = NULL;
593 struct drm_device *dev = plane->dev;
594 struct simpledrm_device *sdev = simpledrm_device_of_dev(dev);
595 int ret;
596
597 if (new_crtc)
598 new_crtc_state = drm_atomic_get_new_crtc_state(state, new_crtc);
599
600 ret = drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state,
601 DRM_PLANE_NO_SCALING,
602 DRM_PLANE_NO_SCALING,
603 false, false);
604 if (ret)
605 return ret;
606 else if (!new_plane_state->visible)
607 return 0;
608
609 if (new_fb->format != sdev->format) {
610 void *buf;
611
612 /* format conversion necessary; reserve buffer */
613 buf = drm_format_conv_state_reserve(&new_shadow_plane_state->fmtcnv_state,
614 sdev->pitch, GFP_KERNEL);
615 if (!buf)
616 return -ENOMEM;
617 }
618
619 return 0;
620}
621
622static void simpledrm_primary_plane_helper_atomic_update(struct drm_plane *plane,
623 struct drm_atomic_state *state)
624{
625 struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
626 struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
627 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
628 struct drm_framebuffer *fb = plane_state->fb;
629 struct drm_device *dev = plane->dev;
630 struct simpledrm_device *sdev = simpledrm_device_of_dev(dev);
631 struct drm_atomic_helper_damage_iter iter;
632 struct drm_rect damage;
633 int ret, idx;
634
635 ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE);
636 if (ret)
637 return;
638
639 if (!drm_dev_enter(dev, &idx))
640 goto out_drm_gem_fb_end_cpu_access;
641
642 drm_atomic_helper_damage_iter_init(&iter, old_plane_state, plane_state);
643 drm_atomic_for_each_plane_damage(&iter, &damage) {
644 struct drm_rect dst_clip = plane_state->dst;
645 struct iosys_map dst = sdev->screen_base;
646
647 if (!drm_rect_intersect(&dst_clip, &damage))
648 continue;
649
650 iosys_map_incr(&dst, drm_fb_clip_offset(sdev->pitch, sdev->format, &dst_clip));
651 drm_fb_blit(&dst, &sdev->pitch, sdev->format->format, shadow_plane_state->data,
652 fb, &damage, &shadow_plane_state->fmtcnv_state);
653 }
654
655 drm_dev_exit(idx);
656out_drm_gem_fb_end_cpu_access:
657 drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
658}
659
660static void simpledrm_primary_plane_helper_atomic_disable(struct drm_plane *plane,
661 struct drm_atomic_state *state)
662{
663 struct drm_device *dev = plane->dev;
664 struct simpledrm_device *sdev = simpledrm_device_of_dev(dev);
665 int idx;
666
667 if (!drm_dev_enter(dev, &idx))
668 return;
669
670 /* Clear screen to black if disabled */
671 iosys_map_memset(&sdev->screen_base, 0, 0, sdev->pitch * sdev->mode.vdisplay);
672
673 drm_dev_exit(idx);
674}
675
676static int simpledrm_primary_plane_helper_get_scanout_buffer(struct drm_plane *plane,
677 struct drm_scanout_buffer *sb)
678{
679 struct simpledrm_device *sdev = simpledrm_device_of_dev(plane->dev);
680
681 sb->width = sdev->mode.hdisplay;
682 sb->height = sdev->mode.vdisplay;
683 sb->format = sdev->format;
684 sb->pitch[0] = sdev->pitch;
685 sb->map[0] = sdev->screen_base;
686
687 return 0;
688}
689
690static const struct drm_plane_helper_funcs simpledrm_primary_plane_helper_funcs = {
691 DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
692 .atomic_check = simpledrm_primary_plane_helper_atomic_check,
693 .atomic_update = simpledrm_primary_plane_helper_atomic_update,
694 .atomic_disable = simpledrm_primary_plane_helper_atomic_disable,
695 .get_scanout_buffer = simpledrm_primary_plane_helper_get_scanout_buffer,
696};
697
698static const struct drm_plane_funcs simpledrm_primary_plane_funcs = {
699 .update_plane = drm_atomic_helper_update_plane,
700 .disable_plane = drm_atomic_helper_disable_plane,
701 .destroy = drm_plane_cleanup,
702 DRM_GEM_SHADOW_PLANE_FUNCS,
703};
704
705static enum drm_mode_status simpledrm_crtc_helper_mode_valid(struct drm_crtc *crtc,
706 const struct drm_display_mode *mode)
707{
708 struct simpledrm_device *sdev = simpledrm_device_of_dev(crtc->dev);
709
710 return drm_crtc_helper_mode_valid_fixed(crtc, mode, &sdev->mode);
711}
712
713/*
714 * The CRTC is always enabled. Screen updates are performed by
715 * the primary plane's atomic_update function. Disabling clears
716 * the screen in the primary plane's atomic_disable function.
717 */
718static const struct drm_crtc_helper_funcs simpledrm_crtc_helper_funcs = {
719 .mode_valid = simpledrm_crtc_helper_mode_valid,
720 .atomic_check = drm_crtc_helper_atomic_check,
721};
722
723static const struct drm_crtc_funcs simpledrm_crtc_funcs = {
724 .reset = drm_atomic_helper_crtc_reset,
725 .destroy = drm_crtc_cleanup,
726 .set_config = drm_atomic_helper_set_config,
727 .page_flip = drm_atomic_helper_page_flip,
728 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
729 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
730};
731
732static const struct drm_encoder_funcs simpledrm_encoder_funcs = {
733 .destroy = drm_encoder_cleanup,
734};
735
736static int simpledrm_connector_helper_get_modes(struct drm_connector *connector)
737{
738 struct simpledrm_device *sdev = simpledrm_device_of_dev(connector->dev);
739
740 return drm_connector_helper_get_modes_fixed(connector, &sdev->mode);
741}
742
743static const struct drm_connector_helper_funcs simpledrm_connector_helper_funcs = {
744 .get_modes = simpledrm_connector_helper_get_modes,
745};
746
747static const struct drm_connector_funcs simpledrm_connector_funcs = {
748 .reset = drm_atomic_helper_connector_reset,
749 .fill_modes = drm_helper_probe_single_connector_modes,
750 .destroy = drm_connector_cleanup,
751 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
752 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
753};
754
755static const struct drm_mode_config_funcs simpledrm_mode_config_funcs = {
756 .fb_create = drm_gem_fb_create_with_dirty,
757 .atomic_check = drm_atomic_helper_check,
758 .atomic_commit = drm_atomic_helper_commit,
759};
760
761/*
762 * Init / Cleanup
763 */
764
765static struct drm_display_mode simpledrm_mode(unsigned int width,
766 unsigned int height,
767 unsigned int width_mm,
768 unsigned int height_mm)
769{
770 const struct drm_display_mode mode = {
771 DRM_MODE_INIT(60, width, height, width_mm, height_mm)
772 };
773
774 return mode;
775}
776
777static struct simpledrm_device *simpledrm_device_create(struct drm_driver *drv,
778 struct platform_device *pdev)
779{
780 const struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev);
781 struct device_node *of_node = pdev->dev.of_node;
782 struct simpledrm_device *sdev;
783 struct drm_device *dev;
784 int width, height, stride;
785 int width_mm = 0, height_mm = 0;
786 struct device_node *panel_node;
787 const struct drm_format_info *format;
788 struct resource *res, *mem = NULL;
789 struct drm_plane *primary_plane;
790 struct drm_crtc *crtc;
791 struct drm_encoder *encoder;
792 struct drm_connector *connector;
793 unsigned long max_width, max_height;
794 size_t nformats;
795 int ret;
796
797 sdev = devm_drm_dev_alloc(&pdev->dev, drv, struct simpledrm_device, dev);
798 if (IS_ERR(sdev))
799 return ERR_CAST(sdev);
800 dev = &sdev->dev;
801 platform_set_drvdata(pdev, sdev);
802
803 /*
804 * Hardware settings
805 */
806
807 ret = simpledrm_device_init_clocks(sdev);
808 if (ret)
809 return ERR_PTR(ret);
810 ret = simpledrm_device_init_regulators(sdev);
811 if (ret)
812 return ERR_PTR(ret);
813 ret = simpledrm_device_attach_genpd(sdev);
814 if (ret)
815 return ERR_PTR(ret);
816
817 if (pd) {
818 width = simplefb_get_width_pd(dev, pd);
819 if (width < 0)
820 return ERR_PTR(width);
821 height = simplefb_get_height_pd(dev, pd);
822 if (height < 0)
823 return ERR_PTR(height);
824 stride = simplefb_get_stride_pd(dev, pd);
825 if (stride < 0)
826 return ERR_PTR(stride);
827 format = simplefb_get_format_pd(dev, pd);
828 if (IS_ERR(format))
829 return ERR_CAST(format);
830 } else if (of_node) {
831 width = simplefb_get_width_of(dev, of_node);
832 if (width < 0)
833 return ERR_PTR(width);
834 height = simplefb_get_height_of(dev, of_node);
835 if (height < 0)
836 return ERR_PTR(height);
837 stride = simplefb_get_stride_of(dev, of_node);
838 if (stride < 0)
839 return ERR_PTR(stride);
840 format = simplefb_get_format_of(dev, of_node);
841 if (IS_ERR(format))
842 return ERR_CAST(format);
843 mem = simplefb_get_memory_of(dev, of_node);
844 if (IS_ERR(mem))
845 return ERR_CAST(mem);
846 panel_node = of_parse_phandle(of_node, "panel", 0);
847 if (panel_node) {
848 simplefb_read_u32_of(dev, panel_node, "width-mm", &width_mm);
849 simplefb_read_u32_of(dev, panel_node, "height-mm", &height_mm);
850 of_node_put(panel_node);
851 }
852 } else {
853 drm_err(dev, "no simplefb configuration found\n");
854 return ERR_PTR(-ENODEV);
855 }
856 if (!stride) {
857 stride = drm_format_info_min_pitch(format, 0, width);
858 if (drm_WARN_ON(dev, !stride))
859 return ERR_PTR(-EINVAL);
860 }
861
862 /*
863 * Assume a monitor resolution of 96 dpi if physical dimensions
864 * are not specified to get a somewhat reasonable screen size.
865 */
866 if (!width_mm)
867 width_mm = DRM_MODE_RES_MM(width, 96ul);
868 if (!height_mm)
869 height_mm = DRM_MODE_RES_MM(height, 96ul);
870
871 sdev->mode = simpledrm_mode(width, height, width_mm, height_mm);
872 sdev->format = format;
873 sdev->pitch = stride;
874
875 drm_dbg(dev, "display mode={" DRM_MODE_FMT "}\n", DRM_MODE_ARG(&sdev->mode));
876 drm_dbg(dev, "framebuffer format=%p4cc, size=%dx%d, stride=%d byte\n",
877 &format->format, width, height, stride);
878
879 /*
880 * Memory management
881 */
882
883 if (mem) {
884 void *screen_base;
885
886 ret = devm_aperture_acquire_for_platform_device(pdev, mem->start,
887 resource_size(mem));
888 if (ret) {
889 drm_err(dev, "could not acquire memory range %pr: %d\n", mem, ret);
890 return ERR_PTR(ret);
891 }
892
893 drm_dbg(dev, "using system memory framebuffer at %pr\n", mem);
894
895 screen_base = devm_memremap(dev->dev, mem->start, resource_size(mem), MEMREMAP_WC);
896 if (IS_ERR(screen_base))
897 return screen_base;
898
899 iosys_map_set_vaddr(&sdev->screen_base, screen_base);
900 } else {
901 void __iomem *screen_base;
902
903 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
904 if (!res)
905 return ERR_PTR(-EINVAL);
906
907 ret = devm_aperture_acquire_for_platform_device(pdev, res->start,
908 resource_size(res));
909 if (ret) {
910 drm_err(dev, "could not acquire memory range %pr: %d\n", res, ret);
911 return ERR_PTR(ret);
912 }
913
914 drm_dbg(dev, "using I/O memory framebuffer at %pr\n", res);
915
916 mem = devm_request_mem_region(&pdev->dev, res->start, resource_size(res),
917 drv->name);
918 if (!mem) {
919 /*
920 * We cannot make this fatal. Sometimes this comes from magic
921 * spaces our resource handlers simply don't know about. Use
922 * the I/O-memory resource as-is and try to map that instead.
923 */
924 drm_warn(dev, "could not acquire memory region %pr\n", res);
925 mem = res;
926 }
927
928 screen_base = devm_ioremap_wc(&pdev->dev, mem->start, resource_size(mem));
929 if (!screen_base)
930 return ERR_PTR(-ENOMEM);
931
932 iosys_map_set_vaddr_iomem(&sdev->screen_base, screen_base);
933 }
934
935 /*
936 * Modesetting
937 */
938
939 ret = drmm_mode_config_init(dev);
940 if (ret)
941 return ERR_PTR(ret);
942
943 max_width = max_t(unsigned long, width, DRM_SHADOW_PLANE_MAX_WIDTH);
944 max_height = max_t(unsigned long, height, DRM_SHADOW_PLANE_MAX_HEIGHT);
945
946 dev->mode_config.min_width = width;
947 dev->mode_config.max_width = max_width;
948 dev->mode_config.min_height = height;
949 dev->mode_config.max_height = max_height;
950 dev->mode_config.preferred_depth = format->depth;
951 dev->mode_config.funcs = &simpledrm_mode_config_funcs;
952
953 /* Primary plane */
954
955 nformats = drm_fb_build_fourcc_list(dev, &format->format, 1,
956 sdev->formats, ARRAY_SIZE(sdev->formats));
957
958 primary_plane = &sdev->primary_plane;
959 ret = drm_universal_plane_init(dev, primary_plane, 0, &simpledrm_primary_plane_funcs,
960 sdev->formats, nformats,
961 simpledrm_primary_plane_format_modifiers,
962 DRM_PLANE_TYPE_PRIMARY, NULL);
963 if (ret)
964 return ERR_PTR(ret);
965 drm_plane_helper_add(primary_plane, &simpledrm_primary_plane_helper_funcs);
966 drm_plane_enable_fb_damage_clips(primary_plane);
967
968 /* CRTC */
969
970 crtc = &sdev->crtc;
971 ret = drm_crtc_init_with_planes(dev, crtc, primary_plane, NULL,
972 &simpledrm_crtc_funcs, NULL);
973 if (ret)
974 return ERR_PTR(ret);
975 drm_crtc_helper_add(crtc, &simpledrm_crtc_helper_funcs);
976
977 /* Encoder */
978
979 encoder = &sdev->encoder;
980 ret = drm_encoder_init(dev, encoder, &simpledrm_encoder_funcs,
981 DRM_MODE_ENCODER_NONE, NULL);
982 if (ret)
983 return ERR_PTR(ret);
984 encoder->possible_crtcs = drm_crtc_mask(crtc);
985
986 /* Connector */
987
988 connector = &sdev->connector;
989 ret = drm_connector_init(dev, connector, &simpledrm_connector_funcs,
990 DRM_MODE_CONNECTOR_Unknown);
991 if (ret)
992 return ERR_PTR(ret);
993 drm_connector_helper_add(connector, &simpledrm_connector_helper_funcs);
994 drm_connector_set_panel_orientation_with_quirk(connector,
995 DRM_MODE_PANEL_ORIENTATION_UNKNOWN,
996 width, height);
997
998 ret = drm_connector_attach_encoder(connector, encoder);
999 if (ret)
1000 return ERR_PTR(ret);
1001
1002 drm_mode_config_reset(dev);
1003
1004 return sdev;
1005}
1006
1007/*
1008 * DRM driver
1009 */
1010
1011DEFINE_DRM_GEM_FOPS(simpledrm_fops);
1012
1013static struct drm_driver simpledrm_driver = {
1014 DRM_GEM_SHMEM_DRIVER_OPS,
1015 DRM_FBDEV_SHMEM_DRIVER_OPS,
1016 .name = DRIVER_NAME,
1017 .desc = DRIVER_DESC,
1018 .date = DRIVER_DATE,
1019 .major = DRIVER_MAJOR,
1020 .minor = DRIVER_MINOR,
1021 .driver_features = DRIVER_ATOMIC | DRIVER_GEM | DRIVER_MODESET,
1022 .fops = &simpledrm_fops,
1023};
1024
1025/*
1026 * Platform driver
1027 */
1028
1029static int simpledrm_probe(struct platform_device *pdev)
1030{
1031 struct simpledrm_device *sdev;
1032 struct drm_device *dev;
1033 int ret;
1034
1035 sdev = simpledrm_device_create(&simpledrm_driver, pdev);
1036 if (IS_ERR(sdev))
1037 return PTR_ERR(sdev);
1038 dev = &sdev->dev;
1039
1040 ret = drm_dev_register(dev, 0);
1041 if (ret)
1042 return ret;
1043
1044 drm_client_setup(dev, sdev->format);
1045
1046 return 0;
1047}
1048
1049static void simpledrm_remove(struct platform_device *pdev)
1050{
1051 struct simpledrm_device *sdev = platform_get_drvdata(pdev);
1052 struct drm_device *dev = &sdev->dev;
1053
1054 drm_dev_unplug(dev);
1055}
1056
1057static const struct of_device_id simpledrm_of_match_table[] = {
1058 { .compatible = "simple-framebuffer", },
1059 { },
1060};
1061MODULE_DEVICE_TABLE(of, simpledrm_of_match_table);
1062
1063static struct platform_driver simpledrm_platform_driver = {
1064 .driver = {
1065 .name = "simple-framebuffer", /* connect to sysfb */
1066 .of_match_table = simpledrm_of_match_table,
1067 },
1068 .probe = simpledrm_probe,
1069 .remove = simpledrm_remove,
1070};
1071
1072module_platform_driver(simpledrm_platform_driver);
1073
1074MODULE_DESCRIPTION(DRIVER_DESC);
1075MODULE_LICENSE("GPL v2");
1// SPDX-License-Identifier: GPL-2.0-only
2
3#include <linux/clk.h>
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>
9
10#include <drm/drm_aperture.h>
11#include <drm/drm_atomic.h>
12#include <drm/drm_atomic_state_helper.h>
13#include <drm/drm_connector.h>
14#include <drm/drm_crtc_helper.h>
15#include <drm/drm_damage_helper.h>
16#include <drm/drm_device.h>
17#include <drm/drm_drv.h>
18#include <drm/drm_fbdev_generic.h>
19#include <drm/drm_format_helper.h>
20#include <drm/drm_gem_atomic_helper.h>
21#include <drm/drm_gem_framebuffer_helper.h>
22#include <drm/drm_gem_shmem_helper.h>
23#include <drm/drm_managed.h>
24#include <drm/drm_modeset_helper_vtables.h>
25#include <drm/drm_plane_helper.h>
26#include <drm/drm_probe_helper.h>
27
28#define DRIVER_NAME "simpledrm"
29#define DRIVER_DESC "DRM driver for simple-framebuffer platform devices"
30#define DRIVER_DATE "20200625"
31#define DRIVER_MAJOR 1
32#define DRIVER_MINOR 0
33
34/*
35 * Helpers for simplefb
36 */
37
38static int
39simplefb_get_validated_int(struct drm_device *dev, const char *name,
40 uint32_t value)
41{
42 if (value > INT_MAX) {
43 drm_err(dev, "simplefb: invalid framebuffer %s of %u\n",
44 name, value);
45 return -EINVAL;
46 }
47 return (int)value;
48}
49
50static int
51simplefb_get_validated_int0(struct drm_device *dev, const char *name,
52 uint32_t value)
53{
54 if (!value) {
55 drm_err(dev, "simplefb: invalid framebuffer %s of %u\n",
56 name, value);
57 return -EINVAL;
58 }
59 return simplefb_get_validated_int(dev, name, value);
60}
61
62static const struct drm_format_info *
63simplefb_get_validated_format(struct drm_device *dev, const char *format_name)
64{
65 static const struct simplefb_format formats[] = SIMPLEFB_FORMATS;
66 const struct simplefb_format *fmt = formats;
67 const struct simplefb_format *end = fmt + ARRAY_SIZE(formats);
68 const struct drm_format_info *info;
69
70 if (!format_name) {
71 drm_err(dev, "simplefb: missing framebuffer format\n");
72 return ERR_PTR(-EINVAL);
73 }
74
75 while (fmt < end) {
76 if (!strcmp(format_name, fmt->name)) {
77 info = drm_format_info(fmt->fourcc);
78 if (!info)
79 return ERR_PTR(-EINVAL);
80 return info;
81 }
82 ++fmt;
83 }
84
85 drm_err(dev, "simplefb: unknown framebuffer format %s\n",
86 format_name);
87
88 return ERR_PTR(-EINVAL);
89}
90
91static int
92simplefb_get_width_pd(struct drm_device *dev,
93 const struct simplefb_platform_data *pd)
94{
95 return simplefb_get_validated_int0(dev, "width", pd->width);
96}
97
98static int
99simplefb_get_height_pd(struct drm_device *dev,
100 const struct simplefb_platform_data *pd)
101{
102 return simplefb_get_validated_int0(dev, "height", pd->height);
103}
104
105static int
106simplefb_get_stride_pd(struct drm_device *dev,
107 const struct simplefb_platform_data *pd)
108{
109 return simplefb_get_validated_int(dev, "stride", pd->stride);
110}
111
112static const struct drm_format_info *
113simplefb_get_format_pd(struct drm_device *dev,
114 const struct simplefb_platform_data *pd)
115{
116 return simplefb_get_validated_format(dev, pd->format);
117}
118
119static int
120simplefb_read_u32_of(struct drm_device *dev, struct device_node *of_node,
121 const char *name, u32 *value)
122{
123 int ret = of_property_read_u32(of_node, name, value);
124
125 if (ret)
126 drm_err(dev, "simplefb: cannot parse framebuffer %s: error %d\n",
127 name, ret);
128 return ret;
129}
130
131static int
132simplefb_read_string_of(struct drm_device *dev, struct device_node *of_node,
133 const char *name, const char **value)
134{
135 int ret = of_property_read_string(of_node, name, value);
136
137 if (ret)
138 drm_err(dev, "simplefb: cannot parse framebuffer %s: error %d\n",
139 name, ret);
140 return ret;
141}
142
143static int
144simplefb_get_width_of(struct drm_device *dev, struct device_node *of_node)
145{
146 u32 width;
147 int ret = simplefb_read_u32_of(dev, of_node, "width", &width);
148
149 if (ret)
150 return ret;
151 return simplefb_get_validated_int0(dev, "width", width);
152}
153
154static int
155simplefb_get_height_of(struct drm_device *dev, struct device_node *of_node)
156{
157 u32 height;
158 int ret = simplefb_read_u32_of(dev, of_node, "height", &height);
159
160 if (ret)
161 return ret;
162 return simplefb_get_validated_int0(dev, "height", height);
163}
164
165static int
166simplefb_get_stride_of(struct drm_device *dev, struct device_node *of_node)
167{
168 u32 stride;
169 int ret = simplefb_read_u32_of(dev, of_node, "stride", &stride);
170
171 if (ret)
172 return ret;
173 return simplefb_get_validated_int(dev, "stride", stride);
174}
175
176static const struct drm_format_info *
177simplefb_get_format_of(struct drm_device *dev, struct device_node *of_node)
178{
179 const char *format;
180 int ret = simplefb_read_string_of(dev, of_node, "format", &format);
181
182 if (ret)
183 return ERR_PTR(ret);
184 return simplefb_get_validated_format(dev, format);
185}
186
187/*
188 * Simple Framebuffer device
189 */
190
191struct simpledrm_device {
192 struct drm_device dev;
193
194 /* clocks */
195#if defined CONFIG_OF && defined CONFIG_COMMON_CLK
196 unsigned int clk_count;
197 struct clk **clks;
198#endif
199 /* regulators */
200#if defined CONFIG_OF && defined CONFIG_REGULATOR
201 unsigned int regulator_count;
202 struct regulator **regulators;
203#endif
204
205 /* simplefb settings */
206 struct drm_display_mode mode;
207 const struct drm_format_info *format;
208 unsigned int pitch;
209
210 /* memory management */
211 void __iomem *screen_base;
212
213 /* modesetting */
214 uint32_t formats[8];
215 size_t nformats;
216 struct drm_plane primary_plane;
217 struct drm_crtc crtc;
218 struct drm_encoder encoder;
219 struct drm_connector connector;
220};
221
222static struct simpledrm_device *simpledrm_device_of_dev(struct drm_device *dev)
223{
224 return container_of(dev, struct simpledrm_device, dev);
225}
226
227/*
228 * Hardware
229 */
230
231#if defined CONFIG_OF && defined CONFIG_COMMON_CLK
232/*
233 * Clock handling code.
234 *
235 * Here we handle the clocks property of our "simple-framebuffer" dt node.
236 * This is necessary so that we can make sure that any clocks needed by
237 * the display engine that the bootloader set up for us (and for which it
238 * provided a simplefb dt node), stay up, for the life of the simplefb
239 * driver.
240 *
241 * When the driver unloads, we cleanly disable, and then release the clocks.
242 *
243 * We only complain about errors here, no action is taken as the most likely
244 * error can only happen due to a mismatch between the bootloader which set
245 * up simplefb, and the clock definitions in the device tree. Chances are
246 * that there are no adverse effects, and if there are, a clean teardown of
247 * the fb probe will not help us much either. So just complain and carry on,
248 * and hope that the user actually gets a working fb at the end of things.
249 */
250
251static void simpledrm_device_release_clocks(void *res)
252{
253 struct simpledrm_device *sdev = simpledrm_device_of_dev(res);
254 unsigned int i;
255
256 for (i = 0; i < sdev->clk_count; ++i) {
257 if (sdev->clks[i]) {
258 clk_disable_unprepare(sdev->clks[i]);
259 clk_put(sdev->clks[i]);
260 }
261 }
262}
263
264static int simpledrm_device_init_clocks(struct simpledrm_device *sdev)
265{
266 struct drm_device *dev = &sdev->dev;
267 struct platform_device *pdev = to_platform_device(dev->dev);
268 struct device_node *of_node = pdev->dev.of_node;
269 struct clk *clock;
270 unsigned int i;
271 int ret;
272
273 if (dev_get_platdata(&pdev->dev) || !of_node)
274 return 0;
275
276 sdev->clk_count = of_clk_get_parent_count(of_node);
277 if (!sdev->clk_count)
278 return 0;
279
280 sdev->clks = drmm_kzalloc(dev, sdev->clk_count * sizeof(sdev->clks[0]),
281 GFP_KERNEL);
282 if (!sdev->clks)
283 return -ENOMEM;
284
285 for (i = 0; i < sdev->clk_count; ++i) {
286 clock = of_clk_get(of_node, i);
287 if (IS_ERR(clock)) {
288 ret = PTR_ERR(clock);
289 if (ret == -EPROBE_DEFER)
290 goto err;
291 drm_err(dev, "clock %u not found: %d\n", i, ret);
292 continue;
293 }
294 ret = clk_prepare_enable(clock);
295 if (ret) {
296 drm_err(dev, "failed to enable clock %u: %d\n",
297 i, ret);
298 clk_put(clock);
299 continue;
300 }
301 sdev->clks[i] = clock;
302 }
303
304 return devm_add_action_or_reset(&pdev->dev,
305 simpledrm_device_release_clocks,
306 sdev);
307
308err:
309 while (i) {
310 --i;
311 if (sdev->clks[i]) {
312 clk_disable_unprepare(sdev->clks[i]);
313 clk_put(sdev->clks[i]);
314 }
315 }
316 return ret;
317}
318#else
319static int simpledrm_device_init_clocks(struct simpledrm_device *sdev)
320{
321 return 0;
322}
323#endif
324
325#if defined CONFIG_OF && defined CONFIG_REGULATOR
326
327#define SUPPLY_SUFFIX "-supply"
328
329/*
330 * Regulator handling code.
331 *
332 * Here we handle the num-supplies and vin*-supply properties of our
333 * "simple-framebuffer" dt node. This is necessary so that we can make sure
334 * that any regulators needed by the display hardware that the bootloader
335 * set up for us (and for which it provided a simplefb dt node), stay up,
336 * for the life of the simplefb driver.
337 *
338 * When the driver unloads, we cleanly disable, and then release the
339 * regulators.
340 *
341 * We only complain about errors here, no action is taken as the most likely
342 * error can only happen due to a mismatch between the bootloader which set
343 * up simplefb, and the regulator definitions in the device tree. Chances are
344 * that there are no adverse effects, and if there are, a clean teardown of
345 * the fb probe will not help us much either. So just complain and carry on,
346 * and hope that the user actually gets a working fb at the end of things.
347 */
348
349static void simpledrm_device_release_regulators(void *res)
350{
351 struct simpledrm_device *sdev = simpledrm_device_of_dev(res);
352 unsigned int i;
353
354 for (i = 0; i < sdev->regulator_count; ++i) {
355 if (sdev->regulators[i]) {
356 regulator_disable(sdev->regulators[i]);
357 regulator_put(sdev->regulators[i]);
358 }
359 }
360}
361
362static int simpledrm_device_init_regulators(struct simpledrm_device *sdev)
363{
364 struct drm_device *dev = &sdev->dev;
365 struct platform_device *pdev = to_platform_device(dev->dev);
366 struct device_node *of_node = pdev->dev.of_node;
367 struct property *prop;
368 struct regulator *regulator;
369 const char *p;
370 unsigned int count = 0, i = 0;
371 int ret;
372
373 if (dev_get_platdata(&pdev->dev) || !of_node)
374 return 0;
375
376 /* Count the number of regulator supplies */
377 for_each_property_of_node(of_node, prop) {
378 p = strstr(prop->name, SUPPLY_SUFFIX);
379 if (p && p != prop->name)
380 ++count;
381 }
382
383 if (!count)
384 return 0;
385
386 sdev->regulators = drmm_kzalloc(dev,
387 count * sizeof(sdev->regulators[0]),
388 GFP_KERNEL);
389 if (!sdev->regulators)
390 return -ENOMEM;
391
392 for_each_property_of_node(of_node, prop) {
393 char name[32]; /* 32 is max size of property name */
394 size_t len;
395
396 p = strstr(prop->name, SUPPLY_SUFFIX);
397 if (!p || p == prop->name)
398 continue;
399 len = strlen(prop->name) - strlen(SUPPLY_SUFFIX) + 1;
400 strscpy(name, prop->name, min(sizeof(name), len));
401
402 regulator = regulator_get_optional(&pdev->dev, name);
403 if (IS_ERR(regulator)) {
404 ret = PTR_ERR(regulator);
405 if (ret == -EPROBE_DEFER)
406 goto err;
407 drm_err(dev, "regulator %s not found: %d\n",
408 name, ret);
409 continue;
410 }
411
412 ret = regulator_enable(regulator);
413 if (ret) {
414 drm_err(dev, "failed to enable regulator %u: %d\n",
415 i, ret);
416 regulator_put(regulator);
417 continue;
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
428err:
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
439static int simpledrm_device_init_regulators(struct simpledrm_device *sdev)
440{
441 return 0;
442}
443#endif
444
445/*
446 * Modesetting
447 */
448
449/*
450 * Support all formats of simplefb and maybe more; in order
451 * of preference. The display's update function will do any
452 * conversion necessary.
453 *
454 * TODO: Add blit helpers for remaining formats and uncomment
455 * constants.
456 */
457static const uint32_t simpledrm_primary_plane_formats[] = {
458 DRM_FORMAT_XRGB8888,
459 DRM_FORMAT_ARGB8888,
460 DRM_FORMAT_RGB565,
461 //DRM_FORMAT_XRGB1555,
462 //DRM_FORMAT_ARGB1555,
463 DRM_FORMAT_RGB888,
464 DRM_FORMAT_XRGB2101010,
465 DRM_FORMAT_ARGB2101010,
466};
467
468static const uint64_t simpledrm_primary_plane_format_modifiers[] = {
469 DRM_FORMAT_MOD_LINEAR,
470 DRM_FORMAT_MOD_INVALID
471};
472
473static void simpledrm_primary_plane_helper_atomic_update(struct drm_plane *plane,
474 struct drm_atomic_state *state)
475{
476 struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
477 struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
478 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
479 struct drm_framebuffer *fb = plane_state->fb;
480 struct drm_device *dev = plane->dev;
481 struct simpledrm_device *sdev = simpledrm_device_of_dev(dev);
482 struct drm_atomic_helper_damage_iter iter;
483 struct drm_rect damage;
484 int ret, idx;
485
486 ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE);
487 if (ret)
488 return;
489
490 if (!drm_dev_enter(dev, &idx))
491 goto out_drm_gem_fb_end_cpu_access;
492
493 drm_atomic_helper_damage_iter_init(&iter, old_plane_state, plane_state);
494 drm_atomic_for_each_plane_damage(&iter, &damage) {
495 struct iosys_map dst = IOSYS_MAP_INIT_VADDR(sdev->screen_base);
496 struct drm_rect dst_clip = plane_state->dst;
497
498 if (!drm_rect_intersect(&dst_clip, &damage))
499 continue;
500
501 iosys_map_incr(&dst, drm_fb_clip_offset(sdev->pitch, sdev->format, &dst_clip));
502 drm_fb_blit(&dst, &sdev->pitch, sdev->format->format, shadow_plane_state->data, fb,
503 &damage);
504 }
505
506 drm_dev_exit(idx);
507out_drm_gem_fb_end_cpu_access:
508 drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
509}
510
511static void simpledrm_primary_plane_helper_atomic_disable(struct drm_plane *plane,
512 struct drm_atomic_state *state)
513{
514 struct drm_device *dev = plane->dev;
515 struct simpledrm_device *sdev = simpledrm_device_of_dev(dev);
516 int idx;
517
518 if (!drm_dev_enter(dev, &idx))
519 return;
520
521 /* Clear screen to black if disabled */
522 memset_io(sdev->screen_base, 0, sdev->pitch * sdev->mode.vdisplay);
523
524 drm_dev_exit(idx);
525}
526
527static const struct drm_plane_helper_funcs simpledrm_primary_plane_helper_funcs = {
528 DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
529 .atomic_check = drm_plane_helper_atomic_check,
530 .atomic_update = simpledrm_primary_plane_helper_atomic_update,
531 .atomic_disable = simpledrm_primary_plane_helper_atomic_disable,
532};
533
534static const struct drm_plane_funcs simpledrm_primary_plane_funcs = {
535 .update_plane = drm_atomic_helper_update_plane,
536 .disable_plane = drm_atomic_helper_disable_plane,
537 .destroy = drm_plane_cleanup,
538 DRM_GEM_SHADOW_PLANE_FUNCS,
539};
540
541static enum drm_mode_status simpledrm_crtc_helper_mode_valid(struct drm_crtc *crtc,
542 const struct drm_display_mode *mode)
543{
544 struct simpledrm_device *sdev = simpledrm_device_of_dev(crtc->dev);
545
546 return drm_crtc_helper_mode_valid_fixed(crtc, mode, &sdev->mode);
547}
548
549/*
550 * The CRTC is always enabled. Screen updates are performed by
551 * the primary plane's atomic_update function. Disabling clears
552 * the screen in the primary plane's atomic_disable function.
553 */
554static const struct drm_crtc_helper_funcs simpledrm_crtc_helper_funcs = {
555 .mode_valid = simpledrm_crtc_helper_mode_valid,
556 .atomic_check = drm_crtc_helper_atomic_check,
557};
558
559static const struct drm_crtc_funcs simpledrm_crtc_funcs = {
560 .reset = drm_atomic_helper_crtc_reset,
561 .destroy = drm_crtc_cleanup,
562 .set_config = drm_atomic_helper_set_config,
563 .page_flip = drm_atomic_helper_page_flip,
564 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
565 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
566};
567
568static const struct drm_encoder_funcs simpledrm_encoder_funcs = {
569 .destroy = drm_encoder_cleanup,
570};
571
572static int simpledrm_connector_helper_get_modes(struct drm_connector *connector)
573{
574 struct simpledrm_device *sdev = simpledrm_device_of_dev(connector->dev);
575
576 return drm_connector_helper_get_modes_fixed(connector, &sdev->mode);
577}
578
579static const struct drm_connector_helper_funcs simpledrm_connector_helper_funcs = {
580 .get_modes = simpledrm_connector_helper_get_modes,
581};
582
583static const struct drm_connector_funcs simpledrm_connector_funcs = {
584 .reset = drm_atomic_helper_connector_reset,
585 .fill_modes = drm_helper_probe_single_connector_modes,
586 .destroy = drm_connector_cleanup,
587 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
588 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
589};
590
591static const struct drm_mode_config_funcs simpledrm_mode_config_funcs = {
592 .fb_create = drm_gem_fb_create_with_dirty,
593 .atomic_check = drm_atomic_helper_check,
594 .atomic_commit = drm_atomic_helper_commit,
595};
596
597/*
598 * Init / Cleanup
599 */
600
601static struct drm_display_mode simpledrm_mode(unsigned int width,
602 unsigned int height)
603{
604 /*
605 * Assume a monitor resolution of 96 dpi to
606 * get a somewhat reasonable screen size.
607 */
608 const struct drm_display_mode mode = {
609 DRM_MODE_INIT(60, width, height,
610 DRM_MODE_RES_MM(width, 96ul),
611 DRM_MODE_RES_MM(height, 96ul))
612 };
613
614 return mode;
615}
616
617static struct simpledrm_device *simpledrm_device_create(struct drm_driver *drv,
618 struct platform_device *pdev)
619{
620 const struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev);
621 struct device_node *of_node = pdev->dev.of_node;
622 struct simpledrm_device *sdev;
623 struct drm_device *dev;
624 int width, height, stride;
625 const struct drm_format_info *format;
626 struct resource *res, *mem;
627 void __iomem *screen_base;
628 struct drm_plane *primary_plane;
629 struct drm_crtc *crtc;
630 struct drm_encoder *encoder;
631 struct drm_connector *connector;
632 unsigned long max_width, max_height;
633 size_t nformats;
634 int ret;
635
636 sdev = devm_drm_dev_alloc(&pdev->dev, drv, struct simpledrm_device, dev);
637 if (IS_ERR(sdev))
638 return ERR_CAST(sdev);
639 dev = &sdev->dev;
640 platform_set_drvdata(pdev, sdev);
641
642 /*
643 * Hardware settings
644 */
645
646 ret = simpledrm_device_init_clocks(sdev);
647 if (ret)
648 return ERR_PTR(ret);
649 ret = simpledrm_device_init_regulators(sdev);
650 if (ret)
651 return ERR_PTR(ret);
652
653 if (pd) {
654 width = simplefb_get_width_pd(dev, pd);
655 if (width < 0)
656 return ERR_PTR(width);
657 height = simplefb_get_height_pd(dev, pd);
658 if (height < 0)
659 return ERR_PTR(height);
660 stride = simplefb_get_stride_pd(dev, pd);
661 if (stride < 0)
662 return ERR_PTR(stride);
663 format = simplefb_get_format_pd(dev, pd);
664 if (IS_ERR(format))
665 return ERR_CAST(format);
666 } else if (of_node) {
667 width = simplefb_get_width_of(dev, of_node);
668 if (width < 0)
669 return ERR_PTR(width);
670 height = simplefb_get_height_of(dev, of_node);
671 if (height < 0)
672 return ERR_PTR(height);
673 stride = simplefb_get_stride_of(dev, of_node);
674 if (stride < 0)
675 return ERR_PTR(stride);
676 format = simplefb_get_format_of(dev, of_node);
677 if (IS_ERR(format))
678 return ERR_CAST(format);
679 } else {
680 drm_err(dev, "no simplefb configuration found\n");
681 return ERR_PTR(-ENODEV);
682 }
683 if (!stride) {
684 stride = drm_format_info_min_pitch(format, 0, width);
685 if (drm_WARN_ON(dev, !stride))
686 return ERR_PTR(-EINVAL);
687 }
688
689 sdev->mode = simpledrm_mode(width, height);
690 sdev->format = format;
691 sdev->pitch = stride;
692
693 drm_dbg(dev, "display mode={" DRM_MODE_FMT "}\n", DRM_MODE_ARG(&sdev->mode));
694 drm_dbg(dev, "framebuffer format=%p4cc, size=%dx%d, stride=%d byte\n",
695 &format->format, width, height, stride);
696
697 /*
698 * Memory management
699 */
700
701 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
702 if (!res)
703 return ERR_PTR(-EINVAL);
704
705 ret = devm_aperture_acquire_from_firmware(dev, res->start, resource_size(res));
706 if (ret) {
707 drm_err(dev, "could not acquire memory range %pr: error %d\n", res, ret);
708 return ERR_PTR(ret);
709 }
710
711 mem = devm_request_mem_region(&pdev->dev, res->start, resource_size(res), drv->name);
712 if (!mem) {
713 /*
714 * We cannot make this fatal. Sometimes this comes from magic
715 * spaces our resource handlers simply don't know about. Use
716 * the I/O-memory resource as-is and try to map that instead.
717 */
718 drm_warn(dev, "could not acquire memory region %pr\n", res);
719 mem = res;
720 }
721
722 screen_base = devm_ioremap_wc(&pdev->dev, mem->start, resource_size(mem));
723 if (!screen_base)
724 return ERR_PTR(-ENOMEM);
725 sdev->screen_base = screen_base;
726
727 /*
728 * Modesetting
729 */
730
731 ret = drmm_mode_config_init(dev);
732 if (ret)
733 return ERR_PTR(ret);
734
735 max_width = max_t(unsigned long, width, DRM_SHADOW_PLANE_MAX_WIDTH);
736 max_height = max_t(unsigned long, height, DRM_SHADOW_PLANE_MAX_HEIGHT);
737
738 dev->mode_config.min_width = width;
739 dev->mode_config.max_width = max_width;
740 dev->mode_config.min_height = height;
741 dev->mode_config.max_height = max_height;
742 dev->mode_config.preferred_depth = format->cpp[0] * 8;
743 dev->mode_config.funcs = &simpledrm_mode_config_funcs;
744
745 /* Primary plane */
746
747 nformats = drm_fb_build_fourcc_list(dev, &format->format, 1,
748 simpledrm_primary_plane_formats,
749 ARRAY_SIZE(simpledrm_primary_plane_formats),
750 sdev->formats, ARRAY_SIZE(sdev->formats));
751
752 primary_plane = &sdev->primary_plane;
753 ret = drm_universal_plane_init(dev, primary_plane, 0, &simpledrm_primary_plane_funcs,
754 sdev->formats, nformats,
755 simpledrm_primary_plane_format_modifiers,
756 DRM_PLANE_TYPE_PRIMARY, NULL);
757 if (ret)
758 return ERR_PTR(ret);
759 drm_plane_helper_add(primary_plane, &simpledrm_primary_plane_helper_funcs);
760 drm_plane_enable_fb_damage_clips(primary_plane);
761
762 /* CRTC */
763
764 crtc = &sdev->crtc;
765 ret = drm_crtc_init_with_planes(dev, crtc, primary_plane, NULL,
766 &simpledrm_crtc_funcs, NULL);
767 if (ret)
768 return ERR_PTR(ret);
769 drm_crtc_helper_add(crtc, &simpledrm_crtc_helper_funcs);
770
771 /* Encoder */
772
773 encoder = &sdev->encoder;
774 ret = drm_encoder_init(dev, encoder, &simpledrm_encoder_funcs,
775 DRM_MODE_ENCODER_NONE, NULL);
776 if (ret)
777 return ERR_PTR(ret);
778 encoder->possible_crtcs = drm_crtc_mask(crtc);
779
780 /* Connector */
781
782 connector = &sdev->connector;
783 ret = drm_connector_init(dev, connector, &simpledrm_connector_funcs,
784 DRM_MODE_CONNECTOR_Unknown);
785 if (ret)
786 return ERR_PTR(ret);
787 drm_connector_helper_add(connector, &simpledrm_connector_helper_funcs);
788 drm_connector_set_panel_orientation_with_quirk(connector,
789 DRM_MODE_PANEL_ORIENTATION_UNKNOWN,
790 width, height);
791
792 ret = drm_connector_attach_encoder(connector, encoder);
793 if (ret)
794 return ERR_PTR(ret);
795
796 drm_mode_config_reset(dev);
797
798 return sdev;
799}
800
801/*
802 * DRM driver
803 */
804
805DEFINE_DRM_GEM_FOPS(simpledrm_fops);
806
807static struct drm_driver simpledrm_driver = {
808 DRM_GEM_SHMEM_DRIVER_OPS,
809 .name = DRIVER_NAME,
810 .desc = DRIVER_DESC,
811 .date = DRIVER_DATE,
812 .major = DRIVER_MAJOR,
813 .minor = DRIVER_MINOR,
814 .driver_features = DRIVER_ATOMIC | DRIVER_GEM | DRIVER_MODESET,
815 .fops = &simpledrm_fops,
816};
817
818/*
819 * Platform driver
820 */
821
822static int simpledrm_probe(struct platform_device *pdev)
823{
824 struct simpledrm_device *sdev;
825 struct drm_device *dev;
826 int ret;
827
828 sdev = simpledrm_device_create(&simpledrm_driver, pdev);
829 if (IS_ERR(sdev))
830 return PTR_ERR(sdev);
831 dev = &sdev->dev;
832
833 ret = drm_dev_register(dev, 0);
834 if (ret)
835 return ret;
836
837 drm_fbdev_generic_setup(dev, 0);
838
839 return 0;
840}
841
842static int simpledrm_remove(struct platform_device *pdev)
843{
844 struct simpledrm_device *sdev = platform_get_drvdata(pdev);
845 struct drm_device *dev = &sdev->dev;
846
847 drm_dev_unplug(dev);
848
849 return 0;
850}
851
852static const struct of_device_id simpledrm_of_match_table[] = {
853 { .compatible = "simple-framebuffer", },
854 { },
855};
856MODULE_DEVICE_TABLE(of, simpledrm_of_match_table);
857
858static struct platform_driver simpledrm_platform_driver = {
859 .driver = {
860 .name = "simple-framebuffer", /* connect to sysfb */
861 .of_match_table = simpledrm_of_match_table,
862 },
863 .probe = simpledrm_probe,
864 .remove = simpledrm_remove,
865};
866
867module_platform_driver(simpledrm_platform_driver);
868
869MODULE_DESCRIPTION(DRIVER_DESC);
870MODULE_LICENSE("GPL v2");