[media] adv7842: i2c dummy clients registration
[linux-2.6/btrfs-unstable.git] / drivers / media / i2c / adv7842.c
blob108e1b02d6c8073ec405ca12b48ba0b870a724fe
1 /*
2 * adv7842 - Analog Devices ADV7842 video decoder driver
4 * Copyright 2013 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6 * This program is free software; you may redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
11 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
12 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
13 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
14 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
15 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
16 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17 * SOFTWARE.
22 * References (c = chapter, p = page):
23 * REF_01 - Analog devices, ADV7842,
24 * Register Settings Recommendations, Rev. 1.9, April 2011
25 * REF_02 - Analog devices, Software User Guide, UG-206,
26 * ADV7842 I2C Register Maps, Rev. 0, November 2010
27 * REF_03 - Analog devices, Hardware User Guide, UG-214,
28 * ADV7842 Fast Switching 2:1 HDMI 1.4 Receiver with 3D-Comb
29 * Decoder and Digitizer , Rev. 0, January 2011
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/slab.h>
36 #include <linux/i2c.h>
37 #include <linux/delay.h>
38 #include <linux/videodev2.h>
39 #include <linux/workqueue.h>
40 #include <linux/v4l2-dv-timings.h>
41 #include <media/v4l2-device.h>
42 #include <media/v4l2-ctrls.h>
43 #include <media/v4l2-dv-timings.h>
44 #include <media/adv7842.h>
46 static int debug;
47 module_param(debug, int, 0644);
48 MODULE_PARM_DESC(debug, "debug level (0-2)");
50 MODULE_DESCRIPTION("Analog Devices ADV7842 video decoder driver");
51 MODULE_AUTHOR("Hans Verkuil <hans.verkuil@cisco.com>");
52 MODULE_AUTHOR("Martin Bugge <marbugge@cisco.com>");
53 MODULE_LICENSE("GPL");
55 /* ADV7842 system clock frequency */
56 #define ADV7842_fsc (28636360)
59 **********************************************************************
61 * Arrays with configuration parameters for the ADV7842
63 **********************************************************************
66 struct adv7842_state {
67 struct adv7842_platform_data pdata;
68 struct v4l2_subdev sd;
69 struct media_pad pad;
70 struct v4l2_ctrl_handler hdl;
71 enum adv7842_mode mode;
72 struct v4l2_dv_timings timings;
73 enum adv7842_vid_std_select vid_std_select;
74 v4l2_std_id norm;
75 struct {
76 u8 edid[256];
77 u32 present;
78 } hdmi_edid;
79 struct {
80 u8 edid[256];
81 u32 present;
82 } vga_edid;
83 struct v4l2_fract aspect_ratio;
84 u32 rgb_quantization_range;
85 bool is_cea_format;
86 struct workqueue_struct *work_queues;
87 struct delayed_work delayed_work_enable_hotplug;
88 bool restart_stdi_once;
89 bool hdmi_port_a;
91 /* i2c clients */
92 struct i2c_client *i2c_sdp_io;
93 struct i2c_client *i2c_sdp;
94 struct i2c_client *i2c_cp;
95 struct i2c_client *i2c_vdp;
96 struct i2c_client *i2c_afe;
97 struct i2c_client *i2c_hdmi;
98 struct i2c_client *i2c_repeater;
99 struct i2c_client *i2c_edid;
100 struct i2c_client *i2c_infoframe;
101 struct i2c_client *i2c_cec;
102 struct i2c_client *i2c_avlink;
104 /* controls */
105 struct v4l2_ctrl *detect_tx_5v_ctrl;
106 struct v4l2_ctrl *analog_sampling_phase_ctrl;
107 struct v4l2_ctrl *free_run_color_ctrl_manual;
108 struct v4l2_ctrl *free_run_color_ctrl;
109 struct v4l2_ctrl *rgb_quantization_range_ctrl;
112 /* Unsupported timings. This device cannot support 720p30. */
113 static const struct v4l2_dv_timings adv7842_timings_exceptions[] = {
114 V4L2_DV_BT_CEA_1280X720P30,
118 static bool adv7842_check_dv_timings(const struct v4l2_dv_timings *t, void *hdl)
120 int i;
122 for (i = 0; adv7842_timings_exceptions[i].bt.width; i++)
123 if (v4l2_match_dv_timings(t, adv7842_timings_exceptions + i, 0))
124 return false;
125 return true;
128 struct adv7842_video_standards {
129 struct v4l2_dv_timings timings;
130 u8 vid_std;
131 u8 v_freq;
134 /* sorted by number of lines */
135 static const struct adv7842_video_standards adv7842_prim_mode_comp[] = {
136 /* { V4L2_DV_BT_CEA_720X480P59_94, 0x0a, 0x00 }, TODO flickering */
137 { V4L2_DV_BT_CEA_720X576P50, 0x0b, 0x00 },
138 { V4L2_DV_BT_CEA_1280X720P50, 0x19, 0x01 },
139 { V4L2_DV_BT_CEA_1280X720P60, 0x19, 0x00 },
140 { V4L2_DV_BT_CEA_1920X1080P24, 0x1e, 0x04 },
141 { V4L2_DV_BT_CEA_1920X1080P25, 0x1e, 0x03 },
142 { V4L2_DV_BT_CEA_1920X1080P30, 0x1e, 0x02 },
143 { V4L2_DV_BT_CEA_1920X1080P50, 0x1e, 0x01 },
144 { V4L2_DV_BT_CEA_1920X1080P60, 0x1e, 0x00 },
145 /* TODO add 1920x1080P60_RB (CVT timing) */
146 { },
149 /* sorted by number of lines */
150 static const struct adv7842_video_standards adv7842_prim_mode_gr[] = {
151 { V4L2_DV_BT_DMT_640X480P60, 0x08, 0x00 },
152 { V4L2_DV_BT_DMT_640X480P72, 0x09, 0x00 },
153 { V4L2_DV_BT_DMT_640X480P75, 0x0a, 0x00 },
154 { V4L2_DV_BT_DMT_640X480P85, 0x0b, 0x00 },
155 { V4L2_DV_BT_DMT_800X600P56, 0x00, 0x00 },
156 { V4L2_DV_BT_DMT_800X600P60, 0x01, 0x00 },
157 { V4L2_DV_BT_DMT_800X600P72, 0x02, 0x00 },
158 { V4L2_DV_BT_DMT_800X600P75, 0x03, 0x00 },
159 { V4L2_DV_BT_DMT_800X600P85, 0x04, 0x00 },
160 { V4L2_DV_BT_DMT_1024X768P60, 0x0c, 0x00 },
161 { V4L2_DV_BT_DMT_1024X768P70, 0x0d, 0x00 },
162 { V4L2_DV_BT_DMT_1024X768P75, 0x0e, 0x00 },
163 { V4L2_DV_BT_DMT_1024X768P85, 0x0f, 0x00 },
164 { V4L2_DV_BT_DMT_1280X1024P60, 0x05, 0x00 },
165 { V4L2_DV_BT_DMT_1280X1024P75, 0x06, 0x00 },
166 { V4L2_DV_BT_DMT_1360X768P60, 0x12, 0x00 },
167 { V4L2_DV_BT_DMT_1366X768P60, 0x13, 0x00 },
168 { V4L2_DV_BT_DMT_1400X1050P60, 0x14, 0x00 },
169 { V4L2_DV_BT_DMT_1400X1050P75, 0x15, 0x00 },
170 { V4L2_DV_BT_DMT_1600X1200P60, 0x16, 0x00 }, /* TODO not tested */
171 /* TODO add 1600X1200P60_RB (not a DMT timing) */
172 { V4L2_DV_BT_DMT_1680X1050P60, 0x18, 0x00 },
173 { V4L2_DV_BT_DMT_1920X1200P60_RB, 0x19, 0x00 }, /* TODO not tested */
174 { },
177 /* sorted by number of lines */
178 static const struct adv7842_video_standards adv7842_prim_mode_hdmi_comp[] = {
179 { V4L2_DV_BT_CEA_720X480P59_94, 0x0a, 0x00 },
180 { V4L2_DV_BT_CEA_720X576P50, 0x0b, 0x00 },
181 { V4L2_DV_BT_CEA_1280X720P50, 0x13, 0x01 },
182 { V4L2_DV_BT_CEA_1280X720P60, 0x13, 0x00 },
183 { V4L2_DV_BT_CEA_1920X1080P24, 0x1e, 0x04 },
184 { V4L2_DV_BT_CEA_1920X1080P25, 0x1e, 0x03 },
185 { V4L2_DV_BT_CEA_1920X1080P30, 0x1e, 0x02 },
186 { V4L2_DV_BT_CEA_1920X1080P50, 0x1e, 0x01 },
187 { V4L2_DV_BT_CEA_1920X1080P60, 0x1e, 0x00 },
188 { },
191 /* sorted by number of lines */
192 static const struct adv7842_video_standards adv7842_prim_mode_hdmi_gr[] = {
193 { V4L2_DV_BT_DMT_640X480P60, 0x08, 0x00 },
194 { V4L2_DV_BT_DMT_640X480P72, 0x09, 0x00 },
195 { V4L2_DV_BT_DMT_640X480P75, 0x0a, 0x00 },
196 { V4L2_DV_BT_DMT_640X480P85, 0x0b, 0x00 },
197 { V4L2_DV_BT_DMT_800X600P56, 0x00, 0x00 },
198 { V4L2_DV_BT_DMT_800X600P60, 0x01, 0x00 },
199 { V4L2_DV_BT_DMT_800X600P72, 0x02, 0x00 },
200 { V4L2_DV_BT_DMT_800X600P75, 0x03, 0x00 },
201 { V4L2_DV_BT_DMT_800X600P85, 0x04, 0x00 },
202 { V4L2_DV_BT_DMT_1024X768P60, 0x0c, 0x00 },
203 { V4L2_DV_BT_DMT_1024X768P70, 0x0d, 0x00 },
204 { V4L2_DV_BT_DMT_1024X768P75, 0x0e, 0x00 },
205 { V4L2_DV_BT_DMT_1024X768P85, 0x0f, 0x00 },
206 { V4L2_DV_BT_DMT_1280X1024P60, 0x05, 0x00 },
207 { V4L2_DV_BT_DMT_1280X1024P75, 0x06, 0x00 },
208 { },
211 /* ----------------------------------------------------------------------- */
213 static inline struct adv7842_state *to_state(struct v4l2_subdev *sd)
215 return container_of(sd, struct adv7842_state, sd);
218 static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
220 return &container_of(ctrl->handler, struct adv7842_state, hdl)->sd;
223 static inline unsigned hblanking(const struct v4l2_bt_timings *t)
225 return V4L2_DV_BT_BLANKING_WIDTH(t);
228 static inline unsigned htotal(const struct v4l2_bt_timings *t)
230 return V4L2_DV_BT_FRAME_WIDTH(t);
233 static inline unsigned vblanking(const struct v4l2_bt_timings *t)
235 return V4L2_DV_BT_BLANKING_HEIGHT(t);
238 static inline unsigned vtotal(const struct v4l2_bt_timings *t)
240 return V4L2_DV_BT_FRAME_HEIGHT(t);
244 /* ----------------------------------------------------------------------- */
246 static s32 adv_smbus_read_byte_data_check(struct i2c_client *client,
247 u8 command, bool check)
249 union i2c_smbus_data data;
251 if (!i2c_smbus_xfer(client->adapter, client->addr, client->flags,
252 I2C_SMBUS_READ, command,
253 I2C_SMBUS_BYTE_DATA, &data))
254 return data.byte;
255 if (check)
256 v4l_err(client, "error reading %02x, %02x\n",
257 client->addr, command);
258 return -EIO;
261 static s32 adv_smbus_read_byte_data(struct i2c_client *client, u8 command)
263 int i;
265 for (i = 0; i < 3; i++) {
266 int ret = adv_smbus_read_byte_data_check(client, command, true);
268 if (ret >= 0) {
269 if (i)
270 v4l_err(client, "read ok after %d retries\n", i);
271 return ret;
274 v4l_err(client, "read failed\n");
275 return -EIO;
278 static s32 adv_smbus_write_byte_data(struct i2c_client *client,
279 u8 command, u8 value)
281 union i2c_smbus_data data;
282 int err;
283 int i;
285 data.byte = value;
286 for (i = 0; i < 3; i++) {
287 err = i2c_smbus_xfer(client->adapter, client->addr,
288 client->flags,
289 I2C_SMBUS_WRITE, command,
290 I2C_SMBUS_BYTE_DATA, &data);
291 if (!err)
292 break;
294 if (err < 0)
295 v4l_err(client, "error writing %02x, %02x, %02x\n",
296 client->addr, command, value);
297 return err;
300 static void adv_smbus_write_byte_no_check(struct i2c_client *client,
301 u8 command, u8 value)
303 union i2c_smbus_data data;
304 data.byte = value;
306 i2c_smbus_xfer(client->adapter, client->addr,
307 client->flags,
308 I2C_SMBUS_WRITE, command,
309 I2C_SMBUS_BYTE_DATA, &data);
312 static s32 adv_smbus_write_i2c_block_data(struct i2c_client *client,
313 u8 command, unsigned length, const u8 *values)
315 union i2c_smbus_data data;
317 if (length > I2C_SMBUS_BLOCK_MAX)
318 length = I2C_SMBUS_BLOCK_MAX;
319 data.block[0] = length;
320 memcpy(data.block + 1, values, length);
321 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
322 I2C_SMBUS_WRITE, command,
323 I2C_SMBUS_I2C_BLOCK_DATA, &data);
326 /* ----------------------------------------------------------------------- */
328 static inline int io_read(struct v4l2_subdev *sd, u8 reg)
330 struct i2c_client *client = v4l2_get_subdevdata(sd);
332 return adv_smbus_read_byte_data(client, reg);
335 static inline int io_write(struct v4l2_subdev *sd, u8 reg, u8 val)
337 struct i2c_client *client = v4l2_get_subdevdata(sd);
339 return adv_smbus_write_byte_data(client, reg, val);
342 static inline int io_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
344 return io_write(sd, reg, (io_read(sd, reg) & mask) | val);
347 static inline int avlink_read(struct v4l2_subdev *sd, u8 reg)
349 struct adv7842_state *state = to_state(sd);
351 return adv_smbus_read_byte_data(state->i2c_avlink, reg);
354 static inline int avlink_write(struct v4l2_subdev *sd, u8 reg, u8 val)
356 struct adv7842_state *state = to_state(sd);
358 return adv_smbus_write_byte_data(state->i2c_avlink, reg, val);
361 static inline int cec_read(struct v4l2_subdev *sd, u8 reg)
363 struct adv7842_state *state = to_state(sd);
365 return adv_smbus_read_byte_data(state->i2c_cec, reg);
368 static inline int cec_write(struct v4l2_subdev *sd, u8 reg, u8 val)
370 struct adv7842_state *state = to_state(sd);
372 return adv_smbus_write_byte_data(state->i2c_cec, reg, val);
375 static inline int cec_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
377 return cec_write(sd, reg, (cec_read(sd, reg) & mask) | val);
380 static inline int infoframe_read(struct v4l2_subdev *sd, u8 reg)
382 struct adv7842_state *state = to_state(sd);
384 return adv_smbus_read_byte_data(state->i2c_infoframe, reg);
387 static inline int infoframe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
389 struct adv7842_state *state = to_state(sd);
391 return adv_smbus_write_byte_data(state->i2c_infoframe, reg, val);
394 static inline int sdp_io_read(struct v4l2_subdev *sd, u8 reg)
396 struct adv7842_state *state = to_state(sd);
398 return adv_smbus_read_byte_data(state->i2c_sdp_io, reg);
401 static inline int sdp_io_write(struct v4l2_subdev *sd, u8 reg, u8 val)
403 struct adv7842_state *state = to_state(sd);
405 return adv_smbus_write_byte_data(state->i2c_sdp_io, reg, val);
408 static inline int sdp_io_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
410 return sdp_io_write(sd, reg, (sdp_io_read(sd, reg) & mask) | val);
413 static inline int sdp_read(struct v4l2_subdev *sd, u8 reg)
415 struct adv7842_state *state = to_state(sd);
417 return adv_smbus_read_byte_data(state->i2c_sdp, reg);
420 static inline int sdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
422 struct adv7842_state *state = to_state(sd);
424 return adv_smbus_write_byte_data(state->i2c_sdp, reg, val);
427 static inline int sdp_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
429 return sdp_write(sd, reg, (sdp_read(sd, reg) & mask) | val);
432 static inline int afe_read(struct v4l2_subdev *sd, u8 reg)
434 struct adv7842_state *state = to_state(sd);
436 return adv_smbus_read_byte_data(state->i2c_afe, reg);
439 static inline int afe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
441 struct adv7842_state *state = to_state(sd);
443 return adv_smbus_write_byte_data(state->i2c_afe, reg, val);
446 static inline int afe_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
448 return afe_write(sd, reg, (afe_read(sd, reg) & mask) | val);
451 static inline int rep_read(struct v4l2_subdev *sd, u8 reg)
453 struct adv7842_state *state = to_state(sd);
455 return adv_smbus_read_byte_data(state->i2c_repeater, reg);
458 static inline int rep_write(struct v4l2_subdev *sd, u8 reg, u8 val)
460 struct adv7842_state *state = to_state(sd);
462 return adv_smbus_write_byte_data(state->i2c_repeater, reg, val);
465 static inline int rep_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
467 return rep_write(sd, reg, (rep_read(sd, reg) & mask) | val);
470 static inline int edid_read(struct v4l2_subdev *sd, u8 reg)
472 struct adv7842_state *state = to_state(sd);
474 return adv_smbus_read_byte_data(state->i2c_edid, reg);
477 static inline int edid_write(struct v4l2_subdev *sd, u8 reg, u8 val)
479 struct adv7842_state *state = to_state(sd);
481 return adv_smbus_write_byte_data(state->i2c_edid, reg, val);
484 static inline int hdmi_read(struct v4l2_subdev *sd, u8 reg)
486 struct adv7842_state *state = to_state(sd);
488 return adv_smbus_read_byte_data(state->i2c_hdmi, reg);
491 static inline int hdmi_write(struct v4l2_subdev *sd, u8 reg, u8 val)
493 struct adv7842_state *state = to_state(sd);
495 return adv_smbus_write_byte_data(state->i2c_hdmi, reg, val);
498 static inline int hdmi_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
500 return hdmi_write(sd, reg, (hdmi_read(sd, reg) & mask) | val);
503 static inline int cp_read(struct v4l2_subdev *sd, u8 reg)
505 struct adv7842_state *state = to_state(sd);
507 return adv_smbus_read_byte_data(state->i2c_cp, reg);
510 static inline int cp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
512 struct adv7842_state *state = to_state(sd);
514 return adv_smbus_write_byte_data(state->i2c_cp, reg, val);
517 static inline int cp_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
519 return cp_write(sd, reg, (cp_read(sd, reg) & mask) | val);
522 static inline int vdp_read(struct v4l2_subdev *sd, u8 reg)
524 struct adv7842_state *state = to_state(sd);
526 return adv_smbus_read_byte_data(state->i2c_vdp, reg);
529 static inline int vdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
531 struct adv7842_state *state = to_state(sd);
533 return adv_smbus_write_byte_data(state->i2c_vdp, reg, val);
536 static void main_reset(struct v4l2_subdev *sd)
538 struct i2c_client *client = v4l2_get_subdevdata(sd);
540 v4l2_dbg(1, debug, sd, "%s:\n", __func__);
542 adv_smbus_write_byte_no_check(client, 0xff, 0x80);
544 mdelay(5);
547 /* ----------------------------------------------------------------------- */
549 static inline bool is_digital_input(struct v4l2_subdev *sd)
551 struct adv7842_state *state = to_state(sd);
553 return state->mode == ADV7842_MODE_HDMI;
556 static const struct v4l2_dv_timings_cap adv7842_timings_cap_analog = {
557 .type = V4L2_DV_BT_656_1120,
558 /* keep this initialization for compatibility with GCC < 4.4.6 */
559 .reserved = { 0 },
560 V4L2_INIT_BT_TIMINGS(0, 1920, 0, 1200, 25000000, 170000000,
561 V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
562 V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT,
563 V4L2_DV_BT_CAP_PROGRESSIVE | V4L2_DV_BT_CAP_REDUCED_BLANKING |
564 V4L2_DV_BT_CAP_CUSTOM)
567 static const struct v4l2_dv_timings_cap adv7842_timings_cap_digital = {
568 .type = V4L2_DV_BT_656_1120,
569 /* keep this initialization for compatibility with GCC < 4.4.6 */
570 .reserved = { 0 },
571 V4L2_INIT_BT_TIMINGS(0, 1920, 0, 1200, 25000000, 225000000,
572 V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
573 V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT,
574 V4L2_DV_BT_CAP_PROGRESSIVE | V4L2_DV_BT_CAP_REDUCED_BLANKING |
575 V4L2_DV_BT_CAP_CUSTOM)
578 static inline const struct v4l2_dv_timings_cap *
579 adv7842_get_dv_timings_cap(struct v4l2_subdev *sd)
581 return is_digital_input(sd) ? &adv7842_timings_cap_digital :
582 &adv7842_timings_cap_analog;
585 /* ----------------------------------------------------------------------- */
587 static void adv7842_delayed_work_enable_hotplug(struct work_struct *work)
589 struct delayed_work *dwork = to_delayed_work(work);
590 struct adv7842_state *state = container_of(dwork,
591 struct adv7842_state, delayed_work_enable_hotplug);
592 struct v4l2_subdev *sd = &state->sd;
593 int present = state->hdmi_edid.present;
594 u8 mask = 0;
596 v4l2_dbg(2, debug, sd, "%s: enable hotplug on ports: 0x%x\n",
597 __func__, present);
599 if (present & (0x04 << ADV7842_EDID_PORT_A))
600 mask |= 0x20;
601 if (present & (0x04 << ADV7842_EDID_PORT_B))
602 mask |= 0x10;
603 io_write_and_or(sd, 0x20, 0xcf, mask);
606 static int edid_write_vga_segment(struct v4l2_subdev *sd)
608 struct i2c_client *client = v4l2_get_subdevdata(sd);
609 struct adv7842_state *state = to_state(sd);
610 const u8 *val = state->vga_edid.edid;
611 int err = 0;
612 int i;
614 v4l2_dbg(2, debug, sd, "%s: write EDID on VGA port\n", __func__);
616 /* HPA disable on port A and B */
617 io_write_and_or(sd, 0x20, 0xcf, 0x00);
619 /* Disable I2C access to internal EDID ram from VGA DDC port */
620 rep_write_and_or(sd, 0x7f, 0x7f, 0x00);
622 /* edid segment pointer '1' for VGA port */
623 rep_write_and_or(sd, 0x77, 0xef, 0x10);
625 for (i = 0; !err && i < 256; i += I2C_SMBUS_BLOCK_MAX)
626 err = adv_smbus_write_i2c_block_data(state->i2c_edid, i,
627 I2C_SMBUS_BLOCK_MAX, val + i);
628 if (err)
629 return err;
631 /* Calculates the checksums and enables I2C access
632 * to internal EDID ram from VGA DDC port.
634 rep_write_and_or(sd, 0x7f, 0x7f, 0x80);
636 for (i = 0; i < 1000; i++) {
637 if (rep_read(sd, 0x79) & 0x20)
638 break;
639 mdelay(1);
641 if (i == 1000) {
642 v4l_err(client, "error enabling edid on VGA port\n");
643 return -EIO;
646 /* enable hotplug after 200 ms */
647 queue_delayed_work(state->work_queues,
648 &state->delayed_work_enable_hotplug, HZ / 5);
650 return 0;
653 static int edid_spa_location(const u8 *edid)
655 u8 d;
658 * TODO, improve and update for other CEA extensions
659 * currently only for 1 segment (256 bytes),
660 * i.e. 1 extension block and CEA revision 3.
662 if ((edid[0x7e] != 1) ||
663 (edid[0x80] != 0x02) ||
664 (edid[0x81] != 0x03)) {
665 return -EINVAL;
668 * search Vendor Specific Data Block (tag 3)
670 d = edid[0x82] & 0x7f;
671 if (d > 4) {
672 int i = 0x84;
673 int end = 0x80 + d;
674 do {
675 u8 tag = edid[i]>>5;
676 u8 len = edid[i] & 0x1f;
678 if ((tag == 3) && (len >= 5))
679 return i + 4;
680 i += len + 1;
681 } while (i < end);
683 return -EINVAL;
686 static int edid_write_hdmi_segment(struct v4l2_subdev *sd, u8 port)
688 struct i2c_client *client = v4l2_get_subdevdata(sd);
689 struct adv7842_state *state = to_state(sd);
690 const u8 *val = state->hdmi_edid.edid;
691 int spa_loc = edid_spa_location(val);
692 int err = 0;
693 int i;
695 v4l2_dbg(2, debug, sd, "%s: write EDID on port %c (spa at 0x%x)\n",
696 __func__, (port == ADV7842_EDID_PORT_A) ? 'A' : 'B', spa_loc);
698 /* HPA disable on port A and B */
699 io_write_and_or(sd, 0x20, 0xcf, 0x00);
701 /* Disable I2C access to internal EDID ram from HDMI DDC ports */
702 rep_write_and_or(sd, 0x77, 0xf3, 0x00);
704 if (!state->hdmi_edid.present)
705 return 0;
707 /* edid segment pointer '0' for HDMI ports */
708 rep_write_and_or(sd, 0x77, 0xef, 0x00);
710 for (i = 0; !err && i < 256; i += I2C_SMBUS_BLOCK_MAX)
711 err = adv_smbus_write_i2c_block_data(state->i2c_edid, i,
712 I2C_SMBUS_BLOCK_MAX, val + i);
713 if (err)
714 return err;
716 if (spa_loc < 0)
717 spa_loc = 0xc0; /* Default value [REF_02, p. 199] */
719 if (port == ADV7842_EDID_PORT_A) {
720 rep_write(sd, 0x72, val[spa_loc]);
721 rep_write(sd, 0x73, val[spa_loc + 1]);
722 } else {
723 rep_write(sd, 0x74, val[spa_loc]);
724 rep_write(sd, 0x75, val[spa_loc + 1]);
726 rep_write(sd, 0x76, spa_loc & 0xff);
727 rep_write_and_or(sd, 0x77, 0xbf, (spa_loc >> 2) & 0x40);
729 /* Calculates the checksums and enables I2C access to internal
730 * EDID ram from HDMI DDC ports
732 rep_write_and_or(sd, 0x77, 0xf3, state->hdmi_edid.present);
734 for (i = 0; i < 1000; i++) {
735 if (rep_read(sd, 0x7d) & state->hdmi_edid.present)
736 break;
737 mdelay(1);
739 if (i == 1000) {
740 v4l_err(client, "error enabling edid on port %c\n",
741 (port == ADV7842_EDID_PORT_A) ? 'A' : 'B');
742 return -EIO;
745 /* enable hotplug after 200 ms */
746 queue_delayed_work(state->work_queues,
747 &state->delayed_work_enable_hotplug, HZ / 5);
749 return 0;
752 /* ----------------------------------------------------------------------- */
754 #ifdef CONFIG_VIDEO_ADV_DEBUG
755 static void adv7842_inv_register(struct v4l2_subdev *sd)
757 v4l2_info(sd, "0x000-0x0ff: IO Map\n");
758 v4l2_info(sd, "0x100-0x1ff: AVLink Map\n");
759 v4l2_info(sd, "0x200-0x2ff: CEC Map\n");
760 v4l2_info(sd, "0x300-0x3ff: InfoFrame Map\n");
761 v4l2_info(sd, "0x400-0x4ff: SDP_IO Map\n");
762 v4l2_info(sd, "0x500-0x5ff: SDP Map\n");
763 v4l2_info(sd, "0x600-0x6ff: AFE Map\n");
764 v4l2_info(sd, "0x700-0x7ff: Repeater Map\n");
765 v4l2_info(sd, "0x800-0x8ff: EDID Map\n");
766 v4l2_info(sd, "0x900-0x9ff: HDMI Map\n");
767 v4l2_info(sd, "0xa00-0xaff: CP Map\n");
768 v4l2_info(sd, "0xb00-0xbff: VDP Map\n");
771 static int adv7842_g_register(struct v4l2_subdev *sd,
772 struct v4l2_dbg_register *reg)
774 reg->size = 1;
775 switch (reg->reg >> 8) {
776 case 0:
777 reg->val = io_read(sd, reg->reg & 0xff);
778 break;
779 case 1:
780 reg->val = avlink_read(sd, reg->reg & 0xff);
781 break;
782 case 2:
783 reg->val = cec_read(sd, reg->reg & 0xff);
784 break;
785 case 3:
786 reg->val = infoframe_read(sd, reg->reg & 0xff);
787 break;
788 case 4:
789 reg->val = sdp_io_read(sd, reg->reg & 0xff);
790 break;
791 case 5:
792 reg->val = sdp_read(sd, reg->reg & 0xff);
793 break;
794 case 6:
795 reg->val = afe_read(sd, reg->reg & 0xff);
796 break;
797 case 7:
798 reg->val = rep_read(sd, reg->reg & 0xff);
799 break;
800 case 8:
801 reg->val = edid_read(sd, reg->reg & 0xff);
802 break;
803 case 9:
804 reg->val = hdmi_read(sd, reg->reg & 0xff);
805 break;
806 case 0xa:
807 reg->val = cp_read(sd, reg->reg & 0xff);
808 break;
809 case 0xb:
810 reg->val = vdp_read(sd, reg->reg & 0xff);
811 break;
812 default:
813 v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
814 adv7842_inv_register(sd);
815 break;
817 return 0;
820 static int adv7842_s_register(struct v4l2_subdev *sd,
821 const struct v4l2_dbg_register *reg)
823 u8 val = reg->val & 0xff;
825 switch (reg->reg >> 8) {
826 case 0:
827 io_write(sd, reg->reg & 0xff, val);
828 break;
829 case 1:
830 avlink_write(sd, reg->reg & 0xff, val);
831 break;
832 case 2:
833 cec_write(sd, reg->reg & 0xff, val);
834 break;
835 case 3:
836 infoframe_write(sd, reg->reg & 0xff, val);
837 break;
838 case 4:
839 sdp_io_write(sd, reg->reg & 0xff, val);
840 break;
841 case 5:
842 sdp_write(sd, reg->reg & 0xff, val);
843 break;
844 case 6:
845 afe_write(sd, reg->reg & 0xff, val);
846 break;
847 case 7:
848 rep_write(sd, reg->reg & 0xff, val);
849 break;
850 case 8:
851 edid_write(sd, reg->reg & 0xff, val);
852 break;
853 case 9:
854 hdmi_write(sd, reg->reg & 0xff, val);
855 break;
856 case 0xa:
857 cp_write(sd, reg->reg & 0xff, val);
858 break;
859 case 0xb:
860 vdp_write(sd, reg->reg & 0xff, val);
861 break;
862 default:
863 v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
864 adv7842_inv_register(sd);
865 break;
867 return 0;
869 #endif
871 static int adv7842_s_detect_tx_5v_ctrl(struct v4l2_subdev *sd)
873 struct adv7842_state *state = to_state(sd);
874 int prev = v4l2_ctrl_g_ctrl(state->detect_tx_5v_ctrl);
875 u8 reg_io_6f = io_read(sd, 0x6f);
876 int val = 0;
878 if (reg_io_6f & 0x02)
879 val |= 1; /* port A */
880 if (reg_io_6f & 0x01)
881 val |= 2; /* port B */
883 v4l2_dbg(1, debug, sd, "%s: 0x%x -> 0x%x\n", __func__, prev, val);
885 if (val != prev)
886 return v4l2_ctrl_s_ctrl(state->detect_tx_5v_ctrl, val);
887 return 0;
890 static int find_and_set_predefined_video_timings(struct v4l2_subdev *sd,
891 u8 prim_mode,
892 const struct adv7842_video_standards *predef_vid_timings,
893 const struct v4l2_dv_timings *timings)
895 int i;
897 for (i = 0; predef_vid_timings[i].timings.bt.width; i++) {
898 if (!v4l2_match_dv_timings(timings, &predef_vid_timings[i].timings,
899 is_digital_input(sd) ? 250000 : 1000000))
900 continue;
901 /* video std */
902 io_write(sd, 0x00, predef_vid_timings[i].vid_std);
903 /* v_freq and prim mode */
904 io_write(sd, 0x01, (predef_vid_timings[i].v_freq << 4) + prim_mode);
905 return 0;
908 return -1;
911 static int configure_predefined_video_timings(struct v4l2_subdev *sd,
912 struct v4l2_dv_timings *timings)
914 struct adv7842_state *state = to_state(sd);
915 int err;
917 v4l2_dbg(1, debug, sd, "%s\n", __func__);
919 /* reset to default values */
920 io_write(sd, 0x16, 0x43);
921 io_write(sd, 0x17, 0x5a);
922 /* disable embedded syncs for auto graphics mode */
923 cp_write_and_or(sd, 0x81, 0xef, 0x00);
924 cp_write(sd, 0x26, 0x00);
925 cp_write(sd, 0x27, 0x00);
926 cp_write(sd, 0x28, 0x00);
927 cp_write(sd, 0x29, 0x00);
928 cp_write(sd, 0x8f, 0x40);
929 cp_write(sd, 0x90, 0x00);
930 cp_write(sd, 0xa5, 0x00);
931 cp_write(sd, 0xa6, 0x00);
932 cp_write(sd, 0xa7, 0x00);
933 cp_write(sd, 0xab, 0x00);
934 cp_write(sd, 0xac, 0x00);
936 switch (state->mode) {
937 case ADV7842_MODE_COMP:
938 case ADV7842_MODE_RGB:
939 err = find_and_set_predefined_video_timings(sd,
940 0x01, adv7842_prim_mode_comp, timings);
941 if (err)
942 err = find_and_set_predefined_video_timings(sd,
943 0x02, adv7842_prim_mode_gr, timings);
944 break;
945 case ADV7842_MODE_HDMI:
946 err = find_and_set_predefined_video_timings(sd,
947 0x05, adv7842_prim_mode_hdmi_comp, timings);
948 if (err)
949 err = find_and_set_predefined_video_timings(sd,
950 0x06, adv7842_prim_mode_hdmi_gr, timings);
951 break;
952 default:
953 v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
954 __func__, state->mode);
955 err = -1;
956 break;
960 return err;
963 static void configure_custom_video_timings(struct v4l2_subdev *sd,
964 const struct v4l2_bt_timings *bt)
966 struct adv7842_state *state = to_state(sd);
967 struct i2c_client *client = v4l2_get_subdevdata(sd);
968 u32 width = htotal(bt);
969 u32 height = vtotal(bt);
970 u16 cp_start_sav = bt->hsync + bt->hbackporch - 4;
971 u16 cp_start_eav = width - bt->hfrontporch;
972 u16 cp_start_vbi = height - bt->vfrontporch + 1;
973 u16 cp_end_vbi = bt->vsync + bt->vbackporch + 1;
974 u16 ch1_fr_ll = (((u32)bt->pixelclock / 100) > 0) ?
975 ((width * (ADV7842_fsc / 100)) / ((u32)bt->pixelclock / 100)) : 0;
976 const u8 pll[2] = {
977 0xc0 | ((width >> 8) & 0x1f),
978 width & 0xff
981 v4l2_dbg(2, debug, sd, "%s\n", __func__);
983 switch (state->mode) {
984 case ADV7842_MODE_COMP:
985 case ADV7842_MODE_RGB:
986 /* auto graphics */
987 io_write(sd, 0x00, 0x07); /* video std */
988 io_write(sd, 0x01, 0x02); /* prim mode */
989 /* enable embedded syncs for auto graphics mode */
990 cp_write_and_or(sd, 0x81, 0xef, 0x10);
992 /* Should only be set in auto-graphics mode [REF_02, p. 91-92] */
993 /* setup PLL_DIV_MAN_EN and PLL_DIV_RATIO */
994 /* IO-map reg. 0x16 and 0x17 should be written in sequence */
995 if (adv_smbus_write_i2c_block_data(client, 0x16, 2, pll)) {
996 v4l2_err(sd, "writing to reg 0x16 and 0x17 failed\n");
997 break;
1000 /* active video - horizontal timing */
1001 cp_write(sd, 0x26, (cp_start_sav >> 8) & 0xf);
1002 cp_write(sd, 0x27, (cp_start_sav & 0xff));
1003 cp_write(sd, 0x28, (cp_start_eav >> 8) & 0xf);
1004 cp_write(sd, 0x29, (cp_start_eav & 0xff));
1006 /* active video - vertical timing */
1007 cp_write(sd, 0xa5, (cp_start_vbi >> 4) & 0xff);
1008 cp_write(sd, 0xa6, ((cp_start_vbi & 0xf) << 4) |
1009 ((cp_end_vbi >> 8) & 0xf));
1010 cp_write(sd, 0xa7, cp_end_vbi & 0xff);
1011 break;
1012 case ADV7842_MODE_HDMI:
1013 /* set default prim_mode/vid_std for HDMI
1014 according to [REF_03, c. 4.2] */
1015 io_write(sd, 0x00, 0x02); /* video std */
1016 io_write(sd, 0x01, 0x06); /* prim mode */
1017 break;
1018 default:
1019 v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
1020 __func__, state->mode);
1021 break;
1024 cp_write(sd, 0x8f, (ch1_fr_ll >> 8) & 0x7);
1025 cp_write(sd, 0x90, ch1_fr_ll & 0xff);
1026 cp_write(sd, 0xab, (height >> 4) & 0xff);
1027 cp_write(sd, 0xac, (height & 0x0f) << 4);
1030 static void set_rgb_quantization_range(struct v4l2_subdev *sd)
1032 struct adv7842_state *state = to_state(sd);
1034 v4l2_dbg(2, debug, sd, "%s: rgb_quantization_range = %d\n",
1035 __func__, state->rgb_quantization_range);
1037 switch (state->rgb_quantization_range) {
1038 case V4L2_DV_RGB_RANGE_AUTO:
1039 if (state->mode == ADV7842_MODE_RGB) {
1040 /* Receiving analog RGB signal
1041 * Set RGB full range (0-255) */
1042 io_write_and_or(sd, 0x02, 0x0f, 0x10);
1043 break;
1046 if (state->mode == ADV7842_MODE_COMP) {
1047 /* Receiving analog YPbPr signal
1048 * Set automode */
1049 io_write_and_or(sd, 0x02, 0x0f, 0xf0);
1050 break;
1053 if (hdmi_read(sd, 0x05) & 0x80) {
1054 /* Receiving HDMI signal
1055 * Set automode */
1056 io_write_and_or(sd, 0x02, 0x0f, 0xf0);
1057 break;
1060 /* Receiving DVI-D signal
1061 * ADV7842 selects RGB limited range regardless of
1062 * input format (CE/IT) in automatic mode */
1063 if (state->timings.bt.standards & V4L2_DV_BT_STD_CEA861) {
1064 /* RGB limited range (16-235) */
1065 io_write_and_or(sd, 0x02, 0x0f, 0x00);
1066 } else {
1067 /* RGB full range (0-255) */
1068 io_write_and_or(sd, 0x02, 0x0f, 0x10);
1070 break;
1071 case V4L2_DV_RGB_RANGE_LIMITED:
1072 if (state->mode == ADV7842_MODE_COMP) {
1073 /* YCrCb limited range (16-235) */
1074 io_write_and_or(sd, 0x02, 0x0f, 0x20);
1075 } else {
1076 /* RGB limited range (16-235) */
1077 io_write_and_or(sd, 0x02, 0x0f, 0x00);
1079 break;
1080 case V4L2_DV_RGB_RANGE_FULL:
1081 if (state->mode == ADV7842_MODE_COMP) {
1082 /* YCrCb full range (0-255) */
1083 io_write_and_or(sd, 0x02, 0x0f, 0x60);
1084 } else {
1085 /* RGB full range (0-255) */
1086 io_write_and_or(sd, 0x02, 0x0f, 0x10);
1088 break;
1092 static int adv7842_s_ctrl(struct v4l2_ctrl *ctrl)
1094 struct v4l2_subdev *sd = to_sd(ctrl);
1095 struct adv7842_state *state = to_state(sd);
1097 /* TODO SDP ctrls
1098 contrast/brightness/hue/free run is acting a bit strange,
1099 not sure if sdp csc is correct.
1101 switch (ctrl->id) {
1102 /* standard ctrls */
1103 case V4L2_CID_BRIGHTNESS:
1104 cp_write(sd, 0x3c, ctrl->val);
1105 sdp_write(sd, 0x14, ctrl->val);
1106 /* ignore lsb sdp 0x17[3:2] */
1107 return 0;
1108 case V4L2_CID_CONTRAST:
1109 cp_write(sd, 0x3a, ctrl->val);
1110 sdp_write(sd, 0x13, ctrl->val);
1111 /* ignore lsb sdp 0x17[1:0] */
1112 return 0;
1113 case V4L2_CID_SATURATION:
1114 cp_write(sd, 0x3b, ctrl->val);
1115 sdp_write(sd, 0x15, ctrl->val);
1116 /* ignore lsb sdp 0x17[5:4] */
1117 return 0;
1118 case V4L2_CID_HUE:
1119 cp_write(sd, 0x3d, ctrl->val);
1120 sdp_write(sd, 0x16, ctrl->val);
1121 /* ignore lsb sdp 0x17[7:6] */
1122 return 0;
1123 /* custom ctrls */
1124 case V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE:
1125 afe_write(sd, 0xc8, ctrl->val);
1126 return 0;
1127 case V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL:
1128 cp_write_and_or(sd, 0xbf, ~0x04, (ctrl->val << 2));
1129 sdp_write_and_or(sd, 0xdd, ~0x04, (ctrl->val << 2));
1130 return 0;
1131 case V4L2_CID_ADV_RX_FREE_RUN_COLOR: {
1132 u8 R = (ctrl->val & 0xff0000) >> 16;
1133 u8 G = (ctrl->val & 0x00ff00) >> 8;
1134 u8 B = (ctrl->val & 0x0000ff);
1135 /* RGB -> YUV, numerical approximation */
1136 int Y = 66 * R + 129 * G + 25 * B;
1137 int U = -38 * R - 74 * G + 112 * B;
1138 int V = 112 * R - 94 * G - 18 * B;
1140 /* Scale down to 8 bits with rounding */
1141 Y = (Y + 128) >> 8;
1142 U = (U + 128) >> 8;
1143 V = (V + 128) >> 8;
1144 /* make U,V positive */
1145 Y += 16;
1146 U += 128;
1147 V += 128;
1149 v4l2_dbg(1, debug, sd, "R %x, G %x, B %x\n", R, G, B);
1150 v4l2_dbg(1, debug, sd, "Y %x, U %x, V %x\n", Y, U, V);
1152 /* CP */
1153 cp_write(sd, 0xc1, R);
1154 cp_write(sd, 0xc0, G);
1155 cp_write(sd, 0xc2, B);
1156 /* SDP */
1157 sdp_write(sd, 0xde, Y);
1158 sdp_write(sd, 0xdf, (V & 0xf0) | ((U >> 4) & 0x0f));
1159 return 0;
1161 case V4L2_CID_DV_RX_RGB_RANGE:
1162 state->rgb_quantization_range = ctrl->val;
1163 set_rgb_quantization_range(sd);
1164 return 0;
1166 return -EINVAL;
1169 static inline bool no_power(struct v4l2_subdev *sd)
1171 return io_read(sd, 0x0c) & 0x24;
1174 static inline bool no_cp_signal(struct v4l2_subdev *sd)
1176 return ((cp_read(sd, 0xb5) & 0xd0) != 0xd0) || !(cp_read(sd, 0xb1) & 0x80);
1179 static inline bool is_hdmi(struct v4l2_subdev *sd)
1181 return hdmi_read(sd, 0x05) & 0x80;
1184 static int adv7842_g_input_status(struct v4l2_subdev *sd, u32 *status)
1186 struct adv7842_state *state = to_state(sd);
1188 *status = 0;
1190 if (io_read(sd, 0x0c) & 0x24)
1191 *status |= V4L2_IN_ST_NO_POWER;
1193 if (state->mode == ADV7842_MODE_SDP) {
1194 /* status from SDP block */
1195 if (!(sdp_read(sd, 0x5A) & 0x01))
1196 *status |= V4L2_IN_ST_NO_SIGNAL;
1198 v4l2_dbg(1, debug, sd, "%s: SDP status = 0x%x\n",
1199 __func__, *status);
1200 return 0;
1202 /* status from CP block */
1203 if ((cp_read(sd, 0xb5) & 0xd0) != 0xd0 ||
1204 !(cp_read(sd, 0xb1) & 0x80))
1205 /* TODO channel 2 */
1206 *status |= V4L2_IN_ST_NO_SIGNAL;
1208 if (is_digital_input(sd) && ((io_read(sd, 0x74) & 0x03) != 0x03))
1209 *status |= V4L2_IN_ST_NO_SIGNAL;
1211 v4l2_dbg(1, debug, sd, "%s: CP status = 0x%x\n",
1212 __func__, *status);
1214 return 0;
1217 struct stdi_readback {
1218 u16 bl, lcf, lcvs;
1219 u8 hs_pol, vs_pol;
1220 bool interlaced;
1223 static int stdi2dv_timings(struct v4l2_subdev *sd,
1224 struct stdi_readback *stdi,
1225 struct v4l2_dv_timings *timings)
1227 struct adv7842_state *state = to_state(sd);
1228 u32 hfreq = (ADV7842_fsc * 8) / stdi->bl;
1229 u32 pix_clk;
1230 int i;
1232 for (i = 0; v4l2_dv_timings_presets[i].bt.width; i++) {
1233 const struct v4l2_bt_timings *bt = &v4l2_dv_timings_presets[i].bt;
1235 if (!v4l2_valid_dv_timings(&v4l2_dv_timings_presets[i],
1236 adv7842_get_dv_timings_cap(sd),
1237 adv7842_check_dv_timings, NULL))
1238 continue;
1239 if (vtotal(bt) != stdi->lcf + 1)
1240 continue;
1241 if (bt->vsync != stdi->lcvs)
1242 continue;
1244 pix_clk = hfreq * htotal(bt);
1246 if ((pix_clk < bt->pixelclock + 1000000) &&
1247 (pix_clk > bt->pixelclock - 1000000)) {
1248 *timings = v4l2_dv_timings_presets[i];
1249 return 0;
1253 if (v4l2_detect_cvt(stdi->lcf + 1, hfreq, stdi->lcvs,
1254 (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
1255 (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
1256 timings))
1257 return 0;
1258 if (v4l2_detect_gtf(stdi->lcf + 1, hfreq, stdi->lcvs,
1259 (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
1260 (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
1261 state->aspect_ratio, timings))
1262 return 0;
1264 v4l2_dbg(2, debug, sd,
1265 "%s: No format candidate found for lcvs = %d, lcf=%d, bl = %d, %chsync, %cvsync\n",
1266 __func__, stdi->lcvs, stdi->lcf, stdi->bl,
1267 stdi->hs_pol, stdi->vs_pol);
1268 return -1;
1271 static int read_stdi(struct v4l2_subdev *sd, struct stdi_readback *stdi)
1273 u32 status;
1275 adv7842_g_input_status(sd, &status);
1276 if (status & V4L2_IN_ST_NO_SIGNAL) {
1277 v4l2_dbg(2, debug, sd, "%s: no signal\n", __func__);
1278 return -ENOLINK;
1281 stdi->bl = ((cp_read(sd, 0xb1) & 0x3f) << 8) | cp_read(sd, 0xb2);
1282 stdi->lcf = ((cp_read(sd, 0xb3) & 0x7) << 8) | cp_read(sd, 0xb4);
1283 stdi->lcvs = cp_read(sd, 0xb3) >> 3;
1285 if ((cp_read(sd, 0xb5) & 0x80) && ((cp_read(sd, 0xb5) & 0x03) == 0x01)) {
1286 stdi->hs_pol = ((cp_read(sd, 0xb5) & 0x10) ?
1287 ((cp_read(sd, 0xb5) & 0x08) ? '+' : '-') : 'x');
1288 stdi->vs_pol = ((cp_read(sd, 0xb5) & 0x40) ?
1289 ((cp_read(sd, 0xb5) & 0x20) ? '+' : '-') : 'x');
1290 } else {
1291 stdi->hs_pol = 'x';
1292 stdi->vs_pol = 'x';
1294 stdi->interlaced = (cp_read(sd, 0xb1) & 0x40) ? true : false;
1296 if (stdi->lcf < 239 || stdi->bl < 8 || stdi->bl == 0x3fff) {
1297 v4l2_dbg(2, debug, sd, "%s: invalid signal\n", __func__);
1298 return -ENOLINK;
1301 v4l2_dbg(2, debug, sd,
1302 "%s: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, %chsync, %cvsync, %s\n",
1303 __func__, stdi->lcf, stdi->bl, stdi->lcvs,
1304 stdi->hs_pol, stdi->vs_pol,
1305 stdi->interlaced ? "interlaced" : "progressive");
1307 return 0;
1310 static int adv7842_enum_dv_timings(struct v4l2_subdev *sd,
1311 struct v4l2_enum_dv_timings *timings)
1313 return v4l2_enum_dv_timings_cap(timings,
1314 adv7842_get_dv_timings_cap(sd), adv7842_check_dv_timings, NULL);
1317 static int adv7842_dv_timings_cap(struct v4l2_subdev *sd,
1318 struct v4l2_dv_timings_cap *cap)
1320 *cap = *adv7842_get_dv_timings_cap(sd);
1321 return 0;
1324 /* Fill the optional fields .standards and .flags in struct v4l2_dv_timings
1325 if the format is listed in adv7842_timings[] */
1326 static void adv7842_fill_optional_dv_timings_fields(struct v4l2_subdev *sd,
1327 struct v4l2_dv_timings *timings)
1329 v4l2_find_dv_timings_cap(timings, adv7842_get_dv_timings_cap(sd),
1330 is_digital_input(sd) ? 250000 : 1000000,
1331 adv7842_check_dv_timings, NULL);
1334 static int adv7842_query_dv_timings(struct v4l2_subdev *sd,
1335 struct v4l2_dv_timings *timings)
1337 struct adv7842_state *state = to_state(sd);
1338 struct v4l2_bt_timings *bt = &timings->bt;
1339 struct stdi_readback stdi = { 0 };
1341 v4l2_dbg(1, debug, sd, "%s:\n", __func__);
1343 /* SDP block */
1344 if (state->mode == ADV7842_MODE_SDP)
1345 return -ENODATA;
1347 /* read STDI */
1348 if (read_stdi(sd, &stdi)) {
1349 state->restart_stdi_once = true;
1350 v4l2_dbg(1, debug, sd, "%s: no valid signal\n", __func__);
1351 return -ENOLINK;
1353 bt->interlaced = stdi.interlaced ?
1354 V4L2_DV_INTERLACED : V4L2_DV_PROGRESSIVE;
1356 if (is_digital_input(sd)) {
1357 uint32_t freq;
1359 timings->type = V4L2_DV_BT_656_1120;
1361 bt->width = (hdmi_read(sd, 0x07) & 0x0f) * 256 + hdmi_read(sd, 0x08);
1362 bt->height = (hdmi_read(sd, 0x09) & 0x0f) * 256 + hdmi_read(sd, 0x0a);
1363 freq = (hdmi_read(sd, 0x06) * 1000000) +
1364 ((hdmi_read(sd, 0x3b) & 0x30) >> 4) * 250000;
1366 if (is_hdmi(sd)) {
1367 /* adjust for deep color mode */
1368 freq = freq * 8 / (((hdmi_read(sd, 0x0b) & 0xc0) >> 5) + 8);
1370 bt->pixelclock = freq;
1371 bt->hfrontporch = (hdmi_read(sd, 0x20) & 0x03) * 256 +
1372 hdmi_read(sd, 0x21);
1373 bt->hsync = (hdmi_read(sd, 0x22) & 0x03) * 256 +
1374 hdmi_read(sd, 0x23);
1375 bt->hbackporch = (hdmi_read(sd, 0x24) & 0x03) * 256 +
1376 hdmi_read(sd, 0x25);
1377 bt->vfrontporch = ((hdmi_read(sd, 0x2a) & 0x1f) * 256 +
1378 hdmi_read(sd, 0x2b)) / 2;
1379 bt->vsync = ((hdmi_read(sd, 0x2e) & 0x1f) * 256 +
1380 hdmi_read(sd, 0x2f)) / 2;
1381 bt->vbackporch = ((hdmi_read(sd, 0x32) & 0x1f) * 256 +
1382 hdmi_read(sd, 0x33)) / 2;
1383 bt->polarities = ((hdmi_read(sd, 0x05) & 0x10) ? V4L2_DV_VSYNC_POS_POL : 0) |
1384 ((hdmi_read(sd, 0x05) & 0x20) ? V4L2_DV_HSYNC_POS_POL : 0);
1385 if (bt->interlaced == V4L2_DV_INTERLACED) {
1386 bt->height += (hdmi_read(sd, 0x0b) & 0x0f) * 256 +
1387 hdmi_read(sd, 0x0c);
1388 bt->il_vfrontporch = ((hdmi_read(sd, 0x2c) & 0x1f) * 256 +
1389 hdmi_read(sd, 0x2d)) / 2;
1390 bt->il_vsync = ((hdmi_read(sd, 0x30) & 0x1f) * 256 +
1391 hdmi_read(sd, 0x31)) / 2;
1392 bt->vbackporch = ((hdmi_read(sd, 0x34) & 0x1f) * 256 +
1393 hdmi_read(sd, 0x35)) / 2;
1395 adv7842_fill_optional_dv_timings_fields(sd, timings);
1396 } else {
1397 /* find format
1398 * Since LCVS values are inaccurate [REF_03, p. 339-340],
1399 * stdi2dv_timings() is called with lcvs +-1 if the first attempt fails.
1401 if (!stdi2dv_timings(sd, &stdi, timings))
1402 goto found;
1403 stdi.lcvs += 1;
1404 v4l2_dbg(1, debug, sd, "%s: lcvs + 1 = %d\n", __func__, stdi.lcvs);
1405 if (!stdi2dv_timings(sd, &stdi, timings))
1406 goto found;
1407 stdi.lcvs -= 2;
1408 v4l2_dbg(1, debug, sd, "%s: lcvs - 1 = %d\n", __func__, stdi.lcvs);
1409 if (stdi2dv_timings(sd, &stdi, timings)) {
1411 * The STDI block may measure wrong values, especially
1412 * for lcvs and lcf. If the driver can not find any
1413 * valid timing, the STDI block is restarted to measure
1414 * the video timings again. The function will return an
1415 * error, but the restart of STDI will generate a new
1416 * STDI interrupt and the format detection process will
1417 * restart.
1419 if (state->restart_stdi_once) {
1420 v4l2_dbg(1, debug, sd, "%s: restart STDI\n", __func__);
1421 /* TODO restart STDI for Sync Channel 2 */
1422 /* enter one-shot mode */
1423 cp_write_and_or(sd, 0x86, 0xf9, 0x00);
1424 /* trigger STDI restart */
1425 cp_write_and_or(sd, 0x86, 0xf9, 0x04);
1426 /* reset to continuous mode */
1427 cp_write_and_or(sd, 0x86, 0xf9, 0x02);
1428 state->restart_stdi_once = false;
1429 return -ENOLINK;
1431 v4l2_dbg(1, debug, sd, "%s: format not supported\n", __func__);
1432 return -ERANGE;
1434 state->restart_stdi_once = true;
1436 found:
1438 if (debug > 1)
1439 v4l2_print_dv_timings(sd->name, "adv7842_query_dv_timings:",
1440 timings, true);
1441 return 0;
1444 static int adv7842_s_dv_timings(struct v4l2_subdev *sd,
1445 struct v4l2_dv_timings *timings)
1447 struct adv7842_state *state = to_state(sd);
1448 struct v4l2_bt_timings *bt;
1449 int err;
1451 v4l2_dbg(1, debug, sd, "%s:\n", __func__);
1453 if (state->mode == ADV7842_MODE_SDP)
1454 return -ENODATA;
1456 bt = &timings->bt;
1458 if (!v4l2_valid_dv_timings(timings, adv7842_get_dv_timings_cap(sd),
1459 adv7842_check_dv_timings, NULL))
1460 return -ERANGE;
1462 adv7842_fill_optional_dv_timings_fields(sd, timings);
1464 state->timings = *timings;
1466 cp_write(sd, 0x91, bt->interlaced ? 0x40 : 0x00);
1468 /* Use prim_mode and vid_std when available */
1469 err = configure_predefined_video_timings(sd, timings);
1470 if (err) {
1471 /* custom settings when the video format
1472 does not have prim_mode/vid_std */
1473 configure_custom_video_timings(sd, bt);
1476 set_rgb_quantization_range(sd);
1479 if (debug > 1)
1480 v4l2_print_dv_timings(sd->name, "adv7842_s_dv_timings: ",
1481 timings, true);
1482 return 0;
1485 static int adv7842_g_dv_timings(struct v4l2_subdev *sd,
1486 struct v4l2_dv_timings *timings)
1488 struct adv7842_state *state = to_state(sd);
1490 if (state->mode == ADV7842_MODE_SDP)
1491 return -ENODATA;
1492 *timings = state->timings;
1493 return 0;
1496 static void enable_input(struct v4l2_subdev *sd)
1498 struct adv7842_state *state = to_state(sd);
1500 set_rgb_quantization_range(sd);
1501 switch (state->mode) {
1502 case ADV7842_MODE_SDP:
1503 case ADV7842_MODE_COMP:
1504 case ADV7842_MODE_RGB:
1505 io_write(sd, 0x15, 0xb0); /* Disable Tristate of Pins (no audio) */
1506 break;
1507 case ADV7842_MODE_HDMI:
1508 hdmi_write(sd, 0x01, 0x00); /* Enable HDMI clock terminators */
1509 io_write(sd, 0x15, 0xa0); /* Disable Tristate of Pins */
1510 hdmi_write_and_or(sd, 0x1a, 0xef, 0x00); /* Unmute audio */
1511 break;
1512 default:
1513 v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
1514 __func__, state->mode);
1515 break;
1519 static void disable_input(struct v4l2_subdev *sd)
1521 hdmi_write_and_or(sd, 0x1a, 0xef, 0x10); /* Mute audio [REF_01, c. 2.2.2] */
1522 msleep(16); /* 512 samples with >= 32 kHz sample rate [REF_03, c. 8.29] */
1523 io_write(sd, 0x15, 0xbe); /* Tristate all outputs from video core */
1524 hdmi_write(sd, 0x01, 0x78); /* Disable HDMI clock terminators */
1527 static void sdp_csc_coeff(struct v4l2_subdev *sd,
1528 const struct adv7842_sdp_csc_coeff *c)
1530 /* csc auto/manual */
1531 sdp_io_write_and_or(sd, 0xe0, 0xbf, c->manual ? 0x00 : 0x40);
1533 if (!c->manual)
1534 return;
1536 /* csc scaling */
1537 sdp_io_write_and_or(sd, 0xe0, 0x7f, c->scaling == 2 ? 0x80 : 0x00);
1539 /* A coeff */
1540 sdp_io_write_and_or(sd, 0xe0, 0xe0, c->A1 >> 8);
1541 sdp_io_write(sd, 0xe1, c->A1);
1542 sdp_io_write_and_or(sd, 0xe2, 0xe0, c->A2 >> 8);
1543 sdp_io_write(sd, 0xe3, c->A2);
1544 sdp_io_write_and_or(sd, 0xe4, 0xe0, c->A3 >> 8);
1545 sdp_io_write(sd, 0xe5, c->A3);
1547 /* A scale */
1548 sdp_io_write_and_or(sd, 0xe6, 0x80, c->A4 >> 8);
1549 sdp_io_write(sd, 0xe7, c->A4);
1551 /* B coeff */
1552 sdp_io_write_and_or(sd, 0xe8, 0xe0, c->B1 >> 8);
1553 sdp_io_write(sd, 0xe9, c->B1);
1554 sdp_io_write_and_or(sd, 0xea, 0xe0, c->B2 >> 8);
1555 sdp_io_write(sd, 0xeb, c->B2);
1556 sdp_io_write_and_or(sd, 0xec, 0xe0, c->B3 >> 8);
1557 sdp_io_write(sd, 0xed, c->B3);
1559 /* B scale */
1560 sdp_io_write_and_or(sd, 0xee, 0x80, c->B4 >> 8);
1561 sdp_io_write(sd, 0xef, c->B4);
1563 /* C coeff */
1564 sdp_io_write_and_or(sd, 0xf0, 0xe0, c->C1 >> 8);
1565 sdp_io_write(sd, 0xf1, c->C1);
1566 sdp_io_write_and_or(sd, 0xf2, 0xe0, c->C2 >> 8);
1567 sdp_io_write(sd, 0xf3, c->C2);
1568 sdp_io_write_and_or(sd, 0xf4, 0xe0, c->C3 >> 8);
1569 sdp_io_write(sd, 0xf5, c->C3);
1571 /* C scale */
1572 sdp_io_write_and_or(sd, 0xf6, 0x80, c->C4 >> 8);
1573 sdp_io_write(sd, 0xf7, c->C4);
1576 static void select_input(struct v4l2_subdev *sd,
1577 enum adv7842_vid_std_select vid_std_select)
1579 struct adv7842_state *state = to_state(sd);
1581 switch (state->mode) {
1582 case ADV7842_MODE_SDP:
1583 io_write(sd, 0x00, vid_std_select); /* video std: CVBS or YC mode */
1584 io_write(sd, 0x01, 0); /* prim mode */
1585 /* enable embedded syncs for auto graphics mode */
1586 cp_write_and_or(sd, 0x81, 0xef, 0x10);
1588 afe_write(sd, 0x00, 0x00); /* power up ADC */
1589 afe_write(sd, 0xc8, 0x00); /* phase control */
1591 io_write(sd, 0x19, 0x83); /* LLC DLL phase */
1592 io_write(sd, 0x33, 0x40); /* LLC DLL enable */
1594 io_write(sd, 0xdd, 0x90); /* Manual 2x output clock */
1595 /* script says register 0xde, which don't exist in manual */
1597 /* Manual analog input muxing mode, CVBS (6.4)*/
1598 afe_write_and_or(sd, 0x02, 0x7f, 0x80);
1599 if (vid_std_select == ADV7842_SDP_VID_STD_CVBS_SD_4x1) {
1600 afe_write(sd, 0x03, 0xa0); /* ADC0 to AIN10 (CVBS), ADC1 N/C*/
1601 afe_write(sd, 0x04, 0x00); /* ADC2 N/C,ADC3 N/C*/
1602 } else {
1603 afe_write(sd, 0x03, 0xa0); /* ADC0 to AIN10 (CVBS), ADC1 N/C*/
1604 afe_write(sd, 0x04, 0xc0); /* ADC2 to AIN12, ADC3 N/C*/
1606 afe_write(sd, 0x0c, 0x1f); /* ADI recommend write */
1607 afe_write(sd, 0x12, 0x63); /* ADI recommend write */
1609 sdp_io_write(sd, 0xb2, 0x60); /* Disable AV codes */
1610 sdp_io_write(sd, 0xc8, 0xe3); /* Disable Ancillary data */
1612 /* SDP recommended settings */
1613 sdp_write(sd, 0x00, 0x3F); /* Autodetect PAL NTSC (not SECAM) */
1614 sdp_write(sd, 0x01, 0x00); /* Pedestal Off */
1616 sdp_write(sd, 0x03, 0xE4); /* Manual VCR Gain Luma 0x40B */
1617 sdp_write(sd, 0x04, 0x0B); /* Manual Luma setting */
1618 sdp_write(sd, 0x05, 0xC3); /* Manual Chroma setting 0x3FE */
1619 sdp_write(sd, 0x06, 0xFE); /* Manual Chroma setting */
1620 sdp_write(sd, 0x12, 0x0D); /* Frame TBC,I_P, 3D comb enabled */
1621 sdp_write(sd, 0xA7, 0x00); /* ADI Recommended Write */
1622 sdp_io_write(sd, 0xB0, 0x00); /* Disable H and v blanking */
1624 /* deinterlacer enabled and 3D comb */
1625 sdp_write_and_or(sd, 0x12, 0xf6, 0x09);
1627 sdp_write(sd, 0xdd, 0x08); /* free run auto */
1629 break;
1631 case ADV7842_MODE_COMP:
1632 case ADV7842_MODE_RGB:
1633 /* Automatic analog input muxing mode */
1634 afe_write_and_or(sd, 0x02, 0x7f, 0x00);
1635 /* set mode and select free run resolution */
1636 io_write(sd, 0x00, vid_std_select); /* video std */
1637 io_write(sd, 0x01, 0x02); /* prim mode */
1638 cp_write_and_or(sd, 0x81, 0xef, 0x10); /* enable embedded syncs
1639 for auto graphics mode */
1641 afe_write(sd, 0x00, 0x00); /* power up ADC */
1642 afe_write(sd, 0xc8, 0x00); /* phase control */
1643 if (state->mode == ADV7842_MODE_COMP) {
1644 /* force to YCrCb */
1645 io_write_and_or(sd, 0x02, 0x0f, 0x60);
1646 } else {
1647 /* force to RGB */
1648 io_write_and_or(sd, 0x02, 0x0f, 0x10);
1651 /* set ADI recommended settings for digitizer */
1652 /* "ADV7842 Register Settings Recommendations
1653 * (rev. 1.8, November 2010)" p. 9. */
1654 afe_write(sd, 0x0c, 0x1f); /* ADC Range improvement */
1655 afe_write(sd, 0x12, 0x63); /* ADC Range improvement */
1657 /* set to default gain for RGB */
1658 cp_write(sd, 0x73, 0x10);
1659 cp_write(sd, 0x74, 0x04);
1660 cp_write(sd, 0x75, 0x01);
1661 cp_write(sd, 0x76, 0x00);
1663 cp_write(sd, 0x3e, 0x04); /* CP core pre-gain control */
1664 cp_write(sd, 0xc3, 0x39); /* CP coast control. Graphics mode */
1665 cp_write(sd, 0x40, 0x5c); /* CP core pre-gain control. Graphics mode */
1666 break;
1668 case ADV7842_MODE_HDMI:
1669 /* Automatic analog input muxing mode */
1670 afe_write_and_or(sd, 0x02, 0x7f, 0x00);
1671 /* set mode and select free run resolution */
1672 if (state->hdmi_port_a)
1673 hdmi_write(sd, 0x00, 0x02); /* select port A */
1674 else
1675 hdmi_write(sd, 0x00, 0x03); /* select port B */
1676 io_write(sd, 0x00, vid_std_select); /* video std */
1677 io_write(sd, 0x01, 5); /* prim mode */
1678 cp_write_and_or(sd, 0x81, 0xef, 0x00); /* disable embedded syncs
1679 for auto graphics mode */
1681 /* set ADI recommended settings for HDMI: */
1682 /* "ADV7842 Register Settings Recommendations
1683 * (rev. 1.8, November 2010)" p. 3. */
1684 hdmi_write(sd, 0xc0, 0x00);
1685 hdmi_write(sd, 0x0d, 0x34); /* ADI recommended write */
1686 hdmi_write(sd, 0x3d, 0x10); /* ADI recommended write */
1687 hdmi_write(sd, 0x44, 0x85); /* TMDS PLL optimization */
1688 hdmi_write(sd, 0x46, 0x1f); /* ADI recommended write */
1689 hdmi_write(sd, 0x57, 0xb6); /* TMDS PLL optimization */
1690 hdmi_write(sd, 0x58, 0x03); /* TMDS PLL optimization */
1691 hdmi_write(sd, 0x60, 0x88); /* TMDS PLL optimization */
1692 hdmi_write(sd, 0x61, 0x88); /* TMDS PLL optimization */
1693 hdmi_write(sd, 0x6c, 0x18); /* Disable ISRC clearing bit,
1694 Improve robustness */
1695 hdmi_write(sd, 0x75, 0x10); /* DDC drive strength */
1696 hdmi_write(sd, 0x85, 0x1f); /* equaliser */
1697 hdmi_write(sd, 0x87, 0x70); /* ADI recommended write */
1698 hdmi_write(sd, 0x89, 0x04); /* equaliser */
1699 hdmi_write(sd, 0x8a, 0x1e); /* equaliser */
1700 hdmi_write(sd, 0x93, 0x04); /* equaliser */
1701 hdmi_write(sd, 0x94, 0x1e); /* equaliser */
1702 hdmi_write(sd, 0x99, 0xa1); /* ADI recommended write */
1703 hdmi_write(sd, 0x9b, 0x09); /* ADI recommended write */
1704 hdmi_write(sd, 0x9d, 0x02); /* equaliser */
1706 afe_write(sd, 0x00, 0xff); /* power down ADC */
1707 afe_write(sd, 0xc8, 0x40); /* phase control */
1709 /* set to default gain for HDMI */
1710 cp_write(sd, 0x73, 0x10);
1711 cp_write(sd, 0x74, 0x04);
1712 cp_write(sd, 0x75, 0x01);
1713 cp_write(sd, 0x76, 0x00);
1715 /* reset ADI recommended settings for digitizer */
1716 /* "ADV7842 Register Settings Recommendations
1717 * (rev. 2.5, June 2010)" p. 17. */
1718 afe_write(sd, 0x12, 0xfb); /* ADC noise shaping filter controls */
1719 afe_write(sd, 0x0c, 0x0d); /* CP core gain controls */
1720 cp_write(sd, 0x3e, 0x80); /* CP core pre-gain control,
1721 enable color control */
1722 /* CP coast control */
1723 cp_write(sd, 0xc3, 0x33); /* Component mode */
1725 /* color space conversion, autodetect color space */
1726 io_write_and_or(sd, 0x02, 0x0f, 0xf0);
1727 break;
1729 default:
1730 v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
1731 __func__, state->mode);
1732 break;
1736 static int adv7842_s_routing(struct v4l2_subdev *sd,
1737 u32 input, u32 output, u32 config)
1739 struct adv7842_state *state = to_state(sd);
1741 v4l2_dbg(2, debug, sd, "%s: input %d\n", __func__, input);
1743 switch (input) {
1744 case ADV7842_SELECT_HDMI_PORT_A:
1745 state->mode = ADV7842_MODE_HDMI;
1746 state->vid_std_select = ADV7842_HDMI_COMP_VID_STD_HD_1250P;
1747 state->hdmi_port_a = true;
1748 break;
1749 case ADV7842_SELECT_HDMI_PORT_B:
1750 state->mode = ADV7842_MODE_HDMI;
1751 state->vid_std_select = ADV7842_HDMI_COMP_VID_STD_HD_1250P;
1752 state->hdmi_port_a = false;
1753 break;
1754 case ADV7842_SELECT_VGA_COMP:
1755 state->mode = ADV7842_MODE_COMP;
1756 state->vid_std_select = ADV7842_RGB_VID_STD_AUTO_GRAPH_MODE;
1757 break;
1758 case ADV7842_SELECT_VGA_RGB:
1759 state->mode = ADV7842_MODE_RGB;
1760 state->vid_std_select = ADV7842_RGB_VID_STD_AUTO_GRAPH_MODE;
1761 break;
1762 case ADV7842_SELECT_SDP_CVBS:
1763 state->mode = ADV7842_MODE_SDP;
1764 state->vid_std_select = ADV7842_SDP_VID_STD_CVBS_SD_4x1;
1765 break;
1766 case ADV7842_SELECT_SDP_YC:
1767 state->mode = ADV7842_MODE_SDP;
1768 state->vid_std_select = ADV7842_SDP_VID_STD_YC_SD4_x1;
1769 break;
1770 default:
1771 return -EINVAL;
1774 disable_input(sd);
1775 select_input(sd, state->vid_std_select);
1776 enable_input(sd);
1778 v4l2_subdev_notify(sd, ADV7842_FMT_CHANGE, NULL);
1780 return 0;
1783 static int adv7842_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned int index,
1784 enum v4l2_mbus_pixelcode *code)
1786 if (index)
1787 return -EINVAL;
1788 /* Good enough for now */
1789 *code = V4L2_MBUS_FMT_FIXED;
1790 return 0;
1793 static int adv7842_g_mbus_fmt(struct v4l2_subdev *sd,
1794 struct v4l2_mbus_framefmt *fmt)
1796 struct adv7842_state *state = to_state(sd);
1798 fmt->width = state->timings.bt.width;
1799 fmt->height = state->timings.bt.height;
1800 fmt->code = V4L2_MBUS_FMT_FIXED;
1801 fmt->field = V4L2_FIELD_NONE;
1803 if (state->mode == ADV7842_MODE_SDP) {
1804 /* SPD block */
1805 if (!(sdp_read(sd, 0x5A) & 0x01))
1806 return -EINVAL;
1807 fmt->width = 720;
1808 /* valid signal */
1809 if (state->norm & V4L2_STD_525_60)
1810 fmt->height = 480;
1811 else
1812 fmt->height = 576;
1813 fmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
1814 return 0;
1817 if (state->timings.bt.standards & V4L2_DV_BT_STD_CEA861) {
1818 fmt->colorspace = (state->timings.bt.height <= 576) ?
1819 V4L2_COLORSPACE_SMPTE170M : V4L2_COLORSPACE_REC709;
1821 return 0;
1824 static void adv7842_irq_enable(struct v4l2_subdev *sd, bool enable)
1826 if (enable) {
1827 /* Enable SSPD, STDI and CP locked/unlocked interrupts */
1828 io_write(sd, 0x46, 0x9c);
1829 /* ESDP_50HZ_DET interrupt */
1830 io_write(sd, 0x5a, 0x10);
1831 /* Enable CABLE_DET_A/B_ST (+5v) interrupt */
1832 io_write(sd, 0x73, 0x03);
1833 /* Enable V_LOCKED and DE_REGEN_LCK interrupts */
1834 io_write(sd, 0x78, 0x03);
1835 /* Enable SDP Standard Detection Change and SDP Video Detected */
1836 io_write(sd, 0xa0, 0x09);
1837 } else {
1838 io_write(sd, 0x46, 0x0);
1839 io_write(sd, 0x5a, 0x0);
1840 io_write(sd, 0x73, 0x0);
1841 io_write(sd, 0x78, 0x0);
1842 io_write(sd, 0xa0, 0x0);
1846 static int adv7842_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
1848 struct adv7842_state *state = to_state(sd);
1849 u8 fmt_change_cp, fmt_change_digital, fmt_change_sdp;
1850 u8 irq_status[5];
1852 adv7842_irq_enable(sd, false);
1854 /* read status */
1855 irq_status[0] = io_read(sd, 0x43);
1856 irq_status[1] = io_read(sd, 0x57);
1857 irq_status[2] = io_read(sd, 0x70);
1858 irq_status[3] = io_read(sd, 0x75);
1859 irq_status[4] = io_read(sd, 0x9d);
1861 /* and clear */
1862 if (irq_status[0])
1863 io_write(sd, 0x44, irq_status[0]);
1864 if (irq_status[1])
1865 io_write(sd, 0x58, irq_status[1]);
1866 if (irq_status[2])
1867 io_write(sd, 0x71, irq_status[2]);
1868 if (irq_status[3])
1869 io_write(sd, 0x76, irq_status[3]);
1870 if (irq_status[4])
1871 io_write(sd, 0x9e, irq_status[4]);
1873 adv7842_irq_enable(sd, true);
1875 v4l2_dbg(1, debug, sd, "%s: irq %x, %x, %x, %x, %x\n", __func__,
1876 irq_status[0], irq_status[1], irq_status[2],
1877 irq_status[3], irq_status[4]);
1879 /* format change CP */
1880 fmt_change_cp = irq_status[0] & 0x9c;
1882 /* format change SDP */
1883 if (state->mode == ADV7842_MODE_SDP)
1884 fmt_change_sdp = (irq_status[1] & 0x30) | (irq_status[4] & 0x09);
1885 else
1886 fmt_change_sdp = 0;
1888 /* digital format CP */
1889 if (is_digital_input(sd))
1890 fmt_change_digital = irq_status[3] & 0x03;
1891 else
1892 fmt_change_digital = 0;
1894 /* notify */
1895 if (fmt_change_cp || fmt_change_digital || fmt_change_sdp) {
1896 v4l2_dbg(1, debug, sd,
1897 "%s: fmt_change_cp = 0x%x, fmt_change_digital = 0x%x, fmt_change_sdp = 0x%x\n",
1898 __func__, fmt_change_cp, fmt_change_digital,
1899 fmt_change_sdp);
1900 v4l2_subdev_notify(sd, ADV7842_FMT_CHANGE, NULL);
1903 /* 5v cable detect */
1904 if (irq_status[2])
1905 adv7842_s_detect_tx_5v_ctrl(sd);
1907 if (handled)
1908 *handled = true;
1910 return 0;
1913 static int adv7842_get_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
1915 struct adv7842_state *state = to_state(sd);
1916 u8 *data = NULL;
1918 if (edid->pad > ADV7842_EDID_PORT_VGA)
1919 return -EINVAL;
1920 if (edid->blocks == 0)
1921 return -EINVAL;
1922 if (edid->blocks > 2)
1923 return -EINVAL;
1924 if (edid->start_block > 1)
1925 return -EINVAL;
1926 if (edid->start_block == 1)
1927 edid->blocks = 1;
1928 if (!edid->edid)
1929 return -EINVAL;
1931 switch (edid->pad) {
1932 case ADV7842_EDID_PORT_A:
1933 case ADV7842_EDID_PORT_B:
1934 if (state->hdmi_edid.present & (0x04 << edid->pad))
1935 data = state->hdmi_edid.edid;
1936 break;
1937 case ADV7842_EDID_PORT_VGA:
1938 if (state->vga_edid.present)
1939 data = state->vga_edid.edid;
1940 break;
1941 default:
1942 return -EINVAL;
1944 if (!data)
1945 return -ENODATA;
1947 memcpy(edid->edid,
1948 data + edid->start_block * 128,
1949 edid->blocks * 128);
1950 return 0;
1953 static int adv7842_set_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *e)
1955 struct adv7842_state *state = to_state(sd);
1956 int err = 0;
1958 if (e->pad > ADV7842_EDID_PORT_VGA)
1959 return -EINVAL;
1960 if (e->start_block != 0)
1961 return -EINVAL;
1962 if (e->blocks > 2)
1963 return -E2BIG;
1964 if (!e->edid)
1965 return -EINVAL;
1967 /* todo, per edid */
1968 state->aspect_ratio = v4l2_calc_aspect_ratio(e->edid[0x15],
1969 e->edid[0x16]);
1971 switch (e->pad) {
1972 case ADV7842_EDID_PORT_VGA:
1973 memset(&state->vga_edid.edid, 0, 256);
1974 state->vga_edid.present = e->blocks ? 0x1 : 0x0;
1975 memcpy(&state->vga_edid.edid, e->edid, 128 * e->blocks);
1976 err = edid_write_vga_segment(sd);
1977 break;
1978 case ADV7842_EDID_PORT_A:
1979 case ADV7842_EDID_PORT_B:
1980 memset(&state->hdmi_edid.edid, 0, 256);
1981 if (e->blocks)
1982 state->hdmi_edid.present |= 0x04 << e->pad;
1983 else
1984 state->hdmi_edid.present &= ~(0x04 << e->pad);
1985 memcpy(&state->hdmi_edid.edid, e->edid, 128 * e->blocks);
1986 err = edid_write_hdmi_segment(sd, e->pad);
1987 break;
1988 default:
1989 return -EINVAL;
1991 if (err < 0)
1992 v4l2_err(sd, "error %d writing edid on port %d\n", err, e->pad);
1993 return err;
1996 /*********** avi info frame CEA-861-E **************/
1997 /* TODO move to common library */
1999 struct avi_info_frame {
2000 uint8_t f17;
2001 uint8_t y10;
2002 uint8_t a0;
2003 uint8_t b10;
2004 uint8_t s10;
2005 uint8_t c10;
2006 uint8_t m10;
2007 uint8_t r3210;
2008 uint8_t itc;
2009 uint8_t ec210;
2010 uint8_t q10;
2011 uint8_t sc10;
2012 uint8_t f47;
2013 uint8_t vic;
2014 uint8_t yq10;
2015 uint8_t cn10;
2016 uint8_t pr3210;
2017 uint16_t etb;
2018 uint16_t sbb;
2019 uint16_t elb;
2020 uint16_t srb;
2023 static const char *y10_txt[4] = {
2024 "RGB",
2025 "YCbCr 4:2:2",
2026 "YCbCr 4:4:4",
2027 "Future",
2030 static const char *c10_txt[4] = {
2031 "No Data",
2032 "SMPTE 170M",
2033 "ITU-R 709",
2034 "Extended Colorimetry information valied",
2037 static const char *itc_txt[2] = {
2038 "No Data",
2039 "IT content",
2042 static const char *ec210_txt[8] = {
2043 "xvYCC601",
2044 "xvYCC709",
2045 "sYCC601",
2046 "AdobeYCC601",
2047 "AdobeRGB",
2048 "5 reserved",
2049 "6 reserved",
2050 "7 reserved",
2053 static const char *q10_txt[4] = {
2054 "Default",
2055 "Limited Range",
2056 "Full Range",
2057 "Reserved",
2060 static void parse_avi_infoframe(struct v4l2_subdev *sd, uint8_t *buf,
2061 struct avi_info_frame *avi)
2063 avi->f17 = (buf[1] >> 7) & 0x1;
2064 avi->y10 = (buf[1] >> 5) & 0x3;
2065 avi->a0 = (buf[1] >> 4) & 0x1;
2066 avi->b10 = (buf[1] >> 2) & 0x3;
2067 avi->s10 = buf[1] & 0x3;
2068 avi->c10 = (buf[2] >> 6) & 0x3;
2069 avi->m10 = (buf[2] >> 4) & 0x3;
2070 avi->r3210 = buf[2] & 0xf;
2071 avi->itc = (buf[3] >> 7) & 0x1;
2072 avi->ec210 = (buf[3] >> 4) & 0x7;
2073 avi->q10 = (buf[3] >> 2) & 0x3;
2074 avi->sc10 = buf[3] & 0x3;
2075 avi->f47 = (buf[4] >> 7) & 0x1;
2076 avi->vic = buf[4] & 0x7f;
2077 avi->yq10 = (buf[5] >> 6) & 0x3;
2078 avi->cn10 = (buf[5] >> 4) & 0x3;
2079 avi->pr3210 = buf[5] & 0xf;
2080 avi->etb = buf[6] + 256*buf[7];
2081 avi->sbb = buf[8] + 256*buf[9];
2082 avi->elb = buf[10] + 256*buf[11];
2083 avi->srb = buf[12] + 256*buf[13];
2086 static void print_avi_infoframe(struct v4l2_subdev *sd)
2088 int i;
2089 uint8_t buf[14];
2090 uint8_t avi_inf_len;
2091 struct avi_info_frame avi;
2093 if (!(hdmi_read(sd, 0x05) & 0x80)) {
2094 v4l2_info(sd, "receive DVI-D signal (AVI infoframe not supported)\n");
2095 return;
2097 if (!(io_read(sd, 0x60) & 0x01)) {
2098 v4l2_info(sd, "AVI infoframe not received\n");
2099 return;
2102 if (io_read(sd, 0x88) & 0x10) {
2103 /* Note: the ADV7842 calculated incorrect checksums for InfoFrames
2104 with a length of 14 or 15. See the ADV7842 Register Settings
2105 Recommendations document for more details. */
2106 v4l2_info(sd, "AVI infoframe checksum error\n");
2107 return;
2110 avi_inf_len = infoframe_read(sd, 0xe2);
2111 v4l2_info(sd, "AVI infoframe version %d (%d byte)\n",
2112 infoframe_read(sd, 0xe1), avi_inf_len);
2114 if (infoframe_read(sd, 0xe1) != 0x02)
2115 return;
2117 for (i = 0; i < 14; i++)
2118 buf[i] = infoframe_read(sd, i);
2120 v4l2_info(sd, "\t%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
2121 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7],
2122 buf[8], buf[9], buf[10], buf[11], buf[12], buf[13]);
2124 parse_avi_infoframe(sd, buf, &avi);
2126 if (avi.vic)
2127 v4l2_info(sd, "\tVIC: %d\n", avi.vic);
2128 if (avi.itc)
2129 v4l2_info(sd, "\t%s\n", itc_txt[avi.itc]);
2131 if (avi.y10)
2132 v4l2_info(sd, "\t%s %s\n", y10_txt[avi.y10], !avi.c10 ? "" :
2133 (avi.c10 == 0x3 ? ec210_txt[avi.ec210] : c10_txt[avi.c10]));
2134 else
2135 v4l2_info(sd, "\t%s %s\n", y10_txt[avi.y10], q10_txt[avi.q10]);
2138 static const char * const prim_mode_txt[] = {
2139 "SDP",
2140 "Component",
2141 "Graphics",
2142 "Reserved",
2143 "CVBS & HDMI AUDIO",
2144 "HDMI-Comp",
2145 "HDMI-GR",
2146 "Reserved",
2147 "Reserved",
2148 "Reserved",
2149 "Reserved",
2150 "Reserved",
2151 "Reserved",
2152 "Reserved",
2153 "Reserved",
2154 "Reserved",
2157 static int adv7842_sdp_log_status(struct v4l2_subdev *sd)
2159 /* SDP (Standard definition processor) block */
2160 uint8_t sdp_signal_detected = sdp_read(sd, 0x5A) & 0x01;
2162 v4l2_info(sd, "Chip powered %s\n", no_power(sd) ? "off" : "on");
2163 v4l2_info(sd, "Prim-mode = 0x%x, video std = 0x%x\n",
2164 io_read(sd, 0x01) & 0x0f, io_read(sd, 0x00) & 0x3f);
2166 v4l2_info(sd, "SDP: free run: %s\n",
2167 (sdp_read(sd, 0x56) & 0x01) ? "on" : "off");
2168 v4l2_info(sd, "SDP: %s\n", sdp_signal_detected ?
2169 "valid SD/PR signal detected" : "invalid/no signal");
2170 if (sdp_signal_detected) {
2171 static const char * const sdp_std_txt[] = {
2172 "NTSC-M/J",
2173 "1?",
2174 "NTSC-443",
2175 "60HzSECAM",
2176 "PAL-M",
2177 "5?",
2178 "PAL-60",
2179 "7?", "8?", "9?", "a?", "b?",
2180 "PAL-CombN",
2181 "d?",
2182 "PAL-BGHID",
2183 "SECAM"
2185 v4l2_info(sd, "SDP: standard %s\n",
2186 sdp_std_txt[sdp_read(sd, 0x52) & 0x0f]);
2187 v4l2_info(sd, "SDP: %s\n",
2188 (sdp_read(sd, 0x59) & 0x08) ? "50Hz" : "60Hz");
2189 v4l2_info(sd, "SDP: %s\n",
2190 (sdp_read(sd, 0x57) & 0x08) ? "Interlaced" : "Progressive");
2191 v4l2_info(sd, "SDP: deinterlacer %s\n",
2192 (sdp_read(sd, 0x12) & 0x08) ? "enabled" : "disabled");
2193 v4l2_info(sd, "SDP: csc %s mode\n",
2194 (sdp_io_read(sd, 0xe0) & 0x40) ? "auto" : "manual");
2196 return 0;
2199 static int adv7842_cp_log_status(struct v4l2_subdev *sd)
2201 /* CP block */
2202 struct adv7842_state *state = to_state(sd);
2203 struct v4l2_dv_timings timings;
2204 uint8_t reg_io_0x02 = io_read(sd, 0x02);
2205 uint8_t reg_io_0x21 = io_read(sd, 0x21);
2206 uint8_t reg_rep_0x77 = rep_read(sd, 0x77);
2207 uint8_t reg_rep_0x7d = rep_read(sd, 0x7d);
2208 bool audio_pll_locked = hdmi_read(sd, 0x04) & 0x01;
2209 bool audio_sample_packet_detect = hdmi_read(sd, 0x18) & 0x01;
2210 bool audio_mute = io_read(sd, 0x65) & 0x40;
2212 static const char * const csc_coeff_sel_rb[16] = {
2213 "bypassed", "YPbPr601 -> RGB", "reserved", "YPbPr709 -> RGB",
2214 "reserved", "RGB -> YPbPr601", "reserved", "RGB -> YPbPr709",
2215 "reserved", "YPbPr709 -> YPbPr601", "YPbPr601 -> YPbPr709",
2216 "reserved", "reserved", "reserved", "reserved", "manual"
2218 static const char * const input_color_space_txt[16] = {
2219 "RGB limited range (16-235)", "RGB full range (0-255)",
2220 "YCbCr Bt.601 (16-235)", "YCbCr Bt.709 (16-235)",
2221 "xvYCC Bt.601", "xvYCC Bt.709",
2222 "YCbCr Bt.601 (0-255)", "YCbCr Bt.709 (0-255)",
2223 "invalid", "invalid", "invalid", "invalid", "invalid",
2224 "invalid", "invalid", "automatic"
2226 static const char * const rgb_quantization_range_txt[] = {
2227 "Automatic",
2228 "RGB limited range (16-235)",
2229 "RGB full range (0-255)",
2231 static const char * const deep_color_mode_txt[4] = {
2232 "8-bits per channel",
2233 "10-bits per channel",
2234 "12-bits per channel",
2235 "16-bits per channel (not supported)"
2238 v4l2_info(sd, "-----Chip status-----\n");
2239 v4l2_info(sd, "Chip power: %s\n", no_power(sd) ? "off" : "on");
2240 v4l2_info(sd, "HDMI/DVI-D port selected: %s\n",
2241 state->hdmi_port_a ? "A" : "B");
2242 v4l2_info(sd, "EDID A %s, B %s\n",
2243 ((reg_rep_0x7d & 0x04) && (reg_rep_0x77 & 0x04)) ?
2244 "enabled" : "disabled",
2245 ((reg_rep_0x7d & 0x08) && (reg_rep_0x77 & 0x08)) ?
2246 "enabled" : "disabled");
2247 v4l2_info(sd, "HPD A %s, B %s\n",
2248 reg_io_0x21 & 0x02 ? "enabled" : "disabled",
2249 reg_io_0x21 & 0x01 ? "enabled" : "disabled");
2250 v4l2_info(sd, "CEC %s\n", !!(cec_read(sd, 0x2a) & 0x01) ?
2251 "enabled" : "disabled");
2253 v4l2_info(sd, "-----Signal status-----\n");
2254 if (state->hdmi_port_a) {
2255 v4l2_info(sd, "Cable detected (+5V power): %s\n",
2256 io_read(sd, 0x6f) & 0x02 ? "true" : "false");
2257 v4l2_info(sd, "TMDS signal detected: %s\n",
2258 (io_read(sd, 0x6a) & 0x02) ? "true" : "false");
2259 v4l2_info(sd, "TMDS signal locked: %s\n",
2260 (io_read(sd, 0x6a) & 0x20) ? "true" : "false");
2261 } else {
2262 v4l2_info(sd, "Cable detected (+5V power):%s\n",
2263 io_read(sd, 0x6f) & 0x01 ? "true" : "false");
2264 v4l2_info(sd, "TMDS signal detected: %s\n",
2265 (io_read(sd, 0x6a) & 0x01) ? "true" : "false");
2266 v4l2_info(sd, "TMDS signal locked: %s\n",
2267 (io_read(sd, 0x6a) & 0x10) ? "true" : "false");
2269 v4l2_info(sd, "CP free run: %s\n",
2270 (!!(cp_read(sd, 0xff) & 0x10) ? "on" : "off"));
2271 v4l2_info(sd, "Prim-mode = 0x%x, video std = 0x%x, v_freq = 0x%x\n",
2272 io_read(sd, 0x01) & 0x0f, io_read(sd, 0x00) & 0x3f,
2273 (io_read(sd, 0x01) & 0x70) >> 4);
2275 v4l2_info(sd, "-----Video Timings-----\n");
2276 if (no_cp_signal(sd)) {
2277 v4l2_info(sd, "STDI: not locked\n");
2278 } else {
2279 uint32_t bl = ((cp_read(sd, 0xb1) & 0x3f) << 8) | cp_read(sd, 0xb2);
2280 uint32_t lcf = ((cp_read(sd, 0xb3) & 0x7) << 8) | cp_read(sd, 0xb4);
2281 uint32_t lcvs = cp_read(sd, 0xb3) >> 3;
2282 uint32_t fcl = ((cp_read(sd, 0xb8) & 0x1f) << 8) | cp_read(sd, 0xb9);
2283 char hs_pol = ((cp_read(sd, 0xb5) & 0x10) ?
2284 ((cp_read(sd, 0xb5) & 0x08) ? '+' : '-') : 'x');
2285 char vs_pol = ((cp_read(sd, 0xb5) & 0x40) ?
2286 ((cp_read(sd, 0xb5) & 0x20) ? '+' : '-') : 'x');
2287 v4l2_info(sd,
2288 "STDI: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, fcl = %d, %s, %chsync, %cvsync\n",
2289 lcf, bl, lcvs, fcl,
2290 (cp_read(sd, 0xb1) & 0x40) ?
2291 "interlaced" : "progressive",
2292 hs_pol, vs_pol);
2294 if (adv7842_query_dv_timings(sd, &timings))
2295 v4l2_info(sd, "No video detected\n");
2296 else
2297 v4l2_print_dv_timings(sd->name, "Detected format: ",
2298 &timings, true);
2299 v4l2_print_dv_timings(sd->name, "Configured format: ",
2300 &state->timings, true);
2302 if (no_cp_signal(sd))
2303 return 0;
2305 v4l2_info(sd, "-----Color space-----\n");
2306 v4l2_info(sd, "RGB quantization range ctrl: %s\n",
2307 rgb_quantization_range_txt[state->rgb_quantization_range]);
2308 v4l2_info(sd, "Input color space: %s\n",
2309 input_color_space_txt[reg_io_0x02 >> 4]);
2310 v4l2_info(sd, "Output color space: %s %s, saturator %s\n",
2311 (reg_io_0x02 & 0x02) ? "RGB" : "YCbCr",
2312 (reg_io_0x02 & 0x04) ? "(16-235)" : "(0-255)",
2313 ((reg_io_0x02 & 0x04) ^ (reg_io_0x02 & 0x01)) ?
2314 "enabled" : "disabled");
2315 v4l2_info(sd, "Color space conversion: %s\n",
2316 csc_coeff_sel_rb[cp_read(sd, 0xf4) >> 4]);
2318 if (!is_digital_input(sd))
2319 return 0;
2321 v4l2_info(sd, "-----%s status-----\n", is_hdmi(sd) ? "HDMI" : "DVI-D");
2322 v4l2_info(sd, "HDCP encrypted content: %s\n",
2323 (hdmi_read(sd, 0x05) & 0x40) ? "true" : "false");
2324 v4l2_info(sd, "HDCP keys read: %s%s\n",
2325 (hdmi_read(sd, 0x04) & 0x20) ? "yes" : "no",
2326 (hdmi_read(sd, 0x04) & 0x10) ? "ERROR" : "");
2327 if (!is_hdmi(sd))
2328 return 0;
2330 v4l2_info(sd, "Audio: pll %s, samples %s, %s\n",
2331 audio_pll_locked ? "locked" : "not locked",
2332 audio_sample_packet_detect ? "detected" : "not detected",
2333 audio_mute ? "muted" : "enabled");
2334 if (audio_pll_locked && audio_sample_packet_detect) {
2335 v4l2_info(sd, "Audio format: %s\n",
2336 (hdmi_read(sd, 0x07) & 0x40) ? "multi-channel" : "stereo");
2338 v4l2_info(sd, "Audio CTS: %u\n", (hdmi_read(sd, 0x5b) << 12) +
2339 (hdmi_read(sd, 0x5c) << 8) +
2340 (hdmi_read(sd, 0x5d) & 0xf0));
2341 v4l2_info(sd, "Audio N: %u\n", ((hdmi_read(sd, 0x5d) & 0x0f) << 16) +
2342 (hdmi_read(sd, 0x5e) << 8) +
2343 hdmi_read(sd, 0x5f));
2344 v4l2_info(sd, "AV Mute: %s\n",
2345 (hdmi_read(sd, 0x04) & 0x40) ? "on" : "off");
2346 v4l2_info(sd, "Deep color mode: %s\n",
2347 deep_color_mode_txt[hdmi_read(sd, 0x0b) >> 6]);
2349 print_avi_infoframe(sd);
2350 return 0;
2353 static int adv7842_log_status(struct v4l2_subdev *sd)
2355 struct adv7842_state *state = to_state(sd);
2357 if (state->mode == ADV7842_MODE_SDP)
2358 return adv7842_sdp_log_status(sd);
2359 return adv7842_cp_log_status(sd);
2362 static int adv7842_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
2364 struct adv7842_state *state = to_state(sd);
2366 v4l2_dbg(1, debug, sd, "%s:\n", __func__);
2368 if (state->mode != ADV7842_MODE_SDP)
2369 return -ENODATA;
2371 if (!(sdp_read(sd, 0x5A) & 0x01)) {
2372 *std = 0;
2373 v4l2_dbg(1, debug, sd, "%s: no valid signal\n", __func__);
2374 return 0;
2377 switch (sdp_read(sd, 0x52) & 0x0f) {
2378 case 0:
2379 /* NTSC-M/J */
2380 *std &= V4L2_STD_NTSC;
2381 break;
2382 case 2:
2383 /* NTSC-443 */
2384 *std &= V4L2_STD_NTSC_443;
2385 break;
2386 case 3:
2387 /* 60HzSECAM */
2388 *std &= V4L2_STD_SECAM;
2389 break;
2390 case 4:
2391 /* PAL-M */
2392 *std &= V4L2_STD_PAL_M;
2393 break;
2394 case 6:
2395 /* PAL-60 */
2396 *std &= V4L2_STD_PAL_60;
2397 break;
2398 case 0xc:
2399 /* PAL-CombN */
2400 *std &= V4L2_STD_PAL_Nc;
2401 break;
2402 case 0xe:
2403 /* PAL-BGHID */
2404 *std &= V4L2_STD_PAL;
2405 break;
2406 case 0xf:
2407 /* SECAM */
2408 *std &= V4L2_STD_SECAM;
2409 break;
2410 default:
2411 *std &= V4L2_STD_ALL;
2412 break;
2414 return 0;
2417 static void adv7842_s_sdp_io(struct v4l2_subdev *sd, struct adv7842_sdp_io_sync_adjustment *s)
2419 if (s && s->adjust) {
2420 sdp_io_write(sd, 0x94, (s->hs_beg >> 8) & 0xf);
2421 sdp_io_write(sd, 0x95, s->hs_beg & 0xff);
2422 sdp_io_write(sd, 0x96, (s->hs_width >> 8) & 0xf);
2423 sdp_io_write(sd, 0x97, s->hs_width & 0xff);
2424 sdp_io_write(sd, 0x98, (s->de_beg >> 8) & 0xf);
2425 sdp_io_write(sd, 0x99, s->de_beg & 0xff);
2426 sdp_io_write(sd, 0x9a, (s->de_end >> 8) & 0xf);
2427 sdp_io_write(sd, 0x9b, s->de_end & 0xff);
2428 sdp_io_write(sd, 0xac, s->de_v_beg_o);
2429 sdp_io_write(sd, 0xad, s->de_v_beg_e);
2430 sdp_io_write(sd, 0xae, s->de_v_end_o);
2431 sdp_io_write(sd, 0xaf, s->de_v_end_e);
2432 } else {
2433 /* set to default */
2434 sdp_io_write(sd, 0x94, 0x00);
2435 sdp_io_write(sd, 0x95, 0x00);
2436 sdp_io_write(sd, 0x96, 0x00);
2437 sdp_io_write(sd, 0x97, 0x20);
2438 sdp_io_write(sd, 0x98, 0x00);
2439 sdp_io_write(sd, 0x99, 0x00);
2440 sdp_io_write(sd, 0x9a, 0x00);
2441 sdp_io_write(sd, 0x9b, 0x00);
2442 sdp_io_write(sd, 0xac, 0x04);
2443 sdp_io_write(sd, 0xad, 0x04);
2444 sdp_io_write(sd, 0xae, 0x04);
2445 sdp_io_write(sd, 0xaf, 0x04);
2449 static int adv7842_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
2451 struct adv7842_state *state = to_state(sd);
2452 struct adv7842_platform_data *pdata = &state->pdata;
2454 v4l2_dbg(1, debug, sd, "%s:\n", __func__);
2456 if (state->mode != ADV7842_MODE_SDP)
2457 return -ENODATA;
2459 if (norm & V4L2_STD_625_50)
2460 adv7842_s_sdp_io(sd, &pdata->sdp_io_sync_625);
2461 else if (norm & V4L2_STD_525_60)
2462 adv7842_s_sdp_io(sd, &pdata->sdp_io_sync_525);
2463 else
2464 adv7842_s_sdp_io(sd, NULL);
2466 if (norm & V4L2_STD_ALL) {
2467 state->norm = norm;
2468 return 0;
2470 return -EINVAL;
2473 static int adv7842_g_std(struct v4l2_subdev *sd, v4l2_std_id *norm)
2475 struct adv7842_state *state = to_state(sd);
2477 v4l2_dbg(1, debug, sd, "%s:\n", __func__);
2479 if (state->mode != ADV7842_MODE_SDP)
2480 return -ENODATA;
2482 *norm = state->norm;
2483 return 0;
2486 /* ----------------------------------------------------------------------- */
2488 static int adv7842_core_init(struct v4l2_subdev *sd)
2490 struct adv7842_state *state = to_state(sd);
2491 struct adv7842_platform_data *pdata = &state->pdata;
2492 hdmi_write(sd, 0x48,
2493 (pdata->disable_pwrdnb ? 0x80 : 0) |
2494 (pdata->disable_cable_det_rst ? 0x40 : 0));
2496 disable_input(sd);
2498 /* power */
2499 io_write(sd, 0x0c, 0x42); /* Power up part and power down VDP */
2500 io_write(sd, 0x15, 0x80); /* Power up pads */
2502 /* video format */
2503 io_write(sd, 0x02,
2504 0xf0 |
2505 pdata->alt_gamma << 3 |
2506 pdata->op_656_range << 2 |
2507 pdata->rgb_out << 1 |
2508 pdata->alt_data_sat << 0);
2509 io_write(sd, 0x03, pdata->op_format_sel);
2510 io_write_and_or(sd, 0x04, 0x1f, pdata->op_ch_sel << 5);
2511 io_write_and_or(sd, 0x05, 0xf0, pdata->blank_data << 3 |
2512 pdata->insert_av_codes << 2 |
2513 pdata->replicate_av_codes << 1 |
2514 pdata->invert_cbcr << 0);
2516 /* HDMI audio */
2517 hdmi_write_and_or(sd, 0x1a, 0xf1, 0x08); /* Wait 1 s before unmute */
2519 /* Drive strength */
2520 io_write_and_or(sd, 0x14, 0xc0, pdata->drive_strength.data<<4 |
2521 pdata->drive_strength.clock<<2 |
2522 pdata->drive_strength.sync);
2524 /* HDMI free run */
2525 cp_write(sd, 0xba, (pdata->hdmi_free_run_mode << 1) | 0x01);
2527 /* TODO from platform data */
2528 cp_write(sd, 0x69, 0x14); /* Enable CP CSC */
2529 io_write(sd, 0x06, 0xa6); /* positive VS and HS and DE */
2530 cp_write(sd, 0xf3, 0xdc); /* Low threshold to enter/exit free run mode */
2531 afe_write(sd, 0xb5, 0x01); /* Setting MCLK to 256Fs */
2533 afe_write(sd, 0x02, pdata->ain_sel); /* Select analog input muxing mode */
2534 io_write_and_or(sd, 0x30, ~(1 << 4), pdata->output_bus_lsb_to_msb << 4);
2536 sdp_csc_coeff(sd, &pdata->sdp_csc_coeff);
2538 /* todo, improve settings for sdram */
2539 if (pdata->sd_ram_size >= 128) {
2540 sdp_write(sd, 0x12, 0x0d); /* Frame TBC,3D comb enabled */
2541 if (pdata->sd_ram_ddr) {
2542 /* SDP setup for the AD eval board */
2543 sdp_io_write(sd, 0x6f, 0x00); /* DDR mode */
2544 sdp_io_write(sd, 0x75, 0x0a); /* 128 MB memory size */
2545 sdp_io_write(sd, 0x7a, 0xa5); /* Timing Adjustment */
2546 sdp_io_write(sd, 0x7b, 0x8f); /* Timing Adjustment */
2547 sdp_io_write(sd, 0x60, 0x01); /* SDRAM reset */
2548 } else {
2549 sdp_io_write(sd, 0x75, 0x0a); /* 64 MB memory size ?*/
2550 sdp_io_write(sd, 0x74, 0x00); /* must be zero for sdr sdram */
2551 sdp_io_write(sd, 0x79, 0x33); /* CAS latency to 3,
2552 depends on memory */
2553 sdp_io_write(sd, 0x6f, 0x01); /* SDR mode */
2554 sdp_io_write(sd, 0x7a, 0xa5); /* Timing Adjustment */
2555 sdp_io_write(sd, 0x7b, 0x8f); /* Timing Adjustment */
2556 sdp_io_write(sd, 0x60, 0x01); /* SDRAM reset */
2558 } else {
2560 * Manual UG-214, rev 0 is bit confusing on this bit
2561 * but a '1' disables any signal if the Ram is active.
2563 sdp_io_write(sd, 0x29, 0x10); /* Tristate memory interface */
2566 select_input(sd, pdata->vid_std_select);
2568 enable_input(sd);
2570 /* disable I2C access to internal EDID ram from HDMI DDC ports */
2571 rep_write_and_or(sd, 0x77, 0xf3, 0x00);
2573 hdmi_write(sd, 0x69, 0xa3); /* HPA manual */
2574 /* HPA disable on port A and B */
2575 io_write_and_or(sd, 0x20, 0xcf, 0x00);
2577 /* LLC */
2578 /* Set phase to 16. TODO: get this from platform_data */
2579 io_write(sd, 0x19, 0x90);
2580 io_write(sd, 0x33, 0x40);
2582 /* interrupts */
2583 io_write(sd, 0x40, 0xf2); /* Configure INT1 */
2585 adv7842_irq_enable(sd, true);
2587 return v4l2_ctrl_handler_setup(sd->ctrl_handler);
2590 /* ----------------------------------------------------------------------- */
2592 static int adv7842_ddr_ram_test(struct v4l2_subdev *sd)
2595 * From ADV784x external Memory test.pdf
2597 * Reset must just been performed before running test.
2598 * Recommended to reset after test.
2600 int i;
2601 int pass = 0;
2602 int fail = 0;
2603 int complete = 0;
2605 io_write(sd, 0x00, 0x01); /* Program SDP 4x1 */
2606 io_write(sd, 0x01, 0x00); /* Program SDP mode */
2607 afe_write(sd, 0x80, 0x92); /* SDP Recommeneded Write */
2608 afe_write(sd, 0x9B, 0x01); /* SDP Recommeneded Write ADV7844ES1 */
2609 afe_write(sd, 0x9C, 0x60); /* SDP Recommeneded Write ADV7844ES1 */
2610 afe_write(sd, 0x9E, 0x02); /* SDP Recommeneded Write ADV7844ES1 */
2611 afe_write(sd, 0xA0, 0x0B); /* SDP Recommeneded Write ADV7844ES1 */
2612 afe_write(sd, 0xC3, 0x02); /* Memory BIST Initialisation */
2613 io_write(sd, 0x0C, 0x40); /* Power up ADV7844 */
2614 io_write(sd, 0x15, 0xBA); /* Enable outputs */
2615 sdp_write(sd, 0x12, 0x00); /* Disable 3D comb, Frame TBC & 3DNR */
2616 io_write(sd, 0xFF, 0x04); /* Reset memory controller */
2618 mdelay(5);
2620 sdp_write(sd, 0x12, 0x00); /* Disable 3D Comb, Frame TBC & 3DNR */
2621 sdp_io_write(sd, 0x2A, 0x01); /* Memory BIST Initialisation */
2622 sdp_io_write(sd, 0x7c, 0x19); /* Memory BIST Initialisation */
2623 sdp_io_write(sd, 0x80, 0x87); /* Memory BIST Initialisation */
2624 sdp_io_write(sd, 0x81, 0x4a); /* Memory BIST Initialisation */
2625 sdp_io_write(sd, 0x82, 0x2c); /* Memory BIST Initialisation */
2626 sdp_io_write(sd, 0x83, 0x0e); /* Memory BIST Initialisation */
2627 sdp_io_write(sd, 0x84, 0x94); /* Memory BIST Initialisation */
2628 sdp_io_write(sd, 0x85, 0x62); /* Memory BIST Initialisation */
2629 sdp_io_write(sd, 0x7d, 0x00); /* Memory BIST Initialisation */
2630 sdp_io_write(sd, 0x7e, 0x1a); /* Memory BIST Initialisation */
2632 mdelay(5);
2634 sdp_io_write(sd, 0xd9, 0xd5); /* Enable BIST Test */
2635 sdp_write(sd, 0x12, 0x05); /* Enable FRAME TBC & 3D COMB */
2637 mdelay(20);
2639 for (i = 0; i < 10; i++) {
2640 u8 result = sdp_io_read(sd, 0xdb);
2641 if (result & 0x10) {
2642 complete++;
2643 if (result & 0x20)
2644 fail++;
2645 else
2646 pass++;
2648 mdelay(20);
2651 v4l2_dbg(1, debug, sd,
2652 "Ram Test: completed %d of %d: pass %d, fail %d\n",
2653 complete, i, pass, fail);
2655 if (!complete || fail)
2656 return -EIO;
2657 return 0;
2660 static void adv7842_rewrite_i2c_addresses(struct v4l2_subdev *sd,
2661 struct adv7842_platform_data *pdata)
2663 io_write(sd, 0xf1, pdata->i2c_sdp << 1);
2664 io_write(sd, 0xf2, pdata->i2c_sdp_io << 1);
2665 io_write(sd, 0xf3, pdata->i2c_avlink << 1);
2666 io_write(sd, 0xf4, pdata->i2c_cec << 1);
2667 io_write(sd, 0xf5, pdata->i2c_infoframe << 1);
2669 io_write(sd, 0xf8, pdata->i2c_afe << 1);
2670 io_write(sd, 0xf9, pdata->i2c_repeater << 1);
2671 io_write(sd, 0xfa, pdata->i2c_edid << 1);
2672 io_write(sd, 0xfb, pdata->i2c_hdmi << 1);
2674 io_write(sd, 0xfd, pdata->i2c_cp << 1);
2675 io_write(sd, 0xfe, pdata->i2c_vdp << 1);
2678 static int adv7842_command_ram_test(struct v4l2_subdev *sd)
2680 struct i2c_client *client = v4l2_get_subdevdata(sd);
2681 struct adv7842_state *state = to_state(sd);
2682 struct adv7842_platform_data *pdata = client->dev.platform_data;
2683 int ret = 0;
2685 if (!pdata)
2686 return -ENODEV;
2688 if (!pdata->sd_ram_size || !pdata->sd_ram_ddr) {
2689 v4l2_info(sd, "no sdram or no ddr sdram\n");
2690 return -EINVAL;
2693 main_reset(sd);
2695 adv7842_rewrite_i2c_addresses(sd, pdata);
2697 /* run ram test */
2698 ret = adv7842_ddr_ram_test(sd);
2700 main_reset(sd);
2702 adv7842_rewrite_i2c_addresses(sd, pdata);
2704 /* and re-init chip and state */
2705 adv7842_core_init(sd);
2707 disable_input(sd);
2709 select_input(sd, state->vid_std_select);
2711 enable_input(sd);
2713 adv7842_s_dv_timings(sd, &state->timings);
2715 edid_write_vga_segment(sd);
2716 edid_write_hdmi_segment(sd, ADV7842_EDID_PORT_A);
2717 edid_write_hdmi_segment(sd, ADV7842_EDID_PORT_B);
2719 return ret;
2722 static long adv7842_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
2724 switch (cmd) {
2725 case ADV7842_CMD_RAM_TEST:
2726 return adv7842_command_ram_test(sd);
2728 return -ENOTTY;
2731 /* ----------------------------------------------------------------------- */
2733 static const struct v4l2_ctrl_ops adv7842_ctrl_ops = {
2734 .s_ctrl = adv7842_s_ctrl,
2737 static const struct v4l2_subdev_core_ops adv7842_core_ops = {
2738 .log_status = adv7842_log_status,
2739 .g_std = adv7842_g_std,
2740 .s_std = adv7842_s_std,
2741 .ioctl = adv7842_ioctl,
2742 .interrupt_service_routine = adv7842_isr,
2743 #ifdef CONFIG_VIDEO_ADV_DEBUG
2744 .g_register = adv7842_g_register,
2745 .s_register = adv7842_s_register,
2746 #endif
2749 static const struct v4l2_subdev_video_ops adv7842_video_ops = {
2750 .s_routing = adv7842_s_routing,
2751 .querystd = adv7842_querystd,
2752 .g_input_status = adv7842_g_input_status,
2753 .s_dv_timings = adv7842_s_dv_timings,
2754 .g_dv_timings = adv7842_g_dv_timings,
2755 .query_dv_timings = adv7842_query_dv_timings,
2756 .enum_dv_timings = adv7842_enum_dv_timings,
2757 .dv_timings_cap = adv7842_dv_timings_cap,
2758 .enum_mbus_fmt = adv7842_enum_mbus_fmt,
2759 .g_mbus_fmt = adv7842_g_mbus_fmt,
2760 .try_mbus_fmt = adv7842_g_mbus_fmt,
2761 .s_mbus_fmt = adv7842_g_mbus_fmt,
2764 static const struct v4l2_subdev_pad_ops adv7842_pad_ops = {
2765 .get_edid = adv7842_get_edid,
2766 .set_edid = adv7842_set_edid,
2769 static const struct v4l2_subdev_ops adv7842_ops = {
2770 .core = &adv7842_core_ops,
2771 .video = &adv7842_video_ops,
2772 .pad = &adv7842_pad_ops,
2775 /* -------------------------- custom ctrls ---------------------------------- */
2777 static const struct v4l2_ctrl_config adv7842_ctrl_analog_sampling_phase = {
2778 .ops = &adv7842_ctrl_ops,
2779 .id = V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE,
2780 .name = "Analog Sampling Phase",
2781 .type = V4L2_CTRL_TYPE_INTEGER,
2782 .min = 0,
2783 .max = 0x1f,
2784 .step = 1,
2785 .def = 0,
2788 static const struct v4l2_ctrl_config adv7842_ctrl_free_run_color_manual = {
2789 .ops = &adv7842_ctrl_ops,
2790 .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL,
2791 .name = "Free Running Color, Manual",
2792 .type = V4L2_CTRL_TYPE_BOOLEAN,
2793 .max = 1,
2794 .step = 1,
2795 .def = 1,
2798 static const struct v4l2_ctrl_config adv7842_ctrl_free_run_color = {
2799 .ops = &adv7842_ctrl_ops,
2800 .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR,
2801 .name = "Free Running Color",
2802 .type = V4L2_CTRL_TYPE_INTEGER,
2803 .max = 0xffffff,
2804 .step = 0x1,
2808 static void adv7842_unregister_clients(struct v4l2_subdev *sd)
2810 struct adv7842_state *state = to_state(sd);
2811 if (state->i2c_avlink)
2812 i2c_unregister_device(state->i2c_avlink);
2813 if (state->i2c_cec)
2814 i2c_unregister_device(state->i2c_cec);
2815 if (state->i2c_infoframe)
2816 i2c_unregister_device(state->i2c_infoframe);
2817 if (state->i2c_sdp_io)
2818 i2c_unregister_device(state->i2c_sdp_io);
2819 if (state->i2c_sdp)
2820 i2c_unregister_device(state->i2c_sdp);
2821 if (state->i2c_afe)
2822 i2c_unregister_device(state->i2c_afe);
2823 if (state->i2c_repeater)
2824 i2c_unregister_device(state->i2c_repeater);
2825 if (state->i2c_edid)
2826 i2c_unregister_device(state->i2c_edid);
2827 if (state->i2c_hdmi)
2828 i2c_unregister_device(state->i2c_hdmi);
2829 if (state->i2c_cp)
2830 i2c_unregister_device(state->i2c_cp);
2831 if (state->i2c_vdp)
2832 i2c_unregister_device(state->i2c_vdp);
2834 state->i2c_avlink = NULL;
2835 state->i2c_cec = NULL;
2836 state->i2c_infoframe = NULL;
2837 state->i2c_sdp_io = NULL;
2838 state->i2c_sdp = NULL;
2839 state->i2c_afe = NULL;
2840 state->i2c_repeater = NULL;
2841 state->i2c_edid = NULL;
2842 state->i2c_hdmi = NULL;
2843 state->i2c_cp = NULL;
2844 state->i2c_vdp = NULL;
2847 static struct i2c_client *adv7842_dummy_client(struct v4l2_subdev *sd, const char *desc,
2848 u8 addr, u8 io_reg)
2850 struct i2c_client *client = v4l2_get_subdevdata(sd);
2851 struct i2c_client *cp;
2853 io_write(sd, io_reg, addr << 1);
2855 if (addr == 0) {
2856 v4l2_err(sd, "no %s i2c addr configured\n", desc);
2857 return NULL;
2860 cp = i2c_new_dummy(client->adapter, io_read(sd, io_reg) >> 1);
2861 if (!cp)
2862 v4l2_err(sd, "register %s on i2c addr 0x%x failed\n", desc, addr);
2864 return cp;
2867 static int adv7842_register_clients(struct v4l2_subdev *sd)
2869 struct adv7842_state *state = to_state(sd);
2870 struct adv7842_platform_data *pdata = &state->pdata;
2872 state->i2c_avlink = adv7842_dummy_client(sd, "avlink", pdata->i2c_avlink, 0xf3);
2873 state->i2c_cec = adv7842_dummy_client(sd, "cec", pdata->i2c_cec, 0xf4);
2874 state->i2c_infoframe = adv7842_dummy_client(sd, "infoframe", pdata->i2c_infoframe, 0xf5);
2875 state->i2c_sdp_io = adv7842_dummy_client(sd, "sdp_io", pdata->i2c_sdp_io, 0xf2);
2876 state->i2c_sdp = adv7842_dummy_client(sd, "sdp", pdata->i2c_sdp, 0xf1);
2877 state->i2c_afe = adv7842_dummy_client(sd, "afe", pdata->i2c_afe, 0xf8);
2878 state->i2c_repeater = adv7842_dummy_client(sd, "repeater", pdata->i2c_repeater, 0xf9);
2879 state->i2c_edid = adv7842_dummy_client(sd, "edid", pdata->i2c_edid, 0xfa);
2880 state->i2c_hdmi = adv7842_dummy_client(sd, "hdmi", pdata->i2c_hdmi, 0xfb);
2881 state->i2c_cp = adv7842_dummy_client(sd, "cp", pdata->i2c_cp, 0xfd);
2882 state->i2c_vdp = adv7842_dummy_client(sd, "vdp", pdata->i2c_vdp, 0xfe);
2884 if (!state->i2c_avlink ||
2885 !state->i2c_cec ||
2886 !state->i2c_infoframe ||
2887 !state->i2c_sdp_io ||
2888 !state->i2c_sdp ||
2889 !state->i2c_afe ||
2890 !state->i2c_repeater ||
2891 !state->i2c_edid ||
2892 !state->i2c_hdmi ||
2893 !state->i2c_cp ||
2894 !state->i2c_vdp)
2895 return -1;
2897 return 0;
2900 static int adv7842_probe(struct i2c_client *client,
2901 const struct i2c_device_id *id)
2903 struct adv7842_state *state;
2904 struct adv7842_platform_data *pdata = client->dev.platform_data;
2905 struct v4l2_ctrl_handler *hdl;
2906 struct v4l2_subdev *sd;
2907 u16 rev;
2908 int err;
2910 /* Check if the adapter supports the needed features */
2911 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
2912 return -EIO;
2914 v4l_dbg(1, debug, client, "detecting adv7842 client on address 0x%x\n",
2915 client->addr << 1);
2917 if (!pdata) {
2918 v4l_err(client, "No platform data!\n");
2919 return -ENODEV;
2922 state = devm_kzalloc(&client->dev, sizeof(struct adv7842_state), GFP_KERNEL);
2923 if (!state) {
2924 v4l_err(client, "Could not allocate adv7842_state memory!\n");
2925 return -ENOMEM;
2928 /* platform data */
2929 state->pdata = *pdata;
2931 sd = &state->sd;
2932 v4l2_i2c_subdev_init(sd, client, &adv7842_ops);
2933 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
2934 state->mode = pdata->mode;
2936 state->hdmi_port_a = pdata->input == ADV7842_SELECT_HDMI_PORT_A;
2937 state->restart_stdi_once = true;
2939 /* i2c access to adv7842? */
2940 rev = adv_smbus_read_byte_data_check(client, 0xea, false) << 8 |
2941 adv_smbus_read_byte_data_check(client, 0xeb, false);
2942 if (rev != 0x2012) {
2943 v4l2_info(sd, "got rev=0x%04x on first read attempt\n", rev);
2944 rev = adv_smbus_read_byte_data_check(client, 0xea, false) << 8 |
2945 adv_smbus_read_byte_data_check(client, 0xeb, false);
2947 if (rev != 0x2012) {
2948 v4l2_info(sd, "not an adv7842 on address 0x%x (rev=0x%04x)\n",
2949 client->addr << 1, rev);
2950 return -ENODEV;
2953 if (pdata->chip_reset)
2954 main_reset(sd);
2956 /* control handlers */
2957 hdl = &state->hdl;
2958 v4l2_ctrl_handler_init(hdl, 6);
2960 /* add in ascending ID order */
2961 v4l2_ctrl_new_std(hdl, &adv7842_ctrl_ops,
2962 V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
2963 v4l2_ctrl_new_std(hdl, &adv7842_ctrl_ops,
2964 V4L2_CID_CONTRAST, 0, 255, 1, 128);
2965 v4l2_ctrl_new_std(hdl, &adv7842_ctrl_ops,
2966 V4L2_CID_SATURATION, 0, 255, 1, 128);
2967 v4l2_ctrl_new_std(hdl, &adv7842_ctrl_ops,
2968 V4L2_CID_HUE, 0, 128, 1, 0);
2970 /* custom controls */
2971 state->detect_tx_5v_ctrl = v4l2_ctrl_new_std(hdl, NULL,
2972 V4L2_CID_DV_RX_POWER_PRESENT, 0, 3, 0, 0);
2973 state->analog_sampling_phase_ctrl = v4l2_ctrl_new_custom(hdl,
2974 &adv7842_ctrl_analog_sampling_phase, NULL);
2975 state->free_run_color_ctrl_manual = v4l2_ctrl_new_custom(hdl,
2976 &adv7842_ctrl_free_run_color_manual, NULL);
2977 state->free_run_color_ctrl = v4l2_ctrl_new_custom(hdl,
2978 &adv7842_ctrl_free_run_color, NULL);
2979 state->rgb_quantization_range_ctrl =
2980 v4l2_ctrl_new_std_menu(hdl, &adv7842_ctrl_ops,
2981 V4L2_CID_DV_RX_RGB_RANGE, V4L2_DV_RGB_RANGE_FULL,
2982 0, V4L2_DV_RGB_RANGE_AUTO);
2983 sd->ctrl_handler = hdl;
2984 if (hdl->error) {
2985 err = hdl->error;
2986 goto err_hdl;
2988 state->detect_tx_5v_ctrl->is_private = true;
2989 state->rgb_quantization_range_ctrl->is_private = true;
2990 state->analog_sampling_phase_ctrl->is_private = true;
2991 state->free_run_color_ctrl_manual->is_private = true;
2992 state->free_run_color_ctrl->is_private = true;
2994 if (adv7842_s_detect_tx_5v_ctrl(sd)) {
2995 err = -ENODEV;
2996 goto err_hdl;
2999 if (adv7842_register_clients(sd) < 0) {
3000 err = -ENOMEM;
3001 v4l2_err(sd, "failed to create all i2c clients\n");
3002 goto err_i2c;
3005 /* work queues */
3006 state->work_queues = create_singlethread_workqueue(client->name);
3007 if (!state->work_queues) {
3008 v4l2_err(sd, "Could not create work queue\n");
3009 err = -ENOMEM;
3010 goto err_i2c;
3013 INIT_DELAYED_WORK(&state->delayed_work_enable_hotplug,
3014 adv7842_delayed_work_enable_hotplug);
3016 state->pad.flags = MEDIA_PAD_FL_SOURCE;
3017 err = media_entity_init(&sd->entity, 1, &state->pad, 0);
3018 if (err)
3019 goto err_work_queues;
3021 err = adv7842_core_init(sd);
3022 if (err)
3023 goto err_entity;
3025 v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
3026 client->addr << 1, client->adapter->name);
3027 return 0;
3029 err_entity:
3030 media_entity_cleanup(&sd->entity);
3031 err_work_queues:
3032 cancel_delayed_work(&state->delayed_work_enable_hotplug);
3033 destroy_workqueue(state->work_queues);
3034 err_i2c:
3035 adv7842_unregister_clients(sd);
3036 err_hdl:
3037 v4l2_ctrl_handler_free(hdl);
3038 return err;
3041 /* ----------------------------------------------------------------------- */
3043 static int adv7842_remove(struct i2c_client *client)
3045 struct v4l2_subdev *sd = i2c_get_clientdata(client);
3046 struct adv7842_state *state = to_state(sd);
3048 adv7842_irq_enable(sd, false);
3050 cancel_delayed_work(&state->delayed_work_enable_hotplug);
3051 destroy_workqueue(state->work_queues);
3052 v4l2_device_unregister_subdev(sd);
3053 media_entity_cleanup(&sd->entity);
3054 adv7842_unregister_clients(sd);
3055 v4l2_ctrl_handler_free(sd->ctrl_handler);
3056 return 0;
3059 /* ----------------------------------------------------------------------- */
3061 static struct i2c_device_id adv7842_id[] = {
3062 { "adv7842", 0 },
3065 MODULE_DEVICE_TABLE(i2c, adv7842_id);
3067 /* ----------------------------------------------------------------------- */
3069 static struct i2c_driver adv7842_driver = {
3070 .driver = {
3071 .owner = THIS_MODULE,
3072 .name = "adv7842",
3074 .probe = adv7842_probe,
3075 .remove = adv7842_remove,
3076 .id_table = adv7842_id,
3079 module_i2c_driver(adv7842_driver);