imxfb.c 25 KB

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