s3c-fb.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716
  1. /* linux/drivers/video/s3c-fb.c
  2. *
  3. * Copyright 2008 Openmoko Inc.
  4. * Copyright 2008-2010 Simtec Electronics
  5. * Ben Dooks <ben@simtec.co.uk>
  6. * http://armlinux.simtec.co.uk/
  7. *
  8. * Samsung SoC Framebuffer driver
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software FoundatIon.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/slab.h>
  19. #include <linux/init.h>
  20. #include <linux/clk.h>
  21. #include <linux/fb.h>
  22. #include <linux/io.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/interrupt.h>
  25. #include <mach/map.h>
  26. #include <plat/regs-fb-v4.h>
  27. #include <plat/fb.h>
  28. /* This driver will export a number of framebuffer interfaces depending
  29. * on the configuration passed in via the platform data. Each fb instance
  30. * maps to a hardware window. Currently there is no support for runtime
  31. * setting of the alpha-blending functions that each window has, so only
  32. * window 0 is actually useful.
  33. *
  34. * Window 0 is treated specially, it is used for the basis of the LCD
  35. * output timings and as the control for the output power-down state.
  36. */
  37. /* note, the previous use of <mach/regs-fb.h> to get platform specific data
  38. * has been replaced by using the platform device name to pick the correct
  39. * configuration data for the system.
  40. */
  41. #ifdef CONFIG_FB_S3C_DEBUG_REGWRITE
  42. #undef writel
  43. #define writel(v, r) do { \
  44. printk(KERN_DEBUG "%s: %08x => %p\n", __func__, (unsigned int)v, r); \
  45. __raw_writel(v, r); } while(0)
  46. #endif /* FB_S3C_DEBUG_REGWRITE */
  47. /* irq_flags bits */
  48. #define S3C_FB_VSYNC_IRQ_EN 0
  49. #define VSYNC_TIMEOUT_MSEC 50
  50. struct s3c_fb;
  51. #define VALID_BPP(x) (1 << ((x) - 1))
  52. #define OSD_BASE(win, variant) ((variant).osd + ((win) * (variant).osd_stride))
  53. #define VIDOSD_A(win, variant) (OSD_BASE(win, variant) + 0x00)
  54. #define VIDOSD_B(win, variant) (OSD_BASE(win, variant) + 0x04)
  55. #define VIDOSD_C(win, variant) (OSD_BASE(win, variant) + 0x08)
  56. #define VIDOSD_D(win, variant) (OSD_BASE(win, variant) + 0x0C)
  57. /**
  58. * struct s3c_fb_variant - fb variant information
  59. * @is_2443: Set if S3C2443/S3C2416 style hardware.
  60. * @nr_windows: The number of windows.
  61. * @vidtcon: The base for the VIDTCONx registers
  62. * @wincon: The base for the WINxCON registers.
  63. * @winmap: The base for the WINxMAP registers.
  64. * @keycon: The abse for the WxKEYCON registers.
  65. * @buf_start: Offset of buffer start registers.
  66. * @buf_size: Offset of buffer size registers.
  67. * @buf_end: Offset of buffer end registers.
  68. * @osd: The base for the OSD registers.
  69. * @palette: Address of palette memory, or 0 if none.
  70. * @has_prtcon: Set if has PRTCON register.
  71. * @has_shadowcon: Set if has SHADOWCON register.
  72. */
  73. struct s3c_fb_variant {
  74. unsigned int is_2443:1;
  75. unsigned short nr_windows;
  76. unsigned short vidtcon;
  77. unsigned short wincon;
  78. unsigned short winmap;
  79. unsigned short keycon;
  80. unsigned short buf_start;
  81. unsigned short buf_end;
  82. unsigned short buf_size;
  83. unsigned short osd;
  84. unsigned short osd_stride;
  85. unsigned short palette[S3C_FB_MAX_WIN];
  86. unsigned int has_prtcon:1;
  87. unsigned int has_shadowcon:1;
  88. };
  89. /**
  90. * struct s3c_fb_win_variant
  91. * @has_osd_c: Set if has OSD C register.
  92. * @has_osd_d: Set if has OSD D register.
  93. * @has_osd_alpha: Set if can change alpha transparency for a window.
  94. * @palette_sz: Size of palette in entries.
  95. * @palette_16bpp: Set if palette is 16bits wide.
  96. * @osd_size_off: If != 0, supports setting up OSD for a window; the appropriate
  97. * register is located at the given offset from OSD_BASE.
  98. * @valid_bpp: 1 bit per BPP setting to show valid bits-per-pixel.
  99. *
  100. * valid_bpp bit x is set if (x+1)BPP is supported.
  101. */
  102. struct s3c_fb_win_variant {
  103. unsigned int has_osd_c:1;
  104. unsigned int has_osd_d:1;
  105. unsigned int has_osd_alpha:1;
  106. unsigned int palette_16bpp:1;
  107. unsigned short osd_size_off;
  108. unsigned short palette_sz;
  109. u32 valid_bpp;
  110. };
  111. /**
  112. * struct s3c_fb_driverdata - per-device type driver data for init time.
  113. * @variant: The variant information for this driver.
  114. * @win: The window information for each window.
  115. */
  116. struct s3c_fb_driverdata {
  117. struct s3c_fb_variant variant;
  118. struct s3c_fb_win_variant *win[S3C_FB_MAX_WIN];
  119. };
  120. /**
  121. * struct s3c_fb_palette - palette information
  122. * @r: Red bitfield.
  123. * @g: Green bitfield.
  124. * @b: Blue bitfield.
  125. * @a: Alpha bitfield.
  126. */
  127. struct s3c_fb_palette {
  128. struct fb_bitfield r;
  129. struct fb_bitfield g;
  130. struct fb_bitfield b;
  131. struct fb_bitfield a;
  132. };
  133. /**
  134. * struct s3c_fb_win - per window private data for each framebuffer.
  135. * @windata: The platform data supplied for the window configuration.
  136. * @parent: The hardware that this window is part of.
  137. * @fbinfo: Pointer pack to the framebuffer info for this window.
  138. * @varint: The variant information for this window.
  139. * @palette_buffer: Buffer/cache to hold palette entries.
  140. * @pseudo_palette: For use in TRUECOLOUR modes for entries 0..15/
  141. * @index: The window number of this window.
  142. * @palette: The bitfields for changing r/g/b into a hardware palette entry.
  143. */
  144. struct s3c_fb_win {
  145. struct s3c_fb_pd_win *windata;
  146. struct s3c_fb *parent;
  147. struct fb_info *fbinfo;
  148. struct s3c_fb_palette palette;
  149. struct s3c_fb_win_variant variant;
  150. u32 *palette_buffer;
  151. u32 pseudo_palette[16];
  152. unsigned int index;
  153. };
  154. /**
  155. * struct s3c_fb_vsync - vsync information
  156. * @wait: a queue for processes waiting for vsync
  157. * @count: vsync interrupt count
  158. */
  159. struct s3c_fb_vsync {
  160. wait_queue_head_t wait;
  161. unsigned int count;
  162. };
  163. /**
  164. * struct s3c_fb - overall hardware state of the hardware
  165. * @dev: The device that we bound to, for printing, etc.
  166. * @regs_res: The resource we claimed for the IO registers.
  167. * @bus_clk: The clk (hclk) feeding our interface and possibly pixclk.
  168. * @regs: The mapped hardware registers.
  169. * @variant: Variant information for this hardware.
  170. * @enabled: A bitmask of enabled hardware windows.
  171. * @pdata: The platform configuration data passed with the device.
  172. * @windows: The hardware windows that have been claimed.
  173. * @irq_no: IRQ line number
  174. * @irq_flags: irq flags
  175. * @vsync_info: VSYNC-related information (count, queues...)
  176. */
  177. struct s3c_fb {
  178. struct device *dev;
  179. struct resource *regs_res;
  180. struct clk *bus_clk;
  181. void __iomem *regs;
  182. struct s3c_fb_variant variant;
  183. unsigned char enabled;
  184. struct s3c_fb_platdata *pdata;
  185. struct s3c_fb_win *windows[S3C_FB_MAX_WIN];
  186. int irq_no;
  187. unsigned long irq_flags;
  188. struct s3c_fb_vsync vsync_info;
  189. };
  190. /**
  191. * s3c_fb_validate_win_bpp - validate the bits-per-pixel for this mode.
  192. * @win: The device window.
  193. * @bpp: The bit depth.
  194. */
  195. static bool s3c_fb_validate_win_bpp(struct s3c_fb_win *win, unsigned int bpp)
  196. {
  197. return win->variant.valid_bpp & VALID_BPP(bpp);
  198. }
  199. /**
  200. * s3c_fb_check_var() - framebuffer layer request to verify a given mode.
  201. * @var: The screen information to verify.
  202. * @info: The framebuffer device.
  203. *
  204. * Framebuffer layer call to verify the given information and allow us to
  205. * update various information depending on the hardware capabilities.
  206. */
  207. static int s3c_fb_check_var(struct fb_var_screeninfo *var,
  208. struct fb_info *info)
  209. {
  210. struct s3c_fb_win *win = info->par;
  211. struct s3c_fb_pd_win *windata = win->windata;
  212. struct s3c_fb *sfb = win->parent;
  213. dev_dbg(sfb->dev, "checking parameters\n");
  214. var->xres_virtual = max((unsigned int)windata->virtual_x, var->xres);
  215. var->yres_virtual = max((unsigned int)windata->virtual_y, var->yres);
  216. if (!s3c_fb_validate_win_bpp(win, var->bits_per_pixel)) {
  217. dev_dbg(sfb->dev, "win %d: unsupported bpp %d\n",
  218. win->index, var->bits_per_pixel);
  219. return -EINVAL;
  220. }
  221. /* always ensure these are zero, for drop through cases below */
  222. var->transp.offset = 0;
  223. var->transp.length = 0;
  224. switch (var->bits_per_pixel) {
  225. case 1:
  226. case 2:
  227. case 4:
  228. case 8:
  229. if (sfb->variant.palette[win->index] != 0) {
  230. /* non palletised, A:1,R:2,G:3,B:2 mode */
  231. var->red.offset = 4;
  232. var->green.offset = 2;
  233. var->blue.offset = 0;
  234. var->red.length = 5;
  235. var->green.length = 3;
  236. var->blue.length = 2;
  237. var->transp.offset = 7;
  238. var->transp.length = 1;
  239. } else {
  240. var->red.offset = 0;
  241. var->red.length = var->bits_per_pixel;
  242. var->green = var->red;
  243. var->blue = var->red;
  244. }
  245. break;
  246. case 19:
  247. /* 666 with one bit alpha/transparency */
  248. var->transp.offset = 18;
  249. var->transp.length = 1;
  250. case 18:
  251. var->bits_per_pixel = 32;
  252. /* 666 format */
  253. var->red.offset = 12;
  254. var->green.offset = 6;
  255. var->blue.offset = 0;
  256. var->red.length = 6;
  257. var->green.length = 6;
  258. var->blue.length = 6;
  259. break;
  260. case 16:
  261. /* 16 bpp, 565 format */
  262. var->red.offset = 11;
  263. var->green.offset = 5;
  264. var->blue.offset = 0;
  265. var->red.length = 5;
  266. var->green.length = 6;
  267. var->blue.length = 5;
  268. break;
  269. case 28:
  270. case 25:
  271. var->transp.length = var->bits_per_pixel - 24;
  272. var->transp.offset = 24;
  273. /* drop through */
  274. case 24:
  275. /* our 24bpp is unpacked, so 32bpp */
  276. var->bits_per_pixel = 32;
  277. case 32:
  278. var->red.offset = 16;
  279. var->red.length = 8;
  280. var->green.offset = 8;
  281. var->green.length = 8;
  282. var->blue.offset = 0;
  283. var->blue.length = 8;
  284. break;
  285. default:
  286. dev_err(sfb->dev, "invalid bpp\n");
  287. }
  288. dev_dbg(sfb->dev, "%s: verified parameters\n", __func__);
  289. return 0;
  290. }
  291. /**
  292. * s3c_fb_calc_pixclk() - calculate the divider to create the pixel clock.
  293. * @sfb: The hardware state.
  294. * @pixclock: The pixel clock wanted, in picoseconds.
  295. *
  296. * Given the specified pixel clock, work out the necessary divider to get
  297. * close to the output frequency.
  298. */
  299. static int s3c_fb_calc_pixclk(struct s3c_fb *sfb, unsigned int pixclk)
  300. {
  301. unsigned long clk = clk_get_rate(sfb->bus_clk);
  302. unsigned long long tmp;
  303. unsigned int result;
  304. tmp = (unsigned long long)clk;
  305. tmp *= pixclk;
  306. do_div(tmp, 1000000000UL);
  307. result = (unsigned int)tmp / 1000;
  308. dev_dbg(sfb->dev, "pixclk=%u, clk=%lu, div=%d (%lu)\n",
  309. pixclk, clk, result, clk / result);
  310. return result;
  311. }
  312. /**
  313. * s3c_fb_align_word() - align pixel count to word boundary
  314. * @bpp: The number of bits per pixel
  315. * @pix: The value to be aligned.
  316. *
  317. * Align the given pixel count so that it will start on an 32bit word
  318. * boundary.
  319. */
  320. static int s3c_fb_align_word(unsigned int bpp, unsigned int pix)
  321. {
  322. int pix_per_word;
  323. if (bpp > 16)
  324. return pix;
  325. pix_per_word = (8 * 32) / bpp;
  326. return ALIGN(pix, pix_per_word);
  327. }
  328. /**
  329. * vidosd_set_size() - set OSD size for a window
  330. *
  331. * @win: the window to set OSD size for
  332. * @size: OSD size register value
  333. */
  334. static void vidosd_set_size(struct s3c_fb_win *win, u32 size)
  335. {
  336. struct s3c_fb *sfb = win->parent;
  337. /* OSD can be set up if osd_size_off != 0 for this window */
  338. if (win->variant.osd_size_off)
  339. writel(size, sfb->regs + OSD_BASE(win->index, sfb->variant)
  340. + win->variant.osd_size_off);
  341. }
  342. /**
  343. * vidosd_set_alpha() - set alpha transparency for a window
  344. *
  345. * @win: the window to set OSD size for
  346. * @alpha: alpha register value
  347. */
  348. static void vidosd_set_alpha(struct s3c_fb_win *win, u32 alpha)
  349. {
  350. struct s3c_fb *sfb = win->parent;
  351. if (win->variant.has_osd_alpha)
  352. writel(alpha, sfb->regs + VIDOSD_C(win->index, sfb->variant));
  353. }
  354. /**
  355. * shadow_protect_win() - disable updating values from shadow registers at vsync
  356. *
  357. * @win: window to protect registers for
  358. * @protect: 1 to protect (disable updates)
  359. */
  360. static void shadow_protect_win(struct s3c_fb_win *win, bool protect)
  361. {
  362. struct s3c_fb *sfb = win->parent;
  363. u32 reg;
  364. if (protect) {
  365. if (sfb->variant.has_prtcon) {
  366. writel(PRTCON_PROTECT, sfb->regs + PRTCON);
  367. } else if (sfb->variant.has_shadowcon) {
  368. reg = readl(sfb->regs + SHADOWCON);
  369. writel(reg | SHADOWCON_WINx_PROTECT(win->index),
  370. sfb->regs + SHADOWCON);
  371. }
  372. } else {
  373. if (sfb->variant.has_prtcon) {
  374. writel(0, sfb->regs + PRTCON);
  375. } else if (sfb->variant.has_shadowcon) {
  376. reg = readl(sfb->regs + SHADOWCON);
  377. writel(reg & ~SHADOWCON_WINx_PROTECT(win->index),
  378. sfb->regs + SHADOWCON);
  379. }
  380. }
  381. }
  382. /**
  383. * s3c_fb_set_par() - framebuffer request to set new framebuffer state.
  384. * @info: The framebuffer to change.
  385. *
  386. * Framebuffer layer request to set a new mode for the specified framebuffer
  387. */
  388. static int s3c_fb_set_par(struct fb_info *info)
  389. {
  390. struct fb_var_screeninfo *var = &info->var;
  391. struct s3c_fb_win *win = info->par;
  392. struct s3c_fb *sfb = win->parent;
  393. void __iomem *regs = sfb->regs;
  394. void __iomem *buf = regs;
  395. int win_no = win->index;
  396. u32 alpha = 0;
  397. u32 data;
  398. u32 pagewidth;
  399. int clkdiv;
  400. dev_dbg(sfb->dev, "setting framebuffer parameters\n");
  401. shadow_protect_win(win, 1);
  402. switch (var->bits_per_pixel) {
  403. case 32:
  404. case 24:
  405. case 16:
  406. case 12:
  407. info->fix.visual = FB_VISUAL_TRUECOLOR;
  408. break;
  409. case 8:
  410. if (win->variant.palette_sz >= 256)
  411. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  412. else
  413. info->fix.visual = FB_VISUAL_TRUECOLOR;
  414. break;
  415. case 1:
  416. info->fix.visual = FB_VISUAL_MONO01;
  417. break;
  418. default:
  419. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  420. break;
  421. }
  422. info->fix.line_length = (var->xres_virtual * var->bits_per_pixel) / 8;
  423. info->fix.xpanstep = info->var.xres_virtual > info->var.xres ? 1 : 0;
  424. info->fix.ypanstep = info->var.yres_virtual > info->var.yres ? 1 : 0;
  425. /* disable the window whilst we update it */
  426. writel(0, regs + WINCON(win_no));
  427. /* use platform specified window as the basis for the lcd timings */
  428. if (win_no == sfb->pdata->default_win) {
  429. clkdiv = s3c_fb_calc_pixclk(sfb, var->pixclock);
  430. data = sfb->pdata->vidcon0;
  431. data &= ~(VIDCON0_CLKVAL_F_MASK | VIDCON0_CLKDIR);
  432. if (clkdiv > 1)
  433. data |= VIDCON0_CLKVAL_F(clkdiv-1) | VIDCON0_CLKDIR;
  434. else
  435. data &= ~VIDCON0_CLKDIR; /* 1:1 clock */
  436. /* write the timing data to the panel */
  437. if (sfb->variant.is_2443)
  438. data |= (1 << 5);
  439. data |= VIDCON0_ENVID | VIDCON0_ENVID_F;
  440. writel(data, regs + VIDCON0);
  441. data = VIDTCON0_VBPD(var->upper_margin - 1) |
  442. VIDTCON0_VFPD(var->lower_margin - 1) |
  443. VIDTCON0_VSPW(var->vsync_len - 1);
  444. writel(data, regs + sfb->variant.vidtcon);
  445. data = VIDTCON1_HBPD(var->left_margin - 1) |
  446. VIDTCON1_HFPD(var->right_margin - 1) |
  447. VIDTCON1_HSPW(var->hsync_len - 1);
  448. /* VIDTCON1 */
  449. writel(data, regs + sfb->variant.vidtcon + 4);
  450. data = VIDTCON2_LINEVAL(var->yres - 1) |
  451. VIDTCON2_HOZVAL(var->xres - 1);
  452. writel(data, regs +sfb->variant.vidtcon + 8 );
  453. }
  454. /* write the buffer address */
  455. /* start and end registers stride is 8 */
  456. buf = regs + win_no * 8;
  457. writel(info->fix.smem_start, buf + sfb->variant.buf_start);
  458. data = info->fix.smem_start + info->fix.line_length * var->yres;
  459. writel(data, buf + sfb->variant.buf_end);
  460. pagewidth = (var->xres * var->bits_per_pixel) >> 3;
  461. data = VIDW_BUF_SIZE_OFFSET(info->fix.line_length - pagewidth) |
  462. VIDW_BUF_SIZE_PAGEWIDTH(pagewidth);
  463. writel(data, regs + sfb->variant.buf_size + (win_no * 4));
  464. /* write 'OSD' registers to control position of framebuffer */
  465. data = VIDOSDxA_TOPLEFT_X(0) | VIDOSDxA_TOPLEFT_Y(0);
  466. writel(data, regs + VIDOSD_A(win_no, sfb->variant));
  467. data = VIDOSDxB_BOTRIGHT_X(s3c_fb_align_word(var->bits_per_pixel,
  468. var->xres - 1)) |
  469. VIDOSDxB_BOTRIGHT_Y(var->yres - 1);
  470. writel(data, regs + VIDOSD_B(win_no, sfb->variant));
  471. data = var->xres * var->yres;
  472. alpha = VIDISD14C_ALPHA1_R(0xf) |
  473. VIDISD14C_ALPHA1_G(0xf) |
  474. VIDISD14C_ALPHA1_B(0xf);
  475. vidosd_set_alpha(win, alpha);
  476. vidosd_set_size(win, data);
  477. data = WINCONx_ENWIN;
  478. /* note, since we have to round up the bits-per-pixel, we end up
  479. * relying on the bitfield information for r/g/b/a to work out
  480. * exactly which mode of operation is intended. */
  481. switch (var->bits_per_pixel) {
  482. case 1:
  483. data |= WINCON0_BPPMODE_1BPP;
  484. data |= WINCONx_BITSWP;
  485. data |= WINCONx_BURSTLEN_4WORD;
  486. break;
  487. case 2:
  488. data |= WINCON0_BPPMODE_2BPP;
  489. data |= WINCONx_BITSWP;
  490. data |= WINCONx_BURSTLEN_8WORD;
  491. break;
  492. case 4:
  493. data |= WINCON0_BPPMODE_4BPP;
  494. data |= WINCONx_BITSWP;
  495. data |= WINCONx_BURSTLEN_8WORD;
  496. break;
  497. case 8:
  498. if (var->transp.length != 0)
  499. data |= WINCON1_BPPMODE_8BPP_1232;
  500. else
  501. data |= WINCON0_BPPMODE_8BPP_PALETTE;
  502. data |= WINCONx_BURSTLEN_8WORD;
  503. data |= WINCONx_BYTSWP;
  504. break;
  505. case 16:
  506. if (var->transp.length != 0)
  507. data |= WINCON1_BPPMODE_16BPP_A1555;
  508. else
  509. data |= WINCON0_BPPMODE_16BPP_565;
  510. data |= WINCONx_HAWSWP;
  511. data |= WINCONx_BURSTLEN_16WORD;
  512. break;
  513. case 24:
  514. case 32:
  515. if (var->red.length == 6) {
  516. if (var->transp.length != 0)
  517. data |= WINCON1_BPPMODE_19BPP_A1666;
  518. else
  519. data |= WINCON1_BPPMODE_18BPP_666;
  520. } else if (var->transp.length == 1)
  521. data |= WINCON1_BPPMODE_25BPP_A1888
  522. | WINCON1_BLD_PIX;
  523. else if (var->transp.length == 4)
  524. data |= WINCON1_BPPMODE_28BPP_A4888
  525. | WINCON1_BLD_PIX | WINCON1_ALPHA_SEL;
  526. else
  527. data |= WINCON0_BPPMODE_24BPP_888;
  528. data |= WINCONx_WSWP;
  529. data |= WINCONx_BURSTLEN_16WORD;
  530. break;
  531. }
  532. /* Enable the colour keying for the window below this one */
  533. if (win_no > 0) {
  534. u32 keycon0_data = 0, keycon1_data = 0;
  535. void __iomem *keycon = regs + sfb->variant.keycon;
  536. keycon0_data = ~(WxKEYCON0_KEYBL_EN |
  537. WxKEYCON0_KEYEN_F |
  538. WxKEYCON0_DIRCON) | WxKEYCON0_COMPKEY(0);
  539. keycon1_data = WxKEYCON1_COLVAL(0xffffff);
  540. keycon += (win_no - 1) * 8;
  541. writel(keycon0_data, keycon + WKEYCON0);
  542. writel(keycon1_data, keycon + WKEYCON1);
  543. }
  544. writel(data, regs + sfb->variant.wincon + (win_no * 4));
  545. writel(0x0, regs + sfb->variant.winmap + (win_no * 4));
  546. /* Enable DMA channel for this window */
  547. if (sfb->variant.has_shadowcon) {
  548. data = readl(sfb->regs + SHADOWCON);
  549. data |= SHADOWCON_CHx_ENABLE(win_no);
  550. writel(data, sfb->regs + SHADOWCON);
  551. }
  552. shadow_protect_win(win, 0);
  553. return 0;
  554. }
  555. /**
  556. * s3c_fb_update_palette() - set or schedule a palette update.
  557. * @sfb: The hardware information.
  558. * @win: The window being updated.
  559. * @reg: The palette index being changed.
  560. * @value: The computed palette value.
  561. *
  562. * Change the value of a palette register, either by directly writing to
  563. * the palette (this requires the palette RAM to be disconnected from the
  564. * hardware whilst this is in progress) or schedule the update for later.
  565. *
  566. * At the moment, since we have no VSYNC interrupt support, we simply set
  567. * the palette entry directly.
  568. */
  569. static void s3c_fb_update_palette(struct s3c_fb *sfb,
  570. struct s3c_fb_win *win,
  571. unsigned int reg,
  572. u32 value)
  573. {
  574. void __iomem *palreg;
  575. u32 palcon;
  576. palreg = sfb->regs + sfb->variant.palette[win->index];
  577. dev_dbg(sfb->dev, "%s: win %d, reg %d (%p): %08x\n",
  578. __func__, win->index, reg, palreg, value);
  579. win->palette_buffer[reg] = value;
  580. palcon = readl(sfb->regs + WPALCON);
  581. writel(palcon | WPALCON_PAL_UPDATE, sfb->regs + WPALCON);
  582. if (win->variant.palette_16bpp)
  583. writew(value, palreg + (reg * 2));
  584. else
  585. writel(value, palreg + (reg * 4));
  586. writel(palcon, sfb->regs + WPALCON);
  587. }
  588. static inline unsigned int chan_to_field(unsigned int chan,
  589. struct fb_bitfield *bf)
  590. {
  591. chan &= 0xffff;
  592. chan >>= 16 - bf->length;
  593. return chan << bf->offset;
  594. }
  595. /**
  596. * s3c_fb_setcolreg() - framebuffer layer request to change palette.
  597. * @regno: The palette index to change.
  598. * @red: The red field for the palette data.
  599. * @green: The green field for the palette data.
  600. * @blue: The blue field for the palette data.
  601. * @trans: The transparency (alpha) field for the palette data.
  602. * @info: The framebuffer being changed.
  603. */
  604. static int s3c_fb_setcolreg(unsigned regno,
  605. unsigned red, unsigned green, unsigned blue,
  606. unsigned transp, struct fb_info *info)
  607. {
  608. struct s3c_fb_win *win = info->par;
  609. struct s3c_fb *sfb = win->parent;
  610. unsigned int val;
  611. dev_dbg(sfb->dev, "%s: win %d: %d => rgb=%d/%d/%d\n",
  612. __func__, win->index, regno, red, green, blue);
  613. switch (info->fix.visual) {
  614. case FB_VISUAL_TRUECOLOR:
  615. /* true-colour, use pseudo-palette */
  616. if (regno < 16) {
  617. u32 *pal = info->pseudo_palette;
  618. val = chan_to_field(red, &info->var.red);
  619. val |= chan_to_field(green, &info->var.green);
  620. val |= chan_to_field(blue, &info->var.blue);
  621. pal[regno] = val;
  622. }
  623. break;
  624. case FB_VISUAL_PSEUDOCOLOR:
  625. if (regno < win->variant.palette_sz) {
  626. val = chan_to_field(red, &win->palette.r);
  627. val |= chan_to_field(green, &win->palette.g);
  628. val |= chan_to_field(blue, &win->palette.b);
  629. s3c_fb_update_palette(sfb, win, regno, val);
  630. }
  631. break;
  632. default:
  633. return 1; /* unknown type */
  634. }
  635. return 0;
  636. }
  637. /**
  638. * s3c_fb_enable() - Set the state of the main LCD output
  639. * @sfb: The main framebuffer state.
  640. * @enable: The state to set.
  641. */
  642. static void s3c_fb_enable(struct s3c_fb *sfb, int enable)
  643. {
  644. u32 vidcon0 = readl(sfb->regs + VIDCON0);
  645. if (enable)
  646. vidcon0 |= VIDCON0_ENVID | VIDCON0_ENVID_F;
  647. else {
  648. /* see the note in the framebuffer datasheet about
  649. * why you cannot take both of these bits down at the
  650. * same time. */
  651. if (!(vidcon0 & VIDCON0_ENVID))
  652. return;
  653. vidcon0 |= VIDCON0_ENVID;
  654. vidcon0 &= ~VIDCON0_ENVID_F;
  655. }
  656. writel(vidcon0, sfb->regs + VIDCON0);
  657. }
  658. /**
  659. * s3c_fb_blank() - blank or unblank the given window
  660. * @blank_mode: The blank state from FB_BLANK_*
  661. * @info: The framebuffer to blank.
  662. *
  663. * Framebuffer layer request to change the power state.
  664. */
  665. static int s3c_fb_blank(int blank_mode, struct fb_info *info)
  666. {
  667. struct s3c_fb_win *win = info->par;
  668. struct s3c_fb *sfb = win->parent;
  669. unsigned int index = win->index;
  670. u32 wincon;
  671. dev_dbg(sfb->dev, "blank mode %d\n", blank_mode);
  672. wincon = readl(sfb->regs + sfb->variant.wincon + (index * 4));
  673. switch (blank_mode) {
  674. case FB_BLANK_POWERDOWN:
  675. wincon &= ~WINCONx_ENWIN;
  676. sfb->enabled &= ~(1 << index);
  677. /* fall through to FB_BLANK_NORMAL */
  678. case FB_BLANK_NORMAL:
  679. /* disable the DMA and display 0x0 (black) */
  680. writel(WINxMAP_MAP | WINxMAP_MAP_COLOUR(0x0),
  681. sfb->regs + sfb->variant.winmap + (index * 4));
  682. break;
  683. case FB_BLANK_UNBLANK:
  684. writel(0x0, sfb->regs + sfb->variant.winmap + (index * 4));
  685. wincon |= WINCONx_ENWIN;
  686. sfb->enabled |= (1 << index);
  687. break;
  688. case FB_BLANK_VSYNC_SUSPEND:
  689. case FB_BLANK_HSYNC_SUSPEND:
  690. default:
  691. return 1;
  692. }
  693. writel(wincon, sfb->regs + sfb->variant.wincon + (index * 4));
  694. /* Check the enabled state to see if we need to be running the
  695. * main LCD interface, as if there are no active windows then
  696. * it is highly likely that we also do not need to output
  697. * anything.
  698. */
  699. /* We could do something like the following code, but the current
  700. * system of using framebuffer events means that we cannot make
  701. * the distinction between just window 0 being inactive and all
  702. * the windows being down.
  703. *
  704. * s3c_fb_enable(sfb, sfb->enabled ? 1 : 0);
  705. */
  706. /* we're stuck with this until we can do something about overriding
  707. * the power control using the blanking event for a single fb.
  708. */
  709. if (index == sfb->pdata->default_win)
  710. s3c_fb_enable(sfb, blank_mode != FB_BLANK_POWERDOWN ? 1 : 0);
  711. return 0;
  712. }
  713. /**
  714. * s3c_fb_pan_display() - Pan the display.
  715. *
  716. * Note that the offsets can be written to the device at any time, as their
  717. * values are latched at each vsync automatically. This also means that only
  718. * the last call to this function will have any effect on next vsync, but
  719. * there is no need to sleep waiting for it to prevent tearing.
  720. *
  721. * @var: The screen information to verify.
  722. * @info: The framebuffer device.
  723. */
  724. static int s3c_fb_pan_display(struct fb_var_screeninfo *var,
  725. struct fb_info *info)
  726. {
  727. struct s3c_fb_win *win = info->par;
  728. struct s3c_fb *sfb = win->parent;
  729. void __iomem *buf = sfb->regs + win->index * 8;
  730. unsigned int start_boff, end_boff;
  731. /* Offset in bytes to the start of the displayed area */
  732. start_boff = var->yoffset * info->fix.line_length;
  733. /* X offset depends on the current bpp */
  734. if (info->var.bits_per_pixel >= 8) {
  735. start_boff += var->xoffset * (info->var.bits_per_pixel >> 3);
  736. } else {
  737. switch (info->var.bits_per_pixel) {
  738. case 4:
  739. start_boff += var->xoffset >> 1;
  740. break;
  741. case 2:
  742. start_boff += var->xoffset >> 2;
  743. break;
  744. case 1:
  745. start_boff += var->xoffset >> 3;
  746. break;
  747. default:
  748. dev_err(sfb->dev, "invalid bpp\n");
  749. return -EINVAL;
  750. }
  751. }
  752. /* Offset in bytes to the end of the displayed area */
  753. end_boff = start_boff + var->yres * info->fix.line_length;
  754. /* Temporarily turn off per-vsync update from shadow registers until
  755. * both start and end addresses are updated to prevent corruption */
  756. shadow_protect_win(win, 1);
  757. writel(info->fix.smem_start + start_boff, buf + sfb->variant.buf_start);
  758. writel(info->fix.smem_start + end_boff, buf + sfb->variant.buf_end);
  759. shadow_protect_win(win, 0);
  760. return 0;
  761. }
  762. /**
  763. * s3c_fb_enable_irq() - enable framebuffer interrupts
  764. * @sfb: main hardware state
  765. */
  766. static void s3c_fb_enable_irq(struct s3c_fb *sfb)
  767. {
  768. void __iomem *regs = sfb->regs;
  769. u32 irq_ctrl_reg;
  770. if (!test_and_set_bit(S3C_FB_VSYNC_IRQ_EN, &sfb->irq_flags)) {
  771. /* IRQ disabled, enable it */
  772. irq_ctrl_reg = readl(regs + VIDINTCON0);
  773. irq_ctrl_reg |= VIDINTCON0_INT_ENABLE;
  774. irq_ctrl_reg |= VIDINTCON0_INT_FRAME;
  775. irq_ctrl_reg &= ~VIDINTCON0_FRAMESEL0_MASK;
  776. irq_ctrl_reg |= VIDINTCON0_FRAMESEL0_VSYNC;
  777. irq_ctrl_reg &= ~VIDINTCON0_FRAMESEL1_MASK;
  778. irq_ctrl_reg |= VIDINTCON0_FRAMESEL1_NONE;
  779. writel(irq_ctrl_reg, regs + VIDINTCON0);
  780. }
  781. }
  782. /**
  783. * s3c_fb_disable_irq() - disable framebuffer interrupts
  784. * @sfb: main hardware state
  785. */
  786. static void s3c_fb_disable_irq(struct s3c_fb *sfb)
  787. {
  788. void __iomem *regs = sfb->regs;
  789. u32 irq_ctrl_reg;
  790. if (test_and_clear_bit(S3C_FB_VSYNC_IRQ_EN, &sfb->irq_flags)) {
  791. /* IRQ enabled, disable it */
  792. irq_ctrl_reg = readl(regs + VIDINTCON0);
  793. irq_ctrl_reg &= ~VIDINTCON0_INT_FRAME;
  794. irq_ctrl_reg &= ~VIDINTCON0_INT_ENABLE;
  795. writel(irq_ctrl_reg, regs + VIDINTCON0);
  796. }
  797. }
  798. static irqreturn_t s3c_fb_irq(int irq, void *dev_id)
  799. {
  800. struct s3c_fb *sfb = dev_id;
  801. void __iomem *regs = sfb->regs;
  802. u32 irq_sts_reg;
  803. irq_sts_reg = readl(regs + VIDINTCON1);
  804. if (irq_sts_reg & VIDINTCON1_INT_FRAME) {
  805. /* VSYNC interrupt, accept it */
  806. writel(VIDINTCON1_INT_FRAME, regs + VIDINTCON1);
  807. sfb->vsync_info.count++;
  808. wake_up_interruptible(&sfb->vsync_info.wait);
  809. }
  810. /* We only support waiting for VSYNC for now, so it's safe
  811. * to always disable irqs here.
  812. */
  813. s3c_fb_disable_irq(sfb);
  814. return IRQ_HANDLED;
  815. }
  816. /**
  817. * s3c_fb_wait_for_vsync() - sleep until next VSYNC interrupt or timeout
  818. * @sfb: main hardware state
  819. * @crtc: head index.
  820. */
  821. static int s3c_fb_wait_for_vsync(struct s3c_fb *sfb, u32 crtc)
  822. {
  823. unsigned long count;
  824. int ret;
  825. if (crtc != 0)
  826. return -ENODEV;
  827. count = sfb->vsync_info.count;
  828. s3c_fb_enable_irq(sfb);
  829. ret = wait_event_interruptible_timeout(sfb->vsync_info.wait,
  830. count != sfb->vsync_info.count,
  831. msecs_to_jiffies(VSYNC_TIMEOUT_MSEC));
  832. if (ret == 0)
  833. return -ETIMEDOUT;
  834. return 0;
  835. }
  836. static int s3c_fb_ioctl(struct fb_info *info, unsigned int cmd,
  837. unsigned long arg)
  838. {
  839. struct s3c_fb_win *win = info->par;
  840. struct s3c_fb *sfb = win->parent;
  841. int ret;
  842. u32 crtc;
  843. switch (cmd) {
  844. case FBIO_WAITFORVSYNC:
  845. if (get_user(crtc, (u32 __user *)arg)) {
  846. ret = -EFAULT;
  847. break;
  848. }
  849. ret = s3c_fb_wait_for_vsync(sfb, crtc);
  850. break;
  851. default:
  852. ret = -ENOTTY;
  853. }
  854. return ret;
  855. }
  856. static struct fb_ops s3c_fb_ops = {
  857. .owner = THIS_MODULE,
  858. .fb_check_var = s3c_fb_check_var,
  859. .fb_set_par = s3c_fb_set_par,
  860. .fb_blank = s3c_fb_blank,
  861. .fb_setcolreg = s3c_fb_setcolreg,
  862. .fb_fillrect = cfb_fillrect,
  863. .fb_copyarea = cfb_copyarea,
  864. .fb_imageblit = cfb_imageblit,
  865. .fb_pan_display = s3c_fb_pan_display,
  866. .fb_ioctl = s3c_fb_ioctl,
  867. };
  868. /**
  869. * s3c_fb_alloc_memory() - allocate display memory for framebuffer window
  870. * @sfb: The base resources for the hardware.
  871. * @win: The window to initialise memory for.
  872. *
  873. * Allocate memory for the given framebuffer.
  874. */
  875. static int __devinit s3c_fb_alloc_memory(struct s3c_fb *sfb,
  876. struct s3c_fb_win *win)
  877. {
  878. struct s3c_fb_pd_win *windata = win->windata;
  879. unsigned int real_size, virt_size, size;
  880. struct fb_info *fbi = win->fbinfo;
  881. dma_addr_t map_dma;
  882. dev_dbg(sfb->dev, "allocating memory for display\n");
  883. real_size = windata->win_mode.xres * windata->win_mode.yres;
  884. virt_size = windata->virtual_x * windata->virtual_y;
  885. dev_dbg(sfb->dev, "real_size=%u (%u.%u), virt_size=%u (%u.%u)\n",
  886. real_size, windata->win_mode.xres, windata->win_mode.yres,
  887. virt_size, windata->virtual_x, windata->virtual_y);
  888. size = (real_size > virt_size) ? real_size : virt_size;
  889. size *= (windata->max_bpp > 16) ? 32 : windata->max_bpp;
  890. size /= 8;
  891. fbi->fix.smem_len = size;
  892. size = PAGE_ALIGN(size);
  893. dev_dbg(sfb->dev, "want %u bytes for window\n", size);
  894. fbi->screen_base = dma_alloc_writecombine(sfb->dev, size,
  895. &map_dma, GFP_KERNEL);
  896. if (!fbi->screen_base)
  897. return -ENOMEM;
  898. dev_dbg(sfb->dev, "mapped %x to %p\n",
  899. (unsigned int)map_dma, fbi->screen_base);
  900. memset(fbi->screen_base, 0x0, size);
  901. fbi->fix.smem_start = map_dma;
  902. return 0;
  903. }
  904. /**
  905. * s3c_fb_free_memory() - free the display memory for the given window
  906. * @sfb: The base resources for the hardware.
  907. * @win: The window to free the display memory for.
  908. *
  909. * Free the display memory allocated by s3c_fb_alloc_memory().
  910. */
  911. static void s3c_fb_free_memory(struct s3c_fb *sfb, struct s3c_fb_win *win)
  912. {
  913. struct fb_info *fbi = win->fbinfo;
  914. if (fbi->screen_base)
  915. dma_free_writecombine(sfb->dev, PAGE_ALIGN(fbi->fix.smem_len),
  916. fbi->screen_base, fbi->fix.smem_start);
  917. }
  918. /**
  919. * s3c_fb_release_win() - release resources for a framebuffer window.
  920. * @win: The window to cleanup the resources for.
  921. *
  922. * Release the resources that where claimed for the hardware window,
  923. * such as the framebuffer instance and any memory claimed for it.
  924. */
  925. static void s3c_fb_release_win(struct s3c_fb *sfb, struct s3c_fb_win *win)
  926. {
  927. u32 data;
  928. if (win->fbinfo) {
  929. if (sfb->variant.has_shadowcon) {
  930. data = readl(sfb->regs + SHADOWCON);
  931. data &= ~SHADOWCON_CHx_ENABLE(win->index);
  932. data &= ~SHADOWCON_CHx_LOCAL_ENABLE(win->index);
  933. writel(data, sfb->regs + SHADOWCON);
  934. }
  935. unregister_framebuffer(win->fbinfo);
  936. if (win->fbinfo->cmap.len)
  937. fb_dealloc_cmap(&win->fbinfo->cmap);
  938. s3c_fb_free_memory(sfb, win);
  939. framebuffer_release(win->fbinfo);
  940. }
  941. }
  942. /**
  943. * s3c_fb_probe_win() - register an hardware window
  944. * @sfb: The base resources for the hardware
  945. * @variant: The variant information for this window.
  946. * @res: Pointer to where to place the resultant window.
  947. *
  948. * Allocate and do the basic initialisation for one of the hardware's graphics
  949. * windows.
  950. */
  951. static int __devinit s3c_fb_probe_win(struct s3c_fb *sfb, unsigned int win_no,
  952. struct s3c_fb_win_variant *variant,
  953. struct s3c_fb_win **res)
  954. {
  955. struct fb_var_screeninfo *var;
  956. struct fb_videomode *initmode;
  957. struct s3c_fb_pd_win *windata;
  958. struct s3c_fb_win *win;
  959. struct fb_info *fbinfo;
  960. int palette_size;
  961. int ret;
  962. dev_dbg(sfb->dev, "probing window %d, variant %p\n", win_no, variant);
  963. init_waitqueue_head(&sfb->vsync_info.wait);
  964. palette_size = variant->palette_sz * 4;
  965. fbinfo = framebuffer_alloc(sizeof(struct s3c_fb_win) +
  966. palette_size * sizeof(u32), sfb->dev);
  967. if (!fbinfo) {
  968. dev_err(sfb->dev, "failed to allocate framebuffer\n");
  969. return -ENOENT;
  970. }
  971. windata = sfb->pdata->win[win_no];
  972. initmode = &windata->win_mode;
  973. WARN_ON(windata->max_bpp == 0);
  974. WARN_ON(windata->win_mode.xres == 0);
  975. WARN_ON(windata->win_mode.yres == 0);
  976. win = fbinfo->par;
  977. *res = win;
  978. var = &fbinfo->var;
  979. win->variant = *variant;
  980. win->fbinfo = fbinfo;
  981. win->parent = sfb;
  982. win->windata = windata;
  983. win->index = win_no;
  984. win->palette_buffer = (u32 *)(win + 1);
  985. ret = s3c_fb_alloc_memory(sfb, win);
  986. if (ret) {
  987. dev_err(sfb->dev, "failed to allocate display memory\n");
  988. return ret;
  989. }
  990. /* setup the r/b/g positions for the window's palette */
  991. if (win->variant.palette_16bpp) {
  992. /* Set RGB 5:6:5 as default */
  993. win->palette.r.offset = 11;
  994. win->palette.r.length = 5;
  995. win->palette.g.offset = 5;
  996. win->palette.g.length = 6;
  997. win->palette.b.offset = 0;
  998. win->palette.b.length = 5;
  999. } else {
  1000. /* Set 8bpp or 8bpp and 1bit alpha */
  1001. win->palette.r.offset = 16;
  1002. win->palette.r.length = 8;
  1003. win->palette.g.offset = 8;
  1004. win->palette.g.length = 8;
  1005. win->palette.b.offset = 0;
  1006. win->palette.b.length = 8;
  1007. }
  1008. /* setup the initial video mode from the window */
  1009. fb_videomode_to_var(&fbinfo->var, initmode);
  1010. fbinfo->fix.type = FB_TYPE_PACKED_PIXELS;
  1011. fbinfo->fix.accel = FB_ACCEL_NONE;
  1012. fbinfo->var.activate = FB_ACTIVATE_NOW;
  1013. fbinfo->var.vmode = FB_VMODE_NONINTERLACED;
  1014. fbinfo->var.bits_per_pixel = windata->default_bpp;
  1015. fbinfo->fbops = &s3c_fb_ops;
  1016. fbinfo->flags = FBINFO_FLAG_DEFAULT;
  1017. fbinfo->pseudo_palette = &win->pseudo_palette;
  1018. /* prepare to actually start the framebuffer */
  1019. ret = s3c_fb_check_var(&fbinfo->var, fbinfo);
  1020. if (ret < 0) {
  1021. dev_err(sfb->dev, "check_var failed on initial video params\n");
  1022. return ret;
  1023. }
  1024. /* create initial colour map */
  1025. ret = fb_alloc_cmap(&fbinfo->cmap, win->variant.palette_sz, 1);
  1026. if (ret == 0)
  1027. fb_set_cmap(&fbinfo->cmap, fbinfo);
  1028. else
  1029. dev_err(sfb->dev, "failed to allocate fb cmap\n");
  1030. s3c_fb_set_par(fbinfo);
  1031. dev_dbg(sfb->dev, "about to register framebuffer\n");
  1032. /* run the check_var and set_par on our configuration. */
  1033. ret = register_framebuffer(fbinfo);
  1034. if (ret < 0) {
  1035. dev_err(sfb->dev, "failed to register framebuffer\n");
  1036. return ret;
  1037. }
  1038. dev_info(sfb->dev, "window %d: fb %s\n", win_no, fbinfo->fix.id);
  1039. return 0;
  1040. }
  1041. /**
  1042. * s3c_fb_clear_win() - clear hardware window registers.
  1043. * @sfb: The base resources for the hardware.
  1044. * @win: The window to process.
  1045. *
  1046. * Reset the specific window registers to a known state.
  1047. */
  1048. static void s3c_fb_clear_win(struct s3c_fb *sfb, int win)
  1049. {
  1050. void __iomem *regs = sfb->regs;
  1051. u32 reg;
  1052. writel(0, regs + sfb->variant.wincon + (win * 4));
  1053. writel(0, regs + VIDOSD_A(win, sfb->variant));
  1054. writel(0, regs + VIDOSD_B(win, sfb->variant));
  1055. writel(0, regs + VIDOSD_C(win, sfb->variant));
  1056. reg = readl(regs + SHADOWCON);
  1057. writel(reg & ~SHADOWCON_WINx_PROTECT(win), regs + SHADOWCON);
  1058. }
  1059. static int __devinit s3c_fb_probe(struct platform_device *pdev)
  1060. {
  1061. struct s3c_fb_driverdata *fbdrv;
  1062. struct device *dev = &pdev->dev;
  1063. struct s3c_fb_platdata *pd;
  1064. struct s3c_fb *sfb;
  1065. struct resource *res;
  1066. int win;
  1067. int ret = 0;
  1068. fbdrv = (struct s3c_fb_driverdata *)platform_get_device_id(pdev)->driver_data;
  1069. if (fbdrv->variant.nr_windows > S3C_FB_MAX_WIN) {
  1070. dev_err(dev, "too many windows, cannot attach\n");
  1071. return -EINVAL;
  1072. }
  1073. pd = pdev->dev.platform_data;
  1074. if (!pd) {
  1075. dev_err(dev, "no platform data specified\n");
  1076. return -EINVAL;
  1077. }
  1078. sfb = kzalloc(sizeof(struct s3c_fb), GFP_KERNEL);
  1079. if (!sfb) {
  1080. dev_err(dev, "no memory for framebuffers\n");
  1081. return -ENOMEM;
  1082. }
  1083. dev_dbg(dev, "allocate new framebuffer %p\n", sfb);
  1084. sfb->dev = dev;
  1085. sfb->pdata = pd;
  1086. sfb->variant = fbdrv->variant;
  1087. sfb->bus_clk = clk_get(dev, "lcd");
  1088. if (IS_ERR(sfb->bus_clk)) {
  1089. dev_err(dev, "failed to get bus clock\n");
  1090. goto err_sfb;
  1091. }
  1092. clk_enable(sfb->bus_clk);
  1093. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1094. if (!res) {
  1095. dev_err(dev, "failed to find registers\n");
  1096. ret = -ENOENT;
  1097. goto err_clk;
  1098. }
  1099. sfb->regs_res = request_mem_region(res->start, resource_size(res),
  1100. dev_name(dev));
  1101. if (!sfb->regs_res) {
  1102. dev_err(dev, "failed to claim register region\n");
  1103. ret = -ENOENT;
  1104. goto err_clk;
  1105. }
  1106. sfb->regs = ioremap(res->start, resource_size(res));
  1107. if (!sfb->regs) {
  1108. dev_err(dev, "failed to map registers\n");
  1109. ret = -ENXIO;
  1110. goto err_req_region;
  1111. }
  1112. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1113. if (!res) {
  1114. dev_err(dev, "failed to acquire irq resource\n");
  1115. ret = -ENOENT;
  1116. goto err_ioremap;
  1117. }
  1118. sfb->irq_no = res->start;
  1119. ret = request_irq(sfb->irq_no, s3c_fb_irq,
  1120. 0, "s3c_fb", sfb);
  1121. if (ret) {
  1122. dev_err(dev, "irq request failed\n");
  1123. goto err_ioremap;
  1124. }
  1125. dev_dbg(dev, "got resources (regs %p), probing windows\n", sfb->regs);
  1126. /* setup gpio and output polarity controls */
  1127. pd->setup_gpio();
  1128. writel(pd->vidcon1, sfb->regs + VIDCON1);
  1129. /* zero all windows before we do anything */
  1130. for (win = 0; win < fbdrv->variant.nr_windows; win++)
  1131. s3c_fb_clear_win(sfb, win);
  1132. /* initialise colour key controls */
  1133. for (win = 0; win < (fbdrv->variant.nr_windows - 1); win++) {
  1134. void __iomem *regs = sfb->regs + sfb->variant.keycon;
  1135. regs += (win * 8);
  1136. writel(0xffffff, regs + WKEYCON0);
  1137. writel(0xffffff, regs + WKEYCON1);
  1138. }
  1139. /* we have the register setup, start allocating framebuffers */
  1140. for (win = 0; win < fbdrv->variant.nr_windows; win++) {
  1141. if (!pd->win[win])
  1142. continue;
  1143. ret = s3c_fb_probe_win(sfb, win, fbdrv->win[win],
  1144. &sfb->windows[win]);
  1145. if (ret < 0) {
  1146. dev_err(dev, "failed to create window %d\n", win);
  1147. for (; win >= 0; win--)
  1148. s3c_fb_release_win(sfb, sfb->windows[win]);
  1149. goto err_irq;
  1150. }
  1151. }
  1152. platform_set_drvdata(pdev, sfb);
  1153. return 0;
  1154. err_irq:
  1155. free_irq(sfb->irq_no, sfb);
  1156. err_ioremap:
  1157. iounmap(sfb->regs);
  1158. err_req_region:
  1159. release_resource(sfb->regs_res);
  1160. kfree(sfb->regs_res);
  1161. err_clk:
  1162. clk_disable(sfb->bus_clk);
  1163. clk_put(sfb->bus_clk);
  1164. err_sfb:
  1165. kfree(sfb);
  1166. return ret;
  1167. }
  1168. /**
  1169. * s3c_fb_remove() - Cleanup on module finalisation
  1170. * @pdev: The platform device we are bound to.
  1171. *
  1172. * Shutdown and then release all the resources that the driver allocated
  1173. * on initialisation.
  1174. */
  1175. static int __devexit s3c_fb_remove(struct platform_device *pdev)
  1176. {
  1177. struct s3c_fb *sfb = platform_get_drvdata(pdev);
  1178. int win;
  1179. for (win = 0; win < S3C_FB_MAX_WIN; win++)
  1180. if (sfb->windows[win])
  1181. s3c_fb_release_win(sfb, sfb->windows[win]);
  1182. free_irq(sfb->irq_no, sfb);
  1183. iounmap(sfb->regs);
  1184. clk_disable(sfb->bus_clk);
  1185. clk_put(sfb->bus_clk);
  1186. release_resource(sfb->regs_res);
  1187. kfree(sfb->regs_res);
  1188. kfree(sfb);
  1189. return 0;
  1190. }
  1191. #ifdef CONFIG_PM
  1192. static int s3c_fb_suspend(struct platform_device *pdev, pm_message_t state)
  1193. {
  1194. struct s3c_fb *sfb = platform_get_drvdata(pdev);
  1195. struct s3c_fb_win *win;
  1196. int win_no;
  1197. for (win_no = S3C_FB_MAX_WIN - 1; win_no >= 0; win_no--) {
  1198. win = sfb->windows[win_no];
  1199. if (!win)
  1200. continue;
  1201. /* use the blank function to push into power-down */
  1202. s3c_fb_blank(FB_BLANK_POWERDOWN, win->fbinfo);
  1203. }
  1204. clk_disable(sfb->bus_clk);
  1205. return 0;
  1206. }
  1207. static int s3c_fb_resume(struct platform_device *pdev)
  1208. {
  1209. struct s3c_fb *sfb = platform_get_drvdata(pdev);
  1210. struct s3c_fb_platdata *pd = sfb->pdata;
  1211. struct s3c_fb_win *win;
  1212. int win_no;
  1213. clk_enable(sfb->bus_clk);
  1214. /* setup registers */
  1215. writel(pd->vidcon1, sfb->regs + VIDCON1);
  1216. /* zero all windows before we do anything */
  1217. for (win_no = 0; win_no < sfb->variant.nr_windows; win_no++)
  1218. s3c_fb_clear_win(sfb, win_no);
  1219. for (win_no = 0; win_no < sfb->variant.nr_windows - 1; win_no++) {
  1220. void __iomem *regs = sfb->regs + sfb->variant.keycon;
  1221. regs += (win_no * 8);
  1222. writel(0xffffff, regs + WKEYCON0);
  1223. writel(0xffffff, regs + WKEYCON1);
  1224. }
  1225. /* restore framebuffers */
  1226. for (win_no = 0; win_no < S3C_FB_MAX_WIN; win_no++) {
  1227. win = sfb->windows[win_no];
  1228. if (!win)
  1229. continue;
  1230. dev_dbg(&pdev->dev, "resuming window %d\n", win_no);
  1231. s3c_fb_set_par(win->fbinfo);
  1232. }
  1233. return 0;
  1234. }
  1235. #else
  1236. #define s3c_fb_suspend NULL
  1237. #define s3c_fb_resume NULL
  1238. #endif
  1239. #define VALID_BPP124 (VALID_BPP(1) | VALID_BPP(2) | VALID_BPP(4))
  1240. #define VALID_BPP1248 (VALID_BPP124 | VALID_BPP(8))
  1241. static struct s3c_fb_win_variant s3c_fb_data_64xx_wins[] = {
  1242. [0] = {
  1243. .has_osd_c = 1,
  1244. .osd_size_off = 0x8,
  1245. .palette_sz = 256,
  1246. .valid_bpp = VALID_BPP1248 | VALID_BPP(16) | VALID_BPP(24),
  1247. },
  1248. [1] = {
  1249. .has_osd_c = 1,
  1250. .has_osd_d = 1,
  1251. .osd_size_off = 0x12,
  1252. .has_osd_alpha = 1,
  1253. .palette_sz = 256,
  1254. .valid_bpp = (VALID_BPP1248 | VALID_BPP(16) |
  1255. VALID_BPP(18) | VALID_BPP(19) |
  1256. VALID_BPP(24) | VALID_BPP(25)),
  1257. },
  1258. [2] = {
  1259. .has_osd_c = 1,
  1260. .has_osd_d = 1,
  1261. .osd_size_off = 0x12,
  1262. .has_osd_alpha = 1,
  1263. .palette_sz = 16,
  1264. .palette_16bpp = 1,
  1265. .valid_bpp = (VALID_BPP1248 | VALID_BPP(16) |
  1266. VALID_BPP(18) | VALID_BPP(19) |
  1267. VALID_BPP(24) | VALID_BPP(25)),
  1268. },
  1269. [3] = {
  1270. .has_osd_c = 1,
  1271. .has_osd_alpha = 1,
  1272. .palette_sz = 16,
  1273. .palette_16bpp = 1,
  1274. .valid_bpp = (VALID_BPP124 | VALID_BPP(16) |
  1275. VALID_BPP(18) | VALID_BPP(19) |
  1276. VALID_BPP(24) | VALID_BPP(25)),
  1277. },
  1278. [4] = {
  1279. .has_osd_c = 1,
  1280. .has_osd_alpha = 1,
  1281. .palette_sz = 4,
  1282. .palette_16bpp = 1,
  1283. .valid_bpp = (VALID_BPP(1) | VALID_BPP(2) |
  1284. VALID_BPP(16) | VALID_BPP(18) |
  1285. VALID_BPP(24) | VALID_BPP(25)),
  1286. },
  1287. };
  1288. static struct s3c_fb_driverdata s3c_fb_data_64xx = {
  1289. .variant = {
  1290. .nr_windows = 5,
  1291. .vidtcon = VIDTCON0,
  1292. .wincon = WINCON(0),
  1293. .winmap = WINxMAP(0),
  1294. .keycon = WKEYCON,
  1295. .osd = VIDOSD_BASE,
  1296. .osd_stride = 16,
  1297. .buf_start = VIDW_BUF_START(0),
  1298. .buf_size = VIDW_BUF_SIZE(0),
  1299. .buf_end = VIDW_BUF_END(0),
  1300. .palette = {
  1301. [0] = 0x400,
  1302. [1] = 0x800,
  1303. [2] = 0x300,
  1304. [3] = 0x320,
  1305. [4] = 0x340,
  1306. },
  1307. .has_prtcon = 1,
  1308. },
  1309. .win[0] = &s3c_fb_data_64xx_wins[0],
  1310. .win[1] = &s3c_fb_data_64xx_wins[1],
  1311. .win[2] = &s3c_fb_data_64xx_wins[2],
  1312. .win[3] = &s3c_fb_data_64xx_wins[3],
  1313. .win[4] = &s3c_fb_data_64xx_wins[4],
  1314. };
  1315. static struct s3c_fb_driverdata s3c_fb_data_s5pc100 = {
  1316. .variant = {
  1317. .nr_windows = 5,
  1318. .vidtcon = VIDTCON0,
  1319. .wincon = WINCON(0),
  1320. .winmap = WINxMAP(0),
  1321. .keycon = WKEYCON,
  1322. .osd = VIDOSD_BASE,
  1323. .osd_stride = 16,
  1324. .buf_start = VIDW_BUF_START(0),
  1325. .buf_size = VIDW_BUF_SIZE(0),
  1326. .buf_end = VIDW_BUF_END(0),
  1327. .palette = {
  1328. [0] = 0x2400,
  1329. [1] = 0x2800,
  1330. [2] = 0x2c00,
  1331. [3] = 0x3000,
  1332. [4] = 0x3400,
  1333. },
  1334. .has_prtcon = 1,
  1335. },
  1336. .win[0] = &s3c_fb_data_64xx_wins[0],
  1337. .win[1] = &s3c_fb_data_64xx_wins[1],
  1338. .win[2] = &s3c_fb_data_64xx_wins[2],
  1339. .win[3] = &s3c_fb_data_64xx_wins[3],
  1340. .win[4] = &s3c_fb_data_64xx_wins[4],
  1341. };
  1342. static struct s3c_fb_driverdata s3c_fb_data_s5pv210 = {
  1343. .variant = {
  1344. .nr_windows = 5,
  1345. .vidtcon = VIDTCON0,
  1346. .wincon = WINCON(0),
  1347. .winmap = WINxMAP(0),
  1348. .keycon = WKEYCON,
  1349. .osd = VIDOSD_BASE,
  1350. .osd_stride = 16,
  1351. .buf_start = VIDW_BUF_START(0),
  1352. .buf_size = VIDW_BUF_SIZE(0),
  1353. .buf_end = VIDW_BUF_END(0),
  1354. .palette = {
  1355. [0] = 0x2400,
  1356. [1] = 0x2800,
  1357. [2] = 0x2c00,
  1358. [3] = 0x3000,
  1359. [4] = 0x3400,
  1360. },
  1361. .has_shadowcon = 1,
  1362. },
  1363. .win[0] = &s3c_fb_data_64xx_wins[0],
  1364. .win[1] = &s3c_fb_data_64xx_wins[1],
  1365. .win[2] = &s3c_fb_data_64xx_wins[2],
  1366. .win[3] = &s3c_fb_data_64xx_wins[3],
  1367. .win[4] = &s3c_fb_data_64xx_wins[4],
  1368. };
  1369. /* S3C2443/S3C2416 style hardware */
  1370. static struct s3c_fb_driverdata s3c_fb_data_s3c2443 = {
  1371. .variant = {
  1372. .nr_windows = 2,
  1373. .is_2443 = 1,
  1374. .vidtcon = 0x08,
  1375. .wincon = 0x14,
  1376. .winmap = 0xd0,
  1377. .keycon = 0xb0,
  1378. .osd = 0x28,
  1379. .osd_stride = 12,
  1380. .buf_start = 0x64,
  1381. .buf_size = 0x94,
  1382. .buf_end = 0x7c,
  1383. .palette = {
  1384. [0] = 0x400,
  1385. [1] = 0x800,
  1386. },
  1387. },
  1388. .win[0] = &(struct s3c_fb_win_variant) {
  1389. .palette_sz = 256,
  1390. .valid_bpp = VALID_BPP1248 | VALID_BPP(16) | VALID_BPP(24),
  1391. },
  1392. .win[1] = &(struct s3c_fb_win_variant) {
  1393. .has_osd_c = 1,
  1394. .has_osd_alpha = 1,
  1395. .palette_sz = 256,
  1396. .valid_bpp = (VALID_BPP1248 | VALID_BPP(16) |
  1397. VALID_BPP(18) | VALID_BPP(19) |
  1398. VALID_BPP(24) | VALID_BPP(25) |
  1399. VALID_BPP(28)),
  1400. },
  1401. };
  1402. static struct platform_device_id s3c_fb_driver_ids[] = {
  1403. {
  1404. .name = "s3c-fb",
  1405. .driver_data = (unsigned long)&s3c_fb_data_64xx,
  1406. }, {
  1407. .name = "s5pc100-fb",
  1408. .driver_data = (unsigned long)&s3c_fb_data_s5pc100,
  1409. }, {
  1410. .name = "s5pv210-fb",
  1411. .driver_data = (unsigned long)&s3c_fb_data_s5pv210,
  1412. }, {
  1413. .name = "s3c2443-fb",
  1414. .driver_data = (unsigned long)&s3c_fb_data_s3c2443,
  1415. },
  1416. {},
  1417. };
  1418. MODULE_DEVICE_TABLE(platform, s3c_fb_driver_ids);
  1419. static struct platform_driver s3c_fb_driver = {
  1420. .probe = s3c_fb_probe,
  1421. .remove = __devexit_p(s3c_fb_remove),
  1422. .suspend = s3c_fb_suspend,
  1423. .resume = s3c_fb_resume,
  1424. .id_table = s3c_fb_driver_ids,
  1425. .driver = {
  1426. .name = "s3c-fb",
  1427. .owner = THIS_MODULE,
  1428. },
  1429. };
  1430. static int __init s3c_fb_init(void)
  1431. {
  1432. return platform_driver_register(&s3c_fb_driver);
  1433. }
  1434. static void __exit s3c_fb_cleanup(void)
  1435. {
  1436. platform_driver_unregister(&s3c_fb_driver);
  1437. }
  1438. module_init(s3c_fb_init);
  1439. module_exit(s3c_fb_cleanup);
  1440. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  1441. MODULE_DESCRIPTION("Samsung S3C SoC Framebuffer driver");
  1442. MODULE_LICENSE("GPL");
  1443. MODULE_ALIAS("platform:s3c-fb");