Linux Audio

Check our new training course

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