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