blob: f339e6faca90ade8c71bd32abca75fe18f265535 [file] [log] [blame]
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -03001/* Texas Instruments Triple 8-/10-BIT 165-/110-MSPS Video and Graphics
2 * Digitizer with Horizontal PLL registers
3 *
4 * Copyright (C) 2009 Texas Instruments Inc
5 * Author: Santiago Nunez-Corrales <santiago.nunez@ridgerun.com>
6 *
7 * This code is partially based upon the TVP5150 driver
8 * written by Mauro Carvalho Chehab (mchehab@infradead.org),
9 * the TVP514x driver written by Vaibhav Hiremath <hvaibhav@ti.com>
10 * and the TVP7002 driver in the TI LSP 2.10.00.14. Revisions by
11 * Muralidharan Karicheri and Snehaprabha Narnakaje (TI).
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27#include <linux/delay.h>
28#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -030030#include <linux/videodev2.h>
Paul Gortmaker7a707b82011-07-03 14:03:12 -040031#include <linux/module.h>
Hans Verkuileb8305b2012-05-15 08:07:24 -030032#include <linux/v4l2-dv-timings.h>
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -030033#include <media/tvp7002.h>
34#include <media/v4l2-device.h>
35#include <media/v4l2-chip-ident.h>
36#include <media/v4l2-common.h>
Hans Verkuil0eb73de2010-12-12 08:46:15 -030037#include <media/v4l2-ctrls.h>
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -030038#include "tvp7002_reg.h"
39
40MODULE_DESCRIPTION("TI TVP7002 Video and Graphics Digitizer driver");
41MODULE_AUTHOR("Santiago Nunez-Corrales <santiago.nunez@ridgerun.com>");
42MODULE_LICENSE("GPL");
43
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -030044/* I2C retry attempts */
45#define I2C_RETRY_COUNT (5)
46
47/* End of registers */
48#define TVP7002_EOR 0x5c
49
50/* Read write definition for registers */
51#define TVP7002_READ 0
52#define TVP7002_WRITE 1
53#define TVP7002_RESERVED 2
54
55/* Interlaced vs progressive mask and shift */
56#define TVP7002_IP_SHIFT 5
57#define TVP7002_INPR_MASK (0x01 << TVP7002_IP_SHIFT)
58
59/* Shift for CPL and LPF registers */
60#define TVP7002_CL_SHIFT 8
61#define TVP7002_CL_MASK 0x0f
62
63/* Debug functions */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103064static bool debug;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -030065module_param(debug, bool, 0644);
66MODULE_PARM_DESC(debug, "Debug level (0-2)");
67
68/* Structure for register values */
69struct i2c_reg_value {
70 u8 reg;
71 u8 value;
72 u8 type;
73};
74
75/*
76 * Register default values (according to tvp7002 datasheet)
77 * In the case of read-only registers, the value (0xff) is
78 * never written. R/W functionality is controlled by the
79 * writable bit in the register struct definition.
80 */
81static const struct i2c_reg_value tvp7002_init_default[] = {
82 { TVP7002_CHIP_REV, 0xff, TVP7002_READ },
83 { TVP7002_HPLL_FDBK_DIV_MSBS, 0x67, TVP7002_WRITE },
84 { TVP7002_HPLL_FDBK_DIV_LSBS, 0x20, TVP7002_WRITE },
85 { TVP7002_HPLL_CRTL, 0xa0, TVP7002_WRITE },
86 { TVP7002_HPLL_PHASE_SEL, 0x80, TVP7002_WRITE },
87 { TVP7002_CLAMP_START, 0x32, TVP7002_WRITE },
88 { TVP7002_CLAMP_W, 0x20, TVP7002_WRITE },
89 { TVP7002_HSYNC_OUT_W, 0x60, TVP7002_WRITE },
90 { TVP7002_B_FINE_GAIN, 0x00, TVP7002_WRITE },
91 { TVP7002_G_FINE_GAIN, 0x00, TVP7002_WRITE },
92 { TVP7002_R_FINE_GAIN, 0x00, TVP7002_WRITE },
93 { TVP7002_B_FINE_OFF_MSBS, 0x80, TVP7002_WRITE },
94 { TVP7002_G_FINE_OFF_MSBS, 0x80, TVP7002_WRITE },
95 { TVP7002_R_FINE_OFF_MSBS, 0x80, TVP7002_WRITE },
96 { TVP7002_SYNC_CTL_1, 0x20, TVP7002_WRITE },
97 { TVP7002_HPLL_AND_CLAMP_CTL, 0x2e, TVP7002_WRITE },
98 { TVP7002_SYNC_ON_G_THRS, 0x5d, TVP7002_WRITE },
99 { TVP7002_SYNC_SEPARATOR_THRS, 0x47, TVP7002_WRITE },
100 { TVP7002_HPLL_PRE_COAST, 0x00, TVP7002_WRITE },
101 { TVP7002_HPLL_POST_COAST, 0x00, TVP7002_WRITE },
102 { TVP7002_SYNC_DETECT_STAT, 0xff, TVP7002_READ },
103 { TVP7002_OUT_FORMATTER, 0x47, TVP7002_WRITE },
104 { TVP7002_MISC_CTL_1, 0x01, TVP7002_WRITE },
105 { TVP7002_MISC_CTL_2, 0x00, TVP7002_WRITE },
106 { TVP7002_MISC_CTL_3, 0x01, TVP7002_WRITE },
107 { TVP7002_IN_MUX_SEL_1, 0x00, TVP7002_WRITE },
108 { TVP7002_IN_MUX_SEL_2, 0x67, TVP7002_WRITE },
109 { TVP7002_B_AND_G_COARSE_GAIN, 0x77, TVP7002_WRITE },
110 { TVP7002_R_COARSE_GAIN, 0x07, TVP7002_WRITE },
111 { TVP7002_FINE_OFF_LSBS, 0x00, TVP7002_WRITE },
112 { TVP7002_B_COARSE_OFF, 0x10, TVP7002_WRITE },
113 { TVP7002_G_COARSE_OFF, 0x10, TVP7002_WRITE },
114 { TVP7002_R_COARSE_OFF, 0x10, TVP7002_WRITE },
115 { TVP7002_HSOUT_OUT_START, 0x08, TVP7002_WRITE },
116 { TVP7002_MISC_CTL_4, 0x00, TVP7002_WRITE },
117 { TVP7002_B_DGTL_ALC_OUT_LSBS, 0xff, TVP7002_READ },
118 { TVP7002_G_DGTL_ALC_OUT_LSBS, 0xff, TVP7002_READ },
119 { TVP7002_R_DGTL_ALC_OUT_LSBS, 0xff, TVP7002_READ },
120 { TVP7002_AUTO_LVL_CTL_ENABLE, 0x80, TVP7002_WRITE },
121 { TVP7002_DGTL_ALC_OUT_MSBS, 0xff, TVP7002_READ },
122 { TVP7002_AUTO_LVL_CTL_FILTER, 0x53, TVP7002_WRITE },
123 { 0x29, 0x08, TVP7002_RESERVED },
124 { TVP7002_FINE_CLAMP_CTL, 0x07, TVP7002_WRITE },
125 /* PWR_CTL is controlled only by the probe and reset functions */
126 { TVP7002_PWR_CTL, 0x00, TVP7002_RESERVED },
127 { TVP7002_ADC_SETUP, 0x50, TVP7002_WRITE },
128 { TVP7002_COARSE_CLAMP_CTL, 0x00, TVP7002_WRITE },
129 { TVP7002_SOG_CLAMP, 0x80, TVP7002_WRITE },
Mats Randgaard425b91c2010-08-03 04:18:04 -0300130 { TVP7002_RGB_COARSE_CLAMP_CTL, 0x8c, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300131 { TVP7002_SOG_COARSE_CLAMP_CTL, 0x04, TVP7002_WRITE },
132 { TVP7002_ALC_PLACEMENT, 0x5a, TVP7002_WRITE },
133 { 0x32, 0x18, TVP7002_RESERVED },
134 { 0x33, 0x60, TVP7002_RESERVED },
135 { TVP7002_MVIS_STRIPPER_W, 0xff, TVP7002_RESERVED },
136 { TVP7002_VSYNC_ALGN, 0x10, TVP7002_WRITE },
137 { TVP7002_SYNC_BYPASS, 0x00, TVP7002_WRITE },
138 { TVP7002_L_FRAME_STAT_LSBS, 0xff, TVP7002_READ },
139 { TVP7002_L_FRAME_STAT_MSBS, 0xff, TVP7002_READ },
140 { TVP7002_CLK_L_STAT_LSBS, 0xff, TVP7002_READ },
141 { TVP7002_CLK_L_STAT_MSBS, 0xff, TVP7002_READ },
142 { TVP7002_HSYNC_W, 0xff, TVP7002_READ },
143 { TVP7002_VSYNC_W, 0xff, TVP7002_READ },
144 { TVP7002_L_LENGTH_TOL, 0x03, TVP7002_WRITE },
145 { 0x3e, 0x60, TVP7002_RESERVED },
146 { TVP7002_VIDEO_BWTH_CTL, 0x01, TVP7002_WRITE },
147 { TVP7002_AVID_START_PIXEL_LSBS, 0x01, TVP7002_WRITE },
148 { TVP7002_AVID_START_PIXEL_MSBS, 0x2c, TVP7002_WRITE },
149 { TVP7002_AVID_STOP_PIXEL_LSBS, 0x06, TVP7002_WRITE },
150 { TVP7002_AVID_STOP_PIXEL_MSBS, 0x2c, TVP7002_WRITE },
151 { TVP7002_VBLK_F_0_START_L_OFF, 0x05, TVP7002_WRITE },
152 { TVP7002_VBLK_F_1_START_L_OFF, 0x00, TVP7002_WRITE },
153 { TVP7002_VBLK_F_0_DURATION, 0x1e, TVP7002_WRITE },
154 { TVP7002_VBLK_F_1_DURATION, 0x00, TVP7002_WRITE },
155 { TVP7002_FBIT_F_0_START_L_OFF, 0x00, TVP7002_WRITE },
156 { TVP7002_FBIT_F_1_START_L_OFF, 0x00, TVP7002_WRITE },
157 { TVP7002_YUV_Y_G_COEF_LSBS, 0xe3, TVP7002_WRITE },
158 { TVP7002_YUV_Y_G_COEF_MSBS, 0x16, TVP7002_WRITE },
159 { TVP7002_YUV_Y_B_COEF_LSBS, 0x4f, TVP7002_WRITE },
160 { TVP7002_YUV_Y_B_COEF_MSBS, 0x02, TVP7002_WRITE },
161 { TVP7002_YUV_Y_R_COEF_LSBS, 0xce, TVP7002_WRITE },
162 { TVP7002_YUV_Y_R_COEF_MSBS, 0x06, TVP7002_WRITE },
163 { TVP7002_YUV_U_G_COEF_LSBS, 0xab, TVP7002_WRITE },
164 { TVP7002_YUV_U_G_COEF_MSBS, 0xf3, TVP7002_WRITE },
165 { TVP7002_YUV_U_B_COEF_LSBS, 0x00, TVP7002_WRITE },
166 { TVP7002_YUV_U_B_COEF_MSBS, 0x10, TVP7002_WRITE },
167 { TVP7002_YUV_U_R_COEF_LSBS, 0x55, TVP7002_WRITE },
168 { TVP7002_YUV_U_R_COEF_MSBS, 0xfc, TVP7002_WRITE },
169 { TVP7002_YUV_V_G_COEF_LSBS, 0x78, TVP7002_WRITE },
170 { TVP7002_YUV_V_G_COEF_MSBS, 0xf1, TVP7002_WRITE },
171 { TVP7002_YUV_V_B_COEF_LSBS, 0x88, TVP7002_WRITE },
172 { TVP7002_YUV_V_B_COEF_MSBS, 0xfe, TVP7002_WRITE },
173 { TVP7002_YUV_V_R_COEF_LSBS, 0x00, TVP7002_WRITE },
174 { TVP7002_YUV_V_R_COEF_MSBS, 0x10, TVP7002_WRITE },
175 /* This signals end of register values */
176 { TVP7002_EOR, 0xff, TVP7002_RESERVED }
177};
178
179/* Register parameters for 480P */
180static const struct i2c_reg_value tvp7002_parms_480P[] = {
181 { TVP7002_HPLL_FDBK_DIV_MSBS, 0x35, TVP7002_WRITE },
Rajashekhara, Sudhakar560afa72010-07-28 05:47:48 -0300182 { TVP7002_HPLL_FDBK_DIV_LSBS, 0xa0, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300183 { TVP7002_HPLL_CRTL, 0x02, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300184 { TVP7002_AVID_START_PIXEL_LSBS, 0x91, TVP7002_WRITE },
185 { TVP7002_AVID_START_PIXEL_MSBS, 0x00, TVP7002_WRITE },
186 { TVP7002_AVID_STOP_PIXEL_LSBS, 0x0B, TVP7002_WRITE },
187 { TVP7002_AVID_STOP_PIXEL_MSBS, 0x00, TVP7002_WRITE },
188 { TVP7002_VBLK_F_0_START_L_OFF, 0x03, TVP7002_WRITE },
189 { TVP7002_VBLK_F_1_START_L_OFF, 0x01, TVP7002_WRITE },
190 { TVP7002_VBLK_F_0_DURATION, 0x13, TVP7002_WRITE },
191 { TVP7002_VBLK_F_1_DURATION, 0x13, TVP7002_WRITE },
192 { TVP7002_ALC_PLACEMENT, 0x18, TVP7002_WRITE },
193 { TVP7002_CLAMP_START, 0x06, TVP7002_WRITE },
194 { TVP7002_CLAMP_W, 0x10, TVP7002_WRITE },
195 { TVP7002_HPLL_PRE_COAST, 0x03, TVP7002_WRITE },
196 { TVP7002_HPLL_POST_COAST, 0x03, TVP7002_WRITE },
197 { TVP7002_EOR, 0xff, TVP7002_RESERVED }
198};
199
200/* Register parameters for 576P */
201static const struct i2c_reg_value tvp7002_parms_576P[] = {
202 { TVP7002_HPLL_FDBK_DIV_MSBS, 0x36, TVP7002_WRITE },
203 { TVP7002_HPLL_FDBK_DIV_LSBS, 0x00, TVP7002_WRITE },
204 { TVP7002_HPLL_CRTL, 0x18, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300205 { TVP7002_AVID_START_PIXEL_LSBS, 0x9B, TVP7002_WRITE },
206 { TVP7002_AVID_START_PIXEL_MSBS, 0x00, TVP7002_WRITE },
207 { TVP7002_AVID_STOP_PIXEL_LSBS, 0x0F, TVP7002_WRITE },
208 { TVP7002_AVID_STOP_PIXEL_MSBS, 0x00, TVP7002_WRITE },
209 { TVP7002_VBLK_F_0_START_L_OFF, 0x00, TVP7002_WRITE },
210 { TVP7002_VBLK_F_1_START_L_OFF, 0x00, TVP7002_WRITE },
211 { TVP7002_VBLK_F_0_DURATION, 0x2D, TVP7002_WRITE },
212 { TVP7002_VBLK_F_1_DURATION, 0x00, TVP7002_WRITE },
213 { TVP7002_ALC_PLACEMENT, 0x18, TVP7002_WRITE },
214 { TVP7002_CLAMP_START, 0x06, TVP7002_WRITE },
215 { TVP7002_CLAMP_W, 0x10, TVP7002_WRITE },
216 { TVP7002_HPLL_PRE_COAST, 0x03, TVP7002_WRITE },
217 { TVP7002_HPLL_POST_COAST, 0x03, TVP7002_WRITE },
218 { TVP7002_EOR, 0xff, TVP7002_RESERVED }
219};
220
221/* Register parameters for 1080I60 */
222static const struct i2c_reg_value tvp7002_parms_1080I60[] = {
223 { TVP7002_HPLL_FDBK_DIV_MSBS, 0x89, TVP7002_WRITE },
Rajashekhara, Sudhakar560afa72010-07-28 05:47:48 -0300224 { TVP7002_HPLL_FDBK_DIV_LSBS, 0x80, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300225 { TVP7002_HPLL_CRTL, 0x98, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300226 { TVP7002_AVID_START_PIXEL_LSBS, 0x06, TVP7002_WRITE },
227 { TVP7002_AVID_START_PIXEL_MSBS, 0x01, TVP7002_WRITE },
228 { TVP7002_AVID_STOP_PIXEL_LSBS, 0x8a, TVP7002_WRITE },
229 { TVP7002_AVID_STOP_PIXEL_MSBS, 0x08, TVP7002_WRITE },
230 { TVP7002_VBLK_F_0_START_L_OFF, 0x02, TVP7002_WRITE },
231 { TVP7002_VBLK_F_1_START_L_OFF, 0x02, TVP7002_WRITE },
232 { TVP7002_VBLK_F_0_DURATION, 0x16, TVP7002_WRITE },
233 { TVP7002_VBLK_F_1_DURATION, 0x17, TVP7002_WRITE },
234 { TVP7002_ALC_PLACEMENT, 0x5a, TVP7002_WRITE },
235 { TVP7002_CLAMP_START, 0x32, TVP7002_WRITE },
236 { TVP7002_CLAMP_W, 0x20, TVP7002_WRITE },
237 { TVP7002_HPLL_PRE_COAST, 0x01, TVP7002_WRITE },
238 { TVP7002_HPLL_POST_COAST, 0x00, TVP7002_WRITE },
239 { TVP7002_EOR, 0xff, TVP7002_RESERVED }
240};
241
242/* Register parameters for 1080P60 */
243static const struct i2c_reg_value tvp7002_parms_1080P60[] = {
244 { TVP7002_HPLL_FDBK_DIV_MSBS, 0x89, TVP7002_WRITE },
Rajashekhara, Sudhakar560afa72010-07-28 05:47:48 -0300245 { TVP7002_HPLL_FDBK_DIV_LSBS, 0x80, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300246 { TVP7002_HPLL_CRTL, 0xE0, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300247 { TVP7002_AVID_START_PIXEL_LSBS, 0x06, TVP7002_WRITE },
248 { TVP7002_AVID_START_PIXEL_MSBS, 0x01, TVP7002_WRITE },
249 { TVP7002_AVID_STOP_PIXEL_LSBS, 0x8a, TVP7002_WRITE },
250 { TVP7002_AVID_STOP_PIXEL_MSBS, 0x08, TVP7002_WRITE },
251 { TVP7002_VBLK_F_0_START_L_OFF, 0x02, TVP7002_WRITE },
252 { TVP7002_VBLK_F_1_START_L_OFF, 0x02, TVP7002_WRITE },
253 { TVP7002_VBLK_F_0_DURATION, 0x16, TVP7002_WRITE },
254 { TVP7002_VBLK_F_1_DURATION, 0x17, TVP7002_WRITE },
255 { TVP7002_ALC_PLACEMENT, 0x5a, TVP7002_WRITE },
256 { TVP7002_CLAMP_START, 0x32, TVP7002_WRITE },
257 { TVP7002_CLAMP_W, 0x20, TVP7002_WRITE },
258 { TVP7002_HPLL_PRE_COAST, 0x01, TVP7002_WRITE },
259 { TVP7002_HPLL_POST_COAST, 0x00, TVP7002_WRITE },
260 { TVP7002_EOR, 0xff, TVP7002_RESERVED }
261};
262
263/* Register parameters for 1080I50 */
264static const struct i2c_reg_value tvp7002_parms_1080I50[] = {
265 { TVP7002_HPLL_FDBK_DIV_MSBS, 0xa5, TVP7002_WRITE },
266 { TVP7002_HPLL_FDBK_DIV_LSBS, 0x00, TVP7002_WRITE },
267 { TVP7002_HPLL_CRTL, 0x98, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300268 { TVP7002_AVID_START_PIXEL_LSBS, 0x06, TVP7002_WRITE },
269 { TVP7002_AVID_START_PIXEL_MSBS, 0x01, TVP7002_WRITE },
270 { TVP7002_AVID_STOP_PIXEL_LSBS, 0x8a, TVP7002_WRITE },
271 { TVP7002_AVID_STOP_PIXEL_MSBS, 0x08, TVP7002_WRITE },
272 { TVP7002_VBLK_F_0_START_L_OFF, 0x02, TVP7002_WRITE },
273 { TVP7002_VBLK_F_1_START_L_OFF, 0x02, TVP7002_WRITE },
274 { TVP7002_VBLK_F_0_DURATION, 0x16, TVP7002_WRITE },
275 { TVP7002_VBLK_F_1_DURATION, 0x17, TVP7002_WRITE },
276 { TVP7002_ALC_PLACEMENT, 0x5a, TVP7002_WRITE },
277 { TVP7002_CLAMP_START, 0x32, TVP7002_WRITE },
278 { TVP7002_CLAMP_W, 0x20, TVP7002_WRITE },
279 { TVP7002_HPLL_PRE_COAST, 0x01, TVP7002_WRITE },
280 { TVP7002_HPLL_POST_COAST, 0x00, TVP7002_WRITE },
281 { TVP7002_EOR, 0xff, TVP7002_RESERVED }
282};
283
284/* Register parameters for 720P60 */
285static const struct i2c_reg_value tvp7002_parms_720P60[] = {
286 { TVP7002_HPLL_FDBK_DIV_MSBS, 0x67, TVP7002_WRITE },
Rajashekhara, Sudhakar560afa72010-07-28 05:47:48 -0300287 { TVP7002_HPLL_FDBK_DIV_LSBS, 0x20, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300288 { TVP7002_HPLL_CRTL, 0xa0, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300289 { TVP7002_AVID_START_PIXEL_LSBS, 0x47, TVP7002_WRITE },
290 { TVP7002_AVID_START_PIXEL_MSBS, 0x01, TVP7002_WRITE },
291 { TVP7002_AVID_STOP_PIXEL_LSBS, 0x4B, TVP7002_WRITE },
292 { TVP7002_AVID_STOP_PIXEL_MSBS, 0x06, TVP7002_WRITE },
293 { TVP7002_VBLK_F_0_START_L_OFF, 0x05, TVP7002_WRITE },
294 { TVP7002_VBLK_F_1_START_L_OFF, 0x00, TVP7002_WRITE },
295 { TVP7002_VBLK_F_0_DURATION, 0x2D, TVP7002_WRITE },
296 { TVP7002_VBLK_F_1_DURATION, 0x00, TVP7002_WRITE },
297 { TVP7002_ALC_PLACEMENT, 0x5a, TVP7002_WRITE },
298 { TVP7002_CLAMP_START, 0x32, TVP7002_WRITE },
299 { TVP7002_CLAMP_W, 0x20, TVP7002_WRITE },
300 { TVP7002_HPLL_PRE_COAST, 0x00, TVP7002_WRITE },
301 { TVP7002_HPLL_POST_COAST, 0x00, TVP7002_WRITE },
302 { TVP7002_EOR, 0xff, TVP7002_RESERVED }
303};
304
305/* Register parameters for 720P50 */
306static const struct i2c_reg_value tvp7002_parms_720P50[] = {
307 { TVP7002_HPLL_FDBK_DIV_MSBS, 0x7b, TVP7002_WRITE },
Rajashekhara, Sudhakar560afa72010-07-28 05:47:48 -0300308 { TVP7002_HPLL_FDBK_DIV_LSBS, 0xc0, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300309 { TVP7002_HPLL_CRTL, 0x98, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300310 { TVP7002_AVID_START_PIXEL_LSBS, 0x47, TVP7002_WRITE },
311 { TVP7002_AVID_START_PIXEL_MSBS, 0x01, TVP7002_WRITE },
312 { TVP7002_AVID_STOP_PIXEL_LSBS, 0x4B, TVP7002_WRITE },
313 { TVP7002_AVID_STOP_PIXEL_MSBS, 0x06, TVP7002_WRITE },
314 { TVP7002_VBLK_F_0_START_L_OFF, 0x05, TVP7002_WRITE },
315 { TVP7002_VBLK_F_1_START_L_OFF, 0x00, TVP7002_WRITE },
316 { TVP7002_VBLK_F_0_DURATION, 0x2D, TVP7002_WRITE },
317 { TVP7002_VBLK_F_1_DURATION, 0x00, TVP7002_WRITE },
318 { TVP7002_ALC_PLACEMENT, 0x5a, TVP7002_WRITE },
319 { TVP7002_CLAMP_START, 0x32, TVP7002_WRITE },
320 { TVP7002_CLAMP_W, 0x20, TVP7002_WRITE },
321 { TVP7002_HPLL_PRE_COAST, 0x01, TVP7002_WRITE },
322 { TVP7002_HPLL_POST_COAST, 0x00, TVP7002_WRITE },
323 { TVP7002_EOR, 0xff, TVP7002_RESERVED }
324};
325
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300326/* Timings definition for handling device operation */
327struct tvp7002_timings_definition {
Hans Verkuileb8305b2012-05-15 08:07:24 -0300328 struct v4l2_dv_timings timings;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300329 const struct i2c_reg_value *p_settings;
330 enum v4l2_colorspace color_space;
331 enum v4l2_field scanmode;
332 u16 progressive;
333 u16 lines_per_frame;
334 u16 cpl_min;
335 u16 cpl_max;
336};
337
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300338/* Struct list for digital video timings */
339static const struct tvp7002_timings_definition tvp7002_timings[] = {
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300340 {
Hans Verkuileb8305b2012-05-15 08:07:24 -0300341 V4L2_DV_BT_CEA_1280X720P60,
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300342 tvp7002_parms_720P60,
343 V4L2_COLORSPACE_REC709,
344 V4L2_FIELD_NONE,
345 1,
346 0x2EE,
347 135,
348 153
349 },
350 {
Hans Verkuileb8305b2012-05-15 08:07:24 -0300351 V4L2_DV_BT_CEA_1920X1080I60,
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300352 tvp7002_parms_1080I60,
353 V4L2_COLORSPACE_REC709,
354 V4L2_FIELD_INTERLACED,
355 0,
356 0x465,
357 181,
358 205
359 },
360 {
Hans Verkuileb8305b2012-05-15 08:07:24 -0300361 V4L2_DV_BT_CEA_1920X1080I50,
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300362 tvp7002_parms_1080I50,
363 V4L2_COLORSPACE_REC709,
364 V4L2_FIELD_INTERLACED,
365 0,
366 0x465,
367 217,
368 245
369 },
370 {
Hans Verkuileb8305b2012-05-15 08:07:24 -0300371 V4L2_DV_BT_CEA_1280X720P50,
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300372 tvp7002_parms_720P50,
373 V4L2_COLORSPACE_REC709,
374 V4L2_FIELD_NONE,
375 1,
376 0x2EE,
377 163,
378 183
379 },
380 {
Hans Verkuileb8305b2012-05-15 08:07:24 -0300381 V4L2_DV_BT_CEA_1920X1080P60,
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300382 tvp7002_parms_1080P60,
383 V4L2_COLORSPACE_REC709,
384 V4L2_FIELD_NONE,
385 1,
386 0x465,
387 90,
388 102
389 },
390 {
Hans Verkuileb8305b2012-05-15 08:07:24 -0300391 V4L2_DV_BT_CEA_720X480P59_94,
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300392 tvp7002_parms_480P,
393 V4L2_COLORSPACE_SMPTE170M,
394 V4L2_FIELD_NONE,
395 1,
396 0x20D,
397 0xffff,
398 0xffff
399 },
400 {
Hans Verkuileb8305b2012-05-15 08:07:24 -0300401 V4L2_DV_BT_CEA_720X576P50,
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300402 tvp7002_parms_576P,
403 V4L2_COLORSPACE_SMPTE170M,
404 V4L2_FIELD_NONE,
405 1,
406 0x271,
407 0xffff,
408 0xffff
409 }
410};
411
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300412#define NUM_TIMINGS ARRAY_SIZE(tvp7002_timings)
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300413
414/* Device definition */
415struct tvp7002 {
416 struct v4l2_subdev sd;
Hans Verkuil0eb73de2010-12-12 08:46:15 -0300417 struct v4l2_ctrl_handler hdl;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300418 const struct tvp7002_config *pdata;
419
420 int ver;
421 int streaming;
422
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300423 const struct tvp7002_timings_definition *current_timings;
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -0300424 struct media_pad pad;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300425};
426
427/*
428 * to_tvp7002 - Obtain device handler TVP7002
429 * @sd: ptr to v4l2_subdev struct
430 *
431 * Returns device handler tvp7002.
432 */
433static inline struct tvp7002 *to_tvp7002(struct v4l2_subdev *sd)
434{
435 return container_of(sd, struct tvp7002, sd);
436}
437
Hans Verkuil0eb73de2010-12-12 08:46:15 -0300438static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
439{
440 return &container_of(ctrl->handler, struct tvp7002, hdl)->sd;
441}
442
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300443/*
444 * tvp7002_read - Read a value from a register in an TVP7002
445 * @sd: ptr to v4l2_subdev struct
Mats Randgaard6fa7dac2010-05-05 07:50:38 -0300446 * @addr: TVP7002 register address
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300447 * @dst: pointer to 8-bit destination
448 *
449 * Returns value read if successful, or non-zero (-1) otherwise.
450 */
451static int tvp7002_read(struct v4l2_subdev *sd, u8 addr, u8 *dst)
452{
453 struct i2c_client *c = v4l2_get_subdevdata(sd);
454 int retry;
455 int error;
456
457 for (retry = 0; retry < I2C_RETRY_COUNT; retry++) {
458 error = i2c_smbus_read_byte_data(c, addr);
459
460 if (error >= 0) {
461 *dst = (u8)error;
462 return 0;
463 }
464
465 msleep_interruptible(10);
466 }
467 v4l2_err(sd, "TVP7002 read error %d\n", error);
468 return error;
469}
470
471/*
472 * tvp7002_read_err() - Read a register value with error code
473 * @sd: pointer to standard V4L2 sub-device structure
474 * @reg: destination register
475 * @val: value to be read
Mats Randgaard6fa7dac2010-05-05 07:50:38 -0300476 * @err: pointer to error value
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300477 *
478 * Read a value in a register and save error value in pointer.
479 * Also update the register table if successful
480 */
481static inline void tvp7002_read_err(struct v4l2_subdev *sd, u8 reg,
482 u8 *dst, int *err)
483{
484 if (!*err)
485 *err = tvp7002_read(sd, reg, dst);
486}
487
488/*
489 * tvp7002_write() - Write a value to a register in TVP7002
490 * @sd: ptr to v4l2_subdev struct
491 * @addr: TVP7002 register address
492 * @value: value to be written to the register
493 *
494 * Write a value to a register in an TVP7002 decoder device.
495 * Returns zero if successful, or non-zero otherwise.
496 */
497static int tvp7002_write(struct v4l2_subdev *sd, u8 addr, u8 value)
498{
499 struct i2c_client *c;
500 int retry;
501 int error;
502
503 c = v4l2_get_subdevdata(sd);
504
505 for (retry = 0; retry < I2C_RETRY_COUNT; retry++) {
506 error = i2c_smbus_write_byte_data(c, addr, value);
507
508 if (error >= 0)
509 return 0;
510
511 v4l2_warn(sd, "Write: retry ... %d\n", retry);
512 msleep_interruptible(10);
513 }
514 v4l2_err(sd, "TVP7002 write error %d\n", error);
515 return error;
516}
517
518/*
519 * tvp7002_write_err() - Write a register value with error code
520 * @sd: pointer to standard V4L2 sub-device structure
521 * @reg: destination register
522 * @val: value to be written
Mats Randgaard6fa7dac2010-05-05 07:50:38 -0300523 * @err: pointer to error value
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300524 *
525 * Write a value in a register and save error value in pointer.
526 * Also update the register table if successful
527 */
528static inline void tvp7002_write_err(struct v4l2_subdev *sd, u8 reg,
529 u8 val, int *err)
530{
531 if (!*err)
532 *err = tvp7002_write(sd, reg, val);
533}
534
535/*
536 * tvp7002_g_chip_ident() - Get chip identification number
537 * @sd: ptr to v4l2_subdev struct
538 * @chip: ptr to v4l2_dbg_chip_ident struct
539 *
540 * Obtains the chip's identification number.
541 * Returns zero or -EINVAL if read operation fails.
542 */
543static int tvp7002_g_chip_ident(struct v4l2_subdev *sd,
544 struct v4l2_dbg_chip_ident *chip)
545{
546 u8 rev;
547 int error;
548 struct i2c_client *client = v4l2_get_subdevdata(sd);
549
550 error = tvp7002_read(sd, TVP7002_CHIP_REV, &rev);
551
552 if (error < 0)
553 return error;
554
555 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_TVP7002, rev);
556}
557
558/*
559 * tvp7002_write_inittab() - Write initialization values
560 * @sd: ptr to v4l2_subdev struct
561 * @regs: ptr to i2c_reg_value struct
562 *
563 * Write initialization values.
564 * Returns zero or -EINVAL if read operation fails.
565 */
566static int tvp7002_write_inittab(struct v4l2_subdev *sd,
567 const struct i2c_reg_value *regs)
568{
569 int error = 0;
570
571 /* Initialize the first (defined) registers */
572 while (TVP7002_EOR != regs->reg) {
573 if (TVP7002_WRITE == regs->type)
574 tvp7002_write_err(sd, regs->reg, regs->value, &error);
575 regs++;
576 }
577
578 return error;
579}
580
Hans Verkuileb8305b2012-05-15 08:07:24 -0300581static int tvp7002_s_dv_timings(struct v4l2_subdev *sd,
582 struct v4l2_dv_timings *dv_timings)
583{
584 struct tvp7002 *device = to_tvp7002(sd);
585 const struct v4l2_bt_timings *bt = &dv_timings->bt;
586 int i;
587
588 if (dv_timings->type != V4L2_DV_BT_656_1120)
589 return -EINVAL;
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300590 for (i = 0; i < NUM_TIMINGS; i++) {
591 const struct v4l2_bt_timings *t = &tvp7002_timings[i].timings.bt;
Hans Verkuileb8305b2012-05-15 08:07:24 -0300592
593 if (!memcmp(bt, t, &bt->standards - &bt->width)) {
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300594 device->current_timings = &tvp7002_timings[i];
595 return tvp7002_write_inittab(sd, tvp7002_timings[i].p_settings);
Hans Verkuileb8305b2012-05-15 08:07:24 -0300596 }
597 }
598 return -EINVAL;
599}
600
601static int tvp7002_g_dv_timings(struct v4l2_subdev *sd,
602 struct v4l2_dv_timings *dv_timings)
603{
604 struct tvp7002 *device = to_tvp7002(sd);
605
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300606 *dv_timings = device->current_timings->timings;
Hans Verkuileb8305b2012-05-15 08:07:24 -0300607 return 0;
608}
609
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300610/*
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300611 * tvp7002_s_ctrl() - Set a control
Hans Verkuil0eb73de2010-12-12 08:46:15 -0300612 * @ctrl: ptr to v4l2_ctrl struct
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300613 *
614 * Set a control in TVP7002 decoder device.
615 * Returns zero when successful or -EINVAL if register access fails.
616 */
Hans Verkuil0eb73de2010-12-12 08:46:15 -0300617static int tvp7002_s_ctrl(struct v4l2_ctrl *ctrl)
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300618{
Hans Verkuil0eb73de2010-12-12 08:46:15 -0300619 struct v4l2_subdev *sd = to_sd(ctrl);
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300620 int error = 0;
621
622 switch (ctrl->id) {
623 case V4L2_CID_GAIN:
Hans Verkuil0eb73de2010-12-12 08:46:15 -0300624 tvp7002_write_err(sd, TVP7002_R_FINE_GAIN, ctrl->val, &error);
625 tvp7002_write_err(sd, TVP7002_G_FINE_GAIN, ctrl->val, &error);
626 tvp7002_write_err(sd, TVP7002_B_FINE_GAIN, ctrl->val, &error);
627 return error;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300628 }
Hans Verkuil0eb73de2010-12-12 08:46:15 -0300629 return -EINVAL;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300630}
631
632/*
Hans Verkuildb7b5462010-05-09 07:07:35 -0300633 * tvp7002_mbus_fmt() - V4L2 decoder interface handler for try/s/g_mbus_fmt
634 * @sd: pointer to standard V4L2 sub-device structure
635 * @f: pointer to mediabus format structure
636 *
637 * Negotiate the image capture size and mediabus format.
638 * There is only one possible format, so this single function works for
639 * get, set and try.
640 */
641static int tvp7002_mbus_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *f)
642{
643 struct tvp7002 *device = to_tvp7002(sd);
Hans Verkuilf96067a2013-02-15 14:46:40 -0300644 const struct v4l2_bt_timings *bt = &device->current_timings->timings.bt;
Hans Verkuildb7b5462010-05-09 07:07:35 -0300645
Hans Verkuilf96067a2013-02-15 14:46:40 -0300646 f->width = bt->width;
647 f->height = bt->height;
Hans Verkuildb7b5462010-05-09 07:07:35 -0300648 f->code = V4L2_MBUS_FMT_YUYV10_1X20;
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300649 f->field = device->current_timings->scanmode;
650 f->colorspace = device->current_timings->color_space;
Hans Verkuildb7b5462010-05-09 07:07:35 -0300651
652 v4l2_dbg(1, debug, sd, "MBUS_FMT: Width - %d, Height - %d",
653 f->width, f->height);
654 return 0;
655}
656
657/*
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300658 * tvp7002_query_dv() - query DV timings
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300659 * @sd: pointer to standard V4L2 sub-device structure
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300660 * @index: index into the tvp7002_timings array
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300661 *
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300662 * Returns the current DV timings detected by TVP7002. If no active input is
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300663 * detected, returns -EINVAL
664 */
Hans Verkuileb8305b2012-05-15 08:07:24 -0300665static int tvp7002_query_dv(struct v4l2_subdev *sd, int *index)
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300666{
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300667 const struct tvp7002_timings_definition *timings = tvp7002_timings;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300668 u8 progressive;
669 u32 lpfr;
670 u32 cpln;
671 int error = 0;
672 u8 lpf_lsb;
673 u8 lpf_msb;
674 u8 cpl_lsb;
675 u8 cpl_msb;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300676
Hans Verkuileb8305b2012-05-15 08:07:24 -0300677 /* Return invalid index if no active input is detected */
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300678 *index = NUM_TIMINGS;
Mats Randgaardab0ab192010-08-03 04:18:03 -0300679
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300680 /* Read standards from device registers */
681 tvp7002_read_err(sd, TVP7002_L_FRAME_STAT_LSBS, &lpf_lsb, &error);
682 tvp7002_read_err(sd, TVP7002_L_FRAME_STAT_MSBS, &lpf_msb, &error);
683
684 if (error < 0)
685 return error;
686
687 tvp7002_read_err(sd, TVP7002_CLK_L_STAT_LSBS, &cpl_lsb, &error);
688 tvp7002_read_err(sd, TVP7002_CLK_L_STAT_MSBS, &cpl_msb, &error);
689
690 if (error < 0)
691 return error;
692
693 /* Get lines per frame, clocks per line and interlaced/progresive */
694 lpfr = lpf_lsb | ((TVP7002_CL_MASK & lpf_msb) << TVP7002_CL_SHIFT);
695 cpln = cpl_lsb | ((TVP7002_CL_MASK & cpl_msb) << TVP7002_CL_SHIFT);
696 progressive = (lpf_msb & TVP7002_INPR_MASK) >> TVP7002_IP_SHIFT;
697
698 /* Do checking of video modes */
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300699 for (*index = 0; *index < NUM_TIMINGS; (*index)++, timings++)
700 if (lpfr == timings->lines_per_frame &&
701 progressive == timings->progressive) {
702 if (timings->cpl_min == 0xffff)
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300703 break;
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300704 if (cpln >= timings->cpl_min && cpln <= timings->cpl_max)
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300705 break;
706 }
707
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300708 if (*index == NUM_TIMINGS) {
Hans Verkuilcf19cd32010-05-01 08:23:07 -0300709 v4l2_dbg(1, debug, sd, "detection failed: lpf = %x, cpl = %x\n",
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300710 lpfr, cpln);
Hans Verkuileb8305b2012-05-15 08:07:24 -0300711 return -ENOLINK;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300712 }
713
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300714 /* Update lines per frame and clocks per line info */
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300715 v4l2_dbg(1, debug, sd, "detected timings: %d\n", *index);
Hans Verkuileb8305b2012-05-15 08:07:24 -0300716 return 0;
717}
718
Hans Verkuileb8305b2012-05-15 08:07:24 -0300719static int tvp7002_query_dv_timings(struct v4l2_subdev *sd,
720 struct v4l2_dv_timings *timings)
721{
722 int index;
723 int err = tvp7002_query_dv(sd, &index);
724
725 if (err)
726 return err;
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300727 *timings = tvp7002_timings[index].timings;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300728 return 0;
729}
730
731#ifdef CONFIG_VIDEO_ADV_DEBUG
732/*
733 * tvp7002_g_register() - Get the value of a register
734 * @sd: ptr to v4l2_subdev struct
Mats Randgaard6fa7dac2010-05-05 07:50:38 -0300735 * @reg: ptr to v4l2_dbg_register struct
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300736 *
737 * Get the value of a TVP7002 decoder device register.
738 * Returns zero when successful, -EINVAL if register read fails or
Lad, Prabhakar7e89bd92013-05-14 01:45:14 -0300739 * access to I2C client fails.
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300740 */
741static int tvp7002_g_register(struct v4l2_subdev *sd,
742 struct v4l2_dbg_register *reg)
743{
744 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuildfbd5d42010-02-25 17:34:09 -0300745 u8 val;
746 int ret;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300747
748 if (!v4l2_chip_match_i2c_client(client, &reg->match))
749 return -EINVAL;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300750
Hans Verkuildfbd5d42010-02-25 17:34:09 -0300751 ret = tvp7002_read(sd, reg->reg & 0xff, &val);
752 reg->val = val;
753 return ret;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300754}
755
756/*
757 * tvp7002_s_register() - set a control
758 * @sd: ptr to v4l2_subdev struct
Mats Randgaard6fa7dac2010-05-05 07:50:38 -0300759 * @reg: ptr to v4l2_dbg_register struct
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300760 *
761 * Get the value of a TVP7002 decoder device register.
Lad, Prabhakar7e89bd92013-05-14 01:45:14 -0300762 * Returns zero when successful, -EINVAL if register read fails.
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300763 */
764static int tvp7002_s_register(struct v4l2_subdev *sd,
Hans Verkuil977ba3b2013-03-24 08:28:46 -0300765 const struct v4l2_dbg_register *reg)
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300766{
767 struct i2c_client *client = v4l2_get_subdevdata(sd);
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300768
769 if (!v4l2_chip_match_i2c_client(client, &reg->match))
770 return -EINVAL;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300771
Hans Verkuildfbd5d42010-02-25 17:34:09 -0300772 return tvp7002_write(sd, reg->reg & 0xff, reg->val & 0xff);
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300773}
774#endif
775
776/*
Hans Verkuildb7b5462010-05-09 07:07:35 -0300777 * tvp7002_enum_mbus_fmt() - Enum supported mediabus formats
778 * @sd: pointer to standard V4L2 sub-device structure
779 * @index: format index
780 * @code: pointer to mediabus format
781 *
782 * Enumerate supported mediabus formats.
783 */
784
785static int tvp7002_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned index,
786 enum v4l2_mbus_pixelcode *code)
787{
788 /* Check requested format index is within range */
789 if (index)
790 return -EINVAL;
791 *code = V4L2_MBUS_FMT_YUYV10_1X20;
792 return 0;
793}
794
795/*
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300796 * tvp7002_s_stream() - V4L2 decoder i/f handler for s_stream
797 * @sd: pointer to standard V4L2 sub-device structure
798 * @enable: streaming enable or disable
799 *
800 * Sets streaming to enable or disable, if possible.
801 */
802static int tvp7002_s_stream(struct v4l2_subdev *sd, int enable)
803{
804 struct tvp7002 *device = to_tvp7002(sd);
805 int error = 0;
806
807 if (device->streaming == enable)
808 return 0;
809
810 if (enable) {
811 /* Set output state on (low impedance means stream on) */
812 error = tvp7002_write(sd, TVP7002_MISC_CTL_2, 0x00);
813 device->streaming = enable;
814 } else {
815 /* Set output state off (high impedance means stream off) */
816 error = tvp7002_write(sd, TVP7002_MISC_CTL_2, 0x03);
817 if (error)
818 v4l2_dbg(1, debug, sd, "Unable to stop streaming\n");
819
820 device->streaming = enable;
821 }
822
823 return error;
824}
825
826/*
827 * tvp7002_log_status() - Print information about register settings
828 * @sd: ptr to v4l2_subdev struct
829 *
830 * Log register values of a TVP7002 decoder device.
831 * Returns zero or -EINVAL if read operation fails.
832 */
833static int tvp7002_log_status(struct v4l2_subdev *sd)
834{
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300835 struct tvp7002 *device = to_tvp7002(sd);
Hans Verkuilf96067a2013-02-15 14:46:40 -0300836 const struct v4l2_bt_timings *bt;
837 int detected;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300838
Hans Verkuilf96067a2013-02-15 14:46:40 -0300839 /* Find my current timings */
840 tvp7002_query_dv(sd, &detected);
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300841
Hans Verkuilf96067a2013-02-15 14:46:40 -0300842 bt = &device->current_timings->timings.bt;
843 v4l2_info(sd, "Selected DV Timings: %ux%u\n", bt->width, bt->height);
844 if (detected == NUM_TIMINGS) {
845 v4l2_info(sd, "Detected DV Timings: None\n");
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300846 } else {
Hans Verkuilf96067a2013-02-15 14:46:40 -0300847 bt = &tvp7002_timings[detected].timings.bt;
848 v4l2_info(sd, "Detected DV Timings: %ux%u\n",
849 bt->width, bt->height);
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300850 }
851 v4l2_info(sd, "Streaming enabled: %s\n",
852 device->streaming ? "yes" : "no");
853
854 /* Print the current value of the gain control */
Hans Verkuil0eb73de2010-12-12 08:46:15 -0300855 v4l2_ctrl_handler_log_status(&device->hdl, sd->name);
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300856
857 return 0;
858}
859
Hans Verkuileb8305b2012-05-15 08:07:24 -0300860static int tvp7002_enum_dv_timings(struct v4l2_subdev *sd,
861 struct v4l2_enum_dv_timings *timings)
862{
863 /* Check requested format index is within range */
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300864 if (timings->index >= NUM_TIMINGS)
Hans Verkuileb8305b2012-05-15 08:07:24 -0300865 return -EINVAL;
866
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300867 timings->timings = tvp7002_timings[timings->index].timings;
Hans Verkuileb8305b2012-05-15 08:07:24 -0300868 return 0;
869}
870
Hans Verkuil0eb73de2010-12-12 08:46:15 -0300871static const struct v4l2_ctrl_ops tvp7002_ctrl_ops = {
872 .s_ctrl = tvp7002_s_ctrl,
873};
874
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -0300875/*
876 * tvp7002_enum_mbus_code() - Enum supported digital video format on pad
877 * @sd: pointer to standard V4L2 sub-device structure
878 * @fh: file handle for the subdev
879 * @code: pointer to subdev enum mbus code struct
880 *
881 * Enumerate supported digital video formats for pad.
882 */
883static int
884tvp7002_enum_mbus_code(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
885 struct v4l2_subdev_mbus_code_enum *code)
886{
887 /* Check requested format index is within range */
888 if (code->index != 0)
889 return -EINVAL;
890
891 code->code = V4L2_MBUS_FMT_YUYV10_1X20;
892
893 return 0;
894}
895
896/*
897 * tvp7002_get_pad_format() - get video format on pad
898 * @sd: pointer to standard V4L2 sub-device structure
899 * @fh: file handle for the subdev
900 * @fmt: pointer to subdev format struct
901 *
902 * get video format for pad.
903 */
904static int
905tvp7002_get_pad_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
906 struct v4l2_subdev_format *fmt)
907{
908 struct tvp7002 *tvp7002 = to_tvp7002(sd);
909
910 fmt->format.code = V4L2_MBUS_FMT_YUYV10_1X20;
911 fmt->format.width = tvp7002->current_timings->timings.bt.width;
912 fmt->format.height = tvp7002->current_timings->timings.bt.height;
913 fmt->format.field = tvp7002->current_timings->scanmode;
914 fmt->format.colorspace = tvp7002->current_timings->color_space;
915
916 return 0;
917}
918
919/*
920 * tvp7002_set_pad_format() - set video format on pad
921 * @sd: pointer to standard V4L2 sub-device structure
922 * @fh: file handle for the subdev
923 * @fmt: pointer to subdev format struct
924 *
925 * set video format for pad.
926 */
927static int
928tvp7002_set_pad_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
929 struct v4l2_subdev_format *fmt)
930{
931 return tvp7002_get_pad_format(sd, fh, fmt);
932}
933
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300934/* V4L2 core operation handlers */
935static const struct v4l2_subdev_core_ops tvp7002_core_ops = {
936 .g_chip_ident = tvp7002_g_chip_ident,
937 .log_status = tvp7002_log_status,
Hans Verkuil0eb73de2010-12-12 08:46:15 -0300938 .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
939 .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
940 .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
941 .g_ctrl = v4l2_subdev_g_ctrl,
942 .s_ctrl = v4l2_subdev_s_ctrl,
943 .queryctrl = v4l2_subdev_queryctrl,
944 .querymenu = v4l2_subdev_querymenu,
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300945#ifdef CONFIG_VIDEO_ADV_DEBUG
946 .g_register = tvp7002_g_register,
947 .s_register = tvp7002_s_register,
948#endif
949};
950
951/* Specific video subsystem operation handlers */
952static const struct v4l2_subdev_video_ops tvp7002_video_ops = {
Hans Verkuileb8305b2012-05-15 08:07:24 -0300953 .g_dv_timings = tvp7002_g_dv_timings,
954 .s_dv_timings = tvp7002_s_dv_timings,
955 .enum_dv_timings = tvp7002_enum_dv_timings,
956 .query_dv_timings = tvp7002_query_dv_timings,
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300957 .s_stream = tvp7002_s_stream,
Hans Verkuildb7b5462010-05-09 07:07:35 -0300958 .g_mbus_fmt = tvp7002_mbus_fmt,
959 .try_mbus_fmt = tvp7002_mbus_fmt,
960 .s_mbus_fmt = tvp7002_mbus_fmt,
961 .enum_mbus_fmt = tvp7002_enum_mbus_fmt,
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300962};
963
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -0300964/* media pad related operation handlers */
965static const struct v4l2_subdev_pad_ops tvp7002_pad_ops = {
966 .enum_mbus_code = tvp7002_enum_mbus_code,
967 .get_fmt = tvp7002_get_pad_format,
968 .set_fmt = tvp7002_set_pad_format,
969};
970
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300971/* V4L2 top level operation handlers */
972static const struct v4l2_subdev_ops tvp7002_ops = {
973 .core = &tvp7002_core_ops,
974 .video = &tvp7002_video_ops,
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -0300975 .pad = &tvp7002_pad_ops,
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300976};
977
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300978/*
979 * tvp7002_probe - Probe a TVP7002 device
Mats Randgaard6fa7dac2010-05-05 07:50:38 -0300980 * @c: ptr to i2c_client struct
981 * @id: ptr to i2c_device_id struct
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300982 *
983 * Initialize the TVP7002 device
984 * Returns zero when successful, -EINVAL if register read fails or
985 * -EIO if i2c access is not available.
986 */
987static int tvp7002_probe(struct i2c_client *c, const struct i2c_device_id *id)
988{
989 struct v4l2_subdev *sd;
990 struct tvp7002 *device;
Hans Verkuilf96067a2013-02-15 14:46:40 -0300991 struct v4l2_dv_timings timings;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300992 int polarity_a;
993 int polarity_b;
994 u8 revision;
995
996 int error;
997
998 /* Check if the adapter supports the needed features */
999 if (!i2c_check_functionality(c->adapter,
1000 I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
1001 return -EIO;
1002
1003 if (!c->dev.platform_data) {
1004 v4l_err(c, "No platform data!!\n");
1005 return -ENODEV;
1006 }
1007
Lad, Prabhakarf3e8e4f2013-01-03 09:46:43 -03001008 device = devm_kzalloc(&c->dev, sizeof(struct tvp7002), GFP_KERNEL);
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -03001009
1010 if (!device)
1011 return -ENOMEM;
1012
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -03001013 sd = &device->sd;
1014 device->pdata = c->dev.platform_data;
Hans Verkuilf0cd0152013-02-15 14:33:51 -03001015 device->current_timings = tvp7002_timings;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -03001016
1017 /* Tell v4l2 the device is ready */
1018 v4l2_i2c_subdev_init(sd, c, &tvp7002_ops);
1019 v4l_info(c, "tvp7002 found @ 0x%02x (%s)\n",
1020 c->addr, c->adapter->name);
1021
1022 error = tvp7002_read(sd, TVP7002_CHIP_REV, &revision);
1023 if (error < 0)
Lad, Prabhakarf3e8e4f2013-01-03 09:46:43 -03001024 return error;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -03001025
1026 /* Get revision number */
1027 v4l2_info(sd, "Rev. %02x detected.\n", revision);
1028 if (revision != 0x02)
1029 v4l2_info(sd, "Unknown revision detected.\n");
1030
1031 /* Initializes TVP7002 to its default values */
1032 error = tvp7002_write_inittab(sd, tvp7002_init_default);
1033
1034 if (error < 0)
Lad, Prabhakarf3e8e4f2013-01-03 09:46:43 -03001035 return error;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -03001036
1037 /* Set polarity information after registers have been set */
1038 polarity_a = 0x20 | device->pdata->hs_polarity << 5
1039 | device->pdata->vs_polarity << 2;
1040 error = tvp7002_write(sd, TVP7002_SYNC_CTL_1, polarity_a);
1041 if (error < 0)
Lad, Prabhakarf3e8e4f2013-01-03 09:46:43 -03001042 return error;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -03001043
1044 polarity_b = 0x01 | device->pdata->fid_polarity << 2
1045 | device->pdata->sog_polarity << 1
1046 | device->pdata->clk_polarity;
1047 error = tvp7002_write(sd, TVP7002_MISC_CTL_3, polarity_b);
1048 if (error < 0)
Lad, Prabhakarf3e8e4f2013-01-03 09:46:43 -03001049 return error;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -03001050
1051 /* Set registers according to default video mode */
Hans Verkuilf96067a2013-02-15 14:46:40 -03001052 timings = device->current_timings->timings;
1053 error = tvp7002_s_dv_timings(sd, &timings);
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -03001054
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -03001055#if defined(CONFIG_MEDIA_CONTROLLER)
1056 strlcpy(sd->name, TVP7002_MODULE_NAME, sizeof(sd->name));
1057 device->pad.flags = MEDIA_PAD_FL_SOURCE;
1058 device->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1059 device->sd.entity.flags |= MEDIA_ENT_T_V4L2_SUBDEV_DECODER;
1060
1061 error = media_entity_init(&device->sd.entity, 1, &device->pad, 0);
1062 if (error < 0)
1063 return error;
1064#endif
1065
Hans Verkuil0eb73de2010-12-12 08:46:15 -03001066 v4l2_ctrl_handler_init(&device->hdl, 1);
1067 v4l2_ctrl_new_std(&device->hdl, &tvp7002_ctrl_ops,
1068 V4L2_CID_GAIN, 0, 255, 1, 0);
1069 sd->ctrl_handler = &device->hdl;
1070 if (device->hdl.error) {
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -03001071 error = device->hdl.error;
1072 goto error;
Hans Verkuil0eb73de2010-12-12 08:46:15 -03001073 }
1074 v4l2_ctrl_handler_setup(&device->hdl);
1075
Lad, Prabhakarf3e8e4f2013-01-03 09:46:43 -03001076 return 0;
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -03001077
1078error:
1079 v4l2_ctrl_handler_free(&device->hdl);
1080#if defined(CONFIG_MEDIA_CONTROLLER)
1081 media_entity_cleanup(&device->sd.entity);
1082#endif
1083 return error;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -03001084}
1085
1086/*
1087 * tvp7002_remove - Remove TVP7002 device support
1088 * @c: ptr to i2c_client struct
1089 *
1090 * Reset the TVP7002 device
1091 * Returns zero.
1092 */
1093static int tvp7002_remove(struct i2c_client *c)
1094{
1095 struct v4l2_subdev *sd = i2c_get_clientdata(c);
1096 struct tvp7002 *device = to_tvp7002(sd);
1097
1098 v4l2_dbg(1, debug, sd, "Removing tvp7002 adapter"
1099 "on address 0x%x\n", c->addr);
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -03001100#if defined(CONFIG_MEDIA_CONTROLLER)
1101 media_entity_cleanup(&device->sd.entity);
1102#endif
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -03001103 v4l2_device_unregister_subdev(sd);
Hans Verkuil0eb73de2010-12-12 08:46:15 -03001104 v4l2_ctrl_handler_free(&device->hdl);
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -03001105 return 0;
1106}
1107
1108/* I2C Device ID table */
1109static const struct i2c_device_id tvp7002_id[] = {
1110 { "tvp7002", 0 },
1111 { }
1112};
1113MODULE_DEVICE_TABLE(i2c, tvp7002_id);
1114
1115/* I2C driver data */
1116static struct i2c_driver tvp7002_driver = {
1117 .driver = {
1118 .owner = THIS_MODULE,
1119 .name = TVP7002_MODULE_NAME,
1120 },
1121 .probe = tvp7002_probe,
1122 .remove = tvp7002_remove,
1123 .id_table = tvp7002_id,
1124};
1125
Axel Linc6e8d862012-02-12 06:56:32 -03001126module_i2c_driver(tvp7002_driver);