2 * Analog Devices ADV7511 HDMI Transmitter Device Driver
4 * Copyright 2013 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
21 * This file is named adv7511-v4l2.c so it doesn't conflict with the Analog
22 * Device ADV7511 (config fragment CONFIG_DRM_I2C_ADV7511).
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/slab.h>
29 #include <linux/i2c.h>
30 #include <linux/delay.h>
31 #include <linux/videodev2.h>
32 #include <linux/gpio.h>
33 #include <linux/workqueue.h>
34 #include <linux/hdmi.h>
35 #include <linux/v4l2-dv-timings.h>
36 #include <media/v4l2-device.h>
37 #include <media/v4l2-common.h>
38 #include <media/v4l2-ctrls.h>
39 #include <media/v4l2-dv-timings.h>
40 #include <media/adv7511.h>
43 module_param(debug, int, 0644);
44 MODULE_PARM_DESC(debug, "debug level (0-2)");
46 MODULE_DESCRIPTION("Analog Devices ADV7511 HDMI Transmitter Device Driver");
47 MODULE_AUTHOR("Hans Verkuil");
48 MODULE_LICENSE("GPL v2");
50 #define MASK_ADV7511_EDID_RDY_INT 0x04
51 #define MASK_ADV7511_MSEN_INT 0x40
52 #define MASK_ADV7511_HPD_INT 0x80
54 #define MASK_ADV7511_HPD_DETECT 0x40
55 #define MASK_ADV7511_MSEN_DETECT 0x20
56 #define MASK_ADV7511_EDID_RDY 0x10
58 #define EDID_MAX_RETRIES (8)
59 #define EDID_DELAY 250
60 #define EDID_MAX_SEGM 8
62 #define ADV7511_MAX_WIDTH 1920
63 #define ADV7511_MAX_HEIGHT 1200
64 #define ADV7511_MIN_PIXELCLOCK 20000000
65 #define ADV7511_MAX_PIXELCLOCK 225000000
68 **********************************************************************
70 * Arrays with configuration parameters for the ADV7511
72 **********************************************************************
75 struct i2c_reg_value {
80 struct adv7511_state_edid {
81 /* total number of blocks */
83 /* Number of segments read */
85 u8 data[EDID_MAX_SEGM * 256];
86 /* Number of EDID read retries left */
87 unsigned read_retries;
91 struct adv7511_state {
92 struct adv7511_platform_data pdata;
93 struct v4l2_subdev sd;
95 struct v4l2_ctrl_handler hdl;
100 /* Is the adv7511 powered on? */
102 /* Did we receive hotplug and rx-sense signals? */
104 /* timings from s_dv_timings */
105 struct v4l2_dv_timings dv_timings;
112 struct v4l2_ctrl *hdmi_mode_ctrl;
113 struct v4l2_ctrl *hotplug_ctrl;
114 struct v4l2_ctrl *rx_sense_ctrl;
115 struct v4l2_ctrl *have_edid0_ctrl;
116 struct v4l2_ctrl *rgb_quantization_range_ctrl;
117 struct i2c_client *i2c_edid;
118 struct i2c_client *i2c_pktmem;
119 struct adv7511_state_edid edid;
120 /* Running counter of the number of detected EDIDs (for debugging) */
121 unsigned edid_detect_counter;
122 struct workqueue_struct *work_queue;
123 struct delayed_work edid_handler; /* work entry */
126 static void adv7511_check_monitor_present_status(struct v4l2_subdev *sd);
127 static bool adv7511_check_edid_status(struct v4l2_subdev *sd);
128 static void adv7511_setup(struct v4l2_subdev *sd);
129 static int adv7511_s_i2s_clock_freq(struct v4l2_subdev *sd, u32 freq);
130 static int adv7511_s_clock_freq(struct v4l2_subdev *sd, u32 freq);
133 static const struct v4l2_dv_timings_cap adv7511_timings_cap = {
134 .type = V4L2_DV_BT_656_1120,
135 /* keep this initialization for compatibility with GCC < 4.4.6 */
137 V4L2_INIT_BT_TIMINGS(0, ADV7511_MAX_WIDTH, 0, ADV7511_MAX_HEIGHT,
138 ADV7511_MIN_PIXELCLOCK, ADV7511_MAX_PIXELCLOCK,
139 V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
140 V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT,
141 V4L2_DV_BT_CAP_PROGRESSIVE | V4L2_DV_BT_CAP_REDUCED_BLANKING |
142 V4L2_DV_BT_CAP_CUSTOM)
145 static inline struct adv7511_state *get_adv7511_state(struct v4l2_subdev *sd)
147 return container_of(sd, struct adv7511_state, sd);
150 static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
152 return &container_of(ctrl->handler, struct adv7511_state, hdl)->sd;
155 /* ------------------------ I2C ----------------------------------------------- */
157 static s32 adv_smbus_read_byte_data_check(struct i2c_client *client,
158 u8 command, bool check)
160 union i2c_smbus_data data;
162 if (!i2c_smbus_xfer(client->adapter, client->addr, client->flags,
163 I2C_SMBUS_READ, command,
164 I2C_SMBUS_BYTE_DATA, &data))
167 v4l_err(client, "error reading %02x, %02x\n",
168 client->addr, command);
172 static s32 adv_smbus_read_byte_data(struct i2c_client *client, u8 command)
175 for (i = 0; i < 3; i++) {
176 int ret = adv_smbus_read_byte_data_check(client, command, true);
179 v4l_err(client, "read ok after %d retries\n", i);
183 v4l_err(client, "read failed\n");
187 static int adv7511_rd(struct v4l2_subdev *sd, u8 reg)
189 struct i2c_client *client = v4l2_get_subdevdata(sd);
191 return adv_smbus_read_byte_data(client, reg);
194 static int adv7511_wr(struct v4l2_subdev *sd, u8 reg, u8 val)
196 struct i2c_client *client = v4l2_get_subdevdata(sd);
200 for (i = 0; i < 3; i++) {
201 ret = i2c_smbus_write_byte_data(client, reg, val);
205 v4l2_err(sd, "%s: i2c write error\n", __func__);
209 /* To set specific bits in the register, a clear-mask is given (to be AND-ed),
210 and then the value-mask (to be OR-ed). */
211 static inline void adv7511_wr_and_or(struct v4l2_subdev *sd, u8 reg, u8 clr_mask, u8 val_mask)
213 adv7511_wr(sd, reg, (adv7511_rd(sd, reg) & clr_mask) | val_mask);
216 static int adv_smbus_read_i2c_block_data(struct i2c_client *client,
217 u8 command, unsigned length, u8 *values)
219 union i2c_smbus_data data;
222 if (length > I2C_SMBUS_BLOCK_MAX)
223 length = I2C_SMBUS_BLOCK_MAX;
224 data.block[0] = length;
226 ret = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
227 I2C_SMBUS_READ, command,
228 I2C_SMBUS_I2C_BLOCK_DATA, &data);
229 memcpy(values, data.block + 1, length);
233 static inline void adv7511_edid_rd(struct v4l2_subdev *sd, u16 len, u8 *buf)
235 struct adv7511_state *state = get_adv7511_state(sd);
239 v4l2_dbg(1, debug, sd, "%s:\n", __func__);
241 for (i = 0; !err && i < len; i += I2C_SMBUS_BLOCK_MAX)
242 err = adv_smbus_read_i2c_block_data(state->i2c_edid, i,
243 I2C_SMBUS_BLOCK_MAX, buf + i);
245 v4l2_err(sd, "%s: i2c read error\n", __func__);
248 static int adv7511_pktmem_rd(struct v4l2_subdev *sd, u8 reg)
250 struct adv7511_state *state = get_adv7511_state(sd);
252 return adv_smbus_read_byte_data(state->i2c_pktmem, reg);
255 static int adv7511_pktmem_wr(struct v4l2_subdev *sd, u8 reg, u8 val)
257 struct adv7511_state *state = get_adv7511_state(sd);
261 for (i = 0; i < 3; i++) {
262 ret = i2c_smbus_write_byte_data(state->i2c_pktmem, reg, val);
266 v4l2_err(sd, "%s: i2c write error\n", __func__);
270 /* To set specific bits in the register, a clear-mask is given (to be AND-ed),
271 and then the value-mask (to be OR-ed). */
272 static inline void adv7511_pktmem_wr_and_or(struct v4l2_subdev *sd, u8 reg, u8 clr_mask, u8 val_mask)
274 adv7511_pktmem_wr(sd, reg, (adv7511_pktmem_rd(sd, reg) & clr_mask) | val_mask);
277 static inline bool adv7511_have_hotplug(struct v4l2_subdev *sd)
279 return adv7511_rd(sd, 0x42) & MASK_ADV7511_HPD_DETECT;
282 static inline bool adv7511_have_rx_sense(struct v4l2_subdev *sd)
284 return adv7511_rd(sd, 0x42) & MASK_ADV7511_MSEN_DETECT;
287 static void adv7511_csc_conversion_mode(struct v4l2_subdev *sd, u8 mode)
289 adv7511_wr_and_or(sd, 0x18, 0x9f, (mode & 0x3)<<5);
292 static void adv7511_csc_coeff(struct v4l2_subdev *sd,
293 u16 A1, u16 A2, u16 A3, u16 A4,
294 u16 B1, u16 B2, u16 B3, u16 B4,
295 u16 C1, u16 C2, u16 C3, u16 C4)
298 adv7511_wr_and_or(sd, 0x18, 0xe0, A1>>8);
299 adv7511_wr(sd, 0x19, A1);
300 adv7511_wr_and_or(sd, 0x1A, 0xe0, A2>>8);
301 adv7511_wr(sd, 0x1B, A2);
302 adv7511_wr_and_or(sd, 0x1c, 0xe0, A3>>8);
303 adv7511_wr(sd, 0x1d, A3);
304 adv7511_wr_and_or(sd, 0x1e, 0xe0, A4>>8);
305 adv7511_wr(sd, 0x1f, A4);
308 adv7511_wr_and_or(sd, 0x20, 0xe0, B1>>8);
309 adv7511_wr(sd, 0x21, B1);
310 adv7511_wr_and_or(sd, 0x22, 0xe0, B2>>8);
311 adv7511_wr(sd, 0x23, B2);
312 adv7511_wr_and_or(sd, 0x24, 0xe0, B3>>8);
313 adv7511_wr(sd, 0x25, B3);
314 adv7511_wr_and_or(sd, 0x26, 0xe0, B4>>8);
315 adv7511_wr(sd, 0x27, B4);
318 adv7511_wr_and_or(sd, 0x28, 0xe0, C1>>8);
319 adv7511_wr(sd, 0x29, C1);
320 adv7511_wr_and_or(sd, 0x2A, 0xe0, C2>>8);
321 adv7511_wr(sd, 0x2B, C2);
322 adv7511_wr_and_or(sd, 0x2C, 0xe0, C3>>8);
323 adv7511_wr(sd, 0x2D, C3);
324 adv7511_wr_and_or(sd, 0x2E, 0xe0, C4>>8);
325 adv7511_wr(sd, 0x2F, C4);
328 static void adv7511_csc_rgb_full2limit(struct v4l2_subdev *sd, bool enable)
332 adv7511_csc_conversion_mode(sd, csc_mode);
333 adv7511_csc_coeff(sd,
336 0, 0, 4096-564, 256);
338 adv7511_wr_and_or(sd, 0x18, 0x7f, 0x80);
339 /* AVI infoframe: Limited range RGB (16-235) */
340 adv7511_wr_and_or(sd, 0x57, 0xf3, 0x04);
343 adv7511_wr_and_or(sd, 0x18, 0x7f, 0x0);
344 /* AVI infoframe: Full range RGB (0-255) */
345 adv7511_wr_and_or(sd, 0x57, 0xf3, 0x08);
349 static void adv7511_set_IT_content_AVI_InfoFrame(struct v4l2_subdev *sd)
351 struct adv7511_state *state = get_adv7511_state(sd);
352 if (state->dv_timings.bt.flags & V4L2_DV_FL_IS_CE_VIDEO) {
353 /* CE format, not IT */
354 adv7511_wr_and_or(sd, 0x57, 0x7f, 0x00);
357 adv7511_wr_and_or(sd, 0x57, 0x7f, 0x80);
361 static int adv7511_set_rgb_quantization_mode(struct v4l2_subdev *sd, struct v4l2_ctrl *ctrl)
367 case V4L2_DV_RGB_RANGE_AUTO: {
369 struct adv7511_state *state = get_adv7511_state(sd);
371 if (state->dv_timings.bt.flags & V4L2_DV_FL_IS_CE_VIDEO) {
372 /* CE format, RGB limited range (16-235) */
373 adv7511_csc_rgb_full2limit(sd, true);
375 /* not CE format, RGB full range (0-255) */
376 adv7511_csc_rgb_full2limit(sd, false);
380 case V4L2_DV_RGB_RANGE_LIMITED:
381 /* RGB limited range (16-235) */
382 adv7511_csc_rgb_full2limit(sd, true);
384 case V4L2_DV_RGB_RANGE_FULL:
385 /* RGB full range (0-255) */
386 adv7511_csc_rgb_full2limit(sd, false);
392 /* ------------------------------ CTRL OPS ------------------------------ */
394 static int adv7511_s_ctrl(struct v4l2_ctrl *ctrl)
396 struct v4l2_subdev *sd = to_sd(ctrl);
397 struct adv7511_state *state = get_adv7511_state(sd);
399 v4l2_dbg(1, debug, sd, "%s: ctrl id: %d, ctrl->val %d\n", __func__, ctrl->id, ctrl->val);
401 if (state->hdmi_mode_ctrl == ctrl) {
402 /* Set HDMI or DVI-D */
403 adv7511_wr_and_or(sd, 0xaf, 0xfd, ctrl->val == V4L2_DV_TX_MODE_HDMI ? 0x02 : 0x00);
406 if (state->rgb_quantization_range_ctrl == ctrl)
407 return adv7511_set_rgb_quantization_mode(sd, ctrl);
412 static const struct v4l2_ctrl_ops adv7511_ctrl_ops = {
413 .s_ctrl = adv7511_s_ctrl,
416 /* ---------------------------- CORE OPS ------------------------------------------- */
418 #ifdef CONFIG_VIDEO_ADV_DEBUG
419 static void adv7511_inv_register(struct v4l2_subdev *sd)
421 v4l2_info(sd, "0x000-0x0ff: Main Map\n");
424 static int adv7511_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
427 switch (reg->reg >> 8) {
429 reg->val = adv7511_rd(sd, reg->reg & 0xff);
432 v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
433 adv7511_inv_register(sd);
439 static int adv7511_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
441 switch (reg->reg >> 8) {
443 adv7511_wr(sd, reg->reg & 0xff, reg->val & 0xff);
446 v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
447 adv7511_inv_register(sd);
454 struct adv7511_cfg_read_infoframe {
462 static u8 hdmi_infoframe_checksum(u8 *ptr, size_t size)
467 /* compute checksum */
468 for (i = 0; i < size; i++)
474 static void log_infoframe(struct v4l2_subdev *sd, const struct adv7511_cfg_read_infoframe *cri)
476 struct i2c_client *client = v4l2_get_subdevdata(sd);
477 struct device *dev = &client->dev;
478 union hdmi_infoframe frame;
483 if (!(adv7511_rd(sd, cri->present_reg) & cri->present_mask)) {
484 v4l2_info(sd, "%s infoframe not transmitted\n", cri->desc);
488 memcpy(buffer, cri->header, sizeof(cri->header));
492 if (len + 4 > sizeof(buffer)) {
493 v4l2_err(sd, "%s: invalid %s infoframe length %d\n", __func__, cri->desc, len);
497 if (cri->payload_addr >= 0x100) {
498 for (i = 0; i < len; i++)
499 buffer[i + 4] = adv7511_pktmem_rd(sd, cri->payload_addr + i - 0x100);
501 for (i = 0; i < len; i++)
502 buffer[i + 4] = adv7511_rd(sd, cri->payload_addr + i);
505 buffer[3] = hdmi_infoframe_checksum(buffer, len + 4);
507 if (hdmi_infoframe_unpack(&frame, buffer) < 0) {
508 v4l2_err(sd, "%s: unpack of %s infoframe failed\n", __func__, cri->desc);
512 hdmi_infoframe_log(KERN_INFO, dev, &frame);
515 static void adv7511_log_infoframes(struct v4l2_subdev *sd)
517 static const struct adv7511_cfg_read_infoframe cri[] = {
518 { "AVI", 0x44, 0x10, { 0x82, 2, 13 }, 0x55 },
519 { "Audio", 0x44, 0x08, { 0x84, 1, 10 }, 0x73 },
520 { "SDP", 0x40, 0x40, { 0x83, 1, 25 }, 0x103 },
524 for (i = 0; i < ARRAY_SIZE(cri); i++)
525 log_infoframe(sd, &cri[i]);
528 static int adv7511_log_status(struct v4l2_subdev *sd)
530 struct adv7511_state *state = get_adv7511_state(sd);
531 struct adv7511_state_edid *edid = &state->edid;
533 static const char * const states[] = {
539 "initializing HDCP repeater",
540 "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"
542 static const char * const errors[] = {
549 "max repeater cascade exceeded",
552 "9", "A", "B", "C", "D", "E", "F"
555 v4l2_info(sd, "power %s\n", state->power_on ? "on" : "off");
556 v4l2_info(sd, "%s hotplug, %s Rx Sense, %s EDID (%d block(s))\n",
557 (adv7511_rd(sd, 0x42) & MASK_ADV7511_HPD_DETECT) ? "detected" : "no",
558 (adv7511_rd(sd, 0x42) & MASK_ADV7511_MSEN_DETECT) ? "detected" : "no",
559 edid->segments ? "found" : "no",
561 v4l2_info(sd, "%s output %s\n",
562 (adv7511_rd(sd, 0xaf) & 0x02) ?
564 (adv7511_rd(sd, 0xa1) & 0x3c) ?
565 "disabled" : "enabled");
566 v4l2_info(sd, "state: %s, error: %s, detect count: %u, msk/irq: %02x/%02x\n",
567 states[adv7511_rd(sd, 0xc8) & 0xf],
568 errors[adv7511_rd(sd, 0xc8) >> 4], state->edid_detect_counter,
569 adv7511_rd(sd, 0x94), adv7511_rd(sd, 0x96));
570 v4l2_info(sd, "RGB quantization: %s range\n", adv7511_rd(sd, 0x18) & 0x80 ? "limited" : "full");
571 if (adv7511_rd(sd, 0xaf) & 0x02) {
573 u8 manual_cts = adv7511_rd(sd, 0x0a) & 0x80;
574 u32 N = (adv7511_rd(sd, 0x01) & 0xf) << 16 |
575 adv7511_rd(sd, 0x02) << 8 |
576 adv7511_rd(sd, 0x03);
577 u8 vic_detect = adv7511_rd(sd, 0x3e) >> 2;
578 u8 vic_sent = adv7511_rd(sd, 0x3d) & 0x3f;
582 CTS = (adv7511_rd(sd, 0x07) & 0xf) << 16 |
583 adv7511_rd(sd, 0x08) << 8 |
584 adv7511_rd(sd, 0x09);
586 CTS = (adv7511_rd(sd, 0x04) & 0xf) << 16 |
587 adv7511_rd(sd, 0x05) << 8 |
588 adv7511_rd(sd, 0x06);
589 v4l2_info(sd, "CTS %s mode: N %d, CTS %d\n",
590 manual_cts ? "manual" : "automatic", N, CTS);
591 v4l2_info(sd, "VIC: detected %d, sent %d\n",
592 vic_detect, vic_sent);
593 adv7511_log_infoframes(sd);
595 if (state->dv_timings.type == V4L2_DV_BT_656_1120)
596 v4l2_print_dv_timings(sd->name, "timings: ",
597 &state->dv_timings, false);
599 v4l2_info(sd, "no timings set\n");
600 v4l2_info(sd, "i2c edid addr: 0x%x\n", state->i2c_edid_addr);
601 v4l2_info(sd, "i2c cec addr: 0x%x\n", state->i2c_cec_addr);
602 v4l2_info(sd, "i2c pktmem addr: 0x%x\n", state->i2c_pktmem_addr);
606 /* Power up/down adv7511 */
607 static int adv7511_s_power(struct v4l2_subdev *sd, int on)
609 struct adv7511_state *state = get_adv7511_state(sd);
610 const int retries = 20;
613 v4l2_dbg(1, debug, sd, "%s: power %s\n", __func__, on ? "on" : "off");
615 state->power_on = on;
619 adv7511_wr_and_or(sd, 0x41, 0xbf, 0x40);
624 /* The adv7511 does not always come up immediately.
625 Retry multiple times. */
626 for (i = 0; i < retries; i++) {
627 adv7511_wr_and_or(sd, 0x41, 0xbf, 0x0);
628 if ((adv7511_rd(sd, 0x41) & 0x40) == 0)
630 adv7511_wr_and_or(sd, 0x41, 0xbf, 0x40);
634 v4l2_dbg(1, debug, sd, "%s: failed to powerup the adv7511!\n", __func__);
635 adv7511_s_power(sd, 0);
639 v4l2_dbg(1, debug, sd, "%s: needed %d retries to powerup the adv7511\n", __func__, i);
641 /* Reserved registers that must be set */
642 adv7511_wr(sd, 0x98, 0x03);
643 adv7511_wr_and_or(sd, 0x9a, 0xfe, 0x70);
644 adv7511_wr(sd, 0x9c, 0x30);
645 adv7511_wr_and_or(sd, 0x9d, 0xfc, 0x01);
646 adv7511_wr(sd, 0xa2, 0xa4);
647 adv7511_wr(sd, 0xa3, 0xa4);
648 adv7511_wr(sd, 0xe0, 0xd0);
649 adv7511_wr(sd, 0xf9, 0x00);
651 adv7511_wr(sd, 0x43, state->i2c_edid_addr);
652 adv7511_wr(sd, 0x45, state->i2c_pktmem_addr);
654 /* Set number of attempts to read the EDID */
655 adv7511_wr(sd, 0xc9, 0xf);
659 /* Enable interrupts */
660 static void adv7511_set_isr(struct v4l2_subdev *sd, bool enable)
662 u8 irqs = MASK_ADV7511_HPD_INT | MASK_ADV7511_MSEN_INT;
666 v4l2_dbg(2, debug, sd, "%s: %s\n", __func__, enable ? "enable" : "disable");
668 /* The datasheet says that the EDID ready interrupt should be
669 disabled if there is no hotplug. */
672 else if (adv7511_have_hotplug(sd))
673 irqs |= MASK_ADV7511_EDID_RDY_INT;
676 * This i2c write can fail (approx. 1 in 1000 writes). But it
677 * is essential that this register is correct, so retry it
680 * Note that the i2c write does not report an error, but the readback
681 * clearly shows the wrong value.
684 adv7511_wr(sd, 0x94, irqs);
685 irqs_rd = adv7511_rd(sd, 0x94);
686 } while (retries-- && irqs_rd != irqs);
690 v4l2_err(sd, "Could not set interrupts: hw failure?\n");
693 /* Interrupt handler */
694 static int adv7511_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
698 /* disable interrupts to prevent a race condition */
699 adv7511_set_isr(sd, false);
700 irq_status = adv7511_rd(sd, 0x96);
701 /* clear detected interrupts */
702 adv7511_wr(sd, 0x96, irq_status);
704 v4l2_dbg(1, debug, sd, "%s: irq 0x%x\n", __func__, irq_status);
706 if (irq_status & (MASK_ADV7511_HPD_INT | MASK_ADV7511_MSEN_INT))
707 adv7511_check_monitor_present_status(sd);
708 if (irq_status & MASK_ADV7511_EDID_RDY_INT)
709 adv7511_check_edid_status(sd);
711 /* enable interrupts */
712 adv7511_set_isr(sd, true);
719 static const struct v4l2_subdev_core_ops adv7511_core_ops = {
720 .log_status = adv7511_log_status,
721 #ifdef CONFIG_VIDEO_ADV_DEBUG
722 .g_register = adv7511_g_register,
723 .s_register = adv7511_s_register,
725 .s_power = adv7511_s_power,
726 .interrupt_service_routine = adv7511_isr,
729 /* ------------------------------ VIDEO OPS ------------------------------ */
731 /* Enable/disable adv7511 output */
732 static int adv7511_s_stream(struct v4l2_subdev *sd, int enable)
734 struct adv7511_state *state = get_adv7511_state(sd);
736 v4l2_dbg(1, debug, sd, "%s: %sable\n", __func__, (enable ? "en" : "dis"));
737 adv7511_wr_and_or(sd, 0xa1, ~0x3c, (enable ? 0 : 0x3c));
739 adv7511_check_monitor_present_status(sd);
741 adv7511_s_power(sd, 0);
742 state->have_monitor = false;
747 static int adv7511_s_dv_timings(struct v4l2_subdev *sd,
748 struct v4l2_dv_timings *timings)
750 struct adv7511_state *state = get_adv7511_state(sd);
752 v4l2_dbg(1, debug, sd, "%s:\n", __func__);
754 /* quick sanity check */
755 if (!v4l2_valid_dv_timings(timings, &adv7511_timings_cap, NULL, NULL))
758 /* Fill the optional fields .standards and .flags in struct v4l2_dv_timings
759 if the format is one of the CEA or DMT timings. */
760 v4l2_find_dv_timings_cap(timings, &adv7511_timings_cap, 0, NULL, NULL);
762 timings->bt.flags &= ~V4L2_DV_FL_REDUCED_FPS;
765 state->dv_timings = *timings;
767 /* update quantization range based on new dv_timings */
768 adv7511_set_rgb_quantization_mode(sd, state->rgb_quantization_range_ctrl);
770 /* update AVI infoframe */
771 adv7511_set_IT_content_AVI_InfoFrame(sd);
776 static int adv7511_g_dv_timings(struct v4l2_subdev *sd,
777 struct v4l2_dv_timings *timings)
779 struct adv7511_state *state = get_adv7511_state(sd);
781 v4l2_dbg(1, debug, sd, "%s:\n", __func__);
786 *timings = state->dv_timings;
791 static int adv7511_enum_dv_timings(struct v4l2_subdev *sd,
792 struct v4l2_enum_dv_timings *timings)
794 if (timings->pad != 0)
797 return v4l2_enum_dv_timings_cap(timings, &adv7511_timings_cap, NULL, NULL);
800 static int adv7511_dv_timings_cap(struct v4l2_subdev *sd,
801 struct v4l2_dv_timings_cap *cap)
806 *cap = adv7511_timings_cap;
810 static const struct v4l2_subdev_video_ops adv7511_video_ops = {
811 .s_stream = adv7511_s_stream,
812 .s_dv_timings = adv7511_s_dv_timings,
813 .g_dv_timings = adv7511_g_dv_timings,
816 /* ------------------------------ AUDIO OPS ------------------------------ */
817 static int adv7511_s_audio_stream(struct v4l2_subdev *sd, int enable)
819 v4l2_dbg(1, debug, sd, "%s: %sable\n", __func__, (enable ? "en" : "dis"));
822 adv7511_wr_and_or(sd, 0x4b, 0x3f, 0x80);
824 adv7511_wr_and_or(sd, 0x4b, 0x3f, 0x40);
829 static int adv7511_s_clock_freq(struct v4l2_subdev *sd, u32 freq)
834 case 32000: N = 4096; break;
835 case 44100: N = 6272; break;
836 case 48000: N = 6144; break;
837 case 88200: N = 12544; break;
838 case 96000: N = 12288; break;
839 case 176400: N = 25088; break;
840 case 192000: N = 24576; break;
845 /* Set N (used with CTS to regenerate the audio clock) */
846 adv7511_wr(sd, 0x01, (N >> 16) & 0xf);
847 adv7511_wr(sd, 0x02, (N >> 8) & 0xff);
848 adv7511_wr(sd, 0x03, N & 0xff);
853 static int adv7511_s_i2s_clock_freq(struct v4l2_subdev *sd, u32 freq)
858 case 32000: i2s_sf = 0x30; break;
859 case 44100: i2s_sf = 0x00; break;
860 case 48000: i2s_sf = 0x20; break;
861 case 88200: i2s_sf = 0x80; break;
862 case 96000: i2s_sf = 0xa0; break;
863 case 176400: i2s_sf = 0xc0; break;
864 case 192000: i2s_sf = 0xe0; break;
869 /* Set sampling frequency for I2S audio to 48 kHz */
870 adv7511_wr_and_or(sd, 0x15, 0xf, i2s_sf);
875 static int adv7511_s_routing(struct v4l2_subdev *sd, u32 input, u32 output, u32 config)
877 /* Only 2 channels in use for application */
878 adv7511_wr_and_or(sd, 0x73, 0xf8, 0x1);
879 /* Speaker mapping */
880 adv7511_wr(sd, 0x76, 0x00);
882 /* 16 bit audio word length */
883 adv7511_wr_and_or(sd, 0x14, 0xf0, 0x02);
888 static const struct v4l2_subdev_audio_ops adv7511_audio_ops = {
889 .s_stream = adv7511_s_audio_stream,
890 .s_clock_freq = adv7511_s_clock_freq,
891 .s_i2s_clock_freq = adv7511_s_i2s_clock_freq,
892 .s_routing = adv7511_s_routing,
895 /* ---------------------------- PAD OPS ------------------------------------- */
897 static int adv7511_get_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
899 struct adv7511_state *state = get_adv7511_state(sd);
901 memset(edid->reserved, 0, sizeof(edid->reserved));
906 if (edid->start_block == 0 && edid->blocks == 0) {
907 edid->blocks = state->edid.segments * 2;
911 if (state->edid.segments == 0)
914 if (edid->start_block >= state->edid.segments * 2)
917 if (edid->start_block + edid->blocks > state->edid.segments * 2)
918 edid->blocks = state->edid.segments * 2 - edid->start_block;
920 memcpy(edid->edid, &state->edid.data[edid->start_block * 128],
926 static int adv7511_enum_mbus_code(struct v4l2_subdev *sd,
927 struct v4l2_subdev_pad_config *cfg,
928 struct v4l2_subdev_mbus_code_enum *code)
933 switch (code->index) {
935 code->code = MEDIA_BUS_FMT_RGB888_1X24;
938 code->code = MEDIA_BUS_FMT_YUYV8_1X16;
941 code->code = MEDIA_BUS_FMT_UYVY8_1X16;
949 static void adv7511_fill_format(struct adv7511_state *state,
950 struct v4l2_mbus_framefmt *format)
952 memset(format, 0, sizeof(*format));
954 format->width = state->dv_timings.bt.width;
955 format->height = state->dv_timings.bt.height;
956 format->field = V4L2_FIELD_NONE;
959 static int adv7511_get_fmt(struct v4l2_subdev *sd,
960 struct v4l2_subdev_pad_config *cfg,
961 struct v4l2_subdev_format *format)
963 struct adv7511_state *state = get_adv7511_state(sd);
965 if (format->pad != 0)
968 adv7511_fill_format(state, &format->format);
970 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
971 struct v4l2_mbus_framefmt *fmt;
973 fmt = v4l2_subdev_get_try_format(sd, cfg, format->pad);
974 format->format.code = fmt->code;
975 format->format.colorspace = fmt->colorspace;
976 format->format.ycbcr_enc = fmt->ycbcr_enc;
977 format->format.quantization = fmt->quantization;
978 format->format.xfer_func = fmt->xfer_func;
980 format->format.code = state->fmt_code;
981 format->format.colorspace = state->colorspace;
982 format->format.ycbcr_enc = state->ycbcr_enc;
983 format->format.quantization = state->quantization;
984 format->format.xfer_func = state->xfer_func;
990 static int adv7511_set_fmt(struct v4l2_subdev *sd,
991 struct v4l2_subdev_pad_config *cfg,
992 struct v4l2_subdev_format *format)
994 struct adv7511_state *state = get_adv7511_state(sd);
996 * Bitfield namings come the CEA-861-F standard, table 8 "Auxiliary
997 * Video Information (AVI) InfoFrame Format"
1000 * ec = Extended Colorimetry
1002 * q = RGB Quantization Range
1003 * yq = YCC Quantization Range
1005 u8 c = HDMI_COLORIMETRY_NONE;
1006 u8 ec = HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
1007 u8 y = HDMI_COLORSPACE_RGB;
1008 u8 q = HDMI_QUANTIZATION_RANGE_DEFAULT;
1009 u8 yq = HDMI_YCC_QUANTIZATION_RANGE_LIMITED;
1011 if (format->pad != 0)
1013 switch (format->format.code) {
1014 case MEDIA_BUS_FMT_UYVY8_1X16:
1015 case MEDIA_BUS_FMT_YUYV8_1X16:
1016 case MEDIA_BUS_FMT_RGB888_1X24:
1022 adv7511_fill_format(state, &format->format);
1023 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1024 struct v4l2_mbus_framefmt *fmt;
1026 fmt = v4l2_subdev_get_try_format(sd, cfg, format->pad);
1027 fmt->code = format->format.code;
1028 fmt->colorspace = format->format.colorspace;
1029 fmt->ycbcr_enc = format->format.ycbcr_enc;
1030 fmt->quantization = format->format.quantization;
1031 fmt->xfer_func = format->format.xfer_func;
1035 switch (format->format.code) {
1036 case MEDIA_BUS_FMT_UYVY8_1X16:
1037 adv7511_wr_and_or(sd, 0x15, 0xf0, 0x01);
1038 adv7511_wr_and_or(sd, 0x16, 0x03, 0xb8);
1039 y = HDMI_COLORSPACE_YUV422;
1041 case MEDIA_BUS_FMT_YUYV8_1X16:
1042 adv7511_wr_and_or(sd, 0x15, 0xf0, 0x01);
1043 adv7511_wr_and_or(sd, 0x16, 0x03, 0xbc);
1044 y = HDMI_COLORSPACE_YUV422;
1046 case MEDIA_BUS_FMT_RGB888_1X24:
1048 adv7511_wr_and_or(sd, 0x15, 0xf0, 0x00);
1049 adv7511_wr_and_or(sd, 0x16, 0x03, 0x00);
1052 state->fmt_code = format->format.code;
1053 state->colorspace = format->format.colorspace;
1054 state->ycbcr_enc = format->format.ycbcr_enc;
1055 state->quantization = format->format.quantization;
1056 state->xfer_func = format->format.xfer_func;
1058 switch (format->format.colorspace) {
1059 case V4L2_COLORSPACE_ADOBERGB:
1060 c = HDMI_COLORIMETRY_EXTENDED;
1061 ec = y ? HDMI_EXTENDED_COLORIMETRY_ADOBE_YCC_601 :
1062 HDMI_EXTENDED_COLORIMETRY_ADOBE_RGB;
1064 case V4L2_COLORSPACE_SMPTE170M:
1065 c = y ? HDMI_COLORIMETRY_ITU_601 : HDMI_COLORIMETRY_NONE;
1066 if (y && format->format.ycbcr_enc == V4L2_YCBCR_ENC_XV601) {
1067 c = HDMI_COLORIMETRY_EXTENDED;
1068 ec = HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
1071 case V4L2_COLORSPACE_REC709:
1072 c = y ? HDMI_COLORIMETRY_ITU_709 : HDMI_COLORIMETRY_NONE;
1073 if (y && format->format.ycbcr_enc == V4L2_YCBCR_ENC_XV709) {
1074 c = HDMI_COLORIMETRY_EXTENDED;
1075 ec = HDMI_EXTENDED_COLORIMETRY_XV_YCC_709;
1078 case V4L2_COLORSPACE_SRGB:
1079 c = y ? HDMI_COLORIMETRY_EXTENDED : HDMI_COLORIMETRY_NONE;
1080 ec = y ? HDMI_EXTENDED_COLORIMETRY_S_YCC_601 :
1081 HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
1083 case V4L2_COLORSPACE_BT2020:
1084 c = HDMI_COLORIMETRY_EXTENDED;
1085 if (y && format->format.ycbcr_enc == V4L2_YCBCR_ENC_BT2020_CONST_LUM)
1086 ec = 5; /* Not yet available in hdmi.h */
1088 ec = 6; /* Not yet available in hdmi.h */
1095 * CEA-861-F says that for RGB formats the YCC range must match the
1096 * RGB range, although sources should ignore the YCC range.
1098 * The RGB quantization range shouldn't be non-zero if the EDID doesn't
1099 * have the Q bit set in the Video Capabilities Data Block, however this
1100 * isn't checked at the moment. The assumption is that the application
1101 * knows the EDID and can detect this.
1103 * The same is true for the YCC quantization range: non-standard YCC
1104 * quantization ranges should only be sent if the EDID has the YQ bit
1105 * set in the Video Capabilities Data Block.
1107 switch (format->format.quantization) {
1108 case V4L2_QUANTIZATION_FULL_RANGE:
1109 q = y ? HDMI_QUANTIZATION_RANGE_DEFAULT :
1110 HDMI_QUANTIZATION_RANGE_FULL;
1111 yq = q ? q - 1 : HDMI_YCC_QUANTIZATION_RANGE_FULL;
1113 case V4L2_QUANTIZATION_LIM_RANGE:
1114 q = y ? HDMI_QUANTIZATION_RANGE_DEFAULT :
1115 HDMI_QUANTIZATION_RANGE_LIMITED;
1116 yq = q ? q - 1 : HDMI_YCC_QUANTIZATION_RANGE_LIMITED;
1120 adv7511_wr_and_or(sd, 0x4a, 0xbf, 0);
1121 adv7511_wr_and_or(sd, 0x55, 0x9f, y << 5);
1122 adv7511_wr_and_or(sd, 0x56, 0x3f, c << 6);
1123 adv7511_wr_and_or(sd, 0x57, 0x83, (ec << 4) | (q << 2));
1124 adv7511_wr_and_or(sd, 0x59, 0x0f, yq << 4);
1125 adv7511_wr_and_or(sd, 0x4a, 0xff, 1);
1130 static const struct v4l2_subdev_pad_ops adv7511_pad_ops = {
1131 .get_edid = adv7511_get_edid,
1132 .enum_mbus_code = adv7511_enum_mbus_code,
1133 .get_fmt = adv7511_get_fmt,
1134 .set_fmt = adv7511_set_fmt,
1135 .enum_dv_timings = adv7511_enum_dv_timings,
1136 .dv_timings_cap = adv7511_dv_timings_cap,
1139 /* --------------------- SUBDEV OPS --------------------------------------- */
1141 static const struct v4l2_subdev_ops adv7511_ops = {
1142 .core = &adv7511_core_ops,
1143 .pad = &adv7511_pad_ops,
1144 .video = &adv7511_video_ops,
1145 .audio = &adv7511_audio_ops,
1148 /* ----------------------------------------------------------------------- */
1149 static void adv7511_dbg_dump_edid(int lvl, int debug, struct v4l2_subdev *sd, int segment, u8 *buf)
1153 v4l2_dbg(lvl, debug, sd, "edid segment %d\n", segment);
1154 for (i = 0; i < 256; i += 16) {
1158 v4l2_dbg(lvl, debug, sd, "\n");
1159 for (j = i; j < i + 16; j++) {
1160 sprintf(bp, "0x%02x, ", buf[j]);
1164 v4l2_dbg(lvl, debug, sd, "%s\n", b);
1169 static void adv7511_notify_no_edid(struct v4l2_subdev *sd)
1171 struct adv7511_state *state = get_adv7511_state(sd);
1172 struct adv7511_edid_detect ed;
1174 /* We failed to read the EDID, so send an event for this. */
1176 ed.segment = adv7511_rd(sd, 0xc4);
1177 v4l2_subdev_notify(sd, ADV7511_EDID_DETECT, (void *)&ed);
1178 v4l2_ctrl_s_ctrl(state->have_edid0_ctrl, 0x0);
1181 static void adv7511_edid_handler(struct work_struct *work)
1183 struct delayed_work *dwork = to_delayed_work(work);
1184 struct adv7511_state *state = container_of(dwork, struct adv7511_state, edid_handler);
1185 struct v4l2_subdev *sd = &state->sd;
1187 v4l2_dbg(1, debug, sd, "%s:\n", __func__);
1189 if (adv7511_check_edid_status(sd)) {
1190 /* Return if we received the EDID. */
1194 if (adv7511_have_hotplug(sd)) {
1195 /* We must retry reading the EDID several times, it is possible
1196 * that initially the EDID couldn't be read due to i2c errors
1197 * (DVI connectors are particularly prone to this problem). */
1198 if (state->edid.read_retries) {
1199 state->edid.read_retries--;
1200 v4l2_dbg(1, debug, sd, "%s: edid read failed\n", __func__);
1201 state->have_monitor = false;
1202 adv7511_s_power(sd, false);
1203 adv7511_s_power(sd, true);
1204 queue_delayed_work(state->work_queue, &state->edid_handler, EDID_DELAY);
1209 /* We failed to read the EDID, so send an event for this. */
1210 adv7511_notify_no_edid(sd);
1211 v4l2_dbg(1, debug, sd, "%s: no edid found\n", __func__);
1214 static void adv7511_audio_setup(struct v4l2_subdev *sd)
1216 v4l2_dbg(1, debug, sd, "%s\n", __func__);
1218 adv7511_s_i2s_clock_freq(sd, 48000);
1219 adv7511_s_clock_freq(sd, 48000);
1220 adv7511_s_routing(sd, 0, 0, 0);
1223 /* Configure hdmi transmitter. */
1224 static void adv7511_setup(struct v4l2_subdev *sd)
1226 struct adv7511_state *state = get_adv7511_state(sd);
1227 v4l2_dbg(1, debug, sd, "%s\n", __func__);
1229 /* Input format: RGB 4:4:4 */
1230 adv7511_wr_and_or(sd, 0x15, 0xf0, 0x0);
1231 /* Output format: RGB 4:4:4 */
1232 adv7511_wr_and_or(sd, 0x16, 0x7f, 0x0);
1233 /* 1st order interpolation 4:2:2 -> 4:4:4 up conversion, Aspect ratio: 16:9 */
1234 adv7511_wr_and_or(sd, 0x17, 0xf9, 0x06);
1235 /* Disable pixel repetition */
1236 adv7511_wr_and_or(sd, 0x3b, 0x9f, 0x0);
1238 adv7511_wr_and_or(sd, 0x18, 0x7f, 0x0);
1239 /* Output format: RGB 4:4:4, Active Format Information is valid,
1241 adv7511_wr_and_or(sd, 0x55, 0x9c, 0x12);
1242 /* AVI Info frame packet enable, Audio Info frame disable */
1243 adv7511_wr_and_or(sd, 0x44, 0xe7, 0x10);
1244 /* Colorimetry, Active format aspect ratio: same as picure. */
1245 adv7511_wr(sd, 0x56, 0xa8);
1247 adv7511_wr_and_or(sd, 0xaf, 0xed, 0x0);
1249 /* Positive clk edge capture for input video clock */
1250 adv7511_wr_and_or(sd, 0xba, 0x1f, 0x60);
1252 adv7511_audio_setup(sd);
1254 v4l2_ctrl_handler_setup(&state->hdl);
1257 static void adv7511_notify_monitor_detect(struct v4l2_subdev *sd)
1259 struct adv7511_monitor_detect mdt;
1260 struct adv7511_state *state = get_adv7511_state(sd);
1262 mdt.present = state->have_monitor;
1263 v4l2_subdev_notify(sd, ADV7511_MONITOR_DETECT, (void *)&mdt);
1266 static void adv7511_check_monitor_present_status(struct v4l2_subdev *sd)
1268 struct adv7511_state *state = get_adv7511_state(sd);
1269 /* read hotplug and rx-sense state */
1270 u8 status = adv7511_rd(sd, 0x42);
1272 v4l2_dbg(1, debug, sd, "%s: status: 0x%x%s%s\n",
1275 status & MASK_ADV7511_HPD_DETECT ? ", hotplug" : "",
1276 status & MASK_ADV7511_MSEN_DETECT ? ", rx-sense" : "");
1278 /* update read only ctrls */
1279 v4l2_ctrl_s_ctrl(state->hotplug_ctrl, adv7511_have_hotplug(sd) ? 0x1 : 0x0);
1280 v4l2_ctrl_s_ctrl(state->rx_sense_ctrl, adv7511_have_rx_sense(sd) ? 0x1 : 0x0);
1282 if ((status & MASK_ADV7511_HPD_DETECT) && ((status & MASK_ADV7511_MSEN_DETECT) || state->edid.segments)) {
1283 v4l2_dbg(1, debug, sd, "%s: hotplug and (rx-sense or edid)\n", __func__);
1284 if (!state->have_monitor) {
1285 v4l2_dbg(1, debug, sd, "%s: monitor detected\n", __func__);
1286 state->have_monitor = true;
1287 adv7511_set_isr(sd, true);
1288 if (!adv7511_s_power(sd, true)) {
1289 v4l2_dbg(1, debug, sd, "%s: monitor detected, powerup failed\n", __func__);
1293 adv7511_notify_monitor_detect(sd);
1294 state->edid.read_retries = EDID_MAX_RETRIES;
1295 queue_delayed_work(state->work_queue, &state->edid_handler, EDID_DELAY);
1297 } else if (status & MASK_ADV7511_HPD_DETECT) {
1298 v4l2_dbg(1, debug, sd, "%s: hotplug detected\n", __func__);
1299 state->edid.read_retries = EDID_MAX_RETRIES;
1300 queue_delayed_work(state->work_queue, &state->edid_handler, EDID_DELAY);
1301 } else if (!(status & MASK_ADV7511_HPD_DETECT)) {
1302 v4l2_dbg(1, debug, sd, "%s: hotplug not detected\n", __func__);
1303 if (state->have_monitor) {
1304 v4l2_dbg(1, debug, sd, "%s: monitor not detected\n", __func__);
1305 state->have_monitor = false;
1306 adv7511_notify_monitor_detect(sd);
1308 adv7511_s_power(sd, false);
1309 memset(&state->edid, 0, sizeof(struct adv7511_state_edid));
1310 adv7511_notify_no_edid(sd);
1314 static bool edid_block_verify_crc(u8 *edid_block)
1319 for (i = 0; i < 128; i++)
1320 sum += edid_block[i];
1324 static bool edid_verify_crc(struct v4l2_subdev *sd, u32 segment)
1326 struct adv7511_state *state = get_adv7511_state(sd);
1327 u32 blocks = state->edid.blocks;
1328 u8 *data = state->edid.data;
1330 if (!edid_block_verify_crc(&data[segment * 256]))
1332 if ((segment + 1) * 2 <= blocks)
1333 return edid_block_verify_crc(&data[segment * 256 + 128]);
1337 static bool edid_verify_header(struct v4l2_subdev *sd, u32 segment)
1339 static const u8 hdmi_header[] = {
1340 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00
1342 struct adv7511_state *state = get_adv7511_state(sd);
1343 u8 *data = state->edid.data;
1347 return !memcmp(data, hdmi_header, sizeof(hdmi_header));
1350 static bool adv7511_check_edid_status(struct v4l2_subdev *sd)
1352 struct adv7511_state *state = get_adv7511_state(sd);
1353 u8 edidRdy = adv7511_rd(sd, 0xc5);
1355 v4l2_dbg(1, debug, sd, "%s: edid ready (retries: %d)\n",
1356 __func__, EDID_MAX_RETRIES - state->edid.read_retries);
1358 if (state->edid.complete)
1361 if (edidRdy & MASK_ADV7511_EDID_RDY) {
1362 int segment = adv7511_rd(sd, 0xc4);
1363 struct adv7511_edid_detect ed;
1365 if (segment >= EDID_MAX_SEGM) {
1366 v4l2_err(sd, "edid segment number too big\n");
1369 v4l2_dbg(1, debug, sd, "%s: got segment %d\n", __func__, segment);
1370 adv7511_edid_rd(sd, 256, &state->edid.data[segment * 256]);
1371 adv7511_dbg_dump_edid(2, debug, sd, segment, &state->edid.data[segment * 256]);
1373 state->edid.blocks = state->edid.data[0x7e] + 1;
1374 v4l2_dbg(1, debug, sd, "%s: %d blocks in total\n", __func__, state->edid.blocks);
1376 if (!edid_verify_crc(sd, segment) ||
1377 !edid_verify_header(sd, segment)) {
1378 /* edid crc error, force reread of edid segment */
1379 v4l2_err(sd, "%s: edid crc or header error\n", __func__);
1380 state->have_monitor = false;
1381 adv7511_s_power(sd, false);
1382 adv7511_s_power(sd, true);
1385 /* one more segment read ok */
1386 state->edid.segments = segment + 1;
1387 v4l2_ctrl_s_ctrl(state->have_edid0_ctrl, 0x1);
1388 if (((state->edid.data[0x7e] >> 1) + 1) > state->edid.segments) {
1389 /* Request next EDID segment */
1390 v4l2_dbg(1, debug, sd, "%s: request segment %d\n", __func__, state->edid.segments);
1391 adv7511_wr(sd, 0xc9, 0xf);
1392 adv7511_wr(sd, 0xc4, state->edid.segments);
1393 state->edid.read_retries = EDID_MAX_RETRIES;
1394 queue_delayed_work(state->work_queue, &state->edid_handler, EDID_DELAY);
1398 v4l2_dbg(1, debug, sd, "%s: edid complete with %d segment(s)\n", __func__, state->edid.segments);
1399 state->edid.complete = true;
1401 /* report when we have all segments
1402 but report only for segment 0
1406 state->edid_detect_counter++;
1407 v4l2_subdev_notify(sd, ADV7511_EDID_DETECT, (void *)&ed);
1414 /* ----------------------------------------------------------------------- */
1416 static void adv7511_init_setup(struct v4l2_subdev *sd)
1418 struct adv7511_state *state = get_adv7511_state(sd);
1419 struct adv7511_state_edid *edid = &state->edid;
1421 v4l2_dbg(1, debug, sd, "%s\n", __func__);
1423 /* clear all interrupts */
1424 adv7511_wr(sd, 0x96, 0xff);
1426 * Stop HPD from resetting a lot of registers.
1427 * It might leave the chip in a partly un-initialized state,
1428 * in particular with regards to hotplug bounces.
1430 adv7511_wr_and_or(sd, 0xd6, 0x3f, 0xc0);
1431 memset(edid, 0, sizeof(struct adv7511_state_edid));
1432 state->have_monitor = false;
1433 adv7511_set_isr(sd, false);
1434 adv7511_s_stream(sd, false);
1435 adv7511_s_audio_stream(sd, false);
1438 static int adv7511_probe(struct i2c_client *client, const struct i2c_device_id *id)
1440 struct adv7511_state *state;
1441 struct adv7511_platform_data *pdata = client->dev.platform_data;
1442 struct v4l2_ctrl_handler *hdl;
1443 struct v4l2_subdev *sd;
1447 /* Check if the adapter supports the needed features */
1448 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1451 state = devm_kzalloc(&client->dev, sizeof(struct adv7511_state), GFP_KERNEL);
1457 v4l_err(client, "No platform data!\n");
1460 memcpy(&state->pdata, pdata, sizeof(state->pdata));
1461 state->fmt_code = MEDIA_BUS_FMT_RGB888_1X24;
1462 state->colorspace = V4L2_COLORSPACE_SRGB;
1466 v4l2_dbg(1, debug, sd, "detecting adv7511 client on address 0x%x\n",
1469 v4l2_i2c_subdev_init(sd, client, &adv7511_ops);
1472 v4l2_ctrl_handler_init(hdl, 10);
1473 /* add in ascending ID order */
1474 state->hdmi_mode_ctrl = v4l2_ctrl_new_std_menu(hdl, &adv7511_ctrl_ops,
1475 V4L2_CID_DV_TX_MODE, V4L2_DV_TX_MODE_HDMI,
1476 0, V4L2_DV_TX_MODE_DVI_D);
1477 state->hotplug_ctrl = v4l2_ctrl_new_std(hdl, NULL,
1478 V4L2_CID_DV_TX_HOTPLUG, 0, 1, 0, 0);
1479 state->rx_sense_ctrl = v4l2_ctrl_new_std(hdl, NULL,
1480 V4L2_CID_DV_TX_RXSENSE, 0, 1, 0, 0);
1481 state->have_edid0_ctrl = v4l2_ctrl_new_std(hdl, NULL,
1482 V4L2_CID_DV_TX_EDID_PRESENT, 0, 1, 0, 0);
1483 state->rgb_quantization_range_ctrl =
1484 v4l2_ctrl_new_std_menu(hdl, &adv7511_ctrl_ops,
1485 V4L2_CID_DV_TX_RGB_RANGE, V4L2_DV_RGB_RANGE_FULL,
1486 0, V4L2_DV_RGB_RANGE_AUTO);
1487 sd->ctrl_handler = hdl;
1492 state->hdmi_mode_ctrl->is_private = true;
1493 state->hotplug_ctrl->is_private = true;
1494 state->rx_sense_ctrl->is_private = true;
1495 state->have_edid0_ctrl->is_private = true;
1496 state->rgb_quantization_range_ctrl->is_private = true;
1498 state->pad.flags = MEDIA_PAD_FL_SINK;
1499 err = media_entity_init(&sd->entity, 1, &state->pad, 0);
1503 /* EDID and CEC i2c addr */
1504 state->i2c_edid_addr = state->pdata.i2c_edid << 1;
1505 state->i2c_cec_addr = state->pdata.i2c_cec << 1;
1506 state->i2c_pktmem_addr = state->pdata.i2c_pktmem << 1;
1508 state->chip_revision = adv7511_rd(sd, 0x0);
1509 chip_id[0] = adv7511_rd(sd, 0xf5);
1510 chip_id[1] = adv7511_rd(sd, 0xf6);
1511 if (chip_id[0] != 0x75 || chip_id[1] != 0x11) {
1512 v4l2_err(sd, "chip_id != 0x7511, read 0x%02x%02x\n", chip_id[0], chip_id[1]);
1517 state->i2c_edid = i2c_new_dummy(client->adapter, state->i2c_edid_addr >> 1);
1518 if (state->i2c_edid == NULL) {
1519 v4l2_err(sd, "failed to register edid i2c client\n");
1524 state->i2c_pktmem = i2c_new_dummy(client->adapter, state->i2c_pktmem_addr >> 1);
1525 if (state->i2c_pktmem == NULL) {
1526 v4l2_err(sd, "failed to register pktmem i2c client\n");
1528 goto err_unreg_edid;
1531 adv7511_wr(sd, 0xe2, 0x01); /* power down cec section */
1532 state->work_queue = create_singlethread_workqueue(sd->name);
1533 if (state->work_queue == NULL) {
1534 v4l2_err(sd, "could not create workqueue\n");
1536 goto err_unreg_pktmem;
1539 INIT_DELAYED_WORK(&state->edid_handler, adv7511_edid_handler);
1541 adv7511_init_setup(sd);
1542 adv7511_set_isr(sd, true);
1543 adv7511_check_monitor_present_status(sd);
1545 v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
1546 client->addr << 1, client->adapter->name);
1550 i2c_unregister_device(state->i2c_pktmem);
1552 i2c_unregister_device(state->i2c_edid);
1554 media_entity_cleanup(&sd->entity);
1556 v4l2_ctrl_handler_free(&state->hdl);
1560 /* ----------------------------------------------------------------------- */
1562 static int adv7511_remove(struct i2c_client *client)
1564 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1565 struct adv7511_state *state = get_adv7511_state(sd);
1567 state->chip_revision = -1;
1569 v4l2_dbg(1, debug, sd, "%s removed @ 0x%x (%s)\n", client->name,
1570 client->addr << 1, client->adapter->name);
1572 adv7511_init_setup(sd);
1573 cancel_delayed_work_sync(&state->edid_handler);
1574 i2c_unregister_device(state->i2c_edid);
1575 i2c_unregister_device(state->i2c_pktmem);
1576 destroy_workqueue(state->work_queue);
1577 v4l2_device_unregister_subdev(sd);
1578 media_entity_cleanup(&sd->entity);
1579 v4l2_ctrl_handler_free(sd->ctrl_handler);
1583 /* ----------------------------------------------------------------------- */
1585 static struct i2c_device_id adv7511_id[] = {
1589 MODULE_DEVICE_TABLE(i2c, adv7511_id);
1591 static struct i2c_driver adv7511_driver = {
1595 .probe = adv7511_probe,
1596 .remove = adv7511_remove,
1597 .id_table = adv7511_id,
1600 module_i2c_driver(adv7511_driver);