mxsfb.c 23 KB

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