1e2842570SBenjamin Gaignard // SPDX-License-Identifier: GPL-2.0
2f2cb3148SBenjamin Gaignard /*
3f2cb3148SBenjamin Gaignard * Copyright (C) STMicroelectronics SA 2014
4f2cb3148SBenjamin Gaignard * Authors: Benjamin Gaignard <benjamin.gaignard@st.com>
5f2cb3148SBenjamin Gaignard * Fabien Dessenne <fabien.dessenne@st.com>
6f2cb3148SBenjamin Gaignard * Vincent Abriou <vincent.abriou@st.com>
7f2cb3148SBenjamin Gaignard * for STMicroelectronics.
8f2cb3148SBenjamin Gaignard */
9f2cb3148SBenjamin Gaignard
10f2cb3148SBenjamin Gaignard #include <linux/module.h>
115e2f97a9SSam Ravnborg #include <linux/io.h>
12f2cb3148SBenjamin Gaignard #include <linux/notifier.h>
13cc6b741cSBenjamin Gaignard #include <linux/of_platform.h>
14f2cb3148SBenjamin Gaignard #include <linux/platform_device.h>
15f2cb3148SBenjamin Gaignard
165e2f97a9SSam Ravnborg #include <drm/drm_modes.h>
175e2f97a9SSam Ravnborg #include <drm/drm_print.h>
18f2cb3148SBenjamin Gaignard
19bdfd36efSVille Syrjälä #include "sti_drv.h"
20f2cb3148SBenjamin Gaignard #include "sti_vtg.h"
21f2cb3148SBenjamin Gaignard
22503290ceSVincent Abriou #define VTG_MODE_MASTER 0
23f2cb3148SBenjamin Gaignard
24f2cb3148SBenjamin Gaignard /* registers offset */
25f2cb3148SBenjamin Gaignard #define VTG_MODE 0x0000
26f2cb3148SBenjamin Gaignard #define VTG_CLKLN 0x0008
27f2cb3148SBenjamin Gaignard #define VTG_HLFLN 0x000C
28f2cb3148SBenjamin Gaignard #define VTG_DRST_AUTOC 0x0010
29f2cb3148SBenjamin Gaignard #define VTG_VID_TFO 0x0040
30f2cb3148SBenjamin Gaignard #define VTG_VID_TFS 0x0044
31f2cb3148SBenjamin Gaignard #define VTG_VID_BFO 0x0048
32f2cb3148SBenjamin Gaignard #define VTG_VID_BFS 0x004C
33f2cb3148SBenjamin Gaignard
34f2cb3148SBenjamin Gaignard #define VTG_HOST_ITS 0x0078
35f2cb3148SBenjamin Gaignard #define VTG_HOST_ITS_BCLR 0x007C
36f2cb3148SBenjamin Gaignard #define VTG_HOST_ITM_BCLR 0x0088
37f2cb3148SBenjamin Gaignard #define VTG_HOST_ITM_BSET 0x008C
38f2cb3148SBenjamin Gaignard
39f2cb3148SBenjamin Gaignard #define VTG_H_HD_1 0x00C0
40f2cb3148SBenjamin Gaignard #define VTG_TOP_V_VD_1 0x00C4
41f2cb3148SBenjamin Gaignard #define VTG_BOT_V_VD_1 0x00C8
42f2cb3148SBenjamin Gaignard #define VTG_TOP_V_HD_1 0x00CC
43f2cb3148SBenjamin Gaignard #define VTG_BOT_V_HD_1 0x00D0
44f2cb3148SBenjamin Gaignard
45f2cb3148SBenjamin Gaignard #define VTG_H_HD_2 0x00E0
46f2cb3148SBenjamin Gaignard #define VTG_TOP_V_VD_2 0x00E4
47f2cb3148SBenjamin Gaignard #define VTG_BOT_V_VD_2 0x00E8
48f2cb3148SBenjamin Gaignard #define VTG_TOP_V_HD_2 0x00EC
49f2cb3148SBenjamin Gaignard #define VTG_BOT_V_HD_2 0x00F0
50f2cb3148SBenjamin Gaignard
51f2cb3148SBenjamin Gaignard #define VTG_H_HD_3 0x0100
52f2cb3148SBenjamin Gaignard #define VTG_TOP_V_VD_3 0x0104
53f2cb3148SBenjamin Gaignard #define VTG_BOT_V_VD_3 0x0108
54f2cb3148SBenjamin Gaignard #define VTG_TOP_V_HD_3 0x010C
55f2cb3148SBenjamin Gaignard #define VTG_BOT_V_HD_3 0x0110
56f2cb3148SBenjamin Gaignard
577f2d479cSBenjamin Gaignard #define VTG_H_HD_4 0x0120
587f2d479cSBenjamin Gaignard #define VTG_TOP_V_VD_4 0x0124
597f2d479cSBenjamin Gaignard #define VTG_BOT_V_VD_4 0x0128
607f2d479cSBenjamin Gaignard #define VTG_TOP_V_HD_4 0x012c
617f2d479cSBenjamin Gaignard #define VTG_BOT_V_HD_4 0x0130
627f2d479cSBenjamin Gaignard
63f2cb3148SBenjamin Gaignard #define VTG_IRQ_BOTTOM BIT(0)
64f2cb3148SBenjamin Gaignard #define VTG_IRQ_TOP BIT(1)
65f2cb3148SBenjamin Gaignard #define VTG_IRQ_MASK (VTG_IRQ_TOP | VTG_IRQ_BOTTOM)
66f2cb3148SBenjamin Gaignard
677f2d479cSBenjamin Gaignard /* Delay introduced by the HDMI in nb of pixel */
688eba2703SVincent Abriou #define HDMI_DELAY (5)
697f2d479cSBenjamin Gaignard
709a024948SBich Hemon /* Delay introduced by the DVO in nb of pixel */
714d703770SBich Hemon #define DVO_DELAY (7)
729a024948SBich Hemon
73f2cb3148SBenjamin Gaignard /* delay introduced by the Arbitrary Waveform Generator in nb of pixels */
74f2cb3148SBenjamin Gaignard #define AWG_DELAY_HD (-9)
75f2cb3148SBenjamin Gaignard #define AWG_DELAY_ED (-8)
76f2cb3148SBenjamin Gaignard #define AWG_DELAY_SD (-7)
77f2cb3148SBenjamin Gaignard
78503290ceSVincent Abriou /*
79503290ceSVincent Abriou * STI VTG register offset structure
80503290ceSVincent Abriou *
81503290ceSVincent Abriou *@h_hd: stores the VTG_H_HD_x register offset
82503290ceSVincent Abriou *@top_v_vd: stores the VTG_TOP_V_VD_x register offset
83503290ceSVincent Abriou *@bot_v_vd: stores the VTG_BOT_V_VD_x register offset
84503290ceSVincent Abriou *@top_v_hd: stores the VTG_TOP_V_HD_x register offset
85503290ceSVincent Abriou *@bot_v_hd: stores the VTG_BOT_V_HD_x register offset
86503290ceSVincent Abriou */
87503290ceSVincent Abriou struct sti_vtg_regs_offs {
88503290ceSVincent Abriou u32 h_hd;
89503290ceSVincent Abriou u32 top_v_vd;
90503290ceSVincent Abriou u32 bot_v_vd;
91503290ceSVincent Abriou u32 top_v_hd;
92503290ceSVincent Abriou u32 bot_v_hd;
93503290ceSVincent Abriou };
94503290ceSVincent Abriou
95503290ceSVincent Abriou #define VTG_MAX_SYNC_OUTPUT 4
96503290ceSVincent Abriou static const struct sti_vtg_regs_offs vtg_regs_offs[VTG_MAX_SYNC_OUTPUT] = {
97503290ceSVincent Abriou { VTG_H_HD_1,
98503290ceSVincent Abriou VTG_TOP_V_VD_1, VTG_BOT_V_VD_1, VTG_TOP_V_HD_1, VTG_BOT_V_HD_1 },
99503290ceSVincent Abriou { VTG_H_HD_2,
100503290ceSVincent Abriou VTG_TOP_V_VD_2, VTG_BOT_V_VD_2, VTG_TOP_V_HD_2, VTG_BOT_V_HD_2 },
101503290ceSVincent Abriou { VTG_H_HD_3,
102503290ceSVincent Abriou VTG_TOP_V_VD_3, VTG_BOT_V_VD_3, VTG_TOP_V_HD_3, VTG_BOT_V_HD_3 },
103503290ceSVincent Abriou { VTG_H_HD_4,
104503290ceSVincent Abriou VTG_TOP_V_VD_4, VTG_BOT_V_VD_4, VTG_TOP_V_HD_4, VTG_BOT_V_HD_4 }
105503290ceSVincent Abriou };
106503290ceSVincent Abriou
107503290ceSVincent Abriou /*
108503290ceSVincent Abriou * STI VTG synchronisation parameters structure
109503290ceSVincent Abriou *
110503290ceSVincent Abriou *@hsync: sample number falling and rising edge
111503290ceSVincent Abriou *@vsync_line_top: vertical top field line number falling and rising edge
112503290ceSVincent Abriou *@vsync_line_bot: vertical bottom field line number falling and rising edge
113503290ceSVincent Abriou *@vsync_off_top: vertical top field sample number rising and falling edge
114503290ceSVincent Abriou *@vsync_off_bot: vertical bottom field sample number rising and falling edge
115503290ceSVincent Abriou */
116503290ceSVincent Abriou struct sti_vtg_sync_params {
117503290ceSVincent Abriou u32 hsync;
118503290ceSVincent Abriou u32 vsync_line_top;
119503290ceSVincent Abriou u32 vsync_line_bot;
120503290ceSVincent Abriou u32 vsync_off_top;
121503290ceSVincent Abriou u32 vsync_off_bot;
122503290ceSVincent Abriou };
123503290ceSVincent Abriou
1245dec1affSBenjamin Gaignard /*
125f2cb3148SBenjamin Gaignard * STI VTG structure
126f2cb3148SBenjamin Gaignard *
127503290ceSVincent Abriou * @regs: register mapping
128503290ceSVincent Abriou * @sync_params: synchronisation parameters used to generate timings
129f2cb3148SBenjamin Gaignard * @irq: VTG irq
130503290ceSVincent Abriou * @irq_status: store the IRQ status value
131f2cb3148SBenjamin Gaignard * @notifier_list: notifier callback
1322388693eSThierry Reding * @crtc: the CRTC for vblank event
133f2cb3148SBenjamin Gaignard */
134f2cb3148SBenjamin Gaignard struct sti_vtg {
135f2cb3148SBenjamin Gaignard void __iomem *regs;
136503290ceSVincent Abriou struct sti_vtg_sync_params sync_params[VTG_MAX_SYNC_OUTPUT];
137f2cb3148SBenjamin Gaignard int irq;
138f2cb3148SBenjamin Gaignard u32 irq_status;
139f2cb3148SBenjamin Gaignard struct raw_notifier_head notifier_list;
1402388693eSThierry Reding struct drm_crtc *crtc;
141f2cb3148SBenjamin Gaignard };
142f2cb3148SBenjamin Gaignard
of_vtg_find(struct device_node * np)143f2cb3148SBenjamin Gaignard struct sti_vtg *of_vtg_find(struct device_node *np)
144f2cb3148SBenjamin Gaignard {
145cc6b741cSBenjamin Gaignard struct platform_device *pdev;
146f2cb3148SBenjamin Gaignard
147cc6b741cSBenjamin Gaignard pdev = of_find_device_by_node(np);
148cc6b741cSBenjamin Gaignard if (!pdev)
149f2cb3148SBenjamin Gaignard return NULL;
150cc6b741cSBenjamin Gaignard
151cc6b741cSBenjamin Gaignard return (struct sti_vtg *)platform_get_drvdata(pdev);
152f2cb3148SBenjamin Gaignard }
153f2cb3148SBenjamin Gaignard
vtg_reset(struct sti_vtg * vtg)154f2cb3148SBenjamin Gaignard static void vtg_reset(struct sti_vtg *vtg)
155f2cb3148SBenjamin Gaignard {
156f2cb3148SBenjamin Gaignard writel(1, vtg->regs + VTG_DRST_AUTOC);
157f2cb3148SBenjamin Gaignard }
158f2cb3148SBenjamin Gaignard
vtg_set_output_window(void __iomem * regs,const struct drm_display_mode * mode)1598eba2703SVincent Abriou static void vtg_set_output_window(void __iomem *regs,
1608eba2703SVincent Abriou const struct drm_display_mode *mode)
1618eba2703SVincent Abriou {
1628eba2703SVincent Abriou u32 video_top_field_start;
1638eba2703SVincent Abriou u32 video_top_field_stop;
1648eba2703SVincent Abriou u32 video_bottom_field_start;
1658eba2703SVincent Abriou u32 video_bottom_field_stop;
1668eba2703SVincent Abriou u32 xstart = sti_vtg_get_pixel_number(*mode, 0);
1678eba2703SVincent Abriou u32 ystart = sti_vtg_get_line_number(*mode, 0);
1688eba2703SVincent Abriou u32 xstop = sti_vtg_get_pixel_number(*mode, mode->hdisplay - 1);
1698eba2703SVincent Abriou u32 ystop = sti_vtg_get_line_number(*mode, mode->vdisplay - 1);
1708eba2703SVincent Abriou
1718eba2703SVincent Abriou /* Set output window to fit the display mode selected */
1728eba2703SVincent Abriou video_top_field_start = (ystart << 16) | xstart;
1738eba2703SVincent Abriou video_top_field_stop = (ystop << 16) | xstop;
1748eba2703SVincent Abriou
1758eba2703SVincent Abriou /* Only progressive supported for now */
1768eba2703SVincent Abriou video_bottom_field_start = video_top_field_start;
1778eba2703SVincent Abriou video_bottom_field_stop = video_top_field_stop;
1788eba2703SVincent Abriou
1798eba2703SVincent Abriou writel(video_top_field_start, regs + VTG_VID_TFO);
1808eba2703SVincent Abriou writel(video_top_field_stop, regs + VTG_VID_TFS);
1818eba2703SVincent Abriou writel(video_bottom_field_start, regs + VTG_VID_BFO);
1828eba2703SVincent Abriou writel(video_bottom_field_stop, regs + VTG_VID_BFS);
1838eba2703SVincent Abriou }
1848eba2703SVincent Abriou
vtg_set_hsync_vsync_pos(struct sti_vtg_sync_params * sync,int delay,const struct drm_display_mode * mode)185503290ceSVincent Abriou static void vtg_set_hsync_vsync_pos(struct sti_vtg_sync_params *sync,
186503290ceSVincent Abriou int delay,
187503290ceSVincent Abriou const struct drm_display_mode *mode)
188f2cb3148SBenjamin Gaignard {
189503290ceSVincent Abriou long clocksperline, start, stop;
190503290ceSVincent Abriou u32 risesync_top, fallsync_top;
191503290ceSVincent Abriou u32 risesync_offs_top, fallsync_offs_top;
192503290ceSVincent Abriou
193503290ceSVincent Abriou clocksperline = mode->htotal;
194503290ceSVincent Abriou
195503290ceSVincent Abriou /* Get the hsync position */
196503290ceSVincent Abriou start = 0;
197503290ceSVincent Abriou stop = mode->hsync_end - mode->hsync_start;
198503290ceSVincent Abriou
199503290ceSVincent Abriou start += delay;
200503290ceSVincent Abriou stop += delay;
201503290ceSVincent Abriou
202503290ceSVincent Abriou if (start < 0)
203503290ceSVincent Abriou start += clocksperline;
204503290ceSVincent Abriou else if (start >= clocksperline)
205503290ceSVincent Abriou start -= clocksperline;
206503290ceSVincent Abriou
207503290ceSVincent Abriou if (stop < 0)
208503290ceSVincent Abriou stop += clocksperline;
209503290ceSVincent Abriou else if (stop >= clocksperline)
210503290ceSVincent Abriou stop -= clocksperline;
211503290ceSVincent Abriou
212503290ceSVincent Abriou sync->hsync = (stop << 16) | start;
213503290ceSVincent Abriou
214503290ceSVincent Abriou /* Get the vsync position */
215503290ceSVincent Abriou if (delay >= 0) {
216503290ceSVincent Abriou risesync_top = 1;
217503290ceSVincent Abriou fallsync_top = risesync_top;
218503290ceSVincent Abriou fallsync_top += mode->vsync_end - mode->vsync_start;
219503290ceSVincent Abriou
220503290ceSVincent Abriou fallsync_offs_top = (u32)delay;
221503290ceSVincent Abriou risesync_offs_top = (u32)delay;
222503290ceSVincent Abriou } else {
223503290ceSVincent Abriou risesync_top = mode->vtotal;
224503290ceSVincent Abriou fallsync_top = mode->vsync_end - mode->vsync_start;
225503290ceSVincent Abriou
226503290ceSVincent Abriou fallsync_offs_top = clocksperline + delay;
227503290ceSVincent Abriou risesync_offs_top = clocksperline + delay;
228503290ceSVincent Abriou }
229503290ceSVincent Abriou
230503290ceSVincent Abriou sync->vsync_line_top = (fallsync_top << 16) | risesync_top;
231503290ceSVincent Abriou sync->vsync_off_top = (fallsync_offs_top << 16) | risesync_offs_top;
232503290ceSVincent Abriou
233503290ceSVincent Abriou /* Only progressive supported for now */
234503290ceSVincent Abriou sync->vsync_line_bot = sync->vsync_line_top;
235503290ceSVincent Abriou sync->vsync_off_bot = sync->vsync_off_top;
236503290ceSVincent Abriou }
237503290ceSVincent Abriou
vtg_set_mode(struct sti_vtg * vtg,int type,struct sti_vtg_sync_params * sync,const struct drm_display_mode * mode)238503290ceSVincent Abriou static void vtg_set_mode(struct sti_vtg *vtg,
239503290ceSVincent Abriou int type,
240503290ceSVincent Abriou struct sti_vtg_sync_params *sync,
241503290ceSVincent Abriou const struct drm_display_mode *mode)
242503290ceSVincent Abriou {
243503290ceSVincent Abriou unsigned int i;
244f2cb3148SBenjamin Gaignard
2458eba2703SVincent Abriou /* Set the number of clock cycles per line */
246f2cb3148SBenjamin Gaignard writel(mode->htotal, vtg->regs + VTG_CLKLN);
2478eba2703SVincent Abriou
2488eba2703SVincent Abriou /* Set Half Line Per Field (only progressive supported for now) */
249f2cb3148SBenjamin Gaignard writel(mode->vtotal * 2, vtg->regs + VTG_HLFLN);
250f2cb3148SBenjamin Gaignard
2518eba2703SVincent Abriou /* Program output window */
2528eba2703SVincent Abriou vtg_set_output_window(vtg->regs, mode);
253f2cb3148SBenjamin Gaignard
254503290ceSVincent Abriou /* Set hsync and vsync position for HDMI */
255503290ceSVincent Abriou vtg_set_hsync_vsync_pos(&sync[VTG_SYNC_ID_HDMI - 1], HDMI_DELAY, mode);
256f2cb3148SBenjamin Gaignard
257503290ceSVincent Abriou /* Set hsync and vsync position for HD DCS */
258503290ceSVincent Abriou vtg_set_hsync_vsync_pos(&sync[VTG_SYNC_ID_HDDCS - 1], 0, mode);
259c58d6d1bSVincent Abriou
260503290ceSVincent Abriou /* Set hsync and vsync position for HDF */
261503290ceSVincent Abriou vtg_set_hsync_vsync_pos(&sync[VTG_SYNC_ID_HDF - 1], AWG_DELAY_HD, mode);
262f2cb3148SBenjamin Gaignard
263503290ceSVincent Abriou /* Set hsync and vsync position for DVO */
2649a024948SBich Hemon vtg_set_hsync_vsync_pos(&sync[VTG_SYNC_ID_DVO - 1], DVO_DELAY, mode);
2657f2d479cSBenjamin Gaignard
266503290ceSVincent Abriou /* Progam the syncs outputs */
267503290ceSVincent Abriou for (i = 0; i < VTG_MAX_SYNC_OUTPUT ; i++) {
268503290ceSVincent Abriou writel(sync[i].hsync,
269503290ceSVincent Abriou vtg->regs + vtg_regs_offs[i].h_hd);
270503290ceSVincent Abriou writel(sync[i].vsync_line_top,
271503290ceSVincent Abriou vtg->regs + vtg_regs_offs[i].top_v_vd);
272503290ceSVincent Abriou writel(sync[i].vsync_line_bot,
273503290ceSVincent Abriou vtg->regs + vtg_regs_offs[i].bot_v_vd);
274503290ceSVincent Abriou writel(sync[i].vsync_off_top,
275503290ceSVincent Abriou vtg->regs + vtg_regs_offs[i].top_v_hd);
276503290ceSVincent Abriou writel(sync[i].vsync_off_bot,
277503290ceSVincent Abriou vtg->regs + vtg_regs_offs[i].bot_v_hd);
278503290ceSVincent Abriou }
2797f2d479cSBenjamin Gaignard
280f2cb3148SBenjamin Gaignard /* mode */
281f2cb3148SBenjamin Gaignard writel(type, vtg->regs + VTG_MODE);
282f2cb3148SBenjamin Gaignard }
283f2cb3148SBenjamin Gaignard
vtg_enable_irq(struct sti_vtg * vtg)284f2cb3148SBenjamin Gaignard static void vtg_enable_irq(struct sti_vtg *vtg)
285f2cb3148SBenjamin Gaignard {
286f2cb3148SBenjamin Gaignard /* clear interrupt status and mask */
287f2cb3148SBenjamin Gaignard writel(0xFFFF, vtg->regs + VTG_HOST_ITS_BCLR);
288f2cb3148SBenjamin Gaignard writel(0xFFFF, vtg->regs + VTG_HOST_ITM_BCLR);
289f2cb3148SBenjamin Gaignard writel(VTG_IRQ_MASK, vtg->regs + VTG_HOST_ITM_BSET);
290f2cb3148SBenjamin Gaignard }
291f2cb3148SBenjamin Gaignard
sti_vtg_set_config(struct sti_vtg * vtg,const struct drm_display_mode * mode)292f2cb3148SBenjamin Gaignard void sti_vtg_set_config(struct sti_vtg *vtg,
293f2cb3148SBenjamin Gaignard const struct drm_display_mode *mode)
294f2cb3148SBenjamin Gaignard {
295f2cb3148SBenjamin Gaignard /* write configuration */
296503290ceSVincent Abriou vtg_set_mode(vtg, VTG_MODE_MASTER, vtg->sync_params, mode);
297f2cb3148SBenjamin Gaignard
298f2cb3148SBenjamin Gaignard vtg_reset(vtg);
299f2cb3148SBenjamin Gaignard
300f2cb3148SBenjamin Gaignard vtg_enable_irq(vtg);
301f2cb3148SBenjamin Gaignard }
302f2cb3148SBenjamin Gaignard
303f2cb3148SBenjamin Gaignard /**
304f2cb3148SBenjamin Gaignard * sti_vtg_get_line_number
305f2cb3148SBenjamin Gaignard *
306f2cb3148SBenjamin Gaignard * @mode: display mode to be used
307f2cb3148SBenjamin Gaignard * @y: line
308f2cb3148SBenjamin Gaignard *
309f2cb3148SBenjamin Gaignard * Return the line number according to the display mode taking
310f2cb3148SBenjamin Gaignard * into account the Sync and Back Porch information.
311f2cb3148SBenjamin Gaignard * Video frame line numbers start at 1, y starts at 0.
312f2cb3148SBenjamin Gaignard * In interlaced modes the start line is the field line number of the odd
313f2cb3148SBenjamin Gaignard * field, but y is still defined as a progressive frame.
314f2cb3148SBenjamin Gaignard */
sti_vtg_get_line_number(struct drm_display_mode mode,int y)315f2cb3148SBenjamin Gaignard u32 sti_vtg_get_line_number(struct drm_display_mode mode, int y)
316f2cb3148SBenjamin Gaignard {
317f2cb3148SBenjamin Gaignard u32 start_line = mode.vtotal - mode.vsync_start + 1;
318f2cb3148SBenjamin Gaignard
319f2cb3148SBenjamin Gaignard if (mode.flags & DRM_MODE_FLAG_INTERLACE)
320f2cb3148SBenjamin Gaignard start_line *= 2;
321f2cb3148SBenjamin Gaignard
322f2cb3148SBenjamin Gaignard return start_line + y;
323f2cb3148SBenjamin Gaignard }
324f2cb3148SBenjamin Gaignard
325f2cb3148SBenjamin Gaignard /**
326f2cb3148SBenjamin Gaignard * sti_vtg_get_pixel_number
327f2cb3148SBenjamin Gaignard *
328f2cb3148SBenjamin Gaignard * @mode: display mode to be used
329f2cb3148SBenjamin Gaignard * @x: row
330f2cb3148SBenjamin Gaignard *
331f2cb3148SBenjamin Gaignard * Return the pixel number according to the display mode taking
332f2cb3148SBenjamin Gaignard * into account the Sync and Back Porch information.
333f2cb3148SBenjamin Gaignard * Pixels are counted from 0.
334f2cb3148SBenjamin Gaignard */
sti_vtg_get_pixel_number(struct drm_display_mode mode,int x)335f2cb3148SBenjamin Gaignard u32 sti_vtg_get_pixel_number(struct drm_display_mode mode, int x)
336f2cb3148SBenjamin Gaignard {
337f2cb3148SBenjamin Gaignard return mode.htotal - mode.hsync_start + x;
338f2cb3148SBenjamin Gaignard }
339f2cb3148SBenjamin Gaignard
sti_vtg_register_client(struct sti_vtg * vtg,struct notifier_block * nb,struct drm_crtc * crtc)3402388693eSThierry Reding int sti_vtg_register_client(struct sti_vtg *vtg, struct notifier_block *nb,
3412388693eSThierry Reding struct drm_crtc *crtc)
342f2cb3148SBenjamin Gaignard {
3432388693eSThierry Reding vtg->crtc = crtc;
344f2cb3148SBenjamin Gaignard return raw_notifier_chain_register(&vtg->notifier_list, nb);
345f2cb3148SBenjamin Gaignard }
346f2cb3148SBenjamin Gaignard
sti_vtg_unregister_client(struct sti_vtg * vtg,struct notifier_block * nb)347f2cb3148SBenjamin Gaignard int sti_vtg_unregister_client(struct sti_vtg *vtg, struct notifier_block *nb)
348f2cb3148SBenjamin Gaignard {
349f2cb3148SBenjamin Gaignard return raw_notifier_chain_unregister(&vtg->notifier_list, nb);
350f2cb3148SBenjamin Gaignard }
351f2cb3148SBenjamin Gaignard
vtg_irq_thread(int irq,void * arg)352f2cb3148SBenjamin Gaignard static irqreturn_t vtg_irq_thread(int irq, void *arg)
353f2cb3148SBenjamin Gaignard {
354f2cb3148SBenjamin Gaignard struct sti_vtg *vtg = arg;
355f2cb3148SBenjamin Gaignard u32 event;
356f2cb3148SBenjamin Gaignard
357f2cb3148SBenjamin Gaignard event = (vtg->irq_status & VTG_IRQ_TOP) ?
358f2cb3148SBenjamin Gaignard VTG_TOP_FIELD_EVENT : VTG_BOTTOM_FIELD_EVENT;
359f2cb3148SBenjamin Gaignard
3602388693eSThierry Reding raw_notifier_call_chain(&vtg->notifier_list, event, vtg->crtc);
361f2cb3148SBenjamin Gaignard
362f2cb3148SBenjamin Gaignard return IRQ_HANDLED;
363f2cb3148SBenjamin Gaignard }
364f2cb3148SBenjamin Gaignard
vtg_irq(int irq,void * arg)365f2cb3148SBenjamin Gaignard static irqreturn_t vtg_irq(int irq, void *arg)
366f2cb3148SBenjamin Gaignard {
367f2cb3148SBenjamin Gaignard struct sti_vtg *vtg = arg;
368f2cb3148SBenjamin Gaignard
369f2cb3148SBenjamin Gaignard vtg->irq_status = readl(vtg->regs + VTG_HOST_ITS);
370f2cb3148SBenjamin Gaignard
371f2cb3148SBenjamin Gaignard writel(vtg->irq_status, vtg->regs + VTG_HOST_ITS_BCLR);
372f2cb3148SBenjamin Gaignard
373f2cb3148SBenjamin Gaignard /* force sync bus write */
374f2cb3148SBenjamin Gaignard readl(vtg->regs + VTG_HOST_ITS);
375f2cb3148SBenjamin Gaignard
376f2cb3148SBenjamin Gaignard return IRQ_WAKE_THREAD;
377f2cb3148SBenjamin Gaignard }
378f2cb3148SBenjamin Gaignard
vtg_probe(struct platform_device * pdev)379f2cb3148SBenjamin Gaignard static int vtg_probe(struct platform_device *pdev)
380f2cb3148SBenjamin Gaignard {
381f2cb3148SBenjamin Gaignard struct device *dev = &pdev->dev;
382f2cb3148SBenjamin Gaignard struct sti_vtg *vtg;
383f2cb3148SBenjamin Gaignard struct resource *res;
384f2cb3148SBenjamin Gaignard int ret;
385f2cb3148SBenjamin Gaignard
386f2cb3148SBenjamin Gaignard vtg = devm_kzalloc(dev, sizeof(*vtg), GFP_KERNEL);
387f2cb3148SBenjamin Gaignard if (!vtg)
388f2cb3148SBenjamin Gaignard return -ENOMEM;
389f2cb3148SBenjamin Gaignard
390f2cb3148SBenjamin Gaignard /* Get Memory ressources */
391f2cb3148SBenjamin Gaignard res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
392f2cb3148SBenjamin Gaignard if (!res) {
393f2cb3148SBenjamin Gaignard DRM_ERROR("Get memory resource failed\n");
394f2cb3148SBenjamin Gaignard return -ENOMEM;
395f2cb3148SBenjamin Gaignard }
396*4bdc0d67SChristoph Hellwig vtg->regs = devm_ioremap(dev, res->start, resource_size(res));
3971ae0d5afSArvind Yadav if (!vtg->regs) {
3981ae0d5afSArvind Yadav DRM_ERROR("failed to remap I/O memory\n");
3991ae0d5afSArvind Yadav return -ENOMEM;
4001ae0d5afSArvind Yadav }
401f2cb3148SBenjamin Gaignard
402f2cb3148SBenjamin Gaignard vtg->irq = platform_get_irq(pdev, 0);
403287980e4SArnd Bergmann if (vtg->irq < 0) {
404f2cb3148SBenjamin Gaignard DRM_ERROR("Failed to get VTG interrupt\n");
405f2cb3148SBenjamin Gaignard return vtg->irq;
406f2cb3148SBenjamin Gaignard }
407f2cb3148SBenjamin Gaignard
408f2cb3148SBenjamin Gaignard RAW_INIT_NOTIFIER_HEAD(&vtg->notifier_list);
409f2cb3148SBenjamin Gaignard
410f2cb3148SBenjamin Gaignard ret = devm_request_threaded_irq(dev, vtg->irq, vtg_irq,
4118b0a99ceSVincent Abriou vtg_irq_thread, IRQF_ONESHOT,
4128b0a99ceSVincent Abriou dev_name(dev), vtg);
413287980e4SArnd Bergmann if (ret < 0) {
414f2cb3148SBenjamin Gaignard DRM_ERROR("Failed to register VTG interrupt\n");
415f2cb3148SBenjamin Gaignard return ret;
416f2cb3148SBenjamin Gaignard }
417f2cb3148SBenjamin Gaignard
418f2cb3148SBenjamin Gaignard platform_set_drvdata(pdev, vtg);
419f2cb3148SBenjamin Gaignard
420cc6b741cSBenjamin Gaignard DRM_INFO("%s %s\n", __func__, dev_name(dev));
421f2cb3148SBenjamin Gaignard
422f2cb3148SBenjamin Gaignard return 0;
423f2cb3148SBenjamin Gaignard }
424f2cb3148SBenjamin Gaignard
425f2cb3148SBenjamin Gaignard static const struct of_device_id vtg_of_match[] = {
426f2cb3148SBenjamin Gaignard { .compatible = "st,vtg", },
427f2cb3148SBenjamin Gaignard { /* sentinel */ }
428f2cb3148SBenjamin Gaignard };
429f2cb3148SBenjamin Gaignard MODULE_DEVICE_TABLE(of, vtg_of_match);
430f2cb3148SBenjamin Gaignard
431f2cb3148SBenjamin Gaignard struct platform_driver sti_vtg_driver = {
432f2cb3148SBenjamin Gaignard .driver = {
433f2cb3148SBenjamin Gaignard .name = "sti-vtg",
434f2cb3148SBenjamin Gaignard .owner = THIS_MODULE,
435f2cb3148SBenjamin Gaignard .of_match_table = vtg_of_match,
436f2cb3148SBenjamin Gaignard },
437f2cb3148SBenjamin Gaignard .probe = vtg_probe,
438f2cb3148SBenjamin Gaignard };
439f2cb3148SBenjamin Gaignard
440f2cb3148SBenjamin Gaignard MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
441f2cb3148SBenjamin Gaignard MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
442f2cb3148SBenjamin Gaignard MODULE_LICENSE("GPL");
443