xref: /OK3568_Linux_fs/kernel/drivers/media/pci/saa7146/mxb.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun     mxb - v4l2 driver for the Multimedia eXtension Board
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun     Copyright (C) 1998-2006 Michael Hunold <michael@mihu.de>
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun     Visit http://www.themm.net/~mihu/linux/saa7146/mxb.html
8*4882a593Smuzhiyun     for further details about this card.
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun */
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #define DEBUG_VARIABLE debug
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #include <media/drv-intf/saa7146_vv.h>
17*4882a593Smuzhiyun #include <media/tuner.h>
18*4882a593Smuzhiyun #include <media/v4l2-common.h>
19*4882a593Smuzhiyun #include <media/i2c/saa7115.h>
20*4882a593Smuzhiyun #include <linux/module.h>
21*4882a593Smuzhiyun #include <linux/kernel.h>
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #include "tea6415c.h"
24*4882a593Smuzhiyun #include "tea6420.h"
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun #define MXB_AUDIOS	6
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun #define I2C_SAA7111A  0x24
29*4882a593Smuzhiyun #define	I2C_TDA9840   0x42
30*4882a593Smuzhiyun #define	I2C_TEA6415C  0x43
31*4882a593Smuzhiyun #define	I2C_TEA6420_1 0x4c
32*4882a593Smuzhiyun #define	I2C_TEA6420_2 0x4d
33*4882a593Smuzhiyun #define	I2C_TUNER     0x60
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun #define MXB_BOARD_CAN_DO_VBI(dev)   (dev->revision != 0)
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun /* global variable */
38*4882a593Smuzhiyun static int mxb_num;
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun /* initial frequence the tuner will be tuned to.
41*4882a593Smuzhiyun    in verden (lower saxony, germany) 4148 is a
42*4882a593Smuzhiyun    channel called "phoenix" */
43*4882a593Smuzhiyun static int freq = 4148;
44*4882a593Smuzhiyun module_param(freq, int, 0644);
45*4882a593Smuzhiyun MODULE_PARM_DESC(freq, "initial frequency the tuner will be tuned to while setup");
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun static int debug;
48*4882a593Smuzhiyun module_param(debug, int, 0644);
49*4882a593Smuzhiyun MODULE_PARM_DESC(debug, "Turn on/off device debugging (default:off).");
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun #define MXB_INPUTS 4
52*4882a593Smuzhiyun enum { TUNER, AUX1, AUX3, AUX3_YC };
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun static struct v4l2_input mxb_inputs[MXB_INPUTS] = {
55*4882a593Smuzhiyun 	{ TUNER,   "Tuner",          V4L2_INPUT_TYPE_TUNER,  0x3f, 0,
56*4882a593Smuzhiyun 		V4L2_STD_PAL_BG | V4L2_STD_PAL_I, 0, V4L2_IN_CAP_STD },
57*4882a593Smuzhiyun 	{ AUX1,	   "AUX1",           V4L2_INPUT_TYPE_CAMERA, 0x3f, 0,
58*4882a593Smuzhiyun 		V4L2_STD_ALL, 0, V4L2_IN_CAP_STD },
59*4882a593Smuzhiyun 	{ AUX3,	   "AUX3 Composite", V4L2_INPUT_TYPE_CAMERA, 0x3f, 0,
60*4882a593Smuzhiyun 		V4L2_STD_ALL, 0, V4L2_IN_CAP_STD },
61*4882a593Smuzhiyun 	{ AUX3_YC, "AUX3 S-Video",   V4L2_INPUT_TYPE_CAMERA, 0x3f, 0,
62*4882a593Smuzhiyun 		V4L2_STD_ALL, 0, V4L2_IN_CAP_STD },
63*4882a593Smuzhiyun };
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun /* this array holds the information, which port of the saa7146 each
66*4882a593Smuzhiyun    input actually uses. the mxb uses port 0 for every input */
67*4882a593Smuzhiyun static struct {
68*4882a593Smuzhiyun 	int hps_source;
69*4882a593Smuzhiyun 	int hps_sync;
70*4882a593Smuzhiyun } input_port_selection[MXB_INPUTS] = {
71*4882a593Smuzhiyun 	{ SAA7146_HPS_SOURCE_PORT_A, SAA7146_HPS_SYNC_PORT_A },
72*4882a593Smuzhiyun 	{ SAA7146_HPS_SOURCE_PORT_A, SAA7146_HPS_SYNC_PORT_A },
73*4882a593Smuzhiyun 	{ SAA7146_HPS_SOURCE_PORT_A, SAA7146_HPS_SYNC_PORT_A },
74*4882a593Smuzhiyun 	{ SAA7146_HPS_SOURCE_PORT_A, SAA7146_HPS_SYNC_PORT_A },
75*4882a593Smuzhiyun };
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun /* this array holds the information of the audio source (mxb_audios),
78*4882a593Smuzhiyun    which has to be switched corresponding to the video source (mxb_channels) */
79*4882a593Smuzhiyun static int video_audio_connect[MXB_INPUTS] =
80*4882a593Smuzhiyun 	{ 0, 1, 3, 3 };
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun struct mxb_routing {
83*4882a593Smuzhiyun 	u32 input;
84*4882a593Smuzhiyun 	u32 output;
85*4882a593Smuzhiyun };
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun /* these are the available audio sources, which can switched
88*4882a593Smuzhiyun    to the line- and cd-output individually */
89*4882a593Smuzhiyun static struct v4l2_audio mxb_audios[MXB_AUDIOS] = {
90*4882a593Smuzhiyun 	    {
91*4882a593Smuzhiyun 		.index	= 0,
92*4882a593Smuzhiyun 		.name	= "Tuner",
93*4882a593Smuzhiyun 		.capability = V4L2_AUDCAP_STEREO,
94*4882a593Smuzhiyun 	} , {
95*4882a593Smuzhiyun 		.index	= 1,
96*4882a593Smuzhiyun 		.name	= "AUX1",
97*4882a593Smuzhiyun 		.capability = V4L2_AUDCAP_STEREO,
98*4882a593Smuzhiyun 	} , {
99*4882a593Smuzhiyun 		.index	= 2,
100*4882a593Smuzhiyun 		.name	= "AUX2",
101*4882a593Smuzhiyun 		.capability = V4L2_AUDCAP_STEREO,
102*4882a593Smuzhiyun 	} , {
103*4882a593Smuzhiyun 		.index	= 3,
104*4882a593Smuzhiyun 		.name	= "AUX3",
105*4882a593Smuzhiyun 		.capability = V4L2_AUDCAP_STEREO,
106*4882a593Smuzhiyun 	} , {
107*4882a593Smuzhiyun 		.index	= 4,
108*4882a593Smuzhiyun 		.name	= "Radio (X9)",
109*4882a593Smuzhiyun 		.capability = V4L2_AUDCAP_STEREO,
110*4882a593Smuzhiyun 	} , {
111*4882a593Smuzhiyun 		.index	= 5,
112*4882a593Smuzhiyun 		.name	= "CD-ROM (X10)",
113*4882a593Smuzhiyun 		.capability = V4L2_AUDCAP_STEREO,
114*4882a593Smuzhiyun 	}
115*4882a593Smuzhiyun };
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun /* These are the necessary input-output-pins for bringing one audio source
118*4882a593Smuzhiyun    (see above) to the CD-output. Note that gain is set to 0 in this table. */
119*4882a593Smuzhiyun static struct mxb_routing TEA6420_cd[MXB_AUDIOS + 1][2] = {
120*4882a593Smuzhiyun 	{ { 1, 1 }, { 1, 1 } },	/* Tuner */
121*4882a593Smuzhiyun 	{ { 5, 1 }, { 6, 1 } },	/* AUX 1 */
122*4882a593Smuzhiyun 	{ { 4, 1 }, { 6, 1 } },	/* AUX 2 */
123*4882a593Smuzhiyun 	{ { 3, 1 }, { 6, 1 } },	/* AUX 3 */
124*4882a593Smuzhiyun 	{ { 1, 1 }, { 3, 1 } },	/* Radio */
125*4882a593Smuzhiyun 	{ { 1, 1 }, { 2, 1 } },	/* CD-Rom */
126*4882a593Smuzhiyun 	{ { 6, 1 }, { 6, 1 } }	/* Mute */
127*4882a593Smuzhiyun };
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun /* These are the necessary input-output-pins for bringing one audio source
130*4882a593Smuzhiyun    (see above) to the line-output. Note that gain is set to 0 in this table. */
131*4882a593Smuzhiyun static struct mxb_routing TEA6420_line[MXB_AUDIOS + 1][2] = {
132*4882a593Smuzhiyun 	{ { 2, 3 }, { 1, 2 } },
133*4882a593Smuzhiyun 	{ { 5, 3 }, { 6, 2 } },
134*4882a593Smuzhiyun 	{ { 4, 3 }, { 6, 2 } },
135*4882a593Smuzhiyun 	{ { 3, 3 }, { 6, 2 } },
136*4882a593Smuzhiyun 	{ { 2, 3 }, { 3, 2 } },
137*4882a593Smuzhiyun 	{ { 2, 3 }, { 2, 2 } },
138*4882a593Smuzhiyun 	{ { 6, 3 }, { 6, 2 } }	/* Mute */
139*4882a593Smuzhiyun };
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun struct mxb
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun 	struct video_device	video_dev;
144*4882a593Smuzhiyun 	struct video_device	vbi_dev;
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	struct i2c_adapter	i2c_adapter;
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	struct v4l2_subdev	*saa7111a;
149*4882a593Smuzhiyun 	struct v4l2_subdev	*tda9840;
150*4882a593Smuzhiyun 	struct v4l2_subdev	*tea6415c;
151*4882a593Smuzhiyun 	struct v4l2_subdev	*tuner;
152*4882a593Smuzhiyun 	struct v4l2_subdev	*tea6420_1;
153*4882a593Smuzhiyun 	struct v4l2_subdev	*tea6420_2;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	int	cur_mode;	/* current audio mode (mono, stereo, ...) */
156*4882a593Smuzhiyun 	int	cur_input;	/* current input */
157*4882a593Smuzhiyun 	int	cur_audinput;	/* current audio input */
158*4882a593Smuzhiyun 	int	cur_mute;	/* current mute status */
159*4882a593Smuzhiyun 	struct v4l2_frequency	cur_freq;	/* current frequency the tuner is tuned to */
160*4882a593Smuzhiyun };
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun #define saa7111a_call(mxb, o, f, args...) \
163*4882a593Smuzhiyun 	v4l2_subdev_call(mxb->saa7111a, o, f, ##args)
164*4882a593Smuzhiyun #define tda9840_call(mxb, o, f, args...) \
165*4882a593Smuzhiyun 	v4l2_subdev_call(mxb->tda9840, o, f, ##args)
166*4882a593Smuzhiyun #define tea6415c_call(mxb, o, f, args...) \
167*4882a593Smuzhiyun 	v4l2_subdev_call(mxb->tea6415c, o, f, ##args)
168*4882a593Smuzhiyun #define tuner_call(mxb, o, f, args...) \
169*4882a593Smuzhiyun 	v4l2_subdev_call(mxb->tuner, o, f, ##args)
170*4882a593Smuzhiyun #define call_all(dev, o, f, args...) \
171*4882a593Smuzhiyun 	v4l2_device_call_until_err(&dev->v4l2_dev, 0, o, f, ##args)
172*4882a593Smuzhiyun 
mxb_update_audmode(struct mxb * mxb)173*4882a593Smuzhiyun static void mxb_update_audmode(struct mxb *mxb)
174*4882a593Smuzhiyun {
175*4882a593Smuzhiyun 	struct v4l2_tuner t = {
176*4882a593Smuzhiyun 		.audmode = mxb->cur_mode,
177*4882a593Smuzhiyun 	};
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	tda9840_call(mxb, tuner, s_tuner, &t);
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun 
tea6420_route(struct mxb * mxb,int idx)182*4882a593Smuzhiyun static inline void tea6420_route(struct mxb *mxb, int idx)
183*4882a593Smuzhiyun {
184*4882a593Smuzhiyun 	v4l2_subdev_call(mxb->tea6420_1, audio, s_routing,
185*4882a593Smuzhiyun 		TEA6420_cd[idx][0].input, TEA6420_cd[idx][0].output, 0);
186*4882a593Smuzhiyun 	v4l2_subdev_call(mxb->tea6420_2, audio, s_routing,
187*4882a593Smuzhiyun 		TEA6420_cd[idx][1].input, TEA6420_cd[idx][1].output, 0);
188*4882a593Smuzhiyun 	v4l2_subdev_call(mxb->tea6420_1, audio, s_routing,
189*4882a593Smuzhiyun 		TEA6420_line[idx][0].input, TEA6420_line[idx][0].output, 0);
190*4882a593Smuzhiyun 	v4l2_subdev_call(mxb->tea6420_2, audio, s_routing,
191*4882a593Smuzhiyun 		TEA6420_line[idx][1].input, TEA6420_line[idx][1].output, 0);
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun static struct saa7146_extension extension;
195*4882a593Smuzhiyun 
mxb_s_ctrl(struct v4l2_ctrl * ctrl)196*4882a593Smuzhiyun static int mxb_s_ctrl(struct v4l2_ctrl *ctrl)
197*4882a593Smuzhiyun {
198*4882a593Smuzhiyun 	struct saa7146_dev *dev = container_of(ctrl->handler,
199*4882a593Smuzhiyun 				struct saa7146_dev, ctrl_handler);
200*4882a593Smuzhiyun 	struct mxb *mxb = dev->ext_priv;
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	switch (ctrl->id) {
203*4882a593Smuzhiyun 	case V4L2_CID_AUDIO_MUTE:
204*4882a593Smuzhiyun 		mxb->cur_mute = ctrl->val;
205*4882a593Smuzhiyun 		/* switch the audio-source */
206*4882a593Smuzhiyun 		tea6420_route(mxb, ctrl->val ? 6 :
207*4882a593Smuzhiyun 				video_audio_connect[mxb->cur_input]);
208*4882a593Smuzhiyun 		break;
209*4882a593Smuzhiyun 	default:
210*4882a593Smuzhiyun 		return -EINVAL;
211*4882a593Smuzhiyun 	}
212*4882a593Smuzhiyun 	return 0;
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun static const struct v4l2_ctrl_ops mxb_ctrl_ops = {
216*4882a593Smuzhiyun 	.s_ctrl = mxb_s_ctrl,
217*4882a593Smuzhiyun };
218*4882a593Smuzhiyun 
mxb_probe(struct saa7146_dev * dev)219*4882a593Smuzhiyun static int mxb_probe(struct saa7146_dev *dev)
220*4882a593Smuzhiyun {
221*4882a593Smuzhiyun 	struct v4l2_ctrl_handler *hdl = &dev->ctrl_handler;
222*4882a593Smuzhiyun 	struct mxb *mxb = NULL;
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	v4l2_ctrl_new_std(hdl, &mxb_ctrl_ops,
225*4882a593Smuzhiyun 			V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
226*4882a593Smuzhiyun 	if (hdl->error)
227*4882a593Smuzhiyun 		return hdl->error;
228*4882a593Smuzhiyun 	mxb = kzalloc(sizeof(struct mxb), GFP_KERNEL);
229*4882a593Smuzhiyun 	if (mxb == NULL) {
230*4882a593Smuzhiyun 		DEB_D("not enough kernel memory\n");
231*4882a593Smuzhiyun 		return -ENOMEM;
232*4882a593Smuzhiyun 	}
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	snprintf(mxb->i2c_adapter.name, sizeof(mxb->i2c_adapter.name), "mxb%d", mxb_num);
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 	saa7146_i2c_adapter_prepare(dev, &mxb->i2c_adapter, SAA7146_I2C_BUS_BIT_RATE_480);
238*4882a593Smuzhiyun 	if (i2c_add_adapter(&mxb->i2c_adapter) < 0) {
239*4882a593Smuzhiyun 		DEB_S("cannot register i2c-device. skipping.\n");
240*4882a593Smuzhiyun 		kfree(mxb);
241*4882a593Smuzhiyun 		return -EFAULT;
242*4882a593Smuzhiyun 	}
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	mxb->saa7111a = v4l2_i2c_new_subdev(&dev->v4l2_dev, &mxb->i2c_adapter,
245*4882a593Smuzhiyun 			"saa7111", I2C_SAA7111A, NULL);
246*4882a593Smuzhiyun 	mxb->tea6420_1 = v4l2_i2c_new_subdev(&dev->v4l2_dev, &mxb->i2c_adapter,
247*4882a593Smuzhiyun 			"tea6420", I2C_TEA6420_1, NULL);
248*4882a593Smuzhiyun 	mxb->tea6420_2 = v4l2_i2c_new_subdev(&dev->v4l2_dev, &mxb->i2c_adapter,
249*4882a593Smuzhiyun 			"tea6420", I2C_TEA6420_2, NULL);
250*4882a593Smuzhiyun 	mxb->tea6415c = v4l2_i2c_new_subdev(&dev->v4l2_dev, &mxb->i2c_adapter,
251*4882a593Smuzhiyun 			"tea6415c", I2C_TEA6415C, NULL);
252*4882a593Smuzhiyun 	mxb->tda9840 = v4l2_i2c_new_subdev(&dev->v4l2_dev, &mxb->i2c_adapter,
253*4882a593Smuzhiyun 			"tda9840", I2C_TDA9840, NULL);
254*4882a593Smuzhiyun 	mxb->tuner = v4l2_i2c_new_subdev(&dev->v4l2_dev, &mxb->i2c_adapter,
255*4882a593Smuzhiyun 			"tuner", I2C_TUNER, NULL);
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	/* check if all devices are present */
258*4882a593Smuzhiyun 	if (!mxb->tea6420_1 || !mxb->tea6420_2 || !mxb->tea6415c ||
259*4882a593Smuzhiyun 	    !mxb->tda9840 || !mxb->saa7111a || !mxb->tuner) {
260*4882a593Smuzhiyun 		pr_err("did not find all i2c devices. aborting\n");
261*4882a593Smuzhiyun 		i2c_del_adapter(&mxb->i2c_adapter);
262*4882a593Smuzhiyun 		kfree(mxb);
263*4882a593Smuzhiyun 		return -ENODEV;
264*4882a593Smuzhiyun 	}
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 	/* all devices are present, probe was successful */
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 	/* we store the pointer in our private data field */
269*4882a593Smuzhiyun 	dev->ext_priv = mxb;
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 	v4l2_ctrl_handler_setup(hdl);
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	return 0;
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun /* some init data for the saa7740, the so-called 'sound arena module'.
277*4882a593Smuzhiyun    there are no specs available, so we simply use some init values */
278*4882a593Smuzhiyun static struct {
279*4882a593Smuzhiyun 	int	length;
280*4882a593Smuzhiyun 	char	data[9];
281*4882a593Smuzhiyun } mxb_saa7740_init[] = {
282*4882a593Smuzhiyun 	{ 3, { 0x80, 0x00, 0x00 } },{ 3, { 0x80, 0x89, 0x00 } },
283*4882a593Smuzhiyun 	{ 3, { 0x80, 0xb0, 0x0a } },{ 3, { 0x00, 0x00, 0x00 } },
284*4882a593Smuzhiyun 	{ 3, { 0x49, 0x00, 0x00 } },{ 3, { 0x4a, 0x00, 0x00 } },
285*4882a593Smuzhiyun 	{ 3, { 0x4b, 0x00, 0x00 } },{ 3, { 0x4c, 0x00, 0x00 } },
286*4882a593Smuzhiyun 	{ 3, { 0x4d, 0x00, 0x00 } },{ 3, { 0x4e, 0x00, 0x00 } },
287*4882a593Smuzhiyun 	{ 3, { 0x4f, 0x00, 0x00 } },{ 3, { 0x50, 0x00, 0x00 } },
288*4882a593Smuzhiyun 	{ 3, { 0x51, 0x00, 0x00 } },{ 3, { 0x52, 0x00, 0x00 } },
289*4882a593Smuzhiyun 	{ 3, { 0x53, 0x00, 0x00 } },{ 3, { 0x54, 0x00, 0x00 } },
290*4882a593Smuzhiyun 	{ 3, { 0x55, 0x00, 0x00 } },{ 3, { 0x56, 0x00, 0x00 } },
291*4882a593Smuzhiyun 	{ 3, { 0x57, 0x00, 0x00 } },{ 3, { 0x58, 0x00, 0x00 } },
292*4882a593Smuzhiyun 	{ 3, { 0x59, 0x00, 0x00 } },{ 3, { 0x5a, 0x00, 0x00 } },
293*4882a593Smuzhiyun 	{ 3, { 0x5b, 0x00, 0x00 } },{ 3, { 0x5c, 0x00, 0x00 } },
294*4882a593Smuzhiyun 	{ 3, { 0x5d, 0x00, 0x00 } },{ 3, { 0x5e, 0x00, 0x00 } },
295*4882a593Smuzhiyun 	{ 3, { 0x5f, 0x00, 0x00 } },{ 3, { 0x60, 0x00, 0x00 } },
296*4882a593Smuzhiyun 	{ 3, { 0x61, 0x00, 0x00 } },{ 3, { 0x62, 0x00, 0x00 } },
297*4882a593Smuzhiyun 	{ 3, { 0x63, 0x00, 0x00 } },{ 3, { 0x64, 0x00, 0x00 } },
298*4882a593Smuzhiyun 	{ 3, { 0x65, 0x00, 0x00 } },{ 3, { 0x66, 0x00, 0x00 } },
299*4882a593Smuzhiyun 	{ 3, { 0x67, 0x00, 0x00 } },{ 3, { 0x68, 0x00, 0x00 } },
300*4882a593Smuzhiyun 	{ 3, { 0x69, 0x00, 0x00 } },{ 3, { 0x6a, 0x00, 0x00 } },
301*4882a593Smuzhiyun 	{ 3, { 0x6b, 0x00, 0x00 } },{ 3, { 0x6c, 0x00, 0x00 } },
302*4882a593Smuzhiyun 	{ 3, { 0x6d, 0x00, 0x00 } },{ 3, { 0x6e, 0x00, 0x00 } },
303*4882a593Smuzhiyun 	{ 3, { 0x6f, 0x00, 0x00 } },{ 3, { 0x70, 0x00, 0x00 } },
304*4882a593Smuzhiyun 	{ 3, { 0x71, 0x00, 0x00 } },{ 3, { 0x72, 0x00, 0x00 } },
305*4882a593Smuzhiyun 	{ 3, { 0x73, 0x00, 0x00 } },{ 3, { 0x74, 0x00, 0x00 } },
306*4882a593Smuzhiyun 	{ 3, { 0x75, 0x00, 0x00 } },{ 3, { 0x76, 0x00, 0x00 } },
307*4882a593Smuzhiyun 	{ 3, { 0x77, 0x00, 0x00 } },{ 3, { 0x41, 0x00, 0x42 } },
308*4882a593Smuzhiyun 	{ 3, { 0x42, 0x10, 0x42 } },{ 3, { 0x43, 0x20, 0x42 } },
309*4882a593Smuzhiyun 	{ 3, { 0x44, 0x30, 0x42 } },{ 3, { 0x45, 0x00, 0x01 } },
310*4882a593Smuzhiyun 	{ 3, { 0x46, 0x00, 0x01 } },{ 3, { 0x47, 0x00, 0x01 } },
311*4882a593Smuzhiyun 	{ 3, { 0x48, 0x00, 0x01 } },
312*4882a593Smuzhiyun 	{ 9, { 0x01, 0x03, 0xc5, 0x5c, 0x7a, 0x85, 0x01, 0x00, 0x54 } },
313*4882a593Smuzhiyun 	{ 9, { 0x21, 0x03, 0xc5, 0x5c, 0x7a, 0x85, 0x01, 0x00, 0x54 } },
314*4882a593Smuzhiyun 	{ 9, { 0x09, 0x0b, 0xb4, 0x6b, 0x74, 0x85, 0x95, 0x00, 0x34 } },
315*4882a593Smuzhiyun 	{ 9, { 0x29, 0x0b, 0xb4, 0x6b, 0x74, 0x85, 0x95, 0x00, 0x34 } },
316*4882a593Smuzhiyun 	{ 9, { 0x11, 0x17, 0x43, 0x62, 0x68, 0x89, 0xd1, 0xff, 0xb0 } },
317*4882a593Smuzhiyun 	{ 9, { 0x31, 0x17, 0x43, 0x62, 0x68, 0x89, 0xd1, 0xff, 0xb0 } },
318*4882a593Smuzhiyun 	{ 9, { 0x19, 0x20, 0x62, 0x51, 0x5a, 0x95, 0x19, 0x01, 0x50 } },
319*4882a593Smuzhiyun 	{ 9, { 0x39, 0x20, 0x62, 0x51, 0x5a, 0x95, 0x19, 0x01, 0x50 } },
320*4882a593Smuzhiyun 	{ 9, { 0x05, 0x3e, 0xd2, 0x69, 0x4e, 0x9a, 0x51, 0x00, 0xf0 } },
321*4882a593Smuzhiyun 	{ 9, { 0x25, 0x3e, 0xd2, 0x69, 0x4e, 0x9a, 0x51, 0x00, 0xf0 } },
322*4882a593Smuzhiyun 	{ 9, { 0x0d, 0x3d, 0xa1, 0x40, 0x7d, 0x9f, 0x29, 0xfe, 0x14 } },
323*4882a593Smuzhiyun 	{ 9, { 0x2d, 0x3d, 0xa1, 0x40, 0x7d, 0x9f, 0x29, 0xfe, 0x14 } },
324*4882a593Smuzhiyun 	{ 9, { 0x15, 0x73, 0xa1, 0x50, 0x5d, 0xa6, 0xf5, 0xfe, 0x38 } },
325*4882a593Smuzhiyun 	{ 9, { 0x35, 0x73, 0xa1, 0x50, 0x5d, 0xa6, 0xf5, 0xfe, 0x38 } },
326*4882a593Smuzhiyun 	{ 9, { 0x1d, 0xed, 0xd0, 0x68, 0x29, 0xb4, 0xe1, 0x00, 0xb8 } },
327*4882a593Smuzhiyun 	{ 9, { 0x3d, 0xed, 0xd0, 0x68, 0x29, 0xb4, 0xe1, 0x00, 0xb8 } },
328*4882a593Smuzhiyun 	{ 3, { 0x80, 0xb3, 0x0a } },
329*4882a593Smuzhiyun 	{-1, { 0 } }
330*4882a593Smuzhiyun };
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun /* bring hardware to a sane state. this has to be done, just in case someone
333*4882a593Smuzhiyun    wants to capture from this device before it has been properly initialized.
334*4882a593Smuzhiyun    the capture engine would badly fail, because no valid signal arrives on the
335*4882a593Smuzhiyun    saa7146, thus leading to timeouts and stuff. */
mxb_init_done(struct saa7146_dev * dev)336*4882a593Smuzhiyun static int mxb_init_done(struct saa7146_dev* dev)
337*4882a593Smuzhiyun {
338*4882a593Smuzhiyun 	struct mxb* mxb = (struct mxb*)dev->ext_priv;
339*4882a593Smuzhiyun 	struct i2c_msg msg;
340*4882a593Smuzhiyun 	struct tuner_setup tun_setup;
341*4882a593Smuzhiyun 	v4l2_std_id std = V4L2_STD_PAL_BG;
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun 	int i = 0, err = 0;
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun 	/* mute audio on tea6420s */
346*4882a593Smuzhiyun 	tea6420_route(mxb, 6);
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 	/* select video mode in saa7111a */
349*4882a593Smuzhiyun 	saa7111a_call(mxb, video, s_std, std);
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 	/* select tuner-output on saa7111a */
352*4882a593Smuzhiyun 	i = 0;
353*4882a593Smuzhiyun 	saa7111a_call(mxb, video, s_routing, SAA7115_COMPOSITE0,
354*4882a593Smuzhiyun 		SAA7111_FMT_CCIR, 0);
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 	/* select a tuner type */
357*4882a593Smuzhiyun 	tun_setup.mode_mask = T_ANALOG_TV;
358*4882a593Smuzhiyun 	tun_setup.addr = ADDR_UNSET;
359*4882a593Smuzhiyun 	tun_setup.type = TUNER_PHILIPS_PAL;
360*4882a593Smuzhiyun 	tuner_call(mxb, tuner, s_type_addr, &tun_setup);
361*4882a593Smuzhiyun 	/* tune in some frequency on tuner */
362*4882a593Smuzhiyun 	mxb->cur_freq.tuner = 0;
363*4882a593Smuzhiyun 	mxb->cur_freq.type = V4L2_TUNER_ANALOG_TV;
364*4882a593Smuzhiyun 	mxb->cur_freq.frequency = freq;
365*4882a593Smuzhiyun 	tuner_call(mxb, tuner, s_frequency, &mxb->cur_freq);
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 	/* set a default video standard */
368*4882a593Smuzhiyun 	/* These two gpio calls set the GPIO pins that control the tda9820 */
369*4882a593Smuzhiyun 	saa7146_write(dev, GPIO_CTRL, 0x00404050);
370*4882a593Smuzhiyun 	saa7111a_call(mxb, core, s_gpio, 1);
371*4882a593Smuzhiyun 	saa7111a_call(mxb, video, s_std, std);
372*4882a593Smuzhiyun 	tuner_call(mxb, video, s_std, std);
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun 	/* switch to tuner-channel on tea6415c */
375*4882a593Smuzhiyun 	tea6415c_call(mxb, video, s_routing, 3, 17, 0);
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun 	/* select tuner-output on multicable on tea6415c */
378*4882a593Smuzhiyun 	tea6415c_call(mxb, video, s_routing, 3, 13, 0);
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun 	/* the rest for mxb */
381*4882a593Smuzhiyun 	mxb->cur_input = 0;
382*4882a593Smuzhiyun 	mxb->cur_audinput = video_audio_connect[mxb->cur_input];
383*4882a593Smuzhiyun 	mxb->cur_mute = 1;
384*4882a593Smuzhiyun 
385*4882a593Smuzhiyun 	mxb->cur_mode = V4L2_TUNER_MODE_STEREO;
386*4882a593Smuzhiyun 	mxb_update_audmode(mxb);
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun 	/* check if the saa7740 (aka 'sound arena module') is present
389*4882a593Smuzhiyun 	   on the mxb. if so, we must initialize it. due to lack of
390*4882a593Smuzhiyun 	   information about the saa7740, the values were reverse
391*4882a593Smuzhiyun 	   engineered. */
392*4882a593Smuzhiyun 	msg.addr = 0x1b;
393*4882a593Smuzhiyun 	msg.flags = 0;
394*4882a593Smuzhiyun 	msg.len = mxb_saa7740_init[0].length;
395*4882a593Smuzhiyun 	msg.buf = &mxb_saa7740_init[0].data[0];
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 	err = i2c_transfer(&mxb->i2c_adapter, &msg, 1);
398*4882a593Smuzhiyun 	if (err == 1) {
399*4882a593Smuzhiyun 		/* the sound arena module is a pos, that's probably the reason
400*4882a593Smuzhiyun 		   philips refuses to hand out a datasheet for the saa7740...
401*4882a593Smuzhiyun 		   it seems to screw up the i2c bus, so we disable fast irq
402*4882a593Smuzhiyun 		   based i2c transactions here and rely on the slow and safe
403*4882a593Smuzhiyun 		   polling method ... */
404*4882a593Smuzhiyun 		extension.flags &= ~SAA7146_USE_I2C_IRQ;
405*4882a593Smuzhiyun 		for (i = 1; ; i++) {
406*4882a593Smuzhiyun 			if (-1 == mxb_saa7740_init[i].length)
407*4882a593Smuzhiyun 				break;
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun 			msg.len = mxb_saa7740_init[i].length;
410*4882a593Smuzhiyun 			msg.buf = &mxb_saa7740_init[i].data[0];
411*4882a593Smuzhiyun 			err = i2c_transfer(&mxb->i2c_adapter, &msg, 1);
412*4882a593Smuzhiyun 			if (err != 1) {
413*4882a593Smuzhiyun 				DEB_D("failed to initialize 'sound arena module'\n");
414*4882a593Smuzhiyun 				goto err;
415*4882a593Smuzhiyun 			}
416*4882a593Smuzhiyun 		}
417*4882a593Smuzhiyun 		pr_info("'sound arena module' detected\n");
418*4882a593Smuzhiyun 	}
419*4882a593Smuzhiyun err:
420*4882a593Smuzhiyun 	/* the rest for saa7146: you should definitely set some basic values
421*4882a593Smuzhiyun 	   for the input-port handling of the saa7146. */
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 	/* ext->saa has been filled by the core driver */
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun 	/* some stuff is done via variables */
426*4882a593Smuzhiyun 	saa7146_set_hps_source_and_sync(dev, input_port_selection[mxb->cur_input].hps_source,
427*4882a593Smuzhiyun 			input_port_selection[mxb->cur_input].hps_sync);
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun 	/* some stuff is done via direct write to the registers */
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 	/* this is ugly, but because of the fact that this is completely
432*4882a593Smuzhiyun 	   hardware dependend, it should be done directly... */
433*4882a593Smuzhiyun 	saa7146_write(dev, DD1_STREAM_B,	0x00000000);
434*4882a593Smuzhiyun 	saa7146_write(dev, DD1_INIT,		0x02000200);
435*4882a593Smuzhiyun 	saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun 	return 0;
438*4882a593Smuzhiyun }
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun /* interrupt-handler. this gets called when irq_mask is != 0.
441*4882a593Smuzhiyun    it must clear the interrupt-bits in irq_mask it has handled */
442*4882a593Smuzhiyun /*
443*4882a593Smuzhiyun void mxb_irq_bh(struct saa7146_dev* dev, u32* irq_mask)
444*4882a593Smuzhiyun {
445*4882a593Smuzhiyun 	struct mxb* mxb = (struct mxb*)dev->ext_priv;
446*4882a593Smuzhiyun }
447*4882a593Smuzhiyun */
448*4882a593Smuzhiyun 
vidioc_enum_input(struct file * file,void * fh,struct v4l2_input * i)449*4882a593Smuzhiyun static int vidioc_enum_input(struct file *file, void *fh, struct v4l2_input *i)
450*4882a593Smuzhiyun {
451*4882a593Smuzhiyun 	DEB_EE("VIDIOC_ENUMINPUT %d\n", i->index);
452*4882a593Smuzhiyun 	if (i->index >= MXB_INPUTS)
453*4882a593Smuzhiyun 		return -EINVAL;
454*4882a593Smuzhiyun 	memcpy(i, &mxb_inputs[i->index], sizeof(struct v4l2_input));
455*4882a593Smuzhiyun 	return 0;
456*4882a593Smuzhiyun }
457*4882a593Smuzhiyun 
vidioc_g_input(struct file * file,void * fh,unsigned int * i)458*4882a593Smuzhiyun static int vidioc_g_input(struct file *file, void *fh, unsigned int *i)
459*4882a593Smuzhiyun {
460*4882a593Smuzhiyun 	struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
461*4882a593Smuzhiyun 	struct mxb *mxb = (struct mxb *)dev->ext_priv;
462*4882a593Smuzhiyun 	*i = mxb->cur_input;
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun 	DEB_EE("VIDIOC_G_INPUT %d\n", *i);
465*4882a593Smuzhiyun 	return 0;
466*4882a593Smuzhiyun }
467*4882a593Smuzhiyun 
vidioc_s_input(struct file * file,void * fh,unsigned int input)468*4882a593Smuzhiyun static int vidioc_s_input(struct file *file, void *fh, unsigned int input)
469*4882a593Smuzhiyun {
470*4882a593Smuzhiyun 	struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
471*4882a593Smuzhiyun 	struct mxb *mxb = (struct mxb *)dev->ext_priv;
472*4882a593Smuzhiyun 	int err = 0;
473*4882a593Smuzhiyun 	int i = 0;
474*4882a593Smuzhiyun 
475*4882a593Smuzhiyun 	DEB_EE("VIDIOC_S_INPUT %d\n", input);
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun 	if (input >= MXB_INPUTS)
478*4882a593Smuzhiyun 		return -EINVAL;
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun 	mxb->cur_input = input;
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun 	saa7146_set_hps_source_and_sync(dev, input_port_selection[input].hps_source,
483*4882a593Smuzhiyun 			input_port_selection[input].hps_sync);
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 	/* prepare switching of tea6415c and saa7111a;
486*4882a593Smuzhiyun 	   have a look at the 'background'-file for further information  */
487*4882a593Smuzhiyun 	switch (input) {
488*4882a593Smuzhiyun 	case TUNER:
489*4882a593Smuzhiyun 		i = SAA7115_COMPOSITE0;
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 		err = tea6415c_call(mxb, video, s_routing, 3, 17, 0);
492*4882a593Smuzhiyun 
493*4882a593Smuzhiyun 		/* connect tuner-output always to multicable */
494*4882a593Smuzhiyun 		if (!err)
495*4882a593Smuzhiyun 			err = tea6415c_call(mxb, video, s_routing, 3, 13, 0);
496*4882a593Smuzhiyun 		break;
497*4882a593Smuzhiyun 	case AUX3_YC:
498*4882a593Smuzhiyun 		/* nothing to be done here. aux3_yc is
499*4882a593Smuzhiyun 		   directly connected to the saa711a */
500*4882a593Smuzhiyun 		i = SAA7115_SVIDEO1;
501*4882a593Smuzhiyun 		break;
502*4882a593Smuzhiyun 	case AUX3:
503*4882a593Smuzhiyun 		/* nothing to be done here. aux3 is
504*4882a593Smuzhiyun 		   directly connected to the saa711a */
505*4882a593Smuzhiyun 		i = SAA7115_COMPOSITE1;
506*4882a593Smuzhiyun 		break;
507*4882a593Smuzhiyun 	case AUX1:
508*4882a593Smuzhiyun 		i = SAA7115_COMPOSITE0;
509*4882a593Smuzhiyun 		err = tea6415c_call(mxb, video, s_routing, 1, 17, 0);
510*4882a593Smuzhiyun 		break;
511*4882a593Smuzhiyun 	}
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun 	if (err)
514*4882a593Smuzhiyun 		return err;
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun 	/* switch video in saa7111a */
517*4882a593Smuzhiyun 	if (saa7111a_call(mxb, video, s_routing, i, SAA7111_FMT_CCIR, 0))
518*4882a593Smuzhiyun 		pr_err("VIDIOC_S_INPUT: could not address saa7111a\n");
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun 	mxb->cur_audinput = video_audio_connect[input];
521*4882a593Smuzhiyun 	/* switch the audio-source only if necessary */
522*4882a593Smuzhiyun 	if (0 == mxb->cur_mute)
523*4882a593Smuzhiyun 		tea6420_route(mxb, mxb->cur_audinput);
524*4882a593Smuzhiyun 	if (mxb->cur_audinput == 0)
525*4882a593Smuzhiyun 		mxb_update_audmode(mxb);
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun 	return 0;
528*4882a593Smuzhiyun }
529*4882a593Smuzhiyun 
vidioc_g_tuner(struct file * file,void * fh,struct v4l2_tuner * t)530*4882a593Smuzhiyun static int vidioc_g_tuner(struct file *file, void *fh, struct v4l2_tuner *t)
531*4882a593Smuzhiyun {
532*4882a593Smuzhiyun 	struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
533*4882a593Smuzhiyun 	struct mxb *mxb = (struct mxb *)dev->ext_priv;
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun 	if (t->index) {
536*4882a593Smuzhiyun 		DEB_D("VIDIOC_G_TUNER: channel %d does not have a tuner attached\n",
537*4882a593Smuzhiyun 		      t->index);
538*4882a593Smuzhiyun 		return -EINVAL;
539*4882a593Smuzhiyun 	}
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 	DEB_EE("VIDIOC_G_TUNER: %d\n", t->index);
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun 	memset(t, 0, sizeof(*t));
544*4882a593Smuzhiyun 	strscpy(t->name, "TV Tuner", sizeof(t->name));
545*4882a593Smuzhiyun 	t->type = V4L2_TUNER_ANALOG_TV;
546*4882a593Smuzhiyun 	t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO |
547*4882a593Smuzhiyun 			V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
548*4882a593Smuzhiyun 	t->audmode = mxb->cur_mode;
549*4882a593Smuzhiyun 	return call_all(dev, tuner, g_tuner, t);
550*4882a593Smuzhiyun }
551*4882a593Smuzhiyun 
vidioc_s_tuner(struct file * file,void * fh,const struct v4l2_tuner * t)552*4882a593Smuzhiyun static int vidioc_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *t)
553*4882a593Smuzhiyun {
554*4882a593Smuzhiyun 	struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
555*4882a593Smuzhiyun 	struct mxb *mxb = (struct mxb *)dev->ext_priv;
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun 	if (t->index) {
558*4882a593Smuzhiyun 		DEB_D("VIDIOC_S_TUNER: channel %d does not have a tuner attached\n",
559*4882a593Smuzhiyun 		      t->index);
560*4882a593Smuzhiyun 		return -EINVAL;
561*4882a593Smuzhiyun 	}
562*4882a593Smuzhiyun 
563*4882a593Smuzhiyun 	mxb->cur_mode = t->audmode;
564*4882a593Smuzhiyun 	return call_all(dev, tuner, s_tuner, t);
565*4882a593Smuzhiyun }
566*4882a593Smuzhiyun 
vidioc_querystd(struct file * file,void * fh,v4l2_std_id * norm)567*4882a593Smuzhiyun static int vidioc_querystd(struct file *file, void *fh, v4l2_std_id *norm)
568*4882a593Smuzhiyun {
569*4882a593Smuzhiyun 	struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
570*4882a593Smuzhiyun 
571*4882a593Smuzhiyun 	return call_all(dev, video, querystd, norm);
572*4882a593Smuzhiyun }
573*4882a593Smuzhiyun 
vidioc_g_frequency(struct file * file,void * fh,struct v4l2_frequency * f)574*4882a593Smuzhiyun static int vidioc_g_frequency(struct file *file, void *fh, struct v4l2_frequency *f)
575*4882a593Smuzhiyun {
576*4882a593Smuzhiyun 	struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
577*4882a593Smuzhiyun 	struct mxb *mxb = (struct mxb *)dev->ext_priv;
578*4882a593Smuzhiyun 
579*4882a593Smuzhiyun 	if (f->tuner)
580*4882a593Smuzhiyun 		return -EINVAL;
581*4882a593Smuzhiyun 	*f = mxb->cur_freq;
582*4882a593Smuzhiyun 
583*4882a593Smuzhiyun 	DEB_EE("VIDIOC_G_FREQ: freq:0x%08x\n", mxb->cur_freq.frequency);
584*4882a593Smuzhiyun 	return 0;
585*4882a593Smuzhiyun }
586*4882a593Smuzhiyun 
vidioc_s_frequency(struct file * file,void * fh,const struct v4l2_frequency * f)587*4882a593Smuzhiyun static int vidioc_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *f)
588*4882a593Smuzhiyun {
589*4882a593Smuzhiyun 	struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
590*4882a593Smuzhiyun 	struct mxb *mxb = (struct mxb *)dev->ext_priv;
591*4882a593Smuzhiyun 	struct saa7146_vv *vv = dev->vv_data;
592*4882a593Smuzhiyun 
593*4882a593Smuzhiyun 	if (f->tuner)
594*4882a593Smuzhiyun 		return -EINVAL;
595*4882a593Smuzhiyun 
596*4882a593Smuzhiyun 	if (V4L2_TUNER_ANALOG_TV != f->type)
597*4882a593Smuzhiyun 		return -EINVAL;
598*4882a593Smuzhiyun 
599*4882a593Smuzhiyun 	DEB_EE("VIDIOC_S_FREQUENCY: freq:0x%08x\n", mxb->cur_freq.frequency);
600*4882a593Smuzhiyun 
601*4882a593Smuzhiyun 	/* tune in desired frequency */
602*4882a593Smuzhiyun 	tuner_call(mxb, tuner, s_frequency, f);
603*4882a593Smuzhiyun 	/* let the tuner subdev clamp the frequency to the tuner range */
604*4882a593Smuzhiyun 	mxb->cur_freq = *f;
605*4882a593Smuzhiyun 	tuner_call(mxb, tuner, g_frequency, &mxb->cur_freq);
606*4882a593Smuzhiyun 	if (mxb->cur_audinput == 0)
607*4882a593Smuzhiyun 		mxb_update_audmode(mxb);
608*4882a593Smuzhiyun 
609*4882a593Smuzhiyun 	if (mxb->cur_input)
610*4882a593Smuzhiyun 		return 0;
611*4882a593Smuzhiyun 
612*4882a593Smuzhiyun 	/* hack: changing the frequency should invalidate the vbi-counter (=> alevt) */
613*4882a593Smuzhiyun 	spin_lock(&dev->slock);
614*4882a593Smuzhiyun 	vv->vbi_fieldcount = 0;
615*4882a593Smuzhiyun 	spin_unlock(&dev->slock);
616*4882a593Smuzhiyun 
617*4882a593Smuzhiyun 	return 0;
618*4882a593Smuzhiyun }
619*4882a593Smuzhiyun 
vidioc_enumaudio(struct file * file,void * fh,struct v4l2_audio * a)620*4882a593Smuzhiyun static int vidioc_enumaudio(struct file *file, void *fh, struct v4l2_audio *a)
621*4882a593Smuzhiyun {
622*4882a593Smuzhiyun 	if (a->index >= MXB_AUDIOS)
623*4882a593Smuzhiyun 		return -EINVAL;
624*4882a593Smuzhiyun 	*a = mxb_audios[a->index];
625*4882a593Smuzhiyun 	return 0;
626*4882a593Smuzhiyun }
627*4882a593Smuzhiyun 
vidioc_g_audio(struct file * file,void * fh,struct v4l2_audio * a)628*4882a593Smuzhiyun static int vidioc_g_audio(struct file *file, void *fh, struct v4l2_audio *a)
629*4882a593Smuzhiyun {
630*4882a593Smuzhiyun 	struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
631*4882a593Smuzhiyun 	struct mxb *mxb = (struct mxb *)dev->ext_priv;
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun 	DEB_EE("VIDIOC_G_AUDIO\n");
634*4882a593Smuzhiyun 	*a = mxb_audios[mxb->cur_audinput];
635*4882a593Smuzhiyun 	return 0;
636*4882a593Smuzhiyun }
637*4882a593Smuzhiyun 
vidioc_s_audio(struct file * file,void * fh,const struct v4l2_audio * a)638*4882a593Smuzhiyun static int vidioc_s_audio(struct file *file, void *fh, const struct v4l2_audio *a)
639*4882a593Smuzhiyun {
640*4882a593Smuzhiyun 	struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
641*4882a593Smuzhiyun 	struct mxb *mxb = (struct mxb *)dev->ext_priv;
642*4882a593Smuzhiyun 
643*4882a593Smuzhiyun 	DEB_D("VIDIOC_S_AUDIO %d\n", a->index);
644*4882a593Smuzhiyun 	if (a->index >= 32 ||
645*4882a593Smuzhiyun 	    !(mxb_inputs[mxb->cur_input].audioset & (1 << a->index)))
646*4882a593Smuzhiyun 		return -EINVAL;
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun 	if (mxb->cur_audinput != a->index) {
649*4882a593Smuzhiyun 		mxb->cur_audinput = a->index;
650*4882a593Smuzhiyun 		tea6420_route(mxb, a->index);
651*4882a593Smuzhiyun 		if (mxb->cur_audinput == 0)
652*4882a593Smuzhiyun 			mxb_update_audmode(mxb);
653*4882a593Smuzhiyun 	}
654*4882a593Smuzhiyun 	return 0;
655*4882a593Smuzhiyun }
656*4882a593Smuzhiyun 
657*4882a593Smuzhiyun #ifdef CONFIG_VIDEO_ADV_DEBUG
vidioc_g_register(struct file * file,void * fh,struct v4l2_dbg_register * reg)658*4882a593Smuzhiyun static int vidioc_g_register(struct file *file, void *fh, struct v4l2_dbg_register *reg)
659*4882a593Smuzhiyun {
660*4882a593Smuzhiyun 	struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
661*4882a593Smuzhiyun 
662*4882a593Smuzhiyun 	if (reg->reg > pci_resource_len(dev->pci, 0) - 4)
663*4882a593Smuzhiyun 		return -EINVAL;
664*4882a593Smuzhiyun 	reg->val = saa7146_read(dev, reg->reg);
665*4882a593Smuzhiyun 	reg->size = 4;
666*4882a593Smuzhiyun 	return 0;
667*4882a593Smuzhiyun }
668*4882a593Smuzhiyun 
vidioc_s_register(struct file * file,void * fh,const struct v4l2_dbg_register * reg)669*4882a593Smuzhiyun static int vidioc_s_register(struct file *file, void *fh, const struct v4l2_dbg_register *reg)
670*4882a593Smuzhiyun {
671*4882a593Smuzhiyun 	struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
672*4882a593Smuzhiyun 
673*4882a593Smuzhiyun 	if (reg->reg > pci_resource_len(dev->pci, 0) - 4)
674*4882a593Smuzhiyun 		return -EINVAL;
675*4882a593Smuzhiyun 	saa7146_write(dev, reg->reg, reg->val);
676*4882a593Smuzhiyun 	return 0;
677*4882a593Smuzhiyun }
678*4882a593Smuzhiyun #endif
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun static struct saa7146_ext_vv vv_data;
681*4882a593Smuzhiyun 
682*4882a593Smuzhiyun /* this function only gets called when the probing was successful */
mxb_attach(struct saa7146_dev * dev,struct saa7146_pci_extension_data * info)683*4882a593Smuzhiyun static int mxb_attach(struct saa7146_dev *dev, struct saa7146_pci_extension_data *info)
684*4882a593Smuzhiyun {
685*4882a593Smuzhiyun 	struct mxb *mxb;
686*4882a593Smuzhiyun 	int ret;
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun 	DEB_EE("dev:%p\n", dev);
689*4882a593Smuzhiyun 
690*4882a593Smuzhiyun 	ret = saa7146_vv_init(dev, &vv_data);
691*4882a593Smuzhiyun 	if (ret) {
692*4882a593Smuzhiyun 		ERR("Error in saa7146_vv_init()");
693*4882a593Smuzhiyun 		return ret;
694*4882a593Smuzhiyun 	}
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun 	if (mxb_probe(dev)) {
697*4882a593Smuzhiyun 		saa7146_vv_release(dev);
698*4882a593Smuzhiyun 		return -1;
699*4882a593Smuzhiyun 	}
700*4882a593Smuzhiyun 	mxb = (struct mxb *)dev->ext_priv;
701*4882a593Smuzhiyun 
702*4882a593Smuzhiyun 	vv_data.vid_ops.vidioc_enum_input = vidioc_enum_input;
703*4882a593Smuzhiyun 	vv_data.vid_ops.vidioc_g_input = vidioc_g_input;
704*4882a593Smuzhiyun 	vv_data.vid_ops.vidioc_s_input = vidioc_s_input;
705*4882a593Smuzhiyun 	vv_data.vid_ops.vidioc_querystd = vidioc_querystd;
706*4882a593Smuzhiyun 	vv_data.vid_ops.vidioc_g_tuner = vidioc_g_tuner;
707*4882a593Smuzhiyun 	vv_data.vid_ops.vidioc_s_tuner = vidioc_s_tuner;
708*4882a593Smuzhiyun 	vv_data.vid_ops.vidioc_g_frequency = vidioc_g_frequency;
709*4882a593Smuzhiyun 	vv_data.vid_ops.vidioc_s_frequency = vidioc_s_frequency;
710*4882a593Smuzhiyun 	vv_data.vid_ops.vidioc_enumaudio = vidioc_enumaudio;
711*4882a593Smuzhiyun 	vv_data.vid_ops.vidioc_g_audio = vidioc_g_audio;
712*4882a593Smuzhiyun 	vv_data.vid_ops.vidioc_s_audio = vidioc_s_audio;
713*4882a593Smuzhiyun #ifdef CONFIG_VIDEO_ADV_DEBUG
714*4882a593Smuzhiyun 	vv_data.vid_ops.vidioc_g_register = vidioc_g_register;
715*4882a593Smuzhiyun 	vv_data.vid_ops.vidioc_s_register = vidioc_s_register;
716*4882a593Smuzhiyun #endif
717*4882a593Smuzhiyun 	if (saa7146_register_device(&mxb->video_dev, dev, "mxb", VFL_TYPE_VIDEO)) {
718*4882a593Smuzhiyun 		ERR("cannot register capture v4l2 device. skipping.\n");
719*4882a593Smuzhiyun 		saa7146_vv_release(dev);
720*4882a593Smuzhiyun 		return -1;
721*4882a593Smuzhiyun 	}
722*4882a593Smuzhiyun 
723*4882a593Smuzhiyun 	/* initialization stuff (vbi) (only for revision > 0 and for extensions which want it)*/
724*4882a593Smuzhiyun 	if (MXB_BOARD_CAN_DO_VBI(dev)) {
725*4882a593Smuzhiyun 		if (saa7146_register_device(&mxb->vbi_dev, dev, "mxb", VFL_TYPE_VBI)) {
726*4882a593Smuzhiyun 			ERR("cannot register vbi v4l2 device. skipping.\n");
727*4882a593Smuzhiyun 		}
728*4882a593Smuzhiyun 	}
729*4882a593Smuzhiyun 
730*4882a593Smuzhiyun 	pr_info("found Multimedia eXtension Board #%d\n", mxb_num);
731*4882a593Smuzhiyun 
732*4882a593Smuzhiyun 	mxb_num++;
733*4882a593Smuzhiyun 	mxb_init_done(dev);
734*4882a593Smuzhiyun 	return 0;
735*4882a593Smuzhiyun }
736*4882a593Smuzhiyun 
mxb_detach(struct saa7146_dev * dev)737*4882a593Smuzhiyun static int mxb_detach(struct saa7146_dev *dev)
738*4882a593Smuzhiyun {
739*4882a593Smuzhiyun 	struct mxb *mxb = (struct mxb *)dev->ext_priv;
740*4882a593Smuzhiyun 
741*4882a593Smuzhiyun 	DEB_EE("dev:%p\n", dev);
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun 	/* mute audio on tea6420s */
744*4882a593Smuzhiyun 	tea6420_route(mxb, 6);
745*4882a593Smuzhiyun 
746*4882a593Smuzhiyun 	saa7146_unregister_device(&mxb->video_dev,dev);
747*4882a593Smuzhiyun 	if (MXB_BOARD_CAN_DO_VBI(dev))
748*4882a593Smuzhiyun 		saa7146_unregister_device(&mxb->vbi_dev, dev);
749*4882a593Smuzhiyun 	saa7146_vv_release(dev);
750*4882a593Smuzhiyun 
751*4882a593Smuzhiyun 	mxb_num--;
752*4882a593Smuzhiyun 
753*4882a593Smuzhiyun 	i2c_del_adapter(&mxb->i2c_adapter);
754*4882a593Smuzhiyun 	kfree(mxb);
755*4882a593Smuzhiyun 
756*4882a593Smuzhiyun 	return 0;
757*4882a593Smuzhiyun }
758*4882a593Smuzhiyun 
std_callback(struct saa7146_dev * dev,struct saa7146_standard * standard)759*4882a593Smuzhiyun static int std_callback(struct saa7146_dev *dev, struct saa7146_standard *standard)
760*4882a593Smuzhiyun {
761*4882a593Smuzhiyun 	struct mxb *mxb = (struct mxb *)dev->ext_priv;
762*4882a593Smuzhiyun 
763*4882a593Smuzhiyun 	if (V4L2_STD_PAL_I == standard->id) {
764*4882a593Smuzhiyun 		v4l2_std_id std = V4L2_STD_PAL_I;
765*4882a593Smuzhiyun 
766*4882a593Smuzhiyun 		DEB_D("VIDIOC_S_STD: setting mxb for PAL_I\n");
767*4882a593Smuzhiyun 		/* These two gpio calls set the GPIO pins that control the tda9820 */
768*4882a593Smuzhiyun 		saa7146_write(dev, GPIO_CTRL, 0x00404050);
769*4882a593Smuzhiyun 		saa7111a_call(mxb, core, s_gpio, 0);
770*4882a593Smuzhiyun 		saa7111a_call(mxb, video, s_std, std);
771*4882a593Smuzhiyun 		if (mxb->cur_input == 0)
772*4882a593Smuzhiyun 			tuner_call(mxb, video, s_std, std);
773*4882a593Smuzhiyun 	} else {
774*4882a593Smuzhiyun 		v4l2_std_id std = V4L2_STD_PAL_BG;
775*4882a593Smuzhiyun 
776*4882a593Smuzhiyun 		if (mxb->cur_input)
777*4882a593Smuzhiyun 			std = standard->id;
778*4882a593Smuzhiyun 		DEB_D("VIDIOC_S_STD: setting mxb for PAL/NTSC/SECAM\n");
779*4882a593Smuzhiyun 		/* These two gpio calls set the GPIO pins that control the tda9820 */
780*4882a593Smuzhiyun 		saa7146_write(dev, GPIO_CTRL, 0x00404050);
781*4882a593Smuzhiyun 		saa7111a_call(mxb, core, s_gpio, 1);
782*4882a593Smuzhiyun 		saa7111a_call(mxb, video, s_std, std);
783*4882a593Smuzhiyun 		if (mxb->cur_input == 0)
784*4882a593Smuzhiyun 			tuner_call(mxb, video, s_std, std);
785*4882a593Smuzhiyun 	}
786*4882a593Smuzhiyun 	return 0;
787*4882a593Smuzhiyun }
788*4882a593Smuzhiyun 
789*4882a593Smuzhiyun static struct saa7146_standard standard[] = {
790*4882a593Smuzhiyun 	{
791*4882a593Smuzhiyun 		.name	= "PAL-BG",	.id	= V4L2_STD_PAL_BG,
792*4882a593Smuzhiyun 		.v_offset	= 0x17,	.v_field	= 288,
793*4882a593Smuzhiyun 		.h_offset	= 0x14,	.h_pixels	= 680,
794*4882a593Smuzhiyun 		.v_max_out	= 576,	.h_max_out	= 768,
795*4882a593Smuzhiyun 	}, {
796*4882a593Smuzhiyun 		.name	= "PAL-I",	.id	= V4L2_STD_PAL_I,
797*4882a593Smuzhiyun 		.v_offset	= 0x17,	.v_field	= 288,
798*4882a593Smuzhiyun 		.h_offset	= 0x14,	.h_pixels	= 680,
799*4882a593Smuzhiyun 		.v_max_out	= 576,	.h_max_out	= 768,
800*4882a593Smuzhiyun 	}, {
801*4882a593Smuzhiyun 		.name	= "NTSC",	.id	= V4L2_STD_NTSC,
802*4882a593Smuzhiyun 		.v_offset	= 0x16,	.v_field	= 240,
803*4882a593Smuzhiyun 		.h_offset	= 0x06,	.h_pixels	= 708,
804*4882a593Smuzhiyun 		.v_max_out	= 480,	.h_max_out	= 640,
805*4882a593Smuzhiyun 	}, {
806*4882a593Smuzhiyun 		.name	= "SECAM",	.id	= V4L2_STD_SECAM,
807*4882a593Smuzhiyun 		.v_offset	= 0x14,	.v_field	= 288,
808*4882a593Smuzhiyun 		.h_offset	= 0x14,	.h_pixels	= 720,
809*4882a593Smuzhiyun 		.v_max_out	= 576,	.h_max_out	= 768,
810*4882a593Smuzhiyun 	}
811*4882a593Smuzhiyun };
812*4882a593Smuzhiyun 
813*4882a593Smuzhiyun static struct saa7146_pci_extension_data mxb = {
814*4882a593Smuzhiyun 	.ext_priv = "Multimedia eXtension Board",
815*4882a593Smuzhiyun 	.ext = &extension,
816*4882a593Smuzhiyun };
817*4882a593Smuzhiyun 
818*4882a593Smuzhiyun static const struct pci_device_id pci_tbl[] = {
819*4882a593Smuzhiyun 	{
820*4882a593Smuzhiyun 		.vendor    = PCI_VENDOR_ID_PHILIPS,
821*4882a593Smuzhiyun 		.device	   = PCI_DEVICE_ID_PHILIPS_SAA7146,
822*4882a593Smuzhiyun 		.subvendor = 0x0000,
823*4882a593Smuzhiyun 		.subdevice = 0x0000,
824*4882a593Smuzhiyun 		.driver_data = (unsigned long)&mxb,
825*4882a593Smuzhiyun 	}, {
826*4882a593Smuzhiyun 		.vendor	= 0,
827*4882a593Smuzhiyun 	}
828*4882a593Smuzhiyun };
829*4882a593Smuzhiyun 
830*4882a593Smuzhiyun MODULE_DEVICE_TABLE(pci, pci_tbl);
831*4882a593Smuzhiyun 
832*4882a593Smuzhiyun static struct saa7146_ext_vv vv_data = {
833*4882a593Smuzhiyun 	.inputs		= MXB_INPUTS,
834*4882a593Smuzhiyun 	.capabilities	= V4L2_CAP_TUNER | V4L2_CAP_VBI_CAPTURE | V4L2_CAP_AUDIO,
835*4882a593Smuzhiyun 	.stds		= &standard[0],
836*4882a593Smuzhiyun 	.num_stds	= ARRAY_SIZE(standard),
837*4882a593Smuzhiyun 	.std_callback	= &std_callback,
838*4882a593Smuzhiyun };
839*4882a593Smuzhiyun 
840*4882a593Smuzhiyun static struct saa7146_extension extension = {
841*4882a593Smuzhiyun 	.name		= "Multimedia eXtension Board",
842*4882a593Smuzhiyun 	.flags		= SAA7146_USE_I2C_IRQ,
843*4882a593Smuzhiyun 
844*4882a593Smuzhiyun 	.pci_tbl	= &pci_tbl[0],
845*4882a593Smuzhiyun 	.module		= THIS_MODULE,
846*4882a593Smuzhiyun 
847*4882a593Smuzhiyun 	.attach		= mxb_attach,
848*4882a593Smuzhiyun 	.detach		= mxb_detach,
849*4882a593Smuzhiyun 
850*4882a593Smuzhiyun 	.irq_mask	= 0,
851*4882a593Smuzhiyun 	.irq_func	= NULL,
852*4882a593Smuzhiyun };
853*4882a593Smuzhiyun 
mxb_init_module(void)854*4882a593Smuzhiyun static int __init mxb_init_module(void)
855*4882a593Smuzhiyun {
856*4882a593Smuzhiyun 	if (saa7146_register_extension(&extension)) {
857*4882a593Smuzhiyun 		DEB_S("failed to register extension\n");
858*4882a593Smuzhiyun 		return -ENODEV;
859*4882a593Smuzhiyun 	}
860*4882a593Smuzhiyun 
861*4882a593Smuzhiyun 	return 0;
862*4882a593Smuzhiyun }
863*4882a593Smuzhiyun 
mxb_cleanup_module(void)864*4882a593Smuzhiyun static void __exit mxb_cleanup_module(void)
865*4882a593Smuzhiyun {
866*4882a593Smuzhiyun 	saa7146_unregister_extension(&extension);
867*4882a593Smuzhiyun }
868*4882a593Smuzhiyun 
869*4882a593Smuzhiyun module_init(mxb_init_module);
870*4882a593Smuzhiyun module_exit(mxb_cleanup_module);
871*4882a593Smuzhiyun 
872*4882a593Smuzhiyun MODULE_DESCRIPTION("video4linux-2 driver for the Siemens-Nixdorf 'Multimedia eXtension board'");
873*4882a593Smuzhiyun MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
874*4882a593Smuzhiyun MODULE_LICENSE("GPL");
875