]>
Commit | Line | Data |
---|---|---|
6ffefff5 MK |
1 | /* |
2 | * Copyright (C) 2009 Texas Instruments Inc | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
17 | * | |
18 | * TODO : add support for VBI & HBI data service | |
19 | * add static buffer allocation | |
20 | */ | |
012eef70 | 21 | |
6ffefff5 | 22 | #include <linux/module.h> |
6ffefff5 | 23 | #include <linux/interrupt.h> |
6ffefff5 | 24 | #include <linux/platform_device.h> |
5a0e3ad6 | 25 | #include <linux/slab.h> |
012eef70 | 26 | |
012eef70 | 27 | #include <media/v4l2-ioctl.h> |
6ffefff5 | 28 | |
6ffefff5 | 29 | #include "vpif.h" |
012eef70 | 30 | #include "vpif_capture.h" |
6ffefff5 MK |
31 | |
32 | MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver"); | |
33 | MODULE_LICENSE("GPL"); | |
64dc3c1a | 34 | MODULE_VERSION(VPIF_CAPTURE_VERSION); |
6ffefff5 MK |
35 | |
36 | #define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg) | |
37 | #define vpif_dbg(level, debug, fmt, arg...) \ | |
38 | v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg) | |
39 | ||
40 | static int debug = 1; | |
41 | static u32 ch0_numbuffers = 3; | |
42 | static u32 ch1_numbuffers = 3; | |
43 | static u32 ch0_bufsize = 1920 * 1080 * 2; | |
44 | static u32 ch1_bufsize = 720 * 576 * 2; | |
45 | ||
46 | module_param(debug, int, 0644); | |
47 | module_param(ch0_numbuffers, uint, S_IRUGO); | |
48 | module_param(ch1_numbuffers, uint, S_IRUGO); | |
49 | module_param(ch0_bufsize, uint, S_IRUGO); | |
50 | module_param(ch1_bufsize, uint, S_IRUGO); | |
51 | ||
52 | MODULE_PARM_DESC(debug, "Debug level 0-1"); | |
53 | MODULE_PARM_DESC(ch2_numbuffers, "Channel0 buffer count (default:3)"); | |
54 | MODULE_PARM_DESC(ch3_numbuffers, "Channel1 buffer count (default:3)"); | |
55 | MODULE_PARM_DESC(ch2_bufsize, "Channel0 buffer size (default:1920 x 1080 x 2)"); | |
56 | MODULE_PARM_DESC(ch3_bufsize, "Channel1 buffer size (default:720 x 576 x 2)"); | |
57 | ||
58 | static struct vpif_config_params config_params = { | |
59 | .min_numbuffers = 3, | |
60 | .numbuffers[0] = 3, | |
61 | .numbuffers[1] = 3, | |
62 | .min_bufsize[0] = 720 * 480 * 2, | |
63 | .min_bufsize[1] = 720 * 480 * 2, | |
64 | .channel_bufsize[0] = 1920 * 1080 * 2, | |
65 | .channel_bufsize[1] = 720 * 576 * 2, | |
66 | }; | |
67 | ||
68 | /* global variables */ | |
69 | static struct vpif_device vpif_obj = { {NULL} }; | |
70 | static struct device *vpif_dev; | |
60aa38d8 LP |
71 | static void vpif_calculate_offsets(struct channel_obj *ch); |
72 | static void vpif_config_addr(struct channel_obj *ch, int muxmode); | |
6ffefff5 | 73 | |
fa09accc LP |
74 | static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] = { {1, 1} }; |
75 | ||
76 | static inline struct vpif_cap_buffer *to_vpif_buffer(struct vb2_buffer *vb) | |
77 | { | |
78 | return container_of(vb, struct vpif_cap_buffer, vb); | |
79 | } | |
80 | ||
6ffefff5 | 81 | /** |
23e76a2a | 82 | * vpif_buffer_prepare : callback function for buffer prepare |
60aa38d8 | 83 | * @vb: ptr to vb2_buffer |
6ffefff5 | 84 | * |
60aa38d8 | 85 | * This is the callback function for buffer prepare when vb2_qbuf() |
6ffefff5 MK |
86 | * function is called. The buffer is prepared and user space virtual address |
87 | * or user address is converted into physical address | |
88 | */ | |
60aa38d8 | 89 | static int vpif_buffer_prepare(struct vb2_buffer *vb) |
6ffefff5 | 90 | { |
60aa38d8 | 91 | struct vb2_queue *q = vb->vb2_queue; |
fa09accc | 92 | struct channel_obj *ch = vb2_get_drv_priv(q); |
6ffefff5 MK |
93 | struct common_obj *common; |
94 | unsigned long addr; | |
95 | ||
6ffefff5 MK |
96 | vpif_dbg(2, debug, "vpif_buffer_prepare\n"); |
97 | ||
98 | common = &ch->common[VPIF_VIDEO_INDEX]; | |
99 | ||
23e76a2a LP |
100 | vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage); |
101 | if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) | |
102 | return -EINVAL; | |
103 | ||
104 | vb->v4l2_buf.field = common->fmt.fmt.pix.field; | |
105 | ||
106 | addr = vb2_dma_contig_plane_dma_addr(vb, 0); | |
107 | if (!IS_ALIGNED((addr + common->ytop_off), 8) || | |
108 | !IS_ALIGNED((addr + common->ybtm_off), 8) || | |
109 | !IS_ALIGNED((addr + common->ctop_off), 8) || | |
110 | !IS_ALIGNED((addr + common->cbtm_off), 8)) { | |
111 | vpif_dbg(1, debug, "offset is not aligned\n"); | |
112 | return -EINVAL; | |
6ffefff5 | 113 | } |
23e76a2a | 114 | |
6ffefff5 | 115 | return 0; |
6ffefff5 MK |
116 | } |
117 | ||
118 | /** | |
60aa38d8 LP |
119 | * vpif_buffer_queue_setup : Callback function for buffer setup. |
120 | * @vq: vb2_queue ptr | |
121 | * @fmt: v4l2 format | |
122 | * @nbuffers: ptr to number of buffers requested by application | |
123 | * @nplanes:: contains number of distinct video planes needed to hold a frame | |
124 | * @sizes[]: contains the size (in bytes) of each plane. | |
125 | * @alloc_ctxs: ptr to allocation context | |
6ffefff5 MK |
126 | * |
127 | * This callback function is called when reqbuf() is called to adjust | |
128 | * the buffer count and buffer size | |
129 | */ | |
60aa38d8 LP |
130 | static int vpif_buffer_queue_setup(struct vb2_queue *vq, |
131 | const struct v4l2_format *fmt, | |
132 | unsigned int *nbuffers, unsigned int *nplanes, | |
133 | unsigned int sizes[], void *alloc_ctxs[]) | |
6ffefff5 | 134 | { |
fa09accc | 135 | struct channel_obj *ch = vb2_get_drv_priv(vq); |
6ffefff5 MK |
136 | struct common_obj *common; |
137 | ||
138 | common = &ch->common[VPIF_VIDEO_INDEX]; | |
139 | ||
140 | vpif_dbg(2, debug, "vpif_buffer_setup\n"); | |
141 | ||
837939de LP |
142 | if (fmt && fmt->fmt.pix.sizeimage < common->fmt.fmt.pix.sizeimage) |
143 | return -EINVAL; | |
764af39a | 144 | |
837939de LP |
145 | if (vq->num_buffers + *nbuffers < 3) |
146 | *nbuffers = 3 - vq->num_buffers; | |
60aa38d8 LP |
147 | |
148 | *nplanes = 1; | |
837939de | 149 | sizes[0] = fmt ? fmt->fmt.pix.sizeimage : common->fmt.fmt.pix.sizeimage; |
60aa38d8 LP |
150 | alloc_ctxs[0] = common->alloc_ctx; |
151 | ||
837939de LP |
152 | /* Calculate the offset for Y and C data in the buffer */ |
153 | vpif_calculate_offsets(ch); | |
154 | ||
6ffefff5 MK |
155 | return 0; |
156 | } | |
157 | ||
158 | /** | |
159 | * vpif_buffer_queue : Callback function to add buffer to DMA queue | |
60aa38d8 | 160 | * @vb: ptr to vb2_buffer |
6ffefff5 | 161 | */ |
60aa38d8 | 162 | static void vpif_buffer_queue(struct vb2_buffer *vb) |
6ffefff5 | 163 | { |
fa09accc LP |
164 | struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue); |
165 | struct vpif_cap_buffer *buf = to_vpif_buffer(vb); | |
6ffefff5 | 166 | struct common_obj *common; |
aec96832 | 167 | unsigned long flags; |
6ffefff5 MK |
168 | |
169 | common = &ch->common[VPIF_VIDEO_INDEX]; | |
170 | ||
171 | vpif_dbg(2, debug, "vpif_buffer_queue\n"); | |
172 | ||
aec96832 | 173 | spin_lock_irqsave(&common->irqlock, flags); |
6ffefff5 | 174 | /* add the buffer to the DMA queue */ |
60aa38d8 | 175 | list_add_tail(&buf->list, &common->dma_queue); |
aec96832 | 176 | spin_unlock_irqrestore(&common->irqlock, flags); |
6ffefff5 MK |
177 | } |
178 | ||
41b9f24e LP |
179 | /** |
180 | * vpif_start_streaming : Starts the DMA engine for streaming | |
181 | * @vb: ptr to vb2_buffer | |
182 | * @count: number of buffers | |
183 | */ | |
60aa38d8 LP |
184 | static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count) |
185 | { | |
186 | struct vpif_capture_config *vpif_config_data = | |
187 | vpif_dev->platform_data; | |
fa09accc | 188 | struct channel_obj *ch = vb2_get_drv_priv(vq); |
60aa38d8 LP |
189 | struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX]; |
190 | struct vpif_params *vpif = &ch->vpifparams; | |
bebd8d12 LP |
191 | struct vpif_cap_buffer *buf, *tmp; |
192 | unsigned long addr, flags; | |
60aa38d8 LP |
193 | int ret; |
194 | ||
aec96832 | 195 | spin_lock_irqsave(&common->irqlock, flags); |
60aa38d8 | 196 | |
60aa38d8 LP |
197 | /* Initialize field_id and started member */ |
198 | ch->field_id = 0; | |
199 | common->started = 1; | |
60aa38d8 | 200 | |
60aa38d8 | 201 | /* configure 1 or 2 channel mode */ |
f4ad8d74 LP |
202 | if (vpif_config_data->setup_input_channel_mode) { |
203 | ret = vpif_config_data-> | |
204 | setup_input_channel_mode(vpif->std_info.ycmux_mode); | |
205 | if (ret < 0) { | |
206 | vpif_dbg(1, debug, "can't set vpif channel mode\n"); | |
bebd8d12 | 207 | goto err; |
f4ad8d74 | 208 | } |
60aa38d8 LP |
209 | } |
210 | ||
d9a3b13c LP |
211 | ret = v4l2_subdev_call(ch->sd, video, s_stream, 1); |
212 | if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) { | |
213 | vpif_dbg(1, debug, "stream on failed in subdev\n"); | |
214 | goto err; | |
215 | } | |
216 | ||
60aa38d8 LP |
217 | /* Call vpif_set_params function to set the parameters and addresses */ |
218 | ret = vpif_set_video_params(vpif, ch->channel_id); | |
219 | ||
220 | if (ret < 0) { | |
221 | vpif_dbg(1, debug, "can't set video params\n"); | |
bebd8d12 | 222 | goto err; |
60aa38d8 LP |
223 | } |
224 | ||
225 | common->started = ret; | |
226 | vpif_config_addr(ch, ret); | |
227 | ||
bebd8d12 LP |
228 | /* Get the next frame from the buffer queue */ |
229 | common->cur_frm = common->next_frm = list_entry(common->dma_queue.next, | |
230 | struct vpif_cap_buffer, list); | |
231 | /* Remove buffer from the buffer queue */ | |
232 | list_del(&common->cur_frm->list); | |
233 | spin_unlock_irqrestore(&common->irqlock, flags); | |
234 | /* Mark state of the current frame to active */ | |
235 | common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE; | |
236 | ||
237 | addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0); | |
238 | ||
60aa38d8 LP |
239 | common->set_addr(addr + common->ytop_off, |
240 | addr + common->ybtm_off, | |
241 | addr + common->ctop_off, | |
242 | addr + common->cbtm_off); | |
243 | ||
244 | /** | |
245 | * Set interrupt for both the fields in VPIF Register enable channel in | |
246 | * VPIF register | |
247 | */ | |
9e18404a | 248 | channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1; |
41b9f24e | 249 | if (VPIF_CHANNEL0_VIDEO == ch->channel_id) { |
60aa38d8 LP |
250 | channel0_intr_assert(); |
251 | channel0_intr_enable(1); | |
252 | enable_channel0(1); | |
253 | } | |
41b9f24e LP |
254 | if (VPIF_CHANNEL1_VIDEO == ch->channel_id || |
255 | common->started == 2) { | |
60aa38d8 LP |
256 | channel1_intr_assert(); |
257 | channel1_intr_enable(1); | |
258 | enable_channel1(1); | |
259 | } | |
60aa38d8 LP |
260 | |
261 | return 0; | |
bebd8d12 LP |
262 | |
263 | err: | |
264 | list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) { | |
265 | list_del(&buf->list); | |
266 | vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED); | |
267 | } | |
268 | ||
269 | return ret; | |
60aa38d8 LP |
270 | } |
271 | ||
41b9f24e LP |
272 | /** |
273 | * vpif_stop_streaming : Stop the DMA engine | |
274 | * @vq: ptr to vb2_queue | |
275 | * | |
276 | * This callback stops the DMA engine and any remaining buffers | |
277 | * in the DMA queue are released. | |
278 | */ | |
e37559b2 | 279 | static void vpif_stop_streaming(struct vb2_queue *vq) |
60aa38d8 | 280 | { |
fa09accc | 281 | struct channel_obj *ch = vb2_get_drv_priv(vq); |
60aa38d8 | 282 | struct common_obj *common; |
aec96832 | 283 | unsigned long flags; |
d9a3b13c | 284 | int ret; |
60aa38d8 | 285 | |
60aa38d8 LP |
286 | common = &ch->common[VPIF_VIDEO_INDEX]; |
287 | ||
e6ba3db1 LP |
288 | /* Disable channel as per its device type and channel id */ |
289 | if (VPIF_CHANNEL0_VIDEO == ch->channel_id) { | |
290 | enable_channel0(0); | |
291 | channel0_intr_enable(0); | |
292 | } | |
41b9f24e LP |
293 | if (VPIF_CHANNEL1_VIDEO == ch->channel_id || |
294 | 2 == common->started) { | |
e6ba3db1 LP |
295 | enable_channel1(0); |
296 | channel1_intr_enable(0); | |
297 | } | |
298 | common->started = 0; | |
299 | ||
d9a3b13c LP |
300 | ret = v4l2_subdev_call(ch->sd, video, s_stream, 0); |
301 | if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) | |
302 | vpif_dbg(1, debug, "stream off failed in subdev\n"); | |
303 | ||
60aa38d8 | 304 | /* release all active buffers */ |
aec96832 | 305 | spin_lock_irqsave(&common->irqlock, flags); |
e6ba3db1 LP |
306 | if (common->cur_frm == common->next_frm) { |
307 | vb2_buffer_done(&common->cur_frm->vb, VB2_BUF_STATE_ERROR); | |
308 | } else { | |
309 | if (common->cur_frm != NULL) | |
310 | vb2_buffer_done(&common->cur_frm->vb, | |
311 | VB2_BUF_STATE_ERROR); | |
312 | if (common->next_frm != NULL) | |
313 | vb2_buffer_done(&common->next_frm->vb, | |
314 | VB2_BUF_STATE_ERROR); | |
315 | } | |
316 | ||
60aa38d8 LP |
317 | while (!list_empty(&common->dma_queue)) { |
318 | common->next_frm = list_entry(common->dma_queue.next, | |
319 | struct vpif_cap_buffer, list); | |
320 | list_del(&common->next_frm->list); | |
321 | vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR); | |
322 | } | |
aec96832 | 323 | spin_unlock_irqrestore(&common->irqlock, flags); |
60aa38d8 LP |
324 | } |
325 | ||
326 | static struct vb2_ops video_qops = { | |
327 | .queue_setup = vpif_buffer_queue_setup, | |
60aa38d8 LP |
328 | .buf_prepare = vpif_buffer_prepare, |
329 | .start_streaming = vpif_start_streaming, | |
330 | .stop_streaming = vpif_stop_streaming, | |
60aa38d8 LP |
331 | .buf_queue = vpif_buffer_queue, |
332 | }; | |
333 | ||
6ffefff5 MK |
334 | /** |
335 | * vpif_process_buffer_complete: process a completed buffer | |
336 | * @common: ptr to common channel object | |
337 | * | |
338 | * This function time stamp the buffer and mark it as DONE. It also | |
339 | * wake up any process waiting on the QUEUE and set the next buffer | |
340 | * as current | |
341 | */ | |
342 | static void vpif_process_buffer_complete(struct common_obj *common) | |
343 | { | |
8e6057b5 | 344 | v4l2_get_timestamp(&common->cur_frm->vb.v4l2_buf.timestamp); |
60aa38d8 LP |
345 | vb2_buffer_done(&common->cur_frm->vb, |
346 | VB2_BUF_STATE_DONE); | |
6ffefff5 MK |
347 | /* Make curFrm pointing to nextFrm */ |
348 | common->cur_frm = common->next_frm; | |
349 | } | |
350 | ||
351 | /** | |
352 | * vpif_schedule_next_buffer: set next buffer address for capture | |
353 | * @common : ptr to common channel object | |
354 | * | |
355 | * This function will get next buffer from the dma queue and | |
356 | * set the buffer address in the vpif register for capture. | |
357 | * the buffer is marked active | |
358 | */ | |
359 | static void vpif_schedule_next_buffer(struct common_obj *common) | |
360 | { | |
361 | unsigned long addr = 0; | |
362 | ||
aec96832 | 363 | spin_lock(&common->irqlock); |
6ffefff5 | 364 | common->next_frm = list_entry(common->dma_queue.next, |
60aa38d8 | 365 | struct vpif_cap_buffer, list); |
6ffefff5 | 366 | /* Remove that buffer from the buffer queue */ |
60aa38d8 | 367 | list_del(&common->next_frm->list); |
aec96832 | 368 | spin_unlock(&common->irqlock); |
60aa38d8 LP |
369 | common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE; |
370 | addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0); | |
6ffefff5 MK |
371 | |
372 | /* Set top and bottom field addresses in VPIF registers */ | |
373 | common->set_addr(addr + common->ytop_off, | |
374 | addr + common->ybtm_off, | |
375 | addr + common->ctop_off, | |
376 | addr + common->cbtm_off); | |
377 | } | |
378 | ||
379 | /** | |
380 | * vpif_channel_isr : ISR handler for vpif capture | |
381 | * @irq: irq number | |
382 | * @dev_id: dev_id ptr | |
383 | * | |
384 | * It changes status of the captured buffer, takes next buffer from the queue | |
2c0ddd17 | 385 | * and sets its address in VPIF registers |
6ffefff5 MK |
386 | */ |
387 | static irqreturn_t vpif_channel_isr(int irq, void *dev_id) | |
388 | { | |
389 | struct vpif_device *dev = &vpif_obj; | |
390 | struct common_obj *common; | |
391 | struct channel_obj *ch; | |
392 | enum v4l2_field field; | |
393 | int channel_id = 0; | |
394 | int fid = -1, i; | |
395 | ||
396 | channel_id = *(int *)(dev_id); | |
b1fc4230 MH |
397 | if (!vpif_intr_status(channel_id)) |
398 | return IRQ_NONE; | |
399 | ||
6ffefff5 MK |
400 | ch = dev->dev[channel_id]; |
401 | ||
402 | field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field; | |
403 | ||
404 | for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) { | |
405 | common = &ch->common[i]; | |
406 | /* skip If streaming is not started in this channel */ | |
407 | if (0 == common->started) | |
408 | continue; | |
409 | ||
410 | /* Check the field format */ | |
411 | if (1 == ch->vpifparams.std_info.frm_fmt) { | |
412 | /* Progressive mode */ | |
aec96832 HV |
413 | spin_lock(&common->irqlock); |
414 | if (list_empty(&common->dma_queue)) { | |
415 | spin_unlock(&common->irqlock); | |
6ffefff5 | 416 | continue; |
aec96832 HV |
417 | } |
418 | spin_unlock(&common->irqlock); | |
6ffefff5 MK |
419 | |
420 | if (!channel_first_int[i][channel_id]) | |
421 | vpif_process_buffer_complete(common); | |
422 | ||
423 | channel_first_int[i][channel_id] = 0; | |
424 | ||
425 | vpif_schedule_next_buffer(common); | |
426 | ||
427 | ||
428 | channel_first_int[i][channel_id] = 0; | |
429 | } else { | |
430 | /** | |
431 | * Interlaced mode. If it is first interrupt, ignore | |
432 | * it | |
433 | */ | |
434 | if (channel_first_int[i][channel_id]) { | |
435 | channel_first_int[i][channel_id] = 0; | |
436 | continue; | |
437 | } | |
438 | if (0 == i) { | |
439 | ch->field_id ^= 1; | |
440 | /* Get field id from VPIF registers */ | |
441 | fid = vpif_channel_getfid(ch->channel_id); | |
442 | if (fid != ch->field_id) { | |
443 | /** | |
444 | * If field id does not match stored | |
445 | * field id, make them in sync | |
446 | */ | |
447 | if (0 == fid) | |
448 | ch->field_id = fid; | |
449 | return IRQ_HANDLED; | |
450 | } | |
451 | } | |
452 | /* device field id and local field id are in sync */ | |
453 | if (0 == fid) { | |
454 | /* this is even field */ | |
455 | if (common->cur_frm == common->next_frm) | |
456 | continue; | |
457 | ||
458 | /* mark the current buffer as done */ | |
459 | vpif_process_buffer_complete(common); | |
460 | } else if (1 == fid) { | |
461 | /* odd field */ | |
aec96832 | 462 | spin_lock(&common->irqlock); |
6ffefff5 | 463 | if (list_empty(&common->dma_queue) || |
aec96832 HV |
464 | (common->cur_frm != common->next_frm)) { |
465 | spin_unlock(&common->irqlock); | |
6ffefff5 | 466 | continue; |
aec96832 HV |
467 | } |
468 | spin_unlock(&common->irqlock); | |
6ffefff5 MK |
469 | |
470 | vpif_schedule_next_buffer(common); | |
471 | } | |
472 | } | |
473 | } | |
474 | return IRQ_HANDLED; | |
475 | } | |
476 | ||
477 | /** | |
478 | * vpif_update_std_info() - update standard related info | |
479 | * @ch: ptr to channel object | |
480 | * | |
481 | * For a given standard selected by application, update values | |
482 | * in the device data structures | |
483 | */ | |
484 | static int vpif_update_std_info(struct channel_obj *ch) | |
485 | { | |
486 | struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX]; | |
487 | struct vpif_params *vpifparams = &ch->vpifparams; | |
488 | const struct vpif_channel_config_params *config; | |
2c0ddd17 | 489 | struct vpif_channel_config_params *std_info = &vpifparams->std_info; |
6ffefff5 MK |
490 | struct video_obj *vid_ch = &ch->video; |
491 | int index; | |
492 | ||
493 | vpif_dbg(2, debug, "vpif_update_std_info\n"); | |
494 | ||
aa444406 | 495 | for (index = 0; index < vpif_ch_params_count; index++) { |
ced9b21f | 496 | config = &vpif_ch_params[index]; |
40c8bcea MR |
497 | if (config->hd_sd == 0) { |
498 | vpif_dbg(2, debug, "SD format\n"); | |
499 | if (config->stdid & vid_ch->stdid) { | |
500 | memcpy(std_info, config, sizeof(*config)); | |
501 | break; | |
502 | } | |
503 | } else { | |
504 | vpif_dbg(2, debug, "HD format\n"); | |
0598c17b HV |
505 | if (!memcmp(&config->dv_timings, &vid_ch->dv_timings, |
506 | sizeof(vid_ch->dv_timings))) { | |
40c8bcea MR |
507 | memcpy(std_info, config, sizeof(*config)); |
508 | break; | |
509 | } | |
6ffefff5 MK |
510 | } |
511 | } | |
512 | ||
513 | /* standard not found */ | |
aa444406 | 514 | if (index == vpif_ch_params_count) |
6ffefff5 MK |
515 | return -EINVAL; |
516 | ||
517 | common->fmt.fmt.pix.width = std_info->width; | |
518 | common->width = std_info->width; | |
519 | common->fmt.fmt.pix.height = std_info->height; | |
520 | common->height = std_info->height; | |
521 | common->fmt.fmt.pix.bytesperline = std_info->width; | |
522 | vpifparams->video_params.hpitch = std_info->width; | |
523 | vpifparams->video_params.storage_mode = std_info->frm_fmt; | |
2c0ddd17 | 524 | |
6ffefff5 MK |
525 | return 0; |
526 | } | |
527 | ||
528 | /** | |
529 | * vpif_calculate_offsets : This function calculates buffers offsets | |
530 | * @ch : ptr to channel object | |
531 | * | |
532 | * This function calculates buffer offsets for Y and C in the top and | |
533 | * bottom field | |
534 | */ | |
535 | static void vpif_calculate_offsets(struct channel_obj *ch) | |
536 | { | |
537 | unsigned int hpitch, vpitch, sizeimage; | |
538 | struct video_obj *vid_ch = &(ch->video); | |
539 | struct vpif_params *vpifparams = &ch->vpifparams; | |
540 | struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX]; | |
541 | enum v4l2_field field = common->fmt.fmt.pix.field; | |
542 | ||
543 | vpif_dbg(2, debug, "vpif_calculate_offsets\n"); | |
544 | ||
545 | if (V4L2_FIELD_ANY == field) { | |
546 | if (vpifparams->std_info.frm_fmt) | |
547 | vid_ch->buf_field = V4L2_FIELD_NONE; | |
548 | else | |
549 | vid_ch->buf_field = V4L2_FIELD_INTERLACED; | |
550 | } else | |
551 | vid_ch->buf_field = common->fmt.fmt.pix.field; | |
552 | ||
60aa38d8 | 553 | sizeimage = common->fmt.fmt.pix.sizeimage; |
6ffefff5 MK |
554 | |
555 | hpitch = common->fmt.fmt.pix.bytesperline; | |
556 | vpitch = sizeimage / (hpitch * 2); | |
557 | ||
558 | if ((V4L2_FIELD_NONE == vid_ch->buf_field) || | |
559 | (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) { | |
560 | /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */ | |
561 | common->ytop_off = 0; | |
562 | common->ybtm_off = hpitch; | |
563 | common->ctop_off = sizeimage / 2; | |
564 | common->cbtm_off = sizeimage / 2 + hpitch; | |
565 | } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) { | |
566 | /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */ | |
567 | common->ytop_off = 0; | |
568 | common->ybtm_off = sizeimage / 4; | |
569 | common->ctop_off = sizeimage / 2; | |
570 | common->cbtm_off = common->ctop_off + sizeimage / 4; | |
571 | } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) { | |
572 | /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */ | |
573 | common->ybtm_off = 0; | |
574 | common->ytop_off = sizeimage / 4; | |
575 | common->cbtm_off = sizeimage / 2; | |
576 | common->ctop_off = common->cbtm_off + sizeimage / 4; | |
577 | } | |
578 | if ((V4L2_FIELD_NONE == vid_ch->buf_field) || | |
579 | (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) | |
580 | vpifparams->video_params.storage_mode = 1; | |
581 | else | |
582 | vpifparams->video_params.storage_mode = 0; | |
583 | ||
584 | if (1 == vpifparams->std_info.frm_fmt) | |
585 | vpifparams->video_params.hpitch = | |
586 | common->fmt.fmt.pix.bytesperline; | |
587 | else { | |
588 | if ((field == V4L2_FIELD_ANY) | |
589 | || (field == V4L2_FIELD_INTERLACED)) | |
590 | vpifparams->video_params.hpitch = | |
591 | common->fmt.fmt.pix.bytesperline * 2; | |
592 | else | |
593 | vpifparams->video_params.hpitch = | |
594 | common->fmt.fmt.pix.bytesperline; | |
595 | } | |
596 | ||
597 | ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid; | |
598 | } | |
599 | ||
600 | /** | |
601 | * vpif_config_format: configure default frame format in the device | |
602 | * ch : ptr to channel object | |
603 | */ | |
604 | static void vpif_config_format(struct channel_obj *ch) | |
605 | { | |
606 | struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX]; | |
607 | ||
608 | vpif_dbg(2, debug, "vpif_config_format\n"); | |
609 | ||
610 | common->fmt.fmt.pix.field = V4L2_FIELD_ANY; | |
6ffefff5 MK |
611 | common->fmt.fmt.pix.sizeimage |
612 | = config_params.channel_bufsize[ch->channel_id]; | |
613 | ||
614 | if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) | |
615 | common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8; | |
616 | else | |
617 | common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P; | |
618 | common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
619 | } | |
620 | ||
621 | /** | |
622 | * vpif_get_default_field() - Get default field type based on interface | |
623 | * @vpif_params - ptr to vpif params | |
624 | */ | |
625 | static inline enum v4l2_field vpif_get_default_field( | |
626 | struct vpif_interface *iface) | |
627 | { | |
628 | return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE : | |
629 | V4L2_FIELD_INTERLACED; | |
630 | } | |
631 | ||
632 | /** | |
633 | * vpif_check_format() - check given pixel format for compatibility | |
634 | * @ch - channel ptr | |
635 | * @pixfmt - Given pixel format | |
636 | * @update - update the values as per hardware requirement | |
637 | * | |
638 | * Check the application pixel format for S_FMT and update the input | |
639 | * values as per hardware limits for TRY_FMT. The default pixel and | |
640 | * field format is selected based on interface type. | |
641 | */ | |
642 | static int vpif_check_format(struct channel_obj *ch, | |
643 | struct v4l2_pix_format *pixfmt, | |
644 | int update) | |
645 | { | |
646 | struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]); | |
647 | struct vpif_params *vpif_params = &ch->vpifparams; | |
648 | enum v4l2_field field = pixfmt->field; | |
649 | u32 sizeimage, hpitch, vpitch; | |
650 | int ret = -EINVAL; | |
651 | ||
652 | vpif_dbg(2, debug, "vpif_check_format\n"); | |
653 | /** | |
654 | * first check for the pixel format. If if_type is Raw bayer, | |
655 | * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only | |
656 | * V4L2_PIX_FMT_YUV422P is supported | |
657 | */ | |
658 | if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) { | |
659 | if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8) { | |
660 | if (!update) { | |
661 | vpif_dbg(2, debug, "invalid pix format\n"); | |
662 | goto exit; | |
663 | } | |
664 | pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8; | |
665 | } | |
666 | } else { | |
667 | if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P) { | |
668 | if (!update) { | |
669 | vpif_dbg(2, debug, "invalid pixel format\n"); | |
670 | goto exit; | |
671 | } | |
672 | pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P; | |
673 | } | |
674 | } | |
675 | ||
676 | if (!(VPIF_VALID_FIELD(field))) { | |
677 | if (!update) { | |
678 | vpif_dbg(2, debug, "invalid field format\n"); | |
679 | goto exit; | |
680 | } | |
681 | /** | |
682 | * By default use FIELD_NONE for RAW Bayer capture | |
683 | * and FIELD_INTERLACED for other interfaces | |
684 | */ | |
685 | field = vpif_get_default_field(&vpif_params->iface); | |
686 | } else if (field == V4L2_FIELD_ANY) | |
687 | /* unsupported field. Use default */ | |
688 | field = vpif_get_default_field(&vpif_params->iface); | |
689 | ||
690 | /* validate the hpitch */ | |
691 | hpitch = pixfmt->bytesperline; | |
692 | if (hpitch < vpif_params->std_info.width) { | |
693 | if (!update) { | |
694 | vpif_dbg(2, debug, "invalid hpitch\n"); | |
695 | goto exit; | |
696 | } | |
697 | hpitch = vpif_params->std_info.width; | |
698 | } | |
699 | ||
60aa38d8 | 700 | sizeimage = pixfmt->sizeimage; |
6ffefff5 MK |
701 | |
702 | vpitch = sizeimage / (hpitch * 2); | |
703 | ||
704 | /* validate the vpitch */ | |
705 | if (vpitch < vpif_params->std_info.height) { | |
706 | if (!update) { | |
707 | vpif_dbg(2, debug, "Invalid vpitch\n"); | |
708 | goto exit; | |
709 | } | |
710 | vpitch = vpif_params->std_info.height; | |
711 | } | |
712 | ||
713 | /* Check for 8 byte alignment */ | |
714 | if (!ALIGN(hpitch, 8)) { | |
715 | if (!update) { | |
716 | vpif_dbg(2, debug, "invalid pitch alignment\n"); | |
717 | goto exit; | |
718 | } | |
719 | /* adjust to next 8 byte boundary */ | |
720 | hpitch = (((hpitch + 7) / 8) * 8); | |
721 | } | |
722 | /* if update is set, modify the bytesperline and sizeimage */ | |
723 | if (update) { | |
724 | pixfmt->bytesperline = hpitch; | |
725 | pixfmt->sizeimage = hpitch * vpitch * 2; | |
726 | } | |
727 | /** | |
728 | * Image width and height is always based on current standard width and | |
729 | * height | |
730 | */ | |
731 | pixfmt->width = common->fmt.fmt.pix.width; | |
732 | pixfmt->height = common->fmt.fmt.pix.height; | |
733 | return 0; | |
734 | exit: | |
735 | return ret; | |
736 | } | |
737 | ||
738 | /** | |
739 | * vpif_config_addr() - function to configure buffer address in vpif | |
740 | * @ch - channel ptr | |
741 | * @muxmode - channel mux mode | |
742 | */ | |
743 | static void vpif_config_addr(struct channel_obj *ch, int muxmode) | |
744 | { | |
745 | struct common_obj *common; | |
746 | ||
747 | vpif_dbg(2, debug, "vpif_config_addr\n"); | |
748 | ||
749 | common = &(ch->common[VPIF_VIDEO_INDEX]); | |
750 | ||
751 | if (VPIF_CHANNEL1_VIDEO == ch->channel_id) | |
752 | common->set_addr = ch1_set_videobuf_addr; | |
753 | else if (2 == muxmode) | |
754 | common->set_addr = ch0_set_videobuf_addr_yc_nmux; | |
755 | else | |
756 | common->set_addr = ch0_set_videobuf_addr; | |
757 | } | |
758 | ||
6ffefff5 | 759 | /** |
178cce12 HV |
760 | * vpif_input_to_subdev() - Maps input to sub device |
761 | * @vpif_cfg - global config ptr | |
762 | * @chan_cfg - channel config ptr | |
6ffefff5 | 763 | * @input_index - Given input index from application |
6ffefff5 MK |
764 | * |
765 | * lookup the sub device information for a given input index. | |
766 | * we report all the inputs to application. inputs table also | |
767 | * has sub device name for the each input | |
768 | */ | |
178cce12 HV |
769 | static int vpif_input_to_subdev( |
770 | struct vpif_capture_config *vpif_cfg, | |
771 | struct vpif_capture_chan_config *chan_cfg, | |
772 | int input_index) | |
6ffefff5 | 773 | { |
178cce12 HV |
774 | struct vpif_subdev_info *subdev_info; |
775 | const char *subdev_name; | |
6ffefff5 MK |
776 | int i; |
777 | ||
178cce12 | 778 | vpif_dbg(2, debug, "vpif_input_to_subdev\n"); |
6ffefff5 | 779 | |
178cce12 HV |
780 | subdev_name = chan_cfg->inputs[input_index].subdev_name; |
781 | if (subdev_name == NULL) | |
782 | return -1; | |
6ffefff5 MK |
783 | |
784 | /* loop through the sub device list to get the sub device info */ | |
785 | for (i = 0; i < vpif_cfg->subdev_count; i++) { | |
786 | subdev_info = &vpif_cfg->subdev_info[i]; | |
787 | if (!strcmp(subdev_info->name, subdev_name)) | |
178cce12 | 788 | return i; |
6ffefff5 | 789 | } |
178cce12 HV |
790 | return -1; |
791 | } | |
6ffefff5 | 792 | |
178cce12 HV |
793 | /** |
794 | * vpif_set_input() - Select an input | |
795 | * @vpif_cfg - global config ptr | |
796 | * @ch - channel | |
797 | * @_index - Given input index from application | |
798 | * | |
799 | * Select the given input. | |
800 | */ | |
801 | static int vpif_set_input( | |
802 | struct vpif_capture_config *vpif_cfg, | |
803 | struct channel_obj *ch, | |
804 | int index) | |
805 | { | |
806 | struct vpif_capture_chan_config *chan_cfg = | |
807 | &vpif_cfg->chan_config[ch->channel_id]; | |
808 | struct vpif_subdev_info *subdev_info = NULL; | |
809 | struct v4l2_subdev *sd = NULL; | |
810 | u32 input = 0, output = 0; | |
811 | int sd_index; | |
812 | int ret; | |
6ffefff5 | 813 | |
178cce12 HV |
814 | sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index); |
815 | if (sd_index >= 0) { | |
816 | sd = vpif_obj.sd[sd_index]; | |
817 | subdev_info = &vpif_cfg->subdev_info[sd_index]; | |
818 | } | |
6ffefff5 | 819 | |
178cce12 HV |
820 | /* first setup input path from sub device to vpif */ |
821 | if (sd && vpif_cfg->setup_input_path) { | |
822 | ret = vpif_cfg->setup_input_path(ch->channel_id, | |
823 | subdev_info->name); | |
824 | if (ret < 0) { | |
825 | vpif_dbg(1, debug, "couldn't setup input path for the" \ | |
826 | " sub device %s, for input index %d\n", | |
827 | subdev_info->name, index); | |
828 | return ret; | |
829 | } | |
830 | } | |
831 | ||
832 | if (sd) { | |
833 | input = chan_cfg->inputs[index].input_route; | |
834 | output = chan_cfg->inputs[index].output_route; | |
835 | ret = v4l2_subdev_call(sd, video, s_routing, | |
836 | input, output, 0); | |
837 | if (ret < 0 && ret != -ENOIOCTLCMD) { | |
838 | vpif_dbg(1, debug, "Failed to set input\n"); | |
839 | return ret; | |
840 | } | |
841 | } | |
842 | ch->input_idx = index; | |
843 | ch->sd = sd; | |
844 | /* copy interface parameters to vpif */ | |
0d4f35f3 | 845 | ch->vpifparams.iface = chan_cfg->vpif_if; |
178cce12 HV |
846 | |
847 | /* update tvnorms from the sub device input info */ | |
848 | ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std; | |
849 | return 0; | |
6ffefff5 MK |
850 | } |
851 | ||
852 | /** | |
853 | * vpif_querystd() - querystd handler | |
854 | * @file: file ptr | |
855 | * @priv: file handle | |
856 | * @std_id: ptr to std id | |
857 | * | |
858 | * This function is called to detect standard at the selected input | |
859 | */ | |
860 | static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id) | |
861 | { | |
7ff51197 LP |
862 | struct video_device *vdev = video_devdata(file); |
863 | struct channel_obj *ch = video_get_drvdata(vdev); | |
6ffefff5 MK |
864 | int ret = 0; |
865 | ||
866 | vpif_dbg(2, debug, "vpif_querystd\n"); | |
867 | ||
6ffefff5 | 868 | /* Call querystd function of decoder device */ |
178cce12 | 869 | ret = v4l2_subdev_call(ch->sd, video, querystd, std_id); |
6ffefff5 | 870 | |
178cce12 HV |
871 | if (ret == -ENOIOCTLCMD || ret == -ENODEV) |
872 | return -ENODATA; | |
873 | if (ret) { | |
874 | vpif_dbg(1, debug, "Failed to query standard for sub devices\n"); | |
875 | return ret; | |
876 | } | |
877 | ||
878 | return 0; | |
6ffefff5 MK |
879 | } |
880 | ||
881 | /** | |
882 | * vpif_g_std() - get STD handler | |
883 | * @file: file ptr | |
884 | * @priv: file handle | |
885 | * @std_id: ptr to std id | |
886 | */ | |
887 | static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std) | |
888 | { | |
7ff51197 LP |
889 | struct video_device *vdev = video_devdata(file); |
890 | struct channel_obj *ch = video_get_drvdata(vdev); | |
6ffefff5 MK |
891 | |
892 | vpif_dbg(2, debug, "vpif_g_std\n"); | |
893 | ||
894 | *std = ch->video.stdid; | |
895 | return 0; | |
896 | } | |
897 | ||
898 | /** | |
899 | * vpif_s_std() - set STD handler | |
900 | * @file: file ptr | |
901 | * @priv: file handle | |
902 | * @std_id: ptr to std id | |
903 | */ | |
314527ac | 904 | static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id) |
6ffefff5 | 905 | { |
7ff51197 LP |
906 | struct video_device *vdev = video_devdata(file); |
907 | struct channel_obj *ch = video_get_drvdata(vdev); | |
6ffefff5 MK |
908 | struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX]; |
909 | int ret = 0; | |
910 | ||
911 | vpif_dbg(2, debug, "vpif_s_std\n"); | |
912 | ||
913 | if (common->started) { | |
914 | vpif_err("streaming in progress\n"); | |
915 | return -EBUSY; | |
916 | } | |
917 | ||
6ffefff5 | 918 | /* Call encoder subdevice function to set the standard */ |
314527ac | 919 | ch->video.stdid = std_id; |
0598c17b | 920 | memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings)); |
6ffefff5 MK |
921 | |
922 | /* Get the information about the standard */ | |
923 | if (vpif_update_std_info(ch)) { | |
6ffefff5 | 924 | vpif_err("Error getting the standard info\n"); |
46656afa | 925 | return -EINVAL; |
6ffefff5 MK |
926 | } |
927 | ||
928 | /* Configure the default format information */ | |
929 | vpif_config_format(ch); | |
930 | ||
931 | /* set standard in the sub device */ | |
314527ac | 932 | ret = v4l2_subdev_call(ch->sd, core, s_std, std_id); |
178cce12 | 933 | if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) { |
6ffefff5 | 934 | vpif_dbg(1, debug, "Failed to set standard for sub devices\n"); |
178cce12 HV |
935 | return ret; |
936 | } | |
937 | return 0; | |
6ffefff5 MK |
938 | } |
939 | ||
940 | /** | |
941 | * vpif_enum_input() - ENUMINPUT handler | |
942 | * @file: file ptr | |
943 | * @priv: file handle | |
944 | * @input: ptr to input structure | |
945 | */ | |
946 | static int vpif_enum_input(struct file *file, void *priv, | |
947 | struct v4l2_input *input) | |
948 | { | |
949 | ||
950 | struct vpif_capture_config *config = vpif_dev->platform_data; | |
7ff51197 LP |
951 | struct video_device *vdev = video_devdata(file); |
952 | struct channel_obj *ch = video_get_drvdata(vdev); | |
6ffefff5 | 953 | struct vpif_capture_chan_config *chan_cfg; |
6ffefff5 MK |
954 | |
955 | chan_cfg = &config->chan_config[ch->channel_id]; | |
956 | ||
957 | if (input->index >= chan_cfg->input_count) { | |
958 | vpif_dbg(1, debug, "Invalid input index\n"); | |
959 | return -EINVAL; | |
960 | } | |
961 | ||
962 | memcpy(input, &chan_cfg->inputs[input->index].input, | |
963 | sizeof(*input)); | |
964 | return 0; | |
965 | } | |
966 | ||
967 | /** | |
968 | * vpif_g_input() - Get INPUT handler | |
969 | * @file: file ptr | |
970 | * @priv: file handle | |
971 | * @index: ptr to input index | |
972 | */ | |
973 | static int vpif_g_input(struct file *file, void *priv, unsigned int *index) | |
974 | { | |
7ff51197 LP |
975 | struct video_device *vdev = video_devdata(file); |
976 | struct channel_obj *ch = video_get_drvdata(vdev); | |
6ffefff5 | 977 | |
6f47c6c6 | 978 | *index = ch->input_idx; |
6ffefff5 MK |
979 | return 0; |
980 | } | |
981 | ||
982 | /** | |
983 | * vpif_s_input() - Set INPUT handler | |
984 | * @file: file ptr | |
985 | * @priv: file handle | |
986 | * @index: input index | |
987 | */ | |
988 | static int vpif_s_input(struct file *file, void *priv, unsigned int index) | |
989 | { | |
990 | struct vpif_capture_config *config = vpif_dev->platform_data; | |
7ff51197 LP |
991 | struct video_device *vdev = video_devdata(file); |
992 | struct channel_obj *ch = video_get_drvdata(vdev); | |
6ffefff5 | 993 | struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX]; |
7ff51197 | 994 | struct vpif_capture_chan_config *chan_cfg; |
6ffefff5 MK |
995 | |
996 | chan_cfg = &config->chan_config[ch->channel_id]; | |
997 | ||
7aaad131 HV |
998 | if (index >= chan_cfg->input_count) |
999 | return -EINVAL; | |
1000 | ||
6ffefff5 MK |
1001 | if (common->started) { |
1002 | vpif_err("Streaming in progress\n"); | |
1003 | return -EBUSY; | |
1004 | } | |
1005 | ||
178cce12 | 1006 | return vpif_set_input(config, ch, index); |
6ffefff5 MK |
1007 | } |
1008 | ||
1009 | /** | |
1010 | * vpif_enum_fmt_vid_cap() - ENUM_FMT handler | |
1011 | * @file: file ptr | |
1012 | * @priv: file handle | |
1013 | * @index: input index | |
1014 | */ | |
1015 | static int vpif_enum_fmt_vid_cap(struct file *file, void *priv, | |
1016 | struct v4l2_fmtdesc *fmt) | |
1017 | { | |
7ff51197 LP |
1018 | struct video_device *vdev = video_devdata(file); |
1019 | struct channel_obj *ch = video_get_drvdata(vdev); | |
6ffefff5 MK |
1020 | |
1021 | if (fmt->index != 0) { | |
1022 | vpif_dbg(1, debug, "Invalid format index\n"); | |
1023 | return -EINVAL; | |
1024 | } | |
1025 | ||
1026 | /* Fill in the information about format */ | |
1027 | if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) { | |
1028 | fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1029 | strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb"); | |
1030 | fmt->pixelformat = V4L2_PIX_FMT_SBGGR8; | |
1031 | } else { | |
1032 | fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1033 | strcpy(fmt->description, "YCbCr4:2:2 YC Planar"); | |
1034 | fmt->pixelformat = V4L2_PIX_FMT_YUV422P; | |
1035 | } | |
1036 | return 0; | |
1037 | } | |
1038 | ||
1039 | /** | |
1040 | * vpif_try_fmt_vid_cap() - TRY_FMT handler | |
1041 | * @file: file ptr | |
1042 | * @priv: file handle | |
1043 | * @fmt: ptr to v4l2 format structure | |
1044 | */ | |
1045 | static int vpif_try_fmt_vid_cap(struct file *file, void *priv, | |
1046 | struct v4l2_format *fmt) | |
1047 | { | |
7ff51197 LP |
1048 | struct video_device *vdev = video_devdata(file); |
1049 | struct channel_obj *ch = video_get_drvdata(vdev); | |
6ffefff5 MK |
1050 | struct v4l2_pix_format *pixfmt = &fmt->fmt.pix; |
1051 | ||
1052 | return vpif_check_format(ch, pixfmt, 1); | |
1053 | } | |
1054 | ||
1055 | ||
1056 | /** | |
1057 | * vpif_g_fmt_vid_cap() - Set INPUT handler | |
1058 | * @file: file ptr | |
1059 | * @priv: file handle | |
1060 | * @fmt: ptr to v4l2 format structure | |
1061 | */ | |
1062 | static int vpif_g_fmt_vid_cap(struct file *file, void *priv, | |
1063 | struct v4l2_format *fmt) | |
1064 | { | |
7ff51197 LP |
1065 | struct video_device *vdev = video_devdata(file); |
1066 | struct channel_obj *ch = video_get_drvdata(vdev); | |
6ffefff5 MK |
1067 | struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX]; |
1068 | ||
1069 | /* Check the validity of the buffer type */ | |
1070 | if (common->fmt.type != fmt->type) | |
1071 | return -EINVAL; | |
1072 | ||
1073 | /* Fill in the information about format */ | |
6ffefff5 | 1074 | *fmt = common->fmt; |
6ffefff5 MK |
1075 | return 0; |
1076 | } | |
1077 | ||
1078 | /** | |
1079 | * vpif_s_fmt_vid_cap() - Set FMT handler | |
1080 | * @file: file ptr | |
1081 | * @priv: file handle | |
1082 | * @fmt: ptr to v4l2 format structure | |
1083 | */ | |
1084 | static int vpif_s_fmt_vid_cap(struct file *file, void *priv, | |
1085 | struct v4l2_format *fmt) | |
1086 | { | |
7ff51197 LP |
1087 | struct video_device *vdev = video_devdata(file); |
1088 | struct channel_obj *ch = video_get_drvdata(vdev); | |
6ffefff5 MK |
1089 | struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX]; |
1090 | struct v4l2_pix_format *pixfmt; | |
1091 | int ret = 0; | |
1092 | ||
2c0ddd17 | 1093 | vpif_dbg(2, debug, "%s\n", __func__); |
6ffefff5 MK |
1094 | |
1095 | /* If streaming is started, return error */ | |
1096 | if (common->started) { | |
1097 | vpif_dbg(1, debug, "Streaming is started\n"); | |
1098 | return -EBUSY; | |
1099 | } | |
1100 | ||
6ffefff5 MK |
1101 | pixfmt = &fmt->fmt.pix; |
1102 | /* Check for valid field format */ | |
1103 | ret = vpif_check_format(ch, pixfmt, 0); | |
1104 | ||
1105 | if (ret) | |
1106 | return ret; | |
1107 | /* store the format in the channel object */ | |
6ffefff5 | 1108 | common->fmt = *fmt; |
6ffefff5 MK |
1109 | return 0; |
1110 | } | |
1111 | ||
1112 | /** | |
1113 | * vpif_querycap() - QUERYCAP handler | |
1114 | * @file: file ptr | |
1115 | * @priv: file handle | |
1116 | * @cap: ptr to v4l2_capability structure | |
1117 | */ | |
1118 | static int vpif_querycap(struct file *file, void *priv, | |
1119 | struct v4l2_capability *cap) | |
1120 | { | |
1121 | struct vpif_capture_config *config = vpif_dev->platform_data; | |
1122 | ||
626d533f LP |
1123 | cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING; |
1124 | cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; | |
1125 | snprintf(cap->driver, sizeof(cap->driver), "%s", dev_name(vpif_dev)); | |
1126 | snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s", | |
1127 | dev_name(vpif_dev)); | |
6ffefff5 MK |
1128 | strlcpy(cap->card, config->card_name, sizeof(cap->card)); |
1129 | ||
1130 | return 0; | |
1131 | } | |
1132 | ||
6ffefff5 MK |
1133 | /** |
1134 | * vpif_cropcap() - cropcap handler | |
1135 | * @file: file ptr | |
1136 | * @priv: file handle | |
1137 | * @crop: ptr to v4l2_cropcap structure | |
1138 | */ | |
1139 | static int vpif_cropcap(struct file *file, void *priv, | |
1140 | struct v4l2_cropcap *crop) | |
1141 | { | |
7ff51197 LP |
1142 | struct video_device *vdev = video_devdata(file); |
1143 | struct channel_obj *ch = video_get_drvdata(vdev); | |
6ffefff5 MK |
1144 | struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX]; |
1145 | ||
1146 | if (V4L2_BUF_TYPE_VIDEO_CAPTURE != crop->type) | |
1147 | return -EINVAL; | |
1148 | ||
1149 | crop->bounds.left = 0; | |
1150 | crop->bounds.top = 0; | |
1151 | crop->bounds.height = common->height; | |
1152 | crop->bounds.width = common->width; | |
1153 | crop->defrect = crop->bounds; | |
1154 | return 0; | |
1155 | } | |
1156 | ||
40c8bcea | 1157 | /** |
0598c17b | 1158 | * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler |
40c8bcea MR |
1159 | * @file: file ptr |
1160 | * @priv: file handle | |
0598c17b | 1161 | * @timings: input timings |
40c8bcea | 1162 | */ |
0598c17b HV |
1163 | static int |
1164 | vpif_enum_dv_timings(struct file *file, void *priv, | |
1165 | struct v4l2_enum_dv_timings *timings) | |
40c8bcea | 1166 | { |
7ff51197 LP |
1167 | struct video_device *vdev = video_devdata(file); |
1168 | struct channel_obj *ch = video_get_drvdata(vdev); | |
178cce12 | 1169 | int ret; |
40c8bcea | 1170 | |
178cce12 | 1171 | ret = v4l2_subdev_call(ch->sd, video, enum_dv_timings, timings); |
e070f1b4 | 1172 | if (ret == -ENOIOCTLCMD || ret == -ENODEV) |
178cce12 HV |
1173 | return -EINVAL; |
1174 | return ret; | |
40c8bcea MR |
1175 | } |
1176 | ||
1177 | /** | |
0598c17b | 1178 | * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler |
40c8bcea MR |
1179 | * @file: file ptr |
1180 | * @priv: file handle | |
0598c17b | 1181 | * @timings: input timings |
40c8bcea | 1182 | */ |
0598c17b HV |
1183 | static int |
1184 | vpif_query_dv_timings(struct file *file, void *priv, | |
1185 | struct v4l2_dv_timings *timings) | |
40c8bcea | 1186 | { |
7ff51197 LP |
1187 | struct video_device *vdev = video_devdata(file); |
1188 | struct channel_obj *ch = video_get_drvdata(vdev); | |
178cce12 | 1189 | int ret; |
40c8bcea | 1190 | |
178cce12 | 1191 | ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings); |
e070f1b4 | 1192 | if (ret == -ENOIOCTLCMD || ret == -ENODEV) |
178cce12 HV |
1193 | return -ENODATA; |
1194 | return ret; | |
40c8bcea MR |
1195 | } |
1196 | ||
c027e165 MR |
1197 | /** |
1198 | * vpif_s_dv_timings() - S_DV_TIMINGS handler | |
1199 | * @file: file ptr | |
1200 | * @priv: file handle | |
1201 | * @timings: digital video timings | |
1202 | */ | |
1203 | static int vpif_s_dv_timings(struct file *file, void *priv, | |
1204 | struct v4l2_dv_timings *timings) | |
1205 | { | |
7ff51197 LP |
1206 | struct video_device *vdev = video_devdata(file); |
1207 | struct channel_obj *ch = video_get_drvdata(vdev); | |
c027e165 MR |
1208 | struct vpif_params *vpifparams = &ch->vpifparams; |
1209 | struct vpif_channel_config_params *std_info = &vpifparams->std_info; | |
1210 | struct video_obj *vid_ch = &ch->video; | |
0598c17b | 1211 | struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt; |
c027e165 MR |
1212 | int ret; |
1213 | ||
1214 | if (timings->type != V4L2_DV_BT_656_1120) { | |
1215 | vpif_dbg(2, debug, "Timing type not defined\n"); | |
1216 | return -EINVAL; | |
1217 | } | |
1218 | ||
1219 | /* Configure subdevice timings, if any */ | |
178cce12 HV |
1220 | ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings); |
1221 | if (ret == -ENOIOCTLCMD || ret == -ENODEV) | |
1222 | ret = 0; | |
c027e165 MR |
1223 | if (ret < 0) { |
1224 | vpif_dbg(2, debug, "Error setting custom DV timings\n"); | |
1225 | return ret; | |
1226 | } | |
1227 | ||
1228 | if (!(timings->bt.width && timings->bt.height && | |
1229 | (timings->bt.hbackporch || | |
1230 | timings->bt.hfrontporch || | |
1231 | timings->bt.hsync) && | |
1232 | timings->bt.vfrontporch && | |
1233 | (timings->bt.vbackporch || | |
1234 | timings->bt.vsync))) { | |
1235 | vpif_dbg(2, debug, "Timings for width, height, " | |
1236 | "horizontal back porch, horizontal sync, " | |
1237 | "horizontal front porch, vertical back porch, " | |
1238 | "vertical sync and vertical back porch " | |
1239 | "must be defined\n"); | |
1240 | return -EINVAL; | |
1241 | } | |
1242 | ||
0598c17b | 1243 | vid_ch->dv_timings = *timings; |
c027e165 MR |
1244 | |
1245 | /* Configure video port timings */ | |
1246 | ||
e3655268 | 1247 | std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8; |
c027e165 MR |
1248 | std_info->sav2eav = bt->width; |
1249 | ||
1250 | std_info->l1 = 1; | |
1251 | std_info->l3 = bt->vsync + bt->vbackporch + 1; | |
1252 | ||
e3655268 | 1253 | std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt); |
c027e165 MR |
1254 | if (bt->interlaced) { |
1255 | if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) { | |
c027e165 MR |
1256 | std_info->l5 = std_info->vsize/2 - |
1257 | (bt->vfrontporch - 1); | |
1258 | std_info->l7 = std_info->vsize/2 + 1; | |
1259 | std_info->l9 = std_info->l7 + bt->il_vsync + | |
1260 | bt->il_vbackporch + 1; | |
1261 | std_info->l11 = std_info->vsize - | |
1262 | (bt->il_vfrontporch - 1); | |
1263 | } else { | |
1264 | vpif_dbg(2, debug, "Required timing values for " | |
1265 | "interlaced BT format missing\n"); | |
1266 | return -EINVAL; | |
1267 | } | |
1268 | } else { | |
c027e165 MR |
1269 | std_info->l5 = std_info->vsize - (bt->vfrontporch - 1); |
1270 | } | |
1271 | strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME); | |
1272 | std_info->width = bt->width; | |
1273 | std_info->height = bt->height; | |
1274 | std_info->frm_fmt = bt->interlaced ? 0 : 1; | |
1275 | std_info->ycmux_mode = 0; | |
1276 | std_info->capture_format = 0; | |
1277 | std_info->vbi_supported = 0; | |
1278 | std_info->hd_sd = 1; | |
1279 | std_info->stdid = 0; | |
c027e165 MR |
1280 | |
1281 | vid_ch->stdid = 0; | |
c027e165 MR |
1282 | return 0; |
1283 | } | |
1284 | ||
1285 | /** | |
1286 | * vpif_g_dv_timings() - G_DV_TIMINGS handler | |
1287 | * @file: file ptr | |
1288 | * @priv: file handle | |
1289 | * @timings: digital video timings | |
1290 | */ | |
1291 | static int vpif_g_dv_timings(struct file *file, void *priv, | |
1292 | struct v4l2_dv_timings *timings) | |
1293 | { | |
7ff51197 LP |
1294 | struct video_device *vdev = video_devdata(file); |
1295 | struct channel_obj *ch = video_get_drvdata(vdev); | |
c027e165 | 1296 | struct video_obj *vid_ch = &ch->video; |
c027e165 | 1297 | |
0598c17b | 1298 | *timings = vid_ch->dv_timings; |
c027e165 MR |
1299 | |
1300 | return 0; | |
1301 | } | |
1302 | ||
7036d6a7 MR |
1303 | /* |
1304 | * vpif_log_status() - Status information | |
1305 | * @file: file ptr | |
1306 | * @priv: file handle | |
1307 | * | |
1308 | * Returns zero. | |
1309 | */ | |
1310 | static int vpif_log_status(struct file *filep, void *priv) | |
1311 | { | |
1312 | /* status for sub devices */ | |
1313 | v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status); | |
1314 | ||
1315 | return 0; | |
1316 | } | |
1317 | ||
6ffefff5 MK |
1318 | /* vpif capture ioctl operations */ |
1319 | static const struct v4l2_ioctl_ops vpif_ioctl_ops = { | |
1320 | .vidioc_querycap = vpif_querycap, | |
6ffefff5 MK |
1321 | .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap, |
1322 | .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap, | |
1323 | .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap, | |
1324 | .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap, | |
1325 | .vidioc_enum_input = vpif_enum_input, | |
1326 | .vidioc_s_input = vpif_s_input, | |
1327 | .vidioc_g_input = vpif_g_input, | |
d9a3b13c LP |
1328 | |
1329 | .vidioc_reqbufs = vb2_ioctl_reqbufs, | |
1330 | .vidioc_create_bufs = vb2_ioctl_create_bufs, | |
1331 | .vidioc_querybuf = vb2_ioctl_querybuf, | |
1332 | .vidioc_qbuf = vb2_ioctl_qbuf, | |
1333 | .vidioc_dqbuf = vb2_ioctl_dqbuf, | |
1334 | .vidioc_expbuf = vb2_ioctl_expbuf, | |
1335 | .vidioc_streamon = vb2_ioctl_streamon, | |
1336 | .vidioc_streamoff = vb2_ioctl_streamoff, | |
1337 | ||
6ffefff5 MK |
1338 | .vidioc_querystd = vpif_querystd, |
1339 | .vidioc_s_std = vpif_s_std, | |
1340 | .vidioc_g_std = vpif_g_std, | |
d9a3b13c | 1341 | |
6ffefff5 | 1342 | .vidioc_cropcap = vpif_cropcap, |
0598c17b HV |
1343 | .vidioc_enum_dv_timings = vpif_enum_dv_timings, |
1344 | .vidioc_query_dv_timings = vpif_query_dv_timings, | |
c027e165 MR |
1345 | .vidioc_s_dv_timings = vpif_s_dv_timings, |
1346 | .vidioc_g_dv_timings = vpif_g_dv_timings, | |
7036d6a7 | 1347 | .vidioc_log_status = vpif_log_status, |
6ffefff5 MK |
1348 | }; |
1349 | ||
1350 | /* vpif file operations */ | |
1351 | static struct v4l2_file_operations vpif_fops = { | |
1352 | .owner = THIS_MODULE, | |
7ff51197 LP |
1353 | .open = v4l2_fh_open, |
1354 | .release = vb2_fop_release, | |
46656afa | 1355 | .unlocked_ioctl = video_ioctl2, |
d26d26b5 LP |
1356 | .mmap = vb2_fop_mmap, |
1357 | .poll = vb2_fop_poll | |
6ffefff5 MK |
1358 | }; |
1359 | ||
1360 | /* vpif video template */ | |
1361 | static struct video_device vpif_video_template = { | |
1362 | .name = "vpif", | |
1363 | .fops = &vpif_fops, | |
1364 | .minor = -1, | |
1365 | .ioctl_ops = &vpif_ioctl_ops, | |
1366 | }; | |
1367 | ||
1368 | /** | |
1369 | * initialize_vpif() - Initialize vpif data structures | |
1370 | * | |
1371 | * Allocate memory for data structures and initialize them | |
1372 | */ | |
1373 | static int initialize_vpif(void) | |
1374 | { | |
1375 | int err = 0, i, j; | |
1376 | int free_channel_objects_index; | |
1377 | ||
1378 | /* Default number of buffers should be 3 */ | |
1379 | if ((ch0_numbuffers > 0) && | |
1380 | (ch0_numbuffers < config_params.min_numbuffers)) | |
1381 | ch0_numbuffers = config_params.min_numbuffers; | |
1382 | if ((ch1_numbuffers > 0) && | |
1383 | (ch1_numbuffers < config_params.min_numbuffers)) | |
1384 | ch1_numbuffers = config_params.min_numbuffers; | |
1385 | ||
1386 | /* Set buffer size to min buffers size if it is invalid */ | |
1387 | if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO]) | |
1388 | ch0_bufsize = | |
1389 | config_params.min_bufsize[VPIF_CHANNEL0_VIDEO]; | |
1390 | if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO]) | |
1391 | ch1_bufsize = | |
1392 | config_params.min_bufsize[VPIF_CHANNEL1_VIDEO]; | |
1393 | ||
1394 | config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers; | |
1395 | config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers; | |
1396 | if (ch0_numbuffers) { | |
1397 | config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO] | |
1398 | = ch0_bufsize; | |
1399 | } | |
1400 | if (ch1_numbuffers) { | |
1401 | config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO] | |
1402 | = ch1_bufsize; | |
1403 | } | |
1404 | ||
1405 | /* Allocate memory for six channel objects */ | |
1406 | for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) { | |
1407 | vpif_obj.dev[i] = | |
1408 | kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL); | |
1409 | /* If memory allocation fails, return error */ | |
1410 | if (!vpif_obj.dev[i]) { | |
1411 | free_channel_objects_index = i; | |
1412 | err = -ENOMEM; | |
1413 | goto vpif_init_free_channel_objects; | |
1414 | } | |
1415 | } | |
1416 | return 0; | |
1417 | ||
1418 | vpif_init_free_channel_objects: | |
1419 | for (j = 0; j < free_channel_objects_index; j++) | |
1420 | kfree(vpif_obj.dev[j]); | |
1421 | return err; | |
1422 | } | |
1423 | ||
873229e4 LP |
1424 | static int vpif_async_bound(struct v4l2_async_notifier *notifier, |
1425 | struct v4l2_subdev *subdev, | |
1426 | struct v4l2_async_subdev *asd) | |
1427 | { | |
1428 | int i; | |
1429 | ||
1430 | for (i = 0; i < vpif_obj.config->subdev_count; i++) | |
1431 | if (!strcmp(vpif_obj.config->subdev_info[i].name, | |
1432 | subdev->name)) { | |
1433 | vpif_obj.sd[i] = subdev; | |
1434 | return 0; | |
1435 | } | |
1436 | ||
1437 | return -EINVAL; | |
1438 | } | |
1439 | ||
1440 | static int vpif_probe_complete(void) | |
1441 | { | |
1442 | struct common_obj *common; | |
d26d26b5 | 1443 | struct video_device *vdev; |
873229e4 | 1444 | struct channel_obj *ch; |
fa09accc | 1445 | struct vb2_queue *q; |
873229e4 LP |
1446 | int i, j, err, k; |
1447 | ||
1448 | for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) { | |
1449 | ch = vpif_obj.dev[j]; | |
1450 | ch->channel_id = j; | |
1451 | common = &(ch->common[VPIF_VIDEO_INDEX]); | |
1452 | spin_lock_init(&common->irqlock); | |
1453 | mutex_init(&common->lock); | |
873229e4 LP |
1454 | |
1455 | /* select input 0 */ | |
1456 | err = vpif_set_input(vpif_obj.config, ch, 0); | |
1457 | if (err) | |
1458 | goto probe_out; | |
1459 | ||
fa09accc LP |
1460 | /* Initialize vb2 queue */ |
1461 | q = &common->buffer_queue; | |
1462 | q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1463 | q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; | |
1464 | q->drv_priv = ch; | |
1465 | q->ops = &video_qops; | |
1466 | q->mem_ops = &vb2_dma_contig_memops; | |
1467 | q->buf_struct_size = sizeof(struct vpif_cap_buffer); | |
1468 | q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; | |
1469 | q->min_buffers_needed = 1; | |
999ba69b | 1470 | q->lock = &common->lock; |
fa09accc LP |
1471 | |
1472 | err = vb2_queue_init(q); | |
1473 | if (err) { | |
1474 | vpif_err("vpif_capture: vb2_queue_init() failed\n"); | |
1475 | goto probe_out; | |
1476 | } | |
1477 | ||
1478 | common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev); | |
1479 | if (IS_ERR(common->alloc_ctx)) { | |
1480 | vpif_err("Failed to get the context\n"); | |
1481 | err = PTR_ERR(common->alloc_ctx); | |
1482 | goto probe_out; | |
1483 | } | |
1484 | ||
1485 | INIT_LIST_HEAD(&common->dma_queue); | |
1486 | ||
d26d26b5 LP |
1487 | vdev = ch->video_dev; |
1488 | vdev->queue = q; | |
7ff51197 LP |
1489 | vdev->lock = &common->lock; |
1490 | set_bit(V4L2_FL_USE_FH_PRIO, &vdev->flags); | |
1491 | video_set_drvdata(ch->video_dev, ch); | |
d26d26b5 | 1492 | err = video_register_device(vdev, |
873229e4 LP |
1493 | VFL_TYPE_GRABBER, (j ? 1 : 0)); |
1494 | if (err) | |
1495 | goto probe_out; | |
1496 | } | |
1497 | ||
1498 | v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n"); | |
1499 | return 0; | |
1500 | ||
1501 | probe_out: | |
1502 | for (k = 0; k < j; k++) { | |
1503 | /* Get the pointer to the channel object */ | |
1504 | ch = vpif_obj.dev[k]; | |
fa09accc LP |
1505 | common = &ch->common[k]; |
1506 | vb2_dma_contig_cleanup_ctx(common->alloc_ctx); | |
873229e4 LP |
1507 | /* Unregister video device */ |
1508 | video_unregister_device(ch->video_dev); | |
1509 | } | |
1510 | kfree(vpif_obj.sd); | |
1511 | for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) { | |
1512 | ch = vpif_obj.dev[i]; | |
1513 | /* Note: does nothing if ch->video_dev == NULL */ | |
1514 | video_device_release(ch->video_dev); | |
1515 | } | |
1516 | v4l2_device_unregister(&vpif_obj.v4l2_dev); | |
1517 | ||
1518 | return err; | |
1519 | } | |
1520 | ||
1521 | static int vpif_async_complete(struct v4l2_async_notifier *notifier) | |
1522 | { | |
1523 | return vpif_probe_complete(); | |
1524 | } | |
1525 | ||
6ffefff5 MK |
1526 | /** |
1527 | * vpif_probe : This function probes the vpif capture driver | |
1528 | * @pdev: platform device pointer | |
1529 | * | |
1530 | * This creates device entries by register itself to the V4L2 driver and | |
1531 | * initializes fields of each channel objects | |
1532 | */ | |
1533 | static __init int vpif_probe(struct platform_device *pdev) | |
1534 | { | |
1535 | struct vpif_subdev_info *subdevdata; | |
873229e4 | 1536 | int i, j, err; |
5be452c3 | 1537 | int res_idx = 0; |
6ffefff5 MK |
1538 | struct i2c_adapter *i2c_adap; |
1539 | struct channel_obj *ch; | |
6ffefff5 MK |
1540 | struct video_device *vfd; |
1541 | struct resource *res; | |
1542 | int subdev_count; | |
1543 | ||
1544 | vpif_dev = &pdev->dev; | |
1545 | ||
1546 | err = initialize_vpif(); | |
1547 | if (err) { | |
1548 | v4l2_err(vpif_dev->driver, "Error initializing vpif\n"); | |
1549 | return err; | |
1550 | } | |
1551 | ||
d2f7a1a2 HV |
1552 | err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev); |
1553 | if (err) { | |
1554 | v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n"); | |
1555 | return err; | |
1556 | } | |
1557 | ||
5be452c3 | 1558 | while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) { |
a76a0b33 LP |
1559 | err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr, |
1560 | IRQF_SHARED, "VPIF_Capture", | |
1561 | (void *)(&vpif_obj.dev[res_idx]-> | |
1562 | channel_id)); | |
1563 | if (err) { | |
1564 | err = -EINVAL; | |
1565 | goto vpif_unregister; | |
6ffefff5 | 1566 | } |
5be452c3 | 1567 | res_idx++; |
6ffefff5 MK |
1568 | } |
1569 | ||
1570 | for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) { | |
1571 | /* Get the pointer to the channel object */ | |
1572 | ch = vpif_obj.dev[i]; | |
1573 | /* Allocate memory for video device */ | |
1574 | vfd = video_device_alloc(); | |
1575 | if (NULL == vfd) { | |
1576 | for (j = 0; j < i; j++) { | |
1577 | ch = vpif_obj.dev[j]; | |
1578 | video_device_release(ch->video_dev); | |
1579 | } | |
1580 | err = -ENOMEM; | |
b2de4f25 | 1581 | goto vpif_unregister; |
6ffefff5 MK |
1582 | } |
1583 | ||
1584 | /* Initialize field of video device */ | |
1585 | *vfd = vpif_video_template; | |
1586 | vfd->v4l2_dev = &vpif_obj.v4l2_dev; | |
1587 | vfd->release = video_device_release; | |
1588 | snprintf(vfd->name, sizeof(vfd->name), | |
0a63172a | 1589 | "VPIF_Capture_DRIVER_V%s", |
64dc3c1a | 1590 | VPIF_CAPTURE_VERSION); |
6ffefff5 MK |
1591 | /* Set video_dev to the video device */ |
1592 | ch->video_dev = vfd; | |
1593 | } | |
1594 | ||
873229e4 | 1595 | vpif_obj.config = pdev->dev.platform_data; |
6ffefff5 | 1596 | |
873229e4 | 1597 | subdev_count = vpif_obj.config->subdev_count; |
1f8766b4 | 1598 | vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count, |
6ffefff5 MK |
1599 | GFP_KERNEL); |
1600 | if (vpif_obj.sd == NULL) { | |
1601 | vpif_err("unable to allocate memory for subdevice pointers\n"); | |
1602 | err = -ENOMEM; | |
5be452c3 | 1603 | goto vpif_sd_error; |
6ffefff5 MK |
1604 | } |
1605 | ||
873229e4 LP |
1606 | if (!vpif_obj.config->asd_sizes) { |
1607 | i2c_adap = i2c_get_adapter(1); | |
1608 | for (i = 0; i < subdev_count; i++) { | |
1609 | subdevdata = &vpif_obj.config->subdev_info[i]; | |
1610 | vpif_obj.sd[i] = | |
1611 | v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev, | |
1612 | i2c_adap, | |
1613 | &subdevdata-> | |
1614 | board_info, | |
1615 | NULL); | |
1616 | ||
1617 | if (!vpif_obj.sd[i]) { | |
1618 | vpif_err("Error registering v4l2 subdevice\n"); | |
2fcd9dc3 | 1619 | err = -ENODEV; |
873229e4 LP |
1620 | goto probe_subdev_out; |
1621 | } | |
1622 | v4l2_info(&vpif_obj.v4l2_dev, | |
1623 | "registered sub device %s\n", | |
1624 | subdevdata->name); | |
1625 | } | |
1626 | vpif_probe_complete(); | |
1627 | } else { | |
e8419d08 | 1628 | vpif_obj.notifier.subdevs = vpif_obj.config->asd; |
873229e4 LP |
1629 | vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0]; |
1630 | vpif_obj.notifier.bound = vpif_async_bound; | |
1631 | vpif_obj.notifier.complete = vpif_async_complete; | |
1632 | err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev, | |
1633 | &vpif_obj.notifier); | |
1634 | if (err) { | |
1635 | vpif_err("Error registering async notifier\n"); | |
1636 | err = -EINVAL; | |
6ffefff5 MK |
1637 | goto probe_subdev_out; |
1638 | } | |
6ffefff5 | 1639 | } |
6ffefff5 MK |
1640 | |
1641 | return 0; | |
1642 | ||
b65814e5 HV |
1643 | probe_subdev_out: |
1644 | /* free sub devices memory */ | |
1645 | kfree(vpif_obj.sd); | |
6ffefff5 | 1646 | |
5be452c3 HV |
1647 | vpif_sd_error: |
1648 | for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) { | |
1649 | ch = vpif_obj.dev[i]; | |
1650 | /* Note: does nothing if ch->video_dev == NULL */ | |
1651 | video_device_release(ch->video_dev); | |
6ffefff5 | 1652 | } |
b2de4f25 | 1653 | vpif_unregister: |
d2f7a1a2 | 1654 | v4l2_device_unregister(&vpif_obj.v4l2_dev); |
b2de4f25 | 1655 | |
6ffefff5 MK |
1656 | return err; |
1657 | } | |
1658 | ||
1659 | /** | |
1660 | * vpif_remove() - driver remove handler | |
1661 | * @device: ptr to platform device structure | |
1662 | * | |
1663 | * The vidoe device is unregistered | |
1664 | */ | |
1665 | static int vpif_remove(struct platform_device *device) | |
1666 | { | |
fa09accc | 1667 | struct common_obj *common; |
6ffefff5 | 1668 | struct channel_obj *ch; |
b2de4f25 | 1669 | int i; |
6ffefff5 MK |
1670 | |
1671 | v4l2_device_unregister(&vpif_obj.v4l2_dev); | |
1672 | ||
410ca605 | 1673 | kfree(vpif_obj.sd); |
6ffefff5 MK |
1674 | /* un-register device */ |
1675 | for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) { | |
1676 | /* Get the pointer to the channel object */ | |
1677 | ch = vpif_obj.dev[i]; | |
fa09accc LP |
1678 | common = &ch->common[i]; |
1679 | vb2_dma_contig_cleanup_ctx(common->alloc_ctx); | |
6ffefff5 MK |
1680 | /* Unregister video device */ |
1681 | video_unregister_device(ch->video_dev); | |
410ca605 | 1682 | kfree(vpif_obj.dev[i]); |
6ffefff5 MK |
1683 | } |
1684 | return 0; | |
1685 | } | |
1686 | ||
3d5946dc | 1687 | #ifdef CONFIG_PM |
6ffefff5 MK |
1688 | /** |
1689 | * vpif_suspend: vpif device suspend | |
6ffefff5 | 1690 | */ |
3d5946dc | 1691 | static int vpif_suspend(struct device *dev) |
6ffefff5 | 1692 | { |
3d5946dc MH |
1693 | |
1694 | struct common_obj *common; | |
1695 | struct channel_obj *ch; | |
1696 | int i; | |
1697 | ||
1698 | for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) { | |
1699 | /* Get the pointer to the channel object */ | |
1700 | ch = vpif_obj.dev[i]; | |
1701 | common = &ch->common[VPIF_VIDEO_INDEX]; | |
1702 | mutex_lock(&common->lock); | |
1703 | if (ch->usrs && common->io_usrs) { | |
1704 | /* Disable channel */ | |
1705 | if (ch->channel_id == VPIF_CHANNEL0_VIDEO) { | |
1706 | enable_channel0(0); | |
1707 | channel0_intr_enable(0); | |
1708 | } | |
1709 | if (ch->channel_id == VPIF_CHANNEL1_VIDEO || | |
1710 | common->started == 2) { | |
1711 | enable_channel1(0); | |
1712 | channel1_intr_enable(0); | |
1713 | } | |
1714 | } | |
1715 | mutex_unlock(&common->lock); | |
1716 | } | |
1717 | ||
1718 | return 0; | |
6ffefff5 MK |
1719 | } |
1720 | ||
3d5946dc | 1721 | /* |
6ffefff5 | 1722 | * vpif_resume: vpif device suspend |
6ffefff5 | 1723 | */ |
3d5946dc | 1724 | static int vpif_resume(struct device *dev) |
6ffefff5 | 1725 | { |
3d5946dc MH |
1726 | struct common_obj *common; |
1727 | struct channel_obj *ch; | |
1728 | int i; | |
1729 | ||
1730 | for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) { | |
1731 | /* Get the pointer to the channel object */ | |
1732 | ch = vpif_obj.dev[i]; | |
1733 | common = &ch->common[VPIF_VIDEO_INDEX]; | |
1734 | mutex_lock(&common->lock); | |
1735 | if (ch->usrs && common->io_usrs) { | |
1736 | /* Disable channel */ | |
1737 | if (ch->channel_id == VPIF_CHANNEL0_VIDEO) { | |
1738 | enable_channel0(1); | |
1739 | channel0_intr_enable(1); | |
1740 | } | |
1741 | if (ch->channel_id == VPIF_CHANNEL1_VIDEO || | |
1742 | common->started == 2) { | |
1743 | enable_channel1(1); | |
1744 | channel1_intr_enable(1); | |
1745 | } | |
1746 | } | |
1747 | mutex_unlock(&common->lock); | |
1748 | } | |
1749 | ||
1750 | return 0; | |
6ffefff5 MK |
1751 | } |
1752 | ||
47145210 | 1753 | static const struct dev_pm_ops vpif_dev_pm_ops = { |
6ffefff5 MK |
1754 | .suspend = vpif_suspend, |
1755 | .resume = vpif_resume, | |
1756 | }; | |
1757 | ||
3d5946dc MH |
1758 | #define vpif_pm_ops (&vpif_dev_pm_ops) |
1759 | #else | |
1760 | #define vpif_pm_ops NULL | |
1761 | #endif | |
1762 | ||
ffa1b391 | 1763 | static __refdata struct platform_driver vpif_driver = { |
6ffefff5 MK |
1764 | .driver = { |
1765 | .name = "vpif_capture", | |
1766 | .owner = THIS_MODULE, | |
3d5946dc | 1767 | .pm = vpif_pm_ops, |
6ffefff5 MK |
1768 | }, |
1769 | .probe = vpif_probe, | |
1770 | .remove = vpif_remove, | |
1771 | }; | |
1772 | ||
fe424b2f | 1773 | module_platform_driver(vpif_driver); |