s3c-fb.c 41 KB

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