mxsfb.c 26 KB

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