imxfb.c 23 KB

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