blob: 708fae51e8ee61555c18ab4f1bae967a9ec8470a [file] [log] [blame]
Dwaine Garden340622c2005-11-08 21:37:34 -08001/*
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -08002 * saa711x - Philips SAA711x video decoder driver version 0.0.1
Dwaine Garden340622c2005-11-08 21:37:34 -08003 *
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -08004 * To do: Now, it handles only saa7113/7114. Should be improved to
5 * handle all Philips saa711x devices.
Dwaine Garden340622c2005-11-08 21:37:34 -08006 *
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -08007 * Based on saa7113 driver from Dave Perks <dperks@ibm.net>
Dwaine Garden340622c2005-11-08 21:37:34 -08008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/delay.h>
27#include <linux/errno.h>
28#include <linux/fs.h>
29#include <linux/kernel.h>
30#include <linux/major.h>
31#include <linux/slab.h>
32#include <linux/mm.h>
33#include <linux/pci.h>
34#include <linux/signal.h>
35#include <asm/io.h>
36#include <asm/pgtable.h>
37#include <asm/page.h>
38#include <linux/sched.h>
Dwaine Garden340622c2005-11-08 21:37:34 -080039#include <linux/types.h>
40#include <asm/uaccess.h>
41#include <linux/videodev.h>
42
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -080043MODULE_DESCRIPTION("Philips SAA711x video decoder driver");
Dwaine Garden340622c2005-11-08 21:37:34 -080044MODULE_AUTHOR("Dave Perks, Jose Ignacio Gijon, Joerg Heckenbach, Mark McClelland, Dwaine Garden");
45MODULE_LICENSE("GPL");
46
47#include <linux/i2c.h>
Dwaine Garden340622c2005-11-08 21:37:34 -080048
49#define I2C_NAME(s) (s)->name
50
51#include <linux/video_decoder.h>
52
53static int debug = 0;
Eric Sesterhenn / snakebyte9b565eb2006-01-13 14:10:23 -020054module_param(debug, int, 0644);
Dwaine Garden340622c2005-11-08 21:37:34 -080055MODULE_PARM_DESC(debug, " Set the default Debug level. Default: 0 (Off) - (0-1)");
56
57
58#define dprintk(num, format, args...) \
59 do { \
60 if (debug >= num) \
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -080061 printk(format, ##args); \
Dwaine Garden340622c2005-11-08 21:37:34 -080062 } while (0)
63
64/* ----------------------------------------------------------------------- */
65
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -080066struct saa711x {
Dwaine Garden340622c2005-11-08 21:37:34 -080067 unsigned char reg[32];
68
69 int norm;
70 int input;
71 int enable;
72 int bright;
73 int contrast;
74 int hue;
75 int sat;
76};
77
78#define I2C_SAA7113 0x4A
Markus Rechberger791b4032005-11-08 21:38:08 -080079#define I2C_SAA7114 0x42
Dwaine Garden340622c2005-11-08 21:37:34 -080080
81/* ----------------------------------------------------------------------- */
82
83static inline int
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -080084saa711x_write (struct i2c_client *client,
Dwaine Garden340622c2005-11-08 21:37:34 -080085 u8 reg,
86 u8 value)
87{
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -080088 struct saa711x *decoder = i2c_get_clientdata(client);
Dwaine Garden340622c2005-11-08 21:37:34 -080089
90 decoder->reg[reg] = value;
91 return i2c_smbus_write_byte_data(client, reg, value);
92}
93
94static int
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -080095saa711x_write_block (struct i2c_client *client,
Dwaine Garden340622c2005-11-08 21:37:34 -080096 const u8 *data,
97 unsigned int len)
98{
99 int ret = -1;
100 u8 reg;
101
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800102 /* the saa711x has an autoincrement function, use it if
Dwaine Garden340622c2005-11-08 21:37:34 -0800103 * the adapter understands raw I2C */
104 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
105 /* do raw I2C, not smbus compatible */
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800106 struct saa711x *decoder = i2c_get_clientdata(client);
Dwaine Garden340622c2005-11-08 21:37:34 -0800107 struct i2c_msg msg;
108 u8 block_data[32];
109
110 msg.addr = client->addr;
111 msg.flags = 0;
112 while (len >= 2) {
113 msg.buf = (char *) block_data;
114 msg.len = 0;
115 block_data[msg.len++] = reg = data[0];
116 do {
117 block_data[msg.len++] =
118 decoder->reg[reg++] = data[1];
119 len -= 2;
120 data += 2;
121 } while (len >= 2 && data[0] == reg &&
122 msg.len < 32);
123 if ((ret = i2c_transfer(client->adapter,
124 &msg, 1)) < 0)
125 break;
126 }
127 } else {
128 /* do some slow I2C emulation kind of thing */
129 while (len >= 2) {
130 reg = *data++;
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800131 if ((ret = saa711x_write(client, reg,
Dwaine Garden340622c2005-11-08 21:37:34 -0800132 *data++)) < 0)
133 break;
134 len -= 2;
135 }
136 }
137
138 return ret;
139}
140
141static int
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800142saa711x_init_decoder (struct i2c_client *client,
Dwaine Garden340622c2005-11-08 21:37:34 -0800143 struct video_decoder_init *init)
144{
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800145 return saa711x_write_block(client, init->data, init->len);
Dwaine Garden340622c2005-11-08 21:37:34 -0800146}
147
148static inline int
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800149saa711x_read (struct i2c_client *client,
Dwaine Garden340622c2005-11-08 21:37:34 -0800150 u8 reg)
151{
152 return i2c_smbus_read_byte_data(client, reg);
153}
154
155/* ----------------------------------------------------------------------- */
156
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800157static const unsigned char saa711x_i2c_init[] = {
158 0x00, 0x00, /* PH711x_CHIP_VERSION 00 - ID byte */
159 0x01, 0x08, /* PH711x_INCREMENT_DELAY - (1) (1) (1) (1) IDEL3 IDEL2 IDELL1 IDEL0 */
160 0x02, 0xc0, /* PH711x_ANALOG_INPUT_CONTR_1 - FUSE1 FUSE0 GUDL1 GUDL0 MODE3 MODE2 MODE1 MODE0 */
161 0x03, 0x23, /* PH711x_ANALOG_INPUT_CONTR_2 - (1) HLNRS VBSL WPOFF HOLDG GAFIX GAI28 GAI18 */
162 0x04, 0x00, /* PH711x_ANALOG_INPUT_CONTR_3 - GAI17 GAI16 GAI15 GAI14 GAI13 GAI12 GAI11 GAI10 */
163 0x05, 0x00, /* PH711x_ANALOG_INPUT_CONTR_4 - GAI27 GAI26 GAI25 GAI24 GAI23 GAI22 GAI21 GAI20 */
164 0x06, 0xeb, /* PH711x_HORIZONTAL_SYNC_START - HSB7 HSB6 HSB5 HSB4 HSB3 HSB2 HSB1 HSB0 */
165 0x07, 0xe0, /* PH711x_HORIZONTAL_SYNC_STOP - HSS7 HSS6 HSS5 HSS4 HSS3 HSS2 HSS1 HSS0 */
166 0x08, 0x88, /* PH711x_SYNC_CONTROL - AUFD FSEL FOET HTC1 HTC0 HPLL VNOI1 VNOI0 */
167 0x09, 0x00, /* PH711x_LUMINANCE_CONTROL - BYPS PREF BPSS1 BPSS0 VBLB UPTCV APER1 APER0 */
168 0x0a, 0x80, /* PH711x_LUMINANCE_BRIGHTNESS - BRIG7 BRIG6 BRIG5 BRIG4 BRIG3 BRIG2 BRIG1 BRIG0 */
169 0x0b, 0x47, /* PH711x_LUMINANCE_CONTRAST - CONT7 CONT6 CONT5 CONT4 CONT3 CONT2 CONT1 CONT0 */
170 0x0c, 0x40, /* PH711x_CHROMA_SATURATION - SATN7 SATN6 SATN5 SATN4 SATN3 SATN2 SATN1 SATN0 */
171 0x0d, 0x00, /* PH711x_CHROMA_HUE_CONTROL - HUEC7 HUEC6 HUEC5 HUEC4 HUEC3 HUEC2 HUEC1 HUEC0 */
172 0x0e, 0x01, /* PH711x_CHROMA_CONTROL - CDTO CSTD2 CSTD1 CSTD0 DCCF FCTC CHBW1 CHBW0 */
173 0x0f, 0xaa, /* PH711x_CHROMA_GAIN_CONTROL - ACGC CGAIN6 CGAIN5 CGAIN4 CGAIN3 CGAIN2 CGAIN1 CGAIN0 */
174 0x10, 0x00, /* PH711x_FORMAT_DELAY_CONTROL - OFTS1 OFTS0 HDEL1 HDEL0 VRLN YDEL2 YDEL1 YDEL0 */
175 0x11, 0x1C, /* PH711x_OUTPUT_CONTROL_1 - GPSW1 CM99 GPSW0 HLSEL OEYC OERT VIPB COLO */
176 0x12, 0x01, /* PH711x_OUTPUT_CONTROL_2 - RTSE13 RTSE12 RTSE11 RTSE10 RTSE03 RTSE02 RTSE01 RTSE00 */
177 0x13, 0x00, /* PH711x_OUTPUT_CONTROL_3 - ADLSB (1) (1) OLDSB FIDP (1) AOSL1 AOSL0 */
Dwaine Garden340622c2005-11-08 21:37:34 -0800178 0x14, 0x00, /* RESERVED 14 - (1) (1) (1) (1) (1) (1) (1) (1) */
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800179 0x15, 0x00, /* PH711x_V_GATE1_START - VSTA7 VSTA6 VSTA5 VSTA4 VSTA3 VSTA2 VSTA1 VSTA0 */
180 0x16, 0x00, /* PH711x_V_GATE1_STOP - VSTO7 VSTO6 VSTO5 VSTO4 VSTO3 VSTO2 VSTO1 VSTO0 */
181 0x17, 0x00, /* PH711x_V_GATE1_MSB - (1) (1) (1) (1) (1) (1) VSTO8 VSTA8 */
Dwaine Garden340622c2005-11-08 21:37:34 -0800182};
183
184static int
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800185saa711x_command (struct i2c_client *client,
Dwaine Garden340622c2005-11-08 21:37:34 -0800186 unsigned int cmd,
187 void *arg)
188{
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800189 struct saa711x *decoder = i2c_get_clientdata(client);
Dwaine Garden340622c2005-11-08 21:37:34 -0800190
191 switch (cmd) {
192
193 case 0:
194 case DECODER_INIT:
195 {
196 struct video_decoder_init *init = arg;
197 if (NULL != init)
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800198 return saa711x_init_decoder(client, init);
Dwaine Garden340622c2005-11-08 21:37:34 -0800199 else {
200 struct video_decoder_init vdi;
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800201 vdi.data = saa711x_i2c_init;
202 vdi.len = sizeof(saa711x_i2c_init);
203 return saa711x_init_decoder(client, &vdi);
Dwaine Garden340622c2005-11-08 21:37:34 -0800204 }
205 }
206
207 case DECODER_DUMP:
208 {
209 int i;
210
211 for (i = 0; i < 32; i += 16) {
212 int j;
213
214 printk(KERN_DEBUG "%s: %03x", I2C_NAME(client), i);
215 for (j = 0; j < 16; ++j) {
216 printk(" %02x",
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800217 saa711x_read(client, i + j));
Dwaine Garden340622c2005-11-08 21:37:34 -0800218 }
219 printk("\n");
220 }
221 }
222 break;
223
224 case DECODER_GET_CAPABILITIES:
225 {
226 struct video_decoder_capability *cap = arg;
227
228 cap->flags = VIDEO_DECODER_PAL |
229 VIDEO_DECODER_NTSC |
230 VIDEO_DECODER_SECAM |
231 VIDEO_DECODER_AUTO |
232 VIDEO_DECODER_CCIR;
233 cap->inputs = 8;
234 cap->outputs = 1;
235 }
236 break;
237
238 case DECODER_GET_STATUS:
239 {
240 int *iarg = arg;
241 int status;
242 int res;
243
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800244 status = saa711x_read(client, 0x1f);
Dwaine Garden340622c2005-11-08 21:37:34 -0800245 dprintk(1, KERN_DEBUG "%s status: 0x%02x\n", I2C_NAME(client),
246 status);
247 res = 0;
248 if ((status & (1 << 6)) == 0) {
249 res |= DECODER_STATUS_GOOD;
250 }
251 switch (decoder->norm) {
252 case VIDEO_MODE_NTSC:
253 res |= DECODER_STATUS_NTSC;
254 break;
255 case VIDEO_MODE_PAL:
256 res |= DECODER_STATUS_PAL;
257 break;
258 case VIDEO_MODE_SECAM:
259 res |= DECODER_STATUS_SECAM;
260 break;
261 default:
262 case VIDEO_MODE_AUTO:
263 if ((status & (1 << 5)) != 0) {
264 res |= DECODER_STATUS_NTSC;
265 } else {
266 res |= DECODER_STATUS_PAL;
267 }
268 break;
269 }
270 if ((status & (1 << 0)) != 0) {
271 res |= DECODER_STATUS_COLOR;
272 }
273 *iarg = res;
274 }
275 break;
276
277 case DECODER_SET_GPIO:
278 {
279 int *iarg = arg;
280 if (0 != *iarg) {
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800281 saa711x_write(client, 0x11,
Dwaine Garden340622c2005-11-08 21:37:34 -0800282 (decoder->reg[0x11] | 0x80));
283 } else {
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800284 saa711x_write(client, 0x11,
Dwaine Garden340622c2005-11-08 21:37:34 -0800285 (decoder->reg[0x11] & 0x7f));
286 }
287 break;
288 }
289
290 case DECODER_SET_VBI_BYPASS:
291 {
292 int *iarg = arg;
293 if (0 != *iarg) {
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800294 saa711x_write(client, 0x13,
Dwaine Garden340622c2005-11-08 21:37:34 -0800295 (decoder->reg[0x13] & 0xf0) | 0x0a);
296 } else {
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800297 saa711x_write(client, 0x13,
Dwaine Garden340622c2005-11-08 21:37:34 -0800298 (decoder->reg[0x13] & 0xf0));
299 }
300 break;
301 }
302
303 case DECODER_SET_NORM:
304 {
305 int *iarg = arg;
306
307 switch (*iarg) {
308
309 case VIDEO_MODE_NTSC:
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800310 saa711x_write(client, 0x08,
Dwaine Garden340622c2005-11-08 21:37:34 -0800311 (decoder->reg[0x08] & 0x3f) | 0x40);
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800312 saa711x_write(client, 0x0e,
Dwaine Garden340622c2005-11-08 21:37:34 -0800313 (decoder->reg[0x0e] & 0x8f));
314 break;
315
316 case VIDEO_MODE_PAL:
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800317 saa711x_write(client, 0x08,
Dwaine Garden340622c2005-11-08 21:37:34 -0800318 (decoder->reg[0x08] & 0x3f) | 0x00);
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800319 saa711x_write(client, 0x0e,
Dwaine Garden340622c2005-11-08 21:37:34 -0800320 (decoder->reg[0x0e] & 0x8f));
321 break;
322
323 case VIDEO_MODE_SECAM:
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800324 saa711x_write(client, 0x08,
Dwaine Garden1bcd2a32005-12-01 00:51:37 -0800325 (decoder->reg[0x08] & 0x3f) | 0x00);
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800326 saa711x_write(client, 0x0e,
Dwaine Garden340622c2005-11-08 21:37:34 -0800327 (decoder->reg[0x0e] & 0x8f) | 0x50);
328 break;
329
330 case VIDEO_MODE_AUTO:
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800331 saa711x_write(client, 0x08,
Dwaine Garden340622c2005-11-08 21:37:34 -0800332 (decoder->reg[0x08] & 0x3f) | 0x80);
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800333 saa711x_write(client, 0x0e,
Dwaine Garden340622c2005-11-08 21:37:34 -0800334 (decoder->reg[0x0e] & 0x8f));
335 break;
336
337 default:
338 return -EINVAL;
339
340 }
341 decoder->norm = *iarg;
342 }
343 break;
344
345 case DECODER_SET_INPUT:
346 {
347 int *iarg = arg;
348 if (*iarg < 0 || *iarg > 9) {
349 return -EINVAL;
350 }
351 if (decoder->input != *iarg) {
352 decoder->input = *iarg;
353 /* select mode */
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800354 saa711x_write(client, 0x02,
Dwaine Garden340622c2005-11-08 21:37:34 -0800355 (decoder->reg[0x02] & 0xf0) | decoder->input);
356 /* bypass chrominance trap for modes 4..7 */
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800357 saa711x_write(client, 0x09,
Dwaine Garden340622c2005-11-08 21:37:34 -0800358 (decoder->reg[0x09] & 0x7f) | ((decoder->input > 3) ? 0x80 : 0));
359 }
360 }
361 break;
362
363 case DECODER_SET_OUTPUT:
364 {
365 int *iarg = arg;
366
367 /* not much choice of outputs */
368 if (*iarg != 0) {
369 return -EINVAL;
370 }
371 }
372 break;
373
374 case DECODER_ENABLE_OUTPUT:
375 {
376 int *iarg = arg;
377 int enable = (*iarg != 0);
378
379 if (decoder->enable != enable) {
380 decoder->enable = enable;
381
382 /* RJ: If output should be disabled (for
383 * playing videos), we also need a open PLL.
384 * The input is set to 0 (where no input
385 * source is connected), although this
386 * is not necessary.
387 *
388 * If output should be enabled, we have to
389 * reverse the above.
390 */
391
392 if (decoder->enable) {
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800393 saa711x_write(client, 0x02,
Dwaine Garden340622c2005-11-08 21:37:34 -0800394 (decoder->
395 reg[0x02] & 0xf8) |
396 decoder->input);
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800397 saa711x_write(client, 0x08,
Dwaine Garden340622c2005-11-08 21:37:34 -0800398 (decoder->reg[0x08] & 0xfb));
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800399 saa711x_write(client, 0x11,
Dwaine Garden340622c2005-11-08 21:37:34 -0800400 (decoder->
401 reg[0x11] & 0xf3) | 0x0c);
402 } else {
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800403 saa711x_write(client, 0x02,
Dwaine Garden340622c2005-11-08 21:37:34 -0800404 (decoder->reg[0x02] & 0xf8));
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800405 saa711x_write(client, 0x08,
Dwaine Garden340622c2005-11-08 21:37:34 -0800406 (decoder->
407 reg[0x08] & 0xfb) | 0x04);
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800408 saa711x_write(client, 0x11,
Dwaine Garden340622c2005-11-08 21:37:34 -0800409 (decoder->reg[0x11] & 0xf3));
410 }
411 }
412 }
413 break;
414
415 case DECODER_SET_PICTURE:
416 {
417 struct video_picture *pic = arg;
418
419 if (decoder->bright != pic->brightness) {
420 /* We want 0 to 255 we get 0-65535 */
421 decoder->bright = pic->brightness;
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800422 saa711x_write(client, 0x0a, decoder->bright >> 8);
Dwaine Garden340622c2005-11-08 21:37:34 -0800423 }
424 if (decoder->contrast != pic->contrast) {
425 /* We want 0 to 127 we get 0-65535 */
426 decoder->contrast = pic->contrast;
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800427 saa711x_write(client, 0x0b,
Dwaine Garden340622c2005-11-08 21:37:34 -0800428 decoder->contrast >> 9);
429 }
430 if (decoder->sat != pic->colour) {
431 /* We want 0 to 127 we get 0-65535 */
432 decoder->sat = pic->colour;
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800433 saa711x_write(client, 0x0c, decoder->sat >> 9);
Dwaine Garden340622c2005-11-08 21:37:34 -0800434 }
435 if (decoder->hue != pic->hue) {
436 /* We want -128 to 127 we get 0-65535 */
437 decoder->hue = pic->hue;
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800438 saa711x_write(client, 0x0d,
Dwaine Garden340622c2005-11-08 21:37:34 -0800439 (decoder->hue - 32768) >> 8);
440 }
441 }
442 break;
443
444 default:
445 return -EINVAL;
446 }
447
448 return 0;
449}
450
451/* ----------------------------------------------------------------------- */
452
453/*
454 * Generic i2c probe
455 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
456 */
457
458/* standard i2c insmod options */
459static unsigned short normal_i2c[] = {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800460 I2C_SAA7113>>1, /* saa7113 */
Markus Rechberger791b4032005-11-08 21:38:08 -0800461 I2C_SAA7114>>1, /* saa7114 */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800462 I2C_CLIENT_END
Dwaine Garden340622c2005-11-08 21:37:34 -0800463};
464
465I2C_CLIENT_INSMOD;
466
467
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800468static struct i2c_driver i2c_driver_saa711x;
Dwaine Garden340622c2005-11-08 21:37:34 -0800469
470static int
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800471saa711x_detect_client (struct i2c_adapter *adapter,
Dwaine Garden340622c2005-11-08 21:37:34 -0800472 int address,
473 int kind)
474{
475 int i;
476 struct i2c_client *client;
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800477 struct saa711x *decoder;
Dwaine Garden340622c2005-11-08 21:37:34 -0800478 struct video_decoder_init vdi;
479
480 dprintk(1,
481 KERN_INFO
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800482 "saa711x.c: detecting saa711x client on address 0x%x\n",
Dwaine Garden340622c2005-11-08 21:37:34 -0800483 address << 1);
484
485 /* Check if the adapter supports the needed features */
486 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
487 return 0;
488
Panagiotis Issaris74081872006-01-11 19:40:56 -0200489 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
Dwaine Garden340622c2005-11-08 21:37:34 -0800490 if (client == 0)
491 return -ENOMEM;
Dwaine Garden340622c2005-11-08 21:37:34 -0800492 client->addr = address;
493 client->adapter = adapter;
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800494 client->driver = &i2c_driver_saa711x;
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800495 strlcpy(I2C_NAME(client), "saa711x", sizeof(I2C_NAME(client)));
Panagiotis Issaris74081872006-01-11 19:40:56 -0200496 decoder = kzalloc(sizeof(struct saa711x), GFP_KERNEL);
Dwaine Garden340622c2005-11-08 21:37:34 -0800497 if (decoder == NULL) {
498 kfree(client);
499 return -ENOMEM;
500 }
Dwaine Garden340622c2005-11-08 21:37:34 -0800501 decoder->norm = VIDEO_MODE_NTSC;
502 decoder->input = 0;
503 decoder->enable = 1;
504 decoder->bright = 32768;
505 decoder->contrast = 32768;
506 decoder->hue = 32768;
507 decoder->sat = 32768;
508 i2c_set_clientdata(client, decoder);
509
510 i = i2c_attach_client(client);
511 if (i) {
512 kfree(client);
513 kfree(decoder);
514 return i;
515 }
516
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800517 vdi.data = saa711x_i2c_init;
518 vdi.len = sizeof(saa711x_i2c_init);
519 i = saa711x_init_decoder(client, &vdi);
Dwaine Garden340622c2005-11-08 21:37:34 -0800520 if (i < 0) {
521 dprintk(1, KERN_ERR "%s_attach error: init status %d\n",
522 I2C_NAME(client), i);
523 } else {
524 dprintk(1,
525 KERN_INFO
526 "%s_attach: chip version %x at address 0x%x\n",
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800527 I2C_NAME(client), saa711x_read(client, 0x00) >> 4,
Dwaine Garden340622c2005-11-08 21:37:34 -0800528 client->addr << 1);
529 }
530
531 return 0;
532}
533
534static int
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800535saa711x_attach_adapter (struct i2c_adapter *adapter)
Dwaine Garden340622c2005-11-08 21:37:34 -0800536{
537 dprintk(1,
538 KERN_INFO
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800539 "saa711x.c: starting probe for adapter %s (0x%x)\n",
Dwaine Garden340622c2005-11-08 21:37:34 -0800540 I2C_NAME(adapter), adapter->id);
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800541 return i2c_probe(adapter, &addr_data, &saa711x_detect_client);
Dwaine Garden340622c2005-11-08 21:37:34 -0800542}
543
544static int
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800545saa711x_detach_client (struct i2c_client *client)
Dwaine Garden340622c2005-11-08 21:37:34 -0800546{
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800547 struct saa711x *decoder = i2c_get_clientdata(client);
Dwaine Garden340622c2005-11-08 21:37:34 -0800548 int err;
549
550 err = i2c_detach_client(client);
551 if (err) {
552 return err;
553 }
554
555 kfree(decoder);
556 kfree(client);
557
558 return 0;
559}
560
561/* ----------------------------------------------------------------------- */
562
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800563static struct i2c_driver i2c_driver_saa711x = {
Laurent Riffard604f28e2005-11-26 20:43:39 +0100564 .driver = {
Laurent Riffard604f28e2005-11-26 20:43:39 +0100565 .name = "saa711x",
566 },
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800567 .id = I2C_DRIVERID_SAA711X,
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800568 .attach_adapter = saa711x_attach_adapter,
569 .detach_client = saa711x_detach_client,
570 .command = saa711x_command,
Dwaine Garden340622c2005-11-08 21:37:34 -0800571};
572
573static int __init
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800574saa711x_init (void)
Dwaine Garden340622c2005-11-08 21:37:34 -0800575{
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800576 return i2c_add_driver(&i2c_driver_saa711x);
Dwaine Garden340622c2005-11-08 21:37:34 -0800577}
578
579static void __exit
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800580saa711x_exit (void)
Dwaine Garden340622c2005-11-08 21:37:34 -0800581{
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800582 i2c_del_driver(&i2c_driver_saa711x);
Dwaine Garden340622c2005-11-08 21:37:34 -0800583}
584
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800585module_init(saa711x_init);
586module_exit(saa711x_exit);