mxsfb.c 25 KB

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