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