2 * Copyright (C) STMicroelectronics SA 2014
3 * Authors: Benjamin Gaignard <benjamin.gaignard@st.com>
4 * Fabien Dessenne <fabien.dessenne@st.com>
5 * Vincent Abriou <vincent.abriou@st.com>
6 * for STMicroelectronics.
7 * License terms: GNU General Public License (GPL), version 2
10 #include <linux/module.h>
11 #include <linux/notifier.h>
12 #include <linux/platform_device.h>
18 #define VTG_MODE_MASTER 0
19 #define VTG_MODE_SLAVE_BY_EXT0 1
21 /* registers offset */
22 #define VTG_MODE 0x0000
23 #define VTG_CLKLN 0x0008
24 #define VTG_HLFLN 0x000C
25 #define VTG_DRST_AUTOC 0x0010
26 #define VTG_VID_TFO 0x0040
27 #define VTG_VID_TFS 0x0044
28 #define VTG_VID_BFO 0x0048
29 #define VTG_VID_BFS 0x004C
31 #define VTG_HOST_ITS 0x0078
32 #define VTG_HOST_ITS_BCLR 0x007C
33 #define VTG_HOST_ITM_BCLR 0x0088
34 #define VTG_HOST_ITM_BSET 0x008C
36 #define VTG_H_HD_1 0x00C0
37 #define VTG_TOP_V_VD_1 0x00C4
38 #define VTG_BOT_V_VD_1 0x00C8
39 #define VTG_TOP_V_HD_1 0x00CC
40 #define VTG_BOT_V_HD_1 0x00D0
42 #define VTG_H_HD_2 0x00E0
43 #define VTG_TOP_V_VD_2 0x00E4
44 #define VTG_BOT_V_VD_2 0x00E8
45 #define VTG_TOP_V_HD_2 0x00EC
46 #define VTG_BOT_V_HD_2 0x00F0
48 #define VTG_H_HD_3 0x0100
49 #define VTG_TOP_V_VD_3 0x0104
50 #define VTG_BOT_V_VD_3 0x0108
51 #define VTG_TOP_V_HD_3 0x010C
52 #define VTG_BOT_V_HD_3 0x0110
54 #define VTG_H_HD_4 0x0120
55 #define VTG_TOP_V_VD_4 0x0124
56 #define VTG_BOT_V_VD_4 0x0128
57 #define VTG_TOP_V_HD_4 0x012c
58 #define VTG_BOT_V_HD_4 0x0130
60 #define VTG_IRQ_BOTTOM BIT(0)
61 #define VTG_IRQ_TOP BIT(1)
62 #define VTG_IRQ_MASK (VTG_IRQ_TOP | VTG_IRQ_BOTTOM)
64 /* Delay introduced by the HDMI in nb of pixel */
65 #define HDMI_DELAY (5)
67 /* Delay introduced by the DVO in nb of pixel */
70 /* delay introduced by the Arbitrary Waveform Generator in nb of pixels */
71 #define AWG_DELAY_HD (-9)
72 #define AWG_DELAY_ED (-8)
73 #define AWG_DELAY_SD (-7)
75 LIST_HEAD(vtg_lookup
);
78 * STI VTG register offset structure
80 *@h_hd: stores the VTG_H_HD_x register offset
81 *@top_v_vd: stores the VTG_TOP_V_VD_x register offset
82 *@bot_v_vd: stores the VTG_BOT_V_VD_x register offset
83 *@top_v_hd: stores the VTG_TOP_V_HD_x register offset
84 *@bot_v_hd: stores the VTG_BOT_V_HD_x register offset
86 struct sti_vtg_regs_offs
{
94 #define VTG_MAX_SYNC_OUTPUT 4
95 static const struct sti_vtg_regs_offs vtg_regs_offs
[VTG_MAX_SYNC_OUTPUT
] = {
97 VTG_TOP_V_VD_1
, VTG_BOT_V_VD_1
, VTG_TOP_V_HD_1
, VTG_BOT_V_HD_1
},
99 VTG_TOP_V_VD_2
, VTG_BOT_V_VD_2
, VTG_TOP_V_HD_2
, VTG_BOT_V_HD_2
},
101 VTG_TOP_V_VD_3
, VTG_BOT_V_VD_3
, VTG_TOP_V_HD_3
, VTG_BOT_V_HD_3
},
103 VTG_TOP_V_VD_4
, VTG_BOT_V_VD_4
, VTG_TOP_V_HD_4
, VTG_BOT_V_HD_4
}
107 * STI VTG synchronisation parameters structure
109 *@hsync: sample number falling and rising edge
110 *@vsync_line_top: vertical top field line number falling and rising edge
111 *@vsync_line_bot: vertical bottom field line number falling and rising edge
112 *@vsync_off_top: vertical top field sample number rising and falling edge
113 *@vsync_off_bot: vertical bottom field sample number rising and falling edge
115 struct sti_vtg_sync_params
{
126 * @dev: pointer to device driver
128 * @regs: register mapping
129 * @sync_params: synchronisation parameters used to generate timings
131 * @irq_status: store the IRQ status value
132 * @notifier_list: notifier callback
133 * @crtc: the CRTC for vblank event
135 * @link: List node to link the structure in lookup list
139 struct device_node
*np
;
141 struct sti_vtg_sync_params sync_params
[VTG_MAX_SYNC_OUTPUT
];
144 struct raw_notifier_head notifier_list
;
145 struct drm_crtc
*crtc
;
146 struct sti_vtg
*slave
;
147 struct list_head link
;
150 static void vtg_register(struct sti_vtg
*vtg
)
152 list_add_tail(&vtg
->link
, &vtg_lookup
);
155 struct sti_vtg
*of_vtg_find(struct device_node
*np
)
159 list_for_each_entry(vtg
, &vtg_lookup
, link
) {
166 static void vtg_reset(struct sti_vtg
*vtg
)
168 /* reset slave and then master */
170 vtg_reset(vtg
->slave
);
172 writel(1, vtg
->regs
+ VTG_DRST_AUTOC
);
175 static void vtg_set_output_window(void __iomem
*regs
,
176 const struct drm_display_mode
*mode
)
178 u32 video_top_field_start
;
179 u32 video_top_field_stop
;
180 u32 video_bottom_field_start
;
181 u32 video_bottom_field_stop
;
182 u32 xstart
= sti_vtg_get_pixel_number(*mode
, 0);
183 u32 ystart
= sti_vtg_get_line_number(*mode
, 0);
184 u32 xstop
= sti_vtg_get_pixel_number(*mode
, mode
->hdisplay
- 1);
185 u32 ystop
= sti_vtg_get_line_number(*mode
, mode
->vdisplay
- 1);
187 /* Set output window to fit the display mode selected */
188 video_top_field_start
= (ystart
<< 16) | xstart
;
189 video_top_field_stop
= (ystop
<< 16) | xstop
;
191 /* Only progressive supported for now */
192 video_bottom_field_start
= video_top_field_start
;
193 video_bottom_field_stop
= video_top_field_stop
;
195 writel(video_top_field_start
, regs
+ VTG_VID_TFO
);
196 writel(video_top_field_stop
, regs
+ VTG_VID_TFS
);
197 writel(video_bottom_field_start
, regs
+ VTG_VID_BFO
);
198 writel(video_bottom_field_stop
, regs
+ VTG_VID_BFS
);
201 static void vtg_set_hsync_vsync_pos(struct sti_vtg_sync_params
*sync
,
203 const struct drm_display_mode
*mode
)
205 long clocksperline
, start
, stop
;
206 u32 risesync_top
, fallsync_top
;
207 u32 risesync_offs_top
, fallsync_offs_top
;
209 clocksperline
= mode
->htotal
;
211 /* Get the hsync position */
213 stop
= mode
->hsync_end
- mode
->hsync_start
;
219 start
+= clocksperline
;
220 else if (start
>= clocksperline
)
221 start
-= clocksperline
;
224 stop
+= clocksperline
;
225 else if (stop
>= clocksperline
)
226 stop
-= clocksperline
;
228 sync
->hsync
= (stop
<< 16) | start
;
230 /* Get the vsync position */
233 fallsync_top
= risesync_top
;
234 fallsync_top
+= mode
->vsync_end
- mode
->vsync_start
;
236 fallsync_offs_top
= (u32
)delay
;
237 risesync_offs_top
= (u32
)delay
;
239 risesync_top
= mode
->vtotal
;
240 fallsync_top
= mode
->vsync_end
- mode
->vsync_start
;
242 fallsync_offs_top
= clocksperline
+ delay
;
243 risesync_offs_top
= clocksperline
+ delay
;
246 sync
->vsync_line_top
= (fallsync_top
<< 16) | risesync_top
;
247 sync
->vsync_off_top
= (fallsync_offs_top
<< 16) | risesync_offs_top
;
249 /* Only progressive supported for now */
250 sync
->vsync_line_bot
= sync
->vsync_line_top
;
251 sync
->vsync_off_bot
= sync
->vsync_off_top
;
254 static void vtg_set_mode(struct sti_vtg
*vtg
,
256 struct sti_vtg_sync_params
*sync
,
257 const struct drm_display_mode
*mode
)
262 vtg_set_mode(vtg
->slave
, VTG_MODE_SLAVE_BY_EXT0
,
263 vtg
->sync_params
, mode
);
265 /* Set the number of clock cycles per line */
266 writel(mode
->htotal
, vtg
->regs
+ VTG_CLKLN
);
268 /* Set Half Line Per Field (only progressive supported for now) */
269 writel(mode
->vtotal
* 2, vtg
->regs
+ VTG_HLFLN
);
271 /* Program output window */
272 vtg_set_output_window(vtg
->regs
, mode
);
274 /* Set hsync and vsync position for HDMI */
275 vtg_set_hsync_vsync_pos(&sync
[VTG_SYNC_ID_HDMI
- 1], HDMI_DELAY
, mode
);
277 /* Set hsync and vsync position for HD DCS */
278 vtg_set_hsync_vsync_pos(&sync
[VTG_SYNC_ID_HDDCS
- 1], 0, mode
);
280 /* Set hsync and vsync position for HDF */
281 vtg_set_hsync_vsync_pos(&sync
[VTG_SYNC_ID_HDF
- 1], AWG_DELAY_HD
, mode
);
283 /* Set hsync and vsync position for DVO */
284 vtg_set_hsync_vsync_pos(&sync
[VTG_SYNC_ID_DVO
- 1], DVO_DELAY
, mode
);
286 /* Progam the syncs outputs */
287 for (i
= 0; i
< VTG_MAX_SYNC_OUTPUT
; i
++) {
288 writel(sync
[i
].hsync
,
289 vtg
->regs
+ vtg_regs_offs
[i
].h_hd
);
290 writel(sync
[i
].vsync_line_top
,
291 vtg
->regs
+ vtg_regs_offs
[i
].top_v_vd
);
292 writel(sync
[i
].vsync_line_bot
,
293 vtg
->regs
+ vtg_regs_offs
[i
].bot_v_vd
);
294 writel(sync
[i
].vsync_off_top
,
295 vtg
->regs
+ vtg_regs_offs
[i
].top_v_hd
);
296 writel(sync
[i
].vsync_off_bot
,
297 vtg
->regs
+ vtg_regs_offs
[i
].bot_v_hd
);
301 writel(type
, vtg
->regs
+ VTG_MODE
);
304 static void vtg_enable_irq(struct sti_vtg
*vtg
)
306 /* clear interrupt status and mask */
307 writel(0xFFFF, vtg
->regs
+ VTG_HOST_ITS_BCLR
);
308 writel(0xFFFF, vtg
->regs
+ VTG_HOST_ITM_BCLR
);
309 writel(VTG_IRQ_MASK
, vtg
->regs
+ VTG_HOST_ITM_BSET
);
312 void sti_vtg_set_config(struct sti_vtg
*vtg
,
313 const struct drm_display_mode
*mode
)
315 /* write configuration */
316 vtg_set_mode(vtg
, VTG_MODE_MASTER
, vtg
->sync_params
, mode
);
320 /* enable irq for the vtg vblank synchro */
322 vtg_enable_irq(vtg
->slave
);
328 * sti_vtg_get_line_number
330 * @mode: display mode to be used
333 * Return the line number according to the display mode taking
334 * into account the Sync and Back Porch information.
335 * Video frame line numbers start at 1, y starts at 0.
336 * In interlaced modes the start line is the field line number of the odd
337 * field, but y is still defined as a progressive frame.
339 u32
sti_vtg_get_line_number(struct drm_display_mode mode
, int y
)
341 u32 start_line
= mode
.vtotal
- mode
.vsync_start
+ 1;
343 if (mode
.flags
& DRM_MODE_FLAG_INTERLACE
)
346 return start_line
+ y
;
350 * sti_vtg_get_pixel_number
352 * @mode: display mode to be used
355 * Return the pixel number according to the display mode taking
356 * into account the Sync and Back Porch information.
357 * Pixels are counted from 0.
359 u32
sti_vtg_get_pixel_number(struct drm_display_mode mode
, int x
)
361 return mode
.htotal
- mode
.hsync_start
+ x
;
364 int sti_vtg_register_client(struct sti_vtg
*vtg
, struct notifier_block
*nb
,
365 struct drm_crtc
*crtc
)
368 return sti_vtg_register_client(vtg
->slave
, nb
, crtc
);
371 return raw_notifier_chain_register(&vtg
->notifier_list
, nb
);
374 int sti_vtg_unregister_client(struct sti_vtg
*vtg
, struct notifier_block
*nb
)
377 return sti_vtg_unregister_client(vtg
->slave
, nb
);
379 return raw_notifier_chain_unregister(&vtg
->notifier_list
, nb
);
382 static irqreturn_t
vtg_irq_thread(int irq
, void *arg
)
384 struct sti_vtg
*vtg
= arg
;
387 event
= (vtg
->irq_status
& VTG_IRQ_TOP
) ?
388 VTG_TOP_FIELD_EVENT
: VTG_BOTTOM_FIELD_EVENT
;
390 raw_notifier_call_chain(&vtg
->notifier_list
, event
, vtg
->crtc
);
395 static irqreturn_t
vtg_irq(int irq
, void *arg
)
397 struct sti_vtg
*vtg
= arg
;
399 vtg
->irq_status
= readl(vtg
->regs
+ VTG_HOST_ITS
);
401 writel(vtg
->irq_status
, vtg
->regs
+ VTG_HOST_ITS_BCLR
);
403 /* force sync bus write */
404 readl(vtg
->regs
+ VTG_HOST_ITS
);
406 return IRQ_WAKE_THREAD
;
409 static int vtg_probe(struct platform_device
*pdev
)
411 struct device
*dev
= &pdev
->dev
;
412 struct device_node
*np
;
414 struct resource
*res
;
417 vtg
= devm_kzalloc(dev
, sizeof(*vtg
), GFP_KERNEL
);
422 vtg
->np
= pdev
->dev
.of_node
;
424 /* Get Memory ressources */
425 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
427 DRM_ERROR("Get memory resource failed\n");
430 vtg
->regs
= devm_ioremap_nocache(dev
, res
->start
, resource_size(res
));
432 np
= of_parse_phandle(pdev
->dev
.of_node
, "st,slave", 0);
434 vtg
->slave
= of_vtg_find(np
);
437 return -EPROBE_DEFER
;
439 vtg
->irq
= platform_get_irq(pdev
, 0);
440 if (IS_ERR_VALUE(vtg
->irq
)) {
441 DRM_ERROR("Failed to get VTG interrupt\n");
445 RAW_INIT_NOTIFIER_HEAD(&vtg
->notifier_list
);
447 ret
= devm_request_threaded_irq(dev
, vtg
->irq
, vtg_irq
,
448 vtg_irq_thread
, IRQF_ONESHOT
,
450 if (IS_ERR_VALUE(ret
)) {
451 DRM_ERROR("Failed to register VTG interrupt\n");
457 platform_set_drvdata(pdev
, vtg
);
459 DRM_INFO("%s %s\n", __func__
, dev_name(vtg
->dev
));
464 static int vtg_remove(struct platform_device
*pdev
)
469 static const struct of_device_id vtg_of_match
[] = {
470 { .compatible
= "st,vtg", },
473 MODULE_DEVICE_TABLE(of
, vtg_of_match
);
475 struct platform_driver sti_vtg_driver
= {
478 .owner
= THIS_MODULE
,
479 .of_match_table
= vtg_of_match
,
482 .remove
= vtg_remove
,
485 MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
486 MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
487 MODULE_LICENSE("GPL");