blob: 9f80d2e282cc80ccb2a77ff2c4703702580e0815 [file] [log] [blame]
Hans Verkuil54450f52012-07-18 05:45:16 -03001/*
2 * adv7604 - Analog Devices ADV7604 video decoder driver
3 *
4 * Copyright 2012 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
5 *
6 * This program is free software; you may redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
11 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
12 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
13 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
14 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
15 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
16 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17 * SOFTWARE.
18 *
19 */
20
21/*
22 * References (c = chapter, p = page):
23 * REF_01 - Analog devices, ADV7604, Register Settings Recommendations,
24 * Revision 2.5, June 2010
25 * REF_02 - Analog devices, Register map documentation, Documentation of
26 * the register maps, Software manual, Rev. F, June 2010
27 * REF_03 - Analog devices, ADV7604, Hardware Manual, Rev. F, August 2010
28 */
29
30
31#include <linux/kernel.h>
32#include <linux/module.h>
33#include <linux/slab.h>
34#include <linux/i2c.h>
35#include <linux/delay.h>
36#include <linux/videodev2.h>
37#include <linux/workqueue.h>
38#include <linux/v4l2-dv-timings.h>
39#include <media/v4l2-device.h>
40#include <media/v4l2-ctrls.h>
Hans Verkuil25764152013-07-29 08:40:56 -030041#include <media/v4l2-dv-timings.h>
Hans Verkuil54450f52012-07-18 05:45:16 -030042#include <media/adv7604.h>
43
44static int debug;
45module_param(debug, int, 0644);
46MODULE_PARM_DESC(debug, "debug level (0-2)");
47
48MODULE_DESCRIPTION("Analog Devices ADV7604 video decoder driver");
49MODULE_AUTHOR("Hans Verkuil <hans.verkuil@cisco.com>");
50MODULE_AUTHOR("Mats Randgaard <mats.randgaard@cisco.com>");
51MODULE_LICENSE("GPL");
52
53/* ADV7604 system clock frequency */
54#define ADV7604_fsc (28636360)
55
Hans Verkuil54450f52012-07-18 05:45:16 -030056/*
57 **********************************************************************
58 *
59 * Arrays with configuration parameters for the ADV7604
60 *
61 **********************************************************************
62 */
63struct adv7604_state {
64 struct adv7604_platform_data pdata;
65 struct v4l2_subdev sd;
66 struct media_pad pad;
67 struct v4l2_ctrl_handler hdl;
Mats Randgaard4a31a932013-12-10 09:45:00 -030068 enum adv7604_input_port selected_input;
Hans Verkuil54450f52012-07-18 05:45:16 -030069 struct v4l2_dv_timings timings;
Mats Randgaard4a31a932013-12-10 09:45:00 -030070 struct {
71 u8 edid[256];
72 u32 present;
73 unsigned blocks;
74 } edid;
Hans Verkuil54450f52012-07-18 05:45:16 -030075 struct v4l2_fract aspect_ratio;
76 u32 rgb_quantization_range;
77 struct workqueue_struct *work_queues;
78 struct delayed_work delayed_work_enable_hotplug;
79 bool connector_hdmi;
Hans Verkuilcf9afb12012-10-16 10:12:55 -030080 bool restart_stdi_once;
Mats Randgaard25a64ac2013-08-14 07:58:45 -030081 u32 prev_input_status;
Hans Verkuil54450f52012-07-18 05:45:16 -030082
83 /* i2c clients */
84 struct i2c_client *i2c_avlink;
85 struct i2c_client *i2c_cec;
86 struct i2c_client *i2c_infoframe;
87 struct i2c_client *i2c_esdp;
88 struct i2c_client *i2c_dpp;
89 struct i2c_client *i2c_afe;
90 struct i2c_client *i2c_repeater;
91 struct i2c_client *i2c_edid;
92 struct i2c_client *i2c_hdmi;
93 struct i2c_client *i2c_test;
94 struct i2c_client *i2c_cp;
95 struct i2c_client *i2c_vdp;
96
97 /* controls */
98 struct v4l2_ctrl *detect_tx_5v_ctrl;
99 struct v4l2_ctrl *analog_sampling_phase_ctrl;
100 struct v4l2_ctrl *free_run_color_manual_ctrl;
101 struct v4l2_ctrl *free_run_color_ctrl;
102 struct v4l2_ctrl *rgb_quantization_range_ctrl;
103};
104
105/* Supported CEA and DMT timings */
106static const struct v4l2_dv_timings adv7604_timings[] = {
107 V4L2_DV_BT_CEA_720X480P59_94,
108 V4L2_DV_BT_CEA_720X576P50,
109 V4L2_DV_BT_CEA_1280X720P24,
110 V4L2_DV_BT_CEA_1280X720P25,
Hans Verkuil54450f52012-07-18 05:45:16 -0300111 V4L2_DV_BT_CEA_1280X720P50,
112 V4L2_DV_BT_CEA_1280X720P60,
113 V4L2_DV_BT_CEA_1920X1080P24,
114 V4L2_DV_BT_CEA_1920X1080P25,
115 V4L2_DV_BT_CEA_1920X1080P30,
116 V4L2_DV_BT_CEA_1920X1080P50,
117 V4L2_DV_BT_CEA_1920X1080P60,
118
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300119 /* sorted by DMT ID */
Hans Verkuil54450f52012-07-18 05:45:16 -0300120 V4L2_DV_BT_DMT_640X350P85,
121 V4L2_DV_BT_DMT_640X400P85,
122 V4L2_DV_BT_DMT_720X400P85,
123 V4L2_DV_BT_DMT_640X480P60,
124 V4L2_DV_BT_DMT_640X480P72,
125 V4L2_DV_BT_DMT_640X480P75,
126 V4L2_DV_BT_DMT_640X480P85,
127 V4L2_DV_BT_DMT_800X600P56,
128 V4L2_DV_BT_DMT_800X600P60,
129 V4L2_DV_BT_DMT_800X600P72,
130 V4L2_DV_BT_DMT_800X600P75,
131 V4L2_DV_BT_DMT_800X600P85,
132 V4L2_DV_BT_DMT_848X480P60,
133 V4L2_DV_BT_DMT_1024X768P60,
134 V4L2_DV_BT_DMT_1024X768P70,
135 V4L2_DV_BT_DMT_1024X768P75,
136 V4L2_DV_BT_DMT_1024X768P85,
137 V4L2_DV_BT_DMT_1152X864P75,
138 V4L2_DV_BT_DMT_1280X768P60_RB,
139 V4L2_DV_BT_DMT_1280X768P60,
140 V4L2_DV_BT_DMT_1280X768P75,
141 V4L2_DV_BT_DMT_1280X768P85,
142 V4L2_DV_BT_DMT_1280X800P60_RB,
143 V4L2_DV_BT_DMT_1280X800P60,
144 V4L2_DV_BT_DMT_1280X800P75,
145 V4L2_DV_BT_DMT_1280X800P85,
146 V4L2_DV_BT_DMT_1280X960P60,
147 V4L2_DV_BT_DMT_1280X960P85,
148 V4L2_DV_BT_DMT_1280X1024P60,
149 V4L2_DV_BT_DMT_1280X1024P75,
150 V4L2_DV_BT_DMT_1280X1024P85,
151 V4L2_DV_BT_DMT_1360X768P60,
152 V4L2_DV_BT_DMT_1400X1050P60_RB,
153 V4L2_DV_BT_DMT_1400X1050P60,
154 V4L2_DV_BT_DMT_1400X1050P75,
155 V4L2_DV_BT_DMT_1400X1050P85,
156 V4L2_DV_BT_DMT_1440X900P60_RB,
157 V4L2_DV_BT_DMT_1440X900P60,
158 V4L2_DV_BT_DMT_1600X1200P60,
159 V4L2_DV_BT_DMT_1680X1050P60_RB,
160 V4L2_DV_BT_DMT_1680X1050P60,
161 V4L2_DV_BT_DMT_1792X1344P60,
162 V4L2_DV_BT_DMT_1856X1392P60,
163 V4L2_DV_BT_DMT_1920X1200P60_RB,
Martin Bugge547ed542013-12-05 10:01:17 -0300164 V4L2_DV_BT_DMT_1366X768P60_RB,
Hans Verkuil54450f52012-07-18 05:45:16 -0300165 V4L2_DV_BT_DMT_1366X768P60,
166 V4L2_DV_BT_DMT_1920X1080P60,
167 { },
168};
169
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300170struct adv7604_video_standards {
171 struct v4l2_dv_timings timings;
172 u8 vid_std;
173 u8 v_freq;
174};
175
176/* sorted by number of lines */
177static const struct adv7604_video_standards adv7604_prim_mode_comp[] = {
178 /* { V4L2_DV_BT_CEA_720X480P59_94, 0x0a, 0x00 }, TODO flickering */
179 { V4L2_DV_BT_CEA_720X576P50, 0x0b, 0x00 },
180 { V4L2_DV_BT_CEA_1280X720P50, 0x19, 0x01 },
181 { V4L2_DV_BT_CEA_1280X720P60, 0x19, 0x00 },
182 { V4L2_DV_BT_CEA_1920X1080P24, 0x1e, 0x04 },
183 { V4L2_DV_BT_CEA_1920X1080P25, 0x1e, 0x03 },
184 { V4L2_DV_BT_CEA_1920X1080P30, 0x1e, 0x02 },
185 { V4L2_DV_BT_CEA_1920X1080P50, 0x1e, 0x01 },
186 { V4L2_DV_BT_CEA_1920X1080P60, 0x1e, 0x00 },
187 /* TODO add 1920x1080P60_RB (CVT timing) */
188 { },
189};
190
191/* sorted by number of lines */
192static const struct adv7604_video_standards adv7604_prim_mode_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 { V4L2_DV_BT_DMT_1360X768P60, 0x12, 0x00 },
209 { V4L2_DV_BT_DMT_1366X768P60, 0x13, 0x00 },
210 { V4L2_DV_BT_DMT_1400X1050P60, 0x14, 0x00 },
211 { V4L2_DV_BT_DMT_1400X1050P75, 0x15, 0x00 },
212 { V4L2_DV_BT_DMT_1600X1200P60, 0x16, 0x00 }, /* TODO not tested */
213 /* TODO add 1600X1200P60_RB (not a DMT timing) */
214 { V4L2_DV_BT_DMT_1680X1050P60, 0x18, 0x00 },
215 { V4L2_DV_BT_DMT_1920X1200P60_RB, 0x19, 0x00 }, /* TODO not tested */
216 { },
217};
218
219/* sorted by number of lines */
220static const struct adv7604_video_standards adv7604_prim_mode_hdmi_comp[] = {
221 { V4L2_DV_BT_CEA_720X480P59_94, 0x0a, 0x00 },
222 { V4L2_DV_BT_CEA_720X576P50, 0x0b, 0x00 },
223 { V4L2_DV_BT_CEA_1280X720P50, 0x13, 0x01 },
224 { V4L2_DV_BT_CEA_1280X720P60, 0x13, 0x00 },
225 { V4L2_DV_BT_CEA_1920X1080P24, 0x1e, 0x04 },
226 { V4L2_DV_BT_CEA_1920X1080P25, 0x1e, 0x03 },
227 { V4L2_DV_BT_CEA_1920X1080P30, 0x1e, 0x02 },
228 { V4L2_DV_BT_CEA_1920X1080P50, 0x1e, 0x01 },
229 { V4L2_DV_BT_CEA_1920X1080P60, 0x1e, 0x00 },
230 { },
231};
232
233/* sorted by number of lines */
234static const struct adv7604_video_standards adv7604_prim_mode_hdmi_gr[] = {
235 { V4L2_DV_BT_DMT_640X480P60, 0x08, 0x00 },
236 { V4L2_DV_BT_DMT_640X480P72, 0x09, 0x00 },
237 { V4L2_DV_BT_DMT_640X480P75, 0x0a, 0x00 },
238 { V4L2_DV_BT_DMT_640X480P85, 0x0b, 0x00 },
239 { V4L2_DV_BT_DMT_800X600P56, 0x00, 0x00 },
240 { V4L2_DV_BT_DMT_800X600P60, 0x01, 0x00 },
241 { V4L2_DV_BT_DMT_800X600P72, 0x02, 0x00 },
242 { V4L2_DV_BT_DMT_800X600P75, 0x03, 0x00 },
243 { V4L2_DV_BT_DMT_800X600P85, 0x04, 0x00 },
244 { V4L2_DV_BT_DMT_1024X768P60, 0x0c, 0x00 },
245 { V4L2_DV_BT_DMT_1024X768P70, 0x0d, 0x00 },
246 { V4L2_DV_BT_DMT_1024X768P75, 0x0e, 0x00 },
247 { V4L2_DV_BT_DMT_1024X768P85, 0x0f, 0x00 },
248 { V4L2_DV_BT_DMT_1280X1024P60, 0x05, 0x00 },
249 { V4L2_DV_BT_DMT_1280X1024P75, 0x06, 0x00 },
250 { },
251};
252
Hans Verkuil54450f52012-07-18 05:45:16 -0300253/* ----------------------------------------------------------------------- */
254
255static inline struct adv7604_state *to_state(struct v4l2_subdev *sd)
256{
257 return container_of(sd, struct adv7604_state, sd);
258}
259
260static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
261{
262 return &container_of(ctrl->handler, struct adv7604_state, hdl)->sd;
263}
264
265static inline unsigned hblanking(const struct v4l2_bt_timings *t)
266{
Hans Verkuileacf8f92013-07-29 08:40:59 -0300267 return V4L2_DV_BT_BLANKING_WIDTH(t);
Hans Verkuil54450f52012-07-18 05:45:16 -0300268}
269
270static inline unsigned htotal(const struct v4l2_bt_timings *t)
271{
Hans Verkuileacf8f92013-07-29 08:40:59 -0300272 return V4L2_DV_BT_FRAME_WIDTH(t);
Hans Verkuil54450f52012-07-18 05:45:16 -0300273}
274
275static inline unsigned vblanking(const struct v4l2_bt_timings *t)
276{
Hans Verkuileacf8f92013-07-29 08:40:59 -0300277 return V4L2_DV_BT_BLANKING_HEIGHT(t);
Hans Verkuil54450f52012-07-18 05:45:16 -0300278}
279
280static inline unsigned vtotal(const struct v4l2_bt_timings *t)
281{
Hans Verkuileacf8f92013-07-29 08:40:59 -0300282 return V4L2_DV_BT_FRAME_HEIGHT(t);
Hans Verkuil54450f52012-07-18 05:45:16 -0300283}
284
285/* ----------------------------------------------------------------------- */
286
287static s32 adv_smbus_read_byte_data_check(struct i2c_client *client,
288 u8 command, bool check)
289{
290 union i2c_smbus_data data;
291
292 if (!i2c_smbus_xfer(client->adapter, client->addr, client->flags,
293 I2C_SMBUS_READ, command,
294 I2C_SMBUS_BYTE_DATA, &data))
295 return data.byte;
296 if (check)
297 v4l_err(client, "error reading %02x, %02x\n",
298 client->addr, command);
299 return -EIO;
300}
301
302static s32 adv_smbus_read_byte_data(struct i2c_client *client, u8 command)
303{
304 return adv_smbus_read_byte_data_check(client, command, true);
305}
306
307static s32 adv_smbus_write_byte_data(struct i2c_client *client,
308 u8 command, u8 value)
309{
310 union i2c_smbus_data data;
311 int err;
312 int i;
313
314 data.byte = value;
315 for (i = 0; i < 3; i++) {
316 err = i2c_smbus_xfer(client->adapter, client->addr,
317 client->flags,
318 I2C_SMBUS_WRITE, command,
319 I2C_SMBUS_BYTE_DATA, &data);
320 if (!err)
321 break;
322 }
323 if (err < 0)
324 v4l_err(client, "error writing %02x, %02x, %02x\n",
325 client->addr, command, value);
326 return err;
327}
328
329static s32 adv_smbus_write_i2c_block_data(struct i2c_client *client,
330 u8 command, unsigned length, const u8 *values)
331{
332 union i2c_smbus_data data;
333
334 if (length > I2C_SMBUS_BLOCK_MAX)
335 length = I2C_SMBUS_BLOCK_MAX;
336 data.block[0] = length;
337 memcpy(data.block + 1, values, length);
338 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
339 I2C_SMBUS_WRITE, command,
340 I2C_SMBUS_I2C_BLOCK_DATA, &data);
341}
342
343/* ----------------------------------------------------------------------- */
344
345static inline int io_read(struct v4l2_subdev *sd, u8 reg)
346{
347 struct i2c_client *client = v4l2_get_subdevdata(sd);
348
349 return adv_smbus_read_byte_data(client, reg);
350}
351
352static inline int io_write(struct v4l2_subdev *sd, u8 reg, u8 val)
353{
354 struct i2c_client *client = v4l2_get_subdevdata(sd);
355
356 return adv_smbus_write_byte_data(client, reg, val);
357}
358
359static inline int io_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
360{
361 return io_write(sd, reg, (io_read(sd, reg) & mask) | val);
362}
363
364static inline int avlink_read(struct v4l2_subdev *sd, u8 reg)
365{
366 struct adv7604_state *state = to_state(sd);
367
368 return adv_smbus_read_byte_data(state->i2c_avlink, reg);
369}
370
371static inline int avlink_write(struct v4l2_subdev *sd, u8 reg, u8 val)
372{
373 struct adv7604_state *state = to_state(sd);
374
375 return adv_smbus_write_byte_data(state->i2c_avlink, reg, val);
376}
377
378static inline int cec_read(struct v4l2_subdev *sd, u8 reg)
379{
380 struct adv7604_state *state = to_state(sd);
381
382 return adv_smbus_read_byte_data(state->i2c_cec, reg);
383}
384
385static inline int cec_write(struct v4l2_subdev *sd, u8 reg, u8 val)
386{
387 struct adv7604_state *state = to_state(sd);
388
389 return adv_smbus_write_byte_data(state->i2c_cec, reg, val);
390}
391
392static inline int cec_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
393{
394 return cec_write(sd, reg, (cec_read(sd, reg) & mask) | val);
395}
396
397static inline int infoframe_read(struct v4l2_subdev *sd, u8 reg)
398{
399 struct adv7604_state *state = to_state(sd);
400
401 return adv_smbus_read_byte_data(state->i2c_infoframe, reg);
402}
403
404static inline int infoframe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
405{
406 struct adv7604_state *state = to_state(sd);
407
408 return adv_smbus_write_byte_data(state->i2c_infoframe, reg, val);
409}
410
411static inline int esdp_read(struct v4l2_subdev *sd, u8 reg)
412{
413 struct adv7604_state *state = to_state(sd);
414
415 return adv_smbus_read_byte_data(state->i2c_esdp, reg);
416}
417
418static inline int esdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
419{
420 struct adv7604_state *state = to_state(sd);
421
422 return adv_smbus_write_byte_data(state->i2c_esdp, reg, val);
423}
424
425static inline int dpp_read(struct v4l2_subdev *sd, u8 reg)
426{
427 struct adv7604_state *state = to_state(sd);
428
429 return adv_smbus_read_byte_data(state->i2c_dpp, reg);
430}
431
432static inline int dpp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
433{
434 struct adv7604_state *state = to_state(sd);
435
436 return adv_smbus_write_byte_data(state->i2c_dpp, reg, val);
437}
438
439static inline int afe_read(struct v4l2_subdev *sd, u8 reg)
440{
441 struct adv7604_state *state = to_state(sd);
442
443 return adv_smbus_read_byte_data(state->i2c_afe, reg);
444}
445
446static inline int afe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
447{
448 struct adv7604_state *state = to_state(sd);
449
450 return adv_smbus_write_byte_data(state->i2c_afe, reg, val);
451}
452
453static inline int rep_read(struct v4l2_subdev *sd, u8 reg)
454{
455 struct adv7604_state *state = to_state(sd);
456
457 return adv_smbus_read_byte_data(state->i2c_repeater, reg);
458}
459
460static inline int rep_write(struct v4l2_subdev *sd, u8 reg, u8 val)
461{
462 struct adv7604_state *state = to_state(sd);
463
464 return adv_smbus_write_byte_data(state->i2c_repeater, reg, val);
465}
466
467static inline int rep_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
468{
469 return rep_write(sd, reg, (rep_read(sd, reg) & mask) | val);
470}
471
472static inline int edid_read(struct v4l2_subdev *sd, u8 reg)
473{
474 struct adv7604_state *state = to_state(sd);
475
476 return adv_smbus_read_byte_data(state->i2c_edid, reg);
477}
478
479static inline int edid_write(struct v4l2_subdev *sd, u8 reg, u8 val)
480{
481 struct adv7604_state *state = to_state(sd);
482
483 return adv_smbus_write_byte_data(state->i2c_edid, reg, val);
484}
485
486static inline int edid_read_block(struct v4l2_subdev *sd, unsigned len, u8 *val)
487{
488 struct adv7604_state *state = to_state(sd);
489 struct i2c_client *client = state->i2c_edid;
490 u8 msgbuf0[1] = { 0 };
491 u8 msgbuf1[256];
Shubhrajyoti D09f29672012-10-25 01:02:36 -0300492 struct i2c_msg msg[2] = {
493 {
494 .addr = client->addr,
495 .len = 1,
496 .buf = msgbuf0
497 },
498 {
499 .addr = client->addr,
500 .flags = I2C_M_RD,
501 .len = len,
502 .buf = msgbuf1
503 },
504 };
Hans Verkuil54450f52012-07-18 05:45:16 -0300505
506 if (i2c_transfer(client->adapter, msg, 2) < 0)
507 return -EIO;
508 memcpy(val, msgbuf1, len);
509 return 0;
510}
511
512static void adv7604_delayed_work_enable_hotplug(struct work_struct *work)
513{
514 struct delayed_work *dwork = to_delayed_work(work);
515 struct adv7604_state *state = container_of(dwork, struct adv7604_state,
516 delayed_work_enable_hotplug);
517 struct v4l2_subdev *sd = &state->sd;
518
519 v4l2_dbg(2, debug, sd, "%s: enable hotplug\n", __func__);
520
Mats Randgaard4a31a932013-12-10 09:45:00 -0300521 v4l2_subdev_notify(sd, ADV7604_HOTPLUG, (void *)&state->edid.present);
Hans Verkuil54450f52012-07-18 05:45:16 -0300522}
523
524static inline int edid_write_block(struct v4l2_subdev *sd,
525 unsigned len, const u8 *val)
526{
527 struct i2c_client *client = v4l2_get_subdevdata(sd);
528 struct adv7604_state *state = to_state(sd);
529 int err = 0;
530 int i;
531
532 v4l2_dbg(2, debug, sd, "%s: write EDID block (%d byte)\n", __func__, len);
533
Hans Verkuil54450f52012-07-18 05:45:16 -0300534 /* Disables I2C access to internal EDID ram from DDC port */
535 rep_write_and_or(sd, 0x77, 0xf0, 0x0);
536
537 for (i = 0; !err && i < len; i += I2C_SMBUS_BLOCK_MAX)
538 err = adv_smbus_write_i2c_block_data(state->i2c_edid, i,
539 I2C_SMBUS_BLOCK_MAX, val + i);
540 if (err)
541 return err;
542
543 /* adv7604 calculates the checksums and enables I2C access to internal
Mats Randgaard4a31a932013-12-10 09:45:00 -0300544 EDID RAM from DDC port. */
545 rep_write_and_or(sd, 0x77, 0xf0, state->edid.present);
Hans Verkuil54450f52012-07-18 05:45:16 -0300546
547 for (i = 0; i < 1000; i++) {
Mats Randgaard4a31a932013-12-10 09:45:00 -0300548 if (rep_read(sd, 0x7d) & state->edid.present)
Hans Verkuil54450f52012-07-18 05:45:16 -0300549 break;
550 mdelay(1);
551 }
552 if (i == 1000) {
Mats Randgaard4a31a932013-12-10 09:45:00 -0300553 v4l_err(client, "error enabling edid (0x%x)\n", state->edid.present);
Hans Verkuil54450f52012-07-18 05:45:16 -0300554 return -EIO;
555 }
556
Hans Verkuil54450f52012-07-18 05:45:16 -0300557 return 0;
558}
559
560static inline int hdmi_read(struct v4l2_subdev *sd, u8 reg)
561{
562 struct adv7604_state *state = to_state(sd);
563
564 return adv_smbus_read_byte_data(state->i2c_hdmi, reg);
565}
566
567static inline int hdmi_write(struct v4l2_subdev *sd, u8 reg, u8 val)
568{
569 struct adv7604_state *state = to_state(sd);
570
571 return adv_smbus_write_byte_data(state->i2c_hdmi, reg, val);
572}
573
Mats Randgaard4a31a932013-12-10 09:45:00 -0300574static inline int hdmi_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
575{
576 return hdmi_write(sd, reg, (hdmi_read(sd, reg) & mask) | val);
577}
578
Hans Verkuil54450f52012-07-18 05:45:16 -0300579static inline int test_read(struct v4l2_subdev *sd, u8 reg)
580{
581 struct adv7604_state *state = to_state(sd);
582
583 return adv_smbus_read_byte_data(state->i2c_test, reg);
584}
585
586static inline int test_write(struct v4l2_subdev *sd, u8 reg, u8 val)
587{
588 struct adv7604_state *state = to_state(sd);
589
590 return adv_smbus_write_byte_data(state->i2c_test, reg, val);
591}
592
593static inline int cp_read(struct v4l2_subdev *sd, u8 reg)
594{
595 struct adv7604_state *state = to_state(sd);
596
597 return adv_smbus_read_byte_data(state->i2c_cp, reg);
598}
599
600static inline int cp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
601{
602 struct adv7604_state *state = to_state(sd);
603
604 return adv_smbus_write_byte_data(state->i2c_cp, reg, val);
605}
606
607static inline int cp_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
608{
609 return cp_write(sd, reg, (cp_read(sd, reg) & mask) | val);
610}
611
612static inline int vdp_read(struct v4l2_subdev *sd, u8 reg)
613{
614 struct adv7604_state *state = to_state(sd);
615
616 return adv_smbus_read_byte_data(state->i2c_vdp, reg);
617}
618
619static inline int vdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
620{
621 struct adv7604_state *state = to_state(sd);
622
623 return adv_smbus_write_byte_data(state->i2c_vdp, reg, val);
624}
625
626/* ----------------------------------------------------------------------- */
627
Mats Randgaard4a31a932013-12-10 09:45:00 -0300628static inline bool is_analog_input(struct v4l2_subdev *sd)
629{
630 struct adv7604_state *state = to_state(sd);
631
632 return state->selected_input == ADV7604_INPUT_VGA_RGB ||
633 state->selected_input == ADV7604_INPUT_VGA_COMP;
634}
635
636static inline bool is_digital_input(struct v4l2_subdev *sd)
637{
638 struct adv7604_state *state = to_state(sd);
639
640 return state->selected_input == ADV7604_INPUT_HDMI_PORT_A ||
641 state->selected_input == ADV7604_INPUT_HDMI_PORT_B ||
642 state->selected_input == ADV7604_INPUT_HDMI_PORT_C ||
643 state->selected_input == ADV7604_INPUT_HDMI_PORT_D;
644}
645
646/* ----------------------------------------------------------------------- */
647
Hans Verkuil54450f52012-07-18 05:45:16 -0300648#ifdef CONFIG_VIDEO_ADV_DEBUG
649static void adv7604_inv_register(struct v4l2_subdev *sd)
650{
651 v4l2_info(sd, "0x000-0x0ff: IO Map\n");
652 v4l2_info(sd, "0x100-0x1ff: AVLink Map\n");
653 v4l2_info(sd, "0x200-0x2ff: CEC Map\n");
654 v4l2_info(sd, "0x300-0x3ff: InfoFrame Map\n");
655 v4l2_info(sd, "0x400-0x4ff: ESDP Map\n");
656 v4l2_info(sd, "0x500-0x5ff: DPP Map\n");
657 v4l2_info(sd, "0x600-0x6ff: AFE Map\n");
658 v4l2_info(sd, "0x700-0x7ff: Repeater Map\n");
659 v4l2_info(sd, "0x800-0x8ff: EDID Map\n");
660 v4l2_info(sd, "0x900-0x9ff: HDMI Map\n");
661 v4l2_info(sd, "0xa00-0xaff: Test Map\n");
662 v4l2_info(sd, "0xb00-0xbff: CP Map\n");
663 v4l2_info(sd, "0xc00-0xcff: VDP Map\n");
664}
665
666static int adv7604_g_register(struct v4l2_subdev *sd,
667 struct v4l2_dbg_register *reg)
668{
Hans Verkuil54450f52012-07-18 05:45:16 -0300669 reg->size = 1;
670 switch (reg->reg >> 8) {
671 case 0:
672 reg->val = io_read(sd, reg->reg & 0xff);
673 break;
674 case 1:
675 reg->val = avlink_read(sd, reg->reg & 0xff);
676 break;
677 case 2:
678 reg->val = cec_read(sd, reg->reg & 0xff);
679 break;
680 case 3:
681 reg->val = infoframe_read(sd, reg->reg & 0xff);
682 break;
683 case 4:
684 reg->val = esdp_read(sd, reg->reg & 0xff);
685 break;
686 case 5:
687 reg->val = dpp_read(sd, reg->reg & 0xff);
688 break;
689 case 6:
690 reg->val = afe_read(sd, reg->reg & 0xff);
691 break;
692 case 7:
693 reg->val = rep_read(sd, reg->reg & 0xff);
694 break;
695 case 8:
696 reg->val = edid_read(sd, reg->reg & 0xff);
697 break;
698 case 9:
699 reg->val = hdmi_read(sd, reg->reg & 0xff);
700 break;
701 case 0xa:
702 reg->val = test_read(sd, reg->reg & 0xff);
703 break;
704 case 0xb:
705 reg->val = cp_read(sd, reg->reg & 0xff);
706 break;
707 case 0xc:
708 reg->val = vdp_read(sd, reg->reg & 0xff);
709 break;
710 default:
711 v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
712 adv7604_inv_register(sd);
713 break;
714 }
715 return 0;
716}
717
718static int adv7604_s_register(struct v4l2_subdev *sd,
Hans Verkuil977ba3b2013-03-24 08:28:46 -0300719 const struct v4l2_dbg_register *reg)
Hans Verkuil54450f52012-07-18 05:45:16 -0300720{
Hans Verkuil54450f52012-07-18 05:45:16 -0300721 switch (reg->reg >> 8) {
722 case 0:
723 io_write(sd, reg->reg & 0xff, reg->val & 0xff);
724 break;
725 case 1:
726 avlink_write(sd, reg->reg & 0xff, reg->val & 0xff);
727 break;
728 case 2:
729 cec_write(sd, reg->reg & 0xff, reg->val & 0xff);
730 break;
731 case 3:
732 infoframe_write(sd, reg->reg & 0xff, reg->val & 0xff);
733 break;
734 case 4:
735 esdp_write(sd, reg->reg & 0xff, reg->val & 0xff);
736 break;
737 case 5:
738 dpp_write(sd, reg->reg & 0xff, reg->val & 0xff);
739 break;
740 case 6:
741 afe_write(sd, reg->reg & 0xff, reg->val & 0xff);
742 break;
743 case 7:
744 rep_write(sd, reg->reg & 0xff, reg->val & 0xff);
745 break;
746 case 8:
747 edid_write(sd, reg->reg & 0xff, reg->val & 0xff);
748 break;
749 case 9:
750 hdmi_write(sd, reg->reg & 0xff, reg->val & 0xff);
751 break;
752 case 0xa:
753 test_write(sd, reg->reg & 0xff, reg->val & 0xff);
754 break;
755 case 0xb:
756 cp_write(sd, reg->reg & 0xff, reg->val & 0xff);
757 break;
758 case 0xc:
759 vdp_write(sd, reg->reg & 0xff, reg->val & 0xff);
760 break;
761 default:
762 v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
763 adv7604_inv_register(sd);
764 break;
765 }
766 return 0;
767}
768#endif
769
770static int adv7604_s_detect_tx_5v_ctrl(struct v4l2_subdev *sd)
771{
772 struct adv7604_state *state = to_state(sd);
Mats Randgaard4a31a932013-12-10 09:45:00 -0300773 u8 reg_io_6f = io_read(sd, 0x6f);
Hans Verkuil54450f52012-07-18 05:45:16 -0300774
Hans Verkuil54450f52012-07-18 05:45:16 -0300775 return v4l2_ctrl_s_ctrl(state->detect_tx_5v_ctrl,
Mats Randgaard4a31a932013-12-10 09:45:00 -0300776 ((reg_io_6f & 0x10) >> 4) |
777 ((reg_io_6f & 0x08) >> 2) |
778 (reg_io_6f & 0x04) |
779 ((reg_io_6f & 0x02) << 2));
Hans Verkuil54450f52012-07-18 05:45:16 -0300780}
781
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300782static int find_and_set_predefined_video_timings(struct v4l2_subdev *sd,
783 u8 prim_mode,
784 const struct adv7604_video_standards *predef_vid_timings,
785 const struct v4l2_dv_timings *timings)
Hans Verkuil54450f52012-07-18 05:45:16 -0300786{
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300787 int i;
788
789 for (i = 0; predef_vid_timings[i].timings.bt.width; i++) {
Hans Verkuilef1ed8f2013-08-15 08:28:47 -0300790 if (!v4l2_match_dv_timings(timings, &predef_vid_timings[i].timings,
Mats Randgaard4a31a932013-12-10 09:45:00 -0300791 is_digital_input(sd) ? 250000 : 1000000))
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300792 continue;
793 io_write(sd, 0x00, predef_vid_timings[i].vid_std); /* video std */
794 io_write(sd, 0x01, (predef_vid_timings[i].v_freq << 4) +
795 prim_mode); /* v_freq and prim mode */
796 return 0;
797 }
798
799 return -1;
800}
801
802static int configure_predefined_video_timings(struct v4l2_subdev *sd,
803 struct v4l2_dv_timings *timings)
804{
805 struct adv7604_state *state = to_state(sd);
806 int err;
807
808 v4l2_dbg(1, debug, sd, "%s", __func__);
809
810 /* reset to default values */
811 io_write(sd, 0x16, 0x43);
812 io_write(sd, 0x17, 0x5a);
813 /* disable embedded syncs for auto graphics mode */
814 cp_write_and_or(sd, 0x81, 0xef, 0x00);
815 cp_write(sd, 0x8f, 0x00);
816 cp_write(sd, 0x90, 0x00);
817 cp_write(sd, 0xa2, 0x00);
818 cp_write(sd, 0xa3, 0x00);
819 cp_write(sd, 0xa4, 0x00);
820 cp_write(sd, 0xa5, 0x00);
821 cp_write(sd, 0xa6, 0x00);
822 cp_write(sd, 0xa7, 0x00);
823 cp_write(sd, 0xab, 0x00);
824 cp_write(sd, 0xac, 0x00);
825
Mats Randgaard4a31a932013-12-10 09:45:00 -0300826 if (is_analog_input(sd)) {
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300827 err = find_and_set_predefined_video_timings(sd,
828 0x01, adv7604_prim_mode_comp, timings);
829 if (err)
830 err = find_and_set_predefined_video_timings(sd,
831 0x02, adv7604_prim_mode_gr, timings);
Mats Randgaard4a31a932013-12-10 09:45:00 -0300832 } else if (is_digital_input(sd)) {
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300833 err = find_and_set_predefined_video_timings(sd,
834 0x05, adv7604_prim_mode_hdmi_comp, timings);
835 if (err)
836 err = find_and_set_predefined_video_timings(sd,
837 0x06, adv7604_prim_mode_hdmi_gr, timings);
Mats Randgaard4a31a932013-12-10 09:45:00 -0300838 } else {
839 v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
840 __func__, state->selected_input);
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300841 err = -1;
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300842 }
843
844
845 return err;
846}
847
848static void configure_custom_video_timings(struct v4l2_subdev *sd,
849 const struct v4l2_bt_timings *bt)
850{
851 struct adv7604_state *state = to_state(sd);
Hans Verkuil54450f52012-07-18 05:45:16 -0300852 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300853 u32 width = htotal(bt);
854 u32 height = vtotal(bt);
855 u16 cp_start_sav = bt->hsync + bt->hbackporch - 4;
856 u16 cp_start_eav = width - bt->hfrontporch;
857 u16 cp_start_vbi = height - bt->vfrontporch;
858 u16 cp_end_vbi = bt->vsync + bt->vbackporch;
859 u16 ch1_fr_ll = (((u32)bt->pixelclock / 100) > 0) ?
860 ((width * (ADV7604_fsc / 100)) / ((u32)bt->pixelclock / 100)) : 0;
861 const u8 pll[2] = {
862 0xc0 | ((width >> 8) & 0x1f),
863 width & 0xff
864 };
Hans Verkuil54450f52012-07-18 05:45:16 -0300865
866 v4l2_dbg(2, debug, sd, "%s\n", __func__);
867
Mats Randgaard4a31a932013-12-10 09:45:00 -0300868 if (is_analog_input(sd)) {
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300869 /* auto graphics */
870 io_write(sd, 0x00, 0x07); /* video std */
871 io_write(sd, 0x01, 0x02); /* prim mode */
872 /* enable embedded syncs for auto graphics mode */
873 cp_write_and_or(sd, 0x81, 0xef, 0x10);
Hans Verkuil54450f52012-07-18 05:45:16 -0300874
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300875 /* Should only be set in auto-graphics mode [REF_02, p. 91-92] */
Hans Verkuil54450f52012-07-18 05:45:16 -0300876 /* setup PLL_DIV_MAN_EN and PLL_DIV_RATIO */
877 /* IO-map reg. 0x16 and 0x17 should be written in sequence */
Mats Randgaard4a31a932013-12-10 09:45:00 -0300878 if (adv_smbus_write_i2c_block_data(client, 0x16, 2, pll))
Hans Verkuil54450f52012-07-18 05:45:16 -0300879 v4l2_err(sd, "writing to reg 0x16 and 0x17 failed\n");
Hans Verkuil54450f52012-07-18 05:45:16 -0300880
881 /* active video - horizontal timing */
Hans Verkuil54450f52012-07-18 05:45:16 -0300882 cp_write(sd, 0xa2, (cp_start_sav >> 4) & 0xff);
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300883 cp_write(sd, 0xa3, ((cp_start_sav & 0x0f) << 4) |
Mats Randgaard4a31a932013-12-10 09:45:00 -0300884 ((cp_start_eav >> 8) & 0x0f));
Hans Verkuil54450f52012-07-18 05:45:16 -0300885 cp_write(sd, 0xa4, cp_start_eav & 0xff);
886
887 /* active video - vertical timing */
Hans Verkuil54450f52012-07-18 05:45:16 -0300888 cp_write(sd, 0xa5, (cp_start_vbi >> 4) & 0xff);
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300889 cp_write(sd, 0xa6, ((cp_start_vbi & 0xf) << 4) |
Mats Randgaard4a31a932013-12-10 09:45:00 -0300890 ((cp_end_vbi >> 8) & 0xf));
Hans Verkuil54450f52012-07-18 05:45:16 -0300891 cp_write(sd, 0xa7, cp_end_vbi & 0xff);
Mats Randgaard4a31a932013-12-10 09:45:00 -0300892 } else if (is_digital_input(sd)) {
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300893 /* set default prim_mode/vid_std for HDMI
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -0300894 according to [REF_03, c. 4.2] */
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300895 io_write(sd, 0x00, 0x02); /* video std */
896 io_write(sd, 0x01, 0x06); /* prim mode */
Mats Randgaard4a31a932013-12-10 09:45:00 -0300897 } else {
898 v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
899 __func__, state->selected_input);
Hans Verkuil54450f52012-07-18 05:45:16 -0300900 }
Hans Verkuil54450f52012-07-18 05:45:16 -0300901
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300902 cp_write(sd, 0x8f, (ch1_fr_ll >> 8) & 0x7);
903 cp_write(sd, 0x90, ch1_fr_ll & 0xff);
904 cp_write(sd, 0xab, (height >> 4) & 0xff);
905 cp_write(sd, 0xac, (height & 0x0f) << 4);
906}
Hans Verkuil54450f52012-07-18 05:45:16 -0300907
908static void set_rgb_quantization_range(struct v4l2_subdev *sd)
909{
910 struct adv7604_state *state = to_state(sd);
911
912 switch (state->rgb_quantization_range) {
913 case V4L2_DV_RGB_RANGE_AUTO:
914 /* automatic */
Mats Randgaard4a31a932013-12-10 09:45:00 -0300915 if (is_digital_input(sd) && !(hdmi_read(sd, 0x05) & 0x80)) {
Hans Verkuil54450f52012-07-18 05:45:16 -0300916 /* receiving DVI-D signal */
917
918 /* ADV7604 selects RGB limited range regardless of
919 input format (CE/IT) in automatic mode */
920 if (state->timings.bt.standards & V4L2_DV_BT_STD_CEA861) {
921 /* RGB limited range (16-235) */
922 io_write_and_or(sd, 0x02, 0x0f, 0x00);
923
924 } else {
925 /* RGB full range (0-255) */
926 io_write_and_or(sd, 0x02, 0x0f, 0x10);
927 }
Hans Verkuil6b0d5d32012-10-16 06:40:45 -0300928 } else {
929 /* receiving HDMI or analog signal, set automode */
930 io_write_and_or(sd, 0x02, 0x0f, 0xf0);
Hans Verkuil54450f52012-07-18 05:45:16 -0300931 }
932 break;
933 case V4L2_DV_RGB_RANGE_LIMITED:
934 /* RGB limited range (16-235) */
935 io_write_and_or(sd, 0x02, 0x0f, 0x00);
936 break;
937 case V4L2_DV_RGB_RANGE_FULL:
938 /* RGB full range (0-255) */
939 io_write_and_or(sd, 0x02, 0x0f, 0x10);
940 break;
941 }
942}
943
944
945static int adv7604_s_ctrl(struct v4l2_ctrl *ctrl)
946{
947 struct v4l2_subdev *sd = to_sd(ctrl);
948 struct adv7604_state *state = to_state(sd);
949
950 switch (ctrl->id) {
951 case V4L2_CID_BRIGHTNESS:
952 cp_write(sd, 0x3c, ctrl->val);
953 return 0;
954 case V4L2_CID_CONTRAST:
955 cp_write(sd, 0x3a, ctrl->val);
956 return 0;
957 case V4L2_CID_SATURATION:
958 cp_write(sd, 0x3b, ctrl->val);
959 return 0;
960 case V4L2_CID_HUE:
961 cp_write(sd, 0x3d, ctrl->val);
962 return 0;
963 case V4L2_CID_DV_RX_RGB_RANGE:
964 state->rgb_quantization_range = ctrl->val;
965 set_rgb_quantization_range(sd);
966 return 0;
967 case V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE:
968 /* Set the analog sampling phase. This is needed to find the
969 best sampling phase for analog video: an application or
970 driver has to try a number of phases and analyze the picture
971 quality before settling on the best performing phase. */
972 afe_write(sd, 0xc8, ctrl->val);
973 return 0;
974 case V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL:
975 /* Use the default blue color for free running mode,
976 or supply your own. */
977 cp_write_and_or(sd, 0xbf, ~0x04, (ctrl->val << 2));
978 return 0;
979 case V4L2_CID_ADV_RX_FREE_RUN_COLOR:
980 cp_write(sd, 0xc0, (ctrl->val & 0xff0000) >> 16);
981 cp_write(sd, 0xc1, (ctrl->val & 0x00ff00) >> 8);
982 cp_write(sd, 0xc2, (u8)(ctrl->val & 0x0000ff));
983 return 0;
984 }
985 return -EINVAL;
986}
987
Hans Verkuil54450f52012-07-18 05:45:16 -0300988/* ----------------------------------------------------------------------- */
989
990static inline bool no_power(struct v4l2_subdev *sd)
991{
992 /* Entire chip or CP powered off */
993 return io_read(sd, 0x0c) & 0x24;
994}
995
996static inline bool no_signal_tmds(struct v4l2_subdev *sd)
997{
Mats Randgaard4a31a932013-12-10 09:45:00 -0300998 struct adv7604_state *state = to_state(sd);
999
1000 return !(io_read(sd, 0x6a) & (0x10 >> state->selected_input));
Hans Verkuil54450f52012-07-18 05:45:16 -03001001}
1002
1003static inline bool no_lock_tmds(struct v4l2_subdev *sd)
1004{
1005 return (io_read(sd, 0x6a) & 0xe0) != 0xe0;
1006}
1007
Martin Buggebb88f322013-08-14 08:52:46 -03001008static inline bool is_hdmi(struct v4l2_subdev *sd)
1009{
1010 return hdmi_read(sd, 0x05) & 0x80;
1011}
1012
Hans Verkuil54450f52012-07-18 05:45:16 -03001013static inline bool no_lock_sspd(struct v4l2_subdev *sd)
1014{
1015 /* TODO channel 2 */
1016 return ((cp_read(sd, 0xb5) & 0xd0) != 0xd0);
1017}
1018
1019static inline bool no_lock_stdi(struct v4l2_subdev *sd)
1020{
1021 /* TODO channel 2 */
1022 return !(cp_read(sd, 0xb1) & 0x80);
1023}
1024
1025static inline bool no_signal(struct v4l2_subdev *sd)
1026{
Hans Verkuil54450f52012-07-18 05:45:16 -03001027 bool ret;
1028
1029 ret = no_power(sd);
1030
1031 ret |= no_lock_stdi(sd);
1032 ret |= no_lock_sspd(sd);
1033
Mats Randgaard4a31a932013-12-10 09:45:00 -03001034 if (is_digital_input(sd)) {
Hans Verkuil54450f52012-07-18 05:45:16 -03001035 ret |= no_lock_tmds(sd);
1036 ret |= no_signal_tmds(sd);
1037 }
1038
1039 return ret;
1040}
1041
1042static inline bool no_lock_cp(struct v4l2_subdev *sd)
1043{
1044 /* CP has detected a non standard number of lines on the incoming
1045 video compared to what it is configured to receive by s_dv_timings */
1046 return io_read(sd, 0x12) & 0x01;
1047}
1048
1049static int adv7604_g_input_status(struct v4l2_subdev *sd, u32 *status)
1050{
Hans Verkuil54450f52012-07-18 05:45:16 -03001051 *status = 0;
1052 *status |= no_power(sd) ? V4L2_IN_ST_NO_POWER : 0;
1053 *status |= no_signal(sd) ? V4L2_IN_ST_NO_SIGNAL : 0;
1054 if (no_lock_cp(sd))
Mats Randgaard4a31a932013-12-10 09:45:00 -03001055 *status |= is_digital_input(sd) ? V4L2_IN_ST_NO_SYNC : V4L2_IN_ST_NO_H_LOCK;
Hans Verkuil54450f52012-07-18 05:45:16 -03001056
1057 v4l2_dbg(1, debug, sd, "%s: status = 0x%x\n", __func__, *status);
1058
1059 return 0;
1060}
1061
1062/* ----------------------------------------------------------------------- */
1063
Hans Verkuil54450f52012-07-18 05:45:16 -03001064struct stdi_readback {
1065 u16 bl, lcf, lcvs;
1066 u8 hs_pol, vs_pol;
1067 bool interlaced;
1068};
1069
1070static int stdi2dv_timings(struct v4l2_subdev *sd,
1071 struct stdi_readback *stdi,
1072 struct v4l2_dv_timings *timings)
1073{
1074 struct adv7604_state *state = to_state(sd);
1075 u32 hfreq = (ADV7604_fsc * 8) / stdi->bl;
1076 u32 pix_clk;
1077 int i;
1078
1079 for (i = 0; adv7604_timings[i].bt.height; i++) {
1080 if (vtotal(&adv7604_timings[i].bt) != stdi->lcf + 1)
1081 continue;
1082 if (adv7604_timings[i].bt.vsync != stdi->lcvs)
1083 continue;
1084
1085 pix_clk = hfreq * htotal(&adv7604_timings[i].bt);
1086
1087 if ((pix_clk < adv7604_timings[i].bt.pixelclock + 1000000) &&
1088 (pix_clk > adv7604_timings[i].bt.pixelclock - 1000000)) {
1089 *timings = adv7604_timings[i];
1090 return 0;
1091 }
1092 }
1093
1094 if (v4l2_detect_cvt(stdi->lcf + 1, hfreq, stdi->lcvs,
1095 (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
1096 (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
1097 timings))
1098 return 0;
1099 if (v4l2_detect_gtf(stdi->lcf + 1, hfreq, stdi->lcvs,
1100 (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
1101 (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
1102 state->aspect_ratio, timings))
1103 return 0;
1104
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001105 v4l2_dbg(2, debug, sd,
1106 "%s: No format candidate found for lcvs = %d, lcf=%d, bl = %d, %chsync, %cvsync\n",
1107 __func__, stdi->lcvs, stdi->lcf, stdi->bl,
1108 stdi->hs_pol, stdi->vs_pol);
Hans Verkuil54450f52012-07-18 05:45:16 -03001109 return -1;
1110}
1111
1112static int read_stdi(struct v4l2_subdev *sd, struct stdi_readback *stdi)
1113{
1114 if (no_lock_stdi(sd) || no_lock_sspd(sd)) {
1115 v4l2_dbg(2, debug, sd, "%s: STDI and/or SSPD not locked\n", __func__);
1116 return -1;
1117 }
1118
1119 /* read STDI */
1120 stdi->bl = ((cp_read(sd, 0xb1) & 0x3f) << 8) | cp_read(sd, 0xb2);
1121 stdi->lcf = ((cp_read(sd, 0xb3) & 0x7) << 8) | cp_read(sd, 0xb4);
1122 stdi->lcvs = cp_read(sd, 0xb3) >> 3;
1123 stdi->interlaced = io_read(sd, 0x12) & 0x10;
1124
1125 /* read SSPD */
1126 if ((cp_read(sd, 0xb5) & 0x03) == 0x01) {
1127 stdi->hs_pol = ((cp_read(sd, 0xb5) & 0x10) ?
1128 ((cp_read(sd, 0xb5) & 0x08) ? '+' : '-') : 'x');
1129 stdi->vs_pol = ((cp_read(sd, 0xb5) & 0x40) ?
1130 ((cp_read(sd, 0xb5) & 0x20) ? '+' : '-') : 'x');
1131 } else {
1132 stdi->hs_pol = 'x';
1133 stdi->vs_pol = 'x';
1134 }
1135
1136 if (no_lock_stdi(sd) || no_lock_sspd(sd)) {
1137 v4l2_dbg(2, debug, sd,
1138 "%s: signal lost during readout of STDI/SSPD\n", __func__);
1139 return -1;
1140 }
1141
1142 if (stdi->lcf < 239 || stdi->bl < 8 || stdi->bl == 0x3fff) {
1143 v4l2_dbg(2, debug, sd, "%s: invalid signal\n", __func__);
1144 memset(stdi, 0, sizeof(struct stdi_readback));
1145 return -1;
1146 }
1147
1148 v4l2_dbg(2, debug, sd,
1149 "%s: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, %chsync, %cvsync, %s\n",
1150 __func__, stdi->lcf, stdi->bl, stdi->lcvs,
1151 stdi->hs_pol, stdi->vs_pol,
1152 stdi->interlaced ? "interlaced" : "progressive");
1153
1154 return 0;
1155}
1156
1157static int adv7604_enum_dv_timings(struct v4l2_subdev *sd,
1158 struct v4l2_enum_dv_timings *timings)
1159{
1160 if (timings->index >= ARRAY_SIZE(adv7604_timings) - 1)
1161 return -EINVAL;
1162 memset(timings->reserved, 0, sizeof(timings->reserved));
1163 timings->timings = adv7604_timings[timings->index];
1164 return 0;
1165}
1166
1167static int adv7604_dv_timings_cap(struct v4l2_subdev *sd,
1168 struct v4l2_dv_timings_cap *cap)
1169{
Hans Verkuil54450f52012-07-18 05:45:16 -03001170 cap->type = V4L2_DV_BT_656_1120;
1171 cap->bt.max_width = 1920;
1172 cap->bt.max_height = 1200;
Hans Verkuilfe9c2562013-08-19 08:07:26 -03001173 cap->bt.min_pixelclock = 25000000;
Mats Randgaard4a31a932013-12-10 09:45:00 -03001174 if (is_digital_input(sd))
Hans Verkuil54450f52012-07-18 05:45:16 -03001175 cap->bt.max_pixelclock = 225000000;
1176 else
1177 cap->bt.max_pixelclock = 170000000;
1178 cap->bt.standards = V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
1179 V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT;
1180 cap->bt.capabilities = V4L2_DV_BT_CAP_PROGRESSIVE |
1181 V4L2_DV_BT_CAP_REDUCED_BLANKING | V4L2_DV_BT_CAP_CUSTOM;
1182 return 0;
1183}
1184
1185/* Fill the optional fields .standards and .flags in struct v4l2_dv_timings
1186 if the format is listed in adv7604_timings[] */
1187static void adv7604_fill_optional_dv_timings_fields(struct v4l2_subdev *sd,
1188 struct v4l2_dv_timings *timings)
1189{
Hans Verkuil54450f52012-07-18 05:45:16 -03001190 int i;
1191
1192 for (i = 0; adv7604_timings[i].bt.width; i++) {
Hans Verkuilef1ed8f2013-08-15 08:28:47 -03001193 if (v4l2_match_dv_timings(timings, &adv7604_timings[i],
Mats Randgaard4a31a932013-12-10 09:45:00 -03001194 is_digital_input(sd) ? 250000 : 1000000)) {
Hans Verkuil54450f52012-07-18 05:45:16 -03001195 *timings = adv7604_timings[i];
1196 break;
1197 }
1198 }
1199}
1200
1201static int adv7604_query_dv_timings(struct v4l2_subdev *sd,
1202 struct v4l2_dv_timings *timings)
1203{
1204 struct adv7604_state *state = to_state(sd);
1205 struct v4l2_bt_timings *bt = &timings->bt;
1206 struct stdi_readback stdi;
1207
1208 if (!timings)
1209 return -EINVAL;
1210
1211 memset(timings, 0, sizeof(struct v4l2_dv_timings));
1212
1213 if (no_signal(sd)) {
1214 v4l2_dbg(1, debug, sd, "%s: no valid signal\n", __func__);
1215 return -ENOLINK;
1216 }
1217
1218 /* read STDI */
1219 if (read_stdi(sd, &stdi)) {
1220 v4l2_dbg(1, debug, sd, "%s: STDI/SSPD not locked\n", __func__);
1221 return -ENOLINK;
1222 }
1223 bt->interlaced = stdi.interlaced ?
1224 V4L2_DV_INTERLACED : V4L2_DV_PROGRESSIVE;
1225
Mats Randgaard4a31a932013-12-10 09:45:00 -03001226 if (is_digital_input(sd)) {
Martin Buggebb88f322013-08-14 08:52:46 -03001227 uint32_t freq;
1228
Hans Verkuil54450f52012-07-18 05:45:16 -03001229 timings->type = V4L2_DV_BT_656_1120;
1230
1231 bt->width = (hdmi_read(sd, 0x07) & 0x0f) * 256 + hdmi_read(sd, 0x08);
1232 bt->height = (hdmi_read(sd, 0x09) & 0x0f) * 256 + hdmi_read(sd, 0x0a);
Martin Buggebb88f322013-08-14 08:52:46 -03001233 freq = (hdmi_read(sd, 0x06) * 1000000) +
Hans Verkuil54450f52012-07-18 05:45:16 -03001234 ((hdmi_read(sd, 0x3b) & 0x30) >> 4) * 250000;
Martin Buggebb88f322013-08-14 08:52:46 -03001235 if (is_hdmi(sd)) {
1236 /* adjust for deep color mode */
1237 unsigned bits_per_channel = ((hdmi_read(sd, 0x0b) & 0x60) >> 4) + 8;
1238
1239 freq = freq * 8 / bits_per_channel;
1240 }
1241 bt->pixelclock = freq;
Hans Verkuil54450f52012-07-18 05:45:16 -03001242 bt->hfrontporch = (hdmi_read(sd, 0x20) & 0x03) * 256 +
1243 hdmi_read(sd, 0x21);
1244 bt->hsync = (hdmi_read(sd, 0x22) & 0x03) * 256 +
1245 hdmi_read(sd, 0x23);
1246 bt->hbackporch = (hdmi_read(sd, 0x24) & 0x03) * 256 +
1247 hdmi_read(sd, 0x25);
1248 bt->vfrontporch = ((hdmi_read(sd, 0x2a) & 0x1f) * 256 +
1249 hdmi_read(sd, 0x2b)) / 2;
1250 bt->vsync = ((hdmi_read(sd, 0x2e) & 0x1f) * 256 +
1251 hdmi_read(sd, 0x2f)) / 2;
1252 bt->vbackporch = ((hdmi_read(sd, 0x32) & 0x1f) * 256 +
1253 hdmi_read(sd, 0x33)) / 2;
1254 bt->polarities = ((hdmi_read(sd, 0x05) & 0x10) ? V4L2_DV_VSYNC_POS_POL : 0) |
1255 ((hdmi_read(sd, 0x05) & 0x20) ? V4L2_DV_HSYNC_POS_POL : 0);
1256 if (bt->interlaced == V4L2_DV_INTERLACED) {
1257 bt->height += (hdmi_read(sd, 0x0b) & 0x0f) * 256 +
1258 hdmi_read(sd, 0x0c);
1259 bt->il_vfrontporch = ((hdmi_read(sd, 0x2c) & 0x1f) * 256 +
1260 hdmi_read(sd, 0x2d)) / 2;
1261 bt->il_vsync = ((hdmi_read(sd, 0x30) & 0x1f) * 256 +
1262 hdmi_read(sd, 0x31)) / 2;
1263 bt->vbackporch = ((hdmi_read(sd, 0x34) & 0x1f) * 256 +
1264 hdmi_read(sd, 0x35)) / 2;
1265 }
1266 adv7604_fill_optional_dv_timings_fields(sd, timings);
1267 } else {
1268 /* find format
Hans Verkuil80939642012-10-16 05:46:21 -03001269 * Since LCVS values are inaccurate [REF_03, p. 275-276],
Hans Verkuil54450f52012-07-18 05:45:16 -03001270 * stdi2dv_timings() is called with lcvs +-1 if the first attempt fails.
1271 */
1272 if (!stdi2dv_timings(sd, &stdi, timings))
1273 goto found;
1274 stdi.lcvs += 1;
1275 v4l2_dbg(1, debug, sd, "%s: lcvs + 1 = %d\n", __func__, stdi.lcvs);
1276 if (!stdi2dv_timings(sd, &stdi, timings))
1277 goto found;
1278 stdi.lcvs -= 2;
1279 v4l2_dbg(1, debug, sd, "%s: lcvs - 1 = %d\n", __func__, stdi.lcvs);
1280 if (stdi2dv_timings(sd, &stdi, timings)) {
Hans Verkuilcf9afb12012-10-16 10:12:55 -03001281 /*
1282 * The STDI block may measure wrong values, especially
1283 * for lcvs and lcf. If the driver can not find any
1284 * valid timing, the STDI block is restarted to measure
1285 * the video timings again. The function will return an
1286 * error, but the restart of STDI will generate a new
1287 * STDI interrupt and the format detection process will
1288 * restart.
1289 */
1290 if (state->restart_stdi_once) {
1291 v4l2_dbg(1, debug, sd, "%s: restart STDI\n", __func__);
1292 /* TODO restart STDI for Sync Channel 2 */
1293 /* enter one-shot mode */
1294 cp_write_and_or(sd, 0x86, 0xf9, 0x00);
1295 /* trigger STDI restart */
1296 cp_write_and_or(sd, 0x86, 0xf9, 0x04);
1297 /* reset to continuous mode */
1298 cp_write_and_or(sd, 0x86, 0xf9, 0x02);
1299 state->restart_stdi_once = false;
1300 return -ENOLINK;
1301 }
Hans Verkuil54450f52012-07-18 05:45:16 -03001302 v4l2_dbg(1, debug, sd, "%s: format not supported\n", __func__);
1303 return -ERANGE;
1304 }
Hans Verkuilcf9afb12012-10-16 10:12:55 -03001305 state->restart_stdi_once = true;
Hans Verkuil54450f52012-07-18 05:45:16 -03001306 }
1307found:
1308
1309 if (no_signal(sd)) {
1310 v4l2_dbg(1, debug, sd, "%s: signal lost during readout\n", __func__);
1311 memset(timings, 0, sizeof(struct v4l2_dv_timings));
1312 return -ENOLINK;
1313 }
1314
Mats Randgaard4a31a932013-12-10 09:45:00 -03001315 if ((is_analog_input(sd) && bt->pixelclock > 170000000) ||
1316 (is_digital_input(sd) && bt->pixelclock > 225000000)) {
Hans Verkuil54450f52012-07-18 05:45:16 -03001317 v4l2_dbg(1, debug, sd, "%s: pixelclock out of range %d\n",
1318 __func__, (u32)bt->pixelclock);
1319 return -ERANGE;
1320 }
1321
1322 if (debug > 1)
Hans Verkuil11d034c2013-08-15 08:05:59 -03001323 v4l2_print_dv_timings(sd->name, "adv7604_query_dv_timings: ",
1324 timings, true);
Hans Verkuil54450f52012-07-18 05:45:16 -03001325
1326 return 0;
1327}
1328
1329static int adv7604_s_dv_timings(struct v4l2_subdev *sd,
1330 struct v4l2_dv_timings *timings)
1331{
1332 struct adv7604_state *state = to_state(sd);
1333 struct v4l2_bt_timings *bt;
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001334 int err;
Hans Verkuil54450f52012-07-18 05:45:16 -03001335
1336 if (!timings)
1337 return -EINVAL;
1338
1339 bt = &timings->bt;
1340
Mats Randgaard4a31a932013-12-10 09:45:00 -03001341 if ((is_analog_input(sd) && bt->pixelclock > 170000000) ||
1342 (is_digital_input(sd) && bt->pixelclock > 225000000)) {
Hans Verkuil54450f52012-07-18 05:45:16 -03001343 v4l2_dbg(1, debug, sd, "%s: pixelclock out of range %d\n",
1344 __func__, (u32)bt->pixelclock);
1345 return -ERANGE;
1346 }
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001347
Hans Verkuil54450f52012-07-18 05:45:16 -03001348 adv7604_fill_optional_dv_timings_fields(sd, timings);
1349
1350 state->timings = *timings;
1351
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001352 cp_write(sd, 0x91, bt->interlaced ? 0x50 : 0x10);
1353
1354 /* Use prim_mode and vid_std when available */
1355 err = configure_predefined_video_timings(sd, timings);
1356 if (err) {
1357 /* custom settings when the video format
1358 does not have prim_mode/vid_std */
1359 configure_custom_video_timings(sd, bt);
1360 }
Hans Verkuil54450f52012-07-18 05:45:16 -03001361
1362 set_rgb_quantization_range(sd);
1363
1364
1365 if (debug > 1)
Hans Verkuil11d034c2013-08-15 08:05:59 -03001366 v4l2_print_dv_timings(sd->name, "adv7604_s_dv_timings: ",
1367 timings, true);
Hans Verkuil54450f52012-07-18 05:45:16 -03001368 return 0;
1369}
1370
1371static int adv7604_g_dv_timings(struct v4l2_subdev *sd,
1372 struct v4l2_dv_timings *timings)
1373{
1374 struct adv7604_state *state = to_state(sd);
1375
1376 *timings = state->timings;
1377 return 0;
1378}
1379
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001380static void enable_input(struct v4l2_subdev *sd)
Hans Verkuil54450f52012-07-18 05:45:16 -03001381{
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001382 struct adv7604_state *state = to_state(sd);
1383
Mats Randgaard4a31a932013-12-10 09:45:00 -03001384 if (is_analog_input(sd)) {
Hans Verkuil54450f52012-07-18 05:45:16 -03001385 /* enable */
1386 io_write(sd, 0x15, 0xb0); /* Disable Tristate of Pins (no audio) */
Mats Randgaard4a31a932013-12-10 09:45:00 -03001387 } else if (is_digital_input(sd)) {
Hans Verkuil54450f52012-07-18 05:45:16 -03001388 /* enable */
Mats Randgaard4a31a932013-12-10 09:45:00 -03001389 hdmi_write_and_or(sd, 0x00, 0xfc, state->selected_input);
Hans Verkuil54450f52012-07-18 05:45:16 -03001390 hdmi_write(sd, 0x1a, 0x0a); /* Unmute audio */
1391 hdmi_write(sd, 0x01, 0x00); /* Enable HDMI clock terminators */
1392 io_write(sd, 0x15, 0xa0); /* Disable Tristate of Pins */
Mats Randgaard4a31a932013-12-10 09:45:00 -03001393 } else {
1394 v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
1395 __func__, state->selected_input);
Hans Verkuil54450f52012-07-18 05:45:16 -03001396 }
1397}
1398
1399static void disable_input(struct v4l2_subdev *sd)
1400{
1401 /* disable */
1402 io_write(sd, 0x15, 0xbe); /* Tristate all outputs from video core */
1403 hdmi_write(sd, 0x1a, 0x1a); /* Mute audio */
1404 hdmi_write(sd, 0x01, 0x78); /* Disable HDMI clock terminators */
1405}
1406
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001407static void select_input(struct v4l2_subdev *sd)
Hans Verkuil54450f52012-07-18 05:45:16 -03001408{
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001409 struct adv7604_state *state = to_state(sd);
Hans Verkuil54450f52012-07-18 05:45:16 -03001410
Mats Randgaard4a31a932013-12-10 09:45:00 -03001411 if (is_analog_input(sd)) {
Hans Verkuil54450f52012-07-18 05:45:16 -03001412 /* reset ADI recommended settings for HDMI: */
1413 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 4. */
1414 hdmi_write(sd, 0x0d, 0x04); /* HDMI filter optimization */
1415 hdmi_write(sd, 0x3d, 0x00); /* DDC bus active pull-up control */
1416 hdmi_write(sd, 0x3e, 0x74); /* TMDS PLL optimization */
1417 hdmi_write(sd, 0x4e, 0x3b); /* TMDS PLL optimization */
1418 hdmi_write(sd, 0x57, 0x74); /* TMDS PLL optimization */
1419 hdmi_write(sd, 0x58, 0x63); /* TMDS PLL optimization */
1420 hdmi_write(sd, 0x8d, 0x18); /* equaliser */
1421 hdmi_write(sd, 0x8e, 0x34); /* equaliser */
1422 hdmi_write(sd, 0x93, 0x88); /* equaliser */
1423 hdmi_write(sd, 0x94, 0x2e); /* equaliser */
1424 hdmi_write(sd, 0x96, 0x00); /* enable automatic EQ changing */
1425
1426 afe_write(sd, 0x00, 0x08); /* power up ADC */
1427 afe_write(sd, 0x01, 0x06); /* power up Analog Front End */
1428 afe_write(sd, 0xc8, 0x00); /* phase control */
1429
1430 /* set ADI recommended settings for digitizer */
1431 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 17. */
1432 afe_write(sd, 0x12, 0x7b); /* ADC noise shaping filter controls */
1433 afe_write(sd, 0x0c, 0x1f); /* CP core gain controls */
1434 cp_write(sd, 0x3e, 0x04); /* CP core pre-gain control */
1435 cp_write(sd, 0xc3, 0x39); /* CP coast control. Graphics mode */
1436 cp_write(sd, 0x40, 0x5c); /* CP core pre-gain control. Graphics mode */
Mats Randgaard4a31a932013-12-10 09:45:00 -03001437 } else if (is_digital_input(sd)) {
1438 hdmi_write(sd, 0x00, state->selected_input & 0x03);
Hans Verkuil54450f52012-07-18 05:45:16 -03001439
Hans Verkuil54450f52012-07-18 05:45:16 -03001440 /* set ADI recommended settings for HDMI: */
1441 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 4. */
1442 hdmi_write(sd, 0x0d, 0x84); /* HDMI filter optimization */
1443 hdmi_write(sd, 0x3d, 0x10); /* DDC bus active pull-up control */
1444 hdmi_write(sd, 0x3e, 0x39); /* TMDS PLL optimization */
1445 hdmi_write(sd, 0x4e, 0x3b); /* TMDS PLL optimization */
1446 hdmi_write(sd, 0x57, 0xb6); /* TMDS PLL optimization */
1447 hdmi_write(sd, 0x58, 0x03); /* TMDS PLL optimization */
1448 hdmi_write(sd, 0x8d, 0x18); /* equaliser */
1449 hdmi_write(sd, 0x8e, 0x34); /* equaliser */
1450 hdmi_write(sd, 0x93, 0x8b); /* equaliser */
1451 hdmi_write(sd, 0x94, 0x2d); /* equaliser */
1452 hdmi_write(sd, 0x96, 0x01); /* enable automatic EQ changing */
1453
1454 afe_write(sd, 0x00, 0xff); /* power down ADC */
1455 afe_write(sd, 0x01, 0xfe); /* power down Analog Front End */
1456 afe_write(sd, 0xc8, 0x40); /* phase control */
1457
1458 /* reset ADI recommended settings for digitizer */
1459 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 17. */
1460 afe_write(sd, 0x12, 0xfb); /* ADC noise shaping filter controls */
1461 afe_write(sd, 0x0c, 0x0d); /* CP core gain controls */
1462 cp_write(sd, 0x3e, 0x00); /* CP core pre-gain control */
1463 cp_write(sd, 0xc3, 0x39); /* CP coast control. Graphics mode */
1464 cp_write(sd, 0x40, 0x80); /* CP core pre-gain control. Graphics mode */
Mats Randgaard4a31a932013-12-10 09:45:00 -03001465 } else {
1466 v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
1467 __func__, state->selected_input);
Hans Verkuil54450f52012-07-18 05:45:16 -03001468 }
1469}
1470
1471static int adv7604_s_routing(struct v4l2_subdev *sd,
1472 u32 input, u32 output, u32 config)
1473{
1474 struct adv7604_state *state = to_state(sd);
1475
1476 v4l2_dbg(2, debug, sd, "%s: input %d", __func__, input);
1477
Mats Randgaard4a31a932013-12-10 09:45:00 -03001478 state->selected_input = input;
Hans Verkuil54450f52012-07-18 05:45:16 -03001479
1480 disable_input(sd);
1481
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001482 select_input(sd);
Hans Verkuil54450f52012-07-18 05:45:16 -03001483
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001484 enable_input(sd);
Hans Verkuil54450f52012-07-18 05:45:16 -03001485
1486 return 0;
1487}
1488
1489static int adv7604_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned int index,
1490 enum v4l2_mbus_pixelcode *code)
1491{
1492 if (index)
1493 return -EINVAL;
1494 /* Good enough for now */
1495 *code = V4L2_MBUS_FMT_FIXED;
1496 return 0;
1497}
1498
1499static int adv7604_g_mbus_fmt(struct v4l2_subdev *sd,
1500 struct v4l2_mbus_framefmt *fmt)
1501{
1502 struct adv7604_state *state = to_state(sd);
1503
1504 fmt->width = state->timings.bt.width;
1505 fmt->height = state->timings.bt.height;
1506 fmt->code = V4L2_MBUS_FMT_FIXED;
1507 fmt->field = V4L2_FIELD_NONE;
1508 if (state->timings.bt.standards & V4L2_DV_BT_STD_CEA861) {
1509 fmt->colorspace = (state->timings.bt.height <= 576) ?
1510 V4L2_COLORSPACE_SMPTE170M : V4L2_COLORSPACE_REC709;
1511 }
1512 return 0;
1513}
1514
1515static int adv7604_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
1516{
1517 struct adv7604_state *state = to_state(sd);
1518 u8 fmt_change, fmt_change_digital, tx_5v;
Mats Randgaard25a64ac2013-08-14 07:58:45 -03001519 u32 input_status;
Hans Verkuil54450f52012-07-18 05:45:16 -03001520
1521 /* format change */
1522 fmt_change = io_read(sd, 0x43) & 0x98;
1523 if (fmt_change)
1524 io_write(sd, 0x44, fmt_change);
Mats Randgaard4a31a932013-12-10 09:45:00 -03001525 fmt_change_digital = is_digital_input(sd) ? (io_read(sd, 0x6b) & 0xc0) : 0;
Hans Verkuil54450f52012-07-18 05:45:16 -03001526 if (fmt_change_digital)
1527 io_write(sd, 0x6c, fmt_change_digital);
1528 if (fmt_change || fmt_change_digital) {
1529 v4l2_dbg(1, debug, sd,
Mats Randgaard25a64ac2013-08-14 07:58:45 -03001530 "%s: fmt_change = 0x%x, fmt_change_digital = 0x%x\n",
Hans Verkuil54450f52012-07-18 05:45:16 -03001531 __func__, fmt_change, fmt_change_digital);
Mats Randgaard25a64ac2013-08-14 07:58:45 -03001532
1533 adv7604_g_input_status(sd, &input_status);
1534 if (input_status != state->prev_input_status) {
1535 v4l2_dbg(1, debug, sd,
1536 "%s: input_status = 0x%x, prev_input_status = 0x%x\n",
1537 __func__, input_status, state->prev_input_status);
1538 state->prev_input_status = input_status;
1539 v4l2_subdev_notify(sd, ADV7604_FMT_CHANGE, NULL);
1540 }
1541
Hans Verkuil54450f52012-07-18 05:45:16 -03001542 if (handled)
1543 *handled = true;
1544 }
1545 /* tx 5v detect */
Mats Randgaard4a31a932013-12-10 09:45:00 -03001546 tx_5v = io_read(sd, 0x70) & 0x1e;
Hans Verkuil54450f52012-07-18 05:45:16 -03001547 if (tx_5v) {
1548 v4l2_dbg(1, debug, sd, "%s: tx_5v: 0x%x\n", __func__, tx_5v);
1549 io_write(sd, 0x71, tx_5v);
1550 adv7604_s_detect_tx_5v_ctrl(sd);
1551 if (handled)
1552 *handled = true;
1553 }
1554 return 0;
1555}
1556
1557static int adv7604_get_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
1558{
1559 struct adv7604_state *state = to_state(sd);
Mats Randgaard4a31a932013-12-10 09:45:00 -03001560 u8 *data = NULL;
Hans Verkuil54450f52012-07-18 05:45:16 -03001561
Mats Randgaard4a31a932013-12-10 09:45:00 -03001562 if (edid->pad > ADV7604_EDID_PORT_D)
Hans Verkuil54450f52012-07-18 05:45:16 -03001563 return -EINVAL;
1564 if (edid->blocks == 0)
1565 return -EINVAL;
Mats Randgaard4a31a932013-12-10 09:45:00 -03001566 if (edid->blocks > 2)
Hans Verkuil54450f52012-07-18 05:45:16 -03001567 return -EINVAL;
Mats Randgaard4a31a932013-12-10 09:45:00 -03001568 if (edid->start_block > 1)
1569 return -EINVAL;
1570 if (edid->start_block == 1)
1571 edid->blocks = 1;
Hans Verkuil54450f52012-07-18 05:45:16 -03001572 if (!edid->edid)
1573 return -EINVAL;
Mats Randgaard4a31a932013-12-10 09:45:00 -03001574
1575 if (edid->blocks > state->edid.blocks)
1576 edid->blocks = state->edid.blocks;
1577
1578 switch (edid->pad) {
1579 case ADV7604_EDID_PORT_A:
1580 case ADV7604_EDID_PORT_B:
1581 case ADV7604_EDID_PORT_C:
1582 case ADV7604_EDID_PORT_D:
1583 if (state->edid.present & (1 << edid->pad))
1584 data = state->edid.edid;
1585 break;
1586 default:
1587 return -EINVAL;
1588 break;
1589 }
1590 if (!data)
1591 return -ENODATA;
1592
1593 memcpy(edid->edid,
1594 data + edid->start_block * 128,
Hans Verkuil54450f52012-07-18 05:45:16 -03001595 edid->blocks * 128);
1596 return 0;
1597}
1598
1599static int adv7604_set_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
1600{
1601 struct adv7604_state *state = to_state(sd);
1602 int err;
1603
Mats Randgaard4a31a932013-12-10 09:45:00 -03001604 if (edid->pad > ADV7604_EDID_PORT_D)
Hans Verkuil54450f52012-07-18 05:45:16 -03001605 return -EINVAL;
1606 if (edid->start_block != 0)
1607 return -EINVAL;
1608 if (edid->blocks == 0) {
1609 /* Pull down the hotplug pin */
Mats Randgaard4a31a932013-12-10 09:45:00 -03001610 state->edid.present &= ~(1 << edid->pad);
1611 v4l2_subdev_notify(sd, ADV7604_HOTPLUG, (void *)&state->edid.present);
Hans Verkuil54450f52012-07-18 05:45:16 -03001612 /* Disables I2C access to internal EDID ram from DDC port */
1613 rep_write_and_or(sd, 0x77, 0xf0, 0x0);
Mats Randgaard4a31a932013-12-10 09:45:00 -03001614 state->edid.blocks = 0;
Hans Verkuil54450f52012-07-18 05:45:16 -03001615 /* Fall back to a 16:9 aspect ratio */
1616 state->aspect_ratio.numerator = 16;
1617 state->aspect_ratio.denominator = 9;
Mats Randgaard4a31a932013-12-10 09:45:00 -03001618 v4l2_dbg(2, debug, sd, "%s: clear edid\n", __func__);
Hans Verkuil54450f52012-07-18 05:45:16 -03001619 return 0;
1620 }
Mats Randgaard4a31a932013-12-10 09:45:00 -03001621 if (edid->blocks > 2) {
1622 edid->blocks = 2;
Hans Verkuil54450f52012-07-18 05:45:16 -03001623 return -E2BIG;
Mats Randgaard4a31a932013-12-10 09:45:00 -03001624 }
Hans Verkuil54450f52012-07-18 05:45:16 -03001625 if (!edid->edid)
1626 return -EINVAL;
Mats Randgaard4a31a932013-12-10 09:45:00 -03001627
1628 cancel_delayed_work_sync(&state->delayed_work_enable_hotplug);
1629 state->edid.present &= ~(1 << edid->pad);
1630 v4l2_subdev_notify(sd, ADV7604_HOTPLUG, (void *)&state->edid.present);
1631
1632 memcpy(state->edid.edid, edid->edid, 128 * edid->blocks);
1633 state->edid.blocks = edid->blocks;
Hans Verkuil54450f52012-07-18 05:45:16 -03001634 state->aspect_ratio = v4l2_calc_aspect_ratio(edid->edid[0x15],
1635 edid->edid[0x16]);
Mats Randgaard4a31a932013-12-10 09:45:00 -03001636 state->edid.present |= edid->pad;
1637
1638 err = edid_write_block(sd, 128 * edid->blocks, state->edid.edid);
1639 if (err < 0) {
Hans Verkuil54450f52012-07-18 05:45:16 -03001640 v4l2_err(sd, "error %d writing edid\n", err);
Mats Randgaard4a31a932013-12-10 09:45:00 -03001641 return err;
1642 }
1643
1644 /* enable hotplug after 100 ms */
1645 queue_delayed_work(state->work_queues,
1646 &state->delayed_work_enable_hotplug, HZ / 10);
1647 return 0;
Hans Verkuil54450f52012-07-18 05:45:16 -03001648}
1649
1650/*********** avi info frame CEA-861-E **************/
1651
1652static void print_avi_infoframe(struct v4l2_subdev *sd)
1653{
1654 int i;
1655 u8 buf[14];
1656 u8 avi_len;
1657 u8 avi_ver;
1658
Martin Buggebb88f322013-08-14 08:52:46 -03001659 if (!is_hdmi(sd)) {
Hans Verkuil54450f52012-07-18 05:45:16 -03001660 v4l2_info(sd, "receive DVI-D signal (AVI infoframe not supported)\n");
1661 return;
1662 }
1663 if (!(io_read(sd, 0x60) & 0x01)) {
1664 v4l2_info(sd, "AVI infoframe not received\n");
1665 return;
1666 }
1667
1668 if (io_read(sd, 0x83) & 0x01) {
1669 v4l2_info(sd, "AVI infoframe checksum error has occurred earlier\n");
1670 io_write(sd, 0x85, 0x01); /* clear AVI_INF_CKS_ERR_RAW */
1671 if (io_read(sd, 0x83) & 0x01) {
1672 v4l2_info(sd, "AVI infoframe checksum error still present\n");
1673 io_write(sd, 0x85, 0x01); /* clear AVI_INF_CKS_ERR_RAW */
1674 }
1675 }
1676
1677 avi_len = infoframe_read(sd, 0xe2);
1678 avi_ver = infoframe_read(sd, 0xe1);
1679 v4l2_info(sd, "AVI infoframe version %d (%d byte)\n",
1680 avi_ver, avi_len);
1681
1682 if (avi_ver != 0x02)
1683 return;
1684
1685 for (i = 0; i < 14; i++)
1686 buf[i] = infoframe_read(sd, i);
1687
1688 v4l2_info(sd,
1689 "\t%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
1690 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7],
1691 buf[8], buf[9], buf[10], buf[11], buf[12], buf[13]);
1692}
1693
1694static int adv7604_log_status(struct v4l2_subdev *sd)
1695{
1696 struct adv7604_state *state = to_state(sd);
1697 struct v4l2_dv_timings timings;
1698 struct stdi_readback stdi;
1699 u8 reg_io_0x02 = io_read(sd, 0x02);
1700
1701 char *csc_coeff_sel_rb[16] = {
1702 "bypassed", "YPbPr601 -> RGB", "reserved", "YPbPr709 -> RGB",
1703 "reserved", "RGB -> YPbPr601", "reserved", "RGB -> YPbPr709",
1704 "reserved", "YPbPr709 -> YPbPr601", "YPbPr601 -> YPbPr709",
1705 "reserved", "reserved", "reserved", "reserved", "manual"
1706 };
1707 char *input_color_space_txt[16] = {
1708 "RGB limited range (16-235)", "RGB full range (0-255)",
1709 "YCbCr Bt.601 (16-235)", "YCbCr Bt.709 (16-235)",
1710 "XvYCC Bt.601", "XvYCC Bt.709",
1711 "YCbCr Bt.601 (0-255)", "YCbCr Bt.709 (0-255)",
1712 "invalid", "invalid", "invalid", "invalid", "invalid",
1713 "invalid", "invalid", "automatic"
1714 };
1715 char *rgb_quantization_range_txt[] = {
1716 "Automatic",
1717 "RGB limited range (16-235)",
1718 "RGB full range (0-255)",
1719 };
Martin Buggebb88f322013-08-14 08:52:46 -03001720 char *deep_color_mode_txt[4] = {
1721 "8-bits per channel",
1722 "10-bits per channel",
1723 "12-bits per channel",
1724 "16-bits per channel (not supported)"
1725 };
Hans Verkuil54450f52012-07-18 05:45:16 -03001726
1727 v4l2_info(sd, "-----Chip status-----\n");
1728 v4l2_info(sd, "Chip power: %s\n", no_power(sd) ? "off" : "on");
1729 v4l2_info(sd, "Connector type: %s\n", state->connector_hdmi ?
Mats Randgaard4a31a932013-12-10 09:45:00 -03001730 "HDMI" : (is_digital_input(sd) ? "DVI-D" : "DVI-A"));
1731 v4l2_info(sd, "EDID enabled port A: %s, B: %s, C: %s, D: %s\n",
1732 ((rep_read(sd, 0x7d) & 0x01) ? "Yes" : "No"),
1733 ((rep_read(sd, 0x7d) & 0x02) ? "Yes" : "No"),
1734 ((rep_read(sd, 0x7d) & 0x04) ? "Yes" : "No"),
1735 ((rep_read(sd, 0x7d) & 0x08) ? "Yes" : "No"));
Hans Verkuil54450f52012-07-18 05:45:16 -03001736 v4l2_info(sd, "CEC: %s\n", !!(cec_read(sd, 0x2a) & 0x01) ?
1737 "enabled" : "disabled");
1738
1739 v4l2_info(sd, "-----Signal status-----\n");
Mats Randgaard4a31a932013-12-10 09:45:00 -03001740 v4l2_info(sd, "Cable detected (+5V power) port A: %s, B: %s, C: %s, D: %s\n",
1741 ((io_read(sd, 0x6f) & 0x10) ? "Yes" : "No"),
1742 ((io_read(sd, 0x6f) & 0x08) ? "Yes" : "No"),
1743 ((io_read(sd, 0x6f) & 0x04) ? "Yes" : "No"),
1744 ((io_read(sd, 0x6f) & 0x02) ? "Yes" : "No"));
Hans Verkuil54450f52012-07-18 05:45:16 -03001745 v4l2_info(sd, "TMDS signal detected: %s\n",
1746 no_signal_tmds(sd) ? "false" : "true");
1747 v4l2_info(sd, "TMDS signal locked: %s\n",
1748 no_lock_tmds(sd) ? "false" : "true");
1749 v4l2_info(sd, "SSPD locked: %s\n", no_lock_sspd(sd) ? "false" : "true");
1750 v4l2_info(sd, "STDI locked: %s\n", no_lock_stdi(sd) ? "false" : "true");
1751 v4l2_info(sd, "CP locked: %s\n", no_lock_cp(sd) ? "false" : "true");
1752 v4l2_info(sd, "CP free run: %s\n",
1753 (!!(cp_read(sd, 0xff) & 0x10) ? "on" : "off"));
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001754 v4l2_info(sd, "Prim-mode = 0x%x, video std = 0x%x, v_freq = 0x%x\n",
1755 io_read(sd, 0x01) & 0x0f, io_read(sd, 0x00) & 0x3f,
1756 (io_read(sd, 0x01) & 0x70) >> 4);
Hans Verkuil54450f52012-07-18 05:45:16 -03001757
1758 v4l2_info(sd, "-----Video Timings-----\n");
1759 if (read_stdi(sd, &stdi))
1760 v4l2_info(sd, "STDI: not locked\n");
1761 else
1762 v4l2_info(sd, "STDI: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, %s, %chsync, %cvsync\n",
1763 stdi.lcf, stdi.bl, stdi.lcvs,
1764 stdi.interlaced ? "interlaced" : "progressive",
1765 stdi.hs_pol, stdi.vs_pol);
1766 if (adv7604_query_dv_timings(sd, &timings))
1767 v4l2_info(sd, "No video detected\n");
1768 else
Hans Verkuil11d034c2013-08-15 08:05:59 -03001769 v4l2_print_dv_timings(sd->name, "Detected format: ",
1770 &timings, true);
1771 v4l2_print_dv_timings(sd->name, "Configured format: ",
1772 &state->timings, true);
Hans Verkuil54450f52012-07-18 05:45:16 -03001773
Mats Randgaard76eb2d32013-08-14 08:56:57 -03001774 if (no_signal(sd))
1775 return 0;
1776
Hans Verkuil54450f52012-07-18 05:45:16 -03001777 v4l2_info(sd, "-----Color space-----\n");
1778 v4l2_info(sd, "RGB quantization range ctrl: %s\n",
1779 rgb_quantization_range_txt[state->rgb_quantization_range]);
1780 v4l2_info(sd, "Input color space: %s\n",
1781 input_color_space_txt[reg_io_0x02 >> 4]);
1782 v4l2_info(sd, "Output color space: %s %s, saturator %s\n",
1783 (reg_io_0x02 & 0x02) ? "RGB" : "YCbCr",
1784 (reg_io_0x02 & 0x04) ? "(16-235)" : "(0-255)",
1785 ((reg_io_0x02 & 0x04) ^ (reg_io_0x02 & 0x01)) ?
Mats Randgaard76eb2d32013-08-14 08:56:57 -03001786 "enabled" : "disabled");
Hans Verkuil54450f52012-07-18 05:45:16 -03001787 v4l2_info(sd, "Color space conversion: %s\n",
1788 csc_coeff_sel_rb[cp_read(sd, 0xfc) >> 4]);
1789
Mats Randgaard4a31a932013-12-10 09:45:00 -03001790 if (!is_digital_input(sd))
Mats Randgaard76eb2d32013-08-14 08:56:57 -03001791 return 0;
1792
1793 v4l2_info(sd, "-----%s status-----\n", is_hdmi(sd) ? "HDMI" : "DVI-D");
Mats Randgaard4a31a932013-12-10 09:45:00 -03001794 v4l2_info(sd, "Digital video port selected: %c\n",
1795 (hdmi_read(sd, 0x00) & 0x03) + 'A');
1796 v4l2_info(sd, "HDCP encrypted content: %s\n",
1797 (hdmi_read(sd, 0x05) & 0x40) ? "true" : "false");
Mats Randgaard76eb2d32013-08-14 08:56:57 -03001798 v4l2_info(sd, "HDCP keys read: %s%s\n",
1799 (hdmi_read(sd, 0x04) & 0x20) ? "yes" : "no",
1800 (hdmi_read(sd, 0x04) & 0x10) ? "ERROR" : "");
1801 if (!is_hdmi(sd)) {
1802 bool audio_pll_locked = hdmi_read(sd, 0x04) & 0x01;
1803 bool audio_sample_packet_detect = hdmi_read(sd, 0x18) & 0x01;
1804 bool audio_mute = io_read(sd, 0x65) & 0x40;
1805
1806 v4l2_info(sd, "Audio: pll %s, samples %s, %s\n",
1807 audio_pll_locked ? "locked" : "not locked",
1808 audio_sample_packet_detect ? "detected" : "not detected",
1809 audio_mute ? "muted" : "enabled");
1810 if (audio_pll_locked && audio_sample_packet_detect) {
1811 v4l2_info(sd, "Audio format: %s\n",
1812 (hdmi_read(sd, 0x07) & 0x20) ? "multi-channel" : "stereo");
1813 }
1814 v4l2_info(sd, "Audio CTS: %u\n", (hdmi_read(sd, 0x5b) << 12) +
1815 (hdmi_read(sd, 0x5c) << 8) +
1816 (hdmi_read(sd, 0x5d) & 0xf0));
1817 v4l2_info(sd, "Audio N: %u\n", ((hdmi_read(sd, 0x5d) & 0x0f) << 16) +
1818 (hdmi_read(sd, 0x5e) << 8) +
1819 hdmi_read(sd, 0x5f));
1820 v4l2_info(sd, "AV Mute: %s\n", (hdmi_read(sd, 0x04) & 0x40) ? "on" : "off");
1821
1822 v4l2_info(sd, "Deep color mode: %s\n", deep_color_mode_txt[(hdmi_read(sd, 0x0b) & 0x60) >> 5]);
1823
Hans Verkuil54450f52012-07-18 05:45:16 -03001824 print_avi_infoframe(sd);
1825 }
1826
1827 return 0;
1828}
1829
1830/* ----------------------------------------------------------------------- */
1831
1832static const struct v4l2_ctrl_ops adv7604_ctrl_ops = {
1833 .s_ctrl = adv7604_s_ctrl,
1834};
1835
1836static const struct v4l2_subdev_core_ops adv7604_core_ops = {
1837 .log_status = adv7604_log_status,
1838 .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
1839 .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
1840 .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
1841 .g_ctrl = v4l2_subdev_g_ctrl,
1842 .s_ctrl = v4l2_subdev_s_ctrl,
1843 .queryctrl = v4l2_subdev_queryctrl,
1844 .querymenu = v4l2_subdev_querymenu,
Hans Verkuil54450f52012-07-18 05:45:16 -03001845 .interrupt_service_routine = adv7604_isr,
1846#ifdef CONFIG_VIDEO_ADV_DEBUG
1847 .g_register = adv7604_g_register,
1848 .s_register = adv7604_s_register,
1849#endif
1850};
1851
1852static const struct v4l2_subdev_video_ops adv7604_video_ops = {
1853 .s_routing = adv7604_s_routing,
1854 .g_input_status = adv7604_g_input_status,
1855 .s_dv_timings = adv7604_s_dv_timings,
1856 .g_dv_timings = adv7604_g_dv_timings,
1857 .query_dv_timings = adv7604_query_dv_timings,
1858 .enum_dv_timings = adv7604_enum_dv_timings,
1859 .dv_timings_cap = adv7604_dv_timings_cap,
1860 .enum_mbus_fmt = adv7604_enum_mbus_fmt,
1861 .g_mbus_fmt = adv7604_g_mbus_fmt,
1862 .try_mbus_fmt = adv7604_g_mbus_fmt,
1863 .s_mbus_fmt = adv7604_g_mbus_fmt,
1864};
1865
1866static const struct v4l2_subdev_pad_ops adv7604_pad_ops = {
1867 .get_edid = adv7604_get_edid,
1868 .set_edid = adv7604_set_edid,
1869};
1870
1871static const struct v4l2_subdev_ops adv7604_ops = {
1872 .core = &adv7604_core_ops,
1873 .video = &adv7604_video_ops,
1874 .pad = &adv7604_pad_ops,
1875};
1876
1877/* -------------------------- custom ctrls ---------------------------------- */
1878
1879static const struct v4l2_ctrl_config adv7604_ctrl_analog_sampling_phase = {
1880 .ops = &adv7604_ctrl_ops,
1881 .id = V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE,
1882 .name = "Analog Sampling Phase",
1883 .type = V4L2_CTRL_TYPE_INTEGER,
1884 .min = 0,
1885 .max = 0x1f,
1886 .step = 1,
1887 .def = 0,
1888};
1889
1890static const struct v4l2_ctrl_config adv7604_ctrl_free_run_color_manual = {
1891 .ops = &adv7604_ctrl_ops,
1892 .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL,
1893 .name = "Free Running Color, Manual",
1894 .type = V4L2_CTRL_TYPE_BOOLEAN,
1895 .min = false,
1896 .max = true,
1897 .step = 1,
1898 .def = false,
1899};
1900
1901static const struct v4l2_ctrl_config adv7604_ctrl_free_run_color = {
1902 .ops = &adv7604_ctrl_ops,
1903 .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR,
1904 .name = "Free Running Color",
1905 .type = V4L2_CTRL_TYPE_INTEGER,
1906 .min = 0x0,
1907 .max = 0xffffff,
1908 .step = 0x1,
1909 .def = 0x0,
1910};
1911
1912/* ----------------------------------------------------------------------- */
1913
1914static int adv7604_core_init(struct v4l2_subdev *sd)
1915{
1916 struct adv7604_state *state = to_state(sd);
1917 struct adv7604_platform_data *pdata = &state->pdata;
1918
1919 hdmi_write(sd, 0x48,
1920 (pdata->disable_pwrdnb ? 0x80 : 0) |
1921 (pdata->disable_cable_det_rst ? 0x40 : 0));
1922
1923 disable_input(sd);
1924
1925 /* power */
1926 io_write(sd, 0x0c, 0x42); /* Power up part and power down VDP */
1927 io_write(sd, 0x0b, 0x44); /* Power down ESDP block */
1928 cp_write(sd, 0xcf, 0x01); /* Power down macrovision */
1929
1930 /* video format */
1931 io_write_and_or(sd, 0x02, 0xf0,
1932 pdata->alt_gamma << 3 |
1933 pdata->op_656_range << 2 |
1934 pdata->rgb_out << 1 |
1935 pdata->alt_data_sat << 0);
1936 io_write(sd, 0x03, pdata->op_format_sel);
1937 io_write_and_or(sd, 0x04, 0x1f, pdata->op_ch_sel << 5);
1938 io_write_and_or(sd, 0x05, 0xf0, pdata->blank_data << 3 |
1939 pdata->insert_av_codes << 2 |
1940 pdata->replicate_av_codes << 1 |
1941 pdata->invert_cbcr << 0);
1942
1943 /* TODO from platform data */
1944 cp_write(sd, 0x69, 0x30); /* Enable CP CSC */
1945 io_write(sd, 0x06, 0xa6); /* positive VS and HS */
Mikhail Khelikf31b62e2013-12-20 05:12:00 -03001946
1947 /* Adjust drive strength */
1948 io_write(sd, 0x14, 0x40 | pdata->dr_str_data << 4 |
1949 pdata->dr_str_clk << 2 |
1950 pdata->dr_str_sync);
1951
Hans Verkuil54450f52012-07-18 05:45:16 -03001952 cp_write(sd, 0xba, (pdata->hdmi_free_run_mode << 1) | 0x01); /* HDMI free run */
1953 cp_write(sd, 0xf3, 0xdc); /* Low threshold to enter/exit free run mode */
1954 cp_write(sd, 0xf9, 0x23); /* STDI ch. 1 - LCVS change threshold -
Hans Verkuil80939642012-10-16 05:46:21 -03001955 ADI recommended setting [REF_01, c. 2.3.3] */
Hans Verkuil54450f52012-07-18 05:45:16 -03001956 cp_write(sd, 0x45, 0x23); /* STDI ch. 2 - LCVS change threshold -
Hans Verkuil80939642012-10-16 05:46:21 -03001957 ADI recommended setting [REF_01, c. 2.3.3] */
Hans Verkuil54450f52012-07-18 05:45:16 -03001958 cp_write(sd, 0xc9, 0x2d); /* use prim_mode and vid_std as free run resolution
1959 for digital formats */
Mats Randgaard4a31a932013-12-10 09:45:00 -03001960 rep_write(sd, 0x76, 0xc0); /* SPA location for port B, C and D */
Hans Verkuil54450f52012-07-18 05:45:16 -03001961
1962 /* TODO from platform data */
1963 afe_write(sd, 0xb5, 0x01); /* Setting MCLK to 256Fs */
1964
1965 afe_write(sd, 0x02, pdata->ain_sel); /* Select analog input muxing mode */
1966 io_write_and_or(sd, 0x30, ~(1 << 4), pdata->output_bus_lsb_to_msb << 4);
1967
Hans Verkuil54450f52012-07-18 05:45:16 -03001968 /* interrupts */
1969 io_write(sd, 0x40, 0xc2); /* Configure INT1 */
1970 io_write(sd, 0x41, 0xd7); /* STDI irq for any change, disable INT2 */
1971 io_write(sd, 0x46, 0x98); /* Enable SSPD, STDI and CP unlocked interrupts */
1972 io_write(sd, 0x6e, 0xc0); /* Enable V_LOCKED and DE_REGEN_LCK interrupts */
Mats Randgaard4a31a932013-12-10 09:45:00 -03001973 io_write(sd, 0x73, 0x1e); /* Enable CABLE_DET_A_ST (+5v) interrupts */
Hans Verkuil54450f52012-07-18 05:45:16 -03001974
1975 return v4l2_ctrl_handler_setup(sd->ctrl_handler);
1976}
1977
1978static void adv7604_unregister_clients(struct adv7604_state *state)
1979{
1980 if (state->i2c_avlink)
1981 i2c_unregister_device(state->i2c_avlink);
1982 if (state->i2c_cec)
1983 i2c_unregister_device(state->i2c_cec);
1984 if (state->i2c_infoframe)
1985 i2c_unregister_device(state->i2c_infoframe);
1986 if (state->i2c_esdp)
1987 i2c_unregister_device(state->i2c_esdp);
1988 if (state->i2c_dpp)
1989 i2c_unregister_device(state->i2c_dpp);
1990 if (state->i2c_afe)
1991 i2c_unregister_device(state->i2c_afe);
1992 if (state->i2c_repeater)
1993 i2c_unregister_device(state->i2c_repeater);
1994 if (state->i2c_edid)
1995 i2c_unregister_device(state->i2c_edid);
1996 if (state->i2c_hdmi)
1997 i2c_unregister_device(state->i2c_hdmi);
1998 if (state->i2c_test)
1999 i2c_unregister_device(state->i2c_test);
2000 if (state->i2c_cp)
2001 i2c_unregister_device(state->i2c_cp);
2002 if (state->i2c_vdp)
2003 i2c_unregister_device(state->i2c_vdp);
2004}
2005
2006static struct i2c_client *adv7604_dummy_client(struct v4l2_subdev *sd,
2007 u8 addr, u8 io_reg)
2008{
2009 struct i2c_client *client = v4l2_get_subdevdata(sd);
2010
2011 if (addr)
2012 io_write(sd, io_reg, addr << 1);
2013 return i2c_new_dummy(client->adapter, io_read(sd, io_reg) >> 1);
2014}
2015
2016static int adv7604_probe(struct i2c_client *client,
2017 const struct i2c_device_id *id)
2018{
2019 struct adv7604_state *state;
2020 struct adv7604_platform_data *pdata = client->dev.platform_data;
2021 struct v4l2_ctrl_handler *hdl;
2022 struct v4l2_subdev *sd;
2023 int err;
2024
2025 /* Check if the adapter supports the needed features */
2026 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
2027 return -EIO;
2028 v4l_dbg(1, debug, client, "detecting adv7604 client on address 0x%x\n",
2029 client->addr << 1);
2030
Laurent Pinchartc02b2112013-05-02 08:29:43 -03002031 state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
Hans Verkuil54450f52012-07-18 05:45:16 -03002032 if (!state) {
2033 v4l_err(client, "Could not allocate adv7604_state memory!\n");
2034 return -ENOMEM;
2035 }
2036
Mats Randgaard25a64ac2013-08-14 07:58:45 -03002037 /* initialize variables */
2038 state->restart_stdi_once = true;
2039 state->prev_input_status = ~0;
2040
Hans Verkuil54450f52012-07-18 05:45:16 -03002041 /* platform data */
2042 if (!pdata) {
2043 v4l_err(client, "No platform data!\n");
Laurent Pinchartc02b2112013-05-02 08:29:43 -03002044 return -ENODEV;
Hans Verkuil54450f52012-07-18 05:45:16 -03002045 }
2046 memcpy(&state->pdata, pdata, sizeof(state->pdata));
2047
2048 sd = &state->sd;
2049 v4l2_i2c_subdev_init(sd, client, &adv7604_ops);
2050 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
2051 state->connector_hdmi = pdata->connector_hdmi;
2052
2053 /* i2c access to adv7604? */
2054 if (adv_smbus_read_byte_data_check(client, 0xfb, false) != 0x68) {
2055 v4l2_info(sd, "not an adv7604 on address 0x%x\n",
2056 client->addr << 1);
Laurent Pinchartc02b2112013-05-02 08:29:43 -03002057 return -ENODEV;
Hans Verkuil54450f52012-07-18 05:45:16 -03002058 }
2059
2060 /* control handlers */
2061 hdl = &state->hdl;
2062 v4l2_ctrl_handler_init(hdl, 9);
2063
2064 v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
2065 V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
2066 v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
2067 V4L2_CID_CONTRAST, 0, 255, 1, 128);
2068 v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
2069 V4L2_CID_SATURATION, 0, 255, 1, 128);
2070 v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
2071 V4L2_CID_HUE, 0, 128, 1, 0);
2072
2073 /* private controls */
2074 state->detect_tx_5v_ctrl = v4l2_ctrl_new_std(hdl, NULL,
Mats Randgaard4a31a932013-12-10 09:45:00 -03002075 V4L2_CID_DV_RX_POWER_PRESENT, 0, 0x0f, 0, 0);
Hans Verkuil54450f52012-07-18 05:45:16 -03002076 state->rgb_quantization_range_ctrl =
2077 v4l2_ctrl_new_std_menu(hdl, &adv7604_ctrl_ops,
2078 V4L2_CID_DV_RX_RGB_RANGE, V4L2_DV_RGB_RANGE_FULL,
2079 0, V4L2_DV_RGB_RANGE_AUTO);
Hans Verkuil54450f52012-07-18 05:45:16 -03002080
2081 /* custom controls */
2082 state->analog_sampling_phase_ctrl =
2083 v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_analog_sampling_phase, NULL);
Hans Verkuil54450f52012-07-18 05:45:16 -03002084 state->free_run_color_manual_ctrl =
2085 v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_free_run_color_manual, NULL);
Hans Verkuil54450f52012-07-18 05:45:16 -03002086 state->free_run_color_ctrl =
2087 v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_free_run_color, NULL);
Hans Verkuil54450f52012-07-18 05:45:16 -03002088
2089 sd->ctrl_handler = hdl;
2090 if (hdl->error) {
2091 err = hdl->error;
2092 goto err_hdl;
2093 }
Hans Verkuil8c0eadb2013-08-22 06:11:17 -03002094 state->detect_tx_5v_ctrl->is_private = true;
2095 state->rgb_quantization_range_ctrl->is_private = true;
2096 state->analog_sampling_phase_ctrl->is_private = true;
2097 state->free_run_color_manual_ctrl->is_private = true;
2098 state->free_run_color_ctrl->is_private = true;
2099
Hans Verkuil54450f52012-07-18 05:45:16 -03002100 if (adv7604_s_detect_tx_5v_ctrl(sd)) {
2101 err = -ENODEV;
2102 goto err_hdl;
2103 }
2104
2105 state->i2c_avlink = adv7604_dummy_client(sd, pdata->i2c_avlink, 0xf3);
2106 state->i2c_cec = adv7604_dummy_client(sd, pdata->i2c_cec, 0xf4);
2107 state->i2c_infoframe = adv7604_dummy_client(sd, pdata->i2c_infoframe, 0xf5);
2108 state->i2c_esdp = adv7604_dummy_client(sd, pdata->i2c_esdp, 0xf6);
2109 state->i2c_dpp = adv7604_dummy_client(sd, pdata->i2c_dpp, 0xf7);
2110 state->i2c_afe = adv7604_dummy_client(sd, pdata->i2c_afe, 0xf8);
2111 state->i2c_repeater = adv7604_dummy_client(sd, pdata->i2c_repeater, 0xf9);
2112 state->i2c_edid = adv7604_dummy_client(sd, pdata->i2c_edid, 0xfa);
2113 state->i2c_hdmi = adv7604_dummy_client(sd, pdata->i2c_hdmi, 0xfb);
2114 state->i2c_test = adv7604_dummy_client(sd, pdata->i2c_test, 0xfc);
2115 state->i2c_cp = adv7604_dummy_client(sd, pdata->i2c_cp, 0xfd);
2116 state->i2c_vdp = adv7604_dummy_client(sd, pdata->i2c_vdp, 0xfe);
2117 if (!state->i2c_avlink || !state->i2c_cec || !state->i2c_infoframe ||
2118 !state->i2c_esdp || !state->i2c_dpp || !state->i2c_afe ||
2119 !state->i2c_repeater || !state->i2c_edid || !state->i2c_hdmi ||
2120 !state->i2c_test || !state->i2c_cp || !state->i2c_vdp) {
2121 err = -ENOMEM;
2122 v4l2_err(sd, "failed to create all i2c clients\n");
2123 goto err_i2c;
2124 }
Hans Verkuil54450f52012-07-18 05:45:16 -03002125
2126 /* work queues */
2127 state->work_queues = create_singlethread_workqueue(client->name);
2128 if (!state->work_queues) {
2129 v4l2_err(sd, "Could not create work queue\n");
2130 err = -ENOMEM;
2131 goto err_i2c;
2132 }
2133
2134 INIT_DELAYED_WORK(&state->delayed_work_enable_hotplug,
2135 adv7604_delayed_work_enable_hotplug);
2136
2137 state->pad.flags = MEDIA_PAD_FL_SOURCE;
2138 err = media_entity_init(&sd->entity, 1, &state->pad, 0);
2139 if (err)
2140 goto err_work_queues;
2141
2142 err = adv7604_core_init(sd);
2143 if (err)
2144 goto err_entity;
2145 v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
2146 client->addr << 1, client->adapter->name);
2147 return 0;
2148
2149err_entity:
2150 media_entity_cleanup(&sd->entity);
2151err_work_queues:
2152 cancel_delayed_work(&state->delayed_work_enable_hotplug);
2153 destroy_workqueue(state->work_queues);
2154err_i2c:
2155 adv7604_unregister_clients(state);
2156err_hdl:
2157 v4l2_ctrl_handler_free(hdl);
Hans Verkuil54450f52012-07-18 05:45:16 -03002158 return err;
2159}
2160
2161/* ----------------------------------------------------------------------- */
2162
2163static int adv7604_remove(struct i2c_client *client)
2164{
2165 struct v4l2_subdev *sd = i2c_get_clientdata(client);
2166 struct adv7604_state *state = to_state(sd);
2167
2168 cancel_delayed_work(&state->delayed_work_enable_hotplug);
2169 destroy_workqueue(state->work_queues);
2170 v4l2_device_unregister_subdev(sd);
2171 media_entity_cleanup(&sd->entity);
2172 adv7604_unregister_clients(to_state(sd));
2173 v4l2_ctrl_handler_free(sd->ctrl_handler);
Hans Verkuil54450f52012-07-18 05:45:16 -03002174 return 0;
2175}
2176
2177/* ----------------------------------------------------------------------- */
2178
2179static struct i2c_device_id adv7604_id[] = {
2180 { "adv7604", 0 },
2181 { }
2182};
2183MODULE_DEVICE_TABLE(i2c, adv7604_id);
2184
2185static struct i2c_driver adv7604_driver = {
2186 .driver = {
2187 .owner = THIS_MODULE,
2188 .name = "adv7604",
2189 },
2190 .probe = adv7604_probe,
2191 .remove = adv7604_remove,
2192 .id_table = adv7604_id,
2193};
2194
2195module_i2c_driver(adv7604_driver);