sm501fb.c 53 KB

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