xref: /OK3568_Linux_fs/kernel/drivers/media/i2c/adv7511-v4l2.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Analog Devices ADV7511 HDMI Transmitter Device Driver
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright 2013 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun /*
9*4882a593Smuzhiyun  * This file is named adv7511-v4l2.c so it doesn't conflict with the Analog
10*4882a593Smuzhiyun  * Device ADV7511 (config fragment CONFIG_DRM_I2C_ADV7511).
11*4882a593Smuzhiyun  */
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include <linux/kernel.h>
15*4882a593Smuzhiyun #include <linux/module.h>
16*4882a593Smuzhiyun #include <linux/slab.h>
17*4882a593Smuzhiyun #include <linux/i2c.h>
18*4882a593Smuzhiyun #include <linux/delay.h>
19*4882a593Smuzhiyun #include <linux/videodev2.h>
20*4882a593Smuzhiyun #include <linux/gpio.h>
21*4882a593Smuzhiyun #include <linux/workqueue.h>
22*4882a593Smuzhiyun #include <linux/hdmi.h>
23*4882a593Smuzhiyun #include <linux/v4l2-dv-timings.h>
24*4882a593Smuzhiyun #include <media/v4l2-device.h>
25*4882a593Smuzhiyun #include <media/v4l2-common.h>
26*4882a593Smuzhiyun #include <media/v4l2-ctrls.h>
27*4882a593Smuzhiyun #include <media/v4l2-dv-timings.h>
28*4882a593Smuzhiyun #include <media/i2c/adv7511.h>
29*4882a593Smuzhiyun #include <media/cec.h>
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun static int debug;
32*4882a593Smuzhiyun module_param(debug, int, 0644);
33*4882a593Smuzhiyun MODULE_PARM_DESC(debug, "debug level (0-2)");
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun MODULE_DESCRIPTION("Analog Devices ADV7511 HDMI Transmitter Device Driver");
36*4882a593Smuzhiyun MODULE_AUTHOR("Hans Verkuil");
37*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun #define MASK_ADV7511_EDID_RDY_INT   0x04
40*4882a593Smuzhiyun #define MASK_ADV7511_MSEN_INT       0x40
41*4882a593Smuzhiyun #define MASK_ADV7511_HPD_INT        0x80
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun #define MASK_ADV7511_HPD_DETECT     0x40
44*4882a593Smuzhiyun #define MASK_ADV7511_MSEN_DETECT    0x20
45*4882a593Smuzhiyun #define MASK_ADV7511_EDID_RDY       0x10
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun #define EDID_MAX_RETRIES (8)
48*4882a593Smuzhiyun #define EDID_DELAY 250
49*4882a593Smuzhiyun #define EDID_MAX_SEGM 8
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun #define ADV7511_MAX_WIDTH 1920
52*4882a593Smuzhiyun #define ADV7511_MAX_HEIGHT 1200
53*4882a593Smuzhiyun #define ADV7511_MIN_PIXELCLOCK 20000000
54*4882a593Smuzhiyun #define ADV7511_MAX_PIXELCLOCK 225000000
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun #define ADV7511_MAX_ADDRS (3)
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun /*
59*4882a593Smuzhiyun **********************************************************************
60*4882a593Smuzhiyun *
61*4882a593Smuzhiyun *  Arrays with configuration parameters for the ADV7511
62*4882a593Smuzhiyun *
63*4882a593Smuzhiyun **********************************************************************
64*4882a593Smuzhiyun */
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun struct i2c_reg_value {
67*4882a593Smuzhiyun 	unsigned char reg;
68*4882a593Smuzhiyun 	unsigned char value;
69*4882a593Smuzhiyun };
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun struct adv7511_state_edid {
72*4882a593Smuzhiyun 	/* total number of blocks */
73*4882a593Smuzhiyun 	u32 blocks;
74*4882a593Smuzhiyun 	/* Number of segments read */
75*4882a593Smuzhiyun 	u32 segments;
76*4882a593Smuzhiyun 	u8 data[EDID_MAX_SEGM * 256];
77*4882a593Smuzhiyun 	/* Number of EDID read retries left */
78*4882a593Smuzhiyun 	unsigned read_retries;
79*4882a593Smuzhiyun 	bool complete;
80*4882a593Smuzhiyun };
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun struct adv7511_state {
83*4882a593Smuzhiyun 	struct adv7511_platform_data pdata;
84*4882a593Smuzhiyun 	struct v4l2_subdev sd;
85*4882a593Smuzhiyun 	struct media_pad pad;
86*4882a593Smuzhiyun 	struct v4l2_ctrl_handler hdl;
87*4882a593Smuzhiyun 	int chip_revision;
88*4882a593Smuzhiyun 	u8 i2c_edid_addr;
89*4882a593Smuzhiyun 	u8 i2c_pktmem_addr;
90*4882a593Smuzhiyun 	u8 i2c_cec_addr;
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	struct i2c_client *i2c_cec;
93*4882a593Smuzhiyun 	struct cec_adapter *cec_adap;
94*4882a593Smuzhiyun 	u8   cec_addr[ADV7511_MAX_ADDRS];
95*4882a593Smuzhiyun 	u8   cec_valid_addrs;
96*4882a593Smuzhiyun 	bool cec_enabled_adap;
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	/* Is the adv7511 powered on? */
99*4882a593Smuzhiyun 	bool power_on;
100*4882a593Smuzhiyun 	/* Did we receive hotplug and rx-sense signals? */
101*4882a593Smuzhiyun 	bool have_monitor;
102*4882a593Smuzhiyun 	bool enabled_irq;
103*4882a593Smuzhiyun 	/* timings from s_dv_timings */
104*4882a593Smuzhiyun 	struct v4l2_dv_timings dv_timings;
105*4882a593Smuzhiyun 	u32 fmt_code;
106*4882a593Smuzhiyun 	u32 colorspace;
107*4882a593Smuzhiyun 	u32 ycbcr_enc;
108*4882a593Smuzhiyun 	u32 quantization;
109*4882a593Smuzhiyun 	u32 xfer_func;
110*4882a593Smuzhiyun 	u32 content_type;
111*4882a593Smuzhiyun 	/* controls */
112*4882a593Smuzhiyun 	struct v4l2_ctrl *hdmi_mode_ctrl;
113*4882a593Smuzhiyun 	struct v4l2_ctrl *hotplug_ctrl;
114*4882a593Smuzhiyun 	struct v4l2_ctrl *rx_sense_ctrl;
115*4882a593Smuzhiyun 	struct v4l2_ctrl *have_edid0_ctrl;
116*4882a593Smuzhiyun 	struct v4l2_ctrl *rgb_quantization_range_ctrl;
117*4882a593Smuzhiyun 	struct v4l2_ctrl *content_type_ctrl;
118*4882a593Smuzhiyun 	struct i2c_client *i2c_edid;
119*4882a593Smuzhiyun 	struct i2c_client *i2c_pktmem;
120*4882a593Smuzhiyun 	struct adv7511_state_edid edid;
121*4882a593Smuzhiyun 	/* Running counter of the number of detected EDIDs (for debugging) */
122*4882a593Smuzhiyun 	unsigned edid_detect_counter;
123*4882a593Smuzhiyun 	struct workqueue_struct *work_queue;
124*4882a593Smuzhiyun 	struct delayed_work edid_handler; /* work entry */
125*4882a593Smuzhiyun };
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun static void adv7511_check_monitor_present_status(struct v4l2_subdev *sd);
128*4882a593Smuzhiyun static bool adv7511_check_edid_status(struct v4l2_subdev *sd);
129*4882a593Smuzhiyun static void adv7511_setup(struct v4l2_subdev *sd);
130*4882a593Smuzhiyun static int adv7511_s_i2s_clock_freq(struct v4l2_subdev *sd, u32 freq);
131*4882a593Smuzhiyun static int adv7511_s_clock_freq(struct v4l2_subdev *sd, u32 freq);
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun static const struct v4l2_dv_timings_cap adv7511_timings_cap = {
135*4882a593Smuzhiyun 	.type = V4L2_DV_BT_656_1120,
136*4882a593Smuzhiyun 	/* keep this initialization for compatibility with GCC < 4.4.6 */
137*4882a593Smuzhiyun 	.reserved = { 0 },
138*4882a593Smuzhiyun 	V4L2_INIT_BT_TIMINGS(640, ADV7511_MAX_WIDTH, 350, ADV7511_MAX_HEIGHT,
139*4882a593Smuzhiyun 		ADV7511_MIN_PIXELCLOCK, ADV7511_MAX_PIXELCLOCK,
140*4882a593Smuzhiyun 		V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
141*4882a593Smuzhiyun 			V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT,
142*4882a593Smuzhiyun 		V4L2_DV_BT_CAP_PROGRESSIVE | V4L2_DV_BT_CAP_REDUCED_BLANKING |
143*4882a593Smuzhiyun 			V4L2_DV_BT_CAP_CUSTOM)
144*4882a593Smuzhiyun };
145*4882a593Smuzhiyun 
get_adv7511_state(struct v4l2_subdev * sd)146*4882a593Smuzhiyun static inline struct adv7511_state *get_adv7511_state(struct v4l2_subdev *sd)
147*4882a593Smuzhiyun {
148*4882a593Smuzhiyun 	return container_of(sd, struct adv7511_state, sd);
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun 
to_sd(struct v4l2_ctrl * ctrl)151*4882a593Smuzhiyun static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
152*4882a593Smuzhiyun {
153*4882a593Smuzhiyun 	return &container_of(ctrl->handler, struct adv7511_state, hdl)->sd;
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun /* ------------------------ I2C ----------------------------------------------- */
157*4882a593Smuzhiyun 
adv_smbus_read_byte_data_check(struct i2c_client * client,u8 command,bool check)158*4882a593Smuzhiyun static s32 adv_smbus_read_byte_data_check(struct i2c_client *client,
159*4882a593Smuzhiyun 					  u8 command, bool check)
160*4882a593Smuzhiyun {
161*4882a593Smuzhiyun 	union i2c_smbus_data data;
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	if (!i2c_smbus_xfer(client->adapter, client->addr, client->flags,
164*4882a593Smuzhiyun 			    I2C_SMBUS_READ, command,
165*4882a593Smuzhiyun 			    I2C_SMBUS_BYTE_DATA, &data))
166*4882a593Smuzhiyun 		return data.byte;
167*4882a593Smuzhiyun 	if (check)
168*4882a593Smuzhiyun 		v4l_err(client, "error reading %02x, %02x\n",
169*4882a593Smuzhiyun 			client->addr, command);
170*4882a593Smuzhiyun 	return -1;
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun 
adv_smbus_read_byte_data(struct i2c_client * client,u8 command)173*4882a593Smuzhiyun static s32 adv_smbus_read_byte_data(struct i2c_client *client, u8 command)
174*4882a593Smuzhiyun {
175*4882a593Smuzhiyun 	int i;
176*4882a593Smuzhiyun 	for (i = 0; i < 3; i++) {
177*4882a593Smuzhiyun 		int ret = adv_smbus_read_byte_data_check(client, command, true);
178*4882a593Smuzhiyun 		if (ret >= 0) {
179*4882a593Smuzhiyun 			if (i)
180*4882a593Smuzhiyun 				v4l_err(client, "read ok after %d retries\n", i);
181*4882a593Smuzhiyun 			return ret;
182*4882a593Smuzhiyun 		}
183*4882a593Smuzhiyun 	}
184*4882a593Smuzhiyun 	v4l_err(client, "read failed\n");
185*4882a593Smuzhiyun 	return -1;
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun 
adv7511_rd(struct v4l2_subdev * sd,u8 reg)188*4882a593Smuzhiyun static int adv7511_rd(struct v4l2_subdev *sd, u8 reg)
189*4882a593Smuzhiyun {
190*4882a593Smuzhiyun 	struct i2c_client *client = v4l2_get_subdevdata(sd);
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 	return adv_smbus_read_byte_data(client, reg);
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun 
adv7511_wr(struct v4l2_subdev * sd,u8 reg,u8 val)195*4882a593Smuzhiyun static int adv7511_wr(struct v4l2_subdev *sd, u8 reg, u8 val)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun 	struct i2c_client *client = v4l2_get_subdevdata(sd);
198*4882a593Smuzhiyun 	int ret;
199*4882a593Smuzhiyun 	int i;
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	for (i = 0; i < 3; i++) {
202*4882a593Smuzhiyun 		ret = i2c_smbus_write_byte_data(client, reg, val);
203*4882a593Smuzhiyun 		if (ret == 0)
204*4882a593Smuzhiyun 			return 0;
205*4882a593Smuzhiyun 	}
206*4882a593Smuzhiyun 	v4l2_err(sd, "%s: i2c write error\n", __func__);
207*4882a593Smuzhiyun 	return ret;
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun /* To set specific bits in the register, a clear-mask is given (to be AND-ed),
211*4882a593Smuzhiyun    and then the value-mask (to be OR-ed). */
adv7511_wr_and_or(struct v4l2_subdev * sd,u8 reg,u8 clr_mask,u8 val_mask)212*4882a593Smuzhiyun static inline void adv7511_wr_and_or(struct v4l2_subdev *sd, u8 reg, u8 clr_mask, u8 val_mask)
213*4882a593Smuzhiyun {
214*4882a593Smuzhiyun 	adv7511_wr(sd, reg, (adv7511_rd(sd, reg) & clr_mask) | val_mask);
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun 
adv_smbus_read_i2c_block_data(struct i2c_client * client,u8 command,unsigned length,u8 * values)217*4882a593Smuzhiyun static int adv_smbus_read_i2c_block_data(struct i2c_client *client,
218*4882a593Smuzhiyun 					 u8 command, unsigned length, u8 *values)
219*4882a593Smuzhiyun {
220*4882a593Smuzhiyun 	union i2c_smbus_data data;
221*4882a593Smuzhiyun 	int ret;
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 	if (length > I2C_SMBUS_BLOCK_MAX)
224*4882a593Smuzhiyun 		length = I2C_SMBUS_BLOCK_MAX;
225*4882a593Smuzhiyun 	data.block[0] = length;
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun 	ret = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
228*4882a593Smuzhiyun 			     I2C_SMBUS_READ, command,
229*4882a593Smuzhiyun 			     I2C_SMBUS_I2C_BLOCK_DATA, &data);
230*4882a593Smuzhiyun 	memcpy(values, data.block + 1, length);
231*4882a593Smuzhiyun 	return ret;
232*4882a593Smuzhiyun }
233*4882a593Smuzhiyun 
adv7511_edid_rd(struct v4l2_subdev * sd,uint16_t len,uint8_t * buf)234*4882a593Smuzhiyun static void adv7511_edid_rd(struct v4l2_subdev *sd, uint16_t len, uint8_t *buf)
235*4882a593Smuzhiyun {
236*4882a593Smuzhiyun 	struct adv7511_state *state = get_adv7511_state(sd);
237*4882a593Smuzhiyun 	int i;
238*4882a593Smuzhiyun 	int err = 0;
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 	v4l2_dbg(1, debug, sd, "%s:\n", __func__);
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	for (i = 0; !err && i < len; i += I2C_SMBUS_BLOCK_MAX)
243*4882a593Smuzhiyun 		err = adv_smbus_read_i2c_block_data(state->i2c_edid, i,
244*4882a593Smuzhiyun 						    I2C_SMBUS_BLOCK_MAX, buf + i);
245*4882a593Smuzhiyun 	if (err)
246*4882a593Smuzhiyun 		v4l2_err(sd, "%s: i2c read error\n", __func__);
247*4882a593Smuzhiyun }
248*4882a593Smuzhiyun 
adv7511_cec_read(struct v4l2_subdev * sd,u8 reg)249*4882a593Smuzhiyun static inline int adv7511_cec_read(struct v4l2_subdev *sd, u8 reg)
250*4882a593Smuzhiyun {
251*4882a593Smuzhiyun 	struct adv7511_state *state = get_adv7511_state(sd);
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	return i2c_smbus_read_byte_data(state->i2c_cec, reg);
254*4882a593Smuzhiyun }
255*4882a593Smuzhiyun 
adv7511_cec_write(struct v4l2_subdev * sd,u8 reg,u8 val)256*4882a593Smuzhiyun static int adv7511_cec_write(struct v4l2_subdev *sd, u8 reg, u8 val)
257*4882a593Smuzhiyun {
258*4882a593Smuzhiyun 	struct adv7511_state *state = get_adv7511_state(sd);
259*4882a593Smuzhiyun 	int ret;
260*4882a593Smuzhiyun 	int i;
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	for (i = 0; i < 3; i++) {
263*4882a593Smuzhiyun 		ret = i2c_smbus_write_byte_data(state->i2c_cec, reg, val);
264*4882a593Smuzhiyun 		if (ret == 0)
265*4882a593Smuzhiyun 			return 0;
266*4882a593Smuzhiyun 	}
267*4882a593Smuzhiyun 	v4l2_err(sd, "%s: I2C Write Problem\n", __func__);
268*4882a593Smuzhiyun 	return ret;
269*4882a593Smuzhiyun }
270*4882a593Smuzhiyun 
adv7511_cec_write_and_or(struct v4l2_subdev * sd,u8 reg,u8 mask,u8 val)271*4882a593Smuzhiyun static inline int adv7511_cec_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask,
272*4882a593Smuzhiyun 				   u8 val)
273*4882a593Smuzhiyun {
274*4882a593Smuzhiyun 	return adv7511_cec_write(sd, reg, (adv7511_cec_read(sd, reg) & mask) | val);
275*4882a593Smuzhiyun }
276*4882a593Smuzhiyun 
adv7511_pktmem_rd(struct v4l2_subdev * sd,u8 reg)277*4882a593Smuzhiyun static int adv7511_pktmem_rd(struct v4l2_subdev *sd, u8 reg)
278*4882a593Smuzhiyun {
279*4882a593Smuzhiyun 	struct adv7511_state *state = get_adv7511_state(sd);
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	return adv_smbus_read_byte_data(state->i2c_pktmem, reg);
282*4882a593Smuzhiyun }
283*4882a593Smuzhiyun 
adv7511_pktmem_wr(struct v4l2_subdev * sd,u8 reg,u8 val)284*4882a593Smuzhiyun static int adv7511_pktmem_wr(struct v4l2_subdev *sd, u8 reg, u8 val)
285*4882a593Smuzhiyun {
286*4882a593Smuzhiyun 	struct adv7511_state *state = get_adv7511_state(sd);
287*4882a593Smuzhiyun 	int ret;
288*4882a593Smuzhiyun 	int i;
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 	for (i = 0; i < 3; i++) {
291*4882a593Smuzhiyun 		ret = i2c_smbus_write_byte_data(state->i2c_pktmem, reg, val);
292*4882a593Smuzhiyun 		if (ret == 0)
293*4882a593Smuzhiyun 			return 0;
294*4882a593Smuzhiyun 	}
295*4882a593Smuzhiyun 	v4l2_err(sd, "%s: i2c write error\n", __func__);
296*4882a593Smuzhiyun 	return ret;
297*4882a593Smuzhiyun }
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun /* To set specific bits in the register, a clear-mask is given (to be AND-ed),
300*4882a593Smuzhiyun    and then the value-mask (to be OR-ed). */
adv7511_pktmem_wr_and_or(struct v4l2_subdev * sd,u8 reg,u8 clr_mask,u8 val_mask)301*4882a593Smuzhiyun static inline void adv7511_pktmem_wr_and_or(struct v4l2_subdev *sd, u8 reg, u8 clr_mask, u8 val_mask)
302*4882a593Smuzhiyun {
303*4882a593Smuzhiyun 	adv7511_pktmem_wr(sd, reg, (adv7511_pktmem_rd(sd, reg) & clr_mask) | val_mask);
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun 
adv7511_have_hotplug(struct v4l2_subdev * sd)306*4882a593Smuzhiyun static inline bool adv7511_have_hotplug(struct v4l2_subdev *sd)
307*4882a593Smuzhiyun {
308*4882a593Smuzhiyun 	return adv7511_rd(sd, 0x42) & MASK_ADV7511_HPD_DETECT;
309*4882a593Smuzhiyun }
310*4882a593Smuzhiyun 
adv7511_have_rx_sense(struct v4l2_subdev * sd)311*4882a593Smuzhiyun static inline bool adv7511_have_rx_sense(struct v4l2_subdev *sd)
312*4882a593Smuzhiyun {
313*4882a593Smuzhiyun 	return adv7511_rd(sd, 0x42) & MASK_ADV7511_MSEN_DETECT;
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun 
adv7511_csc_conversion_mode(struct v4l2_subdev * sd,u8 mode)316*4882a593Smuzhiyun static void adv7511_csc_conversion_mode(struct v4l2_subdev *sd, u8 mode)
317*4882a593Smuzhiyun {
318*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0x18, 0x9f, (mode & 0x3)<<5);
319*4882a593Smuzhiyun }
320*4882a593Smuzhiyun 
adv7511_csc_coeff(struct v4l2_subdev * sd,u16 A1,u16 A2,u16 A3,u16 A4,u16 B1,u16 B2,u16 B3,u16 B4,u16 C1,u16 C2,u16 C3,u16 C4)321*4882a593Smuzhiyun static void adv7511_csc_coeff(struct v4l2_subdev *sd,
322*4882a593Smuzhiyun 			      u16 A1, u16 A2, u16 A3, u16 A4,
323*4882a593Smuzhiyun 			      u16 B1, u16 B2, u16 B3, u16 B4,
324*4882a593Smuzhiyun 			      u16 C1, u16 C2, u16 C3, u16 C4)
325*4882a593Smuzhiyun {
326*4882a593Smuzhiyun 	/* A */
327*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0x18, 0xe0, A1>>8);
328*4882a593Smuzhiyun 	adv7511_wr(sd, 0x19, A1);
329*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0x1A, 0xe0, A2>>8);
330*4882a593Smuzhiyun 	adv7511_wr(sd, 0x1B, A2);
331*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0x1c, 0xe0, A3>>8);
332*4882a593Smuzhiyun 	adv7511_wr(sd, 0x1d, A3);
333*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0x1e, 0xe0, A4>>8);
334*4882a593Smuzhiyun 	adv7511_wr(sd, 0x1f, A4);
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun 	/* B */
337*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0x20, 0xe0, B1>>8);
338*4882a593Smuzhiyun 	adv7511_wr(sd, 0x21, B1);
339*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0x22, 0xe0, B2>>8);
340*4882a593Smuzhiyun 	adv7511_wr(sd, 0x23, B2);
341*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0x24, 0xe0, B3>>8);
342*4882a593Smuzhiyun 	adv7511_wr(sd, 0x25, B3);
343*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0x26, 0xe0, B4>>8);
344*4882a593Smuzhiyun 	adv7511_wr(sd, 0x27, B4);
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun 	/* C */
347*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0x28, 0xe0, C1>>8);
348*4882a593Smuzhiyun 	adv7511_wr(sd, 0x29, C1);
349*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0x2A, 0xe0, C2>>8);
350*4882a593Smuzhiyun 	adv7511_wr(sd, 0x2B, C2);
351*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0x2C, 0xe0, C3>>8);
352*4882a593Smuzhiyun 	adv7511_wr(sd, 0x2D, C3);
353*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0x2E, 0xe0, C4>>8);
354*4882a593Smuzhiyun 	adv7511_wr(sd, 0x2F, C4);
355*4882a593Smuzhiyun }
356*4882a593Smuzhiyun 
adv7511_csc_rgb_full2limit(struct v4l2_subdev * sd,bool enable)357*4882a593Smuzhiyun static void adv7511_csc_rgb_full2limit(struct v4l2_subdev *sd, bool enable)
358*4882a593Smuzhiyun {
359*4882a593Smuzhiyun 	if (enable) {
360*4882a593Smuzhiyun 		u8 csc_mode = 0;
361*4882a593Smuzhiyun 		adv7511_csc_conversion_mode(sd, csc_mode);
362*4882a593Smuzhiyun 		adv7511_csc_coeff(sd,
363*4882a593Smuzhiyun 				  4096-564, 0, 0, 256,
364*4882a593Smuzhiyun 				  0, 4096-564, 0, 256,
365*4882a593Smuzhiyun 				  0, 0, 4096-564, 256);
366*4882a593Smuzhiyun 		/* enable CSC */
367*4882a593Smuzhiyun 		adv7511_wr_and_or(sd, 0x18, 0x7f, 0x80);
368*4882a593Smuzhiyun 		/* AVI infoframe: Limited range RGB (16-235) */
369*4882a593Smuzhiyun 		adv7511_wr_and_or(sd, 0x57, 0xf3, 0x04);
370*4882a593Smuzhiyun 	} else {
371*4882a593Smuzhiyun 		/* disable CSC */
372*4882a593Smuzhiyun 		adv7511_wr_and_or(sd, 0x18, 0x7f, 0x0);
373*4882a593Smuzhiyun 		/* AVI infoframe: Full range RGB (0-255) */
374*4882a593Smuzhiyun 		adv7511_wr_and_or(sd, 0x57, 0xf3, 0x08);
375*4882a593Smuzhiyun 	}
376*4882a593Smuzhiyun }
377*4882a593Smuzhiyun 
adv7511_set_rgb_quantization_mode(struct v4l2_subdev * sd,struct v4l2_ctrl * ctrl)378*4882a593Smuzhiyun static void adv7511_set_rgb_quantization_mode(struct v4l2_subdev *sd, struct v4l2_ctrl *ctrl)
379*4882a593Smuzhiyun {
380*4882a593Smuzhiyun 	struct adv7511_state *state = get_adv7511_state(sd);
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun 	/* Only makes sense for RGB formats */
383*4882a593Smuzhiyun 	if (state->fmt_code != MEDIA_BUS_FMT_RGB888_1X24) {
384*4882a593Smuzhiyun 		/* so just keep quantization */
385*4882a593Smuzhiyun 		adv7511_csc_rgb_full2limit(sd, false);
386*4882a593Smuzhiyun 		return;
387*4882a593Smuzhiyun 	}
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun 	switch (ctrl->val) {
390*4882a593Smuzhiyun 	case V4L2_DV_RGB_RANGE_AUTO:
391*4882a593Smuzhiyun 		/* automatic */
392*4882a593Smuzhiyun 		if (state->dv_timings.bt.flags & V4L2_DV_FL_IS_CE_VIDEO) {
393*4882a593Smuzhiyun 			/* CE format, RGB limited range (16-235) */
394*4882a593Smuzhiyun 			adv7511_csc_rgb_full2limit(sd, true);
395*4882a593Smuzhiyun 		} else {
396*4882a593Smuzhiyun 			/* not CE format, RGB full range (0-255) */
397*4882a593Smuzhiyun 			adv7511_csc_rgb_full2limit(sd, false);
398*4882a593Smuzhiyun 		}
399*4882a593Smuzhiyun 		break;
400*4882a593Smuzhiyun 	case V4L2_DV_RGB_RANGE_LIMITED:
401*4882a593Smuzhiyun 		/* RGB limited range (16-235) */
402*4882a593Smuzhiyun 		adv7511_csc_rgb_full2limit(sd, true);
403*4882a593Smuzhiyun 		break;
404*4882a593Smuzhiyun 	case V4L2_DV_RGB_RANGE_FULL:
405*4882a593Smuzhiyun 		/* RGB full range (0-255) */
406*4882a593Smuzhiyun 		adv7511_csc_rgb_full2limit(sd, false);
407*4882a593Smuzhiyun 		break;
408*4882a593Smuzhiyun 	}
409*4882a593Smuzhiyun }
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun /* ------------------------------ CTRL OPS ------------------------------ */
412*4882a593Smuzhiyun 
adv7511_s_ctrl(struct v4l2_ctrl * ctrl)413*4882a593Smuzhiyun static int adv7511_s_ctrl(struct v4l2_ctrl *ctrl)
414*4882a593Smuzhiyun {
415*4882a593Smuzhiyun 	struct v4l2_subdev *sd = to_sd(ctrl);
416*4882a593Smuzhiyun 	struct adv7511_state *state = get_adv7511_state(sd);
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 	v4l2_dbg(1, debug, sd, "%s: ctrl id: %d, ctrl->val %d\n", __func__, ctrl->id, ctrl->val);
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 	if (state->hdmi_mode_ctrl == ctrl) {
421*4882a593Smuzhiyun 		/* Set HDMI or DVI-D */
422*4882a593Smuzhiyun 		adv7511_wr_and_or(sd, 0xaf, 0xfd, ctrl->val == V4L2_DV_TX_MODE_HDMI ? 0x02 : 0x00);
423*4882a593Smuzhiyun 		return 0;
424*4882a593Smuzhiyun 	}
425*4882a593Smuzhiyun 	if (state->rgb_quantization_range_ctrl == ctrl) {
426*4882a593Smuzhiyun 		adv7511_set_rgb_quantization_mode(sd, ctrl);
427*4882a593Smuzhiyun 		return 0;
428*4882a593Smuzhiyun 	}
429*4882a593Smuzhiyun 	if (state->content_type_ctrl == ctrl) {
430*4882a593Smuzhiyun 		u8 itc, cn;
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 		state->content_type = ctrl->val;
433*4882a593Smuzhiyun 		itc = state->content_type != V4L2_DV_IT_CONTENT_TYPE_NO_ITC;
434*4882a593Smuzhiyun 		cn = itc ? state->content_type : V4L2_DV_IT_CONTENT_TYPE_GRAPHICS;
435*4882a593Smuzhiyun 		adv7511_wr_and_or(sd, 0x57, 0x7f, itc << 7);
436*4882a593Smuzhiyun 		adv7511_wr_and_or(sd, 0x59, 0xcf, cn << 4);
437*4882a593Smuzhiyun 		return 0;
438*4882a593Smuzhiyun 	}
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun 	return -EINVAL;
441*4882a593Smuzhiyun }
442*4882a593Smuzhiyun 
443*4882a593Smuzhiyun static const struct v4l2_ctrl_ops adv7511_ctrl_ops = {
444*4882a593Smuzhiyun 	.s_ctrl = adv7511_s_ctrl,
445*4882a593Smuzhiyun };
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun /* ---------------------------- CORE OPS ------------------------------------------- */
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun #ifdef CONFIG_VIDEO_ADV_DEBUG
adv7511_inv_register(struct v4l2_subdev * sd)450*4882a593Smuzhiyun static void adv7511_inv_register(struct v4l2_subdev *sd)
451*4882a593Smuzhiyun {
452*4882a593Smuzhiyun 	struct adv7511_state *state = get_adv7511_state(sd);
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun 	v4l2_info(sd, "0x000-0x0ff: Main Map\n");
455*4882a593Smuzhiyun 	if (state->i2c_cec)
456*4882a593Smuzhiyun 		v4l2_info(sd, "0x100-0x1ff: CEC Map\n");
457*4882a593Smuzhiyun }
458*4882a593Smuzhiyun 
adv7511_g_register(struct v4l2_subdev * sd,struct v4l2_dbg_register * reg)459*4882a593Smuzhiyun static int adv7511_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
460*4882a593Smuzhiyun {
461*4882a593Smuzhiyun 	struct adv7511_state *state = get_adv7511_state(sd);
462*4882a593Smuzhiyun 
463*4882a593Smuzhiyun 	reg->size = 1;
464*4882a593Smuzhiyun 	switch (reg->reg >> 8) {
465*4882a593Smuzhiyun 	case 0:
466*4882a593Smuzhiyun 		reg->val = adv7511_rd(sd, reg->reg & 0xff);
467*4882a593Smuzhiyun 		break;
468*4882a593Smuzhiyun 	case 1:
469*4882a593Smuzhiyun 		if (state->i2c_cec) {
470*4882a593Smuzhiyun 			reg->val = adv7511_cec_read(sd, reg->reg & 0xff);
471*4882a593Smuzhiyun 			break;
472*4882a593Smuzhiyun 		}
473*4882a593Smuzhiyun 		fallthrough;
474*4882a593Smuzhiyun 	default:
475*4882a593Smuzhiyun 		v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
476*4882a593Smuzhiyun 		adv7511_inv_register(sd);
477*4882a593Smuzhiyun 		break;
478*4882a593Smuzhiyun 	}
479*4882a593Smuzhiyun 	return 0;
480*4882a593Smuzhiyun }
481*4882a593Smuzhiyun 
adv7511_s_register(struct v4l2_subdev * sd,const struct v4l2_dbg_register * reg)482*4882a593Smuzhiyun static int adv7511_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
483*4882a593Smuzhiyun {
484*4882a593Smuzhiyun 	struct adv7511_state *state = get_adv7511_state(sd);
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun 	switch (reg->reg >> 8) {
487*4882a593Smuzhiyun 	case 0:
488*4882a593Smuzhiyun 		adv7511_wr(sd, reg->reg & 0xff, reg->val & 0xff);
489*4882a593Smuzhiyun 		break;
490*4882a593Smuzhiyun 	case 1:
491*4882a593Smuzhiyun 		if (state->i2c_cec) {
492*4882a593Smuzhiyun 			adv7511_cec_write(sd, reg->reg & 0xff, reg->val & 0xff);
493*4882a593Smuzhiyun 			break;
494*4882a593Smuzhiyun 		}
495*4882a593Smuzhiyun 		fallthrough;
496*4882a593Smuzhiyun 	default:
497*4882a593Smuzhiyun 		v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
498*4882a593Smuzhiyun 		adv7511_inv_register(sd);
499*4882a593Smuzhiyun 		break;
500*4882a593Smuzhiyun 	}
501*4882a593Smuzhiyun 	return 0;
502*4882a593Smuzhiyun }
503*4882a593Smuzhiyun #endif
504*4882a593Smuzhiyun 
505*4882a593Smuzhiyun struct adv7511_cfg_read_infoframe {
506*4882a593Smuzhiyun 	const char *desc;
507*4882a593Smuzhiyun 	u8 present_reg;
508*4882a593Smuzhiyun 	u8 present_mask;
509*4882a593Smuzhiyun 	u8 header[3];
510*4882a593Smuzhiyun 	u16 payload_addr;
511*4882a593Smuzhiyun };
512*4882a593Smuzhiyun 
hdmi_infoframe_checksum(u8 * ptr,size_t size)513*4882a593Smuzhiyun static u8 hdmi_infoframe_checksum(u8 *ptr, size_t size)
514*4882a593Smuzhiyun {
515*4882a593Smuzhiyun 	u8 csum = 0;
516*4882a593Smuzhiyun 	size_t i;
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun 	/* compute checksum */
519*4882a593Smuzhiyun 	for (i = 0; i < size; i++)
520*4882a593Smuzhiyun 		csum += ptr[i];
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun 	return 256 - csum;
523*4882a593Smuzhiyun }
524*4882a593Smuzhiyun 
log_infoframe(struct v4l2_subdev * sd,const struct adv7511_cfg_read_infoframe * cri)525*4882a593Smuzhiyun static void log_infoframe(struct v4l2_subdev *sd, const struct adv7511_cfg_read_infoframe *cri)
526*4882a593Smuzhiyun {
527*4882a593Smuzhiyun 	struct i2c_client *client = v4l2_get_subdevdata(sd);
528*4882a593Smuzhiyun 	struct device *dev = &client->dev;
529*4882a593Smuzhiyun 	union hdmi_infoframe frame;
530*4882a593Smuzhiyun 	u8 buffer[32];
531*4882a593Smuzhiyun 	u8 len;
532*4882a593Smuzhiyun 	int i;
533*4882a593Smuzhiyun 
534*4882a593Smuzhiyun 	if (!(adv7511_rd(sd, cri->present_reg) & cri->present_mask)) {
535*4882a593Smuzhiyun 		v4l2_info(sd, "%s infoframe not transmitted\n", cri->desc);
536*4882a593Smuzhiyun 		return;
537*4882a593Smuzhiyun 	}
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun 	memcpy(buffer, cri->header, sizeof(cri->header));
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 	len = buffer[2];
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun 	if (len + 4 > sizeof(buffer)) {
544*4882a593Smuzhiyun 		v4l2_err(sd, "%s: invalid %s infoframe length %d\n", __func__, cri->desc, len);
545*4882a593Smuzhiyun 		return;
546*4882a593Smuzhiyun 	}
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun 	if (cri->payload_addr >= 0x100) {
549*4882a593Smuzhiyun 		for (i = 0; i < len; i++)
550*4882a593Smuzhiyun 			buffer[i + 4] = adv7511_pktmem_rd(sd, cri->payload_addr + i - 0x100);
551*4882a593Smuzhiyun 	} else {
552*4882a593Smuzhiyun 		for (i = 0; i < len; i++)
553*4882a593Smuzhiyun 			buffer[i + 4] = adv7511_rd(sd, cri->payload_addr + i);
554*4882a593Smuzhiyun 	}
555*4882a593Smuzhiyun 	buffer[3] = 0;
556*4882a593Smuzhiyun 	buffer[3] = hdmi_infoframe_checksum(buffer, len + 4);
557*4882a593Smuzhiyun 
558*4882a593Smuzhiyun 	if (hdmi_infoframe_unpack(&frame, buffer, len + 4) < 0) {
559*4882a593Smuzhiyun 		v4l2_err(sd, "%s: unpack of %s infoframe failed\n", __func__, cri->desc);
560*4882a593Smuzhiyun 		return;
561*4882a593Smuzhiyun 	}
562*4882a593Smuzhiyun 
563*4882a593Smuzhiyun 	hdmi_infoframe_log(KERN_INFO, dev, &frame);
564*4882a593Smuzhiyun }
565*4882a593Smuzhiyun 
adv7511_log_infoframes(struct v4l2_subdev * sd)566*4882a593Smuzhiyun static void adv7511_log_infoframes(struct v4l2_subdev *sd)
567*4882a593Smuzhiyun {
568*4882a593Smuzhiyun 	static const struct adv7511_cfg_read_infoframe cri[] = {
569*4882a593Smuzhiyun 		{ "AVI", 0x44, 0x10, { 0x82, 2, 13 }, 0x55 },
570*4882a593Smuzhiyun 		{ "Audio", 0x44, 0x08, { 0x84, 1, 10 }, 0x73 },
571*4882a593Smuzhiyun 		{ "SDP", 0x40, 0x40, { 0x83, 1, 25 }, 0x103 },
572*4882a593Smuzhiyun 	};
573*4882a593Smuzhiyun 	int i;
574*4882a593Smuzhiyun 
575*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(cri); i++)
576*4882a593Smuzhiyun 		log_infoframe(sd, &cri[i]);
577*4882a593Smuzhiyun }
578*4882a593Smuzhiyun 
adv7511_log_status(struct v4l2_subdev * sd)579*4882a593Smuzhiyun static int adv7511_log_status(struct v4l2_subdev *sd)
580*4882a593Smuzhiyun {
581*4882a593Smuzhiyun 	struct adv7511_state *state = get_adv7511_state(sd);
582*4882a593Smuzhiyun 	struct adv7511_state_edid *edid = &state->edid;
583*4882a593Smuzhiyun 	int i;
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun 	static const char * const states[] = {
586*4882a593Smuzhiyun 		"in reset",
587*4882a593Smuzhiyun 		"reading EDID",
588*4882a593Smuzhiyun 		"idle",
589*4882a593Smuzhiyun 		"initializing HDCP",
590*4882a593Smuzhiyun 		"HDCP enabled",
591*4882a593Smuzhiyun 		"initializing HDCP repeater",
592*4882a593Smuzhiyun 		"6", "7", "8", "9", "A", "B", "C", "D", "E", "F"
593*4882a593Smuzhiyun 	};
594*4882a593Smuzhiyun 	static const char * const errors[] = {
595*4882a593Smuzhiyun 		"no error",
596*4882a593Smuzhiyun 		"bad receiver BKSV",
597*4882a593Smuzhiyun 		"Ri mismatch",
598*4882a593Smuzhiyun 		"Pj mismatch",
599*4882a593Smuzhiyun 		"i2c error",
600*4882a593Smuzhiyun 		"timed out",
601*4882a593Smuzhiyun 		"max repeater cascade exceeded",
602*4882a593Smuzhiyun 		"hash check failed",
603*4882a593Smuzhiyun 		"too many devices",
604*4882a593Smuzhiyun 		"9", "A", "B", "C", "D", "E", "F"
605*4882a593Smuzhiyun 	};
606*4882a593Smuzhiyun 
607*4882a593Smuzhiyun 	v4l2_info(sd, "power %s\n", state->power_on ? "on" : "off");
608*4882a593Smuzhiyun 	v4l2_info(sd, "%s hotplug, %s Rx Sense, %s EDID (%d block(s))\n",
609*4882a593Smuzhiyun 		  (adv7511_rd(sd, 0x42) & MASK_ADV7511_HPD_DETECT) ? "detected" : "no",
610*4882a593Smuzhiyun 		  (adv7511_rd(sd, 0x42) & MASK_ADV7511_MSEN_DETECT) ? "detected" : "no",
611*4882a593Smuzhiyun 		  edid->segments ? "found" : "no",
612*4882a593Smuzhiyun 		  edid->blocks);
613*4882a593Smuzhiyun 	v4l2_info(sd, "%s output %s\n",
614*4882a593Smuzhiyun 		  (adv7511_rd(sd, 0xaf) & 0x02) ?
615*4882a593Smuzhiyun 		  "HDMI" : "DVI-D",
616*4882a593Smuzhiyun 		  (adv7511_rd(sd, 0xa1) & 0x3c) ?
617*4882a593Smuzhiyun 		  "disabled" : "enabled");
618*4882a593Smuzhiyun 	v4l2_info(sd, "state: %s, error: %s, detect count: %u, msk/irq: %02x/%02x\n",
619*4882a593Smuzhiyun 			  states[adv7511_rd(sd, 0xc8) & 0xf],
620*4882a593Smuzhiyun 			  errors[adv7511_rd(sd, 0xc8) >> 4], state->edid_detect_counter,
621*4882a593Smuzhiyun 			  adv7511_rd(sd, 0x94), adv7511_rd(sd, 0x96));
622*4882a593Smuzhiyun 	v4l2_info(sd, "RGB quantization: %s range\n", adv7511_rd(sd, 0x18) & 0x80 ? "limited" : "full");
623*4882a593Smuzhiyun 	if (adv7511_rd(sd, 0xaf) & 0x02) {
624*4882a593Smuzhiyun 		/* HDMI only */
625*4882a593Smuzhiyun 		u8 manual_cts = adv7511_rd(sd, 0x0a) & 0x80;
626*4882a593Smuzhiyun 		u32 N = (adv7511_rd(sd, 0x01) & 0xf) << 16 |
627*4882a593Smuzhiyun 			adv7511_rd(sd, 0x02) << 8 |
628*4882a593Smuzhiyun 			adv7511_rd(sd, 0x03);
629*4882a593Smuzhiyun 		u8 vic_detect = adv7511_rd(sd, 0x3e) >> 2;
630*4882a593Smuzhiyun 		u8 vic_sent = adv7511_rd(sd, 0x3d) & 0x3f;
631*4882a593Smuzhiyun 		u32 CTS;
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun 		if (manual_cts)
634*4882a593Smuzhiyun 			CTS = (adv7511_rd(sd, 0x07) & 0xf) << 16 |
635*4882a593Smuzhiyun 			      adv7511_rd(sd, 0x08) << 8 |
636*4882a593Smuzhiyun 			      adv7511_rd(sd, 0x09);
637*4882a593Smuzhiyun 		else
638*4882a593Smuzhiyun 			CTS = (adv7511_rd(sd, 0x04) & 0xf) << 16 |
639*4882a593Smuzhiyun 			      adv7511_rd(sd, 0x05) << 8 |
640*4882a593Smuzhiyun 			      adv7511_rd(sd, 0x06);
641*4882a593Smuzhiyun 		v4l2_info(sd, "CTS %s mode: N %d, CTS %d\n",
642*4882a593Smuzhiyun 			  manual_cts ? "manual" : "automatic", N, CTS);
643*4882a593Smuzhiyun 		v4l2_info(sd, "VIC: detected %d, sent %d\n",
644*4882a593Smuzhiyun 			  vic_detect, vic_sent);
645*4882a593Smuzhiyun 		adv7511_log_infoframes(sd);
646*4882a593Smuzhiyun 	}
647*4882a593Smuzhiyun 	if (state->dv_timings.type == V4L2_DV_BT_656_1120)
648*4882a593Smuzhiyun 		v4l2_print_dv_timings(sd->name, "timings: ",
649*4882a593Smuzhiyun 				&state->dv_timings, false);
650*4882a593Smuzhiyun 	else
651*4882a593Smuzhiyun 		v4l2_info(sd, "no timings set\n");
652*4882a593Smuzhiyun 	v4l2_info(sd, "i2c edid addr: 0x%x\n", state->i2c_edid_addr);
653*4882a593Smuzhiyun 
654*4882a593Smuzhiyun 	if (state->i2c_cec == NULL)
655*4882a593Smuzhiyun 		return 0;
656*4882a593Smuzhiyun 
657*4882a593Smuzhiyun 	v4l2_info(sd, "i2c cec addr: 0x%x\n", state->i2c_cec_addr);
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun 	v4l2_info(sd, "CEC: %s\n", state->cec_enabled_adap ?
660*4882a593Smuzhiyun 			"enabled" : "disabled");
661*4882a593Smuzhiyun 	if (state->cec_enabled_adap) {
662*4882a593Smuzhiyun 		for (i = 0; i < ADV7511_MAX_ADDRS; i++) {
663*4882a593Smuzhiyun 			bool is_valid = state->cec_valid_addrs & (1 << i);
664*4882a593Smuzhiyun 
665*4882a593Smuzhiyun 			if (is_valid)
666*4882a593Smuzhiyun 				v4l2_info(sd, "CEC Logical Address: 0x%x\n",
667*4882a593Smuzhiyun 					  state->cec_addr[i]);
668*4882a593Smuzhiyun 		}
669*4882a593Smuzhiyun 	}
670*4882a593Smuzhiyun 	v4l2_info(sd, "i2c pktmem addr: 0x%x\n", state->i2c_pktmem_addr);
671*4882a593Smuzhiyun 	return 0;
672*4882a593Smuzhiyun }
673*4882a593Smuzhiyun 
674*4882a593Smuzhiyun /* Power up/down adv7511 */
adv7511_s_power(struct v4l2_subdev * sd,int on)675*4882a593Smuzhiyun static int adv7511_s_power(struct v4l2_subdev *sd, int on)
676*4882a593Smuzhiyun {
677*4882a593Smuzhiyun 	struct adv7511_state *state = get_adv7511_state(sd);
678*4882a593Smuzhiyun 	const int retries = 20;
679*4882a593Smuzhiyun 	int i;
680*4882a593Smuzhiyun 
681*4882a593Smuzhiyun 	v4l2_dbg(1, debug, sd, "%s: power %s\n", __func__, on ? "on" : "off");
682*4882a593Smuzhiyun 
683*4882a593Smuzhiyun 	state->power_on = on;
684*4882a593Smuzhiyun 
685*4882a593Smuzhiyun 	if (!on) {
686*4882a593Smuzhiyun 		/* Power down */
687*4882a593Smuzhiyun 		adv7511_wr_and_or(sd, 0x41, 0xbf, 0x40);
688*4882a593Smuzhiyun 		return true;
689*4882a593Smuzhiyun 	}
690*4882a593Smuzhiyun 
691*4882a593Smuzhiyun 	/* Power up */
692*4882a593Smuzhiyun 	/* The adv7511 does not always come up immediately.
693*4882a593Smuzhiyun 	   Retry multiple times. */
694*4882a593Smuzhiyun 	for (i = 0; i < retries; i++) {
695*4882a593Smuzhiyun 		adv7511_wr_and_or(sd, 0x41, 0xbf, 0x0);
696*4882a593Smuzhiyun 		if ((adv7511_rd(sd, 0x41) & 0x40) == 0)
697*4882a593Smuzhiyun 			break;
698*4882a593Smuzhiyun 		adv7511_wr_and_or(sd, 0x41, 0xbf, 0x40);
699*4882a593Smuzhiyun 		msleep(10);
700*4882a593Smuzhiyun 	}
701*4882a593Smuzhiyun 	if (i == retries) {
702*4882a593Smuzhiyun 		v4l2_dbg(1, debug, sd, "%s: failed to powerup the adv7511!\n", __func__);
703*4882a593Smuzhiyun 		adv7511_s_power(sd, 0);
704*4882a593Smuzhiyun 		return false;
705*4882a593Smuzhiyun 	}
706*4882a593Smuzhiyun 	if (i > 1)
707*4882a593Smuzhiyun 		v4l2_dbg(1, debug, sd, "%s: needed %d retries to powerup the adv7511\n", __func__, i);
708*4882a593Smuzhiyun 
709*4882a593Smuzhiyun 	/* Reserved registers that must be set */
710*4882a593Smuzhiyun 	adv7511_wr(sd, 0x98, 0x03);
711*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0x9a, 0xfe, 0x70);
712*4882a593Smuzhiyun 	adv7511_wr(sd, 0x9c, 0x30);
713*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0x9d, 0xfc, 0x01);
714*4882a593Smuzhiyun 	adv7511_wr(sd, 0xa2, 0xa4);
715*4882a593Smuzhiyun 	adv7511_wr(sd, 0xa3, 0xa4);
716*4882a593Smuzhiyun 	adv7511_wr(sd, 0xe0, 0xd0);
717*4882a593Smuzhiyun 	adv7511_wr(sd, 0xf9, 0x00);
718*4882a593Smuzhiyun 
719*4882a593Smuzhiyun 	adv7511_wr(sd, 0x43, state->i2c_edid_addr);
720*4882a593Smuzhiyun 	adv7511_wr(sd, 0x45, state->i2c_pktmem_addr);
721*4882a593Smuzhiyun 
722*4882a593Smuzhiyun 	/* Set number of attempts to read the EDID */
723*4882a593Smuzhiyun 	adv7511_wr(sd, 0xc9, 0xf);
724*4882a593Smuzhiyun 	return true;
725*4882a593Smuzhiyun }
726*4882a593Smuzhiyun 
727*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_VIDEO_ADV7511_CEC)
adv7511_cec_adap_enable(struct cec_adapter * adap,bool enable)728*4882a593Smuzhiyun static int adv7511_cec_adap_enable(struct cec_adapter *adap, bool enable)
729*4882a593Smuzhiyun {
730*4882a593Smuzhiyun 	struct adv7511_state *state = cec_get_drvdata(adap);
731*4882a593Smuzhiyun 	struct v4l2_subdev *sd = &state->sd;
732*4882a593Smuzhiyun 
733*4882a593Smuzhiyun 	if (state->i2c_cec == NULL)
734*4882a593Smuzhiyun 		return -EIO;
735*4882a593Smuzhiyun 
736*4882a593Smuzhiyun 	if (!state->cec_enabled_adap && enable) {
737*4882a593Smuzhiyun 		/* power up cec section */
738*4882a593Smuzhiyun 		adv7511_cec_write_and_or(sd, 0x4e, 0xfc, 0x01);
739*4882a593Smuzhiyun 		/* legacy mode and clear all rx buffers */
740*4882a593Smuzhiyun 		adv7511_cec_write(sd, 0x4a, 0x00);
741*4882a593Smuzhiyun 		adv7511_cec_write(sd, 0x4a, 0x07);
742*4882a593Smuzhiyun 		adv7511_cec_write_and_or(sd, 0x11, 0xfe, 0); /* initially disable tx */
743*4882a593Smuzhiyun 		/* enabled irqs: */
744*4882a593Smuzhiyun 		/* tx: ready */
745*4882a593Smuzhiyun 		/* tx: arbitration lost */
746*4882a593Smuzhiyun 		/* tx: retry timeout */
747*4882a593Smuzhiyun 		/* rx: ready 1 */
748*4882a593Smuzhiyun 		if (state->enabled_irq)
749*4882a593Smuzhiyun 			adv7511_wr_and_or(sd, 0x95, 0xc0, 0x39);
750*4882a593Smuzhiyun 	} else if (state->cec_enabled_adap && !enable) {
751*4882a593Smuzhiyun 		if (state->enabled_irq)
752*4882a593Smuzhiyun 			adv7511_wr_and_or(sd, 0x95, 0xc0, 0x00);
753*4882a593Smuzhiyun 		/* disable address mask 1-3 */
754*4882a593Smuzhiyun 		adv7511_cec_write_and_or(sd, 0x4b, 0x8f, 0x00);
755*4882a593Smuzhiyun 		/* power down cec section */
756*4882a593Smuzhiyun 		adv7511_cec_write_and_or(sd, 0x4e, 0xfc, 0x00);
757*4882a593Smuzhiyun 		state->cec_valid_addrs = 0;
758*4882a593Smuzhiyun 	}
759*4882a593Smuzhiyun 	state->cec_enabled_adap = enable;
760*4882a593Smuzhiyun 	return 0;
761*4882a593Smuzhiyun }
762*4882a593Smuzhiyun 
adv7511_cec_adap_log_addr(struct cec_adapter * adap,u8 addr)763*4882a593Smuzhiyun static int adv7511_cec_adap_log_addr(struct cec_adapter *adap, u8 addr)
764*4882a593Smuzhiyun {
765*4882a593Smuzhiyun 	struct adv7511_state *state = cec_get_drvdata(adap);
766*4882a593Smuzhiyun 	struct v4l2_subdev *sd = &state->sd;
767*4882a593Smuzhiyun 	unsigned int i, free_idx = ADV7511_MAX_ADDRS;
768*4882a593Smuzhiyun 
769*4882a593Smuzhiyun 	if (!state->cec_enabled_adap)
770*4882a593Smuzhiyun 		return addr == CEC_LOG_ADDR_INVALID ? 0 : -EIO;
771*4882a593Smuzhiyun 
772*4882a593Smuzhiyun 	if (addr == CEC_LOG_ADDR_INVALID) {
773*4882a593Smuzhiyun 		adv7511_cec_write_and_or(sd, 0x4b, 0x8f, 0);
774*4882a593Smuzhiyun 		state->cec_valid_addrs = 0;
775*4882a593Smuzhiyun 		return 0;
776*4882a593Smuzhiyun 	}
777*4882a593Smuzhiyun 
778*4882a593Smuzhiyun 	for (i = 0; i < ADV7511_MAX_ADDRS; i++) {
779*4882a593Smuzhiyun 		bool is_valid = state->cec_valid_addrs & (1 << i);
780*4882a593Smuzhiyun 
781*4882a593Smuzhiyun 		if (free_idx == ADV7511_MAX_ADDRS && !is_valid)
782*4882a593Smuzhiyun 			free_idx = i;
783*4882a593Smuzhiyun 		if (is_valid && state->cec_addr[i] == addr)
784*4882a593Smuzhiyun 			return 0;
785*4882a593Smuzhiyun 	}
786*4882a593Smuzhiyun 	if (i == ADV7511_MAX_ADDRS) {
787*4882a593Smuzhiyun 		i = free_idx;
788*4882a593Smuzhiyun 		if (i == ADV7511_MAX_ADDRS)
789*4882a593Smuzhiyun 			return -ENXIO;
790*4882a593Smuzhiyun 	}
791*4882a593Smuzhiyun 	state->cec_addr[i] = addr;
792*4882a593Smuzhiyun 	state->cec_valid_addrs |= 1 << i;
793*4882a593Smuzhiyun 
794*4882a593Smuzhiyun 	switch (i) {
795*4882a593Smuzhiyun 	case 0:
796*4882a593Smuzhiyun 		/* enable address mask 0 */
797*4882a593Smuzhiyun 		adv7511_cec_write_and_or(sd, 0x4b, 0xef, 0x10);
798*4882a593Smuzhiyun 		/* set address for mask 0 */
799*4882a593Smuzhiyun 		adv7511_cec_write_and_or(sd, 0x4c, 0xf0, addr);
800*4882a593Smuzhiyun 		break;
801*4882a593Smuzhiyun 	case 1:
802*4882a593Smuzhiyun 		/* enable address mask 1 */
803*4882a593Smuzhiyun 		adv7511_cec_write_and_or(sd, 0x4b, 0xdf, 0x20);
804*4882a593Smuzhiyun 		/* set address for mask 1 */
805*4882a593Smuzhiyun 		adv7511_cec_write_and_or(sd, 0x4c, 0x0f, addr << 4);
806*4882a593Smuzhiyun 		break;
807*4882a593Smuzhiyun 	case 2:
808*4882a593Smuzhiyun 		/* enable address mask 2 */
809*4882a593Smuzhiyun 		adv7511_cec_write_and_or(sd, 0x4b, 0xbf, 0x40);
810*4882a593Smuzhiyun 		/* set address for mask 1 */
811*4882a593Smuzhiyun 		adv7511_cec_write_and_or(sd, 0x4d, 0xf0, addr);
812*4882a593Smuzhiyun 		break;
813*4882a593Smuzhiyun 	}
814*4882a593Smuzhiyun 	return 0;
815*4882a593Smuzhiyun }
816*4882a593Smuzhiyun 
adv7511_cec_adap_transmit(struct cec_adapter * adap,u8 attempts,u32 signal_free_time,struct cec_msg * msg)817*4882a593Smuzhiyun static int adv7511_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
818*4882a593Smuzhiyun 				     u32 signal_free_time, struct cec_msg *msg)
819*4882a593Smuzhiyun {
820*4882a593Smuzhiyun 	struct adv7511_state *state = cec_get_drvdata(adap);
821*4882a593Smuzhiyun 	struct v4l2_subdev *sd = &state->sd;
822*4882a593Smuzhiyun 	u8 len = msg->len;
823*4882a593Smuzhiyun 	unsigned int i;
824*4882a593Smuzhiyun 
825*4882a593Smuzhiyun 	v4l2_dbg(1, debug, sd, "%s: len %d\n", __func__, len);
826*4882a593Smuzhiyun 
827*4882a593Smuzhiyun 	if (len > 16) {
828*4882a593Smuzhiyun 		v4l2_err(sd, "%s: len exceeded 16 (%d)\n", __func__, len);
829*4882a593Smuzhiyun 		return -EINVAL;
830*4882a593Smuzhiyun 	}
831*4882a593Smuzhiyun 
832*4882a593Smuzhiyun 	/*
833*4882a593Smuzhiyun 	 * The number of retries is the number of attempts - 1, but retry
834*4882a593Smuzhiyun 	 * at least once. It's not clear if a value of 0 is allowed, so
835*4882a593Smuzhiyun 	 * let's do at least one retry.
836*4882a593Smuzhiyun 	 */
837*4882a593Smuzhiyun 	adv7511_cec_write_and_or(sd, 0x12, ~0x70, max(1, attempts - 1) << 4);
838*4882a593Smuzhiyun 
839*4882a593Smuzhiyun 	/* clear cec tx irq status */
840*4882a593Smuzhiyun 	adv7511_wr(sd, 0x97, 0x38);
841*4882a593Smuzhiyun 
842*4882a593Smuzhiyun 	/* write data */
843*4882a593Smuzhiyun 	for (i = 0; i < len; i++)
844*4882a593Smuzhiyun 		adv7511_cec_write(sd, i, msg->msg[i]);
845*4882a593Smuzhiyun 
846*4882a593Smuzhiyun 	/* set length (data + header) */
847*4882a593Smuzhiyun 	adv7511_cec_write(sd, 0x10, len);
848*4882a593Smuzhiyun 	/* start transmit, enable tx */
849*4882a593Smuzhiyun 	adv7511_cec_write(sd, 0x11, 0x01);
850*4882a593Smuzhiyun 	return 0;
851*4882a593Smuzhiyun }
852*4882a593Smuzhiyun 
adv_cec_tx_raw_status(struct v4l2_subdev * sd,u8 tx_raw_status)853*4882a593Smuzhiyun static void adv_cec_tx_raw_status(struct v4l2_subdev *sd, u8 tx_raw_status)
854*4882a593Smuzhiyun {
855*4882a593Smuzhiyun 	struct adv7511_state *state = get_adv7511_state(sd);
856*4882a593Smuzhiyun 
857*4882a593Smuzhiyun 	if ((adv7511_cec_read(sd, 0x11) & 0x01) == 0) {
858*4882a593Smuzhiyun 		v4l2_dbg(1, debug, sd, "%s: tx raw: tx disabled\n", __func__);
859*4882a593Smuzhiyun 		return;
860*4882a593Smuzhiyun 	}
861*4882a593Smuzhiyun 
862*4882a593Smuzhiyun 	if (tx_raw_status & 0x10) {
863*4882a593Smuzhiyun 		v4l2_dbg(1, debug, sd,
864*4882a593Smuzhiyun 			 "%s: tx raw: arbitration lost\n", __func__);
865*4882a593Smuzhiyun 		cec_transmit_done(state->cec_adap, CEC_TX_STATUS_ARB_LOST,
866*4882a593Smuzhiyun 				  1, 0, 0, 0);
867*4882a593Smuzhiyun 		return;
868*4882a593Smuzhiyun 	}
869*4882a593Smuzhiyun 	if (tx_raw_status & 0x08) {
870*4882a593Smuzhiyun 		u8 status;
871*4882a593Smuzhiyun 		u8 nack_cnt;
872*4882a593Smuzhiyun 		u8 low_drive_cnt;
873*4882a593Smuzhiyun 
874*4882a593Smuzhiyun 		v4l2_dbg(1, debug, sd, "%s: tx raw: retry failed\n", __func__);
875*4882a593Smuzhiyun 		/*
876*4882a593Smuzhiyun 		 * We set this status bit since this hardware performs
877*4882a593Smuzhiyun 		 * retransmissions.
878*4882a593Smuzhiyun 		 */
879*4882a593Smuzhiyun 		status = CEC_TX_STATUS_MAX_RETRIES;
880*4882a593Smuzhiyun 		nack_cnt = adv7511_cec_read(sd, 0x14) & 0xf;
881*4882a593Smuzhiyun 		if (nack_cnt)
882*4882a593Smuzhiyun 			status |= CEC_TX_STATUS_NACK;
883*4882a593Smuzhiyun 		low_drive_cnt = adv7511_cec_read(sd, 0x14) >> 4;
884*4882a593Smuzhiyun 		if (low_drive_cnt)
885*4882a593Smuzhiyun 			status |= CEC_TX_STATUS_LOW_DRIVE;
886*4882a593Smuzhiyun 		cec_transmit_done(state->cec_adap, status,
887*4882a593Smuzhiyun 				  0, nack_cnt, low_drive_cnt, 0);
888*4882a593Smuzhiyun 		return;
889*4882a593Smuzhiyun 	}
890*4882a593Smuzhiyun 	if (tx_raw_status & 0x20) {
891*4882a593Smuzhiyun 		v4l2_dbg(1, debug, sd, "%s: tx raw: ready ok\n", __func__);
892*4882a593Smuzhiyun 		cec_transmit_done(state->cec_adap, CEC_TX_STATUS_OK, 0, 0, 0, 0);
893*4882a593Smuzhiyun 		return;
894*4882a593Smuzhiyun 	}
895*4882a593Smuzhiyun }
896*4882a593Smuzhiyun 
897*4882a593Smuzhiyun static const struct cec_adap_ops adv7511_cec_adap_ops = {
898*4882a593Smuzhiyun 	.adap_enable = adv7511_cec_adap_enable,
899*4882a593Smuzhiyun 	.adap_log_addr = adv7511_cec_adap_log_addr,
900*4882a593Smuzhiyun 	.adap_transmit = adv7511_cec_adap_transmit,
901*4882a593Smuzhiyun };
902*4882a593Smuzhiyun #endif
903*4882a593Smuzhiyun 
904*4882a593Smuzhiyun /* Enable interrupts */
adv7511_set_isr(struct v4l2_subdev * sd,bool enable)905*4882a593Smuzhiyun static void adv7511_set_isr(struct v4l2_subdev *sd, bool enable)
906*4882a593Smuzhiyun {
907*4882a593Smuzhiyun 	struct adv7511_state *state = get_adv7511_state(sd);
908*4882a593Smuzhiyun 	u8 irqs = MASK_ADV7511_HPD_INT | MASK_ADV7511_MSEN_INT;
909*4882a593Smuzhiyun 	u8 irqs_rd;
910*4882a593Smuzhiyun 	int retries = 100;
911*4882a593Smuzhiyun 
912*4882a593Smuzhiyun 	v4l2_dbg(2, debug, sd, "%s: %s\n", __func__, enable ? "enable" : "disable");
913*4882a593Smuzhiyun 
914*4882a593Smuzhiyun 	if (state->enabled_irq == enable)
915*4882a593Smuzhiyun 		return;
916*4882a593Smuzhiyun 	state->enabled_irq = enable;
917*4882a593Smuzhiyun 
918*4882a593Smuzhiyun 	/* The datasheet says that the EDID ready interrupt should be
919*4882a593Smuzhiyun 	   disabled if there is no hotplug. */
920*4882a593Smuzhiyun 	if (!enable)
921*4882a593Smuzhiyun 		irqs = 0;
922*4882a593Smuzhiyun 	else if (adv7511_have_hotplug(sd))
923*4882a593Smuzhiyun 		irqs |= MASK_ADV7511_EDID_RDY_INT;
924*4882a593Smuzhiyun 
925*4882a593Smuzhiyun 	/*
926*4882a593Smuzhiyun 	 * This i2c write can fail (approx. 1 in 1000 writes). But it
927*4882a593Smuzhiyun 	 * is essential that this register is correct, so retry it
928*4882a593Smuzhiyun 	 * multiple times.
929*4882a593Smuzhiyun 	 *
930*4882a593Smuzhiyun 	 * Note that the i2c write does not report an error, but the readback
931*4882a593Smuzhiyun 	 * clearly shows the wrong value.
932*4882a593Smuzhiyun 	 */
933*4882a593Smuzhiyun 	do {
934*4882a593Smuzhiyun 		adv7511_wr(sd, 0x94, irqs);
935*4882a593Smuzhiyun 		irqs_rd = adv7511_rd(sd, 0x94);
936*4882a593Smuzhiyun 	} while (retries-- && irqs_rd != irqs);
937*4882a593Smuzhiyun 
938*4882a593Smuzhiyun 	if (irqs_rd != irqs)
939*4882a593Smuzhiyun 		v4l2_err(sd, "Could not set interrupts: hw failure?\n");
940*4882a593Smuzhiyun 
941*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0x95, 0xc0,
942*4882a593Smuzhiyun 			  (state->cec_enabled_adap && enable) ? 0x39 : 0x00);
943*4882a593Smuzhiyun }
944*4882a593Smuzhiyun 
945*4882a593Smuzhiyun /* Interrupt handler */
adv7511_isr(struct v4l2_subdev * sd,u32 status,bool * handled)946*4882a593Smuzhiyun static int adv7511_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
947*4882a593Smuzhiyun {
948*4882a593Smuzhiyun 	u8 irq_status;
949*4882a593Smuzhiyun 	u8 cec_irq;
950*4882a593Smuzhiyun 
951*4882a593Smuzhiyun 	/* disable interrupts to prevent a race condition */
952*4882a593Smuzhiyun 	adv7511_set_isr(sd, false);
953*4882a593Smuzhiyun 	irq_status = adv7511_rd(sd, 0x96);
954*4882a593Smuzhiyun 	cec_irq = adv7511_rd(sd, 0x97);
955*4882a593Smuzhiyun 	/* clear detected interrupts */
956*4882a593Smuzhiyun 	adv7511_wr(sd, 0x96, irq_status);
957*4882a593Smuzhiyun 	adv7511_wr(sd, 0x97, cec_irq);
958*4882a593Smuzhiyun 
959*4882a593Smuzhiyun 	v4l2_dbg(1, debug, sd, "%s: irq 0x%x, cec-irq 0x%x\n", __func__,
960*4882a593Smuzhiyun 		 irq_status, cec_irq);
961*4882a593Smuzhiyun 
962*4882a593Smuzhiyun 	if (irq_status & (MASK_ADV7511_HPD_INT | MASK_ADV7511_MSEN_INT))
963*4882a593Smuzhiyun 		adv7511_check_monitor_present_status(sd);
964*4882a593Smuzhiyun 	if (irq_status & MASK_ADV7511_EDID_RDY_INT)
965*4882a593Smuzhiyun 		adv7511_check_edid_status(sd);
966*4882a593Smuzhiyun 
967*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_VIDEO_ADV7511_CEC)
968*4882a593Smuzhiyun 	if (cec_irq & 0x38)
969*4882a593Smuzhiyun 		adv_cec_tx_raw_status(sd, cec_irq);
970*4882a593Smuzhiyun 
971*4882a593Smuzhiyun 	if (cec_irq & 1) {
972*4882a593Smuzhiyun 		struct adv7511_state *state = get_adv7511_state(sd);
973*4882a593Smuzhiyun 		struct cec_msg msg;
974*4882a593Smuzhiyun 
975*4882a593Smuzhiyun 		msg.len = adv7511_cec_read(sd, 0x25) & 0x1f;
976*4882a593Smuzhiyun 
977*4882a593Smuzhiyun 		v4l2_dbg(1, debug, sd, "%s: cec msg len %d\n", __func__,
978*4882a593Smuzhiyun 			 msg.len);
979*4882a593Smuzhiyun 
980*4882a593Smuzhiyun 		if (msg.len > 16)
981*4882a593Smuzhiyun 			msg.len = 16;
982*4882a593Smuzhiyun 
983*4882a593Smuzhiyun 		if (msg.len) {
984*4882a593Smuzhiyun 			u8 i;
985*4882a593Smuzhiyun 
986*4882a593Smuzhiyun 			for (i = 0; i < msg.len; i++)
987*4882a593Smuzhiyun 				msg.msg[i] = adv7511_cec_read(sd, i + 0x15);
988*4882a593Smuzhiyun 
989*4882a593Smuzhiyun 			adv7511_cec_write(sd, 0x4a, 0); /* toggle to re-enable rx 1 */
990*4882a593Smuzhiyun 			adv7511_cec_write(sd, 0x4a, 1);
991*4882a593Smuzhiyun 			cec_received_msg(state->cec_adap, &msg);
992*4882a593Smuzhiyun 		}
993*4882a593Smuzhiyun 	}
994*4882a593Smuzhiyun #endif
995*4882a593Smuzhiyun 
996*4882a593Smuzhiyun 	/* enable interrupts */
997*4882a593Smuzhiyun 	adv7511_set_isr(sd, true);
998*4882a593Smuzhiyun 
999*4882a593Smuzhiyun 	if (handled)
1000*4882a593Smuzhiyun 		*handled = true;
1001*4882a593Smuzhiyun 	return 0;
1002*4882a593Smuzhiyun }
1003*4882a593Smuzhiyun 
1004*4882a593Smuzhiyun static const struct v4l2_subdev_core_ops adv7511_core_ops = {
1005*4882a593Smuzhiyun 	.log_status = adv7511_log_status,
1006*4882a593Smuzhiyun #ifdef CONFIG_VIDEO_ADV_DEBUG
1007*4882a593Smuzhiyun 	.g_register = adv7511_g_register,
1008*4882a593Smuzhiyun 	.s_register = adv7511_s_register,
1009*4882a593Smuzhiyun #endif
1010*4882a593Smuzhiyun 	.s_power = adv7511_s_power,
1011*4882a593Smuzhiyun 	.interrupt_service_routine = adv7511_isr,
1012*4882a593Smuzhiyun };
1013*4882a593Smuzhiyun 
1014*4882a593Smuzhiyun /* ------------------------------ VIDEO OPS ------------------------------ */
1015*4882a593Smuzhiyun 
1016*4882a593Smuzhiyun /* Enable/disable adv7511 output */
adv7511_s_stream(struct v4l2_subdev * sd,int enable)1017*4882a593Smuzhiyun static int adv7511_s_stream(struct v4l2_subdev *sd, int enable)
1018*4882a593Smuzhiyun {
1019*4882a593Smuzhiyun 	struct adv7511_state *state = get_adv7511_state(sd);
1020*4882a593Smuzhiyun 
1021*4882a593Smuzhiyun 	v4l2_dbg(1, debug, sd, "%s: %sable\n", __func__, (enable ? "en" : "dis"));
1022*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0xa1, ~0x3c, (enable ? 0 : 0x3c));
1023*4882a593Smuzhiyun 	if (enable) {
1024*4882a593Smuzhiyun 		adv7511_check_monitor_present_status(sd);
1025*4882a593Smuzhiyun 	} else {
1026*4882a593Smuzhiyun 		adv7511_s_power(sd, 0);
1027*4882a593Smuzhiyun 		state->have_monitor = false;
1028*4882a593Smuzhiyun 	}
1029*4882a593Smuzhiyun 	return 0;
1030*4882a593Smuzhiyun }
1031*4882a593Smuzhiyun 
adv7511_s_dv_timings(struct v4l2_subdev * sd,struct v4l2_dv_timings * timings)1032*4882a593Smuzhiyun static int adv7511_s_dv_timings(struct v4l2_subdev *sd,
1033*4882a593Smuzhiyun 			       struct v4l2_dv_timings *timings)
1034*4882a593Smuzhiyun {
1035*4882a593Smuzhiyun 	struct adv7511_state *state = get_adv7511_state(sd);
1036*4882a593Smuzhiyun 	struct v4l2_bt_timings *bt = &timings->bt;
1037*4882a593Smuzhiyun 	u32 fps;
1038*4882a593Smuzhiyun 
1039*4882a593Smuzhiyun 	v4l2_dbg(1, debug, sd, "%s:\n", __func__);
1040*4882a593Smuzhiyun 
1041*4882a593Smuzhiyun 	/* quick sanity check */
1042*4882a593Smuzhiyun 	if (!v4l2_valid_dv_timings(timings, &adv7511_timings_cap, NULL, NULL))
1043*4882a593Smuzhiyun 		return -EINVAL;
1044*4882a593Smuzhiyun 
1045*4882a593Smuzhiyun 	/* Fill the optional fields .standards and .flags in struct v4l2_dv_timings
1046*4882a593Smuzhiyun 	   if the format is one of the CEA or DMT timings. */
1047*4882a593Smuzhiyun 	v4l2_find_dv_timings_cap(timings, &adv7511_timings_cap, 0, NULL, NULL);
1048*4882a593Smuzhiyun 
1049*4882a593Smuzhiyun 	/* save timings */
1050*4882a593Smuzhiyun 	state->dv_timings = *timings;
1051*4882a593Smuzhiyun 
1052*4882a593Smuzhiyun 	/* set h/vsync polarities */
1053*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0x17, 0x9f,
1054*4882a593Smuzhiyun 		((bt->polarities & V4L2_DV_VSYNC_POS_POL) ? 0 : 0x40) |
1055*4882a593Smuzhiyun 		((bt->polarities & V4L2_DV_HSYNC_POS_POL) ? 0 : 0x20));
1056*4882a593Smuzhiyun 
1057*4882a593Smuzhiyun 	fps = (u32)bt->pixelclock / (V4L2_DV_BT_FRAME_WIDTH(bt) * V4L2_DV_BT_FRAME_HEIGHT(bt));
1058*4882a593Smuzhiyun 	switch (fps) {
1059*4882a593Smuzhiyun 	case 24:
1060*4882a593Smuzhiyun 		adv7511_wr_and_or(sd, 0xfb, 0xf9, 1 << 1);
1061*4882a593Smuzhiyun 		break;
1062*4882a593Smuzhiyun 	case 25:
1063*4882a593Smuzhiyun 		adv7511_wr_and_or(sd, 0xfb, 0xf9, 2 << 1);
1064*4882a593Smuzhiyun 		break;
1065*4882a593Smuzhiyun 	case 30:
1066*4882a593Smuzhiyun 		adv7511_wr_and_or(sd, 0xfb, 0xf9, 3 << 1);
1067*4882a593Smuzhiyun 		break;
1068*4882a593Smuzhiyun 	default:
1069*4882a593Smuzhiyun 		adv7511_wr_and_or(sd, 0xfb, 0xf9, 0);
1070*4882a593Smuzhiyun 		break;
1071*4882a593Smuzhiyun 	}
1072*4882a593Smuzhiyun 
1073*4882a593Smuzhiyun 	/* update quantization range based on new dv_timings */
1074*4882a593Smuzhiyun 	adv7511_set_rgb_quantization_mode(sd, state->rgb_quantization_range_ctrl);
1075*4882a593Smuzhiyun 
1076*4882a593Smuzhiyun 	return 0;
1077*4882a593Smuzhiyun }
1078*4882a593Smuzhiyun 
adv7511_g_dv_timings(struct v4l2_subdev * sd,struct v4l2_dv_timings * timings)1079*4882a593Smuzhiyun static int adv7511_g_dv_timings(struct v4l2_subdev *sd,
1080*4882a593Smuzhiyun 				struct v4l2_dv_timings *timings)
1081*4882a593Smuzhiyun {
1082*4882a593Smuzhiyun 	struct adv7511_state *state = get_adv7511_state(sd);
1083*4882a593Smuzhiyun 
1084*4882a593Smuzhiyun 	v4l2_dbg(1, debug, sd, "%s:\n", __func__);
1085*4882a593Smuzhiyun 
1086*4882a593Smuzhiyun 	if (!timings)
1087*4882a593Smuzhiyun 		return -EINVAL;
1088*4882a593Smuzhiyun 
1089*4882a593Smuzhiyun 	*timings = state->dv_timings;
1090*4882a593Smuzhiyun 
1091*4882a593Smuzhiyun 	return 0;
1092*4882a593Smuzhiyun }
1093*4882a593Smuzhiyun 
adv7511_enum_dv_timings(struct v4l2_subdev * sd,struct v4l2_enum_dv_timings * timings)1094*4882a593Smuzhiyun static int adv7511_enum_dv_timings(struct v4l2_subdev *sd,
1095*4882a593Smuzhiyun 				   struct v4l2_enum_dv_timings *timings)
1096*4882a593Smuzhiyun {
1097*4882a593Smuzhiyun 	if (timings->pad != 0)
1098*4882a593Smuzhiyun 		return -EINVAL;
1099*4882a593Smuzhiyun 
1100*4882a593Smuzhiyun 	return v4l2_enum_dv_timings_cap(timings, &adv7511_timings_cap, NULL, NULL);
1101*4882a593Smuzhiyun }
1102*4882a593Smuzhiyun 
adv7511_dv_timings_cap(struct v4l2_subdev * sd,struct v4l2_dv_timings_cap * cap)1103*4882a593Smuzhiyun static int adv7511_dv_timings_cap(struct v4l2_subdev *sd,
1104*4882a593Smuzhiyun 				  struct v4l2_dv_timings_cap *cap)
1105*4882a593Smuzhiyun {
1106*4882a593Smuzhiyun 	if (cap->pad != 0)
1107*4882a593Smuzhiyun 		return -EINVAL;
1108*4882a593Smuzhiyun 
1109*4882a593Smuzhiyun 	*cap = adv7511_timings_cap;
1110*4882a593Smuzhiyun 	return 0;
1111*4882a593Smuzhiyun }
1112*4882a593Smuzhiyun 
1113*4882a593Smuzhiyun static const struct v4l2_subdev_video_ops adv7511_video_ops = {
1114*4882a593Smuzhiyun 	.s_stream = adv7511_s_stream,
1115*4882a593Smuzhiyun 	.s_dv_timings = adv7511_s_dv_timings,
1116*4882a593Smuzhiyun 	.g_dv_timings = adv7511_g_dv_timings,
1117*4882a593Smuzhiyun };
1118*4882a593Smuzhiyun 
1119*4882a593Smuzhiyun /* ------------------------------ AUDIO OPS ------------------------------ */
adv7511_s_audio_stream(struct v4l2_subdev * sd,int enable)1120*4882a593Smuzhiyun static int adv7511_s_audio_stream(struct v4l2_subdev *sd, int enable)
1121*4882a593Smuzhiyun {
1122*4882a593Smuzhiyun 	v4l2_dbg(1, debug, sd, "%s: %sable\n", __func__, (enable ? "en" : "dis"));
1123*4882a593Smuzhiyun 
1124*4882a593Smuzhiyun 	if (enable)
1125*4882a593Smuzhiyun 		adv7511_wr_and_or(sd, 0x4b, 0x3f, 0x80);
1126*4882a593Smuzhiyun 	else
1127*4882a593Smuzhiyun 		adv7511_wr_and_or(sd, 0x4b, 0x3f, 0x40);
1128*4882a593Smuzhiyun 
1129*4882a593Smuzhiyun 	return 0;
1130*4882a593Smuzhiyun }
1131*4882a593Smuzhiyun 
adv7511_s_clock_freq(struct v4l2_subdev * sd,u32 freq)1132*4882a593Smuzhiyun static int adv7511_s_clock_freq(struct v4l2_subdev *sd, u32 freq)
1133*4882a593Smuzhiyun {
1134*4882a593Smuzhiyun 	u32 N;
1135*4882a593Smuzhiyun 
1136*4882a593Smuzhiyun 	switch (freq) {
1137*4882a593Smuzhiyun 	case 32000:  N = 4096;  break;
1138*4882a593Smuzhiyun 	case 44100:  N = 6272;  break;
1139*4882a593Smuzhiyun 	case 48000:  N = 6144;  break;
1140*4882a593Smuzhiyun 	case 88200:  N = 12544; break;
1141*4882a593Smuzhiyun 	case 96000:  N = 12288; break;
1142*4882a593Smuzhiyun 	case 176400: N = 25088; break;
1143*4882a593Smuzhiyun 	case 192000: N = 24576; break;
1144*4882a593Smuzhiyun 	default:
1145*4882a593Smuzhiyun 		return -EINVAL;
1146*4882a593Smuzhiyun 	}
1147*4882a593Smuzhiyun 
1148*4882a593Smuzhiyun 	/* Set N (used with CTS to regenerate the audio clock) */
1149*4882a593Smuzhiyun 	adv7511_wr(sd, 0x01, (N >> 16) & 0xf);
1150*4882a593Smuzhiyun 	adv7511_wr(sd, 0x02, (N >> 8) & 0xff);
1151*4882a593Smuzhiyun 	adv7511_wr(sd, 0x03, N & 0xff);
1152*4882a593Smuzhiyun 
1153*4882a593Smuzhiyun 	return 0;
1154*4882a593Smuzhiyun }
1155*4882a593Smuzhiyun 
adv7511_s_i2s_clock_freq(struct v4l2_subdev * sd,u32 freq)1156*4882a593Smuzhiyun static int adv7511_s_i2s_clock_freq(struct v4l2_subdev *sd, u32 freq)
1157*4882a593Smuzhiyun {
1158*4882a593Smuzhiyun 	u32 i2s_sf;
1159*4882a593Smuzhiyun 
1160*4882a593Smuzhiyun 	switch (freq) {
1161*4882a593Smuzhiyun 	case 32000:  i2s_sf = 0x30; break;
1162*4882a593Smuzhiyun 	case 44100:  i2s_sf = 0x00; break;
1163*4882a593Smuzhiyun 	case 48000:  i2s_sf = 0x20; break;
1164*4882a593Smuzhiyun 	case 88200:  i2s_sf = 0x80; break;
1165*4882a593Smuzhiyun 	case 96000:  i2s_sf = 0xa0; break;
1166*4882a593Smuzhiyun 	case 176400: i2s_sf = 0xc0; break;
1167*4882a593Smuzhiyun 	case 192000: i2s_sf = 0xe0; break;
1168*4882a593Smuzhiyun 	default:
1169*4882a593Smuzhiyun 		return -EINVAL;
1170*4882a593Smuzhiyun 	}
1171*4882a593Smuzhiyun 
1172*4882a593Smuzhiyun 	/* Set sampling frequency for I2S audio to 48 kHz */
1173*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0x15, 0xf, i2s_sf);
1174*4882a593Smuzhiyun 
1175*4882a593Smuzhiyun 	return 0;
1176*4882a593Smuzhiyun }
1177*4882a593Smuzhiyun 
adv7511_s_routing(struct v4l2_subdev * sd,u32 input,u32 output,u32 config)1178*4882a593Smuzhiyun static int adv7511_s_routing(struct v4l2_subdev *sd, u32 input, u32 output, u32 config)
1179*4882a593Smuzhiyun {
1180*4882a593Smuzhiyun 	/* Only 2 channels in use for application */
1181*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0x73, 0xf8, 0x1);
1182*4882a593Smuzhiyun 	/* Speaker mapping */
1183*4882a593Smuzhiyun 	adv7511_wr(sd, 0x76, 0x00);
1184*4882a593Smuzhiyun 
1185*4882a593Smuzhiyun 	/* 16 bit audio word length */
1186*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0x14, 0xf0, 0x02);
1187*4882a593Smuzhiyun 
1188*4882a593Smuzhiyun 	return 0;
1189*4882a593Smuzhiyun }
1190*4882a593Smuzhiyun 
1191*4882a593Smuzhiyun static const struct v4l2_subdev_audio_ops adv7511_audio_ops = {
1192*4882a593Smuzhiyun 	.s_stream = adv7511_s_audio_stream,
1193*4882a593Smuzhiyun 	.s_clock_freq = adv7511_s_clock_freq,
1194*4882a593Smuzhiyun 	.s_i2s_clock_freq = adv7511_s_i2s_clock_freq,
1195*4882a593Smuzhiyun 	.s_routing = adv7511_s_routing,
1196*4882a593Smuzhiyun };
1197*4882a593Smuzhiyun 
1198*4882a593Smuzhiyun /* ---------------------------- PAD OPS ------------------------------------- */
1199*4882a593Smuzhiyun 
adv7511_get_edid(struct v4l2_subdev * sd,struct v4l2_edid * edid)1200*4882a593Smuzhiyun static int adv7511_get_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
1201*4882a593Smuzhiyun {
1202*4882a593Smuzhiyun 	struct adv7511_state *state = get_adv7511_state(sd);
1203*4882a593Smuzhiyun 
1204*4882a593Smuzhiyun 	memset(edid->reserved, 0, sizeof(edid->reserved));
1205*4882a593Smuzhiyun 
1206*4882a593Smuzhiyun 	if (edid->pad != 0)
1207*4882a593Smuzhiyun 		return -EINVAL;
1208*4882a593Smuzhiyun 
1209*4882a593Smuzhiyun 	if (edid->start_block == 0 && edid->blocks == 0) {
1210*4882a593Smuzhiyun 		edid->blocks = state->edid.segments * 2;
1211*4882a593Smuzhiyun 		return 0;
1212*4882a593Smuzhiyun 	}
1213*4882a593Smuzhiyun 
1214*4882a593Smuzhiyun 	if (state->edid.segments == 0)
1215*4882a593Smuzhiyun 		return -ENODATA;
1216*4882a593Smuzhiyun 
1217*4882a593Smuzhiyun 	if (edid->start_block >= state->edid.segments * 2)
1218*4882a593Smuzhiyun 		return -EINVAL;
1219*4882a593Smuzhiyun 
1220*4882a593Smuzhiyun 	if (edid->start_block + edid->blocks > state->edid.segments * 2)
1221*4882a593Smuzhiyun 		edid->blocks = state->edid.segments * 2 - edid->start_block;
1222*4882a593Smuzhiyun 
1223*4882a593Smuzhiyun 	memcpy(edid->edid, &state->edid.data[edid->start_block * 128],
1224*4882a593Smuzhiyun 			128 * edid->blocks);
1225*4882a593Smuzhiyun 
1226*4882a593Smuzhiyun 	return 0;
1227*4882a593Smuzhiyun }
1228*4882a593Smuzhiyun 
adv7511_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code)1229*4882a593Smuzhiyun static int adv7511_enum_mbus_code(struct v4l2_subdev *sd,
1230*4882a593Smuzhiyun 				  struct v4l2_subdev_pad_config *cfg,
1231*4882a593Smuzhiyun 				  struct v4l2_subdev_mbus_code_enum *code)
1232*4882a593Smuzhiyun {
1233*4882a593Smuzhiyun 	if (code->pad != 0)
1234*4882a593Smuzhiyun 		return -EINVAL;
1235*4882a593Smuzhiyun 
1236*4882a593Smuzhiyun 	switch (code->index) {
1237*4882a593Smuzhiyun 	case 0:
1238*4882a593Smuzhiyun 		code->code = MEDIA_BUS_FMT_RGB888_1X24;
1239*4882a593Smuzhiyun 		break;
1240*4882a593Smuzhiyun 	case 1:
1241*4882a593Smuzhiyun 		code->code = MEDIA_BUS_FMT_YUYV8_1X16;
1242*4882a593Smuzhiyun 		break;
1243*4882a593Smuzhiyun 	case 2:
1244*4882a593Smuzhiyun 		code->code = MEDIA_BUS_FMT_UYVY8_1X16;
1245*4882a593Smuzhiyun 		break;
1246*4882a593Smuzhiyun 	default:
1247*4882a593Smuzhiyun 		return -EINVAL;
1248*4882a593Smuzhiyun 	}
1249*4882a593Smuzhiyun 	return 0;
1250*4882a593Smuzhiyun }
1251*4882a593Smuzhiyun 
adv7511_fill_format(struct adv7511_state * state,struct v4l2_mbus_framefmt * format)1252*4882a593Smuzhiyun static void adv7511_fill_format(struct adv7511_state *state,
1253*4882a593Smuzhiyun 				struct v4l2_mbus_framefmt *format)
1254*4882a593Smuzhiyun {
1255*4882a593Smuzhiyun 	format->width = state->dv_timings.bt.width;
1256*4882a593Smuzhiyun 	format->height = state->dv_timings.bt.height;
1257*4882a593Smuzhiyun 	format->field = V4L2_FIELD_NONE;
1258*4882a593Smuzhiyun }
1259*4882a593Smuzhiyun 
adv7511_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * format)1260*4882a593Smuzhiyun static int adv7511_get_fmt(struct v4l2_subdev *sd,
1261*4882a593Smuzhiyun 			   struct v4l2_subdev_pad_config *cfg,
1262*4882a593Smuzhiyun 			   struct v4l2_subdev_format *format)
1263*4882a593Smuzhiyun {
1264*4882a593Smuzhiyun 	struct adv7511_state *state = get_adv7511_state(sd);
1265*4882a593Smuzhiyun 
1266*4882a593Smuzhiyun 	if (format->pad != 0)
1267*4882a593Smuzhiyun 		return -EINVAL;
1268*4882a593Smuzhiyun 
1269*4882a593Smuzhiyun 	memset(&format->format, 0, sizeof(format->format));
1270*4882a593Smuzhiyun 	adv7511_fill_format(state, &format->format);
1271*4882a593Smuzhiyun 
1272*4882a593Smuzhiyun 	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1273*4882a593Smuzhiyun 		struct v4l2_mbus_framefmt *fmt;
1274*4882a593Smuzhiyun 
1275*4882a593Smuzhiyun 		fmt = v4l2_subdev_get_try_format(sd, cfg, format->pad);
1276*4882a593Smuzhiyun 		format->format.code = fmt->code;
1277*4882a593Smuzhiyun 		format->format.colorspace = fmt->colorspace;
1278*4882a593Smuzhiyun 		format->format.ycbcr_enc = fmt->ycbcr_enc;
1279*4882a593Smuzhiyun 		format->format.quantization = fmt->quantization;
1280*4882a593Smuzhiyun 		format->format.xfer_func = fmt->xfer_func;
1281*4882a593Smuzhiyun 	} else {
1282*4882a593Smuzhiyun 		format->format.code = state->fmt_code;
1283*4882a593Smuzhiyun 		format->format.colorspace = state->colorspace;
1284*4882a593Smuzhiyun 		format->format.ycbcr_enc = state->ycbcr_enc;
1285*4882a593Smuzhiyun 		format->format.quantization = state->quantization;
1286*4882a593Smuzhiyun 		format->format.xfer_func = state->xfer_func;
1287*4882a593Smuzhiyun 	}
1288*4882a593Smuzhiyun 
1289*4882a593Smuzhiyun 	return 0;
1290*4882a593Smuzhiyun }
1291*4882a593Smuzhiyun 
adv7511_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * format)1292*4882a593Smuzhiyun static int adv7511_set_fmt(struct v4l2_subdev *sd,
1293*4882a593Smuzhiyun 			   struct v4l2_subdev_pad_config *cfg,
1294*4882a593Smuzhiyun 			   struct v4l2_subdev_format *format)
1295*4882a593Smuzhiyun {
1296*4882a593Smuzhiyun 	struct adv7511_state *state = get_adv7511_state(sd);
1297*4882a593Smuzhiyun 	/*
1298*4882a593Smuzhiyun 	 * Bitfield namings come the CEA-861-F standard, table 8 "Auxiliary
1299*4882a593Smuzhiyun 	 * Video Information (AVI) InfoFrame Format"
1300*4882a593Smuzhiyun 	 *
1301*4882a593Smuzhiyun 	 * c = Colorimetry
1302*4882a593Smuzhiyun 	 * ec = Extended Colorimetry
1303*4882a593Smuzhiyun 	 * y = RGB or YCbCr
1304*4882a593Smuzhiyun 	 * q = RGB Quantization Range
1305*4882a593Smuzhiyun 	 * yq = YCC Quantization Range
1306*4882a593Smuzhiyun 	 */
1307*4882a593Smuzhiyun 	u8 c = HDMI_COLORIMETRY_NONE;
1308*4882a593Smuzhiyun 	u8 ec = HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
1309*4882a593Smuzhiyun 	u8 y = HDMI_COLORSPACE_RGB;
1310*4882a593Smuzhiyun 	u8 q = HDMI_QUANTIZATION_RANGE_DEFAULT;
1311*4882a593Smuzhiyun 	u8 yq = HDMI_YCC_QUANTIZATION_RANGE_LIMITED;
1312*4882a593Smuzhiyun 	u8 itc = state->content_type != V4L2_DV_IT_CONTENT_TYPE_NO_ITC;
1313*4882a593Smuzhiyun 	u8 cn = itc ? state->content_type : V4L2_DV_IT_CONTENT_TYPE_GRAPHICS;
1314*4882a593Smuzhiyun 
1315*4882a593Smuzhiyun 	if (format->pad != 0)
1316*4882a593Smuzhiyun 		return -EINVAL;
1317*4882a593Smuzhiyun 	switch (format->format.code) {
1318*4882a593Smuzhiyun 	case MEDIA_BUS_FMT_UYVY8_1X16:
1319*4882a593Smuzhiyun 	case MEDIA_BUS_FMT_YUYV8_1X16:
1320*4882a593Smuzhiyun 	case MEDIA_BUS_FMT_RGB888_1X24:
1321*4882a593Smuzhiyun 		break;
1322*4882a593Smuzhiyun 	default:
1323*4882a593Smuzhiyun 		return -EINVAL;
1324*4882a593Smuzhiyun 	}
1325*4882a593Smuzhiyun 
1326*4882a593Smuzhiyun 	adv7511_fill_format(state, &format->format);
1327*4882a593Smuzhiyun 	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1328*4882a593Smuzhiyun 		struct v4l2_mbus_framefmt *fmt;
1329*4882a593Smuzhiyun 
1330*4882a593Smuzhiyun 		fmt = v4l2_subdev_get_try_format(sd, cfg, format->pad);
1331*4882a593Smuzhiyun 		fmt->code = format->format.code;
1332*4882a593Smuzhiyun 		fmt->colorspace = format->format.colorspace;
1333*4882a593Smuzhiyun 		fmt->ycbcr_enc = format->format.ycbcr_enc;
1334*4882a593Smuzhiyun 		fmt->quantization = format->format.quantization;
1335*4882a593Smuzhiyun 		fmt->xfer_func = format->format.xfer_func;
1336*4882a593Smuzhiyun 		return 0;
1337*4882a593Smuzhiyun 	}
1338*4882a593Smuzhiyun 
1339*4882a593Smuzhiyun 	switch (format->format.code) {
1340*4882a593Smuzhiyun 	case MEDIA_BUS_FMT_UYVY8_1X16:
1341*4882a593Smuzhiyun 		adv7511_wr_and_or(sd, 0x15, 0xf0, 0x01);
1342*4882a593Smuzhiyun 		adv7511_wr_and_or(sd, 0x16, 0x03, 0xb8);
1343*4882a593Smuzhiyun 		y = HDMI_COLORSPACE_YUV422;
1344*4882a593Smuzhiyun 		break;
1345*4882a593Smuzhiyun 	case MEDIA_BUS_FMT_YUYV8_1X16:
1346*4882a593Smuzhiyun 		adv7511_wr_and_or(sd, 0x15, 0xf0, 0x01);
1347*4882a593Smuzhiyun 		adv7511_wr_and_or(sd, 0x16, 0x03, 0xbc);
1348*4882a593Smuzhiyun 		y = HDMI_COLORSPACE_YUV422;
1349*4882a593Smuzhiyun 		break;
1350*4882a593Smuzhiyun 	case MEDIA_BUS_FMT_RGB888_1X24:
1351*4882a593Smuzhiyun 	default:
1352*4882a593Smuzhiyun 		adv7511_wr_and_or(sd, 0x15, 0xf0, 0x00);
1353*4882a593Smuzhiyun 		adv7511_wr_and_or(sd, 0x16, 0x03, 0x00);
1354*4882a593Smuzhiyun 		break;
1355*4882a593Smuzhiyun 	}
1356*4882a593Smuzhiyun 	state->fmt_code = format->format.code;
1357*4882a593Smuzhiyun 	state->colorspace = format->format.colorspace;
1358*4882a593Smuzhiyun 	state->ycbcr_enc = format->format.ycbcr_enc;
1359*4882a593Smuzhiyun 	state->quantization = format->format.quantization;
1360*4882a593Smuzhiyun 	state->xfer_func = format->format.xfer_func;
1361*4882a593Smuzhiyun 
1362*4882a593Smuzhiyun 	switch (format->format.colorspace) {
1363*4882a593Smuzhiyun 	case V4L2_COLORSPACE_OPRGB:
1364*4882a593Smuzhiyun 		c = HDMI_COLORIMETRY_EXTENDED;
1365*4882a593Smuzhiyun 		ec = y ? HDMI_EXTENDED_COLORIMETRY_OPYCC_601 :
1366*4882a593Smuzhiyun 			 HDMI_EXTENDED_COLORIMETRY_OPRGB;
1367*4882a593Smuzhiyun 		break;
1368*4882a593Smuzhiyun 	case V4L2_COLORSPACE_SMPTE170M:
1369*4882a593Smuzhiyun 		c = y ? HDMI_COLORIMETRY_ITU_601 : HDMI_COLORIMETRY_NONE;
1370*4882a593Smuzhiyun 		if (y && format->format.ycbcr_enc == V4L2_YCBCR_ENC_XV601) {
1371*4882a593Smuzhiyun 			c = HDMI_COLORIMETRY_EXTENDED;
1372*4882a593Smuzhiyun 			ec = HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
1373*4882a593Smuzhiyun 		}
1374*4882a593Smuzhiyun 		break;
1375*4882a593Smuzhiyun 	case V4L2_COLORSPACE_REC709:
1376*4882a593Smuzhiyun 		c = y ? HDMI_COLORIMETRY_ITU_709 : HDMI_COLORIMETRY_NONE;
1377*4882a593Smuzhiyun 		if (y && format->format.ycbcr_enc == V4L2_YCBCR_ENC_XV709) {
1378*4882a593Smuzhiyun 			c = HDMI_COLORIMETRY_EXTENDED;
1379*4882a593Smuzhiyun 			ec = HDMI_EXTENDED_COLORIMETRY_XV_YCC_709;
1380*4882a593Smuzhiyun 		}
1381*4882a593Smuzhiyun 		break;
1382*4882a593Smuzhiyun 	case V4L2_COLORSPACE_SRGB:
1383*4882a593Smuzhiyun 		c = y ? HDMI_COLORIMETRY_EXTENDED : HDMI_COLORIMETRY_NONE;
1384*4882a593Smuzhiyun 		ec = y ? HDMI_EXTENDED_COLORIMETRY_S_YCC_601 :
1385*4882a593Smuzhiyun 			 HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
1386*4882a593Smuzhiyun 		break;
1387*4882a593Smuzhiyun 	case V4L2_COLORSPACE_BT2020:
1388*4882a593Smuzhiyun 		c = HDMI_COLORIMETRY_EXTENDED;
1389*4882a593Smuzhiyun 		if (y && format->format.ycbcr_enc == V4L2_YCBCR_ENC_BT2020_CONST_LUM)
1390*4882a593Smuzhiyun 			ec = 5; /* Not yet available in hdmi.h */
1391*4882a593Smuzhiyun 		else
1392*4882a593Smuzhiyun 			ec = 6; /* Not yet available in hdmi.h */
1393*4882a593Smuzhiyun 		break;
1394*4882a593Smuzhiyun 	default:
1395*4882a593Smuzhiyun 		break;
1396*4882a593Smuzhiyun 	}
1397*4882a593Smuzhiyun 
1398*4882a593Smuzhiyun 	/*
1399*4882a593Smuzhiyun 	 * CEA-861-F says that for RGB formats the YCC range must match the
1400*4882a593Smuzhiyun 	 * RGB range, although sources should ignore the YCC range.
1401*4882a593Smuzhiyun 	 *
1402*4882a593Smuzhiyun 	 * The RGB quantization range shouldn't be non-zero if the EDID doesn't
1403*4882a593Smuzhiyun 	 * have the Q bit set in the Video Capabilities Data Block, however this
1404*4882a593Smuzhiyun 	 * isn't checked at the moment. The assumption is that the application
1405*4882a593Smuzhiyun 	 * knows the EDID and can detect this.
1406*4882a593Smuzhiyun 	 *
1407*4882a593Smuzhiyun 	 * The same is true for the YCC quantization range: non-standard YCC
1408*4882a593Smuzhiyun 	 * quantization ranges should only be sent if the EDID has the YQ bit
1409*4882a593Smuzhiyun 	 * set in the Video Capabilities Data Block.
1410*4882a593Smuzhiyun 	 */
1411*4882a593Smuzhiyun 	switch (format->format.quantization) {
1412*4882a593Smuzhiyun 	case V4L2_QUANTIZATION_FULL_RANGE:
1413*4882a593Smuzhiyun 		q = y ? HDMI_QUANTIZATION_RANGE_DEFAULT :
1414*4882a593Smuzhiyun 			HDMI_QUANTIZATION_RANGE_FULL;
1415*4882a593Smuzhiyun 		yq = q ? q - 1 : HDMI_YCC_QUANTIZATION_RANGE_FULL;
1416*4882a593Smuzhiyun 		break;
1417*4882a593Smuzhiyun 	case V4L2_QUANTIZATION_LIM_RANGE:
1418*4882a593Smuzhiyun 		q = y ? HDMI_QUANTIZATION_RANGE_DEFAULT :
1419*4882a593Smuzhiyun 			HDMI_QUANTIZATION_RANGE_LIMITED;
1420*4882a593Smuzhiyun 		yq = q ? q - 1 : HDMI_YCC_QUANTIZATION_RANGE_LIMITED;
1421*4882a593Smuzhiyun 		break;
1422*4882a593Smuzhiyun 	}
1423*4882a593Smuzhiyun 
1424*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0x4a, 0xbf, 0);
1425*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0x55, 0x9f, y << 5);
1426*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0x56, 0x3f, c << 6);
1427*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0x57, 0x83, (ec << 4) | (q << 2) | (itc << 7));
1428*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0x59, 0x0f, (yq << 6) | (cn << 4));
1429*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0x4a, 0xff, 1);
1430*4882a593Smuzhiyun 	adv7511_set_rgb_quantization_mode(sd, state->rgb_quantization_range_ctrl);
1431*4882a593Smuzhiyun 
1432*4882a593Smuzhiyun 	return 0;
1433*4882a593Smuzhiyun }
1434*4882a593Smuzhiyun 
1435*4882a593Smuzhiyun static const struct v4l2_subdev_pad_ops adv7511_pad_ops = {
1436*4882a593Smuzhiyun 	.get_edid = adv7511_get_edid,
1437*4882a593Smuzhiyun 	.enum_mbus_code = adv7511_enum_mbus_code,
1438*4882a593Smuzhiyun 	.get_fmt = adv7511_get_fmt,
1439*4882a593Smuzhiyun 	.set_fmt = adv7511_set_fmt,
1440*4882a593Smuzhiyun 	.enum_dv_timings = adv7511_enum_dv_timings,
1441*4882a593Smuzhiyun 	.dv_timings_cap = adv7511_dv_timings_cap,
1442*4882a593Smuzhiyun };
1443*4882a593Smuzhiyun 
1444*4882a593Smuzhiyun /* --------------------- SUBDEV OPS --------------------------------------- */
1445*4882a593Smuzhiyun 
1446*4882a593Smuzhiyun static const struct v4l2_subdev_ops adv7511_ops = {
1447*4882a593Smuzhiyun 	.core  = &adv7511_core_ops,
1448*4882a593Smuzhiyun 	.pad  = &adv7511_pad_ops,
1449*4882a593Smuzhiyun 	.video = &adv7511_video_ops,
1450*4882a593Smuzhiyun 	.audio = &adv7511_audio_ops,
1451*4882a593Smuzhiyun };
1452*4882a593Smuzhiyun 
1453*4882a593Smuzhiyun /* ----------------------------------------------------------------------- */
adv7511_dbg_dump_edid(int lvl,int debug,struct v4l2_subdev * sd,int segment,u8 * buf)1454*4882a593Smuzhiyun static void adv7511_dbg_dump_edid(int lvl, int debug, struct v4l2_subdev *sd, int segment, u8 *buf)
1455*4882a593Smuzhiyun {
1456*4882a593Smuzhiyun 	if (debug >= lvl) {
1457*4882a593Smuzhiyun 		int i, j;
1458*4882a593Smuzhiyun 		v4l2_dbg(lvl, debug, sd, "edid segment %d\n", segment);
1459*4882a593Smuzhiyun 		for (i = 0; i < 256; i += 16) {
1460*4882a593Smuzhiyun 			u8 b[128];
1461*4882a593Smuzhiyun 			u8 *bp = b;
1462*4882a593Smuzhiyun 			if (i == 128)
1463*4882a593Smuzhiyun 				v4l2_dbg(lvl, debug, sd, "\n");
1464*4882a593Smuzhiyun 			for (j = i; j < i + 16; j++) {
1465*4882a593Smuzhiyun 				sprintf(bp, "0x%02x, ", buf[j]);
1466*4882a593Smuzhiyun 				bp += 6;
1467*4882a593Smuzhiyun 			}
1468*4882a593Smuzhiyun 			bp[0] = '\0';
1469*4882a593Smuzhiyun 			v4l2_dbg(lvl, debug, sd, "%s\n", b);
1470*4882a593Smuzhiyun 		}
1471*4882a593Smuzhiyun 	}
1472*4882a593Smuzhiyun }
1473*4882a593Smuzhiyun 
adv7511_notify_no_edid(struct v4l2_subdev * sd)1474*4882a593Smuzhiyun static void adv7511_notify_no_edid(struct v4l2_subdev *sd)
1475*4882a593Smuzhiyun {
1476*4882a593Smuzhiyun 	struct adv7511_state *state = get_adv7511_state(sd);
1477*4882a593Smuzhiyun 	struct adv7511_edid_detect ed;
1478*4882a593Smuzhiyun 
1479*4882a593Smuzhiyun 	/* We failed to read the EDID, so send an event for this. */
1480*4882a593Smuzhiyun 	ed.present = false;
1481*4882a593Smuzhiyun 	ed.segment = adv7511_rd(sd, 0xc4);
1482*4882a593Smuzhiyun 	ed.phys_addr = CEC_PHYS_ADDR_INVALID;
1483*4882a593Smuzhiyun 	cec_s_phys_addr(state->cec_adap, ed.phys_addr, false);
1484*4882a593Smuzhiyun 	v4l2_subdev_notify(sd, ADV7511_EDID_DETECT, (void *)&ed);
1485*4882a593Smuzhiyun 	v4l2_ctrl_s_ctrl(state->have_edid0_ctrl, 0x0);
1486*4882a593Smuzhiyun }
1487*4882a593Smuzhiyun 
adv7511_edid_handler(struct work_struct * work)1488*4882a593Smuzhiyun static void adv7511_edid_handler(struct work_struct *work)
1489*4882a593Smuzhiyun {
1490*4882a593Smuzhiyun 	struct delayed_work *dwork = to_delayed_work(work);
1491*4882a593Smuzhiyun 	struct adv7511_state *state = container_of(dwork, struct adv7511_state, edid_handler);
1492*4882a593Smuzhiyun 	struct v4l2_subdev *sd = &state->sd;
1493*4882a593Smuzhiyun 
1494*4882a593Smuzhiyun 	v4l2_dbg(1, debug, sd, "%s:\n", __func__);
1495*4882a593Smuzhiyun 
1496*4882a593Smuzhiyun 	if (adv7511_check_edid_status(sd)) {
1497*4882a593Smuzhiyun 		/* Return if we received the EDID. */
1498*4882a593Smuzhiyun 		return;
1499*4882a593Smuzhiyun 	}
1500*4882a593Smuzhiyun 
1501*4882a593Smuzhiyun 	if (adv7511_have_hotplug(sd)) {
1502*4882a593Smuzhiyun 		/* We must retry reading the EDID several times, it is possible
1503*4882a593Smuzhiyun 		 * that initially the EDID couldn't be read due to i2c errors
1504*4882a593Smuzhiyun 		 * (DVI connectors are particularly prone to this problem). */
1505*4882a593Smuzhiyun 		if (state->edid.read_retries) {
1506*4882a593Smuzhiyun 			state->edid.read_retries--;
1507*4882a593Smuzhiyun 			v4l2_dbg(1, debug, sd, "%s: edid read failed\n", __func__);
1508*4882a593Smuzhiyun 			state->have_monitor = false;
1509*4882a593Smuzhiyun 			adv7511_s_power(sd, false);
1510*4882a593Smuzhiyun 			adv7511_s_power(sd, true);
1511*4882a593Smuzhiyun 			queue_delayed_work(state->work_queue, &state->edid_handler, EDID_DELAY);
1512*4882a593Smuzhiyun 			return;
1513*4882a593Smuzhiyun 		}
1514*4882a593Smuzhiyun 	}
1515*4882a593Smuzhiyun 
1516*4882a593Smuzhiyun 	/* We failed to read the EDID, so send an event for this. */
1517*4882a593Smuzhiyun 	adv7511_notify_no_edid(sd);
1518*4882a593Smuzhiyun 	v4l2_dbg(1, debug, sd, "%s: no edid found\n", __func__);
1519*4882a593Smuzhiyun }
1520*4882a593Smuzhiyun 
adv7511_audio_setup(struct v4l2_subdev * sd)1521*4882a593Smuzhiyun static void adv7511_audio_setup(struct v4l2_subdev *sd)
1522*4882a593Smuzhiyun {
1523*4882a593Smuzhiyun 	v4l2_dbg(1, debug, sd, "%s\n", __func__);
1524*4882a593Smuzhiyun 
1525*4882a593Smuzhiyun 	adv7511_s_i2s_clock_freq(sd, 48000);
1526*4882a593Smuzhiyun 	adv7511_s_clock_freq(sd, 48000);
1527*4882a593Smuzhiyun 	adv7511_s_routing(sd, 0, 0, 0);
1528*4882a593Smuzhiyun }
1529*4882a593Smuzhiyun 
1530*4882a593Smuzhiyun /* Configure hdmi transmitter. */
adv7511_setup(struct v4l2_subdev * sd)1531*4882a593Smuzhiyun static void adv7511_setup(struct v4l2_subdev *sd)
1532*4882a593Smuzhiyun {
1533*4882a593Smuzhiyun 	struct adv7511_state *state = get_adv7511_state(sd);
1534*4882a593Smuzhiyun 	v4l2_dbg(1, debug, sd, "%s\n", __func__);
1535*4882a593Smuzhiyun 
1536*4882a593Smuzhiyun 	/* Input format: RGB 4:4:4 */
1537*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0x15, 0xf0, 0x0);
1538*4882a593Smuzhiyun 	/* Output format: RGB 4:4:4 */
1539*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0x16, 0x7f, 0x0);
1540*4882a593Smuzhiyun 	/* 1st order interpolation 4:2:2 -> 4:4:4 up conversion, Aspect ratio: 16:9 */
1541*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0x17, 0xf9, 0x06);
1542*4882a593Smuzhiyun 	/* Disable pixel repetition */
1543*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0x3b, 0x9f, 0x0);
1544*4882a593Smuzhiyun 	/* Disable CSC */
1545*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0x18, 0x7f, 0x0);
1546*4882a593Smuzhiyun 	/* Output format: RGB 4:4:4, Active Format Information is valid,
1547*4882a593Smuzhiyun 	 * underscanned */
1548*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0x55, 0x9c, 0x12);
1549*4882a593Smuzhiyun 	/* AVI Info frame packet enable, Audio Info frame disable */
1550*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0x44, 0xe7, 0x10);
1551*4882a593Smuzhiyun 	/* Colorimetry, Active format aspect ratio: same as picure. */
1552*4882a593Smuzhiyun 	adv7511_wr(sd, 0x56, 0xa8);
1553*4882a593Smuzhiyun 	/* No encryption */
1554*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0xaf, 0xed, 0x0);
1555*4882a593Smuzhiyun 
1556*4882a593Smuzhiyun 	/* Positive clk edge capture for input video clock */
1557*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0xba, 0x1f, 0x60);
1558*4882a593Smuzhiyun 
1559*4882a593Smuzhiyun 	adv7511_audio_setup(sd);
1560*4882a593Smuzhiyun 
1561*4882a593Smuzhiyun 	v4l2_ctrl_handler_setup(&state->hdl);
1562*4882a593Smuzhiyun }
1563*4882a593Smuzhiyun 
adv7511_notify_monitor_detect(struct v4l2_subdev * sd)1564*4882a593Smuzhiyun static void adv7511_notify_monitor_detect(struct v4l2_subdev *sd)
1565*4882a593Smuzhiyun {
1566*4882a593Smuzhiyun 	struct adv7511_monitor_detect mdt;
1567*4882a593Smuzhiyun 	struct adv7511_state *state = get_adv7511_state(sd);
1568*4882a593Smuzhiyun 
1569*4882a593Smuzhiyun 	mdt.present = state->have_monitor;
1570*4882a593Smuzhiyun 	v4l2_subdev_notify(sd, ADV7511_MONITOR_DETECT, (void *)&mdt);
1571*4882a593Smuzhiyun }
1572*4882a593Smuzhiyun 
adv7511_check_monitor_present_status(struct v4l2_subdev * sd)1573*4882a593Smuzhiyun static void adv7511_check_monitor_present_status(struct v4l2_subdev *sd)
1574*4882a593Smuzhiyun {
1575*4882a593Smuzhiyun 	struct adv7511_state *state = get_adv7511_state(sd);
1576*4882a593Smuzhiyun 	/* read hotplug and rx-sense state */
1577*4882a593Smuzhiyun 	u8 status = adv7511_rd(sd, 0x42);
1578*4882a593Smuzhiyun 
1579*4882a593Smuzhiyun 	v4l2_dbg(1, debug, sd, "%s: status: 0x%x%s%s\n",
1580*4882a593Smuzhiyun 			 __func__,
1581*4882a593Smuzhiyun 			 status,
1582*4882a593Smuzhiyun 			 status & MASK_ADV7511_HPD_DETECT ? ", hotplug" : "",
1583*4882a593Smuzhiyun 			 status & MASK_ADV7511_MSEN_DETECT ? ", rx-sense" : "");
1584*4882a593Smuzhiyun 
1585*4882a593Smuzhiyun 	/* update read only ctrls */
1586*4882a593Smuzhiyun 	v4l2_ctrl_s_ctrl(state->hotplug_ctrl, adv7511_have_hotplug(sd) ? 0x1 : 0x0);
1587*4882a593Smuzhiyun 	v4l2_ctrl_s_ctrl(state->rx_sense_ctrl, adv7511_have_rx_sense(sd) ? 0x1 : 0x0);
1588*4882a593Smuzhiyun 
1589*4882a593Smuzhiyun 	if ((status & MASK_ADV7511_HPD_DETECT) && ((status & MASK_ADV7511_MSEN_DETECT) || state->edid.segments)) {
1590*4882a593Smuzhiyun 		v4l2_dbg(1, debug, sd, "%s: hotplug and (rx-sense or edid)\n", __func__);
1591*4882a593Smuzhiyun 		if (!state->have_monitor) {
1592*4882a593Smuzhiyun 			v4l2_dbg(1, debug, sd, "%s: monitor detected\n", __func__);
1593*4882a593Smuzhiyun 			state->have_monitor = true;
1594*4882a593Smuzhiyun 			adv7511_set_isr(sd, true);
1595*4882a593Smuzhiyun 			if (!adv7511_s_power(sd, true)) {
1596*4882a593Smuzhiyun 				v4l2_dbg(1, debug, sd, "%s: monitor detected, powerup failed\n", __func__);
1597*4882a593Smuzhiyun 				return;
1598*4882a593Smuzhiyun 			}
1599*4882a593Smuzhiyun 			adv7511_setup(sd);
1600*4882a593Smuzhiyun 			adv7511_notify_monitor_detect(sd);
1601*4882a593Smuzhiyun 			state->edid.read_retries = EDID_MAX_RETRIES;
1602*4882a593Smuzhiyun 			queue_delayed_work(state->work_queue, &state->edid_handler, EDID_DELAY);
1603*4882a593Smuzhiyun 		}
1604*4882a593Smuzhiyun 	} else if (status & MASK_ADV7511_HPD_DETECT) {
1605*4882a593Smuzhiyun 		v4l2_dbg(1, debug, sd, "%s: hotplug detected\n", __func__);
1606*4882a593Smuzhiyun 		state->edid.read_retries = EDID_MAX_RETRIES;
1607*4882a593Smuzhiyun 		queue_delayed_work(state->work_queue, &state->edid_handler, EDID_DELAY);
1608*4882a593Smuzhiyun 	} else if (!(status & MASK_ADV7511_HPD_DETECT)) {
1609*4882a593Smuzhiyun 		v4l2_dbg(1, debug, sd, "%s: hotplug not detected\n", __func__);
1610*4882a593Smuzhiyun 		if (state->have_monitor) {
1611*4882a593Smuzhiyun 			v4l2_dbg(1, debug, sd, "%s: monitor not detected\n", __func__);
1612*4882a593Smuzhiyun 			state->have_monitor = false;
1613*4882a593Smuzhiyun 			adv7511_notify_monitor_detect(sd);
1614*4882a593Smuzhiyun 		}
1615*4882a593Smuzhiyun 		adv7511_s_power(sd, false);
1616*4882a593Smuzhiyun 		memset(&state->edid, 0, sizeof(struct adv7511_state_edid));
1617*4882a593Smuzhiyun 		adv7511_notify_no_edid(sd);
1618*4882a593Smuzhiyun 	}
1619*4882a593Smuzhiyun }
1620*4882a593Smuzhiyun 
edid_block_verify_crc(u8 * edid_block)1621*4882a593Smuzhiyun static bool edid_block_verify_crc(u8 *edid_block)
1622*4882a593Smuzhiyun {
1623*4882a593Smuzhiyun 	u8 sum = 0;
1624*4882a593Smuzhiyun 	int i;
1625*4882a593Smuzhiyun 
1626*4882a593Smuzhiyun 	for (i = 0; i < 128; i++)
1627*4882a593Smuzhiyun 		sum += edid_block[i];
1628*4882a593Smuzhiyun 	return sum == 0;
1629*4882a593Smuzhiyun }
1630*4882a593Smuzhiyun 
edid_verify_crc(struct v4l2_subdev * sd,u32 segment)1631*4882a593Smuzhiyun static bool edid_verify_crc(struct v4l2_subdev *sd, u32 segment)
1632*4882a593Smuzhiyun {
1633*4882a593Smuzhiyun 	struct adv7511_state *state = get_adv7511_state(sd);
1634*4882a593Smuzhiyun 	u32 blocks = state->edid.blocks;
1635*4882a593Smuzhiyun 	u8 *data = state->edid.data;
1636*4882a593Smuzhiyun 
1637*4882a593Smuzhiyun 	if (!edid_block_verify_crc(&data[segment * 256]))
1638*4882a593Smuzhiyun 		return false;
1639*4882a593Smuzhiyun 	if ((segment + 1) * 2 <= blocks)
1640*4882a593Smuzhiyun 		return edid_block_verify_crc(&data[segment * 256 + 128]);
1641*4882a593Smuzhiyun 	return true;
1642*4882a593Smuzhiyun }
1643*4882a593Smuzhiyun 
edid_verify_header(struct v4l2_subdev * sd,u32 segment)1644*4882a593Smuzhiyun static bool edid_verify_header(struct v4l2_subdev *sd, u32 segment)
1645*4882a593Smuzhiyun {
1646*4882a593Smuzhiyun 	static const u8 hdmi_header[] = {
1647*4882a593Smuzhiyun 		0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00
1648*4882a593Smuzhiyun 	};
1649*4882a593Smuzhiyun 	struct adv7511_state *state = get_adv7511_state(sd);
1650*4882a593Smuzhiyun 	u8 *data = state->edid.data;
1651*4882a593Smuzhiyun 
1652*4882a593Smuzhiyun 	if (segment != 0)
1653*4882a593Smuzhiyun 		return true;
1654*4882a593Smuzhiyun 	return !memcmp(data, hdmi_header, sizeof(hdmi_header));
1655*4882a593Smuzhiyun }
1656*4882a593Smuzhiyun 
adv7511_check_edid_status(struct v4l2_subdev * sd)1657*4882a593Smuzhiyun static bool adv7511_check_edid_status(struct v4l2_subdev *sd)
1658*4882a593Smuzhiyun {
1659*4882a593Smuzhiyun 	struct adv7511_state *state = get_adv7511_state(sd);
1660*4882a593Smuzhiyun 	u8 edidRdy = adv7511_rd(sd, 0xc5);
1661*4882a593Smuzhiyun 
1662*4882a593Smuzhiyun 	v4l2_dbg(1, debug, sd, "%s: edid ready (retries: %d)\n",
1663*4882a593Smuzhiyun 			 __func__, EDID_MAX_RETRIES - state->edid.read_retries);
1664*4882a593Smuzhiyun 
1665*4882a593Smuzhiyun 	if (state->edid.complete)
1666*4882a593Smuzhiyun 		return true;
1667*4882a593Smuzhiyun 
1668*4882a593Smuzhiyun 	if (edidRdy & MASK_ADV7511_EDID_RDY) {
1669*4882a593Smuzhiyun 		int segment = adv7511_rd(sd, 0xc4);
1670*4882a593Smuzhiyun 		struct adv7511_edid_detect ed;
1671*4882a593Smuzhiyun 
1672*4882a593Smuzhiyun 		if (segment >= EDID_MAX_SEGM) {
1673*4882a593Smuzhiyun 			v4l2_err(sd, "edid segment number too big\n");
1674*4882a593Smuzhiyun 			return false;
1675*4882a593Smuzhiyun 		}
1676*4882a593Smuzhiyun 		v4l2_dbg(1, debug, sd, "%s: got segment %d\n", __func__, segment);
1677*4882a593Smuzhiyun 		adv7511_edid_rd(sd, 256, &state->edid.data[segment * 256]);
1678*4882a593Smuzhiyun 		adv7511_dbg_dump_edid(2, debug, sd, segment, &state->edid.data[segment * 256]);
1679*4882a593Smuzhiyun 		if (segment == 0) {
1680*4882a593Smuzhiyun 			state->edid.blocks = state->edid.data[0x7e] + 1;
1681*4882a593Smuzhiyun 			v4l2_dbg(1, debug, sd, "%s: %d blocks in total\n", __func__, state->edid.blocks);
1682*4882a593Smuzhiyun 		}
1683*4882a593Smuzhiyun 		if (!edid_verify_crc(sd, segment) ||
1684*4882a593Smuzhiyun 		    !edid_verify_header(sd, segment)) {
1685*4882a593Smuzhiyun 			/* edid crc error, force reread of edid segment */
1686*4882a593Smuzhiyun 			v4l2_err(sd, "%s: edid crc or header error\n", __func__);
1687*4882a593Smuzhiyun 			state->have_monitor = false;
1688*4882a593Smuzhiyun 			adv7511_s_power(sd, false);
1689*4882a593Smuzhiyun 			adv7511_s_power(sd, true);
1690*4882a593Smuzhiyun 			return false;
1691*4882a593Smuzhiyun 		}
1692*4882a593Smuzhiyun 		/* one more segment read ok */
1693*4882a593Smuzhiyun 		state->edid.segments = segment + 1;
1694*4882a593Smuzhiyun 		v4l2_ctrl_s_ctrl(state->have_edid0_ctrl, 0x1);
1695*4882a593Smuzhiyun 		if (((state->edid.data[0x7e] >> 1) + 1) > state->edid.segments) {
1696*4882a593Smuzhiyun 			/* Request next EDID segment */
1697*4882a593Smuzhiyun 			v4l2_dbg(1, debug, sd, "%s: request segment %d\n", __func__, state->edid.segments);
1698*4882a593Smuzhiyun 			adv7511_wr(sd, 0xc9, 0xf);
1699*4882a593Smuzhiyun 			adv7511_wr(sd, 0xc4, state->edid.segments);
1700*4882a593Smuzhiyun 			state->edid.read_retries = EDID_MAX_RETRIES;
1701*4882a593Smuzhiyun 			queue_delayed_work(state->work_queue, &state->edid_handler, EDID_DELAY);
1702*4882a593Smuzhiyun 			return false;
1703*4882a593Smuzhiyun 		}
1704*4882a593Smuzhiyun 
1705*4882a593Smuzhiyun 		v4l2_dbg(1, debug, sd, "%s: edid complete with %d segment(s)\n", __func__, state->edid.segments);
1706*4882a593Smuzhiyun 		state->edid.complete = true;
1707*4882a593Smuzhiyun 		ed.phys_addr = cec_get_edid_phys_addr(state->edid.data,
1708*4882a593Smuzhiyun 						      state->edid.segments * 256,
1709*4882a593Smuzhiyun 						      NULL);
1710*4882a593Smuzhiyun 		/* report when we have all segments
1711*4882a593Smuzhiyun 		   but report only for segment 0
1712*4882a593Smuzhiyun 		 */
1713*4882a593Smuzhiyun 		ed.present = true;
1714*4882a593Smuzhiyun 		ed.segment = 0;
1715*4882a593Smuzhiyun 		state->edid_detect_counter++;
1716*4882a593Smuzhiyun 		cec_s_phys_addr(state->cec_adap, ed.phys_addr, false);
1717*4882a593Smuzhiyun 		v4l2_subdev_notify(sd, ADV7511_EDID_DETECT, (void *)&ed);
1718*4882a593Smuzhiyun 		return ed.present;
1719*4882a593Smuzhiyun 	}
1720*4882a593Smuzhiyun 
1721*4882a593Smuzhiyun 	return false;
1722*4882a593Smuzhiyun }
1723*4882a593Smuzhiyun 
adv7511_registered(struct v4l2_subdev * sd)1724*4882a593Smuzhiyun static int adv7511_registered(struct v4l2_subdev *sd)
1725*4882a593Smuzhiyun {
1726*4882a593Smuzhiyun 	struct adv7511_state *state = get_adv7511_state(sd);
1727*4882a593Smuzhiyun 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1728*4882a593Smuzhiyun 	int err;
1729*4882a593Smuzhiyun 
1730*4882a593Smuzhiyun 	err = cec_register_adapter(state->cec_adap, &client->dev);
1731*4882a593Smuzhiyun 	if (err)
1732*4882a593Smuzhiyun 		cec_delete_adapter(state->cec_adap);
1733*4882a593Smuzhiyun 	return err;
1734*4882a593Smuzhiyun }
1735*4882a593Smuzhiyun 
adv7511_unregistered(struct v4l2_subdev * sd)1736*4882a593Smuzhiyun static void adv7511_unregistered(struct v4l2_subdev *sd)
1737*4882a593Smuzhiyun {
1738*4882a593Smuzhiyun 	struct adv7511_state *state = get_adv7511_state(sd);
1739*4882a593Smuzhiyun 
1740*4882a593Smuzhiyun 	cec_unregister_adapter(state->cec_adap);
1741*4882a593Smuzhiyun }
1742*4882a593Smuzhiyun 
1743*4882a593Smuzhiyun static const struct v4l2_subdev_internal_ops adv7511_int_ops = {
1744*4882a593Smuzhiyun 	.registered = adv7511_registered,
1745*4882a593Smuzhiyun 	.unregistered = adv7511_unregistered,
1746*4882a593Smuzhiyun };
1747*4882a593Smuzhiyun 
1748*4882a593Smuzhiyun /* ----------------------------------------------------------------------- */
1749*4882a593Smuzhiyun /* Setup ADV7511 */
adv7511_init_setup(struct v4l2_subdev * sd)1750*4882a593Smuzhiyun static void adv7511_init_setup(struct v4l2_subdev *sd)
1751*4882a593Smuzhiyun {
1752*4882a593Smuzhiyun 	struct adv7511_state *state = get_adv7511_state(sd);
1753*4882a593Smuzhiyun 	struct adv7511_state_edid *edid = &state->edid;
1754*4882a593Smuzhiyun 	u32 cec_clk = state->pdata.cec_clk;
1755*4882a593Smuzhiyun 	u8 ratio;
1756*4882a593Smuzhiyun 
1757*4882a593Smuzhiyun 	v4l2_dbg(1, debug, sd, "%s\n", __func__);
1758*4882a593Smuzhiyun 
1759*4882a593Smuzhiyun 	/* clear all interrupts */
1760*4882a593Smuzhiyun 	adv7511_wr(sd, 0x96, 0xff);
1761*4882a593Smuzhiyun 	adv7511_wr(sd, 0x97, 0xff);
1762*4882a593Smuzhiyun 	/*
1763*4882a593Smuzhiyun 	 * Stop HPD from resetting a lot of registers.
1764*4882a593Smuzhiyun 	 * It might leave the chip in a partly un-initialized state,
1765*4882a593Smuzhiyun 	 * in particular with regards to hotplug bounces.
1766*4882a593Smuzhiyun 	 */
1767*4882a593Smuzhiyun 	adv7511_wr_and_or(sd, 0xd6, 0x3f, 0xc0);
1768*4882a593Smuzhiyun 	memset(edid, 0, sizeof(struct adv7511_state_edid));
1769*4882a593Smuzhiyun 	state->have_monitor = false;
1770*4882a593Smuzhiyun 	adv7511_set_isr(sd, false);
1771*4882a593Smuzhiyun 	adv7511_s_stream(sd, false);
1772*4882a593Smuzhiyun 	adv7511_s_audio_stream(sd, false);
1773*4882a593Smuzhiyun 
1774*4882a593Smuzhiyun 	if (state->i2c_cec == NULL)
1775*4882a593Smuzhiyun 		return;
1776*4882a593Smuzhiyun 
1777*4882a593Smuzhiyun 	v4l2_dbg(1, debug, sd, "%s: cec_clk %d\n", __func__, cec_clk);
1778*4882a593Smuzhiyun 
1779*4882a593Smuzhiyun 	/* cec soft reset */
1780*4882a593Smuzhiyun 	adv7511_cec_write(sd, 0x50, 0x01);
1781*4882a593Smuzhiyun 	adv7511_cec_write(sd, 0x50, 0x00);
1782*4882a593Smuzhiyun 
1783*4882a593Smuzhiyun 	/* legacy mode */
1784*4882a593Smuzhiyun 	adv7511_cec_write(sd, 0x4a, 0x00);
1785*4882a593Smuzhiyun 	adv7511_cec_write(sd, 0x4a, 0x07);
1786*4882a593Smuzhiyun 
1787*4882a593Smuzhiyun 	if (cec_clk % 750000 != 0)
1788*4882a593Smuzhiyun 		v4l2_err(sd, "%s: cec_clk %d, not multiple of 750 Khz\n",
1789*4882a593Smuzhiyun 			 __func__, cec_clk);
1790*4882a593Smuzhiyun 
1791*4882a593Smuzhiyun 	ratio = (cec_clk / 750000) - 1;
1792*4882a593Smuzhiyun 	adv7511_cec_write(sd, 0x4e, ratio << 2);
1793*4882a593Smuzhiyun }
1794*4882a593Smuzhiyun 
adv7511_probe(struct i2c_client * client,const struct i2c_device_id * id)1795*4882a593Smuzhiyun static int adv7511_probe(struct i2c_client *client, const struct i2c_device_id *id)
1796*4882a593Smuzhiyun {
1797*4882a593Smuzhiyun 	struct adv7511_state *state;
1798*4882a593Smuzhiyun 	struct adv7511_platform_data *pdata = client->dev.platform_data;
1799*4882a593Smuzhiyun 	struct v4l2_ctrl_handler *hdl;
1800*4882a593Smuzhiyun 	struct v4l2_subdev *sd;
1801*4882a593Smuzhiyun 	u8 chip_id[2];
1802*4882a593Smuzhiyun 	int err = -EIO;
1803*4882a593Smuzhiyun 
1804*4882a593Smuzhiyun 	/* Check if the adapter supports the needed features */
1805*4882a593Smuzhiyun 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1806*4882a593Smuzhiyun 		return -EIO;
1807*4882a593Smuzhiyun 
1808*4882a593Smuzhiyun 	state = devm_kzalloc(&client->dev, sizeof(struct adv7511_state), GFP_KERNEL);
1809*4882a593Smuzhiyun 	if (!state)
1810*4882a593Smuzhiyun 		return -ENOMEM;
1811*4882a593Smuzhiyun 
1812*4882a593Smuzhiyun 	/* Platform data */
1813*4882a593Smuzhiyun 	if (!pdata) {
1814*4882a593Smuzhiyun 		v4l_err(client, "No platform data!\n");
1815*4882a593Smuzhiyun 		return -ENODEV;
1816*4882a593Smuzhiyun 	}
1817*4882a593Smuzhiyun 	memcpy(&state->pdata, pdata, sizeof(state->pdata));
1818*4882a593Smuzhiyun 	state->fmt_code = MEDIA_BUS_FMT_RGB888_1X24;
1819*4882a593Smuzhiyun 	state->colorspace = V4L2_COLORSPACE_SRGB;
1820*4882a593Smuzhiyun 
1821*4882a593Smuzhiyun 	sd = &state->sd;
1822*4882a593Smuzhiyun 
1823*4882a593Smuzhiyun 	v4l2_dbg(1, debug, sd, "detecting adv7511 client on address 0x%x\n",
1824*4882a593Smuzhiyun 			 client->addr << 1);
1825*4882a593Smuzhiyun 
1826*4882a593Smuzhiyun 	v4l2_i2c_subdev_init(sd, client, &adv7511_ops);
1827*4882a593Smuzhiyun 	sd->internal_ops = &adv7511_int_ops;
1828*4882a593Smuzhiyun 
1829*4882a593Smuzhiyun 	hdl = &state->hdl;
1830*4882a593Smuzhiyun 	v4l2_ctrl_handler_init(hdl, 10);
1831*4882a593Smuzhiyun 	/* add in ascending ID order */
1832*4882a593Smuzhiyun 	state->hdmi_mode_ctrl = v4l2_ctrl_new_std_menu(hdl, &adv7511_ctrl_ops,
1833*4882a593Smuzhiyun 			V4L2_CID_DV_TX_MODE, V4L2_DV_TX_MODE_HDMI,
1834*4882a593Smuzhiyun 			0, V4L2_DV_TX_MODE_DVI_D);
1835*4882a593Smuzhiyun 	state->hotplug_ctrl = v4l2_ctrl_new_std(hdl, NULL,
1836*4882a593Smuzhiyun 			V4L2_CID_DV_TX_HOTPLUG, 0, 1, 0, 0);
1837*4882a593Smuzhiyun 	state->rx_sense_ctrl = v4l2_ctrl_new_std(hdl, NULL,
1838*4882a593Smuzhiyun 			V4L2_CID_DV_TX_RXSENSE, 0, 1, 0, 0);
1839*4882a593Smuzhiyun 	state->have_edid0_ctrl = v4l2_ctrl_new_std(hdl, NULL,
1840*4882a593Smuzhiyun 			V4L2_CID_DV_TX_EDID_PRESENT, 0, 1, 0, 0);
1841*4882a593Smuzhiyun 	state->rgb_quantization_range_ctrl =
1842*4882a593Smuzhiyun 		v4l2_ctrl_new_std_menu(hdl, &adv7511_ctrl_ops,
1843*4882a593Smuzhiyun 			V4L2_CID_DV_TX_RGB_RANGE, V4L2_DV_RGB_RANGE_FULL,
1844*4882a593Smuzhiyun 			0, V4L2_DV_RGB_RANGE_AUTO);
1845*4882a593Smuzhiyun 	state->content_type_ctrl =
1846*4882a593Smuzhiyun 		v4l2_ctrl_new_std_menu(hdl, &adv7511_ctrl_ops,
1847*4882a593Smuzhiyun 			V4L2_CID_DV_TX_IT_CONTENT_TYPE, V4L2_DV_IT_CONTENT_TYPE_NO_ITC,
1848*4882a593Smuzhiyun 			0, V4L2_DV_IT_CONTENT_TYPE_NO_ITC);
1849*4882a593Smuzhiyun 	sd->ctrl_handler = hdl;
1850*4882a593Smuzhiyun 	if (hdl->error) {
1851*4882a593Smuzhiyun 		err = hdl->error;
1852*4882a593Smuzhiyun 		goto err_hdl;
1853*4882a593Smuzhiyun 	}
1854*4882a593Smuzhiyun 	state->pad.flags = MEDIA_PAD_FL_SINK;
1855*4882a593Smuzhiyun 	sd->entity.function = MEDIA_ENT_F_DV_ENCODER;
1856*4882a593Smuzhiyun 	err = media_entity_pads_init(&sd->entity, 1, &state->pad);
1857*4882a593Smuzhiyun 	if (err)
1858*4882a593Smuzhiyun 		goto err_hdl;
1859*4882a593Smuzhiyun 
1860*4882a593Smuzhiyun 	/* EDID and CEC i2c addr */
1861*4882a593Smuzhiyun 	state->i2c_edid_addr = state->pdata.i2c_edid << 1;
1862*4882a593Smuzhiyun 	state->i2c_cec_addr = state->pdata.i2c_cec << 1;
1863*4882a593Smuzhiyun 	state->i2c_pktmem_addr = state->pdata.i2c_pktmem << 1;
1864*4882a593Smuzhiyun 
1865*4882a593Smuzhiyun 	state->chip_revision = adv7511_rd(sd, 0x0);
1866*4882a593Smuzhiyun 	chip_id[0] = adv7511_rd(sd, 0xf5);
1867*4882a593Smuzhiyun 	chip_id[1] = adv7511_rd(sd, 0xf6);
1868*4882a593Smuzhiyun 	if (chip_id[0] != 0x75 || chip_id[1] != 0x11) {
1869*4882a593Smuzhiyun 		v4l2_err(sd, "chip_id != 0x7511, read 0x%02x%02x\n", chip_id[0],
1870*4882a593Smuzhiyun 			 chip_id[1]);
1871*4882a593Smuzhiyun 		err = -EIO;
1872*4882a593Smuzhiyun 		goto err_entity;
1873*4882a593Smuzhiyun 	}
1874*4882a593Smuzhiyun 
1875*4882a593Smuzhiyun 	state->i2c_edid = i2c_new_dummy_device(client->adapter,
1876*4882a593Smuzhiyun 					state->i2c_edid_addr >> 1);
1877*4882a593Smuzhiyun 	if (IS_ERR(state->i2c_edid)) {
1878*4882a593Smuzhiyun 		v4l2_err(sd, "failed to register edid i2c client\n");
1879*4882a593Smuzhiyun 		err = PTR_ERR(state->i2c_edid);
1880*4882a593Smuzhiyun 		goto err_entity;
1881*4882a593Smuzhiyun 	}
1882*4882a593Smuzhiyun 
1883*4882a593Smuzhiyun 	adv7511_wr(sd, 0xe1, state->i2c_cec_addr);
1884*4882a593Smuzhiyun 	if (state->pdata.cec_clk < 3000000 ||
1885*4882a593Smuzhiyun 	    state->pdata.cec_clk > 100000000) {
1886*4882a593Smuzhiyun 		v4l2_err(sd, "%s: cec_clk %u outside range, disabling cec\n",
1887*4882a593Smuzhiyun 				__func__, state->pdata.cec_clk);
1888*4882a593Smuzhiyun 		state->pdata.cec_clk = 0;
1889*4882a593Smuzhiyun 	}
1890*4882a593Smuzhiyun 
1891*4882a593Smuzhiyun 	if (state->pdata.cec_clk) {
1892*4882a593Smuzhiyun 		state->i2c_cec = i2c_new_dummy_device(client->adapter,
1893*4882a593Smuzhiyun 					       state->i2c_cec_addr >> 1);
1894*4882a593Smuzhiyun 		if (IS_ERR(state->i2c_cec)) {
1895*4882a593Smuzhiyun 			v4l2_err(sd, "failed to register cec i2c client\n");
1896*4882a593Smuzhiyun 			err = PTR_ERR(state->i2c_cec);
1897*4882a593Smuzhiyun 			goto err_unreg_edid;
1898*4882a593Smuzhiyun 		}
1899*4882a593Smuzhiyun 		adv7511_wr(sd, 0xe2, 0x00); /* power up cec section */
1900*4882a593Smuzhiyun 	} else {
1901*4882a593Smuzhiyun 		adv7511_wr(sd, 0xe2, 0x01); /* power down cec section */
1902*4882a593Smuzhiyun 	}
1903*4882a593Smuzhiyun 
1904*4882a593Smuzhiyun 	state->i2c_pktmem = i2c_new_dummy_device(client->adapter, state->i2c_pktmem_addr >> 1);
1905*4882a593Smuzhiyun 	if (IS_ERR(state->i2c_pktmem)) {
1906*4882a593Smuzhiyun 		v4l2_err(sd, "failed to register pktmem i2c client\n");
1907*4882a593Smuzhiyun 		err = PTR_ERR(state->i2c_pktmem);
1908*4882a593Smuzhiyun 		goto err_unreg_cec;
1909*4882a593Smuzhiyun 	}
1910*4882a593Smuzhiyun 
1911*4882a593Smuzhiyun 	state->work_queue = create_singlethread_workqueue(sd->name);
1912*4882a593Smuzhiyun 	if (state->work_queue == NULL) {
1913*4882a593Smuzhiyun 		v4l2_err(sd, "could not create workqueue\n");
1914*4882a593Smuzhiyun 		err = -ENOMEM;
1915*4882a593Smuzhiyun 		goto err_unreg_pktmem;
1916*4882a593Smuzhiyun 	}
1917*4882a593Smuzhiyun 
1918*4882a593Smuzhiyun 	INIT_DELAYED_WORK(&state->edid_handler, adv7511_edid_handler);
1919*4882a593Smuzhiyun 
1920*4882a593Smuzhiyun 	adv7511_init_setup(sd);
1921*4882a593Smuzhiyun 
1922*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_VIDEO_ADV7511_CEC)
1923*4882a593Smuzhiyun 	state->cec_adap = cec_allocate_adapter(&adv7511_cec_adap_ops,
1924*4882a593Smuzhiyun 		state, dev_name(&client->dev), CEC_CAP_DEFAULTS,
1925*4882a593Smuzhiyun 		ADV7511_MAX_ADDRS);
1926*4882a593Smuzhiyun 	err = PTR_ERR_OR_ZERO(state->cec_adap);
1927*4882a593Smuzhiyun 	if (err) {
1928*4882a593Smuzhiyun 		destroy_workqueue(state->work_queue);
1929*4882a593Smuzhiyun 		goto err_unreg_pktmem;
1930*4882a593Smuzhiyun 	}
1931*4882a593Smuzhiyun #endif
1932*4882a593Smuzhiyun 
1933*4882a593Smuzhiyun 	adv7511_set_isr(sd, true);
1934*4882a593Smuzhiyun 	adv7511_check_monitor_present_status(sd);
1935*4882a593Smuzhiyun 
1936*4882a593Smuzhiyun 	v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
1937*4882a593Smuzhiyun 			  client->addr << 1, client->adapter->name);
1938*4882a593Smuzhiyun 	return 0;
1939*4882a593Smuzhiyun 
1940*4882a593Smuzhiyun err_unreg_pktmem:
1941*4882a593Smuzhiyun 	i2c_unregister_device(state->i2c_pktmem);
1942*4882a593Smuzhiyun err_unreg_cec:
1943*4882a593Smuzhiyun 	i2c_unregister_device(state->i2c_cec);
1944*4882a593Smuzhiyun err_unreg_edid:
1945*4882a593Smuzhiyun 	i2c_unregister_device(state->i2c_edid);
1946*4882a593Smuzhiyun err_entity:
1947*4882a593Smuzhiyun 	media_entity_cleanup(&sd->entity);
1948*4882a593Smuzhiyun err_hdl:
1949*4882a593Smuzhiyun 	v4l2_ctrl_handler_free(&state->hdl);
1950*4882a593Smuzhiyun 	return err;
1951*4882a593Smuzhiyun }
1952*4882a593Smuzhiyun 
1953*4882a593Smuzhiyun /* ----------------------------------------------------------------------- */
1954*4882a593Smuzhiyun 
adv7511_remove(struct i2c_client * client)1955*4882a593Smuzhiyun static int adv7511_remove(struct i2c_client *client)
1956*4882a593Smuzhiyun {
1957*4882a593Smuzhiyun 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1958*4882a593Smuzhiyun 	struct adv7511_state *state = get_adv7511_state(sd);
1959*4882a593Smuzhiyun 
1960*4882a593Smuzhiyun 	state->chip_revision = -1;
1961*4882a593Smuzhiyun 
1962*4882a593Smuzhiyun 	v4l2_dbg(1, debug, sd, "%s removed @ 0x%x (%s)\n", client->name,
1963*4882a593Smuzhiyun 		 client->addr << 1, client->adapter->name);
1964*4882a593Smuzhiyun 
1965*4882a593Smuzhiyun 	adv7511_set_isr(sd, false);
1966*4882a593Smuzhiyun 	adv7511_init_setup(sd);
1967*4882a593Smuzhiyun 	cancel_delayed_work_sync(&state->edid_handler);
1968*4882a593Smuzhiyun 	i2c_unregister_device(state->i2c_edid);
1969*4882a593Smuzhiyun 	i2c_unregister_device(state->i2c_cec);
1970*4882a593Smuzhiyun 	i2c_unregister_device(state->i2c_pktmem);
1971*4882a593Smuzhiyun 	destroy_workqueue(state->work_queue);
1972*4882a593Smuzhiyun 	v4l2_device_unregister_subdev(sd);
1973*4882a593Smuzhiyun 	media_entity_cleanup(&sd->entity);
1974*4882a593Smuzhiyun 	v4l2_ctrl_handler_free(sd->ctrl_handler);
1975*4882a593Smuzhiyun 	return 0;
1976*4882a593Smuzhiyun }
1977*4882a593Smuzhiyun 
1978*4882a593Smuzhiyun /* ----------------------------------------------------------------------- */
1979*4882a593Smuzhiyun 
1980*4882a593Smuzhiyun static const struct i2c_device_id adv7511_id[] = {
1981*4882a593Smuzhiyun 	{ "adv7511-v4l2", 0 },
1982*4882a593Smuzhiyun 	{ }
1983*4882a593Smuzhiyun };
1984*4882a593Smuzhiyun MODULE_DEVICE_TABLE(i2c, adv7511_id);
1985*4882a593Smuzhiyun 
1986*4882a593Smuzhiyun static struct i2c_driver adv7511_driver = {
1987*4882a593Smuzhiyun 	.driver = {
1988*4882a593Smuzhiyun 		.name = "adv7511-v4l2",
1989*4882a593Smuzhiyun 	},
1990*4882a593Smuzhiyun 	.probe = adv7511_probe,
1991*4882a593Smuzhiyun 	.remove = adv7511_remove,
1992*4882a593Smuzhiyun 	.id_table = adv7511_id,
1993*4882a593Smuzhiyun };
1994*4882a593Smuzhiyun 
1995*4882a593Smuzhiyun module_i2c_driver(adv7511_driver);
1996