exynos_mixer.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  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_wait_for_vblank(void *ctx)
  607. {
  608. struct mixer_context *mixer_ctx = ctx;
  609. struct mixer_resources *res = &mixer_ctx->mixer_res;
  610. int ret;
  611. ret = wait_for((mixer_reg_read(res, MXR_INT_STATUS) &
  612. MXR_INT_STATUS_VSYNC), 50);
  613. if (ret < 0)
  614. DRM_DEBUG_KMS("vblank wait timed out.\n");
  615. }
  616. static void mixer_win_mode_set(void *ctx,
  617. struct exynos_drm_overlay *overlay)
  618. {
  619. struct mixer_context *mixer_ctx = ctx;
  620. struct hdmi_win_data *win_data;
  621. int win;
  622. DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
  623. if (!overlay) {
  624. DRM_ERROR("overlay is NULL\n");
  625. return;
  626. }
  627. DRM_DEBUG_KMS("set [%d]x[%d] at (%d,%d) to [%d]x[%d] at (%d,%d)\n",
  628. overlay->fb_width, overlay->fb_height,
  629. overlay->fb_x, overlay->fb_y,
  630. overlay->crtc_width, overlay->crtc_height,
  631. overlay->crtc_x, overlay->crtc_y);
  632. win = overlay->zpos;
  633. if (win == DEFAULT_ZPOS)
  634. win = MIXER_DEFAULT_WIN;
  635. if (win < 0 || win > MIXER_WIN_NR) {
  636. DRM_ERROR("mixer window[%d] is wrong\n", win);
  637. return;
  638. }
  639. win_data = &mixer_ctx->win_data[win];
  640. win_data->dma_addr = overlay->dma_addr[0];
  641. win_data->vaddr = overlay->vaddr[0];
  642. win_data->chroma_dma_addr = overlay->dma_addr[1];
  643. win_data->chroma_vaddr = overlay->vaddr[1];
  644. win_data->pixel_format = overlay->pixel_format;
  645. win_data->bpp = overlay->bpp;
  646. win_data->crtc_x = overlay->crtc_x;
  647. win_data->crtc_y = overlay->crtc_y;
  648. win_data->crtc_width = overlay->crtc_width;
  649. win_data->crtc_height = overlay->crtc_height;
  650. win_data->fb_x = overlay->fb_x;
  651. win_data->fb_y = overlay->fb_y;
  652. win_data->fb_width = overlay->fb_width;
  653. win_data->fb_height = overlay->fb_height;
  654. win_data->src_width = overlay->src_width;
  655. win_data->src_height = overlay->src_height;
  656. win_data->mode_width = overlay->mode_width;
  657. win_data->mode_height = overlay->mode_height;
  658. win_data->scan_flags = overlay->scan_flag;
  659. }
  660. static void mixer_win_commit(void *ctx, int win)
  661. {
  662. struct mixer_context *mixer_ctx = ctx;
  663. DRM_DEBUG_KMS("[%d] %s, win: %d\n", __LINE__, __func__, win);
  664. if (win > 1)
  665. vp_video_buffer(mixer_ctx, win);
  666. else
  667. mixer_graph_buffer(mixer_ctx, win);
  668. }
  669. static void mixer_win_disable(void *ctx, int win)
  670. {
  671. struct mixer_context *mixer_ctx = ctx;
  672. struct mixer_resources *res = &mixer_ctx->mixer_res;
  673. unsigned long flags;
  674. DRM_DEBUG_KMS("[%d] %s, win: %d\n", __LINE__, __func__, win);
  675. spin_lock_irqsave(&res->reg_slock, flags);
  676. mixer_vsync_set_update(mixer_ctx, false);
  677. mixer_cfg_layer(mixer_ctx, win, false);
  678. mixer_vsync_set_update(mixer_ctx, true);
  679. spin_unlock_irqrestore(&res->reg_slock, flags);
  680. }
  681. static struct exynos_mixer_ops mixer_ops = {
  682. /* manager */
  683. .enable_vblank = mixer_enable_vblank,
  684. .disable_vblank = mixer_disable_vblank,
  685. .dpms = mixer_dpms,
  686. /* overlay */
  687. .wait_for_vblank = mixer_wait_for_vblank,
  688. .win_mode_set = mixer_win_mode_set,
  689. .win_commit = mixer_win_commit,
  690. .win_disable = mixer_win_disable,
  691. };
  692. /* for pageflip event */
  693. static void mixer_finish_pageflip(struct drm_device *drm_dev, int crtc)
  694. {
  695. struct exynos_drm_private *dev_priv = drm_dev->dev_private;
  696. struct drm_pending_vblank_event *e, *t;
  697. struct timeval now;
  698. unsigned long flags;
  699. bool is_checked = false;
  700. spin_lock_irqsave(&drm_dev->event_lock, flags);
  701. list_for_each_entry_safe(e, t, &dev_priv->pageflip_event_list,
  702. base.link) {
  703. /* if event's pipe isn't same as crtc then ignore it. */
  704. if (crtc != e->pipe)
  705. continue;
  706. is_checked = true;
  707. do_gettimeofday(&now);
  708. e->event.sequence = 0;
  709. e->event.tv_sec = now.tv_sec;
  710. e->event.tv_usec = now.tv_usec;
  711. list_move_tail(&e->base.link, &e->base.file_priv->event_list);
  712. wake_up_interruptible(&e->base.file_priv->event_wait);
  713. }
  714. if (is_checked)
  715. /*
  716. * call drm_vblank_put only in case that drm_vblank_get was
  717. * called.
  718. */
  719. if (atomic_read(&drm_dev->vblank_refcount[crtc]) > 0)
  720. drm_vblank_put(drm_dev, crtc);
  721. spin_unlock_irqrestore(&drm_dev->event_lock, flags);
  722. }
  723. static irqreturn_t mixer_irq_handler(int irq, void *arg)
  724. {
  725. struct exynos_drm_hdmi_context *drm_hdmi_ctx = arg;
  726. struct mixer_context *ctx = drm_hdmi_ctx->ctx;
  727. struct mixer_resources *res = &ctx->mixer_res;
  728. u32 val, base, shadow;
  729. spin_lock(&res->reg_slock);
  730. /* read interrupt status for handling and clearing flags for VSYNC */
  731. val = mixer_reg_read(res, MXR_INT_STATUS);
  732. /* handling VSYNC */
  733. if (val & MXR_INT_STATUS_VSYNC) {
  734. /* interlace scan need to check shadow register */
  735. if (ctx->interlace) {
  736. base = mixer_reg_read(res, MXR_GRAPHIC_BASE(0));
  737. shadow = mixer_reg_read(res, MXR_GRAPHIC_BASE_S(0));
  738. if (base != shadow)
  739. goto out;
  740. base = mixer_reg_read(res, MXR_GRAPHIC_BASE(1));
  741. shadow = mixer_reg_read(res, MXR_GRAPHIC_BASE_S(1));
  742. if (base != shadow)
  743. goto out;
  744. }
  745. drm_handle_vblank(drm_hdmi_ctx->drm_dev, ctx->pipe);
  746. mixer_finish_pageflip(drm_hdmi_ctx->drm_dev, ctx->pipe);
  747. }
  748. out:
  749. /* clear interrupts */
  750. if (~val & MXR_INT_EN_VSYNC) {
  751. /* vsync interrupt use different bit for read and clear */
  752. val &= ~MXR_INT_EN_VSYNC;
  753. val |= MXR_INT_CLEAR_VSYNC;
  754. }
  755. mixer_reg_write(res, MXR_INT_STATUS, val);
  756. spin_unlock(&res->reg_slock);
  757. return IRQ_HANDLED;
  758. }
  759. static int __devinit mixer_resources_init(struct exynos_drm_hdmi_context *ctx,
  760. struct platform_device *pdev)
  761. {
  762. struct mixer_context *mixer_ctx = 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. spin_lock_init(&mixer_res->reg_slock);
  768. mixer_res->mixer = clk_get(dev, "mixer");
  769. if (IS_ERR_OR_NULL(mixer_res->mixer)) {
  770. dev_err(dev, "failed to get clock 'mixer'\n");
  771. ret = -ENODEV;
  772. goto fail;
  773. }
  774. mixer_res->vp = clk_get(dev, "vp");
  775. if (IS_ERR_OR_NULL(mixer_res->vp)) {
  776. dev_err(dev, "failed to get clock 'vp'\n");
  777. ret = -ENODEV;
  778. goto fail;
  779. }
  780. mixer_res->sclk_mixer = clk_get(dev, "sclk_mixer");
  781. if (IS_ERR_OR_NULL(mixer_res->sclk_mixer)) {
  782. dev_err(dev, "failed to get clock 'sclk_mixer'\n");
  783. ret = -ENODEV;
  784. goto fail;
  785. }
  786. mixer_res->sclk_hdmi = clk_get(dev, "sclk_hdmi");
  787. if (IS_ERR_OR_NULL(mixer_res->sclk_hdmi)) {
  788. dev_err(dev, "failed to get clock 'sclk_hdmi'\n");
  789. ret = -ENODEV;
  790. goto fail;
  791. }
  792. mixer_res->sclk_dac = clk_get(dev, "sclk_dac");
  793. if (IS_ERR_OR_NULL(mixer_res->sclk_dac)) {
  794. dev_err(dev, "failed to get clock 'sclk_dac'\n");
  795. ret = -ENODEV;
  796. goto fail;
  797. }
  798. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mxr");
  799. if (res == NULL) {
  800. dev_err(dev, "get memory resource failed.\n");
  801. ret = -ENXIO;
  802. goto fail;
  803. }
  804. clk_set_parent(mixer_res->sclk_mixer, mixer_res->sclk_hdmi);
  805. mixer_res->mixer_regs = devm_ioremap(&pdev->dev, res->start,
  806. 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;
  817. }
  818. mixer_res->vp_regs = devm_ioremap(&pdev->dev, res->start,
  819. resource_size(res));
  820. if (mixer_res->vp_regs == NULL) {
  821. dev_err(dev, "register mapping failed.\n");
  822. ret = -ENXIO;
  823. goto fail;
  824. }
  825. res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "irq");
  826. if (res == NULL) {
  827. dev_err(dev, "get interrupt resource failed.\n");
  828. ret = -ENXIO;
  829. goto fail;
  830. }
  831. ret = devm_request_irq(&pdev->dev, res->start, mixer_irq_handler,
  832. 0, "drm_mixer", ctx);
  833. if (ret) {
  834. dev_err(dev, "request interrupt failed.\n");
  835. goto fail;
  836. }
  837. mixer_res->irq = res->start;
  838. return 0;
  839. fail:
  840. if (!IS_ERR_OR_NULL(mixer_res->sclk_dac))
  841. clk_put(mixer_res->sclk_dac);
  842. if (!IS_ERR_OR_NULL(mixer_res->sclk_hdmi))
  843. clk_put(mixer_res->sclk_hdmi);
  844. if (!IS_ERR_OR_NULL(mixer_res->sclk_mixer))
  845. clk_put(mixer_res->sclk_mixer);
  846. if (!IS_ERR_OR_NULL(mixer_res->vp))
  847. clk_put(mixer_res->vp);
  848. if (!IS_ERR_OR_NULL(mixer_res->mixer))
  849. clk_put(mixer_res->mixer);
  850. return ret;
  851. }
  852. static int __devinit mixer_probe(struct platform_device *pdev)
  853. {
  854. struct device *dev = &pdev->dev;
  855. struct exynos_drm_hdmi_context *drm_hdmi_ctx;
  856. struct mixer_context *ctx;
  857. int ret;
  858. dev_info(dev, "probe start\n");
  859. drm_hdmi_ctx = devm_kzalloc(&pdev->dev, sizeof(*drm_hdmi_ctx),
  860. GFP_KERNEL);
  861. if (!drm_hdmi_ctx) {
  862. DRM_ERROR("failed to allocate common hdmi context.\n");
  863. return -ENOMEM;
  864. }
  865. ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
  866. if (!ctx) {
  867. DRM_ERROR("failed to alloc mixer context.\n");
  868. return -ENOMEM;
  869. }
  870. mutex_init(&ctx->mixer_mutex);
  871. ctx->dev = &pdev->dev;
  872. drm_hdmi_ctx->ctx = (void *)ctx;
  873. platform_set_drvdata(pdev, drm_hdmi_ctx);
  874. /* acquire resources: regs, irqs, clocks */
  875. ret = mixer_resources_init(drm_hdmi_ctx, pdev);
  876. if (ret)
  877. goto fail;
  878. /* register specific callback point to common hdmi. */
  879. exynos_mixer_ops_register(&mixer_ops);
  880. pm_runtime_enable(dev);
  881. return 0;
  882. fail:
  883. dev_info(dev, "probe failed\n");
  884. return ret;
  885. }
  886. static int mixer_remove(struct platform_device *pdev)
  887. {
  888. dev_info(&pdev->dev, "remove successful\n");
  889. pm_runtime_disable(&pdev->dev);
  890. return 0;
  891. }
  892. #ifdef CONFIG_PM_SLEEP
  893. static int mixer_suspend(struct device *dev)
  894. {
  895. struct exynos_drm_hdmi_context *drm_hdmi_ctx = get_mixer_context(dev);
  896. struct mixer_context *ctx = drm_hdmi_ctx->ctx;
  897. mixer_poweroff(ctx);
  898. return 0;
  899. }
  900. #endif
  901. static SIMPLE_DEV_PM_OPS(mixer_pm_ops, mixer_suspend, NULL);
  902. struct platform_driver mixer_driver = {
  903. .driver = {
  904. .name = "s5p-mixer",
  905. .owner = THIS_MODULE,
  906. .pm = &mixer_pm_ops,
  907. },
  908. .probe = mixer_probe,
  909. .remove = __devexit_p(mixer_remove),
  910. };