exynos_mixer.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  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_NV12M:
  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_ALPHA_VAL(0xff); /* non-transparent alpha */
  515. /* the same configuration for both layers */
  516. mixer_reg_write(res, MXR_GRAPHIC_CFG(0), val);
  517. val |= MXR_GRP_CFG_BLEND_PRE_MUL;
  518. val |= MXR_GRP_CFG_PIXEL_BLEND_EN;
  519. mixer_reg_write(res, MXR_GRAPHIC_CFG(1), val);
  520. /* configuration of Video Processor Registers */
  521. vp_win_reset(ctx);
  522. vp_default_filter(res);
  523. /* disable all layers */
  524. mixer_reg_writemask(res, MXR_CFG, 0, MXR_CFG_GRP0_ENABLE);
  525. mixer_reg_writemask(res, MXR_CFG, 0, MXR_CFG_GRP1_ENABLE);
  526. mixer_reg_writemask(res, MXR_CFG, 0, MXR_CFG_VP_ENABLE);
  527. mixer_vsync_set_update(ctx, true);
  528. spin_unlock_irqrestore(&res->reg_slock, flags);
  529. }
  530. static void mixer_poweron(struct mixer_context *ctx)
  531. {
  532. struct mixer_resources *res = &ctx->mixer_res;
  533. DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
  534. mutex_lock(&ctx->mixer_mutex);
  535. if (ctx->powered) {
  536. mutex_unlock(&ctx->mixer_mutex);
  537. return;
  538. }
  539. ctx->powered = true;
  540. mutex_unlock(&ctx->mixer_mutex);
  541. pm_runtime_get_sync(ctx->dev);
  542. clk_enable(res->mixer);
  543. clk_enable(res->vp);
  544. clk_enable(res->sclk_mixer);
  545. mixer_reg_write(res, MXR_INT_EN, ctx->int_en);
  546. mixer_win_reset(ctx);
  547. }
  548. static void mixer_poweroff(struct mixer_context *ctx)
  549. {
  550. struct mixer_resources *res = &ctx->mixer_res;
  551. DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
  552. mutex_lock(&ctx->mixer_mutex);
  553. if (!ctx->powered)
  554. goto out;
  555. mutex_unlock(&ctx->mixer_mutex);
  556. ctx->int_en = mixer_reg_read(res, MXR_INT_EN);
  557. clk_disable(res->mixer);
  558. clk_disable(res->vp);
  559. clk_disable(res->sclk_mixer);
  560. pm_runtime_put_sync(ctx->dev);
  561. mutex_lock(&ctx->mixer_mutex);
  562. ctx->powered = false;
  563. out:
  564. mutex_unlock(&ctx->mixer_mutex);
  565. }
  566. static int mixer_enable_vblank(void *ctx, int pipe)
  567. {
  568. struct mixer_context *mixer_ctx = ctx;
  569. struct mixer_resources *res = &mixer_ctx->mixer_res;
  570. DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
  571. mixer_ctx->pipe = pipe;
  572. /* enable vsync interrupt */
  573. mixer_reg_writemask(res, MXR_INT_EN, MXR_INT_EN_VSYNC,
  574. MXR_INT_EN_VSYNC);
  575. return 0;
  576. }
  577. static void mixer_disable_vblank(void *ctx)
  578. {
  579. struct mixer_context *mixer_ctx = ctx;
  580. struct mixer_resources *res = &mixer_ctx->mixer_res;
  581. DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
  582. /* disable vsync interrupt */
  583. mixer_reg_writemask(res, MXR_INT_EN, 0, MXR_INT_EN_VSYNC);
  584. }
  585. static void mixer_dpms(void *ctx, int mode)
  586. {
  587. struct mixer_context *mixer_ctx = ctx;
  588. DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
  589. switch (mode) {
  590. case DRM_MODE_DPMS_ON:
  591. mixer_poweron(mixer_ctx);
  592. break;
  593. case DRM_MODE_DPMS_STANDBY:
  594. case DRM_MODE_DPMS_SUSPEND:
  595. case DRM_MODE_DPMS_OFF:
  596. mixer_poweroff(mixer_ctx);
  597. break;
  598. default:
  599. DRM_DEBUG_KMS("unknown dpms mode: %d\n", mode);
  600. break;
  601. }
  602. }
  603. static void mixer_win_mode_set(void *ctx,
  604. struct exynos_drm_overlay *overlay)
  605. {
  606. struct mixer_context *mixer_ctx = ctx;
  607. struct hdmi_win_data *win_data;
  608. int win;
  609. DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
  610. if (!overlay) {
  611. DRM_ERROR("overlay is NULL\n");
  612. return;
  613. }
  614. DRM_DEBUG_KMS("set [%d]x[%d] at (%d,%d) to [%d]x[%d] at (%d,%d)\n",
  615. overlay->fb_width, overlay->fb_height,
  616. overlay->fb_x, overlay->fb_y,
  617. overlay->crtc_width, overlay->crtc_height,
  618. overlay->crtc_x, overlay->crtc_y);
  619. win = overlay->zpos;
  620. if (win == DEFAULT_ZPOS)
  621. win = MIXER_DEFAULT_WIN;
  622. if (win < 0 || win > MIXER_WIN_NR) {
  623. DRM_ERROR("mixer window[%d] is wrong\n", win);
  624. return;
  625. }
  626. win_data = &mixer_ctx->win_data[win];
  627. win_data->dma_addr = overlay->dma_addr[0];
  628. win_data->vaddr = overlay->vaddr[0];
  629. win_data->chroma_dma_addr = overlay->dma_addr[1];
  630. win_data->chroma_vaddr = overlay->vaddr[1];
  631. win_data->pixel_format = overlay->pixel_format;
  632. win_data->bpp = overlay->bpp;
  633. win_data->crtc_x = overlay->crtc_x;
  634. win_data->crtc_y = overlay->crtc_y;
  635. win_data->crtc_width = overlay->crtc_width;
  636. win_data->crtc_height = overlay->crtc_height;
  637. win_data->fb_x = overlay->fb_x;
  638. win_data->fb_y = overlay->fb_y;
  639. win_data->fb_width = overlay->fb_width;
  640. win_data->fb_height = overlay->fb_height;
  641. win_data->src_width = overlay->src_width;
  642. win_data->src_height = overlay->src_height;
  643. win_data->mode_width = overlay->mode_width;
  644. win_data->mode_height = overlay->mode_height;
  645. win_data->scan_flags = overlay->scan_flag;
  646. }
  647. static void mixer_win_commit(void *ctx, int win)
  648. {
  649. struct mixer_context *mixer_ctx = ctx;
  650. DRM_DEBUG_KMS("[%d] %s, win: %d\n", __LINE__, __func__, win);
  651. if (win > 1)
  652. vp_video_buffer(mixer_ctx, win);
  653. else
  654. mixer_graph_buffer(mixer_ctx, win);
  655. }
  656. static void mixer_win_disable(void *ctx, int win)
  657. {
  658. struct mixer_context *mixer_ctx = ctx;
  659. struct mixer_resources *res = &mixer_ctx->mixer_res;
  660. unsigned long flags;
  661. DRM_DEBUG_KMS("[%d] %s, win: %d\n", __LINE__, __func__, win);
  662. spin_lock_irqsave(&res->reg_slock, flags);
  663. mixer_vsync_set_update(mixer_ctx, false);
  664. mixer_cfg_layer(mixer_ctx, win, false);
  665. mixer_vsync_set_update(mixer_ctx, true);
  666. spin_unlock_irqrestore(&res->reg_slock, flags);
  667. }
  668. static struct exynos_mixer_ops mixer_ops = {
  669. /* manager */
  670. .enable_vblank = mixer_enable_vblank,
  671. .disable_vblank = mixer_disable_vblank,
  672. .dpms = mixer_dpms,
  673. /* overlay */
  674. .win_mode_set = mixer_win_mode_set,
  675. .win_commit = mixer_win_commit,
  676. .win_disable = mixer_win_disable,
  677. };
  678. /* for pageflip event */
  679. static void mixer_finish_pageflip(struct drm_device *drm_dev, int crtc)
  680. {
  681. struct exynos_drm_private *dev_priv = drm_dev->dev_private;
  682. struct drm_pending_vblank_event *e, *t;
  683. struct timeval now;
  684. unsigned long flags;
  685. bool is_checked = false;
  686. spin_lock_irqsave(&drm_dev->event_lock, flags);
  687. list_for_each_entry_safe(e, t, &dev_priv->pageflip_event_list,
  688. base.link) {
  689. /* if event's pipe isn't same as crtc then ignore it. */
  690. if (crtc != e->pipe)
  691. continue;
  692. is_checked = true;
  693. do_gettimeofday(&now);
  694. e->event.sequence = 0;
  695. e->event.tv_sec = now.tv_sec;
  696. e->event.tv_usec = now.tv_usec;
  697. list_move_tail(&e->base.link, &e->base.file_priv->event_list);
  698. wake_up_interruptible(&e->base.file_priv->event_wait);
  699. }
  700. if (is_checked)
  701. /*
  702. * call drm_vblank_put only in case that drm_vblank_get was
  703. * called.
  704. */
  705. if (atomic_read(&drm_dev->vblank_refcount[crtc]) > 0)
  706. drm_vblank_put(drm_dev, crtc);
  707. spin_unlock_irqrestore(&drm_dev->event_lock, flags);
  708. }
  709. static irqreturn_t mixer_irq_handler(int irq, void *arg)
  710. {
  711. struct exynos_drm_hdmi_context *drm_hdmi_ctx = arg;
  712. struct mixer_context *ctx = drm_hdmi_ctx->ctx;
  713. struct mixer_resources *res = &ctx->mixer_res;
  714. u32 val, base, shadow;
  715. spin_lock(&res->reg_slock);
  716. /* read interrupt status for handling and clearing flags for VSYNC */
  717. val = mixer_reg_read(res, MXR_INT_STATUS);
  718. /* handling VSYNC */
  719. if (val & MXR_INT_STATUS_VSYNC) {
  720. /* interlace scan need to check shadow register */
  721. if (ctx->interlace) {
  722. base = mixer_reg_read(res, MXR_GRAPHIC_BASE(0));
  723. shadow = mixer_reg_read(res, MXR_GRAPHIC_BASE_S(0));
  724. if (base != shadow)
  725. goto out;
  726. base = mixer_reg_read(res, MXR_GRAPHIC_BASE(1));
  727. shadow = mixer_reg_read(res, MXR_GRAPHIC_BASE_S(1));
  728. if (base != shadow)
  729. goto out;
  730. }
  731. drm_handle_vblank(drm_hdmi_ctx->drm_dev, ctx->pipe);
  732. mixer_finish_pageflip(drm_hdmi_ctx->drm_dev, ctx->pipe);
  733. }
  734. out:
  735. /* clear interrupts */
  736. if (~val & MXR_INT_EN_VSYNC) {
  737. /* vsync interrupt use different bit for read and clear */
  738. val &= ~MXR_INT_EN_VSYNC;
  739. val |= MXR_INT_CLEAR_VSYNC;
  740. }
  741. mixer_reg_write(res, MXR_INT_STATUS, val);
  742. spin_unlock(&res->reg_slock);
  743. return IRQ_HANDLED;
  744. }
  745. static int __devinit mixer_resources_init(struct exynos_drm_hdmi_context *ctx,
  746. struct platform_device *pdev)
  747. {
  748. struct mixer_context *mixer_ctx = ctx->ctx;
  749. struct device *dev = &pdev->dev;
  750. struct mixer_resources *mixer_res = &mixer_ctx->mixer_res;
  751. struct resource *res;
  752. int ret;
  753. spin_lock_init(&mixer_res->reg_slock);
  754. mixer_res->mixer = clk_get(dev, "mixer");
  755. if (IS_ERR_OR_NULL(mixer_res->mixer)) {
  756. dev_err(dev, "failed to get clock 'mixer'\n");
  757. ret = -ENODEV;
  758. goto fail;
  759. }
  760. mixer_res->vp = clk_get(dev, "vp");
  761. if (IS_ERR_OR_NULL(mixer_res->vp)) {
  762. dev_err(dev, "failed to get clock 'vp'\n");
  763. ret = -ENODEV;
  764. goto fail;
  765. }
  766. mixer_res->sclk_mixer = clk_get(dev, "sclk_mixer");
  767. if (IS_ERR_OR_NULL(mixer_res->sclk_mixer)) {
  768. dev_err(dev, "failed to get clock 'sclk_mixer'\n");
  769. ret = -ENODEV;
  770. goto fail;
  771. }
  772. mixer_res->sclk_hdmi = clk_get(dev, "sclk_hdmi");
  773. if (IS_ERR_OR_NULL(mixer_res->sclk_hdmi)) {
  774. dev_err(dev, "failed to get clock 'sclk_hdmi'\n");
  775. ret = -ENODEV;
  776. goto fail;
  777. }
  778. mixer_res->sclk_dac = clk_get(dev, "sclk_dac");
  779. if (IS_ERR_OR_NULL(mixer_res->sclk_dac)) {
  780. dev_err(dev, "failed to get clock 'sclk_dac'\n");
  781. ret = -ENODEV;
  782. goto fail;
  783. }
  784. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mxr");
  785. if (res == NULL) {
  786. dev_err(dev, "get memory resource failed.\n");
  787. ret = -ENXIO;
  788. goto fail;
  789. }
  790. clk_set_parent(mixer_res->sclk_mixer, mixer_res->sclk_hdmi);
  791. mixer_res->mixer_regs = ioremap(res->start, resource_size(res));
  792. if (mixer_res->mixer_regs == NULL) {
  793. dev_err(dev, "register mapping failed.\n");
  794. ret = -ENXIO;
  795. goto fail;
  796. }
  797. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "vp");
  798. if (res == NULL) {
  799. dev_err(dev, "get memory resource failed.\n");
  800. ret = -ENXIO;
  801. goto fail_mixer_regs;
  802. }
  803. mixer_res->vp_regs = ioremap(res->start, resource_size(res));
  804. if (mixer_res->vp_regs == NULL) {
  805. dev_err(dev, "register mapping failed.\n");
  806. ret = -ENXIO;
  807. goto fail_mixer_regs;
  808. }
  809. res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "irq");
  810. if (res == NULL) {
  811. dev_err(dev, "get interrupt resource failed.\n");
  812. ret = -ENXIO;
  813. goto fail_vp_regs;
  814. }
  815. ret = request_irq(res->start, mixer_irq_handler, 0, "drm_mixer", ctx);
  816. if (ret) {
  817. dev_err(dev, "request interrupt failed.\n");
  818. goto fail_vp_regs;
  819. }
  820. mixer_res->irq = res->start;
  821. return 0;
  822. fail_vp_regs:
  823. iounmap(mixer_res->vp_regs);
  824. fail_mixer_regs:
  825. iounmap(mixer_res->mixer_regs);
  826. fail:
  827. if (!IS_ERR_OR_NULL(mixer_res->sclk_dac))
  828. clk_put(mixer_res->sclk_dac);
  829. if (!IS_ERR_OR_NULL(mixer_res->sclk_hdmi))
  830. clk_put(mixer_res->sclk_hdmi);
  831. if (!IS_ERR_OR_NULL(mixer_res->sclk_mixer))
  832. clk_put(mixer_res->sclk_mixer);
  833. if (!IS_ERR_OR_NULL(mixer_res->vp))
  834. clk_put(mixer_res->vp);
  835. if (!IS_ERR_OR_NULL(mixer_res->mixer))
  836. clk_put(mixer_res->mixer);
  837. return ret;
  838. }
  839. static void mixer_resources_cleanup(struct mixer_context *ctx)
  840. {
  841. struct mixer_resources *res = &ctx->mixer_res;
  842. free_irq(res->irq, ctx);
  843. iounmap(res->vp_regs);
  844. iounmap(res->mixer_regs);
  845. }
  846. static int __devinit mixer_probe(struct platform_device *pdev)
  847. {
  848. struct device *dev = &pdev->dev;
  849. struct exynos_drm_hdmi_context *drm_hdmi_ctx;
  850. struct mixer_context *ctx;
  851. int ret;
  852. dev_info(dev, "probe start\n");
  853. drm_hdmi_ctx = kzalloc(sizeof(*drm_hdmi_ctx), GFP_KERNEL);
  854. if (!drm_hdmi_ctx) {
  855. DRM_ERROR("failed to allocate common hdmi context.\n");
  856. return -ENOMEM;
  857. }
  858. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  859. if (!ctx) {
  860. DRM_ERROR("failed to alloc mixer context.\n");
  861. kfree(drm_hdmi_ctx);
  862. return -ENOMEM;
  863. }
  864. mutex_init(&ctx->mixer_mutex);
  865. ctx->dev = &pdev->dev;
  866. drm_hdmi_ctx->ctx = (void *)ctx;
  867. platform_set_drvdata(pdev, drm_hdmi_ctx);
  868. /* acquire resources: regs, irqs, clocks */
  869. ret = mixer_resources_init(drm_hdmi_ctx, pdev);
  870. if (ret)
  871. goto fail;
  872. /* register specific callback point to common hdmi. */
  873. exynos_mixer_ops_register(&mixer_ops);
  874. pm_runtime_enable(dev);
  875. return 0;
  876. fail:
  877. dev_info(dev, "probe failed\n");
  878. return ret;
  879. }
  880. static int mixer_remove(struct platform_device *pdev)
  881. {
  882. struct device *dev = &pdev->dev;
  883. struct exynos_drm_hdmi_context *drm_hdmi_ctx =
  884. platform_get_drvdata(pdev);
  885. struct mixer_context *ctx = drm_hdmi_ctx->ctx;
  886. dev_info(dev, "remove successful\n");
  887. pm_runtime_disable(&pdev->dev);
  888. mixer_resources_cleanup(ctx);
  889. return 0;
  890. }
  891. #ifdef CONFIG_PM_SLEEP
  892. static int mixer_suspend(struct device *dev)
  893. {
  894. struct exynos_drm_hdmi_context *drm_hdmi_ctx = get_mixer_context(dev);
  895. struct mixer_context *ctx = drm_hdmi_ctx->ctx;
  896. mixer_poweroff(ctx);
  897. return 0;
  898. }
  899. #endif
  900. static SIMPLE_DEV_PM_OPS(mixer_pm_ops, mixer_suspend, NULL);
  901. struct platform_driver mixer_driver = {
  902. .driver = {
  903. .name = "s5p-mixer",
  904. .owner = THIS_MODULE,
  905. .pm = &mixer_pm_ops,
  906. },
  907. .probe = mixer_probe,
  908. .remove = __devexit_p(mixer_remove),
  909. };