sm501fb.c 43 KB

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