s3c-fb.c 45 KB

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