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