Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/evalenti/linux...
[linux/fpc-iii.git] / drivers / media / i2c / adv7511.c
blobbd822f032b08c870dddebaa1b59743859d16e8f5
1 /*
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
17 * SOFTWARE.
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>
37 static int debug;
38 module_param(debug, int, 0644);
39 MODULE_PARM_DESC(debug, "debug level (0-2)");
41 MODULE_DESCRIPTION("Analog Devices ADV7511 HDMI Transmitter Device Driver");
42 MODULE_AUTHOR("Hans Verkuil");
43 MODULE_LICENSE("GPL v2");
45 #define MASK_ADV7511_EDID_RDY_INT 0x04
46 #define MASK_ADV7511_MSEN_INT 0x40
47 #define MASK_ADV7511_HPD_INT 0x80
49 #define MASK_ADV7511_HPD_DETECT 0x40
50 #define MASK_ADV7511_MSEN_DETECT 0x20
51 #define MASK_ADV7511_EDID_RDY 0x10
53 #define EDID_MAX_RETRIES (8)
54 #define EDID_DELAY 250
55 #define EDID_MAX_SEGM 8
57 #define ADV7511_MAX_WIDTH 1920
58 #define ADV7511_MAX_HEIGHT 1200
59 #define ADV7511_MIN_PIXELCLOCK 20000000
60 #define ADV7511_MAX_PIXELCLOCK 225000000
63 **********************************************************************
65 * Arrays with configuration parameters for the ADV7511
67 **********************************************************************
70 struct i2c_reg_value {
71 unsigned char reg;
72 unsigned char value;
75 struct 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;
86 struct 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 */
123 static void adv7511_check_monitor_present_status(struct v4l2_subdev *sd);
124 static bool adv7511_check_edid_status(struct v4l2_subdev *sd);
125 static void adv7511_setup(struct v4l2_subdev *sd);
126 static int adv7511_s_i2s_clock_freq(struct v4l2_subdev *sd, u32 freq);
127 static int adv7511_s_clock_freq(struct v4l2_subdev *sd, u32 freq);
130 static 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)
142 static inline struct adv7511_state *get_adv7511_state(struct v4l2_subdev *sd)
144 return container_of(sd, struct adv7511_state, sd);
147 static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
149 return &container_of(ctrl->handler, struct adv7511_state, hdl)->sd;
152 /* ------------------------ I2C ----------------------------------------------- */
154 static s32 adv_smbus_read_byte_data_check(struct i2c_client *client,
155 u8 command, bool check)
157 union i2c_smbus_data data;
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;
169 static s32 adv_smbus_read_byte_data(struct i2c_client *client, u8 command)
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;
180 v4l_err(client, "read failed\n");
181 return -1;
184 static int adv7511_rd(struct v4l2_subdev *sd, u8 reg)
186 struct i2c_client *client = v4l2_get_subdevdata(sd);
188 return adv_smbus_read_byte_data(client, reg);
191 static int adv7511_wr(struct v4l2_subdev *sd, u8 reg, u8 val)
193 struct i2c_client *client = v4l2_get_subdevdata(sd);
194 int ret;
195 int i;
197 for (i = 0; i < 3; i++) {
198 ret = i2c_smbus_write_byte_data(client, reg, val);
199 if (ret == 0)
200 return 0;
202 v4l2_err(sd, "%s: i2c write error\n", __func__);
203 return ret;
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). */
208 static inline void adv7511_wr_and_or(struct v4l2_subdev *sd, u8 reg, u8 clr_mask, u8 val_mask)
210 adv7511_wr(sd, reg, (adv7511_rd(sd, reg) & clr_mask) | val_mask);
213 static int adv_smbus_read_i2c_block_data(struct i2c_client *client,
214 u8 command, unsigned length, u8 *values)
216 union i2c_smbus_data data;
217 int ret;
219 if (length > I2C_SMBUS_BLOCK_MAX)
220 length = I2C_SMBUS_BLOCK_MAX;
221 data.block[0] = length;
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;
230 static inline void adv7511_edid_rd(struct v4l2_subdev *sd, u16 len, u8 *buf)
232 struct adv7511_state *state = get_adv7511_state(sd);
233 int i;
234 int err = 0;
236 v4l2_dbg(1, debug, sd, "%s:\n", __func__);
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__);
245 static int adv7511_pktmem_rd(struct v4l2_subdev *sd, u8 reg)
247 struct adv7511_state *state = get_adv7511_state(sd);
249 return adv_smbus_read_byte_data(state->i2c_pktmem, reg);
252 static int adv7511_pktmem_wr(struct v4l2_subdev *sd, u8 reg, u8 val)
254 struct adv7511_state *state = get_adv7511_state(sd);
255 int ret;
256 int i;
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;
263 v4l2_err(sd, "%s: i2c write error\n", __func__);
264 return ret;
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). */
269 static inline void adv7511_pktmem_wr_and_or(struct v4l2_subdev *sd, u8 reg, u8 clr_mask, u8 val_mask)
271 adv7511_pktmem_wr(sd, reg, (adv7511_pktmem_rd(sd, reg) & clr_mask) | val_mask);
274 static inline bool adv7511_have_hotplug(struct v4l2_subdev *sd)
276 return adv7511_rd(sd, 0x42) & MASK_ADV7511_HPD_DETECT;
279 static inline bool adv7511_have_rx_sense(struct v4l2_subdev *sd)
281 return adv7511_rd(sd, 0x42) & MASK_ADV7511_MSEN_DETECT;
284 static void adv7511_csc_conversion_mode(struct v4l2_subdev *sd, u8 mode)
286 adv7511_wr_and_or(sd, 0x18, 0x9f, (mode & 0x3)<<5);
289 static 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)
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);
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);
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);
325 static void adv7511_csc_rgb_full2limit(struct v4l2_subdev *sd, bool enable)
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);
346 static void adv7511_set_IT_content_AVI_InfoFrame(struct v4l2_subdev *sd)
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);
358 static int adv7511_set_rgb_quantization_mode(struct v4l2_subdev *sd, struct v4l2_ctrl *ctrl)
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);
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);
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;
386 return 0;
389 /* ------------------------------ CTRL OPS ------------------------------ */
391 static int adv7511_s_ctrl(struct v4l2_ctrl *ctrl)
393 struct v4l2_subdev *sd = to_sd(ctrl);
394 struct adv7511_state *state = get_adv7511_state(sd);
396 v4l2_dbg(1, debug, sd, "%s: ctrl id: %d, ctrl->val %d\n", __func__, ctrl->id, ctrl->val);
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;
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;
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;
416 return -EINVAL;
419 static const struct v4l2_ctrl_ops adv7511_ctrl_ops = {
420 .s_ctrl = adv7511_s_ctrl,
423 /* ---------------------------- CORE OPS ------------------------------------------- */
425 #ifdef CONFIG_VIDEO_ADV_DEBUG
426 static void adv7511_inv_register(struct v4l2_subdev *sd)
428 v4l2_info(sd, "0x000-0x0ff: Main Map\n");
431 static int adv7511_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
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;
443 return 0;
446 static int adv7511_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
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;
457 return 0;
459 #endif
461 struct adv7511_cfg_read_infoframe {
462 const char *desc;
463 u8 present_reg;
464 u8 present_mask;
465 u8 header[3];
466 u16 payload_addr;
469 static u8 hdmi_infoframe_checksum(u8 *ptr, size_t size)
471 u8 csum = 0;
472 size_t i;
474 /* compute checksum */
475 for (i = 0; i < size; i++)
476 csum += ptr[i];
478 return 256 - csum;
481 static void log_infoframe(struct v4l2_subdev *sd, const struct adv7511_cfg_read_infoframe *cri)
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;
490 if (!(adv7511_rd(sd, cri->present_reg) & cri->present_mask)) {
491 v4l2_info(sd, "%s infoframe not transmitted\n", cri->desc);
492 return;
495 memcpy(buffer, cri->header, sizeof(cri->header));
497 len = buffer[2];
499 if (len + 4 > sizeof(buffer)) {
500 v4l2_err(sd, "%s: invalid %s infoframe length %d\n", __func__, cri->desc, len);
501 return;
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);
511 buffer[3] = 0;
512 buffer[3] = hdmi_infoframe_checksum(buffer, len + 4);
514 if (hdmi_infoframe_unpack(&frame, buffer) < 0) {
515 v4l2_err(sd, "%s: unpack of %s infoframe failed\n", __func__, cri->desc);
516 return;
519 hdmi_infoframe_log(KERN_INFO, dev, &frame);
522 static void adv7511_log_infoframes(struct v4l2_subdev *sd)
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 },
529 int i;
531 for (i = 0; i < ARRAY_SIZE(cri); i++)
532 log_infoframe(sd, &cri[i]);
535 static int adv7511_log_status(struct v4l2_subdev *sd)
537 struct adv7511_state *state = get_adv7511_state(sd);
538 struct adv7511_state_edid *edid = &state->edid;
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"
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"
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;
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);
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;
613 /* Power up/down adv7511 */
614 static int adv7511_s_power(struct v4l2_subdev *sd, int on)
616 struct adv7511_state *state = get_adv7511_state(sd);
617 const int retries = 20;
618 int i;
620 v4l2_dbg(1, debug, sd, "%s: power %s\n", __func__, on ? "on" : "off");
622 state->power_on = on;
624 if (!on) {
625 /* Power down */
626 adv7511_wr_and_or(sd, 0x41, 0xbf, 0x40);
627 return true;
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);
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;
645 if (i > 1)
646 v4l2_dbg(1, debug, sd, "%s: needed %d retries to powerup the adv7511\n", __func__, i);
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);
658 adv7511_wr(sd, 0x43, state->i2c_edid_addr);
659 adv7511_wr(sd, 0x45, state->i2c_pktmem_addr);
661 /* Set number of attempts to read the EDID */
662 adv7511_wr(sd, 0xc9, 0xf);
663 return true;
666 /* Enable interrupts */
667 static void adv7511_set_isr(struct v4l2_subdev *sd, bool enable)
669 u8 irqs = MASK_ADV7511_HPD_INT | MASK_ADV7511_MSEN_INT;
670 u8 irqs_rd;
671 int retries = 100;
673 v4l2_dbg(2, debug, sd, "%s: %s\n", __func__, enable ? "enable" : "disable");
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;
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.
687 * Note that the i2c write does not report an error, but the readback
688 * clearly shows the wrong value.
690 do {
691 adv7511_wr(sd, 0x94, irqs);
692 irqs_rd = adv7511_rd(sd, 0x94);
693 } while (retries-- && irqs_rd != irqs);
695 if (irqs_rd == irqs)
696 return;
697 v4l2_err(sd, "Could not set interrupts: hw failure?\n");
700 /* Interrupt handler */
701 static int adv7511_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
703 u8 irq_status;
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);
711 v4l2_dbg(1, debug, sd, "%s: irq 0x%x\n", __func__, irq_status);
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);
718 /* enable interrupts */
719 adv7511_set_isr(sd, true);
721 if (handled)
722 *handled = true;
723 return 0;
726 static 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,
736 /* ------------------------------ VIDEO OPS ------------------------------ */
738 /* Enable/disable adv7511 output */
739 static int adv7511_s_stream(struct v4l2_subdev *sd, int enable)
741 struct adv7511_state *state = get_adv7511_state(sd);
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;
751 return 0;
754 static int adv7511_s_dv_timings(struct v4l2_subdev *sd,
755 struct v4l2_dv_timings *timings)
757 struct adv7511_state *state = get_adv7511_state(sd);
759 v4l2_dbg(1, debug, sd, "%s:\n", __func__);
761 /* quick sanity check */
762 if (!v4l2_valid_dv_timings(timings, &adv7511_timings_cap, NULL, NULL))
763 return -EINVAL;
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);
769 timings->bt.flags &= ~V4L2_DV_FL_REDUCED_FPS;
771 /* save timings */
772 state->dv_timings = *timings;
774 /* update quantization range based on new dv_timings */
775 adv7511_set_rgb_quantization_mode(sd, state->rgb_quantization_range_ctrl);
777 /* update AVI infoframe */
778 adv7511_set_IT_content_AVI_InfoFrame(sd);
780 return 0;
783 static int adv7511_g_dv_timings(struct v4l2_subdev *sd,
784 struct v4l2_dv_timings *timings)
786 struct adv7511_state *state = get_adv7511_state(sd);
788 v4l2_dbg(1, debug, sd, "%s:\n", __func__);
790 if (!timings)
791 return -EINVAL;
793 *timings = state->dv_timings;
795 return 0;
798 static int adv7511_enum_dv_timings(struct v4l2_subdev *sd,
799 struct v4l2_enum_dv_timings *timings)
801 if (timings->pad != 0)
802 return -EINVAL;
804 return v4l2_enum_dv_timings_cap(timings, &adv7511_timings_cap, NULL, NULL);
807 static int adv7511_dv_timings_cap(struct v4l2_subdev *sd,
808 struct v4l2_dv_timings_cap *cap)
810 if (cap->pad != 0)
811 return -EINVAL;
813 *cap = adv7511_timings_cap;
814 return 0;
817 static 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,
823 /* ------------------------------ AUDIO OPS ------------------------------ */
824 static int adv7511_s_audio_stream(struct v4l2_subdev *sd, int enable)
826 v4l2_dbg(1, debug, sd, "%s: %sable\n", __func__, (enable ? "en" : "dis"));
828 if (enable)
829 adv7511_wr_and_or(sd, 0x4b, 0x3f, 0x80);
830 else
831 adv7511_wr_and_or(sd, 0x4b, 0x3f, 0x40);
833 return 0;
836 static int adv7511_s_clock_freq(struct v4l2_subdev *sd, u32 freq)
838 u32 N;
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;
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);
857 return 0;
860 static int adv7511_s_i2s_clock_freq(struct v4l2_subdev *sd, u32 freq)
862 u32 i2s_sf;
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;
876 /* Set sampling frequency for I2S audio to 48 kHz */
877 adv7511_wr_and_or(sd, 0x15, 0xf, i2s_sf);
879 return 0;
882 static int adv7511_s_routing(struct v4l2_subdev *sd, u32 input, u32 output, u32 config)
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);
889 /* 16 bit audio word length */
890 adv7511_wr_and_or(sd, 0x14, 0xf0, 0x02);
892 return 0;
895 static 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,
902 /* ---------------------------- PAD OPS ------------------------------------- */
904 static int adv7511_get_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
906 struct adv7511_state *state = get_adv7511_state(sd);
908 memset(edid->reserved, 0, sizeof(edid->reserved));
910 if (edid->pad != 0)
911 return -EINVAL;
913 if (edid->start_block == 0 && edid->blocks == 0) {
914 edid->blocks = state->edid.segments * 2;
915 return 0;
918 if (state->edid.segments == 0)
919 return -ENODATA;
921 if (edid->start_block >= state->edid.segments * 2)
922 return -EINVAL;
924 if (edid->start_block + edid->blocks > state->edid.segments * 2)
925 edid->blocks = state->edid.segments * 2 - edid->start_block;
927 memcpy(edid->edid, &state->edid.data[edid->start_block * 128],
928 128 * edid->blocks);
930 return 0;
933 static int adv7511_enum_mbus_code(struct v4l2_subdev *sd,
934 struct v4l2_subdev_pad_config *cfg,
935 struct v4l2_subdev_mbus_code_enum *code)
937 if (code->pad != 0)
938 return -EINVAL;
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;
953 return 0;
956 static void adv7511_fill_format(struct adv7511_state *state,
957 struct v4l2_mbus_framefmt *format)
959 memset(format, 0, sizeof(*format));
961 format->width = state->dv_timings.bt.width;
962 format->height = state->dv_timings.bt.height;
963 format->field = V4L2_FIELD_NONE;
966 static int adv7511_get_fmt(struct v4l2_subdev *sd,
967 struct v4l2_subdev_pad_config *cfg,
968 struct v4l2_subdev_format *format)
970 struct adv7511_state *state = get_adv7511_state(sd);
972 if (format->pad != 0)
973 return -EINVAL;
975 adv7511_fill_format(state, &format->format);
977 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
978 struct v4l2_mbus_framefmt *fmt;
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;
994 return 0;
997 static int adv7511_set_fmt(struct v4l2_subdev *sd,
998 struct v4l2_subdev_pad_config *cfg,
999 struct v4l2_subdev_format *format)
1001 struct adv7511_state *state = get_adv7511_state(sd);
1003 * Bitfield namings come the CEA-861-F standard, table 8 "Auxiliary
1004 * Video Information (AVI) InfoFrame Format"
1006 * c = Colorimetry
1007 * ec = Extended Colorimetry
1008 * y = RGB or YCbCr
1009 * q = RGB Quantization Range
1010 * yq = YCC Quantization Range
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;
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;
1031 adv7511_fill_format(state, &format->format);
1032 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1033 struct v4l2_mbus_framefmt *fmt;
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;
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;
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;
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;
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;
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;
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.
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.
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.
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;
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);
1136 return 0;
1139 static 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,
1148 /* --------------------- SUBDEV OPS --------------------------------------- */
1150 static 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,
1157 /* ----------------------------------------------------------------------- */
1158 static void adv7511_dbg_dump_edid(int lvl, int debug, struct v4l2_subdev *sd, int segment, u8 *buf)
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;
1172 bp[0] = '\0';
1173 v4l2_dbg(lvl, debug, sd, "%s\n", b);
1178 static void adv7511_notify_no_edid(struct v4l2_subdev *sd)
1180 struct adv7511_state *state = get_adv7511_state(sd);
1181 struct adv7511_edid_detect ed;
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);
1190 static void adv7511_edid_handler(struct work_struct *work)
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;
1196 v4l2_dbg(1, debug, sd, "%s:\n", __func__);
1198 if (adv7511_check_edid_status(sd)) {
1199 /* Return if we received the EDID. */
1200 return;
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;
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__);
1223 static void adv7511_audio_setup(struct v4l2_subdev *sd)
1225 v4l2_dbg(1, debug, sd, "%s\n", __func__);
1227 adv7511_s_i2s_clock_freq(sd, 48000);
1228 adv7511_s_clock_freq(sd, 48000);
1229 adv7511_s_routing(sd, 0, 0, 0);
1232 /* Configure hdmi transmitter. */
1233 static void adv7511_setup(struct v4l2_subdev *sd)
1235 struct adv7511_state *state = get_adv7511_state(sd);
1236 v4l2_dbg(1, debug, sd, "%s\n", __func__);
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);
1258 /* Positive clk edge capture for input video clock */
1259 adv7511_wr_and_or(sd, 0xba, 0x1f, 0x60);
1261 adv7511_audio_setup(sd);
1263 v4l2_ctrl_handler_setup(&state->hdl);
1266 static void adv7511_notify_monitor_detect(struct v4l2_subdev *sd)
1268 struct adv7511_monitor_detect mdt;
1269 struct adv7511_state *state = get_adv7511_state(sd);
1271 mdt.present = state->have_monitor;
1272 v4l2_subdev_notify(sd, ADV7511_MONITOR_DETECT, (void *)&mdt);
1275 static void adv7511_check_monitor_present_status(struct v4l2_subdev *sd)
1277 struct adv7511_state *state = get_adv7511_state(sd);
1278 /* read hotplug and rx-sense state */
1279 u8 status = adv7511_rd(sd, 0x42);
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" : "");
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);
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;
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);
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);
1317 adv7511_s_power(sd, false);
1318 memset(&state->edid, 0, sizeof(struct adv7511_state_edid));
1319 adv7511_notify_no_edid(sd);
1323 static bool edid_block_verify_crc(u8 *edid_block)
1325 u8 sum = 0;
1326 int i;
1328 for (i = 0; i < 128; i++)
1329 sum += edid_block[i];
1330 return sum == 0;
1333 static bool edid_verify_crc(struct v4l2_subdev *sd, u32 segment)
1335 struct adv7511_state *state = get_adv7511_state(sd);
1336 u32 blocks = state->edid.blocks;
1337 u8 *data = state->edid.data;
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;
1346 static bool edid_verify_header(struct v4l2_subdev *sd, u32 segment)
1348 static const u8 hdmi_header[] = {
1349 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00
1351 struct adv7511_state *state = get_adv7511_state(sd);
1352 u8 *data = state->edid.data;
1354 if (segment != 0)
1355 return true;
1356 return !memcmp(data, hdmi_header, sizeof(hdmi_header));
1359 static bool adv7511_check_edid_status(struct v4l2_subdev *sd)
1361 struct adv7511_state *state = get_adv7511_state(sd);
1362 u8 edidRdy = adv7511_rd(sd, 0xc5);
1364 v4l2_dbg(1, debug, sd, "%s: edid ready (retries: %d)\n",
1365 __func__, EDID_MAX_RETRIES - state->edid.read_retries);
1367 if (state->edid.complete)
1368 return true;
1370 if (edidRdy & MASK_ADV7511_EDID_RDY) {
1371 int segment = adv7511_rd(sd, 0xc4);
1372 struct adv7511_edid_detect ed;
1374 if (segment >= EDID_MAX_SEGM) {
1375 v4l2_err(sd, "edid segment number too big\n");
1376 return false;
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);
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;
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;
1407 v4l2_dbg(1, debug, sd, "%s: edid complete with %d segment(s)\n", __func__, state->edid.segments);
1408 state->edid.complete = true;
1410 /* report when we have all segments
1411 but report only for segment 0
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;
1420 return false;
1423 /* ----------------------------------------------------------------------- */
1424 /* Setup ADV7511 */
1425 static void adv7511_init_setup(struct v4l2_subdev *sd)
1427 struct adv7511_state *state = get_adv7511_state(sd);
1428 struct adv7511_state_edid *edid = &state->edid;
1430 v4l2_dbg(1, debug, sd, "%s\n", __func__);
1432 /* clear all interrupts */
1433 adv7511_wr(sd, 0x96, 0xff);
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.
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);
1447 static int adv7511_probe(struct i2c_client *client, const struct i2c_device_id *id)
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;
1456 /* Check if the adapter supports the needed features */
1457 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1458 return -EIO;
1460 state = devm_kzalloc(&client->dev, sizeof(struct adv7511_state), GFP_KERNEL);
1461 if (!state)
1462 return -ENOMEM;
1464 /* Platform data */
1465 if (!pdata) {
1466 v4l_err(client, "No platform data!\n");
1467 return -ENODEV;
1469 memcpy(&state->pdata, pdata, sizeof(state->pdata));
1470 state->fmt_code = MEDIA_BUS_FMT_RGB888_1X24;
1471 state->colorspace = V4L2_COLORSPACE_SRGB;
1473 sd = &state->sd;
1475 v4l2_dbg(1, debug, sd, "detecting adv7511 client on address 0x%x\n",
1476 client->addr << 1);
1478 v4l2_i2c_subdev_init(sd, client, &adv7511_ops);
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;
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;
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;
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;
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;
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;
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;
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;
1552 INIT_DELAYED_WORK(&state->edid_handler, adv7511_edid_handler);
1554 adv7511_init_setup(sd);
1555 adv7511_set_isr(sd, true);
1556 adv7511_check_monitor_present_status(sd);
1558 v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
1559 client->addr << 1, client->adapter->name);
1560 return 0;
1562 err_unreg_pktmem:
1563 i2c_unregister_device(state->i2c_pktmem);
1564 err_unreg_edid:
1565 i2c_unregister_device(state->i2c_edid);
1566 err_entity:
1567 media_entity_cleanup(&sd->entity);
1568 err_hdl:
1569 v4l2_ctrl_handler_free(&state->hdl);
1570 return err;
1573 /* ----------------------------------------------------------------------- */
1575 static int adv7511_remove(struct i2c_client *client)
1577 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1578 struct adv7511_state *state = get_adv7511_state(sd);
1580 state->chip_revision = -1;
1582 v4l2_dbg(1, debug, sd, "%s removed @ 0x%x (%s)\n", client->name,
1583 client->addr << 1, client->adapter->name);
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;
1596 /* ----------------------------------------------------------------------- */
1598 static struct i2c_device_id adv7511_id[] = {
1599 { "adv7511", 0 },
1602 MODULE_DEVICE_TABLE(i2c, adv7511_id);
1604 static struct i2c_driver adv7511_driver = {
1605 .driver = {
1606 .name = "adv7511",
1608 .probe = adv7511_probe,
1609 .remove = adv7511_remove,
1610 .id_table = adv7511_id,
1613 module_i2c_driver(adv7511_driver);