exynos_mixer.c 28 KB

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