sm501fb.c 53 KB

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