mx3fb.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596
  1. /*
  2. * Copyright (C) 2008
  3. * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de>
  4. *
  5. * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/sched.h>
  15. #include <linux/errno.h>
  16. #include <linux/string.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/slab.h>
  19. #include <linux/fb.h>
  20. #include <linux/delay.h>
  21. #include <linux/init.h>
  22. #include <linux/ioport.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/dmaengine.h>
  25. #include <linux/console.h>
  26. #include <linux/clk.h>
  27. #include <linux/mutex.h>
  28. #include <mach/dma.h>
  29. #include <mach/hardware.h>
  30. #include <mach/ipu.h>
  31. #include <mach/mx3fb.h>
  32. #include <asm/io.h>
  33. #include <asm/uaccess.h>
  34. #define MX3FB_NAME "mx3_sdc_fb"
  35. #define MX3FB_REG_OFFSET 0xB4
  36. /* SDC Registers */
  37. #define SDC_COM_CONF (0xB4 - MX3FB_REG_OFFSET)
  38. #define SDC_GW_CTRL (0xB8 - MX3FB_REG_OFFSET)
  39. #define SDC_FG_POS (0xBC - MX3FB_REG_OFFSET)
  40. #define SDC_BG_POS (0xC0 - MX3FB_REG_OFFSET)
  41. #define SDC_CUR_POS (0xC4 - MX3FB_REG_OFFSET)
  42. #define SDC_PWM_CTRL (0xC8 - MX3FB_REG_OFFSET)
  43. #define SDC_CUR_MAP (0xCC - MX3FB_REG_OFFSET)
  44. #define SDC_HOR_CONF (0xD0 - MX3FB_REG_OFFSET)
  45. #define SDC_VER_CONF (0xD4 - MX3FB_REG_OFFSET)
  46. #define SDC_SHARP_CONF_1 (0xD8 - MX3FB_REG_OFFSET)
  47. #define SDC_SHARP_CONF_2 (0xDC - MX3FB_REG_OFFSET)
  48. /* Register bits */
  49. #define SDC_COM_TFT_COLOR 0x00000001UL
  50. #define SDC_COM_FG_EN 0x00000010UL
  51. #define SDC_COM_GWSEL 0x00000020UL
  52. #define SDC_COM_GLB_A 0x00000040UL
  53. #define SDC_COM_KEY_COLOR_G 0x00000080UL
  54. #define SDC_COM_BG_EN 0x00000200UL
  55. #define SDC_COM_SHARP 0x00001000UL
  56. #define SDC_V_SYNC_WIDTH_L 0x00000001UL
  57. /* Display Interface registers */
  58. #define DI_DISP_IF_CONF (0x0124 - MX3FB_REG_OFFSET)
  59. #define DI_DISP_SIG_POL (0x0128 - MX3FB_REG_OFFSET)
  60. #define DI_SER_DISP1_CONF (0x012C - MX3FB_REG_OFFSET)
  61. #define DI_SER_DISP2_CONF (0x0130 - MX3FB_REG_OFFSET)
  62. #define DI_HSP_CLK_PER (0x0134 - MX3FB_REG_OFFSET)
  63. #define DI_DISP0_TIME_CONF_1 (0x0138 - MX3FB_REG_OFFSET)
  64. #define DI_DISP0_TIME_CONF_2 (0x013C - MX3FB_REG_OFFSET)
  65. #define DI_DISP0_TIME_CONF_3 (0x0140 - MX3FB_REG_OFFSET)
  66. #define DI_DISP1_TIME_CONF_1 (0x0144 - MX3FB_REG_OFFSET)
  67. #define DI_DISP1_TIME_CONF_2 (0x0148 - MX3FB_REG_OFFSET)
  68. #define DI_DISP1_TIME_CONF_3 (0x014C - MX3FB_REG_OFFSET)
  69. #define DI_DISP2_TIME_CONF_1 (0x0150 - MX3FB_REG_OFFSET)
  70. #define DI_DISP2_TIME_CONF_2 (0x0154 - MX3FB_REG_OFFSET)
  71. #define DI_DISP2_TIME_CONF_3 (0x0158 - MX3FB_REG_OFFSET)
  72. #define DI_DISP3_TIME_CONF (0x015C - MX3FB_REG_OFFSET)
  73. #define DI_DISP0_DB0_MAP (0x0160 - MX3FB_REG_OFFSET)
  74. #define DI_DISP0_DB1_MAP (0x0164 - MX3FB_REG_OFFSET)
  75. #define DI_DISP0_DB2_MAP (0x0168 - MX3FB_REG_OFFSET)
  76. #define DI_DISP0_CB0_MAP (0x016C - MX3FB_REG_OFFSET)
  77. #define DI_DISP0_CB1_MAP (0x0170 - MX3FB_REG_OFFSET)
  78. #define DI_DISP0_CB2_MAP (0x0174 - MX3FB_REG_OFFSET)
  79. #define DI_DISP1_DB0_MAP (0x0178 - MX3FB_REG_OFFSET)
  80. #define DI_DISP1_DB1_MAP (0x017C - MX3FB_REG_OFFSET)
  81. #define DI_DISP1_DB2_MAP (0x0180 - MX3FB_REG_OFFSET)
  82. #define DI_DISP1_CB0_MAP (0x0184 - MX3FB_REG_OFFSET)
  83. #define DI_DISP1_CB1_MAP (0x0188 - MX3FB_REG_OFFSET)
  84. #define DI_DISP1_CB2_MAP (0x018C - MX3FB_REG_OFFSET)
  85. #define DI_DISP2_DB0_MAP (0x0190 - MX3FB_REG_OFFSET)
  86. #define DI_DISP2_DB1_MAP (0x0194 - MX3FB_REG_OFFSET)
  87. #define DI_DISP2_DB2_MAP (0x0198 - MX3FB_REG_OFFSET)
  88. #define DI_DISP2_CB0_MAP (0x019C - MX3FB_REG_OFFSET)
  89. #define DI_DISP2_CB1_MAP (0x01A0 - MX3FB_REG_OFFSET)
  90. #define DI_DISP2_CB2_MAP (0x01A4 - MX3FB_REG_OFFSET)
  91. #define DI_DISP3_B0_MAP (0x01A8 - MX3FB_REG_OFFSET)
  92. #define DI_DISP3_B1_MAP (0x01AC - MX3FB_REG_OFFSET)
  93. #define DI_DISP3_B2_MAP (0x01B0 - MX3FB_REG_OFFSET)
  94. #define DI_DISP_ACC_CC (0x01B4 - MX3FB_REG_OFFSET)
  95. #define DI_DISP_LLA_CONF (0x01B8 - MX3FB_REG_OFFSET)
  96. #define DI_DISP_LLA_DATA (0x01BC - MX3FB_REG_OFFSET)
  97. /* DI_DISP_SIG_POL bits */
  98. #define DI_D3_VSYNC_POL_SHIFT 28
  99. #define DI_D3_HSYNC_POL_SHIFT 27
  100. #define DI_D3_DRDY_SHARP_POL_SHIFT 26
  101. #define DI_D3_CLK_POL_SHIFT 25
  102. #define DI_D3_DATA_POL_SHIFT 24
  103. /* DI_DISP_IF_CONF bits */
  104. #define DI_D3_CLK_IDLE_SHIFT 26
  105. #define DI_D3_CLK_SEL_SHIFT 25
  106. #define DI_D3_DATAMSK_SHIFT 24
  107. enum ipu_panel {
  108. IPU_PANEL_SHARP_TFT,
  109. IPU_PANEL_TFT,
  110. };
  111. struct ipu_di_signal_cfg {
  112. unsigned datamask_en:1;
  113. unsigned clksel_en:1;
  114. unsigned clkidle_en:1;
  115. unsigned data_pol:1; /* true = inverted */
  116. unsigned clk_pol:1; /* true = rising edge */
  117. unsigned enable_pol:1;
  118. unsigned Hsync_pol:1; /* true = active high */
  119. unsigned Vsync_pol:1;
  120. };
  121. static const struct fb_videomode mx3fb_modedb[] = {
  122. {
  123. /* 240x320 @ 60 Hz */
  124. .name = "Sharp-QVGA",
  125. .refresh = 60,
  126. .xres = 240,
  127. .yres = 320,
  128. .pixclock = 185925,
  129. .left_margin = 9,
  130. .right_margin = 16,
  131. .upper_margin = 7,
  132. .lower_margin = 9,
  133. .hsync_len = 1,
  134. .vsync_len = 1,
  135. .sync = FB_SYNC_HOR_HIGH_ACT | FB_SYNC_SHARP_MODE |
  136. FB_SYNC_CLK_INVERT | FB_SYNC_DATA_INVERT |
  137. FB_SYNC_CLK_IDLE_EN,
  138. .vmode = FB_VMODE_NONINTERLACED,
  139. .flag = 0,
  140. }, {
  141. /* 240x33 @ 60 Hz */
  142. .name = "Sharp-CLI",
  143. .refresh = 60,
  144. .xres = 240,
  145. .yres = 33,
  146. .pixclock = 185925,
  147. .left_margin = 9,
  148. .right_margin = 16,
  149. .upper_margin = 7,
  150. .lower_margin = 9 + 287,
  151. .hsync_len = 1,
  152. .vsync_len = 1,
  153. .sync = FB_SYNC_HOR_HIGH_ACT | FB_SYNC_SHARP_MODE |
  154. FB_SYNC_CLK_INVERT | FB_SYNC_DATA_INVERT |
  155. FB_SYNC_CLK_IDLE_EN,
  156. .vmode = FB_VMODE_NONINTERLACED,
  157. .flag = 0,
  158. }, {
  159. /* 640x480 @ 60 Hz */
  160. .name = "NEC-VGA",
  161. .refresh = 60,
  162. .xres = 640,
  163. .yres = 480,
  164. .pixclock = 38255,
  165. .left_margin = 144,
  166. .right_margin = 0,
  167. .upper_margin = 34,
  168. .lower_margin = 40,
  169. .hsync_len = 1,
  170. .vsync_len = 1,
  171. .sync = FB_SYNC_VERT_HIGH_ACT | FB_SYNC_OE_ACT_HIGH,
  172. .vmode = FB_VMODE_NONINTERLACED,
  173. .flag = 0,
  174. }, {
  175. /* NTSC TV output */
  176. .name = "TV-NTSC",
  177. .refresh = 60,
  178. .xres = 640,
  179. .yres = 480,
  180. .pixclock = 37538,
  181. .left_margin = 38,
  182. .right_margin = 858 - 640 - 38 - 3,
  183. .upper_margin = 36,
  184. .lower_margin = 518 - 480 - 36 - 1,
  185. .hsync_len = 3,
  186. .vsync_len = 1,
  187. .sync = 0,
  188. .vmode = FB_VMODE_NONINTERLACED,
  189. .flag = 0,
  190. }, {
  191. /* PAL TV output */
  192. .name = "TV-PAL",
  193. .refresh = 50,
  194. .xres = 640,
  195. .yres = 480,
  196. .pixclock = 37538,
  197. .left_margin = 38,
  198. .right_margin = 960 - 640 - 38 - 32,
  199. .upper_margin = 32,
  200. .lower_margin = 555 - 480 - 32 - 3,
  201. .hsync_len = 32,
  202. .vsync_len = 3,
  203. .sync = 0,
  204. .vmode = FB_VMODE_NONINTERLACED,
  205. .flag = 0,
  206. }, {
  207. /* TV output VGA mode, 640x480 @ 65 Hz */
  208. .name = "TV-VGA",
  209. .refresh = 60,
  210. .xres = 640,
  211. .yres = 480,
  212. .pixclock = 40574,
  213. .left_margin = 35,
  214. .right_margin = 45,
  215. .upper_margin = 9,
  216. .lower_margin = 1,
  217. .hsync_len = 46,
  218. .vsync_len = 5,
  219. .sync = 0,
  220. .vmode = FB_VMODE_NONINTERLACED,
  221. .flag = 0,
  222. },
  223. };
  224. struct mx3fb_data {
  225. struct fb_info *fbi;
  226. int backlight_level;
  227. void __iomem *reg_base;
  228. spinlock_t lock;
  229. struct device *dev;
  230. uint32_t h_start_width;
  231. uint32_t v_start_width;
  232. };
  233. struct dma_chan_request {
  234. struct mx3fb_data *mx3fb;
  235. enum ipu_channel id;
  236. };
  237. /* MX3 specific framebuffer information. */
  238. struct mx3fb_info {
  239. int blank;
  240. enum ipu_channel ipu_ch;
  241. uint32_t cur_ipu_buf;
  242. u32 pseudo_palette[16];
  243. struct completion flip_cmpl;
  244. struct mutex mutex; /* Protects fb-ops */
  245. struct mx3fb_data *mx3fb;
  246. struct idmac_channel *idmac_channel;
  247. struct dma_async_tx_descriptor *txd;
  248. dma_cookie_t cookie;
  249. struct scatterlist sg[2];
  250. u32 sync; /* preserve var->sync flags */
  251. };
  252. static void mx3fb_dma_done(void *);
  253. /* Used fb-mode and bpp. Can be set on kernel command line, therefore file-static. */
  254. static const char *fb_mode;
  255. static unsigned long default_bpp = 16;
  256. static u32 mx3fb_read_reg(struct mx3fb_data *mx3fb, unsigned long reg)
  257. {
  258. return __raw_readl(mx3fb->reg_base + reg);
  259. }
  260. static void mx3fb_write_reg(struct mx3fb_data *mx3fb, u32 value, unsigned long reg)
  261. {
  262. __raw_writel(value, mx3fb->reg_base + reg);
  263. }
  264. static const uint32_t di_mappings[] = {
  265. 0x1600AAAA, 0x00E05555, 0x00070000, 3, /* RGB888 */
  266. 0x0005000F, 0x000B000F, 0x0011000F, 1, /* RGB666 */
  267. 0x0011000F, 0x000B000F, 0x0005000F, 1, /* BGR666 */
  268. 0x0004003F, 0x000A000F, 0x000F003F, 1 /* RGB565 */
  269. };
  270. static void sdc_fb_init(struct mx3fb_info *fbi)
  271. {
  272. struct mx3fb_data *mx3fb = fbi->mx3fb;
  273. uint32_t reg;
  274. reg = mx3fb_read_reg(mx3fb, SDC_COM_CONF);
  275. mx3fb_write_reg(mx3fb, reg | SDC_COM_BG_EN, SDC_COM_CONF);
  276. }
  277. /* Returns enabled flag before uninit */
  278. static uint32_t sdc_fb_uninit(struct mx3fb_info *fbi)
  279. {
  280. struct mx3fb_data *mx3fb = fbi->mx3fb;
  281. uint32_t reg;
  282. reg = mx3fb_read_reg(mx3fb, SDC_COM_CONF);
  283. mx3fb_write_reg(mx3fb, reg & ~SDC_COM_BG_EN, SDC_COM_CONF);
  284. return reg & SDC_COM_BG_EN;
  285. }
  286. static void sdc_enable_channel(struct mx3fb_info *mx3_fbi)
  287. {
  288. struct mx3fb_data *mx3fb = mx3_fbi->mx3fb;
  289. struct idmac_channel *ichan = mx3_fbi->idmac_channel;
  290. struct dma_chan *dma_chan = &ichan->dma_chan;
  291. unsigned long flags;
  292. dma_cookie_t cookie;
  293. if (mx3_fbi->txd)
  294. dev_dbg(mx3fb->dev, "mx3fbi %p, desc %p, sg %p\n", mx3_fbi,
  295. to_tx_desc(mx3_fbi->txd), to_tx_desc(mx3_fbi->txd)->sg);
  296. else
  297. dev_dbg(mx3fb->dev, "mx3fbi %p, txd = NULL\n", mx3_fbi);
  298. /* This enables the channel */
  299. if (mx3_fbi->cookie < 0) {
  300. mx3_fbi->txd = dma_chan->device->device_prep_slave_sg(dma_chan,
  301. &mx3_fbi->sg[0], 1, DMA_TO_DEVICE, DMA_PREP_INTERRUPT);
  302. if (!mx3_fbi->txd) {
  303. dev_err(mx3fb->dev, "Cannot allocate descriptor on %d\n",
  304. dma_chan->chan_id);
  305. return;
  306. }
  307. mx3_fbi->txd->callback_param = mx3_fbi->txd;
  308. mx3_fbi->txd->callback = mx3fb_dma_done;
  309. cookie = mx3_fbi->txd->tx_submit(mx3_fbi->txd);
  310. dev_dbg(mx3fb->dev, "%d: Submit %p #%d [%c]\n", __LINE__,
  311. mx3_fbi->txd, cookie, list_empty(&ichan->queue) ? '-' : '+');
  312. } else {
  313. if (!mx3_fbi->txd || !mx3_fbi->txd->tx_submit) {
  314. dev_err(mx3fb->dev, "Cannot enable channel %d\n",
  315. dma_chan->chan_id);
  316. return;
  317. }
  318. /* Just re-activate the same buffer */
  319. dma_async_issue_pending(dma_chan);
  320. cookie = mx3_fbi->cookie;
  321. dev_dbg(mx3fb->dev, "%d: Re-submit %p #%d [%c]\n", __LINE__,
  322. mx3_fbi->txd, cookie, list_empty(&ichan->queue) ? '-' : '+');
  323. }
  324. if (cookie >= 0) {
  325. spin_lock_irqsave(&mx3fb->lock, flags);
  326. sdc_fb_init(mx3_fbi);
  327. mx3_fbi->cookie = cookie;
  328. spin_unlock_irqrestore(&mx3fb->lock, flags);
  329. }
  330. /*
  331. * Attention! Without this msleep the channel keeps generating
  332. * interrupts. Next sdc_set_brightness() is going to be called
  333. * from mx3fb_blank().
  334. */
  335. msleep(2);
  336. }
  337. static void sdc_disable_channel(struct mx3fb_info *mx3_fbi)
  338. {
  339. struct mx3fb_data *mx3fb = mx3_fbi->mx3fb;
  340. uint32_t enabled;
  341. unsigned long flags;
  342. spin_lock_irqsave(&mx3fb->lock, flags);
  343. enabled = sdc_fb_uninit(mx3_fbi);
  344. spin_unlock_irqrestore(&mx3fb->lock, flags);
  345. mx3_fbi->txd->chan->device->device_control(mx3_fbi->txd->chan,
  346. DMA_TERMINATE_ALL, 0);
  347. mx3_fbi->txd = NULL;
  348. mx3_fbi->cookie = -EINVAL;
  349. }
  350. /**
  351. * sdc_set_window_pos() - set window position of the respective plane.
  352. * @mx3fb: mx3fb context.
  353. * @channel: IPU DMAC channel ID.
  354. * @x_pos: X coordinate relative to the top left corner to place window at.
  355. * @y_pos: Y coordinate relative to the top left corner to place window at.
  356. * @return: 0 on success or negative error code on failure.
  357. */
  358. static int sdc_set_window_pos(struct mx3fb_data *mx3fb, enum ipu_channel channel,
  359. int16_t x_pos, int16_t y_pos)
  360. {
  361. if (channel != IDMAC_SDC_0)
  362. return -EINVAL;
  363. x_pos += mx3fb->h_start_width;
  364. y_pos += mx3fb->v_start_width;
  365. mx3fb_write_reg(mx3fb, (x_pos << 16) | y_pos, SDC_BG_POS);
  366. return 0;
  367. }
  368. /**
  369. * sdc_init_panel() - initialize a synchronous LCD panel.
  370. * @mx3fb: mx3fb context.
  371. * @panel: panel type.
  372. * @pixel_clk: desired pixel clock frequency in Hz.
  373. * @width: width of panel in pixels.
  374. * @height: height of panel in pixels.
  375. * @pixel_fmt: pixel format of buffer as FOURCC ASCII code.
  376. * @h_start_width: number of pixel clocks between the HSYNC signal pulse
  377. * and the start of valid data.
  378. * @h_sync_width: width of the HSYNC signal in units of pixel clocks.
  379. * @h_end_width: number of pixel clocks between the end of valid data
  380. * and the HSYNC signal for next line.
  381. * @v_start_width: number of lines between the VSYNC signal pulse and the
  382. * start of valid data.
  383. * @v_sync_width: width of the VSYNC signal in units of lines
  384. * @v_end_width: number of lines between the end of valid data and the
  385. * VSYNC signal for next frame.
  386. * @sig: bitfield of signal polarities for LCD interface.
  387. * @return: 0 on success or negative error code on failure.
  388. */
  389. static int sdc_init_panel(struct mx3fb_data *mx3fb, enum ipu_panel panel,
  390. uint32_t pixel_clk,
  391. uint16_t width, uint16_t height,
  392. enum pixel_fmt pixel_fmt,
  393. uint16_t h_start_width, uint16_t h_sync_width,
  394. uint16_t h_end_width, uint16_t v_start_width,
  395. uint16_t v_sync_width, uint16_t v_end_width,
  396. struct ipu_di_signal_cfg sig)
  397. {
  398. unsigned long lock_flags;
  399. uint32_t reg;
  400. uint32_t old_conf;
  401. uint32_t div;
  402. struct clk *ipu_clk;
  403. dev_dbg(mx3fb->dev, "panel size = %d x %d", width, height);
  404. if (v_sync_width == 0 || h_sync_width == 0)
  405. return -EINVAL;
  406. /* Init panel size and blanking periods */
  407. reg = ((uint32_t) (h_sync_width - 1) << 26) |
  408. ((uint32_t) (width + h_start_width + h_end_width - 1) << 16);
  409. mx3fb_write_reg(mx3fb, reg, SDC_HOR_CONF);
  410. #ifdef DEBUG
  411. printk(KERN_CONT " hor_conf %x,", reg);
  412. #endif
  413. reg = ((uint32_t) (v_sync_width - 1) << 26) | SDC_V_SYNC_WIDTH_L |
  414. ((uint32_t) (height + v_start_width + v_end_width - 1) << 16);
  415. mx3fb_write_reg(mx3fb, reg, SDC_VER_CONF);
  416. #ifdef DEBUG
  417. printk(KERN_CONT " ver_conf %x\n", reg);
  418. #endif
  419. mx3fb->h_start_width = h_start_width;
  420. mx3fb->v_start_width = v_start_width;
  421. switch (panel) {
  422. case IPU_PANEL_SHARP_TFT:
  423. mx3fb_write_reg(mx3fb, 0x00FD0102L, SDC_SHARP_CONF_1);
  424. mx3fb_write_reg(mx3fb, 0x00F500F4L, SDC_SHARP_CONF_2);
  425. mx3fb_write_reg(mx3fb, SDC_COM_SHARP | SDC_COM_TFT_COLOR, SDC_COM_CONF);
  426. break;
  427. case IPU_PANEL_TFT:
  428. mx3fb_write_reg(mx3fb, SDC_COM_TFT_COLOR, SDC_COM_CONF);
  429. break;
  430. default:
  431. return -EINVAL;
  432. }
  433. /* Init clocking */
  434. /*
  435. * Calculate divider: fractional part is 4 bits so simply multiple by
  436. * 2^4 to get fractional part, as long as we stay under ~250MHz and on
  437. * i.MX31 it (HSP_CLK) is <= 178MHz. Currently 128.267MHz
  438. */
  439. ipu_clk = clk_get(mx3fb->dev, NULL);
  440. if (!IS_ERR(ipu_clk)) {
  441. div = clk_get_rate(ipu_clk) * 16 / pixel_clk;
  442. clk_put(ipu_clk);
  443. } else {
  444. div = 0;
  445. }
  446. if (div < 0x40) { /* Divider less than 4 */
  447. dev_dbg(mx3fb->dev,
  448. "InitPanel() - Pixel clock divider less than 4\n");
  449. div = 0x40;
  450. }
  451. dev_dbg(mx3fb->dev, "pixel clk = %u, divider %u.%u\n",
  452. pixel_clk, div >> 4, (div & 7) * 125);
  453. spin_lock_irqsave(&mx3fb->lock, lock_flags);
  454. /*
  455. * DISP3_IF_CLK_DOWN_WR is half the divider value and 2 fraction bits
  456. * fewer. Subtract 1 extra from DISP3_IF_CLK_DOWN_WR based on timing
  457. * debug. DISP3_IF_CLK_UP_WR is 0
  458. */
  459. mx3fb_write_reg(mx3fb, (((div / 8) - 1) << 22) | div, DI_DISP3_TIME_CONF);
  460. /* DI settings */
  461. old_conf = mx3fb_read_reg(mx3fb, DI_DISP_IF_CONF) & 0x78FFFFFF;
  462. old_conf |= sig.datamask_en << DI_D3_DATAMSK_SHIFT |
  463. sig.clksel_en << DI_D3_CLK_SEL_SHIFT |
  464. sig.clkidle_en << DI_D3_CLK_IDLE_SHIFT;
  465. mx3fb_write_reg(mx3fb, old_conf, DI_DISP_IF_CONF);
  466. old_conf = mx3fb_read_reg(mx3fb, DI_DISP_SIG_POL) & 0xE0FFFFFF;
  467. old_conf |= sig.data_pol << DI_D3_DATA_POL_SHIFT |
  468. sig.clk_pol << DI_D3_CLK_POL_SHIFT |
  469. sig.enable_pol << DI_D3_DRDY_SHARP_POL_SHIFT |
  470. sig.Hsync_pol << DI_D3_HSYNC_POL_SHIFT |
  471. sig.Vsync_pol << DI_D3_VSYNC_POL_SHIFT;
  472. mx3fb_write_reg(mx3fb, old_conf, DI_DISP_SIG_POL);
  473. switch (pixel_fmt) {
  474. case IPU_PIX_FMT_RGB24:
  475. mx3fb_write_reg(mx3fb, di_mappings[0], DI_DISP3_B0_MAP);
  476. mx3fb_write_reg(mx3fb, di_mappings[1], DI_DISP3_B1_MAP);
  477. mx3fb_write_reg(mx3fb, di_mappings[2], DI_DISP3_B2_MAP);
  478. mx3fb_write_reg(mx3fb, mx3fb_read_reg(mx3fb, DI_DISP_ACC_CC) |
  479. ((di_mappings[3] - 1) << 12), DI_DISP_ACC_CC);
  480. break;
  481. case IPU_PIX_FMT_RGB666:
  482. mx3fb_write_reg(mx3fb, di_mappings[4], DI_DISP3_B0_MAP);
  483. mx3fb_write_reg(mx3fb, di_mappings[5], DI_DISP3_B1_MAP);
  484. mx3fb_write_reg(mx3fb, di_mappings[6], DI_DISP3_B2_MAP);
  485. mx3fb_write_reg(mx3fb, mx3fb_read_reg(mx3fb, DI_DISP_ACC_CC) |
  486. ((di_mappings[7] - 1) << 12), DI_DISP_ACC_CC);
  487. break;
  488. case IPU_PIX_FMT_BGR666:
  489. mx3fb_write_reg(mx3fb, di_mappings[8], DI_DISP3_B0_MAP);
  490. mx3fb_write_reg(mx3fb, di_mappings[9], DI_DISP3_B1_MAP);
  491. mx3fb_write_reg(mx3fb, di_mappings[10], DI_DISP3_B2_MAP);
  492. mx3fb_write_reg(mx3fb, mx3fb_read_reg(mx3fb, DI_DISP_ACC_CC) |
  493. ((di_mappings[11] - 1) << 12), DI_DISP_ACC_CC);
  494. break;
  495. default:
  496. mx3fb_write_reg(mx3fb, di_mappings[12], DI_DISP3_B0_MAP);
  497. mx3fb_write_reg(mx3fb, di_mappings[13], DI_DISP3_B1_MAP);
  498. mx3fb_write_reg(mx3fb, di_mappings[14], DI_DISP3_B2_MAP);
  499. mx3fb_write_reg(mx3fb, mx3fb_read_reg(mx3fb, DI_DISP_ACC_CC) |
  500. ((di_mappings[15] - 1) << 12), DI_DISP_ACC_CC);
  501. break;
  502. }
  503. spin_unlock_irqrestore(&mx3fb->lock, lock_flags);
  504. dev_dbg(mx3fb->dev, "DI_DISP_IF_CONF = 0x%08X\n",
  505. mx3fb_read_reg(mx3fb, DI_DISP_IF_CONF));
  506. dev_dbg(mx3fb->dev, "DI_DISP_SIG_POL = 0x%08X\n",
  507. mx3fb_read_reg(mx3fb, DI_DISP_SIG_POL));
  508. dev_dbg(mx3fb->dev, "DI_DISP3_TIME_CONF = 0x%08X\n",
  509. mx3fb_read_reg(mx3fb, DI_DISP3_TIME_CONF));
  510. return 0;
  511. }
  512. /**
  513. * sdc_set_color_key() - set the transparent color key for SDC graphic plane.
  514. * @mx3fb: mx3fb context.
  515. * @channel: IPU DMAC channel ID.
  516. * @enable: boolean to enable or disable color keyl.
  517. * @color_key: 24-bit RGB color to use as transparent color key.
  518. * @return: 0 on success or negative error code on failure.
  519. */
  520. static int sdc_set_color_key(struct mx3fb_data *mx3fb, enum ipu_channel channel,
  521. bool enable, uint32_t color_key)
  522. {
  523. uint32_t reg, sdc_conf;
  524. unsigned long lock_flags;
  525. spin_lock_irqsave(&mx3fb->lock, lock_flags);
  526. sdc_conf = mx3fb_read_reg(mx3fb, SDC_COM_CONF);
  527. if (channel == IDMAC_SDC_0)
  528. sdc_conf &= ~SDC_COM_GWSEL;
  529. else
  530. sdc_conf |= SDC_COM_GWSEL;
  531. if (enable) {
  532. reg = mx3fb_read_reg(mx3fb, SDC_GW_CTRL) & 0xFF000000L;
  533. mx3fb_write_reg(mx3fb, reg | (color_key & 0x00FFFFFFL),
  534. SDC_GW_CTRL);
  535. sdc_conf |= SDC_COM_KEY_COLOR_G;
  536. } else {
  537. sdc_conf &= ~SDC_COM_KEY_COLOR_G;
  538. }
  539. mx3fb_write_reg(mx3fb, sdc_conf, SDC_COM_CONF);
  540. spin_unlock_irqrestore(&mx3fb->lock, lock_flags);
  541. return 0;
  542. }
  543. /**
  544. * sdc_set_global_alpha() - set global alpha blending modes.
  545. * @mx3fb: mx3fb context.
  546. * @enable: boolean to enable or disable global alpha blending. If disabled,
  547. * per pixel blending is used.
  548. * @alpha: global alpha value.
  549. * @return: 0 on success or negative error code on failure.
  550. */
  551. static int sdc_set_global_alpha(struct mx3fb_data *mx3fb, bool enable, uint8_t alpha)
  552. {
  553. uint32_t reg;
  554. unsigned long lock_flags;
  555. spin_lock_irqsave(&mx3fb->lock, lock_flags);
  556. if (enable) {
  557. reg = mx3fb_read_reg(mx3fb, SDC_GW_CTRL) & 0x00FFFFFFL;
  558. mx3fb_write_reg(mx3fb, reg | ((uint32_t) alpha << 24), SDC_GW_CTRL);
  559. reg = mx3fb_read_reg(mx3fb, SDC_COM_CONF);
  560. mx3fb_write_reg(mx3fb, reg | SDC_COM_GLB_A, SDC_COM_CONF);
  561. } else {
  562. reg = mx3fb_read_reg(mx3fb, SDC_COM_CONF);
  563. mx3fb_write_reg(mx3fb, reg & ~SDC_COM_GLB_A, SDC_COM_CONF);
  564. }
  565. spin_unlock_irqrestore(&mx3fb->lock, lock_flags);
  566. return 0;
  567. }
  568. static void sdc_set_brightness(struct mx3fb_data *mx3fb, uint8_t value)
  569. {
  570. dev_dbg(mx3fb->dev, "%s: value = %d\n", __func__, value);
  571. /* This might be board-specific */
  572. mx3fb_write_reg(mx3fb, 0x03000000UL | value << 16, SDC_PWM_CTRL);
  573. return;
  574. }
  575. static uint32_t bpp_to_pixfmt(int bpp)
  576. {
  577. uint32_t pixfmt = 0;
  578. switch (bpp) {
  579. case 24:
  580. pixfmt = IPU_PIX_FMT_BGR24;
  581. break;
  582. case 32:
  583. pixfmt = IPU_PIX_FMT_BGR32;
  584. break;
  585. case 16:
  586. pixfmt = IPU_PIX_FMT_RGB565;
  587. break;
  588. }
  589. return pixfmt;
  590. }
  591. static int mx3fb_blank(int blank, struct fb_info *fbi);
  592. static int mx3fb_map_video_memory(struct fb_info *fbi, unsigned int mem_len,
  593. bool lock);
  594. static int mx3fb_unmap_video_memory(struct fb_info *fbi);
  595. /**
  596. * mx3fb_set_fix() - set fixed framebuffer parameters from variable settings.
  597. * @info: framebuffer information pointer
  598. * @return: 0 on success or negative error code on failure.
  599. */
  600. static int mx3fb_set_fix(struct fb_info *fbi)
  601. {
  602. struct fb_fix_screeninfo *fix = &fbi->fix;
  603. struct fb_var_screeninfo *var = &fbi->var;
  604. strncpy(fix->id, "DISP3 BG", 8);
  605. fix->line_length = var->xres_virtual * var->bits_per_pixel / 8;
  606. fix->type = FB_TYPE_PACKED_PIXELS;
  607. fix->accel = FB_ACCEL_NONE;
  608. fix->visual = FB_VISUAL_TRUECOLOR;
  609. fix->xpanstep = 1;
  610. fix->ypanstep = 1;
  611. return 0;
  612. }
  613. static void mx3fb_dma_done(void *arg)
  614. {
  615. struct idmac_tx_desc *tx_desc = to_tx_desc(arg);
  616. struct dma_chan *chan = tx_desc->txd.chan;
  617. struct idmac_channel *ichannel = to_idmac_chan(chan);
  618. struct mx3fb_data *mx3fb = ichannel->client;
  619. struct mx3fb_info *mx3_fbi = mx3fb->fbi->par;
  620. dev_dbg(mx3fb->dev, "irq %d callback\n", ichannel->eof_irq);
  621. /* We only need one interrupt, it will be re-enabled as needed */
  622. disable_irq_nosync(ichannel->eof_irq);
  623. complete(&mx3_fbi->flip_cmpl);
  624. }
  625. static int __set_par(struct fb_info *fbi, bool lock)
  626. {
  627. u32 mem_len;
  628. struct ipu_di_signal_cfg sig_cfg;
  629. enum ipu_panel mode = IPU_PANEL_TFT;
  630. struct mx3fb_info *mx3_fbi = fbi->par;
  631. struct mx3fb_data *mx3fb = mx3_fbi->mx3fb;
  632. struct idmac_channel *ichan = mx3_fbi->idmac_channel;
  633. struct idmac_video_param *video = &ichan->params.video;
  634. struct scatterlist *sg = mx3_fbi->sg;
  635. /* Total cleanup */
  636. if (mx3_fbi->txd)
  637. sdc_disable_channel(mx3_fbi);
  638. mx3fb_set_fix(fbi);
  639. mem_len = fbi->var.yres_virtual * fbi->fix.line_length;
  640. if (mem_len > fbi->fix.smem_len) {
  641. if (fbi->fix.smem_start)
  642. mx3fb_unmap_video_memory(fbi);
  643. if (mx3fb_map_video_memory(fbi, mem_len, lock) < 0)
  644. return -ENOMEM;
  645. }
  646. sg_init_table(&sg[0], 1);
  647. sg_init_table(&sg[1], 1);
  648. sg_dma_address(&sg[0]) = fbi->fix.smem_start;
  649. sg_set_page(&sg[0], virt_to_page(fbi->screen_base),
  650. fbi->fix.smem_len,
  651. offset_in_page(fbi->screen_base));
  652. if (mx3_fbi->ipu_ch == IDMAC_SDC_0) {
  653. memset(&sig_cfg, 0, sizeof(sig_cfg));
  654. if (fbi->var.sync & FB_SYNC_HOR_HIGH_ACT)
  655. sig_cfg.Hsync_pol = true;
  656. if (fbi->var.sync & FB_SYNC_VERT_HIGH_ACT)
  657. sig_cfg.Vsync_pol = true;
  658. if (fbi->var.sync & FB_SYNC_CLK_INVERT)
  659. sig_cfg.clk_pol = true;
  660. if (fbi->var.sync & FB_SYNC_DATA_INVERT)
  661. sig_cfg.data_pol = true;
  662. if (fbi->var.sync & FB_SYNC_OE_ACT_HIGH)
  663. sig_cfg.enable_pol = true;
  664. if (fbi->var.sync & FB_SYNC_CLK_IDLE_EN)
  665. sig_cfg.clkidle_en = true;
  666. if (fbi->var.sync & FB_SYNC_CLK_SEL_EN)
  667. sig_cfg.clksel_en = true;
  668. if (fbi->var.sync & FB_SYNC_SHARP_MODE)
  669. mode = IPU_PANEL_SHARP_TFT;
  670. dev_dbg(fbi->device, "pixclock = %ul Hz\n",
  671. (u32) (PICOS2KHZ(fbi->var.pixclock) * 1000UL));
  672. if (sdc_init_panel(mx3fb, mode,
  673. (PICOS2KHZ(fbi->var.pixclock)) * 1000UL,
  674. fbi->var.xres, fbi->var.yres,
  675. (fbi->var.sync & FB_SYNC_SWAP_RGB) ?
  676. IPU_PIX_FMT_BGR666 : IPU_PIX_FMT_RGB666,
  677. fbi->var.left_margin,
  678. fbi->var.hsync_len,
  679. fbi->var.right_margin +
  680. fbi->var.hsync_len,
  681. fbi->var.upper_margin,
  682. fbi->var.vsync_len,
  683. fbi->var.lower_margin +
  684. fbi->var.vsync_len, sig_cfg) != 0) {
  685. dev_err(fbi->device,
  686. "mx3fb: Error initializing panel.\n");
  687. return -EINVAL;
  688. }
  689. }
  690. sdc_set_window_pos(mx3fb, mx3_fbi->ipu_ch, 0, 0);
  691. mx3_fbi->cur_ipu_buf = 0;
  692. video->out_pixel_fmt = bpp_to_pixfmt(fbi->var.bits_per_pixel);
  693. video->out_width = fbi->var.xres;
  694. video->out_height = fbi->var.yres;
  695. video->out_stride = fbi->var.xres_virtual;
  696. if (mx3_fbi->blank == FB_BLANK_UNBLANK)
  697. sdc_enable_channel(mx3_fbi);
  698. return 0;
  699. }
  700. /**
  701. * mx3fb_set_par() - set framebuffer parameters and change the operating mode.
  702. * @fbi: framebuffer information pointer.
  703. * @return: 0 on success or negative error code on failure.
  704. */
  705. static int mx3fb_set_par(struct fb_info *fbi)
  706. {
  707. struct mx3fb_info *mx3_fbi = fbi->par;
  708. struct mx3fb_data *mx3fb = mx3_fbi->mx3fb;
  709. struct idmac_channel *ichan = mx3_fbi->idmac_channel;
  710. int ret;
  711. dev_dbg(mx3fb->dev, "%s [%c]\n", __func__, list_empty(&ichan->queue) ? '-' : '+');
  712. mutex_lock(&mx3_fbi->mutex);
  713. ret = __set_par(fbi, true);
  714. mutex_unlock(&mx3_fbi->mutex);
  715. return ret;
  716. }
  717. /**
  718. * mx3fb_check_var() - check and adjust framebuffer variable parameters.
  719. * @var: framebuffer variable parameters
  720. * @fbi: framebuffer information pointer
  721. */
  722. static int mx3fb_check_var(struct fb_var_screeninfo *var, struct fb_info *fbi)
  723. {
  724. struct mx3fb_info *mx3_fbi = fbi->par;
  725. u32 vtotal;
  726. u32 htotal;
  727. dev_dbg(fbi->device, "%s\n", __func__);
  728. if (var->xres_virtual < var->xres)
  729. var->xres_virtual = var->xres;
  730. if (var->yres_virtual < var->yres)
  731. var->yres_virtual = var->yres;
  732. if ((var->bits_per_pixel != 32) && (var->bits_per_pixel != 24) &&
  733. (var->bits_per_pixel != 16))
  734. var->bits_per_pixel = default_bpp;
  735. switch (var->bits_per_pixel) {
  736. case 16:
  737. var->red.length = 5;
  738. var->red.offset = 11;
  739. var->red.msb_right = 0;
  740. var->green.length = 6;
  741. var->green.offset = 5;
  742. var->green.msb_right = 0;
  743. var->blue.length = 5;
  744. var->blue.offset = 0;
  745. var->blue.msb_right = 0;
  746. var->transp.length = 0;
  747. var->transp.offset = 0;
  748. var->transp.msb_right = 0;
  749. break;
  750. case 24:
  751. var->red.length = 8;
  752. var->red.offset = 16;
  753. var->red.msb_right = 0;
  754. var->green.length = 8;
  755. var->green.offset = 8;
  756. var->green.msb_right = 0;
  757. var->blue.length = 8;
  758. var->blue.offset = 0;
  759. var->blue.msb_right = 0;
  760. var->transp.length = 0;
  761. var->transp.offset = 0;
  762. var->transp.msb_right = 0;
  763. break;
  764. case 32:
  765. var->red.length = 8;
  766. var->red.offset = 16;
  767. var->red.msb_right = 0;
  768. var->green.length = 8;
  769. var->green.offset = 8;
  770. var->green.msb_right = 0;
  771. var->blue.length = 8;
  772. var->blue.offset = 0;
  773. var->blue.msb_right = 0;
  774. var->transp.length = 8;
  775. var->transp.offset = 24;
  776. var->transp.msb_right = 0;
  777. break;
  778. }
  779. if (var->pixclock < 1000) {
  780. htotal = var->xres + var->right_margin + var->hsync_len +
  781. var->left_margin;
  782. vtotal = var->yres + var->lower_margin + var->vsync_len +
  783. var->upper_margin;
  784. var->pixclock = (vtotal * htotal * 6UL) / 100UL;
  785. var->pixclock = KHZ2PICOS(var->pixclock);
  786. dev_dbg(fbi->device, "pixclock set for 60Hz refresh = %u ps\n",
  787. var->pixclock);
  788. }
  789. var->height = -1;
  790. var->width = -1;
  791. var->grayscale = 0;
  792. /* Preserve sync flags */
  793. var->sync |= mx3_fbi->sync;
  794. mx3_fbi->sync |= var->sync;
  795. return 0;
  796. }
  797. static u32 chan_to_field(unsigned int chan, struct fb_bitfield *bf)
  798. {
  799. chan &= 0xffff;
  800. chan >>= 16 - bf->length;
  801. return chan << bf->offset;
  802. }
  803. static int mx3fb_setcolreg(unsigned int regno, unsigned int red,
  804. unsigned int green, unsigned int blue,
  805. unsigned int trans, struct fb_info *fbi)
  806. {
  807. struct mx3fb_info *mx3_fbi = fbi->par;
  808. u32 val;
  809. int ret = 1;
  810. dev_dbg(fbi->device, "%s, regno = %u\n", __func__, regno);
  811. mutex_lock(&mx3_fbi->mutex);
  812. /*
  813. * If greyscale is true, then we convert the RGB value
  814. * to greyscale no matter what visual we are using.
  815. */
  816. if (fbi->var.grayscale)
  817. red = green = blue = (19595 * red + 38470 * green +
  818. 7471 * blue) >> 16;
  819. switch (fbi->fix.visual) {
  820. case FB_VISUAL_TRUECOLOR:
  821. /*
  822. * 16-bit True Colour. We encode the RGB value
  823. * according to the RGB bitfield information.
  824. */
  825. if (regno < 16) {
  826. u32 *pal = fbi->pseudo_palette;
  827. val = chan_to_field(red, &fbi->var.red);
  828. val |= chan_to_field(green, &fbi->var.green);
  829. val |= chan_to_field(blue, &fbi->var.blue);
  830. pal[regno] = val;
  831. ret = 0;
  832. }
  833. break;
  834. case FB_VISUAL_STATIC_PSEUDOCOLOR:
  835. case FB_VISUAL_PSEUDOCOLOR:
  836. break;
  837. }
  838. mutex_unlock(&mx3_fbi->mutex);
  839. return ret;
  840. }
  841. static void __blank(int blank, struct fb_info *fbi)
  842. {
  843. struct mx3fb_info *mx3_fbi = fbi->par;
  844. struct mx3fb_data *mx3fb = mx3_fbi->mx3fb;
  845. mx3_fbi->blank = blank;
  846. switch (blank) {
  847. case FB_BLANK_POWERDOWN:
  848. case FB_BLANK_VSYNC_SUSPEND:
  849. case FB_BLANK_HSYNC_SUSPEND:
  850. case FB_BLANK_NORMAL:
  851. sdc_set_brightness(mx3fb, 0);
  852. memset((char *)fbi->screen_base, 0, fbi->fix.smem_len);
  853. /* Give LCD time to update - enough for 50 and 60 Hz */
  854. msleep(25);
  855. sdc_disable_channel(mx3_fbi);
  856. break;
  857. case FB_BLANK_UNBLANK:
  858. sdc_enable_channel(mx3_fbi);
  859. sdc_set_brightness(mx3fb, mx3fb->backlight_level);
  860. break;
  861. }
  862. }
  863. /**
  864. * mx3fb_blank() - blank the display.
  865. */
  866. static int mx3fb_blank(int blank, struct fb_info *fbi)
  867. {
  868. struct mx3fb_info *mx3_fbi = fbi->par;
  869. dev_dbg(fbi->device, "%s, blank = %d, base %p, len %u\n", __func__,
  870. blank, fbi->screen_base, fbi->fix.smem_len);
  871. if (mx3_fbi->blank == blank)
  872. return 0;
  873. mutex_lock(&mx3_fbi->mutex);
  874. __blank(blank, fbi);
  875. mutex_unlock(&mx3_fbi->mutex);
  876. return 0;
  877. }
  878. /**
  879. * mx3fb_pan_display() - pan or wrap the display
  880. * @var: variable screen buffer information.
  881. * @info: framebuffer information pointer.
  882. *
  883. * We look only at xoffset, yoffset and the FB_VMODE_YWRAP flag
  884. */
  885. static int mx3fb_pan_display(struct fb_var_screeninfo *var,
  886. struct fb_info *fbi)
  887. {
  888. struct mx3fb_info *mx3_fbi = fbi->par;
  889. u32 y_bottom;
  890. unsigned long base;
  891. off_t offset;
  892. dma_cookie_t cookie;
  893. struct scatterlist *sg = mx3_fbi->sg;
  894. struct dma_chan *dma_chan = &mx3_fbi->idmac_channel->dma_chan;
  895. struct dma_async_tx_descriptor *txd;
  896. int ret;
  897. dev_dbg(fbi->device, "%s [%c]\n", __func__,
  898. list_empty(&mx3_fbi->idmac_channel->queue) ? '-' : '+');
  899. if (var->xoffset > 0) {
  900. dev_dbg(fbi->device, "x panning not supported\n");
  901. return -EINVAL;
  902. }
  903. if (fbi->var.xoffset == var->xoffset &&
  904. fbi->var.yoffset == var->yoffset)
  905. return 0; /* No change, do nothing */
  906. y_bottom = var->yoffset;
  907. if (!(var->vmode & FB_VMODE_YWRAP))
  908. y_bottom += var->yres;
  909. if (y_bottom > fbi->var.yres_virtual)
  910. return -EINVAL;
  911. mutex_lock(&mx3_fbi->mutex);
  912. offset = (var->yoffset * var->xres_virtual + var->xoffset) *
  913. (var->bits_per_pixel / 8);
  914. base = fbi->fix.smem_start + offset;
  915. dev_dbg(fbi->device, "Updating SDC BG buf %d address=0x%08lX\n",
  916. mx3_fbi->cur_ipu_buf, base);
  917. /*
  918. * We enable the End of Frame interrupt, which will free a tx-descriptor,
  919. * which we will need for the next device_prep_slave_sg(). The
  920. * IRQ-handler will disable the IRQ again.
  921. */
  922. init_completion(&mx3_fbi->flip_cmpl);
  923. enable_irq(mx3_fbi->idmac_channel->eof_irq);
  924. ret = wait_for_completion_timeout(&mx3_fbi->flip_cmpl, HZ / 10);
  925. if (ret <= 0) {
  926. mutex_unlock(&mx3_fbi->mutex);
  927. dev_info(fbi->device, "Panning failed due to %s\n", ret < 0 ?
  928. "user interrupt" : "timeout");
  929. disable_irq(mx3_fbi->idmac_channel->eof_irq);
  930. return ret ? : -ETIMEDOUT;
  931. }
  932. mx3_fbi->cur_ipu_buf = !mx3_fbi->cur_ipu_buf;
  933. sg_dma_address(&sg[mx3_fbi->cur_ipu_buf]) = base;
  934. sg_set_page(&sg[mx3_fbi->cur_ipu_buf],
  935. virt_to_page(fbi->screen_base + offset), fbi->fix.smem_len,
  936. offset_in_page(fbi->screen_base + offset));
  937. if (mx3_fbi->txd)
  938. async_tx_ack(mx3_fbi->txd);
  939. txd = dma_chan->device->device_prep_slave_sg(dma_chan, sg +
  940. mx3_fbi->cur_ipu_buf, 1, DMA_TO_DEVICE, DMA_PREP_INTERRUPT);
  941. if (!txd) {
  942. dev_err(fbi->device,
  943. "Error preparing a DMA transaction descriptor.\n");
  944. mutex_unlock(&mx3_fbi->mutex);
  945. return -EIO;
  946. }
  947. txd->callback_param = txd;
  948. txd->callback = mx3fb_dma_done;
  949. /*
  950. * Emulate original mx3fb behaviour: each new call to idmac_tx_submit()
  951. * should switch to another buffer
  952. */
  953. cookie = txd->tx_submit(txd);
  954. dev_dbg(fbi->device, "%d: Submit %p #%d\n", __LINE__, txd, cookie);
  955. if (cookie < 0) {
  956. dev_err(fbi->device,
  957. "Error updating SDC buf %d to address=0x%08lX\n",
  958. mx3_fbi->cur_ipu_buf, base);
  959. mutex_unlock(&mx3_fbi->mutex);
  960. return -EIO;
  961. }
  962. mx3_fbi->txd = txd;
  963. fbi->var.xoffset = var->xoffset;
  964. fbi->var.yoffset = var->yoffset;
  965. if (var->vmode & FB_VMODE_YWRAP)
  966. fbi->var.vmode |= FB_VMODE_YWRAP;
  967. else
  968. fbi->var.vmode &= ~FB_VMODE_YWRAP;
  969. mutex_unlock(&mx3_fbi->mutex);
  970. dev_dbg(fbi->device, "Update complete\n");
  971. return 0;
  972. }
  973. /*
  974. * This structure contains the pointers to the control functions that are
  975. * invoked by the core framebuffer driver to perform operations like
  976. * blitting, rectangle filling, copy regions and cursor definition.
  977. */
  978. static struct fb_ops mx3fb_ops = {
  979. .owner = THIS_MODULE,
  980. .fb_set_par = mx3fb_set_par,
  981. .fb_check_var = mx3fb_check_var,
  982. .fb_setcolreg = mx3fb_setcolreg,
  983. .fb_pan_display = mx3fb_pan_display,
  984. .fb_fillrect = cfb_fillrect,
  985. .fb_copyarea = cfb_copyarea,
  986. .fb_imageblit = cfb_imageblit,
  987. .fb_blank = mx3fb_blank,
  988. };
  989. #ifdef CONFIG_PM
  990. /*
  991. * Power management hooks. Note that we won't be called from IRQ context,
  992. * unlike the blank functions above, so we may sleep.
  993. */
  994. /*
  995. * Suspends the framebuffer and blanks the screen. Power management support
  996. */
  997. static int mx3fb_suspend(struct platform_device *pdev, pm_message_t state)
  998. {
  999. struct mx3fb_data *mx3fb = platform_get_drvdata(pdev);
  1000. struct mx3fb_info *mx3_fbi = mx3fb->fbi->par;
  1001. console_lock();
  1002. fb_set_suspend(mx3fb->fbi, 1);
  1003. console_unlock();
  1004. if (mx3_fbi->blank == FB_BLANK_UNBLANK) {
  1005. sdc_disable_channel(mx3_fbi);
  1006. sdc_set_brightness(mx3fb, 0);
  1007. }
  1008. return 0;
  1009. }
  1010. /*
  1011. * Resumes the framebuffer and unblanks the screen. Power management support
  1012. */
  1013. static int mx3fb_resume(struct platform_device *pdev)
  1014. {
  1015. struct mx3fb_data *mx3fb = platform_get_drvdata(pdev);
  1016. struct mx3fb_info *mx3_fbi = mx3fb->fbi->par;
  1017. if (mx3_fbi->blank == FB_BLANK_UNBLANK) {
  1018. sdc_enable_channel(mx3_fbi);
  1019. sdc_set_brightness(mx3fb, mx3fb->backlight_level);
  1020. }
  1021. console_lock();
  1022. fb_set_suspend(mx3fb->fbi, 0);
  1023. console_unlock();
  1024. return 0;
  1025. }
  1026. #else
  1027. #define mx3fb_suspend NULL
  1028. #define mx3fb_resume NULL
  1029. #endif
  1030. /*
  1031. * Main framebuffer functions
  1032. */
  1033. /**
  1034. * mx3fb_map_video_memory() - allocates the DRAM memory for the frame buffer.
  1035. * @fbi: framebuffer information pointer
  1036. * @mem_len: length of mapped memory
  1037. * @lock: do not lock during initialisation
  1038. * @return: Error code indicating success or failure
  1039. *
  1040. * This buffer is remapped into a non-cached, non-buffered, memory region to
  1041. * allow palette and pixel writes to occur without flushing the cache. Once this
  1042. * area is remapped, all virtual memory access to the video memory should occur
  1043. * at the new region.
  1044. */
  1045. static int mx3fb_map_video_memory(struct fb_info *fbi, unsigned int mem_len,
  1046. bool lock)
  1047. {
  1048. int retval = 0;
  1049. dma_addr_t addr;
  1050. fbi->screen_base = dma_alloc_writecombine(fbi->device,
  1051. mem_len,
  1052. &addr, GFP_DMA);
  1053. if (!fbi->screen_base) {
  1054. dev_err(fbi->device, "Cannot allocate %u bytes framebuffer memory\n",
  1055. mem_len);
  1056. retval = -EBUSY;
  1057. goto err0;
  1058. }
  1059. if (lock)
  1060. mutex_lock(&fbi->mm_lock);
  1061. fbi->fix.smem_start = addr;
  1062. fbi->fix.smem_len = mem_len;
  1063. if (lock)
  1064. mutex_unlock(&fbi->mm_lock);
  1065. dev_dbg(fbi->device, "allocated fb @ p=0x%08x, v=0x%p, size=%d.\n",
  1066. (uint32_t) fbi->fix.smem_start, fbi->screen_base, fbi->fix.smem_len);
  1067. fbi->screen_size = fbi->fix.smem_len;
  1068. /* Clear the screen */
  1069. memset((char *)fbi->screen_base, 0, fbi->fix.smem_len);
  1070. return 0;
  1071. err0:
  1072. fbi->fix.smem_len = 0;
  1073. fbi->fix.smem_start = 0;
  1074. fbi->screen_base = NULL;
  1075. return retval;
  1076. }
  1077. /**
  1078. * mx3fb_unmap_video_memory() - de-allocate frame buffer memory.
  1079. * @fbi: framebuffer information pointer
  1080. * @return: error code indicating success or failure
  1081. */
  1082. static int mx3fb_unmap_video_memory(struct fb_info *fbi)
  1083. {
  1084. dma_free_writecombine(fbi->device, fbi->fix.smem_len,
  1085. fbi->screen_base, fbi->fix.smem_start);
  1086. fbi->screen_base = 0;
  1087. mutex_lock(&fbi->mm_lock);
  1088. fbi->fix.smem_start = 0;
  1089. fbi->fix.smem_len = 0;
  1090. mutex_unlock(&fbi->mm_lock);
  1091. return 0;
  1092. }
  1093. /**
  1094. * mx3fb_init_fbinfo() - initialize framebuffer information object.
  1095. * @return: initialized framebuffer structure.
  1096. */
  1097. static struct fb_info *mx3fb_init_fbinfo(struct device *dev, struct fb_ops *ops)
  1098. {
  1099. struct fb_info *fbi;
  1100. struct mx3fb_info *mx3fbi;
  1101. int ret;
  1102. /* Allocate sufficient memory for the fb structure */
  1103. fbi = framebuffer_alloc(sizeof(struct mx3fb_info), dev);
  1104. if (!fbi)
  1105. return NULL;
  1106. mx3fbi = fbi->par;
  1107. mx3fbi->cookie = -EINVAL;
  1108. mx3fbi->cur_ipu_buf = 0;
  1109. fbi->var.activate = FB_ACTIVATE_NOW;
  1110. fbi->fbops = ops;
  1111. fbi->flags = FBINFO_FLAG_DEFAULT;
  1112. fbi->pseudo_palette = mx3fbi->pseudo_palette;
  1113. mutex_init(&mx3fbi->mutex);
  1114. /* Allocate colormap */
  1115. ret = fb_alloc_cmap(&fbi->cmap, 16, 0);
  1116. if (ret < 0) {
  1117. framebuffer_release(fbi);
  1118. return NULL;
  1119. }
  1120. return fbi;
  1121. }
  1122. static int init_fb_chan(struct mx3fb_data *mx3fb, struct idmac_channel *ichan)
  1123. {
  1124. struct device *dev = mx3fb->dev;
  1125. struct mx3fb_platform_data *mx3fb_pdata = dev->platform_data;
  1126. const char *name = mx3fb_pdata->name;
  1127. unsigned int irq;
  1128. struct fb_info *fbi;
  1129. struct mx3fb_info *mx3fbi;
  1130. const struct fb_videomode *mode;
  1131. int ret, num_modes;
  1132. ichan->client = mx3fb;
  1133. irq = ichan->eof_irq;
  1134. if (ichan->dma_chan.chan_id != IDMAC_SDC_0)
  1135. return -EINVAL;
  1136. fbi = mx3fb_init_fbinfo(dev, &mx3fb_ops);
  1137. if (!fbi)
  1138. return -ENOMEM;
  1139. if (!fb_mode)
  1140. fb_mode = name;
  1141. if (!fb_mode) {
  1142. ret = -EINVAL;
  1143. goto emode;
  1144. }
  1145. if (mx3fb_pdata->mode && mx3fb_pdata->num_modes) {
  1146. mode = mx3fb_pdata->mode;
  1147. num_modes = mx3fb_pdata->num_modes;
  1148. } else {
  1149. mode = mx3fb_modedb;
  1150. num_modes = ARRAY_SIZE(mx3fb_modedb);
  1151. }
  1152. if (!fb_find_mode(&fbi->var, fbi, fb_mode, mode,
  1153. num_modes, NULL, default_bpp)) {
  1154. ret = -EBUSY;
  1155. goto emode;
  1156. }
  1157. fb_videomode_to_modelist(mode, num_modes, &fbi->modelist);
  1158. /* Default Y virtual size is 2x panel size */
  1159. fbi->var.yres_virtual = fbi->var.yres * 2;
  1160. mx3fb->fbi = fbi;
  1161. /* set Display Interface clock period */
  1162. mx3fb_write_reg(mx3fb, 0x00100010L, DI_HSP_CLK_PER);
  1163. /* Might need to trigger HSP clock change - see 44.3.3.8.5 */
  1164. sdc_set_brightness(mx3fb, 255);
  1165. sdc_set_global_alpha(mx3fb, true, 0xFF);
  1166. sdc_set_color_key(mx3fb, IDMAC_SDC_0, false, 0);
  1167. mx3fbi = fbi->par;
  1168. mx3fbi->idmac_channel = ichan;
  1169. mx3fbi->ipu_ch = ichan->dma_chan.chan_id;
  1170. mx3fbi->mx3fb = mx3fb;
  1171. mx3fbi->blank = FB_BLANK_NORMAL;
  1172. init_completion(&mx3fbi->flip_cmpl);
  1173. disable_irq(ichan->eof_irq);
  1174. dev_dbg(mx3fb->dev, "disabling irq %d\n", ichan->eof_irq);
  1175. ret = __set_par(fbi, false);
  1176. if (ret < 0)
  1177. goto esetpar;
  1178. __blank(FB_BLANK_UNBLANK, fbi);
  1179. dev_info(dev, "registered, using mode %s\n", fb_mode);
  1180. ret = register_framebuffer(fbi);
  1181. if (ret < 0)
  1182. goto erfb;
  1183. return 0;
  1184. erfb:
  1185. esetpar:
  1186. emode:
  1187. fb_dealloc_cmap(&fbi->cmap);
  1188. framebuffer_release(fbi);
  1189. return ret;
  1190. }
  1191. static bool chan_filter(struct dma_chan *chan, void *arg)
  1192. {
  1193. struct dma_chan_request *rq = arg;
  1194. struct device *dev;
  1195. struct mx3fb_platform_data *mx3fb_pdata;
  1196. if (!imx_dma_is_ipu(chan))
  1197. return false;
  1198. if (!rq)
  1199. return false;
  1200. dev = rq->mx3fb->dev;
  1201. mx3fb_pdata = dev->platform_data;
  1202. return rq->id == chan->chan_id &&
  1203. mx3fb_pdata->dma_dev == chan->device->dev;
  1204. }
  1205. static void release_fbi(struct fb_info *fbi)
  1206. {
  1207. mx3fb_unmap_video_memory(fbi);
  1208. fb_dealloc_cmap(&fbi->cmap);
  1209. unregister_framebuffer(fbi);
  1210. framebuffer_release(fbi);
  1211. }
  1212. static int mx3fb_probe(struct platform_device *pdev)
  1213. {
  1214. struct device *dev = &pdev->dev;
  1215. int ret;
  1216. struct resource *sdc_reg;
  1217. struct mx3fb_data *mx3fb;
  1218. dma_cap_mask_t mask;
  1219. struct dma_chan *chan;
  1220. struct dma_chan_request rq;
  1221. /*
  1222. * Display Interface (DI) and Synchronous Display Controller (SDC)
  1223. * registers
  1224. */
  1225. sdc_reg = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1226. if (!sdc_reg)
  1227. return -EINVAL;
  1228. mx3fb = kzalloc(sizeof(*mx3fb), GFP_KERNEL);
  1229. if (!mx3fb)
  1230. return -ENOMEM;
  1231. spin_lock_init(&mx3fb->lock);
  1232. mx3fb->reg_base = ioremap(sdc_reg->start, resource_size(sdc_reg));
  1233. if (!mx3fb->reg_base) {
  1234. ret = -ENOMEM;
  1235. goto eremap;
  1236. }
  1237. pr_debug("Remapped %pR at %p\n", sdc_reg, mx3fb->reg_base);
  1238. /* IDMAC interface */
  1239. dmaengine_get();
  1240. mx3fb->dev = dev;
  1241. platform_set_drvdata(pdev, mx3fb);
  1242. rq.mx3fb = mx3fb;
  1243. dma_cap_zero(mask);
  1244. dma_cap_set(DMA_SLAVE, mask);
  1245. dma_cap_set(DMA_PRIVATE, mask);
  1246. rq.id = IDMAC_SDC_0;
  1247. chan = dma_request_channel(mask, chan_filter, &rq);
  1248. if (!chan) {
  1249. ret = -EBUSY;
  1250. goto ersdc0;
  1251. }
  1252. mx3fb->backlight_level = 255;
  1253. ret = init_fb_chan(mx3fb, to_idmac_chan(chan));
  1254. if (ret < 0)
  1255. goto eisdc0;
  1256. return 0;
  1257. eisdc0:
  1258. dma_release_channel(chan);
  1259. ersdc0:
  1260. dmaengine_put();
  1261. iounmap(mx3fb->reg_base);
  1262. eremap:
  1263. kfree(mx3fb);
  1264. dev_err(dev, "mx3fb: failed to register fb\n");
  1265. return ret;
  1266. }
  1267. static int mx3fb_remove(struct platform_device *dev)
  1268. {
  1269. struct mx3fb_data *mx3fb = platform_get_drvdata(dev);
  1270. struct fb_info *fbi = mx3fb->fbi;
  1271. struct mx3fb_info *mx3_fbi = fbi->par;
  1272. struct dma_chan *chan;
  1273. chan = &mx3_fbi->idmac_channel->dma_chan;
  1274. release_fbi(fbi);
  1275. dma_release_channel(chan);
  1276. dmaengine_put();
  1277. iounmap(mx3fb->reg_base);
  1278. kfree(mx3fb);
  1279. return 0;
  1280. }
  1281. static struct platform_driver mx3fb_driver = {
  1282. .driver = {
  1283. .name = MX3FB_NAME,
  1284. },
  1285. .probe = mx3fb_probe,
  1286. .remove = mx3fb_remove,
  1287. .suspend = mx3fb_suspend,
  1288. .resume = mx3fb_resume,
  1289. };
  1290. /*
  1291. * Parse user specified options (`video=mx3fb:')
  1292. * example:
  1293. * video=mx3fb:bpp=16
  1294. */
  1295. static int __init mx3fb_setup(void)
  1296. {
  1297. #ifndef MODULE
  1298. char *opt, *options = NULL;
  1299. if (fb_get_options("mx3fb", &options))
  1300. return -ENODEV;
  1301. if (!options || !*options)
  1302. return 0;
  1303. while ((opt = strsep(&options, ",")) != NULL) {
  1304. if (!*opt)
  1305. continue;
  1306. if (!strncmp(opt, "bpp=", 4))
  1307. default_bpp = simple_strtoul(opt + 4, NULL, 0);
  1308. else
  1309. fb_mode = opt;
  1310. }
  1311. #endif
  1312. return 0;
  1313. }
  1314. static int __init mx3fb_init(void)
  1315. {
  1316. int ret = mx3fb_setup();
  1317. if (ret < 0)
  1318. return ret;
  1319. ret = platform_driver_register(&mx3fb_driver);
  1320. return ret;
  1321. }
  1322. static void __exit mx3fb_exit(void)
  1323. {
  1324. platform_driver_unregister(&mx3fb_driver);
  1325. }
  1326. module_init(mx3fb_init);
  1327. module_exit(mx3fb_exit);
  1328. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  1329. MODULE_DESCRIPTION("MX3 framebuffer driver");
  1330. MODULE_ALIAS("platform:" MX3FB_NAME);
  1331. MODULE_LICENSE("GPL v2");