fsl-diu-fb.c 44 KB

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