blob: 3b49ebb21b13746fb9c47e7ff225f150dce080f5 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * device driver for Conexant 2388x based TV cards
5 * video4linux video interface
6 *
7 * (c) 2003-04 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
8 *
Mauro Carvalho Chehab32590812018-04-25 05:34:48 -04009 * (c) 2005-2006 Mauro Carvalho Chehab <mchehab@kernel.org>
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -030010 * - Multituner support
11 * - video_ioctl2 conversion
12 * - PAL/M fixes
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
14
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -020015#include "cx88.h"
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/init.h>
18#include <linux/list.h>
19#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/kmod.h>
21#include <linux/kernel.h>
22#include <linux/slab.h>
23#include <linux/interrupt.h>
Andrew Mortonc24228d2007-05-06 14:51:55 -070024#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/delay.h>
26#include <linux/kthread.h>
27#include <asm/div64.h>
28
Michael Krufky5e453dc2006-01-09 15:32:31 -020029#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030030#include <media/v4l2-ioctl.h>
Hans Verkuil1a3c60a2012-05-11 11:25:03 -030031#include <media/v4l2-event.h>
Mauro Carvalho Chehabb5dcee22015-11-10 12:01:44 -020032#include <media/i2c/wm8775.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
35MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
36MODULE_LICENSE("GPL");
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030037MODULE_VERSION(CX88_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39/* ------------------------------------------------------------------ */
40
41static unsigned int video_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
42static unsigned int vbi_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
43static unsigned int radio_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
44
45module_param_array(video_nr, int, NULL, 0444);
46module_param_array(vbi_nr, int, NULL, 0444);
47module_param_array(radio_nr, int, NULL, 0444);
48
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -020049MODULE_PARM_DESC(video_nr, "video device numbers");
50MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
51MODULE_PARM_DESC(radio_nr, "radio device numbers");
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030053static unsigned int video_debug;
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -020054module_param(video_debug, int, 0644);
55MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030057static unsigned int irq_debug;
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -020058module_param(irq_debug, int, 0644);
59MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -020061#define dprintk(level, fmt, arg...) do { \
62 if (video_debug >= level) \
63 printk(KERN_DEBUG pr_fmt("%s: video:" fmt), \
64 __func__, ##arg); \
65} while (0)
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/* ------------------------------------------------------------------- */
68/* static data */
69
lawrence rust2e4e98e2010-08-25 09:50:20 -030070static const struct cx8800_fmt formats[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 {
72 .name = "8 bpp, gray",
73 .fourcc = V4L2_PIX_FMT_GREY,
74 .cxformat = ColorFormatY8,
75 .depth = 8,
76 .flags = FORMAT_FLAGS_PACKED,
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -020077 }, {
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 .name = "15 bpp RGB, le",
79 .fourcc = V4L2_PIX_FMT_RGB555,
80 .cxformat = ColorFormatRGB15,
81 .depth = 16,
82 .flags = FORMAT_FLAGS_PACKED,
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -020083 }, {
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 .name = "15 bpp RGB, be",
85 .fourcc = V4L2_PIX_FMT_RGB555X,
86 .cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
87 .depth = 16,
88 .flags = FORMAT_FLAGS_PACKED,
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -020089 }, {
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 .name = "16 bpp RGB, le",
91 .fourcc = V4L2_PIX_FMT_RGB565,
92 .cxformat = ColorFormatRGB16,
93 .depth = 16,
94 .flags = FORMAT_FLAGS_PACKED,
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -020095 }, {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 .name = "16 bpp RGB, be",
97 .fourcc = V4L2_PIX_FMT_RGB565X,
98 .cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
99 .depth = 16,
100 .flags = FORMAT_FLAGS_PACKED,
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200101 }, {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 .name = "24 bpp RGB, le",
103 .fourcc = V4L2_PIX_FMT_BGR24,
104 .cxformat = ColorFormatRGB24,
105 .depth = 24,
106 .flags = FORMAT_FLAGS_PACKED,
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200107 }, {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 .name = "32 bpp RGB, le",
109 .fourcc = V4L2_PIX_FMT_BGR32,
110 .cxformat = ColorFormatRGB32,
111 .depth = 32,
112 .flags = FORMAT_FLAGS_PACKED,
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200113 }, {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 .name = "32 bpp RGB, be",
115 .fourcc = V4L2_PIX_FMT_RGB32,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200116 .cxformat = ColorFormatRGB32 | ColorFormatBSWAP |
117 ColorFormatWSWAP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 .depth = 32,
119 .flags = FORMAT_FLAGS_PACKED,
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200120 }, {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 .name = "4:2:2, packed, YUYV",
122 .fourcc = V4L2_PIX_FMT_YUYV,
123 .cxformat = ColorFormatYUY2,
124 .depth = 16,
125 .flags = FORMAT_FLAGS_PACKED,
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200126 }, {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 .name = "4:2:2, packed, UYVY",
128 .fourcc = V4L2_PIX_FMT_UYVY,
129 .cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
130 .depth = 16,
131 .flags = FORMAT_FLAGS_PACKED,
132 },
133};
134
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200135static const struct cx8800_fmt *format_by_fourcc(unsigned int fourcc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 unsigned int i;
138
139 for (i = 0; i < ARRAY_SIZE(formats); i++)
140 if (formats[i].fourcc == fourcc)
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200141 return formats + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return NULL;
143}
144
145/* ------------------------------------------------------------------- */
146
Hans Verkuilbac63982012-06-10 07:39:52 -0300147struct cx88_ctrl {
148 /* control information */
149 u32 id;
150 s32 minimum;
151 s32 maximum;
152 u32 step;
153 s32 default_value;
154
155 /* control register information */
156 u32 off;
157 u32 reg;
158 u32 sreg;
159 u32 mask;
160 u32 shift;
161};
162
Hans Verkuil8c7cb122012-05-11 09:07:45 -0300163static const struct cx88_ctrl cx8800_vid_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 /* --- video --- */
165 {
Hans Verkuilbac63982012-06-10 07:39:52 -0300166 .id = V4L2_CID_BRIGHTNESS,
167 .minimum = 0x00,
168 .maximum = 0xff,
169 .step = 1,
170 .default_value = 0x7f,
171 .off = 128,
172 .reg = MO_CONTR_BRIGHT,
173 .mask = 0x00ff,
174 .shift = 0,
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200175 }, {
Hans Verkuilbac63982012-06-10 07:39:52 -0300176 .id = V4L2_CID_CONTRAST,
177 .minimum = 0,
178 .maximum = 0xff,
179 .step = 1,
180 .default_value = 0x3f,
181 .off = 0,
182 .reg = MO_CONTR_BRIGHT,
183 .mask = 0xff00,
184 .shift = 8,
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200185 }, {
Hans Verkuilbac63982012-06-10 07:39:52 -0300186 .id = V4L2_CID_HUE,
187 .minimum = 0,
188 .maximum = 0xff,
189 .step = 1,
190 .default_value = 0x7f,
191 .off = 128,
192 .reg = MO_HUE,
193 .mask = 0x00ff,
194 .shift = 0,
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200195 }, {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 /* strictly, this only describes only U saturation.
197 * V saturation is handled specially through code.
198 */
Hans Verkuilbac63982012-06-10 07:39:52 -0300199 .id = V4L2_CID_SATURATION,
200 .minimum = 0,
201 .maximum = 0xff,
202 .step = 1,
203 .default_value = 0x7f,
204 .off = 0,
205 .reg = MO_UV_SATURATION,
206 .mask = 0x00ff,
207 .shift = 0,
istvan_v@mailbox.hueea16e32011-07-11 11:02:19 -0300208 }, {
Hans Verkuilbac63982012-06-10 07:39:52 -0300209 .id = V4L2_CID_SHARPNESS,
210 .minimum = 0,
211 .maximum = 4,
212 .step = 1,
213 .default_value = 0x0,
214 .off = 0,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200215 /*
216 * NOTE: the value is converted and written to both even
217 * and odd registers in the code
218 */
Hans Verkuilbac63982012-06-10 07:39:52 -0300219 .reg = MO_FILTER_ODD,
220 .mask = 7 << 7,
221 .shift = 7,
istvan_v@mailbox.hueea16e32011-07-11 11:02:19 -0300222 }, {
Hans Verkuilbac63982012-06-10 07:39:52 -0300223 .id = V4L2_CID_CHROMA_AGC,
224 .minimum = 0,
225 .maximum = 1,
226 .default_value = 0x1,
227 .reg = MO_INPUT_FORMAT,
228 .mask = 1 << 10,
229 .shift = 10,
Frej Drejhammar6d042032008-03-23 22:43:21 -0300230 }, {
Hans Verkuilbac63982012-06-10 07:39:52 -0300231 .id = V4L2_CID_COLOR_KILLER,
232 .minimum = 0,
233 .maximum = 1,
234 .default_value = 0x1,
235 .reg = MO_INPUT_FORMAT,
236 .mask = 1 << 9,
237 .shift = 9,
Frej Drejhammar1b879c42008-03-23 22:43:24 -0300238 }, {
Hans Verkuilbac63982012-06-10 07:39:52 -0300239 .id = V4L2_CID_BAND_STOP_FILTER,
240 .minimum = 0,
241 .maximum = 1,
242 .step = 1,
243 .default_value = 0x0,
244 .off = 0,
245 .reg = MO_HTOTAL,
246 .mask = 3 << 11,
247 .shift = 11,
Hans Verkuil8c7cb122012-05-11 09:07:45 -0300248 }
249};
250
251static const struct cx88_ctrl cx8800_aud_ctls[] = {
252 {
Hans Verkuilbac63982012-06-10 07:39:52 -0300253 /* --- audio --- */
254 .id = V4L2_CID_AUDIO_MUTE,
255 .minimum = 0,
256 .maximum = 1,
257 .default_value = 1,
258 .reg = AUD_VOL_CTL,
259 .sreg = SHADOW_AUD_VOL_CTL,
260 .mask = (1 << 6),
261 .shift = 6,
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200262 }, {
Hans Verkuilbac63982012-06-10 07:39:52 -0300263 .id = V4L2_CID_AUDIO_VOLUME,
264 .minimum = 0,
265 .maximum = 0x3f,
266 .step = 1,
267 .default_value = 0x3f,
268 .reg = AUD_VOL_CTL,
269 .sreg = SHADOW_AUD_VOL_CTL,
270 .mask = 0x3f,
271 .shift = 0,
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200272 }, {
Hans Verkuilbac63982012-06-10 07:39:52 -0300273 .id = V4L2_CID_AUDIO_BALANCE,
274 .minimum = 0,
275 .maximum = 0x7f,
276 .step = 1,
277 .default_value = 0x40,
278 .reg = AUD_BAL_CTL,
279 .sreg = SHADOW_AUD_BAL_CTL,
280 .mask = 0x7f,
281 .shift = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283};
Hans Verkuilbac63982012-06-10 07:39:52 -0300284
Hans Verkuil8c7cb122012-05-11 09:07:45 -0300285enum {
286 CX8800_VID_CTLS = ARRAY_SIZE(cx8800_vid_ctls),
287 CX8800_AUD_CTLS = ARRAY_SIZE(cx8800_aud_ctls),
288};
Michael Krufky38a27132006-06-26 23:42:39 -0300289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290/* ------------------------------------------------------------------ */
291
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -0300292int cx88_video_mux(struct cx88_core *core, unsigned int input)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700294 /* struct cx88_core *core = dev->core; */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200296 dprintk(1, "video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
Trent Piepho6a59d642007-08-15 14:41:57 -0300297 input, INPUT(input).vmux,
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200298 INPUT(input).gpio0, INPUT(input).gpio1,
299 INPUT(input).gpio2, INPUT(input).gpio3);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700300 core->input = input;
Trent Piepho6a59d642007-08-15 14:41:57 -0300301 cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input).vmux << 14);
302 cx_write(MO_GP3_IO, INPUT(input).gpio3);
303 cx_write(MO_GP0_IO, INPUT(input).gpio0);
304 cx_write(MO_GP1_IO, INPUT(input).gpio1);
305 cx_write(MO_GP2_IO, INPUT(input).gpio2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Trent Piepho6a59d642007-08-15 14:41:57 -0300307 switch (INPUT(input).type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 case CX88_VMUX_SVIDEO:
309 cx_set(MO_AFECFG_IO, 0x00000001);
310 cx_set(MO_INPUT_FORMAT, 0x00010010);
311 cx_set(MO_FILTER_EVEN, 0x00002020);
312 cx_set(MO_FILTER_ODD, 0x00002020);
313 break;
314 default:
315 cx_clear(MO_AFECFG_IO, 0x00000001);
316 cx_clear(MO_INPUT_FORMAT, 0x00010010);
317 cx_clear(MO_FILTER_EVEN, 0x00002020);
318 cx_clear(MO_FILTER_ODD, 0x00002020);
319 break;
320 }
Michael Krufkyf24546a2006-10-16 16:51:11 -0300321
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200322 /*
323 * if there are audioroutes defined, we have an external
324 * ADC to deal with audio
325 */
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300326 if (INPUT(input).audioroute) {
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200327 /*
328 * The wm8775 module has the "2" route hardwired into
329 * the initialization. Some boards may use different
330 * routes for different inputs. HVR-1300 surely does
331 */
Hans Verkuil609c4c12013-05-29 10:44:22 -0300332 if (core->sd_wm8775) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300333 call_all(core, audio, s_routing,
istvan_v@mailbox.hu6e1f4df2010-03-27 09:31:32 -0300334 INPUT(input).audioroute, 0, 0);
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300335 }
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200336 /*
337 * cx2388's C-ADC is connected to the tuner only.
338 * When used with S-Video, that ADC is busy dealing with
339 * chroma, so an external must be used for baseband audio
340 */
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 */
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200372 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
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200378 /*
379 * Enables corresponding bits at PCI_INT_STAT:
380 * bits 0 to 4: video, audio, transport stream, VIP, Host
381 * bit 7: timer
382 * bits 8 and 9: DMA complete for: SRC, DST
383 * bits 10 and 11: BERR signal asserted for RISC: RD, WR
384 * bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700385 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 cx_set(MO_VID_INTMSK, 0x0f0011);
387
388 /* enable capture */
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200389 cx_set(VID_CAPTURE_CONTROL, 0x06);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 /* start dma */
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200392 cx_set(MO_DEV_CNTRL2, (1 << 5));
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700393 cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 return 0;
396}
397
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -0300398#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399static int stop_video_dma(struct cx8800_dev *dev)
400{
401 struct cx88_core *core = dev->core;
402
403 /* stop dma */
404 cx_clear(MO_VID_DMACNTRL, 0x11);
405
406 /* disable capture */
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200407 cx_clear(VID_CAPTURE_CONTROL, 0x06);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 /* disable irqs */
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300410 cx_clear(MO_PCI_INTMSK, PCI_INT_VIDINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 cx_clear(MO_VID_INTMSK, 0x0f0011);
412 return 0;
413}
414
415static int restart_video_queue(struct cx8800_dev *dev,
416 struct cx88_dmaqueue *q)
417{
Hans Verkuil6f11adc2014-08-10 11:56:14 -0300418 struct cx88_buffer *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 if (!list_empty(&q->active)) {
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300421 buf = list_entry(q->active.next, struct cx88_buffer, list);
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200422 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
Junghak Sung2d700712015-09-22 10:30:30 -0300423 buf, buf->vb.vb2_buf.index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 start_video_dma(dev, q, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
Hans Verkuil6f11adc2014-08-10 11:56:14 -0300426 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
Mauro Carvalho Chehabbc5e66b2015-06-10 13:55:35 -0300428#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430/* ------------------------------------------------------------------ */
431
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200432static int queue_setup(struct vb2_queue *q,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200433 unsigned int *num_buffers, unsigned int *num_planes,
434 unsigned int sizes[], struct device *alloc_devs[])
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return 0;
442}
443
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300444static int buffer_prepare(struct vb2_buffer *vb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Junghak Sung2d700712015-09-22 10:30:30 -0300446 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
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;
Junghak Sung2d700712015-09-22 10:30:30 -0300449 struct cx88_buffer *buf = container_of(vbuf, struct cx88_buffer, vb);
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300450 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 }
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200491 dprintk(2,
492 "[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
Junghak Sung2d700712015-09-22 10:30:30 -0300493 buf, buf->vb.vb2_buf.index,
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300494 core->width, core->height, dev->fmt->depth, dev->fmt->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 (unsigned long)buf->risc.dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300499static void buffer_finish(struct vb2_buffer *vb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Junghak Sung2d700712015-09-22 10:30:30 -0300501 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300502 struct cx8800_dev *dev = vb->vb2_queue->drv_priv;
Junghak Sung2d700712015-09-22 10:30:30 -0300503 struct cx88_buffer *buf = container_of(vbuf, struct cx88_buffer, vb);
Hans Verkuil5e7045e2014-08-29 04:11:54 -0300504 struct cx88_riscmem *risc = &buf->risc;
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300505
Hans Verkuil5e7045e2014-08-29 04:11:54 -0300506 if (risc->cpu)
507 pci_free_consistent(dev->pci, risc->size, risc->cpu, risc->dma);
508 memset(risc, 0, sizeof(*risc));
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300509}
510
511static void buffer_queue(struct vb2_buffer *vb)
512{
Junghak Sung2d700712015-09-22 10:30:30 -0300513 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300514 struct cx8800_dev *dev = vb->vb2_queue->drv_priv;
Junghak Sung2d700712015-09-22 10:30:30 -0300515 struct cx88_buffer *buf = container_of(vbuf, struct cx88_buffer, vb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 struct cx88_buffer *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 struct cx88_dmaqueue *q = &dev->vidq;
518
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300519 /* add jump to start */
520 buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 8);
521 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
522 buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Hans Verkuil6f11adc2014-08-10 11:56:14 -0300524 if (list_empty(&q->active)) {
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300525 list_add_tail(&buf->list, &q->active);
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200526 dprintk(2, "[%p/%d] buffer_queue - first active\n",
Junghak Sung2d700712015-09-22 10:30:30 -0300527 buf, buf->vb.vb2_buf.index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 } else {
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300530 buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
531 prev = list_entry(q->active.prev, struct cx88_buffer, list);
532 list_add_tail(&buf->list, &q->active);
Hans Verkuil637bc202014-08-29 03:46:05 -0300533 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
534 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
Junghak Sung2d700712015-09-22 10:30:30 -0300535 buf, buf->vb.vb2_buf.index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
537}
538
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300539static int start_streaming(struct vb2_queue *q, unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300541 struct cx8800_dev *dev = q->drv_priv;
542 struct cx88_dmaqueue *dmaq = &dev->vidq;
543 struct cx88_buffer *buf = list_entry(dmaq->active.next,
544 struct cx88_buffer, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300546 start_video_dma(dev, dmaq, buf);
547 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300550static void stop_streaming(struct vb2_queue *q)
551{
552 struct cx8800_dev *dev = q->drv_priv;
553 struct cx88_core *core = dev->core;
554 struct cx88_dmaqueue *dmaq = &dev->vidq;
555 unsigned long flags;
556
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300557 cx_clear(MO_VID_DMACNTRL, 0x11);
558 cx_clear(VID_CAPTURE_CONTROL, 0x06);
559 spin_lock_irqsave(&dev->slock, flags);
560 while (!list_empty(&dmaq->active)) {
561 struct cx88_buffer *buf = list_entry(dmaq->active.next,
562 struct cx88_buffer, list);
563
564 list_del(&buf->list);
Junghak Sung2d700712015-09-22 10:30:30 -0300565 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300566 }
567 spin_unlock_irqrestore(&dev->slock, flags);
568}
569
Julia Lawall10accd22016-09-08 20:59:18 -0300570static const struct vb2_ops cx8800_video_qops = {
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300571 .queue_setup = queue_setup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 .buf_prepare = buffer_prepare,
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300573 .buf_finish = buffer_finish,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 .buf_queue = buffer_queue,
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300575 .wait_prepare = vb2_ops_wait_prepare,
576 .wait_finish = vb2_ops_wait_finish,
577 .start_streaming = start_streaming,
578 .stop_streaming = stop_streaming,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579};
580
581/* ------------------------------------------------------------------ */
582
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300583static int radio_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200585 struct cx8800_dev *dev = video_drvdata(file);
Dan Carpenter5401c2d2010-10-21 16:21:45 -0300586 struct cx88_core *core = dev->core;
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300587 int ret = v4l2_fh_open(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300589 if (ret)
590 return ret;
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200591
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300592 cx_write(MO_GP3_IO, core->board.radio.gpio3);
593 cx_write(MO_GP0_IO, core->board.radio.gpio0);
594 cx_write(MO_GP1_IO, core->board.radio.gpio1);
595 cx_write(MO_GP2_IO, core->board.radio.gpio2);
596 if (core->board.radio.audioroute) {
597 if (core->sd_wm8775) {
598 call_all(core, audio, s_routing,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200599 core->board.radio.audioroute, 0, 0);
Darron Broad430189d2008-10-15 14:18:42 -0300600 }
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300601 /* "I2S ADC mode" */
602 core->tvaudio = WW_I2SADC;
603 cx88_set_tvaudio(core);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 } else {
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300605 /* FM Mode */
606 core->tvaudio = WW_FM;
607 cx88_set_tvaudio(core);
608 cx88_set_stereo(core, V4L2_TUNER_MODE_STEREO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300610 call_all(core, tuner, s_radio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return 0;
612}
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614/* ------------------------------------------------------------------ */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300615/* VIDEO CTRL IOCTLS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Hans Verkuil8c7cb122012-05-11 09:07:45 -0300617static int cx8800_s_vid_ctrl(struct v4l2_ctrl *ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Hans Verkuilbac63982012-06-10 07:39:52 -0300619 struct cx88_core *core =
Hans Verkuil8c7cb122012-05-11 09:07:45 -0300620 container_of(ctrl->handler, struct cx88_core, video_hdl);
621 const struct cx88_ctrl *cc = ctrl->priv;
622 u32 value, mask;
623
624 mask = cc->mask;
625 switch (ctrl->id) {
626 case V4L2_CID_SATURATION:
627 /* special v_sat handling */
628
629 value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
630
631 if (core->tvnorm & V4L2_STD_SECAM) {
632 /* For SECAM, both U and V sat should be equal */
633 value = value << 8 | value;
634 } else {
635 /* Keeps U Saturation proportional to V Sat */
636 value = (value * 0x5a) / 0x7f << 8 | value;
637 }
638 mask = 0xffff;
639 break;
640 case V4L2_CID_SHARPNESS:
641 /* 0b000, 0b100, 0b101, 0b110, or 0b111 */
642 value = (ctrl->val < 1 ? 0 : ((ctrl->val + 3) << 7));
643 /* needs to be set for both fields */
644 cx_andor(MO_FILTER_EVEN, mask, value);
645 break;
646 case V4L2_CID_CHROMA_AGC:
647 value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
648 break;
649 default:
650 value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
651 break;
652 }
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200653 dprintk(1,
654 "set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
655 ctrl->id, ctrl->name, ctrl->val, cc->reg, value,
656 mask, cc->sreg ? " [shadowed]" : "");
Hans Verkuil8c7cb122012-05-11 09:07:45 -0300657 if (cc->sreg)
658 cx_sandor(cc->sreg, cc->reg, mask, value);
659 else
660 cx_andor(cc->reg, mask, value);
661 return 0;
662}
663
664static int cx8800_s_aud_ctrl(struct v4l2_ctrl *ctrl)
665{
666 struct cx88_core *core =
667 container_of(ctrl->handler, struct cx88_core, audio_hdl);
Hans Verkuilbac63982012-06-10 07:39:52 -0300668 const struct cx88_ctrl *cc = ctrl->priv;
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200669 u32 value, mask;
Lawrence Rust69518032011-02-06 17:46:12 -0300670
671 /* Pass changes onto any WM8775 */
Hans Verkuil609c4c12013-05-29 10:44:22 -0300672 if (core->sd_wm8775) {
Hans Verkuilbac63982012-06-10 07:39:52 -0300673 switch (ctrl->id) {
Lawrence Rust69518032011-02-06 17:46:12 -0300674 case V4L2_CID_AUDIO_MUTE:
Hans Verkuilbac63982012-06-10 07:39:52 -0300675 wm8775_s_ctrl(core, ctrl->id, ctrl->val);
Lawrence Rust69518032011-02-06 17:46:12 -0300676 break;
677 case V4L2_CID_AUDIO_VOLUME:
Hans Verkuilbac63982012-06-10 07:39:52 -0300678 wm8775_s_ctrl(core, ctrl->id, (ctrl->val) ?
679 (0x90 + ctrl->val) << 8 : 0);
Lawrence Rust69518032011-02-06 17:46:12 -0300680 break;
681 case V4L2_CID_AUDIO_BALANCE:
Hans Verkuilbac63982012-06-10 07:39:52 -0300682 wm8775_s_ctrl(core, ctrl->id, ctrl->val << 9);
Lawrence Rust69518032011-02-06 17:46:12 -0300683 break;
684 default:
Lawrence Rust69518032011-02-06 17:46:12 -0300685 break;
686 }
Lawrence Rust69518032011-02-06 17:46:12 -0300687 }
688
Hans Verkuilbac63982012-06-10 07:39:52 -0300689 mask = cc->mask;
690 switch (ctrl->id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 case V4L2_CID_AUDIO_BALANCE:
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200692 value = (ctrl->val < 0x40) ?
693 (0x7f - ctrl->val) : (ctrl->val - 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 break;
695 case V4L2_CID_AUDIO_VOLUME:
Hans Verkuilbac63982012-06-10 07:39:52 -0300696 value = 0x3f - (ctrl->val & 0x3f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 default:
Hans Verkuilbac63982012-06-10 07:39:52 -0300699 value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 break;
701 }
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200702 dprintk(1,
703 "set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
704 ctrl->id, ctrl->name, ctrl->val, cc->reg, value,
705 mask, cc->sreg ? " [shadowed]" : "");
Hans Verkuilbac63982012-06-10 07:39:52 -0300706 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 Chehab399426c2016-11-19 19:27:30 -0200717 struct v4l2_format *f)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300718{
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,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200735 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
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);
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200744 if (!fmt)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300745 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 Chehab399426c2016-11-19 19:27:30 -0200781 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;
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200785 int err = vidioc_try_fmt_vid_cap(file, priv, f);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700786
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200787 if (err != 0)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300788 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
Mauro Carvalho Chehab4839c582017-09-28 18:39:32 -0400800int cx88_querycap(struct file *file, struct cx88_core *core,
801 struct v4l2_capability *cap)
Hans Verkuil902e1972012-05-09 16:23:07 -0300802{
803 struct video_device *vdev = video_devdata(file);
804
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -0400805 strscpy(cap->card, core->board.name, sizeof(cap->card));
Hans Verkuil902e1972012-05-09 16:23:07 -0300806 cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200807 if (core->board.tuner_type != UNSET)
Hans Verkuil902e1972012-05-09 16:23:07 -0300808 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;
Mauro Carvalho Chehab4839c582017-09-28 18:39:32 -0400819 default:
820 return -EINVAL;
Hans Verkuil902e1972012-05-09 16:23:07 -0300821 }
822 cap->capabilities = cap->device_caps | V4L2_CAP_VIDEO_CAPTURE |
823 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_DEVICE_CAPS;
824 if (core->board.radio.type == CX88_RADIO)
825 cap->capabilities |= V4L2_CAP_RADIO;
Mauro Carvalho Chehab4839c582017-09-28 18:39:32 -0400826 return 0;
Hans Verkuil902e1972012-05-09 16:23:07 -0300827}
828EXPORT_SYMBOL(cx88_querycap);
829
830static int vidioc_querycap(struct file *file, void *priv,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200831 struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300833 struct cx8800_dev *dev = video_drvdata(file);
834 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Mauro Carvalho Chehabcc1e6312018-09-10 16:20:42 -0400836 strscpy(cap->driver, "cx8800", sizeof(cap->driver));
Hans Verkuil902e1972012-05-09 16:23:07 -0300837 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
Mauro Carvalho Chehab4839c582017-09-28 18:39:32 -0400838 return cx88_querycap(file, core, cap);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300839}
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700840
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200841static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200842 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300843{
844 if (unlikely(f->index >= ARRAY_SIZE(formats)))
845 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -0400847 strscpy(f->description, formats[f->index].name, sizeof(f->description));
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300848 f->pixelformat = formats[f->index].fourcc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300850 return 0;
851}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Hans Verkuil48d68802012-05-25 12:15:30 -0300853static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorm)
854{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300855 struct cx8800_dev *dev = video_drvdata(file);
856 struct cx88_core *core = dev->core;
Hans Verkuil48d68802012-05-25 12:15:30 -0300857
858 *tvnorm = core->tvnorm;
859 return 0;
860}
861
Hans Verkuil314527a2013-03-15 06:10:40 -0300862static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300863{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300864 struct cx8800_dev *dev = video_drvdata(file);
865 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300866
Hans Verkuil078859a2014-08-29 06:08:07 -0300867 return cx88_set_tvnorm(core, tvnorms);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300868}
869
870/* only one input in this sample driver */
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200871int cx88_enum_input(struct cx88_core *core, struct v4l2_input *i)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300872{
lawrence rust2e4e98e2010-08-25 09:50:20 -0300873 static const char * const iname[] = {
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200874 [CX88_VMUX_COMPOSITE1] = "Composite1",
875 [CX88_VMUX_COMPOSITE2] = "Composite2",
876 [CX88_VMUX_COMPOSITE3] = "Composite3",
877 [CX88_VMUX_COMPOSITE4] = "Composite4",
878 [CX88_VMUX_SVIDEO] = "S-Video",
879 [CX88_VMUX_TELEVISION] = "Television",
880 [CX88_VMUX_CABLE] = "Cable TV",
881 [CX88_VMUX_DVB] = "DVB",
882 [CX88_VMUX_DEBUG] = "for debug only",
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300883 };
Trent Piephof3334bc2009-03-04 01:21:03 -0300884 unsigned int n = i->index;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300885
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300886 if (n >= 4)
887 return -EINVAL;
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200888 if (!INPUT(n).type)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300889 return -EINVAL;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300890 i->type = V4L2_INPUT_TYPE_CAMERA;
Mauro Carvalho Chehabcc1e6312018-09-10 16:20:42 -0400891 strscpy(i->name, iname[INPUT(n).type], sizeof(i->name));
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200892 if ((INPUT(n).type == CX88_VMUX_TELEVISION) ||
893 (INPUT(n).type == CX88_VMUX_CABLE))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300894 i->type = V4L2_INPUT_TYPE_TUNER;
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200895
Hans Verkuilf33e9862012-05-25 12:04:10 -0300896 i->std = CX88_NORMS;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300897 return 0;
898}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300899EXPORT_SYMBOL(cx88_enum_input);
900
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200901static int vidioc_enum_input(struct file *file, void *priv,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200902 struct v4l2_input *i)
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300903{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300904 struct cx8800_dev *dev = video_drvdata(file);
905 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200906
907 return cx88_enum_input(core, i);
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300908}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300909
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200910static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300911{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300912 struct cx8800_dev *dev = video_drvdata(file);
913 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300914
915 *i = core->input;
916 return 0;
917}
918
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200919static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300920{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300921 struct cx8800_dev *dev = video_drvdata(file);
922 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300923
924 if (i >= 4)
925 return -EINVAL;
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200926 if (!INPUT(i).type)
Hans Verkuilf33e9862012-05-25 12:04:10 -0300927 return -EINVAL;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300928
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300929 cx88_newstation(core);
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200930 cx88_video_mux(core, i);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300931 return 0;
932}
933
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200934static int vidioc_g_tuner(struct file *file, void *priv,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200935 struct v4l2_tuner *t)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300936{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300937 struct cx8800_dev *dev = video_drvdata(file);
938 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300939 u32 reg;
940
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200941 if (unlikely(core->board.tuner_type == UNSET))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300942 return -EINVAL;
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200943 if (t->index != 0)
Mauro Carvalho Chehab243d8c02007-01-20 13:58:36 -0300944 return -EINVAL;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300945
Mauro Carvalho Chehabcc1e6312018-09-10 16:20:42 -0400946 strscpy(t->name, "Television", sizeof(t->name));
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300947 t->capability = V4L2_TUNER_CAP_NORM;
948 t->rangehigh = 0xffffffffUL;
Hans Verkuilf33e9862012-05-25 12:04:10 -0300949 call_all(core, tuner, g_tuner, t);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300950
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200951 cx88_get_stereo(core, t);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300952 reg = cx_read(MO_DEVICE_STATUS);
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200953 t->signal = (reg & (1 << 5)) ? 0xffff : 0x0000;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300954 return 0;
955}
956
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200957static int vidioc_s_tuner(struct file *file, void *priv,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200958 const struct v4l2_tuner *t)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300959{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300960 struct cx8800_dev *dev = video_drvdata(file);
961 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300962
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200963 if (core->board.tuner_type == UNSET)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300964 return -EINVAL;
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200965 if (t->index != 0)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300966 return -EINVAL;
967
968 cx88_set_stereo(core, t->audmode, 1);
969 return 0;
970}
971
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200972static int vidioc_g_frequency(struct file *file, void *priv,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200973 struct v4l2_frequency *f)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300974{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300975 struct cx8800_dev *dev = video_drvdata(file);
976 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300977
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200978 if (unlikely(core->board.tuner_type == UNSET))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300979 return -EINVAL;
Hans Verkuilf33e9862012-05-25 12:04:10 -0300980 if (f->tuner)
981 return -EINVAL;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300982
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300983 f->frequency = core->freq;
984
Hans Verkuilb8341e12009-03-29 08:26:01 -0300985 call_all(core, tuner, g_frequency, f);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300986
987 return 0;
988}
989
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200990int cx88_set_freq(struct cx88_core *core,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200991 const struct v4l2_frequency *f)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300992{
Hans Verkuilb530a442013-03-19 04:09:26 -0300993 struct v4l2_frequency new_freq = *f;
994
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200995 if (unlikely(core->board.tuner_type == UNSET))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300996 return -EINVAL;
997 if (unlikely(f->tuner != 0))
998 return -EINVAL;
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300999
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001000 cx88_newstation(core);
Hans Verkuilb8341e12009-03-29 08:26:01 -03001001 call_all(core, tuner, s_frequency, f);
Hans Verkuilb530a442013-03-19 04:09:26 -03001002 call_all(core, tuner, g_frequency, &new_freq);
1003 core->freq = new_freq.frequency;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001004
1005 /* When changing channels it is required to reset TVAUDIO */
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001006 usleep_range(10000, 20000);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001007 cx88_set_tvaudio(core);
1008
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001009 return 0;
1010}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001011EXPORT_SYMBOL(cx88_set_freq);
1012
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001013static int vidioc_s_frequency(struct file *file, void *priv,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -02001014 const struct v4l2_frequency *f)
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001015{
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001016 struct cx8800_dev *dev = video_drvdata(file);
1017 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001018
Hans Verkuiledbd1382012-05-11 10:33:25 -03001019 return cx88_set_freq(core, f);
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -03001020}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001021
Trent Piephodbbff482007-01-22 23:31:53 -03001022#ifdef CONFIG_VIDEO_ADV_DEBUG
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001023static int vidioc_g_register(struct file *file, void *fh,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -02001024 struct v4l2_dbg_register *reg)
Trent Piephodbbff482007-01-22 23:31:53 -03001025{
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001026 struct cx8800_dev *dev = video_drvdata(file);
1027 struct cx88_core *core = dev->core;
Trent Piephodbbff482007-01-22 23:31:53 -03001028
Trent Piephodbbff482007-01-22 23:31:53 -03001029 /* cx2388x has a 24-bit register space */
Hans Verkuil7feeb142013-05-29 06:22:02 -03001030 reg->val = cx_read(reg->reg & 0xfffffc);
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001031 reg->size = 4;
Trent Piephodbbff482007-01-22 23:31:53 -03001032 return 0;
1033}
1034
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001035static int vidioc_s_register(struct file *file, void *fh,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -02001036 const struct v4l2_dbg_register *reg)
Trent Piephodbbff482007-01-22 23:31:53 -03001037{
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001038 struct cx8800_dev *dev = video_drvdata(file);
1039 struct cx88_core *core = dev->core;
Trent Piephodbbff482007-01-22 23:31:53 -03001040
Hans Verkuil7feeb142013-05-29 06:22:02 -03001041 cx_write(reg->reg & 0xfffffc, reg->val);
Trent Piephodbbff482007-01-22 23:31:53 -03001042 return 0;
1043}
1044#endif
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046/* ----------------------------------------------------------- */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001047/* RADIO ESPECIFIC IOCTLS */
1048/* ----------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001050static int radio_g_tuner(struct file *file, void *priv,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -02001051 struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001053 struct cx8800_dev *dev = video_drvdata(file);
1054 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001055
1056 if (unlikely(t->index > 0))
1057 return -EINVAL;
1058
Mauro Carvalho Chehabcc1e6312018-09-10 16:20:42 -04001059 strscpy(t->name, "Radio", sizeof(t->name));
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001060
Hans Verkuilb8341e12009-03-29 08:26:01 -03001061 call_all(core, tuner, g_tuner, t);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001062 return 0;
1063}
1064
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001065static int radio_s_tuner(struct file *file, void *priv,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -02001066 const struct v4l2_tuner *t)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001067{
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001068 struct cx8800_dev *dev = video_drvdata(file);
1069 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001070
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001071 if (t->index != 0)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001072 return -EINVAL;
1073
Hans Verkuilb8341e12009-03-29 08:26:01 -03001074 call_all(core, tuner, s_tuner, t);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001075 return 0;
1076}
1077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078/* ----------------------------------------------------------- */
1079
lawrence rust2e4e98e2010-08-25 09:50:20 -03001080static const char *cx88_vid_irqs[32] = {
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -07001081 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1082 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1083 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1084 "y_sync", "u_sync", "v_sync", "vbi_sync",
1085 "opc_err", "par_err", "rip_err", "pci_abort",
1086};
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088static void cx8800_vid_irq(struct cx8800_dev *dev)
1089{
1090 struct cx88_core *core = dev->core;
1091 u32 status, mask, count;
1092
1093 status = cx_read(MO_VID_INTSTAT);
1094 mask = cx_read(MO_VID_INTMSK);
1095 if (0 == (status & mask))
1096 return;
1097 cx_write(MO_VID_INTSTAT, status);
1098 if (irq_debug || (status & mask & ~0xff))
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001099 cx88_print_irqbits("irq vid",
Mauro Carvalho Chehab66623a02007-03-29 08:47:04 -03001100 cx88_vid_irqs, ARRAY_SIZE(cx88_vid_irqs),
1101 status, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 /* risc op code error */
1104 if (status & (1 << 16)) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001105 pr_warn("video risc op code error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 cx_clear(MO_VID_DMACNTRL, 0x11);
1107 cx_clear(VID_CAPTURE_CONTROL, 0x06);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001108 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
1110
1111 /* risc1 y */
1112 if (status & 0x01) {
1113 spin_lock(&dev->slock);
1114 count = cx_read(MO_VIDY_GPCNT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001115 cx88_wakeup(core, &dev->vidq, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 spin_unlock(&dev->slock);
1117 }
1118
1119 /* risc1 vbi */
1120 if (status & 0x08) {
1121 spin_lock(&dev->slock);
1122 count = cx_read(MO_VBI_GPCNT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001123 cx88_wakeup(core, &dev->vbiq, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 spin_unlock(&dev->slock);
1125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126}
1127
David Howells7d12e782006-10-05 14:55:46 +01001128static irqreturn_t cx8800_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
1130 struct cx8800_dev *dev = dev_id;
1131 struct cx88_core *core = dev->core;
1132 u32 status;
1133 int loop, handled = 0;
1134
1135 for (loop = 0; loop < 10; loop++) {
Trent Piepho8ddac9e2007-08-18 06:57:55 -03001136 status = cx_read(MO_PCI_INTSTAT) &
1137 (core->pci_irqmask | PCI_INT_VIDINT);
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001138 if (status == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 goto out;
1140 cx_write(MO_PCI_INTSTAT, status);
1141 handled = 1;
1142
1143 if (status & core->pci_irqmask)
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001144 cx88_core_irq(core, status);
Trent Piepho8ddac9e2007-08-18 06:57:55 -03001145 if (status & PCI_INT_VIDINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 cx8800_vid_irq(dev);
Peter Senna Tschudinc2c1b412012-09-28 05:37:22 -03001147 }
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001148 if (loop == 10) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001149 pr_warn("irq loop -- clearing mask\n");
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001150 cx_write(MO_PCI_INTMSK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
1152
1153 out:
1154 return IRQ_RETVAL(handled);
1155}
1156
1157/* ----------------------------------------------------------- */
1158/* exported stuff */
1159
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001160static const struct v4l2_file_operations video_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 .owner = THIS_MODULE,
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001162 .open = v4l2_fh_open,
1163 .release = vb2_fop_release,
1164 .read = vb2_fop_read,
1165 .poll = vb2_fop_poll,
1166 .mmap = vb2_fop_mmap,
Mauro Carvalho Chehabb61872642011-02-13 21:52:02 -03001167 .unlocked_ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168};
1169
Hans Verkuila3998102008-07-21 02:57:38 -03001170static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001171 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001172 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1173 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1174 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1175 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001176 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1177 .vidioc_querybuf = vb2_ioctl_querybuf,
1178 .vidioc_qbuf = vb2_ioctl_qbuf,
1179 .vidioc_dqbuf = vb2_ioctl_dqbuf,
Hans Verkuil48d68802012-05-25 12:15:30 -03001180 .vidioc_g_std = vidioc_g_std,
Hans Verkuilf33e9862012-05-25 12:04:10 -03001181 .vidioc_s_std = vidioc_s_std,
1182 .vidioc_enum_input = vidioc_enum_input,
1183 .vidioc_g_input = vidioc_g_input,
1184 .vidioc_s_input = vidioc_s_input,
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001185 .vidioc_streamon = vb2_ioctl_streamon,
1186 .vidioc_streamoff = vb2_ioctl_streamoff,
Hans Verkuilf33e9862012-05-25 12:04:10 -03001187 .vidioc_g_tuner = vidioc_g_tuner,
1188 .vidioc_s_tuner = vidioc_s_tuner,
1189 .vidioc_g_frequency = vidioc_g_frequency,
1190 .vidioc_s_frequency = vidioc_s_frequency,
1191 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1192 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Hans Verkuilf33e9862012-05-25 12:04:10 -03001193#ifdef CONFIG_VIDEO_ADV_DEBUG
1194 .vidioc_g_register = vidioc_g_register,
1195 .vidioc_s_register = vidioc_s_register,
1196#endif
1197};
1198
1199static const struct video_device cx8800_video_template = {
1200 .name = "cx8800-video",
1201 .fops = &video_fops,
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001202 .ioctl_ops = &video_ioctl_ops,
Hans Verkuilf33e9862012-05-25 12:04:10 -03001203 .tvnorms = CX88_NORMS,
Hans Verkuilf33e9862012-05-25 12:04:10 -03001204};
1205
1206static const struct v4l2_ioctl_ops vbi_ioctl_ops = {
1207 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001208 .vidioc_g_fmt_vbi_cap = cx8800_vbi_fmt,
1209 .vidioc_try_fmt_vbi_cap = cx8800_vbi_fmt,
1210 .vidioc_s_fmt_vbi_cap = cx8800_vbi_fmt,
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001211 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1212 .vidioc_querybuf = vb2_ioctl_querybuf,
1213 .vidioc_qbuf = vb2_ioctl_qbuf,
1214 .vidioc_dqbuf = vb2_ioctl_dqbuf,
Hans Verkuil48d68802012-05-25 12:15:30 -03001215 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001216 .vidioc_s_std = vidioc_s_std,
1217 .vidioc_enum_input = vidioc_enum_input,
1218 .vidioc_g_input = vidioc_g_input,
1219 .vidioc_s_input = vidioc_s_input,
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001220 .vidioc_streamon = vb2_ioctl_streamon,
1221 .vidioc_streamoff = vb2_ioctl_streamoff,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001222 .vidioc_g_tuner = vidioc_g_tuner,
1223 .vidioc_s_tuner = vidioc_s_tuner,
1224 .vidioc_g_frequency = vidioc_g_frequency,
1225 .vidioc_s_frequency = vidioc_s_frequency,
Trent Piephodbbff482007-01-22 23:31:53 -03001226#ifdef CONFIG_VIDEO_ADV_DEBUG
1227 .vidioc_g_register = vidioc_g_register,
1228 .vidioc_s_register = vidioc_s_register,
1229#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001230};
1231
Hans Verkuilf33e9862012-05-25 12:04:10 -03001232static const struct video_device cx8800_vbi_template = {
1233 .name = "cx8800-vbi",
Hans Verkuila3998102008-07-21 02:57:38 -03001234 .fops = &video_fops,
Hans Verkuilf33e9862012-05-25 12:04:10 -03001235 .ioctl_ops = &vbi_ioctl_ops,
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001236 .tvnorms = CX88_NORMS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237};
1238
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001239static const struct v4l2_file_operations radio_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 .owner = THIS_MODULE,
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001241 .open = radio_open,
Hans Verkuil1a3c60a2012-05-11 11:25:03 -03001242 .poll = v4l2_ctrl_poll,
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001243 .release = v4l2_fh_release,
Mauro Carvalho Chehabb61872642011-02-13 21:52:02 -03001244 .unlocked_ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245};
1246
Hans Verkuila3998102008-07-21 02:57:38 -03001247static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Hans Verkuil902e1972012-05-09 16:23:07 -03001248 .vidioc_querycap = vidioc_querycap,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001249 .vidioc_g_tuner = radio_g_tuner,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001250 .vidioc_s_tuner = radio_s_tuner,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001251 .vidioc_g_frequency = vidioc_g_frequency,
1252 .vidioc_s_frequency = vidioc_s_frequency,
Hans Verkuil1a3c60a2012-05-11 11:25:03 -03001253 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1254 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Trent Piephoa75d2042007-08-01 00:13:28 -03001255#ifdef CONFIG_VIDEO_ADV_DEBUG
1256 .vidioc_g_register = vidioc_g_register,
1257 .vidioc_s_register = vidioc_s_register,
1258#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259};
1260
lawrence rust2e4e98e2010-08-25 09:50:20 -03001261static const struct video_device cx8800_radio_template = {
Hans Verkuila3998102008-07-21 02:57:38 -03001262 .name = "cx8800-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03001263 .fops = &radio_fops,
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001264 .ioctl_ops = &radio_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03001265};
1266
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001267static const struct v4l2_ctrl_ops cx8800_ctrl_vid_ops = {
1268 .s_ctrl = cx8800_s_vid_ctrl,
1269};
1270
1271static const struct v4l2_ctrl_ops cx8800_ctrl_aud_ops = {
1272 .s_ctrl = cx8800_s_aud_ctrl,
Hans Verkuilbac63982012-06-10 07:39:52 -03001273};
1274
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275/* ----------------------------------------------------------- */
1276
1277static void cx8800_unregister_video(struct cx8800_dev *dev)
1278{
Hans Verkuil34080bc2015-03-09 13:34:00 -03001279 video_unregister_device(&dev->radio_dev);
1280 video_unregister_device(&dev->vbi_dev);
1281 video_unregister_device(&dev->video_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282}
1283
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001284static int cx8800_initdev(struct pci_dev *pci_dev,
1285 const struct pci_device_id *pci_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
1287 struct cx8800_dev *dev;
1288 struct cx88_core *core;
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001289 struct vb2_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 int err;
Hans Verkuilbac63982012-06-10 07:39:52 -03001291 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001293 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -02001294 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 /* pci init */
1298 dev->pci = pci_dev;
1299 if (pci_enable_device(pci_dev)) {
1300 err = -EIO;
1301 goto fail_free;
1302 }
1303 core = cx88_core_get(dev->pci);
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -02001304 if (!core) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 err = -EINVAL;
1306 goto fail_free;
1307 }
1308 dev->core = core;
1309
1310 /* print pci info */
Bjørn Morkabd34d82011-03-21 11:35:56 -03001311 dev->pci_rev = pci_dev->revision;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001312 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001313 pr_info("found at %s, rev: %d, irq: %d, latency: %d, mmio: 0x%llx\n",
1314 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
1315 dev->pci_lat,
1316 (unsigned long long)pci_resource_start(pci_dev, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 pci_set_master(pci_dev);
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001319 err = pci_set_dma_mask(pci_dev, DMA_BIT_MASK(32));
Christoph Hellwig1a47de62015-11-20 15:57:07 -08001320 if (err) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001321 pr_err("Oops: no 32bit PCI DMA ???\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 goto fail_core;
1323 }
1324
1325 /* initialize driver struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 spin_lock_init(&dev->slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 /* init video dma queues */
1329 INIT_LIST_HEAD(&dev->vidq.active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 /* init vbi dma queues */
1332 INIT_LIST_HEAD(&dev->vbiq.active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 /* get irq */
1335 err = request_irq(pci_dev->irq, cx8800_irq,
Michael Opdenacker3e018fe2013-10-13 02:49:29 -03001336 IRQF_SHARED, core->name, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 if (err < 0) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001338 pr_err("can't get IRQ %d\n", pci_dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 goto fail_core;
1340 }
1341 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1342
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001343 for (i = 0; i < CX8800_AUD_CTLS; i++) {
1344 const struct cx88_ctrl *cc = &cx8800_aud_ctls[i];
Hans Verkuilbac63982012-06-10 07:39:52 -03001345 struct v4l2_ctrl *vc;
1346
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001347 vc = v4l2_ctrl_new_std(&core->audio_hdl, &cx8800_ctrl_aud_ops,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -02001348 cc->id, cc->minimum, cc->maximum,
1349 cc->step, cc->default_value);
1350 if (!vc) {
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001351 err = core->audio_hdl.error;
Hans Verkuilbac63982012-06-10 07:39:52 -03001352 goto fail_core;
1353 }
1354 vc->priv = (void *)cc;
1355 }
1356
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001357 for (i = 0; i < CX8800_VID_CTLS; i++) {
1358 const struct cx88_ctrl *cc = &cx8800_vid_ctls[i];
1359 struct v4l2_ctrl *vc;
1360
1361 vc = v4l2_ctrl_new_std(&core->video_hdl, &cx8800_ctrl_vid_ops,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -02001362 cc->id, cc->minimum, cc->maximum,
1363 cc->step, cc->default_value);
1364 if (!vc) {
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001365 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 Verkuilda1b1ae2018-05-21 04:54:36 -04001372 v4l2_ctrl_add_handler(&core->video_hdl, &core->audio_hdl, NULL, false);
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,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -02001390 &wm8775_info, NULL);
1391 if (sd) {
Hans Verkuilbac63982012-06-10 07:39:52 -03001392 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) {
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -02001398 /*
1399 * This probes for a tda9874 as is used on some
1400 * Pixelview Ultra boards.
1401 */
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -03001402 v4l2_i2c_new_subdev(&core->v4l2_dev, &core->i2c_adap,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -02001403 "tvaudio", 0, I2C_ADDRS(0xb0 >> 1));
Hans Verkuilb8341e12009-03-29 08:26:01 -03001404 }
Steven Toth30579062006-09-25 12:43:42 -03001405
Michael Krufky6fcecce2007-08-24 01:32:31 -03001406 switch (core->boardnr) {
1407 case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
Hans Verkuilb8341e12009-03-29 08:26:01 -03001408 case CX88_BOARD_DVICO_FUSIONHDTV_7_GOLD: {
lawrence rust2e4e98e2010-08-25 09:50:20 -03001409 static const struct i2c_board_info rtc_info = {
Hans Verkuilb8341e12009-03-29 08:26:01 -03001410 I2C_BOARD_INFO("isl1208", 0x6f)
1411 };
1412
Michael Krufky6fcecce2007-08-24 01:32:31 -03001413 request_module("rtc-isl1208");
Hans Verkuilb8341e12009-03-29 08:26:01 -03001414 core->i2c_rtc = i2c_new_device(&core->i2c_adap, &rtc_info);
1415 }
Mauro Carvalho Chehab06eeefe2017-05-18 08:13:28 -03001416 /* fall-through */
Michael Krufky8efd2e22008-04-22 14:45:14 -03001417 case CX88_BOARD_DVICO_FUSIONHDTV_5_PCI_NANO:
1418 request_module("ir-kbd-i2c");
Michael Krufky6fcecce2007-08-24 01:32:31 -03001419 }
1420
Mauro Carvalho Chehab121ec132011-02-14 07:01:51 -03001421 /* Sets device info at pci_dev */
1422 pci_set_drvdata(pci_dev, dev);
1423
Hans Verkuilccd6f1d2014-09-20 09:23:44 -03001424 dev->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
Hans Verkuilc5a86142012-05-11 10:45:18 -03001425
Hans Verkuil078859a2014-08-29 06:08:07 -03001426 /* Maintain a reference so cx88-blackbird can query the 8800 device. */
1427 core->v4ldev = dev;
1428
Mauro Carvalho Chehab121ec132011-02-14 07:01:51 -03001429 /* initial device configuration */
1430 mutex_lock(&core->lock);
Devin Heitmueller4e0973a2014-09-20 09:23:44 -03001431 cx88_set_tvnorm(core, V4L2_STD_NTSC_M);
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001432 v4l2_ctrl_handler_setup(&core->video_hdl);
1433 v4l2_ctrl_handler_setup(&core->audio_hdl);
Mauro Carvalho Chehab121ec132011-02-14 07:01:51 -03001434 cx88_video_mux(core, 0);
1435
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001436 q = &dev->vb2_vidq;
1437 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1438 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1439 q->gfp_flags = GFP_DMA32;
1440 q->min_buffers_needed = 2;
1441 q->drv_priv = dev;
1442 q->buf_struct_size = sizeof(struct cx88_buffer);
1443 q->ops = &cx8800_video_qops;
1444 q->mem_ops = &vb2_dma_sg_memops;
1445 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1446 q->lock = &core->lock;
Hans Verkuil2bc46b32016-02-15 12:37:15 -02001447 q->dev = &dev->pci->dev;
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001448
1449 err = vb2_queue_init(q);
1450 if (err < 0)
1451 goto fail_unreg;
1452
1453 q = &dev->vb2_vbiq;
1454 q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1455 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1456 q->gfp_flags = GFP_DMA32;
1457 q->min_buffers_needed = 2;
1458 q->drv_priv = dev;
1459 q->buf_struct_size = sizeof(struct cx88_buffer);
1460 q->ops = &cx8800_vbi_qops;
1461 q->mem_ops = &vb2_dma_sg_memops;
1462 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1463 q->lock = &core->lock;
Hans Verkuil2bc46b32016-02-15 12:37:15 -02001464 q->dev = &dev->pci->dev;
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001465
1466 err = vb2_queue_init(q);
1467 if (err < 0)
1468 goto fail_unreg;
1469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 /* register v4l devices */
Hans Verkuil34080bc2015-03-09 13:34:00 -03001471 cx88_vdev_init(core, dev->pci, &dev->video_dev,
1472 &cx8800_video_template, "video");
1473 video_set_drvdata(&dev->video_dev, dev);
1474 dev->video_dev.ctrl_handler = &core->video_hdl;
1475 dev->video_dev.queue = &dev->vb2_vidq;
1476 err = video_register_device(&dev->video_dev, VFL_TYPE_GRABBER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 video_nr[core->nr]);
1478 if (err < 0) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001479 pr_err("can't register video device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 goto fail_unreg;
1481 }
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001482 pr_info("registered device %s [v4l2]\n",
1483 video_device_node_name(&dev->video_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Hans Verkuil34080bc2015-03-09 13:34:00 -03001485 cx88_vdev_init(core, dev->pci, &dev->vbi_dev,
1486 &cx8800_vbi_template, "vbi");
1487 video_set_drvdata(&dev->vbi_dev, dev);
1488 dev->vbi_dev.queue = &dev->vb2_vbiq;
1489 err = video_register_device(&dev->vbi_dev, VFL_TYPE_VBI,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 vbi_nr[core->nr]);
1491 if (err < 0) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001492 pr_err("can't register vbi device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 goto fail_unreg;
1494 }
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001495 pr_info("registered device %s\n",
1496 video_device_node_name(&dev->vbi_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Trent Piepho6a59d642007-08-15 14:41:57 -03001498 if (core->board.radio.type == CX88_RADIO) {
Hans Verkuil34080bc2015-03-09 13:34:00 -03001499 cx88_vdev_init(core, dev->pci, &dev->radio_dev,
1500 &cx8800_radio_template, "radio");
1501 video_set_drvdata(&dev->radio_dev, dev);
1502 dev->radio_dev.ctrl_handler = &core->audio_hdl;
1503 err = video_register_device(&dev->radio_dev, VFL_TYPE_RADIO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 radio_nr[core->nr]);
1505 if (err < 0) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001506 pr_err("can't register radio device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 goto fail_unreg;
1508 }
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001509 pr_info("registered device %s\n",
1510 video_device_node_name(&dev->radio_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 }
1512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 /* start tvaudio thread */
Hans Verkuilc39ba3302014-08-29 07:45:00 -03001514 if (core->board.tuner_type != UNSET) {
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -02001515 core->kthread = kthread_run(cx88_audio_thread,
1516 core, "cx88 tvaudio");
Cyrill Gorcunov32b78de2007-07-19 11:44:11 -03001517 if (IS_ERR(core->kthread)) {
1518 err = PTR_ERR(core->kthread);
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001519 pr_err("failed to create cx88 audio thread, err=%d\n",
1520 err);
Cyrill Gorcunov32b78de2007-07-19 11:44:11 -03001521 }
1522 }
Mauro Carvalho Chehab121ec132011-02-14 07:01:51 -03001523 mutex_unlock(&core->lock);
1524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 return 0;
1526
1527fail_unreg:
1528 cx8800_unregister_video(dev);
1529 free_irq(pci_dev->irq, dev);
Mauro Carvalho Chehab121ec132011-02-14 07:01:51 -03001530 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531fail_core:
Hans Verkuil078859a2014-08-29 06:08:07 -03001532 core->v4ldev = NULL;
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001533 cx88_core_put(core, dev->pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534fail_free:
1535 kfree(dev);
1536 return err;
1537}
1538
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001539static void cx8800_finidev(struct pci_dev *pci_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001541 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001542 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544 /* stop thread */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001545 if (core->kthread) {
1546 kthread_stop(core->kthread);
1547 core->kthread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 }
1549
Marton Balintb12203d2008-03-26 02:07:35 -03001550 if (core->ir)
Mauro Carvalho Chehab92f4fc12010-03-31 16:07:49 -03001551 cx88_ir_stop(core);
Marton Balintb12203d2008-03-26 02:07:35 -03001552
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001553 cx88_shutdown(core); /* FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
1555 /* unregister stuff */
1556
1557 free_irq(pci_dev->irq, dev);
1558 cx8800_unregister_video(dev);
Hans Verkuil98822de2014-09-05 09:37:09 -03001559 pci_disable_device(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Hans Verkuil078859a2014-08-29 06:08:07 -03001561 core->v4ldev = NULL;
1562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 /* free memory */
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001564 cx88_core_put(core, dev->pci);
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)) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001578 pr_info("suspend video\n");
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)) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001582 pr_info("suspend vbi\n");
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);
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -02001593 if (pci_set_power_state(pci_dev,
1594 pci_choose_state(pci_dev, state)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 pci_disable_device(pci_dev);
1596 dev->state.disabled = 1;
1597 }
1598 return 0;
1599}
1600
1601static int cx8800_resume(struct pci_dev *pci_dev)
1602{
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07001603 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 struct cx88_core *core = dev->core;
Alexey Khoroshilov5ddfbbb2013-04-13 18:52:04 -03001605 unsigned long flags;
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001606 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
1608 if (dev->state.disabled) {
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001609 err = pci_enable_device(pci_dev);
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001610 if (err) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001611 pr_err("can't enable device\n");
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 Chehab7b61ba82016-11-16 06:59:49 -02001617 err = pci_set_power_state(pci_dev, PCI_D0);
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001618 if (err) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001619 pr_err("can't set power state\n");
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)) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001637 pr_info("resume video\n");
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001638 restart_video_queue(dev, &dev->vidq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 }
1640 if (!list_empty(&dev->vbiq.active)) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001641 pr_info("resume vbi\n");
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001642 cx8800_restart_vbi_queue(dev, &dev->vbiq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
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,
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001658 }, {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 /* --- 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);