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