GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / firmware / google / framebuffer-coreboot.c
1 /*
2  * framebuffer-coreboot.c
3  *
4  * Memory based framebuffer accessed through coreboot table.
5  *
6  * Copyright 2012-2013 David Herrmann <dh.herrmann@gmail.com>
7  * Copyright 2017 Google Inc.
8  * Copyright 2017 Samuel Holland <samuel@sholland.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License v2.0 as published by
12  * the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  */
19
20 #include <linux/device.h>
21 #include <linux/kernel.h>
22 #include <linux/mm.h>
23 #include <linux/module.h>
24 #include <linux/platform_data/simplefb.h>
25 #include <linux/platform_device.h>
26
27 #include "coreboot_table.h"
28
29 #define CB_TAG_FRAMEBUFFER 0x12
30
31 static const struct simplefb_format formats[] = SIMPLEFB_FORMATS;
32
33 static int framebuffer_probe(struct coreboot_device *dev)
34 {
35         int i;
36         u32 length;
37         struct lb_framebuffer *fb = &dev->framebuffer;
38         struct platform_device *pdev;
39         struct resource res;
40         struct simplefb_platform_data pdata = {
41                 .width = fb->x_resolution,
42                 .height = fb->y_resolution,
43                 .stride = fb->bytes_per_line,
44                 .format = NULL,
45         };
46
47         for (i = 0; i < ARRAY_SIZE(formats); ++i) {
48                 if (fb->bits_per_pixel     == formats[i].bits_per_pixel &&
49                     fb->red_mask_pos       == formats[i].red.offset &&
50                     fb->red_mask_size      == formats[i].red.length &&
51                     fb->green_mask_pos     == formats[i].green.offset &&
52                     fb->green_mask_size    == formats[i].green.length &&
53                     fb->blue_mask_pos      == formats[i].blue.offset &&
54                     fb->blue_mask_size     == formats[i].blue.length)
55                         pdata.format = formats[i].name;
56         }
57         if (!pdata.format)
58                 return -ENODEV;
59
60         memset(&res, 0, sizeof(res));
61         res.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
62         res.name = "Coreboot Framebuffer";
63         res.start = fb->physical_address;
64         length = PAGE_ALIGN(fb->y_resolution * fb->bytes_per_line);
65         res.end = res.start + length - 1;
66         if (res.end <= res.start)
67                 return -EINVAL;
68
69         pdev = platform_device_register_resndata(&dev->dev,
70                                                  "simple-framebuffer", 0,
71                                                  &res, 1, &pdata,
72                                                  sizeof(pdata));
73         if (IS_ERR(pdev))
74                 pr_warn("coreboot: could not register framebuffer\n");
75         else
76                 dev_set_drvdata(&dev->dev, pdev);
77
78         return PTR_ERR_OR_ZERO(pdev);
79 }
80
81 static int framebuffer_remove(struct coreboot_device *dev)
82 {
83         struct platform_device *pdev = dev_get_drvdata(&dev->dev);
84
85         platform_device_unregister(pdev);
86
87         return 0;
88 }
89
90 static struct coreboot_driver framebuffer_driver = {
91         .probe = framebuffer_probe,
92         .remove = framebuffer_remove,
93         .drv = {
94                 .name = "framebuffer",
95         },
96         .tag = CB_TAG_FRAMEBUFFER,
97 };
98
99 static int __init coreboot_framebuffer_init(void)
100 {
101         return coreboot_driver_register(&framebuffer_driver);
102 }
103
104 static void coreboot_framebuffer_exit(void)
105 {
106         coreboot_driver_unregister(&framebuffer_driver);
107 }
108
109 module_init(coreboot_framebuffer_init);
110 module_exit(coreboot_framebuffer_exit);
111
112 MODULE_AUTHOR("Samuel Holland <samuel@sholland.org>");
113 MODULE_LICENSE("GPL");