exynos_mixer.c 28 KB

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