imxfb.c 23 KB

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