exynos_mixer.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  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 get_mixer_context(dev) platform_get_drvdata(to_platform_device(dev))
  35. struct hdmi_win_data {
  36. dma_addr_t dma_addr;
  37. void __iomem *vaddr;
  38. dma_addr_t chroma_dma_addr;
  39. void __iomem *chroma_vaddr;
  40. uint32_t pixel_format;
  41. unsigned int bpp;
  42. unsigned int crtc_x;
  43. unsigned int crtc_y;
  44. unsigned int crtc_width;
  45. unsigned int crtc_height;
  46. unsigned int fb_x;
  47. unsigned int fb_y;
  48. unsigned int fb_width;
  49. unsigned int fb_height;
  50. unsigned int mode_width;
  51. unsigned int mode_height;
  52. unsigned int scan_flags;
  53. };
  54. struct mixer_resources {
  55. int irq;
  56. void __iomem *mixer_regs;
  57. void __iomem *vp_regs;
  58. spinlock_t reg_slock;
  59. struct clk *mixer;
  60. struct clk *vp;
  61. struct clk *sclk_mixer;
  62. struct clk *sclk_hdmi;
  63. struct clk *sclk_dac;
  64. };
  65. struct mixer_context {
  66. struct device *dev;
  67. int pipe;
  68. bool interlace;
  69. bool powered;
  70. u32 int_en;
  71. struct mutex mixer_mutex;
  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 void mixer_win_reset(struct mixer_context *ctx)
  506. {
  507. struct mixer_resources *res = &ctx->mixer_res;
  508. unsigned long flags;
  509. u32 val; /* value stored to register */
  510. spin_lock_irqsave(&res->reg_slock, flags);
  511. mixer_vsync_set_update(ctx, false);
  512. mixer_reg_writemask(res, MXR_CFG, MXR_CFG_DST_HDMI, MXR_CFG_DST_MASK);
  513. /* set output in RGB888 mode */
  514. mixer_reg_writemask(res, MXR_CFG, MXR_CFG_OUT_RGB888, MXR_CFG_OUT_MASK);
  515. /* 16 beat burst in DMA */
  516. mixer_reg_writemask(res, MXR_STATUS, MXR_STATUS_16_BURST,
  517. MXR_STATUS_BURST_MASK);
  518. /* setting default layer priority: layer1 > layer0 > video
  519. * because typical usage scenario would be
  520. * layer1 - OSD
  521. * layer0 - framebuffer
  522. * video - video overlay
  523. */
  524. val = MXR_LAYER_CFG_GRP1_VAL(3);
  525. val |= MXR_LAYER_CFG_GRP0_VAL(2);
  526. val |= MXR_LAYER_CFG_VP_VAL(1);
  527. mixer_reg_write(res, MXR_LAYER_CFG, val);
  528. /* setting background color */
  529. mixer_reg_write(res, MXR_BG_COLOR0, 0x008080);
  530. mixer_reg_write(res, MXR_BG_COLOR1, 0x008080);
  531. mixer_reg_write(res, MXR_BG_COLOR2, 0x008080);
  532. /* setting graphical layers */
  533. val = MXR_GRP_CFG_COLOR_KEY_DISABLE; /* no blank key */
  534. val |= MXR_GRP_CFG_WIN_BLEND_EN;
  535. val |= MXR_GRP_CFG_ALPHA_VAL(0xff); /* non-transparent alpha */
  536. /* the same configuration for both layers */
  537. mixer_reg_write(res, MXR_GRAPHIC_CFG(0), val);
  538. val |= MXR_GRP_CFG_BLEND_PRE_MUL;
  539. val |= MXR_GRP_CFG_PIXEL_BLEND_EN;
  540. mixer_reg_write(res, MXR_GRAPHIC_CFG(1), val);
  541. /* configuration of Video Processor Registers */
  542. vp_win_reset(ctx);
  543. vp_default_filter(res);
  544. /* disable all layers */
  545. mixer_reg_writemask(res, MXR_CFG, 0, MXR_CFG_GRP0_ENABLE);
  546. mixer_reg_writemask(res, MXR_CFG, 0, MXR_CFG_GRP1_ENABLE);
  547. mixer_reg_writemask(res, MXR_CFG, 0, MXR_CFG_VP_ENABLE);
  548. mixer_vsync_set_update(ctx, true);
  549. spin_unlock_irqrestore(&res->reg_slock, flags);
  550. }
  551. static void mixer_poweron(struct mixer_context *ctx)
  552. {
  553. struct mixer_resources *res = &ctx->mixer_res;
  554. DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
  555. mutex_lock(&ctx->mixer_mutex);
  556. if (ctx->powered) {
  557. mutex_unlock(&ctx->mixer_mutex);
  558. return;
  559. }
  560. ctx->powered = true;
  561. mutex_unlock(&ctx->mixer_mutex);
  562. pm_runtime_get_sync(ctx->dev);
  563. clk_enable(res->mixer);
  564. clk_enable(res->vp);
  565. clk_enable(res->sclk_mixer);
  566. mixer_reg_write(res, MXR_INT_EN, ctx->int_en);
  567. mixer_win_reset(ctx);
  568. }
  569. static void mixer_poweroff(struct mixer_context *ctx)
  570. {
  571. struct mixer_resources *res = &ctx->mixer_res;
  572. DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
  573. mutex_lock(&ctx->mixer_mutex);
  574. if (!ctx->powered)
  575. goto out;
  576. mutex_unlock(&ctx->mixer_mutex);
  577. ctx->int_en = mixer_reg_read(res, MXR_INT_EN);
  578. clk_disable(res->mixer);
  579. clk_disable(res->vp);
  580. clk_disable(res->sclk_mixer);
  581. pm_runtime_put_sync(ctx->dev);
  582. mutex_lock(&ctx->mixer_mutex);
  583. ctx->powered = false;
  584. out:
  585. mutex_unlock(&ctx->mixer_mutex);
  586. }
  587. static int mixer_enable_vblank(void *ctx, int pipe)
  588. {
  589. struct mixer_context *mixer_ctx = ctx;
  590. struct mixer_resources *res = &mixer_ctx->mixer_res;
  591. DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
  592. mixer_ctx->pipe = pipe;
  593. /* enable vsync interrupt */
  594. mixer_reg_writemask(res, MXR_INT_EN, MXR_INT_EN_VSYNC,
  595. MXR_INT_EN_VSYNC);
  596. return 0;
  597. }
  598. static void mixer_disable_vblank(void *ctx)
  599. {
  600. struct mixer_context *mixer_ctx = ctx;
  601. struct mixer_resources *res = &mixer_ctx->mixer_res;
  602. DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
  603. /* disable vsync interrupt */
  604. mixer_reg_writemask(res, MXR_INT_EN, 0, MXR_INT_EN_VSYNC);
  605. }
  606. static void mixer_dpms(void *ctx, int mode)
  607. {
  608. struct mixer_context *mixer_ctx = ctx;
  609. DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
  610. switch (mode) {
  611. case DRM_MODE_DPMS_ON:
  612. mixer_poweron(mixer_ctx);
  613. break;
  614. case DRM_MODE_DPMS_STANDBY:
  615. case DRM_MODE_DPMS_SUSPEND:
  616. case DRM_MODE_DPMS_OFF:
  617. mixer_poweroff(mixer_ctx);
  618. break;
  619. default:
  620. DRM_DEBUG_KMS("unknown dpms mode: %d\n", mode);
  621. break;
  622. }
  623. }
  624. static void mixer_win_mode_set(void *ctx,
  625. struct exynos_drm_overlay *overlay)
  626. {
  627. struct mixer_context *mixer_ctx = ctx;
  628. struct hdmi_win_data *win_data;
  629. int win;
  630. DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
  631. if (!overlay) {
  632. DRM_ERROR("overlay is NULL\n");
  633. return;
  634. }
  635. DRM_DEBUG_KMS("set [%d]x[%d] at (%d,%d) to [%d]x[%d] at (%d,%d)\n",
  636. overlay->fb_width, overlay->fb_height,
  637. overlay->fb_x, overlay->fb_y,
  638. overlay->crtc_width, overlay->crtc_height,
  639. overlay->crtc_x, overlay->crtc_y);
  640. win = overlay->zpos;
  641. if (win == DEFAULT_ZPOS)
  642. win = MIXER_DEFAULT_WIN;
  643. if (win < 0 || win > MIXER_WIN_NR) {
  644. DRM_ERROR("mixer window[%d] is wrong\n", win);
  645. return;
  646. }
  647. win_data = &mixer_ctx->win_data[win];
  648. win_data->dma_addr = overlay->dma_addr[0];
  649. win_data->vaddr = overlay->vaddr[0];
  650. win_data->chroma_dma_addr = overlay->dma_addr[1];
  651. win_data->chroma_vaddr = overlay->vaddr[1];
  652. win_data->pixel_format = overlay->pixel_format;
  653. win_data->bpp = overlay->bpp;
  654. win_data->crtc_x = overlay->crtc_x;
  655. win_data->crtc_y = overlay->crtc_y;
  656. win_data->crtc_width = overlay->crtc_width;
  657. win_data->crtc_height = overlay->crtc_height;
  658. win_data->fb_x = overlay->fb_x;
  659. win_data->fb_y = overlay->fb_y;
  660. win_data->fb_width = overlay->fb_width;
  661. win_data->fb_height = overlay->fb_height;
  662. win_data->mode_width = overlay->mode_width;
  663. win_data->mode_height = overlay->mode_height;
  664. win_data->scan_flags = overlay->scan_flag;
  665. }
  666. static void mixer_win_commit(void *ctx, int win)
  667. {
  668. struct mixer_context *mixer_ctx = ctx;
  669. DRM_DEBUG_KMS("[%d] %s, win: %d\n", __LINE__, __func__, win);
  670. if (win > 1)
  671. vp_video_buffer(mixer_ctx, win);
  672. else
  673. mixer_graph_buffer(mixer_ctx, win);
  674. }
  675. static void mixer_win_disable(void *ctx, int win)
  676. {
  677. struct mixer_context *mixer_ctx = ctx;
  678. struct mixer_resources *res = &mixer_ctx->mixer_res;
  679. unsigned long flags;
  680. DRM_DEBUG_KMS("[%d] %s, win: %d\n", __LINE__, __func__, win);
  681. spin_lock_irqsave(&res->reg_slock, flags);
  682. mixer_vsync_set_update(mixer_ctx, false);
  683. mixer_cfg_layer(mixer_ctx, win, false);
  684. mixer_vsync_set_update(mixer_ctx, true);
  685. spin_unlock_irqrestore(&res->reg_slock, flags);
  686. }
  687. static struct exynos_mixer_ops mixer_ops = {
  688. /* manager */
  689. .enable_vblank = mixer_enable_vblank,
  690. .disable_vblank = mixer_disable_vblank,
  691. .dpms = mixer_dpms,
  692. /* overlay */
  693. .win_mode_set = mixer_win_mode_set,
  694. .win_commit = mixer_win_commit,
  695. .win_disable = mixer_win_disable,
  696. };
  697. /* for pageflip event */
  698. static void mixer_finish_pageflip(struct drm_device *drm_dev, int crtc)
  699. {
  700. struct exynos_drm_private *dev_priv = drm_dev->dev_private;
  701. struct drm_pending_vblank_event *e, *t;
  702. struct timeval now;
  703. unsigned long flags;
  704. bool is_checked = false;
  705. spin_lock_irqsave(&drm_dev->event_lock, flags);
  706. list_for_each_entry_safe(e, t, &dev_priv->pageflip_event_list,
  707. base.link) {
  708. /* if event's pipe isn't same as crtc then ignore it. */
  709. if (crtc != e->pipe)
  710. continue;
  711. is_checked = true;
  712. do_gettimeofday(&now);
  713. e->event.sequence = 0;
  714. e->event.tv_sec = now.tv_sec;
  715. e->event.tv_usec = now.tv_usec;
  716. list_move_tail(&e->base.link, &e->base.file_priv->event_list);
  717. wake_up_interruptible(&e->base.file_priv->event_wait);
  718. }
  719. if (is_checked)
  720. /*
  721. * call drm_vblank_put only in case that drm_vblank_get was
  722. * called.
  723. */
  724. if (atomic_read(&drm_dev->vblank_refcount[crtc]) > 0)
  725. drm_vblank_put(drm_dev, crtc);
  726. spin_unlock_irqrestore(&drm_dev->event_lock, flags);
  727. }
  728. static irqreturn_t mixer_irq_handler(int irq, void *arg)
  729. {
  730. struct exynos_drm_hdmi_context *drm_hdmi_ctx = arg;
  731. struct mixer_context *ctx = drm_hdmi_ctx->ctx;
  732. struct mixer_resources *res = &ctx->mixer_res;
  733. u32 val, base, shadow;
  734. spin_lock(&res->reg_slock);
  735. /* read interrupt status for handling and clearing flags for VSYNC */
  736. val = mixer_reg_read(res, MXR_INT_STATUS);
  737. /* handling VSYNC */
  738. if (val & MXR_INT_STATUS_VSYNC) {
  739. /* interlace scan need to check shadow register */
  740. if (ctx->interlace) {
  741. base = mixer_reg_read(res, MXR_GRAPHIC_BASE(0));
  742. shadow = mixer_reg_read(res, MXR_GRAPHIC_BASE_S(0));
  743. if (base != shadow)
  744. goto out;
  745. base = mixer_reg_read(res, MXR_GRAPHIC_BASE(1));
  746. shadow = mixer_reg_read(res, MXR_GRAPHIC_BASE_S(1));
  747. if (base != shadow)
  748. goto out;
  749. }
  750. drm_handle_vblank(drm_hdmi_ctx->drm_dev, ctx->pipe);
  751. mixer_finish_pageflip(drm_hdmi_ctx->drm_dev, ctx->pipe);
  752. }
  753. out:
  754. /* clear interrupts */
  755. if (~val & MXR_INT_EN_VSYNC) {
  756. /* vsync interrupt use different bit for read and clear */
  757. val &= ~MXR_INT_EN_VSYNC;
  758. val |= MXR_INT_CLEAR_VSYNC;
  759. }
  760. mixer_reg_write(res, MXR_INT_STATUS, val);
  761. spin_unlock(&res->reg_slock);
  762. return IRQ_HANDLED;
  763. }
  764. static int __devinit mixer_resources_init(struct exynos_drm_hdmi_context *ctx,
  765. struct platform_device *pdev)
  766. {
  767. struct mixer_context *mixer_ctx = ctx->ctx;
  768. struct device *dev = &pdev->dev;
  769. struct mixer_resources *mixer_res = &mixer_ctx->mixer_res;
  770. struct resource *res;
  771. int ret;
  772. spin_lock_init(&mixer_res->reg_slock);
  773. mixer_res->mixer = clk_get(dev, "mixer");
  774. if (IS_ERR_OR_NULL(mixer_res->mixer)) {
  775. dev_err(dev, "failed to get clock 'mixer'\n");
  776. ret = -ENODEV;
  777. goto fail;
  778. }
  779. mixer_res->vp = clk_get(dev, "vp");
  780. if (IS_ERR_OR_NULL(mixer_res->vp)) {
  781. dev_err(dev, "failed to get clock 'vp'\n");
  782. ret = -ENODEV;
  783. goto fail;
  784. }
  785. mixer_res->sclk_mixer = clk_get(dev, "sclk_mixer");
  786. if (IS_ERR_OR_NULL(mixer_res->sclk_mixer)) {
  787. dev_err(dev, "failed to get clock 'sclk_mixer'\n");
  788. ret = -ENODEV;
  789. goto fail;
  790. }
  791. mixer_res->sclk_hdmi = clk_get(dev, "sclk_hdmi");
  792. if (IS_ERR_OR_NULL(mixer_res->sclk_hdmi)) {
  793. dev_err(dev, "failed to get clock 'sclk_hdmi'\n");
  794. ret = -ENODEV;
  795. goto fail;
  796. }
  797. mixer_res->sclk_dac = clk_get(dev, "sclk_dac");
  798. if (IS_ERR_OR_NULL(mixer_res->sclk_dac)) {
  799. dev_err(dev, "failed to get clock 'sclk_dac'\n");
  800. ret = -ENODEV;
  801. goto fail;
  802. }
  803. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mxr");
  804. if (res == NULL) {
  805. dev_err(dev, "get memory resource failed.\n");
  806. ret = -ENXIO;
  807. goto fail;
  808. }
  809. clk_set_parent(mixer_res->sclk_mixer, mixer_res->sclk_hdmi);
  810. mixer_res->mixer_regs = ioremap(res->start, resource_size(res));
  811. if (mixer_res->mixer_regs == NULL) {
  812. dev_err(dev, "register mapping failed.\n");
  813. ret = -ENXIO;
  814. goto fail;
  815. }
  816. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "vp");
  817. if (res == NULL) {
  818. dev_err(dev, "get memory resource failed.\n");
  819. ret = -ENXIO;
  820. goto fail_mixer_regs;
  821. }
  822. mixer_res->vp_regs = ioremap(res->start, resource_size(res));
  823. if (mixer_res->vp_regs == NULL) {
  824. dev_err(dev, "register mapping failed.\n");
  825. ret = -ENXIO;
  826. goto fail_mixer_regs;
  827. }
  828. res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "irq");
  829. if (res == NULL) {
  830. dev_err(dev, "get interrupt resource failed.\n");
  831. ret = -ENXIO;
  832. goto fail_vp_regs;
  833. }
  834. ret = request_irq(res->start, mixer_irq_handler, 0, "drm_mixer", ctx);
  835. if (ret) {
  836. dev_err(dev, "request interrupt failed.\n");
  837. goto fail_vp_regs;
  838. }
  839. mixer_res->irq = res->start;
  840. return 0;
  841. fail_vp_regs:
  842. iounmap(mixer_res->vp_regs);
  843. fail_mixer_regs:
  844. iounmap(mixer_res->mixer_regs);
  845. fail:
  846. if (!IS_ERR_OR_NULL(mixer_res->sclk_dac))
  847. clk_put(mixer_res->sclk_dac);
  848. if (!IS_ERR_OR_NULL(mixer_res->sclk_hdmi))
  849. clk_put(mixer_res->sclk_hdmi);
  850. if (!IS_ERR_OR_NULL(mixer_res->sclk_mixer))
  851. clk_put(mixer_res->sclk_mixer);
  852. if (!IS_ERR_OR_NULL(mixer_res->vp))
  853. clk_put(mixer_res->vp);
  854. if (!IS_ERR_OR_NULL(mixer_res->mixer))
  855. clk_put(mixer_res->mixer);
  856. return ret;
  857. }
  858. static void mixer_resources_cleanup(struct mixer_context *ctx)
  859. {
  860. struct mixer_resources *res = &ctx->mixer_res;
  861. free_irq(res->irq, ctx);
  862. iounmap(res->vp_regs);
  863. iounmap(res->mixer_regs);
  864. }
  865. static int __devinit mixer_probe(struct platform_device *pdev)
  866. {
  867. struct device *dev = &pdev->dev;
  868. struct exynos_drm_hdmi_context *drm_hdmi_ctx;
  869. struct mixer_context *ctx;
  870. int ret;
  871. dev_info(dev, "probe start\n");
  872. drm_hdmi_ctx = kzalloc(sizeof(*drm_hdmi_ctx), GFP_KERNEL);
  873. if (!drm_hdmi_ctx) {
  874. DRM_ERROR("failed to allocate common hdmi context.\n");
  875. return -ENOMEM;
  876. }
  877. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  878. if (!ctx) {
  879. DRM_ERROR("failed to alloc mixer context.\n");
  880. kfree(drm_hdmi_ctx);
  881. return -ENOMEM;
  882. }
  883. mutex_init(&ctx->mixer_mutex);
  884. ctx->dev = &pdev->dev;
  885. drm_hdmi_ctx->ctx = (void *)ctx;
  886. platform_set_drvdata(pdev, drm_hdmi_ctx);
  887. /* acquire resources: regs, irqs, clocks */
  888. ret = mixer_resources_init(drm_hdmi_ctx, pdev);
  889. if (ret)
  890. goto fail;
  891. /* register specific callback point to common hdmi. */
  892. exynos_mixer_ops_register(&mixer_ops);
  893. pm_runtime_enable(dev);
  894. return 0;
  895. fail:
  896. dev_info(dev, "probe failed\n");
  897. return ret;
  898. }
  899. static int mixer_remove(struct platform_device *pdev)
  900. {
  901. struct device *dev = &pdev->dev;
  902. struct exynos_drm_hdmi_context *drm_hdmi_ctx =
  903. platform_get_drvdata(pdev);
  904. struct mixer_context *ctx = drm_hdmi_ctx->ctx;
  905. dev_info(dev, "remove successful\n");
  906. pm_runtime_disable(&pdev->dev);
  907. mixer_resources_cleanup(ctx);
  908. return 0;
  909. }
  910. #ifdef CONFIG_PM_SLEEP
  911. static int mixer_suspend(struct device *dev)
  912. {
  913. struct exynos_drm_hdmi_context *drm_hdmi_ctx = get_mixer_context(dev);
  914. struct mixer_context *ctx = drm_hdmi_ctx->ctx;
  915. mixer_poweroff(ctx);
  916. return 0;
  917. }
  918. #endif
  919. static SIMPLE_DEV_PM_OPS(mixer_pm_ops, mixer_suspend, NULL);
  920. struct platform_driver mixer_driver = {
  921. .driver = {
  922. .name = "s5p-mixer",
  923. .owner = THIS_MODULE,
  924. .pm = &mixer_pm_ops,
  925. },
  926. .probe = mixer_probe,
  927. .remove = __devexit_p(mixer_remove),
  928. };