xref: /openbmc/linux/drivers/media/i2c/adv7842.c (revision e9bf5137)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * adv7842 - Analog Devices ADV7842 video decoder driver
4  *
5  * Copyright 2013 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6  */
7 
8 /*
9  * References (c = chapter, p = page):
10  * REF_01 - Analog devices, ADV7842,
11  *		Register Settings Recommendations, Rev. 1.9, April 2011
12  * REF_02 - Analog devices, Software User Guide, UG-206,
13  *		ADV7842 I2C Register Maps, Rev. 0, November 2010
14  * REF_03 - Analog devices, Hardware User Guide, UG-214,
15  *		ADV7842 Fast Switching 2:1 HDMI 1.4 Receiver with 3D-Comb
16  *		Decoder and Digitizer , Rev. 0, January 2011
17  */
18 
19 
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/i2c.h>
24 #include <linux/delay.h>
25 #include <linux/videodev2.h>
26 #include <linux/workqueue.h>
27 #include <linux/v4l2-dv-timings.h>
28 #include <linux/hdmi.h>
29 #include <media/cec.h>
30 #include <media/v4l2-device.h>
31 #include <media/v4l2-event.h>
32 #include <media/v4l2-ctrls.h>
33 #include <media/v4l2-dv-timings.h>
34 #include <media/i2c/adv7842.h>
35 
36 static int debug;
37 module_param(debug, int, 0644);
38 MODULE_PARM_DESC(debug, "debug level (0-2)");
39 
40 MODULE_DESCRIPTION("Analog Devices ADV7842 video decoder driver");
41 MODULE_AUTHOR("Hans Verkuil <hans.verkuil@cisco.com>");
42 MODULE_AUTHOR("Martin Bugge <marbugge@cisco.com>");
43 MODULE_LICENSE("GPL");
44 
45 /* ADV7842 system clock frequency */
46 #define ADV7842_fsc (28636360)
47 
48 #define ADV7842_RGB_OUT					(1 << 1)
49 
50 #define ADV7842_OP_FORMAT_SEL_8BIT			(0 << 0)
51 #define ADV7842_OP_FORMAT_SEL_10BIT			(1 << 0)
52 #define ADV7842_OP_FORMAT_SEL_12BIT			(2 << 0)
53 
54 #define ADV7842_OP_MODE_SEL_SDR_422			(0 << 5)
55 #define ADV7842_OP_MODE_SEL_DDR_422			(1 << 5)
56 #define ADV7842_OP_MODE_SEL_SDR_444			(2 << 5)
57 #define ADV7842_OP_MODE_SEL_DDR_444			(3 << 5)
58 #define ADV7842_OP_MODE_SEL_SDR_422_2X			(4 << 5)
59 #define ADV7842_OP_MODE_SEL_ADI_CM			(5 << 5)
60 
61 #define ADV7842_OP_CH_SEL_GBR				(0 << 5)
62 #define ADV7842_OP_CH_SEL_GRB				(1 << 5)
63 #define ADV7842_OP_CH_SEL_BGR				(2 << 5)
64 #define ADV7842_OP_CH_SEL_RGB				(3 << 5)
65 #define ADV7842_OP_CH_SEL_BRG				(4 << 5)
66 #define ADV7842_OP_CH_SEL_RBG				(5 << 5)
67 
68 #define ADV7842_OP_SWAP_CB_CR				(1 << 0)
69 
70 #define ADV7842_MAX_ADDRS (3)
71 
72 /*
73 **********************************************************************
74 *
75 *  Arrays with configuration parameters for the ADV7842
76 *
77 **********************************************************************
78 */
79 
80 struct adv7842_format_info {
81 	u32 code;
82 	u8 op_ch_sel;
83 	bool rgb_out;
84 	bool swap_cb_cr;
85 	u8 op_format_sel;
86 };
87 
88 struct adv7842_state {
89 	struct adv7842_platform_data pdata;
90 	struct v4l2_subdev sd;
91 	struct media_pad pads[ADV7842_PAD_SOURCE + 1];
92 	struct v4l2_ctrl_handler hdl;
93 	enum adv7842_mode mode;
94 	struct v4l2_dv_timings timings;
95 	enum adv7842_vid_std_select vid_std_select;
96 
97 	const struct adv7842_format_info *format;
98 
99 	v4l2_std_id norm;
100 	struct {
101 		u8 edid[512];
102 		u32 blocks;
103 		u32 present;
104 	} hdmi_edid;
105 	struct {
106 		u8 edid[128];
107 		u32 blocks;
108 		u32 present;
109 	} vga_edid;
110 	struct v4l2_fract aspect_ratio;
111 	u32 rgb_quantization_range;
112 	bool is_cea_format;
113 	struct delayed_work delayed_work_enable_hotplug;
114 	bool restart_stdi_once;
115 	bool hdmi_port_a;
116 
117 	/* i2c clients */
118 	struct i2c_client *i2c_sdp_io;
119 	struct i2c_client *i2c_sdp;
120 	struct i2c_client *i2c_cp;
121 	struct i2c_client *i2c_vdp;
122 	struct i2c_client *i2c_afe;
123 	struct i2c_client *i2c_hdmi;
124 	struct i2c_client *i2c_repeater;
125 	struct i2c_client *i2c_edid;
126 	struct i2c_client *i2c_infoframe;
127 	struct i2c_client *i2c_cec;
128 	struct i2c_client *i2c_avlink;
129 
130 	/* controls */
131 	struct v4l2_ctrl *detect_tx_5v_ctrl;
132 	struct v4l2_ctrl *analog_sampling_phase_ctrl;
133 	struct v4l2_ctrl *free_run_color_ctrl_manual;
134 	struct v4l2_ctrl *free_run_color_ctrl;
135 	struct v4l2_ctrl *rgb_quantization_range_ctrl;
136 
137 	struct cec_adapter *cec_adap;
138 	u8   cec_addr[ADV7842_MAX_ADDRS];
139 	u8   cec_valid_addrs;
140 	bool cec_enabled_adap;
141 };
142 
143 /* Unsupported timings. This device cannot support 720p30. */
144 static const struct v4l2_dv_timings adv7842_timings_exceptions[] = {
145 	V4L2_DV_BT_CEA_1280X720P30,
146 	{ }
147 };
148 
adv7842_check_dv_timings(const struct v4l2_dv_timings * t,void * hdl)149 static bool adv7842_check_dv_timings(const struct v4l2_dv_timings *t, void *hdl)
150 {
151 	int i;
152 
153 	for (i = 0; adv7842_timings_exceptions[i].bt.width; i++)
154 		if (v4l2_match_dv_timings(t, adv7842_timings_exceptions + i, 0, false))
155 			return false;
156 	return true;
157 }
158 
159 struct adv7842_video_standards {
160 	struct v4l2_dv_timings timings;
161 	u8 vid_std;
162 	u8 v_freq;
163 };
164 
165 /* sorted by number of lines */
166 static const struct adv7842_video_standards adv7842_prim_mode_comp[] = {
167 	/* { V4L2_DV_BT_CEA_720X480P59_94, 0x0a, 0x00 }, TODO flickering */
168 	{ V4L2_DV_BT_CEA_720X576P50, 0x0b, 0x00 },
169 	{ V4L2_DV_BT_CEA_1280X720P50, 0x19, 0x01 },
170 	{ V4L2_DV_BT_CEA_1280X720P60, 0x19, 0x00 },
171 	{ V4L2_DV_BT_CEA_1920X1080P24, 0x1e, 0x04 },
172 	{ V4L2_DV_BT_CEA_1920X1080P25, 0x1e, 0x03 },
173 	{ V4L2_DV_BT_CEA_1920X1080P30, 0x1e, 0x02 },
174 	{ V4L2_DV_BT_CEA_1920X1080P50, 0x1e, 0x01 },
175 	{ V4L2_DV_BT_CEA_1920X1080P60, 0x1e, 0x00 },
176 	/* TODO add 1920x1080P60_RB (CVT timing) */
177 	{ },
178 };
179 
180 /* sorted by number of lines */
181 static const struct adv7842_video_standards adv7842_prim_mode_gr[] = {
182 	{ V4L2_DV_BT_DMT_640X480P60, 0x08, 0x00 },
183 	{ V4L2_DV_BT_DMT_640X480P72, 0x09, 0x00 },
184 	{ V4L2_DV_BT_DMT_640X480P75, 0x0a, 0x00 },
185 	{ V4L2_DV_BT_DMT_640X480P85, 0x0b, 0x00 },
186 	{ V4L2_DV_BT_DMT_800X600P56, 0x00, 0x00 },
187 	{ V4L2_DV_BT_DMT_800X600P60, 0x01, 0x00 },
188 	{ V4L2_DV_BT_DMT_800X600P72, 0x02, 0x00 },
189 	{ V4L2_DV_BT_DMT_800X600P75, 0x03, 0x00 },
190 	{ V4L2_DV_BT_DMT_800X600P85, 0x04, 0x00 },
191 	{ V4L2_DV_BT_DMT_1024X768P60, 0x0c, 0x00 },
192 	{ V4L2_DV_BT_DMT_1024X768P70, 0x0d, 0x00 },
193 	{ V4L2_DV_BT_DMT_1024X768P75, 0x0e, 0x00 },
194 	{ V4L2_DV_BT_DMT_1024X768P85, 0x0f, 0x00 },
195 	{ V4L2_DV_BT_DMT_1280X1024P60, 0x05, 0x00 },
196 	{ V4L2_DV_BT_DMT_1280X1024P75, 0x06, 0x00 },
197 	{ V4L2_DV_BT_DMT_1360X768P60, 0x12, 0x00 },
198 	{ V4L2_DV_BT_DMT_1366X768P60, 0x13, 0x00 },
199 	{ V4L2_DV_BT_DMT_1400X1050P60, 0x14, 0x00 },
200 	{ V4L2_DV_BT_DMT_1400X1050P75, 0x15, 0x00 },
201 	{ V4L2_DV_BT_DMT_1600X1200P60, 0x16, 0x00 }, /* TODO not tested */
202 	/* TODO add 1600X1200P60_RB (not a DMT timing) */
203 	{ V4L2_DV_BT_DMT_1680X1050P60, 0x18, 0x00 },
204 	{ V4L2_DV_BT_DMT_1920X1200P60_RB, 0x19, 0x00 }, /* TODO not tested */
205 	{ },
206 };
207 
208 /* sorted by number of lines */
209 static const struct adv7842_video_standards adv7842_prim_mode_hdmi_comp[] = {
210 	{ V4L2_DV_BT_CEA_720X480P59_94, 0x0a, 0x00 },
211 	{ V4L2_DV_BT_CEA_720X576P50, 0x0b, 0x00 },
212 	{ V4L2_DV_BT_CEA_1280X720P50, 0x13, 0x01 },
213 	{ V4L2_DV_BT_CEA_1280X720P60, 0x13, 0x00 },
214 	{ V4L2_DV_BT_CEA_1920X1080P24, 0x1e, 0x04 },
215 	{ V4L2_DV_BT_CEA_1920X1080P25, 0x1e, 0x03 },
216 	{ V4L2_DV_BT_CEA_1920X1080P30, 0x1e, 0x02 },
217 	{ V4L2_DV_BT_CEA_1920X1080P50, 0x1e, 0x01 },
218 	{ V4L2_DV_BT_CEA_1920X1080P60, 0x1e, 0x00 },
219 	{ },
220 };
221 
222 /* sorted by number of lines */
223 static const struct adv7842_video_standards adv7842_prim_mode_hdmi_gr[] = {
224 	{ V4L2_DV_BT_DMT_640X480P60, 0x08, 0x00 },
225 	{ V4L2_DV_BT_DMT_640X480P72, 0x09, 0x00 },
226 	{ V4L2_DV_BT_DMT_640X480P75, 0x0a, 0x00 },
227 	{ V4L2_DV_BT_DMT_640X480P85, 0x0b, 0x00 },
228 	{ V4L2_DV_BT_DMT_800X600P56, 0x00, 0x00 },
229 	{ V4L2_DV_BT_DMT_800X600P60, 0x01, 0x00 },
230 	{ V4L2_DV_BT_DMT_800X600P72, 0x02, 0x00 },
231 	{ V4L2_DV_BT_DMT_800X600P75, 0x03, 0x00 },
232 	{ V4L2_DV_BT_DMT_800X600P85, 0x04, 0x00 },
233 	{ V4L2_DV_BT_DMT_1024X768P60, 0x0c, 0x00 },
234 	{ V4L2_DV_BT_DMT_1024X768P70, 0x0d, 0x00 },
235 	{ V4L2_DV_BT_DMT_1024X768P75, 0x0e, 0x00 },
236 	{ V4L2_DV_BT_DMT_1024X768P85, 0x0f, 0x00 },
237 	{ V4L2_DV_BT_DMT_1280X1024P60, 0x05, 0x00 },
238 	{ V4L2_DV_BT_DMT_1280X1024P75, 0x06, 0x00 },
239 	{ },
240 };
241 
242 static const struct v4l2_event adv7842_ev_fmt = {
243 	.type = V4L2_EVENT_SOURCE_CHANGE,
244 	.u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
245 };
246 
247 /* ----------------------------------------------------------------------- */
248 
to_state(struct v4l2_subdev * sd)249 static inline struct adv7842_state *to_state(struct v4l2_subdev *sd)
250 {
251 	return container_of(sd, struct adv7842_state, sd);
252 }
253 
to_sd(struct v4l2_ctrl * ctrl)254 static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
255 {
256 	return &container_of(ctrl->handler, struct adv7842_state, hdl)->sd;
257 }
258 
htotal(const struct v4l2_bt_timings * t)259 static inline unsigned htotal(const struct v4l2_bt_timings *t)
260 {
261 	return V4L2_DV_BT_FRAME_WIDTH(t);
262 }
263 
vtotal(const struct v4l2_bt_timings * t)264 static inline unsigned vtotal(const struct v4l2_bt_timings *t)
265 {
266 	return V4L2_DV_BT_FRAME_HEIGHT(t);
267 }
268 
269 
270 /* ----------------------------------------------------------------------- */
271 
adv_smbus_read_byte_data_check(struct i2c_client * client,u8 command,bool check)272 static s32 adv_smbus_read_byte_data_check(struct i2c_client *client,
273 					  u8 command, bool check)
274 {
275 	union i2c_smbus_data data;
276 
277 	if (!i2c_smbus_xfer(client->adapter, client->addr, client->flags,
278 			    I2C_SMBUS_READ, command,
279 			    I2C_SMBUS_BYTE_DATA, &data))
280 		return data.byte;
281 	if (check)
282 		v4l_err(client, "error reading %02x, %02x\n",
283 			client->addr, command);
284 	return -EIO;
285 }
286 
adv_smbus_read_byte_data(struct i2c_client * client,u8 command)287 static s32 adv_smbus_read_byte_data(struct i2c_client *client, u8 command)
288 {
289 	int i;
290 
291 	for (i = 0; i < 3; i++) {
292 		int ret = adv_smbus_read_byte_data_check(client, command, true);
293 
294 		if (ret >= 0) {
295 			if (i)
296 				v4l_err(client, "read ok after %d retries\n", i);
297 			return ret;
298 		}
299 	}
300 	v4l_err(client, "read failed\n");
301 	return -EIO;
302 }
303 
adv_smbus_write_byte_data(struct i2c_client * client,u8 command,u8 value)304 static s32 adv_smbus_write_byte_data(struct i2c_client *client,
305 				     u8 command, u8 value)
306 {
307 	union i2c_smbus_data data;
308 	int err;
309 	int i;
310 
311 	data.byte = value;
312 	for (i = 0; i < 3; i++) {
313 		err = i2c_smbus_xfer(client->adapter, client->addr,
314 				     client->flags,
315 				     I2C_SMBUS_WRITE, command,
316 				     I2C_SMBUS_BYTE_DATA, &data);
317 		if (!err)
318 			break;
319 	}
320 	if (err < 0)
321 		v4l_err(client, "error writing %02x, %02x, %02x\n",
322 			client->addr, command, value);
323 	return err;
324 }
325 
adv_smbus_write_byte_no_check(struct i2c_client * client,u8 command,u8 value)326 static void adv_smbus_write_byte_no_check(struct i2c_client *client,
327 					  u8 command, u8 value)
328 {
329 	union i2c_smbus_data data;
330 	data.byte = value;
331 
332 	i2c_smbus_xfer(client->adapter, client->addr,
333 		       client->flags,
334 		       I2C_SMBUS_WRITE, command,
335 		       I2C_SMBUS_BYTE_DATA, &data);
336 }
337 
338 /* ----------------------------------------------------------------------- */
339 
io_read(struct v4l2_subdev * sd,u8 reg)340 static inline int io_read(struct v4l2_subdev *sd, u8 reg)
341 {
342 	struct i2c_client *client = v4l2_get_subdevdata(sd);
343 
344 	return adv_smbus_read_byte_data(client, reg);
345 }
346 
io_write(struct v4l2_subdev * sd,u8 reg,u8 val)347 static inline int io_write(struct v4l2_subdev *sd, u8 reg, u8 val)
348 {
349 	struct i2c_client *client = v4l2_get_subdevdata(sd);
350 
351 	return adv_smbus_write_byte_data(client, reg, val);
352 }
353 
io_write_and_or(struct v4l2_subdev * sd,u8 reg,u8 mask,u8 val)354 static inline int io_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
355 {
356 	return io_write(sd, reg, (io_read(sd, reg) & mask) | val);
357 }
358 
io_write_clr_set(struct v4l2_subdev * sd,u8 reg,u8 mask,u8 val)359 static inline int io_write_clr_set(struct v4l2_subdev *sd,
360 				   u8 reg, u8 mask, u8 val)
361 {
362 	return io_write(sd, reg, (io_read(sd, reg) & ~mask) | val);
363 }
364 
avlink_read(struct v4l2_subdev * sd,u8 reg)365 static inline int avlink_read(struct v4l2_subdev *sd, u8 reg)
366 {
367 	struct adv7842_state *state = to_state(sd);
368 
369 	return adv_smbus_read_byte_data(state->i2c_avlink, reg);
370 }
371 
avlink_write(struct v4l2_subdev * sd,u8 reg,u8 val)372 static inline int avlink_write(struct v4l2_subdev *sd, u8 reg, u8 val)
373 {
374 	struct adv7842_state *state = to_state(sd);
375 
376 	return adv_smbus_write_byte_data(state->i2c_avlink, reg, val);
377 }
378 
cec_read(struct v4l2_subdev * sd,u8 reg)379 static inline int cec_read(struct v4l2_subdev *sd, u8 reg)
380 {
381 	struct adv7842_state *state = to_state(sd);
382 
383 	return adv_smbus_read_byte_data(state->i2c_cec, reg);
384 }
385 
cec_write(struct v4l2_subdev * sd,u8 reg,u8 val)386 static inline int cec_write(struct v4l2_subdev *sd, u8 reg, u8 val)
387 {
388 	struct adv7842_state *state = to_state(sd);
389 
390 	return adv_smbus_write_byte_data(state->i2c_cec, reg, val);
391 }
392 
cec_write_clr_set(struct v4l2_subdev * sd,u8 reg,u8 mask,u8 val)393 static inline int cec_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
394 {
395 	return cec_write(sd, reg, (cec_read(sd, reg) & ~mask) | val);
396 }
397 
infoframe_read(struct v4l2_subdev * sd,u8 reg)398 static inline int infoframe_read(struct v4l2_subdev *sd, u8 reg)
399 {
400 	struct adv7842_state *state = to_state(sd);
401 
402 	return adv_smbus_read_byte_data(state->i2c_infoframe, reg);
403 }
404 
infoframe_write(struct v4l2_subdev * sd,u8 reg,u8 val)405 static inline int infoframe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
406 {
407 	struct adv7842_state *state = to_state(sd);
408 
409 	return adv_smbus_write_byte_data(state->i2c_infoframe, reg, val);
410 }
411 
sdp_io_read(struct v4l2_subdev * sd,u8 reg)412 static inline int sdp_io_read(struct v4l2_subdev *sd, u8 reg)
413 {
414 	struct adv7842_state *state = to_state(sd);
415 
416 	return adv_smbus_read_byte_data(state->i2c_sdp_io, reg);
417 }
418 
sdp_io_write(struct v4l2_subdev * sd,u8 reg,u8 val)419 static inline int sdp_io_write(struct v4l2_subdev *sd, u8 reg, u8 val)
420 {
421 	struct adv7842_state *state = to_state(sd);
422 
423 	return adv_smbus_write_byte_data(state->i2c_sdp_io, reg, val);
424 }
425 
sdp_io_write_and_or(struct v4l2_subdev * sd,u8 reg,u8 mask,u8 val)426 static inline int sdp_io_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
427 {
428 	return sdp_io_write(sd, reg, (sdp_io_read(sd, reg) & mask) | val);
429 }
430 
sdp_read(struct v4l2_subdev * sd,u8 reg)431 static inline int sdp_read(struct v4l2_subdev *sd, u8 reg)
432 {
433 	struct adv7842_state *state = to_state(sd);
434 
435 	return adv_smbus_read_byte_data(state->i2c_sdp, reg);
436 }
437 
sdp_write(struct v4l2_subdev * sd,u8 reg,u8 val)438 static inline int sdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
439 {
440 	struct adv7842_state *state = to_state(sd);
441 
442 	return adv_smbus_write_byte_data(state->i2c_sdp, reg, val);
443 }
444 
sdp_write_and_or(struct v4l2_subdev * sd,u8 reg,u8 mask,u8 val)445 static inline int sdp_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
446 {
447 	return sdp_write(sd, reg, (sdp_read(sd, reg) & mask) | val);
448 }
449 
afe_read(struct v4l2_subdev * sd,u8 reg)450 static inline int afe_read(struct v4l2_subdev *sd, u8 reg)
451 {
452 	struct adv7842_state *state = to_state(sd);
453 
454 	return adv_smbus_read_byte_data(state->i2c_afe, reg);
455 }
456 
afe_write(struct v4l2_subdev * sd,u8 reg,u8 val)457 static inline int afe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
458 {
459 	struct adv7842_state *state = to_state(sd);
460 
461 	return adv_smbus_write_byte_data(state->i2c_afe, reg, val);
462 }
463 
afe_write_and_or(struct v4l2_subdev * sd,u8 reg,u8 mask,u8 val)464 static inline int afe_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
465 {
466 	return afe_write(sd, reg, (afe_read(sd, reg) & mask) | val);
467 }
468 
rep_read(struct v4l2_subdev * sd,u8 reg)469 static inline int rep_read(struct v4l2_subdev *sd, u8 reg)
470 {
471 	struct adv7842_state *state = to_state(sd);
472 
473 	return adv_smbus_read_byte_data(state->i2c_repeater, reg);
474 }
475 
rep_write(struct v4l2_subdev * sd,u8 reg,u8 val)476 static inline int rep_write(struct v4l2_subdev *sd, u8 reg, u8 val)
477 {
478 	struct adv7842_state *state = to_state(sd);
479 
480 	return adv_smbus_write_byte_data(state->i2c_repeater, reg, val);
481 }
482 
rep_write_and_or(struct v4l2_subdev * sd,u8 reg,u8 mask,u8 val)483 static inline int rep_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
484 {
485 	return rep_write(sd, reg, (rep_read(sd, reg) & mask) | val);
486 }
487 
edid_read(struct v4l2_subdev * sd,u8 reg)488 static inline int edid_read(struct v4l2_subdev *sd, u8 reg)
489 {
490 	struct adv7842_state *state = to_state(sd);
491 
492 	return adv_smbus_read_byte_data(state->i2c_edid, reg);
493 }
494 
edid_write(struct v4l2_subdev * sd,u8 reg,u8 val)495 static inline int edid_write(struct v4l2_subdev *sd, u8 reg, u8 val)
496 {
497 	struct adv7842_state *state = to_state(sd);
498 
499 	return adv_smbus_write_byte_data(state->i2c_edid, reg, val);
500 }
501 
hdmi_read(struct v4l2_subdev * sd,u8 reg)502 static inline int hdmi_read(struct v4l2_subdev *sd, u8 reg)
503 {
504 	struct adv7842_state *state = to_state(sd);
505 
506 	return adv_smbus_read_byte_data(state->i2c_hdmi, reg);
507 }
508 
hdmi_write(struct v4l2_subdev * sd,u8 reg,u8 val)509 static inline int hdmi_write(struct v4l2_subdev *sd, u8 reg, u8 val)
510 {
511 	struct adv7842_state *state = to_state(sd);
512 
513 	return adv_smbus_write_byte_data(state->i2c_hdmi, reg, val);
514 }
515 
hdmi_write_and_or(struct v4l2_subdev * sd,u8 reg,u8 mask,u8 val)516 static inline int hdmi_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
517 {
518 	return hdmi_write(sd, reg, (hdmi_read(sd, reg) & mask) | val);
519 }
520 
cp_read(struct v4l2_subdev * sd,u8 reg)521 static inline int cp_read(struct v4l2_subdev *sd, u8 reg)
522 {
523 	struct adv7842_state *state = to_state(sd);
524 
525 	return adv_smbus_read_byte_data(state->i2c_cp, reg);
526 }
527 
cp_write(struct v4l2_subdev * sd,u8 reg,u8 val)528 static inline int cp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
529 {
530 	struct adv7842_state *state = to_state(sd);
531 
532 	return adv_smbus_write_byte_data(state->i2c_cp, reg, val);
533 }
534 
cp_write_and_or(struct v4l2_subdev * sd,u8 reg,u8 mask,u8 val)535 static inline int cp_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
536 {
537 	return cp_write(sd, reg, (cp_read(sd, reg) & mask) | val);
538 }
539 
vdp_read(struct v4l2_subdev * sd,u8 reg)540 static inline int vdp_read(struct v4l2_subdev *sd, u8 reg)
541 {
542 	struct adv7842_state *state = to_state(sd);
543 
544 	return adv_smbus_read_byte_data(state->i2c_vdp, reg);
545 }
546 
vdp_write(struct v4l2_subdev * sd,u8 reg,u8 val)547 static inline int vdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
548 {
549 	struct adv7842_state *state = to_state(sd);
550 
551 	return adv_smbus_write_byte_data(state->i2c_vdp, reg, val);
552 }
553 
main_reset(struct v4l2_subdev * sd)554 static void main_reset(struct v4l2_subdev *sd)
555 {
556 	struct i2c_client *client = v4l2_get_subdevdata(sd);
557 
558 	v4l2_dbg(1, debug, sd, "%s:\n", __func__);
559 
560 	adv_smbus_write_byte_no_check(client, 0xff, 0x80);
561 
562 	mdelay(5);
563 }
564 
565 /* -----------------------------------------------------------------------------
566  * Format helpers
567  */
568 
569 static const struct adv7842_format_info adv7842_formats[] = {
570 	{ MEDIA_BUS_FMT_RGB888_1X24, ADV7842_OP_CH_SEL_RGB, true, false,
571 	  ADV7842_OP_MODE_SEL_SDR_444 | ADV7842_OP_FORMAT_SEL_8BIT },
572 	{ MEDIA_BUS_FMT_YUYV8_2X8, ADV7842_OP_CH_SEL_RGB, false, false,
573 	  ADV7842_OP_MODE_SEL_SDR_422 | ADV7842_OP_FORMAT_SEL_8BIT },
574 	{ MEDIA_BUS_FMT_YVYU8_2X8, ADV7842_OP_CH_SEL_RGB, false, true,
575 	  ADV7842_OP_MODE_SEL_SDR_422 | ADV7842_OP_FORMAT_SEL_8BIT },
576 	{ MEDIA_BUS_FMT_YUYV10_2X10, ADV7842_OP_CH_SEL_RGB, false, false,
577 	  ADV7842_OP_MODE_SEL_SDR_422 | ADV7842_OP_FORMAT_SEL_10BIT },
578 	{ MEDIA_BUS_FMT_YVYU10_2X10, ADV7842_OP_CH_SEL_RGB, false, true,
579 	  ADV7842_OP_MODE_SEL_SDR_422 | ADV7842_OP_FORMAT_SEL_10BIT },
580 	{ MEDIA_BUS_FMT_YUYV12_2X12, ADV7842_OP_CH_SEL_RGB, false, false,
581 	  ADV7842_OP_MODE_SEL_SDR_422 | ADV7842_OP_FORMAT_SEL_12BIT },
582 	{ MEDIA_BUS_FMT_YVYU12_2X12, ADV7842_OP_CH_SEL_RGB, false, true,
583 	  ADV7842_OP_MODE_SEL_SDR_422 | ADV7842_OP_FORMAT_SEL_12BIT },
584 	{ MEDIA_BUS_FMT_UYVY8_1X16, ADV7842_OP_CH_SEL_RBG, false, false,
585 	  ADV7842_OP_MODE_SEL_SDR_422_2X | ADV7842_OP_FORMAT_SEL_8BIT },
586 	{ MEDIA_BUS_FMT_VYUY8_1X16, ADV7842_OP_CH_SEL_RBG, false, true,
587 	  ADV7842_OP_MODE_SEL_SDR_422_2X | ADV7842_OP_FORMAT_SEL_8BIT },
588 	{ MEDIA_BUS_FMT_YUYV8_1X16, ADV7842_OP_CH_SEL_RGB, false, false,
589 	  ADV7842_OP_MODE_SEL_SDR_422_2X | ADV7842_OP_FORMAT_SEL_8BIT },
590 	{ MEDIA_BUS_FMT_YVYU8_1X16, ADV7842_OP_CH_SEL_RGB, false, true,
591 	  ADV7842_OP_MODE_SEL_SDR_422_2X | ADV7842_OP_FORMAT_SEL_8BIT },
592 	{ MEDIA_BUS_FMT_UYVY10_1X20, ADV7842_OP_CH_SEL_RBG, false, false,
593 	  ADV7842_OP_MODE_SEL_SDR_422_2X | ADV7842_OP_FORMAT_SEL_10BIT },
594 	{ MEDIA_BUS_FMT_VYUY10_1X20, ADV7842_OP_CH_SEL_RBG, false, true,
595 	  ADV7842_OP_MODE_SEL_SDR_422_2X | ADV7842_OP_FORMAT_SEL_10BIT },
596 	{ MEDIA_BUS_FMT_YUYV10_1X20, ADV7842_OP_CH_SEL_RGB, false, false,
597 	  ADV7842_OP_MODE_SEL_SDR_422_2X | ADV7842_OP_FORMAT_SEL_10BIT },
598 	{ MEDIA_BUS_FMT_YVYU10_1X20, ADV7842_OP_CH_SEL_RGB, false, true,
599 	  ADV7842_OP_MODE_SEL_SDR_422_2X | ADV7842_OP_FORMAT_SEL_10BIT },
600 	{ MEDIA_BUS_FMT_UYVY12_1X24, ADV7842_OP_CH_SEL_RBG, false, false,
601 	  ADV7842_OP_MODE_SEL_SDR_422_2X | ADV7842_OP_FORMAT_SEL_12BIT },
602 	{ MEDIA_BUS_FMT_VYUY12_1X24, ADV7842_OP_CH_SEL_RBG, false, true,
603 	  ADV7842_OP_MODE_SEL_SDR_422_2X | ADV7842_OP_FORMAT_SEL_12BIT },
604 	{ MEDIA_BUS_FMT_YUYV12_1X24, ADV7842_OP_CH_SEL_RGB, false, false,
605 	  ADV7842_OP_MODE_SEL_SDR_422_2X | ADV7842_OP_FORMAT_SEL_12BIT },
606 	{ MEDIA_BUS_FMT_YVYU12_1X24, ADV7842_OP_CH_SEL_RGB, false, true,
607 	  ADV7842_OP_MODE_SEL_SDR_422_2X | ADV7842_OP_FORMAT_SEL_12BIT },
608 };
609 
610 static const struct adv7842_format_info *
adv7842_format_info(struct adv7842_state * state,u32 code)611 adv7842_format_info(struct adv7842_state *state, u32 code)
612 {
613 	unsigned int i;
614 
615 	for (i = 0; i < ARRAY_SIZE(adv7842_formats); ++i) {
616 		if (adv7842_formats[i].code == code)
617 			return &adv7842_formats[i];
618 	}
619 
620 	return NULL;
621 }
622 
623 /* ----------------------------------------------------------------------- */
624 
is_analog_input(struct v4l2_subdev * sd)625 static inline bool is_analog_input(struct v4l2_subdev *sd)
626 {
627 	struct adv7842_state *state = to_state(sd);
628 
629 	return ((state->mode == ADV7842_MODE_RGB) ||
630 		(state->mode == ADV7842_MODE_COMP));
631 }
632 
is_digital_input(struct v4l2_subdev * sd)633 static inline bool is_digital_input(struct v4l2_subdev *sd)
634 {
635 	struct adv7842_state *state = to_state(sd);
636 
637 	return state->mode == ADV7842_MODE_HDMI;
638 }
639 
640 static const struct v4l2_dv_timings_cap adv7842_timings_cap_analog = {
641 	.type = V4L2_DV_BT_656_1120,
642 	/* keep this initialization for compatibility with GCC < 4.4.6 */
643 	.reserved = { 0 },
644 	V4L2_INIT_BT_TIMINGS(640, 1920, 350, 1200, 25000000, 170000000,
645 		V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
646 			V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT,
647 		V4L2_DV_BT_CAP_PROGRESSIVE | V4L2_DV_BT_CAP_REDUCED_BLANKING |
648 			V4L2_DV_BT_CAP_CUSTOM)
649 };
650 
651 static const struct v4l2_dv_timings_cap adv7842_timings_cap_digital = {
652 	.type = V4L2_DV_BT_656_1120,
653 	/* keep this initialization for compatibility with GCC < 4.4.6 */
654 	.reserved = { 0 },
655 	V4L2_INIT_BT_TIMINGS(640, 1920, 350, 1200, 25000000, 225000000,
656 		V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
657 			V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT,
658 		V4L2_DV_BT_CAP_PROGRESSIVE | V4L2_DV_BT_CAP_REDUCED_BLANKING |
659 			V4L2_DV_BT_CAP_CUSTOM)
660 };
661 
662 static inline const struct v4l2_dv_timings_cap *
adv7842_get_dv_timings_cap(struct v4l2_subdev * sd)663 adv7842_get_dv_timings_cap(struct v4l2_subdev *sd)
664 {
665 	return is_digital_input(sd) ? &adv7842_timings_cap_digital :
666 				      &adv7842_timings_cap_analog;
667 }
668 
669 /* ----------------------------------------------------------------------- */
670 
adv7842_read_cable_det(struct v4l2_subdev * sd)671 static u16 adv7842_read_cable_det(struct v4l2_subdev *sd)
672 {
673 	u8 reg = io_read(sd, 0x6f);
674 	u16 val = 0;
675 
676 	if (reg & 0x02)
677 		val |= 1; /* port A */
678 	if (reg & 0x01)
679 		val |= 2; /* port B */
680 	return val;
681 }
682 
adv7842_delayed_work_enable_hotplug(struct work_struct * work)683 static void adv7842_delayed_work_enable_hotplug(struct work_struct *work)
684 {
685 	struct delayed_work *dwork = to_delayed_work(work);
686 	struct adv7842_state *state = container_of(dwork,
687 			struct adv7842_state, delayed_work_enable_hotplug);
688 	struct v4l2_subdev *sd = &state->sd;
689 	int present = state->hdmi_edid.present;
690 	u8 mask = 0;
691 
692 	v4l2_dbg(2, debug, sd, "%s: enable hotplug on ports: 0x%x\n",
693 			__func__, present);
694 
695 	if (present & (0x04 << ADV7842_EDID_PORT_A))
696 		mask |= 0x20;
697 	if (present & (0x04 << ADV7842_EDID_PORT_B))
698 		mask |= 0x10;
699 	io_write_and_or(sd, 0x20, 0xcf, mask);
700 }
701 
edid_write_vga_segment(struct v4l2_subdev * sd)702 static int edid_write_vga_segment(struct v4l2_subdev *sd)
703 {
704 	struct i2c_client *client = v4l2_get_subdevdata(sd);
705 	struct adv7842_state *state = to_state(sd);
706 	const u8 *edid = state->vga_edid.edid;
707 	u32 blocks = state->vga_edid.blocks;
708 	int err = 0;
709 	int i;
710 
711 	v4l2_dbg(2, debug, sd, "%s: write EDID on VGA port\n", __func__);
712 
713 	if (!state->vga_edid.present)
714 		return 0;
715 
716 	/* HPA disable on port A and B */
717 	io_write_and_or(sd, 0x20, 0xcf, 0x00);
718 
719 	/* Disable I2C access to internal EDID ram from VGA DDC port */
720 	rep_write_and_or(sd, 0x7f, 0x7f, 0x00);
721 
722 	/* edid segment pointer '1' for VGA port */
723 	rep_write_and_or(sd, 0x77, 0xef, 0x10);
724 
725 	for (i = 0; !err && i < blocks * 128; i += I2C_SMBUS_BLOCK_MAX)
726 		err = i2c_smbus_write_i2c_block_data(state->i2c_edid, i,
727 						     I2C_SMBUS_BLOCK_MAX,
728 						     edid + i);
729 	if (err)
730 		return err;
731 
732 	/* Calculates the checksums and enables I2C access
733 	 * to internal EDID ram from VGA DDC port.
734 	 */
735 	rep_write_and_or(sd, 0x7f, 0x7f, 0x80);
736 
737 	for (i = 0; i < 1000; i++) {
738 		if (rep_read(sd, 0x79) & 0x20)
739 			break;
740 		mdelay(1);
741 	}
742 	if (i == 1000) {
743 		v4l_err(client, "error enabling edid on VGA port\n");
744 		return -EIO;
745 	}
746 
747 	/* enable hotplug after 200 ms */
748 	schedule_delayed_work(&state->delayed_work_enable_hotplug, HZ / 5);
749 
750 	return 0;
751 }
752 
edid_write_hdmi_segment(struct v4l2_subdev * sd,u8 port)753 static int edid_write_hdmi_segment(struct v4l2_subdev *sd, u8 port)
754 {
755 	struct i2c_client *client = v4l2_get_subdevdata(sd);
756 	struct adv7842_state *state = to_state(sd);
757 	const u8 *edid = state->hdmi_edid.edid;
758 	u32 blocks = state->hdmi_edid.blocks;
759 	unsigned int spa_loc;
760 	u16 pa, parent_pa;
761 	int err = 0;
762 	int i;
763 
764 	v4l2_dbg(2, debug, sd, "%s: write EDID on port %c\n",
765 			__func__, (port == ADV7842_EDID_PORT_A) ? 'A' : 'B');
766 
767 	/* HPA disable on port A and B */
768 	io_write_and_or(sd, 0x20, 0xcf, 0x00);
769 
770 	/* Disable I2C access to internal EDID ram from HDMI DDC ports */
771 	rep_write_and_or(sd, 0x77, 0xf3, 0x00);
772 
773 	if (!state->hdmi_edid.present) {
774 		cec_phys_addr_invalidate(state->cec_adap);
775 		return 0;
776 	}
777 
778 	pa = v4l2_get_edid_phys_addr(edid, blocks * 128, &spa_loc);
779 	err = v4l2_phys_addr_validate(pa, &parent_pa, NULL);
780 	if (err)
781 		return err;
782 
783 	if (!spa_loc) {
784 		/*
785 		 * There is no SPA, so just set spa_loc to 128 and pa to whatever
786 		 * data is there.
787 		 */
788 		spa_loc = 128;
789 		pa = (edid[spa_loc] << 8) | edid[spa_loc + 1];
790 	}
791 
792 
793 	for (i = 0; !err && i < blocks * 128; i += I2C_SMBUS_BLOCK_MAX) {
794 		/* set edid segment pointer for HDMI ports */
795 		if (i % 256 == 0)
796 			rep_write_and_or(sd, 0x77, 0xef, i >= 256 ? 0x10 : 0x00);
797 		err = i2c_smbus_write_i2c_block_data(state->i2c_edid, i,
798 						     I2C_SMBUS_BLOCK_MAX, edid + i);
799 	}
800 	if (err)
801 		return err;
802 
803 	if (port == ADV7842_EDID_PORT_A) {
804 		rep_write(sd, 0x72, pa >> 8);
805 		rep_write(sd, 0x73, pa & 0xff);
806 	} else {
807 		rep_write(sd, 0x74, pa >> 8);
808 		rep_write(sd, 0x75, pa & 0xff);
809 	}
810 	rep_write(sd, 0x76, spa_loc & 0xff);
811 	rep_write_and_or(sd, 0x77, 0xbf, (spa_loc >> 2) & 0x40);
812 
813 	/* Calculates the checksums and enables I2C access to internal
814 	 * EDID ram from HDMI DDC ports
815 	 */
816 	rep_write_and_or(sd, 0x77, 0xf3, state->hdmi_edid.present);
817 
818 	for (i = 0; i < 1000; i++) {
819 		if (rep_read(sd, 0x7d) & state->hdmi_edid.present)
820 			break;
821 		mdelay(1);
822 	}
823 	if (i == 1000) {
824 		v4l_err(client, "error enabling edid on port %c\n",
825 				(port == ADV7842_EDID_PORT_A) ? 'A' : 'B');
826 		return -EIO;
827 	}
828 	cec_s_phys_addr(state->cec_adap, parent_pa, false);
829 
830 	/* enable hotplug after 200 ms */
831 	schedule_delayed_work(&state->delayed_work_enable_hotplug, HZ / 5);
832 
833 	return 0;
834 }
835 
836 /* ----------------------------------------------------------------------- */
837 
838 #ifdef CONFIG_VIDEO_ADV_DEBUG
adv7842_inv_register(struct v4l2_subdev * sd)839 static void adv7842_inv_register(struct v4l2_subdev *sd)
840 {
841 	v4l2_info(sd, "0x000-0x0ff: IO Map\n");
842 	v4l2_info(sd, "0x100-0x1ff: AVLink Map\n");
843 	v4l2_info(sd, "0x200-0x2ff: CEC Map\n");
844 	v4l2_info(sd, "0x300-0x3ff: InfoFrame Map\n");
845 	v4l2_info(sd, "0x400-0x4ff: SDP_IO Map\n");
846 	v4l2_info(sd, "0x500-0x5ff: SDP Map\n");
847 	v4l2_info(sd, "0x600-0x6ff: AFE Map\n");
848 	v4l2_info(sd, "0x700-0x7ff: Repeater Map\n");
849 	v4l2_info(sd, "0x800-0x8ff: EDID Map\n");
850 	v4l2_info(sd, "0x900-0x9ff: HDMI Map\n");
851 	v4l2_info(sd, "0xa00-0xaff: CP Map\n");
852 	v4l2_info(sd, "0xb00-0xbff: VDP Map\n");
853 }
854 
adv7842_g_register(struct v4l2_subdev * sd,struct v4l2_dbg_register * reg)855 static int adv7842_g_register(struct v4l2_subdev *sd,
856 			      struct v4l2_dbg_register *reg)
857 {
858 	reg->size = 1;
859 	switch (reg->reg >> 8) {
860 	case 0:
861 		reg->val = io_read(sd, reg->reg & 0xff);
862 		break;
863 	case 1:
864 		reg->val = avlink_read(sd, reg->reg & 0xff);
865 		break;
866 	case 2:
867 		reg->val = cec_read(sd, reg->reg & 0xff);
868 		break;
869 	case 3:
870 		reg->val = infoframe_read(sd, reg->reg & 0xff);
871 		break;
872 	case 4:
873 		reg->val = sdp_io_read(sd, reg->reg & 0xff);
874 		break;
875 	case 5:
876 		reg->val = sdp_read(sd, reg->reg & 0xff);
877 		break;
878 	case 6:
879 		reg->val = afe_read(sd, reg->reg & 0xff);
880 		break;
881 	case 7:
882 		reg->val = rep_read(sd, reg->reg & 0xff);
883 		break;
884 	case 8:
885 		reg->val = edid_read(sd, reg->reg & 0xff);
886 		break;
887 	case 9:
888 		reg->val = hdmi_read(sd, reg->reg & 0xff);
889 		break;
890 	case 0xa:
891 		reg->val = cp_read(sd, reg->reg & 0xff);
892 		break;
893 	case 0xb:
894 		reg->val = vdp_read(sd, reg->reg & 0xff);
895 		break;
896 	default:
897 		v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
898 		adv7842_inv_register(sd);
899 		break;
900 	}
901 	return 0;
902 }
903 
adv7842_s_register(struct v4l2_subdev * sd,const struct v4l2_dbg_register * reg)904 static int adv7842_s_register(struct v4l2_subdev *sd,
905 		const struct v4l2_dbg_register *reg)
906 {
907 	u8 val = reg->val & 0xff;
908 
909 	switch (reg->reg >> 8) {
910 	case 0:
911 		io_write(sd, reg->reg & 0xff, val);
912 		break;
913 	case 1:
914 		avlink_write(sd, reg->reg & 0xff, val);
915 		break;
916 	case 2:
917 		cec_write(sd, reg->reg & 0xff, val);
918 		break;
919 	case 3:
920 		infoframe_write(sd, reg->reg & 0xff, val);
921 		break;
922 	case 4:
923 		sdp_io_write(sd, reg->reg & 0xff, val);
924 		break;
925 	case 5:
926 		sdp_write(sd, reg->reg & 0xff, val);
927 		break;
928 	case 6:
929 		afe_write(sd, reg->reg & 0xff, val);
930 		break;
931 	case 7:
932 		rep_write(sd, reg->reg & 0xff, val);
933 		break;
934 	case 8:
935 		edid_write(sd, reg->reg & 0xff, val);
936 		break;
937 	case 9:
938 		hdmi_write(sd, reg->reg & 0xff, val);
939 		break;
940 	case 0xa:
941 		cp_write(sd, reg->reg & 0xff, val);
942 		break;
943 	case 0xb:
944 		vdp_write(sd, reg->reg & 0xff, val);
945 		break;
946 	default:
947 		v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
948 		adv7842_inv_register(sd);
949 		break;
950 	}
951 	return 0;
952 }
953 #endif
954 
adv7842_s_detect_tx_5v_ctrl(struct v4l2_subdev * sd)955 static int adv7842_s_detect_tx_5v_ctrl(struct v4l2_subdev *sd)
956 {
957 	struct adv7842_state *state = to_state(sd);
958 	u16 cable_det = adv7842_read_cable_det(sd);
959 
960 	v4l2_dbg(1, debug, sd, "%s: 0x%x\n", __func__, cable_det);
961 
962 	return v4l2_ctrl_s_ctrl(state->detect_tx_5v_ctrl, cable_det);
963 }
964 
find_and_set_predefined_video_timings(struct v4l2_subdev * sd,u8 prim_mode,const struct adv7842_video_standards * predef_vid_timings,const struct v4l2_dv_timings * timings)965 static int find_and_set_predefined_video_timings(struct v4l2_subdev *sd,
966 		u8 prim_mode,
967 		const struct adv7842_video_standards *predef_vid_timings,
968 		const struct v4l2_dv_timings *timings)
969 {
970 	int i;
971 
972 	for (i = 0; predef_vid_timings[i].timings.bt.width; i++) {
973 		if (!v4l2_match_dv_timings(timings, &predef_vid_timings[i].timings,
974 				  is_digital_input(sd) ? 250000 : 1000000, false))
975 			continue;
976 		/* video std */
977 		io_write(sd, 0x00, predef_vid_timings[i].vid_std);
978 		/* v_freq and prim mode */
979 		io_write(sd, 0x01, (predef_vid_timings[i].v_freq << 4) + prim_mode);
980 		return 0;
981 	}
982 
983 	return -1;
984 }
985 
configure_predefined_video_timings(struct v4l2_subdev * sd,struct v4l2_dv_timings * timings)986 static int configure_predefined_video_timings(struct v4l2_subdev *sd,
987 		struct v4l2_dv_timings *timings)
988 {
989 	struct adv7842_state *state = to_state(sd);
990 	int err;
991 
992 	v4l2_dbg(1, debug, sd, "%s\n", __func__);
993 
994 	/* reset to default values */
995 	io_write(sd, 0x16, 0x43);
996 	io_write(sd, 0x17, 0x5a);
997 	/* disable embedded syncs for auto graphics mode */
998 	cp_write_and_or(sd, 0x81, 0xef, 0x00);
999 	cp_write(sd, 0x26, 0x00);
1000 	cp_write(sd, 0x27, 0x00);
1001 	cp_write(sd, 0x28, 0x00);
1002 	cp_write(sd, 0x29, 0x00);
1003 	cp_write(sd, 0x8f, 0x40);
1004 	cp_write(sd, 0x90, 0x00);
1005 	cp_write(sd, 0xa5, 0x00);
1006 	cp_write(sd, 0xa6, 0x00);
1007 	cp_write(sd, 0xa7, 0x00);
1008 	cp_write(sd, 0xab, 0x00);
1009 	cp_write(sd, 0xac, 0x00);
1010 
1011 	switch (state->mode) {
1012 	case ADV7842_MODE_COMP:
1013 	case ADV7842_MODE_RGB:
1014 		err = find_and_set_predefined_video_timings(sd,
1015 				0x01, adv7842_prim_mode_comp, timings);
1016 		if (err)
1017 			err = find_and_set_predefined_video_timings(sd,
1018 					0x02, adv7842_prim_mode_gr, timings);
1019 		break;
1020 	case ADV7842_MODE_HDMI:
1021 		err = find_and_set_predefined_video_timings(sd,
1022 				0x05, adv7842_prim_mode_hdmi_comp, timings);
1023 		if (err)
1024 			err = find_and_set_predefined_video_timings(sd,
1025 					0x06, adv7842_prim_mode_hdmi_gr, timings);
1026 		break;
1027 	default:
1028 		v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
1029 				__func__, state->mode);
1030 		err = -1;
1031 		break;
1032 	}
1033 
1034 
1035 	return err;
1036 }
1037 
configure_custom_video_timings(struct v4l2_subdev * sd,const struct v4l2_bt_timings * bt)1038 static void configure_custom_video_timings(struct v4l2_subdev *sd,
1039 		const struct v4l2_bt_timings *bt)
1040 {
1041 	struct adv7842_state *state = to_state(sd);
1042 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1043 	u32 width = htotal(bt);
1044 	u32 height = vtotal(bt);
1045 	u16 cp_start_sav = bt->hsync + bt->hbackporch - 4;
1046 	u16 cp_start_eav = width - bt->hfrontporch;
1047 	u16 cp_start_vbi = height - bt->vfrontporch + 1;
1048 	u16 cp_end_vbi = bt->vsync + bt->vbackporch + 1;
1049 	u16 ch1_fr_ll = (((u32)bt->pixelclock / 100) > 0) ?
1050 		((width * (ADV7842_fsc / 100)) / ((u32)bt->pixelclock / 100)) : 0;
1051 	const u8 pll[2] = {
1052 		0xc0 | ((width >> 8) & 0x1f),
1053 		width & 0xff
1054 	};
1055 
1056 	v4l2_dbg(2, debug, sd, "%s\n", __func__);
1057 
1058 	switch (state->mode) {
1059 	case ADV7842_MODE_COMP:
1060 	case ADV7842_MODE_RGB:
1061 		/* auto graphics */
1062 		io_write(sd, 0x00, 0x07); /* video std */
1063 		io_write(sd, 0x01, 0x02); /* prim mode */
1064 		/* enable embedded syncs for auto graphics mode */
1065 		cp_write_and_or(sd, 0x81, 0xef, 0x10);
1066 
1067 		/* Should only be set in auto-graphics mode [REF_02, p. 91-92] */
1068 		/* setup PLL_DIV_MAN_EN and PLL_DIV_RATIO */
1069 		/* IO-map reg. 0x16 and 0x17 should be written in sequence */
1070 		if (i2c_smbus_write_i2c_block_data(client, 0x16, 2, pll)) {
1071 			v4l2_err(sd, "writing to reg 0x16 and 0x17 failed\n");
1072 			break;
1073 		}
1074 
1075 		/* active video - horizontal timing */
1076 		cp_write(sd, 0x26, (cp_start_sav >> 8) & 0xf);
1077 		cp_write(sd, 0x27, (cp_start_sav & 0xff));
1078 		cp_write(sd, 0x28, (cp_start_eav >> 8) & 0xf);
1079 		cp_write(sd, 0x29, (cp_start_eav & 0xff));
1080 
1081 		/* active video - vertical timing */
1082 		cp_write(sd, 0xa5, (cp_start_vbi >> 4) & 0xff);
1083 		cp_write(sd, 0xa6, ((cp_start_vbi & 0xf) << 4) |
1084 					((cp_end_vbi >> 8) & 0xf));
1085 		cp_write(sd, 0xa7, cp_end_vbi & 0xff);
1086 		break;
1087 	case ADV7842_MODE_HDMI:
1088 		/* set default prim_mode/vid_std for HDMI
1089 		   according to [REF_03, c. 4.2] */
1090 		io_write(sd, 0x00, 0x02); /* video std */
1091 		io_write(sd, 0x01, 0x06); /* prim mode */
1092 		break;
1093 	default:
1094 		v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
1095 				__func__, state->mode);
1096 		break;
1097 	}
1098 
1099 	cp_write(sd, 0x8f, (ch1_fr_ll >> 8) & 0x7);
1100 	cp_write(sd, 0x90, ch1_fr_ll & 0xff);
1101 	cp_write(sd, 0xab, (height >> 4) & 0xff);
1102 	cp_write(sd, 0xac, (height & 0x0f) << 4);
1103 }
1104 
adv7842_set_offset(struct v4l2_subdev * sd,bool auto_offset,u16 offset_a,u16 offset_b,u16 offset_c)1105 static void adv7842_set_offset(struct v4l2_subdev *sd, bool auto_offset, u16 offset_a, u16 offset_b, u16 offset_c)
1106 {
1107 	struct adv7842_state *state = to_state(sd);
1108 	u8 offset_buf[4];
1109 
1110 	if (auto_offset) {
1111 		offset_a = 0x3ff;
1112 		offset_b = 0x3ff;
1113 		offset_c = 0x3ff;
1114 	}
1115 
1116 	v4l2_dbg(2, debug, sd, "%s: %s offset: a = 0x%x, b = 0x%x, c = 0x%x\n",
1117 		 __func__, auto_offset ? "Auto" : "Manual",
1118 		 offset_a, offset_b, offset_c);
1119 
1120 	offset_buf[0]= (cp_read(sd, 0x77) & 0xc0) | ((offset_a & 0x3f0) >> 4);
1121 	offset_buf[1] = ((offset_a & 0x00f) << 4) | ((offset_b & 0x3c0) >> 6);
1122 	offset_buf[2] = ((offset_b & 0x03f) << 2) | ((offset_c & 0x300) >> 8);
1123 	offset_buf[3] = offset_c & 0x0ff;
1124 
1125 	/* Registers must be written in this order with no i2c access in between */
1126 	if (i2c_smbus_write_i2c_block_data(state->i2c_cp, 0x77, 4, offset_buf))
1127 		v4l2_err(sd, "%s: i2c error writing to CP reg 0x77, 0x78, 0x79, 0x7a\n", __func__);
1128 }
1129 
adv7842_set_gain(struct v4l2_subdev * sd,bool auto_gain,u16 gain_a,u16 gain_b,u16 gain_c)1130 static void adv7842_set_gain(struct v4l2_subdev *sd, bool auto_gain, u16 gain_a, u16 gain_b, u16 gain_c)
1131 {
1132 	struct adv7842_state *state = to_state(sd);
1133 	u8 gain_buf[4];
1134 	u8 gain_man = 1;
1135 	u8 agc_mode_man = 1;
1136 
1137 	if (auto_gain) {
1138 		gain_man = 0;
1139 		agc_mode_man = 0;
1140 		gain_a = 0x100;
1141 		gain_b = 0x100;
1142 		gain_c = 0x100;
1143 	}
1144 
1145 	v4l2_dbg(2, debug, sd, "%s: %s gain: a = 0x%x, b = 0x%x, c = 0x%x\n",
1146 		 __func__, auto_gain ? "Auto" : "Manual",
1147 		 gain_a, gain_b, gain_c);
1148 
1149 	gain_buf[0] = ((gain_man << 7) | (agc_mode_man << 6) | ((gain_a & 0x3f0) >> 4));
1150 	gain_buf[1] = (((gain_a & 0x00f) << 4) | ((gain_b & 0x3c0) >> 6));
1151 	gain_buf[2] = (((gain_b & 0x03f) << 2) | ((gain_c & 0x300) >> 8));
1152 	gain_buf[3] = ((gain_c & 0x0ff));
1153 
1154 	/* Registers must be written in this order with no i2c access in between */
1155 	if (i2c_smbus_write_i2c_block_data(state->i2c_cp, 0x73, 4, gain_buf))
1156 		v4l2_err(sd, "%s: i2c error writing to CP reg 0x73, 0x74, 0x75, 0x76\n", __func__);
1157 }
1158 
set_rgb_quantization_range(struct v4l2_subdev * sd)1159 static void set_rgb_quantization_range(struct v4l2_subdev *sd)
1160 {
1161 	struct adv7842_state *state = to_state(sd);
1162 	bool rgb_output = io_read(sd, 0x02) & 0x02;
1163 	bool hdmi_signal = hdmi_read(sd, 0x05) & 0x80;
1164 	u8 y = HDMI_COLORSPACE_RGB;
1165 
1166 	if (hdmi_signal && (io_read(sd, 0x60) & 1))
1167 		y = infoframe_read(sd, 0x01) >> 5;
1168 
1169 	v4l2_dbg(2, debug, sd, "%s: RGB quantization range: %d, RGB out: %d, HDMI: %d\n",
1170 			__func__, state->rgb_quantization_range,
1171 			rgb_output, hdmi_signal);
1172 
1173 	adv7842_set_gain(sd, true, 0x0, 0x0, 0x0);
1174 	adv7842_set_offset(sd, true, 0x0, 0x0, 0x0);
1175 	io_write_clr_set(sd, 0x02, 0x04, rgb_output ? 0 : 4);
1176 
1177 	switch (state->rgb_quantization_range) {
1178 	case V4L2_DV_RGB_RANGE_AUTO:
1179 		if (state->mode == ADV7842_MODE_RGB) {
1180 			/* Receiving analog RGB signal
1181 			 * Set RGB full range (0-255) */
1182 			io_write_and_or(sd, 0x02, 0x0f, 0x10);
1183 			break;
1184 		}
1185 
1186 		if (state->mode == ADV7842_MODE_COMP) {
1187 			/* Receiving analog YPbPr signal
1188 			 * Set automode */
1189 			io_write_and_or(sd, 0x02, 0x0f, 0xf0);
1190 			break;
1191 		}
1192 
1193 		if (hdmi_signal) {
1194 			/* Receiving HDMI signal
1195 			 * Set automode */
1196 			io_write_and_or(sd, 0x02, 0x0f, 0xf0);
1197 			break;
1198 		}
1199 
1200 		/* Receiving DVI-D signal
1201 		 * ADV7842 selects RGB limited range regardless of
1202 		 * input format (CE/IT) in automatic mode */
1203 		if (state->timings.bt.flags & V4L2_DV_FL_IS_CE_VIDEO) {
1204 			/* RGB limited range (16-235) */
1205 			io_write_and_or(sd, 0x02, 0x0f, 0x00);
1206 		} else {
1207 			/* RGB full range (0-255) */
1208 			io_write_and_or(sd, 0x02, 0x0f, 0x10);
1209 
1210 			if (is_digital_input(sd) && rgb_output) {
1211 				adv7842_set_offset(sd, false, 0x40, 0x40, 0x40);
1212 			} else {
1213 				adv7842_set_gain(sd, false, 0xe0, 0xe0, 0xe0);
1214 				adv7842_set_offset(sd, false, 0x70, 0x70, 0x70);
1215 			}
1216 		}
1217 		break;
1218 	case V4L2_DV_RGB_RANGE_LIMITED:
1219 		if (state->mode == ADV7842_MODE_COMP) {
1220 			/* YCrCb limited range (16-235) */
1221 			io_write_and_or(sd, 0x02, 0x0f, 0x20);
1222 			break;
1223 		}
1224 
1225 		if (y != HDMI_COLORSPACE_RGB)
1226 			break;
1227 
1228 		/* RGB limited range (16-235) */
1229 		io_write_and_or(sd, 0x02, 0x0f, 0x00);
1230 
1231 		break;
1232 	case V4L2_DV_RGB_RANGE_FULL:
1233 		if (state->mode == ADV7842_MODE_COMP) {
1234 			/* YCrCb full range (0-255) */
1235 			io_write_and_or(sd, 0x02, 0x0f, 0x60);
1236 			break;
1237 		}
1238 
1239 		if (y != HDMI_COLORSPACE_RGB)
1240 			break;
1241 
1242 		/* RGB full range (0-255) */
1243 		io_write_and_or(sd, 0x02, 0x0f, 0x10);
1244 
1245 		if (is_analog_input(sd) || hdmi_signal)
1246 			break;
1247 
1248 		/* Adjust gain/offset for DVI-D signals only */
1249 		if (rgb_output) {
1250 			adv7842_set_offset(sd, false, 0x40, 0x40, 0x40);
1251 		} else {
1252 			adv7842_set_gain(sd, false, 0xe0, 0xe0, 0xe0);
1253 			adv7842_set_offset(sd, false, 0x70, 0x70, 0x70);
1254 		}
1255 		break;
1256 	}
1257 }
1258 
adv7842_s_ctrl(struct v4l2_ctrl * ctrl)1259 static int adv7842_s_ctrl(struct v4l2_ctrl *ctrl)
1260 {
1261 	struct v4l2_subdev *sd = to_sd(ctrl);
1262 	struct adv7842_state *state = to_state(sd);
1263 
1264 	/* TODO SDP ctrls
1265 	   contrast/brightness/hue/free run is acting a bit strange,
1266 	   not sure if sdp csc is correct.
1267 	 */
1268 	switch (ctrl->id) {
1269 	/* standard ctrls */
1270 	case V4L2_CID_BRIGHTNESS:
1271 		cp_write(sd, 0x3c, ctrl->val);
1272 		sdp_write(sd, 0x14, ctrl->val);
1273 		/* ignore lsb sdp 0x17[3:2] */
1274 		return 0;
1275 	case V4L2_CID_CONTRAST:
1276 		cp_write(sd, 0x3a, ctrl->val);
1277 		sdp_write(sd, 0x13, ctrl->val);
1278 		/* ignore lsb sdp 0x17[1:0] */
1279 		return 0;
1280 	case V4L2_CID_SATURATION:
1281 		cp_write(sd, 0x3b, ctrl->val);
1282 		sdp_write(sd, 0x15, ctrl->val);
1283 		/* ignore lsb sdp 0x17[5:4] */
1284 		return 0;
1285 	case V4L2_CID_HUE:
1286 		cp_write(sd, 0x3d, ctrl->val);
1287 		sdp_write(sd, 0x16, ctrl->val);
1288 		/* ignore lsb sdp 0x17[7:6] */
1289 		return 0;
1290 		/* custom ctrls */
1291 	case V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE:
1292 		afe_write(sd, 0xc8, ctrl->val);
1293 		return 0;
1294 	case V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL:
1295 		cp_write_and_or(sd, 0xbf, ~0x04, (ctrl->val << 2));
1296 		sdp_write_and_or(sd, 0xdd, ~0x04, (ctrl->val << 2));
1297 		return 0;
1298 	case V4L2_CID_ADV_RX_FREE_RUN_COLOR: {
1299 		u8 R = (ctrl->val & 0xff0000) >> 16;
1300 		u8 G = (ctrl->val & 0x00ff00) >> 8;
1301 		u8 B = (ctrl->val & 0x0000ff);
1302 		/* RGB -> YUV, numerical approximation */
1303 		int Y = 66 * R + 129 * G + 25 * B;
1304 		int U = -38 * R - 74 * G + 112 * B;
1305 		int V = 112 * R - 94 * G - 18 * B;
1306 
1307 		/* Scale down to 8 bits with rounding */
1308 		Y = (Y + 128) >> 8;
1309 		U = (U + 128) >> 8;
1310 		V = (V + 128) >> 8;
1311 		/* make U,V positive */
1312 		Y += 16;
1313 		U += 128;
1314 		V += 128;
1315 
1316 		v4l2_dbg(1, debug, sd, "R %x, G %x, B %x\n", R, G, B);
1317 		v4l2_dbg(1, debug, sd, "Y %x, U %x, V %x\n", Y, U, V);
1318 
1319 		/* CP */
1320 		cp_write(sd, 0xc1, R);
1321 		cp_write(sd, 0xc0, G);
1322 		cp_write(sd, 0xc2, B);
1323 		/* SDP */
1324 		sdp_write(sd, 0xde, Y);
1325 		sdp_write(sd, 0xdf, (V & 0xf0) | ((U >> 4) & 0x0f));
1326 		return 0;
1327 	}
1328 	case V4L2_CID_DV_RX_RGB_RANGE:
1329 		state->rgb_quantization_range = ctrl->val;
1330 		set_rgb_quantization_range(sd);
1331 		return 0;
1332 	}
1333 	return -EINVAL;
1334 }
1335 
adv7842_g_volatile_ctrl(struct v4l2_ctrl * ctrl)1336 static int adv7842_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1337 {
1338 	struct v4l2_subdev *sd = to_sd(ctrl);
1339 
1340 	if (ctrl->id == V4L2_CID_DV_RX_IT_CONTENT_TYPE) {
1341 		ctrl->val = V4L2_DV_IT_CONTENT_TYPE_NO_ITC;
1342 		if ((io_read(sd, 0x60) & 1) && (infoframe_read(sd, 0x03) & 0x80))
1343 			ctrl->val = (infoframe_read(sd, 0x05) >> 4) & 3;
1344 		return 0;
1345 	}
1346 	return -EINVAL;
1347 }
1348 
no_power(struct v4l2_subdev * sd)1349 static inline bool no_power(struct v4l2_subdev *sd)
1350 {
1351 	return io_read(sd, 0x0c) & 0x24;
1352 }
1353 
no_cp_signal(struct v4l2_subdev * sd)1354 static inline bool no_cp_signal(struct v4l2_subdev *sd)
1355 {
1356 	return ((cp_read(sd, 0xb5) & 0xd0) != 0xd0) || !(cp_read(sd, 0xb1) & 0x80);
1357 }
1358 
is_hdmi(struct v4l2_subdev * sd)1359 static inline bool is_hdmi(struct v4l2_subdev *sd)
1360 {
1361 	return hdmi_read(sd, 0x05) & 0x80;
1362 }
1363 
adv7842_g_input_status(struct v4l2_subdev * sd,u32 * status)1364 static int adv7842_g_input_status(struct v4l2_subdev *sd, u32 *status)
1365 {
1366 	struct adv7842_state *state = to_state(sd);
1367 
1368 	*status = 0;
1369 
1370 	if (io_read(sd, 0x0c) & 0x24)
1371 		*status |= V4L2_IN_ST_NO_POWER;
1372 
1373 	if (state->mode == ADV7842_MODE_SDP) {
1374 		/* status from SDP block */
1375 		if (!(sdp_read(sd, 0x5A) & 0x01))
1376 			*status |= V4L2_IN_ST_NO_SIGNAL;
1377 
1378 		v4l2_dbg(1, debug, sd, "%s: SDP status = 0x%x\n",
1379 				__func__, *status);
1380 		return 0;
1381 	}
1382 	/* status from CP block */
1383 	if ((cp_read(sd, 0xb5) & 0xd0) != 0xd0 ||
1384 			!(cp_read(sd, 0xb1) & 0x80))
1385 		/* TODO channel 2 */
1386 		*status |= V4L2_IN_ST_NO_SIGNAL;
1387 
1388 	if (is_digital_input(sd) && ((io_read(sd, 0x74) & 0x03) != 0x03))
1389 		*status |= V4L2_IN_ST_NO_SIGNAL;
1390 
1391 	v4l2_dbg(1, debug, sd, "%s: CP status = 0x%x\n",
1392 			__func__, *status);
1393 
1394 	return 0;
1395 }
1396 
1397 struct stdi_readback {
1398 	u16 bl, lcf, lcvs;
1399 	u8 hs_pol, vs_pol;
1400 	bool interlaced;
1401 };
1402 
stdi2dv_timings(struct v4l2_subdev * sd,struct stdi_readback * stdi,struct v4l2_dv_timings * timings)1403 static int stdi2dv_timings(struct v4l2_subdev *sd,
1404 		struct stdi_readback *stdi,
1405 		struct v4l2_dv_timings *timings)
1406 {
1407 	struct adv7842_state *state = to_state(sd);
1408 	u32 hfreq = (ADV7842_fsc * 8) / stdi->bl;
1409 	u32 pix_clk;
1410 	int i;
1411 
1412 	for (i = 0; v4l2_dv_timings_presets[i].bt.width; i++) {
1413 		const struct v4l2_bt_timings *bt = &v4l2_dv_timings_presets[i].bt;
1414 
1415 		if (!v4l2_valid_dv_timings(&v4l2_dv_timings_presets[i],
1416 					   adv7842_get_dv_timings_cap(sd),
1417 					   adv7842_check_dv_timings, NULL))
1418 			continue;
1419 		if (vtotal(bt) != stdi->lcf + 1)
1420 			continue;
1421 		if (bt->vsync != stdi->lcvs)
1422 			continue;
1423 
1424 		pix_clk = hfreq * htotal(bt);
1425 
1426 		if ((pix_clk < bt->pixelclock + 1000000) &&
1427 		    (pix_clk > bt->pixelclock - 1000000)) {
1428 			*timings = v4l2_dv_timings_presets[i];
1429 			return 0;
1430 		}
1431 	}
1432 
1433 	if (v4l2_detect_cvt(stdi->lcf + 1, hfreq, stdi->lcvs, 0,
1434 			    (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
1435 			    (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
1436 			    false, adv7842_get_dv_timings_cap(sd), timings))
1437 		return 0;
1438 	if (v4l2_detect_gtf(stdi->lcf + 1, hfreq, stdi->lcvs,
1439 			    (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
1440 			    (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
1441 			    false, state->aspect_ratio,
1442 			    adv7842_get_dv_timings_cap(sd), timings))
1443 		return 0;
1444 
1445 	v4l2_dbg(2, debug, sd,
1446 		"%s: No format candidate found for lcvs = %d, lcf=%d, bl = %d, %chsync, %cvsync\n",
1447 		__func__, stdi->lcvs, stdi->lcf, stdi->bl,
1448 		stdi->hs_pol, stdi->vs_pol);
1449 	return -1;
1450 }
1451 
read_stdi(struct v4l2_subdev * sd,struct stdi_readback * stdi)1452 static int read_stdi(struct v4l2_subdev *sd, struct stdi_readback *stdi)
1453 {
1454 	u32 status;
1455 
1456 	adv7842_g_input_status(sd, &status);
1457 	if (status & V4L2_IN_ST_NO_SIGNAL) {
1458 		v4l2_dbg(2, debug, sd, "%s: no signal\n", __func__);
1459 		return -ENOLINK;
1460 	}
1461 
1462 	stdi->bl = ((cp_read(sd, 0xb1) & 0x3f) << 8) | cp_read(sd, 0xb2);
1463 	stdi->lcf = ((cp_read(sd, 0xb3) & 0x7) << 8) | cp_read(sd, 0xb4);
1464 	stdi->lcvs = cp_read(sd, 0xb3) >> 3;
1465 
1466 	if ((cp_read(sd, 0xb5) & 0x80) && ((cp_read(sd, 0xb5) & 0x03) == 0x01)) {
1467 		stdi->hs_pol = ((cp_read(sd, 0xb5) & 0x10) ?
1468 			((cp_read(sd, 0xb5) & 0x08) ? '+' : '-') : 'x');
1469 		stdi->vs_pol = ((cp_read(sd, 0xb5) & 0x40) ?
1470 			((cp_read(sd, 0xb5) & 0x20) ? '+' : '-') : 'x');
1471 	} else {
1472 		stdi->hs_pol = 'x';
1473 		stdi->vs_pol = 'x';
1474 	}
1475 	stdi->interlaced = (cp_read(sd, 0xb1) & 0x40) ? true : false;
1476 
1477 	if (stdi->lcf < 239 || stdi->bl < 8 || stdi->bl == 0x3fff) {
1478 		v4l2_dbg(2, debug, sd, "%s: invalid signal\n", __func__);
1479 		return -ENOLINK;
1480 	}
1481 
1482 	v4l2_dbg(2, debug, sd,
1483 		"%s: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, %chsync, %cvsync, %s\n",
1484 		 __func__, stdi->lcf, stdi->bl, stdi->lcvs,
1485 		 stdi->hs_pol, stdi->vs_pol,
1486 		 stdi->interlaced ? "interlaced" : "progressive");
1487 
1488 	return 0;
1489 }
1490 
adv7842_enum_dv_timings(struct v4l2_subdev * sd,struct v4l2_enum_dv_timings * timings)1491 static int adv7842_enum_dv_timings(struct v4l2_subdev *sd,
1492 				   struct v4l2_enum_dv_timings *timings)
1493 {
1494 	if (timings->pad != 0)
1495 		return -EINVAL;
1496 
1497 	return v4l2_enum_dv_timings_cap(timings,
1498 		adv7842_get_dv_timings_cap(sd), adv7842_check_dv_timings, NULL);
1499 }
1500 
adv7842_dv_timings_cap(struct v4l2_subdev * sd,struct v4l2_dv_timings_cap * cap)1501 static int adv7842_dv_timings_cap(struct v4l2_subdev *sd,
1502 				  struct v4l2_dv_timings_cap *cap)
1503 {
1504 	if (cap->pad != 0)
1505 		return -EINVAL;
1506 
1507 	*cap = *adv7842_get_dv_timings_cap(sd);
1508 	return 0;
1509 }
1510 
1511 /* Fill the optional fields .standards and .flags in struct v4l2_dv_timings
1512    if the format is listed in adv7842_timings[] */
adv7842_fill_optional_dv_timings_fields(struct v4l2_subdev * sd,struct v4l2_dv_timings * timings)1513 static void adv7842_fill_optional_dv_timings_fields(struct v4l2_subdev *sd,
1514 		struct v4l2_dv_timings *timings)
1515 {
1516 	v4l2_find_dv_timings_cap(timings, adv7842_get_dv_timings_cap(sd),
1517 			is_digital_input(sd) ? 250000 : 1000000,
1518 			adv7842_check_dv_timings, NULL);
1519 	timings->bt.flags |= V4L2_DV_FL_CAN_DETECT_REDUCED_FPS;
1520 }
1521 
adv7842_query_dv_timings(struct v4l2_subdev * sd,struct v4l2_dv_timings * timings)1522 static int adv7842_query_dv_timings(struct v4l2_subdev *sd,
1523 				    struct v4l2_dv_timings *timings)
1524 {
1525 	struct adv7842_state *state = to_state(sd);
1526 	struct v4l2_bt_timings *bt = &timings->bt;
1527 	struct stdi_readback stdi = { 0 };
1528 
1529 	v4l2_dbg(1, debug, sd, "%s:\n", __func__);
1530 
1531 	memset(timings, 0, sizeof(struct v4l2_dv_timings));
1532 
1533 	/* SDP block */
1534 	if (state->mode == ADV7842_MODE_SDP)
1535 		return -ENODATA;
1536 
1537 	/* read STDI */
1538 	if (read_stdi(sd, &stdi)) {
1539 		state->restart_stdi_once = true;
1540 		v4l2_dbg(1, debug, sd, "%s: no valid signal\n", __func__);
1541 		return -ENOLINK;
1542 	}
1543 	bt->interlaced = stdi.interlaced ?
1544 		V4L2_DV_INTERLACED : V4L2_DV_PROGRESSIVE;
1545 	bt->standards = V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
1546 			V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT;
1547 
1548 	if (is_digital_input(sd)) {
1549 		u32 freq;
1550 
1551 		timings->type = V4L2_DV_BT_656_1120;
1552 
1553 		bt->width = (hdmi_read(sd, 0x07) & 0x0f) * 256 + hdmi_read(sd, 0x08);
1554 		bt->height = (hdmi_read(sd, 0x09) & 0x0f) * 256 + hdmi_read(sd, 0x0a);
1555 		freq = ((hdmi_read(sd, 0x51) << 1) + (hdmi_read(sd, 0x52) >> 7)) * 1000000;
1556 		freq += ((hdmi_read(sd, 0x52) & 0x7f) * 7813);
1557 		if (is_hdmi(sd)) {
1558 			/* adjust for deep color mode */
1559 			freq = freq * 8 / (((hdmi_read(sd, 0x0b) & 0xc0) >> 6) * 2 + 8);
1560 		}
1561 		bt->pixelclock = freq;
1562 		bt->hfrontporch = (hdmi_read(sd, 0x20) & 0x03) * 256 +
1563 			hdmi_read(sd, 0x21);
1564 		bt->hsync = (hdmi_read(sd, 0x22) & 0x03) * 256 +
1565 			hdmi_read(sd, 0x23);
1566 		bt->hbackporch = (hdmi_read(sd, 0x24) & 0x03) * 256 +
1567 			hdmi_read(sd, 0x25);
1568 		bt->vfrontporch = ((hdmi_read(sd, 0x2a) & 0x1f) * 256 +
1569 			hdmi_read(sd, 0x2b)) / 2;
1570 		bt->vsync = ((hdmi_read(sd, 0x2e) & 0x1f) * 256 +
1571 			hdmi_read(sd, 0x2f)) / 2;
1572 		bt->vbackporch = ((hdmi_read(sd, 0x32) & 0x1f) * 256 +
1573 			hdmi_read(sd, 0x33)) / 2;
1574 		bt->polarities = ((hdmi_read(sd, 0x05) & 0x10) ? V4L2_DV_VSYNC_POS_POL : 0) |
1575 			((hdmi_read(sd, 0x05) & 0x20) ? V4L2_DV_HSYNC_POS_POL : 0);
1576 		if (bt->interlaced == V4L2_DV_INTERLACED) {
1577 			bt->height += (hdmi_read(sd, 0x0b) & 0x0f) * 256 +
1578 					hdmi_read(sd, 0x0c);
1579 			bt->il_vfrontporch = ((hdmi_read(sd, 0x2c) & 0x1f) * 256 +
1580 					hdmi_read(sd, 0x2d)) / 2;
1581 			bt->il_vsync = ((hdmi_read(sd, 0x30) & 0x1f) * 256 +
1582 					hdmi_read(sd, 0x31)) / 2;
1583 			bt->il_vbackporch = ((hdmi_read(sd, 0x34) & 0x1f) * 256 +
1584 					hdmi_read(sd, 0x35)) / 2;
1585 		} else {
1586 			bt->il_vfrontporch = 0;
1587 			bt->il_vsync = 0;
1588 			bt->il_vbackporch = 0;
1589 		}
1590 		adv7842_fill_optional_dv_timings_fields(sd, timings);
1591 		if ((timings->bt.flags & V4L2_DV_FL_CAN_REDUCE_FPS) &&
1592 		    freq < bt->pixelclock) {
1593 			u32 reduced_freq = ((u32)bt->pixelclock / 1001) * 1000;
1594 			u32 delta_freq = abs(freq - reduced_freq);
1595 
1596 			if (delta_freq < ((u32)bt->pixelclock - reduced_freq) / 2)
1597 				timings->bt.flags |= V4L2_DV_FL_REDUCED_FPS;
1598 		}
1599 	} else {
1600 		/* find format
1601 		 * Since LCVS values are inaccurate [REF_03, p. 339-340],
1602 		 * stdi2dv_timings() is called with lcvs +-1 if the first attempt fails.
1603 		 */
1604 		if (!stdi2dv_timings(sd, &stdi, timings))
1605 			goto found;
1606 		stdi.lcvs += 1;
1607 		v4l2_dbg(1, debug, sd, "%s: lcvs + 1 = %d\n", __func__, stdi.lcvs);
1608 		if (!stdi2dv_timings(sd, &stdi, timings))
1609 			goto found;
1610 		stdi.lcvs -= 2;
1611 		v4l2_dbg(1, debug, sd, "%s: lcvs - 1 = %d\n", __func__, stdi.lcvs);
1612 		if (stdi2dv_timings(sd, &stdi, timings)) {
1613 			/*
1614 			 * The STDI block may measure wrong values, especially
1615 			 * for lcvs and lcf. If the driver can not find any
1616 			 * valid timing, the STDI block is restarted to measure
1617 			 * the video timings again. The function will return an
1618 			 * error, but the restart of STDI will generate a new
1619 			 * STDI interrupt and the format detection process will
1620 			 * restart.
1621 			 */
1622 			if (state->restart_stdi_once) {
1623 				v4l2_dbg(1, debug, sd, "%s: restart STDI\n", __func__);
1624 				/* TODO restart STDI for Sync Channel 2 */
1625 				/* enter one-shot mode */
1626 				cp_write_and_or(sd, 0x86, 0xf9, 0x00);
1627 				/* trigger STDI restart */
1628 				cp_write_and_or(sd, 0x86, 0xf9, 0x04);
1629 				/* reset to continuous mode */
1630 				cp_write_and_or(sd, 0x86, 0xf9, 0x02);
1631 				state->restart_stdi_once = false;
1632 				return -ENOLINK;
1633 			}
1634 			v4l2_dbg(1, debug, sd, "%s: format not supported\n", __func__);
1635 			return -ERANGE;
1636 		}
1637 		state->restart_stdi_once = true;
1638 	}
1639 found:
1640 
1641 	if (debug > 1)
1642 		v4l2_print_dv_timings(sd->name, "adv7842_query_dv_timings:",
1643 				timings, true);
1644 	return 0;
1645 }
1646 
adv7842_s_dv_timings(struct v4l2_subdev * sd,struct v4l2_dv_timings * timings)1647 static int adv7842_s_dv_timings(struct v4l2_subdev *sd,
1648 				struct v4l2_dv_timings *timings)
1649 {
1650 	struct adv7842_state *state = to_state(sd);
1651 	struct v4l2_bt_timings *bt;
1652 	int err;
1653 
1654 	v4l2_dbg(1, debug, sd, "%s:\n", __func__);
1655 
1656 	if (state->mode == ADV7842_MODE_SDP)
1657 		return -ENODATA;
1658 
1659 	if (v4l2_match_dv_timings(&state->timings, timings, 0, false)) {
1660 		v4l2_dbg(1, debug, sd, "%s: no change\n", __func__);
1661 		return 0;
1662 	}
1663 
1664 	bt = &timings->bt;
1665 
1666 	if (!v4l2_valid_dv_timings(timings, adv7842_get_dv_timings_cap(sd),
1667 				   adv7842_check_dv_timings, NULL))
1668 		return -ERANGE;
1669 
1670 	adv7842_fill_optional_dv_timings_fields(sd, timings);
1671 
1672 	state->timings = *timings;
1673 
1674 	cp_write(sd, 0x91, bt->interlaced ? 0x40 : 0x00);
1675 
1676 	/* Use prim_mode and vid_std when available */
1677 	err = configure_predefined_video_timings(sd, timings);
1678 	if (err) {
1679 		/* custom settings when the video format
1680 		  does not have prim_mode/vid_std */
1681 		configure_custom_video_timings(sd, bt);
1682 	}
1683 
1684 	set_rgb_quantization_range(sd);
1685 
1686 
1687 	if (debug > 1)
1688 		v4l2_print_dv_timings(sd->name, "adv7842_s_dv_timings: ",
1689 				      timings, true);
1690 	return 0;
1691 }
1692 
adv7842_g_dv_timings(struct v4l2_subdev * sd,struct v4l2_dv_timings * timings)1693 static int adv7842_g_dv_timings(struct v4l2_subdev *sd,
1694 				struct v4l2_dv_timings *timings)
1695 {
1696 	struct adv7842_state *state = to_state(sd);
1697 
1698 	if (state->mode == ADV7842_MODE_SDP)
1699 		return -ENODATA;
1700 	*timings = state->timings;
1701 	return 0;
1702 }
1703 
enable_input(struct v4l2_subdev * sd)1704 static void enable_input(struct v4l2_subdev *sd)
1705 {
1706 	struct adv7842_state *state = to_state(sd);
1707 
1708 	set_rgb_quantization_range(sd);
1709 	switch (state->mode) {
1710 	case ADV7842_MODE_SDP:
1711 	case ADV7842_MODE_COMP:
1712 	case ADV7842_MODE_RGB:
1713 		io_write(sd, 0x15, 0xb0);   /* Disable Tristate of Pins (no audio) */
1714 		break;
1715 	case ADV7842_MODE_HDMI:
1716 		hdmi_write(sd, 0x01, 0x00); /* Enable HDMI clock terminators */
1717 		io_write(sd, 0x15, 0xa0);   /* Disable Tristate of Pins */
1718 		hdmi_write_and_or(sd, 0x1a, 0xef, 0x00); /* Unmute audio */
1719 		break;
1720 	default:
1721 		v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
1722 			 __func__, state->mode);
1723 		break;
1724 	}
1725 }
1726 
disable_input(struct v4l2_subdev * sd)1727 static void disable_input(struct v4l2_subdev *sd)
1728 {
1729 	hdmi_write_and_or(sd, 0x1a, 0xef, 0x10); /* Mute audio [REF_01, c. 2.2.2] */
1730 	msleep(16); /* 512 samples with >= 32 kHz sample rate [REF_03, c. 8.29] */
1731 	io_write(sd, 0x15, 0xbe);   /* Tristate all outputs from video core */
1732 	hdmi_write(sd, 0x01, 0x78); /* Disable HDMI clock terminators */
1733 }
1734 
sdp_csc_coeff(struct v4l2_subdev * sd,const struct adv7842_sdp_csc_coeff * c)1735 static void sdp_csc_coeff(struct v4l2_subdev *sd,
1736 			  const struct adv7842_sdp_csc_coeff *c)
1737 {
1738 	/* csc auto/manual */
1739 	sdp_io_write_and_or(sd, 0xe0, 0xbf, c->manual ? 0x00 : 0x40);
1740 
1741 	if (!c->manual)
1742 		return;
1743 
1744 	/* csc scaling */
1745 	sdp_io_write_and_or(sd, 0xe0, 0x7f, c->scaling == 2 ? 0x80 : 0x00);
1746 
1747 	/* A coeff */
1748 	sdp_io_write_and_or(sd, 0xe0, 0xe0, c->A1 >> 8);
1749 	sdp_io_write(sd, 0xe1, c->A1);
1750 	sdp_io_write_and_or(sd, 0xe2, 0xe0, c->A2 >> 8);
1751 	sdp_io_write(sd, 0xe3, c->A2);
1752 	sdp_io_write_and_or(sd, 0xe4, 0xe0, c->A3 >> 8);
1753 	sdp_io_write(sd, 0xe5, c->A3);
1754 
1755 	/* A scale */
1756 	sdp_io_write_and_or(sd, 0xe6, 0x80, c->A4 >> 8);
1757 	sdp_io_write(sd, 0xe7, c->A4);
1758 
1759 	/* B coeff */
1760 	sdp_io_write_and_or(sd, 0xe8, 0xe0, c->B1 >> 8);
1761 	sdp_io_write(sd, 0xe9, c->B1);
1762 	sdp_io_write_and_or(sd, 0xea, 0xe0, c->B2 >> 8);
1763 	sdp_io_write(sd, 0xeb, c->B2);
1764 	sdp_io_write_and_or(sd, 0xec, 0xe0, c->B3 >> 8);
1765 	sdp_io_write(sd, 0xed, c->B3);
1766 
1767 	/* B scale */
1768 	sdp_io_write_and_or(sd, 0xee, 0x80, c->B4 >> 8);
1769 	sdp_io_write(sd, 0xef, c->B4);
1770 
1771 	/* C coeff */
1772 	sdp_io_write_and_or(sd, 0xf0, 0xe0, c->C1 >> 8);
1773 	sdp_io_write(sd, 0xf1, c->C1);
1774 	sdp_io_write_and_or(sd, 0xf2, 0xe0, c->C2 >> 8);
1775 	sdp_io_write(sd, 0xf3, c->C2);
1776 	sdp_io_write_and_or(sd, 0xf4, 0xe0, c->C3 >> 8);
1777 	sdp_io_write(sd, 0xf5, c->C3);
1778 
1779 	/* C scale */
1780 	sdp_io_write_and_or(sd, 0xf6, 0x80, c->C4 >> 8);
1781 	sdp_io_write(sd, 0xf7, c->C4);
1782 }
1783 
select_input(struct v4l2_subdev * sd,enum adv7842_vid_std_select vid_std_select)1784 static void select_input(struct v4l2_subdev *sd,
1785 			 enum adv7842_vid_std_select vid_std_select)
1786 {
1787 	struct adv7842_state *state = to_state(sd);
1788 
1789 	switch (state->mode) {
1790 	case ADV7842_MODE_SDP:
1791 		io_write(sd, 0x00, vid_std_select); /* video std: CVBS or YC mode */
1792 		io_write(sd, 0x01, 0); /* prim mode */
1793 		/* enable embedded syncs for auto graphics mode */
1794 		cp_write_and_or(sd, 0x81, 0xef, 0x10);
1795 
1796 		afe_write(sd, 0x00, 0x00); /* power up ADC */
1797 		afe_write(sd, 0xc8, 0x00); /* phase control */
1798 
1799 		io_write(sd, 0xdd, 0x90); /* Manual 2x output clock */
1800 		/* script says register 0xde, which don't exist in manual */
1801 
1802 		/* Manual analog input muxing mode, CVBS (6.4)*/
1803 		afe_write_and_or(sd, 0x02, 0x7f, 0x80);
1804 		if (vid_std_select == ADV7842_SDP_VID_STD_CVBS_SD_4x1) {
1805 			afe_write(sd, 0x03, 0xa0); /* ADC0 to AIN10 (CVBS), ADC1 N/C*/
1806 			afe_write(sd, 0x04, 0x00); /* ADC2 N/C,ADC3 N/C*/
1807 		} else {
1808 			afe_write(sd, 0x03, 0xa0); /* ADC0 to AIN10 (CVBS), ADC1 N/C*/
1809 			afe_write(sd, 0x04, 0xc0); /* ADC2 to AIN12, ADC3 N/C*/
1810 		}
1811 		afe_write(sd, 0x0c, 0x1f); /* ADI recommend write */
1812 		afe_write(sd, 0x12, 0x63); /* ADI recommend write */
1813 
1814 		sdp_io_write(sd, 0xb2, 0x60); /* Disable AV codes */
1815 		sdp_io_write(sd, 0xc8, 0xe3); /* Disable Ancillary data */
1816 
1817 		/* SDP recommended settings */
1818 		sdp_write(sd, 0x00, 0x3F); /* Autodetect PAL NTSC (not SECAM) */
1819 		sdp_write(sd, 0x01, 0x00); /* Pedestal Off */
1820 
1821 		sdp_write(sd, 0x03, 0xE4); /* Manual VCR Gain Luma 0x40B */
1822 		sdp_write(sd, 0x04, 0x0B); /* Manual Luma setting */
1823 		sdp_write(sd, 0x05, 0xC3); /* Manual Chroma setting 0x3FE */
1824 		sdp_write(sd, 0x06, 0xFE); /* Manual Chroma setting */
1825 		sdp_write(sd, 0x12, 0x0D); /* Frame TBC,I_P, 3D comb enabled */
1826 		sdp_write(sd, 0xA7, 0x00); /* ADI Recommended Write */
1827 		sdp_io_write(sd, 0xB0, 0x00); /* Disable H and v blanking */
1828 
1829 		/* deinterlacer enabled and 3D comb */
1830 		sdp_write_and_or(sd, 0x12, 0xf6, 0x09);
1831 
1832 		break;
1833 
1834 	case ADV7842_MODE_COMP:
1835 	case ADV7842_MODE_RGB:
1836 		/* Automatic analog input muxing mode */
1837 		afe_write_and_or(sd, 0x02, 0x7f, 0x00);
1838 		/* set mode and select free run resolution */
1839 		io_write(sd, 0x00, vid_std_select); /* video std */
1840 		io_write(sd, 0x01, 0x02); /* prim mode */
1841 		cp_write_and_or(sd, 0x81, 0xef, 0x10); /* enable embedded syncs
1842 							  for auto graphics mode */
1843 
1844 		afe_write(sd, 0x00, 0x00); /* power up ADC */
1845 		afe_write(sd, 0xc8, 0x00); /* phase control */
1846 		if (state->mode == ADV7842_MODE_COMP) {
1847 			/* force to YCrCb */
1848 			io_write_and_or(sd, 0x02, 0x0f, 0x60);
1849 		} else {
1850 			/* force to RGB */
1851 			io_write_and_or(sd, 0x02, 0x0f, 0x10);
1852 		}
1853 
1854 		/* set ADI recommended settings for digitizer */
1855 		/* "ADV7842 Register Settings Recommendations
1856 		 * (rev. 1.8, November 2010)" p. 9. */
1857 		afe_write(sd, 0x0c, 0x1f); /* ADC Range improvement */
1858 		afe_write(sd, 0x12, 0x63); /* ADC Range improvement */
1859 
1860 		/* set to default gain for RGB */
1861 		cp_write(sd, 0x73, 0x10);
1862 		cp_write(sd, 0x74, 0x04);
1863 		cp_write(sd, 0x75, 0x01);
1864 		cp_write(sd, 0x76, 0x00);
1865 
1866 		cp_write(sd, 0x3e, 0x04); /* CP core pre-gain control */
1867 		cp_write(sd, 0xc3, 0x39); /* CP coast control. Graphics mode */
1868 		cp_write(sd, 0x40, 0x5c); /* CP core pre-gain control. Graphics mode */
1869 		break;
1870 
1871 	case ADV7842_MODE_HDMI:
1872 		/* Automatic analog input muxing mode */
1873 		afe_write_and_or(sd, 0x02, 0x7f, 0x00);
1874 		/* set mode and select free run resolution */
1875 		if (state->hdmi_port_a)
1876 			hdmi_write(sd, 0x00, 0x02); /* select port A */
1877 		else
1878 			hdmi_write(sd, 0x00, 0x03); /* select port B */
1879 		io_write(sd, 0x00, vid_std_select); /* video std */
1880 		io_write(sd, 0x01, 5); /* prim mode */
1881 		cp_write_and_or(sd, 0x81, 0xef, 0x00); /* disable embedded syncs
1882 							  for auto graphics mode */
1883 
1884 		/* set ADI recommended settings for HDMI: */
1885 		/* "ADV7842 Register Settings Recommendations
1886 		 * (rev. 1.8, November 2010)" p. 3. */
1887 		hdmi_write(sd, 0xc0, 0x00);
1888 		hdmi_write(sd, 0x0d, 0x34); /* ADI recommended write */
1889 		hdmi_write(sd, 0x3d, 0x10); /* ADI recommended write */
1890 		hdmi_write(sd, 0x44, 0x85); /* TMDS PLL optimization */
1891 		hdmi_write(sd, 0x46, 0x1f); /* ADI recommended write */
1892 		hdmi_write(sd, 0x57, 0xb6); /* TMDS PLL optimization */
1893 		hdmi_write(sd, 0x58, 0x03); /* TMDS PLL optimization */
1894 		hdmi_write(sd, 0x60, 0x88); /* TMDS PLL optimization */
1895 		hdmi_write(sd, 0x61, 0x88); /* TMDS PLL optimization */
1896 		hdmi_write(sd, 0x6c, 0x18); /* Disable ISRC clearing bit,
1897 					       Improve robustness */
1898 		hdmi_write(sd, 0x75, 0x10); /* DDC drive strength */
1899 		hdmi_write(sd, 0x85, 0x1f); /* equaliser */
1900 		hdmi_write(sd, 0x87, 0x70); /* ADI recommended write */
1901 		hdmi_write(sd, 0x89, 0x04); /* equaliser */
1902 		hdmi_write(sd, 0x8a, 0x1e); /* equaliser */
1903 		hdmi_write(sd, 0x93, 0x04); /* equaliser */
1904 		hdmi_write(sd, 0x94, 0x1e); /* equaliser */
1905 		hdmi_write(sd, 0x99, 0xa1); /* ADI recommended write */
1906 		hdmi_write(sd, 0x9b, 0x09); /* ADI recommended write */
1907 		hdmi_write(sd, 0x9d, 0x02); /* equaliser */
1908 
1909 		afe_write(sd, 0x00, 0xff); /* power down ADC */
1910 		afe_write(sd, 0xc8, 0x40); /* phase control */
1911 
1912 		/* set to default gain for HDMI */
1913 		cp_write(sd, 0x73, 0x10);
1914 		cp_write(sd, 0x74, 0x04);
1915 		cp_write(sd, 0x75, 0x01);
1916 		cp_write(sd, 0x76, 0x00);
1917 
1918 		/* reset ADI recommended settings for digitizer */
1919 		/* "ADV7842 Register Settings Recommendations
1920 		 * (rev. 2.5, June 2010)" p. 17. */
1921 		afe_write(sd, 0x12, 0xfb); /* ADC noise shaping filter controls */
1922 		afe_write(sd, 0x0c, 0x0d); /* CP core gain controls */
1923 		cp_write(sd, 0x3e, 0x00); /* CP core pre-gain control */
1924 
1925 		/* CP coast control */
1926 		cp_write(sd, 0xc3, 0x33); /* Component mode */
1927 
1928 		/* color space conversion, autodetect color space */
1929 		io_write_and_or(sd, 0x02, 0x0f, 0xf0);
1930 		break;
1931 
1932 	default:
1933 		v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
1934 			 __func__, state->mode);
1935 		break;
1936 	}
1937 }
1938 
adv7842_s_routing(struct v4l2_subdev * sd,u32 input,u32 output,u32 config)1939 static int adv7842_s_routing(struct v4l2_subdev *sd,
1940 		u32 input, u32 output, u32 config)
1941 {
1942 	struct adv7842_state *state = to_state(sd);
1943 
1944 	v4l2_dbg(2, debug, sd, "%s: input %d\n", __func__, input);
1945 
1946 	switch (input) {
1947 	case ADV7842_SELECT_HDMI_PORT_A:
1948 		state->mode = ADV7842_MODE_HDMI;
1949 		state->vid_std_select = ADV7842_HDMI_COMP_VID_STD_HD_1250P;
1950 		state->hdmi_port_a = true;
1951 		break;
1952 	case ADV7842_SELECT_HDMI_PORT_B:
1953 		state->mode = ADV7842_MODE_HDMI;
1954 		state->vid_std_select = ADV7842_HDMI_COMP_VID_STD_HD_1250P;
1955 		state->hdmi_port_a = false;
1956 		break;
1957 	case ADV7842_SELECT_VGA_COMP:
1958 		state->mode = ADV7842_MODE_COMP;
1959 		state->vid_std_select = ADV7842_RGB_VID_STD_AUTO_GRAPH_MODE;
1960 		break;
1961 	case ADV7842_SELECT_VGA_RGB:
1962 		state->mode = ADV7842_MODE_RGB;
1963 		state->vid_std_select = ADV7842_RGB_VID_STD_AUTO_GRAPH_MODE;
1964 		break;
1965 	case ADV7842_SELECT_SDP_CVBS:
1966 		state->mode = ADV7842_MODE_SDP;
1967 		state->vid_std_select = ADV7842_SDP_VID_STD_CVBS_SD_4x1;
1968 		break;
1969 	case ADV7842_SELECT_SDP_YC:
1970 		state->mode = ADV7842_MODE_SDP;
1971 		state->vid_std_select = ADV7842_SDP_VID_STD_YC_SD4_x1;
1972 		break;
1973 	default:
1974 		return -EINVAL;
1975 	}
1976 
1977 	disable_input(sd);
1978 	select_input(sd, state->vid_std_select);
1979 	enable_input(sd);
1980 
1981 	v4l2_subdev_notify_event(sd, &adv7842_ev_fmt);
1982 
1983 	return 0;
1984 }
1985 
adv7842_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_mbus_code_enum * code)1986 static int adv7842_enum_mbus_code(struct v4l2_subdev *sd,
1987 		struct v4l2_subdev_state *sd_state,
1988 		struct v4l2_subdev_mbus_code_enum *code)
1989 {
1990 	if (code->index >= ARRAY_SIZE(adv7842_formats))
1991 		return -EINVAL;
1992 	code->code = adv7842_formats[code->index].code;
1993 	return 0;
1994 }
1995 
adv7842_fill_format(struct adv7842_state * state,struct v4l2_mbus_framefmt * format)1996 static void adv7842_fill_format(struct adv7842_state *state,
1997 				struct v4l2_mbus_framefmt *format)
1998 {
1999 	memset(format, 0, sizeof(*format));
2000 
2001 	format->width = state->timings.bt.width;
2002 	format->height = state->timings.bt.height;
2003 	format->field = V4L2_FIELD_NONE;
2004 	format->colorspace = V4L2_COLORSPACE_SRGB;
2005 
2006 	if (state->timings.bt.flags & V4L2_DV_FL_IS_CE_VIDEO)
2007 		format->colorspace = (state->timings.bt.height <= 576) ?
2008 			V4L2_COLORSPACE_SMPTE170M : V4L2_COLORSPACE_REC709;
2009 }
2010 
2011 /*
2012  * Compute the op_ch_sel value required to obtain on the bus the component order
2013  * corresponding to the selected format taking into account bus reordering
2014  * applied by the board at the output of the device.
2015  *
2016  * The following table gives the op_ch_value from the format component order
2017  * (expressed as op_ch_sel value in column) and the bus reordering (expressed as
2018  * adv7842_bus_order value in row).
2019  *
2020  *           |	GBR(0)	GRB(1)	BGR(2)	RGB(3)	BRG(4)	RBG(5)
2021  * ----------+-------------------------------------------------
2022  * RGB (NOP) |	GBR	GRB	BGR	RGB	BRG	RBG
2023  * GRB (1-2) |	BGR	RGB	GBR	GRB	RBG	BRG
2024  * RBG (2-3) |	GRB	GBR	BRG	RBG	BGR	RGB
2025  * BGR (1-3) |	RBG	BRG	RGB	BGR	GRB	GBR
2026  * BRG (ROR) |	BRG	RBG	GRB	GBR	RGB	BGR
2027  * GBR (ROL) |	RGB	BGR	RBG	BRG	GBR	GRB
2028  */
adv7842_op_ch_sel(struct adv7842_state * state)2029 static unsigned int adv7842_op_ch_sel(struct adv7842_state *state)
2030 {
2031 #define _SEL(a, b, c, d, e, f)	{ \
2032 	ADV7842_OP_CH_SEL_##a, ADV7842_OP_CH_SEL_##b, ADV7842_OP_CH_SEL_##c, \
2033 	ADV7842_OP_CH_SEL_##d, ADV7842_OP_CH_SEL_##e, ADV7842_OP_CH_SEL_##f }
2034 #define _BUS(x)			[ADV7842_BUS_ORDER_##x]
2035 
2036 	static const unsigned int op_ch_sel[6][6] = {
2037 		_BUS(RGB) /* NOP */ = _SEL(GBR, GRB, BGR, RGB, BRG, RBG),
2038 		_BUS(GRB) /* 1-2 */ = _SEL(BGR, RGB, GBR, GRB, RBG, BRG),
2039 		_BUS(RBG) /* 2-3 */ = _SEL(GRB, GBR, BRG, RBG, BGR, RGB),
2040 		_BUS(BGR) /* 1-3 */ = _SEL(RBG, BRG, RGB, BGR, GRB, GBR),
2041 		_BUS(BRG) /* ROR */ = _SEL(BRG, RBG, GRB, GBR, RGB, BGR),
2042 		_BUS(GBR) /* ROL */ = _SEL(RGB, BGR, RBG, BRG, GBR, GRB),
2043 	};
2044 
2045 	return op_ch_sel[state->pdata.bus_order][state->format->op_ch_sel >> 5];
2046 }
2047 
adv7842_setup_format(struct adv7842_state * state)2048 static void adv7842_setup_format(struct adv7842_state *state)
2049 {
2050 	struct v4l2_subdev *sd = &state->sd;
2051 
2052 	io_write_clr_set(sd, 0x02, 0x02,
2053 			state->format->rgb_out ? ADV7842_RGB_OUT : 0);
2054 	io_write(sd, 0x03, state->format->op_format_sel |
2055 		 state->pdata.op_format_mode_sel);
2056 	io_write_clr_set(sd, 0x04, 0xe0, adv7842_op_ch_sel(state));
2057 	io_write_clr_set(sd, 0x05, 0x01,
2058 			state->format->swap_cb_cr ? ADV7842_OP_SWAP_CB_CR : 0);
2059 	set_rgb_quantization_range(sd);
2060 }
2061 
adv7842_get_format(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * format)2062 static int adv7842_get_format(struct v4l2_subdev *sd,
2063 			      struct v4l2_subdev_state *sd_state,
2064 			      struct v4l2_subdev_format *format)
2065 {
2066 	struct adv7842_state *state = to_state(sd);
2067 
2068 	if (format->pad != ADV7842_PAD_SOURCE)
2069 		return -EINVAL;
2070 
2071 	if (state->mode == ADV7842_MODE_SDP) {
2072 		/* SPD block */
2073 		if (!(sdp_read(sd, 0x5a) & 0x01))
2074 			return -EINVAL;
2075 		format->format.code = MEDIA_BUS_FMT_YUYV8_2X8;
2076 		format->format.width = 720;
2077 		/* valid signal */
2078 		if (state->norm & V4L2_STD_525_60)
2079 			format->format.height = 480;
2080 		else
2081 			format->format.height = 576;
2082 		format->format.colorspace = V4L2_COLORSPACE_SMPTE170M;
2083 		return 0;
2084 	}
2085 
2086 	adv7842_fill_format(state, &format->format);
2087 
2088 	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
2089 		struct v4l2_mbus_framefmt *fmt;
2090 
2091 		fmt = v4l2_subdev_get_try_format(sd, sd_state, format->pad);
2092 		format->format.code = fmt->code;
2093 	} else {
2094 		format->format.code = state->format->code;
2095 	}
2096 
2097 	return 0;
2098 }
2099 
adv7842_set_format(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * format)2100 static int adv7842_set_format(struct v4l2_subdev *sd,
2101 			      struct v4l2_subdev_state *sd_state,
2102 			      struct v4l2_subdev_format *format)
2103 {
2104 	struct adv7842_state *state = to_state(sd);
2105 	const struct adv7842_format_info *info;
2106 
2107 	if (format->pad != ADV7842_PAD_SOURCE)
2108 		return -EINVAL;
2109 
2110 	if (state->mode == ADV7842_MODE_SDP)
2111 		return adv7842_get_format(sd, sd_state, format);
2112 
2113 	info = adv7842_format_info(state, format->format.code);
2114 	if (info == NULL)
2115 		info = adv7842_format_info(state, MEDIA_BUS_FMT_YUYV8_2X8);
2116 
2117 	adv7842_fill_format(state, &format->format);
2118 	format->format.code = info->code;
2119 
2120 	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
2121 		struct v4l2_mbus_framefmt *fmt;
2122 
2123 		fmt = v4l2_subdev_get_try_format(sd, sd_state, format->pad);
2124 		fmt->code = format->format.code;
2125 	} else {
2126 		state->format = info;
2127 		adv7842_setup_format(state);
2128 	}
2129 
2130 	return 0;
2131 }
2132 
adv7842_irq_enable(struct v4l2_subdev * sd,bool enable)2133 static void adv7842_irq_enable(struct v4l2_subdev *sd, bool enable)
2134 {
2135 	if (enable) {
2136 		/* Enable SSPD, STDI and CP locked/unlocked interrupts */
2137 		io_write(sd, 0x46, 0x9c);
2138 		/* ESDP_50HZ_DET interrupt */
2139 		io_write(sd, 0x5a, 0x10);
2140 		/* Enable CABLE_DET_A/B_ST (+5v) interrupt */
2141 		io_write(sd, 0x73, 0x03);
2142 		/* Enable V_LOCKED and DE_REGEN_LCK interrupts */
2143 		io_write(sd, 0x78, 0x03);
2144 		/* Enable SDP Standard Detection Change and SDP Video Detected */
2145 		io_write(sd, 0xa0, 0x09);
2146 		/* Enable HDMI_MODE interrupt */
2147 		io_write(sd, 0x69, 0x08);
2148 	} else {
2149 		io_write(sd, 0x46, 0x0);
2150 		io_write(sd, 0x5a, 0x0);
2151 		io_write(sd, 0x73, 0x0);
2152 		io_write(sd, 0x78, 0x0);
2153 		io_write(sd, 0xa0, 0x0);
2154 		io_write(sd, 0x69, 0x0);
2155 	}
2156 }
2157 
2158 #if IS_ENABLED(CONFIG_VIDEO_ADV7842_CEC)
adv7842_cec_tx_raw_status(struct v4l2_subdev * sd,u8 tx_raw_status)2159 static void adv7842_cec_tx_raw_status(struct v4l2_subdev *sd, u8 tx_raw_status)
2160 {
2161 	struct adv7842_state *state = to_state(sd);
2162 
2163 	if ((cec_read(sd, 0x11) & 0x01) == 0) {
2164 		v4l2_dbg(1, debug, sd, "%s: tx raw: tx disabled\n", __func__);
2165 		return;
2166 	}
2167 
2168 	if (tx_raw_status & 0x02) {
2169 		v4l2_dbg(1, debug, sd, "%s: tx raw: arbitration lost\n",
2170 			 __func__);
2171 		cec_transmit_done(state->cec_adap, CEC_TX_STATUS_ARB_LOST,
2172 				  1, 0, 0, 0);
2173 		return;
2174 	}
2175 	if (tx_raw_status & 0x04) {
2176 		u8 status;
2177 		u8 nack_cnt;
2178 		u8 low_drive_cnt;
2179 
2180 		v4l2_dbg(1, debug, sd, "%s: tx raw: retry failed\n", __func__);
2181 		/*
2182 		 * We set this status bit since this hardware performs
2183 		 * retransmissions.
2184 		 */
2185 		status = CEC_TX_STATUS_MAX_RETRIES;
2186 		nack_cnt = cec_read(sd, 0x14) & 0xf;
2187 		if (nack_cnt)
2188 			status |= CEC_TX_STATUS_NACK;
2189 		low_drive_cnt = cec_read(sd, 0x14) >> 4;
2190 		if (low_drive_cnt)
2191 			status |= CEC_TX_STATUS_LOW_DRIVE;
2192 		cec_transmit_done(state->cec_adap, status,
2193 				  0, nack_cnt, low_drive_cnt, 0);
2194 		return;
2195 	}
2196 	if (tx_raw_status & 0x01) {
2197 		v4l2_dbg(1, debug, sd, "%s: tx raw: ready ok\n", __func__);
2198 		cec_transmit_done(state->cec_adap, CEC_TX_STATUS_OK, 0, 0, 0, 0);
2199 		return;
2200 	}
2201 }
2202 
adv7842_cec_isr(struct v4l2_subdev * sd,bool * handled)2203 static void adv7842_cec_isr(struct v4l2_subdev *sd, bool *handled)
2204 {
2205 	u8 cec_irq;
2206 
2207 	/* cec controller */
2208 	cec_irq = io_read(sd, 0x93) & 0x0f;
2209 	if (!cec_irq)
2210 		return;
2211 
2212 	v4l2_dbg(1, debug, sd, "%s: cec: irq 0x%x\n", __func__, cec_irq);
2213 	adv7842_cec_tx_raw_status(sd, cec_irq);
2214 	if (cec_irq & 0x08) {
2215 		struct adv7842_state *state = to_state(sd);
2216 		struct cec_msg msg;
2217 
2218 		msg.len = cec_read(sd, 0x25) & 0x1f;
2219 		if (msg.len > CEC_MAX_MSG_SIZE)
2220 			msg.len = CEC_MAX_MSG_SIZE;
2221 
2222 		if (msg.len) {
2223 			u8 i;
2224 
2225 			for (i = 0; i < msg.len; i++)
2226 				msg.msg[i] = cec_read(sd, i + 0x15);
2227 			cec_write(sd, 0x26, 0x01); /* re-enable rx */
2228 			cec_received_msg(state->cec_adap, &msg);
2229 		}
2230 	}
2231 
2232 	io_write(sd, 0x94, cec_irq);
2233 
2234 	if (handled)
2235 		*handled = true;
2236 }
2237 
adv7842_cec_adap_enable(struct cec_adapter * adap,bool enable)2238 static int adv7842_cec_adap_enable(struct cec_adapter *adap, bool enable)
2239 {
2240 	struct adv7842_state *state = cec_get_drvdata(adap);
2241 	struct v4l2_subdev *sd = &state->sd;
2242 
2243 	if (!state->cec_enabled_adap && enable) {
2244 		cec_write_clr_set(sd, 0x2a, 0x01, 0x01); /* power up cec */
2245 		cec_write(sd, 0x2c, 0x01);	/* cec soft reset */
2246 		cec_write_clr_set(sd, 0x11, 0x01, 0); /* initially disable tx */
2247 		/* enabled irqs: */
2248 		/* tx: ready */
2249 		/* tx: arbitration lost */
2250 		/* tx: retry timeout */
2251 		/* rx: ready */
2252 		io_write_clr_set(sd, 0x96, 0x0f, 0x0f);
2253 		cec_write(sd, 0x26, 0x01);            /* enable rx */
2254 	} else if (state->cec_enabled_adap && !enable) {
2255 		/* disable cec interrupts */
2256 		io_write_clr_set(sd, 0x96, 0x0f, 0x00);
2257 		/* disable address mask 1-3 */
2258 		cec_write_clr_set(sd, 0x27, 0x70, 0x00);
2259 		/* power down cec section */
2260 		cec_write_clr_set(sd, 0x2a, 0x01, 0x00);
2261 		state->cec_valid_addrs = 0;
2262 	}
2263 	state->cec_enabled_adap = enable;
2264 	return 0;
2265 }
2266 
adv7842_cec_adap_log_addr(struct cec_adapter * adap,u8 addr)2267 static int adv7842_cec_adap_log_addr(struct cec_adapter *adap, u8 addr)
2268 {
2269 	struct adv7842_state *state = cec_get_drvdata(adap);
2270 	struct v4l2_subdev *sd = &state->sd;
2271 	unsigned int i, free_idx = ADV7842_MAX_ADDRS;
2272 
2273 	if (!state->cec_enabled_adap)
2274 		return addr == CEC_LOG_ADDR_INVALID ? 0 : -EIO;
2275 
2276 	if (addr == CEC_LOG_ADDR_INVALID) {
2277 		cec_write_clr_set(sd, 0x27, 0x70, 0);
2278 		state->cec_valid_addrs = 0;
2279 		return 0;
2280 	}
2281 
2282 	for (i = 0; i < ADV7842_MAX_ADDRS; i++) {
2283 		bool is_valid = state->cec_valid_addrs & (1 << i);
2284 
2285 		if (free_idx == ADV7842_MAX_ADDRS && !is_valid)
2286 			free_idx = i;
2287 		if (is_valid && state->cec_addr[i] == addr)
2288 			return 0;
2289 	}
2290 	if (i == ADV7842_MAX_ADDRS) {
2291 		i = free_idx;
2292 		if (i == ADV7842_MAX_ADDRS)
2293 			return -ENXIO;
2294 	}
2295 	state->cec_addr[i] = addr;
2296 	state->cec_valid_addrs |= 1 << i;
2297 
2298 	switch (i) {
2299 	case 0:
2300 		/* enable address mask 0 */
2301 		cec_write_clr_set(sd, 0x27, 0x10, 0x10);
2302 		/* set address for mask 0 */
2303 		cec_write_clr_set(sd, 0x28, 0x0f, addr);
2304 		break;
2305 	case 1:
2306 		/* enable address mask 1 */
2307 		cec_write_clr_set(sd, 0x27, 0x20, 0x20);
2308 		/* set address for mask 1 */
2309 		cec_write_clr_set(sd, 0x28, 0xf0, addr << 4);
2310 		break;
2311 	case 2:
2312 		/* enable address mask 2 */
2313 		cec_write_clr_set(sd, 0x27, 0x40, 0x40);
2314 		/* set address for mask 1 */
2315 		cec_write_clr_set(sd, 0x29, 0x0f, addr);
2316 		break;
2317 	}
2318 	return 0;
2319 }
2320 
adv7842_cec_adap_transmit(struct cec_adapter * adap,u8 attempts,u32 signal_free_time,struct cec_msg * msg)2321 static int adv7842_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
2322 				     u32 signal_free_time, struct cec_msg *msg)
2323 {
2324 	struct adv7842_state *state = cec_get_drvdata(adap);
2325 	struct v4l2_subdev *sd = &state->sd;
2326 	u8 len = msg->len;
2327 	unsigned int i;
2328 
2329 	/*
2330 	 * The number of retries is the number of attempts - 1, but retry
2331 	 * at least once. It's not clear if a value of 0 is allowed, so
2332 	 * let's do at least one retry.
2333 	 */
2334 	cec_write_clr_set(sd, 0x12, 0x70, max(1, attempts - 1) << 4);
2335 
2336 	if (len > 16) {
2337 		v4l2_err(sd, "%s: len exceeded 16 (%d)\n", __func__, len);
2338 		return -EINVAL;
2339 	}
2340 
2341 	/* write data */
2342 	for (i = 0; i < len; i++)
2343 		cec_write(sd, i, msg->msg[i]);
2344 
2345 	/* set length (data + header) */
2346 	cec_write(sd, 0x10, len);
2347 	/* start transmit, enable tx */
2348 	cec_write(sd, 0x11, 0x01);
2349 	return 0;
2350 }
2351 
2352 static const struct cec_adap_ops adv7842_cec_adap_ops = {
2353 	.adap_enable = adv7842_cec_adap_enable,
2354 	.adap_log_addr = adv7842_cec_adap_log_addr,
2355 	.adap_transmit = adv7842_cec_adap_transmit,
2356 };
2357 #endif
2358 
adv7842_isr(struct v4l2_subdev * sd,u32 status,bool * handled)2359 static int adv7842_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
2360 {
2361 	struct adv7842_state *state = to_state(sd);
2362 	u8 fmt_change_cp, fmt_change_digital, fmt_change_sdp;
2363 	u8 irq_status[6];
2364 
2365 	adv7842_irq_enable(sd, false);
2366 
2367 	/* read status */
2368 	irq_status[0] = io_read(sd, 0x43);
2369 	irq_status[1] = io_read(sd, 0x57);
2370 	irq_status[2] = io_read(sd, 0x70);
2371 	irq_status[3] = io_read(sd, 0x75);
2372 	irq_status[4] = io_read(sd, 0x9d);
2373 	irq_status[5] = io_read(sd, 0x66);
2374 
2375 	/* and clear */
2376 	if (irq_status[0])
2377 		io_write(sd, 0x44, irq_status[0]);
2378 	if (irq_status[1])
2379 		io_write(sd, 0x58, irq_status[1]);
2380 	if (irq_status[2])
2381 		io_write(sd, 0x71, irq_status[2]);
2382 	if (irq_status[3])
2383 		io_write(sd, 0x76, irq_status[3]);
2384 	if (irq_status[4])
2385 		io_write(sd, 0x9e, irq_status[4]);
2386 	if (irq_status[5])
2387 		io_write(sd, 0x67, irq_status[5]);
2388 
2389 	adv7842_irq_enable(sd, true);
2390 
2391 	v4l2_dbg(1, debug, sd, "%s: irq %x, %x, %x, %x, %x, %x\n", __func__,
2392 		 irq_status[0], irq_status[1], irq_status[2],
2393 		 irq_status[3], irq_status[4], irq_status[5]);
2394 
2395 	/* format change CP */
2396 	fmt_change_cp = irq_status[0] & 0x9c;
2397 
2398 	/* format change SDP */
2399 	if (state->mode == ADV7842_MODE_SDP)
2400 		fmt_change_sdp = (irq_status[1] & 0x30) | (irq_status[4] & 0x09);
2401 	else
2402 		fmt_change_sdp = 0;
2403 
2404 	/* digital format CP */
2405 	if (is_digital_input(sd))
2406 		fmt_change_digital = irq_status[3] & 0x03;
2407 	else
2408 		fmt_change_digital = 0;
2409 
2410 	/* format change */
2411 	if (fmt_change_cp || fmt_change_digital || fmt_change_sdp) {
2412 		v4l2_dbg(1, debug, sd,
2413 			 "%s: fmt_change_cp = 0x%x, fmt_change_digital = 0x%x, fmt_change_sdp = 0x%x\n",
2414 			 __func__, fmt_change_cp, fmt_change_digital,
2415 			 fmt_change_sdp);
2416 		v4l2_subdev_notify_event(sd, &adv7842_ev_fmt);
2417 		if (handled)
2418 			*handled = true;
2419 	}
2420 
2421 	/* HDMI/DVI mode */
2422 	if (irq_status[5] & 0x08) {
2423 		v4l2_dbg(1, debug, sd, "%s: irq %s mode\n", __func__,
2424 			 (io_read(sd, 0x65) & 0x08) ? "HDMI" : "DVI");
2425 		set_rgb_quantization_range(sd);
2426 		if (handled)
2427 			*handled = true;
2428 	}
2429 
2430 #if IS_ENABLED(CONFIG_VIDEO_ADV7842_CEC)
2431 	/* cec */
2432 	adv7842_cec_isr(sd, handled);
2433 #endif
2434 
2435 	/* tx 5v detect */
2436 	if (irq_status[2] & 0x3) {
2437 		v4l2_dbg(1, debug, sd, "%s: irq tx_5v\n", __func__);
2438 		adv7842_s_detect_tx_5v_ctrl(sd);
2439 		if (handled)
2440 			*handled = true;
2441 	}
2442 	return 0;
2443 }
2444 
adv7842_get_edid(struct v4l2_subdev * sd,struct v4l2_edid * edid)2445 static int adv7842_get_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
2446 {
2447 	struct adv7842_state *state = to_state(sd);
2448 	u32 blocks = 0;
2449 	u8 *data = NULL;
2450 
2451 	memset(edid->reserved, 0, sizeof(edid->reserved));
2452 
2453 	switch (edid->pad) {
2454 	case ADV7842_EDID_PORT_A:
2455 	case ADV7842_EDID_PORT_B:
2456 		if (state->hdmi_edid.present & (0x04 << edid->pad)) {
2457 			data = state->hdmi_edid.edid;
2458 			blocks = state->hdmi_edid.blocks;
2459 		}
2460 		break;
2461 	case ADV7842_EDID_PORT_VGA:
2462 		if (state->vga_edid.present) {
2463 			data = state->vga_edid.edid;
2464 			blocks = state->vga_edid.blocks;
2465 		}
2466 		break;
2467 	default:
2468 		return -EINVAL;
2469 	}
2470 
2471 	if (edid->start_block == 0 && edid->blocks == 0) {
2472 		edid->blocks = blocks;
2473 		return 0;
2474 	}
2475 
2476 	if (!data)
2477 		return -ENODATA;
2478 
2479 	if (edid->start_block >= blocks)
2480 		return -EINVAL;
2481 
2482 	if (edid->start_block + edid->blocks > blocks)
2483 		edid->blocks = blocks - edid->start_block;
2484 
2485 	memcpy(edid->edid, data + edid->start_block * 128, edid->blocks * 128);
2486 
2487 	return 0;
2488 }
2489 
2490 /*
2491  * If the VGA_EDID_ENABLE bit is set (Repeater Map 0x7f, bit 7), then
2492  * the first two blocks of the EDID are for the HDMI, and the first block
2493  * of segment 1 (i.e. the third block of the EDID) is for VGA.
2494  * So if a VGA EDID is installed, then the maximum size of the HDMI EDID
2495  * is 2 blocks.
2496  */
adv7842_set_edid(struct v4l2_subdev * sd,struct v4l2_edid * e)2497 static int adv7842_set_edid(struct v4l2_subdev *sd, struct v4l2_edid *e)
2498 {
2499 	struct adv7842_state *state = to_state(sd);
2500 	unsigned int max_blocks = e->pad == ADV7842_EDID_PORT_VGA ? 1 : 4;
2501 	int err = 0;
2502 
2503 	memset(e->reserved, 0, sizeof(e->reserved));
2504 
2505 	if (e->pad > ADV7842_EDID_PORT_VGA)
2506 		return -EINVAL;
2507 	if (e->start_block != 0)
2508 		return -EINVAL;
2509 	if (e->pad < ADV7842_EDID_PORT_VGA && state->vga_edid.blocks)
2510 		max_blocks = 2;
2511 	if (e->pad == ADV7842_EDID_PORT_VGA && state->hdmi_edid.blocks > 2)
2512 		return -EBUSY;
2513 	if (e->blocks > max_blocks) {
2514 		e->blocks = max_blocks;
2515 		return -E2BIG;
2516 	}
2517 
2518 	/* todo, per edid */
2519 	if (e->blocks)
2520 		state->aspect_ratio = v4l2_calc_aspect_ratio(e->edid[0x15],
2521 							     e->edid[0x16]);
2522 
2523 	switch (e->pad) {
2524 	case ADV7842_EDID_PORT_VGA:
2525 		memset(state->vga_edid.edid, 0, sizeof(state->vga_edid.edid));
2526 		state->vga_edid.blocks = e->blocks;
2527 		state->vga_edid.present = e->blocks ? 0x1 : 0x0;
2528 		if (e->blocks)
2529 			memcpy(state->vga_edid.edid, e->edid, 128);
2530 		err = edid_write_vga_segment(sd);
2531 		break;
2532 	case ADV7842_EDID_PORT_A:
2533 	case ADV7842_EDID_PORT_B:
2534 		memset(state->hdmi_edid.edid, 0, sizeof(state->hdmi_edid.edid));
2535 		state->hdmi_edid.blocks = e->blocks;
2536 		if (e->blocks) {
2537 			state->hdmi_edid.present |= 0x04 << e->pad;
2538 			memcpy(state->hdmi_edid.edid, e->edid, 128 * e->blocks);
2539 		} else {
2540 			state->hdmi_edid.present &= ~(0x04 << e->pad);
2541 			adv7842_s_detect_tx_5v_ctrl(sd);
2542 		}
2543 		err = edid_write_hdmi_segment(sd, e->pad);
2544 		break;
2545 	default:
2546 		return -EINVAL;
2547 	}
2548 	if (err < 0)
2549 		v4l2_err(sd, "error %d writing edid on port %d\n", err, e->pad);
2550 	return err;
2551 }
2552 
2553 struct adv7842_cfg_read_infoframe {
2554 	const char *desc;
2555 	u8 present_mask;
2556 	u8 head_addr;
2557 	u8 payload_addr;
2558 };
2559 
log_infoframe(struct v4l2_subdev * sd,const struct adv7842_cfg_read_infoframe * cri)2560 static void log_infoframe(struct v4l2_subdev *sd, const struct adv7842_cfg_read_infoframe *cri)
2561 {
2562 	int i;
2563 	u8 buffer[32];
2564 	union hdmi_infoframe frame;
2565 	u8 len;
2566 	struct i2c_client *client = v4l2_get_subdevdata(sd);
2567 	struct device *dev = &client->dev;
2568 
2569 	if (!(io_read(sd, 0x60) & cri->present_mask)) {
2570 		v4l2_info(sd, "%s infoframe not received\n", cri->desc);
2571 		return;
2572 	}
2573 
2574 	for (i = 0; i < 3; i++)
2575 		buffer[i] = infoframe_read(sd, cri->head_addr + i);
2576 
2577 	len = buffer[2] + 1;
2578 
2579 	if (len + 3 > sizeof(buffer)) {
2580 		v4l2_err(sd, "%s: invalid %s infoframe length %d\n", __func__, cri->desc, len);
2581 		return;
2582 	}
2583 
2584 	for (i = 0; i < len; i++)
2585 		buffer[i + 3] = infoframe_read(sd, cri->payload_addr + i);
2586 
2587 	if (hdmi_infoframe_unpack(&frame, buffer, len + 3) < 0) {
2588 		v4l2_err(sd, "%s: unpack of %s infoframe failed\n", __func__, cri->desc);
2589 		return;
2590 	}
2591 
2592 	hdmi_infoframe_log(KERN_INFO, dev, &frame);
2593 }
2594 
adv7842_log_infoframes(struct v4l2_subdev * sd)2595 static void adv7842_log_infoframes(struct v4l2_subdev *sd)
2596 {
2597 	int i;
2598 	static const struct adv7842_cfg_read_infoframe cri[] = {
2599 		{ "AVI", 0x01, 0xe0, 0x00 },
2600 		{ "Audio", 0x02, 0xe3, 0x1c },
2601 		{ "SDP", 0x04, 0xe6, 0x2a },
2602 		{ "Vendor", 0x10, 0xec, 0x54 }
2603 	};
2604 
2605 	if (!(hdmi_read(sd, 0x05) & 0x80)) {
2606 		v4l2_info(sd, "receive DVI-D signal, no infoframes\n");
2607 		return;
2608 	}
2609 
2610 	for (i = 0; i < ARRAY_SIZE(cri); i++)
2611 		log_infoframe(sd, &cri[i]);
2612 }
2613 
2614 #if 0
2615 /* Let's keep it here for now, as it could be useful for debug */
2616 static const char * const prim_mode_txt[] = {
2617 	"SDP",
2618 	"Component",
2619 	"Graphics",
2620 	"Reserved",
2621 	"CVBS & HDMI AUDIO",
2622 	"HDMI-Comp",
2623 	"HDMI-GR",
2624 	"Reserved",
2625 	"Reserved",
2626 	"Reserved",
2627 	"Reserved",
2628 	"Reserved",
2629 	"Reserved",
2630 	"Reserved",
2631 	"Reserved",
2632 	"Reserved",
2633 };
2634 #endif
2635 
adv7842_sdp_log_status(struct v4l2_subdev * sd)2636 static int adv7842_sdp_log_status(struct v4l2_subdev *sd)
2637 {
2638 	/* SDP (Standard definition processor) block */
2639 	u8 sdp_signal_detected = sdp_read(sd, 0x5A) & 0x01;
2640 
2641 	v4l2_info(sd, "Chip powered %s\n", no_power(sd) ? "off" : "on");
2642 	v4l2_info(sd, "Prim-mode = 0x%x, video std = 0x%x\n",
2643 		  io_read(sd, 0x01) & 0x0f, io_read(sd, 0x00) & 0x3f);
2644 
2645 	v4l2_info(sd, "SDP: free run: %s\n",
2646 		(sdp_read(sd, 0x56) & 0x01) ? "on" : "off");
2647 	v4l2_info(sd, "SDP: %s\n", sdp_signal_detected ?
2648 		"valid SD/PR signal detected" : "invalid/no signal");
2649 	if (sdp_signal_detected) {
2650 		static const char * const sdp_std_txt[] = {
2651 			"NTSC-M/J",
2652 			"1?",
2653 			"NTSC-443",
2654 			"60HzSECAM",
2655 			"PAL-M",
2656 			"5?",
2657 			"PAL-60",
2658 			"7?", "8?", "9?", "a?", "b?",
2659 			"PAL-CombN",
2660 			"d?",
2661 			"PAL-BGHID",
2662 			"SECAM"
2663 		};
2664 		v4l2_info(sd, "SDP: standard %s\n",
2665 			sdp_std_txt[sdp_read(sd, 0x52) & 0x0f]);
2666 		v4l2_info(sd, "SDP: %s\n",
2667 			(sdp_read(sd, 0x59) & 0x08) ? "50Hz" : "60Hz");
2668 		v4l2_info(sd, "SDP: %s\n",
2669 			(sdp_read(sd, 0x57) & 0x08) ? "Interlaced" : "Progressive");
2670 		v4l2_info(sd, "SDP: deinterlacer %s\n",
2671 			(sdp_read(sd, 0x12) & 0x08) ? "enabled" : "disabled");
2672 		v4l2_info(sd, "SDP: csc %s mode\n",
2673 			(sdp_io_read(sd, 0xe0) & 0x40) ? "auto" : "manual");
2674 	}
2675 	return 0;
2676 }
2677 
adv7842_cp_log_status(struct v4l2_subdev * sd)2678 static int adv7842_cp_log_status(struct v4l2_subdev *sd)
2679 {
2680 	/* CP block */
2681 	struct adv7842_state *state = to_state(sd);
2682 	struct v4l2_dv_timings timings;
2683 	u8 reg_io_0x02 = io_read(sd, 0x02);
2684 	u8 reg_io_0x21 = io_read(sd, 0x21);
2685 	u8 reg_rep_0x77 = rep_read(sd, 0x77);
2686 	u8 reg_rep_0x7d = rep_read(sd, 0x7d);
2687 	bool audio_pll_locked = hdmi_read(sd, 0x04) & 0x01;
2688 	bool audio_sample_packet_detect = hdmi_read(sd, 0x18) & 0x01;
2689 	bool audio_mute = io_read(sd, 0x65) & 0x40;
2690 
2691 	static const char * const csc_coeff_sel_rb[16] = {
2692 		"bypassed", "YPbPr601 -> RGB", "reserved", "YPbPr709 -> RGB",
2693 		"reserved", "RGB -> YPbPr601", "reserved", "RGB -> YPbPr709",
2694 		"reserved", "YPbPr709 -> YPbPr601", "YPbPr601 -> YPbPr709",
2695 		"reserved", "reserved", "reserved", "reserved", "manual"
2696 	};
2697 	static const char * const input_color_space_txt[16] = {
2698 		"RGB limited range (16-235)", "RGB full range (0-255)",
2699 		"YCbCr Bt.601 (16-235)", "YCbCr Bt.709 (16-235)",
2700 		"xvYCC Bt.601", "xvYCC Bt.709",
2701 		"YCbCr Bt.601 (0-255)", "YCbCr Bt.709 (0-255)",
2702 		"invalid", "invalid", "invalid", "invalid", "invalid",
2703 		"invalid", "invalid", "automatic"
2704 	};
2705 	static const char * const rgb_quantization_range_txt[] = {
2706 		"Automatic",
2707 		"RGB limited range (16-235)",
2708 		"RGB full range (0-255)",
2709 	};
2710 	static const char * const deep_color_mode_txt[4] = {
2711 		"8-bits per channel",
2712 		"10-bits per channel",
2713 		"12-bits per channel",
2714 		"16-bits per channel (not supported)"
2715 	};
2716 
2717 	v4l2_info(sd, "-----Chip status-----\n");
2718 	v4l2_info(sd, "Chip power: %s\n", no_power(sd) ? "off" : "on");
2719 	v4l2_info(sd, "HDMI/DVI-D port selected: %s\n",
2720 			state->hdmi_port_a ? "A" : "B");
2721 	v4l2_info(sd, "EDID A %s, B %s\n",
2722 		  ((reg_rep_0x7d & 0x04) && (reg_rep_0x77 & 0x04)) ?
2723 		  "enabled" : "disabled",
2724 		  ((reg_rep_0x7d & 0x08) && (reg_rep_0x77 & 0x08)) ?
2725 		  "enabled" : "disabled");
2726 	v4l2_info(sd, "HPD A %s, B %s\n",
2727 		  reg_io_0x21 & 0x02 ? "enabled" : "disabled",
2728 		  reg_io_0x21 & 0x01 ? "enabled" : "disabled");
2729 	v4l2_info(sd, "CEC: %s\n", state->cec_enabled_adap ?
2730 			"enabled" : "disabled");
2731 	if (state->cec_enabled_adap) {
2732 		int i;
2733 
2734 		for (i = 0; i < ADV7842_MAX_ADDRS; i++) {
2735 			bool is_valid = state->cec_valid_addrs & (1 << i);
2736 
2737 			if (is_valid)
2738 				v4l2_info(sd, "CEC Logical Address: 0x%x\n",
2739 					  state->cec_addr[i]);
2740 		}
2741 	}
2742 
2743 	v4l2_info(sd, "-----Signal status-----\n");
2744 	if (state->hdmi_port_a) {
2745 		v4l2_info(sd, "Cable detected (+5V power): %s\n",
2746 			  io_read(sd, 0x6f) & 0x02 ? "true" : "false");
2747 		v4l2_info(sd, "TMDS signal detected: %s\n",
2748 			  (io_read(sd, 0x6a) & 0x02) ? "true" : "false");
2749 		v4l2_info(sd, "TMDS signal locked: %s\n",
2750 			  (io_read(sd, 0x6a) & 0x20) ? "true" : "false");
2751 	} else {
2752 		v4l2_info(sd, "Cable detected (+5V power):%s\n",
2753 			  io_read(sd, 0x6f) & 0x01 ? "true" : "false");
2754 		v4l2_info(sd, "TMDS signal detected: %s\n",
2755 			  (io_read(sd, 0x6a) & 0x01) ? "true" : "false");
2756 		v4l2_info(sd, "TMDS signal locked: %s\n",
2757 			  (io_read(sd, 0x6a) & 0x10) ? "true" : "false");
2758 	}
2759 	v4l2_info(sd, "CP free run: %s\n",
2760 		  (!!(cp_read(sd, 0xff) & 0x10) ? "on" : "off"));
2761 	v4l2_info(sd, "Prim-mode = 0x%x, video std = 0x%x, v_freq = 0x%x\n",
2762 		  io_read(sd, 0x01) & 0x0f, io_read(sd, 0x00) & 0x3f,
2763 		  (io_read(sd, 0x01) & 0x70) >> 4);
2764 
2765 	v4l2_info(sd, "-----Video Timings-----\n");
2766 	if (no_cp_signal(sd)) {
2767 		v4l2_info(sd, "STDI: not locked\n");
2768 	} else {
2769 		u32 bl = ((cp_read(sd, 0xb1) & 0x3f) << 8) | cp_read(sd, 0xb2);
2770 		u32 lcf = ((cp_read(sd, 0xb3) & 0x7) << 8) | cp_read(sd, 0xb4);
2771 		u32 lcvs = cp_read(sd, 0xb3) >> 3;
2772 		u32 fcl = ((cp_read(sd, 0xb8) & 0x1f) << 8) | cp_read(sd, 0xb9);
2773 		char hs_pol = ((cp_read(sd, 0xb5) & 0x10) ?
2774 				((cp_read(sd, 0xb5) & 0x08) ? '+' : '-') : 'x');
2775 		char vs_pol = ((cp_read(sd, 0xb5) & 0x40) ?
2776 				((cp_read(sd, 0xb5) & 0x20) ? '+' : '-') : 'x');
2777 		v4l2_info(sd,
2778 			"STDI: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, fcl = %d, %s, %chsync, %cvsync\n",
2779 			lcf, bl, lcvs, fcl,
2780 			(cp_read(sd, 0xb1) & 0x40) ?
2781 				"interlaced" : "progressive",
2782 			hs_pol, vs_pol);
2783 	}
2784 	if (adv7842_query_dv_timings(sd, &timings))
2785 		v4l2_info(sd, "No video detected\n");
2786 	else
2787 		v4l2_print_dv_timings(sd->name, "Detected format: ",
2788 				      &timings, true);
2789 	v4l2_print_dv_timings(sd->name, "Configured format: ",
2790 			&state->timings, true);
2791 
2792 	if (no_cp_signal(sd))
2793 		return 0;
2794 
2795 	v4l2_info(sd, "-----Color space-----\n");
2796 	v4l2_info(sd, "RGB quantization range ctrl: %s\n",
2797 		  rgb_quantization_range_txt[state->rgb_quantization_range]);
2798 	v4l2_info(sd, "Input color space: %s\n",
2799 		  input_color_space_txt[reg_io_0x02 >> 4]);
2800 	v4l2_info(sd, "Output color space: %s %s, alt-gamma %s\n",
2801 		  (reg_io_0x02 & 0x02) ? "RGB" : "YCbCr",
2802 		  (((reg_io_0x02 >> 2) & 0x01) ^ (reg_io_0x02 & 0x01)) ?
2803 			"(16-235)" : "(0-255)",
2804 		  (reg_io_0x02 & 0x08) ? "enabled" : "disabled");
2805 	v4l2_info(sd, "Color space conversion: %s\n",
2806 		  csc_coeff_sel_rb[cp_read(sd, 0xf4) >> 4]);
2807 
2808 	if (!is_digital_input(sd))
2809 		return 0;
2810 
2811 	v4l2_info(sd, "-----%s status-----\n", is_hdmi(sd) ? "HDMI" : "DVI-D");
2812 	v4l2_info(sd, "HDCP encrypted content: %s\n",
2813 			(hdmi_read(sd, 0x05) & 0x40) ? "true" : "false");
2814 	v4l2_info(sd, "HDCP keys read: %s%s\n",
2815 			(hdmi_read(sd, 0x04) & 0x20) ? "yes" : "no",
2816 			(hdmi_read(sd, 0x04) & 0x10) ? "ERROR" : "");
2817 	if (!is_hdmi(sd))
2818 		return 0;
2819 
2820 	v4l2_info(sd, "Audio: pll %s, samples %s, %s\n",
2821 			audio_pll_locked ? "locked" : "not locked",
2822 			audio_sample_packet_detect ? "detected" : "not detected",
2823 			audio_mute ? "muted" : "enabled");
2824 	if (audio_pll_locked && audio_sample_packet_detect) {
2825 		v4l2_info(sd, "Audio format: %s\n",
2826 			(hdmi_read(sd, 0x07) & 0x40) ? "multi-channel" : "stereo");
2827 	}
2828 	v4l2_info(sd, "Audio CTS: %u\n", (hdmi_read(sd, 0x5b) << 12) +
2829 			(hdmi_read(sd, 0x5c) << 8) +
2830 			(hdmi_read(sd, 0x5d) & 0xf0));
2831 	v4l2_info(sd, "Audio N: %u\n", ((hdmi_read(sd, 0x5d) & 0x0f) << 16) +
2832 			(hdmi_read(sd, 0x5e) << 8) +
2833 			hdmi_read(sd, 0x5f));
2834 	v4l2_info(sd, "AV Mute: %s\n",
2835 			(hdmi_read(sd, 0x04) & 0x40) ? "on" : "off");
2836 	v4l2_info(sd, "Deep color mode: %s\n",
2837 			deep_color_mode_txt[hdmi_read(sd, 0x0b) >> 6]);
2838 
2839 	adv7842_log_infoframes(sd);
2840 
2841 	return 0;
2842 }
2843 
adv7842_log_status(struct v4l2_subdev * sd)2844 static int adv7842_log_status(struct v4l2_subdev *sd)
2845 {
2846 	struct adv7842_state *state = to_state(sd);
2847 
2848 	if (state->mode == ADV7842_MODE_SDP)
2849 		return adv7842_sdp_log_status(sd);
2850 	return adv7842_cp_log_status(sd);
2851 }
2852 
adv7842_querystd(struct v4l2_subdev * sd,v4l2_std_id * std)2853 static int adv7842_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
2854 {
2855 	struct adv7842_state *state = to_state(sd);
2856 
2857 	v4l2_dbg(1, debug, sd, "%s:\n", __func__);
2858 
2859 	if (state->mode != ADV7842_MODE_SDP)
2860 		return -ENODATA;
2861 
2862 	if (!(sdp_read(sd, 0x5A) & 0x01)) {
2863 		*std = 0;
2864 		v4l2_dbg(1, debug, sd, "%s: no valid signal\n", __func__);
2865 		return 0;
2866 	}
2867 
2868 	switch (sdp_read(sd, 0x52) & 0x0f) {
2869 	case 0:
2870 		/* NTSC-M/J */
2871 		*std &= V4L2_STD_NTSC;
2872 		break;
2873 	case 2:
2874 		/* NTSC-443 */
2875 		*std &= V4L2_STD_NTSC_443;
2876 		break;
2877 	case 3:
2878 		/* 60HzSECAM */
2879 		*std &= V4L2_STD_SECAM;
2880 		break;
2881 	case 4:
2882 		/* PAL-M */
2883 		*std &= V4L2_STD_PAL_M;
2884 		break;
2885 	case 6:
2886 		/* PAL-60 */
2887 		*std &= V4L2_STD_PAL_60;
2888 		break;
2889 	case 0xc:
2890 		/* PAL-CombN */
2891 		*std &= V4L2_STD_PAL_Nc;
2892 		break;
2893 	case 0xe:
2894 		/* PAL-BGHID */
2895 		*std &= V4L2_STD_PAL;
2896 		break;
2897 	case 0xf:
2898 		/* SECAM */
2899 		*std &= V4L2_STD_SECAM;
2900 		break;
2901 	default:
2902 		*std &= V4L2_STD_ALL;
2903 		break;
2904 	}
2905 	return 0;
2906 }
2907 
adv7842_s_sdp_io(struct v4l2_subdev * sd,struct adv7842_sdp_io_sync_adjustment * s)2908 static void adv7842_s_sdp_io(struct v4l2_subdev *sd, struct adv7842_sdp_io_sync_adjustment *s)
2909 {
2910 	if (s && s->adjust) {
2911 		sdp_io_write(sd, 0x94, (s->hs_beg >> 8) & 0xf);
2912 		sdp_io_write(sd, 0x95, s->hs_beg & 0xff);
2913 		sdp_io_write(sd, 0x96, (s->hs_width >> 8) & 0xf);
2914 		sdp_io_write(sd, 0x97, s->hs_width & 0xff);
2915 		sdp_io_write(sd, 0x98, (s->de_beg >> 8) & 0xf);
2916 		sdp_io_write(sd, 0x99, s->de_beg & 0xff);
2917 		sdp_io_write(sd, 0x9a, (s->de_end >> 8) & 0xf);
2918 		sdp_io_write(sd, 0x9b, s->de_end & 0xff);
2919 		sdp_io_write(sd, 0xa8, s->vs_beg_o);
2920 		sdp_io_write(sd, 0xa9, s->vs_beg_e);
2921 		sdp_io_write(sd, 0xaa, s->vs_end_o);
2922 		sdp_io_write(sd, 0xab, s->vs_end_e);
2923 		sdp_io_write(sd, 0xac, s->de_v_beg_o);
2924 		sdp_io_write(sd, 0xad, s->de_v_beg_e);
2925 		sdp_io_write(sd, 0xae, s->de_v_end_o);
2926 		sdp_io_write(sd, 0xaf, s->de_v_end_e);
2927 	} else {
2928 		/* set to default */
2929 		sdp_io_write(sd, 0x94, 0x00);
2930 		sdp_io_write(sd, 0x95, 0x00);
2931 		sdp_io_write(sd, 0x96, 0x00);
2932 		sdp_io_write(sd, 0x97, 0x20);
2933 		sdp_io_write(sd, 0x98, 0x00);
2934 		sdp_io_write(sd, 0x99, 0x00);
2935 		sdp_io_write(sd, 0x9a, 0x00);
2936 		sdp_io_write(sd, 0x9b, 0x00);
2937 		sdp_io_write(sd, 0xa8, 0x04);
2938 		sdp_io_write(sd, 0xa9, 0x04);
2939 		sdp_io_write(sd, 0xaa, 0x04);
2940 		sdp_io_write(sd, 0xab, 0x04);
2941 		sdp_io_write(sd, 0xac, 0x04);
2942 		sdp_io_write(sd, 0xad, 0x04);
2943 		sdp_io_write(sd, 0xae, 0x04);
2944 		sdp_io_write(sd, 0xaf, 0x04);
2945 	}
2946 }
2947 
adv7842_s_std(struct v4l2_subdev * sd,v4l2_std_id norm)2948 static int adv7842_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
2949 {
2950 	struct adv7842_state *state = to_state(sd);
2951 	struct adv7842_platform_data *pdata = &state->pdata;
2952 
2953 	v4l2_dbg(1, debug, sd, "%s:\n", __func__);
2954 
2955 	if (state->mode != ADV7842_MODE_SDP)
2956 		return -ENODATA;
2957 
2958 	if (norm & V4L2_STD_625_50)
2959 		adv7842_s_sdp_io(sd, &pdata->sdp_io_sync_625);
2960 	else if (norm & V4L2_STD_525_60)
2961 		adv7842_s_sdp_io(sd, &pdata->sdp_io_sync_525);
2962 	else
2963 		adv7842_s_sdp_io(sd, NULL);
2964 
2965 	if (norm & V4L2_STD_ALL) {
2966 		state->norm = norm;
2967 		return 0;
2968 	}
2969 	return -EINVAL;
2970 }
2971 
adv7842_g_std(struct v4l2_subdev * sd,v4l2_std_id * norm)2972 static int adv7842_g_std(struct v4l2_subdev *sd, v4l2_std_id *norm)
2973 {
2974 	struct adv7842_state *state = to_state(sd);
2975 
2976 	v4l2_dbg(1, debug, sd, "%s:\n", __func__);
2977 
2978 	if (state->mode != ADV7842_MODE_SDP)
2979 		return -ENODATA;
2980 
2981 	*norm = state->norm;
2982 	return 0;
2983 }
2984 
2985 /* ----------------------------------------------------------------------- */
2986 
adv7842_core_init(struct v4l2_subdev * sd)2987 static int adv7842_core_init(struct v4l2_subdev *sd)
2988 {
2989 	struct adv7842_state *state = to_state(sd);
2990 	struct adv7842_platform_data *pdata = &state->pdata;
2991 	hdmi_write(sd, 0x48,
2992 		   (pdata->disable_pwrdnb ? 0x80 : 0) |
2993 		   (pdata->disable_cable_det_rst ? 0x40 : 0));
2994 
2995 	disable_input(sd);
2996 
2997 	/*
2998 	 * Disable I2C access to internal EDID ram from HDMI DDC ports
2999 	 * Disable auto edid enable when leaving powerdown mode
3000 	 */
3001 	rep_write_and_or(sd, 0x77, 0xd3, 0x20);
3002 
3003 	/* power */
3004 	io_write(sd, 0x0c, 0x42);   /* Power up part and power down VDP */
3005 	io_write(sd, 0x15, 0x80);   /* Power up pads */
3006 
3007 	/* video format */
3008 	io_write(sd, 0x02, 0xf0 | pdata->alt_gamma << 3);
3009 	io_write_and_or(sd, 0x05, 0xf0, pdata->blank_data << 3 |
3010 			pdata->insert_av_codes << 2 |
3011 			pdata->replicate_av_codes << 1);
3012 	adv7842_setup_format(state);
3013 
3014 	/* HDMI audio */
3015 	hdmi_write_and_or(sd, 0x1a, 0xf1, 0x08); /* Wait 1 s before unmute */
3016 
3017 	/* Drive strength */
3018 	io_write_and_or(sd, 0x14, 0xc0,
3019 			pdata->dr_str_data << 4 |
3020 			pdata->dr_str_clk << 2 |
3021 			pdata->dr_str_sync);
3022 
3023 	/* HDMI free run */
3024 	cp_write_and_or(sd, 0xba, 0xfc, pdata->hdmi_free_run_enable |
3025 					(pdata->hdmi_free_run_mode << 1));
3026 
3027 	/* SPD free run */
3028 	sdp_write_and_or(sd, 0xdd, 0xf0, pdata->sdp_free_run_force |
3029 					 (pdata->sdp_free_run_cbar_en << 1) |
3030 					 (pdata->sdp_free_run_man_col_en << 2) |
3031 					 (pdata->sdp_free_run_auto << 3));
3032 
3033 	/* TODO from platform data */
3034 	cp_write(sd, 0x69, 0x14);   /* Enable CP CSC */
3035 	io_write(sd, 0x06, 0xa6);   /* positive VS and HS and DE */
3036 	cp_write(sd, 0xf3, 0xdc); /* Low threshold to enter/exit free run mode */
3037 	afe_write(sd, 0xb5, 0x01);  /* Setting MCLK to 256Fs */
3038 
3039 	afe_write(sd, 0x02, pdata->ain_sel); /* Select analog input muxing mode */
3040 	io_write_and_or(sd, 0x30, ~(1 << 4), pdata->output_bus_lsb_to_msb << 4);
3041 
3042 	sdp_csc_coeff(sd, &pdata->sdp_csc_coeff);
3043 
3044 	/* todo, improve settings for sdram */
3045 	if (pdata->sd_ram_size >= 128) {
3046 		sdp_write(sd, 0x12, 0x0d); /* Frame TBC,3D comb enabled */
3047 		if (pdata->sd_ram_ddr) {
3048 			/* SDP setup for the AD eval board */
3049 			sdp_io_write(sd, 0x6f, 0x00); /* DDR mode */
3050 			sdp_io_write(sd, 0x75, 0x0a); /* 128 MB memory size */
3051 			sdp_io_write(sd, 0x7a, 0xa5); /* Timing Adjustment */
3052 			sdp_io_write(sd, 0x7b, 0x8f); /* Timing Adjustment */
3053 			sdp_io_write(sd, 0x60, 0x01); /* SDRAM reset */
3054 		} else {
3055 			sdp_io_write(sd, 0x75, 0x0a); /* 64 MB memory size ?*/
3056 			sdp_io_write(sd, 0x74, 0x00); /* must be zero for sdr sdram */
3057 			sdp_io_write(sd, 0x79, 0x33); /* CAS latency to 3,
3058 							 depends on memory */
3059 			sdp_io_write(sd, 0x6f, 0x01); /* SDR mode */
3060 			sdp_io_write(sd, 0x7a, 0xa5); /* Timing Adjustment */
3061 			sdp_io_write(sd, 0x7b, 0x8f); /* Timing Adjustment */
3062 			sdp_io_write(sd, 0x60, 0x01); /* SDRAM reset */
3063 		}
3064 	} else {
3065 		/*
3066 		 * Manual UG-214, rev 0 is bit confusing on this bit
3067 		 * but a '1' disables any signal if the Ram is active.
3068 		 */
3069 		sdp_io_write(sd, 0x29, 0x10); /* Tristate memory interface */
3070 	}
3071 
3072 	select_input(sd, pdata->vid_std_select);
3073 
3074 	enable_input(sd);
3075 
3076 	if (pdata->hpa_auto) {
3077 		/* HPA auto, HPA 0.5s after Edid set and Cable detect */
3078 		hdmi_write(sd, 0x69, 0x5c);
3079 	} else {
3080 		/* HPA manual */
3081 		hdmi_write(sd, 0x69, 0xa3);
3082 		/* HPA disable on port A and B */
3083 		io_write_and_or(sd, 0x20, 0xcf, 0x00);
3084 	}
3085 
3086 	/* LLC */
3087 	io_write(sd, 0x19, 0x80 | pdata->llc_dll_phase);
3088 	io_write(sd, 0x33, 0x40);
3089 
3090 	/* interrupts */
3091 	io_write(sd, 0x40, 0xf2); /* Configure INT1 */
3092 
3093 	adv7842_irq_enable(sd, true);
3094 
3095 	return v4l2_ctrl_handler_setup(sd->ctrl_handler);
3096 }
3097 
3098 /* ----------------------------------------------------------------------- */
3099 
adv7842_ddr_ram_test(struct v4l2_subdev * sd)3100 static int adv7842_ddr_ram_test(struct v4l2_subdev *sd)
3101 {
3102 	/*
3103 	 * From ADV784x external Memory test.pdf
3104 	 *
3105 	 * Reset must just been performed before running test.
3106 	 * Recommended to reset after test.
3107 	 */
3108 	int i;
3109 	int pass = 0;
3110 	int fail = 0;
3111 	int complete = 0;
3112 
3113 	io_write(sd, 0x00, 0x01);  /* Program SDP 4x1 */
3114 	io_write(sd, 0x01, 0x00);  /* Program SDP mode */
3115 	afe_write(sd, 0x80, 0x92); /* SDP Recommended Write */
3116 	afe_write(sd, 0x9B, 0x01); /* SDP Recommended Write ADV7844ES1 */
3117 	afe_write(sd, 0x9C, 0x60); /* SDP Recommended Write ADV7844ES1 */
3118 	afe_write(sd, 0x9E, 0x02); /* SDP Recommended Write ADV7844ES1 */
3119 	afe_write(sd, 0xA0, 0x0B); /* SDP Recommended Write ADV7844ES1 */
3120 	afe_write(sd, 0xC3, 0x02); /* Memory BIST Initialisation */
3121 	io_write(sd, 0x0C, 0x40);  /* Power up ADV7844 */
3122 	io_write(sd, 0x15, 0xBA);  /* Enable outputs */
3123 	sdp_write(sd, 0x12, 0x00); /* Disable 3D comb, Frame TBC & 3DNR */
3124 	io_write(sd, 0xFF, 0x04);  /* Reset memory controller */
3125 
3126 	usleep_range(5000, 6000);
3127 
3128 	sdp_write(sd, 0x12, 0x00);    /* Disable 3D Comb, Frame TBC & 3DNR */
3129 	sdp_io_write(sd, 0x2A, 0x01); /* Memory BIST Initialisation */
3130 	sdp_io_write(sd, 0x7c, 0x19); /* Memory BIST Initialisation */
3131 	sdp_io_write(sd, 0x80, 0x87); /* Memory BIST Initialisation */
3132 	sdp_io_write(sd, 0x81, 0x4a); /* Memory BIST Initialisation */
3133 	sdp_io_write(sd, 0x82, 0x2c); /* Memory BIST Initialisation */
3134 	sdp_io_write(sd, 0x83, 0x0e); /* Memory BIST Initialisation */
3135 	sdp_io_write(sd, 0x84, 0x94); /* Memory BIST Initialisation */
3136 	sdp_io_write(sd, 0x85, 0x62); /* Memory BIST Initialisation */
3137 	sdp_io_write(sd, 0x7d, 0x00); /* Memory BIST Initialisation */
3138 	sdp_io_write(sd, 0x7e, 0x1a); /* Memory BIST Initialisation */
3139 
3140 	usleep_range(5000, 6000);
3141 
3142 	sdp_io_write(sd, 0xd9, 0xd5); /* Enable BIST Test */
3143 	sdp_write(sd, 0x12, 0x05); /* Enable FRAME TBC & 3D COMB */
3144 
3145 	msleep(20);
3146 
3147 	for (i = 0; i < 10; i++) {
3148 		u8 result = sdp_io_read(sd, 0xdb);
3149 		if (result & 0x10) {
3150 			complete++;
3151 			if (result & 0x20)
3152 				fail++;
3153 			else
3154 				pass++;
3155 		}
3156 		msleep(20);
3157 	}
3158 
3159 	v4l2_dbg(1, debug, sd,
3160 		"Ram Test: completed %d of %d: pass %d, fail %d\n",
3161 		complete, i, pass, fail);
3162 
3163 	if (!complete || fail)
3164 		return -EIO;
3165 	return 0;
3166 }
3167 
adv7842_rewrite_i2c_addresses(struct v4l2_subdev * sd,struct adv7842_platform_data * pdata)3168 static void adv7842_rewrite_i2c_addresses(struct v4l2_subdev *sd,
3169 		struct adv7842_platform_data *pdata)
3170 {
3171 	io_write(sd, 0xf1, pdata->i2c_sdp << 1);
3172 	io_write(sd, 0xf2, pdata->i2c_sdp_io << 1);
3173 	io_write(sd, 0xf3, pdata->i2c_avlink << 1);
3174 	io_write(sd, 0xf4, pdata->i2c_cec << 1);
3175 	io_write(sd, 0xf5, pdata->i2c_infoframe << 1);
3176 
3177 	io_write(sd, 0xf8, pdata->i2c_afe << 1);
3178 	io_write(sd, 0xf9, pdata->i2c_repeater << 1);
3179 	io_write(sd, 0xfa, pdata->i2c_edid << 1);
3180 	io_write(sd, 0xfb, pdata->i2c_hdmi << 1);
3181 
3182 	io_write(sd, 0xfd, pdata->i2c_cp << 1);
3183 	io_write(sd, 0xfe, pdata->i2c_vdp << 1);
3184 }
3185 
adv7842_command_ram_test(struct v4l2_subdev * sd)3186 static int adv7842_command_ram_test(struct v4l2_subdev *sd)
3187 {
3188 	struct i2c_client *client = v4l2_get_subdevdata(sd);
3189 	struct adv7842_state *state = to_state(sd);
3190 	struct adv7842_platform_data *pdata = client->dev.platform_data;
3191 	struct v4l2_dv_timings timings;
3192 	int ret = 0;
3193 
3194 	if (!pdata)
3195 		return -ENODEV;
3196 
3197 	if (!pdata->sd_ram_size || !pdata->sd_ram_ddr) {
3198 		v4l2_info(sd, "no sdram or no ddr sdram\n");
3199 		return -EINVAL;
3200 	}
3201 
3202 	main_reset(sd);
3203 
3204 	adv7842_rewrite_i2c_addresses(sd, pdata);
3205 
3206 	/* run ram test */
3207 	ret = adv7842_ddr_ram_test(sd);
3208 
3209 	main_reset(sd);
3210 
3211 	adv7842_rewrite_i2c_addresses(sd, pdata);
3212 
3213 	/* and re-init chip and state */
3214 	adv7842_core_init(sd);
3215 
3216 	disable_input(sd);
3217 
3218 	select_input(sd, state->vid_std_select);
3219 
3220 	enable_input(sd);
3221 
3222 	edid_write_vga_segment(sd);
3223 	edid_write_hdmi_segment(sd, ADV7842_EDID_PORT_A);
3224 	edid_write_hdmi_segment(sd, ADV7842_EDID_PORT_B);
3225 
3226 	timings = state->timings;
3227 
3228 	memset(&state->timings, 0, sizeof(struct v4l2_dv_timings));
3229 
3230 	adv7842_s_dv_timings(sd, &timings);
3231 
3232 	return ret;
3233 }
3234 
adv7842_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)3235 static long adv7842_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
3236 {
3237 	switch (cmd) {
3238 	case ADV7842_CMD_RAM_TEST:
3239 		return adv7842_command_ram_test(sd);
3240 	}
3241 	return -ENOTTY;
3242 }
3243 
adv7842_subscribe_event(struct v4l2_subdev * sd,struct v4l2_fh * fh,struct v4l2_event_subscription * sub)3244 static int adv7842_subscribe_event(struct v4l2_subdev *sd,
3245 				   struct v4l2_fh *fh,
3246 				   struct v4l2_event_subscription *sub)
3247 {
3248 	switch (sub->type) {
3249 	case V4L2_EVENT_SOURCE_CHANGE:
3250 		return v4l2_src_change_event_subdev_subscribe(sd, fh, sub);
3251 	case V4L2_EVENT_CTRL:
3252 		return v4l2_ctrl_subdev_subscribe_event(sd, fh, sub);
3253 	default:
3254 		return -EINVAL;
3255 	}
3256 }
3257 
adv7842_registered(struct v4l2_subdev * sd)3258 static int adv7842_registered(struct v4l2_subdev *sd)
3259 {
3260 	struct adv7842_state *state = to_state(sd);
3261 	struct i2c_client *client = v4l2_get_subdevdata(sd);
3262 	int err;
3263 
3264 	err = cec_register_adapter(state->cec_adap, &client->dev);
3265 	if (err)
3266 		cec_delete_adapter(state->cec_adap);
3267 	return err;
3268 }
3269 
adv7842_unregistered(struct v4l2_subdev * sd)3270 static void adv7842_unregistered(struct v4l2_subdev *sd)
3271 {
3272 	struct adv7842_state *state = to_state(sd);
3273 
3274 	cec_unregister_adapter(state->cec_adap);
3275 }
3276 
3277 /* ----------------------------------------------------------------------- */
3278 
3279 static const struct v4l2_ctrl_ops adv7842_ctrl_ops = {
3280 	.s_ctrl = adv7842_s_ctrl,
3281 	.g_volatile_ctrl = adv7842_g_volatile_ctrl,
3282 };
3283 
3284 static const struct v4l2_subdev_core_ops adv7842_core_ops = {
3285 	.log_status = adv7842_log_status,
3286 	.ioctl = adv7842_ioctl,
3287 	.interrupt_service_routine = adv7842_isr,
3288 	.subscribe_event = adv7842_subscribe_event,
3289 	.unsubscribe_event = v4l2_event_subdev_unsubscribe,
3290 #ifdef CONFIG_VIDEO_ADV_DEBUG
3291 	.g_register = adv7842_g_register,
3292 	.s_register = adv7842_s_register,
3293 #endif
3294 };
3295 
3296 static const struct v4l2_subdev_video_ops adv7842_video_ops = {
3297 	.g_std = adv7842_g_std,
3298 	.s_std = adv7842_s_std,
3299 	.s_routing = adv7842_s_routing,
3300 	.querystd = adv7842_querystd,
3301 	.g_input_status = adv7842_g_input_status,
3302 	.s_dv_timings = adv7842_s_dv_timings,
3303 	.g_dv_timings = adv7842_g_dv_timings,
3304 	.query_dv_timings = adv7842_query_dv_timings,
3305 };
3306 
3307 static const struct v4l2_subdev_pad_ops adv7842_pad_ops = {
3308 	.enum_mbus_code = adv7842_enum_mbus_code,
3309 	.get_fmt = adv7842_get_format,
3310 	.set_fmt = adv7842_set_format,
3311 	.get_edid = adv7842_get_edid,
3312 	.set_edid = adv7842_set_edid,
3313 	.enum_dv_timings = adv7842_enum_dv_timings,
3314 	.dv_timings_cap = adv7842_dv_timings_cap,
3315 };
3316 
3317 static const struct v4l2_subdev_ops adv7842_ops = {
3318 	.core = &adv7842_core_ops,
3319 	.video = &adv7842_video_ops,
3320 	.pad = &adv7842_pad_ops,
3321 };
3322 
3323 static const struct v4l2_subdev_internal_ops adv7842_int_ops = {
3324 	.registered = adv7842_registered,
3325 	.unregistered = adv7842_unregistered,
3326 };
3327 
3328 /* -------------------------- custom ctrls ---------------------------------- */
3329 
3330 static const struct v4l2_ctrl_config adv7842_ctrl_analog_sampling_phase = {
3331 	.ops = &adv7842_ctrl_ops,
3332 	.id = V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE,
3333 	.name = "Analog Sampling Phase",
3334 	.type = V4L2_CTRL_TYPE_INTEGER,
3335 	.min = 0,
3336 	.max = 0x1f,
3337 	.step = 1,
3338 	.def = 0,
3339 };
3340 
3341 static const struct v4l2_ctrl_config adv7842_ctrl_free_run_color_manual = {
3342 	.ops = &adv7842_ctrl_ops,
3343 	.id = V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL,
3344 	.name = "Free Running Color, Manual",
3345 	.type = V4L2_CTRL_TYPE_BOOLEAN,
3346 	.max = 1,
3347 	.step = 1,
3348 	.def = 1,
3349 };
3350 
3351 static const struct v4l2_ctrl_config adv7842_ctrl_free_run_color = {
3352 	.ops = &adv7842_ctrl_ops,
3353 	.id = V4L2_CID_ADV_RX_FREE_RUN_COLOR,
3354 	.name = "Free Running Color",
3355 	.type = V4L2_CTRL_TYPE_INTEGER,
3356 	.max = 0xffffff,
3357 	.step = 0x1,
3358 };
3359 
3360 
adv7842_unregister_clients(struct v4l2_subdev * sd)3361 static void adv7842_unregister_clients(struct v4l2_subdev *sd)
3362 {
3363 	struct adv7842_state *state = to_state(sd);
3364 	i2c_unregister_device(state->i2c_avlink);
3365 	i2c_unregister_device(state->i2c_cec);
3366 	i2c_unregister_device(state->i2c_infoframe);
3367 	i2c_unregister_device(state->i2c_sdp_io);
3368 	i2c_unregister_device(state->i2c_sdp);
3369 	i2c_unregister_device(state->i2c_afe);
3370 	i2c_unregister_device(state->i2c_repeater);
3371 	i2c_unregister_device(state->i2c_edid);
3372 	i2c_unregister_device(state->i2c_hdmi);
3373 	i2c_unregister_device(state->i2c_cp);
3374 	i2c_unregister_device(state->i2c_vdp);
3375 
3376 	state->i2c_avlink = NULL;
3377 	state->i2c_cec = NULL;
3378 	state->i2c_infoframe = NULL;
3379 	state->i2c_sdp_io = NULL;
3380 	state->i2c_sdp = NULL;
3381 	state->i2c_afe = NULL;
3382 	state->i2c_repeater = NULL;
3383 	state->i2c_edid = NULL;
3384 	state->i2c_hdmi = NULL;
3385 	state->i2c_cp = NULL;
3386 	state->i2c_vdp = NULL;
3387 }
3388 
adv7842_dummy_client(struct v4l2_subdev * sd,const char * desc,u8 addr,u8 io_reg)3389 static struct i2c_client *adv7842_dummy_client(struct v4l2_subdev *sd, const char *desc,
3390 					       u8 addr, u8 io_reg)
3391 {
3392 	struct i2c_client *client = v4l2_get_subdevdata(sd);
3393 	struct i2c_client *cp;
3394 
3395 	io_write(sd, io_reg, addr << 1);
3396 
3397 	if (addr == 0) {
3398 		v4l2_err(sd, "no %s i2c addr configured\n", desc);
3399 		return NULL;
3400 	}
3401 
3402 	cp = i2c_new_dummy_device(client->adapter, io_read(sd, io_reg) >> 1);
3403 	if (IS_ERR(cp)) {
3404 		v4l2_err(sd, "register %s on i2c addr 0x%x failed with %ld\n",
3405 			 desc, addr, PTR_ERR(cp));
3406 		cp = NULL;
3407 	}
3408 
3409 	return cp;
3410 }
3411 
adv7842_register_clients(struct v4l2_subdev * sd)3412 static int adv7842_register_clients(struct v4l2_subdev *sd)
3413 {
3414 	struct adv7842_state *state = to_state(sd);
3415 	struct adv7842_platform_data *pdata = &state->pdata;
3416 
3417 	state->i2c_avlink = adv7842_dummy_client(sd, "avlink", pdata->i2c_avlink, 0xf3);
3418 	state->i2c_cec = adv7842_dummy_client(sd, "cec", pdata->i2c_cec, 0xf4);
3419 	state->i2c_infoframe = adv7842_dummy_client(sd, "infoframe", pdata->i2c_infoframe, 0xf5);
3420 	state->i2c_sdp_io = adv7842_dummy_client(sd, "sdp_io", pdata->i2c_sdp_io, 0xf2);
3421 	state->i2c_sdp = adv7842_dummy_client(sd, "sdp", pdata->i2c_sdp, 0xf1);
3422 	state->i2c_afe = adv7842_dummy_client(sd, "afe", pdata->i2c_afe, 0xf8);
3423 	state->i2c_repeater = adv7842_dummy_client(sd, "repeater", pdata->i2c_repeater, 0xf9);
3424 	state->i2c_edid = adv7842_dummy_client(sd, "edid", pdata->i2c_edid, 0xfa);
3425 	state->i2c_hdmi = adv7842_dummy_client(sd, "hdmi", pdata->i2c_hdmi, 0xfb);
3426 	state->i2c_cp = adv7842_dummy_client(sd, "cp", pdata->i2c_cp, 0xfd);
3427 	state->i2c_vdp = adv7842_dummy_client(sd, "vdp", pdata->i2c_vdp, 0xfe);
3428 
3429 	if (!state->i2c_avlink ||
3430 	    !state->i2c_cec ||
3431 	    !state->i2c_infoframe ||
3432 	    !state->i2c_sdp_io ||
3433 	    !state->i2c_sdp ||
3434 	    !state->i2c_afe ||
3435 	    !state->i2c_repeater ||
3436 	    !state->i2c_edid ||
3437 	    !state->i2c_hdmi ||
3438 	    !state->i2c_cp ||
3439 	    !state->i2c_vdp)
3440 		return -1;
3441 
3442 	return 0;
3443 }
3444 
adv7842_probe(struct i2c_client * client)3445 static int adv7842_probe(struct i2c_client *client)
3446 {
3447 	struct adv7842_state *state;
3448 	static const struct v4l2_dv_timings cea640x480 =
3449 		V4L2_DV_BT_CEA_640X480P59_94;
3450 	struct adv7842_platform_data *pdata = client->dev.platform_data;
3451 	struct v4l2_ctrl_handler *hdl;
3452 	struct v4l2_ctrl *ctrl;
3453 	struct v4l2_subdev *sd;
3454 	unsigned int i;
3455 	u16 rev;
3456 	int err;
3457 
3458 	/* Check if the adapter supports the needed features */
3459 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
3460 		return -EIO;
3461 
3462 	v4l_dbg(1, debug, client, "detecting adv7842 client on address 0x%x\n",
3463 		client->addr << 1);
3464 
3465 	if (!pdata) {
3466 		v4l_err(client, "No platform data!\n");
3467 		return -ENODEV;
3468 	}
3469 
3470 	state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
3471 	if (!state)
3472 		return -ENOMEM;
3473 
3474 	/* platform data */
3475 	state->pdata = *pdata;
3476 	state->timings = cea640x480;
3477 	state->format = adv7842_format_info(state, MEDIA_BUS_FMT_YUYV8_2X8);
3478 
3479 	sd = &state->sd;
3480 	v4l2_i2c_subdev_init(sd, client, &adv7842_ops);
3481 	sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
3482 	sd->internal_ops = &adv7842_int_ops;
3483 	state->mode = pdata->mode;
3484 
3485 	state->hdmi_port_a = pdata->input == ADV7842_SELECT_HDMI_PORT_A;
3486 	state->restart_stdi_once = true;
3487 
3488 	/* i2c access to adv7842? */
3489 	rev = adv_smbus_read_byte_data_check(client, 0xea, false) << 8 |
3490 		adv_smbus_read_byte_data_check(client, 0xeb, false);
3491 	if (rev != 0x2012) {
3492 		v4l2_info(sd, "got rev=0x%04x on first read attempt\n", rev);
3493 		rev = adv_smbus_read_byte_data_check(client, 0xea, false) << 8 |
3494 			adv_smbus_read_byte_data_check(client, 0xeb, false);
3495 	}
3496 	if (rev != 0x2012) {
3497 		v4l2_info(sd, "not an adv7842 on address 0x%x (rev=0x%04x)\n",
3498 			  client->addr << 1, rev);
3499 		return -ENODEV;
3500 	}
3501 
3502 	if (pdata->chip_reset)
3503 		main_reset(sd);
3504 
3505 	/* control handlers */
3506 	hdl = &state->hdl;
3507 	v4l2_ctrl_handler_init(hdl, 6);
3508 
3509 	/* add in ascending ID order */
3510 	v4l2_ctrl_new_std(hdl, &adv7842_ctrl_ops,
3511 			  V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
3512 	v4l2_ctrl_new_std(hdl, &adv7842_ctrl_ops,
3513 			  V4L2_CID_CONTRAST, 0, 255, 1, 128);
3514 	v4l2_ctrl_new_std(hdl, &adv7842_ctrl_ops,
3515 			  V4L2_CID_SATURATION, 0, 255, 1, 128);
3516 	v4l2_ctrl_new_std(hdl, &adv7842_ctrl_ops,
3517 			  V4L2_CID_HUE, 0, 128, 1, 0);
3518 	ctrl = v4l2_ctrl_new_std_menu(hdl, &adv7842_ctrl_ops,
3519 			V4L2_CID_DV_RX_IT_CONTENT_TYPE, V4L2_DV_IT_CONTENT_TYPE_NO_ITC,
3520 			0, V4L2_DV_IT_CONTENT_TYPE_NO_ITC);
3521 	if (ctrl)
3522 		ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
3523 
3524 	/* custom controls */
3525 	state->detect_tx_5v_ctrl = v4l2_ctrl_new_std(hdl, NULL,
3526 			V4L2_CID_DV_RX_POWER_PRESENT, 0, 3, 0, 0);
3527 	state->analog_sampling_phase_ctrl = v4l2_ctrl_new_custom(hdl,
3528 			&adv7842_ctrl_analog_sampling_phase, NULL);
3529 	state->free_run_color_ctrl_manual = v4l2_ctrl_new_custom(hdl,
3530 			&adv7842_ctrl_free_run_color_manual, NULL);
3531 	state->free_run_color_ctrl = v4l2_ctrl_new_custom(hdl,
3532 			&adv7842_ctrl_free_run_color, NULL);
3533 	state->rgb_quantization_range_ctrl =
3534 		v4l2_ctrl_new_std_menu(hdl, &adv7842_ctrl_ops,
3535 			V4L2_CID_DV_RX_RGB_RANGE, V4L2_DV_RGB_RANGE_FULL,
3536 			0, V4L2_DV_RGB_RANGE_AUTO);
3537 	sd->ctrl_handler = hdl;
3538 	if (hdl->error) {
3539 		err = hdl->error;
3540 		goto err_hdl;
3541 	}
3542 	if (adv7842_s_detect_tx_5v_ctrl(sd)) {
3543 		err = -ENODEV;
3544 		goto err_hdl;
3545 	}
3546 
3547 	if (adv7842_register_clients(sd) < 0) {
3548 		err = -ENOMEM;
3549 		v4l2_err(sd, "failed to create all i2c clients\n");
3550 		goto err_i2c;
3551 	}
3552 
3553 
3554 	INIT_DELAYED_WORK(&state->delayed_work_enable_hotplug,
3555 			adv7842_delayed_work_enable_hotplug);
3556 
3557 	sd->entity.function = MEDIA_ENT_F_DV_DECODER;
3558 	for (i = 0; i < ADV7842_PAD_SOURCE; ++i)
3559 		state->pads[i].flags = MEDIA_PAD_FL_SINK;
3560 	state->pads[ADV7842_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
3561 	err = media_entity_pads_init(&sd->entity, ADV7842_PAD_SOURCE + 1,
3562 				     state->pads);
3563 	if (err)
3564 		goto err_work_queues;
3565 
3566 	err = adv7842_core_init(sd);
3567 	if (err)
3568 		goto err_entity;
3569 
3570 #if IS_ENABLED(CONFIG_VIDEO_ADV7842_CEC)
3571 	state->cec_adap = cec_allocate_adapter(&adv7842_cec_adap_ops,
3572 		state, dev_name(&client->dev),
3573 		CEC_CAP_DEFAULTS, ADV7842_MAX_ADDRS);
3574 	err = PTR_ERR_OR_ZERO(state->cec_adap);
3575 	if (err)
3576 		goto err_entity;
3577 #endif
3578 
3579 	v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
3580 		  client->addr << 1, client->adapter->name);
3581 	return 0;
3582 
3583 err_entity:
3584 	media_entity_cleanup(&sd->entity);
3585 err_work_queues:
3586 	cancel_delayed_work(&state->delayed_work_enable_hotplug);
3587 err_i2c:
3588 	adv7842_unregister_clients(sd);
3589 err_hdl:
3590 	v4l2_ctrl_handler_free(hdl);
3591 	return err;
3592 }
3593 
3594 /* ----------------------------------------------------------------------- */
3595 
adv7842_remove(struct i2c_client * client)3596 static void adv7842_remove(struct i2c_client *client)
3597 {
3598 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
3599 	struct adv7842_state *state = to_state(sd);
3600 
3601 	adv7842_irq_enable(sd, false);
3602 	cancel_delayed_work_sync(&state->delayed_work_enable_hotplug);
3603 	v4l2_device_unregister_subdev(sd);
3604 	media_entity_cleanup(&sd->entity);
3605 	adv7842_unregister_clients(sd);
3606 	v4l2_ctrl_handler_free(sd->ctrl_handler);
3607 }
3608 
3609 /* ----------------------------------------------------------------------- */
3610 
3611 static const struct i2c_device_id adv7842_id[] = {
3612 	{ "adv7842", 0 },
3613 	{ }
3614 };
3615 MODULE_DEVICE_TABLE(i2c, adv7842_id);
3616 
3617 /* ----------------------------------------------------------------------- */
3618 
3619 static struct i2c_driver adv7842_driver = {
3620 	.driver = {
3621 		.name = "adv7842",
3622 	},
3623 	.probe = adv7842_probe,
3624 	.remove = adv7842_remove,
3625 	.id_table = adv7842_id,
3626 };
3627 
3628 module_i2c_driver(adv7842_driver);
3629