~ubuntu-branches/ubuntu/lucid/linux-rt/lucid

« back to all changes in this revision

Viewing changes to drivers/media/video/adv7175.c

  • Committer: Bazaar Package Importer
  • Author(s): Luke Yelavich
  • Date: 2009-08-05 23:00:52 UTC
  • Revision ID: james.westby@ubuntu.com-20090805230052-7xedvqcyk9dnnxb2
Tags: 2.6.31-1.1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
#include <asm/uaccess.h>
31
31
#include <linux/i2c.h>
32
32
#include <linux/i2c-id.h>
33
 
#include <linux/videodev.h>
34
 
#include <linux/video_encoder.h>
35
 
#include <media/v4l2-common.h>
36
 
#include <media/v4l2-i2c-drv-legacy.h>
 
33
#include <linux/videodev2.h>
 
34
#include <media/v4l2-device.h>
 
35
#include <media/v4l2-chip-ident.h>
 
36
#include <media/v4l2-i2c-drv.h>
37
37
 
38
38
MODULE_DESCRIPTION("Analog Devices ADV7175 video encoder driver");
39
39
MODULE_AUTHOR("Dave Perks");
40
40
MODULE_LICENSE("GPL");
41
41
 
 
42
#define   I2C_ADV7175        0xd4
 
43
#define   I2C_ADV7176        0x54
 
44
 
 
45
 
42
46
static int debug;
43
47
module_param(debug, int, 0);
44
48
MODULE_PARM_DESC(debug, "Debug level (0-1)");
46
50
/* ----------------------------------------------------------------------- */
47
51
 
48
52
struct adv7175 {
49
 
        int norm;
 
53
        struct v4l2_subdev sd;
 
54
        v4l2_std_id norm;
50
55
        int input;
51
 
        int enable;
52
 
        int bright;
53
 
        int contrast;
54
 
        int hue;
55
 
        int sat;
56
56
};
57
57
 
58
 
#define   I2C_ADV7175        0xd4
59
 
#define   I2C_ADV7176        0x54
 
58
static inline struct adv7175 *to_adv7175(struct v4l2_subdev *sd)
 
59
{
 
60
        return container_of(sd, struct adv7175, sd);
 
61
}
60
62
 
61
63
static char *inputs[] = { "pass_through", "play_back", "color_bar" };
62
 
static char *norms[] = { "PAL", "NTSC", "SECAM->PAL (may not work!)" };
63
64
 
64
65
/* ----------------------------------------------------------------------- */
65
66
 
66
 
static inline int adv7175_write(struct i2c_client *client, u8 reg, u8 value)
 
67
static inline int adv7175_write(struct v4l2_subdev *sd, u8 reg, u8 value)
67
68
{
 
69
        struct i2c_client *client = v4l2_get_subdevdata(sd);
 
70
 
68
71
        return i2c_smbus_write_byte_data(client, reg, value);
69
72
}
70
73
 
71
 
static inline int adv7175_read(struct i2c_client *client, u8 reg)
 
74
static inline int adv7175_read(struct v4l2_subdev *sd, u8 reg)
72
75
{
 
76
        struct i2c_client *client = v4l2_get_subdevdata(sd);
 
77
 
73
78
        return i2c_smbus_read_byte_data(client, reg);
74
79
}
75
80
 
76
 
static int adv7175_write_block(struct i2c_client *client,
 
81
static int adv7175_write_block(struct v4l2_subdev *sd,
77
82
                     const u8 *data, unsigned int len)
78
83
{
 
84
        struct i2c_client *client = v4l2_get_subdevdata(sd);
79
85
        int ret = -1;
80
86
        u8 reg;
81
87
 
103
109
                /* do some slow I2C emulation kind of thing */
104
110
                while (len >= 2) {
105
111
                        reg = *data++;
106
 
                        ret = adv7175_write(client, reg, *data++);
 
112
                        ret = adv7175_write(sd, reg, *data++);
107
113
                        if (ret < 0)
108
114
                                break;
109
115
                        len -= 2;
113
119
        return ret;
114
120
}
115
121
 
116
 
static void set_subcarrier_freq(struct i2c_client *client, int pass_through)
 
122
static void set_subcarrier_freq(struct v4l2_subdev *sd, int pass_through)
117
123
{
118
124
        /* for some reason pass_through NTSC needs
119
125
         * a different sub-carrier freq to remain stable. */
120
126
        if (pass_through)
121
 
                adv7175_write(client, 0x02, 0x00);
 
127
                adv7175_write(sd, 0x02, 0x00);
122
128
        else
123
 
                adv7175_write(client, 0x02, 0x55);
 
129
                adv7175_write(sd, 0x02, 0x55);
124
130
 
125
 
        adv7175_write(client, 0x03, 0x55);
126
 
        adv7175_write(client, 0x04, 0x55);
127
 
        adv7175_write(client, 0x05, 0x25);
 
131
        adv7175_write(sd, 0x03, 0x55);
 
132
        adv7175_write(sd, 0x04, 0x55);
 
133
        adv7175_write(sd, 0x05, 0x25);
128
134
}
129
135
 
130
136
/* ----------------------------------------------------------------------- */
184
190
        0x06, 0x1a,             /* subc. phase */
185
191
};
186
192
 
187
 
static int adv7175_command(struct i2c_client *client, unsigned cmd, void *arg)
188
 
{
189
 
        struct adv7175 *encoder = i2c_get_clientdata(client);
190
 
 
191
 
        switch (cmd) {
 
193
static int adv7175_init(struct v4l2_subdev *sd, u32 val)
 
194
{
 
195
        /* This is just for testing!!! */
 
196
        adv7175_write_block(sd, init_common, sizeof(init_common));
 
197
        adv7175_write(sd, 0x07, TR0MODE | TR0RST);
 
198
        adv7175_write(sd, 0x07, TR0MODE);
 
199
        return 0;
 
200
}
 
201
 
 
202
static int adv7175_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std)
 
203
{
 
204
        struct adv7175 *encoder = to_adv7175(sd);
 
205
 
 
206
        if (std & V4L2_STD_NTSC) {
 
207
                adv7175_write_block(sd, init_ntsc, sizeof(init_ntsc));
 
208
                if (encoder->input == 0)
 
209
                        adv7175_write(sd, 0x0d, 0x4f);  /* Enable genlock */
 
210
                adv7175_write(sd, 0x07, TR0MODE | TR0RST);
 
211
                adv7175_write(sd, 0x07, TR0MODE);
 
212
        } else if (std & V4L2_STD_PAL) {
 
213
                adv7175_write_block(sd, init_pal, sizeof(init_pal));
 
214
                if (encoder->input == 0)
 
215
                        adv7175_write(sd, 0x0d, 0x4f);  /* Enable genlock */
 
216
                adv7175_write(sd, 0x07, TR0MODE | TR0RST);
 
217
                adv7175_write(sd, 0x07, TR0MODE);
 
218
        } else if (std & V4L2_STD_SECAM) {
 
219
                /* This is an attempt to convert
 
220
                 * SECAM->PAL (typically it does not work
 
221
                 * due to genlock: when decoder is in SECAM
 
222
                 * and encoder in in PAL the subcarrier can
 
223
                 * not be syncronized with horizontal
 
224
                 * quency) */
 
225
                adv7175_write_block(sd, init_pal, sizeof(init_pal));
 
226
                if (encoder->input == 0)
 
227
                        adv7175_write(sd, 0x0d, 0x49);  /* Disable genlock */
 
228
                adv7175_write(sd, 0x07, TR0MODE | TR0RST);
 
229
                adv7175_write(sd, 0x07, TR0MODE);
 
230
        } else {
 
231
                v4l2_dbg(1, debug, sd, "illegal norm: %llx\n",
 
232
                                (unsigned long long)std);
 
233
                return -EINVAL;
 
234
        }
 
235
        v4l2_dbg(1, debug, sd, "switched to %llx\n", (unsigned long long)std);
 
236
        encoder->norm = std;
 
237
        return 0;
 
238
}
 
239
 
 
240
static int adv7175_s_routing(struct v4l2_subdev *sd,
 
241
                             u32 input, u32 output, u32 config)
 
242
{
 
243
        struct adv7175 *encoder = to_adv7175(sd);
 
244
 
 
245
        /* RJ: input = 0: input is from decoder
 
246
           input = 1: input is from ZR36060
 
247
           input = 2: color bar */
 
248
 
 
249
        switch (input) {
192
250
        case 0:
193
 
                /* This is just for testing!!! */
194
 
                adv7175_write_block(client, init_common,
195
 
                                    sizeof(init_common));
196
 
                adv7175_write(client, 0x07, TR0MODE | TR0RST);
197
 
                adv7175_write(client, 0x07, TR0MODE);
198
 
                break;
199
 
 
200
 
        case ENCODER_GET_CAPABILITIES:
201
 
        {
202
 
                struct video_encoder_capability *cap = arg;
203
 
 
204
 
                cap->flags = VIDEO_ENCODER_PAL |
205
 
                             VIDEO_ENCODER_NTSC |
206
 
                             VIDEO_ENCODER_SECAM; /* well, hacky */
207
 
                cap->inputs = 2;
208
 
                cap->outputs = 1;
209
 
                break;
210
 
        }
211
 
 
212
 
        case ENCODER_SET_NORM:
213
 
        {
214
 
                int iarg = *(int *) arg;
215
 
 
216
 
                switch (iarg) {
217
 
                case VIDEO_MODE_NTSC:
218
 
                        adv7175_write_block(client, init_ntsc,
219
 
                                            sizeof(init_ntsc));
220
 
                        if (encoder->input == 0)
221
 
                                adv7175_write(client, 0x0d, 0x4f);      // Enable genlock
222
 
                        adv7175_write(client, 0x07, TR0MODE | TR0RST);
223
 
                        adv7175_write(client, 0x07, TR0MODE);
224
 
                        break;
225
 
 
226
 
                case VIDEO_MODE_PAL:
227
 
                        adv7175_write_block(client, init_pal,
228
 
                                            sizeof(init_pal));
229
 
                        if (encoder->input == 0)
230
 
                                adv7175_write(client, 0x0d, 0x4f);      // Enable genlock
231
 
                        adv7175_write(client, 0x07, TR0MODE | TR0RST);
232
 
                        adv7175_write(client, 0x07, TR0MODE);
233
 
                        break;
234
 
 
235
 
                case VIDEO_MODE_SECAM:  // WARNING! ADV7176 does not support SECAM.
236
 
                        /* This is an attempt to convert
237
 
                         * SECAM->PAL (typically it does not work
238
 
                         * due to genlock: when decoder is in SECAM
239
 
                         * and encoder in in PAL the subcarrier can
240
 
                         * not be syncronized with horizontal
241
 
                         * quency) */
242
 
                        adv7175_write_block(client, init_pal,
243
 
                                            sizeof(init_pal));
244
 
                        if (encoder->input == 0)
245
 
                                adv7175_write(client, 0x0d, 0x49);      // Disable genlock
246
 
                        adv7175_write(client, 0x07, TR0MODE | TR0RST);
247
 
                        adv7175_write(client, 0x07, TR0MODE);
248
 
                        break;
249
 
                default:
250
 
                        v4l_dbg(1, debug, client, "illegal norm: %d\n", iarg);
251
 
                        return -EINVAL;
252
 
                }
253
 
                v4l_dbg(1, debug, client, "switched to %s\n", norms[iarg]);
254
 
                encoder->norm = iarg;
255
 
                break;
256
 
        }
257
 
 
258
 
        case ENCODER_SET_INPUT:
259
 
        {
260
 
                int iarg = *(int *) arg;
261
 
 
262
 
                /* RJ: *iarg = 0: input is from SAA7110
263
 
                 *iarg = 1: input is from ZR36060
264
 
                 *iarg = 2: color bar */
265
 
 
266
 
                switch (iarg) {
267
 
                case 0:
268
 
                        adv7175_write(client, 0x01, 0x00);
269
 
 
270
 
                        if (encoder->norm == VIDEO_MODE_NTSC)
271
 
                                set_subcarrier_freq(client, 1);
272
 
 
273
 
                        adv7175_write(client, 0x0c, TR1CAPT);   /* TR1 */
274
 
                        if (encoder->norm == VIDEO_MODE_SECAM)
275
 
                                adv7175_write(client, 0x0d, 0x49);      // Disable genlock
276
 
                        else
277
 
                                adv7175_write(client, 0x0d, 0x4f);      // Enable genlock
278
 
                        adv7175_write(client, 0x07, TR0MODE | TR0RST);
279
 
                        adv7175_write(client, 0x07, TR0MODE);
280
 
                        //udelay(10);
281
 
                        break;
282
 
 
283
 
                case 1:
284
 
                        adv7175_write(client, 0x01, 0x00);
285
 
 
286
 
                        if (encoder->norm == VIDEO_MODE_NTSC)
287
 
                                set_subcarrier_freq(client, 0);
288
 
 
289
 
                        adv7175_write(client, 0x0c, TR1PLAY);   /* TR1 */
290
 
                        adv7175_write(client, 0x0d, 0x49);
291
 
                        adv7175_write(client, 0x07, TR0MODE | TR0RST);
292
 
                        adv7175_write(client, 0x07, TR0MODE);
293
 
                        /* udelay(10); */
294
 
                        break;
295
 
 
296
 
                case 2:
297
 
                        adv7175_write(client, 0x01, 0x80);
298
 
 
299
 
                        if (encoder->norm == VIDEO_MODE_NTSC)
300
 
                                set_subcarrier_freq(client, 0);
301
 
 
302
 
                        adv7175_write(client, 0x0d, 0x49);
303
 
                        adv7175_write(client, 0x07, TR0MODE | TR0RST);
304
 
                        adv7175_write(client, 0x07, TR0MODE);
305
 
                        /* udelay(10); */
306
 
                        break;
307
 
 
308
 
                default:
309
 
                        v4l_dbg(1, debug, client, "illegal input: %d\n", iarg);
310
 
                        return -EINVAL;
311
 
                }
312
 
                v4l_dbg(1, debug, client, "switched to %s\n", inputs[iarg]);
313
 
                encoder->input = iarg;
314
 
                break;
315
 
        }
316
 
 
317
 
        case ENCODER_SET_OUTPUT:
318
 
        {
319
 
                int *iarg = arg;
320
 
 
321
 
                /* not much choice of outputs */
322
 
                if (*iarg != 0)
323
 
                        return -EINVAL;
324
 
                break;
325
 
        }
326
 
 
327
 
        case ENCODER_ENABLE_OUTPUT:
328
 
        {
329
 
                int *iarg = arg;
330
 
 
331
 
                encoder->enable = !!*iarg;
332
 
                break;
333
 
        }
 
251
                adv7175_write(sd, 0x01, 0x00);
 
252
 
 
253
                if (encoder->norm & V4L2_STD_NTSC)
 
254
                        set_subcarrier_freq(sd, 1);
 
255
 
 
256
                adv7175_write(sd, 0x0c, TR1CAPT);       /* TR1 */
 
257
                if (encoder->norm & V4L2_STD_SECAM)
 
258
                        adv7175_write(sd, 0x0d, 0x49);  /* Disable genlock */
 
259
                else
 
260
                        adv7175_write(sd, 0x0d, 0x4f);  /* Enable genlock */
 
261
                adv7175_write(sd, 0x07, TR0MODE | TR0RST);
 
262
                adv7175_write(sd, 0x07, TR0MODE);
 
263
                /*udelay(10);*/
 
264
                break;
 
265
 
 
266
        case 1:
 
267
                adv7175_write(sd, 0x01, 0x00);
 
268
 
 
269
                if (encoder->norm & V4L2_STD_NTSC)
 
270
                        set_subcarrier_freq(sd, 0);
 
271
 
 
272
                adv7175_write(sd, 0x0c, TR1PLAY);       /* TR1 */
 
273
                adv7175_write(sd, 0x0d, 0x49);
 
274
                adv7175_write(sd, 0x07, TR0MODE | TR0RST);
 
275
                adv7175_write(sd, 0x07, TR0MODE);
 
276
                /* udelay(10); */
 
277
                break;
 
278
 
 
279
        case 2:
 
280
                adv7175_write(sd, 0x01, 0x80);
 
281
 
 
282
                if (encoder->norm & V4L2_STD_NTSC)
 
283
                        set_subcarrier_freq(sd, 0);
 
284
 
 
285
                adv7175_write(sd, 0x0d, 0x49);
 
286
                adv7175_write(sd, 0x07, TR0MODE | TR0RST);
 
287
                adv7175_write(sd, 0x07, TR0MODE);
 
288
                /* udelay(10); */
 
289
                break;
334
290
 
335
291
        default:
 
292
                v4l2_dbg(1, debug, sd, "illegal input: %d\n", input);
336
293
                return -EINVAL;
337
294
        }
338
 
 
 
295
        v4l2_dbg(1, debug, sd, "switched to %s\n", inputs[input]);
 
296
        encoder->input = input;
339
297
        return 0;
340
298
}
341
299
 
342
 
/* ----------------------------------------------------------------------- */
343
 
 
344
 
/*
345
 
 * Generic i2c probe
346
 
 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
347
 
 */
348
 
static unsigned short normal_i2c[] = {
349
 
        I2C_ADV7175 >> 1, (I2C_ADV7175 >> 1) + 1,
350
 
        I2C_ADV7176 >> 1, (I2C_ADV7176 >> 1) + 1,
351
 
        I2C_CLIENT_END
352
 
};
353
 
 
354
 
I2C_CLIENT_INSMOD;
 
300
static int adv7175_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
 
301
{
 
302
        struct i2c_client *client = v4l2_get_subdevdata(sd);
 
303
 
 
304
        return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_ADV7175, 0);
 
305
}
 
306
 
 
307
/* ----------------------------------------------------------------------- */
 
308
 
 
309
static const struct v4l2_subdev_core_ops adv7175_core_ops = {
 
310
        .g_chip_ident = adv7175_g_chip_ident,
 
311
        .init = adv7175_init,
 
312
};
 
313
 
 
314
static const struct v4l2_subdev_video_ops adv7175_video_ops = {
 
315
        .s_std_output = adv7175_s_std_output,
 
316
        .s_routing = adv7175_s_routing,
 
317
};
 
318
 
 
319
static const struct v4l2_subdev_ops adv7175_ops = {
 
320
        .core = &adv7175_core_ops,
 
321
        .video = &adv7175_video_ops,
 
322
};
 
323
 
 
324
/* ----------------------------------------------------------------------- */
355
325
 
356
326
static int adv7175_probe(struct i2c_client *client,
357
327
                        const struct i2c_device_id *id)
358
328
{
359
329
        int i;
360
330
        struct adv7175 *encoder;
 
331
        struct v4l2_subdev *sd;
361
332
 
362
333
        /* Check if the adapter supports the needed features */
363
334
        if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
369
340
        encoder = kzalloc(sizeof(struct adv7175), GFP_KERNEL);
370
341
        if (encoder == NULL)
371
342
                return -ENOMEM;
372
 
        encoder->norm = VIDEO_MODE_PAL;
 
343
        sd = &encoder->sd;
 
344
        v4l2_i2c_subdev_init(sd, client, &adv7175_ops);
 
345
        encoder->norm = V4L2_STD_NTSC;
373
346
        encoder->input = 0;
374
 
        encoder->enable = 1;
375
 
        i2c_set_clientdata(client, encoder);
376
347
 
377
 
        i = adv7175_write_block(client, init_common, sizeof(init_common));
 
348
        i = adv7175_write_block(sd, init_common, sizeof(init_common));
378
349
        if (i >= 0) {
379
 
                i = adv7175_write(client, 0x07, TR0MODE | TR0RST);
380
 
                i = adv7175_write(client, 0x07, TR0MODE);
381
 
                i = adv7175_read(client, 0x12);
382
 
                v4l_dbg(1, debug, client, "revision %d\n", i & 1);
 
350
                i = adv7175_write(sd, 0x07, TR0MODE | TR0RST);
 
351
                i = adv7175_write(sd, 0x07, TR0MODE);
 
352
                i = adv7175_read(sd, 0x12);
 
353
                v4l2_dbg(1, debug, sd, "revision %d\n", i & 1);
383
354
        }
384
355
        if (i < 0)
385
 
                v4l_dbg(1, debug, client, "init error 0x%x\n", i);
 
356
                v4l2_dbg(1, debug, sd, "init error 0x%x\n", i);
386
357
        return 0;
387
358
}
388
359
 
389
360
static int adv7175_remove(struct i2c_client *client)
390
361
{
391
 
        kfree(i2c_get_clientdata(client));
 
362
        struct v4l2_subdev *sd = i2c_get_clientdata(client);
 
363
 
 
364
        v4l2_device_unregister_subdev(sd);
 
365
        kfree(to_adv7175(sd));
392
366
        return 0;
393
367
}
394
368
 
403
377
 
404
378
static struct v4l2_i2c_driver_data v4l2_i2c_data = {
405
379
        .name = "adv7175",
406
 
        .driverid = I2C_DRIVERID_ADV7175,
407
 
        .command = adv7175_command,
408
380
        .probe = adv7175_probe,
409
381
        .remove = adv7175_remove,
410
382
        .id_table = adv7175_id,