]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/video/fbdev/s1d13xxxfb.c
ipv4: convert dst_metrics.refcnt from atomic_t to refcount_t
[mirror_ubuntu-artful-kernel.git] / drivers / video / fbdev / s1d13xxxfb.c
CommitLineData
1da177e4
LT
1/* drivers/video/s1d13xxxfb.c
2 *
3 * (c) 2004 Simtec Electronics
4 * (c) 2005 Thibaut VARENE <varenet@parisc-linux.org>
3ed167af 5 * (c) 2009 Kristoffer Ericson <kristoffer.ericson@gmail.com>
1da177e4
LT
6 *
7 * Driver for Epson S1D13xxx series framebuffer chips
8 *
9 * Adapted from
10 * linux/drivers/video/skeletonfb.c
11 * linux/drivers/video/epson1355fb.c
12 * linux/drivers/video/epson/s1d13xxxfb.c (2.4 driver by Epson)
13 *
1da177e4
LT
14 * TODO: - handle dual screen display (CRT and LCD at the same time).
15 * - check_var(), mode change, etc.
3ed167af
KE
16 * - probably not SMP safe :)
17 * - support all bitblt operations on all cards
1da177e4
LT
18 *
19 * This file is subject to the terms and conditions of the GNU General Public
20 * License. See the file COPYING in the main directory of this archive for
21 * more details.
22 */
23
1da177e4 24#include <linux/module.h>
d052d1be 25#include <linux/platform_device.h>
1da177e4 26#include <linux/delay.h>
1da177e4
LT
27#include <linux/types.h>
28#include <linux/errno.h>
29#include <linux/mm.h>
30#include <linux/mman.h>
31#include <linux/fb.h>
3ed167af
KE
32#include <linux/spinlock_types.h>
33#include <linux/spinlock.h>
5a0e3ad6 34#include <linux/slab.h>
2584cf83 35#include <linux/io.h>
1da177e4
LT
36
37#include <video/s1d13xxxfb.h>
38
3ed167af
KE
39#define PFX "s1d13xxxfb: "
40#define BLIT "s1d13xxxfb_bitblt: "
1da177e4 41
3ed167af
KE
42/*
43 * set this to enable debugging on general functions
44 */
1da177e4
LT
45#if 0
46#define dbg(fmt, args...) do { printk(KERN_INFO fmt, ## args); } while(0)
47#else
48#define dbg(fmt, args...) do { } while (0)
49#endif
50
afbb9d8d 51/*
3ed167af
KE
52 * set this to enable debugging on 2D acceleration
53 */
54#if 0
55#define dbg_blit(fmt, args...) do { printk(KERN_INFO BLIT fmt, ## args); } while (0)
56#else
57#define dbg_blit(fmt, args...) do { } while (0)
58#endif
59
60/*
61 * we make sure only one bitblt operation is running
62 */
63static DEFINE_SPINLOCK(s1d13xxxfb_bitblt_lock);
64
65/*
66 * list of card production ids
afbb9d8d
KE
67 */
68static const int s1d13xxxfb_prod_ids[] = {
69 S1D13505_PROD_ID,
70 S1D13506_PROD_ID,
71 S1D13806_PROD_ID,
72};
73
74/*
75 * List of card strings
76 */
77static const char *s1d13xxxfb_prod_names[] = {
78 "S1D13505",
79 "S1D13506",
80 "S1D13806",
0b178883
KE
81};
82
1da177e4 83/*
3ed167af 84 * here we define the default struct fb_fix_screeninfo
1da177e4 85 */
ca9384c5 86static const struct fb_fix_screeninfo s1d13xxxfb_fix = {
1da177e4
LT
87 .id = S1D_FBID,
88 .type = FB_TYPE_PACKED_PIXELS,
89 .visual = FB_VISUAL_PSEUDOCOLOR,
90 .xpanstep = 0,
91 .ypanstep = 1,
92 .ywrapstep = 0,
93 .accel = FB_ACCEL_NONE,
94};
95
96static inline u8
97s1d13xxxfb_readreg(struct s1d13xxxfb_par *par, u16 regno)
98{
316240f6
HT
99#if defined(CONFIG_PLAT_M32700UT) || defined(CONFIG_PLAT_OPSPUT) || defined(CONFIG_PLAT_MAPPI3)
100 regno=((regno & 1) ? (regno & ~1L) : (regno + 1));
101#endif
1da177e4
LT
102 return readb(par->regs + regno);
103}
104
105static inline void
106s1d13xxxfb_writereg(struct s1d13xxxfb_par *par, u16 regno, u8 value)
107{
316240f6
HT
108#if defined(CONFIG_PLAT_M32700UT) || defined(CONFIG_PLAT_OPSPUT) || defined(CONFIG_PLAT_MAPPI3)
109 regno=((regno & 1) ? (regno & ~1L) : (regno + 1));
110#endif
1da177e4
LT
111 writeb(value, par->regs + regno);
112}
113
114static inline void
115s1d13xxxfb_runinit(struct s1d13xxxfb_par *par,
116 const struct s1d13xxxfb_regval *initregs,
117 const unsigned int size)
118{
119 int i;
120
121 for (i = 0; i < size; i++) {
122 if ((initregs[i].addr == S1DREG_DELAYOFF) ||
123 (initregs[i].addr == S1DREG_DELAYON))
124 mdelay((int)initregs[i].value);
125 else {
126 s1d13xxxfb_writereg(par, initregs[i].addr, initregs[i].value);
127 }
128 }
129
130 /* make sure the hardware can cope with us */
131 mdelay(1);
132}
133
134static inline void
135lcd_enable(struct s1d13xxxfb_par *par, int enable)
136{
137 u8 mode = s1d13xxxfb_readreg(par, S1DREG_COM_DISP_MODE);
138
139 if (enable)
140 mode |= 0x01;
141 else
142 mode &= ~0x01;
143
144 s1d13xxxfb_writereg(par, S1DREG_COM_DISP_MODE, mode);
145}
146
147static inline void
148crt_enable(struct s1d13xxxfb_par *par, int enable)
149{
150 u8 mode = s1d13xxxfb_readreg(par, S1DREG_COM_DISP_MODE);
151
152 if (enable)
153 mode |= 0x02;
154 else
155 mode &= ~0x02;
156
157 s1d13xxxfb_writereg(par, S1DREG_COM_DISP_MODE, mode);
158}
159
1da177e4 160
3ed167af
KE
161/*************************************************************
162 framebuffer control functions
163 *************************************************************/
1da177e4
LT
164static inline void
165s1d13xxxfb_setup_pseudocolour(struct fb_info *info)
166{
167 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
168
169 info->var.red.length = 4;
170 info->var.green.length = 4;
171 info->var.blue.length = 4;
172}
173
174static inline void
175s1d13xxxfb_setup_truecolour(struct fb_info *info)
176{
177 info->fix.visual = FB_VISUAL_TRUECOLOR;
178 info->var.bits_per_pixel = 16;
179
180 info->var.red.length = 5;
181 info->var.red.offset = 11;
182
183 info->var.green.length = 6;
184 info->var.green.offset = 5;
185
186 info->var.blue.length = 5;
187 info->var.blue.offset = 0;
188}
189
190/**
191 * s1d13xxxfb_set_par - Alters the hardware state.
192 * @info: frame buffer structure
193 *
194 * Using the fb_var_screeninfo in fb_info we set the depth of the
195 * framebuffer. This function alters the par AND the
196 * fb_fix_screeninfo stored in fb_info. It doesn't not alter var in
197 * fb_info since we are using that data. This means we depend on the
198 * data in var inside fb_info to be supported by the hardware.
199 * xxxfb_check_var is always called before xxxfb_set_par to ensure this.
200 *
201 * XXX TODO: write proper s1d13xxxfb_check_var(), without which that
202 * function is quite useless.
203 */
204static int
205s1d13xxxfb_set_par(struct fb_info *info)
206{
207 struct s1d13xxxfb_par *s1dfb = info->par;
208 unsigned int val;
209
210 dbg("s1d13xxxfb_set_par: bpp=%d\n", info->var.bits_per_pixel);
211
212 if ((s1dfb->display & 0x01)) /* LCD */
213 val = s1d13xxxfb_readreg(s1dfb, S1DREG_LCD_DISP_MODE); /* read colour control */
214 else /* CRT */
215 val = s1d13xxxfb_readreg(s1dfb, S1DREG_CRT_DISP_MODE); /* read colour control */
216
217 val &= ~0x07;
218
219 switch (info->var.bits_per_pixel) {
220 case 4:
221 dbg("pseudo colour 4\n");
222 s1d13xxxfb_setup_pseudocolour(info);
223 val |= 2;
224 break;
225 case 8:
226 dbg("pseudo colour 8\n");
227 s1d13xxxfb_setup_pseudocolour(info);
228 val |= 3;
229 break;
230 case 16:
231 dbg("true colour\n");
232 s1d13xxxfb_setup_truecolour(info);
233 val |= 5;
234 break;
235
236 default:
237 dbg("bpp not supported!\n");
238 return -EINVAL;
239 }
240
241 dbg("writing %02x to display mode register\n", val);
242
243 if ((s1dfb->display & 0x01)) /* LCD */
244 s1d13xxxfb_writereg(s1dfb, S1DREG_LCD_DISP_MODE, val);
245 else /* CRT */
246 s1d13xxxfb_writereg(s1dfb, S1DREG_CRT_DISP_MODE, val);
247
248 info->fix.line_length = info->var.xres * info->var.bits_per_pixel;
249 info->fix.line_length /= 8;
250
251 dbg("setting line_length to %d\n", info->fix.line_length);
252
253 dbg("done setup\n");
254
255 return 0;
256}
257
258/**
3ed167af
KE
259 * s1d13xxxfb_setcolreg - sets a color register.
260 * @regno: Which register in the CLUT we are programming
261 * @red: The red value which can be up to 16 bits wide
1da177e4
LT
262 * @green: The green value which can be up to 16 bits wide
263 * @blue: The blue value which can be up to 16 bits wide.
264 * @transp: If supported the alpha value which can be up to 16 bits wide.
3ed167af 265 * @info: frame buffer info structure
1da177e4
LT
266 *
267 * Returns negative errno on error, or zero on success.
268 */
269static int
270s1d13xxxfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
271 u_int transp, struct fb_info *info)
272{
273 struct s1d13xxxfb_par *s1dfb = info->par;
274 unsigned int pseudo_val;
275
276 if (regno >= S1D_PALETTE_SIZE)
277 return -EINVAL;
278
279 dbg("s1d13xxxfb_setcolreg: %d: rgb=%d,%d,%d, tr=%d\n",
280 regno, red, green, blue, transp);
281
282 if (info->var.grayscale)
283 red = green = blue = (19595*red + 38470*green + 7471*blue) >> 16;
284
285 switch (info->fix.visual) {
286 case FB_VISUAL_TRUECOLOR:
287 if (regno >= 16)
288 return -EINVAL;
289
290 /* deal with creating pseudo-palette entries */
291
292 pseudo_val = (red >> 11) << info->var.red.offset;
293 pseudo_val |= (green >> 10) << info->var.green.offset;
294 pseudo_val |= (blue >> 11) << info->var.blue.offset;
295
296 dbg("s1d13xxxfb_setcolreg: pseudo %d, val %08x\n",
297 regno, pseudo_val);
298
316240f6
HT
299#if defined(CONFIG_PLAT_MAPPI)
300 ((u32 *)info->pseudo_palette)[regno] = cpu_to_le16(pseudo_val);
301#else
1da177e4 302 ((u32 *)info->pseudo_palette)[regno] = pseudo_val;
316240f6 303#endif
1da177e4
LT
304
305 break;
306 case FB_VISUAL_PSEUDOCOLOR:
307 s1d13xxxfb_writereg(s1dfb, S1DREG_LKUP_ADDR, regno);
308 s1d13xxxfb_writereg(s1dfb, S1DREG_LKUP_DATA, red);
309 s1d13xxxfb_writereg(s1dfb, S1DREG_LKUP_DATA, green);
310 s1d13xxxfb_writereg(s1dfb, S1DREG_LKUP_DATA, blue);
311
312 break;
313 default:
314 return -ENOSYS;
315 }
316
317 dbg("s1d13xxxfb_setcolreg: done\n");
318
319 return 0;
320}
321
322/**
323 * s1d13xxxfb_blank - blanks the display.
324 * @blank_mode: the blank mode we want.
325 * @info: frame buffer structure that represents a single frame buffer
326 *
327 * Blank the screen if blank_mode != 0, else unblank. Return 0 if
328 * blanking succeeded, != 0 if un-/blanking failed due to e.g. a
329 * video mode which doesn't support it. Implements VESA suspend
330 * and powerdown modes on hardware that supports disabling hsync/vsync:
331 * blank_mode == 2: suspend vsync
332 * blank_mode == 3: suspend hsync
333 * blank_mode == 4: powerdown
334 *
335 * Returns negative errno on error, or zero on success.
336 */
337static int
338s1d13xxxfb_blank(int blank_mode, struct fb_info *info)
339{
340 struct s1d13xxxfb_par *par = info->par;
341
342 dbg("s1d13xxxfb_blank: blank=%d, info=%p\n", blank_mode, info);
343
344 switch (blank_mode) {
345 case FB_BLANK_UNBLANK:
346 case FB_BLANK_NORMAL:
347 if ((par->display & 0x01) != 0)
348 lcd_enable(par, 1);
349 if ((par->display & 0x02) != 0)
350 crt_enable(par, 1);
351 break;
352 case FB_BLANK_VSYNC_SUSPEND:
353 case FB_BLANK_HSYNC_SUSPEND:
354 break;
355 case FB_BLANK_POWERDOWN:
356 lcd_enable(par, 0);
357 crt_enable(par, 0);
358 break;
359 default:
360 return -EINVAL;
361 }
362
363 /* let fbcon do a soft blank for us */
364 return ((blank_mode == FB_BLANK_NORMAL) ? 1 : 0);
365}
366
367/**
3ed167af
KE
368 * s1d13xxxfb_pan_display - Pans the display.
369 * @var: frame buffer variable screen structure
370 * @info: frame buffer structure that represents a single frame buffer
1da177e4
LT
371 *
372 * Pan (or wrap, depending on the `vmode' field) the display using the
3ed167af
KE
373 * `yoffset' field of the `var' structure (`xoffset' not yet supported).
374 * If the values don't fit, return -EINVAL.
1da177e4 375 *
3ed167af 376 * Returns negative errno on error, or zero on success.
1da177e4
LT
377 */
378static int
379s1d13xxxfb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
380{
381 struct s1d13xxxfb_par *par = info->par;
382 u32 start;
383
384 if (var->xoffset != 0) /* not yet ... */
385 return -EINVAL;
386
387 if (var->yoffset + info->var.yres > info->var.yres_virtual)
388 return -EINVAL;
389
390 start = (info->fix.line_length >> 1) * var->yoffset;
391
392 if ((par->display & 0x01)) {
393 /* LCD */
394 s1d13xxxfb_writereg(par, S1DREG_LCD_DISP_START0, (start & 0xff));
395 s1d13xxxfb_writereg(par, S1DREG_LCD_DISP_START1, ((start >> 8) & 0xff));
396 s1d13xxxfb_writereg(par, S1DREG_LCD_DISP_START2, ((start >> 16) & 0x0f));
397 } else {
398 /* CRT */
399 s1d13xxxfb_writereg(par, S1DREG_CRT_DISP_START0, (start & 0xff));
400 s1d13xxxfb_writereg(par, S1DREG_CRT_DISP_START1, ((start >> 8) & 0xff));
401 s1d13xxxfb_writereg(par, S1DREG_CRT_DISP_START2, ((start >> 16) & 0x0f));
402 }
403
404 return 0;
405}
406
3ed167af
KE
407/************************************************************
408 functions to handle bitblt acceleration
409 ************************************************************/
410
3ed167af
KE
411/**
412 * bltbit_wait_bitclear - waits for change in register value
413 * @info : frambuffer structure
414 * @bit : value currently in register
415 * @timeout : ...
416 *
417 * waits until value changes FROM bit
418 *
419 */
420static u8
421bltbit_wait_bitclear(struct fb_info *info, u8 bit, int timeout)
422{
423 while (s1d13xxxfb_readreg(info->par, S1DREG_BBLT_CTL0) & bit) {
424 udelay(10);
425 if (!--timeout) {
426 dbg_blit("wait_bitclear timeout\n");
427 break;
428 }
429 }
430
431 return timeout;
432}
433
3ed167af
KE
434/*
435 * s1d13xxxfb_bitblt_copyarea - accelerated copyarea function
436 * @info : framebuffer structure
437 * @area : fb_copyarea structure
438 *
439 * supports (atleast) S1D13506
440 *
441 */
442static void
443s1d13xxxfb_bitblt_copyarea(struct fb_info *info, const struct fb_copyarea *area)
444{
445 u32 dst, src;
446 u32 stride;
447 u16 reverse = 0;
448 u16 sx = area->sx, sy = area->sy;
449 u16 dx = area->dx, dy = area->dy;
450 u16 width = area->width, height = area->height;
451 u16 bpp;
452
453 spin_lock(&s1d13xxxfb_bitblt_lock);
454
455 /* bytes per xres line */
456 bpp = (info->var.bits_per_pixel >> 3);
457 stride = bpp * info->var.xres;
458
459 /* reverse, calculate the last pixel in rectangle */
460 if ((dy > sy) || ((dy == sy) && (dx >= sx))) {
461 dst = (((dy + height - 1) * stride) + (bpp * (dx + width - 1)));
462 src = (((sy + height - 1) * stride) + (bpp * (sx + width - 1)));
463 reverse = 1;
464 /* not reverse, calculate the first pixel in rectangle */
465 } else { /* (y * xres) + (bpp * x) */
466 dst = (dy * stride) + (bpp * dx);
467 src = (sy * stride) + (bpp * sx);
468 }
469
3ad2f3fb 470 /* set source address */
3ed167af
KE
471 s1d13xxxfb_writereg(info->par, S1DREG_BBLT_SRC_START0, (src & 0xff));
472 s1d13xxxfb_writereg(info->par, S1DREG_BBLT_SRC_START1, (src >> 8) & 0x00ff);
473 s1d13xxxfb_writereg(info->par, S1DREG_BBLT_SRC_START2, (src >> 16) & 0x00ff);
474
3ad2f3fb 475 /* set destination address */
3ed167af
KE
476 s1d13xxxfb_writereg(info->par, S1DREG_BBLT_DST_START0, (dst & 0xff));
477 s1d13xxxfb_writereg(info->par, S1DREG_BBLT_DST_START1, (dst >> 8) & 0x00ff);
478 s1d13xxxfb_writereg(info->par, S1DREG_BBLT_DST_START2, (dst >> 16) & 0x00ff);
479
480 /* program height and width */
481 s1d13xxxfb_writereg(info->par, S1DREG_BBLT_WIDTH0, (width & 0xff) - 1);
482 s1d13xxxfb_writereg(info->par, S1DREG_BBLT_WIDTH1, (width >> 8));
483
484 s1d13xxxfb_writereg(info->par, S1DREG_BBLT_HEIGHT0, (height & 0xff) - 1);
485 s1d13xxxfb_writereg(info->par, S1DREG_BBLT_HEIGHT1, (height >> 8));
486
487 /* negative direction ROP */
488 if (reverse == 1) {
489 dbg_blit("(copyarea) negative rop\n");
490 s1d13xxxfb_writereg(info->par, S1DREG_BBLT_OP, 0x03);
491 } else /* positive direction ROP */ {
492 s1d13xxxfb_writereg(info->par, S1DREG_BBLT_OP, 0x02);
493 dbg_blit("(copyarea) positive rop\n");
494 }
495
496 /* set for rectangel mode and not linear */
497 s1d13xxxfb_writereg(info->par, S1DREG_BBLT_CTL0, 0x0);
498
499 /* setup the bpp 1 = 16bpp, 0 = 8bpp*/
500 s1d13xxxfb_writereg(info->par, S1DREG_BBLT_CTL1, (bpp >> 1));
501
502 /* set words per xres */
503 s1d13xxxfb_writereg(info->par, S1DREG_BBLT_MEM_OFF0, (stride >> 1) & 0xff);
504 s1d13xxxfb_writereg(info->par, S1DREG_BBLT_MEM_OFF1, (stride >> 9));
505
506 dbg_blit("(copyarea) dx=%d, dy=%d\n", dx, dy);
507 dbg_blit("(copyarea) sx=%d, sy=%d\n", sx, sy);
508 dbg_blit("(copyarea) width=%d, height=%d\n", width - 1, height - 1);
509 dbg_blit("(copyarea) stride=%d\n", stride);
510 dbg_blit("(copyarea) bpp=%d=0x0%d, mem_offset1=%d, mem_offset2=%d\n", bpp, (bpp >> 1),
511 (stride >> 1) & 0xff, stride >> 9);
512
513 s1d13xxxfb_writereg(info->par, S1DREG_BBLT_CC_EXP, 0x0c);
514
515 /* initialize the engine */
516 s1d13xxxfb_writereg(info->par, S1DREG_BBLT_CTL0, 0x80);
517
518 /* wait to complete */
519 bltbit_wait_bitclear(info, 0x80, 8000);
520
521 spin_unlock(&s1d13xxxfb_bitblt_lock);
522}
523
524/**
525 *
526 * s1d13xxxfb_bitblt_solidfill - accelerated solidfill function
527 * @info : framebuffer structure
528 * @rect : fb_fillrect structure
529 *
530 * supports (atleast 13506)
531 *
532 **/
533static void
534s1d13xxxfb_bitblt_solidfill(struct fb_info *info, const struct fb_fillrect *rect)
535{
536 u32 screen_stride, dest;
537 u32 fg;
538 u16 bpp = (info->var.bits_per_pixel >> 3);
539
540 /* grab spinlock */
541 spin_lock(&s1d13xxxfb_bitblt_lock);
542
543 /* bytes per x width */
544 screen_stride = (bpp * info->var.xres);
545
546 /* bytes to starting point */
547 dest = ((rect->dy * screen_stride) + (bpp * rect->dx));
548
549 dbg_blit("(solidfill) dx=%d, dy=%d, stride=%d, dest=%d\n"
550 "(solidfill) : rect_width=%d, rect_height=%d\n",
551 rect->dx, rect->dy, screen_stride, dest,
552 rect->width - 1, rect->height - 1);
553
554 dbg_blit("(solidfill) : xres=%d, yres=%d, bpp=%d\n",
555 info->var.xres, info->var.yres,
556 info->var.bits_per_pixel);
557 dbg_blit("(solidfill) : rop=%d\n", rect->rop);
558
559 /* We split the destination into the three registers */
560 s1d13xxxfb_writereg(info->par, S1DREG_BBLT_DST_START0, (dest & 0x00ff));
561 s1d13xxxfb_writereg(info->par, S1DREG_BBLT_DST_START1, ((dest >> 8) & 0x00ff));
562 s1d13xxxfb_writereg(info->par, S1DREG_BBLT_DST_START2, ((dest >> 16) & 0x00ff));
563
564 /* give information regarding rectangel width */
565 s1d13xxxfb_writereg(info->par, S1DREG_BBLT_WIDTH0, ((rect->width) & 0x00ff) - 1);
566 s1d13xxxfb_writereg(info->par, S1DREG_BBLT_WIDTH1, (rect->width >> 8));
567
568 /* give information regarding rectangel height */
569 s1d13xxxfb_writereg(info->par, S1DREG_BBLT_HEIGHT0, ((rect->height) & 0x00ff) - 1);
570 s1d13xxxfb_writereg(info->par, S1DREG_BBLT_HEIGHT1, (rect->height >> 8));
571
572 if (info->fix.visual == FB_VISUAL_TRUECOLOR ||
573 info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
574 fg = ((u32 *)info->pseudo_palette)[rect->color];
575 dbg_blit("(solidfill) truecolor/directcolor\n");
576 dbg_blit("(solidfill) pseudo_palette[%d] = %d\n", rect->color, fg);
577 } else {
578 fg = rect->color;
579 dbg_blit("(solidfill) color = %d\n", rect->color);
580 }
581
582 /* set foreground color */
583 s1d13xxxfb_writereg(info->par, S1DREG_BBLT_FGC0, (fg & 0xff));
584 s1d13xxxfb_writereg(info->par, S1DREG_BBLT_FGC1, (fg >> 8) & 0xff);
585
586 /* set rectangual region of memory (rectangle and not linear) */
587 s1d13xxxfb_writereg(info->par, S1DREG_BBLT_CTL0, 0x0);
588
589 /* set operation mode SOLID_FILL */
590 s1d13xxxfb_writereg(info->par, S1DREG_BBLT_OP, BBLT_SOLID_FILL);
591
592 /* set bits per pixel (1 = 16bpp, 0 = 8bpp) */
593 s1d13xxxfb_writereg(info->par, S1DREG_BBLT_CTL1, (info->var.bits_per_pixel >> 4));
594
595 /* set the memory offset for the bblt in word sizes */
596 s1d13xxxfb_writereg(info->par, S1DREG_BBLT_MEM_OFF0, (screen_stride >> 1) & 0x00ff);
597 s1d13xxxfb_writereg(info->par, S1DREG_BBLT_MEM_OFF1, (screen_stride >> 9));
598
599 /* and away we go.... */
600 s1d13xxxfb_writereg(info->par, S1DREG_BBLT_CTL0, 0x80);
601
602 /* wait until its done */
603 bltbit_wait_bitclear(info, 0x80, 8000);
604
605 /* let others play */
606 spin_unlock(&s1d13xxxfb_bitblt_lock);
607}
608
609/* framebuffer information structures */
1da177e4
LT
610static struct fb_ops s1d13xxxfb_fbops = {
611 .owner = THIS_MODULE,
612 .fb_set_par = s1d13xxxfb_set_par,
613 .fb_setcolreg = s1d13xxxfb_setcolreg,
614 .fb_blank = s1d13xxxfb_blank,
615
616 .fb_pan_display = s1d13xxxfb_pan_display,
617
3ed167af 618 /* gets replaced at chip detection time */
1da177e4
LT
619 .fb_fillrect = cfb_fillrect,
620 .fb_copyarea = cfb_copyarea,
621 .fb_imageblit = cfb_imageblit,
1da177e4
LT
622};
623
48c68c4f 624static int s1d13xxxfb_width_tab[2][4] = {
1da177e4
LT
625 {4, 8, 16, -1},
626 {9, 12, 18, -1},
627};
628
629/**
3ed167af 630 * s1d13xxxfb_fetch_hw_state - Configure the framebuffer according to
1da177e4 631 * hardware setup.
3ed167af 632 * @info: frame buffer structure
1da177e4
LT
633 *
634 * We setup the framebuffer structures according to the current
635 * hardware setup. On some machines, the BIOS will have filled
636 * the chip registers with such info, on others, these values will
637 * have been written in some init procedure. In any case, the
638 * software values needs to match the hardware ones. This is what
639 * this function ensures.
640 *
641 * Note: some of the hardcoded values here might need some love to
642 * work on various chips, and might need to no longer be hardcoded.
643 */
48c68c4f 644static void s1d13xxxfb_fetch_hw_state(struct fb_info *info)
1da177e4
LT
645{
646 struct fb_var_screeninfo *var = &info->var;
647 struct fb_fix_screeninfo *fix = &info->fix;
648 struct s1d13xxxfb_par *par = info->par;
649 u8 panel, display;
650 u16 offset;
651 u32 xres, yres;
652 u32 xres_virtual, yres_virtual;
653 int bpp, lcd_bpp;
654 int is_color, is_dual, is_tft;
655 int lcd_enabled, crt_enabled;
656
657 fix->type = FB_TYPE_PACKED_PIXELS;
658
659 /* general info */
660 par->display = s1d13xxxfb_readreg(par, S1DREG_COM_DISP_MODE);
661 crt_enabled = (par->display & 0x02) != 0;
662 lcd_enabled = (par->display & 0x01) != 0;
663
664 if (lcd_enabled && crt_enabled)
665 printk(KERN_WARNING PFX "Warning: LCD and CRT detected, using LCD\n");
666
667 if (lcd_enabled)
668 display = s1d13xxxfb_readreg(par, S1DREG_LCD_DISP_MODE);
669 else /* CRT */
670 display = s1d13xxxfb_readreg(par, S1DREG_CRT_DISP_MODE);
671
672 bpp = display & 0x07;
673
674 switch (bpp) {
675 case 2: /* 4 bpp */
676 case 3: /* 8 bpp */
677 var->bits_per_pixel = 8;
678 var->red.offset = var->green.offset = var->blue.offset = 0;
679 var->red.length = var->green.length = var->blue.length = 8;
680 break;
681 case 5: /* 16 bpp */
682 s1d13xxxfb_setup_truecolour(info);
683 break;
684 default:
685 dbg("bpp: %i\n", bpp);
686 }
687 fb_alloc_cmap(&info->cmap, 256, 0);
688
689 /* LCD info */
690 panel = s1d13xxxfb_readreg(par, S1DREG_PANEL_TYPE);
691 is_color = (panel & 0x04) != 0;
692 is_dual = (panel & 0x02) != 0;
693 is_tft = (panel & 0x01) != 0;
694 lcd_bpp = s1d13xxxfb_width_tab[is_tft][(panel >> 4) & 3];
695
696 if (lcd_enabled) {
697 xres = (s1d13xxxfb_readreg(par, S1DREG_LCD_DISP_HWIDTH) + 1) * 8;
698 yres = (s1d13xxxfb_readreg(par, S1DREG_LCD_DISP_VHEIGHT0) +
699 ((s1d13xxxfb_readreg(par, S1DREG_LCD_DISP_VHEIGHT1) & 0x03) << 8) + 1);
700
701 offset = (s1d13xxxfb_readreg(par, S1DREG_LCD_MEM_OFF0) +
702 ((s1d13xxxfb_readreg(par, S1DREG_LCD_MEM_OFF1) & 0x7) << 8));
703 } else { /* crt */
704 xres = (s1d13xxxfb_readreg(par, S1DREG_CRT_DISP_HWIDTH) + 1) * 8;
705 yres = (s1d13xxxfb_readreg(par, S1DREG_CRT_DISP_VHEIGHT0) +
706 ((s1d13xxxfb_readreg(par, S1DREG_CRT_DISP_VHEIGHT1) & 0x03) << 8) + 1);
707
708 offset = (s1d13xxxfb_readreg(par, S1DREG_CRT_MEM_OFF0) +
709 ((s1d13xxxfb_readreg(par, S1DREG_CRT_MEM_OFF1) & 0x7) << 8));
710 }
711 xres_virtual = offset * 16 / var->bits_per_pixel;
712 yres_virtual = fix->smem_len / (offset * 2);
713
714 var->xres = xres;
715 var->yres = yres;
716 var->xres_virtual = xres_virtual;
717 var->yres_virtual = yres_virtual;
718 var->xoffset = var->yoffset = 0;
719
720 fix->line_length = offset * 2;
721
722 var->grayscale = !is_color;
723
724 var->activate = FB_ACTIVATE_NOW;
725
726 dbg(PFX "bpp=%d, lcd_bpp=%d, "
727 "crt_enabled=%d, lcd_enabled=%d\n",
728 var->bits_per_pixel, lcd_bpp, crt_enabled, lcd_enabled);
729 dbg(PFX "xres=%d, yres=%d, vxres=%d, vyres=%d "
730 "is_color=%d, is_dual=%d, is_tft=%d\n",
731 xres, yres, xres_virtual, yres_virtual, is_color, is_dual, is_tft);
732}
733
734
27f931da 735static int
3ae5eaec 736s1d13xxxfb_remove(struct platform_device *pdev)
1da177e4 737{
3ae5eaec 738 struct fb_info *info = platform_get_drvdata(pdev);
1da177e4
LT
739 struct s1d13xxxfb_par *par = NULL;
740
741 if (info) {
742 par = info->par;
743 if (par && par->regs) {
744 /* disable output & enable powersave */
745 s1d13xxxfb_writereg(par, S1DREG_COM_DISP_MODE, 0x00);
746 s1d13xxxfb_writereg(par, S1DREG_PS_CNF, 0x11);
747 iounmap(par->regs);
748 }
749
750 fb_dealloc_cmap(&info->cmap);
751
752 if (info->screen_base)
753 iounmap(info->screen_base);
754
755 framebuffer_release(info);
756 }
757
758 release_mem_region(pdev->resource[0].start,
759 pdev->resource[0].end - pdev->resource[0].start +1);
760 release_mem_region(pdev->resource[1].start,
761 pdev->resource[1].end - pdev->resource[1].start +1);
762 return 0;
763}
764
48c68c4f 765static int s1d13xxxfb_probe(struct platform_device *pdev)
1da177e4 766{
1da177e4
LT
767 struct s1d13xxxfb_par *default_par;
768 struct fb_info *info;
769 struct s1d13xxxfb_pdata *pdata = NULL;
770 int ret = 0;
0b178883 771 int i;
afbb9d8d 772 u8 revision, prod_id;
1da177e4 773
28822f22 774 dbg("probe called: device is %p\n", pdev);
1da177e4
LT
775
776 printk(KERN_INFO "Epson S1D13XXX FB Driver\n");
777
778 /* enable platform-dependent hardware glue, if any */
772ee6da
JH
779 if (dev_get_platdata(&pdev->dev))
780 pdata = dev_get_platdata(&pdev->dev);
1da177e4
LT
781
782 if (pdata && pdata->platform_init_video)
783 pdata->platform_init_video();
784
1da177e4
LT
785 if (pdev->num_resources != 2) {
786 dev_err(&pdev->dev, "invalid num_resources: %i\n",
787 pdev->num_resources);
788 ret = -ENODEV;
789 goto bail;
790 }
791
792 /* resource[0] is VRAM, resource[1] is registers */
793 if (pdev->resource[0].flags != IORESOURCE_MEM
794 || pdev->resource[1].flags != IORESOURCE_MEM) {
795 dev_err(&pdev->dev, "invalid resource type\n");
796 ret = -ENODEV;
797 goto bail;
798 }
799
800 if (!request_mem_region(pdev->resource[0].start,
801 pdev->resource[0].end - pdev->resource[0].start +1, "s1d13xxxfb mem")) {
3ae5eaec 802 dev_dbg(&pdev->dev, "request_mem_region failed\n");
1da177e4
LT
803 ret = -EBUSY;
804 goto bail;
805 }
806
807 if (!request_mem_region(pdev->resource[1].start,
808 pdev->resource[1].end - pdev->resource[1].start +1, "s1d13xxxfb regs")) {
3ae5eaec 809 dev_dbg(&pdev->dev, "request_mem_region failed\n");
1da177e4
LT
810 ret = -EBUSY;
811 goto bail;
812 }
813
814 info = framebuffer_alloc(sizeof(struct s1d13xxxfb_par) + sizeof(u32) * 256, &pdev->dev);
815 if (!info) {
816 ret = -ENOMEM;
817 goto bail;
818 }
819
98365f5e 820 platform_set_drvdata(pdev, info);
1da177e4
LT
821 default_par = info->par;
822 default_par->regs = ioremap_nocache(pdev->resource[1].start,
823 pdev->resource[1].end - pdev->resource[1].start +1);
824 if (!default_par->regs) {
825 printk(KERN_ERR PFX "unable to map registers\n");
826 ret = -ENOMEM;
827 goto bail;
828 }
829 info->pseudo_palette = default_par->pseudo_palette;
830
831 info->screen_base = ioremap_nocache(pdev->resource[0].start,
832 pdev->resource[0].end - pdev->resource[0].start +1);
833
834 if (!info->screen_base) {
835 printk(KERN_ERR PFX "unable to map framebuffer\n");
836 ret = -ENOMEM;
837 goto bail;
838 }
839
afbb9d8d
KE
840 /* production id is top 6 bits */
841 prod_id = s1d13xxxfb_readreg(default_par, S1DREG_REV_CODE) >> 2;
842 /* revision id is lower 2 bits */
843 revision = s1d13xxxfb_readreg(default_par, S1DREG_REV_CODE) & 0x3;
0b178883
KE
844 ret = -ENODEV;
845
afbb9d8d
KE
846 for (i = 0; i < ARRAY_SIZE(s1d13xxxfb_prod_ids); i++) {
847 if (prod_id == s1d13xxxfb_prod_ids[i]) {
848 /* looks like we got it in our list */
849 default_par->prod_id = prod_id;
850 default_par->revision = revision;
0b178883 851 ret = 0;
afbb9d8d
KE
852 break;
853 }
0b178883
KE
854 }
855
afbb9d8d
KE
856 if (!ret) {
857 printk(KERN_INFO PFX "chip production id %i = %s\n",
858 prod_id, s1d13xxxfb_prod_names[i]);
0b178883 859 printk(KERN_INFO PFX "chip revision %i\n", revision);
afbb9d8d
KE
860 } else {
861 printk(KERN_INFO PFX
862 "unknown chip production id %i, revision %i\n",
863 prod_id, revision);
a895d57d 864 printk(KERN_INFO PFX "please contact maintainer\n");
1da177e4
LT
865 goto bail;
866 }
867
868 info->fix = s1d13xxxfb_fix;
869 info->fix.mmio_start = pdev->resource[1].start;
3ed167af 870 info->fix.mmio_len = pdev->resource[1].end - pdev->resource[1].start + 1;
1da177e4 871 info->fix.smem_start = pdev->resource[0].start;
3ed167af 872 info->fix.smem_len = pdev->resource[0].end - pdev->resource[0].start + 1;
1da177e4
LT
873
874 printk(KERN_INFO PFX "regs mapped at 0x%p, fb %d KiB mapped at 0x%p\n",
875 default_par->regs, info->fix.smem_len / 1024, info->screen_base);
876
877 info->par = default_par;
1da177e4 878 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
3ed167af
KE
879 info->fbops = &s1d13xxxfb_fbops;
880
881 switch(prod_id) {
882 case S1D13506_PROD_ID: /* activate acceleration */
883 s1d13xxxfb_fbops.fb_fillrect = s1d13xxxfb_bitblt_solidfill;
884 s1d13xxxfb_fbops.fb_copyarea = s1d13xxxfb_bitblt_copyarea;
885 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN |
886 FBINFO_HWACCEL_FILLRECT | FBINFO_HWACCEL_COPYAREA;
887 break;
888 default:
889 break;
890 }
1da177e4
LT
891
892 /* perform "manual" chip initialization, if needed */
893 if (pdata && pdata->initregs)
894 s1d13xxxfb_runinit(info->par, pdata->initregs, pdata->initregssize);
895
896 s1d13xxxfb_fetch_hw_state(info);
897
898 if (register_framebuffer(info) < 0) {
899 ret = -EINVAL;
900 goto bail;
901 }
902
31b6780c 903 fb_info(info, "%s frame buffer device\n", info->fix.id);
1da177e4
LT
904
905 return 0;
906
907bail:
3ae5eaec 908 s1d13xxxfb_remove(pdev);
1da177e4
LT
909 return ret;
910
911}
912
913#ifdef CONFIG_PM
3ae5eaec 914static int s1d13xxxfb_suspend(struct platform_device *dev, pm_message_t state)
1da177e4 915{
3ae5eaec 916 struct fb_info *info = platform_get_drvdata(dev);
1da177e4
LT
917 struct s1d13xxxfb_par *s1dfb = info->par;
918 struct s1d13xxxfb_pdata *pdata = NULL;
919
920 /* disable display */
921 lcd_enable(s1dfb, 0);
922 crt_enable(s1dfb, 0);
923
772ee6da
JH
924 if (dev_get_platdata(&dev->dev))
925 pdata = dev_get_platdata(&dev->dev);
1da177e4
LT
926
927#if 0
928 if (!s1dfb->disp_save)
929 s1dfb->disp_save = kmalloc(info->fix.smem_len, GFP_KERNEL);
930
931 if (!s1dfb->disp_save) {
3f3ed26b 932 printk(KERN_ERR PFX "no memory to save screen\n");
1da177e4
LT
933 return -ENOMEM;
934 }
935
936 memcpy_fromio(s1dfb->disp_save, info->screen_base, info->fix.smem_len);
937#else
938 s1dfb->disp_save = NULL;
939#endif
940
941 if (!s1dfb->regs_save)
942 s1dfb->regs_save = kmalloc(info->fix.mmio_len, GFP_KERNEL);
943
944 if (!s1dfb->regs_save) {
945 printk(KERN_ERR PFX "no memory to save registers");
946 return -ENOMEM;
947 }
948
949 /* backup all registers */
950 memcpy_fromio(s1dfb->regs_save, s1dfb->regs, info->fix.mmio_len);
951
952 /* now activate power save mode */
953 s1d13xxxfb_writereg(s1dfb, S1DREG_PS_CNF, 0x11);
954
955 if (pdata && pdata->platform_suspend_video)
956 return pdata->platform_suspend_video();
957 else
958 return 0;
959}
960
3ae5eaec 961static int s1d13xxxfb_resume(struct platform_device *dev)
1da177e4 962{
3ae5eaec 963 struct fb_info *info = platform_get_drvdata(dev);
1da177e4
LT
964 struct s1d13xxxfb_par *s1dfb = info->par;
965 struct s1d13xxxfb_pdata *pdata = NULL;
966
1da177e4
LT
967 /* awaken the chip */
968 s1d13xxxfb_writereg(s1dfb, S1DREG_PS_CNF, 0x10);
969
970 /* do not let go until SDRAM "wakes up" */
971 while ((s1d13xxxfb_readreg(s1dfb, S1DREG_PS_STATUS) & 0x01))
972 udelay(10);
973
772ee6da
JH
974 if (dev_get_platdata(&dev->dev))
975 pdata = dev_get_platdata(&dev->dev);
1da177e4
LT
976
977 if (s1dfb->regs_save) {
978 /* will write RO regs, *should* get away with it :) */
979 memcpy_toio(s1dfb->regs, s1dfb->regs_save, info->fix.mmio_len);
980 kfree(s1dfb->regs_save);
981 }
982
983 if (s1dfb->disp_save) {
984 memcpy_toio(info->screen_base, s1dfb->disp_save,
985 info->fix.smem_len);
986 kfree(s1dfb->disp_save); /* XXX kmalloc()'d when? */
987 }
988
989 if ((s1dfb->display & 0x01) != 0)
990 lcd_enable(s1dfb, 1);
991 if ((s1dfb->display & 0x02) != 0)
992 crt_enable(s1dfb, 1);
993
994 if (pdata && pdata->platform_resume_video)
995 return pdata->platform_resume_video();
996 else
997 return 0;
998}
999#endif /* CONFIG_PM */
1000
3ae5eaec 1001static struct platform_driver s1d13xxxfb_driver = {
1da177e4
LT
1002 .probe = s1d13xxxfb_probe,
1003 .remove = s1d13xxxfb_remove,
1004#ifdef CONFIG_PM
1005 .suspend = s1d13xxxfb_suspend,
3ae5eaec 1006 .resume = s1d13xxxfb_resume,
1da177e4 1007#endif
3ae5eaec
RK
1008 .driver = {
1009 .name = S1D_DEVICENAME,
1010 },
1da177e4
LT
1011};
1012
1013
1014static int __init
1015s1d13xxxfb_init(void)
1016{
28822f22
SB
1017
1018#ifndef MODULE
1da177e4
LT
1019 if (fb_get_options("s1d13xxxfb", NULL))
1020 return -ENODEV;
28822f22 1021#endif
1da177e4 1022
3ae5eaec 1023 return platform_driver_register(&s1d13xxxfb_driver);
1da177e4
LT
1024}
1025
1026
1027static void __exit
1028s1d13xxxfb_exit(void)
1029{
3ae5eaec 1030 platform_driver_unregister(&s1d13xxxfb_driver);
1da177e4
LT
1031}
1032
1033module_init(s1d13xxxfb_init);
1034module_exit(s1d13xxxfb_exit);
1035
1036
1037MODULE_LICENSE("GPL");
1038MODULE_DESCRIPTION("Framebuffer driver for S1D13xxx devices");
1039MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, Thibaut VARENE <varenet@parisc-linux.org>");