update hdmi drivers: mod gpio control for phonepad project
[firefly-linux-kernel-4.4.55.git] / drivers / video / efifb.c
1 /*
2  * Framebuffer driver for EFI/UEFI based system
3  *
4  * (c) 2006 Edgar Hucek <gimli@dark-green.com>
5  * Original efi driver written by Gerd Knorr <kraxel@goldbach.in-berlin.de>
6  *
7  */
8
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/errno.h>
12 #include <linux/fb.h>
13 #include <linux/platform_device.h>
14 #include <linux/screen_info.h>
15 #include <linux/dmi.h>
16 #include <linux/pci.h>
17 #include <video/vga.h>
18
19 static struct fb_var_screeninfo efifb_defined __initdata = {
20         .activate               = FB_ACTIVATE_NOW,
21         .height                 = -1,
22         .width                  = -1,
23         .right_margin           = 32,
24         .upper_margin           = 16,
25         .lower_margin           = 4,
26         .vsync_len              = 4,
27         .vmode                  = FB_VMODE_NONINTERLACED,
28 };
29
30 static struct fb_fix_screeninfo efifb_fix __initdata = {
31         .id                     = "EFI VGA",
32         .type                   = FB_TYPE_PACKED_PIXELS,
33         .accel                  = FB_ACCEL_NONE,
34         .visual                 = FB_VISUAL_TRUECOLOR,
35 };
36
37 enum {
38         M_I17,          /* 17-Inch iMac */
39         M_I20,          /* 20-Inch iMac */
40         M_I20_SR,       /* 20-Inch iMac (Santa Rosa) */
41         M_I24,          /* 24-Inch iMac */
42         M_I24_8_1,      /* 24-Inch iMac, 8,1th gen */
43         M_I24_10_1,     /* 24-Inch iMac, 10,1th gen */
44         M_I27_11_1,     /* 27-Inch iMac, 11,1th gen */
45         M_MINI,         /* Mac Mini */
46         M_MINI_3_1,     /* Mac Mini, 3,1th gen */
47         M_MINI_4_1,     /* Mac Mini, 4,1th gen */
48         M_MB,           /* MacBook */
49         M_MB_2,         /* MacBook, 2nd rev. */
50         M_MB_3,         /* MacBook, 3rd rev. */
51         M_MB_5_1,       /* MacBook, 5th rev. */
52         M_MB_6_1,       /* MacBook, 6th rev. */
53         M_MB_7_1,       /* MacBook, 7th rev. */
54         M_MB_SR,        /* MacBook, 2nd gen, (Santa Rosa) */
55         M_MBA,          /* MacBook Air */
56         M_MBP,          /* MacBook Pro */
57         M_MBP_2,        /* MacBook Pro 2nd gen */
58         M_MBP_2_2,      /* MacBook Pro 2,2nd gen */
59         M_MBP_SR,       /* MacBook Pro (Santa Rosa) */
60         M_MBP_4,        /* MacBook Pro, 4th gen */
61         M_MBP_5_1,    /* MacBook Pro, 5,1th gen */
62         M_MBP_5_2,      /* MacBook Pro, 5,2th gen */
63         M_MBP_5_3,      /* MacBook Pro, 5,3rd gen */
64         M_MBP_6_1,      /* MacBook Pro, 6,1th gen */
65         M_MBP_6_2,      /* MacBook Pro, 6,2th gen */
66         M_MBP_7_1,      /* MacBook Pro, 7,1th gen */
67         M_UNKNOWN       /* placeholder */
68 };
69
70 static struct efifb_dmi_info {
71         char *optname;
72         unsigned long base;
73         int stride;
74         int width;
75         int height;
76 } dmi_list[] = {
77         [M_I17] = { "i17", 0x80010000, 1472 * 4, 1440, 900 },
78         [M_I20] = { "i20", 0x80010000, 1728 * 4, 1680, 1050 }, /* guess */
79         [M_I20_SR] = { "imac7", 0x40010000, 1728 * 4, 1680, 1050 },
80         [M_I24] = { "i24", 0x80010000, 2048 * 4, 1920, 1200 }, /* guess */
81         [M_I24_8_1] = { "imac8", 0xc0060000, 2048 * 4, 1920, 1200 },
82         [M_I24_10_1] = { "imac10", 0xc0010000, 2048 * 4, 1920, 1080 },
83         [M_I27_11_1] = { "imac11", 0xc0010000, 2560 * 4, 2560, 1440 },
84         [M_MINI]= { "mini", 0x80000000, 2048 * 4, 1024, 768 },
85         [M_MINI_3_1] = { "mini31", 0x40010000, 1024 * 4, 1024, 768 },
86         [M_MINI_4_1] = { "mini41", 0xc0010000, 2048 * 4, 1920, 1200 },
87         [M_MB] = { "macbook", 0x80000000, 2048 * 4, 1280, 800 },
88         [M_MB_5_1] = { "macbook51", 0x80010000, 2048 * 4, 1280, 800 },
89         [M_MB_6_1] = { "macbook61", 0x80010000, 2048 * 4, 1280, 800 },
90         [M_MB_7_1] = { "macbook71", 0x80010000, 2048 * 4, 1280, 800 },
91         [M_MBA] = { "mba", 0x80000000, 2048 * 4, 1280, 800 },
92         [M_MBP] = { "mbp", 0x80010000, 1472 * 4, 1440, 900 },
93         [M_MBP_2] = { "mbp2", 0, 0, 0, 0 }, /* placeholder */
94         [M_MBP_2_2] = { "mbp22", 0x80010000, 1472 * 4, 1440, 900 },
95         [M_MBP_SR] = { "mbp3", 0x80030000, 2048 * 4, 1440, 900 },
96         [M_MBP_4] = { "mbp4", 0xc0060000, 2048 * 4, 1920, 1200 },
97         [M_MBP_5_1] = { "mbp51", 0xc0010000, 2048 * 4, 1440, 900 },
98         [M_MBP_5_2] = { "mbp52", 0xc0010000, 2048 * 4, 1920, 1200 },
99         [M_MBP_5_3] = { "mbp53", 0xd0010000, 2048 * 4, 1440, 900 },
100         [M_MBP_6_1] = { "mbp61", 0x90030000, 2048 * 4, 1920, 1200 },
101         [M_MBP_6_2] = { "mbp62", 0x90030000, 2048 * 4, 1680, 1050 },
102         [M_MBP_7_1] = { "mbp71", 0xc0010000, 2048 * 4, 1280, 800 },
103         [M_UNKNOWN] = { NULL, 0, 0, 0, 0 }
104 };
105
106 static int set_system(const struct dmi_system_id *id);
107
108 #define EFIFB_DMI_SYSTEM_ID(vendor, name, enumid)               \
109         { set_system, name, {                                   \
110                 DMI_MATCH(DMI_BIOS_VENDOR, vendor),             \
111                 DMI_MATCH(DMI_PRODUCT_NAME, name) },            \
112           &dmi_list[enumid] }
113
114 static struct dmi_system_id __initdata dmi_system_table[] = {
115         EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "iMac4,1", M_I17),
116         /* At least one of these two will be right; maybe both? */
117         EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "iMac5,1", M_I20),
118         EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac5,1", M_I20),
119         /* At least one of these two will be right; maybe both? */
120         EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "iMac6,1", M_I24),
121         EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac6,1", M_I24),
122         EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac7,1", M_I20_SR),
123         EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac8,1", M_I24_8_1),
124         EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac10,1", M_I24_10_1),
125         EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac11,1", M_I27_11_1),
126         EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "Macmini1,1", M_MINI),
127         EFIFB_DMI_SYSTEM_ID("Apple Inc.", "Macmini3,1", M_MINI_3_1),
128         EFIFB_DMI_SYSTEM_ID("Apple Inc.", "Macmini4,1", M_MINI_4_1),
129         EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBook1,1", M_MB),
130         /* At least one of these two will be right; maybe both? */
131         EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBook2,1", M_MB),
132         EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook2,1", M_MB),
133         /* At least one of these two will be right; maybe both? */
134         EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBook3,1", M_MB),
135         EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook3,1", M_MB),
136         EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook4,1", M_MB),
137         EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook5,1", M_MB_5_1),
138         EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook6,1", M_MB_6_1),
139         EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook7,1", M_MB_7_1),
140         EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookAir1,1", M_MBA),
141         EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBookPro1,1", M_MBP),
142         EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBookPro2,1", M_MBP_2),
143         EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBookPro2,2", M_MBP_2_2),
144         EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro2,1", M_MBP_2),
145         EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBookPro3,1", M_MBP_SR),
146         EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro3,1", M_MBP_SR),
147         EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro4,1", M_MBP_4),
148         EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro5,1", M_MBP_5_1),
149         EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro5,2", M_MBP_5_2),
150         EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro5,3", M_MBP_5_3),
151         EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro6,1", M_MBP_6_1),
152         EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro6,2", M_MBP_6_2),
153         EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro7,1", M_MBP_7_1),
154         {},
155 };
156
157 static int set_system(const struct dmi_system_id *id)
158 {
159         struct efifb_dmi_info *info = id->driver_data;
160         if (info->base == 0)
161                 return 0;
162
163         printk(KERN_INFO "efifb: dmi detected %s - framebuffer at %p "
164                          "(%dx%d, stride %d)\n", id->ident,
165                          (void *)info->base, info->width, info->height,
166                          info->stride);
167
168         /* Trust the bootloader over the DMI tables */
169         if (screen_info.lfb_base == 0) {
170 #if defined(CONFIG_PCI)
171                 struct pci_dev *dev = NULL;
172                 int found_bar = 0;
173 #endif
174                 screen_info.lfb_base = info->base;
175
176 #if defined(CONFIG_PCI)
177                 /* make sure that the address in the table is actually on a
178                  * VGA device's PCI BAR */
179
180                 for_each_pci_dev(dev) {
181                         int i;
182                         if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
183                                 continue;
184                         for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
185                                 resource_size_t start, end;
186
187                                 start = pci_resource_start(dev, i);
188                                 if (start == 0)
189                                         break;
190                                 end = pci_resource_end(dev, i);
191                                 if (screen_info.lfb_base >= start &&
192                                                 screen_info.lfb_base < end) {
193                                         found_bar = 1;
194                                 }
195                         }
196                 }
197                 if (!found_bar)
198                         screen_info.lfb_base = 0;
199 #endif
200         }
201         if (screen_info.lfb_base) {
202                 if (screen_info.lfb_linelength == 0)
203                         screen_info.lfb_linelength = info->stride;
204                 if (screen_info.lfb_width == 0)
205                         screen_info.lfb_width = info->width;
206                 if (screen_info.lfb_height == 0)
207                         screen_info.lfb_height = info->height;
208                 if (screen_info.orig_video_isVGA == 0)
209                         screen_info.orig_video_isVGA = VIDEO_TYPE_EFI;
210         } else {
211                 screen_info.lfb_linelength = 0;
212                 screen_info.lfb_width = 0;
213                 screen_info.lfb_height = 0;
214                 screen_info.orig_video_isVGA = 0;
215                 return 0;
216         }
217         return 1;
218 }
219
220 static int efifb_setcolreg(unsigned regno, unsigned red, unsigned green,
221                            unsigned blue, unsigned transp,
222                            struct fb_info *info)
223 {
224         /*
225          *  Set a single color register. The values supplied are
226          *  already rounded down to the hardware's capabilities
227          *  (according to the entries in the `var' structure). Return
228          *  != 0 for invalid regno.
229          */
230
231         if (regno >= info->cmap.len)
232                 return 1;
233
234         if (regno < 16) {
235                 red   >>= 8;
236                 green >>= 8;
237                 blue  >>= 8;
238                 ((u32 *)(info->pseudo_palette))[regno] =
239                         (red   << info->var.red.offset)   |
240                         (green << info->var.green.offset) |
241                         (blue  << info->var.blue.offset);
242         }
243         return 0;
244 }
245
246 static void efifb_destroy(struct fb_info *info)
247 {
248         if (info->screen_base)
249                 iounmap(info->screen_base);
250         release_mem_region(info->aperture_base, info->aperture_size);
251         framebuffer_release(info);
252 }
253
254 static struct fb_ops efifb_ops = {
255         .owner          = THIS_MODULE,
256         .fb_destroy     = efifb_destroy,
257         .fb_setcolreg   = efifb_setcolreg,
258         .fb_fillrect    = cfb_fillrect,
259         .fb_copyarea    = cfb_copyarea,
260         .fb_imageblit   = cfb_imageblit,
261 };
262
263 static int __init efifb_setup(char *options)
264 {
265         char *this_opt;
266         int i;
267
268         if (!options || !*options)
269                 return 0;
270
271         while ((this_opt = strsep(&options, ",")) != NULL) {
272                 if (!*this_opt) continue;
273
274                 for (i = 0; i < M_UNKNOWN; i++) {
275                         if (!strcmp(this_opt, dmi_list[i].optname) &&
276                                         dmi_list[i].base != 0) {
277                                 screen_info.lfb_base = dmi_list[i].base;
278                                 screen_info.lfb_linelength = dmi_list[i].stride;
279                                 screen_info.lfb_width = dmi_list[i].width;
280                                 screen_info.lfb_height = dmi_list[i].height;
281                         }
282                 }
283                 if (!strncmp(this_opt, "base:", 5))
284                         screen_info.lfb_base = simple_strtoul(this_opt+5, NULL, 0);
285                 else if (!strncmp(this_opt, "stride:", 7))
286                         screen_info.lfb_linelength = simple_strtoul(this_opt+7, NULL, 0) * 4;
287                 else if (!strncmp(this_opt, "height:", 7))
288                         screen_info.lfb_height = simple_strtoul(this_opt+7, NULL, 0);
289                 else if (!strncmp(this_opt, "width:", 6))
290                         screen_info.lfb_width = simple_strtoul(this_opt+6, NULL, 0);
291         }
292         return 0;
293 }
294
295 static int __init efifb_probe(struct platform_device *dev)
296 {
297         struct fb_info *info;
298         int err;
299         unsigned int size_vmode;
300         unsigned int size_remap;
301         unsigned int size_total;
302         int request_succeeded = 0;
303
304         if (!screen_info.lfb_depth)
305                 screen_info.lfb_depth = 32;
306         if (!screen_info.pages)
307                 screen_info.pages = 1;
308         if (!screen_info.lfb_base) {
309                 printk(KERN_DEBUG "efifb: invalid framebuffer address\n");
310                 return -ENODEV;
311         }
312         printk(KERN_INFO "efifb: probing for efifb\n");
313
314         /* just assume they're all unset if any are */
315         if (!screen_info.blue_size) {
316                 screen_info.blue_size = 8;
317                 screen_info.blue_pos = 0;
318                 screen_info.green_size = 8;
319                 screen_info.green_pos = 8;
320                 screen_info.red_size = 8;
321                 screen_info.red_pos = 16;
322                 screen_info.rsvd_size = 8;
323                 screen_info.rsvd_pos = 24;
324         }
325
326         efifb_fix.smem_start = screen_info.lfb_base;
327         efifb_defined.bits_per_pixel = screen_info.lfb_depth;
328         efifb_defined.xres = screen_info.lfb_width;
329         efifb_defined.yres = screen_info.lfb_height;
330         efifb_fix.line_length = screen_info.lfb_linelength;
331
332         /*   size_vmode -- that is the amount of memory needed for the
333          *                 used video mode, i.e. the minimum amount of
334          *                 memory we need. */
335         size_vmode = efifb_defined.yres * efifb_fix.line_length;
336
337         /*   size_total -- all video memory we have. Used for
338          *                 entries, ressource allocation and bounds
339          *                 checking. */
340         size_total = screen_info.lfb_size;
341         if (size_total < size_vmode)
342                 size_total = size_vmode;
343
344         /*   size_remap -- the amount of video memory we are going to
345          *                 use for efifb.  With modern cards it is no
346          *                 option to simply use size_total as that
347          *                 wastes plenty of kernel address space. */
348         size_remap  = size_vmode * 2;
349         if (size_remap > size_total)
350                 size_remap = size_total;
351         if (size_remap % PAGE_SIZE)
352                 size_remap += PAGE_SIZE - (size_remap % PAGE_SIZE);
353         efifb_fix.smem_len = size_remap;
354
355         if (request_mem_region(efifb_fix.smem_start, size_remap, "efifb")) {
356                 request_succeeded = 1;
357         } else {
358                 /* We cannot make this fatal. Sometimes this comes from magic
359                    spaces our resource handlers simply don't know about */
360                 printk(KERN_WARNING
361                        "efifb: cannot reserve video memory at 0x%lx\n",
362                         efifb_fix.smem_start);
363         }
364
365         info = framebuffer_alloc(sizeof(u32) * 16, &dev->dev);
366         if (!info) {
367                 printk(KERN_ERR "efifb: cannot allocate framebuffer\n");
368                 err = -ENOMEM;
369                 goto err_release_mem;
370         }
371         info->pseudo_palette = info->par;
372         info->par = NULL;
373
374         info->aperture_base = efifb_fix.smem_start;
375         info->aperture_size = size_remap;
376
377         info->screen_base = ioremap(efifb_fix.smem_start, efifb_fix.smem_len);
378         if (!info->screen_base) {
379                 printk(KERN_ERR "efifb: abort, cannot ioremap video memory "
380                                 "0x%x @ 0x%lx\n",
381                         efifb_fix.smem_len, efifb_fix.smem_start);
382                 err = -EIO;
383                 goto err_release_fb;
384         }
385
386         printk(KERN_INFO "efifb: framebuffer at 0x%lx, mapped to 0x%p, "
387                "using %dk, total %dk\n",
388                efifb_fix.smem_start, info->screen_base,
389                size_remap/1024, size_total/1024);
390         printk(KERN_INFO "efifb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
391                efifb_defined.xres, efifb_defined.yres,
392                efifb_defined.bits_per_pixel, efifb_fix.line_length,
393                screen_info.pages);
394
395         efifb_defined.xres_virtual = efifb_defined.xres;
396         efifb_defined.yres_virtual = efifb_fix.smem_len /
397                                         efifb_fix.line_length;
398         printk(KERN_INFO "efifb: scrolling: redraw\n");
399         efifb_defined.yres_virtual = efifb_defined.yres;
400
401         /* some dummy values for timing to make fbset happy */
402         efifb_defined.pixclock     = 10000000 / efifb_defined.xres *
403                                         1000 / efifb_defined.yres;
404         efifb_defined.left_margin  = (efifb_defined.xres / 8) & 0xf8;
405         efifb_defined.hsync_len    = (efifb_defined.xres / 8) & 0xf8;
406
407         efifb_defined.red.offset    = screen_info.red_pos;
408         efifb_defined.red.length    = screen_info.red_size;
409         efifb_defined.green.offset  = screen_info.green_pos;
410         efifb_defined.green.length  = screen_info.green_size;
411         efifb_defined.blue.offset   = screen_info.blue_pos;
412         efifb_defined.blue.length   = screen_info.blue_size;
413         efifb_defined.transp.offset = screen_info.rsvd_pos;
414         efifb_defined.transp.length = screen_info.rsvd_size;
415
416         printk(KERN_INFO "efifb: %s: "
417                "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
418                "Truecolor",
419                screen_info.rsvd_size,
420                screen_info.red_size,
421                screen_info.green_size,
422                screen_info.blue_size,
423                screen_info.rsvd_pos,
424                screen_info.red_pos,
425                screen_info.green_pos,
426                screen_info.blue_pos);
427
428         efifb_fix.ypanstep  = 0;
429         efifb_fix.ywrapstep = 0;
430
431         info->fbops = &efifb_ops;
432         info->var = efifb_defined;
433         info->fix = efifb_fix;
434         info->flags = FBINFO_FLAG_DEFAULT | FBINFO_MISC_FIRMWARE;
435
436         if ((err = fb_alloc_cmap(&info->cmap, 256, 0)) < 0) {
437                 printk(KERN_ERR "efifb: cannot allocate colormap\n");
438                 goto err_unmap;
439         }
440         if ((err = register_framebuffer(info)) < 0) {
441                 printk(KERN_ERR "efifb: cannot register framebuffer\n");
442                 goto err_fb_dealoc;
443         }
444         printk(KERN_INFO "fb%d: %s frame buffer device\n",
445                 info->node, info->fix.id);
446         return 0;
447
448 err_fb_dealoc:
449         fb_dealloc_cmap(&info->cmap);
450 err_unmap:
451         iounmap(info->screen_base);
452 err_release_fb:
453         framebuffer_release(info);
454 err_release_mem:
455         if (request_succeeded)
456                 release_mem_region(efifb_fix.smem_start, size_total);
457         return err;
458 }
459
460 static struct platform_driver efifb_driver = {
461         .probe  = efifb_probe,
462         .driver = {
463                 .name   = "efifb",
464         },
465 };
466
467 static struct platform_device efifb_device = {
468         .name   = "efifb",
469 };
470
471 static int __init efifb_init(void)
472 {
473         int ret;
474         char *option = NULL;
475
476         dmi_check_system(dmi_system_table);
477
478         if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI)
479                 return -ENODEV;
480
481         if (fb_get_options("efifb", &option))
482                 return -ENODEV;
483         efifb_setup(option);
484
485         /* We don't get linelength from UGA Draw Protocol, only from
486          * EFI Graphics Protocol.  So if it's not in DMI, and it's not
487          * passed in from the user, we really can't use the framebuffer.
488          */
489         if (!screen_info.lfb_linelength)
490                 return -ENODEV;
491
492         ret = platform_driver_register(&efifb_driver);
493
494         if (!ret) {
495                 ret = platform_device_register(&efifb_device);
496                 if (ret)
497                         platform_driver_unregister(&efifb_driver);
498         }
499         return ret;
500 }
501 module_init(efifb_init);
502
503 MODULE_LICENSE("GPL");