imxfb.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. /*
  2. * Freescale i.MX Frame Buffer device driver
  3. *
  4. * Copyright (C) 2004 Sascha Hauer, Pengutronix
  5. * Based on acornfb.c Copyright (C) Russell King.
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License. See the file COPYING in the main directory of this archive for
  9. * more details.
  10. *
  11. * Please direct your questions and comments on this driver to the following
  12. * email address:
  13. *
  14. * linux-arm-kernel@lists.arm.linux.org.uk
  15. */
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/errno.h>
  19. #include <linux/string.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/slab.h>
  22. #include <linux/mm.h>
  23. #include <linux/fb.h>
  24. #include <linux/delay.h>
  25. #include <linux/init.h>
  26. #include <linux/ioport.h>
  27. #include <linux/cpufreq.h>
  28. #include <linux/clk.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/dma-mapping.h>
  31. #include <linux/io.h>
  32. #include <linux/math64.h>
  33. #include <mach/imxfb.h>
  34. /*
  35. * Complain if VAR is out of range.
  36. */
  37. #define DEBUG_VAR 1
  38. #define DRIVER_NAME "imx-fb"
  39. #define LCDC_SSA 0x00
  40. #define LCDC_SIZE 0x04
  41. #define SIZE_XMAX(x) ((((x) >> 4) & 0x3f) << 20)
  42. #ifdef CONFIG_ARCH_MX1
  43. #define SIZE_YMAX(y) ((y) & 0x1ff)
  44. #else
  45. #define SIZE_YMAX(y) ((y) & 0x3ff)
  46. #endif
  47. #define LCDC_VPW 0x08
  48. #define VPW_VPW(x) ((x) & 0x3ff)
  49. #define LCDC_CPOS 0x0C
  50. #define CPOS_CC1 (1<<31)
  51. #define CPOS_CC0 (1<<30)
  52. #define CPOS_OP (1<<28)
  53. #define CPOS_CXP(x) (((x) & 3ff) << 16)
  54. #ifdef CONFIG_ARCH_MX1
  55. #define CPOS_CYP(y) ((y) & 0x1ff)
  56. #else
  57. #define CPOS_CYP(y) ((y) & 0x3ff)
  58. #endif
  59. #define LCDC_LCWHB 0x10
  60. #define LCWHB_BK_EN (1<<31)
  61. #define LCWHB_CW(w) (((w) & 0x1f) << 24)
  62. #define LCWHB_CH(h) (((h) & 0x1f) << 16)
  63. #define LCWHB_BD(x) ((x) & 0xff)
  64. #define LCDC_LCHCC 0x14
  65. #ifdef CONFIG_ARCH_MX1
  66. #define LCHCC_CUR_COL_R(r) (((r) & 0x1f) << 11)
  67. #define LCHCC_CUR_COL_G(g) (((g) & 0x3f) << 5)
  68. #define LCHCC_CUR_COL_B(b) ((b) & 0x1f)
  69. #else
  70. #define LCHCC_CUR_COL_R(r) (((r) & 0x3f) << 12)
  71. #define LCHCC_CUR_COL_G(g) (((g) & 0x3f) << 6)
  72. #define LCHCC_CUR_COL_B(b) ((b) & 0x3f)
  73. #endif
  74. #define LCDC_PCR 0x18
  75. #define LCDC_HCR 0x1C
  76. #define HCR_H_WIDTH(x) (((x) & 0x3f) << 26)
  77. #define HCR_H_WAIT_1(x) (((x) & 0xff) << 8)
  78. #define HCR_H_WAIT_2(x) ((x) & 0xff)
  79. #define LCDC_VCR 0x20
  80. #define VCR_V_WIDTH(x) (((x) & 0x3f) << 26)
  81. #define VCR_V_WAIT_1(x) (((x) & 0xff) << 8)
  82. #define VCR_V_WAIT_2(x) ((x) & 0xff)
  83. #define LCDC_POS 0x24
  84. #define POS_POS(x) ((x) & 1f)
  85. #define LCDC_LSCR1 0x28
  86. /* bit fields in imxfb.h */
  87. #define LCDC_PWMR 0x2C
  88. /* bit fields in imxfb.h */
  89. #define LCDC_DMACR 0x30
  90. /* bit fields in imxfb.h */
  91. #define LCDC_RMCR 0x34
  92. #ifdef CONFIG_ARCH_MX1
  93. #define RMCR_LCDC_EN (1<<1)
  94. #else
  95. #define RMCR_LCDC_EN 0
  96. #endif
  97. #define RMCR_SELF_REF (1<<0)
  98. #define LCDC_LCDICR 0x38
  99. #define LCDICR_INT_SYN (1<<2)
  100. #define LCDICR_INT_CON (1)
  101. #define LCDC_LCDISR 0x40
  102. #define LCDISR_UDR_ERR (1<<3)
  103. #define LCDISR_ERR_RES (1<<2)
  104. #define LCDISR_EOF (1<<1)
  105. #define LCDISR_BOF (1<<0)
  106. /*
  107. * These are the bitfields for each
  108. * display depth that we support.
  109. */
  110. struct imxfb_rgb {
  111. struct fb_bitfield red;
  112. struct fb_bitfield green;
  113. struct fb_bitfield blue;
  114. struct fb_bitfield transp;
  115. };
  116. struct imxfb_info {
  117. struct platform_device *pdev;
  118. void __iomem *regs;
  119. struct clk *clk;
  120. u_int max_bpp;
  121. u_int max_xres;
  122. u_int max_yres;
  123. /*
  124. * These are the addresses we mapped
  125. * the framebuffer memory region to.
  126. */
  127. dma_addr_t map_dma;
  128. u_char *map_cpu;
  129. u_int map_size;
  130. u_char *screen_cpu;
  131. dma_addr_t screen_dma;
  132. u_int palette_size;
  133. dma_addr_t dbar1;
  134. dma_addr_t dbar2;
  135. u_int pcr;
  136. u_int pwmr;
  137. u_int lscr1;
  138. u_int dmacr;
  139. u_int cmap_inverse:1,
  140. cmap_static:1,
  141. unused:30;
  142. void (*lcd_power)(int);
  143. void (*backlight_power)(int);
  144. };
  145. #define IMX_NAME "IMX"
  146. /*
  147. * Minimum X and Y resolutions
  148. */
  149. #define MIN_XRES 64
  150. #define MIN_YRES 64
  151. /* Actually this really is 18bit support, the lowest 2 bits of each colour
  152. * are unused in hardware. We claim to have 24bit support to make software
  153. * like X work, which does not support 18bit.
  154. */
  155. static struct imxfb_rgb def_rgb_18 = {
  156. .red = {.offset = 16, .length = 8,},
  157. .green = {.offset = 8, .length = 8,},
  158. .blue = {.offset = 0, .length = 8,},
  159. .transp = {.offset = 0, .length = 0,},
  160. };
  161. static struct imxfb_rgb def_rgb_16_tft = {
  162. .red = {.offset = 11, .length = 5,},
  163. .green = {.offset = 5, .length = 6,},
  164. .blue = {.offset = 0, .length = 5,},
  165. .transp = {.offset = 0, .length = 0,},
  166. };
  167. static struct imxfb_rgb def_rgb_16_stn = {
  168. .red = {.offset = 8, .length = 4,},
  169. .green = {.offset = 4, .length = 4,},
  170. .blue = {.offset = 0, .length = 4,},
  171. .transp = {.offset = 0, .length = 0,},
  172. };
  173. static struct imxfb_rgb def_rgb_8 = {
  174. .red = {.offset = 0, .length = 8,},
  175. .green = {.offset = 0, .length = 8,},
  176. .blue = {.offset = 0, .length = 8,},
  177. .transp = {.offset = 0, .length = 0,},
  178. };
  179. static int imxfb_activate_var(struct fb_var_screeninfo *var,
  180. struct fb_info *info);
  181. static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
  182. {
  183. chan &= 0xffff;
  184. chan >>= 16 - bf->length;
  185. return chan << bf->offset;
  186. }
  187. static int imxfb_setpalettereg(u_int regno, u_int red, u_int green, u_int blue,
  188. u_int trans, struct fb_info *info)
  189. {
  190. struct imxfb_info *fbi = info->par;
  191. u_int val, ret = 1;
  192. #define CNVT_TOHW(val,width) ((((val)<<(width))+0x7FFF-(val))>>16)
  193. if (regno < fbi->palette_size) {
  194. val = (CNVT_TOHW(red, 4) << 8) |
  195. (CNVT_TOHW(green,4) << 4) |
  196. CNVT_TOHW(blue, 4);
  197. writel(val, fbi->regs + 0x800 + (regno << 2));
  198. ret = 0;
  199. }
  200. return ret;
  201. }
  202. static int imxfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  203. u_int trans, struct fb_info *info)
  204. {
  205. struct imxfb_info *fbi = info->par;
  206. unsigned int val;
  207. int ret = 1;
  208. /*
  209. * If inverse mode was selected, invert all the colours
  210. * rather than the register number. The register number
  211. * is what you poke into the framebuffer to produce the
  212. * colour you requested.
  213. */
  214. if (fbi->cmap_inverse) {
  215. red = 0xffff - red;
  216. green = 0xffff - green;
  217. blue = 0xffff - blue;
  218. }
  219. /*
  220. * If greyscale is true, then we convert the RGB value
  221. * to greyscale no mater what visual we are using.
  222. */
  223. if (info->var.grayscale)
  224. red = green = blue = (19595 * red + 38470 * green +
  225. 7471 * blue) >> 16;
  226. switch (info->fix.visual) {
  227. case FB_VISUAL_TRUECOLOR:
  228. /*
  229. * 12 or 16-bit True Colour. We encode the RGB value
  230. * according to the RGB bitfield information.
  231. */
  232. if (regno < 16) {
  233. u32 *pal = info->pseudo_palette;
  234. val = chan_to_field(red, &info->var.red);
  235. val |= chan_to_field(green, &info->var.green);
  236. val |= chan_to_field(blue, &info->var.blue);
  237. pal[regno] = val;
  238. ret = 0;
  239. }
  240. break;
  241. case FB_VISUAL_STATIC_PSEUDOCOLOR:
  242. case FB_VISUAL_PSEUDOCOLOR:
  243. ret = imxfb_setpalettereg(regno, red, green, blue, trans, info);
  244. break;
  245. }
  246. return ret;
  247. }
  248. /*
  249. * imxfb_check_var():
  250. * Round up in the following order: bits_per_pixel, xres,
  251. * yres, xres_virtual, yres_virtual, xoffset, yoffset, grayscale,
  252. * bitfields, horizontal timing, vertical timing.
  253. */
  254. static int imxfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  255. {
  256. struct imxfb_info *fbi = info->par;
  257. struct imxfb_rgb *rgb;
  258. if (var->xres < MIN_XRES)
  259. var->xres = MIN_XRES;
  260. if (var->yres < MIN_YRES)
  261. var->yres = MIN_YRES;
  262. if (var->xres > fbi->max_xres)
  263. var->xres = fbi->max_xres;
  264. if (var->yres > fbi->max_yres)
  265. var->yres = fbi->max_yres;
  266. var->xres_virtual = max(var->xres_virtual, var->xres);
  267. var->yres_virtual = max(var->yres_virtual, var->yres);
  268. pr_debug("var->bits_per_pixel=%d\n", var->bits_per_pixel);
  269. switch (var->bits_per_pixel) {
  270. case 32:
  271. rgb = &def_rgb_18;
  272. break;
  273. case 16:
  274. default:
  275. if (fbi->pcr & PCR_TFT)
  276. rgb = &def_rgb_16_tft;
  277. else
  278. rgb = &def_rgb_16_stn;
  279. break;
  280. case 8:
  281. rgb = &def_rgb_8;
  282. break;
  283. }
  284. /*
  285. * Copy the RGB parameters for this display
  286. * from the machine specific parameters.
  287. */
  288. var->red = rgb->red;
  289. var->green = rgb->green;
  290. var->blue = rgb->blue;
  291. var->transp = rgb->transp;
  292. pr_debug("RGBT length = %d:%d:%d:%d\n",
  293. var->red.length, var->green.length, var->blue.length,
  294. var->transp.length);
  295. pr_debug("RGBT offset = %d:%d:%d:%d\n",
  296. var->red.offset, var->green.offset, var->blue.offset,
  297. var->transp.offset);
  298. return 0;
  299. }
  300. /*
  301. * imxfb_set_par():
  302. * Set the user defined part of the display for the specified console
  303. */
  304. static int imxfb_set_par(struct fb_info *info)
  305. {
  306. struct imxfb_info *fbi = info->par;
  307. struct fb_var_screeninfo *var = &info->var;
  308. if (var->bits_per_pixel == 16 || var->bits_per_pixel == 32)
  309. info->fix.visual = FB_VISUAL_TRUECOLOR;
  310. else if (!fbi->cmap_static)
  311. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  312. else {
  313. /*
  314. * Some people have weird ideas about wanting static
  315. * pseudocolor maps. I suspect their user space
  316. * applications are broken.
  317. */
  318. info->fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
  319. }
  320. info->fix.line_length = var->xres_virtual * var->bits_per_pixel / 8;
  321. fbi->palette_size = var->bits_per_pixel == 8 ? 256 : 16;
  322. imxfb_activate_var(var, info);
  323. return 0;
  324. }
  325. static void imxfb_enable_controller(struct imxfb_info *fbi)
  326. {
  327. pr_debug("Enabling LCD controller\n");
  328. writel(fbi->screen_dma, fbi->regs + LCDC_SSA);
  329. /* physical screen start address */
  330. writel(VPW_VPW(fbi->max_xres * fbi->max_bpp / 8 / 4),
  331. fbi->regs + LCDC_VPW);
  332. /* panning offset 0 (0 pixel offset) */
  333. writel(0x00000000, fbi->regs + LCDC_POS);
  334. /* disable hardware cursor */
  335. writel(readl(fbi->regs + LCDC_CPOS) & ~(CPOS_CC0 | CPOS_CC1),
  336. fbi->regs + LCDC_CPOS);
  337. writel(RMCR_LCDC_EN, fbi->regs + LCDC_RMCR);
  338. clk_enable(fbi->clk);
  339. if (fbi->backlight_power)
  340. fbi->backlight_power(1);
  341. if (fbi->lcd_power)
  342. fbi->lcd_power(1);
  343. }
  344. static void imxfb_disable_controller(struct imxfb_info *fbi)
  345. {
  346. pr_debug("Disabling LCD controller\n");
  347. if (fbi->backlight_power)
  348. fbi->backlight_power(0);
  349. if (fbi->lcd_power)
  350. fbi->lcd_power(0);
  351. clk_disable(fbi->clk);
  352. writel(0, fbi->regs + LCDC_RMCR);
  353. }
  354. static int imxfb_blank(int blank, struct fb_info *info)
  355. {
  356. struct imxfb_info *fbi = info->par;
  357. pr_debug("imxfb_blank: blank=%d\n", blank);
  358. switch (blank) {
  359. case FB_BLANK_POWERDOWN:
  360. case FB_BLANK_VSYNC_SUSPEND:
  361. case FB_BLANK_HSYNC_SUSPEND:
  362. case FB_BLANK_NORMAL:
  363. imxfb_disable_controller(fbi);
  364. break;
  365. case FB_BLANK_UNBLANK:
  366. imxfb_enable_controller(fbi);
  367. break;
  368. }
  369. return 0;
  370. }
  371. static struct fb_ops imxfb_ops = {
  372. .owner = THIS_MODULE,
  373. .fb_check_var = imxfb_check_var,
  374. .fb_set_par = imxfb_set_par,
  375. .fb_setcolreg = imxfb_setcolreg,
  376. .fb_fillrect = cfb_fillrect,
  377. .fb_copyarea = cfb_copyarea,
  378. .fb_imageblit = cfb_imageblit,
  379. .fb_blank = imxfb_blank,
  380. };
  381. /*
  382. * imxfb_activate_var():
  383. * Configures LCD Controller based on entries in var parameter. Settings are
  384. * only written to the controller if changes were made.
  385. */
  386. static int imxfb_activate_var(struct fb_var_screeninfo *var, struct fb_info *info)
  387. {
  388. struct imxfb_info *fbi = info->par;
  389. unsigned int pcr, lcd_clk;
  390. unsigned long long tmp;
  391. pr_debug("var: xres=%d hslen=%d lm=%d rm=%d\n",
  392. var->xres, var->hsync_len,
  393. var->left_margin, var->right_margin);
  394. pr_debug("var: yres=%d vslen=%d um=%d bm=%d\n",
  395. var->yres, var->vsync_len,
  396. var->upper_margin, var->lower_margin);
  397. #if DEBUG_VAR
  398. if (var->xres < 16 || var->xres > 1024)
  399. printk(KERN_ERR "%s: invalid xres %d\n",
  400. info->fix.id, var->xres);
  401. if (var->hsync_len < 1 || var->hsync_len > 64)
  402. printk(KERN_ERR "%s: invalid hsync_len %d\n",
  403. info->fix.id, var->hsync_len);
  404. if (var->left_margin > 255)
  405. printk(KERN_ERR "%s: invalid left_margin %d\n",
  406. info->fix.id, var->left_margin);
  407. if (var->right_margin > 255)
  408. printk(KERN_ERR "%s: invalid right_margin %d\n",
  409. info->fix.id, var->right_margin);
  410. if (var->yres < 1 || var->yres > 511)
  411. printk(KERN_ERR "%s: invalid yres %d\n",
  412. info->fix.id, var->yres);
  413. if (var->vsync_len > 100)
  414. printk(KERN_ERR "%s: invalid vsync_len %d\n",
  415. info->fix.id, var->vsync_len);
  416. if (var->upper_margin > 63)
  417. printk(KERN_ERR "%s: invalid upper_margin %d\n",
  418. info->fix.id, var->upper_margin);
  419. if (var->lower_margin > 255)
  420. printk(KERN_ERR "%s: invalid lower_margin %d\n",
  421. info->fix.id, var->lower_margin);
  422. #endif
  423. writel(HCR_H_WIDTH(var->hsync_len - 1) |
  424. HCR_H_WAIT_1(var->right_margin - 1) |
  425. HCR_H_WAIT_2(var->left_margin - 3),
  426. fbi->regs + LCDC_HCR);
  427. writel(VCR_V_WIDTH(var->vsync_len) |
  428. VCR_V_WAIT_1(var->lower_margin) |
  429. VCR_V_WAIT_2(var->upper_margin),
  430. fbi->regs + LCDC_VCR);
  431. writel(SIZE_XMAX(var->xres) | SIZE_YMAX(var->yres),
  432. fbi->regs + LCDC_SIZE);
  433. lcd_clk = clk_get_rate(fbi->clk);
  434. tmp = var->pixclock * (unsigned long long)lcd_clk;
  435. do_div(tmp, 1000000);
  436. if (do_div(tmp, 1000000) > 500000)
  437. tmp++;
  438. pcr = (unsigned int)tmp;
  439. if (--pcr > 0x3F) {
  440. pcr = 0x3F;
  441. printk(KERN_WARNING "Must limit pixel clock to %uHz\n",
  442. lcd_clk / pcr);
  443. }
  444. /* add sync polarities */
  445. pcr |= fbi->pcr & ~0x3F;
  446. writel(pcr, fbi->regs + LCDC_PCR);
  447. writel(fbi->pwmr, fbi->regs + LCDC_PWMR);
  448. writel(fbi->lscr1, fbi->regs + LCDC_LSCR1);
  449. writel(fbi->dmacr, fbi->regs + LCDC_DMACR);
  450. return 0;
  451. }
  452. #ifdef CONFIG_PM
  453. /*
  454. * Power management hooks. Note that we won't be called from IRQ context,
  455. * unlike the blank functions above, so we may sleep.
  456. */
  457. static int imxfb_suspend(struct platform_device *dev, pm_message_t state)
  458. {
  459. struct imxfb_info *fbi = platform_get_drvdata(dev);
  460. pr_debug("%s\n", __func__);
  461. imxfb_disable_controller(fbi);
  462. return 0;
  463. }
  464. static int imxfb_resume(struct platform_device *dev)
  465. {
  466. struct imxfb_info *fbi = platform_get_drvdata(dev);
  467. pr_debug("%s\n", __func__);
  468. imxfb_enable_controller(fbi);
  469. return 0;
  470. }
  471. #else
  472. #define imxfb_suspend NULL
  473. #define imxfb_resume NULL
  474. #endif
  475. static int __init imxfb_init_fbinfo(struct platform_device *pdev)
  476. {
  477. struct imx_fb_platform_data *pdata = pdev->dev.platform_data;
  478. struct fb_info *info = dev_get_drvdata(&pdev->dev);
  479. struct imxfb_info *fbi = info->par;
  480. pr_debug("%s\n",__func__);
  481. info->pseudo_palette = kmalloc(sizeof(u32) * 16, GFP_KERNEL);
  482. if (!info->pseudo_palette)
  483. return -ENOMEM;
  484. memset(fbi, 0, sizeof(struct imxfb_info));
  485. strlcpy(info->fix.id, IMX_NAME, sizeof(info->fix.id));
  486. info->fix.type = FB_TYPE_PACKED_PIXELS;
  487. info->fix.type_aux = 0;
  488. info->fix.xpanstep = 0;
  489. info->fix.ypanstep = 0;
  490. info->fix.ywrapstep = 0;
  491. info->fix.accel = FB_ACCEL_NONE;
  492. info->var.nonstd = 0;
  493. info->var.activate = FB_ACTIVATE_NOW;
  494. info->var.height = -1;
  495. info->var.width = -1;
  496. info->var.accel_flags = 0;
  497. info->var.vmode = FB_VMODE_NONINTERLACED;
  498. info->fbops = &imxfb_ops;
  499. info->flags = FBINFO_FLAG_DEFAULT |
  500. FBINFO_READS_FAST;
  501. fbi->max_xres = pdata->xres;
  502. info->var.xres = pdata->xres;
  503. info->var.xres_virtual = pdata->xres;
  504. fbi->max_yres = pdata->yres;
  505. info->var.yres = pdata->yres;
  506. info->var.yres_virtual = pdata->yres;
  507. fbi->max_bpp = pdata->bpp;
  508. info->var.bits_per_pixel = pdata->bpp;
  509. info->var.nonstd = pdata->nonstd;
  510. info->var.pixclock = pdata->pixclock;
  511. info->var.hsync_len = pdata->hsync_len;
  512. info->var.left_margin = pdata->left_margin;
  513. info->var.right_margin = pdata->right_margin;
  514. info->var.vsync_len = pdata->vsync_len;
  515. info->var.upper_margin = pdata->upper_margin;
  516. info->var.lower_margin = pdata->lower_margin;
  517. info->var.sync = pdata->sync;
  518. info->var.grayscale = pdata->cmap_greyscale;
  519. fbi->cmap_inverse = pdata->cmap_inverse;
  520. fbi->cmap_static = pdata->cmap_static;
  521. fbi->pcr = pdata->pcr;
  522. fbi->lscr1 = pdata->lscr1;
  523. fbi->dmacr = pdata->dmacr;
  524. fbi->pwmr = pdata->pwmr;
  525. fbi->lcd_power = pdata->lcd_power;
  526. fbi->backlight_power = pdata->backlight_power;
  527. info->fix.smem_len = fbi->max_xres * fbi->max_yres *
  528. fbi->max_bpp / 8;
  529. return 0;
  530. }
  531. static int __init imxfb_probe(struct platform_device *pdev)
  532. {
  533. struct imxfb_info *fbi;
  534. struct fb_info *info;
  535. struct imx_fb_platform_data *pdata;
  536. struct resource *res;
  537. int ret;
  538. printk("i.MX Framebuffer driver\n");
  539. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  540. if (!res)
  541. return -ENODEV;
  542. pdata = pdev->dev.platform_data;
  543. if (!pdata) {
  544. dev_err(&pdev->dev,"No platform_data available\n");
  545. return -ENOMEM;
  546. }
  547. info = framebuffer_alloc(sizeof(struct imxfb_info), &pdev->dev);
  548. if (!info)
  549. return -ENOMEM;
  550. fbi = info->par;
  551. platform_set_drvdata(pdev, info);
  552. ret = imxfb_init_fbinfo(pdev);
  553. if (ret < 0)
  554. goto failed_init;
  555. res = request_mem_region(res->start, resource_size(res),
  556. DRIVER_NAME);
  557. if (!res) {
  558. ret = -EBUSY;
  559. goto failed_req;
  560. }
  561. fbi->clk = clk_get(&pdev->dev, NULL);
  562. if (IS_ERR(fbi->clk)) {
  563. ret = PTR_ERR(fbi->clk);;
  564. dev_err(&pdev->dev, "unable to get clock: %d\n", ret);
  565. goto failed_getclock;
  566. }
  567. fbi->regs = ioremap(res->start, resource_size(res));
  568. if (fbi->regs == NULL) {
  569. printk(KERN_ERR"Cannot map frame buffer registers\n");
  570. goto failed_ioremap;
  571. }
  572. if (!pdata->fixed_screen_cpu) {
  573. fbi->map_size = PAGE_ALIGN(info->fix.smem_len);
  574. fbi->map_cpu = dma_alloc_writecombine(&pdev->dev,
  575. fbi->map_size, &fbi->map_dma, GFP_KERNEL);
  576. if (!fbi->map_cpu) {
  577. dev_err(&pdev->dev, "Failed to allocate video RAM: %d\n", ret);
  578. ret = -ENOMEM;
  579. goto failed_map;
  580. }
  581. info->screen_base = fbi->map_cpu;
  582. fbi->screen_cpu = fbi->map_cpu;
  583. fbi->screen_dma = fbi->map_dma;
  584. info->fix.smem_start = fbi->screen_dma;
  585. } else {
  586. /* Fixed framebuffer mapping enables location of the screen in eSRAM */
  587. fbi->map_cpu = pdata->fixed_screen_cpu;
  588. fbi->map_dma = pdata->fixed_screen_dma;
  589. info->screen_base = fbi->map_cpu;
  590. fbi->screen_cpu = fbi->map_cpu;
  591. fbi->screen_dma = fbi->map_dma;
  592. info->fix.smem_start = fbi->screen_dma;
  593. }
  594. if (pdata->init) {
  595. ret = pdata->init(fbi->pdev);
  596. if (ret)
  597. goto failed_platform_init;
  598. }
  599. /*
  600. * This makes sure that our colour bitfield
  601. * descriptors are correctly initialised.
  602. */
  603. imxfb_check_var(&info->var, info);
  604. ret = fb_alloc_cmap(&info->cmap, 1 << info->var.bits_per_pixel, 0);
  605. if (ret < 0)
  606. goto failed_cmap;
  607. imxfb_set_par(info);
  608. ret = register_framebuffer(info);
  609. if (ret < 0) {
  610. dev_err(&pdev->dev, "failed to register framebuffer\n");
  611. goto failed_register;
  612. }
  613. imxfb_enable_controller(fbi);
  614. return 0;
  615. failed_register:
  616. fb_dealloc_cmap(&info->cmap);
  617. failed_cmap:
  618. if (pdata->exit)
  619. pdata->exit(fbi->pdev);
  620. failed_platform_init:
  621. if (!pdata->fixed_screen_cpu)
  622. dma_free_writecombine(&pdev->dev,fbi->map_size,fbi->map_cpu,
  623. fbi->map_dma);
  624. failed_map:
  625. clk_put(fbi->clk);
  626. failed_getclock:
  627. iounmap(fbi->regs);
  628. failed_ioremap:
  629. release_mem_region(res->start, res->end - res->start);
  630. failed_req:
  631. kfree(info->pseudo_palette);
  632. failed_init:
  633. platform_set_drvdata(pdev, NULL);
  634. framebuffer_release(info);
  635. return ret;
  636. }
  637. static int __devexit imxfb_remove(struct platform_device *pdev)
  638. {
  639. struct imx_fb_platform_data *pdata;
  640. struct fb_info *info = platform_get_drvdata(pdev);
  641. struct imxfb_info *fbi = info->par;
  642. struct resource *res;
  643. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  644. imxfb_disable_controller(fbi);
  645. unregister_framebuffer(info);
  646. pdata = pdev->dev.platform_data;
  647. if (pdata->exit)
  648. pdata->exit(fbi->pdev);
  649. fb_dealloc_cmap(&info->cmap);
  650. kfree(info->pseudo_palette);
  651. framebuffer_release(info);
  652. iounmap(fbi->regs);
  653. release_mem_region(res->start, res->end - res->start + 1);
  654. clk_disable(fbi->clk);
  655. clk_put(fbi->clk);
  656. platform_set_drvdata(pdev, NULL);
  657. return 0;
  658. }
  659. void imxfb_shutdown(struct platform_device * dev)
  660. {
  661. struct fb_info *info = platform_get_drvdata(dev);
  662. struct imxfb_info *fbi = info->par;
  663. imxfb_disable_controller(fbi);
  664. }
  665. static struct platform_driver imxfb_driver = {
  666. .suspend = imxfb_suspend,
  667. .resume = imxfb_resume,
  668. .remove = __devexit_p(imxfb_remove),
  669. .shutdown = imxfb_shutdown,
  670. .driver = {
  671. .name = DRIVER_NAME,
  672. },
  673. };
  674. int __init imxfb_init(void)
  675. {
  676. return platform_driver_probe(&imxfb_driver, imxfb_probe);
  677. }
  678. static void __exit imxfb_cleanup(void)
  679. {
  680. platform_driver_unregister(&imxfb_driver);
  681. }
  682. module_init(imxfb_init);
  683. module_exit(imxfb_cleanup);
  684. MODULE_DESCRIPTION("Motorola i.MX framebuffer driver");
  685. MODULE_AUTHOR("Sascha Hauer, Pengutronix");
  686. MODULE_LICENSE("GPL");