exynos_mixer.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  1. /*
  2. * Copyright (C) 2011 Samsung Electronics Co.Ltd
  3. * Authors:
  4. * Seung-Woo Kim <sw0312.kim@samsung.com>
  5. * Inki Dae <inki.dae@samsung.com>
  6. * Joonyoung Shim <jy0922.shim@samsung.com>
  7. *
  8. * Based on drivers/media/video/s5p-tv/mixer_reg.c
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. *
  15. */
  16. #include "drmP.h"
  17. #include "regs-mixer.h"
  18. #include "regs-vp.h"
  19. #include <linux/kernel.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/wait.h>
  22. #include <linux/i2c.h>
  23. #include <linux/module.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/irq.h>
  27. #include <linux/delay.h>
  28. #include <linux/pm_runtime.h>
  29. #include <linux/clk.h>
  30. #include <linux/regulator/consumer.h>
  31. #include <drm/exynos_drm.h>
  32. #include "exynos_drm_drv.h"
  33. #include "exynos_drm_hdmi.h"
  34. #define MIXER_WIN_NR 3
  35. #define MIXER_DEFAULT_WIN 0
  36. #define get_mixer_context(dev) platform_get_drvdata(to_platform_device(dev))
  37. struct hdmi_win_data {
  38. dma_addr_t dma_addr;
  39. void __iomem *vaddr;
  40. dma_addr_t chroma_dma_addr;
  41. void __iomem *chroma_vaddr;
  42. uint32_t pixel_format;
  43. unsigned int bpp;
  44. unsigned int crtc_x;
  45. unsigned int crtc_y;
  46. unsigned int crtc_width;
  47. unsigned int crtc_height;
  48. unsigned int fb_x;
  49. unsigned int fb_y;
  50. unsigned int fb_width;
  51. unsigned int fb_height;
  52. unsigned int mode_width;
  53. unsigned int mode_height;
  54. unsigned int scan_flags;
  55. };
  56. struct mixer_resources {
  57. struct device *dev;
  58. int irq;
  59. void __iomem *mixer_regs;
  60. void __iomem *vp_regs;
  61. spinlock_t reg_slock;
  62. struct clk *mixer;
  63. struct clk *vp;
  64. struct clk *sclk_mixer;
  65. struct clk *sclk_hdmi;
  66. struct clk *sclk_dac;
  67. };
  68. struct mixer_context {
  69. unsigned int irq;
  70. int pipe;
  71. bool interlace;
  72. struct mixer_resources mixer_res;
  73. struct hdmi_win_data win_data[MIXER_WIN_NR];
  74. };
  75. static const u8 filter_y_horiz_tap8[] = {
  76. 0, -1, -1, -1, -1, -1, -1, -1,
  77. -1, -1, -1, -1, -1, 0, 0, 0,
  78. 0, 2, 4, 5, 6, 6, 6, 6,
  79. 6, 5, 5, 4, 3, 2, 1, 1,
  80. 0, -6, -12, -16, -18, -20, -21, -20,
  81. -20, -18, -16, -13, -10, -8, -5, -2,
  82. 127, 126, 125, 121, 114, 107, 99, 89,
  83. 79, 68, 57, 46, 35, 25, 16, 8,
  84. };
  85. static const u8 filter_y_vert_tap4[] = {
  86. 0, -3, -6, -8, -8, -8, -8, -7,
  87. -6, -5, -4, -3, -2, -1, -1, 0,
  88. 127, 126, 124, 118, 111, 102, 92, 81,
  89. 70, 59, 48, 37, 27, 19, 11, 5,
  90. 0, 5, 11, 19, 27, 37, 48, 59,
  91. 70, 81, 92, 102, 111, 118, 124, 126,
  92. 0, 0, -1, -1, -2, -3, -4, -5,
  93. -6, -7, -8, -8, -8, -8, -6, -3,
  94. };
  95. static const u8 filter_cr_horiz_tap4[] = {
  96. 0, -3, -6, -8, -8, -8, -8, -7,
  97. -6, -5, -4, -3, -2, -1, -1, 0,
  98. 127, 126, 124, 118, 111, 102, 92, 81,
  99. 70, 59, 48, 37, 27, 19, 11, 5,
  100. };
  101. static inline u32 vp_reg_read(struct mixer_resources *res, u32 reg_id)
  102. {
  103. return readl(res->vp_regs + reg_id);
  104. }
  105. static inline void vp_reg_write(struct mixer_resources *res, u32 reg_id,
  106. u32 val)
  107. {
  108. writel(val, res->vp_regs + reg_id);
  109. }
  110. static inline void vp_reg_writemask(struct mixer_resources *res, u32 reg_id,
  111. u32 val, u32 mask)
  112. {
  113. u32 old = vp_reg_read(res, reg_id);
  114. val = (val & mask) | (old & ~mask);
  115. writel(val, res->vp_regs + reg_id);
  116. }
  117. static inline u32 mixer_reg_read(struct mixer_resources *res, u32 reg_id)
  118. {
  119. return readl(res->mixer_regs + reg_id);
  120. }
  121. static inline void mixer_reg_write(struct mixer_resources *res, u32 reg_id,
  122. u32 val)
  123. {
  124. writel(val, res->mixer_regs + reg_id);
  125. }
  126. static inline void mixer_reg_writemask(struct mixer_resources *res,
  127. u32 reg_id, u32 val, u32 mask)
  128. {
  129. u32 old = mixer_reg_read(res, reg_id);
  130. val = (val & mask) | (old & ~mask);
  131. writel(val, res->mixer_regs + reg_id);
  132. }
  133. static void mixer_regs_dump(struct mixer_context *ctx)
  134. {
  135. #define DUMPREG(reg_id) \
  136. do { \
  137. DRM_DEBUG_KMS(#reg_id " = %08x\n", \
  138. (u32)readl(ctx->mixer_res.mixer_regs + reg_id)); \
  139. } while (0)
  140. DUMPREG(MXR_STATUS);
  141. DUMPREG(MXR_CFG);
  142. DUMPREG(MXR_INT_EN);
  143. DUMPREG(MXR_INT_STATUS);
  144. DUMPREG(MXR_LAYER_CFG);
  145. DUMPREG(MXR_VIDEO_CFG);
  146. DUMPREG(MXR_GRAPHIC0_CFG);
  147. DUMPREG(MXR_GRAPHIC0_BASE);
  148. DUMPREG(MXR_GRAPHIC0_SPAN);
  149. DUMPREG(MXR_GRAPHIC0_WH);
  150. DUMPREG(MXR_GRAPHIC0_SXY);
  151. DUMPREG(MXR_GRAPHIC0_DXY);
  152. DUMPREG(MXR_GRAPHIC1_CFG);
  153. DUMPREG(MXR_GRAPHIC1_BASE);
  154. DUMPREG(MXR_GRAPHIC1_SPAN);
  155. DUMPREG(MXR_GRAPHIC1_WH);
  156. DUMPREG(MXR_GRAPHIC1_SXY);
  157. DUMPREG(MXR_GRAPHIC1_DXY);
  158. #undef DUMPREG
  159. }
  160. static void vp_regs_dump(struct mixer_context *ctx)
  161. {
  162. #define DUMPREG(reg_id) \
  163. do { \
  164. DRM_DEBUG_KMS(#reg_id " = %08x\n", \
  165. (u32) readl(ctx->mixer_res.vp_regs + reg_id)); \
  166. } while (0)
  167. DUMPREG(VP_ENABLE);
  168. DUMPREG(VP_SRESET);
  169. DUMPREG(VP_SHADOW_UPDATE);
  170. DUMPREG(VP_FIELD_ID);
  171. DUMPREG(VP_MODE);
  172. DUMPREG(VP_IMG_SIZE_Y);
  173. DUMPREG(VP_IMG_SIZE_C);
  174. DUMPREG(VP_PER_RATE_CTRL);
  175. DUMPREG(VP_TOP_Y_PTR);
  176. DUMPREG(VP_BOT_Y_PTR);
  177. DUMPREG(VP_TOP_C_PTR);
  178. DUMPREG(VP_BOT_C_PTR);
  179. DUMPREG(VP_ENDIAN_MODE);
  180. DUMPREG(VP_SRC_H_POSITION);
  181. DUMPREG(VP_SRC_V_POSITION);
  182. DUMPREG(VP_SRC_WIDTH);
  183. DUMPREG(VP_SRC_HEIGHT);
  184. DUMPREG(VP_DST_H_POSITION);
  185. DUMPREG(VP_DST_V_POSITION);
  186. DUMPREG(VP_DST_WIDTH);
  187. DUMPREG(VP_DST_HEIGHT);
  188. DUMPREG(VP_H_RATIO);
  189. DUMPREG(VP_V_RATIO);
  190. #undef DUMPREG
  191. }
  192. static inline void vp_filter_set(struct mixer_resources *res,
  193. int reg_id, const u8 *data, unsigned int size)
  194. {
  195. /* assure 4-byte align */
  196. BUG_ON(size & 3);
  197. for (; size; size -= 4, reg_id += 4, data += 4) {
  198. u32 val = (data[0] << 24) | (data[1] << 16) |
  199. (data[2] << 8) | data[3];
  200. vp_reg_write(res, reg_id, val);
  201. }
  202. }
  203. static void vp_default_filter(struct mixer_resources *res)
  204. {
  205. vp_filter_set(res, VP_POLY8_Y0_LL,
  206. filter_y_horiz_tap8, sizeof filter_y_horiz_tap8);
  207. vp_filter_set(res, VP_POLY4_Y0_LL,
  208. filter_y_vert_tap4, sizeof filter_y_vert_tap4);
  209. vp_filter_set(res, VP_POLY4_C0_LL,
  210. filter_cr_horiz_tap4, sizeof filter_cr_horiz_tap4);
  211. }
  212. static void mixer_vsync_set_update(struct mixer_context *ctx, bool enable)
  213. {
  214. struct mixer_resources *res = &ctx->mixer_res;
  215. /* block update on vsync */
  216. mixer_reg_writemask(res, MXR_STATUS, enable ?
  217. MXR_STATUS_SYNC_ENABLE : 0, MXR_STATUS_SYNC_ENABLE);
  218. vp_reg_write(res, VP_SHADOW_UPDATE, enable ?
  219. VP_SHADOW_UPDATE_ENABLE : 0);
  220. }
  221. static void mixer_cfg_scan(struct mixer_context *ctx, unsigned int height)
  222. {
  223. struct mixer_resources *res = &ctx->mixer_res;
  224. u32 val;
  225. /* choosing between interlace and progressive mode */
  226. val = (ctx->interlace ? MXR_CFG_SCAN_INTERLACE :
  227. MXR_CFG_SCAN_PROGRASSIVE);
  228. /* choosing between porper HD and SD mode */
  229. if (height == 480)
  230. val |= MXR_CFG_SCAN_NTSC | MXR_CFG_SCAN_SD;
  231. else if (height == 576)
  232. val |= MXR_CFG_SCAN_PAL | MXR_CFG_SCAN_SD;
  233. else if (height == 720)
  234. val |= MXR_CFG_SCAN_HD_720 | MXR_CFG_SCAN_HD;
  235. else if (height == 1080)
  236. val |= MXR_CFG_SCAN_HD_1080 | MXR_CFG_SCAN_HD;
  237. else
  238. val |= MXR_CFG_SCAN_HD_720 | MXR_CFG_SCAN_HD;
  239. mixer_reg_writemask(res, MXR_CFG, val, MXR_CFG_SCAN_MASK);
  240. }
  241. static void mixer_cfg_rgb_fmt(struct mixer_context *ctx, unsigned int height)
  242. {
  243. struct mixer_resources *res = &ctx->mixer_res;
  244. u32 val;
  245. if (height == 480) {
  246. val = MXR_CFG_RGB601_0_255;
  247. } else if (height == 576) {
  248. val = MXR_CFG_RGB601_0_255;
  249. } else if (height == 720) {
  250. val = MXR_CFG_RGB709_16_235;
  251. mixer_reg_write(res, MXR_CM_COEFF_Y,
  252. (1 << 30) | (94 << 20) | (314 << 10) |
  253. (32 << 0));
  254. mixer_reg_write(res, MXR_CM_COEFF_CB,
  255. (972 << 20) | (851 << 10) | (225 << 0));
  256. mixer_reg_write(res, MXR_CM_COEFF_CR,
  257. (225 << 20) | (820 << 10) | (1004 << 0));
  258. } else if (height == 1080) {
  259. val = MXR_CFG_RGB709_16_235;
  260. mixer_reg_write(res, MXR_CM_COEFF_Y,
  261. (1 << 30) | (94 << 20) | (314 << 10) |
  262. (32 << 0));
  263. mixer_reg_write(res, MXR_CM_COEFF_CB,
  264. (972 << 20) | (851 << 10) | (225 << 0));
  265. mixer_reg_write(res, MXR_CM_COEFF_CR,
  266. (225 << 20) | (820 << 10) | (1004 << 0));
  267. } else {
  268. val = MXR_CFG_RGB709_16_235;
  269. mixer_reg_write(res, MXR_CM_COEFF_Y,
  270. (1 << 30) | (94 << 20) | (314 << 10) |
  271. (32 << 0));
  272. mixer_reg_write(res, MXR_CM_COEFF_CB,
  273. (972 << 20) | (851 << 10) | (225 << 0));
  274. mixer_reg_write(res, MXR_CM_COEFF_CR,
  275. (225 << 20) | (820 << 10) | (1004 << 0));
  276. }
  277. mixer_reg_writemask(res, MXR_CFG, val, MXR_CFG_RGB_FMT_MASK);
  278. }
  279. static void mixer_cfg_layer(struct mixer_context *ctx, int win, bool enable)
  280. {
  281. struct mixer_resources *res = &ctx->mixer_res;
  282. u32 val = enable ? ~0 : 0;
  283. switch (win) {
  284. case 0:
  285. mixer_reg_writemask(res, MXR_CFG, val, MXR_CFG_GRP0_ENABLE);
  286. break;
  287. case 1:
  288. mixer_reg_writemask(res, MXR_CFG, val, MXR_CFG_GRP1_ENABLE);
  289. break;
  290. case 2:
  291. vp_reg_writemask(res, VP_ENABLE, val, VP_ENABLE_ON);
  292. mixer_reg_writemask(res, MXR_CFG, val, MXR_CFG_VP_ENABLE);
  293. break;
  294. }
  295. }
  296. static void mixer_run(struct mixer_context *ctx)
  297. {
  298. struct mixer_resources *res = &ctx->mixer_res;
  299. mixer_reg_writemask(res, MXR_STATUS, ~0, MXR_STATUS_REG_RUN);
  300. mixer_regs_dump(ctx);
  301. }
  302. static void vp_video_buffer(struct mixer_context *ctx, int win)
  303. {
  304. struct mixer_resources *res = &ctx->mixer_res;
  305. unsigned long flags;
  306. struct hdmi_win_data *win_data;
  307. unsigned int full_width, full_height, width, height;
  308. unsigned int x_ratio, y_ratio;
  309. unsigned int src_x_offset, src_y_offset, dst_x_offset, dst_y_offset;
  310. unsigned int mode_width, mode_height;
  311. unsigned int buf_num;
  312. dma_addr_t luma_addr[2], chroma_addr[2];
  313. bool tiled_mode = false;
  314. bool crcb_mode = false;
  315. u32 val;
  316. win_data = &ctx->win_data[win];
  317. switch (win_data->pixel_format) {
  318. case DRM_FORMAT_NV12MT:
  319. tiled_mode = true;
  320. case DRM_FORMAT_NV12M:
  321. crcb_mode = false;
  322. buf_num = 2;
  323. break;
  324. /* TODO: single buffer format NV12, NV21 */
  325. default:
  326. /* ignore pixel format at disable time */
  327. if (!win_data->dma_addr)
  328. break;
  329. DRM_ERROR("pixel format for vp is wrong [%d].\n",
  330. win_data->pixel_format);
  331. return;
  332. }
  333. full_width = win_data->fb_width;
  334. full_height = win_data->fb_height;
  335. width = win_data->crtc_width;
  336. height = win_data->crtc_height;
  337. mode_width = win_data->mode_width;
  338. mode_height = win_data->mode_height;
  339. /* scaling feature: (src << 16) / dst */
  340. x_ratio = (width << 16) / width;
  341. y_ratio = (height << 16) / height;
  342. src_x_offset = win_data->fb_x;
  343. src_y_offset = win_data->fb_y;
  344. dst_x_offset = win_data->crtc_x;
  345. dst_y_offset = win_data->crtc_y;
  346. if (buf_num == 2) {
  347. luma_addr[0] = win_data->dma_addr;
  348. chroma_addr[0] = win_data->chroma_dma_addr;
  349. } else {
  350. luma_addr[0] = win_data->dma_addr;
  351. chroma_addr[0] = win_data->dma_addr
  352. + (full_width * full_height);
  353. }
  354. if (win_data->scan_flags & DRM_MODE_FLAG_INTERLACE) {
  355. ctx->interlace = true;
  356. if (tiled_mode) {
  357. luma_addr[1] = luma_addr[0] + 0x40;
  358. chroma_addr[1] = chroma_addr[0] + 0x40;
  359. } else {
  360. luma_addr[1] = luma_addr[0] + full_width;
  361. chroma_addr[1] = chroma_addr[0] + full_width;
  362. }
  363. } else {
  364. ctx->interlace = false;
  365. luma_addr[1] = 0;
  366. chroma_addr[1] = 0;
  367. }
  368. spin_lock_irqsave(&res->reg_slock, flags);
  369. mixer_vsync_set_update(ctx, false);
  370. /* interlace or progressive scan mode */
  371. val = (ctx->interlace ? ~0 : 0);
  372. vp_reg_writemask(res, VP_MODE, val, VP_MODE_LINE_SKIP);
  373. /* setup format */
  374. val = (crcb_mode ? VP_MODE_NV21 : VP_MODE_NV12);
  375. val |= (tiled_mode ? VP_MODE_MEM_TILED : VP_MODE_MEM_LINEAR);
  376. vp_reg_writemask(res, VP_MODE, val, VP_MODE_FMT_MASK);
  377. /* setting size of input image */
  378. vp_reg_write(res, VP_IMG_SIZE_Y, VP_IMG_HSIZE(full_width) |
  379. VP_IMG_VSIZE(full_height));
  380. /* chroma height has to reduced by 2 to avoid chroma distorions */
  381. vp_reg_write(res, VP_IMG_SIZE_C, VP_IMG_HSIZE(full_width) |
  382. VP_IMG_VSIZE(full_height / 2));
  383. vp_reg_write(res, VP_SRC_WIDTH, width);
  384. vp_reg_write(res, VP_SRC_HEIGHT, height);
  385. vp_reg_write(res, VP_SRC_H_POSITION,
  386. VP_SRC_H_POSITION_VAL(src_x_offset));
  387. vp_reg_write(res, VP_SRC_V_POSITION, src_y_offset);
  388. vp_reg_write(res, VP_DST_WIDTH, width);
  389. vp_reg_write(res, VP_DST_H_POSITION, dst_x_offset);
  390. if (ctx->interlace) {
  391. vp_reg_write(res, VP_DST_HEIGHT, height / 2);
  392. vp_reg_write(res, VP_DST_V_POSITION, dst_y_offset / 2);
  393. } else {
  394. vp_reg_write(res, VP_DST_HEIGHT, height);
  395. vp_reg_write(res, VP_DST_V_POSITION, dst_y_offset);
  396. }
  397. vp_reg_write(res, VP_H_RATIO, x_ratio);
  398. vp_reg_write(res, VP_V_RATIO, y_ratio);
  399. vp_reg_write(res, VP_ENDIAN_MODE, VP_ENDIAN_MODE_LITTLE);
  400. /* set buffer address to vp */
  401. vp_reg_write(res, VP_TOP_Y_PTR, luma_addr[0]);
  402. vp_reg_write(res, VP_BOT_Y_PTR, luma_addr[1]);
  403. vp_reg_write(res, VP_TOP_C_PTR, chroma_addr[0]);
  404. vp_reg_write(res, VP_BOT_C_PTR, chroma_addr[1]);
  405. mixer_cfg_scan(ctx, mode_height);
  406. mixer_cfg_rgb_fmt(ctx, mode_height);
  407. mixer_cfg_layer(ctx, win, true);
  408. mixer_run(ctx);
  409. mixer_vsync_set_update(ctx, true);
  410. spin_unlock_irqrestore(&res->reg_slock, flags);
  411. vp_regs_dump(ctx);
  412. }
  413. static void mixer_graph_buffer(struct mixer_context *ctx, int win)
  414. {
  415. struct mixer_resources *res = &ctx->mixer_res;
  416. unsigned long flags;
  417. struct hdmi_win_data *win_data;
  418. unsigned int full_width, width, height;
  419. unsigned int x_ratio, y_ratio;
  420. unsigned int src_x_offset, src_y_offset, dst_x_offset, dst_y_offset;
  421. unsigned int mode_width, mode_height;
  422. dma_addr_t dma_addr;
  423. unsigned int fmt;
  424. u32 val;
  425. win_data = &ctx->win_data[win];
  426. #define RGB565 4
  427. #define ARGB1555 5
  428. #define ARGB4444 6
  429. #define ARGB8888 7
  430. switch (win_data->bpp) {
  431. case 16:
  432. fmt = ARGB4444;
  433. break;
  434. case 32:
  435. fmt = ARGB8888;
  436. break;
  437. default:
  438. fmt = ARGB8888;
  439. }
  440. dma_addr = win_data->dma_addr;
  441. full_width = win_data->fb_width;
  442. width = win_data->crtc_width;
  443. height = win_data->crtc_height;
  444. mode_width = win_data->mode_width;
  445. mode_height = win_data->mode_height;
  446. /* 2x scaling feature */
  447. x_ratio = 0;
  448. y_ratio = 0;
  449. src_x_offset = win_data->fb_x;
  450. src_y_offset = win_data->fb_y;
  451. dst_x_offset = win_data->crtc_x;
  452. dst_y_offset = win_data->crtc_y;
  453. /* converting dma address base and source offset */
  454. dma_addr = dma_addr
  455. + (src_x_offset * win_data->bpp >> 3)
  456. + (src_y_offset * full_width * win_data->bpp >> 3);
  457. src_x_offset = 0;
  458. src_y_offset = 0;
  459. if (win_data->scan_flags & DRM_MODE_FLAG_INTERLACE)
  460. ctx->interlace = true;
  461. else
  462. ctx->interlace = false;
  463. spin_lock_irqsave(&res->reg_slock, flags);
  464. mixer_vsync_set_update(ctx, false);
  465. /* setup format */
  466. mixer_reg_writemask(res, MXR_GRAPHIC_CFG(win),
  467. MXR_GRP_CFG_FORMAT_VAL(fmt), MXR_GRP_CFG_FORMAT_MASK);
  468. /* setup geometry */
  469. mixer_reg_write(res, MXR_GRAPHIC_SPAN(win), full_width);
  470. val = MXR_GRP_WH_WIDTH(width);
  471. val |= MXR_GRP_WH_HEIGHT(height);
  472. val |= MXR_GRP_WH_H_SCALE(x_ratio);
  473. val |= MXR_GRP_WH_V_SCALE(y_ratio);
  474. mixer_reg_write(res, MXR_GRAPHIC_WH(win), val);
  475. /* setup offsets in source image */
  476. val = MXR_GRP_SXY_SX(src_x_offset);
  477. val |= MXR_GRP_SXY_SY(src_y_offset);
  478. mixer_reg_write(res, MXR_GRAPHIC_SXY(win), val);
  479. /* setup offsets in display image */
  480. val = MXR_GRP_DXY_DX(dst_x_offset);
  481. val |= MXR_GRP_DXY_DY(dst_y_offset);
  482. mixer_reg_write(res, MXR_GRAPHIC_DXY(win), val);
  483. /* set buffer address to mixer */
  484. mixer_reg_write(res, MXR_GRAPHIC_BASE(win), dma_addr);
  485. mixer_cfg_scan(ctx, mode_height);
  486. mixer_cfg_rgb_fmt(ctx, mode_height);
  487. mixer_cfg_layer(ctx, win, true);
  488. mixer_run(ctx);
  489. mixer_vsync_set_update(ctx, true);
  490. spin_unlock_irqrestore(&res->reg_slock, flags);
  491. }
  492. static void vp_win_reset(struct mixer_context *ctx)
  493. {
  494. struct mixer_resources *res = &ctx->mixer_res;
  495. int tries = 100;
  496. vp_reg_write(res, VP_SRESET, VP_SRESET_PROCESSING);
  497. for (tries = 100; tries; --tries) {
  498. /* waiting until VP_SRESET_PROCESSING is 0 */
  499. if (~vp_reg_read(res, VP_SRESET) & VP_SRESET_PROCESSING)
  500. break;
  501. mdelay(10);
  502. }
  503. WARN(tries == 0, "failed to reset Video Processor\n");
  504. }
  505. static int mixer_enable_vblank(void *ctx, int pipe)
  506. {
  507. struct mixer_context *mixer_ctx = ctx;
  508. struct mixer_resources *res = &mixer_ctx->mixer_res;
  509. DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
  510. mixer_ctx->pipe = pipe;
  511. /* enable vsync interrupt */
  512. mixer_reg_writemask(res, MXR_INT_EN, MXR_INT_EN_VSYNC,
  513. MXR_INT_EN_VSYNC);
  514. return 0;
  515. }
  516. static void mixer_disable_vblank(void *ctx)
  517. {
  518. struct mixer_context *mixer_ctx = ctx;
  519. struct mixer_resources *res = &mixer_ctx->mixer_res;
  520. DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
  521. /* disable vsync interrupt */
  522. mixer_reg_writemask(res, MXR_INT_EN, 0, MXR_INT_EN_VSYNC);
  523. }
  524. static void mixer_win_mode_set(void *ctx,
  525. struct exynos_drm_overlay *overlay)
  526. {
  527. struct mixer_context *mixer_ctx = ctx;
  528. struct hdmi_win_data *win_data;
  529. int win;
  530. DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
  531. if (!overlay) {
  532. DRM_ERROR("overlay is NULL\n");
  533. return;
  534. }
  535. DRM_DEBUG_KMS("set [%d]x[%d] at (%d,%d) to [%d]x[%d] at (%d,%d)\n",
  536. overlay->fb_width, overlay->fb_height,
  537. overlay->fb_x, overlay->fb_y,
  538. overlay->crtc_width, overlay->crtc_height,
  539. overlay->crtc_x, overlay->crtc_y);
  540. win = overlay->zpos;
  541. if (win == DEFAULT_ZPOS)
  542. win = MIXER_DEFAULT_WIN;
  543. if (win < 0 || win > MIXER_WIN_NR) {
  544. DRM_ERROR("overlay plane[%d] is wrong\n", win);
  545. return;
  546. }
  547. win_data = &mixer_ctx->win_data[win];
  548. win_data->dma_addr = overlay->dma_addr[0];
  549. win_data->vaddr = overlay->vaddr[0];
  550. win_data->chroma_dma_addr = overlay->dma_addr[1];
  551. win_data->chroma_vaddr = overlay->vaddr[1];
  552. win_data->pixel_format = overlay->pixel_format;
  553. win_data->bpp = overlay->bpp;
  554. win_data->crtc_x = overlay->crtc_x;
  555. win_data->crtc_y = overlay->crtc_y;
  556. win_data->crtc_width = overlay->crtc_width;
  557. win_data->crtc_height = overlay->crtc_height;
  558. win_data->fb_x = overlay->fb_x;
  559. win_data->fb_y = overlay->fb_y;
  560. win_data->fb_width = overlay->fb_width;
  561. win_data->fb_height = overlay->fb_height;
  562. win_data->mode_width = overlay->mode_width;
  563. win_data->mode_height = overlay->mode_height;
  564. win_data->scan_flags = overlay->scan_flag;
  565. }
  566. static void mixer_win_commit(void *ctx, int zpos)
  567. {
  568. struct mixer_context *mixer_ctx = ctx;
  569. int win = zpos;
  570. DRM_DEBUG_KMS("[%d] %s, win: %d\n", __LINE__, __func__, win);
  571. if (win == DEFAULT_ZPOS)
  572. win = MIXER_DEFAULT_WIN;
  573. if (win < 0 || win > MIXER_WIN_NR) {
  574. DRM_ERROR("overlay plane[%d] is wrong\n", win);
  575. return;
  576. }
  577. if (win > 1)
  578. vp_video_buffer(mixer_ctx, win);
  579. else
  580. mixer_graph_buffer(mixer_ctx, win);
  581. }
  582. static void mixer_win_disable(void *ctx, int zpos)
  583. {
  584. struct mixer_context *mixer_ctx = ctx;
  585. struct mixer_resources *res = &mixer_ctx->mixer_res;
  586. unsigned long flags;
  587. int win = zpos;
  588. DRM_DEBUG_KMS("[%d] %s, win: %d\n", __LINE__, __func__, win);
  589. if (win == DEFAULT_ZPOS)
  590. win = MIXER_DEFAULT_WIN;
  591. if (win < 0 || win > MIXER_WIN_NR) {
  592. DRM_ERROR("overlay plane[%d] is wrong\n", win);
  593. return;
  594. }
  595. spin_lock_irqsave(&res->reg_slock, flags);
  596. mixer_vsync_set_update(mixer_ctx, false);
  597. mixer_cfg_layer(mixer_ctx, win, false);
  598. mixer_vsync_set_update(mixer_ctx, true);
  599. spin_unlock_irqrestore(&res->reg_slock, flags);
  600. }
  601. static struct exynos_mixer_ops mixer_ops = {
  602. /* manager */
  603. .enable_vblank = mixer_enable_vblank,
  604. .disable_vblank = mixer_disable_vblank,
  605. /* overlay */
  606. .win_mode_set = mixer_win_mode_set,
  607. .win_commit = mixer_win_commit,
  608. .win_disable = mixer_win_disable,
  609. };
  610. /* for pageflip event */
  611. static void mixer_finish_pageflip(struct drm_device *drm_dev, int crtc)
  612. {
  613. struct exynos_drm_private *dev_priv = drm_dev->dev_private;
  614. struct drm_pending_vblank_event *e, *t;
  615. struct timeval now;
  616. unsigned long flags;
  617. bool is_checked = false;
  618. spin_lock_irqsave(&drm_dev->event_lock, flags);
  619. list_for_each_entry_safe(e, t, &dev_priv->pageflip_event_list,
  620. base.link) {
  621. /* if event's pipe isn't same as crtc then ignore it. */
  622. if (crtc != e->pipe)
  623. continue;
  624. is_checked = true;
  625. do_gettimeofday(&now);
  626. e->event.sequence = 0;
  627. e->event.tv_sec = now.tv_sec;
  628. e->event.tv_usec = now.tv_usec;
  629. list_move_tail(&e->base.link, &e->base.file_priv->event_list);
  630. wake_up_interruptible(&e->base.file_priv->event_wait);
  631. }
  632. if (is_checked)
  633. /*
  634. * call drm_vblank_put only in case that drm_vblank_get was
  635. * called.
  636. */
  637. if (atomic_read(&drm_dev->vblank_refcount[crtc]) > 0)
  638. drm_vblank_put(drm_dev, crtc);
  639. spin_unlock_irqrestore(&drm_dev->event_lock, flags);
  640. }
  641. static irqreturn_t mixer_irq_handler(int irq, void *arg)
  642. {
  643. struct exynos_drm_hdmi_context *drm_hdmi_ctx = arg;
  644. struct mixer_context *ctx = drm_hdmi_ctx->ctx;
  645. struct mixer_resources *res = &ctx->mixer_res;
  646. u32 val, val_base;
  647. spin_lock(&res->reg_slock);
  648. /* read interrupt status for handling and clearing flags for VSYNC */
  649. val = mixer_reg_read(res, MXR_INT_STATUS);
  650. /* handling VSYNC */
  651. if (val & MXR_INT_STATUS_VSYNC) {
  652. /* interlace scan need to check shadow register */
  653. if (ctx->interlace) {
  654. val_base = mixer_reg_read(res, MXR_GRAPHIC_BASE_S(0));
  655. if (ctx->win_data[0].dma_addr != val_base)
  656. goto out;
  657. val_base = mixer_reg_read(res, MXR_GRAPHIC_BASE_S(1));
  658. if (ctx->win_data[1].dma_addr != val_base)
  659. goto out;
  660. }
  661. drm_handle_vblank(drm_hdmi_ctx->drm_dev, ctx->pipe);
  662. mixer_finish_pageflip(drm_hdmi_ctx->drm_dev, ctx->pipe);
  663. }
  664. out:
  665. /* clear interrupts */
  666. if (~val & MXR_INT_EN_VSYNC) {
  667. /* vsync interrupt use different bit for read and clear */
  668. val &= ~MXR_INT_EN_VSYNC;
  669. val |= MXR_INT_CLEAR_VSYNC;
  670. }
  671. mixer_reg_write(res, MXR_INT_STATUS, val);
  672. spin_unlock(&res->reg_slock);
  673. return IRQ_HANDLED;
  674. }
  675. static void mixer_win_reset(struct mixer_context *ctx)
  676. {
  677. struct mixer_resources *res = &ctx->mixer_res;
  678. unsigned long flags;
  679. u32 val; /* value stored to register */
  680. spin_lock_irqsave(&res->reg_slock, flags);
  681. mixer_vsync_set_update(ctx, false);
  682. mixer_reg_writemask(res, MXR_CFG, MXR_CFG_DST_HDMI, MXR_CFG_DST_MASK);
  683. /* set output in RGB888 mode */
  684. mixer_reg_writemask(res, MXR_CFG, MXR_CFG_OUT_RGB888, MXR_CFG_OUT_MASK);
  685. /* 16 beat burst in DMA */
  686. mixer_reg_writemask(res, MXR_STATUS, MXR_STATUS_16_BURST,
  687. MXR_STATUS_BURST_MASK);
  688. /* setting default layer priority: layer1 > layer0 > video
  689. * because typical usage scenario would be
  690. * layer1 - OSD
  691. * layer0 - framebuffer
  692. * video - video overlay
  693. */
  694. val = MXR_LAYER_CFG_GRP1_VAL(3);
  695. val |= MXR_LAYER_CFG_GRP0_VAL(2);
  696. val |= MXR_LAYER_CFG_VP_VAL(1);
  697. mixer_reg_write(res, MXR_LAYER_CFG, val);
  698. /* setting background color */
  699. mixer_reg_write(res, MXR_BG_COLOR0, 0x008080);
  700. mixer_reg_write(res, MXR_BG_COLOR1, 0x008080);
  701. mixer_reg_write(res, MXR_BG_COLOR2, 0x008080);
  702. /* setting graphical layers */
  703. val = MXR_GRP_CFG_COLOR_KEY_DISABLE; /* no blank key */
  704. val |= MXR_GRP_CFG_WIN_BLEND_EN;
  705. val |= MXR_GRP_CFG_ALPHA_VAL(0xff); /* non-transparent alpha */
  706. /* the same configuration for both layers */
  707. mixer_reg_write(res, MXR_GRAPHIC_CFG(0), val);
  708. val |= MXR_GRP_CFG_BLEND_PRE_MUL;
  709. val |= MXR_GRP_CFG_PIXEL_BLEND_EN;
  710. mixer_reg_write(res, MXR_GRAPHIC_CFG(1), val);
  711. /* configuration of Video Processor Registers */
  712. vp_win_reset(ctx);
  713. vp_default_filter(res);
  714. /* disable all layers */
  715. mixer_reg_writemask(res, MXR_CFG, 0, MXR_CFG_GRP0_ENABLE);
  716. mixer_reg_writemask(res, MXR_CFG, 0, MXR_CFG_GRP1_ENABLE);
  717. mixer_reg_writemask(res, MXR_CFG, 0, MXR_CFG_VP_ENABLE);
  718. mixer_vsync_set_update(ctx, true);
  719. spin_unlock_irqrestore(&res->reg_slock, flags);
  720. }
  721. static void mixer_resource_poweron(struct mixer_context *ctx)
  722. {
  723. struct mixer_resources *res = &ctx->mixer_res;
  724. DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
  725. clk_enable(res->mixer);
  726. clk_enable(res->vp);
  727. clk_enable(res->sclk_mixer);
  728. mixer_win_reset(ctx);
  729. }
  730. static void mixer_resource_poweroff(struct mixer_context *ctx)
  731. {
  732. struct mixer_resources *res = &ctx->mixer_res;
  733. DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
  734. clk_disable(res->mixer);
  735. clk_disable(res->vp);
  736. clk_disable(res->sclk_mixer);
  737. }
  738. static int mixer_runtime_resume(struct device *dev)
  739. {
  740. struct exynos_drm_hdmi_context *ctx = get_mixer_context(dev);
  741. DRM_DEBUG_KMS("resume - start\n");
  742. mixer_resource_poweron(ctx->ctx);
  743. return 0;
  744. }
  745. static int mixer_runtime_suspend(struct device *dev)
  746. {
  747. struct exynos_drm_hdmi_context *ctx = get_mixer_context(dev);
  748. DRM_DEBUG_KMS("suspend - start\n");
  749. mixer_resource_poweroff(ctx->ctx);
  750. return 0;
  751. }
  752. static const struct dev_pm_ops mixer_pm_ops = {
  753. .runtime_suspend = mixer_runtime_suspend,
  754. .runtime_resume = mixer_runtime_resume,
  755. };
  756. static int __devinit mixer_resources_init(struct exynos_drm_hdmi_context *ctx,
  757. struct platform_device *pdev)
  758. {
  759. struct mixer_context *mixer_ctx = ctx->ctx;
  760. struct device *dev = &pdev->dev;
  761. struct mixer_resources *mixer_res = &mixer_ctx->mixer_res;
  762. struct resource *res;
  763. int ret;
  764. mixer_res->dev = dev;
  765. spin_lock_init(&mixer_res->reg_slock);
  766. mixer_res->mixer = clk_get(dev, "mixer");
  767. if (IS_ERR_OR_NULL(mixer_res->mixer)) {
  768. dev_err(dev, "failed to get clock 'mixer'\n");
  769. ret = -ENODEV;
  770. goto fail;
  771. }
  772. mixer_res->vp = clk_get(dev, "vp");
  773. if (IS_ERR_OR_NULL(mixer_res->vp)) {
  774. dev_err(dev, "failed to get clock 'vp'\n");
  775. ret = -ENODEV;
  776. goto fail;
  777. }
  778. mixer_res->sclk_mixer = clk_get(dev, "sclk_mixer");
  779. if (IS_ERR_OR_NULL(mixer_res->sclk_mixer)) {
  780. dev_err(dev, "failed to get clock 'sclk_mixer'\n");
  781. ret = -ENODEV;
  782. goto fail;
  783. }
  784. mixer_res->sclk_hdmi = clk_get(dev, "sclk_hdmi");
  785. if (IS_ERR_OR_NULL(mixer_res->sclk_hdmi)) {
  786. dev_err(dev, "failed to get clock 'sclk_hdmi'\n");
  787. ret = -ENODEV;
  788. goto fail;
  789. }
  790. mixer_res->sclk_dac = clk_get(dev, "sclk_dac");
  791. if (IS_ERR_OR_NULL(mixer_res->sclk_dac)) {
  792. dev_err(dev, "failed to get clock 'sclk_dac'\n");
  793. ret = -ENODEV;
  794. goto fail;
  795. }
  796. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mxr");
  797. if (res == NULL) {
  798. dev_err(dev, "get memory resource failed.\n");
  799. ret = -ENXIO;
  800. goto fail;
  801. }
  802. clk_set_parent(mixer_res->sclk_mixer, mixer_res->sclk_hdmi);
  803. mixer_res->mixer_regs = ioremap(res->start, resource_size(res));
  804. if (mixer_res->mixer_regs == NULL) {
  805. dev_err(dev, "register mapping failed.\n");
  806. ret = -ENXIO;
  807. goto fail;
  808. }
  809. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "vp");
  810. if (res == NULL) {
  811. dev_err(dev, "get memory resource failed.\n");
  812. ret = -ENXIO;
  813. goto fail_mixer_regs;
  814. }
  815. mixer_res->vp_regs = ioremap(res->start, resource_size(res));
  816. if (mixer_res->vp_regs == NULL) {
  817. dev_err(dev, "register mapping failed.\n");
  818. ret = -ENXIO;
  819. goto fail_mixer_regs;
  820. }
  821. res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "irq");
  822. if (res == NULL) {
  823. dev_err(dev, "get interrupt resource failed.\n");
  824. ret = -ENXIO;
  825. goto fail_vp_regs;
  826. }
  827. ret = request_irq(res->start, mixer_irq_handler, 0, "drm_mixer", ctx);
  828. if (ret) {
  829. dev_err(dev, "request interrupt failed.\n");
  830. goto fail_vp_regs;
  831. }
  832. mixer_res->irq = res->start;
  833. return 0;
  834. fail_vp_regs:
  835. iounmap(mixer_res->vp_regs);
  836. fail_mixer_regs:
  837. iounmap(mixer_res->mixer_regs);
  838. fail:
  839. if (!IS_ERR_OR_NULL(mixer_res->sclk_dac))
  840. clk_put(mixer_res->sclk_dac);
  841. if (!IS_ERR_OR_NULL(mixer_res->sclk_hdmi))
  842. clk_put(mixer_res->sclk_hdmi);
  843. if (!IS_ERR_OR_NULL(mixer_res->sclk_mixer))
  844. clk_put(mixer_res->sclk_mixer);
  845. if (!IS_ERR_OR_NULL(mixer_res->vp))
  846. clk_put(mixer_res->vp);
  847. if (!IS_ERR_OR_NULL(mixer_res->mixer))
  848. clk_put(mixer_res->mixer);
  849. mixer_res->dev = NULL;
  850. return ret;
  851. }
  852. static void mixer_resources_cleanup(struct mixer_context *ctx)
  853. {
  854. struct mixer_resources *res = &ctx->mixer_res;
  855. disable_irq(res->irq);
  856. free_irq(res->irq, ctx);
  857. iounmap(res->vp_regs);
  858. iounmap(res->mixer_regs);
  859. }
  860. static int __devinit mixer_probe(struct platform_device *pdev)
  861. {
  862. struct device *dev = &pdev->dev;
  863. struct exynos_drm_hdmi_context *drm_hdmi_ctx;
  864. struct mixer_context *ctx;
  865. int ret;
  866. dev_info(dev, "probe start\n");
  867. drm_hdmi_ctx = kzalloc(sizeof(*drm_hdmi_ctx), GFP_KERNEL);
  868. if (!drm_hdmi_ctx) {
  869. DRM_ERROR("failed to allocate common hdmi context.\n");
  870. return -ENOMEM;
  871. }
  872. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  873. if (!ctx) {
  874. DRM_ERROR("failed to alloc mixer context.\n");
  875. kfree(drm_hdmi_ctx);
  876. return -ENOMEM;
  877. }
  878. drm_hdmi_ctx->ctx = (void *)ctx;
  879. platform_set_drvdata(pdev, drm_hdmi_ctx);
  880. /* acquire resources: regs, irqs, clocks */
  881. ret = mixer_resources_init(drm_hdmi_ctx, pdev);
  882. if (ret)
  883. goto fail;
  884. /* register specific callback point to common hdmi. */
  885. exynos_mixer_ops_register(&mixer_ops);
  886. mixer_resource_poweron(ctx);
  887. return 0;
  888. fail:
  889. dev_info(dev, "probe failed\n");
  890. return ret;
  891. }
  892. static int mixer_remove(struct platform_device *pdev)
  893. {
  894. struct device *dev = &pdev->dev;
  895. struct exynos_drm_hdmi_context *drm_hdmi_ctx =
  896. platform_get_drvdata(pdev);
  897. struct mixer_context *ctx = drm_hdmi_ctx->ctx;
  898. dev_info(dev, "remove successful\n");
  899. mixer_resource_poweroff(ctx);
  900. mixer_resources_cleanup(ctx);
  901. return 0;
  902. }
  903. struct platform_driver mixer_driver = {
  904. .driver = {
  905. .name = "s5p-mixer",
  906. .owner = THIS_MODULE,
  907. .pm = &mixer_pm_ops,
  908. },
  909. .probe = mixer_probe,
  910. .remove = __devexit_p(mixer_remove),
  911. };