mxsfb.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. /*
  2. * Copyright (C) 2010 Juergen Beisert, Pengutronix
  3. *
  4. * This code is based on:
  5. * Author: Vitaly Wool <vital@embeddedalley.com>
  6. *
  7. * Copyright 2008-2009 Freescale Semiconductor, Inc. All Rights Reserved.
  8. * Copyright 2008 Embedded Alley Solutions, Inc All Rights Reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or (at your option) any later version.
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #define DRIVER_NAME "mxsfb"
  20. /**
  21. * @file
  22. * @brief LCDIF driver for i.MX23 and i.MX28
  23. *
  24. * The LCDIF support four modes of operation
  25. * - MPU interface (to drive smart displays) -> not supported yet
  26. * - VSYNC interface (like MPU interface plus Vsync) -> not supported yet
  27. * - Dotclock interface (to drive LC displays with RGB data and sync signals)
  28. * - DVI (to drive ITU-R BT656) -> not supported yet
  29. *
  30. * This driver depends on a correct setup of the pins used for this purpose
  31. * (platform specific).
  32. *
  33. * For the developer: Don't forget to set the data bus width to the display
  34. * in the imx_fb_videomode structure. You will else end up with ugly colours.
  35. * If you fight against jitter you can vary the clock delay. This is a feature
  36. * of the i.MX28 and you can vary it between 2 ns ... 8 ns in 2 ns steps. Give
  37. * the required value in the imx_fb_videomode structure.
  38. */
  39. #include <linux/module.h>
  40. #include <linux/kernel.h>
  41. #include <linux/of_device.h>
  42. #include <linux/platform_device.h>
  43. #include <linux/clk.h>
  44. #include <linux/dma-mapping.h>
  45. #include <linux/io.h>
  46. #include <linux/pinctrl/consumer.h>
  47. #include <linux/fb.h>
  48. #include <linux/regulator/consumer.h>
  49. #include <video/of_display_timing.h>
  50. #include <video/videomode.h>
  51. #define REG_SET 4
  52. #define REG_CLR 8
  53. #define LCDC_CTRL 0x00
  54. #define LCDC_CTRL1 0x10
  55. #define LCDC_V4_CTRL2 0x20
  56. #define LCDC_V3_TRANSFER_COUNT 0x20
  57. #define LCDC_V4_TRANSFER_COUNT 0x30
  58. #define LCDC_V4_CUR_BUF 0x40
  59. #define LCDC_V4_NEXT_BUF 0x50
  60. #define LCDC_V3_CUR_BUF 0x30
  61. #define LCDC_V3_NEXT_BUF 0x40
  62. #define LCDC_TIMING 0x60
  63. #define LCDC_VDCTRL0 0x70
  64. #define LCDC_VDCTRL1 0x80
  65. #define LCDC_VDCTRL2 0x90
  66. #define LCDC_VDCTRL3 0xa0
  67. #define LCDC_VDCTRL4 0xb0
  68. #define LCDC_DVICTRL0 0xc0
  69. #define LCDC_DVICTRL1 0xd0
  70. #define LCDC_DVICTRL2 0xe0
  71. #define LCDC_DVICTRL3 0xf0
  72. #define LCDC_DVICTRL4 0x100
  73. #define LCDC_V4_DATA 0x180
  74. #define LCDC_V3_DATA 0x1b0
  75. #define LCDC_V4_DEBUG0 0x1d0
  76. #define LCDC_V3_DEBUG0 0x1f0
  77. #define CTRL_SFTRST (1 << 31)
  78. #define CTRL_CLKGATE (1 << 30)
  79. #define CTRL_BYPASS_COUNT (1 << 19)
  80. #define CTRL_VSYNC_MODE (1 << 18)
  81. #define CTRL_DOTCLK_MODE (1 << 17)
  82. #define CTRL_DATA_SELECT (1 << 16)
  83. #define CTRL_SET_BUS_WIDTH(x) (((x) & 0x3) << 10)
  84. #define CTRL_GET_BUS_WIDTH(x) (((x) >> 10) & 0x3)
  85. #define CTRL_SET_WORD_LENGTH(x) (((x) & 0x3) << 8)
  86. #define CTRL_GET_WORD_LENGTH(x) (((x) >> 8) & 0x3)
  87. #define CTRL_MASTER (1 << 5)
  88. #define CTRL_DF16 (1 << 3)
  89. #define CTRL_DF18 (1 << 2)
  90. #define CTRL_DF24 (1 << 1)
  91. #define CTRL_RUN (1 << 0)
  92. #define CTRL1_FIFO_CLEAR (1 << 21)
  93. #define CTRL1_SET_BYTE_PACKAGING(x) (((x) & 0xf) << 16)
  94. #define CTRL1_GET_BYTE_PACKAGING(x) (((x) >> 16) & 0xf)
  95. #define TRANSFER_COUNT_SET_VCOUNT(x) (((x) & 0xffff) << 16)
  96. #define TRANSFER_COUNT_GET_VCOUNT(x) (((x) >> 16) & 0xffff)
  97. #define TRANSFER_COUNT_SET_HCOUNT(x) ((x) & 0xffff)
  98. #define TRANSFER_COUNT_GET_HCOUNT(x) ((x) & 0xffff)
  99. #define VDCTRL0_ENABLE_PRESENT (1 << 28)
  100. #define VDCTRL0_VSYNC_ACT_HIGH (1 << 27)
  101. #define VDCTRL0_HSYNC_ACT_HIGH (1 << 26)
  102. #define VDCTRL0_DOTCLK_ACT_FALLING (1 << 25)
  103. #define VDCTRL0_ENABLE_ACT_HIGH (1 << 24)
  104. #define VDCTRL0_VSYNC_PERIOD_UNIT (1 << 21)
  105. #define VDCTRL0_VSYNC_PULSE_WIDTH_UNIT (1 << 20)
  106. #define VDCTRL0_HALF_LINE (1 << 19)
  107. #define VDCTRL0_HALF_LINE_MODE (1 << 18)
  108. #define VDCTRL0_SET_VSYNC_PULSE_WIDTH(x) ((x) & 0x3ffff)
  109. #define VDCTRL0_GET_VSYNC_PULSE_WIDTH(x) ((x) & 0x3ffff)
  110. #define VDCTRL2_SET_HSYNC_PERIOD(x) ((x) & 0x3ffff)
  111. #define VDCTRL2_GET_HSYNC_PERIOD(x) ((x) & 0x3ffff)
  112. #define VDCTRL3_MUX_SYNC_SIGNALS (1 << 29)
  113. #define VDCTRL3_VSYNC_ONLY (1 << 28)
  114. #define SET_HOR_WAIT_CNT(x) (((x) & 0xfff) << 16)
  115. #define GET_HOR_WAIT_CNT(x) (((x) >> 16) & 0xfff)
  116. #define SET_VERT_WAIT_CNT(x) ((x) & 0xffff)
  117. #define GET_VERT_WAIT_CNT(x) ((x) & 0xffff)
  118. #define VDCTRL4_SET_DOTCLK_DLY(x) (((x) & 0x7) << 29) /* v4 only */
  119. #define VDCTRL4_GET_DOTCLK_DLY(x) (((x) >> 29) & 0x7) /* v4 only */
  120. #define VDCTRL4_SYNC_SIGNALS_ON (1 << 18)
  121. #define SET_DOTCLK_H_VALID_DATA_CNT(x) ((x) & 0x3ffff)
  122. #define DEBUG0_HSYNC (1 < 26)
  123. #define DEBUG0_VSYNC (1 < 25)
  124. #define MIN_XRES 120
  125. #define MIN_YRES 120
  126. #define RED 0
  127. #define GREEN 1
  128. #define BLUE 2
  129. #define TRANSP 3
  130. #define STMLCDIF_8BIT 1 /** pixel data bus to the display is of 8 bit width */
  131. #define STMLCDIF_16BIT 0 /** pixel data bus to the display is of 16 bit width */
  132. #define STMLCDIF_18BIT 2 /** pixel data bus to the display is of 18 bit width */
  133. #define STMLCDIF_24BIT 3 /** pixel data bus to the display is of 24 bit width */
  134. #define MXSFB_SYNC_DATA_ENABLE_HIGH_ACT (1 << 6)
  135. #define MXSFB_SYNC_DOTCLK_FALLING_ACT (1 << 7) /* negtive edge sampling */
  136. enum mxsfb_devtype {
  137. MXSFB_V3,
  138. MXSFB_V4,
  139. };
  140. /* CPU dependent register offsets */
  141. struct mxsfb_devdata {
  142. unsigned transfer_count;
  143. unsigned cur_buf;
  144. unsigned next_buf;
  145. unsigned debug0;
  146. unsigned hs_wdth_mask;
  147. unsigned hs_wdth_shift;
  148. unsigned ipversion;
  149. };
  150. struct mxsfb_info {
  151. struct fb_info fb_info;
  152. struct platform_device *pdev;
  153. struct clk *clk;
  154. void __iomem *base; /* registers */
  155. unsigned allocated_size;
  156. int enabled;
  157. unsigned ld_intf_width;
  158. unsigned dotclk_delay;
  159. const struct mxsfb_devdata *devdata;
  160. u32 sync;
  161. struct regulator *reg_lcd;
  162. };
  163. #define mxsfb_is_v3(host) (host->devdata->ipversion == 3)
  164. #define mxsfb_is_v4(host) (host->devdata->ipversion == 4)
  165. static const struct mxsfb_devdata mxsfb_devdata[] = {
  166. [MXSFB_V3] = {
  167. .transfer_count = LCDC_V3_TRANSFER_COUNT,
  168. .cur_buf = LCDC_V3_CUR_BUF,
  169. .next_buf = LCDC_V3_NEXT_BUF,
  170. .debug0 = LCDC_V3_DEBUG0,
  171. .hs_wdth_mask = 0xff,
  172. .hs_wdth_shift = 24,
  173. .ipversion = 3,
  174. },
  175. [MXSFB_V4] = {
  176. .transfer_count = LCDC_V4_TRANSFER_COUNT,
  177. .cur_buf = LCDC_V4_CUR_BUF,
  178. .next_buf = LCDC_V4_NEXT_BUF,
  179. .debug0 = LCDC_V4_DEBUG0,
  180. .hs_wdth_mask = 0x3fff,
  181. .hs_wdth_shift = 18,
  182. .ipversion = 4,
  183. },
  184. };
  185. #define to_imxfb_host(x) (container_of(x, struct mxsfb_info, fb_info))
  186. /* mask and shift depends on architecture */
  187. static inline u32 set_hsync_pulse_width(struct mxsfb_info *host, unsigned val)
  188. {
  189. return (val & host->devdata->hs_wdth_mask) <<
  190. host->devdata->hs_wdth_shift;
  191. }
  192. static inline u32 get_hsync_pulse_width(struct mxsfb_info *host, unsigned val)
  193. {
  194. return (val >> host->devdata->hs_wdth_shift) &
  195. host->devdata->hs_wdth_mask;
  196. }
  197. static const struct fb_bitfield def_rgb565[] = {
  198. [RED] = {
  199. .offset = 11,
  200. .length = 5,
  201. },
  202. [GREEN] = {
  203. .offset = 5,
  204. .length = 6,
  205. },
  206. [BLUE] = {
  207. .offset = 0,
  208. .length = 5,
  209. },
  210. [TRANSP] = { /* no support for transparency */
  211. .length = 0,
  212. }
  213. };
  214. static const struct fb_bitfield def_rgb666[] = {
  215. [RED] = {
  216. .offset = 16,
  217. .length = 6,
  218. },
  219. [GREEN] = {
  220. .offset = 8,
  221. .length = 6,
  222. },
  223. [BLUE] = {
  224. .offset = 0,
  225. .length = 6,
  226. },
  227. [TRANSP] = { /* no support for transparency */
  228. .length = 0,
  229. }
  230. };
  231. static const struct fb_bitfield def_rgb888[] = {
  232. [RED] = {
  233. .offset = 16,
  234. .length = 8,
  235. },
  236. [GREEN] = {
  237. .offset = 8,
  238. .length = 8,
  239. },
  240. [BLUE] = {
  241. .offset = 0,
  242. .length = 8,
  243. },
  244. [TRANSP] = { /* no support for transparency */
  245. .length = 0,
  246. }
  247. };
  248. static inline unsigned chan_to_field(unsigned chan, struct fb_bitfield *bf)
  249. {
  250. chan &= 0xffff;
  251. chan >>= 16 - bf->length;
  252. return chan << bf->offset;
  253. }
  254. static int mxsfb_check_var(struct fb_var_screeninfo *var,
  255. struct fb_info *fb_info)
  256. {
  257. struct mxsfb_info *host = to_imxfb_host(fb_info);
  258. const struct fb_bitfield *rgb = NULL;
  259. if (var->xres < MIN_XRES)
  260. var->xres = MIN_XRES;
  261. if (var->yres < MIN_YRES)
  262. var->yres = MIN_YRES;
  263. var->xres_virtual = var->xres;
  264. var->yres_virtual = var->yres;
  265. switch (var->bits_per_pixel) {
  266. case 16:
  267. /* always expect RGB 565 */
  268. rgb = def_rgb565;
  269. break;
  270. case 32:
  271. switch (host->ld_intf_width) {
  272. case STMLCDIF_8BIT:
  273. pr_debug("Unsupported LCD bus width mapping\n");
  274. break;
  275. case STMLCDIF_16BIT:
  276. case STMLCDIF_18BIT:
  277. /* 24 bit to 18 bit mapping */
  278. rgb = def_rgb666;
  279. break;
  280. case STMLCDIF_24BIT:
  281. /* real 24 bit */
  282. rgb = def_rgb888;
  283. break;
  284. }
  285. break;
  286. default:
  287. pr_debug("Unsupported colour depth: %u\n", var->bits_per_pixel);
  288. return -EINVAL;
  289. }
  290. /*
  291. * Copy the RGB parameters for this display
  292. * from the machine specific parameters.
  293. */
  294. var->red = rgb[RED];
  295. var->green = rgb[GREEN];
  296. var->blue = rgb[BLUE];
  297. var->transp = rgb[TRANSP];
  298. return 0;
  299. }
  300. static void mxsfb_enable_controller(struct fb_info *fb_info)
  301. {
  302. struct mxsfb_info *host = to_imxfb_host(fb_info);
  303. u32 reg;
  304. int ret;
  305. dev_dbg(&host->pdev->dev, "%s\n", __func__);
  306. if (host->reg_lcd) {
  307. ret = regulator_enable(host->reg_lcd);
  308. if (ret) {
  309. dev_err(&host->pdev->dev,
  310. "lcd regulator enable failed: %d\n", ret);
  311. return;
  312. }
  313. }
  314. clk_prepare_enable(host->clk);
  315. clk_set_rate(host->clk, PICOS2KHZ(fb_info->var.pixclock) * 1000U);
  316. /* if it was disabled, re-enable the mode again */
  317. writel(CTRL_DOTCLK_MODE, host->base + LCDC_CTRL + REG_SET);
  318. /* enable the SYNC signals first, then the DMA engine */
  319. reg = readl(host->base + LCDC_VDCTRL4);
  320. reg |= VDCTRL4_SYNC_SIGNALS_ON;
  321. writel(reg, host->base + LCDC_VDCTRL4);
  322. writel(CTRL_RUN, host->base + LCDC_CTRL + REG_SET);
  323. host->enabled = 1;
  324. }
  325. static void mxsfb_disable_controller(struct fb_info *fb_info)
  326. {
  327. struct mxsfb_info *host = to_imxfb_host(fb_info);
  328. unsigned loop;
  329. u32 reg;
  330. int ret;
  331. dev_dbg(&host->pdev->dev, "%s\n", __func__);
  332. /*
  333. * Even if we disable the controller here, it will still continue
  334. * until its FIFOs are running out of data
  335. */
  336. writel(CTRL_DOTCLK_MODE, host->base + LCDC_CTRL + REG_CLR);
  337. loop = 1000;
  338. while (loop) {
  339. reg = readl(host->base + LCDC_CTRL);
  340. if (!(reg & CTRL_RUN))
  341. break;
  342. loop--;
  343. }
  344. reg = readl(host->base + LCDC_VDCTRL4);
  345. writel(reg & ~VDCTRL4_SYNC_SIGNALS_ON, host->base + LCDC_VDCTRL4);
  346. clk_disable_unprepare(host->clk);
  347. host->enabled = 0;
  348. if (host->reg_lcd) {
  349. ret = regulator_disable(host->reg_lcd);
  350. if (ret)
  351. dev_err(&host->pdev->dev,
  352. "lcd regulator disable failed: %d\n", ret);
  353. }
  354. }
  355. static int mxsfb_set_par(struct fb_info *fb_info)
  356. {
  357. struct mxsfb_info *host = to_imxfb_host(fb_info);
  358. u32 ctrl, vdctrl0, vdctrl4;
  359. int line_size, fb_size;
  360. int reenable = 0;
  361. line_size = fb_info->var.xres * (fb_info->var.bits_per_pixel >> 3);
  362. fb_size = fb_info->var.yres_virtual * line_size;
  363. if (fb_size > fb_info->fix.smem_len)
  364. return -ENOMEM;
  365. fb_info->fix.line_length = line_size;
  366. /*
  367. * It seems, you can't re-program the controller if it is still running.
  368. * This may lead into shifted pictures (FIFO issue?).
  369. * So, first stop the controller and drain its FIFOs
  370. */
  371. if (host->enabled) {
  372. reenable = 1;
  373. mxsfb_disable_controller(fb_info);
  374. }
  375. /* clear the FIFOs */
  376. writel(CTRL1_FIFO_CLEAR, host->base + LCDC_CTRL1 + REG_SET);
  377. ctrl = CTRL_BYPASS_COUNT | CTRL_MASTER |
  378. CTRL_SET_BUS_WIDTH(host->ld_intf_width);
  379. switch (fb_info->var.bits_per_pixel) {
  380. case 16:
  381. dev_dbg(&host->pdev->dev, "Setting up RGB565 mode\n");
  382. ctrl |= CTRL_SET_WORD_LENGTH(0);
  383. writel(CTRL1_SET_BYTE_PACKAGING(0xf), host->base + LCDC_CTRL1);
  384. break;
  385. case 32:
  386. dev_dbg(&host->pdev->dev, "Setting up RGB888/666 mode\n");
  387. ctrl |= CTRL_SET_WORD_LENGTH(3);
  388. switch (host->ld_intf_width) {
  389. case STMLCDIF_8BIT:
  390. dev_dbg(&host->pdev->dev,
  391. "Unsupported LCD bus width mapping\n");
  392. return -EINVAL;
  393. case STMLCDIF_16BIT:
  394. case STMLCDIF_18BIT:
  395. /* 24 bit to 18 bit mapping */
  396. ctrl |= CTRL_DF24; /* ignore the upper 2 bits in
  397. * each colour component
  398. */
  399. break;
  400. case STMLCDIF_24BIT:
  401. /* real 24 bit */
  402. break;
  403. }
  404. /* do not use packed pixels = one pixel per word instead */
  405. writel(CTRL1_SET_BYTE_PACKAGING(0x7), host->base + LCDC_CTRL1);
  406. break;
  407. default:
  408. dev_dbg(&host->pdev->dev, "Unhandled color depth of %u\n",
  409. fb_info->var.bits_per_pixel);
  410. return -EINVAL;
  411. }
  412. writel(ctrl, host->base + LCDC_CTRL);
  413. writel(TRANSFER_COUNT_SET_VCOUNT(fb_info->var.yres) |
  414. TRANSFER_COUNT_SET_HCOUNT(fb_info->var.xres),
  415. host->base + host->devdata->transfer_count);
  416. vdctrl0 = VDCTRL0_ENABLE_PRESENT | /* always in DOTCLOCK mode */
  417. VDCTRL0_VSYNC_PERIOD_UNIT |
  418. VDCTRL0_VSYNC_PULSE_WIDTH_UNIT |
  419. VDCTRL0_SET_VSYNC_PULSE_WIDTH(fb_info->var.vsync_len);
  420. if (fb_info->var.sync & FB_SYNC_HOR_HIGH_ACT)
  421. vdctrl0 |= VDCTRL0_HSYNC_ACT_HIGH;
  422. if (fb_info->var.sync & FB_SYNC_VERT_HIGH_ACT)
  423. vdctrl0 |= VDCTRL0_VSYNC_ACT_HIGH;
  424. if (host->sync & MXSFB_SYNC_DATA_ENABLE_HIGH_ACT)
  425. vdctrl0 |= VDCTRL0_ENABLE_ACT_HIGH;
  426. if (host->sync & MXSFB_SYNC_DOTCLK_FALLING_ACT)
  427. vdctrl0 |= VDCTRL0_DOTCLK_ACT_FALLING;
  428. writel(vdctrl0, host->base + LCDC_VDCTRL0);
  429. /* frame length in lines */
  430. writel(fb_info->var.upper_margin + fb_info->var.vsync_len +
  431. fb_info->var.lower_margin + fb_info->var.yres,
  432. host->base + LCDC_VDCTRL1);
  433. /* line length in units of clocks or pixels */
  434. writel(set_hsync_pulse_width(host, fb_info->var.hsync_len) |
  435. VDCTRL2_SET_HSYNC_PERIOD(fb_info->var.left_margin +
  436. fb_info->var.hsync_len + fb_info->var.right_margin +
  437. fb_info->var.xres),
  438. host->base + LCDC_VDCTRL2);
  439. writel(SET_HOR_WAIT_CNT(fb_info->var.left_margin +
  440. fb_info->var.hsync_len) |
  441. SET_VERT_WAIT_CNT(fb_info->var.upper_margin +
  442. fb_info->var.vsync_len),
  443. host->base + LCDC_VDCTRL3);
  444. vdctrl4 = SET_DOTCLK_H_VALID_DATA_CNT(fb_info->var.xres);
  445. if (mxsfb_is_v4(host))
  446. vdctrl4 |= VDCTRL4_SET_DOTCLK_DLY(host->dotclk_delay);
  447. writel(vdctrl4, host->base + LCDC_VDCTRL4);
  448. writel(fb_info->fix.smem_start +
  449. fb_info->fix.line_length * fb_info->var.yoffset,
  450. host->base + host->devdata->next_buf);
  451. if (reenable)
  452. mxsfb_enable_controller(fb_info);
  453. return 0;
  454. }
  455. static int mxsfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  456. u_int transp, struct fb_info *fb_info)
  457. {
  458. unsigned int val;
  459. int ret = -EINVAL;
  460. /*
  461. * If greyscale is true, then we convert the RGB value
  462. * to greyscale no matter what visual we are using.
  463. */
  464. if (fb_info->var.grayscale)
  465. red = green = blue = (19595 * red + 38470 * green +
  466. 7471 * blue) >> 16;
  467. switch (fb_info->fix.visual) {
  468. case FB_VISUAL_TRUECOLOR:
  469. /*
  470. * 12 or 16-bit True Colour. We encode the RGB value
  471. * according to the RGB bitfield information.
  472. */
  473. if (regno < 16) {
  474. u32 *pal = fb_info->pseudo_palette;
  475. val = chan_to_field(red, &fb_info->var.red);
  476. val |= chan_to_field(green, &fb_info->var.green);
  477. val |= chan_to_field(blue, &fb_info->var.blue);
  478. pal[regno] = val;
  479. ret = 0;
  480. }
  481. break;
  482. case FB_VISUAL_STATIC_PSEUDOCOLOR:
  483. case FB_VISUAL_PSEUDOCOLOR:
  484. break;
  485. }
  486. return ret;
  487. }
  488. static int mxsfb_blank(int blank, struct fb_info *fb_info)
  489. {
  490. struct mxsfb_info *host = to_imxfb_host(fb_info);
  491. switch (blank) {
  492. case FB_BLANK_POWERDOWN:
  493. case FB_BLANK_VSYNC_SUSPEND:
  494. case FB_BLANK_HSYNC_SUSPEND:
  495. case FB_BLANK_NORMAL:
  496. if (host->enabled)
  497. mxsfb_disable_controller(fb_info);
  498. break;
  499. case FB_BLANK_UNBLANK:
  500. if (!host->enabled)
  501. mxsfb_enable_controller(fb_info);
  502. break;
  503. }
  504. return 0;
  505. }
  506. static int mxsfb_pan_display(struct fb_var_screeninfo *var,
  507. struct fb_info *fb_info)
  508. {
  509. struct mxsfb_info *host = to_imxfb_host(fb_info);
  510. unsigned offset;
  511. if (var->xoffset != 0)
  512. return -EINVAL;
  513. offset = fb_info->fix.line_length * var->yoffset;
  514. /* update on next VSYNC */
  515. writel(fb_info->fix.smem_start + offset,
  516. host->base + host->devdata->next_buf);
  517. return 0;
  518. }
  519. static struct fb_ops mxsfb_ops = {
  520. .owner = THIS_MODULE,
  521. .fb_check_var = mxsfb_check_var,
  522. .fb_set_par = mxsfb_set_par,
  523. .fb_setcolreg = mxsfb_setcolreg,
  524. .fb_blank = mxsfb_blank,
  525. .fb_pan_display = mxsfb_pan_display,
  526. .fb_fillrect = cfb_fillrect,
  527. .fb_copyarea = cfb_copyarea,
  528. .fb_imageblit = cfb_imageblit,
  529. };
  530. static int mxsfb_restore_mode(struct mxsfb_info *host)
  531. {
  532. struct fb_info *fb_info = &host->fb_info;
  533. unsigned line_count;
  534. unsigned period;
  535. unsigned long pa, fbsize;
  536. int bits_per_pixel, ofs;
  537. u32 transfer_count, vdctrl0, vdctrl2, vdctrl3, vdctrl4, ctrl;
  538. struct fb_videomode vmode;
  539. /* Only restore the mode when the controller is running */
  540. ctrl = readl(host->base + LCDC_CTRL);
  541. if (!(ctrl & CTRL_RUN))
  542. return -EINVAL;
  543. vdctrl0 = readl(host->base + LCDC_VDCTRL0);
  544. vdctrl2 = readl(host->base + LCDC_VDCTRL2);
  545. vdctrl3 = readl(host->base + LCDC_VDCTRL3);
  546. vdctrl4 = readl(host->base + LCDC_VDCTRL4);
  547. transfer_count = readl(host->base + host->devdata->transfer_count);
  548. vmode.xres = TRANSFER_COUNT_GET_HCOUNT(transfer_count);
  549. vmode.yres = TRANSFER_COUNT_GET_VCOUNT(transfer_count);
  550. switch (CTRL_GET_WORD_LENGTH(ctrl)) {
  551. case 0:
  552. bits_per_pixel = 16;
  553. break;
  554. case 3:
  555. bits_per_pixel = 32;
  556. case 1:
  557. default:
  558. return -EINVAL;
  559. }
  560. fb_info->var.bits_per_pixel = bits_per_pixel;
  561. vmode.pixclock = KHZ2PICOS(clk_get_rate(host->clk) / 1000U);
  562. vmode.hsync_len = get_hsync_pulse_width(host, vdctrl2);
  563. vmode.left_margin = GET_HOR_WAIT_CNT(vdctrl3) - vmode.hsync_len;
  564. vmode.right_margin = VDCTRL2_GET_HSYNC_PERIOD(vdctrl2) - vmode.hsync_len -
  565. vmode.left_margin - vmode.xres;
  566. vmode.vsync_len = VDCTRL0_GET_VSYNC_PULSE_WIDTH(vdctrl0);
  567. period = readl(host->base + LCDC_VDCTRL1);
  568. vmode.upper_margin = GET_VERT_WAIT_CNT(vdctrl3) - vmode.vsync_len;
  569. vmode.lower_margin = period - vmode.vsync_len - vmode.upper_margin - vmode.yres;
  570. vmode.vmode = FB_VMODE_NONINTERLACED;
  571. vmode.sync = 0;
  572. if (vdctrl0 & VDCTRL0_HSYNC_ACT_HIGH)
  573. vmode.sync |= FB_SYNC_HOR_HIGH_ACT;
  574. if (vdctrl0 & VDCTRL0_VSYNC_ACT_HIGH)
  575. vmode.sync |= FB_SYNC_VERT_HIGH_ACT;
  576. pr_debug("Reconstructed video mode:\n");
  577. pr_debug("%dx%d, hsync: %u left: %u, right: %u, vsync: %u, upper: %u, lower: %u\n",
  578. vmode.xres, vmode.yres,
  579. vmode.hsync_len, vmode.left_margin, vmode.right_margin,
  580. vmode.vsync_len, vmode.upper_margin, vmode.lower_margin);
  581. pr_debug("pixclk: %ldkHz\n", PICOS2KHZ(vmode.pixclock));
  582. fb_add_videomode(&vmode, &fb_info->modelist);
  583. host->ld_intf_width = CTRL_GET_BUS_WIDTH(ctrl);
  584. host->dotclk_delay = VDCTRL4_GET_DOTCLK_DLY(vdctrl4);
  585. fb_info->fix.line_length = vmode.xres * (bits_per_pixel >> 3);
  586. pa = readl(host->base + host->devdata->cur_buf);
  587. fbsize = fb_info->fix.line_length * vmode.yres;
  588. if (pa < fb_info->fix.smem_start)
  589. return -EINVAL;
  590. if (pa + fbsize > fb_info->fix.smem_start + fb_info->fix.smem_len)
  591. return -EINVAL;
  592. ofs = pa - fb_info->fix.smem_start;
  593. if (ofs) {
  594. memmove(fb_info->screen_base, fb_info->screen_base + ofs, fbsize);
  595. writel(fb_info->fix.smem_start, host->base + host->devdata->next_buf);
  596. }
  597. line_count = fb_info->fix.smem_len / fb_info->fix.line_length;
  598. fb_info->fix.ypanstep = 1;
  599. clk_prepare_enable(host->clk);
  600. host->enabled = 1;
  601. return 0;
  602. }
  603. static int mxsfb_init_fbinfo_dt(struct mxsfb_info *host)
  604. {
  605. struct fb_info *fb_info = &host->fb_info;
  606. struct fb_var_screeninfo *var = &fb_info->var;
  607. struct device *dev = &host->pdev->dev;
  608. struct device_node *np = host->pdev->dev.of_node;
  609. struct device_node *display_np;
  610. struct device_node *timings_np;
  611. struct display_timings *timings;
  612. u32 width;
  613. int i;
  614. int ret = 0;
  615. display_np = of_parse_phandle(np, "display", 0);
  616. if (!display_np) {
  617. dev_err(dev, "failed to find display phandle\n");
  618. return -ENOENT;
  619. }
  620. ret = of_property_read_u32(display_np, "bus-width", &width);
  621. if (ret < 0) {
  622. dev_err(dev, "failed to get property bus-width\n");
  623. goto put_display_node;
  624. }
  625. switch (width) {
  626. case 8:
  627. host->ld_intf_width = STMLCDIF_8BIT;
  628. break;
  629. case 16:
  630. host->ld_intf_width = STMLCDIF_16BIT;
  631. break;
  632. case 18:
  633. host->ld_intf_width = STMLCDIF_18BIT;
  634. break;
  635. case 24:
  636. host->ld_intf_width = STMLCDIF_24BIT;
  637. break;
  638. default:
  639. dev_err(dev, "invalid bus-width value\n");
  640. ret = -EINVAL;
  641. goto put_display_node;
  642. }
  643. ret = of_property_read_u32(display_np, "bits-per-pixel",
  644. &var->bits_per_pixel);
  645. if (ret < 0) {
  646. dev_err(dev, "failed to get property bits-per-pixel\n");
  647. goto put_display_node;
  648. }
  649. timings = of_get_display_timings(display_np);
  650. if (!timings) {
  651. dev_err(dev, "failed to get display timings\n");
  652. ret = -ENOENT;
  653. goto put_display_node;
  654. }
  655. timings_np = of_find_node_by_name(display_np,
  656. "display-timings");
  657. if (!timings_np) {
  658. dev_err(dev, "failed to find display-timings node\n");
  659. ret = -ENOENT;
  660. goto put_display_node;
  661. }
  662. for (i = 0; i < of_get_child_count(timings_np); i++) {
  663. struct videomode vm;
  664. struct fb_videomode fb_vm;
  665. ret = videomode_from_timings(timings, &vm, i);
  666. if (ret < 0)
  667. goto put_timings_node;
  668. ret = fb_videomode_from_videomode(&vm, &fb_vm);
  669. if (ret < 0)
  670. goto put_timings_node;
  671. if (vm.flags & DISPLAY_FLAGS_DE_HIGH)
  672. host->sync |= MXSFB_SYNC_DATA_ENABLE_HIGH_ACT;
  673. if (vm.flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE)
  674. host->sync |= MXSFB_SYNC_DOTCLK_FALLING_ACT;
  675. fb_add_videomode(&fb_vm, &fb_info->modelist);
  676. }
  677. put_timings_node:
  678. of_node_put(timings_np);
  679. put_display_node:
  680. of_node_put(display_np);
  681. return ret;
  682. }
  683. static int mxsfb_init_fbinfo(struct mxsfb_info *host)
  684. {
  685. struct fb_info *fb_info = &host->fb_info;
  686. struct fb_var_screeninfo *var = &fb_info->var;
  687. dma_addr_t fb_phys;
  688. void *fb_virt;
  689. unsigned fb_size;
  690. int ret;
  691. fb_info->fbops = &mxsfb_ops;
  692. fb_info->flags = FBINFO_FLAG_DEFAULT | FBINFO_READS_FAST;
  693. strlcpy(fb_info->fix.id, "mxs", sizeof(fb_info->fix.id));
  694. fb_info->fix.type = FB_TYPE_PACKED_PIXELS;
  695. fb_info->fix.ypanstep = 1;
  696. fb_info->fix.visual = FB_VISUAL_TRUECOLOR,
  697. fb_info->fix.accel = FB_ACCEL_NONE;
  698. ret = mxsfb_init_fbinfo_dt(host);
  699. if (ret)
  700. return ret;
  701. var->nonstd = 0;
  702. var->activate = FB_ACTIVATE_NOW;
  703. var->accel_flags = 0;
  704. var->vmode = FB_VMODE_NONINTERLACED;
  705. /* Memory allocation for framebuffer */
  706. fb_size = SZ_2M;
  707. fb_virt = alloc_pages_exact(fb_size, GFP_DMA);
  708. if (!fb_virt)
  709. return -ENOMEM;
  710. fb_phys = virt_to_phys(fb_virt);
  711. fb_info->fix.smem_start = fb_phys;
  712. fb_info->screen_base = fb_virt;
  713. fb_info->screen_size = fb_info->fix.smem_len = fb_size;
  714. if (mxsfb_restore_mode(host))
  715. memset(fb_virt, 0, fb_size);
  716. return 0;
  717. }
  718. static void mxsfb_free_videomem(struct mxsfb_info *host)
  719. {
  720. struct fb_info *fb_info = &host->fb_info;
  721. free_pages_exact(fb_info->screen_base, fb_info->fix.smem_len);
  722. }
  723. static struct platform_device_id mxsfb_devtype[] = {
  724. {
  725. .name = "imx23-fb",
  726. .driver_data = MXSFB_V3,
  727. }, {
  728. .name = "imx28-fb",
  729. .driver_data = MXSFB_V4,
  730. }, {
  731. /* sentinel */
  732. }
  733. };
  734. MODULE_DEVICE_TABLE(platform, mxsfb_devtype);
  735. static const struct of_device_id mxsfb_dt_ids[] = {
  736. { .compatible = "fsl,imx23-lcdif", .data = &mxsfb_devtype[0], },
  737. { .compatible = "fsl,imx28-lcdif", .data = &mxsfb_devtype[1], },
  738. { /* sentinel */ }
  739. };
  740. MODULE_DEVICE_TABLE(of, mxsfb_dt_ids);
  741. static int mxsfb_probe(struct platform_device *pdev)
  742. {
  743. const struct of_device_id *of_id =
  744. of_match_device(mxsfb_dt_ids, &pdev->dev);
  745. struct resource *res;
  746. struct mxsfb_info *host;
  747. struct fb_info *fb_info;
  748. struct fb_modelist *modelist;
  749. struct pinctrl *pinctrl;
  750. int ret;
  751. if (of_id)
  752. pdev->id_entry = of_id->data;
  753. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  754. if (!res) {
  755. dev_err(&pdev->dev, "Cannot get memory IO resource\n");
  756. return -ENODEV;
  757. }
  758. fb_info = framebuffer_alloc(sizeof(struct mxsfb_info), &pdev->dev);
  759. if (!fb_info) {
  760. dev_err(&pdev->dev, "Failed to allocate fbdev\n");
  761. return -ENOMEM;
  762. }
  763. host = to_imxfb_host(fb_info);
  764. host->base = devm_ioremap_resource(&pdev->dev, res);
  765. if (IS_ERR(host->base)) {
  766. dev_err(&pdev->dev, "ioremap failed\n");
  767. ret = PTR_ERR(host->base);
  768. goto fb_release;
  769. }
  770. host->pdev = pdev;
  771. platform_set_drvdata(pdev, host);
  772. host->devdata = &mxsfb_devdata[pdev->id_entry->driver_data];
  773. pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
  774. if (IS_ERR(pinctrl)) {
  775. ret = PTR_ERR(pinctrl);
  776. goto fb_release;
  777. }
  778. host->clk = devm_clk_get(&host->pdev->dev, NULL);
  779. if (IS_ERR(host->clk)) {
  780. ret = PTR_ERR(host->clk);
  781. goto fb_release;
  782. }
  783. host->reg_lcd = devm_regulator_get(&pdev->dev, "lcd");
  784. if (IS_ERR(host->reg_lcd))
  785. host->reg_lcd = NULL;
  786. fb_info->pseudo_palette = devm_kzalloc(&pdev->dev, sizeof(u32) * 16,
  787. GFP_KERNEL);
  788. if (!fb_info->pseudo_palette) {
  789. ret = -ENOMEM;
  790. goto fb_release;
  791. }
  792. INIT_LIST_HEAD(&fb_info->modelist);
  793. ret = mxsfb_init_fbinfo(host);
  794. if (ret != 0)
  795. goto fb_release;
  796. modelist = list_first_entry(&fb_info->modelist,
  797. struct fb_modelist, list);
  798. fb_videomode_to_var(&fb_info->var, &modelist->mode);
  799. /* init the color fields */
  800. mxsfb_check_var(&fb_info->var, fb_info);
  801. platform_set_drvdata(pdev, fb_info);
  802. ret = register_framebuffer(fb_info);
  803. if (ret != 0) {
  804. dev_err(&pdev->dev,"Failed to register framebuffer\n");
  805. goto fb_destroy;
  806. }
  807. if (!host->enabled) {
  808. writel(0, host->base + LCDC_CTRL);
  809. mxsfb_set_par(fb_info);
  810. mxsfb_enable_controller(fb_info);
  811. }
  812. dev_info(&pdev->dev, "initialized\n");
  813. return 0;
  814. fb_destroy:
  815. if (host->enabled)
  816. clk_disable_unprepare(host->clk);
  817. fb_destroy_modelist(&fb_info->modelist);
  818. fb_release:
  819. framebuffer_release(fb_info);
  820. return ret;
  821. }
  822. static int mxsfb_remove(struct platform_device *pdev)
  823. {
  824. struct fb_info *fb_info = platform_get_drvdata(pdev);
  825. struct mxsfb_info *host = to_imxfb_host(fb_info);
  826. if (host->enabled)
  827. mxsfb_disable_controller(fb_info);
  828. unregister_framebuffer(fb_info);
  829. mxsfb_free_videomem(host);
  830. framebuffer_release(fb_info);
  831. platform_set_drvdata(pdev, NULL);
  832. return 0;
  833. }
  834. static void mxsfb_shutdown(struct platform_device *pdev)
  835. {
  836. struct fb_info *fb_info = platform_get_drvdata(pdev);
  837. struct mxsfb_info *host = to_imxfb_host(fb_info);
  838. /*
  839. * Force stop the LCD controller as keeping it running during reboot
  840. * might interfere with the BootROM's boot mode pads sampling.
  841. */
  842. writel(CTRL_RUN, host->base + LCDC_CTRL + REG_CLR);
  843. }
  844. static struct platform_driver mxsfb_driver = {
  845. .probe = mxsfb_probe,
  846. .remove = mxsfb_remove,
  847. .shutdown = mxsfb_shutdown,
  848. .id_table = mxsfb_devtype,
  849. .driver = {
  850. .name = DRIVER_NAME,
  851. .of_match_table = mxsfb_dt_ids,
  852. },
  853. };
  854. module_platform_driver(mxsfb_driver);
  855. MODULE_DESCRIPTION("Freescale mxs framebuffer driver");
  856. MODULE_AUTHOR("Sascha Hauer, Pengutronix");
  857. MODULE_LICENSE("GPL");