imxfb.c 24 KB

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