exynos_mixer.c 28 KB

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