V4L/DVB: tvp514x: there is only one supported format, so simplify the code
[pandora-kernel.git] / drivers / media / video / tvp514x.c
1 /*
2  * drivers/media/video/tvp514x.c
3  *
4  * TI TVP5146/47 decoder driver
5  *
6  * Copyright (C) 2008 Texas Instruments Inc
7  * Author: Vaibhav Hiremath <hvaibhav@ti.com>
8  *
9  * Contributors:
10  *     Sivaraj R <sivaraj@ti.com>
11  *     Brijesh R Jadav <brijesh.j@ti.com>
12  *     Hardik Shah <hardik.shah@ti.com>
13  *     Manjunath Hadli <mrh@ti.com>
14  *     Karicheri Muralidharan <m-karicheri2@ti.com>
15  *
16  * This package is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License version 2 as
18  * published by the Free Software Foundation.
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
31 #include <linux/i2c.h>
32 #include <linux/slab.h>
33 #include <linux/delay.h>
34 #include <linux/videodev2.h>
35
36 #include <media/v4l2-device.h>
37 #include <media/v4l2-common.h>
38 #include <media/v4l2-chip-ident.h>
39 #include <media/tvp514x.h>
40
41 #include "tvp514x_regs.h"
42
43 /* Module Name */
44 #define TVP514X_MODULE_NAME             "tvp514x"
45
46 /* Private macros for TVP */
47 #define I2C_RETRY_COUNT                 (5)
48 #define LOCK_RETRY_COUNT                (5)
49 #define LOCK_RETRY_DELAY                (200)
50
51 /* Debug functions */
52 static int debug;
53 module_param(debug, bool, 0644);
54 MODULE_PARM_DESC(debug, "Debug level (0-1)");
55
56 MODULE_AUTHOR("Texas Instruments");
57 MODULE_DESCRIPTION("TVP514X linux decoder driver");
58 MODULE_LICENSE("GPL");
59
60 /* enum tvp514x_std - enum for supported standards */
61 enum tvp514x_std {
62         STD_NTSC_MJ = 0,
63         STD_PAL_BDGHIN,
64         STD_INVALID
65 };
66
67 /**
68  * struct tvp514x_std_info - Structure to store standard informations
69  * @width: Line width in pixels
70  * @height:Number of active lines
71  * @video_std: Value to write in REG_VIDEO_STD register
72  * @standard: v4l2 standard structure information
73  */
74 struct tvp514x_std_info {
75         unsigned long width;
76         unsigned long height;
77         u8 video_std;
78         struct v4l2_standard standard;
79 };
80
81 static struct tvp514x_reg tvp514x_reg_list_default[0x40];
82
83 static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable);
84 /**
85  * struct tvp514x_decoder - TVP5146/47 decoder object
86  * @sd: Subdevice Slave handle
87  * @tvp514x_regs: copy of hw's regs with preset values.
88  * @pdata: Board specific
89  * @ver: Chip version
90  * @streaming: TVP5146/47 decoder streaming - enabled or disabled.
91  * @pix: Current pixel format
92  * @current_std: Current standard
93  * @num_stds: Number of standards
94  * @std_list: Standards list
95  * @input: Input routing at chip level
96  * @output: Output routing at chip level
97  */
98 struct tvp514x_decoder {
99         struct v4l2_subdev sd;
100         struct tvp514x_reg tvp514x_regs[ARRAY_SIZE(tvp514x_reg_list_default)];
101         const struct tvp514x_platform_data *pdata;
102
103         int ver;
104         int streaming;
105
106         struct v4l2_pix_format pix;
107
108         enum tvp514x_std current_std;
109         int num_stds;
110         const struct tvp514x_std_info *std_list;
111         /* Input and Output Routing parameters */
112         u32 input;
113         u32 output;
114 };
115
116 /* TVP514x default register values */
117 static struct tvp514x_reg tvp514x_reg_list_default[] = {
118         /* Composite selected */
119         {TOK_WRITE, REG_INPUT_SEL, 0x05},
120         {TOK_WRITE, REG_AFE_GAIN_CTRL, 0x0F},
121         /* Auto mode */
122         {TOK_WRITE, REG_VIDEO_STD, 0x00},
123         {TOK_WRITE, REG_OPERATION_MODE, 0x00},
124         {TOK_SKIP, REG_AUTOSWITCH_MASK, 0x3F},
125         {TOK_WRITE, REG_COLOR_KILLER, 0x10},
126         {TOK_WRITE, REG_LUMA_CONTROL1, 0x00},
127         {TOK_WRITE, REG_LUMA_CONTROL2, 0x00},
128         {TOK_WRITE, REG_LUMA_CONTROL3, 0x02},
129         {TOK_WRITE, REG_BRIGHTNESS, 0x80},
130         {TOK_WRITE, REG_CONTRAST, 0x80},
131         {TOK_WRITE, REG_SATURATION, 0x80},
132         {TOK_WRITE, REG_HUE, 0x00},
133         {TOK_WRITE, REG_CHROMA_CONTROL1, 0x00},
134         {TOK_WRITE, REG_CHROMA_CONTROL2, 0x0E},
135         /* Reserved */
136         {TOK_SKIP, 0x0F, 0x00},
137         {TOK_WRITE, REG_COMP_PR_SATURATION, 0x80},
138         {TOK_WRITE, REG_COMP_Y_CONTRAST, 0x80},
139         {TOK_WRITE, REG_COMP_PB_SATURATION, 0x80},
140         /* Reserved */
141         {TOK_SKIP, 0x13, 0x00},
142         {TOK_WRITE, REG_COMP_Y_BRIGHTNESS, 0x80},
143         /* Reserved */
144         {TOK_SKIP, 0x15, 0x00},
145         /* NTSC timing */
146         {TOK_SKIP, REG_AVID_START_PIXEL_LSB, 0x55},
147         {TOK_SKIP, REG_AVID_START_PIXEL_MSB, 0x00},
148         {TOK_SKIP, REG_AVID_STOP_PIXEL_LSB, 0x25},
149         {TOK_SKIP, REG_AVID_STOP_PIXEL_MSB, 0x03},
150         /* NTSC timing */
151         {TOK_SKIP, REG_HSYNC_START_PIXEL_LSB, 0x00},
152         {TOK_SKIP, REG_HSYNC_START_PIXEL_MSB, 0x00},
153         {TOK_SKIP, REG_HSYNC_STOP_PIXEL_LSB, 0x40},
154         {TOK_SKIP, REG_HSYNC_STOP_PIXEL_MSB, 0x00},
155         /* NTSC timing */
156         {TOK_SKIP, REG_VSYNC_START_LINE_LSB, 0x04},
157         {TOK_SKIP, REG_VSYNC_START_LINE_MSB, 0x00},
158         {TOK_SKIP, REG_VSYNC_STOP_LINE_LSB, 0x07},
159         {TOK_SKIP, REG_VSYNC_STOP_LINE_MSB, 0x00},
160         /* NTSC timing */
161         {TOK_SKIP, REG_VBLK_START_LINE_LSB, 0x01},
162         {TOK_SKIP, REG_VBLK_START_LINE_MSB, 0x00},
163         {TOK_SKIP, REG_VBLK_STOP_LINE_LSB, 0x15},
164         {TOK_SKIP, REG_VBLK_STOP_LINE_MSB, 0x00},
165         /* Reserved */
166         {TOK_SKIP, 0x26, 0x00},
167         /* Reserved */
168         {TOK_SKIP, 0x27, 0x00},
169         {TOK_SKIP, REG_FAST_SWTICH_CONTROL, 0xCC},
170         /* Reserved */
171         {TOK_SKIP, 0x29, 0x00},
172         {TOK_SKIP, REG_FAST_SWTICH_SCART_DELAY, 0x00},
173         /* Reserved */
174         {TOK_SKIP, 0x2B, 0x00},
175         {TOK_SKIP, REG_SCART_DELAY, 0x00},
176         {TOK_SKIP, REG_CTI_DELAY, 0x00},
177         {TOK_SKIP, REG_CTI_CONTROL, 0x00},
178         /* Reserved */
179         {TOK_SKIP, 0x2F, 0x00},
180         /* Reserved */
181         {TOK_SKIP, 0x30, 0x00},
182         /* Reserved */
183         {TOK_SKIP, 0x31, 0x00},
184         /* HS, VS active high */
185         {TOK_WRITE, REG_SYNC_CONTROL, 0x00},
186         /* 10-bit BT.656 */
187         {TOK_WRITE, REG_OUTPUT_FORMATTER1, 0x00},
188         /* Enable clk & data */
189         {TOK_WRITE, REG_OUTPUT_FORMATTER2, 0x11},
190         /* Enable AVID & FLD */
191         {TOK_WRITE, REG_OUTPUT_FORMATTER3, 0xEE},
192         /* Enable VS & HS */
193         {TOK_WRITE, REG_OUTPUT_FORMATTER4, 0xAF},
194         {TOK_WRITE, REG_OUTPUT_FORMATTER5, 0xFF},
195         {TOK_WRITE, REG_OUTPUT_FORMATTER6, 0xFF},
196         /* Clear status */
197         {TOK_WRITE, REG_CLEAR_LOST_LOCK, 0x01},
198         {TOK_TERM, 0, 0},
199 };
200
201 /**
202  * List of image formats supported by TVP5146/47 decoder
203  * Currently we are using 8 bit mode only, but can be
204  * extended to 10/20 bit mode.
205  */
206 static const struct v4l2_fmtdesc tvp514x_fmt_list[] = {
207         {
208          .index = 0,
209          .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
210          .flags = 0,
211          .description = "8-bit UYVY 4:2:2 Format",
212          .pixelformat = V4L2_PIX_FMT_UYVY,
213         },
214 };
215
216 /**
217  * Supported standards -
218  *
219  * Currently supports two standards only, need to add support for rest of the
220  * modes, like SECAM, etc...
221  */
222 static const struct tvp514x_std_info tvp514x_std_list[] = {
223         /* Standard: STD_NTSC_MJ */
224         [STD_NTSC_MJ] = {
225          .width = NTSC_NUM_ACTIVE_PIXELS,
226          .height = NTSC_NUM_ACTIVE_LINES,
227          .video_std = VIDEO_STD_NTSC_MJ_BIT,
228          .standard = {
229                       .index = 0,
230                       .id = V4L2_STD_NTSC,
231                       .name = "NTSC",
232                       .frameperiod = {1001, 30000},
233                       .framelines = 525
234                      },
235         /* Standard: STD_PAL_BDGHIN */
236         },
237         [STD_PAL_BDGHIN] = {
238          .width = PAL_NUM_ACTIVE_PIXELS,
239          .height = PAL_NUM_ACTIVE_LINES,
240          .video_std = VIDEO_STD_PAL_BDGHIN_BIT,
241          .standard = {
242                       .index = 1,
243                       .id = V4L2_STD_PAL,
244                       .name = "PAL",
245                       .frameperiod = {1, 25},
246                       .framelines = 625
247                      },
248         },
249         /* Standard: need to add for additional standard */
250 };
251
252
253 static inline struct tvp514x_decoder *to_decoder(struct v4l2_subdev *sd)
254 {
255         return container_of(sd, struct tvp514x_decoder, sd);
256 }
257
258
259 /**
260  * tvp514x_read_reg() - Read a value from a register in an TVP5146/47.
261  * @sd: ptr to v4l2_subdev struct
262  * @reg: TVP5146/47 register address
263  *
264  * Returns value read if successful, or non-zero (-1) otherwise.
265  */
266 static int tvp514x_read_reg(struct v4l2_subdev *sd, u8 reg)
267 {
268         int err, retry = 0;
269         struct i2c_client *client = v4l2_get_subdevdata(sd);
270
271 read_again:
272
273         err = i2c_smbus_read_byte_data(client, reg);
274         if (err < 0) {
275                 if (retry <= I2C_RETRY_COUNT) {
276                         v4l2_warn(sd, "Read: retry ... %d\n", retry);
277                         retry++;
278                         msleep_interruptible(10);
279                         goto read_again;
280                 }
281         }
282
283         return err;
284 }
285
286 /**
287  * dump_reg() - dump the register content of TVP5146/47.
288  * @sd: ptr to v4l2_subdev struct
289  * @reg: TVP5146/47 register address
290  */
291 static void dump_reg(struct v4l2_subdev *sd, u8 reg)
292 {
293         u32 val;
294
295         val = tvp514x_read_reg(sd, reg);
296         v4l2_info(sd, "Reg(0x%.2X): 0x%.2X\n", reg, val);
297 }
298
299 /**
300  * tvp514x_write_reg() - Write a value to a register in TVP5146/47
301  * @sd: ptr to v4l2_subdev struct
302  * @reg: TVP5146/47 register address
303  * @val: value to be written to the register
304  *
305  * Write a value to a register in an TVP5146/47 decoder device.
306  * Returns zero if successful, or non-zero otherwise.
307  */
308 static int tvp514x_write_reg(struct v4l2_subdev *sd, u8 reg, u8 val)
309 {
310         int err, retry = 0;
311         struct i2c_client *client = v4l2_get_subdevdata(sd);
312
313 write_again:
314
315         err = i2c_smbus_write_byte_data(client, reg, val);
316         if (err) {
317                 if (retry <= I2C_RETRY_COUNT) {
318                         v4l2_warn(sd, "Write: retry ... %d\n", retry);
319                         retry++;
320                         msleep_interruptible(10);
321                         goto write_again;
322                 }
323         }
324
325         return err;
326 }
327
328 /**
329  * tvp514x_write_regs() : Initializes a list of TVP5146/47 registers
330  * @sd: ptr to v4l2_subdev struct
331  * @reglist: list of TVP5146/47 registers and values
332  *
333  * Initializes a list of TVP5146/47 registers:-
334  *              if token is TOK_TERM, then entire write operation terminates
335  *              if token is TOK_DELAY, then a delay of 'val' msec is introduced
336  *              if token is TOK_SKIP, then the register write is skipped
337  *              if token is TOK_WRITE, then the register write is performed
338  * Returns zero if successful, or non-zero otherwise.
339  */
340 static int tvp514x_write_regs(struct v4l2_subdev *sd,
341                               const struct tvp514x_reg reglist[])
342 {
343         int err;
344         const struct tvp514x_reg *next = reglist;
345
346         for (; next->token != TOK_TERM; next++) {
347                 if (next->token == TOK_DELAY) {
348                         msleep(next->val);
349                         continue;
350                 }
351
352                 if (next->token == TOK_SKIP)
353                         continue;
354
355                 err = tvp514x_write_reg(sd, next->reg, (u8) next->val);
356                 if (err) {
357                         v4l2_err(sd, "Write failed. Err[%d]\n", err);
358                         return err;
359                 }
360         }
361         return 0;
362 }
363
364 /**
365  * tvp514x_query_current_std() : Query the current standard detected by TVP5146/47
366  * @sd: ptr to v4l2_subdev struct
367  *
368  * Returns the current standard detected by TVP5146/47, STD_INVALID if there is no
369  * standard detected.
370  */
371 static enum tvp514x_std tvp514x_query_current_std(struct v4l2_subdev *sd)
372 {
373         u8 std, std_status;
374
375         std = tvp514x_read_reg(sd, REG_VIDEO_STD);
376         if ((std & VIDEO_STD_MASK) == VIDEO_STD_AUTO_SWITCH_BIT)
377                 /* use the standard status register */
378                 std_status = tvp514x_read_reg(sd, REG_VIDEO_STD_STATUS);
379         else
380                 /* use the standard register itself */
381                 std_status = std;
382
383         switch (std_status & VIDEO_STD_MASK) {
384         case VIDEO_STD_NTSC_MJ_BIT:
385                 return STD_NTSC_MJ;
386
387         case VIDEO_STD_PAL_BDGHIN_BIT:
388                 return STD_PAL_BDGHIN;
389
390         default:
391                 return STD_INVALID;
392         }
393
394         return STD_INVALID;
395 }
396
397 /* TVP5146/47 register dump function */
398 static void tvp514x_reg_dump(struct v4l2_subdev *sd)
399 {
400         dump_reg(sd, REG_INPUT_SEL);
401         dump_reg(sd, REG_AFE_GAIN_CTRL);
402         dump_reg(sd, REG_VIDEO_STD);
403         dump_reg(sd, REG_OPERATION_MODE);
404         dump_reg(sd, REG_COLOR_KILLER);
405         dump_reg(sd, REG_LUMA_CONTROL1);
406         dump_reg(sd, REG_LUMA_CONTROL2);
407         dump_reg(sd, REG_LUMA_CONTROL3);
408         dump_reg(sd, REG_BRIGHTNESS);
409         dump_reg(sd, REG_CONTRAST);
410         dump_reg(sd, REG_SATURATION);
411         dump_reg(sd, REG_HUE);
412         dump_reg(sd, REG_CHROMA_CONTROL1);
413         dump_reg(sd, REG_CHROMA_CONTROL2);
414         dump_reg(sd, REG_COMP_PR_SATURATION);
415         dump_reg(sd, REG_COMP_Y_CONTRAST);
416         dump_reg(sd, REG_COMP_PB_SATURATION);
417         dump_reg(sd, REG_COMP_Y_BRIGHTNESS);
418         dump_reg(sd, REG_AVID_START_PIXEL_LSB);
419         dump_reg(sd, REG_AVID_START_PIXEL_MSB);
420         dump_reg(sd, REG_AVID_STOP_PIXEL_LSB);
421         dump_reg(sd, REG_AVID_STOP_PIXEL_MSB);
422         dump_reg(sd, REG_HSYNC_START_PIXEL_LSB);
423         dump_reg(sd, REG_HSYNC_START_PIXEL_MSB);
424         dump_reg(sd, REG_HSYNC_STOP_PIXEL_LSB);
425         dump_reg(sd, REG_HSYNC_STOP_PIXEL_MSB);
426         dump_reg(sd, REG_VSYNC_START_LINE_LSB);
427         dump_reg(sd, REG_VSYNC_START_LINE_MSB);
428         dump_reg(sd, REG_VSYNC_STOP_LINE_LSB);
429         dump_reg(sd, REG_VSYNC_STOP_LINE_MSB);
430         dump_reg(sd, REG_VBLK_START_LINE_LSB);
431         dump_reg(sd, REG_VBLK_START_LINE_MSB);
432         dump_reg(sd, REG_VBLK_STOP_LINE_LSB);
433         dump_reg(sd, REG_VBLK_STOP_LINE_MSB);
434         dump_reg(sd, REG_SYNC_CONTROL);
435         dump_reg(sd, REG_OUTPUT_FORMATTER1);
436         dump_reg(sd, REG_OUTPUT_FORMATTER2);
437         dump_reg(sd, REG_OUTPUT_FORMATTER3);
438         dump_reg(sd, REG_OUTPUT_FORMATTER4);
439         dump_reg(sd, REG_OUTPUT_FORMATTER5);
440         dump_reg(sd, REG_OUTPUT_FORMATTER6);
441         dump_reg(sd, REG_CLEAR_LOST_LOCK);
442 }
443
444 /**
445  * tvp514x_configure() - Configure the TVP5146/47 registers
446  * @sd: ptr to v4l2_subdev struct
447  * @decoder: ptr to tvp514x_decoder structure
448  *
449  * Returns zero if successful, or non-zero otherwise.
450  */
451 static int tvp514x_configure(struct v4l2_subdev *sd,
452                 struct tvp514x_decoder *decoder)
453 {
454         int err;
455
456         /* common register initialization */
457         err =
458             tvp514x_write_regs(sd, decoder->tvp514x_regs);
459         if (err)
460                 return err;
461
462         if (debug)
463                 tvp514x_reg_dump(sd);
464
465         return 0;
466 }
467
468 /**
469  * tvp514x_detect() - Detect if an tvp514x is present, and if so which revision.
470  * @sd: pointer to standard V4L2 sub-device structure
471  * @decoder: pointer to tvp514x_decoder structure
472  *
473  * A device is considered to be detected if the chip ID (LSB and MSB)
474  * registers match the expected values.
475  * Any value of the rom version register is accepted.
476  * Returns ENODEV error number if no device is detected, or zero
477  * if a device is detected.
478  */
479 static int tvp514x_detect(struct v4l2_subdev *sd,
480                 struct tvp514x_decoder *decoder)
481 {
482         u8 chip_id_msb, chip_id_lsb, rom_ver;
483         struct i2c_client *client = v4l2_get_subdevdata(sd);
484
485         chip_id_msb = tvp514x_read_reg(sd, REG_CHIP_ID_MSB);
486         chip_id_lsb = tvp514x_read_reg(sd, REG_CHIP_ID_LSB);
487         rom_ver = tvp514x_read_reg(sd, REG_ROM_VERSION);
488
489         v4l2_dbg(1, debug, sd,
490                  "chip id detected msb:0x%x lsb:0x%x rom version:0x%x\n",
491                  chip_id_msb, chip_id_lsb, rom_ver);
492         if ((chip_id_msb != TVP514X_CHIP_ID_MSB)
493                 || ((chip_id_lsb != TVP5146_CHIP_ID_LSB)
494                 && (chip_id_lsb != TVP5147_CHIP_ID_LSB))) {
495                 /* We didn't read the values we expected, so this must not be
496                  * an TVP5146/47.
497                  */
498                 v4l2_err(sd, "chip id mismatch msb:0x%x lsb:0x%x\n",
499                                 chip_id_msb, chip_id_lsb);
500                 return -ENODEV;
501         }
502
503         decoder->ver = rom_ver;
504
505         v4l2_info(sd, "%s (Version - 0x%.2x) found at 0x%x (%s)\n",
506                         client->name, decoder->ver,
507                         client->addr << 1, client->adapter->name);
508         return 0;
509 }
510
511 /**
512  * tvp514x_querystd() - V4L2 decoder interface handler for querystd
513  * @sd: pointer to standard V4L2 sub-device structure
514  * @std_id: standard V4L2 std_id ioctl enum
515  *
516  * Returns the current standard detected by TVP5146/47. If no active input is
517  * detected then *std_id is set to 0 and the function returns 0.
518  */
519 static int tvp514x_querystd(struct v4l2_subdev *sd, v4l2_std_id *std_id)
520 {
521         struct tvp514x_decoder *decoder = to_decoder(sd);
522         enum tvp514x_std current_std;
523         enum tvp514x_input input_sel;
524         u8 sync_lock_status, lock_mask;
525
526         if (std_id == NULL)
527                 return -EINVAL;
528
529         *std_id = V4L2_STD_UNKNOWN;
530
531         /* query the current standard */
532         current_std = tvp514x_query_current_std(sd);
533         if (current_std == STD_INVALID)
534                 return 0;
535
536         input_sel = decoder->input;
537
538         switch (input_sel) {
539         case INPUT_CVBS_VI1A:
540         case INPUT_CVBS_VI1B:
541         case INPUT_CVBS_VI1C:
542         case INPUT_CVBS_VI2A:
543         case INPUT_CVBS_VI2B:
544         case INPUT_CVBS_VI2C:
545         case INPUT_CVBS_VI3A:
546         case INPUT_CVBS_VI3B:
547         case INPUT_CVBS_VI3C:
548         case INPUT_CVBS_VI4A:
549                 lock_mask = STATUS_CLR_SUBCAR_LOCK_BIT |
550                         STATUS_HORZ_SYNC_LOCK_BIT |
551                         STATUS_VIRT_SYNC_LOCK_BIT;
552                 break;
553
554         case INPUT_SVIDEO_VI2A_VI1A:
555         case INPUT_SVIDEO_VI2B_VI1B:
556         case INPUT_SVIDEO_VI2C_VI1C:
557         case INPUT_SVIDEO_VI2A_VI3A:
558         case INPUT_SVIDEO_VI2B_VI3B:
559         case INPUT_SVIDEO_VI2C_VI3C:
560         case INPUT_SVIDEO_VI4A_VI1A:
561         case INPUT_SVIDEO_VI4A_VI1B:
562         case INPUT_SVIDEO_VI4A_VI1C:
563         case INPUT_SVIDEO_VI4A_VI3A:
564         case INPUT_SVIDEO_VI4A_VI3B:
565         case INPUT_SVIDEO_VI4A_VI3C:
566                 lock_mask = STATUS_HORZ_SYNC_LOCK_BIT |
567                         STATUS_VIRT_SYNC_LOCK_BIT;
568                 break;
569                 /*Need to add other interfaces*/
570         default:
571                 return -EINVAL;
572         }
573         /* check whether signal is locked */
574         sync_lock_status = tvp514x_read_reg(sd, REG_STATUS1);
575         if (lock_mask != (sync_lock_status & lock_mask))
576                 return 0;       /* No input detected */
577
578         *std_id = decoder->std_list[current_std].standard.id;
579
580         v4l2_dbg(1, debug, sd, "Current STD: %s\n",
581                         decoder->std_list[current_std].standard.name);
582         return 0;
583 }
584
585 /**
586  * tvp514x_s_std() - V4L2 decoder interface handler for s_std
587  * @sd: pointer to standard V4L2 sub-device structure
588  * @std_id: standard V4L2 v4l2_std_id ioctl enum
589  *
590  * If std_id is supported, sets the requested standard. Otherwise, returns
591  * -EINVAL
592  */
593 static int tvp514x_s_std(struct v4l2_subdev *sd, v4l2_std_id std_id)
594 {
595         struct tvp514x_decoder *decoder = to_decoder(sd);
596         int err, i;
597
598         for (i = 0; i < decoder->num_stds; i++)
599                 if (std_id & decoder->std_list[i].standard.id)
600                         break;
601
602         if ((i == decoder->num_stds) || (i == STD_INVALID))
603                 return -EINVAL;
604
605         err = tvp514x_write_reg(sd, REG_VIDEO_STD,
606                                 decoder->std_list[i].video_std);
607         if (err)
608                 return err;
609
610         decoder->current_std = i;
611         decoder->tvp514x_regs[REG_VIDEO_STD].val =
612                 decoder->std_list[i].video_std;
613
614         v4l2_dbg(1, debug, sd, "Standard set to: %s",
615                         decoder->std_list[i].standard.name);
616         return 0;
617 }
618
619 /**
620  * tvp514x_s_routing() - V4L2 decoder interface handler for s_routing
621  * @sd: pointer to standard V4L2 sub-device structure
622  * @input: input selector for routing the signal
623  * @output: output selector for routing the signal
624  * @config: config value. Not used
625  *
626  * If index is valid, selects the requested input. Otherwise, returns -EINVAL if
627  * the input is not supported or there is no active signal present in the
628  * selected input.
629  */
630 static int tvp514x_s_routing(struct v4l2_subdev *sd,
631                                 u32 input, u32 output, u32 config)
632 {
633         struct tvp514x_decoder *decoder = to_decoder(sd);
634         int err;
635         enum tvp514x_input input_sel;
636         enum tvp514x_output output_sel;
637         u8 sync_lock_status, lock_mask;
638         int try_count = LOCK_RETRY_COUNT;
639
640         if ((input >= INPUT_INVALID) ||
641                         (output >= OUTPUT_INVALID))
642                 /* Index out of bound */
643                 return -EINVAL;
644
645         /*
646          * For the sequence streamon -> streamoff and again s_input
647          * it fails to lock the signal, since streamoff puts TVP514x
648          * into power off state which leads to failure in sub-sequent s_input.
649          *
650          * So power up the TVP514x device here, since it is important to lock
651          * the signal at this stage.
652          */
653         if (!decoder->streaming)
654                 tvp514x_s_stream(sd, 1);
655
656         input_sel = input;
657         output_sel = output;
658
659         err = tvp514x_write_reg(sd, REG_INPUT_SEL, input_sel);
660         if (err)
661                 return err;
662
663         output_sel |= tvp514x_read_reg(sd,
664                         REG_OUTPUT_FORMATTER1) & 0x7;
665         err = tvp514x_write_reg(sd, REG_OUTPUT_FORMATTER1,
666                         output_sel);
667         if (err)
668                 return err;
669
670         decoder->tvp514x_regs[REG_INPUT_SEL].val = input_sel;
671         decoder->tvp514x_regs[REG_OUTPUT_FORMATTER1].val = output_sel;
672
673         /* Clear status */
674         msleep(LOCK_RETRY_DELAY);
675         err =
676             tvp514x_write_reg(sd, REG_CLEAR_LOST_LOCK, 0x01);
677         if (err)
678                 return err;
679
680         switch (input_sel) {
681         case INPUT_CVBS_VI1A:
682         case INPUT_CVBS_VI1B:
683         case INPUT_CVBS_VI1C:
684         case INPUT_CVBS_VI2A:
685         case INPUT_CVBS_VI2B:
686         case INPUT_CVBS_VI2C:
687         case INPUT_CVBS_VI3A:
688         case INPUT_CVBS_VI3B:
689         case INPUT_CVBS_VI3C:
690         case INPUT_CVBS_VI4A:
691                 lock_mask = STATUS_CLR_SUBCAR_LOCK_BIT |
692                         STATUS_HORZ_SYNC_LOCK_BIT |
693                         STATUS_VIRT_SYNC_LOCK_BIT;
694                 break;
695
696         case INPUT_SVIDEO_VI2A_VI1A:
697         case INPUT_SVIDEO_VI2B_VI1B:
698         case INPUT_SVIDEO_VI2C_VI1C:
699         case INPUT_SVIDEO_VI2A_VI3A:
700         case INPUT_SVIDEO_VI2B_VI3B:
701         case INPUT_SVIDEO_VI2C_VI3C:
702         case INPUT_SVIDEO_VI4A_VI1A:
703         case INPUT_SVIDEO_VI4A_VI1B:
704         case INPUT_SVIDEO_VI4A_VI1C:
705         case INPUT_SVIDEO_VI4A_VI3A:
706         case INPUT_SVIDEO_VI4A_VI3B:
707         case INPUT_SVIDEO_VI4A_VI3C:
708                 lock_mask = STATUS_HORZ_SYNC_LOCK_BIT |
709                         STATUS_VIRT_SYNC_LOCK_BIT;
710                 break;
711         /* Need to add other interfaces*/
712         default:
713                 return -EINVAL;
714         }
715
716         while (try_count-- > 0) {
717                 /* Allow decoder to sync up with new input */
718                 msleep(LOCK_RETRY_DELAY);
719
720                 sync_lock_status = tvp514x_read_reg(sd,
721                                 REG_STATUS1);
722                 if (lock_mask == (sync_lock_status & lock_mask))
723                         /* Input detected */
724                         break;
725         }
726
727         if (try_count < 0)
728                 return -EINVAL;
729
730         decoder->input = input;
731         decoder->output = output;
732
733         v4l2_dbg(1, debug, sd, "Input set to: %d\n", input_sel);
734
735         return 0;
736 }
737
738 /**
739  * tvp514x_queryctrl() - V4L2 decoder interface handler for queryctrl
740  * @sd: pointer to standard V4L2 sub-device structure
741  * @qctrl: standard V4L2 v4l2_queryctrl structure
742  *
743  * If the requested control is supported, returns the control information.
744  * Otherwise, returns -EINVAL if the control is not supported.
745  */
746 static int
747 tvp514x_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qctrl)
748 {
749         int err = -EINVAL;
750
751         if (qctrl == NULL)
752                 return err;
753
754         switch (qctrl->id) {
755         case V4L2_CID_BRIGHTNESS:
756                 /* Brightness supported is (0-255), */
757                 err = v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 128);
758                 break;
759         case V4L2_CID_CONTRAST:
760         case V4L2_CID_SATURATION:
761                 /**
762                  * Saturation and Contrast supported is -
763                  *      Contrast: 0 - 255 (Default - 128)
764                  *      Saturation: 0 - 255 (Default - 128)
765                  */
766                 err = v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 128);
767                 break;
768         case V4L2_CID_HUE:
769                 /* Hue Supported is -
770                  *      Hue - -180 - +180 (Default - 0, Step - +180)
771                  */
772                 err = v4l2_ctrl_query_fill(qctrl, -180, 180, 180, 0);
773                 break;
774         case V4L2_CID_AUTOGAIN:
775                 /**
776                  * Auto Gain supported is -
777                  *      0 - 1 (Default - 1)
778                  */
779                 err = v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 1);
780                 break;
781         default:
782                 v4l2_err(sd, "invalid control id %d\n", qctrl->id);
783                 return err;
784         }
785
786         v4l2_dbg(1, debug, sd, "Query Control:%s: Min - %d, Max - %d, Def - %d",
787                         qctrl->name, qctrl->minimum, qctrl->maximum,
788                         qctrl->default_value);
789
790         return err;
791 }
792
793 /**
794  * tvp514x_g_ctrl() - V4L2 decoder interface handler for g_ctrl
795  * @sd: pointer to standard V4L2 sub-device structure
796  * @ctrl: pointer to v4l2_control structure
797  *
798  * If the requested control is supported, returns the control's current
799  * value from the decoder. Otherwise, returns -EINVAL if the control is not
800  * supported.
801  */
802 static int
803 tvp514x_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
804 {
805         struct tvp514x_decoder *decoder = to_decoder(sd);
806
807         if (ctrl == NULL)
808                 return -EINVAL;
809
810         switch (ctrl->id) {
811         case V4L2_CID_BRIGHTNESS:
812                 ctrl->value = decoder->tvp514x_regs[REG_BRIGHTNESS].val;
813                 break;
814         case V4L2_CID_CONTRAST:
815                 ctrl->value = decoder->tvp514x_regs[REG_CONTRAST].val;
816                 break;
817         case V4L2_CID_SATURATION:
818                 ctrl->value = decoder->tvp514x_regs[REG_SATURATION].val;
819                 break;
820         case V4L2_CID_HUE:
821                 ctrl->value = decoder->tvp514x_regs[REG_HUE].val;
822                 if (ctrl->value == 0x7F)
823                         ctrl->value = 180;
824                 else if (ctrl->value == 0x80)
825                         ctrl->value = -180;
826                 else
827                         ctrl->value = 0;
828
829                 break;
830         case V4L2_CID_AUTOGAIN:
831                 ctrl->value = decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val;
832                 if ((ctrl->value & 0x3) == 3)
833                         ctrl->value = 1;
834                 else
835                         ctrl->value = 0;
836
837                 break;
838         default:
839                 v4l2_err(sd, "invalid control id %d\n", ctrl->id);
840                 return -EINVAL;
841         }
842
843         v4l2_dbg(1, debug, sd, "Get Control: ID - %d - %d",
844                         ctrl->id, ctrl->value);
845         return 0;
846 }
847
848 /**
849  * tvp514x_s_ctrl() - V4L2 decoder interface handler for s_ctrl
850  * @sd: pointer to standard V4L2 sub-device structure
851  * @ctrl: pointer to v4l2_control structure
852  *
853  * If the requested control is supported, sets the control's current
854  * value in HW. Otherwise, returns -EINVAL if the control is not supported.
855  */
856 static int
857 tvp514x_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
858 {
859         struct tvp514x_decoder *decoder = to_decoder(sd);
860         int err = -EINVAL, value;
861
862         if (ctrl == NULL)
863                 return err;
864
865         value = ctrl->value;
866
867         switch (ctrl->id) {
868         case V4L2_CID_BRIGHTNESS:
869                 if (ctrl->value < 0 || ctrl->value > 255) {
870                         v4l2_err(sd, "invalid brightness setting %d\n",
871                                         ctrl->value);
872                         return -ERANGE;
873                 }
874                 err = tvp514x_write_reg(sd, REG_BRIGHTNESS,
875                                 value);
876                 if (err)
877                         return err;
878
879                 decoder->tvp514x_regs[REG_BRIGHTNESS].val = value;
880                 break;
881         case V4L2_CID_CONTRAST:
882                 if (ctrl->value < 0 || ctrl->value > 255) {
883                         v4l2_err(sd, "invalid contrast setting %d\n",
884                                         ctrl->value);
885                         return -ERANGE;
886                 }
887                 err = tvp514x_write_reg(sd, REG_CONTRAST, value);
888                 if (err)
889                         return err;
890
891                 decoder->tvp514x_regs[REG_CONTRAST].val = value;
892                 break;
893         case V4L2_CID_SATURATION:
894                 if (ctrl->value < 0 || ctrl->value > 255) {
895                         v4l2_err(sd, "invalid saturation setting %d\n",
896                                         ctrl->value);
897                         return -ERANGE;
898                 }
899                 err = tvp514x_write_reg(sd, REG_SATURATION, value);
900                 if (err)
901                         return err;
902
903                 decoder->tvp514x_regs[REG_SATURATION].val = value;
904                 break;
905         case V4L2_CID_HUE:
906                 if (value == 180)
907                         value = 0x7F;
908                 else if (value == -180)
909                         value = 0x80;
910                 else if (value == 0)
911                         value = 0;
912                 else {
913                         v4l2_err(sd, "invalid hue setting %d\n", ctrl->value);
914                         return -ERANGE;
915                 }
916                 err = tvp514x_write_reg(sd, REG_HUE, value);
917                 if (err)
918                         return err;
919
920                 decoder->tvp514x_regs[REG_HUE].val = value;
921                 break;
922         case V4L2_CID_AUTOGAIN:
923                 if (value == 1)
924                         value = 0x0F;
925                 else if (value == 0)
926                         value = 0x0C;
927                 else {
928                         v4l2_err(sd, "invalid auto gain setting %d\n",
929                                         ctrl->value);
930                         return -ERANGE;
931                 }
932                 err = tvp514x_write_reg(sd, REG_AFE_GAIN_CTRL, value);
933                 if (err)
934                         return err;
935
936                 decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val = value;
937                 break;
938         default:
939                 v4l2_err(sd, "invalid control id %d\n", ctrl->id);
940                 return err;
941         }
942
943         v4l2_dbg(1, debug, sd, "Set Control: ID - %d - %d",
944                         ctrl->id, ctrl->value);
945
946         return err;
947 }
948
949 /**
950  * tvp514x_enum_fmt_cap() - V4L2 decoder interface handler for enum_fmt
951  * @sd: pointer to standard V4L2 sub-device structure
952  * @fmt: standard V4L2 VIDIOC_ENUM_FMT ioctl structure
953  *
954  * Implement the VIDIOC_ENUM_FMT ioctl to enumerate supported formats
955  */
956 static int
957 tvp514x_enum_fmt_cap(struct v4l2_subdev *sd, struct v4l2_fmtdesc *fmt)
958 {
959         if (fmt == NULL || fmt->index)
960                 return -EINVAL;
961
962         if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
963                 /* only capture is supported */
964                 return -EINVAL;
965
966         /* only one format */
967         fmt->flags = 0;
968         strlcpy(fmt->description, "8-bit UYVY 4:2:2 Format",
969                                         sizeof(fmt->description));
970         fmt->pixelformat = V4L2_PIX_FMT_UYVY;
971         return 0;
972 }
973
974 /**
975  * tvp514x_try_fmt_cap() - V4L2 decoder interface handler for try_fmt
976  * @sd: pointer to standard V4L2 sub-device structure
977  * @f: pointer to standard V4L2 VIDIOC_TRY_FMT ioctl structure
978  *
979  * Implement the VIDIOC_TRY_FMT ioctl for the CAPTURE buffer type. This
980  * ioctl is used to negotiate the image capture size and pixel format
981  * without actually making it take effect.
982  */
983 static int
984 tvp514x_try_fmt_cap(struct v4l2_subdev *sd, struct v4l2_format *f)
985 {
986         struct tvp514x_decoder *decoder = to_decoder(sd);
987         struct v4l2_pix_format *pix;
988         enum tvp514x_std current_std;
989
990         if (f == NULL)
991                 return -EINVAL;
992
993         if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
994                 /* only capture is supported */
995                 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
996
997         pix = &f->fmt.pix;
998
999         /* Calculate height and width based on current standard */
1000         current_std = decoder->current_std;
1001
1002         pix->pixelformat = V4L2_PIX_FMT_UYVY;
1003         pix->width = decoder->std_list[current_std].width;
1004         pix->height = decoder->std_list[current_std].height;
1005         pix->field = V4L2_FIELD_INTERLACED;
1006         pix->bytesperline = pix->width * 2;
1007         pix->sizeimage = pix->bytesperline * pix->height;
1008         pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
1009         pix->priv = 0;
1010
1011         v4l2_dbg(1, debug, sd, "Try FMT: bytesperline - %d"
1012                         "Width - %d, Height - %d",
1013                         pix->bytesperline,
1014                         pix->width, pix->height);
1015         return 0;
1016 }
1017
1018 /**
1019  * tvp514x_s_fmt_cap() - V4L2 decoder interface handler for s_fmt
1020  * @sd: pointer to standard V4L2 sub-device structure
1021  * @f: pointer to standard V4L2 VIDIOC_S_FMT ioctl structure
1022  *
1023  * If the requested format is supported, configures the HW to use that
1024  * format, returns error code if format not supported or HW can't be
1025  * correctly configured.
1026  */
1027 static int
1028 tvp514x_s_fmt_cap(struct v4l2_subdev *sd, struct v4l2_format *f)
1029 {
1030         struct tvp514x_decoder *decoder = to_decoder(sd);
1031         struct v4l2_pix_format *pix;
1032         int rval;
1033
1034         if (f == NULL)
1035                 return -EINVAL;
1036
1037         if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1038                 /* only capture is supported */
1039                 return -EINVAL;
1040
1041         pix = &f->fmt.pix;
1042         rval = tvp514x_try_fmt_cap(sd, f);
1043         if (rval)
1044                 return rval;
1045
1046                 decoder->pix = *pix;
1047
1048         return rval;
1049 }
1050
1051 /**
1052  * tvp514x_g_fmt_cap() - V4L2 decoder interface handler for tvp514x_g_fmt_cap
1053  * @sd: pointer to standard V4L2 sub-device structure
1054  * @f: pointer to standard V4L2 v4l2_format structure
1055  *
1056  * Returns the decoder's current pixel format in the v4l2_format
1057  * parameter.
1058  */
1059 static int
1060 tvp514x_g_fmt_cap(struct v4l2_subdev *sd, struct v4l2_format *f)
1061 {
1062         struct tvp514x_decoder *decoder = to_decoder(sd);
1063
1064         if (f == NULL)
1065                 return -EINVAL;
1066
1067         if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1068                 /* only capture is supported */
1069                 return -EINVAL;
1070
1071         f->fmt.pix = decoder->pix;
1072
1073         v4l2_dbg(1, debug, sd, "Current FMT: bytesperline - %d"
1074                         "Width - %d, Height - %d",
1075                         decoder->pix.bytesperline,
1076                         decoder->pix.width, decoder->pix.height);
1077         return 0;
1078 }
1079
1080 /**
1081  * tvp514x_g_parm() - V4L2 decoder interface handler for g_parm
1082  * @sd: pointer to standard V4L2 sub-device structure
1083  * @a: pointer to standard V4L2 VIDIOC_G_PARM ioctl structure
1084  *
1085  * Returns the decoder's video CAPTURE parameters.
1086  */
1087 static int
1088 tvp514x_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
1089 {
1090         struct tvp514x_decoder *decoder = to_decoder(sd);
1091         struct v4l2_captureparm *cparm;
1092         enum tvp514x_std current_std;
1093
1094         if (a == NULL)
1095                 return -EINVAL;
1096
1097         if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1098                 /* only capture is supported */
1099                 return -EINVAL;
1100
1101         /* get the current standard */
1102         current_std = decoder->current_std;
1103
1104         cparm = &a->parm.capture;
1105         cparm->capability = V4L2_CAP_TIMEPERFRAME;
1106         cparm->timeperframe =
1107                 decoder->std_list[current_std].standard.frameperiod;
1108
1109         return 0;
1110 }
1111
1112 /**
1113  * tvp514x_s_parm() - V4L2 decoder interface handler for s_parm
1114  * @sd: pointer to standard V4L2 sub-device structure
1115  * @a: pointer to standard V4L2 VIDIOC_S_PARM ioctl structure
1116  *
1117  * Configures the decoder to use the input parameters, if possible. If
1118  * not possible, returns the appropriate error code.
1119  */
1120 static int
1121 tvp514x_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
1122 {
1123         struct tvp514x_decoder *decoder = to_decoder(sd);
1124         struct v4l2_fract *timeperframe;
1125         enum tvp514x_std current_std;
1126
1127         if (a == NULL)
1128                 return -EINVAL;
1129
1130         if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1131                 /* only capture is supported */
1132                 return -EINVAL;
1133
1134         timeperframe = &a->parm.capture.timeperframe;
1135
1136         /* get the current standard */
1137         current_std = decoder->current_std;
1138
1139         *timeperframe =
1140             decoder->std_list[current_std].standard.frameperiod;
1141
1142         return 0;
1143 }
1144
1145 /**
1146  * tvp514x_s_stream() - V4L2 decoder i/f handler for s_stream
1147  * @sd: pointer to standard V4L2 sub-device structure
1148  * @enable: streaming enable or disable
1149  *
1150  * Sets streaming to enable or disable, if possible.
1151  */
1152 static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable)
1153 {
1154         int err = 0;
1155         struct i2c_client *client = v4l2_get_subdevdata(sd);
1156         struct tvp514x_decoder *decoder = to_decoder(sd);
1157
1158         if (decoder->streaming == enable)
1159                 return 0;
1160
1161         switch (enable) {
1162         case 0:
1163         {
1164                 /* Power Down Sequence */
1165                 err = tvp514x_write_reg(sd, REG_OPERATION_MODE, 0x01);
1166                 if (err) {
1167                         v4l2_err(sd, "Unable to turn off decoder\n");
1168                         return err;
1169                 }
1170                 decoder->streaming = enable;
1171                 break;
1172         }
1173         case 1:
1174         {
1175                 struct tvp514x_reg *int_seq = (struct tvp514x_reg *)
1176                                 client->driver->id_table->driver_data;
1177
1178                 /* Power Up Sequence */
1179                 err = tvp514x_write_regs(sd, int_seq);
1180                 if (err) {
1181                         v4l2_err(sd, "Unable to turn on decoder\n");
1182                         return err;
1183                 }
1184                 /* Detect if not already detected */
1185                 err = tvp514x_detect(sd, decoder);
1186                 if (err) {
1187                         v4l2_err(sd, "Unable to detect decoder\n");
1188                         return err;
1189                 }
1190                 err = tvp514x_configure(sd, decoder);
1191                 if (err) {
1192                         v4l2_err(sd, "Unable to configure decoder\n");
1193                         return err;
1194                 }
1195                 decoder->streaming = enable;
1196                 break;
1197         }
1198         default:
1199                 err = -ENODEV;
1200                 break;
1201         }
1202
1203         return err;
1204 }
1205
1206 static const struct v4l2_subdev_core_ops tvp514x_core_ops = {
1207         .queryctrl = tvp514x_queryctrl,
1208         .g_ctrl = tvp514x_g_ctrl,
1209         .s_ctrl = tvp514x_s_ctrl,
1210         .s_std = tvp514x_s_std,
1211 };
1212
1213 static const struct v4l2_subdev_video_ops tvp514x_video_ops = {
1214         .s_routing = tvp514x_s_routing,
1215         .querystd = tvp514x_querystd,
1216         .enum_fmt = tvp514x_enum_fmt_cap,
1217         .g_fmt = tvp514x_g_fmt_cap,
1218         .try_fmt = tvp514x_try_fmt_cap,
1219         .s_fmt = tvp514x_s_fmt_cap,
1220         .g_parm = tvp514x_g_parm,
1221         .s_parm = tvp514x_s_parm,
1222         .s_stream = tvp514x_s_stream,
1223 };
1224
1225 static const struct v4l2_subdev_ops tvp514x_ops = {
1226         .core = &tvp514x_core_ops,
1227         .video = &tvp514x_video_ops,
1228 };
1229
1230 static struct tvp514x_decoder tvp514x_dev = {
1231         .streaming = 0,
1232
1233         .pix = {
1234                 /* Default to NTSC 8-bit YUV 422 */
1235                 .width = NTSC_NUM_ACTIVE_PIXELS,
1236                 .height = NTSC_NUM_ACTIVE_LINES,
1237                 .pixelformat = V4L2_PIX_FMT_UYVY,
1238                 .field = V4L2_FIELD_INTERLACED,
1239                 .bytesperline = NTSC_NUM_ACTIVE_PIXELS * 2,
1240                 .sizeimage =
1241                 NTSC_NUM_ACTIVE_PIXELS * 2 * NTSC_NUM_ACTIVE_LINES,
1242                 .colorspace = V4L2_COLORSPACE_SMPTE170M,
1243                 },
1244
1245         .current_std = STD_NTSC_MJ,
1246         .std_list = tvp514x_std_list,
1247         .num_stds = ARRAY_SIZE(tvp514x_std_list),
1248
1249 };
1250
1251 /**
1252  * tvp514x_probe() - decoder driver i2c probe handler
1253  * @client: i2c driver client device structure
1254  * @id: i2c driver id table
1255  *
1256  * Register decoder as an i2c client device and V4L2
1257  * device.
1258  */
1259 static int
1260 tvp514x_probe(struct i2c_client *client, const struct i2c_device_id *id)
1261 {
1262         struct tvp514x_decoder *decoder;
1263         struct v4l2_subdev *sd;
1264
1265         /* Check if the adapter supports the needed features */
1266         if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1267                 return -EIO;
1268
1269         if (!client->dev.platform_data) {
1270                 v4l2_err(client, "No platform data!!\n");
1271                 return -ENODEV;
1272         }
1273
1274         decoder = kzalloc(sizeof(*decoder), GFP_KERNEL);
1275         if (!decoder)
1276                 return -ENOMEM;
1277
1278         /* Initialize the tvp514x_decoder with default configuration */
1279         *decoder = tvp514x_dev;
1280         /* Copy default register configuration */
1281         memcpy(decoder->tvp514x_regs, tvp514x_reg_list_default,
1282                         sizeof(tvp514x_reg_list_default));
1283
1284         /* Copy board specific information here */
1285         decoder->pdata = client->dev.platform_data;
1286
1287         /**
1288          * Fetch platform specific data, and configure the
1289          * tvp514x_reg_list[] accordingly. Since this is one
1290          * time configuration, no need to preserve.
1291          */
1292         decoder->tvp514x_regs[REG_OUTPUT_FORMATTER2].val |=
1293                 (decoder->pdata->clk_polarity << 1);
1294         decoder->tvp514x_regs[REG_SYNC_CONTROL].val |=
1295                 ((decoder->pdata->hs_polarity << 2) |
1296                  (decoder->pdata->vs_polarity << 3));
1297         /* Set default standard to auto */
1298         decoder->tvp514x_regs[REG_VIDEO_STD].val =
1299                 VIDEO_STD_AUTO_SWITCH_BIT;
1300
1301         /* Register with V4L2 layer as slave device */
1302         sd = &decoder->sd;
1303         v4l2_i2c_subdev_init(sd, client, &tvp514x_ops);
1304
1305         v4l2_info(sd, "%s decoder driver registered !!\n", sd->name);
1306
1307         return 0;
1308
1309 }
1310
1311 /**
1312  * tvp514x_remove() - decoder driver i2c remove handler
1313  * @client: i2c driver client device structure
1314  *
1315  * Unregister decoder as an i2c client device and V4L2
1316  * device. Complement of tvp514x_probe().
1317  */
1318 static int tvp514x_remove(struct i2c_client *client)
1319 {
1320         struct v4l2_subdev *sd = i2c_get_clientdata(client);
1321         struct tvp514x_decoder *decoder = to_decoder(sd);
1322
1323         v4l2_device_unregister_subdev(sd);
1324         kfree(decoder);
1325         return 0;
1326 }
1327 /* TVP5146 Init/Power on Sequence */
1328 static const struct tvp514x_reg tvp5146_init_reg_seq[] = {
1329         {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
1330         {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1331         {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
1332         {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1333         {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1334         {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1335         {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1336         {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1337         {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
1338         {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1339         {TOK_WRITE, REG_OPERATION_MODE, 0x00},
1340         {TOK_TERM, 0, 0},
1341 };
1342
1343 /* TVP5147 Init/Power on Sequence */
1344 static const struct tvp514x_reg tvp5147_init_reg_seq[] =        {
1345         {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
1346         {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1347         {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
1348         {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1349         {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1350         {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1351         {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1352         {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1353         {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x16},
1354         {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1355         {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xA0},
1356         {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x16},
1357         {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1358         {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1359         {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1360         {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
1361         {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1362         {TOK_WRITE, REG_OPERATION_MODE, 0x00},
1363         {TOK_TERM, 0, 0},
1364 };
1365
1366 /* TVP5146M2/TVP5147M1 Init/Power on Sequence */
1367 static const struct tvp514x_reg tvp514xm_init_reg_seq[] = {
1368         {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1369         {TOK_WRITE, REG_OPERATION_MODE, 0x00},
1370         {TOK_TERM, 0, 0},
1371 };
1372
1373 /**
1374  * I2C Device Table -
1375  *
1376  * name - Name of the actual device/chip.
1377  * driver_data - Driver data
1378  */
1379 static const struct i2c_device_id tvp514x_id[] = {
1380         {"tvp5146", (unsigned long)tvp5146_init_reg_seq},
1381         {"tvp5146m2", (unsigned long)tvp514xm_init_reg_seq},
1382         {"tvp5147", (unsigned long)tvp5147_init_reg_seq},
1383         {"tvp5147m1", (unsigned long)tvp514xm_init_reg_seq},
1384         {},
1385 };
1386
1387 MODULE_DEVICE_TABLE(i2c, tvp514x_id);
1388
1389 static struct i2c_driver tvp514x_driver = {
1390         .driver = {
1391                 .owner = THIS_MODULE,
1392                 .name = TVP514X_MODULE_NAME,
1393         },
1394         .probe = tvp514x_probe,
1395         .remove = tvp514x_remove,
1396         .id_table = tvp514x_id,
1397 };
1398
1399 static int __init tvp514x_init(void)
1400 {
1401         return i2c_add_driver(&tvp514x_driver);
1402 }
1403
1404 static void __exit tvp514x_exit(void)
1405 {
1406         i2c_del_driver(&tvp514x_driver);
1407 }
1408
1409 module_init(tvp514x_init);
1410 module_exit(tvp514x_exit);