blob: d5c9347f4c6dce632b1e5af8d76f8e733c028f7f [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>
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -030015 * Prabhakar Lad <prabhakar.lad@ti.com>
Vaibhav Hiremath07b1747c2008-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 Hiremath07b1747c2008-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>
Sachin Kamat098bcba2013-10-18 00:07:13 -030038#include <linux/of.h>
Philipp Zabelfd9fdb72014-02-10 22:01:48 +010039#include <linux/of_graph.h>
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -030040
Lad, Prabhakar8f23acb2013-06-24 11:51:39 -030041#include <media/v4l2-async.h>
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -030042#include <media/v4l2-device.h>
43#include <media/v4l2-common.h>
Hans Verkuil83811912010-05-09 06:50:25 -030044#include <media/v4l2-mediabus.h>
Lad, Prabhakarb610b592013-06-04 12:26:23 -030045#include <media/v4l2-of.h>
Hans Verkuilcf6832a2010-12-12 08:45:22 -030046#include <media/v4l2-ctrls.h>
Mauro Carvalho Chehabb5dcee22015-11-10 12:01:44 -020047#include <media/i2c/tvp514x.h>
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -030048#include <media/media-entity.h>
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030049
50#include "tvp514x_regs.h"
51
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030052/* Private macros for TVP */
53#define I2C_RETRY_COUNT (5)
54#define LOCK_RETRY_COUNT (5)
55#define LOCK_RETRY_DELAY (200)
56
57/* Debug functions */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103058static bool debug;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030059module_param(debug, bool, 0644);
60MODULE_PARM_DESC(debug, "Debug level (0-1)");
61
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -030062MODULE_AUTHOR("Texas Instruments");
63MODULE_DESCRIPTION("TVP514X linux decoder driver");
64MODULE_LICENSE("GPL");
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030065
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -030066/* enum tvp514x_std - enum for supported standards */
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030067enum tvp514x_std {
68 STD_NTSC_MJ = 0,
69 STD_PAL_BDGHIN,
70 STD_INVALID
71};
72
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -030073/**
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030074 * struct tvp514x_std_info - Structure to store standard informations
75 * @width: Line width in pixels
76 * @height:Number of active lines
77 * @video_std: Value to write in REG_VIDEO_STD register
78 * @standard: v4l2 standard structure information
79 */
80struct tvp514x_std_info {
81 unsigned long width;
82 unsigned long height;
83 u8 video_std;
84 struct v4l2_standard standard;
85};
86
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -030087static struct tvp514x_reg tvp514x_reg_list_default[0x40];
Vaibhav Hiremath63b59ce2010-03-27 09:37:54 -030088
89static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable);
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -030090/**
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -030091 * struct tvp514x_decoder - TVP5146/47 decoder object
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -030092 * @sd: Subdevice Slave handle
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -030093 * @tvp514x_regs: copy of hw's regs with preset values.
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030094 * @pdata: Board specific
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030095 * @ver: Chip version
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -030096 * @streaming: TVP5146/47 decoder streaming - enabled or disabled.
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -030097 * @pix: Current pixel format
98 * @num_fmts: Number of formats
99 * @fmt_list: Format list
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300100 * @current_std: Current standard
101 * @num_stds: Number of standards
102 * @std_list: Standards list
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300103 * @input: Input routing at chip level
104 * @output: Output routing at chip level
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300105 */
106struct tvp514x_decoder {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300107 struct v4l2_subdev sd;
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300108 struct v4l2_ctrl_handler hdl;
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300109 struct tvp514x_reg tvp514x_regs[ARRAY_SIZE(tvp514x_reg_list_default)];
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300110 const struct tvp514x_platform_data *pdata;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300111
112 int ver;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300113 int streaming;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300114
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -0300115 struct v4l2_pix_format pix;
116 int num_fmts;
117 const struct v4l2_fmtdesc *fmt_list;
118
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300119 enum tvp514x_std current_std;
120 int num_stds;
Hans Verkuila75ffc12010-05-09 06:32:47 -0300121 const struct tvp514x_std_info *std_list;
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300122 /* Input and Output Routing parameters */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300123 u32 input;
124 u32 output;
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -0300125
126 /* mc related members */
127 struct media_pad pad;
128 struct v4l2_mbus_framefmt format;
Lars-Peter Clausenf0a12d02013-06-23 10:01:35 -0300129
130 struct tvp514x_reg *int_seq;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300131};
132
133/* TVP514x default register values */
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300134static struct tvp514x_reg tvp514x_reg_list_default[] = {
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300135 /* Composite selected */
136 {TOK_WRITE, REG_INPUT_SEL, 0x05},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300137 {TOK_WRITE, REG_AFE_GAIN_CTRL, 0x0F},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300138 /* Auto mode */
139 {TOK_WRITE, REG_VIDEO_STD, 0x00},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300140 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
141 {TOK_SKIP, REG_AUTOSWITCH_MASK, 0x3F},
142 {TOK_WRITE, REG_COLOR_KILLER, 0x10},
143 {TOK_WRITE, REG_LUMA_CONTROL1, 0x00},
144 {TOK_WRITE, REG_LUMA_CONTROL2, 0x00},
145 {TOK_WRITE, REG_LUMA_CONTROL3, 0x02},
146 {TOK_WRITE, REG_BRIGHTNESS, 0x80},
147 {TOK_WRITE, REG_CONTRAST, 0x80},
148 {TOK_WRITE, REG_SATURATION, 0x80},
149 {TOK_WRITE, REG_HUE, 0x00},
150 {TOK_WRITE, REG_CHROMA_CONTROL1, 0x00},
151 {TOK_WRITE, REG_CHROMA_CONTROL2, 0x0E},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300152 /* Reserved */
153 {TOK_SKIP, 0x0F, 0x00},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300154 {TOK_WRITE, REG_COMP_PR_SATURATION, 0x80},
155 {TOK_WRITE, REG_COMP_Y_CONTRAST, 0x80},
156 {TOK_WRITE, REG_COMP_PB_SATURATION, 0x80},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300157 /* Reserved */
158 {TOK_SKIP, 0x13, 0x00},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300159 {TOK_WRITE, REG_COMP_Y_BRIGHTNESS, 0x80},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300160 /* Reserved */
161 {TOK_SKIP, 0x15, 0x00},
162 /* NTSC timing */
163 {TOK_SKIP, REG_AVID_START_PIXEL_LSB, 0x55},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300164 {TOK_SKIP, REG_AVID_START_PIXEL_MSB, 0x00},
165 {TOK_SKIP, REG_AVID_STOP_PIXEL_LSB, 0x25},
166 {TOK_SKIP, REG_AVID_STOP_PIXEL_MSB, 0x03},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300167 /* NTSC timing */
168 {TOK_SKIP, REG_HSYNC_START_PIXEL_LSB, 0x00},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300169 {TOK_SKIP, REG_HSYNC_START_PIXEL_MSB, 0x00},
170 {TOK_SKIP, REG_HSYNC_STOP_PIXEL_LSB, 0x40},
171 {TOK_SKIP, REG_HSYNC_STOP_PIXEL_MSB, 0x00},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300172 /* NTSC timing */
173 {TOK_SKIP, REG_VSYNC_START_LINE_LSB, 0x04},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300174 {TOK_SKIP, REG_VSYNC_START_LINE_MSB, 0x00},
175 {TOK_SKIP, REG_VSYNC_STOP_LINE_LSB, 0x07},
176 {TOK_SKIP, REG_VSYNC_STOP_LINE_MSB, 0x00},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300177 /* NTSC timing */
178 {TOK_SKIP, REG_VBLK_START_LINE_LSB, 0x01},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300179 {TOK_SKIP, REG_VBLK_START_LINE_MSB, 0x00},
180 {TOK_SKIP, REG_VBLK_STOP_LINE_LSB, 0x15},
181 {TOK_SKIP, REG_VBLK_STOP_LINE_MSB, 0x00},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300182 /* Reserved */
183 {TOK_SKIP, 0x26, 0x00},
184 /* Reserved */
185 {TOK_SKIP, 0x27, 0x00},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300186 {TOK_SKIP, REG_FAST_SWTICH_CONTROL, 0xCC},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300187 /* Reserved */
188 {TOK_SKIP, 0x29, 0x00},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300189 {TOK_SKIP, REG_FAST_SWTICH_SCART_DELAY, 0x00},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300190 /* Reserved */
191 {TOK_SKIP, 0x2B, 0x00},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300192 {TOK_SKIP, REG_SCART_DELAY, 0x00},
193 {TOK_SKIP, REG_CTI_DELAY, 0x00},
194 {TOK_SKIP, REG_CTI_CONTROL, 0x00},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300195 /* Reserved */
196 {TOK_SKIP, 0x2F, 0x00},
197 /* Reserved */
198 {TOK_SKIP, 0x30, 0x00},
199 /* Reserved */
200 {TOK_SKIP, 0x31, 0x00},
201 /* HS, VS active high */
202 {TOK_WRITE, REG_SYNC_CONTROL, 0x00},
203 /* 10-bit BT.656 */
204 {TOK_WRITE, REG_OUTPUT_FORMATTER1, 0x00},
205 /* Enable clk & data */
206 {TOK_WRITE, REG_OUTPUT_FORMATTER2, 0x11},
207 /* Enable AVID & FLD */
208 {TOK_WRITE, REG_OUTPUT_FORMATTER3, 0xEE},
209 /* Enable VS & HS */
210 {TOK_WRITE, REG_OUTPUT_FORMATTER4, 0xAF},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300211 {TOK_WRITE, REG_OUTPUT_FORMATTER5, 0xFF},
212 {TOK_WRITE, REG_OUTPUT_FORMATTER6, 0xFF},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300213 /* Clear status */
214 {TOK_WRITE, REG_CLEAR_LOST_LOCK, 0x01},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300215 {TOK_TERM, 0, 0},
216};
217
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300218/**
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -0300219 * List of image formats supported by TVP5146/47 decoder
220 * Currently we are using 8 bit mode only, but can be
221 * extended to 10/20 bit mode.
222 */
223static const struct v4l2_fmtdesc tvp514x_fmt_list[] = {
224 {
225 .index = 0,
226 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
227 .flags = 0,
228 .description = "8-bit UYVY 4:2:2 Format",
229 .pixelformat = V4L2_PIX_FMT_UYVY,
230 },
231};
232
233/**
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300234 * Supported standards -
235 *
236 * Currently supports two standards only, need to add support for rest of the
237 * modes, like SECAM, etc...
238 */
Hans Verkuila75ffc12010-05-09 06:32:47 -0300239static const struct tvp514x_std_info tvp514x_std_list[] = {
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300240 /* Standard: STD_NTSC_MJ */
241 [STD_NTSC_MJ] = {
242 .width = NTSC_NUM_ACTIVE_PIXELS,
243 .height = NTSC_NUM_ACTIVE_LINES,
244 .video_std = VIDEO_STD_NTSC_MJ_BIT,
245 .standard = {
246 .index = 0,
247 .id = V4L2_STD_NTSC,
248 .name = "NTSC",
249 .frameperiod = {1001, 30000},
250 .framelines = 525
251 },
252 /* Standard: STD_PAL_BDGHIN */
253 },
254 [STD_PAL_BDGHIN] = {
255 .width = PAL_NUM_ACTIVE_PIXELS,
256 .height = PAL_NUM_ACTIVE_LINES,
257 .video_std = VIDEO_STD_PAL_BDGHIN_BIT,
258 .standard = {
259 .index = 1,
260 .id = V4L2_STD_PAL,
261 .name = "PAL",
262 .frameperiod = {1, 25},
263 .framelines = 625
264 },
265 },
266 /* Standard: need to add for additional standard */
267};
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300268
269
270static inline struct tvp514x_decoder *to_decoder(struct v4l2_subdev *sd)
271{
272 return container_of(sd, struct tvp514x_decoder, sd);
273}
274
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300275static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
276{
277 return &container_of(ctrl->handler, struct tvp514x_decoder, hdl)->sd;
278}
279
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300280
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300281/**
282 * tvp514x_read_reg() - Read a value from a register in an TVP5146/47.
283 * @sd: ptr to v4l2_subdev struct
284 * @reg: TVP5146/47 register address
285 *
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300286 * Returns value read if successful, or non-zero (-1) otherwise.
287 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300288static int tvp514x_read_reg(struct v4l2_subdev *sd, u8 reg)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300289{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300290 int err, retry = 0;
291 struct i2c_client *client = v4l2_get_subdevdata(sd);
292
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300293read_again:
294
295 err = i2c_smbus_read_byte_data(client, reg);
Sebastian Andrzej Siewior6f901a92009-11-04 15:35:09 -0300296 if (err < 0) {
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300297 if (retry <= I2C_RETRY_COUNT) {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300298 v4l2_warn(sd, "Read: retry ... %d\n", retry);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300299 retry++;
300 msleep_interruptible(10);
301 goto read_again;
302 }
303 }
304
305 return err;
306}
307
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300308/**
309 * dump_reg() - dump the register content of TVP5146/47.
310 * @sd: ptr to v4l2_subdev struct
311 * @reg: TVP5146/47 register address
312 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300313static void dump_reg(struct v4l2_subdev *sd, u8 reg)
314{
315 u32 val;
316
317 val = tvp514x_read_reg(sd, reg);
318 v4l2_info(sd, "Reg(0x%.2X): 0x%.2X\n", reg, val);
319}
320
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300321/**
322 * tvp514x_write_reg() - Write a value to a register in TVP5146/47
323 * @sd: ptr to v4l2_subdev struct
324 * @reg: TVP5146/47 register address
325 * @val: value to be written to the register
326 *
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300327 * Write a value to a register in an TVP5146/47 decoder device.
328 * Returns zero if successful, or non-zero otherwise.
329 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300330static int tvp514x_write_reg(struct v4l2_subdev *sd, u8 reg, u8 val)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300331{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300332 int err, retry = 0;
333 struct i2c_client *client = v4l2_get_subdevdata(sd);
334
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300335write_again:
336
337 err = i2c_smbus_write_byte_data(client, reg, val);
338 if (err) {
339 if (retry <= I2C_RETRY_COUNT) {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300340 v4l2_warn(sd, "Write: retry ... %d\n", retry);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300341 retry++;
342 msleep_interruptible(10);
343 goto write_again;
344 }
345 }
346
347 return err;
348}
349
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300350/**
351 * tvp514x_write_regs() : Initializes a list of TVP5146/47 registers
352 * @sd: ptr to v4l2_subdev struct
353 * @reglist: list of TVP5146/47 registers and values
354 *
355 * Initializes a list of TVP5146/47 registers:-
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300356 * if token is TOK_TERM, then entire write operation terminates
357 * if token is TOK_DELAY, then a delay of 'val' msec is introduced
358 * if token is TOK_SKIP, then the register write is skipped
359 * if token is TOK_WRITE, then the register write is performed
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300360 * Returns zero if successful, or non-zero otherwise.
361 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300362static int tvp514x_write_regs(struct v4l2_subdev *sd,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300363 const struct tvp514x_reg reglist[])
364{
365 int err;
366 const struct tvp514x_reg *next = reglist;
367
368 for (; next->token != TOK_TERM; next++) {
369 if (next->token == TOK_DELAY) {
370 msleep(next->val);
371 continue;
372 }
373
374 if (next->token == TOK_SKIP)
375 continue;
376
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300377 err = tvp514x_write_reg(sd, next->reg, (u8) next->val);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300378 if (err) {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300379 v4l2_err(sd, "Write failed. Err[%d]\n", err);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300380 return err;
381 }
382 }
383 return 0;
384}
385
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300386/**
Hans Verkuil2db4e782010-05-09 06:30:15 -0300387 * tvp514x_query_current_std() : Query the current standard detected by TVP5146/47
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300388 * @sd: ptr to v4l2_subdev struct
389 *
Hans Verkuil2db4e782010-05-09 06:30:15 -0300390 * Returns the current standard detected by TVP5146/47, STD_INVALID if there is no
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300391 * standard detected.
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300392 */
Hans Verkuil2db4e782010-05-09 06:30:15 -0300393static enum tvp514x_std tvp514x_query_current_std(struct v4l2_subdev *sd)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300394{
395 u8 std, std_status;
396
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300397 std = tvp514x_read_reg(sd, REG_VIDEO_STD);
398 if ((std & VIDEO_STD_MASK) == VIDEO_STD_AUTO_SWITCH_BIT)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300399 /* use the standard status register */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300400 std_status = tvp514x_read_reg(sd, REG_VIDEO_STD_STATUS);
401 else
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300402 /* use the standard register itself */
403 std_status = std;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300404
405 switch (std_status & VIDEO_STD_MASK) {
406 case VIDEO_STD_NTSC_MJ_BIT:
407 return STD_NTSC_MJ;
408
409 case VIDEO_STD_PAL_BDGHIN_BIT:
410 return STD_PAL_BDGHIN;
411
412 default:
413 return STD_INVALID;
414 }
415
416 return STD_INVALID;
417}
418
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300419/* TVP5146/47 register dump function */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300420static void tvp514x_reg_dump(struct v4l2_subdev *sd)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300421{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300422 dump_reg(sd, REG_INPUT_SEL);
423 dump_reg(sd, REG_AFE_GAIN_CTRL);
424 dump_reg(sd, REG_VIDEO_STD);
425 dump_reg(sd, REG_OPERATION_MODE);
426 dump_reg(sd, REG_COLOR_KILLER);
427 dump_reg(sd, REG_LUMA_CONTROL1);
428 dump_reg(sd, REG_LUMA_CONTROL2);
429 dump_reg(sd, REG_LUMA_CONTROL3);
430 dump_reg(sd, REG_BRIGHTNESS);
431 dump_reg(sd, REG_CONTRAST);
432 dump_reg(sd, REG_SATURATION);
433 dump_reg(sd, REG_HUE);
434 dump_reg(sd, REG_CHROMA_CONTROL1);
435 dump_reg(sd, REG_CHROMA_CONTROL2);
436 dump_reg(sd, REG_COMP_PR_SATURATION);
437 dump_reg(sd, REG_COMP_Y_CONTRAST);
438 dump_reg(sd, REG_COMP_PB_SATURATION);
439 dump_reg(sd, REG_COMP_Y_BRIGHTNESS);
440 dump_reg(sd, REG_AVID_START_PIXEL_LSB);
441 dump_reg(sd, REG_AVID_START_PIXEL_MSB);
442 dump_reg(sd, REG_AVID_STOP_PIXEL_LSB);
443 dump_reg(sd, REG_AVID_STOP_PIXEL_MSB);
444 dump_reg(sd, REG_HSYNC_START_PIXEL_LSB);
445 dump_reg(sd, REG_HSYNC_START_PIXEL_MSB);
446 dump_reg(sd, REG_HSYNC_STOP_PIXEL_LSB);
447 dump_reg(sd, REG_HSYNC_STOP_PIXEL_MSB);
448 dump_reg(sd, REG_VSYNC_START_LINE_LSB);
449 dump_reg(sd, REG_VSYNC_START_LINE_MSB);
450 dump_reg(sd, REG_VSYNC_STOP_LINE_LSB);
451 dump_reg(sd, REG_VSYNC_STOP_LINE_MSB);
452 dump_reg(sd, REG_VBLK_START_LINE_LSB);
453 dump_reg(sd, REG_VBLK_START_LINE_MSB);
454 dump_reg(sd, REG_VBLK_STOP_LINE_LSB);
455 dump_reg(sd, REG_VBLK_STOP_LINE_MSB);
456 dump_reg(sd, REG_SYNC_CONTROL);
457 dump_reg(sd, REG_OUTPUT_FORMATTER1);
458 dump_reg(sd, REG_OUTPUT_FORMATTER2);
459 dump_reg(sd, REG_OUTPUT_FORMATTER3);
460 dump_reg(sd, REG_OUTPUT_FORMATTER4);
461 dump_reg(sd, REG_OUTPUT_FORMATTER5);
462 dump_reg(sd, REG_OUTPUT_FORMATTER6);
463 dump_reg(sd, REG_CLEAR_LOST_LOCK);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300464}
465
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300466/**
467 * tvp514x_configure() - Configure the TVP5146/47 registers
468 * @sd: ptr to v4l2_subdev struct
469 * @decoder: ptr to tvp514x_decoder structure
470 *
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300471 * Returns zero if successful, or non-zero otherwise.
472 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300473static int tvp514x_configure(struct v4l2_subdev *sd,
474 struct tvp514x_decoder *decoder)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300475{
476 int err;
477
478 /* common register initialization */
479 err =
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300480 tvp514x_write_regs(sd, decoder->tvp514x_regs);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300481 if (err)
482 return err;
483
484 if (debug)
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300485 tvp514x_reg_dump(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300486
487 return 0;
488}
489
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300490/**
491 * tvp514x_detect() - Detect if an tvp514x is present, and if so which revision.
492 * @sd: pointer to standard V4L2 sub-device structure
493 * @decoder: pointer to tvp514x_decoder structure
494 *
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300495 * A device is considered to be detected if the chip ID (LSB and MSB)
496 * registers match the expected values.
497 * Any value of the rom version register is accepted.
498 * Returns ENODEV error number if no device is detected, or zero
499 * if a device is detected.
500 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300501static int tvp514x_detect(struct v4l2_subdev *sd,
502 struct tvp514x_decoder *decoder)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300503{
504 u8 chip_id_msb, chip_id_lsb, rom_ver;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300505 struct i2c_client *client = v4l2_get_subdevdata(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300506
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300507 chip_id_msb = tvp514x_read_reg(sd, REG_CHIP_ID_MSB);
508 chip_id_lsb = tvp514x_read_reg(sd, REG_CHIP_ID_LSB);
509 rom_ver = tvp514x_read_reg(sd, REG_ROM_VERSION);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300510
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300511 v4l2_dbg(1, debug, sd,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300512 "chip id detected msb:0x%x lsb:0x%x rom version:0x%x\n",
513 chip_id_msb, chip_id_lsb, rom_ver);
514 if ((chip_id_msb != TVP514X_CHIP_ID_MSB)
515 || ((chip_id_lsb != TVP5146_CHIP_ID_LSB)
516 && (chip_id_lsb != TVP5147_CHIP_ID_LSB))) {
517 /* We didn't read the values we expected, so this must not be
518 * an TVP5146/47.
519 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300520 v4l2_err(sd, "chip id mismatch msb:0x%x lsb:0x%x\n",
521 chip_id_msb, chip_id_lsb);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300522 return -ENODEV;
523 }
524
525 decoder->ver = rom_ver;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300526
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300527 v4l2_info(sd, "%s (Version - 0x%.2x) found at 0x%x (%s)\n",
528 client->name, decoder->ver,
529 client->addr << 1, client->adapter->name);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300530 return 0;
531}
532
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300533/**
534 * tvp514x_querystd() - V4L2 decoder interface handler for querystd
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300535 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300536 * @std_id: standard V4L2 std_id ioctl enum
537 *
538 * Returns the current standard detected by TVP5146/47. If no active input is
Hans Verkuil2db4e782010-05-09 06:30:15 -0300539 * detected then *std_id is set to 0 and the function returns 0.
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300540 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300541static int tvp514x_querystd(struct v4l2_subdev *sd, v4l2_std_id *std_id)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300542{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300543 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300544 enum tvp514x_std current_std;
545 enum tvp514x_input input_sel;
546 u8 sync_lock_status, lock_mask;
547
548 if (std_id == NULL)
549 return -EINVAL;
550
Hans Verkuilc3896482012-09-20 09:06:33 -0300551 /* To query the standard the TVP514x must power on the ADCs. */
552 if (!decoder->streaming) {
553 tvp514x_s_stream(sd, 1);
554 msleep(LOCK_RETRY_DELAY);
555 }
556
Hans Verkuil2db4e782010-05-09 06:30:15 -0300557 /* query the current standard */
558 current_std = tvp514x_query_current_std(sd);
Hans Verkuil55852cb2013-05-29 10:19:00 -0300559 if (current_std == STD_INVALID) {
560 *std_id = V4L2_STD_UNKNOWN;
Hans Verkuil2db4e782010-05-09 06:30:15 -0300561 return 0;
Hans Verkuil55852cb2013-05-29 10:19:00 -0300562 }
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300563
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300564 input_sel = decoder->input;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300565
566 switch (input_sel) {
567 case INPUT_CVBS_VI1A:
568 case INPUT_CVBS_VI1B:
569 case INPUT_CVBS_VI1C:
570 case INPUT_CVBS_VI2A:
571 case INPUT_CVBS_VI2B:
572 case INPUT_CVBS_VI2C:
573 case INPUT_CVBS_VI3A:
574 case INPUT_CVBS_VI3B:
575 case INPUT_CVBS_VI3C:
576 case INPUT_CVBS_VI4A:
577 lock_mask = STATUS_CLR_SUBCAR_LOCK_BIT |
578 STATUS_HORZ_SYNC_LOCK_BIT |
579 STATUS_VIRT_SYNC_LOCK_BIT;
580 break;
581
582 case INPUT_SVIDEO_VI2A_VI1A:
583 case INPUT_SVIDEO_VI2B_VI1B:
584 case INPUT_SVIDEO_VI2C_VI1C:
585 case INPUT_SVIDEO_VI2A_VI3A:
586 case INPUT_SVIDEO_VI2B_VI3B:
587 case INPUT_SVIDEO_VI2C_VI3C:
588 case INPUT_SVIDEO_VI4A_VI1A:
589 case INPUT_SVIDEO_VI4A_VI1B:
590 case INPUT_SVIDEO_VI4A_VI1C:
591 case INPUT_SVIDEO_VI4A_VI3A:
592 case INPUT_SVIDEO_VI4A_VI3B:
593 case INPUT_SVIDEO_VI4A_VI3C:
594 lock_mask = STATUS_HORZ_SYNC_LOCK_BIT |
595 STATUS_VIRT_SYNC_LOCK_BIT;
596 break;
597 /*Need to add other interfaces*/
598 default:
599 return -EINVAL;
600 }
601 /* check whether signal is locked */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300602 sync_lock_status = tvp514x_read_reg(sd, REG_STATUS1);
Hans Verkuil55852cb2013-05-29 10:19:00 -0300603 if (lock_mask != (sync_lock_status & lock_mask)) {
604 *std_id = V4L2_STD_UNKNOWN;
Hans Verkuil2db4e782010-05-09 06:30:15 -0300605 return 0; /* No input detected */
Hans Verkuil55852cb2013-05-29 10:19:00 -0300606 }
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300607
Hans Verkuil55852cb2013-05-29 10:19:00 -0300608 *std_id &= decoder->std_list[current_std].standard.id;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300609
Hans Verkuil2db4e782010-05-09 06:30:15 -0300610 v4l2_dbg(1, debug, sd, "Current STD: %s\n",
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300611 decoder->std_list[current_std].standard.name);
612 return 0;
613}
614
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300615/**
616 * tvp514x_s_std() - V4L2 decoder interface handler for s_std
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300617 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300618 * @std_id: standard V4L2 v4l2_std_id ioctl enum
619 *
620 * If std_id is supported, sets the requested standard. Otherwise, returns
621 * -EINVAL
622 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300623static int tvp514x_s_std(struct v4l2_subdev *sd, v4l2_std_id std_id)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300624{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300625 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300626 int err, i;
627
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300628 for (i = 0; i < decoder->num_stds; i++)
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300629 if (std_id & decoder->std_list[i].standard.id)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300630 break;
631
632 if ((i == decoder->num_stds) || (i == STD_INVALID))
633 return -EINVAL;
634
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300635 err = tvp514x_write_reg(sd, REG_VIDEO_STD,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300636 decoder->std_list[i].video_std);
637 if (err)
638 return err;
639
640 decoder->current_std = i;
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300641 decoder->tvp514x_regs[REG_VIDEO_STD].val =
642 decoder->std_list[i].video_std;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300643
Hans Verkuil3907b072010-05-09 06:39:44 -0300644 v4l2_dbg(1, debug, sd, "Standard set to: %s\n",
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300645 decoder->std_list[i].standard.name);
646 return 0;
647}
648
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300649/**
650 * tvp514x_s_routing() - V4L2 decoder interface handler for s_routing
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300651 * @sd: pointer to standard V4L2 sub-device structure
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300652 * @input: input selector for routing the signal
653 * @output: output selector for routing the signal
654 * @config: config value. Not used
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300655 *
656 * If index is valid, selects the requested input. Otherwise, returns -EINVAL if
657 * the input is not supported or there is no active signal present in the
658 * selected input.
659 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300660static int tvp514x_s_routing(struct v4l2_subdev *sd,
661 u32 input, u32 output, u32 config)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300662{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300663 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300664 int err;
665 enum tvp514x_input input_sel;
666 enum tvp514x_output output_sel;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300667
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300668 if ((input >= INPUT_INVALID) ||
669 (output >= OUTPUT_INVALID))
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300670 /* Index out of bound */
671 return -EINVAL;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300672
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300673 input_sel = input;
674 output_sel = output;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300675
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300676 err = tvp514x_write_reg(sd, REG_INPUT_SEL, input_sel);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300677 if (err)
678 return err;
679
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300680 output_sel |= tvp514x_read_reg(sd,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300681 REG_OUTPUT_FORMATTER1) & 0x7;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300682 err = tvp514x_write_reg(sd, REG_OUTPUT_FORMATTER1,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300683 output_sel);
684 if (err)
685 return err;
686
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300687 decoder->tvp514x_regs[REG_INPUT_SEL].val = input_sel;
688 decoder->tvp514x_regs[REG_OUTPUT_FORMATTER1].val = output_sel;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300689 decoder->input = input;
690 decoder->output = output;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300691
Hans Verkuil2db4e782010-05-09 06:30:15 -0300692 v4l2_dbg(1, debug, sd, "Input set to: %d\n", input_sel);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300693
694 return 0;
695}
696
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300697/**
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300698 * tvp514x_s_ctrl() - V4L2 decoder interface handler for s_ctrl
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300699 * @ctrl: pointer to v4l2_ctrl structure
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300700 *
701 * If the requested control is supported, sets the control's current
702 * value in HW. Otherwise, returns -EINVAL if the control is not supported.
703 */
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300704static int tvp514x_s_ctrl(struct v4l2_ctrl *ctrl)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300705{
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300706 struct v4l2_subdev *sd = to_sd(ctrl);
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300707 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300708 int err = -EINVAL, value;
709
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300710 value = ctrl->val;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300711
712 switch (ctrl->id) {
713 case V4L2_CID_BRIGHTNESS:
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300714 err = tvp514x_write_reg(sd, REG_BRIGHTNESS, value);
715 if (!err)
716 decoder->tvp514x_regs[REG_BRIGHTNESS].val = value;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300717 break;
718 case V4L2_CID_CONTRAST:
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300719 err = tvp514x_write_reg(sd, REG_CONTRAST, value);
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300720 if (!err)
721 decoder->tvp514x_regs[REG_CONTRAST].val = value;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300722 break;
723 case V4L2_CID_SATURATION:
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300724 err = tvp514x_write_reg(sd, REG_SATURATION, value);
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300725 if (!err)
726 decoder->tvp514x_regs[REG_SATURATION].val = value;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300727 break;
728 case V4L2_CID_HUE:
729 if (value == 180)
730 value = 0x7F;
731 else if (value == -180)
732 value = 0x80;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300733 err = tvp514x_write_reg(sd, REG_HUE, value);
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300734 if (!err)
735 decoder->tvp514x_regs[REG_HUE].val = value;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300736 break;
737 case V4L2_CID_AUTOGAIN:
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300738 err = tvp514x_write_reg(sd, REG_AFE_GAIN_CTRL, value ? 0x0f : 0x0c);
739 if (!err)
740 decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val = value;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300741 break;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300742 }
743
Hans Verkuil3907b072010-05-09 06:39:44 -0300744 v4l2_dbg(1, debug, sd, "Set Control: ID - %d - %d\n",
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300745 ctrl->id, ctrl->val);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300746 return err;
747}
748
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300749/**
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300750 * tvp514x_g_parm() - V4L2 decoder interface handler for g_parm
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300751 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300752 * @a: pointer to standard V4L2 VIDIOC_G_PARM ioctl structure
753 *
754 * Returns the decoder's video CAPTURE parameters.
755 */
756static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300757tvp514x_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300758{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300759 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300760 struct v4l2_captureparm *cparm;
761 enum tvp514x_std current_std;
762
763 if (a == NULL)
764 return -EINVAL;
765
766 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300767 /* only capture is supported */
768 return -EINVAL;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300769
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300770 /* get the current standard */
Hans Verkuil2db4e782010-05-09 06:30:15 -0300771 current_std = decoder->current_std;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300772
773 cparm = &a->parm.capture;
774 cparm->capability = V4L2_CAP_TIMEPERFRAME;
775 cparm->timeperframe =
776 decoder->std_list[current_std].standard.frameperiod;
777
778 return 0;
779}
780
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300781/**
782 * tvp514x_s_parm() - V4L2 decoder interface handler for s_parm
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300783 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300784 * @a: pointer to standard V4L2 VIDIOC_S_PARM ioctl structure
785 *
786 * Configures the decoder to use the input parameters, if possible. If
787 * not possible, returns the appropriate error code.
788 */
789static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300790tvp514x_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300791{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300792 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300793 struct v4l2_fract *timeperframe;
794 enum tvp514x_std current_std;
795
796 if (a == NULL)
797 return -EINVAL;
798
799 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300800 /* only capture is supported */
801 return -EINVAL;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300802
803 timeperframe = &a->parm.capture.timeperframe;
804
805 /* get the current standard */
Hans Verkuil2db4e782010-05-09 06:30:15 -0300806 current_std = decoder->current_std;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300807
808 *timeperframe =
809 decoder->std_list[current_std].standard.frameperiod;
810
811 return 0;
812}
813
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300814/**
815 * tvp514x_s_stream() - V4L2 decoder i/f handler for s_stream
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300816 * @sd: pointer to standard V4L2 sub-device structure
817 * @enable: streaming enable or disable
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300818 *
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300819 * Sets streaming to enable or disable, if possible.
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300820 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300821static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300822{
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300823 int err = 0;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300824 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300825
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300826 if (decoder->streaming == enable)
827 return 0;
828
829 switch (enable) {
830 case 0:
831 {
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300832 /* Power Down Sequence */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300833 err = tvp514x_write_reg(sd, REG_OPERATION_MODE, 0x01);
834 if (err) {
835 v4l2_err(sd, "Unable to turn off decoder\n");
836 return err;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300837 }
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300838 decoder->streaming = enable;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300839 break;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300840 }
841 case 1:
842 {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300843 /* Power Up Sequence */
Lars-Peter Clausenf0a12d02013-06-23 10:01:35 -0300844 err = tvp514x_write_regs(sd, decoder->int_seq);
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300845 if (err) {
846 v4l2_err(sd, "Unable to turn on decoder\n");
847 return err;
848 }
849 /* Detect if not already detected */
850 err = tvp514x_detect(sd, decoder);
851 if (err) {
852 v4l2_err(sd, "Unable to detect decoder\n");
853 return err;
854 }
855 err = tvp514x_configure(sd, decoder);
856 if (err) {
857 v4l2_err(sd, "Unable to configure decoder\n");
858 return err;
859 }
860 decoder->streaming = enable;
861 break;
862 }
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300863 default:
864 err = -ENODEV;
865 break;
866 }
867
868 return err;
869}
870
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300871static const struct v4l2_ctrl_ops tvp514x_ctrl_ops = {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300872 .s_ctrl = tvp514x_s_ctrl,
Hans Verkuilcf6832a2010-12-12 08:45:22 -0300873};
874
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -0300875/**
876 * tvp514x_enum_mbus_code() - V4L2 decoder interface handler for enum_mbus_code
877 * @sd: pointer to standard V4L2 sub-device structure
Hans Verkuilf7234132015-03-04 01:47:54 -0800878 * @cfg: pad configuration
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -0300879 * @code: pointer to v4l2_subdev_mbus_code_enum structure
880 *
881 * Enumertaes mbus codes supported
882 */
883static int tvp514x_enum_mbus_code(struct v4l2_subdev *sd,
Hans Verkuilf7234132015-03-04 01:47:54 -0800884 struct v4l2_subdev_pad_config *cfg,
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -0300885 struct v4l2_subdev_mbus_code_enum *code)
886{
887 u32 pad = code->pad;
888 u32 index = code->index;
889
890 memset(code, 0, sizeof(*code));
891 code->index = index;
892 code->pad = pad;
893
894 if (index != 0)
895 return -EINVAL;
896
Boris BREZILLONf5fe58f2014-11-10 14:28:29 -0300897 code->code = MEDIA_BUS_FMT_YUYV8_2X8;
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -0300898
899 return 0;
900}
901
902/**
903 * tvp514x_get_pad_format() - V4L2 decoder interface handler for get pad format
904 * @sd: pointer to standard V4L2 sub-device structure
Hans Verkuilf7234132015-03-04 01:47:54 -0800905 * @cfg: pad configuration
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -0300906 * @format: pointer to v4l2_subdev_format structure
907 *
908 * Retrieves pad format which is active or tried based on requirement
909 */
910static int tvp514x_get_pad_format(struct v4l2_subdev *sd,
Hans Verkuilf7234132015-03-04 01:47:54 -0800911 struct v4l2_subdev_pad_config *cfg,
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -0300912 struct v4l2_subdev_format *format)
913{
914 struct tvp514x_decoder *decoder = to_decoder(sd);
915 __u32 which = format->which;
916
Hans Verkuilda298c62015-04-09 04:02:34 -0300917 if (format->pad)
918 return -EINVAL;
919
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -0300920 if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
921 format->format = decoder->format;
922 return 0;
923 }
924
Boris BREZILLONf5fe58f2014-11-10 14:28:29 -0300925 format->format.code = MEDIA_BUS_FMT_YUYV8_2X8;
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -0300926 format->format.width = tvp514x_std_list[decoder->current_std].width;
927 format->format.height = tvp514x_std_list[decoder->current_std].height;
928 format->format.colorspace = V4L2_COLORSPACE_SMPTE170M;
929 format->format.field = V4L2_FIELD_INTERLACED;
930
931 return 0;
932}
933
934/**
935 * tvp514x_set_pad_format() - V4L2 decoder interface handler for set pad format
936 * @sd: pointer to standard V4L2 sub-device structure
Hans Verkuilf7234132015-03-04 01:47:54 -0800937 * @cfg: pad configuration
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -0300938 * @format: pointer to v4l2_subdev_format structure
939 *
940 * Set pad format for the output pad
941 */
942static int tvp514x_set_pad_format(struct v4l2_subdev *sd,
Hans Verkuilf7234132015-03-04 01:47:54 -0800943 struct v4l2_subdev_pad_config *cfg,
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -0300944 struct v4l2_subdev_format *fmt)
945{
946 struct tvp514x_decoder *decoder = to_decoder(sd);
947
948 if (fmt->format.field != V4L2_FIELD_INTERLACED ||
Boris BREZILLONf5fe58f2014-11-10 14:28:29 -0300949 fmt->format.code != MEDIA_BUS_FMT_YUYV8_2X8 ||
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -0300950 fmt->format.colorspace != V4L2_COLORSPACE_SMPTE170M ||
951 fmt->format.width != tvp514x_std_list[decoder->current_std].width ||
952 fmt->format.height != tvp514x_std_list[decoder->current_std].height)
953 return -EINVAL;
954
955 decoder->format = fmt->format;
956
957 return 0;
958}
959
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300960static const struct v4l2_subdev_video_ops tvp514x_video_ops = {
Laurent Pinchart8774bed2014-04-28 16:53:01 -0300961 .s_std = tvp514x_s_std,
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300962 .s_routing = tvp514x_s_routing,
963 .querystd = tvp514x_querystd,
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300964 .g_parm = tvp514x_g_parm,
965 .s_parm = tvp514x_s_parm,
966 .s_stream = tvp514x_s_stream,
967};
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300968
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -0300969static const struct v4l2_subdev_pad_ops tvp514x_pad_ops = {
970 .enum_mbus_code = tvp514x_enum_mbus_code,
971 .get_fmt = tvp514x_get_pad_format,
972 .set_fmt = tvp514x_set_pad_format,
973};
974
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300975static const struct v4l2_subdev_ops tvp514x_ops = {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300976 .video = &tvp514x_video_ops,
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -0300977 .pad = &tvp514x_pad_ops,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300978};
979
Julia Lawalldb83d082016-09-11 10:05:55 -0300980static const struct tvp514x_decoder tvp514x_dev = {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300981 .streaming = 0,
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -0300982 .fmt_list = tvp514x_fmt_list,
983 .num_fmts = ARRAY_SIZE(tvp514x_fmt_list),
984 .pix = {
985 /* Default to NTSC 8-bit YUV 422 */
986 .width = NTSC_NUM_ACTIVE_PIXELS,
987 .height = NTSC_NUM_ACTIVE_LINES,
988 .pixelformat = V4L2_PIX_FMT_UYVY,
989 .field = V4L2_FIELD_INTERLACED,
990 .bytesperline = NTSC_NUM_ACTIVE_PIXELS * 2,
991 .sizeimage = NTSC_NUM_ACTIVE_PIXELS * 2 *
992 NTSC_NUM_ACTIVE_LINES,
993 .colorspace = V4L2_COLORSPACE_SMPTE170M,
994 },
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300995 .current_std = STD_NTSC_MJ,
996 .std_list = tvp514x_std_list,
997 .num_stds = ARRAY_SIZE(tvp514x_std_list),
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300998
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300999};
1000
Lad, Prabhakarb610b592013-06-04 12:26:23 -03001001static struct tvp514x_platform_data *
1002tvp514x_get_pdata(struct i2c_client *client)
1003{
Javier Martinez Canillasfe1e6ac2016-01-11 14:47:13 -02001004 struct tvp514x_platform_data *pdata = NULL;
Lad, Prabhakarb610b592013-06-04 12:26:23 -03001005 struct v4l2_of_endpoint bus_cfg;
1006 struct device_node *endpoint;
1007 unsigned int flags;
1008
1009 if (!IS_ENABLED(CONFIG_OF) || !client->dev.of_node)
1010 return client->dev.platform_data;
1011
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01001012 endpoint = of_graph_get_next_endpoint(client->dev.of_node, NULL);
Lad, Prabhakarb610b592013-06-04 12:26:23 -03001013 if (!endpoint)
1014 return NULL;
1015
Javier Martinez Canillasfe1e6ac2016-01-11 14:47:13 -02001016 if (v4l2_of_parse_endpoint(endpoint, &bus_cfg))
1017 goto done;
1018
Lad, Prabhakarb610b592013-06-04 12:26:23 -03001019 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
1020 if (!pdata)
1021 goto done;
1022
Lad, Prabhakarb610b592013-06-04 12:26:23 -03001023 flags = bus_cfg.bus.parallel.flags;
1024
1025 if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
1026 pdata->hs_polarity = 1;
1027
1028 if (flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
1029 pdata->vs_polarity = 1;
1030
1031 if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
1032 pdata->clk_polarity = 1;
1033
1034done:
1035 of_node_put(endpoint);
1036 return pdata;
1037}
1038
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001039/**
1040 * tvp514x_probe() - decoder driver i2c probe handler
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001041 * @client: i2c driver client device structure
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001042 * @id: i2c driver id table
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001043 *
1044 * Register decoder as an i2c client device and V4L2
1045 * device.
1046 */
1047static int
1048tvp514x_probe(struct i2c_client *client, const struct i2c_device_id *id)
1049{
Lad, Prabhakarb610b592013-06-04 12:26:23 -03001050 struct tvp514x_platform_data *pdata = tvp514x_get_pdata(client);
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001051 struct tvp514x_decoder *decoder;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001052 struct v4l2_subdev *sd;
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -03001053 int ret;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001054
Lad, Prabhakarb610b592013-06-04 12:26:23 -03001055 if (pdata == NULL) {
1056 dev_err(&client->dev, "No platform data\n");
1057 return -EINVAL;
1058 }
1059
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001060 /* Check if the adapter supports the needed features */
1061 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1062 return -EIO;
1063
Lad, Prabhakar08754d32013-01-03 10:04:57 -03001064 decoder = devm_kzalloc(&client->dev, sizeof(*decoder), GFP_KERNEL);
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001065 if (!decoder)
1066 return -ENOMEM;
1067
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001068 /* Initialize the tvp514x_decoder with default configuration */
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001069 *decoder = tvp514x_dev;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001070 /* Copy default register configuration */
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001071 memcpy(decoder->tvp514x_regs, tvp514x_reg_list_default,
1072 sizeof(tvp514x_reg_list_default));
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001073
Lars-Peter Clausenf0a12d02013-06-23 10:01:35 -03001074 decoder->int_seq = (struct tvp514x_reg *)id->driver_data;
1075
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001076 /* Copy board specific information here */
Lad, Prabhakarb610b592013-06-04 12:26:23 -03001077 decoder->pdata = pdata;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001078
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001079 /**
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001080 * Fetch platform specific data, and configure the
1081 * tvp514x_reg_list[] accordingly. Since this is one
1082 * time configuration, no need to preserve.
1083 */
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001084 decoder->tvp514x_regs[REG_OUTPUT_FORMATTER2].val |=
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001085 (decoder->pdata->clk_polarity << 1);
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001086 decoder->tvp514x_regs[REG_SYNC_CONTROL].val |=
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001087 ((decoder->pdata->hs_polarity << 2) |
1088 (decoder->pdata->vs_polarity << 3));
1089 /* Set default standard to auto */
1090 decoder->tvp514x_regs[REG_VIDEO_STD].val =
1091 VIDEO_STD_AUTO_SWITCH_BIT;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001092
1093 /* Register with V4L2 layer as slave device */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001094 sd = &decoder->sd;
1095 v4l2_i2c_subdev_init(sd, client, &tvp514x_ops);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001096
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -03001097#if defined(CONFIG_MEDIA_CONTROLLER)
1098 decoder->pad.flags = MEDIA_PAD_FL_SOURCE;
1099 decoder->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
Mauro Carvalho Chehab4ca72ef2015-12-10 17:25:41 -02001100 decoder->sd.entity.flags |= MEDIA_ENT_F_ATV_DECODER;
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -03001101
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -02001102 ret = media_entity_pads_init(&decoder->sd.entity, 1, &decoder->pad);
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -03001103 if (ret < 0) {
1104 v4l2_err(sd, "%s decoder driver failed to register !!\n",
1105 sd->name);
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -03001106 return ret;
1107 }
1108#endif
Hans Verkuilcf6832a2010-12-12 08:45:22 -03001109 v4l2_ctrl_handler_init(&decoder->hdl, 5);
1110 v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
1111 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
1112 v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
1113 V4L2_CID_CONTRAST, 0, 255, 1, 128);
1114 v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
1115 V4L2_CID_SATURATION, 0, 255, 1, 128);
1116 v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
1117 V4L2_CID_HUE, -180, 180, 180, 0);
1118 v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
1119 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
1120 sd->ctrl_handler = &decoder->hdl;
1121 if (decoder->hdl.error) {
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -03001122 ret = decoder->hdl.error;
Lad, Prabhakar8f23acb2013-06-24 11:51:39 -03001123 goto done;
Hans Verkuilcf6832a2010-12-12 08:45:22 -03001124 }
1125 v4l2_ctrl_handler_setup(&decoder->hdl);
1126
Lad, Prabhakar8f23acb2013-06-24 11:51:39 -03001127 ret = v4l2_async_register_subdev(&decoder->sd);
1128 if (!ret)
1129 v4l2_info(sd, "%s decoder driver registered !!\n", sd->name);
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001130
Lad, Prabhakar8f23acb2013-06-24 11:51:39 -03001131done:
1132 if (ret < 0) {
1133 v4l2_ctrl_handler_free(&decoder->hdl);
1134#if defined(CONFIG_MEDIA_CONTROLLER)
1135 media_entity_cleanup(&decoder->sd.entity);
1136#endif
1137 }
1138 return ret;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001139}
1140
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001141/**
1142 * tvp514x_remove() - decoder driver i2c remove handler
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001143 * @client: i2c driver client device structure
1144 *
1145 * Unregister decoder as an i2c client device and V4L2
1146 * device. Complement of tvp514x_probe().
1147 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001148static int tvp514x_remove(struct i2c_client *client)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001149{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001150 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1151 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001152
Lad, Prabhakar8f23acb2013-06-24 11:51:39 -03001153 v4l2_async_unregister_subdev(&decoder->sd);
Manjunath Hadli5b38b0f2013-03-08 04:13:35 -03001154#if defined(CONFIG_MEDIA_CONTROLLER)
1155 media_entity_cleanup(&decoder->sd.entity);
1156#endif
Hans Verkuilcf6832a2010-12-12 08:45:22 -03001157 v4l2_ctrl_handler_free(&decoder->hdl);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001158 return 0;
1159}
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001160/* TVP5146 Init/Power on Sequence */
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001161static const struct tvp514x_reg tvp5146_init_reg_seq[] = {
1162 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
1163 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1164 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
1165 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1166 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1167 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1168 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1169 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1170 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
1171 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1172 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001173 {TOK_TERM, 0, 0},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001174};
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001175
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001176/* TVP5147 Init/Power on Sequence */
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001177static const struct tvp514x_reg tvp5147_init_reg_seq[] = {
1178 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
1179 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1180 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
1181 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1182 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1183 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1184 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1185 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1186 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x16},
1187 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1188 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xA0},
1189 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x16},
1190 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1191 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1192 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1193 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
1194 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1195 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001196 {TOK_TERM, 0, 0},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001197};
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001198
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001199/* TVP5146M2/TVP5147M1 Init/Power on Sequence */
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001200static const struct tvp514x_reg tvp514xm_init_reg_seq[] = {
1201 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1202 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001203 {TOK_TERM, 0, 0},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001204};
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001205
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001206/**
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001207 * I2C Device Table -
1208 *
1209 * name - Name of the actual device/chip.
1210 * driver_data - Driver data
1211 */
1212static const struct i2c_device_id tvp514x_id[] = {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001213 {"tvp5146", (unsigned long)tvp5146_init_reg_seq},
1214 {"tvp5146m2", (unsigned long)tvp514xm_init_reg_seq},
1215 {"tvp5147", (unsigned long)tvp5147_init_reg_seq},
1216 {"tvp5147m1", (unsigned long)tvp514xm_init_reg_seq},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001217 {},
1218};
1219
1220MODULE_DEVICE_TABLE(i2c, tvp514x_id);
1221
Lad, Prabhakarb610b592013-06-04 12:26:23 -03001222#if IS_ENABLED(CONFIG_OF)
1223static const struct of_device_id tvp514x_of_match[] = {
1224 { .compatible = "ti,tvp5146", },
1225 { .compatible = "ti,tvp5146m2", },
1226 { .compatible = "ti,tvp5147", },
1227 { .compatible = "ti,tvp5147m1", },
1228 { /* sentinel */ },
1229};
1230MODULE_DEVICE_TABLE(of, tvp514x_of_match);
1231#endif
1232
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001233static struct i2c_driver tvp514x_driver = {
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001234 .driver = {
Lad, Prabhakarb610b592013-06-04 12:26:23 -03001235 .of_match_table = of_match_ptr(tvp514x_of_match),
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001236 .name = TVP514X_MODULE_NAME,
1237 },
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001238 .probe = tvp514x_probe,
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001239 .remove = tvp514x_remove,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001240 .id_table = tvp514x_id,
1241};
1242
Axel Linc6e8d862012-02-12 06:56:32 -03001243module_i2c_driver(tvp514x_driver);