fsl-diu-fb.c 44 KB

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