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
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>
17 #include <drm/drm_crtc_helper.h>
20 #define TVO_CSC_MAIN_M0 0x000
21 #define TVO_CSC_MAIN_M1 0x004
22 #define TVO_CSC_MAIN_M2 0x008
23 #define TVO_CSC_MAIN_M3 0x00c
24 #define TVO_CSC_MAIN_M4 0x010
25 #define TVO_CSC_MAIN_M5 0x014
26 #define TVO_CSC_MAIN_M6 0x018
27 #define TVO_CSC_MAIN_M7 0x01c
28 #define TVO_MAIN_IN_VID_FORMAT 0x030
29 #define TVO_CSC_AUX_M0 0x100
30 #define TVO_CSC_AUX_M1 0x104
31 #define TVO_CSC_AUX_M2 0x108
32 #define TVO_CSC_AUX_M3 0x10c
33 #define TVO_CSC_AUX_M4 0x110
34 #define TVO_CSC_AUX_M5 0x114
35 #define TVO_CSC_AUX_M6 0x118
36 #define TVO_CSC_AUX_M7 0x11c
37 #define TVO_AUX_IN_VID_FORMAT 0x130
38 #define TVO_VIP_HDF 0x400
39 #define TVO_HD_SYNC_SEL 0x418
40 #define TVO_HD_DAC_CFG_OFF 0x420
41 #define TVO_VIP_HDMI 0x500
42 #define TVO_HDMI_FORCE_COLOR_0 0x504
43 #define TVO_HDMI_FORCE_COLOR_1 0x508
44 #define TVO_HDMI_CLIP_VALUE_B_CB 0x50c
45 #define TVO_HDMI_CLIP_VALUE_Y_G 0x510
46 #define TVO_HDMI_CLIP_VALUE_R_CR 0x514
47 #define TVO_HDMI_SYNC_SEL 0x518
48 #define TVO_HDMI_DFV_OBS 0x540
50 #define TVO_IN_FMT_SIGNED BIT(0)
51 #define TVO_SYNC_EXT BIT(4)
53 #define TVO_VIP_REORDER_R_SHIFT 24
54 #define TVO_VIP_REORDER_G_SHIFT 20
55 #define TVO_VIP_REORDER_B_SHIFT 16
56 #define TVO_VIP_REORDER_MASK 0x3
57 #define TVO_VIP_REORDER_Y_G_SEL 0
58 #define TVO_VIP_REORDER_CB_B_SEL 1
59 #define TVO_VIP_REORDER_CR_R_SEL 2
61 #define TVO_VIP_CLIP_SHIFT 8
62 #define TVO_VIP_CLIP_MASK 0x7
63 #define TVO_VIP_CLIP_DISABLED 0
64 #define TVO_VIP_CLIP_EAV_SAV 1
65 #define TVO_VIP_CLIP_LIMITED_RANGE_RGB_Y 2
66 #define TVO_VIP_CLIP_LIMITED_RANGE_CB_CR 3
67 #define TVO_VIP_CLIP_PROG_RANGE 4
69 #define TVO_VIP_RND_SHIFT 4
70 #define TVO_VIP_RND_MASK 0x3
71 #define TVO_VIP_RND_8BIT_ROUNDED 0
72 #define TVO_VIP_RND_10BIT_ROUNDED 1
73 #define TVO_VIP_RND_12BIT_ROUNDED 2
75 #define TVO_VIP_SEL_INPUT_MASK 0xf
76 #define TVO_VIP_SEL_INPUT_MAIN 0x0
77 #define TVO_VIP_SEL_INPUT_AUX 0x8
78 #define TVO_VIP_SEL_INPUT_FORCE_COLOR 0xf
79 #define TVO_VIP_SEL_INPUT_BYPASS_MASK 0x1
80 #define TVO_VIP_SEL_INPUT_BYPASSED 1
82 #define TVO_SYNC_MAIN_VTG_SET_REF 0x00
83 #define TVO_SYNC_MAIN_VTG_SET_1 0x01
84 #define TVO_SYNC_MAIN_VTG_SET_2 0x02
85 #define TVO_SYNC_MAIN_VTG_SET_3 0x03
86 #define TVO_SYNC_MAIN_VTG_SET_4 0x04
87 #define TVO_SYNC_MAIN_VTG_SET_5 0x05
88 #define TVO_SYNC_MAIN_VTG_SET_6 0x06
89 #define TVO_SYNC_AUX_VTG_SET_REF 0x10
90 #define TVO_SYNC_AUX_VTG_SET_1 0x11
91 #define TVO_SYNC_AUX_VTG_SET_2 0x12
92 #define TVO_SYNC_AUX_VTG_SET_3 0x13
93 #define TVO_SYNC_AUX_VTG_SET_4 0x14
94 #define TVO_SYNC_AUX_VTG_SET_5 0x15
95 #define TVO_SYNC_AUX_VTG_SET_6 0x16
97 #define TVO_SYNC_HD_DCS_SHIFT 8
99 #define ENCODER_MAIN_CRTC_MASK BIT(0)
101 /* enum listing the supported output data format */
102 enum sti_tvout_video_out_type {
103 STI_TVOUT_VIDEO_OUT_RGB,
104 STI_TVOUT_VIDEO_OUT_YUV,
109 struct drm_device *drm_dev;
111 struct reset_control *reset;
112 struct drm_encoder *hdmi;
113 struct drm_encoder *hda;
116 struct sti_tvout_encoder {
117 struct drm_encoder encoder;
118 struct sti_tvout *tvout;
121 #define to_sti_tvout_encoder(x) \
122 container_of(x, struct sti_tvout_encoder, encoder)
124 #define to_sti_tvout(x) to_sti_tvout_encoder(x)->tvout
126 /* preformatter conversion matrix */
127 static const u32 rgb_to_ycbcr_601[8] = {
128 0xF927082E, 0x04C9FEAB, 0x01D30964, 0xFA95FD3D,
129 0x0000082E, 0x00002000, 0x00002000, 0x00000000
132 /* 709 RGB to YCbCr */
133 static const u32 rgb_to_ycbcr_709[8] = {
134 0xF891082F, 0x0367FF40, 0x01280B71, 0xF9B1FE20,
135 0x0000082F, 0x00002000, 0x00002000, 0x00000000
138 static u32 tvout_read(struct sti_tvout *tvout, int offset)
140 return readl(tvout->regs + offset);
143 static void tvout_write(struct sti_tvout *tvout, u32 val, int offset)
145 writel(val, tvout->regs + offset);
149 * Set the clipping mode of a VIP
151 * @tvout: tvout structure
156 static void tvout_vip_set_color_order(struct sti_tvout *tvout,
157 u32 cr_r, u32 y_g, u32 cb_b)
159 u32 val = tvout_read(tvout, TVO_VIP_HDMI);
161 val &= ~(TVO_VIP_REORDER_MASK << TVO_VIP_REORDER_R_SHIFT);
162 val &= ~(TVO_VIP_REORDER_MASK << TVO_VIP_REORDER_G_SHIFT);
163 val &= ~(TVO_VIP_REORDER_MASK << TVO_VIP_REORDER_B_SHIFT);
164 val |= cr_r << TVO_VIP_REORDER_R_SHIFT;
165 val |= y_g << TVO_VIP_REORDER_G_SHIFT;
166 val |= cb_b << TVO_VIP_REORDER_B_SHIFT;
168 tvout_write(tvout, val, TVO_VIP_HDMI);
172 * Set the clipping mode of a VIP
174 * @tvout: tvout structure
175 * @range: clipping range
177 static void tvout_vip_set_clip_mode(struct sti_tvout *tvout, u32 range)
179 u32 val = tvout_read(tvout, TVO_VIP_HDMI);
181 val &= ~(TVO_VIP_CLIP_MASK << TVO_VIP_CLIP_SHIFT);
182 val |= range << TVO_VIP_CLIP_SHIFT;
183 tvout_write(tvout, val, TVO_VIP_HDMI);
187 * Set the rounded value of a VIP
189 * @tvout: tvout structure
190 * @rnd: rounded val per component
192 static void tvout_vip_set_rnd(struct sti_tvout *tvout, u32 rnd)
194 u32 val = tvout_read(tvout, TVO_VIP_HDMI);
196 val &= ~(TVO_VIP_RND_MASK << TVO_VIP_RND_SHIFT);
197 val |= rnd << TVO_VIP_RND_SHIFT;
198 tvout_write(tvout, val, TVO_VIP_HDMI);
202 * Select the VIP input
204 * @tvout: tvout structure
205 * @sel_input: selected_input (main/aux + conv)
207 static void tvout_vip_set_sel_input(struct sti_tvout *tvout,
209 bool sel_input_logic_inverted,
210 enum sti_tvout_video_out_type video_out)
213 u32 val = tvout_read(tvout, TVO_VIP_HDMI);
216 sel_input = TVO_VIP_SEL_INPUT_MAIN;
218 sel_input = TVO_VIP_SEL_INPUT_AUX;
221 case STI_TVOUT_VIDEO_OUT_RGB:
222 sel_input |= TVO_VIP_SEL_INPUT_BYPASSED;
224 case STI_TVOUT_VIDEO_OUT_YUV:
225 sel_input &= ~TVO_VIP_SEL_INPUT_BYPASSED;
229 /* on stih407 chip the sel_input bypass mode logic is inverted */
230 if (sel_input_logic_inverted)
231 sel_input = sel_input ^ TVO_VIP_SEL_INPUT_BYPASS_MASK;
233 val &= ~TVO_VIP_SEL_INPUT_MASK;
235 tvout_write(tvout, val, TVO_VIP_HDMI);
239 * Select the input video signed or unsigned
241 * @tvout: tvout structure
242 * @in_vid_signed: used video input format
244 static void tvout_vip_set_in_vid_fmt(struct sti_tvout *tvout, u32 in_vid_fmt)
246 u32 val = tvout_read(tvout, TVO_VIP_HDMI);
248 val &= ~TVO_IN_FMT_SIGNED;
250 tvout_write(tvout, val, TVO_MAIN_IN_VID_FORMAT);
254 * Start VIP block for HDMI output
256 * @tvout: pointer on tvout structure
257 * @main_path: true if main path has to be used in the vip configuration
258 * else aux path is used.
260 static void tvout_hdmi_start(struct sti_tvout *tvout, bool main_path)
262 struct device_node *node = tvout->dev->of_node;
263 bool sel_input_logic_inverted = false;
265 dev_dbg(tvout->dev, "%s\n", __func__);
268 DRM_DEBUG_DRIVER("main vip for hdmi\n");
269 /* select the input sync for hdmi = VTG set 1 */
270 tvout_write(tvout, TVO_SYNC_MAIN_VTG_SET_1, TVO_HDMI_SYNC_SEL);
272 DRM_DEBUG_DRIVER("aux vip for hdmi\n");
273 /* select the input sync for hdmi = VTG set 1 */
274 tvout_write(tvout, TVO_SYNC_AUX_VTG_SET_1, TVO_HDMI_SYNC_SEL);
277 /* set color channel order */
278 tvout_vip_set_color_order(tvout,
279 TVO_VIP_REORDER_CR_R_SEL,
280 TVO_VIP_REORDER_Y_G_SEL,
281 TVO_VIP_REORDER_CB_B_SEL);
283 /* set clipping mode (Limited range RGB/Y) */
284 tvout_vip_set_clip_mode(tvout, TVO_VIP_CLIP_LIMITED_RANGE_RGB_Y);
286 /* set round mode (rounded to 8-bit per component) */
287 tvout_vip_set_rnd(tvout, TVO_VIP_RND_8BIT_ROUNDED);
289 if (of_device_is_compatible(node, "st,stih407-tvout")) {
290 /* set input video format */
291 tvout_vip_set_in_vid_fmt(tvout->regs + TVO_MAIN_IN_VID_FORMAT,
293 sel_input_logic_inverted = true;
296 /* input selection */
297 tvout_vip_set_sel_input(tvout, main_path,
298 sel_input_logic_inverted, STI_TVOUT_VIDEO_OUT_RGB);
302 * Start HDF VIP and HD DAC
304 * @tvout: pointer on tvout structure
305 * @main_path: true if main path has to be used in the vip configuration
306 * else aux path is used.
308 static void tvout_hda_start(struct sti_tvout *tvout, bool main_path)
310 struct device_node *node = tvout->dev->of_node;
311 bool sel_input_logic_inverted = false;
313 dev_dbg(tvout->dev, "%s\n", __func__);
316 DRM_ERROR("HD Analog on aux not implemented\n");
320 DRM_DEBUG_DRIVER("main vip for HDF\n");
322 /* set color channel order */
323 tvout_vip_set_color_order(tvout->regs + TVO_VIP_HDF,
324 TVO_VIP_REORDER_CR_R_SEL,
325 TVO_VIP_REORDER_Y_G_SEL,
326 TVO_VIP_REORDER_CB_B_SEL);
328 /* set clipping mode (Limited range RGB/Y) */
329 tvout_vip_set_clip_mode(tvout->regs + TVO_VIP_HDF,
330 TVO_VIP_CLIP_LIMITED_RANGE_CB_CR);
332 /* set round mode (rounded to 10-bit per component) */
333 tvout_vip_set_rnd(tvout->regs + TVO_VIP_HDF, TVO_VIP_RND_10BIT_ROUNDED);
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_FMT_SIGNED);
338 sel_input_logic_inverted = true;
341 /* Input selection */
342 tvout_vip_set_sel_input(tvout->regs + TVO_VIP_HDF,
344 sel_input_logic_inverted,
345 STI_TVOUT_VIDEO_OUT_YUV);
347 /* select the input sync for HD analog = VTG set 3
348 * and HD DCS = VTG set 2 */
350 (TVO_SYNC_MAIN_VTG_SET_2 << TVO_SYNC_HD_DCS_SHIFT)
351 | TVO_SYNC_MAIN_VTG_SET_3,
354 /* power up HD DAC */
355 tvout_write(tvout, 0, TVO_HD_DAC_CFG_OFF);
358 static void sti_tvout_encoder_dpms(struct drm_encoder *encoder, int mode)
362 static bool sti_tvout_encoder_mode_fixup(struct drm_encoder *encoder,
363 const struct drm_display_mode *mode,
364 struct drm_display_mode *adjusted_mode)
369 static void sti_tvout_encoder_mode_set(struct drm_encoder *encoder,
370 struct drm_display_mode *mode,
371 struct drm_display_mode *adjusted_mode)
375 static void sti_tvout_encoder_prepare(struct drm_encoder *encoder)
379 static void sti_tvout_encoder_destroy(struct drm_encoder *encoder)
381 struct sti_tvout_encoder *sti_encoder = to_sti_tvout_encoder(encoder);
383 drm_encoder_cleanup(encoder);
387 static const struct drm_encoder_funcs sti_tvout_encoder_funcs = {
388 .destroy = sti_tvout_encoder_destroy,
391 static void sti_hda_encoder_commit(struct drm_encoder *encoder)
393 struct sti_tvout *tvout = to_sti_tvout(encoder);
395 tvout_hda_start(tvout, true);
398 static void sti_hda_encoder_disable(struct drm_encoder *encoder)
400 struct sti_tvout *tvout = to_sti_tvout(encoder);
402 /* reset VIP register */
403 tvout_write(tvout, 0x0, TVO_VIP_HDF);
405 /* power down HD DAC */
406 tvout_write(tvout, 1, TVO_HD_DAC_CFG_OFF);
409 static const struct drm_encoder_helper_funcs sti_hda_encoder_helper_funcs = {
410 .dpms = sti_tvout_encoder_dpms,
411 .mode_fixup = sti_tvout_encoder_mode_fixup,
412 .mode_set = sti_tvout_encoder_mode_set,
413 .prepare = sti_tvout_encoder_prepare,
414 .commit = sti_hda_encoder_commit,
415 .disable = sti_hda_encoder_disable,
418 static struct drm_encoder *sti_tvout_create_hda_encoder(struct drm_device *dev,
419 struct sti_tvout *tvout)
421 struct sti_tvout_encoder *encoder;
422 struct drm_encoder *drm_encoder;
424 encoder = devm_kzalloc(tvout->dev, sizeof(*encoder), GFP_KERNEL);
428 encoder->tvout = tvout;
430 drm_encoder = (struct drm_encoder *) encoder;
432 drm_encoder->possible_crtcs = ENCODER_MAIN_CRTC_MASK;
433 drm_encoder->possible_clones = 1 << 0;
435 drm_encoder_init(dev, drm_encoder,
436 &sti_tvout_encoder_funcs, DRM_MODE_ENCODER_DAC);
438 drm_encoder_helper_add(drm_encoder, &sti_hda_encoder_helper_funcs);
443 static void sti_hdmi_encoder_commit(struct drm_encoder *encoder)
445 struct sti_tvout *tvout = to_sti_tvout(encoder);
447 tvout_hdmi_start(tvout, true);
450 static void sti_hdmi_encoder_disable(struct drm_encoder *encoder)
452 struct sti_tvout *tvout = to_sti_tvout(encoder);
454 /* reset VIP register */
455 tvout_write(tvout, 0x0, TVO_VIP_HDMI);
458 static const struct drm_encoder_helper_funcs sti_hdmi_encoder_helper_funcs = {
459 .dpms = sti_tvout_encoder_dpms,
460 .mode_fixup = sti_tvout_encoder_mode_fixup,
461 .mode_set = sti_tvout_encoder_mode_set,
462 .prepare = sti_tvout_encoder_prepare,
463 .commit = sti_hdmi_encoder_commit,
464 .disable = sti_hdmi_encoder_disable,
467 static struct drm_encoder *sti_tvout_create_hdmi_encoder(struct drm_device *dev,
468 struct sti_tvout *tvout)
470 struct sti_tvout_encoder *encoder;
471 struct drm_encoder *drm_encoder;
473 encoder = devm_kzalloc(tvout->dev, sizeof(*encoder), GFP_KERNEL);
477 encoder->tvout = tvout;
479 drm_encoder = (struct drm_encoder *) encoder;
481 drm_encoder->possible_crtcs = ENCODER_MAIN_CRTC_MASK;
482 drm_encoder->possible_clones = 1 << 1;
484 drm_encoder_init(dev, drm_encoder,
485 &sti_tvout_encoder_funcs, DRM_MODE_ENCODER_TMDS);
487 drm_encoder_helper_add(drm_encoder, &sti_hdmi_encoder_helper_funcs);
492 static void sti_tvout_create_encoders(struct drm_device *dev,
493 struct sti_tvout *tvout)
495 tvout->hdmi = sti_tvout_create_hdmi_encoder(dev, tvout);
496 tvout->hda = sti_tvout_create_hda_encoder(dev, tvout);
499 static void sti_tvout_destroy_encoders(struct sti_tvout *tvout)
502 drm_encoder_cleanup(tvout->hdmi);
506 drm_encoder_cleanup(tvout->hda);
510 static int sti_tvout_bind(struct device *dev, struct device *master, void *data)
512 struct sti_tvout *tvout = dev_get_drvdata(dev);
513 struct drm_device *drm_dev = data;
517 tvout->drm_dev = drm_dev;
519 /* set preformatter matrix */
520 for (i = 0; i < 8; i++) {
521 tvout_write(tvout, rgb_to_ycbcr_601[i],
522 TVO_CSC_MAIN_M0 + (i * 4));
523 tvout_write(tvout, rgb_to_ycbcr_601[i],
524 TVO_CSC_AUX_M0 + (i * 4));
527 sti_tvout_create_encoders(drm_dev, tvout);
529 ret = component_bind_all(dev, drm_dev);
531 sti_tvout_destroy_encoders(tvout);
536 static void sti_tvout_unbind(struct device *dev, struct device *master,
542 static const struct component_ops sti_tvout_ops = {
543 .bind = sti_tvout_bind,
544 .unbind = sti_tvout_unbind,
547 static int compare_of(struct device *dev, void *data)
549 return dev->of_node == data;
552 static int sti_tvout_master_bind(struct device *dev)
557 static void sti_tvout_master_unbind(struct device *dev)
562 static const struct component_master_ops sti_tvout_master_ops = {
563 .bind = sti_tvout_master_bind,
564 .unbind = sti_tvout_master_unbind,
567 static int sti_tvout_probe(struct platform_device *pdev)
569 struct device *dev = &pdev->dev;
570 struct device_node *node = dev->of_node;
571 struct sti_tvout *tvout;
572 struct resource *res;
573 struct device_node *child_np;
574 struct component_match *match = NULL;
576 DRM_INFO("%s\n", __func__);
581 tvout = devm_kzalloc(dev, sizeof(*tvout), GFP_KERNEL);
587 /* get Memory ressources */
588 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "tvout-reg");
590 DRM_ERROR("Invalid glue resource\n");
593 tvout->regs = devm_ioremap_nocache(dev, res->start, resource_size(res));
594 if (IS_ERR(tvout->regs))
595 return PTR_ERR(tvout->regs);
597 /* get reset resources */
598 tvout->reset = devm_reset_control_get(dev, "tvout");
599 /* take tvout out of reset */
600 if (!IS_ERR(tvout->reset))
601 reset_control_deassert(tvout->reset);
603 platform_set_drvdata(pdev, tvout);
605 of_platform_populate(node, NULL, NULL, dev);
607 child_np = of_get_next_available_child(node, NULL);
610 component_match_add(dev, &match, compare_of, child_np);
611 of_node_put(child_np);
612 child_np = of_get_next_available_child(node, child_np);
615 component_master_add_with_match(dev, &sti_tvout_master_ops, match);
617 return component_add(dev, &sti_tvout_ops);
620 static int sti_tvout_remove(struct platform_device *pdev)
622 component_master_del(&pdev->dev, &sti_tvout_master_ops);
623 component_del(&pdev->dev, &sti_tvout_ops);
627 static struct of_device_id tvout_of_match[] = {
628 { .compatible = "st,stih416-tvout", },
629 { .compatible = "st,stih407-tvout", },
632 MODULE_DEVICE_TABLE(of, tvout_of_match);
634 struct platform_driver sti_tvout_driver = {
637 .owner = THIS_MODULE,
638 .of_match_table = tvout_of_match,
640 .probe = sti_tvout_probe,
641 .remove = sti_tvout_remove,
644 module_platform_driver(sti_tvout_driver);
646 MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
647 MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
648 MODULE_LICENSE("GPL");