mxsfb.c 26 KB

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