sm501fb.c 50 KB

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