imxfb.c 27 KB

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