blob: 270e6997cc76f8f34cb6c5bd3a5779a9eb6a6997 [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
44/* Module Name */
45#define TVP7002_MODULE_NAME "tvp7002"
46
47/* I2C retry attempts */
48#define I2C_RETRY_COUNT (5)
49
50/* End of registers */
51#define TVP7002_EOR 0x5c
52
53/* Read write definition for registers */
54#define TVP7002_READ 0
55#define TVP7002_WRITE 1
56#define TVP7002_RESERVED 2
57
58/* Interlaced vs progressive mask and shift */
59#define TVP7002_IP_SHIFT 5
60#define TVP7002_INPR_MASK (0x01 << TVP7002_IP_SHIFT)
61
62/* Shift for CPL and LPF registers */
63#define TVP7002_CL_SHIFT 8
64#define TVP7002_CL_MASK 0x0f
65
66/* Debug functions */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103067static bool debug;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -030068module_param(debug, bool, 0644);
69MODULE_PARM_DESC(debug, "Debug level (0-2)");
70
71/* Structure for register values */
72struct i2c_reg_value {
73 u8 reg;
74 u8 value;
75 u8 type;
76};
77
78/*
79 * Register default values (according to tvp7002 datasheet)
80 * In the case of read-only registers, the value (0xff) is
81 * never written. R/W functionality is controlled by the
82 * writable bit in the register struct definition.
83 */
84static const struct i2c_reg_value tvp7002_init_default[] = {
85 { TVP7002_CHIP_REV, 0xff, TVP7002_READ },
86 { TVP7002_HPLL_FDBK_DIV_MSBS, 0x67, TVP7002_WRITE },
87 { TVP7002_HPLL_FDBK_DIV_LSBS, 0x20, TVP7002_WRITE },
88 { TVP7002_HPLL_CRTL, 0xa0, TVP7002_WRITE },
89 { TVP7002_HPLL_PHASE_SEL, 0x80, TVP7002_WRITE },
90 { TVP7002_CLAMP_START, 0x32, TVP7002_WRITE },
91 { TVP7002_CLAMP_W, 0x20, TVP7002_WRITE },
92 { TVP7002_HSYNC_OUT_W, 0x60, TVP7002_WRITE },
93 { TVP7002_B_FINE_GAIN, 0x00, TVP7002_WRITE },
94 { TVP7002_G_FINE_GAIN, 0x00, TVP7002_WRITE },
95 { TVP7002_R_FINE_GAIN, 0x00, TVP7002_WRITE },
96 { TVP7002_B_FINE_OFF_MSBS, 0x80, TVP7002_WRITE },
97 { TVP7002_G_FINE_OFF_MSBS, 0x80, TVP7002_WRITE },
98 { TVP7002_R_FINE_OFF_MSBS, 0x80, TVP7002_WRITE },
99 { TVP7002_SYNC_CTL_1, 0x20, TVP7002_WRITE },
100 { TVP7002_HPLL_AND_CLAMP_CTL, 0x2e, TVP7002_WRITE },
101 { TVP7002_SYNC_ON_G_THRS, 0x5d, TVP7002_WRITE },
102 { TVP7002_SYNC_SEPARATOR_THRS, 0x47, TVP7002_WRITE },
103 { TVP7002_HPLL_PRE_COAST, 0x00, TVP7002_WRITE },
104 { TVP7002_HPLL_POST_COAST, 0x00, TVP7002_WRITE },
105 { TVP7002_SYNC_DETECT_STAT, 0xff, TVP7002_READ },
106 { TVP7002_OUT_FORMATTER, 0x47, TVP7002_WRITE },
107 { TVP7002_MISC_CTL_1, 0x01, TVP7002_WRITE },
108 { TVP7002_MISC_CTL_2, 0x00, TVP7002_WRITE },
109 { TVP7002_MISC_CTL_3, 0x01, TVP7002_WRITE },
110 { TVP7002_IN_MUX_SEL_1, 0x00, TVP7002_WRITE },
111 { TVP7002_IN_MUX_SEL_2, 0x67, TVP7002_WRITE },
112 { TVP7002_B_AND_G_COARSE_GAIN, 0x77, TVP7002_WRITE },
113 { TVP7002_R_COARSE_GAIN, 0x07, TVP7002_WRITE },
114 { TVP7002_FINE_OFF_LSBS, 0x00, TVP7002_WRITE },
115 { TVP7002_B_COARSE_OFF, 0x10, TVP7002_WRITE },
116 { TVP7002_G_COARSE_OFF, 0x10, TVP7002_WRITE },
117 { TVP7002_R_COARSE_OFF, 0x10, TVP7002_WRITE },
118 { TVP7002_HSOUT_OUT_START, 0x08, TVP7002_WRITE },
119 { TVP7002_MISC_CTL_4, 0x00, TVP7002_WRITE },
120 { TVP7002_B_DGTL_ALC_OUT_LSBS, 0xff, TVP7002_READ },
121 { TVP7002_G_DGTL_ALC_OUT_LSBS, 0xff, TVP7002_READ },
122 { TVP7002_R_DGTL_ALC_OUT_LSBS, 0xff, TVP7002_READ },
123 { TVP7002_AUTO_LVL_CTL_ENABLE, 0x80, TVP7002_WRITE },
124 { TVP7002_DGTL_ALC_OUT_MSBS, 0xff, TVP7002_READ },
125 { TVP7002_AUTO_LVL_CTL_FILTER, 0x53, TVP7002_WRITE },
126 { 0x29, 0x08, TVP7002_RESERVED },
127 { TVP7002_FINE_CLAMP_CTL, 0x07, TVP7002_WRITE },
128 /* PWR_CTL is controlled only by the probe and reset functions */
129 { TVP7002_PWR_CTL, 0x00, TVP7002_RESERVED },
130 { TVP7002_ADC_SETUP, 0x50, TVP7002_WRITE },
131 { TVP7002_COARSE_CLAMP_CTL, 0x00, TVP7002_WRITE },
132 { TVP7002_SOG_CLAMP, 0x80, TVP7002_WRITE },
Mats Randgaard425b91c2010-08-03 04:18:04 -0300133 { TVP7002_RGB_COARSE_CLAMP_CTL, 0x8c, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300134 { TVP7002_SOG_COARSE_CLAMP_CTL, 0x04, TVP7002_WRITE },
135 { TVP7002_ALC_PLACEMENT, 0x5a, TVP7002_WRITE },
136 { 0x32, 0x18, TVP7002_RESERVED },
137 { 0x33, 0x60, TVP7002_RESERVED },
138 { TVP7002_MVIS_STRIPPER_W, 0xff, TVP7002_RESERVED },
139 { TVP7002_VSYNC_ALGN, 0x10, TVP7002_WRITE },
140 { TVP7002_SYNC_BYPASS, 0x00, TVP7002_WRITE },
141 { TVP7002_L_FRAME_STAT_LSBS, 0xff, TVP7002_READ },
142 { TVP7002_L_FRAME_STAT_MSBS, 0xff, TVP7002_READ },
143 { TVP7002_CLK_L_STAT_LSBS, 0xff, TVP7002_READ },
144 { TVP7002_CLK_L_STAT_MSBS, 0xff, TVP7002_READ },
145 { TVP7002_HSYNC_W, 0xff, TVP7002_READ },
146 { TVP7002_VSYNC_W, 0xff, TVP7002_READ },
147 { TVP7002_L_LENGTH_TOL, 0x03, TVP7002_WRITE },
148 { 0x3e, 0x60, TVP7002_RESERVED },
149 { TVP7002_VIDEO_BWTH_CTL, 0x01, TVP7002_WRITE },
150 { TVP7002_AVID_START_PIXEL_LSBS, 0x01, TVP7002_WRITE },
151 { TVP7002_AVID_START_PIXEL_MSBS, 0x2c, TVP7002_WRITE },
152 { TVP7002_AVID_STOP_PIXEL_LSBS, 0x06, TVP7002_WRITE },
153 { TVP7002_AVID_STOP_PIXEL_MSBS, 0x2c, TVP7002_WRITE },
154 { TVP7002_VBLK_F_0_START_L_OFF, 0x05, TVP7002_WRITE },
155 { TVP7002_VBLK_F_1_START_L_OFF, 0x00, TVP7002_WRITE },
156 { TVP7002_VBLK_F_0_DURATION, 0x1e, TVP7002_WRITE },
157 { TVP7002_VBLK_F_1_DURATION, 0x00, TVP7002_WRITE },
158 { TVP7002_FBIT_F_0_START_L_OFF, 0x00, TVP7002_WRITE },
159 { TVP7002_FBIT_F_1_START_L_OFF, 0x00, TVP7002_WRITE },
160 { TVP7002_YUV_Y_G_COEF_LSBS, 0xe3, TVP7002_WRITE },
161 { TVP7002_YUV_Y_G_COEF_MSBS, 0x16, TVP7002_WRITE },
162 { TVP7002_YUV_Y_B_COEF_LSBS, 0x4f, TVP7002_WRITE },
163 { TVP7002_YUV_Y_B_COEF_MSBS, 0x02, TVP7002_WRITE },
164 { TVP7002_YUV_Y_R_COEF_LSBS, 0xce, TVP7002_WRITE },
165 { TVP7002_YUV_Y_R_COEF_MSBS, 0x06, TVP7002_WRITE },
166 { TVP7002_YUV_U_G_COEF_LSBS, 0xab, TVP7002_WRITE },
167 { TVP7002_YUV_U_G_COEF_MSBS, 0xf3, TVP7002_WRITE },
168 { TVP7002_YUV_U_B_COEF_LSBS, 0x00, TVP7002_WRITE },
169 { TVP7002_YUV_U_B_COEF_MSBS, 0x10, TVP7002_WRITE },
170 { TVP7002_YUV_U_R_COEF_LSBS, 0x55, TVP7002_WRITE },
171 { TVP7002_YUV_U_R_COEF_MSBS, 0xfc, TVP7002_WRITE },
172 { TVP7002_YUV_V_G_COEF_LSBS, 0x78, TVP7002_WRITE },
173 { TVP7002_YUV_V_G_COEF_MSBS, 0xf1, TVP7002_WRITE },
174 { TVP7002_YUV_V_B_COEF_LSBS, 0x88, TVP7002_WRITE },
175 { TVP7002_YUV_V_B_COEF_MSBS, 0xfe, TVP7002_WRITE },
176 { TVP7002_YUV_V_R_COEF_LSBS, 0x00, TVP7002_WRITE },
177 { TVP7002_YUV_V_R_COEF_MSBS, 0x10, TVP7002_WRITE },
178 /* This signals end of register values */
179 { TVP7002_EOR, 0xff, TVP7002_RESERVED }
180};
181
182/* Register parameters for 480P */
183static const struct i2c_reg_value tvp7002_parms_480P[] = {
184 { TVP7002_HPLL_FDBK_DIV_MSBS, 0x35, TVP7002_WRITE },
Rajashekhara, Sudhakar560afa72010-07-28 05:47:48 -0300185 { TVP7002_HPLL_FDBK_DIV_LSBS, 0xa0, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300186 { TVP7002_HPLL_CRTL, 0x02, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300187 { TVP7002_AVID_START_PIXEL_LSBS, 0x91, TVP7002_WRITE },
188 { TVP7002_AVID_START_PIXEL_MSBS, 0x00, TVP7002_WRITE },
189 { TVP7002_AVID_STOP_PIXEL_LSBS, 0x0B, TVP7002_WRITE },
190 { TVP7002_AVID_STOP_PIXEL_MSBS, 0x00, TVP7002_WRITE },
191 { TVP7002_VBLK_F_0_START_L_OFF, 0x03, TVP7002_WRITE },
192 { TVP7002_VBLK_F_1_START_L_OFF, 0x01, TVP7002_WRITE },
193 { TVP7002_VBLK_F_0_DURATION, 0x13, TVP7002_WRITE },
194 { TVP7002_VBLK_F_1_DURATION, 0x13, TVP7002_WRITE },
195 { TVP7002_ALC_PLACEMENT, 0x18, TVP7002_WRITE },
196 { TVP7002_CLAMP_START, 0x06, TVP7002_WRITE },
197 { TVP7002_CLAMP_W, 0x10, TVP7002_WRITE },
198 { TVP7002_HPLL_PRE_COAST, 0x03, TVP7002_WRITE },
199 { TVP7002_HPLL_POST_COAST, 0x03, TVP7002_WRITE },
200 { TVP7002_EOR, 0xff, TVP7002_RESERVED }
201};
202
203/* Register parameters for 576P */
204static const struct i2c_reg_value tvp7002_parms_576P[] = {
205 { TVP7002_HPLL_FDBK_DIV_MSBS, 0x36, TVP7002_WRITE },
206 { TVP7002_HPLL_FDBK_DIV_LSBS, 0x00, TVP7002_WRITE },
207 { TVP7002_HPLL_CRTL, 0x18, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300208 { TVP7002_AVID_START_PIXEL_LSBS, 0x9B, TVP7002_WRITE },
209 { TVP7002_AVID_START_PIXEL_MSBS, 0x00, TVP7002_WRITE },
210 { TVP7002_AVID_STOP_PIXEL_LSBS, 0x0F, TVP7002_WRITE },
211 { TVP7002_AVID_STOP_PIXEL_MSBS, 0x00, TVP7002_WRITE },
212 { TVP7002_VBLK_F_0_START_L_OFF, 0x00, TVP7002_WRITE },
213 { TVP7002_VBLK_F_1_START_L_OFF, 0x00, TVP7002_WRITE },
214 { TVP7002_VBLK_F_0_DURATION, 0x2D, TVP7002_WRITE },
215 { TVP7002_VBLK_F_1_DURATION, 0x00, TVP7002_WRITE },
216 { TVP7002_ALC_PLACEMENT, 0x18, TVP7002_WRITE },
217 { TVP7002_CLAMP_START, 0x06, TVP7002_WRITE },
218 { TVP7002_CLAMP_W, 0x10, TVP7002_WRITE },
219 { TVP7002_HPLL_PRE_COAST, 0x03, TVP7002_WRITE },
220 { TVP7002_HPLL_POST_COAST, 0x03, TVP7002_WRITE },
221 { TVP7002_EOR, 0xff, TVP7002_RESERVED }
222};
223
224/* Register parameters for 1080I60 */
225static const struct i2c_reg_value tvp7002_parms_1080I60[] = {
226 { TVP7002_HPLL_FDBK_DIV_MSBS, 0x89, TVP7002_WRITE },
Rajashekhara, Sudhakar560afa72010-07-28 05:47:48 -0300227 { TVP7002_HPLL_FDBK_DIV_LSBS, 0x80, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300228 { TVP7002_HPLL_CRTL, 0x98, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300229 { TVP7002_AVID_START_PIXEL_LSBS, 0x06, TVP7002_WRITE },
230 { TVP7002_AVID_START_PIXEL_MSBS, 0x01, TVP7002_WRITE },
231 { TVP7002_AVID_STOP_PIXEL_LSBS, 0x8a, TVP7002_WRITE },
232 { TVP7002_AVID_STOP_PIXEL_MSBS, 0x08, TVP7002_WRITE },
233 { TVP7002_VBLK_F_0_START_L_OFF, 0x02, TVP7002_WRITE },
234 { TVP7002_VBLK_F_1_START_L_OFF, 0x02, TVP7002_WRITE },
235 { TVP7002_VBLK_F_0_DURATION, 0x16, TVP7002_WRITE },
236 { TVP7002_VBLK_F_1_DURATION, 0x17, TVP7002_WRITE },
237 { TVP7002_ALC_PLACEMENT, 0x5a, TVP7002_WRITE },
238 { TVP7002_CLAMP_START, 0x32, TVP7002_WRITE },
239 { TVP7002_CLAMP_W, 0x20, TVP7002_WRITE },
240 { TVP7002_HPLL_PRE_COAST, 0x01, TVP7002_WRITE },
241 { TVP7002_HPLL_POST_COAST, 0x00, TVP7002_WRITE },
242 { TVP7002_EOR, 0xff, TVP7002_RESERVED }
243};
244
245/* Register parameters for 1080P60 */
246static const struct i2c_reg_value tvp7002_parms_1080P60[] = {
247 { TVP7002_HPLL_FDBK_DIV_MSBS, 0x89, TVP7002_WRITE },
Rajashekhara, Sudhakar560afa72010-07-28 05:47:48 -0300248 { TVP7002_HPLL_FDBK_DIV_LSBS, 0x80, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300249 { TVP7002_HPLL_CRTL, 0xE0, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300250 { TVP7002_AVID_START_PIXEL_LSBS, 0x06, TVP7002_WRITE },
251 { TVP7002_AVID_START_PIXEL_MSBS, 0x01, TVP7002_WRITE },
252 { TVP7002_AVID_STOP_PIXEL_LSBS, 0x8a, TVP7002_WRITE },
253 { TVP7002_AVID_STOP_PIXEL_MSBS, 0x08, TVP7002_WRITE },
254 { TVP7002_VBLK_F_0_START_L_OFF, 0x02, TVP7002_WRITE },
255 { TVP7002_VBLK_F_1_START_L_OFF, 0x02, TVP7002_WRITE },
256 { TVP7002_VBLK_F_0_DURATION, 0x16, TVP7002_WRITE },
257 { TVP7002_VBLK_F_1_DURATION, 0x17, TVP7002_WRITE },
258 { TVP7002_ALC_PLACEMENT, 0x5a, TVP7002_WRITE },
259 { TVP7002_CLAMP_START, 0x32, TVP7002_WRITE },
260 { TVP7002_CLAMP_W, 0x20, TVP7002_WRITE },
261 { TVP7002_HPLL_PRE_COAST, 0x01, TVP7002_WRITE },
262 { TVP7002_HPLL_POST_COAST, 0x00, TVP7002_WRITE },
263 { TVP7002_EOR, 0xff, TVP7002_RESERVED }
264};
265
266/* Register parameters for 1080I50 */
267static const struct i2c_reg_value tvp7002_parms_1080I50[] = {
268 { TVP7002_HPLL_FDBK_DIV_MSBS, 0xa5, TVP7002_WRITE },
269 { TVP7002_HPLL_FDBK_DIV_LSBS, 0x00, TVP7002_WRITE },
270 { TVP7002_HPLL_CRTL, 0x98, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300271 { TVP7002_AVID_START_PIXEL_LSBS, 0x06, TVP7002_WRITE },
272 { TVP7002_AVID_START_PIXEL_MSBS, 0x01, TVP7002_WRITE },
273 { TVP7002_AVID_STOP_PIXEL_LSBS, 0x8a, TVP7002_WRITE },
274 { TVP7002_AVID_STOP_PIXEL_MSBS, 0x08, TVP7002_WRITE },
275 { TVP7002_VBLK_F_0_START_L_OFF, 0x02, TVP7002_WRITE },
276 { TVP7002_VBLK_F_1_START_L_OFF, 0x02, TVP7002_WRITE },
277 { TVP7002_VBLK_F_0_DURATION, 0x16, TVP7002_WRITE },
278 { TVP7002_VBLK_F_1_DURATION, 0x17, TVP7002_WRITE },
279 { TVP7002_ALC_PLACEMENT, 0x5a, TVP7002_WRITE },
280 { TVP7002_CLAMP_START, 0x32, TVP7002_WRITE },
281 { TVP7002_CLAMP_W, 0x20, TVP7002_WRITE },
282 { TVP7002_HPLL_PRE_COAST, 0x01, TVP7002_WRITE },
283 { TVP7002_HPLL_POST_COAST, 0x00, TVP7002_WRITE },
284 { TVP7002_EOR, 0xff, TVP7002_RESERVED }
285};
286
287/* Register parameters for 720P60 */
288static const struct i2c_reg_value tvp7002_parms_720P60[] = {
289 { TVP7002_HPLL_FDBK_DIV_MSBS, 0x67, TVP7002_WRITE },
Rajashekhara, Sudhakar560afa72010-07-28 05:47:48 -0300290 { TVP7002_HPLL_FDBK_DIV_LSBS, 0x20, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300291 { TVP7002_HPLL_CRTL, 0xa0, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300292 { TVP7002_AVID_START_PIXEL_LSBS, 0x47, TVP7002_WRITE },
293 { TVP7002_AVID_START_PIXEL_MSBS, 0x01, TVP7002_WRITE },
294 { TVP7002_AVID_STOP_PIXEL_LSBS, 0x4B, TVP7002_WRITE },
295 { TVP7002_AVID_STOP_PIXEL_MSBS, 0x06, TVP7002_WRITE },
296 { TVP7002_VBLK_F_0_START_L_OFF, 0x05, TVP7002_WRITE },
297 { TVP7002_VBLK_F_1_START_L_OFF, 0x00, TVP7002_WRITE },
298 { TVP7002_VBLK_F_0_DURATION, 0x2D, TVP7002_WRITE },
299 { TVP7002_VBLK_F_1_DURATION, 0x00, TVP7002_WRITE },
300 { TVP7002_ALC_PLACEMENT, 0x5a, TVP7002_WRITE },
301 { TVP7002_CLAMP_START, 0x32, TVP7002_WRITE },
302 { TVP7002_CLAMP_W, 0x20, TVP7002_WRITE },
303 { TVP7002_HPLL_PRE_COAST, 0x00, TVP7002_WRITE },
304 { TVP7002_HPLL_POST_COAST, 0x00, TVP7002_WRITE },
305 { TVP7002_EOR, 0xff, TVP7002_RESERVED }
306};
307
308/* Register parameters for 720P50 */
309static const struct i2c_reg_value tvp7002_parms_720P50[] = {
310 { TVP7002_HPLL_FDBK_DIV_MSBS, 0x7b, TVP7002_WRITE },
Rajashekhara, Sudhakar560afa72010-07-28 05:47:48 -0300311 { TVP7002_HPLL_FDBK_DIV_LSBS, 0xc0, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300312 { TVP7002_HPLL_CRTL, 0x98, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300313 { TVP7002_AVID_START_PIXEL_LSBS, 0x47, TVP7002_WRITE },
314 { TVP7002_AVID_START_PIXEL_MSBS, 0x01, TVP7002_WRITE },
315 { TVP7002_AVID_STOP_PIXEL_LSBS, 0x4B, TVP7002_WRITE },
316 { TVP7002_AVID_STOP_PIXEL_MSBS, 0x06, TVP7002_WRITE },
317 { TVP7002_VBLK_F_0_START_L_OFF, 0x05, TVP7002_WRITE },
318 { TVP7002_VBLK_F_1_START_L_OFF, 0x00, TVP7002_WRITE },
319 { TVP7002_VBLK_F_0_DURATION, 0x2D, TVP7002_WRITE },
320 { TVP7002_VBLK_F_1_DURATION, 0x00, TVP7002_WRITE },
321 { TVP7002_ALC_PLACEMENT, 0x5a, TVP7002_WRITE },
322 { TVP7002_CLAMP_START, 0x32, TVP7002_WRITE },
323 { TVP7002_CLAMP_W, 0x20, TVP7002_WRITE },
324 { TVP7002_HPLL_PRE_COAST, 0x01, TVP7002_WRITE },
325 { TVP7002_HPLL_POST_COAST, 0x00, TVP7002_WRITE },
326 { TVP7002_EOR, 0xff, TVP7002_RESERVED }
327};
328
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300329/* Timings definition for handling device operation */
330struct tvp7002_timings_definition {
Hans Verkuileb8305b2012-05-15 08:07:24 -0300331 struct v4l2_dv_timings timings;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300332 const struct i2c_reg_value *p_settings;
333 enum v4l2_colorspace color_space;
334 enum v4l2_field scanmode;
335 u16 progressive;
336 u16 lines_per_frame;
337 u16 cpl_min;
338 u16 cpl_max;
339};
340
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300341/* Struct list for digital video timings */
342static const struct tvp7002_timings_definition tvp7002_timings[] = {
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300343 {
Hans Verkuileb8305b2012-05-15 08:07:24 -0300344 V4L2_DV_BT_CEA_1280X720P60,
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300345 tvp7002_parms_720P60,
346 V4L2_COLORSPACE_REC709,
347 V4L2_FIELD_NONE,
348 1,
349 0x2EE,
350 135,
351 153
352 },
353 {
Hans Verkuileb8305b2012-05-15 08:07:24 -0300354 V4L2_DV_BT_CEA_1920X1080I60,
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300355 tvp7002_parms_1080I60,
356 V4L2_COLORSPACE_REC709,
357 V4L2_FIELD_INTERLACED,
358 0,
359 0x465,
360 181,
361 205
362 },
363 {
Hans Verkuileb8305b2012-05-15 08:07:24 -0300364 V4L2_DV_BT_CEA_1920X1080I50,
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300365 tvp7002_parms_1080I50,
366 V4L2_COLORSPACE_REC709,
367 V4L2_FIELD_INTERLACED,
368 0,
369 0x465,
370 217,
371 245
372 },
373 {
Hans Verkuileb8305b2012-05-15 08:07:24 -0300374 V4L2_DV_BT_CEA_1280X720P50,
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300375 tvp7002_parms_720P50,
376 V4L2_COLORSPACE_REC709,
377 V4L2_FIELD_NONE,
378 1,
379 0x2EE,
380 163,
381 183
382 },
383 {
Hans Verkuileb8305b2012-05-15 08:07:24 -0300384 V4L2_DV_BT_CEA_1920X1080P60,
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300385 tvp7002_parms_1080P60,
386 V4L2_COLORSPACE_REC709,
387 V4L2_FIELD_NONE,
388 1,
389 0x465,
390 90,
391 102
392 },
393 {
Hans Verkuileb8305b2012-05-15 08:07:24 -0300394 V4L2_DV_BT_CEA_720X480P59_94,
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300395 tvp7002_parms_480P,
396 V4L2_COLORSPACE_SMPTE170M,
397 V4L2_FIELD_NONE,
398 1,
399 0x20D,
400 0xffff,
401 0xffff
402 },
403 {
Hans Verkuileb8305b2012-05-15 08:07:24 -0300404 V4L2_DV_BT_CEA_720X576P50,
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300405 tvp7002_parms_576P,
406 V4L2_COLORSPACE_SMPTE170M,
407 V4L2_FIELD_NONE,
408 1,
409 0x271,
410 0xffff,
411 0xffff
412 }
413};
414
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300415#define NUM_TIMINGS ARRAY_SIZE(tvp7002_timings)
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300416
417/* Device definition */
418struct tvp7002 {
419 struct v4l2_subdev sd;
Hans Verkuil0eb73de2010-12-12 08:46:15 -0300420 struct v4l2_ctrl_handler hdl;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300421 const struct tvp7002_config *pdata;
422
423 int ver;
424 int streaming;
425
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300426 const struct tvp7002_timings_definition *current_timings;
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -0300427 struct media_pad pad;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300428};
429
430/*
431 * to_tvp7002 - Obtain device handler TVP7002
432 * @sd: ptr to v4l2_subdev struct
433 *
434 * Returns device handler tvp7002.
435 */
436static inline struct tvp7002 *to_tvp7002(struct v4l2_subdev *sd)
437{
438 return container_of(sd, struct tvp7002, sd);
439}
440
Hans Verkuil0eb73de2010-12-12 08:46:15 -0300441static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
442{
443 return &container_of(ctrl->handler, struct tvp7002, hdl)->sd;
444}
445
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300446/*
447 * tvp7002_read - Read a value from a register in an TVP7002
448 * @sd: ptr to v4l2_subdev struct
Mats Randgaard6fa7dac2010-05-05 07:50:38 -0300449 * @addr: TVP7002 register address
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300450 * @dst: pointer to 8-bit destination
451 *
452 * Returns value read if successful, or non-zero (-1) otherwise.
453 */
454static int tvp7002_read(struct v4l2_subdev *sd, u8 addr, u8 *dst)
455{
456 struct i2c_client *c = v4l2_get_subdevdata(sd);
457 int retry;
458 int error;
459
460 for (retry = 0; retry < I2C_RETRY_COUNT; retry++) {
461 error = i2c_smbus_read_byte_data(c, addr);
462
463 if (error >= 0) {
464 *dst = (u8)error;
465 return 0;
466 }
467
468 msleep_interruptible(10);
469 }
470 v4l2_err(sd, "TVP7002 read error %d\n", error);
471 return error;
472}
473
474/*
475 * tvp7002_read_err() - Read a register value with error code
476 * @sd: pointer to standard V4L2 sub-device structure
477 * @reg: destination register
478 * @val: value to be read
Mats Randgaard6fa7dac2010-05-05 07:50:38 -0300479 * @err: pointer to error value
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300480 *
481 * Read a value in a register and save error value in pointer.
482 * Also update the register table if successful
483 */
484static inline void tvp7002_read_err(struct v4l2_subdev *sd, u8 reg,
485 u8 *dst, int *err)
486{
487 if (!*err)
488 *err = tvp7002_read(sd, reg, dst);
489}
490
491/*
492 * tvp7002_write() - Write a value to a register in TVP7002
493 * @sd: ptr to v4l2_subdev struct
494 * @addr: TVP7002 register address
495 * @value: value to be written to the register
496 *
497 * Write a value to a register in an TVP7002 decoder device.
498 * Returns zero if successful, or non-zero otherwise.
499 */
500static int tvp7002_write(struct v4l2_subdev *sd, u8 addr, u8 value)
501{
502 struct i2c_client *c;
503 int retry;
504 int error;
505
506 c = v4l2_get_subdevdata(sd);
507
508 for (retry = 0; retry < I2C_RETRY_COUNT; retry++) {
509 error = i2c_smbus_write_byte_data(c, addr, value);
510
511 if (error >= 0)
512 return 0;
513
514 v4l2_warn(sd, "Write: retry ... %d\n", retry);
515 msleep_interruptible(10);
516 }
517 v4l2_err(sd, "TVP7002 write error %d\n", error);
518 return error;
519}
520
521/*
522 * tvp7002_write_err() - Write a register value with error code
523 * @sd: pointer to standard V4L2 sub-device structure
524 * @reg: destination register
525 * @val: value to be written
Mats Randgaard6fa7dac2010-05-05 07:50:38 -0300526 * @err: pointer to error value
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300527 *
528 * Write a value in a register and save error value in pointer.
529 * Also update the register table if successful
530 */
531static inline void tvp7002_write_err(struct v4l2_subdev *sd, u8 reg,
532 u8 val, int *err)
533{
534 if (!*err)
535 *err = tvp7002_write(sd, reg, val);
536}
537
538/*
539 * tvp7002_g_chip_ident() - Get chip identification number
540 * @sd: ptr to v4l2_subdev struct
541 * @chip: ptr to v4l2_dbg_chip_ident struct
542 *
543 * Obtains the chip's identification number.
544 * Returns zero or -EINVAL if read operation fails.
545 */
546static int tvp7002_g_chip_ident(struct v4l2_subdev *sd,
547 struct v4l2_dbg_chip_ident *chip)
548{
549 u8 rev;
550 int error;
551 struct i2c_client *client = v4l2_get_subdevdata(sd);
552
553 error = tvp7002_read(sd, TVP7002_CHIP_REV, &rev);
554
555 if (error < 0)
556 return error;
557
558 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_TVP7002, rev);
559}
560
561/*
562 * tvp7002_write_inittab() - Write initialization values
563 * @sd: ptr to v4l2_subdev struct
564 * @regs: ptr to i2c_reg_value struct
565 *
566 * Write initialization values.
567 * Returns zero or -EINVAL if read operation fails.
568 */
569static int tvp7002_write_inittab(struct v4l2_subdev *sd,
570 const struct i2c_reg_value *regs)
571{
572 int error = 0;
573
574 /* Initialize the first (defined) registers */
575 while (TVP7002_EOR != regs->reg) {
576 if (TVP7002_WRITE == regs->type)
577 tvp7002_write_err(sd, regs->reg, regs->value, &error);
578 regs++;
579 }
580
581 return error;
582}
583
Hans Verkuileb8305b2012-05-15 08:07:24 -0300584static int tvp7002_s_dv_timings(struct v4l2_subdev *sd,
585 struct v4l2_dv_timings *dv_timings)
586{
587 struct tvp7002 *device = to_tvp7002(sd);
588 const struct v4l2_bt_timings *bt = &dv_timings->bt;
589 int i;
590
591 if (dv_timings->type != V4L2_DV_BT_656_1120)
592 return -EINVAL;
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300593 for (i = 0; i < NUM_TIMINGS; i++) {
594 const struct v4l2_bt_timings *t = &tvp7002_timings[i].timings.bt;
Hans Verkuileb8305b2012-05-15 08:07:24 -0300595
596 if (!memcmp(bt, t, &bt->standards - &bt->width)) {
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300597 device->current_timings = &tvp7002_timings[i];
598 return tvp7002_write_inittab(sd, tvp7002_timings[i].p_settings);
Hans Verkuileb8305b2012-05-15 08:07:24 -0300599 }
600 }
601 return -EINVAL;
602}
603
604static int tvp7002_g_dv_timings(struct v4l2_subdev *sd,
605 struct v4l2_dv_timings *dv_timings)
606{
607 struct tvp7002 *device = to_tvp7002(sd);
608
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300609 *dv_timings = device->current_timings->timings;
Hans Verkuileb8305b2012-05-15 08:07:24 -0300610 return 0;
611}
612
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300613/*
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300614 * tvp7002_s_ctrl() - Set a control
Hans Verkuil0eb73de2010-12-12 08:46:15 -0300615 * @ctrl: ptr to v4l2_ctrl struct
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300616 *
617 * Set a control in TVP7002 decoder device.
618 * Returns zero when successful or -EINVAL if register access fails.
619 */
Hans Verkuil0eb73de2010-12-12 08:46:15 -0300620static int tvp7002_s_ctrl(struct v4l2_ctrl *ctrl)
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300621{
Hans Verkuil0eb73de2010-12-12 08:46:15 -0300622 struct v4l2_subdev *sd = to_sd(ctrl);
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300623 int error = 0;
624
625 switch (ctrl->id) {
626 case V4L2_CID_GAIN:
Hans Verkuil0eb73de2010-12-12 08:46:15 -0300627 tvp7002_write_err(sd, TVP7002_R_FINE_GAIN, ctrl->val, &error);
628 tvp7002_write_err(sd, TVP7002_G_FINE_GAIN, ctrl->val, &error);
629 tvp7002_write_err(sd, TVP7002_B_FINE_GAIN, ctrl->val, &error);
630 return error;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300631 }
Hans Verkuil0eb73de2010-12-12 08:46:15 -0300632 return -EINVAL;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300633}
634
635/*
Hans Verkuildb7b5462010-05-09 07:07:35 -0300636 * tvp7002_mbus_fmt() - V4L2 decoder interface handler for try/s/g_mbus_fmt
637 * @sd: pointer to standard V4L2 sub-device structure
638 * @f: pointer to mediabus format structure
639 *
640 * Negotiate the image capture size and mediabus format.
641 * There is only one possible format, so this single function works for
642 * get, set and try.
643 */
644static int tvp7002_mbus_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *f)
645{
646 struct tvp7002 *device = to_tvp7002(sd);
Hans Verkuilf96067a2013-02-15 14:46:40 -0300647 const struct v4l2_bt_timings *bt = &device->current_timings->timings.bt;
Hans Verkuildb7b5462010-05-09 07:07:35 -0300648
Hans Verkuilf96067a2013-02-15 14:46:40 -0300649 f->width = bt->width;
650 f->height = bt->height;
Hans Verkuildb7b5462010-05-09 07:07:35 -0300651 f->code = V4L2_MBUS_FMT_YUYV10_1X20;
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300652 f->field = device->current_timings->scanmode;
653 f->colorspace = device->current_timings->color_space;
Hans Verkuildb7b5462010-05-09 07:07:35 -0300654
655 v4l2_dbg(1, debug, sd, "MBUS_FMT: Width - %d, Height - %d",
656 f->width, f->height);
657 return 0;
658}
659
660/*
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300661 * tvp7002_query_dv() - query DV timings
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300662 * @sd: pointer to standard V4L2 sub-device structure
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300663 * @index: index into the tvp7002_timings array
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300664 *
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300665 * Returns the current DV timings detected by TVP7002. If no active input is
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300666 * detected, returns -EINVAL
667 */
Hans Verkuileb8305b2012-05-15 08:07:24 -0300668static int tvp7002_query_dv(struct v4l2_subdev *sd, int *index)
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300669{
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300670 const struct tvp7002_timings_definition *timings = tvp7002_timings;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300671 u8 progressive;
672 u32 lpfr;
673 u32 cpln;
674 int error = 0;
675 u8 lpf_lsb;
676 u8 lpf_msb;
677 u8 cpl_lsb;
678 u8 cpl_msb;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300679
Hans Verkuileb8305b2012-05-15 08:07:24 -0300680 /* Return invalid index if no active input is detected */
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300681 *index = NUM_TIMINGS;
Mats Randgaardab0ab192010-08-03 04:18:03 -0300682
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300683 /* Read standards from device registers */
684 tvp7002_read_err(sd, TVP7002_L_FRAME_STAT_LSBS, &lpf_lsb, &error);
685 tvp7002_read_err(sd, TVP7002_L_FRAME_STAT_MSBS, &lpf_msb, &error);
686
687 if (error < 0)
688 return error;
689
690 tvp7002_read_err(sd, TVP7002_CLK_L_STAT_LSBS, &cpl_lsb, &error);
691 tvp7002_read_err(sd, TVP7002_CLK_L_STAT_MSBS, &cpl_msb, &error);
692
693 if (error < 0)
694 return error;
695
696 /* Get lines per frame, clocks per line and interlaced/progresive */
697 lpfr = lpf_lsb | ((TVP7002_CL_MASK & lpf_msb) << TVP7002_CL_SHIFT);
698 cpln = cpl_lsb | ((TVP7002_CL_MASK & cpl_msb) << TVP7002_CL_SHIFT);
699 progressive = (lpf_msb & TVP7002_INPR_MASK) >> TVP7002_IP_SHIFT;
700
701 /* Do checking of video modes */
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300702 for (*index = 0; *index < NUM_TIMINGS; (*index)++, timings++)
703 if (lpfr == timings->lines_per_frame &&
704 progressive == timings->progressive) {
705 if (timings->cpl_min == 0xffff)
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300706 break;
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300707 if (cpln >= timings->cpl_min && cpln <= timings->cpl_max)
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300708 break;
709 }
710
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300711 if (*index == NUM_TIMINGS) {
Hans Verkuilcf19cd32010-05-01 08:23:07 -0300712 v4l2_dbg(1, debug, sd, "detection failed: lpf = %x, cpl = %x\n",
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300713 lpfr, cpln);
Hans Verkuileb8305b2012-05-15 08:07:24 -0300714 return -ENOLINK;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300715 }
716
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300717 /* Update lines per frame and clocks per line info */
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300718 v4l2_dbg(1, debug, sd, "detected timings: %d\n", *index);
Hans Verkuileb8305b2012-05-15 08:07:24 -0300719 return 0;
720}
721
Hans Verkuileb8305b2012-05-15 08:07:24 -0300722static int tvp7002_query_dv_timings(struct v4l2_subdev *sd,
723 struct v4l2_dv_timings *timings)
724{
725 int index;
726 int err = tvp7002_query_dv(sd, &index);
727
728 if (err)
729 return err;
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300730 *timings = tvp7002_timings[index].timings;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300731 return 0;
732}
733
734#ifdef CONFIG_VIDEO_ADV_DEBUG
735/*
736 * tvp7002_g_register() - Get the value of a register
737 * @sd: ptr to v4l2_subdev struct
Mats Randgaard6fa7dac2010-05-05 07:50:38 -0300738 * @reg: ptr to v4l2_dbg_register struct
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300739 *
740 * Get the value of a TVP7002 decoder device register.
741 * Returns zero when successful, -EINVAL if register read fails or
742 * access to I2C client fails, -EPERM if the call is not allowed
Justin P. Mattockeb78bd72010-12-30 15:08:03 -0800743 * by disabled CAP_SYS_ADMIN.
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300744 */
745static int tvp7002_g_register(struct v4l2_subdev *sd,
746 struct v4l2_dbg_register *reg)
747{
748 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuildfbd5d42010-02-25 17:34:09 -0300749 u8 val;
750 int ret;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300751
752 if (!v4l2_chip_match_i2c_client(client, &reg->match))
753 return -EINVAL;
754 if (!capable(CAP_SYS_ADMIN))
755 return -EPERM;
756
Hans Verkuildfbd5d42010-02-25 17:34:09 -0300757 ret = tvp7002_read(sd, reg->reg & 0xff, &val);
758 reg->val = val;
759 return ret;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300760}
761
762/*
763 * tvp7002_s_register() - set a control
764 * @sd: ptr to v4l2_subdev struct
Mats Randgaard6fa7dac2010-05-05 07:50:38 -0300765 * @reg: ptr to v4l2_dbg_register struct
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300766 *
767 * Get the value of a TVP7002 decoder device register.
768 * Returns zero when successful, -EINVAL if register read fails or
769 * -EPERM if call not allowed.
770 */
771static int tvp7002_s_register(struct v4l2_subdev *sd,
Hans Verkuil977ba3b2013-03-24 08:28:46 -0300772 const struct v4l2_dbg_register *reg)
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300773{
774 struct i2c_client *client = v4l2_get_subdevdata(sd);
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300775
776 if (!v4l2_chip_match_i2c_client(client, &reg->match))
777 return -EINVAL;
778 if (!capable(CAP_SYS_ADMIN))
779 return -EPERM;
780
Hans Verkuildfbd5d42010-02-25 17:34:09 -0300781 return tvp7002_write(sd, reg->reg & 0xff, reg->val & 0xff);
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300782}
783#endif
784
785/*
Hans Verkuildb7b5462010-05-09 07:07:35 -0300786 * tvp7002_enum_mbus_fmt() - Enum supported mediabus formats
787 * @sd: pointer to standard V4L2 sub-device structure
788 * @index: format index
789 * @code: pointer to mediabus format
790 *
791 * Enumerate supported mediabus formats.
792 */
793
794static int tvp7002_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned index,
795 enum v4l2_mbus_pixelcode *code)
796{
797 /* Check requested format index is within range */
798 if (index)
799 return -EINVAL;
800 *code = V4L2_MBUS_FMT_YUYV10_1X20;
801 return 0;
802}
803
804/*
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300805 * tvp7002_s_stream() - V4L2 decoder i/f handler for s_stream
806 * @sd: pointer to standard V4L2 sub-device structure
807 * @enable: streaming enable or disable
808 *
809 * Sets streaming to enable or disable, if possible.
810 */
811static int tvp7002_s_stream(struct v4l2_subdev *sd, int enable)
812{
813 struct tvp7002 *device = to_tvp7002(sd);
814 int error = 0;
815
816 if (device->streaming == enable)
817 return 0;
818
819 if (enable) {
820 /* Set output state on (low impedance means stream on) */
821 error = tvp7002_write(sd, TVP7002_MISC_CTL_2, 0x00);
822 device->streaming = enable;
823 } else {
824 /* Set output state off (high impedance means stream off) */
825 error = tvp7002_write(sd, TVP7002_MISC_CTL_2, 0x03);
826 if (error)
827 v4l2_dbg(1, debug, sd, "Unable to stop streaming\n");
828
829 device->streaming = enable;
830 }
831
832 return error;
833}
834
835/*
836 * tvp7002_log_status() - Print information about register settings
837 * @sd: ptr to v4l2_subdev struct
838 *
839 * Log register values of a TVP7002 decoder device.
840 * Returns zero or -EINVAL if read operation fails.
841 */
842static int tvp7002_log_status(struct v4l2_subdev *sd)
843{
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300844 struct tvp7002 *device = to_tvp7002(sd);
Hans Verkuilf96067a2013-02-15 14:46:40 -0300845 const struct v4l2_bt_timings *bt;
846 int detected;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300847
Hans Verkuilf96067a2013-02-15 14:46:40 -0300848 /* Find my current timings */
849 tvp7002_query_dv(sd, &detected);
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300850
Hans Verkuilf96067a2013-02-15 14:46:40 -0300851 bt = &device->current_timings->timings.bt;
852 v4l2_info(sd, "Selected DV Timings: %ux%u\n", bt->width, bt->height);
853 if (detected == NUM_TIMINGS) {
854 v4l2_info(sd, "Detected DV Timings: None\n");
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300855 } else {
Hans Verkuilf96067a2013-02-15 14:46:40 -0300856 bt = &tvp7002_timings[detected].timings.bt;
857 v4l2_info(sd, "Detected DV Timings: %ux%u\n",
858 bt->width, bt->height);
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300859 }
860 v4l2_info(sd, "Streaming enabled: %s\n",
861 device->streaming ? "yes" : "no");
862
863 /* Print the current value of the gain control */
Hans Verkuil0eb73de2010-12-12 08:46:15 -0300864 v4l2_ctrl_handler_log_status(&device->hdl, sd->name);
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300865
866 return 0;
867}
868
Hans Verkuileb8305b2012-05-15 08:07:24 -0300869static int tvp7002_enum_dv_timings(struct v4l2_subdev *sd,
870 struct v4l2_enum_dv_timings *timings)
871{
872 /* Check requested format index is within range */
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300873 if (timings->index >= NUM_TIMINGS)
Hans Verkuileb8305b2012-05-15 08:07:24 -0300874 return -EINVAL;
875
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300876 timings->timings = tvp7002_timings[timings->index].timings;
Hans Verkuileb8305b2012-05-15 08:07:24 -0300877 return 0;
878}
879
Hans Verkuil0eb73de2010-12-12 08:46:15 -0300880static const struct v4l2_ctrl_ops tvp7002_ctrl_ops = {
881 .s_ctrl = tvp7002_s_ctrl,
882};
883
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -0300884/*
885 * tvp7002_enum_mbus_code() - Enum supported digital video format on pad
886 * @sd: pointer to standard V4L2 sub-device structure
887 * @fh: file handle for the subdev
888 * @code: pointer to subdev enum mbus code struct
889 *
890 * Enumerate supported digital video formats for pad.
891 */
892static int
893tvp7002_enum_mbus_code(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
894 struct v4l2_subdev_mbus_code_enum *code)
895{
896 /* Check requested format index is within range */
897 if (code->index != 0)
898 return -EINVAL;
899
900 code->code = V4L2_MBUS_FMT_YUYV10_1X20;
901
902 return 0;
903}
904
905/*
906 * tvp7002_get_pad_format() - get video format on pad
907 * @sd: pointer to standard V4L2 sub-device structure
908 * @fh: file handle for the subdev
909 * @fmt: pointer to subdev format struct
910 *
911 * get video format for pad.
912 */
913static int
914tvp7002_get_pad_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
915 struct v4l2_subdev_format *fmt)
916{
917 struct tvp7002 *tvp7002 = to_tvp7002(sd);
918
919 fmt->format.code = V4L2_MBUS_FMT_YUYV10_1X20;
920 fmt->format.width = tvp7002->current_timings->timings.bt.width;
921 fmt->format.height = tvp7002->current_timings->timings.bt.height;
922 fmt->format.field = tvp7002->current_timings->scanmode;
923 fmt->format.colorspace = tvp7002->current_timings->color_space;
924
925 return 0;
926}
927
928/*
929 * tvp7002_set_pad_format() - set video format on pad
930 * @sd: pointer to standard V4L2 sub-device structure
931 * @fh: file handle for the subdev
932 * @fmt: pointer to subdev format struct
933 *
934 * set video format for pad.
935 */
936static int
937tvp7002_set_pad_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
938 struct v4l2_subdev_format *fmt)
939{
940 return tvp7002_get_pad_format(sd, fh, fmt);
941}
942
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300943/* V4L2 core operation handlers */
944static const struct v4l2_subdev_core_ops tvp7002_core_ops = {
945 .g_chip_ident = tvp7002_g_chip_ident,
946 .log_status = tvp7002_log_status,
Hans Verkuil0eb73de2010-12-12 08:46:15 -0300947 .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
948 .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
949 .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
950 .g_ctrl = v4l2_subdev_g_ctrl,
951 .s_ctrl = v4l2_subdev_s_ctrl,
952 .queryctrl = v4l2_subdev_queryctrl,
953 .querymenu = v4l2_subdev_querymenu,
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300954#ifdef CONFIG_VIDEO_ADV_DEBUG
955 .g_register = tvp7002_g_register,
956 .s_register = tvp7002_s_register,
957#endif
958};
959
960/* Specific video subsystem operation handlers */
961static const struct v4l2_subdev_video_ops tvp7002_video_ops = {
Hans Verkuileb8305b2012-05-15 08:07:24 -0300962 .g_dv_timings = tvp7002_g_dv_timings,
963 .s_dv_timings = tvp7002_s_dv_timings,
964 .enum_dv_timings = tvp7002_enum_dv_timings,
965 .query_dv_timings = tvp7002_query_dv_timings,
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300966 .s_stream = tvp7002_s_stream,
Hans Verkuildb7b5462010-05-09 07:07:35 -0300967 .g_mbus_fmt = tvp7002_mbus_fmt,
968 .try_mbus_fmt = tvp7002_mbus_fmt,
969 .s_mbus_fmt = tvp7002_mbus_fmt,
970 .enum_mbus_fmt = tvp7002_enum_mbus_fmt,
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300971};
972
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -0300973/* media pad related operation handlers */
974static const struct v4l2_subdev_pad_ops tvp7002_pad_ops = {
975 .enum_mbus_code = tvp7002_enum_mbus_code,
976 .get_fmt = tvp7002_get_pad_format,
977 .set_fmt = tvp7002_set_pad_format,
978};
979
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300980/* V4L2 top level operation handlers */
981static const struct v4l2_subdev_ops tvp7002_ops = {
982 .core = &tvp7002_core_ops,
983 .video = &tvp7002_video_ops,
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -0300984 .pad = &tvp7002_pad_ops,
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300985};
986
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300987/*
988 * tvp7002_probe - Probe a TVP7002 device
Mats Randgaard6fa7dac2010-05-05 07:50:38 -0300989 * @c: ptr to i2c_client struct
990 * @id: ptr to i2c_device_id struct
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300991 *
992 * Initialize the TVP7002 device
993 * Returns zero when successful, -EINVAL if register read fails or
994 * -EIO if i2c access is not available.
995 */
996static int tvp7002_probe(struct i2c_client *c, const struct i2c_device_id *id)
997{
998 struct v4l2_subdev *sd;
999 struct tvp7002 *device;
Hans Verkuilf96067a2013-02-15 14:46:40 -03001000 struct v4l2_dv_timings timings;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -03001001 int polarity_a;
1002 int polarity_b;
1003 u8 revision;
1004
1005 int error;
1006
1007 /* Check if the adapter supports the needed features */
1008 if (!i2c_check_functionality(c->adapter,
1009 I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
1010 return -EIO;
1011
1012 if (!c->dev.platform_data) {
1013 v4l_err(c, "No platform data!!\n");
1014 return -ENODEV;
1015 }
1016
Lad, Prabhakarf3e8e4f2013-01-03 09:46:43 -03001017 device = devm_kzalloc(&c->dev, sizeof(struct tvp7002), GFP_KERNEL);
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -03001018
1019 if (!device)
1020 return -ENOMEM;
1021
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -03001022 sd = &device->sd;
1023 device->pdata = c->dev.platform_data;
Hans Verkuilf0cd0152013-02-15 14:33:51 -03001024 device->current_timings = tvp7002_timings;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -03001025
1026 /* Tell v4l2 the device is ready */
1027 v4l2_i2c_subdev_init(sd, c, &tvp7002_ops);
1028 v4l_info(c, "tvp7002 found @ 0x%02x (%s)\n",
1029 c->addr, c->adapter->name);
1030
1031 error = tvp7002_read(sd, TVP7002_CHIP_REV, &revision);
1032 if (error < 0)
Lad, Prabhakarf3e8e4f2013-01-03 09:46:43 -03001033 return error;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -03001034
1035 /* Get revision number */
1036 v4l2_info(sd, "Rev. %02x detected.\n", revision);
1037 if (revision != 0x02)
1038 v4l2_info(sd, "Unknown revision detected.\n");
1039
1040 /* Initializes TVP7002 to its default values */
1041 error = tvp7002_write_inittab(sd, tvp7002_init_default);
1042
1043 if (error < 0)
Lad, Prabhakarf3e8e4f2013-01-03 09:46:43 -03001044 return error;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -03001045
1046 /* Set polarity information after registers have been set */
1047 polarity_a = 0x20 | device->pdata->hs_polarity << 5
1048 | device->pdata->vs_polarity << 2;
1049 error = tvp7002_write(sd, TVP7002_SYNC_CTL_1, polarity_a);
1050 if (error < 0)
Lad, Prabhakarf3e8e4f2013-01-03 09:46:43 -03001051 return error;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -03001052
1053 polarity_b = 0x01 | device->pdata->fid_polarity << 2
1054 | device->pdata->sog_polarity << 1
1055 | device->pdata->clk_polarity;
1056 error = tvp7002_write(sd, TVP7002_MISC_CTL_3, polarity_b);
1057 if (error < 0)
Lad, Prabhakarf3e8e4f2013-01-03 09:46:43 -03001058 return error;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -03001059
1060 /* Set registers according to default video mode */
Hans Verkuilf96067a2013-02-15 14:46:40 -03001061 timings = device->current_timings->timings;
1062 error = tvp7002_s_dv_timings(sd, &timings);
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -03001063
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -03001064#if defined(CONFIG_MEDIA_CONTROLLER)
1065 strlcpy(sd->name, TVP7002_MODULE_NAME, sizeof(sd->name));
1066 device->pad.flags = MEDIA_PAD_FL_SOURCE;
1067 device->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1068 device->sd.entity.flags |= MEDIA_ENT_T_V4L2_SUBDEV_DECODER;
1069
1070 error = media_entity_init(&device->sd.entity, 1, &device->pad, 0);
1071 if (error < 0)
1072 return error;
1073#endif
1074
Hans Verkuil0eb73de2010-12-12 08:46:15 -03001075 v4l2_ctrl_handler_init(&device->hdl, 1);
1076 v4l2_ctrl_new_std(&device->hdl, &tvp7002_ctrl_ops,
1077 V4L2_CID_GAIN, 0, 255, 1, 0);
1078 sd->ctrl_handler = &device->hdl;
1079 if (device->hdl.error) {
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -03001080 error = device->hdl.error;
1081 goto error;
Hans Verkuil0eb73de2010-12-12 08:46:15 -03001082 }
1083 v4l2_ctrl_handler_setup(&device->hdl);
1084
Lad, Prabhakarf3e8e4f2013-01-03 09:46:43 -03001085 return 0;
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -03001086
1087error:
1088 v4l2_ctrl_handler_free(&device->hdl);
1089#if defined(CONFIG_MEDIA_CONTROLLER)
1090 media_entity_cleanup(&device->sd.entity);
1091#endif
1092 return error;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -03001093}
1094
1095/*
1096 * tvp7002_remove - Remove TVP7002 device support
1097 * @c: ptr to i2c_client struct
1098 *
1099 * Reset the TVP7002 device
1100 * Returns zero.
1101 */
1102static int tvp7002_remove(struct i2c_client *c)
1103{
1104 struct v4l2_subdev *sd = i2c_get_clientdata(c);
1105 struct tvp7002 *device = to_tvp7002(sd);
1106
1107 v4l2_dbg(1, debug, sd, "Removing tvp7002 adapter"
1108 "on address 0x%x\n", c->addr);
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -03001109#if defined(CONFIG_MEDIA_CONTROLLER)
1110 media_entity_cleanup(&device->sd.entity);
1111#endif
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -03001112 v4l2_device_unregister_subdev(sd);
Hans Verkuil0eb73de2010-12-12 08:46:15 -03001113 v4l2_ctrl_handler_free(&device->hdl);
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -03001114 return 0;
1115}
1116
1117/* I2C Device ID table */
1118static const struct i2c_device_id tvp7002_id[] = {
1119 { "tvp7002", 0 },
1120 { }
1121};
1122MODULE_DEVICE_TABLE(i2c, tvp7002_id);
1123
1124/* I2C driver data */
1125static struct i2c_driver tvp7002_driver = {
1126 .driver = {
1127 .owner = THIS_MODULE,
1128 .name = TVP7002_MODULE_NAME,
1129 },
1130 .probe = tvp7002_probe,
1131 .remove = tvp7002_remove,
1132 .id_table = tvp7002_id,
1133};
1134
Axel Linc6e8d862012-02-12 06:56:32 -03001135module_i2c_driver(tvp7002_driver);