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