blob: cebb07d87617290d7fb03eb2ddf9a72c52525238 [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
28#include <linux/init.h>
29#include <linux/list.h>
30#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/kmod.h>
32#include <linux/kernel.h>
33#include <linux/slab.h>
34#include <linux/interrupt.h>
Andrew Mortonc24228d2007-05-06 14:51:55 -070035#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/delay.h>
37#include <linux/kthread.h>
38#include <asm/div64.h>
39
40#include "cx88.h"
Michael Krufky5e453dc2006-01-09 15:32:31 -020041#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030042#include <media/v4l2-ioctl.h>
Hans Verkuil1a3c60a2012-05-11 11:25:03 -030043#include <media/v4l2-event.h>
Lawrence Rust69518032011-02-06 17:46:12 -030044#include <media/wm8775.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
47MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
48MODULE_LICENSE("GPL");
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030049MODULE_VERSION(CX88_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/* ------------------------------------------------------------------ */
52
53static unsigned int video_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
54static unsigned int vbi_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
55static unsigned int radio_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
56
57module_param_array(video_nr, int, NULL, 0444);
58module_param_array(vbi_nr, int, NULL, 0444);
59module_param_array(radio_nr, int, NULL, 0444);
60
61MODULE_PARM_DESC(video_nr,"video device numbers");
62MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
63MODULE_PARM_DESC(radio_nr,"radio device numbers");
64
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030065static unsigned int video_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066module_param(video_debug,int,0644);
67MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
68
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030069static unsigned int irq_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070module_param(irq_debug,int,0644);
71MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#define dprintk(level,fmt, arg...) if (video_debug >= level) \
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -070074 printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/* ------------------------------------------------------------------- */
77/* static data */
78
lawrence rust2e4e98e2010-08-25 09:50:20 -030079static const struct cx8800_fmt formats[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 {
81 .name = "8 bpp, gray",
82 .fourcc = V4L2_PIX_FMT_GREY,
83 .cxformat = ColorFormatY8,
84 .depth = 8,
85 .flags = FORMAT_FLAGS_PACKED,
86 },{
87 .name = "15 bpp RGB, le",
88 .fourcc = V4L2_PIX_FMT_RGB555,
89 .cxformat = ColorFormatRGB15,
90 .depth = 16,
91 .flags = FORMAT_FLAGS_PACKED,
92 },{
93 .name = "15 bpp RGB, be",
94 .fourcc = V4L2_PIX_FMT_RGB555X,
95 .cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
96 .depth = 16,
97 .flags = FORMAT_FLAGS_PACKED,
98 },{
99 .name = "16 bpp RGB, le",
100 .fourcc = V4L2_PIX_FMT_RGB565,
101 .cxformat = ColorFormatRGB16,
102 .depth = 16,
103 .flags = FORMAT_FLAGS_PACKED,
104 },{
105 .name = "16 bpp RGB, be",
106 .fourcc = V4L2_PIX_FMT_RGB565X,
107 .cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
108 .depth = 16,
109 .flags = FORMAT_FLAGS_PACKED,
110 },{
111 .name = "24 bpp RGB, le",
112 .fourcc = V4L2_PIX_FMT_BGR24,
113 .cxformat = ColorFormatRGB24,
114 .depth = 24,
115 .flags = FORMAT_FLAGS_PACKED,
116 },{
117 .name = "32 bpp RGB, le",
118 .fourcc = V4L2_PIX_FMT_BGR32,
119 .cxformat = ColorFormatRGB32,
120 .depth = 32,
121 .flags = FORMAT_FLAGS_PACKED,
122 },{
123 .name = "32 bpp RGB, be",
124 .fourcc = V4L2_PIX_FMT_RGB32,
125 .cxformat = ColorFormatRGB32 | ColorFormatBSWAP | ColorFormatWSWAP,
126 .depth = 32,
127 .flags = FORMAT_FLAGS_PACKED,
128 },{
129 .name = "4:2:2, packed, YUYV",
130 .fourcc = V4L2_PIX_FMT_YUYV,
131 .cxformat = ColorFormatYUY2,
132 .depth = 16,
133 .flags = FORMAT_FLAGS_PACKED,
134 },{
135 .name = "4:2:2, packed, UYVY",
136 .fourcc = V4L2_PIX_FMT_UYVY,
137 .cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
138 .depth = 16,
139 .flags = FORMAT_FLAGS_PACKED,
140 },
141};
142
lawrence rust2e4e98e2010-08-25 09:50:20 -0300143static const struct cx8800_fmt* format_by_fourcc(unsigned int fourcc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 unsigned int i;
146
147 for (i = 0; i < ARRAY_SIZE(formats); i++)
148 if (formats[i].fourcc == fourcc)
149 return formats+i;
150 return NULL;
151}
152
153/* ------------------------------------------------------------------- */
154
Hans Verkuilbac63982012-06-10 07:39:52 -0300155struct cx88_ctrl {
156 /* control information */
157 u32 id;
158 s32 minimum;
159 s32 maximum;
160 u32 step;
161 s32 default_value;
162
163 /* control register information */
164 u32 off;
165 u32 reg;
166 u32 sreg;
167 u32 mask;
168 u32 shift;
169};
170
Hans Verkuil8c7cb122012-05-11 09:07:45 -0300171static const struct cx88_ctrl cx8800_vid_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 /* --- video --- */
173 {
Hans Verkuilbac63982012-06-10 07:39:52 -0300174 .id = V4L2_CID_BRIGHTNESS,
175 .minimum = 0x00,
176 .maximum = 0xff,
177 .step = 1,
178 .default_value = 0x7f,
179 .off = 128,
180 .reg = MO_CONTR_BRIGHT,
181 .mask = 0x00ff,
182 .shift = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 },{
Hans Verkuilbac63982012-06-10 07:39:52 -0300184 .id = V4L2_CID_CONTRAST,
185 .minimum = 0,
186 .maximum = 0xff,
187 .step = 1,
188 .default_value = 0x3f,
189 .off = 0,
190 .reg = MO_CONTR_BRIGHT,
191 .mask = 0xff00,
192 .shift = 8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 },{
Hans Verkuilbac63982012-06-10 07:39:52 -0300194 .id = V4L2_CID_HUE,
195 .minimum = 0,
196 .maximum = 0xff,
197 .step = 1,
198 .default_value = 0x7f,
199 .off = 128,
200 .reg = MO_HUE,
201 .mask = 0x00ff,
202 .shift = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 },{
204 /* strictly, this only describes only U saturation.
205 * V saturation is handled specially through code.
206 */
Hans Verkuilbac63982012-06-10 07:39:52 -0300207 .id = V4L2_CID_SATURATION,
208 .minimum = 0,
209 .maximum = 0xff,
210 .step = 1,
211 .default_value = 0x7f,
212 .off = 0,
213 .reg = MO_UV_SATURATION,
214 .mask = 0x00ff,
215 .shift = 0,
istvan_v@mailbox.hueea16e32011-07-11 11:02:19 -0300216 }, {
Hans Verkuilbac63982012-06-10 07:39:52 -0300217 .id = V4L2_CID_SHARPNESS,
218 .minimum = 0,
219 .maximum = 4,
220 .step = 1,
221 .default_value = 0x0,
222 .off = 0,
istvan_v@mailbox.hueea16e32011-07-11 11:02:19 -0300223 /* NOTE: the value is converted and written to both even
224 and odd registers in the code */
Hans Verkuilbac63982012-06-10 07:39:52 -0300225 .reg = MO_FILTER_ODD,
226 .mask = 7 << 7,
227 .shift = 7,
istvan_v@mailbox.hueea16e32011-07-11 11:02:19 -0300228 }, {
Hans Verkuilbac63982012-06-10 07:39:52 -0300229 .id = V4L2_CID_CHROMA_AGC,
230 .minimum = 0,
231 .maximum = 1,
232 .default_value = 0x1,
233 .reg = MO_INPUT_FORMAT,
234 .mask = 1 << 10,
235 .shift = 10,
Frej Drejhammar6d042032008-03-23 22:43:21 -0300236 }, {
Hans Verkuilbac63982012-06-10 07:39:52 -0300237 .id = V4L2_CID_COLOR_KILLER,
238 .minimum = 0,
239 .maximum = 1,
240 .default_value = 0x1,
241 .reg = MO_INPUT_FORMAT,
242 .mask = 1 << 9,
243 .shift = 9,
Frej Drejhammar1b879c42008-03-23 22:43:24 -0300244 }, {
Hans Verkuilbac63982012-06-10 07:39:52 -0300245 .id = V4L2_CID_BAND_STOP_FILTER,
246 .minimum = 0,
247 .maximum = 1,
248 .step = 1,
249 .default_value = 0x0,
250 .off = 0,
251 .reg = MO_HTOTAL,
252 .mask = 3 << 11,
253 .shift = 11,
Hans Verkuil8c7cb122012-05-11 09:07:45 -0300254 }
255};
256
257static const struct cx88_ctrl cx8800_aud_ctls[] = {
258 {
Hans Verkuilbac63982012-06-10 07:39:52 -0300259 /* --- audio --- */
260 .id = V4L2_CID_AUDIO_MUTE,
261 .minimum = 0,
262 .maximum = 1,
263 .default_value = 1,
264 .reg = AUD_VOL_CTL,
265 .sreg = SHADOW_AUD_VOL_CTL,
266 .mask = (1 << 6),
267 .shift = 6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 },{
Hans Verkuilbac63982012-06-10 07:39:52 -0300269 .id = V4L2_CID_AUDIO_VOLUME,
270 .minimum = 0,
271 .maximum = 0x3f,
272 .step = 1,
273 .default_value = 0x3f,
274 .reg = AUD_VOL_CTL,
275 .sreg = SHADOW_AUD_VOL_CTL,
276 .mask = 0x3f,
277 .shift = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 },{
Hans Verkuilbac63982012-06-10 07:39:52 -0300279 .id = V4L2_CID_AUDIO_BALANCE,
280 .minimum = 0,
281 .maximum = 0x7f,
282 .step = 1,
283 .default_value = 0x40,
284 .reg = AUD_BAL_CTL,
285 .sreg = SHADOW_AUD_BAL_CTL,
286 .mask = 0x7f,
287 .shift = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
289};
Hans Verkuilbac63982012-06-10 07:39:52 -0300290
Hans Verkuil8c7cb122012-05-11 09:07:45 -0300291enum {
292 CX8800_VID_CTLS = ARRAY_SIZE(cx8800_vid_ctls),
293 CX8800_AUD_CTLS = ARRAY_SIZE(cx8800_aud_ctls),
294};
Michael Krufky38a27132006-06-26 23:42:39 -0300295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296/* ------------------------------------------------------------------ */
297
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -0300298int cx88_video_mux(struct cx88_core *core, unsigned int input)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700300 /* struct cx88_core *core = dev->core; */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
Trent Piepho6a59d642007-08-15 14:41:57 -0300303 input, INPUT(input).vmux,
304 INPUT(input).gpio0,INPUT(input).gpio1,
305 INPUT(input).gpio2,INPUT(input).gpio3);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700306 core->input = input;
Trent Piepho6a59d642007-08-15 14:41:57 -0300307 cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input).vmux << 14);
308 cx_write(MO_GP3_IO, INPUT(input).gpio3);
309 cx_write(MO_GP0_IO, INPUT(input).gpio0);
310 cx_write(MO_GP1_IO, INPUT(input).gpio1);
311 cx_write(MO_GP2_IO, INPUT(input).gpio2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Trent Piepho6a59d642007-08-15 14:41:57 -0300313 switch (INPUT(input).type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 case CX88_VMUX_SVIDEO:
315 cx_set(MO_AFECFG_IO, 0x00000001);
316 cx_set(MO_INPUT_FORMAT, 0x00010010);
317 cx_set(MO_FILTER_EVEN, 0x00002020);
318 cx_set(MO_FILTER_ODD, 0x00002020);
319 break;
320 default:
321 cx_clear(MO_AFECFG_IO, 0x00000001);
322 cx_clear(MO_INPUT_FORMAT, 0x00010010);
323 cx_clear(MO_FILTER_EVEN, 0x00002020);
324 cx_clear(MO_FILTER_ODD, 0x00002020);
325 break;
326 }
Michael Krufkyf24546a2006-10-16 16:51:11 -0300327
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300328 /* if there are audioroutes defined, we have an external
329 ADC to deal with audio */
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300330 if (INPUT(input).audioroute) {
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300331 /* The wm8775 module has the "2" route hardwired into
332 the initialization. Some boards may use different
333 routes for different inputs. HVR-1300 surely does */
Hans Verkuil609c4c12013-05-29 10:44:22 -0300334 if (core->sd_wm8775) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300335 call_all(core, audio, s_routing,
istvan_v@mailbox.hu6e1f4df2010-03-27 09:31:32 -0300336 INPUT(input).audioroute, 0, 0);
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300337 }
Darron Broad430189d2008-10-15 14:18:42 -0300338 /* cx2388's C-ADC is connected to the tuner only.
339 When used with S-Video, that ADC is busy dealing with
340 chroma, so an external must be used for baseband audio */
istvan_v@mailbox.hu6e1f4df2010-03-27 09:31:32 -0300341 if (INPUT(input).type != CX88_VMUX_TELEVISION &&
342 INPUT(input).type != CX88_VMUX_CABLE) {
Darron Broad430189d2008-10-15 14:18:42 -0300343 /* "I2S ADC mode" */
344 core->tvaudio = WW_I2SADC;
345 cx88_set_tvaudio(core);
346 } else {
347 /* Normal mode */
348 cx_write(AUD_I2SCNTL, 0x0);
349 cx_clear(AUD_CTL, EN_I2SIN_ENABLE);
350 }
Michael Krufkyf24546a2006-10-16 16:51:11 -0300351 }
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return 0;
354}
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -0300355EXPORT_SYMBOL(cx88_video_mux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357/* ------------------------------------------------------------------ */
358
359static int start_video_dma(struct cx8800_dev *dev,
360 struct cx88_dmaqueue *q,
361 struct cx88_buffer *buf)
362{
363 struct cx88_core *core = dev->core;
364
365 /* setup fifo + format */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700366 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 buf->bpl, buf->risc.dma);
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300368 cx88_set_scale(core, core->width, core->height, core->field);
Hans Verkuil637bc202014-08-29 03:46:05 -0300369 cx_write(MO_COLOR_CTRL, dev->fmt->cxformat | ColorFormatGamma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 /* reset counter */
372 cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
Hans Verkuil94506842015-04-03 07:22:40 -0300373 q->count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 /* enable irqs */
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300376 cx_set(MO_PCI_INTMSK, core->pci_irqmask | PCI_INT_VIDINT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700377
378 /* Enables corresponding bits at PCI_INT_STAT:
379 bits 0 to 4: video, audio, transport stream, VIP, Host
380 bit 7: timer
381 bits 8 and 9: DMA complete for: SRC, DST
382 bits 10 and 11: BERR signal asserted for RISC: RD, WR
383 bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
384 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 cx_set(MO_VID_INTMSK, 0x0f0011);
386
387 /* enable capture */
388 cx_set(VID_CAPTURE_CONTROL,0x06);
389
390 /* start dma */
391 cx_set(MO_DEV_CNTRL2, (1<<5));
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700392 cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 return 0;
395}
396
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -0300397#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398static int stop_video_dma(struct cx8800_dev *dev)
399{
400 struct cx88_core *core = dev->core;
401
402 /* stop dma */
403 cx_clear(MO_VID_DMACNTRL, 0x11);
404
405 /* disable capture */
406 cx_clear(VID_CAPTURE_CONTROL,0x06);
407
408 /* disable irqs */
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300409 cx_clear(MO_PCI_INTMSK, PCI_INT_VIDINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 cx_clear(MO_VID_INTMSK, 0x0f0011);
411 return 0;
412}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -0300413#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415static int restart_video_queue(struct cx8800_dev *dev,
416 struct cx88_dmaqueue *q)
417{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700418 struct cx88_core *core = dev->core;
Hans Verkuil6f11adc2014-08-10 11:56:14 -0300419 struct cx88_buffer *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 if (!list_empty(&q->active)) {
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300422 buf = list_entry(q->active.next, struct cx88_buffer, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300424 buf, buf->vb.v4l2_buf.index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 start_video_dma(dev, q, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
Hans Verkuil6f11adc2014-08-10 11:56:14 -0300427 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
430/* ------------------------------------------------------------------ */
431
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300432static int queue_setup(struct vb2_queue *q, const struct v4l2_format *fmt,
433 unsigned int *num_buffers, unsigned int *num_planes,
434 unsigned int sizes[], void *alloc_ctxs[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300436 struct cx8800_dev *dev = q->drv_priv;
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300437 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300439 *num_planes = 1;
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300440 sizes[0] = (dev->fmt->depth * core->width * core->height) >> 3;
Hans Verkuil165d0042014-12-08 13:23:49 -0300441 alloc_ctxs[0] = dev->alloc_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return 0;
443}
444
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300445static int buffer_prepare(struct vb2_buffer *vb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300447 struct cx8800_dev *dev = vb->vb2_queue->drv_priv;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700448 struct cx88_core *core = dev->core;
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300449 struct cx88_buffer *buf = container_of(vb, struct cx88_buffer, vb);
450 struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300452 buf->bpl = core->width * dev->fmt->depth >> 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300454 if (vb2_plane_size(vb, 0) < core->height * buf->bpl)
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300455 return -EINVAL;
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300456 vb2_set_plane_payload(vb, 0, core->height * buf->bpl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300458 switch (core->field) {
Hans Verkuil637bc202014-08-29 03:46:05 -0300459 case V4L2_FIELD_TOP:
460 cx88_risc_buffer(dev->pci, &buf->risc,
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300461 sgt->sgl, 0, UNSET,
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300462 buf->bpl, 0, core->height);
Hans Verkuil637bc202014-08-29 03:46:05 -0300463 break;
464 case V4L2_FIELD_BOTTOM:
465 cx88_risc_buffer(dev->pci, &buf->risc,
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300466 sgt->sgl, UNSET, 0,
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300467 buf->bpl, 0, core->height);
Hans Verkuil637bc202014-08-29 03:46:05 -0300468 break;
469 case V4L2_FIELD_SEQ_TB:
470 cx88_risc_buffer(dev->pci, &buf->risc,
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300471 sgt->sgl,
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300472 0, buf->bpl * (core->height >> 1),
Hans Verkuil637bc202014-08-29 03:46:05 -0300473 buf->bpl, 0,
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300474 core->height >> 1);
Hans Verkuil637bc202014-08-29 03:46:05 -0300475 break;
476 case V4L2_FIELD_SEQ_BT:
477 cx88_risc_buffer(dev->pci, &buf->risc,
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300478 sgt->sgl,
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300479 buf->bpl * (core->height >> 1), 0,
Hans Verkuil637bc202014-08-29 03:46:05 -0300480 buf->bpl, 0,
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300481 core->height >> 1);
Hans Verkuil637bc202014-08-29 03:46:05 -0300482 break;
483 case V4L2_FIELD_INTERLACED:
484 default:
485 cx88_risc_buffer(dev->pci, &buf->risc,
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300486 sgt->sgl, 0, buf->bpl,
Hans Verkuil637bc202014-08-29 03:46:05 -0300487 buf->bpl, buf->bpl,
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300488 core->height >> 1);
Hans Verkuil637bc202014-08-29 03:46:05 -0300489 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
491 dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300492 buf, buf->vb.v4l2_buf.index,
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300493 core->width, core->height, dev->fmt->depth, dev->fmt->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 (unsigned long)buf->risc.dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300498static void buffer_finish(struct vb2_buffer *vb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300500 struct cx8800_dev *dev = vb->vb2_queue->drv_priv;
501 struct cx88_buffer *buf = container_of(vb, struct cx88_buffer, vb);
Hans Verkuil5e7045e2014-08-29 04:11:54 -0300502 struct cx88_riscmem *risc = &buf->risc;
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300503
Hans Verkuil5e7045e2014-08-29 04:11:54 -0300504 if (risc->cpu)
505 pci_free_consistent(dev->pci, risc->size, risc->cpu, risc->dma);
506 memset(risc, 0, sizeof(*risc));
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300507}
508
509static void buffer_queue(struct vb2_buffer *vb)
510{
511 struct cx8800_dev *dev = vb->vb2_queue->drv_priv;
512 struct cx88_buffer *buf = container_of(vb, struct cx88_buffer, vb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 struct cx88_buffer *prev;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700514 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 struct cx88_dmaqueue *q = &dev->vidq;
516
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300517 /* add jump to start */
518 buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 8);
519 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
520 buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Hans Verkuil6f11adc2014-08-10 11:56:14 -0300522 if (list_empty(&q->active)) {
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300523 list_add_tail(&buf->list, &q->active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 dprintk(2,"[%p/%d] buffer_queue - first active\n",
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300525 buf, buf->vb.v4l2_buf.index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 } else {
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300528 buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
529 prev = list_entry(q->active.prev, struct cx88_buffer, list);
530 list_add_tail(&buf->list, &q->active);
Hans Verkuil637bc202014-08-29 03:46:05 -0300531 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
532 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300533 buf, buf->vb.v4l2_buf.index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
535}
536
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300537static int start_streaming(struct vb2_queue *q, unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300539 struct cx8800_dev *dev = q->drv_priv;
540 struct cx88_dmaqueue *dmaq = &dev->vidq;
541 struct cx88_buffer *buf = list_entry(dmaq->active.next,
542 struct cx88_buffer, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300544 start_video_dma(dev, dmaq, buf);
545 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300548static void stop_streaming(struct vb2_queue *q)
549{
550 struct cx8800_dev *dev = q->drv_priv;
551 struct cx88_core *core = dev->core;
552 struct cx88_dmaqueue *dmaq = &dev->vidq;
553 unsigned long flags;
554
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300555 cx_clear(MO_VID_DMACNTRL, 0x11);
556 cx_clear(VID_CAPTURE_CONTROL, 0x06);
557 spin_lock_irqsave(&dev->slock, flags);
558 while (!list_empty(&dmaq->active)) {
559 struct cx88_buffer *buf = list_entry(dmaq->active.next,
560 struct cx88_buffer, list);
561
562 list_del(&buf->list);
563 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
564 }
565 spin_unlock_irqrestore(&dev->slock, flags);
566}
567
568static struct vb2_ops cx8800_video_qops = {
569 .queue_setup = queue_setup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 .buf_prepare = buffer_prepare,
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300571 .buf_finish = buffer_finish,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 .buf_queue = buffer_queue,
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300573 .wait_prepare = vb2_ops_wait_prepare,
574 .wait_finish = vb2_ops_wait_finish,
575 .start_streaming = start_streaming,
576 .stop_streaming = stop_streaming,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577};
578
579/* ------------------------------------------------------------------ */
580
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300581static int radio_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200583 struct cx8800_dev *dev = video_drvdata(file);
Dan Carpenter5401c2d2010-10-21 16:21:45 -0300584 struct cx88_core *core = dev->core;
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300585 int ret = v4l2_fh_open(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300587 if (ret)
588 return ret;
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200589
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300590 cx_write(MO_GP3_IO, core->board.radio.gpio3);
591 cx_write(MO_GP0_IO, core->board.radio.gpio0);
592 cx_write(MO_GP1_IO, core->board.radio.gpio1);
593 cx_write(MO_GP2_IO, core->board.radio.gpio2);
594 if (core->board.radio.audioroute) {
595 if (core->sd_wm8775) {
596 call_all(core, audio, s_routing,
Hans Verkuil5325b422009-04-02 11:26:22 -0300597 core->board.radio.audioroute, 0, 0);
Darron Broad430189d2008-10-15 14:18:42 -0300598 }
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300599 /* "I2S ADC mode" */
600 core->tvaudio = WW_I2SADC;
601 cx88_set_tvaudio(core);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 } else {
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300603 /* FM Mode */
604 core->tvaudio = WW_FM;
605 cx88_set_tvaudio(core);
606 cx88_set_stereo(core, V4L2_TUNER_MODE_STEREO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300608 call_all(core, tuner, s_radio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return 0;
610}
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612/* ------------------------------------------------------------------ */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300613/* VIDEO CTRL IOCTLS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Hans Verkuil8c7cb122012-05-11 09:07:45 -0300615static int cx8800_s_vid_ctrl(struct v4l2_ctrl *ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Hans Verkuilbac63982012-06-10 07:39:52 -0300617 struct cx88_core *core =
Hans Verkuil8c7cb122012-05-11 09:07:45 -0300618 container_of(ctrl->handler, struct cx88_core, video_hdl);
619 const struct cx88_ctrl *cc = ctrl->priv;
620 u32 value, mask;
621
622 mask = cc->mask;
623 switch (ctrl->id) {
624 case V4L2_CID_SATURATION:
625 /* special v_sat handling */
626
627 value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
628
629 if (core->tvnorm & V4L2_STD_SECAM) {
630 /* For SECAM, both U and V sat should be equal */
631 value = value << 8 | value;
632 } else {
633 /* Keeps U Saturation proportional to V Sat */
634 value = (value * 0x5a) / 0x7f << 8 | value;
635 }
636 mask = 0xffff;
637 break;
638 case V4L2_CID_SHARPNESS:
639 /* 0b000, 0b100, 0b101, 0b110, or 0b111 */
640 value = (ctrl->val < 1 ? 0 : ((ctrl->val + 3) << 7));
641 /* needs to be set for both fields */
642 cx_andor(MO_FILTER_EVEN, mask, value);
643 break;
644 case V4L2_CID_CHROMA_AGC:
645 value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
646 break;
647 default:
648 value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
649 break;
650 }
651 dprintk(1, "set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
652 ctrl->id, ctrl->name, ctrl->val, cc->reg, value,
653 mask, cc->sreg ? " [shadowed]" : "");
654 if (cc->sreg)
655 cx_sandor(cc->sreg, cc->reg, mask, value);
656 else
657 cx_andor(cc->reg, mask, value);
658 return 0;
659}
660
661static int cx8800_s_aud_ctrl(struct v4l2_ctrl *ctrl)
662{
663 struct cx88_core *core =
664 container_of(ctrl->handler, struct cx88_core, audio_hdl);
Hans Verkuilbac63982012-06-10 07:39:52 -0300665 const struct cx88_ctrl *cc = ctrl->priv;
Mauro Carvalho Chehab70f00042006-01-09 15:25:26 -0200666 u32 value,mask;
Lawrence Rust69518032011-02-06 17:46:12 -0300667
668 /* Pass changes onto any WM8775 */
Hans Verkuil609c4c12013-05-29 10:44:22 -0300669 if (core->sd_wm8775) {
Hans Verkuilbac63982012-06-10 07:39:52 -0300670 switch (ctrl->id) {
Lawrence Rust69518032011-02-06 17:46:12 -0300671 case V4L2_CID_AUDIO_MUTE:
Hans Verkuilbac63982012-06-10 07:39:52 -0300672 wm8775_s_ctrl(core, ctrl->id, ctrl->val);
Lawrence Rust69518032011-02-06 17:46:12 -0300673 break;
674 case V4L2_CID_AUDIO_VOLUME:
Hans Verkuilbac63982012-06-10 07:39:52 -0300675 wm8775_s_ctrl(core, ctrl->id, (ctrl->val) ?
676 (0x90 + ctrl->val) << 8 : 0);
Lawrence Rust69518032011-02-06 17:46:12 -0300677 break;
678 case V4L2_CID_AUDIO_BALANCE:
Hans Verkuilbac63982012-06-10 07:39:52 -0300679 wm8775_s_ctrl(core, ctrl->id, ctrl->val << 9);
Lawrence Rust69518032011-02-06 17:46:12 -0300680 break;
681 default:
Lawrence Rust69518032011-02-06 17:46:12 -0300682 break;
683 }
Lawrence Rust69518032011-02-06 17:46:12 -0300684 }
685
Hans Verkuilbac63982012-06-10 07:39:52 -0300686 mask = cc->mask;
687 switch (ctrl->id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 case V4L2_CID_AUDIO_BALANCE:
Hans Verkuilbac63982012-06-10 07:39:52 -0300689 value = (ctrl->val < 0x40) ? (0x7f - ctrl->val) : (ctrl->val - 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 break;
691 case V4L2_CID_AUDIO_VOLUME:
Hans Verkuilbac63982012-06-10 07:39:52 -0300692 value = 0x3f - (ctrl->val & 0x3f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 default:
Hans Verkuilbac63982012-06-10 07:39:52 -0300695 value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 break;
697 }
Ian Pickworth6457af52006-02-27 00:09:45 -0300698 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 -0300699 ctrl->id, ctrl->name, ctrl->val, cc->reg, value,
700 mask, cc->sreg ? " [shadowed]" : "");
701 if (cc->sreg)
702 cx_sandor(cc->sreg, cc->reg, mask, value);
703 else
704 cx_andor(cc->reg, mask, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return 0;
706}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708/* ------------------------------------------------------------------ */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300709/* VIDEO IOCTLS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Hans Verkuil78b526a2008-05-28 12:16:41 -0300711static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300712 struct v4l2_format *f)
713{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300714 struct cx8800_dev *dev = video_drvdata(file);
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300715 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300716
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300717 f->fmt.pix.width = core->width;
718 f->fmt.pix.height = core->height;
719 f->fmt.pix.field = core->field;
Hans Verkuilc5a86142012-05-11 10:45:18 -0300720 f->fmt.pix.pixelformat = dev->fmt->fourcc;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300721 f->fmt.pix.bytesperline =
Hans Verkuilc5a86142012-05-11 10:45:18 -0300722 (f->fmt.pix.width * dev->fmt->depth) >> 3;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300723 f->fmt.pix.sizeimage =
724 f->fmt.pix.height * f->fmt.pix.bytesperline;
Hans Verkuilc5a86142012-05-11 10:45:18 -0300725 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300726 return 0;
727}
728
Hans Verkuil78b526a2008-05-28 12:16:41 -0300729static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 struct v4l2_format *f)
731{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300732 struct cx8800_dev *dev = video_drvdata(file);
733 struct cx88_core *core = dev->core;
lawrence rust2e4e98e2010-08-25 09:50:20 -0300734 const struct cx8800_fmt *fmt;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300735 enum v4l2_field field;
736 unsigned int maxw, maxh;
737
738 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
739 if (NULL == fmt)
740 return -EINVAL;
741
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300742 maxw = norm_maxw(core->tvnorm);
743 maxh = norm_maxh(core->tvnorm);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300744
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300745 field = f->fmt.pix.field;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300746
747 switch (field) {
748 case V4L2_FIELD_TOP:
749 case V4L2_FIELD_BOTTOM:
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300750 case V4L2_FIELD_INTERLACED:
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300751 case V4L2_FIELD_SEQ_BT:
752 case V4L2_FIELD_SEQ_TB:
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300753 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 default:
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300755 field = (f->fmt.pix.height > maxh / 2)
756 ? V4L2_FIELD_INTERLACED
757 : V4L2_FIELD_BOTTOM;
758 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300760 if (V4L2_FIELD_HAS_T_OR_B(field))
761 maxh /= 2;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300762
Trent Piepho4b899452009-05-30 21:45:46 -0300763 v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
764 &f->fmt.pix.height, 32, maxh, 0, 0);
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300765 f->fmt.pix.field = field;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300766 f->fmt.pix.bytesperline =
767 (f->fmt.pix.width * fmt->depth) >> 3;
768 f->fmt.pix.sizeimage =
769 f->fmt.pix.height * f->fmt.pix.bytesperline;
Hans Verkuil94506842015-04-03 07:22:40 -0300770 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300771
772 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
Hans Verkuil78b526a2008-05-28 12:16:41 -0300775static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300776 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300778 struct cx8800_dev *dev = video_drvdata(file);
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300779 struct cx88_core *core = dev->core;
Hans Verkuil78b526a2008-05-28 12:16:41 -0300780 int err = vidioc_try_fmt_vid_cap (file,priv,f);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700781
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300782 if (0 != err)
783 return err;
Hans Verkuil078859a2014-08-29 06:08:07 -0300784 if (vb2_is_busy(&dev->vb2_vidq) || vb2_is_busy(&dev->vb2_vbiq))
785 return -EBUSY;
786 if (core->dvbdev && vb2_is_busy(&core->dvbdev->vb2_mpegq))
787 return -EBUSY;
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300788 dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
789 core->width = f->fmt.pix.width;
790 core->height = f->fmt.pix.height;
791 core->field = f->fmt.pix.field;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300792 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
794
Hans Verkuil902e1972012-05-09 16:23:07 -0300795void cx88_querycap(struct file *file, struct cx88_core *core,
796 struct v4l2_capability *cap)
797{
798 struct video_device *vdev = video_devdata(file);
799
800 strlcpy(cap->card, core->board.name, sizeof(cap->card));
801 cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
802 if (UNSET != core->board.tuner_type)
803 cap->device_caps |= V4L2_CAP_TUNER;
804 switch (vdev->vfl_type) {
805 case VFL_TYPE_RADIO:
806 cap->device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
807 break;
808 case VFL_TYPE_GRABBER:
809 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
810 break;
811 case VFL_TYPE_VBI:
812 cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
813 break;
814 }
815 cap->capabilities = cap->device_caps | V4L2_CAP_VIDEO_CAPTURE |
816 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_DEVICE_CAPS;
817 if (core->board.radio.type == CX88_RADIO)
818 cap->capabilities |= V4L2_CAP_RADIO;
819}
820EXPORT_SYMBOL(cx88_querycap);
821
822static int vidioc_querycap(struct file *file, void *priv,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300823 struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300825 struct cx8800_dev *dev = video_drvdata(file);
826 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300828 strcpy(cap->driver, "cx8800");
Hans Verkuil902e1972012-05-09 16:23:07 -0300829 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
830 cx88_querycap(file, core, cap);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300831 return 0;
832}
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700833
Hans Verkuil78b526a2008-05-28 12:16:41 -0300834static int vidioc_enum_fmt_vid_cap (struct file *file, void *priv,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300835 struct v4l2_fmtdesc *f)
836{
837 if (unlikely(f->index >= ARRAY_SIZE(formats)))
838 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300840 strlcpy(f->description,formats[f->index].name,sizeof(f->description));
841 f->pixelformat = formats[f->index].fourcc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300843 return 0;
844}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Hans Verkuil48d68802012-05-25 12:15:30 -0300846static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorm)
847{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300848 struct cx8800_dev *dev = video_drvdata(file);
849 struct cx88_core *core = dev->core;
Hans Verkuil48d68802012-05-25 12:15:30 -0300850
851 *tvnorm = core->tvnorm;
852 return 0;
853}
854
Hans Verkuil314527a2013-03-15 06:10:40 -0300855static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300856{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300857 struct cx8800_dev *dev = video_drvdata(file);
858 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300859
Hans Verkuil078859a2014-08-29 06:08:07 -0300860 return cx88_set_tvnorm(core, tvnorms);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300861}
862
863/* only one input in this sample driver */
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300864int cx88_enum_input (struct cx88_core *core,struct v4l2_input *i)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300865{
lawrence rust2e4e98e2010-08-25 09:50:20 -0300866 static const char * const iname[] = {
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300867 [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
868 [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
869 [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
870 [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
871 [ CX88_VMUX_SVIDEO ] = "S-Video",
872 [ CX88_VMUX_TELEVISION ] = "Television",
873 [ CX88_VMUX_CABLE ] = "Cable TV",
874 [ CX88_VMUX_DVB ] = "DVB",
875 [ CX88_VMUX_DEBUG ] = "for debug only",
876 };
Trent Piephof3334bc2009-03-04 01:21:03 -0300877 unsigned int n = i->index;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300878
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300879 if (n >= 4)
880 return -EINVAL;
Trent Piepho6a59d642007-08-15 14:41:57 -0300881 if (0 == INPUT(n).type)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300882 return -EINVAL;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300883 i->type = V4L2_INPUT_TYPE_CAMERA;
Trent Piepho6a59d642007-08-15 14:41:57 -0300884 strcpy(i->name,iname[INPUT(n).type]);
885 if ((CX88_VMUX_TELEVISION == INPUT(n).type) ||
Julia Lawall473d8022010-08-05 17:22:44 -0300886 (CX88_VMUX_CABLE == INPUT(n).type)) {
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300887 i->type = V4L2_INPUT_TYPE_TUNER;
Julia Lawall473d8022010-08-05 17:22:44 -0300888 }
Hans Verkuilf33e9862012-05-25 12:04:10 -0300889 i->std = CX88_NORMS;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300890 return 0;
891}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300892EXPORT_SYMBOL(cx88_enum_input);
893
894static int vidioc_enum_input (struct file *file, void *priv,
895 struct v4l2_input *i)
896{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300897 struct cx8800_dev *dev = video_drvdata(file);
898 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300899 return cx88_enum_input (core,i);
900}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300901
902static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
903{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300904 struct cx8800_dev *dev = video_drvdata(file);
905 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300906
907 *i = core->input;
908 return 0;
909}
910
911static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
912{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300913 struct cx8800_dev *dev = video_drvdata(file);
914 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300915
916 if (i >= 4)
917 return -EINVAL;
Hans Verkuilf33e9862012-05-25 12:04:10 -0300918 if (0 == INPUT(i).type)
919 return -EINVAL;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300920
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300921 cx88_newstation(core);
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -0300922 cx88_video_mux(core,i);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300923 return 0;
924}
925
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300926static int vidioc_g_tuner (struct file *file, void *priv,
927 struct v4l2_tuner *t)
928{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300929 struct cx8800_dev *dev = video_drvdata(file);
930 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300931 u32 reg;
932
Trent Piepho6a59d642007-08-15 14:41:57 -0300933 if (unlikely(UNSET == core->board.tuner_type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300934 return -EINVAL;
Mauro Carvalho Chehab243d8c02007-01-20 13:58:36 -0300935 if (0 != t->index)
936 return -EINVAL;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300937
938 strcpy(t->name, "Television");
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300939 t->capability = V4L2_TUNER_CAP_NORM;
940 t->rangehigh = 0xffffffffUL;
Hans Verkuilf33e9862012-05-25 12:04:10 -0300941 call_all(core, tuner, g_tuner, t);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300942
943 cx88_get_stereo(core ,t);
944 reg = cx_read(MO_DEVICE_STATUS);
945 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
946 return 0;
947}
948
949static int vidioc_s_tuner (struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -0300950 const struct v4l2_tuner *t)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300951{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300952 struct cx8800_dev *dev = video_drvdata(file);
953 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300954
Trent Piepho6a59d642007-08-15 14:41:57 -0300955 if (UNSET == core->board.tuner_type)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300956 return -EINVAL;
957 if (0 != t->index)
958 return -EINVAL;
959
960 cx88_set_stereo(core, t->audmode, 1);
961 return 0;
962}
963
964static int vidioc_g_frequency (struct file *file, void *priv,
965 struct v4l2_frequency *f)
966{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300967 struct cx8800_dev *dev = video_drvdata(file);
968 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300969
Trent Piepho6a59d642007-08-15 14:41:57 -0300970 if (unlikely(UNSET == core->board.tuner_type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300971 return -EINVAL;
Hans Verkuilf33e9862012-05-25 12:04:10 -0300972 if (f->tuner)
973 return -EINVAL;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300974
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300975 f->frequency = core->freq;
976
Hans Verkuilb8341e12009-03-29 08:26:01 -0300977 call_all(core, tuner, g_frequency, f);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300978
979 return 0;
980}
981
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300982int cx88_set_freq (struct cx88_core *core,
Hans Verkuilb530a442013-03-19 04:09:26 -0300983 const struct v4l2_frequency *f)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300984{
Hans Verkuilb530a442013-03-19 04:09:26 -0300985 struct v4l2_frequency new_freq = *f;
986
Trent Piepho6a59d642007-08-15 14:41:57 -0300987 if (unlikely(UNSET == core->board.tuner_type))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300988 return -EINVAL;
989 if (unlikely(f->tuner != 0))
990 return -EINVAL;
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300991
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300992 cx88_newstation(core);
Hans Verkuilb8341e12009-03-29 08:26:01 -0300993 call_all(core, tuner, s_frequency, f);
Hans Verkuilb530a442013-03-19 04:09:26 -0300994 call_all(core, tuner, g_frequency, &new_freq);
995 core->freq = new_freq.frequency;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300996
997 /* When changing channels it is required to reset TVAUDIO */
998 msleep (10);
999 cx88_set_tvaudio(core);
1000
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001001 return 0;
1002}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001003EXPORT_SYMBOL(cx88_set_freq);
1004
1005static int vidioc_s_frequency (struct file *file, void *priv,
Hans Verkuilb530a442013-03-19 04:09:26 -03001006 const struct v4l2_frequency *f)
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001007{
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001008 struct cx8800_dev *dev = video_drvdata(file);
1009 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001010
Hans Verkuiledbd1382012-05-11 10:33:25 -03001011 return cx88_set_freq(core, f);
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001012}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001013
Trent Piephodbbff482007-01-22 23:31:53 -03001014#ifdef CONFIG_VIDEO_ADV_DEBUG
1015static int vidioc_g_register (struct file *file, void *fh,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001016 struct v4l2_dbg_register *reg)
Trent Piephodbbff482007-01-22 23:31:53 -03001017{
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001018 struct cx8800_dev *dev = video_drvdata(file);
1019 struct cx88_core *core = dev->core;
Trent Piephodbbff482007-01-22 23:31:53 -03001020
Trent Piephodbbff482007-01-22 23:31:53 -03001021 /* cx2388x has a 24-bit register space */
Hans Verkuil7feeb142013-05-29 06:22:02 -03001022 reg->val = cx_read(reg->reg & 0xfffffc);
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001023 reg->size = 4;
Trent Piephodbbff482007-01-22 23:31:53 -03001024 return 0;
1025}
1026
1027static int vidioc_s_register (struct file *file, void *fh,
Hans Verkuil977ba3b12013-03-24 08:28:46 -03001028 const struct v4l2_dbg_register *reg)
Trent Piephodbbff482007-01-22 23:31:53 -03001029{
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001030 struct cx8800_dev *dev = video_drvdata(file);
1031 struct cx88_core *core = dev->core;
Trent Piephodbbff482007-01-22 23:31:53 -03001032
Hans Verkuil7feeb142013-05-29 06:22:02 -03001033 cx_write(reg->reg & 0xfffffc, reg->val);
Trent Piephodbbff482007-01-22 23:31:53 -03001034 return 0;
1035}
1036#endif
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038/* ----------------------------------------------------------- */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001039/* RADIO ESPECIFIC IOCTLS */
1040/* ----------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001042static int radio_g_tuner (struct file *file, void *priv,
1043 struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001045 struct cx8800_dev *dev = video_drvdata(file);
1046 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001047
1048 if (unlikely(t->index > 0))
1049 return -EINVAL;
1050
1051 strcpy(t->name, "Radio");
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001052
Hans Verkuilb8341e12009-03-29 08:26:01 -03001053 call_all(core, tuner, g_tuner, t);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001054 return 0;
1055}
1056
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001057static int radio_s_tuner (struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -03001058 const struct v4l2_tuner *t)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001059{
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001060 struct cx8800_dev *dev = video_drvdata(file);
1061 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001062
1063 if (0 != t->index)
1064 return -EINVAL;
1065
Hans Verkuilb8341e12009-03-29 08:26:01 -03001066 call_all(core, tuner, s_tuner, t);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001067 return 0;
1068}
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070/* ----------------------------------------------------------- */
1071
lawrence rust2e4e98e2010-08-25 09:50:20 -03001072static const char *cx88_vid_irqs[32] = {
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -07001073 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1074 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1075 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1076 "y_sync", "u_sync", "v_sync", "vbi_sync",
1077 "opc_err", "par_err", "rip_err", "pci_abort",
1078};
1079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080static void cx8800_vid_irq(struct cx8800_dev *dev)
1081{
1082 struct cx88_core *core = dev->core;
1083 u32 status, mask, count;
1084
1085 status = cx_read(MO_VID_INTSTAT);
1086 mask = cx_read(MO_VID_INTMSK);
1087 if (0 == (status & mask))
1088 return;
1089 cx_write(MO_VID_INTSTAT, status);
1090 if (irq_debug || (status & mask & ~0xff))
1091 cx88_print_irqbits(core->name, "irq vid",
Mauro Carvalho Chehab66623a02007-03-29 08:47:04 -03001092 cx88_vid_irqs, ARRAY_SIZE(cx88_vid_irqs),
1093 status, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095 /* risc op code error */
1096 if (status & (1 << 16)) {
1097 printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
1098 cx_clear(MO_VID_DMACNTRL, 0x11);
1099 cx_clear(VID_CAPTURE_CONTROL, 0x06);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001100 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 }
1102
1103 /* risc1 y */
1104 if (status & 0x01) {
1105 spin_lock(&dev->slock);
1106 count = cx_read(MO_VIDY_GPCNT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001107 cx88_wakeup(core, &dev->vidq, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 spin_unlock(&dev->slock);
1109 }
1110
1111 /* risc1 vbi */
1112 if (status & 0x08) {
1113 spin_lock(&dev->slock);
1114 count = cx_read(MO_VBI_GPCNT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001115 cx88_wakeup(core, &dev->vbiq, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 spin_unlock(&dev->slock);
1117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
1119
David Howells7d12e782006-10-05 14:55:46 +01001120static irqreturn_t cx8800_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
1122 struct cx8800_dev *dev = dev_id;
1123 struct cx88_core *core = dev->core;
1124 u32 status;
1125 int loop, handled = 0;
1126
1127 for (loop = 0; loop < 10; loop++) {
Trent Piepho8ddac9e2007-08-18 06:57:55 -03001128 status = cx_read(MO_PCI_INTSTAT) &
1129 (core->pci_irqmask | PCI_INT_VIDINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (0 == status)
1131 goto out;
1132 cx_write(MO_PCI_INTSTAT, status);
1133 handled = 1;
1134
1135 if (status & core->pci_irqmask)
1136 cx88_core_irq(core,status);
Trent Piepho8ddac9e2007-08-18 06:57:55 -03001137 if (status & PCI_INT_VIDINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 cx8800_vid_irq(dev);
Peter Senna Tschudinc2c1b412012-09-28 05:37:22 -03001139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (10 == loop) {
1141 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
1142 core->name);
1143 cx_write(MO_PCI_INTMSK,0);
1144 }
1145
1146 out:
1147 return IRQ_RETVAL(handled);
1148}
1149
1150/* ----------------------------------------------------------- */
1151/* exported stuff */
1152
Hans Verkuilbec43662008-12-30 06:58:20 -03001153static const struct v4l2_file_operations video_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
1155 .owner = THIS_MODULE,
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001156 .open = v4l2_fh_open,
1157 .release = vb2_fop_release,
1158 .read = vb2_fop_read,
1159 .poll = vb2_fop_poll,
1160 .mmap = vb2_fop_mmap,
Mauro Carvalho Chehabb61872642011-02-13 21:52:02 -03001161 .unlocked_ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162};
1163
Hans Verkuila3998102008-07-21 02:57:38 -03001164static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001165 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001166 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1167 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1168 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1169 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001170 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1171 .vidioc_querybuf = vb2_ioctl_querybuf,
1172 .vidioc_qbuf = vb2_ioctl_qbuf,
1173 .vidioc_dqbuf = vb2_ioctl_dqbuf,
Hans Verkuil48d68802012-05-25 12:15:30 -03001174 .vidioc_g_std = vidioc_g_std,
Hans Verkuilf33e9862012-05-25 12:04:10 -03001175 .vidioc_s_std = vidioc_s_std,
1176 .vidioc_enum_input = vidioc_enum_input,
1177 .vidioc_g_input = vidioc_g_input,
1178 .vidioc_s_input = vidioc_s_input,
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001179 .vidioc_streamon = vb2_ioctl_streamon,
1180 .vidioc_streamoff = vb2_ioctl_streamoff,
Hans Verkuilf33e9862012-05-25 12:04:10 -03001181 .vidioc_g_tuner = vidioc_g_tuner,
1182 .vidioc_s_tuner = vidioc_s_tuner,
1183 .vidioc_g_frequency = vidioc_g_frequency,
1184 .vidioc_s_frequency = vidioc_s_frequency,
1185 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1186 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Hans Verkuilf33e9862012-05-25 12:04:10 -03001187#ifdef CONFIG_VIDEO_ADV_DEBUG
1188 .vidioc_g_register = vidioc_g_register,
1189 .vidioc_s_register = vidioc_s_register,
1190#endif
1191};
1192
1193static const struct video_device cx8800_video_template = {
1194 .name = "cx8800-video",
1195 .fops = &video_fops,
1196 .ioctl_ops = &video_ioctl_ops,
1197 .tvnorms = CX88_NORMS,
Hans Verkuilf33e9862012-05-25 12:04:10 -03001198};
1199
1200static const struct v4l2_ioctl_ops vbi_ioctl_ops = {
1201 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001202 .vidioc_g_fmt_vbi_cap = cx8800_vbi_fmt,
1203 .vidioc_try_fmt_vbi_cap = cx8800_vbi_fmt,
1204 .vidioc_s_fmt_vbi_cap = cx8800_vbi_fmt,
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001205 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1206 .vidioc_querybuf = vb2_ioctl_querybuf,
1207 .vidioc_qbuf = vb2_ioctl_qbuf,
1208 .vidioc_dqbuf = vb2_ioctl_dqbuf,
Hans Verkuil48d68802012-05-25 12:15:30 -03001209 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001210 .vidioc_s_std = vidioc_s_std,
1211 .vidioc_enum_input = vidioc_enum_input,
1212 .vidioc_g_input = vidioc_g_input,
1213 .vidioc_s_input = vidioc_s_input,
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001214 .vidioc_streamon = vb2_ioctl_streamon,
1215 .vidioc_streamoff = vb2_ioctl_streamoff,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001216 .vidioc_g_tuner = vidioc_g_tuner,
1217 .vidioc_s_tuner = vidioc_s_tuner,
1218 .vidioc_g_frequency = vidioc_g_frequency,
1219 .vidioc_s_frequency = vidioc_s_frequency,
Trent Piephodbbff482007-01-22 23:31:53 -03001220#ifdef CONFIG_VIDEO_ADV_DEBUG
1221 .vidioc_g_register = vidioc_g_register,
1222 .vidioc_s_register = vidioc_s_register,
1223#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001224};
1225
Hans Verkuilf33e9862012-05-25 12:04:10 -03001226static const struct video_device cx8800_vbi_template = {
1227 .name = "cx8800-vbi",
Hans Verkuila3998102008-07-21 02:57:38 -03001228 .fops = &video_fops,
Hans Verkuilf33e9862012-05-25 12:04:10 -03001229 .ioctl_ops = &vbi_ioctl_ops,
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001230 .tvnorms = CX88_NORMS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231};
1232
Hans Verkuilbec43662008-12-30 06:58:20 -03001233static const struct v4l2_file_operations radio_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
1235 .owner = THIS_MODULE,
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001236 .open = radio_open,
Hans Verkuil1a3c60a2012-05-11 11:25:03 -03001237 .poll = v4l2_ctrl_poll,
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001238 .release = v4l2_fh_release,
Mauro Carvalho Chehabb61872642011-02-13 21:52:02 -03001239 .unlocked_ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240};
1241
Hans Verkuila3998102008-07-21 02:57:38 -03001242static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Hans Verkuil902e1972012-05-09 16:23:07 -03001243 .vidioc_querycap = vidioc_querycap,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001244 .vidioc_g_tuner = radio_g_tuner,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001245 .vidioc_s_tuner = radio_s_tuner,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001246 .vidioc_g_frequency = vidioc_g_frequency,
1247 .vidioc_s_frequency = vidioc_s_frequency,
Hans Verkuil1a3c60a2012-05-11 11:25:03 -03001248 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1249 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Trent Piephoa75d2042007-08-01 00:13:28 -03001250#ifdef CONFIG_VIDEO_ADV_DEBUG
1251 .vidioc_g_register = vidioc_g_register,
1252 .vidioc_s_register = vidioc_s_register,
1253#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254};
1255
lawrence rust2e4e98e2010-08-25 09:50:20 -03001256static const struct video_device cx8800_radio_template = {
Hans Verkuila3998102008-07-21 02:57:38 -03001257 .name = "cx8800-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03001258 .fops = &radio_fops,
Hans Verkuila3998102008-07-21 02:57:38 -03001259 .ioctl_ops = &radio_ioctl_ops,
1260};
1261
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001262static const struct v4l2_ctrl_ops cx8800_ctrl_vid_ops = {
1263 .s_ctrl = cx8800_s_vid_ctrl,
1264};
1265
1266static const struct v4l2_ctrl_ops cx8800_ctrl_aud_ops = {
1267 .s_ctrl = cx8800_s_aud_ctrl,
Hans Verkuilbac63982012-06-10 07:39:52 -03001268};
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270/* ----------------------------------------------------------- */
1271
1272static void cx8800_unregister_video(struct cx8800_dev *dev)
1273{
Hans Verkuil34080bc2015-03-09 13:34:00 -03001274 video_unregister_device(&dev->radio_dev);
1275 video_unregister_device(&dev->vbi_dev);
1276 video_unregister_device(&dev->video_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277}
1278
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001279static int cx8800_initdev(struct pci_dev *pci_dev,
1280 const struct pci_device_id *pci_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
1282 struct cx8800_dev *dev;
1283 struct cx88_core *core;
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001284 struct vb2_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 int err;
Hans Verkuilbac63982012-06-10 07:39:52 -03001286 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Panagiotis Issaris74081872006-01-11 19:40:56 -02001288 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (NULL == dev)
1290 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 /* pci init */
1293 dev->pci = pci_dev;
1294 if (pci_enable_device(pci_dev)) {
1295 err = -EIO;
1296 goto fail_free;
1297 }
1298 core = cx88_core_get(dev->pci);
1299 if (NULL == core) {
1300 err = -EINVAL;
1301 goto fail_free;
1302 }
1303 dev->core = core;
1304
1305 /* print pci info */
Bjørn Morkabd34d82011-03-21 11:35:56 -03001306 dev->pci_rev = pci_dev->revision;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001307 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
1308 printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -07001309 "latency: %d, mmio: 0x%llx\n", core->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -07001311 dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 pci_set_master(pci_dev);
Yang Hongyang284901a2009-04-06 19:01:15 -07001314 if (!pci_dma_supported(pci_dev,DMA_BIT_MASK(32))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
1316 err = -EIO;
1317 goto fail_core;
1318 }
Hans Verkuil165d0042014-12-08 13:23:49 -03001319 dev->alloc_ctx = vb2_dma_sg_init_ctx(&pci_dev->dev);
1320 if (IS_ERR(dev->alloc_ctx)) {
1321 err = PTR_ERR(dev->alloc_ctx);
1322 goto fail_core;
1323 }
1324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 /* initialize driver struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 spin_lock_init(&dev->slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 /* init video dma queues */
1330 INIT_LIST_HEAD(&dev->vidq.active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 /* init vbi dma queues */
1333 INIT_LIST_HEAD(&dev->vbiq.active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 /* get irq */
1336 err = request_irq(pci_dev->irq, cx8800_irq,
Michael Opdenacker3e018fe2013-10-13 02:49:29 -03001337 IRQF_SHARED, core->name, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001339 printk(KERN_ERR "%s/0: can't get IRQ %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 core->name,pci_dev->irq);
1341 goto fail_core;
1342 }
1343 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1344
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001345 for (i = 0; i < CX8800_AUD_CTLS; i++) {
1346 const struct cx88_ctrl *cc = &cx8800_aud_ctls[i];
Hans Verkuilbac63982012-06-10 07:39:52 -03001347 struct v4l2_ctrl *vc;
1348
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001349 vc = v4l2_ctrl_new_std(&core->audio_hdl, &cx8800_ctrl_aud_ops,
Hans Verkuilbac63982012-06-10 07:39:52 -03001350 cc->id, cc->minimum, cc->maximum, cc->step, cc->default_value);
1351 if (vc == NULL) {
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001352 err = core->audio_hdl.error;
Hans Verkuilbac63982012-06-10 07:39:52 -03001353 goto fail_core;
1354 }
1355 vc->priv = (void *)cc;
1356 }
1357
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001358 for (i = 0; i < CX8800_VID_CTLS; i++) {
1359 const struct cx88_ctrl *cc = &cx8800_vid_ctls[i];
1360 struct v4l2_ctrl *vc;
1361
1362 vc = v4l2_ctrl_new_std(&core->video_hdl, &cx8800_ctrl_vid_ops,
1363 cc->id, cc->minimum, cc->maximum, cc->step, cc->default_value);
1364 if (vc == NULL) {
1365 err = core->video_hdl.error;
1366 goto fail_core;
1367 }
1368 vc->priv = (void *)cc;
1369 if (vc->id == V4L2_CID_CHROMA_AGC)
1370 core->chroma_agc = vc;
1371 }
Hans Verkuil34a6b7d2012-09-14 07:15:03 -03001372 v4l2_ctrl_add_handler(&core->video_hdl, &core->audio_hdl, NULL);
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 /* load and configure helper modules */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001375
Hans Verkuilfacd2362013-05-26 07:56:06 -03001376 if (core->board.audio_chip == CX88_AUDIO_WM8775) {
Lawrence Rust69518032011-02-06 17:46:12 -03001377 struct i2c_board_info wm8775_info = {
1378 .type = "wm8775",
1379 .addr = 0x36 >> 1,
1380 .platform_data = &core->wm8775_data,
1381 };
1382 struct v4l2_subdev *sd;
1383
1384 if (core->boardnr == CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1)
1385 core->wm8775_data.is_nova_s = true;
1386 else
1387 core->wm8775_data.is_nova_s = false;
1388
1389 sd = v4l2_i2c_new_subdev_board(&core->v4l2_dev, &core->i2c_adap,
1390 &wm8775_info, NULL);
Hans Verkuilbac63982012-06-10 07:39:52 -03001391 if (sd != NULL) {
1392 core->sd_wm8775 = sd;
Lawrence Rust69518032011-02-06 17:46:12 -03001393 sd->grp_id = WM8775_GID;
Hans Verkuilbac63982012-06-10 07:39:52 -03001394 }
Lawrence Rust69518032011-02-06 17:46:12 -03001395 }
Hans Verkuilb8341e12009-03-29 08:26:01 -03001396
Hans Verkuilfacd2362013-05-26 07:56:06 -03001397 if (core->board.audio_chip == CX88_AUDIO_TVAUDIO) {
Hans Verkuilb8341e12009-03-29 08:26:01 -03001398 /* This probes for a tda9874 as is used on some
1399 Pixelview Ultra boards. */
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -03001400 v4l2_i2c_new_subdev(&core->v4l2_dev, &core->i2c_adap,
1401 "tvaudio", 0, I2C_ADDRS(0xb0 >> 1));
Hans Verkuilb8341e12009-03-29 08:26:01 -03001402 }
Steven Toth30579062006-09-25 12:43:42 -03001403
Michael Krufky6fcecce2007-08-24 01:32:31 -03001404 switch (core->boardnr) {
1405 case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
Hans Verkuilb8341e12009-03-29 08:26:01 -03001406 case CX88_BOARD_DVICO_FUSIONHDTV_7_GOLD: {
lawrence rust2e4e98e2010-08-25 09:50:20 -03001407 static const struct i2c_board_info rtc_info = {
Hans Verkuilb8341e12009-03-29 08:26:01 -03001408 I2C_BOARD_INFO("isl1208", 0x6f)
1409 };
1410
Michael Krufky6fcecce2007-08-24 01:32:31 -03001411 request_module("rtc-isl1208");
Hans Verkuilb8341e12009-03-29 08:26:01 -03001412 core->i2c_rtc = i2c_new_device(&core->i2c_adap, &rtc_info);
1413 }
Michael Krufky8efd2e22008-04-22 14:45:14 -03001414 /* break intentionally omitted */
1415 case CX88_BOARD_DVICO_FUSIONHDTV_5_PCI_NANO:
1416 request_module("ir-kbd-i2c");
Michael Krufky6fcecce2007-08-24 01:32:31 -03001417 }
1418
Mauro Carvalho Chehab121ec132011-02-14 07:01:51 -03001419 /* Sets device info at pci_dev */
1420 pci_set_drvdata(pci_dev, dev);
1421
Hans Verkuilccd6f1d2014-09-20 09:23:44 -03001422 dev->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
Hans Verkuilc5a86142012-05-11 10:45:18 -03001423
Hans Verkuil078859a2014-08-29 06:08:07 -03001424 /* Maintain a reference so cx88-blackbird can query the 8800 device. */
1425 core->v4ldev = dev;
1426
Mauro Carvalho Chehab121ec132011-02-14 07:01:51 -03001427 /* initial device configuration */
1428 mutex_lock(&core->lock);
1429 cx88_set_tvnorm(core, core->tvnorm);
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001430 v4l2_ctrl_handler_setup(&core->video_hdl);
1431 v4l2_ctrl_handler_setup(&core->audio_hdl);
Mauro Carvalho Chehab121ec132011-02-14 07:01:51 -03001432 cx88_video_mux(core, 0);
1433
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001434 q = &dev->vb2_vidq;
1435 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1436 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1437 q->gfp_flags = GFP_DMA32;
1438 q->min_buffers_needed = 2;
1439 q->drv_priv = dev;
1440 q->buf_struct_size = sizeof(struct cx88_buffer);
1441 q->ops = &cx8800_video_qops;
1442 q->mem_ops = &vb2_dma_sg_memops;
1443 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1444 q->lock = &core->lock;
1445
1446 err = vb2_queue_init(q);
1447 if (err < 0)
1448 goto fail_unreg;
1449
1450 q = &dev->vb2_vbiq;
1451 q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1452 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1453 q->gfp_flags = GFP_DMA32;
1454 q->min_buffers_needed = 2;
1455 q->drv_priv = dev;
1456 q->buf_struct_size = sizeof(struct cx88_buffer);
1457 q->ops = &cx8800_vbi_qops;
1458 q->mem_ops = &vb2_dma_sg_memops;
1459 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1460 q->lock = &core->lock;
1461
1462 err = vb2_queue_init(q);
1463 if (err < 0)
1464 goto fail_unreg;
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 /* register v4l devices */
Hans Verkuil34080bc2015-03-09 13:34:00 -03001467 cx88_vdev_init(core, dev->pci, &dev->video_dev,
1468 &cx8800_video_template, "video");
1469 video_set_drvdata(&dev->video_dev, dev);
1470 dev->video_dev.ctrl_handler = &core->video_hdl;
1471 dev->video_dev.queue = &dev->vb2_vidq;
1472 err = video_register_device(&dev->video_dev, VFL_TYPE_GRABBER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 video_nr[core->nr]);
1474 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001475 printk(KERN_ERR "%s/0: can't register video device\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 core->name);
1477 goto fail_unreg;
1478 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001479 printk(KERN_INFO "%s/0: registered device %s [v4l2]\n",
Hans Verkuil34080bc2015-03-09 13:34:00 -03001480 core->name, video_device_node_name(&dev->video_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Hans Verkuil34080bc2015-03-09 13:34:00 -03001482 cx88_vdev_init(core, dev->pci, &dev->vbi_dev,
1483 &cx8800_vbi_template, "vbi");
1484 video_set_drvdata(&dev->vbi_dev, dev);
1485 dev->vbi_dev.queue = &dev->vb2_vbiq;
1486 err = video_register_device(&dev->vbi_dev, VFL_TYPE_VBI,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 vbi_nr[core->nr]);
1488 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001489 printk(KERN_ERR "%s/0: can't register vbi device\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 core->name);
1491 goto fail_unreg;
1492 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001493 printk(KERN_INFO "%s/0: registered device %s\n",
Hans Verkuil34080bc2015-03-09 13:34:00 -03001494 core->name, video_device_node_name(&dev->vbi_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Trent Piepho6a59d642007-08-15 14:41:57 -03001496 if (core->board.radio.type == CX88_RADIO) {
Hans Verkuil34080bc2015-03-09 13:34:00 -03001497 cx88_vdev_init(core, dev->pci, &dev->radio_dev,
1498 &cx8800_radio_template, "radio");
1499 video_set_drvdata(&dev->radio_dev, dev);
1500 dev->radio_dev.ctrl_handler = &core->audio_hdl;
1501 err = video_register_device(&dev->radio_dev, VFL_TYPE_RADIO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 radio_nr[core->nr]);
1503 if (err < 0) {
Trent Piepho5772f812007-08-15 14:41:59 -03001504 printk(KERN_ERR "%s/0: can't register radio device\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 core->name);
1506 goto fail_unreg;
1507 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001508 printk(KERN_INFO "%s/0: registered device %s\n",
Hans Verkuil34080bc2015-03-09 13:34:00 -03001509 core->name, video_device_node_name(&dev->radio_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 }
1511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 /* start tvaudio thread */
Hans Verkuilc39ba3302014-08-29 07:45:00 -03001513 if (core->board.tuner_type != UNSET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
Cyrill Gorcunov32b78de2007-07-19 11:44:11 -03001515 if (IS_ERR(core->kthread)) {
1516 err = PTR_ERR(core->kthread);
Trent Piepho5772f812007-08-15 14:41:59 -03001517 printk(KERN_ERR "%s/0: failed to create cx88 audio thread, err=%d\n",
1518 core->name, err);
Cyrill Gorcunov32b78de2007-07-19 11:44:11 -03001519 }
1520 }
Mauro Carvalho Chehab121ec132011-02-14 07:01:51 -03001521 mutex_unlock(&core->lock);
1522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 return 0;
1524
1525fail_unreg:
1526 cx8800_unregister_video(dev);
1527 free_irq(pci_dev->irq, dev);
Mauro Carvalho Chehab121ec132011-02-14 07:01:51 -03001528 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529fail_core:
Hans Verkuil165d0042014-12-08 13:23:49 -03001530 vb2_dma_sg_cleanup_ctx(dev->alloc_ctx);
Hans Verkuil078859a2014-08-29 06:08:07 -03001531 core->v4ldev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 cx88_core_put(core,dev->pci);
1533fail_free:
1534 kfree(dev);
1535 return err;
1536}
1537
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001538static void cx8800_finidev(struct pci_dev *pci_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001540 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001541 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
1543 /* stop thread */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001544 if (core->kthread) {
1545 kthread_stop(core->kthread);
1546 core->kthread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 }
1548
Marton Balintb12203d2008-03-26 02:07:35 -03001549 if (core->ir)
Mauro Carvalho Chehab92f4fc12010-03-31 16:07:49 -03001550 cx88_ir_stop(core);
Marton Balintb12203d2008-03-26 02:07:35 -03001551
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001552 cx88_shutdown(core); /* FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
1554 /* unregister stuff */
1555
1556 free_irq(pci_dev->irq, dev);
1557 cx8800_unregister_video(dev);
Hans Verkuil98822de2014-09-05 09:37:09 -03001558 pci_disable_device(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Hans Verkuil078859a2014-08-29 06:08:07 -03001560 core->v4ldev = NULL;
1561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 /* free memory */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001563 cx88_core_put(core,dev->pci);
Hans Verkuil165d0042014-12-08 13:23:49 -03001564 vb2_dma_sg_cleanup_ctx(dev->alloc_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 kfree(dev);
1566}
1567
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03001568#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
1570{
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07001571 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 struct cx88_core *core = dev->core;
Alexey Khoroshilov5ddfbbb2013-04-13 18:52:04 -03001573 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
1575 /* stop video+vbi capture */
Alexey Khoroshilov5ddfbbb2013-04-13 18:52:04 -03001576 spin_lock_irqsave(&dev->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 if (!list_empty(&dev->vidq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03001578 printk("%s/0: suspend video\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 stop_video_dma(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 }
1581 if (!list_empty(&dev->vbiq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03001582 printk("%s/0: suspend vbi\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 cx8800_stop_vbi_dma(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 }
Alexey Khoroshilov5ddfbbb2013-04-13 18:52:04 -03001585 spin_unlock_irqrestore(&dev->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Mauro Carvalho Chehab13595a52007-10-01 08:51:39 -03001587 if (core->ir)
Mauro Carvalho Chehab92f4fc12010-03-31 16:07:49 -03001588 cx88_ir_stop(core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 /* FIXME -- shutdown device */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001590 cx88_shutdown(core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 pci_save_state(pci_dev);
1593 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
1594 pci_disable_device(pci_dev);
1595 dev->state.disabled = 1;
1596 }
1597 return 0;
1598}
1599
1600static int cx8800_resume(struct pci_dev *pci_dev)
1601{
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07001602 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 struct cx88_core *core = dev->core;
Alexey Khoroshilov5ddfbbb2013-04-13 18:52:04 -03001604 unsigned long flags;
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001605 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 if (dev->state.disabled) {
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001608 err=pci_enable_device(pci_dev);
1609 if (err) {
Trent Piepho5772f812007-08-15 14:41:59 -03001610 printk(KERN_ERR "%s/0: can't enable device\n",
1611 core->name);
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001612 return err;
1613 }
1614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 dev->state.disabled = 0;
1616 }
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001617 err= pci_set_power_state(pci_dev, PCI_D0);
1618 if (err) {
Trent Piepho5772f812007-08-15 14:41:59 -03001619 printk(KERN_ERR "%s/0: can't set power state\n", core->name);
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001620 pci_disable_device(pci_dev);
1621 dev->state.disabled = 1;
1622
1623 return err;
1624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 pci_restore_state(pci_dev);
1626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 /* FIXME: re-initialize hardware */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001628 cx88_reset(core);
Mauro Carvalho Chehab13595a52007-10-01 08:51:39 -03001629 if (core->ir)
Mauro Carvalho Chehab92f4fc12010-03-31 16:07:49 -03001630 cx88_ir_start(core);
Mauro Carvalho Chehab13595a52007-10-01 08:51:39 -03001631
1632 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
1634 /* restart video+vbi capture */
Alexey Khoroshilov5ddfbbb2013-04-13 18:52:04 -03001635 spin_lock_irqsave(&dev->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 if (!list_empty(&dev->vidq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03001637 printk("%s/0: resume video\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 restart_video_queue(dev,&dev->vidq);
1639 }
1640 if (!list_empty(&dev->vbiq.active)) {
Trent Piepho5772f812007-08-15 14:41:59 -03001641 printk("%s/0: resume vbi\n", core->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1643 }
Alexey Khoroshilov5ddfbbb2013-04-13 18:52:04 -03001644 spin_unlock_irqrestore(&dev->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646 return 0;
1647}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03001648#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
1650/* ----------------------------------------------------------- */
1651
lawrence rust2e4e98e2010-08-25 09:50:20 -03001652static const struct pci_device_id cx8800_pci_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 {
1654 .vendor = 0x14f1,
1655 .device = 0x8800,
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07001656 .subvendor = PCI_ANY_ID,
1657 .subdevice = PCI_ANY_ID,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 },{
1659 /* --- end of list --- */
1660 }
1661};
1662MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
1663
1664static struct pci_driver cx8800_pci_driver = {
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07001665 .name = "cx8800",
1666 .id_table = cx8800_pci_tbl,
1667 .probe = cx8800_initdev,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001668 .remove = cx8800_finidev,
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03001669#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 .suspend = cx8800_suspend,
1671 .resume = cx8800_resume,
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03001672#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673};
1674
Sachin Kamat06333e02013-09-20 05:32:07 -03001675module_pci_driver(cx8800_pci_driver);