imxfb.c 22 KB

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