1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/errno.h>
16#include <linux/string.h>
17#include <linux/console.h>
18#include <linux/mm.h>
19#include <linux/suspend.h>
20#include <linux/delay.h>
21#include <linux/fb.h>
22#include <linux/init.h>
23#include <linux/pci.h>
24#include <linux/uaccess.h>
25
26#include "lxfb.h"
27
28static char *mode_option;
29static int noclear, nopanel, nocrt;
30static int vram;
31static int vt_switch;
32
33
34
35
36
37
38static struct fb_videomode geode_modedb[] = {
39
40 { NULL, 60, 640, 480, 39682, 48, 8, 25, 2, 88, 2,
41 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
42 FB_VMODE_NONINTERLACED, 0 },
43
44 { NULL, 70, 640, 400, 39770, 40, 8, 28, 5, 96, 2,
45 FB_SYNC_HOR_HIGH_ACT,
46 FB_VMODE_NONINTERLACED, 0 },
47
48 { NULL, 70, 640, 480, 35014, 88, 24, 15, 2, 64, 3,
49 0, FB_VMODE_NONINTERLACED, 0 },
50
51 { NULL, 72, 640, 480, 32102, 120, 16, 20, 1, 40, 3,
52 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
53 FB_VMODE_NONINTERLACED, 0 },
54
55 { NULL, 75, 640, 480, 31746, 120, 16, 16, 1, 64, 3,
56 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
57 FB_VMODE_NONINTERLACED, 0 },
58
59 { NULL, 85, 640, 480, 27780, 80, 56, 25, 1, 56, 3,
60 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
61 FB_VMODE_NONINTERLACED, 0 },
62
63 { NULL, 90, 640, 480, 26392, 96, 32, 22, 1, 64, 3,
64 0, FB_VMODE_NONINTERLACED, 0 },
65
66 { NULL, 100, 640, 480, 23167, 104, 40, 25, 1, 64, 3,
67 0, FB_VMODE_NONINTERLACED, 0 },
68
69 { NULL, 60, 640, 480, 39682, 48, 16, 25, 10, 88, 2,
70 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
71 FB_VMODE_NONINTERLACED, 0 },
72
73 { NULL, 56, 800, 600, 27901, 128, 24, 22, 1, 72, 2,
74 0, FB_VMODE_NONINTERLACED, 0 },
75
76 { NULL, 60, 800, 600, 25131, 72, 32, 23, 1, 136, 4,
77 0, FB_VMODE_NONINTERLACED, 0 },
78
79 { NULL, 70, 800, 600, 21873, 120, 40, 21, 4, 80, 3,
80 0, FB_VMODE_NONINTERLACED, 0 },
81
82 { NULL, 72, 800, 600, 20052, 64, 56, 23, 37, 120, 6,
83 0, FB_VMODE_NONINTERLACED, 0 },
84
85 { NULL, 75, 800, 600, 20202, 160, 16, 21, 1, 80, 3,
86 0, FB_VMODE_NONINTERLACED, 0 },
87
88 { NULL, 85, 800, 600, 17790, 152, 32, 27, 1, 64, 3,
89 0, FB_VMODE_NONINTERLACED, 0 },
90
91 { NULL, 90, 800, 600, 16648, 128, 40, 28, 1, 88, 3,
92 0, FB_VMODE_NONINTERLACED, 0 },
93
94 { NULL, 100, 800, 600, 14667, 136, 48, 27, 1, 88, 3,
95 0, FB_VMODE_NONINTERLACED, 0 },
96
97 { NULL, 60, 800, 600, 25131, 88, 40, 23, 1, 128, 4,
98 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
99 FB_VMODE_NONINTERLACED, 0 },
100
101 { NULL, 60, 1024, 768, 15385, 160, 24, 29, 3, 136, 6,
102 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
103 FB_VMODE_NONINTERLACED, 0 },
104
105 { NULL, 70, 1024, 768, 13346, 144, 24, 29, 3, 136, 6,
106 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
107 FB_VMODE_NONINTERLACED, 0 },
108
109 { NULL, 72, 1024, 768, 12702, 168, 56, 29, 4, 112, 3,
110 0, FB_VMODE_NONINTERLACED, 0 },
111
112 { NULL, 75, 1024, 768, 12703, 176, 16, 28, 1, 96, 3,
113 0, FB_VMODE_NONINTERLACED, 0 },
114
115 { NULL, 85, 1024, 768, 10581, 208, 48, 36, 1, 96, 3,
116 0, FB_VMODE_NONINTERLACED, 0 },
117
118 { NULL, 90, 1024, 768, 9981, 176, 64, 37, 1, 112, 3,
119 0, FB_VMODE_NONINTERLACED, 0 },
120
121 { NULL, 100, 1024, 768, 8825, 184, 72, 42, 1, 112, 3,
122 0, FB_VMODE_NONINTERLACED, 0 },
123
124 { NULL, 60, 1024, 768, 15385, 160, 24, 29, 3, 136, 6,
125 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
126 FB_VMODE_NONINTERLACED, 0 },
127
128 { NULL, 60, 1152, 864, 12251, 184, 64, 27, 1, 120, 3,
129 0, FB_VMODE_NONINTERLACED, 0 },
130
131 { NULL, 70, 1152, 864, 10254, 192, 72, 32, 8, 120, 3,
132 0, FB_VMODE_NONINTERLACED, 0 },
133
134 { NULL, 72, 1152, 864, 9866, 200, 72, 33, 7, 128, 3,
135 0, FB_VMODE_NONINTERLACED, 0 },
136
137 { NULL, 75, 1152, 864, 9259, 256, 64, 32, 1, 128, 3,
138 0, FB_VMODE_NONINTERLACED, 0 },
139
140 { NULL, 85, 1152, 864, 8357, 200, 72, 37, 3, 128, 3,
141 0, FB_VMODE_NONINTERLACED, 0 },
142
143 { NULL, 90, 1152, 864, 7719, 208, 80, 42, 9, 128, 3,
144 0, FB_VMODE_NONINTERLACED, 0 },
145
146 { NULL, 100, 1152, 864, 6947, 208, 80, 48, 3, 128, 3,
147 0, FB_VMODE_NONINTERLACED, 0 },
148
149 { NULL, 60, 1152, 864, 12251, 184, 64, 27, 1, 120, 3,
150 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
151 FB_VMODE_NONINTERLACED, 0 },
152
153 { NULL, 60, 1280, 1024, 9262, 248, 48, 38, 1, 112, 3,
154 0, FB_VMODE_NONINTERLACED, 0 },
155
156 { NULL, 70, 1280, 1024, 7719, 224, 88, 38, 6, 136, 3,
157 0, FB_VMODE_NONINTERLACED, 0 },
158
159 { NULL, 72, 1280, 1024, 7490, 224, 88, 39, 7, 136, 3,
160 0, FB_VMODE_NONINTERLACED, 0 },
161
162 { NULL, 75, 1280, 1024, 7409, 248, 16, 38, 1, 144, 3,
163 0, FB_VMODE_NONINTERLACED, 0 },
164
165 { NULL, 85, 1280, 1024, 6351, 224, 64, 44, 1, 160, 3,
166 0, FB_VMODE_NONINTERLACED, 0 },
167
168 { NULL, 90, 1280, 1024, 5791, 240, 96, 51, 12, 144, 3,
169 0, FB_VMODE_NONINTERLACED, 0 },
170
171 { NULL, 100, 1280, 1024, 5212, 240, 96, 57, 6, 144, 3,
172 0, FB_VMODE_NONINTERLACED, 0 },
173
174 { NULL, 60, 1280, 1024, 9262, 248, 48, 38, 1, 112, 3,
175 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
176 FB_VMODE_NONINTERLACED, 0 },
177
178 { NULL, 60, 1600, 1200, 6172, 304, 64, 46, 1, 192, 3,
179 0, FB_VMODE_NONINTERLACED, 0 },
180
181 { NULL, 70, 1600, 1200, 5291, 304, 64, 46, 1, 192, 3,
182 0, FB_VMODE_NONINTERLACED, 0 },
183
184 { NULL, 72, 1600, 1200, 5053, 288, 112, 47, 13, 176, 3,
185 0, FB_VMODE_NONINTERLACED, 0 },
186
187 { NULL, 75, 1600, 1200, 4938, 304, 64, 46, 1, 192, 3,
188 0, FB_VMODE_NONINTERLACED, 0 },
189
190 { NULL, 85, 1600, 1200, 4357, 304, 64, 46, 1, 192, 3,
191 0, FB_VMODE_NONINTERLACED, 0 },
192
193 { NULL, 90, 1600, 1200, 3981, 304, 128, 60, 1, 176, 3,
194 0, FB_VMODE_NONINTERLACED, 0 },
195
196 { NULL, 100, 1600, 1200, 3563, 304, 128, 67, 1, 176, 3,
197 0, FB_VMODE_NONINTERLACED, 0 },
198
199 { NULL, 60, 1600, 1200, 6172, 304, 64, 46, 1, 192, 3,
200 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
201 FB_VMODE_NONINTERLACED, 0 },
202
203 { NULL, 60, 1920, 1440, 4273, 344, 128, 56, 1, 208, 3,
204 0, FB_VMODE_NONINTERLACED, 0 },
205
206 { NULL, 70, 1920, 1440, 3593, 360, 152, 55, 8, 208, 3,
207 0, FB_VMODE_NONINTERLACED, 0 },
208
209 { NULL, 72, 1920, 1440, 3472, 360, 152, 68, 4, 208, 3,
210 0, FB_VMODE_NONINTERLACED, 0 },
211
212 { NULL, 75, 1920, 1440, 3367, 352, 144, 56, 1, 224, 3,
213 0, FB_VMODE_NONINTERLACED, 0 },
214
215 { NULL, 85, 1920, 1440, 2929, 368, 152, 68, 1, 216, 3,
216 0, FB_VMODE_NONINTERLACED, 0 },
217};
218
219#ifdef CONFIG_OLPC
220#include <asm/olpc.h>
221
222static struct fb_videomode olpc_dcon_modedb[] = {
223
224 { NULL, 50, 1200, 900, 17460, 24, 8, 4, 5, 8, 3,
225 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
226 FB_VMODE_NONINTERLACED, 0 }
227};
228
229static void get_modedb(struct fb_videomode **modedb, unsigned int *size)
230{
231 if (olpc_has_dcon()) {
232 *modedb = (struct fb_videomode *) olpc_dcon_modedb;
233 *size = ARRAY_SIZE(olpc_dcon_modedb);
234 } else {
235 *modedb = (struct fb_videomode *) geode_modedb;
236 *size = ARRAY_SIZE(geode_modedb);
237 }
238}
239
240#else
241static void get_modedb(struct fb_videomode **modedb, unsigned int *size)
242{
243 *modedb = (struct fb_videomode *) geode_modedb;
244 *size = ARRAY_SIZE(geode_modedb);
245}
246#endif
247
248static int lxfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
249{
250 if (var->xres > 1920 || var->yres > 1440)
251 return -EINVAL;
252
253 if (var->bits_per_pixel == 32) {
254 var->red.offset = 16; var->red.length = 8;
255 var->green.offset = 8; var->green.length = 8;
256 var->blue.offset = 0; var->blue.length = 8;
257 } else if (var->bits_per_pixel == 16) {
258 var->red.offset = 11; var->red.length = 5;
259 var->green.offset = 5; var->green.length = 6;
260 var->blue.offset = 0; var->blue.length = 5;
261 } else if (var->bits_per_pixel == 8) {
262 var->red.offset = 0; var->red.length = 8;
263 var->green.offset = 0; var->green.length = 8;
264 var->blue.offset = 0; var->blue.length = 8;
265 } else
266 return -EINVAL;
267
268 var->transp.offset = 0; var->transp.length = 0;
269
270
271 if ((lx_get_pitch(var->xres, var->bits_per_pixel) * var->yres)
272 > info->fix.smem_len)
273 return -EINVAL;
274
275 return 0;
276}
277
278static int lxfb_set_par(struct fb_info *info)
279{
280 if (info->var.bits_per_pixel > 8)
281 info->fix.visual = FB_VISUAL_TRUECOLOR;
282 else
283 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
284
285 info->fix.line_length = lx_get_pitch(info->var.xres,
286 info->var.bits_per_pixel);
287
288 lx_set_mode(info);
289 return 0;
290}
291
292static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
293{
294 chan &= 0xffff;
295 chan >>= 16 - bf->length;
296 return chan << bf->offset;
297}
298
299static int lxfb_setcolreg(unsigned regno, unsigned red, unsigned green,
300 unsigned blue, unsigned transp,
301 struct fb_info *info)
302{
303 if (info->var.grayscale) {
304
305 red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
306 }
307
308
309 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
310 u32 *pal = info->pseudo_palette;
311 u32 v;
312
313 if (regno >= 16)
314 return -EINVAL;
315
316 v = chan_to_field(red, &info->var.red);
317 v |= chan_to_field(green, &info->var.green);
318 v |= chan_to_field(blue, &info->var.blue);
319
320 pal[regno] = v;
321 } else {
322 if (regno >= 256)
323 return -EINVAL;
324
325 lx_set_palette_reg(info, regno, red, green, blue);
326 }
327
328 return 0;
329}
330
331static int lxfb_blank(int blank_mode, struct fb_info *info)
332{
333 return lx_blank_display(info, blank_mode);
334}
335
336
337static int lxfb_map_video_memory(struct fb_info *info, struct pci_dev *dev)
338{
339 struct lxfb_par *par = info->par;
340 int ret;
341
342 ret = pci_enable_device(dev);
343
344 if (ret)
345 return ret;
346
347 ret = pci_request_region(dev, 0, "lxfb-framebuffer");
348
349 if (ret)
350 return ret;
351
352 ret = pci_request_region(dev, 1, "lxfb-gp");
353
354 if (ret)
355 return ret;
356
357 ret = pci_request_region(dev, 2, "lxfb-vg");
358
359 if (ret)
360 return ret;
361
362 ret = pci_request_region(dev, 3, "lxfb-vp");
363
364 if (ret)
365 return ret;
366
367 info->fix.smem_start = pci_resource_start(dev, 0);
368 info->fix.smem_len = vram ? vram : lx_framebuffer_size();
369
370 info->screen_base = ioremap(info->fix.smem_start, info->fix.smem_len);
371
372 ret = -ENOMEM;
373
374 if (info->screen_base == NULL)
375 return ret;
376
377 par->gp_regs = pci_ioremap_bar(dev, 1);
378
379 if (par->gp_regs == NULL)
380 return ret;
381
382 par->dc_regs = pci_ioremap_bar(dev, 2);
383
384 if (par->dc_regs == NULL)
385 return ret;
386
387 par->vp_regs = pci_ioremap_bar(dev, 3);
388
389 if (par->vp_regs == NULL)
390 return ret;
391
392 write_dc(par, DC_UNLOCK, DC_UNLOCK_UNLOCK);
393 write_dc(par, DC_GLIU0_MEM_OFFSET, info->fix.smem_start & 0xFF000000);
394 write_dc(par, DC_UNLOCK, DC_UNLOCK_LOCK);
395
396 dev_info(&dev->dev, "%d KB of video memory at 0x%lx\n",
397 info->fix.smem_len / 1024, info->fix.smem_start);
398
399 return 0;
400}
401
402static struct fb_ops lxfb_ops = {
403 .owner = THIS_MODULE,
404 .fb_check_var = lxfb_check_var,
405 .fb_set_par = lxfb_set_par,
406 .fb_setcolreg = lxfb_setcolreg,
407 .fb_blank = lxfb_blank,
408
409 .fb_fillrect = cfb_fillrect,
410 .fb_copyarea = cfb_copyarea,
411 .fb_imageblit = cfb_imageblit,
412};
413
414static struct fb_info *lxfb_init_fbinfo(struct device *dev)
415{
416 struct lxfb_par *par;
417 struct fb_info *info;
418
419
420 info = framebuffer_alloc(sizeof(struct lxfb_par) + sizeof(u32) * 16,
421 dev);
422 if (!info)
423 return NULL;
424
425 par = info->par;
426
427 strcpy(info->fix.id, "Geode LX");
428
429 info->fix.type = FB_TYPE_PACKED_PIXELS;
430 info->fix.type_aux = 0;
431 info->fix.xpanstep = 0;
432 info->fix.ypanstep = 0;
433 info->fix.ywrapstep = 0;
434 info->fix.accel = FB_ACCEL_NONE;
435
436 info->var.nonstd = 0;
437 info->var.activate = FB_ACTIVATE_NOW;
438 info->var.height = -1;
439 info->var.width = -1;
440 info->var.accel_flags = 0;
441 info->var.vmode = FB_VMODE_NONINTERLACED;
442
443 info->fbops = &lxfb_ops;
444 info->flags = FBINFO_DEFAULT;
445 info->node = -1;
446
447 info->pseudo_palette = (void *)par + sizeof(struct lxfb_par);
448
449 if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) {
450 framebuffer_release(info);
451 return NULL;
452 }
453
454 info->var.grayscale = 0;
455
456 return info;
457}
458
459#ifdef CONFIG_PM
460static int lxfb_suspend(struct pci_dev *pdev, pm_message_t state)
461{
462 struct fb_info *info = pci_get_drvdata(pdev);
463
464 if (state.event == PM_EVENT_SUSPEND) {
465 console_lock();
466 lx_powerdown(info);
467 fb_set_suspend(info, 1);
468 console_unlock();
469 }
470
471
472
473
474 return 0;
475}
476
477static int lxfb_resume(struct pci_dev *pdev)
478{
479 struct fb_info *info = pci_get_drvdata(pdev);
480 int ret;
481
482 console_lock();
483 ret = lx_powerup(info);
484 if (ret) {
485 printk(KERN_ERR "lxfb: power up failed!\n");
486 return ret;
487 }
488
489 fb_set_suspend(info, 0);
490 console_unlock();
491 return 0;
492}
493#else
494#define lxfb_suspend NULL
495#define lxfb_resume NULL
496#endif
497
498static int lxfb_probe(struct pci_dev *pdev, const struct pci_device_id *id)
499{
500 struct lxfb_par *par;
501 struct fb_info *info;
502 int ret;
503
504 struct fb_videomode *modedb_ptr;
505 unsigned int modedb_size;
506
507 info = lxfb_init_fbinfo(&pdev->dev);
508
509 if (info == NULL)
510 return -ENOMEM;
511
512 par = info->par;
513
514 ret = lxfb_map_video_memory(info, pdev);
515
516 if (ret < 0) {
517 dev_err(&pdev->dev,
518 "failed to map frame buffer or controller registers\n");
519 goto err;
520 }
521
522
523
524 par->output = 0;
525 par->output |= (nopanel) ? 0 : OUTPUT_PANEL;
526 par->output |= (nocrt) ? 0 : OUTPUT_CRT;
527
528
529
530 get_modedb(&modedb_ptr, &modedb_size);
531 ret = fb_find_mode(&info->var, info, mode_option,
532 modedb_ptr, modedb_size, NULL, 16);
533
534 if (ret == 0 || ret == 4) {
535 dev_err(&pdev->dev, "could not find valid video mode\n");
536 ret = -EINVAL;
537 goto err;
538 }
539
540
541
542
543 if (!noclear)
544 memset_io(info->screen_base, 0, info->fix.smem_len);
545
546
547
548 lxfb_check_var(&info->var, info);
549 lxfb_set_par(info);
550
551 pm_set_vt_switch(vt_switch);
552
553 if (register_framebuffer(info) < 0) {
554 ret = -EINVAL;
555 goto err;
556 }
557 pci_set_drvdata(pdev, info);
558 fb_info(info, "%s frame buffer device\n", info->fix.id);
559
560 return 0;
561
562err:
563 if (info->screen_base) {
564 iounmap(info->screen_base);
565 pci_release_region(pdev, 0);
566 }
567 if (par->gp_regs) {
568 iounmap(par->gp_regs);
569 pci_release_region(pdev, 1);
570 }
571 if (par->dc_regs) {
572 iounmap(par->dc_regs);
573 pci_release_region(pdev, 2);
574 }
575 if (par->vp_regs) {
576 iounmap(par->vp_regs);
577 pci_release_region(pdev, 3);
578 }
579
580 fb_dealloc_cmap(&info->cmap);
581 framebuffer_release(info);
582
583 return ret;
584}
585
586static void lxfb_remove(struct pci_dev *pdev)
587{
588 struct fb_info *info = pci_get_drvdata(pdev);
589 struct lxfb_par *par = info->par;
590
591 unregister_framebuffer(info);
592
593 iounmap(info->screen_base);
594 pci_release_region(pdev, 0);
595
596 iounmap(par->gp_regs);
597 pci_release_region(pdev, 1);
598
599 iounmap(par->dc_regs);
600 pci_release_region(pdev, 2);
601
602 iounmap(par->vp_regs);
603 pci_release_region(pdev, 3);
604
605 fb_dealloc_cmap(&info->cmap);
606 framebuffer_release(info);
607}
608
609static struct pci_device_id lxfb_id_table[] = {
610 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_LX_VIDEO) },
611 { 0, }
612};
613
614MODULE_DEVICE_TABLE(pci, lxfb_id_table);
615
616static struct pci_driver lxfb_driver = {
617 .name = "lxfb",
618 .id_table = lxfb_id_table,
619 .probe = lxfb_probe,
620 .remove = lxfb_remove,
621 .suspend = lxfb_suspend,
622 .resume = lxfb_resume,
623};
624
625#ifndef MODULE
626static int __init lxfb_setup(char *options)
627{
628 char *opt;
629
630 if (!options || !*options)
631 return 0;
632
633 while ((opt = strsep(&options, ",")) != NULL) {
634 if (!*opt)
635 continue;
636
637 if (!strcmp(opt, "noclear"))
638 noclear = 1;
639 else if (!strcmp(opt, "nopanel"))
640 nopanel = 1;
641 else if (!strcmp(opt, "nocrt"))
642 nocrt = 1;
643 else
644 mode_option = opt;
645 }
646
647 return 0;
648}
649#endif
650
651static int __init lxfb_init(void)
652{
653#ifndef MODULE
654 char *option = NULL;
655
656 if (fb_get_options("lxfb", &option))
657 return -ENODEV;
658
659 lxfb_setup(option);
660#endif
661 return pci_register_driver(&lxfb_driver);
662}
663static void __exit lxfb_cleanup(void)
664{
665 pci_unregister_driver(&lxfb_driver);
666}
667
668module_init(lxfb_init);
669module_exit(lxfb_cleanup);
670
671module_param(mode_option, charp, 0);
672MODULE_PARM_DESC(mode_option, "video mode (<x>x<y>[-<bpp>][@<refr>])");
673
674module_param(vram, int, 0);
675MODULE_PARM_DESC(vram, "video memory size");
676
677module_param(vt_switch, int, 0);
678MODULE_PARM_DESC(vt_switch, "enable VT switch during suspend/resume");
679
680MODULE_DESCRIPTION("Framebuffer driver for the AMD Geode LX");
681MODULE_LICENSE("GPL");
682