hitfb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. * linux/drivers/video/hitfb.c -- Hitachi LCD frame buffer device
  3. *
  4. * (C) 1999 Mihai Spatar
  5. * (C) 2000 YAEGASHI Takeshi
  6. * (C) 2003, 2004 Paul Mundt
  7. * (C) 2003, 2004, 2006 Andriy Skulysh
  8. *
  9. * This file is subject to the terms and conditions of the GNU General Public
  10. * License. See the file COPYING in the main directory of this archive for
  11. * more details.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/errno.h>
  16. #include <linux/string.h>
  17. #include <linux/mm.h>
  18. #include <linux/delay.h>
  19. #include <linux/init.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/fb.h>
  22. #include <asm/machvec.h>
  23. #include <asm/uaccess.h>
  24. #include <asm/pgtable.h>
  25. #include <asm/io.h>
  26. #include <asm/hd64461.h>
  27. #include <cpu/dac.h>
  28. #define WIDTH 640
  29. static struct fb_var_screeninfo hitfb_var = {
  30. .activate = FB_ACTIVATE_NOW,
  31. .height = -1,
  32. .width = -1,
  33. .vmode = FB_VMODE_NONINTERLACED,
  34. };
  35. static struct fb_fix_screeninfo hitfb_fix = {
  36. .id = "Hitachi HD64461",
  37. .type = FB_TYPE_PACKED_PIXELS,
  38. .accel = FB_ACCEL_NONE,
  39. };
  40. static inline void hitfb_accel_wait(void)
  41. {
  42. while (fb_readw(HD64461_GRCFGR) & HD64461_GRCFGR_ACCSTATUS) ;
  43. }
  44. static inline void hitfb_accel_start(int truecolor)
  45. {
  46. if (truecolor) {
  47. fb_writew(6, HD64461_GRCFGR);
  48. } else {
  49. fb_writew(7, HD64461_GRCFGR);
  50. }
  51. }
  52. static inline void hitfb_accel_set_dest(int truecolor, u16 dx, u16 dy,
  53. u16 width, u16 height)
  54. {
  55. u32 saddr = WIDTH * dy + dx;
  56. if (truecolor)
  57. saddr <<= 1;
  58. fb_writew(width-1, HD64461_BBTDWR);
  59. fb_writew(height-1, HD64461_BBTDHR);
  60. fb_writew(saddr & 0xffff, HD64461_BBTDSARL);
  61. fb_writew(saddr >> 16, HD64461_BBTDSARH);
  62. }
  63. static inline void hitfb_accel_bitblt(int truecolor, u16 sx, u16 sy, u16 dx,
  64. u16 dy, u16 width, u16 height, u16 rop,
  65. u32 mask_addr)
  66. {
  67. u32 saddr, daddr;
  68. u32 maddr = 0;
  69. height--;
  70. width--;
  71. fb_writew(rop, HD64461_BBTROPR);
  72. if ((sy < dy) || ((sy == dy) && (sx <= dx))) {
  73. saddr = WIDTH * (sy + height) + sx + width;
  74. daddr = WIDTH * (dy + height) + dx + width;
  75. if (mask_addr) {
  76. if (truecolor)
  77. maddr = ((width >> 3) + 1) * (height + 1) - 1;
  78. else
  79. maddr =
  80. (((width >> 4) + 1) * (height + 1) - 1) * 2;
  81. fb_writew((1 << 5) | 1, HD64461_BBTMDR);
  82. } else
  83. fb_writew(1, HD64461_BBTMDR);
  84. } else {
  85. saddr = WIDTH * sy + sx;
  86. daddr = WIDTH * dy + dx;
  87. if (mask_addr) {
  88. fb_writew((1 << 5), HD64461_BBTMDR);
  89. } else {
  90. fb_writew(0, HD64461_BBTMDR);
  91. }
  92. }
  93. if (truecolor) {
  94. saddr <<= 1;
  95. daddr <<= 1;
  96. }
  97. fb_writew(width, HD64461_BBTDWR);
  98. fb_writew(height, HD64461_BBTDHR);
  99. fb_writew(saddr & 0xffff, HD64461_BBTSSARL);
  100. fb_writew(saddr >> 16, HD64461_BBTSSARH);
  101. fb_writew(daddr & 0xffff, HD64461_BBTDSARL);
  102. fb_writew(daddr >> 16, HD64461_BBTDSARH);
  103. if (mask_addr) {
  104. maddr += mask_addr;
  105. fb_writew(maddr & 0xffff, HD64461_BBTMARL);
  106. fb_writew(maddr >> 16, HD64461_BBTMARH);
  107. }
  108. hitfb_accel_start(truecolor);
  109. }
  110. static void hitfb_fillrect(struct fb_info *p, const struct fb_fillrect *rect)
  111. {
  112. if (rect->rop != ROP_COPY)
  113. cfb_fillrect(p, rect);
  114. else {
  115. hitfb_accel_wait();
  116. fb_writew(0x00f0, HD64461_BBTROPR);
  117. fb_writew(16, HD64461_BBTMDR);
  118. if (p->var.bits_per_pixel == 16) {
  119. fb_writew(((u32 *) (p->pseudo_palette))[rect->color],
  120. HD64461_GRSCR);
  121. hitfb_accel_set_dest(1, rect->dx, rect->dy, rect->width,
  122. rect->height);
  123. hitfb_accel_start(1);
  124. } else {
  125. fb_writew(rect->color, HD64461_GRSCR);
  126. hitfb_accel_set_dest(0, rect->dx, rect->dy, rect->width,
  127. rect->height);
  128. hitfb_accel_start(0);
  129. }
  130. }
  131. }
  132. static void hitfb_copyarea(struct fb_info *p, const struct fb_copyarea *area)
  133. {
  134. hitfb_accel_wait();
  135. hitfb_accel_bitblt(p->var.bits_per_pixel == 16, area->sx, area->sy,
  136. area->dx, area->dy, area->width, area->height,
  137. 0x00cc, 0);
  138. }
  139. static int hitfb_pan_display(struct fb_var_screeninfo *var,
  140. struct fb_info *info)
  141. {
  142. int xoffset = var->xoffset;
  143. int yoffset = var->yoffset;
  144. if (xoffset != 0)
  145. return -EINVAL;
  146. fb_writew((yoffset*info->fix.line_length)>>10, HD64461_LCDCBAR);
  147. return 0;
  148. }
  149. int hitfb_blank(int blank_mode, struct fb_info *info)
  150. {
  151. unsigned short v;
  152. if (blank_mode) {
  153. v = fb_readw(HD64461_LDR1);
  154. v &= ~HD64461_LDR1_DON;
  155. fb_writew(v, HD64461_LDR1);
  156. v = fb_readw(HD64461_LCDCCR);
  157. v |= HD64461_LCDCCR_MOFF;
  158. fb_writew(v, HD64461_LCDCCR);
  159. v = fb_readw(HD64461_STBCR);
  160. v |= HD64461_STBCR_SLCDST;
  161. fb_writew(v, HD64461_STBCR);
  162. } else {
  163. v = fb_readw(HD64461_STBCR);
  164. v &= ~HD64461_STBCR_SLCDST;
  165. fb_writew(v, HD64461_STBCR);
  166. v = fb_readw(HD64461_LCDCCR);
  167. v &= ~(HD64461_LCDCCR_MOFF | HD64461_LCDCCR_STREQ);
  168. fb_writew(v, HD64461_LCDCCR);
  169. do {
  170. v = fb_readw(HD64461_LCDCCR);
  171. } while(v&HD64461_LCDCCR_STBACK);
  172. v = fb_readw(HD64461_LDR1);
  173. v |= HD64461_LDR1_DON;
  174. fb_writew(v, HD64461_LDR1);
  175. }
  176. return 0;
  177. }
  178. static int hitfb_setcolreg(unsigned regno, unsigned red, unsigned green,
  179. unsigned blue, unsigned transp, struct fb_info *info)
  180. {
  181. if (regno >= 256)
  182. return 1;
  183. switch (info->var.bits_per_pixel) {
  184. case 8:
  185. fb_writew(regno << 8, HD64461_CPTWAR);
  186. fb_writew(red >> 10, HD64461_CPTWDR);
  187. fb_writew(green >> 10, HD64461_CPTWDR);
  188. fb_writew(blue >> 10, HD64461_CPTWDR);
  189. break;
  190. case 16:
  191. if (regno >= 16)
  192. return 1;
  193. ((u32 *) (info->pseudo_palette))[regno] =
  194. ((red & 0xf800)) |
  195. ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
  196. break;
  197. }
  198. return 0;
  199. }
  200. static int hitfb_sync(struct fb_info *info)
  201. {
  202. hitfb_accel_wait();
  203. return 0;
  204. }
  205. static int hitfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  206. {
  207. int maxy;
  208. var->xres = info->var.xres;
  209. var->xres_virtual = info->var.xres;
  210. var->yres = info->var.yres;
  211. if ((var->bits_per_pixel != 8) && (var->bits_per_pixel != 16))
  212. var->bits_per_pixel = info->var.bits_per_pixel;
  213. if (var->yres_virtual < var->yres)
  214. var->yres_virtual = var->yres;
  215. maxy = info->fix.smem_len / var->xres;
  216. if (var->bits_per_pixel == 16)
  217. maxy /= 2;
  218. if (var->yres_virtual > maxy)
  219. var->yres_virtual = maxy;
  220. var->xoffset = 0;
  221. var->yoffset = 0;
  222. switch (var->bits_per_pixel) {
  223. case 8:
  224. var->red.offset = 0;
  225. var->red.length = 8;
  226. var->green.offset = 0;
  227. var->green.length = 8;
  228. var->blue.offset = 0;
  229. var->blue.length = 8;
  230. var->transp.offset = 0;
  231. var->transp.length = 0;
  232. break;
  233. case 16: /* RGB 565 */
  234. var->red.offset = 11;
  235. var->red.length = 5;
  236. var->green.offset = 5;
  237. var->green.length = 6;
  238. var->blue.offset = 0;
  239. var->blue.length = 5;
  240. var->transp.offset = 0;
  241. var->transp.length = 0;
  242. break;
  243. }
  244. return 0;
  245. }
  246. static int hitfb_set_par(struct fb_info *info)
  247. {
  248. unsigned short ldr3;
  249. switch (info->var.bits_per_pixel) {
  250. case 8:
  251. info->fix.line_length = info->var.xres;
  252. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  253. info->fix.ypanstep = 16;
  254. break;
  255. case 16:
  256. info->fix.line_length = info->var.xres*2;
  257. info->fix.visual = FB_VISUAL_TRUECOLOR;
  258. info->fix.ypanstep = 8;
  259. break;
  260. }
  261. fb_writew(info->fix.line_length, HD64461_LCDCLOR);
  262. ldr3 = fb_readw(HD64461_LDR3);
  263. ldr3 &= ~15;
  264. ldr3 |= (info->var.bits_per_pixel == 8) ? 4 : 8;
  265. fb_writew(ldr3, HD64461_LDR3);
  266. return 0;
  267. }
  268. static struct fb_ops hitfb_ops = {
  269. .owner = THIS_MODULE,
  270. .fb_check_var = hitfb_check_var,
  271. .fb_set_par = hitfb_set_par,
  272. .fb_setcolreg = hitfb_setcolreg,
  273. .fb_blank = hitfb_blank,
  274. .fb_sync = hitfb_sync,
  275. .fb_pan_display = hitfb_pan_display,
  276. .fb_fillrect = hitfb_fillrect,
  277. .fb_copyarea = hitfb_copyarea,
  278. .fb_imageblit = cfb_imageblit,
  279. };
  280. static int hitfb_probe(struct platform_device *dev)
  281. {
  282. unsigned short lcdclor, ldr3, ldvndr;
  283. struct fb_info *info;
  284. int ret;
  285. if (fb_get_options("hitfb", NULL))
  286. return -ENODEV;
  287. hitfb_fix.mmio_start = HD64461_IO_OFFSET(0x1000);
  288. hitfb_fix.mmio_len = 0x1000;
  289. hitfb_fix.smem_start = HD64461_IO_OFFSET(0x02000000);
  290. hitfb_fix.smem_len = 512 * 1024;
  291. lcdclor = fb_readw(HD64461_LCDCLOR);
  292. ldvndr = fb_readw(HD64461_LDVNDR);
  293. ldr3 = fb_readw(HD64461_LDR3);
  294. switch (ldr3 & 15) {
  295. default:
  296. case 4:
  297. hitfb_var.bits_per_pixel = 8;
  298. hitfb_var.xres = lcdclor;
  299. break;
  300. case 8:
  301. hitfb_var.bits_per_pixel = 16;
  302. hitfb_var.xres = lcdclor / 2;
  303. break;
  304. }
  305. hitfb_fix.line_length = lcdclor;
  306. hitfb_fix.visual = (hitfb_var.bits_per_pixel == 8) ?
  307. FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
  308. hitfb_var.yres = ldvndr + 1;
  309. hitfb_var.xres_virtual = hitfb_var.xres;
  310. hitfb_var.yres_virtual = hitfb_fix.smem_len / lcdclor;
  311. switch (hitfb_var.bits_per_pixel) {
  312. case 8:
  313. hitfb_var.red.offset = 0;
  314. hitfb_var.red.length = 8;
  315. hitfb_var.green.offset = 0;
  316. hitfb_var.green.length = 8;
  317. hitfb_var.blue.offset = 0;
  318. hitfb_var.blue.length = 8;
  319. hitfb_var.transp.offset = 0;
  320. hitfb_var.transp.length = 0;
  321. break;
  322. case 16: /* RGB 565 */
  323. hitfb_var.red.offset = 11;
  324. hitfb_var.red.length = 5;
  325. hitfb_var.green.offset = 5;
  326. hitfb_var.green.length = 6;
  327. hitfb_var.blue.offset = 0;
  328. hitfb_var.blue.length = 5;
  329. hitfb_var.transp.offset = 0;
  330. hitfb_var.transp.length = 0;
  331. break;
  332. }
  333. info = framebuffer_alloc(sizeof(u32) * 16, &dev->dev);
  334. if (unlikely(!info))
  335. return -ENOMEM;
  336. info->fbops = &hitfb_ops;
  337. info->var = hitfb_var;
  338. info->fix = hitfb_fix;
  339. info->pseudo_palette = info->par;
  340. info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN |
  341. FBINFO_HWACCEL_FILLRECT | FBINFO_HWACCEL_COPYAREA;
  342. info->screen_base = (void *)hitfb_fix.smem_start;
  343. ret = fb_alloc_cmap(&info->cmap, 256, 0);
  344. if (unlikely(ret < 0))
  345. goto err_fb;
  346. ret = register_framebuffer(info);
  347. if (unlikely(ret < 0))
  348. goto err;
  349. platform_set_drvdata(dev, info);
  350. fb_info(info, "%s frame buffer device\n", info->fix.id);
  351. return 0;
  352. err:
  353. fb_dealloc_cmap(&info->cmap);
  354. err_fb:
  355. framebuffer_release(info);
  356. return ret;
  357. }
  358. static int hitfb_remove(struct platform_device *dev)
  359. {
  360. struct fb_info *info = platform_get_drvdata(dev);
  361. unregister_framebuffer(info);
  362. fb_dealloc_cmap(&info->cmap);
  363. framebuffer_release(info);
  364. return 0;
  365. }
  366. static int hitfb_suspend(struct device *dev)
  367. {
  368. u16 v;
  369. hitfb_blank(1,0);
  370. v = fb_readw(HD64461_STBCR);
  371. v |= HD64461_STBCR_SLCKE_IST;
  372. fb_writew(v, HD64461_STBCR);
  373. return 0;
  374. }
  375. static int hitfb_resume(struct device *dev)
  376. {
  377. u16 v;
  378. v = fb_readw(HD64461_STBCR);
  379. v &= ~HD64461_STBCR_SLCKE_OST;
  380. msleep(100);
  381. v = fb_readw(HD64461_STBCR);
  382. v &= ~HD64461_STBCR_SLCKE_IST;
  383. fb_writew(v, HD64461_STBCR);
  384. hitfb_blank(0,0);
  385. return 0;
  386. }
  387. static const struct dev_pm_ops hitfb_dev_pm_ops = {
  388. .suspend = hitfb_suspend,
  389. .resume = hitfb_resume,
  390. };
  391. static struct platform_driver hitfb_driver = {
  392. .probe = hitfb_probe,
  393. .remove = hitfb_remove,
  394. .driver = {
  395. .name = "hitfb",
  396. .pm = &hitfb_dev_pm_ops,
  397. },
  398. };
  399. static struct platform_device hitfb_device = {
  400. .name = "hitfb",
  401. .id = -1,
  402. };
  403. static int __init hitfb_init(void)
  404. {
  405. int ret;
  406. ret = platform_driver_register(&hitfb_driver);
  407. if (!ret) {
  408. ret = platform_device_register(&hitfb_device);
  409. if (ret)
  410. platform_driver_unregister(&hitfb_driver);
  411. }
  412. return ret;
  413. }
  414. static void __exit hitfb_exit(void)
  415. {
  416. platform_device_unregister(&hitfb_device);
  417. platform_driver_unregister(&hitfb_driver);
  418. }
  419. module_init(hitfb_init);
  420. module_exit(hitfb_exit);
  421. MODULE_LICENSE("GPL");