exynos_mixer.c 28 KB

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