exynos_mixer.c 27 KB

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