sti_tvout.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. /*
  2. * Copyright (C) STMicroelectronics SA 2014
  3. * Authors: Benjamin Gaignard <benjamin.gaignard@st.com>
  4. * Vincent Abriou <vincent.abriou@st.com>
  5. * for STMicroelectronics.
  6. * License terms: GNU General Public License (GPL), version 2
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/component.h>
  10. #include <linux/module.h>
  11. #include <linux/of_platform.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/reset.h>
  14. #include <drm/drmP.h>
  15. #include <drm/drm_crtc_helper.h>
  16. #include "sti_crtc.h"
  17. /* glue registers */
  18. #define TVO_CSC_MAIN_M0 0x000
  19. #define TVO_CSC_MAIN_M1 0x004
  20. #define TVO_CSC_MAIN_M2 0x008
  21. #define TVO_CSC_MAIN_M3 0x00c
  22. #define TVO_CSC_MAIN_M4 0x010
  23. #define TVO_CSC_MAIN_M5 0x014
  24. #define TVO_CSC_MAIN_M6 0x018
  25. #define TVO_CSC_MAIN_M7 0x01c
  26. #define TVO_MAIN_IN_VID_FORMAT 0x030
  27. #define TVO_CSC_AUX_M0 0x100
  28. #define TVO_CSC_AUX_M1 0x104
  29. #define TVO_CSC_AUX_M2 0x108
  30. #define TVO_CSC_AUX_M3 0x10c
  31. #define TVO_CSC_AUX_M4 0x110
  32. #define TVO_CSC_AUX_M5 0x114
  33. #define TVO_CSC_AUX_M6 0x118
  34. #define TVO_CSC_AUX_M7 0x11c
  35. #define TVO_AUX_IN_VID_FORMAT 0x130
  36. #define TVO_VIP_HDF 0x400
  37. #define TVO_HD_SYNC_SEL 0x418
  38. #define TVO_HD_DAC_CFG_OFF 0x420
  39. #define TVO_VIP_HDMI 0x500
  40. #define TVO_HDMI_FORCE_COLOR_0 0x504
  41. #define TVO_HDMI_FORCE_COLOR_1 0x508
  42. #define TVO_HDMI_CLIP_VALUE_B_CB 0x50c
  43. #define TVO_HDMI_CLIP_VALUE_Y_G 0x510
  44. #define TVO_HDMI_CLIP_VALUE_R_CR 0x514
  45. #define TVO_HDMI_SYNC_SEL 0x518
  46. #define TVO_HDMI_DFV_OBS 0x540
  47. #define TVO_VIP_DVO 0x600
  48. #define TVO_DVO_SYNC_SEL 0x618
  49. #define TVO_DVO_CONFIG 0x620
  50. #define TVO_IN_FMT_SIGNED BIT(0)
  51. #define TVO_SYNC_EXT BIT(4)
  52. #define TVO_VIP_REORDER_R_SHIFT 24
  53. #define TVO_VIP_REORDER_G_SHIFT 20
  54. #define TVO_VIP_REORDER_B_SHIFT 16
  55. #define TVO_VIP_REORDER_MASK 0x3
  56. #define TVO_VIP_REORDER_Y_G_SEL 0
  57. #define TVO_VIP_REORDER_CB_B_SEL 1
  58. #define TVO_VIP_REORDER_CR_R_SEL 2
  59. #define TVO_VIP_CLIP_SHIFT 8
  60. #define TVO_VIP_CLIP_MASK 0x7
  61. #define TVO_VIP_CLIP_DISABLED 0
  62. #define TVO_VIP_CLIP_EAV_SAV 1
  63. #define TVO_VIP_CLIP_LIMITED_RANGE_RGB_Y 2
  64. #define TVO_VIP_CLIP_LIMITED_RANGE_CB_CR 3
  65. #define TVO_VIP_CLIP_PROG_RANGE 4
  66. #define TVO_VIP_RND_SHIFT 4
  67. #define TVO_VIP_RND_MASK 0x3
  68. #define TVO_VIP_RND_8BIT_ROUNDED 0
  69. #define TVO_VIP_RND_10BIT_ROUNDED 1
  70. #define TVO_VIP_RND_12BIT_ROUNDED 2
  71. #define TVO_VIP_SEL_INPUT_MASK 0xf
  72. #define TVO_VIP_SEL_INPUT_MAIN 0x0
  73. #define TVO_VIP_SEL_INPUT_AUX 0x8
  74. #define TVO_VIP_SEL_INPUT_FORCE_COLOR 0xf
  75. #define TVO_VIP_SEL_INPUT_BYPASS_MASK 0x1
  76. #define TVO_VIP_SEL_INPUT_BYPASSED 1
  77. #define TVO_SYNC_MAIN_VTG_SET_REF 0x00
  78. #define TVO_SYNC_MAIN_VTG_SET_1 0x01
  79. #define TVO_SYNC_MAIN_VTG_SET_2 0x02
  80. #define TVO_SYNC_MAIN_VTG_SET_3 0x03
  81. #define TVO_SYNC_MAIN_VTG_SET_4 0x04
  82. #define TVO_SYNC_MAIN_VTG_SET_5 0x05
  83. #define TVO_SYNC_MAIN_VTG_SET_6 0x06
  84. #define TVO_SYNC_AUX_VTG_SET_REF 0x10
  85. #define TVO_SYNC_AUX_VTG_SET_1 0x11
  86. #define TVO_SYNC_AUX_VTG_SET_2 0x12
  87. #define TVO_SYNC_AUX_VTG_SET_3 0x13
  88. #define TVO_SYNC_AUX_VTG_SET_4 0x14
  89. #define TVO_SYNC_AUX_VTG_SET_5 0x15
  90. #define TVO_SYNC_AUX_VTG_SET_6 0x16
  91. #define TVO_SYNC_HD_DCS_SHIFT 8
  92. #define TVO_SYNC_DVO_PAD_HSYNC_SHIFT 8
  93. #define TVO_SYNC_DVO_PAD_VSYNC_SHIFT 16
  94. #define ENCODER_CRTC_MASK (BIT(0) | BIT(1))
  95. /* enum listing the supported output data format */
  96. enum sti_tvout_video_out_type {
  97. STI_TVOUT_VIDEO_OUT_RGB,
  98. STI_TVOUT_VIDEO_OUT_YUV,
  99. };
  100. struct sti_tvout {
  101. struct device *dev;
  102. struct drm_device *drm_dev;
  103. void __iomem *regs;
  104. struct reset_control *reset;
  105. struct drm_encoder *hdmi;
  106. struct drm_encoder *hda;
  107. struct drm_encoder *dvo;
  108. };
  109. struct sti_tvout_encoder {
  110. struct drm_encoder encoder;
  111. struct sti_tvout *tvout;
  112. };
  113. #define to_sti_tvout_encoder(x) \
  114. container_of(x, struct sti_tvout_encoder, encoder)
  115. #define to_sti_tvout(x) to_sti_tvout_encoder(x)->tvout
  116. /* preformatter conversion matrix */
  117. static const u32 rgb_to_ycbcr_601[8] = {
  118. 0xF927082E, 0x04C9FEAB, 0x01D30964, 0xFA95FD3D,
  119. 0x0000082E, 0x00002000, 0x00002000, 0x00000000
  120. };
  121. /* 709 RGB to YCbCr */
  122. static const u32 rgb_to_ycbcr_709[8] = {
  123. 0xF891082F, 0x0367FF40, 0x01280B71, 0xF9B1FE20,
  124. 0x0000082F, 0x00002000, 0x00002000, 0x00000000
  125. };
  126. static u32 tvout_read(struct sti_tvout *tvout, int offset)
  127. {
  128. return readl(tvout->regs + offset);
  129. }
  130. static void tvout_write(struct sti_tvout *tvout, u32 val, int offset)
  131. {
  132. writel(val, tvout->regs + offset);
  133. }
  134. /**
  135. * Set the clipping mode of a VIP
  136. *
  137. * @tvout: tvout structure
  138. * @reg: register to set
  139. * @cr_r:
  140. * @y_g:
  141. * @cb_b:
  142. */
  143. static void tvout_vip_set_color_order(struct sti_tvout *tvout, int reg,
  144. u32 cr_r, u32 y_g, u32 cb_b)
  145. {
  146. u32 val = tvout_read(tvout, reg);
  147. val &= ~(TVO_VIP_REORDER_MASK << TVO_VIP_REORDER_R_SHIFT);
  148. val &= ~(TVO_VIP_REORDER_MASK << TVO_VIP_REORDER_G_SHIFT);
  149. val &= ~(TVO_VIP_REORDER_MASK << TVO_VIP_REORDER_B_SHIFT);
  150. val |= cr_r << TVO_VIP_REORDER_R_SHIFT;
  151. val |= y_g << TVO_VIP_REORDER_G_SHIFT;
  152. val |= cb_b << TVO_VIP_REORDER_B_SHIFT;
  153. tvout_write(tvout, val, reg);
  154. }
  155. /**
  156. * Set the clipping mode of a VIP
  157. *
  158. * @tvout: tvout structure
  159. * @reg: register to set
  160. * @range: clipping range
  161. */
  162. static void tvout_vip_set_clip_mode(struct sti_tvout *tvout, int reg, u32 range)
  163. {
  164. u32 val = tvout_read(tvout, reg);
  165. val &= ~(TVO_VIP_CLIP_MASK << TVO_VIP_CLIP_SHIFT);
  166. val |= range << TVO_VIP_CLIP_SHIFT;
  167. tvout_write(tvout, val, reg);
  168. }
  169. /**
  170. * Set the rounded value of a VIP
  171. *
  172. * @tvout: tvout structure
  173. * @reg: register to set
  174. * @rnd: rounded val per component
  175. */
  176. static void tvout_vip_set_rnd(struct sti_tvout *tvout, int reg, u32 rnd)
  177. {
  178. u32 val = tvout_read(tvout, reg);
  179. val &= ~(TVO_VIP_RND_MASK << TVO_VIP_RND_SHIFT);
  180. val |= rnd << TVO_VIP_RND_SHIFT;
  181. tvout_write(tvout, val, reg);
  182. }
  183. /**
  184. * Select the VIP input
  185. *
  186. * @tvout: tvout structure
  187. * @reg: register to set
  188. * @main_path: main or auxiliary path
  189. * @sel_input_logic_inverted: need to invert the logic
  190. * @sel_input: selected_input (main/aux + conv)
  191. */
  192. static void tvout_vip_set_sel_input(struct sti_tvout *tvout,
  193. int reg,
  194. bool main_path,
  195. bool sel_input_logic_inverted,
  196. enum sti_tvout_video_out_type video_out)
  197. {
  198. u32 sel_input;
  199. u32 val = tvout_read(tvout, reg);
  200. if (main_path)
  201. sel_input = TVO_VIP_SEL_INPUT_MAIN;
  202. else
  203. sel_input = TVO_VIP_SEL_INPUT_AUX;
  204. switch (video_out) {
  205. case STI_TVOUT_VIDEO_OUT_RGB:
  206. sel_input |= TVO_VIP_SEL_INPUT_BYPASSED;
  207. break;
  208. case STI_TVOUT_VIDEO_OUT_YUV:
  209. sel_input &= ~TVO_VIP_SEL_INPUT_BYPASSED;
  210. break;
  211. }
  212. /* on stih407 chip the sel_input bypass mode logic is inverted */
  213. if (sel_input_logic_inverted)
  214. sel_input = sel_input ^ TVO_VIP_SEL_INPUT_BYPASS_MASK;
  215. val &= ~TVO_VIP_SEL_INPUT_MASK;
  216. val |= sel_input;
  217. tvout_write(tvout, val, reg);
  218. }
  219. /**
  220. * Select the input video signed or unsigned
  221. *
  222. * @tvout: tvout structure
  223. * @reg: register to set
  224. * @in_vid_signed: used video input format
  225. */
  226. static void tvout_vip_set_in_vid_fmt(struct sti_tvout *tvout,
  227. int reg, u32 in_vid_fmt)
  228. {
  229. u32 val = tvout_read(tvout, reg);
  230. val &= ~TVO_IN_FMT_SIGNED;
  231. val |= in_vid_fmt;
  232. tvout_write(tvout, val, reg);
  233. }
  234. /**
  235. * Start VIP block for DVO output
  236. *
  237. * @tvout: pointer on tvout structure
  238. * @main_path: true if main path has to be used in the vip configuration
  239. * else aux path is used.
  240. */
  241. static void tvout_dvo_start(struct sti_tvout *tvout, bool main_path)
  242. {
  243. struct device_node *node = tvout->dev->of_node;
  244. bool sel_input_logic_inverted = false;
  245. u32 tvo_in_vid_format;
  246. int val;
  247. dev_dbg(tvout->dev, "%s\n", __func__);
  248. if (main_path) {
  249. DRM_DEBUG_DRIVER("main vip for DVO\n");
  250. /* Select the input sync for dvo = VTG set 4 */
  251. val = TVO_SYNC_MAIN_VTG_SET_4 << TVO_SYNC_DVO_PAD_VSYNC_SHIFT;
  252. val |= TVO_SYNC_MAIN_VTG_SET_4 << TVO_SYNC_DVO_PAD_HSYNC_SHIFT;
  253. val |= TVO_SYNC_MAIN_VTG_SET_4;
  254. tvout_write(tvout, val, TVO_DVO_SYNC_SEL);
  255. tvo_in_vid_format = TVO_MAIN_IN_VID_FORMAT;
  256. } else {
  257. DRM_DEBUG_DRIVER("aux vip for DVO\n");
  258. /* Select the input sync for dvo = VTG set 4 */
  259. val = TVO_SYNC_AUX_VTG_SET_4 << TVO_SYNC_DVO_PAD_VSYNC_SHIFT;
  260. val |= TVO_SYNC_AUX_VTG_SET_4 << TVO_SYNC_DVO_PAD_HSYNC_SHIFT;
  261. val |= TVO_SYNC_AUX_VTG_SET_4;
  262. tvout_write(tvout, val, TVO_DVO_SYNC_SEL);
  263. tvo_in_vid_format = TVO_AUX_IN_VID_FORMAT;
  264. }
  265. /* Set color channel order */
  266. tvout_vip_set_color_order(tvout, TVO_VIP_DVO,
  267. TVO_VIP_REORDER_CR_R_SEL,
  268. TVO_VIP_REORDER_Y_G_SEL,
  269. TVO_VIP_REORDER_CB_B_SEL);
  270. /* Set clipping mode (Limited range RGB/Y) */
  271. tvout_vip_set_clip_mode(tvout, TVO_VIP_DVO,
  272. TVO_VIP_CLIP_LIMITED_RANGE_RGB_Y);
  273. /* Set round mode (rounded to 8-bit per component) */
  274. tvout_vip_set_rnd(tvout, TVO_VIP_DVO, TVO_VIP_RND_8BIT_ROUNDED);
  275. if (of_device_is_compatible(node, "st,stih407-tvout")) {
  276. /* Set input video format */
  277. tvout_vip_set_in_vid_fmt(tvout, tvo_in_vid_format,
  278. TVO_IN_FMT_SIGNED);
  279. sel_input_logic_inverted = true;
  280. }
  281. /* Input selection */
  282. tvout_vip_set_sel_input(tvout, TVO_VIP_DVO, main_path,
  283. sel_input_logic_inverted,
  284. STI_TVOUT_VIDEO_OUT_RGB);
  285. }
  286. /**
  287. * Start VIP block for HDMI output
  288. *
  289. * @tvout: pointer on tvout structure
  290. * @main_path: true if main path has to be used in the vip configuration
  291. * else aux path is used.
  292. */
  293. static void tvout_hdmi_start(struct sti_tvout *tvout, bool main_path)
  294. {
  295. struct device_node *node = tvout->dev->of_node;
  296. bool sel_input_logic_inverted = false;
  297. u32 tvo_in_vid_format;
  298. dev_dbg(tvout->dev, "%s\n", __func__);
  299. if (main_path) {
  300. DRM_DEBUG_DRIVER("main vip for hdmi\n");
  301. /* select the input sync for hdmi = VTG set 1 */
  302. tvout_write(tvout, TVO_SYNC_MAIN_VTG_SET_1, TVO_HDMI_SYNC_SEL);
  303. tvo_in_vid_format = TVO_MAIN_IN_VID_FORMAT;
  304. } else {
  305. DRM_DEBUG_DRIVER("aux vip for hdmi\n");
  306. /* select the input sync for hdmi = VTG set 1 */
  307. tvout_write(tvout, TVO_SYNC_AUX_VTG_SET_1, TVO_HDMI_SYNC_SEL);
  308. tvo_in_vid_format = TVO_AUX_IN_VID_FORMAT;
  309. }
  310. /* set color channel order */
  311. tvout_vip_set_color_order(tvout, TVO_VIP_HDMI,
  312. TVO_VIP_REORDER_CR_R_SEL,
  313. TVO_VIP_REORDER_Y_G_SEL,
  314. TVO_VIP_REORDER_CB_B_SEL);
  315. /* set clipping mode (Limited range RGB/Y) */
  316. tvout_vip_set_clip_mode(tvout, TVO_VIP_HDMI,
  317. TVO_VIP_CLIP_LIMITED_RANGE_RGB_Y);
  318. /* set round mode (rounded to 8-bit per component) */
  319. tvout_vip_set_rnd(tvout, TVO_VIP_HDMI, TVO_VIP_RND_8BIT_ROUNDED);
  320. if (of_device_is_compatible(node, "st,stih407-tvout")) {
  321. /* set input video format */
  322. tvout_vip_set_in_vid_fmt(tvout, tvo_in_vid_format,
  323. TVO_IN_FMT_SIGNED);
  324. sel_input_logic_inverted = true;
  325. }
  326. /* input selection */
  327. tvout_vip_set_sel_input(tvout, TVO_VIP_HDMI, main_path,
  328. sel_input_logic_inverted, STI_TVOUT_VIDEO_OUT_RGB);
  329. }
  330. /**
  331. * Start HDF VIP and HD DAC
  332. *
  333. * @tvout: pointer on tvout structure
  334. * @main_path: true if main path has to be used in the vip configuration
  335. * else aux path is used.
  336. */
  337. static void tvout_hda_start(struct sti_tvout *tvout, bool main_path)
  338. {
  339. struct device_node *node = tvout->dev->of_node;
  340. bool sel_input_logic_inverted = false;
  341. u32 tvo_in_vid_format;
  342. int val;
  343. dev_dbg(tvout->dev, "%s\n", __func__);
  344. if (main_path) {
  345. val = TVO_SYNC_MAIN_VTG_SET_2 << TVO_SYNC_HD_DCS_SHIFT;
  346. val |= TVO_SYNC_MAIN_VTG_SET_3;
  347. tvout_write(tvout, val, TVO_HD_SYNC_SEL);
  348. tvo_in_vid_format = TVO_MAIN_IN_VID_FORMAT;
  349. } else {
  350. val = TVO_SYNC_AUX_VTG_SET_2 << TVO_SYNC_HD_DCS_SHIFT;
  351. val |= TVO_SYNC_AUX_VTG_SET_3;
  352. tvout_write(tvout, val, TVO_HD_SYNC_SEL);
  353. tvo_in_vid_format = TVO_AUX_IN_VID_FORMAT;
  354. }
  355. /* set color channel order */
  356. tvout_vip_set_color_order(tvout, TVO_VIP_HDF,
  357. TVO_VIP_REORDER_CR_R_SEL,
  358. TVO_VIP_REORDER_Y_G_SEL,
  359. TVO_VIP_REORDER_CB_B_SEL);
  360. /* set clipping mode (EAV/SAV clipping) */
  361. tvout_vip_set_clip_mode(tvout, TVO_VIP_HDF, TVO_VIP_CLIP_EAV_SAV);
  362. /* set round mode (rounded to 10-bit per component) */
  363. tvout_vip_set_rnd(tvout, TVO_VIP_HDF, TVO_VIP_RND_10BIT_ROUNDED);
  364. if (of_device_is_compatible(node, "st,stih407-tvout")) {
  365. /* set input video format */
  366. tvout_vip_set_in_vid_fmt(tvout,
  367. tvo_in_vid_format, TVO_IN_FMT_SIGNED);
  368. sel_input_logic_inverted = true;
  369. }
  370. /* Input selection */
  371. tvout_vip_set_sel_input(tvout, TVO_VIP_HDF, main_path,
  372. sel_input_logic_inverted,
  373. STI_TVOUT_VIDEO_OUT_YUV);
  374. /* power up HD DAC */
  375. tvout_write(tvout, 0, TVO_HD_DAC_CFG_OFF);
  376. }
  377. static void sti_tvout_encoder_dpms(struct drm_encoder *encoder, int mode)
  378. {
  379. }
  380. static bool sti_tvout_encoder_mode_fixup(struct drm_encoder *encoder,
  381. const struct drm_display_mode *mode,
  382. struct drm_display_mode *adjusted_mode)
  383. {
  384. return true;
  385. }
  386. static void sti_tvout_encoder_mode_set(struct drm_encoder *encoder,
  387. struct drm_display_mode *mode,
  388. struct drm_display_mode *adjusted_mode)
  389. {
  390. }
  391. static void sti_tvout_encoder_prepare(struct drm_encoder *encoder)
  392. {
  393. }
  394. static void sti_tvout_encoder_destroy(struct drm_encoder *encoder)
  395. {
  396. struct sti_tvout_encoder *sti_encoder = to_sti_tvout_encoder(encoder);
  397. drm_encoder_cleanup(encoder);
  398. kfree(sti_encoder);
  399. }
  400. static const struct drm_encoder_funcs sti_tvout_encoder_funcs = {
  401. .destroy = sti_tvout_encoder_destroy,
  402. };
  403. static void sti_dvo_encoder_commit(struct drm_encoder *encoder)
  404. {
  405. struct sti_tvout *tvout = to_sti_tvout(encoder);
  406. tvout_dvo_start(tvout, sti_crtc_is_main(encoder->crtc));
  407. }
  408. static void sti_dvo_encoder_disable(struct drm_encoder *encoder)
  409. {
  410. struct sti_tvout *tvout = to_sti_tvout(encoder);
  411. /* Reset VIP register */
  412. tvout_write(tvout, 0x0, TVO_VIP_DVO);
  413. }
  414. static const struct drm_encoder_helper_funcs sti_dvo_encoder_helper_funcs = {
  415. .dpms = sti_tvout_encoder_dpms,
  416. .mode_fixup = sti_tvout_encoder_mode_fixup,
  417. .mode_set = sti_tvout_encoder_mode_set,
  418. .prepare = sti_tvout_encoder_prepare,
  419. .commit = sti_dvo_encoder_commit,
  420. .disable = sti_dvo_encoder_disable,
  421. };
  422. static struct drm_encoder *
  423. sti_tvout_create_dvo_encoder(struct drm_device *dev,
  424. struct sti_tvout *tvout)
  425. {
  426. struct sti_tvout_encoder *encoder;
  427. struct drm_encoder *drm_encoder;
  428. encoder = devm_kzalloc(tvout->dev, sizeof(*encoder), GFP_KERNEL);
  429. if (!encoder)
  430. return NULL;
  431. encoder->tvout = tvout;
  432. drm_encoder = (struct drm_encoder *)encoder;
  433. drm_encoder->possible_crtcs = ENCODER_CRTC_MASK;
  434. drm_encoder->possible_clones = 1 << 0;
  435. drm_encoder_init(dev, drm_encoder,
  436. &sti_tvout_encoder_funcs, DRM_MODE_ENCODER_LVDS);
  437. drm_encoder_helper_add(drm_encoder, &sti_dvo_encoder_helper_funcs);
  438. return drm_encoder;
  439. }
  440. static void sti_hda_encoder_commit(struct drm_encoder *encoder)
  441. {
  442. struct sti_tvout *tvout = to_sti_tvout(encoder);
  443. tvout_hda_start(tvout, sti_crtc_is_main(encoder->crtc));
  444. }
  445. static void sti_hda_encoder_disable(struct drm_encoder *encoder)
  446. {
  447. struct sti_tvout *tvout = to_sti_tvout(encoder);
  448. /* reset VIP register */
  449. tvout_write(tvout, 0x0, TVO_VIP_HDF);
  450. /* power down HD DAC */
  451. tvout_write(tvout, 1, TVO_HD_DAC_CFG_OFF);
  452. }
  453. static const struct drm_encoder_helper_funcs sti_hda_encoder_helper_funcs = {
  454. .dpms = sti_tvout_encoder_dpms,
  455. .mode_fixup = sti_tvout_encoder_mode_fixup,
  456. .mode_set = sti_tvout_encoder_mode_set,
  457. .prepare = sti_tvout_encoder_prepare,
  458. .commit = sti_hda_encoder_commit,
  459. .disable = sti_hda_encoder_disable,
  460. };
  461. static struct drm_encoder *sti_tvout_create_hda_encoder(struct drm_device *dev,
  462. struct sti_tvout *tvout)
  463. {
  464. struct sti_tvout_encoder *encoder;
  465. struct drm_encoder *drm_encoder;
  466. encoder = devm_kzalloc(tvout->dev, sizeof(*encoder), GFP_KERNEL);
  467. if (!encoder)
  468. return NULL;
  469. encoder->tvout = tvout;
  470. drm_encoder = (struct drm_encoder *) encoder;
  471. drm_encoder->possible_crtcs = ENCODER_CRTC_MASK;
  472. drm_encoder->possible_clones = 1 << 0;
  473. drm_encoder_init(dev, drm_encoder,
  474. &sti_tvout_encoder_funcs, DRM_MODE_ENCODER_DAC);
  475. drm_encoder_helper_add(drm_encoder, &sti_hda_encoder_helper_funcs);
  476. return drm_encoder;
  477. }
  478. static void sti_hdmi_encoder_commit(struct drm_encoder *encoder)
  479. {
  480. struct sti_tvout *tvout = to_sti_tvout(encoder);
  481. tvout_hdmi_start(tvout, sti_crtc_is_main(encoder->crtc));
  482. }
  483. static void sti_hdmi_encoder_disable(struct drm_encoder *encoder)
  484. {
  485. struct sti_tvout *tvout = to_sti_tvout(encoder);
  486. /* reset VIP register */
  487. tvout_write(tvout, 0x0, TVO_VIP_HDMI);
  488. }
  489. static const struct drm_encoder_helper_funcs sti_hdmi_encoder_helper_funcs = {
  490. .dpms = sti_tvout_encoder_dpms,
  491. .mode_fixup = sti_tvout_encoder_mode_fixup,
  492. .mode_set = sti_tvout_encoder_mode_set,
  493. .prepare = sti_tvout_encoder_prepare,
  494. .commit = sti_hdmi_encoder_commit,
  495. .disable = sti_hdmi_encoder_disable,
  496. };
  497. static struct drm_encoder *sti_tvout_create_hdmi_encoder(struct drm_device *dev,
  498. struct sti_tvout *tvout)
  499. {
  500. struct sti_tvout_encoder *encoder;
  501. struct drm_encoder *drm_encoder;
  502. encoder = devm_kzalloc(tvout->dev, sizeof(*encoder), GFP_KERNEL);
  503. if (!encoder)
  504. return NULL;
  505. encoder->tvout = tvout;
  506. drm_encoder = (struct drm_encoder *) encoder;
  507. drm_encoder->possible_crtcs = ENCODER_CRTC_MASK;
  508. drm_encoder->possible_clones = 1 << 1;
  509. drm_encoder_init(dev, drm_encoder,
  510. &sti_tvout_encoder_funcs, DRM_MODE_ENCODER_TMDS);
  511. drm_encoder_helper_add(drm_encoder, &sti_hdmi_encoder_helper_funcs);
  512. return drm_encoder;
  513. }
  514. static void sti_tvout_create_encoders(struct drm_device *dev,
  515. struct sti_tvout *tvout)
  516. {
  517. tvout->hdmi = sti_tvout_create_hdmi_encoder(dev, tvout);
  518. tvout->hda = sti_tvout_create_hda_encoder(dev, tvout);
  519. tvout->dvo = sti_tvout_create_dvo_encoder(dev, tvout);
  520. }
  521. static void sti_tvout_destroy_encoders(struct sti_tvout *tvout)
  522. {
  523. if (tvout->hdmi)
  524. drm_encoder_cleanup(tvout->hdmi);
  525. tvout->hdmi = NULL;
  526. if (tvout->hda)
  527. drm_encoder_cleanup(tvout->hda);
  528. tvout->hda = NULL;
  529. }
  530. static int sti_tvout_bind(struct device *dev, struct device *master, void *data)
  531. {
  532. struct sti_tvout *tvout = dev_get_drvdata(dev);
  533. struct drm_device *drm_dev = data;
  534. unsigned int i;
  535. tvout->drm_dev = drm_dev;
  536. /* set preformatter matrix */
  537. for (i = 0; i < 8; i++) {
  538. tvout_write(tvout, rgb_to_ycbcr_601[i],
  539. TVO_CSC_MAIN_M0 + (i * 4));
  540. tvout_write(tvout, rgb_to_ycbcr_601[i],
  541. TVO_CSC_AUX_M0 + (i * 4));
  542. }
  543. sti_tvout_create_encoders(drm_dev, tvout);
  544. return 0;
  545. }
  546. static void sti_tvout_unbind(struct device *dev, struct device *master,
  547. void *data)
  548. {
  549. struct sti_tvout *tvout = dev_get_drvdata(dev);
  550. sti_tvout_destroy_encoders(tvout);
  551. }
  552. static const struct component_ops sti_tvout_ops = {
  553. .bind = sti_tvout_bind,
  554. .unbind = sti_tvout_unbind,
  555. };
  556. static int sti_tvout_probe(struct platform_device *pdev)
  557. {
  558. struct device *dev = &pdev->dev;
  559. struct device_node *node = dev->of_node;
  560. struct sti_tvout *tvout;
  561. struct resource *res;
  562. DRM_INFO("%s\n", __func__);
  563. if (!node)
  564. return -ENODEV;
  565. tvout = devm_kzalloc(dev, sizeof(*tvout), GFP_KERNEL);
  566. if (!tvout)
  567. return -ENOMEM;
  568. tvout->dev = dev;
  569. /* get Memory ressources */
  570. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "tvout-reg");
  571. if (!res) {
  572. DRM_ERROR("Invalid glue resource\n");
  573. return -ENOMEM;
  574. }
  575. tvout->regs = devm_ioremap_nocache(dev, res->start, resource_size(res));
  576. if (!tvout->regs)
  577. return -ENOMEM;
  578. /* get reset resources */
  579. tvout->reset = devm_reset_control_get(dev, "tvout");
  580. /* take tvout out of reset */
  581. if (!IS_ERR(tvout->reset))
  582. reset_control_deassert(tvout->reset);
  583. platform_set_drvdata(pdev, tvout);
  584. return component_add(dev, &sti_tvout_ops);
  585. }
  586. static int sti_tvout_remove(struct platform_device *pdev)
  587. {
  588. component_del(&pdev->dev, &sti_tvout_ops);
  589. return 0;
  590. }
  591. static const struct of_device_id tvout_of_match[] = {
  592. { .compatible = "st,stih416-tvout", },
  593. { .compatible = "st,stih407-tvout", },
  594. { /* end node */ }
  595. };
  596. MODULE_DEVICE_TABLE(of, tvout_of_match);
  597. struct platform_driver sti_tvout_driver = {
  598. .driver = {
  599. .name = "sti-tvout",
  600. .owner = THIS_MODULE,
  601. .of_match_table = tvout_of_match,
  602. },
  603. .probe = sti_tvout_probe,
  604. .remove = sti_tvout_remove,
  605. };
  606. MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
  607. MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
  608. MODULE_LICENSE("GPL");