blob: 3d349dfb23ffdb6db8cd509300e7f42eaca72154 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * device driver for Conexant 2388x based TV cards
4 * video4linux video interface
5 *
6 * (c) 2003-04 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
7 *
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03008 * (c) 2005-2006 Mauro Carvalho Chehab <mchehab@infradead.org>
9 * - Multituner support
10 * - video_ioctl2 conversion
11 * - PAL/M fixes
12 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -020028#include "cx88.h"
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/init.h>
31#include <linux/list.h>
32#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/kmod.h>
34#include <linux/kernel.h>
35#include <linux/slab.h>
36#include <linux/interrupt.h>
Andrew Mortonc24228d2007-05-06 14:51:55 -070037#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/delay.h>
39#include <linux/kthread.h>
40#include <asm/div64.h>
41
Michael Krufky5e453dc2006-01-09 15:32:31 -020042#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030043#include <media/v4l2-ioctl.h>
Hans Verkuil1a3c60a2012-05-11 11:25:03 -030044#include <media/v4l2-event.h>
Mauro Carvalho Chehabb5dcee22015-11-10 12:01:44 -020045#include <media/i2c/wm8775.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
48MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
49MODULE_LICENSE("GPL");
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030050MODULE_VERSION(CX88_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/* ------------------------------------------------------------------ */
53
54static unsigned int video_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
55static unsigned int vbi_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
56static unsigned int radio_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
57
58module_param_array(video_nr, int, NULL, 0444);
59module_param_array(vbi_nr, int, NULL, 0444);
60module_param_array(radio_nr, int, NULL, 0444);
61
62MODULE_PARM_DESC(video_nr,"video device numbers");
63MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
64MODULE_PARM_DESC(radio_nr,"radio device numbers");
65
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030066static unsigned int video_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067module_param(video_debug,int,0644);
68MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
69
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030070static unsigned int irq_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071module_param(irq_debug,int,0644);
72MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
73
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -020074#define dprintk(level, fmt, arg...) do { \
75 if (video_debug >= level) \
76 printk(KERN_DEBUG pr_fmt("%s: video:" fmt), \
77 __func__, ##arg); \
78} while (0)
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/* ------------------------------------------------------------------- */
82/* static data */
83
lawrence rust2e4e98e2010-08-25 09:50:20 -030084static const struct cx8800_fmt formats[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 {
86 .name = "8 bpp, gray",
87 .fourcc = V4L2_PIX_FMT_GREY,
88 .cxformat = ColorFormatY8,
89 .depth = 8,
90 .flags = FORMAT_FLAGS_PACKED,
91 },{
92 .name = "15 bpp RGB, le",
93 .fourcc = V4L2_PIX_FMT_RGB555,
94 .cxformat = ColorFormatRGB15,
95 .depth = 16,
96 .flags = FORMAT_FLAGS_PACKED,
97 },{
98 .name = "15 bpp RGB, be",
99 .fourcc = V4L2_PIX_FMT_RGB555X,
100 .cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
101 .depth = 16,
102 .flags = FORMAT_FLAGS_PACKED,
103 },{
104 .name = "16 bpp RGB, le",
105 .fourcc = V4L2_PIX_FMT_RGB565,
106 .cxformat = ColorFormatRGB16,
107 .depth = 16,
108 .flags = FORMAT_FLAGS_PACKED,
109 },{
110 .name = "16 bpp RGB, be",
111 .fourcc = V4L2_PIX_FMT_RGB565X,
112 .cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
113 .depth = 16,
114 .flags = FORMAT_FLAGS_PACKED,
115 },{
116 .name = "24 bpp RGB, le",
117 .fourcc = V4L2_PIX_FMT_BGR24,
118 .cxformat = ColorFormatRGB24,
119 .depth = 24,
120 .flags = FORMAT_FLAGS_PACKED,
121 },{
122 .name = "32 bpp RGB, le",
123 .fourcc = V4L2_PIX_FMT_BGR32,
124 .cxformat = ColorFormatRGB32,
125 .depth = 32,
126 .flags = FORMAT_FLAGS_PACKED,
127 },{
128 .name = "32 bpp RGB, be",
129 .fourcc = V4L2_PIX_FMT_RGB32,
130 .cxformat = ColorFormatRGB32 | ColorFormatBSWAP | ColorFormatWSWAP,
131 .depth = 32,
132 .flags = FORMAT_FLAGS_PACKED,
133 },{
134 .name = "4:2:2, packed, YUYV",
135 .fourcc = V4L2_PIX_FMT_YUYV,
136 .cxformat = ColorFormatYUY2,
137 .depth = 16,
138 .flags = FORMAT_FLAGS_PACKED,
139 },{
140 .name = "4:2:2, packed, UYVY",
141 .fourcc = V4L2_PIX_FMT_UYVY,
142 .cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
143 .depth = 16,
144 .flags = FORMAT_FLAGS_PACKED,
145 },
146};
147
lawrence rust2e4e98e2010-08-25 09:50:20 -0300148static const struct cx8800_fmt* format_by_fourcc(unsigned int fourcc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 unsigned int i;
151
152 for (i = 0; i < ARRAY_SIZE(formats); i++)
153 if (formats[i].fourcc == fourcc)
154 return formats+i;
155 return NULL;
156}
157
158/* ------------------------------------------------------------------- */
159
Hans Verkuilbac63982012-06-10 07:39:52 -0300160struct cx88_ctrl {
161 /* control information */
162 u32 id;
163 s32 minimum;
164 s32 maximum;
165 u32 step;
166 s32 default_value;
167
168 /* control register information */
169 u32 off;
170 u32 reg;
171 u32 sreg;
172 u32 mask;
173 u32 shift;
174};
175
Hans Verkuil8c7cb122012-05-11 09:07:45 -0300176static const struct cx88_ctrl cx8800_vid_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 /* --- video --- */
178 {
Hans Verkuilbac63982012-06-10 07:39:52 -0300179 .id = V4L2_CID_BRIGHTNESS,
180 .minimum = 0x00,
181 .maximum = 0xff,
182 .step = 1,
183 .default_value = 0x7f,
184 .off = 128,
185 .reg = MO_CONTR_BRIGHT,
186 .mask = 0x00ff,
187 .shift = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 },{
Hans Verkuilbac63982012-06-10 07:39:52 -0300189 .id = V4L2_CID_CONTRAST,
190 .minimum = 0,
191 .maximum = 0xff,
192 .step = 1,
193 .default_value = 0x3f,
194 .off = 0,
195 .reg = MO_CONTR_BRIGHT,
196 .mask = 0xff00,
197 .shift = 8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 },{
Hans Verkuilbac63982012-06-10 07:39:52 -0300199 .id = V4L2_CID_HUE,
200 .minimum = 0,
201 .maximum = 0xff,
202 .step = 1,
203 .default_value = 0x7f,
204 .off = 128,
205 .reg = MO_HUE,
206 .mask = 0x00ff,
207 .shift = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 },{
209 /* strictly, this only describes only U saturation.
210 * V saturation is handled specially through code.
211 */
Hans Verkuilbac63982012-06-10 07:39:52 -0300212 .id = V4L2_CID_SATURATION,
213 .minimum = 0,
214 .maximum = 0xff,
215 .step = 1,
216 .default_value = 0x7f,
217 .off = 0,
218 .reg = MO_UV_SATURATION,
219 .mask = 0x00ff,
220 .shift = 0,
istvan_v@mailbox.hueea16e32011-07-11 11:02:19 -0300221 }, {
Hans Verkuilbac63982012-06-10 07:39:52 -0300222 .id = V4L2_CID_SHARPNESS,
223 .minimum = 0,
224 .maximum = 4,
225 .step = 1,
226 .default_value = 0x0,
227 .off = 0,
istvan_v@mailbox.hueea16e32011-07-11 11:02:19 -0300228 /* NOTE: the value is converted and written to both even
229 and odd registers in the code */
Hans Verkuilbac63982012-06-10 07:39:52 -0300230 .reg = MO_FILTER_ODD,
231 .mask = 7 << 7,
232 .shift = 7,
istvan_v@mailbox.hueea16e32011-07-11 11:02:19 -0300233 }, {
Hans Verkuilbac63982012-06-10 07:39:52 -0300234 .id = V4L2_CID_CHROMA_AGC,
235 .minimum = 0,
236 .maximum = 1,
237 .default_value = 0x1,
238 .reg = MO_INPUT_FORMAT,
239 .mask = 1 << 10,
240 .shift = 10,
Frej Drejhammar6d042032008-03-23 22:43:21 -0300241 }, {
Hans Verkuilbac63982012-06-10 07:39:52 -0300242 .id = V4L2_CID_COLOR_KILLER,
243 .minimum = 0,
244 .maximum = 1,
245 .default_value = 0x1,
246 .reg = MO_INPUT_FORMAT,
247 .mask = 1 << 9,
248 .shift = 9,
Frej Drejhammar1b879c42008-03-23 22:43:24 -0300249 }, {
Hans Verkuilbac63982012-06-10 07:39:52 -0300250 .id = V4L2_CID_BAND_STOP_FILTER,
251 .minimum = 0,
252 .maximum = 1,
253 .step = 1,
254 .default_value = 0x0,
255 .off = 0,
256 .reg = MO_HTOTAL,
257 .mask = 3 << 11,
258 .shift = 11,
Hans Verkuil8c7cb122012-05-11 09:07:45 -0300259 }
260};
261
262static const struct cx88_ctrl cx8800_aud_ctls[] = {
263 {
Hans Verkuilbac63982012-06-10 07:39:52 -0300264 /* --- audio --- */
265 .id = V4L2_CID_AUDIO_MUTE,
266 .minimum = 0,
267 .maximum = 1,
268 .default_value = 1,
269 .reg = AUD_VOL_CTL,
270 .sreg = SHADOW_AUD_VOL_CTL,
271 .mask = (1 << 6),
272 .shift = 6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 },{
Hans Verkuilbac63982012-06-10 07:39:52 -0300274 .id = V4L2_CID_AUDIO_VOLUME,
275 .minimum = 0,
276 .maximum = 0x3f,
277 .step = 1,
278 .default_value = 0x3f,
279 .reg = AUD_VOL_CTL,
280 .sreg = SHADOW_AUD_VOL_CTL,
281 .mask = 0x3f,
282 .shift = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 },{
Hans Verkuilbac63982012-06-10 07:39:52 -0300284 .id = V4L2_CID_AUDIO_BALANCE,
285 .minimum = 0,
286 .maximum = 0x7f,
287 .step = 1,
288 .default_value = 0x40,
289 .reg = AUD_BAL_CTL,
290 .sreg = SHADOW_AUD_BAL_CTL,
291 .mask = 0x7f,
292 .shift = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294};
Hans Verkuilbac63982012-06-10 07:39:52 -0300295
Hans Verkuil8c7cb122012-05-11 09:07:45 -0300296enum {
297 CX8800_VID_CTLS = ARRAY_SIZE(cx8800_vid_ctls),
298 CX8800_AUD_CTLS = ARRAY_SIZE(cx8800_aud_ctls),
299};
Michael Krufky38a27132006-06-26 23:42:39 -0300300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301/* ------------------------------------------------------------------ */
302
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -0300303int cx88_video_mux(struct cx88_core *core, unsigned int input)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700305 /* struct cx88_core *core = dev->core; */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
Trent Piepho6a59d642007-08-15 14:41:57 -0300308 input, INPUT(input).vmux,
309 INPUT(input).gpio0,INPUT(input).gpio1,
310 INPUT(input).gpio2,INPUT(input).gpio3);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700311 core->input = input;
Trent Piepho6a59d642007-08-15 14:41:57 -0300312 cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input).vmux << 14);
313 cx_write(MO_GP3_IO, INPUT(input).gpio3);
314 cx_write(MO_GP0_IO, INPUT(input).gpio0);
315 cx_write(MO_GP1_IO, INPUT(input).gpio1);
316 cx_write(MO_GP2_IO, INPUT(input).gpio2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Trent Piepho6a59d642007-08-15 14:41:57 -0300318 switch (INPUT(input).type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 case CX88_VMUX_SVIDEO:
320 cx_set(MO_AFECFG_IO, 0x00000001);
321 cx_set(MO_INPUT_FORMAT, 0x00010010);
322 cx_set(MO_FILTER_EVEN, 0x00002020);
323 cx_set(MO_FILTER_ODD, 0x00002020);
324 break;
325 default:
326 cx_clear(MO_AFECFG_IO, 0x00000001);
327 cx_clear(MO_INPUT_FORMAT, 0x00010010);
328 cx_clear(MO_FILTER_EVEN, 0x00002020);
329 cx_clear(MO_FILTER_ODD, 0x00002020);
330 break;
331 }
Michael Krufkyf24546a2006-10-16 16:51:11 -0300332
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300333 /* if there are audioroutes defined, we have an external
334 ADC to deal with audio */
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300335 if (INPUT(input).audioroute) {
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300336 /* The wm8775 module has the "2" route hardwired into
337 the initialization. Some boards may use different
338 routes for different inputs. HVR-1300 surely does */
Hans Verkuil609c4c12013-05-29 10:44:22 -0300339 if (core->sd_wm8775) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300340 call_all(core, audio, s_routing,
istvan_v@mailbox.hu6e1f4df2010-03-27 09:31:32 -0300341 INPUT(input).audioroute, 0, 0);
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300342 }
Darron Broad430189d2008-10-15 14:18:42 -0300343 /* cx2388's C-ADC is connected to the tuner only.
344 When used with S-Video, that ADC is busy dealing with
345 chroma, so an external must be used for baseband audio */
istvan_v@mailbox.hu6e1f4df2010-03-27 09:31:32 -0300346 if (INPUT(input).type != CX88_VMUX_TELEVISION &&
347 INPUT(input).type != CX88_VMUX_CABLE) {
Darron Broad430189d2008-10-15 14:18:42 -0300348 /* "I2S ADC mode" */
349 core->tvaudio = WW_I2SADC;
350 cx88_set_tvaudio(core);
351 } else {
352 /* Normal mode */
353 cx_write(AUD_I2SCNTL, 0x0);
354 cx_clear(AUD_CTL, EN_I2SIN_ENABLE);
355 }
Michael Krufkyf24546a2006-10-16 16:51:11 -0300356 }
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return 0;
359}
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -0300360EXPORT_SYMBOL(cx88_video_mux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362/* ------------------------------------------------------------------ */
363
364static int start_video_dma(struct cx8800_dev *dev,
365 struct cx88_dmaqueue *q,
366 struct cx88_buffer *buf)
367{
368 struct cx88_core *core = dev->core;
369
370 /* setup fifo + format */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700371 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 buf->bpl, buf->risc.dma);
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300373 cx88_set_scale(core, core->width, core->height, core->field);
Hans Verkuil637bc202014-08-29 03:46:05 -0300374 cx_write(MO_COLOR_CTRL, dev->fmt->cxformat | ColorFormatGamma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 /* reset counter */
377 cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
Hans Verkuil94506842015-04-03 07:22:40 -0300378 q->count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 /* enable irqs */
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300381 cx_set(MO_PCI_INTMSK, core->pci_irqmask | PCI_INT_VIDINT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700382
383 /* Enables corresponding bits at PCI_INT_STAT:
384 bits 0 to 4: video, audio, transport stream, VIP, Host
385 bit 7: timer
386 bits 8 and 9: DMA complete for: SRC, DST
387 bits 10 and 11: BERR signal asserted for RISC: RD, WR
388 bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
389 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 cx_set(MO_VID_INTMSK, 0x0f0011);
391
392 /* enable capture */
393 cx_set(VID_CAPTURE_CONTROL,0x06);
394
395 /* start dma */
396 cx_set(MO_DEV_CNTRL2, (1<<5));
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700397 cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 return 0;
400}
401
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -0300402#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403static int stop_video_dma(struct cx8800_dev *dev)
404{
405 struct cx88_core *core = dev->core;
406
407 /* stop dma */
408 cx_clear(MO_VID_DMACNTRL, 0x11);
409
410 /* disable capture */
411 cx_clear(VID_CAPTURE_CONTROL,0x06);
412
413 /* disable irqs */
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300414 cx_clear(MO_PCI_INTMSK, PCI_INT_VIDINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 cx_clear(MO_VID_INTMSK, 0x0f0011);
416 return 0;
417}
418
419static int restart_video_queue(struct cx8800_dev *dev,
420 struct cx88_dmaqueue *q)
421{
Hans Verkuil6f11adc2014-08-10 11:56:14 -0300422 struct cx88_buffer *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 if (!list_empty(&q->active)) {
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300425 buf = list_entry(q->active.next, struct cx88_buffer, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
Junghak Sung2d700712015-09-22 10:30:30 -0300427 buf, buf->vb.vb2_buf.index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 start_video_dma(dev, q, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
Hans Verkuil6f11adc2014-08-10 11:56:14 -0300430 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
Mauro Carvalho Chehabbc5e66b2015-06-10 13:55:35 -0300432#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434/* ------------------------------------------------------------------ */
435
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200436static int queue_setup(struct vb2_queue *q,
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300437 unsigned int *num_buffers, unsigned int *num_planes,
Hans Verkuil36c0f8b2016-04-15 09:15:05 -0300438 unsigned int sizes[], struct device *alloc_devs[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300440 struct cx8800_dev *dev = q->drv_priv;
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300441 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300443 *num_planes = 1;
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300444 sizes[0] = (dev->fmt->depth * core->width * core->height) >> 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return 0;
446}
447
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300448static int buffer_prepare(struct vb2_buffer *vb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Junghak Sung2d700712015-09-22 10:30:30 -0300450 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300451 struct cx8800_dev *dev = vb->vb2_queue->drv_priv;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700452 struct cx88_core *core = dev->core;
Junghak Sung2d700712015-09-22 10:30:30 -0300453 struct cx88_buffer *buf = container_of(vbuf, struct cx88_buffer, vb);
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300454 struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300456 buf->bpl = core->width * dev->fmt->depth >> 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300458 if (vb2_plane_size(vb, 0) < core->height * buf->bpl)
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300459 return -EINVAL;
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300460 vb2_set_plane_payload(vb, 0, core->height * buf->bpl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300462 switch (core->field) {
Hans Verkuil637bc202014-08-29 03:46:05 -0300463 case V4L2_FIELD_TOP:
464 cx88_risc_buffer(dev->pci, &buf->risc,
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300465 sgt->sgl, 0, UNSET,
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300466 buf->bpl, 0, core->height);
Hans Verkuil637bc202014-08-29 03:46:05 -0300467 break;
468 case V4L2_FIELD_BOTTOM:
469 cx88_risc_buffer(dev->pci, &buf->risc,
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300470 sgt->sgl, UNSET, 0,
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300471 buf->bpl, 0, core->height);
Hans Verkuil637bc202014-08-29 03:46:05 -0300472 break;
473 case V4L2_FIELD_SEQ_TB:
474 cx88_risc_buffer(dev->pci, &buf->risc,
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300475 sgt->sgl,
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300476 0, buf->bpl * (core->height >> 1),
Hans Verkuil637bc202014-08-29 03:46:05 -0300477 buf->bpl, 0,
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300478 core->height >> 1);
Hans Verkuil637bc202014-08-29 03:46:05 -0300479 break;
480 case V4L2_FIELD_SEQ_BT:
481 cx88_risc_buffer(dev->pci, &buf->risc,
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300482 sgt->sgl,
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300483 buf->bpl * (core->height >> 1), 0,
Hans Verkuil637bc202014-08-29 03:46:05 -0300484 buf->bpl, 0,
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300485 core->height >> 1);
Hans Verkuil637bc202014-08-29 03:46:05 -0300486 break;
487 case V4L2_FIELD_INTERLACED:
488 default:
489 cx88_risc_buffer(dev->pci, &buf->risc,
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300490 sgt->sgl, 0, buf->bpl,
Hans Verkuil637bc202014-08-29 03:46:05 -0300491 buf->bpl, buf->bpl,
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300492 core->height >> 1);
Hans Verkuil637bc202014-08-29 03:46:05 -0300493 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
495 dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
Junghak Sung2d700712015-09-22 10:30:30 -0300496 buf, buf->vb.vb2_buf.index,
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300497 core->width, core->height, dev->fmt->depth, dev->fmt->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 (unsigned long)buf->risc.dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300502static void buffer_finish(struct vb2_buffer *vb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Junghak Sung2d700712015-09-22 10:30:30 -0300504 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300505 struct cx8800_dev *dev = vb->vb2_queue->drv_priv;
Junghak Sung2d700712015-09-22 10:30:30 -0300506 struct cx88_buffer *buf = container_of(vbuf, struct cx88_buffer, vb);
Hans Verkuil5e7045e2014-08-29 04:11:54 -0300507 struct cx88_riscmem *risc = &buf->risc;
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300508
Hans Verkuil5e7045e2014-08-29 04:11:54 -0300509 if (risc->cpu)
510 pci_free_consistent(dev->pci, risc->size, risc->cpu, risc->dma);
511 memset(risc, 0, sizeof(*risc));
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300512}
513
514static void buffer_queue(struct vb2_buffer *vb)
515{
Junghak Sung2d700712015-09-22 10:30:30 -0300516 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300517 struct cx8800_dev *dev = vb->vb2_queue->drv_priv;
Junghak Sung2d700712015-09-22 10:30:30 -0300518 struct cx88_buffer *buf = container_of(vbuf, struct cx88_buffer, vb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 struct cx88_buffer *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 struct cx88_dmaqueue *q = &dev->vidq;
521
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300522 /* add jump to start */
523 buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 8);
524 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
525 buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Hans Verkuil6f11adc2014-08-10 11:56:14 -0300527 if (list_empty(&q->active)) {
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300528 list_add_tail(&buf->list, &q->active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 dprintk(2,"[%p/%d] buffer_queue - first active\n",
Junghak Sung2d700712015-09-22 10:30:30 -0300530 buf, buf->vb.vb2_buf.index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 } else {
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300533 buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
534 prev = list_entry(q->active.prev, struct cx88_buffer, list);
535 list_add_tail(&buf->list, &q->active);
Hans Verkuil637bc202014-08-29 03:46:05 -0300536 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
537 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
Junghak Sung2d700712015-09-22 10:30:30 -0300538 buf, buf->vb.vb2_buf.index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
540}
541
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300542static int start_streaming(struct vb2_queue *q, unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300544 struct cx8800_dev *dev = q->drv_priv;
545 struct cx88_dmaqueue *dmaq = &dev->vidq;
546 struct cx88_buffer *buf = list_entry(dmaq->active.next,
547 struct cx88_buffer, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300549 start_video_dma(dev, dmaq, buf);
550 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300553static void stop_streaming(struct vb2_queue *q)
554{
555 struct cx8800_dev *dev = q->drv_priv;
556 struct cx88_core *core = dev->core;
557 struct cx88_dmaqueue *dmaq = &dev->vidq;
558 unsigned long flags;
559
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300560 cx_clear(MO_VID_DMACNTRL, 0x11);
561 cx_clear(VID_CAPTURE_CONTROL, 0x06);
562 spin_lock_irqsave(&dev->slock, flags);
563 while (!list_empty(&dmaq->active)) {
564 struct cx88_buffer *buf = list_entry(dmaq->active.next,
565 struct cx88_buffer, list);
566
567 list_del(&buf->list);
Junghak Sung2d700712015-09-22 10:30:30 -0300568 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300569 }
570 spin_unlock_irqrestore(&dev->slock, flags);
571}
572
Julia Lawall10accd22016-09-08 20:59:18 -0300573static const struct vb2_ops cx8800_video_qops = {
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300574 .queue_setup = queue_setup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 .buf_prepare = buffer_prepare,
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300576 .buf_finish = buffer_finish,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 .buf_queue = buffer_queue,
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300578 .wait_prepare = vb2_ops_wait_prepare,
579 .wait_finish = vb2_ops_wait_finish,
580 .start_streaming = start_streaming,
581 .stop_streaming = stop_streaming,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582};
583
584/* ------------------------------------------------------------------ */
585
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300586static int radio_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200588 struct cx8800_dev *dev = video_drvdata(file);
Dan Carpenter5401c2d2010-10-21 16:21:45 -0300589 struct cx88_core *core = dev->core;
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300590 int ret = v4l2_fh_open(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300592 if (ret)
593 return ret;
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200594
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300595 cx_write(MO_GP3_IO, core->board.radio.gpio3);
596 cx_write(MO_GP0_IO, core->board.radio.gpio0);
597 cx_write(MO_GP1_IO, core->board.radio.gpio1);
598 cx_write(MO_GP2_IO, core->board.radio.gpio2);
599 if (core->board.radio.audioroute) {
600 if (core->sd_wm8775) {
601 call_all(core, audio, s_routing,
Hans Verkuil5325b422009-04-02 11:26:22 -0300602 core->board.radio.audioroute, 0, 0);
Darron Broad430189d2008-10-15 14:18:42 -0300603 }
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300604 /* "I2S ADC mode" */
605 core->tvaudio = WW_I2SADC;
606 cx88_set_tvaudio(core);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 } else {
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300608 /* FM Mode */
609 core->tvaudio = WW_FM;
610 cx88_set_tvaudio(core);
611 cx88_set_stereo(core, V4L2_TUNER_MODE_STEREO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300613 call_all(core, tuner, s_radio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return 0;
615}
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617/* ------------------------------------------------------------------ */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300618/* VIDEO CTRL IOCTLS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Hans Verkuil8c7cb122012-05-11 09:07:45 -0300620static int cx8800_s_vid_ctrl(struct v4l2_ctrl *ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Hans Verkuilbac63982012-06-10 07:39:52 -0300622 struct cx88_core *core =
Hans Verkuil8c7cb122012-05-11 09:07:45 -0300623 container_of(ctrl->handler, struct cx88_core, video_hdl);
624 const struct cx88_ctrl *cc = ctrl->priv;
625 u32 value, mask;
626
627 mask = cc->mask;
628 switch (ctrl->id) {
629 case V4L2_CID_SATURATION:
630 /* special v_sat handling */
631
632 value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
633
634 if (core->tvnorm & V4L2_STD_SECAM) {
635 /* For SECAM, both U and V sat should be equal */
636 value = value << 8 | value;
637 } else {
638 /* Keeps U Saturation proportional to V Sat */
639 value = (value * 0x5a) / 0x7f << 8 | value;
640 }
641 mask = 0xffff;
642 break;
643 case V4L2_CID_SHARPNESS:
644 /* 0b000, 0b100, 0b101, 0b110, or 0b111 */
645 value = (ctrl->val < 1 ? 0 : ((ctrl->val + 3) << 7));
646 /* needs to be set for both fields */
647 cx_andor(MO_FILTER_EVEN, mask, value);
648 break;
649 case V4L2_CID_CHROMA_AGC:
650 value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
651 break;
652 default:
653 value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
654 break;
655 }
656 dprintk(1, "set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
657 ctrl->id, ctrl->name, ctrl->val, cc->reg, value,
658 mask, cc->sreg ? " [shadowed]" : "");
659 if (cc->sreg)
660 cx_sandor(cc->sreg, cc->reg, mask, value);
661 else
662 cx_andor(cc->reg, mask, value);
663 return 0;
664}
665
666static int cx8800_s_aud_ctrl(struct v4l2_ctrl *ctrl)
667{
668 struct cx88_core *core =
669 container_of(ctrl->handler, struct cx88_core, audio_hdl);
Hans Verkuilbac63982012-06-10 07:39:52 -0300670 const struct cx88_ctrl *cc = ctrl->priv;
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200671 u32 value,mask;
Lawrence Rust69518032011-02-06 17:46:12 -0300672
673 /* Pass changes onto any WM8775 */
Hans Verkuil609c4c12013-05-29 10:44:22 -0300674 if (core->sd_wm8775) {
Hans Verkuilbac63982012-06-10 07:39:52 -0300675 switch (ctrl->id) {
Lawrence Rust69518032011-02-06 17:46:12 -0300676 case V4L2_CID_AUDIO_MUTE:
Hans Verkuilbac63982012-06-10 07:39:52 -0300677 wm8775_s_ctrl(core, ctrl->id, ctrl->val);
Lawrence Rust69518032011-02-06 17:46:12 -0300678 break;
679 case V4L2_CID_AUDIO_VOLUME:
Hans Verkuilbac63982012-06-10 07:39:52 -0300680 wm8775_s_ctrl(core, ctrl->id, (ctrl->val) ?
681 (0x90 + ctrl->val) << 8 : 0);
Lawrence Rust69518032011-02-06 17:46:12 -0300682 break;
683 case V4L2_CID_AUDIO_BALANCE:
Hans Verkuilbac63982012-06-10 07:39:52 -0300684 wm8775_s_ctrl(core, ctrl->id, ctrl->val << 9);
Lawrence Rust69518032011-02-06 17:46:12 -0300685 break;
686 default:
Lawrence Rust69518032011-02-06 17:46:12 -0300687 break;
688 }
Lawrence Rust69518032011-02-06 17:46:12 -0300689 }
690
Hans Verkuilbac63982012-06-10 07:39:52 -0300691 mask = cc->mask;
692 switch (ctrl->id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 case V4L2_CID_AUDIO_BALANCE:
Hans Verkuilbac63982012-06-10 07:39:52 -0300694 value = (ctrl->val < 0x40) ? (0x7f - ctrl->val) : (ctrl->val - 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 break;
696 case V4L2_CID_AUDIO_VOLUME:
Hans Verkuilbac63982012-06-10 07:39:52 -0300697 value = 0x3f - (ctrl->val & 0x3f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 default:
Hans Verkuilbac63982012-06-10 07:39:52 -0300700 value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 break;
702 }
Ian Pickworth6457af52006-02-27 00:09:45 -0300703 dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
Hans Verkuilbac63982012-06-10 07:39:52 -0300704 ctrl->id, ctrl->name, ctrl->val, cc->reg, value,
705 mask, cc->sreg ? " [shadowed]" : "");
706 if (cc->sreg)
707 cx_sandor(cc->sreg, cc->reg, mask, value);
708 else
709 cx_andor(cc->reg, mask, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return 0;
711}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713/* ------------------------------------------------------------------ */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300714/* VIDEO IOCTLS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Hans Verkuil78b526a2008-05-28 12:16:41 -0300716static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300717 struct v4l2_format *f)
718{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300719 struct cx8800_dev *dev = video_drvdata(file);
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300720 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300721
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300722 f->fmt.pix.width = core->width;
723 f->fmt.pix.height = core->height;
724 f->fmt.pix.field = core->field;
Hans Verkuilc5a86142012-05-11 10:45:18 -0300725 f->fmt.pix.pixelformat = dev->fmt->fourcc;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300726 f->fmt.pix.bytesperline =
Hans Verkuilc5a86142012-05-11 10:45:18 -0300727 (f->fmt.pix.width * dev->fmt->depth) >> 3;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300728 f->fmt.pix.sizeimage =
729 f->fmt.pix.height * f->fmt.pix.bytesperline;
Hans Verkuilc5a86142012-05-11 10:45:18 -0300730 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300731 return 0;
732}
733
Hans Verkuil78b526a2008-05-28 12:16:41 -0300734static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 struct v4l2_format *f)
736{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300737 struct cx8800_dev *dev = video_drvdata(file);
738 struct cx88_core *core = dev->core;
lawrence rust2e4e98e2010-08-25 09:50:20 -0300739 const struct cx8800_fmt *fmt;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300740 enum v4l2_field field;
741 unsigned int maxw, maxh;
742
743 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
744 if (NULL == fmt)
745 return -EINVAL;
746
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300747 maxw = norm_maxw(core->tvnorm);
748 maxh = norm_maxh(core->tvnorm);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300749
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300750 field = f->fmt.pix.field;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300751
752 switch (field) {
753 case V4L2_FIELD_TOP:
754 case V4L2_FIELD_BOTTOM:
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300755 case V4L2_FIELD_INTERLACED:
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300756 case V4L2_FIELD_SEQ_BT:
757 case V4L2_FIELD_SEQ_TB:
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300758 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 default:
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300760 field = (f->fmt.pix.height > maxh / 2)
761 ? V4L2_FIELD_INTERLACED
762 : V4L2_FIELD_BOTTOM;
763 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300765 if (V4L2_FIELD_HAS_T_OR_B(field))
766 maxh /= 2;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300767
Trent Piepho4b899452009-05-30 21:45:46 -0300768 v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
769 &f->fmt.pix.height, 32, maxh, 0, 0);
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300770 f->fmt.pix.field = field;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300771 f->fmt.pix.bytesperline =
772 (f->fmt.pix.width * fmt->depth) >> 3;
773 f->fmt.pix.sizeimage =
774 f->fmt.pix.height * f->fmt.pix.bytesperline;
Hans Verkuil94506842015-04-03 07:22:40 -0300775 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300776
777 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
Hans Verkuil78b526a2008-05-28 12:16:41 -0300780static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300781 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300783 struct cx8800_dev *dev = video_drvdata(file);
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300784 struct cx88_core *core = dev->core;
Hans Verkuil78b526a2008-05-28 12:16:41 -0300785 int err = vidioc_try_fmt_vid_cap (file,priv,f);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700786
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300787 if (0 != err)
788 return err;
Hans Verkuil078859a2014-08-29 06:08:07 -0300789 if (vb2_is_busy(&dev->vb2_vidq) || vb2_is_busy(&dev->vb2_vbiq))
790 return -EBUSY;
791 if (core->dvbdev && vb2_is_busy(&core->dvbdev->vb2_mpegq))
792 return -EBUSY;
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300793 dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
794 core->width = f->fmt.pix.width;
795 core->height = f->fmt.pix.height;
796 core->field = f->fmt.pix.field;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300797 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
799
Hans Verkuil902e1972012-05-09 16:23:07 -0300800void cx88_querycap(struct file *file, struct cx88_core *core,
801 struct v4l2_capability *cap)
802{
803 struct video_device *vdev = video_devdata(file);
804
805 strlcpy(cap->card, core->board.name, sizeof(cap->card));
806 cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
807 if (UNSET != core->board.tuner_type)
808 cap->device_caps |= V4L2_CAP_TUNER;
809 switch (vdev->vfl_type) {
810 case VFL_TYPE_RADIO:
811 cap->device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
812 break;
813 case VFL_TYPE_GRABBER:
814 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
815 break;
816 case VFL_TYPE_VBI:
817 cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
818 break;
819 }
820 cap->capabilities = cap->device_caps | V4L2_CAP_VIDEO_CAPTURE |
821 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_DEVICE_CAPS;
822 if (core->board.radio.type == CX88_RADIO)
823 cap->capabilities |= V4L2_CAP_RADIO;
824}
825EXPORT_SYMBOL(cx88_querycap);
826
827static int vidioc_querycap(struct file *file, void *priv,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300828 struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300830 struct cx8800_dev *dev = video_drvdata(file);
831 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300833 strcpy(cap->driver, "cx8800");
Hans Verkuil902e1972012-05-09 16:23:07 -0300834 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
835 cx88_querycap(file, core, cap);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300836 return 0;
837}
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700838
Hans Verkuil78b526a2008-05-28 12:16:41 -0300839static int vidioc_enum_fmt_vid_cap (struct file *file, void *priv,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300840 struct v4l2_fmtdesc *f)
841{
842 if (unlikely(f->index >= ARRAY_SIZE(formats)))
843 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300845 strlcpy(f->description,formats[f->index].name,sizeof(f->description));
846 f->pixelformat = formats[f->index].fourcc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300848 return 0;
849}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Hans Verkuil48d68802012-05-25 12:15:30 -0300851static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorm)
852{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300853 struct cx8800_dev *dev = video_drvdata(file);
854 struct cx88_core *core = dev->core;
Hans Verkuil48d68802012-05-25 12:15:30 -0300855
856 *tvnorm = core->tvnorm;
857 return 0;
858}
859
Hans Verkuil314527a2013-03-15 06:10:40 -0300860static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300861{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300862 struct cx8800_dev *dev = video_drvdata(file);
863 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300864
Hans Verkuil078859a2014-08-29 06:08:07 -0300865 return cx88_set_tvnorm(core, tvnorms);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300866}
867
868/* only one input in this sample driver */
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300869int cx88_enum_input (struct cx88_core *core,struct v4l2_input *i)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300870{
lawrence rust2e4e98e2010-08-25 09:50:20 -0300871 static const char * const iname[] = {
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300872 [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
873 [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
874 [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
875 [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
876 [ CX88_VMUX_SVIDEO ] = "S-Video",
877 [ CX88_VMUX_TELEVISION ] = "Television",
878 [ CX88_VMUX_CABLE ] = "Cable TV",
879 [ CX88_VMUX_DVB ] = "DVB",
880 [ CX88_VMUX_DEBUG ] = "for debug only",
881 };
Trent Piephof3334bc2009-03-04 01:21:03 -0300882 unsigned int n = i->index;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300883
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300884 if (n >= 4)
885 return -EINVAL;
Trent Piepho6a59d642007-08-15 14:41:57 -0300886 if (0 == INPUT(n).type)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300887 return -EINVAL;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300888 i->type = V4L2_INPUT_TYPE_CAMERA;
Trent Piepho6a59d642007-08-15 14:41:57 -0300889 strcpy(i->name,iname[INPUT(n).type]);
890 if ((CX88_VMUX_TELEVISION == INPUT(n).type) ||
Julia Lawall473d8022010-08-05 17:22:44 -0300891 (CX88_VMUX_CABLE == INPUT(n).type)) {
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300892 i->type = V4L2_INPUT_TYPE_TUNER;
Julia Lawall473d8022010-08-05 17:22:44 -0300893 }
Hans Verkuilf33e9862012-05-25 12:04:10 -0300894 i->std = CX88_NORMS;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300895 return 0;
896}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300897EXPORT_SYMBOL(cx88_enum_input);
898
899static int vidioc_enum_input (struct file *file, void *priv,
900 struct v4l2_input *i)
901{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300902 struct cx8800_dev *dev = video_drvdata(file);
903 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300904 return cx88_enum_input (core,i);
905}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300906
907static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
908{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300909 struct cx8800_dev *dev = video_drvdata(file);
910 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300911
912 *i = core->input;
913 return 0;
914}
915
916static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
917{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300918 struct cx8800_dev *dev = video_drvdata(file);
919 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300920
921 if (i >= 4)
922 return -EINVAL;
Hans Verkuilf33e9862012-05-25 12:04:10 -0300923 if (0 == INPUT(i).type)
924 return -EINVAL;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300925
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300926 cx88_newstation(core);
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -0300927 cx88_video_mux(core,i);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300928 return 0;
929}
930
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300931static int vidioc_g_tuner (struct file *file, void *priv,
932 struct v4l2_tuner *t)
933{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300934 struct cx8800_dev *dev = video_drvdata(file);
935 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300936 u32 reg;
937
Trent Piepho6a59d642007-08-15 14:41:57 -0300938 if (unlikely(UNSET == core->board.tuner_type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300939 return -EINVAL;
Mauro Carvalho Chehab243d8c02007-01-20 13:58:36 -0300940 if (0 != t->index)
941 return -EINVAL;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300942
943 strcpy(t->name, "Television");
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300944 t->capability = V4L2_TUNER_CAP_NORM;
945 t->rangehigh = 0xffffffffUL;
Hans Verkuilf33e9862012-05-25 12:04:10 -0300946 call_all(core, tuner, g_tuner, t);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300947
948 cx88_get_stereo(core ,t);
949 reg = cx_read(MO_DEVICE_STATUS);
950 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
951 return 0;
952}
953
954static int vidioc_s_tuner (struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -0300955 const struct v4l2_tuner *t)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300956{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300957 struct cx8800_dev *dev = video_drvdata(file);
958 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300959
Trent Piepho6a59d642007-08-15 14:41:57 -0300960 if (UNSET == core->board.tuner_type)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300961 return -EINVAL;
962 if (0 != t->index)
963 return -EINVAL;
964
965 cx88_set_stereo(core, t->audmode, 1);
966 return 0;
967}
968
969static int vidioc_g_frequency (struct file *file, void *priv,
970 struct v4l2_frequency *f)
971{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300972 struct cx8800_dev *dev = video_drvdata(file);
973 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300974
Trent Piepho6a59d642007-08-15 14:41:57 -0300975 if (unlikely(UNSET == core->board.tuner_type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300976 return -EINVAL;
Hans Verkuilf33e9862012-05-25 12:04:10 -0300977 if (f->tuner)
978 return -EINVAL;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300979
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300980 f->frequency = core->freq;
981
Hans Verkuilb8341e12009-03-29 08:26:01 -0300982 call_all(core, tuner, g_frequency, f);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300983
984 return 0;
985}
986
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300987int cx88_set_freq (struct cx88_core *core,
Hans Verkuilb530a442013-03-19 04:09:26 -0300988 const struct v4l2_frequency *f)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300989{
Hans Verkuilb530a442013-03-19 04:09:26 -0300990 struct v4l2_frequency new_freq = *f;
991
Trent Piepho6a59d642007-08-15 14:41:57 -0300992 if (unlikely(UNSET == core->board.tuner_type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300993 return -EINVAL;
994 if (unlikely(f->tuner != 0))
995 return -EINVAL;
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300996
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300997 cx88_newstation(core);
Hans Verkuilb8341e12009-03-29 08:26:01 -0300998 call_all(core, tuner, s_frequency, f);
Hans Verkuilb530a442013-03-19 04:09:26 -0300999 call_all(core, tuner, g_frequency, &new_freq);
1000 core->freq = new_freq.frequency;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001001
1002 /* When changing channels it is required to reset TVAUDIO */
1003 msleep (10);
1004 cx88_set_tvaudio(core);
1005
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001006 return 0;
1007}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001008EXPORT_SYMBOL(cx88_set_freq);
1009
1010static int vidioc_s_frequency (struct file *file, void *priv,
Hans Verkuilb530a442013-03-19 04:09:26 -03001011 const struct v4l2_frequency *f)
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001012{
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001013 struct cx8800_dev *dev = video_drvdata(file);
1014 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001015
Hans Verkuiledbd1382012-05-11 10:33:25 -03001016 return cx88_set_freq(core, f);
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001017}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001018
Trent Piephodbbff482007-01-22 23:31:53 -03001019#ifdef CONFIG_VIDEO_ADV_DEBUG
1020static int vidioc_g_register (struct file *file, void *fh,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001021 struct v4l2_dbg_register *reg)
Trent Piephodbbff482007-01-22 23:31:53 -03001022{
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001023 struct cx8800_dev *dev = video_drvdata(file);
1024 struct cx88_core *core = dev->core;
Trent Piephodbbff482007-01-22 23:31:53 -03001025
Trent Piephodbbff482007-01-22 23:31:53 -03001026 /* cx2388x has a 24-bit register space */
Hans Verkuil7feeb142013-05-29 06:22:02 -03001027 reg->val = cx_read(reg->reg & 0xfffffc);
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001028 reg->size = 4;
Trent Piephodbbff482007-01-22 23:31:53 -03001029 return 0;
1030}
1031
1032static int vidioc_s_register (struct file *file, void *fh,
Hans Verkuil977ba3b12013-03-24 08:28:46 -03001033 const struct v4l2_dbg_register *reg)
Trent Piephodbbff482007-01-22 23:31:53 -03001034{
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001035 struct cx8800_dev *dev = video_drvdata(file);
1036 struct cx88_core *core = dev->core;
Trent Piephodbbff482007-01-22 23:31:53 -03001037
Hans Verkuil7feeb142013-05-29 06:22:02 -03001038 cx_write(reg->reg & 0xfffffc, reg->val);
Trent Piephodbbff482007-01-22 23:31:53 -03001039 return 0;
1040}
1041#endif
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043/* ----------------------------------------------------------- */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001044/* RADIO ESPECIFIC IOCTLS */
1045/* ----------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001047static int radio_g_tuner (struct file *file, void *priv,
1048 struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001050 struct cx8800_dev *dev = video_drvdata(file);
1051 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001052
1053 if (unlikely(t->index > 0))
1054 return -EINVAL;
1055
1056 strcpy(t->name, "Radio");
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001057
Hans Verkuilb8341e12009-03-29 08:26:01 -03001058 call_all(core, tuner, g_tuner, t);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001059 return 0;
1060}
1061
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001062static int radio_s_tuner (struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -03001063 const struct v4l2_tuner *t)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001064{
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001065 struct cx8800_dev *dev = video_drvdata(file);
1066 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001067
1068 if (0 != t->index)
1069 return -EINVAL;
1070
Hans Verkuilb8341e12009-03-29 08:26:01 -03001071 call_all(core, tuner, s_tuner, t);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001072 return 0;
1073}
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075/* ----------------------------------------------------------- */
1076
lawrence rust2e4e98e2010-08-25 09:50:20 -03001077static const char *cx88_vid_irqs[32] = {
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -07001078 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1079 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1080 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1081 "y_sync", "u_sync", "v_sync", "vbi_sync",
1082 "opc_err", "par_err", "rip_err", "pci_abort",
1083};
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085static void cx8800_vid_irq(struct cx8800_dev *dev)
1086{
1087 struct cx88_core *core = dev->core;
1088 u32 status, mask, count;
1089
1090 status = cx_read(MO_VID_INTSTAT);
1091 mask = cx_read(MO_VID_INTMSK);
1092 if (0 == (status & mask))
1093 return;
1094 cx_write(MO_VID_INTSTAT, status);
1095 if (irq_debug || (status & mask & ~0xff))
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001096 cx88_print_irqbits("irq vid",
Mauro Carvalho Chehab66623a02007-03-29 08:47:04 -03001097 cx88_vid_irqs, ARRAY_SIZE(cx88_vid_irqs),
1098 status, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 /* risc op code error */
1101 if (status & (1 << 16)) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001102 pr_warn("video risc op code error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 cx_clear(MO_VID_DMACNTRL, 0x11);
1104 cx_clear(VID_CAPTURE_CONTROL, 0x06);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001105 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
1107
1108 /* risc1 y */
1109 if (status & 0x01) {
1110 spin_lock(&dev->slock);
1111 count = cx_read(MO_VIDY_GPCNT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001112 cx88_wakeup(core, &dev->vidq, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 spin_unlock(&dev->slock);
1114 }
1115
1116 /* risc1 vbi */
1117 if (status & 0x08) {
1118 spin_lock(&dev->slock);
1119 count = cx_read(MO_VBI_GPCNT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001120 cx88_wakeup(core, &dev->vbiq, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 spin_unlock(&dev->slock);
1122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123}
1124
David Howells7d12e782006-10-05 14:55:46 +01001125static irqreturn_t cx8800_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
1127 struct cx8800_dev *dev = dev_id;
1128 struct cx88_core *core = dev->core;
1129 u32 status;
1130 int loop, handled = 0;
1131
1132 for (loop = 0; loop < 10; loop++) {
Trent Piepho8ddac9e2007-08-18 06:57:55 -03001133 status = cx_read(MO_PCI_INTSTAT) &
1134 (core->pci_irqmask | PCI_INT_VIDINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if (0 == status)
1136 goto out;
1137 cx_write(MO_PCI_INTSTAT, status);
1138 handled = 1;
1139
1140 if (status & core->pci_irqmask)
1141 cx88_core_irq(core,status);
Trent Piepho8ddac9e2007-08-18 06:57:55 -03001142 if (status & PCI_INT_VIDINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 cx8800_vid_irq(dev);
Peter Senna Tschudinc2c1b412012-09-28 05:37:22 -03001144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (10 == loop) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001146 pr_warn("irq loop -- clearing mask\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 cx_write(MO_PCI_INTMSK,0);
1148 }
1149
1150 out:
1151 return IRQ_RETVAL(handled);
1152}
1153
1154/* ----------------------------------------------------------- */
1155/* exported stuff */
1156
Hans Verkuilbec43662008-12-30 06:58:20 -03001157static const struct v4l2_file_operations video_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
1159 .owner = THIS_MODULE,
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001160 .open = v4l2_fh_open,
1161 .release = vb2_fop_release,
1162 .read = vb2_fop_read,
1163 .poll = vb2_fop_poll,
1164 .mmap = vb2_fop_mmap,
Mauro Carvalho Chehabb61872642011-02-13 21:52:02 -03001165 .unlocked_ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166};
1167
Hans Verkuila3998102008-07-21 02:57:38 -03001168static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001169 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001170 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1171 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1172 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1173 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001174 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1175 .vidioc_querybuf = vb2_ioctl_querybuf,
1176 .vidioc_qbuf = vb2_ioctl_qbuf,
1177 .vidioc_dqbuf = vb2_ioctl_dqbuf,
Hans Verkuil48d68802012-05-25 12:15:30 -03001178 .vidioc_g_std = vidioc_g_std,
Hans Verkuilf33e9862012-05-25 12:04:10 -03001179 .vidioc_s_std = vidioc_s_std,
1180 .vidioc_enum_input = vidioc_enum_input,
1181 .vidioc_g_input = vidioc_g_input,
1182 .vidioc_s_input = vidioc_s_input,
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001183 .vidioc_streamon = vb2_ioctl_streamon,
1184 .vidioc_streamoff = vb2_ioctl_streamoff,
Hans Verkuilf33e9862012-05-25 12:04:10 -03001185 .vidioc_g_tuner = vidioc_g_tuner,
1186 .vidioc_s_tuner = vidioc_s_tuner,
1187 .vidioc_g_frequency = vidioc_g_frequency,
1188 .vidioc_s_frequency = vidioc_s_frequency,
1189 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1190 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Hans Verkuilf33e9862012-05-25 12:04:10 -03001191#ifdef CONFIG_VIDEO_ADV_DEBUG
1192 .vidioc_g_register = vidioc_g_register,
1193 .vidioc_s_register = vidioc_s_register,
1194#endif
1195};
1196
1197static const struct video_device cx8800_video_template = {
1198 .name = "cx8800-video",
1199 .fops = &video_fops,
1200 .ioctl_ops = &video_ioctl_ops,
1201 .tvnorms = CX88_NORMS,
Hans Verkuilf33e9862012-05-25 12:04:10 -03001202};
1203
1204static const struct v4l2_ioctl_ops vbi_ioctl_ops = {
1205 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001206 .vidioc_g_fmt_vbi_cap = cx8800_vbi_fmt,
1207 .vidioc_try_fmt_vbi_cap = cx8800_vbi_fmt,
1208 .vidioc_s_fmt_vbi_cap = cx8800_vbi_fmt,
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001209 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1210 .vidioc_querybuf = vb2_ioctl_querybuf,
1211 .vidioc_qbuf = vb2_ioctl_qbuf,
1212 .vidioc_dqbuf = vb2_ioctl_dqbuf,
Hans Verkuil48d68802012-05-25 12:15:30 -03001213 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001214 .vidioc_s_std = vidioc_s_std,
1215 .vidioc_enum_input = vidioc_enum_input,
1216 .vidioc_g_input = vidioc_g_input,
1217 .vidioc_s_input = vidioc_s_input,
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001218 .vidioc_streamon = vb2_ioctl_streamon,
1219 .vidioc_streamoff = vb2_ioctl_streamoff,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001220 .vidioc_g_tuner = vidioc_g_tuner,
1221 .vidioc_s_tuner = vidioc_s_tuner,
1222 .vidioc_g_frequency = vidioc_g_frequency,
1223 .vidioc_s_frequency = vidioc_s_frequency,
Trent Piephodbbff482007-01-22 23:31:53 -03001224#ifdef CONFIG_VIDEO_ADV_DEBUG
1225 .vidioc_g_register = vidioc_g_register,
1226 .vidioc_s_register = vidioc_s_register,
1227#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001228};
1229
Hans Verkuilf33e9862012-05-25 12:04:10 -03001230static const struct video_device cx8800_vbi_template = {
1231 .name = "cx8800-vbi",
Hans Verkuila3998102008-07-21 02:57:38 -03001232 .fops = &video_fops,
Hans Verkuilf33e9862012-05-25 12:04:10 -03001233 .ioctl_ops = &vbi_ioctl_ops,
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001234 .tvnorms = CX88_NORMS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235};
1236
Hans Verkuilbec43662008-12-30 06:58:20 -03001237static const struct v4l2_file_operations radio_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
1239 .owner = THIS_MODULE,
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001240 .open = radio_open,
Hans Verkuil1a3c60a2012-05-11 11:25:03 -03001241 .poll = v4l2_ctrl_poll,
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001242 .release = v4l2_fh_release,
Mauro Carvalho Chehabb61872642011-02-13 21:52:02 -03001243 .unlocked_ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244};
1245
Hans Verkuila3998102008-07-21 02:57:38 -03001246static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Hans Verkuil902e1972012-05-09 16:23:07 -03001247 .vidioc_querycap = vidioc_querycap,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001248 .vidioc_g_tuner = radio_g_tuner,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001249 .vidioc_s_tuner = radio_s_tuner,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001250 .vidioc_g_frequency = vidioc_g_frequency,
1251 .vidioc_s_frequency = vidioc_s_frequency,
Hans Verkuil1a3c60a2012-05-11 11:25:03 -03001252 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1253 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Trent Piephoa75d2042007-08-01 00:13:28 -03001254#ifdef CONFIG_VIDEO_ADV_DEBUG
1255 .vidioc_g_register = vidioc_g_register,
1256 .vidioc_s_register = vidioc_s_register,
1257#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258};
1259
lawrence rust2e4e98e2010-08-25 09:50:20 -03001260static const struct video_device cx8800_radio_template = {
Hans Verkuila3998102008-07-21 02:57:38 -03001261 .name = "cx8800-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03001262 .fops = &radio_fops,
Hans Verkuila3998102008-07-21 02:57:38 -03001263 .ioctl_ops = &radio_ioctl_ops,
1264};
1265
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001266static const struct v4l2_ctrl_ops cx8800_ctrl_vid_ops = {
1267 .s_ctrl = cx8800_s_vid_ctrl,
1268};
1269
1270static const struct v4l2_ctrl_ops cx8800_ctrl_aud_ops = {
1271 .s_ctrl = cx8800_s_aud_ctrl,
Hans Verkuilbac63982012-06-10 07:39:52 -03001272};
1273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274/* ----------------------------------------------------------- */
1275
1276static void cx8800_unregister_video(struct cx8800_dev *dev)
1277{
Hans Verkuil34080bc2015-03-09 13:34:00 -03001278 video_unregister_device(&dev->radio_dev);
1279 video_unregister_device(&dev->vbi_dev);
1280 video_unregister_device(&dev->video_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
1282
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001283static int cx8800_initdev(struct pci_dev *pci_dev,
1284 const struct pci_device_id *pci_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
1286 struct cx8800_dev *dev;
1287 struct cx88_core *core;
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001288 struct vb2_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 int err;
Hans Verkuilbac63982012-06-10 07:39:52 -03001290 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Panagiotis Issaris74081872006-01-11 19:40:56 -02001292 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 if (NULL == dev)
1294 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 /* pci init */
1297 dev->pci = pci_dev;
1298 if (pci_enable_device(pci_dev)) {
1299 err = -EIO;
1300 goto fail_free;
1301 }
1302 core = cx88_core_get(dev->pci);
1303 if (NULL == core) {
1304 err = -EINVAL;
1305 goto fail_free;
1306 }
1307 dev->core = core;
1308
1309 /* print pci info */
Bjørn Morkabd34d82011-03-21 11:35:56 -03001310 dev->pci_rev = pci_dev->revision;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001311 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001312 pr_info("found at %s, rev: %d, irq: %d, latency: %d, mmio: 0x%llx\n",
1313 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
1314 dev->pci_lat,
1315 (unsigned long long)pci_resource_start(pci_dev, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 pci_set_master(pci_dev);
Christoph Hellwig1a47de62015-11-20 15:57:07 -08001318 err = pci_set_dma_mask(pci_dev,DMA_BIT_MASK(32));
1319 if (err) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001320 pr_err("Oops: no 32bit PCI DMA ???\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 goto fail_core;
1322 }
1323
1324 /* initialize driver struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 spin_lock_init(&dev->slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 /* init video dma queues */
1328 INIT_LIST_HEAD(&dev->vidq.active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 /* init vbi dma queues */
1331 INIT_LIST_HEAD(&dev->vbiq.active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 /* get irq */
1334 err = request_irq(pci_dev->irq, cx8800_irq,
Michael Opdenacker3e018fe2013-10-13 02:49:29 -03001335 IRQF_SHARED, core->name, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 if (err < 0) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001337 pr_err("can't get IRQ %d\n", pci_dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 goto fail_core;
1339 }
1340 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1341
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001342 for (i = 0; i < CX8800_AUD_CTLS; i++) {
1343 const struct cx88_ctrl *cc = &cx8800_aud_ctls[i];
Hans Verkuilbac63982012-06-10 07:39:52 -03001344 struct v4l2_ctrl *vc;
1345
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001346 vc = v4l2_ctrl_new_std(&core->audio_hdl, &cx8800_ctrl_aud_ops,
Hans Verkuilbac63982012-06-10 07:39:52 -03001347 cc->id, cc->minimum, cc->maximum, cc->step, cc->default_value);
1348 if (vc == NULL) {
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001349 err = core->audio_hdl.error;
Hans Verkuilbac63982012-06-10 07:39:52 -03001350 goto fail_core;
1351 }
1352 vc->priv = (void *)cc;
1353 }
1354
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001355 for (i = 0; i < CX8800_VID_CTLS; i++) {
1356 const struct cx88_ctrl *cc = &cx8800_vid_ctls[i];
1357 struct v4l2_ctrl *vc;
1358
1359 vc = v4l2_ctrl_new_std(&core->video_hdl, &cx8800_ctrl_vid_ops,
1360 cc->id, cc->minimum, cc->maximum, cc->step, cc->default_value);
1361 if (vc == NULL) {
1362 err = core->video_hdl.error;
1363 goto fail_core;
1364 }
1365 vc->priv = (void *)cc;
1366 if (vc->id == V4L2_CID_CHROMA_AGC)
1367 core->chroma_agc = vc;
1368 }
Hans Verkuil34a6b7d2012-09-14 07:15:03 -03001369 v4l2_ctrl_add_handler(&core->video_hdl, &core->audio_hdl, NULL);
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 /* load and configure helper modules */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001372
Hans Verkuilfacd2362013-05-26 07:56:06 -03001373 if (core->board.audio_chip == CX88_AUDIO_WM8775) {
Lawrence Rust69518032011-02-06 17:46:12 -03001374 struct i2c_board_info wm8775_info = {
1375 .type = "wm8775",
1376 .addr = 0x36 >> 1,
1377 .platform_data = &core->wm8775_data,
1378 };
1379 struct v4l2_subdev *sd;
1380
1381 if (core->boardnr == CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1)
1382 core->wm8775_data.is_nova_s = true;
1383 else
1384 core->wm8775_data.is_nova_s = false;
1385
1386 sd = v4l2_i2c_new_subdev_board(&core->v4l2_dev, &core->i2c_adap,
1387 &wm8775_info, NULL);
Hans Verkuilbac63982012-06-10 07:39:52 -03001388 if (sd != NULL) {
1389 core->sd_wm8775 = sd;
Lawrence Rust69518032011-02-06 17:46:12 -03001390 sd->grp_id = WM8775_GID;
Hans Verkuilbac63982012-06-10 07:39:52 -03001391 }
Lawrence Rust69518032011-02-06 17:46:12 -03001392 }
Hans Verkuilb8341e12009-03-29 08:26:01 -03001393
Hans Verkuilfacd2362013-05-26 07:56:06 -03001394 if (core->board.audio_chip == CX88_AUDIO_TVAUDIO) {
Hans Verkuilb8341e12009-03-29 08:26:01 -03001395 /* This probes for a tda9874 as is used on some
1396 Pixelview Ultra boards. */
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -03001397 v4l2_i2c_new_subdev(&core->v4l2_dev, &core->i2c_adap,
1398 "tvaudio", 0, I2C_ADDRS(0xb0 >> 1));
Hans Verkuilb8341e12009-03-29 08:26:01 -03001399 }
Steven Toth30579062006-09-25 12:43:42 -03001400
Michael Krufky6fcecce2007-08-24 01:32:31 -03001401 switch (core->boardnr) {
1402 case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
Hans Verkuilb8341e12009-03-29 08:26:01 -03001403 case CX88_BOARD_DVICO_FUSIONHDTV_7_GOLD: {
lawrence rust2e4e98e2010-08-25 09:50:20 -03001404 static const struct i2c_board_info rtc_info = {
Hans Verkuilb8341e12009-03-29 08:26:01 -03001405 I2C_BOARD_INFO("isl1208", 0x6f)
1406 };
1407
Michael Krufky6fcecce2007-08-24 01:32:31 -03001408 request_module("rtc-isl1208");
Hans Verkuilb8341e12009-03-29 08:26:01 -03001409 core->i2c_rtc = i2c_new_device(&core->i2c_adap, &rtc_info);
1410 }
Michael Krufky8efd2e22008-04-22 14:45:14 -03001411 /* break intentionally omitted */
1412 case CX88_BOARD_DVICO_FUSIONHDTV_5_PCI_NANO:
1413 request_module("ir-kbd-i2c");
Michael Krufky6fcecce2007-08-24 01:32:31 -03001414 }
1415
Mauro Carvalho Chehab121ec132011-02-14 07:01:51 -03001416 /* Sets device info at pci_dev */
1417 pci_set_drvdata(pci_dev, dev);
1418
Hans Verkuilccd6f1d2014-09-20 09:23:44 -03001419 dev->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
Hans Verkuilc5a86142012-05-11 10:45:18 -03001420
Hans Verkuil078859a2014-08-29 06:08:07 -03001421 /* Maintain a reference so cx88-blackbird can query the 8800 device. */
1422 core->v4ldev = dev;
1423
Mauro Carvalho Chehab121ec132011-02-14 07:01:51 -03001424 /* initial device configuration */
1425 mutex_lock(&core->lock);
1426 cx88_set_tvnorm(core, core->tvnorm);
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001427 v4l2_ctrl_handler_setup(&core->video_hdl);
1428 v4l2_ctrl_handler_setup(&core->audio_hdl);
Mauro Carvalho Chehab121ec132011-02-14 07:01:51 -03001429 cx88_video_mux(core, 0);
1430
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001431 q = &dev->vb2_vidq;
1432 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1433 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1434 q->gfp_flags = GFP_DMA32;
1435 q->min_buffers_needed = 2;
1436 q->drv_priv = dev;
1437 q->buf_struct_size = sizeof(struct cx88_buffer);
1438 q->ops = &cx8800_video_qops;
1439 q->mem_ops = &vb2_dma_sg_memops;
1440 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1441 q->lock = &core->lock;
Hans Verkuil2bc46b32016-02-15 12:37:15 -02001442 q->dev = &dev->pci->dev;
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001443
1444 err = vb2_queue_init(q);
1445 if (err < 0)
1446 goto fail_unreg;
1447
1448 q = &dev->vb2_vbiq;
1449 q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1450 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1451 q->gfp_flags = GFP_DMA32;
1452 q->min_buffers_needed = 2;
1453 q->drv_priv = dev;
1454 q->buf_struct_size = sizeof(struct cx88_buffer);
1455 q->ops = &cx8800_vbi_qops;
1456 q->mem_ops = &vb2_dma_sg_memops;
1457 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1458 q->lock = &core->lock;
Hans Verkuil2bc46b32016-02-15 12:37:15 -02001459 q->dev = &dev->pci->dev;
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001460
1461 err = vb2_queue_init(q);
1462 if (err < 0)
1463 goto fail_unreg;
1464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 /* register v4l devices */
Hans Verkuil34080bc2015-03-09 13:34:00 -03001466 cx88_vdev_init(core, dev->pci, &dev->video_dev,
1467 &cx8800_video_template, "video");
1468 video_set_drvdata(&dev->video_dev, dev);
1469 dev->video_dev.ctrl_handler = &core->video_hdl;
1470 dev->video_dev.queue = &dev->vb2_vidq;
1471 err = video_register_device(&dev->video_dev, VFL_TYPE_GRABBER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 video_nr[core->nr]);
1473 if (err < 0) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001474 pr_err("can't register video device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 goto fail_unreg;
1476 }
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001477 pr_info("registered device %s [v4l2]\n",
1478 video_device_node_name(&dev->video_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Hans Verkuil34080bc2015-03-09 13:34:00 -03001480 cx88_vdev_init(core, dev->pci, &dev->vbi_dev,
1481 &cx8800_vbi_template, "vbi");
1482 video_set_drvdata(&dev->vbi_dev, dev);
1483 dev->vbi_dev.queue = &dev->vb2_vbiq;
1484 err = video_register_device(&dev->vbi_dev, VFL_TYPE_VBI,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 vbi_nr[core->nr]);
1486 if (err < 0) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001487 pr_err("can't register vbi device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 goto fail_unreg;
1489 }
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001490 pr_info("registered device %s\n",
1491 video_device_node_name(&dev->vbi_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Trent Piepho6a59d642007-08-15 14:41:57 -03001493 if (core->board.radio.type == CX88_RADIO) {
Hans Verkuil34080bc2015-03-09 13:34:00 -03001494 cx88_vdev_init(core, dev->pci, &dev->radio_dev,
1495 &cx8800_radio_template, "radio");
1496 video_set_drvdata(&dev->radio_dev, dev);
1497 dev->radio_dev.ctrl_handler = &core->audio_hdl;
1498 err = video_register_device(&dev->radio_dev, VFL_TYPE_RADIO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 radio_nr[core->nr]);
1500 if (err < 0) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001501 pr_err("can't register radio device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 goto fail_unreg;
1503 }
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001504 pr_info("registered device %s\n",
1505 video_device_node_name(&dev->radio_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 }
1507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 /* start tvaudio thread */
Hans Verkuilc39ba3302014-08-29 07:45:00 -03001509 if (core->board.tuner_type != UNSET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
Cyrill Gorcunov32b78de2007-07-19 11:44:11 -03001511 if (IS_ERR(core->kthread)) {
1512 err = PTR_ERR(core->kthread);
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001513 pr_err("failed to create cx88 audio thread, err=%d\n",
1514 err);
Cyrill Gorcunov32b78de2007-07-19 11:44:11 -03001515 }
1516 }
Mauro Carvalho Chehab121ec132011-02-14 07:01:51 -03001517 mutex_unlock(&core->lock);
1518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return 0;
1520
1521fail_unreg:
1522 cx8800_unregister_video(dev);
1523 free_irq(pci_dev->irq, dev);
Mauro Carvalho Chehab121ec132011-02-14 07:01:51 -03001524 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525fail_core:
Hans Verkuil078859a2014-08-29 06:08:07 -03001526 core->v4ldev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 cx88_core_put(core,dev->pci);
1528fail_free:
1529 kfree(dev);
1530 return err;
1531}
1532
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001533static void cx8800_finidev(struct pci_dev *pci_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001535 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001536 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
1538 /* stop thread */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001539 if (core->kthread) {
1540 kthread_stop(core->kthread);
1541 core->kthread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 }
1543
Marton Balintb12203d2008-03-26 02:07:35 -03001544 if (core->ir)
Mauro Carvalho Chehab92f4fc12010-03-31 16:07:49 -03001545 cx88_ir_stop(core);
Marton Balintb12203d2008-03-26 02:07:35 -03001546
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001547 cx88_shutdown(core); /* FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
1549 /* unregister stuff */
1550
1551 free_irq(pci_dev->irq, dev);
1552 cx8800_unregister_video(dev);
Hans Verkuil98822de2014-09-05 09:37:09 -03001553 pci_disable_device(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Hans Verkuil078859a2014-08-29 06:08:07 -03001555 core->v4ldev = NULL;
1556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 /* free memory */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001558 cx88_core_put(core,dev->pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 kfree(dev);
1560}
1561
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03001562#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
1564{
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07001565 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 struct cx88_core *core = dev->core;
Alexey Khoroshilov5ddfbbb2013-04-13 18:52:04 -03001567 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
1569 /* stop video+vbi capture */
Alexey Khoroshilov5ddfbbb2013-04-13 18:52:04 -03001570 spin_lock_irqsave(&dev->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 if (!list_empty(&dev->vidq.active)) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001572 pr_info("suspend video\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 stop_video_dma(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 }
1575 if (!list_empty(&dev->vbiq.active)) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001576 pr_info("suspend vbi\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 cx8800_stop_vbi_dma(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 }
Alexey Khoroshilov5ddfbbb2013-04-13 18:52:04 -03001579 spin_unlock_irqrestore(&dev->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Mauro Carvalho Chehab13595a52007-10-01 08:51:39 -03001581 if (core->ir)
Mauro Carvalho Chehab92f4fc12010-03-31 16:07:49 -03001582 cx88_ir_stop(core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 /* FIXME -- shutdown device */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001584 cx88_shutdown(core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 pci_save_state(pci_dev);
1587 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
1588 pci_disable_device(pci_dev);
1589 dev->state.disabled = 1;
1590 }
1591 return 0;
1592}
1593
1594static int cx8800_resume(struct pci_dev *pci_dev)
1595{
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07001596 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 struct cx88_core *core = dev->core;
Alexey Khoroshilov5ddfbbb2013-04-13 18:52:04 -03001598 unsigned long flags;
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001599 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 if (dev->state.disabled) {
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001602 err=pci_enable_device(pci_dev);
1603 if (err) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001604 pr_err("can't enable device\n");
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001605 return err;
1606 }
1607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 dev->state.disabled = 0;
1609 }
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001610 err= pci_set_power_state(pci_dev, PCI_D0);
1611 if (err) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001612 pr_err("can't set power state\n");
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001613 pci_disable_device(pci_dev);
1614 dev->state.disabled = 1;
1615
1616 return err;
1617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 pci_restore_state(pci_dev);
1619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 /* FIXME: re-initialize hardware */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001621 cx88_reset(core);
Mauro Carvalho Chehab13595a52007-10-01 08:51:39 -03001622 if (core->ir)
Mauro Carvalho Chehab92f4fc12010-03-31 16:07:49 -03001623 cx88_ir_start(core);
Mauro Carvalho Chehab13595a52007-10-01 08:51:39 -03001624
1625 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
1627 /* restart video+vbi capture */
Alexey Khoroshilov5ddfbbb2013-04-13 18:52:04 -03001628 spin_lock_irqsave(&dev->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 if (!list_empty(&dev->vidq.active)) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001630 pr_info("resume video\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 restart_video_queue(dev,&dev->vidq);
1632 }
1633 if (!list_empty(&dev->vbiq.active)) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001634 pr_info("resume vbi\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1636 }
Alexey Khoroshilov5ddfbbb2013-04-13 18:52:04 -03001637 spin_unlock_irqrestore(&dev->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
1639 return 0;
1640}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03001641#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
1643/* ----------------------------------------------------------- */
1644
lawrence rust2e4e98e2010-08-25 09:50:20 -03001645static const struct pci_device_id cx8800_pci_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 {
1647 .vendor = 0x14f1,
1648 .device = 0x8800,
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07001649 .subvendor = PCI_ANY_ID,
1650 .subdevice = PCI_ANY_ID,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 },{
1652 /* --- end of list --- */
1653 }
1654};
1655MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
1656
1657static struct pci_driver cx8800_pci_driver = {
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07001658 .name = "cx8800",
1659 .id_table = cx8800_pci_tbl,
1660 .probe = cx8800_initdev,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001661 .remove = cx8800_finidev,
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03001662#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 .suspend = cx8800_suspend,
1664 .resume = cx8800_resume,
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03001665#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666};
1667
Sachin Kamat06333e02013-09-20 05:32:07 -03001668module_pci_driver(cx8800_pci_driver);