]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - drivers/media/i2c/adv7604.c
[media] adv7604: Make output format configurable through pad format operations
[mirror_ubuntu-eoan-kernel.git] / drivers / media / i2c / adv7604.c
CommitLineData
54450f59
HV
1/*
2 * adv7604 - Analog Devices ADV7604 video decoder driver
3 *
4 * Copyright 2012 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
5 *
6 * This program is free software; you may redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
11 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
12 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
13 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
14 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
15 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
16 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17 * SOFTWARE.
18 *
19 */
20
21/*
22 * References (c = chapter, p = page):
23 * REF_01 - Analog devices, ADV7604, Register Settings Recommendations,
24 * Revision 2.5, June 2010
25 * REF_02 - Analog devices, Register map documentation, Documentation of
26 * the register maps, Software manual, Rev. F, June 2010
27 * REF_03 - Analog devices, ADV7604, Hardware Manual, Rev. F, August 2010
28 */
29
30
31#include <linux/kernel.h>
32#include <linux/module.h>
33#include <linux/slab.h>
34#include <linux/i2c.h>
35#include <linux/delay.h>
36#include <linux/videodev2.h>
37#include <linux/workqueue.h>
38#include <linux/v4l2-dv-timings.h>
39#include <media/v4l2-device.h>
40#include <media/v4l2-ctrls.h>
25764158 41#include <media/v4l2-dv-timings.h>
54450f59
HV
42#include <media/adv7604.h>
43
44static int debug;
45module_param(debug, int, 0644);
46MODULE_PARM_DESC(debug, "debug level (0-2)");
47
48MODULE_DESCRIPTION("Analog Devices ADV7604 video decoder driver");
49MODULE_AUTHOR("Hans Verkuil <hans.verkuil@cisco.com>");
50MODULE_AUTHOR("Mats Randgaard <mats.randgaard@cisco.com>");
51MODULE_LICENSE("GPL");
52
53/* ADV7604 system clock frequency */
54#define ADV7604_fsc (28636360)
55
539b33b0
LP
56#define ADV7604_RGB_OUT (1 << 1)
57
58#define ADV7604_OP_FORMAT_SEL_8BIT (0 << 0)
59#define ADV7604_OP_FORMAT_SEL_10BIT (1 << 0)
60#define ADV7604_OP_FORMAT_SEL_12BIT (2 << 0)
61
62#define ADV7604_OP_MODE_SEL_SDR_422 (0 << 5)
63#define ADV7604_OP_MODE_SEL_DDR_422 (1 << 5)
64#define ADV7604_OP_MODE_SEL_SDR_444 (2 << 5)
65#define ADV7604_OP_MODE_SEL_DDR_444 (3 << 5)
66#define ADV7604_OP_MODE_SEL_SDR_422_2X (4 << 5)
67#define ADV7604_OP_MODE_SEL_ADI_CM (5 << 5)
68
69#define ADV7604_OP_CH_SEL_GBR (0 << 5)
70#define ADV7604_OP_CH_SEL_GRB (1 << 5)
71#define ADV7604_OP_CH_SEL_BGR (2 << 5)
72#define ADV7604_OP_CH_SEL_RGB (3 << 5)
73#define ADV7604_OP_CH_SEL_BRG (4 << 5)
74#define ADV7604_OP_CH_SEL_RBG (5 << 5)
75
76#define ADV7604_OP_SWAP_CB_CR (1 << 0)
77
d42010a1
LPC
78enum adv7604_type {
79 ADV7604,
80 ADV7611,
81};
82
83struct adv7604_reg_seq {
84 unsigned int reg;
85 u8 val;
86};
87
539b33b0
LP
88struct adv7604_format_info {
89 enum v4l2_mbus_pixelcode code;
90 u8 op_ch_sel;
91 bool rgb_out;
92 bool swap_cb_cr;
93 u8 op_format_sel;
94};
95
d42010a1
LPC
96struct adv7604_chip_info {
97 enum adv7604_type type;
98
99 bool has_afe;
100 unsigned int max_port;
101 unsigned int num_dv_ports;
102
103 unsigned int edid_enable_reg;
104 unsigned int edid_status_reg;
105 unsigned int lcf_reg;
106
107 unsigned int cable_det_mask;
108 unsigned int tdms_lock_mask;
109 unsigned int fmt_change_digital_mask;
110
539b33b0
LP
111 const struct adv7604_format_info *formats;
112 unsigned int nformats;
113
d42010a1
LPC
114 void (*set_termination)(struct v4l2_subdev *sd, bool enable);
115 void (*setup_irqs)(struct v4l2_subdev *sd);
116 unsigned int (*read_hdmi_pixelclock)(struct v4l2_subdev *sd);
117 unsigned int (*read_cable_det)(struct v4l2_subdev *sd);
118
119 /* 0 = AFE, 1 = HDMI */
120 const struct adv7604_reg_seq *recommended_settings[2];
121 unsigned int num_recommended_settings[2];
122
123 unsigned long page_mask;
124};
125
54450f59
HV
126/*
127 **********************************************************************
128 *
129 * Arrays with configuration parameters for the ADV7604
130 *
131 **********************************************************************
132 */
c784b1e2 133
54450f59 134struct adv7604_state {
d42010a1 135 const struct adv7604_chip_info *info;
54450f59 136 struct adv7604_platform_data pdata;
539b33b0 137
54450f59 138 struct v4l2_subdev sd;
c784b1e2
LP
139 struct media_pad pads[ADV7604_PAD_MAX];
140 unsigned int source_pad;
539b33b0 141
54450f59 142 struct v4l2_ctrl_handler hdl;
539b33b0 143
c784b1e2 144 enum adv7604_pad selected_input;
539b33b0 145
54450f59 146 struct v4l2_dv_timings timings;
539b33b0
LP
147 const struct adv7604_format_info *format;
148
4a31a93a
MR
149 struct {
150 u8 edid[256];
151 u32 present;
152 unsigned blocks;
153 } edid;
dd08beb9 154 u16 spa_port_a[2];
54450f59
HV
155 struct v4l2_fract aspect_ratio;
156 u32 rgb_quantization_range;
157 struct workqueue_struct *work_queues;
158 struct delayed_work delayed_work_enable_hotplug;
cf9afb1d 159 bool restart_stdi_once;
54450f59
HV
160
161 /* i2c clients */
162 struct i2c_client *i2c_avlink;
163 struct i2c_client *i2c_cec;
164 struct i2c_client *i2c_infoframe;
165 struct i2c_client *i2c_esdp;
166 struct i2c_client *i2c_dpp;
167 struct i2c_client *i2c_afe;
168 struct i2c_client *i2c_repeater;
169 struct i2c_client *i2c_edid;
170 struct i2c_client *i2c_hdmi;
171 struct i2c_client *i2c_test;
172 struct i2c_client *i2c_cp;
173 struct i2c_client *i2c_vdp;
174
175 /* controls */
176 struct v4l2_ctrl *detect_tx_5v_ctrl;
177 struct v4l2_ctrl *analog_sampling_phase_ctrl;
178 struct v4l2_ctrl *free_run_color_manual_ctrl;
179 struct v4l2_ctrl *free_run_color_ctrl;
180 struct v4l2_ctrl *rgb_quantization_range_ctrl;
181};
182
d42010a1
LPC
183static bool adv7604_has_afe(struct adv7604_state *state)
184{
185 return state->info->has_afe;
186}
187
54450f59
HV
188/* Supported CEA and DMT timings */
189static const struct v4l2_dv_timings adv7604_timings[] = {
190 V4L2_DV_BT_CEA_720X480P59_94,
191 V4L2_DV_BT_CEA_720X576P50,
192 V4L2_DV_BT_CEA_1280X720P24,
193 V4L2_DV_BT_CEA_1280X720P25,
54450f59
HV
194 V4L2_DV_BT_CEA_1280X720P50,
195 V4L2_DV_BT_CEA_1280X720P60,
196 V4L2_DV_BT_CEA_1920X1080P24,
197 V4L2_DV_BT_CEA_1920X1080P25,
198 V4L2_DV_BT_CEA_1920X1080P30,
199 V4L2_DV_BT_CEA_1920X1080P50,
200 V4L2_DV_BT_CEA_1920X1080P60,
201
ccbd5bc4 202 /* sorted by DMT ID */
54450f59
HV
203 V4L2_DV_BT_DMT_640X350P85,
204 V4L2_DV_BT_DMT_640X400P85,
205 V4L2_DV_BT_DMT_720X400P85,
206 V4L2_DV_BT_DMT_640X480P60,
207 V4L2_DV_BT_DMT_640X480P72,
208 V4L2_DV_BT_DMT_640X480P75,
209 V4L2_DV_BT_DMT_640X480P85,
210 V4L2_DV_BT_DMT_800X600P56,
211 V4L2_DV_BT_DMT_800X600P60,
212 V4L2_DV_BT_DMT_800X600P72,
213 V4L2_DV_BT_DMT_800X600P75,
214 V4L2_DV_BT_DMT_800X600P85,
215 V4L2_DV_BT_DMT_848X480P60,
216 V4L2_DV_BT_DMT_1024X768P60,
217 V4L2_DV_BT_DMT_1024X768P70,
218 V4L2_DV_BT_DMT_1024X768P75,
219 V4L2_DV_BT_DMT_1024X768P85,
220 V4L2_DV_BT_DMT_1152X864P75,
221 V4L2_DV_BT_DMT_1280X768P60_RB,
222 V4L2_DV_BT_DMT_1280X768P60,
223 V4L2_DV_BT_DMT_1280X768P75,
224 V4L2_DV_BT_DMT_1280X768P85,
225 V4L2_DV_BT_DMT_1280X800P60_RB,
226 V4L2_DV_BT_DMT_1280X800P60,
227 V4L2_DV_BT_DMT_1280X800P75,
228 V4L2_DV_BT_DMT_1280X800P85,
229 V4L2_DV_BT_DMT_1280X960P60,
230 V4L2_DV_BT_DMT_1280X960P85,
231 V4L2_DV_BT_DMT_1280X1024P60,
232 V4L2_DV_BT_DMT_1280X1024P75,
233 V4L2_DV_BT_DMT_1280X1024P85,
234 V4L2_DV_BT_DMT_1360X768P60,
235 V4L2_DV_BT_DMT_1400X1050P60_RB,
236 V4L2_DV_BT_DMT_1400X1050P60,
237 V4L2_DV_BT_DMT_1400X1050P75,
238 V4L2_DV_BT_DMT_1400X1050P85,
239 V4L2_DV_BT_DMT_1440X900P60_RB,
240 V4L2_DV_BT_DMT_1440X900P60,
241 V4L2_DV_BT_DMT_1600X1200P60,
242 V4L2_DV_BT_DMT_1680X1050P60_RB,
243 V4L2_DV_BT_DMT_1680X1050P60,
244 V4L2_DV_BT_DMT_1792X1344P60,
245 V4L2_DV_BT_DMT_1856X1392P60,
246 V4L2_DV_BT_DMT_1920X1200P60_RB,
547ed542 247 V4L2_DV_BT_DMT_1366X768P60_RB,
54450f59
HV
248 V4L2_DV_BT_DMT_1366X768P60,
249 V4L2_DV_BT_DMT_1920X1080P60,
250 { },
251};
252
ccbd5bc4
HV
253struct adv7604_video_standards {
254 struct v4l2_dv_timings timings;
255 u8 vid_std;
256 u8 v_freq;
257};
258
259/* sorted by number of lines */
260static const struct adv7604_video_standards adv7604_prim_mode_comp[] = {
261 /* { V4L2_DV_BT_CEA_720X480P59_94, 0x0a, 0x00 }, TODO flickering */
262 { V4L2_DV_BT_CEA_720X576P50, 0x0b, 0x00 },
263 { V4L2_DV_BT_CEA_1280X720P50, 0x19, 0x01 },
264 { V4L2_DV_BT_CEA_1280X720P60, 0x19, 0x00 },
265 { V4L2_DV_BT_CEA_1920X1080P24, 0x1e, 0x04 },
266 { V4L2_DV_BT_CEA_1920X1080P25, 0x1e, 0x03 },
267 { V4L2_DV_BT_CEA_1920X1080P30, 0x1e, 0x02 },
268 { V4L2_DV_BT_CEA_1920X1080P50, 0x1e, 0x01 },
269 { V4L2_DV_BT_CEA_1920X1080P60, 0x1e, 0x00 },
270 /* TODO add 1920x1080P60_RB (CVT timing) */
271 { },
272};
273
274/* sorted by number of lines */
275static const struct adv7604_video_standards adv7604_prim_mode_gr[] = {
276 { V4L2_DV_BT_DMT_640X480P60, 0x08, 0x00 },
277 { V4L2_DV_BT_DMT_640X480P72, 0x09, 0x00 },
278 { V4L2_DV_BT_DMT_640X480P75, 0x0a, 0x00 },
279 { V4L2_DV_BT_DMT_640X480P85, 0x0b, 0x00 },
280 { V4L2_DV_BT_DMT_800X600P56, 0x00, 0x00 },
281 { V4L2_DV_BT_DMT_800X600P60, 0x01, 0x00 },
282 { V4L2_DV_BT_DMT_800X600P72, 0x02, 0x00 },
283 { V4L2_DV_BT_DMT_800X600P75, 0x03, 0x00 },
284 { V4L2_DV_BT_DMT_800X600P85, 0x04, 0x00 },
285 { V4L2_DV_BT_DMT_1024X768P60, 0x0c, 0x00 },
286 { V4L2_DV_BT_DMT_1024X768P70, 0x0d, 0x00 },
287 { V4L2_DV_BT_DMT_1024X768P75, 0x0e, 0x00 },
288 { V4L2_DV_BT_DMT_1024X768P85, 0x0f, 0x00 },
289 { V4L2_DV_BT_DMT_1280X1024P60, 0x05, 0x00 },
290 { V4L2_DV_BT_DMT_1280X1024P75, 0x06, 0x00 },
291 { V4L2_DV_BT_DMT_1360X768P60, 0x12, 0x00 },
292 { V4L2_DV_BT_DMT_1366X768P60, 0x13, 0x00 },
293 { V4L2_DV_BT_DMT_1400X1050P60, 0x14, 0x00 },
294 { V4L2_DV_BT_DMT_1400X1050P75, 0x15, 0x00 },
295 { V4L2_DV_BT_DMT_1600X1200P60, 0x16, 0x00 }, /* TODO not tested */
296 /* TODO add 1600X1200P60_RB (not a DMT timing) */
297 { V4L2_DV_BT_DMT_1680X1050P60, 0x18, 0x00 },
298 { V4L2_DV_BT_DMT_1920X1200P60_RB, 0x19, 0x00 }, /* TODO not tested */
299 { },
300};
301
302/* sorted by number of lines */
303static const struct adv7604_video_standards adv7604_prim_mode_hdmi_comp[] = {
304 { V4L2_DV_BT_CEA_720X480P59_94, 0x0a, 0x00 },
305 { V4L2_DV_BT_CEA_720X576P50, 0x0b, 0x00 },
306 { V4L2_DV_BT_CEA_1280X720P50, 0x13, 0x01 },
307 { V4L2_DV_BT_CEA_1280X720P60, 0x13, 0x00 },
308 { V4L2_DV_BT_CEA_1920X1080P24, 0x1e, 0x04 },
309 { V4L2_DV_BT_CEA_1920X1080P25, 0x1e, 0x03 },
310 { V4L2_DV_BT_CEA_1920X1080P30, 0x1e, 0x02 },
311 { V4L2_DV_BT_CEA_1920X1080P50, 0x1e, 0x01 },
312 { V4L2_DV_BT_CEA_1920X1080P60, 0x1e, 0x00 },
313 { },
314};
315
316/* sorted by number of lines */
317static const struct adv7604_video_standards adv7604_prim_mode_hdmi_gr[] = {
318 { V4L2_DV_BT_DMT_640X480P60, 0x08, 0x00 },
319 { V4L2_DV_BT_DMT_640X480P72, 0x09, 0x00 },
320 { V4L2_DV_BT_DMT_640X480P75, 0x0a, 0x00 },
321 { V4L2_DV_BT_DMT_640X480P85, 0x0b, 0x00 },
322 { V4L2_DV_BT_DMT_800X600P56, 0x00, 0x00 },
323 { V4L2_DV_BT_DMT_800X600P60, 0x01, 0x00 },
324 { V4L2_DV_BT_DMT_800X600P72, 0x02, 0x00 },
325 { V4L2_DV_BT_DMT_800X600P75, 0x03, 0x00 },
326 { V4L2_DV_BT_DMT_800X600P85, 0x04, 0x00 },
327 { V4L2_DV_BT_DMT_1024X768P60, 0x0c, 0x00 },
328 { V4L2_DV_BT_DMT_1024X768P70, 0x0d, 0x00 },
329 { V4L2_DV_BT_DMT_1024X768P75, 0x0e, 0x00 },
330 { V4L2_DV_BT_DMT_1024X768P85, 0x0f, 0x00 },
331 { V4L2_DV_BT_DMT_1280X1024P60, 0x05, 0x00 },
332 { V4L2_DV_BT_DMT_1280X1024P75, 0x06, 0x00 },
333 { },
334};
335
54450f59
HV
336/* ----------------------------------------------------------------------- */
337
338static inline struct adv7604_state *to_state(struct v4l2_subdev *sd)
339{
340 return container_of(sd, struct adv7604_state, sd);
341}
342
343static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
344{
345 return &container_of(ctrl->handler, struct adv7604_state, hdl)->sd;
346}
347
348static inline unsigned hblanking(const struct v4l2_bt_timings *t)
349{
eacf8f9a 350 return V4L2_DV_BT_BLANKING_WIDTH(t);
54450f59
HV
351}
352
353static inline unsigned htotal(const struct v4l2_bt_timings *t)
354{
eacf8f9a 355 return V4L2_DV_BT_FRAME_WIDTH(t);
54450f59
HV
356}
357
358static inline unsigned vblanking(const struct v4l2_bt_timings *t)
359{
eacf8f9a 360 return V4L2_DV_BT_BLANKING_HEIGHT(t);
54450f59
HV
361}
362
363static inline unsigned vtotal(const struct v4l2_bt_timings *t)
364{
eacf8f9a 365 return V4L2_DV_BT_FRAME_HEIGHT(t);
54450f59
HV
366}
367
368/* ----------------------------------------------------------------------- */
369
370static s32 adv_smbus_read_byte_data_check(struct i2c_client *client,
371 u8 command, bool check)
372{
373 union i2c_smbus_data data;
374
375 if (!i2c_smbus_xfer(client->adapter, client->addr, client->flags,
376 I2C_SMBUS_READ, command,
377 I2C_SMBUS_BYTE_DATA, &data))
378 return data.byte;
379 if (check)
380 v4l_err(client, "error reading %02x, %02x\n",
381 client->addr, command);
382 return -EIO;
383}
384
385static s32 adv_smbus_read_byte_data(struct i2c_client *client, u8 command)
386{
387 return adv_smbus_read_byte_data_check(client, command, true);
388}
389
390static s32 adv_smbus_write_byte_data(struct i2c_client *client,
391 u8 command, u8 value)
392{
393 union i2c_smbus_data data;
394 int err;
395 int i;
396
397 data.byte = value;
398 for (i = 0; i < 3; i++) {
399 err = i2c_smbus_xfer(client->adapter, client->addr,
400 client->flags,
401 I2C_SMBUS_WRITE, command,
402 I2C_SMBUS_BYTE_DATA, &data);
403 if (!err)
404 break;
405 }
406 if (err < 0)
407 v4l_err(client, "error writing %02x, %02x, %02x\n",
408 client->addr, command, value);
409 return err;
410}
411
412static s32 adv_smbus_write_i2c_block_data(struct i2c_client *client,
413 u8 command, unsigned length, const u8 *values)
414{
415 union i2c_smbus_data data;
416
417 if (length > I2C_SMBUS_BLOCK_MAX)
418 length = I2C_SMBUS_BLOCK_MAX;
419 data.block[0] = length;
420 memcpy(data.block + 1, values, length);
421 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
422 I2C_SMBUS_WRITE, command,
423 I2C_SMBUS_I2C_BLOCK_DATA, &data);
424}
425
426/* ----------------------------------------------------------------------- */
427
428static inline int io_read(struct v4l2_subdev *sd, u8 reg)
429{
430 struct i2c_client *client = v4l2_get_subdevdata(sd);
431
432 return adv_smbus_read_byte_data(client, reg);
433}
434
435static inline int io_write(struct v4l2_subdev *sd, u8 reg, u8 val)
436{
437 struct i2c_client *client = v4l2_get_subdevdata(sd);
438
439 return adv_smbus_write_byte_data(client, reg, val);
440}
441
442static inline int io_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
443{
444 return io_write(sd, reg, (io_read(sd, reg) & mask) | val);
445}
446
447static inline int avlink_read(struct v4l2_subdev *sd, u8 reg)
448{
449 struct adv7604_state *state = to_state(sd);
450
451 return adv_smbus_read_byte_data(state->i2c_avlink, reg);
452}
453
454static inline int avlink_write(struct v4l2_subdev *sd, u8 reg, u8 val)
455{
456 struct adv7604_state *state = to_state(sd);
457
458 return adv_smbus_write_byte_data(state->i2c_avlink, reg, val);
459}
460
461static inline int cec_read(struct v4l2_subdev *sd, u8 reg)
462{
463 struct adv7604_state *state = to_state(sd);
464
465 return adv_smbus_read_byte_data(state->i2c_cec, reg);
466}
467
468static inline int cec_write(struct v4l2_subdev *sd, u8 reg, u8 val)
469{
470 struct adv7604_state *state = to_state(sd);
471
472 return adv_smbus_write_byte_data(state->i2c_cec, reg, val);
473}
474
475static inline int cec_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
476{
477 return cec_write(sd, reg, (cec_read(sd, reg) & mask) | val);
478}
479
480static inline int infoframe_read(struct v4l2_subdev *sd, u8 reg)
481{
482 struct adv7604_state *state = to_state(sd);
483
484 return adv_smbus_read_byte_data(state->i2c_infoframe, reg);
485}
486
487static inline int infoframe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
488{
489 struct adv7604_state *state = to_state(sd);
490
491 return adv_smbus_write_byte_data(state->i2c_infoframe, reg, val);
492}
493
494static inline int esdp_read(struct v4l2_subdev *sd, u8 reg)
495{
496 struct adv7604_state *state = to_state(sd);
497
498 return adv_smbus_read_byte_data(state->i2c_esdp, reg);
499}
500
501static inline int esdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
502{
503 struct adv7604_state *state = to_state(sd);
504
505 return adv_smbus_write_byte_data(state->i2c_esdp, reg, val);
506}
507
508static inline int dpp_read(struct v4l2_subdev *sd, u8 reg)
509{
510 struct adv7604_state *state = to_state(sd);
511
512 return adv_smbus_read_byte_data(state->i2c_dpp, reg);
513}
514
515static inline int dpp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
516{
517 struct adv7604_state *state = to_state(sd);
518
519 return adv_smbus_write_byte_data(state->i2c_dpp, reg, val);
520}
521
522static inline int afe_read(struct v4l2_subdev *sd, u8 reg)
523{
524 struct adv7604_state *state = to_state(sd);
525
526 return adv_smbus_read_byte_data(state->i2c_afe, reg);
527}
528
529static inline int afe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
530{
531 struct adv7604_state *state = to_state(sd);
532
533 return adv_smbus_write_byte_data(state->i2c_afe, reg, val);
534}
535
536static inline int rep_read(struct v4l2_subdev *sd, u8 reg)
537{
538 struct adv7604_state *state = to_state(sd);
539
540 return adv_smbus_read_byte_data(state->i2c_repeater, reg);
541}
542
543static inline int rep_write(struct v4l2_subdev *sd, u8 reg, u8 val)
544{
545 struct adv7604_state *state = to_state(sd);
546
547 return adv_smbus_write_byte_data(state->i2c_repeater, reg, val);
548}
549
550static inline int rep_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
551{
552 return rep_write(sd, reg, (rep_read(sd, reg) & mask) | val);
553}
554
555static inline int edid_read(struct v4l2_subdev *sd, u8 reg)
556{
557 struct adv7604_state *state = to_state(sd);
558
559 return adv_smbus_read_byte_data(state->i2c_edid, reg);
560}
561
562static inline int edid_write(struct v4l2_subdev *sd, u8 reg, u8 val)
563{
564 struct adv7604_state *state = to_state(sd);
565
566 return adv_smbus_write_byte_data(state->i2c_edid, reg, val);
567}
568
569static inline int edid_read_block(struct v4l2_subdev *sd, unsigned len, u8 *val)
570{
571 struct adv7604_state *state = to_state(sd);
572 struct i2c_client *client = state->i2c_edid;
573 u8 msgbuf0[1] = { 0 };
574 u8 msgbuf1[256];
09f29673
S
575 struct i2c_msg msg[2] = {
576 {
577 .addr = client->addr,
578 .len = 1,
579 .buf = msgbuf0
580 },
581 {
582 .addr = client->addr,
583 .flags = I2C_M_RD,
584 .len = len,
585 .buf = msgbuf1
586 },
587 };
54450f59
HV
588
589 if (i2c_transfer(client->adapter, msg, 2) < 0)
590 return -EIO;
591 memcpy(val, msgbuf1, len);
592 return 0;
593}
594
54450f59
HV
595static inline int edid_write_block(struct v4l2_subdev *sd,
596 unsigned len, const u8 *val)
597{
54450f59
HV
598 struct adv7604_state *state = to_state(sd);
599 int err = 0;
600 int i;
601
602 v4l2_dbg(2, debug, sd, "%s: write EDID block (%d byte)\n", __func__, len);
603
54450f59
HV
604 for (i = 0; !err && i < len; i += I2C_SMBUS_BLOCK_MAX)
605 err = adv_smbus_write_i2c_block_data(state->i2c_edid, i,
606 I2C_SMBUS_BLOCK_MAX, val + i);
dd08beb9
MR
607 return err;
608}
54450f59 609
dd08beb9
MR
610static void adv7604_delayed_work_enable_hotplug(struct work_struct *work)
611{
612 struct delayed_work *dwork = to_delayed_work(work);
613 struct adv7604_state *state = container_of(dwork, struct adv7604_state,
614 delayed_work_enable_hotplug);
615 struct v4l2_subdev *sd = &state->sd;
54450f59 616
dd08beb9 617 v4l2_dbg(2, debug, sd, "%s: enable hotplug\n", __func__);
54450f59 618
dd08beb9 619 v4l2_subdev_notify(sd, ADV7604_HOTPLUG, (void *)&state->edid.present);
54450f59
HV
620}
621
622static inline int hdmi_read(struct v4l2_subdev *sd, u8 reg)
623{
624 struct adv7604_state *state = to_state(sd);
625
626 return adv_smbus_read_byte_data(state->i2c_hdmi, reg);
627}
628
51182a94
LP
629static u16 hdmi_read16(struct v4l2_subdev *sd, u8 reg, u16 mask)
630{
631 return ((hdmi_read(sd, reg) << 8) | hdmi_read(sd, reg + 1)) & mask;
632}
633
54450f59
HV
634static inline int hdmi_write(struct v4l2_subdev *sd, u8 reg, u8 val)
635{
636 struct adv7604_state *state = to_state(sd);
637
638 return adv_smbus_write_byte_data(state->i2c_hdmi, reg, val);
639}
640
4a31a93a
MR
641static inline int hdmi_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
642{
643 return hdmi_write(sd, reg, (hdmi_read(sd, reg) & mask) | val);
644}
645
54450f59
HV
646static inline int test_read(struct v4l2_subdev *sd, u8 reg)
647{
648 struct adv7604_state *state = to_state(sd);
649
650 return adv_smbus_read_byte_data(state->i2c_test, reg);
651}
652
653static inline int test_write(struct v4l2_subdev *sd, u8 reg, u8 val)
654{
655 struct adv7604_state *state = to_state(sd);
656
657 return adv_smbus_write_byte_data(state->i2c_test, reg, val);
658}
659
660static inline int cp_read(struct v4l2_subdev *sd, u8 reg)
661{
662 struct adv7604_state *state = to_state(sd);
663
664 return adv_smbus_read_byte_data(state->i2c_cp, reg);
665}
666
51182a94
LP
667static u16 cp_read16(struct v4l2_subdev *sd, u8 reg, u16 mask)
668{
669 return ((cp_read(sd, reg) << 8) | cp_read(sd, reg + 1)) & mask;
670}
671
54450f59
HV
672static inline int cp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
673{
674 struct adv7604_state *state = to_state(sd);
675
676 return adv_smbus_write_byte_data(state->i2c_cp, reg, val);
677}
678
679static inline int cp_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
680{
681 return cp_write(sd, reg, (cp_read(sd, reg) & mask) | val);
682}
683
684static inline int vdp_read(struct v4l2_subdev *sd, u8 reg)
685{
686 struct adv7604_state *state = to_state(sd);
687
688 return adv_smbus_read_byte_data(state->i2c_vdp, reg);
689}
690
691static inline int vdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
692{
693 struct adv7604_state *state = to_state(sd);
694
695 return adv_smbus_write_byte_data(state->i2c_vdp, reg, val);
696}
697
d42010a1
LPC
698enum {
699 ADV7604_PAGE_IO,
700 ADV7604_PAGE_AVLINK,
701 ADV7604_PAGE_CEC,
702 ADV7604_PAGE_INFOFRAME,
703 ADV7604_PAGE_ESDP,
704 ADV7604_PAGE_DPP,
705 ADV7604_PAGE_AFE,
706 ADV7604_PAGE_REP,
707 ADV7604_PAGE_EDID,
708 ADV7604_PAGE_HDMI,
709 ADV7604_PAGE_TEST,
710 ADV7604_PAGE_CP,
711 ADV7604_PAGE_VDP,
712 ADV7604_PAGE_TERM,
713};
714
715#define ADV7604_REG(page, offset) (((page) << 8) | (offset))
716#define ADV7604_REG_SEQ_TERM 0xffff
717
718#ifdef CONFIG_VIDEO_ADV_DEBUG
719static int adv7604_read_reg(struct v4l2_subdev *sd, unsigned int reg)
720{
721 struct adv7604_state *state = to_state(sd);
722 unsigned int page = reg >> 8;
723
724 if (!(BIT(page) & state->info->page_mask))
725 return -EINVAL;
726
727 reg &= 0xff;
728
729 switch (page) {
730 case ADV7604_PAGE_IO:
731 return io_read(sd, reg);
732 case ADV7604_PAGE_AVLINK:
733 return avlink_read(sd, reg);
734 case ADV7604_PAGE_CEC:
735 return cec_read(sd, reg);
736 case ADV7604_PAGE_INFOFRAME:
737 return infoframe_read(sd, reg);
738 case ADV7604_PAGE_ESDP:
739 return esdp_read(sd, reg);
740 case ADV7604_PAGE_DPP:
741 return dpp_read(sd, reg);
742 case ADV7604_PAGE_AFE:
743 return afe_read(sd, reg);
744 case ADV7604_PAGE_REP:
745 return rep_read(sd, reg);
746 case ADV7604_PAGE_EDID:
747 return edid_read(sd, reg);
748 case ADV7604_PAGE_HDMI:
749 return hdmi_read(sd, reg);
750 case ADV7604_PAGE_TEST:
751 return test_read(sd, reg);
752 case ADV7604_PAGE_CP:
753 return cp_read(sd, reg);
754 case ADV7604_PAGE_VDP:
755 return vdp_read(sd, reg);
756 }
757
758 return -EINVAL;
759}
760#endif
761
762static int adv7604_write_reg(struct v4l2_subdev *sd, unsigned int reg, u8 val)
763{
764 struct adv7604_state *state = to_state(sd);
765 unsigned int page = reg >> 8;
766
767 if (!(BIT(page) & state->info->page_mask))
768 return -EINVAL;
769
770 reg &= 0xff;
771
772 switch (page) {
773 case ADV7604_PAGE_IO:
774 return io_write(sd, reg, val);
775 case ADV7604_PAGE_AVLINK:
776 return avlink_write(sd, reg, val);
777 case ADV7604_PAGE_CEC:
778 return cec_write(sd, reg, val);
779 case ADV7604_PAGE_INFOFRAME:
780 return infoframe_write(sd, reg, val);
781 case ADV7604_PAGE_ESDP:
782 return esdp_write(sd, reg, val);
783 case ADV7604_PAGE_DPP:
784 return dpp_write(sd, reg, val);
785 case ADV7604_PAGE_AFE:
786 return afe_write(sd, reg, val);
787 case ADV7604_PAGE_REP:
788 return rep_write(sd, reg, val);
789 case ADV7604_PAGE_EDID:
790 return edid_write(sd, reg, val);
791 case ADV7604_PAGE_HDMI:
792 return hdmi_write(sd, reg, val);
793 case ADV7604_PAGE_TEST:
794 return test_write(sd, reg, val);
795 case ADV7604_PAGE_CP:
796 return cp_write(sd, reg, val);
797 case ADV7604_PAGE_VDP:
798 return vdp_write(sd, reg, val);
799 }
800
801 return -EINVAL;
802}
803
804static void adv7604_write_reg_seq(struct v4l2_subdev *sd,
805 const struct adv7604_reg_seq *reg_seq)
806{
807 unsigned int i;
808
809 for (i = 0; reg_seq[i].reg != ADV7604_REG_SEQ_TERM; i++)
810 adv7604_write_reg(sd, reg_seq[i].reg, reg_seq[i].val);
811}
812
539b33b0
LP
813/* -----------------------------------------------------------------------------
814 * Format helpers
815 */
816
817static const struct adv7604_format_info adv7604_formats[] = {
818 { V4L2_MBUS_FMT_RGB888_1X24, ADV7604_OP_CH_SEL_RGB, true, false,
819 ADV7604_OP_MODE_SEL_SDR_444 | ADV7604_OP_FORMAT_SEL_8BIT },
820 { V4L2_MBUS_FMT_YUYV8_2X8, ADV7604_OP_CH_SEL_RGB, false, false,
821 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_8BIT },
822 { V4L2_MBUS_FMT_YVYU8_2X8, ADV7604_OP_CH_SEL_RGB, false, true,
823 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_8BIT },
824 { V4L2_MBUS_FMT_YUYV10_2X10, ADV7604_OP_CH_SEL_RGB, false, false,
825 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_10BIT },
826 { V4L2_MBUS_FMT_YVYU10_2X10, ADV7604_OP_CH_SEL_RGB, false, true,
827 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_10BIT },
828 { V4L2_MBUS_FMT_YUYV12_2X12, ADV7604_OP_CH_SEL_RGB, false, false,
829 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_12BIT },
830 { V4L2_MBUS_FMT_YVYU12_2X12, ADV7604_OP_CH_SEL_RGB, false, true,
831 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_12BIT },
832 { V4L2_MBUS_FMT_UYVY8_1X16, ADV7604_OP_CH_SEL_RBG, false, false,
833 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
834 { V4L2_MBUS_FMT_VYUY8_1X16, ADV7604_OP_CH_SEL_RBG, false, true,
835 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
836 { V4L2_MBUS_FMT_YUYV8_1X16, ADV7604_OP_CH_SEL_RGB, false, false,
837 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
838 { V4L2_MBUS_FMT_YVYU8_1X16, ADV7604_OP_CH_SEL_RGB, false, true,
839 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
840 { V4L2_MBUS_FMT_UYVY10_1X20, ADV7604_OP_CH_SEL_RBG, false, false,
841 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_10BIT },
842 { V4L2_MBUS_FMT_VYUY10_1X20, ADV7604_OP_CH_SEL_RBG, false, true,
843 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_10BIT },
844 { V4L2_MBUS_FMT_YUYV10_1X20, ADV7604_OP_CH_SEL_RGB, false, false,
845 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_10BIT },
846 { V4L2_MBUS_FMT_YVYU10_1X20, ADV7604_OP_CH_SEL_RGB, false, true,
847 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_10BIT },
848 { V4L2_MBUS_FMT_UYVY12_1X24, ADV7604_OP_CH_SEL_RBG, false, false,
849 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
850 { V4L2_MBUS_FMT_VYUY12_1X24, ADV7604_OP_CH_SEL_RBG, false, true,
851 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
852 { V4L2_MBUS_FMT_YUYV12_1X24, ADV7604_OP_CH_SEL_RGB, false, false,
853 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
854 { V4L2_MBUS_FMT_YVYU12_1X24, ADV7604_OP_CH_SEL_RGB, false, true,
855 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
856};
857
858static const struct adv7604_format_info adv7611_formats[] = {
859 { V4L2_MBUS_FMT_RGB888_1X24, ADV7604_OP_CH_SEL_RGB, true, false,
860 ADV7604_OP_MODE_SEL_SDR_444 | ADV7604_OP_FORMAT_SEL_8BIT },
861 { V4L2_MBUS_FMT_YUYV8_2X8, ADV7604_OP_CH_SEL_RGB, false, false,
862 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_8BIT },
863 { V4L2_MBUS_FMT_YVYU8_2X8, ADV7604_OP_CH_SEL_RGB, false, true,
864 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_8BIT },
865 { V4L2_MBUS_FMT_YUYV12_2X12, ADV7604_OP_CH_SEL_RGB, false, false,
866 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_12BIT },
867 { V4L2_MBUS_FMT_YVYU12_2X12, ADV7604_OP_CH_SEL_RGB, false, true,
868 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_12BIT },
869 { V4L2_MBUS_FMT_UYVY8_1X16, ADV7604_OP_CH_SEL_RBG, false, false,
870 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
871 { V4L2_MBUS_FMT_VYUY8_1X16, ADV7604_OP_CH_SEL_RBG, false, true,
872 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
873 { V4L2_MBUS_FMT_YUYV8_1X16, ADV7604_OP_CH_SEL_RGB, false, false,
874 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
875 { V4L2_MBUS_FMT_YVYU8_1X16, ADV7604_OP_CH_SEL_RGB, false, true,
876 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
877 { V4L2_MBUS_FMT_UYVY12_1X24, ADV7604_OP_CH_SEL_RBG, false, false,
878 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
879 { V4L2_MBUS_FMT_VYUY12_1X24, ADV7604_OP_CH_SEL_RBG, false, true,
880 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
881 { V4L2_MBUS_FMT_YUYV12_1X24, ADV7604_OP_CH_SEL_RGB, false, false,
882 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
883 { V4L2_MBUS_FMT_YVYU12_1X24, ADV7604_OP_CH_SEL_RGB, false, true,
884 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
885};
886
887static const struct adv7604_format_info *
888adv7604_format_info(struct adv7604_state *state, enum v4l2_mbus_pixelcode code)
889{
890 unsigned int i;
891
892 for (i = 0; i < state->info->nformats; ++i) {
893 if (state->info->formats[i].code == code)
894 return &state->info->formats[i];
895 }
896
897 return NULL;
898}
899
54450f59
HV
900/* ----------------------------------------------------------------------- */
901
4a31a93a
MR
902static inline bool is_analog_input(struct v4l2_subdev *sd)
903{
904 struct adv7604_state *state = to_state(sd);
905
c784b1e2
LP
906 return state->selected_input == ADV7604_PAD_VGA_RGB ||
907 state->selected_input == ADV7604_PAD_VGA_COMP;
4a31a93a
MR
908}
909
910static inline bool is_digital_input(struct v4l2_subdev *sd)
911{
912 struct adv7604_state *state = to_state(sd);
913
c784b1e2
LP
914 return state->selected_input == ADV7604_PAD_HDMI_PORT_A ||
915 state->selected_input == ADV7604_PAD_HDMI_PORT_B ||
916 state->selected_input == ADV7604_PAD_HDMI_PORT_C ||
917 state->selected_input == ADV7604_PAD_HDMI_PORT_D;
4a31a93a
MR
918}
919
920/* ----------------------------------------------------------------------- */
921
54450f59
HV
922#ifdef CONFIG_VIDEO_ADV_DEBUG
923static void adv7604_inv_register(struct v4l2_subdev *sd)
924{
925 v4l2_info(sd, "0x000-0x0ff: IO Map\n");
926 v4l2_info(sd, "0x100-0x1ff: AVLink Map\n");
927 v4l2_info(sd, "0x200-0x2ff: CEC Map\n");
928 v4l2_info(sd, "0x300-0x3ff: InfoFrame Map\n");
929 v4l2_info(sd, "0x400-0x4ff: ESDP Map\n");
930 v4l2_info(sd, "0x500-0x5ff: DPP Map\n");
931 v4l2_info(sd, "0x600-0x6ff: AFE Map\n");
932 v4l2_info(sd, "0x700-0x7ff: Repeater Map\n");
933 v4l2_info(sd, "0x800-0x8ff: EDID Map\n");
934 v4l2_info(sd, "0x900-0x9ff: HDMI Map\n");
935 v4l2_info(sd, "0xa00-0xaff: Test Map\n");
936 v4l2_info(sd, "0xb00-0xbff: CP Map\n");
937 v4l2_info(sd, "0xc00-0xcff: VDP Map\n");
938}
939
940static int adv7604_g_register(struct v4l2_subdev *sd,
941 struct v4l2_dbg_register *reg)
942{
d42010a1
LPC
943 int ret;
944
945 ret = adv7604_read_reg(sd, reg->reg);
946 if (ret < 0) {
54450f59
HV
947 v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
948 adv7604_inv_register(sd);
d42010a1 949 return ret;
54450f59 950 }
d42010a1
LPC
951
952 reg->size = 1;
953 reg->val = ret;
954
54450f59
HV
955 return 0;
956}
957
958static int adv7604_s_register(struct v4l2_subdev *sd,
977ba3b1 959 const struct v4l2_dbg_register *reg)
54450f59 960{
d42010a1 961 int ret;
1577461b 962
d42010a1
LPC
963 ret = adv7604_write_reg(sd, reg->reg, reg->val);
964 if (ret < 0) {
54450f59
HV
965 v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
966 adv7604_inv_register(sd);
d42010a1 967 return ret;
54450f59 968 }
d42010a1 969
54450f59
HV
970 return 0;
971}
972#endif
973
d42010a1
LPC
974static unsigned int adv7604_read_cable_det(struct v4l2_subdev *sd)
975{
976 u8 value = io_read(sd, 0x6f);
977
978 return ((value & 0x10) >> 4)
979 | ((value & 0x08) >> 2)
980 | ((value & 0x04) << 0)
981 | ((value & 0x02) << 2);
982}
983
984static unsigned int adv7611_read_cable_det(struct v4l2_subdev *sd)
985{
986 u8 value = io_read(sd, 0x6f);
987
988 return value & 1;
989}
990
54450f59
HV
991static int adv7604_s_detect_tx_5v_ctrl(struct v4l2_subdev *sd)
992{
993 struct adv7604_state *state = to_state(sd);
d42010a1 994 const struct adv7604_chip_info *info = state->info;
54450f59 995
54450f59 996 return v4l2_ctrl_s_ctrl(state->detect_tx_5v_ctrl,
d42010a1 997 info->read_cable_det(sd));
54450f59
HV
998}
999
ccbd5bc4
HV
1000static int find_and_set_predefined_video_timings(struct v4l2_subdev *sd,
1001 u8 prim_mode,
1002 const struct adv7604_video_standards *predef_vid_timings,
1003 const struct v4l2_dv_timings *timings)
1004{
ccbd5bc4
HV
1005 int i;
1006
1007 for (i = 0; predef_vid_timings[i].timings.bt.width; i++) {
ef1ed8f5 1008 if (!v4l2_match_dv_timings(timings, &predef_vid_timings[i].timings,
4a31a93a 1009 is_digital_input(sd) ? 250000 : 1000000))
ccbd5bc4
HV
1010 continue;
1011 io_write(sd, 0x00, predef_vid_timings[i].vid_std); /* video std */
1012 io_write(sd, 0x01, (predef_vid_timings[i].v_freq << 4) +
1013 prim_mode); /* v_freq and prim mode */
1014 return 0;
1015 }
1016
1017 return -1;
1018}
1019
1020static int configure_predefined_video_timings(struct v4l2_subdev *sd,
1021 struct v4l2_dv_timings *timings)
54450f59 1022{
ccbd5bc4
HV
1023 struct adv7604_state *state = to_state(sd);
1024 int err;
1025
1026 v4l2_dbg(1, debug, sd, "%s", __func__);
1027
d42010a1
LPC
1028 if (adv7604_has_afe(state)) {
1029 /* reset to default values */
1030 io_write(sd, 0x16, 0x43);
1031 io_write(sd, 0x17, 0x5a);
1032 }
ccbd5bc4
HV
1033 /* disable embedded syncs for auto graphics mode */
1034 cp_write_and_or(sd, 0x81, 0xef, 0x00);
1035 cp_write(sd, 0x8f, 0x00);
1036 cp_write(sd, 0x90, 0x00);
1037 cp_write(sd, 0xa2, 0x00);
1038 cp_write(sd, 0xa3, 0x00);
1039 cp_write(sd, 0xa4, 0x00);
1040 cp_write(sd, 0xa5, 0x00);
1041 cp_write(sd, 0xa6, 0x00);
1042 cp_write(sd, 0xa7, 0x00);
1043 cp_write(sd, 0xab, 0x00);
1044 cp_write(sd, 0xac, 0x00);
1045
4a31a93a 1046 if (is_analog_input(sd)) {
ccbd5bc4
HV
1047 err = find_and_set_predefined_video_timings(sd,
1048 0x01, adv7604_prim_mode_comp, timings);
1049 if (err)
1050 err = find_and_set_predefined_video_timings(sd,
1051 0x02, adv7604_prim_mode_gr, timings);
4a31a93a 1052 } else if (is_digital_input(sd)) {
ccbd5bc4
HV
1053 err = find_and_set_predefined_video_timings(sd,
1054 0x05, adv7604_prim_mode_hdmi_comp, timings);
1055 if (err)
1056 err = find_and_set_predefined_video_timings(sd,
1057 0x06, adv7604_prim_mode_hdmi_gr, timings);
4a31a93a
MR
1058 } else {
1059 v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
1060 __func__, state->selected_input);
ccbd5bc4 1061 err = -1;
ccbd5bc4
HV
1062 }
1063
1064
1065 return err;
1066}
1067
1068static void configure_custom_video_timings(struct v4l2_subdev *sd,
1069 const struct v4l2_bt_timings *bt)
1070{
1071 struct adv7604_state *state = to_state(sd);
54450f59 1072 struct i2c_client *client = v4l2_get_subdevdata(sd);
ccbd5bc4
HV
1073 u32 width = htotal(bt);
1074 u32 height = vtotal(bt);
1075 u16 cp_start_sav = bt->hsync + bt->hbackporch - 4;
1076 u16 cp_start_eav = width - bt->hfrontporch;
1077 u16 cp_start_vbi = height - bt->vfrontporch;
1078 u16 cp_end_vbi = bt->vsync + bt->vbackporch;
1079 u16 ch1_fr_ll = (((u32)bt->pixelclock / 100) > 0) ?
1080 ((width * (ADV7604_fsc / 100)) / ((u32)bt->pixelclock / 100)) : 0;
1081 const u8 pll[2] = {
1082 0xc0 | ((width >> 8) & 0x1f),
1083 width & 0xff
1084 };
54450f59
HV
1085
1086 v4l2_dbg(2, debug, sd, "%s\n", __func__);
1087
4a31a93a 1088 if (is_analog_input(sd)) {
ccbd5bc4
HV
1089 /* auto graphics */
1090 io_write(sd, 0x00, 0x07); /* video std */
1091 io_write(sd, 0x01, 0x02); /* prim mode */
1092 /* enable embedded syncs for auto graphics mode */
1093 cp_write_and_or(sd, 0x81, 0xef, 0x10);
54450f59 1094
ccbd5bc4 1095 /* Should only be set in auto-graphics mode [REF_02, p. 91-92] */
54450f59
HV
1096 /* setup PLL_DIV_MAN_EN and PLL_DIV_RATIO */
1097 /* IO-map reg. 0x16 and 0x17 should be written in sequence */
4a31a93a 1098 if (adv_smbus_write_i2c_block_data(client, 0x16, 2, pll))
54450f59 1099 v4l2_err(sd, "writing to reg 0x16 and 0x17 failed\n");
54450f59
HV
1100
1101 /* active video - horizontal timing */
54450f59 1102 cp_write(sd, 0xa2, (cp_start_sav >> 4) & 0xff);
ccbd5bc4 1103 cp_write(sd, 0xa3, ((cp_start_sav & 0x0f) << 4) |
4a31a93a 1104 ((cp_start_eav >> 8) & 0x0f));
54450f59
HV
1105 cp_write(sd, 0xa4, cp_start_eav & 0xff);
1106
1107 /* active video - vertical timing */
54450f59 1108 cp_write(sd, 0xa5, (cp_start_vbi >> 4) & 0xff);
ccbd5bc4 1109 cp_write(sd, 0xa6, ((cp_start_vbi & 0xf) << 4) |
4a31a93a 1110 ((cp_end_vbi >> 8) & 0xf));
54450f59 1111 cp_write(sd, 0xa7, cp_end_vbi & 0xff);
4a31a93a 1112 } else if (is_digital_input(sd)) {
ccbd5bc4 1113 /* set default prim_mode/vid_std for HDMI
39c1cb2b 1114 according to [REF_03, c. 4.2] */
ccbd5bc4
HV
1115 io_write(sd, 0x00, 0x02); /* video std */
1116 io_write(sd, 0x01, 0x06); /* prim mode */
4a31a93a
MR
1117 } else {
1118 v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
1119 __func__, state->selected_input);
54450f59 1120 }
54450f59 1121
ccbd5bc4
HV
1122 cp_write(sd, 0x8f, (ch1_fr_ll >> 8) & 0x7);
1123 cp_write(sd, 0x90, ch1_fr_ll & 0xff);
1124 cp_write(sd, 0xab, (height >> 4) & 0xff);
1125 cp_write(sd, 0xac, (height & 0x0f) << 4);
1126}
54450f59 1127
5c6c6349
MR
1128static void adv7604_set_offset(struct v4l2_subdev *sd, bool auto_offset, u16 offset_a, u16 offset_b, u16 offset_c)
1129{
1130 struct adv7604_state *state = to_state(sd);
1131 u8 offset_buf[4];
1132
1133 if (auto_offset) {
1134 offset_a = 0x3ff;
1135 offset_b = 0x3ff;
1136 offset_c = 0x3ff;
1137 }
1138
1139 v4l2_dbg(2, debug, sd, "%s: %s offset: a = 0x%x, b = 0x%x, c = 0x%x\n",
1140 __func__, auto_offset ? "Auto" : "Manual",
1141 offset_a, offset_b, offset_c);
1142
1143 offset_buf[0] = (cp_read(sd, 0x77) & 0xc0) | ((offset_a & 0x3f0) >> 4);
1144 offset_buf[1] = ((offset_a & 0x00f) << 4) | ((offset_b & 0x3c0) >> 6);
1145 offset_buf[2] = ((offset_b & 0x03f) << 2) | ((offset_c & 0x300) >> 8);
1146 offset_buf[3] = offset_c & 0x0ff;
1147
1148 /* Registers must be written in this order with no i2c access in between */
1149 if (adv_smbus_write_i2c_block_data(state->i2c_cp, 0x77, 4, offset_buf))
1150 v4l2_err(sd, "%s: i2c error writing to CP reg 0x77, 0x78, 0x79, 0x7a\n", __func__);
1151}
1152
1153static void adv7604_set_gain(struct v4l2_subdev *sd, bool auto_gain, u16 gain_a, u16 gain_b, u16 gain_c)
1154{
1155 struct adv7604_state *state = to_state(sd);
1156 u8 gain_buf[4];
1157 u8 gain_man = 1;
1158 u8 agc_mode_man = 1;
1159
1160 if (auto_gain) {
1161 gain_man = 0;
1162 agc_mode_man = 0;
1163 gain_a = 0x100;
1164 gain_b = 0x100;
1165 gain_c = 0x100;
1166 }
1167
1168 v4l2_dbg(2, debug, sd, "%s: %s gain: a = 0x%x, b = 0x%x, c = 0x%x\n",
1169 __func__, auto_gain ? "Auto" : "Manual",
1170 gain_a, gain_b, gain_c);
1171
1172 gain_buf[0] = ((gain_man << 7) | (agc_mode_man << 6) | ((gain_a & 0x3f0) >> 4));
1173 gain_buf[1] = (((gain_a & 0x00f) << 4) | ((gain_b & 0x3c0) >> 6));
1174 gain_buf[2] = (((gain_b & 0x03f) << 2) | ((gain_c & 0x300) >> 8));
1175 gain_buf[3] = ((gain_c & 0x0ff));
1176
1177 /* Registers must be written in this order with no i2c access in between */
1178 if (adv_smbus_write_i2c_block_data(state->i2c_cp, 0x73, 4, gain_buf))
1179 v4l2_err(sd, "%s: i2c error writing to CP reg 0x73, 0x74, 0x75, 0x76\n", __func__);
1180}
1181
54450f59
HV
1182static void set_rgb_quantization_range(struct v4l2_subdev *sd)
1183{
1184 struct adv7604_state *state = to_state(sd);
5c6c6349
MR
1185 bool rgb_output = io_read(sd, 0x02) & 0x02;
1186 bool hdmi_signal = hdmi_read(sd, 0x05) & 0x80;
1187
1188 v4l2_dbg(2, debug, sd, "%s: RGB quantization range: %d, RGB out: %d, HDMI: %d\n",
1189 __func__, state->rgb_quantization_range,
1190 rgb_output, hdmi_signal);
54450f59 1191
5c6c6349
MR
1192 adv7604_set_gain(sd, true, 0x0, 0x0, 0x0);
1193 adv7604_set_offset(sd, true, 0x0, 0x0, 0x0);
9833239e 1194
54450f59
HV
1195 switch (state->rgb_quantization_range) {
1196 case V4L2_DV_RGB_RANGE_AUTO:
c784b1e2 1197 if (state->selected_input == ADV7604_PAD_VGA_RGB) {
9833239e
MR
1198 /* Receiving analog RGB signal
1199 * Set RGB full range (0-255) */
1200 io_write_and_or(sd, 0x02, 0x0f, 0x10);
1201 break;
1202 }
1203
c784b1e2 1204 if (state->selected_input == ADV7604_PAD_VGA_COMP) {
9833239e
MR
1205 /* Receiving analog YPbPr signal
1206 * Set automode */
1207 io_write_and_or(sd, 0x02, 0x0f, 0xf0);
1208 break;
1209 }
1210
5c6c6349 1211 if (hdmi_signal) {
9833239e
MR
1212 /* Receiving HDMI signal
1213 * Set automode */
6b0d5d34 1214 io_write_and_or(sd, 0x02, 0x0f, 0xf0);
9833239e
MR
1215 break;
1216 }
1217
1218 /* Receiving DVI-D signal
1219 * ADV7604 selects RGB limited range regardless of
1220 * input format (CE/IT) in automatic mode */
1221 if (state->timings.bt.standards & V4L2_DV_BT_STD_CEA861) {
1222 /* RGB limited range (16-235) */
1223 io_write_and_or(sd, 0x02, 0x0f, 0x00);
1224 } else {
1225 /* RGB full range (0-255) */
1226 io_write_and_or(sd, 0x02, 0x0f, 0x10);
5c6c6349
MR
1227
1228 if (is_digital_input(sd) && rgb_output) {
1229 adv7604_set_offset(sd, false, 0x40, 0x40, 0x40);
1230 } else {
1231 adv7604_set_gain(sd, false, 0xe0, 0xe0, 0xe0);
1232 adv7604_set_offset(sd, false, 0x70, 0x70, 0x70);
1233 }
54450f59
HV
1234 }
1235 break;
1236 case V4L2_DV_RGB_RANGE_LIMITED:
c784b1e2 1237 if (state->selected_input == ADV7604_PAD_VGA_COMP) {
d261e842
MR
1238 /* YCrCb limited range (16-235) */
1239 io_write_and_or(sd, 0x02, 0x0f, 0x20);
5c6c6349 1240 break;
d261e842 1241 }
5c6c6349
MR
1242
1243 /* RGB limited range (16-235) */
1244 io_write_and_or(sd, 0x02, 0x0f, 0x00);
1245
54450f59
HV
1246 break;
1247 case V4L2_DV_RGB_RANGE_FULL:
c784b1e2 1248 if (state->selected_input == ADV7604_PAD_VGA_COMP) {
d261e842
MR
1249 /* YCrCb full range (0-255) */
1250 io_write_and_or(sd, 0x02, 0x0f, 0x60);
5c6c6349
MR
1251 break;
1252 }
1253
1254 /* RGB full range (0-255) */
1255 io_write_and_or(sd, 0x02, 0x0f, 0x10);
1256
1257 if (is_analog_input(sd) || hdmi_signal)
1258 break;
1259
1260 /* Adjust gain/offset for DVI-D signals only */
1261 if (rgb_output) {
1262 adv7604_set_offset(sd, false, 0x40, 0x40, 0x40);
d261e842 1263 } else {
5c6c6349
MR
1264 adv7604_set_gain(sd, false, 0xe0, 0xe0, 0xe0);
1265 adv7604_set_offset(sd, false, 0x70, 0x70, 0x70);
d261e842 1266 }
54450f59
HV
1267 break;
1268 }
1269}
1270
54450f59
HV
1271static int adv7604_s_ctrl(struct v4l2_ctrl *ctrl)
1272{
1273 struct v4l2_subdev *sd = to_sd(ctrl);
1274 struct adv7604_state *state = to_state(sd);
1275
1276 switch (ctrl->id) {
1277 case V4L2_CID_BRIGHTNESS:
1278 cp_write(sd, 0x3c, ctrl->val);
1279 return 0;
1280 case V4L2_CID_CONTRAST:
1281 cp_write(sd, 0x3a, ctrl->val);
1282 return 0;
1283 case V4L2_CID_SATURATION:
1284 cp_write(sd, 0x3b, ctrl->val);
1285 return 0;
1286 case V4L2_CID_HUE:
1287 cp_write(sd, 0x3d, ctrl->val);
1288 return 0;
1289 case V4L2_CID_DV_RX_RGB_RANGE:
1290 state->rgb_quantization_range = ctrl->val;
1291 set_rgb_quantization_range(sd);
1292 return 0;
1293 case V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE:
d42010a1
LPC
1294 if (!adv7604_has_afe(state))
1295 return -EINVAL;
54450f59
HV
1296 /* Set the analog sampling phase. This is needed to find the
1297 best sampling phase for analog video: an application or
1298 driver has to try a number of phases and analyze the picture
1299 quality before settling on the best performing phase. */
1300 afe_write(sd, 0xc8, ctrl->val);
1301 return 0;
1302 case V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL:
1303 /* Use the default blue color for free running mode,
1304 or supply your own. */
1305 cp_write_and_or(sd, 0xbf, ~0x04, (ctrl->val << 2));
1306 return 0;
1307 case V4L2_CID_ADV_RX_FREE_RUN_COLOR:
1308 cp_write(sd, 0xc0, (ctrl->val & 0xff0000) >> 16);
1309 cp_write(sd, 0xc1, (ctrl->val & 0x00ff00) >> 8);
1310 cp_write(sd, 0xc2, (u8)(ctrl->val & 0x0000ff));
1311 return 0;
1312 }
1313 return -EINVAL;
1314}
1315
54450f59
HV
1316/* ----------------------------------------------------------------------- */
1317
1318static inline bool no_power(struct v4l2_subdev *sd)
1319{
1320 /* Entire chip or CP powered off */
1321 return io_read(sd, 0x0c) & 0x24;
1322}
1323
1324static inline bool no_signal_tmds(struct v4l2_subdev *sd)
1325{
4a31a93a
MR
1326 struct adv7604_state *state = to_state(sd);
1327
1328 return !(io_read(sd, 0x6a) & (0x10 >> state->selected_input));
54450f59
HV
1329}
1330
1331static inline bool no_lock_tmds(struct v4l2_subdev *sd)
1332{
d42010a1
LPC
1333 struct adv7604_state *state = to_state(sd);
1334 const struct adv7604_chip_info *info = state->info;
1335
1336 return (io_read(sd, 0x6a) & info->tdms_lock_mask) != info->tdms_lock_mask;
54450f59
HV
1337}
1338
bb88f325
MB
1339static inline bool is_hdmi(struct v4l2_subdev *sd)
1340{
1341 return hdmi_read(sd, 0x05) & 0x80;
1342}
1343
54450f59
HV
1344static inline bool no_lock_sspd(struct v4l2_subdev *sd)
1345{
d42010a1
LPC
1346 struct adv7604_state *state = to_state(sd);
1347
1348 /*
1349 * Chips without a AFE don't expose registers for the SSPD, so just assume
1350 * that we have a lock.
1351 */
1352 if (adv7604_has_afe(state))
1353 return false;
1354
54450f59
HV
1355 /* TODO channel 2 */
1356 return ((cp_read(sd, 0xb5) & 0xd0) != 0xd0);
1357}
1358
1359static inline bool no_lock_stdi(struct v4l2_subdev *sd)
1360{
1361 /* TODO channel 2 */
1362 return !(cp_read(sd, 0xb1) & 0x80);
1363}
1364
1365static inline bool no_signal(struct v4l2_subdev *sd)
1366{
54450f59
HV
1367 bool ret;
1368
1369 ret = no_power(sd);
1370
1371 ret |= no_lock_stdi(sd);
1372 ret |= no_lock_sspd(sd);
1373
4a31a93a 1374 if (is_digital_input(sd)) {
54450f59
HV
1375 ret |= no_lock_tmds(sd);
1376 ret |= no_signal_tmds(sd);
1377 }
1378
1379 return ret;
1380}
1381
1382static inline bool no_lock_cp(struct v4l2_subdev *sd)
1383{
d42010a1
LPC
1384 struct adv7604_state *state = to_state(sd);
1385
1386 if (!adv7604_has_afe(state))
1387 return false;
1388
54450f59
HV
1389 /* CP has detected a non standard number of lines on the incoming
1390 video compared to what it is configured to receive by s_dv_timings */
1391 return io_read(sd, 0x12) & 0x01;
1392}
1393
1394static int adv7604_g_input_status(struct v4l2_subdev *sd, u32 *status)
1395{
54450f59
HV
1396 *status = 0;
1397 *status |= no_power(sd) ? V4L2_IN_ST_NO_POWER : 0;
1398 *status |= no_signal(sd) ? V4L2_IN_ST_NO_SIGNAL : 0;
1399 if (no_lock_cp(sd))
4a31a93a 1400 *status |= is_digital_input(sd) ? V4L2_IN_ST_NO_SYNC : V4L2_IN_ST_NO_H_LOCK;
54450f59
HV
1401
1402 v4l2_dbg(1, debug, sd, "%s: status = 0x%x\n", __func__, *status);
1403
1404 return 0;
1405}
1406
1407/* ----------------------------------------------------------------------- */
1408
54450f59
HV
1409struct stdi_readback {
1410 u16 bl, lcf, lcvs;
1411 u8 hs_pol, vs_pol;
1412 bool interlaced;
1413};
1414
1415static int stdi2dv_timings(struct v4l2_subdev *sd,
1416 struct stdi_readback *stdi,
1417 struct v4l2_dv_timings *timings)
1418{
1419 struct adv7604_state *state = to_state(sd);
1420 u32 hfreq = (ADV7604_fsc * 8) / stdi->bl;
1421 u32 pix_clk;
1422 int i;
1423
1424 for (i = 0; adv7604_timings[i].bt.height; i++) {
1425 if (vtotal(&adv7604_timings[i].bt) != stdi->lcf + 1)
1426 continue;
1427 if (adv7604_timings[i].bt.vsync != stdi->lcvs)
1428 continue;
1429
1430 pix_clk = hfreq * htotal(&adv7604_timings[i].bt);
1431
1432 if ((pix_clk < adv7604_timings[i].bt.pixelclock + 1000000) &&
1433 (pix_clk > adv7604_timings[i].bt.pixelclock - 1000000)) {
1434 *timings = adv7604_timings[i];
1435 return 0;
1436 }
1437 }
1438
1439 if (v4l2_detect_cvt(stdi->lcf + 1, hfreq, stdi->lcvs,
1440 (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
1441 (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
1442 timings))
1443 return 0;
1444 if (v4l2_detect_gtf(stdi->lcf + 1, hfreq, stdi->lcvs,
1445 (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
1446 (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
1447 state->aspect_ratio, timings))
1448 return 0;
1449
ccbd5bc4
HV
1450 v4l2_dbg(2, debug, sd,
1451 "%s: No format candidate found for lcvs = %d, lcf=%d, bl = %d, %chsync, %cvsync\n",
1452 __func__, stdi->lcvs, stdi->lcf, stdi->bl,
1453 stdi->hs_pol, stdi->vs_pol);
54450f59
HV
1454 return -1;
1455}
1456
d42010a1 1457
54450f59
HV
1458static int read_stdi(struct v4l2_subdev *sd, struct stdi_readback *stdi)
1459{
d42010a1
LPC
1460 struct adv7604_state *state = to_state(sd);
1461 const struct adv7604_chip_info *info = state->info;
4a2ccdd2
LP
1462 u8 polarity;
1463
54450f59
HV
1464 if (no_lock_stdi(sd) || no_lock_sspd(sd)) {
1465 v4l2_dbg(2, debug, sd, "%s: STDI and/or SSPD not locked\n", __func__);
1466 return -1;
1467 }
1468
1469 /* read STDI */
51182a94 1470 stdi->bl = cp_read16(sd, 0xb1, 0x3fff);
d42010a1 1471 stdi->lcf = cp_read16(sd, info->lcf_reg, 0x7ff);
54450f59
HV
1472 stdi->lcvs = cp_read(sd, 0xb3) >> 3;
1473 stdi->interlaced = io_read(sd, 0x12) & 0x10;
1474
d42010a1
LPC
1475 if (adv7604_has_afe(state)) {
1476 /* read SSPD */
1477 polarity = cp_read(sd, 0xb5);
1478 if ((polarity & 0x03) == 0x01) {
1479 stdi->hs_pol = polarity & 0x10
1480 ? (polarity & 0x08 ? '+' : '-') : 'x';
1481 stdi->vs_pol = polarity & 0x40
1482 ? (polarity & 0x20 ? '+' : '-') : 'x';
1483 } else {
1484 stdi->hs_pol = 'x';
1485 stdi->vs_pol = 'x';
1486 }
54450f59 1487 } else {
d42010a1
LPC
1488 polarity = hdmi_read(sd, 0x05);
1489 stdi->hs_pol = polarity & 0x20 ? '+' : '-';
1490 stdi->vs_pol = polarity & 0x10 ? '+' : '-';
54450f59
HV
1491 }
1492
1493 if (no_lock_stdi(sd) || no_lock_sspd(sd)) {
1494 v4l2_dbg(2, debug, sd,
1495 "%s: signal lost during readout of STDI/SSPD\n", __func__);
1496 return -1;
1497 }
1498
1499 if (stdi->lcf < 239 || stdi->bl < 8 || stdi->bl == 0x3fff) {
1500 v4l2_dbg(2, debug, sd, "%s: invalid signal\n", __func__);
1501 memset(stdi, 0, sizeof(struct stdi_readback));
1502 return -1;
1503 }
1504
1505 v4l2_dbg(2, debug, sd,
1506 "%s: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, %chsync, %cvsync, %s\n",
1507 __func__, stdi->lcf, stdi->bl, stdi->lcvs,
1508 stdi->hs_pol, stdi->vs_pol,
1509 stdi->interlaced ? "interlaced" : "progressive");
1510
1511 return 0;
1512}
1513
1514static int adv7604_enum_dv_timings(struct v4l2_subdev *sd,
1515 struct v4l2_enum_dv_timings *timings)
1516{
1517 if (timings->index >= ARRAY_SIZE(adv7604_timings) - 1)
1518 return -EINVAL;
1519 memset(timings->reserved, 0, sizeof(timings->reserved));
1520 timings->timings = adv7604_timings[timings->index];
1521 return 0;
1522}
1523
1524static int adv7604_dv_timings_cap(struct v4l2_subdev *sd,
1525 struct v4l2_dv_timings_cap *cap)
1526{
54450f59
HV
1527 cap->type = V4L2_DV_BT_656_1120;
1528 cap->bt.max_width = 1920;
1529 cap->bt.max_height = 1200;
fe9c2564 1530 cap->bt.min_pixelclock = 25000000;
4a31a93a 1531 if (is_digital_input(sd))
54450f59
HV
1532 cap->bt.max_pixelclock = 225000000;
1533 else
1534 cap->bt.max_pixelclock = 170000000;
1535 cap->bt.standards = V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
1536 V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT;
1537 cap->bt.capabilities = V4L2_DV_BT_CAP_PROGRESSIVE |
1538 V4L2_DV_BT_CAP_REDUCED_BLANKING | V4L2_DV_BT_CAP_CUSTOM;
1539 return 0;
1540}
1541
1542/* Fill the optional fields .standards and .flags in struct v4l2_dv_timings
1543 if the format is listed in adv7604_timings[] */
1544static void adv7604_fill_optional_dv_timings_fields(struct v4l2_subdev *sd,
1545 struct v4l2_dv_timings *timings)
1546{
54450f59
HV
1547 int i;
1548
1549 for (i = 0; adv7604_timings[i].bt.width; i++) {
ef1ed8f5 1550 if (v4l2_match_dv_timings(timings, &adv7604_timings[i],
4a31a93a 1551 is_digital_input(sd) ? 250000 : 1000000)) {
54450f59
HV
1552 *timings = adv7604_timings[i];
1553 break;
1554 }
1555 }
1556}
1557
d42010a1
LPC
1558static unsigned int adv7604_read_hdmi_pixelclock(struct v4l2_subdev *sd)
1559{
1560 unsigned int freq;
1561 int a, b;
1562
1563 a = hdmi_read(sd, 0x06);
1564 b = hdmi_read(sd, 0x3b);
1565 if (a < 0 || b < 0)
1566 return 0;
1567 freq = a * 1000000 + ((b & 0x30) >> 4) * 250000;
1568
1569 if (is_hdmi(sd)) {
1570 /* adjust for deep color mode */
1571 unsigned bits_per_channel = ((hdmi_read(sd, 0x0b) & 0x60) >> 4) + 8;
1572
1573 freq = freq * 8 / bits_per_channel;
1574 }
1575
1576 return freq;
1577}
1578
1579static unsigned int adv7611_read_hdmi_pixelclock(struct v4l2_subdev *sd)
1580{
1581 int a, b;
1582
1583 a = hdmi_read(sd, 0x51);
1584 b = hdmi_read(sd, 0x52);
1585 if (a < 0 || b < 0)
1586 return 0;
1587 return ((a << 1) | (b >> 7)) * 1000000 + (b & 0x7f) * 1000000 / 128;
1588}
1589
54450f59
HV
1590static int adv7604_query_dv_timings(struct v4l2_subdev *sd,
1591 struct v4l2_dv_timings *timings)
1592{
1593 struct adv7604_state *state = to_state(sd);
d42010a1 1594 const struct adv7604_chip_info *info = state->info;
54450f59
HV
1595 struct v4l2_bt_timings *bt = &timings->bt;
1596 struct stdi_readback stdi;
1597
1598 if (!timings)
1599 return -EINVAL;
1600
1601 memset(timings, 0, sizeof(struct v4l2_dv_timings));
1602
1603 if (no_signal(sd)) {
1e0b9156 1604 state->restart_stdi_once = true;
54450f59
HV
1605 v4l2_dbg(1, debug, sd, "%s: no valid signal\n", __func__);
1606 return -ENOLINK;
1607 }
1608
1609 /* read STDI */
1610 if (read_stdi(sd, &stdi)) {
1611 v4l2_dbg(1, debug, sd, "%s: STDI/SSPD not locked\n", __func__);
1612 return -ENOLINK;
1613 }
1614 bt->interlaced = stdi.interlaced ?
1615 V4L2_DV_INTERLACED : V4L2_DV_PROGRESSIVE;
1616
4a31a93a 1617 if (is_digital_input(sd)) {
54450f59
HV
1618 timings->type = V4L2_DV_BT_656_1120;
1619
d42010a1 1620 /* FIXME: All masks are incorrect for ADV7611 */
51182a94
LP
1621 bt->width = hdmi_read16(sd, 0x07, 0xfff);
1622 bt->height = hdmi_read16(sd, 0x09, 0xfff);
d42010a1 1623 bt->pixelclock = info->read_hdmi_pixelclock(sd);
51182a94
LP
1624 bt->hfrontporch = hdmi_read16(sd, 0x20, 0x3ff);
1625 bt->hsync = hdmi_read16(sd, 0x22, 0x3ff);
1626 bt->hbackporch = hdmi_read16(sd, 0x24, 0x3ff);
1627 bt->vfrontporch = hdmi_read16(sd, 0x2a, 0x1fff) / 2;
1628 bt->vsync = hdmi_read16(sd, 0x2e, 0x1fff) / 2;
1629 bt->vbackporch = hdmi_read16(sd, 0x32, 0x1fff) / 2;
54450f59
HV
1630 bt->polarities = ((hdmi_read(sd, 0x05) & 0x10) ? V4L2_DV_VSYNC_POS_POL : 0) |
1631 ((hdmi_read(sd, 0x05) & 0x20) ? V4L2_DV_HSYNC_POS_POL : 0);
1632 if (bt->interlaced == V4L2_DV_INTERLACED) {
51182a94
LP
1633 bt->height += hdmi_read16(sd, 0x0b, 0xfff);
1634 bt->il_vfrontporch = hdmi_read16(sd, 0x2c, 0x1fff) / 2;
1635 bt->il_vsync = hdmi_read16(sd, 0x30, 0x1fff) / 2;
1636 bt->vbackporch = hdmi_read16(sd, 0x34, 0x1fff) / 2;
54450f59
HV
1637 }
1638 adv7604_fill_optional_dv_timings_fields(sd, timings);
1639 } else {
1640 /* find format
80939647 1641 * Since LCVS values are inaccurate [REF_03, p. 275-276],
54450f59
HV
1642 * stdi2dv_timings() is called with lcvs +-1 if the first attempt fails.
1643 */
1644 if (!stdi2dv_timings(sd, &stdi, timings))
1645 goto found;
1646 stdi.lcvs += 1;
1647 v4l2_dbg(1, debug, sd, "%s: lcvs + 1 = %d\n", __func__, stdi.lcvs);
1648 if (!stdi2dv_timings(sd, &stdi, timings))
1649 goto found;
1650 stdi.lcvs -= 2;
1651 v4l2_dbg(1, debug, sd, "%s: lcvs - 1 = %d\n", __func__, stdi.lcvs);
1652 if (stdi2dv_timings(sd, &stdi, timings)) {
cf9afb1d
HV
1653 /*
1654 * The STDI block may measure wrong values, especially
1655 * for lcvs and lcf. If the driver can not find any
1656 * valid timing, the STDI block is restarted to measure
1657 * the video timings again. The function will return an
1658 * error, but the restart of STDI will generate a new
1659 * STDI interrupt and the format detection process will
1660 * restart.
1661 */
1662 if (state->restart_stdi_once) {
1663 v4l2_dbg(1, debug, sd, "%s: restart STDI\n", __func__);
1664 /* TODO restart STDI for Sync Channel 2 */
1665 /* enter one-shot mode */
1666 cp_write_and_or(sd, 0x86, 0xf9, 0x00);
1667 /* trigger STDI restart */
1668 cp_write_and_or(sd, 0x86, 0xf9, 0x04);
1669 /* reset to continuous mode */
1670 cp_write_and_or(sd, 0x86, 0xf9, 0x02);
1671 state->restart_stdi_once = false;
1672 return -ENOLINK;
1673 }
54450f59
HV
1674 v4l2_dbg(1, debug, sd, "%s: format not supported\n", __func__);
1675 return -ERANGE;
1676 }
cf9afb1d 1677 state->restart_stdi_once = true;
54450f59
HV
1678 }
1679found:
1680
1681 if (no_signal(sd)) {
1682 v4l2_dbg(1, debug, sd, "%s: signal lost during readout\n", __func__);
1683 memset(timings, 0, sizeof(struct v4l2_dv_timings));
1684 return -ENOLINK;
1685 }
1686
4a31a93a
MR
1687 if ((is_analog_input(sd) && bt->pixelclock > 170000000) ||
1688 (is_digital_input(sd) && bt->pixelclock > 225000000)) {
54450f59
HV
1689 v4l2_dbg(1, debug, sd, "%s: pixelclock out of range %d\n",
1690 __func__, (u32)bt->pixelclock);
1691 return -ERANGE;
1692 }
1693
1694 if (debug > 1)
11d034c8
HV
1695 v4l2_print_dv_timings(sd->name, "adv7604_query_dv_timings: ",
1696 timings, true);
54450f59
HV
1697
1698 return 0;
1699}
1700
1701static int adv7604_s_dv_timings(struct v4l2_subdev *sd,
1702 struct v4l2_dv_timings *timings)
1703{
1704 struct adv7604_state *state = to_state(sd);
1705 struct v4l2_bt_timings *bt;
ccbd5bc4 1706 int err;
54450f59
HV
1707
1708 if (!timings)
1709 return -EINVAL;
1710
d48eb48c
MR
1711 if (v4l2_match_dv_timings(&state->timings, timings, 0)) {
1712 v4l2_dbg(1, debug, sd, "%s: no change\n", __func__);
1713 return 0;
1714 }
1715
54450f59
HV
1716 bt = &timings->bt;
1717
4a31a93a
MR
1718 if ((is_analog_input(sd) && bt->pixelclock > 170000000) ||
1719 (is_digital_input(sd) && bt->pixelclock > 225000000)) {
54450f59
HV
1720 v4l2_dbg(1, debug, sd, "%s: pixelclock out of range %d\n",
1721 __func__, (u32)bt->pixelclock);
1722 return -ERANGE;
1723 }
ccbd5bc4 1724
54450f59
HV
1725 adv7604_fill_optional_dv_timings_fields(sd, timings);
1726
1727 state->timings = *timings;
1728
d42010a1 1729 cp_write_and_or(sd, 0x91, 0xbf, bt->interlaced ? 0x40 : 0x00);
ccbd5bc4
HV
1730
1731 /* Use prim_mode and vid_std when available */
1732 err = configure_predefined_video_timings(sd, timings);
1733 if (err) {
1734 /* custom settings when the video format
1735 does not have prim_mode/vid_std */
1736 configure_custom_video_timings(sd, bt);
1737 }
54450f59
HV
1738
1739 set_rgb_quantization_range(sd);
1740
54450f59 1741 if (debug > 1)
11d034c8
HV
1742 v4l2_print_dv_timings(sd->name, "adv7604_s_dv_timings: ",
1743 timings, true);
54450f59
HV
1744 return 0;
1745}
1746
1747static int adv7604_g_dv_timings(struct v4l2_subdev *sd,
1748 struct v4l2_dv_timings *timings)
1749{
1750 struct adv7604_state *state = to_state(sd);
1751
1752 *timings = state->timings;
1753 return 0;
1754}
1755
d42010a1
LPC
1756static void adv7604_set_termination(struct v4l2_subdev *sd, bool enable)
1757{
1758 hdmi_write(sd, 0x01, enable ? 0x00 : 0x78);
1759}
1760
1761static void adv7611_set_termination(struct v4l2_subdev *sd, bool enable)
1762{
1763 hdmi_write(sd, 0x83, enable ? 0xfe : 0xff);
1764}
1765
6b0d5d34 1766static void enable_input(struct v4l2_subdev *sd)
54450f59 1767{
6b0d5d34
HV
1768 struct adv7604_state *state = to_state(sd);
1769
4a31a93a 1770 if (is_analog_input(sd)) {
54450f59 1771 io_write(sd, 0x15, 0xb0); /* Disable Tristate of Pins (no audio) */
4a31a93a 1772 } else if (is_digital_input(sd)) {
4a31a93a 1773 hdmi_write_and_or(sd, 0x00, 0xfc, state->selected_input);
d42010a1 1774 state->info->set_termination(sd, true);
54450f59 1775 io_write(sd, 0x15, 0xa0); /* Disable Tristate of Pins */
5474b983 1776 hdmi_write_and_or(sd, 0x1a, 0xef, 0x00); /* Unmute audio */
4a31a93a
MR
1777 } else {
1778 v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
1779 __func__, state->selected_input);
54450f59
HV
1780 }
1781}
1782
1783static void disable_input(struct v4l2_subdev *sd)
1784{
d42010a1
LPC
1785 struct adv7604_state *state = to_state(sd);
1786
5474b983
MR
1787 hdmi_write_and_or(sd, 0x1a, 0xef, 0x10); /* Mute audio */
1788 msleep(16); /* 512 samples with >= 32 kHz sample rate [REF_03, c. 7.16.10] */
54450f59 1789 io_write(sd, 0x15, 0xbe); /* Tristate all outputs from video core */
d42010a1 1790 state->info->set_termination(sd, false);
54450f59
HV
1791}
1792
6b0d5d34 1793static void select_input(struct v4l2_subdev *sd)
54450f59 1794{
6b0d5d34 1795 struct adv7604_state *state = to_state(sd);
d42010a1 1796 const struct adv7604_chip_info *info = state->info;
54450f59 1797
4a31a93a 1798 if (is_analog_input(sd)) {
d42010a1 1799 adv7604_write_reg_seq(sd, info->recommended_settings[0]);
54450f59
HV
1800
1801 afe_write(sd, 0x00, 0x08); /* power up ADC */
1802 afe_write(sd, 0x01, 0x06); /* power up Analog Front End */
1803 afe_write(sd, 0xc8, 0x00); /* phase control */
4a31a93a
MR
1804 } else if (is_digital_input(sd)) {
1805 hdmi_write(sd, 0x00, state->selected_input & 0x03);
54450f59 1806
d42010a1
LPC
1807 adv7604_write_reg_seq(sd, info->recommended_settings[1]);
1808
1809 if (adv7604_has_afe(state)) {
1810 afe_write(sd, 0x00, 0xff); /* power down ADC */
1811 afe_write(sd, 0x01, 0xfe); /* power down Analog Front End */
1812 afe_write(sd, 0xc8, 0x40); /* phase control */
1813 }
1814
54450f59
HV
1815 cp_write(sd, 0x3e, 0x00); /* CP core pre-gain control */
1816 cp_write(sd, 0xc3, 0x39); /* CP coast control. Graphics mode */
1817 cp_write(sd, 0x40, 0x80); /* CP core pre-gain control. Graphics mode */
4a31a93a
MR
1818 } else {
1819 v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
1820 __func__, state->selected_input);
54450f59
HV
1821 }
1822}
1823
1824static int adv7604_s_routing(struct v4l2_subdev *sd,
1825 u32 input, u32 output, u32 config)
1826{
1827 struct adv7604_state *state = to_state(sd);
1828
ff4f80fd
MR
1829 v4l2_dbg(2, debug, sd, "%s: input %d, selected input %d",
1830 __func__, input, state->selected_input);
1831
1832 if (input == state->selected_input)
1833 return 0;
54450f59 1834
d42010a1
LPC
1835 if (input > state->info->max_port)
1836 return -EINVAL;
1837
4a31a93a 1838 state->selected_input = input;
54450f59
HV
1839
1840 disable_input(sd);
1841
6b0d5d34 1842 select_input(sd);
54450f59 1843
6b0d5d34 1844 enable_input(sd);
54450f59
HV
1845
1846 return 0;
1847}
1848
539b33b0
LP
1849static int adv7604_enum_mbus_code(struct v4l2_subdev *sd,
1850 struct v4l2_subdev_fh *fh,
1851 struct v4l2_subdev_mbus_code_enum *code)
54450f59 1852{
539b33b0
LP
1853 struct adv7604_state *state = to_state(sd);
1854
1855 if (code->index >= state->info->nformats)
54450f59 1856 return -EINVAL;
539b33b0
LP
1857
1858 code->code = state->info->formats[code->index].code;
1859
54450f59
HV
1860 return 0;
1861}
1862
539b33b0
LP
1863static void adv7604_fill_format(struct adv7604_state *state,
1864 struct v4l2_mbus_framefmt *format)
54450f59 1865{
539b33b0 1866 memset(format, 0, sizeof(*format));
54450f59 1867
539b33b0
LP
1868 format->width = state->timings.bt.width;
1869 format->height = state->timings.bt.height;
1870 format->field = V4L2_FIELD_NONE;
1871
1872 if (state->timings.bt.standards & V4L2_DV_BT_STD_CEA861)
1873 format->colorspace = (state->timings.bt.height <= 576) ?
54450f59 1874 V4L2_COLORSPACE_SMPTE170M : V4L2_COLORSPACE_REC709;
539b33b0
LP
1875}
1876
1877/*
1878 * Compute the op_ch_sel value required to obtain on the bus the component order
1879 * corresponding to the selected format taking into account bus reordering
1880 * applied by the board at the output of the device.
1881 *
1882 * The following table gives the op_ch_value from the format component order
1883 * (expressed as op_ch_sel value in column) and the bus reordering (expressed as
1884 * adv7604_bus_order value in row).
1885 *
1886 * | GBR(0) GRB(1) BGR(2) RGB(3) BRG(4) RBG(5)
1887 * ----------+-------------------------------------------------
1888 * RGB (NOP) | GBR GRB BGR RGB BRG RBG
1889 * GRB (1-2) | BGR RGB GBR GRB RBG BRG
1890 * RBG (2-3) | GRB GBR BRG RBG BGR RGB
1891 * BGR (1-3) | RBG BRG RGB BGR GRB GBR
1892 * BRG (ROR) | BRG RBG GRB GBR RGB BGR
1893 * GBR (ROL) | RGB BGR RBG BRG GBR GRB
1894 */
1895static unsigned int adv7604_op_ch_sel(struct adv7604_state *state)
1896{
1897#define _SEL(a,b,c,d,e,f) { \
1898 ADV7604_OP_CH_SEL_##a, ADV7604_OP_CH_SEL_##b, ADV7604_OP_CH_SEL_##c, \
1899 ADV7604_OP_CH_SEL_##d, ADV7604_OP_CH_SEL_##e, ADV7604_OP_CH_SEL_##f }
1900#define _BUS(x) [ADV7604_BUS_ORDER_##x]
1901
1902 static const unsigned int op_ch_sel[6][6] = {
1903 _BUS(RGB) /* NOP */ = _SEL(GBR, GRB, BGR, RGB, BRG, RBG),
1904 _BUS(GRB) /* 1-2 */ = _SEL(BGR, RGB, GBR, GRB, RBG, BRG),
1905 _BUS(RBG) /* 2-3 */ = _SEL(GRB, GBR, BRG, RBG, BGR, RGB),
1906 _BUS(BGR) /* 1-3 */ = _SEL(RBG, BRG, RGB, BGR, GRB, GBR),
1907 _BUS(BRG) /* ROR */ = _SEL(BRG, RBG, GRB, GBR, RGB, BGR),
1908 _BUS(GBR) /* ROL */ = _SEL(RGB, BGR, RBG, BRG, GBR, GRB),
1909 };
1910
1911 return op_ch_sel[state->pdata.bus_order][state->format->op_ch_sel >> 5];
1912}
1913
1914static void adv7604_setup_format(struct adv7604_state *state)
1915{
1916 struct v4l2_subdev *sd = &state->sd;
1917
1918 io_write_and_or(sd, 0x02, 0xfd,
1919 state->format->rgb_out ? ADV7604_RGB_OUT : 0);
1920 io_write(sd, 0x03, state->format->op_format_sel |
1921 state->pdata.op_format_mode_sel);
1922 io_write_and_or(sd, 0x04, 0x1f, adv7604_op_ch_sel(state));
1923 io_write_and_or(sd, 0x05, 0xfe,
1924 state->format->swap_cb_cr ? ADV7604_OP_SWAP_CB_CR : 0);
1925}
1926
1927static int adv7604_get_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
1928 struct v4l2_subdev_format *format)
1929{
1930 struct adv7604_state *state = to_state(sd);
1931
1932 if (format->pad != state->source_pad)
1933 return -EINVAL;
1934
1935 adv7604_fill_format(state, &format->format);
1936
1937 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1938 struct v4l2_mbus_framefmt *fmt;
1939
1940 fmt = v4l2_subdev_get_try_format(fh, format->pad);
1941 format->format.code = fmt->code;
1942 } else {
1943 format->format.code = state->format->code;
54450f59 1944 }
539b33b0
LP
1945
1946 return 0;
1947}
1948
1949static int adv7604_set_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
1950 struct v4l2_subdev_format *format)
1951{
1952 struct adv7604_state *state = to_state(sd);
1953 const struct adv7604_format_info *info;
1954
1955 if (format->pad != state->source_pad)
1956 return -EINVAL;
1957
1958 info = adv7604_format_info(state, format->format.code);
1959 if (info == NULL)
1960 info = adv7604_format_info(state, V4L2_MBUS_FMT_YUYV8_2X8);
1961
1962 adv7604_fill_format(state, &format->format);
1963 format->format.code = info->code;
1964
1965 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1966 struct v4l2_mbus_framefmt *fmt;
1967
1968 fmt = v4l2_subdev_get_try_format(fh, format->pad);
1969 fmt->code = format->format.code;
1970 } else {
1971 state->format = info;
1972 adv7604_setup_format(state);
1973 }
1974
54450f59
HV
1975 return 0;
1976}
1977
1978static int adv7604_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
1979{
d42010a1
LPC
1980 struct adv7604_state *state = to_state(sd);
1981 const struct adv7604_chip_info *info = state->info;
f24d229c
MR
1982 const u8 irq_reg_0x43 = io_read(sd, 0x43);
1983 const u8 irq_reg_0x6b = io_read(sd, 0x6b);
1984 const u8 irq_reg_0x70 = io_read(sd, 0x70);
1985 u8 fmt_change_digital;
1986 u8 fmt_change;
1987 u8 tx_5v;
1988
1989 if (irq_reg_0x43)
1990 io_write(sd, 0x44, irq_reg_0x43);
1991 if (irq_reg_0x70)
1992 io_write(sd, 0x71, irq_reg_0x70);
1993 if (irq_reg_0x6b)
1994 io_write(sd, 0x6c, irq_reg_0x6b);
54450f59 1995
ff4f80fd
MR
1996 v4l2_dbg(2, debug, sd, "%s: ", __func__);
1997
54450f59 1998 /* format change */
f24d229c 1999 fmt_change = irq_reg_0x43 & 0x98;
d42010a1
LPC
2000 fmt_change_digital = is_digital_input(sd)
2001 ? irq_reg_0x6b & info->fmt_change_digital_mask
2002 : 0;
14d03233 2003
54450f59
HV
2004 if (fmt_change || fmt_change_digital) {
2005 v4l2_dbg(1, debug, sd,
25a64ac9 2006 "%s: fmt_change = 0x%x, fmt_change_digital = 0x%x\n",
54450f59 2007 __func__, fmt_change, fmt_change_digital);
25a64ac9 2008
14d03233 2009 v4l2_subdev_notify(sd, ADV7604_FMT_CHANGE, NULL);
25a64ac9 2010
54450f59
HV
2011 if (handled)
2012 *handled = true;
2013 }
f24d229c
MR
2014 /* HDMI/DVI mode */
2015 if (irq_reg_0x6b & 0x01) {
2016 v4l2_dbg(1, debug, sd, "%s: irq %s mode\n", __func__,
2017 (io_read(sd, 0x6a) & 0x01) ? "HDMI" : "DVI");
2018 set_rgb_quantization_range(sd);
2019 if (handled)
2020 *handled = true;
2021 }
2022
54450f59 2023 /* tx 5v detect */
d42010a1 2024 tx_5v = io_read(sd, 0x70) & info->cable_det_mask;
54450f59
HV
2025 if (tx_5v) {
2026 v4l2_dbg(1, debug, sd, "%s: tx_5v: 0x%x\n", __func__, tx_5v);
2027 io_write(sd, 0x71, tx_5v);
2028 adv7604_s_detect_tx_5v_ctrl(sd);
2029 if (handled)
2030 *handled = true;
2031 }
2032 return 0;
2033}
2034
b09dfac8 2035static int adv7604_get_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
54450f59
HV
2036{
2037 struct adv7604_state *state = to_state(sd);
4a31a93a 2038 u8 *data = NULL;
54450f59 2039
c784b1e2 2040 if (edid->pad > ADV7604_PAD_HDMI_PORT_D)
54450f59
HV
2041 return -EINVAL;
2042 if (edid->blocks == 0)
2043 return -EINVAL;
4a31a93a 2044 if (edid->blocks > 2)
54450f59 2045 return -EINVAL;
4a31a93a
MR
2046 if (edid->start_block > 1)
2047 return -EINVAL;
2048 if (edid->start_block == 1)
2049 edid->blocks = 1;
4a31a93a
MR
2050
2051 if (edid->blocks > state->edid.blocks)
2052 edid->blocks = state->edid.blocks;
2053
2054 switch (edid->pad) {
c784b1e2
LP
2055 case ADV7604_PAD_HDMI_PORT_A:
2056 case ADV7604_PAD_HDMI_PORT_B:
2057 case ADV7604_PAD_HDMI_PORT_C:
2058 case ADV7604_PAD_HDMI_PORT_D:
4a31a93a
MR
2059 if (state->edid.present & (1 << edid->pad))
2060 data = state->edid.edid;
2061 break;
2062 default:
2063 return -EINVAL;
2064 break;
2065 }
2066 if (!data)
2067 return -ENODATA;
2068
2069 memcpy(edid->edid,
2070 data + edid->start_block * 128,
54450f59
HV
2071 edid->blocks * 128);
2072 return 0;
2073}
2074
dd08beb9 2075static int get_edid_spa_location(const u8 *edid)
3e86aa85
MR
2076{
2077 u8 d;
2078
2079 if ((edid[0x7e] != 1) ||
2080 (edid[0x80] != 0x02) ||
2081 (edid[0x81] != 0x03)) {
2082 return -1;
2083 }
2084
2085 /* search Vendor Specific Data Block (tag 3) */
2086 d = edid[0x82] & 0x7f;
2087 if (d > 4) {
2088 int i = 0x84;
2089 int end = 0x80 + d;
2090
2091 do {
2092 u8 tag = edid[i] >> 5;
2093 u8 len = edid[i] & 0x1f;
2094
2095 if ((tag == 3) && (len >= 5))
2096 return i + 4;
2097 i += len + 1;
2098 } while (i < end);
2099 }
2100 return -1;
2101}
2102
b09dfac8 2103static int adv7604_set_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
54450f59
HV
2104{
2105 struct adv7604_state *state = to_state(sd);
d42010a1 2106 const struct adv7604_chip_info *info = state->info;
dd08beb9 2107 int spa_loc;
3e86aa85 2108 int tmp = 0;
54450f59 2109 int err;
dd08beb9 2110 int i;
54450f59 2111
c784b1e2 2112 if (edid->pad > ADV7604_PAD_HDMI_PORT_D)
54450f59
HV
2113 return -EINVAL;
2114 if (edid->start_block != 0)
2115 return -EINVAL;
2116 if (edid->blocks == 0) {
3e86aa85 2117 /* Disable hotplug and I2C access to EDID RAM from DDC port */
4a31a93a
MR
2118 state->edid.present &= ~(1 << edid->pad);
2119 v4l2_subdev_notify(sd, ADV7604_HOTPLUG, (void *)&state->edid.present);
d42010a1 2120 rep_write_and_or(sd, info->edid_enable_reg, 0xf0, state->edid.present);
3e86aa85 2121
54450f59
HV
2122 /* Fall back to a 16:9 aspect ratio */
2123 state->aspect_ratio.numerator = 16;
2124 state->aspect_ratio.denominator = 9;
3e86aa85
MR
2125
2126 if (!state->edid.present)
2127 state->edid.blocks = 0;
2128
2129 v4l2_dbg(2, debug, sd, "%s: clear EDID pad %d, edid.present = 0x%x\n",
2130 __func__, edid->pad, state->edid.present);
54450f59
HV
2131 return 0;
2132 }
4a31a93a
MR
2133 if (edid->blocks > 2) {
2134 edid->blocks = 2;
54450f59 2135 return -E2BIG;
4a31a93a 2136 }
4a31a93a 2137
dd08beb9
MR
2138 v4l2_dbg(2, debug, sd, "%s: write EDID pad %d, edid.present = 0x%x\n",
2139 __func__, edid->pad, state->edid.present);
2140
3e86aa85 2141 /* Disable hotplug and I2C access to EDID RAM from DDC port */
4a31a93a 2142 cancel_delayed_work_sync(&state->delayed_work_enable_hotplug);
3e86aa85 2143 v4l2_subdev_notify(sd, ADV7604_HOTPLUG, (void *)&tmp);
d42010a1 2144 rep_write_and_or(sd, info->edid_enable_reg, 0xf0, 0x00);
3e86aa85 2145
dd08beb9
MR
2146 spa_loc = get_edid_spa_location(edid->edid);
2147 if (spa_loc < 0)
2148 spa_loc = 0xc0; /* Default value [REF_02, p. 116] */
2149
3e86aa85 2150 switch (edid->pad) {
c784b1e2 2151 case ADV7604_PAD_HDMI_PORT_A:
dd08beb9
MR
2152 state->spa_port_a[0] = edid->edid[spa_loc];
2153 state->spa_port_a[1] = edid->edid[spa_loc + 1];
3e86aa85 2154 break;
c784b1e2 2155 case ADV7604_PAD_HDMI_PORT_B:
dd08beb9
MR
2156 rep_write(sd, 0x70, edid->edid[spa_loc]);
2157 rep_write(sd, 0x71, edid->edid[spa_loc + 1]);
3e86aa85 2158 break;
c784b1e2 2159 case ADV7604_PAD_HDMI_PORT_C:
dd08beb9
MR
2160 rep_write(sd, 0x72, edid->edid[spa_loc]);
2161 rep_write(sd, 0x73, edid->edid[spa_loc + 1]);
3e86aa85 2162 break;
c784b1e2 2163 case ADV7604_PAD_HDMI_PORT_D:
dd08beb9
MR
2164 rep_write(sd, 0x74, edid->edid[spa_loc]);
2165 rep_write(sd, 0x75, edid->edid[spa_loc + 1]);
3e86aa85 2166 break;
dd08beb9
MR
2167 default:
2168 return -EINVAL;
3e86aa85 2169 }
d42010a1
LPC
2170
2171 if (info->type == ADV7604) {
2172 rep_write(sd, 0x76, spa_loc & 0xff);
2173 rep_write_and_or(sd, 0x77, 0xbf, (spa_loc & 0x100) >> 2);
2174 } else {
2175 /* FIXME: Where is the SPA location LSB register ? */
2176 rep_write_and_or(sd, 0x71, 0xfe, (spa_loc & 0x100) >> 8);
2177 }
3e86aa85 2178
dd08beb9
MR
2179 edid->edid[spa_loc] = state->spa_port_a[0];
2180 edid->edid[spa_loc + 1] = state->spa_port_a[1];
4a31a93a
MR
2181
2182 memcpy(state->edid.edid, edid->edid, 128 * edid->blocks);
2183 state->edid.blocks = edid->blocks;
54450f59
HV
2184 state->aspect_ratio = v4l2_calc_aspect_ratio(edid->edid[0x15],
2185 edid->edid[0x16]);
3e86aa85 2186 state->edid.present |= 1 << edid->pad;
4a31a93a
MR
2187
2188 err = edid_write_block(sd, 128 * edid->blocks, state->edid.edid);
2189 if (err < 0) {
3e86aa85 2190 v4l2_err(sd, "error %d writing edid pad %d\n", err, edid->pad);
4a31a93a
MR
2191 return err;
2192 }
2193
dd08beb9
MR
2194 /* adv7604 calculates the checksums and enables I2C access to internal
2195 EDID RAM from DDC port. */
d42010a1 2196 rep_write_and_or(sd, info->edid_enable_reg, 0xf0, state->edid.present);
dd08beb9
MR
2197
2198 for (i = 0; i < 1000; i++) {
d42010a1 2199 if (rep_read(sd, info->edid_status_reg) & state->edid.present)
dd08beb9
MR
2200 break;
2201 mdelay(1);
2202 }
2203 if (i == 1000) {
2204 v4l2_err(sd, "error enabling edid (0x%x)\n", state->edid.present);
2205 return -EIO;
2206 }
2207
2208
4a31a93a
MR
2209 /* enable hotplug after 100 ms */
2210 queue_delayed_work(state->work_queues,
2211 &state->delayed_work_enable_hotplug, HZ / 10);
2212 return 0;
54450f59
HV
2213}
2214
2215/*********** avi info frame CEA-861-E **************/
2216
2217static void print_avi_infoframe(struct v4l2_subdev *sd)
2218{
2219 int i;
2220 u8 buf[14];
2221 u8 avi_len;
2222 u8 avi_ver;
2223
bb88f325 2224 if (!is_hdmi(sd)) {
54450f59
HV
2225 v4l2_info(sd, "receive DVI-D signal (AVI infoframe not supported)\n");
2226 return;
2227 }
2228 if (!(io_read(sd, 0x60) & 0x01)) {
2229 v4l2_info(sd, "AVI infoframe not received\n");
2230 return;
2231 }
2232
2233 if (io_read(sd, 0x83) & 0x01) {
2234 v4l2_info(sd, "AVI infoframe checksum error has occurred earlier\n");
2235 io_write(sd, 0x85, 0x01); /* clear AVI_INF_CKS_ERR_RAW */
2236 if (io_read(sd, 0x83) & 0x01) {
2237 v4l2_info(sd, "AVI infoframe checksum error still present\n");
2238 io_write(sd, 0x85, 0x01); /* clear AVI_INF_CKS_ERR_RAW */
2239 }
2240 }
2241
2242 avi_len = infoframe_read(sd, 0xe2);
2243 avi_ver = infoframe_read(sd, 0xe1);
2244 v4l2_info(sd, "AVI infoframe version %d (%d byte)\n",
2245 avi_ver, avi_len);
2246
2247 if (avi_ver != 0x02)
2248 return;
2249
2250 for (i = 0; i < 14; i++)
2251 buf[i] = infoframe_read(sd, i);
2252
2253 v4l2_info(sd,
2254 "\t%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
2255 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7],
2256 buf[8], buf[9], buf[10], buf[11], buf[12], buf[13]);
2257}
2258
2259static int adv7604_log_status(struct v4l2_subdev *sd)
2260{
2261 struct adv7604_state *state = to_state(sd);
d42010a1 2262 const struct adv7604_chip_info *info = state->info;
54450f59
HV
2263 struct v4l2_dv_timings timings;
2264 struct stdi_readback stdi;
2265 u8 reg_io_0x02 = io_read(sd, 0x02);
4a2ccdd2
LP
2266 u8 edid_enabled;
2267 u8 cable_det;
54450f59 2268
f216ccb3 2269 static const char * const csc_coeff_sel_rb[16] = {
54450f59
HV
2270 "bypassed", "YPbPr601 -> RGB", "reserved", "YPbPr709 -> RGB",
2271 "reserved", "RGB -> YPbPr601", "reserved", "RGB -> YPbPr709",
2272 "reserved", "YPbPr709 -> YPbPr601", "YPbPr601 -> YPbPr709",
2273 "reserved", "reserved", "reserved", "reserved", "manual"
2274 };
f216ccb3 2275 static const char * const input_color_space_txt[16] = {
54450f59
HV
2276 "RGB limited range (16-235)", "RGB full range (0-255)",
2277 "YCbCr Bt.601 (16-235)", "YCbCr Bt.709 (16-235)",
9833239e 2278 "xvYCC Bt.601", "xvYCC Bt.709",
54450f59
HV
2279 "YCbCr Bt.601 (0-255)", "YCbCr Bt.709 (0-255)",
2280 "invalid", "invalid", "invalid", "invalid", "invalid",
2281 "invalid", "invalid", "automatic"
2282 };
f216ccb3 2283 static const char * const rgb_quantization_range_txt[] = {
54450f59
HV
2284 "Automatic",
2285 "RGB limited range (16-235)",
2286 "RGB full range (0-255)",
2287 };
f216ccb3 2288 static const char * const deep_color_mode_txt[4] = {
bb88f325
MB
2289 "8-bits per channel",
2290 "10-bits per channel",
2291 "12-bits per channel",
2292 "16-bits per channel (not supported)"
2293 };
54450f59
HV
2294
2295 v4l2_info(sd, "-----Chip status-----\n");
2296 v4l2_info(sd, "Chip power: %s\n", no_power(sd) ? "off" : "on");
d42010a1 2297 edid_enabled = rep_read(sd, info->edid_status_reg);
4a31a93a 2298 v4l2_info(sd, "EDID enabled port A: %s, B: %s, C: %s, D: %s\n",
4a2ccdd2
LP
2299 ((edid_enabled & 0x01) ? "Yes" : "No"),
2300 ((edid_enabled & 0x02) ? "Yes" : "No"),
2301 ((edid_enabled & 0x04) ? "Yes" : "No"),
2302 ((edid_enabled & 0x08) ? "Yes" : "No"));
54450f59
HV
2303 v4l2_info(sd, "CEC: %s\n", !!(cec_read(sd, 0x2a) & 0x01) ?
2304 "enabled" : "disabled");
2305
2306 v4l2_info(sd, "-----Signal status-----\n");
d42010a1 2307 cable_det = info->read_cable_det(sd);
4a31a93a 2308 v4l2_info(sd, "Cable detected (+5V power) port A: %s, B: %s, C: %s, D: %s\n",
d42010a1
LPC
2309 ((cable_det & 0x01) ? "Yes" : "No"),
2310 ((cable_det & 0x02) ? "Yes" : "No"),
4a2ccdd2 2311 ((cable_det & 0x04) ? "Yes" : "No"),
d42010a1 2312 ((cable_det & 0x08) ? "Yes" : "No"));
54450f59
HV
2313 v4l2_info(sd, "TMDS signal detected: %s\n",
2314 no_signal_tmds(sd) ? "false" : "true");
2315 v4l2_info(sd, "TMDS signal locked: %s\n",
2316 no_lock_tmds(sd) ? "false" : "true");
2317 v4l2_info(sd, "SSPD locked: %s\n", no_lock_sspd(sd) ? "false" : "true");
2318 v4l2_info(sd, "STDI locked: %s\n", no_lock_stdi(sd) ? "false" : "true");
2319 v4l2_info(sd, "CP locked: %s\n", no_lock_cp(sd) ? "false" : "true");
2320 v4l2_info(sd, "CP free run: %s\n",
2321 (!!(cp_read(sd, 0xff) & 0x10) ? "on" : "off"));
ccbd5bc4
HV
2322 v4l2_info(sd, "Prim-mode = 0x%x, video std = 0x%x, v_freq = 0x%x\n",
2323 io_read(sd, 0x01) & 0x0f, io_read(sd, 0x00) & 0x3f,
2324 (io_read(sd, 0x01) & 0x70) >> 4);
54450f59
HV
2325
2326 v4l2_info(sd, "-----Video Timings-----\n");
2327 if (read_stdi(sd, &stdi))
2328 v4l2_info(sd, "STDI: not locked\n");
2329 else
2330 v4l2_info(sd, "STDI: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, %s, %chsync, %cvsync\n",
2331 stdi.lcf, stdi.bl, stdi.lcvs,
2332 stdi.interlaced ? "interlaced" : "progressive",
2333 stdi.hs_pol, stdi.vs_pol);
2334 if (adv7604_query_dv_timings(sd, &timings))
2335 v4l2_info(sd, "No video detected\n");
2336 else
11d034c8
HV
2337 v4l2_print_dv_timings(sd->name, "Detected format: ",
2338 &timings, true);
2339 v4l2_print_dv_timings(sd->name, "Configured format: ",
2340 &state->timings, true);
54450f59 2341
76eb2d30
MR
2342 if (no_signal(sd))
2343 return 0;
2344
54450f59
HV
2345 v4l2_info(sd, "-----Color space-----\n");
2346 v4l2_info(sd, "RGB quantization range ctrl: %s\n",
2347 rgb_quantization_range_txt[state->rgb_quantization_range]);
2348 v4l2_info(sd, "Input color space: %s\n",
2349 input_color_space_txt[reg_io_0x02 >> 4]);
2350 v4l2_info(sd, "Output color space: %s %s, saturator %s\n",
2351 (reg_io_0x02 & 0x02) ? "RGB" : "YCbCr",
2352 (reg_io_0x02 & 0x04) ? "(16-235)" : "(0-255)",
2353 ((reg_io_0x02 & 0x04) ^ (reg_io_0x02 & 0x01)) ?
76eb2d30 2354 "enabled" : "disabled");
54450f59
HV
2355 v4l2_info(sd, "Color space conversion: %s\n",
2356 csc_coeff_sel_rb[cp_read(sd, 0xfc) >> 4]);
2357
4a31a93a 2358 if (!is_digital_input(sd))
76eb2d30
MR
2359 return 0;
2360
2361 v4l2_info(sd, "-----%s status-----\n", is_hdmi(sd) ? "HDMI" : "DVI-D");
4a31a93a
MR
2362 v4l2_info(sd, "Digital video port selected: %c\n",
2363 (hdmi_read(sd, 0x00) & 0x03) + 'A');
2364 v4l2_info(sd, "HDCP encrypted content: %s\n",
2365 (hdmi_read(sd, 0x05) & 0x40) ? "true" : "false");
76eb2d30
MR
2366 v4l2_info(sd, "HDCP keys read: %s%s\n",
2367 (hdmi_read(sd, 0x04) & 0x20) ? "yes" : "no",
2368 (hdmi_read(sd, 0x04) & 0x10) ? "ERROR" : "");
2369 if (!is_hdmi(sd)) {
2370 bool audio_pll_locked = hdmi_read(sd, 0x04) & 0x01;
2371 bool audio_sample_packet_detect = hdmi_read(sd, 0x18) & 0x01;
2372 bool audio_mute = io_read(sd, 0x65) & 0x40;
2373
2374 v4l2_info(sd, "Audio: pll %s, samples %s, %s\n",
2375 audio_pll_locked ? "locked" : "not locked",
2376 audio_sample_packet_detect ? "detected" : "not detected",
2377 audio_mute ? "muted" : "enabled");
2378 if (audio_pll_locked && audio_sample_packet_detect) {
2379 v4l2_info(sd, "Audio format: %s\n",
2380 (hdmi_read(sd, 0x07) & 0x20) ? "multi-channel" : "stereo");
2381 }
2382 v4l2_info(sd, "Audio CTS: %u\n", (hdmi_read(sd, 0x5b) << 12) +
2383 (hdmi_read(sd, 0x5c) << 8) +
2384 (hdmi_read(sd, 0x5d) & 0xf0));
2385 v4l2_info(sd, "Audio N: %u\n", ((hdmi_read(sd, 0x5d) & 0x0f) << 16) +
2386 (hdmi_read(sd, 0x5e) << 8) +
2387 hdmi_read(sd, 0x5f));
2388 v4l2_info(sd, "AV Mute: %s\n", (hdmi_read(sd, 0x04) & 0x40) ? "on" : "off");
2389
2390 v4l2_info(sd, "Deep color mode: %s\n", deep_color_mode_txt[(hdmi_read(sd, 0x0b) & 0x60) >> 5]);
2391
54450f59
HV
2392 print_avi_infoframe(sd);
2393 }
2394
2395 return 0;
2396}
2397
2398/* ----------------------------------------------------------------------- */
2399
2400static const struct v4l2_ctrl_ops adv7604_ctrl_ops = {
2401 .s_ctrl = adv7604_s_ctrl,
2402};
2403
2404static const struct v4l2_subdev_core_ops adv7604_core_ops = {
2405 .log_status = adv7604_log_status,
54450f59
HV
2406 .interrupt_service_routine = adv7604_isr,
2407#ifdef CONFIG_VIDEO_ADV_DEBUG
2408 .g_register = adv7604_g_register,
2409 .s_register = adv7604_s_register,
2410#endif
2411};
2412
2413static const struct v4l2_subdev_video_ops adv7604_video_ops = {
2414 .s_routing = adv7604_s_routing,
2415 .g_input_status = adv7604_g_input_status,
2416 .s_dv_timings = adv7604_s_dv_timings,
2417 .g_dv_timings = adv7604_g_dv_timings,
2418 .query_dv_timings = adv7604_query_dv_timings,
2419 .enum_dv_timings = adv7604_enum_dv_timings,
2420 .dv_timings_cap = adv7604_dv_timings_cap,
54450f59
HV
2421};
2422
2423static const struct v4l2_subdev_pad_ops adv7604_pad_ops = {
539b33b0
LP
2424 .enum_mbus_code = adv7604_enum_mbus_code,
2425 .get_fmt = adv7604_get_format,
2426 .set_fmt = adv7604_set_format,
54450f59
HV
2427 .get_edid = adv7604_get_edid,
2428 .set_edid = adv7604_set_edid,
2429};
2430
2431static const struct v4l2_subdev_ops adv7604_ops = {
2432 .core = &adv7604_core_ops,
2433 .video = &adv7604_video_ops,
2434 .pad = &adv7604_pad_ops,
2435};
2436
2437/* -------------------------- custom ctrls ---------------------------------- */
2438
2439static const struct v4l2_ctrl_config adv7604_ctrl_analog_sampling_phase = {
2440 .ops = &adv7604_ctrl_ops,
2441 .id = V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE,
2442 .name = "Analog Sampling Phase",
2443 .type = V4L2_CTRL_TYPE_INTEGER,
2444 .min = 0,
2445 .max = 0x1f,
2446 .step = 1,
2447 .def = 0,
2448};
2449
2450static const struct v4l2_ctrl_config adv7604_ctrl_free_run_color_manual = {
2451 .ops = &adv7604_ctrl_ops,
2452 .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL,
2453 .name = "Free Running Color, Manual",
2454 .type = V4L2_CTRL_TYPE_BOOLEAN,
2455 .min = false,
2456 .max = true,
2457 .step = 1,
2458 .def = false,
2459};
2460
2461static const struct v4l2_ctrl_config adv7604_ctrl_free_run_color = {
2462 .ops = &adv7604_ctrl_ops,
2463 .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR,
2464 .name = "Free Running Color",
2465 .type = V4L2_CTRL_TYPE_INTEGER,
2466 .min = 0x0,
2467 .max = 0xffffff,
2468 .step = 0x1,
2469 .def = 0x0,
2470};
2471
2472/* ----------------------------------------------------------------------- */
2473
2474static int adv7604_core_init(struct v4l2_subdev *sd)
2475{
2476 struct adv7604_state *state = to_state(sd);
d42010a1 2477 const struct adv7604_chip_info *info = state->info;
54450f59
HV
2478 struct adv7604_platform_data *pdata = &state->pdata;
2479
2480 hdmi_write(sd, 0x48,
2481 (pdata->disable_pwrdnb ? 0x80 : 0) |
2482 (pdata->disable_cable_det_rst ? 0x40 : 0));
2483
2484 disable_input(sd);
2485
2486 /* power */
2487 io_write(sd, 0x0c, 0x42); /* Power up part and power down VDP */
2488 io_write(sd, 0x0b, 0x44); /* Power down ESDP block */
2489 cp_write(sd, 0xcf, 0x01); /* Power down macrovision */
2490
2491 /* video format */
2492 io_write_and_or(sd, 0x02, 0xf0,
2493 pdata->alt_gamma << 3 |
2494 pdata->op_656_range << 2 |
54450f59 2495 pdata->alt_data_sat << 0);
539b33b0
LP
2496 io_write_and_or(sd, 0x05, 0xf1, pdata->blank_data << 3 |
2497 pdata->insert_av_codes << 2 |
2498 pdata->replicate_av_codes << 1);
2499 adv7604_setup_format(state);
54450f59 2500
54450f59 2501 cp_write(sd, 0x69, 0x30); /* Enable CP CSC */
98908696
MB
2502
2503 /* VS, HS polarities */
2504 io_write(sd, 0x06, 0xa0 | pdata->inv_vs_pol << 2 | pdata->inv_hs_pol << 1);
f31b62e1
MK
2505
2506 /* Adjust drive strength */
2507 io_write(sd, 0x14, 0x40 | pdata->dr_str_data << 4 |
2508 pdata->dr_str_clk << 2 |
2509 pdata->dr_str_sync);
2510
54450f59
HV
2511 cp_write(sd, 0xba, (pdata->hdmi_free_run_mode << 1) | 0x01); /* HDMI free run */
2512 cp_write(sd, 0xf3, 0xdc); /* Low threshold to enter/exit free run mode */
2513 cp_write(sd, 0xf9, 0x23); /* STDI ch. 1 - LCVS change threshold -
80939647 2514 ADI recommended setting [REF_01, c. 2.3.3] */
54450f59 2515 cp_write(sd, 0x45, 0x23); /* STDI ch. 2 - LCVS change threshold -
80939647 2516 ADI recommended setting [REF_01, c. 2.3.3] */
54450f59
HV
2517 cp_write(sd, 0xc9, 0x2d); /* use prim_mode and vid_std as free run resolution
2518 for digital formats */
2519
5474b983
MR
2520 /* HDMI audio */
2521 hdmi_write_and_or(sd, 0x15, 0xfc, 0x03); /* Mute on FIFO over-/underflow [REF_01, c. 1.2.18] */
2522 hdmi_write_and_or(sd, 0x1a, 0xf1, 0x08); /* Wait 1 s before unmute */
2523 hdmi_write_and_or(sd, 0x68, 0xf9, 0x06); /* FIFO reset on over-/underflow [REF_01, c. 1.2.19] */
2524
54450f59
HV
2525 /* TODO from platform data */
2526 afe_write(sd, 0xb5, 0x01); /* Setting MCLK to 256Fs */
2527
d42010a1
LPC
2528 if (adv7604_has_afe(state)) {
2529 afe_write(sd, 0x02, pdata->ain_sel); /* Select analog input muxing mode */
2530 io_write_and_or(sd, 0x30, ~(1 << 4), pdata->output_bus_lsb_to_msb << 4);
2531 }
54450f59 2532
54450f59 2533 /* interrupts */
d42010a1 2534 io_write(sd, 0x40, 0xc0 | pdata->int1_config); /* Configure INT1 */
54450f59 2535 io_write(sd, 0x46, 0x98); /* Enable SSPD, STDI and CP unlocked interrupts */
d42010a1
LPC
2536 io_write(sd, 0x6e, info->fmt_change_digital_mask); /* Enable V_LOCKED and DE_REGEN_LCK interrupts */
2537 io_write(sd, 0x73, info->cable_det_mask); /* Enable cable detection (+5v) interrupts */
2538 info->setup_irqs(sd);
54450f59
HV
2539
2540 return v4l2_ctrl_handler_setup(sd->ctrl_handler);
2541}
2542
d42010a1
LPC
2543static void adv7604_setup_irqs(struct v4l2_subdev *sd)
2544{
2545 io_write(sd, 0x41, 0xd7); /* STDI irq for any change, disable INT2 */
2546}
2547
2548static void adv7611_setup_irqs(struct v4l2_subdev *sd)
2549{
2550 io_write(sd, 0x41, 0xd0); /* STDI irq for any change, disable INT2 */
2551}
2552
54450f59
HV
2553static void adv7604_unregister_clients(struct adv7604_state *state)
2554{
2555 if (state->i2c_avlink)
2556 i2c_unregister_device(state->i2c_avlink);
2557 if (state->i2c_cec)
2558 i2c_unregister_device(state->i2c_cec);
2559 if (state->i2c_infoframe)
2560 i2c_unregister_device(state->i2c_infoframe);
2561 if (state->i2c_esdp)
2562 i2c_unregister_device(state->i2c_esdp);
2563 if (state->i2c_dpp)
2564 i2c_unregister_device(state->i2c_dpp);
2565 if (state->i2c_afe)
2566 i2c_unregister_device(state->i2c_afe);
2567 if (state->i2c_repeater)
2568 i2c_unregister_device(state->i2c_repeater);
2569 if (state->i2c_edid)
2570 i2c_unregister_device(state->i2c_edid);
2571 if (state->i2c_hdmi)
2572 i2c_unregister_device(state->i2c_hdmi);
2573 if (state->i2c_test)
2574 i2c_unregister_device(state->i2c_test);
2575 if (state->i2c_cp)
2576 i2c_unregister_device(state->i2c_cp);
2577 if (state->i2c_vdp)
2578 i2c_unregister_device(state->i2c_vdp);
2579}
2580
2581static struct i2c_client *adv7604_dummy_client(struct v4l2_subdev *sd,
2582 u8 addr, u8 io_reg)
2583{
2584 struct i2c_client *client = v4l2_get_subdevdata(sd);
2585
2586 if (addr)
2587 io_write(sd, io_reg, addr << 1);
2588 return i2c_new_dummy(client->adapter, io_read(sd, io_reg) >> 1);
2589}
2590
d42010a1
LPC
2591static const struct adv7604_reg_seq adv7604_recommended_settings_afe[] = {
2592 /* reset ADI recommended settings for HDMI: */
2593 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 4. */
2594 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x0d), 0x04 }, /* HDMI filter optimization */
2595 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x0d), 0x04 }, /* HDMI filter optimization */
2596 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x3d), 0x00 }, /* DDC bus active pull-up control */
2597 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x3e), 0x74 }, /* TMDS PLL optimization */
2598 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x4e), 0x3b }, /* TMDS PLL optimization */
2599 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x57), 0x74 }, /* TMDS PLL optimization */
2600 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x58), 0x63 }, /* TMDS PLL optimization */
2601 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8d), 0x18 }, /* equaliser */
2602 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8e), 0x34 }, /* equaliser */
2603 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x93), 0x88 }, /* equaliser */
2604 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x94), 0x2e }, /* equaliser */
2605 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x96), 0x00 }, /* enable automatic EQ changing */
2606
2607 /* set ADI recommended settings for digitizer */
2608 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 17. */
2609 { ADV7604_REG(ADV7604_PAGE_AFE, 0x12), 0x7b }, /* ADC noise shaping filter controls */
2610 { ADV7604_REG(ADV7604_PAGE_AFE, 0x0c), 0x1f }, /* CP core gain controls */
2611 { ADV7604_REG(ADV7604_PAGE_CP, 0x3e), 0x04 }, /* CP core pre-gain control */
2612 { ADV7604_REG(ADV7604_PAGE_CP, 0xc3), 0x39 }, /* CP coast control. Graphics mode */
2613 { ADV7604_REG(ADV7604_PAGE_CP, 0x40), 0x5c }, /* CP core pre-gain control. Graphics mode */
2614
2615 { ADV7604_REG_SEQ_TERM, 0 },
2616};
2617
2618static const struct adv7604_reg_seq adv7604_recommended_settings_hdmi[] = {
2619 /* set ADI recommended settings for HDMI: */
2620 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 4. */
2621 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x0d), 0x84 }, /* HDMI filter optimization */
2622 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x3d), 0x10 }, /* DDC bus active pull-up control */
2623 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x3e), 0x39 }, /* TMDS PLL optimization */
2624 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x4e), 0x3b }, /* TMDS PLL optimization */
2625 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x57), 0xb6 }, /* TMDS PLL optimization */
2626 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x58), 0x03 }, /* TMDS PLL optimization */
2627 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8d), 0x18 }, /* equaliser */
2628 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8e), 0x34 }, /* equaliser */
2629 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x93), 0x8b }, /* equaliser */
2630 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x94), 0x2d }, /* equaliser */
2631 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x96), 0x01 }, /* enable automatic EQ changing */
2632
2633 /* reset ADI recommended settings for digitizer */
2634 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 17. */
2635 { ADV7604_REG(ADV7604_PAGE_AFE, 0x12), 0xfb }, /* ADC noise shaping filter controls */
2636 { ADV7604_REG(ADV7604_PAGE_AFE, 0x0c), 0x0d }, /* CP core gain controls */
2637
2638 { ADV7604_REG_SEQ_TERM, 0 },
2639};
2640
2641static const struct adv7604_reg_seq adv7611_recommended_settings_hdmi[] = {
2642 { ADV7604_REG(ADV7604_PAGE_CP, 0x6c), 0x00 },
2643 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x6f), 0x0c },
2644 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x87), 0x70 },
2645 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x57), 0xda },
2646 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x58), 0x01 },
2647 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x03), 0x98 },
2648 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x4c), 0x44 },
2649 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8d), 0x04 },
2650 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8e), 0x1e },
2651
2652 { ADV7604_REG_SEQ_TERM, 0 },
2653};
2654
2655static const struct adv7604_chip_info adv7604_chip_info[] = {
2656 [ADV7604] = {
2657 .type = ADV7604,
2658 .has_afe = true,
c784b1e2 2659 .max_port = ADV7604_PAD_VGA_COMP,
d42010a1
LPC
2660 .num_dv_ports = 4,
2661 .edid_enable_reg = 0x77,
2662 .edid_status_reg = 0x7d,
2663 .lcf_reg = 0xb3,
2664 .tdms_lock_mask = 0xe0,
2665 .cable_det_mask = 0x1e,
2666 .fmt_change_digital_mask = 0xc1,
539b33b0
LP
2667 .formats = adv7604_formats,
2668 .nformats = ARRAY_SIZE(adv7604_formats),
d42010a1
LPC
2669 .set_termination = adv7604_set_termination,
2670 .setup_irqs = adv7604_setup_irqs,
2671 .read_hdmi_pixelclock = adv7604_read_hdmi_pixelclock,
2672 .read_cable_det = adv7604_read_cable_det,
2673 .recommended_settings = {
2674 [0] = adv7604_recommended_settings_afe,
2675 [1] = adv7604_recommended_settings_hdmi,
2676 },
2677 .num_recommended_settings = {
2678 [0] = ARRAY_SIZE(adv7604_recommended_settings_afe),
2679 [1] = ARRAY_SIZE(adv7604_recommended_settings_hdmi),
2680 },
2681 .page_mask = BIT(ADV7604_PAGE_IO) | BIT(ADV7604_PAGE_AVLINK) |
2682 BIT(ADV7604_PAGE_CEC) | BIT(ADV7604_PAGE_INFOFRAME) |
2683 BIT(ADV7604_PAGE_ESDP) | BIT(ADV7604_PAGE_DPP) |
2684 BIT(ADV7604_PAGE_AFE) | BIT(ADV7604_PAGE_REP) |
2685 BIT(ADV7604_PAGE_EDID) | BIT(ADV7604_PAGE_HDMI) |
2686 BIT(ADV7604_PAGE_TEST) | BIT(ADV7604_PAGE_CP) |
2687 BIT(ADV7604_PAGE_VDP),
2688 },
2689 [ADV7611] = {
2690 .type = ADV7611,
2691 .has_afe = false,
c784b1e2 2692 .max_port = ADV7604_PAD_HDMI_PORT_A,
d42010a1
LPC
2693 .num_dv_ports = 1,
2694 .edid_enable_reg = 0x74,
2695 .edid_status_reg = 0x76,
2696 .lcf_reg = 0xa3,
2697 .tdms_lock_mask = 0x43,
2698 .cable_det_mask = 0x01,
2699 .fmt_change_digital_mask = 0x03,
539b33b0
LP
2700 .formats = adv7611_formats,
2701 .nformats = ARRAY_SIZE(adv7611_formats),
d42010a1
LPC
2702 .set_termination = adv7611_set_termination,
2703 .setup_irqs = adv7611_setup_irqs,
2704 .read_hdmi_pixelclock = adv7611_read_hdmi_pixelclock,
2705 .read_cable_det = adv7611_read_cable_det,
2706 .recommended_settings = {
2707 [1] = adv7611_recommended_settings_hdmi,
2708 },
2709 .num_recommended_settings = {
2710 [1] = ARRAY_SIZE(adv7611_recommended_settings_hdmi),
2711 },
2712 .page_mask = BIT(ADV7604_PAGE_IO) | BIT(ADV7604_PAGE_CEC) |
2713 BIT(ADV7604_PAGE_INFOFRAME) | BIT(ADV7604_PAGE_AFE) |
2714 BIT(ADV7604_PAGE_REP) | BIT(ADV7604_PAGE_EDID) |
2715 BIT(ADV7604_PAGE_HDMI) | BIT(ADV7604_PAGE_CP),
2716 },
2717};
2718
54450f59
HV
2719static int adv7604_probe(struct i2c_client *client,
2720 const struct i2c_device_id *id)
2721{
591b72fe
HV
2722 static const struct v4l2_dv_timings cea640x480 =
2723 V4L2_DV_BT_CEA_640X480P59_94;
54450f59
HV
2724 struct adv7604_state *state;
2725 struct adv7604_platform_data *pdata = client->dev.platform_data;
2726 struct v4l2_ctrl_handler *hdl;
2727 struct v4l2_subdev *sd;
c784b1e2 2728 unsigned int i;
d42010a1 2729 u16 val;
54450f59
HV
2730 int err;
2731
2732 /* Check if the adapter supports the needed features */
2733 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
2734 return -EIO;
2735 v4l_dbg(1, debug, client, "detecting adv7604 client on address 0x%x\n",
2736 client->addr << 1);
2737
c02b211d 2738 state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
54450f59
HV
2739 if (!state) {
2740 v4l_err(client, "Could not allocate adv7604_state memory!\n");
2741 return -ENOMEM;
2742 }
2743
d42010a1
LPC
2744 state->info = &adv7604_chip_info[id->driver_data];
2745
25a64ac9
MR
2746 /* initialize variables */
2747 state->restart_stdi_once = true;
ff4f80fd 2748 state->selected_input = ~0;
25a64ac9 2749
54450f59
HV
2750 /* platform data */
2751 if (!pdata) {
2752 v4l_err(client, "No platform data!\n");
c02b211d 2753 return -ENODEV;
54450f59 2754 }
591b72fe
HV
2755 state->pdata = *pdata;
2756 state->timings = cea640x480;
539b33b0 2757 state->format = adv7604_format_info(state, V4L2_MBUS_FMT_YUYV8_2X8);
54450f59
HV
2758
2759 sd = &state->sd;
2760 v4l2_i2c_subdev_init(sd, client, &adv7604_ops);
d42010a1
LPC
2761 snprintf(sd->name, sizeof(sd->name), "%s %d-%04x",
2762 id->name, i2c_adapter_id(client->adapter),
2763 client->addr);
54450f59 2764 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
54450f59 2765
d42010a1
LPC
2766 /*
2767 * Verify that the chip is present. On ADV7604 the RD_INFO register only
2768 * identifies the revision, while on ADV7611 it identifies the model as
2769 * well. Use the HDMI slave address on ADV7604 and RD_INFO on ADV7611.
2770 */
2771 if (state->info->type == ADV7604) {
2772 val = adv_smbus_read_byte_data_check(client, 0xfb, false);
2773 if (val != 0x68) {
2774 v4l2_info(sd, "not an adv7604 on address 0x%x\n",
2775 client->addr << 1);
2776 return -ENODEV;
2777 }
2778 } else {
2779 val = (adv_smbus_read_byte_data_check(client, 0xea, false) << 8)
2780 | (adv_smbus_read_byte_data_check(client, 0xeb, false) << 0);
2781 if (val != 0x2051) {
2782 v4l2_info(sd, "not an adv7611 on address 0x%x\n",
2783 client->addr << 1);
2784 return -ENODEV;
2785 }
54450f59
HV
2786 }
2787
2788 /* control handlers */
2789 hdl = &state->hdl;
d42010a1 2790 v4l2_ctrl_handler_init(hdl, adv7604_has_afe(state) ? 9 : 8);
54450f59
HV
2791
2792 v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
2793 V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
2794 v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
2795 V4L2_CID_CONTRAST, 0, 255, 1, 128);
2796 v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
2797 V4L2_CID_SATURATION, 0, 255, 1, 128);
2798 v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
2799 V4L2_CID_HUE, 0, 128, 1, 0);
2800
2801 /* private controls */
2802 state->detect_tx_5v_ctrl = v4l2_ctrl_new_std(hdl, NULL,
d42010a1
LPC
2803 V4L2_CID_DV_RX_POWER_PRESENT, 0,
2804 (1 << state->info->num_dv_ports) - 1, 0, 0);
54450f59
HV
2805 state->rgb_quantization_range_ctrl =
2806 v4l2_ctrl_new_std_menu(hdl, &adv7604_ctrl_ops,
2807 V4L2_CID_DV_RX_RGB_RANGE, V4L2_DV_RGB_RANGE_FULL,
2808 0, V4L2_DV_RGB_RANGE_AUTO);
54450f59
HV
2809
2810 /* custom controls */
d42010a1
LPC
2811 if (adv7604_has_afe(state))
2812 state->analog_sampling_phase_ctrl =
2813 v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_analog_sampling_phase, NULL);
54450f59
HV
2814 state->free_run_color_manual_ctrl =
2815 v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_free_run_color_manual, NULL);
54450f59
HV
2816 state->free_run_color_ctrl =
2817 v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_free_run_color, NULL);
54450f59
HV
2818
2819 sd->ctrl_handler = hdl;
2820 if (hdl->error) {
2821 err = hdl->error;
2822 goto err_hdl;
2823 }
8c0eadb8
HV
2824 state->detect_tx_5v_ctrl->is_private = true;
2825 state->rgb_quantization_range_ctrl->is_private = true;
d42010a1
LPC
2826 if (adv7604_has_afe(state))
2827 state->analog_sampling_phase_ctrl->is_private = true;
8c0eadb8
HV
2828 state->free_run_color_manual_ctrl->is_private = true;
2829 state->free_run_color_ctrl->is_private = true;
2830
54450f59
HV
2831 if (adv7604_s_detect_tx_5v_ctrl(sd)) {
2832 err = -ENODEV;
2833 goto err_hdl;
2834 }
2835
54450f59
HV
2836 state->i2c_cec = adv7604_dummy_client(sd, pdata->i2c_cec, 0xf4);
2837 state->i2c_infoframe = adv7604_dummy_client(sd, pdata->i2c_infoframe, 0xf5);
54450f59
HV
2838 state->i2c_afe = adv7604_dummy_client(sd, pdata->i2c_afe, 0xf8);
2839 state->i2c_repeater = adv7604_dummy_client(sd, pdata->i2c_repeater, 0xf9);
2840 state->i2c_edid = adv7604_dummy_client(sd, pdata->i2c_edid, 0xfa);
2841 state->i2c_hdmi = adv7604_dummy_client(sd, pdata->i2c_hdmi, 0xfb);
54450f59 2842 state->i2c_cp = adv7604_dummy_client(sd, pdata->i2c_cp, 0xfd);
d42010a1 2843 if (!state->i2c_cec || !state->i2c_infoframe || !state->i2c_afe ||
54450f59 2844 !state->i2c_repeater || !state->i2c_edid || !state->i2c_hdmi ||
d42010a1 2845 !state->i2c_cp) {
54450f59 2846 err = -ENOMEM;
d42010a1 2847 v4l2_err(sd, "failed to create digital i2c clients\n");
54450f59
HV
2848 goto err_i2c;
2849 }
2850
d42010a1
LPC
2851 if (adv7604_has_afe(state)) {
2852 state->i2c_avlink = adv7604_dummy_client(sd, pdata->i2c_avlink, 0xf3);
2853 state->i2c_esdp = adv7604_dummy_client(sd, pdata->i2c_esdp, 0xf6);
2854 state->i2c_dpp = adv7604_dummy_client(sd, pdata->i2c_dpp, 0xf7);
2855 state->i2c_test = adv7604_dummy_client(sd, pdata->i2c_test, 0xfc);
2856 state->i2c_vdp = adv7604_dummy_client(sd, pdata->i2c_vdp, 0xfe);
2857 if (!state->i2c_avlink || !state->i2c_esdp || !state->i2c_dpp ||
2858 !state->i2c_test || !state->i2c_vdp) {
2859 err = -ENOMEM;
2860 v4l2_err(sd, "failed to create analog i2c clients\n");
2861 goto err_i2c;
2862 }
2863 }
54450f59
HV
2864 /* work queues */
2865 state->work_queues = create_singlethread_workqueue(client->name);
2866 if (!state->work_queues) {
2867 v4l2_err(sd, "Could not create work queue\n");
2868 err = -ENOMEM;
2869 goto err_i2c;
2870 }
2871
2872 INIT_DELAYED_WORK(&state->delayed_work_enable_hotplug,
2873 adv7604_delayed_work_enable_hotplug);
2874
c784b1e2
LP
2875 state->source_pad = state->info->num_dv_ports
2876 + (state->info->has_afe ? 2 : 0);
2877 for (i = 0; i < state->source_pad; ++i)
2878 state->pads[i].flags = MEDIA_PAD_FL_SINK;
2879 state->pads[state->source_pad].flags = MEDIA_PAD_FL_SOURCE;
2880
2881 err = media_entity_init(&sd->entity, state->source_pad + 1,
2882 state->pads, 0);
54450f59
HV
2883 if (err)
2884 goto err_work_queues;
2885
2886 err = adv7604_core_init(sd);
2887 if (err)
2888 goto err_entity;
2889 v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
2890 client->addr << 1, client->adapter->name);
bedc3939
LPC
2891
2892 err = v4l2_async_register_subdev(sd);
2893 if (err)
2894 goto err_entity;
2895
54450f59
HV
2896 return 0;
2897
2898err_entity:
2899 media_entity_cleanup(&sd->entity);
2900err_work_queues:
2901 cancel_delayed_work(&state->delayed_work_enable_hotplug);
2902 destroy_workqueue(state->work_queues);
2903err_i2c:
2904 adv7604_unregister_clients(state);
2905err_hdl:
2906 v4l2_ctrl_handler_free(hdl);
54450f59
HV
2907 return err;
2908}
2909
2910/* ----------------------------------------------------------------------- */
2911
2912static int adv7604_remove(struct i2c_client *client)
2913{
2914 struct v4l2_subdev *sd = i2c_get_clientdata(client);
2915 struct adv7604_state *state = to_state(sd);
2916
2917 cancel_delayed_work(&state->delayed_work_enable_hotplug);
2918 destroy_workqueue(state->work_queues);
bedc3939 2919 v4l2_async_unregister_subdev(sd);
54450f59
HV
2920 v4l2_device_unregister_subdev(sd);
2921 media_entity_cleanup(&sd->entity);
2922 adv7604_unregister_clients(to_state(sd));
2923 v4l2_ctrl_handler_free(sd->ctrl_handler);
54450f59
HV
2924 return 0;
2925}
2926
2927/* ----------------------------------------------------------------------- */
2928
2929static struct i2c_device_id adv7604_id[] = {
d42010a1
LPC
2930 { "adv7604", ADV7604 },
2931 { "adv7611", ADV7611 },
54450f59
HV
2932 { }
2933};
2934MODULE_DEVICE_TABLE(i2c, adv7604_id);
2935
2936static struct i2c_driver adv7604_driver = {
2937 .driver = {
2938 .owner = THIS_MODULE,
2939 .name = "adv7604",
2940 },
2941 .probe = adv7604_probe,
2942 .remove = adv7604_remove,
2943 .id_table = adv7604_id,
2944};
2945
2946module_i2c_driver(adv7604_driver);