fsl-diu-fb.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738
  1. /*
  2. * Copyright 2008 Freescale Semiconductor, Inc. All Rights Reserved.
  3. *
  4. * Freescale DIU Frame Buffer device driver
  5. *
  6. * Authors: Hongjun Chen <hong-jun.chen@freescale.com>
  7. * Paul Widmer <paul.widmer@freescale.com>
  8. * Srikanth Srinivasan <srikanth.srinivasan@freescale.com>
  9. * York Sun <yorksun@freescale.com>
  10. *
  11. * Based on imxfb.c Copyright (C) 2004 S.Hauer, Pengutronix
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License as published by the
  15. * Free Software Foundation; either version 2 of the License, or (at your
  16. * option) any later version.
  17. *
  18. */
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/errno.h>
  22. #include <linux/string.h>
  23. #include <linux/slab.h>
  24. #include <linux/fb.h>
  25. #include <linux/init.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/clk.h>
  30. #include <linux/uaccess.h>
  31. #include <linux/vmalloc.h>
  32. #include <linux/of_platform.h>
  33. #include <sysdev/fsl_soc.h>
  34. #include "fsl-diu-fb.h"
  35. /*
  36. * These parameters give default parameters
  37. * for video output 1024x768,
  38. * FIXME - change timing to proper amounts
  39. * hsync 31.5kHz, vsync 60Hz
  40. */
  41. static struct fb_videomode __devinitdata fsl_diu_default_mode = {
  42. .refresh = 60,
  43. .xres = 1024,
  44. .yres = 768,
  45. .pixclock = 15385,
  46. .left_margin = 160,
  47. .right_margin = 24,
  48. .upper_margin = 29,
  49. .lower_margin = 3,
  50. .hsync_len = 136,
  51. .vsync_len = 6,
  52. .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  53. .vmode = FB_VMODE_NONINTERLACED
  54. };
  55. static struct fb_videomode __devinitdata fsl_diu_mode_db[] = {
  56. {
  57. .name = "1024x768-60",
  58. .refresh = 60,
  59. .xres = 1024,
  60. .yres = 768,
  61. .pixclock = 15385,
  62. .left_margin = 160,
  63. .right_margin = 24,
  64. .upper_margin = 29,
  65. .lower_margin = 3,
  66. .hsync_len = 136,
  67. .vsync_len = 6,
  68. .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  69. .vmode = FB_VMODE_NONINTERLACED
  70. },
  71. {
  72. .name = "1024x768-70",
  73. .refresh = 70,
  74. .xres = 1024,
  75. .yres = 768,
  76. .pixclock = 16886,
  77. .left_margin = 3,
  78. .right_margin = 3,
  79. .upper_margin = 2,
  80. .lower_margin = 2,
  81. .hsync_len = 40,
  82. .vsync_len = 18,
  83. .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  84. .vmode = FB_VMODE_NONINTERLACED
  85. },
  86. {
  87. .name = "1024x768-75",
  88. .refresh = 75,
  89. .xres = 1024,
  90. .yres = 768,
  91. .pixclock = 15009,
  92. .left_margin = 3,
  93. .right_margin = 3,
  94. .upper_margin = 2,
  95. .lower_margin = 2,
  96. .hsync_len = 80,
  97. .vsync_len = 32,
  98. .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  99. .vmode = FB_VMODE_NONINTERLACED
  100. },
  101. {
  102. .name = "1280x1024-60",
  103. .refresh = 60,
  104. .xres = 1280,
  105. .yres = 1024,
  106. .pixclock = 9375,
  107. .left_margin = 38,
  108. .right_margin = 128,
  109. .upper_margin = 2,
  110. .lower_margin = 7,
  111. .hsync_len = 216,
  112. .vsync_len = 37,
  113. .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  114. .vmode = FB_VMODE_NONINTERLACED
  115. },
  116. {
  117. .name = "1280x1024-70",
  118. .refresh = 70,
  119. .xres = 1280,
  120. .yres = 1024,
  121. .pixclock = 9380,
  122. .left_margin = 6,
  123. .right_margin = 6,
  124. .upper_margin = 4,
  125. .lower_margin = 4,
  126. .hsync_len = 60,
  127. .vsync_len = 94,
  128. .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  129. .vmode = FB_VMODE_NONINTERLACED
  130. },
  131. {
  132. .name = "1280x1024-75",
  133. .refresh = 75,
  134. .xres = 1280,
  135. .yres = 1024,
  136. .pixclock = 9380,
  137. .left_margin = 6,
  138. .right_margin = 6,
  139. .upper_margin = 4,
  140. .lower_margin = 4,
  141. .hsync_len = 60,
  142. .vsync_len = 15,
  143. .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  144. .vmode = FB_VMODE_NONINTERLACED
  145. },
  146. {
  147. .name = "320x240", /* for AOI only */
  148. .refresh = 60,
  149. .xres = 320,
  150. .yres = 240,
  151. .pixclock = 15385,
  152. .left_margin = 0,
  153. .right_margin = 0,
  154. .upper_margin = 0,
  155. .lower_margin = 0,
  156. .hsync_len = 0,
  157. .vsync_len = 0,
  158. .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  159. .vmode = FB_VMODE_NONINTERLACED
  160. },
  161. {
  162. .name = "1280x480-60",
  163. .refresh = 60,
  164. .xres = 1280,
  165. .yres = 480,
  166. .pixclock = 18939,
  167. .left_margin = 353,
  168. .right_margin = 47,
  169. .upper_margin = 39,
  170. .lower_margin = 4,
  171. .hsync_len = 8,
  172. .vsync_len = 2,
  173. .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  174. .vmode = FB_VMODE_NONINTERLACED
  175. },
  176. };
  177. static char *fb_mode = "1024x768-32@60";
  178. static unsigned long default_bpp = 32;
  179. static int monitor_port;
  180. #if defined(CONFIG_NOT_COHERENT_CACHE)
  181. static u8 *coherence_data;
  182. static size_t coherence_data_size;
  183. static unsigned int d_cache_line_size;
  184. #endif
  185. static DEFINE_SPINLOCK(diu_lock);
  186. struct fsl_diu_data {
  187. struct fb_info *fsl_diu_info[FSL_AOI_NUM - 1];
  188. /*FSL_AOI_NUM has one dummy AOI */
  189. struct device_attribute dev_attr;
  190. struct diu_ad *dummy_ad;
  191. void *dummy_aoi_virt;
  192. unsigned int irq;
  193. int fb_enabled;
  194. int monitor_port;
  195. };
  196. struct mfb_info {
  197. int index;
  198. int type;
  199. char *id;
  200. int registered;
  201. int blank;
  202. unsigned long pseudo_palette[16];
  203. struct diu_ad *ad;
  204. int cursor_reset;
  205. unsigned char g_alpha;
  206. unsigned int count;
  207. int x_aoi_d; /* aoi display x offset to physical screen */
  208. int y_aoi_d; /* aoi display y offset to physical screen */
  209. struct fsl_diu_data *parent;
  210. };
  211. static struct mfb_info mfb_template[] = {
  212. { /* AOI 0 for plane 0 */
  213. .index = 0,
  214. .type = MFB_TYPE_OUTPUT,
  215. .id = "Panel0",
  216. .registered = 0,
  217. .count = 0,
  218. .x_aoi_d = 0,
  219. .y_aoi_d = 0,
  220. },
  221. { /* AOI 0 for plane 1 */
  222. .index = 1,
  223. .type = MFB_TYPE_OUTPUT,
  224. .id = "Panel1 AOI0",
  225. .registered = 0,
  226. .g_alpha = 0xff,
  227. .count = 0,
  228. .x_aoi_d = 0,
  229. .y_aoi_d = 0,
  230. },
  231. { /* AOI 1 for plane 1 */
  232. .index = 2,
  233. .type = MFB_TYPE_OUTPUT,
  234. .id = "Panel1 AOI1",
  235. .registered = 0,
  236. .g_alpha = 0xff,
  237. .count = 0,
  238. .x_aoi_d = 0,
  239. .y_aoi_d = 480,
  240. },
  241. { /* AOI 0 for plane 2 */
  242. .index = 3,
  243. .type = MFB_TYPE_OUTPUT,
  244. .id = "Panel2 AOI0",
  245. .registered = 0,
  246. .g_alpha = 0xff,
  247. .count = 0,
  248. .x_aoi_d = 640,
  249. .y_aoi_d = 0,
  250. },
  251. { /* AOI 1 for plane 2 */
  252. .index = 4,
  253. .type = MFB_TYPE_OUTPUT,
  254. .id = "Panel2 AOI1",
  255. .registered = 0,
  256. .g_alpha = 0xff,
  257. .count = 0,
  258. .x_aoi_d = 640,
  259. .y_aoi_d = 480,
  260. },
  261. };
  262. static struct diu_hw dr = {
  263. .mode = MFB_MODE1,
  264. .reg_lock = __SPIN_LOCK_UNLOCKED(diu_hw.reg_lock),
  265. };
  266. static struct diu_pool pool;
  267. /**
  268. * fsl_diu_alloc - allocate memory for the DIU
  269. * @size: number of bytes to allocate
  270. * @param: returned physical address of memory
  271. *
  272. * This function allocates a physically-contiguous block of memory.
  273. */
  274. static void *fsl_diu_alloc(size_t size, phys_addr_t *phys)
  275. {
  276. void *virt;
  277. pr_debug("size=%zu\n", size);
  278. virt = alloc_pages_exact(size, GFP_DMA | __GFP_ZERO);
  279. if (virt) {
  280. *phys = virt_to_phys(virt);
  281. pr_debug("virt=%p phys=%llx\n", virt,
  282. (unsigned long long)*phys);
  283. }
  284. return virt;
  285. }
  286. /**
  287. * fsl_diu_free - release DIU memory
  288. * @virt: pointer returned by fsl_diu_alloc()
  289. * @size: number of bytes allocated by fsl_diu_alloc()
  290. *
  291. * This function releases memory allocated by fsl_diu_alloc().
  292. */
  293. static void fsl_diu_free(void *virt, size_t size)
  294. {
  295. pr_debug("virt=%p size=%zu\n", virt, size);
  296. if (virt && size)
  297. free_pages_exact(virt, size);
  298. }
  299. static int fsl_diu_enable_panel(struct fb_info *info)
  300. {
  301. struct mfb_info *pmfbi, *cmfbi, *mfbi = info->par;
  302. struct diu *hw = dr.diu_reg;
  303. struct diu_ad *ad = mfbi->ad;
  304. struct fsl_diu_data *machine_data = mfbi->parent;
  305. int res = 0;
  306. pr_debug("enable_panel index %d\n", mfbi->index);
  307. if (mfbi->type != MFB_TYPE_OFF) {
  308. switch (mfbi->index) {
  309. case 0: /* plane 0 */
  310. if (hw->desc[0] != ad->paddr)
  311. out_be32(&hw->desc[0], ad->paddr);
  312. break;
  313. case 1: /* plane 1 AOI 0 */
  314. cmfbi = machine_data->fsl_diu_info[2]->par;
  315. if (hw->desc[1] != ad->paddr) { /* AOI0 closed */
  316. if (cmfbi->count > 0) /* AOI1 open */
  317. ad->next_ad =
  318. cpu_to_le32(cmfbi->ad->paddr);
  319. else
  320. ad->next_ad = 0;
  321. out_be32(&hw->desc[1], ad->paddr);
  322. }
  323. break;
  324. case 3: /* plane 2 AOI 0 */
  325. cmfbi = machine_data->fsl_diu_info[4]->par;
  326. if (hw->desc[2] != ad->paddr) { /* AOI0 closed */
  327. if (cmfbi->count > 0) /* AOI1 open */
  328. ad->next_ad =
  329. cpu_to_le32(cmfbi->ad->paddr);
  330. else
  331. ad->next_ad = 0;
  332. out_be32(&hw->desc[2], ad->paddr);
  333. }
  334. break;
  335. case 2: /* plane 1 AOI 1 */
  336. pmfbi = machine_data->fsl_diu_info[1]->par;
  337. ad->next_ad = 0;
  338. if (hw->desc[1] == machine_data->dummy_ad->paddr)
  339. out_be32(&hw->desc[1], ad->paddr);
  340. else /* AOI0 open */
  341. pmfbi->ad->next_ad = cpu_to_le32(ad->paddr);
  342. break;
  343. case 4: /* plane 2 AOI 1 */
  344. pmfbi = machine_data->fsl_diu_info[3]->par;
  345. ad->next_ad = 0;
  346. if (hw->desc[2] == machine_data->dummy_ad->paddr)
  347. out_be32(&hw->desc[2], ad->paddr);
  348. else /* AOI0 was open */
  349. pmfbi->ad->next_ad = cpu_to_le32(ad->paddr);
  350. break;
  351. default:
  352. res = -EINVAL;
  353. break;
  354. }
  355. } else
  356. res = -EINVAL;
  357. return res;
  358. }
  359. static int fsl_diu_disable_panel(struct fb_info *info)
  360. {
  361. struct mfb_info *pmfbi, *cmfbi, *mfbi = info->par;
  362. struct diu *hw = dr.diu_reg;
  363. struct diu_ad *ad = mfbi->ad;
  364. struct fsl_diu_data *machine_data = mfbi->parent;
  365. int res = 0;
  366. switch (mfbi->index) {
  367. case 0: /* plane 0 */
  368. if (hw->desc[0] != machine_data->dummy_ad->paddr)
  369. out_be32(&hw->desc[0],
  370. machine_data->dummy_ad->paddr);
  371. break;
  372. case 1: /* plane 1 AOI 0 */
  373. cmfbi = machine_data->fsl_diu_info[2]->par;
  374. if (cmfbi->count > 0) /* AOI1 is open */
  375. out_be32(&hw->desc[1], cmfbi->ad->paddr);
  376. /* move AOI1 to the first */
  377. else /* AOI1 was closed */
  378. out_be32(&hw->desc[1],
  379. machine_data->dummy_ad->paddr);
  380. /* close AOI 0 */
  381. break;
  382. case 3: /* plane 2 AOI 0 */
  383. cmfbi = machine_data->fsl_diu_info[4]->par;
  384. if (cmfbi->count > 0) /* AOI1 is open */
  385. out_be32(&hw->desc[2], cmfbi->ad->paddr);
  386. /* move AOI1 to the first */
  387. else /* AOI1 was closed */
  388. out_be32(&hw->desc[2],
  389. machine_data->dummy_ad->paddr);
  390. /* close AOI 0 */
  391. break;
  392. case 2: /* plane 1 AOI 1 */
  393. pmfbi = machine_data->fsl_diu_info[1]->par;
  394. if (hw->desc[1] != ad->paddr) {
  395. /* AOI1 is not the first in the chain */
  396. if (pmfbi->count > 0)
  397. /* AOI0 is open, must be the first */
  398. pmfbi->ad->next_ad = 0;
  399. } else /* AOI1 is the first in the chain */
  400. out_be32(&hw->desc[1], machine_data->dummy_ad->paddr);
  401. /* close AOI 1 */
  402. break;
  403. case 4: /* plane 2 AOI 1 */
  404. pmfbi = machine_data->fsl_diu_info[3]->par;
  405. if (hw->desc[2] != ad->paddr) {
  406. /* AOI1 is not the first in the chain */
  407. if (pmfbi->count > 0)
  408. /* AOI0 is open, must be the first */
  409. pmfbi->ad->next_ad = 0;
  410. } else /* AOI1 is the first in the chain */
  411. out_be32(&hw->desc[2], machine_data->dummy_ad->paddr);
  412. /* close AOI 1 */
  413. break;
  414. default:
  415. res = -EINVAL;
  416. break;
  417. }
  418. return res;
  419. }
  420. static void enable_lcdc(struct fb_info *info)
  421. {
  422. struct diu *hw = dr.diu_reg;
  423. struct mfb_info *mfbi = info->par;
  424. struct fsl_diu_data *machine_data = mfbi->parent;
  425. if (!machine_data->fb_enabled) {
  426. out_be32(&hw->diu_mode, dr.mode);
  427. machine_data->fb_enabled++;
  428. }
  429. }
  430. static void disable_lcdc(struct fb_info *info)
  431. {
  432. struct diu *hw = dr.diu_reg;
  433. struct mfb_info *mfbi = info->par;
  434. struct fsl_diu_data *machine_data = mfbi->parent;
  435. if (machine_data->fb_enabled) {
  436. out_be32(&hw->diu_mode, 0);
  437. machine_data->fb_enabled = 0;
  438. }
  439. }
  440. static void adjust_aoi_size_position(struct fb_var_screeninfo *var,
  441. struct fb_info *info)
  442. {
  443. struct mfb_info *lower_aoi_mfbi, *upper_aoi_mfbi, *mfbi = info->par;
  444. struct fsl_diu_data *machine_data = mfbi->parent;
  445. int available_height, upper_aoi_bottom, index = mfbi->index;
  446. int lower_aoi_is_open, upper_aoi_is_open;
  447. __u32 base_plane_width, base_plane_height, upper_aoi_height;
  448. base_plane_width = machine_data->fsl_diu_info[0]->var.xres;
  449. base_plane_height = machine_data->fsl_diu_info[0]->var.yres;
  450. if (mfbi->x_aoi_d < 0)
  451. mfbi->x_aoi_d = 0;
  452. if (mfbi->y_aoi_d < 0)
  453. mfbi->y_aoi_d = 0;
  454. switch (index) {
  455. case 0:
  456. if (mfbi->x_aoi_d != 0)
  457. mfbi->x_aoi_d = 0;
  458. if (mfbi->y_aoi_d != 0)
  459. mfbi->y_aoi_d = 0;
  460. break;
  461. case 1: /* AOI 0 */
  462. case 3:
  463. lower_aoi_mfbi = machine_data->fsl_diu_info[index+1]->par;
  464. lower_aoi_is_open = lower_aoi_mfbi->count > 0 ? 1 : 0;
  465. if (var->xres > base_plane_width)
  466. var->xres = base_plane_width;
  467. if ((mfbi->x_aoi_d + var->xres) > base_plane_width)
  468. mfbi->x_aoi_d = base_plane_width - var->xres;
  469. if (lower_aoi_is_open)
  470. available_height = lower_aoi_mfbi->y_aoi_d;
  471. else
  472. available_height = base_plane_height;
  473. if (var->yres > available_height)
  474. var->yres = available_height;
  475. if ((mfbi->y_aoi_d + var->yres) > available_height)
  476. mfbi->y_aoi_d = available_height - var->yres;
  477. break;
  478. case 2: /* AOI 1 */
  479. case 4:
  480. upper_aoi_mfbi = machine_data->fsl_diu_info[index-1]->par;
  481. upper_aoi_height =
  482. machine_data->fsl_diu_info[index-1]->var.yres;
  483. upper_aoi_bottom = upper_aoi_mfbi->y_aoi_d + upper_aoi_height;
  484. upper_aoi_is_open = upper_aoi_mfbi->count > 0 ? 1 : 0;
  485. if (var->xres > base_plane_width)
  486. var->xres = base_plane_width;
  487. if ((mfbi->x_aoi_d + var->xres) > base_plane_width)
  488. mfbi->x_aoi_d = base_plane_width - var->xres;
  489. if (mfbi->y_aoi_d < 0)
  490. mfbi->y_aoi_d = 0;
  491. if (upper_aoi_is_open) {
  492. if (mfbi->y_aoi_d < upper_aoi_bottom)
  493. mfbi->y_aoi_d = upper_aoi_bottom;
  494. available_height = base_plane_height
  495. - upper_aoi_bottom;
  496. } else
  497. available_height = base_plane_height;
  498. if (var->yres > available_height)
  499. var->yres = available_height;
  500. if ((mfbi->y_aoi_d + var->yres) > base_plane_height)
  501. mfbi->y_aoi_d = base_plane_height - var->yres;
  502. break;
  503. }
  504. }
  505. /*
  506. * Checks to see if the hardware supports the state requested by var passed
  507. * in. This function does not alter the hardware state! If the var passed in
  508. * is slightly off by what the hardware can support then we alter the var
  509. * PASSED in to what we can do. If the hardware doesn't support mode change
  510. * a -EINVAL will be returned by the upper layers.
  511. */
  512. static int fsl_diu_check_var(struct fb_var_screeninfo *var,
  513. struct fb_info *info)
  514. {
  515. unsigned long htotal, vtotal;
  516. pr_debug("check_var xres: %d\n", var->xres);
  517. pr_debug("check_var yres: %d\n", var->yres);
  518. if (var->xres_virtual < var->xres)
  519. var->xres_virtual = var->xres;
  520. if (var->yres_virtual < var->yres)
  521. var->yres_virtual = var->yres;
  522. if (var->xoffset < 0)
  523. var->xoffset = 0;
  524. if (var->yoffset < 0)
  525. var->yoffset = 0;
  526. if (var->xoffset + info->var.xres > info->var.xres_virtual)
  527. var->xoffset = info->var.xres_virtual - info->var.xres;
  528. if (var->yoffset + info->var.yres > info->var.yres_virtual)
  529. var->yoffset = info->var.yres_virtual - info->var.yres;
  530. if ((var->bits_per_pixel != 32) && (var->bits_per_pixel != 24) &&
  531. (var->bits_per_pixel != 16))
  532. var->bits_per_pixel = default_bpp;
  533. switch (var->bits_per_pixel) {
  534. case 16:
  535. var->red.length = 5;
  536. var->red.offset = 11;
  537. var->red.msb_right = 0;
  538. var->green.length = 6;
  539. var->green.offset = 5;
  540. var->green.msb_right = 0;
  541. var->blue.length = 5;
  542. var->blue.offset = 0;
  543. var->blue.msb_right = 0;
  544. var->transp.length = 0;
  545. var->transp.offset = 0;
  546. var->transp.msb_right = 0;
  547. break;
  548. case 24:
  549. var->red.length = 8;
  550. var->red.offset = 0;
  551. var->red.msb_right = 0;
  552. var->green.length = 8;
  553. var->green.offset = 8;
  554. var->green.msb_right = 0;
  555. var->blue.length = 8;
  556. var->blue.offset = 16;
  557. var->blue.msb_right = 0;
  558. var->transp.length = 0;
  559. var->transp.offset = 0;
  560. var->transp.msb_right = 0;
  561. break;
  562. case 32:
  563. var->red.length = 8;
  564. var->red.offset = 16;
  565. var->red.msb_right = 0;
  566. var->green.length = 8;
  567. var->green.offset = 8;
  568. var->green.msb_right = 0;
  569. var->blue.length = 8;
  570. var->blue.offset = 0;
  571. var->blue.msb_right = 0;
  572. var->transp.length = 8;
  573. var->transp.offset = 24;
  574. var->transp.msb_right = 0;
  575. break;
  576. }
  577. /* If the pixclock is below the minimum spec'd value then set to
  578. * refresh rate for 60Hz since this is supported by most monitors.
  579. * Refer to Documentation/fb/ for calculations.
  580. */
  581. if ((var->pixclock < MIN_PIX_CLK) || (var->pixclock > MAX_PIX_CLK)) {
  582. htotal = var->xres + var->right_margin + var->hsync_len +
  583. var->left_margin;
  584. vtotal = var->yres + var->lower_margin + var->vsync_len +
  585. var->upper_margin;
  586. var->pixclock = (vtotal * htotal * 6UL) / 100UL;
  587. var->pixclock = KHZ2PICOS(var->pixclock);
  588. pr_debug("pixclock set for 60Hz refresh = %u ps\n",
  589. var->pixclock);
  590. }
  591. var->height = -1;
  592. var->width = -1;
  593. var->grayscale = 0;
  594. /* Copy nonstd field to/from sync for fbset usage */
  595. var->sync |= var->nonstd;
  596. var->nonstd |= var->sync;
  597. adjust_aoi_size_position(var, info);
  598. return 0;
  599. }
  600. static void set_fix(struct fb_info *info)
  601. {
  602. struct fb_fix_screeninfo *fix = &info->fix;
  603. struct fb_var_screeninfo *var = &info->var;
  604. struct mfb_info *mfbi = info->par;
  605. strncpy(fix->id, mfbi->id, strlen(mfbi->id));
  606. fix->line_length = var->xres_virtual * var->bits_per_pixel / 8;
  607. fix->type = FB_TYPE_PACKED_PIXELS;
  608. fix->accel = FB_ACCEL_NONE;
  609. fix->visual = FB_VISUAL_TRUECOLOR;
  610. fix->xpanstep = 1;
  611. fix->ypanstep = 1;
  612. }
  613. static void update_lcdc(struct fb_info *info)
  614. {
  615. struct fb_var_screeninfo *var = &info->var;
  616. struct mfb_info *mfbi = info->par;
  617. struct fsl_diu_data *machine_data = mfbi->parent;
  618. struct diu *hw;
  619. int i, j;
  620. char __iomem *cursor_base, *gamma_table_base;
  621. u32 temp;
  622. hw = dr.diu_reg;
  623. if (mfbi->type == MFB_TYPE_OFF) {
  624. fsl_diu_disable_panel(info);
  625. return;
  626. }
  627. diu_ops.set_monitor_port(machine_data->monitor_port);
  628. gamma_table_base = pool.gamma.vaddr;
  629. cursor_base = pool.cursor.vaddr;
  630. /* Prep for DIU init - gamma table, cursor table */
  631. for (i = 0; i <= 2; i++)
  632. for (j = 0; j <= 255; j++)
  633. *gamma_table_base++ = j;
  634. diu_ops.set_gamma_table(machine_data->monitor_port, pool.gamma.vaddr);
  635. pr_debug("update-lcdc: HW - %p\n Disabling DIU\n", hw);
  636. disable_lcdc(info);
  637. /* Program DIU registers */
  638. out_be32(&hw->gamma, pool.gamma.paddr);
  639. out_be32(&hw->cursor, pool.cursor.paddr);
  640. out_be32(&hw->bgnd, 0x007F7F7F); /* BGND */
  641. out_be32(&hw->bgnd_wb, 0); /* BGND_WB */
  642. out_be32(&hw->disp_size, (var->yres << 16 | var->xres));
  643. /* DISP SIZE */
  644. pr_debug("DIU xres: %d\n", var->xres);
  645. pr_debug("DIU yres: %d\n", var->yres);
  646. out_be32(&hw->wb_size, 0); /* WB SIZE */
  647. out_be32(&hw->wb_mem_addr, 0); /* WB MEM ADDR */
  648. /* Horizontal and vertical configuration register */
  649. temp = var->left_margin << 22 | /* BP_H */
  650. var->hsync_len << 11 | /* PW_H */
  651. var->right_margin; /* FP_H */
  652. out_be32(&hw->hsyn_para, temp);
  653. temp = var->upper_margin << 22 | /* BP_V */
  654. var->vsync_len << 11 | /* PW_V */
  655. var->lower_margin; /* FP_V */
  656. out_be32(&hw->vsyn_para, temp);
  657. pr_debug("DIU right_margin - %d\n", var->right_margin);
  658. pr_debug("DIU left_margin - %d\n", var->left_margin);
  659. pr_debug("DIU hsync_len - %d\n", var->hsync_len);
  660. pr_debug("DIU upper_margin - %d\n", var->upper_margin);
  661. pr_debug("DIU lower_margin - %d\n", var->lower_margin);
  662. pr_debug("DIU vsync_len - %d\n", var->vsync_len);
  663. pr_debug("DIU HSYNC - 0x%08x\n", hw->hsyn_para);
  664. pr_debug("DIU VSYNC - 0x%08x\n", hw->vsyn_para);
  665. diu_ops.set_pixel_clock(var->pixclock);
  666. out_be32(&hw->syn_pol, 0); /* SYNC SIGNALS POLARITY */
  667. out_be32(&hw->thresholds, 0x00037800); /* The Thresholds */
  668. out_be32(&hw->int_status, 0); /* INTERRUPT STATUS */
  669. out_be32(&hw->plut, 0x01F5F666);
  670. /* Enable the DIU */
  671. enable_lcdc(info);
  672. }
  673. static int map_video_memory(struct fb_info *info)
  674. {
  675. phys_addr_t phys;
  676. u32 smem_len = info->fix.line_length * info->var.yres_virtual;
  677. pr_debug("info->var.xres_virtual = %d\n", info->var.xres_virtual);
  678. pr_debug("info->var.yres_virtual = %d\n", info->var.yres_virtual);
  679. pr_debug("info->fix.line_length = %d\n", info->fix.line_length);
  680. pr_debug("MAP_VIDEO_MEMORY: smem_len = %u\n", smem_len);
  681. info->screen_base = fsl_diu_alloc(smem_len, &phys);
  682. if (info->screen_base == NULL) {
  683. printk(KERN_ERR "Unable to allocate fb memory\n");
  684. return -ENOMEM;
  685. }
  686. mutex_lock(&info->mm_lock);
  687. info->fix.smem_start = (unsigned long) phys;
  688. info->fix.smem_len = smem_len;
  689. mutex_unlock(&info->mm_lock);
  690. info->screen_size = info->fix.smem_len;
  691. pr_debug("Allocated fb @ paddr=0x%08lx, size=%d.\n",
  692. info->fix.smem_start, info->fix.smem_len);
  693. pr_debug("screen base %p\n", info->screen_base);
  694. return 0;
  695. }
  696. static void unmap_video_memory(struct fb_info *info)
  697. {
  698. fsl_diu_free(info->screen_base, info->fix.smem_len);
  699. mutex_lock(&info->mm_lock);
  700. info->screen_base = NULL;
  701. info->fix.smem_start = 0;
  702. info->fix.smem_len = 0;
  703. mutex_unlock(&info->mm_lock);
  704. }
  705. /*
  706. * Using the fb_var_screeninfo in fb_info we set the aoi of this
  707. * particular framebuffer. It is a light version of fsl_diu_set_par.
  708. */
  709. static int fsl_diu_set_aoi(struct fb_info *info)
  710. {
  711. struct fb_var_screeninfo *var = &info->var;
  712. struct mfb_info *mfbi = info->par;
  713. struct diu_ad *ad = mfbi->ad;
  714. /* AOI should not be greater than display size */
  715. ad->offset_xyi = cpu_to_le32((var->yoffset << 16) | var->xoffset);
  716. ad->offset_xyd = cpu_to_le32((mfbi->y_aoi_d << 16) | mfbi->x_aoi_d);
  717. return 0;
  718. }
  719. /*
  720. * Using the fb_var_screeninfo in fb_info we set the resolution of this
  721. * particular framebuffer. This function alters the fb_fix_screeninfo stored
  722. * in fb_info. It does not alter var in fb_info since we are using that
  723. * data. This means we depend on the data in var inside fb_info to be
  724. * supported by the hardware. fsl_diu_check_var is always called before
  725. * fsl_diu_set_par to ensure this.
  726. */
  727. static int fsl_diu_set_par(struct fb_info *info)
  728. {
  729. unsigned long len;
  730. struct fb_var_screeninfo *var = &info->var;
  731. struct mfb_info *mfbi = info->par;
  732. struct fsl_diu_data *machine_data = mfbi->parent;
  733. struct diu_ad *ad = mfbi->ad;
  734. struct diu *hw;
  735. hw = dr.diu_reg;
  736. set_fix(info);
  737. mfbi->cursor_reset = 1;
  738. len = info->var.yres_virtual * info->fix.line_length;
  739. /* Alloc & dealloc each time resolution/bpp change */
  740. if (len != info->fix.smem_len) {
  741. if (info->fix.smem_start)
  742. unmap_video_memory(info);
  743. pr_debug("SET PAR: smem_len = %d\n", info->fix.smem_len);
  744. /* Memory allocation for framebuffer */
  745. if (map_video_memory(info)) {
  746. printk(KERN_ERR "Unable to allocate fb memory 1\n");
  747. return -ENOMEM;
  748. }
  749. }
  750. ad->pix_fmt =
  751. diu_ops.get_pixel_format(var->bits_per_pixel,
  752. machine_data->monitor_port);
  753. ad->addr = cpu_to_le32(info->fix.smem_start);
  754. ad->src_size_g_alpha = cpu_to_le32((var->yres_virtual << 12) |
  755. var->xres_virtual) | mfbi->g_alpha;
  756. /* AOI should not be greater than display size */
  757. ad->aoi_size = cpu_to_le32((var->yres << 16) | var->xres);
  758. ad->offset_xyi = cpu_to_le32((var->yoffset << 16) | var->xoffset);
  759. ad->offset_xyd = cpu_to_le32((mfbi->y_aoi_d << 16) | mfbi->x_aoi_d);
  760. /* Disable chroma keying function */
  761. ad->ckmax_r = 0;
  762. ad->ckmax_g = 0;
  763. ad->ckmax_b = 0;
  764. ad->ckmin_r = 255;
  765. ad->ckmin_g = 255;
  766. ad->ckmin_b = 255;
  767. if (mfbi->index == 0)
  768. update_lcdc(info);
  769. return 0;
  770. }
  771. static inline __u32 CNVT_TOHW(__u32 val, __u32 width)
  772. {
  773. return ((val<<width) + 0x7FFF - val)>>16;
  774. }
  775. /*
  776. * Set a single color register. The values supplied have a 16 bit magnitude
  777. * which needs to be scaled in this function for the hardware. Things to take
  778. * into consideration are how many color registers, if any, are supported with
  779. * the current color visual. With truecolor mode no color palettes are
  780. * supported. Here a psuedo palette is created which we store the value in
  781. * pseudo_palette in struct fb_info. For pseudocolor mode we have a limited
  782. * color palette.
  783. */
  784. static int fsl_diu_setcolreg(unsigned regno, unsigned red, unsigned green,
  785. unsigned blue, unsigned transp, struct fb_info *info)
  786. {
  787. int ret = 1;
  788. /*
  789. * If greyscale is true, then we convert the RGB value
  790. * to greyscale no matter what visual we are using.
  791. */
  792. if (info->var.grayscale)
  793. red = green = blue = (19595 * red + 38470 * green +
  794. 7471 * blue) >> 16;
  795. switch (info->fix.visual) {
  796. case FB_VISUAL_TRUECOLOR:
  797. /*
  798. * 16-bit True Colour. We encode the RGB value
  799. * according to the RGB bitfield information.
  800. */
  801. if (regno < 16) {
  802. u32 *pal = info->pseudo_palette;
  803. u32 v;
  804. red = CNVT_TOHW(red, info->var.red.length);
  805. green = CNVT_TOHW(green, info->var.green.length);
  806. blue = CNVT_TOHW(blue, info->var.blue.length);
  807. transp = CNVT_TOHW(transp, info->var.transp.length);
  808. v = (red << info->var.red.offset) |
  809. (green << info->var.green.offset) |
  810. (blue << info->var.blue.offset) |
  811. (transp << info->var.transp.offset);
  812. pal[regno] = v;
  813. ret = 0;
  814. }
  815. break;
  816. case FB_VISUAL_STATIC_PSEUDOCOLOR:
  817. case FB_VISUAL_PSEUDOCOLOR:
  818. break;
  819. }
  820. return ret;
  821. }
  822. /*
  823. * Pan (or wrap, depending on the `vmode' field) the display using the
  824. * 'xoffset' and 'yoffset' fields of the 'var' structure. If the values
  825. * don't fit, return -EINVAL.
  826. */
  827. static int fsl_diu_pan_display(struct fb_var_screeninfo *var,
  828. struct fb_info *info)
  829. {
  830. if ((info->var.xoffset == var->xoffset) &&
  831. (info->var.yoffset == var->yoffset))
  832. return 0; /* No change, do nothing */
  833. if (var->xoffset < 0 || var->yoffset < 0
  834. || var->xoffset + info->var.xres > info->var.xres_virtual
  835. || var->yoffset + info->var.yres > info->var.yres_virtual)
  836. return -EINVAL;
  837. info->var.xoffset = var->xoffset;
  838. info->var.yoffset = var->yoffset;
  839. if (var->vmode & FB_VMODE_YWRAP)
  840. info->var.vmode |= FB_VMODE_YWRAP;
  841. else
  842. info->var.vmode &= ~FB_VMODE_YWRAP;
  843. fsl_diu_set_aoi(info);
  844. return 0;
  845. }
  846. /*
  847. * Blank the screen if blank_mode != 0, else unblank. Return 0 if blanking
  848. * succeeded, != 0 if un-/blanking failed.
  849. * blank_mode == 2: suspend vsync
  850. * blank_mode == 3: suspend hsync
  851. * blank_mode == 4: powerdown
  852. */
  853. static int fsl_diu_blank(int blank_mode, struct fb_info *info)
  854. {
  855. struct mfb_info *mfbi = info->par;
  856. mfbi->blank = blank_mode;
  857. switch (blank_mode) {
  858. case FB_BLANK_VSYNC_SUSPEND:
  859. case FB_BLANK_HSYNC_SUSPEND:
  860. /* FIXME: fixes to enable_panel and enable lcdc needed */
  861. case FB_BLANK_NORMAL:
  862. /* fsl_diu_disable_panel(info);*/
  863. break;
  864. case FB_BLANK_POWERDOWN:
  865. /* disable_lcdc(info); */
  866. break;
  867. case FB_BLANK_UNBLANK:
  868. /* fsl_diu_enable_panel(info);*/
  869. break;
  870. }
  871. return 0;
  872. }
  873. static int fsl_diu_ioctl(struct fb_info *info, unsigned int cmd,
  874. unsigned long arg)
  875. {
  876. struct mfb_info *mfbi = info->par;
  877. struct diu_ad *ad = mfbi->ad;
  878. struct mfb_chroma_key ck;
  879. unsigned char global_alpha;
  880. struct aoi_display_offset aoi_d;
  881. __u32 pix_fmt;
  882. void __user *buf = (void __user *)arg;
  883. if (!arg)
  884. return -EINVAL;
  885. switch (cmd) {
  886. case MFB_SET_PIXFMT:
  887. if (copy_from_user(&pix_fmt, buf, sizeof(pix_fmt)))
  888. return -EFAULT;
  889. ad->pix_fmt = pix_fmt;
  890. pr_debug("Set pixel format to 0x%08x\n", ad->pix_fmt);
  891. break;
  892. case MFB_GET_PIXFMT:
  893. pix_fmt = ad->pix_fmt;
  894. if (copy_to_user(buf, &pix_fmt, sizeof(pix_fmt)))
  895. return -EFAULT;
  896. pr_debug("get pixel format 0x%08x\n", ad->pix_fmt);
  897. break;
  898. case MFB_SET_AOID:
  899. if (copy_from_user(&aoi_d, buf, sizeof(aoi_d)))
  900. return -EFAULT;
  901. mfbi->x_aoi_d = aoi_d.x_aoi_d;
  902. mfbi->y_aoi_d = aoi_d.y_aoi_d;
  903. pr_debug("set AOI display offset of index %d to (%d,%d)\n",
  904. mfbi->index, aoi_d.x_aoi_d, aoi_d.y_aoi_d);
  905. fsl_diu_check_var(&info->var, info);
  906. fsl_diu_set_aoi(info);
  907. break;
  908. case MFB_GET_AOID:
  909. aoi_d.x_aoi_d = mfbi->x_aoi_d;
  910. aoi_d.y_aoi_d = mfbi->y_aoi_d;
  911. if (copy_to_user(buf, &aoi_d, sizeof(aoi_d)))
  912. return -EFAULT;
  913. pr_debug("get AOI display offset of index %d (%d,%d)\n",
  914. mfbi->index, aoi_d.x_aoi_d, aoi_d.y_aoi_d);
  915. break;
  916. case MFB_GET_ALPHA:
  917. global_alpha = mfbi->g_alpha;
  918. if (copy_to_user(buf, &global_alpha, sizeof(global_alpha)))
  919. return -EFAULT;
  920. pr_debug("get global alpha of index %d\n", mfbi->index);
  921. break;
  922. case MFB_SET_ALPHA:
  923. /* set panel information */
  924. if (copy_from_user(&global_alpha, buf, sizeof(global_alpha)))
  925. return -EFAULT;
  926. ad->src_size_g_alpha = (ad->src_size_g_alpha & (~0xff)) |
  927. (global_alpha & 0xff);
  928. mfbi->g_alpha = global_alpha;
  929. pr_debug("set global alpha for index %d\n", mfbi->index);
  930. break;
  931. case MFB_SET_CHROMA_KEY:
  932. /* set panel winformation */
  933. if (copy_from_user(&ck, buf, sizeof(ck)))
  934. return -EFAULT;
  935. if (ck.enable &&
  936. (ck.red_max < ck.red_min ||
  937. ck.green_max < ck.green_min ||
  938. ck.blue_max < ck.blue_min))
  939. return -EINVAL;
  940. if (!ck.enable) {
  941. ad->ckmax_r = 0;
  942. ad->ckmax_g = 0;
  943. ad->ckmax_b = 0;
  944. ad->ckmin_r = 255;
  945. ad->ckmin_g = 255;
  946. ad->ckmin_b = 255;
  947. } else {
  948. ad->ckmax_r = ck.red_max;
  949. ad->ckmax_g = ck.green_max;
  950. ad->ckmax_b = ck.blue_max;
  951. ad->ckmin_r = ck.red_min;
  952. ad->ckmin_g = ck.green_min;
  953. ad->ckmin_b = ck.blue_min;
  954. }
  955. pr_debug("set chroma key\n");
  956. break;
  957. case FBIOGET_GWINFO:
  958. if (mfbi->type == MFB_TYPE_OFF)
  959. return -ENODEV;
  960. /* get graphic window information */
  961. if (copy_to_user(buf, ad, sizeof(*ad)))
  962. return -EFAULT;
  963. break;
  964. case FBIOGET_HWCINFO:
  965. pr_debug("FBIOGET_HWCINFO:0x%08x\n", FBIOGET_HWCINFO);
  966. break;
  967. case FBIOPUT_MODEINFO:
  968. pr_debug("FBIOPUT_MODEINFO:0x%08x\n", FBIOPUT_MODEINFO);
  969. break;
  970. case FBIOGET_DISPINFO:
  971. pr_debug("FBIOGET_DISPINFO:0x%08x\n", FBIOGET_DISPINFO);
  972. break;
  973. default:
  974. printk(KERN_ERR "Unknown ioctl command (0x%08X)\n", cmd);
  975. return -ENOIOCTLCMD;
  976. }
  977. return 0;
  978. }
  979. /* turn on fb if count == 1
  980. */
  981. static int fsl_diu_open(struct fb_info *info, int user)
  982. {
  983. struct mfb_info *mfbi = info->par;
  984. int res = 0;
  985. spin_lock(&diu_lock);
  986. mfbi->count++;
  987. if (mfbi->count == 1) {
  988. pr_debug("open plane index %d\n", mfbi->index);
  989. fsl_diu_check_var(&info->var, info);
  990. res = fsl_diu_set_par(info);
  991. if (res < 0)
  992. mfbi->count--;
  993. else {
  994. res = fsl_diu_enable_panel(info);
  995. if (res < 0)
  996. mfbi->count--;
  997. }
  998. }
  999. spin_unlock(&diu_lock);
  1000. return res;
  1001. }
  1002. /* turn off fb if count == 0
  1003. */
  1004. static int fsl_diu_release(struct fb_info *info, int user)
  1005. {
  1006. struct mfb_info *mfbi = info->par;
  1007. int res = 0;
  1008. spin_lock(&diu_lock);
  1009. mfbi->count--;
  1010. if (mfbi->count == 0) {
  1011. pr_debug("release plane index %d\n", mfbi->index);
  1012. res = fsl_diu_disable_panel(info);
  1013. if (res < 0)
  1014. mfbi->count++;
  1015. }
  1016. spin_unlock(&diu_lock);
  1017. return res;
  1018. }
  1019. static struct fb_ops fsl_diu_ops = {
  1020. .owner = THIS_MODULE,
  1021. .fb_check_var = fsl_diu_check_var,
  1022. .fb_set_par = fsl_diu_set_par,
  1023. .fb_setcolreg = fsl_diu_setcolreg,
  1024. .fb_blank = fsl_diu_blank,
  1025. .fb_pan_display = fsl_diu_pan_display,
  1026. .fb_fillrect = cfb_fillrect,
  1027. .fb_copyarea = cfb_copyarea,
  1028. .fb_imageblit = cfb_imageblit,
  1029. .fb_ioctl = fsl_diu_ioctl,
  1030. .fb_open = fsl_diu_open,
  1031. .fb_release = fsl_diu_release,
  1032. };
  1033. static int init_fbinfo(struct fb_info *info)
  1034. {
  1035. struct mfb_info *mfbi = info->par;
  1036. info->device = NULL;
  1037. info->var.activate = FB_ACTIVATE_NOW;
  1038. info->fbops = &fsl_diu_ops;
  1039. info->flags = FBINFO_FLAG_DEFAULT;
  1040. info->pseudo_palette = &mfbi->pseudo_palette;
  1041. /* Allocate colormap */
  1042. fb_alloc_cmap(&info->cmap, 16, 0);
  1043. return 0;
  1044. }
  1045. static int __devinit install_fb(struct fb_info *info)
  1046. {
  1047. int rc;
  1048. struct mfb_info *mfbi = info->par;
  1049. const char *aoi_mode, *init_aoi_mode = "320x240";
  1050. if (init_fbinfo(info))
  1051. return -EINVAL;
  1052. if (mfbi->index == 0) /* plane 0 */
  1053. aoi_mode = fb_mode;
  1054. else
  1055. aoi_mode = init_aoi_mode;
  1056. pr_debug("mode used = %s\n", aoi_mode);
  1057. rc = fb_find_mode(&info->var, info, aoi_mode, fsl_diu_mode_db,
  1058. ARRAY_SIZE(fsl_diu_mode_db), &fsl_diu_default_mode, default_bpp);
  1059. switch (rc) {
  1060. case 1:
  1061. pr_debug("using mode specified in @mode\n");
  1062. break;
  1063. case 2:
  1064. pr_debug("using mode specified in @mode "
  1065. "with ignored refresh rate\n");
  1066. break;
  1067. case 3:
  1068. pr_debug("using mode default mode\n");
  1069. break;
  1070. case 4:
  1071. pr_debug("using mode from list\n");
  1072. break;
  1073. default:
  1074. pr_debug("rc = %d\n", rc);
  1075. pr_debug("failed to find mode\n");
  1076. return -EINVAL;
  1077. break;
  1078. }
  1079. pr_debug("xres_virtual %d\n", info->var.xres_virtual);
  1080. pr_debug("bits_per_pixel %d\n", info->var.bits_per_pixel);
  1081. pr_debug("info->var.yres_virtual = %d\n", info->var.yres_virtual);
  1082. pr_debug("info->fix.line_length = %d\n", info->fix.line_length);
  1083. if (mfbi->type == MFB_TYPE_OFF)
  1084. mfbi->blank = FB_BLANK_NORMAL;
  1085. else
  1086. mfbi->blank = FB_BLANK_UNBLANK;
  1087. if (fsl_diu_check_var(&info->var, info)) {
  1088. printk(KERN_ERR "fb_check_var failed");
  1089. fb_dealloc_cmap(&info->cmap);
  1090. return -EINVAL;
  1091. }
  1092. if (register_framebuffer(info) < 0) {
  1093. printk(KERN_ERR "register_framebuffer failed");
  1094. unmap_video_memory(info);
  1095. fb_dealloc_cmap(&info->cmap);
  1096. return -EINVAL;
  1097. }
  1098. mfbi->registered = 1;
  1099. printk(KERN_INFO "fb%d: %s fb device registered successfully.\n",
  1100. info->node, info->fix.id);
  1101. return 0;
  1102. }
  1103. static void uninstall_fb(struct fb_info *info)
  1104. {
  1105. struct mfb_info *mfbi = info->par;
  1106. if (!mfbi->registered)
  1107. return;
  1108. unregister_framebuffer(info);
  1109. unmap_video_memory(info);
  1110. if (&info->cmap)
  1111. fb_dealloc_cmap(&info->cmap);
  1112. mfbi->registered = 0;
  1113. }
  1114. static irqreturn_t fsl_diu_isr(int irq, void *dev_id)
  1115. {
  1116. struct diu *hw = dr.diu_reg;
  1117. unsigned int status = in_be32(&hw->int_status);
  1118. if (status) {
  1119. /* This is the workaround for underrun */
  1120. if (status & INT_UNDRUN) {
  1121. out_be32(&hw->diu_mode, 0);
  1122. pr_debug("Err: DIU occurs underrun!\n");
  1123. udelay(1);
  1124. out_be32(&hw->diu_mode, 1);
  1125. }
  1126. #if defined(CONFIG_NOT_COHERENT_CACHE)
  1127. else if (status & INT_VSYNC) {
  1128. unsigned int i;
  1129. for (i = 0; i < coherence_data_size;
  1130. i += d_cache_line_size)
  1131. __asm__ __volatile__ (
  1132. "dcbz 0, %[input]"
  1133. ::[input]"r"(&coherence_data[i]));
  1134. }
  1135. #endif
  1136. return IRQ_HANDLED;
  1137. }
  1138. return IRQ_NONE;
  1139. }
  1140. static int request_irq_local(int irq)
  1141. {
  1142. unsigned long status, ints;
  1143. struct diu *hw;
  1144. int ret;
  1145. hw = dr.diu_reg;
  1146. /* Read to clear the status */
  1147. status = in_be32(&hw->int_status);
  1148. ret = request_irq(irq, fsl_diu_isr, 0, "diu", NULL);
  1149. if (ret)
  1150. pr_info("Request diu IRQ failed.\n");
  1151. else {
  1152. ints = INT_PARERR | INT_LS_BF_VS;
  1153. #if !defined(CONFIG_NOT_COHERENT_CACHE)
  1154. ints |= INT_VSYNC;
  1155. #endif
  1156. if (dr.mode == MFB_MODE2 || dr.mode == MFB_MODE3)
  1157. ints |= INT_VSYNC_WB;
  1158. /* Read to clear the status */
  1159. status = in_be32(&hw->int_status);
  1160. out_be32(&hw->int_mask, ints);
  1161. }
  1162. return ret;
  1163. }
  1164. static void free_irq_local(int irq)
  1165. {
  1166. struct diu *hw = dr.diu_reg;
  1167. /* Disable all LCDC interrupt */
  1168. out_be32(&hw->int_mask, 0x1f);
  1169. free_irq(irq, NULL);
  1170. }
  1171. #ifdef CONFIG_PM
  1172. /*
  1173. * Power management hooks. Note that we won't be called from IRQ context,
  1174. * unlike the blank functions above, so we may sleep.
  1175. */
  1176. static int fsl_diu_suspend(struct of_device *ofdev, pm_message_t state)
  1177. {
  1178. struct fsl_diu_data *machine_data;
  1179. machine_data = dev_get_drvdata(&ofdev->dev);
  1180. disable_lcdc(machine_data->fsl_diu_info[0]);
  1181. return 0;
  1182. }
  1183. static int fsl_diu_resume(struct of_device *ofdev)
  1184. {
  1185. struct fsl_diu_data *machine_data;
  1186. machine_data = dev_get_drvdata(&ofdev->dev);
  1187. enable_lcdc(machine_data->fsl_diu_info[0]);
  1188. return 0;
  1189. }
  1190. #else
  1191. #define fsl_diu_suspend NULL
  1192. #define fsl_diu_resume NULL
  1193. #endif /* CONFIG_PM */
  1194. /* Align to 64-bit(8-byte), 32-byte, etc. */
  1195. static int allocate_buf(struct device *dev, struct diu_addr *buf, u32 size,
  1196. u32 bytes_align)
  1197. {
  1198. u32 offset, ssize;
  1199. u32 mask;
  1200. dma_addr_t paddr = 0;
  1201. ssize = size + bytes_align;
  1202. buf->vaddr = dma_alloc_coherent(dev, ssize, &paddr, GFP_DMA |
  1203. __GFP_ZERO);
  1204. if (!buf->vaddr)
  1205. return -ENOMEM;
  1206. buf->paddr = (__u32) paddr;
  1207. mask = bytes_align - 1;
  1208. offset = (u32)buf->paddr & mask;
  1209. if (offset) {
  1210. buf->offset = bytes_align - offset;
  1211. buf->paddr = (u32)buf->paddr + offset;
  1212. } else
  1213. buf->offset = 0;
  1214. return 0;
  1215. }
  1216. static void free_buf(struct device *dev, struct diu_addr *buf, u32 size,
  1217. u32 bytes_align)
  1218. {
  1219. dma_free_coherent(dev, size + bytes_align,
  1220. buf->vaddr, (buf->paddr - buf->offset));
  1221. return;
  1222. }
  1223. static ssize_t store_monitor(struct device *device,
  1224. struct device_attribute *attr, const char *buf, size_t count)
  1225. {
  1226. int old_monitor_port;
  1227. unsigned long val;
  1228. struct fsl_diu_data *machine_data =
  1229. container_of(attr, struct fsl_diu_data, dev_attr);
  1230. if (strict_strtoul(buf, 10, &val))
  1231. return 0;
  1232. old_monitor_port = machine_data->monitor_port;
  1233. machine_data->monitor_port = diu_ops.set_sysfs_monitor_port(val);
  1234. if (old_monitor_port != machine_data->monitor_port) {
  1235. /* All AOIs need adjust pixel format
  1236. * fsl_diu_set_par only change the pixsel format here
  1237. * unlikely to fail. */
  1238. fsl_diu_set_par(machine_data->fsl_diu_info[0]);
  1239. fsl_diu_set_par(machine_data->fsl_diu_info[1]);
  1240. fsl_diu_set_par(machine_data->fsl_diu_info[2]);
  1241. fsl_diu_set_par(machine_data->fsl_diu_info[3]);
  1242. fsl_diu_set_par(machine_data->fsl_diu_info[4]);
  1243. }
  1244. return count;
  1245. }
  1246. static ssize_t show_monitor(struct device *device,
  1247. struct device_attribute *attr, char *buf)
  1248. {
  1249. struct fsl_diu_data *machine_data =
  1250. container_of(attr, struct fsl_diu_data, dev_attr);
  1251. return diu_ops.show_monitor_port(machine_data->monitor_port, buf);
  1252. }
  1253. static int __devinit fsl_diu_probe(struct of_device *ofdev,
  1254. const struct of_device_id *match)
  1255. {
  1256. struct device_node *np = ofdev->node;
  1257. struct mfb_info *mfbi;
  1258. phys_addr_t dummy_ad_addr;
  1259. int ret, i, error = 0;
  1260. struct resource res;
  1261. struct fsl_diu_data *machine_data;
  1262. machine_data = kzalloc(sizeof(struct fsl_diu_data), GFP_KERNEL);
  1263. if (!machine_data)
  1264. return -ENOMEM;
  1265. for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++) {
  1266. machine_data->fsl_diu_info[i] =
  1267. framebuffer_alloc(sizeof(struct mfb_info), &ofdev->dev);
  1268. if (!machine_data->fsl_diu_info[i]) {
  1269. dev_err(&ofdev->dev, "cannot allocate memory\n");
  1270. ret = -ENOMEM;
  1271. goto error2;
  1272. }
  1273. mfbi = machine_data->fsl_diu_info[i]->par;
  1274. memcpy(mfbi, &mfb_template[i], sizeof(struct mfb_info));
  1275. mfbi->parent = machine_data;
  1276. }
  1277. ret = of_address_to_resource(np, 0, &res);
  1278. if (ret) {
  1279. dev_err(&ofdev->dev, "could not obtain DIU address\n");
  1280. goto error;
  1281. }
  1282. if (!res.start) {
  1283. dev_err(&ofdev->dev, "invalid DIU address\n");
  1284. goto error;
  1285. }
  1286. dev_dbg(&ofdev->dev, "%s, res.start: 0x%08x\n", __func__, res.start);
  1287. dr.diu_reg = ioremap(res.start, sizeof(struct diu));
  1288. if (!dr.diu_reg) {
  1289. dev_err(&ofdev->dev, "Err: can't map DIU registers!\n");
  1290. ret = -EFAULT;
  1291. goto error2;
  1292. }
  1293. out_be32(&dr.diu_reg->diu_mode, 0); /* disable DIU anyway*/
  1294. /* Get the IRQ of the DIU */
  1295. machine_data->irq = irq_of_parse_and_map(np, 0);
  1296. if (!machine_data->irq) {
  1297. dev_err(&ofdev->dev, "could not get DIU IRQ\n");
  1298. ret = -EINVAL;
  1299. goto error;
  1300. }
  1301. machine_data->monitor_port = monitor_port;
  1302. /* Area descriptor memory pool aligns to 64-bit boundary */
  1303. if (allocate_buf(&ofdev->dev, &pool.ad,
  1304. sizeof(struct diu_ad) * FSL_AOI_NUM, 8))
  1305. return -ENOMEM;
  1306. /* Get memory for Gamma Table - 32-byte aligned memory */
  1307. if (allocate_buf(&ofdev->dev, &pool.gamma, 768, 32)) {
  1308. ret = -ENOMEM;
  1309. goto error;
  1310. }
  1311. /* For performance, cursor bitmap buffer aligns to 32-byte boundary */
  1312. if (allocate_buf(&ofdev->dev, &pool.cursor, MAX_CURS * MAX_CURS * 2,
  1313. 32)) {
  1314. ret = -ENOMEM;
  1315. goto error;
  1316. }
  1317. i = ARRAY_SIZE(machine_data->fsl_diu_info);
  1318. machine_data->dummy_ad = (struct diu_ad *)
  1319. ((u32)pool.ad.vaddr + pool.ad.offset) + i;
  1320. machine_data->dummy_ad->paddr = pool.ad.paddr +
  1321. i * sizeof(struct diu_ad);
  1322. machine_data->dummy_aoi_virt = fsl_diu_alloc(64, &dummy_ad_addr);
  1323. if (!machine_data->dummy_aoi_virt) {
  1324. ret = -ENOMEM;
  1325. goto error;
  1326. }
  1327. machine_data->dummy_ad->addr = cpu_to_le32(dummy_ad_addr);
  1328. machine_data->dummy_ad->pix_fmt = 0x88882317;
  1329. machine_data->dummy_ad->src_size_g_alpha = cpu_to_le32((4 << 12) | 4);
  1330. machine_data->dummy_ad->aoi_size = cpu_to_le32((4 << 16) | 2);
  1331. machine_data->dummy_ad->offset_xyi = 0;
  1332. machine_data->dummy_ad->offset_xyd = 0;
  1333. machine_data->dummy_ad->next_ad = 0;
  1334. out_be32(&dr.diu_reg->desc[0], machine_data->dummy_ad->paddr);
  1335. out_be32(&dr.diu_reg->desc[1], machine_data->dummy_ad->paddr);
  1336. out_be32(&dr.diu_reg->desc[2], machine_data->dummy_ad->paddr);
  1337. for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++) {
  1338. machine_data->fsl_diu_info[i]->fix.smem_start = 0;
  1339. mfbi = machine_data->fsl_diu_info[i]->par;
  1340. mfbi->ad = (struct diu_ad *)((u32)pool.ad.vaddr
  1341. + pool.ad.offset) + i;
  1342. mfbi->ad->paddr = pool.ad.paddr + i * sizeof(struct diu_ad);
  1343. ret = install_fb(machine_data->fsl_diu_info[i]);
  1344. if (ret) {
  1345. dev_err(&ofdev->dev,
  1346. "Failed to register framebuffer %d\n",
  1347. i);
  1348. goto error;
  1349. }
  1350. }
  1351. if (request_irq_local(machine_data->irq)) {
  1352. dev_err(machine_data->fsl_diu_info[0]->dev,
  1353. "could not request irq for diu.");
  1354. goto error;
  1355. }
  1356. machine_data->dev_attr.attr.name = "monitor";
  1357. machine_data->dev_attr.attr.mode = S_IRUGO|S_IWUSR;
  1358. machine_data->dev_attr.show = show_monitor;
  1359. machine_data->dev_attr.store = store_monitor;
  1360. error = device_create_file(machine_data->fsl_diu_info[0]->dev,
  1361. &machine_data->dev_attr);
  1362. if (error) {
  1363. dev_err(machine_data->fsl_diu_info[0]->dev,
  1364. "could not create sysfs %s file\n",
  1365. machine_data->dev_attr.attr.name);
  1366. }
  1367. dev_set_drvdata(&ofdev->dev, machine_data);
  1368. return 0;
  1369. error:
  1370. for (i = ARRAY_SIZE(machine_data->fsl_diu_info);
  1371. i > 0; i--)
  1372. uninstall_fb(machine_data->fsl_diu_info[i - 1]);
  1373. if (pool.ad.vaddr)
  1374. free_buf(&ofdev->dev, &pool.ad,
  1375. sizeof(struct diu_ad) * FSL_AOI_NUM, 8);
  1376. if (pool.gamma.vaddr)
  1377. free_buf(&ofdev->dev, &pool.gamma, 768, 32);
  1378. if (pool.cursor.vaddr)
  1379. free_buf(&ofdev->dev, &pool.cursor, MAX_CURS * MAX_CURS * 2,
  1380. 32);
  1381. if (machine_data->dummy_aoi_virt)
  1382. fsl_diu_free(machine_data->dummy_aoi_virt, 64);
  1383. iounmap(dr.diu_reg);
  1384. error2:
  1385. for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++)
  1386. if (machine_data->fsl_diu_info[i])
  1387. framebuffer_release(machine_data->fsl_diu_info[i]);
  1388. kfree(machine_data);
  1389. return ret;
  1390. }
  1391. static int fsl_diu_remove(struct of_device *ofdev)
  1392. {
  1393. struct fsl_diu_data *machine_data;
  1394. int i;
  1395. machine_data = dev_get_drvdata(&ofdev->dev);
  1396. disable_lcdc(machine_data->fsl_diu_info[0]);
  1397. free_irq_local(machine_data->irq);
  1398. for (i = ARRAY_SIZE(machine_data->fsl_diu_info); i > 0; i--)
  1399. uninstall_fb(machine_data->fsl_diu_info[i - 1]);
  1400. if (pool.ad.vaddr)
  1401. free_buf(&ofdev->dev, &pool.ad,
  1402. sizeof(struct diu_ad) * FSL_AOI_NUM, 8);
  1403. if (pool.gamma.vaddr)
  1404. free_buf(&ofdev->dev, &pool.gamma, 768, 32);
  1405. if (pool.cursor.vaddr)
  1406. free_buf(&ofdev->dev, &pool.cursor, MAX_CURS * MAX_CURS * 2,
  1407. 32);
  1408. if (machine_data->dummy_aoi_virt)
  1409. fsl_diu_free(machine_data->dummy_aoi_virt, 64);
  1410. iounmap(dr.diu_reg);
  1411. for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++)
  1412. if (machine_data->fsl_diu_info[i])
  1413. framebuffer_release(machine_data->fsl_diu_info[i]);
  1414. kfree(machine_data);
  1415. return 0;
  1416. }
  1417. #ifndef MODULE
  1418. static int __init fsl_diu_setup(char *options)
  1419. {
  1420. char *opt;
  1421. unsigned long val;
  1422. if (!options || !*options)
  1423. return 0;
  1424. while ((opt = strsep(&options, ",")) != NULL) {
  1425. if (!*opt)
  1426. continue;
  1427. if (!strncmp(opt, "monitor=", 8)) {
  1428. if (!strict_strtoul(opt + 8, 10, &val) && (val <= 2))
  1429. monitor_port = val;
  1430. } else if (!strncmp(opt, "bpp=", 4)) {
  1431. if (!strict_strtoul(opt + 4, 10, &val))
  1432. default_bpp = val;
  1433. } else
  1434. fb_mode = opt;
  1435. }
  1436. return 0;
  1437. }
  1438. #endif
  1439. static struct of_device_id fsl_diu_match[] = {
  1440. {
  1441. .compatible = "fsl,diu",
  1442. },
  1443. {}
  1444. };
  1445. MODULE_DEVICE_TABLE(of, fsl_diu_match);
  1446. static struct of_platform_driver fsl_diu_driver = {
  1447. .owner = THIS_MODULE,
  1448. .name = "fsl_diu",
  1449. .match_table = fsl_diu_match,
  1450. .probe = fsl_diu_probe,
  1451. .remove = fsl_diu_remove,
  1452. .suspend = fsl_diu_suspend,
  1453. .resume = fsl_diu_resume,
  1454. };
  1455. static int __init fsl_diu_init(void)
  1456. {
  1457. #ifdef CONFIG_NOT_COHERENT_CACHE
  1458. struct device_node *np;
  1459. const u32 *prop;
  1460. #endif
  1461. int ret;
  1462. #ifndef MODULE
  1463. char *option;
  1464. /*
  1465. * For kernel boot options (in 'video=xxxfb:<options>' format)
  1466. */
  1467. if (fb_get_options("fslfb", &option))
  1468. return -ENODEV;
  1469. fsl_diu_setup(option);
  1470. #endif
  1471. printk(KERN_INFO "Freescale DIU driver\n");
  1472. #ifdef CONFIG_NOT_COHERENT_CACHE
  1473. np = of_find_node_by_type(NULL, "cpu");
  1474. if (!np) {
  1475. printk(KERN_ERR "Err: can't find device node 'cpu'\n");
  1476. return -ENODEV;
  1477. }
  1478. prop = of_get_property(np, "d-cache-size", NULL);
  1479. if (prop == NULL) {
  1480. of_node_put(np);
  1481. return -ENODEV;
  1482. }
  1483. /* Freescale PLRU requires 13/8 times the cache size to do a proper
  1484. displacement flush
  1485. */
  1486. coherence_data_size = *prop * 13;
  1487. coherence_data_size /= 8;
  1488. prop = of_get_property(np, "d-cache-line-size", NULL);
  1489. if (prop == NULL) {
  1490. of_node_put(np);
  1491. return -ENODEV;
  1492. }
  1493. d_cache_line_size = *prop;
  1494. of_node_put(np);
  1495. coherence_data = vmalloc(coherence_data_size);
  1496. if (!coherence_data)
  1497. return -ENOMEM;
  1498. #endif
  1499. ret = of_register_platform_driver(&fsl_diu_driver);
  1500. if (ret) {
  1501. printk(KERN_ERR
  1502. "fsl-diu: failed to register platform driver\n");
  1503. #if defined(CONFIG_NOT_COHERENT_CACHE)
  1504. vfree(coherence_data);
  1505. #endif
  1506. iounmap(dr.diu_reg);
  1507. }
  1508. return ret;
  1509. }
  1510. static void __exit fsl_diu_exit(void)
  1511. {
  1512. of_unregister_platform_driver(&fsl_diu_driver);
  1513. #if defined(CONFIG_NOT_COHERENT_CACHE)
  1514. vfree(coherence_data);
  1515. #endif
  1516. }
  1517. module_init(fsl_diu_init);
  1518. module_exit(fsl_diu_exit);
  1519. MODULE_AUTHOR("York Sun <yorksun@freescale.com>");
  1520. MODULE_DESCRIPTION("Freescale DIU framebuffer driver");
  1521. MODULE_LICENSE("GPL");
  1522. module_param_named(mode, fb_mode, charp, 0);
  1523. MODULE_PARM_DESC(mode,
  1524. "Specify resolution as \"<xres>x<yres>[-<bpp>][@<refresh>]\" ");
  1525. module_param_named(bpp, default_bpp, ulong, 0);
  1526. MODULE_PARM_DESC(bpp, "Specify bit-per-pixel if not specified mode");
  1527. module_param_named(monitor, monitor_port, int, 0);
  1528. MODULE_PARM_DESC(monitor,
  1529. "Specify the monitor port (0, 1 or 2) if supported by the platform");