imxfb.c 21 KB

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