imxfb.c 24 KB

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