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