imxfb.c 26 KB

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