exynos_mixer.c 28 KB

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