blob: dcc0f02aeb70ca05b9b15031f86641896176285b [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]");
Mauro Carvalho Chehab513dbd32019-06-05 13:24:35 -040036MODULE_LICENSE("GPL v2");
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 {
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 .fourcc = V4L2_PIX_FMT_GREY,
73 .cxformat = ColorFormatY8,
74 .depth = 8,
75 .flags = FORMAT_FLAGS_PACKED,
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -020076 }, {
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 .fourcc = V4L2_PIX_FMT_RGB555,
78 .cxformat = ColorFormatRGB15,
79 .depth = 16,
80 .flags = FORMAT_FLAGS_PACKED,
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -020081 }, {
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 .fourcc = V4L2_PIX_FMT_RGB555X,
83 .cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
84 .depth = 16,
85 .flags = FORMAT_FLAGS_PACKED,
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -020086 }, {
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 .fourcc = V4L2_PIX_FMT_RGB565,
88 .cxformat = ColorFormatRGB16,
89 .depth = 16,
90 .flags = FORMAT_FLAGS_PACKED,
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -020091 }, {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 .fourcc = V4L2_PIX_FMT_RGB565X,
93 .cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
94 .depth = 16,
95 .flags = FORMAT_FLAGS_PACKED,
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -020096 }, {
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 .fourcc = V4L2_PIX_FMT_BGR24,
98 .cxformat = ColorFormatRGB24,
99 .depth = 24,
100 .flags = FORMAT_FLAGS_PACKED,
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200101 }, {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 .fourcc = V4L2_PIX_FMT_BGR32,
103 .cxformat = ColorFormatRGB32,
104 .depth = 32,
105 .flags = FORMAT_FLAGS_PACKED,
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200106 }, {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 .fourcc = V4L2_PIX_FMT_RGB32,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200108 .cxformat = ColorFormatRGB32 | ColorFormatBSWAP |
109 ColorFormatWSWAP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 .depth = 32,
111 .flags = FORMAT_FLAGS_PACKED,
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200112 }, {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 .fourcc = V4L2_PIX_FMT_YUYV,
114 .cxformat = ColorFormatYUY2,
115 .depth = 16,
116 .flags = FORMAT_FLAGS_PACKED,
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200117 }, {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 .fourcc = V4L2_PIX_FMT_UYVY,
119 .cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
120 .depth = 16,
121 .flags = FORMAT_FLAGS_PACKED,
122 },
123};
124
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200125static const struct cx8800_fmt *format_by_fourcc(unsigned int fourcc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
127 unsigned int i;
128
129 for (i = 0; i < ARRAY_SIZE(formats); i++)
130 if (formats[i].fourcc == fourcc)
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200131 return formats + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return NULL;
133}
134
135/* ------------------------------------------------------------------- */
136
Hans Verkuilbac63982012-06-10 07:39:52 -0300137struct cx88_ctrl {
138 /* control information */
139 u32 id;
140 s32 minimum;
141 s32 maximum;
142 u32 step;
143 s32 default_value;
144
145 /* control register information */
146 u32 off;
147 u32 reg;
148 u32 sreg;
149 u32 mask;
150 u32 shift;
151};
152
Hans Verkuil8c7cb122012-05-11 09:07:45 -0300153static const struct cx88_ctrl cx8800_vid_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 /* --- video --- */
155 {
Hans Verkuilbac63982012-06-10 07:39:52 -0300156 .id = V4L2_CID_BRIGHTNESS,
157 .minimum = 0x00,
158 .maximum = 0xff,
159 .step = 1,
160 .default_value = 0x7f,
161 .off = 128,
162 .reg = MO_CONTR_BRIGHT,
163 .mask = 0x00ff,
164 .shift = 0,
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200165 }, {
Hans Verkuilbac63982012-06-10 07:39:52 -0300166 .id = V4L2_CID_CONTRAST,
167 .minimum = 0,
168 .maximum = 0xff,
169 .step = 1,
170 .default_value = 0x3f,
171 .off = 0,
172 .reg = MO_CONTR_BRIGHT,
173 .mask = 0xff00,
174 .shift = 8,
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200175 }, {
Hans Verkuilbac63982012-06-10 07:39:52 -0300176 .id = V4L2_CID_HUE,
177 .minimum = 0,
178 .maximum = 0xff,
179 .step = 1,
180 .default_value = 0x7f,
181 .off = 128,
182 .reg = MO_HUE,
183 .mask = 0x00ff,
184 .shift = 0,
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200185 }, {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 /* strictly, this only describes only U saturation.
187 * V saturation is handled specially through code.
188 */
Hans Verkuilbac63982012-06-10 07:39:52 -0300189 .id = V4L2_CID_SATURATION,
190 .minimum = 0,
191 .maximum = 0xff,
192 .step = 1,
193 .default_value = 0x7f,
194 .off = 0,
195 .reg = MO_UV_SATURATION,
196 .mask = 0x00ff,
197 .shift = 0,
istvan_v@mailbox.hueea16e32011-07-11 11:02:19 -0300198 }, {
Hans Verkuilbac63982012-06-10 07:39:52 -0300199 .id = V4L2_CID_SHARPNESS,
200 .minimum = 0,
201 .maximum = 4,
202 .step = 1,
203 .default_value = 0x0,
204 .off = 0,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200205 /*
206 * NOTE: the value is converted and written to both even
207 * and odd registers in the code
208 */
Hans Verkuilbac63982012-06-10 07:39:52 -0300209 .reg = MO_FILTER_ODD,
210 .mask = 7 << 7,
211 .shift = 7,
istvan_v@mailbox.hueea16e32011-07-11 11:02:19 -0300212 }, {
Hans Verkuilbac63982012-06-10 07:39:52 -0300213 .id = V4L2_CID_CHROMA_AGC,
214 .minimum = 0,
215 .maximum = 1,
216 .default_value = 0x1,
217 .reg = MO_INPUT_FORMAT,
218 .mask = 1 << 10,
219 .shift = 10,
Frej Drejhammar6d042032008-03-23 22:43:21 -0300220 }, {
Hans Verkuilbac63982012-06-10 07:39:52 -0300221 .id = V4L2_CID_COLOR_KILLER,
222 .minimum = 0,
223 .maximum = 1,
224 .default_value = 0x1,
225 .reg = MO_INPUT_FORMAT,
226 .mask = 1 << 9,
227 .shift = 9,
Frej Drejhammar1b879c42008-03-23 22:43:24 -0300228 }, {
Hans Verkuilbac63982012-06-10 07:39:52 -0300229 .id = V4L2_CID_BAND_STOP_FILTER,
230 .minimum = 0,
231 .maximum = 1,
232 .step = 1,
233 .default_value = 0x0,
234 .off = 0,
235 .reg = MO_HTOTAL,
236 .mask = 3 << 11,
237 .shift = 11,
Hans Verkuil8c7cb122012-05-11 09:07:45 -0300238 }
239};
240
241static const struct cx88_ctrl cx8800_aud_ctls[] = {
242 {
Hans Verkuilbac63982012-06-10 07:39:52 -0300243 /* --- audio --- */
244 .id = V4L2_CID_AUDIO_MUTE,
245 .minimum = 0,
246 .maximum = 1,
247 .default_value = 1,
248 .reg = AUD_VOL_CTL,
249 .sreg = SHADOW_AUD_VOL_CTL,
250 .mask = (1 << 6),
251 .shift = 6,
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200252 }, {
Hans Verkuilbac63982012-06-10 07:39:52 -0300253 .id = V4L2_CID_AUDIO_VOLUME,
254 .minimum = 0,
255 .maximum = 0x3f,
256 .step = 1,
257 .default_value = 0x3f,
258 .reg = AUD_VOL_CTL,
259 .sreg = SHADOW_AUD_VOL_CTL,
260 .mask = 0x3f,
261 .shift = 0,
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200262 }, {
Hans Verkuilbac63982012-06-10 07:39:52 -0300263 .id = V4L2_CID_AUDIO_BALANCE,
264 .minimum = 0,
265 .maximum = 0x7f,
266 .step = 1,
267 .default_value = 0x40,
268 .reg = AUD_BAL_CTL,
269 .sreg = SHADOW_AUD_BAL_CTL,
270 .mask = 0x7f,
271 .shift = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273};
Hans Verkuilbac63982012-06-10 07:39:52 -0300274
Hans Verkuil8c7cb122012-05-11 09:07:45 -0300275enum {
276 CX8800_VID_CTLS = ARRAY_SIZE(cx8800_vid_ctls),
277 CX8800_AUD_CTLS = ARRAY_SIZE(cx8800_aud_ctls),
278};
Michael Krufky38a27132006-06-26 23:42:39 -0300279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280/* ------------------------------------------------------------------ */
281
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -0300282int cx88_video_mux(struct cx88_core *core, unsigned int input)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700284 /* struct cx88_core *core = dev->core; */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200286 dprintk(1, "video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
Trent Piepho6a59d642007-08-15 14:41:57 -0300287 input, INPUT(input).vmux,
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200288 INPUT(input).gpio0, INPUT(input).gpio1,
289 INPUT(input).gpio2, INPUT(input).gpio3);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700290 core->input = input;
Trent Piepho6a59d642007-08-15 14:41:57 -0300291 cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input).vmux << 14);
292 cx_write(MO_GP3_IO, INPUT(input).gpio3);
293 cx_write(MO_GP0_IO, INPUT(input).gpio0);
294 cx_write(MO_GP1_IO, INPUT(input).gpio1);
295 cx_write(MO_GP2_IO, INPUT(input).gpio2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Trent Piepho6a59d642007-08-15 14:41:57 -0300297 switch (INPUT(input).type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 case CX88_VMUX_SVIDEO:
299 cx_set(MO_AFECFG_IO, 0x00000001);
300 cx_set(MO_INPUT_FORMAT, 0x00010010);
301 cx_set(MO_FILTER_EVEN, 0x00002020);
302 cx_set(MO_FILTER_ODD, 0x00002020);
303 break;
304 default:
305 cx_clear(MO_AFECFG_IO, 0x00000001);
306 cx_clear(MO_INPUT_FORMAT, 0x00010010);
307 cx_clear(MO_FILTER_EVEN, 0x00002020);
308 cx_clear(MO_FILTER_ODD, 0x00002020);
309 break;
310 }
Michael Krufkyf24546a2006-10-16 16:51:11 -0300311
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200312 /*
313 * if there are audioroutes defined, we have an external
314 * ADC to deal with audio
315 */
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300316 if (INPUT(input).audioroute) {
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200317 /*
318 * The wm8775 module has the "2" route hardwired into
319 * the initialization. Some boards may use different
320 * routes for different inputs. HVR-1300 surely does
321 */
Hans Verkuil609c4c12013-05-29 10:44:22 -0300322 if (core->sd_wm8775) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300323 call_all(core, audio, s_routing,
istvan_v@mailbox.hu6e1f4df2010-03-27 09:31:32 -0300324 INPUT(input).audioroute, 0, 0);
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300325 }
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200326 /*
327 * cx2388's C-ADC is connected to the tuner only.
328 * When used with S-Video, that ADC is busy dealing with
329 * chroma, so an external must be used for baseband audio
330 */
istvan_v@mailbox.hu6e1f4df2010-03-27 09:31:32 -0300331 if (INPUT(input).type != CX88_VMUX_TELEVISION &&
332 INPUT(input).type != CX88_VMUX_CABLE) {
Darron Broad430189d2008-10-15 14:18:42 -0300333 /* "I2S ADC mode" */
334 core->tvaudio = WW_I2SADC;
335 cx88_set_tvaudio(core);
336 } else {
337 /* Normal mode */
338 cx_write(AUD_I2SCNTL, 0x0);
339 cx_clear(AUD_CTL, EN_I2SIN_ENABLE);
340 }
Michael Krufkyf24546a2006-10-16 16:51:11 -0300341 }
Ricardo Cerqueira66e6fbd2007-10-16 20:52:08 -0300342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return 0;
344}
Mauro Carvalho Chehabe90311a2007-01-20 13:58:29 -0300345EXPORT_SYMBOL(cx88_video_mux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347/* ------------------------------------------------------------------ */
348
349static int start_video_dma(struct cx8800_dev *dev,
350 struct cx88_dmaqueue *q,
351 struct cx88_buffer *buf)
352{
353 struct cx88_core *core = dev->core;
354
355 /* setup fifo + format */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700356 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 buf->bpl, buf->risc.dma);
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300358 cx88_set_scale(core, core->width, core->height, core->field);
Hans Verkuil637bc202014-08-29 03:46:05 -0300359 cx_write(MO_COLOR_CTRL, dev->fmt->cxformat | ColorFormatGamma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 /* reset counter */
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200362 cx_write(MO_VIDY_GPCNTRL, GP_COUNT_CONTROL_RESET);
Hans Verkuil94506842015-04-03 07:22:40 -0300363 q->count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 /* enable irqs */
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300366 cx_set(MO_PCI_INTMSK, core->pci_irqmask | PCI_INT_VIDINT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700367
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200368 /*
369 * Enables corresponding bits at PCI_INT_STAT:
370 * bits 0 to 4: video, audio, transport stream, VIP, Host
371 * bit 7: timer
372 * bits 8 and 9: DMA complete for: SRC, DST
373 * bits 10 and 11: BERR signal asserted for RISC: RD, WR
374 * bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700375 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 cx_set(MO_VID_INTMSK, 0x0f0011);
377
378 /* enable capture */
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200379 cx_set(VID_CAPTURE_CONTROL, 0x06);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 /* start dma */
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200382 cx_set(MO_DEV_CNTRL2, (1 << 5));
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700383 cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 return 0;
386}
387
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -0300388#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389static int stop_video_dma(struct cx8800_dev *dev)
390{
391 struct cx88_core *core = dev->core;
392
393 /* stop dma */
394 cx_clear(MO_VID_DMACNTRL, 0x11);
395
396 /* disable capture */
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200397 cx_clear(VID_CAPTURE_CONTROL, 0x06);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 /* disable irqs */
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300400 cx_clear(MO_PCI_INTMSK, PCI_INT_VIDINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 cx_clear(MO_VID_INTMSK, 0x0f0011);
402 return 0;
403}
404
405static int restart_video_queue(struct cx8800_dev *dev,
406 struct cx88_dmaqueue *q)
407{
Hans Verkuil6f11adc2014-08-10 11:56:14 -0300408 struct cx88_buffer *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 if (!list_empty(&q->active)) {
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300411 buf = list_entry(q->active.next, struct cx88_buffer, list);
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200412 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
Junghak Sung2d700712015-09-22 10:30:30 -0300413 buf, buf->vb.vb2_buf.index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 start_video_dma(dev, q, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
Hans Verkuil6f11adc2014-08-10 11:56:14 -0300416 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
Mauro Carvalho Chehabbc5e66b2015-06-10 13:55:35 -0300418#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420/* ------------------------------------------------------------------ */
421
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200422static int queue_setup(struct vb2_queue *q,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200423 unsigned int *num_buffers, unsigned int *num_planes,
424 unsigned int sizes[], struct device *alloc_devs[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300426 struct cx8800_dev *dev = q->drv_priv;
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300427 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300429 *num_planes = 1;
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300430 sizes[0] = (dev->fmt->depth * core->width * core->height) >> 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return 0;
432}
433
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300434static int buffer_prepare(struct vb2_buffer *vb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Junghak Sung2d700712015-09-22 10:30:30 -0300436 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300437 struct cx8800_dev *dev = vb->vb2_queue->drv_priv;
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700438 struct cx88_core *core = dev->core;
Junghak Sung2d700712015-09-22 10:30:30 -0300439 struct cx88_buffer *buf = container_of(vbuf, struct cx88_buffer, vb);
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300440 struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300442 buf->bpl = core->width * dev->fmt->depth >> 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300444 if (vb2_plane_size(vb, 0) < core->height * buf->bpl)
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300445 return -EINVAL;
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300446 vb2_set_plane_payload(vb, 0, core->height * buf->bpl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300448 switch (core->field) {
Hans Verkuil637bc202014-08-29 03:46:05 -0300449 case V4L2_FIELD_TOP:
450 cx88_risc_buffer(dev->pci, &buf->risc,
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300451 sgt->sgl, 0, UNSET,
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300452 buf->bpl, 0, core->height);
Hans Verkuil637bc202014-08-29 03:46:05 -0300453 break;
454 case V4L2_FIELD_BOTTOM:
455 cx88_risc_buffer(dev->pci, &buf->risc,
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300456 sgt->sgl, UNSET, 0,
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300457 buf->bpl, 0, core->height);
Hans Verkuil637bc202014-08-29 03:46:05 -0300458 break;
459 case V4L2_FIELD_SEQ_TB:
460 cx88_risc_buffer(dev->pci, &buf->risc,
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300461 sgt->sgl,
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300462 0, buf->bpl * (core->height >> 1),
Hans Verkuil637bc202014-08-29 03:46:05 -0300463 buf->bpl, 0,
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300464 core->height >> 1);
Hans Verkuil637bc202014-08-29 03:46:05 -0300465 break;
466 case V4L2_FIELD_SEQ_BT:
467 cx88_risc_buffer(dev->pci, &buf->risc,
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300468 sgt->sgl,
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300469 buf->bpl * (core->height >> 1), 0,
Hans Verkuil637bc202014-08-29 03:46:05 -0300470 buf->bpl, 0,
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300471 core->height >> 1);
Hans Verkuil637bc202014-08-29 03:46:05 -0300472 break;
473 case V4L2_FIELD_INTERLACED:
474 default:
475 cx88_risc_buffer(dev->pci, &buf->risc,
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300476 sgt->sgl, 0, buf->bpl,
Hans Verkuil637bc202014-08-29 03:46:05 -0300477 buf->bpl, buf->bpl,
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300478 core->height >> 1);
Hans Verkuil637bc202014-08-29 03:46:05 -0300479 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200481 dprintk(2,
Hans Verkuil2169e6d2019-06-11 09:49:53 -0400482 "[%p/%d] %s - %dx%d %dbpp 0x%08x - dma=0x%08lx\n",
483 buf, buf->vb.vb2_buf.index, __func__,
484 core->width, core->height, dev->fmt->depth, dev->fmt->fourcc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 (unsigned long)buf->risc.dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300489static void buffer_finish(struct vb2_buffer *vb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Junghak Sung2d700712015-09-22 10:30:30 -0300491 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300492 struct cx8800_dev *dev = vb->vb2_queue->drv_priv;
Junghak Sung2d700712015-09-22 10:30:30 -0300493 struct cx88_buffer *buf = container_of(vbuf, struct cx88_buffer, vb);
Hans Verkuil5e7045e2014-08-29 04:11:54 -0300494 struct cx88_riscmem *risc = &buf->risc;
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300495
Hans Verkuil5e7045e2014-08-29 04:11:54 -0300496 if (risc->cpu)
497 pci_free_consistent(dev->pci, risc->size, risc->cpu, risc->dma);
498 memset(risc, 0, sizeof(*risc));
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300499}
500
501static void buffer_queue(struct vb2_buffer *vb)
502{
Junghak Sung2d700712015-09-22 10:30:30 -0300503 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300504 struct cx8800_dev *dev = vb->vb2_queue->drv_priv;
Junghak Sung2d700712015-09-22 10:30:30 -0300505 struct cx88_buffer *buf = container_of(vbuf, struct cx88_buffer, vb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 struct cx88_buffer *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 struct cx88_dmaqueue *q = &dev->vidq;
508
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300509 /* add jump to start */
510 buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 8);
511 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
512 buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Hans Verkuil6f11adc2014-08-10 11:56:14 -0300514 if (list_empty(&q->active)) {
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300515 list_add_tail(&buf->list, &q->active);
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200516 dprintk(2, "[%p/%d] buffer_queue - first active\n",
Junghak Sung2d700712015-09-22 10:30:30 -0300517 buf, buf->vb.vb2_buf.index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 } else {
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300520 buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
521 prev = list_entry(q->active.prev, struct cx88_buffer, list);
522 list_add_tail(&buf->list, &q->active);
Hans Verkuil637bc202014-08-29 03:46:05 -0300523 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
524 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
Junghak Sung2d700712015-09-22 10:30:30 -0300525 buf, buf->vb.vb2_buf.index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
527}
528
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300529static int start_streaming(struct vb2_queue *q, unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300531 struct cx8800_dev *dev = q->drv_priv;
532 struct cx88_dmaqueue *dmaq = &dev->vidq;
533 struct cx88_buffer *buf = list_entry(dmaq->active.next,
534 struct cx88_buffer, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300536 start_video_dma(dev, dmaq, buf);
537 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300540static void stop_streaming(struct vb2_queue *q)
541{
542 struct cx8800_dev *dev = q->drv_priv;
543 struct cx88_core *core = dev->core;
544 struct cx88_dmaqueue *dmaq = &dev->vidq;
545 unsigned long flags;
546
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300547 cx_clear(MO_VID_DMACNTRL, 0x11);
548 cx_clear(VID_CAPTURE_CONTROL, 0x06);
549 spin_lock_irqsave(&dev->slock, flags);
550 while (!list_empty(&dmaq->active)) {
551 struct cx88_buffer *buf = list_entry(dmaq->active.next,
552 struct cx88_buffer, list);
553
554 list_del(&buf->list);
Junghak Sung2d700712015-09-22 10:30:30 -0300555 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300556 }
557 spin_unlock_irqrestore(&dev->slock, flags);
558}
559
Julia Lawall10accd22016-09-08 20:59:18 -0300560static const struct vb2_ops cx8800_video_qops = {
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300561 .queue_setup = queue_setup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 .buf_prepare = buffer_prepare,
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300563 .buf_finish = buffer_finish,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 .buf_queue = buffer_queue,
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300565 .wait_prepare = vb2_ops_wait_prepare,
566 .wait_finish = vb2_ops_wait_finish,
567 .start_streaming = start_streaming,
568 .stop_streaming = stop_streaming,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569};
570
571/* ------------------------------------------------------------------ */
572
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300573static int radio_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200575 struct cx8800_dev *dev = video_drvdata(file);
Dan Carpenter5401c2d2010-10-21 16:21:45 -0300576 struct cx88_core *core = dev->core;
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300577 int ret = v4l2_fh_open(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300579 if (ret)
580 return ret;
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200581
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300582 cx_write(MO_GP3_IO, core->board.radio.gpio3);
583 cx_write(MO_GP0_IO, core->board.radio.gpio0);
584 cx_write(MO_GP1_IO, core->board.radio.gpio1);
585 cx_write(MO_GP2_IO, core->board.radio.gpio2);
586 if (core->board.radio.audioroute) {
587 if (core->sd_wm8775) {
588 call_all(core, audio, s_routing,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200589 core->board.radio.audioroute, 0, 0);
Darron Broad430189d2008-10-15 14:18:42 -0300590 }
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300591 /* "I2S ADC mode" */
592 core->tvaudio = WW_I2SADC;
593 cx88_set_tvaudio(core);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 } else {
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300595 /* FM Mode */
596 core->tvaudio = WW_FM;
597 cx88_set_tvaudio(core);
598 cx88_set_stereo(core, V4L2_TUNER_MODE_STEREO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300600 call_all(core, tuner, s_radio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return 0;
602}
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604/* ------------------------------------------------------------------ */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300605/* VIDEO CTRL IOCTLS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Hans Verkuil8c7cb122012-05-11 09:07:45 -0300607static int cx8800_s_vid_ctrl(struct v4l2_ctrl *ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Hans Verkuilbac63982012-06-10 07:39:52 -0300609 struct cx88_core *core =
Hans Verkuil8c7cb122012-05-11 09:07:45 -0300610 container_of(ctrl->handler, struct cx88_core, video_hdl);
611 const struct cx88_ctrl *cc = ctrl->priv;
612 u32 value, mask;
613
614 mask = cc->mask;
615 switch (ctrl->id) {
616 case V4L2_CID_SATURATION:
617 /* special v_sat handling */
618
619 value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
620
621 if (core->tvnorm & V4L2_STD_SECAM) {
622 /* For SECAM, both U and V sat should be equal */
623 value = value << 8 | value;
624 } else {
625 /* Keeps U Saturation proportional to V Sat */
626 value = (value * 0x5a) / 0x7f << 8 | value;
627 }
628 mask = 0xffff;
629 break;
630 case V4L2_CID_SHARPNESS:
631 /* 0b000, 0b100, 0b101, 0b110, or 0b111 */
632 value = (ctrl->val < 1 ? 0 : ((ctrl->val + 3) << 7));
633 /* needs to be set for both fields */
634 cx_andor(MO_FILTER_EVEN, mask, value);
635 break;
636 case V4L2_CID_CHROMA_AGC:
637 value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
638 break;
639 default:
640 value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
641 break;
642 }
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200643 dprintk(1,
644 "set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
645 ctrl->id, ctrl->name, ctrl->val, cc->reg, value,
646 mask, cc->sreg ? " [shadowed]" : "");
Hans Verkuil8c7cb122012-05-11 09:07:45 -0300647 if (cc->sreg)
648 cx_sandor(cc->sreg, cc->reg, mask, value);
649 else
650 cx_andor(cc->reg, mask, value);
651 return 0;
652}
653
654static int cx8800_s_aud_ctrl(struct v4l2_ctrl *ctrl)
655{
656 struct cx88_core *core =
657 container_of(ctrl->handler, struct cx88_core, audio_hdl);
Hans Verkuilbac63982012-06-10 07:39:52 -0300658 const struct cx88_ctrl *cc = ctrl->priv;
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200659 u32 value, mask;
Lawrence Rust69518032011-02-06 17:46:12 -0300660
661 /* Pass changes onto any WM8775 */
Hans Verkuil609c4c12013-05-29 10:44:22 -0300662 if (core->sd_wm8775) {
Hans Verkuilbac63982012-06-10 07:39:52 -0300663 switch (ctrl->id) {
Lawrence Rust69518032011-02-06 17:46:12 -0300664 case V4L2_CID_AUDIO_MUTE:
Hans Verkuilbac63982012-06-10 07:39:52 -0300665 wm8775_s_ctrl(core, ctrl->id, ctrl->val);
Lawrence Rust69518032011-02-06 17:46:12 -0300666 break;
667 case V4L2_CID_AUDIO_VOLUME:
Hans Verkuilbac63982012-06-10 07:39:52 -0300668 wm8775_s_ctrl(core, ctrl->id, (ctrl->val) ?
669 (0x90 + ctrl->val) << 8 : 0);
Lawrence Rust69518032011-02-06 17:46:12 -0300670 break;
671 case V4L2_CID_AUDIO_BALANCE:
Hans Verkuilbac63982012-06-10 07:39:52 -0300672 wm8775_s_ctrl(core, ctrl->id, ctrl->val << 9);
Lawrence Rust69518032011-02-06 17:46:12 -0300673 break;
674 default:
Lawrence Rust69518032011-02-06 17:46:12 -0300675 break;
676 }
Lawrence Rust69518032011-02-06 17:46:12 -0300677 }
678
Hans Verkuilbac63982012-06-10 07:39:52 -0300679 mask = cc->mask;
680 switch (ctrl->id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 case V4L2_CID_AUDIO_BALANCE:
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200682 value = (ctrl->val < 0x40) ?
683 (0x7f - ctrl->val) : (ctrl->val - 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 break;
685 case V4L2_CID_AUDIO_VOLUME:
Hans Verkuilbac63982012-06-10 07:39:52 -0300686 value = 0x3f - (ctrl->val & 0x3f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 default:
Hans Verkuilbac63982012-06-10 07:39:52 -0300689 value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 break;
691 }
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200692 dprintk(1,
693 "set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
694 ctrl->id, ctrl->name, ctrl->val, cc->reg, value,
695 mask, cc->sreg ? " [shadowed]" : "");
Hans Verkuilbac63982012-06-10 07:39:52 -0300696 if (cc->sreg)
697 cx_sandor(cc->sreg, cc->reg, mask, value);
698 else
699 cx_andor(cc->reg, mask, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return 0;
701}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703/* ------------------------------------------------------------------ */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300704/* VIDEO IOCTLS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Hans Verkuil78b526a2008-05-28 12:16:41 -0300706static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200707 struct v4l2_format *f)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300708{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300709 struct cx8800_dev *dev = video_drvdata(file);
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300710 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300711
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300712 f->fmt.pix.width = core->width;
713 f->fmt.pix.height = core->height;
714 f->fmt.pix.field = core->field;
Hans Verkuilc5a86142012-05-11 10:45:18 -0300715 f->fmt.pix.pixelformat = dev->fmt->fourcc;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300716 f->fmt.pix.bytesperline =
Hans Verkuilc5a86142012-05-11 10:45:18 -0300717 (f->fmt.pix.width * dev->fmt->depth) >> 3;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300718 f->fmt.pix.sizeimage =
719 f->fmt.pix.height * f->fmt.pix.bytesperline;
Hans Verkuilc5a86142012-05-11 10:45:18 -0300720 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300721 return 0;
722}
723
Hans Verkuil78b526a2008-05-28 12:16:41 -0300724static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200725 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300727 struct cx8800_dev *dev = video_drvdata(file);
728 struct cx88_core *core = dev->core;
lawrence rust2e4e98e2010-08-25 09:50:20 -0300729 const struct cx8800_fmt *fmt;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300730 enum v4l2_field field;
731 unsigned int maxw, maxh;
732
733 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200734 if (!fmt)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300735 return -EINVAL;
736
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300737 maxw = norm_maxw(core->tvnorm);
738 maxh = norm_maxh(core->tvnorm);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300739
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300740 field = f->fmt.pix.field;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300741
742 switch (field) {
743 case V4L2_FIELD_TOP:
744 case V4L2_FIELD_BOTTOM:
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300745 case V4L2_FIELD_INTERLACED:
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300746 case V4L2_FIELD_SEQ_BT:
747 case V4L2_FIELD_SEQ_TB:
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300748 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 default:
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300750 field = (f->fmt.pix.height > maxh / 2)
751 ? V4L2_FIELD_INTERLACED
752 : V4L2_FIELD_BOTTOM;
753 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300755 if (V4L2_FIELD_HAS_T_OR_B(field))
756 maxh /= 2;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300757
Trent Piepho4b899452009-05-30 21:45:46 -0300758 v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
759 &f->fmt.pix.height, 32, maxh, 0, 0);
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300760 f->fmt.pix.field = field;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300761 f->fmt.pix.bytesperline =
762 (f->fmt.pix.width * fmt->depth) >> 3;
763 f->fmt.pix.sizeimage =
764 f->fmt.pix.height * f->fmt.pix.bytesperline;
Hans Verkuil94506842015-04-03 07:22:40 -0300765 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300766
767 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
Hans Verkuil78b526a2008-05-28 12:16:41 -0300770static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200771 struct v4l2_format *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300773 struct cx8800_dev *dev = video_drvdata(file);
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300774 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200775 int err = vidioc_try_fmt_vid_cap(file, priv, f);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700776
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200777 if (err != 0)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300778 return err;
Hans Verkuil078859a2014-08-29 06:08:07 -0300779 if (vb2_is_busy(&dev->vb2_vidq) || vb2_is_busy(&dev->vb2_vbiq))
780 return -EBUSY;
781 if (core->dvbdev && vb2_is_busy(&core->dvbdev->vb2_mpegq))
782 return -EBUSY;
Hans Verkuilccd6f1d2014-09-20 09:23:44 -0300783 dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
784 core->width = f->fmt.pix.width;
785 core->height = f->fmt.pix.height;
786 core->field = f->fmt.pix.field;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300787 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
789
Mauro Carvalho Chehab4839c582017-09-28 18:39:32 -0400790int cx88_querycap(struct file *file, struct cx88_core *core,
791 struct v4l2_capability *cap)
Hans Verkuil902e1972012-05-09 16:23:07 -0300792{
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -0400793 strscpy(cap->card, core->board.name, sizeof(cap->card));
Hans Verkuil21615362019-06-17 05:36:16 -0400794 cap->capabilities = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
795 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE |
796 V4L2_CAP_DEVICE_CAPS;
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200797 if (core->board.tuner_type != UNSET)
Hans Verkuil21615362019-06-17 05:36:16 -0400798 cap->capabilities |= V4L2_CAP_TUNER;
Hans Verkuil902e1972012-05-09 16:23:07 -0300799 if (core->board.radio.type == CX88_RADIO)
800 cap->capabilities |= V4L2_CAP_RADIO;
Mauro Carvalho Chehab4839c582017-09-28 18:39:32 -0400801 return 0;
Hans Verkuil902e1972012-05-09 16:23:07 -0300802}
803EXPORT_SYMBOL(cx88_querycap);
804
805static int vidioc_querycap(struct file *file, void *priv,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200806 struct v4l2_capability *cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300808 struct cx8800_dev *dev = video_drvdata(file);
809 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Mauro Carvalho Chehabcc1e6312018-09-10 16:20:42 -0400811 strscpy(cap->driver, "cx8800", sizeof(cap->driver));
Hans Verkuil902e1972012-05-09 16:23:07 -0300812 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
Mauro Carvalho Chehab4839c582017-09-28 18:39:32 -0400813 return cx88_querycap(file, core, cap);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300814}
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -0700815
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200816static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200817 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300818{
819 if (unlikely(f->index >= ARRAY_SIZE(formats)))
820 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300822 f->pixelformat = formats[f->index].fourcc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300824 return 0;
825}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Hans Verkuil48d68802012-05-25 12:15:30 -0300827static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorm)
828{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300829 struct cx8800_dev *dev = video_drvdata(file);
830 struct cx88_core *core = dev->core;
Hans Verkuil48d68802012-05-25 12:15:30 -0300831
832 *tvnorm = core->tvnorm;
833 return 0;
834}
835
Hans Verkuil314527a2013-03-15 06:10:40 -0300836static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300837{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300838 struct cx8800_dev *dev = video_drvdata(file);
839 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300840
Hans Verkuil078859a2014-08-29 06:08:07 -0300841 return cx88_set_tvnorm(core, tvnorms);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300842}
843
844/* only one input in this sample driver */
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200845int cx88_enum_input(struct cx88_core *core, struct v4l2_input *i)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300846{
lawrence rust2e4e98e2010-08-25 09:50:20 -0300847 static const char * const iname[] = {
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200848 [CX88_VMUX_COMPOSITE1] = "Composite1",
849 [CX88_VMUX_COMPOSITE2] = "Composite2",
850 [CX88_VMUX_COMPOSITE3] = "Composite3",
851 [CX88_VMUX_COMPOSITE4] = "Composite4",
852 [CX88_VMUX_SVIDEO] = "S-Video",
853 [CX88_VMUX_TELEVISION] = "Television",
854 [CX88_VMUX_CABLE] = "Cable TV",
855 [CX88_VMUX_DVB] = "DVB",
856 [CX88_VMUX_DEBUG] = "for debug only",
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300857 };
Trent Piephof3334bc2009-03-04 01:21:03 -0300858 unsigned int n = i->index;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300859
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300860 if (n >= 4)
861 return -EINVAL;
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200862 if (!INPUT(n).type)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300863 return -EINVAL;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300864 i->type = V4L2_INPUT_TYPE_CAMERA;
Mauro Carvalho Chehabcc1e6312018-09-10 16:20:42 -0400865 strscpy(i->name, iname[INPUT(n).type], sizeof(i->name));
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200866 if ((INPUT(n).type == CX88_VMUX_TELEVISION) ||
867 (INPUT(n).type == CX88_VMUX_CABLE))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300868 i->type = V4L2_INPUT_TYPE_TUNER;
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200869
Hans Verkuilf33e9862012-05-25 12:04:10 -0300870 i->std = CX88_NORMS;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300871 return 0;
872}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300873EXPORT_SYMBOL(cx88_enum_input);
874
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200875static int vidioc_enum_input(struct file *file, void *priv,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200876 struct v4l2_input *i)
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300877{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300878 struct cx8800_dev *dev = video_drvdata(file);
879 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200880
881 return cx88_enum_input(core, i);
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300882}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300883
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200884static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300885{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300886 struct cx8800_dev *dev = video_drvdata(file);
887 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300888
889 *i = core->input;
890 return 0;
891}
892
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200893static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300894{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300895 struct cx8800_dev *dev = video_drvdata(file);
896 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300897
898 if (i >= 4)
899 return -EINVAL;
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200900 if (!INPUT(i).type)
Hans Verkuilf33e9862012-05-25 12:04:10 -0300901 return -EINVAL;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300902
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300903 cx88_newstation(core);
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200904 cx88_video_mux(core, i);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300905 return 0;
906}
907
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200908static int vidioc_g_tuner(struct file *file, void *priv,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200909 struct v4l2_tuner *t)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300910{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300911 struct cx8800_dev *dev = video_drvdata(file);
912 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300913 u32 reg;
914
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200915 if (unlikely(core->board.tuner_type == UNSET))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300916 return -EINVAL;
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200917 if (t->index != 0)
Mauro Carvalho Chehab243d8c02007-01-20 13:58:36 -0300918 return -EINVAL;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300919
Mauro Carvalho Chehabcc1e6312018-09-10 16:20:42 -0400920 strscpy(t->name, "Television", sizeof(t->name));
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300921 t->capability = V4L2_TUNER_CAP_NORM;
922 t->rangehigh = 0xffffffffUL;
Hans Verkuilf33e9862012-05-25 12:04:10 -0300923 call_all(core, tuner, g_tuner, t);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300924
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200925 cx88_get_stereo(core, t);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300926 reg = cx_read(MO_DEVICE_STATUS);
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200927 t->signal = (reg & (1 << 5)) ? 0xffff : 0x0000;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300928 return 0;
929}
930
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200931static int vidioc_s_tuner(struct file *file, void *priv,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200932 const struct v4l2_tuner *t)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300933{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300934 struct cx8800_dev *dev = video_drvdata(file);
935 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300936
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200937 if (core->board.tuner_type == UNSET)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300938 return -EINVAL;
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200939 if (t->index != 0)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300940 return -EINVAL;
941
942 cx88_set_stereo(core, t->audmode, 1);
943 return 0;
944}
945
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200946static int vidioc_g_frequency(struct file *file, void *priv,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200947 struct v4l2_frequency *f)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300948{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300949 struct cx8800_dev *dev = video_drvdata(file);
950 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300951
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200952 if (unlikely(core->board.tuner_type == UNSET))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300953 return -EINVAL;
Hans Verkuilf33e9862012-05-25 12:04:10 -0300954 if (f->tuner)
955 return -EINVAL;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300956
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300957 f->frequency = core->freq;
958
Hans Verkuilb8341e12009-03-29 08:26:01 -0300959 call_all(core, tuner, g_frequency, f);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300960
961 return 0;
962}
963
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200964int cx88_set_freq(struct cx88_core *core,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200965 const struct v4l2_frequency *f)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300966{
Hans Verkuilb530a442013-03-19 04:09:26 -0300967 struct v4l2_frequency new_freq = *f;
968
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200969 if (unlikely(core->board.tuner_type == UNSET))
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300970 return -EINVAL;
971 if (unlikely(f->tuner != 0))
972 return -EINVAL;
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300973
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300974 cx88_newstation(core);
Hans Verkuilb8341e12009-03-29 08:26:01 -0300975 call_all(core, tuner, s_frequency, f);
Hans Verkuilb530a442013-03-19 04:09:26 -0300976 call_all(core, tuner, g_frequency, &new_freq);
977 core->freq = new_freq.frequency;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300978
979 /* When changing channels it is required to reset TVAUDIO */
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200980 usleep_range(10000, 20000);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300981 cx88_set_tvaudio(core);
982
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300983 return 0;
984}
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300985EXPORT_SYMBOL(cx88_set_freq);
986
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200987static int vidioc_s_frequency(struct file *file, void *priv,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200988 const struct v4l2_frequency *f)
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300989{
Hans Verkuil0b6b6302014-09-20 09:22:18 -0300990 struct cx8800_dev *dev = video_drvdata(file);
991 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300992
Hans Verkuiledbd1382012-05-11 10:33:25 -0300993 return cx88_set_freq(core, f);
Mauro Carvalho Chehab54da49f2007-01-20 13:58:26 -0300994}
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -0300995
Trent Piephodbbff482007-01-22 23:31:53 -0300996#ifdef CONFIG_VIDEO_ADV_DEBUG
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200997static int vidioc_g_register(struct file *file, void *fh,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200998 struct v4l2_dbg_register *reg)
Trent Piephodbbff482007-01-22 23:31:53 -0300999{
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001000 struct cx8800_dev *dev = video_drvdata(file);
1001 struct cx88_core *core = dev->core;
Trent Piephodbbff482007-01-22 23:31:53 -03001002
Trent Piephodbbff482007-01-22 23:31:53 -03001003 /* cx2388x has a 24-bit register space */
Hans Verkuil7feeb142013-05-29 06:22:02 -03001004 reg->val = cx_read(reg->reg & 0xfffffc);
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001005 reg->size = 4;
Trent Piephodbbff482007-01-22 23:31:53 -03001006 return 0;
1007}
1008
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001009static int vidioc_s_register(struct file *file, void *fh,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -02001010 const struct v4l2_dbg_register *reg)
Trent Piephodbbff482007-01-22 23:31:53 -03001011{
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001012 struct cx8800_dev *dev = video_drvdata(file);
1013 struct cx88_core *core = dev->core;
Trent Piephodbbff482007-01-22 23:31:53 -03001014
Hans Verkuil7feeb142013-05-29 06:22:02 -03001015 cx_write(reg->reg & 0xfffffc, reg->val);
Trent Piephodbbff482007-01-22 23:31:53 -03001016 return 0;
1017}
1018#endif
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020/* ----------------------------------------------------------- */
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001021/* RADIO ESPECIFIC IOCTLS */
1022/* ----------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001024static int radio_g_tuner(struct file *file, void *priv,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -02001025 struct v4l2_tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001027 struct cx8800_dev *dev = video_drvdata(file);
1028 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001029
1030 if (unlikely(t->index > 0))
1031 return -EINVAL;
1032
Mauro Carvalho Chehabcc1e6312018-09-10 16:20:42 -04001033 strscpy(t->name, "Radio", sizeof(t->name));
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001034
Hans Verkuilb8341e12009-03-29 08:26:01 -03001035 call_all(core, tuner, g_tuner, t);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001036 return 0;
1037}
1038
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001039static int radio_s_tuner(struct file *file, void *priv,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -02001040 const struct v4l2_tuner *t)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001041{
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001042 struct cx8800_dev *dev = video_drvdata(file);
1043 struct cx88_core *core = dev->core;
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001044
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001045 if (t->index != 0)
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001046 return -EINVAL;
1047
Hans Verkuilb8341e12009-03-29 08:26:01 -03001048 call_all(core, tuner, s_tuner, t);
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001049 return 0;
1050}
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052/* ----------------------------------------------------------- */
1053
lawrence rust2e4e98e2010-08-25 09:50:20 -03001054static const char *cx88_vid_irqs[32] = {
Mauro Carvalho Chehab41ef7c12005-07-12 13:58:44 -07001055 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1056 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1057 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1058 "y_sync", "u_sync", "v_sync", "vbi_sync",
1059 "opc_err", "par_err", "rip_err", "pci_abort",
1060};
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062static void cx8800_vid_irq(struct cx8800_dev *dev)
1063{
1064 struct cx88_core *core = dev->core;
1065 u32 status, mask, count;
1066
1067 status = cx_read(MO_VID_INTSTAT);
1068 mask = cx_read(MO_VID_INTMSK);
1069 if (0 == (status & mask))
1070 return;
1071 cx_write(MO_VID_INTSTAT, status);
1072 if (irq_debug || (status & mask & ~0xff))
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001073 cx88_print_irqbits("irq vid",
Mauro Carvalho Chehab66623a02007-03-29 08:47:04 -03001074 cx88_vid_irqs, ARRAY_SIZE(cx88_vid_irqs),
1075 status, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 /* risc op code error */
1078 if (status & (1 << 16)) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001079 pr_warn("video risc op code error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 cx_clear(MO_VID_DMACNTRL, 0x11);
1081 cx_clear(VID_CAPTURE_CONTROL, 0x06);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001082 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 }
1084
1085 /* risc1 y */
1086 if (status & 0x01) {
1087 spin_lock(&dev->slock);
1088 count = cx_read(MO_VIDY_GPCNT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001089 cx88_wakeup(core, &dev->vidq, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 spin_unlock(&dev->slock);
1091 }
1092
1093 /* risc1 vbi */
1094 if (status & 0x08) {
1095 spin_lock(&dev->slock);
1096 count = cx_read(MO_VBI_GPCNT);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001097 cx88_wakeup(core, &dev->vbiq, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 spin_unlock(&dev->slock);
1099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100}
1101
David Howells7d12e782006-10-05 14:55:46 +01001102static irqreturn_t cx8800_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
1104 struct cx8800_dev *dev = dev_id;
1105 struct cx88_core *core = dev->core;
1106 u32 status;
1107 int loop, handled = 0;
1108
1109 for (loop = 0; loop < 10; loop++) {
Trent Piepho8ddac9e2007-08-18 06:57:55 -03001110 status = cx_read(MO_PCI_INTSTAT) &
1111 (core->pci_irqmask | PCI_INT_VIDINT);
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001112 if (status == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 goto out;
1114 cx_write(MO_PCI_INTSTAT, status);
1115 handled = 1;
1116
1117 if (status & core->pci_irqmask)
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001118 cx88_core_irq(core, status);
Trent Piepho8ddac9e2007-08-18 06:57:55 -03001119 if (status & PCI_INT_VIDINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 cx8800_vid_irq(dev);
Peter Senna Tschudinc2c1b412012-09-28 05:37:22 -03001121 }
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001122 if (loop == 10) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001123 pr_warn("irq loop -- clearing mask\n");
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001124 cx_write(MO_PCI_INTMSK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
1126
1127 out:
1128 return IRQ_RETVAL(handled);
1129}
1130
1131/* ----------------------------------------------------------- */
1132/* exported stuff */
1133
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001134static const struct v4l2_file_operations video_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 .owner = THIS_MODULE,
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001136 .open = v4l2_fh_open,
1137 .release = vb2_fop_release,
1138 .read = vb2_fop_read,
1139 .poll = vb2_fop_poll,
1140 .mmap = vb2_fop_mmap,
Mauro Carvalho Chehabb61872642011-02-13 21:52:02 -03001141 .unlocked_ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142};
1143
Hans Verkuila3998102008-07-21 02:57:38 -03001144static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001145 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001146 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1147 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1148 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1149 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001150 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1151 .vidioc_querybuf = vb2_ioctl_querybuf,
1152 .vidioc_qbuf = vb2_ioctl_qbuf,
1153 .vidioc_dqbuf = vb2_ioctl_dqbuf,
Hans Verkuil48d68802012-05-25 12:15:30 -03001154 .vidioc_g_std = vidioc_g_std,
Hans Verkuilf33e9862012-05-25 12:04:10 -03001155 .vidioc_s_std = vidioc_s_std,
1156 .vidioc_enum_input = vidioc_enum_input,
1157 .vidioc_g_input = vidioc_g_input,
1158 .vidioc_s_input = vidioc_s_input,
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001159 .vidioc_streamon = vb2_ioctl_streamon,
1160 .vidioc_streamoff = vb2_ioctl_streamoff,
Hans Verkuilf33e9862012-05-25 12:04:10 -03001161 .vidioc_g_tuner = vidioc_g_tuner,
1162 .vidioc_s_tuner = vidioc_s_tuner,
1163 .vidioc_g_frequency = vidioc_g_frequency,
1164 .vidioc_s_frequency = vidioc_s_frequency,
1165 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1166 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Hans Verkuilf33e9862012-05-25 12:04:10 -03001167#ifdef CONFIG_VIDEO_ADV_DEBUG
1168 .vidioc_g_register = vidioc_g_register,
1169 .vidioc_s_register = vidioc_s_register,
1170#endif
1171};
1172
1173static const struct video_device cx8800_video_template = {
1174 .name = "cx8800-video",
1175 .fops = &video_fops,
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001176 .ioctl_ops = &video_ioctl_ops,
Hans Verkuilf33e9862012-05-25 12:04:10 -03001177 .tvnorms = CX88_NORMS,
Hans Verkuilf33e9862012-05-25 12:04:10 -03001178};
1179
1180static const struct v4l2_ioctl_ops vbi_ioctl_ops = {
1181 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001182 .vidioc_g_fmt_vbi_cap = cx8800_vbi_fmt,
1183 .vidioc_try_fmt_vbi_cap = cx8800_vbi_fmt,
1184 .vidioc_s_fmt_vbi_cap = cx8800_vbi_fmt,
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001185 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1186 .vidioc_querybuf = vb2_ioctl_querybuf,
1187 .vidioc_qbuf = vb2_ioctl_qbuf,
1188 .vidioc_dqbuf = vb2_ioctl_dqbuf,
Hans Verkuil48d68802012-05-25 12:15:30 -03001189 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001190 .vidioc_s_std = vidioc_s_std,
1191 .vidioc_enum_input = vidioc_enum_input,
1192 .vidioc_g_input = vidioc_g_input,
1193 .vidioc_s_input = vidioc_s_input,
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001194 .vidioc_streamon = vb2_ioctl_streamon,
1195 .vidioc_streamoff = vb2_ioctl_streamoff,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001196 .vidioc_g_tuner = vidioc_g_tuner,
1197 .vidioc_s_tuner = vidioc_s_tuner,
1198 .vidioc_g_frequency = vidioc_g_frequency,
1199 .vidioc_s_frequency = vidioc_s_frequency,
Trent Piephodbbff482007-01-22 23:31:53 -03001200#ifdef CONFIG_VIDEO_ADV_DEBUG
1201 .vidioc_g_register = vidioc_g_register,
1202 .vidioc_s_register = vidioc_s_register,
1203#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001204};
1205
Hans Verkuilf33e9862012-05-25 12:04:10 -03001206static const struct video_device cx8800_vbi_template = {
1207 .name = "cx8800-vbi",
Hans Verkuila3998102008-07-21 02:57:38 -03001208 .fops = &video_fops,
Hans Verkuilf33e9862012-05-25 12:04:10 -03001209 .ioctl_ops = &vbi_ioctl_ops,
Mauro Carvalho Chehab63ab1bd2007-01-20 13:58:33 -03001210 .tvnorms = CX88_NORMS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211};
1212
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001213static const struct v4l2_file_operations radio_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 .owner = THIS_MODULE,
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001215 .open = radio_open,
Hans Verkuil1a3c60a2012-05-11 11:25:03 -03001216 .poll = v4l2_ctrl_poll,
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001217 .release = v4l2_fh_release,
Mauro Carvalho Chehabb61872642011-02-13 21:52:02 -03001218 .unlocked_ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219};
1220
Hans Verkuila3998102008-07-21 02:57:38 -03001221static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Hans Verkuil902e1972012-05-09 16:23:07 -03001222 .vidioc_querycap = vidioc_querycap,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001223 .vidioc_g_tuner = radio_g_tuner,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001224 .vidioc_s_tuner = radio_s_tuner,
Mauro Carvalho Chehab8d87cb92007-01-20 13:58:17 -03001225 .vidioc_g_frequency = vidioc_g_frequency,
1226 .vidioc_s_frequency = vidioc_s_frequency,
Hans Verkuil1a3c60a2012-05-11 11:25:03 -03001227 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1228 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Trent Piephoa75d2042007-08-01 00:13:28 -03001229#ifdef CONFIG_VIDEO_ADV_DEBUG
1230 .vidioc_g_register = vidioc_g_register,
1231 .vidioc_s_register = vidioc_s_register,
1232#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233};
1234
lawrence rust2e4e98e2010-08-25 09:50:20 -03001235static const struct video_device cx8800_radio_template = {
Hans Verkuila3998102008-07-21 02:57:38 -03001236 .name = "cx8800-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03001237 .fops = &radio_fops,
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001238 .ioctl_ops = &radio_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03001239};
1240
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001241static const struct v4l2_ctrl_ops cx8800_ctrl_vid_ops = {
1242 .s_ctrl = cx8800_s_vid_ctrl,
1243};
1244
1245static const struct v4l2_ctrl_ops cx8800_ctrl_aud_ops = {
1246 .s_ctrl = cx8800_s_aud_ctrl,
Hans Verkuilbac63982012-06-10 07:39:52 -03001247};
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249/* ----------------------------------------------------------- */
1250
1251static void cx8800_unregister_video(struct cx8800_dev *dev)
1252{
Hans Verkuil34080bc2015-03-09 13:34:00 -03001253 video_unregister_device(&dev->radio_dev);
1254 video_unregister_device(&dev->vbi_dev);
1255 video_unregister_device(&dev->video_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256}
1257
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001258static int cx8800_initdev(struct pci_dev *pci_dev,
1259 const struct pci_device_id *pci_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
1261 struct cx8800_dev *dev;
1262 struct cx88_core *core;
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001263 struct vb2_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 int err;
Hans Verkuilbac63982012-06-10 07:39:52 -03001265 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001267 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -02001268 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 /* pci init */
1272 dev->pci = pci_dev;
1273 if (pci_enable_device(pci_dev)) {
1274 err = -EIO;
1275 goto fail_free;
1276 }
1277 core = cx88_core_get(dev->pci);
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -02001278 if (!core) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 err = -EINVAL;
1280 goto fail_free;
1281 }
1282 dev->core = core;
1283
1284 /* print pci info */
Bjørn Morkabd34d82011-03-21 11:35:56 -03001285 dev->pci_rev = pci_dev->revision;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001286 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001287 pr_info("found at %s, rev: %d, irq: %d, latency: %d, mmio: 0x%llx\n",
1288 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
1289 dev->pci_lat,
1290 (unsigned long long)pci_resource_start(pci_dev, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 pci_set_master(pci_dev);
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001293 err = pci_set_dma_mask(pci_dev, DMA_BIT_MASK(32));
Christoph Hellwig1a47de62015-11-20 15:57:07 -08001294 if (err) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001295 pr_err("Oops: no 32bit PCI DMA ???\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 goto fail_core;
1297 }
1298
1299 /* initialize driver struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 spin_lock_init(&dev->slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 /* init video dma queues */
1303 INIT_LIST_HEAD(&dev->vidq.active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 /* init vbi dma queues */
1306 INIT_LIST_HEAD(&dev->vbiq.active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 /* get irq */
1309 err = request_irq(pci_dev->irq, cx8800_irq,
Michael Opdenacker3e018fe2013-10-13 02:49:29 -03001310 IRQF_SHARED, core->name, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if (err < 0) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001312 pr_err("can't get IRQ %d\n", pci_dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 goto fail_core;
1314 }
1315 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1316
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001317 for (i = 0; i < CX8800_AUD_CTLS; i++) {
1318 const struct cx88_ctrl *cc = &cx8800_aud_ctls[i];
Hans Verkuilbac63982012-06-10 07:39:52 -03001319 struct v4l2_ctrl *vc;
1320
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001321 vc = v4l2_ctrl_new_std(&core->audio_hdl, &cx8800_ctrl_aud_ops,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -02001322 cc->id, cc->minimum, cc->maximum,
1323 cc->step, cc->default_value);
1324 if (!vc) {
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001325 err = core->audio_hdl.error;
Hans Verkuilbac63982012-06-10 07:39:52 -03001326 goto fail_core;
1327 }
1328 vc->priv = (void *)cc;
1329 }
1330
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001331 for (i = 0; i < CX8800_VID_CTLS; i++) {
1332 const struct cx88_ctrl *cc = &cx8800_vid_ctls[i];
1333 struct v4l2_ctrl *vc;
1334
1335 vc = v4l2_ctrl_new_std(&core->video_hdl, &cx8800_ctrl_vid_ops,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -02001336 cc->id, cc->minimum, cc->maximum,
1337 cc->step, cc->default_value);
1338 if (!vc) {
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001339 err = core->video_hdl.error;
1340 goto fail_core;
1341 }
1342 vc->priv = (void *)cc;
1343 if (vc->id == V4L2_CID_CHROMA_AGC)
1344 core->chroma_agc = vc;
1345 }
Hans Verkuilda1b1ae2018-05-21 04:54:36 -04001346 v4l2_ctrl_add_handler(&core->video_hdl, &core->audio_hdl, NULL, false);
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 /* load and configure helper modules */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001349
Hans Verkuilfacd2362013-05-26 07:56:06 -03001350 if (core->board.audio_chip == CX88_AUDIO_WM8775) {
Lawrence Rust69518032011-02-06 17:46:12 -03001351 struct i2c_board_info wm8775_info = {
1352 .type = "wm8775",
1353 .addr = 0x36 >> 1,
1354 .platform_data = &core->wm8775_data,
1355 };
1356 struct v4l2_subdev *sd;
1357
1358 if (core->boardnr == CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1)
1359 core->wm8775_data.is_nova_s = true;
1360 else
1361 core->wm8775_data.is_nova_s = false;
1362
1363 sd = v4l2_i2c_new_subdev_board(&core->v4l2_dev, &core->i2c_adap,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -02001364 &wm8775_info, NULL);
1365 if (sd) {
Hans Verkuilbac63982012-06-10 07:39:52 -03001366 core->sd_wm8775 = sd;
Lawrence Rust69518032011-02-06 17:46:12 -03001367 sd->grp_id = WM8775_GID;
Hans Verkuilbac63982012-06-10 07:39:52 -03001368 }
Lawrence Rust69518032011-02-06 17:46:12 -03001369 }
Hans Verkuilb8341e12009-03-29 08:26:01 -03001370
Hans Verkuilfacd2362013-05-26 07:56:06 -03001371 if (core->board.audio_chip == CX88_AUDIO_TVAUDIO) {
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -02001372 /*
1373 * This probes for a tda9874 as is used on some
1374 * Pixelview Ultra boards.
1375 */
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -03001376 v4l2_i2c_new_subdev(&core->v4l2_dev, &core->i2c_adap,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -02001377 "tvaudio", 0, I2C_ADDRS(0xb0 >> 1));
Hans Verkuilb8341e12009-03-29 08:26:01 -03001378 }
Steven Toth30579062006-09-25 12:43:42 -03001379
Michael Krufky6fcecce2007-08-24 01:32:31 -03001380 switch (core->boardnr) {
1381 case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
Hans Verkuilb8341e12009-03-29 08:26:01 -03001382 case CX88_BOARD_DVICO_FUSIONHDTV_7_GOLD: {
lawrence rust2e4e98e2010-08-25 09:50:20 -03001383 static const struct i2c_board_info rtc_info = {
Hans Verkuilb8341e12009-03-29 08:26:01 -03001384 I2C_BOARD_INFO("isl1208", 0x6f)
1385 };
1386
Michael Krufky6fcecce2007-08-24 01:32:31 -03001387 request_module("rtc-isl1208");
Hans Verkuilb8341e12009-03-29 08:26:01 -03001388 core->i2c_rtc = i2c_new_device(&core->i2c_adap, &rtc_info);
1389 }
Mauro Carvalho Chehab06eeefe2017-05-18 08:13:28 -03001390 /* fall-through */
Michael Krufky8efd2e22008-04-22 14:45:14 -03001391 case CX88_BOARD_DVICO_FUSIONHDTV_5_PCI_NANO:
1392 request_module("ir-kbd-i2c");
Michael Krufky6fcecce2007-08-24 01:32:31 -03001393 }
1394
Mauro Carvalho Chehab121ec132011-02-14 07:01:51 -03001395 /* Sets device info at pci_dev */
1396 pci_set_drvdata(pci_dev, dev);
1397
Hans Verkuilccd6f1d2014-09-20 09:23:44 -03001398 dev->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
Hans Verkuilc5a86142012-05-11 10:45:18 -03001399
Hans Verkuil078859a2014-08-29 06:08:07 -03001400 /* Maintain a reference so cx88-blackbird can query the 8800 device. */
1401 core->v4ldev = dev;
1402
Mauro Carvalho Chehab121ec132011-02-14 07:01:51 -03001403 /* initial device configuration */
1404 mutex_lock(&core->lock);
Devin Heitmueller4e0973a2014-09-20 09:23:44 -03001405 cx88_set_tvnorm(core, V4L2_STD_NTSC_M);
Hans Verkuil8c7cb122012-05-11 09:07:45 -03001406 v4l2_ctrl_handler_setup(&core->video_hdl);
1407 v4l2_ctrl_handler_setup(&core->audio_hdl);
Mauro Carvalho Chehab121ec132011-02-14 07:01:51 -03001408 cx88_video_mux(core, 0);
1409
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001410 q = &dev->vb2_vidq;
1411 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1412 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1413 q->gfp_flags = GFP_DMA32;
1414 q->min_buffers_needed = 2;
1415 q->drv_priv = dev;
1416 q->buf_struct_size = sizeof(struct cx88_buffer);
1417 q->ops = &cx8800_video_qops;
1418 q->mem_ops = &vb2_dma_sg_memops;
1419 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1420 q->lock = &core->lock;
Hans Verkuil2bc46b32016-02-15 12:37:15 -02001421 q->dev = &dev->pci->dev;
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001422
1423 err = vb2_queue_init(q);
1424 if (err < 0)
1425 goto fail_unreg;
1426
1427 q = &dev->vb2_vbiq;
1428 q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1429 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1430 q->gfp_flags = GFP_DMA32;
1431 q->min_buffers_needed = 2;
1432 q->drv_priv = dev;
1433 q->buf_struct_size = sizeof(struct cx88_buffer);
1434 q->ops = &cx8800_vbi_qops;
1435 q->mem_ops = &vb2_dma_sg_memops;
1436 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1437 q->lock = &core->lock;
Hans Verkuil2bc46b32016-02-15 12:37:15 -02001438 q->dev = &dev->pci->dev;
Hans Verkuil0b6b6302014-09-20 09:22:18 -03001439
1440 err = vb2_queue_init(q);
1441 if (err < 0)
1442 goto fail_unreg;
1443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 /* register v4l devices */
Hans Verkuil34080bc2015-03-09 13:34:00 -03001445 cx88_vdev_init(core, dev->pci, &dev->video_dev,
1446 &cx8800_video_template, "video");
1447 video_set_drvdata(&dev->video_dev, dev);
1448 dev->video_dev.ctrl_handler = &core->video_hdl;
1449 dev->video_dev.queue = &dev->vb2_vidq;
Hans Verkuil21615362019-06-17 05:36:16 -04001450 dev->video_dev.device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
1451 V4L2_CAP_VIDEO_CAPTURE;
1452 if (core->board.tuner_type != UNSET)
1453 dev->video_dev.device_caps |= V4L2_CAP_TUNER;
Hans Verkuil34080bc2015-03-09 13:34:00 -03001454 err = video_register_device(&dev->video_dev, VFL_TYPE_GRABBER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 video_nr[core->nr]);
1456 if (err < 0) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001457 pr_err("can't register video device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 goto fail_unreg;
1459 }
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001460 pr_info("registered device %s [v4l2]\n",
1461 video_device_node_name(&dev->video_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Hans Verkuil34080bc2015-03-09 13:34:00 -03001463 cx88_vdev_init(core, dev->pci, &dev->vbi_dev,
1464 &cx8800_vbi_template, "vbi");
1465 video_set_drvdata(&dev->vbi_dev, dev);
1466 dev->vbi_dev.queue = &dev->vb2_vbiq;
Hans Verkuil21615362019-06-17 05:36:16 -04001467 dev->vbi_dev.device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
1468 V4L2_CAP_VBI_CAPTURE;
1469 if (core->board.tuner_type != UNSET)
1470 dev->vbi_dev.device_caps |= V4L2_CAP_TUNER;
Hans Verkuil34080bc2015-03-09 13:34:00 -03001471 err = video_register_device(&dev->vbi_dev, VFL_TYPE_VBI,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 vbi_nr[core->nr]);
1473 if (err < 0) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001474 pr_err("can't register vbi device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 goto fail_unreg;
1476 }
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001477 pr_info("registered device %s\n",
1478 video_device_node_name(&dev->vbi_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Trent Piepho6a59d642007-08-15 14:41:57 -03001480 if (core->board.radio.type == CX88_RADIO) {
Hans Verkuil34080bc2015-03-09 13:34:00 -03001481 cx88_vdev_init(core, dev->pci, &dev->radio_dev,
1482 &cx8800_radio_template, "radio");
1483 video_set_drvdata(&dev->radio_dev, dev);
1484 dev->radio_dev.ctrl_handler = &core->audio_hdl;
Hans Verkuil21615362019-06-17 05:36:16 -04001485 dev->radio_dev.device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
Hans Verkuil34080bc2015-03-09 13:34:00 -03001486 err = video_register_device(&dev->radio_dev, VFL_TYPE_RADIO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 radio_nr[core->nr]);
1488 if (err < 0) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001489 pr_err("can't register radio device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 goto fail_unreg;
1491 }
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001492 pr_info("registered device %s\n",
1493 video_device_node_name(&dev->radio_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 }
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 /* start tvaudio thread */
Hans Verkuilc39ba3302014-08-29 07:45:00 -03001497 if (core->board.tuner_type != UNSET) {
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -02001498 core->kthread = kthread_run(cx88_audio_thread,
1499 core, "cx88 tvaudio");
Cyrill Gorcunov32b78de2007-07-19 11:44:11 -03001500 if (IS_ERR(core->kthread)) {
1501 err = PTR_ERR(core->kthread);
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001502 pr_err("failed to create cx88 audio thread, err=%d\n",
1503 err);
Cyrill Gorcunov32b78de2007-07-19 11:44:11 -03001504 }
1505 }
Mauro Carvalho Chehab121ec132011-02-14 07:01:51 -03001506 mutex_unlock(&core->lock);
1507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 return 0;
1509
1510fail_unreg:
1511 cx8800_unregister_video(dev);
1512 free_irq(pci_dev->irq, dev);
Mauro Carvalho Chehab121ec132011-02-14 07:01:51 -03001513 mutex_unlock(&core->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514fail_core:
Hans Verkuil078859a2014-08-29 06:08:07 -03001515 core->v4ldev = NULL;
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001516 cx88_core_put(core, dev->pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517fail_free:
1518 kfree(dev);
1519 return err;
1520}
1521
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001522static void cx8800_finidev(struct pci_dev *pci_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001524 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001525 struct cx88_core *core = dev->core;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 /* stop thread */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001528 if (core->kthread) {
1529 kthread_stop(core->kthread);
1530 core->kthread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 }
1532
Marton Balintb12203d2008-03-26 02:07:35 -03001533 if (core->ir)
Mauro Carvalho Chehab92f4fc12010-03-31 16:07:49 -03001534 cx88_ir_stop(core);
Marton Balintb12203d2008-03-26 02:07:35 -03001535
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001536 cx88_shutdown(core); /* FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
1538 /* unregister stuff */
1539
1540 free_irq(pci_dev->irq, dev);
1541 cx8800_unregister_video(dev);
Hans Verkuil98822de2014-09-05 09:37:09 -03001542 pci_disable_device(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Hans Verkuil078859a2014-08-29 06:08:07 -03001544 core->v4ldev = NULL;
1545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 /* free memory */
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001547 cx88_core_put(core, dev->pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 kfree(dev);
1549}
1550
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03001551#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
1553{
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07001554 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 struct cx88_core *core = dev->core;
Alexey Khoroshilov5ddfbbb2013-04-13 18:52:04 -03001556 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 /* stop video+vbi capture */
Alexey Khoroshilov5ddfbbb2013-04-13 18:52:04 -03001559 spin_lock_irqsave(&dev->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 if (!list_empty(&dev->vidq.active)) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001561 pr_info("suspend video\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 stop_video_dma(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 }
1564 if (!list_empty(&dev->vbiq.active)) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001565 pr_info("suspend vbi\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 cx8800_stop_vbi_dma(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 }
Alexey Khoroshilov5ddfbbb2013-04-13 18:52:04 -03001568 spin_unlock_irqrestore(&dev->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Mauro Carvalho Chehab13595a52007-10-01 08:51:39 -03001570 if (core->ir)
Mauro Carvalho Chehab92f4fc12010-03-31 16:07:49 -03001571 cx88_ir_stop(core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 /* FIXME -- shutdown device */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001573 cx88_shutdown(core);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
1575 pci_save_state(pci_dev);
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -02001576 if (pci_set_power_state(pci_dev,
1577 pci_choose_state(pci_dev, state)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 pci_disable_device(pci_dev);
1579 dev->state.disabled = 1;
1580 }
1581 return 0;
1582}
1583
1584static int cx8800_resume(struct pci_dev *pci_dev)
1585{
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07001586 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 struct cx88_core *core = dev->core;
Alexey Khoroshilov5ddfbbb2013-04-13 18:52:04 -03001588 unsigned long flags;
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001589 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591 if (dev->state.disabled) {
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001592 err = pci_enable_device(pci_dev);
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001593 if (err) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001594 pr_err("can't enable device\n");
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001595 return err;
1596 }
1597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 dev->state.disabled = 0;
1599 }
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001600 err = pci_set_power_state(pci_dev, PCI_D0);
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001601 if (err) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001602 pr_err("can't set power state\n");
Mauro Carvalho Chehab08adb9e2005-09-09 13:03:55 -07001603 pci_disable_device(pci_dev);
1604 dev->state.disabled = 1;
1605
1606 return err;
1607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 pci_restore_state(pci_dev);
1609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 /* FIXME: re-initialize hardware */
Mauro Carvalho Chehabe52e98a2005-09-09 13:03:41 -07001611 cx88_reset(core);
Mauro Carvalho Chehab13595a52007-10-01 08:51:39 -03001612 if (core->ir)
Mauro Carvalho Chehab92f4fc12010-03-31 16:07:49 -03001613 cx88_ir_start(core);
Mauro Carvalho Chehab13595a52007-10-01 08:51:39 -03001614
1615 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
1617 /* restart video+vbi capture */
Alexey Khoroshilov5ddfbbb2013-04-13 18:52:04 -03001618 spin_lock_irqsave(&dev->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 if (!list_empty(&dev->vidq.active)) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001620 pr_info("resume video\n");
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001621 restart_video_queue(dev, &dev->vidq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 }
1623 if (!list_empty(&dev->vbiq.active)) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -02001624 pr_info("resume vbi\n");
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001625 cx8800_restart_vbi_queue(dev, &dev->vbiq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 }
Alexey Khoroshilov5ddfbbb2013-04-13 18:52:04 -03001627 spin_unlock_irqrestore(&dev->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
1629 return 0;
1630}
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03001631#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633/* ----------------------------------------------------------- */
1634
lawrence rust2e4e98e2010-08-25 09:50:20 -03001635static const struct pci_device_id cx8800_pci_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 {
1637 .vendor = 0x14f1,
1638 .device = 0x8800,
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07001639 .subvendor = PCI_ANY_ID,
1640 .subdevice = PCI_ANY_ID,
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02001641 }, {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 /* --- end of list --- */
1643 }
1644};
1645MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
1646
1647static struct pci_driver cx8800_pci_driver = {
Mauro Carvalho Chehabb45009b2005-06-23 22:05:03 -07001648 .name = "cx8800",
1649 .id_table = cx8800_pci_tbl,
1650 .probe = cx8800_initdev,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001651 .remove = cx8800_finidev,
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03001652#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 .suspend = cx8800_suspend,
1654 .resume = cx8800_resume,
Alexey Dobriyan17bc98a2006-08-12 22:01:27 -03001655#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656};
1657
Sachin Kamat06333e02013-09-20 05:32:07 -03001658module_pci_driver(cx8800_pci_driver);