V4L/DVB (10655): tvp514x: make the module aware of rich people
[linux-2.6/libata-dev.git] / drivers / media / video / tvp514x.c
blobf0b2b8ed2fe46b6024749e4216b95e71963c7675
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>
34 #include <media/v4l2-int-device.h>
35 #include <media/tvp514x.h>
37 #include "tvp514x_regs.h"
39 /* Module Name */
40 #define TVP514X_MODULE_NAME "tvp514x"
42 /* Private macros for TVP */
43 #define I2C_RETRY_COUNT (5)
44 #define LOCK_RETRY_COUNT (5)
45 #define LOCK_RETRY_DELAY (200)
47 /* Debug functions */
48 static int debug;
49 module_param(debug, bool, 0644);
50 MODULE_PARM_DESC(debug, "Debug level (0-1)");
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)
58 /**
59 * enum tvp514x_std - enum for supported standards
61 enum tvp514x_std {
62 STD_NTSC_MJ = 0,
63 STD_PAL_BDGHIN,
64 STD_INVALID
67 /**
68 * enum tvp514x_state - enum for different decoder states
70 enum tvp514x_state {
71 STATE_NOT_DETECTED,
72 STATE_DETECTED
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
82 struct tvp514x_std_info {
83 unsigned long width;
84 unsigned long height;
85 u8 video_std;
86 struct v4l2_standard standard;
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
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;
115 struct i2c_device_id *id;
117 int ver;
118 enum tvp514x_state state;
120 struct v4l2_pix_format pix;
121 int num_fmts;
122 const struct v4l2_fmtdesc *fmt_list;
124 enum tvp514x_std current_std;
125 int num_stds;
126 struct tvp514x_std_info *std_list;
128 struct v4l2_routing route;
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},
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.
198 static const struct v4l2_fmtdesc tvp514x_fmt_list[] = {
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,
209 * Supported standards -
211 * Currently supports two standards only, need to add support for rest of the
212 * modes, like SECAM, etc...
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
227 /* Standard: STD_PAL_BDGHIN */
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
241 /* Standard: need to add for additional standard */
244 * Control structure for Auto Gain
245 * This is temporary data, will get replaced once
246 * v4l2_ctrl_query_fill supports it.
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,
259 * Read a value from a register in an TVP5146/47 decoder device.
260 * Returns value read if successful, or non-zero (-1) otherwise.
262 static int tvp514x_read_reg(struct i2c_client *client, u8 reg)
264 int err;
265 int retry = 0;
266 read_again:
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;
278 return err;
282 * Write a value to a register in an TVP5146/47 decoder device.
283 * Returns zero if successful, or non-zero otherwise.
285 static int tvp514x_write_reg(struct i2c_client *client, u8 reg, u8 val)
287 int err;
288 int retry = 0;
289 write_again:
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;
301 return err;
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
311 * reglist - list of registers to be written
312 * Returns zero if successful, or non-zero otherwise.
314 static int tvp514x_write_regs(struct i2c_client *client,
315 const struct tvp514x_reg reglist[])
317 int err;
318 const struct tvp514x_reg *next = reglist;
320 for (; next->token != TOK_TERM; next++) {
321 if (next->token == TOK_DELAY) {
322 msleep(next->val);
323 continue;
326 if (next->token == TOK_SKIP)
327 continue;
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;
335 return 0;
339 * tvp514x_get_current_std:
340 * Returns the current standard detected by TVP5146/47
342 static enum tvp514x_std tvp514x_get_current_std(struct tvp514x_decoder
343 *decoder)
345 u8 std, std_status;
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 */
355 switch (std_status & VIDEO_STD_MASK) {
356 case VIDEO_STD_NTSC_MJ_BIT:
357 return STD_NTSC_MJ;
359 case VIDEO_STD_PAL_BDGHIN_BIT:
360 return STD_PAL_BDGHIN;
362 default:
363 return STD_INVALID;
366 return STD_INVALID;
370 * TVP5146/47 register dump function
372 static void tvp514x_reg_dump(struct tvp514x_decoder *decoder)
374 u8 value;
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);
421 * Configure the TVP5146/47 with the current register settings
422 * Returns zero if successful, or non-zero otherwise.
424 static int tvp514x_configure(struct tvp514x_decoder *decoder)
426 int err;
428 /* common register initialization */
429 err =
430 tvp514x_write_regs(decoder->client, decoder->tvp514x_regs);
431 if (err)
432 return err;
434 if (debug)
435 tvp514x_reg_dump(decoder);
437 return 0;
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.
448 static int tvp514x_detect(struct tvp514x_decoder *decoder)
450 u8 chip_id_msb, chip_id_lsb, rom_ver;
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);
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.
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;
471 decoder->ver = rom_ver;
472 decoder->state = STATE_DETECTED;
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;
482 * Following are decoder interface functions implemented by
483 * TVP5146/47 decoder driver.
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
491 * Returns the current standard detected by TVP5146/47. If no active input is
492 * detected, returns -EINVAL
494 static int ioctl_querystd(struct v4l2_int_device *s, v4l2_std_id *std_id)
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;
501 if (std_id == NULL)
502 return -EINVAL;
504 /* get the current standard */
505 current_std = tvp514x_get_current_std(decoder);
506 if (current_std == STD_INVALID)
507 return -EINVAL;
509 input_sel = decoder->route.input;
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;
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;
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 */
551 decoder->current_std = current_std;
552 *std_id = decoder->std_list[current_std].standard.id;
554 v4l_dbg(1, debug, decoder->client, "Current STD: %s",
555 decoder->std_list[current_std].standard.name);
556 return 0;
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
564 * If std_id is supported, sets the requested standard. Otherwise, returns
565 * -EINVAL
567 static int ioctl_s_std(struct v4l2_int_device *s, v4l2_std_id *std_id)
569 struct tvp514x_decoder *decoder = s->priv;
570 int err, i;
572 if (std_id == NULL)
573 return -EINVAL;
575 for (i = 0; i < decoder->num_stds; i++)
576 if (*std_id & decoder->std_list[i].standard.id)
577 break;
579 if ((i == decoder->num_stds) || (i == STD_INVALID))
580 return -EINVAL;
582 err = tvp514x_write_reg(decoder->client, REG_VIDEO_STD,
583 decoder->std_list[i].video_std);
584 if (err)
585 return err;
587 decoder->current_std = i;
588 decoder->tvp514x_regs[REG_VIDEO_STD].val =
589 decoder->std_list[i].video_std;
591 v4l_dbg(1, debug, decoder->client, "Standard set to: %s",
592 decoder->std_list[i].standard.name);
593 return 0;
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
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.
605 static int ioctl_s_routing(struct v4l2_int_device *s,
606 struct v4l2_routing *route)
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;
616 if ((!route) || (route->input >= INPUT_INVALID) ||
617 (route->output >= OUTPUT_INVALID))
618 return -EINVAL; /* Index out of bound */
620 input_sel = route->input;
621 output_sel = route->output;
623 err = tvp514x_write_reg(decoder->client, REG_INPUT_SEL, input_sel);
624 if (err)
625 return err;
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;
634 decoder->tvp514x_regs[REG_INPUT_SEL].val = input_sel;
635 decoder->tvp514x_regs[REG_OUTPUT_FORMATTER1].val = output_sel;
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;
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;
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;
680 while (try_count-- > 0) {
681 /* Allow decoder to sync up with new input */
682 msleep(LOCK_RETRY_DELAY);
684 /* get the current standard for future reference */
685 current_std = tvp514x_get_current_std(decoder);
686 if (current_std == STD_INVALID)
687 continue;
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 */
695 if ((current_std == STD_INVALID) || (try_count <= 0))
696 return -EINVAL;
698 decoder->current_std = current_std;
699 decoder->route.input = route->input;
700 decoder->route.output = route->output;
702 v4l_dbg(1, debug, decoder->client,
703 "Input set to: %d, std : %d",
704 input_sel, current_std);
706 return 0;
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
714 * If the requested control is supported, returns the control information.
715 * Otherwise, returns -EINVAL if the control is not supported.
717 static int
718 ioctl_queryctrl(struct v4l2_int_device *s, struct v4l2_queryctrl *qctrl)
720 struct tvp514x_decoder *decoder = s->priv;
721 int err = -EINVAL;
723 if (qctrl == NULL)
724 return err;
726 switch (qctrl->id) {
727 case V4L2_CID_BRIGHTNESS:
728 /* Brightness supported is same as standard one (0-255),
729 * so make use of standard API provided.
731 err = v4l2_ctrl_query_fill_std(qctrl);
732 break;
733 case V4L2_CID_CONTRAST:
734 case V4L2_CID_SATURATION:
735 /* Saturation and Contrast supported is -
736 * Contrast: 0 - 255 (Default - 128)
737 * Saturation: 0 - 255 (Default - 128)
739 err = v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 128);
740 break;
741 case V4L2_CID_HUE:
742 /* Hue Supported is -
743 * Hue - -180 - +180 (Default - 0, Step - +180)
745 err = v4l2_ctrl_query_fill(qctrl, -180, 180, 180, 0);
746 break;
747 case V4L2_CID_AUTOGAIN:
748 /* Autogain is either 0 or 1*/
749 memcpy(qctrl, &tvp514x_autogain_ctrl,
750 sizeof(struct v4l2_queryctrl));
751 err = 0;
752 break;
753 default:
754 v4l_err(decoder->client,
755 "invalid control id %d\n", qctrl->id);
756 return err;
759 v4l_dbg(1, debug, decoder->client,
760 "Query Control: %s : Min - %d, Max - %d, Def - %d",
761 qctrl->name,
762 qctrl->minimum,
763 qctrl->maximum,
764 qctrl->default_value);
766 return err;
770 * ioctl_g_ctrl - V4L2 decoder interface handler for VIDIOC_G_CTRL ioctl
771 * @s: pointer to standard V4L2 device structure
772 * @ctrl: pointer to v4l2_control structure
774 * If the requested control is supported, returns the control's current
775 * value from the decoder. Otherwise, returns -EINVAL if the control is not
776 * supported.
778 static int
779 ioctl_g_ctrl(struct v4l2_int_device *s, struct v4l2_control *ctrl)
781 struct tvp514x_decoder *decoder = s->priv;
783 if (ctrl == NULL)
784 return -EINVAL;
786 switch (ctrl->id) {
787 case V4L2_CID_BRIGHTNESS:
788 ctrl->value = decoder->tvp514x_regs[REG_BRIGHTNESS].val;
789 break;
790 case V4L2_CID_CONTRAST:
791 ctrl->value = decoder->tvp514x_regs[REG_CONTRAST].val;
792 break;
793 case V4L2_CID_SATURATION:
794 ctrl->value = decoder->tvp514x_regs[REG_SATURATION].val;
795 break;
796 case V4L2_CID_HUE:
797 ctrl->value = decoder->tvp514x_regs[REG_HUE].val;
798 if (ctrl->value == 0x7F)
799 ctrl->value = 180;
800 else if (ctrl->value == 0x80)
801 ctrl->value = -180;
802 else
803 ctrl->value = 0;
805 break;
806 case V4L2_CID_AUTOGAIN:
807 ctrl->value = decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val;
808 if ((ctrl->value & 0x3) == 3)
809 ctrl->value = 1;
810 else
811 ctrl->value = 0;
813 break;
814 default:
815 v4l_err(decoder->client,
816 "invalid control id %d\n", ctrl->id);
817 return -EINVAL;
820 v4l_dbg(1, debug, decoder->client,
821 "Get Control: ID - %d - %d",
822 ctrl->id, ctrl->value);
823 return 0;
827 * ioctl_s_ctrl - V4L2 decoder interface handler for VIDIOC_S_CTRL ioctl
828 * @s: pointer to standard V4L2 device structure
829 * @ctrl: pointer to v4l2_control structure
831 * If the requested control is supported, sets the control's current
832 * value in HW. Otherwise, returns -EINVAL if the control is not supported.
834 static int
835 ioctl_s_ctrl(struct v4l2_int_device *s, struct v4l2_control *ctrl)
837 struct tvp514x_decoder *decoder = s->priv;
838 int err = -EINVAL, value;
840 if (ctrl == NULL)
841 return err;
843 value = (__s32) ctrl->value;
845 switch (ctrl->id) {
846 case V4L2_CID_BRIGHTNESS:
847 if (ctrl->value < 0 || ctrl->value > 255) {
848 v4l_err(decoder->client,
849 "invalid brightness setting %d\n",
850 ctrl->value);
851 return -ERANGE;
853 err = tvp514x_write_reg(decoder->client, REG_BRIGHTNESS,
854 value);
855 if (err)
856 return err;
857 decoder->tvp514x_regs[REG_BRIGHTNESS].val = value;
858 break;
859 case V4L2_CID_CONTRAST:
860 if (ctrl->value < 0 || ctrl->value > 255) {
861 v4l_err(decoder->client,
862 "invalid contrast setting %d\n",
863 ctrl->value);
864 return -ERANGE;
866 err = tvp514x_write_reg(decoder->client, REG_CONTRAST,
867 value);
868 if (err)
869 return err;
870 decoder->tvp514x_regs[REG_CONTRAST].val = value;
871 break;
872 case V4L2_CID_SATURATION:
873 if (ctrl->value < 0 || ctrl->value > 255) {
874 v4l_err(decoder->client,
875 "invalid saturation setting %d\n",
876 ctrl->value);
877 return -ERANGE;
879 err = tvp514x_write_reg(decoder->client, REG_SATURATION,
880 value);
881 if (err)
882 return err;
883 decoder->tvp514x_regs[REG_SATURATION].val = value;
884 break;
885 case V4L2_CID_HUE:
886 if (value == 180)
887 value = 0x7F;
888 else if (value == -180)
889 value = 0x80;
890 else if (value == 0)
891 value = 0;
892 else {
893 v4l_err(decoder->client,
894 "invalid hue setting %d\n",
895 ctrl->value);
896 return -ERANGE;
898 err = tvp514x_write_reg(decoder->client, REG_HUE,
899 value);
900 if (err)
901 return err;
902 decoder->tvp514x_regs[REG_HUE].val = value;
903 break;
904 case V4L2_CID_AUTOGAIN:
905 if (value == 1)
906 value = 0x0F;
907 else if (value == 0)
908 value = 0x0C;
909 else {
910 v4l_err(decoder->client,
911 "invalid auto gain setting %d\n",
912 ctrl->value);
913 return -ERANGE;
915 err = tvp514x_write_reg(decoder->client, REG_AFE_GAIN_CTRL,
916 value);
917 if (err)
918 return err;
919 decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val = value;
920 break;
921 default:
922 v4l_err(decoder->client,
923 "invalid control id %d\n", ctrl->id);
924 return err;
927 v4l_dbg(1, debug, decoder->client,
928 "Set Control: ID - %d - %d",
929 ctrl->id, ctrl->value);
931 return err;
935 * ioctl_enum_fmt_cap - Implement the CAPTURE buffer VIDIOC_ENUM_FMT ioctl
936 * @s: pointer to standard V4L2 device structure
937 * @fmt: standard V4L2 VIDIOC_ENUM_FMT ioctl structure
939 * Implement the VIDIOC_ENUM_FMT ioctl to enumerate supported formats
941 static int
942 ioctl_enum_fmt_cap(struct v4l2_int_device *s, struct v4l2_fmtdesc *fmt)
944 struct tvp514x_decoder *decoder = s->priv;
945 int index;
947 if (fmt == NULL)
948 return -EINVAL;
950 index = fmt->index;
951 if ((index >= decoder->num_fmts) || (index < 0))
952 return -EINVAL; /* Index out of bound */
954 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
955 return -EINVAL; /* only capture is supported */
957 memcpy(fmt, &decoder->fmt_list[index],
958 sizeof(struct v4l2_fmtdesc));
960 v4l_dbg(1, debug, decoder->client,
961 "Current FMT: index - %d (%s)",
962 decoder->fmt_list[index].index,
963 decoder->fmt_list[index].description);
964 return 0;
968 * ioctl_try_fmt_cap - Implement the CAPTURE buffer VIDIOC_TRY_FMT ioctl
969 * @s: pointer to standard V4L2 device structure
970 * @f: pointer to standard V4L2 VIDIOC_TRY_FMT ioctl structure
972 * Implement the VIDIOC_TRY_FMT ioctl for the CAPTURE buffer type. This
973 * ioctl is used to negotiate the image capture size and pixel format
974 * without actually making it take effect.
976 static int
977 ioctl_try_fmt_cap(struct v4l2_int_device *s, struct v4l2_format *f)
979 struct tvp514x_decoder *decoder = s->priv;
980 int ifmt;
981 struct v4l2_pix_format *pix;
982 enum tvp514x_std current_std;
984 if (f == NULL)
985 return -EINVAL;
987 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
988 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
990 pix = &f->fmt.pix;
992 /* Calculate height and width based on current standard */
993 current_std = tvp514x_get_current_std(decoder);
994 if (current_std == STD_INVALID)
995 return -EINVAL;
997 decoder->current_std = current_std;
998 pix->width = decoder->std_list[current_std].width;
999 pix->height = decoder->std_list[current_std].height;
1001 for (ifmt = 0; ifmt < decoder->num_fmts; ifmt++) {
1002 if (pix->pixelformat ==
1003 decoder->fmt_list[ifmt].pixelformat)
1004 break;
1006 if (ifmt == decoder->num_fmts)
1007 ifmt = 0; /* None of the format matched, select default */
1008 pix->pixelformat = decoder->fmt_list[ifmt].pixelformat;
1010 pix->field = V4L2_FIELD_INTERLACED;
1011 pix->bytesperline = pix->width * 2;
1012 pix->sizeimage = pix->bytesperline * pix->height;
1013 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
1014 pix->priv = 0;
1016 v4l_dbg(1, debug, decoder->client,
1017 "Try FMT: pixelformat - %s, bytesperline - %d"
1018 "Width - %d, Height - %d",
1019 decoder->fmt_list[ifmt].description, pix->bytesperline,
1020 pix->width, pix->height);
1021 return 0;
1025 * ioctl_s_fmt_cap - V4L2 decoder interface handler for VIDIOC_S_FMT ioctl
1026 * @s: pointer to standard V4L2 device structure
1027 * @f: pointer to standard V4L2 VIDIOC_S_FMT ioctl structure
1029 * If the requested format is supported, configures the HW to use that
1030 * format, returns error code if format not supported or HW can't be
1031 * correctly configured.
1033 static int
1034 ioctl_s_fmt_cap(struct v4l2_int_device *s, struct v4l2_format *f)
1036 struct tvp514x_decoder *decoder = s->priv;
1037 struct v4l2_pix_format *pix;
1038 int rval;
1040 if (f == NULL)
1041 return -EINVAL;
1043 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1044 return -EINVAL; /* only capture is supported */
1046 pix = &f->fmt.pix;
1047 rval = ioctl_try_fmt_cap(s, f);
1048 if (rval)
1049 return rval;
1051 decoder->pix = *pix;
1053 return rval;
1057 * ioctl_g_fmt_cap - V4L2 decoder interface handler for ioctl_g_fmt_cap
1058 * @s: pointer to standard V4L2 device structure
1059 * @f: pointer to standard V4L2 v4l2_format structure
1061 * Returns the decoder's current pixel format in the v4l2_format
1062 * parameter.
1064 static int
1065 ioctl_g_fmt_cap(struct v4l2_int_device *s, struct v4l2_format *f)
1067 struct tvp514x_decoder *decoder = s->priv;
1069 if (f == NULL)
1070 return -EINVAL;
1072 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1073 return -EINVAL; /* only capture is supported */
1075 f->fmt.pix = decoder->pix;
1077 v4l_dbg(1, debug, decoder->client,
1078 "Current FMT: bytesperline - %d"
1079 "Width - %d, Height - %d",
1080 decoder->pix.bytesperline,
1081 decoder->pix.width, decoder->pix.height);
1082 return 0;
1086 * ioctl_g_parm - V4L2 decoder interface handler for VIDIOC_G_PARM ioctl
1087 * @s: pointer to standard V4L2 device structure
1088 * @a: pointer to standard V4L2 VIDIOC_G_PARM ioctl structure
1090 * Returns the decoder's video CAPTURE parameters.
1092 static int
1093 ioctl_g_parm(struct v4l2_int_device *s, struct v4l2_streamparm *a)
1095 struct tvp514x_decoder *decoder = s->priv;
1096 struct v4l2_captureparm *cparm;
1097 enum tvp514x_std current_std;
1099 if (a == NULL)
1100 return -EINVAL;
1102 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1103 return -EINVAL; /* only capture is supported */
1105 memset(a, 0, sizeof(*a));
1106 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1108 /* get the current standard */
1109 current_std = tvp514x_get_current_std(decoder);
1110 if (current_std == STD_INVALID)
1111 return -EINVAL;
1113 decoder->current_std = current_std;
1115 cparm = &a->parm.capture;
1116 cparm->capability = V4L2_CAP_TIMEPERFRAME;
1117 cparm->timeperframe =
1118 decoder->std_list[current_std].standard.frameperiod;
1120 return 0;
1124 * ioctl_s_parm - V4L2 decoder interface handler for VIDIOC_S_PARM ioctl
1125 * @s: pointer to standard V4L2 device structure
1126 * @a: pointer to standard V4L2 VIDIOC_S_PARM ioctl structure
1128 * Configures the decoder to use the input parameters, if possible. If
1129 * not possible, returns the appropriate error code.
1131 static int
1132 ioctl_s_parm(struct v4l2_int_device *s, struct v4l2_streamparm *a)
1134 struct tvp514x_decoder *decoder = s->priv;
1135 struct v4l2_fract *timeperframe;
1136 enum tvp514x_std current_std;
1138 if (a == NULL)
1139 return -EINVAL;
1141 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1142 return -EINVAL; /* only capture is supported */
1144 timeperframe = &a->parm.capture.timeperframe;
1146 /* get the current standard */
1147 current_std = tvp514x_get_current_std(decoder);
1148 if (current_std == STD_INVALID)
1149 return -EINVAL;
1151 decoder->current_std = current_std;
1153 *timeperframe =
1154 decoder->std_list[current_std].standard.frameperiod;
1156 return 0;
1160 * ioctl_g_ifparm - V4L2 decoder interface handler for vidioc_int_g_ifparm_num
1161 * @s: pointer to standard V4L2 device structure
1162 * @p: pointer to standard V4L2 vidioc_int_g_ifparm_num ioctl structure
1164 * Gets slave interface parameters.
1165 * Calculates the required xclk value to support the requested
1166 * clock parameters in p. This value is returned in the p
1167 * parameter.
1169 static int ioctl_g_ifparm(struct v4l2_int_device *s, struct v4l2_ifparm *p)
1171 struct tvp514x_decoder *decoder = s->priv;
1172 int rval;
1174 if (p == NULL)
1175 return -EINVAL;
1177 if (NULL == decoder->pdata->ifparm)
1178 return -EINVAL;
1180 rval = decoder->pdata->ifparm(p);
1181 if (rval) {
1182 v4l_err(decoder->client, "g_ifparm.Err[%d]\n", rval);
1183 return rval;
1186 p->u.bt656.clock_curr = TVP514X_XCLK_BT656;
1188 return 0;
1192 * ioctl_g_priv - V4L2 decoder interface handler for vidioc_int_g_priv_num
1193 * @s: pointer to standard V4L2 device structure
1194 * @p: void pointer to hold decoder's private data address
1196 * Returns device's (decoder's) private data area address in p parameter
1198 static int ioctl_g_priv(struct v4l2_int_device *s, void *p)
1200 struct tvp514x_decoder *decoder = s->priv;
1202 if (NULL == decoder->pdata->priv_data_set)
1203 return -EINVAL;
1205 return decoder->pdata->priv_data_set(p);
1209 * ioctl_s_power - V4L2 decoder interface handler for vidioc_int_s_power_num
1210 * @s: pointer to standard V4L2 device structure
1211 * @on: power state to which device is to be set
1213 * Sets devices power state to requrested state, if possible.
1215 static int ioctl_s_power(struct v4l2_int_device *s, enum v4l2_power on)
1217 struct tvp514x_decoder *decoder = s->priv;
1218 int err = 0;
1220 switch (on) {
1221 case V4L2_POWER_OFF:
1222 /* Power Down Sequence */
1223 err =
1224 tvp514x_write_reg(decoder->client, REG_OPERATION_MODE,
1225 0x01);
1226 /* Disable mux for TVP5146/47 decoder data path */
1227 if (decoder->pdata->power_set)
1228 err |= decoder->pdata->power_set(on);
1229 decoder->state = STATE_NOT_DETECTED;
1230 break;
1232 case V4L2_POWER_STANDBY:
1233 if (decoder->pdata->power_set)
1234 err = decoder->pdata->power_set(on);
1235 break;
1237 case V4L2_POWER_ON:
1238 /* Enable mux for TVP5146/47 decoder data path */
1239 if ((decoder->pdata->power_set) &&
1240 (decoder->state == STATE_NOT_DETECTED)) {
1241 int i;
1242 struct tvp514x_init_seq *int_seq =
1243 (struct tvp514x_init_seq *)
1244 decoder->id->driver_data;
1246 err = decoder->pdata->power_set(on);
1248 /* Power Up Sequence */
1249 for (i = 0; i < int_seq->no_regs; i++) {
1250 err |= tvp514x_write_reg(decoder->client,
1251 int_seq->init_reg_seq[i].reg,
1252 int_seq->init_reg_seq[i].val);
1254 /* Detect the sensor is not already detected */
1255 err |= tvp514x_detect(decoder);
1256 if (err) {
1257 v4l_err(decoder->client,
1258 "Unable to detect decoder\n");
1259 return err;
1262 err |= tvp514x_configure(decoder);
1263 break;
1265 default:
1266 err = -ENODEV;
1267 break;
1270 return err;
1274 * ioctl_init - V4L2 decoder interface handler for VIDIOC_INT_INIT
1275 * @s: pointer to standard V4L2 device structure
1277 * Initialize the decoder device (calls tvp514x_configure())
1279 static int ioctl_init(struct v4l2_int_device *s)
1281 struct tvp514x_decoder *decoder = s->priv;
1283 /* Set default standard to auto */
1284 decoder->tvp514x_regs[REG_VIDEO_STD].val =
1285 VIDEO_STD_AUTO_SWITCH_BIT;
1287 return tvp514x_configure(decoder);
1291 * ioctl_dev_exit - V4L2 decoder interface handler for vidioc_int_dev_exit_num
1292 * @s: pointer to standard V4L2 device structure
1294 * Delinitialise the dev. at slave detach. The complement of ioctl_dev_init.
1296 static int ioctl_dev_exit(struct v4l2_int_device *s)
1298 return 0;
1302 * ioctl_dev_init - V4L2 decoder interface handler for vidioc_int_dev_init_num
1303 * @s: pointer to standard V4L2 device structure
1305 * Initialise the device when slave attaches to the master. Returns 0 if
1306 * TVP5146/47 device could be found, otherwise returns appropriate error.
1308 static int ioctl_dev_init(struct v4l2_int_device *s)
1310 struct tvp514x_decoder *decoder = s->priv;
1311 int err;
1313 err = tvp514x_detect(decoder);
1314 if (err < 0) {
1315 v4l_err(decoder->client,
1316 "Unable to detect decoder\n");
1317 return err;
1320 v4l_info(decoder->client,
1321 "chip version 0x%.2x detected\n", decoder->ver);
1323 return 0;
1326 static struct v4l2_int_ioctl_desc tvp514x_ioctl_desc[] = {
1327 {vidioc_int_dev_init_num, (v4l2_int_ioctl_func*) ioctl_dev_init},
1328 {vidioc_int_dev_exit_num, (v4l2_int_ioctl_func*) ioctl_dev_exit},
1329 {vidioc_int_s_power_num, (v4l2_int_ioctl_func*) ioctl_s_power},
1330 {vidioc_int_g_priv_num, (v4l2_int_ioctl_func*) ioctl_g_priv},
1331 {vidioc_int_g_ifparm_num, (v4l2_int_ioctl_func*) ioctl_g_ifparm},
1332 {vidioc_int_init_num, (v4l2_int_ioctl_func*) ioctl_init},
1333 {vidioc_int_enum_fmt_cap_num,
1334 (v4l2_int_ioctl_func *) ioctl_enum_fmt_cap},
1335 {vidioc_int_try_fmt_cap_num,
1336 (v4l2_int_ioctl_func *) ioctl_try_fmt_cap},
1337 {vidioc_int_g_fmt_cap_num,
1338 (v4l2_int_ioctl_func *) ioctl_g_fmt_cap},
1339 {vidioc_int_s_fmt_cap_num,
1340 (v4l2_int_ioctl_func *) ioctl_s_fmt_cap},
1341 {vidioc_int_g_parm_num, (v4l2_int_ioctl_func *) ioctl_g_parm},
1342 {vidioc_int_s_parm_num, (v4l2_int_ioctl_func *) ioctl_s_parm},
1343 {vidioc_int_queryctrl_num,
1344 (v4l2_int_ioctl_func *) ioctl_queryctrl},
1345 {vidioc_int_g_ctrl_num, (v4l2_int_ioctl_func *) ioctl_g_ctrl},
1346 {vidioc_int_s_ctrl_num, (v4l2_int_ioctl_func *) ioctl_s_ctrl},
1347 {vidioc_int_querystd_num, (v4l2_int_ioctl_func *) ioctl_querystd},
1348 {vidioc_int_s_std_num, (v4l2_int_ioctl_func *) ioctl_s_std},
1349 {vidioc_int_s_video_routing_num,
1350 (v4l2_int_ioctl_func *) ioctl_s_routing},
1353 static struct tvp514x_decoder tvp514x_dev = {
1354 .state = STATE_NOT_DETECTED,
1356 .fmt_list = tvp514x_fmt_list,
1357 .num_fmts = ARRAY_SIZE(tvp514x_fmt_list),
1359 .pix = { /* Default to NTSC 8-bit YUV 422 */
1360 .width = NTSC_NUM_ACTIVE_PIXELS,
1361 .height = NTSC_NUM_ACTIVE_LINES,
1362 .pixelformat = V4L2_PIX_FMT_UYVY,
1363 .field = V4L2_FIELD_INTERLACED,
1364 .bytesperline = NTSC_NUM_ACTIVE_PIXELS * 2,
1365 .sizeimage =
1366 NTSC_NUM_ACTIVE_PIXELS * 2 * NTSC_NUM_ACTIVE_LINES,
1367 .colorspace = V4L2_COLORSPACE_SMPTE170M,
1370 .current_std = STD_NTSC_MJ,
1371 .std_list = tvp514x_std_list,
1372 .num_stds = ARRAY_SIZE(tvp514x_std_list),
1373 .v4l2_int_device = {
1374 .module = THIS_MODULE,
1375 .name = TVP514X_MODULE_NAME,
1376 .type = v4l2_int_type_slave,
1378 .tvp514x_slave = {
1379 .ioctls = tvp514x_ioctl_desc,
1380 .num_ioctls = ARRAY_SIZE(tvp514x_ioctl_desc),
1385 * tvp514x_probe - decoder driver i2c probe handler
1386 * @client: i2c driver client device structure
1388 * Register decoder as an i2c client device and V4L2
1389 * device.
1391 static int
1392 tvp514x_probe(struct i2c_client *client, const struct i2c_device_id *id)
1394 struct tvp514x_decoder *decoder;
1395 int err;
1397 /* Check if the adapter supports the needed features */
1398 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1399 return -EIO;
1401 decoder = kzalloc(sizeof(*decoder), GFP_KERNEL);
1402 if (!decoder)
1403 return -ENOMEM;
1405 if (!client->dev.platform_data) {
1406 v4l_err(client, "No platform data!!\n");
1407 err = -ENODEV;
1408 goto out_free;
1411 *decoder = tvp514x_dev;
1412 decoder->v4l2_int_device.priv = decoder;
1413 decoder->pdata = client->dev.platform_data;
1414 decoder->v4l2_int_device.u.slave = &decoder->tvp514x_slave;
1415 memcpy(decoder->tvp514x_regs, tvp514x_reg_list_default,
1416 sizeof(tvp514x_reg_list_default));
1418 * Fetch platform specific data, and configure the
1419 * tvp514x_reg_list[] accordingly. Since this is one
1420 * time configuration, no need to preserve.
1422 decoder->tvp514x_regs[REG_OUTPUT_FORMATTER2].val |=
1423 (decoder->pdata->clk_polarity << 1);
1424 decoder->tvp514x_regs[REG_SYNC_CONTROL].val |=
1425 ((decoder->pdata->hs_polarity << 2) |
1426 (decoder->pdata->vs_polarity << 3));
1428 * Save the id data, required for power up sequence
1430 decoder->id = (struct i2c_device_id *)id;
1431 /* Attach to Master */
1432 strcpy(decoder->v4l2_int_device.u.slave->attach_to,
1433 decoder->pdata->master);
1434 decoder->client = client;
1435 i2c_set_clientdata(client, decoder);
1437 /* Register with V4L2 layer as slave device */
1438 err = v4l2_int_device_register(&decoder->v4l2_int_device);
1439 if (err) {
1440 i2c_set_clientdata(client, NULL);
1441 v4l_err(client,
1442 "Unable to register to v4l2. Err[%d]\n", err);
1443 goto out_free;
1445 } else
1446 v4l_info(client, "Registered to v4l2 master %s!!\n",
1447 decoder->pdata->master);
1448 return 0;
1450 out_free:
1451 kfree(decoder);
1452 return err;
1456 * tvp514x_remove - decoder driver i2c remove handler
1457 * @client: i2c driver client device structure
1459 * Unregister decoder as an i2c client device and V4L2
1460 * device. Complement of tvp514x_probe().
1462 static int __exit tvp514x_remove(struct i2c_client *client)
1464 struct tvp514x_decoder *decoder = i2c_get_clientdata(client);
1466 if (!client->adapter)
1467 return -ENODEV; /* our client isn't attached */
1469 v4l2_int_device_unregister(&decoder->v4l2_int_device);
1470 i2c_set_clientdata(client, NULL);
1471 kfree(decoder);
1472 return 0;
1475 * TVP5146 Init/Power on Sequence
1477 static const struct tvp514x_reg tvp5146_init_reg_seq[] = {
1478 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
1479 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1480 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
1481 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1482 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1483 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1484 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1485 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1486 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
1487 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1488 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
1490 static const struct tvp514x_init_seq tvp5146_init = {
1491 .no_regs = ARRAY_SIZE(tvp5146_init_reg_seq),
1492 .init_reg_seq = tvp5146_init_reg_seq,
1495 * TVP5147 Init/Power on Sequence
1497 static const struct tvp514x_reg tvp5147_init_reg_seq[] = {
1498 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
1499 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1500 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
1501 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1502 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1503 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1504 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1505 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1506 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x16},
1507 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1508 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xA0},
1509 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x16},
1510 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1511 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1512 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1513 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
1514 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1515 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
1517 static const struct tvp514x_init_seq tvp5147_init = {
1518 .no_regs = ARRAY_SIZE(tvp5147_init_reg_seq),
1519 .init_reg_seq = tvp5147_init_reg_seq,
1522 * TVP5146M2/TVP5147M1 Init/Power on Sequence
1524 static const struct tvp514x_reg tvp514xm_init_reg_seq[] = {
1525 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1526 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
1528 static const struct tvp514x_init_seq tvp514xm_init = {
1529 .no_regs = ARRAY_SIZE(tvp514xm_init_reg_seq),
1530 .init_reg_seq = tvp514xm_init_reg_seq,
1533 * I2C Device Table -
1535 * name - Name of the actual device/chip.
1536 * driver_data - Driver data
1538 static const struct i2c_device_id tvp514x_id[] = {
1539 {"tvp5146", (unsigned long)&tvp5146_init},
1540 {"tvp5146m2", (unsigned long)&tvp514xm_init},
1541 {"tvp5147", (unsigned long)&tvp5147_init},
1542 {"tvp5147m1", (unsigned long)&tvp514xm_init},
1546 MODULE_DEVICE_TABLE(i2c, tvp514x_id);
1548 static struct i2c_driver tvp514x_i2c_driver = {
1549 .driver = {
1550 .name = TVP514X_MODULE_NAME,
1551 .owner = THIS_MODULE,
1553 .probe = tvp514x_probe,
1554 .remove = __exit_p(tvp514x_remove),
1555 .id_table = tvp514x_id,
1559 * tvp514x_init
1561 * Module init function
1563 static int __init tvp514x_init(void)
1565 return i2c_add_driver(&tvp514x_i2c_driver);
1569 * tvp514x_cleanup
1571 * Module exit function
1573 static void __exit tvp514x_cleanup(void)
1575 i2c_del_driver(&tvp514x_i2c_driver);
1578 module_init(tvp514x_init);
1579 module_exit(tvp514x_cleanup);
1581 MODULE_AUTHOR("Texas Instruments");
1582 MODULE_DESCRIPTION("TVP514X linux decoder driver");
1583 MODULE_LICENSE("GPL");