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