exynos_mixer.c 28 KB

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