blob: edea9e3d2dca00b7d0b2f281b935d0e8c3d875b8 [file] [log] [blame]
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -03001/* saa7115 - Philips SAA7113/SAA7114/SAA7115 video decoder driver
Hans Verkuile19b2fc2005-11-13 16:08:04 -08002 *
3 * Based on saa7114 driver by Maxim Yevtyushkin, which is based on
4 * the saa7111 driver by Dave Perks.
5 *
6 * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
7 * Copyright (C) 2002 Maxim Yevtyushkin <max@linuxmedialabs.com>
8 *
9 * Slight changes for video timing and attachment output by
10 * Wolfgang Scherr <scherr@net4you.net>
11 *
12 * Moved over to the linux >= 2.4.x i2c protocol (1/1/2003)
13 * by Ronald Bultje <rbultje@ronald.bitfreak.net>
14 *
15 * Added saa7115 support by Kevin Thayer <nufan_wfk at yahoo.com>
16 * (2/17/2003)
17 *
18 * VBI support (2004) and cleanups (2005) by Hans Verkuil <hverkuil@xs4all.nl>
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -030019 * SAA7113 support by Mauro Carvalho Chehab <mchehab@infradead.org>
Hans Verkuile19b2fc2005-11-13 16:08:04 -080020 *
21 * This program is free software; you can redistribute it and/or
22 * modify it under the terms of the GNU General Public License
23 * as published by the Free Software Foundation; either version 2
24 * of the License, or (at your option) any later version.
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30 *
31 * You should have received a copy of the GNU General Public License
32 * along with this program; if not, write to the Free Software
33 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
34 */
35
36
37#include <linux/kernel.h>
38#include <linux/module.h>
39#include <linux/slab.h>
40#include <linux/i2c.h>
41#include <linux/videodev2.h>
Hans Verkuile19b2fc2005-11-13 16:08:04 -080042#include <media/v4l2-common.h>
Hans Verkuil1f8f5fa2006-03-25 09:20:28 -030043#include <media/saa7115.h>
Hans Verkuil3578d3d2006-01-09 15:25:41 -020044#include <asm/div64.h>
Hans Verkuile19b2fc2005-11-13 16:08:04 -080045
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -030046MODULE_DESCRIPTION("Philips SAA7113/SAA7114/SAA7115 video decoder driver");
47MODULE_AUTHOR( "Maxim Yevtyushkin, Kevin Thayer, Chris Kennedy, "
48 "Hans Verkuil, Mauro Carvalho Chehab");
Hans Verkuile19b2fc2005-11-13 16:08:04 -080049MODULE_LICENSE("GPL");
50
51static int debug = 0;
Hans Verkuilfac9e892006-01-09 15:32:40 -020052module_param(debug, bool, 0644);
Hans Verkuile19b2fc2005-11-13 16:08:04 -080053
54MODULE_PARM_DESC(debug, "Debug level (0-1)");
55
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -030056static unsigned short normal_i2c[] = {
Hans Verkuil01342352006-03-25 08:19:47 -030057 0x4a >> 1, 0x48 >> 1, /* SAA7113 */
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -030058 0x42 >> 1, 0x40 >> 1, /* SAA7114 and SAA7115 */
59 I2C_CLIENT_END };
Hans Verkuile19b2fc2005-11-13 16:08:04 -080060
61
62I2C_CLIENT_INSMOD;
63
64struct saa7115_state {
65 v4l2_std_id std;
66 int input;
67 int enable;
Hans Verkuil3faeeae2006-01-09 15:25:44 -020068 int radio;
Hans Verkuile19b2fc2005-11-13 16:08:04 -080069 int bright;
70 int contrast;
71 int hue;
72 int sat;
73 enum v4l2_chip_ident ident;
Hans Verkuil3578d3d2006-01-09 15:25:41 -020074 u32 audclk_freq;
Hans Verkuilb7f82922006-04-02 12:50:42 -030075 u32 crystal_freq;
76 u8 ucgc;
77 u8 cgcdiv;
78 u8 apll;
Hans Verkuile19b2fc2005-11-13 16:08:04 -080079};
80
81/* ----------------------------------------------------------------------- */
82
83static inline int saa7115_write(struct i2c_client *client, u8 reg, u8 value)
84{
85 return i2c_smbus_write_byte_data(client, reg, value);
86}
87
88static int saa7115_writeregs(struct i2c_client *client, const unsigned char *regs)
89{
90 unsigned char reg, data;
91
92 while (*regs != 0x00) {
93 reg = *(regs++);
94 data = *(regs++);
95 if (saa7115_write(client, reg, data) < 0)
96 return -1;
97 }
98 return 0;
99}
100
101static inline int saa7115_read(struct i2c_client *client, u8 reg)
102{
103 return i2c_smbus_read_byte_data(client, reg);
104}
105
106/* ----------------------------------------------------------------------- */
107
108/* If a value differs from the Hauppauge driver values, then the comment starts with
109 'was 0xXX' to denote the Hauppauge value. Otherwise the value is identical to what the
110 Hauppauge driver sets. */
111
112static const unsigned char saa7115_init_auto_input[] = {
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -0300113 /* Front-End Part */
Hans Verkuile19b2fc2005-11-13 16:08:04 -0800114 0x01, 0x48, /* white peak control disabled */
115 0x03, 0x20, /* was 0x30. 0x20: long vertical blanking */
116 0x04, 0x90, /* analog gain set to 0 */
117 0x05, 0x90, /* analog gain set to 0 */
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -0300118 /* Decoder Part */
Hans Verkuile19b2fc2005-11-13 16:08:04 -0800119 0x06, 0xeb, /* horiz sync begin = -21 */
120 0x07, 0xe0, /* horiz sync stop = -17 */
121 0x0a, 0x80, /* was 0x88. decoder brightness, 0x80 is itu standard */
122 0x0b, 0x44, /* was 0x48. decoder contrast, 0x44 is itu standard */
123 0x0c, 0x40, /* was 0x47. decoder saturation, 0x40 is itu standard */
124 0x0d, 0x00, /* chrominance hue control */
125 0x0f, 0x00, /* chrominance gain control: use automicatic mode */
126 0x10, 0x06, /* chrominance/luminance control: active adaptive combfilter */
127 0x11, 0x00, /* delay control */
128 0x12, 0x9d, /* RTS0 output control: VGATE */
129 0x13, 0x80, /* X-port output control: ITU656 standard mode, RTCO output enable RTCE */
130 0x14, 0x00, /* analog/ADC/auto compatibility control */
131 0x18, 0x40, /* raw data gain 0x00 = nominal */
132 0x19, 0x80, /* raw data offset 0x80 = 0 LSB */
133 0x1a, 0x77, /* color killer level control 0x77 = recommended */
134 0x1b, 0x42, /* misc chroma control 0x42 = recommended */
135 0x1c, 0xa9, /* combfilter control 0xA9 = recommended */
136 0x1d, 0x01, /* combfilter control 0x01 = recommended */
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -0300137
138 /* Power Device Control */
Hans Verkuile19b2fc2005-11-13 16:08:04 -0800139 0x88, 0xd0, /* reset device */
140 0x88, 0xf0, /* set device programmed, all in operational mode */
141 0x00, 0x00
142};
143
144static const unsigned char saa7115_cfg_reset_scaler[] = {
145 0x87, 0x00, /* disable I-port output */
146 0x88, 0xd0, /* reset scaler */
147 0x88, 0xf0, /* activate scaler */
148 0x87, 0x01, /* enable I-port output */
149 0x00, 0x00
150};
151
152/* ============== SAA7715 VIDEO templates ============= */
153
154static const unsigned char saa7115_cfg_60hz_fullres_x[] = {
155 0xcc, 0xd0, /* hsize low (output), hor. output window size = 0x2d0 = 720 */
156 0xcd, 0x02, /* hsize hi (output) */
157
158 /* Why not in 60hz-Land, too? */
159 0xd0, 0x01, /* downscale = 1 */
160 0xd8, 0x00, /* hor lum scaling 0x0400 = 1 */
161 0xd9, 0x04,
162 0xdc, 0x00, /* hor chrom scaling 0x0200. must be hor lum scaling / 2 */
163 0xdd, 0x02, /* H-scaling incr chroma */
164
165 0x00, 0x00
166};
167static const unsigned char saa7115_cfg_60hz_fullres_y[] = {
168 0xce, 0xf8, /* vsize low (output), ver. output window size = 248 (but 60hz is 240?) */
169 0xcf, 0x00, /* vsize hi (output) */
170
171 /* Why not in 60hz-Land, too? */
172 0xd5, 0x40, /* Lum contrast, nominal value = 0x40 */
173 0xd6, 0x40, /* Chroma satur. nominal value = 0x80 */
174
175 0xe0, 0x00, /* V-scaling incr luma low */
176 0xe1, 0x04, /* " hi */
177 0xe2, 0x00, /* V-scaling incr chroma low */
178 0xe3, 0x04, /* " hi */
179
180 0x00, 0x00
181};
182
183static const unsigned char saa7115_cfg_60hz_video[] = {
184 0x80, 0x00, /* reset tasks */
185 0x88, 0xd0, /* reset scaler */
186
187 0x15, 0x03, /* VGATE pulse start */
188 0x16, 0x11, /* VGATE pulse stop */
189 0x17, 0x9c, /* VGATE MSB and other values */
190
191 0x08, 0x68, /* 0xBO: auto detection, 0x68 = NTSC */
192 0x0e, 0x07, /* lots of different stuff... video autodetection is on */
193
194 0x5a, 0x06, /* Vertical offset, standard 60hz value for ITU656 line counting */
195
196 /* Task A */
197 0x90, 0x80, /* Task Handling Control */
198 0x91, 0x48, /* X-port formats/config */
199 0x92, 0x40, /* Input Ref. signal Def. */
200 0x93, 0x84, /* I-port config */
201 0x94, 0x01, /* hoffset low (input), 0x0002 is minimum */
202 0x95, 0x00, /* hoffset hi (input) */
203 0x96, 0xd0, /* hsize low (input), 0x02d0 = 720 */
204 0x97, 0x02, /* hsize hi (input) */
205 0x98, 0x05, /* voffset low (input) */
206 0x99, 0x00, /* voffset hi (input) */
207 0x9a, 0x0c, /* vsize low (input), 0x0c = 12 */
208 0x9b, 0x00, /* vsize hi (input) */
209 0x9c, 0xa0, /* hsize low (output), 0x05a0 = 1440 */
210 0x9d, 0x05, /* hsize hi (output) */
211 0x9e, 0x0c, /* vsize low (output), 0x0c = 12 */
212 0x9f, 0x00, /* vsize hi (output) */
213
214 /* Task B */
215 0xc0, 0x00, /* Task Handling Control */
216 0xc1, 0x08, /* X-port formats/config */
217 0xc2, 0x00, /* Input Ref. signal Def. */
218 0xc3, 0x80, /* I-port config */
219 0xc4, 0x02, /* hoffset low (input), 0x0002 is minimum */
220 0xc5, 0x00, /* hoffset hi (input) */
221 0xc6, 0xd0, /* hsize low (input), 0x02d0 = 720 */
222 0xc7, 0x02, /* hsize hi (input) */
223 0xc8, 0x12, /* voffset low (input), 0x12 = 18 */
224 0xc9, 0x00, /* voffset hi (input) */
225 0xca, 0xf8, /* vsize low (input), 0xf8 = 248 */
226 0xcb, 0x00, /* vsize hi (input) */
227 0xcc, 0xd0, /* hsize low (output), 0x02d0 = 720 */
228 0xcd, 0x02, /* hsize hi (output) */
229
230 0xf0, 0xad, /* Set PLL Register. 60hz 525 lines per frame, 27 MHz */
231 0xf1, 0x05, /* low bit with 0xF0 */
232 0xf5, 0xad, /* Set pulse generator register */
233 0xf6, 0x01,
234
235 0x87, 0x00, /* Disable I-port output */
236 0x88, 0xd0, /* reset scaler */
237 0x80, 0x20, /* Activate only task "B", continuous mode (was 0xA0) */
238 0x88, 0xf0, /* activate scaler */
239 0x87, 0x01, /* Enable I-port output */
240 0x00, 0x00
241};
242
243static const unsigned char saa7115_cfg_50hz_fullres_x[] = {
244 0xcc, 0xd0, /* hsize low (output), 720 same as 60hz */
245 0xcd, 0x02, /* hsize hi (output) */
246
247 0xd0, 0x01, /* down scale = 1 */
248 0xd8, 0x00, /* hor lum scaling 0x0400 = 1 */
249 0xd9, 0x04,
250 0xdc, 0x00, /* hor chrom scaling 0x0200. must be hor lum scaling / 2 */
251 0xdd, 0x02, /* H-scaling incr chroma */
252
253 0x00, 0x00
254};
255static const unsigned char saa7115_cfg_50hz_fullres_y[] = {
256 0xce, 0x20, /* vsize low (output), 0x0120 = 288 */
257 0xcf, 0x01, /* vsize hi (output) */
258
259 0xd5, 0x40, /* Lum contrast, nominal value = 0x40 */
260 0xd6, 0x40, /* Chroma satur. nominal value = 0x80 */
261
262 0xe0, 0x00, /* V-scaling incr luma low */
263 0xe1, 0x04, /* " hi */
264 0xe2, 0x00, /* V-scaling incr chroma low */
265 0xe3, 0x04, /* " hi */
266
267 0x00, 0x00
268};
269
270static const unsigned char saa7115_cfg_50hz_video[] = {
271 0x80, 0x00, /* reset tasks */
272 0x88, 0xd0, /* reset scaler */
273
274 0x15, 0x37, /* VGATE start */
275 0x16, 0x16, /* VGATE stop */
276 0x17, 0x99, /* VGATE MSB and other values */
277
278 0x08, 0x28, /* 0x28 = PAL */
279 0x0e, 0x07, /* chrominance control 1 */
280
281 0x5a, 0x03, /* Vertical offset, standard 50hz value */
282
283 /* Task A */
284 0x90, 0x81, /* Task Handling Control */
285 0x91, 0x48, /* X-port formats/config */
286 0x92, 0x40, /* Input Ref. signal Def. */
287 0x93, 0x84, /* I-port config */
288 /* This is weird: the datasheet says that you should use 2 as the minimum value, */
289 /* but Hauppauge uses 0, and changing that to 2 causes indeed problems (for 50hz) */
290 0x94, 0x00, /* hoffset low (input), 0x0002 is minimum */
291 0x95, 0x00, /* hoffset hi (input) */
292 0x96, 0xd0, /* hsize low (input), 0x02d0 = 720 */
293 0x97, 0x02, /* hsize hi (input) */
294 0x98, 0x03, /* voffset low (input) */
295 0x99, 0x00, /* voffset hi (input) */
296 0x9a, 0x12, /* vsize low (input), 0x12 = 18 */
297 0x9b, 0x00, /* vsize hi (input) */
298 0x9c, 0xa0, /* hsize low (output), 0x05a0 = 1440 */
299 0x9d, 0x05, /* hsize hi (output) */
300 0x9e, 0x12, /* vsize low (output), 0x12 = 18 */
301 0x9f, 0x00, /* vsize hi (output) */
302
303 /* Task B */
304 0xc0, 0x00, /* Task Handling Control */
305 0xc1, 0x08, /* X-port formats/config */
306 0xc2, 0x00, /* Input Ref. signal Def. */
307 0xc3, 0x80, /* I-port config */
308 0xc4, 0x00, /* hoffset low (input), 0x0002 is minimum. See comment at 0x94 above. */
309 0xc5, 0x00, /* hoffset hi (input) */
310 0xc6, 0xd0, /* hsize low (input), 0x02d0 = 720 */
311 0xc7, 0x02, /* hsize hi (input) */
312 0xc8, 0x16, /* voffset low (input), 0x16 = 22 */
313 0xc9, 0x00, /* voffset hi (input) */
314 0xca, 0x20, /* vsize low (input), 0x0120 = 288 */
315 0xcb, 0x01, /* vsize hi (input) */
316 0xcc, 0xd0, /* hsize low (output), 0x02d0 = 720 */
317 0xcd, 0x02, /* hsize hi (output) */
318 0xce, 0x20, /* vsize low (output), 0x0120 = 288 */
319 0xcf, 0x01, /* vsize hi (output) */
320
321 0xf0, 0xb0, /* Set PLL Register. 50hz 625 lines per frame, 27 MHz */
322 0xf1, 0x05, /* low bit with 0xF0, (was 0x05) */
323 0xf5, 0xb0, /* Set pulse generator register */
324 0xf6, 0x01,
325
326 0x87, 0x00, /* Disable I-port output */
327 0x88, 0xd0, /* reset scaler (was 0xD0) */
328 0x80, 0x20, /* Activate only task "B" */
329 0x88, 0xf0, /* activate scaler */
330 0x87, 0x01, /* Enable I-port output */
331 0x00, 0x00
332};
333
334/* ============== SAA7715 VIDEO templates (end) ======= */
335
336static const unsigned char saa7115_cfg_vbi_on[] = {
337 0x80, 0x00, /* reset tasks */
338 0x88, 0xd0, /* reset scaler */
339 0x80, 0x30, /* Activate both tasks */
340 0x88, 0xf0, /* activate scaler */
341 0x87, 0x01, /* Enable I-port output */
342 0x00, 0x00
343};
344
345static const unsigned char saa7115_cfg_vbi_off[] = {
346 0x80, 0x00, /* reset tasks */
347 0x88, 0xd0, /* reset scaler */
348 0x80, 0x20, /* Activate only task "B" */
349 0x88, 0xf0, /* activate scaler */
350 0x87, 0x01, /* Enable I-port output */
351 0x00, 0x00
352};
353
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -0300354static const unsigned char saa7113_init_auto_input[] = {
355 0x01, 0x08, /* PH7113_INCREMENT_DELAY - (1) (1) (1) (1) IDEL3 IDEL2 IDELL1 IDEL0 */
356 0x02, 0xc2, /* PH7113_ANALOG_INPUT_CONTR_1 - FUSE1 FUSE0 GUDL1 GUDL0 MODE3 MODE2 MODE1 MODE0 */
357 0x03, 0x30, /* PH7113_ANALOG_INPUT_CONTR_2 - (1) HLNRS VBSL WPOFF HOLDG GAFIX GAI28 GAI18 */
358 0x04, 0x00, /* PH7113_ANALOG_INPUT_CONTR_3 - GAI17 GAI16 GAI15 GAI14 GAI13 GAI12 GAI11 GAI10 */
359 0x05, 0x00, /* PH7113_ANALOG_INPUT_CONTR_4 - GAI27 GAI26 GAI25 GAI24 GAI23 GAI22 GAI21 GAI20 */
360 0x06, 0x89, /* PH7113_HORIZONTAL_SYNC_START - HSB7 HSB6 HSB5 HSB4 HSB3 HSB2 HSB1 HSB0 */
361 0x07, 0x0d, /* PH7113_HORIZONTAL_SYNC_STOP - HSS7 HSS6 HSS5 HSS4 HSS3 HSS2 HSS1 HSS0 */
362 0x08, 0x88, /* PH7113_SYNC_CONTROL - AUFD FSEL FOET HTC1 HTC0 HPLL VNOI1 VNOI0 */
363 0x09, 0x01, /* PH7113_LUMINANCE_CONTROL - BYPS PREF BPSS1 BPSS0 VBLB UPTCV APER1 APER0 */
364 0x0a, 0x80, /* PH7113_LUMINANCE_BRIGHTNESS - BRIG7 BRIG6 BRIG5 BRIG4 BRIG3 BRIG2 BRIG1 BRIG0 */
365 0x0b, 0x47, /* PH7113_LUMINANCE_CONTRAST - CONT7 CONT6 CONT5 CONT4 CONT3 CONT2 CONT1 CONT0 */
366 0x0c, 0x40, /* PH7113_CHROMA_SATURATION - SATN7 SATN6 SATN5 SATN4 SATN3 SATN2 SATN1 SATN0 */
367 0x0d, 0x00, /* PH7113_CHROMA_HUE_CONTROL - HUEC7 HUEC6 HUEC5 HUEC4 HUEC3 HUEC2 HUEC1 HUEC0 */
368 0x0e, 0x01, /* PH7113_CHROMA_CONTROL - CDTO CSTD2 CSTD1 CSTD0 DCCF FCTC CHBW1 CHBW0 */
369 0x0f, 0x2a, /* PH7113_CHROMA_GAIN_CONTROL - ACGC CGAIN6 CGAIN5 CGAIN4 CGAIN3 CGAIN2 CGAIN1 CGAIN0 */
370 0x10, 0x08, /* PH7113_FORMAT_DELAY_CONTROL - OFTS1 OFTS0 HDEL1 HDEL0 VRLN YDEL2 YDEL1 YDEL0 */
371 0x11, 0x0c, /* PH7113_OUTPUT_CONTROL_1 - GPSW1 CM99 GPSW0 HLSEL OEYC OERT VIPB COLO */
372 0x12, 0x07, /* PH7113_OUTPUT_CONTROL_2 - RTSE13 RTSE12 RTSE11 RTSE10 RTSE03 RTSE02 RTSE01 RTSE00 */
373 0x13, 0x00, /* PH7113_OUTPUT_CONTROL_3 - ADLSB (1) (1) OLDSB FIDP (1) AOSL1 AOSL0 */
374 0x14, 0x00, /* RESERVED 14 - (1) (1) (1) (1) (1) (1) (1) (1) */
375 0x15, 0x00, /* PH7113_V_GATE1_START - VSTA7 VSTA6 VSTA5 VSTA4 VSTA3 VSTA2 VSTA1 VSTA0 */
376 0x16, 0x00, /* PH7113_V_GATE1_STOP - VSTO7 VSTO6 VSTO5 VSTO4 VSTO3 VSTO2 VSTO1 VSTO0 */
377 0x17, 0x00, /* PH7113_V_GATE1_MSB - (1) (1) (1) (1) (1) (1) VSTO8 VSTA8 */
378 0x00, 0x00
379};
380
Hans Verkuile19b2fc2005-11-13 16:08:04 -0800381static const unsigned char saa7115_init_misc[] = {
Hans Verkuile19b2fc2005-11-13 16:08:04 -0800382 0x81, 0x01, /* reg 0x15,0x16 define blanking window */
383 0x82, 0x00,
384 0x83, 0x01, /* I port settings */
385 0x84, 0x20,
386 0x85, 0x21,
387 0x86, 0xc5,
388 0x87, 0x01,
389
390 /* Task A */
391 0xa0, 0x01, /* down scale = 1 */
392 0xa1, 0x00, /* prescale accumulation length = 1 */
393 0xa2, 0x00, /* dc gain and fir prefilter control */
394 0xa4, 0x80, /* Lum Brightness, nominal value = 0x80 */
395 0xa5, 0x40, /* Lum contrast, nominal value = 0x40 */
396 0xa6, 0x40, /* Chroma satur. nominal value = 0x80 */
397 0xa8, 0x00, /* hor lum scaling 0x0200 = 2 zoom */
398 0xa9, 0x02, /* note: 2 x zoom ensures that VBI lines have same length as video lines. */
399 0xaa, 0x00, /* H-phase offset Luma = 0 */
400 0xac, 0x00, /* hor chrom scaling 0x0200. must be hor lum scaling / 2 */
401 0xad, 0x01, /* H-scaling incr chroma */
402 0xae, 0x00, /* H-phase offset chroma. must be offset luma / 2 */
403
404 0xb0, 0x00, /* V-scaling incr luma low */
405 0xb1, 0x04, /* " hi */
406 0xb2, 0x00, /* V-scaling incr chroma low */
407 0xb3, 0x04, /* " hi */
408 0xb4, 0x01, /* V-scaling mode control */
409 0xb8, 0x00, /* V-phase offset chroma 00 */
410 0xb9, 0x00, /* V-phase offset chroma 01 */
411 0xba, 0x00, /* V-phase offset chroma 10 */
412 0xbb, 0x00, /* V-phase offset chroma 11 */
413 0xbc, 0x00, /* V-phase offset luma 00 */
414 0xbd, 0x00, /* V-phase offset luma 01 */
415 0xbe, 0x00, /* V-phase offset luma 10 */
416 0xbf, 0x00, /* V-phase offset luma 11 */
417
418 /* Task B */
419 0xd0, 0x01, /* down scale = 1 */
420 0xd1, 0x00, /* prescale accumulation length = 1 */
421 0xd2, 0x00, /* dc gain and fir prefilter control */
422 0xd4, 0x80, /* Lum Brightness, nominal value = 0x80 */
423 0xd5, 0x40, /* Lum contrast, nominal value = 0x40 */
424 0xd6, 0x40, /* Chroma satur. nominal value = 0x80 */
425 0xd8, 0x00, /* hor lum scaling 0x0400 = 1 */
426 0xd9, 0x04,
427 0xda, 0x00, /* H-phase offset Luma = 0 */
428 0xdc, 0x00, /* hor chrom scaling 0x0200. must be hor lum scaling / 2 */
429 0xdd, 0x02, /* H-scaling incr chroma */
430 0xde, 0x00, /* H-phase offset chroma. must be offset luma / 2 */
431
432 0xe0, 0x00, /* V-scaling incr luma low */
433 0xe1, 0x04, /* " hi */
434 0xe2, 0x00, /* V-scaling incr chroma low */
435 0xe3, 0x04, /* " hi */
436 0xe4, 0x01, /* V-scaling mode control */
437 0xe8, 0x00, /* V-phase offset chroma 00 */
438 0xe9, 0x00, /* V-phase offset chroma 01 */
439 0xea, 0x00, /* V-phase offset chroma 10 */
440 0xeb, 0x00, /* V-phase offset chroma 11 */
441 0xec, 0x00, /* V-phase offset luma 00 */
442 0xed, 0x00, /* V-phase offset luma 01 */
443 0xee, 0x00, /* V-phase offset luma 10 */
444 0xef, 0x00, /* V-phase offset luma 11 */
445
446 0xf2, 0x50, /* crystal clock = 24.576 MHz, target = 27MHz */
447 0xf3, 0x46,
448 0xf4, 0x00,
449 0xf7, 0x4b, /* not the recommended settings! */
450 0xf8, 0x00,
451 0xf9, 0x4b,
452 0xfa, 0x00,
453 0xfb, 0x4b,
454 0xff, 0x88, /* PLL2 lock detection settings: 71 lines 50% phase error */
455
456 /* Turn off VBI */
457 0x40, 0x20, /* No framing code errors allowed. */
458 0x41, 0xff,
459 0x42, 0xff,
460 0x43, 0xff,
461 0x44, 0xff,
462 0x45, 0xff,
463 0x46, 0xff,
464 0x47, 0xff,
465 0x48, 0xff,
466 0x49, 0xff,
467 0x4a, 0xff,
468 0x4b, 0xff,
469 0x4c, 0xff,
470 0x4d, 0xff,
471 0x4e, 0xff,
472 0x4f, 0xff,
473 0x50, 0xff,
474 0x51, 0xff,
475 0x52, 0xff,
476 0x53, 0xff,
477 0x54, 0xff,
478 0x55, 0xff,
479 0x56, 0xff,
480 0x57, 0xff,
481 0x58, 0x40,
482 0x59, 0x47,
483 0x5b, 0x83,
484 0x5d, 0xbd,
485 0x5e, 0x35,
486
487 0x02, 0x84, /* input tuner -> input 4, amplifier active */
488 0x09, 0x53, /* 0x53, was 0x56 for 60hz. luminance control */
489
490 0x80, 0x20, /* enable task B */
491 0x88, 0xd0,
492 0x88, 0xf0,
493 0x00, 0x00
494};
495
Hans Verkuile19b2fc2005-11-13 16:08:04 -0800496static int saa7115_odd_parity(u8 c)
497{
498 c ^= (c >> 4);
499 c ^= (c >> 2);
500 c ^= (c >> 1);
501
502 return c & 1;
503}
504
505static int saa7115_decode_vps(u8 * dst, u8 * p)
506{
507 static const u8 biphase_tbl[] = {
508 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,
509 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,
510 0xd2, 0x5a, 0x52, 0xd2, 0x96, 0x1e, 0x16, 0x96,
511 0x92, 0x1a, 0x12, 0x92, 0xd2, 0x5a, 0x52, 0xd2,
512 0xd0, 0x58, 0x50, 0xd0, 0x94, 0x1c, 0x14, 0x94,
513 0x90, 0x18, 0x10, 0x90, 0xd0, 0x58, 0x50, 0xd0,
514 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,
515 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,
516 0xe1, 0x69, 0x61, 0xe1, 0xa5, 0x2d, 0x25, 0xa5,
517 0xa1, 0x29, 0x21, 0xa1, 0xe1, 0x69, 0x61, 0xe1,
518 0xc3, 0x4b, 0x43, 0xc3, 0x87, 0x0f, 0x07, 0x87,
519 0x83, 0x0b, 0x03, 0x83, 0xc3, 0x4b, 0x43, 0xc3,
520 0xc1, 0x49, 0x41, 0xc1, 0x85, 0x0d, 0x05, 0x85,
521 0x81, 0x09, 0x01, 0x81, 0xc1, 0x49, 0x41, 0xc1,
522 0xe1, 0x69, 0x61, 0xe1, 0xa5, 0x2d, 0x25, 0xa5,
523 0xa1, 0x29, 0x21, 0xa1, 0xe1, 0x69, 0x61, 0xe1,
524 0xe0, 0x68, 0x60, 0xe0, 0xa4, 0x2c, 0x24, 0xa4,
525 0xa0, 0x28, 0x20, 0xa0, 0xe0, 0x68, 0x60, 0xe0,
526 0xc2, 0x4a, 0x42, 0xc2, 0x86, 0x0e, 0x06, 0x86,
527 0x82, 0x0a, 0x02, 0x82, 0xc2, 0x4a, 0x42, 0xc2,
528 0xc0, 0x48, 0x40, 0xc0, 0x84, 0x0c, 0x04, 0x84,
529 0x80, 0x08, 0x00, 0x80, 0xc0, 0x48, 0x40, 0xc0,
530 0xe0, 0x68, 0x60, 0xe0, 0xa4, 0x2c, 0x24, 0xa4,
531 0xa0, 0x28, 0x20, 0xa0, 0xe0, 0x68, 0x60, 0xe0,
532 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,
533 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,
534 0xd2, 0x5a, 0x52, 0xd2, 0x96, 0x1e, 0x16, 0x96,
535 0x92, 0x1a, 0x12, 0x92, 0xd2, 0x5a, 0x52, 0xd2,
536 0xd0, 0x58, 0x50, 0xd0, 0x94, 0x1c, 0x14, 0x94,
537 0x90, 0x18, 0x10, 0x90, 0xd0, 0x58, 0x50, 0xd0,
538 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,
539 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,
540 };
541 int i;
542 u8 c, err = 0;
543
544 for (i = 0; i < 2 * 13; i += 2) {
545 err |= biphase_tbl[p[i]] | biphase_tbl[p[i + 1]];
546 c = (biphase_tbl[p[i + 1]] & 0xf) | ((biphase_tbl[p[i]] & 0xf) << 4);
547 dst[i / 2] = c;
548 }
549 return err & 0xf0;
550}
551
552static int saa7115_decode_wss(u8 * p)
553{
554 static const int wss_bits[8] = {
555 0, 0, 0, 1, 0, 1, 1, 1
556 };
557 unsigned char parity;
558 int wss = 0;
559 int i;
560
561 for (i = 0; i < 16; i++) {
562 int b1 = wss_bits[p[i] & 7];
563 int b2 = wss_bits[(p[i] >> 3) & 7];
564
565 if (b1 == b2)
566 return -1;
567 wss |= b2 << i;
568 }
569 parity = wss & 15;
570 parity ^= parity >> 2;
571 parity ^= parity >> 1;
572
573 if (!(parity & 1))
574 return -1;
575
576 return wss;
577}
578
579
Hans Verkuil3578d3d2006-01-09 15:25:41 -0200580static int saa7115_set_audio_clock_freq(struct i2c_client *client, u32 freq)
Hans Verkuile19b2fc2005-11-13 16:08:04 -0800581{
582 struct saa7115_state *state = i2c_get_clientdata(client);
Hans Verkuil3578d3d2006-01-09 15:25:41 -0200583 u32 acpf;
584 u32 acni;
585 u32 hz;
586 u64 f;
Hans Verkuilb7f82922006-04-02 12:50:42 -0300587 u8 acc = 0; /* reg 0x3a, audio clock control */
Hans Verkuile19b2fc2005-11-13 16:08:04 -0800588
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200589 v4l_dbg(1, debug, client, "set audio clock freq: %d\n", freq);
Hans Verkuil3578d3d2006-01-09 15:25:41 -0200590
591 /* sanity check */
592 if (freq < 32000 || freq > 48000)
593 return -EINVAL;
594
Hans Verkuilb7f82922006-04-02 12:50:42 -0300595 /* The saa7113 has no audio clock */
596 if (state->ident == V4L2_IDENT_SAA7113)
597 return 0;
598
Hans Verkuil3578d3d2006-01-09 15:25:41 -0200599 /* hz is the refresh rate times 100 */
600 hz = (state->std & V4L2_STD_525_60) ? 5994 : 5000;
601 /* acpf = (256 * freq) / field_frequency == (256 * 100 * freq) / hz */
602 acpf = (25600 * freq) / hz;
603 /* acni = (256 * freq * 2^23) / crystal_frequency =
604 (freq * 2^(8+23)) / crystal_frequency =
Hans Verkuilb7f82922006-04-02 12:50:42 -0300605 (freq << 31) / crystal_frequency */
Hans Verkuil3578d3d2006-01-09 15:25:41 -0200606 f = freq;
607 f = f << 31;
Hans Verkuilb7f82922006-04-02 12:50:42 -0300608 do_div(f, state->crystal_freq);
Hans Verkuil3578d3d2006-01-09 15:25:41 -0200609 acni = f;
Hans Verkuilb7f82922006-04-02 12:50:42 -0300610 if (state->ucgc) {
611 acpf = acpf * state->cgcdiv / 16;
612 acni = acni * state->cgcdiv / 16;
613 acc = 0x80;
614 if (state->cgcdiv == 3)
615 acc |= 0x40;
616 }
617 if (state->apll)
618 acc |= 0x08;
Hans Verkuil3578d3d2006-01-09 15:25:41 -0200619
Hans Verkuilb7f82922006-04-02 12:50:42 -0300620 saa7115_write(client, 0x38, 0x03);
621 saa7115_write(client, 0x39, 0x10);
622 saa7115_write(client, 0x3a, acc);
Hans Verkuil3578d3d2006-01-09 15:25:41 -0200623 saa7115_write(client, 0x30, acpf & 0xff);
624 saa7115_write(client, 0x31, (acpf >> 8) & 0xff);
625 saa7115_write(client, 0x32, (acpf >> 16) & 0x03);
626 saa7115_write(client, 0x34, acni & 0xff);
627 saa7115_write(client, 0x35, (acni >> 8) & 0xff);
628 saa7115_write(client, 0x36, (acni >> 16) & 0x3f);
Hans Verkuile19b2fc2005-11-13 16:08:04 -0800629 state->audclk_freq = freq;
630 return 0;
631}
632
633static int saa7115_set_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
634{
635 struct saa7115_state *state = i2c_get_clientdata(client);
636
637 switch (ctrl->id) {
638 case V4L2_CID_BRIGHTNESS:
639 if (ctrl->value < 0 || ctrl->value > 255) {
Hans Verkuilfac9e892006-01-09 15:32:40 -0200640 v4l_err(client, "invalid brightness setting %d\n", ctrl->value);
Hans Verkuile19b2fc2005-11-13 16:08:04 -0800641 return -ERANGE;
642 }
643
644 state->bright = ctrl->value;
645 saa7115_write(client, 0x0a, state->bright);
646 break;
647
648 case V4L2_CID_CONTRAST:
649 if (ctrl->value < 0 || ctrl->value > 127) {
Hans Verkuilfac9e892006-01-09 15:32:40 -0200650 v4l_err(client, "invalid contrast setting %d\n", ctrl->value);
Hans Verkuile19b2fc2005-11-13 16:08:04 -0800651 return -ERANGE;
652 }
653
654 state->contrast = ctrl->value;
655 saa7115_write(client, 0x0b, state->contrast);
656 break;
657
658 case V4L2_CID_SATURATION:
659 if (ctrl->value < 0 || ctrl->value > 127) {
Hans Verkuilfac9e892006-01-09 15:32:40 -0200660 v4l_err(client, "invalid saturation setting %d\n", ctrl->value);
Hans Verkuile19b2fc2005-11-13 16:08:04 -0800661 return -ERANGE;
662 }
663
664 state->sat = ctrl->value;
665 saa7115_write(client, 0x0c, state->sat);
666 break;
667
668 case V4L2_CID_HUE:
669 if (ctrl->value < -127 || ctrl->value > 127) {
Hans Verkuilfac9e892006-01-09 15:32:40 -0200670 v4l_err(client, "invalid hue setting %d\n", ctrl->value);
Hans Verkuile19b2fc2005-11-13 16:08:04 -0800671 return -ERANGE;
672 }
673
674 state->hue = ctrl->value;
675 saa7115_write(client, 0x0d, state->hue);
676 break;
Hans Verkuil3faeeae2006-01-09 15:25:44 -0200677
678 default:
679 return -EINVAL;
Hans Verkuile19b2fc2005-11-13 16:08:04 -0800680 }
681
682 return 0;
683}
684
685static int saa7115_get_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
686{
687 struct saa7115_state *state = i2c_get_clientdata(client);
688
689 switch (ctrl->id) {
690 case V4L2_CID_BRIGHTNESS:
691 ctrl->value = state->bright;
692 break;
693 case V4L2_CID_CONTRAST:
694 ctrl->value = state->contrast;
695 break;
696 case V4L2_CID_SATURATION:
697 ctrl->value = state->sat;
698 break;
699 case V4L2_CID_HUE:
700 ctrl->value = state->hue;
701 break;
702 default:
703 return -EINVAL;
704 }
705
706 return 0;
707}
708
709static void saa7115_set_v4lstd(struct i2c_client *client, v4l2_std_id std)
710{
711 struct saa7115_state *state = i2c_get_clientdata(client);
712 int taskb = saa7115_read(client, 0x80) & 0x10;
713
Hans Verkuil30b54d52006-01-09 15:25:43 -0200714 /* Prevent unnecessary standard changes. During a standard
715 change the I-Port is temporarily disabled. Any devices
716 reading from that port can get confused.
717 Note that VIDIOC_S_STD is also used to switch from
718 radio to TV mode, so if a VIDIOC_S_STD is broadcast to
719 all I2C devices then you do not want to have an unwanted
720 side-effect here. */
721 if (std == state->std)
722 return;
723
Hans Verkuile19b2fc2005-11-13 16:08:04 -0800724 // This works for NTSC-M, SECAM-L and the 50Hz PAL variants.
725 if (std & V4L2_STD_525_60) {
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200726 v4l_dbg(1, debug, client, "decoder set standard 60 Hz\n");
Hans Verkuile19b2fc2005-11-13 16:08:04 -0800727 saa7115_writeregs(client, saa7115_cfg_60hz_video);
728 } else {
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200729 v4l_dbg(1, debug, client, "decoder set standard 50 Hz\n");
Hans Verkuile19b2fc2005-11-13 16:08:04 -0800730 saa7115_writeregs(client, saa7115_cfg_50hz_video);
731 }
732
Mauro Carvalho Chehabf89982a2006-03-14 16:33:33 -0300733 /* Register 0E - Bits D6-D4 on NO-AUTO mode
734 (SAA7113 doesn't have auto mode)
735 50 Hz / 625 lines 60 Hz / 525 lines
736 000 PAL BGDHI (4.43Mhz) NTSC M (3.58MHz)
737 001 NTSC 4.43 (50 Hz) PAL 4.43 (60 Hz)
738 010 Combination-PAL N (3.58MHz) NTSC 4.43 (60 Hz)
739 011 NTSC N (3.58MHz) PAL M (3.58MHz)
740 100 reserved NTSC-Japan (3.58MHz)
741 */
742 if (state->ident == V4L2_IDENT_SAA7113) {
Hans Verkuil01342352006-03-25 08:19:47 -0300743 u8 reg = saa7115_read(client, 0x0e) & 0x8f;
Mauro Carvalho Chehabf89982a2006-03-14 16:33:33 -0300744
Mauro Carvalho Chehab02c17222006-03-16 18:44:07 -0300745 if (std == V4L2_STD_PAL_M) {
Hans Verkuil01342352006-03-25 08:19:47 -0300746 reg |= 0x30;
Mauro Carvalho Chehab02c17222006-03-16 18:44:07 -0300747 } else if (std == V4L2_STD_PAL_N) {
Hans Verkuil01342352006-03-25 08:19:47 -0300748 reg |= 0x20;
Mauro Carvalho Chehab02c17222006-03-16 18:44:07 -0300749 } else if (std == V4L2_STD_PAL_60) {
Hans Verkuil01342352006-03-25 08:19:47 -0300750 reg |= 0x10;
Mauro Carvalho Chehab02c17222006-03-16 18:44:07 -0300751 } else if (std == V4L2_STD_NTSC_M_JP) {
Hans Verkuil01342352006-03-25 08:19:47 -0300752 reg |= 0x40;
Mauro Carvalho Chehabf89982a2006-03-14 16:33:33 -0300753 }
754 saa7115_write(client, 0x0e, reg);
755 }
756
757
Hans Verkuile19b2fc2005-11-13 16:08:04 -0800758 state->std = std;
759
760 /* restart task B if needed */
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -0300761 if (taskb && state->ident != V4L2_IDENT_SAA7115) {
Hans Verkuile19b2fc2005-11-13 16:08:04 -0800762 saa7115_writeregs(client, saa7115_cfg_vbi_on);
763 }
764
765 /* switch audio mode too! */
766 saa7115_set_audio_clock_freq(client, state->audclk_freq);
767}
768
769static v4l2_std_id saa7115_get_v4lstd(struct i2c_client *client)
770{
771 struct saa7115_state *state = i2c_get_clientdata(client);
772
773 return state->std;
774}
775
776static void saa7115_log_status(struct i2c_client *client)
777{
Hans Verkuile19b2fc2005-11-13 16:08:04 -0800778 struct saa7115_state *state = i2c_get_clientdata(client);
779 int reg1e, reg1f;
780 int signalOk;
781 int vcr;
782
Hans Verkuilfac9e892006-01-09 15:32:40 -0200783 v4l_info(client, "Audio frequency: %d Hz\n", state->audclk_freq);
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -0300784 if (state->ident != V4L2_IDENT_SAA7115) {
Hans Verkuile19b2fc2005-11-13 16:08:04 -0800785 /* status for the saa7114 */
786 reg1f = saa7115_read(client, 0x1f);
787 signalOk = (reg1f & 0xc1) == 0x81;
Hans Verkuilfac9e892006-01-09 15:32:40 -0200788 v4l_info(client, "Video signal: %s\n", signalOk ? "ok" : "bad");
789 v4l_info(client, "Frequency: %s\n", (reg1f & 0x20) ? "60 Hz" : "50 Hz");
Hans Verkuile19b2fc2005-11-13 16:08:04 -0800790 return;
791 }
792
793 /* status for the saa7115 */
794 reg1e = saa7115_read(client, 0x1e);
795 reg1f = saa7115_read(client, 0x1f);
796
797 signalOk = (reg1f & 0xc1) == 0x81 && (reg1e & 0xc0) == 0x80;
798 vcr = !(reg1f & 0x10);
799
Hans Verkuil21fa7152006-01-09 15:25:41 -0200800 if (state->input >= 6) {
Hans Verkuilfac9e892006-01-09 15:32:40 -0200801 v4l_info(client, "Input: S-Video %d\n", state->input - 6);
Hans Verkuil21fa7152006-01-09 15:25:41 -0200802 } else {
Hans Verkuilfac9e892006-01-09 15:32:40 -0200803 v4l_info(client, "Input: Composite %d\n", state->input);
Hans Verkuil21fa7152006-01-09 15:25:41 -0200804 }
Hans Verkuilfac9e892006-01-09 15:32:40 -0200805 v4l_info(client, "Video signal: %s\n", signalOk ? (vcr ? "VCR" : "broadcast/DVD") : "bad");
806 v4l_info(client, "Frequency: %s\n", (reg1f & 0x20) ? "60 Hz" : "50 Hz");
Hans Verkuile19b2fc2005-11-13 16:08:04 -0800807
808 switch (reg1e & 0x03) {
809 case 1:
Hans Verkuilfac9e892006-01-09 15:32:40 -0200810 v4l_info(client, "Detected format: NTSC\n");
Hans Verkuile19b2fc2005-11-13 16:08:04 -0800811 break;
812 case 2:
Hans Verkuilfac9e892006-01-09 15:32:40 -0200813 v4l_info(client, "Detected format: PAL\n");
Hans Verkuile19b2fc2005-11-13 16:08:04 -0800814 break;
815 case 3:
Hans Verkuilfac9e892006-01-09 15:32:40 -0200816 v4l_info(client, "Detected format: SECAM\n");
Hans Verkuile19b2fc2005-11-13 16:08:04 -0800817 break;
818 default:
Hans Verkuilfac9e892006-01-09 15:32:40 -0200819 v4l_info(client, "Detected format: BW/No color\n");
Hans Verkuile19b2fc2005-11-13 16:08:04 -0800820 break;
821 }
822}
823
824/* setup the sliced VBI lcr registers according to the sliced VBI format */
825static void saa7115_set_lcr(struct i2c_client *client, struct v4l2_sliced_vbi_format *fmt)
826{
827 struct saa7115_state *state = i2c_get_clientdata(client);
828 int is_50hz = (state->std & V4L2_STD_625_50);
829 u8 lcr[24];
830 int i, x;
831
Hans Verkuil01342352006-03-25 08:19:47 -0300832 /* saa7113/7114 doesn't yet support VBI */
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -0300833 if (state->ident != V4L2_IDENT_SAA7115)
Hans Verkuile19b2fc2005-11-13 16:08:04 -0800834 return;
835
836 for (i = 0; i <= 23; i++)
837 lcr[i] = 0xff;
838
839 if (fmt->service_set == 0) {
840 /* raw VBI */
841 if (is_50hz)
842 for (i = 6; i <= 23; i++)
843 lcr[i] = 0xdd;
844 else
845 for (i = 10; i <= 21; i++)
846 lcr[i] = 0xdd;
847 } else {
848 /* sliced VBI */
849 /* first clear lines that cannot be captured */
850 if (is_50hz) {
851 for (i = 0; i <= 5; i++)
852 fmt->service_lines[0][i] =
853 fmt->service_lines[1][i] = 0;
854 }
855 else {
856 for (i = 0; i <= 9; i++)
857 fmt->service_lines[0][i] =
858 fmt->service_lines[1][i] = 0;
859 for (i = 22; i <= 23; i++)
860 fmt->service_lines[0][i] =
861 fmt->service_lines[1][i] = 0;
862 }
863
864 /* Now set the lcr values according to the specified service */
865 for (i = 6; i <= 23; i++) {
866 lcr[i] = 0;
867 for (x = 0; x <= 1; x++) {
868 switch (fmt->service_lines[1-x][i]) {
869 case 0:
870 lcr[i] |= 0xf << (4 * x);
871 break;
Hans Verkuil9bc74002006-03-29 18:02:51 -0300872 case V4L2_SLICED_TELETEXT_B:
Hans Verkuile19b2fc2005-11-13 16:08:04 -0800873 lcr[i] |= 1 << (4 * x);
874 break;
875 case V4L2_SLICED_CAPTION_525:
876 lcr[i] |= 4 << (4 * x);
877 break;
878 case V4L2_SLICED_WSS_625:
879 lcr[i] |= 5 << (4 * x);
880 break;
881 case V4L2_SLICED_VPS:
882 lcr[i] |= 7 << (4 * x);
883 break;
884 }
885 }
886 }
887 }
888
889 /* write the lcr registers */
890 for (i = 2; i <= 23; i++) {
891 saa7115_write(client, i - 2 + 0x41, lcr[i]);
892 }
893
894 /* enable/disable raw VBI capturing */
895 saa7115_writeregs(client, fmt->service_set == 0 ? saa7115_cfg_vbi_on : saa7115_cfg_vbi_off);
896}
897
898static int saa7115_get_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
899{
900 static u16 lcr2vbi[] = {
Hans Verkuil9bc74002006-03-29 18:02:51 -0300901 0, V4L2_SLICED_TELETEXT_B, 0, /* 1 */
Hans Verkuile19b2fc2005-11-13 16:08:04 -0800902 0, V4L2_SLICED_CAPTION_525, /* 4 */
903 V4L2_SLICED_WSS_625, 0, /* 5 */
904 V4L2_SLICED_VPS, 0, 0, 0, 0, /* 7 */
905 0, 0, 0, 0
906 };
907 struct v4l2_sliced_vbi_format *sliced = &fmt->fmt.sliced;
908 int i;
909
910 if (fmt->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
911 return -EINVAL;
912 memset(sliced, 0, sizeof(*sliced));
913 /* done if using raw VBI */
914 if (saa7115_read(client, 0x80) & 0x10)
915 return 0;
916 for (i = 2; i <= 23; i++) {
917 u8 v = saa7115_read(client, i - 2 + 0x41);
918
919 sliced->service_lines[0][i] = lcr2vbi[v >> 4];
920 sliced->service_lines[1][i] = lcr2vbi[v & 0xf];
921 sliced->service_set |=
922 sliced->service_lines[0][i] | sliced->service_lines[1][i];
923 }
924 return 0;
925}
926
927static int saa7115_set_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
928{
929 struct saa7115_state *state = i2c_get_clientdata(client);
930 struct v4l2_pix_format *pix;
931 int HPSC, HFSC;
932 int VSCY, Vsrc;
933 int is_50hz = state->std & V4L2_STD_625_50;
934
935 if (fmt->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
936 saa7115_set_lcr(client, &fmt->fmt.sliced);
937 return 0;
938 }
939 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
940 return -EINVAL;
941
942 pix = &(fmt->fmt.pix);
943
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200944 v4l_dbg(1, debug, client, "decoder set size\n");
Hans Verkuile19b2fc2005-11-13 16:08:04 -0800945
946 /* FIXME need better bounds checking here */
947 if ((pix->width < 1) || (pix->width > 1440))
948 return -EINVAL;
949 if ((pix->height < 1) || (pix->height > 960))
950 return -EINVAL;
951
952 /* probably have a valid size, let's set it */
953 /* Set output width/height */
954 /* width */
955 saa7115_write(client, 0xcc, (u8) (pix->width & 0xff));
956 saa7115_write(client, 0xcd, (u8) ((pix->width >> 8) & 0xff));
957 /* height */
958 saa7115_write(client, 0xce, (u8) (pix->height & 0xff));
959 saa7115_write(client, 0xcf, (u8) ((pix->height >> 8) & 0xff));
960
961 /* Scaling settings */
962 /* Hprescaler is floor(inres/outres) */
963 /* FIXME hardcoding input res */
964 if (pix->width != 720) {
965 HPSC = (int)(720 / pix->width);
966 /* 0 is not allowed (div. by zero) */
967 HPSC = HPSC ? HPSC : 1;
968 HFSC = (int)((1024 * 720) / (HPSC * pix->width));
969
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200970 v4l_dbg(1, debug, client, "Hpsc: 0x%05x, Hfsc: 0x%05x\n", HPSC, HFSC);
Hans Verkuile19b2fc2005-11-13 16:08:04 -0800971 /* FIXME hardcodes to "Task B"
972 * write H prescaler integer */
973 saa7115_write(client, 0xd0, (u8) (HPSC & 0x3f));
974
975 /* write H fine-scaling (luminance) */
976 saa7115_write(client, 0xd8, (u8) (HFSC & 0xff));
977 saa7115_write(client, 0xd9, (u8) ((HFSC >> 8) & 0xff));
978 /* write H fine-scaling (chrominance)
979 * must be lum/2, so i'll just bitshift :) */
980 saa7115_write(client, 0xDC, (u8) ((HFSC >> 1) & 0xff));
981 saa7115_write(client, 0xDD, (u8) ((HFSC >> 9) & 0xff));
982 } else {
983 if (is_50hz) {
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200984 v4l_dbg(1, debug, client, "Setting full 50hz width\n");
Hans Verkuile19b2fc2005-11-13 16:08:04 -0800985 saa7115_writeregs(client, saa7115_cfg_50hz_fullres_x);
986 } else {
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200987 v4l_dbg(1, debug, client, "Setting full 60hz width\n");
Hans Verkuile19b2fc2005-11-13 16:08:04 -0800988 saa7115_writeregs(client, saa7115_cfg_60hz_fullres_x);
989 }
990 }
991
992 Vsrc = is_50hz ? 576 : 480;
993
994 if (pix->height != Vsrc) {
995 VSCY = (int)((1024 * Vsrc) / pix->height);
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200996 v4l_dbg(1, debug, client, "Vsrc: %d, Vscy: 0x%05x\n", Vsrc, VSCY);
Hans Verkuile19b2fc2005-11-13 16:08:04 -0800997
998 /* Correct Contrast and Luminance */
999 saa7115_write(client, 0xd5, (u8) (64 * 1024 / VSCY));
1000 saa7115_write(client, 0xd6, (u8) (64 * 1024 / VSCY));
1001
1002 /* write V fine-scaling (luminance) */
1003 saa7115_write(client, 0xe0, (u8) (VSCY & 0xff));
1004 saa7115_write(client, 0xe1, (u8) ((VSCY >> 8) & 0xff));
1005 /* write V fine-scaling (chrominance) */
1006 saa7115_write(client, 0xe2, (u8) (VSCY & 0xff));
1007 saa7115_write(client, 0xe3, (u8) ((VSCY >> 8) & 0xff));
1008 } else {
1009 if (is_50hz) {
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -02001010 v4l_dbg(1, debug, client, "Setting full 50Hz height\n");
Hans Verkuile19b2fc2005-11-13 16:08:04 -08001011 saa7115_writeregs(client, saa7115_cfg_50hz_fullres_y);
1012 } else {
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -02001013 v4l_dbg(1, debug, client, "Setting full 60hz height\n");
Hans Verkuile19b2fc2005-11-13 16:08:04 -08001014 saa7115_writeregs(client, saa7115_cfg_60hz_fullres_y);
1015 }
1016 }
1017
1018 saa7115_writeregs(client, saa7115_cfg_reset_scaler);
1019 return 0;
1020}
1021
1022/* Decode the sliced VBI data stream as created by the saa7115.
1023 The format is described in the saa7115 datasheet in Tables 25 and 26
1024 and in Figure 33.
1025 The current implementation uses SAV/EAV codes and not the ancillary data
1026 headers. The vbi->p pointer points to the SDID byte right after the SAV
1027 code. */
1028static void saa7115_decode_vbi_line(struct i2c_client *client,
1029 struct v4l2_decode_vbi_line *vbi)
1030{
1031 static const char vbi_no_data_pattern[] = {
1032 0xa0, 0xa0, 0xa0, 0xa0, 0xa0, 0xa0, 0xa0, 0xa0, 0xa0, 0xa0
1033 };
1034 struct saa7115_state *state = i2c_get_clientdata(client);
1035 u8 *p = vbi->p;
1036 u32 wss;
1037 int id1, id2; /* the ID1 and ID2 bytes from the internal header */
1038
1039 vbi->type = 0; /* mark result as a failure */
1040 id1 = p[2];
1041 id2 = p[3];
1042 /* Note: the field bit is inverted for 60 Hz video */
1043 if (state->std & V4L2_STD_525_60)
1044 id1 ^= 0x40;
1045
1046 /* Skip internal header, p now points to the start of the payload */
1047 p += 4;
1048 vbi->p = p;
1049
1050 /* calculate field and line number of the VBI packet (1-23) */
1051 vbi->is_second_field = ((id1 & 0x40) != 0);
1052 vbi->line = (id1 & 0x3f) << 3;
1053 vbi->line |= (id2 & 0x70) >> 4;
1054
1055 /* Obtain data type */
1056 id2 &= 0xf;
1057
1058 /* If the VBI slicer does not detect any signal it will fill up
1059 the payload buffer with 0xa0 bytes. */
1060 if (!memcmp(p, vbi_no_data_pattern, sizeof(vbi_no_data_pattern)))
1061 return;
1062
1063 /* decode payloads */
1064 switch (id2) {
1065 case 1:
Hans Verkuil9bc74002006-03-29 18:02:51 -03001066 vbi->type = V4L2_SLICED_TELETEXT_B;
Hans Verkuile19b2fc2005-11-13 16:08:04 -08001067 break;
1068 case 4:
1069 if (!saa7115_odd_parity(p[0]) || !saa7115_odd_parity(p[1]))
1070 return;
1071 vbi->type = V4L2_SLICED_CAPTION_525;
1072 break;
1073 case 5:
1074 wss = saa7115_decode_wss(p);
1075 if (wss == -1)
1076 return;
1077 p[0] = wss & 0xff;
1078 p[1] = wss >> 8;
1079 vbi->type = V4L2_SLICED_WSS_625;
1080 break;
1081 case 7:
1082 if (saa7115_decode_vps(p, p) != 0)
1083 return;
1084 vbi->type = V4L2_SLICED_VPS;
1085 break;
1086 default:
1087 return;
1088 }
1089}
1090
1091/* ============ SAA7115 AUDIO settings (end) ============= */
1092
Hans Verkuilda4ae5a2006-01-09 15:32:41 -02001093static struct v4l2_queryctrl saa7115_qctrl[] = {
1094 {
1095 .id = V4L2_CID_BRIGHTNESS,
1096 .type = V4L2_CTRL_TYPE_INTEGER,
1097 .name = "Brightness",
1098 .minimum = 0,
1099 .maximum = 255,
1100 .step = 1,
1101 .default_value = 128,
1102 .flags = 0,
1103 }, {
1104 .id = V4L2_CID_CONTRAST,
1105 .type = V4L2_CTRL_TYPE_INTEGER,
1106 .name = "Contrast",
1107 .minimum = 0,
Hans Verkuilecc0b942006-02-27 00:08:20 -03001108 .maximum = 127,
Hans Verkuilda4ae5a2006-01-09 15:32:41 -02001109 .step = 1,
1110 .default_value = 64,
1111 .flags = 0,
1112 }, {
1113 .id = V4L2_CID_SATURATION,
1114 .type = V4L2_CTRL_TYPE_INTEGER,
1115 .name = "Saturation",
1116 .minimum = 0,
Hans Verkuilecc0b942006-02-27 00:08:20 -03001117 .maximum = 127,
Hans Verkuilda4ae5a2006-01-09 15:32:41 -02001118 .step = 1,
1119 .default_value = 64,
1120 .flags = 0,
1121 }, {
1122 .id = V4L2_CID_HUE,
1123 .type = V4L2_CTRL_TYPE_INTEGER,
1124 .name = "Hue",
1125 .minimum = -128,
1126 .maximum = 127,
1127 .step = 1,
1128 .default_value = 0,
1129 .flags = 0,
1130 },
1131};
1132
1133/* ----------------------------------------------------------------------- */
1134
Hans Verkuile19b2fc2005-11-13 16:08:04 -08001135static int saa7115_command(struct i2c_client *client, unsigned int cmd, void *arg)
1136{
1137 struct saa7115_state *state = i2c_get_clientdata(client);
1138 int *iarg = arg;
1139
1140 /* ioctls to allow direct access to the saa7115 registers for testing */
1141 switch (cmd) {
1142 case VIDIOC_S_FMT:
1143 return saa7115_set_v4lfmt(client, (struct v4l2_format *)arg);
1144
1145 case VIDIOC_G_FMT:
1146 return saa7115_get_v4lfmt(client, (struct v4l2_format *)arg);
1147
1148 case VIDIOC_INT_AUDIO_CLOCK_FREQ:
Hans Verkuil3578d3d2006-01-09 15:25:41 -02001149 return saa7115_set_audio_clock_freq(client, *(u32 *)arg);
Hans Verkuile19b2fc2005-11-13 16:08:04 -08001150
1151 case VIDIOC_G_TUNER:
1152 {
1153 struct v4l2_tuner *vt = arg;
1154 int status;
1155
Hans Verkuil3faeeae2006-01-09 15:25:44 -02001156 if (state->radio)
1157 break;
Hans Verkuile19b2fc2005-11-13 16:08:04 -08001158 status = saa7115_read(client, 0x1f);
1159
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -02001160 v4l_dbg(1, debug, client, "status: 0x%02x\n", status);
Hans Verkuile19b2fc2005-11-13 16:08:04 -08001161 vt->signal = ((status & (1 << 6)) == 0) ? 0xffff : 0x0;
1162 break;
1163 }
1164
1165 case VIDIOC_LOG_STATUS:
1166 saa7115_log_status(client);
1167 break;
1168
1169 case VIDIOC_G_CTRL:
1170 return saa7115_get_v4lctrl(client, (struct v4l2_control *)arg);
1171
1172 case VIDIOC_S_CTRL:
1173 return saa7115_set_v4lctrl(client, (struct v4l2_control *)arg);
1174
Hans Verkuilda4ae5a2006-01-09 15:32:41 -02001175 case VIDIOC_QUERYCTRL:
1176 {
1177 struct v4l2_queryctrl *qc = arg;
1178 int i;
1179
1180 for (i = 0; i < ARRAY_SIZE(saa7115_qctrl); i++)
1181 if (qc->id && qc->id == saa7115_qctrl[i].id) {
1182 memcpy(qc, &saa7115_qctrl[i], sizeof(*qc));
1183 return 0;
1184 }
1185 return -EINVAL;
1186 }
1187
Hans Verkuile19b2fc2005-11-13 16:08:04 -08001188 case VIDIOC_G_STD:
1189 *(v4l2_std_id *)arg = saa7115_get_v4lstd(client);
1190 break;
1191
1192 case VIDIOC_S_STD:
Hans Verkuil3faeeae2006-01-09 15:25:44 -02001193 state->radio = 0;
Hans Verkuile19b2fc2005-11-13 16:08:04 -08001194 saa7115_set_v4lstd(client, *(v4l2_std_id *)arg);
1195 break;
1196
Hans Verkuil3faeeae2006-01-09 15:25:44 -02001197 case AUDC_SET_RADIO:
1198 state->radio = 1;
1199 break;
1200
Hans Verkuil1f8f5fa2006-03-25 09:20:28 -03001201 case VIDIOC_INT_G_VIDEO_ROUTING:
1202 {
1203 struct v4l2_routing *route = arg;
1204
1205 route->input = state->input;
1206 route->output = 0;
1207 break;
1208 }
1209
1210 case VIDIOC_INT_S_VIDEO_ROUTING:
1211 {
1212 struct v4l2_routing *route = arg;
1213
1214 v4l_dbg(1, debug, client, "decoder set input %d\n", route->input);
1215 /* saa7113 does not have these inputs */
1216 if (state->ident == V4L2_IDENT_SAA7113 &&
1217 (route->input == SAA7115_COMPOSITE4 ||
1218 route->input == SAA7115_COMPOSITE5)) {
1219 return -EINVAL;
1220 }
1221 if (route->input > SAA7115_SVIDEO3)
1222 return -EINVAL;
1223 if (state->input == route->input)
1224 break;
1225 v4l_dbg(1, debug, client, "now setting %s input\n",
1226 (route->input >= SAA7115_SVIDEO0) ? "S-Video" : "Composite");
1227 state->input = route->input;
1228
1229 /* select mode */
1230 saa7115_write(client, 0x02,
1231 (saa7115_read(client, 0x02) & 0xf0) |
1232 state->input);
1233
1234 /* bypass chrominance trap for S-Video modes */
1235 saa7115_write(client, 0x09,
1236 (saa7115_read(client, 0x09) & 0x7f) |
1237 (state->input >= SAA7115_SVIDEO0 ? 0x80 : 0x0));
1238 break;
1239 }
1240
Hans Verkuile19b2fc2005-11-13 16:08:04 -08001241 case VIDIOC_G_INPUT:
1242 *(int *)arg = state->input;
1243 break;
1244
1245 case VIDIOC_S_INPUT:
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -02001246 v4l_dbg(1, debug, client, "decoder set input %d\n", *iarg);
Hans Verkuile19b2fc2005-11-13 16:08:04 -08001247 /* inputs from 0-9 are available */
1248 if (*iarg < 0 || *iarg > 9) {
1249 return -EINVAL;
1250 }
1251
1252 if (state->input == *iarg)
1253 break;
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -02001254 v4l_dbg(1, debug, client, "now setting %s input\n",
Hans Verkuile19b2fc2005-11-13 16:08:04 -08001255 *iarg >= 6 ? "S-Video" : "Composite");
1256 state->input = *iarg;
1257
1258 /* select mode */
1259 saa7115_write(client, 0x02,
1260 (saa7115_read(client, 0x02) & 0xf0) |
1261 state->input);
1262
1263 /* bypass chrominance trap for modes 6..9 */
1264 saa7115_write(client, 0x09,
1265 (saa7115_read(client, 0x09) & 0x7f) |
1266 (state->input < 6 ? 0x0 : 0x80));
1267 break;
1268
1269 case VIDIOC_STREAMON:
1270 case VIDIOC_STREAMOFF:
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -02001271 v4l_dbg(1, debug, client, "%s output\n",
Hans Verkuile19b2fc2005-11-13 16:08:04 -08001272 (cmd == VIDIOC_STREAMON) ? "enable" : "disable");
1273
1274 if (state->enable != (cmd == VIDIOC_STREAMON)) {
1275 state->enable = (cmd == VIDIOC_STREAMON);
1276 saa7115_write(client, 0x87, state->enable);
1277 }
1278 break;
1279
Hans Verkuilb7f82922006-04-02 12:50:42 -03001280 case VIDIOC_INT_S_CRYSTAL_FREQ:
1281 {
1282 struct v4l2_crystal_freq *freq = arg;
1283
1284 if (freq->freq != SAA7115_FREQ_32_11_MHZ &&
1285 freq->freq != SAA7115_FREQ_24_576_MHZ)
1286 return -EINVAL;
1287 state->crystal_freq = freq->freq;
1288 state->cgcdiv = (freq->flags & SAA7115_FREQ_FL_CGCDIV) ? 3 : 4;
1289 state->ucgc = (freq->flags & SAA7115_FREQ_FL_UCGC) ? 1 : 0;
1290 state->apll = (freq->flags & SAA7115_FREQ_FL_APLL) ? 1 : 0;
1291 saa7115_set_audio_clock_freq(client, state->audclk_freq);
1292 break;
1293 }
1294
Hans Verkuile19b2fc2005-11-13 16:08:04 -08001295 case VIDIOC_INT_DECODE_VBI_LINE:
1296 saa7115_decode_vbi_line(client, arg);
1297 break;
1298
1299 case VIDIOC_INT_RESET:
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -02001300 v4l_dbg(1, debug, client, "decoder RESET\n");
Hans Verkuile19b2fc2005-11-13 16:08:04 -08001301 saa7115_writeregs(client, saa7115_cfg_reset_scaler);
1302 break;
1303
1304 case VIDIOC_INT_G_VBI_DATA:
1305 {
1306 struct v4l2_sliced_vbi_data *data = arg;
1307
1308 switch (data->id) {
1309 case V4L2_SLICED_WSS_625:
1310 if (saa7115_read(client, 0x6b) & 0xc0)
1311 return -EIO;
1312 data->data[0] = saa7115_read(client, 0x6c);
1313 data->data[1] = saa7115_read(client, 0x6d);
1314 return 0;
1315 case V4L2_SLICED_CAPTION_525:
1316 if (data->field == 0) {
1317 /* CC */
1318 if (saa7115_read(client, 0x66) & 0xc0)
1319 return -EIO;
1320 data->data[0] = saa7115_read(client, 0x67);
1321 data->data[1] = saa7115_read(client, 0x68);
1322 return 0;
1323 }
1324 /* XDS */
1325 if (saa7115_read(client, 0x66) & 0x30)
1326 return -EIO;
1327 data->data[0] = saa7115_read(client, 0x69);
1328 data->data[1] = saa7115_read(client, 0x6a);
1329 return 0;
1330 default:
1331 return -EINVAL;
1332 }
1333 break;
1334 }
1335
1336#ifdef CONFIG_VIDEO_ADV_DEBUG
1337 case VIDIOC_INT_G_REGISTER:
1338 {
1339 struct v4l2_register *reg = arg;
1340
1341 if (reg->i2c_id != I2C_DRIVERID_SAA711X)
1342 return -EINVAL;
1343 reg->val = saa7115_read(client, reg->reg & 0xff);
1344 break;
1345 }
1346
1347 case VIDIOC_INT_S_REGISTER:
1348 {
1349 struct v4l2_register *reg = arg;
1350
1351 if (reg->i2c_id != I2C_DRIVERID_SAA711X)
1352 return -EINVAL;
1353 if (!capable(CAP_SYS_ADMIN))
1354 return -EPERM;
1355 saa7115_write(client, reg->reg & 0xff, reg->val & 0xff);
1356 break;
1357 }
1358#endif
1359
1360 case VIDIOC_INT_G_CHIP_IDENT:
1361 *iarg = state->ident;
1362 break;
1363
1364 default:
1365 return -EINVAL;
1366 }
1367
1368 return 0;
1369}
1370
1371/* ----------------------------------------------------------------------- */
1372
1373static struct i2c_driver i2c_driver_saa7115;
1374
1375static int saa7115_attach(struct i2c_adapter *adapter, int address, int kind)
1376{
1377 struct i2c_client *client;
1378 struct saa7115_state *state;
1379 u8 chip_id;
1380
1381 /* Check if the adapter supports the needed features */
1382 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1383 return 0;
1384
Panagiotis Issaris74081872006-01-11 19:40:56 -02001385 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
Hans Verkuile19b2fc2005-11-13 16:08:04 -08001386 if (client == 0)
1387 return -ENOMEM;
Hans Verkuile19b2fc2005-11-13 16:08:04 -08001388 client->addr = address;
1389 client->adapter = adapter;
1390 client->driver = &i2c_driver_saa7115;
Hans Verkuile19b2fc2005-11-13 16:08:04 -08001391 snprintf(client->name, sizeof(client->name) - 1, "saa7115");
1392
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -02001393 v4l_dbg(1, debug, client, "detecting saa7115 client on address 0x%x\n", address << 1);
Hans Verkuile19b2fc2005-11-13 16:08:04 -08001394
1395 saa7115_write(client, 0, 5);
1396 chip_id = saa7115_read(client, 0) & 0x0f;
Hans Verkuil01342352006-03-25 08:19:47 -03001397 if (chip_id < 3 && chip_id > 5) {
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -02001398 v4l_dbg(1, debug, client, "saa7115 not found\n");
Hans Verkuile19b2fc2005-11-13 16:08:04 -08001399 kfree(client);
1400 return 0;
1401 }
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -03001402 snprintf(client->name, sizeof(client->name) - 1, "saa711%d",chip_id);
Hans Verkuilfac9e892006-01-09 15:32:40 -02001403 v4l_info(client, "saa711%d found @ 0x%x (%s)\n", chip_id, address << 1, adapter->name);
Hans Verkuile19b2fc2005-11-13 16:08:04 -08001404
Panagiotis Issaris74081872006-01-11 19:40:56 -02001405 state = kzalloc(sizeof(struct saa7115_state), GFP_KERNEL);
Hans Verkuile19b2fc2005-11-13 16:08:04 -08001406 i2c_set_clientdata(client, state);
1407 if (state == NULL) {
1408 kfree(client);
1409 return -ENOMEM;
1410 }
Hans Verkuile19b2fc2005-11-13 16:08:04 -08001411 state->std = V4L2_STD_NTSC;
1412 state->input = -1;
1413 state->enable = 1;
Hans Verkuil3faeeae2006-01-09 15:25:44 -02001414 state->radio = 0;
Hans Verkuile19b2fc2005-11-13 16:08:04 -08001415 state->bright = 128;
1416 state->contrast = 64;
1417 state->hue = 0;
1418 state->sat = 64;
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -03001419 switch (chip_id) {
1420 case 3:
1421 state->ident = V4L2_IDENT_SAA7113;
1422 break;
1423 case 4:
1424 state->ident = V4L2_IDENT_SAA7114;
1425 break;
1426 default:
1427 state->ident = V4L2_IDENT_SAA7115;
1428 break;
1429 }
1430
Hans Verkuil3578d3d2006-01-09 15:25:41 -02001431 state->audclk_freq = 48000;
Hans Verkuile19b2fc2005-11-13 16:08:04 -08001432
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -02001433 v4l_dbg(1, debug, client, "writing init values\n");
Hans Verkuile19b2fc2005-11-13 16:08:04 -08001434
1435 /* init to 60hz/48khz */
Hans Verkuilb7f82922006-04-02 12:50:42 -03001436 if (state->ident == V4L2_IDENT_SAA7113) {
1437 state->crystal_freq = SAA7115_FREQ_24_576_MHZ;
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -03001438 saa7115_writeregs(client, saa7113_init_auto_input);
Hans Verkuilb7f82922006-04-02 12:50:42 -03001439 } else {
1440 state->crystal_freq = SAA7115_FREQ_32_11_MHZ;
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -03001441 saa7115_writeregs(client, saa7115_init_auto_input);
Hans Verkuilb7f82922006-04-02 12:50:42 -03001442 }
Hans Verkuile19b2fc2005-11-13 16:08:04 -08001443 saa7115_writeregs(client, saa7115_init_misc);
1444 saa7115_writeregs(client, saa7115_cfg_60hz_fullres_x);
1445 saa7115_writeregs(client, saa7115_cfg_60hz_fullres_y);
1446 saa7115_writeregs(client, saa7115_cfg_60hz_video);
Hans Verkuil3578d3d2006-01-09 15:25:41 -02001447 saa7115_set_audio_clock_freq(client, state->audclk_freq);
Hans Verkuile19b2fc2005-11-13 16:08:04 -08001448 saa7115_writeregs(client, saa7115_cfg_reset_scaler);
1449
1450 i2c_attach_client(client);
1451
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -02001452 v4l_dbg(1, debug, client, "status: (1E) 0x%02x, (1F) 0x%02x\n",
Hans Verkuile19b2fc2005-11-13 16:08:04 -08001453 saa7115_read(client, 0x1e), saa7115_read(client, 0x1f));
1454
1455 return 0;
1456}
1457
1458static int saa7115_probe(struct i2c_adapter *adapter)
1459{
Hans Verkuile19b2fc2005-11-13 16:08:04 -08001460 if (adapter->class & I2C_CLASS_TV_ANALOG)
Hans Verkuile19b2fc2005-11-13 16:08:04 -08001461 return i2c_probe(adapter, &addr_data, &saa7115_attach);
1462 return 0;
1463}
1464
1465static int saa7115_detach(struct i2c_client *client)
1466{
1467 struct saa7115_state *state = i2c_get_clientdata(client);
1468 int err;
1469
1470 err = i2c_detach_client(client);
1471 if (err) {
1472 return err;
1473 }
1474
1475 kfree(state);
1476 kfree(client);
1477 return 0;
1478}
1479
1480/* ----------------------------------------------------------------------- */
1481
1482/* i2c implementation */
1483static struct i2c_driver i2c_driver_saa7115 = {
Laurent Riffard604f28e2005-11-26 20:43:39 +01001484 .driver = {
Laurent Riffard604f28e2005-11-26 20:43:39 +01001485 .name = "saa7115",
1486 },
Hans Verkuile19b2fc2005-11-13 16:08:04 -08001487 .id = I2C_DRIVERID_SAA711X,
Hans Verkuile19b2fc2005-11-13 16:08:04 -08001488 .attach_adapter = saa7115_probe,
1489 .detach_client = saa7115_detach,
1490 .command = saa7115_command,
Hans Verkuile19b2fc2005-11-13 16:08:04 -08001491};
1492
1493
1494static int __init saa7115_init_module(void)
1495{
1496 return i2c_add_driver(&i2c_driver_saa7115);
1497}
1498
1499static void __exit saa7115_cleanup_module(void)
1500{
1501 i2c_del_driver(&i2c_driver_saa7115);
1502}
1503
1504module_init(saa7115_init_module);
1505module_exit(saa7115_cleanup_module);