sm501fb.c 42 KB

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