blob: f0b2b8ed2fe46b6024749e4216b95e71963c7675 [file] [log] [blame]
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001/*
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 */
48static int debug;
49module_param(debug, bool, 0644);
50MODULE_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 */
61enum 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 */
70enum 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 */
82struct tvp514x_std_info {
83 unsigned long width;
84 unsigned long height;
85 u8 video_std;
86 struct v4l2_standard standard;
87};
88
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -030089static struct tvp514x_reg tvp514x_reg_list_default[0x40];
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030090/**
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -030091 * struct tvp514x_decoder - TVP5146/47 decoder object
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030092 * @v4l2_int_device: Slave handle
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -030093 * @tvp514x_slave: Slave pointer which is used by @v4l2_int_device
94 * @tvp514x_regs: copy of hw's regs with preset values.
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030095 * @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 */
108struct tvp514x_decoder {
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300109 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)];
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300112 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 */
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300132static struct tvp514x_reg tvp514x_reg_list_default[] = {
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300133 {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 */
198static 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 */
214static 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 */
248static 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 */
262static int tvp514x_read_reg(struct i2c_client *client, u8 reg)
263{
264 int err;
265 int retry = 0;
266read_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 */
285static int tvp514x_write_reg(struct i2c_client *client, u8 reg, u8 val)
286{
287 int err;
288 int retry = 0;
289write_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 */
314static 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 */
342static 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 */
372static 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 */
424static int tvp514x_configure(struct tvp514x_decoder *decoder)
425{
426 int err;
427
428 /* common register initialization */
429 err =
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300430 tvp514x_write_regs(decoder->client, decoder->tvp514x_regs);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300431 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 */
448static 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 */
494static 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 */
567static 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;
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300588 decoder->tvp514x_regs[REG_VIDEO_STD].val =
589 decoder->std_list[i].video_std;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300590
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 */
605static 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
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300634 decoder->tvp514x_regs[REG_INPUT_SEL].val = input_sel;
635 decoder->tvp514x_regs[REG_OUTPUT_FORMATTER1].val = output_sel;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300636
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
Roel Kluin320a4642009-02-09 18:57:06 -0300695 if ((current_std == STD_INVALID) || (try_count <= 0))
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300696 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 */
717static int
718ioctl_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 same as standard one (0-255),
729 * so make use of standard API provided.
730 */
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)
738 */
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)
744 */
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;
757 }
758
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);
765
766 return err;
767}
768
769/**
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
773 *
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.
777 */
778static int
779ioctl_g_ctrl(struct v4l2_int_device *s, struct v4l2_control *ctrl)
780{
781 struct tvp514x_decoder *decoder = s->priv;
782
783 if (ctrl == NULL)
784 return -EINVAL;
785
786 switch (ctrl->id) {
787 case V4L2_CID_BRIGHTNESS:
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300788 ctrl->value = decoder->tvp514x_regs[REG_BRIGHTNESS].val;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300789 break;
790 case V4L2_CID_CONTRAST:
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300791 ctrl->value = decoder->tvp514x_regs[REG_CONTRAST].val;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300792 break;
793 case V4L2_CID_SATURATION:
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300794 ctrl->value = decoder->tvp514x_regs[REG_SATURATION].val;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300795 break;
796 case V4L2_CID_HUE:
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300797 ctrl->value = decoder->tvp514x_regs[REG_HUE].val;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300798 if (ctrl->value == 0x7F)
799 ctrl->value = 180;
800 else if (ctrl->value == 0x80)
801 ctrl->value = -180;
802 else
803 ctrl->value = 0;
804
805 break;
806 case V4L2_CID_AUTOGAIN:
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300807 ctrl->value = decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300808 if ((ctrl->value & 0x3) == 3)
809 ctrl->value = 1;
810 else
811 ctrl->value = 0;
812
813 break;
814 default:
815 v4l_err(decoder->client,
816 "invalid control id %d\n", ctrl->id);
817 return -EINVAL;
818 }
819
820 v4l_dbg(1, debug, decoder->client,
821 "Get Control: ID - %d - %d",
822 ctrl->id, ctrl->value);
823 return 0;
824}
825
826/**
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
830 *
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.
833 */
834static int
835ioctl_s_ctrl(struct v4l2_int_device *s, struct v4l2_control *ctrl)
836{
837 struct tvp514x_decoder *decoder = s->priv;
838 int err = -EINVAL, value;
839
840 if (ctrl == NULL)
841 return err;
842
843 value = (__s32) ctrl->value;
844
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;
852 }
853 err = tvp514x_write_reg(decoder->client, REG_BRIGHTNESS,
854 value);
855 if (err)
856 return err;
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300857 decoder->tvp514x_regs[REG_BRIGHTNESS].val = value;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300858 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;
865 }
866 err = tvp514x_write_reg(decoder->client, REG_CONTRAST,
867 value);
868 if (err)
869 return err;
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300870 decoder->tvp514x_regs[REG_CONTRAST].val = value;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300871 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;
878 }
879 err = tvp514x_write_reg(decoder->client, REG_SATURATION,
880 value);
881 if (err)
882 return err;
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300883 decoder->tvp514x_regs[REG_SATURATION].val = value;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300884 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;
897 }
898 err = tvp514x_write_reg(decoder->client, REG_HUE,
899 value);
900 if (err)
901 return err;
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300902 decoder->tvp514x_regs[REG_HUE].val = value;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300903 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;
914 }
915 err = tvp514x_write_reg(decoder->client, REG_AFE_GAIN_CTRL,
916 value);
917 if (err)
918 return err;
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300919 decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val = value;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300920 break;
921 default:
922 v4l_err(decoder->client,
923 "invalid control id %d\n", ctrl->id);
924 return err;
925 }
926
927 v4l_dbg(1, debug, decoder->client,
928 "Set Control: ID - %d - %d",
929 ctrl->id, ctrl->value);
930
931 return err;
932}
933
934/**
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
938 *
939 * Implement the VIDIOC_ENUM_FMT ioctl to enumerate supported formats
940 */
941static int
942ioctl_enum_fmt_cap(struct v4l2_int_device *s, struct v4l2_fmtdesc *fmt)
943{
944 struct tvp514x_decoder *decoder = s->priv;
945 int index;
946
947 if (fmt == NULL)
948 return -EINVAL;
949
950 index = fmt->index;
951 if ((index >= decoder->num_fmts) || (index < 0))
952 return -EINVAL; /* Index out of bound */
953
954 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
955 return -EINVAL; /* only capture is supported */
956
957 memcpy(fmt, &decoder->fmt_list[index],
958 sizeof(struct v4l2_fmtdesc));
959
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;
965}
966
967/**
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
971 *
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.
975 */
976static int
977ioctl_try_fmt_cap(struct v4l2_int_device *s, struct v4l2_format *f)
978{
979 struct tvp514x_decoder *decoder = s->priv;
980 int ifmt;
981 struct v4l2_pix_format *pix;
982 enum tvp514x_std current_std;
983
984 if (f == NULL)
985 return -EINVAL;
986
987 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
988 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
989
990 pix = &f->fmt.pix;
991
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;
996
997 decoder->current_std = current_std;
998 pix->width = decoder->std_list[current_std].width;
999 pix->height = decoder->std_list[current_std].height;
1000
1001 for (ifmt = 0; ifmt < decoder->num_fmts; ifmt++) {
1002 if (pix->pixelformat ==
1003 decoder->fmt_list[ifmt].pixelformat)
1004 break;
1005 }
1006 if (ifmt == decoder->num_fmts)
1007 ifmt = 0; /* None of the format matched, select default */
1008 pix->pixelformat = decoder->fmt_list[ifmt].pixelformat;
1009
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;
1015
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;
1022}
1023
1024/**
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
1028 *
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.
1032 */
1033static int
1034ioctl_s_fmt_cap(struct v4l2_int_device *s, struct v4l2_format *f)
1035{
1036 struct tvp514x_decoder *decoder = s->priv;
1037 struct v4l2_pix_format *pix;
1038 int rval;
1039
1040 if (f == NULL)
1041 return -EINVAL;
1042
1043 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1044 return -EINVAL; /* only capture is supported */
1045
1046 pix = &f->fmt.pix;
1047 rval = ioctl_try_fmt_cap(s, f);
1048 if (rval)
1049 return rval;
1050
1051 decoder->pix = *pix;
1052
1053 return rval;
1054}
1055
1056/**
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
1060 *
1061 * Returns the decoder's current pixel format in the v4l2_format
1062 * parameter.
1063 */
1064static int
1065ioctl_g_fmt_cap(struct v4l2_int_device *s, struct v4l2_format *f)
1066{
1067 struct tvp514x_decoder *decoder = s->priv;
1068
1069 if (f == NULL)
1070 return -EINVAL;
1071
1072 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1073 return -EINVAL; /* only capture is supported */
1074
1075 f->fmt.pix = decoder->pix;
1076
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;
1083}
1084
1085/**
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
1089 *
1090 * Returns the decoder's video CAPTURE parameters.
1091 */
1092static int
1093ioctl_g_parm(struct v4l2_int_device *s, struct v4l2_streamparm *a)
1094{
1095 struct tvp514x_decoder *decoder = s->priv;
1096 struct v4l2_captureparm *cparm;
1097 enum tvp514x_std current_std;
1098
1099 if (a == NULL)
1100 return -EINVAL;
1101
1102 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1103 return -EINVAL; /* only capture is supported */
1104
1105 memset(a, 0, sizeof(*a));
1106 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1107
1108 /* get the current standard */
1109 current_std = tvp514x_get_current_std(decoder);
1110 if (current_std == STD_INVALID)
1111 return -EINVAL;
1112
1113 decoder->current_std = current_std;
1114
1115 cparm = &a->parm.capture;
1116 cparm->capability = V4L2_CAP_TIMEPERFRAME;
1117 cparm->timeperframe =
1118 decoder->std_list[current_std].standard.frameperiod;
1119
1120 return 0;
1121}
1122
1123/**
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
1127 *
1128 * Configures the decoder to use the input parameters, if possible. If
1129 * not possible, returns the appropriate error code.
1130 */
1131static int
1132ioctl_s_parm(struct v4l2_int_device *s, struct v4l2_streamparm *a)
1133{
1134 struct tvp514x_decoder *decoder = s->priv;
1135 struct v4l2_fract *timeperframe;
1136 enum tvp514x_std current_std;
1137
1138 if (a == NULL)
1139 return -EINVAL;
1140
1141 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1142 return -EINVAL; /* only capture is supported */
1143
1144 timeperframe = &a->parm.capture.timeperframe;
1145
1146 /* get the current standard */
1147 current_std = tvp514x_get_current_std(decoder);
1148 if (current_std == STD_INVALID)
1149 return -EINVAL;
1150
1151 decoder->current_std = current_std;
1152
1153 *timeperframe =
1154 decoder->std_list[current_std].standard.frameperiod;
1155
1156 return 0;
1157}
1158
1159/**
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
1163 *
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.
1168 */
1169static int ioctl_g_ifparm(struct v4l2_int_device *s, struct v4l2_ifparm *p)
1170{
1171 struct tvp514x_decoder *decoder = s->priv;
1172 int rval;
1173
1174 if (p == NULL)
1175 return -EINVAL;
1176
1177 if (NULL == decoder->pdata->ifparm)
1178 return -EINVAL;
1179
1180 rval = decoder->pdata->ifparm(p);
1181 if (rval) {
1182 v4l_err(decoder->client, "g_ifparm.Err[%d]\n", rval);
1183 return rval;
1184 }
1185
1186 p->u.bt656.clock_curr = TVP514X_XCLK_BT656;
1187
1188 return 0;
1189}
1190
1191/**
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
1195 *
1196 * Returns device's (decoder's) private data area address in p parameter
1197 */
1198static int ioctl_g_priv(struct v4l2_int_device *s, void *p)
1199{
1200 struct tvp514x_decoder *decoder = s->priv;
1201
1202 if (NULL == decoder->pdata->priv_data_set)
1203 return -EINVAL;
1204
1205 return decoder->pdata->priv_data_set(p);
1206}
1207
1208/**
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
1212 *
1213 * Sets devices power state to requrested state, if possible.
1214 */
1215static int ioctl_s_power(struct v4l2_int_device *s, enum v4l2_power on)
1216{
1217 struct tvp514x_decoder *decoder = s->priv;
1218 int err = 0;
1219
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;
1231
1232 case V4L2_POWER_STANDBY:
1233 if (decoder->pdata->power_set)
1234 err = decoder->pdata->power_set(on);
1235 break;
1236
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;
1245
1246 err = decoder->pdata->power_set(on);
1247
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);
1253 }
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;
1260 }
1261 }
1262 err |= tvp514x_configure(decoder);
1263 break;
1264
1265 default:
1266 err = -ENODEV;
1267 break;
1268 }
1269
1270 return err;
1271}
1272
1273/**
1274 * ioctl_init - V4L2 decoder interface handler for VIDIOC_INT_INIT
1275 * @s: pointer to standard V4L2 device structure
1276 *
1277 * Initialize the decoder device (calls tvp514x_configure())
1278 */
1279static int ioctl_init(struct v4l2_int_device *s)
1280{
1281 struct tvp514x_decoder *decoder = s->priv;
1282
1283 /* Set default standard to auto */
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001284 decoder->tvp514x_regs[REG_VIDEO_STD].val =
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001285 VIDEO_STD_AUTO_SWITCH_BIT;
1286
1287 return tvp514x_configure(decoder);
1288}
1289
1290/**
1291 * ioctl_dev_exit - V4L2 decoder interface handler for vidioc_int_dev_exit_num
1292 * @s: pointer to standard V4L2 device structure
1293 *
1294 * Delinitialise the dev. at slave detach. The complement of ioctl_dev_init.
1295 */
1296static int ioctl_dev_exit(struct v4l2_int_device *s)
1297{
1298 return 0;
1299}
1300
1301/**
1302 * ioctl_dev_init - V4L2 decoder interface handler for vidioc_int_dev_init_num
1303 * @s: pointer to standard V4L2 device structure
1304 *
1305 * Initialise the device when slave attaches to the master. Returns 0 if
1306 * TVP5146/47 device could be found, otherwise returns appropriate error.
1307 */
1308static int ioctl_dev_init(struct v4l2_int_device *s)
1309{
1310 struct tvp514x_decoder *decoder = s->priv;
1311 int err;
1312
1313 err = tvp514x_detect(decoder);
1314 if (err < 0) {
1315 v4l_err(decoder->client,
1316 "Unable to detect decoder\n");
1317 return err;
1318 }
1319
1320 v4l_info(decoder->client,
1321 "chip version 0x%.2x detected\n", decoder->ver);
1322
1323 return 0;
1324}
1325
1326static 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},
1351};
1352
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001353static struct tvp514x_decoder tvp514x_dev = {
1354 .state = STATE_NOT_DETECTED,
1355
1356 .fmt_list = tvp514x_fmt_list,
1357 .num_fmts = ARRAY_SIZE(tvp514x_fmt_list),
1358
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,
1368 },
1369
1370 .current_std = STD_NTSC_MJ,
1371 .std_list = tvp514x_std_list,
1372 .num_stds = ARRAY_SIZE(tvp514x_std_list),
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001373 .v4l2_int_device = {
1374 .module = THIS_MODULE,
1375 .name = TVP514X_MODULE_NAME,
1376 .type = v4l2_int_type_slave,
1377 },
1378 .tvp514x_slave = {
1379 .ioctls = tvp514x_ioctl_desc,
1380 .num_ioctls = ARRAY_SIZE(tvp514x_ioctl_desc),
1381 },
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001382};
1383
1384/**
1385 * tvp514x_probe - decoder driver i2c probe handler
1386 * @client: i2c driver client device structure
1387 *
1388 * Register decoder as an i2c client device and V4L2
1389 * device.
1390 */
1391static int
1392tvp514x_probe(struct i2c_client *client, const struct i2c_device_id *id)
1393{
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001394 struct tvp514x_decoder *decoder;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001395 int err;
1396
1397 /* Check if the adapter supports the needed features */
1398 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1399 return -EIO;
1400
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001401 decoder = kzalloc(sizeof(*decoder), GFP_KERNEL);
1402 if (!decoder)
1403 return -ENOMEM;
1404
1405 if (!client->dev.platform_data) {
Sebastian Andrzej Siewior0c37dd7a2009-01-08 12:49:17 -03001406 v4l_err(client, "No platform data!!\n");
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001407 err = -ENODEV;
1408 goto out_free;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001409 }
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001410
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));
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001417 /*
1418 * Fetch platform specific data, and configure the
1419 * tvp514x_reg_list[] accordingly. Since this is one
1420 * time configuration, no need to preserve.
1421 */
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001422 decoder->tvp514x_regs[REG_OUTPUT_FORMATTER2].val |=
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001423 (decoder->pdata->clk_polarity << 1);
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001424 decoder->tvp514x_regs[REG_SYNC_CONTROL].val |=
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001425 ((decoder->pdata->hs_polarity << 2) |
1426 (decoder->pdata->vs_polarity << 3));
1427 /*
1428 * Save the id data, required for power up sequence
1429 */
1430 decoder->id = (struct i2c_device_id *)id;
1431 /* Attach to Master */
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001432 strcpy(decoder->v4l2_int_device.u.slave->attach_to,
1433 decoder->pdata->master);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001434 decoder->client = client;
1435 i2c_set_clientdata(client, decoder);
1436
1437 /* Register with V4L2 layer as slave device */
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001438 err = v4l2_int_device_register(&decoder->v4l2_int_device);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001439 if (err) {
1440 i2c_set_clientdata(client, NULL);
1441 v4l_err(client,
1442 "Unable to register to v4l2. Err[%d]\n", err);
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001443 goto out_free;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001444
1445 } else
1446 v4l_info(client, "Registered to v4l2 master %s!!\n",
1447 decoder->pdata->master);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001448 return 0;
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001449
1450out_free:
1451 kfree(decoder);
1452 return err;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001453}
1454
1455/**
1456 * tvp514x_remove - decoder driver i2c remove handler
1457 * @client: i2c driver client device structure
1458 *
1459 * Unregister decoder as an i2c client device and V4L2
1460 * device. Complement of tvp514x_probe().
1461 */
1462static int __exit tvp514x_remove(struct i2c_client *client)
1463{
1464 struct tvp514x_decoder *decoder = i2c_get_clientdata(client);
1465
1466 if (!client->adapter)
1467 return -ENODEV; /* our client isn't attached */
1468
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001469 v4l2_int_device_unregister(&decoder->v4l2_int_device);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001470 i2c_set_clientdata(client, NULL);
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001471 kfree(decoder);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001472 return 0;
1473}
1474/*
1475 * TVP5146 Init/Power on Sequence
1476 */
1477static 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},
1489};
1490static const struct tvp514x_init_seq tvp5146_init = {
1491 .no_regs = ARRAY_SIZE(tvp5146_init_reg_seq),
1492 .init_reg_seq = tvp5146_init_reg_seq,
1493};
1494/*
1495 * TVP5147 Init/Power on Sequence
1496 */
1497static 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},
1516};
1517static const struct tvp514x_init_seq tvp5147_init = {
1518 .no_regs = ARRAY_SIZE(tvp5147_init_reg_seq),
1519 .init_reg_seq = tvp5147_init_reg_seq,
1520};
1521/*
1522 * TVP5146M2/TVP5147M1 Init/Power on Sequence
1523 */
1524static const struct tvp514x_reg tvp514xm_init_reg_seq[] = {
1525 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1526 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
1527};
1528static const struct tvp514x_init_seq tvp514xm_init = {
1529 .no_regs = ARRAY_SIZE(tvp514xm_init_reg_seq),
1530 .init_reg_seq = tvp514xm_init_reg_seq,
1531};
1532/*
1533 * I2C Device Table -
1534 *
1535 * name - Name of the actual device/chip.
1536 * driver_data - Driver data
1537 */
1538static 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},
1543 {},
1544};
1545
1546MODULE_DEVICE_TABLE(i2c, tvp514x_id);
1547
1548static struct i2c_driver tvp514x_i2c_driver = {
1549 .driver = {
1550 .name = TVP514X_MODULE_NAME,
1551 .owner = THIS_MODULE,
1552 },
1553 .probe = tvp514x_probe,
1554 .remove = __exit_p(tvp514x_remove),
1555 .id_table = tvp514x_id,
1556};
1557
1558/**
1559 * tvp514x_init
1560 *
1561 * Module init function
1562 */
1563static int __init tvp514x_init(void)
1564{
1565 return i2c_add_driver(&tvp514x_i2c_driver);
1566}
1567
1568/**
1569 * tvp514x_cleanup
1570 *
1571 * Module exit function
1572 */
1573static void __exit tvp514x_cleanup(void)
1574{
1575 i2c_del_driver(&tvp514x_i2c_driver);
1576}
1577
1578module_init(tvp514x_init);
1579module_exit(tvp514x_cleanup);
1580
1581MODULE_AUTHOR("Texas Instruments");
1582MODULE_DESCRIPTION("TVP514X linux decoder driver");
1583MODULE_LICENSE("GPL");