sm501fb.c 43 KB

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