imxfb.c 22 KB

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