s1d13xxxfb.c 28 KB

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