Hans Verkuil | 5a544cc | 2013-08-23 09:12:36 -0300 | [diff] [blame] | 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/v4l2-dv-timings.h> |
| 30 | #include <media/v4l2-device.h> |
| 31 | #include <media/v4l2-common.h> |
| 32 | #include <media/v4l2-ctrls.h> |
| 33 | #include <media/v4l2-dv-timings.h> |
| 34 | #include <media/adv7511.h> |
| 35 | |
| 36 | static int debug; |
| 37 | module_param(debug, int, 0644); |
| 38 | MODULE_PARM_DESC(debug, "debug level (0-2)"); |
| 39 | |
| 40 | MODULE_DESCRIPTION("Analog Devices ADV7511 HDMI Transmitter Device Driver"); |
| 41 | MODULE_AUTHOR("Hans Verkuil"); |
| 42 | MODULE_LICENSE("GPL"); |
| 43 | |
| 44 | #define MASK_ADV7511_EDID_RDY_INT 0x04 |
| 45 | #define MASK_ADV7511_MSEN_INT 0x40 |
| 46 | #define MASK_ADV7511_HPD_INT 0x80 |
| 47 | |
| 48 | #define MASK_ADV7511_HPD_DETECT 0x40 |
| 49 | #define MASK_ADV7511_MSEN_DETECT 0x20 |
| 50 | #define MASK_ADV7511_EDID_RDY 0x10 |
| 51 | |
| 52 | #define EDID_MAX_RETRIES (8) |
| 53 | #define EDID_DELAY 250 |
| 54 | #define EDID_MAX_SEGM 8 |
| 55 | |
| 56 | #define ADV7511_MAX_WIDTH 1920 |
| 57 | #define ADV7511_MAX_HEIGHT 1200 |
| 58 | #define ADV7511_MIN_PIXELCLOCK 20000000 |
| 59 | #define ADV7511_MAX_PIXELCLOCK 225000000 |
| 60 | |
| 61 | /* |
| 62 | ********************************************************************** |
| 63 | * |
| 64 | * Arrays with configuration parameters for the ADV7511 |
| 65 | * |
| 66 | ********************************************************************** |
| 67 | */ |
| 68 | |
| 69 | struct i2c_reg_value { |
| 70 | unsigned char reg; |
| 71 | unsigned char value; |
| 72 | }; |
| 73 | |
| 74 | struct adv7511_state_edid { |
| 75 | /* total number of blocks */ |
| 76 | u32 blocks; |
| 77 | /* Number of segments read */ |
| 78 | u32 segments; |
| 79 | uint8_t data[EDID_MAX_SEGM * 256]; |
| 80 | /* Number of EDID read retries left */ |
| 81 | unsigned read_retries; |
| 82 | bool complete; |
| 83 | }; |
| 84 | |
| 85 | struct adv7511_state { |
| 86 | struct adv7511_platform_data pdata; |
| 87 | struct v4l2_subdev sd; |
| 88 | struct media_pad pad; |
| 89 | struct v4l2_ctrl_handler hdl; |
| 90 | int chip_revision; |
| 91 | uint8_t i2c_edid_addr; |
| 92 | uint8_t i2c_cec_addr; |
| 93 | /* Is the adv7511 powered on? */ |
| 94 | bool power_on; |
| 95 | /* Did we receive hotplug and rx-sense signals? */ |
| 96 | bool have_monitor; |
| 97 | /* timings from s_dv_timings */ |
| 98 | struct v4l2_dv_timings dv_timings; |
| 99 | /* controls */ |
| 100 | struct v4l2_ctrl *hdmi_mode_ctrl; |
| 101 | struct v4l2_ctrl *hotplug_ctrl; |
| 102 | struct v4l2_ctrl *rx_sense_ctrl; |
| 103 | struct v4l2_ctrl *have_edid0_ctrl; |
| 104 | struct v4l2_ctrl *rgb_quantization_range_ctrl; |
| 105 | struct i2c_client *i2c_edid; |
| 106 | struct adv7511_state_edid edid; |
| 107 | /* Running counter of the number of detected EDIDs (for debugging) */ |
| 108 | unsigned edid_detect_counter; |
| 109 | struct workqueue_struct *work_queue; |
| 110 | struct delayed_work edid_handler; /* work entry */ |
| 111 | }; |
| 112 | |
| 113 | static void adv7511_check_monitor_present_status(struct v4l2_subdev *sd); |
| 114 | static bool adv7511_check_edid_status(struct v4l2_subdev *sd); |
| 115 | static void adv7511_setup(struct v4l2_subdev *sd); |
| 116 | static int adv7511_s_i2s_clock_freq(struct v4l2_subdev *sd, u32 freq); |
| 117 | static int adv7511_s_clock_freq(struct v4l2_subdev *sd, u32 freq); |
| 118 | |
| 119 | |
| 120 | static const struct v4l2_dv_timings_cap adv7511_timings_cap = { |
| 121 | .type = V4L2_DV_BT_656_1120, |
Gianluca Gennari | aefd4a5 | 2013-08-30 08:29:23 -0300 | [diff] [blame] | 122 | /* keep this initialization for compatibility with GCC < 4.4.6 */ |
| 123 | .reserved = { 0 }, |
| 124 | V4L2_INIT_BT_TIMINGS(0, ADV7511_MAX_WIDTH, 0, ADV7511_MAX_HEIGHT, |
| 125 | ADV7511_MIN_PIXELCLOCK, ADV7511_MAX_PIXELCLOCK, |
| 126 | V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT | |
Hans Verkuil | 5a544cc | 2013-08-23 09:12:36 -0300 | [diff] [blame] | 127 | V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT, |
Gianluca Gennari | aefd4a5 | 2013-08-30 08:29:23 -0300 | [diff] [blame] | 128 | V4L2_DV_BT_CAP_PROGRESSIVE | V4L2_DV_BT_CAP_REDUCED_BLANKING | |
| 129 | V4L2_DV_BT_CAP_CUSTOM) |
Hans Verkuil | 5a544cc | 2013-08-23 09:12:36 -0300 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | static inline struct adv7511_state *get_adv7511_state(struct v4l2_subdev *sd) |
| 133 | { |
| 134 | return container_of(sd, struct adv7511_state, sd); |
| 135 | } |
| 136 | |
| 137 | static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl) |
| 138 | { |
| 139 | return &container_of(ctrl->handler, struct adv7511_state, hdl)->sd; |
| 140 | } |
| 141 | |
| 142 | /* ------------------------ I2C ----------------------------------------------- */ |
| 143 | |
| 144 | static s32 adv_smbus_read_byte_data_check(struct i2c_client *client, |
| 145 | u8 command, bool check) |
| 146 | { |
| 147 | union i2c_smbus_data data; |
| 148 | |
| 149 | if (!i2c_smbus_xfer(client->adapter, client->addr, client->flags, |
| 150 | I2C_SMBUS_READ, command, |
| 151 | I2C_SMBUS_BYTE_DATA, &data)) |
| 152 | return data.byte; |
| 153 | if (check) |
| 154 | v4l_err(client, "error reading %02x, %02x\n", |
| 155 | client->addr, command); |
| 156 | return -1; |
| 157 | } |
| 158 | |
| 159 | static s32 adv_smbus_read_byte_data(struct i2c_client *client, u8 command) |
| 160 | { |
| 161 | int i; |
| 162 | for (i = 0; i < 3; i++) { |
| 163 | int ret = adv_smbus_read_byte_data_check(client, command, true); |
| 164 | if (ret >= 0) { |
| 165 | if (i) |
| 166 | v4l_err(client, "read ok after %d retries\n", i); |
| 167 | return ret; |
| 168 | } |
| 169 | } |
| 170 | v4l_err(client, "read failed\n"); |
| 171 | return -1; |
| 172 | } |
| 173 | |
| 174 | static int adv7511_rd(struct v4l2_subdev *sd, u8 reg) |
| 175 | { |
| 176 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 177 | |
| 178 | return adv_smbus_read_byte_data(client, reg); |
| 179 | } |
| 180 | |
| 181 | static int adv7511_wr(struct v4l2_subdev *sd, u8 reg, u8 val) |
| 182 | { |
| 183 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 184 | int ret; |
| 185 | int i; |
| 186 | |
| 187 | for (i = 0; i < 3; i++) { |
| 188 | ret = i2c_smbus_write_byte_data(client, reg, val); |
| 189 | if (ret == 0) |
| 190 | return 0; |
| 191 | } |
| 192 | v4l2_err(sd, "%s: i2c write error\n", __func__); |
| 193 | return ret; |
| 194 | } |
| 195 | |
| 196 | /* To set specific bits in the register, a clear-mask is given (to be AND-ed), |
| 197 | and then the value-mask (to be OR-ed). */ |
| 198 | static inline void adv7511_wr_and_or(struct v4l2_subdev *sd, u8 reg, uint8_t clr_mask, uint8_t val_mask) |
| 199 | { |
| 200 | adv7511_wr(sd, reg, (adv7511_rd(sd, reg) & clr_mask) | val_mask); |
| 201 | } |
| 202 | |
| 203 | static int adv_smbus_read_i2c_block_data(struct i2c_client *client, |
| 204 | u8 command, unsigned length, u8 *values) |
| 205 | { |
| 206 | union i2c_smbus_data data; |
| 207 | int ret; |
| 208 | |
| 209 | if (length > I2C_SMBUS_BLOCK_MAX) |
| 210 | length = I2C_SMBUS_BLOCK_MAX; |
| 211 | data.block[0] = length; |
| 212 | |
| 213 | ret = i2c_smbus_xfer(client->adapter, client->addr, client->flags, |
| 214 | I2C_SMBUS_READ, command, |
| 215 | I2C_SMBUS_I2C_BLOCK_DATA, &data); |
| 216 | memcpy(values, data.block + 1, length); |
| 217 | return ret; |
| 218 | } |
| 219 | |
| 220 | static inline void adv7511_edid_rd(struct v4l2_subdev *sd, uint16_t len, uint8_t *buf) |
| 221 | { |
| 222 | struct adv7511_state *state = get_adv7511_state(sd); |
| 223 | int i; |
| 224 | int err = 0; |
| 225 | |
| 226 | v4l2_dbg(1, debug, sd, "%s:\n", __func__); |
| 227 | |
| 228 | for (i = 0; !err && i < len; i += I2C_SMBUS_BLOCK_MAX) |
| 229 | err = adv_smbus_read_i2c_block_data(state->i2c_edid, i, |
| 230 | I2C_SMBUS_BLOCK_MAX, buf + i); |
| 231 | if (err) |
| 232 | v4l2_err(sd, "%s: i2c read error\n", __func__); |
| 233 | } |
| 234 | |
| 235 | static inline bool adv7511_have_hotplug(struct v4l2_subdev *sd) |
| 236 | { |
| 237 | return adv7511_rd(sd, 0x42) & MASK_ADV7511_HPD_DETECT; |
| 238 | } |
| 239 | |
| 240 | static inline bool adv7511_have_rx_sense(struct v4l2_subdev *sd) |
| 241 | { |
| 242 | return adv7511_rd(sd, 0x42) & MASK_ADV7511_MSEN_DETECT; |
| 243 | } |
| 244 | |
| 245 | static void adv7511_csc_conversion_mode(struct v4l2_subdev *sd, uint8_t mode) |
| 246 | { |
| 247 | adv7511_wr_and_or(sd, 0x18, 0x9f, (mode & 0x3)<<5); |
| 248 | } |
| 249 | |
| 250 | static void adv7511_csc_coeff(struct v4l2_subdev *sd, |
| 251 | u16 A1, u16 A2, u16 A3, u16 A4, |
| 252 | u16 B1, u16 B2, u16 B3, u16 B4, |
| 253 | u16 C1, u16 C2, u16 C3, u16 C4) |
| 254 | { |
| 255 | /* A */ |
| 256 | adv7511_wr_and_or(sd, 0x18, 0xe0, A1>>8); |
| 257 | adv7511_wr(sd, 0x19, A1); |
| 258 | adv7511_wr_and_or(sd, 0x1A, 0xe0, A2>>8); |
| 259 | adv7511_wr(sd, 0x1B, A2); |
| 260 | adv7511_wr_and_or(sd, 0x1c, 0xe0, A3>>8); |
| 261 | adv7511_wr(sd, 0x1d, A3); |
| 262 | adv7511_wr_and_or(sd, 0x1e, 0xe0, A4>>8); |
| 263 | adv7511_wr(sd, 0x1f, A4); |
| 264 | |
| 265 | /* B */ |
| 266 | adv7511_wr_and_or(sd, 0x20, 0xe0, B1>>8); |
| 267 | adv7511_wr(sd, 0x21, B1); |
| 268 | adv7511_wr_and_or(sd, 0x22, 0xe0, B2>>8); |
| 269 | adv7511_wr(sd, 0x23, B2); |
| 270 | adv7511_wr_and_or(sd, 0x24, 0xe0, B3>>8); |
| 271 | adv7511_wr(sd, 0x25, B3); |
| 272 | adv7511_wr_and_or(sd, 0x26, 0xe0, B4>>8); |
| 273 | adv7511_wr(sd, 0x27, B4); |
| 274 | |
| 275 | /* C */ |
| 276 | adv7511_wr_and_or(sd, 0x28, 0xe0, C1>>8); |
| 277 | adv7511_wr(sd, 0x29, C1); |
| 278 | adv7511_wr_and_or(sd, 0x2A, 0xe0, C2>>8); |
| 279 | adv7511_wr(sd, 0x2B, C2); |
| 280 | adv7511_wr_and_or(sd, 0x2C, 0xe0, C3>>8); |
| 281 | adv7511_wr(sd, 0x2D, C3); |
| 282 | adv7511_wr_and_or(sd, 0x2E, 0xe0, C4>>8); |
| 283 | adv7511_wr(sd, 0x2F, C4); |
| 284 | } |
| 285 | |
| 286 | static void adv7511_csc_rgb_full2limit(struct v4l2_subdev *sd, bool enable) |
| 287 | { |
| 288 | if (enable) { |
| 289 | uint8_t csc_mode = 0; |
| 290 | adv7511_csc_conversion_mode(sd, csc_mode); |
| 291 | adv7511_csc_coeff(sd, |
| 292 | 4096-564, 0, 0, 256, |
| 293 | 0, 4096-564, 0, 256, |
| 294 | 0, 0, 4096-564, 256); |
| 295 | /* enable CSC */ |
| 296 | adv7511_wr_and_or(sd, 0x18, 0x7f, 0x80); |
| 297 | /* AVI infoframe: Limited range RGB (16-235) */ |
| 298 | adv7511_wr_and_or(sd, 0x57, 0xf3, 0x04); |
| 299 | } else { |
| 300 | /* disable CSC */ |
| 301 | adv7511_wr_and_or(sd, 0x18, 0x7f, 0x0); |
| 302 | /* AVI infoframe: Full range RGB (0-255) */ |
| 303 | adv7511_wr_and_or(sd, 0x57, 0xf3, 0x08); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | static void adv7511_set_IT_content_AVI_InfoFrame(struct v4l2_subdev *sd) |
| 308 | { |
| 309 | struct adv7511_state *state = get_adv7511_state(sd); |
| 310 | if (state->dv_timings.bt.standards & V4L2_DV_BT_STD_CEA861) { |
| 311 | /* CEA format, not IT */ |
| 312 | adv7511_wr_and_or(sd, 0x57, 0x7f, 0x00); |
| 313 | } else { |
| 314 | /* IT format */ |
| 315 | adv7511_wr_and_or(sd, 0x57, 0x7f, 0x80); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | static int adv7511_set_rgb_quantization_mode(struct v4l2_subdev *sd, struct v4l2_ctrl *ctrl) |
| 320 | { |
| 321 | switch (ctrl->val) { |
| 322 | default: |
| 323 | return -EINVAL; |
| 324 | break; |
| 325 | case V4L2_DV_RGB_RANGE_AUTO: { |
| 326 | /* automatic */ |
| 327 | struct adv7511_state *state = get_adv7511_state(sd); |
| 328 | |
| 329 | if (state->dv_timings.bt.standards & V4L2_DV_BT_STD_CEA861) { |
| 330 | /* cea format, RGB limited range (16-235) */ |
| 331 | adv7511_csc_rgb_full2limit(sd, true); |
| 332 | } else { |
| 333 | /* not cea format, RGB full range (0-255) */ |
| 334 | adv7511_csc_rgb_full2limit(sd, false); |
| 335 | } |
| 336 | } |
| 337 | break; |
| 338 | case V4L2_DV_RGB_RANGE_LIMITED: |
| 339 | /* RGB limited range (16-235) */ |
| 340 | adv7511_csc_rgb_full2limit(sd, true); |
| 341 | break; |
| 342 | case V4L2_DV_RGB_RANGE_FULL: |
| 343 | /* RGB full range (0-255) */ |
| 344 | adv7511_csc_rgb_full2limit(sd, false); |
| 345 | break; |
| 346 | } |
| 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | /* ------------------------------ CTRL OPS ------------------------------ */ |
| 351 | |
| 352 | static int adv7511_s_ctrl(struct v4l2_ctrl *ctrl) |
| 353 | { |
| 354 | struct v4l2_subdev *sd = to_sd(ctrl); |
| 355 | struct adv7511_state *state = get_adv7511_state(sd); |
| 356 | |
| 357 | v4l2_dbg(1, debug, sd, "%s: ctrl id: %d, ctrl->val %d\n", __func__, ctrl->id, ctrl->val); |
| 358 | |
| 359 | if (state->hdmi_mode_ctrl == ctrl) { |
| 360 | /* Set HDMI or DVI-D */ |
| 361 | adv7511_wr_and_or(sd, 0xaf, 0xfd, ctrl->val == V4L2_DV_TX_MODE_HDMI ? 0x02 : 0x00); |
| 362 | return 0; |
| 363 | } |
| 364 | if (state->rgb_quantization_range_ctrl == ctrl) |
| 365 | return adv7511_set_rgb_quantization_mode(sd, ctrl); |
| 366 | |
| 367 | return -EINVAL; |
| 368 | } |
| 369 | |
| 370 | static const struct v4l2_ctrl_ops adv7511_ctrl_ops = { |
| 371 | .s_ctrl = adv7511_s_ctrl, |
| 372 | }; |
| 373 | |
| 374 | /* ---------------------------- CORE OPS ------------------------------------------- */ |
| 375 | |
| 376 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 377 | static void adv7511_inv_register(struct v4l2_subdev *sd) |
| 378 | { |
| 379 | v4l2_info(sd, "0x000-0x0ff: Main Map\n"); |
| 380 | } |
| 381 | |
| 382 | static int adv7511_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg) |
| 383 | { |
| 384 | reg->size = 1; |
| 385 | switch (reg->reg >> 8) { |
| 386 | case 0: |
| 387 | reg->val = adv7511_rd(sd, reg->reg & 0xff); |
| 388 | break; |
| 389 | default: |
| 390 | v4l2_info(sd, "Register %03llx not supported\n", reg->reg); |
| 391 | adv7511_inv_register(sd); |
| 392 | break; |
| 393 | } |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | static int adv7511_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg) |
| 398 | { |
| 399 | switch (reg->reg >> 8) { |
| 400 | case 0: |
| 401 | adv7511_wr(sd, reg->reg & 0xff, reg->val & 0xff); |
| 402 | break; |
| 403 | default: |
| 404 | v4l2_info(sd, "Register %03llx not supported\n", reg->reg); |
| 405 | adv7511_inv_register(sd); |
| 406 | break; |
| 407 | } |
| 408 | return 0; |
| 409 | } |
| 410 | #endif |
| 411 | |
| 412 | static int adv7511_log_status(struct v4l2_subdev *sd) |
| 413 | { |
| 414 | struct adv7511_state *state = get_adv7511_state(sd); |
| 415 | struct adv7511_state_edid *edid = &state->edid; |
| 416 | |
| 417 | static const char * const states[] = { |
| 418 | "in reset", |
| 419 | "reading EDID", |
| 420 | "idle", |
| 421 | "initializing HDCP", |
| 422 | "HDCP enabled", |
| 423 | "initializing HDCP repeater", |
| 424 | "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" |
| 425 | }; |
| 426 | static const char * const errors[] = { |
| 427 | "no error", |
| 428 | "bad receiver BKSV", |
| 429 | "Ri mismatch", |
| 430 | "Pj mismatch", |
| 431 | "i2c error", |
| 432 | "timed out", |
| 433 | "max repeater cascade exceeded", |
| 434 | "hash check failed", |
| 435 | "too many devices", |
| 436 | "9", "A", "B", "C", "D", "E", "F" |
| 437 | }; |
| 438 | |
| 439 | v4l2_info(sd, "power %s\n", state->power_on ? "on" : "off"); |
| 440 | v4l2_info(sd, "%s hotplug, %s Rx Sense, %s EDID (%d block(s))\n", |
| 441 | (adv7511_rd(sd, 0x42) & MASK_ADV7511_HPD_DETECT) ? "detected" : "no", |
| 442 | (adv7511_rd(sd, 0x42) & MASK_ADV7511_MSEN_DETECT) ? "detected" : "no", |
| 443 | edid->segments ? "found" : "no", |
| 444 | edid->blocks); |
| 445 | v4l2_info(sd, "%s output %s\n", |
| 446 | (adv7511_rd(sd, 0xaf) & 0x02) ? |
| 447 | "HDMI" : "DVI-D", |
| 448 | (adv7511_rd(sd, 0xa1) & 0x3c) ? |
| 449 | "disabled" : "enabled"); |
| 450 | v4l2_info(sd, "state: %s, error: %s, detect count: %u, msk/irq: %02x/%02x\n", |
| 451 | states[adv7511_rd(sd, 0xc8) & 0xf], |
| 452 | errors[adv7511_rd(sd, 0xc8) >> 4], state->edid_detect_counter, |
| 453 | adv7511_rd(sd, 0x94), adv7511_rd(sd, 0x96)); |
| 454 | v4l2_info(sd, "RGB quantization: %s range\n", adv7511_rd(sd, 0x18) & 0x80 ? "limited" : "full"); |
Martin Bugge | 3ecabed | 2013-12-05 09:06:29 -0300 | [diff] [blame] | 455 | if (adv7511_rd(sd, 0xaf) & 0x02) { |
| 456 | /* HDMI only */ |
| 457 | u8 manual_cts = adv7511_rd(sd, 0x0a) & 0x80; |
| 458 | u32 N = (adv7511_rd(sd, 0x01) & 0xf) << 16 | |
| 459 | adv7511_rd(sd, 0x02) << 8 | |
| 460 | adv7511_rd(sd, 0x03); |
| 461 | u8 vic_detect = adv7511_rd(sd, 0x3e) >> 2; |
| 462 | u8 vic_sent = adv7511_rd(sd, 0x3d) & 0x3f; |
| 463 | u32 CTS; |
| 464 | |
| 465 | if (manual_cts) |
| 466 | CTS = (adv7511_rd(sd, 0x07) & 0xf) << 16 | |
| 467 | adv7511_rd(sd, 0x08) << 8 | |
| 468 | adv7511_rd(sd, 0x09); |
| 469 | else |
| 470 | CTS = (adv7511_rd(sd, 0x04) & 0xf) << 16 | |
| 471 | adv7511_rd(sd, 0x05) << 8 | |
| 472 | adv7511_rd(sd, 0x06); |
| 473 | v4l2_info(sd, "CTS %s mode: N %d, CTS %d\n", |
| 474 | manual_cts ? "manual" : "automatic", N, CTS); |
| 475 | v4l2_info(sd, "VIC: detected %d, sent %d\n", |
| 476 | vic_detect, vic_sent); |
| 477 | } |
Hans Verkuil | 5a544cc | 2013-08-23 09:12:36 -0300 | [diff] [blame] | 478 | if (state->dv_timings.type == V4L2_DV_BT_656_1120) |
| 479 | v4l2_print_dv_timings(sd->name, "timings: ", |
| 480 | &state->dv_timings, false); |
| 481 | else |
| 482 | v4l2_info(sd, "no timings set\n"); |
| 483 | v4l2_info(sd, "i2c edid addr: 0x%x\n", state->i2c_edid_addr); |
| 484 | v4l2_info(sd, "i2c cec addr: 0x%x\n", state->i2c_cec_addr); |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | /* Power up/down adv7511 */ |
| 489 | static int adv7511_s_power(struct v4l2_subdev *sd, int on) |
| 490 | { |
| 491 | struct adv7511_state *state = get_adv7511_state(sd); |
| 492 | const int retries = 20; |
| 493 | int i; |
| 494 | |
| 495 | v4l2_dbg(1, debug, sd, "%s: power %s\n", __func__, on ? "on" : "off"); |
| 496 | |
| 497 | state->power_on = on; |
| 498 | |
| 499 | if (!on) { |
| 500 | /* Power down */ |
| 501 | adv7511_wr_and_or(sd, 0x41, 0xbf, 0x40); |
| 502 | return true; |
| 503 | } |
| 504 | |
| 505 | /* Power up */ |
| 506 | /* The adv7511 does not always come up immediately. |
| 507 | Retry multiple times. */ |
| 508 | for (i = 0; i < retries; i++) { |
| 509 | adv7511_wr_and_or(sd, 0x41, 0xbf, 0x0); |
| 510 | if ((adv7511_rd(sd, 0x41) & 0x40) == 0) |
| 511 | break; |
| 512 | adv7511_wr_and_or(sd, 0x41, 0xbf, 0x40); |
| 513 | msleep(10); |
| 514 | } |
| 515 | if (i == retries) { |
| 516 | v4l2_dbg(1, debug, sd, "%s: failed to powerup the adv7511!\n", __func__); |
| 517 | adv7511_s_power(sd, 0); |
| 518 | return false; |
| 519 | } |
| 520 | if (i > 1) |
| 521 | v4l2_dbg(1, debug, sd, "%s: needed %d retries to powerup the adv7511\n", __func__, i); |
| 522 | |
| 523 | /* Reserved registers that must be set */ |
| 524 | adv7511_wr(sd, 0x98, 0x03); |
| 525 | adv7511_wr_and_or(sd, 0x9a, 0xfe, 0x70); |
| 526 | adv7511_wr(sd, 0x9c, 0x30); |
| 527 | adv7511_wr_and_or(sd, 0x9d, 0xfc, 0x01); |
| 528 | adv7511_wr(sd, 0xa2, 0xa4); |
| 529 | adv7511_wr(sd, 0xa3, 0xa4); |
| 530 | adv7511_wr(sd, 0xe0, 0xd0); |
| 531 | adv7511_wr(sd, 0xf9, 0x00); |
| 532 | |
| 533 | adv7511_wr(sd, 0x43, state->i2c_edid_addr); |
| 534 | |
| 535 | /* Set number of attempts to read the EDID */ |
| 536 | adv7511_wr(sd, 0xc9, 0xf); |
| 537 | return true; |
| 538 | } |
| 539 | |
| 540 | /* Enable interrupts */ |
| 541 | static void adv7511_set_isr(struct v4l2_subdev *sd, bool enable) |
| 542 | { |
| 543 | uint8_t irqs = MASK_ADV7511_HPD_INT | MASK_ADV7511_MSEN_INT; |
| 544 | uint8_t irqs_rd; |
| 545 | int retries = 100; |
| 546 | |
| 547 | v4l2_dbg(2, debug, sd, "%s: %s\n", __func__, enable ? "enable" : "disable"); |
| 548 | |
| 549 | /* The datasheet says that the EDID ready interrupt should be |
| 550 | disabled if there is no hotplug. */ |
| 551 | if (!enable) |
| 552 | irqs = 0; |
| 553 | else if (adv7511_have_hotplug(sd)) |
| 554 | irqs |= MASK_ADV7511_EDID_RDY_INT; |
| 555 | |
| 556 | /* |
| 557 | * This i2c write can fail (approx. 1 in 1000 writes). But it |
| 558 | * is essential that this register is correct, so retry it |
| 559 | * multiple times. |
| 560 | * |
| 561 | * Note that the i2c write does not report an error, but the readback |
| 562 | * clearly shows the wrong value. |
| 563 | */ |
| 564 | do { |
| 565 | adv7511_wr(sd, 0x94, irqs); |
| 566 | irqs_rd = adv7511_rd(sd, 0x94); |
| 567 | } while (retries-- && irqs_rd != irqs); |
| 568 | |
| 569 | if (irqs_rd == irqs) |
| 570 | return; |
| 571 | v4l2_err(sd, "Could not set interrupts: hw failure?\n"); |
| 572 | } |
| 573 | |
| 574 | /* Interrupt handler */ |
| 575 | static int adv7511_isr(struct v4l2_subdev *sd, u32 status, bool *handled) |
| 576 | { |
| 577 | uint8_t irq_status; |
| 578 | |
| 579 | /* disable interrupts to prevent a race condition */ |
| 580 | adv7511_set_isr(sd, false); |
| 581 | irq_status = adv7511_rd(sd, 0x96); |
| 582 | /* clear detected interrupts */ |
| 583 | adv7511_wr(sd, 0x96, irq_status); |
| 584 | |
| 585 | v4l2_dbg(1, debug, sd, "%s: irq 0x%x\n", __func__, irq_status); |
| 586 | |
| 587 | if (irq_status & (MASK_ADV7511_HPD_INT | MASK_ADV7511_MSEN_INT)) |
| 588 | adv7511_check_monitor_present_status(sd); |
| 589 | if (irq_status & MASK_ADV7511_EDID_RDY_INT) |
| 590 | adv7511_check_edid_status(sd); |
| 591 | |
| 592 | /* enable interrupts */ |
| 593 | adv7511_set_isr(sd, true); |
| 594 | |
| 595 | if (handled) |
| 596 | *handled = true; |
| 597 | return 0; |
| 598 | } |
| 599 | |
Hans Verkuil | b09dfac | 2014-03-04 08:05:19 -0300 | [diff] [blame] | 600 | static int adv7511_get_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid) |
Hans Verkuil | 5a544cc | 2013-08-23 09:12:36 -0300 | [diff] [blame] | 601 | { |
| 602 | struct adv7511_state *state = get_adv7511_state(sd); |
| 603 | |
| 604 | if (edid->pad != 0) |
| 605 | return -EINVAL; |
| 606 | if ((edid->blocks == 0) || (edid->blocks > 256)) |
| 607 | return -EINVAL; |
| 608 | if (!edid->edid) |
| 609 | return -EINVAL; |
| 610 | if (!state->edid.segments) { |
| 611 | v4l2_dbg(1, debug, sd, "EDID segment 0 not found\n"); |
| 612 | return -ENODATA; |
| 613 | } |
| 614 | if (edid->start_block >= state->edid.segments * 2) |
| 615 | return -E2BIG; |
| 616 | if ((edid->blocks + edid->start_block) >= state->edid.segments * 2) |
| 617 | edid->blocks = state->edid.segments * 2 - edid->start_block; |
| 618 | |
| 619 | memcpy(edid->edid, &state->edid.data[edid->start_block * 128], |
| 620 | 128 * edid->blocks); |
| 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | static const struct v4l2_subdev_pad_ops adv7511_pad_ops = { |
| 625 | .get_edid = adv7511_get_edid, |
| 626 | }; |
| 627 | |
| 628 | static const struct v4l2_subdev_core_ops adv7511_core_ops = { |
| 629 | .log_status = adv7511_log_status, |
| 630 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 631 | .g_register = adv7511_g_register, |
| 632 | .s_register = adv7511_s_register, |
| 633 | #endif |
| 634 | .s_power = adv7511_s_power, |
| 635 | .interrupt_service_routine = adv7511_isr, |
| 636 | }; |
| 637 | |
| 638 | /* ------------------------------ VIDEO OPS ------------------------------ */ |
| 639 | |
| 640 | /* Enable/disable adv7511 output */ |
| 641 | static int adv7511_s_stream(struct v4l2_subdev *sd, int enable) |
| 642 | { |
| 643 | struct adv7511_state *state = get_adv7511_state(sd); |
| 644 | |
| 645 | v4l2_dbg(1, debug, sd, "%s: %sable\n", __func__, (enable ? "en" : "dis")); |
| 646 | adv7511_wr_and_or(sd, 0xa1, ~0x3c, (enable ? 0 : 0x3c)); |
| 647 | if (enable) { |
| 648 | adv7511_check_monitor_present_status(sd); |
| 649 | } else { |
| 650 | adv7511_s_power(sd, 0); |
| 651 | state->have_monitor = false; |
| 652 | } |
| 653 | return 0; |
| 654 | } |
| 655 | |
| 656 | static int adv7511_s_dv_timings(struct v4l2_subdev *sd, |
| 657 | struct v4l2_dv_timings *timings) |
| 658 | { |
| 659 | struct adv7511_state *state = get_adv7511_state(sd); |
| 660 | |
| 661 | v4l2_dbg(1, debug, sd, "%s:\n", __func__); |
| 662 | |
| 663 | /* quick sanity check */ |
| 664 | if (!v4l2_valid_dv_timings(timings, &adv7511_timings_cap, NULL, NULL)) |
| 665 | return -EINVAL; |
| 666 | |
| 667 | /* Fill the optional fields .standards and .flags in struct v4l2_dv_timings |
| 668 | if the format is one of the CEA or DMT timings. */ |
| 669 | v4l2_find_dv_timings_cap(timings, &adv7511_timings_cap, 0, NULL, NULL); |
| 670 | |
| 671 | timings->bt.flags &= ~V4L2_DV_FL_REDUCED_FPS; |
| 672 | |
| 673 | /* save timings */ |
| 674 | state->dv_timings = *timings; |
| 675 | |
| 676 | /* update quantization range based on new dv_timings */ |
| 677 | adv7511_set_rgb_quantization_mode(sd, state->rgb_quantization_range_ctrl); |
| 678 | |
| 679 | /* update AVI infoframe */ |
| 680 | adv7511_set_IT_content_AVI_InfoFrame(sd); |
| 681 | |
| 682 | return 0; |
| 683 | } |
| 684 | |
| 685 | static int adv7511_g_dv_timings(struct v4l2_subdev *sd, |
| 686 | struct v4l2_dv_timings *timings) |
| 687 | { |
| 688 | struct adv7511_state *state = get_adv7511_state(sd); |
| 689 | |
| 690 | v4l2_dbg(1, debug, sd, "%s:\n", __func__); |
| 691 | |
| 692 | if (!timings) |
| 693 | return -EINVAL; |
| 694 | |
| 695 | *timings = state->dv_timings; |
| 696 | |
| 697 | return 0; |
| 698 | } |
| 699 | |
| 700 | static int adv7511_enum_dv_timings(struct v4l2_subdev *sd, |
| 701 | struct v4l2_enum_dv_timings *timings) |
| 702 | { |
| 703 | return v4l2_enum_dv_timings_cap(timings, &adv7511_timings_cap, NULL, NULL); |
| 704 | } |
| 705 | |
| 706 | static int adv7511_dv_timings_cap(struct v4l2_subdev *sd, |
| 707 | struct v4l2_dv_timings_cap *cap) |
| 708 | { |
| 709 | *cap = adv7511_timings_cap; |
| 710 | return 0; |
| 711 | } |
| 712 | |
| 713 | static const struct v4l2_subdev_video_ops adv7511_video_ops = { |
| 714 | .s_stream = adv7511_s_stream, |
| 715 | .s_dv_timings = adv7511_s_dv_timings, |
| 716 | .g_dv_timings = adv7511_g_dv_timings, |
| 717 | .enum_dv_timings = adv7511_enum_dv_timings, |
| 718 | .dv_timings_cap = adv7511_dv_timings_cap, |
| 719 | }; |
| 720 | |
| 721 | /* ------------------------------ AUDIO OPS ------------------------------ */ |
| 722 | static int adv7511_s_audio_stream(struct v4l2_subdev *sd, int enable) |
| 723 | { |
| 724 | v4l2_dbg(1, debug, sd, "%s: %sable\n", __func__, (enable ? "en" : "dis")); |
| 725 | |
| 726 | if (enable) |
| 727 | adv7511_wr_and_or(sd, 0x4b, 0x3f, 0x80); |
| 728 | else |
| 729 | adv7511_wr_and_or(sd, 0x4b, 0x3f, 0x40); |
| 730 | |
| 731 | return 0; |
| 732 | } |
| 733 | |
| 734 | static int adv7511_s_clock_freq(struct v4l2_subdev *sd, u32 freq) |
| 735 | { |
| 736 | u32 N; |
| 737 | |
| 738 | switch (freq) { |
| 739 | case 32000: N = 4096; break; |
| 740 | case 44100: N = 6272; break; |
| 741 | case 48000: N = 6144; break; |
| 742 | case 88200: N = 12544; break; |
| 743 | case 96000: N = 12288; break; |
| 744 | case 176400: N = 25088; break; |
| 745 | case 192000: N = 24576; break; |
| 746 | default: |
| 747 | return -EINVAL; |
| 748 | } |
| 749 | |
| 750 | /* Set N (used with CTS to regenerate the audio clock) */ |
| 751 | adv7511_wr(sd, 0x01, (N >> 16) & 0xf); |
| 752 | adv7511_wr(sd, 0x02, (N >> 8) & 0xff); |
| 753 | adv7511_wr(sd, 0x03, N & 0xff); |
| 754 | |
| 755 | return 0; |
| 756 | } |
| 757 | |
| 758 | static int adv7511_s_i2s_clock_freq(struct v4l2_subdev *sd, u32 freq) |
| 759 | { |
| 760 | u32 i2s_sf; |
| 761 | |
| 762 | switch (freq) { |
| 763 | case 32000: i2s_sf = 0x30; break; |
| 764 | case 44100: i2s_sf = 0x00; break; |
| 765 | case 48000: i2s_sf = 0x20; break; |
| 766 | case 88200: i2s_sf = 0x80; break; |
| 767 | case 96000: i2s_sf = 0xa0; break; |
| 768 | case 176400: i2s_sf = 0xc0; break; |
| 769 | case 192000: i2s_sf = 0xe0; break; |
| 770 | default: |
| 771 | return -EINVAL; |
| 772 | } |
| 773 | |
| 774 | /* Set sampling frequency for I2S audio to 48 kHz */ |
| 775 | adv7511_wr_and_or(sd, 0x15, 0xf, i2s_sf); |
| 776 | |
| 777 | return 0; |
| 778 | } |
| 779 | |
| 780 | static int adv7511_s_routing(struct v4l2_subdev *sd, u32 input, u32 output, u32 config) |
| 781 | { |
| 782 | /* Only 2 channels in use for application */ |
| 783 | adv7511_wr_and_or(sd, 0x73, 0xf8, 0x1); |
| 784 | /* Speaker mapping */ |
| 785 | adv7511_wr(sd, 0x76, 0x00); |
| 786 | |
| 787 | /* 16 bit audio word length */ |
| 788 | adv7511_wr_and_or(sd, 0x14, 0xf0, 0x02); |
| 789 | |
| 790 | return 0; |
| 791 | } |
| 792 | |
| 793 | static const struct v4l2_subdev_audio_ops adv7511_audio_ops = { |
| 794 | .s_stream = adv7511_s_audio_stream, |
| 795 | .s_clock_freq = adv7511_s_clock_freq, |
| 796 | .s_i2s_clock_freq = adv7511_s_i2s_clock_freq, |
| 797 | .s_routing = adv7511_s_routing, |
| 798 | }; |
| 799 | |
| 800 | /* --------------------- SUBDEV OPS --------------------------------------- */ |
| 801 | |
| 802 | static const struct v4l2_subdev_ops adv7511_ops = { |
| 803 | .core = &adv7511_core_ops, |
| 804 | .pad = &adv7511_pad_ops, |
| 805 | .video = &adv7511_video_ops, |
| 806 | .audio = &adv7511_audio_ops, |
| 807 | }; |
| 808 | |
| 809 | /* ----------------------------------------------------------------------- */ |
| 810 | static void adv7511_dbg_dump_edid(int lvl, int debug, struct v4l2_subdev *sd, int segment, uint8_t *buf) |
| 811 | { |
| 812 | if (debug >= lvl) { |
| 813 | int i, j; |
| 814 | v4l2_dbg(lvl, debug, sd, "edid segment %d\n", segment); |
| 815 | for (i = 0; i < 256; i += 16) { |
| 816 | u8 b[128]; |
| 817 | u8 *bp = b; |
| 818 | if (i == 128) |
| 819 | v4l2_dbg(lvl, debug, sd, "\n"); |
| 820 | for (j = i; j < i + 16; j++) { |
| 821 | sprintf(bp, "0x%02x, ", buf[j]); |
| 822 | bp += 6; |
| 823 | } |
| 824 | bp[0] = '\0'; |
| 825 | v4l2_dbg(lvl, debug, sd, "%s\n", b); |
| 826 | } |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | static void adv7511_edid_handler(struct work_struct *work) |
| 831 | { |
| 832 | struct delayed_work *dwork = to_delayed_work(work); |
| 833 | struct adv7511_state *state = container_of(dwork, struct adv7511_state, edid_handler); |
| 834 | struct v4l2_subdev *sd = &state->sd; |
| 835 | struct adv7511_edid_detect ed; |
| 836 | |
| 837 | v4l2_dbg(1, debug, sd, "%s:\n", __func__); |
| 838 | |
| 839 | if (adv7511_check_edid_status(sd)) { |
| 840 | /* Return if we received the EDID. */ |
| 841 | return; |
| 842 | } |
| 843 | |
| 844 | if (adv7511_have_hotplug(sd)) { |
| 845 | /* We must retry reading the EDID several times, it is possible |
| 846 | * that initially the EDID couldn't be read due to i2c errors |
| 847 | * (DVI connectors are particularly prone to this problem). */ |
| 848 | if (state->edid.read_retries) { |
| 849 | state->edid.read_retries--; |
| 850 | v4l2_dbg(1, debug, sd, "%s: edid read failed\n", __func__); |
| 851 | state->have_monitor = false; |
| 852 | adv7511_s_power(sd, false); |
| 853 | adv7511_s_power(sd, true); |
| 854 | queue_delayed_work(state->work_queue, &state->edid_handler, EDID_DELAY); |
| 855 | return; |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | /* We failed to read the EDID, so send an event for this. */ |
| 860 | ed.present = false; |
| 861 | ed.segment = adv7511_rd(sd, 0xc4); |
| 862 | v4l2_subdev_notify(sd, ADV7511_EDID_DETECT, (void *)&ed); |
| 863 | v4l2_dbg(1, debug, sd, "%s: no edid found\n", __func__); |
| 864 | } |
| 865 | |
| 866 | static void adv7511_audio_setup(struct v4l2_subdev *sd) |
| 867 | { |
| 868 | v4l2_dbg(1, debug, sd, "%s\n", __func__); |
| 869 | |
| 870 | adv7511_s_i2s_clock_freq(sd, 48000); |
| 871 | adv7511_s_clock_freq(sd, 48000); |
| 872 | adv7511_s_routing(sd, 0, 0, 0); |
| 873 | } |
| 874 | |
| 875 | /* Configure hdmi transmitter. */ |
| 876 | static void adv7511_setup(struct v4l2_subdev *sd) |
| 877 | { |
| 878 | struct adv7511_state *state = get_adv7511_state(sd); |
| 879 | v4l2_dbg(1, debug, sd, "%s\n", __func__); |
| 880 | |
| 881 | /* Input format: RGB 4:4:4 */ |
| 882 | adv7511_wr_and_or(sd, 0x15, 0xf0, 0x0); |
| 883 | /* Output format: RGB 4:4:4 */ |
| 884 | adv7511_wr_and_or(sd, 0x16, 0x7f, 0x0); |
| 885 | /* 1st order interpolation 4:2:2 -> 4:4:4 up conversion, Aspect ratio: 16:9 */ |
| 886 | adv7511_wr_and_or(sd, 0x17, 0xf9, 0x06); |
| 887 | /* Disable pixel repetition */ |
| 888 | adv7511_wr_and_or(sd, 0x3b, 0x9f, 0x0); |
| 889 | /* Disable CSC */ |
| 890 | adv7511_wr_and_or(sd, 0x18, 0x7f, 0x0); |
| 891 | /* Output format: RGB 4:4:4, Active Format Information is valid, |
| 892 | * underscanned */ |
| 893 | adv7511_wr_and_or(sd, 0x55, 0x9c, 0x12); |
| 894 | /* AVI Info frame packet enable, Audio Info frame disable */ |
| 895 | adv7511_wr_and_or(sd, 0x44, 0xe7, 0x10); |
| 896 | /* Colorimetry, Active format aspect ratio: same as picure. */ |
| 897 | adv7511_wr(sd, 0x56, 0xa8); |
| 898 | /* No encryption */ |
| 899 | adv7511_wr_and_or(sd, 0xaf, 0xed, 0x0); |
| 900 | |
| 901 | /* Positive clk edge capture for input video clock */ |
| 902 | adv7511_wr_and_or(sd, 0xba, 0x1f, 0x60); |
| 903 | |
| 904 | adv7511_audio_setup(sd); |
| 905 | |
| 906 | v4l2_ctrl_handler_setup(&state->hdl); |
| 907 | } |
| 908 | |
| 909 | static void adv7511_notify_monitor_detect(struct v4l2_subdev *sd) |
| 910 | { |
| 911 | struct adv7511_monitor_detect mdt; |
| 912 | struct adv7511_state *state = get_adv7511_state(sd); |
| 913 | |
| 914 | mdt.present = state->have_monitor; |
| 915 | v4l2_subdev_notify(sd, ADV7511_MONITOR_DETECT, (void *)&mdt); |
| 916 | } |
| 917 | |
| 918 | static void adv7511_check_monitor_present_status(struct v4l2_subdev *sd) |
| 919 | { |
| 920 | struct adv7511_state *state = get_adv7511_state(sd); |
| 921 | /* read hotplug and rx-sense state */ |
| 922 | uint8_t status = adv7511_rd(sd, 0x42); |
| 923 | |
| 924 | v4l2_dbg(1, debug, sd, "%s: status: 0x%x%s%s\n", |
| 925 | __func__, |
| 926 | status, |
| 927 | status & MASK_ADV7511_HPD_DETECT ? ", hotplug" : "", |
| 928 | status & MASK_ADV7511_MSEN_DETECT ? ", rx-sense" : ""); |
| 929 | |
| 930 | /* update read only ctrls */ |
| 931 | v4l2_ctrl_s_ctrl(state->hotplug_ctrl, adv7511_have_hotplug(sd) ? 0x1 : 0x0); |
| 932 | v4l2_ctrl_s_ctrl(state->rx_sense_ctrl, adv7511_have_rx_sense(sd) ? 0x1 : 0x0); |
| 933 | v4l2_ctrl_s_ctrl(state->have_edid0_ctrl, state->edid.segments ? 0x1 : 0x0); |
| 934 | |
| 935 | if ((status & MASK_ADV7511_HPD_DETECT) && ((status & MASK_ADV7511_MSEN_DETECT) || state->edid.segments)) { |
| 936 | v4l2_dbg(1, debug, sd, "%s: hotplug and (rx-sense or edid)\n", __func__); |
| 937 | if (!state->have_monitor) { |
| 938 | v4l2_dbg(1, debug, sd, "%s: monitor detected\n", __func__); |
| 939 | state->have_monitor = true; |
| 940 | adv7511_set_isr(sd, true); |
| 941 | if (!adv7511_s_power(sd, true)) { |
| 942 | v4l2_dbg(1, debug, sd, "%s: monitor detected, powerup failed\n", __func__); |
| 943 | return; |
| 944 | } |
| 945 | adv7511_setup(sd); |
| 946 | adv7511_notify_monitor_detect(sd); |
| 947 | state->edid.read_retries = EDID_MAX_RETRIES; |
| 948 | queue_delayed_work(state->work_queue, &state->edid_handler, EDID_DELAY); |
| 949 | } |
| 950 | } else if (status & MASK_ADV7511_HPD_DETECT) { |
| 951 | v4l2_dbg(1, debug, sd, "%s: hotplug detected\n", __func__); |
| 952 | state->edid.read_retries = EDID_MAX_RETRIES; |
| 953 | queue_delayed_work(state->work_queue, &state->edid_handler, EDID_DELAY); |
| 954 | } else if (!(status & MASK_ADV7511_HPD_DETECT)) { |
| 955 | v4l2_dbg(1, debug, sd, "%s: hotplug not detected\n", __func__); |
| 956 | if (state->have_monitor) { |
| 957 | v4l2_dbg(1, debug, sd, "%s: monitor not detected\n", __func__); |
| 958 | state->have_monitor = false; |
| 959 | adv7511_notify_monitor_detect(sd); |
| 960 | } |
| 961 | adv7511_s_power(sd, false); |
| 962 | memset(&state->edid, 0, sizeof(struct adv7511_state_edid)); |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | static bool edid_block_verify_crc(uint8_t *edid_block) |
| 967 | { |
Hans Verkuil | 5a544cc | 2013-08-23 09:12:36 -0300 | [diff] [blame] | 968 | uint8_t sum = 0; |
Martin Bugge | 928b0fe | 2013-12-17 09:17:10 -0300 | [diff] [blame] | 969 | int i; |
Hans Verkuil | 5a544cc | 2013-08-23 09:12:36 -0300 | [diff] [blame] | 970 | |
| 971 | for (i = 0; i < 128; i++) |
Martin Bugge | 928b0fe | 2013-12-17 09:17:10 -0300 | [diff] [blame] | 972 | sum += edid_block[i]; |
| 973 | return sum == 0; |
Hans Verkuil | 5a544cc | 2013-08-23 09:12:36 -0300 | [diff] [blame] | 974 | } |
| 975 | |
Martin Bugge | 928b0fe | 2013-12-17 09:17:10 -0300 | [diff] [blame] | 976 | static bool edid_verify_crc(struct v4l2_subdev *sd, u32 segment) |
Hans Verkuil | 5a544cc | 2013-08-23 09:12:36 -0300 | [diff] [blame] | 977 | { |
| 978 | struct adv7511_state *state = get_adv7511_state(sd); |
| 979 | u32 blocks = state->edid.blocks; |
| 980 | uint8_t *data = state->edid.data; |
| 981 | |
Martin Bugge | 928b0fe | 2013-12-17 09:17:10 -0300 | [diff] [blame] | 982 | if (!edid_block_verify_crc(&data[segment * 256])) |
| 983 | return false; |
| 984 | if ((segment + 1) * 2 <= blocks) |
| 985 | return edid_block_verify_crc(&data[segment * 256 + 128]); |
| 986 | return true; |
| 987 | } |
| 988 | |
| 989 | static bool edid_verify_header(struct v4l2_subdev *sd, u32 segment) |
| 990 | { |
| 991 | static const u8 hdmi_header[] = { |
| 992 | 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 |
| 993 | }; |
| 994 | struct adv7511_state *state = get_adv7511_state(sd); |
| 995 | u8 *data = state->edid.data; |
| 996 | |
| 997 | if (segment != 0) |
Hans Verkuil | 5a544cc | 2013-08-23 09:12:36 -0300 | [diff] [blame] | 998 | return true; |
Martin Bugge | 928b0fe | 2013-12-17 09:17:10 -0300 | [diff] [blame] | 999 | return !memcmp(data, hdmi_header, sizeof(hdmi_header)); |
Hans Verkuil | 5a544cc | 2013-08-23 09:12:36 -0300 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | static bool adv7511_check_edid_status(struct v4l2_subdev *sd) |
| 1003 | { |
| 1004 | struct adv7511_state *state = get_adv7511_state(sd); |
| 1005 | uint8_t edidRdy = adv7511_rd(sd, 0xc5); |
| 1006 | |
| 1007 | v4l2_dbg(1, debug, sd, "%s: edid ready (retries: %d)\n", |
| 1008 | __func__, EDID_MAX_RETRIES - state->edid.read_retries); |
| 1009 | |
| 1010 | if (state->edid.complete) |
| 1011 | return true; |
| 1012 | |
| 1013 | if (edidRdy & MASK_ADV7511_EDID_RDY) { |
| 1014 | int segment = adv7511_rd(sd, 0xc4); |
| 1015 | struct adv7511_edid_detect ed; |
| 1016 | |
| 1017 | if (segment >= EDID_MAX_SEGM) { |
| 1018 | v4l2_err(sd, "edid segment number too big\n"); |
| 1019 | return false; |
| 1020 | } |
| 1021 | v4l2_dbg(1, debug, sd, "%s: got segment %d\n", __func__, segment); |
| 1022 | adv7511_edid_rd(sd, 256, &state->edid.data[segment * 256]); |
| 1023 | adv7511_dbg_dump_edid(2, debug, sd, segment, &state->edid.data[segment * 256]); |
| 1024 | if (segment == 0) { |
| 1025 | state->edid.blocks = state->edid.data[0x7e] + 1; |
| 1026 | v4l2_dbg(1, debug, sd, "%s: %d blocks in total\n", __func__, state->edid.blocks); |
| 1027 | } |
Martin Bugge | 928b0fe | 2013-12-17 09:17:10 -0300 | [diff] [blame] | 1028 | if (!edid_verify_crc(sd, segment) || |
| 1029 | !edid_verify_header(sd, segment)) { |
Hans Verkuil | 5a544cc | 2013-08-23 09:12:36 -0300 | [diff] [blame] | 1030 | /* edid crc error, force reread of edid segment */ |
Martin Bugge | 928b0fe | 2013-12-17 09:17:10 -0300 | [diff] [blame] | 1031 | v4l2_err(sd, "%s: edid crc or header error\n", __func__); |
Hans Verkuil | 5a544cc | 2013-08-23 09:12:36 -0300 | [diff] [blame] | 1032 | state->have_monitor = false; |
| 1033 | adv7511_s_power(sd, false); |
| 1034 | adv7511_s_power(sd, true); |
| 1035 | return false; |
| 1036 | } |
| 1037 | /* one more segment read ok */ |
| 1038 | state->edid.segments = segment + 1; |
| 1039 | if (((state->edid.data[0x7e] >> 1) + 1) > state->edid.segments) { |
| 1040 | /* Request next EDID segment */ |
| 1041 | v4l2_dbg(1, debug, sd, "%s: request segment %d\n", __func__, state->edid.segments); |
| 1042 | adv7511_wr(sd, 0xc9, 0xf); |
| 1043 | adv7511_wr(sd, 0xc4, state->edid.segments); |
| 1044 | state->edid.read_retries = EDID_MAX_RETRIES; |
| 1045 | queue_delayed_work(state->work_queue, &state->edid_handler, EDID_DELAY); |
| 1046 | return false; |
| 1047 | } |
| 1048 | |
| 1049 | v4l2_dbg(1, debug, sd, "%s: edid complete with %d segment(s)\n", __func__, state->edid.segments); |
| 1050 | state->edid.complete = true; |
| 1051 | |
| 1052 | /* report when we have all segments |
| 1053 | but report only for segment 0 |
| 1054 | */ |
| 1055 | ed.present = true; |
| 1056 | ed.segment = 0; |
| 1057 | state->edid_detect_counter++; |
| 1058 | v4l2_ctrl_s_ctrl(state->have_edid0_ctrl, state->edid.segments ? 0x1 : 0x0); |
| 1059 | v4l2_subdev_notify(sd, ADV7511_EDID_DETECT, (void *)&ed); |
| 1060 | return ed.present; |
| 1061 | } |
| 1062 | |
| 1063 | return false; |
| 1064 | } |
| 1065 | |
| 1066 | /* ----------------------------------------------------------------------- */ |
| 1067 | /* Setup ADV7511 */ |
| 1068 | static void adv7511_init_setup(struct v4l2_subdev *sd) |
| 1069 | { |
| 1070 | struct adv7511_state *state = get_adv7511_state(sd); |
| 1071 | struct adv7511_state_edid *edid = &state->edid; |
| 1072 | |
| 1073 | v4l2_dbg(1, debug, sd, "%s\n", __func__); |
| 1074 | |
| 1075 | /* clear all interrupts */ |
| 1076 | adv7511_wr(sd, 0x96, 0xff); |
Martin Bugge | a62c621 | 2013-12-05 09:02:20 -0300 | [diff] [blame] | 1077 | /* |
| 1078 | * Stop HPD from resetting a lot of registers. |
| 1079 | * It might leave the chip in a partly un-initialized state, |
| 1080 | * in particular with regards to hotplug bounces. |
| 1081 | */ |
| 1082 | adv7511_wr_and_or(sd, 0xd6, 0x3f, 0xc0); |
Hans Verkuil | 5a544cc | 2013-08-23 09:12:36 -0300 | [diff] [blame] | 1083 | memset(edid, 0, sizeof(struct adv7511_state_edid)); |
| 1084 | state->have_monitor = false; |
| 1085 | adv7511_set_isr(sd, false); |
| 1086 | adv7511_s_stream(sd, false); |
| 1087 | adv7511_s_audio_stream(sd, false); |
| 1088 | } |
| 1089 | |
| 1090 | static int adv7511_probe(struct i2c_client *client, const struct i2c_device_id *id) |
| 1091 | { |
| 1092 | struct adv7511_state *state; |
| 1093 | struct adv7511_platform_data *pdata = client->dev.platform_data; |
| 1094 | struct v4l2_ctrl_handler *hdl; |
| 1095 | struct v4l2_subdev *sd; |
| 1096 | u8 chip_id[2]; |
| 1097 | int err = -EIO; |
| 1098 | |
| 1099 | /* Check if the adapter supports the needed features */ |
| 1100 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 1101 | return -EIO; |
| 1102 | |
| 1103 | state = devm_kzalloc(&client->dev, sizeof(struct adv7511_state), GFP_KERNEL); |
| 1104 | if (!state) |
| 1105 | return -ENOMEM; |
| 1106 | |
| 1107 | /* Platform data */ |
| 1108 | if (!pdata) { |
| 1109 | v4l_err(client, "No platform data!\n"); |
| 1110 | return -ENODEV; |
| 1111 | } |
| 1112 | memcpy(&state->pdata, pdata, sizeof(state->pdata)); |
| 1113 | |
| 1114 | sd = &state->sd; |
| 1115 | |
| 1116 | v4l2_dbg(1, debug, sd, "detecting adv7511 client on address 0x%x\n", |
| 1117 | client->addr << 1); |
| 1118 | |
| 1119 | v4l2_i2c_subdev_init(sd, client, &adv7511_ops); |
| 1120 | |
| 1121 | hdl = &state->hdl; |
| 1122 | v4l2_ctrl_handler_init(hdl, 10); |
| 1123 | /* add in ascending ID order */ |
| 1124 | state->hdmi_mode_ctrl = v4l2_ctrl_new_std_menu(hdl, &adv7511_ctrl_ops, |
| 1125 | V4L2_CID_DV_TX_MODE, V4L2_DV_TX_MODE_HDMI, |
| 1126 | 0, V4L2_DV_TX_MODE_DVI_D); |
| 1127 | state->hotplug_ctrl = v4l2_ctrl_new_std(hdl, NULL, |
| 1128 | V4L2_CID_DV_TX_HOTPLUG, 0, 1, 0, 0); |
| 1129 | state->rx_sense_ctrl = v4l2_ctrl_new_std(hdl, NULL, |
| 1130 | V4L2_CID_DV_TX_RXSENSE, 0, 1, 0, 0); |
| 1131 | state->have_edid0_ctrl = v4l2_ctrl_new_std(hdl, NULL, |
| 1132 | V4L2_CID_DV_TX_EDID_PRESENT, 0, 1, 0, 0); |
| 1133 | state->rgb_quantization_range_ctrl = |
| 1134 | v4l2_ctrl_new_std_menu(hdl, &adv7511_ctrl_ops, |
| 1135 | V4L2_CID_DV_TX_RGB_RANGE, V4L2_DV_RGB_RANGE_FULL, |
| 1136 | 0, V4L2_DV_RGB_RANGE_AUTO); |
| 1137 | sd->ctrl_handler = hdl; |
| 1138 | if (hdl->error) { |
| 1139 | err = hdl->error; |
| 1140 | goto err_hdl; |
| 1141 | } |
| 1142 | state->hdmi_mode_ctrl->is_private = true; |
| 1143 | state->hotplug_ctrl->is_private = true; |
| 1144 | state->rx_sense_ctrl->is_private = true; |
| 1145 | state->have_edid0_ctrl->is_private = true; |
| 1146 | state->rgb_quantization_range_ctrl->is_private = true; |
| 1147 | |
| 1148 | state->pad.flags = MEDIA_PAD_FL_SINK; |
| 1149 | err = media_entity_init(&sd->entity, 1, &state->pad, 0); |
| 1150 | if (err) |
| 1151 | goto err_hdl; |
| 1152 | |
| 1153 | /* EDID and CEC i2c addr */ |
| 1154 | state->i2c_edid_addr = state->pdata.i2c_edid << 1; |
| 1155 | state->i2c_cec_addr = state->pdata.i2c_cec << 1; |
| 1156 | |
| 1157 | state->chip_revision = adv7511_rd(sd, 0x0); |
| 1158 | chip_id[0] = adv7511_rd(sd, 0xf5); |
| 1159 | chip_id[1] = adv7511_rd(sd, 0xf6); |
| 1160 | if (chip_id[0] != 0x75 || chip_id[1] != 0x11) { |
| 1161 | v4l2_err(sd, "chip_id != 0x7511, read 0x%02x%02x\n", chip_id[0], chip_id[1]); |
| 1162 | err = -EIO; |
| 1163 | goto err_entity; |
| 1164 | } |
| 1165 | |
| 1166 | state->i2c_edid = i2c_new_dummy(client->adapter, state->i2c_edid_addr >> 1); |
| 1167 | if (state->i2c_edid == NULL) { |
| 1168 | v4l2_err(sd, "failed to register edid i2c client\n"); |
Wei Yongjun | f527b17 | 2013-09-11 10:07:58 -0300 | [diff] [blame] | 1169 | err = -ENOMEM; |
Hans Verkuil | 5a544cc | 2013-08-23 09:12:36 -0300 | [diff] [blame] | 1170 | goto err_entity; |
| 1171 | } |
| 1172 | |
| 1173 | adv7511_wr(sd, 0xe2, 0x01); /* power down cec section */ |
| 1174 | state->work_queue = create_singlethread_workqueue(sd->name); |
| 1175 | if (state->work_queue == NULL) { |
| 1176 | v4l2_err(sd, "could not create workqueue\n"); |
Wei Yongjun | f527b17 | 2013-09-11 10:07:58 -0300 | [diff] [blame] | 1177 | err = -ENOMEM; |
Hans Verkuil | 5a544cc | 2013-08-23 09:12:36 -0300 | [diff] [blame] | 1178 | goto err_unreg_cec; |
| 1179 | } |
| 1180 | |
| 1181 | INIT_DELAYED_WORK(&state->edid_handler, adv7511_edid_handler); |
| 1182 | |
| 1183 | adv7511_init_setup(sd); |
| 1184 | adv7511_set_isr(sd, true); |
| 1185 | adv7511_check_monitor_present_status(sd); |
| 1186 | |
| 1187 | v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name, |
| 1188 | client->addr << 1, client->adapter->name); |
| 1189 | return 0; |
| 1190 | |
| 1191 | err_unreg_cec: |
| 1192 | i2c_unregister_device(state->i2c_edid); |
| 1193 | err_entity: |
| 1194 | media_entity_cleanup(&sd->entity); |
| 1195 | err_hdl: |
| 1196 | v4l2_ctrl_handler_free(&state->hdl); |
| 1197 | return err; |
| 1198 | } |
| 1199 | |
| 1200 | /* ----------------------------------------------------------------------- */ |
| 1201 | |
| 1202 | static int adv7511_remove(struct i2c_client *client) |
| 1203 | { |
| 1204 | struct v4l2_subdev *sd = i2c_get_clientdata(client); |
| 1205 | struct adv7511_state *state = get_adv7511_state(sd); |
| 1206 | |
| 1207 | state->chip_revision = -1; |
| 1208 | |
| 1209 | v4l2_dbg(1, debug, sd, "%s removed @ 0x%x (%s)\n", client->name, |
| 1210 | client->addr << 1, client->adapter->name); |
| 1211 | |
| 1212 | adv7511_init_setup(sd); |
| 1213 | cancel_delayed_work(&state->edid_handler); |
| 1214 | i2c_unregister_device(state->i2c_edid); |
| 1215 | destroy_workqueue(state->work_queue); |
| 1216 | v4l2_device_unregister_subdev(sd); |
| 1217 | media_entity_cleanup(&sd->entity); |
| 1218 | v4l2_ctrl_handler_free(sd->ctrl_handler); |
| 1219 | return 0; |
| 1220 | } |
| 1221 | |
| 1222 | /* ----------------------------------------------------------------------- */ |
| 1223 | |
| 1224 | static struct i2c_device_id adv7511_id[] = { |
| 1225 | { "adv7511", 0 }, |
| 1226 | { } |
| 1227 | }; |
| 1228 | MODULE_DEVICE_TABLE(i2c, adv7511_id); |
| 1229 | |
| 1230 | static struct i2c_driver adv7511_driver = { |
| 1231 | .driver = { |
| 1232 | .owner = THIS_MODULE, |
| 1233 | .name = "adv7511", |
| 1234 | }, |
| 1235 | .probe = adv7511_probe, |
| 1236 | .remove = adv7511_remove, |
| 1237 | .id_table = adv7511_id, |
| 1238 | }; |
| 1239 | |
| 1240 | module_i2c_driver(adv7511_driver); |