blob: ada4ec5ef7828e00b955a90e73b124a42015f304 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -03002/* Texas Instruments Triple 8-/10-BIT 165-/110-MSPS Video and Graphics
3 * Digitizer with Horizontal PLL registers
4 *
5 * Copyright (C) 2009 Texas Instruments Inc
6 * Author: Santiago Nunez-Corrales <santiago.nunez@ridgerun.com>
7 *
8 * This code is partially based upon the TVP5150 driver
Mauro Carvalho Chehab32590812018-04-25 05:34:48 -04009 * written by Mauro Carvalho Chehab <mchehab@kernel.org>,
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -030010 * the TVP514x driver written by Vaibhav Hiremath <hvaibhav@ti.com>
11 * and the TVP7002 driver in the TI LSP 2.10.00.14. Revisions by
12 * Muralidharan Karicheri and Snehaprabha Narnakaje (TI).
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -030013 */
14#include <linux/delay.h>
15#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -030017#include <linux/videodev2.h>
Paul Gortmaker7a707b82011-07-03 14:03:12 -040018#include <linux/module.h>
Sachin Kamatb91670a2013-10-18 00:07:14 -030019#include <linux/of.h>
Philipp Zabelfd9fdb72014-02-10 22:01:48 +010020#include <linux/of_graph.h>
Hans Verkuileb8305b2012-05-15 08:07:24 -030021#include <linux/v4l2-dv-timings.h>
Mauro Carvalho Chehabb5dcee22015-11-10 12:01:44 -020022#include <media/i2c/tvp7002.h>
Lad, Prabhakar25ba2c82013-06-22 13:44:14 -030023#include <media/v4l2-async.h>
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -030024#include <media/v4l2-device.h>
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -030025#include <media/v4l2-common.h>
Hans Verkuil0eb73de2010-12-12 08:46:15 -030026#include <media/v4l2-ctrls.h>
Sakari Ailus859969b2016-08-26 20:17:25 -030027#include <media/v4l2-fwnode.h>
Lad, Prabhakarc0d96442013-08-11 02:25:21 -030028
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -030029#include "tvp7002_reg.h"
30
31MODULE_DESCRIPTION("TI TVP7002 Video and Graphics Digitizer driver");
32MODULE_AUTHOR("Santiago Nunez-Corrales <santiago.nunez@ridgerun.com>");
33MODULE_LICENSE("GPL");
34
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -030035/* I2C retry attempts */
36#define I2C_RETRY_COUNT (5)
37
38/* End of registers */
39#define TVP7002_EOR 0x5c
40
41/* Read write definition for registers */
42#define TVP7002_READ 0
43#define TVP7002_WRITE 1
44#define TVP7002_RESERVED 2
45
46/* Interlaced vs progressive mask and shift */
47#define TVP7002_IP_SHIFT 5
48#define TVP7002_INPR_MASK (0x01 << TVP7002_IP_SHIFT)
49
50/* Shift for CPL and LPF registers */
51#define TVP7002_CL_SHIFT 8
52#define TVP7002_CL_MASK 0x0f
53
54/* Debug functions */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103055static bool debug;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -030056module_param(debug, bool, 0644);
57MODULE_PARM_DESC(debug, "Debug level (0-2)");
58
59/* Structure for register values */
60struct i2c_reg_value {
61 u8 reg;
62 u8 value;
63 u8 type;
64};
65
66/*
67 * Register default values (according to tvp7002 datasheet)
68 * In the case of read-only registers, the value (0xff) is
69 * never written. R/W functionality is controlled by the
70 * writable bit in the register struct definition.
71 */
72static const struct i2c_reg_value tvp7002_init_default[] = {
73 { TVP7002_CHIP_REV, 0xff, TVP7002_READ },
74 { TVP7002_HPLL_FDBK_DIV_MSBS, 0x67, TVP7002_WRITE },
75 { TVP7002_HPLL_FDBK_DIV_LSBS, 0x20, TVP7002_WRITE },
76 { TVP7002_HPLL_CRTL, 0xa0, TVP7002_WRITE },
77 { TVP7002_HPLL_PHASE_SEL, 0x80, TVP7002_WRITE },
78 { TVP7002_CLAMP_START, 0x32, TVP7002_WRITE },
79 { TVP7002_CLAMP_W, 0x20, TVP7002_WRITE },
80 { TVP7002_HSYNC_OUT_W, 0x60, TVP7002_WRITE },
81 { TVP7002_B_FINE_GAIN, 0x00, TVP7002_WRITE },
82 { TVP7002_G_FINE_GAIN, 0x00, TVP7002_WRITE },
83 { TVP7002_R_FINE_GAIN, 0x00, TVP7002_WRITE },
84 { TVP7002_B_FINE_OFF_MSBS, 0x80, TVP7002_WRITE },
85 { TVP7002_G_FINE_OFF_MSBS, 0x80, TVP7002_WRITE },
86 { TVP7002_R_FINE_OFF_MSBS, 0x80, TVP7002_WRITE },
87 { TVP7002_SYNC_CTL_1, 0x20, TVP7002_WRITE },
88 { TVP7002_HPLL_AND_CLAMP_CTL, 0x2e, TVP7002_WRITE },
89 { TVP7002_SYNC_ON_G_THRS, 0x5d, TVP7002_WRITE },
90 { TVP7002_SYNC_SEPARATOR_THRS, 0x47, TVP7002_WRITE },
91 { TVP7002_HPLL_PRE_COAST, 0x00, TVP7002_WRITE },
92 { TVP7002_HPLL_POST_COAST, 0x00, TVP7002_WRITE },
93 { TVP7002_SYNC_DETECT_STAT, 0xff, TVP7002_READ },
94 { TVP7002_OUT_FORMATTER, 0x47, TVP7002_WRITE },
95 { TVP7002_MISC_CTL_1, 0x01, TVP7002_WRITE },
96 { TVP7002_MISC_CTL_2, 0x00, TVP7002_WRITE },
97 { TVP7002_MISC_CTL_3, 0x01, TVP7002_WRITE },
98 { TVP7002_IN_MUX_SEL_1, 0x00, TVP7002_WRITE },
99 { TVP7002_IN_MUX_SEL_2, 0x67, TVP7002_WRITE },
100 { TVP7002_B_AND_G_COARSE_GAIN, 0x77, TVP7002_WRITE },
101 { TVP7002_R_COARSE_GAIN, 0x07, TVP7002_WRITE },
102 { TVP7002_FINE_OFF_LSBS, 0x00, TVP7002_WRITE },
103 { TVP7002_B_COARSE_OFF, 0x10, TVP7002_WRITE },
104 { TVP7002_G_COARSE_OFF, 0x10, TVP7002_WRITE },
105 { TVP7002_R_COARSE_OFF, 0x10, TVP7002_WRITE },
106 { TVP7002_HSOUT_OUT_START, 0x08, TVP7002_WRITE },
107 { TVP7002_MISC_CTL_4, 0x00, TVP7002_WRITE },
108 { TVP7002_B_DGTL_ALC_OUT_LSBS, 0xff, TVP7002_READ },
109 { TVP7002_G_DGTL_ALC_OUT_LSBS, 0xff, TVP7002_READ },
110 { TVP7002_R_DGTL_ALC_OUT_LSBS, 0xff, TVP7002_READ },
111 { TVP7002_AUTO_LVL_CTL_ENABLE, 0x80, TVP7002_WRITE },
112 { TVP7002_DGTL_ALC_OUT_MSBS, 0xff, TVP7002_READ },
113 { TVP7002_AUTO_LVL_CTL_FILTER, 0x53, TVP7002_WRITE },
114 { 0x29, 0x08, TVP7002_RESERVED },
115 { TVP7002_FINE_CLAMP_CTL, 0x07, TVP7002_WRITE },
116 /* PWR_CTL is controlled only by the probe and reset functions */
117 { TVP7002_PWR_CTL, 0x00, TVP7002_RESERVED },
118 { TVP7002_ADC_SETUP, 0x50, TVP7002_WRITE },
119 { TVP7002_COARSE_CLAMP_CTL, 0x00, TVP7002_WRITE },
120 { TVP7002_SOG_CLAMP, 0x80, TVP7002_WRITE },
Mats Randgaard425b91c2010-08-03 04:18:04 -0300121 { TVP7002_RGB_COARSE_CLAMP_CTL, 0x8c, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300122 { TVP7002_SOG_COARSE_CLAMP_CTL, 0x04, TVP7002_WRITE },
123 { TVP7002_ALC_PLACEMENT, 0x5a, TVP7002_WRITE },
124 { 0x32, 0x18, TVP7002_RESERVED },
125 { 0x33, 0x60, TVP7002_RESERVED },
126 { TVP7002_MVIS_STRIPPER_W, 0xff, TVP7002_RESERVED },
127 { TVP7002_VSYNC_ALGN, 0x10, TVP7002_WRITE },
128 { TVP7002_SYNC_BYPASS, 0x00, TVP7002_WRITE },
129 { TVP7002_L_FRAME_STAT_LSBS, 0xff, TVP7002_READ },
130 { TVP7002_L_FRAME_STAT_MSBS, 0xff, TVP7002_READ },
131 { TVP7002_CLK_L_STAT_LSBS, 0xff, TVP7002_READ },
132 { TVP7002_CLK_L_STAT_MSBS, 0xff, TVP7002_READ },
133 { TVP7002_HSYNC_W, 0xff, TVP7002_READ },
134 { TVP7002_VSYNC_W, 0xff, TVP7002_READ },
135 { TVP7002_L_LENGTH_TOL, 0x03, TVP7002_WRITE },
136 { 0x3e, 0x60, TVP7002_RESERVED },
137 { TVP7002_VIDEO_BWTH_CTL, 0x01, TVP7002_WRITE },
138 { TVP7002_AVID_START_PIXEL_LSBS, 0x01, TVP7002_WRITE },
139 { TVP7002_AVID_START_PIXEL_MSBS, 0x2c, TVP7002_WRITE },
140 { TVP7002_AVID_STOP_PIXEL_LSBS, 0x06, TVP7002_WRITE },
141 { TVP7002_AVID_STOP_PIXEL_MSBS, 0x2c, TVP7002_WRITE },
142 { TVP7002_VBLK_F_0_START_L_OFF, 0x05, TVP7002_WRITE },
143 { TVP7002_VBLK_F_1_START_L_OFF, 0x00, TVP7002_WRITE },
144 { TVP7002_VBLK_F_0_DURATION, 0x1e, TVP7002_WRITE },
145 { TVP7002_VBLK_F_1_DURATION, 0x00, TVP7002_WRITE },
146 { TVP7002_FBIT_F_0_START_L_OFF, 0x00, TVP7002_WRITE },
147 { TVP7002_FBIT_F_1_START_L_OFF, 0x00, TVP7002_WRITE },
148 { TVP7002_YUV_Y_G_COEF_LSBS, 0xe3, TVP7002_WRITE },
149 { TVP7002_YUV_Y_G_COEF_MSBS, 0x16, TVP7002_WRITE },
150 { TVP7002_YUV_Y_B_COEF_LSBS, 0x4f, TVP7002_WRITE },
151 { TVP7002_YUV_Y_B_COEF_MSBS, 0x02, TVP7002_WRITE },
152 { TVP7002_YUV_Y_R_COEF_LSBS, 0xce, TVP7002_WRITE },
153 { TVP7002_YUV_Y_R_COEF_MSBS, 0x06, TVP7002_WRITE },
154 { TVP7002_YUV_U_G_COEF_LSBS, 0xab, TVP7002_WRITE },
155 { TVP7002_YUV_U_G_COEF_MSBS, 0xf3, TVP7002_WRITE },
156 { TVP7002_YUV_U_B_COEF_LSBS, 0x00, TVP7002_WRITE },
157 { TVP7002_YUV_U_B_COEF_MSBS, 0x10, TVP7002_WRITE },
158 { TVP7002_YUV_U_R_COEF_LSBS, 0x55, TVP7002_WRITE },
159 { TVP7002_YUV_U_R_COEF_MSBS, 0xfc, TVP7002_WRITE },
160 { TVP7002_YUV_V_G_COEF_LSBS, 0x78, TVP7002_WRITE },
161 { TVP7002_YUV_V_G_COEF_MSBS, 0xf1, TVP7002_WRITE },
162 { TVP7002_YUV_V_B_COEF_LSBS, 0x88, TVP7002_WRITE },
163 { TVP7002_YUV_V_B_COEF_MSBS, 0xfe, TVP7002_WRITE },
164 { TVP7002_YUV_V_R_COEF_LSBS, 0x00, TVP7002_WRITE },
165 { TVP7002_YUV_V_R_COEF_MSBS, 0x10, TVP7002_WRITE },
166 /* This signals end of register values */
167 { TVP7002_EOR, 0xff, TVP7002_RESERVED }
168};
169
170/* Register parameters for 480P */
171static const struct i2c_reg_value tvp7002_parms_480P[] = {
172 { TVP7002_HPLL_FDBK_DIV_MSBS, 0x35, TVP7002_WRITE },
Rajashekhara, Sudhakar560afa72010-07-28 05:47:48 -0300173 { TVP7002_HPLL_FDBK_DIV_LSBS, 0xa0, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300174 { TVP7002_HPLL_CRTL, 0x02, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300175 { TVP7002_AVID_START_PIXEL_LSBS, 0x91, TVP7002_WRITE },
176 { TVP7002_AVID_START_PIXEL_MSBS, 0x00, TVP7002_WRITE },
177 { TVP7002_AVID_STOP_PIXEL_LSBS, 0x0B, TVP7002_WRITE },
178 { TVP7002_AVID_STOP_PIXEL_MSBS, 0x00, TVP7002_WRITE },
179 { TVP7002_VBLK_F_0_START_L_OFF, 0x03, TVP7002_WRITE },
180 { TVP7002_VBLK_F_1_START_L_OFF, 0x01, TVP7002_WRITE },
181 { TVP7002_VBLK_F_0_DURATION, 0x13, TVP7002_WRITE },
182 { TVP7002_VBLK_F_1_DURATION, 0x13, TVP7002_WRITE },
183 { TVP7002_ALC_PLACEMENT, 0x18, TVP7002_WRITE },
184 { TVP7002_CLAMP_START, 0x06, TVP7002_WRITE },
185 { TVP7002_CLAMP_W, 0x10, TVP7002_WRITE },
186 { TVP7002_HPLL_PRE_COAST, 0x03, TVP7002_WRITE },
187 { TVP7002_HPLL_POST_COAST, 0x03, TVP7002_WRITE },
188 { TVP7002_EOR, 0xff, TVP7002_RESERVED }
189};
190
191/* Register parameters for 576P */
192static const struct i2c_reg_value tvp7002_parms_576P[] = {
193 { TVP7002_HPLL_FDBK_DIV_MSBS, 0x36, TVP7002_WRITE },
194 { TVP7002_HPLL_FDBK_DIV_LSBS, 0x00, TVP7002_WRITE },
195 { TVP7002_HPLL_CRTL, 0x18, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300196 { TVP7002_AVID_START_PIXEL_LSBS, 0x9B, TVP7002_WRITE },
197 { TVP7002_AVID_START_PIXEL_MSBS, 0x00, TVP7002_WRITE },
198 { TVP7002_AVID_STOP_PIXEL_LSBS, 0x0F, TVP7002_WRITE },
199 { TVP7002_AVID_STOP_PIXEL_MSBS, 0x00, TVP7002_WRITE },
200 { TVP7002_VBLK_F_0_START_L_OFF, 0x00, TVP7002_WRITE },
201 { TVP7002_VBLK_F_1_START_L_OFF, 0x00, TVP7002_WRITE },
202 { TVP7002_VBLK_F_0_DURATION, 0x2D, TVP7002_WRITE },
203 { TVP7002_VBLK_F_1_DURATION, 0x00, TVP7002_WRITE },
204 { TVP7002_ALC_PLACEMENT, 0x18, TVP7002_WRITE },
205 { TVP7002_CLAMP_START, 0x06, TVP7002_WRITE },
206 { TVP7002_CLAMP_W, 0x10, TVP7002_WRITE },
207 { TVP7002_HPLL_PRE_COAST, 0x03, TVP7002_WRITE },
208 { TVP7002_HPLL_POST_COAST, 0x03, TVP7002_WRITE },
209 { TVP7002_EOR, 0xff, TVP7002_RESERVED }
210};
211
212/* Register parameters for 1080I60 */
213static const struct i2c_reg_value tvp7002_parms_1080I60[] = {
214 { TVP7002_HPLL_FDBK_DIV_MSBS, 0x89, TVP7002_WRITE },
Rajashekhara, Sudhakar560afa72010-07-28 05:47:48 -0300215 { TVP7002_HPLL_FDBK_DIV_LSBS, 0x80, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300216 { TVP7002_HPLL_CRTL, 0x98, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300217 { TVP7002_AVID_START_PIXEL_LSBS, 0x06, TVP7002_WRITE },
218 { TVP7002_AVID_START_PIXEL_MSBS, 0x01, TVP7002_WRITE },
219 { TVP7002_AVID_STOP_PIXEL_LSBS, 0x8a, TVP7002_WRITE },
220 { TVP7002_AVID_STOP_PIXEL_MSBS, 0x08, TVP7002_WRITE },
221 { TVP7002_VBLK_F_0_START_L_OFF, 0x02, TVP7002_WRITE },
222 { TVP7002_VBLK_F_1_START_L_OFF, 0x02, TVP7002_WRITE },
223 { TVP7002_VBLK_F_0_DURATION, 0x16, TVP7002_WRITE },
224 { TVP7002_VBLK_F_1_DURATION, 0x17, TVP7002_WRITE },
225 { TVP7002_ALC_PLACEMENT, 0x5a, TVP7002_WRITE },
226 { TVP7002_CLAMP_START, 0x32, TVP7002_WRITE },
227 { TVP7002_CLAMP_W, 0x20, TVP7002_WRITE },
228 { TVP7002_HPLL_PRE_COAST, 0x01, TVP7002_WRITE },
229 { TVP7002_HPLL_POST_COAST, 0x00, TVP7002_WRITE },
230 { TVP7002_EOR, 0xff, TVP7002_RESERVED }
231};
232
233/* Register parameters for 1080P60 */
234static const struct i2c_reg_value tvp7002_parms_1080P60[] = {
235 { TVP7002_HPLL_FDBK_DIV_MSBS, 0x89, TVP7002_WRITE },
Rajashekhara, Sudhakar560afa72010-07-28 05:47:48 -0300236 { TVP7002_HPLL_FDBK_DIV_LSBS, 0x80, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300237 { TVP7002_HPLL_CRTL, 0xE0, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300238 { TVP7002_AVID_START_PIXEL_LSBS, 0x06, TVP7002_WRITE },
239 { TVP7002_AVID_START_PIXEL_MSBS, 0x01, TVP7002_WRITE },
240 { TVP7002_AVID_STOP_PIXEL_LSBS, 0x8a, TVP7002_WRITE },
241 { TVP7002_AVID_STOP_PIXEL_MSBS, 0x08, TVP7002_WRITE },
242 { TVP7002_VBLK_F_0_START_L_OFF, 0x02, TVP7002_WRITE },
243 { TVP7002_VBLK_F_1_START_L_OFF, 0x02, TVP7002_WRITE },
244 { TVP7002_VBLK_F_0_DURATION, 0x16, TVP7002_WRITE },
245 { TVP7002_VBLK_F_1_DURATION, 0x17, TVP7002_WRITE },
246 { TVP7002_ALC_PLACEMENT, 0x5a, TVP7002_WRITE },
247 { TVP7002_CLAMP_START, 0x32, TVP7002_WRITE },
248 { TVP7002_CLAMP_W, 0x20, TVP7002_WRITE },
249 { TVP7002_HPLL_PRE_COAST, 0x01, TVP7002_WRITE },
250 { TVP7002_HPLL_POST_COAST, 0x00, TVP7002_WRITE },
251 { TVP7002_EOR, 0xff, TVP7002_RESERVED }
252};
253
254/* Register parameters for 1080I50 */
255static const struct i2c_reg_value tvp7002_parms_1080I50[] = {
256 { TVP7002_HPLL_FDBK_DIV_MSBS, 0xa5, TVP7002_WRITE },
257 { TVP7002_HPLL_FDBK_DIV_LSBS, 0x00, TVP7002_WRITE },
258 { TVP7002_HPLL_CRTL, 0x98, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300259 { TVP7002_AVID_START_PIXEL_LSBS, 0x06, TVP7002_WRITE },
260 { TVP7002_AVID_START_PIXEL_MSBS, 0x01, TVP7002_WRITE },
261 { TVP7002_AVID_STOP_PIXEL_LSBS, 0x8a, TVP7002_WRITE },
262 { TVP7002_AVID_STOP_PIXEL_MSBS, 0x08, TVP7002_WRITE },
263 { TVP7002_VBLK_F_0_START_L_OFF, 0x02, TVP7002_WRITE },
264 { TVP7002_VBLK_F_1_START_L_OFF, 0x02, TVP7002_WRITE },
265 { TVP7002_VBLK_F_0_DURATION, 0x16, TVP7002_WRITE },
266 { TVP7002_VBLK_F_1_DURATION, 0x17, TVP7002_WRITE },
267 { TVP7002_ALC_PLACEMENT, 0x5a, TVP7002_WRITE },
268 { TVP7002_CLAMP_START, 0x32, TVP7002_WRITE },
269 { TVP7002_CLAMP_W, 0x20, TVP7002_WRITE },
270 { TVP7002_HPLL_PRE_COAST, 0x01, TVP7002_WRITE },
271 { TVP7002_HPLL_POST_COAST, 0x00, TVP7002_WRITE },
272 { TVP7002_EOR, 0xff, TVP7002_RESERVED }
273};
274
275/* Register parameters for 720P60 */
276static const struct i2c_reg_value tvp7002_parms_720P60[] = {
277 { TVP7002_HPLL_FDBK_DIV_MSBS, 0x67, TVP7002_WRITE },
Rajashekhara, Sudhakar560afa72010-07-28 05:47:48 -0300278 { TVP7002_HPLL_FDBK_DIV_LSBS, 0x20, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300279 { TVP7002_HPLL_CRTL, 0xa0, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300280 { TVP7002_AVID_START_PIXEL_LSBS, 0x47, TVP7002_WRITE },
281 { TVP7002_AVID_START_PIXEL_MSBS, 0x01, TVP7002_WRITE },
282 { TVP7002_AVID_STOP_PIXEL_LSBS, 0x4B, TVP7002_WRITE },
283 { TVP7002_AVID_STOP_PIXEL_MSBS, 0x06, TVP7002_WRITE },
284 { TVP7002_VBLK_F_0_START_L_OFF, 0x05, TVP7002_WRITE },
285 { TVP7002_VBLK_F_1_START_L_OFF, 0x00, TVP7002_WRITE },
286 { TVP7002_VBLK_F_0_DURATION, 0x2D, TVP7002_WRITE },
287 { TVP7002_VBLK_F_1_DURATION, 0x00, TVP7002_WRITE },
288 { TVP7002_ALC_PLACEMENT, 0x5a, TVP7002_WRITE },
289 { TVP7002_CLAMP_START, 0x32, TVP7002_WRITE },
290 { TVP7002_CLAMP_W, 0x20, TVP7002_WRITE },
291 { TVP7002_HPLL_PRE_COAST, 0x00, TVP7002_WRITE },
292 { TVP7002_HPLL_POST_COAST, 0x00, TVP7002_WRITE },
293 { TVP7002_EOR, 0xff, TVP7002_RESERVED }
294};
295
296/* Register parameters for 720P50 */
297static const struct i2c_reg_value tvp7002_parms_720P50[] = {
298 { TVP7002_HPLL_FDBK_DIV_MSBS, 0x7b, TVP7002_WRITE },
Rajashekhara, Sudhakar560afa72010-07-28 05:47:48 -0300299 { TVP7002_HPLL_FDBK_DIV_LSBS, 0xc0, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300300 { TVP7002_HPLL_CRTL, 0x98, TVP7002_WRITE },
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300301 { TVP7002_AVID_START_PIXEL_LSBS, 0x47, TVP7002_WRITE },
302 { TVP7002_AVID_START_PIXEL_MSBS, 0x01, TVP7002_WRITE },
303 { TVP7002_AVID_STOP_PIXEL_LSBS, 0x4B, TVP7002_WRITE },
304 { TVP7002_AVID_STOP_PIXEL_MSBS, 0x06, TVP7002_WRITE },
305 { TVP7002_VBLK_F_0_START_L_OFF, 0x05, TVP7002_WRITE },
306 { TVP7002_VBLK_F_1_START_L_OFF, 0x00, TVP7002_WRITE },
307 { TVP7002_VBLK_F_0_DURATION, 0x2D, TVP7002_WRITE },
308 { TVP7002_VBLK_F_1_DURATION, 0x00, TVP7002_WRITE },
309 { TVP7002_ALC_PLACEMENT, 0x5a, TVP7002_WRITE },
310 { TVP7002_CLAMP_START, 0x32, TVP7002_WRITE },
311 { TVP7002_CLAMP_W, 0x20, TVP7002_WRITE },
312 { TVP7002_HPLL_PRE_COAST, 0x01, TVP7002_WRITE },
313 { TVP7002_HPLL_POST_COAST, 0x00, TVP7002_WRITE },
314 { TVP7002_EOR, 0xff, TVP7002_RESERVED }
315};
316
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300317/* Timings definition for handling device operation */
318struct tvp7002_timings_definition {
Hans Verkuileb8305b2012-05-15 08:07:24 -0300319 struct v4l2_dv_timings timings;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300320 const struct i2c_reg_value *p_settings;
321 enum v4l2_colorspace color_space;
322 enum v4l2_field scanmode;
323 u16 progressive;
324 u16 lines_per_frame;
325 u16 cpl_min;
326 u16 cpl_max;
327};
328
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300329/* Struct list for digital video timings */
330static const struct tvp7002_timings_definition tvp7002_timings[] = {
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300331 {
Hans Verkuileb8305b2012-05-15 08:07:24 -0300332 V4L2_DV_BT_CEA_1280X720P60,
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300333 tvp7002_parms_720P60,
334 V4L2_COLORSPACE_REC709,
335 V4L2_FIELD_NONE,
336 1,
337 0x2EE,
338 135,
339 153
340 },
341 {
Hans Verkuileb8305b2012-05-15 08:07:24 -0300342 V4L2_DV_BT_CEA_1920X1080I60,
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300343 tvp7002_parms_1080I60,
344 V4L2_COLORSPACE_REC709,
345 V4L2_FIELD_INTERLACED,
346 0,
347 0x465,
348 181,
349 205
350 },
351 {
Hans Verkuileb8305b2012-05-15 08:07:24 -0300352 V4L2_DV_BT_CEA_1920X1080I50,
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300353 tvp7002_parms_1080I50,
354 V4L2_COLORSPACE_REC709,
355 V4L2_FIELD_INTERLACED,
356 0,
357 0x465,
358 217,
359 245
360 },
361 {
Hans Verkuileb8305b2012-05-15 08:07:24 -0300362 V4L2_DV_BT_CEA_1280X720P50,
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300363 tvp7002_parms_720P50,
364 V4L2_COLORSPACE_REC709,
365 V4L2_FIELD_NONE,
366 1,
367 0x2EE,
368 163,
369 183
370 },
371 {
Hans Verkuileb8305b2012-05-15 08:07:24 -0300372 V4L2_DV_BT_CEA_1920X1080P60,
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300373 tvp7002_parms_1080P60,
374 V4L2_COLORSPACE_REC709,
375 V4L2_FIELD_NONE,
376 1,
377 0x465,
378 90,
379 102
380 },
381 {
Hans Verkuileb8305b2012-05-15 08:07:24 -0300382 V4L2_DV_BT_CEA_720X480P59_94,
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300383 tvp7002_parms_480P,
384 V4L2_COLORSPACE_SMPTE170M,
385 V4L2_FIELD_NONE,
386 1,
387 0x20D,
388 0xffff,
389 0xffff
390 },
391 {
Hans Verkuileb8305b2012-05-15 08:07:24 -0300392 V4L2_DV_BT_CEA_720X576P50,
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300393 tvp7002_parms_576P,
394 V4L2_COLORSPACE_SMPTE170M,
395 V4L2_FIELD_NONE,
396 1,
397 0x271,
398 0xffff,
399 0xffff
400 }
401};
402
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300403#define NUM_TIMINGS ARRAY_SIZE(tvp7002_timings)
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300404
405/* Device definition */
406struct tvp7002 {
407 struct v4l2_subdev sd;
Hans Verkuil0eb73de2010-12-12 08:46:15 -0300408 struct v4l2_ctrl_handler hdl;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300409 const struct tvp7002_config *pdata;
410
411 int ver;
412 int streaming;
413
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300414 const struct tvp7002_timings_definition *current_timings;
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -0300415 struct media_pad pad;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300416};
417
418/*
419 * to_tvp7002 - Obtain device handler TVP7002
420 * @sd: ptr to v4l2_subdev struct
421 *
422 * Returns device handler tvp7002.
423 */
424static inline struct tvp7002 *to_tvp7002(struct v4l2_subdev *sd)
425{
426 return container_of(sd, struct tvp7002, sd);
427}
428
Hans Verkuil0eb73de2010-12-12 08:46:15 -0300429static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
430{
431 return &container_of(ctrl->handler, struct tvp7002, hdl)->sd;
432}
433
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300434/*
435 * tvp7002_read - Read a value from a register in an TVP7002
436 * @sd: ptr to v4l2_subdev struct
Mats Randgaard6fa7dac2010-05-05 07:50:38 -0300437 * @addr: TVP7002 register address
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300438 * @dst: pointer to 8-bit destination
439 *
440 * Returns value read if successful, or non-zero (-1) otherwise.
441 */
442static int tvp7002_read(struct v4l2_subdev *sd, u8 addr, u8 *dst)
443{
444 struct i2c_client *c = v4l2_get_subdevdata(sd);
445 int retry;
446 int error;
447
448 for (retry = 0; retry < I2C_RETRY_COUNT; retry++) {
449 error = i2c_smbus_read_byte_data(c, addr);
450
451 if (error >= 0) {
452 *dst = (u8)error;
453 return 0;
454 }
455
456 msleep_interruptible(10);
457 }
458 v4l2_err(sd, "TVP7002 read error %d\n", error);
459 return error;
460}
461
462/*
463 * tvp7002_read_err() - Read a register value with error code
464 * @sd: pointer to standard V4L2 sub-device structure
465 * @reg: destination register
466 * @val: value to be read
Mats Randgaard6fa7dac2010-05-05 07:50:38 -0300467 * @err: pointer to error value
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300468 *
469 * Read a value in a register and save error value in pointer.
470 * Also update the register table if successful
471 */
472static inline void tvp7002_read_err(struct v4l2_subdev *sd, u8 reg,
473 u8 *dst, int *err)
474{
475 if (!*err)
476 *err = tvp7002_read(sd, reg, dst);
477}
478
479/*
480 * tvp7002_write() - Write a value to a register in TVP7002
481 * @sd: ptr to v4l2_subdev struct
482 * @addr: TVP7002 register address
483 * @value: value to be written to the register
484 *
485 * Write a value to a register in an TVP7002 decoder device.
486 * Returns zero if successful, or non-zero otherwise.
487 */
488static int tvp7002_write(struct v4l2_subdev *sd, u8 addr, u8 value)
489{
490 struct i2c_client *c;
491 int retry;
492 int error;
493
494 c = v4l2_get_subdevdata(sd);
495
496 for (retry = 0; retry < I2C_RETRY_COUNT; retry++) {
497 error = i2c_smbus_write_byte_data(c, addr, value);
498
499 if (error >= 0)
500 return 0;
501
502 v4l2_warn(sd, "Write: retry ... %d\n", retry);
503 msleep_interruptible(10);
504 }
505 v4l2_err(sd, "TVP7002 write error %d\n", error);
506 return error;
507}
508
509/*
510 * tvp7002_write_err() - Write a register value with error code
511 * @sd: pointer to standard V4L2 sub-device structure
512 * @reg: destination register
513 * @val: value to be written
Mats Randgaard6fa7dac2010-05-05 07:50:38 -0300514 * @err: pointer to error value
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300515 *
516 * Write a value in a register and save error value in pointer.
517 * Also update the register table if successful
518 */
519static inline void tvp7002_write_err(struct v4l2_subdev *sd, u8 reg,
520 u8 val, int *err)
521{
522 if (!*err)
523 *err = tvp7002_write(sd, reg, val);
524}
525
526/*
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300527 * tvp7002_write_inittab() - Write initialization values
528 * @sd: ptr to v4l2_subdev struct
529 * @regs: ptr to i2c_reg_value struct
530 *
531 * Write initialization values.
532 * Returns zero or -EINVAL if read operation fails.
533 */
534static int tvp7002_write_inittab(struct v4l2_subdev *sd,
535 const struct i2c_reg_value *regs)
536{
537 int error = 0;
538
539 /* Initialize the first (defined) registers */
540 while (TVP7002_EOR != regs->reg) {
541 if (TVP7002_WRITE == regs->type)
542 tvp7002_write_err(sd, regs->reg, regs->value, &error);
543 regs++;
544 }
545
546 return error;
547}
548
Hans Verkuileb8305b2012-05-15 08:07:24 -0300549static int tvp7002_s_dv_timings(struct v4l2_subdev *sd,
550 struct v4l2_dv_timings *dv_timings)
551{
552 struct tvp7002 *device = to_tvp7002(sd);
553 const struct v4l2_bt_timings *bt = &dv_timings->bt;
554 int i;
555
556 if (dv_timings->type != V4L2_DV_BT_656_1120)
557 return -EINVAL;
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300558 for (i = 0; i < NUM_TIMINGS; i++) {
559 const struct v4l2_bt_timings *t = &tvp7002_timings[i].timings.bt;
Hans Verkuileb8305b2012-05-15 08:07:24 -0300560
561 if (!memcmp(bt, t, &bt->standards - &bt->width)) {
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300562 device->current_timings = &tvp7002_timings[i];
563 return tvp7002_write_inittab(sd, tvp7002_timings[i].p_settings);
Hans Verkuileb8305b2012-05-15 08:07:24 -0300564 }
565 }
566 return -EINVAL;
567}
568
569static int tvp7002_g_dv_timings(struct v4l2_subdev *sd,
570 struct v4l2_dv_timings *dv_timings)
571{
572 struct tvp7002 *device = to_tvp7002(sd);
573
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300574 *dv_timings = device->current_timings->timings;
Hans Verkuileb8305b2012-05-15 08:07:24 -0300575 return 0;
576}
577
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300578/*
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300579 * tvp7002_s_ctrl() - Set a control
Hans Verkuil0eb73de2010-12-12 08:46:15 -0300580 * @ctrl: ptr to v4l2_ctrl struct
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300581 *
582 * Set a control in TVP7002 decoder device.
583 * Returns zero when successful or -EINVAL if register access fails.
584 */
Hans Verkuil0eb73de2010-12-12 08:46:15 -0300585static int tvp7002_s_ctrl(struct v4l2_ctrl *ctrl)
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300586{
Hans Verkuil0eb73de2010-12-12 08:46:15 -0300587 struct v4l2_subdev *sd = to_sd(ctrl);
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300588 int error = 0;
589
590 switch (ctrl->id) {
591 case V4L2_CID_GAIN:
Hans Verkuil0eb73de2010-12-12 08:46:15 -0300592 tvp7002_write_err(sd, TVP7002_R_FINE_GAIN, ctrl->val, &error);
593 tvp7002_write_err(sd, TVP7002_G_FINE_GAIN, ctrl->val, &error);
594 tvp7002_write_err(sd, TVP7002_B_FINE_GAIN, ctrl->val, &error);
595 return error;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300596 }
Hans Verkuil0eb73de2010-12-12 08:46:15 -0300597 return -EINVAL;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300598}
599
600/*
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300601 * tvp7002_query_dv() - query DV timings
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300602 * @sd: pointer to standard V4L2 sub-device structure
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300603 * @index: index into the tvp7002_timings array
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300604 *
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300605 * Returns the current DV timings detected by TVP7002. If no active input is
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300606 * detected, returns -EINVAL
607 */
Hans Verkuileb8305b2012-05-15 08:07:24 -0300608static int tvp7002_query_dv(struct v4l2_subdev *sd, int *index)
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300609{
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300610 const struct tvp7002_timings_definition *timings = tvp7002_timings;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300611 u8 progressive;
612 u32 lpfr;
613 u32 cpln;
614 int error = 0;
615 u8 lpf_lsb;
616 u8 lpf_msb;
617 u8 cpl_lsb;
618 u8 cpl_msb;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300619
Hans Verkuileb8305b2012-05-15 08:07:24 -0300620 /* Return invalid index if no active input is detected */
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300621 *index = NUM_TIMINGS;
Mats Randgaardab0ab192010-08-03 04:18:03 -0300622
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300623 /* Read standards from device registers */
624 tvp7002_read_err(sd, TVP7002_L_FRAME_STAT_LSBS, &lpf_lsb, &error);
625 tvp7002_read_err(sd, TVP7002_L_FRAME_STAT_MSBS, &lpf_msb, &error);
626
627 if (error < 0)
628 return error;
629
630 tvp7002_read_err(sd, TVP7002_CLK_L_STAT_LSBS, &cpl_lsb, &error);
631 tvp7002_read_err(sd, TVP7002_CLK_L_STAT_MSBS, &cpl_msb, &error);
632
633 if (error < 0)
634 return error;
635
636 /* Get lines per frame, clocks per line and interlaced/progresive */
637 lpfr = lpf_lsb | ((TVP7002_CL_MASK & lpf_msb) << TVP7002_CL_SHIFT);
638 cpln = cpl_lsb | ((TVP7002_CL_MASK & cpl_msb) << TVP7002_CL_SHIFT);
639 progressive = (lpf_msb & TVP7002_INPR_MASK) >> TVP7002_IP_SHIFT;
640
641 /* Do checking of video modes */
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300642 for (*index = 0; *index < NUM_TIMINGS; (*index)++, timings++)
643 if (lpfr == timings->lines_per_frame &&
644 progressive == timings->progressive) {
645 if (timings->cpl_min == 0xffff)
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300646 break;
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300647 if (cpln >= timings->cpl_min && cpln <= timings->cpl_max)
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300648 break;
649 }
650
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300651 if (*index == NUM_TIMINGS) {
Hans Verkuilcf19cd32010-05-01 08:23:07 -0300652 v4l2_dbg(1, debug, sd, "detection failed: lpf = %x, cpl = %x\n",
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300653 lpfr, cpln);
Hans Verkuileb8305b2012-05-15 08:07:24 -0300654 return -ENOLINK;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300655 }
656
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300657 /* Update lines per frame and clocks per line info */
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300658 v4l2_dbg(1, debug, sd, "detected timings: %d\n", *index);
Hans Verkuileb8305b2012-05-15 08:07:24 -0300659 return 0;
660}
661
Hans Verkuileb8305b2012-05-15 08:07:24 -0300662static int tvp7002_query_dv_timings(struct v4l2_subdev *sd,
663 struct v4l2_dv_timings *timings)
664{
665 int index;
666 int err = tvp7002_query_dv(sd, &index);
667
668 if (err)
669 return err;
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300670 *timings = tvp7002_timings[index].timings;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300671 return 0;
672}
673
674#ifdef CONFIG_VIDEO_ADV_DEBUG
675/*
676 * tvp7002_g_register() - Get the value of a register
677 * @sd: ptr to v4l2_subdev struct
Mats Randgaard6fa7dac2010-05-05 07:50:38 -0300678 * @reg: ptr to v4l2_dbg_register struct
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300679 *
680 * Get the value of a TVP7002 decoder device register.
681 * Returns zero when successful, -EINVAL if register read fails or
Lad, Prabhakar7e89bd92013-05-14 01:45:14 -0300682 * access to I2C client fails.
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300683 */
684static int tvp7002_g_register(struct v4l2_subdev *sd,
685 struct v4l2_dbg_register *reg)
686{
Hans Verkuildfbd5d42010-02-25 17:34:09 -0300687 u8 val;
688 int ret;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300689
Hans Verkuildfbd5d42010-02-25 17:34:09 -0300690 ret = tvp7002_read(sd, reg->reg & 0xff, &val);
Hans Verkuil23110722020-10-08 13:59:12 +0200691 if (ret < 0)
692 return ret;
Hans Verkuildfbd5d42010-02-25 17:34:09 -0300693 reg->val = val;
Hans Verkuil15c4fee2013-05-29 07:00:07 -0300694 reg->size = 1;
Hans Verkuil23110722020-10-08 13:59:12 +0200695 return 0;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300696}
697
698/*
699 * tvp7002_s_register() - set a control
700 * @sd: ptr to v4l2_subdev struct
Mats Randgaard6fa7dac2010-05-05 07:50:38 -0300701 * @reg: ptr to v4l2_dbg_register struct
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300702 *
703 * Get the value of a TVP7002 decoder device register.
Lad, Prabhakar7e89bd92013-05-14 01:45:14 -0300704 * Returns zero when successful, -EINVAL if register read fails.
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300705 */
706static int tvp7002_s_register(struct v4l2_subdev *sd,
Hans Verkuil977ba3b12013-03-24 08:28:46 -0300707 const struct v4l2_dbg_register *reg)
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300708{
Hans Verkuildfbd5d42010-02-25 17:34:09 -0300709 return tvp7002_write(sd, reg->reg & 0xff, reg->val & 0xff);
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300710}
711#endif
712
713/*
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300714 * tvp7002_s_stream() - V4L2 decoder i/f handler for s_stream
715 * @sd: pointer to standard V4L2 sub-device structure
716 * @enable: streaming enable or disable
717 *
718 * Sets streaming to enable or disable, if possible.
719 */
720static int tvp7002_s_stream(struct v4l2_subdev *sd, int enable)
721{
722 struct tvp7002 *device = to_tvp7002(sd);
Axel Lin18cb6502014-08-16 03:57:30 -0300723 int error;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300724
725 if (device->streaming == enable)
726 return 0;
727
Axel Lin18cb6502014-08-16 03:57:30 -0300728 /* low impedance: on, high impedance: off */
729 error = tvp7002_write(sd, TVP7002_MISC_CTL_2, enable ? 0x00 : 0x03);
730 if (error) {
731 v4l2_dbg(1, debug, sd, "Fail to set streaming\n");
732 return error;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300733 }
734
Axel Lin18cb6502014-08-16 03:57:30 -0300735 device->streaming = enable;
736 return 0;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300737}
738
739/*
740 * tvp7002_log_status() - Print information about register settings
741 * @sd: ptr to v4l2_subdev struct
742 *
743 * Log register values of a TVP7002 decoder device.
744 * Returns zero or -EINVAL if read operation fails.
745 */
746static int tvp7002_log_status(struct v4l2_subdev *sd)
747{
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300748 struct tvp7002 *device = to_tvp7002(sd);
Hans Verkuilf96067a2013-02-15 14:46:40 -0300749 const struct v4l2_bt_timings *bt;
750 int detected;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300751
Hans Verkuilf96067a2013-02-15 14:46:40 -0300752 /* Find my current timings */
753 tvp7002_query_dv(sd, &detected);
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300754
Hans Verkuilf96067a2013-02-15 14:46:40 -0300755 bt = &device->current_timings->timings.bt;
756 v4l2_info(sd, "Selected DV Timings: %ux%u\n", bt->width, bt->height);
757 if (detected == NUM_TIMINGS) {
758 v4l2_info(sd, "Detected DV Timings: None\n");
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300759 } else {
Hans Verkuilf96067a2013-02-15 14:46:40 -0300760 bt = &tvp7002_timings[detected].timings.bt;
761 v4l2_info(sd, "Detected DV Timings: %ux%u\n",
762 bt->width, bt->height);
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300763 }
764 v4l2_info(sd, "Streaming enabled: %s\n",
765 device->streaming ? "yes" : "no");
766
767 /* Print the current value of the gain control */
Hans Verkuil0eb73de2010-12-12 08:46:15 -0300768 v4l2_ctrl_handler_log_status(&device->hdl, sd->name);
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300769
770 return 0;
771}
772
Hans Verkuileb8305b2012-05-15 08:07:24 -0300773static int tvp7002_enum_dv_timings(struct v4l2_subdev *sd,
774 struct v4l2_enum_dv_timings *timings)
775{
Laurent Pinchart307d3bd2014-01-31 08:51:18 -0300776 if (timings->pad != 0)
777 return -EINVAL;
778
Hans Verkuileb8305b2012-05-15 08:07:24 -0300779 /* Check requested format index is within range */
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300780 if (timings->index >= NUM_TIMINGS)
Hans Verkuileb8305b2012-05-15 08:07:24 -0300781 return -EINVAL;
782
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300783 timings->timings = tvp7002_timings[timings->index].timings;
Hans Verkuileb8305b2012-05-15 08:07:24 -0300784 return 0;
785}
786
Hans Verkuil0eb73de2010-12-12 08:46:15 -0300787static const struct v4l2_ctrl_ops tvp7002_ctrl_ops = {
788 .s_ctrl = tvp7002_s_ctrl,
789};
790
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -0300791/*
792 * tvp7002_enum_mbus_code() - Enum supported digital video format on pad
793 * @sd: pointer to standard V4L2 sub-device structure
Hans Verkuilf7234132015-03-04 01:47:54 -0800794 * @cfg: pad configuration
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -0300795 * @code: pointer to subdev enum mbus code struct
796 *
797 * Enumerate supported digital video formats for pad.
798 */
799static int
Hans Verkuilf7234132015-03-04 01:47:54 -0800800tvp7002_enum_mbus_code(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -0300801 struct v4l2_subdev_mbus_code_enum *code)
802{
803 /* Check requested format index is within range */
804 if (code->index != 0)
805 return -EINVAL;
806
Boris BREZILLONf5fe58f2014-11-10 14:28:29 -0300807 code->code = MEDIA_BUS_FMT_YUYV10_1X20;
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -0300808
809 return 0;
810}
811
812/*
813 * tvp7002_get_pad_format() - get video format on pad
814 * @sd: pointer to standard V4L2 sub-device structure
Hans Verkuilf7234132015-03-04 01:47:54 -0800815 * @cfg: pad configuration
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -0300816 * @fmt: pointer to subdev format struct
817 *
818 * get video format for pad.
819 */
820static int
Hans Verkuilf7234132015-03-04 01:47:54 -0800821tvp7002_get_pad_format(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -0300822 struct v4l2_subdev_format *fmt)
823{
824 struct tvp7002 *tvp7002 = to_tvp7002(sd);
825
Boris BREZILLONf5fe58f2014-11-10 14:28:29 -0300826 fmt->format.code = MEDIA_BUS_FMT_YUYV10_1X20;
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -0300827 fmt->format.width = tvp7002->current_timings->timings.bt.width;
828 fmt->format.height = tvp7002->current_timings->timings.bt.height;
829 fmt->format.field = tvp7002->current_timings->scanmode;
830 fmt->format.colorspace = tvp7002->current_timings->color_space;
831
832 return 0;
833}
834
835/*
836 * tvp7002_set_pad_format() - set video format on pad
837 * @sd: pointer to standard V4L2 sub-device structure
Hans Verkuilf7234132015-03-04 01:47:54 -0800838 * @cfg: pad configuration
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -0300839 * @fmt: pointer to subdev format struct
840 *
841 * set video format for pad.
842 */
843static int
Hans Verkuilf7234132015-03-04 01:47:54 -0800844tvp7002_set_pad_format(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -0300845 struct v4l2_subdev_format *fmt)
846{
Hans Verkuilf7234132015-03-04 01:47:54 -0800847 return tvp7002_get_pad_format(sd, cfg, fmt);
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -0300848}
849
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300850/* V4L2 core operation handlers */
851static const struct v4l2_subdev_core_ops tvp7002_core_ops = {
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300852 .log_status = tvp7002_log_status,
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300853#ifdef CONFIG_VIDEO_ADV_DEBUG
854 .g_register = tvp7002_g_register,
855 .s_register = tvp7002_s_register,
856#endif
857};
858
859/* Specific video subsystem operation handlers */
860static const struct v4l2_subdev_video_ops tvp7002_video_ops = {
Hans Verkuileb8305b2012-05-15 08:07:24 -0300861 .g_dv_timings = tvp7002_g_dv_timings,
862 .s_dv_timings = tvp7002_s_dv_timings,
Hans Verkuileb8305b2012-05-15 08:07:24 -0300863 .query_dv_timings = tvp7002_query_dv_timings,
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300864 .s_stream = tvp7002_s_stream,
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300865};
866
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -0300867/* media pad related operation handlers */
868static const struct v4l2_subdev_pad_ops tvp7002_pad_ops = {
869 .enum_mbus_code = tvp7002_enum_mbus_code,
870 .get_fmt = tvp7002_get_pad_format,
871 .set_fmt = tvp7002_set_pad_format,
Laurent Pinchart307d3bd2014-01-31 08:51:18 -0300872 .enum_dv_timings = tvp7002_enum_dv_timings,
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -0300873};
874
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300875/* V4L2 top level operation handlers */
876static const struct v4l2_subdev_ops tvp7002_ops = {
877 .core = &tvp7002_core_ops,
878 .video = &tvp7002_video_ops,
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -0300879 .pad = &tvp7002_pad_ops,
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300880};
881
Lad, Prabhakarc0d96442013-08-11 02:25:21 -0300882static struct tvp7002_config *
883tvp7002_get_pdata(struct i2c_client *client)
884{
Sakari Ailus60359a22018-07-31 05:15:50 -0400885 struct v4l2_fwnode_endpoint bus_cfg = { .bus_type = 0 };
Javier Martinez Canillasbaf40b52016-01-11 14:47:14 -0200886 struct tvp7002_config *pdata = NULL;
Lad, Prabhakarc0d96442013-08-11 02:25:21 -0300887 struct device_node *endpoint;
888 unsigned int flags;
889
890 if (!IS_ENABLED(CONFIG_OF) || !client->dev.of_node)
891 return client->dev.platform_data;
892
Philipp Zabelfd9fdb72014-02-10 22:01:48 +0100893 endpoint = of_graph_get_next_endpoint(client->dev.of_node, NULL);
Lad, Prabhakarc0d96442013-08-11 02:25:21 -0300894 if (!endpoint)
895 return NULL;
896
Sakari Ailus859969b2016-08-26 20:17:25 -0300897 if (v4l2_fwnode_endpoint_parse(of_fwnode_handle(endpoint), &bus_cfg))
Javier Martinez Canillasbaf40b52016-01-11 14:47:14 -0200898 goto done;
899
Lad, Prabhakarc0d96442013-08-11 02:25:21 -0300900 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
901 if (!pdata)
902 goto done;
903
Lad, Prabhakarc0d96442013-08-11 02:25:21 -0300904 flags = bus_cfg.bus.parallel.flags;
905
906 if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
907 pdata->hs_polarity = 1;
908
909 if (flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
910 pdata->vs_polarity = 1;
911
912 if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
913 pdata->clk_polarity = 1;
914
915 if (flags & V4L2_MBUS_FIELD_EVEN_HIGH)
916 pdata->fid_polarity = 1;
917
918 if (flags & V4L2_MBUS_VIDEO_SOG_ACTIVE_HIGH)
919 pdata->sog_polarity = 1;
920
921done:
922 of_node_put(endpoint);
923 return pdata;
924}
925
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300926/*
927 * tvp7002_probe - Probe a TVP7002 device
Mats Randgaard6fa7dac2010-05-05 07:50:38 -0300928 * @c: ptr to i2c_client struct
929 * @id: ptr to i2c_device_id struct
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300930 *
931 * Initialize the TVP7002 device
932 * Returns zero when successful, -EINVAL if register read fails or
933 * -EIO if i2c access is not available.
934 */
Kieran Binghame6714992019-07-10 18:51:49 -0300935static int tvp7002_probe(struct i2c_client *c)
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300936{
Lad, Prabhakarc0d96442013-08-11 02:25:21 -0300937 struct tvp7002_config *pdata = tvp7002_get_pdata(c);
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300938 struct v4l2_subdev *sd;
939 struct tvp7002 *device;
Hans Verkuilf96067a2013-02-15 14:46:40 -0300940 struct v4l2_dv_timings timings;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300941 int polarity_a;
942 int polarity_b;
943 u8 revision;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300944 int error;
945
Lad, Prabhakarc0d96442013-08-11 02:25:21 -0300946 if (pdata == NULL) {
947 dev_err(&c->dev, "No platform data\n");
948 return -EINVAL;
949 }
950
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300951 /* Check if the adapter supports the needed features */
952 if (!i2c_check_functionality(c->adapter,
953 I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
954 return -EIO;
955
Lad, Prabhakarf3e8e4f2013-01-03 09:46:43 -0300956 device = devm_kzalloc(&c->dev, sizeof(struct tvp7002), GFP_KERNEL);
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300957
958 if (!device)
959 return -ENOMEM;
960
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300961 sd = &device->sd;
Lad, Prabhakarc0d96442013-08-11 02:25:21 -0300962 device->pdata = pdata;
Hans Verkuilf0cd0152013-02-15 14:33:51 -0300963 device->current_timings = tvp7002_timings;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300964
965 /* Tell v4l2 the device is ready */
966 v4l2_i2c_subdev_init(sd, c, &tvp7002_ops);
967 v4l_info(c, "tvp7002 found @ 0x%02x (%s)\n",
968 c->addr, c->adapter->name);
969
970 error = tvp7002_read(sd, TVP7002_CHIP_REV, &revision);
971 if (error < 0)
Lad, Prabhakarf3e8e4f2013-01-03 09:46:43 -0300972 return error;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300973
974 /* Get revision number */
975 v4l2_info(sd, "Rev. %02x detected.\n", revision);
976 if (revision != 0x02)
977 v4l2_info(sd, "Unknown revision detected.\n");
978
979 /* Initializes TVP7002 to its default values */
980 error = tvp7002_write_inittab(sd, tvp7002_init_default);
981
982 if (error < 0)
Lad, Prabhakarf3e8e4f2013-01-03 09:46:43 -0300983 return error;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300984
985 /* Set polarity information after registers have been set */
986 polarity_a = 0x20 | device->pdata->hs_polarity << 5
987 | device->pdata->vs_polarity << 2;
988 error = tvp7002_write(sd, TVP7002_SYNC_CTL_1, polarity_a);
989 if (error < 0)
Lad, Prabhakarf3e8e4f2013-01-03 09:46:43 -0300990 return error;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300991
992 polarity_b = 0x01 | device->pdata->fid_polarity << 2
993 | device->pdata->sog_polarity << 1
994 | device->pdata->clk_polarity;
995 error = tvp7002_write(sd, TVP7002_MISC_CTL_3, polarity_b);
996 if (error < 0)
Lad, Prabhakarf3e8e4f2013-01-03 09:46:43 -0300997 return error;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -0300998
999 /* Set registers according to default video mode */
Hans Verkuilf96067a2013-02-15 14:46:40 -03001000 timings = device->current_timings->timings;
1001 error = tvp7002_s_dv_timings(sd, &timings);
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -03001002
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -03001003#if defined(CONFIG_MEDIA_CONTROLLER)
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -03001004 device->pad.flags = MEDIA_PAD_FL_SOURCE;
1005 device->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
Hans Verkuilca0fa5f2018-06-18 05:09:45 -04001006 device->sd.entity.function = MEDIA_ENT_F_ATV_DECODER;
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -03001007
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -02001008 error = media_entity_pads_init(&device->sd.entity, 1, &device->pad);
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -03001009 if (error < 0)
1010 return error;
1011#endif
1012
Hans Verkuil0eb73de2010-12-12 08:46:15 -03001013 v4l2_ctrl_handler_init(&device->hdl, 1);
1014 v4l2_ctrl_new_std(&device->hdl, &tvp7002_ctrl_ops,
1015 V4L2_CID_GAIN, 0, 255, 1, 0);
1016 sd->ctrl_handler = &device->hdl;
1017 if (device->hdl.error) {
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -03001018 error = device->hdl.error;
1019 goto error;
Hans Verkuil0eb73de2010-12-12 08:46:15 -03001020 }
1021 v4l2_ctrl_handler_setup(&device->hdl);
1022
Lad, Prabhakar25ba2c82013-06-22 13:44:14 -03001023 error = v4l2_async_register_subdev(&device->sd);
1024 if (error)
1025 goto error;
1026
Lad, Prabhakarf3e8e4f2013-01-03 09:46:43 -03001027 return 0;
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -03001028
1029error:
1030 v4l2_ctrl_handler_free(&device->hdl);
1031#if defined(CONFIG_MEDIA_CONTROLLER)
1032 media_entity_cleanup(&device->sd.entity);
1033#endif
1034 return error;
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -03001035}
1036
1037/*
1038 * tvp7002_remove - Remove TVP7002 device support
1039 * @c: ptr to i2c_client struct
1040 *
1041 * Reset the TVP7002 device
1042 * Returns zero.
1043 */
1044static int tvp7002_remove(struct i2c_client *c)
1045{
1046 struct v4l2_subdev *sd = i2c_get_clientdata(c);
1047 struct tvp7002 *device = to_tvp7002(sd);
1048
1049 v4l2_dbg(1, debug, sd, "Removing tvp7002 adapter"
1050 "on address 0x%x\n", c->addr);
Lad, Prabhakar25ba2c82013-06-22 13:44:14 -03001051 v4l2_async_unregister_subdev(&device->sd);
Lad, Prabhakar63eb2ca2013-05-03 04:17:19 -03001052#if defined(CONFIG_MEDIA_CONTROLLER)
1053 media_entity_cleanup(&device->sd.entity);
1054#endif
Hans Verkuil0eb73de2010-12-12 08:46:15 -03001055 v4l2_ctrl_handler_free(&device->hdl);
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -03001056 return 0;
1057}
1058
1059/* I2C Device ID table */
1060static const struct i2c_device_id tvp7002_id[] = {
1061 { "tvp7002", 0 },
1062 { }
1063};
1064MODULE_DEVICE_TABLE(i2c, tvp7002_id);
1065
Lad, Prabhakarc0d96442013-08-11 02:25:21 -03001066#if IS_ENABLED(CONFIG_OF)
1067static const struct of_device_id tvp7002_of_match[] = {
1068 { .compatible = "ti,tvp7002", },
1069 { /* sentinel */ },
1070};
1071MODULE_DEVICE_TABLE(of, tvp7002_of_match);
1072#endif
1073
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -03001074/* I2C driver data */
1075static struct i2c_driver tvp7002_driver = {
1076 .driver = {
Lad, Prabhakarc0d96442013-08-11 02:25:21 -03001077 .of_match_table = of_match_ptr(tvp7002_of_match),
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -03001078 .name = TVP7002_MODULE_NAME,
1079 },
Kieran Binghame6714992019-07-10 18:51:49 -03001080 .probe_new = tvp7002_probe,
Santiago Nunez-Corrales0b675532009-12-21 16:14:04 -03001081 .remove = tvp7002_remove,
1082 .id_table = tvp7002_id,
1083};
1084
Axel Linc6e8d862012-02-12 06:56:32 -03001085module_i2c_driver(tvp7002_driver);