imxfb.c 24 KB

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