s3c-fb.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701
  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. shadow_protect_win(win, 0);
  547. return 0;
  548. }
  549. /**
  550. * s3c_fb_update_palette() - set or schedule a palette update.
  551. * @sfb: The hardware information.
  552. * @win: The window being updated.
  553. * @reg: The palette index being changed.
  554. * @value: The computed palette value.
  555. *
  556. * Change the value of a palette register, either by directly writing to
  557. * the palette (this requires the palette RAM to be disconnected from the
  558. * hardware whilst this is in progress) or schedule the update for later.
  559. *
  560. * At the moment, since we have no VSYNC interrupt support, we simply set
  561. * the palette entry directly.
  562. */
  563. static void s3c_fb_update_palette(struct s3c_fb *sfb,
  564. struct s3c_fb_win *win,
  565. unsigned int reg,
  566. u32 value)
  567. {
  568. void __iomem *palreg;
  569. u32 palcon;
  570. palreg = sfb->regs + sfb->variant.palette[win->index];
  571. dev_dbg(sfb->dev, "%s: win %d, reg %d (%p): %08x\n",
  572. __func__, win->index, reg, palreg, value);
  573. win->palette_buffer[reg] = value;
  574. palcon = readl(sfb->regs + WPALCON);
  575. writel(palcon | WPALCON_PAL_UPDATE, sfb->regs + WPALCON);
  576. if (win->variant.palette_16bpp)
  577. writew(value, palreg + (reg * 2));
  578. else
  579. writel(value, palreg + (reg * 4));
  580. writel(palcon, sfb->regs + WPALCON);
  581. }
  582. static inline unsigned int chan_to_field(unsigned int chan,
  583. struct fb_bitfield *bf)
  584. {
  585. chan &= 0xffff;
  586. chan >>= 16 - bf->length;
  587. return chan << bf->offset;
  588. }
  589. /**
  590. * s3c_fb_setcolreg() - framebuffer layer request to change palette.
  591. * @regno: The palette index to change.
  592. * @red: The red field for the palette data.
  593. * @green: The green field for the palette data.
  594. * @blue: The blue field for the palette data.
  595. * @trans: The transparency (alpha) field for the palette data.
  596. * @info: The framebuffer being changed.
  597. */
  598. static int s3c_fb_setcolreg(unsigned regno,
  599. unsigned red, unsigned green, unsigned blue,
  600. unsigned transp, struct fb_info *info)
  601. {
  602. struct s3c_fb_win *win = info->par;
  603. struct s3c_fb *sfb = win->parent;
  604. unsigned int val;
  605. dev_dbg(sfb->dev, "%s: win %d: %d => rgb=%d/%d/%d\n",
  606. __func__, win->index, regno, red, green, blue);
  607. switch (info->fix.visual) {
  608. case FB_VISUAL_TRUECOLOR:
  609. /* true-colour, use pseudo-palette */
  610. if (regno < 16) {
  611. u32 *pal = info->pseudo_palette;
  612. val = chan_to_field(red, &info->var.red);
  613. val |= chan_to_field(green, &info->var.green);
  614. val |= chan_to_field(blue, &info->var.blue);
  615. pal[regno] = val;
  616. }
  617. break;
  618. case FB_VISUAL_PSEUDOCOLOR:
  619. if (regno < win->variant.palette_sz) {
  620. val = chan_to_field(red, &win->palette.r);
  621. val |= chan_to_field(green, &win->palette.g);
  622. val |= chan_to_field(blue, &win->palette.b);
  623. s3c_fb_update_palette(sfb, win, regno, val);
  624. }
  625. break;
  626. default:
  627. return 1; /* unknown type */
  628. }
  629. return 0;
  630. }
  631. /**
  632. * s3c_fb_enable() - Set the state of the main LCD output
  633. * @sfb: The main framebuffer state.
  634. * @enable: The state to set.
  635. */
  636. static void s3c_fb_enable(struct s3c_fb *sfb, int enable)
  637. {
  638. u32 vidcon0 = readl(sfb->regs + VIDCON0);
  639. if (enable)
  640. vidcon0 |= VIDCON0_ENVID | VIDCON0_ENVID_F;
  641. else {
  642. /* see the note in the framebuffer datasheet about
  643. * why you cannot take both of these bits down at the
  644. * same time. */
  645. if (!(vidcon0 & VIDCON0_ENVID))
  646. return;
  647. vidcon0 |= VIDCON0_ENVID;
  648. vidcon0 &= ~VIDCON0_ENVID_F;
  649. }
  650. writel(vidcon0, sfb->regs + VIDCON0);
  651. }
  652. /**
  653. * s3c_fb_blank() - blank or unblank the given window
  654. * @blank_mode: The blank state from FB_BLANK_*
  655. * @info: The framebuffer to blank.
  656. *
  657. * Framebuffer layer request to change the power state.
  658. */
  659. static int s3c_fb_blank(int blank_mode, struct fb_info *info)
  660. {
  661. struct s3c_fb_win *win = info->par;
  662. struct s3c_fb *sfb = win->parent;
  663. unsigned int index = win->index;
  664. u32 wincon;
  665. dev_dbg(sfb->dev, "blank mode %d\n", blank_mode);
  666. wincon = readl(sfb->regs + sfb->variant.wincon + (index * 4));
  667. switch (blank_mode) {
  668. case FB_BLANK_POWERDOWN:
  669. wincon &= ~WINCONx_ENWIN;
  670. sfb->enabled &= ~(1 << index);
  671. /* fall through to FB_BLANK_NORMAL */
  672. case FB_BLANK_NORMAL:
  673. /* disable the DMA and display 0x0 (black) */
  674. writel(WINxMAP_MAP | WINxMAP_MAP_COLOUR(0x0),
  675. sfb->regs + sfb->variant.winmap + (index * 4));
  676. break;
  677. case FB_BLANK_UNBLANK:
  678. writel(0x0, sfb->regs + sfb->variant.winmap + (index * 4));
  679. wincon |= WINCONx_ENWIN;
  680. sfb->enabled |= (1 << index);
  681. break;
  682. case FB_BLANK_VSYNC_SUSPEND:
  683. case FB_BLANK_HSYNC_SUSPEND:
  684. default:
  685. return 1;
  686. }
  687. writel(wincon, sfb->regs + sfb->variant.wincon + (index * 4));
  688. /* Check the enabled state to see if we need to be running the
  689. * main LCD interface, as if there are no active windows then
  690. * it is highly likely that we also do not need to output
  691. * anything.
  692. */
  693. /* We could do something like the following code, but the current
  694. * system of using framebuffer events means that we cannot make
  695. * the distinction between just window 0 being inactive and all
  696. * the windows being down.
  697. *
  698. * s3c_fb_enable(sfb, sfb->enabled ? 1 : 0);
  699. */
  700. /* we're stuck with this until we can do something about overriding
  701. * the power control using the blanking event for a single fb.
  702. */
  703. if (index == sfb->pdata->default_win)
  704. s3c_fb_enable(sfb, blank_mode != FB_BLANK_POWERDOWN ? 1 : 0);
  705. return 0;
  706. }
  707. /**
  708. * s3c_fb_pan_display() - Pan the display.
  709. *
  710. * Note that the offsets can be written to the device at any time, as their
  711. * values are latched at each vsync automatically. This also means that only
  712. * the last call to this function will have any effect on next vsync, but
  713. * there is no need to sleep waiting for it to prevent tearing.
  714. *
  715. * @var: The screen information to verify.
  716. * @info: The framebuffer device.
  717. */
  718. static int s3c_fb_pan_display(struct fb_var_screeninfo *var,
  719. struct fb_info *info)
  720. {
  721. struct s3c_fb_win *win = info->par;
  722. struct s3c_fb *sfb = win->parent;
  723. void __iomem *buf = sfb->regs + win->index * 8;
  724. unsigned int start_boff, end_boff;
  725. /* Offset in bytes to the start of the displayed area */
  726. start_boff = var->yoffset * info->fix.line_length;
  727. /* X offset depends on the current bpp */
  728. if (info->var.bits_per_pixel >= 8) {
  729. start_boff += var->xoffset * (info->var.bits_per_pixel >> 3);
  730. } else {
  731. switch (info->var.bits_per_pixel) {
  732. case 4:
  733. start_boff += var->xoffset >> 1;
  734. break;
  735. case 2:
  736. start_boff += var->xoffset >> 2;
  737. break;
  738. case 1:
  739. start_boff += var->xoffset >> 3;
  740. break;
  741. default:
  742. dev_err(sfb->dev, "invalid bpp\n");
  743. return -EINVAL;
  744. }
  745. }
  746. /* Offset in bytes to the end of the displayed area */
  747. end_boff = start_boff + var->yres * info->fix.line_length;
  748. /* Temporarily turn off per-vsync update from shadow registers until
  749. * both start and end addresses are updated to prevent corruption */
  750. shadow_protect_win(win, 1);
  751. writel(info->fix.smem_start + start_boff, buf + sfb->variant.buf_start);
  752. writel(info->fix.smem_start + end_boff, buf + sfb->variant.buf_end);
  753. shadow_protect_win(win, 0);
  754. return 0;
  755. }
  756. /**
  757. * s3c_fb_enable_irq() - enable framebuffer interrupts
  758. * @sfb: main hardware state
  759. */
  760. static void s3c_fb_enable_irq(struct s3c_fb *sfb)
  761. {
  762. void __iomem *regs = sfb->regs;
  763. u32 irq_ctrl_reg;
  764. if (!test_and_set_bit(S3C_FB_VSYNC_IRQ_EN, &sfb->irq_flags)) {
  765. /* IRQ disabled, enable it */
  766. irq_ctrl_reg = readl(regs + VIDINTCON0);
  767. irq_ctrl_reg |= VIDINTCON0_INT_ENABLE;
  768. irq_ctrl_reg |= VIDINTCON0_INT_FRAME;
  769. irq_ctrl_reg &= ~VIDINTCON0_FRAMESEL0_MASK;
  770. irq_ctrl_reg |= VIDINTCON0_FRAMESEL0_VSYNC;
  771. irq_ctrl_reg &= ~VIDINTCON0_FRAMESEL1_MASK;
  772. irq_ctrl_reg |= VIDINTCON0_FRAMESEL1_NONE;
  773. writel(irq_ctrl_reg, regs + VIDINTCON0);
  774. }
  775. }
  776. /**
  777. * s3c_fb_disable_irq() - disable framebuffer interrupts
  778. * @sfb: main hardware state
  779. */
  780. static void s3c_fb_disable_irq(struct s3c_fb *sfb)
  781. {
  782. void __iomem *regs = sfb->regs;
  783. u32 irq_ctrl_reg;
  784. if (test_and_clear_bit(S3C_FB_VSYNC_IRQ_EN, &sfb->irq_flags)) {
  785. /* IRQ enabled, disable it */
  786. irq_ctrl_reg = readl(regs + VIDINTCON0);
  787. irq_ctrl_reg &= ~VIDINTCON0_INT_FRAME;
  788. irq_ctrl_reg &= ~VIDINTCON0_INT_ENABLE;
  789. writel(irq_ctrl_reg, regs + VIDINTCON0);
  790. }
  791. }
  792. static irqreturn_t s3c_fb_irq(int irq, void *dev_id)
  793. {
  794. struct s3c_fb *sfb = dev_id;
  795. void __iomem *regs = sfb->regs;
  796. u32 irq_sts_reg;
  797. irq_sts_reg = readl(regs + VIDINTCON1);
  798. if (irq_sts_reg & VIDINTCON1_INT_FRAME) {
  799. /* VSYNC interrupt, accept it */
  800. writel(VIDINTCON1_INT_FRAME, regs + VIDINTCON1);
  801. sfb->vsync_info.count++;
  802. wake_up_interruptible(&sfb->vsync_info.wait);
  803. }
  804. /* We only support waiting for VSYNC for now, so it's safe
  805. * to always disable irqs here.
  806. */
  807. s3c_fb_disable_irq(sfb);
  808. return IRQ_HANDLED;
  809. }
  810. /**
  811. * s3c_fb_wait_for_vsync() - sleep until next VSYNC interrupt or timeout
  812. * @sfb: main hardware state
  813. * @crtc: head index.
  814. */
  815. static int s3c_fb_wait_for_vsync(struct s3c_fb *sfb, u32 crtc)
  816. {
  817. unsigned long count;
  818. int ret;
  819. if (crtc != 0)
  820. return -ENODEV;
  821. count = sfb->vsync_info.count;
  822. s3c_fb_enable_irq(sfb);
  823. ret = wait_event_interruptible_timeout(sfb->vsync_info.wait,
  824. count != sfb->vsync_info.count,
  825. msecs_to_jiffies(VSYNC_TIMEOUT_MSEC));
  826. if (ret == 0)
  827. return -ETIMEDOUT;
  828. return 0;
  829. }
  830. static int s3c_fb_ioctl(struct fb_info *info, unsigned int cmd,
  831. unsigned long arg)
  832. {
  833. struct s3c_fb_win *win = info->par;
  834. struct s3c_fb *sfb = win->parent;
  835. int ret;
  836. u32 crtc;
  837. switch (cmd) {
  838. case FBIO_WAITFORVSYNC:
  839. if (get_user(crtc, (u32 __user *)arg)) {
  840. ret = -EFAULT;
  841. break;
  842. }
  843. ret = s3c_fb_wait_for_vsync(sfb, crtc);
  844. break;
  845. default:
  846. ret = -ENOTTY;
  847. }
  848. return ret;
  849. }
  850. static struct fb_ops s3c_fb_ops = {
  851. .owner = THIS_MODULE,
  852. .fb_check_var = s3c_fb_check_var,
  853. .fb_set_par = s3c_fb_set_par,
  854. .fb_blank = s3c_fb_blank,
  855. .fb_setcolreg = s3c_fb_setcolreg,
  856. .fb_fillrect = cfb_fillrect,
  857. .fb_copyarea = cfb_copyarea,
  858. .fb_imageblit = cfb_imageblit,
  859. .fb_pan_display = s3c_fb_pan_display,
  860. .fb_ioctl = s3c_fb_ioctl,
  861. };
  862. /**
  863. * s3c_fb_alloc_memory() - allocate display memory for framebuffer window
  864. * @sfb: The base resources for the hardware.
  865. * @win: The window to initialise memory for.
  866. *
  867. * Allocate memory for the given framebuffer.
  868. */
  869. static int __devinit s3c_fb_alloc_memory(struct s3c_fb *sfb,
  870. struct s3c_fb_win *win)
  871. {
  872. struct s3c_fb_pd_win *windata = win->windata;
  873. unsigned int real_size, virt_size, size;
  874. struct fb_info *fbi = win->fbinfo;
  875. dma_addr_t map_dma;
  876. dev_dbg(sfb->dev, "allocating memory for display\n");
  877. real_size = windata->win_mode.xres * windata->win_mode.yres;
  878. virt_size = windata->virtual_x * windata->virtual_y;
  879. dev_dbg(sfb->dev, "real_size=%u (%u.%u), virt_size=%u (%u.%u)\n",
  880. real_size, windata->win_mode.xres, windata->win_mode.yres,
  881. virt_size, windata->virtual_x, windata->virtual_y);
  882. size = (real_size > virt_size) ? real_size : virt_size;
  883. size *= (windata->max_bpp > 16) ? 32 : windata->max_bpp;
  884. size /= 8;
  885. fbi->fix.smem_len = size;
  886. size = PAGE_ALIGN(size);
  887. dev_dbg(sfb->dev, "want %u bytes for window\n", size);
  888. fbi->screen_base = dma_alloc_writecombine(sfb->dev, size,
  889. &map_dma, GFP_KERNEL);
  890. if (!fbi->screen_base)
  891. return -ENOMEM;
  892. dev_dbg(sfb->dev, "mapped %x to %p\n",
  893. (unsigned int)map_dma, fbi->screen_base);
  894. memset(fbi->screen_base, 0x0, size);
  895. fbi->fix.smem_start = map_dma;
  896. return 0;
  897. }
  898. /**
  899. * s3c_fb_free_memory() - free the display memory for the given window
  900. * @sfb: The base resources for the hardware.
  901. * @win: The window to free the display memory for.
  902. *
  903. * Free the display memory allocated by s3c_fb_alloc_memory().
  904. */
  905. static void s3c_fb_free_memory(struct s3c_fb *sfb, struct s3c_fb_win *win)
  906. {
  907. struct fb_info *fbi = win->fbinfo;
  908. if (fbi->screen_base)
  909. dma_free_writecombine(sfb->dev, PAGE_ALIGN(fbi->fix.smem_len),
  910. fbi->screen_base, fbi->fix.smem_start);
  911. }
  912. /**
  913. * s3c_fb_release_win() - release resources for a framebuffer window.
  914. * @win: The window to cleanup the resources for.
  915. *
  916. * Release the resources that where claimed for the hardware window,
  917. * such as the framebuffer instance and any memory claimed for it.
  918. */
  919. static void s3c_fb_release_win(struct s3c_fb *sfb, struct s3c_fb_win *win)
  920. {
  921. if (win->fbinfo) {
  922. unregister_framebuffer(win->fbinfo);
  923. if (win->fbinfo->cmap.len)
  924. fb_dealloc_cmap(&win->fbinfo->cmap);
  925. s3c_fb_free_memory(sfb, win);
  926. framebuffer_release(win->fbinfo);
  927. }
  928. }
  929. /**
  930. * s3c_fb_probe_win() - register an hardware window
  931. * @sfb: The base resources for the hardware
  932. * @variant: The variant information for this window.
  933. * @res: Pointer to where to place the resultant window.
  934. *
  935. * Allocate and do the basic initialisation for one of the hardware's graphics
  936. * windows.
  937. */
  938. static int __devinit s3c_fb_probe_win(struct s3c_fb *sfb, unsigned int win_no,
  939. struct s3c_fb_win_variant *variant,
  940. struct s3c_fb_win **res)
  941. {
  942. struct fb_var_screeninfo *var;
  943. struct fb_videomode *initmode;
  944. struct s3c_fb_pd_win *windata;
  945. struct s3c_fb_win *win;
  946. struct fb_info *fbinfo;
  947. int palette_size;
  948. int ret;
  949. dev_dbg(sfb->dev, "probing window %d, variant %p\n", win_no, variant);
  950. init_waitqueue_head(&sfb->vsync_info.wait);
  951. palette_size = variant->palette_sz * 4;
  952. fbinfo = framebuffer_alloc(sizeof(struct s3c_fb_win) +
  953. palette_size * sizeof(u32), sfb->dev);
  954. if (!fbinfo) {
  955. dev_err(sfb->dev, "failed to allocate framebuffer\n");
  956. return -ENOENT;
  957. }
  958. windata = sfb->pdata->win[win_no];
  959. initmode = &windata->win_mode;
  960. WARN_ON(windata->max_bpp == 0);
  961. WARN_ON(windata->win_mode.xres == 0);
  962. WARN_ON(windata->win_mode.yres == 0);
  963. win = fbinfo->par;
  964. *res = win;
  965. var = &fbinfo->var;
  966. win->variant = *variant;
  967. win->fbinfo = fbinfo;
  968. win->parent = sfb;
  969. win->windata = windata;
  970. win->index = win_no;
  971. win->palette_buffer = (u32 *)(win + 1);
  972. ret = s3c_fb_alloc_memory(sfb, win);
  973. if (ret) {
  974. dev_err(sfb->dev, "failed to allocate display memory\n");
  975. return ret;
  976. }
  977. /* setup the r/b/g positions for the window's palette */
  978. if (win->variant.palette_16bpp) {
  979. /* Set RGB 5:6:5 as default */
  980. win->palette.r.offset = 11;
  981. win->palette.r.length = 5;
  982. win->palette.g.offset = 5;
  983. win->palette.g.length = 6;
  984. win->palette.b.offset = 0;
  985. win->palette.b.length = 5;
  986. } else {
  987. /* Set 8bpp or 8bpp and 1bit alpha */
  988. win->palette.r.offset = 16;
  989. win->palette.r.length = 8;
  990. win->palette.g.offset = 8;
  991. win->palette.g.length = 8;
  992. win->palette.b.offset = 0;
  993. win->palette.b.length = 8;
  994. }
  995. /* setup the initial video mode from the window */
  996. fb_videomode_to_var(&fbinfo->var, initmode);
  997. fbinfo->fix.type = FB_TYPE_PACKED_PIXELS;
  998. fbinfo->fix.accel = FB_ACCEL_NONE;
  999. fbinfo->var.activate = FB_ACTIVATE_NOW;
  1000. fbinfo->var.vmode = FB_VMODE_NONINTERLACED;
  1001. fbinfo->var.bits_per_pixel = windata->default_bpp;
  1002. fbinfo->fbops = &s3c_fb_ops;
  1003. fbinfo->flags = FBINFO_FLAG_DEFAULT;
  1004. fbinfo->pseudo_palette = &win->pseudo_palette;
  1005. /* prepare to actually start the framebuffer */
  1006. ret = s3c_fb_check_var(&fbinfo->var, fbinfo);
  1007. if (ret < 0) {
  1008. dev_err(sfb->dev, "check_var failed on initial video params\n");
  1009. return ret;
  1010. }
  1011. /* create initial colour map */
  1012. ret = fb_alloc_cmap(&fbinfo->cmap, win->variant.palette_sz, 1);
  1013. if (ret == 0)
  1014. fb_set_cmap(&fbinfo->cmap, fbinfo);
  1015. else
  1016. dev_err(sfb->dev, "failed to allocate fb cmap\n");
  1017. s3c_fb_set_par(fbinfo);
  1018. dev_dbg(sfb->dev, "about to register framebuffer\n");
  1019. /* run the check_var and set_par on our configuration. */
  1020. ret = register_framebuffer(fbinfo);
  1021. if (ret < 0) {
  1022. dev_err(sfb->dev, "failed to register framebuffer\n");
  1023. return ret;
  1024. }
  1025. dev_info(sfb->dev, "window %d: fb %s\n", win_no, fbinfo->fix.id);
  1026. return 0;
  1027. }
  1028. /**
  1029. * s3c_fb_clear_win() - clear hardware window registers.
  1030. * @sfb: The base resources for the hardware.
  1031. * @win: The window to process.
  1032. *
  1033. * Reset the specific window registers to a known state.
  1034. */
  1035. static void s3c_fb_clear_win(struct s3c_fb *sfb, int win)
  1036. {
  1037. void __iomem *regs = sfb->regs;
  1038. u32 reg;
  1039. writel(0, regs + sfb->variant.wincon + (win * 4));
  1040. writel(0, regs + VIDOSD_A(win, sfb->variant));
  1041. writel(0, regs + VIDOSD_B(win, sfb->variant));
  1042. writel(0, regs + VIDOSD_C(win, sfb->variant));
  1043. reg = readl(regs + SHADOWCON);
  1044. writel(reg & ~SHADOWCON_WINx_PROTECT(win), regs + SHADOWCON);
  1045. }
  1046. static int __devinit s3c_fb_probe(struct platform_device *pdev)
  1047. {
  1048. struct s3c_fb_driverdata *fbdrv;
  1049. struct device *dev = &pdev->dev;
  1050. struct s3c_fb_platdata *pd;
  1051. struct s3c_fb *sfb;
  1052. struct resource *res;
  1053. int win;
  1054. int ret = 0;
  1055. fbdrv = (struct s3c_fb_driverdata *)platform_get_device_id(pdev)->driver_data;
  1056. if (fbdrv->variant.nr_windows > S3C_FB_MAX_WIN) {
  1057. dev_err(dev, "too many windows, cannot attach\n");
  1058. return -EINVAL;
  1059. }
  1060. pd = pdev->dev.platform_data;
  1061. if (!pd) {
  1062. dev_err(dev, "no platform data specified\n");
  1063. return -EINVAL;
  1064. }
  1065. sfb = kzalloc(sizeof(struct s3c_fb), GFP_KERNEL);
  1066. if (!sfb) {
  1067. dev_err(dev, "no memory for framebuffers\n");
  1068. return -ENOMEM;
  1069. }
  1070. dev_dbg(dev, "allocate new framebuffer %p\n", sfb);
  1071. sfb->dev = dev;
  1072. sfb->pdata = pd;
  1073. sfb->variant = fbdrv->variant;
  1074. sfb->bus_clk = clk_get(dev, "lcd");
  1075. if (IS_ERR(sfb->bus_clk)) {
  1076. dev_err(dev, "failed to get bus clock\n");
  1077. goto err_sfb;
  1078. }
  1079. clk_enable(sfb->bus_clk);
  1080. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1081. if (!res) {
  1082. dev_err(dev, "failed to find registers\n");
  1083. ret = -ENOENT;
  1084. goto err_clk;
  1085. }
  1086. sfb->regs_res = request_mem_region(res->start, resource_size(res),
  1087. dev_name(dev));
  1088. if (!sfb->regs_res) {
  1089. dev_err(dev, "failed to claim register region\n");
  1090. ret = -ENOENT;
  1091. goto err_clk;
  1092. }
  1093. sfb->regs = ioremap(res->start, resource_size(res));
  1094. if (!sfb->regs) {
  1095. dev_err(dev, "failed to map registers\n");
  1096. ret = -ENXIO;
  1097. goto err_req_region;
  1098. }
  1099. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1100. if (!res) {
  1101. dev_err(dev, "failed to acquire irq resource\n");
  1102. ret = -ENOENT;
  1103. goto err_ioremap;
  1104. }
  1105. sfb->irq_no = res->start;
  1106. ret = request_irq(sfb->irq_no, s3c_fb_irq,
  1107. 0, "s3c_fb", sfb);
  1108. if (ret) {
  1109. dev_err(dev, "irq request failed\n");
  1110. goto err_ioremap;
  1111. }
  1112. dev_dbg(dev, "got resources (regs %p), probing windows\n", sfb->regs);
  1113. /* setup gpio and output polarity controls */
  1114. pd->setup_gpio();
  1115. writel(pd->vidcon1, sfb->regs + VIDCON1);
  1116. /* zero all windows before we do anything */
  1117. for (win = 0; win < fbdrv->variant.nr_windows; win++)
  1118. s3c_fb_clear_win(sfb, win);
  1119. /* initialise colour key controls */
  1120. for (win = 0; win < (fbdrv->variant.nr_windows - 1); win++) {
  1121. void __iomem *regs = sfb->regs + sfb->variant.keycon;
  1122. regs += (win * 8);
  1123. writel(0xffffff, regs + WKEYCON0);
  1124. writel(0xffffff, regs + WKEYCON1);
  1125. }
  1126. /* we have the register setup, start allocating framebuffers */
  1127. for (win = 0; win < fbdrv->variant.nr_windows; win++) {
  1128. if (!pd->win[win])
  1129. continue;
  1130. ret = s3c_fb_probe_win(sfb, win, fbdrv->win[win],
  1131. &sfb->windows[win]);
  1132. if (ret < 0) {
  1133. dev_err(dev, "failed to create window %d\n", win);
  1134. for (; win >= 0; win--)
  1135. s3c_fb_release_win(sfb, sfb->windows[win]);
  1136. goto err_irq;
  1137. }
  1138. }
  1139. platform_set_drvdata(pdev, sfb);
  1140. return 0;
  1141. err_irq:
  1142. free_irq(sfb->irq_no, sfb);
  1143. err_ioremap:
  1144. iounmap(sfb->regs);
  1145. err_req_region:
  1146. release_resource(sfb->regs_res);
  1147. kfree(sfb->regs_res);
  1148. err_clk:
  1149. clk_disable(sfb->bus_clk);
  1150. clk_put(sfb->bus_clk);
  1151. err_sfb:
  1152. kfree(sfb);
  1153. return ret;
  1154. }
  1155. /**
  1156. * s3c_fb_remove() - Cleanup on module finalisation
  1157. * @pdev: The platform device we are bound to.
  1158. *
  1159. * Shutdown and then release all the resources that the driver allocated
  1160. * on initialisation.
  1161. */
  1162. static int __devexit s3c_fb_remove(struct platform_device *pdev)
  1163. {
  1164. struct s3c_fb *sfb = platform_get_drvdata(pdev);
  1165. int win;
  1166. for (win = 0; win < S3C_FB_MAX_WIN; win++)
  1167. if (sfb->windows[win])
  1168. s3c_fb_release_win(sfb, sfb->windows[win]);
  1169. free_irq(sfb->irq_no, sfb);
  1170. iounmap(sfb->regs);
  1171. clk_disable(sfb->bus_clk);
  1172. clk_put(sfb->bus_clk);
  1173. release_resource(sfb->regs_res);
  1174. kfree(sfb->regs_res);
  1175. kfree(sfb);
  1176. return 0;
  1177. }
  1178. #ifdef CONFIG_PM
  1179. static int s3c_fb_suspend(struct platform_device *pdev, pm_message_t state)
  1180. {
  1181. struct s3c_fb *sfb = platform_get_drvdata(pdev);
  1182. struct s3c_fb_win *win;
  1183. int win_no;
  1184. for (win_no = S3C_FB_MAX_WIN - 1; win_no >= 0; win_no--) {
  1185. win = sfb->windows[win_no];
  1186. if (!win)
  1187. continue;
  1188. /* use the blank function to push into power-down */
  1189. s3c_fb_blank(FB_BLANK_POWERDOWN, win->fbinfo);
  1190. }
  1191. clk_disable(sfb->bus_clk);
  1192. return 0;
  1193. }
  1194. static int s3c_fb_resume(struct platform_device *pdev)
  1195. {
  1196. struct s3c_fb *sfb = platform_get_drvdata(pdev);
  1197. struct s3c_fb_platdata *pd = sfb->pdata;
  1198. struct s3c_fb_win *win;
  1199. int win_no;
  1200. clk_enable(sfb->bus_clk);
  1201. /* setup registers */
  1202. writel(pd->vidcon1, sfb->regs + VIDCON1);
  1203. /* zero all windows before we do anything */
  1204. for (win_no = 0; win_no < sfb->variant.nr_windows; win_no++)
  1205. s3c_fb_clear_win(sfb, win_no);
  1206. for (win_no = 0; win_no < sfb->variant.nr_windows - 1; win_no++) {
  1207. void __iomem *regs = sfb->regs + sfb->variant.keycon;
  1208. regs += (win_no * 8);
  1209. writel(0xffffff, regs + WKEYCON0);
  1210. writel(0xffffff, regs + WKEYCON1);
  1211. }
  1212. /* restore framebuffers */
  1213. for (win_no = 0; win_no < S3C_FB_MAX_WIN; win_no++) {
  1214. win = sfb->windows[win_no];
  1215. if (!win)
  1216. continue;
  1217. dev_dbg(&pdev->dev, "resuming window %d\n", win_no);
  1218. s3c_fb_set_par(win->fbinfo);
  1219. }
  1220. return 0;
  1221. }
  1222. #else
  1223. #define s3c_fb_suspend NULL
  1224. #define s3c_fb_resume NULL
  1225. #endif
  1226. #define VALID_BPP124 (VALID_BPP(1) | VALID_BPP(2) | VALID_BPP(4))
  1227. #define VALID_BPP1248 (VALID_BPP124 | VALID_BPP(8))
  1228. static struct s3c_fb_win_variant s3c_fb_data_64xx_wins[] __devinitdata = {
  1229. [0] = {
  1230. .has_osd_c = 1,
  1231. .osd_size_off = 0x8,
  1232. .palette_sz = 256,
  1233. .valid_bpp = VALID_BPP1248 | VALID_BPP(16) | VALID_BPP(24),
  1234. },
  1235. [1] = {
  1236. .has_osd_c = 1,
  1237. .has_osd_d = 1,
  1238. .osd_size_off = 0x12,
  1239. .has_osd_alpha = 1,
  1240. .palette_sz = 256,
  1241. .valid_bpp = (VALID_BPP1248 | VALID_BPP(16) |
  1242. VALID_BPP(18) | VALID_BPP(19) |
  1243. VALID_BPP(24) | VALID_BPP(25)),
  1244. },
  1245. [2] = {
  1246. .has_osd_c = 1,
  1247. .has_osd_d = 1,
  1248. .osd_size_off = 0x12,
  1249. .has_osd_alpha = 1,
  1250. .palette_sz = 16,
  1251. .palette_16bpp = 1,
  1252. .valid_bpp = (VALID_BPP1248 | VALID_BPP(16) |
  1253. VALID_BPP(18) | VALID_BPP(19) |
  1254. VALID_BPP(24) | VALID_BPP(25)),
  1255. },
  1256. [3] = {
  1257. .has_osd_c = 1,
  1258. .has_osd_alpha = 1,
  1259. .palette_sz = 16,
  1260. .palette_16bpp = 1,
  1261. .valid_bpp = (VALID_BPP124 | VALID_BPP(16) |
  1262. VALID_BPP(18) | VALID_BPP(19) |
  1263. VALID_BPP(24) | VALID_BPP(25)),
  1264. },
  1265. [4] = {
  1266. .has_osd_c = 1,
  1267. .has_osd_alpha = 1,
  1268. .palette_sz = 4,
  1269. .palette_16bpp = 1,
  1270. .valid_bpp = (VALID_BPP(1) | VALID_BPP(2) |
  1271. VALID_BPP(16) | VALID_BPP(18) |
  1272. VALID_BPP(24) | VALID_BPP(25)),
  1273. },
  1274. };
  1275. static struct s3c_fb_driverdata s3c_fb_data_64xx __devinitdata = {
  1276. .variant = {
  1277. .nr_windows = 5,
  1278. .vidtcon = VIDTCON0,
  1279. .wincon = WINCON(0),
  1280. .winmap = WINxMAP(0),
  1281. .keycon = WKEYCON,
  1282. .osd = VIDOSD_BASE,
  1283. .osd_stride = 16,
  1284. .buf_start = VIDW_BUF_START(0),
  1285. .buf_size = VIDW_BUF_SIZE(0),
  1286. .buf_end = VIDW_BUF_END(0),
  1287. .palette = {
  1288. [0] = 0x400,
  1289. [1] = 0x800,
  1290. [2] = 0x300,
  1291. [3] = 0x320,
  1292. [4] = 0x340,
  1293. },
  1294. .has_prtcon = 1,
  1295. },
  1296. .win[0] = &s3c_fb_data_64xx_wins[0],
  1297. .win[1] = &s3c_fb_data_64xx_wins[1],
  1298. .win[2] = &s3c_fb_data_64xx_wins[2],
  1299. .win[3] = &s3c_fb_data_64xx_wins[3],
  1300. .win[4] = &s3c_fb_data_64xx_wins[4],
  1301. };
  1302. static struct s3c_fb_driverdata s3c_fb_data_s5pc100 __devinitdata = {
  1303. .variant = {
  1304. .nr_windows = 5,
  1305. .vidtcon = VIDTCON0,
  1306. .wincon = WINCON(0),
  1307. .winmap = WINxMAP(0),
  1308. .keycon = WKEYCON,
  1309. .osd = VIDOSD_BASE,
  1310. .osd_stride = 16,
  1311. .buf_start = VIDW_BUF_START(0),
  1312. .buf_size = VIDW_BUF_SIZE(0),
  1313. .buf_end = VIDW_BUF_END(0),
  1314. .palette = {
  1315. [0] = 0x2400,
  1316. [1] = 0x2800,
  1317. [2] = 0x2c00,
  1318. [3] = 0x3000,
  1319. [4] = 0x3400,
  1320. },
  1321. .has_prtcon = 1,
  1322. },
  1323. .win[0] = &s3c_fb_data_64xx_wins[0],
  1324. .win[1] = &s3c_fb_data_64xx_wins[1],
  1325. .win[2] = &s3c_fb_data_64xx_wins[2],
  1326. .win[3] = &s3c_fb_data_64xx_wins[3],
  1327. .win[4] = &s3c_fb_data_64xx_wins[4],
  1328. };
  1329. static struct s3c_fb_driverdata s3c_fb_data_s5pv210 __devinitdata = {
  1330. .variant = {
  1331. .nr_windows = 5,
  1332. .vidtcon = VIDTCON0,
  1333. .wincon = WINCON(0),
  1334. .winmap = WINxMAP(0),
  1335. .keycon = WKEYCON,
  1336. .osd = VIDOSD_BASE,
  1337. .osd_stride = 16,
  1338. .buf_start = VIDW_BUF_START(0),
  1339. .buf_size = VIDW_BUF_SIZE(0),
  1340. .buf_end = VIDW_BUF_END(0),
  1341. .palette = {
  1342. [0] = 0x2400,
  1343. [1] = 0x2800,
  1344. [2] = 0x2c00,
  1345. [3] = 0x3000,
  1346. [4] = 0x3400,
  1347. },
  1348. .has_shadowcon = 1,
  1349. },
  1350. .win[0] = &s3c_fb_data_64xx_wins[0],
  1351. .win[1] = &s3c_fb_data_64xx_wins[1],
  1352. .win[2] = &s3c_fb_data_64xx_wins[2],
  1353. .win[3] = &s3c_fb_data_64xx_wins[3],
  1354. .win[4] = &s3c_fb_data_64xx_wins[4],
  1355. };
  1356. /* S3C2443/S3C2416 style hardware */
  1357. static struct s3c_fb_driverdata s3c_fb_data_s3c2443 __devinitdata = {
  1358. .variant = {
  1359. .nr_windows = 2,
  1360. .is_2443 = 1,
  1361. .vidtcon = 0x08,
  1362. .wincon = 0x14,
  1363. .winmap = 0xd0,
  1364. .keycon = 0xb0,
  1365. .osd = 0x28,
  1366. .osd_stride = 12,
  1367. .buf_start = 0x64,
  1368. .buf_size = 0x94,
  1369. .buf_end = 0x7c,
  1370. .palette = {
  1371. [0] = 0x400,
  1372. [1] = 0x800,
  1373. },
  1374. },
  1375. .win[0] = &(struct s3c_fb_win_variant) {
  1376. .palette_sz = 256,
  1377. .valid_bpp = VALID_BPP1248 | VALID_BPP(16) | VALID_BPP(24),
  1378. },
  1379. .win[1] = &(struct s3c_fb_win_variant) {
  1380. .has_osd_c = 1,
  1381. .has_osd_alpha = 1,
  1382. .palette_sz = 256,
  1383. .valid_bpp = (VALID_BPP1248 | VALID_BPP(16) |
  1384. VALID_BPP(18) | VALID_BPP(19) |
  1385. VALID_BPP(24) | VALID_BPP(25) |
  1386. VALID_BPP(28)),
  1387. },
  1388. };
  1389. static struct platform_device_id s3c_fb_driver_ids[] = {
  1390. {
  1391. .name = "s3c-fb",
  1392. .driver_data = (unsigned long)&s3c_fb_data_64xx,
  1393. }, {
  1394. .name = "s5pc100-fb",
  1395. .driver_data = (unsigned long)&s3c_fb_data_s5pc100,
  1396. }, {
  1397. .name = "s5pv210-fb",
  1398. .driver_data = (unsigned long)&s3c_fb_data_s5pv210,
  1399. }, {
  1400. .name = "s3c2443-fb",
  1401. .driver_data = (unsigned long)&s3c_fb_data_s3c2443,
  1402. },
  1403. {},
  1404. };
  1405. MODULE_DEVICE_TABLE(platform, s3c_fb_driver_ids);
  1406. static struct platform_driver s3c_fb_driver = {
  1407. .probe = s3c_fb_probe,
  1408. .remove = __devexit_p(s3c_fb_remove),
  1409. .suspend = s3c_fb_suspend,
  1410. .resume = s3c_fb_resume,
  1411. .id_table = s3c_fb_driver_ids,
  1412. .driver = {
  1413. .name = "s3c-fb",
  1414. .owner = THIS_MODULE,
  1415. },
  1416. };
  1417. static int __init s3c_fb_init(void)
  1418. {
  1419. return platform_driver_register(&s3c_fb_driver);
  1420. }
  1421. static void __exit s3c_fb_cleanup(void)
  1422. {
  1423. platform_driver_unregister(&s3c_fb_driver);
  1424. }
  1425. module_init(s3c_fb_init);
  1426. module_exit(s3c_fb_cleanup);
  1427. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  1428. MODULE_DESCRIPTION("Samsung S3C SoC Framebuffer driver");
  1429. MODULE_LICENSE("GPL");
  1430. MODULE_ALIAS("platform:s3c-fb");