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