sm501fb.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847
  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_DIRECTCOLOR;
  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. break;
  484. case 32:
  485. control |= SM501_DC_CRT_CONTROL_32BPP;
  486. sm501fb_setup_gamma(fbi, SM501_DC_CRT_PALETTE);
  487. break;
  488. default:
  489. BUG();
  490. }
  491. control |= SM501_DC_CRT_CONTROL_SEL; /* CRT displays CRT data */
  492. control |= SM501_DC_CRT_CONTROL_TE; /* enable CRT timing */
  493. control |= SM501_DC_CRT_CONTROL_ENABLE; /* enable CRT plane */
  494. out_update:
  495. dev_dbg(fbi->dev, "new control is %08lx\n", control);
  496. writel(control, fbi->regs + SM501_DC_CRT_CONTROL);
  497. sm501fb_sync_regs(fbi);
  498. return 0;
  499. }
  500. static void sm501fb_panel_power(struct sm501fb_info *fbi, int to)
  501. {
  502. unsigned long control;
  503. void __iomem *ctrl_reg = fbi->regs + SM501_DC_PANEL_CONTROL;
  504. struct sm501_platdata_fbsub *pd = fbi->pdata->fb_pnl;
  505. control = readl(ctrl_reg);
  506. if (to && (control & SM501_DC_PANEL_CONTROL_VDD) == 0) {
  507. /* enable panel power */
  508. control |= SM501_DC_PANEL_CONTROL_VDD; /* FPVDDEN */
  509. writel(control, ctrl_reg);
  510. sm501fb_sync_regs(fbi);
  511. mdelay(10);
  512. control |= SM501_DC_PANEL_CONTROL_DATA; /* DATA */
  513. writel(control, ctrl_reg);
  514. sm501fb_sync_regs(fbi);
  515. mdelay(10);
  516. if (pd->flags & SM501FB_FLAG_PANEL_USE_VBIASEN) {
  517. control |= SM501_DC_PANEL_CONTROL_BIAS; /* VBIASEN */
  518. writel(control, ctrl_reg);
  519. sm501fb_sync_regs(fbi);
  520. mdelay(10);
  521. }
  522. if (pd->flags & SM501FB_FLAG_PANEL_USE_FPEN) {
  523. control |= SM501_DC_PANEL_CONTROL_FPEN;
  524. writel(control, ctrl_reg);
  525. sm501fb_sync_regs(fbi);
  526. mdelay(10);
  527. }
  528. } else if (!to && (control & SM501_DC_PANEL_CONTROL_VDD) != 0) {
  529. /* disable panel power */
  530. if (pd->flags & SM501FB_FLAG_PANEL_USE_FPEN) {
  531. control &= ~SM501_DC_PANEL_CONTROL_FPEN;
  532. writel(control, ctrl_reg);
  533. sm501fb_sync_regs(fbi);
  534. mdelay(10);
  535. }
  536. if (pd->flags & SM501FB_FLAG_PANEL_USE_VBIASEN) {
  537. control &= ~SM501_DC_PANEL_CONTROL_BIAS;
  538. writel(control, ctrl_reg);
  539. sm501fb_sync_regs(fbi);
  540. mdelay(10);
  541. }
  542. control &= ~SM501_DC_PANEL_CONTROL_DATA;
  543. writel(control, ctrl_reg);
  544. sm501fb_sync_regs(fbi);
  545. mdelay(10);
  546. control &= ~SM501_DC_PANEL_CONTROL_VDD;
  547. writel(control, ctrl_reg);
  548. sm501fb_sync_regs(fbi);
  549. mdelay(10);
  550. }
  551. sm501fb_sync_regs(fbi);
  552. }
  553. /* sm501fb_set_par_pnl
  554. *
  555. * Set the panel video mode from the fb_info structure
  556. */
  557. static int sm501fb_set_par_pnl(struct fb_info *info)
  558. {
  559. struct sm501fb_par *par = info->par;
  560. struct sm501fb_info *fbi = par->info;
  561. struct fb_var_screeninfo *var = &info->var;
  562. unsigned long control;
  563. unsigned long reg;
  564. int ret;
  565. dev_dbg(fbi->dev, "%s(%p)\n", __func__, info);
  566. /* activate this new configuration */
  567. ret = sm501fb_set_par_common(info, var);
  568. if (ret)
  569. return ret;
  570. sm501fb_pan_pnl(var, info);
  571. sm501fb_set_par_geometry(info, var);
  572. /* update control register */
  573. control = readl(fbi->regs + SM501_DC_PANEL_CONTROL);
  574. control &= (SM501_DC_PANEL_CONTROL_GAMMA |
  575. SM501_DC_PANEL_CONTROL_VDD |
  576. SM501_DC_PANEL_CONTROL_DATA |
  577. SM501_DC_PANEL_CONTROL_BIAS |
  578. SM501_DC_PANEL_CONTROL_FPEN |
  579. SM501_DC_PANEL_CONTROL_CP |
  580. SM501_DC_PANEL_CONTROL_CK |
  581. SM501_DC_PANEL_CONTROL_HP |
  582. SM501_DC_PANEL_CONTROL_VP |
  583. SM501_DC_PANEL_CONTROL_HPD |
  584. SM501_DC_PANEL_CONTROL_VPD);
  585. control |= SM501_FIFO_3; /* fill if >3 free slots */
  586. switch(var->bits_per_pixel) {
  587. case 8:
  588. control |= SM501_DC_PANEL_CONTROL_8BPP;
  589. break;
  590. case 16:
  591. control |= SM501_DC_PANEL_CONTROL_16BPP;
  592. break;
  593. case 32:
  594. control |= SM501_DC_PANEL_CONTROL_32BPP;
  595. sm501fb_setup_gamma(fbi, SM501_DC_PANEL_PALETTE);
  596. break;
  597. default:
  598. BUG();
  599. }
  600. writel(0x0, fbi->regs + SM501_DC_PANEL_PANNING_CONTROL);
  601. /* panel plane top left and bottom right location */
  602. writel(0x00, fbi->regs + SM501_DC_PANEL_TL_LOC);
  603. reg = var->xres - 1;
  604. reg |= (var->yres - 1) << 16;
  605. writel(reg, fbi->regs + SM501_DC_PANEL_BR_LOC);
  606. /* program panel control register */
  607. control |= SM501_DC_PANEL_CONTROL_TE; /* enable PANEL timing */
  608. control |= SM501_DC_PANEL_CONTROL_EN; /* enable PANEL gfx plane */
  609. if ((var->sync & FB_SYNC_HOR_HIGH_ACT) == 0)
  610. control |= SM501_DC_PANEL_CONTROL_HSP;
  611. if ((var->sync & FB_SYNC_VERT_HIGH_ACT) == 0)
  612. control |= SM501_DC_PANEL_CONTROL_VSP;
  613. writel(control, fbi->regs + SM501_DC_PANEL_CONTROL);
  614. sm501fb_sync_regs(fbi);
  615. /* ensure the panel interface is not tristated at this point */
  616. sm501_modify_reg(fbi->dev->parent, SM501_SYSTEM_CONTROL,
  617. 0, SM501_SYSCTRL_PANEL_TRISTATE);
  618. /* power the panel up */
  619. sm501fb_panel_power(fbi, 1);
  620. return 0;
  621. }
  622. /* chan_to_field
  623. *
  624. * convert a colour value into a field position
  625. *
  626. * from pxafb.c
  627. */
  628. static inline unsigned int chan_to_field(unsigned int chan,
  629. struct fb_bitfield *bf)
  630. {
  631. chan &= 0xffff;
  632. chan >>= 16 - bf->length;
  633. return chan << bf->offset;
  634. }
  635. /* sm501fb_setcolreg
  636. *
  637. * set the colour mapping for modes that support palettised data
  638. */
  639. static int sm501fb_setcolreg(unsigned regno,
  640. unsigned red, unsigned green, unsigned blue,
  641. unsigned transp, struct fb_info *info)
  642. {
  643. struct sm501fb_par *par = info->par;
  644. struct sm501fb_info *fbi = par->info;
  645. void __iomem *base = fbi->regs;
  646. unsigned int val;
  647. if (par->head == HEAD_CRT)
  648. base += SM501_DC_CRT_PALETTE;
  649. else
  650. base += SM501_DC_PANEL_PALETTE;
  651. switch (info->fix.visual) {
  652. case FB_VISUAL_TRUECOLOR:
  653. /* true-colour, use pseuo-palette */
  654. if (regno < 16) {
  655. u32 *pal = par->pseudo_palette;
  656. val = chan_to_field(red, &info->var.red);
  657. val |= chan_to_field(green, &info->var.green);
  658. val |= chan_to_field(blue, &info->var.blue);
  659. pal[regno] = val;
  660. }
  661. break;
  662. case FB_VISUAL_PSEUDOCOLOR:
  663. if (regno < 256) {
  664. val = (red >> 8) << 16;
  665. val |= (green >> 8) << 8;
  666. val |= blue >> 8;
  667. writel(val, base + (regno * 4));
  668. }
  669. break;
  670. default:
  671. return 1; /* unknown type */
  672. }
  673. return 0;
  674. }
  675. /* sm501fb_blank_pnl
  676. *
  677. * Blank or un-blank the panel interface
  678. */
  679. static int sm501fb_blank_pnl(int blank_mode, struct fb_info *info)
  680. {
  681. struct sm501fb_par *par = info->par;
  682. struct sm501fb_info *fbi = par->info;
  683. dev_dbg(fbi->dev, "%s(mode=%d, %p)\n", __func__, blank_mode, info);
  684. switch (blank_mode) {
  685. case FB_BLANK_POWERDOWN:
  686. sm501fb_panel_power(fbi, 0);
  687. break;
  688. case FB_BLANK_UNBLANK:
  689. sm501fb_panel_power(fbi, 1);
  690. break;
  691. case FB_BLANK_NORMAL:
  692. case FB_BLANK_VSYNC_SUSPEND:
  693. case FB_BLANK_HSYNC_SUSPEND:
  694. default:
  695. return 1;
  696. }
  697. return 0;
  698. }
  699. /* sm501fb_blank_crt
  700. *
  701. * Blank or un-blank the crt interface
  702. */
  703. static int sm501fb_blank_crt(int blank_mode, struct fb_info *info)
  704. {
  705. struct sm501fb_par *par = info->par;
  706. struct sm501fb_info *fbi = par->info;
  707. unsigned long ctrl;
  708. dev_dbg(fbi->dev, "%s(mode=%d, %p)\n", __func__, blank_mode, info);
  709. ctrl = readl(fbi->regs + SM501_DC_CRT_CONTROL);
  710. switch (blank_mode) {
  711. case FB_BLANK_POWERDOWN:
  712. ctrl &= ~SM501_DC_CRT_CONTROL_ENABLE;
  713. sm501_misc_control(fbi->dev->parent, SM501_MISC_DAC_POWER, 0);
  714. case FB_BLANK_NORMAL:
  715. ctrl |= SM501_DC_CRT_CONTROL_BLANK;
  716. break;
  717. case FB_BLANK_UNBLANK:
  718. ctrl &= ~SM501_DC_CRT_CONTROL_BLANK;
  719. ctrl |= SM501_DC_CRT_CONTROL_ENABLE;
  720. sm501_misc_control(fbi->dev->parent, 0, SM501_MISC_DAC_POWER);
  721. break;
  722. case FB_BLANK_VSYNC_SUSPEND:
  723. case FB_BLANK_HSYNC_SUSPEND:
  724. default:
  725. return 1;
  726. }
  727. writel(ctrl, fbi->regs + SM501_DC_CRT_CONTROL);
  728. sm501fb_sync_regs(fbi);
  729. return 0;
  730. }
  731. /* sm501fb_cursor
  732. *
  733. * set or change the hardware cursor parameters
  734. */
  735. static int sm501fb_cursor(struct fb_info *info, struct fb_cursor *cursor)
  736. {
  737. struct sm501fb_par *par = info->par;
  738. struct sm501fb_info *fbi = par->info;
  739. void __iomem *base = fbi->regs;
  740. unsigned long hwc_addr;
  741. unsigned long fg, bg;
  742. dev_dbg(fbi->dev, "%s(%p,%p)\n", __func__, info, cursor);
  743. if (par->head == HEAD_CRT)
  744. base += SM501_DC_CRT_HWC_BASE;
  745. else
  746. base += SM501_DC_PANEL_HWC_BASE;
  747. /* check not being asked to exceed capabilities */
  748. if (cursor->image.width > 64)
  749. return -EINVAL;
  750. if (cursor->image.height > 64)
  751. return -EINVAL;
  752. if (cursor->image.depth > 1)
  753. return -EINVAL;
  754. hwc_addr = readl(base + SM501_OFF_HWC_ADDR);
  755. if (cursor->enable)
  756. writel(hwc_addr | SM501_HWC_EN, base + SM501_OFF_HWC_ADDR);
  757. else
  758. writel(hwc_addr & ~SM501_HWC_EN, base + SM501_OFF_HWC_ADDR);
  759. /* set data */
  760. if (cursor->set & FB_CUR_SETPOS) {
  761. unsigned int x = cursor->image.dx;
  762. unsigned int y = cursor->image.dy;
  763. if (x >= 2048 || y >= 2048 )
  764. return -EINVAL;
  765. dev_dbg(fbi->dev, "set position %d,%d\n", x, y);
  766. //y += cursor->image.height;
  767. writel(x | (y << 16), base + SM501_OFF_HWC_LOC);
  768. }
  769. if (cursor->set & FB_CUR_SETCMAP) {
  770. unsigned int bg_col = cursor->image.bg_color;
  771. unsigned int fg_col = cursor->image.fg_color;
  772. dev_dbg(fbi->dev, "%s: update cmap (%08x,%08x)\n",
  773. __func__, bg_col, fg_col);
  774. bg = ((info->cmap.red[bg_col] & 0xF8) << 8) |
  775. ((info->cmap.green[bg_col] & 0xFC) << 3) |
  776. ((info->cmap.blue[bg_col] & 0xF8) >> 3);
  777. fg = ((info->cmap.red[fg_col] & 0xF8) << 8) |
  778. ((info->cmap.green[fg_col] & 0xFC) << 3) |
  779. ((info->cmap.blue[fg_col] & 0xF8) >> 3);
  780. dev_dbg(fbi->dev, "fgcol %08lx, bgcol %08lx\n", fg, bg);
  781. writel(bg, base + SM501_OFF_HWC_COLOR_1_2);
  782. writel(fg, base + SM501_OFF_HWC_COLOR_3);
  783. }
  784. if (cursor->set & FB_CUR_SETSIZE ||
  785. cursor->set & (FB_CUR_SETIMAGE | FB_CUR_SETSHAPE)) {
  786. /* SM501 cursor is a two bpp 64x64 bitmap this routine
  787. * clears it to transparent then combines the cursor
  788. * shape plane with the colour plane to set the
  789. * cursor */
  790. int x, y;
  791. const unsigned char *pcol = cursor->image.data;
  792. const unsigned char *pmsk = cursor->mask;
  793. void __iomem *dst = par->cursor.k_addr;
  794. unsigned char dcol = 0;
  795. unsigned char dmsk = 0;
  796. unsigned int op;
  797. dev_dbg(fbi->dev, "%s: setting shape (%d,%d)\n",
  798. __func__, cursor->image.width, cursor->image.height);
  799. for (op = 0; op < (64*64*2)/8; op+=4)
  800. writel(0x0, dst + op);
  801. for (y = 0; y < cursor->image.height; y++) {
  802. for (x = 0; x < cursor->image.width; x++) {
  803. if ((x % 8) == 0) {
  804. dcol = *pcol++;
  805. dmsk = *pmsk++;
  806. } else {
  807. dcol >>= 1;
  808. dmsk >>= 1;
  809. }
  810. if (dmsk & 1) {
  811. op = (dcol & 1) ? 1 : 3;
  812. op <<= ((x % 4) * 2);
  813. op |= readb(dst + (x / 4));
  814. writeb(op, dst + (x / 4));
  815. }
  816. }
  817. dst += (64*2)/8;
  818. }
  819. }
  820. sm501fb_sync_regs(fbi); /* ensure cursor data flushed */
  821. return 0;
  822. }
  823. /* sm501fb_crtsrc_show
  824. *
  825. * device attribute code to show where the crt output is sourced from
  826. */
  827. static ssize_t sm501fb_crtsrc_show(struct device *dev,
  828. struct device_attribute *attr, char *buf)
  829. {
  830. struct sm501fb_info *info = dev_get_drvdata(dev);
  831. unsigned long ctrl;
  832. ctrl = readl(info->regs + SM501_DC_CRT_CONTROL);
  833. ctrl &= SM501_DC_CRT_CONTROL_SEL;
  834. return snprintf(buf, PAGE_SIZE, "%s\n", ctrl ? "crt" : "panel");
  835. }
  836. /* sm501fb_crtsrc_show
  837. *
  838. * device attribute code to set where the crt output is sourced from
  839. */
  840. static ssize_t sm501fb_crtsrc_store(struct device *dev,
  841. struct device_attribute *attr,
  842. const char *buf, size_t len)
  843. {
  844. struct sm501fb_info *info = dev_get_drvdata(dev);
  845. enum sm501_controller head;
  846. unsigned long ctrl;
  847. if (len < 1)
  848. return -EINVAL;
  849. if (strnicmp(buf, "crt", 3) == 0)
  850. head = HEAD_CRT;
  851. else if (strnicmp(buf, "panel", 5) == 0)
  852. head = HEAD_PANEL;
  853. else
  854. return -EINVAL;
  855. dev_info(dev, "setting crt source to head %d\n", head);
  856. ctrl = readl(info->regs + SM501_DC_CRT_CONTROL);
  857. if (head == HEAD_CRT) {
  858. ctrl |= SM501_DC_CRT_CONTROL_SEL;
  859. ctrl |= SM501_DC_CRT_CONTROL_ENABLE;
  860. ctrl |= SM501_DC_CRT_CONTROL_TE;
  861. } else {
  862. ctrl &= ~SM501_DC_CRT_CONTROL_SEL;
  863. ctrl &= ~SM501_DC_CRT_CONTROL_ENABLE;
  864. ctrl &= ~SM501_DC_CRT_CONTROL_TE;
  865. }
  866. writel(ctrl, info->regs + SM501_DC_CRT_CONTROL);
  867. sm501fb_sync_regs(info);
  868. return len;
  869. }
  870. /* Prepare the device_attr for registration with sysfs later */
  871. static DEVICE_ATTR(crt_src, 0666, sm501fb_crtsrc_show, sm501fb_crtsrc_store);
  872. /* sm501fb_show_regs
  873. *
  874. * show the primary sm501 registers
  875. */
  876. static int sm501fb_show_regs(struct sm501fb_info *info, char *ptr,
  877. unsigned int start, unsigned int len)
  878. {
  879. void __iomem *mem = info->regs;
  880. char *buf = ptr;
  881. unsigned int reg;
  882. for (reg = start; reg < (len + start); reg += 4)
  883. ptr += sprintf(ptr, "%08x = %08x\n", reg, readl(mem + reg));
  884. return ptr - buf;
  885. }
  886. /* sm501fb_debug_show_crt
  887. *
  888. * show the crt control and cursor registers
  889. */
  890. static ssize_t sm501fb_debug_show_crt(struct device *dev,
  891. struct device_attribute *attr, char *buf)
  892. {
  893. struct sm501fb_info *info = dev_get_drvdata(dev);
  894. char *ptr = buf;
  895. ptr += sm501fb_show_regs(info, ptr, SM501_DC_CRT_CONTROL, 0x40);
  896. ptr += sm501fb_show_regs(info, ptr, SM501_DC_CRT_HWC_BASE, 0x10);
  897. return ptr - buf;
  898. }
  899. static DEVICE_ATTR(fbregs_crt, 0444, sm501fb_debug_show_crt, NULL);
  900. /* sm501fb_debug_show_pnl
  901. *
  902. * show the panel control and cursor registers
  903. */
  904. static ssize_t sm501fb_debug_show_pnl(struct device *dev,
  905. struct device_attribute *attr, char *buf)
  906. {
  907. struct sm501fb_info *info = dev_get_drvdata(dev);
  908. char *ptr = buf;
  909. ptr += sm501fb_show_regs(info, ptr, 0x0, 0x40);
  910. ptr += sm501fb_show_regs(info, ptr, SM501_DC_PANEL_HWC_BASE, 0x10);
  911. return ptr - buf;
  912. }
  913. static DEVICE_ATTR(fbregs_pnl, 0444, sm501fb_debug_show_pnl, NULL);
  914. /* framebuffer ops */
  915. static struct fb_ops sm501fb_ops_crt = {
  916. .owner = THIS_MODULE,
  917. .fb_check_var = sm501fb_check_var_crt,
  918. .fb_set_par = sm501fb_set_par_crt,
  919. .fb_blank = sm501fb_blank_crt,
  920. .fb_setcolreg = sm501fb_setcolreg,
  921. .fb_pan_display = sm501fb_pan_crt,
  922. .fb_cursor = sm501fb_cursor,
  923. .fb_fillrect = cfb_fillrect,
  924. .fb_copyarea = cfb_copyarea,
  925. .fb_imageblit = cfb_imageblit,
  926. };
  927. static struct fb_ops sm501fb_ops_pnl = {
  928. .owner = THIS_MODULE,
  929. .fb_check_var = sm501fb_check_var_pnl,
  930. .fb_set_par = sm501fb_set_par_pnl,
  931. .fb_pan_display = sm501fb_pan_pnl,
  932. .fb_blank = sm501fb_blank_pnl,
  933. .fb_setcolreg = sm501fb_setcolreg,
  934. .fb_cursor = sm501fb_cursor,
  935. .fb_fillrect = cfb_fillrect,
  936. .fb_copyarea = cfb_copyarea,
  937. .fb_imageblit = cfb_imageblit,
  938. };
  939. /* sm501fb_info_alloc
  940. *
  941. * creates and initialises an sm501fb_info structure
  942. */
  943. static struct sm501fb_info *sm501fb_info_alloc(struct fb_info *fbinfo_crt,
  944. struct fb_info *fbinfo_pnl)
  945. {
  946. struct sm501fb_info *info;
  947. struct sm501fb_par *par;
  948. info = kzalloc(sizeof(struct sm501fb_info), GFP_KERNEL);
  949. if (info) {
  950. /* set the references back */
  951. par = fbinfo_crt->par;
  952. par->info = info;
  953. par->head = HEAD_CRT;
  954. fbinfo_crt->pseudo_palette = &par->pseudo_palette;
  955. par = fbinfo_pnl->par;
  956. par->info = info;
  957. par->head = HEAD_PANEL;
  958. fbinfo_pnl->pseudo_palette = &par->pseudo_palette;
  959. /* store the two fbs into our info */
  960. info->fb[HEAD_CRT] = fbinfo_crt;
  961. info->fb[HEAD_PANEL] = fbinfo_pnl;
  962. }
  963. return info;
  964. }
  965. /* sm501_init_cursor
  966. *
  967. * initialise hw cursor parameters
  968. */
  969. static int sm501_init_cursor(struct fb_info *fbi, unsigned int reg_base)
  970. {
  971. struct sm501fb_par *par = fbi->par;
  972. struct sm501fb_info *info = par->info;
  973. int ret;
  974. par->cursor_regs = info->regs + reg_base;
  975. ret = sm501_alloc_mem(info, &par->cursor, SM501_MEMF_CURSOR, 1024);
  976. if (ret < 0)
  977. return ret;
  978. /* initialise the colour registers */
  979. writel(par->cursor.sm_addr, par->cursor_regs + SM501_OFF_HWC_ADDR);
  980. writel(0x00, par->cursor_regs + SM501_OFF_HWC_LOC);
  981. writel(0x00, par->cursor_regs + SM501_OFF_HWC_COLOR_1_2);
  982. writel(0x00, par->cursor_regs + SM501_OFF_HWC_COLOR_3);
  983. sm501fb_sync_regs(info);
  984. return 0;
  985. }
  986. /* sm501fb_info_start
  987. *
  988. * fills the par structure claiming resources and remapping etc.
  989. */
  990. static int sm501fb_start(struct sm501fb_info *info,
  991. struct platform_device *pdev)
  992. {
  993. struct resource *res;
  994. struct device *dev;
  995. int k;
  996. int ret;
  997. info->dev = dev = &pdev->dev;
  998. platform_set_drvdata(pdev, info);
  999. info->irq = ret = platform_get_irq(pdev, 0);
  1000. if (ret < 0) {
  1001. /* we currently do not use the IRQ */
  1002. dev_warn(dev, "no irq for device\n");
  1003. }
  1004. /* allocate, reserve and remap resources for registers */
  1005. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1006. if (res == NULL) {
  1007. dev_err(dev, "no resource definition for registers\n");
  1008. ret = -ENOENT;
  1009. goto err_release;
  1010. }
  1011. info->regs_res = request_mem_region(res->start,
  1012. res->end - res->start,
  1013. pdev->name);
  1014. if (info->regs_res == NULL) {
  1015. dev_err(dev, "cannot claim registers\n");
  1016. ret = -ENXIO;
  1017. goto err_release;
  1018. }
  1019. info->regs = ioremap(res->start, (res->end - res->start)+1);
  1020. if (info->regs == NULL) {
  1021. dev_err(dev, "cannot remap registers\n");
  1022. ret = -ENXIO;
  1023. goto err_regs_res;
  1024. }
  1025. /* allocate, reserve resources for framebuffer */
  1026. res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  1027. if (res == NULL) {
  1028. dev_err(dev, "no memory resource defined\n");
  1029. ret = -ENXIO;
  1030. goto err_regs_map;
  1031. }
  1032. info->fbmem_res = request_mem_region(res->start,
  1033. (res->end - res->start)+1,
  1034. pdev->name);
  1035. if (info->fbmem_res == NULL) {
  1036. dev_err(dev, "cannot claim framebuffer\n");
  1037. ret = -ENXIO;
  1038. goto err_regs_map;
  1039. }
  1040. info->fbmem = ioremap(res->start, (res->end - res->start)+1);
  1041. if (info->fbmem == NULL) {
  1042. dev_err(dev, "cannot remap framebuffer\n");
  1043. goto err_mem_res;
  1044. }
  1045. info->fbmem_len = (res->end - res->start)+1;
  1046. /* clear framebuffer memory - avoids garbage data on unused fb */
  1047. memset(info->fbmem, 0, info->fbmem_len);
  1048. /* clear palette ram - undefined at power on */
  1049. for (k = 0; k < (256 * 3); k++)
  1050. writel(0, info->regs + SM501_DC_PANEL_PALETTE + (k * 4));
  1051. /* enable display controller */
  1052. sm501_unit_power(dev->parent, SM501_GATE_DISPLAY, 1);
  1053. /* setup cursors */
  1054. sm501_init_cursor(info->fb[HEAD_CRT], SM501_DC_CRT_HWC_ADDR);
  1055. sm501_init_cursor(info->fb[HEAD_PANEL], SM501_DC_PANEL_HWC_ADDR);
  1056. return 0; /* everything is setup */
  1057. err_mem_res:
  1058. release_resource(info->fbmem_res);
  1059. kfree(info->fbmem_res);
  1060. err_regs_map:
  1061. iounmap(info->regs);
  1062. err_regs_res:
  1063. release_resource(info->regs_res);
  1064. kfree(info->regs_res);
  1065. err_release:
  1066. return ret;
  1067. }
  1068. static void sm501fb_stop(struct sm501fb_info *info)
  1069. {
  1070. /* disable display controller */
  1071. sm501_unit_power(info->dev->parent, SM501_GATE_DISPLAY, 0);
  1072. iounmap(info->fbmem);
  1073. release_resource(info->fbmem_res);
  1074. kfree(info->fbmem_res);
  1075. iounmap(info->regs);
  1076. release_resource(info->regs_res);
  1077. kfree(info->regs_res);
  1078. }
  1079. static void sm501fb_info_release(struct sm501fb_info *info)
  1080. {
  1081. kfree(info);
  1082. }
  1083. static int sm501fb_init_fb(struct fb_info *fb,
  1084. enum sm501_controller head,
  1085. const char *fbname)
  1086. {
  1087. struct sm501_platdata_fbsub *pd;
  1088. struct sm501fb_par *par = fb->par;
  1089. struct sm501fb_info *info = par->info;
  1090. unsigned long ctrl;
  1091. unsigned int enable;
  1092. int ret;
  1093. switch (head) {
  1094. case HEAD_CRT:
  1095. pd = info->pdata->fb_crt;
  1096. ctrl = readl(info->regs + SM501_DC_CRT_CONTROL);
  1097. enable = (ctrl & SM501_DC_CRT_CONTROL_ENABLE) ? 1 : 0;
  1098. /* ensure we set the correct source register */
  1099. if (info->pdata->fb_route != SM501_FB_CRT_PANEL) {
  1100. ctrl |= SM501_DC_CRT_CONTROL_SEL;
  1101. writel(ctrl, info->regs + SM501_DC_CRT_CONTROL);
  1102. }
  1103. break;
  1104. case HEAD_PANEL:
  1105. pd = info->pdata->fb_pnl;
  1106. ctrl = readl(info->regs + SM501_DC_PANEL_CONTROL);
  1107. enable = (ctrl & SM501_DC_PANEL_CONTROL_EN) ? 1 : 0;
  1108. break;
  1109. default:
  1110. pd = NULL; /* stop compiler warnings */
  1111. ctrl = 0;
  1112. enable = 0;
  1113. BUG();
  1114. }
  1115. dev_info(info->dev, "fb %s %sabled at start\n",
  1116. fbname, enable ? "en" : "dis");
  1117. /* check to see if our routing allows this */
  1118. if (head == HEAD_CRT && info->pdata->fb_route == SM501_FB_CRT_PANEL) {
  1119. ctrl &= ~SM501_DC_CRT_CONTROL_SEL;
  1120. writel(ctrl, info->regs + SM501_DC_CRT_CONTROL);
  1121. enable = 0;
  1122. }
  1123. strlcpy(fb->fix.id, fbname, sizeof(fb->fix.id));
  1124. memcpy(&par->ops,
  1125. (head == HEAD_CRT) ? &sm501fb_ops_crt : &sm501fb_ops_pnl,
  1126. sizeof(struct fb_ops));
  1127. /* update ops dependant on what we've been passed */
  1128. if ((pd->flags & SM501FB_FLAG_USE_HWCURSOR) == 0)
  1129. par->ops.fb_cursor = NULL;
  1130. fb->fbops = &par->ops;
  1131. fb->flags = FBINFO_FLAG_DEFAULT |
  1132. FBINFO_HWACCEL_XPAN | FBINFO_HWACCEL_YPAN;
  1133. /* fixed data */
  1134. fb->fix.type = FB_TYPE_PACKED_PIXELS;
  1135. fb->fix.type_aux = 0;
  1136. fb->fix.xpanstep = 1;
  1137. fb->fix.ypanstep = 1;
  1138. fb->fix.ywrapstep = 0;
  1139. fb->fix.accel = FB_ACCEL_NONE;
  1140. /* screenmode */
  1141. fb->var.nonstd = 0;
  1142. fb->var.activate = FB_ACTIVATE_NOW;
  1143. fb->var.accel_flags = 0;
  1144. fb->var.vmode = FB_VMODE_NONINTERLACED;
  1145. fb->var.bits_per_pixel = 16;
  1146. if (enable && (pd->flags & SM501FB_FLAG_USE_INIT_MODE) && 0) {
  1147. /* TODO read the mode from the current display */
  1148. } else {
  1149. if (pd->def_mode) {
  1150. dev_info(info->dev, "using supplied mode\n");
  1151. fb_videomode_to_var(&fb->var, pd->def_mode);
  1152. fb->var.bits_per_pixel = pd->def_bpp ? pd->def_bpp : 8;
  1153. fb->var.xres_virtual = fb->var.xres;
  1154. fb->var.yres_virtual = fb->var.yres;
  1155. } else {
  1156. ret = fb_find_mode(&fb->var, fb,
  1157. NULL, NULL, 0, NULL, 8);
  1158. if (ret == 0 || ret == 4) {
  1159. dev_err(info->dev,
  1160. "failed to get initial mode\n");
  1161. return -EINVAL;
  1162. }
  1163. }
  1164. }
  1165. /* initialise and set the palette */
  1166. fb_alloc_cmap(&fb->cmap, NR_PALETTE, 0);
  1167. fb_set_cmap(&fb->cmap, fb);
  1168. ret = (fb->fbops->fb_check_var)(&fb->var, fb);
  1169. if (ret)
  1170. dev_err(info->dev, "check_var() failed on initial setup?\n");
  1171. /* ensure we've activated our new configuration */
  1172. (fb->fbops->fb_set_par)(fb);
  1173. return 0;
  1174. }
  1175. /* default platform data if none is supplied (ie, PCI device) */
  1176. static struct sm501_platdata_fbsub sm501fb_pdata_crt = {
  1177. .flags = (SM501FB_FLAG_USE_INIT_MODE |
  1178. SM501FB_FLAG_USE_HWCURSOR |
  1179. SM501FB_FLAG_USE_HWACCEL |
  1180. SM501FB_FLAG_DISABLE_AT_EXIT),
  1181. };
  1182. static struct sm501_platdata_fbsub sm501fb_pdata_pnl = {
  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_fb sm501fb_def_pdata = {
  1189. .fb_route = SM501_FB_OWN,
  1190. .fb_crt = &sm501fb_pdata_crt,
  1191. .fb_pnl = &sm501fb_pdata_pnl,
  1192. };
  1193. static char driver_name_crt[] = "sm501fb-crt";
  1194. static char driver_name_pnl[] = "sm501fb-panel";
  1195. static int __init sm501fb_probe(struct platform_device *pdev)
  1196. {
  1197. struct sm501fb_info *info;
  1198. struct device *dev = &pdev->dev;
  1199. struct fb_info *fbinfo_crt;
  1200. struct fb_info *fbinfo_pnl;
  1201. int ret;
  1202. /* allocate our framebuffers */
  1203. fbinfo_crt = framebuffer_alloc(sizeof(struct sm501fb_par), dev);
  1204. if (fbinfo_crt == NULL) {
  1205. dev_err(dev, "cannot allocate crt framebuffer\n");
  1206. return -ENOMEM;
  1207. }
  1208. fbinfo_pnl = framebuffer_alloc(sizeof(struct sm501fb_par), dev);
  1209. if (fbinfo_pnl == NULL) {
  1210. dev_err(dev, "cannot allocate panel framebuffer\n");
  1211. ret = -ENOMEM;
  1212. goto fbinfo_crt_alloc_fail;
  1213. }
  1214. info = sm501fb_info_alloc(fbinfo_crt, fbinfo_pnl);
  1215. if (info == NULL) {
  1216. dev_err(dev, "cannot allocate par\n");
  1217. ret = -ENOMEM;
  1218. goto sm501fb_alloc_fail;
  1219. }
  1220. if (dev->parent->platform_data) {
  1221. struct sm501_platdata *pd = dev->parent->platform_data;
  1222. info->pdata = pd->fb;
  1223. }
  1224. if (info->pdata == NULL) {
  1225. dev_info(dev, "using default configuration data\n");
  1226. info->pdata = &sm501fb_def_pdata;
  1227. }
  1228. /* start the framebuffers */
  1229. ret = sm501fb_start(info, pdev);
  1230. if (ret) {
  1231. dev_err(dev, "cannot initialise SM501\n");
  1232. goto sm501fb_start_fail;
  1233. }
  1234. /* CRT framebuffer setup */
  1235. ret = sm501fb_init_fb(fbinfo_crt, HEAD_CRT, driver_name_crt);
  1236. if (ret) {
  1237. dev_err(dev, "cannot initialise CRT fb\n");
  1238. goto sm501fb_start_fail;
  1239. }
  1240. /* Panel framebuffer setup */
  1241. ret = sm501fb_init_fb(fbinfo_pnl, HEAD_PANEL, driver_name_pnl);
  1242. if (ret) {
  1243. dev_err(dev, "cannot initialise Panel fb\n");
  1244. goto sm501fb_start_fail;
  1245. }
  1246. /* register framebuffers */
  1247. ret = register_framebuffer(fbinfo_crt);
  1248. if (ret < 0) {
  1249. dev_err(dev, "failed to register CRT fb (%d)\n", ret);
  1250. goto register_crt_fail;
  1251. }
  1252. ret = register_framebuffer(fbinfo_pnl);
  1253. if (ret < 0) {
  1254. dev_err(dev, "failed to register panel fb (%d)\n", ret);
  1255. goto register_pnl_fail;
  1256. }
  1257. dev_info(dev, "fb%d: %s frame buffer device\n",
  1258. fbinfo_crt->node, fbinfo_crt->fix.id);
  1259. dev_info(dev, "fb%d: %s frame buffer device\n",
  1260. fbinfo_pnl->node, fbinfo_pnl->fix.id);
  1261. /* create device files */
  1262. ret = device_create_file(dev, &dev_attr_crt_src);
  1263. if (ret)
  1264. goto crtsrc_fail;
  1265. ret = device_create_file(dev, &dev_attr_fbregs_pnl);
  1266. if (ret)
  1267. goto fbregs_pnl_fail;
  1268. ret = device_create_file(dev, &dev_attr_fbregs_crt);
  1269. if (ret)
  1270. goto fbregs_crt_fail;
  1271. /* we registered, return ok */
  1272. return 0;
  1273. fbregs_crt_fail:
  1274. device_remove_file(dev, &dev_attr_fbregs_pnl);
  1275. fbregs_pnl_fail:
  1276. device_remove_file(dev, &dev_attr_crt_src);
  1277. crtsrc_fail:
  1278. unregister_framebuffer(fbinfo_pnl);
  1279. register_pnl_fail:
  1280. unregister_framebuffer(fbinfo_crt);
  1281. register_crt_fail:
  1282. sm501fb_stop(info);
  1283. sm501fb_start_fail:
  1284. sm501fb_info_release(info);
  1285. sm501fb_alloc_fail:
  1286. framebuffer_release(fbinfo_pnl);
  1287. fbinfo_crt_alloc_fail:
  1288. framebuffer_release(fbinfo_crt);
  1289. return ret;
  1290. }
  1291. /*
  1292. * Cleanup
  1293. */
  1294. static int sm501fb_remove(struct platform_device *pdev)
  1295. {
  1296. struct sm501fb_info *info = platform_get_drvdata(pdev);
  1297. struct fb_info *fbinfo_crt = info->fb[0];
  1298. struct fb_info *fbinfo_pnl = info->fb[1];
  1299. device_remove_file(&pdev->dev, &dev_attr_fbregs_crt);
  1300. device_remove_file(&pdev->dev, &dev_attr_fbregs_pnl);
  1301. device_remove_file(&pdev->dev, &dev_attr_crt_src);
  1302. unregister_framebuffer(fbinfo_crt);
  1303. unregister_framebuffer(fbinfo_pnl);
  1304. sm501fb_stop(info);
  1305. sm501fb_info_release(info);
  1306. framebuffer_release(fbinfo_pnl);
  1307. framebuffer_release(fbinfo_crt);
  1308. return 0;
  1309. }
  1310. #ifdef CONFIG_PM
  1311. static int sm501fb_suspend_fb(struct sm501fb_info *info,
  1312. enum sm501_controller head)
  1313. {
  1314. struct fb_info *fbi = info->fb[head];
  1315. struct sm501fb_par *par = fbi->par;
  1316. if (par->screen.size == 0)
  1317. return 0;
  1318. /* blank the relevant interface to ensure unit power minimised */
  1319. (par->ops.fb_blank)(FB_BLANK_POWERDOWN, fbi);
  1320. /* tell console/fb driver we are suspending */
  1321. acquire_console_sem();
  1322. fb_set_suspend(fbi, 1);
  1323. release_console_sem();
  1324. /* backup copies in case chip is powered down over suspend */
  1325. par->store_fb = vmalloc(par->screen.size);
  1326. if (par->store_fb == NULL) {
  1327. dev_err(info->dev, "no memory to store screen\n");
  1328. return -ENOMEM;
  1329. }
  1330. par->store_cursor = vmalloc(par->cursor.size);
  1331. if (par->store_cursor == NULL) {
  1332. dev_err(info->dev, "no memory to store cursor\n");
  1333. goto err_nocursor;
  1334. }
  1335. dev_dbg(info->dev, "suspending screen to %p\n", par->store_fb);
  1336. dev_dbg(info->dev, "suspending cursor to %p\n", par->store_cursor);
  1337. memcpy_fromio(par->store_fb, par->screen.k_addr, par->screen.size);
  1338. memcpy_fromio(par->store_cursor, par->cursor.k_addr, par->cursor.size);
  1339. return 0;
  1340. err_nocursor:
  1341. vfree(par->store_fb);
  1342. par->store_fb = NULL;
  1343. return -ENOMEM;
  1344. }
  1345. static void sm501fb_resume_fb(struct sm501fb_info *info,
  1346. enum sm501_controller head)
  1347. {
  1348. struct fb_info *fbi = info->fb[head];
  1349. struct sm501fb_par *par = fbi->par;
  1350. if (par->screen.size == 0)
  1351. return;
  1352. /* re-activate the configuration */
  1353. (par->ops.fb_set_par)(fbi);
  1354. /* restore the data */
  1355. dev_dbg(info->dev, "restoring screen from %p\n", par->store_fb);
  1356. dev_dbg(info->dev, "restoring cursor from %p\n", par->store_cursor);
  1357. if (par->store_fb)
  1358. memcpy_toio(par->screen.k_addr, par->store_fb,
  1359. par->screen.size);
  1360. if (par->store_cursor)
  1361. memcpy_toio(par->cursor.k_addr, par->store_cursor,
  1362. par->cursor.size);
  1363. acquire_console_sem();
  1364. fb_set_suspend(fbi, 0);
  1365. release_console_sem();
  1366. vfree(par->store_fb);
  1367. vfree(par->store_cursor);
  1368. }
  1369. /* suspend and resume support */
  1370. static int sm501fb_suspend(struct platform_device *pdev, pm_message_t state)
  1371. {
  1372. struct sm501fb_info *info = platform_get_drvdata(pdev);
  1373. /* store crt control to resume with */
  1374. info->pm_crt_ctrl = readl(info->regs + SM501_DC_CRT_CONTROL);
  1375. sm501fb_suspend_fb(info, HEAD_CRT);
  1376. sm501fb_suspend_fb(info, HEAD_PANEL);
  1377. /* turn off the clocks, in case the device is not powered down */
  1378. sm501_unit_power(info->dev->parent, SM501_GATE_DISPLAY, 0);
  1379. return 0;
  1380. }
  1381. #define SM501_CRT_CTRL_SAVE (SM501_DC_CRT_CONTROL_TVP | \
  1382. SM501_DC_CRT_CONTROL_SEL)
  1383. static int sm501fb_resume(struct platform_device *pdev)
  1384. {
  1385. struct sm501fb_info *info = platform_get_drvdata(pdev);
  1386. unsigned long crt_ctrl;
  1387. sm501_unit_power(info->dev->parent, SM501_GATE_DISPLAY, 1);
  1388. /* restore the items we want to be saved for crt control */
  1389. crt_ctrl = readl(info->regs + SM501_DC_CRT_CONTROL);
  1390. crt_ctrl &= ~SM501_CRT_CTRL_SAVE;
  1391. crt_ctrl |= info->pm_crt_ctrl & SM501_CRT_CTRL_SAVE;
  1392. writel(crt_ctrl, info->regs + SM501_DC_CRT_CONTROL);
  1393. sm501fb_resume_fb(info, HEAD_CRT);
  1394. sm501fb_resume_fb(info, HEAD_PANEL);
  1395. return 0;
  1396. }
  1397. #else
  1398. #define sm501fb_suspend NULL
  1399. #define sm501fb_resume NULL
  1400. #endif
  1401. static struct platform_driver sm501fb_driver = {
  1402. .probe = sm501fb_probe,
  1403. .remove = sm501fb_remove,
  1404. .suspend = sm501fb_suspend,
  1405. .resume = sm501fb_resume,
  1406. .driver = {
  1407. .name = "sm501-fb",
  1408. .owner = THIS_MODULE,
  1409. },
  1410. };
  1411. static int __devinit sm501fb_init(void)
  1412. {
  1413. return platform_driver_register(&sm501fb_driver);
  1414. }
  1415. static void __exit sm501fb_cleanup(void)
  1416. {
  1417. platform_driver_unregister(&sm501fb_driver);
  1418. }
  1419. module_init(sm501fb_init);
  1420. module_exit(sm501fb_cleanup);
  1421. MODULE_AUTHOR("Ben Dooks, Vincent Sanders");
  1422. MODULE_DESCRIPTION("SM501 Framebuffer driver");
  1423. MODULE_LICENSE("GPL v2");