blob: 269d7114a93abc8ebf4995a5cbed7ef58af5ccc1 [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>
Dwaine Garden340622c2005-11-08 21:37:34 -080038#include <linux/types.h>
39#include <asm/uaccess.h>
40#include <linux/videodev.h>
41
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -080042MODULE_DESCRIPTION("Philips SAA711x video decoder driver");
Dwaine Garden340622c2005-11-08 21:37:34 -080043MODULE_AUTHOR("Dave Perks, Jose Ignacio Gijon, Joerg Heckenbach, Mark McClelland, Dwaine Garden");
44MODULE_LICENSE("GPL");
45
46#include <linux/i2c.h>
Dwaine Garden340622c2005-11-08 21:37:34 -080047
48#define I2C_NAME(s) (s)->name
49
50#include <linux/video_decoder.h>
51
52static int debug = 0;
Eric Sesterhenn / snakebyte9b565eb2006-01-13 14:10:23 -020053module_param(debug, int, 0644);
Dwaine Garden340622c2005-11-08 21:37:34 -080054MODULE_PARM_DESC(debug, " Set the default Debug level. Default: 0 (Off) - (0-1)");
55
56
57#define dprintk(num, format, args...) \
58 do { \
59 if (debug >= num) \
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -080060 printk(format, ##args); \
Dwaine Garden340622c2005-11-08 21:37:34 -080061 } while (0)
62
63/* ----------------------------------------------------------------------- */
64
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -080065struct saa711x {
Dwaine Garden340622c2005-11-08 21:37:34 -080066 unsigned char reg[32];
67
68 int norm;
69 int input;
70 int enable;
71 int bright;
72 int contrast;
73 int hue;
74 int sat;
75};
76
77#define I2C_SAA7113 0x4A
Markus Rechberger791b4032005-11-08 21:38:08 -080078#define I2C_SAA7114 0x42
Dwaine Garden340622c2005-11-08 21:37:34 -080079
80/* ----------------------------------------------------------------------- */
81
82static inline int
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -080083saa711x_write (struct i2c_client *client,
Dwaine Garden340622c2005-11-08 21:37:34 -080084 u8 reg,
85 u8 value)
86{
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -080087 struct saa711x *decoder = i2c_get_clientdata(client);
Dwaine Garden340622c2005-11-08 21:37:34 -080088
89 decoder->reg[reg] = value;
90 return i2c_smbus_write_byte_data(client, reg, value);
91}
92
93static int
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -080094saa711x_write_block (struct i2c_client *client,
Dwaine Garden340622c2005-11-08 21:37:34 -080095 const u8 *data,
96 unsigned int len)
97{
98 int ret = -1;
99 u8 reg;
100
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800101 /* the saa711x has an autoincrement function, use it if
Dwaine Garden340622c2005-11-08 21:37:34 -0800102 * the adapter understands raw I2C */
103 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
104 /* do raw I2C, not smbus compatible */
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800105 struct saa711x *decoder = i2c_get_clientdata(client);
Dwaine Garden340622c2005-11-08 21:37:34 -0800106 struct i2c_msg msg;
107 u8 block_data[32];
108
109 msg.addr = client->addr;
110 msg.flags = 0;
111 while (len >= 2) {
112 msg.buf = (char *) block_data;
113 msg.len = 0;
114 block_data[msg.len++] = reg = data[0];
115 do {
116 block_data[msg.len++] =
117 decoder->reg[reg++] = data[1];
118 len -= 2;
119 data += 2;
120 } while (len >= 2 && data[0] == reg &&
121 msg.len < 32);
122 if ((ret = i2c_transfer(client->adapter,
123 &msg, 1)) < 0)
124 break;
125 }
126 } else {
127 /* do some slow I2C emulation kind of thing */
128 while (len >= 2) {
129 reg = *data++;
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800130 if ((ret = saa711x_write(client, reg,
Dwaine Garden340622c2005-11-08 21:37:34 -0800131 *data++)) < 0)
132 break;
133 len -= 2;
134 }
135 }
136
137 return ret;
138}
139
140static int
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800141saa711x_init_decoder (struct i2c_client *client,
Dwaine Garden340622c2005-11-08 21:37:34 -0800142 struct video_decoder_init *init)
143{
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800144 return saa711x_write_block(client, init->data, init->len);
Dwaine Garden340622c2005-11-08 21:37:34 -0800145}
146
147static inline int
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800148saa711x_read (struct i2c_client *client,
Dwaine Garden340622c2005-11-08 21:37:34 -0800149 u8 reg)
150{
151 return i2c_smbus_read_byte_data(client, reg);
152}
153
154/* ----------------------------------------------------------------------- */
155
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800156static const unsigned char saa711x_i2c_init[] = {
157 0x00, 0x00, /* PH711x_CHIP_VERSION 00 - ID byte */
158 0x01, 0x08, /* PH711x_INCREMENT_DELAY - (1) (1) (1) (1) IDEL3 IDEL2 IDELL1 IDEL0 */
159 0x02, 0xc0, /* PH711x_ANALOG_INPUT_CONTR_1 - FUSE1 FUSE0 GUDL1 GUDL0 MODE3 MODE2 MODE1 MODE0 */
160 0x03, 0x23, /* PH711x_ANALOG_INPUT_CONTR_2 - (1) HLNRS VBSL WPOFF HOLDG GAFIX GAI28 GAI18 */
161 0x04, 0x00, /* PH711x_ANALOG_INPUT_CONTR_3 - GAI17 GAI16 GAI15 GAI14 GAI13 GAI12 GAI11 GAI10 */
162 0x05, 0x00, /* PH711x_ANALOG_INPUT_CONTR_4 - GAI27 GAI26 GAI25 GAI24 GAI23 GAI22 GAI21 GAI20 */
163 0x06, 0xeb, /* PH711x_HORIZONTAL_SYNC_START - HSB7 HSB6 HSB5 HSB4 HSB3 HSB2 HSB1 HSB0 */
164 0x07, 0xe0, /* PH711x_HORIZONTAL_SYNC_STOP - HSS7 HSS6 HSS5 HSS4 HSS3 HSS2 HSS1 HSS0 */
165 0x08, 0x88, /* PH711x_SYNC_CONTROL - AUFD FSEL FOET HTC1 HTC0 HPLL VNOI1 VNOI0 */
166 0x09, 0x00, /* PH711x_LUMINANCE_CONTROL - BYPS PREF BPSS1 BPSS0 VBLB UPTCV APER1 APER0 */
167 0x0a, 0x80, /* PH711x_LUMINANCE_BRIGHTNESS - BRIG7 BRIG6 BRIG5 BRIG4 BRIG3 BRIG2 BRIG1 BRIG0 */
168 0x0b, 0x47, /* PH711x_LUMINANCE_CONTRAST - CONT7 CONT6 CONT5 CONT4 CONT3 CONT2 CONT1 CONT0 */
169 0x0c, 0x40, /* PH711x_CHROMA_SATURATION - SATN7 SATN6 SATN5 SATN4 SATN3 SATN2 SATN1 SATN0 */
170 0x0d, 0x00, /* PH711x_CHROMA_HUE_CONTROL - HUEC7 HUEC6 HUEC5 HUEC4 HUEC3 HUEC2 HUEC1 HUEC0 */
171 0x0e, 0x01, /* PH711x_CHROMA_CONTROL - CDTO CSTD2 CSTD1 CSTD0 DCCF FCTC CHBW1 CHBW0 */
172 0x0f, 0xaa, /* PH711x_CHROMA_GAIN_CONTROL - ACGC CGAIN6 CGAIN5 CGAIN4 CGAIN3 CGAIN2 CGAIN1 CGAIN0 */
173 0x10, 0x00, /* PH711x_FORMAT_DELAY_CONTROL - OFTS1 OFTS0 HDEL1 HDEL0 VRLN YDEL2 YDEL1 YDEL0 */
174 0x11, 0x1C, /* PH711x_OUTPUT_CONTROL_1 - GPSW1 CM99 GPSW0 HLSEL OEYC OERT VIPB COLO */
175 0x12, 0x01, /* PH711x_OUTPUT_CONTROL_2 - RTSE13 RTSE12 RTSE11 RTSE10 RTSE03 RTSE02 RTSE01 RTSE00 */
176 0x13, 0x00, /* PH711x_OUTPUT_CONTROL_3 - ADLSB (1) (1) OLDSB FIDP (1) AOSL1 AOSL0 */
Dwaine Garden340622c2005-11-08 21:37:34 -0800177 0x14, 0x00, /* RESERVED 14 - (1) (1) (1) (1) (1) (1) (1) (1) */
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800178 0x15, 0x00, /* PH711x_V_GATE1_START - VSTA7 VSTA6 VSTA5 VSTA4 VSTA3 VSTA2 VSTA1 VSTA0 */
179 0x16, 0x00, /* PH711x_V_GATE1_STOP - VSTO7 VSTO6 VSTO5 VSTO4 VSTO3 VSTO2 VSTO1 VSTO0 */
180 0x17, 0x00, /* PH711x_V_GATE1_MSB - (1) (1) (1) (1) (1) (1) VSTO8 VSTA8 */
Dwaine Garden340622c2005-11-08 21:37:34 -0800181};
182
183static int
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800184saa711x_command (struct i2c_client *client,
Dwaine Garden340622c2005-11-08 21:37:34 -0800185 unsigned int cmd,
186 void *arg)
187{
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800188 struct saa711x *decoder = i2c_get_clientdata(client);
Dwaine Garden340622c2005-11-08 21:37:34 -0800189
190 switch (cmd) {
191
192 case 0:
193 case DECODER_INIT:
194 {
195 struct video_decoder_init *init = arg;
196 if (NULL != init)
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800197 return saa711x_init_decoder(client, init);
Dwaine Garden340622c2005-11-08 21:37:34 -0800198 else {
199 struct video_decoder_init vdi;
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800200 vdi.data = saa711x_i2c_init;
201 vdi.len = sizeof(saa711x_i2c_init);
202 return saa711x_init_decoder(client, &vdi);
Dwaine Garden340622c2005-11-08 21:37:34 -0800203 }
204 }
205
206 case DECODER_DUMP:
207 {
208 int i;
209
210 for (i = 0; i < 32; i += 16) {
211 int j;
212
213 printk(KERN_DEBUG "%s: %03x", I2C_NAME(client), i);
214 for (j = 0; j < 16; ++j) {
215 printk(" %02x",
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800216 saa711x_read(client, i + j));
Dwaine Garden340622c2005-11-08 21:37:34 -0800217 }
218 printk("\n");
219 }
220 }
221 break;
222
223 case DECODER_GET_CAPABILITIES:
224 {
225 struct video_decoder_capability *cap = arg;
226
227 cap->flags = VIDEO_DECODER_PAL |
228 VIDEO_DECODER_NTSC |
229 VIDEO_DECODER_SECAM |
230 VIDEO_DECODER_AUTO |
231 VIDEO_DECODER_CCIR;
232 cap->inputs = 8;
233 cap->outputs = 1;
234 }
235 break;
236
237 case DECODER_GET_STATUS:
238 {
239 int *iarg = arg;
240 int status;
241 int res;
242
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800243 status = saa711x_read(client, 0x1f);
Dwaine Garden340622c2005-11-08 21:37:34 -0800244 dprintk(1, KERN_DEBUG "%s status: 0x%02x\n", I2C_NAME(client),
245 status);
246 res = 0;
247 if ((status & (1 << 6)) == 0) {
248 res |= DECODER_STATUS_GOOD;
249 }
250 switch (decoder->norm) {
251 case VIDEO_MODE_NTSC:
252 res |= DECODER_STATUS_NTSC;
253 break;
254 case VIDEO_MODE_PAL:
255 res |= DECODER_STATUS_PAL;
256 break;
257 case VIDEO_MODE_SECAM:
258 res |= DECODER_STATUS_SECAM;
259 break;
260 default:
261 case VIDEO_MODE_AUTO:
262 if ((status & (1 << 5)) != 0) {
263 res |= DECODER_STATUS_NTSC;
264 } else {
265 res |= DECODER_STATUS_PAL;
266 }
267 break;
268 }
269 if ((status & (1 << 0)) != 0) {
270 res |= DECODER_STATUS_COLOR;
271 }
272 *iarg = res;
273 }
274 break;
275
276 case DECODER_SET_GPIO:
277 {
278 int *iarg = arg;
279 if (0 != *iarg) {
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800280 saa711x_write(client, 0x11,
Dwaine Garden340622c2005-11-08 21:37:34 -0800281 (decoder->reg[0x11] | 0x80));
282 } else {
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800283 saa711x_write(client, 0x11,
Dwaine Garden340622c2005-11-08 21:37:34 -0800284 (decoder->reg[0x11] & 0x7f));
285 }
286 break;
287 }
288
289 case DECODER_SET_VBI_BYPASS:
290 {
291 int *iarg = arg;
292 if (0 != *iarg) {
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800293 saa711x_write(client, 0x13,
Dwaine Garden340622c2005-11-08 21:37:34 -0800294 (decoder->reg[0x13] & 0xf0) | 0x0a);
295 } else {
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800296 saa711x_write(client, 0x13,
Dwaine Garden340622c2005-11-08 21:37:34 -0800297 (decoder->reg[0x13] & 0xf0));
298 }
299 break;
300 }
301
302 case DECODER_SET_NORM:
303 {
304 int *iarg = arg;
305
306 switch (*iarg) {
307
308 case VIDEO_MODE_NTSC:
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800309 saa711x_write(client, 0x08,
Dwaine Garden340622c2005-11-08 21:37:34 -0800310 (decoder->reg[0x08] & 0x3f) | 0x40);
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800311 saa711x_write(client, 0x0e,
Dwaine Garden340622c2005-11-08 21:37:34 -0800312 (decoder->reg[0x0e] & 0x8f));
313 break;
314
315 case VIDEO_MODE_PAL:
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800316 saa711x_write(client, 0x08,
Dwaine Garden340622c2005-11-08 21:37:34 -0800317 (decoder->reg[0x08] & 0x3f) | 0x00);
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800318 saa711x_write(client, 0x0e,
Dwaine Garden340622c2005-11-08 21:37:34 -0800319 (decoder->reg[0x0e] & 0x8f));
320 break;
321
322 case VIDEO_MODE_SECAM:
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800323 saa711x_write(client, 0x08,
Dwaine Garden1bcd2a32005-12-01 00:51:37 -0800324 (decoder->reg[0x08] & 0x3f) | 0x00);
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800325 saa711x_write(client, 0x0e,
Dwaine Garden340622c2005-11-08 21:37:34 -0800326 (decoder->reg[0x0e] & 0x8f) | 0x50);
327 break;
328
329 case VIDEO_MODE_AUTO:
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800330 saa711x_write(client, 0x08,
Dwaine Garden340622c2005-11-08 21:37:34 -0800331 (decoder->reg[0x08] & 0x3f) | 0x80);
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800332 saa711x_write(client, 0x0e,
Dwaine Garden340622c2005-11-08 21:37:34 -0800333 (decoder->reg[0x0e] & 0x8f));
334 break;
335
336 default:
337 return -EINVAL;
338
339 }
340 decoder->norm = *iarg;
341 }
342 break;
343
344 case DECODER_SET_INPUT:
345 {
346 int *iarg = arg;
347 if (*iarg < 0 || *iarg > 9) {
348 return -EINVAL;
349 }
350 if (decoder->input != *iarg) {
351 decoder->input = *iarg;
352 /* select mode */
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800353 saa711x_write(client, 0x02,
Dwaine Garden340622c2005-11-08 21:37:34 -0800354 (decoder->reg[0x02] & 0xf0) | decoder->input);
355 /* bypass chrominance trap for modes 4..7 */
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800356 saa711x_write(client, 0x09,
Dwaine Garden340622c2005-11-08 21:37:34 -0800357 (decoder->reg[0x09] & 0x7f) | ((decoder->input > 3) ? 0x80 : 0));
358 }
359 }
360 break;
361
362 case DECODER_SET_OUTPUT:
363 {
364 int *iarg = arg;
365
366 /* not much choice of outputs */
367 if (*iarg != 0) {
368 return -EINVAL;
369 }
370 }
371 break;
372
373 case DECODER_ENABLE_OUTPUT:
374 {
375 int *iarg = arg;
376 int enable = (*iarg != 0);
377
378 if (decoder->enable != enable) {
379 decoder->enable = enable;
380
381 /* RJ: If output should be disabled (for
382 * playing videos), we also need a open PLL.
383 * The input is set to 0 (where no input
384 * source is connected), although this
385 * is not necessary.
386 *
387 * If output should be enabled, we have to
388 * reverse the above.
389 */
390
391 if (decoder->enable) {
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800392 saa711x_write(client, 0x02,
Dwaine Garden340622c2005-11-08 21:37:34 -0800393 (decoder->
394 reg[0x02] & 0xf8) |
395 decoder->input);
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800396 saa711x_write(client, 0x08,
Dwaine Garden340622c2005-11-08 21:37:34 -0800397 (decoder->reg[0x08] & 0xfb));
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800398 saa711x_write(client, 0x11,
Dwaine Garden340622c2005-11-08 21:37:34 -0800399 (decoder->
400 reg[0x11] & 0xf3) | 0x0c);
401 } else {
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800402 saa711x_write(client, 0x02,
Dwaine Garden340622c2005-11-08 21:37:34 -0800403 (decoder->reg[0x02] & 0xf8));
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800404 saa711x_write(client, 0x08,
Dwaine Garden340622c2005-11-08 21:37:34 -0800405 (decoder->
406 reg[0x08] & 0xfb) | 0x04);
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800407 saa711x_write(client, 0x11,
Dwaine Garden340622c2005-11-08 21:37:34 -0800408 (decoder->reg[0x11] & 0xf3));
409 }
410 }
411 }
412 break;
413
414 case DECODER_SET_PICTURE:
415 {
416 struct video_picture *pic = arg;
417
418 if (decoder->bright != pic->brightness) {
419 /* We want 0 to 255 we get 0-65535 */
420 decoder->bright = pic->brightness;
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800421 saa711x_write(client, 0x0a, decoder->bright >> 8);
Dwaine Garden340622c2005-11-08 21:37:34 -0800422 }
423 if (decoder->contrast != pic->contrast) {
424 /* We want 0 to 127 we get 0-65535 */
425 decoder->contrast = pic->contrast;
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800426 saa711x_write(client, 0x0b,
Dwaine Garden340622c2005-11-08 21:37:34 -0800427 decoder->contrast >> 9);
428 }
429 if (decoder->sat != pic->colour) {
430 /* We want 0 to 127 we get 0-65535 */
431 decoder->sat = pic->colour;
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800432 saa711x_write(client, 0x0c, decoder->sat >> 9);
Dwaine Garden340622c2005-11-08 21:37:34 -0800433 }
434 if (decoder->hue != pic->hue) {
435 /* We want -128 to 127 we get 0-65535 */
436 decoder->hue = pic->hue;
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800437 saa711x_write(client, 0x0d,
Dwaine Garden340622c2005-11-08 21:37:34 -0800438 (decoder->hue - 32768) >> 8);
439 }
440 }
441 break;
442
443 default:
444 return -EINVAL;
445 }
446
447 return 0;
448}
449
450/* ----------------------------------------------------------------------- */
451
452/*
453 * Generic i2c probe
454 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
455 */
456
457/* standard i2c insmod options */
458static unsigned short normal_i2c[] = {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800459 I2C_SAA7113>>1, /* saa7113 */
Markus Rechberger791b4032005-11-08 21:38:08 -0800460 I2C_SAA7114>>1, /* saa7114 */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800461 I2C_CLIENT_END
Dwaine Garden340622c2005-11-08 21:37:34 -0800462};
463
464I2C_CLIENT_INSMOD;
465
466
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800467static struct i2c_driver i2c_driver_saa711x;
Dwaine Garden340622c2005-11-08 21:37:34 -0800468
469static int
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800470saa711x_detect_client (struct i2c_adapter *adapter,
Dwaine Garden340622c2005-11-08 21:37:34 -0800471 int address,
472 int kind)
473{
474 int i;
475 struct i2c_client *client;
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800476 struct saa711x *decoder;
Dwaine Garden340622c2005-11-08 21:37:34 -0800477 struct video_decoder_init vdi;
478
479 dprintk(1,
480 KERN_INFO
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800481 "saa711x.c: detecting saa711x client on address 0x%x\n",
Dwaine Garden340622c2005-11-08 21:37:34 -0800482 address << 1);
483
484 /* Check if the adapter supports the needed features */
485 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
486 return 0;
487
Panagiotis Issaris74081872006-01-11 19:40:56 -0200488 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
Dwaine Garden340622c2005-11-08 21:37:34 -0800489 if (client == 0)
490 return -ENOMEM;
Dwaine Garden340622c2005-11-08 21:37:34 -0800491 client->addr = address;
492 client->adapter = adapter;
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800493 client->driver = &i2c_driver_saa711x;
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800494 strlcpy(I2C_NAME(client), "saa711x", sizeof(I2C_NAME(client)));
Panagiotis Issaris74081872006-01-11 19:40:56 -0200495 decoder = kzalloc(sizeof(struct saa711x), GFP_KERNEL);
Dwaine Garden340622c2005-11-08 21:37:34 -0800496 if (decoder == NULL) {
497 kfree(client);
498 return -ENOMEM;
499 }
Dwaine Garden340622c2005-11-08 21:37:34 -0800500 decoder->norm = VIDEO_MODE_NTSC;
501 decoder->input = 0;
502 decoder->enable = 1;
503 decoder->bright = 32768;
504 decoder->contrast = 32768;
505 decoder->hue = 32768;
506 decoder->sat = 32768;
507 i2c_set_clientdata(client, decoder);
508
509 i = i2c_attach_client(client);
510 if (i) {
511 kfree(client);
512 kfree(decoder);
513 return i;
514 }
515
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800516 vdi.data = saa711x_i2c_init;
517 vdi.len = sizeof(saa711x_i2c_init);
518 i = saa711x_init_decoder(client, &vdi);
Dwaine Garden340622c2005-11-08 21:37:34 -0800519 if (i < 0) {
520 dprintk(1, KERN_ERR "%s_attach error: init status %d\n",
521 I2C_NAME(client), i);
522 } else {
523 dprintk(1,
524 KERN_INFO
525 "%s_attach: chip version %x at address 0x%x\n",
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800526 I2C_NAME(client), saa711x_read(client, 0x00) >> 4,
Dwaine Garden340622c2005-11-08 21:37:34 -0800527 client->addr << 1);
528 }
529
530 return 0;
531}
532
533static int
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800534saa711x_attach_adapter (struct i2c_adapter *adapter)
Dwaine Garden340622c2005-11-08 21:37:34 -0800535{
536 dprintk(1,
537 KERN_INFO
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800538 "saa711x.c: starting probe for adapter %s (0x%x)\n",
Dwaine Garden340622c2005-11-08 21:37:34 -0800539 I2C_NAME(adapter), adapter->id);
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800540 return i2c_probe(adapter, &addr_data, &saa711x_detect_client);
Dwaine Garden340622c2005-11-08 21:37:34 -0800541}
542
543static int
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800544saa711x_detach_client (struct i2c_client *client)
Dwaine Garden340622c2005-11-08 21:37:34 -0800545{
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800546 struct saa711x *decoder = i2c_get_clientdata(client);
Dwaine Garden340622c2005-11-08 21:37:34 -0800547 int err;
548
549 err = i2c_detach_client(client);
550 if (err) {
551 return err;
552 }
553
554 kfree(decoder);
555 kfree(client);
556
557 return 0;
558}
559
560/* ----------------------------------------------------------------------- */
561
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800562static struct i2c_driver i2c_driver_saa711x = {
Laurent Riffard604f28e2005-11-26 20:43:39 +0100563 .driver = {
Laurent Riffard604f28e2005-11-26 20:43:39 +0100564 .name = "saa711x",
565 },
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800566 .id = I2C_DRIVERID_SAA711X,
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800567 .attach_adapter = saa711x_attach_adapter,
568 .detach_client = saa711x_detach_client,
569 .command = saa711x_command,
Dwaine Garden340622c2005-11-08 21:37:34 -0800570};
571
572static int __init
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800573saa711x_init (void)
Dwaine Garden340622c2005-11-08 21:37:34 -0800574{
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800575 return i2c_add_driver(&i2c_driver_saa711x);
Dwaine Garden340622c2005-11-08 21:37:34 -0800576}
577
578static void __exit
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800579saa711x_exit (void)
Dwaine Garden340622c2005-11-08 21:37:34 -0800580{
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800581 i2c_del_driver(&i2c_driver_saa711x);
Dwaine Garden340622c2005-11-08 21:37:34 -0800582}
583
Mauro Carvalho Chehab404b32f2005-11-08 21:38:29 -0800584module_init(saa711x_init);
585module_exit(saa711x_exit);