blob: a4738a2fb4d32753498b0091fdcf97d716cbd94f [file] [log] [blame]
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * saa7111 - Philips SAA7111A video decoder driver version 0.0.3
3 *
4 * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
5 *
6 * Slight changes for video timing and attachment output by
7 * Wolfgang Scherr <scherr@net4you.net>
8 *
9 * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
10 * - moved over to linux>=2.4.x i2c protocol (1/1/2003)
11 *
12 * Changes by Michael Hunold <michael@mihu.de>
13 * - implemented DECODER_SET_GPIO, DECODER_INIT, DECODER_SET_VBI_BYPASS
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 */
29
30#include <linux/module.h>
Mauro Carvalho Chehab18f3fa12007-07-02 15:39:57 -030031#include <linux/types.h>
Hans Verkuil4e1a04d2008-09-07 08:00:49 -030032#include <linux/ioctl.h>
Mauro Carvalho Chehab18f3fa12007-07-02 15:39:57 -030033#include <asm/uaccess.h>
Hans Verkuil4e1a04d2008-09-07 08:00:49 -030034#include <linux/i2c.h>
35#include <linux/i2c-id.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/videodev.h>
Mauro Carvalho Chehab18f3fa12007-07-02 15:39:57 -030037#include <linux/video_decoder.h>
Hans Verkuil4e1a04d2008-09-07 08:00:49 -030038#include <media/v4l2-common.h>
39#include <media/v4l2-i2c-drv-legacy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41MODULE_DESCRIPTION("Philips SAA7111 video decoder driver");
42MODULE_AUTHOR("Dave Perks");
43MODULE_LICENSE("GPL");
44
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030045static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046module_param(debug, int, 0644);
47MODULE_PARM_DESC(debug, "Debug level (0-1)");
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/* ----------------------------------------------------------------------- */
50
Jean Delvare6eb5d9c2006-03-22 03:48:32 -030051#define SAA7111_NR_REG 0x18
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053struct saa7111 {
Jean Delvare6eb5d9c2006-03-22 03:48:32 -030054 unsigned char reg[SAA7111_NR_REG];
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56 int norm;
57 int input;
58 int enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/* ----------------------------------------------------------------------- */
62
Hans Verkuil4e1a04d2008-09-07 08:00:49 -030063static inline int saa7111_write(struct i2c_client *client, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 struct saa7111 *decoder = i2c_get_clientdata(client);
66
67 decoder->reg[reg] = value;
68 return i2c_smbus_write_byte_data(client, reg, value);
69}
70
Hans Verkuil4e1a04d2008-09-07 08:00:49 -030071static inline void saa7111_write_if_changed(struct i2c_client *client, u8 reg, u8 value)
Russell King624fc7f2007-05-26 07:55:32 -030072{
73 struct saa7111 *decoder = i2c_get_clientdata(client);
74
75 if (decoder->reg[reg] != value) {
76 decoder->reg[reg] = value;
77 i2c_smbus_write_byte_data(client, reg, value);
78 }
79}
80
Hans Verkuil4e1a04d2008-09-07 08:00:49 -030081static int saa7111_write_block(struct i2c_client *client, const u8 *data, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83 int ret = -1;
84 u8 reg;
85
86 /* the saa7111 has an autoincrement function, use it if
87 * the adapter understands raw I2C */
88 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
89 /* do raw I2C, not smbus compatible */
90 struct saa7111 *decoder = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 u8 block_data[32];
Jean Delvare9aa45e32006-03-22 03:48:35 -030092 int block_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 while (len >= 2) {
Jean Delvare9aa45e32006-03-22 03:48:35 -030095 block_len = 0;
96 block_data[block_len++] = reg = data[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 do {
Jean Delvare9aa45e32006-03-22 03:48:35 -030098 block_data[block_len++] =
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 decoder->reg[reg++] = data[1];
100 len -= 2;
101 data += 2;
Hans Verkuil4e1a04d2008-09-07 08:00:49 -0300102 } while (len >= 2 && data[0] == reg && block_len < 32);
103 ret = i2c_master_send(client, block_data, block_len);
104 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 break;
106 }
107 } else {
108 /* do some slow I2C emulation kind of thing */
109 while (len >= 2) {
110 reg = *data++;
Hans Verkuil4e1a04d2008-09-07 08:00:49 -0300111 ret = saa7111_write(client, reg, *data++);
112 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 break;
114 len -= 2;
115 }
116 }
117
118 return ret;
119}
120
Hans Verkuil4e1a04d2008-09-07 08:00:49 -0300121static int saa7111_init_decoder(struct i2c_client *client,
122 struct video_decoder_init *init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 return saa7111_write_block(client, init->data, init->len);
125}
126
Hans Verkuil4e1a04d2008-09-07 08:00:49 -0300127static inline int saa7111_read(struct i2c_client *client, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 return i2c_smbus_read_byte_data(client, reg);
130}
131
132/* ----------------------------------------------------------------------- */
133
134static const unsigned char saa7111_i2c_init[] = {
135 0x00, 0x00, /* 00 - ID byte */
136 0x01, 0x00, /* 01 - reserved */
137
138 /*front end */
139 0x02, 0xd0, /* 02 - FUSE=3, GUDL=2, MODE=0 */
140 0x03, 0x23, /* 03 - HLNRS=0, VBSL=1, WPOFF=0,
141 * HOLDG=0, GAFIX=0, GAI1=256, GAI2=256 */
142 0x04, 0x00, /* 04 - GAI1=256 */
143 0x05, 0x00, /* 05 - GAI2=256 */
144
145 /* decoder */
146 0x06, 0xf3, /* 06 - HSB at 13(50Hz) / 17(60Hz)
147 * pixels after end of last line */
148 /*0x07, 0x13, * 07 - HSS at 113(50Hz) / 117(60Hz) pixels
149 * after end of last line */
150 0x07, 0xe8, /* 07 - HSS seems to be needed to
151 * work with NTSC, too */
152 0x08, 0xc8, /* 08 - AUFD=1, FSEL=1, EXFIL=0,
153 * VTRC=1, HPLL=0, VNOI=0 */
154 0x09, 0x01, /* 09 - BYPS=0, PREF=0, BPSS=0,
155 * VBLB=0, UPTCV=0, APER=1 */
156 0x0a, 0x80, /* 0a - BRIG=128 */
157 0x0b, 0x47, /* 0b - CONT=1.109 */
158 0x0c, 0x40, /* 0c - SATN=1.0 */
159 0x0d, 0x00, /* 0d - HUE=0 */
160 0x0e, 0x01, /* 0e - CDTO=0, CSTD=0, DCCF=0,
161 * FCTC=0, CHBW=1 */
162 0x0f, 0x00, /* 0f - reserved */
163 0x10, 0x48, /* 10 - OFTS=1, HDEL=0, VRLN=1, YDEL=0 */
164 0x11, 0x1c, /* 11 - GPSW=0, CM99=0, FECO=0, COMPO=1,
165 * OEYC=1, OEHV=1, VIPB=0, COLO=0 */
166 0x12, 0x00, /* 12 - output control 2 */
167 0x13, 0x00, /* 13 - output control 3 */
168 0x14, 0x00, /* 14 - reserved */
169 0x15, 0x00, /* 15 - VBI */
170 0x16, 0x00, /* 16 - VBI */
171 0x17, 0x00, /* 17 - VBI */
172};
173
Hans Verkuil4e1a04d2008-09-07 08:00:49 -0300174static int saa7111_command(struct i2c_client *client, unsigned cmd, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 struct saa7111 *decoder = i2c_get_clientdata(client);
177
178 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 case 0:
Martin Samuelsson58a0b842006-03-22 03:48:38 -0300180 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 case DECODER_INIT:
182 {
183 struct video_decoder_init *init = arg;
Hans Verkuil4e1a04d2008-09-07 08:00:49 -0300184 struct video_decoder_init vdi;
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 if (NULL != init)
187 return saa7111_init_decoder(client, init);
Hans Verkuil4e1a04d2008-09-07 08:00:49 -0300188 vdi.data = saa7111_i2c_init;
189 vdi.len = sizeof(saa7111_i2c_init);
190 return saa7111_init_decoder(client, &vdi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
192
193 case DECODER_DUMP:
194 {
195 int i;
196
Jean Delvare6eb5d9c2006-03-22 03:48:32 -0300197 for (i = 0; i < SAA7111_NR_REG; i += 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 int j;
199
Hans Verkuil4e1a04d2008-09-07 08:00:49 -0300200 v4l_info(client, "%03x", i);
Jean Delvare6eb5d9c2006-03-22 03:48:32 -0300201 for (j = 0; j < 16 && i + j < SAA7111_NR_REG; ++j) {
Hans Verkuil4e1a04d2008-09-07 08:00:49 -0300202 printk(KERN_CONT " %02x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 saa7111_read(client, i + j));
204 }
Hans Verkuil4e1a04d2008-09-07 08:00:49 -0300205 printk(KERN_CONT "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 break;
Hans Verkuil4e1a04d2008-09-07 08:00:49 -0300208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 case DECODER_GET_CAPABILITIES:
211 {
212 struct video_decoder_capability *cap = arg;
213
214 cap->flags = VIDEO_DECODER_PAL |
215 VIDEO_DECODER_NTSC |
216 VIDEO_DECODER_SECAM |
217 VIDEO_DECODER_AUTO |
218 VIDEO_DECODER_CCIR;
219 cap->inputs = 8;
220 cap->outputs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 break;
Hans Verkuil4e1a04d2008-09-07 08:00:49 -0300222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 case DECODER_GET_STATUS:
225 {
226 int *iarg = arg;
227 int status;
228 int res;
229
230 status = saa7111_read(client, 0x1f);
Hans Verkuil4e1a04d2008-09-07 08:00:49 -0300231 v4l_dbg(1, debug, client, "status: 0x%02x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 res = 0;
233 if ((status & (1 << 6)) == 0) {
234 res |= DECODER_STATUS_GOOD;
235 }
236 switch (decoder->norm) {
237 case VIDEO_MODE_NTSC:
238 res |= DECODER_STATUS_NTSC;
239 break;
240 case VIDEO_MODE_PAL:
241 res |= DECODER_STATUS_PAL;
242 break;
243 case VIDEO_MODE_SECAM:
244 res |= DECODER_STATUS_SECAM;
245 break;
246 default:
247 case VIDEO_MODE_AUTO:
248 if ((status & (1 << 5)) != 0) {
249 res |= DECODER_STATUS_NTSC;
250 } else {
251 res |= DECODER_STATUS_PAL;
252 }
253 break;
254 }
255 if ((status & (1 << 0)) != 0) {
256 res |= DECODER_STATUS_COLOR;
257 }
258 *iarg = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 break;
Hans Verkuil4e1a04d2008-09-07 08:00:49 -0300260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 case DECODER_SET_GPIO:
263 {
264 int *iarg = arg;
265 if (0 != *iarg) {
266 saa7111_write(client, 0x11,
267 (decoder->reg[0x11] | 0x80));
268 } else {
269 saa7111_write(client, 0x11,
270 (decoder->reg[0x11] & 0x7f));
271 }
272 break;
273 }
274
275 case DECODER_SET_VBI_BYPASS:
276 {
277 int *iarg = arg;
278 if (0 != *iarg) {
279 saa7111_write(client, 0x13,
280 (decoder->reg[0x13] & 0xf0) | 0x0a);
281 } else {
282 saa7111_write(client, 0x13,
283 (decoder->reg[0x13] & 0xf0));
284 }
285 break;
286 }
287
288 case DECODER_SET_NORM:
289 {
290 int *iarg = arg;
291
292 switch (*iarg) {
293
294 case VIDEO_MODE_NTSC:
295 saa7111_write(client, 0x08,
296 (decoder->reg[0x08] & 0x3f) | 0x40);
297 saa7111_write(client, 0x0e,
298 (decoder->reg[0x0e] & 0x8f));
299 break;
300
301 case VIDEO_MODE_PAL:
302 saa7111_write(client, 0x08,
303 (decoder->reg[0x08] & 0x3f) | 0x00);
304 saa7111_write(client, 0x0e,
305 (decoder->reg[0x0e] & 0x8f));
306 break;
307
308 case VIDEO_MODE_SECAM:
309 saa7111_write(client, 0x08,
310 (decoder->reg[0x08] & 0x3f) | 0x00);
311 saa7111_write(client, 0x0e,
312 (decoder->reg[0x0e] & 0x8f) | 0x50);
313 break;
314
315 case VIDEO_MODE_AUTO:
316 saa7111_write(client, 0x08,
317 (decoder->reg[0x08] & 0x3f) | 0x80);
318 saa7111_write(client, 0x0e,
319 (decoder->reg[0x0e] & 0x8f));
320 break;
321
322 default:
323 return -EINVAL;
324
325 }
326 decoder->norm = *iarg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 break;
Hans Verkuil4e1a04d2008-09-07 08:00:49 -0300328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 case DECODER_SET_INPUT:
331 {
332 int *iarg = arg;
333
334 if (*iarg < 0 || *iarg > 7) {
335 return -EINVAL;
336 }
337
338 if (decoder->input != *iarg) {
339 decoder->input = *iarg;
340 /* select mode */
341 saa7111_write(client, 0x02,
342 (decoder->
343 reg[0x02] & 0xf8) | decoder->input);
344 /* bypass chrominance trap for modes 4..7 */
345 saa7111_write(client, 0x09,
346 (decoder->
347 reg[0x09] & 0x7f) | ((decoder->
348 input >
349 3) ? 0x80 :
350 0));
351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 break;
Hans Verkuil4e1a04d2008-09-07 08:00:49 -0300353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 case DECODER_SET_OUTPUT:
356 {
357 int *iarg = arg;
358
359 /* not much choice of outputs */
360 if (*iarg != 0) {
361 return -EINVAL;
362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 break;
Hans Verkuil4e1a04d2008-09-07 08:00:49 -0300364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 case DECODER_ENABLE_OUTPUT:
367 {
368 int *iarg = arg;
369 int enable = (*iarg != 0);
370
371 if (decoder->enable != enable) {
372 decoder->enable = enable;
373
374 /* RJ: If output should be disabled (for
375 * playing videos), we also need a open PLL.
376 * The input is set to 0 (where no input
377 * source is connected), although this
378 * is not necessary.
379 *
380 * If output should be enabled, we have to
381 * reverse the above.
382 */
383
384 if (decoder->enable) {
385 saa7111_write(client, 0x02,
386 (decoder->
387 reg[0x02] & 0xf8) |
388 decoder->input);
389 saa7111_write(client, 0x08,
390 (decoder->reg[0x08] & 0xfb));
391 saa7111_write(client, 0x11,
392 (decoder->
393 reg[0x11] & 0xf3) | 0x0c);
394 } else {
395 saa7111_write(client, 0x02,
396 (decoder->reg[0x02] & 0xf8));
397 saa7111_write(client, 0x08,
398 (decoder->
399 reg[0x08] & 0xfb) | 0x04);
400 saa7111_write(client, 0x11,
401 (decoder->reg[0x11] & 0xf3));
402 }
403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 break;
Hans Verkuil4e1a04d2008-09-07 08:00:49 -0300405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 case DECODER_SET_PICTURE:
408 {
409 struct video_picture *pic = arg;
410
Russell King624fc7f2007-05-26 07:55:32 -0300411 /* We want 0 to 255 we get 0-65535 */
412 saa7111_write_if_changed(client, 0x0a, pic->brightness >> 8);
413 /* We want 0 to 127 we get 0-65535 */
414 saa7111_write(client, 0x0b, pic->contrast >> 9);
415 /* We want 0 to 127 we get 0-65535 */
416 saa7111_write(client, 0x0c, pic->colour >> 9);
417 /* We want -128 to 127 we get 0-65535 */
418 saa7111_write(client, 0x0d, (pic->hue - 32768) >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 break;
Hans Verkuil4e1a04d2008-09-07 08:00:49 -0300420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 default:
423 return -EINVAL;
424 }
425
426 return 0;
427}
428
429/* ----------------------------------------------------------------------- */
430
Hans Verkuil4e1a04d2008-09-07 08:00:49 -0300431static unsigned short normal_i2c[] = { 0x48 >> 1, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Hans Verkuil4e1a04d2008-09-07 08:00:49 -0300433I2C_CLIENT_INSMOD;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300434
Hans Verkuil4e1a04d2008-09-07 08:00:49 -0300435static int saa7111_probe(struct i2c_client *client,
436 const struct i2c_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
438 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 struct saa7111 *decoder;
440 struct video_decoder_init vdi;
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 /* Check if the adapter supports the needed features */
Hans Verkuil4e1a04d2008-09-07 08:00:49 -0300443 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
444 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Hans Verkuil4e1a04d2008-09-07 08:00:49 -0300446 v4l_info(client, "chip found @ 0x%x (%s)\n",
447 client->addr << 1, client->adapter->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Panagiotis Issaris74081872006-01-11 19:40:56 -0200449 decoder = kzalloc(sizeof(struct saa7111), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if (decoder == NULL) {
451 kfree(client);
452 return -ENOMEM;
453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 decoder->norm = VIDEO_MODE_NTSC;
455 decoder->input = 0;
456 decoder->enable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 i2c_set_clientdata(client, decoder);
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 vdi.data = saa7111_i2c_init;
460 vdi.len = sizeof(saa7111_i2c_init);
461 i = saa7111_init_decoder(client, &vdi);
462 if (i < 0) {
Hans Verkuil4e1a04d2008-09-07 08:00:49 -0300463 v4l_dbg(1, debug, client, "init status %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 } else {
Hans Verkuil4e1a04d2008-09-07 08:00:49 -0300465 v4l_dbg(1, debug, client, "revision %x\n",
466 saa7111_read(client, 0x00) >> 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return 0;
469}
470
Hans Verkuil4e1a04d2008-09-07 08:00:49 -0300471static int saa7111_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Hans Verkuil4e1a04d2008-09-07 08:00:49 -0300473 kfree(i2c_get_clientdata(client));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return 0;
475}
476
477/* ----------------------------------------------------------------------- */
478
Hans Verkuil4e1a04d2008-09-07 08:00:49 -0300479static const struct i2c_device_id saa7111_id[] = {
480 { "saa7111_old", 0 }, /* "saa7111" maps to the saa7115 driver */
481 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482};
Hans Verkuil4e1a04d2008-09-07 08:00:49 -0300483MODULE_DEVICE_TABLE(i2c, saa7111_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Hans Verkuil4e1a04d2008-09-07 08:00:49 -0300485static struct v4l2_i2c_driver_data v4l2_i2c_data = {
486 .name = "saa7111",
487 .driverid = I2C_DRIVERID_SAA7111A,
488 .command = saa7111_command,
489 .probe = saa7111_probe,
490 .remove = saa7111_remove,
491 .id_table = saa7111_id,
492};