blob: 864eb14ae9b10046043fde7d9272f01365638b87 [file] [log] [blame]
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001/*
Mauro Carvalho Chehabcb7a01a2012-08-14 16:23:43 -03002 * drivers/media/i2c/tvp514x.c
Vaibhav Hiremath07b17472008-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>
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -030015 * Prabhakar Lad <prabhakar.lad@ti.com>
Vaibhav Hiremath07b17472008-12-05 10:19:36 -030016 *
17 * This package is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License version 2 as
19 * published by the Free Software Foundation.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 *
30 */
31
32#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Vaibhav Hiremath07b17472008-12-05 10:19:36 -030034#include <linux/delay.h>
35#include <linux/videodev2.h>
Paul Gortmaker7a707b82011-07-03 14:03:12 -040036#include <linux/module.h>
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -030037#include <linux/v4l2-mediabus.h>
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -030038
39#include <media/v4l2-device.h>
40#include <media/v4l2-common.h>
Hans Verkuil83811912010-05-09 06:50:25 -030041#include <media/v4l2-mediabus.h>
Lad, Prabhakarb610b592013-06-04 12:26:23 -030042#include <media/v4l2-of.h>
Hans Verkuilcf6832a2010-12-12 08:45:22 -030043#include <media/v4l2-ctrls.h>
Vaibhav Hiremath07b17472008-12-05 10:19:36 -030044#include <media/tvp514x.h>
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -030045#include <media/media-entity.h>
Vaibhav Hiremath07b17472008-12-05 10:19:36 -030046
47#include "tvp514x_regs.h"
48
Vaibhav Hiremath07b17472008-12-05 10:19:36 -030049/* 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 Hiremath07b17472008-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 Hiremath07b17472008-12-05 10:19:36 -030062
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -030063/* enum tvp514x_std - enum for supported standards */
Vaibhav Hiremath07b17472008-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 Hiremath07b17472008-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 Hiremath07b17472008-12-05 10:19:36 -030091 * @pdata: Board specific
Vaibhav Hiremath07b17472008-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.
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -030094 * @pix: Current pixel format
95 * @num_fmts: Number of formats
96 * @fmt_list: Format list
Vaibhav Hiremath07b17472008-12-05 10:19:36 -030097 * @current_std: Current standard
98 * @num_stds: Number of standards
99 * @std_list: Standards list
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300100 * @input: Input routing at chip level
101 * @output: Output routing at chip level
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300102 */
103struct tvp514x_decoder {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300104 struct v4l2_subdev sd;
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300105 struct v4l2_ctrl_handler hdl;
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300106 struct tvp514x_reg tvp514x_regs[ARRAY_SIZE(tvp514x_reg_list_default)];
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300107 const struct tvp514x_platform_data *pdata;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300108
109 int ver;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300110 int streaming;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300111
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -0300112 struct v4l2_pix_format pix;
113 int num_fmts;
114 const struct v4l2_fmtdesc *fmt_list;
115
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300116 enum tvp514x_std current_std;
117 int num_stds;
Hans Verkuila75ffc12010-05-09 06:32:47 -0300118 const struct tvp514x_std_info *std_list;
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300119 /* Input and Output Routing parameters */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300120 u32 input;
121 u32 output;
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -0300122
123 /* mc related members */
124 struct media_pad pad;
125 struct v4l2_mbus_framefmt format;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300126};
127
128/* TVP514x default register values */
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300129static struct tvp514x_reg tvp514x_reg_list_default[] = {
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300130 /* Composite selected */
131 {TOK_WRITE, REG_INPUT_SEL, 0x05},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300132 {TOK_WRITE, REG_AFE_GAIN_CTRL, 0x0F},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300133 /* Auto mode */
134 {TOK_WRITE, REG_VIDEO_STD, 0x00},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300135 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
136 {TOK_SKIP, REG_AUTOSWITCH_MASK, 0x3F},
137 {TOK_WRITE, REG_COLOR_KILLER, 0x10},
138 {TOK_WRITE, REG_LUMA_CONTROL1, 0x00},
139 {TOK_WRITE, REG_LUMA_CONTROL2, 0x00},
140 {TOK_WRITE, REG_LUMA_CONTROL3, 0x02},
141 {TOK_WRITE, REG_BRIGHTNESS, 0x80},
142 {TOK_WRITE, REG_CONTRAST, 0x80},
143 {TOK_WRITE, REG_SATURATION, 0x80},
144 {TOK_WRITE, REG_HUE, 0x00},
145 {TOK_WRITE, REG_CHROMA_CONTROL1, 0x00},
146 {TOK_WRITE, REG_CHROMA_CONTROL2, 0x0E},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300147 /* Reserved */
148 {TOK_SKIP, 0x0F, 0x00},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300149 {TOK_WRITE, REG_COMP_PR_SATURATION, 0x80},
150 {TOK_WRITE, REG_COMP_Y_CONTRAST, 0x80},
151 {TOK_WRITE, REG_COMP_PB_SATURATION, 0x80},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300152 /* Reserved */
153 {TOK_SKIP, 0x13, 0x00},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300154 {TOK_WRITE, REG_COMP_Y_BRIGHTNESS, 0x80},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300155 /* Reserved */
156 {TOK_SKIP, 0x15, 0x00},
157 /* NTSC timing */
158 {TOK_SKIP, REG_AVID_START_PIXEL_LSB, 0x55},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300159 {TOK_SKIP, REG_AVID_START_PIXEL_MSB, 0x00},
160 {TOK_SKIP, REG_AVID_STOP_PIXEL_LSB, 0x25},
161 {TOK_SKIP, REG_AVID_STOP_PIXEL_MSB, 0x03},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300162 /* NTSC timing */
163 {TOK_SKIP, REG_HSYNC_START_PIXEL_LSB, 0x00},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300164 {TOK_SKIP, REG_HSYNC_START_PIXEL_MSB, 0x00},
165 {TOK_SKIP, REG_HSYNC_STOP_PIXEL_LSB, 0x40},
166 {TOK_SKIP, REG_HSYNC_STOP_PIXEL_MSB, 0x00},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300167 /* NTSC timing */
168 {TOK_SKIP, REG_VSYNC_START_LINE_LSB, 0x04},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300169 {TOK_SKIP, REG_VSYNC_START_LINE_MSB, 0x00},
170 {TOK_SKIP, REG_VSYNC_STOP_LINE_LSB, 0x07},
171 {TOK_SKIP, REG_VSYNC_STOP_LINE_MSB, 0x00},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300172 /* NTSC timing */
173 {TOK_SKIP, REG_VBLK_START_LINE_LSB, 0x01},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300174 {TOK_SKIP, REG_VBLK_START_LINE_MSB, 0x00},
175 {TOK_SKIP, REG_VBLK_STOP_LINE_LSB, 0x15},
176 {TOK_SKIP, REG_VBLK_STOP_LINE_MSB, 0x00},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300177 /* Reserved */
178 {TOK_SKIP, 0x26, 0x00},
179 /* Reserved */
180 {TOK_SKIP, 0x27, 0x00},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300181 {TOK_SKIP, REG_FAST_SWTICH_CONTROL, 0xCC},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300182 /* Reserved */
183 {TOK_SKIP, 0x29, 0x00},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300184 {TOK_SKIP, REG_FAST_SWTICH_SCART_DELAY, 0x00},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300185 /* Reserved */
186 {TOK_SKIP, 0x2B, 0x00},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300187 {TOK_SKIP, REG_SCART_DELAY, 0x00},
188 {TOK_SKIP, REG_CTI_DELAY, 0x00},
189 {TOK_SKIP, REG_CTI_CONTROL, 0x00},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300190 /* Reserved */
191 {TOK_SKIP, 0x2F, 0x00},
192 /* Reserved */
193 {TOK_SKIP, 0x30, 0x00},
194 /* Reserved */
195 {TOK_SKIP, 0x31, 0x00},
196 /* HS, VS active high */
197 {TOK_WRITE, REG_SYNC_CONTROL, 0x00},
198 /* 10-bit BT.656 */
199 {TOK_WRITE, REG_OUTPUT_FORMATTER1, 0x00},
200 /* Enable clk & data */
201 {TOK_WRITE, REG_OUTPUT_FORMATTER2, 0x11},
202 /* Enable AVID & FLD */
203 {TOK_WRITE, REG_OUTPUT_FORMATTER3, 0xEE},
204 /* Enable VS & HS */
205 {TOK_WRITE, REG_OUTPUT_FORMATTER4, 0xAF},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300206 {TOK_WRITE, REG_OUTPUT_FORMATTER5, 0xFF},
207 {TOK_WRITE, REG_OUTPUT_FORMATTER6, 0xFF},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300208 /* Clear status */
209 {TOK_WRITE, REG_CLEAR_LOST_LOCK, 0x01},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300210 {TOK_TERM, 0, 0},
211};
212
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300213/**
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -0300214 * List of image formats supported by TVP5146/47 decoder
215 * Currently we are using 8 bit mode only, but can be
216 * extended to 10/20 bit mode.
217 */
218static const struct v4l2_fmtdesc tvp514x_fmt_list[] = {
219 {
220 .index = 0,
221 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
222 .flags = 0,
223 .description = "8-bit UYVY 4:2:2 Format",
224 .pixelformat = V4L2_PIX_FMT_UYVY,
225 },
226};
227
228/**
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300229 * Supported standards -
230 *
231 * Currently supports two standards only, need to add support for rest of the
232 * modes, like SECAM, etc...
233 */
Hans Verkuila75ffc12010-05-09 06:32:47 -0300234static const struct tvp514x_std_info tvp514x_std_list[] = {
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300235 /* Standard: STD_NTSC_MJ */
236 [STD_NTSC_MJ] = {
237 .width = NTSC_NUM_ACTIVE_PIXELS,
238 .height = NTSC_NUM_ACTIVE_LINES,
239 .video_std = VIDEO_STD_NTSC_MJ_BIT,
240 .standard = {
241 .index = 0,
242 .id = V4L2_STD_NTSC,
243 .name = "NTSC",
244 .frameperiod = {1001, 30000},
245 .framelines = 525
246 },
247 /* Standard: STD_PAL_BDGHIN */
248 },
249 [STD_PAL_BDGHIN] = {
250 .width = PAL_NUM_ACTIVE_PIXELS,
251 .height = PAL_NUM_ACTIVE_LINES,
252 .video_std = VIDEO_STD_PAL_BDGHIN_BIT,
253 .standard = {
254 .index = 1,
255 .id = V4L2_STD_PAL,
256 .name = "PAL",
257 .frameperiod = {1, 25},
258 .framelines = 625
259 },
260 },
261 /* Standard: need to add for additional standard */
262};
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300263
264
265static inline struct tvp514x_decoder *to_decoder(struct v4l2_subdev *sd)
266{
267 return container_of(sd, struct tvp514x_decoder, sd);
268}
269
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300270static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
271{
272 return &container_of(ctrl->handler, struct tvp514x_decoder, hdl)->sd;
273}
274
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300275
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300276/**
277 * tvp514x_read_reg() - Read a value from a register in an TVP5146/47.
278 * @sd: ptr to v4l2_subdev struct
279 * @reg: TVP5146/47 register address
280 *
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300281 * Returns value read if successful, or non-zero (-1) otherwise.
282 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300283static int tvp514x_read_reg(struct v4l2_subdev *sd, u8 reg)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300284{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300285 int err, retry = 0;
286 struct i2c_client *client = v4l2_get_subdevdata(sd);
287
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300288read_again:
289
290 err = i2c_smbus_read_byte_data(client, reg);
Sebastian Andrzej Siewior6f901a92009-11-04 15:35:09 -0300291 if (err < 0) {
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300292 if (retry <= I2C_RETRY_COUNT) {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300293 v4l2_warn(sd, "Read: retry ... %d\n", retry);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300294 retry++;
295 msleep_interruptible(10);
296 goto read_again;
297 }
298 }
299
300 return err;
301}
302
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300303/**
304 * dump_reg() - dump the register content of TVP5146/47.
305 * @sd: ptr to v4l2_subdev struct
306 * @reg: TVP5146/47 register address
307 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300308static void dump_reg(struct v4l2_subdev *sd, u8 reg)
309{
310 u32 val;
311
312 val = tvp514x_read_reg(sd, reg);
313 v4l2_info(sd, "Reg(0x%.2X): 0x%.2X\n", reg, val);
314}
315
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300316/**
317 * tvp514x_write_reg() - Write a value to a register in TVP5146/47
318 * @sd: ptr to v4l2_subdev struct
319 * @reg: TVP5146/47 register address
320 * @val: value to be written to the register
321 *
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300322 * Write a value to a register in an TVP5146/47 decoder device.
323 * Returns zero if successful, or non-zero otherwise.
324 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300325static int tvp514x_write_reg(struct v4l2_subdev *sd, u8 reg, u8 val)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300326{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300327 int err, retry = 0;
328 struct i2c_client *client = v4l2_get_subdevdata(sd);
329
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300330write_again:
331
332 err = i2c_smbus_write_byte_data(client, reg, val);
333 if (err) {
334 if (retry <= I2C_RETRY_COUNT) {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300335 v4l2_warn(sd, "Write: retry ... %d\n", retry);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300336 retry++;
337 msleep_interruptible(10);
338 goto write_again;
339 }
340 }
341
342 return err;
343}
344
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300345/**
346 * tvp514x_write_regs() : Initializes a list of TVP5146/47 registers
347 * @sd: ptr to v4l2_subdev struct
348 * @reglist: list of TVP5146/47 registers and values
349 *
350 * Initializes a list of TVP5146/47 registers:-
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300351 * if token is TOK_TERM, then entire write operation terminates
352 * if token is TOK_DELAY, then a delay of 'val' msec is introduced
353 * if token is TOK_SKIP, then the register write is skipped
354 * if token is TOK_WRITE, then the register write is performed
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300355 * Returns zero if successful, or non-zero otherwise.
356 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300357static int tvp514x_write_regs(struct v4l2_subdev *sd,
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300358 const struct tvp514x_reg reglist[])
359{
360 int err;
361 const struct tvp514x_reg *next = reglist;
362
363 for (; next->token != TOK_TERM; next++) {
364 if (next->token == TOK_DELAY) {
365 msleep(next->val);
366 continue;
367 }
368
369 if (next->token == TOK_SKIP)
370 continue;
371
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300372 err = tvp514x_write_reg(sd, next->reg, (u8) next->val);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300373 if (err) {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300374 v4l2_err(sd, "Write failed. Err[%d]\n", err);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300375 return err;
376 }
377 }
378 return 0;
379}
380
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300381/**
Hans Verkuil2db4e782010-05-09 06:30:15 -0300382 * tvp514x_query_current_std() : Query the current standard detected by TVP5146/47
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300383 * @sd: ptr to v4l2_subdev struct
384 *
Hans Verkuil2db4e782010-05-09 06:30:15 -0300385 * Returns the current standard detected by TVP5146/47, STD_INVALID if there is no
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300386 * standard detected.
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300387 */
Hans Verkuil2db4e782010-05-09 06:30:15 -0300388static enum tvp514x_std tvp514x_query_current_std(struct v4l2_subdev *sd)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300389{
390 u8 std, std_status;
391
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300392 std = tvp514x_read_reg(sd, REG_VIDEO_STD);
393 if ((std & VIDEO_STD_MASK) == VIDEO_STD_AUTO_SWITCH_BIT)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300394 /* use the standard status register */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300395 std_status = tvp514x_read_reg(sd, REG_VIDEO_STD_STATUS);
396 else
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300397 /* use the standard register itself */
398 std_status = std;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300399
400 switch (std_status & VIDEO_STD_MASK) {
401 case VIDEO_STD_NTSC_MJ_BIT:
402 return STD_NTSC_MJ;
403
404 case VIDEO_STD_PAL_BDGHIN_BIT:
405 return STD_PAL_BDGHIN;
406
407 default:
408 return STD_INVALID;
409 }
410
411 return STD_INVALID;
412}
413
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300414/* TVP5146/47 register dump function */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300415static void tvp514x_reg_dump(struct v4l2_subdev *sd)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300416{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300417 dump_reg(sd, REG_INPUT_SEL);
418 dump_reg(sd, REG_AFE_GAIN_CTRL);
419 dump_reg(sd, REG_VIDEO_STD);
420 dump_reg(sd, REG_OPERATION_MODE);
421 dump_reg(sd, REG_COLOR_KILLER);
422 dump_reg(sd, REG_LUMA_CONTROL1);
423 dump_reg(sd, REG_LUMA_CONTROL2);
424 dump_reg(sd, REG_LUMA_CONTROL3);
425 dump_reg(sd, REG_BRIGHTNESS);
426 dump_reg(sd, REG_CONTRAST);
427 dump_reg(sd, REG_SATURATION);
428 dump_reg(sd, REG_HUE);
429 dump_reg(sd, REG_CHROMA_CONTROL1);
430 dump_reg(sd, REG_CHROMA_CONTROL2);
431 dump_reg(sd, REG_COMP_PR_SATURATION);
432 dump_reg(sd, REG_COMP_Y_CONTRAST);
433 dump_reg(sd, REG_COMP_PB_SATURATION);
434 dump_reg(sd, REG_COMP_Y_BRIGHTNESS);
435 dump_reg(sd, REG_AVID_START_PIXEL_LSB);
436 dump_reg(sd, REG_AVID_START_PIXEL_MSB);
437 dump_reg(sd, REG_AVID_STOP_PIXEL_LSB);
438 dump_reg(sd, REG_AVID_STOP_PIXEL_MSB);
439 dump_reg(sd, REG_HSYNC_START_PIXEL_LSB);
440 dump_reg(sd, REG_HSYNC_START_PIXEL_MSB);
441 dump_reg(sd, REG_HSYNC_STOP_PIXEL_LSB);
442 dump_reg(sd, REG_HSYNC_STOP_PIXEL_MSB);
443 dump_reg(sd, REG_VSYNC_START_LINE_LSB);
444 dump_reg(sd, REG_VSYNC_START_LINE_MSB);
445 dump_reg(sd, REG_VSYNC_STOP_LINE_LSB);
446 dump_reg(sd, REG_VSYNC_STOP_LINE_MSB);
447 dump_reg(sd, REG_VBLK_START_LINE_LSB);
448 dump_reg(sd, REG_VBLK_START_LINE_MSB);
449 dump_reg(sd, REG_VBLK_STOP_LINE_LSB);
450 dump_reg(sd, REG_VBLK_STOP_LINE_MSB);
451 dump_reg(sd, REG_SYNC_CONTROL);
452 dump_reg(sd, REG_OUTPUT_FORMATTER1);
453 dump_reg(sd, REG_OUTPUT_FORMATTER2);
454 dump_reg(sd, REG_OUTPUT_FORMATTER3);
455 dump_reg(sd, REG_OUTPUT_FORMATTER4);
456 dump_reg(sd, REG_OUTPUT_FORMATTER5);
457 dump_reg(sd, REG_OUTPUT_FORMATTER6);
458 dump_reg(sd, REG_CLEAR_LOST_LOCK);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300459}
460
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300461/**
462 * tvp514x_configure() - Configure the TVP5146/47 registers
463 * @sd: ptr to v4l2_subdev struct
464 * @decoder: ptr to tvp514x_decoder structure
465 *
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300466 * Returns zero if successful, or non-zero otherwise.
467 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300468static int tvp514x_configure(struct v4l2_subdev *sd,
469 struct tvp514x_decoder *decoder)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300470{
471 int err;
472
473 /* common register initialization */
474 err =
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300475 tvp514x_write_regs(sd, decoder->tvp514x_regs);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300476 if (err)
477 return err;
478
479 if (debug)
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300480 tvp514x_reg_dump(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300481
482 return 0;
483}
484
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300485/**
486 * tvp514x_detect() - Detect if an tvp514x is present, and if so which revision.
487 * @sd: pointer to standard V4L2 sub-device structure
488 * @decoder: pointer to tvp514x_decoder structure
489 *
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300490 * A device is considered to be detected if the chip ID (LSB and MSB)
491 * registers match the expected values.
492 * Any value of the rom version register is accepted.
493 * Returns ENODEV error number if no device is detected, or zero
494 * if a device is detected.
495 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300496static int tvp514x_detect(struct v4l2_subdev *sd,
497 struct tvp514x_decoder *decoder)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300498{
499 u8 chip_id_msb, chip_id_lsb, rom_ver;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300500 struct i2c_client *client = v4l2_get_subdevdata(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300501
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300502 chip_id_msb = tvp514x_read_reg(sd, REG_CHIP_ID_MSB);
503 chip_id_lsb = tvp514x_read_reg(sd, REG_CHIP_ID_LSB);
504 rom_ver = tvp514x_read_reg(sd, REG_ROM_VERSION);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300505
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300506 v4l2_dbg(1, debug, sd,
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300507 "chip id detected msb:0x%x lsb:0x%x rom version:0x%x\n",
508 chip_id_msb, chip_id_lsb, rom_ver);
509 if ((chip_id_msb != TVP514X_CHIP_ID_MSB)
510 || ((chip_id_lsb != TVP5146_CHIP_ID_LSB)
511 && (chip_id_lsb != TVP5147_CHIP_ID_LSB))) {
512 /* We didn't read the values we expected, so this must not be
513 * an TVP5146/47.
514 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300515 v4l2_err(sd, "chip id mismatch msb:0x%x lsb:0x%x\n",
516 chip_id_msb, chip_id_lsb);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300517 return -ENODEV;
518 }
519
520 decoder->ver = rom_ver;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300521
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300522 v4l2_info(sd, "%s (Version - 0x%.2x) found at 0x%x (%s)\n",
523 client->name, decoder->ver,
524 client->addr << 1, client->adapter->name);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300525 return 0;
526}
527
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300528/**
529 * tvp514x_querystd() - V4L2 decoder interface handler for querystd
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300530 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300531 * @std_id: standard V4L2 std_id ioctl enum
532 *
533 * Returns the current standard detected by TVP5146/47. If no active input is
Hans Verkuil2db4e782010-05-09 06:30:15 -0300534 * detected then *std_id is set to 0 and the function returns 0.
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300535 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300536static int tvp514x_querystd(struct v4l2_subdev *sd, v4l2_std_id *std_id)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300537{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300538 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300539 enum tvp514x_std current_std;
540 enum tvp514x_input input_sel;
541 u8 sync_lock_status, lock_mask;
542
543 if (std_id == NULL)
544 return -EINVAL;
545
Hans Verkuilc3896482012-09-20 09:06:33 -0300546 /* To query the standard the TVP514x must power on the ADCs. */
547 if (!decoder->streaming) {
548 tvp514x_s_stream(sd, 1);
549 msleep(LOCK_RETRY_DELAY);
550 }
551
Hans Verkuil2db4e782010-05-09 06:30:15 -0300552 /* query the current standard */
553 current_std = tvp514x_query_current_std(sd);
Hans Verkuil55852cb2013-05-29 10:19:00 -0300554 if (current_std == STD_INVALID) {
555 *std_id = V4L2_STD_UNKNOWN;
Hans Verkuil2db4e782010-05-09 06:30:15 -0300556 return 0;
Hans Verkuil55852cb2013-05-29 10:19:00 -0300557 }
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300558
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300559 input_sel = decoder->input;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300560
561 switch (input_sel) {
562 case INPUT_CVBS_VI1A:
563 case INPUT_CVBS_VI1B:
564 case INPUT_CVBS_VI1C:
565 case INPUT_CVBS_VI2A:
566 case INPUT_CVBS_VI2B:
567 case INPUT_CVBS_VI2C:
568 case INPUT_CVBS_VI3A:
569 case INPUT_CVBS_VI3B:
570 case INPUT_CVBS_VI3C:
571 case INPUT_CVBS_VI4A:
572 lock_mask = STATUS_CLR_SUBCAR_LOCK_BIT |
573 STATUS_HORZ_SYNC_LOCK_BIT |
574 STATUS_VIRT_SYNC_LOCK_BIT;
575 break;
576
577 case INPUT_SVIDEO_VI2A_VI1A:
578 case INPUT_SVIDEO_VI2B_VI1B:
579 case INPUT_SVIDEO_VI2C_VI1C:
580 case INPUT_SVIDEO_VI2A_VI3A:
581 case INPUT_SVIDEO_VI2B_VI3B:
582 case INPUT_SVIDEO_VI2C_VI3C:
583 case INPUT_SVIDEO_VI4A_VI1A:
584 case INPUT_SVIDEO_VI4A_VI1B:
585 case INPUT_SVIDEO_VI4A_VI1C:
586 case INPUT_SVIDEO_VI4A_VI3A:
587 case INPUT_SVIDEO_VI4A_VI3B:
588 case INPUT_SVIDEO_VI4A_VI3C:
589 lock_mask = STATUS_HORZ_SYNC_LOCK_BIT |
590 STATUS_VIRT_SYNC_LOCK_BIT;
591 break;
592 /*Need to add other interfaces*/
593 default:
594 return -EINVAL;
595 }
596 /* check whether signal is locked */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300597 sync_lock_status = tvp514x_read_reg(sd, REG_STATUS1);
Hans Verkuil55852cb2013-05-29 10:19:00 -0300598 if (lock_mask != (sync_lock_status & lock_mask)) {
599 *std_id = V4L2_STD_UNKNOWN;
Hans Verkuil2db4e782010-05-09 06:30:15 -0300600 return 0; /* No input detected */
Hans Verkuil55852cb2013-05-29 10:19:00 -0300601 }
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300602
Hans Verkuil55852cb2013-05-29 10:19:00 -0300603 *std_id &= decoder->std_list[current_std].standard.id;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300604
Hans Verkuil2db4e782010-05-09 06:30:15 -0300605 v4l2_dbg(1, debug, sd, "Current STD: %s\n",
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300606 decoder->std_list[current_std].standard.name);
607 return 0;
608}
609
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300610/**
611 * tvp514x_s_std() - V4L2 decoder interface handler for s_std
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300612 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300613 * @std_id: standard V4L2 v4l2_std_id ioctl enum
614 *
615 * If std_id is supported, sets the requested standard. Otherwise, returns
616 * -EINVAL
617 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300618static int tvp514x_s_std(struct v4l2_subdev *sd, v4l2_std_id std_id)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300619{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300620 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300621 int err, i;
622
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300623 for (i = 0; i < decoder->num_stds; i++)
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300624 if (std_id & decoder->std_list[i].standard.id)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300625 break;
626
627 if ((i == decoder->num_stds) || (i == STD_INVALID))
628 return -EINVAL;
629
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300630 err = tvp514x_write_reg(sd, REG_VIDEO_STD,
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300631 decoder->std_list[i].video_std);
632 if (err)
633 return err;
634
635 decoder->current_std = i;
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300636 decoder->tvp514x_regs[REG_VIDEO_STD].val =
637 decoder->std_list[i].video_std;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300638
Hans Verkuil3907b072010-05-09 06:39:44 -0300639 v4l2_dbg(1, debug, sd, "Standard set to: %s\n",
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300640 decoder->std_list[i].standard.name);
641 return 0;
642}
643
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300644/**
645 * tvp514x_s_routing() - V4L2 decoder interface handler for s_routing
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300646 * @sd: pointer to standard V4L2 sub-device structure
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300647 * @input: input selector for routing the signal
648 * @output: output selector for routing the signal
649 * @config: config value. Not used
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300650 *
651 * If index is valid, selects the requested input. Otherwise, returns -EINVAL if
652 * the input is not supported or there is no active signal present in the
653 * selected input.
654 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300655static int tvp514x_s_routing(struct v4l2_subdev *sd,
656 u32 input, u32 output, u32 config)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300657{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300658 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300659 int err;
660 enum tvp514x_input input_sel;
661 enum tvp514x_output output_sel;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300662
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300663 if ((input >= INPUT_INVALID) ||
664 (output >= OUTPUT_INVALID))
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300665 /* Index out of bound */
666 return -EINVAL;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300667
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300668 input_sel = input;
669 output_sel = output;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300670
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300671 err = tvp514x_write_reg(sd, REG_INPUT_SEL, input_sel);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300672 if (err)
673 return err;
674
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300675 output_sel |= tvp514x_read_reg(sd,
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300676 REG_OUTPUT_FORMATTER1) & 0x7;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300677 err = tvp514x_write_reg(sd, REG_OUTPUT_FORMATTER1,
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300678 output_sel);
679 if (err)
680 return err;
681
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300682 decoder->tvp514x_regs[REG_INPUT_SEL].val = input_sel;
683 decoder->tvp514x_regs[REG_OUTPUT_FORMATTER1].val = output_sel;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300684 decoder->input = input;
685 decoder->output = output;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300686
Hans Verkuil2db4e782010-05-09 06:30:15 -0300687 v4l2_dbg(1, debug, sd, "Input set to: %d\n", input_sel);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300688
689 return 0;
690}
691
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300692/**
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300693 * tvp514x_s_ctrl() - V4L2 decoder interface handler for s_ctrl
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300694 * @ctrl: pointer to v4l2_ctrl structure
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300695 *
696 * If the requested control is supported, sets the control's current
697 * value in HW. Otherwise, returns -EINVAL if the control is not supported.
698 */
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300699static int tvp514x_s_ctrl(struct v4l2_ctrl *ctrl)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300700{
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300701 struct v4l2_subdev *sd = to_sd(ctrl);
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300702 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300703 int err = -EINVAL, value;
704
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300705 value = ctrl->val;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300706
707 switch (ctrl->id) {
708 case V4L2_CID_BRIGHTNESS:
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300709 err = tvp514x_write_reg(sd, REG_BRIGHTNESS, value);
710 if (!err)
711 decoder->tvp514x_regs[REG_BRIGHTNESS].val = value;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300712 break;
713 case V4L2_CID_CONTRAST:
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300714 err = tvp514x_write_reg(sd, REG_CONTRAST, value);
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300715 if (!err)
716 decoder->tvp514x_regs[REG_CONTRAST].val = value;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300717 break;
718 case V4L2_CID_SATURATION:
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300719 err = tvp514x_write_reg(sd, REG_SATURATION, value);
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300720 if (!err)
721 decoder->tvp514x_regs[REG_SATURATION].val = value;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300722 break;
723 case V4L2_CID_HUE:
724 if (value == 180)
725 value = 0x7F;
726 else if (value == -180)
727 value = 0x80;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300728 err = tvp514x_write_reg(sd, REG_HUE, value);
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300729 if (!err)
730 decoder->tvp514x_regs[REG_HUE].val = value;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300731 break;
732 case V4L2_CID_AUTOGAIN:
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300733 err = tvp514x_write_reg(sd, REG_AFE_GAIN_CTRL, value ? 0x0f : 0x0c);
734 if (!err)
735 decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val = value;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300736 break;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300737 }
738
Hans Verkuil3907b072010-05-09 06:39:44 -0300739 v4l2_dbg(1, debug, sd, "Set Control: ID - %d - %d\n",
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300740 ctrl->id, ctrl->val);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300741 return err;
742}
743
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300744/**
Hans Verkuil83811912010-05-09 06:50:25 -0300745 * tvp514x_enum_mbus_fmt() - V4L2 decoder interface handler for enum_mbus_fmt
746 * @sd: pointer to standard V4L2 sub-device structure
747 * @index: index of pixelcode to retrieve
748 * @code: receives the pixelcode
749 *
750 * Enumerates supported mediabus formats
751 */
752static int
753tvp514x_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned index,
754 enum v4l2_mbus_pixelcode *code)
755{
756 if (index)
757 return -EINVAL;
758
759 *code = V4L2_MBUS_FMT_YUYV10_2X10;
760 return 0;
761}
762
763/**
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -0300764 * tvp514x_mbus_fmt() - V4L2 decoder interface handler for try/s/g_mbus_fmt
Hans Verkuil83811912010-05-09 06:50:25 -0300765 * @sd: pointer to standard V4L2 sub-device structure
766 * @f: pointer to the mediabus format structure
767 *
768 * Negotiates the image capture size and mediabus format.
769 */
770static int
771tvp514x_mbus_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *f)
772{
773 struct tvp514x_decoder *decoder = to_decoder(sd);
774 enum tvp514x_std current_std;
775
776 if (f == NULL)
777 return -EINVAL;
778
779 /* Calculate height and width based on current standard */
780 current_std = decoder->current_std;
781
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -0300782 f->code = V4L2_MBUS_FMT_YUYV8_2X8;
Hans Verkuil83811912010-05-09 06:50:25 -0300783 f->width = decoder->std_list[current_std].width;
784 f->height = decoder->std_list[current_std].height;
785 f->field = V4L2_FIELD_INTERLACED;
786 f->colorspace = V4L2_COLORSPACE_SMPTE170M;
Hans Verkuil83811912010-05-09 06:50:25 -0300787 v4l2_dbg(1, debug, sd, "MBUS_FMT: Width - %d, Height - %d\n",
788 f->width, f->height);
789 return 0;
790}
791
792/**
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300793 * tvp514x_g_parm() - V4L2 decoder interface handler for g_parm
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300794 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300795 * @a: pointer to standard V4L2 VIDIOC_G_PARM ioctl structure
796 *
797 * Returns the decoder's video CAPTURE parameters.
798 */
799static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300800tvp514x_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300801{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300802 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300803 struct v4l2_captureparm *cparm;
804 enum tvp514x_std current_std;
805
806 if (a == NULL)
807 return -EINVAL;
808
809 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300810 /* only capture is supported */
811 return -EINVAL;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300812
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300813 /* get the current standard */
Hans Verkuil2db4e782010-05-09 06:30:15 -0300814 current_std = decoder->current_std;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300815
816 cparm = &a->parm.capture;
817 cparm->capability = V4L2_CAP_TIMEPERFRAME;
818 cparm->timeperframe =
819 decoder->std_list[current_std].standard.frameperiod;
820
821 return 0;
822}
823
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300824/**
825 * tvp514x_s_parm() - V4L2 decoder interface handler for s_parm
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300826 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300827 * @a: pointer to standard V4L2 VIDIOC_S_PARM ioctl structure
828 *
829 * Configures the decoder to use the input parameters, if possible. If
830 * not possible, returns the appropriate error code.
831 */
832static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300833tvp514x_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300834{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300835 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300836 struct v4l2_fract *timeperframe;
837 enum tvp514x_std current_std;
838
839 if (a == NULL)
840 return -EINVAL;
841
842 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300843 /* only capture is supported */
844 return -EINVAL;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300845
846 timeperframe = &a->parm.capture.timeperframe;
847
848 /* get the current standard */
Hans Verkuil2db4e782010-05-09 06:30:15 -0300849 current_std = decoder->current_std;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300850
851 *timeperframe =
852 decoder->std_list[current_std].standard.frameperiod;
853
854 return 0;
855}
856
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300857/**
858 * tvp514x_s_stream() - V4L2 decoder i/f handler for s_stream
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300859 * @sd: pointer to standard V4L2 sub-device structure
860 * @enable: streaming enable or disable
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300861 *
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300862 * Sets streaming to enable or disable, if possible.
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300863 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300864static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300865{
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300866 int err = 0;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300867 struct i2c_client *client = v4l2_get_subdevdata(sd);
868 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300869
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300870 if (decoder->streaming == enable)
871 return 0;
872
873 switch (enable) {
874 case 0:
875 {
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300876 /* Power Down Sequence */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300877 err = tvp514x_write_reg(sd, REG_OPERATION_MODE, 0x01);
878 if (err) {
879 v4l2_err(sd, "Unable to turn off decoder\n");
880 return err;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300881 }
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300882 decoder->streaming = enable;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300883 break;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300884 }
885 case 1:
886 {
887 struct tvp514x_reg *int_seq = (struct tvp514x_reg *)
888 client->driver->id_table->driver_data;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300889
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300890 /* Power Up Sequence */
891 err = tvp514x_write_regs(sd, int_seq);
892 if (err) {
893 v4l2_err(sd, "Unable to turn on decoder\n");
894 return err;
895 }
896 /* Detect if not already detected */
897 err = tvp514x_detect(sd, decoder);
898 if (err) {
899 v4l2_err(sd, "Unable to detect decoder\n");
900 return err;
901 }
902 err = tvp514x_configure(sd, decoder);
903 if (err) {
904 v4l2_err(sd, "Unable to configure decoder\n");
905 return err;
906 }
907 decoder->streaming = enable;
908 break;
909 }
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300910 default:
911 err = -ENODEV;
912 break;
913 }
914
915 return err;
916}
917
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300918static const struct v4l2_ctrl_ops tvp514x_ctrl_ops = {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300919 .s_ctrl = tvp514x_s_ctrl,
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300920};
921
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -0300922/**
923 * tvp514x_enum_mbus_code() - V4L2 decoder interface handler for enum_mbus_code
924 * @sd: pointer to standard V4L2 sub-device structure
925 * @fh: file handle
926 * @code: pointer to v4l2_subdev_mbus_code_enum structure
927 *
928 * Enumertaes mbus codes supported
929 */
930static int tvp514x_enum_mbus_code(struct v4l2_subdev *sd,
931 struct v4l2_subdev_fh *fh,
932 struct v4l2_subdev_mbus_code_enum *code)
933{
934 u32 pad = code->pad;
935 u32 index = code->index;
936
937 memset(code, 0, sizeof(*code));
938 code->index = index;
939 code->pad = pad;
940
941 if (index != 0)
942 return -EINVAL;
943
944 code->code = V4L2_MBUS_FMT_YUYV8_2X8;
945
946 return 0;
947}
948
949/**
950 * tvp514x_get_pad_format() - V4L2 decoder interface handler for get pad format
951 * @sd: pointer to standard V4L2 sub-device structure
952 * @fh: file handle
953 * @format: pointer to v4l2_subdev_format structure
954 *
955 * Retrieves pad format which is active or tried based on requirement
956 */
957static int tvp514x_get_pad_format(struct v4l2_subdev *sd,
958 struct v4l2_subdev_fh *fh,
959 struct v4l2_subdev_format *format)
960{
961 struct tvp514x_decoder *decoder = to_decoder(sd);
962 __u32 which = format->which;
963
964 if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
965 format->format = decoder->format;
966 return 0;
967 }
968
969 format->format.code = V4L2_MBUS_FMT_YUYV8_2X8;
970 format->format.width = tvp514x_std_list[decoder->current_std].width;
971 format->format.height = tvp514x_std_list[decoder->current_std].height;
972 format->format.colorspace = V4L2_COLORSPACE_SMPTE170M;
973 format->format.field = V4L2_FIELD_INTERLACED;
974
975 return 0;
976}
977
978/**
979 * tvp514x_set_pad_format() - V4L2 decoder interface handler for set pad format
980 * @sd: pointer to standard V4L2 sub-device structure
981 * @fh: file handle
982 * @format: pointer to v4l2_subdev_format structure
983 *
984 * Set pad format for the output pad
985 */
986static int tvp514x_set_pad_format(struct v4l2_subdev *sd,
987 struct v4l2_subdev_fh *fh,
988 struct v4l2_subdev_format *fmt)
989{
990 struct tvp514x_decoder *decoder = to_decoder(sd);
991
992 if (fmt->format.field != V4L2_FIELD_INTERLACED ||
993 fmt->format.code != V4L2_MBUS_FMT_YUYV8_2X8 ||
994 fmt->format.colorspace != V4L2_COLORSPACE_SMPTE170M ||
995 fmt->format.width != tvp514x_std_list[decoder->current_std].width ||
996 fmt->format.height != tvp514x_std_list[decoder->current_std].height)
997 return -EINVAL;
998
999 decoder->format = fmt->format;
1000
1001 return 0;
1002}
1003
Hans Verkuilcf6832a2010-12-12 08:45:22 -03001004static const struct v4l2_subdev_core_ops tvp514x_core_ops = {
1005 .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
1006 .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
1007 .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
1008 .g_ctrl = v4l2_subdev_g_ctrl,
1009 .s_ctrl = v4l2_subdev_s_ctrl,
1010 .queryctrl = v4l2_subdev_queryctrl,
1011 .querymenu = v4l2_subdev_querymenu,
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001012 .s_std = tvp514x_s_std,
1013};
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001014
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001015static const struct v4l2_subdev_video_ops tvp514x_video_ops = {
1016 .s_routing = tvp514x_s_routing,
1017 .querystd = tvp514x_querystd,
Hans Verkuil83811912010-05-09 06:50:25 -03001018 .enum_mbus_fmt = tvp514x_enum_mbus_fmt,
1019 .g_mbus_fmt = tvp514x_mbus_fmt,
1020 .try_mbus_fmt = tvp514x_mbus_fmt,
1021 .s_mbus_fmt = tvp514x_mbus_fmt,
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001022 .g_parm = tvp514x_g_parm,
1023 .s_parm = tvp514x_s_parm,
1024 .s_stream = tvp514x_s_stream,
1025};
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001026
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -03001027static const struct v4l2_subdev_pad_ops tvp514x_pad_ops = {
1028 .enum_mbus_code = tvp514x_enum_mbus_code,
1029 .get_fmt = tvp514x_get_pad_format,
1030 .set_fmt = tvp514x_set_pad_format,
1031};
1032
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001033static const struct v4l2_subdev_ops tvp514x_ops = {
1034 .core = &tvp514x_core_ops,
1035 .video = &tvp514x_video_ops,
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -03001036 .pad = &tvp514x_pad_ops,
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001037};
1038
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001039static struct tvp514x_decoder tvp514x_dev = {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001040 .streaming = 0,
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -03001041 .fmt_list = tvp514x_fmt_list,
1042 .num_fmts = ARRAY_SIZE(tvp514x_fmt_list),
1043 .pix = {
1044 /* Default to NTSC 8-bit YUV 422 */
1045 .width = NTSC_NUM_ACTIVE_PIXELS,
1046 .height = NTSC_NUM_ACTIVE_LINES,
1047 .pixelformat = V4L2_PIX_FMT_UYVY,
1048 .field = V4L2_FIELD_INTERLACED,
1049 .bytesperline = NTSC_NUM_ACTIVE_PIXELS * 2,
1050 .sizeimage = NTSC_NUM_ACTIVE_PIXELS * 2 *
1051 NTSC_NUM_ACTIVE_LINES,
1052 .colorspace = V4L2_COLORSPACE_SMPTE170M,
1053 },
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001054 .current_std = STD_NTSC_MJ,
1055 .std_list = tvp514x_std_list,
1056 .num_stds = ARRAY_SIZE(tvp514x_std_list),
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001057
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001058};
1059
Lad, Prabhakarb610b592013-06-04 12:26:23 -03001060static struct tvp514x_platform_data *
1061tvp514x_get_pdata(struct i2c_client *client)
1062{
1063 struct tvp514x_platform_data *pdata;
1064 struct v4l2_of_endpoint bus_cfg;
1065 struct device_node *endpoint;
1066 unsigned int flags;
1067
1068 if (!IS_ENABLED(CONFIG_OF) || !client->dev.of_node)
1069 return client->dev.platform_data;
1070
1071 endpoint = v4l2_of_get_next_endpoint(client->dev.of_node, NULL);
1072 if (!endpoint)
1073 return NULL;
1074
1075 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
1076 if (!pdata)
1077 goto done;
1078
1079 v4l2_of_parse_endpoint(endpoint, &bus_cfg);
1080 flags = bus_cfg.bus.parallel.flags;
1081
1082 if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
1083 pdata->hs_polarity = 1;
1084
1085 if (flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
1086 pdata->vs_polarity = 1;
1087
1088 if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
1089 pdata->clk_polarity = 1;
1090
1091done:
1092 of_node_put(endpoint);
1093 return pdata;
1094}
1095
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001096/**
1097 * tvp514x_probe() - decoder driver i2c probe handler
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001098 * @client: i2c driver client device structure
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001099 * @id: i2c driver id table
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001100 *
1101 * Register decoder as an i2c client device and V4L2
1102 * device.
1103 */
1104static int
1105tvp514x_probe(struct i2c_client *client, const struct i2c_device_id *id)
1106{
Lad, Prabhakarb610b592013-06-04 12:26:23 -03001107 struct tvp514x_platform_data *pdata = tvp514x_get_pdata(client);
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001108 struct tvp514x_decoder *decoder;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001109 struct v4l2_subdev *sd;
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -03001110 int ret;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001111
Lad, Prabhakarb610b592013-06-04 12:26:23 -03001112 if (pdata == NULL) {
1113 dev_err(&client->dev, "No platform data\n");
1114 return -EINVAL;
1115 }
1116
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001117 /* Check if the adapter supports the needed features */
1118 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1119 return -EIO;
1120
Lad, Prabhakar08754d32013-01-03 10:04:57 -03001121 decoder = devm_kzalloc(&client->dev, sizeof(*decoder), GFP_KERNEL);
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001122 if (!decoder)
1123 return -ENOMEM;
1124
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001125 /* Initialize the tvp514x_decoder with default configuration */
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001126 *decoder = tvp514x_dev;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001127 /* Copy default register configuration */
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001128 memcpy(decoder->tvp514x_regs, tvp514x_reg_list_default,
1129 sizeof(tvp514x_reg_list_default));
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001130
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001131 /* Copy board specific information here */
Lad, Prabhakarb610b592013-06-04 12:26:23 -03001132 decoder->pdata = pdata;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001133
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001134 /**
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001135 * Fetch platform specific data, and configure the
1136 * tvp514x_reg_list[] accordingly. Since this is one
1137 * time configuration, no need to preserve.
1138 */
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001139 decoder->tvp514x_regs[REG_OUTPUT_FORMATTER2].val |=
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001140 (decoder->pdata->clk_polarity << 1);
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001141 decoder->tvp514x_regs[REG_SYNC_CONTROL].val |=
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001142 ((decoder->pdata->hs_polarity << 2) |
1143 (decoder->pdata->vs_polarity << 3));
1144 /* Set default standard to auto */
1145 decoder->tvp514x_regs[REG_VIDEO_STD].val =
1146 VIDEO_STD_AUTO_SWITCH_BIT;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001147
1148 /* Register with V4L2 layer as slave device */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001149 sd = &decoder->sd;
1150 v4l2_i2c_subdev_init(sd, client, &tvp514x_ops);
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -03001151 strlcpy(sd->name, TVP514X_MODULE_NAME, sizeof(sd->name));
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001152
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -03001153#if defined(CONFIG_MEDIA_CONTROLLER)
1154 decoder->pad.flags = MEDIA_PAD_FL_SOURCE;
1155 decoder->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1156 decoder->sd.entity.flags |= MEDIA_ENT_T_V4L2_SUBDEV_DECODER;
1157
1158 ret = media_entity_init(&decoder->sd.entity, 1, &decoder->pad, 0);
1159 if (ret < 0) {
1160 v4l2_err(sd, "%s decoder driver failed to register !!\n",
1161 sd->name);
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -03001162 return ret;
1163 }
1164#endif
Hans Verkuilcf6832a2010-12-12 08:45:22 -03001165 v4l2_ctrl_handler_init(&decoder->hdl, 5);
1166 v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
1167 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
1168 v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
1169 V4L2_CID_CONTRAST, 0, 255, 1, 128);
1170 v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
1171 V4L2_CID_SATURATION, 0, 255, 1, 128);
1172 v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
1173 V4L2_CID_HUE, -180, 180, 180, 0);
1174 v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
1175 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
1176 sd->ctrl_handler = &decoder->hdl;
1177 if (decoder->hdl.error) {
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -03001178 ret = decoder->hdl.error;
Hans Verkuilcf6832a2010-12-12 08:45:22 -03001179
1180 v4l2_ctrl_handler_free(&decoder->hdl);
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -03001181 return ret;
Hans Verkuilcf6832a2010-12-12 08:45:22 -03001182 }
1183 v4l2_ctrl_handler_setup(&decoder->hdl);
1184
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001185 v4l2_info(sd, "%s decoder driver registered !!\n", sd->name);
1186
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001187 return 0;
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001188
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001189}
1190
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001191/**
1192 * tvp514x_remove() - decoder driver i2c remove handler
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001193 * @client: i2c driver client device structure
1194 *
1195 * Unregister decoder as an i2c client device and V4L2
1196 * device. Complement of tvp514x_probe().
1197 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001198static int tvp514x_remove(struct i2c_client *client)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001199{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001200 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1201 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001202
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001203 v4l2_device_unregister_subdev(sd);
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -03001204#if defined(CONFIG_MEDIA_CONTROLLER)
1205 media_entity_cleanup(&decoder->sd.entity);
1206#endif
Hans Verkuilcf6832a2010-12-12 08:45:22 -03001207 v4l2_ctrl_handler_free(&decoder->hdl);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001208 return 0;
1209}
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001210/* TVP5146 Init/Power on Sequence */
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001211static const struct tvp514x_reg tvp5146_init_reg_seq[] = {
1212 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
1213 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1214 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
1215 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1216 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1217 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1218 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1219 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1220 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
1221 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1222 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001223 {TOK_TERM, 0, 0},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001224};
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001225
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001226/* TVP5147 Init/Power on Sequence */
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001227static const struct tvp514x_reg tvp5147_init_reg_seq[] = {
1228 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
1229 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1230 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
1231 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1232 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1233 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1234 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1235 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1236 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x16},
1237 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1238 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xA0},
1239 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x16},
1240 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1241 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1242 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1243 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
1244 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1245 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001246 {TOK_TERM, 0, 0},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001247};
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001248
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001249/* TVP5146M2/TVP5147M1 Init/Power on Sequence */
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001250static const struct tvp514x_reg tvp514xm_init_reg_seq[] = {
1251 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1252 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001253 {TOK_TERM, 0, 0},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001254};
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001255
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001256/**
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001257 * I2C Device Table -
1258 *
1259 * name - Name of the actual device/chip.
1260 * driver_data - Driver data
1261 */
1262static const struct i2c_device_id tvp514x_id[] = {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001263 {"tvp5146", (unsigned long)tvp5146_init_reg_seq},
1264 {"tvp5146m2", (unsigned long)tvp514xm_init_reg_seq},
1265 {"tvp5147", (unsigned long)tvp5147_init_reg_seq},
1266 {"tvp5147m1", (unsigned long)tvp514xm_init_reg_seq},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001267 {},
1268};
1269
1270MODULE_DEVICE_TABLE(i2c, tvp514x_id);
1271
Lad, Prabhakarb610b592013-06-04 12:26:23 -03001272#if IS_ENABLED(CONFIG_OF)
1273static const struct of_device_id tvp514x_of_match[] = {
1274 { .compatible = "ti,tvp5146", },
1275 { .compatible = "ti,tvp5146m2", },
1276 { .compatible = "ti,tvp5147", },
1277 { .compatible = "ti,tvp5147m1", },
1278 { /* sentinel */ },
1279};
1280MODULE_DEVICE_TABLE(of, tvp514x_of_match);
1281#endif
1282
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001283static struct i2c_driver tvp514x_driver = {
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001284 .driver = {
Lad, Prabhakarb610b592013-06-04 12:26:23 -03001285 .of_match_table = of_match_ptr(tvp514x_of_match),
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001286 .owner = THIS_MODULE,
1287 .name = TVP514X_MODULE_NAME,
1288 },
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001289 .probe = tvp514x_probe,
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001290 .remove = tvp514x_remove,
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001291 .id_table = tvp514x_id,
1292};
1293
Axel Linc6e8d862012-02-12 06:56:32 -03001294module_i2c_driver(tvp514x_driver);