s1d13xxxfb.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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 { no_printk(KERN_INFO fmt, ## args); } 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. * s1d13xxxfb_bitblt_solidfill - accelerated solidfill function
  433. * @info : framebuffer structure
  434. * @rect : fb_fillrect structure
  435. *
  436. * supports (atleast 13506)
  437. *
  438. **/
  439. static void
  440. s1d13xxxfb_bitblt_solidfill(struct fb_info *info, const struct fb_fillrect *rect)
  441. {
  442. u32 screen_stride, dest;
  443. u32 fg;
  444. u16 bpp = (info->var.bits_per_pixel >> 3);
  445. /* grab spinlock */
  446. spin_lock(&s1d13xxxfb_bitblt_lock);
  447. /* bytes per x width */
  448. screen_stride = (bpp * info->var.xres);
  449. /* bytes to starting point */
  450. dest = ((rect->dy * screen_stride) + (bpp * rect->dx));
  451. dbg_blit("(solidfill) dx=%d, dy=%d, stride=%d, dest=%d\n"
  452. "(solidfill) : rect_width=%d, rect_height=%d\n",
  453. rect->dx, rect->dy, screen_stride, dest,
  454. rect->width - 1, rect->height - 1);
  455. dbg_blit("(solidfill) : xres=%d, yres=%d, bpp=%d\n",
  456. info->var.xres, info->var.yres,
  457. info->var.bits_per_pixel);
  458. dbg_blit("(solidfill) : rop=%d\n", rect->rop);
  459. /* We split the destination into the three registers */
  460. s1d13xxxfb_writereg(info->par, S1DREG_BBLT_DST_START0, (dest & 0x00ff));
  461. s1d13xxxfb_writereg(info->par, S1DREG_BBLT_DST_START1, ((dest >> 8) & 0x00ff));
  462. s1d13xxxfb_writereg(info->par, S1DREG_BBLT_DST_START2, ((dest >> 16) & 0x00ff));
  463. /* give information regarding rectangel width */
  464. s1d13xxxfb_writereg(info->par, S1DREG_BBLT_WIDTH0, ((rect->width) & 0x00ff) - 1);
  465. s1d13xxxfb_writereg(info->par, S1DREG_BBLT_WIDTH1, (rect->width >> 8));
  466. /* give information regarding rectangel height */
  467. s1d13xxxfb_writereg(info->par, S1DREG_BBLT_HEIGHT0, ((rect->height) & 0x00ff) - 1);
  468. s1d13xxxfb_writereg(info->par, S1DREG_BBLT_HEIGHT1, (rect->height >> 8));
  469. if (info->fix.visual == FB_VISUAL_TRUECOLOR ||
  470. info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
  471. fg = ((u32 *)info->pseudo_palette)[rect->color];
  472. dbg_blit("(solidfill) truecolor/directcolor\n");
  473. dbg_blit("(solidfill) pseudo_palette[%d] = %d\n", rect->color, fg);
  474. } else {
  475. fg = rect->color;
  476. dbg_blit("(solidfill) color = %d\n", rect->color);
  477. }
  478. /* set foreground color */
  479. s1d13xxxfb_writereg(info->par, S1DREG_BBLT_FGC0, (fg & 0xff));
  480. s1d13xxxfb_writereg(info->par, S1DREG_BBLT_FGC1, (fg >> 8) & 0xff);
  481. /* set rectangual region of memory (rectangle and not linear) */
  482. s1d13xxxfb_writereg(info->par, S1DREG_BBLT_CTL0, 0x0);
  483. /* set operation mode SOLID_FILL */
  484. s1d13xxxfb_writereg(info->par, S1DREG_BBLT_OP, BBLT_SOLID_FILL);
  485. /* set bits per pixel (1 = 16bpp, 0 = 8bpp) */
  486. s1d13xxxfb_writereg(info->par, S1DREG_BBLT_CTL1, (info->var.bits_per_pixel >> 4));
  487. /* set the memory offset for the bblt in word sizes */
  488. s1d13xxxfb_writereg(info->par, S1DREG_BBLT_MEM_OFF0, (screen_stride >> 1) & 0x00ff);
  489. s1d13xxxfb_writereg(info->par, S1DREG_BBLT_MEM_OFF1, (screen_stride >> 9));
  490. /* and away we go.... */
  491. s1d13xxxfb_writereg(info->par, S1DREG_BBLT_CTL0, 0x80);
  492. /* wait until its done */
  493. bltbit_wait_bitclear(info, 0x80, 8000);
  494. /* let others play */
  495. spin_unlock(&s1d13xxxfb_bitblt_lock);
  496. }
  497. /* framebuffer information structures */
  498. static const struct fb_ops s1d13xxxfb_fbops = {
  499. .owner = THIS_MODULE,
  500. FB_DEFAULT_IOMEM_OPS,
  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. };
  506. static const struct fb_ops s1d13xxxfb_fbops_s1d13506 = {
  507. .owner = THIS_MODULE,
  508. __FB_DEFAULT_IOMEM_OPS_RDWR,
  509. .fb_set_par = s1d13xxxfb_set_par,
  510. .fb_setcolreg = s1d13xxxfb_setcolreg,
  511. .fb_blank = s1d13xxxfb_blank,
  512. .fb_pan_display = s1d13xxxfb_pan_display,
  513. .fb_fillrect = s1d13xxxfb_bitblt_solidfill,
  514. .fb_copyarea = s1d13xxxfb_bitblt_copyarea,
  515. .fb_imageblit = cfb_imageblit,
  516. __FB_DEFAULT_IOMEM_OPS_MMAP,
  517. };
  518. static int s1d13xxxfb_width_tab[2][4] = {
  519. {4, 8, 16, -1},
  520. {9, 12, 18, -1},
  521. };
  522. /**
  523. * s1d13xxxfb_fetch_hw_state - Configure the framebuffer according to
  524. * hardware setup.
  525. * @info: frame buffer structure
  526. *
  527. * We setup the framebuffer structures according to the current
  528. * hardware setup. On some machines, the BIOS will have filled
  529. * the chip registers with such info, on others, these values will
  530. * have been written in some init procedure. In any case, the
  531. * software values needs to match the hardware ones. This is what
  532. * this function ensures.
  533. *
  534. * Note: some of the hardcoded values here might need some love to
  535. * work on various chips, and might need to no longer be hardcoded.
  536. */
  537. static void s1d13xxxfb_fetch_hw_state(struct fb_info *info)
  538. {
  539. struct fb_var_screeninfo *var = &info->var;
  540. struct fb_fix_screeninfo *fix = &info->fix;
  541. struct s1d13xxxfb_par *par = info->par;
  542. u8 panel, display;
  543. u16 offset;
  544. u32 xres, yres;
  545. u32 xres_virtual, yres_virtual;
  546. int bpp, lcd_bpp;
  547. int is_color, is_dual, is_tft;
  548. int lcd_enabled, crt_enabled;
  549. fix->type = FB_TYPE_PACKED_PIXELS;
  550. /* general info */
  551. par->display = s1d13xxxfb_readreg(par, S1DREG_COM_DISP_MODE);
  552. crt_enabled = (par->display & 0x02) != 0;
  553. lcd_enabled = (par->display & 0x01) != 0;
  554. if (lcd_enabled && crt_enabled)
  555. printk(KERN_WARNING PFX "Warning: LCD and CRT detected, using LCD\n");
  556. if (lcd_enabled)
  557. display = s1d13xxxfb_readreg(par, S1DREG_LCD_DISP_MODE);
  558. else /* CRT */
  559. display = s1d13xxxfb_readreg(par, S1DREG_CRT_DISP_MODE);
  560. bpp = display & 0x07;
  561. switch (bpp) {
  562. case 2: /* 4 bpp */
  563. case 3: /* 8 bpp */
  564. var->bits_per_pixel = 8;
  565. var->red.offset = var->green.offset = var->blue.offset = 0;
  566. var->red.length = var->green.length = var->blue.length = 8;
  567. break;
  568. case 5: /* 16 bpp */
  569. s1d13xxxfb_setup_truecolour(info);
  570. break;
  571. default:
  572. dbg("bpp: %i\n", bpp);
  573. }
  574. fb_alloc_cmap(&info->cmap, 256, 0);
  575. /* LCD info */
  576. panel = s1d13xxxfb_readreg(par, S1DREG_PANEL_TYPE);
  577. is_color = (panel & 0x04) != 0;
  578. is_dual = (panel & 0x02) != 0;
  579. is_tft = (panel & 0x01) != 0;
  580. lcd_bpp = s1d13xxxfb_width_tab[is_tft][(panel >> 4) & 3];
  581. if (lcd_enabled) {
  582. xres = (s1d13xxxfb_readreg(par, S1DREG_LCD_DISP_HWIDTH) + 1) * 8;
  583. yres = (s1d13xxxfb_readreg(par, S1DREG_LCD_DISP_VHEIGHT0) +
  584. ((s1d13xxxfb_readreg(par, S1DREG_LCD_DISP_VHEIGHT1) & 0x03) << 8) + 1);
  585. offset = (s1d13xxxfb_readreg(par, S1DREG_LCD_MEM_OFF0) +
  586. ((s1d13xxxfb_readreg(par, S1DREG_LCD_MEM_OFF1) & 0x7) << 8));
  587. } else { /* crt */
  588. xres = (s1d13xxxfb_readreg(par, S1DREG_CRT_DISP_HWIDTH) + 1) * 8;
  589. yres = (s1d13xxxfb_readreg(par, S1DREG_CRT_DISP_VHEIGHT0) +
  590. ((s1d13xxxfb_readreg(par, S1DREG_CRT_DISP_VHEIGHT1) & 0x03) << 8) + 1);
  591. offset = (s1d13xxxfb_readreg(par, S1DREG_CRT_MEM_OFF0) +
  592. ((s1d13xxxfb_readreg(par, S1DREG_CRT_MEM_OFF1) & 0x7) << 8));
  593. }
  594. xres_virtual = offset * 16 / var->bits_per_pixel;
  595. yres_virtual = fix->smem_len / (offset * 2);
  596. var->xres = xres;
  597. var->yres = yres;
  598. var->xres_virtual = xres_virtual;
  599. var->yres_virtual = yres_virtual;
  600. var->xoffset = var->yoffset = 0;
  601. fix->line_length = offset * 2;
  602. var->grayscale = !is_color;
  603. var->activate = FB_ACTIVATE_NOW;
  604. dbg(PFX "bpp=%d, lcd_bpp=%d, "
  605. "crt_enabled=%d, lcd_enabled=%d\n",
  606. var->bits_per_pixel, lcd_bpp, crt_enabled, lcd_enabled);
  607. dbg(PFX "xres=%d, yres=%d, vxres=%d, vyres=%d "
  608. "is_color=%d, is_dual=%d, is_tft=%d\n",
  609. xres, yres, xres_virtual, yres_virtual, is_color, is_dual, is_tft);
  610. }
  611. static void __s1d13xxxfb_remove(struct platform_device *pdev)
  612. {
  613. struct fb_info *info = platform_get_drvdata(pdev);
  614. struct s1d13xxxfb_par *par = NULL;
  615. if (info) {
  616. par = info->par;
  617. if (par && par->regs) {
  618. /* disable output & enable powersave */
  619. s1d13xxxfb_writereg(par, S1DREG_COM_DISP_MODE, 0x00);
  620. s1d13xxxfb_writereg(par, S1DREG_PS_CNF, 0x11);
  621. iounmap(par->regs);
  622. }
  623. fb_dealloc_cmap(&info->cmap);
  624. if (info->screen_base)
  625. iounmap(info->screen_base);
  626. framebuffer_release(info);
  627. }
  628. release_mem_region(pdev->resource[0].start,
  629. resource_size(&pdev->resource[0]));
  630. release_mem_region(pdev->resource[1].start,
  631. resource_size(&pdev->resource[1]));
  632. }
  633. static void s1d13xxxfb_remove(struct platform_device *pdev)
  634. {
  635. struct fb_info *info = platform_get_drvdata(pdev);
  636. unregister_framebuffer(info);
  637. __s1d13xxxfb_remove(pdev);
  638. }
  639. static int 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 (dev_get_platdata(&pdev->dev))
  651. pdata = dev_get_platdata(&pdev->dev);
  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. resource_size(&pdev->resource[0]), "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. resource_size(&pdev->resource[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(pdev->resource[1].start,
  687. resource_size(&pdev->resource[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(pdev->resource[0].start,
  695. resource_size(&pdev->resource[0]));
  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 contact 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 = resource_size(&pdev->resource[1]);
  729. info->fix.smem_start = pdev->resource[0].start;
  730. info->fix.smem_len = resource_size(&pdev->resource[0]);
  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. switch(prod_id) {
  735. case S1D13506_PROD_ID: /* activate acceleration */
  736. info->flags = FBINFO_HWACCEL_YPAN |
  737. FBINFO_HWACCEL_FILLRECT | FBINFO_HWACCEL_COPYAREA;
  738. info->fbops = &s1d13xxxfb_fbops_s1d13506;
  739. break;
  740. default:
  741. info->flags = FBINFO_HWACCEL_YPAN;
  742. info->fbops = &s1d13xxxfb_fbops;
  743. break;
  744. }
  745. /* perform "manual" chip initialization, if needed */
  746. if (pdata && pdata->initregs)
  747. s1d13xxxfb_runinit(info->par, pdata->initregs, pdata->initregssize);
  748. s1d13xxxfb_fetch_hw_state(info);
  749. if (register_framebuffer(info) < 0) {
  750. ret = -EINVAL;
  751. goto bail;
  752. }
  753. fb_info(info, "%s frame buffer device\n", info->fix.id);
  754. return 0;
  755. bail:
  756. __s1d13xxxfb_remove(pdev);
  757. return ret;
  758. }
  759. #ifdef CONFIG_PM
  760. static int s1d13xxxfb_suspend(struct platform_device *dev, pm_message_t state)
  761. {
  762. struct fb_info *info = platform_get_drvdata(dev);
  763. struct s1d13xxxfb_par *s1dfb = info->par;
  764. struct s1d13xxxfb_pdata *pdata = NULL;
  765. /* disable display */
  766. lcd_enable(s1dfb, 0);
  767. crt_enable(s1dfb, 0);
  768. if (dev_get_platdata(&dev->dev))
  769. pdata = dev_get_platdata(&dev->dev);
  770. #if 0
  771. if (!s1dfb->disp_save)
  772. s1dfb->disp_save = kmalloc(info->fix.smem_len, GFP_KERNEL);
  773. if (!s1dfb->disp_save) {
  774. printk(KERN_ERR PFX "no memory to save screen\n");
  775. return -ENOMEM;
  776. }
  777. memcpy_fromio(s1dfb->disp_save, info->screen_base, info->fix.smem_len);
  778. #else
  779. s1dfb->disp_save = NULL;
  780. #endif
  781. if (!s1dfb->regs_save)
  782. s1dfb->regs_save = kmalloc(info->fix.mmio_len, GFP_KERNEL);
  783. if (!s1dfb->regs_save) {
  784. printk(KERN_ERR PFX "no memory to save registers");
  785. return -ENOMEM;
  786. }
  787. /* backup all registers */
  788. memcpy_fromio(s1dfb->regs_save, s1dfb->regs, info->fix.mmio_len);
  789. /* now activate power save mode */
  790. s1d13xxxfb_writereg(s1dfb, S1DREG_PS_CNF, 0x11);
  791. if (pdata && pdata->platform_suspend_video)
  792. return pdata->platform_suspend_video();
  793. else
  794. return 0;
  795. }
  796. static int s1d13xxxfb_resume(struct platform_device *dev)
  797. {
  798. struct fb_info *info = platform_get_drvdata(dev);
  799. struct s1d13xxxfb_par *s1dfb = info->par;
  800. struct s1d13xxxfb_pdata *pdata = NULL;
  801. /* awaken the chip */
  802. s1d13xxxfb_writereg(s1dfb, S1DREG_PS_CNF, 0x10);
  803. /* do not let go until SDRAM "wakes up" */
  804. while ((s1d13xxxfb_readreg(s1dfb, S1DREG_PS_STATUS) & 0x01))
  805. udelay(10);
  806. if (dev_get_platdata(&dev->dev))
  807. pdata = dev_get_platdata(&dev->dev);
  808. if (s1dfb->regs_save) {
  809. /* will write RO regs, *should* get away with it :) */
  810. memcpy_toio(s1dfb->regs, s1dfb->regs_save, info->fix.mmio_len);
  811. kfree(s1dfb->regs_save);
  812. }
  813. if (s1dfb->disp_save) {
  814. memcpy_toio(info->screen_base, s1dfb->disp_save,
  815. info->fix.smem_len);
  816. kfree(s1dfb->disp_save); /* XXX kmalloc()'d when? */
  817. }
  818. if ((s1dfb->display & 0x01) != 0)
  819. lcd_enable(s1dfb, 1);
  820. if ((s1dfb->display & 0x02) != 0)
  821. crt_enable(s1dfb, 1);
  822. if (pdata && pdata->platform_resume_video)
  823. return pdata->platform_resume_video();
  824. else
  825. return 0;
  826. }
  827. #endif /* CONFIG_PM */
  828. static struct platform_driver s1d13xxxfb_driver = {
  829. .probe = s1d13xxxfb_probe,
  830. .remove = s1d13xxxfb_remove,
  831. #ifdef CONFIG_PM
  832. .suspend = s1d13xxxfb_suspend,
  833. .resume = s1d13xxxfb_resume,
  834. #endif
  835. .driver = {
  836. .name = S1D_DEVICENAME,
  837. },
  838. };
  839. static int __init
  840. s1d13xxxfb_init(void)
  841. {
  842. #ifndef MODULE
  843. if (fb_get_options("s1d13xxxfb", NULL))
  844. return -ENODEV;
  845. #endif
  846. return platform_driver_register(&s1d13xxxfb_driver);
  847. }
  848. static void __exit
  849. s1d13xxxfb_exit(void)
  850. {
  851. platform_driver_unregister(&s1d13xxxfb_driver);
  852. }
  853. module_init(s1d13xxxfb_init);
  854. module_exit(s1d13xxxfb_exit);
  855. MODULE_LICENSE("GPL");
  856. MODULE_DESCRIPTION("Framebuffer driver for S1D13xxx devices");
  857. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, Thibaut VARENE <varenet@parisc-linux.org>");