mxsfb.c 24 KB

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