imxfb.c 23 KB

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