1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * This is the driver for the STA2x11 Video Input Port.
4 *
5 * Copyright (C) 2012 ST Microelectronics
6 * author: Federico Vaga <federico.vaga@gmail.com>
7 * Copyright (C) 2010 WindRiver Systems, Inc.
8 * authors: Andreas Kies <andreas.kies@windriver.com>
9 * Vlad Lungu <vlad.lungu@windriver.com>
10 */
11
12 #include <linux/types.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/videodev2.h>
17 #include <linux/kmod.h>
18 #include <linux/pci.h>
19 #include <linux/interrupt.h>
20 #include <linux/io.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/gpio.h>
23 #include <linux/i2c.h>
24 #include <linux/delay.h>
25 #include <media/v4l2-common.h>
26 #include <media/v4l2-device.h>
27 #include <media/v4l2-ctrls.h>
28 #include <media/v4l2-ioctl.h>
29 #include <media/v4l2-fh.h>
30 #include <media/v4l2-event.h>
31 #include <media/videobuf2-dma-contig.h>
32
33 #include "sta2x11_vip.h"
34
35 #define DRV_VERSION "1.3"
36
37 #ifndef PCI_DEVICE_ID_STMICRO_VIP
38 #define PCI_DEVICE_ID_STMICRO_VIP 0xCC0D
39 #endif
40
41 #define MAX_FRAMES 4
42
43 /*Register offsets*/
44 #define DVP_CTL 0x00
45 #define DVP_TFO 0x04
46 #define DVP_TFS 0x08
47 #define DVP_BFO 0x0C
48 #define DVP_BFS 0x10
49 #define DVP_VTP 0x14
50 #define DVP_VBP 0x18
51 #define DVP_VMP 0x1C
52 #define DVP_ITM 0x98
53 #define DVP_ITS 0x9C
54 #define DVP_STA 0xA0
55 #define DVP_HLFLN 0xA8
56 #define DVP_RGB 0xC0
57 #define DVP_PKZ 0xF0
58
59 /*Register fields*/
60 #define DVP_CTL_ENA 0x00000001
61 #define DVP_CTL_RST 0x80000000
62 #define DVP_CTL_DIS (~0x00040001)
63
64 #define DVP_IT_VSB 0x00000008
65 #define DVP_IT_VST 0x00000010
66 #define DVP_IT_FIFO 0x00000020
67
68 #define DVP_HLFLN_SD 0x00000001
69
70 #define SAVE_COUNT 8
71 #define AUX_COUNT 3
72 #define IRQ_COUNT 1
73
74
75 struct vip_buffer {
76 struct vb2_v4l2_buffer vb;
77 struct list_head list;
78 dma_addr_t dma;
79 };
to_vip_buffer(struct vb2_v4l2_buffer * vb2)80 static inline struct vip_buffer *to_vip_buffer(struct vb2_v4l2_buffer *vb2)
81 {
82 return container_of(vb2, struct vip_buffer, vb);
83 }
84
85 /**
86 * struct sta2x11_vip - All internal data for one instance of device
87 * @v4l2_dev: device registered in v4l layer
88 * @video_dev: properties of our device
89 * @pdev: PCI device
90 * @adapter: contains I2C adapter information
91 * @register_save_area: All relevant register are saved here during suspend
92 * @decoder: contains information about video DAC
93 * @ctrl_hdl: handler for control framework
94 * @format: pixel format, fixed UYVY
95 * @std: video standard (e.g. PAL/NTSC)
96 * @input: input line for video signal ( 0 or 1 )
97 * @disabled: Device is in power down state
98 * @slock: for excluse access of registers
99 * @vb_vidq: queue maintained by videobuf2 layer
100 * @buffer_list: list of buffer in use
101 * @sequence: sequence number of acquired buffer
102 * @active: current active buffer
103 * @lock: used in videobuf2 callback
104 * @v4l_lock: serialize its video4linux ioctls
105 * @tcount: Number of top frames
106 * @bcount: Number of bottom frames
107 * @overflow: Number of FIFO overflows
108 * @iomem: hardware base address
109 * @config: I2C and gpio config from platform
110 *
111 * All non-local data is accessed via this structure.
112 */
113 struct sta2x11_vip {
114 struct v4l2_device v4l2_dev;
115 struct video_device video_dev;
116 struct pci_dev *pdev;
117 struct i2c_adapter *adapter;
118 unsigned int register_save_area[IRQ_COUNT + SAVE_COUNT + AUX_COUNT];
119 struct v4l2_subdev *decoder;
120 struct v4l2_ctrl_handler ctrl_hdl;
121
122
123 struct v4l2_pix_format format;
124 v4l2_std_id std;
125 unsigned int input;
126 int disabled;
127 spinlock_t slock;
128
129 struct vb2_queue vb_vidq;
130 struct list_head buffer_list;
131 unsigned int sequence;
132 struct vip_buffer *active; /* current active buffer */
133 spinlock_t lock; /* Used in videobuf2 callback */
134 struct mutex v4l_lock;
135
136 /* Interrupt counters */
137 int tcount, bcount;
138 int overflow;
139
140 void __iomem *iomem; /* I/O Memory */
141 struct vip_config *config;
142 };
143
144 static const unsigned int registers_to_save[AUX_COUNT] = {
145 DVP_HLFLN, DVP_RGB, DVP_PKZ
146 };
147
148 static struct v4l2_pix_format formats_50[] = {
149 { /*PAL interlaced */
150 .width = 720,
151 .height = 576,
152 .pixelformat = V4L2_PIX_FMT_UYVY,
153 .field = V4L2_FIELD_INTERLACED,
154 .bytesperline = 720 * 2,
155 .sizeimage = 720 * 2 * 576,
156 .colorspace = V4L2_COLORSPACE_SMPTE170M},
157 { /*PAL top */
158 .width = 720,
159 .height = 288,
160 .pixelformat = V4L2_PIX_FMT_UYVY,
161 .field = V4L2_FIELD_TOP,
162 .bytesperline = 720 * 2,
163 .sizeimage = 720 * 2 * 288,
164 .colorspace = V4L2_COLORSPACE_SMPTE170M},
165 { /*PAL bottom */
166 .width = 720,
167 .height = 288,
168 .pixelformat = V4L2_PIX_FMT_UYVY,
169 .field = V4L2_FIELD_BOTTOM,
170 .bytesperline = 720 * 2,
171 .sizeimage = 720 * 2 * 288,
172 .colorspace = V4L2_COLORSPACE_SMPTE170M},
173
174 };
175
176 static struct v4l2_pix_format formats_60[] = {
177 { /*NTSC interlaced */
178 .width = 720,
179 .height = 480,
180 .pixelformat = V4L2_PIX_FMT_UYVY,
181 .field = V4L2_FIELD_INTERLACED,
182 .bytesperline = 720 * 2,
183 .sizeimage = 720 * 2 * 480,
184 .colorspace = V4L2_COLORSPACE_SMPTE170M},
185 { /*NTSC top */
186 .width = 720,
187 .height = 240,
188 .pixelformat = V4L2_PIX_FMT_UYVY,
189 .field = V4L2_FIELD_TOP,
190 .bytesperline = 720 * 2,
191 .sizeimage = 720 * 2 * 240,
192 .colorspace = V4L2_COLORSPACE_SMPTE170M},
193 { /*NTSC bottom */
194 .width = 720,
195 .height = 240,
196 .pixelformat = V4L2_PIX_FMT_UYVY,
197 .field = V4L2_FIELD_BOTTOM,
198 .bytesperline = 720 * 2,
199 .sizeimage = 720 * 2 * 240,
200 .colorspace = V4L2_COLORSPACE_SMPTE170M},
201 };
202
203 /* Write VIP register */
reg_write(struct sta2x11_vip * vip,unsigned int reg,u32 val)204 static inline void reg_write(struct sta2x11_vip *vip, unsigned int reg, u32 val)
205 {
206 iowrite32((val), (vip->iomem)+(reg));
207 }
208 /* Read VIP register */
reg_read(struct sta2x11_vip * vip,unsigned int reg)209 static inline u32 reg_read(struct sta2x11_vip *vip, unsigned int reg)
210 {
211 return ioread32((vip->iomem)+(reg));
212 }
213 /* Start DMA acquisition */
start_dma(struct sta2x11_vip * vip,struct vip_buffer * vip_buf)214 static void start_dma(struct sta2x11_vip *vip, struct vip_buffer *vip_buf)
215 {
216 unsigned long offset = 0;
217
218 if (vip->format.field == V4L2_FIELD_INTERLACED)
219 offset = vip->format.width * 2;
220
221 spin_lock_irq(&vip->slock);
222 /* Enable acquisition */
223 reg_write(vip, DVP_CTL, reg_read(vip, DVP_CTL) | DVP_CTL_ENA);
224 /* Set Top and Bottom Field memory address */
225 reg_write(vip, DVP_VTP, (u32)vip_buf->dma);
226 reg_write(vip, DVP_VBP, (u32)vip_buf->dma + offset);
227 spin_unlock_irq(&vip->slock);
228 }
229
230 /* Fetch the next buffer to activate */
vip_active_buf_next(struct sta2x11_vip * vip)231 static void vip_active_buf_next(struct sta2x11_vip *vip)
232 {
233 /* Get the next buffer */
234 spin_lock(&vip->lock);
235 if (list_empty(&vip->buffer_list)) {/* No available buffer */
236 spin_unlock(&vip->lock);
237 return;
238 }
239 vip->active = list_first_entry(&vip->buffer_list,
240 struct vip_buffer,
241 list);
242 /* Reset Top and Bottom counter */
243 vip->tcount = 0;
244 vip->bcount = 0;
245 spin_unlock(&vip->lock);
246 if (vb2_is_streaming(&vip->vb_vidq)) { /* streaming is on */
247 start_dma(vip, vip->active); /* start dma capture */
248 }
249 }
250
251
252 /* Videobuf2 Operations */
queue_setup(struct vb2_queue * vq,unsigned int * nbuffers,unsigned int * nplanes,unsigned int sizes[],struct device * alloc_devs[])253 static int queue_setup(struct vb2_queue *vq,
254 unsigned int *nbuffers, unsigned int *nplanes,
255 unsigned int sizes[], struct device *alloc_devs[])
256 {
257 struct sta2x11_vip *vip = vb2_get_drv_priv(vq);
258
259 if (!(*nbuffers) || *nbuffers < MAX_FRAMES)
260 *nbuffers = MAX_FRAMES;
261
262 *nplanes = 1;
263 sizes[0] = vip->format.sizeimage;
264
265 vip->sequence = 0;
266 vip->active = NULL;
267 vip->tcount = 0;
268 vip->bcount = 0;
269
270 return 0;
271 };
buffer_init(struct vb2_buffer * vb)272 static int buffer_init(struct vb2_buffer *vb)
273 {
274 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
275 struct vip_buffer *vip_buf = to_vip_buffer(vbuf);
276
277 vip_buf->dma = vb2_dma_contig_plane_dma_addr(vb, 0);
278 INIT_LIST_HEAD(&vip_buf->list);
279 return 0;
280 }
281
buffer_prepare(struct vb2_buffer * vb)282 static int buffer_prepare(struct vb2_buffer *vb)
283 {
284 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
285 struct sta2x11_vip *vip = vb2_get_drv_priv(vb->vb2_queue);
286 struct vip_buffer *vip_buf = to_vip_buffer(vbuf);
287 unsigned long size;
288
289 size = vip->format.sizeimage;
290 if (vb2_plane_size(vb, 0) < size) {
291 v4l2_err(&vip->v4l2_dev, "buffer too small (%lu < %lu)\n",
292 vb2_plane_size(vb, 0), size);
293 return -EINVAL;
294 }
295
296 vb2_set_plane_payload(&vip_buf->vb.vb2_buf, 0, size);
297
298 return 0;
299 }
buffer_queue(struct vb2_buffer * vb)300 static void buffer_queue(struct vb2_buffer *vb)
301 {
302 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
303 struct sta2x11_vip *vip = vb2_get_drv_priv(vb->vb2_queue);
304 struct vip_buffer *vip_buf = to_vip_buffer(vbuf);
305
306 spin_lock(&vip->lock);
307 list_add_tail(&vip_buf->list, &vip->buffer_list);
308 if (!vip->active) { /* No active buffer, active the first one */
309 vip->active = list_first_entry(&vip->buffer_list,
310 struct vip_buffer,
311 list);
312 if (vb2_is_streaming(&vip->vb_vidq)) /* streaming is on */
313 start_dma(vip, vip_buf); /* start dma capture */
314 }
315 spin_unlock(&vip->lock);
316 }
buffer_finish(struct vb2_buffer * vb)317 static void buffer_finish(struct vb2_buffer *vb)
318 {
319 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
320 struct sta2x11_vip *vip = vb2_get_drv_priv(vb->vb2_queue);
321 struct vip_buffer *vip_buf = to_vip_buffer(vbuf);
322
323 /* Buffer handled, remove it from the list */
324 spin_lock(&vip->lock);
325 list_del_init(&vip_buf->list);
326 spin_unlock(&vip->lock);
327
328 if (vb2_is_streaming(vb->vb2_queue))
329 vip_active_buf_next(vip);
330 }
331
start_streaming(struct vb2_queue * vq,unsigned int count)332 static int start_streaming(struct vb2_queue *vq, unsigned int count)
333 {
334 struct sta2x11_vip *vip = vb2_get_drv_priv(vq);
335
336 spin_lock_irq(&vip->slock);
337 /* Enable interrupt VSYNC Top and Bottom*/
338 reg_write(vip, DVP_ITM, DVP_IT_VSB | DVP_IT_VST);
339 spin_unlock_irq(&vip->slock);
340
341 if (count)
342 start_dma(vip, vip->active);
343
344 return 0;
345 }
346
347 /* abort streaming and wait for last buffer */
stop_streaming(struct vb2_queue * vq)348 static void stop_streaming(struct vb2_queue *vq)
349 {
350 struct sta2x11_vip *vip = vb2_get_drv_priv(vq);
351 struct vip_buffer *vip_buf, *node;
352
353 /* Disable acquisition */
354 reg_write(vip, DVP_CTL, reg_read(vip, DVP_CTL) & ~DVP_CTL_ENA);
355 /* Disable all interrupts */
356 reg_write(vip, DVP_ITM, 0);
357
358 /* Release all active buffers */
359 spin_lock(&vip->lock);
360 list_for_each_entry_safe(vip_buf, node, &vip->buffer_list, list) {
361 vb2_buffer_done(&vip_buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
362 list_del(&vip_buf->list);
363 }
364 spin_unlock(&vip->lock);
365 }
366
367 static const struct vb2_ops vip_video_qops = {
368 .queue_setup = queue_setup,
369 .buf_init = buffer_init,
370 .buf_prepare = buffer_prepare,
371 .buf_finish = buffer_finish,
372 .buf_queue = buffer_queue,
373 .start_streaming = start_streaming,
374 .stop_streaming = stop_streaming,
375 .wait_prepare = vb2_ops_wait_prepare,
376 .wait_finish = vb2_ops_wait_finish,
377 };
378
379
380 /* File Operations */
381 static const struct v4l2_file_operations vip_fops = {
382 .owner = THIS_MODULE,
383 .open = v4l2_fh_open,
384 .release = vb2_fop_release,
385 .unlocked_ioctl = video_ioctl2,
386 .read = vb2_fop_read,
387 .mmap = vb2_fop_mmap,
388 .poll = vb2_fop_poll
389 };
390
391
392 /**
393 * vidioc_querycap - return capabilities of device
394 * @file: descriptor of device
395 * @cap: contains return values
396 * @priv: unused
397 *
398 * the capabilities of the device are returned
399 *
400 * return value: 0, no error.
401 */
vidioc_querycap(struct file * file,void * priv,struct v4l2_capability * cap)402 static int vidioc_querycap(struct file *file, void *priv,
403 struct v4l2_capability *cap)
404 {
405 strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
406 strscpy(cap->card, KBUILD_MODNAME, sizeof(cap->card));
407 return 0;
408 }
409
410 /**
411 * vidioc_s_std - set video standard
412 * @file: descriptor of device
413 * @std: contains standard to be set
414 * @priv: unused
415 *
416 * the video standard is set
417 *
418 * return value: 0, no error.
419 *
420 * -EIO, no input signal detected
421 *
422 * other, returned from video DAC.
423 */
vidioc_s_std(struct file * file,void * priv,v4l2_std_id std)424 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id std)
425 {
426 struct sta2x11_vip *vip = video_drvdata(file);
427
428 /*
429 * This is here for backwards compatibility only.
430 * The use of V4L2_STD_ALL to trigger a querystd is non-standard.
431 */
432 if (std == V4L2_STD_ALL) {
433 v4l2_subdev_call(vip->decoder, video, querystd, &std);
434 if (std == V4L2_STD_UNKNOWN)
435 return -EIO;
436 }
437
438 if (vip->std != std) {
439 vip->std = std;
440 if (V4L2_STD_525_60 & std)
441 vip->format = formats_60[0];
442 else
443 vip->format = formats_50[0];
444 }
445
446 return v4l2_subdev_call(vip->decoder, video, s_std, std);
447 }
448
449 /**
450 * vidioc_g_std - get video standard
451 * @file: descriptor of device
452 * @priv: unused
453 * @std: contains return values
454 *
455 * the current video standard is returned
456 *
457 * return value: 0, no error.
458 */
vidioc_g_std(struct file * file,void * priv,v4l2_std_id * std)459 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *std)
460 {
461 struct sta2x11_vip *vip = video_drvdata(file);
462
463 *std = vip->std;
464 return 0;
465 }
466
467 /**
468 * vidioc_querystd - get possible video standards
469 * @file: descriptor of device
470 * @priv: unused
471 * @std: contains return values
472 *
473 * all possible video standards are returned
474 *
475 * return value: delivered by video DAC routine.
476 */
vidioc_querystd(struct file * file,void * priv,v4l2_std_id * std)477 static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *std)
478 {
479 struct sta2x11_vip *vip = video_drvdata(file);
480
481 return v4l2_subdev_call(vip->decoder, video, querystd, std);
482 }
483
vidioc_enum_input(struct file * file,void * priv,struct v4l2_input * inp)484 static int vidioc_enum_input(struct file *file, void *priv,
485 struct v4l2_input *inp)
486 {
487 if (inp->index > 1)
488 return -EINVAL;
489
490 inp->type = V4L2_INPUT_TYPE_CAMERA;
491 inp->std = V4L2_STD_ALL;
492 sprintf(inp->name, "Camera %u", inp->index);
493
494 return 0;
495 }
496
497 /**
498 * vidioc_s_input - set input line
499 * @file: descriptor of device
500 * @priv: unused
501 * @i: new input line number
502 *
503 * the current active input line is set
504 *
505 * return value: 0, no error.
506 *
507 * -EINVAL, line number out of range
508 */
vidioc_s_input(struct file * file,void * priv,unsigned int i)509 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
510 {
511 struct sta2x11_vip *vip = video_drvdata(file);
512 int ret;
513
514 if (i > 1)
515 return -EINVAL;
516 ret = v4l2_subdev_call(vip->decoder, video, s_routing, i, 0, 0);
517
518 if (!ret)
519 vip->input = i;
520
521 return 0;
522 }
523
524 /**
525 * vidioc_g_input - return input line
526 * @file: descriptor of device
527 * @priv: unused
528 * @i: returned input line number
529 *
530 * the current active input line is returned
531 *
532 * return value: always 0.
533 */
vidioc_g_input(struct file * file,void * priv,unsigned int * i)534 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
535 {
536 struct sta2x11_vip *vip = video_drvdata(file);
537
538 *i = vip->input;
539 return 0;
540 }
541
542 /**
543 * vidioc_enum_fmt_vid_cap - return video capture format
544 * @file: descriptor of device
545 * @priv: unused
546 * @f: returned format information
547 *
548 * returns name and format of video capture
549 * Only UYVY is supported by hardware.
550 *
551 * return value: always 0.
552 */
vidioc_enum_fmt_vid_cap(struct file * file,void * priv,struct v4l2_fmtdesc * f)553 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
554 struct v4l2_fmtdesc *f)
555 {
556
557 if (f->index != 0)
558 return -EINVAL;
559
560 f->pixelformat = V4L2_PIX_FMT_UYVY;
561 return 0;
562 }
563
564 /**
565 * vidioc_try_fmt_vid_cap - set video capture format
566 * @file: descriptor of device
567 * @priv: unused
568 * @f: new format
569 *
570 * new video format is set which includes width and
571 * field type. width is fixed to 720, no scaling.
572 * Only UYVY is supported by this hardware.
573 * the minimum height is 200, the maximum is 576 (PAL)
574 *
575 * return value: 0, no error
576 *
577 * -EINVAL, pixel or field format not supported
578 *
579 */
vidioc_try_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)580 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
581 struct v4l2_format *f)
582 {
583 struct sta2x11_vip *vip = video_drvdata(file);
584 int interlace_lim;
585
586 if (V4L2_PIX_FMT_UYVY != f->fmt.pix.pixelformat) {
587 v4l2_warn(&vip->v4l2_dev, "Invalid format, only UYVY supported\n");
588 return -EINVAL;
589 }
590
591 if (V4L2_STD_525_60 & vip->std)
592 interlace_lim = 240;
593 else
594 interlace_lim = 288;
595
596 switch (f->fmt.pix.field) {
597 default:
598 case V4L2_FIELD_ANY:
599 if (interlace_lim < f->fmt.pix.height)
600 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
601 else
602 f->fmt.pix.field = V4L2_FIELD_BOTTOM;
603 break;
604 case V4L2_FIELD_TOP:
605 case V4L2_FIELD_BOTTOM:
606 if (interlace_lim < f->fmt.pix.height)
607 f->fmt.pix.height = interlace_lim;
608 break;
609 case V4L2_FIELD_INTERLACED:
610 break;
611 }
612
613 /* It is the only supported format */
614 f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
615 f->fmt.pix.height &= ~1;
616 if (2 * interlace_lim < f->fmt.pix.height)
617 f->fmt.pix.height = 2 * interlace_lim;
618 if (200 > f->fmt.pix.height)
619 f->fmt.pix.height = 200;
620 f->fmt.pix.width = 720;
621 f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
622 f->fmt.pix.sizeimage = f->fmt.pix.width * 2 * f->fmt.pix.height;
623 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
624 return 0;
625 }
626
627 /**
628 * vidioc_s_fmt_vid_cap - set current video format parameters
629 * @file: descriptor of device
630 * @priv: unused
631 * @f: returned format information
632 *
633 * set new capture format
634 * return value: 0, no error
635 *
636 * other, delivered by video DAC routine.
637 */
vidioc_s_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)638 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
639 struct v4l2_format *f)
640 {
641 struct sta2x11_vip *vip = video_drvdata(file);
642 unsigned int t_stop, b_stop, pitch;
643 int ret;
644
645 ret = vidioc_try_fmt_vid_cap(file, priv, f);
646 if (ret)
647 return ret;
648
649 if (vb2_is_busy(&vip->vb_vidq)) {
650 /* Can't change format during acquisition */
651 v4l2_err(&vip->v4l2_dev, "device busy\n");
652 return -EBUSY;
653 }
654 vip->format = f->fmt.pix;
655 switch (vip->format.field) {
656 case V4L2_FIELD_INTERLACED:
657 t_stop = ((vip->format.height / 2 - 1) << 16) |
658 (2 * vip->format.width - 1);
659 b_stop = t_stop;
660 pitch = 4 * vip->format.width;
661 break;
662 case V4L2_FIELD_TOP:
663 t_stop = ((vip->format.height - 1) << 16) |
664 (2 * vip->format.width - 1);
665 b_stop = (0 << 16) | (2 * vip->format.width - 1);
666 pitch = 2 * vip->format.width;
667 break;
668 case V4L2_FIELD_BOTTOM:
669 t_stop = (0 << 16) | (2 * vip->format.width - 1);
670 b_stop = (vip->format.height << 16) |
671 (2 * vip->format.width - 1);
672 pitch = 2 * vip->format.width;
673 break;
674 default:
675 v4l2_err(&vip->v4l2_dev, "unknown field format\n");
676 return -EINVAL;
677 }
678
679 spin_lock_irq(&vip->slock);
680 /* Y-X Top Field Offset */
681 reg_write(vip, DVP_TFO, 0);
682 /* Y-X Bottom Field Offset */
683 reg_write(vip, DVP_BFO, 0);
684 /* Y-X Top Field Stop*/
685 reg_write(vip, DVP_TFS, t_stop);
686 /* Y-X Bottom Field Stop */
687 reg_write(vip, DVP_BFS, b_stop);
688 /* Video Memory Pitch */
689 reg_write(vip, DVP_VMP, pitch);
690 spin_unlock_irq(&vip->slock);
691
692 return 0;
693 }
694
695 /**
696 * vidioc_g_fmt_vid_cap - get current video format parameters
697 * @file: descriptor of device
698 * @priv: unused
699 * @f: contains format information
700 *
701 * returns current video format parameters
702 *
703 * return value: 0, always successful
704 */
vidioc_g_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)705 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
706 struct v4l2_format *f)
707 {
708 struct sta2x11_vip *vip = video_drvdata(file);
709
710 f->fmt.pix = vip->format;
711
712 return 0;
713 }
714
715 static const struct v4l2_ioctl_ops vip_ioctl_ops = {
716 .vidioc_querycap = vidioc_querycap,
717 /* FMT handling */
718 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
719 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
720 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
721 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
722 /* Buffer handlers */
723 .vidioc_create_bufs = vb2_ioctl_create_bufs,
724 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
725 .vidioc_reqbufs = vb2_ioctl_reqbufs,
726 .vidioc_querybuf = vb2_ioctl_querybuf,
727 .vidioc_qbuf = vb2_ioctl_qbuf,
728 .vidioc_dqbuf = vb2_ioctl_dqbuf,
729 /* Stream on/off */
730 .vidioc_streamon = vb2_ioctl_streamon,
731 .vidioc_streamoff = vb2_ioctl_streamoff,
732 /* Standard handling */
733 .vidioc_g_std = vidioc_g_std,
734 .vidioc_s_std = vidioc_s_std,
735 .vidioc_querystd = vidioc_querystd,
736 /* Input handling */
737 .vidioc_enum_input = vidioc_enum_input,
738 .vidioc_g_input = vidioc_g_input,
739 .vidioc_s_input = vidioc_s_input,
740 /* Log status ioctl */
741 .vidioc_log_status = v4l2_ctrl_log_status,
742 /* Event handling */
743 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
744 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
745 };
746
747 static const struct video_device video_dev_template = {
748 .name = KBUILD_MODNAME,
749 .release = video_device_release_empty,
750 .fops = &vip_fops,
751 .ioctl_ops = &vip_ioctl_ops,
752 .tvnorms = V4L2_STD_ALL,
753 .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
754 V4L2_CAP_STREAMING,
755 };
756
757 /**
758 * vip_irq - interrupt routine
759 * @irq: Number of interrupt ( not used, correct number is assumed )
760 * @data: local data structure containing all information
761 *
762 * check for both frame interrupts set ( top and bottom ).
763 * check FIFO overflow, but limit number of log messages after open.
764 * signal a complete buffer if done
765 *
766 * return value: IRQ_NONE, interrupt was not generated by VIP
767 *
768 * IRQ_HANDLED, interrupt done.
769 */
vip_irq(int irq,void * data)770 static irqreturn_t vip_irq(int irq, void *data)
771 {
772 struct sta2x11_vip *vip = data;
773 unsigned int status;
774
775 status = reg_read(vip, DVP_ITS);
776
777 if (!status) /* No interrupt to handle */
778 return IRQ_NONE;
779
780 if (status & DVP_IT_FIFO)
781 if (vip->overflow++ > 5)
782 pr_info("VIP: fifo overflow\n");
783
784 if ((status & DVP_IT_VST) && (status & DVP_IT_VSB)) {
785 /* this is bad, we are too slow, hope the condition is gone
786 * on the next frame */
787 return IRQ_HANDLED;
788 }
789
790 if (status & DVP_IT_VST)
791 if ((++vip->tcount) < 2)
792 return IRQ_HANDLED;
793 if (status & DVP_IT_VSB) {
794 vip->bcount++;
795 return IRQ_HANDLED;
796 }
797
798 if (vip->active) { /* Acquisition is over on this buffer */
799 /* Disable acquisition */
800 reg_write(vip, DVP_CTL, reg_read(vip, DVP_CTL) & ~DVP_CTL_ENA);
801 /* Remove the active buffer from the list */
802 vip->active->vb.vb2_buf.timestamp = ktime_get_ns();
803 vip->active->vb.sequence = vip->sequence++;
804 vb2_buffer_done(&vip->active->vb.vb2_buf, VB2_BUF_STATE_DONE);
805 }
806
807 return IRQ_HANDLED;
808 }
809
sta2x11_vip_init_register(struct sta2x11_vip * vip)810 static void sta2x11_vip_init_register(struct sta2x11_vip *vip)
811 {
812 /* Register initialization */
813 spin_lock_irq(&vip->slock);
814 /* Clean interrupt */
815 reg_read(vip, DVP_ITS);
816 /* Enable Half Line per vertical */
817 reg_write(vip, DVP_HLFLN, DVP_HLFLN_SD);
818 /* Reset VIP control */
819 reg_write(vip, DVP_CTL, DVP_CTL_RST);
820 /* Clear VIP control */
821 reg_write(vip, DVP_CTL, 0);
822 spin_unlock_irq(&vip->slock);
823 }
sta2x11_vip_clear_register(struct sta2x11_vip * vip)824 static void sta2x11_vip_clear_register(struct sta2x11_vip *vip)
825 {
826 spin_lock_irq(&vip->slock);
827 /* Disable interrupt */
828 reg_write(vip, DVP_ITM, 0);
829 /* Reset VIP Control */
830 reg_write(vip, DVP_CTL, DVP_CTL_RST);
831 /* Clear VIP Control */
832 reg_write(vip, DVP_CTL, 0);
833 /* Clean VIP Interrupt */
834 reg_read(vip, DVP_ITS);
835 spin_unlock_irq(&vip->slock);
836 }
sta2x11_vip_init_buffer(struct sta2x11_vip * vip)837 static int sta2x11_vip_init_buffer(struct sta2x11_vip *vip)
838 {
839 int err;
840
841 err = dma_set_coherent_mask(&vip->pdev->dev, DMA_BIT_MASK(29));
842 if (err) {
843 v4l2_err(&vip->v4l2_dev, "Cannot configure coherent mask");
844 return err;
845 }
846 memset(&vip->vb_vidq, 0, sizeof(struct vb2_queue));
847 vip->vb_vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
848 vip->vb_vidq.io_modes = VB2_MMAP | VB2_READ;
849 vip->vb_vidq.drv_priv = vip;
850 vip->vb_vidq.buf_struct_size = sizeof(struct vip_buffer);
851 vip->vb_vidq.ops = &vip_video_qops;
852 vip->vb_vidq.mem_ops = &vb2_dma_contig_memops;
853 vip->vb_vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
854 vip->vb_vidq.dev = &vip->pdev->dev;
855 vip->vb_vidq.lock = &vip->v4l_lock;
856 err = vb2_queue_init(&vip->vb_vidq);
857 if (err)
858 return err;
859 INIT_LIST_HEAD(&vip->buffer_list);
860 spin_lock_init(&vip->lock);
861 return 0;
862 }
863
sta2x11_vip_init_controls(struct sta2x11_vip * vip)864 static int sta2x11_vip_init_controls(struct sta2x11_vip *vip)
865 {
866 /*
867 * Inititialize an empty control so VIP can inerithing controls
868 * from ADV7180
869 */
870 v4l2_ctrl_handler_init(&vip->ctrl_hdl, 0);
871
872 vip->v4l2_dev.ctrl_handler = &vip->ctrl_hdl;
873 if (vip->ctrl_hdl.error) {
874 int err = vip->ctrl_hdl.error;
875
876 v4l2_ctrl_handler_free(&vip->ctrl_hdl);
877 return err;
878 }
879
880 return 0;
881 }
882
883 /**
884 * vip_gpio_reserve - reserve gpio pin
885 * @dev: device
886 * @pin: GPIO pin number
887 * @dir: direction, input or output
888 * @name: GPIO pin name
889 *
890 */
vip_gpio_reserve(struct device * dev,int pin,int dir,const char * name)891 static int vip_gpio_reserve(struct device *dev, int pin, int dir,
892 const char *name)
893 {
894 struct gpio_desc *desc = gpio_to_desc(pin);
895 int ret = -ENODEV;
896
897 if (!gpio_is_valid(pin))
898 return ret;
899
900 ret = gpio_request(pin, name);
901 if (ret) {
902 dev_err(dev, "Failed to allocate pin %d (%s)\n", pin, name);
903 return ret;
904 }
905
906 ret = gpiod_direction_output(desc, dir);
907 if (ret) {
908 dev_err(dev, "Failed to set direction for pin %d (%s)\n",
909 pin, name);
910 gpio_free(pin);
911 return ret;
912 }
913
914 ret = gpiod_export(desc, false);
915 if (ret) {
916 dev_err(dev, "Failed to export pin %d (%s)\n", pin, name);
917 gpio_free(pin);
918 return ret;
919 }
920
921 return 0;
922 }
923
924 /**
925 * vip_gpio_release - release gpio pin
926 * @dev: device
927 * @pin: GPIO pin number
928 * @name: GPIO pin name
929 *
930 */
vip_gpio_release(struct device * dev,int pin,const char * name)931 static void vip_gpio_release(struct device *dev, int pin, const char *name)
932 {
933 if (gpio_is_valid(pin)) {
934 struct gpio_desc *desc = gpio_to_desc(pin);
935
936 dev_dbg(dev, "releasing pin %d (%s)\n", pin, name);
937 gpiod_unexport(desc);
938 gpio_free(pin);
939 }
940 }
941
942 /**
943 * sta2x11_vip_init_one - init one instance of video device
944 * @pdev: PCI device
945 * @ent: (not used)
946 *
947 * allocate reset pins for DAC.
948 * Reset video DAC, this is done via reset line.
949 * allocate memory for managing device
950 * request interrupt
951 * map IO region
952 * register device
953 * find and initialize video DAC
954 *
955 * return value: 0, no error
956 *
957 * -ENOMEM, no memory
958 *
959 * -ENODEV, device could not be detected or registered
960 */
sta2x11_vip_init_one(struct pci_dev * pdev,const struct pci_device_id * ent)961 static int sta2x11_vip_init_one(struct pci_dev *pdev,
962 const struct pci_device_id *ent)
963 {
964 int ret;
965 struct sta2x11_vip *vip;
966 struct vip_config *config;
967
968 /* Check if hardware support 26-bit DMA */
969 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(26))) {
970 dev_err(&pdev->dev, "26-bit DMA addressing not available\n");
971 return -EINVAL;
972 }
973 /* Enable PCI */
974 ret = pci_enable_device(pdev);
975 if (ret)
976 return ret;
977
978 /* Get VIP platform data */
979 config = dev_get_platdata(&pdev->dev);
980 if (!config) {
981 dev_info(&pdev->dev, "VIP slot disabled\n");
982 ret = -EINVAL;
983 goto disable;
984 }
985
986 /* Power configuration */
987 ret = vip_gpio_reserve(&pdev->dev, config->pwr_pin, 0,
988 config->pwr_name);
989 if (ret)
990 goto disable;
991
992 ret = vip_gpio_reserve(&pdev->dev, config->reset_pin, 0,
993 config->reset_name);
994 if (ret) {
995 vip_gpio_release(&pdev->dev, config->pwr_pin,
996 config->pwr_name);
997 goto disable;
998 }
999
1000 if (gpio_is_valid(config->pwr_pin)) {
1001 /* Datasheet says 5ms between PWR and RST */
1002 usleep_range(5000, 25000);
1003 gpio_direction_output(config->pwr_pin, 1);
1004 }
1005
1006 if (gpio_is_valid(config->reset_pin)) {
1007 /* Datasheet says 5ms between PWR and RST */
1008 usleep_range(5000, 25000);
1009 gpio_direction_output(config->reset_pin, 1);
1010 }
1011 usleep_range(5000, 25000);
1012
1013 /* Allocate a new VIP instance */
1014 vip = kzalloc(sizeof(struct sta2x11_vip), GFP_KERNEL);
1015 if (!vip) {
1016 ret = -ENOMEM;
1017 goto release_gpios;
1018 }
1019 vip->pdev = pdev;
1020 vip->std = V4L2_STD_PAL;
1021 vip->format = formats_50[0];
1022 vip->config = config;
1023 mutex_init(&vip->v4l_lock);
1024
1025 ret = sta2x11_vip_init_controls(vip);
1026 if (ret)
1027 goto free_mem;
1028 ret = v4l2_device_register(&pdev->dev, &vip->v4l2_dev);
1029 if (ret)
1030 goto free_mem;
1031
1032 dev_dbg(&pdev->dev, "BAR #0 at 0x%lx 0x%lx irq %d\n",
1033 (unsigned long)pci_resource_start(pdev, 0),
1034 (unsigned long)pci_resource_len(pdev, 0), pdev->irq);
1035
1036 pci_set_master(pdev);
1037
1038 ret = pci_request_regions(pdev, KBUILD_MODNAME);
1039 if (ret)
1040 goto unreg;
1041
1042 vip->iomem = pci_iomap(pdev, 0, 0x100);
1043 if (!vip->iomem) {
1044 ret = -ENOMEM;
1045 goto release;
1046 }
1047
1048 pci_enable_msi(pdev);
1049
1050 /* Initialize buffer */
1051 ret = sta2x11_vip_init_buffer(vip);
1052 if (ret)
1053 goto unmap;
1054
1055 spin_lock_init(&vip->slock);
1056
1057 ret = request_irq(pdev->irq, vip_irq, IRQF_SHARED, KBUILD_MODNAME, vip);
1058 if (ret) {
1059 dev_err(&pdev->dev, "request_irq failed\n");
1060 ret = -ENODEV;
1061 goto release_buf;
1062 }
1063
1064 /* Initialize and register video device */
1065 vip->video_dev = video_dev_template;
1066 vip->video_dev.v4l2_dev = &vip->v4l2_dev;
1067 vip->video_dev.queue = &vip->vb_vidq;
1068 vip->video_dev.lock = &vip->v4l_lock;
1069 video_set_drvdata(&vip->video_dev, vip);
1070
1071 ret = video_register_device(&vip->video_dev, VFL_TYPE_VIDEO, -1);
1072 if (ret)
1073 goto vrelease;
1074
1075 /* Get ADV7180 subdevice */
1076 vip->adapter = i2c_get_adapter(vip->config->i2c_id);
1077 if (!vip->adapter) {
1078 ret = -ENODEV;
1079 dev_err(&pdev->dev, "no I2C adapter found\n");
1080 goto vunreg;
1081 }
1082
1083 vip->decoder = v4l2_i2c_new_subdev(&vip->v4l2_dev, vip->adapter,
1084 "adv7180", vip->config->i2c_addr,
1085 NULL);
1086 if (!vip->decoder) {
1087 ret = -ENODEV;
1088 dev_err(&pdev->dev, "no decoder found\n");
1089 goto vunreg;
1090 }
1091
1092 i2c_put_adapter(vip->adapter);
1093 v4l2_subdev_call(vip->decoder, core, init, 0);
1094
1095 sta2x11_vip_init_register(vip);
1096
1097 dev_info(&pdev->dev, "STA2X11 Video Input Port (VIP) loaded\n");
1098 return 0;
1099
1100 vunreg:
1101 video_set_drvdata(&vip->video_dev, NULL);
1102 vrelease:
1103 vb2_video_unregister_device(&vip->video_dev);
1104 free_irq(pdev->irq, vip);
1105 release_buf:
1106 pci_disable_msi(pdev);
1107 unmap:
1108 pci_iounmap(pdev, vip->iomem);
1109 release:
1110 pci_release_regions(pdev);
1111 unreg:
1112 v4l2_device_unregister(&vip->v4l2_dev);
1113 free_mem:
1114 kfree(vip);
1115 release_gpios:
1116 vip_gpio_release(&pdev->dev, config->reset_pin, config->reset_name);
1117 vip_gpio_release(&pdev->dev, config->pwr_pin, config->pwr_name);
1118 disable:
1119 /*
1120 * do not call pci_disable_device on sta2x11 because it break all
1121 * other Bus masters on this EP
1122 */
1123 return ret;
1124 }
1125
1126 /**
1127 * sta2x11_vip_remove_one - release device
1128 * @pdev: PCI device
1129 *
1130 * Undo everything done in .._init_one
1131 *
1132 * unregister video device
1133 * free interrupt
1134 * unmap ioadresses
1135 * free memory
1136 * free GPIO pins
1137 */
sta2x11_vip_remove_one(struct pci_dev * pdev)1138 static void sta2x11_vip_remove_one(struct pci_dev *pdev)
1139 {
1140 struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
1141 struct sta2x11_vip *vip =
1142 container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
1143
1144 sta2x11_vip_clear_register(vip);
1145
1146 video_set_drvdata(&vip->video_dev, NULL);
1147 vb2_video_unregister_device(&vip->video_dev);
1148 free_irq(pdev->irq, vip);
1149 pci_disable_msi(pdev);
1150 pci_iounmap(pdev, vip->iomem);
1151 pci_release_regions(pdev);
1152
1153 v4l2_device_unregister(&vip->v4l2_dev);
1154
1155 vip_gpio_release(&pdev->dev, vip->config->pwr_pin,
1156 vip->config->pwr_name);
1157 vip_gpio_release(&pdev->dev, vip->config->reset_pin,
1158 vip->config->reset_name);
1159
1160 kfree(vip);
1161 /*
1162 * do not call pci_disable_device on sta2x11 because it break all
1163 * other Bus masters on this EP
1164 */
1165 }
1166
1167 /**
1168 * sta2x11_vip_suspend - set device into power save mode
1169 * @dev_d: PCI device
1170 *
1171 * all relevant registers are saved and an attempt to set a new state is made.
1172 *
1173 * return value: 0 always indicate success,
1174 * even if device could not be disabled. (workaround for hardware problem)
1175 */
sta2x11_vip_suspend(struct device * dev_d)1176 static int __maybe_unused sta2x11_vip_suspend(struct device *dev_d)
1177 {
1178 struct v4l2_device *v4l2_dev = dev_get_drvdata(dev_d);
1179 struct sta2x11_vip *vip =
1180 container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
1181 unsigned long flags;
1182 int i;
1183
1184 spin_lock_irqsave(&vip->slock, flags);
1185 vip->register_save_area[0] = reg_read(vip, DVP_CTL);
1186 reg_write(vip, DVP_CTL, vip->register_save_area[0] & DVP_CTL_DIS);
1187 vip->register_save_area[SAVE_COUNT] = reg_read(vip, DVP_ITM);
1188 reg_write(vip, DVP_ITM, 0);
1189 for (i = 1; i < SAVE_COUNT; i++)
1190 vip->register_save_area[i] = reg_read(vip, 4 * i);
1191 for (i = 0; i < AUX_COUNT; i++)
1192 vip->register_save_area[SAVE_COUNT + IRQ_COUNT + i] =
1193 reg_read(vip, registers_to_save[i]);
1194 spin_unlock_irqrestore(&vip->slock, flags);
1195
1196 vip->disabled = 1;
1197
1198 pr_info("VIP: suspend\n");
1199 return 0;
1200 }
1201
1202 /**
1203 * sta2x11_vip_resume - resume device operation
1204 * @dev_d : PCI device
1205 *
1206 * return value: 0, no error.
1207 *
1208 * other, could not set device to power on state.
1209 */
sta2x11_vip_resume(struct device * dev_d)1210 static int __maybe_unused sta2x11_vip_resume(struct device *dev_d)
1211 {
1212 struct v4l2_device *v4l2_dev = dev_get_drvdata(dev_d);
1213 struct sta2x11_vip *vip =
1214 container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
1215 unsigned long flags;
1216 int i;
1217
1218 pr_info("VIP: resume\n");
1219
1220 vip->disabled = 0;
1221
1222 spin_lock_irqsave(&vip->slock, flags);
1223 for (i = 1; i < SAVE_COUNT; i++)
1224 reg_write(vip, 4 * i, vip->register_save_area[i]);
1225 for (i = 0; i < AUX_COUNT; i++)
1226 reg_write(vip, registers_to_save[i],
1227 vip->register_save_area[SAVE_COUNT + IRQ_COUNT + i]);
1228 reg_write(vip, DVP_CTL, vip->register_save_area[0]);
1229 reg_write(vip, DVP_ITM, vip->register_save_area[SAVE_COUNT]);
1230 spin_unlock_irqrestore(&vip->slock, flags);
1231 return 0;
1232 }
1233
1234 static const struct pci_device_id sta2x11_vip_pci_tbl[] = {
1235 {PCI_DEVICE(PCI_VENDOR_ID_STMICRO, PCI_DEVICE_ID_STMICRO_VIP)},
1236 {0,}
1237 };
1238
1239 static SIMPLE_DEV_PM_OPS(sta2x11_vip_pm_ops,
1240 sta2x11_vip_suspend,
1241 sta2x11_vip_resume);
1242
1243 static struct pci_driver sta2x11_vip_driver = {
1244 .name = KBUILD_MODNAME,
1245 .probe = sta2x11_vip_init_one,
1246 .remove = sta2x11_vip_remove_one,
1247 .id_table = sta2x11_vip_pci_tbl,
1248 .driver.pm = &sta2x11_vip_pm_ops,
1249 };
1250
sta2x11_vip_init_module(void)1251 static int __init sta2x11_vip_init_module(void)
1252 {
1253 return pci_register_driver(&sta2x11_vip_driver);
1254 }
1255
sta2x11_vip_exit_module(void)1256 static void __exit sta2x11_vip_exit_module(void)
1257 {
1258 pci_unregister_driver(&sta2x11_vip_driver);
1259 }
1260
1261 #ifdef MODULE
1262 module_init(sta2x11_vip_init_module);
1263 module_exit(sta2x11_vip_exit_module);
1264 #else
1265 late_initcall_sync(sta2x11_vip_init_module);
1266 #endif
1267
1268 MODULE_DESCRIPTION("STA2X11 Video Input Port driver");
1269 MODULE_AUTHOR("Wind River");
1270 MODULE_LICENSE("GPL v2");
1271 MODULE_VERSION(DRV_VERSION);
1272 MODULE_DEVICE_TABLE(pci, sta2x11_vip_pci_tbl);
1273