Full support for Ginger Console
[linux-ginger.git] / drivers / media / video / tvp514x.c
blobc9797ee040f8582ed695db5cf9df1c407150ec20
1 /*
2 * drivers/media/video/tvp514x.c
4 * TI TVP5146/47 decoder driver
6 * Copyright (C) 2008 Texas Instruments Inc
7 * Author: Vaibhav Hiremath <hvaibhav@ti.com>
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>
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.
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.
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.
31 #include <linux/i2c.h>
32 #include <linux/delay.h>
33 #include <linux/videodev2.h>
35 #include <media/v4l2-device.h>
36 #include <media/v4l2-common.h>
37 #include <media/v4l2-chip-ident.h>
38 #include <media/tvp514x.h>
40 #include "tvp514x_regs.h"
42 /* Module Name */
43 #define TVP514X_MODULE_NAME "tvp514x"
45 /* Private macros for TVP */
46 #define I2C_RETRY_COUNT (5)
47 #define LOCK_RETRY_COUNT (5)
48 #define LOCK_RETRY_DELAY (200)
50 /* Debug functions */
51 static int debug;
52 module_param(debug, bool, 0644);
53 MODULE_PARM_DESC(debug, "Debug level (0-1)");
55 MODULE_AUTHOR("Texas Instruments");
56 MODULE_DESCRIPTION("TVP514X linux decoder driver");
57 MODULE_LICENSE("GPL");
59 /* enum tvp514x_std - enum for supported standards */
60 enum tvp514x_std {
61 STD_NTSC_MJ = 0,
62 STD_PAL_BDGHIN,
63 STD_INVALID
66 /**
67 * struct tvp514x_std_info - Structure to store standard informations
68 * @width: Line width in pixels
69 * @height:Number of active lines
70 * @video_std: Value to write in REG_VIDEO_STD register
71 * @standard: v4l2 standard structure information
73 struct tvp514x_std_info {
74 unsigned long width;
75 unsigned long height;
76 u8 video_std;
77 struct v4l2_standard standard;
80 static struct tvp514x_reg tvp514x_reg_list_default[0x40];
81 /**
82 * struct tvp514x_decoder - TVP5146/47 decoder object
83 * @sd: Subdevice Slave handle
84 * @tvp514x_regs: copy of hw's regs with preset values.
85 * @pdata: Board specific
86 * @ver: Chip version
87 * @streaming: TVP5146/47 decoder streaming - enabled or disabled.
88 * @pix: Current pixel format
89 * @num_fmts: Number of formats
90 * @fmt_list: Format list
91 * @current_std: Current standard
92 * @num_stds: Number of standards
93 * @std_list: Standards list
94 * @input: Input routing at chip level
95 * @output: Output routing at chip level
97 struct tvp514x_decoder {
98 struct v4l2_subdev sd;
99 struct tvp514x_reg tvp514x_regs[ARRAY_SIZE(tvp514x_reg_list_default)];
100 const struct tvp514x_platform_data *pdata;
102 int ver;
103 int streaming;
105 struct v4l2_pix_format pix;
106 int num_fmts;
107 const struct v4l2_fmtdesc *fmt_list;
109 enum tvp514x_std current_std;
110 int num_stds;
111 struct tvp514x_std_info *std_list;
112 /* Input and Output Routing parameters */
113 u32 input;
114 u32 output;
117 /* TVP514x default register values */
118 static struct tvp514x_reg tvp514x_reg_list_default[] = {
119 /* Composite selected */
120 {TOK_WRITE, REG_INPUT_SEL, 0x05},
121 {TOK_WRITE, REG_AFE_GAIN_CTRL, 0x0F},
122 /* Auto mode */
123 {TOK_WRITE, REG_VIDEO_STD, 0x00},
124 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
125 {TOK_SKIP, REG_AUTOSWITCH_MASK, 0x3F},
126 {TOK_WRITE, REG_COLOR_KILLER, 0x10},
127 {TOK_WRITE, REG_LUMA_CONTROL1, 0x00},
128 {TOK_WRITE, REG_LUMA_CONTROL2, 0x00},
129 {TOK_WRITE, REG_LUMA_CONTROL3, 0x02},
130 {TOK_WRITE, REG_BRIGHTNESS, 0x80},
131 {TOK_WRITE, REG_CONTRAST, 0x80},
132 {TOK_WRITE, REG_SATURATION, 0x80},
133 {TOK_WRITE, REG_HUE, 0x00},
134 {TOK_WRITE, REG_CHROMA_CONTROL1, 0x00},
135 {TOK_WRITE, REG_CHROMA_CONTROL2, 0x0E},
136 /* Reserved */
137 {TOK_SKIP, 0x0F, 0x00},
138 {TOK_WRITE, REG_COMP_PR_SATURATION, 0x80},
139 {TOK_WRITE, REG_COMP_Y_CONTRAST, 0x80},
140 {TOK_WRITE, REG_COMP_PB_SATURATION, 0x80},
141 /* Reserved */
142 {TOK_SKIP, 0x13, 0x00},
143 {TOK_WRITE, REG_COMP_Y_BRIGHTNESS, 0x80},
144 /* Reserved */
145 {TOK_SKIP, 0x15, 0x00},
146 /* NTSC timing */
147 {TOK_SKIP, REG_AVID_START_PIXEL_LSB, 0x55},
148 {TOK_SKIP, REG_AVID_START_PIXEL_MSB, 0x00},
149 {TOK_SKIP, REG_AVID_STOP_PIXEL_LSB, 0x25},
150 {TOK_SKIP, REG_AVID_STOP_PIXEL_MSB, 0x03},
151 /* NTSC timing */
152 {TOK_SKIP, REG_HSYNC_START_PIXEL_LSB, 0x00},
153 {TOK_SKIP, REG_HSYNC_START_PIXEL_MSB, 0x00},
154 {TOK_SKIP, REG_HSYNC_STOP_PIXEL_LSB, 0x40},
155 {TOK_SKIP, REG_HSYNC_STOP_PIXEL_MSB, 0x00},
156 /* NTSC timing */
157 {TOK_SKIP, REG_VSYNC_START_LINE_LSB, 0x04},
158 {TOK_SKIP, REG_VSYNC_START_LINE_MSB, 0x00},
159 {TOK_SKIP, REG_VSYNC_STOP_LINE_LSB, 0x07},
160 {TOK_SKIP, REG_VSYNC_STOP_LINE_MSB, 0x00},
161 /* NTSC timing */
162 {TOK_SKIP, REG_VBLK_START_LINE_LSB, 0x01},
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 /* Reserved */
167 {TOK_SKIP, 0x26, 0x00},
168 /* Reserved */
169 {TOK_SKIP, 0x27, 0x00},
170 {TOK_SKIP, REG_FAST_SWTICH_CONTROL, 0xCC},
171 /* Reserved */
172 {TOK_SKIP, 0x29, 0x00},
173 {TOK_SKIP, REG_FAST_SWTICH_SCART_DELAY, 0x00},
174 /* Reserved */
175 {TOK_SKIP, 0x2B, 0x00},
176 {TOK_SKIP, REG_SCART_DELAY, 0x00},
177 {TOK_SKIP, REG_CTI_DELAY, 0x00},
178 {TOK_SKIP, REG_CTI_CONTROL, 0x00},
179 /* Reserved */
180 {TOK_SKIP, 0x2F, 0x00},
181 /* Reserved */
182 {TOK_SKIP, 0x30, 0x00},
183 /* Reserved */
184 {TOK_SKIP, 0x31, 0x00},
185 /* HS, VS active high */
186 {TOK_WRITE, REG_SYNC_CONTROL, 0x00},
187 /* 10-bit BT.656 */
188 {TOK_WRITE, REG_OUTPUT_FORMATTER1, 0x00},
189 /* Enable clk & data */
190 {TOK_WRITE, REG_OUTPUT_FORMATTER2, 0x11},
191 /* Enable AVID & FLD */
192 {TOK_WRITE, REG_OUTPUT_FORMATTER3, 0xEE},
193 /* Enable VS & HS */
194 {TOK_WRITE, REG_OUTPUT_FORMATTER4, 0xAF},
195 {TOK_WRITE, REG_OUTPUT_FORMATTER5, 0xFF},
196 {TOK_WRITE, REG_OUTPUT_FORMATTER6, 0xFF},
197 /* Clear status */
198 {TOK_WRITE, REG_CLEAR_LOST_LOCK, 0x01},
199 {TOK_TERM, 0, 0},
203 * List of image formats supported by TVP5146/47 decoder
204 * Currently we are using 8 bit mode only, but can be
205 * extended to 10/20 bit mode.
207 static const struct v4l2_fmtdesc tvp514x_fmt_list[] = {
209 .index = 0,
210 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
211 .flags = 0,
212 .description = "8-bit UYVY 4:2:2 Format",
213 .pixelformat = V4L2_PIX_FMT_UYVY,
216 .index = 1,
217 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
218 .flags = 0,
219 .description = "8-bit YUYV 4:2:2 Format",
220 .pixelformat = V4L2_PIX_FMT_YUYV,
225 * Supported standards -
227 * Currently supports two standards only, need to add support for rest of the
228 * modes, like SECAM, etc...
230 static struct tvp514x_std_info tvp514x_std_list[] = {
231 /* Standard: STD_NTSC_MJ */
232 [STD_NTSC_MJ] = {
233 .width = NTSC_NUM_ACTIVE_PIXELS,
234 .height = NTSC_NUM_ACTIVE_LINES,
235 .video_std = VIDEO_STD_NTSC_MJ_BIT,
236 .standard = {
237 .index = 0,
238 .id = V4L2_STD_NTSC,
239 .name = "NTSC",
240 .frameperiod = {1001, 30000},
241 .framelines = 525
243 /* Standard: STD_PAL_BDGHIN */
245 [STD_PAL_BDGHIN] = {
246 .width = PAL_NUM_ACTIVE_PIXELS,
247 .height = PAL_NUM_ACTIVE_LINES,
248 .video_std = VIDEO_STD_PAL_BDGHIN_BIT,
249 .standard = {
250 .index = 1,
251 .id = V4L2_STD_PAL,
252 .name = "PAL",
253 .frameperiod = {1, 25},
254 .framelines = 625
257 /* Standard: need to add for additional standard */
261 static inline struct tvp514x_decoder *to_decoder(struct v4l2_subdev *sd)
263 return container_of(sd, struct tvp514x_decoder, sd);
268 * tvp514x_read_reg() - Read a value from a register in an TVP5146/47.
269 * @sd: ptr to v4l2_subdev struct
270 * @reg: TVP5146/47 register address
272 * Returns value read if successful, or non-zero (-1) otherwise.
274 static int tvp514x_read_reg(struct v4l2_subdev *sd, u8 reg)
276 int err, retry = 0;
277 struct i2c_client *client = v4l2_get_subdevdata(sd);
279 read_again:
281 err = i2c_smbus_read_byte_data(client, reg);
282 if (err == -1) {
283 if (retry <= I2C_RETRY_COUNT) {
284 v4l2_warn(sd, "Read: retry ... %d\n", retry);
285 retry++;
286 msleep_interruptible(10);
287 goto read_again;
291 return err;
295 * dump_reg() - dump the register content of TVP5146/47.
296 * @sd: ptr to v4l2_subdev struct
297 * @reg: TVP5146/47 register address
299 static void dump_reg(struct v4l2_subdev *sd, u8 reg)
301 u32 val;
303 val = tvp514x_read_reg(sd, reg);
304 v4l2_info(sd, "Reg(0x%.2X): 0x%.2X\n", reg, val);
308 * tvp514x_write_reg() - Write a value to a register in TVP5146/47
309 * @sd: ptr to v4l2_subdev struct
310 * @reg: TVP5146/47 register address
311 * @val: value to be written to the register
313 * Write a value to a register in an TVP5146/47 decoder device.
314 * Returns zero if successful, or non-zero otherwise.
316 static int tvp514x_write_reg(struct v4l2_subdev *sd, u8 reg, u8 val)
318 int err, retry = 0;
319 struct i2c_client *client = v4l2_get_subdevdata(sd);
321 write_again:
323 err = i2c_smbus_write_byte_data(client, reg, val);
324 if (err) {
325 if (retry <= I2C_RETRY_COUNT) {
326 v4l2_warn(sd, "Write: retry ... %d\n", retry);
327 retry++;
328 msleep_interruptible(10);
329 goto write_again;
333 return err;
337 * tvp514x_write_regs() : Initializes a list of TVP5146/47 registers
338 * @sd: ptr to v4l2_subdev struct
339 * @reglist: list of TVP5146/47 registers and values
341 * Initializes a list of TVP5146/47 registers:-
342 * if token is TOK_TERM, then entire write operation terminates
343 * if token is TOK_DELAY, then a delay of 'val' msec is introduced
344 * if token is TOK_SKIP, then the register write is skipped
345 * if token is TOK_WRITE, then the register write is performed
346 * Returns zero if successful, or non-zero otherwise.
348 static int tvp514x_write_regs(struct v4l2_subdev *sd,
349 const struct tvp514x_reg reglist[])
351 int err;
352 const struct tvp514x_reg *next = reglist;
354 for (; next->token != TOK_TERM; next++) {
355 if (next->token == TOK_DELAY) {
356 msleep(next->val);
357 continue;
360 if (next->token == TOK_SKIP)
361 continue;
363 err = tvp514x_write_reg(sd, next->reg, (u8) next->val);
364 if (err) {
365 v4l2_err(sd, "Write failed. Err[%d]\n", err);
366 return err;
369 return 0;
373 * tvp514x_get_current_std() : Get the current standard detected by TVP5146/47
374 * @sd: ptr to v4l2_subdev struct
376 * Get current standard detected by TVP5146/47, STD_INVALID if there is no
377 * standard detected.
379 static enum tvp514x_std tvp514x_get_current_std(struct v4l2_subdev *sd)
381 u8 std, std_status;
383 std = tvp514x_read_reg(sd, REG_VIDEO_STD);
384 if ((std & VIDEO_STD_MASK) == VIDEO_STD_AUTO_SWITCH_BIT)
385 /* use the standard status register */
386 std_status = tvp514x_read_reg(sd, REG_VIDEO_STD_STATUS);
387 else
388 /* use the standard register itself */
389 std_status = std;
391 switch (std_status & VIDEO_STD_MASK) {
392 case VIDEO_STD_NTSC_MJ_BIT:
393 return STD_NTSC_MJ;
395 case VIDEO_STD_PAL_BDGHIN_BIT:
396 return STD_PAL_BDGHIN;
398 default:
399 return STD_INVALID;
402 return STD_INVALID;
405 /* TVP5146/47 register dump function */
406 static void tvp514x_reg_dump(struct v4l2_subdev *sd)
408 dump_reg(sd, REG_INPUT_SEL);
409 dump_reg(sd, REG_AFE_GAIN_CTRL);
410 dump_reg(sd, REG_VIDEO_STD);
411 dump_reg(sd, REG_OPERATION_MODE);
412 dump_reg(sd, REG_COLOR_KILLER);
413 dump_reg(sd, REG_LUMA_CONTROL1);
414 dump_reg(sd, REG_LUMA_CONTROL2);
415 dump_reg(sd, REG_LUMA_CONTROL3);
416 dump_reg(sd, REG_BRIGHTNESS);
417 dump_reg(sd, REG_CONTRAST);
418 dump_reg(sd, REG_SATURATION);
419 dump_reg(sd, REG_HUE);
420 dump_reg(sd, REG_CHROMA_CONTROL1);
421 dump_reg(sd, REG_CHROMA_CONTROL2);
422 dump_reg(sd, REG_COMP_PR_SATURATION);
423 dump_reg(sd, REG_COMP_Y_CONTRAST);
424 dump_reg(sd, REG_COMP_PB_SATURATION);
425 dump_reg(sd, REG_COMP_Y_BRIGHTNESS);
426 dump_reg(sd, REG_AVID_START_PIXEL_LSB);
427 dump_reg(sd, REG_AVID_START_PIXEL_MSB);
428 dump_reg(sd, REG_AVID_STOP_PIXEL_LSB);
429 dump_reg(sd, REG_AVID_STOP_PIXEL_MSB);
430 dump_reg(sd, REG_HSYNC_START_PIXEL_LSB);
431 dump_reg(sd, REG_HSYNC_START_PIXEL_MSB);
432 dump_reg(sd, REG_HSYNC_STOP_PIXEL_LSB);
433 dump_reg(sd, REG_HSYNC_STOP_PIXEL_MSB);
434 dump_reg(sd, REG_VSYNC_START_LINE_LSB);
435 dump_reg(sd, REG_VSYNC_START_LINE_MSB);
436 dump_reg(sd, REG_VSYNC_STOP_LINE_LSB);
437 dump_reg(sd, REG_VSYNC_STOP_LINE_MSB);
438 dump_reg(sd, REG_VBLK_START_LINE_LSB);
439 dump_reg(sd, REG_VBLK_START_LINE_MSB);
440 dump_reg(sd, REG_VBLK_STOP_LINE_LSB);
441 dump_reg(sd, REG_VBLK_STOP_LINE_MSB);
442 dump_reg(sd, REG_SYNC_CONTROL);
443 dump_reg(sd, REG_OUTPUT_FORMATTER1);
444 dump_reg(sd, REG_OUTPUT_FORMATTER2);
445 dump_reg(sd, REG_OUTPUT_FORMATTER3);
446 dump_reg(sd, REG_OUTPUT_FORMATTER4);
447 dump_reg(sd, REG_OUTPUT_FORMATTER5);
448 dump_reg(sd, REG_OUTPUT_FORMATTER6);
449 dump_reg(sd, REG_CLEAR_LOST_LOCK);
453 * tvp514x_configure() - Configure the TVP5146/47 registers
454 * @sd: ptr to v4l2_subdev struct
455 * @decoder: ptr to tvp514x_decoder structure
457 * Returns zero if successful, or non-zero otherwise.
459 static int tvp514x_configure(struct v4l2_subdev *sd,
460 struct tvp514x_decoder *decoder)
462 int err;
464 /* common register initialization */
465 err =
466 tvp514x_write_regs(sd, decoder->tvp514x_regs);
467 if (err)
468 return err;
470 if (debug)
471 tvp514x_reg_dump(sd);
473 return 0;
477 * tvp514x_detect() - Detect if an tvp514x is present, and if so which revision.
478 * @sd: pointer to standard V4L2 sub-device structure
479 * @decoder: pointer to tvp514x_decoder structure
481 * A device is considered to be detected if the chip ID (LSB and MSB)
482 * registers match the expected values.
483 * Any value of the rom version register is accepted.
484 * Returns ENODEV error number if no device is detected, or zero
485 * if a device is detected.
487 static int tvp514x_detect(struct v4l2_subdev *sd,
488 struct tvp514x_decoder *decoder)
490 u8 chip_id_msb, chip_id_lsb, rom_ver;
491 struct i2c_client *client = v4l2_get_subdevdata(sd);
493 chip_id_msb = tvp514x_read_reg(sd, REG_CHIP_ID_MSB);
494 chip_id_lsb = tvp514x_read_reg(sd, REG_CHIP_ID_LSB);
495 rom_ver = tvp514x_read_reg(sd, REG_ROM_VERSION);
497 v4l2_dbg(1, debug, sd,
498 "chip id detected msb:0x%x lsb:0x%x rom version:0x%x\n",
499 chip_id_msb, chip_id_lsb, rom_ver);
500 if ((chip_id_msb != TVP514X_CHIP_ID_MSB)
501 || ((chip_id_lsb != TVP5146_CHIP_ID_LSB)
502 && (chip_id_lsb != TVP5147_CHIP_ID_LSB))) {
503 /* We didn't read the values we expected, so this must not be
504 * an TVP5146/47.
506 v4l2_err(sd, "chip id mismatch msb:0x%x lsb:0x%x\n",
507 chip_id_msb, chip_id_lsb);
508 return -ENODEV;
511 decoder->ver = rom_ver;
513 v4l2_info(sd, "%s (Version - 0x%.2x) found at 0x%x (%s)\n",
514 client->name, decoder->ver,
515 client->addr << 1, client->adapter->name);
516 return 0;
520 * tvp514x_querystd() - V4L2 decoder interface handler for querystd
521 * @sd: pointer to standard V4L2 sub-device structure
522 * @std_id: standard V4L2 std_id ioctl enum
524 * Returns the current standard detected by TVP5146/47. If no active input is
525 * detected, returns -EINVAL
527 static int tvp514x_querystd(struct v4l2_subdev *sd, v4l2_std_id *std_id)
529 struct tvp514x_decoder *decoder = to_decoder(sd);
530 enum tvp514x_std current_std;
531 enum tvp514x_input input_sel;
532 u8 sync_lock_status, lock_mask;
533 int err;
535 if (std_id == NULL)
536 return -EINVAL;
538 err = tvp514x_write_reg(sd, REG_VIDEO_STD,
539 VIDEO_STD_AUTO_SWITCH_BIT);
540 if (err < 0)
541 return err;
543 msleep(LOCK_RETRY_DELAY);
545 /* get the current standard */
546 current_std = tvp514x_get_current_std(sd);
547 if (current_std == STD_INVALID)
548 return -EINVAL;
550 input_sel = decoder->input;
552 switch (input_sel) {
553 case INPUT_CVBS_VI1A:
554 case INPUT_CVBS_VI1B:
555 case INPUT_CVBS_VI1C:
556 case INPUT_CVBS_VI2A:
557 case INPUT_CVBS_VI2B:
558 case INPUT_CVBS_VI2C:
559 case INPUT_CVBS_VI3A:
560 case INPUT_CVBS_VI3B:
561 case INPUT_CVBS_VI3C:
562 case INPUT_CVBS_VI4A:
563 lock_mask = STATUS_CLR_SUBCAR_LOCK_BIT |
564 STATUS_HORZ_SYNC_LOCK_BIT |
565 STATUS_VIRT_SYNC_LOCK_BIT;
566 break;
568 case INPUT_SVIDEO_VI2A_VI1A:
569 case INPUT_SVIDEO_VI2B_VI1B:
570 case INPUT_SVIDEO_VI2C_VI1C:
571 case INPUT_SVIDEO_VI2A_VI3A:
572 case INPUT_SVIDEO_VI2B_VI3B:
573 case INPUT_SVIDEO_VI2C_VI3C:
574 case INPUT_SVIDEO_VI4A_VI1A:
575 case INPUT_SVIDEO_VI4A_VI1B:
576 case INPUT_SVIDEO_VI4A_VI1C:
577 case INPUT_SVIDEO_VI4A_VI3A:
578 case INPUT_SVIDEO_VI4A_VI3B:
579 case INPUT_SVIDEO_VI4A_VI3C:
580 lock_mask = STATUS_HORZ_SYNC_LOCK_BIT |
581 STATUS_VIRT_SYNC_LOCK_BIT;
582 break;
583 /*Need to add other interfaces*/
584 default:
585 return -EINVAL;
587 /* check whether signal is locked */
588 sync_lock_status = tvp514x_read_reg(sd, REG_STATUS1);
589 if (lock_mask != (sync_lock_status & lock_mask))
590 return -EINVAL; /* No input detected */
592 decoder->current_std = current_std;
593 *std_id = decoder->std_list[current_std].standard.id;
595 v4l2_dbg(1, debug, sd, "Current STD: %s",
596 decoder->std_list[current_std].standard.name);
597 return 0;
601 * tvp514x_s_std() - V4L2 decoder interface handler for s_std
602 * @sd: pointer to standard V4L2 sub-device structure
603 * @std_id: standard V4L2 v4l2_std_id ioctl enum
605 * If std_id is supported, sets the requested standard. Otherwise, returns
606 * -EINVAL
608 static int tvp514x_s_std(struct v4l2_subdev *sd, v4l2_std_id std_id)
610 struct tvp514x_decoder *decoder = to_decoder(sd);
611 int err, i;
613 for (i = 0; i < decoder->num_stds; i++)
614 if (std_id & decoder->std_list[i].standard.id)
615 break;
617 if ((i == decoder->num_stds) || (i == STD_INVALID))
618 return -EINVAL;
620 err = tvp514x_write_reg(sd, REG_VIDEO_STD,
621 decoder->std_list[i].video_std);
622 if (err)
623 return err;
625 decoder->current_std = i;
626 decoder->tvp514x_regs[REG_VIDEO_STD].val =
627 decoder->std_list[i].video_std;
629 v4l2_dbg(1, debug, sd, "Standard set to: %s",
630 decoder->std_list[i].standard.name);
631 return 0;
635 * tvp514x_s_routing() - V4L2 decoder interface handler for s_routing
636 * @sd: pointer to standard V4L2 sub-device structure
637 * @input: input selector for routing the signal
638 * @output: output selector for routing the signal
639 * @config: config value. Not used
641 * If index is valid, selects the requested input. Otherwise, returns -EINVAL if
642 * the input is not supported or there is no active signal present in the
643 * selected input.
645 static int tvp514x_s_routing(struct v4l2_subdev *sd,
646 u32 input, u32 output, u32 config)
648 struct tvp514x_decoder *decoder = to_decoder(sd);
649 int err;
650 enum tvp514x_input input_sel;
651 enum tvp514x_output output_sel;
652 enum tvp514x_std current_std = STD_INVALID;
653 u8 sync_lock_status, lock_mask;
654 int try_count = LOCK_RETRY_COUNT;
656 if ((input >= INPUT_INVALID) ||
657 (output >= OUTPUT_INVALID))
658 /* Index out of bound */
659 return -EINVAL;
661 /* Since this api is goint to detect the input, it is required
662 to set the standard in the auto switch mode */
663 err = tvp514x_write_reg(sd, REG_VIDEO_STD,
664 VIDEO_STD_AUTO_SWITCH_BIT);
665 if (err < 0)
666 return err;
668 msleep(LOCK_RETRY_DELAY);
670 input_sel = input;
671 output_sel = output;
673 err = tvp514x_write_reg(sd, REG_INPUT_SEL, input_sel);
674 if (err)
675 return err;
677 output_sel |= tvp514x_read_reg(sd,
678 REG_OUTPUT_FORMATTER1) & 0x7;
679 err = tvp514x_write_reg(sd, REG_OUTPUT_FORMATTER1,
680 output_sel);
681 if (err)
682 return err;
684 decoder->tvp514x_regs[REG_INPUT_SEL].val = input_sel;
685 decoder->tvp514x_regs[REG_OUTPUT_FORMATTER1].val = output_sel;
687 /* Clear status */
688 msleep(LOCK_RETRY_DELAY);
689 err =
690 tvp514x_write_reg(sd, REG_CLEAR_LOST_LOCK, 0x01);
691 if (err)
692 return err;
694 switch (input_sel) {
695 case INPUT_CVBS_VI1A:
696 case INPUT_CVBS_VI1B:
697 case INPUT_CVBS_VI1C:
698 case INPUT_CVBS_VI2A:
699 case INPUT_CVBS_VI2B:
700 case INPUT_CVBS_VI2C:
701 case INPUT_CVBS_VI3A:
702 case INPUT_CVBS_VI3B:
703 case INPUT_CVBS_VI3C:
704 case INPUT_CVBS_VI4A:
705 lock_mask = STATUS_CLR_SUBCAR_LOCK_BIT |
706 STATUS_HORZ_SYNC_LOCK_BIT |
707 STATUS_VIRT_SYNC_LOCK_BIT;
708 break;
710 case INPUT_SVIDEO_VI2A_VI1A:
711 case INPUT_SVIDEO_VI2B_VI1B:
712 case INPUT_SVIDEO_VI2C_VI1C:
713 case INPUT_SVIDEO_VI2A_VI3A:
714 case INPUT_SVIDEO_VI2B_VI3B:
715 case INPUT_SVIDEO_VI2C_VI3C:
716 case INPUT_SVIDEO_VI4A_VI1A:
717 case INPUT_SVIDEO_VI4A_VI1B:
718 case INPUT_SVIDEO_VI4A_VI1C:
719 case INPUT_SVIDEO_VI4A_VI3A:
720 case INPUT_SVIDEO_VI4A_VI3B:
721 case INPUT_SVIDEO_VI4A_VI3C:
722 lock_mask = STATUS_HORZ_SYNC_LOCK_BIT |
723 STATUS_VIRT_SYNC_LOCK_BIT;
724 break;
725 /* Need to add other interfaces*/
726 default:
727 return -EINVAL;
730 while (try_count-- > 0) {
731 /* Allow decoder to sync up with new input */
732 msleep(LOCK_RETRY_DELAY);
734 /* get the current standard for future reference */
735 current_std = tvp514x_get_current_std(sd);
736 if (current_std == STD_INVALID)
737 continue;
739 sync_lock_status = tvp514x_read_reg(sd,
740 REG_STATUS1);
741 if (lock_mask == (sync_lock_status & lock_mask))
742 /* Input detected */
743 break;
746 if ((current_std == STD_INVALID) || (try_count < 0))
747 return -EINVAL;
749 decoder->current_std = current_std;
750 decoder->input = input;
751 decoder->output = output;
753 v4l2_dbg(1, debug, sd, "Input set to: %d, std : %d",
754 input_sel, current_std);
756 return 0;
760 * tvp514x_queryctrl() - V4L2 decoder interface handler for queryctrl
761 * @sd: pointer to standard V4L2 sub-device structure
762 * @qctrl: standard V4L2 v4l2_queryctrl structure
764 * If the requested control is supported, returns the control information.
765 * Otherwise, returns -EINVAL if the control is not supported.
767 static int
768 tvp514x_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qctrl)
770 int err = -EINVAL;
772 if (qctrl == NULL)
773 return err;
775 switch (qctrl->id) {
776 case V4L2_CID_BRIGHTNESS:
777 /* Brightness supported is (0-255), */
778 err = v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 128);
779 break;
780 case V4L2_CID_CONTRAST:
781 case V4L2_CID_SATURATION:
783 * Saturation and Contrast supported is -
784 * Contrast: 0 - 255 (Default - 128)
785 * Saturation: 0 - 255 (Default - 128)
787 err = v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 128);
788 break;
789 case V4L2_CID_HUE:
790 /* Hue Supported is -
791 * Hue - -180 - +180 (Default - 0, Step - +180)
793 err = v4l2_ctrl_query_fill(qctrl, -180, 180, 180, 0);
794 break;
795 case V4L2_CID_AUTOGAIN:
797 * Auto Gain supported is -
798 * 0 - 1 (Default - 1)
800 err = v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 1);
801 break;
802 default:
803 v4l2_err(sd, "invalid control id %d\n", qctrl->id);
804 return err;
807 v4l2_dbg(1, debug, sd, "Query Control:%s: Min - %d, Max - %d, Def - %d",
808 qctrl->name, qctrl->minimum, qctrl->maximum,
809 qctrl->default_value);
811 return err;
815 * tvp514x_g_ctrl() - V4L2 decoder interface handler for g_ctrl
816 * @sd: pointer to standard V4L2 sub-device structure
817 * @ctrl: pointer to v4l2_control structure
819 * If the requested control is supported, returns the control's current
820 * value from the decoder. Otherwise, returns -EINVAL if the control is not
821 * supported.
823 static int
824 tvp514x_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
826 struct tvp514x_decoder *decoder = to_decoder(sd);
828 if (ctrl == NULL)
829 return -EINVAL;
831 switch (ctrl->id) {
832 case V4L2_CID_BRIGHTNESS:
833 ctrl->value = decoder->tvp514x_regs[REG_BRIGHTNESS].val;
834 break;
835 case V4L2_CID_CONTRAST:
836 ctrl->value = decoder->tvp514x_regs[REG_CONTRAST].val;
837 break;
838 case V4L2_CID_SATURATION:
839 ctrl->value = decoder->tvp514x_regs[REG_SATURATION].val;
840 break;
841 case V4L2_CID_HUE:
842 ctrl->value = decoder->tvp514x_regs[REG_HUE].val;
843 if (ctrl->value == 0x7F)
844 ctrl->value = 180;
845 else if (ctrl->value == 0x80)
846 ctrl->value = -180;
847 else
848 ctrl->value = 0;
850 break;
851 case V4L2_CID_AUTOGAIN:
852 ctrl->value = decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val;
853 if ((ctrl->value & 0x3) == 3)
854 ctrl->value = 1;
855 else
856 ctrl->value = 0;
858 break;
859 default:
860 v4l2_err(sd, "invalid control id %d\n", ctrl->id);
861 return -EINVAL;
864 v4l2_dbg(1, debug, sd, "Get Control: ID - %d - %d",
865 ctrl->id, ctrl->value);
866 return 0;
870 * tvp514x_s_ctrl() - V4L2 decoder interface handler for s_ctrl
871 * @sd: pointer to standard V4L2 sub-device structure
872 * @ctrl: pointer to v4l2_control structure
874 * If the requested control is supported, sets the control's current
875 * value in HW. Otherwise, returns -EINVAL if the control is not supported.
877 static int
878 tvp514x_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
880 struct tvp514x_decoder *decoder = to_decoder(sd);
881 int err = -EINVAL, value;
883 if (ctrl == NULL)
884 return err;
886 value = ctrl->value;
888 switch (ctrl->id) {
889 case V4L2_CID_BRIGHTNESS:
890 if (ctrl->value < 0 || ctrl->value > 255) {
891 v4l2_err(sd, "invalid brightness setting %d\n",
892 ctrl->value);
893 return -ERANGE;
895 err = tvp514x_write_reg(sd, REG_BRIGHTNESS,
896 value);
897 if (err)
898 return err;
900 decoder->tvp514x_regs[REG_BRIGHTNESS].val = value;
901 break;
902 case V4L2_CID_CONTRAST:
903 if (ctrl->value < 0 || ctrl->value > 255) {
904 v4l2_err(sd, "invalid contrast setting %d\n",
905 ctrl->value);
906 return -ERANGE;
908 err = tvp514x_write_reg(sd, REG_CONTRAST, value);
909 if (err)
910 return err;
912 decoder->tvp514x_regs[REG_CONTRAST].val = value;
913 break;
914 case V4L2_CID_SATURATION:
915 if (ctrl->value < 0 || ctrl->value > 255) {
916 v4l2_err(sd, "invalid saturation setting %d\n",
917 ctrl->value);
918 return -ERANGE;
920 err = tvp514x_write_reg(sd, REG_SATURATION, value);
921 if (err)
922 return err;
924 decoder->tvp514x_regs[REG_SATURATION].val = value;
925 break;
926 case V4L2_CID_HUE:
927 if (value == 180)
928 value = 0x7F;
929 else if (value == -180)
930 value = 0x80;
931 else if (value == 0)
932 value = 0;
933 else {
934 v4l2_err(sd, "invalid hue setting %d\n", ctrl->value);
935 return -ERANGE;
937 err = tvp514x_write_reg(sd, REG_HUE, value);
938 if (err)
939 return err;
941 decoder->tvp514x_regs[REG_HUE].val = value;
942 break;
943 case V4L2_CID_AUTOGAIN:
944 if (value == 1)
945 value = 0x0F;
946 else if (value == 0)
947 value = 0x0C;
948 else {
949 v4l2_err(sd, "invalid auto gain setting %d\n",
950 ctrl->value);
951 return -ERANGE;
953 err = tvp514x_write_reg(sd, REG_AFE_GAIN_CTRL, value);
954 if (err)
955 return err;
957 decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val = value;
958 break;
959 default:
960 v4l2_err(sd, "invalid control id %d\n", ctrl->id);
961 return err;
964 v4l2_dbg(1, debug, sd, "Set Control: ID - %d - %d",
965 ctrl->id, ctrl->value);
967 return err;
971 * tvp514x_enum_fmt_cap() - V4L2 decoder interface handler for enum_fmt
972 * @sd: pointer to standard V4L2 sub-device structure
973 * @fmt: standard V4L2 VIDIOC_ENUM_FMT ioctl structure
975 * Implement the VIDIOC_ENUM_FMT ioctl to enumerate supported formats
977 static int
978 tvp514x_enum_fmt_cap(struct v4l2_subdev *sd, struct v4l2_fmtdesc *fmt)
980 struct tvp514x_decoder *decoder = to_decoder(sd);
981 int index;
983 if (fmt == NULL)
984 return -EINVAL;
986 index = fmt->index;
987 if ((index >= decoder->num_fmts) || (index < 0))
988 /* Index out of bound */
989 return -EINVAL;
991 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
992 /* only capture is supported */
993 return -EINVAL;
995 memcpy(fmt, &decoder->fmt_list[index],
996 sizeof(struct v4l2_fmtdesc));
998 v4l2_dbg(1, debug, sd, "Current FMT: index - %d (%s)",
999 decoder->fmt_list[index].index,
1000 decoder->fmt_list[index].description);
1001 return 0;
1005 * tvp514x_try_fmt_cap() - V4L2 decoder interface handler for try_fmt
1006 * @sd: pointer to standard V4L2 sub-device structure
1007 * @f: pointer to standard V4L2 VIDIOC_TRY_FMT ioctl structure
1009 * Implement the VIDIOC_TRY_FMT ioctl for the CAPTURE buffer type. This
1010 * ioctl is used to negotiate the image capture size and pixel format
1011 * without actually making it take effect.
1013 static int
1014 tvp514x_try_fmt_cap(struct v4l2_subdev *sd, struct v4l2_format *f)
1016 struct tvp514x_decoder *decoder = to_decoder(sd);
1017 int ifmt;
1018 struct v4l2_pix_format *pix;
1019 enum tvp514x_std current_std;
1021 if (f == NULL)
1022 return -EINVAL;
1024 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1025 /* only capture is supported */
1026 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1028 pix = &f->fmt.pix;
1030 /* Calculate height and width based on current standard */
1031 current_std = tvp514x_get_current_std(sd);
1032 if (current_std == STD_INVALID)
1033 return -EINVAL;
1035 decoder->current_std = current_std;
1036 pix->width = decoder->std_list[current_std].width;
1037 pix->height = decoder->std_list[current_std].height;
1039 for (ifmt = 0; ifmt < decoder->num_fmts; ifmt++) {
1040 if (pix->pixelformat ==
1041 decoder->fmt_list[ifmt].pixelformat)
1042 break;
1044 if (ifmt == decoder->num_fmts)
1045 /* None of the format matched, select default */
1046 ifmt = 0;
1047 pix->pixelformat = decoder->fmt_list[ifmt].pixelformat;
1049 pix->field = V4L2_FIELD_INTERLACED;
1050 pix->bytesperline = pix->width * 2;
1051 pix->sizeimage = pix->bytesperline * pix->height;
1052 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
1053 pix->priv = 0;
1055 v4l2_dbg(1, debug, sd, "Try FMT: pixelformat - %s, bytesperline - %d"
1056 "Width - %d, Height - %d",
1057 decoder->fmt_list[ifmt].description, pix->bytesperline,
1058 pix->width, pix->height);
1059 return 0;
1063 * tvp514x_s_fmt_cap() - V4L2 decoder interface handler for s_fmt
1064 * @sd: pointer to standard V4L2 sub-device structure
1065 * @f: pointer to standard V4L2 VIDIOC_S_FMT ioctl structure
1067 * If the requested format is supported, configures the HW to use that
1068 * format, returns error code if format not supported or HW can't be
1069 * correctly configured.
1071 static int
1072 tvp514x_s_fmt_cap(struct v4l2_subdev *sd, struct v4l2_format *f)
1074 struct tvp514x_decoder *decoder = to_decoder(sd);
1075 struct v4l2_pix_format *pix;
1076 int rval;
1078 if (f == NULL)
1079 return -EINVAL;
1081 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1082 /* only capture is supported */
1083 return -EINVAL;
1085 pix = &f->fmt.pix;
1086 rval = tvp514x_try_fmt_cap(sd, f);
1087 if (rval)
1088 return rval;
1090 decoder->pix = *pix;
1092 return rval;
1096 * tvp514x_g_fmt_cap() - V4L2 decoder interface handler for tvp514x_g_fmt_cap
1097 * @sd: pointer to standard V4L2 sub-device structure
1098 * @f: pointer to standard V4L2 v4l2_format structure
1100 * Returns the decoder's current pixel format in the v4l2_format
1101 * parameter.
1103 static int
1104 tvp514x_g_fmt_cap(struct v4l2_subdev *sd, struct v4l2_format *f)
1106 struct tvp514x_decoder *decoder = to_decoder(sd);
1108 if (f == NULL)
1109 return -EINVAL;
1111 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1112 /* only capture is supported */
1113 return -EINVAL;
1115 f->fmt.pix = decoder->pix;
1117 v4l2_dbg(1, debug, sd, "Current FMT: bytesperline - %d"
1118 "Width - %d, Height - %d",
1119 decoder->pix.bytesperline,
1120 decoder->pix.width, decoder->pix.height);
1121 return 0;
1125 * tvp514x_g_parm() - V4L2 decoder interface handler for g_parm
1126 * @sd: pointer to standard V4L2 sub-device structure
1127 * @a: pointer to standard V4L2 VIDIOC_G_PARM ioctl structure
1129 * Returns the decoder's video CAPTURE parameters.
1131 static int
1132 tvp514x_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
1134 struct tvp514x_decoder *decoder = to_decoder(sd);
1135 struct v4l2_captureparm *cparm;
1136 enum tvp514x_std current_std;
1138 if (a == NULL)
1139 return -EINVAL;
1141 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1142 /* only capture is supported */
1143 return -EINVAL;
1145 memset(a, 0, sizeof(*a));
1146 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1148 /* get the current standard */
1149 current_std = tvp514x_get_current_std(sd);
1150 if (current_std == STD_INVALID)
1151 return -EINVAL;
1153 decoder->current_std = current_std;
1155 cparm = &a->parm.capture;
1156 cparm->capability = V4L2_CAP_TIMEPERFRAME;
1157 cparm->timeperframe =
1158 decoder->std_list[current_std].standard.frameperiod;
1160 return 0;
1164 * tvp514x_s_parm() - V4L2 decoder interface handler for s_parm
1165 * @sd: pointer to standard V4L2 sub-device structure
1166 * @a: pointer to standard V4L2 VIDIOC_S_PARM ioctl structure
1168 * Configures the decoder to use the input parameters, if possible. If
1169 * not possible, returns the appropriate error code.
1171 static int
1172 tvp514x_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
1174 struct tvp514x_decoder *decoder = to_decoder(sd);
1175 struct v4l2_fract *timeperframe;
1176 enum tvp514x_std current_std;
1178 if (a == NULL)
1179 return -EINVAL;
1181 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1182 /* only capture is supported */
1183 return -EINVAL;
1185 timeperframe = &a->parm.capture.timeperframe;
1187 /* get the current standard */
1188 current_std = tvp514x_get_current_std(sd);
1189 if (current_std == STD_INVALID)
1190 return -EINVAL;
1192 decoder->current_std = current_std;
1194 *timeperframe =
1195 decoder->std_list[current_std].standard.frameperiod;
1197 return 0;
1201 * tvp514x_s_stream() - V4L2 decoder i/f handler for s_stream
1202 * @sd: pointer to standard V4L2 sub-device structure
1203 * @enable: streaming enable or disable
1205 * Sets streaming to enable or disable, if possible.
1207 static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable)
1209 int err = 0;
1210 struct i2c_client *client = v4l2_get_subdevdata(sd);
1211 struct tvp514x_decoder *decoder = to_decoder(sd);
1213 if (decoder->streaming == enable)
1214 return 0;
1216 switch (enable) {
1217 case 0:
1219 /* Power Down Sequence */
1220 err = tvp514x_write_reg(sd, REG_OPERATION_MODE, 0x01);
1221 if (err) {
1222 v4l2_err(sd, "Unable to turn off decoder\n");
1223 return err;
1225 decoder->streaming = enable;
1226 break;
1228 case 1:
1230 struct tvp514x_reg *int_seq = (struct tvp514x_reg *)
1231 client->driver->id_table->driver_data;
1233 /* Power Up Sequence */
1234 err = tvp514x_write_regs(sd, int_seq);
1235 if (err) {
1236 v4l2_err(sd, "Unable to turn on decoder\n");
1237 return err;
1239 /* Detect if not already detected */
1240 err = tvp514x_detect(sd, decoder);
1241 if (err) {
1242 v4l2_err(sd, "Unable to detect decoder\n");
1243 return err;
1245 err = tvp514x_configure(sd, decoder);
1246 if (err) {
1247 v4l2_err(sd, "Unable to configure decoder\n");
1248 return err;
1250 decoder->streaming = enable;
1251 break;
1253 default:
1254 err = -ENODEV;
1255 break;
1258 return err;
1261 static const struct v4l2_subdev_core_ops tvp514x_core_ops = {
1262 .queryctrl = tvp514x_queryctrl,
1263 .g_ctrl = tvp514x_g_ctrl,
1264 .s_ctrl = tvp514x_s_ctrl,
1265 .s_std = tvp514x_s_std,
1268 static const struct v4l2_subdev_video_ops tvp514x_video_ops = {
1269 .s_routing = tvp514x_s_routing,
1270 .querystd = tvp514x_querystd,
1271 .enum_fmt = tvp514x_enum_fmt_cap,
1272 .g_fmt = tvp514x_g_fmt_cap,
1273 .try_fmt = tvp514x_try_fmt_cap,
1274 .s_fmt = tvp514x_s_fmt_cap,
1275 .g_parm = tvp514x_g_parm,
1276 .s_parm = tvp514x_s_parm,
1277 .s_stream = tvp514x_s_stream,
1280 static const struct v4l2_subdev_ops tvp514x_ops = {
1281 .core = &tvp514x_core_ops,
1282 .video = &tvp514x_video_ops,
1285 static struct tvp514x_decoder tvp514x_dev = {
1286 .streaming = 0,
1288 .fmt_list = tvp514x_fmt_list,
1289 .num_fmts = ARRAY_SIZE(tvp514x_fmt_list),
1291 .pix = {
1292 /* Default to NTSC 8-bit YUV 422 */
1293 .width = NTSC_NUM_ACTIVE_PIXELS,
1294 .height = NTSC_NUM_ACTIVE_LINES,
1295 .pixelformat = V4L2_PIX_FMT_UYVY,
1296 .field = V4L2_FIELD_INTERLACED,
1297 .bytesperline = NTSC_NUM_ACTIVE_PIXELS * 2,
1298 .sizeimage =
1299 NTSC_NUM_ACTIVE_PIXELS * 2 * NTSC_NUM_ACTIVE_LINES,
1300 .colorspace = V4L2_COLORSPACE_SMPTE170M,
1303 .current_std = STD_NTSC_MJ,
1304 .std_list = tvp514x_std_list,
1305 .num_stds = ARRAY_SIZE(tvp514x_std_list),
1310 * tvp514x_probe() - decoder driver i2c probe handler
1311 * @client: i2c driver client device structure
1312 * @id: i2c driver id table
1314 * Register decoder as an i2c client device and V4L2
1315 * device.
1317 static int
1318 tvp514x_probe(struct i2c_client *client, const struct i2c_device_id *id)
1320 struct tvp514x_decoder *decoder;
1321 struct v4l2_subdev *sd;
1323 /* Check if the adapter supports the needed features */
1324 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1325 return -EIO;
1327 if (!client->dev.platform_data) {
1328 v4l2_err(client, "No platform data!!\n");
1329 return -ENODEV;
1332 decoder = kzalloc(sizeof(*decoder), GFP_KERNEL);
1333 if (!decoder)
1334 return -ENOMEM;
1336 /* Initialize the tvp514x_decoder with default configuration */
1337 *decoder = tvp514x_dev;
1338 /* Copy default register configuration */
1339 memcpy(decoder->tvp514x_regs, tvp514x_reg_list_default,
1340 sizeof(tvp514x_reg_list_default));
1342 /* Copy board specific information here */
1343 decoder->pdata = client->dev.platform_data;
1346 * Fetch platform specific data, and configure the
1347 * tvp514x_reg_list[] accordingly. Since this is one
1348 * time configuration, no need to preserve.
1350 decoder->tvp514x_regs[REG_OUTPUT_FORMATTER2].val |=
1351 (decoder->pdata->clk_polarity << 1);
1352 decoder->tvp514x_regs[REG_SYNC_CONTROL].val |=
1353 ((decoder->pdata->hs_polarity << 2) |
1354 (decoder->pdata->vs_polarity << 3));
1355 /* Set default standard to auto */
1356 decoder->tvp514x_regs[REG_VIDEO_STD].val =
1357 VIDEO_STD_AUTO_SWITCH_BIT;
1359 /* Register with V4L2 layer as slave device */
1360 sd = &decoder->sd;
1361 v4l2_i2c_subdev_init(sd, client, &tvp514x_ops);
1363 v4l2_info(sd, "%s decoder driver registered !!\n", sd->name);
1365 return 0;
1370 * tvp514x_remove() - decoder driver i2c remove handler
1371 * @client: i2c driver client device structure
1373 * Unregister decoder as an i2c client device and V4L2
1374 * device. Complement of tvp514x_probe().
1376 static int tvp514x_remove(struct i2c_client *client)
1378 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1379 struct tvp514x_decoder *decoder = to_decoder(sd);
1381 v4l2_device_unregister_subdev(sd);
1382 kfree(decoder);
1383 return 0;
1385 /* TVP5146 Init/Power on Sequence */
1386 static const struct tvp514x_reg tvp5146_init_reg_seq[] = {
1387 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
1388 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1389 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
1390 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1391 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1392 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1393 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1394 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1395 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
1396 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1397 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
1398 {TOK_TERM, 0, 0},
1401 /* TVP5147 Init/Power on Sequence */
1402 static const struct tvp514x_reg tvp5147_init_reg_seq[] = {
1403 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
1404 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1405 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
1406 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1407 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1408 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1409 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1410 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1411 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x16},
1412 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1413 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xA0},
1414 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x16},
1415 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1416 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1417 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1418 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
1419 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1420 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
1421 {TOK_TERM, 0, 0},
1424 /* TVP5146M2/TVP5147M1 Init/Power on Sequence */
1425 static const struct tvp514x_reg tvp514xm_init_reg_seq[] = {
1426 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1427 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
1428 {TOK_TERM, 0, 0},
1432 * I2C Device Table -
1434 * name - Name of the actual device/chip.
1435 * driver_data - Driver data
1437 static const struct i2c_device_id tvp514x_id[] = {
1438 {"tvp5146", (unsigned long)tvp5146_init_reg_seq},
1439 {"tvp5146m2", (unsigned long)tvp514xm_init_reg_seq},
1440 {"tvp5147", (unsigned long)tvp5147_init_reg_seq},
1441 {"tvp5147m1", (unsigned long)tvp514xm_init_reg_seq},
1445 MODULE_DEVICE_TABLE(i2c, tvp514x_id);
1447 static struct i2c_driver tvp514x_driver = {
1448 .driver = {
1449 .owner = THIS_MODULE,
1450 .name = TVP514X_MODULE_NAME,
1452 .probe = tvp514x_probe,
1453 .remove = tvp514x_remove,
1454 .id_table = tvp514x_id,
1457 static int __init tvp514x_init(void)
1459 return i2c_add_driver(&tvp514x_driver);
1462 static void __exit tvp514x_exit(void)
1464 i2c_del_driver(&tvp514x_driver);
1467 module_init(tvp514x_init);
1468 module_exit(tvp514x_exit);