blob: 8092418ac65e9dd546a19f829886083774749521 [file] [log] [blame]
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +02001/*
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
9#include <linux/clk.h>
10#include <linux/component.h>
11#include <linux/module.h>
12#include <linux/of_platform.h>
13#include <linux/platform_device.h>
14#include <linux/reset.h>
15
16#include <drm/drmP.h>
17#include <drm/drm_crtc_helper.h>
18
Vincent Abriou9e1f05b2015-07-31 11:32:34 +020019#include "sti_crtc.h"
Vincent Abriou503290c2016-01-28 13:08:48 +010020#include "sti_vtg.h"
Benjamin Gaignard5e03abc2014-12-08 17:32:36 +010021
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +020022/* glue registers */
23#define TVO_CSC_MAIN_M0 0x000
24#define TVO_CSC_MAIN_M1 0x004
25#define TVO_CSC_MAIN_M2 0x008
26#define TVO_CSC_MAIN_M3 0x00c
27#define TVO_CSC_MAIN_M4 0x010
28#define TVO_CSC_MAIN_M5 0x014
29#define TVO_CSC_MAIN_M6 0x018
30#define TVO_CSC_MAIN_M7 0x01c
31#define TVO_MAIN_IN_VID_FORMAT 0x030
32#define TVO_CSC_AUX_M0 0x100
33#define TVO_CSC_AUX_M1 0x104
34#define TVO_CSC_AUX_M2 0x108
35#define TVO_CSC_AUX_M3 0x10c
36#define TVO_CSC_AUX_M4 0x110
37#define TVO_CSC_AUX_M5 0x114
38#define TVO_CSC_AUX_M6 0x118
39#define TVO_CSC_AUX_M7 0x11c
40#define TVO_AUX_IN_VID_FORMAT 0x130
41#define TVO_VIP_HDF 0x400
42#define TVO_HD_SYNC_SEL 0x418
43#define TVO_HD_DAC_CFG_OFF 0x420
44#define TVO_VIP_HDMI 0x500
45#define TVO_HDMI_FORCE_COLOR_0 0x504
46#define TVO_HDMI_FORCE_COLOR_1 0x508
47#define TVO_HDMI_CLIP_VALUE_B_CB 0x50c
48#define TVO_HDMI_CLIP_VALUE_Y_G 0x510
49#define TVO_HDMI_CLIP_VALUE_R_CR 0x514
50#define TVO_HDMI_SYNC_SEL 0x518
51#define TVO_HDMI_DFV_OBS 0x540
Benjamin Gaignardf32c4c52014-12-30 15:08:16 +010052#define TVO_VIP_DVO 0x600
53#define TVO_DVO_SYNC_SEL 0x618
54#define TVO_DVO_CONFIG 0x620
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +020055
56#define TVO_IN_FMT_SIGNED BIT(0)
57#define TVO_SYNC_EXT BIT(4)
58
59#define TVO_VIP_REORDER_R_SHIFT 24
60#define TVO_VIP_REORDER_G_SHIFT 20
61#define TVO_VIP_REORDER_B_SHIFT 16
62#define TVO_VIP_REORDER_MASK 0x3
63#define TVO_VIP_REORDER_Y_G_SEL 0
64#define TVO_VIP_REORDER_CB_B_SEL 1
65#define TVO_VIP_REORDER_CR_R_SEL 2
66
67#define TVO_VIP_CLIP_SHIFT 8
68#define TVO_VIP_CLIP_MASK 0x7
69#define TVO_VIP_CLIP_DISABLED 0
70#define TVO_VIP_CLIP_EAV_SAV 1
71#define TVO_VIP_CLIP_LIMITED_RANGE_RGB_Y 2
72#define TVO_VIP_CLIP_LIMITED_RANGE_CB_CR 3
73#define TVO_VIP_CLIP_PROG_RANGE 4
74
75#define TVO_VIP_RND_SHIFT 4
76#define TVO_VIP_RND_MASK 0x3
77#define TVO_VIP_RND_8BIT_ROUNDED 0
78#define TVO_VIP_RND_10BIT_ROUNDED 1
79#define TVO_VIP_RND_12BIT_ROUNDED 2
80
81#define TVO_VIP_SEL_INPUT_MASK 0xf
82#define TVO_VIP_SEL_INPUT_MAIN 0x0
83#define TVO_VIP_SEL_INPUT_AUX 0x8
84#define TVO_VIP_SEL_INPUT_FORCE_COLOR 0xf
85#define TVO_VIP_SEL_INPUT_BYPASS_MASK 0x1
86#define TVO_VIP_SEL_INPUT_BYPASSED 1
87
88#define TVO_SYNC_MAIN_VTG_SET_REF 0x00
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +020089#define TVO_SYNC_AUX_VTG_SET_REF 0x10
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +020090
91#define TVO_SYNC_HD_DCS_SHIFT 8
92
Benjamin Gaignardf32c4c52014-12-30 15:08:16 +010093#define TVO_SYNC_DVO_PAD_HSYNC_SHIFT 8
94#define TVO_SYNC_DVO_PAD_VSYNC_SHIFT 16
95
Benjamin Gaignard5e03abc2014-12-08 17:32:36 +010096#define ENCODER_CRTC_MASK (BIT(0) | BIT(1))
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +020097
Bich Hemon05a142c2016-02-10 10:39:23 +010098#define TVO_MIN_HD_HEIGHT 720
99
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200100/* enum listing the supported output data format */
101enum sti_tvout_video_out_type {
102 STI_TVOUT_VIDEO_OUT_RGB,
103 STI_TVOUT_VIDEO_OUT_YUV,
104};
105
106struct sti_tvout {
107 struct device *dev;
108 struct drm_device *drm_dev;
109 void __iomem *regs;
110 struct reset_control *reset;
111 struct drm_encoder *hdmi;
112 struct drm_encoder *hda;
Benjamin Gaignardf32c4c52014-12-30 15:08:16 +0100113 struct drm_encoder *dvo;
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200114};
115
116struct sti_tvout_encoder {
117 struct drm_encoder encoder;
118 struct sti_tvout *tvout;
119};
120
121#define to_sti_tvout_encoder(x) \
122 container_of(x, struct sti_tvout_encoder, encoder)
123
124#define to_sti_tvout(x) to_sti_tvout_encoder(x)->tvout
125
126/* preformatter conversion matrix */
127static const u32 rgb_to_ycbcr_601[8] = {
128 0xF927082E, 0x04C9FEAB, 0x01D30964, 0xFA95FD3D,
129 0x0000082E, 0x00002000, 0x00002000, 0x00000000
130};
131
132/* 709 RGB to YCbCr */
133static const u32 rgb_to_ycbcr_709[8] = {
134 0xF891082F, 0x0367FF40, 0x01280B71, 0xF9B1FE20,
135 0x0000082F, 0x00002000, 0x00002000, 0x00000000
136};
137
138static u32 tvout_read(struct sti_tvout *tvout, int offset)
139{
140 return readl(tvout->regs + offset);
141}
142
143static void tvout_write(struct sti_tvout *tvout, u32 val, int offset)
144{
145 writel(val, tvout->regs + offset);
146}
147
148/**
149 * Set the clipping mode of a VIP
150 *
151 * @tvout: tvout structure
Benjamin Gaignardca279602014-12-04 11:31:37 +0100152 * @reg: register to set
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200153 * @cr_r:
154 * @y_g:
155 * @cb_b:
156 */
Benjamin Gaignardca279602014-12-04 11:31:37 +0100157static void tvout_vip_set_color_order(struct sti_tvout *tvout, int reg,
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200158 u32 cr_r, u32 y_g, u32 cb_b)
159{
Benjamin Gaignardca279602014-12-04 11:31:37 +0100160 u32 val = tvout_read(tvout, reg);
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200161
162 val &= ~(TVO_VIP_REORDER_MASK << TVO_VIP_REORDER_R_SHIFT);
163 val &= ~(TVO_VIP_REORDER_MASK << TVO_VIP_REORDER_G_SHIFT);
164 val &= ~(TVO_VIP_REORDER_MASK << TVO_VIP_REORDER_B_SHIFT);
165 val |= cr_r << TVO_VIP_REORDER_R_SHIFT;
166 val |= y_g << TVO_VIP_REORDER_G_SHIFT;
167 val |= cb_b << TVO_VIP_REORDER_B_SHIFT;
168
Benjamin Gaignardca279602014-12-04 11:31:37 +0100169 tvout_write(tvout, val, reg);
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200170}
171
172/**
173 * Set the clipping mode of a VIP
174 *
175 * @tvout: tvout structure
Benjamin Gaignardca279602014-12-04 11:31:37 +0100176 * @reg: register to set
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200177 * @range: clipping range
178 */
Benjamin Gaignardca279602014-12-04 11:31:37 +0100179static void tvout_vip_set_clip_mode(struct sti_tvout *tvout, int reg, u32 range)
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200180{
Benjamin Gaignardca279602014-12-04 11:31:37 +0100181 u32 val = tvout_read(tvout, reg);
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200182
183 val &= ~(TVO_VIP_CLIP_MASK << TVO_VIP_CLIP_SHIFT);
184 val |= range << TVO_VIP_CLIP_SHIFT;
Benjamin Gaignardca279602014-12-04 11:31:37 +0100185 tvout_write(tvout, val, reg);
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200186}
187
188/**
189 * Set the rounded value of a VIP
190 *
191 * @tvout: tvout structure
Benjamin Gaignardca279602014-12-04 11:31:37 +0100192 * @reg: register to set
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200193 * @rnd: rounded val per component
194 */
Benjamin Gaignardca279602014-12-04 11:31:37 +0100195static void tvout_vip_set_rnd(struct sti_tvout *tvout, int reg, u32 rnd)
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200196{
Benjamin Gaignardca279602014-12-04 11:31:37 +0100197 u32 val = tvout_read(tvout, reg);
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200198
199 val &= ~(TVO_VIP_RND_MASK << TVO_VIP_RND_SHIFT);
200 val |= rnd << TVO_VIP_RND_SHIFT;
Benjamin Gaignardca279602014-12-04 11:31:37 +0100201 tvout_write(tvout, val, reg);
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200202}
203
204/**
205 * Select the VIP input
206 *
207 * @tvout: tvout structure
Benjamin Gaignardca279602014-12-04 11:31:37 +0100208 * @reg: register to set
209 * @main_path: main or auxiliary path
210 * @sel_input_logic_inverted: need to invert the logic
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200211 * @sel_input: selected_input (main/aux + conv)
212 */
213static void tvout_vip_set_sel_input(struct sti_tvout *tvout,
Benjamin Gaignardca279602014-12-04 11:31:37 +0100214 int reg,
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200215 bool main_path,
216 bool sel_input_logic_inverted,
217 enum sti_tvout_video_out_type video_out)
218{
219 u32 sel_input;
Benjamin Gaignardca279602014-12-04 11:31:37 +0100220 u32 val = tvout_read(tvout, reg);
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200221
222 if (main_path)
223 sel_input = TVO_VIP_SEL_INPUT_MAIN;
224 else
225 sel_input = TVO_VIP_SEL_INPUT_AUX;
226
227 switch (video_out) {
228 case STI_TVOUT_VIDEO_OUT_RGB:
229 sel_input |= TVO_VIP_SEL_INPUT_BYPASSED;
230 break;
231 case STI_TVOUT_VIDEO_OUT_YUV:
232 sel_input &= ~TVO_VIP_SEL_INPUT_BYPASSED;
233 break;
234 }
235
236 /* on stih407 chip the sel_input bypass mode logic is inverted */
237 if (sel_input_logic_inverted)
238 sel_input = sel_input ^ TVO_VIP_SEL_INPUT_BYPASS_MASK;
239
240 val &= ~TVO_VIP_SEL_INPUT_MASK;
241 val |= sel_input;
Benjamin Gaignardca279602014-12-04 11:31:37 +0100242 tvout_write(tvout, val, reg);
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200243}
244
245/**
246 * Select the input video signed or unsigned
247 *
248 * @tvout: tvout structure
Benjamin Gaignardca279602014-12-04 11:31:37 +0100249 * @reg: register to set
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200250 * @in_vid_signed: used video input format
251 */
Benjamin Gaignardca279602014-12-04 11:31:37 +0100252static void tvout_vip_set_in_vid_fmt(struct sti_tvout *tvout,
253 int reg, u32 in_vid_fmt)
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200254{
Benjamin Gaignardca279602014-12-04 11:31:37 +0100255 u32 val = tvout_read(tvout, reg);
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200256
257 val &= ~TVO_IN_FMT_SIGNED;
258 val |= in_vid_fmt;
Benjamin Gaignardca279602014-12-04 11:31:37 +0100259 tvout_write(tvout, val, reg);
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200260}
261
262/**
Bich Hemon05a142c2016-02-10 10:39:23 +0100263 * Set preformatter matrix
264 *
265 * @tvout: tvout structure
266 * @mode: display mode structure
267 */
268static void tvout_preformatter_set_matrix(struct sti_tvout *tvout,
269 struct drm_display_mode *mode)
270{
271 unsigned int i;
272 const u32 *pf_matrix;
273
274 if (mode->vdisplay >= TVO_MIN_HD_HEIGHT)
275 pf_matrix = rgb_to_ycbcr_709;
276 else
277 pf_matrix = rgb_to_ycbcr_601;
278
279 for (i = 0; i < 8; i++) {
280 tvout_write(tvout, *(pf_matrix + i),
281 TVO_CSC_MAIN_M0 + (i * 4));
282 tvout_write(tvout, *(pf_matrix + i),
283 TVO_CSC_AUX_M0 + (i * 4));
284 }
285}
286
287/**
Benjamin Gaignardf32c4c52014-12-30 15:08:16 +0100288 * Start VIP block for DVO output
289 *
290 * @tvout: pointer on tvout structure
291 * @main_path: true if main path has to be used in the vip configuration
292 * else aux path is used.
293 */
294static void tvout_dvo_start(struct sti_tvout *tvout, bool main_path)
295{
296 struct device_node *node = tvout->dev->of_node;
297 bool sel_input_logic_inverted = false;
298 u32 tvo_in_vid_format;
Vincent Abriou503290c2016-01-28 13:08:48 +0100299 int val, tmp;
Benjamin Gaignardf32c4c52014-12-30 15:08:16 +0100300
301 dev_dbg(tvout->dev, "%s\n", __func__);
302
303 if (main_path) {
304 DRM_DEBUG_DRIVER("main vip for DVO\n");
Vincent Abriou503290c2016-01-28 13:08:48 +0100305 /* Select the input sync for dvo */
306 tmp = TVO_SYNC_MAIN_VTG_SET_REF | VTG_SYNC_ID_DVO;
307 val = tmp << TVO_SYNC_DVO_PAD_VSYNC_SHIFT;
308 val |= tmp << TVO_SYNC_DVO_PAD_HSYNC_SHIFT;
309 val |= tmp;
Benjamin Gaignardf32c4c52014-12-30 15:08:16 +0100310 tvout_write(tvout, val, TVO_DVO_SYNC_SEL);
311 tvo_in_vid_format = TVO_MAIN_IN_VID_FORMAT;
312 } else {
313 DRM_DEBUG_DRIVER("aux vip for DVO\n");
Vincent Abriou503290c2016-01-28 13:08:48 +0100314 /* Select the input sync for dvo */
315 tmp = TVO_SYNC_AUX_VTG_SET_REF | VTG_SYNC_ID_DVO;
316 val = tmp << TVO_SYNC_DVO_PAD_VSYNC_SHIFT;
317 val |= tmp << TVO_SYNC_DVO_PAD_HSYNC_SHIFT;
318 val |= tmp;
Benjamin Gaignardf32c4c52014-12-30 15:08:16 +0100319 tvout_write(tvout, val, TVO_DVO_SYNC_SEL);
320 tvo_in_vid_format = TVO_AUX_IN_VID_FORMAT;
321 }
322
323 /* Set color channel order */
324 tvout_vip_set_color_order(tvout, TVO_VIP_DVO,
325 TVO_VIP_REORDER_CR_R_SEL,
326 TVO_VIP_REORDER_Y_G_SEL,
327 TVO_VIP_REORDER_CB_B_SEL);
328
Vincent Abriou1834b842016-02-01 17:51:33 +0100329 /* Set clipping mode */
330 tvout_vip_set_clip_mode(tvout, TVO_VIP_DVO, TVO_VIP_CLIP_DISABLED);
Benjamin Gaignardf32c4c52014-12-30 15:08:16 +0100331
332 /* Set round mode (rounded to 8-bit per component) */
333 tvout_vip_set_rnd(tvout, TVO_VIP_DVO, TVO_VIP_RND_8BIT_ROUNDED);
334
335 if (of_device_is_compatible(node, "st,stih407-tvout")) {
336 /* Set input video format */
337 tvout_vip_set_in_vid_fmt(tvout, tvo_in_vid_format,
338 TVO_IN_FMT_SIGNED);
339 sel_input_logic_inverted = true;
340 }
341
342 /* Input selection */
343 tvout_vip_set_sel_input(tvout, TVO_VIP_DVO, main_path,
344 sel_input_logic_inverted,
345 STI_TVOUT_VIDEO_OUT_RGB);
346}
347
348/**
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200349 * Start VIP block for HDMI output
350 *
351 * @tvout: pointer on tvout structure
352 * @main_path: true if main path has to be used in the vip configuration
353 * else aux path is used.
354 */
355static void tvout_hdmi_start(struct sti_tvout *tvout, bool main_path)
356{
357 struct device_node *node = tvout->dev->of_node;
358 bool sel_input_logic_inverted = false;
Benjamin Gaignardca279602014-12-04 11:31:37 +0100359 u32 tvo_in_vid_format;
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200360
361 dev_dbg(tvout->dev, "%s\n", __func__);
362
363 if (main_path) {
364 DRM_DEBUG_DRIVER("main vip for hdmi\n");
Vincent Abriou503290c2016-01-28 13:08:48 +0100365 /* select the input sync for hdmi */
366 tvout_write(tvout,
367 TVO_SYNC_MAIN_VTG_SET_REF | VTG_SYNC_ID_HDMI,
368 TVO_HDMI_SYNC_SEL);
Benjamin Gaignardca279602014-12-04 11:31:37 +0100369 tvo_in_vid_format = TVO_MAIN_IN_VID_FORMAT;
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200370 } else {
371 DRM_DEBUG_DRIVER("aux vip for hdmi\n");
Vincent Abriou503290c2016-01-28 13:08:48 +0100372 /* select the input sync for hdmi */
373 tvout_write(tvout,
374 TVO_SYNC_AUX_VTG_SET_REF | VTG_SYNC_ID_HDMI,
375 TVO_HDMI_SYNC_SEL);
Benjamin Gaignardca279602014-12-04 11:31:37 +0100376 tvo_in_vid_format = TVO_AUX_IN_VID_FORMAT;
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200377 }
378
379 /* set color channel order */
Benjamin Gaignardca279602014-12-04 11:31:37 +0100380 tvout_vip_set_color_order(tvout, TVO_VIP_HDMI,
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200381 TVO_VIP_REORDER_CR_R_SEL,
382 TVO_VIP_REORDER_Y_G_SEL,
383 TVO_VIP_REORDER_CB_B_SEL);
384
Vincent Abriou1834b842016-02-01 17:51:33 +0100385 /* set clipping mode */
386 tvout_vip_set_clip_mode(tvout, TVO_VIP_HDMI, TVO_VIP_CLIP_DISABLED);
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200387
388 /* set round mode (rounded to 8-bit per component) */
Benjamin Gaignardca279602014-12-04 11:31:37 +0100389 tvout_vip_set_rnd(tvout, TVO_VIP_HDMI, TVO_VIP_RND_8BIT_ROUNDED);
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200390
391 if (of_device_is_compatible(node, "st,stih407-tvout")) {
392 /* set input video format */
Benjamin Gaignardca279602014-12-04 11:31:37 +0100393 tvout_vip_set_in_vid_fmt(tvout, tvo_in_vid_format,
394 TVO_IN_FMT_SIGNED);
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200395 sel_input_logic_inverted = true;
396 }
397
398 /* input selection */
Benjamin Gaignardca279602014-12-04 11:31:37 +0100399 tvout_vip_set_sel_input(tvout, TVO_VIP_HDMI, main_path,
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200400 sel_input_logic_inverted, STI_TVOUT_VIDEO_OUT_RGB);
401}
402
403/**
404 * Start HDF VIP and HD DAC
405 *
406 * @tvout: pointer on tvout structure
407 * @main_path: true if main path has to be used in the vip configuration
408 * else aux path is used.
409 */
410static void tvout_hda_start(struct sti_tvout *tvout, bool main_path)
411{
412 struct device_node *node = tvout->dev->of_node;
413 bool sel_input_logic_inverted = false;
Benjamin Gaignardca279602014-12-04 11:31:37 +0100414 u32 tvo_in_vid_format;
415 int val;
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200416
417 dev_dbg(tvout->dev, "%s\n", __func__);
418
Benjamin Gaignardca279602014-12-04 11:31:37 +0100419 if (main_path) {
Vincent Abriou503290c2016-01-28 13:08:48 +0100420 DRM_DEBUG_DRIVER("main vip for HDF\n");
421 /* Select the input sync for HD analog and HD DCS */
422 val = TVO_SYNC_MAIN_VTG_SET_REF | VTG_SYNC_ID_HDDCS;
423 val = val << TVO_SYNC_HD_DCS_SHIFT;
424 val |= TVO_SYNC_MAIN_VTG_SET_REF | VTG_SYNC_ID_HDF;
Benjamin Gaignardca279602014-12-04 11:31:37 +0100425 tvout_write(tvout, val, TVO_HD_SYNC_SEL);
426 tvo_in_vid_format = TVO_MAIN_IN_VID_FORMAT;
427 } else {
Vincent Abriou503290c2016-01-28 13:08:48 +0100428 DRM_DEBUG_DRIVER("aux vip for HDF\n");
429 /* Select the input sync for HD analog and HD DCS */
430 val = TVO_SYNC_AUX_VTG_SET_REF | VTG_SYNC_ID_HDDCS;
431 val = val << TVO_SYNC_HD_DCS_SHIFT;
432 val |= TVO_SYNC_AUX_VTG_SET_REF | VTG_SYNC_ID_HDF;
Benjamin Gaignardca279602014-12-04 11:31:37 +0100433 tvout_write(tvout, val, TVO_HD_SYNC_SEL);
434 tvo_in_vid_format = TVO_AUX_IN_VID_FORMAT;
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200435 }
436
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200437 /* set color channel order */
Benjamin Gaignardca279602014-12-04 11:31:37 +0100438 tvout_vip_set_color_order(tvout, TVO_VIP_HDF,
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200439 TVO_VIP_REORDER_CR_R_SEL,
440 TVO_VIP_REORDER_Y_G_SEL,
441 TVO_VIP_REORDER_CB_B_SEL);
442
Vincent Abriou1834b842016-02-01 17:51:33 +0100443 /* set clipping mode */
444 tvout_vip_set_clip_mode(tvout, TVO_VIP_HDF, TVO_VIP_CLIP_DISABLED);
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200445
446 /* set round mode (rounded to 10-bit per component) */
Benjamin Gaignardca279602014-12-04 11:31:37 +0100447 tvout_vip_set_rnd(tvout, TVO_VIP_HDF, TVO_VIP_RND_10BIT_ROUNDED);
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200448
449 if (of_device_is_compatible(node, "st,stih407-tvout")) {
450 /* set input video format */
Benjamin Gaignardca279602014-12-04 11:31:37 +0100451 tvout_vip_set_in_vid_fmt(tvout,
452 tvo_in_vid_format, TVO_IN_FMT_SIGNED);
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200453 sel_input_logic_inverted = true;
454 }
455
456 /* Input selection */
Benjamin Gaignardca279602014-12-04 11:31:37 +0100457 tvout_vip_set_sel_input(tvout, TVO_VIP_HDF, main_path,
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200458 sel_input_logic_inverted,
459 STI_TVOUT_VIDEO_OUT_YUV);
460
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200461 /* power up HD DAC */
462 tvout_write(tvout, 0, TVO_HD_DAC_CFG_OFF);
463}
464
465static void sti_tvout_encoder_dpms(struct drm_encoder *encoder, int mode)
466{
467}
468
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200469static void sti_tvout_encoder_mode_set(struct drm_encoder *encoder,
470 struct drm_display_mode *mode,
471 struct drm_display_mode *adjusted_mode)
472{
473}
474
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200475static void sti_tvout_encoder_destroy(struct drm_encoder *encoder)
476{
477 struct sti_tvout_encoder *sti_encoder = to_sti_tvout_encoder(encoder);
478
479 drm_encoder_cleanup(encoder);
480 kfree(sti_encoder);
481}
482
483static const struct drm_encoder_funcs sti_tvout_encoder_funcs = {
484 .destroy = sti_tvout_encoder_destroy,
485};
486
Bich Hemon05a142c2016-02-10 10:39:23 +0100487static void sti_dvo_encoder_enable(struct drm_encoder *encoder)
Benjamin Gaignardf32c4c52014-12-30 15:08:16 +0100488{
489 struct sti_tvout *tvout = to_sti_tvout(encoder);
490
Bich Hemon05a142c2016-02-10 10:39:23 +0100491 tvout_preformatter_set_matrix(tvout, &encoder->crtc->mode);
492
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200493 tvout_dvo_start(tvout, sti_crtc_is_main(encoder->crtc));
Benjamin Gaignardf32c4c52014-12-30 15:08:16 +0100494}
495
496static void sti_dvo_encoder_disable(struct drm_encoder *encoder)
497{
498 struct sti_tvout *tvout = to_sti_tvout(encoder);
499
500 /* Reset VIP register */
501 tvout_write(tvout, 0x0, TVO_VIP_DVO);
502}
503
504static const struct drm_encoder_helper_funcs sti_dvo_encoder_helper_funcs = {
505 .dpms = sti_tvout_encoder_dpms,
Benjamin Gaignardf32c4c52014-12-30 15:08:16 +0100506 .mode_set = sti_tvout_encoder_mode_set,
Bich Hemon05a142c2016-02-10 10:39:23 +0100507 .enable = sti_dvo_encoder_enable,
Benjamin Gaignardf32c4c52014-12-30 15:08:16 +0100508 .disable = sti_dvo_encoder_disable,
509};
510
511static struct drm_encoder *
512sti_tvout_create_dvo_encoder(struct drm_device *dev,
513 struct sti_tvout *tvout)
514{
515 struct sti_tvout_encoder *encoder;
516 struct drm_encoder *drm_encoder;
517
518 encoder = devm_kzalloc(tvout->dev, sizeof(*encoder), GFP_KERNEL);
519 if (!encoder)
520 return NULL;
521
522 encoder->tvout = tvout;
523
524 drm_encoder = (struct drm_encoder *)encoder;
525
526 drm_encoder->possible_crtcs = ENCODER_CRTC_MASK;
527 drm_encoder->possible_clones = 1 << 0;
528
529 drm_encoder_init(dev, drm_encoder,
Ville Syrjälä13a3d912015-12-09 16:20:18 +0200530 &sti_tvout_encoder_funcs, DRM_MODE_ENCODER_LVDS,
531 NULL);
Benjamin Gaignardf32c4c52014-12-30 15:08:16 +0100532
533 drm_encoder_helper_add(drm_encoder, &sti_dvo_encoder_helper_funcs);
534
535 return drm_encoder;
536}
537
Bich Hemon05a142c2016-02-10 10:39:23 +0100538static void sti_hda_encoder_enable(struct drm_encoder *encoder)
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200539{
540 struct sti_tvout *tvout = to_sti_tvout(encoder);
541
Bich Hemon05a142c2016-02-10 10:39:23 +0100542 tvout_preformatter_set_matrix(tvout, &encoder->crtc->mode);
543
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200544 tvout_hda_start(tvout, sti_crtc_is_main(encoder->crtc));
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200545}
546
547static void sti_hda_encoder_disable(struct drm_encoder *encoder)
548{
549 struct sti_tvout *tvout = to_sti_tvout(encoder);
550
551 /* reset VIP register */
552 tvout_write(tvout, 0x0, TVO_VIP_HDF);
553
554 /* power down HD DAC */
555 tvout_write(tvout, 1, TVO_HD_DAC_CFG_OFF);
556}
557
558static const struct drm_encoder_helper_funcs sti_hda_encoder_helper_funcs = {
559 .dpms = sti_tvout_encoder_dpms,
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200560 .mode_set = sti_tvout_encoder_mode_set,
Bich Hemon05a142c2016-02-10 10:39:23 +0100561 .commit = sti_hda_encoder_enable,
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200562 .disable = sti_hda_encoder_disable,
563};
564
565static struct drm_encoder *sti_tvout_create_hda_encoder(struct drm_device *dev,
566 struct sti_tvout *tvout)
567{
568 struct sti_tvout_encoder *encoder;
569 struct drm_encoder *drm_encoder;
570
571 encoder = devm_kzalloc(tvout->dev, sizeof(*encoder), GFP_KERNEL);
572 if (!encoder)
573 return NULL;
574
575 encoder->tvout = tvout;
576
577 drm_encoder = (struct drm_encoder *) encoder;
578
Benjamin Gaignard5e03abc2014-12-08 17:32:36 +0100579 drm_encoder->possible_crtcs = ENCODER_CRTC_MASK;
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200580 drm_encoder->possible_clones = 1 << 0;
581
582 drm_encoder_init(dev, drm_encoder,
Ville Syrjälä13a3d912015-12-09 16:20:18 +0200583 &sti_tvout_encoder_funcs, DRM_MODE_ENCODER_DAC, NULL);
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200584
585 drm_encoder_helper_add(drm_encoder, &sti_hda_encoder_helper_funcs);
586
587 return drm_encoder;
588}
589
Bich Hemon05a142c2016-02-10 10:39:23 +0100590static void sti_hdmi_encoder_enable(struct drm_encoder *encoder)
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200591{
592 struct sti_tvout *tvout = to_sti_tvout(encoder);
593
Bich Hemon05a142c2016-02-10 10:39:23 +0100594 tvout_preformatter_set_matrix(tvout, &encoder->crtc->mode);
595
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200596 tvout_hdmi_start(tvout, sti_crtc_is_main(encoder->crtc));
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200597}
598
599static void sti_hdmi_encoder_disable(struct drm_encoder *encoder)
600{
601 struct sti_tvout *tvout = to_sti_tvout(encoder);
602
603 /* reset VIP register */
604 tvout_write(tvout, 0x0, TVO_VIP_HDMI);
605}
606
607static const struct drm_encoder_helper_funcs sti_hdmi_encoder_helper_funcs = {
608 .dpms = sti_tvout_encoder_dpms,
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200609 .mode_set = sti_tvout_encoder_mode_set,
Bich Hemon05a142c2016-02-10 10:39:23 +0100610 .commit = sti_hdmi_encoder_enable,
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200611 .disable = sti_hdmi_encoder_disable,
612};
613
614static struct drm_encoder *sti_tvout_create_hdmi_encoder(struct drm_device *dev,
615 struct sti_tvout *tvout)
616{
617 struct sti_tvout_encoder *encoder;
618 struct drm_encoder *drm_encoder;
619
620 encoder = devm_kzalloc(tvout->dev, sizeof(*encoder), GFP_KERNEL);
621 if (!encoder)
622 return NULL;
623
624 encoder->tvout = tvout;
625
626 drm_encoder = (struct drm_encoder *) encoder;
627
Benjamin Gaignard5e03abc2014-12-08 17:32:36 +0100628 drm_encoder->possible_crtcs = ENCODER_CRTC_MASK;
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200629 drm_encoder->possible_clones = 1 << 1;
630
631 drm_encoder_init(dev, drm_encoder,
Ville Syrjälä13a3d912015-12-09 16:20:18 +0200632 &sti_tvout_encoder_funcs, DRM_MODE_ENCODER_TMDS, NULL);
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200633
634 drm_encoder_helper_add(drm_encoder, &sti_hdmi_encoder_helper_funcs);
635
636 return drm_encoder;
637}
638
639static void sti_tvout_create_encoders(struct drm_device *dev,
640 struct sti_tvout *tvout)
641{
642 tvout->hdmi = sti_tvout_create_hdmi_encoder(dev, tvout);
643 tvout->hda = sti_tvout_create_hda_encoder(dev, tvout);
Benjamin Gaignardf32c4c52014-12-30 15:08:16 +0100644 tvout->dvo = sti_tvout_create_dvo_encoder(dev, tvout);
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200645}
646
647static void sti_tvout_destroy_encoders(struct sti_tvout *tvout)
648{
649 if (tvout->hdmi)
650 drm_encoder_cleanup(tvout->hdmi);
651 tvout->hdmi = NULL;
652
653 if (tvout->hda)
654 drm_encoder_cleanup(tvout->hda);
655 tvout->hda = NULL;
656}
657
658static int sti_tvout_bind(struct device *dev, struct device *master, void *data)
659{
660 struct sti_tvout *tvout = dev_get_drvdata(dev);
661 struct drm_device *drm_dev = data;
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200662
663 tvout->drm_dev = drm_dev;
664
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200665 sti_tvout_create_encoders(drm_dev, tvout);
666
Benjamin Gaignard53bdcf52015-07-17 12:06:11 +0200667 return 0;
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200668}
669
670static void sti_tvout_unbind(struct device *dev, struct device *master,
671 void *data)
672{
Benjamin Gaignard53bdcf52015-07-17 12:06:11 +0200673 struct sti_tvout *tvout = dev_get_drvdata(dev);
674
675 sti_tvout_destroy_encoders(tvout);
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200676}
677
678static const struct component_ops sti_tvout_ops = {
679 .bind = sti_tvout_bind,
680 .unbind = sti_tvout_unbind,
681};
682
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200683static int sti_tvout_probe(struct platform_device *pdev)
684{
685 struct device *dev = &pdev->dev;
686 struct device_node *node = dev->of_node;
687 struct sti_tvout *tvout;
688 struct resource *res;
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200689
690 DRM_INFO("%s\n", __func__);
691
692 if (!node)
693 return -ENODEV;
694
695 tvout = devm_kzalloc(dev, sizeof(*tvout), GFP_KERNEL);
696 if (!tvout)
697 return -ENOMEM;
698
699 tvout->dev = dev;
700
701 /* get Memory ressources */
702 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "tvout-reg");
703 if (!res) {
704 DRM_ERROR("Invalid glue resource\n");
705 return -ENOMEM;
706 }
707 tvout->regs = devm_ioremap_nocache(dev, res->start, resource_size(res));
Wei Yongjun31f32a22014-08-26 12:17:36 +0200708 if (!tvout->regs)
709 return -ENOMEM;
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200710
711 /* get reset resources */
712 tvout->reset = devm_reset_control_get(dev, "tvout");
713 /* take tvout out of reset */
714 if (!IS_ERR(tvout->reset))
715 reset_control_deassert(tvout->reset);
716
717 platform_set_drvdata(pdev, tvout);
718
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200719 return component_add(dev, &sti_tvout_ops);
720}
721
722static int sti_tvout_remove(struct platform_device *pdev)
723{
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200724 component_del(&pdev->dev, &sti_tvout_ops);
725 return 0;
726}
727
Kiran Padwal8e932cf2014-08-26 12:25:24 +0200728static const struct of_device_id tvout_of_match[] = {
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200729 { .compatible = "st,stih416-tvout", },
730 { .compatible = "st,stih407-tvout", },
731 { /* end node */ }
732};
733MODULE_DEVICE_TABLE(of, tvout_of_match);
734
735struct platform_driver sti_tvout_driver = {
736 .driver = {
737 .name = "sti-tvout",
738 .owner = THIS_MODULE,
739 .of_match_table = tvout_of_match,
740 },
741 .probe = sti_tvout_probe,
742 .remove = sti_tvout_remove,
743};
744
Benjamin Gaignardcdfbff72014-07-30 19:26:17 +0200745MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
746MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
747MODULE_LICENSE("GPL");