Input: xpad - add support for Xbox1 PDP Camo series gamepad
[linux/fpc-iii.git] / drivers / gpu / drm / sti / sti_vtg.c
blobc3d9c8ae14afeb66b998a59a37b1bdbb60bb87d8
1 /*
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
8 */
10 #include <linux/module.h>
11 #include <linux/notifier.h>
12 #include <linux/platform_device.h>
14 #include <drm/drmP.h>
16 #include "sti_drv.h"
17 #include "sti_vtg.h"
19 #define VTG_MODE_MASTER 0
20 #define VTG_MODE_SLAVE_BY_EXT0 1
22 /* registers offset */
23 #define VTG_MODE 0x0000
24 #define VTG_CLKLN 0x0008
25 #define VTG_HLFLN 0x000C
26 #define VTG_DRST_AUTOC 0x0010
27 #define VTG_VID_TFO 0x0040
28 #define VTG_VID_TFS 0x0044
29 #define VTG_VID_BFO 0x0048
30 #define VTG_VID_BFS 0x004C
32 #define VTG_HOST_ITS 0x0078
33 #define VTG_HOST_ITS_BCLR 0x007C
34 #define VTG_HOST_ITM_BCLR 0x0088
35 #define VTG_HOST_ITM_BSET 0x008C
37 #define VTG_H_HD_1 0x00C0
38 #define VTG_TOP_V_VD_1 0x00C4
39 #define VTG_BOT_V_VD_1 0x00C8
40 #define VTG_TOP_V_HD_1 0x00CC
41 #define VTG_BOT_V_HD_1 0x00D0
43 #define VTG_H_HD_2 0x00E0
44 #define VTG_TOP_V_VD_2 0x00E4
45 #define VTG_BOT_V_VD_2 0x00E8
46 #define VTG_TOP_V_HD_2 0x00EC
47 #define VTG_BOT_V_HD_2 0x00F0
49 #define VTG_H_HD_3 0x0100
50 #define VTG_TOP_V_VD_3 0x0104
51 #define VTG_BOT_V_VD_3 0x0108
52 #define VTG_TOP_V_HD_3 0x010C
53 #define VTG_BOT_V_HD_3 0x0110
55 #define VTG_H_HD_4 0x0120
56 #define VTG_TOP_V_VD_4 0x0124
57 #define VTG_BOT_V_VD_4 0x0128
58 #define VTG_TOP_V_HD_4 0x012c
59 #define VTG_BOT_V_HD_4 0x0130
61 #define VTG_IRQ_BOTTOM BIT(0)
62 #define VTG_IRQ_TOP BIT(1)
63 #define VTG_IRQ_MASK (VTG_IRQ_TOP | VTG_IRQ_BOTTOM)
65 /* Delay introduced by the HDMI in nb of pixel */
66 #define HDMI_DELAY (5)
68 /* Delay introduced by the DVO in nb of pixel */
69 #define DVO_DELAY (7)
71 /* delay introduced by the Arbitrary Waveform Generator in nb of pixels */
72 #define AWG_DELAY_HD (-9)
73 #define AWG_DELAY_ED (-8)
74 #define AWG_DELAY_SD (-7)
76 static LIST_HEAD(vtg_lookup);
79 * STI VTG register offset structure
81 *@h_hd: stores the VTG_H_HD_x register offset
82 *@top_v_vd: stores the VTG_TOP_V_VD_x register offset
83 *@bot_v_vd: stores the VTG_BOT_V_VD_x register offset
84 *@top_v_hd: stores the VTG_TOP_V_HD_x register offset
85 *@bot_v_hd: stores the VTG_BOT_V_HD_x register offset
87 struct sti_vtg_regs_offs {
88 u32 h_hd;
89 u32 top_v_vd;
90 u32 bot_v_vd;
91 u32 top_v_hd;
92 u32 bot_v_hd;
95 #define VTG_MAX_SYNC_OUTPUT 4
96 static const struct sti_vtg_regs_offs vtg_regs_offs[VTG_MAX_SYNC_OUTPUT] = {
97 { VTG_H_HD_1,
98 VTG_TOP_V_VD_1, VTG_BOT_V_VD_1, VTG_TOP_V_HD_1, VTG_BOT_V_HD_1 },
99 { VTG_H_HD_2,
100 VTG_TOP_V_VD_2, VTG_BOT_V_VD_2, VTG_TOP_V_HD_2, VTG_BOT_V_HD_2 },
101 { VTG_H_HD_3,
102 VTG_TOP_V_VD_3, VTG_BOT_V_VD_3, VTG_TOP_V_HD_3, VTG_BOT_V_HD_3 },
103 { VTG_H_HD_4,
104 VTG_TOP_V_VD_4, VTG_BOT_V_VD_4, VTG_TOP_V_HD_4, VTG_BOT_V_HD_4 }
108 * STI VTG synchronisation parameters structure
110 *@hsync: sample number falling and rising edge
111 *@vsync_line_top: vertical top field line number falling and rising edge
112 *@vsync_line_bot: vertical bottom field line number falling and rising edge
113 *@vsync_off_top: vertical top field sample number rising and falling edge
114 *@vsync_off_bot: vertical bottom field sample number rising and falling edge
116 struct sti_vtg_sync_params {
117 u32 hsync;
118 u32 vsync_line_top;
119 u32 vsync_line_bot;
120 u32 vsync_off_top;
121 u32 vsync_off_bot;
125 * STI VTG structure
127 * @dev: pointer to device driver
128 * @np: device node
129 * @regs: register mapping
130 * @sync_params: synchronisation parameters used to generate timings
131 * @irq: VTG irq
132 * @irq_status: store the IRQ status value
133 * @notifier_list: notifier callback
134 * @crtc: the CRTC for vblank event
135 * @slave: slave vtg
136 * @link: List node to link the structure in lookup list
138 struct sti_vtg {
139 struct device *dev;
140 struct device_node *np;
141 void __iomem *regs;
142 struct sti_vtg_sync_params sync_params[VTG_MAX_SYNC_OUTPUT];
143 int irq;
144 u32 irq_status;
145 struct raw_notifier_head notifier_list;
146 struct drm_crtc *crtc;
147 struct sti_vtg *slave;
148 struct list_head link;
151 static void vtg_register(struct sti_vtg *vtg)
153 list_add_tail(&vtg->link, &vtg_lookup);
156 struct sti_vtg *of_vtg_find(struct device_node *np)
158 struct sti_vtg *vtg;
160 list_for_each_entry(vtg, &vtg_lookup, link) {
161 if (vtg->np == np)
162 return vtg;
164 return NULL;
167 static void vtg_reset(struct sti_vtg *vtg)
169 /* reset slave and then master */
170 if (vtg->slave)
171 vtg_reset(vtg->slave);
173 writel(1, vtg->regs + VTG_DRST_AUTOC);
176 static void vtg_set_output_window(void __iomem *regs,
177 const struct drm_display_mode *mode)
179 u32 video_top_field_start;
180 u32 video_top_field_stop;
181 u32 video_bottom_field_start;
182 u32 video_bottom_field_stop;
183 u32 xstart = sti_vtg_get_pixel_number(*mode, 0);
184 u32 ystart = sti_vtg_get_line_number(*mode, 0);
185 u32 xstop = sti_vtg_get_pixel_number(*mode, mode->hdisplay - 1);
186 u32 ystop = sti_vtg_get_line_number(*mode, mode->vdisplay - 1);
188 /* Set output window to fit the display mode selected */
189 video_top_field_start = (ystart << 16) | xstart;
190 video_top_field_stop = (ystop << 16) | xstop;
192 /* Only progressive supported for now */
193 video_bottom_field_start = video_top_field_start;
194 video_bottom_field_stop = video_top_field_stop;
196 writel(video_top_field_start, regs + VTG_VID_TFO);
197 writel(video_top_field_stop, regs + VTG_VID_TFS);
198 writel(video_bottom_field_start, regs + VTG_VID_BFO);
199 writel(video_bottom_field_stop, regs + VTG_VID_BFS);
202 static void vtg_set_hsync_vsync_pos(struct sti_vtg_sync_params *sync,
203 int delay,
204 const struct drm_display_mode *mode)
206 long clocksperline, start, stop;
207 u32 risesync_top, fallsync_top;
208 u32 risesync_offs_top, fallsync_offs_top;
210 clocksperline = mode->htotal;
212 /* Get the hsync position */
213 start = 0;
214 stop = mode->hsync_end - mode->hsync_start;
216 start += delay;
217 stop += delay;
219 if (start < 0)
220 start += clocksperline;
221 else if (start >= clocksperline)
222 start -= clocksperline;
224 if (stop < 0)
225 stop += clocksperline;
226 else if (stop >= clocksperline)
227 stop -= clocksperline;
229 sync->hsync = (stop << 16) | start;
231 /* Get the vsync position */
232 if (delay >= 0) {
233 risesync_top = 1;
234 fallsync_top = risesync_top;
235 fallsync_top += mode->vsync_end - mode->vsync_start;
237 fallsync_offs_top = (u32)delay;
238 risesync_offs_top = (u32)delay;
239 } else {
240 risesync_top = mode->vtotal;
241 fallsync_top = mode->vsync_end - mode->vsync_start;
243 fallsync_offs_top = clocksperline + delay;
244 risesync_offs_top = clocksperline + delay;
247 sync->vsync_line_top = (fallsync_top << 16) | risesync_top;
248 sync->vsync_off_top = (fallsync_offs_top << 16) | risesync_offs_top;
250 /* Only progressive supported for now */
251 sync->vsync_line_bot = sync->vsync_line_top;
252 sync->vsync_off_bot = sync->vsync_off_top;
255 static void vtg_set_mode(struct sti_vtg *vtg,
256 int type,
257 struct sti_vtg_sync_params *sync,
258 const struct drm_display_mode *mode)
260 unsigned int i;
262 if (vtg->slave)
263 vtg_set_mode(vtg->slave, VTG_MODE_SLAVE_BY_EXT0,
264 vtg->sync_params, mode);
266 /* Set the number of clock cycles per line */
267 writel(mode->htotal, vtg->regs + VTG_CLKLN);
269 /* Set Half Line Per Field (only progressive supported for now) */
270 writel(mode->vtotal * 2, vtg->regs + VTG_HLFLN);
272 /* Program output window */
273 vtg_set_output_window(vtg->regs, mode);
275 /* Set hsync and vsync position for HDMI */
276 vtg_set_hsync_vsync_pos(&sync[VTG_SYNC_ID_HDMI - 1], HDMI_DELAY, mode);
278 /* Set hsync and vsync position for HD DCS */
279 vtg_set_hsync_vsync_pos(&sync[VTG_SYNC_ID_HDDCS - 1], 0, mode);
281 /* Set hsync and vsync position for HDF */
282 vtg_set_hsync_vsync_pos(&sync[VTG_SYNC_ID_HDF - 1], AWG_DELAY_HD, mode);
284 /* Set hsync and vsync position for DVO */
285 vtg_set_hsync_vsync_pos(&sync[VTG_SYNC_ID_DVO - 1], DVO_DELAY, mode);
287 /* Progam the syncs outputs */
288 for (i = 0; i < VTG_MAX_SYNC_OUTPUT ; i++) {
289 writel(sync[i].hsync,
290 vtg->regs + vtg_regs_offs[i].h_hd);
291 writel(sync[i].vsync_line_top,
292 vtg->regs + vtg_regs_offs[i].top_v_vd);
293 writel(sync[i].vsync_line_bot,
294 vtg->regs + vtg_regs_offs[i].bot_v_vd);
295 writel(sync[i].vsync_off_top,
296 vtg->regs + vtg_regs_offs[i].top_v_hd);
297 writel(sync[i].vsync_off_bot,
298 vtg->regs + vtg_regs_offs[i].bot_v_hd);
301 /* mode */
302 writel(type, vtg->regs + VTG_MODE);
305 static void vtg_enable_irq(struct sti_vtg *vtg)
307 /* clear interrupt status and mask */
308 writel(0xFFFF, vtg->regs + VTG_HOST_ITS_BCLR);
309 writel(0xFFFF, vtg->regs + VTG_HOST_ITM_BCLR);
310 writel(VTG_IRQ_MASK, vtg->regs + VTG_HOST_ITM_BSET);
313 void sti_vtg_set_config(struct sti_vtg *vtg,
314 const struct drm_display_mode *mode)
316 /* write configuration */
317 vtg_set_mode(vtg, VTG_MODE_MASTER, vtg->sync_params, mode);
319 vtg_reset(vtg);
321 /* enable irq for the vtg vblank synchro */
322 if (vtg->slave)
323 vtg_enable_irq(vtg->slave);
324 else
325 vtg_enable_irq(vtg);
329 * sti_vtg_get_line_number
331 * @mode: display mode to be used
332 * @y: line
334 * Return the line number according to the display mode taking
335 * into account the Sync and Back Porch information.
336 * Video frame line numbers start at 1, y starts at 0.
337 * In interlaced modes the start line is the field line number of the odd
338 * field, but y is still defined as a progressive frame.
340 u32 sti_vtg_get_line_number(struct drm_display_mode mode, int y)
342 u32 start_line = mode.vtotal - mode.vsync_start + 1;
344 if (mode.flags & DRM_MODE_FLAG_INTERLACE)
345 start_line *= 2;
347 return start_line + y;
351 * sti_vtg_get_pixel_number
353 * @mode: display mode to be used
354 * @x: row
356 * Return the pixel number according to the display mode taking
357 * into account the Sync and Back Porch information.
358 * Pixels are counted from 0.
360 u32 sti_vtg_get_pixel_number(struct drm_display_mode mode, int x)
362 return mode.htotal - mode.hsync_start + x;
365 int sti_vtg_register_client(struct sti_vtg *vtg, struct notifier_block *nb,
366 struct drm_crtc *crtc)
368 if (vtg->slave)
369 return sti_vtg_register_client(vtg->slave, nb, crtc);
371 vtg->crtc = crtc;
372 return raw_notifier_chain_register(&vtg->notifier_list, nb);
375 int sti_vtg_unregister_client(struct sti_vtg *vtg, struct notifier_block *nb)
377 if (vtg->slave)
378 return sti_vtg_unregister_client(vtg->slave, nb);
380 return raw_notifier_chain_unregister(&vtg->notifier_list, nb);
383 static irqreturn_t vtg_irq_thread(int irq, void *arg)
385 struct sti_vtg *vtg = arg;
386 u32 event;
388 event = (vtg->irq_status & VTG_IRQ_TOP) ?
389 VTG_TOP_FIELD_EVENT : VTG_BOTTOM_FIELD_EVENT;
391 raw_notifier_call_chain(&vtg->notifier_list, event, vtg->crtc);
393 return IRQ_HANDLED;
396 static irqreturn_t vtg_irq(int irq, void *arg)
398 struct sti_vtg *vtg = arg;
400 vtg->irq_status = readl(vtg->regs + VTG_HOST_ITS);
402 writel(vtg->irq_status, vtg->regs + VTG_HOST_ITS_BCLR);
404 /* force sync bus write */
405 readl(vtg->regs + VTG_HOST_ITS);
407 return IRQ_WAKE_THREAD;
410 static int vtg_probe(struct platform_device *pdev)
412 struct device *dev = &pdev->dev;
413 struct device_node *np;
414 struct sti_vtg *vtg;
415 struct resource *res;
416 int ret;
418 vtg = devm_kzalloc(dev, sizeof(*vtg), GFP_KERNEL);
419 if (!vtg)
420 return -ENOMEM;
422 vtg->dev = dev;
423 vtg->np = pdev->dev.of_node;
425 /* Get Memory ressources */
426 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
427 if (!res) {
428 DRM_ERROR("Get memory resource failed\n");
429 return -ENOMEM;
431 vtg->regs = devm_ioremap_nocache(dev, res->start, resource_size(res));
432 if (!vtg->regs) {
433 DRM_ERROR("failed to remap I/O memory\n");
434 return -ENOMEM;
437 np = of_parse_phandle(pdev->dev.of_node, "st,slave", 0);
438 if (np) {
439 vtg->slave = of_vtg_find(np);
440 of_node_put(np);
442 if (!vtg->slave)
443 return -EPROBE_DEFER;
444 } else {
445 vtg->irq = platform_get_irq(pdev, 0);
446 if (vtg->irq < 0) {
447 DRM_ERROR("Failed to get VTG interrupt\n");
448 return vtg->irq;
451 RAW_INIT_NOTIFIER_HEAD(&vtg->notifier_list);
453 ret = devm_request_threaded_irq(dev, vtg->irq, vtg_irq,
454 vtg_irq_thread, IRQF_ONESHOT,
455 dev_name(dev), vtg);
456 if (ret < 0) {
457 DRM_ERROR("Failed to register VTG interrupt\n");
458 return ret;
462 vtg_register(vtg);
463 platform_set_drvdata(pdev, vtg);
465 DRM_INFO("%s %s\n", __func__, dev_name(vtg->dev));
467 return 0;
470 static int vtg_remove(struct platform_device *pdev)
472 return 0;
475 static const struct of_device_id vtg_of_match[] = {
476 { .compatible = "st,vtg", },
477 { /* sentinel */ }
479 MODULE_DEVICE_TABLE(of, vtg_of_match);
481 struct platform_driver sti_vtg_driver = {
482 .driver = {
483 .name = "sti-vtg",
484 .owner = THIS_MODULE,
485 .of_match_table = vtg_of_match,
487 .probe = vtg_probe,
488 .remove = vtg_remove,
491 MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
492 MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
493 MODULE_LICENSE("GPL");