1 /*
2  *  cobalt interrupt handling
3  *
4  *  Copyright 2012-2015 Cisco Systems, Inc. and/or its affiliates.
5  *  All rights reserved.
6  *
7  *  This program is free software; you may redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; version 2 of the License.
10  *
11  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
12  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
13  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
15  *  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
16  *  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
17  *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18  *  SOFTWARE.
19  */
20 
21 #include <media/adv7604.h>
22 
23 #include "cobalt-driver.h"
24 #include "cobalt-irq.h"
25 #include "cobalt-omnitek.h"
26 
27 static void cobalt_dma_stream_queue_handler(struct cobalt_stream *s)
28 {
29 	struct cobalt *cobalt = s->cobalt;
30 	int rx = s->video_channel;
31 	volatile struct m00473_freewheel_regmap __iomem *fw =
32 		COBALT_CVI_FREEWHEEL(s->cobalt, rx);
33 	volatile struct m00233_video_measure_regmap __iomem *vmr =
34 		COBALT_CVI_VMR(s->cobalt, rx);
35 	volatile struct m00389_cvi_regmap __iomem *cvi =
36 		COBALT_CVI(s->cobalt, rx);
37 	volatile struct m00479_clk_loss_detector_regmap __iomem *clkloss =
38 		COBALT_CVI_CLK_LOSS(s->cobalt, rx);
39 	struct cobalt_buffer *cb;
40 	bool skip = false;
41 
42 	spin_lock(&s->irqlock);
43 
44 	if (list_empty(&s->bufs)) {
45 		pr_err("no buffers!\n");
46 		spin_unlock(&s->irqlock);
47 		return;
48 	}
49 
50 	/* Give the fresh filled up buffer to the user.
51 	 * Note that the interrupt is only sent if the DMA can continue
52 	 * with a new buffer, so it is always safe to return this buffer
53 	 * to userspace. */
54 	cb = list_first_entry(&s->bufs, struct cobalt_buffer, list);
55 	list_del(&cb->list);
56 	spin_unlock(&s->irqlock);
57 
58 	if (s->is_audio || s->is_output)
59 		goto done;
60 
61 	if (s->unstable_frame) {
62 		uint32_t stat = vmr->irq_status;
63 
64 		vmr->irq_status = stat;
65 		if (!(vmr->status & M00233_STATUS_BITMAP_INIT_DONE_MSK)) {
66 			cobalt_dbg(1, "!init_done\n");
67 			if (s->enable_freewheel)
68 				goto restart_fw;
69 			goto done;
70 		}
71 
72 		if (clkloss->status & M00479_STATUS_BITMAP_CLOCK_MISSING_MSK) {
73 			clkloss->ctrl = 0;
74 			clkloss->ctrl = M00479_CTRL_BITMAP_ENABLE_MSK;
75 			cobalt_dbg(1, "no clock\n");
76 			if (s->enable_freewheel)
77 				goto restart_fw;
78 			goto done;
79 		}
80 		if ((stat & (M00233_IRQ_STATUS_BITMAP_VACTIVE_AREA_MSK |
81 			     M00233_IRQ_STATUS_BITMAP_HACTIVE_AREA_MSK)) ||
82 				vmr->vactive_area != s->timings.bt.height ||
83 				vmr->hactive_area != s->timings.bt.width) {
84 			cobalt_dbg(1, "unstable\n");
85 			if (s->enable_freewheel)
86 				goto restart_fw;
87 			goto done;
88 		}
89 		if (!s->enable_cvi) {
90 			s->enable_cvi = true;
91 			cvi->control = M00389_CONTROL_BITMAP_ENABLE_MSK;
92 			goto done;
93 		}
94 		if (!(cvi->status & M00389_STATUS_BITMAP_LOCK_MSK)) {
95 			cobalt_dbg(1, "cvi no lock\n");
96 			if (s->enable_freewheel)
97 				goto restart_fw;
98 			goto done;
99 		}
100 		if (!s->enable_freewheel) {
101 			cobalt_dbg(1, "stable\n");
102 			s->enable_freewheel = true;
103 			fw->ctrl = 0;
104 			goto done;
105 		}
106 		cobalt_dbg(1, "enabled fw\n");
107 		vmr->control = M00233_CONTROL_BITMAP_ENABLE_MEASURE_MSK |
108 			       M00233_CONTROL_BITMAP_ENABLE_INTERRUPT_MSK;
109 		fw->ctrl = M00473_CTRL_BITMAP_ENABLE_MSK;
110 		s->enable_freewheel = false;
111 		s->unstable_frame = false;
112 		s->skip_first_frames = 2;
113 		skip = true;
114 		goto done;
115 	}
116 	if (fw->status & M00473_STATUS_BITMAP_FREEWHEEL_MODE_MSK) {
117 restart_fw:
118 		cobalt_dbg(1, "lost lock\n");
119 		vmr->control = M00233_CONTROL_BITMAP_ENABLE_MEASURE_MSK;
120 		fw->ctrl = M00473_CTRL_BITMAP_ENABLE_MSK |
121 			   M00473_CTRL_BITMAP_FORCE_FREEWHEEL_MODE_MSK;
122 		cvi->control = 0;
123 		s->unstable_frame = true;
124 		s->enable_freewheel = false;
125 		s->enable_cvi = false;
126 	}
127 done:
128 	if (s->skip_first_frames) {
129 		skip = true;
130 		s->skip_first_frames--;
131 	}
132 	v4l2_get_timestamp(&cb->vb.v4l2_buf.timestamp);
133 	/* TODO: the sequence number should be read from the FPGA so we
134 	   also know about dropped frames. */
135 	cb->vb.v4l2_buf.sequence = s->sequence++;
136 	vb2_buffer_done(&cb->vb, (skip || s->unstable_frame) ?
137 			VB2_BUF_STATE_QUEUED : VB2_BUF_STATE_DONE);
138 }
139 
140 irqreturn_t cobalt_irq_handler(int irq, void *dev_id)
141 {
142 	struct cobalt *cobalt = (struct cobalt *)dev_id;
143 	u32 dma_interrupt =
144 		cobalt_read_bar0(cobalt, DMA_INTERRUPT_STATUS_REG) & 0xffff;
145 	u32 mask = cobalt_read_bar1(cobalt, COBALT_SYS_STAT_MASK);
146 	u32 edge = cobalt_read_bar1(cobalt, COBALT_SYS_STAT_EDGE);
147 	int i;
148 
149 	/* Clear DMA interrupt */
150 	cobalt_write_bar0(cobalt, DMA_INTERRUPT_STATUS_REG, dma_interrupt);
151 	cobalt_write_bar1(cobalt, COBALT_SYS_STAT_MASK, mask & ~edge);
152 	cobalt_write_bar1(cobalt, COBALT_SYS_STAT_EDGE, edge);
153 
154 	for (i = 0; i < COBALT_NUM_STREAMS; i++) {
155 		struct cobalt_stream *s = &cobalt->streams[i];
156 		unsigned dma_fifo_mask =
157 		    COBALT_SYSSTAT_VI0_LOST_DATA_MSK << (4 * s->video_channel);
158 
159 		if (dma_interrupt & (1 << s->dma_channel)) {
160 			cobalt->irq_dma[i]++;
161 			/* Give fresh buffer to user and chain newly
162 			 * queued buffers */
163 			cobalt_dma_stream_queue_handler(s);
164 			if (!s->is_audio) {
165 				edge &= ~dma_fifo_mask;
166 				cobalt_write_bar1(cobalt, COBALT_SYS_STAT_MASK,
167 						  mask & ~edge);
168 			}
169 		}
170 		if (s->is_audio)
171 			continue;
172 		if (edge & (0x20 << (4 * s->video_channel)))
173 			set_bit(COBALT_STREAM_FL_ADV_IRQ, &s->flags);
174 		if ((edge & mask & dma_fifo_mask) && vb2_is_streaming(&s->q)) {
175 			cobalt_info("full rx FIFO %d\n", i);
176 			cobalt->irq_full_fifo++;
177 		}
178 	}
179 
180 	queue_work(cobalt->irq_work_queues, &cobalt->irq_work_queue);
181 
182 	if (edge & mask & (COBALT_SYSSTAT_VI0_INT1_MSK |
183 			   COBALT_SYSSTAT_VI1_INT1_MSK |
184 			   COBALT_SYSSTAT_VI2_INT1_MSK |
185 			   COBALT_SYSSTAT_VI3_INT1_MSK |
186 			   COBALT_SYSSTAT_VIHSMA_INT1_MSK |
187 			   COBALT_SYSSTAT_VOHSMA_INT1_MSK))
188 		cobalt->irq_adv1++;
189 	if (edge & mask & (COBALT_SYSSTAT_VI0_INT2_MSK |
190 			   COBALT_SYSSTAT_VI1_INT2_MSK |
191 			   COBALT_SYSSTAT_VI2_INT2_MSK |
192 			   COBALT_SYSSTAT_VI3_INT2_MSK |
193 			   COBALT_SYSSTAT_VIHSMA_INT2_MSK))
194 		cobalt->irq_adv2++;
195 	if (edge & mask & COBALT_SYSSTAT_VOHSMA_INT1_MSK)
196 		cobalt->irq_advout++;
197 	if (dma_interrupt)
198 		cobalt->irq_dma_tot++;
199 	if (!(edge & mask) && !dma_interrupt)
200 		cobalt->irq_none++;
201 	dma_interrupt = cobalt_read_bar0(cobalt, DMA_INTERRUPT_STATUS_REG);
202 
203 	return IRQ_HANDLED;
204 }
205 
206 void cobalt_irq_work_handler(struct work_struct *work)
207 {
208 	struct cobalt *cobalt =
209 		container_of(work, struct cobalt, irq_work_queue);
210 	int i;
211 
212 	for (i = 0; i < COBALT_NUM_NODES; i++) {
213 		struct cobalt_stream *s = &cobalt->streams[i];
214 
215 		if (test_and_clear_bit(COBALT_STREAM_FL_ADV_IRQ, &s->flags)) {
216 			u32 mask;
217 
218 			v4l2_subdev_call(cobalt->streams[i].sd, core,
219 					interrupt_service_routine, 0, NULL);
220 			mask = cobalt_read_bar1(cobalt, COBALT_SYS_STAT_MASK);
221 			cobalt_write_bar1(cobalt, COBALT_SYS_STAT_MASK,
222 				mask | (0x20 << (4 * s->video_channel)));
223 		}
224 	}
225 }
226 
227 void cobalt_irq_log_status(struct cobalt *cobalt)
228 {
229 	u32 mask;
230 	int i;
231 
232 	cobalt_info("irq: adv1=%u adv2=%u advout=%u none=%u full=%u\n",
233 		    cobalt->irq_adv1, cobalt->irq_adv2, cobalt->irq_advout,
234 		    cobalt->irq_none, cobalt->irq_full_fifo);
235 	cobalt_info("irq: dma_tot=%u (", cobalt->irq_dma_tot);
236 	for (i = 0; i < COBALT_NUM_STREAMS; i++)
237 		pr_cont("%s%u", i ? "/" : "", cobalt->irq_dma[i]);
238 	pr_cont(")\n");
239 	cobalt->irq_dma_tot = cobalt->irq_adv1 = cobalt->irq_adv2 = 0;
240 	cobalt->irq_advout = cobalt->irq_none = cobalt->irq_full_fifo = 0;
241 	memset(cobalt->irq_dma, 0, sizeof(cobalt->irq_dma));
242 
243 	mask = cobalt_read_bar1(cobalt, COBALT_SYS_STAT_MASK);
244 	cobalt_write_bar1(cobalt, COBALT_SYS_STAT_MASK,
245 			mask |
246 			COBALT_SYSSTAT_VI0_LOST_DATA_MSK |
247 			COBALT_SYSSTAT_VI1_LOST_DATA_MSK |
248 			COBALT_SYSSTAT_VI2_LOST_DATA_MSK |
249 			COBALT_SYSSTAT_VI3_LOST_DATA_MSK |
250 			COBALT_SYSSTAT_VIHSMA_LOST_DATA_MSK |
251 			COBALT_SYSSTAT_VOHSMA_LOST_DATA_MSK |
252 			COBALT_SYSSTAT_AUD_IN_LOST_DATA_MSK |
253 			COBALT_SYSSTAT_AUD_OUT_LOST_DATA_MSK);
254 }
255