s1d13xxxfb.c 28 KB

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