Commit | Line | Data |
---|---|---|
d56410e0 | 1 | /* |
1da177e4 LT |
2 | * 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> | |
18f3fa1e | 31 | #include <linux/types.h> |
4e1a04d9 | 32 | #include <linux/ioctl.h> |
18f3fa1e | 33 | #include <asm/uaccess.h> |
4e1a04d9 HV |
34 | #include <linux/i2c.h> |
35 | #include <linux/i2c-id.h> | |
1da177e4 | 36 | #include <linux/videodev.h> |
18f3fa1e | 37 | #include <linux/video_decoder.h> |
4e1a04d9 HV |
38 | #include <media/v4l2-common.h> |
39 | #include <media/v4l2-i2c-drv-legacy.h> | |
1da177e4 LT |
40 | |
41 | MODULE_DESCRIPTION("Philips SAA7111 video decoder driver"); | |
42 | MODULE_AUTHOR("Dave Perks"); | |
43 | MODULE_LICENSE("GPL"); | |
44 | ||
ff699e6b | 45 | static int debug; |
1da177e4 LT |
46 | module_param(debug, int, 0644); |
47 | MODULE_PARM_DESC(debug, "Debug level (0-1)"); | |
48 | ||
1da177e4 LT |
49 | /* ----------------------------------------------------------------------- */ |
50 | ||
6eb5d9ca JD |
51 | #define SAA7111_NR_REG 0x18 |
52 | ||
1da177e4 | 53 | struct saa7111 { |
6eb5d9ca | 54 | unsigned char reg[SAA7111_NR_REG]; |
1da177e4 LT |
55 | |
56 | int norm; | |
57 | int input; | |
58 | int enable; | |
1da177e4 LT |
59 | }; |
60 | ||
1da177e4 LT |
61 | /* ----------------------------------------------------------------------- */ |
62 | ||
4e1a04d9 | 63 | static inline int saa7111_write(struct i2c_client *client, u8 reg, u8 value) |
1da177e4 LT |
64 | { |
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 | ||
4e1a04d9 | 71 | static inline void saa7111_write_if_changed(struct i2c_client *client, u8 reg, u8 value) |
624fc7f5 RK |
72 | { |
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 | ||
4e1a04d9 | 81 | static int saa7111_write_block(struct i2c_client *client, const u8 *data, unsigned int len) |
1da177e4 LT |
82 | { |
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); | |
1da177e4 | 91 | u8 block_data[32]; |
9aa45e34 | 92 | int block_len; |
1da177e4 | 93 | |
1da177e4 | 94 | while (len >= 2) { |
9aa45e34 JD |
95 | block_len = 0; |
96 | block_data[block_len++] = reg = data[0]; | |
1da177e4 | 97 | do { |
9aa45e34 | 98 | block_data[block_len++] = |
1da177e4 LT |
99 | decoder->reg[reg++] = data[1]; |
100 | len -= 2; | |
101 | data += 2; | |
4e1a04d9 HV |
102 | } while (len >= 2 && data[0] == reg && block_len < 32); |
103 | ret = i2c_master_send(client, block_data, block_len); | |
104 | if (ret < 0) | |
1da177e4 LT |
105 | break; |
106 | } | |
107 | } else { | |
108 | /* do some slow I2C emulation kind of thing */ | |
109 | while (len >= 2) { | |
110 | reg = *data++; | |
4e1a04d9 HV |
111 | ret = saa7111_write(client, reg, *data++); |
112 | if (ret < 0) | |
1da177e4 LT |
113 | break; |
114 | len -= 2; | |
115 | } | |
116 | } | |
117 | ||
118 | return ret; | |
119 | } | |
120 | ||
4e1a04d9 HV |
121 | static int saa7111_init_decoder(struct i2c_client *client, |
122 | struct video_decoder_init *init) | |
1da177e4 LT |
123 | { |
124 | return saa7111_write_block(client, init->data, init->len); | |
125 | } | |
126 | ||
4e1a04d9 | 127 | static inline int saa7111_read(struct i2c_client *client, u8 reg) |
1da177e4 LT |
128 | { |
129 | return i2c_smbus_read_byte_data(client, reg); | |
130 | } | |
131 | ||
132 | /* ----------------------------------------------------------------------- */ | |
133 | ||
134 | static 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 | ||
4e1a04d9 | 174 | static int saa7111_command(struct i2c_client *client, unsigned cmd, void *arg) |
1da177e4 LT |
175 | { |
176 | struct saa7111 *decoder = i2c_get_clientdata(client); | |
177 | ||
178 | switch (cmd) { | |
1da177e4 | 179 | case 0: |
58a0b84c | 180 | break; |
1da177e4 LT |
181 | case DECODER_INIT: |
182 | { | |
183 | struct video_decoder_init *init = arg; | |
4e1a04d9 HV |
184 | struct video_decoder_init vdi; |
185 | ||
1da177e4 LT |
186 | if (NULL != init) |
187 | return saa7111_init_decoder(client, init); | |
4e1a04d9 HV |
188 | vdi.data = saa7111_i2c_init; |
189 | vdi.len = sizeof(saa7111_i2c_init); | |
190 | return saa7111_init_decoder(client, &vdi); | |
1da177e4 LT |
191 | } |
192 | ||
193 | case DECODER_DUMP: | |
194 | { | |
195 | int i; | |
196 | ||
6eb5d9ca | 197 | for (i = 0; i < SAA7111_NR_REG; i += 16) { |
1da177e4 LT |
198 | int j; |
199 | ||
4e1a04d9 | 200 | v4l_info(client, "%03x", i); |
6eb5d9ca | 201 | for (j = 0; j < 16 && i + j < SAA7111_NR_REG; ++j) { |
4e1a04d9 | 202 | printk(KERN_CONT " %02x", |
1da177e4 LT |
203 | saa7111_read(client, i + j)); |
204 | } | |
4e1a04d9 | 205 | printk(KERN_CONT "\n"); |
1da177e4 | 206 | } |
1da177e4 | 207 | break; |
4e1a04d9 | 208 | } |
1da177e4 LT |
209 | |
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; | |
1da177e4 | 221 | break; |
4e1a04d9 | 222 | } |
1da177e4 LT |
223 | |
224 | case DECODER_GET_STATUS: | |
225 | { | |
226 | int *iarg = arg; | |
227 | int status; | |
228 | int res; | |
229 | ||
230 | status = saa7111_read(client, 0x1f); | |
4e1a04d9 | 231 | v4l_dbg(1, debug, client, "status: 0x%02x\n", status); |
1da177e4 LT |
232 | 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; | |
1da177e4 | 259 | break; |
4e1a04d9 | 260 | } |
1da177e4 LT |
261 | |
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; | |
1da177e4 | 327 | break; |
4e1a04d9 | 328 | } |
1da177e4 LT |
329 | |
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 | } | |
1da177e4 | 352 | break; |
4e1a04d9 | 353 | } |
1da177e4 LT |
354 | |
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 | } | |
1da177e4 | 363 | break; |
4e1a04d9 | 364 | } |
1da177e4 LT |
365 | |
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 | } | |
1da177e4 | 404 | break; |
4e1a04d9 | 405 | } |
1da177e4 LT |
406 | |
407 | case DECODER_SET_PICTURE: | |
408 | { | |
409 | struct video_picture *pic = arg; | |
410 | ||
624fc7f5 RK |
411 | /* 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); | |
1da177e4 | 419 | break; |
4e1a04d9 | 420 | } |
1da177e4 LT |
421 | |
422 | default: | |
423 | return -EINVAL; | |
424 | } | |
425 | ||
426 | return 0; | |
427 | } | |
428 | ||
429 | /* ----------------------------------------------------------------------- */ | |
430 | ||
4e1a04d9 | 431 | static unsigned short normal_i2c[] = { 0x48 >> 1, I2C_CLIENT_END }; |
1da177e4 | 432 | |
4e1a04d9 | 433 | I2C_CLIENT_INSMOD; |
1da177e4 | 434 | |
4e1a04d9 HV |
435 | static int saa7111_probe(struct i2c_client *client, |
436 | const struct i2c_device_id *id) | |
1da177e4 LT |
437 | { |
438 | int i; | |
1da177e4 LT |
439 | struct saa7111 *decoder; |
440 | struct video_decoder_init vdi; | |
441 | ||
1da177e4 | 442 | /* Check if the adapter supports the needed features */ |
4e1a04d9 HV |
443 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
444 | return -ENODEV; | |
1da177e4 | 445 | |
4e1a04d9 HV |
446 | v4l_info(client, "chip found @ 0x%x (%s)\n", |
447 | client->addr << 1, client->adapter->name); | |
1da177e4 | 448 | |
7408187d | 449 | decoder = kzalloc(sizeof(struct saa7111), GFP_KERNEL); |
1da177e4 LT |
450 | if (decoder == NULL) { |
451 | kfree(client); | |
452 | return -ENOMEM; | |
453 | } | |
1da177e4 LT |
454 | decoder->norm = VIDEO_MODE_NTSC; |
455 | decoder->input = 0; | |
456 | decoder->enable = 1; | |
1da177e4 LT |
457 | i2c_set_clientdata(client, decoder); |
458 | ||
1da177e4 LT |
459 | vdi.data = saa7111_i2c_init; |
460 | vdi.len = sizeof(saa7111_i2c_init); | |
461 | i = saa7111_init_decoder(client, &vdi); | |
462 | if (i < 0) { | |
4e1a04d9 | 463 | v4l_dbg(1, debug, client, "init status %d\n", i); |
1da177e4 | 464 | } else { |
4e1a04d9 HV |
465 | v4l_dbg(1, debug, client, "revision %x\n", |
466 | saa7111_read(client, 0x00) >> 4); | |
1da177e4 | 467 | } |
1da177e4 LT |
468 | return 0; |
469 | } | |
470 | ||
4e1a04d9 | 471 | static int saa7111_remove(struct i2c_client *client) |
1da177e4 | 472 | { |
4e1a04d9 | 473 | kfree(i2c_get_clientdata(client)); |
1da177e4 LT |
474 | return 0; |
475 | } | |
476 | ||
477 | /* ----------------------------------------------------------------------- */ | |
478 | ||
4e1a04d9 HV |
479 | static const struct i2c_device_id saa7111_id[] = { |
480 | { "saa7111_old", 0 }, /* "saa7111" maps to the saa7115 driver */ | |
481 | { } | |
482 | }; | |
483 | MODULE_DEVICE_TABLE(i2c, saa7111_id); | |
1da177e4 | 484 | |
4e1a04d9 HV |
485 | static struct v4l2_i2c_driver_data v4l2_i2c_data = { |
486 | .name = "saa7111", | |
487 | .driverid = I2C_DRIVERID_SAA7111A, | |
1da177e4 | 488 | .command = saa7111_command, |
4e1a04d9 HV |
489 | .probe = saa7111_probe, |
490 | .remove = saa7111_remove, | |
491 | .id_table = saa7111_id, | |
1da177e4 | 492 | }; |