2 * adv7604 - Analog Devices ADV7604 video decoder driver
4 * Copyright 2012 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
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.
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
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
30 #include <linux/delay.h>
31 #include <linux/gpio/consumer.h>
32 #include <linux/i2c.h>
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/slab.h>
36 #include <linux/v4l2-dv-timings.h>
37 #include <linux/videodev2.h>
38 #include <linux/workqueue.h>
40 #include <media/adv7604.h>
41 #include <media/v4l2-ctrls.h>
42 #include <media/v4l2-device.h>
43 #include <media/v4l2-dv-timings.h>
44 #include <media/v4l2-of.h>
47 module_param(debug, int, 0644);
48 MODULE_PARM_DESC(debug, "debug level (0-2)");
50 MODULE_DESCRIPTION("Analog Devices ADV7604 video decoder driver");
51 MODULE_AUTHOR("Hans Verkuil <hans.verkuil@cisco.com>");
52 MODULE_AUTHOR("Mats Randgaard <mats.randgaard@cisco.com>");
53 MODULE_LICENSE("GPL");
55 /* ADV7604 system clock frequency */
56 #define ADV7604_fsc (28636360)
58 #define ADV7604_RGB_OUT (1 << 1)
60 #define ADV7604_OP_FORMAT_SEL_8BIT (0 << 0)
61 #define ADV7604_OP_FORMAT_SEL_10BIT (1 << 0)
62 #define ADV7604_OP_FORMAT_SEL_12BIT (2 << 0)
64 #define ADV7604_OP_MODE_SEL_SDR_422 (0 << 5)
65 #define ADV7604_OP_MODE_SEL_DDR_422 (1 << 5)
66 #define ADV7604_OP_MODE_SEL_SDR_444 (2 << 5)
67 #define ADV7604_OP_MODE_SEL_DDR_444 (3 << 5)
68 #define ADV7604_OP_MODE_SEL_SDR_422_2X (4 << 5)
69 #define ADV7604_OP_MODE_SEL_ADI_CM (5 << 5)
71 #define ADV7604_OP_CH_SEL_GBR (0 << 5)
72 #define ADV7604_OP_CH_SEL_GRB (1 << 5)
73 #define ADV7604_OP_CH_SEL_BGR (2 << 5)
74 #define ADV7604_OP_CH_SEL_RGB (3 << 5)
75 #define ADV7604_OP_CH_SEL_BRG (4 << 5)
76 #define ADV7604_OP_CH_SEL_RBG (5 << 5)
78 #define ADV7604_OP_SWAP_CB_CR (1 << 0)
85 struct adv7604_reg_seq {
90 struct adv7604_format_info {
98 struct adv7604_chip_info {
99 enum adv7604_type type;
102 unsigned int max_port;
103 unsigned int num_dv_ports;
105 unsigned int edid_enable_reg;
106 unsigned int edid_status_reg;
107 unsigned int lcf_reg;
109 unsigned int cable_det_mask;
110 unsigned int tdms_lock_mask;
111 unsigned int fmt_change_digital_mask;
113 const struct adv7604_format_info *formats;
114 unsigned int nformats;
116 void (*set_termination)(struct v4l2_subdev *sd, bool enable);
117 void (*setup_irqs)(struct v4l2_subdev *sd);
118 unsigned int (*read_hdmi_pixelclock)(struct v4l2_subdev *sd);
119 unsigned int (*read_cable_det)(struct v4l2_subdev *sd);
121 /* 0 = AFE, 1 = HDMI */
122 const struct adv7604_reg_seq *recommended_settings[2];
123 unsigned int num_recommended_settings[2];
125 unsigned long page_mask;
129 **********************************************************************
131 * Arrays with configuration parameters for the ADV7604
133 **********************************************************************
136 struct adv7604_state {
137 const struct adv7604_chip_info *info;
138 struct adv7604_platform_data pdata;
140 struct gpio_desc *hpd_gpio[4];
142 struct v4l2_subdev sd;
143 struct media_pad pads[ADV7604_PAD_MAX];
144 unsigned int source_pad;
146 struct v4l2_ctrl_handler hdl;
148 enum adv7604_pad selected_input;
150 struct v4l2_dv_timings timings;
151 const struct adv7604_format_info *format;
159 struct v4l2_fract aspect_ratio;
160 u32 rgb_quantization_range;
161 struct workqueue_struct *work_queues;
162 struct delayed_work delayed_work_enable_hotplug;
163 bool restart_stdi_once;
166 struct i2c_client *i2c_clients[ADV7604_PAGE_MAX];
169 struct v4l2_ctrl *detect_tx_5v_ctrl;
170 struct v4l2_ctrl *analog_sampling_phase_ctrl;
171 struct v4l2_ctrl *free_run_color_manual_ctrl;
172 struct v4l2_ctrl *free_run_color_ctrl;
173 struct v4l2_ctrl *rgb_quantization_range_ctrl;
176 static bool adv7604_has_afe(struct adv7604_state *state)
178 return state->info->has_afe;
181 /* Supported CEA and DMT timings */
182 static const struct v4l2_dv_timings adv7604_timings[] = {
183 V4L2_DV_BT_CEA_720X480P59_94,
184 V4L2_DV_BT_CEA_720X576P50,
185 V4L2_DV_BT_CEA_1280X720P24,
186 V4L2_DV_BT_CEA_1280X720P25,
187 V4L2_DV_BT_CEA_1280X720P50,
188 V4L2_DV_BT_CEA_1280X720P60,
189 V4L2_DV_BT_CEA_1920X1080P24,
190 V4L2_DV_BT_CEA_1920X1080P25,
191 V4L2_DV_BT_CEA_1920X1080P30,
192 V4L2_DV_BT_CEA_1920X1080P50,
193 V4L2_DV_BT_CEA_1920X1080P60,
195 /* sorted by DMT ID */
196 V4L2_DV_BT_DMT_640X350P85,
197 V4L2_DV_BT_DMT_640X400P85,
198 V4L2_DV_BT_DMT_720X400P85,
199 V4L2_DV_BT_DMT_640X480P60,
200 V4L2_DV_BT_DMT_640X480P72,
201 V4L2_DV_BT_DMT_640X480P75,
202 V4L2_DV_BT_DMT_640X480P85,
203 V4L2_DV_BT_DMT_800X600P56,
204 V4L2_DV_BT_DMT_800X600P60,
205 V4L2_DV_BT_DMT_800X600P72,
206 V4L2_DV_BT_DMT_800X600P75,
207 V4L2_DV_BT_DMT_800X600P85,
208 V4L2_DV_BT_DMT_848X480P60,
209 V4L2_DV_BT_DMT_1024X768P60,
210 V4L2_DV_BT_DMT_1024X768P70,
211 V4L2_DV_BT_DMT_1024X768P75,
212 V4L2_DV_BT_DMT_1024X768P85,
213 V4L2_DV_BT_DMT_1152X864P75,
214 V4L2_DV_BT_DMT_1280X768P60_RB,
215 V4L2_DV_BT_DMT_1280X768P60,
216 V4L2_DV_BT_DMT_1280X768P75,
217 V4L2_DV_BT_DMT_1280X768P85,
218 V4L2_DV_BT_DMT_1280X800P60_RB,
219 V4L2_DV_BT_DMT_1280X800P60,
220 V4L2_DV_BT_DMT_1280X800P75,
221 V4L2_DV_BT_DMT_1280X800P85,
222 V4L2_DV_BT_DMT_1280X960P60,
223 V4L2_DV_BT_DMT_1280X960P85,
224 V4L2_DV_BT_DMT_1280X1024P60,
225 V4L2_DV_BT_DMT_1280X1024P75,
226 V4L2_DV_BT_DMT_1280X1024P85,
227 V4L2_DV_BT_DMT_1360X768P60,
228 V4L2_DV_BT_DMT_1400X1050P60_RB,
229 V4L2_DV_BT_DMT_1400X1050P60,
230 V4L2_DV_BT_DMT_1400X1050P75,
231 V4L2_DV_BT_DMT_1400X1050P85,
232 V4L2_DV_BT_DMT_1440X900P60_RB,
233 V4L2_DV_BT_DMT_1440X900P60,
234 V4L2_DV_BT_DMT_1600X1200P60,
235 V4L2_DV_BT_DMT_1680X1050P60_RB,
236 V4L2_DV_BT_DMT_1680X1050P60,
237 V4L2_DV_BT_DMT_1792X1344P60,
238 V4L2_DV_BT_DMT_1856X1392P60,
239 V4L2_DV_BT_DMT_1920X1200P60_RB,
240 V4L2_DV_BT_DMT_1366X768P60_RB,
241 V4L2_DV_BT_DMT_1366X768P60,
242 V4L2_DV_BT_DMT_1920X1080P60,
246 struct adv7604_video_standards {
247 struct v4l2_dv_timings timings;
252 /* sorted by number of lines */
253 static const struct adv7604_video_standards adv7604_prim_mode_comp[] = {
254 /* { V4L2_DV_BT_CEA_720X480P59_94, 0x0a, 0x00 }, TODO flickering */
255 { V4L2_DV_BT_CEA_720X576P50, 0x0b, 0x00 },
256 { V4L2_DV_BT_CEA_1280X720P50, 0x19, 0x01 },
257 { V4L2_DV_BT_CEA_1280X720P60, 0x19, 0x00 },
258 { V4L2_DV_BT_CEA_1920X1080P24, 0x1e, 0x04 },
259 { V4L2_DV_BT_CEA_1920X1080P25, 0x1e, 0x03 },
260 { V4L2_DV_BT_CEA_1920X1080P30, 0x1e, 0x02 },
261 { V4L2_DV_BT_CEA_1920X1080P50, 0x1e, 0x01 },
262 { V4L2_DV_BT_CEA_1920X1080P60, 0x1e, 0x00 },
263 /* TODO add 1920x1080P60_RB (CVT timing) */
267 /* sorted by number of lines */
268 static const struct adv7604_video_standards adv7604_prim_mode_gr[] = {
269 { V4L2_DV_BT_DMT_640X480P60, 0x08, 0x00 },
270 { V4L2_DV_BT_DMT_640X480P72, 0x09, 0x00 },
271 { V4L2_DV_BT_DMT_640X480P75, 0x0a, 0x00 },
272 { V4L2_DV_BT_DMT_640X480P85, 0x0b, 0x00 },
273 { V4L2_DV_BT_DMT_800X600P56, 0x00, 0x00 },
274 { V4L2_DV_BT_DMT_800X600P60, 0x01, 0x00 },
275 { V4L2_DV_BT_DMT_800X600P72, 0x02, 0x00 },
276 { V4L2_DV_BT_DMT_800X600P75, 0x03, 0x00 },
277 { V4L2_DV_BT_DMT_800X600P85, 0x04, 0x00 },
278 { V4L2_DV_BT_DMT_1024X768P60, 0x0c, 0x00 },
279 { V4L2_DV_BT_DMT_1024X768P70, 0x0d, 0x00 },
280 { V4L2_DV_BT_DMT_1024X768P75, 0x0e, 0x00 },
281 { V4L2_DV_BT_DMT_1024X768P85, 0x0f, 0x00 },
282 { V4L2_DV_BT_DMT_1280X1024P60, 0x05, 0x00 },
283 { V4L2_DV_BT_DMT_1280X1024P75, 0x06, 0x00 },
284 { V4L2_DV_BT_DMT_1360X768P60, 0x12, 0x00 },
285 { V4L2_DV_BT_DMT_1366X768P60, 0x13, 0x00 },
286 { V4L2_DV_BT_DMT_1400X1050P60, 0x14, 0x00 },
287 { V4L2_DV_BT_DMT_1400X1050P75, 0x15, 0x00 },
288 { V4L2_DV_BT_DMT_1600X1200P60, 0x16, 0x00 }, /* TODO not tested */
289 /* TODO add 1600X1200P60_RB (not a DMT timing) */
290 { V4L2_DV_BT_DMT_1680X1050P60, 0x18, 0x00 },
291 { V4L2_DV_BT_DMT_1920X1200P60_RB, 0x19, 0x00 }, /* TODO not tested */
295 /* sorted by number of lines */
296 static const struct adv7604_video_standards adv7604_prim_mode_hdmi_comp[] = {
297 { V4L2_DV_BT_CEA_720X480P59_94, 0x0a, 0x00 },
298 { V4L2_DV_BT_CEA_720X576P50, 0x0b, 0x00 },
299 { V4L2_DV_BT_CEA_1280X720P50, 0x13, 0x01 },
300 { V4L2_DV_BT_CEA_1280X720P60, 0x13, 0x00 },
301 { V4L2_DV_BT_CEA_1920X1080P24, 0x1e, 0x04 },
302 { V4L2_DV_BT_CEA_1920X1080P25, 0x1e, 0x03 },
303 { V4L2_DV_BT_CEA_1920X1080P30, 0x1e, 0x02 },
304 { V4L2_DV_BT_CEA_1920X1080P50, 0x1e, 0x01 },
305 { V4L2_DV_BT_CEA_1920X1080P60, 0x1e, 0x00 },
309 /* sorted by number of lines */
310 static const struct adv7604_video_standards adv7604_prim_mode_hdmi_gr[] = {
311 { V4L2_DV_BT_DMT_640X480P60, 0x08, 0x00 },
312 { V4L2_DV_BT_DMT_640X480P72, 0x09, 0x00 },
313 { V4L2_DV_BT_DMT_640X480P75, 0x0a, 0x00 },
314 { V4L2_DV_BT_DMT_640X480P85, 0x0b, 0x00 },
315 { V4L2_DV_BT_DMT_800X600P56, 0x00, 0x00 },
316 { V4L2_DV_BT_DMT_800X600P60, 0x01, 0x00 },
317 { V4L2_DV_BT_DMT_800X600P72, 0x02, 0x00 },
318 { V4L2_DV_BT_DMT_800X600P75, 0x03, 0x00 },
319 { V4L2_DV_BT_DMT_800X600P85, 0x04, 0x00 },
320 { V4L2_DV_BT_DMT_1024X768P60, 0x0c, 0x00 },
321 { V4L2_DV_BT_DMT_1024X768P70, 0x0d, 0x00 },
322 { V4L2_DV_BT_DMT_1024X768P75, 0x0e, 0x00 },
323 { V4L2_DV_BT_DMT_1024X768P85, 0x0f, 0x00 },
324 { V4L2_DV_BT_DMT_1280X1024P60, 0x05, 0x00 },
325 { V4L2_DV_BT_DMT_1280X1024P75, 0x06, 0x00 },
329 /* ----------------------------------------------------------------------- */
331 static inline struct adv7604_state *to_state(struct v4l2_subdev *sd)
333 return container_of(sd, struct adv7604_state, sd);
336 static inline unsigned htotal(const struct v4l2_bt_timings *t)
338 return V4L2_DV_BT_FRAME_WIDTH(t);
341 static inline unsigned vtotal(const struct v4l2_bt_timings *t)
343 return V4L2_DV_BT_FRAME_HEIGHT(t);
346 /* ----------------------------------------------------------------------- */
348 static s32 adv_smbus_read_byte_data_check(struct i2c_client *client,
349 u8 command, bool check)
351 union i2c_smbus_data data;
353 if (!i2c_smbus_xfer(client->adapter, client->addr, client->flags,
354 I2C_SMBUS_READ, command,
355 I2C_SMBUS_BYTE_DATA, &data))
358 v4l_err(client, "error reading %02x, %02x\n",
359 client->addr, command);
363 static s32 adv_smbus_read_byte_data(struct adv7604_state *state,
364 enum adv7604_page page, u8 command)
366 return adv_smbus_read_byte_data_check(state->i2c_clients[page],
370 static s32 adv_smbus_write_byte_data(struct adv7604_state *state,
371 enum adv7604_page page, u8 command,
374 struct i2c_client *client = state->i2c_clients[page];
375 union i2c_smbus_data data;
380 for (i = 0; i < 3; i++) {
381 err = i2c_smbus_xfer(client->adapter, client->addr,
383 I2C_SMBUS_WRITE, command,
384 I2C_SMBUS_BYTE_DATA, &data);
389 v4l_err(client, "error writing %02x, %02x, %02x\n",
390 client->addr, command, value);
394 static s32 adv_smbus_write_i2c_block_data(struct adv7604_state *state,
395 enum adv7604_page page, u8 command,
396 unsigned length, const u8 *values)
398 struct i2c_client *client = state->i2c_clients[page];
399 union i2c_smbus_data data;
401 if (length > I2C_SMBUS_BLOCK_MAX)
402 length = I2C_SMBUS_BLOCK_MAX;
403 data.block[0] = length;
404 memcpy(data.block + 1, values, length);
405 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
406 I2C_SMBUS_WRITE, command,
407 I2C_SMBUS_I2C_BLOCK_DATA, &data);
410 /* ----------------------------------------------------------------------- */
412 static inline int io_read(struct v4l2_subdev *sd, u8 reg)
414 struct adv7604_state *state = to_state(sd);
416 return adv_smbus_read_byte_data(state, ADV7604_PAGE_IO, reg);
419 static inline int io_write(struct v4l2_subdev *sd, u8 reg, u8 val)
421 struct adv7604_state *state = to_state(sd);
423 return adv_smbus_write_byte_data(state, ADV7604_PAGE_IO, reg, val);
426 static inline int io_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
428 return io_write(sd, reg, (io_read(sd, reg) & ~mask) | val);
431 static inline int avlink_read(struct v4l2_subdev *sd, u8 reg)
433 struct adv7604_state *state = to_state(sd);
435 return adv_smbus_read_byte_data(state, ADV7604_PAGE_AVLINK, reg);
438 static inline int avlink_write(struct v4l2_subdev *sd, u8 reg, u8 val)
440 struct adv7604_state *state = to_state(sd);
442 return adv_smbus_write_byte_data(state, ADV7604_PAGE_AVLINK, reg, val);
445 static inline int cec_read(struct v4l2_subdev *sd, u8 reg)
447 struct adv7604_state *state = to_state(sd);
449 return adv_smbus_read_byte_data(state, ADV7604_PAGE_CEC, reg);
452 static inline int cec_write(struct v4l2_subdev *sd, u8 reg, u8 val)
454 struct adv7604_state *state = to_state(sd);
456 return adv_smbus_write_byte_data(state, ADV7604_PAGE_CEC, reg, val);
459 static inline int infoframe_read(struct v4l2_subdev *sd, u8 reg)
461 struct adv7604_state *state = to_state(sd);
463 return adv_smbus_read_byte_data(state, ADV7604_PAGE_INFOFRAME, reg);
466 static inline int infoframe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
468 struct adv7604_state *state = to_state(sd);
470 return adv_smbus_write_byte_data(state, ADV7604_PAGE_INFOFRAME,
474 static inline int afe_read(struct v4l2_subdev *sd, u8 reg)
476 struct adv7604_state *state = to_state(sd);
478 return adv_smbus_read_byte_data(state, ADV7604_PAGE_AFE, reg);
481 static inline int afe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
483 struct adv7604_state *state = to_state(sd);
485 return adv_smbus_write_byte_data(state, ADV7604_PAGE_AFE, reg, val);
488 static inline int rep_read(struct v4l2_subdev *sd, u8 reg)
490 struct adv7604_state *state = to_state(sd);
492 return adv_smbus_read_byte_data(state, ADV7604_PAGE_REP, reg);
495 static inline int rep_write(struct v4l2_subdev *sd, u8 reg, u8 val)
497 struct adv7604_state *state = to_state(sd);
499 return adv_smbus_write_byte_data(state, ADV7604_PAGE_REP, reg, val);
502 static inline int rep_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
504 return rep_write(sd, reg, (rep_read(sd, reg) & ~mask) | val);
507 static inline int edid_read(struct v4l2_subdev *sd, u8 reg)
509 struct adv7604_state *state = to_state(sd);
511 return adv_smbus_read_byte_data(state, ADV7604_PAGE_EDID, reg);
514 static inline int edid_write(struct v4l2_subdev *sd, u8 reg, u8 val)
516 struct adv7604_state *state = to_state(sd);
518 return adv_smbus_write_byte_data(state, ADV7604_PAGE_EDID, reg, val);
521 static inline int edid_write_block(struct v4l2_subdev *sd,
522 unsigned len, const u8 *val)
524 struct adv7604_state *state = to_state(sd);
528 v4l2_dbg(2, debug, sd, "%s: write EDID block (%d byte)\n", __func__, len);
530 for (i = 0; !err && i < len; i += I2C_SMBUS_BLOCK_MAX)
531 err = adv_smbus_write_i2c_block_data(state, ADV7604_PAGE_EDID,
532 i, I2C_SMBUS_BLOCK_MAX, val + i);
536 static void adv7604_set_hpd(struct adv7604_state *state, unsigned int hpd)
540 for (i = 0; i < state->info->num_dv_ports; ++i) {
541 if (IS_ERR(state->hpd_gpio[i]))
544 gpiod_set_value_cansleep(state->hpd_gpio[i], hpd & BIT(i));
547 v4l2_subdev_notify(&state->sd, ADV7604_HOTPLUG, &hpd);
550 static void adv7604_delayed_work_enable_hotplug(struct work_struct *work)
552 struct delayed_work *dwork = to_delayed_work(work);
553 struct adv7604_state *state = container_of(dwork, struct adv7604_state,
554 delayed_work_enable_hotplug);
555 struct v4l2_subdev *sd = &state->sd;
557 v4l2_dbg(2, debug, sd, "%s: enable hotplug\n", __func__);
559 adv7604_set_hpd(state, state->edid.present);
562 static inline int hdmi_read(struct v4l2_subdev *sd, u8 reg)
564 struct adv7604_state *state = to_state(sd);
566 return adv_smbus_read_byte_data(state, ADV7604_PAGE_HDMI, reg);
569 static u16 hdmi_read16(struct v4l2_subdev *sd, u8 reg, u16 mask)
571 return ((hdmi_read(sd, reg) << 8) | hdmi_read(sd, reg + 1)) & mask;
574 static inline int hdmi_write(struct v4l2_subdev *sd, u8 reg, u8 val)
576 struct adv7604_state *state = to_state(sd);
578 return adv_smbus_write_byte_data(state, ADV7604_PAGE_HDMI, reg, val);
581 static inline int hdmi_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
583 return hdmi_write(sd, reg, (hdmi_read(sd, reg) & ~mask) | val);
586 static inline int test_write(struct v4l2_subdev *sd, u8 reg, u8 val)
588 struct adv7604_state *state = to_state(sd);
590 return adv_smbus_write_byte_data(state, ADV7604_PAGE_TEST, reg, val);
593 static inline int cp_read(struct v4l2_subdev *sd, u8 reg)
595 struct adv7604_state *state = to_state(sd);
597 return adv_smbus_read_byte_data(state, ADV7604_PAGE_CP, reg);
600 static u16 cp_read16(struct v4l2_subdev *sd, u8 reg, u16 mask)
602 return ((cp_read(sd, reg) << 8) | cp_read(sd, reg + 1)) & mask;
605 static inline int cp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
607 struct adv7604_state *state = to_state(sd);
609 return adv_smbus_write_byte_data(state, ADV7604_PAGE_CP, reg, val);
612 static inline int cp_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
614 return cp_write(sd, reg, (cp_read(sd, reg) & ~mask) | val);
617 static inline int vdp_read(struct v4l2_subdev *sd, u8 reg)
619 struct adv7604_state *state = to_state(sd);
621 return adv_smbus_read_byte_data(state, ADV7604_PAGE_VDP, reg);
624 static inline int vdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
626 struct adv7604_state *state = to_state(sd);
628 return adv_smbus_write_byte_data(state, ADV7604_PAGE_VDP, reg, val);
631 #define ADV7604_REG(page, offset) (((page) << 8) | (offset))
632 #define ADV7604_REG_SEQ_TERM 0xffff
634 #ifdef CONFIG_VIDEO_ADV_DEBUG
635 static int adv7604_read_reg(struct v4l2_subdev *sd, unsigned int reg)
637 struct adv7604_state *state = to_state(sd);
638 unsigned int page = reg >> 8;
640 if (!(BIT(page) & state->info->page_mask))
645 return adv_smbus_read_byte_data(state, page, reg);
649 static int adv7604_write_reg(struct v4l2_subdev *sd, unsigned int reg, u8 val)
651 struct adv7604_state *state = to_state(sd);
652 unsigned int page = reg >> 8;
654 if (!(BIT(page) & state->info->page_mask))
659 return adv_smbus_write_byte_data(state, page, reg, val);
662 static void adv7604_write_reg_seq(struct v4l2_subdev *sd,
663 const struct adv7604_reg_seq *reg_seq)
667 for (i = 0; reg_seq[i].reg != ADV7604_REG_SEQ_TERM; i++)
668 adv7604_write_reg(sd, reg_seq[i].reg, reg_seq[i].val);
671 /* -----------------------------------------------------------------------------
675 static const struct adv7604_format_info adv7604_formats[] = {
676 { MEDIA_BUS_FMT_RGB888_1X24, ADV7604_OP_CH_SEL_RGB, true, false,
677 ADV7604_OP_MODE_SEL_SDR_444 | ADV7604_OP_FORMAT_SEL_8BIT },
678 { MEDIA_BUS_FMT_YUYV8_2X8, ADV7604_OP_CH_SEL_RGB, false, false,
679 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_8BIT },
680 { MEDIA_BUS_FMT_YVYU8_2X8, ADV7604_OP_CH_SEL_RGB, false, true,
681 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_8BIT },
682 { MEDIA_BUS_FMT_YUYV10_2X10, ADV7604_OP_CH_SEL_RGB, false, false,
683 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_10BIT },
684 { MEDIA_BUS_FMT_YVYU10_2X10, ADV7604_OP_CH_SEL_RGB, false, true,
685 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_10BIT },
686 { MEDIA_BUS_FMT_YUYV12_2X12, ADV7604_OP_CH_SEL_RGB, false, false,
687 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_12BIT },
688 { MEDIA_BUS_FMT_YVYU12_2X12, ADV7604_OP_CH_SEL_RGB, false, true,
689 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_12BIT },
690 { MEDIA_BUS_FMT_UYVY8_1X16, ADV7604_OP_CH_SEL_RBG, false, false,
691 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
692 { MEDIA_BUS_FMT_VYUY8_1X16, ADV7604_OP_CH_SEL_RBG, false, true,
693 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
694 { MEDIA_BUS_FMT_YUYV8_1X16, ADV7604_OP_CH_SEL_RGB, false, false,
695 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
696 { MEDIA_BUS_FMT_YVYU8_1X16, ADV7604_OP_CH_SEL_RGB, false, true,
697 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
698 { MEDIA_BUS_FMT_UYVY10_1X20, ADV7604_OP_CH_SEL_RBG, false, false,
699 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_10BIT },
700 { MEDIA_BUS_FMT_VYUY10_1X20, ADV7604_OP_CH_SEL_RBG, false, true,
701 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_10BIT },
702 { MEDIA_BUS_FMT_YUYV10_1X20, ADV7604_OP_CH_SEL_RGB, false, false,
703 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_10BIT },
704 { MEDIA_BUS_FMT_YVYU10_1X20, ADV7604_OP_CH_SEL_RGB, false, true,
705 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_10BIT },
706 { MEDIA_BUS_FMT_UYVY12_1X24, ADV7604_OP_CH_SEL_RBG, false, false,
707 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
708 { MEDIA_BUS_FMT_VYUY12_1X24, ADV7604_OP_CH_SEL_RBG, false, true,
709 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
710 { MEDIA_BUS_FMT_YUYV12_1X24, ADV7604_OP_CH_SEL_RGB, false, false,
711 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
712 { MEDIA_BUS_FMT_YVYU12_1X24, ADV7604_OP_CH_SEL_RGB, false, true,
713 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
716 static const struct adv7604_format_info adv7611_formats[] = {
717 { MEDIA_BUS_FMT_RGB888_1X24, ADV7604_OP_CH_SEL_RGB, true, false,
718 ADV7604_OP_MODE_SEL_SDR_444 | ADV7604_OP_FORMAT_SEL_8BIT },
719 { MEDIA_BUS_FMT_YUYV8_2X8, ADV7604_OP_CH_SEL_RGB, false, false,
720 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_8BIT },
721 { MEDIA_BUS_FMT_YVYU8_2X8, ADV7604_OP_CH_SEL_RGB, false, true,
722 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_8BIT },
723 { MEDIA_BUS_FMT_YUYV12_2X12, ADV7604_OP_CH_SEL_RGB, false, false,
724 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_12BIT },
725 { MEDIA_BUS_FMT_YVYU12_2X12, ADV7604_OP_CH_SEL_RGB, false, true,
726 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_12BIT },
727 { MEDIA_BUS_FMT_UYVY8_1X16, ADV7604_OP_CH_SEL_RBG, false, false,
728 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
729 { MEDIA_BUS_FMT_VYUY8_1X16, ADV7604_OP_CH_SEL_RBG, false, true,
730 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
731 { MEDIA_BUS_FMT_YUYV8_1X16, ADV7604_OP_CH_SEL_RGB, false, false,
732 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
733 { MEDIA_BUS_FMT_YVYU8_1X16, ADV7604_OP_CH_SEL_RGB, false, true,
734 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
735 { MEDIA_BUS_FMT_UYVY12_1X24, ADV7604_OP_CH_SEL_RBG, false, false,
736 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
737 { MEDIA_BUS_FMT_VYUY12_1X24, ADV7604_OP_CH_SEL_RBG, false, true,
738 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
739 { MEDIA_BUS_FMT_YUYV12_1X24, ADV7604_OP_CH_SEL_RGB, false, false,
740 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
741 { MEDIA_BUS_FMT_YVYU12_1X24, ADV7604_OP_CH_SEL_RGB, false, true,
742 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
745 static const struct adv7604_format_info *
746 adv7604_format_info(struct adv7604_state *state, u32 code)
750 for (i = 0; i < state->info->nformats; ++i) {
751 if (state->info->formats[i].code == code)
752 return &state->info->formats[i];
758 /* ----------------------------------------------------------------------- */
760 static inline bool is_analog_input(struct v4l2_subdev *sd)
762 struct adv7604_state *state = to_state(sd);
764 return state->selected_input == ADV7604_PAD_VGA_RGB ||
765 state->selected_input == ADV7604_PAD_VGA_COMP;
768 static inline bool is_digital_input(struct v4l2_subdev *sd)
770 struct adv7604_state *state = to_state(sd);
772 return state->selected_input == ADV7604_PAD_HDMI_PORT_A ||
773 state->selected_input == ADV7604_PAD_HDMI_PORT_B ||
774 state->selected_input == ADV7604_PAD_HDMI_PORT_C ||
775 state->selected_input == ADV7604_PAD_HDMI_PORT_D;
778 /* ----------------------------------------------------------------------- */
780 #ifdef CONFIG_VIDEO_ADV_DEBUG
781 static void adv7604_inv_register(struct v4l2_subdev *sd)
783 v4l2_info(sd, "0x000-0x0ff: IO Map\n");
784 v4l2_info(sd, "0x100-0x1ff: AVLink Map\n");
785 v4l2_info(sd, "0x200-0x2ff: CEC Map\n");
786 v4l2_info(sd, "0x300-0x3ff: InfoFrame Map\n");
787 v4l2_info(sd, "0x400-0x4ff: ESDP Map\n");
788 v4l2_info(sd, "0x500-0x5ff: DPP Map\n");
789 v4l2_info(sd, "0x600-0x6ff: AFE Map\n");
790 v4l2_info(sd, "0x700-0x7ff: Repeater Map\n");
791 v4l2_info(sd, "0x800-0x8ff: EDID Map\n");
792 v4l2_info(sd, "0x900-0x9ff: HDMI Map\n");
793 v4l2_info(sd, "0xa00-0xaff: Test Map\n");
794 v4l2_info(sd, "0xb00-0xbff: CP Map\n");
795 v4l2_info(sd, "0xc00-0xcff: VDP Map\n");
798 static int adv7604_g_register(struct v4l2_subdev *sd,
799 struct v4l2_dbg_register *reg)
803 ret = adv7604_read_reg(sd, reg->reg);
805 v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
806 adv7604_inv_register(sd);
816 static int adv7604_s_register(struct v4l2_subdev *sd,
817 const struct v4l2_dbg_register *reg)
821 ret = adv7604_write_reg(sd, reg->reg, reg->val);
823 v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
824 adv7604_inv_register(sd);
832 static unsigned int adv7604_read_cable_det(struct v4l2_subdev *sd)
834 u8 value = io_read(sd, 0x6f);
836 return ((value & 0x10) >> 4)
837 | ((value & 0x08) >> 2)
838 | ((value & 0x04) << 0)
839 | ((value & 0x02) << 2);
842 static unsigned int adv7611_read_cable_det(struct v4l2_subdev *sd)
844 u8 value = io_read(sd, 0x6f);
849 static int adv7604_s_detect_tx_5v_ctrl(struct v4l2_subdev *sd)
851 struct adv7604_state *state = to_state(sd);
852 const struct adv7604_chip_info *info = state->info;
854 return v4l2_ctrl_s_ctrl(state->detect_tx_5v_ctrl,
855 info->read_cable_det(sd));
858 static int find_and_set_predefined_video_timings(struct v4l2_subdev *sd,
860 const struct adv7604_video_standards *predef_vid_timings,
861 const struct v4l2_dv_timings *timings)
865 for (i = 0; predef_vid_timings[i].timings.bt.width; i++) {
866 if (!v4l2_match_dv_timings(timings, &predef_vid_timings[i].timings,
867 is_digital_input(sd) ? 250000 : 1000000))
869 io_write(sd, 0x00, predef_vid_timings[i].vid_std); /* video std */
870 io_write(sd, 0x01, (predef_vid_timings[i].v_freq << 4) +
871 prim_mode); /* v_freq and prim mode */
878 static int configure_predefined_video_timings(struct v4l2_subdev *sd,
879 struct v4l2_dv_timings *timings)
881 struct adv7604_state *state = to_state(sd);
884 v4l2_dbg(1, debug, sd, "%s", __func__);
886 if (adv7604_has_afe(state)) {
887 /* reset to default values */
888 io_write(sd, 0x16, 0x43);
889 io_write(sd, 0x17, 0x5a);
891 /* disable embedded syncs for auto graphics mode */
892 cp_write_clr_set(sd, 0x81, 0x10, 0x00);
893 cp_write(sd, 0x8f, 0x00);
894 cp_write(sd, 0x90, 0x00);
895 cp_write(sd, 0xa2, 0x00);
896 cp_write(sd, 0xa3, 0x00);
897 cp_write(sd, 0xa4, 0x00);
898 cp_write(sd, 0xa5, 0x00);
899 cp_write(sd, 0xa6, 0x00);
900 cp_write(sd, 0xa7, 0x00);
901 cp_write(sd, 0xab, 0x00);
902 cp_write(sd, 0xac, 0x00);
904 if (is_analog_input(sd)) {
905 err = find_and_set_predefined_video_timings(sd,
906 0x01, adv7604_prim_mode_comp, timings);
908 err = find_and_set_predefined_video_timings(sd,
909 0x02, adv7604_prim_mode_gr, timings);
910 } else if (is_digital_input(sd)) {
911 err = find_and_set_predefined_video_timings(sd,
912 0x05, adv7604_prim_mode_hdmi_comp, timings);
914 err = find_and_set_predefined_video_timings(sd,
915 0x06, adv7604_prim_mode_hdmi_gr, timings);
917 v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
918 __func__, state->selected_input);
926 static void configure_custom_video_timings(struct v4l2_subdev *sd,
927 const struct v4l2_bt_timings *bt)
929 struct adv7604_state *state = to_state(sd);
930 u32 width = htotal(bt);
931 u32 height = vtotal(bt);
932 u16 cp_start_sav = bt->hsync + bt->hbackporch - 4;
933 u16 cp_start_eav = width - bt->hfrontporch;
934 u16 cp_start_vbi = height - bt->vfrontporch;
935 u16 cp_end_vbi = bt->vsync + bt->vbackporch;
936 u16 ch1_fr_ll = (((u32)bt->pixelclock / 100) > 0) ?
937 ((width * (ADV7604_fsc / 100)) / ((u32)bt->pixelclock / 100)) : 0;
939 0xc0 | ((width >> 8) & 0x1f),
943 v4l2_dbg(2, debug, sd, "%s\n", __func__);
945 if (is_analog_input(sd)) {
947 io_write(sd, 0x00, 0x07); /* video std */
948 io_write(sd, 0x01, 0x02); /* prim mode */
949 /* enable embedded syncs for auto graphics mode */
950 cp_write_clr_set(sd, 0x81, 0x10, 0x10);
952 /* Should only be set in auto-graphics mode [REF_02, p. 91-92] */
953 /* setup PLL_DIV_MAN_EN and PLL_DIV_RATIO */
954 /* IO-map reg. 0x16 and 0x17 should be written in sequence */
955 if (adv_smbus_write_i2c_block_data(state, ADV7604_PAGE_IO,
957 v4l2_err(sd, "writing to reg 0x16 and 0x17 failed\n");
959 /* active video - horizontal timing */
960 cp_write(sd, 0xa2, (cp_start_sav >> 4) & 0xff);
961 cp_write(sd, 0xa3, ((cp_start_sav & 0x0f) << 4) |
962 ((cp_start_eav >> 8) & 0x0f));
963 cp_write(sd, 0xa4, cp_start_eav & 0xff);
965 /* active video - vertical timing */
966 cp_write(sd, 0xa5, (cp_start_vbi >> 4) & 0xff);
967 cp_write(sd, 0xa6, ((cp_start_vbi & 0xf) << 4) |
968 ((cp_end_vbi >> 8) & 0xf));
969 cp_write(sd, 0xa7, cp_end_vbi & 0xff);
970 } else if (is_digital_input(sd)) {
971 /* set default prim_mode/vid_std for HDMI
972 according to [REF_03, c. 4.2] */
973 io_write(sd, 0x00, 0x02); /* video std */
974 io_write(sd, 0x01, 0x06); /* prim mode */
976 v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
977 __func__, state->selected_input);
980 cp_write(sd, 0x8f, (ch1_fr_ll >> 8) & 0x7);
981 cp_write(sd, 0x90, ch1_fr_ll & 0xff);
982 cp_write(sd, 0xab, (height >> 4) & 0xff);
983 cp_write(sd, 0xac, (height & 0x0f) << 4);
986 static void adv7604_set_offset(struct v4l2_subdev *sd, bool auto_offset, u16 offset_a, u16 offset_b, u16 offset_c)
988 struct adv7604_state *state = to_state(sd);
997 v4l2_dbg(2, debug, sd, "%s: %s offset: a = 0x%x, b = 0x%x, c = 0x%x\n",
998 __func__, auto_offset ? "Auto" : "Manual",
999 offset_a, offset_b, offset_c);
1001 offset_buf[0] = (cp_read(sd, 0x77) & 0xc0) | ((offset_a & 0x3f0) >> 4);
1002 offset_buf[1] = ((offset_a & 0x00f) << 4) | ((offset_b & 0x3c0) >> 6);
1003 offset_buf[2] = ((offset_b & 0x03f) << 2) | ((offset_c & 0x300) >> 8);
1004 offset_buf[3] = offset_c & 0x0ff;
1006 /* Registers must be written in this order with no i2c access in between */
1007 if (adv_smbus_write_i2c_block_data(state, ADV7604_PAGE_CP,
1008 0x77, 4, offset_buf))
1009 v4l2_err(sd, "%s: i2c error writing to CP reg 0x77, 0x78, 0x79, 0x7a\n", __func__);
1012 static void adv7604_set_gain(struct v4l2_subdev *sd, bool auto_gain, u16 gain_a, u16 gain_b, u16 gain_c)
1014 struct adv7604_state *state = to_state(sd);
1017 u8 agc_mode_man = 1;
1027 v4l2_dbg(2, debug, sd, "%s: %s gain: a = 0x%x, b = 0x%x, c = 0x%x\n",
1028 __func__, auto_gain ? "Auto" : "Manual",
1029 gain_a, gain_b, gain_c);
1031 gain_buf[0] = ((gain_man << 7) | (agc_mode_man << 6) | ((gain_a & 0x3f0) >> 4));
1032 gain_buf[1] = (((gain_a & 0x00f) << 4) | ((gain_b & 0x3c0) >> 6));
1033 gain_buf[2] = (((gain_b & 0x03f) << 2) | ((gain_c & 0x300) >> 8));
1034 gain_buf[3] = ((gain_c & 0x0ff));
1036 /* Registers must be written in this order with no i2c access in between */
1037 if (adv_smbus_write_i2c_block_data(state, ADV7604_PAGE_CP,
1039 v4l2_err(sd, "%s: i2c error writing to CP reg 0x73, 0x74, 0x75, 0x76\n", __func__);
1042 static void set_rgb_quantization_range(struct v4l2_subdev *sd)
1044 struct adv7604_state *state = to_state(sd);
1045 bool rgb_output = io_read(sd, 0x02) & 0x02;
1046 bool hdmi_signal = hdmi_read(sd, 0x05) & 0x80;
1048 v4l2_dbg(2, debug, sd, "%s: RGB quantization range: %d, RGB out: %d, HDMI: %d\n",
1049 __func__, state->rgb_quantization_range,
1050 rgb_output, hdmi_signal);
1052 adv7604_set_gain(sd, true, 0x0, 0x0, 0x0);
1053 adv7604_set_offset(sd, true, 0x0, 0x0, 0x0);
1055 switch (state->rgb_quantization_range) {
1056 case V4L2_DV_RGB_RANGE_AUTO:
1057 if (state->selected_input == ADV7604_PAD_VGA_RGB) {
1058 /* Receiving analog RGB signal
1059 * Set RGB full range (0-255) */
1060 io_write_clr_set(sd, 0x02, 0xf0, 0x10);
1064 if (state->selected_input == ADV7604_PAD_VGA_COMP) {
1065 /* Receiving analog YPbPr signal
1067 io_write_clr_set(sd, 0x02, 0xf0, 0xf0);
1072 /* Receiving HDMI signal
1074 io_write_clr_set(sd, 0x02, 0xf0, 0xf0);
1078 /* Receiving DVI-D signal
1079 * ADV7604 selects RGB limited range regardless of
1080 * input format (CE/IT) in automatic mode */
1081 if (state->timings.bt.standards & V4L2_DV_BT_STD_CEA861) {
1082 /* RGB limited range (16-235) */
1083 io_write_clr_set(sd, 0x02, 0xf0, 0x00);
1085 /* RGB full range (0-255) */
1086 io_write_clr_set(sd, 0x02, 0xf0, 0x10);
1088 if (is_digital_input(sd) && rgb_output) {
1089 adv7604_set_offset(sd, false, 0x40, 0x40, 0x40);
1091 adv7604_set_gain(sd, false, 0xe0, 0xe0, 0xe0);
1092 adv7604_set_offset(sd, false, 0x70, 0x70, 0x70);
1096 case V4L2_DV_RGB_RANGE_LIMITED:
1097 if (state->selected_input == ADV7604_PAD_VGA_COMP) {
1098 /* YCrCb limited range (16-235) */
1099 io_write_clr_set(sd, 0x02, 0xf0, 0x20);
1103 /* RGB limited range (16-235) */
1104 io_write_clr_set(sd, 0x02, 0xf0, 0x00);
1107 case V4L2_DV_RGB_RANGE_FULL:
1108 if (state->selected_input == ADV7604_PAD_VGA_COMP) {
1109 /* YCrCb full range (0-255) */
1110 io_write_clr_set(sd, 0x02, 0xf0, 0x60);
1114 /* RGB full range (0-255) */
1115 io_write_clr_set(sd, 0x02, 0xf0, 0x10);
1117 if (is_analog_input(sd) || hdmi_signal)
1120 /* Adjust gain/offset for DVI-D signals only */
1122 adv7604_set_offset(sd, false, 0x40, 0x40, 0x40);
1124 adv7604_set_gain(sd, false, 0xe0, 0xe0, 0xe0);
1125 adv7604_set_offset(sd, false, 0x70, 0x70, 0x70);
1131 static int adv7604_s_ctrl(struct v4l2_ctrl *ctrl)
1133 struct v4l2_subdev *sd =
1134 &container_of(ctrl->handler, struct adv7604_state, hdl)->sd;
1136 struct adv7604_state *state = to_state(sd);
1139 case V4L2_CID_BRIGHTNESS:
1140 cp_write(sd, 0x3c, ctrl->val);
1142 case V4L2_CID_CONTRAST:
1143 cp_write(sd, 0x3a, ctrl->val);
1145 case V4L2_CID_SATURATION:
1146 cp_write(sd, 0x3b, ctrl->val);
1149 cp_write(sd, 0x3d, ctrl->val);
1151 case V4L2_CID_DV_RX_RGB_RANGE:
1152 state->rgb_quantization_range = ctrl->val;
1153 set_rgb_quantization_range(sd);
1155 case V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE:
1156 if (!adv7604_has_afe(state))
1158 /* Set the analog sampling phase. This is needed to find the
1159 best sampling phase for analog video: an application or
1160 driver has to try a number of phases and analyze the picture
1161 quality before settling on the best performing phase. */
1162 afe_write(sd, 0xc8, ctrl->val);
1164 case V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL:
1165 /* Use the default blue color for free running mode,
1166 or supply your own. */
1167 cp_write_clr_set(sd, 0xbf, 0x04, ctrl->val << 2);
1169 case V4L2_CID_ADV_RX_FREE_RUN_COLOR:
1170 cp_write(sd, 0xc0, (ctrl->val & 0xff0000) >> 16);
1171 cp_write(sd, 0xc1, (ctrl->val & 0x00ff00) >> 8);
1172 cp_write(sd, 0xc2, (u8)(ctrl->val & 0x0000ff));
1178 /* ----------------------------------------------------------------------- */
1180 static inline bool no_power(struct v4l2_subdev *sd)
1182 /* Entire chip or CP powered off */
1183 return io_read(sd, 0x0c) & 0x24;
1186 static inline bool no_signal_tmds(struct v4l2_subdev *sd)
1188 struct adv7604_state *state = to_state(sd);
1190 return !(io_read(sd, 0x6a) & (0x10 >> state->selected_input));
1193 static inline bool no_lock_tmds(struct v4l2_subdev *sd)
1195 struct adv7604_state *state = to_state(sd);
1196 const struct adv7604_chip_info *info = state->info;
1198 return (io_read(sd, 0x6a) & info->tdms_lock_mask) != info->tdms_lock_mask;
1201 static inline bool is_hdmi(struct v4l2_subdev *sd)
1203 return hdmi_read(sd, 0x05) & 0x80;
1206 static inline bool no_lock_sspd(struct v4l2_subdev *sd)
1208 struct adv7604_state *state = to_state(sd);
1211 * Chips without a AFE don't expose registers for the SSPD, so just assume
1212 * that we have a lock.
1214 if (adv7604_has_afe(state))
1217 /* TODO channel 2 */
1218 return ((cp_read(sd, 0xb5) & 0xd0) != 0xd0);
1221 static inline bool no_lock_stdi(struct v4l2_subdev *sd)
1223 /* TODO channel 2 */
1224 return !(cp_read(sd, 0xb1) & 0x80);
1227 static inline bool no_signal(struct v4l2_subdev *sd)
1233 ret |= no_lock_stdi(sd);
1234 ret |= no_lock_sspd(sd);
1236 if (is_digital_input(sd)) {
1237 ret |= no_lock_tmds(sd);
1238 ret |= no_signal_tmds(sd);
1244 static inline bool no_lock_cp(struct v4l2_subdev *sd)
1246 struct adv7604_state *state = to_state(sd);
1248 if (!adv7604_has_afe(state))
1251 /* CP has detected a non standard number of lines on the incoming
1252 video compared to what it is configured to receive by s_dv_timings */
1253 return io_read(sd, 0x12) & 0x01;
1256 static int adv7604_g_input_status(struct v4l2_subdev *sd, u32 *status)
1259 *status |= no_power(sd) ? V4L2_IN_ST_NO_POWER : 0;
1260 *status |= no_signal(sd) ? V4L2_IN_ST_NO_SIGNAL : 0;
1262 *status |= is_digital_input(sd) ? V4L2_IN_ST_NO_SYNC : V4L2_IN_ST_NO_H_LOCK;
1264 v4l2_dbg(1, debug, sd, "%s: status = 0x%x\n", __func__, *status);
1269 /* ----------------------------------------------------------------------- */
1271 struct stdi_readback {
1277 static int stdi2dv_timings(struct v4l2_subdev *sd,
1278 struct stdi_readback *stdi,
1279 struct v4l2_dv_timings *timings)
1281 struct adv7604_state *state = to_state(sd);
1282 u32 hfreq = (ADV7604_fsc * 8) / stdi->bl;
1286 for (i = 0; adv7604_timings[i].bt.height; i++) {
1287 if (vtotal(&adv7604_timings[i].bt) != stdi->lcf + 1)
1289 if (adv7604_timings[i].bt.vsync != stdi->lcvs)
1292 pix_clk = hfreq * htotal(&adv7604_timings[i].bt);
1294 if ((pix_clk < adv7604_timings[i].bt.pixelclock + 1000000) &&
1295 (pix_clk > adv7604_timings[i].bt.pixelclock - 1000000)) {
1296 *timings = adv7604_timings[i];
1301 if (v4l2_detect_cvt(stdi->lcf + 1, hfreq, stdi->lcvs,
1302 (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
1303 (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
1306 if (v4l2_detect_gtf(stdi->lcf + 1, hfreq, stdi->lcvs,
1307 (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
1308 (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
1309 state->aspect_ratio, timings))
1312 v4l2_dbg(2, debug, sd,
1313 "%s: No format candidate found for lcvs = %d, lcf=%d, bl = %d, %chsync, %cvsync\n",
1314 __func__, stdi->lcvs, stdi->lcf, stdi->bl,
1315 stdi->hs_pol, stdi->vs_pol);
1320 static int read_stdi(struct v4l2_subdev *sd, struct stdi_readback *stdi)
1322 struct adv7604_state *state = to_state(sd);
1323 const struct adv7604_chip_info *info = state->info;
1326 if (no_lock_stdi(sd) || no_lock_sspd(sd)) {
1327 v4l2_dbg(2, debug, sd, "%s: STDI and/or SSPD not locked\n", __func__);
1332 stdi->bl = cp_read16(sd, 0xb1, 0x3fff);
1333 stdi->lcf = cp_read16(sd, info->lcf_reg, 0x7ff);
1334 stdi->lcvs = cp_read(sd, 0xb3) >> 3;
1335 stdi->interlaced = io_read(sd, 0x12) & 0x10;
1337 if (adv7604_has_afe(state)) {
1339 polarity = cp_read(sd, 0xb5);
1340 if ((polarity & 0x03) == 0x01) {
1341 stdi->hs_pol = polarity & 0x10
1342 ? (polarity & 0x08 ? '+' : '-') : 'x';
1343 stdi->vs_pol = polarity & 0x40
1344 ? (polarity & 0x20 ? '+' : '-') : 'x';
1350 polarity = hdmi_read(sd, 0x05);
1351 stdi->hs_pol = polarity & 0x20 ? '+' : '-';
1352 stdi->vs_pol = polarity & 0x10 ? '+' : '-';
1355 if (no_lock_stdi(sd) || no_lock_sspd(sd)) {
1356 v4l2_dbg(2, debug, sd,
1357 "%s: signal lost during readout of STDI/SSPD\n", __func__);
1361 if (stdi->lcf < 239 || stdi->bl < 8 || stdi->bl == 0x3fff) {
1362 v4l2_dbg(2, debug, sd, "%s: invalid signal\n", __func__);
1363 memset(stdi, 0, sizeof(struct stdi_readback));
1367 v4l2_dbg(2, debug, sd,
1368 "%s: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, %chsync, %cvsync, %s\n",
1369 __func__, stdi->lcf, stdi->bl, stdi->lcvs,
1370 stdi->hs_pol, stdi->vs_pol,
1371 stdi->interlaced ? "interlaced" : "progressive");
1376 static int adv7604_enum_dv_timings(struct v4l2_subdev *sd,
1377 struct v4l2_enum_dv_timings *timings)
1379 struct adv7604_state *state = to_state(sd);
1381 if (timings->index >= ARRAY_SIZE(adv7604_timings) - 1)
1384 if (timings->pad >= state->source_pad)
1387 memset(timings->reserved, 0, sizeof(timings->reserved));
1388 timings->timings = adv7604_timings[timings->index];
1392 static int adv7604_dv_timings_cap(struct v4l2_subdev *sd,
1393 struct v4l2_dv_timings_cap *cap)
1395 struct adv7604_state *state = to_state(sd);
1397 if (cap->pad >= state->source_pad)
1400 cap->type = V4L2_DV_BT_656_1120;
1401 cap->bt.max_width = 1920;
1402 cap->bt.max_height = 1200;
1403 cap->bt.min_pixelclock = 25000000;
1406 case ADV7604_PAD_HDMI_PORT_A:
1407 case ADV7604_PAD_HDMI_PORT_B:
1408 case ADV7604_PAD_HDMI_PORT_C:
1409 case ADV7604_PAD_HDMI_PORT_D:
1410 cap->bt.max_pixelclock = 225000000;
1412 case ADV7604_PAD_VGA_RGB:
1413 case ADV7604_PAD_VGA_COMP:
1415 cap->bt.max_pixelclock = 170000000;
1419 cap->bt.standards = V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
1420 V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT;
1421 cap->bt.capabilities = V4L2_DV_BT_CAP_PROGRESSIVE |
1422 V4L2_DV_BT_CAP_REDUCED_BLANKING | V4L2_DV_BT_CAP_CUSTOM;
1426 /* Fill the optional fields .standards and .flags in struct v4l2_dv_timings
1427 if the format is listed in adv7604_timings[] */
1428 static void adv7604_fill_optional_dv_timings_fields(struct v4l2_subdev *sd,
1429 struct v4l2_dv_timings *timings)
1433 for (i = 0; adv7604_timings[i].bt.width; i++) {
1434 if (v4l2_match_dv_timings(timings, &adv7604_timings[i],
1435 is_digital_input(sd) ? 250000 : 1000000)) {
1436 *timings = adv7604_timings[i];
1442 static unsigned int adv7604_read_hdmi_pixelclock(struct v4l2_subdev *sd)
1447 a = hdmi_read(sd, 0x06);
1448 b = hdmi_read(sd, 0x3b);
1451 freq = a * 1000000 + ((b & 0x30) >> 4) * 250000;
1454 /* adjust for deep color mode */
1455 unsigned bits_per_channel = ((hdmi_read(sd, 0x0b) & 0x60) >> 4) + 8;
1457 freq = freq * 8 / bits_per_channel;
1463 static unsigned int adv7611_read_hdmi_pixelclock(struct v4l2_subdev *sd)
1467 a = hdmi_read(sd, 0x51);
1468 b = hdmi_read(sd, 0x52);
1471 return ((a << 1) | (b >> 7)) * 1000000 + (b & 0x7f) * 1000000 / 128;
1474 static int adv7604_query_dv_timings(struct v4l2_subdev *sd,
1475 struct v4l2_dv_timings *timings)
1477 struct adv7604_state *state = to_state(sd);
1478 const struct adv7604_chip_info *info = state->info;
1479 struct v4l2_bt_timings *bt = &timings->bt;
1480 struct stdi_readback stdi;
1485 memset(timings, 0, sizeof(struct v4l2_dv_timings));
1487 if (no_signal(sd)) {
1488 state->restart_stdi_once = true;
1489 v4l2_dbg(1, debug, sd, "%s: no valid signal\n", __func__);
1494 if (read_stdi(sd, &stdi)) {
1495 v4l2_dbg(1, debug, sd, "%s: STDI/SSPD not locked\n", __func__);
1498 bt->interlaced = stdi.interlaced ?
1499 V4L2_DV_INTERLACED : V4L2_DV_PROGRESSIVE;
1501 if (is_digital_input(sd)) {
1502 timings->type = V4L2_DV_BT_656_1120;
1504 /* FIXME: All masks are incorrect for ADV7611 */
1505 bt->width = hdmi_read16(sd, 0x07, 0xfff);
1506 bt->height = hdmi_read16(sd, 0x09, 0xfff);
1507 bt->pixelclock = info->read_hdmi_pixelclock(sd);
1508 bt->hfrontporch = hdmi_read16(sd, 0x20, 0x3ff);
1509 bt->hsync = hdmi_read16(sd, 0x22, 0x3ff);
1510 bt->hbackporch = hdmi_read16(sd, 0x24, 0x3ff);
1511 bt->vfrontporch = hdmi_read16(sd, 0x2a, 0x1fff) / 2;
1512 bt->vsync = hdmi_read16(sd, 0x2e, 0x1fff) / 2;
1513 bt->vbackporch = hdmi_read16(sd, 0x32, 0x1fff) / 2;
1514 bt->polarities = ((hdmi_read(sd, 0x05) & 0x10) ? V4L2_DV_VSYNC_POS_POL : 0) |
1515 ((hdmi_read(sd, 0x05) & 0x20) ? V4L2_DV_HSYNC_POS_POL : 0);
1516 if (bt->interlaced == V4L2_DV_INTERLACED) {
1517 bt->height += hdmi_read16(sd, 0x0b, 0xfff);
1518 bt->il_vfrontporch = hdmi_read16(sd, 0x2c, 0x1fff) / 2;
1519 bt->il_vsync = hdmi_read16(sd, 0x30, 0x1fff) / 2;
1520 bt->il_vbackporch = hdmi_read16(sd, 0x34, 0x1fff) / 2;
1522 adv7604_fill_optional_dv_timings_fields(sd, timings);
1525 * Since LCVS values are inaccurate [REF_03, p. 275-276],
1526 * stdi2dv_timings() is called with lcvs +-1 if the first attempt fails.
1528 if (!stdi2dv_timings(sd, &stdi, timings))
1531 v4l2_dbg(1, debug, sd, "%s: lcvs + 1 = %d\n", __func__, stdi.lcvs);
1532 if (!stdi2dv_timings(sd, &stdi, timings))
1535 v4l2_dbg(1, debug, sd, "%s: lcvs - 1 = %d\n", __func__, stdi.lcvs);
1536 if (stdi2dv_timings(sd, &stdi, timings)) {
1538 * The STDI block may measure wrong values, especially
1539 * for lcvs and lcf. If the driver can not find any
1540 * valid timing, the STDI block is restarted to measure
1541 * the video timings again. The function will return an
1542 * error, but the restart of STDI will generate a new
1543 * STDI interrupt and the format detection process will
1546 if (state->restart_stdi_once) {
1547 v4l2_dbg(1, debug, sd, "%s: restart STDI\n", __func__);
1548 /* TODO restart STDI for Sync Channel 2 */
1549 /* enter one-shot mode */
1550 cp_write_clr_set(sd, 0x86, 0x06, 0x00);
1551 /* trigger STDI restart */
1552 cp_write_clr_set(sd, 0x86, 0x06, 0x04);
1553 /* reset to continuous mode */
1554 cp_write_clr_set(sd, 0x86, 0x06, 0x02);
1555 state->restart_stdi_once = false;
1558 v4l2_dbg(1, debug, sd, "%s: format not supported\n", __func__);
1561 state->restart_stdi_once = true;
1565 if (no_signal(sd)) {
1566 v4l2_dbg(1, debug, sd, "%s: signal lost during readout\n", __func__);
1567 memset(timings, 0, sizeof(struct v4l2_dv_timings));
1571 if ((is_analog_input(sd) && bt->pixelclock > 170000000) ||
1572 (is_digital_input(sd) && bt->pixelclock > 225000000)) {
1573 v4l2_dbg(1, debug, sd, "%s: pixelclock out of range %d\n",
1574 __func__, (u32)bt->pixelclock);
1579 v4l2_print_dv_timings(sd->name, "adv7604_query_dv_timings: ",
1585 static int adv7604_s_dv_timings(struct v4l2_subdev *sd,
1586 struct v4l2_dv_timings *timings)
1588 struct adv7604_state *state = to_state(sd);
1589 struct v4l2_bt_timings *bt;
1595 if (v4l2_match_dv_timings(&state->timings, timings, 0)) {
1596 v4l2_dbg(1, debug, sd, "%s: no change\n", __func__);
1602 if ((is_analog_input(sd) && bt->pixelclock > 170000000) ||
1603 (is_digital_input(sd) && bt->pixelclock > 225000000)) {
1604 v4l2_dbg(1, debug, sd, "%s: pixelclock out of range %d\n",
1605 __func__, (u32)bt->pixelclock);
1609 adv7604_fill_optional_dv_timings_fields(sd, timings);
1611 state->timings = *timings;
1613 cp_write_clr_set(sd, 0x91, 0x40, bt->interlaced ? 0x40 : 0x00);
1615 /* Use prim_mode and vid_std when available */
1616 err = configure_predefined_video_timings(sd, timings);
1618 /* custom settings when the video format
1619 does not have prim_mode/vid_std */
1620 configure_custom_video_timings(sd, bt);
1623 set_rgb_quantization_range(sd);
1626 v4l2_print_dv_timings(sd->name, "adv7604_s_dv_timings: ",
1631 static int adv7604_g_dv_timings(struct v4l2_subdev *sd,
1632 struct v4l2_dv_timings *timings)
1634 struct adv7604_state *state = to_state(sd);
1636 *timings = state->timings;
1640 static void adv7604_set_termination(struct v4l2_subdev *sd, bool enable)
1642 hdmi_write(sd, 0x01, enable ? 0x00 : 0x78);
1645 static void adv7611_set_termination(struct v4l2_subdev *sd, bool enable)
1647 hdmi_write(sd, 0x83, enable ? 0xfe : 0xff);
1650 static void enable_input(struct v4l2_subdev *sd)
1652 struct adv7604_state *state = to_state(sd);
1654 if (is_analog_input(sd)) {
1655 io_write(sd, 0x15, 0xb0); /* Disable Tristate of Pins (no audio) */
1656 } else if (is_digital_input(sd)) {
1657 hdmi_write_clr_set(sd, 0x00, 0x03, state->selected_input);
1658 state->info->set_termination(sd, true);
1659 io_write(sd, 0x15, 0xa0); /* Disable Tristate of Pins */
1660 hdmi_write_clr_set(sd, 0x1a, 0x10, 0x00); /* Unmute audio */
1662 v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
1663 __func__, state->selected_input);
1667 static void disable_input(struct v4l2_subdev *sd)
1669 struct adv7604_state *state = to_state(sd);
1671 hdmi_write_clr_set(sd, 0x1a, 0x10, 0x10); /* Mute audio */
1672 msleep(16); /* 512 samples with >= 32 kHz sample rate [REF_03, c. 7.16.10] */
1673 io_write(sd, 0x15, 0xbe); /* Tristate all outputs from video core */
1674 state->info->set_termination(sd, false);
1677 static void select_input(struct v4l2_subdev *sd)
1679 struct adv7604_state *state = to_state(sd);
1680 const struct adv7604_chip_info *info = state->info;
1682 if (is_analog_input(sd)) {
1683 adv7604_write_reg_seq(sd, info->recommended_settings[0]);
1685 afe_write(sd, 0x00, 0x08); /* power up ADC */
1686 afe_write(sd, 0x01, 0x06); /* power up Analog Front End */
1687 afe_write(sd, 0xc8, 0x00); /* phase control */
1688 } else if (is_digital_input(sd)) {
1689 hdmi_write(sd, 0x00, state->selected_input & 0x03);
1691 adv7604_write_reg_seq(sd, info->recommended_settings[1]);
1693 if (adv7604_has_afe(state)) {
1694 afe_write(sd, 0x00, 0xff); /* power down ADC */
1695 afe_write(sd, 0x01, 0xfe); /* power down Analog Front End */
1696 afe_write(sd, 0xc8, 0x40); /* phase control */
1699 cp_write(sd, 0x3e, 0x00); /* CP core pre-gain control */
1700 cp_write(sd, 0xc3, 0x39); /* CP coast control. Graphics mode */
1701 cp_write(sd, 0x40, 0x80); /* CP core pre-gain control. Graphics mode */
1703 v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
1704 __func__, state->selected_input);
1708 static int adv7604_s_routing(struct v4l2_subdev *sd,
1709 u32 input, u32 output, u32 config)
1711 struct adv7604_state *state = to_state(sd);
1713 v4l2_dbg(2, debug, sd, "%s: input %d, selected input %d",
1714 __func__, input, state->selected_input);
1716 if (input == state->selected_input)
1719 if (input > state->info->max_port)
1722 state->selected_input = input;
1733 static int adv7604_enum_mbus_code(struct v4l2_subdev *sd,
1734 struct v4l2_subdev_fh *fh,
1735 struct v4l2_subdev_mbus_code_enum *code)
1737 struct adv7604_state *state = to_state(sd);
1739 if (code->index >= state->info->nformats)
1742 code->code = state->info->formats[code->index].code;
1747 static void adv7604_fill_format(struct adv7604_state *state,
1748 struct v4l2_mbus_framefmt *format)
1750 memset(format, 0, sizeof(*format));
1752 format->width = state->timings.bt.width;
1753 format->height = state->timings.bt.height;
1754 format->field = V4L2_FIELD_NONE;
1756 if (state->timings.bt.standards & V4L2_DV_BT_STD_CEA861)
1757 format->colorspace = (state->timings.bt.height <= 576) ?
1758 V4L2_COLORSPACE_SMPTE170M : V4L2_COLORSPACE_REC709;
1762 * Compute the op_ch_sel value required to obtain on the bus the component order
1763 * corresponding to the selected format taking into account bus reordering
1764 * applied by the board at the output of the device.
1766 * The following table gives the op_ch_value from the format component order
1767 * (expressed as op_ch_sel value in column) and the bus reordering (expressed as
1768 * adv7604_bus_order value in row).
1770 * | GBR(0) GRB(1) BGR(2) RGB(3) BRG(4) RBG(5)
1771 * ----------+-------------------------------------------------
1772 * RGB (NOP) | GBR GRB BGR RGB BRG RBG
1773 * GRB (1-2) | BGR RGB GBR GRB RBG BRG
1774 * RBG (2-3) | GRB GBR BRG RBG BGR RGB
1775 * BGR (1-3) | RBG BRG RGB BGR GRB GBR
1776 * BRG (ROR) | BRG RBG GRB GBR RGB BGR
1777 * GBR (ROL) | RGB BGR RBG BRG GBR GRB
1779 static unsigned int adv7604_op_ch_sel(struct adv7604_state *state)
1781 #define _SEL(a,b,c,d,e,f) { \
1782 ADV7604_OP_CH_SEL_##a, ADV7604_OP_CH_SEL_##b, ADV7604_OP_CH_SEL_##c, \
1783 ADV7604_OP_CH_SEL_##d, ADV7604_OP_CH_SEL_##e, ADV7604_OP_CH_SEL_##f }
1784 #define _BUS(x) [ADV7604_BUS_ORDER_##x]
1786 static const unsigned int op_ch_sel[6][6] = {
1787 _BUS(RGB) /* NOP */ = _SEL(GBR, GRB, BGR, RGB, BRG, RBG),
1788 _BUS(GRB) /* 1-2 */ = _SEL(BGR, RGB, GBR, GRB, RBG, BRG),
1789 _BUS(RBG) /* 2-3 */ = _SEL(GRB, GBR, BRG, RBG, BGR, RGB),
1790 _BUS(BGR) /* 1-3 */ = _SEL(RBG, BRG, RGB, BGR, GRB, GBR),
1791 _BUS(BRG) /* ROR */ = _SEL(BRG, RBG, GRB, GBR, RGB, BGR),
1792 _BUS(GBR) /* ROL */ = _SEL(RGB, BGR, RBG, BRG, GBR, GRB),
1795 return op_ch_sel[state->pdata.bus_order][state->format->op_ch_sel >> 5];
1798 static void adv7604_setup_format(struct adv7604_state *state)
1800 struct v4l2_subdev *sd = &state->sd;
1802 io_write_clr_set(sd, 0x02, 0x02,
1803 state->format->rgb_out ? ADV7604_RGB_OUT : 0);
1804 io_write(sd, 0x03, state->format->op_format_sel |
1805 state->pdata.op_format_mode_sel);
1806 io_write_clr_set(sd, 0x04, 0xe0, adv7604_op_ch_sel(state));
1807 io_write_clr_set(sd, 0x05, 0x01,
1808 state->format->swap_cb_cr ? ADV7604_OP_SWAP_CB_CR : 0);
1811 static int adv7604_get_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
1812 struct v4l2_subdev_format *format)
1814 struct adv7604_state *state = to_state(sd);
1816 if (format->pad != state->source_pad)
1819 adv7604_fill_format(state, &format->format);
1821 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1822 struct v4l2_mbus_framefmt *fmt;
1824 fmt = v4l2_subdev_get_try_format(fh, format->pad);
1825 format->format.code = fmt->code;
1827 format->format.code = state->format->code;
1833 static int adv7604_set_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
1834 struct v4l2_subdev_format *format)
1836 struct adv7604_state *state = to_state(sd);
1837 const struct adv7604_format_info *info;
1839 if (format->pad != state->source_pad)
1842 info = adv7604_format_info(state, format->format.code);
1844 info = adv7604_format_info(state, MEDIA_BUS_FMT_YUYV8_2X8);
1846 adv7604_fill_format(state, &format->format);
1847 format->format.code = info->code;
1849 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1850 struct v4l2_mbus_framefmt *fmt;
1852 fmt = v4l2_subdev_get_try_format(fh, format->pad);
1853 fmt->code = format->format.code;
1855 state->format = info;
1856 adv7604_setup_format(state);
1862 static int adv7604_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
1864 struct adv7604_state *state = to_state(sd);
1865 const struct adv7604_chip_info *info = state->info;
1866 const u8 irq_reg_0x43 = io_read(sd, 0x43);
1867 const u8 irq_reg_0x6b = io_read(sd, 0x6b);
1868 const u8 irq_reg_0x70 = io_read(sd, 0x70);
1869 u8 fmt_change_digital;
1874 io_write(sd, 0x44, irq_reg_0x43);
1876 io_write(sd, 0x71, irq_reg_0x70);
1878 io_write(sd, 0x6c, irq_reg_0x6b);
1880 v4l2_dbg(2, debug, sd, "%s: ", __func__);
1883 fmt_change = irq_reg_0x43 & 0x98;
1884 fmt_change_digital = is_digital_input(sd)
1885 ? irq_reg_0x6b & info->fmt_change_digital_mask
1888 if (fmt_change || fmt_change_digital) {
1889 v4l2_dbg(1, debug, sd,
1890 "%s: fmt_change = 0x%x, fmt_change_digital = 0x%x\n",
1891 __func__, fmt_change, fmt_change_digital);
1893 v4l2_subdev_notify(sd, ADV7604_FMT_CHANGE, NULL);
1899 if (irq_reg_0x6b & 0x01) {
1900 v4l2_dbg(1, debug, sd, "%s: irq %s mode\n", __func__,
1901 (io_read(sd, 0x6a) & 0x01) ? "HDMI" : "DVI");
1902 set_rgb_quantization_range(sd);
1908 tx_5v = io_read(sd, 0x70) & info->cable_det_mask;
1910 v4l2_dbg(1, debug, sd, "%s: tx_5v: 0x%x\n", __func__, tx_5v);
1911 io_write(sd, 0x71, tx_5v);
1912 adv7604_s_detect_tx_5v_ctrl(sd);
1919 static int adv7604_get_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
1921 struct adv7604_state *state = to_state(sd);
1924 memset(edid->reserved, 0, sizeof(edid->reserved));
1926 switch (edid->pad) {
1927 case ADV7604_PAD_HDMI_PORT_A:
1928 case ADV7604_PAD_HDMI_PORT_B:
1929 case ADV7604_PAD_HDMI_PORT_C:
1930 case ADV7604_PAD_HDMI_PORT_D:
1931 if (state->edid.present & (1 << edid->pad))
1932 data = state->edid.edid;
1938 if (edid->start_block == 0 && edid->blocks == 0) {
1939 edid->blocks = data ? state->edid.blocks : 0;
1946 if (edid->start_block >= state->edid.blocks)
1949 if (edid->start_block + edid->blocks > state->edid.blocks)
1950 edid->blocks = state->edid.blocks - edid->start_block;
1952 memcpy(edid->edid, data + edid->start_block * 128, edid->blocks * 128);
1957 static int get_edid_spa_location(const u8 *edid)
1961 if ((edid[0x7e] != 1) ||
1962 (edid[0x80] != 0x02) ||
1963 (edid[0x81] != 0x03)) {
1967 /* search Vendor Specific Data Block (tag 3) */
1968 d = edid[0x82] & 0x7f;
1974 u8 tag = edid[i] >> 5;
1975 u8 len = edid[i] & 0x1f;
1977 if ((tag == 3) && (len >= 5))
1985 static int adv7604_set_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
1987 struct adv7604_state *state = to_state(sd);
1988 const struct adv7604_chip_info *info = state->info;
1993 memset(edid->reserved, 0, sizeof(edid->reserved));
1995 if (edid->pad > ADV7604_PAD_HDMI_PORT_D)
1997 if (edid->start_block != 0)
1999 if (edid->blocks == 0) {
2000 /* Disable hotplug and I2C access to EDID RAM from DDC port */
2001 state->edid.present &= ~(1 << edid->pad);
2002 adv7604_set_hpd(state, state->edid.present);
2003 rep_write_clr_set(sd, info->edid_enable_reg, 0x0f, state->edid.present);
2005 /* Fall back to a 16:9 aspect ratio */
2006 state->aspect_ratio.numerator = 16;
2007 state->aspect_ratio.denominator = 9;
2009 if (!state->edid.present)
2010 state->edid.blocks = 0;
2012 v4l2_dbg(2, debug, sd, "%s: clear EDID pad %d, edid.present = 0x%x\n",
2013 __func__, edid->pad, state->edid.present);
2016 if (edid->blocks > 2) {
2021 v4l2_dbg(2, debug, sd, "%s: write EDID pad %d, edid.present = 0x%x\n",
2022 __func__, edid->pad, state->edid.present);
2024 /* Disable hotplug and I2C access to EDID RAM from DDC port */
2025 cancel_delayed_work_sync(&state->delayed_work_enable_hotplug);
2026 adv7604_set_hpd(state, 0);
2027 rep_write_clr_set(sd, info->edid_enable_reg, 0x0f, 0x00);
2029 spa_loc = get_edid_spa_location(edid->edid);
2031 spa_loc = 0xc0; /* Default value [REF_02, p. 116] */
2033 switch (edid->pad) {
2034 case ADV7604_PAD_HDMI_PORT_A:
2035 state->spa_port_a[0] = edid->edid[spa_loc];
2036 state->spa_port_a[1] = edid->edid[spa_loc + 1];
2038 case ADV7604_PAD_HDMI_PORT_B:
2039 rep_write(sd, 0x70, edid->edid[spa_loc]);
2040 rep_write(sd, 0x71, edid->edid[spa_loc + 1]);
2042 case ADV7604_PAD_HDMI_PORT_C:
2043 rep_write(sd, 0x72, edid->edid[spa_loc]);
2044 rep_write(sd, 0x73, edid->edid[spa_loc + 1]);
2046 case ADV7604_PAD_HDMI_PORT_D:
2047 rep_write(sd, 0x74, edid->edid[spa_loc]);
2048 rep_write(sd, 0x75, edid->edid[spa_loc + 1]);
2054 if (info->type == ADV7604) {
2055 rep_write(sd, 0x76, spa_loc & 0xff);
2056 rep_write_clr_set(sd, 0x77, 0x40, (spa_loc & 0x100) >> 2);
2058 /* FIXME: Where is the SPA location LSB register ? */
2059 rep_write_clr_set(sd, 0x71, 0x01, (spa_loc & 0x100) >> 8);
2062 edid->edid[spa_loc] = state->spa_port_a[0];
2063 edid->edid[spa_loc + 1] = state->spa_port_a[1];
2065 memcpy(state->edid.edid, edid->edid, 128 * edid->blocks);
2066 state->edid.blocks = edid->blocks;
2067 state->aspect_ratio = v4l2_calc_aspect_ratio(edid->edid[0x15],
2069 state->edid.present |= 1 << edid->pad;
2071 err = edid_write_block(sd, 128 * edid->blocks, state->edid.edid);
2073 v4l2_err(sd, "error %d writing edid pad %d\n", err, edid->pad);
2077 /* adv7604 calculates the checksums and enables I2C access to internal
2078 EDID RAM from DDC port. */
2079 rep_write_clr_set(sd, info->edid_enable_reg, 0x0f, state->edid.present);
2081 for (i = 0; i < 1000; i++) {
2082 if (rep_read(sd, info->edid_status_reg) & state->edid.present)
2087 v4l2_err(sd, "error enabling edid (0x%x)\n", state->edid.present);
2091 /* enable hotplug after 100 ms */
2092 queue_delayed_work(state->work_queues,
2093 &state->delayed_work_enable_hotplug, HZ / 10);
2097 /*********** avi info frame CEA-861-E **************/
2099 static void print_avi_infoframe(struct v4l2_subdev *sd)
2107 v4l2_info(sd, "receive DVI-D signal (AVI infoframe not supported)\n");
2110 if (!(io_read(sd, 0x60) & 0x01)) {
2111 v4l2_info(sd, "AVI infoframe not received\n");
2115 if (io_read(sd, 0x83) & 0x01) {
2116 v4l2_info(sd, "AVI infoframe checksum error has occurred earlier\n");
2117 io_write(sd, 0x85, 0x01); /* clear AVI_INF_CKS_ERR_RAW */
2118 if (io_read(sd, 0x83) & 0x01) {
2119 v4l2_info(sd, "AVI infoframe checksum error still present\n");
2120 io_write(sd, 0x85, 0x01); /* clear AVI_INF_CKS_ERR_RAW */
2124 avi_len = infoframe_read(sd, 0xe2);
2125 avi_ver = infoframe_read(sd, 0xe1);
2126 v4l2_info(sd, "AVI infoframe version %d (%d byte)\n",
2129 if (avi_ver != 0x02)
2132 for (i = 0; i < 14; i++)
2133 buf[i] = infoframe_read(sd, i);
2136 "\t%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
2137 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7],
2138 buf[8], buf[9], buf[10], buf[11], buf[12], buf[13]);
2141 static int adv7604_log_status(struct v4l2_subdev *sd)
2143 struct adv7604_state *state = to_state(sd);
2144 const struct adv7604_chip_info *info = state->info;
2145 struct v4l2_dv_timings timings;
2146 struct stdi_readback stdi;
2147 u8 reg_io_0x02 = io_read(sd, 0x02);
2151 static const char * const csc_coeff_sel_rb[16] = {
2152 "bypassed", "YPbPr601 -> RGB", "reserved", "YPbPr709 -> RGB",
2153 "reserved", "RGB -> YPbPr601", "reserved", "RGB -> YPbPr709",
2154 "reserved", "YPbPr709 -> YPbPr601", "YPbPr601 -> YPbPr709",
2155 "reserved", "reserved", "reserved", "reserved", "manual"
2157 static const char * const input_color_space_txt[16] = {
2158 "RGB limited range (16-235)", "RGB full range (0-255)",
2159 "YCbCr Bt.601 (16-235)", "YCbCr Bt.709 (16-235)",
2160 "xvYCC Bt.601", "xvYCC Bt.709",
2161 "YCbCr Bt.601 (0-255)", "YCbCr Bt.709 (0-255)",
2162 "invalid", "invalid", "invalid", "invalid", "invalid",
2163 "invalid", "invalid", "automatic"
2165 static const char * const rgb_quantization_range_txt[] = {
2167 "RGB limited range (16-235)",
2168 "RGB full range (0-255)",
2170 static const char * const deep_color_mode_txt[4] = {
2171 "8-bits per channel",
2172 "10-bits per channel",
2173 "12-bits per channel",
2174 "16-bits per channel (not supported)"
2177 v4l2_info(sd, "-----Chip status-----\n");
2178 v4l2_info(sd, "Chip power: %s\n", no_power(sd) ? "off" : "on");
2179 edid_enabled = rep_read(sd, info->edid_status_reg);
2180 v4l2_info(sd, "EDID enabled port A: %s, B: %s, C: %s, D: %s\n",
2181 ((edid_enabled & 0x01) ? "Yes" : "No"),
2182 ((edid_enabled & 0x02) ? "Yes" : "No"),
2183 ((edid_enabled & 0x04) ? "Yes" : "No"),
2184 ((edid_enabled & 0x08) ? "Yes" : "No"));
2185 v4l2_info(sd, "CEC: %s\n", !!(cec_read(sd, 0x2a) & 0x01) ?
2186 "enabled" : "disabled");
2188 v4l2_info(sd, "-----Signal status-----\n");
2189 cable_det = info->read_cable_det(sd);
2190 v4l2_info(sd, "Cable detected (+5V power) port A: %s, B: %s, C: %s, D: %s\n",
2191 ((cable_det & 0x01) ? "Yes" : "No"),
2192 ((cable_det & 0x02) ? "Yes" : "No"),
2193 ((cable_det & 0x04) ? "Yes" : "No"),
2194 ((cable_det & 0x08) ? "Yes" : "No"));
2195 v4l2_info(sd, "TMDS signal detected: %s\n",
2196 no_signal_tmds(sd) ? "false" : "true");
2197 v4l2_info(sd, "TMDS signal locked: %s\n",
2198 no_lock_tmds(sd) ? "false" : "true");
2199 v4l2_info(sd, "SSPD locked: %s\n", no_lock_sspd(sd) ? "false" : "true");
2200 v4l2_info(sd, "STDI locked: %s\n", no_lock_stdi(sd) ? "false" : "true");
2201 v4l2_info(sd, "CP locked: %s\n", no_lock_cp(sd) ? "false" : "true");
2202 v4l2_info(sd, "CP free run: %s\n",
2203 (!!(cp_read(sd, 0xff) & 0x10) ? "on" : "off"));
2204 v4l2_info(sd, "Prim-mode = 0x%x, video std = 0x%x, v_freq = 0x%x\n",
2205 io_read(sd, 0x01) & 0x0f, io_read(sd, 0x00) & 0x3f,
2206 (io_read(sd, 0x01) & 0x70) >> 4);
2208 v4l2_info(sd, "-----Video Timings-----\n");
2209 if (read_stdi(sd, &stdi))
2210 v4l2_info(sd, "STDI: not locked\n");
2212 v4l2_info(sd, "STDI: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, %s, %chsync, %cvsync\n",
2213 stdi.lcf, stdi.bl, stdi.lcvs,
2214 stdi.interlaced ? "interlaced" : "progressive",
2215 stdi.hs_pol, stdi.vs_pol);
2216 if (adv7604_query_dv_timings(sd, &timings))
2217 v4l2_info(sd, "No video detected\n");
2219 v4l2_print_dv_timings(sd->name, "Detected format: ",
2221 v4l2_print_dv_timings(sd->name, "Configured format: ",
2222 &state->timings, true);
2227 v4l2_info(sd, "-----Color space-----\n");
2228 v4l2_info(sd, "RGB quantization range ctrl: %s\n",
2229 rgb_quantization_range_txt[state->rgb_quantization_range]);
2230 v4l2_info(sd, "Input color space: %s\n",
2231 input_color_space_txt[reg_io_0x02 >> 4]);
2232 v4l2_info(sd, "Output color space: %s %s, saturator %s\n",
2233 (reg_io_0x02 & 0x02) ? "RGB" : "YCbCr",
2234 (reg_io_0x02 & 0x04) ? "(16-235)" : "(0-255)",
2235 ((reg_io_0x02 & 0x04) ^ (reg_io_0x02 & 0x01)) ?
2236 "enabled" : "disabled");
2237 v4l2_info(sd, "Color space conversion: %s\n",
2238 csc_coeff_sel_rb[cp_read(sd, 0xfc) >> 4]);
2240 if (!is_digital_input(sd))
2243 v4l2_info(sd, "-----%s status-----\n", is_hdmi(sd) ? "HDMI" : "DVI-D");
2244 v4l2_info(sd, "Digital video port selected: %c\n",
2245 (hdmi_read(sd, 0x00) & 0x03) + 'A');
2246 v4l2_info(sd, "HDCP encrypted content: %s\n",
2247 (hdmi_read(sd, 0x05) & 0x40) ? "true" : "false");
2248 v4l2_info(sd, "HDCP keys read: %s%s\n",
2249 (hdmi_read(sd, 0x04) & 0x20) ? "yes" : "no",
2250 (hdmi_read(sd, 0x04) & 0x10) ? "ERROR" : "");
2252 bool audio_pll_locked = hdmi_read(sd, 0x04) & 0x01;
2253 bool audio_sample_packet_detect = hdmi_read(sd, 0x18) & 0x01;
2254 bool audio_mute = io_read(sd, 0x65) & 0x40;
2256 v4l2_info(sd, "Audio: pll %s, samples %s, %s\n",
2257 audio_pll_locked ? "locked" : "not locked",
2258 audio_sample_packet_detect ? "detected" : "not detected",
2259 audio_mute ? "muted" : "enabled");
2260 if (audio_pll_locked && audio_sample_packet_detect) {
2261 v4l2_info(sd, "Audio format: %s\n",
2262 (hdmi_read(sd, 0x07) & 0x20) ? "multi-channel" : "stereo");
2264 v4l2_info(sd, "Audio CTS: %u\n", (hdmi_read(sd, 0x5b) << 12) +
2265 (hdmi_read(sd, 0x5c) << 8) +
2266 (hdmi_read(sd, 0x5d) & 0xf0));
2267 v4l2_info(sd, "Audio N: %u\n", ((hdmi_read(sd, 0x5d) & 0x0f) << 16) +
2268 (hdmi_read(sd, 0x5e) << 8) +
2269 hdmi_read(sd, 0x5f));
2270 v4l2_info(sd, "AV Mute: %s\n", (hdmi_read(sd, 0x04) & 0x40) ? "on" : "off");
2272 v4l2_info(sd, "Deep color mode: %s\n", deep_color_mode_txt[(hdmi_read(sd, 0x0b) & 0x60) >> 5]);
2274 print_avi_infoframe(sd);
2280 /* ----------------------------------------------------------------------- */
2282 static const struct v4l2_ctrl_ops adv7604_ctrl_ops = {
2283 .s_ctrl = adv7604_s_ctrl,
2286 static const struct v4l2_subdev_core_ops adv7604_core_ops = {
2287 .log_status = adv7604_log_status,
2288 .interrupt_service_routine = adv7604_isr,
2289 #ifdef CONFIG_VIDEO_ADV_DEBUG
2290 .g_register = adv7604_g_register,
2291 .s_register = adv7604_s_register,
2295 static const struct v4l2_subdev_video_ops adv7604_video_ops = {
2296 .s_routing = adv7604_s_routing,
2297 .g_input_status = adv7604_g_input_status,
2298 .s_dv_timings = adv7604_s_dv_timings,
2299 .g_dv_timings = adv7604_g_dv_timings,
2300 .query_dv_timings = adv7604_query_dv_timings,
2303 static const struct v4l2_subdev_pad_ops adv7604_pad_ops = {
2304 .enum_mbus_code = adv7604_enum_mbus_code,
2305 .get_fmt = adv7604_get_format,
2306 .set_fmt = adv7604_set_format,
2307 .get_edid = adv7604_get_edid,
2308 .set_edid = adv7604_set_edid,
2309 .dv_timings_cap = adv7604_dv_timings_cap,
2310 .enum_dv_timings = adv7604_enum_dv_timings,
2313 static const struct v4l2_subdev_ops adv7604_ops = {
2314 .core = &adv7604_core_ops,
2315 .video = &adv7604_video_ops,
2316 .pad = &adv7604_pad_ops,
2319 /* -------------------------- custom ctrls ---------------------------------- */
2321 static const struct v4l2_ctrl_config adv7604_ctrl_analog_sampling_phase = {
2322 .ops = &adv7604_ctrl_ops,
2323 .id = V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE,
2324 .name = "Analog Sampling Phase",
2325 .type = V4L2_CTRL_TYPE_INTEGER,
2332 static const struct v4l2_ctrl_config adv7604_ctrl_free_run_color_manual = {
2333 .ops = &adv7604_ctrl_ops,
2334 .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL,
2335 .name = "Free Running Color, Manual",
2336 .type = V4L2_CTRL_TYPE_BOOLEAN,
2343 static const struct v4l2_ctrl_config adv7604_ctrl_free_run_color = {
2344 .ops = &adv7604_ctrl_ops,
2345 .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR,
2346 .name = "Free Running Color",
2347 .type = V4L2_CTRL_TYPE_INTEGER,
2354 /* ----------------------------------------------------------------------- */
2356 static int adv7604_core_init(struct v4l2_subdev *sd)
2358 struct adv7604_state *state = to_state(sd);
2359 const struct adv7604_chip_info *info = state->info;
2360 struct adv7604_platform_data *pdata = &state->pdata;
2362 hdmi_write(sd, 0x48,
2363 (pdata->disable_pwrdnb ? 0x80 : 0) |
2364 (pdata->disable_cable_det_rst ? 0x40 : 0));
2368 if (pdata->default_input >= 0 &&
2369 pdata->default_input < state->source_pad) {
2370 state->selected_input = pdata->default_input;
2376 io_write(sd, 0x0c, 0x42); /* Power up part and power down VDP */
2377 io_write(sd, 0x0b, 0x44); /* Power down ESDP block */
2378 cp_write(sd, 0xcf, 0x01); /* Power down macrovision */
2381 io_write_clr_set(sd, 0x02, 0x0f,
2382 pdata->alt_gamma << 3 |
2383 pdata->op_656_range << 2 |
2384 pdata->alt_data_sat << 0);
2385 io_write_clr_set(sd, 0x05, 0x0e, pdata->blank_data << 3 |
2386 pdata->insert_av_codes << 2 |
2387 pdata->replicate_av_codes << 1);
2388 adv7604_setup_format(state);
2390 cp_write(sd, 0x69, 0x30); /* Enable CP CSC */
2392 /* VS, HS polarities */
2393 io_write(sd, 0x06, 0xa0 | pdata->inv_vs_pol << 2 |
2394 pdata->inv_hs_pol << 1 | pdata->inv_llc_pol);
2396 /* Adjust drive strength */
2397 io_write(sd, 0x14, 0x40 | pdata->dr_str_data << 4 |
2398 pdata->dr_str_clk << 2 |
2399 pdata->dr_str_sync);
2401 cp_write(sd, 0xba, (pdata->hdmi_free_run_mode << 1) | 0x01); /* HDMI free run */
2402 cp_write(sd, 0xf3, 0xdc); /* Low threshold to enter/exit free run mode */
2403 cp_write(sd, 0xf9, 0x23); /* STDI ch. 1 - LCVS change threshold -
2404 ADI recommended setting [REF_01, c. 2.3.3] */
2405 cp_write(sd, 0x45, 0x23); /* STDI ch. 2 - LCVS change threshold -
2406 ADI recommended setting [REF_01, c. 2.3.3] */
2407 cp_write(sd, 0xc9, 0x2d); /* use prim_mode and vid_std as free run resolution
2408 for digital formats */
2411 hdmi_write_clr_set(sd, 0x15, 0x03, 0x03); /* Mute on FIFO over-/underflow [REF_01, c. 1.2.18] */
2412 hdmi_write_clr_set(sd, 0x1a, 0x0e, 0x08); /* Wait 1 s before unmute */
2413 hdmi_write_clr_set(sd, 0x68, 0x06, 0x06); /* FIFO reset on over-/underflow [REF_01, c. 1.2.19] */
2415 /* TODO from platform data */
2416 afe_write(sd, 0xb5, 0x01); /* Setting MCLK to 256Fs */
2418 if (adv7604_has_afe(state)) {
2419 afe_write(sd, 0x02, pdata->ain_sel); /* Select analog input muxing mode */
2420 io_write_clr_set(sd, 0x30, 1 << 4, pdata->output_bus_lsb_to_msb << 4);
2424 io_write(sd, 0x40, 0xc0 | pdata->int1_config); /* Configure INT1 */
2425 io_write(sd, 0x46, 0x98); /* Enable SSPD, STDI and CP unlocked interrupts */
2426 io_write(sd, 0x6e, info->fmt_change_digital_mask); /* Enable V_LOCKED and DE_REGEN_LCK interrupts */
2427 io_write(sd, 0x73, info->cable_det_mask); /* Enable cable detection (+5v) interrupts */
2428 info->setup_irqs(sd);
2430 return v4l2_ctrl_handler_setup(sd->ctrl_handler);
2433 static void adv7604_setup_irqs(struct v4l2_subdev *sd)
2435 io_write(sd, 0x41, 0xd7); /* STDI irq for any change, disable INT2 */
2438 static void adv7611_setup_irqs(struct v4l2_subdev *sd)
2440 io_write(sd, 0x41, 0xd0); /* STDI irq for any change, disable INT2 */
2443 static void adv7604_unregister_clients(struct adv7604_state *state)
2447 for (i = 1; i < ARRAY_SIZE(state->i2c_clients); ++i) {
2448 if (state->i2c_clients[i])
2449 i2c_unregister_device(state->i2c_clients[i]);
2453 static struct i2c_client *adv7604_dummy_client(struct v4l2_subdev *sd,
2456 struct i2c_client *client = v4l2_get_subdevdata(sd);
2459 io_write(sd, io_reg, addr << 1);
2460 return i2c_new_dummy(client->adapter, io_read(sd, io_reg) >> 1);
2463 static const struct adv7604_reg_seq adv7604_recommended_settings_afe[] = {
2464 /* reset ADI recommended settings for HDMI: */
2465 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 4. */
2466 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x0d), 0x04 }, /* HDMI filter optimization */
2467 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x0d), 0x04 }, /* HDMI filter optimization */
2468 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x3d), 0x00 }, /* DDC bus active pull-up control */
2469 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x3e), 0x74 }, /* TMDS PLL optimization */
2470 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x4e), 0x3b }, /* TMDS PLL optimization */
2471 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x57), 0x74 }, /* TMDS PLL optimization */
2472 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x58), 0x63 }, /* TMDS PLL optimization */
2473 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8d), 0x18 }, /* equaliser */
2474 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8e), 0x34 }, /* equaliser */
2475 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x93), 0x88 }, /* equaliser */
2476 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x94), 0x2e }, /* equaliser */
2477 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x96), 0x00 }, /* enable automatic EQ changing */
2479 /* set ADI recommended settings for digitizer */
2480 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 17. */
2481 { ADV7604_REG(ADV7604_PAGE_AFE, 0x12), 0x7b }, /* ADC noise shaping filter controls */
2482 { ADV7604_REG(ADV7604_PAGE_AFE, 0x0c), 0x1f }, /* CP core gain controls */
2483 { ADV7604_REG(ADV7604_PAGE_CP, 0x3e), 0x04 }, /* CP core pre-gain control */
2484 { ADV7604_REG(ADV7604_PAGE_CP, 0xc3), 0x39 }, /* CP coast control. Graphics mode */
2485 { ADV7604_REG(ADV7604_PAGE_CP, 0x40), 0x5c }, /* CP core pre-gain control. Graphics mode */
2487 { ADV7604_REG_SEQ_TERM, 0 },
2490 static const struct adv7604_reg_seq adv7604_recommended_settings_hdmi[] = {
2491 /* set ADI recommended settings for HDMI: */
2492 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 4. */
2493 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x0d), 0x84 }, /* HDMI filter optimization */
2494 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x3d), 0x10 }, /* DDC bus active pull-up control */
2495 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x3e), 0x39 }, /* TMDS PLL optimization */
2496 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x4e), 0x3b }, /* TMDS PLL optimization */
2497 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x57), 0xb6 }, /* TMDS PLL optimization */
2498 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x58), 0x03 }, /* TMDS PLL optimization */
2499 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8d), 0x18 }, /* equaliser */
2500 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8e), 0x34 }, /* equaliser */
2501 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x93), 0x8b }, /* equaliser */
2502 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x94), 0x2d }, /* equaliser */
2503 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x96), 0x01 }, /* enable automatic EQ changing */
2505 /* reset ADI recommended settings for digitizer */
2506 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 17. */
2507 { ADV7604_REG(ADV7604_PAGE_AFE, 0x12), 0xfb }, /* ADC noise shaping filter controls */
2508 { ADV7604_REG(ADV7604_PAGE_AFE, 0x0c), 0x0d }, /* CP core gain controls */
2510 { ADV7604_REG_SEQ_TERM, 0 },
2513 static const struct adv7604_reg_seq adv7611_recommended_settings_hdmi[] = {
2514 /* ADV7611 Register Settings Recommendations Rev 1.5, May 2014 */
2515 { ADV7604_REG(ADV7604_PAGE_CP, 0x6c), 0x00 },
2516 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x9b), 0x03 },
2517 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x6f), 0x08 },
2518 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x85), 0x1f },
2519 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x87), 0x70 },
2520 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x57), 0xda },
2521 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x58), 0x01 },
2522 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x03), 0x98 },
2523 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x4c), 0x44 },
2524 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8d), 0x04 },
2525 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8e), 0x1e },
2527 { ADV7604_REG_SEQ_TERM, 0 },
2530 static const struct adv7604_chip_info adv7604_chip_info[] = {
2534 .max_port = ADV7604_PAD_VGA_COMP,
2536 .edid_enable_reg = 0x77,
2537 .edid_status_reg = 0x7d,
2539 .tdms_lock_mask = 0xe0,
2540 .cable_det_mask = 0x1e,
2541 .fmt_change_digital_mask = 0xc1,
2542 .formats = adv7604_formats,
2543 .nformats = ARRAY_SIZE(adv7604_formats),
2544 .set_termination = adv7604_set_termination,
2545 .setup_irqs = adv7604_setup_irqs,
2546 .read_hdmi_pixelclock = adv7604_read_hdmi_pixelclock,
2547 .read_cable_det = adv7604_read_cable_det,
2548 .recommended_settings = {
2549 [0] = adv7604_recommended_settings_afe,
2550 [1] = adv7604_recommended_settings_hdmi,
2552 .num_recommended_settings = {
2553 [0] = ARRAY_SIZE(adv7604_recommended_settings_afe),
2554 [1] = ARRAY_SIZE(adv7604_recommended_settings_hdmi),
2556 .page_mask = BIT(ADV7604_PAGE_IO) | BIT(ADV7604_PAGE_AVLINK) |
2557 BIT(ADV7604_PAGE_CEC) | BIT(ADV7604_PAGE_INFOFRAME) |
2558 BIT(ADV7604_PAGE_ESDP) | BIT(ADV7604_PAGE_DPP) |
2559 BIT(ADV7604_PAGE_AFE) | BIT(ADV7604_PAGE_REP) |
2560 BIT(ADV7604_PAGE_EDID) | BIT(ADV7604_PAGE_HDMI) |
2561 BIT(ADV7604_PAGE_TEST) | BIT(ADV7604_PAGE_CP) |
2562 BIT(ADV7604_PAGE_VDP),
2567 .max_port = ADV7604_PAD_HDMI_PORT_A,
2569 .edid_enable_reg = 0x74,
2570 .edid_status_reg = 0x76,
2572 .tdms_lock_mask = 0x43,
2573 .cable_det_mask = 0x01,
2574 .fmt_change_digital_mask = 0x03,
2575 .formats = adv7611_formats,
2576 .nformats = ARRAY_SIZE(adv7611_formats),
2577 .set_termination = adv7611_set_termination,
2578 .setup_irqs = adv7611_setup_irqs,
2579 .read_hdmi_pixelclock = adv7611_read_hdmi_pixelclock,
2580 .read_cable_det = adv7611_read_cable_det,
2581 .recommended_settings = {
2582 [1] = adv7611_recommended_settings_hdmi,
2584 .num_recommended_settings = {
2585 [1] = ARRAY_SIZE(adv7611_recommended_settings_hdmi),
2587 .page_mask = BIT(ADV7604_PAGE_IO) | BIT(ADV7604_PAGE_CEC) |
2588 BIT(ADV7604_PAGE_INFOFRAME) | BIT(ADV7604_PAGE_AFE) |
2589 BIT(ADV7604_PAGE_REP) | BIT(ADV7604_PAGE_EDID) |
2590 BIT(ADV7604_PAGE_HDMI) | BIT(ADV7604_PAGE_CP),
2594 static struct i2c_device_id adv7604_i2c_id[] = {
2595 { "adv7604", (kernel_ulong_t)&adv7604_chip_info[ADV7604] },
2596 { "adv7611", (kernel_ulong_t)&adv7604_chip_info[ADV7611] },
2599 MODULE_DEVICE_TABLE(i2c, adv7604_i2c_id);
2601 static struct of_device_id adv7604_of_id[] __maybe_unused = {
2602 { .compatible = "adi,adv7611", .data = &adv7604_chip_info[ADV7611] },
2605 MODULE_DEVICE_TABLE(of, adv7604_of_id);
2607 static int adv7604_parse_dt(struct adv7604_state *state)
2609 struct v4l2_of_endpoint bus_cfg;
2610 struct device_node *endpoint;
2611 struct device_node *np;
2614 np = state->i2c_clients[ADV7604_PAGE_IO]->dev.of_node;
2616 /* Parse the endpoint. */
2617 endpoint = of_graph_get_next_endpoint(np, NULL);
2621 v4l2_of_parse_endpoint(endpoint, &bus_cfg);
2622 of_node_put(endpoint);
2624 flags = bus_cfg.bus.parallel.flags;
2626 if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
2627 state->pdata.inv_hs_pol = 1;
2629 if (flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
2630 state->pdata.inv_vs_pol = 1;
2632 if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
2633 state->pdata.inv_llc_pol = 1;
2635 if (bus_cfg.bus_type == V4L2_MBUS_BT656) {
2636 state->pdata.insert_av_codes = 1;
2637 state->pdata.op_656_range = 1;
2640 /* Disable the interrupt for now as no DT-based board uses it. */
2641 state->pdata.int1_config = ADV7604_INT1_CONFIG_DISABLED;
2643 /* Use the default I2C addresses. */
2644 state->pdata.i2c_addresses[ADV7604_PAGE_AVLINK] = 0x42;
2645 state->pdata.i2c_addresses[ADV7604_PAGE_CEC] = 0x40;
2646 state->pdata.i2c_addresses[ADV7604_PAGE_INFOFRAME] = 0x3e;
2647 state->pdata.i2c_addresses[ADV7604_PAGE_ESDP] = 0x38;
2648 state->pdata.i2c_addresses[ADV7604_PAGE_DPP] = 0x3c;
2649 state->pdata.i2c_addresses[ADV7604_PAGE_AFE] = 0x26;
2650 state->pdata.i2c_addresses[ADV7604_PAGE_REP] = 0x32;
2651 state->pdata.i2c_addresses[ADV7604_PAGE_EDID] = 0x36;
2652 state->pdata.i2c_addresses[ADV7604_PAGE_HDMI] = 0x34;
2653 state->pdata.i2c_addresses[ADV7604_PAGE_TEST] = 0x30;
2654 state->pdata.i2c_addresses[ADV7604_PAGE_CP] = 0x22;
2655 state->pdata.i2c_addresses[ADV7604_PAGE_VDP] = 0x24;
2657 /* Hardcode the remaining platform data fields. */
2658 state->pdata.disable_pwrdnb = 0;
2659 state->pdata.disable_cable_det_rst = 0;
2660 state->pdata.default_input = -1;
2661 state->pdata.blank_data = 1;
2662 state->pdata.alt_data_sat = 1;
2663 state->pdata.op_format_mode_sel = ADV7604_OP_FORMAT_MODE0;
2664 state->pdata.bus_order = ADV7604_BUS_ORDER_RGB;
2669 static int adv7604_probe(struct i2c_client *client,
2670 const struct i2c_device_id *id)
2672 static const struct v4l2_dv_timings cea640x480 =
2673 V4L2_DV_BT_CEA_640X480P59_94;
2674 struct adv7604_state *state;
2675 struct v4l2_ctrl_handler *hdl;
2676 struct v4l2_subdev *sd;
2681 /* Check if the adapter supports the needed features */
2682 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
2684 v4l_dbg(1, debug, client, "detecting adv7604 client on address 0x%x\n",
2687 state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
2689 v4l_err(client, "Could not allocate adv7604_state memory!\n");
2693 state->i2c_clients[ADV7604_PAGE_IO] = client;
2695 /* initialize variables */
2696 state->restart_stdi_once = true;
2697 state->selected_input = ~0;
2699 if (IS_ENABLED(CONFIG_OF) && client->dev.of_node) {
2700 const struct of_device_id *oid;
2702 oid = of_match_node(adv7604_of_id, client->dev.of_node);
2703 state->info = oid->data;
2705 err = adv7604_parse_dt(state);
2707 v4l_err(client, "DT parsing error\n");
2710 } else if (client->dev.platform_data) {
2711 struct adv7604_platform_data *pdata = client->dev.platform_data;
2713 state->info = (const struct adv7604_chip_info *)id->driver_data;
2714 state->pdata = *pdata;
2716 v4l_err(client, "No platform data!\n");
2720 /* Request GPIOs. */
2721 for (i = 0; i < state->info->num_dv_ports; ++i) {
2722 state->hpd_gpio[i] =
2723 devm_gpiod_get_index(&client->dev, "hpd", i);
2724 if (IS_ERR(state->hpd_gpio[i]))
2727 gpiod_direction_output(state->hpd_gpio[i], 0);
2729 v4l_info(client, "Handling HPD %u GPIO\n", i);
2732 state->timings = cea640x480;
2733 state->format = adv7604_format_info(state, MEDIA_BUS_FMT_YUYV8_2X8);
2736 v4l2_i2c_subdev_init(sd, client, &adv7604_ops);
2737 snprintf(sd->name, sizeof(sd->name), "%s %d-%04x",
2738 id->name, i2c_adapter_id(client->adapter),
2740 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
2743 * Verify that the chip is present. On ADV7604 the RD_INFO register only
2744 * identifies the revision, while on ADV7611 it identifies the model as
2745 * well. Use the HDMI slave address on ADV7604 and RD_INFO on ADV7611.
2747 if (state->info->type == ADV7604) {
2748 val = adv_smbus_read_byte_data_check(client, 0xfb, false);
2750 v4l2_info(sd, "not an adv7604 on address 0x%x\n",
2755 val = (adv_smbus_read_byte_data_check(client, 0xea, false) << 8)
2756 | (adv_smbus_read_byte_data_check(client, 0xeb, false) << 0);
2757 if (val != 0x2051) {
2758 v4l2_info(sd, "not an adv7611 on address 0x%x\n",
2764 /* control handlers */
2766 v4l2_ctrl_handler_init(hdl, adv7604_has_afe(state) ? 9 : 8);
2768 v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
2769 V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
2770 v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
2771 V4L2_CID_CONTRAST, 0, 255, 1, 128);
2772 v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
2773 V4L2_CID_SATURATION, 0, 255, 1, 128);
2774 v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
2775 V4L2_CID_HUE, 0, 128, 1, 0);
2777 /* private controls */
2778 state->detect_tx_5v_ctrl = v4l2_ctrl_new_std(hdl, NULL,
2779 V4L2_CID_DV_RX_POWER_PRESENT, 0,
2780 (1 << state->info->num_dv_ports) - 1, 0, 0);
2781 state->rgb_quantization_range_ctrl =
2782 v4l2_ctrl_new_std_menu(hdl, &adv7604_ctrl_ops,
2783 V4L2_CID_DV_RX_RGB_RANGE, V4L2_DV_RGB_RANGE_FULL,
2784 0, V4L2_DV_RGB_RANGE_AUTO);
2786 /* custom controls */
2787 if (adv7604_has_afe(state))
2788 state->analog_sampling_phase_ctrl =
2789 v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_analog_sampling_phase, NULL);
2790 state->free_run_color_manual_ctrl =
2791 v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_free_run_color_manual, NULL);
2792 state->free_run_color_ctrl =
2793 v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_free_run_color, NULL);
2795 sd->ctrl_handler = hdl;
2800 state->detect_tx_5v_ctrl->is_private = true;
2801 state->rgb_quantization_range_ctrl->is_private = true;
2802 if (adv7604_has_afe(state))
2803 state->analog_sampling_phase_ctrl->is_private = true;
2804 state->free_run_color_manual_ctrl->is_private = true;
2805 state->free_run_color_ctrl->is_private = true;
2807 if (adv7604_s_detect_tx_5v_ctrl(sd)) {
2812 for (i = 1; i < ADV7604_PAGE_MAX; ++i) {
2813 if (!(BIT(i) & state->info->page_mask))
2816 state->i2c_clients[i] =
2817 adv7604_dummy_client(sd, state->pdata.i2c_addresses[i],
2819 if (state->i2c_clients[i] == NULL) {
2821 v4l2_err(sd, "failed to create i2c client %u\n", i);
2827 state->work_queues = create_singlethread_workqueue(client->name);
2828 if (!state->work_queues) {
2829 v4l2_err(sd, "Could not create work queue\n");
2834 INIT_DELAYED_WORK(&state->delayed_work_enable_hotplug,
2835 adv7604_delayed_work_enable_hotplug);
2837 state->source_pad = state->info->num_dv_ports
2838 + (state->info->has_afe ? 2 : 0);
2839 for (i = 0; i < state->source_pad; ++i)
2840 state->pads[i].flags = MEDIA_PAD_FL_SINK;
2841 state->pads[state->source_pad].flags = MEDIA_PAD_FL_SOURCE;
2843 err = media_entity_init(&sd->entity, state->source_pad + 1,
2846 goto err_work_queues;
2848 err = adv7604_core_init(sd);
2851 v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
2852 client->addr << 1, client->adapter->name);
2854 err = v4l2_async_register_subdev(sd);
2861 media_entity_cleanup(&sd->entity);
2863 cancel_delayed_work(&state->delayed_work_enable_hotplug);
2864 destroy_workqueue(state->work_queues);
2866 adv7604_unregister_clients(state);
2868 v4l2_ctrl_handler_free(hdl);
2872 /* ----------------------------------------------------------------------- */
2874 static int adv7604_remove(struct i2c_client *client)
2876 struct v4l2_subdev *sd = i2c_get_clientdata(client);
2877 struct adv7604_state *state = to_state(sd);
2879 cancel_delayed_work(&state->delayed_work_enable_hotplug);
2880 destroy_workqueue(state->work_queues);
2881 v4l2_async_unregister_subdev(sd);
2882 v4l2_device_unregister_subdev(sd);
2883 media_entity_cleanup(&sd->entity);
2884 adv7604_unregister_clients(to_state(sd));
2885 v4l2_ctrl_handler_free(sd->ctrl_handler);
2889 /* ----------------------------------------------------------------------- */
2891 static struct i2c_driver adv7604_driver = {
2893 .owner = THIS_MODULE,
2895 .of_match_table = of_match_ptr(adv7604_of_id),
2897 .probe = adv7604_probe,
2898 .remove = adv7604_remove,
2899 .id_table = adv7604_i2c_id,
2902 module_i2c_driver(adv7604_driver);