blob: d5e10215a28f46197d480aa51156cab605d7ef21 [file] [log] [blame]
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001/*
Mauro Carvalho Chehabcb7a01a2012-08-14 16:23:43 -03002 * drivers/media/i2c/tvp514x.c
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03003 *
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030033#include <linux/delay.h>
34#include <linux/videodev2.h>
Paul Gortmaker7a707b82011-07-03 14:03:12 -040035#include <linux/module.h>
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -030036
37#include <media/v4l2-device.h>
38#include <media/v4l2-common.h>
Hans Verkuil83811912010-05-09 06:50:25 -030039#include <media/v4l2-mediabus.h>
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -030040#include <media/v4l2-chip-ident.h>
Hans Verkuilcf6832a2010-12-12 08:45:22 -030041#include <media/v4l2-ctrls.h>
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030042#include <media/tvp514x.h>
43
44#include "tvp514x_regs.h"
45
46/* Module Name */
47#define TVP514X_MODULE_NAME "tvp514x"
48
49/* Private macros for TVP */
50#define I2C_RETRY_COUNT (5)
51#define LOCK_RETRY_COUNT (5)
52#define LOCK_RETRY_DELAY (200)
53
54/* Debug functions */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103055static bool debug;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030056module_param(debug, bool, 0644);
57MODULE_PARM_DESC(debug, "Debug level (0-1)");
58
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -030059MODULE_AUTHOR("Texas Instruments");
60MODULE_DESCRIPTION("TVP514X linux decoder driver");
61MODULE_LICENSE("GPL");
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030062
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -030063/* enum tvp514x_std - enum for supported standards */
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030064enum tvp514x_std {
65 STD_NTSC_MJ = 0,
66 STD_PAL_BDGHIN,
67 STD_INVALID
68};
69
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -030070/**
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030071 * struct tvp514x_std_info - Structure to store standard informations
72 * @width: Line width in pixels
73 * @height:Number of active lines
74 * @video_std: Value to write in REG_VIDEO_STD register
75 * @standard: v4l2 standard structure information
76 */
77struct tvp514x_std_info {
78 unsigned long width;
79 unsigned long height;
80 u8 video_std;
81 struct v4l2_standard standard;
82};
83
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -030084static struct tvp514x_reg tvp514x_reg_list_default[0x40];
Vaibhav Hiremath63b59ce2010-03-27 09:37:54 -030085
86static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable);
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -030087/**
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -030088 * struct tvp514x_decoder - TVP5146/47 decoder object
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -030089 * @sd: Subdevice Slave handle
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -030090 * @tvp514x_regs: copy of hw's regs with preset values.
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030091 * @pdata: Board specific
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030092 * @ver: Chip version
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -030093 * @streaming: TVP5146/47 decoder streaming - enabled or disabled.
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030094 * @current_std: Current standard
95 * @num_stds: Number of standards
96 * @std_list: Standards list
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -030097 * @input: Input routing at chip level
98 * @output: Output routing at chip level
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030099 */
100struct tvp514x_decoder {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300101 struct v4l2_subdev sd;
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300102 struct v4l2_ctrl_handler hdl;
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300103 struct tvp514x_reg tvp514x_regs[ARRAY_SIZE(tvp514x_reg_list_default)];
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300104 const struct tvp514x_platform_data *pdata;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300105
106 int ver;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300107 int streaming;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300108
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300109 enum tvp514x_std current_std;
110 int num_stds;
Hans Verkuila75ffc12010-05-09 06:32:47 -0300111 const struct tvp514x_std_info *std_list;
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300112 /* Input and Output Routing parameters */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300113 u32 input;
114 u32 output;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300115};
116
117/* TVP514x default register values */
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300118static struct tvp514x_reg tvp514x_reg_list_default[] = {
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300119 /* Composite selected */
120 {TOK_WRITE, REG_INPUT_SEL, 0x05},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300121 {TOK_WRITE, REG_AFE_GAIN_CTRL, 0x0F},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300122 /* Auto mode */
123 {TOK_WRITE, REG_VIDEO_STD, 0x00},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300124 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
125 {TOK_SKIP, REG_AUTOSWITCH_MASK, 0x3F},
126 {TOK_WRITE, REG_COLOR_KILLER, 0x10},
127 {TOK_WRITE, REG_LUMA_CONTROL1, 0x00},
128 {TOK_WRITE, REG_LUMA_CONTROL2, 0x00},
129 {TOK_WRITE, REG_LUMA_CONTROL3, 0x02},
130 {TOK_WRITE, REG_BRIGHTNESS, 0x80},
131 {TOK_WRITE, REG_CONTRAST, 0x80},
132 {TOK_WRITE, REG_SATURATION, 0x80},
133 {TOK_WRITE, REG_HUE, 0x00},
134 {TOK_WRITE, REG_CHROMA_CONTROL1, 0x00},
135 {TOK_WRITE, REG_CHROMA_CONTROL2, 0x0E},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300136 /* Reserved */
137 {TOK_SKIP, 0x0F, 0x00},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300138 {TOK_WRITE, REG_COMP_PR_SATURATION, 0x80},
139 {TOK_WRITE, REG_COMP_Y_CONTRAST, 0x80},
140 {TOK_WRITE, REG_COMP_PB_SATURATION, 0x80},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300141 /* Reserved */
142 {TOK_SKIP, 0x13, 0x00},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300143 {TOK_WRITE, REG_COMP_Y_BRIGHTNESS, 0x80},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300144 /* Reserved */
145 {TOK_SKIP, 0x15, 0x00},
146 /* NTSC timing */
147 {TOK_SKIP, REG_AVID_START_PIXEL_LSB, 0x55},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300148 {TOK_SKIP, REG_AVID_START_PIXEL_MSB, 0x00},
149 {TOK_SKIP, REG_AVID_STOP_PIXEL_LSB, 0x25},
150 {TOK_SKIP, REG_AVID_STOP_PIXEL_MSB, 0x03},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300151 /* NTSC timing */
152 {TOK_SKIP, REG_HSYNC_START_PIXEL_LSB, 0x00},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300153 {TOK_SKIP, REG_HSYNC_START_PIXEL_MSB, 0x00},
154 {TOK_SKIP, REG_HSYNC_STOP_PIXEL_LSB, 0x40},
155 {TOK_SKIP, REG_HSYNC_STOP_PIXEL_MSB, 0x00},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300156 /* NTSC timing */
157 {TOK_SKIP, REG_VSYNC_START_LINE_LSB, 0x04},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300158 {TOK_SKIP, REG_VSYNC_START_LINE_MSB, 0x00},
159 {TOK_SKIP, REG_VSYNC_STOP_LINE_LSB, 0x07},
160 {TOK_SKIP, REG_VSYNC_STOP_LINE_MSB, 0x00},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300161 /* NTSC timing */
162 {TOK_SKIP, REG_VBLK_START_LINE_LSB, 0x01},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300163 {TOK_SKIP, REG_VBLK_START_LINE_MSB, 0x00},
164 {TOK_SKIP, REG_VBLK_STOP_LINE_LSB, 0x15},
165 {TOK_SKIP, REG_VBLK_STOP_LINE_MSB, 0x00},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300166 /* Reserved */
167 {TOK_SKIP, 0x26, 0x00},
168 /* Reserved */
169 {TOK_SKIP, 0x27, 0x00},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300170 {TOK_SKIP, REG_FAST_SWTICH_CONTROL, 0xCC},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300171 /* Reserved */
172 {TOK_SKIP, 0x29, 0x00},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300173 {TOK_SKIP, REG_FAST_SWTICH_SCART_DELAY, 0x00},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300174 /* Reserved */
175 {TOK_SKIP, 0x2B, 0x00},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300176 {TOK_SKIP, REG_SCART_DELAY, 0x00},
177 {TOK_SKIP, REG_CTI_DELAY, 0x00},
178 {TOK_SKIP, REG_CTI_CONTROL, 0x00},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300179 /* Reserved */
180 {TOK_SKIP, 0x2F, 0x00},
181 /* Reserved */
182 {TOK_SKIP, 0x30, 0x00},
183 /* Reserved */
184 {TOK_SKIP, 0x31, 0x00},
185 /* HS, VS active high */
186 {TOK_WRITE, REG_SYNC_CONTROL, 0x00},
187 /* 10-bit BT.656 */
188 {TOK_WRITE, REG_OUTPUT_FORMATTER1, 0x00},
189 /* Enable clk & data */
190 {TOK_WRITE, REG_OUTPUT_FORMATTER2, 0x11},
191 /* Enable AVID & FLD */
192 {TOK_WRITE, REG_OUTPUT_FORMATTER3, 0xEE},
193 /* Enable VS & HS */
194 {TOK_WRITE, REG_OUTPUT_FORMATTER4, 0xAF},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300195 {TOK_WRITE, REG_OUTPUT_FORMATTER5, 0xFF},
196 {TOK_WRITE, REG_OUTPUT_FORMATTER6, 0xFF},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300197 /* Clear status */
198 {TOK_WRITE, REG_CLEAR_LOST_LOCK, 0x01},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300199 {TOK_TERM, 0, 0},
200};
201
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300202/**
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300203 * Supported standards -
204 *
205 * Currently supports two standards only, need to add support for rest of the
206 * modes, like SECAM, etc...
207 */
Hans Verkuila75ffc12010-05-09 06:32:47 -0300208static const struct tvp514x_std_info tvp514x_std_list[] = {
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300209 /* Standard: STD_NTSC_MJ */
210 [STD_NTSC_MJ] = {
211 .width = NTSC_NUM_ACTIVE_PIXELS,
212 .height = NTSC_NUM_ACTIVE_LINES,
213 .video_std = VIDEO_STD_NTSC_MJ_BIT,
214 .standard = {
215 .index = 0,
216 .id = V4L2_STD_NTSC,
217 .name = "NTSC",
218 .frameperiod = {1001, 30000},
219 .framelines = 525
220 },
221 /* Standard: STD_PAL_BDGHIN */
222 },
223 [STD_PAL_BDGHIN] = {
224 .width = PAL_NUM_ACTIVE_PIXELS,
225 .height = PAL_NUM_ACTIVE_LINES,
226 .video_std = VIDEO_STD_PAL_BDGHIN_BIT,
227 .standard = {
228 .index = 1,
229 .id = V4L2_STD_PAL,
230 .name = "PAL",
231 .frameperiod = {1, 25},
232 .framelines = 625
233 },
234 },
235 /* Standard: need to add for additional standard */
236};
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300237
238
239static inline struct tvp514x_decoder *to_decoder(struct v4l2_subdev *sd)
240{
241 return container_of(sd, struct tvp514x_decoder, sd);
242}
243
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300244static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
245{
246 return &container_of(ctrl->handler, struct tvp514x_decoder, hdl)->sd;
247}
248
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300249
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300250/**
251 * tvp514x_read_reg() - Read a value from a register in an TVP5146/47.
252 * @sd: ptr to v4l2_subdev struct
253 * @reg: TVP5146/47 register address
254 *
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300255 * Returns value read if successful, or non-zero (-1) otherwise.
256 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300257static int tvp514x_read_reg(struct v4l2_subdev *sd, u8 reg)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300258{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300259 int err, retry = 0;
260 struct i2c_client *client = v4l2_get_subdevdata(sd);
261
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300262read_again:
263
264 err = i2c_smbus_read_byte_data(client, reg);
Sebastian Andrzej Siewior6f901a92009-11-04 15:35:09 -0300265 if (err < 0) {
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300266 if (retry <= I2C_RETRY_COUNT) {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300267 v4l2_warn(sd, "Read: retry ... %d\n", retry);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300268 retry++;
269 msleep_interruptible(10);
270 goto read_again;
271 }
272 }
273
274 return err;
275}
276
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300277/**
278 * dump_reg() - dump the register content of TVP5146/47.
279 * @sd: ptr to v4l2_subdev struct
280 * @reg: TVP5146/47 register address
281 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300282static void dump_reg(struct v4l2_subdev *sd, u8 reg)
283{
284 u32 val;
285
286 val = tvp514x_read_reg(sd, reg);
287 v4l2_info(sd, "Reg(0x%.2X): 0x%.2X\n", reg, val);
288}
289
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300290/**
291 * tvp514x_write_reg() - Write a value to a register in TVP5146/47
292 * @sd: ptr to v4l2_subdev struct
293 * @reg: TVP5146/47 register address
294 * @val: value to be written to the register
295 *
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300296 * Write a value to a register in an TVP5146/47 decoder device.
297 * Returns zero if successful, or non-zero otherwise.
298 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300299static int tvp514x_write_reg(struct v4l2_subdev *sd, u8 reg, u8 val)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300300{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300301 int err, retry = 0;
302 struct i2c_client *client = v4l2_get_subdevdata(sd);
303
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300304write_again:
305
306 err = i2c_smbus_write_byte_data(client, reg, val);
307 if (err) {
308 if (retry <= I2C_RETRY_COUNT) {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300309 v4l2_warn(sd, "Write: retry ... %d\n", retry);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300310 retry++;
311 msleep_interruptible(10);
312 goto write_again;
313 }
314 }
315
316 return err;
317}
318
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300319/**
320 * tvp514x_write_regs() : Initializes a list of TVP5146/47 registers
321 * @sd: ptr to v4l2_subdev struct
322 * @reglist: list of TVP5146/47 registers and values
323 *
324 * Initializes a list of TVP5146/47 registers:-
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300325 * if token is TOK_TERM, then entire write operation terminates
326 * if token is TOK_DELAY, then a delay of 'val' msec is introduced
327 * if token is TOK_SKIP, then the register write is skipped
328 * if token is TOK_WRITE, then the register write is performed
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300329 * Returns zero if successful, or non-zero otherwise.
330 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300331static int tvp514x_write_regs(struct v4l2_subdev *sd,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300332 const struct tvp514x_reg reglist[])
333{
334 int err;
335 const struct tvp514x_reg *next = reglist;
336
337 for (; next->token != TOK_TERM; next++) {
338 if (next->token == TOK_DELAY) {
339 msleep(next->val);
340 continue;
341 }
342
343 if (next->token == TOK_SKIP)
344 continue;
345
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300346 err = tvp514x_write_reg(sd, next->reg, (u8) next->val);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300347 if (err) {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300348 v4l2_err(sd, "Write failed. Err[%d]\n", err);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300349 return err;
350 }
351 }
352 return 0;
353}
354
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300355/**
Hans Verkuil2db4e782010-05-09 06:30:15 -0300356 * tvp514x_query_current_std() : Query the current standard detected by TVP5146/47
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300357 * @sd: ptr to v4l2_subdev struct
358 *
Hans Verkuil2db4e782010-05-09 06:30:15 -0300359 * Returns the current standard detected by TVP5146/47, STD_INVALID if there is no
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300360 * standard detected.
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300361 */
Hans Verkuil2db4e782010-05-09 06:30:15 -0300362static enum tvp514x_std tvp514x_query_current_std(struct v4l2_subdev *sd)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300363{
364 u8 std, std_status;
365
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300366 std = tvp514x_read_reg(sd, REG_VIDEO_STD);
367 if ((std & VIDEO_STD_MASK) == VIDEO_STD_AUTO_SWITCH_BIT)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300368 /* use the standard status register */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300369 std_status = tvp514x_read_reg(sd, REG_VIDEO_STD_STATUS);
370 else
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300371 /* use the standard register itself */
372 std_status = std;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300373
374 switch (std_status & VIDEO_STD_MASK) {
375 case VIDEO_STD_NTSC_MJ_BIT:
376 return STD_NTSC_MJ;
377
378 case VIDEO_STD_PAL_BDGHIN_BIT:
379 return STD_PAL_BDGHIN;
380
381 default:
382 return STD_INVALID;
383 }
384
385 return STD_INVALID;
386}
387
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300388/* TVP5146/47 register dump function */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300389static void tvp514x_reg_dump(struct v4l2_subdev *sd)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300390{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300391 dump_reg(sd, REG_INPUT_SEL);
392 dump_reg(sd, REG_AFE_GAIN_CTRL);
393 dump_reg(sd, REG_VIDEO_STD);
394 dump_reg(sd, REG_OPERATION_MODE);
395 dump_reg(sd, REG_COLOR_KILLER);
396 dump_reg(sd, REG_LUMA_CONTROL1);
397 dump_reg(sd, REG_LUMA_CONTROL2);
398 dump_reg(sd, REG_LUMA_CONTROL3);
399 dump_reg(sd, REG_BRIGHTNESS);
400 dump_reg(sd, REG_CONTRAST);
401 dump_reg(sd, REG_SATURATION);
402 dump_reg(sd, REG_HUE);
403 dump_reg(sd, REG_CHROMA_CONTROL1);
404 dump_reg(sd, REG_CHROMA_CONTROL2);
405 dump_reg(sd, REG_COMP_PR_SATURATION);
406 dump_reg(sd, REG_COMP_Y_CONTRAST);
407 dump_reg(sd, REG_COMP_PB_SATURATION);
408 dump_reg(sd, REG_COMP_Y_BRIGHTNESS);
409 dump_reg(sd, REG_AVID_START_PIXEL_LSB);
410 dump_reg(sd, REG_AVID_START_PIXEL_MSB);
411 dump_reg(sd, REG_AVID_STOP_PIXEL_LSB);
412 dump_reg(sd, REG_AVID_STOP_PIXEL_MSB);
413 dump_reg(sd, REG_HSYNC_START_PIXEL_LSB);
414 dump_reg(sd, REG_HSYNC_START_PIXEL_MSB);
415 dump_reg(sd, REG_HSYNC_STOP_PIXEL_LSB);
416 dump_reg(sd, REG_HSYNC_STOP_PIXEL_MSB);
417 dump_reg(sd, REG_VSYNC_START_LINE_LSB);
418 dump_reg(sd, REG_VSYNC_START_LINE_MSB);
419 dump_reg(sd, REG_VSYNC_STOP_LINE_LSB);
420 dump_reg(sd, REG_VSYNC_STOP_LINE_MSB);
421 dump_reg(sd, REG_VBLK_START_LINE_LSB);
422 dump_reg(sd, REG_VBLK_START_LINE_MSB);
423 dump_reg(sd, REG_VBLK_STOP_LINE_LSB);
424 dump_reg(sd, REG_VBLK_STOP_LINE_MSB);
425 dump_reg(sd, REG_SYNC_CONTROL);
426 dump_reg(sd, REG_OUTPUT_FORMATTER1);
427 dump_reg(sd, REG_OUTPUT_FORMATTER2);
428 dump_reg(sd, REG_OUTPUT_FORMATTER3);
429 dump_reg(sd, REG_OUTPUT_FORMATTER4);
430 dump_reg(sd, REG_OUTPUT_FORMATTER5);
431 dump_reg(sd, REG_OUTPUT_FORMATTER6);
432 dump_reg(sd, REG_CLEAR_LOST_LOCK);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300433}
434
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300435/**
436 * tvp514x_configure() - Configure the TVP5146/47 registers
437 * @sd: ptr to v4l2_subdev struct
438 * @decoder: ptr to tvp514x_decoder structure
439 *
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300440 * Returns zero if successful, or non-zero otherwise.
441 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300442static int tvp514x_configure(struct v4l2_subdev *sd,
443 struct tvp514x_decoder *decoder)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300444{
445 int err;
446
447 /* common register initialization */
448 err =
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300449 tvp514x_write_regs(sd, decoder->tvp514x_regs);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300450 if (err)
451 return err;
452
453 if (debug)
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300454 tvp514x_reg_dump(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300455
456 return 0;
457}
458
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300459/**
460 * tvp514x_detect() - Detect if an tvp514x is present, and if so which revision.
461 * @sd: pointer to standard V4L2 sub-device structure
462 * @decoder: pointer to tvp514x_decoder structure
463 *
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300464 * A device is considered to be detected if the chip ID (LSB and MSB)
465 * registers match the expected values.
466 * Any value of the rom version register is accepted.
467 * Returns ENODEV error number if no device is detected, or zero
468 * if a device is detected.
469 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300470static int tvp514x_detect(struct v4l2_subdev *sd,
471 struct tvp514x_decoder *decoder)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300472{
473 u8 chip_id_msb, chip_id_lsb, rom_ver;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300474 struct i2c_client *client = v4l2_get_subdevdata(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300475
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300476 chip_id_msb = tvp514x_read_reg(sd, REG_CHIP_ID_MSB);
477 chip_id_lsb = tvp514x_read_reg(sd, REG_CHIP_ID_LSB);
478 rom_ver = tvp514x_read_reg(sd, REG_ROM_VERSION);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300479
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300480 v4l2_dbg(1, debug, sd,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300481 "chip id detected msb:0x%x lsb:0x%x rom version:0x%x\n",
482 chip_id_msb, chip_id_lsb, rom_ver);
483 if ((chip_id_msb != TVP514X_CHIP_ID_MSB)
484 || ((chip_id_lsb != TVP5146_CHIP_ID_LSB)
485 && (chip_id_lsb != TVP5147_CHIP_ID_LSB))) {
486 /* We didn't read the values we expected, so this must not be
487 * an TVP5146/47.
488 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300489 v4l2_err(sd, "chip id mismatch msb:0x%x lsb:0x%x\n",
490 chip_id_msb, chip_id_lsb);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300491 return -ENODEV;
492 }
493
494 decoder->ver = rom_ver;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300495
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300496 v4l2_info(sd, "%s (Version - 0x%.2x) found at 0x%x (%s)\n",
497 client->name, decoder->ver,
498 client->addr << 1, client->adapter->name);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300499 return 0;
500}
501
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300502/**
503 * tvp514x_querystd() - V4L2 decoder interface handler for querystd
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300504 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300505 * @std_id: standard V4L2 std_id ioctl enum
506 *
507 * Returns the current standard detected by TVP5146/47. If no active input is
Hans Verkuil2db4e782010-05-09 06:30:15 -0300508 * detected then *std_id is set to 0 and the function returns 0.
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300509 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300510static int tvp514x_querystd(struct v4l2_subdev *sd, v4l2_std_id *std_id)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300511{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300512 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300513 enum tvp514x_std current_std;
514 enum tvp514x_input input_sel;
515 u8 sync_lock_status, lock_mask;
516
517 if (std_id == NULL)
518 return -EINVAL;
519
Hans Verkuil2db4e782010-05-09 06:30:15 -0300520 *std_id = V4L2_STD_UNKNOWN;
521
Hans Verkuilc3896482012-09-20 09:06:33 -0300522 /* To query the standard the TVP514x must power on the ADCs. */
523 if (!decoder->streaming) {
524 tvp514x_s_stream(sd, 1);
525 msleep(LOCK_RETRY_DELAY);
526 }
527
Hans Verkuil2db4e782010-05-09 06:30:15 -0300528 /* query the current standard */
529 current_std = tvp514x_query_current_std(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300530 if (current_std == STD_INVALID)
Hans Verkuil2db4e782010-05-09 06:30:15 -0300531 return 0;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300532
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300533 input_sel = decoder->input;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300534
535 switch (input_sel) {
536 case INPUT_CVBS_VI1A:
537 case INPUT_CVBS_VI1B:
538 case INPUT_CVBS_VI1C:
539 case INPUT_CVBS_VI2A:
540 case INPUT_CVBS_VI2B:
541 case INPUT_CVBS_VI2C:
542 case INPUT_CVBS_VI3A:
543 case INPUT_CVBS_VI3B:
544 case INPUT_CVBS_VI3C:
545 case INPUT_CVBS_VI4A:
546 lock_mask = STATUS_CLR_SUBCAR_LOCK_BIT |
547 STATUS_HORZ_SYNC_LOCK_BIT |
548 STATUS_VIRT_SYNC_LOCK_BIT;
549 break;
550
551 case INPUT_SVIDEO_VI2A_VI1A:
552 case INPUT_SVIDEO_VI2B_VI1B:
553 case INPUT_SVIDEO_VI2C_VI1C:
554 case INPUT_SVIDEO_VI2A_VI3A:
555 case INPUT_SVIDEO_VI2B_VI3B:
556 case INPUT_SVIDEO_VI2C_VI3C:
557 case INPUT_SVIDEO_VI4A_VI1A:
558 case INPUT_SVIDEO_VI4A_VI1B:
559 case INPUT_SVIDEO_VI4A_VI1C:
560 case INPUT_SVIDEO_VI4A_VI3A:
561 case INPUT_SVIDEO_VI4A_VI3B:
562 case INPUT_SVIDEO_VI4A_VI3C:
563 lock_mask = STATUS_HORZ_SYNC_LOCK_BIT |
564 STATUS_VIRT_SYNC_LOCK_BIT;
565 break;
566 /*Need to add other interfaces*/
567 default:
568 return -EINVAL;
569 }
570 /* check whether signal is locked */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300571 sync_lock_status = tvp514x_read_reg(sd, REG_STATUS1);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300572 if (lock_mask != (sync_lock_status & lock_mask))
Hans Verkuil2db4e782010-05-09 06:30:15 -0300573 return 0; /* No input detected */
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300574
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300575 *std_id = decoder->std_list[current_std].standard.id;
576
Hans Verkuil2db4e782010-05-09 06:30:15 -0300577 v4l2_dbg(1, debug, sd, "Current STD: %s\n",
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300578 decoder->std_list[current_std].standard.name);
579 return 0;
580}
581
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300582/**
583 * tvp514x_s_std() - V4L2 decoder interface handler for s_std
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300584 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300585 * @std_id: standard V4L2 v4l2_std_id ioctl enum
586 *
587 * If std_id is supported, sets the requested standard. Otherwise, returns
588 * -EINVAL
589 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300590static int tvp514x_s_std(struct v4l2_subdev *sd, v4l2_std_id std_id)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300591{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300592 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300593 int err, i;
594
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300595 for (i = 0; i < decoder->num_stds; i++)
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300596 if (std_id & decoder->std_list[i].standard.id)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300597 break;
598
599 if ((i == decoder->num_stds) || (i == STD_INVALID))
600 return -EINVAL;
601
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300602 err = tvp514x_write_reg(sd, REG_VIDEO_STD,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300603 decoder->std_list[i].video_std);
604 if (err)
605 return err;
606
607 decoder->current_std = i;
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300608 decoder->tvp514x_regs[REG_VIDEO_STD].val =
609 decoder->std_list[i].video_std;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300610
Hans Verkuil3907b072010-05-09 06:39:44 -0300611 v4l2_dbg(1, debug, sd, "Standard set to: %s\n",
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300612 decoder->std_list[i].standard.name);
613 return 0;
614}
615
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300616/**
617 * tvp514x_s_routing() - V4L2 decoder interface handler for s_routing
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300618 * @sd: pointer to standard V4L2 sub-device structure
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300619 * @input: input selector for routing the signal
620 * @output: output selector for routing the signal
621 * @config: config value. Not used
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300622 *
623 * If index is valid, selects the requested input. Otherwise, returns -EINVAL if
624 * the input is not supported or there is no active signal present in the
625 * selected input.
626 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300627static int tvp514x_s_routing(struct v4l2_subdev *sd,
628 u32 input, u32 output, u32 config)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300629{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300630 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300631 int err;
632 enum tvp514x_input input_sel;
633 enum tvp514x_output output_sel;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300634
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300635 if ((input >= INPUT_INVALID) ||
636 (output >= OUTPUT_INVALID))
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300637 /* Index out of bound */
638 return -EINVAL;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300639
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300640 input_sel = input;
641 output_sel = output;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300642
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300643 err = tvp514x_write_reg(sd, REG_INPUT_SEL, input_sel);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300644 if (err)
645 return err;
646
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300647 output_sel |= tvp514x_read_reg(sd,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300648 REG_OUTPUT_FORMATTER1) & 0x7;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300649 err = tvp514x_write_reg(sd, REG_OUTPUT_FORMATTER1,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300650 output_sel);
651 if (err)
652 return err;
653
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300654 decoder->tvp514x_regs[REG_INPUT_SEL].val = input_sel;
655 decoder->tvp514x_regs[REG_OUTPUT_FORMATTER1].val = output_sel;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300656 decoder->input = input;
657 decoder->output = output;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300658
Hans Verkuil2db4e782010-05-09 06:30:15 -0300659 v4l2_dbg(1, debug, sd, "Input set to: %d\n", input_sel);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300660
661 return 0;
662}
663
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300664/**
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300665 * tvp514x_s_ctrl() - V4L2 decoder interface handler for s_ctrl
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300666 * @ctrl: pointer to v4l2_ctrl structure
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300667 *
668 * If the requested control is supported, sets the control's current
669 * value in HW. Otherwise, returns -EINVAL if the control is not supported.
670 */
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300671static int tvp514x_s_ctrl(struct v4l2_ctrl *ctrl)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300672{
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300673 struct v4l2_subdev *sd = to_sd(ctrl);
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300674 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300675 int err = -EINVAL, value;
676
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300677 value = ctrl->val;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300678
679 switch (ctrl->id) {
680 case V4L2_CID_BRIGHTNESS:
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300681 err = tvp514x_write_reg(sd, REG_BRIGHTNESS, value);
682 if (!err)
683 decoder->tvp514x_regs[REG_BRIGHTNESS].val = value;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300684 break;
685 case V4L2_CID_CONTRAST:
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300686 err = tvp514x_write_reg(sd, REG_CONTRAST, value);
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300687 if (!err)
688 decoder->tvp514x_regs[REG_CONTRAST].val = value;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300689 break;
690 case V4L2_CID_SATURATION:
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300691 err = tvp514x_write_reg(sd, REG_SATURATION, value);
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300692 if (!err)
693 decoder->tvp514x_regs[REG_SATURATION].val = value;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300694 break;
695 case V4L2_CID_HUE:
696 if (value == 180)
697 value = 0x7F;
698 else if (value == -180)
699 value = 0x80;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300700 err = tvp514x_write_reg(sd, REG_HUE, value);
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300701 if (!err)
702 decoder->tvp514x_regs[REG_HUE].val = value;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300703 break;
704 case V4L2_CID_AUTOGAIN:
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300705 err = tvp514x_write_reg(sd, REG_AFE_GAIN_CTRL, value ? 0x0f : 0x0c);
706 if (!err)
707 decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val = value;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300708 break;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300709 }
710
Hans Verkuil3907b072010-05-09 06:39:44 -0300711 v4l2_dbg(1, debug, sd, "Set Control: ID - %d - %d\n",
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300712 ctrl->id, ctrl->val);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300713 return err;
714}
715
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300716/**
Hans Verkuil83811912010-05-09 06:50:25 -0300717 * tvp514x_enum_mbus_fmt() - V4L2 decoder interface handler for enum_mbus_fmt
718 * @sd: pointer to standard V4L2 sub-device structure
719 * @index: index of pixelcode to retrieve
720 * @code: receives the pixelcode
721 *
722 * Enumerates supported mediabus formats
723 */
724static int
725tvp514x_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned index,
726 enum v4l2_mbus_pixelcode *code)
727{
728 if (index)
729 return -EINVAL;
730
731 *code = V4L2_MBUS_FMT_YUYV10_2X10;
732 return 0;
733}
734
735/**
Hans Verkuil83811912010-05-09 06:50:25 -0300736 * tvp514x_mbus_fmt_cap() - V4L2 decoder interface handler for try/s/g_mbus_fmt
737 * @sd: pointer to standard V4L2 sub-device structure
738 * @f: pointer to the mediabus format structure
739 *
740 * Negotiates the image capture size and mediabus format.
741 */
742static int
743tvp514x_mbus_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *f)
744{
745 struct tvp514x_decoder *decoder = to_decoder(sd);
746 enum tvp514x_std current_std;
747
748 if (f == NULL)
749 return -EINVAL;
750
751 /* Calculate height and width based on current standard */
752 current_std = decoder->current_std;
753
754 f->code = V4L2_MBUS_FMT_YUYV10_2X10;
755 f->width = decoder->std_list[current_std].width;
756 f->height = decoder->std_list[current_std].height;
757 f->field = V4L2_FIELD_INTERLACED;
758 f->colorspace = V4L2_COLORSPACE_SMPTE170M;
759
760 v4l2_dbg(1, debug, sd, "MBUS_FMT: Width - %d, Height - %d\n",
761 f->width, f->height);
762 return 0;
763}
764
765/**
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300766 * tvp514x_g_parm() - V4L2 decoder interface handler for g_parm
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300767 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300768 * @a: pointer to standard V4L2 VIDIOC_G_PARM ioctl structure
769 *
770 * Returns the decoder's video CAPTURE parameters.
771 */
772static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300773tvp514x_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300774{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300775 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300776 struct v4l2_captureparm *cparm;
777 enum tvp514x_std current_std;
778
779 if (a == NULL)
780 return -EINVAL;
781
782 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300783 /* only capture is supported */
784 return -EINVAL;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300785
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300786 /* get the current standard */
Hans Verkuil2db4e782010-05-09 06:30:15 -0300787 current_std = decoder->current_std;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300788
789 cparm = &a->parm.capture;
790 cparm->capability = V4L2_CAP_TIMEPERFRAME;
791 cparm->timeperframe =
792 decoder->std_list[current_std].standard.frameperiod;
793
794 return 0;
795}
796
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300797/**
798 * tvp514x_s_parm() - V4L2 decoder interface handler for s_parm
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300799 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300800 * @a: pointer to standard V4L2 VIDIOC_S_PARM ioctl structure
801 *
802 * Configures the decoder to use the input parameters, if possible. If
803 * not possible, returns the appropriate error code.
804 */
805static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300806tvp514x_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300807{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300808 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300809 struct v4l2_fract *timeperframe;
810 enum tvp514x_std current_std;
811
812 if (a == NULL)
813 return -EINVAL;
814
815 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300816 /* only capture is supported */
817 return -EINVAL;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300818
819 timeperframe = &a->parm.capture.timeperframe;
820
821 /* get the current standard */
Hans Verkuil2db4e782010-05-09 06:30:15 -0300822 current_std = decoder->current_std;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300823
824 *timeperframe =
825 decoder->std_list[current_std].standard.frameperiod;
826
827 return 0;
828}
829
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300830/**
831 * tvp514x_s_stream() - V4L2 decoder i/f handler for s_stream
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300832 * @sd: pointer to standard V4L2 sub-device structure
833 * @enable: streaming enable or disable
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300834 *
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300835 * Sets streaming to enable or disable, if possible.
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300836 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300837static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300838{
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300839 int err = 0;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300840 struct i2c_client *client = v4l2_get_subdevdata(sd);
841 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300842
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300843 if (decoder->streaming == enable)
844 return 0;
845
846 switch (enable) {
847 case 0:
848 {
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300849 /* Power Down Sequence */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300850 err = tvp514x_write_reg(sd, REG_OPERATION_MODE, 0x01);
851 if (err) {
852 v4l2_err(sd, "Unable to turn off decoder\n");
853 return err;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300854 }
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300855 decoder->streaming = enable;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300856 break;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300857 }
858 case 1:
859 {
860 struct tvp514x_reg *int_seq = (struct tvp514x_reg *)
861 client->driver->id_table->driver_data;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300862
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300863 /* Power Up Sequence */
864 err = tvp514x_write_regs(sd, int_seq);
865 if (err) {
866 v4l2_err(sd, "Unable to turn on decoder\n");
867 return err;
868 }
869 /* Detect if not already detected */
870 err = tvp514x_detect(sd, decoder);
871 if (err) {
872 v4l2_err(sd, "Unable to detect decoder\n");
873 return err;
874 }
875 err = tvp514x_configure(sd, decoder);
876 if (err) {
877 v4l2_err(sd, "Unable to configure decoder\n");
878 return err;
879 }
880 decoder->streaming = enable;
881 break;
882 }
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300883 default:
884 err = -ENODEV;
885 break;
886 }
887
888 return err;
889}
890
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300891static const struct v4l2_ctrl_ops tvp514x_ctrl_ops = {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300892 .s_ctrl = tvp514x_s_ctrl,
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300893};
894
895static const struct v4l2_subdev_core_ops tvp514x_core_ops = {
896 .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
897 .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
898 .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
899 .g_ctrl = v4l2_subdev_g_ctrl,
900 .s_ctrl = v4l2_subdev_s_ctrl,
901 .queryctrl = v4l2_subdev_queryctrl,
902 .querymenu = v4l2_subdev_querymenu,
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300903 .s_std = tvp514x_s_std,
904};
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300905
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300906static const struct v4l2_subdev_video_ops tvp514x_video_ops = {
907 .s_routing = tvp514x_s_routing,
908 .querystd = tvp514x_querystd,
Hans Verkuil83811912010-05-09 06:50:25 -0300909 .enum_mbus_fmt = tvp514x_enum_mbus_fmt,
910 .g_mbus_fmt = tvp514x_mbus_fmt,
911 .try_mbus_fmt = tvp514x_mbus_fmt,
912 .s_mbus_fmt = tvp514x_mbus_fmt,
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300913 .g_parm = tvp514x_g_parm,
914 .s_parm = tvp514x_s_parm,
915 .s_stream = tvp514x_s_stream,
916};
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300917
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300918static const struct v4l2_subdev_ops tvp514x_ops = {
919 .core = &tvp514x_core_ops,
920 .video = &tvp514x_video_ops,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300921};
922
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300923static struct tvp514x_decoder tvp514x_dev = {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300924 .streaming = 0,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300925 .current_std = STD_NTSC_MJ,
926 .std_list = tvp514x_std_list,
927 .num_stds = ARRAY_SIZE(tvp514x_std_list),
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300928
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300929};
930
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300931/**
932 * tvp514x_probe() - decoder driver i2c probe handler
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300933 * @client: i2c driver client device structure
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300934 * @id: i2c driver id table
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300935 *
936 * Register decoder as an i2c client device and V4L2
937 * device.
938 */
939static int
940tvp514x_probe(struct i2c_client *client, const struct i2c_device_id *id)
941{
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300942 struct tvp514x_decoder *decoder;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300943 struct v4l2_subdev *sd;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300944
945 /* Check if the adapter supports the needed features */
946 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
947 return -EIO;
948
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300949 if (!client->dev.platform_data) {
950 v4l2_err(client, "No platform data!!\n");
951 return -ENODEV;
952 }
953
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300954 decoder = kzalloc(sizeof(*decoder), GFP_KERNEL);
955 if (!decoder)
956 return -ENOMEM;
957
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300958 /* Initialize the tvp514x_decoder with default configuration */
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300959 *decoder = tvp514x_dev;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300960 /* Copy default register configuration */
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300961 memcpy(decoder->tvp514x_regs, tvp514x_reg_list_default,
962 sizeof(tvp514x_reg_list_default));
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300963
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300964 /* Copy board specific information here */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300965 decoder->pdata = client->dev.platform_data;
966
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300967 /**
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300968 * Fetch platform specific data, and configure the
969 * tvp514x_reg_list[] accordingly. Since this is one
970 * time configuration, no need to preserve.
971 */
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300972 decoder->tvp514x_regs[REG_OUTPUT_FORMATTER2].val |=
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300973 (decoder->pdata->clk_polarity << 1);
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300974 decoder->tvp514x_regs[REG_SYNC_CONTROL].val |=
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300975 ((decoder->pdata->hs_polarity << 2) |
976 (decoder->pdata->vs_polarity << 3));
977 /* Set default standard to auto */
978 decoder->tvp514x_regs[REG_VIDEO_STD].val =
979 VIDEO_STD_AUTO_SWITCH_BIT;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300980
981 /* Register with V4L2 layer as slave device */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300982 sd = &decoder->sd;
983 v4l2_i2c_subdev_init(sd, client, &tvp514x_ops);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300984
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300985 v4l2_ctrl_handler_init(&decoder->hdl, 5);
986 v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
987 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
988 v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
989 V4L2_CID_CONTRAST, 0, 255, 1, 128);
990 v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
991 V4L2_CID_SATURATION, 0, 255, 1, 128);
992 v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
993 V4L2_CID_HUE, -180, 180, 180, 0);
994 v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
995 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
996 sd->ctrl_handler = &decoder->hdl;
997 if (decoder->hdl.error) {
998 int err = decoder->hdl.error;
999
1000 v4l2_ctrl_handler_free(&decoder->hdl);
1001 kfree(decoder);
1002 return err;
1003 }
1004 v4l2_ctrl_handler_setup(&decoder->hdl);
1005
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001006 v4l2_info(sd, "%s decoder driver registered !!\n", sd->name);
1007
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001008 return 0;
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001009
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001010}
1011
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001012/**
1013 * tvp514x_remove() - decoder driver i2c remove handler
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001014 * @client: i2c driver client device structure
1015 *
1016 * Unregister decoder as an i2c client device and V4L2
1017 * device. Complement of tvp514x_probe().
1018 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001019static int tvp514x_remove(struct i2c_client *client)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001020{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001021 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1022 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001023
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001024 v4l2_device_unregister_subdev(sd);
Hans Verkuilcf6832a2010-12-12 08:45:22 -03001025 v4l2_ctrl_handler_free(&decoder->hdl);
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001026 kfree(decoder);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001027 return 0;
1028}
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001029/* TVP5146 Init/Power on Sequence */
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001030static const struct tvp514x_reg tvp5146_init_reg_seq[] = {
1031 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
1032 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1033 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
1034 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1035 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1036 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1037 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1038 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1039 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
1040 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1041 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001042 {TOK_TERM, 0, 0},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001043};
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001044
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001045/* TVP5147 Init/Power on Sequence */
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001046static const struct tvp514x_reg tvp5147_init_reg_seq[] = {
1047 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
1048 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1049 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
1050 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1051 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1052 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1053 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1054 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1055 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x16},
1056 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1057 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xA0},
1058 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x16},
1059 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1060 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1061 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1062 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
1063 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1064 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001065 {TOK_TERM, 0, 0},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001066};
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001067
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001068/* TVP5146M2/TVP5147M1 Init/Power on Sequence */
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001069static const struct tvp514x_reg tvp514xm_init_reg_seq[] = {
1070 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1071 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001072 {TOK_TERM, 0, 0},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001073};
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001074
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001075/**
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001076 * I2C Device Table -
1077 *
1078 * name - Name of the actual device/chip.
1079 * driver_data - Driver data
1080 */
1081static const struct i2c_device_id tvp514x_id[] = {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001082 {"tvp5146", (unsigned long)tvp5146_init_reg_seq},
1083 {"tvp5146m2", (unsigned long)tvp514xm_init_reg_seq},
1084 {"tvp5147", (unsigned long)tvp5147_init_reg_seq},
1085 {"tvp5147m1", (unsigned long)tvp514xm_init_reg_seq},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001086 {},
1087};
1088
1089MODULE_DEVICE_TABLE(i2c, tvp514x_id);
1090
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001091static struct i2c_driver tvp514x_driver = {
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001092 .driver = {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001093 .owner = THIS_MODULE,
1094 .name = TVP514X_MODULE_NAME,
1095 },
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001096 .probe = tvp514x_probe,
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001097 .remove = tvp514x_remove,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001098 .id_table = tvp514x_id,
1099};
1100
Axel Linc6e8d862012-02-12 06:56:32 -03001101module_i2c_driver(tvp514x_driver);