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