sm501fb.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936
  1. /* linux/drivers/video/sm501fb.c
  2. *
  3. * Copyright (c) 2006 Simtec Electronics
  4. * Vincent Sanders <vince@simtec.co.uk>
  5. * Ben Dooks <ben@simtec.co.uk>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * Framebuffer driver for the Silicon Motion SM501
  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/tty.h>
  19. #include <linux/slab.h>
  20. #include <linux/delay.h>
  21. #include <linux/fb.h>
  22. #include <linux/init.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/workqueue.h>
  27. #include <linux/wait.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/clk.h>
  30. #include <linux/console.h>
  31. #include <asm/io.h>
  32. #include <asm/uaccess.h>
  33. #include <asm/div64.h>
  34. #ifdef CONFIG_PM
  35. #include <linux/pm.h>
  36. #endif
  37. #include <linux/sm501.h>
  38. #include <linux/sm501-regs.h>
  39. #define NR_PALETTE 256
  40. enum sm501_controller {
  41. HEAD_CRT = 0,
  42. HEAD_PANEL = 1,
  43. };
  44. /* SM501 memory address.
  45. *
  46. * This structure is used to track memory usage within the SM501 framebuffer
  47. * allocation. The sm_addr field is stored as an offset as it is often used
  48. * against both the physical and mapped addresses.
  49. */
  50. struct sm501_mem {
  51. unsigned long size;
  52. unsigned long sm_addr; /* offset from base of sm501 fb. */
  53. void __iomem *k_addr;
  54. };
  55. /* private data that is shared between all frambuffers* */
  56. struct sm501fb_info {
  57. struct device *dev;
  58. struct fb_info *fb[2]; /* fb info for both heads */
  59. struct resource *fbmem_res; /* framebuffer resource */
  60. struct resource *regs_res; /* registers resource */
  61. struct sm501_platdata_fb *pdata; /* our platform data */
  62. unsigned long pm_crt_ctrl; /* pm: crt ctrl save */
  63. int irq;
  64. int swap_endian; /* set to swap rgb=>bgr */
  65. void __iomem *regs; /* remapped registers */
  66. void __iomem *fbmem; /* remapped framebuffer */
  67. size_t fbmem_len; /* length of remapped region */
  68. };
  69. /* per-framebuffer private data */
  70. struct sm501fb_par {
  71. u32 pseudo_palette[16];
  72. enum sm501_controller head;
  73. struct sm501_mem cursor;
  74. struct sm501_mem screen;
  75. struct fb_ops ops;
  76. void *store_fb;
  77. void *store_cursor;
  78. void __iomem *cursor_regs;
  79. struct sm501fb_info *info;
  80. };
  81. /* Helper functions */
  82. static inline int h_total(struct fb_var_screeninfo *var)
  83. {
  84. return var->xres + var->left_margin +
  85. var->right_margin + var->hsync_len;
  86. }
  87. static inline int v_total(struct fb_var_screeninfo *var)
  88. {
  89. return var->yres + var->upper_margin +
  90. var->lower_margin + var->vsync_len;
  91. }
  92. /* sm501fb_sync_regs()
  93. *
  94. * This call is mainly for PCI bus systems where we need to
  95. * ensure that any writes to the bus are completed before the
  96. * next phase, or after completing a function.
  97. */
  98. static inline void sm501fb_sync_regs(struct sm501fb_info *info)
  99. {
  100. readl(info->regs);
  101. }
  102. /* sm501_alloc_mem
  103. *
  104. * This is an attempt to lay out memory for the two framebuffers and
  105. * everything else
  106. *
  107. * |fbmem_res->start fbmem_res->end|
  108. * | |
  109. * |fb[0].fix.smem_start | |fb[1].fix.smem_start | 2K |
  110. * |-> fb[0].fix.smem_len <-| spare |-> fb[1].fix.smem_len <-|-> cursors <-|
  111. *
  112. * The "spare" space is for the 2d engine data
  113. * the fixed is space for the cursors (2x1Kbyte)
  114. *
  115. * we need to allocate memory for the 2D acceleration engine
  116. * command list and the data for the engine to deal with.
  117. *
  118. * - all allocations must be 128bit aligned
  119. * - cursors are 64x64x2 bits (1Kbyte)
  120. *
  121. */
  122. #define SM501_MEMF_CURSOR (1)
  123. #define SM501_MEMF_PANEL (2)
  124. #define SM501_MEMF_CRT (4)
  125. #define SM501_MEMF_ACCEL (8)
  126. static int sm501_alloc_mem(struct sm501fb_info *inf, struct sm501_mem *mem,
  127. unsigned int why, size_t size)
  128. {
  129. struct sm501fb_par *par;
  130. struct fb_info *fbi;
  131. unsigned int ptr;
  132. unsigned int end;
  133. switch (why) {
  134. case SM501_MEMF_CURSOR:
  135. ptr = inf->fbmem_len - size;
  136. inf->fbmem_len = ptr; /* adjust available memory. */
  137. break;
  138. case SM501_MEMF_PANEL:
  139. ptr = inf->fbmem_len - size;
  140. fbi = inf->fb[HEAD_CRT];
  141. /* round down, some programs such as directfb do not draw
  142. * 0,0 correctly unless the start is aligned to a page start.
  143. */
  144. if (ptr > 0)
  145. ptr &= ~(PAGE_SIZE - 1);
  146. if (fbi && ptr < fbi->fix.smem_len)
  147. return -ENOMEM;
  148. if (ptr < 0)
  149. return -ENOMEM;
  150. break;
  151. case SM501_MEMF_CRT:
  152. ptr = 0;
  153. /* check to see if we have panel memory allocated
  154. * which would put an limit on available memory. */
  155. fbi = inf->fb[HEAD_PANEL];
  156. if (fbi) {
  157. par = fbi->par;
  158. end = par->screen.k_addr ? par->screen.sm_addr : inf->fbmem_len;
  159. } else
  160. end = inf->fbmem_len;
  161. if ((ptr + size) > end)
  162. return -ENOMEM;
  163. break;
  164. case SM501_MEMF_ACCEL:
  165. fbi = inf->fb[HEAD_CRT];
  166. ptr = fbi ? fbi->fix.smem_len : 0;
  167. fbi = inf->fb[HEAD_PANEL];
  168. if (fbi) {
  169. par = fbi->par;
  170. end = par->screen.sm_addr;
  171. } else
  172. end = inf->fbmem_len;
  173. if ((ptr + size) > end)
  174. return -ENOMEM;
  175. break;
  176. default:
  177. return -EINVAL;
  178. }
  179. mem->size = size;
  180. mem->sm_addr = ptr;
  181. mem->k_addr = inf->fbmem + ptr;
  182. dev_dbg(inf->dev, "%s: result %08lx, %p - %u, %zd\n",
  183. __func__, mem->sm_addr, mem->k_addr, why, size);
  184. return 0;
  185. }
  186. /* sm501fb_ps_to_hz
  187. *
  188. * Converts a period in picoseconds to Hz.
  189. *
  190. * Note, we try to keep this in Hz to minimise rounding with
  191. * the limited PLL settings on the SM501.
  192. */
  193. static unsigned long sm501fb_ps_to_hz(unsigned long psvalue)
  194. {
  195. unsigned long long numerator=1000000000000ULL;
  196. /* 10^12 / picosecond period gives frequency in Hz */
  197. do_div(numerator, psvalue);
  198. return (unsigned long)numerator;
  199. }
  200. /* sm501fb_hz_to_ps is identical to the oposite transform */
  201. #define sm501fb_hz_to_ps(x) sm501fb_ps_to_hz(x)
  202. /* sm501fb_setup_gamma
  203. *
  204. * Programs a linear 1.0 gamma ramp in case the gamma
  205. * correction is enabled without programming anything else.
  206. */
  207. static void sm501fb_setup_gamma(struct sm501fb_info *fbi,
  208. unsigned long palette)
  209. {
  210. unsigned long value = 0;
  211. int offset;
  212. /* set gamma values */
  213. for (offset = 0; offset < 256 * 4; offset += 4) {
  214. writel(value, fbi->regs + palette + offset);
  215. value += 0x010101; /* Advance RGB by 1,1,1.*/
  216. }
  217. }
  218. /* sm501fb_check_var
  219. *
  220. * check common variables for both panel and crt
  221. */
  222. static int sm501fb_check_var(struct fb_var_screeninfo *var,
  223. struct fb_info *info)
  224. {
  225. struct sm501fb_par *par = info->par;
  226. struct sm501fb_info *sm = par->info;
  227. unsigned long tmp;
  228. /* check we can fit these values into the registers */
  229. if (var->hsync_len > 255 || var->vsync_len > 63)
  230. return -EINVAL;
  231. /* hdisplay end and hsync start */
  232. if ((var->xres + var->right_margin) > 4096)
  233. return -EINVAL;
  234. /* vdisplay end and vsync start */
  235. if ((var->yres + var->lower_margin) > 2048)
  236. return -EINVAL;
  237. /* hard limits of device */
  238. if (h_total(var) > 4096 || v_total(var) > 2048)
  239. return -EINVAL;
  240. /* check our line length is going to be 128 bit aligned */
  241. tmp = (var->xres * var->bits_per_pixel) / 8;
  242. if ((tmp & 15) != 0)
  243. return -EINVAL;
  244. /* check the virtual size */
  245. if (var->xres_virtual > 4096 || var->yres_virtual > 2048)
  246. return -EINVAL;
  247. /* can cope with 8,16 or 32bpp */
  248. if (var->bits_per_pixel <= 8)
  249. var->bits_per_pixel = 8;
  250. else if (var->bits_per_pixel <= 16)
  251. var->bits_per_pixel = 16;
  252. else if (var->bits_per_pixel == 24)
  253. var->bits_per_pixel = 32;
  254. /* set r/g/b positions and validate bpp */
  255. switch(var->bits_per_pixel) {
  256. case 8:
  257. var->red.length = var->bits_per_pixel;
  258. var->red.offset = 0;
  259. var->green.length = var->bits_per_pixel;
  260. var->green.offset = 0;
  261. var->blue.length = var->bits_per_pixel;
  262. var->blue.offset = 0;
  263. var->transp.length = 0;
  264. var->transp.offset = 0;
  265. break;
  266. case 16:
  267. if (sm->pdata->flags & SM501_FBPD_SWAP_FB_ENDIAN) {
  268. var->blue.offset = 11;
  269. var->green.offset = 5;
  270. var->red.offset = 0;
  271. } else {
  272. var->red.offset = 11;
  273. var->green.offset = 5;
  274. var->blue.offset = 0;
  275. }
  276. var->transp.offset = 0;
  277. var->red.length = 5;
  278. var->green.length = 6;
  279. var->blue.length = 5;
  280. var->transp.length = 0;
  281. break;
  282. case 32:
  283. if (sm->pdata->flags & SM501_FBPD_SWAP_FB_ENDIAN) {
  284. var->transp.offset = 0;
  285. var->red.offset = 8;
  286. var->green.offset = 16;
  287. var->blue.offset = 24;
  288. } else {
  289. var->transp.offset = 24;
  290. var->red.offset = 16;
  291. var->green.offset = 8;
  292. var->blue.offset = 0;
  293. }
  294. var->red.length = 8;
  295. var->green.length = 8;
  296. var->blue.length = 8;
  297. var->transp.length = 0;
  298. break;
  299. default:
  300. return -EINVAL;
  301. }
  302. return 0;
  303. }
  304. /*
  305. * sm501fb_check_var_crt():
  306. *
  307. * check the parameters for the CRT head, and either bring them
  308. * back into range, or return -EINVAL.
  309. */
  310. static int sm501fb_check_var_crt(struct fb_var_screeninfo *var,
  311. struct fb_info *info)
  312. {
  313. return sm501fb_check_var(var, info);
  314. }
  315. /* sm501fb_check_var_pnl():
  316. *
  317. * check the parameters for the CRT head, and either bring them
  318. * back into range, or return -EINVAL.
  319. */
  320. static int sm501fb_check_var_pnl(struct fb_var_screeninfo *var,
  321. struct fb_info *info)
  322. {
  323. return sm501fb_check_var(var, info);
  324. }
  325. /* sm501fb_set_par_common
  326. *
  327. * set common registers for framebuffers
  328. */
  329. static int sm501fb_set_par_common(struct fb_info *info,
  330. struct fb_var_screeninfo *var)
  331. {
  332. struct sm501fb_par *par = info->par;
  333. struct sm501fb_info *fbi = par->info;
  334. unsigned long pixclock; /* pixelclock in Hz */
  335. unsigned long sm501pixclock; /* pixelclock the 501 can achive in Hz */
  336. unsigned int mem_type;
  337. unsigned int clock_type;
  338. unsigned int head_addr;
  339. dev_dbg(fbi->dev, "%s: %dx%d, bpp = %d, virtual %dx%d\n",
  340. __func__, var->xres, var->yres, var->bits_per_pixel,
  341. var->xres_virtual, var->yres_virtual);
  342. switch (par->head) {
  343. case HEAD_CRT:
  344. mem_type = SM501_MEMF_CRT;
  345. clock_type = SM501_CLOCK_V2XCLK;
  346. head_addr = SM501_DC_CRT_FB_ADDR;
  347. break;
  348. case HEAD_PANEL:
  349. mem_type = SM501_MEMF_PANEL;
  350. clock_type = SM501_CLOCK_P2XCLK;
  351. head_addr = SM501_DC_PANEL_FB_ADDR;
  352. break;
  353. default:
  354. mem_type = 0; /* stop compiler warnings */
  355. head_addr = 0;
  356. clock_type = 0;
  357. }
  358. switch (var->bits_per_pixel) {
  359. case 8:
  360. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  361. break;
  362. case 16:
  363. info->fix.visual = FB_VISUAL_TRUECOLOR;
  364. break;
  365. case 32:
  366. info->fix.visual = FB_VISUAL_TRUECOLOR;
  367. break;
  368. }
  369. /* allocate fb memory within 501 */
  370. info->fix.line_length = (var->xres_virtual * var->bits_per_pixel)/8;
  371. info->fix.smem_len = info->fix.line_length * var->yres_virtual;
  372. dev_dbg(fbi->dev, "%s: line length = %u\n", __func__,
  373. info->fix.line_length);
  374. if (sm501_alloc_mem(fbi, &par->screen, mem_type,
  375. info->fix.smem_len)) {
  376. dev_err(fbi->dev, "no memory available\n");
  377. return -ENOMEM;
  378. }
  379. info->fix.smem_start = fbi->fbmem_res->start + par->screen.sm_addr;
  380. info->screen_base = fbi->fbmem + par->screen.sm_addr;
  381. info->screen_size = info->fix.smem_len;
  382. /* set start of framebuffer to the screen */
  383. writel(par->screen.sm_addr | SM501_ADDR_FLIP, fbi->regs + head_addr);
  384. /* program CRT clock */
  385. pixclock = sm501fb_ps_to_hz(var->pixclock);
  386. sm501pixclock = sm501_set_clock(fbi->dev->parent, clock_type,
  387. pixclock);
  388. /* update fb layer with actual clock used */
  389. var->pixclock = sm501fb_hz_to_ps(sm501pixclock);
  390. dev_dbg(fbi->dev, "%s: pixclock(ps) = %u, pixclock(Hz) = %lu, "
  391. "sm501pixclock = %lu, error = %ld%%\n",
  392. __func__, var->pixclock, pixclock, sm501pixclock,
  393. ((pixclock - sm501pixclock)*100)/pixclock);
  394. return 0;
  395. }
  396. /* sm501fb_set_par_geometry
  397. *
  398. * set the geometry registers for specified framebuffer.
  399. */
  400. static void sm501fb_set_par_geometry(struct fb_info *info,
  401. struct fb_var_screeninfo *var)
  402. {
  403. struct sm501fb_par *par = info->par;
  404. struct sm501fb_info *fbi = par->info;
  405. void __iomem *base = fbi->regs;
  406. unsigned long reg;
  407. if (par->head == HEAD_CRT)
  408. base += SM501_DC_CRT_H_TOT;
  409. else
  410. base += SM501_DC_PANEL_H_TOT;
  411. /* set framebuffer width and display width */
  412. reg = info->fix.line_length;
  413. reg |= ((var->xres * var->bits_per_pixel)/8) << 16;
  414. writel(reg, fbi->regs + (par->head == HEAD_CRT ?
  415. SM501_DC_CRT_FB_OFFSET : SM501_DC_PANEL_FB_OFFSET));
  416. /* program horizontal total */
  417. reg = (h_total(var) - 1) << 16;
  418. reg |= (var->xres - 1);
  419. writel(reg, base + SM501_OFF_DC_H_TOT);
  420. /* program horizontal sync */
  421. reg = var->hsync_len << 16;
  422. reg |= var->xres + var->right_margin - 1;
  423. writel(reg, base + SM501_OFF_DC_H_SYNC);
  424. /* program vertical total */
  425. reg = (v_total(var) - 1) << 16;
  426. reg |= (var->yres - 1);
  427. writel(reg, base + SM501_OFF_DC_V_TOT);
  428. /* program vertical sync */
  429. reg = var->vsync_len << 16;
  430. reg |= var->yres + var->lower_margin - 1;
  431. writel(reg, base + SM501_OFF_DC_V_SYNC);
  432. }
  433. /* sm501fb_pan_crt
  434. *
  435. * pan the CRT display output within an virtual framebuffer
  436. */
  437. static int sm501fb_pan_crt(struct fb_var_screeninfo *var,
  438. struct fb_info *info)
  439. {
  440. struct sm501fb_par *par = info->par;
  441. struct sm501fb_info *fbi = par->info;
  442. unsigned int bytes_pixel = var->bits_per_pixel / 8;
  443. unsigned long reg;
  444. unsigned long xoffs;
  445. xoffs = var->xoffset * bytes_pixel;
  446. reg = readl(fbi->regs + SM501_DC_CRT_CONTROL);
  447. reg &= ~SM501_DC_CRT_CONTROL_PIXEL_MASK;
  448. reg |= ((xoffs & 15) / bytes_pixel) << 4;
  449. writel(reg, fbi->regs + SM501_DC_CRT_CONTROL);
  450. reg = (par->screen.sm_addr + xoffs +
  451. var->yoffset * info->fix.line_length);
  452. writel(reg | SM501_ADDR_FLIP, fbi->regs + SM501_DC_CRT_FB_ADDR);
  453. sm501fb_sync_regs(fbi);
  454. return 0;
  455. }
  456. /* sm501fb_pan_pnl
  457. *
  458. * pan the panel display output within an virtual framebuffer
  459. */
  460. static int sm501fb_pan_pnl(struct fb_var_screeninfo *var,
  461. struct fb_info *info)
  462. {
  463. struct sm501fb_par *par = info->par;
  464. struct sm501fb_info *fbi = par->info;
  465. unsigned long reg;
  466. reg = var->xoffset | (var->xres_virtual << 16);
  467. writel(reg, fbi->regs + SM501_DC_PANEL_FB_WIDTH);
  468. reg = var->yoffset | (var->yres_virtual << 16);
  469. writel(reg, fbi->regs + SM501_DC_PANEL_FB_HEIGHT);
  470. sm501fb_sync_regs(fbi);
  471. return 0;
  472. }
  473. /* sm501fb_set_par_crt
  474. *
  475. * Set the CRT video mode from the fb_info structure
  476. */
  477. static int sm501fb_set_par_crt(struct fb_info *info)
  478. {
  479. struct sm501fb_par *par = info->par;
  480. struct sm501fb_info *fbi = par->info;
  481. struct fb_var_screeninfo *var = &info->var;
  482. unsigned long control; /* control register */
  483. int ret;
  484. /* activate new configuration */
  485. dev_dbg(fbi->dev, "%s(%p)\n", __func__, info);
  486. /* enable CRT DAC - note 0 is on!*/
  487. sm501_misc_control(fbi->dev->parent, 0, SM501_MISC_DAC_POWER);
  488. control = readl(fbi->regs + SM501_DC_CRT_CONTROL);
  489. control &= (SM501_DC_CRT_CONTROL_PIXEL_MASK |
  490. SM501_DC_CRT_CONTROL_GAMMA |
  491. SM501_DC_CRT_CONTROL_BLANK |
  492. SM501_DC_CRT_CONTROL_SEL |
  493. SM501_DC_CRT_CONTROL_CP |
  494. SM501_DC_CRT_CONTROL_TVP);
  495. /* set the sync polarities before we check data source */
  496. if ((var->sync & FB_SYNC_HOR_HIGH_ACT) == 0)
  497. control |= SM501_DC_CRT_CONTROL_HSP;
  498. if ((var->sync & FB_SYNC_VERT_HIGH_ACT) == 0)
  499. control |= SM501_DC_CRT_CONTROL_VSP;
  500. if ((control & SM501_DC_CRT_CONTROL_SEL) == 0) {
  501. /* the head is displaying panel data... */
  502. sm501_alloc_mem(fbi, &par->screen, SM501_MEMF_CRT, 0);
  503. goto out_update;
  504. }
  505. ret = sm501fb_set_par_common(info, var);
  506. if (ret) {
  507. dev_err(fbi->dev, "failed to set common parameters\n");
  508. return ret;
  509. }
  510. sm501fb_pan_crt(var, info);
  511. sm501fb_set_par_geometry(info, var);
  512. control |= SM501_FIFO_3; /* fill if >3 free slots */
  513. switch(var->bits_per_pixel) {
  514. case 8:
  515. control |= SM501_DC_CRT_CONTROL_8BPP;
  516. break;
  517. case 16:
  518. control |= SM501_DC_CRT_CONTROL_16BPP;
  519. sm501fb_setup_gamma(fbi, SM501_DC_CRT_PALETTE);
  520. break;
  521. case 32:
  522. control |= SM501_DC_CRT_CONTROL_32BPP;
  523. sm501fb_setup_gamma(fbi, SM501_DC_CRT_PALETTE);
  524. break;
  525. default:
  526. BUG();
  527. }
  528. control |= SM501_DC_CRT_CONTROL_SEL; /* CRT displays CRT data */
  529. control |= SM501_DC_CRT_CONTROL_TE; /* enable CRT timing */
  530. control |= SM501_DC_CRT_CONTROL_ENABLE; /* enable CRT plane */
  531. out_update:
  532. dev_dbg(fbi->dev, "new control is %08lx\n", control);
  533. writel(control, fbi->regs + SM501_DC_CRT_CONTROL);
  534. sm501fb_sync_regs(fbi);
  535. return 0;
  536. }
  537. static void sm501fb_panel_power(struct sm501fb_info *fbi, int to)
  538. {
  539. unsigned long control;
  540. void __iomem *ctrl_reg = fbi->regs + SM501_DC_PANEL_CONTROL;
  541. struct sm501_platdata_fbsub *pd = fbi->pdata->fb_pnl;
  542. control = readl(ctrl_reg);
  543. if (to && (control & SM501_DC_PANEL_CONTROL_VDD) == 0) {
  544. /* enable panel power */
  545. control |= SM501_DC_PANEL_CONTROL_VDD; /* FPVDDEN */
  546. writel(control, ctrl_reg);
  547. sm501fb_sync_regs(fbi);
  548. mdelay(10);
  549. control |= SM501_DC_PANEL_CONTROL_DATA; /* DATA */
  550. writel(control, ctrl_reg);
  551. sm501fb_sync_regs(fbi);
  552. mdelay(10);
  553. /* VBIASEN */
  554. if (!(pd->flags & SM501FB_FLAG_PANEL_NO_VBIASEN)) {
  555. if (pd->flags & SM501FB_FLAG_PANEL_INV_VBIASEN)
  556. control &= ~SM501_DC_PANEL_CONTROL_BIAS;
  557. else
  558. control |= SM501_DC_PANEL_CONTROL_BIAS;
  559. writel(control, ctrl_reg);
  560. sm501fb_sync_regs(fbi);
  561. mdelay(10);
  562. }
  563. if (!(pd->flags & SM501FB_FLAG_PANEL_NO_FPEN)) {
  564. if (pd->flags & SM501FB_FLAG_PANEL_INV_FPEN)
  565. control &= ~SM501_DC_PANEL_CONTROL_FPEN;
  566. else
  567. control |= SM501_DC_PANEL_CONTROL_FPEN;
  568. writel(control, ctrl_reg);
  569. sm501fb_sync_regs(fbi);
  570. mdelay(10);
  571. }
  572. } else if (!to && (control & SM501_DC_PANEL_CONTROL_VDD) != 0) {
  573. /* disable panel power */
  574. if (!(pd->flags & SM501FB_FLAG_PANEL_NO_FPEN)) {
  575. if (pd->flags & SM501FB_FLAG_PANEL_INV_FPEN)
  576. control |= SM501_DC_PANEL_CONTROL_FPEN;
  577. else
  578. control &= ~SM501_DC_PANEL_CONTROL_FPEN;
  579. writel(control, ctrl_reg);
  580. sm501fb_sync_regs(fbi);
  581. mdelay(10);
  582. }
  583. if (!(pd->flags & SM501FB_FLAG_PANEL_NO_VBIASEN)) {
  584. if (pd->flags & SM501FB_FLAG_PANEL_INV_VBIASEN)
  585. control |= SM501_DC_PANEL_CONTROL_BIAS;
  586. else
  587. control &= ~SM501_DC_PANEL_CONTROL_BIAS;
  588. writel(control, ctrl_reg);
  589. sm501fb_sync_regs(fbi);
  590. mdelay(10);
  591. }
  592. control &= ~SM501_DC_PANEL_CONTROL_DATA;
  593. writel(control, ctrl_reg);
  594. sm501fb_sync_regs(fbi);
  595. mdelay(10);
  596. control &= ~SM501_DC_PANEL_CONTROL_VDD;
  597. writel(control, ctrl_reg);
  598. sm501fb_sync_regs(fbi);
  599. mdelay(10);
  600. }
  601. sm501fb_sync_regs(fbi);
  602. }
  603. /* sm501fb_set_par_pnl
  604. *
  605. * Set the panel video mode from the fb_info structure
  606. */
  607. static int sm501fb_set_par_pnl(struct fb_info *info)
  608. {
  609. struct sm501fb_par *par = info->par;
  610. struct sm501fb_info *fbi = par->info;
  611. struct fb_var_screeninfo *var = &info->var;
  612. unsigned long control;
  613. unsigned long reg;
  614. int ret;
  615. dev_dbg(fbi->dev, "%s(%p)\n", __func__, info);
  616. /* activate this new configuration */
  617. ret = sm501fb_set_par_common(info, var);
  618. if (ret)
  619. return ret;
  620. sm501fb_pan_pnl(var, info);
  621. sm501fb_set_par_geometry(info, var);
  622. /* update control register */
  623. control = readl(fbi->regs + SM501_DC_PANEL_CONTROL);
  624. control &= (SM501_DC_PANEL_CONTROL_GAMMA |
  625. SM501_DC_PANEL_CONTROL_VDD |
  626. SM501_DC_PANEL_CONTROL_DATA |
  627. SM501_DC_PANEL_CONTROL_BIAS |
  628. SM501_DC_PANEL_CONTROL_FPEN |
  629. SM501_DC_PANEL_CONTROL_CP |
  630. SM501_DC_PANEL_CONTROL_CK |
  631. SM501_DC_PANEL_CONTROL_HP |
  632. SM501_DC_PANEL_CONTROL_VP |
  633. SM501_DC_PANEL_CONTROL_HPD |
  634. SM501_DC_PANEL_CONTROL_VPD);
  635. control |= SM501_FIFO_3; /* fill if >3 free slots */
  636. switch(var->bits_per_pixel) {
  637. case 8:
  638. control |= SM501_DC_PANEL_CONTROL_8BPP;
  639. break;
  640. case 16:
  641. control |= SM501_DC_PANEL_CONTROL_16BPP;
  642. sm501fb_setup_gamma(fbi, SM501_DC_PANEL_PALETTE);
  643. break;
  644. case 32:
  645. control |= SM501_DC_PANEL_CONTROL_32BPP;
  646. sm501fb_setup_gamma(fbi, SM501_DC_PANEL_PALETTE);
  647. break;
  648. default:
  649. BUG();
  650. }
  651. writel(0x0, fbi->regs + SM501_DC_PANEL_PANNING_CONTROL);
  652. /* panel plane top left and bottom right location */
  653. writel(0x00, fbi->regs + SM501_DC_PANEL_TL_LOC);
  654. reg = var->xres - 1;
  655. reg |= (var->yres - 1) << 16;
  656. writel(reg, fbi->regs + SM501_DC_PANEL_BR_LOC);
  657. /* program panel control register */
  658. control |= SM501_DC_PANEL_CONTROL_TE; /* enable PANEL timing */
  659. control |= SM501_DC_PANEL_CONTROL_EN; /* enable PANEL gfx plane */
  660. if ((var->sync & FB_SYNC_HOR_HIGH_ACT) == 0)
  661. control |= SM501_DC_PANEL_CONTROL_HSP;
  662. if ((var->sync & FB_SYNC_VERT_HIGH_ACT) == 0)
  663. control |= SM501_DC_PANEL_CONTROL_VSP;
  664. writel(control, fbi->regs + SM501_DC_PANEL_CONTROL);
  665. sm501fb_sync_regs(fbi);
  666. /* ensure the panel interface is not tristated at this point */
  667. sm501_modify_reg(fbi->dev->parent, SM501_SYSTEM_CONTROL,
  668. 0, SM501_SYSCTRL_PANEL_TRISTATE);
  669. /* power the panel up */
  670. sm501fb_panel_power(fbi, 1);
  671. return 0;
  672. }
  673. /* chan_to_field
  674. *
  675. * convert a colour value into a field position
  676. *
  677. * from pxafb.c
  678. */
  679. static inline unsigned int chan_to_field(unsigned int chan,
  680. struct fb_bitfield *bf)
  681. {
  682. chan &= 0xffff;
  683. chan >>= 16 - bf->length;
  684. return chan << bf->offset;
  685. }
  686. /* sm501fb_setcolreg
  687. *
  688. * set the colour mapping for modes that support palettised data
  689. */
  690. static int sm501fb_setcolreg(unsigned regno,
  691. unsigned red, unsigned green, unsigned blue,
  692. unsigned transp, struct fb_info *info)
  693. {
  694. struct sm501fb_par *par = info->par;
  695. struct sm501fb_info *fbi = par->info;
  696. void __iomem *base = fbi->regs;
  697. unsigned int val;
  698. if (par->head == HEAD_CRT)
  699. base += SM501_DC_CRT_PALETTE;
  700. else
  701. base += SM501_DC_PANEL_PALETTE;
  702. switch (info->fix.visual) {
  703. case FB_VISUAL_TRUECOLOR:
  704. /* true-colour, use pseuo-palette */
  705. if (regno < 16) {
  706. u32 *pal = par->pseudo_palette;
  707. val = chan_to_field(red, &info->var.red);
  708. val |= chan_to_field(green, &info->var.green);
  709. val |= chan_to_field(blue, &info->var.blue);
  710. pal[regno] = val;
  711. }
  712. break;
  713. case FB_VISUAL_PSEUDOCOLOR:
  714. if (regno < 256) {
  715. val = (red >> 8) << 16;
  716. val |= (green >> 8) << 8;
  717. val |= blue >> 8;
  718. writel(val, base + (regno * 4));
  719. }
  720. break;
  721. default:
  722. return 1; /* unknown type */
  723. }
  724. return 0;
  725. }
  726. /* sm501fb_blank_pnl
  727. *
  728. * Blank or un-blank the panel interface
  729. */
  730. static int sm501fb_blank_pnl(int blank_mode, struct fb_info *info)
  731. {
  732. struct sm501fb_par *par = info->par;
  733. struct sm501fb_info *fbi = par->info;
  734. dev_dbg(fbi->dev, "%s(mode=%d, %p)\n", __func__, blank_mode, info);
  735. switch (blank_mode) {
  736. case FB_BLANK_POWERDOWN:
  737. sm501fb_panel_power(fbi, 0);
  738. break;
  739. case FB_BLANK_UNBLANK:
  740. sm501fb_panel_power(fbi, 1);
  741. break;
  742. case FB_BLANK_NORMAL:
  743. case FB_BLANK_VSYNC_SUSPEND:
  744. case FB_BLANK_HSYNC_SUSPEND:
  745. default:
  746. return 1;
  747. }
  748. return 0;
  749. }
  750. /* sm501fb_blank_crt
  751. *
  752. * Blank or un-blank the crt interface
  753. */
  754. static int sm501fb_blank_crt(int blank_mode, struct fb_info *info)
  755. {
  756. struct sm501fb_par *par = info->par;
  757. struct sm501fb_info *fbi = par->info;
  758. unsigned long ctrl;
  759. dev_dbg(fbi->dev, "%s(mode=%d, %p)\n", __func__, blank_mode, info);
  760. ctrl = readl(fbi->regs + SM501_DC_CRT_CONTROL);
  761. switch (blank_mode) {
  762. case FB_BLANK_POWERDOWN:
  763. ctrl &= ~SM501_DC_CRT_CONTROL_ENABLE;
  764. sm501_misc_control(fbi->dev->parent, SM501_MISC_DAC_POWER, 0);
  765. case FB_BLANK_NORMAL:
  766. ctrl |= SM501_DC_CRT_CONTROL_BLANK;
  767. break;
  768. case FB_BLANK_UNBLANK:
  769. ctrl &= ~SM501_DC_CRT_CONTROL_BLANK;
  770. ctrl |= SM501_DC_CRT_CONTROL_ENABLE;
  771. sm501_misc_control(fbi->dev->parent, 0, SM501_MISC_DAC_POWER);
  772. break;
  773. case FB_BLANK_VSYNC_SUSPEND:
  774. case FB_BLANK_HSYNC_SUSPEND:
  775. default:
  776. return 1;
  777. }
  778. writel(ctrl, fbi->regs + SM501_DC_CRT_CONTROL);
  779. sm501fb_sync_regs(fbi);
  780. return 0;
  781. }
  782. /* sm501fb_cursor
  783. *
  784. * set or change the hardware cursor parameters
  785. */
  786. static int sm501fb_cursor(struct fb_info *info, struct fb_cursor *cursor)
  787. {
  788. struct sm501fb_par *par = info->par;
  789. struct sm501fb_info *fbi = par->info;
  790. void __iomem *base = fbi->regs;
  791. unsigned long hwc_addr;
  792. unsigned long fg, bg;
  793. dev_dbg(fbi->dev, "%s(%p,%p)\n", __func__, info, cursor);
  794. if (par->head == HEAD_CRT)
  795. base += SM501_DC_CRT_HWC_BASE;
  796. else
  797. base += SM501_DC_PANEL_HWC_BASE;
  798. /* check not being asked to exceed capabilities */
  799. if (cursor->image.width > 64)
  800. return -EINVAL;
  801. if (cursor->image.height > 64)
  802. return -EINVAL;
  803. if (cursor->image.depth > 1)
  804. return -EINVAL;
  805. hwc_addr = readl(base + SM501_OFF_HWC_ADDR);
  806. if (cursor->enable)
  807. writel(hwc_addr | SM501_HWC_EN, base + SM501_OFF_HWC_ADDR);
  808. else
  809. writel(hwc_addr & ~SM501_HWC_EN, base + SM501_OFF_HWC_ADDR);
  810. /* set data */
  811. if (cursor->set & FB_CUR_SETPOS) {
  812. unsigned int x = cursor->image.dx;
  813. unsigned int y = cursor->image.dy;
  814. if (x >= 2048 || y >= 2048 )
  815. return -EINVAL;
  816. dev_dbg(fbi->dev, "set position %d,%d\n", x, y);
  817. //y += cursor->image.height;
  818. writel(x | (y << 16), base + SM501_OFF_HWC_LOC);
  819. }
  820. if (cursor->set & FB_CUR_SETCMAP) {
  821. unsigned int bg_col = cursor->image.bg_color;
  822. unsigned int fg_col = cursor->image.fg_color;
  823. dev_dbg(fbi->dev, "%s: update cmap (%08x,%08x)\n",
  824. __func__, bg_col, fg_col);
  825. bg = ((info->cmap.red[bg_col] & 0xF8) << 8) |
  826. ((info->cmap.green[bg_col] & 0xFC) << 3) |
  827. ((info->cmap.blue[bg_col] & 0xF8) >> 3);
  828. fg = ((info->cmap.red[fg_col] & 0xF8) << 8) |
  829. ((info->cmap.green[fg_col] & 0xFC) << 3) |
  830. ((info->cmap.blue[fg_col] & 0xF8) >> 3);
  831. dev_dbg(fbi->dev, "fgcol %08lx, bgcol %08lx\n", fg, bg);
  832. writel(bg, base + SM501_OFF_HWC_COLOR_1_2);
  833. writel(fg, base + SM501_OFF_HWC_COLOR_3);
  834. }
  835. if (cursor->set & FB_CUR_SETSIZE ||
  836. cursor->set & (FB_CUR_SETIMAGE | FB_CUR_SETSHAPE)) {
  837. /* SM501 cursor is a two bpp 64x64 bitmap this routine
  838. * clears it to transparent then combines the cursor
  839. * shape plane with the colour plane to set the
  840. * cursor */
  841. int x, y;
  842. const unsigned char *pcol = cursor->image.data;
  843. const unsigned char *pmsk = cursor->mask;
  844. void __iomem *dst = par->cursor.k_addr;
  845. unsigned char dcol = 0;
  846. unsigned char dmsk = 0;
  847. unsigned int op;
  848. dev_dbg(fbi->dev, "%s: setting shape (%d,%d)\n",
  849. __func__, cursor->image.width, cursor->image.height);
  850. for (op = 0; op < (64*64*2)/8; op+=4)
  851. writel(0x0, dst + op);
  852. for (y = 0; y < cursor->image.height; y++) {
  853. for (x = 0; x < cursor->image.width; x++) {
  854. if ((x % 8) == 0) {
  855. dcol = *pcol++;
  856. dmsk = *pmsk++;
  857. } else {
  858. dcol >>= 1;
  859. dmsk >>= 1;
  860. }
  861. if (dmsk & 1) {
  862. op = (dcol & 1) ? 1 : 3;
  863. op <<= ((x % 4) * 2);
  864. op |= readb(dst + (x / 4));
  865. writeb(op, dst + (x / 4));
  866. }
  867. }
  868. dst += (64*2)/8;
  869. }
  870. }
  871. sm501fb_sync_regs(fbi); /* ensure cursor data flushed */
  872. return 0;
  873. }
  874. /* sm501fb_crtsrc_show
  875. *
  876. * device attribute code to show where the crt output is sourced from
  877. */
  878. static ssize_t sm501fb_crtsrc_show(struct device *dev,
  879. struct device_attribute *attr, char *buf)
  880. {
  881. struct sm501fb_info *info = dev_get_drvdata(dev);
  882. unsigned long ctrl;
  883. ctrl = readl(info->regs + SM501_DC_CRT_CONTROL);
  884. ctrl &= SM501_DC_CRT_CONTROL_SEL;
  885. return snprintf(buf, PAGE_SIZE, "%s\n", ctrl ? "crt" : "panel");
  886. }
  887. /* sm501fb_crtsrc_show
  888. *
  889. * device attribute code to set where the crt output is sourced from
  890. */
  891. static ssize_t sm501fb_crtsrc_store(struct device *dev,
  892. struct device_attribute *attr,
  893. const char *buf, size_t len)
  894. {
  895. struct sm501fb_info *info = dev_get_drvdata(dev);
  896. enum sm501_controller head;
  897. unsigned long ctrl;
  898. if (len < 1)
  899. return -EINVAL;
  900. if (strnicmp(buf, "crt", 3) == 0)
  901. head = HEAD_CRT;
  902. else if (strnicmp(buf, "panel", 5) == 0)
  903. head = HEAD_PANEL;
  904. else
  905. return -EINVAL;
  906. dev_info(dev, "setting crt source to head %d\n", head);
  907. ctrl = readl(info->regs + SM501_DC_CRT_CONTROL);
  908. if (head == HEAD_CRT) {
  909. ctrl |= SM501_DC_CRT_CONTROL_SEL;
  910. ctrl |= SM501_DC_CRT_CONTROL_ENABLE;
  911. ctrl |= SM501_DC_CRT_CONTROL_TE;
  912. } else {
  913. ctrl &= ~SM501_DC_CRT_CONTROL_SEL;
  914. ctrl &= ~SM501_DC_CRT_CONTROL_ENABLE;
  915. ctrl &= ~SM501_DC_CRT_CONTROL_TE;
  916. }
  917. writel(ctrl, info->regs + SM501_DC_CRT_CONTROL);
  918. sm501fb_sync_regs(info);
  919. return len;
  920. }
  921. /* Prepare the device_attr for registration with sysfs later */
  922. static DEVICE_ATTR(crt_src, 0666, sm501fb_crtsrc_show, sm501fb_crtsrc_store);
  923. /* sm501fb_show_regs
  924. *
  925. * show the primary sm501 registers
  926. */
  927. static int sm501fb_show_regs(struct sm501fb_info *info, char *ptr,
  928. unsigned int start, unsigned int len)
  929. {
  930. void __iomem *mem = info->regs;
  931. char *buf = ptr;
  932. unsigned int reg;
  933. for (reg = start; reg < (len + start); reg += 4)
  934. ptr += sprintf(ptr, "%08x = %08x\n", reg, readl(mem + reg));
  935. return ptr - buf;
  936. }
  937. /* sm501fb_debug_show_crt
  938. *
  939. * show the crt control and cursor registers
  940. */
  941. static ssize_t sm501fb_debug_show_crt(struct device *dev,
  942. struct device_attribute *attr, char *buf)
  943. {
  944. struct sm501fb_info *info = dev_get_drvdata(dev);
  945. char *ptr = buf;
  946. ptr += sm501fb_show_regs(info, ptr, SM501_DC_CRT_CONTROL, 0x40);
  947. ptr += sm501fb_show_regs(info, ptr, SM501_DC_CRT_HWC_BASE, 0x10);
  948. return ptr - buf;
  949. }
  950. static DEVICE_ATTR(fbregs_crt, 0444, sm501fb_debug_show_crt, NULL);
  951. /* sm501fb_debug_show_pnl
  952. *
  953. * show the panel control and cursor registers
  954. */
  955. static ssize_t sm501fb_debug_show_pnl(struct device *dev,
  956. struct device_attribute *attr, char *buf)
  957. {
  958. struct sm501fb_info *info = dev_get_drvdata(dev);
  959. char *ptr = buf;
  960. ptr += sm501fb_show_regs(info, ptr, 0x0, 0x40);
  961. ptr += sm501fb_show_regs(info, ptr, SM501_DC_PANEL_HWC_BASE, 0x10);
  962. return ptr - buf;
  963. }
  964. static DEVICE_ATTR(fbregs_pnl, 0444, sm501fb_debug_show_pnl, NULL);
  965. /* framebuffer ops */
  966. static struct fb_ops sm501fb_ops_crt = {
  967. .owner = THIS_MODULE,
  968. .fb_check_var = sm501fb_check_var_crt,
  969. .fb_set_par = sm501fb_set_par_crt,
  970. .fb_blank = sm501fb_blank_crt,
  971. .fb_setcolreg = sm501fb_setcolreg,
  972. .fb_pan_display = sm501fb_pan_crt,
  973. .fb_cursor = sm501fb_cursor,
  974. .fb_fillrect = cfb_fillrect,
  975. .fb_copyarea = cfb_copyarea,
  976. .fb_imageblit = cfb_imageblit,
  977. };
  978. static struct fb_ops sm501fb_ops_pnl = {
  979. .owner = THIS_MODULE,
  980. .fb_check_var = sm501fb_check_var_pnl,
  981. .fb_set_par = sm501fb_set_par_pnl,
  982. .fb_pan_display = sm501fb_pan_pnl,
  983. .fb_blank = sm501fb_blank_pnl,
  984. .fb_setcolreg = sm501fb_setcolreg,
  985. .fb_cursor = sm501fb_cursor,
  986. .fb_fillrect = cfb_fillrect,
  987. .fb_copyarea = cfb_copyarea,
  988. .fb_imageblit = cfb_imageblit,
  989. };
  990. /* sm501_init_cursor
  991. *
  992. * initialise hw cursor parameters
  993. */
  994. static int sm501_init_cursor(struct fb_info *fbi, unsigned int reg_base)
  995. {
  996. struct sm501fb_par *par;
  997. struct sm501fb_info *info;
  998. int ret;
  999. if (fbi == NULL)
  1000. return 0;
  1001. par = fbi->par;
  1002. info = par->info;
  1003. par->cursor_regs = info->regs + reg_base;
  1004. ret = sm501_alloc_mem(info, &par->cursor, SM501_MEMF_CURSOR, 1024);
  1005. if (ret < 0)
  1006. return ret;
  1007. /* initialise the colour registers */
  1008. writel(par->cursor.sm_addr, par->cursor_regs + SM501_OFF_HWC_ADDR);
  1009. writel(0x00, par->cursor_regs + SM501_OFF_HWC_LOC);
  1010. writel(0x00, par->cursor_regs + SM501_OFF_HWC_COLOR_1_2);
  1011. writel(0x00, par->cursor_regs + SM501_OFF_HWC_COLOR_3);
  1012. sm501fb_sync_regs(info);
  1013. return 0;
  1014. }
  1015. /* sm501fb_info_start
  1016. *
  1017. * fills the par structure claiming resources and remapping etc.
  1018. */
  1019. static int sm501fb_start(struct sm501fb_info *info,
  1020. struct platform_device *pdev)
  1021. {
  1022. struct resource *res;
  1023. struct device *dev = &pdev->dev;
  1024. int k;
  1025. int ret;
  1026. info->irq = ret = platform_get_irq(pdev, 0);
  1027. if (ret < 0) {
  1028. /* we currently do not use the IRQ */
  1029. dev_warn(dev, "no irq for device\n");
  1030. }
  1031. /* allocate, reserve and remap resources for registers */
  1032. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1033. if (res == NULL) {
  1034. dev_err(dev, "no resource definition for registers\n");
  1035. ret = -ENOENT;
  1036. goto err_release;
  1037. }
  1038. info->regs_res = request_mem_region(res->start,
  1039. res->end - res->start,
  1040. pdev->name);
  1041. if (info->regs_res == NULL) {
  1042. dev_err(dev, "cannot claim registers\n");
  1043. ret = -ENXIO;
  1044. goto err_release;
  1045. }
  1046. info->regs = ioremap(res->start, (res->end - res->start)+1);
  1047. if (info->regs == NULL) {
  1048. dev_err(dev, "cannot remap registers\n");
  1049. ret = -ENXIO;
  1050. goto err_regs_res;
  1051. }
  1052. /* allocate, reserve resources for framebuffer */
  1053. res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  1054. if (res == NULL) {
  1055. dev_err(dev, "no memory resource defined\n");
  1056. ret = -ENXIO;
  1057. goto err_regs_map;
  1058. }
  1059. info->fbmem_res = request_mem_region(res->start,
  1060. (res->end - res->start)+1,
  1061. pdev->name);
  1062. if (info->fbmem_res == NULL) {
  1063. dev_err(dev, "cannot claim framebuffer\n");
  1064. ret = -ENXIO;
  1065. goto err_regs_map;
  1066. }
  1067. info->fbmem = ioremap(res->start, (res->end - res->start)+1);
  1068. if (info->fbmem == NULL) {
  1069. dev_err(dev, "cannot remap framebuffer\n");
  1070. goto err_mem_res;
  1071. }
  1072. info->fbmem_len = (res->end - res->start)+1;
  1073. /* clear framebuffer memory - avoids garbage data on unused fb */
  1074. memset(info->fbmem, 0, info->fbmem_len);
  1075. /* clear palette ram - undefined at power on */
  1076. for (k = 0; k < (256 * 3); k++)
  1077. writel(0, info->regs + SM501_DC_PANEL_PALETTE + (k * 4));
  1078. /* enable display controller */
  1079. sm501_unit_power(dev->parent, SM501_GATE_DISPLAY, 1);
  1080. /* setup cursors */
  1081. sm501_init_cursor(info->fb[HEAD_CRT], SM501_DC_CRT_HWC_ADDR);
  1082. sm501_init_cursor(info->fb[HEAD_PANEL], SM501_DC_PANEL_HWC_ADDR);
  1083. return 0; /* everything is setup */
  1084. err_mem_res:
  1085. release_resource(info->fbmem_res);
  1086. kfree(info->fbmem_res);
  1087. err_regs_map:
  1088. iounmap(info->regs);
  1089. err_regs_res:
  1090. release_resource(info->regs_res);
  1091. kfree(info->regs_res);
  1092. err_release:
  1093. return ret;
  1094. }
  1095. static void sm501fb_stop(struct sm501fb_info *info)
  1096. {
  1097. /* disable display controller */
  1098. sm501_unit_power(info->dev->parent, SM501_GATE_DISPLAY, 0);
  1099. iounmap(info->fbmem);
  1100. release_resource(info->fbmem_res);
  1101. kfree(info->fbmem_res);
  1102. iounmap(info->regs);
  1103. release_resource(info->regs_res);
  1104. kfree(info->regs_res);
  1105. }
  1106. static int sm501fb_init_fb(struct fb_info *fb,
  1107. enum sm501_controller head,
  1108. const char *fbname)
  1109. {
  1110. struct sm501_platdata_fbsub *pd;
  1111. struct sm501fb_par *par = fb->par;
  1112. struct sm501fb_info *info = par->info;
  1113. unsigned long ctrl;
  1114. unsigned int enable;
  1115. int ret;
  1116. switch (head) {
  1117. case HEAD_CRT:
  1118. pd = info->pdata->fb_crt;
  1119. ctrl = readl(info->regs + SM501_DC_CRT_CONTROL);
  1120. enable = (ctrl & SM501_DC_CRT_CONTROL_ENABLE) ? 1 : 0;
  1121. /* ensure we set the correct source register */
  1122. if (info->pdata->fb_route != SM501_FB_CRT_PANEL) {
  1123. ctrl |= SM501_DC_CRT_CONTROL_SEL;
  1124. writel(ctrl, info->regs + SM501_DC_CRT_CONTROL);
  1125. }
  1126. break;
  1127. case HEAD_PANEL:
  1128. pd = info->pdata->fb_pnl;
  1129. ctrl = readl(info->regs + SM501_DC_PANEL_CONTROL);
  1130. enable = (ctrl & SM501_DC_PANEL_CONTROL_EN) ? 1 : 0;
  1131. break;
  1132. default:
  1133. pd = NULL; /* stop compiler warnings */
  1134. ctrl = 0;
  1135. enable = 0;
  1136. BUG();
  1137. }
  1138. dev_info(info->dev, "fb %s %sabled at start\n",
  1139. fbname, enable ? "en" : "dis");
  1140. /* check to see if our routing allows this */
  1141. if (head == HEAD_CRT && info->pdata->fb_route == SM501_FB_CRT_PANEL) {
  1142. ctrl &= ~SM501_DC_CRT_CONTROL_SEL;
  1143. writel(ctrl, info->regs + SM501_DC_CRT_CONTROL);
  1144. enable = 0;
  1145. }
  1146. strlcpy(fb->fix.id, fbname, sizeof(fb->fix.id));
  1147. memcpy(&par->ops,
  1148. (head == HEAD_CRT) ? &sm501fb_ops_crt : &sm501fb_ops_pnl,
  1149. sizeof(struct fb_ops));
  1150. /* update ops dependant on what we've been passed */
  1151. if ((pd->flags & SM501FB_FLAG_USE_HWCURSOR) == 0)
  1152. par->ops.fb_cursor = NULL;
  1153. fb->fbops = &par->ops;
  1154. fb->flags = FBINFO_FLAG_DEFAULT |
  1155. FBINFO_HWACCEL_XPAN | FBINFO_HWACCEL_YPAN;
  1156. /* fixed data */
  1157. fb->fix.type = FB_TYPE_PACKED_PIXELS;
  1158. fb->fix.type_aux = 0;
  1159. fb->fix.xpanstep = 1;
  1160. fb->fix.ypanstep = 1;
  1161. fb->fix.ywrapstep = 0;
  1162. fb->fix.accel = FB_ACCEL_NONE;
  1163. /* screenmode */
  1164. fb->var.nonstd = 0;
  1165. fb->var.activate = FB_ACTIVATE_NOW;
  1166. fb->var.accel_flags = 0;
  1167. fb->var.vmode = FB_VMODE_NONINTERLACED;
  1168. fb->var.bits_per_pixel = 16;
  1169. if (enable && (pd->flags & SM501FB_FLAG_USE_INIT_MODE) && 0) {
  1170. /* TODO read the mode from the current display */
  1171. } else {
  1172. if (pd->def_mode) {
  1173. dev_info(info->dev, "using supplied mode\n");
  1174. fb_videomode_to_var(&fb->var, pd->def_mode);
  1175. fb->var.bits_per_pixel = pd->def_bpp ? pd->def_bpp : 8;
  1176. fb->var.xres_virtual = fb->var.xres;
  1177. fb->var.yres_virtual = fb->var.yres;
  1178. } else {
  1179. ret = fb_find_mode(&fb->var, fb,
  1180. NULL, NULL, 0, NULL, 8);
  1181. if (ret == 0 || ret == 4) {
  1182. dev_err(info->dev,
  1183. "failed to get initial mode\n");
  1184. return -EINVAL;
  1185. }
  1186. }
  1187. }
  1188. /* initialise and set the palette */
  1189. fb_alloc_cmap(&fb->cmap, NR_PALETTE, 0);
  1190. fb_set_cmap(&fb->cmap, fb);
  1191. ret = (fb->fbops->fb_check_var)(&fb->var, fb);
  1192. if (ret)
  1193. dev_err(info->dev, "check_var() failed on initial setup?\n");
  1194. /* ensure we've activated our new configuration */
  1195. (fb->fbops->fb_set_par)(fb);
  1196. return 0;
  1197. }
  1198. /* default platform data if none is supplied (ie, PCI device) */
  1199. static struct sm501_platdata_fbsub sm501fb_pdata_crt = {
  1200. .flags = (SM501FB_FLAG_USE_INIT_MODE |
  1201. SM501FB_FLAG_USE_HWCURSOR |
  1202. SM501FB_FLAG_USE_HWACCEL |
  1203. SM501FB_FLAG_DISABLE_AT_EXIT),
  1204. };
  1205. static struct sm501_platdata_fbsub sm501fb_pdata_pnl = {
  1206. .flags = (SM501FB_FLAG_USE_INIT_MODE |
  1207. SM501FB_FLAG_USE_HWCURSOR |
  1208. SM501FB_FLAG_USE_HWACCEL |
  1209. SM501FB_FLAG_DISABLE_AT_EXIT),
  1210. };
  1211. static struct sm501_platdata_fb sm501fb_def_pdata = {
  1212. .fb_route = SM501_FB_OWN,
  1213. .fb_crt = &sm501fb_pdata_crt,
  1214. .fb_pnl = &sm501fb_pdata_pnl,
  1215. };
  1216. static char driver_name_crt[] = "sm501fb-crt";
  1217. static char driver_name_pnl[] = "sm501fb-panel";
  1218. static int __devinit sm501fb_probe_one(struct sm501fb_info *info,
  1219. enum sm501_controller head)
  1220. {
  1221. unsigned char *name = (head == HEAD_CRT) ? "crt" : "panel";
  1222. struct sm501_platdata_fbsub *pd;
  1223. struct sm501fb_par *par;
  1224. struct fb_info *fbi;
  1225. pd = (head == HEAD_CRT) ? info->pdata->fb_crt : info->pdata->fb_pnl;
  1226. /* Do not initialise if we've not been given any platform data */
  1227. if (pd == NULL) {
  1228. dev_info(info->dev, "no data for fb %s (disabled)\n", name);
  1229. return 0;
  1230. }
  1231. fbi = framebuffer_alloc(sizeof(struct sm501fb_par), info->dev);
  1232. if (fbi == NULL) {
  1233. dev_err(info->dev, "cannot allocate %s framebuffer\n", name);
  1234. return -ENOMEM;
  1235. }
  1236. par = fbi->par;
  1237. par->info = info;
  1238. par->head = head;
  1239. fbi->pseudo_palette = &par->pseudo_palette;
  1240. info->fb[head] = fbi;
  1241. return 0;
  1242. }
  1243. /* Free up anything allocated by sm501fb_init_fb */
  1244. static void sm501_free_init_fb(struct sm501fb_info *info,
  1245. enum sm501_controller head)
  1246. {
  1247. struct fb_info *fbi = info->fb[head];
  1248. fb_dealloc_cmap(&fbi->cmap);
  1249. }
  1250. static int __devinit sm501fb_start_one(struct sm501fb_info *info,
  1251. enum sm501_controller head,
  1252. const char *drvname)
  1253. {
  1254. struct fb_info *fbi = info->fb[head];
  1255. int ret;
  1256. if (!fbi)
  1257. return 0;
  1258. ret = sm501fb_init_fb(info->fb[head], head, drvname);
  1259. if (ret) {
  1260. dev_err(info->dev, "cannot initialise fb %s\n", drvname);
  1261. return ret;
  1262. }
  1263. ret = register_framebuffer(info->fb[head]);
  1264. if (ret) {
  1265. dev_err(info->dev, "failed to register fb %s\n", drvname);
  1266. sm501_free_init_fb(info, head);
  1267. return ret;
  1268. }
  1269. dev_info(info->dev, "fb%d: %s frame buffer\n", fbi->node, fbi->fix.id);
  1270. return 0;
  1271. }
  1272. static int __devinit sm501fb_probe(struct platform_device *pdev)
  1273. {
  1274. struct sm501fb_info *info;
  1275. struct device *dev = &pdev->dev;
  1276. int ret;
  1277. /* allocate our framebuffers */
  1278. info = kzalloc(sizeof(struct sm501fb_info), GFP_KERNEL);
  1279. if (!info) {
  1280. dev_err(dev, "failed to allocate state\n");
  1281. return -ENOMEM;
  1282. }
  1283. info->dev = dev = &pdev->dev;
  1284. platform_set_drvdata(pdev, info);
  1285. if (dev->parent->platform_data) {
  1286. struct sm501_platdata *pd = dev->parent->platform_data;
  1287. info->pdata = pd->fb;
  1288. }
  1289. if (info->pdata == NULL) {
  1290. dev_info(dev, "using default configuration data\n");
  1291. info->pdata = &sm501fb_def_pdata;
  1292. }
  1293. /* probe for the presence of each panel */
  1294. ret = sm501fb_probe_one(info, HEAD_CRT);
  1295. if (ret < 0) {
  1296. dev_err(dev, "failed to probe CRT\n");
  1297. goto err_alloc;
  1298. }
  1299. ret = sm501fb_probe_one(info, HEAD_PANEL);
  1300. if (ret < 0) {
  1301. dev_err(dev, "failed to probe PANEL\n");
  1302. goto err_probed_crt;
  1303. }
  1304. if (info->fb[HEAD_PANEL] == NULL &&
  1305. info->fb[HEAD_CRT] == NULL) {
  1306. dev_err(dev, "no framebuffers found\n");
  1307. goto err_alloc;
  1308. }
  1309. /* get the resources for both of the framebuffers */
  1310. ret = sm501fb_start(info, pdev);
  1311. if (ret) {
  1312. dev_err(dev, "cannot initialise SM501\n");
  1313. goto err_probed_panel;
  1314. }
  1315. ret = sm501fb_start_one(info, HEAD_CRT, driver_name_crt);
  1316. if (ret) {
  1317. dev_err(dev, "failed to start CRT\n");
  1318. goto err_started;
  1319. }
  1320. ret = sm501fb_start_one(info, HEAD_PANEL, driver_name_pnl);
  1321. if (ret) {
  1322. dev_err(dev, "failed to start Panel\n");
  1323. goto err_started_crt;
  1324. }
  1325. /* create device files */
  1326. ret = device_create_file(dev, &dev_attr_crt_src);
  1327. if (ret)
  1328. goto err_started_panel;
  1329. ret = device_create_file(dev, &dev_attr_fbregs_pnl);
  1330. if (ret)
  1331. goto err_attached_crtsrc_file;
  1332. ret = device_create_file(dev, &dev_attr_fbregs_crt);
  1333. if (ret)
  1334. goto err_attached_pnlregs_file;
  1335. /* we registered, return ok */
  1336. return 0;
  1337. err_attached_pnlregs_file:
  1338. device_remove_file(dev, &dev_attr_fbregs_pnl);
  1339. err_attached_crtsrc_file:
  1340. device_remove_file(dev, &dev_attr_crt_src);
  1341. err_started_panel:
  1342. unregister_framebuffer(info->fb[HEAD_PANEL]);
  1343. sm501_free_init_fb(info, HEAD_PANEL);
  1344. err_started_crt:
  1345. unregister_framebuffer(info->fb[HEAD_CRT]);
  1346. sm501_free_init_fb(info, HEAD_CRT);
  1347. err_started:
  1348. sm501fb_stop(info);
  1349. err_probed_panel:
  1350. framebuffer_release(info->fb[HEAD_PANEL]);
  1351. err_probed_crt:
  1352. framebuffer_release(info->fb[HEAD_CRT]);
  1353. err_alloc:
  1354. kfree(info);
  1355. return ret;
  1356. }
  1357. /*
  1358. * Cleanup
  1359. */
  1360. static int sm501fb_remove(struct platform_device *pdev)
  1361. {
  1362. struct sm501fb_info *info = platform_get_drvdata(pdev);
  1363. struct fb_info *fbinfo_crt = info->fb[0];
  1364. struct fb_info *fbinfo_pnl = info->fb[1];
  1365. device_remove_file(&pdev->dev, &dev_attr_fbregs_crt);
  1366. device_remove_file(&pdev->dev, &dev_attr_fbregs_pnl);
  1367. device_remove_file(&pdev->dev, &dev_attr_crt_src);
  1368. sm501_free_init_fb(info, HEAD_CRT);
  1369. sm501_free_init_fb(info, HEAD_PANEL);
  1370. unregister_framebuffer(fbinfo_crt);
  1371. unregister_framebuffer(fbinfo_pnl);
  1372. sm501fb_stop(info);
  1373. kfree(info);
  1374. framebuffer_release(fbinfo_pnl);
  1375. framebuffer_release(fbinfo_crt);
  1376. return 0;
  1377. }
  1378. #ifdef CONFIG_PM
  1379. static int sm501fb_suspend_fb(struct sm501fb_info *info,
  1380. enum sm501_controller head)
  1381. {
  1382. struct fb_info *fbi = info->fb[head];
  1383. struct sm501fb_par *par = fbi->par;
  1384. if (par->screen.size == 0)
  1385. return 0;
  1386. /* blank the relevant interface to ensure unit power minimised */
  1387. (par->ops.fb_blank)(FB_BLANK_POWERDOWN, fbi);
  1388. /* tell console/fb driver we are suspending */
  1389. acquire_console_sem();
  1390. fb_set_suspend(fbi, 1);
  1391. release_console_sem();
  1392. /* backup copies in case chip is powered down over suspend */
  1393. par->store_fb = vmalloc(par->screen.size);
  1394. if (par->store_fb == NULL) {
  1395. dev_err(info->dev, "no memory to store screen\n");
  1396. return -ENOMEM;
  1397. }
  1398. par->store_cursor = vmalloc(par->cursor.size);
  1399. if (par->store_cursor == NULL) {
  1400. dev_err(info->dev, "no memory to store cursor\n");
  1401. goto err_nocursor;
  1402. }
  1403. dev_dbg(info->dev, "suspending screen to %p\n", par->store_fb);
  1404. dev_dbg(info->dev, "suspending cursor to %p\n", par->store_cursor);
  1405. memcpy_fromio(par->store_fb, par->screen.k_addr, par->screen.size);
  1406. memcpy_fromio(par->store_cursor, par->cursor.k_addr, par->cursor.size);
  1407. return 0;
  1408. err_nocursor:
  1409. vfree(par->store_fb);
  1410. par->store_fb = NULL;
  1411. return -ENOMEM;
  1412. }
  1413. static void sm501fb_resume_fb(struct sm501fb_info *info,
  1414. enum sm501_controller head)
  1415. {
  1416. struct fb_info *fbi = info->fb[head];
  1417. struct sm501fb_par *par = fbi->par;
  1418. if (par->screen.size == 0)
  1419. return;
  1420. /* re-activate the configuration */
  1421. (par->ops.fb_set_par)(fbi);
  1422. /* restore the data */
  1423. dev_dbg(info->dev, "restoring screen from %p\n", par->store_fb);
  1424. dev_dbg(info->dev, "restoring cursor from %p\n", par->store_cursor);
  1425. if (par->store_fb)
  1426. memcpy_toio(par->screen.k_addr, par->store_fb,
  1427. par->screen.size);
  1428. if (par->store_cursor)
  1429. memcpy_toio(par->cursor.k_addr, par->store_cursor,
  1430. par->cursor.size);
  1431. acquire_console_sem();
  1432. fb_set_suspend(fbi, 0);
  1433. release_console_sem();
  1434. vfree(par->store_fb);
  1435. vfree(par->store_cursor);
  1436. }
  1437. /* suspend and resume support */
  1438. static int sm501fb_suspend(struct platform_device *pdev, pm_message_t state)
  1439. {
  1440. struct sm501fb_info *info = platform_get_drvdata(pdev);
  1441. /* store crt control to resume with */
  1442. info->pm_crt_ctrl = readl(info->regs + SM501_DC_CRT_CONTROL);
  1443. sm501fb_suspend_fb(info, HEAD_CRT);
  1444. sm501fb_suspend_fb(info, HEAD_PANEL);
  1445. /* turn off the clocks, in case the device is not powered down */
  1446. sm501_unit_power(info->dev->parent, SM501_GATE_DISPLAY, 0);
  1447. return 0;
  1448. }
  1449. #define SM501_CRT_CTRL_SAVE (SM501_DC_CRT_CONTROL_TVP | \
  1450. SM501_DC_CRT_CONTROL_SEL)
  1451. static int sm501fb_resume(struct platform_device *pdev)
  1452. {
  1453. struct sm501fb_info *info = platform_get_drvdata(pdev);
  1454. unsigned long crt_ctrl;
  1455. sm501_unit_power(info->dev->parent, SM501_GATE_DISPLAY, 1);
  1456. /* restore the items we want to be saved for crt control */
  1457. crt_ctrl = readl(info->regs + SM501_DC_CRT_CONTROL);
  1458. crt_ctrl &= ~SM501_CRT_CTRL_SAVE;
  1459. crt_ctrl |= info->pm_crt_ctrl & SM501_CRT_CTRL_SAVE;
  1460. writel(crt_ctrl, info->regs + SM501_DC_CRT_CONTROL);
  1461. sm501fb_resume_fb(info, HEAD_CRT);
  1462. sm501fb_resume_fb(info, HEAD_PANEL);
  1463. return 0;
  1464. }
  1465. #else
  1466. #define sm501fb_suspend NULL
  1467. #define sm501fb_resume NULL
  1468. #endif
  1469. static struct platform_driver sm501fb_driver = {
  1470. .probe = sm501fb_probe,
  1471. .remove = sm501fb_remove,
  1472. .suspend = sm501fb_suspend,
  1473. .resume = sm501fb_resume,
  1474. .driver = {
  1475. .name = "sm501-fb",
  1476. .owner = THIS_MODULE,
  1477. },
  1478. };
  1479. static int __devinit sm501fb_init(void)
  1480. {
  1481. return platform_driver_register(&sm501fb_driver);
  1482. }
  1483. static void __exit sm501fb_cleanup(void)
  1484. {
  1485. platform_driver_unregister(&sm501fb_driver);
  1486. }
  1487. module_init(sm501fb_init);
  1488. module_exit(sm501fb_cleanup);
  1489. MODULE_AUTHOR("Ben Dooks, Vincent Sanders");
  1490. MODULE_DESCRIPTION("SM501 Framebuffer driver");
  1491. MODULE_LICENSE("GPL v2");