blob: f9503d2d1ed054d1b6b09713539150a977e1a198 [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;
Mats Randgaarddd08beb2013-12-10 09:57:09 -030075 u16 spa_port_a[2];
Hans Verkuil54450f52012-07-18 05:45:16 -030076 struct v4l2_fract aspect_ratio;
77 u32 rgb_quantization_range;
78 struct workqueue_struct *work_queues;
79 struct delayed_work delayed_work_enable_hotplug;
Hans Verkuilcf9afb12012-10-16 10:12:55 -030080 bool restart_stdi_once;
Hans Verkuil54450f52012-07-18 05:45:16 -030081
82 /* i2c clients */
83 struct i2c_client *i2c_avlink;
84 struct i2c_client *i2c_cec;
85 struct i2c_client *i2c_infoframe;
86 struct i2c_client *i2c_esdp;
87 struct i2c_client *i2c_dpp;
88 struct i2c_client *i2c_afe;
89 struct i2c_client *i2c_repeater;
90 struct i2c_client *i2c_edid;
91 struct i2c_client *i2c_hdmi;
92 struct i2c_client *i2c_test;
93 struct i2c_client *i2c_cp;
94 struct i2c_client *i2c_vdp;
95
96 /* controls */
97 struct v4l2_ctrl *detect_tx_5v_ctrl;
98 struct v4l2_ctrl *analog_sampling_phase_ctrl;
99 struct v4l2_ctrl *free_run_color_manual_ctrl;
100 struct v4l2_ctrl *free_run_color_ctrl;
101 struct v4l2_ctrl *rgb_quantization_range_ctrl;
102};
103
104/* Supported CEA and DMT timings */
105static const struct v4l2_dv_timings adv7604_timings[] = {
106 V4L2_DV_BT_CEA_720X480P59_94,
107 V4L2_DV_BT_CEA_720X576P50,
108 V4L2_DV_BT_CEA_1280X720P24,
109 V4L2_DV_BT_CEA_1280X720P25,
Hans Verkuil54450f52012-07-18 05:45:16 -0300110 V4L2_DV_BT_CEA_1280X720P50,
111 V4L2_DV_BT_CEA_1280X720P60,
112 V4L2_DV_BT_CEA_1920X1080P24,
113 V4L2_DV_BT_CEA_1920X1080P25,
114 V4L2_DV_BT_CEA_1920X1080P30,
115 V4L2_DV_BT_CEA_1920X1080P50,
116 V4L2_DV_BT_CEA_1920X1080P60,
117
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300118 /* sorted by DMT ID */
Hans Verkuil54450f52012-07-18 05:45:16 -0300119 V4L2_DV_BT_DMT_640X350P85,
120 V4L2_DV_BT_DMT_640X400P85,
121 V4L2_DV_BT_DMT_720X400P85,
122 V4L2_DV_BT_DMT_640X480P60,
123 V4L2_DV_BT_DMT_640X480P72,
124 V4L2_DV_BT_DMT_640X480P75,
125 V4L2_DV_BT_DMT_640X480P85,
126 V4L2_DV_BT_DMT_800X600P56,
127 V4L2_DV_BT_DMT_800X600P60,
128 V4L2_DV_BT_DMT_800X600P72,
129 V4L2_DV_BT_DMT_800X600P75,
130 V4L2_DV_BT_DMT_800X600P85,
131 V4L2_DV_BT_DMT_848X480P60,
132 V4L2_DV_BT_DMT_1024X768P60,
133 V4L2_DV_BT_DMT_1024X768P70,
134 V4L2_DV_BT_DMT_1024X768P75,
135 V4L2_DV_BT_DMT_1024X768P85,
136 V4L2_DV_BT_DMT_1152X864P75,
137 V4L2_DV_BT_DMT_1280X768P60_RB,
138 V4L2_DV_BT_DMT_1280X768P60,
139 V4L2_DV_BT_DMT_1280X768P75,
140 V4L2_DV_BT_DMT_1280X768P85,
141 V4L2_DV_BT_DMT_1280X800P60_RB,
142 V4L2_DV_BT_DMT_1280X800P60,
143 V4L2_DV_BT_DMT_1280X800P75,
144 V4L2_DV_BT_DMT_1280X800P85,
145 V4L2_DV_BT_DMT_1280X960P60,
146 V4L2_DV_BT_DMT_1280X960P85,
147 V4L2_DV_BT_DMT_1280X1024P60,
148 V4L2_DV_BT_DMT_1280X1024P75,
149 V4L2_DV_BT_DMT_1280X1024P85,
150 V4L2_DV_BT_DMT_1360X768P60,
151 V4L2_DV_BT_DMT_1400X1050P60_RB,
152 V4L2_DV_BT_DMT_1400X1050P60,
153 V4L2_DV_BT_DMT_1400X1050P75,
154 V4L2_DV_BT_DMT_1400X1050P85,
155 V4L2_DV_BT_DMT_1440X900P60_RB,
156 V4L2_DV_BT_DMT_1440X900P60,
157 V4L2_DV_BT_DMT_1600X1200P60,
158 V4L2_DV_BT_DMT_1680X1050P60_RB,
159 V4L2_DV_BT_DMT_1680X1050P60,
160 V4L2_DV_BT_DMT_1792X1344P60,
161 V4L2_DV_BT_DMT_1856X1392P60,
162 V4L2_DV_BT_DMT_1920X1200P60_RB,
Martin Bugge547ed542013-12-05 10:01:17 -0300163 V4L2_DV_BT_DMT_1366X768P60_RB,
Hans Verkuil54450f52012-07-18 05:45:16 -0300164 V4L2_DV_BT_DMT_1366X768P60,
165 V4L2_DV_BT_DMT_1920X1080P60,
166 { },
167};
168
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300169struct adv7604_video_standards {
170 struct v4l2_dv_timings timings;
171 u8 vid_std;
172 u8 v_freq;
173};
174
175/* sorted by number of lines */
176static const struct adv7604_video_standards adv7604_prim_mode_comp[] = {
177 /* { V4L2_DV_BT_CEA_720X480P59_94, 0x0a, 0x00 }, TODO flickering */
178 { V4L2_DV_BT_CEA_720X576P50, 0x0b, 0x00 },
179 { V4L2_DV_BT_CEA_1280X720P50, 0x19, 0x01 },
180 { V4L2_DV_BT_CEA_1280X720P60, 0x19, 0x00 },
181 { V4L2_DV_BT_CEA_1920X1080P24, 0x1e, 0x04 },
182 { V4L2_DV_BT_CEA_1920X1080P25, 0x1e, 0x03 },
183 { V4L2_DV_BT_CEA_1920X1080P30, 0x1e, 0x02 },
184 { V4L2_DV_BT_CEA_1920X1080P50, 0x1e, 0x01 },
185 { V4L2_DV_BT_CEA_1920X1080P60, 0x1e, 0x00 },
186 /* TODO add 1920x1080P60_RB (CVT timing) */
187 { },
188};
189
190/* sorted by number of lines */
191static const struct adv7604_video_standards adv7604_prim_mode_gr[] = {
192 { V4L2_DV_BT_DMT_640X480P60, 0x08, 0x00 },
193 { V4L2_DV_BT_DMT_640X480P72, 0x09, 0x00 },
194 { V4L2_DV_BT_DMT_640X480P75, 0x0a, 0x00 },
195 { V4L2_DV_BT_DMT_640X480P85, 0x0b, 0x00 },
196 { V4L2_DV_BT_DMT_800X600P56, 0x00, 0x00 },
197 { V4L2_DV_BT_DMT_800X600P60, 0x01, 0x00 },
198 { V4L2_DV_BT_DMT_800X600P72, 0x02, 0x00 },
199 { V4L2_DV_BT_DMT_800X600P75, 0x03, 0x00 },
200 { V4L2_DV_BT_DMT_800X600P85, 0x04, 0x00 },
201 { V4L2_DV_BT_DMT_1024X768P60, 0x0c, 0x00 },
202 { V4L2_DV_BT_DMT_1024X768P70, 0x0d, 0x00 },
203 { V4L2_DV_BT_DMT_1024X768P75, 0x0e, 0x00 },
204 { V4L2_DV_BT_DMT_1024X768P85, 0x0f, 0x00 },
205 { V4L2_DV_BT_DMT_1280X1024P60, 0x05, 0x00 },
206 { V4L2_DV_BT_DMT_1280X1024P75, 0x06, 0x00 },
207 { V4L2_DV_BT_DMT_1360X768P60, 0x12, 0x00 },
208 { V4L2_DV_BT_DMT_1366X768P60, 0x13, 0x00 },
209 { V4L2_DV_BT_DMT_1400X1050P60, 0x14, 0x00 },
210 { V4L2_DV_BT_DMT_1400X1050P75, 0x15, 0x00 },
211 { V4L2_DV_BT_DMT_1600X1200P60, 0x16, 0x00 }, /* TODO not tested */
212 /* TODO add 1600X1200P60_RB (not a DMT timing) */
213 { V4L2_DV_BT_DMT_1680X1050P60, 0x18, 0x00 },
214 { V4L2_DV_BT_DMT_1920X1200P60_RB, 0x19, 0x00 }, /* TODO not tested */
215 { },
216};
217
218/* sorted by number of lines */
219static const struct adv7604_video_standards adv7604_prim_mode_hdmi_comp[] = {
220 { V4L2_DV_BT_CEA_720X480P59_94, 0x0a, 0x00 },
221 { V4L2_DV_BT_CEA_720X576P50, 0x0b, 0x00 },
222 { V4L2_DV_BT_CEA_1280X720P50, 0x13, 0x01 },
223 { V4L2_DV_BT_CEA_1280X720P60, 0x13, 0x00 },
224 { V4L2_DV_BT_CEA_1920X1080P24, 0x1e, 0x04 },
225 { V4L2_DV_BT_CEA_1920X1080P25, 0x1e, 0x03 },
226 { V4L2_DV_BT_CEA_1920X1080P30, 0x1e, 0x02 },
227 { V4L2_DV_BT_CEA_1920X1080P50, 0x1e, 0x01 },
228 { V4L2_DV_BT_CEA_1920X1080P60, 0x1e, 0x00 },
229 { },
230};
231
232/* sorted by number of lines */
233static const struct adv7604_video_standards adv7604_prim_mode_hdmi_gr[] = {
234 { V4L2_DV_BT_DMT_640X480P60, 0x08, 0x00 },
235 { V4L2_DV_BT_DMT_640X480P72, 0x09, 0x00 },
236 { V4L2_DV_BT_DMT_640X480P75, 0x0a, 0x00 },
237 { V4L2_DV_BT_DMT_640X480P85, 0x0b, 0x00 },
238 { V4L2_DV_BT_DMT_800X600P56, 0x00, 0x00 },
239 { V4L2_DV_BT_DMT_800X600P60, 0x01, 0x00 },
240 { V4L2_DV_BT_DMT_800X600P72, 0x02, 0x00 },
241 { V4L2_DV_BT_DMT_800X600P75, 0x03, 0x00 },
242 { V4L2_DV_BT_DMT_800X600P85, 0x04, 0x00 },
243 { V4L2_DV_BT_DMT_1024X768P60, 0x0c, 0x00 },
244 { V4L2_DV_BT_DMT_1024X768P70, 0x0d, 0x00 },
245 { V4L2_DV_BT_DMT_1024X768P75, 0x0e, 0x00 },
246 { V4L2_DV_BT_DMT_1024X768P85, 0x0f, 0x00 },
247 { V4L2_DV_BT_DMT_1280X1024P60, 0x05, 0x00 },
248 { V4L2_DV_BT_DMT_1280X1024P75, 0x06, 0x00 },
249 { },
250};
251
Hans Verkuil54450f52012-07-18 05:45:16 -0300252/* ----------------------------------------------------------------------- */
253
254static inline struct adv7604_state *to_state(struct v4l2_subdev *sd)
255{
256 return container_of(sd, struct adv7604_state, sd);
257}
258
259static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
260{
261 return &container_of(ctrl->handler, struct adv7604_state, hdl)->sd;
262}
263
264static inline unsigned hblanking(const struct v4l2_bt_timings *t)
265{
Hans Verkuileacf8f92013-07-29 08:40:59 -0300266 return V4L2_DV_BT_BLANKING_WIDTH(t);
Hans Verkuil54450f52012-07-18 05:45:16 -0300267}
268
269static inline unsigned htotal(const struct v4l2_bt_timings *t)
270{
Hans Verkuileacf8f92013-07-29 08:40:59 -0300271 return V4L2_DV_BT_FRAME_WIDTH(t);
Hans Verkuil54450f52012-07-18 05:45:16 -0300272}
273
274static inline unsigned vblanking(const struct v4l2_bt_timings *t)
275{
Hans Verkuileacf8f92013-07-29 08:40:59 -0300276 return V4L2_DV_BT_BLANKING_HEIGHT(t);
Hans Verkuil54450f52012-07-18 05:45:16 -0300277}
278
279static inline unsigned vtotal(const struct v4l2_bt_timings *t)
280{
Hans Verkuileacf8f92013-07-29 08:40:59 -0300281 return V4L2_DV_BT_FRAME_HEIGHT(t);
Hans Verkuil54450f52012-07-18 05:45:16 -0300282}
283
284/* ----------------------------------------------------------------------- */
285
286static s32 adv_smbus_read_byte_data_check(struct i2c_client *client,
287 u8 command, bool check)
288{
289 union i2c_smbus_data data;
290
291 if (!i2c_smbus_xfer(client->adapter, client->addr, client->flags,
292 I2C_SMBUS_READ, command,
293 I2C_SMBUS_BYTE_DATA, &data))
294 return data.byte;
295 if (check)
296 v4l_err(client, "error reading %02x, %02x\n",
297 client->addr, command);
298 return -EIO;
299}
300
301static s32 adv_smbus_read_byte_data(struct i2c_client *client, u8 command)
302{
303 return adv_smbus_read_byte_data_check(client, command, true);
304}
305
306static s32 adv_smbus_write_byte_data(struct i2c_client *client,
307 u8 command, u8 value)
308{
309 union i2c_smbus_data data;
310 int err;
311 int i;
312
313 data.byte = value;
314 for (i = 0; i < 3; i++) {
315 err = i2c_smbus_xfer(client->adapter, client->addr,
316 client->flags,
317 I2C_SMBUS_WRITE, command,
318 I2C_SMBUS_BYTE_DATA, &data);
319 if (!err)
320 break;
321 }
322 if (err < 0)
323 v4l_err(client, "error writing %02x, %02x, %02x\n",
324 client->addr, command, value);
325 return err;
326}
327
328static s32 adv_smbus_write_i2c_block_data(struct i2c_client *client,
329 u8 command, unsigned length, const u8 *values)
330{
331 union i2c_smbus_data data;
332
333 if (length > I2C_SMBUS_BLOCK_MAX)
334 length = I2C_SMBUS_BLOCK_MAX;
335 data.block[0] = length;
336 memcpy(data.block + 1, values, length);
337 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
338 I2C_SMBUS_WRITE, command,
339 I2C_SMBUS_I2C_BLOCK_DATA, &data);
340}
341
342/* ----------------------------------------------------------------------- */
343
344static inline int io_read(struct v4l2_subdev *sd, u8 reg)
345{
346 struct i2c_client *client = v4l2_get_subdevdata(sd);
347
348 return adv_smbus_read_byte_data(client, reg);
349}
350
351static inline int io_write(struct v4l2_subdev *sd, u8 reg, u8 val)
352{
353 struct i2c_client *client = v4l2_get_subdevdata(sd);
354
355 return adv_smbus_write_byte_data(client, reg, val);
356}
357
358static inline int io_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
359{
360 return io_write(sd, reg, (io_read(sd, reg) & mask) | val);
361}
362
363static inline int avlink_read(struct v4l2_subdev *sd, u8 reg)
364{
365 struct adv7604_state *state = to_state(sd);
366
367 return adv_smbus_read_byte_data(state->i2c_avlink, reg);
368}
369
370static inline int avlink_write(struct v4l2_subdev *sd, u8 reg, u8 val)
371{
372 struct adv7604_state *state = to_state(sd);
373
374 return adv_smbus_write_byte_data(state->i2c_avlink, reg, val);
375}
376
377static inline int cec_read(struct v4l2_subdev *sd, u8 reg)
378{
379 struct adv7604_state *state = to_state(sd);
380
381 return adv_smbus_read_byte_data(state->i2c_cec, reg);
382}
383
384static inline int cec_write(struct v4l2_subdev *sd, u8 reg, u8 val)
385{
386 struct adv7604_state *state = to_state(sd);
387
388 return adv_smbus_write_byte_data(state->i2c_cec, reg, val);
389}
390
391static inline int cec_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
392{
393 return cec_write(sd, reg, (cec_read(sd, reg) & mask) | val);
394}
395
396static inline int infoframe_read(struct v4l2_subdev *sd, u8 reg)
397{
398 struct adv7604_state *state = to_state(sd);
399
400 return adv_smbus_read_byte_data(state->i2c_infoframe, reg);
401}
402
403static inline int infoframe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
404{
405 struct adv7604_state *state = to_state(sd);
406
407 return adv_smbus_write_byte_data(state->i2c_infoframe, reg, val);
408}
409
410static inline int esdp_read(struct v4l2_subdev *sd, u8 reg)
411{
412 struct adv7604_state *state = to_state(sd);
413
414 return adv_smbus_read_byte_data(state->i2c_esdp, reg);
415}
416
417static inline int esdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
418{
419 struct adv7604_state *state = to_state(sd);
420
421 return adv_smbus_write_byte_data(state->i2c_esdp, reg, val);
422}
423
424static inline int dpp_read(struct v4l2_subdev *sd, u8 reg)
425{
426 struct adv7604_state *state = to_state(sd);
427
428 return adv_smbus_read_byte_data(state->i2c_dpp, reg);
429}
430
431static inline int dpp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
432{
433 struct adv7604_state *state = to_state(sd);
434
435 return adv_smbus_write_byte_data(state->i2c_dpp, reg, val);
436}
437
438static inline int afe_read(struct v4l2_subdev *sd, u8 reg)
439{
440 struct adv7604_state *state = to_state(sd);
441
442 return adv_smbus_read_byte_data(state->i2c_afe, reg);
443}
444
445static inline int afe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
446{
447 struct adv7604_state *state = to_state(sd);
448
449 return adv_smbus_write_byte_data(state->i2c_afe, reg, val);
450}
451
452static inline int rep_read(struct v4l2_subdev *sd, u8 reg)
453{
454 struct adv7604_state *state = to_state(sd);
455
456 return adv_smbus_read_byte_data(state->i2c_repeater, reg);
457}
458
459static inline int rep_write(struct v4l2_subdev *sd, u8 reg, u8 val)
460{
461 struct adv7604_state *state = to_state(sd);
462
463 return adv_smbus_write_byte_data(state->i2c_repeater, reg, val);
464}
465
466static inline int rep_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
467{
468 return rep_write(sd, reg, (rep_read(sd, reg) & mask) | val);
469}
470
471static inline int edid_read(struct v4l2_subdev *sd, u8 reg)
472{
473 struct adv7604_state *state = to_state(sd);
474
475 return adv_smbus_read_byte_data(state->i2c_edid, reg);
476}
477
478static inline int edid_write(struct v4l2_subdev *sd, u8 reg, u8 val)
479{
480 struct adv7604_state *state = to_state(sd);
481
482 return adv_smbus_write_byte_data(state->i2c_edid, reg, val);
483}
484
485static inline int edid_read_block(struct v4l2_subdev *sd, unsigned len, u8 *val)
486{
487 struct adv7604_state *state = to_state(sd);
488 struct i2c_client *client = state->i2c_edid;
489 u8 msgbuf0[1] = { 0 };
490 u8 msgbuf1[256];
Shubhrajyoti D09f29672012-10-25 01:02:36 -0300491 struct i2c_msg msg[2] = {
492 {
493 .addr = client->addr,
494 .len = 1,
495 .buf = msgbuf0
496 },
497 {
498 .addr = client->addr,
499 .flags = I2C_M_RD,
500 .len = len,
501 .buf = msgbuf1
502 },
503 };
Hans Verkuil54450f52012-07-18 05:45:16 -0300504
505 if (i2c_transfer(client->adapter, msg, 2) < 0)
506 return -EIO;
507 memcpy(val, msgbuf1, len);
508 return 0;
509}
510
Mats Randgaarddd08beb2013-12-10 09:57:09 -0300511static inline int edid_write_block(struct v4l2_subdev *sd,
512 unsigned len, const u8 *val)
513{
514 struct adv7604_state *state = to_state(sd);
515 int err = 0;
516 int i;
517
518 v4l2_dbg(2, debug, sd, "%s: write EDID block (%d byte)\n", __func__, len);
519
520 for (i = 0; !err && i < len; i += I2C_SMBUS_BLOCK_MAX)
521 err = adv_smbus_write_i2c_block_data(state->i2c_edid, i,
522 I2C_SMBUS_BLOCK_MAX, val + i);
523 return err;
524}
525
Hans Verkuil54450f52012-07-18 05:45:16 -0300526static void adv7604_delayed_work_enable_hotplug(struct work_struct *work)
527{
528 struct delayed_work *dwork = to_delayed_work(work);
529 struct adv7604_state *state = container_of(dwork, struct adv7604_state,
530 delayed_work_enable_hotplug);
531 struct v4l2_subdev *sd = &state->sd;
532
533 v4l2_dbg(2, debug, sd, "%s: enable hotplug\n", __func__);
534
Mats Randgaard4a31a932013-12-10 09:45:00 -0300535 v4l2_subdev_notify(sd, ADV7604_HOTPLUG, (void *)&state->edid.present);
Hans Verkuil54450f52012-07-18 05:45:16 -0300536}
537
Hans Verkuil54450f52012-07-18 05:45:16 -0300538static inline int hdmi_read(struct v4l2_subdev *sd, u8 reg)
539{
540 struct adv7604_state *state = to_state(sd);
541
542 return adv_smbus_read_byte_data(state->i2c_hdmi, reg);
543}
544
Laurent Pinchart51182a92014-01-08 19:30:37 -0300545static u16 hdmi_read16(struct v4l2_subdev *sd, u8 reg, u16 mask)
546{
547 return ((hdmi_read(sd, reg) << 8) | hdmi_read(sd, reg + 1)) & mask;
548}
549
Hans Verkuil54450f52012-07-18 05:45:16 -0300550static inline int hdmi_write(struct v4l2_subdev *sd, u8 reg, u8 val)
551{
552 struct adv7604_state *state = to_state(sd);
553
554 return adv_smbus_write_byte_data(state->i2c_hdmi, reg, val);
555}
556
Mats Randgaard4a31a932013-12-10 09:45:00 -0300557static inline int hdmi_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
558{
559 return hdmi_write(sd, reg, (hdmi_read(sd, reg) & mask) | val);
560}
561
Hans Verkuil54450f52012-07-18 05:45:16 -0300562static inline int test_read(struct v4l2_subdev *sd, u8 reg)
563{
564 struct adv7604_state *state = to_state(sd);
565
566 return adv_smbus_read_byte_data(state->i2c_test, reg);
567}
568
569static inline int test_write(struct v4l2_subdev *sd, u8 reg, u8 val)
570{
571 struct adv7604_state *state = to_state(sd);
572
573 return adv_smbus_write_byte_data(state->i2c_test, reg, val);
574}
575
576static inline int cp_read(struct v4l2_subdev *sd, u8 reg)
577{
578 struct adv7604_state *state = to_state(sd);
579
580 return adv_smbus_read_byte_data(state->i2c_cp, reg);
581}
582
Laurent Pinchart51182a92014-01-08 19:30:37 -0300583static u16 cp_read16(struct v4l2_subdev *sd, u8 reg, u16 mask)
584{
585 return ((cp_read(sd, reg) << 8) | cp_read(sd, reg + 1)) & mask;
586}
587
Hans Verkuil54450f52012-07-18 05:45:16 -0300588static inline int cp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
589{
590 struct adv7604_state *state = to_state(sd);
591
592 return adv_smbus_write_byte_data(state->i2c_cp, reg, val);
593}
594
595static inline int cp_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
596{
597 return cp_write(sd, reg, (cp_read(sd, reg) & mask) | val);
598}
599
600static inline int vdp_read(struct v4l2_subdev *sd, u8 reg)
601{
602 struct adv7604_state *state = to_state(sd);
603
604 return adv_smbus_read_byte_data(state->i2c_vdp, reg);
605}
606
607static inline int vdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
608{
609 struct adv7604_state *state = to_state(sd);
610
611 return adv_smbus_write_byte_data(state->i2c_vdp, reg, val);
612}
613
614/* ----------------------------------------------------------------------- */
615
Mats Randgaard4a31a932013-12-10 09:45:00 -0300616static inline bool is_analog_input(struct v4l2_subdev *sd)
617{
618 struct adv7604_state *state = to_state(sd);
619
620 return state->selected_input == ADV7604_INPUT_VGA_RGB ||
621 state->selected_input == ADV7604_INPUT_VGA_COMP;
622}
623
624static inline bool is_digital_input(struct v4l2_subdev *sd)
625{
626 struct adv7604_state *state = to_state(sd);
627
628 return state->selected_input == ADV7604_INPUT_HDMI_PORT_A ||
629 state->selected_input == ADV7604_INPUT_HDMI_PORT_B ||
630 state->selected_input == ADV7604_INPUT_HDMI_PORT_C ||
631 state->selected_input == ADV7604_INPUT_HDMI_PORT_D;
632}
633
634/* ----------------------------------------------------------------------- */
635
Hans Verkuil54450f52012-07-18 05:45:16 -0300636#ifdef CONFIG_VIDEO_ADV_DEBUG
637static void adv7604_inv_register(struct v4l2_subdev *sd)
638{
639 v4l2_info(sd, "0x000-0x0ff: IO Map\n");
640 v4l2_info(sd, "0x100-0x1ff: AVLink Map\n");
641 v4l2_info(sd, "0x200-0x2ff: CEC Map\n");
642 v4l2_info(sd, "0x300-0x3ff: InfoFrame Map\n");
643 v4l2_info(sd, "0x400-0x4ff: ESDP Map\n");
644 v4l2_info(sd, "0x500-0x5ff: DPP Map\n");
645 v4l2_info(sd, "0x600-0x6ff: AFE Map\n");
646 v4l2_info(sd, "0x700-0x7ff: Repeater Map\n");
647 v4l2_info(sd, "0x800-0x8ff: EDID Map\n");
648 v4l2_info(sd, "0x900-0x9ff: HDMI Map\n");
649 v4l2_info(sd, "0xa00-0xaff: Test Map\n");
650 v4l2_info(sd, "0xb00-0xbff: CP Map\n");
651 v4l2_info(sd, "0xc00-0xcff: VDP Map\n");
652}
653
654static int adv7604_g_register(struct v4l2_subdev *sd,
655 struct v4l2_dbg_register *reg)
656{
Hans Verkuil54450f52012-07-18 05:45:16 -0300657 reg->size = 1;
658 switch (reg->reg >> 8) {
659 case 0:
660 reg->val = io_read(sd, reg->reg & 0xff);
661 break;
662 case 1:
663 reg->val = avlink_read(sd, reg->reg & 0xff);
664 break;
665 case 2:
666 reg->val = cec_read(sd, reg->reg & 0xff);
667 break;
668 case 3:
669 reg->val = infoframe_read(sd, reg->reg & 0xff);
670 break;
671 case 4:
672 reg->val = esdp_read(sd, reg->reg & 0xff);
673 break;
674 case 5:
675 reg->val = dpp_read(sd, reg->reg & 0xff);
676 break;
677 case 6:
678 reg->val = afe_read(sd, reg->reg & 0xff);
679 break;
680 case 7:
681 reg->val = rep_read(sd, reg->reg & 0xff);
682 break;
683 case 8:
684 reg->val = edid_read(sd, reg->reg & 0xff);
685 break;
686 case 9:
687 reg->val = hdmi_read(sd, reg->reg & 0xff);
688 break;
689 case 0xa:
690 reg->val = test_read(sd, reg->reg & 0xff);
691 break;
692 case 0xb:
693 reg->val = cp_read(sd, reg->reg & 0xff);
694 break;
695 case 0xc:
696 reg->val = vdp_read(sd, reg->reg & 0xff);
697 break;
698 default:
699 v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
700 adv7604_inv_register(sd);
701 break;
702 }
703 return 0;
704}
705
706static int adv7604_s_register(struct v4l2_subdev *sd,
Hans Verkuil977ba3b2013-03-24 08:28:46 -0300707 const struct v4l2_dbg_register *reg)
Hans Verkuil54450f52012-07-18 05:45:16 -0300708{
Hans Verkuil15774612013-12-10 10:02:43 -0300709 u8 val = reg->val & 0xff;
710
Hans Verkuil54450f52012-07-18 05:45:16 -0300711 switch (reg->reg >> 8) {
712 case 0:
Hans Verkuil15774612013-12-10 10:02:43 -0300713 io_write(sd, reg->reg & 0xff, val);
Hans Verkuil54450f52012-07-18 05:45:16 -0300714 break;
715 case 1:
Hans Verkuil15774612013-12-10 10:02:43 -0300716 avlink_write(sd, reg->reg & 0xff, val);
Hans Verkuil54450f52012-07-18 05:45:16 -0300717 break;
718 case 2:
Hans Verkuil15774612013-12-10 10:02:43 -0300719 cec_write(sd, reg->reg & 0xff, val);
Hans Verkuil54450f52012-07-18 05:45:16 -0300720 break;
721 case 3:
Hans Verkuil15774612013-12-10 10:02:43 -0300722 infoframe_write(sd, reg->reg & 0xff, val);
Hans Verkuil54450f52012-07-18 05:45:16 -0300723 break;
724 case 4:
Hans Verkuil15774612013-12-10 10:02:43 -0300725 esdp_write(sd, reg->reg & 0xff, val);
Hans Verkuil54450f52012-07-18 05:45:16 -0300726 break;
727 case 5:
Hans Verkuil15774612013-12-10 10:02:43 -0300728 dpp_write(sd, reg->reg & 0xff, val);
Hans Verkuil54450f52012-07-18 05:45:16 -0300729 break;
730 case 6:
Hans Verkuil15774612013-12-10 10:02:43 -0300731 afe_write(sd, reg->reg & 0xff, val);
Hans Verkuil54450f52012-07-18 05:45:16 -0300732 break;
733 case 7:
Hans Verkuil15774612013-12-10 10:02:43 -0300734 rep_write(sd, reg->reg & 0xff, val);
Hans Verkuil54450f52012-07-18 05:45:16 -0300735 break;
736 case 8:
Hans Verkuil15774612013-12-10 10:02:43 -0300737 edid_write(sd, reg->reg & 0xff, val);
Hans Verkuil54450f52012-07-18 05:45:16 -0300738 break;
739 case 9:
Hans Verkuil15774612013-12-10 10:02:43 -0300740 hdmi_write(sd, reg->reg & 0xff, val);
Hans Verkuil54450f52012-07-18 05:45:16 -0300741 break;
742 case 0xa:
Hans Verkuil15774612013-12-10 10:02:43 -0300743 test_write(sd, reg->reg & 0xff, val);
Hans Verkuil54450f52012-07-18 05:45:16 -0300744 break;
745 case 0xb:
Hans Verkuil15774612013-12-10 10:02:43 -0300746 cp_write(sd, reg->reg & 0xff, val);
Hans Verkuil54450f52012-07-18 05:45:16 -0300747 break;
748 case 0xc:
Hans Verkuil15774612013-12-10 10:02:43 -0300749 vdp_write(sd, reg->reg & 0xff, val);
Hans Verkuil54450f52012-07-18 05:45:16 -0300750 break;
751 default:
752 v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
753 adv7604_inv_register(sd);
754 break;
755 }
756 return 0;
757}
758#endif
759
760static int adv7604_s_detect_tx_5v_ctrl(struct v4l2_subdev *sd)
761{
762 struct adv7604_state *state = to_state(sd);
Mats Randgaard4a31a932013-12-10 09:45:00 -0300763 u8 reg_io_6f = io_read(sd, 0x6f);
Hans Verkuil54450f52012-07-18 05:45:16 -0300764
Hans Verkuil54450f52012-07-18 05:45:16 -0300765 return v4l2_ctrl_s_ctrl(state->detect_tx_5v_ctrl,
Mats Randgaard4a31a932013-12-10 09:45:00 -0300766 ((reg_io_6f & 0x10) >> 4) |
767 ((reg_io_6f & 0x08) >> 2) |
768 (reg_io_6f & 0x04) |
769 ((reg_io_6f & 0x02) << 2));
Hans Verkuil54450f52012-07-18 05:45:16 -0300770}
771
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300772static int find_and_set_predefined_video_timings(struct v4l2_subdev *sd,
773 u8 prim_mode,
774 const struct adv7604_video_standards *predef_vid_timings,
775 const struct v4l2_dv_timings *timings)
Hans Verkuil54450f52012-07-18 05:45:16 -0300776{
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300777 int i;
778
779 for (i = 0; predef_vid_timings[i].timings.bt.width; i++) {
Hans Verkuilef1ed8f2013-08-15 08:28:47 -0300780 if (!v4l2_match_dv_timings(timings, &predef_vid_timings[i].timings,
Mats Randgaard4a31a932013-12-10 09:45:00 -0300781 is_digital_input(sd) ? 250000 : 1000000))
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300782 continue;
783 io_write(sd, 0x00, predef_vid_timings[i].vid_std); /* video std */
784 io_write(sd, 0x01, (predef_vid_timings[i].v_freq << 4) +
785 prim_mode); /* v_freq and prim mode */
786 return 0;
787 }
788
789 return -1;
790}
791
792static int configure_predefined_video_timings(struct v4l2_subdev *sd,
793 struct v4l2_dv_timings *timings)
794{
795 struct adv7604_state *state = to_state(sd);
796 int err;
797
798 v4l2_dbg(1, debug, sd, "%s", __func__);
799
800 /* reset to default values */
801 io_write(sd, 0x16, 0x43);
802 io_write(sd, 0x17, 0x5a);
803 /* disable embedded syncs for auto graphics mode */
804 cp_write_and_or(sd, 0x81, 0xef, 0x00);
805 cp_write(sd, 0x8f, 0x00);
806 cp_write(sd, 0x90, 0x00);
807 cp_write(sd, 0xa2, 0x00);
808 cp_write(sd, 0xa3, 0x00);
809 cp_write(sd, 0xa4, 0x00);
810 cp_write(sd, 0xa5, 0x00);
811 cp_write(sd, 0xa6, 0x00);
812 cp_write(sd, 0xa7, 0x00);
813 cp_write(sd, 0xab, 0x00);
814 cp_write(sd, 0xac, 0x00);
815
Mats Randgaard4a31a932013-12-10 09:45:00 -0300816 if (is_analog_input(sd)) {
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300817 err = find_and_set_predefined_video_timings(sd,
818 0x01, adv7604_prim_mode_comp, timings);
819 if (err)
820 err = find_and_set_predefined_video_timings(sd,
821 0x02, adv7604_prim_mode_gr, timings);
Mats Randgaard4a31a932013-12-10 09:45:00 -0300822 } else if (is_digital_input(sd)) {
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300823 err = find_and_set_predefined_video_timings(sd,
824 0x05, adv7604_prim_mode_hdmi_comp, timings);
825 if (err)
826 err = find_and_set_predefined_video_timings(sd,
827 0x06, adv7604_prim_mode_hdmi_gr, timings);
Mats Randgaard4a31a932013-12-10 09:45:00 -0300828 } else {
829 v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
830 __func__, state->selected_input);
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300831 err = -1;
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300832 }
833
834
835 return err;
836}
837
838static void configure_custom_video_timings(struct v4l2_subdev *sd,
839 const struct v4l2_bt_timings *bt)
840{
841 struct adv7604_state *state = to_state(sd);
Hans Verkuil54450f52012-07-18 05:45:16 -0300842 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300843 u32 width = htotal(bt);
844 u32 height = vtotal(bt);
845 u16 cp_start_sav = bt->hsync + bt->hbackporch - 4;
846 u16 cp_start_eav = width - bt->hfrontporch;
847 u16 cp_start_vbi = height - bt->vfrontporch;
848 u16 cp_end_vbi = bt->vsync + bt->vbackporch;
849 u16 ch1_fr_ll = (((u32)bt->pixelclock / 100) > 0) ?
850 ((width * (ADV7604_fsc / 100)) / ((u32)bt->pixelclock / 100)) : 0;
851 const u8 pll[2] = {
852 0xc0 | ((width >> 8) & 0x1f),
853 width & 0xff
854 };
Hans Verkuil54450f52012-07-18 05:45:16 -0300855
856 v4l2_dbg(2, debug, sd, "%s\n", __func__);
857
Mats Randgaard4a31a932013-12-10 09:45:00 -0300858 if (is_analog_input(sd)) {
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300859 /* auto graphics */
860 io_write(sd, 0x00, 0x07); /* video std */
861 io_write(sd, 0x01, 0x02); /* prim mode */
862 /* enable embedded syncs for auto graphics mode */
863 cp_write_and_or(sd, 0x81, 0xef, 0x10);
Hans Verkuil54450f52012-07-18 05:45:16 -0300864
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300865 /* Should only be set in auto-graphics mode [REF_02, p. 91-92] */
Hans Verkuil54450f52012-07-18 05:45:16 -0300866 /* setup PLL_DIV_MAN_EN and PLL_DIV_RATIO */
867 /* IO-map reg. 0x16 and 0x17 should be written in sequence */
Mats Randgaard4a31a932013-12-10 09:45:00 -0300868 if (adv_smbus_write_i2c_block_data(client, 0x16, 2, pll))
Hans Verkuil54450f52012-07-18 05:45:16 -0300869 v4l2_err(sd, "writing to reg 0x16 and 0x17 failed\n");
Hans Verkuil54450f52012-07-18 05:45:16 -0300870
871 /* active video - horizontal timing */
Hans Verkuil54450f52012-07-18 05:45:16 -0300872 cp_write(sd, 0xa2, (cp_start_sav >> 4) & 0xff);
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300873 cp_write(sd, 0xa3, ((cp_start_sav & 0x0f) << 4) |
Mats Randgaard4a31a932013-12-10 09:45:00 -0300874 ((cp_start_eav >> 8) & 0x0f));
Hans Verkuil54450f52012-07-18 05:45:16 -0300875 cp_write(sd, 0xa4, cp_start_eav & 0xff);
876
877 /* active video - vertical timing */
Hans Verkuil54450f52012-07-18 05:45:16 -0300878 cp_write(sd, 0xa5, (cp_start_vbi >> 4) & 0xff);
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300879 cp_write(sd, 0xa6, ((cp_start_vbi & 0xf) << 4) |
Mats Randgaard4a31a932013-12-10 09:45:00 -0300880 ((cp_end_vbi >> 8) & 0xf));
Hans Verkuil54450f52012-07-18 05:45:16 -0300881 cp_write(sd, 0xa7, cp_end_vbi & 0xff);
Mats Randgaard4a31a932013-12-10 09:45:00 -0300882 } else if (is_digital_input(sd)) {
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300883 /* set default prim_mode/vid_std for HDMI
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -0300884 according to [REF_03, c. 4.2] */
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300885 io_write(sd, 0x00, 0x02); /* video std */
886 io_write(sd, 0x01, 0x06); /* prim mode */
Mats Randgaard4a31a932013-12-10 09:45:00 -0300887 } else {
888 v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
889 __func__, state->selected_input);
Hans Verkuil54450f52012-07-18 05:45:16 -0300890 }
Hans Verkuil54450f52012-07-18 05:45:16 -0300891
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300892 cp_write(sd, 0x8f, (ch1_fr_ll >> 8) & 0x7);
893 cp_write(sd, 0x90, ch1_fr_ll & 0xff);
894 cp_write(sd, 0xab, (height >> 4) & 0xff);
895 cp_write(sd, 0xac, (height & 0x0f) << 4);
896}
Hans Verkuil54450f52012-07-18 05:45:16 -0300897
Mats Randgaard5c6c6342013-12-05 10:39:04 -0300898static void adv7604_set_offset(struct v4l2_subdev *sd, bool auto_offset, u16 offset_a, u16 offset_b, u16 offset_c)
899{
900 struct adv7604_state *state = to_state(sd);
901 u8 offset_buf[4];
902
903 if (auto_offset) {
904 offset_a = 0x3ff;
905 offset_b = 0x3ff;
906 offset_c = 0x3ff;
907 }
908
909 v4l2_dbg(2, debug, sd, "%s: %s offset: a = 0x%x, b = 0x%x, c = 0x%x\n",
910 __func__, auto_offset ? "Auto" : "Manual",
911 offset_a, offset_b, offset_c);
912
913 offset_buf[0] = (cp_read(sd, 0x77) & 0xc0) | ((offset_a & 0x3f0) >> 4);
914 offset_buf[1] = ((offset_a & 0x00f) << 4) | ((offset_b & 0x3c0) >> 6);
915 offset_buf[2] = ((offset_b & 0x03f) << 2) | ((offset_c & 0x300) >> 8);
916 offset_buf[3] = offset_c & 0x0ff;
917
918 /* Registers must be written in this order with no i2c access in between */
919 if (adv_smbus_write_i2c_block_data(state->i2c_cp, 0x77, 4, offset_buf))
920 v4l2_err(sd, "%s: i2c error writing to CP reg 0x77, 0x78, 0x79, 0x7a\n", __func__);
921}
922
923static void adv7604_set_gain(struct v4l2_subdev *sd, bool auto_gain, u16 gain_a, u16 gain_b, u16 gain_c)
924{
925 struct adv7604_state *state = to_state(sd);
926 u8 gain_buf[4];
927 u8 gain_man = 1;
928 u8 agc_mode_man = 1;
929
930 if (auto_gain) {
931 gain_man = 0;
932 agc_mode_man = 0;
933 gain_a = 0x100;
934 gain_b = 0x100;
935 gain_c = 0x100;
936 }
937
938 v4l2_dbg(2, debug, sd, "%s: %s gain: a = 0x%x, b = 0x%x, c = 0x%x\n",
939 __func__, auto_gain ? "Auto" : "Manual",
940 gain_a, gain_b, gain_c);
941
942 gain_buf[0] = ((gain_man << 7) | (agc_mode_man << 6) | ((gain_a & 0x3f0) >> 4));
943 gain_buf[1] = (((gain_a & 0x00f) << 4) | ((gain_b & 0x3c0) >> 6));
944 gain_buf[2] = (((gain_b & 0x03f) << 2) | ((gain_c & 0x300) >> 8));
945 gain_buf[3] = ((gain_c & 0x0ff));
946
947 /* Registers must be written in this order with no i2c access in between */
948 if (adv_smbus_write_i2c_block_data(state->i2c_cp, 0x73, 4, gain_buf))
949 v4l2_err(sd, "%s: i2c error writing to CP reg 0x73, 0x74, 0x75, 0x76\n", __func__);
950}
951
Hans Verkuil54450f52012-07-18 05:45:16 -0300952static void set_rgb_quantization_range(struct v4l2_subdev *sd)
953{
954 struct adv7604_state *state = to_state(sd);
Mats Randgaard5c6c6342013-12-05 10:39:04 -0300955 bool rgb_output = io_read(sd, 0x02) & 0x02;
956 bool hdmi_signal = hdmi_read(sd, 0x05) & 0x80;
Hans Verkuil54450f52012-07-18 05:45:16 -0300957
Mats Randgaard5c6c6342013-12-05 10:39:04 -0300958 v4l2_dbg(2, debug, sd, "%s: RGB quantization range: %d, RGB out: %d, HDMI: %d\n",
959 __func__, state->rgb_quantization_range,
960 rgb_output, hdmi_signal);
961
962 adv7604_set_gain(sd, true, 0x0, 0x0, 0x0);
963 adv7604_set_offset(sd, true, 0x0, 0x0, 0x0);
Mats Randgaard98332392013-12-05 10:05:58 -0300964
Hans Verkuil54450f52012-07-18 05:45:16 -0300965 switch (state->rgb_quantization_range) {
966 case V4L2_DV_RGB_RANGE_AUTO:
Mats Randgaard98332392013-12-05 10:05:58 -0300967 if (state->selected_input == ADV7604_INPUT_VGA_RGB) {
968 /* Receiving analog RGB signal
969 * Set RGB full range (0-255) */
970 io_write_and_or(sd, 0x02, 0x0f, 0x10);
971 break;
972 }
Hans Verkuil54450f52012-07-18 05:45:16 -0300973
Mats Randgaard98332392013-12-05 10:05:58 -0300974 if (state->selected_input == ADV7604_INPUT_VGA_COMP) {
975 /* Receiving analog YPbPr signal
976 * Set automode */
Hans Verkuil6b0d5d32012-10-16 06:40:45 -0300977 io_write_and_or(sd, 0x02, 0x0f, 0xf0);
Mats Randgaard98332392013-12-05 10:05:58 -0300978 break;
979 }
980
Mats Randgaard5c6c6342013-12-05 10:39:04 -0300981 if (hdmi_signal) {
Mats Randgaard98332392013-12-05 10:05:58 -0300982 /* Receiving HDMI signal
983 * Set automode */
984 io_write_and_or(sd, 0x02, 0x0f, 0xf0);
985 break;
986 }
987
988 /* Receiving DVI-D signal
989 * ADV7604 selects RGB limited range regardless of
990 * input format (CE/IT) in automatic mode */
991 if (state->timings.bt.standards & V4L2_DV_BT_STD_CEA861) {
992 /* RGB limited range (16-235) */
993 io_write_and_or(sd, 0x02, 0x0f, 0x00);
994 } else {
995 /* RGB full range (0-255) */
996 io_write_and_or(sd, 0x02, 0x0f, 0x10);
Mats Randgaard5c6c6342013-12-05 10:39:04 -0300997
998 if (is_digital_input(sd) && rgb_output) {
999 adv7604_set_offset(sd, false, 0x40, 0x40, 0x40);
1000 } else {
1001 adv7604_set_gain(sd, false, 0xe0, 0xe0, 0xe0);
1002 adv7604_set_offset(sd, false, 0x70, 0x70, 0x70);
1003 }
Hans Verkuil54450f52012-07-18 05:45:16 -03001004 }
1005 break;
1006 case V4L2_DV_RGB_RANGE_LIMITED:
Mats Randgaardd261e842013-12-05 10:17:15 -03001007 if (state->selected_input == ADV7604_INPUT_VGA_COMP) {
1008 /* YCrCb limited range (16-235) */
1009 io_write_and_or(sd, 0x02, 0x0f, 0x20);
Mats Randgaard5c6c6342013-12-05 10:39:04 -03001010 break;
Mats Randgaardd261e842013-12-05 10:17:15 -03001011 }
Mats Randgaard5c6c6342013-12-05 10:39:04 -03001012
1013 /* RGB limited range (16-235) */
1014 io_write_and_or(sd, 0x02, 0x0f, 0x00);
1015
Hans Verkuil54450f52012-07-18 05:45:16 -03001016 break;
1017 case V4L2_DV_RGB_RANGE_FULL:
Mats Randgaardd261e842013-12-05 10:17:15 -03001018 if (state->selected_input == ADV7604_INPUT_VGA_COMP) {
1019 /* YCrCb full range (0-255) */
1020 io_write_and_or(sd, 0x02, 0x0f, 0x60);
Mats Randgaard5c6c6342013-12-05 10:39:04 -03001021 break;
1022 }
1023
1024 /* RGB full range (0-255) */
1025 io_write_and_or(sd, 0x02, 0x0f, 0x10);
1026
1027 if (is_analog_input(sd) || hdmi_signal)
1028 break;
1029
1030 /* Adjust gain/offset for DVI-D signals only */
1031 if (rgb_output) {
1032 adv7604_set_offset(sd, false, 0x40, 0x40, 0x40);
Mats Randgaardd261e842013-12-05 10:17:15 -03001033 } else {
Mats Randgaard5c6c6342013-12-05 10:39:04 -03001034 adv7604_set_gain(sd, false, 0xe0, 0xe0, 0xe0);
1035 adv7604_set_offset(sd, false, 0x70, 0x70, 0x70);
Mats Randgaardd261e842013-12-05 10:17:15 -03001036 }
Hans Verkuil54450f52012-07-18 05:45:16 -03001037 break;
1038 }
1039}
1040
Hans Verkuil54450f52012-07-18 05:45:16 -03001041static int adv7604_s_ctrl(struct v4l2_ctrl *ctrl)
1042{
1043 struct v4l2_subdev *sd = to_sd(ctrl);
1044 struct adv7604_state *state = to_state(sd);
1045
1046 switch (ctrl->id) {
1047 case V4L2_CID_BRIGHTNESS:
1048 cp_write(sd, 0x3c, ctrl->val);
1049 return 0;
1050 case V4L2_CID_CONTRAST:
1051 cp_write(sd, 0x3a, ctrl->val);
1052 return 0;
1053 case V4L2_CID_SATURATION:
1054 cp_write(sd, 0x3b, ctrl->val);
1055 return 0;
1056 case V4L2_CID_HUE:
1057 cp_write(sd, 0x3d, ctrl->val);
1058 return 0;
1059 case V4L2_CID_DV_RX_RGB_RANGE:
1060 state->rgb_quantization_range = ctrl->val;
1061 set_rgb_quantization_range(sd);
1062 return 0;
1063 case V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE:
1064 /* Set the analog sampling phase. This is needed to find the
1065 best sampling phase for analog video: an application or
1066 driver has to try a number of phases and analyze the picture
1067 quality before settling on the best performing phase. */
1068 afe_write(sd, 0xc8, ctrl->val);
1069 return 0;
1070 case V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL:
1071 /* Use the default blue color for free running mode,
1072 or supply your own. */
1073 cp_write_and_or(sd, 0xbf, ~0x04, (ctrl->val << 2));
1074 return 0;
1075 case V4L2_CID_ADV_RX_FREE_RUN_COLOR:
1076 cp_write(sd, 0xc0, (ctrl->val & 0xff0000) >> 16);
1077 cp_write(sd, 0xc1, (ctrl->val & 0x00ff00) >> 8);
1078 cp_write(sd, 0xc2, (u8)(ctrl->val & 0x0000ff));
1079 return 0;
1080 }
1081 return -EINVAL;
1082}
1083
Hans Verkuil54450f52012-07-18 05:45:16 -03001084/* ----------------------------------------------------------------------- */
1085
1086static inline bool no_power(struct v4l2_subdev *sd)
1087{
1088 /* Entire chip or CP powered off */
1089 return io_read(sd, 0x0c) & 0x24;
1090}
1091
1092static inline bool no_signal_tmds(struct v4l2_subdev *sd)
1093{
Mats Randgaard4a31a932013-12-10 09:45:00 -03001094 struct adv7604_state *state = to_state(sd);
1095
1096 return !(io_read(sd, 0x6a) & (0x10 >> state->selected_input));
Hans Verkuil54450f52012-07-18 05:45:16 -03001097}
1098
1099static inline bool no_lock_tmds(struct v4l2_subdev *sd)
1100{
1101 return (io_read(sd, 0x6a) & 0xe0) != 0xe0;
1102}
1103
Martin Buggebb88f322013-08-14 08:52:46 -03001104static inline bool is_hdmi(struct v4l2_subdev *sd)
1105{
1106 return hdmi_read(sd, 0x05) & 0x80;
1107}
1108
Hans Verkuil54450f52012-07-18 05:45:16 -03001109static inline bool no_lock_sspd(struct v4l2_subdev *sd)
1110{
1111 /* TODO channel 2 */
1112 return ((cp_read(sd, 0xb5) & 0xd0) != 0xd0);
1113}
1114
1115static inline bool no_lock_stdi(struct v4l2_subdev *sd)
1116{
1117 /* TODO channel 2 */
1118 return !(cp_read(sd, 0xb1) & 0x80);
1119}
1120
1121static inline bool no_signal(struct v4l2_subdev *sd)
1122{
Hans Verkuil54450f52012-07-18 05:45:16 -03001123 bool ret;
1124
1125 ret = no_power(sd);
1126
1127 ret |= no_lock_stdi(sd);
1128 ret |= no_lock_sspd(sd);
1129
Mats Randgaard4a31a932013-12-10 09:45:00 -03001130 if (is_digital_input(sd)) {
Hans Verkuil54450f52012-07-18 05:45:16 -03001131 ret |= no_lock_tmds(sd);
1132 ret |= no_signal_tmds(sd);
1133 }
1134
1135 return ret;
1136}
1137
1138static inline bool no_lock_cp(struct v4l2_subdev *sd)
1139{
1140 /* CP has detected a non standard number of lines on the incoming
1141 video compared to what it is configured to receive by s_dv_timings */
1142 return io_read(sd, 0x12) & 0x01;
1143}
1144
1145static int adv7604_g_input_status(struct v4l2_subdev *sd, u32 *status)
1146{
Hans Verkuil54450f52012-07-18 05:45:16 -03001147 *status = 0;
1148 *status |= no_power(sd) ? V4L2_IN_ST_NO_POWER : 0;
1149 *status |= no_signal(sd) ? V4L2_IN_ST_NO_SIGNAL : 0;
1150 if (no_lock_cp(sd))
Mats Randgaard4a31a932013-12-10 09:45:00 -03001151 *status |= is_digital_input(sd) ? V4L2_IN_ST_NO_SYNC : V4L2_IN_ST_NO_H_LOCK;
Hans Verkuil54450f52012-07-18 05:45:16 -03001152
1153 v4l2_dbg(1, debug, sd, "%s: status = 0x%x\n", __func__, *status);
1154
1155 return 0;
1156}
1157
1158/* ----------------------------------------------------------------------- */
1159
Hans Verkuil54450f52012-07-18 05:45:16 -03001160struct stdi_readback {
1161 u16 bl, lcf, lcvs;
1162 u8 hs_pol, vs_pol;
1163 bool interlaced;
1164};
1165
1166static int stdi2dv_timings(struct v4l2_subdev *sd,
1167 struct stdi_readback *stdi,
1168 struct v4l2_dv_timings *timings)
1169{
1170 struct adv7604_state *state = to_state(sd);
1171 u32 hfreq = (ADV7604_fsc * 8) / stdi->bl;
1172 u32 pix_clk;
1173 int i;
1174
1175 for (i = 0; adv7604_timings[i].bt.height; i++) {
1176 if (vtotal(&adv7604_timings[i].bt) != stdi->lcf + 1)
1177 continue;
1178 if (adv7604_timings[i].bt.vsync != stdi->lcvs)
1179 continue;
1180
1181 pix_clk = hfreq * htotal(&adv7604_timings[i].bt);
1182
1183 if ((pix_clk < adv7604_timings[i].bt.pixelclock + 1000000) &&
1184 (pix_clk > adv7604_timings[i].bt.pixelclock - 1000000)) {
1185 *timings = adv7604_timings[i];
1186 return 0;
1187 }
1188 }
1189
1190 if (v4l2_detect_cvt(stdi->lcf + 1, hfreq, stdi->lcvs,
1191 (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
1192 (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
1193 timings))
1194 return 0;
1195 if (v4l2_detect_gtf(stdi->lcf + 1, hfreq, stdi->lcvs,
1196 (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
1197 (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
1198 state->aspect_ratio, timings))
1199 return 0;
1200
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001201 v4l2_dbg(2, debug, sd,
1202 "%s: No format candidate found for lcvs = %d, lcf=%d, bl = %d, %chsync, %cvsync\n",
1203 __func__, stdi->lcvs, stdi->lcf, stdi->bl,
1204 stdi->hs_pol, stdi->vs_pol);
Hans Verkuil54450f52012-07-18 05:45:16 -03001205 return -1;
1206}
1207
1208static int read_stdi(struct v4l2_subdev *sd, struct stdi_readback *stdi)
1209{
Laurent Pinchart4a2ccdd2014-01-08 20:26:55 -03001210 u8 polarity;
1211
Hans Verkuil54450f52012-07-18 05:45:16 -03001212 if (no_lock_stdi(sd) || no_lock_sspd(sd)) {
1213 v4l2_dbg(2, debug, sd, "%s: STDI and/or SSPD not locked\n", __func__);
1214 return -1;
1215 }
1216
1217 /* read STDI */
Laurent Pinchart51182a92014-01-08 19:30:37 -03001218 stdi->bl = cp_read16(sd, 0xb1, 0x3fff);
1219 stdi->lcf = cp_read16(sd, 0xb3, 0x7ff);
Hans Verkuil54450f52012-07-18 05:45:16 -03001220 stdi->lcvs = cp_read(sd, 0xb3) >> 3;
1221 stdi->interlaced = io_read(sd, 0x12) & 0x10;
1222
1223 /* read SSPD */
Laurent Pinchart4a2ccdd2014-01-08 20:26:55 -03001224 polarity = cp_read(sd, 0xb5);
1225 if ((polarity & 0x03) == 0x01) {
1226 stdi->hs_pol = polarity & 0x10
1227 ? (polarity & 0x08 ? '+' : '-') : 'x';
1228 stdi->vs_pol = polarity & 0x40
1229 ? (polarity & 0x20 ? '+' : '-') : 'x';
Hans Verkuil54450f52012-07-18 05:45:16 -03001230 } else {
1231 stdi->hs_pol = 'x';
1232 stdi->vs_pol = 'x';
1233 }
1234
1235 if (no_lock_stdi(sd) || no_lock_sspd(sd)) {
1236 v4l2_dbg(2, debug, sd,
1237 "%s: signal lost during readout of STDI/SSPD\n", __func__);
1238 return -1;
1239 }
1240
1241 if (stdi->lcf < 239 || stdi->bl < 8 || stdi->bl == 0x3fff) {
1242 v4l2_dbg(2, debug, sd, "%s: invalid signal\n", __func__);
1243 memset(stdi, 0, sizeof(struct stdi_readback));
1244 return -1;
1245 }
1246
1247 v4l2_dbg(2, debug, sd,
1248 "%s: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, %chsync, %cvsync, %s\n",
1249 __func__, stdi->lcf, stdi->bl, stdi->lcvs,
1250 stdi->hs_pol, stdi->vs_pol,
1251 stdi->interlaced ? "interlaced" : "progressive");
1252
1253 return 0;
1254}
1255
1256static int adv7604_enum_dv_timings(struct v4l2_subdev *sd,
1257 struct v4l2_enum_dv_timings *timings)
1258{
1259 if (timings->index >= ARRAY_SIZE(adv7604_timings) - 1)
1260 return -EINVAL;
1261 memset(timings->reserved, 0, sizeof(timings->reserved));
1262 timings->timings = adv7604_timings[timings->index];
1263 return 0;
1264}
1265
1266static int adv7604_dv_timings_cap(struct v4l2_subdev *sd,
1267 struct v4l2_dv_timings_cap *cap)
1268{
Hans Verkuil54450f52012-07-18 05:45:16 -03001269 cap->type = V4L2_DV_BT_656_1120;
1270 cap->bt.max_width = 1920;
1271 cap->bt.max_height = 1200;
Hans Verkuilfe9c2562013-08-19 08:07:26 -03001272 cap->bt.min_pixelclock = 25000000;
Mats Randgaard4a31a932013-12-10 09:45:00 -03001273 if (is_digital_input(sd))
Hans Verkuil54450f52012-07-18 05:45:16 -03001274 cap->bt.max_pixelclock = 225000000;
1275 else
1276 cap->bt.max_pixelclock = 170000000;
1277 cap->bt.standards = V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
1278 V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT;
1279 cap->bt.capabilities = V4L2_DV_BT_CAP_PROGRESSIVE |
1280 V4L2_DV_BT_CAP_REDUCED_BLANKING | V4L2_DV_BT_CAP_CUSTOM;
1281 return 0;
1282}
1283
1284/* Fill the optional fields .standards and .flags in struct v4l2_dv_timings
1285 if the format is listed in adv7604_timings[] */
1286static void adv7604_fill_optional_dv_timings_fields(struct v4l2_subdev *sd,
1287 struct v4l2_dv_timings *timings)
1288{
Hans Verkuil54450f52012-07-18 05:45:16 -03001289 int i;
1290
1291 for (i = 0; adv7604_timings[i].bt.width; i++) {
Hans Verkuilef1ed8f2013-08-15 08:28:47 -03001292 if (v4l2_match_dv_timings(timings, &adv7604_timings[i],
Mats Randgaard4a31a932013-12-10 09:45:00 -03001293 is_digital_input(sd) ? 250000 : 1000000)) {
Hans Verkuil54450f52012-07-18 05:45:16 -03001294 *timings = adv7604_timings[i];
1295 break;
1296 }
1297 }
1298}
1299
1300static int adv7604_query_dv_timings(struct v4l2_subdev *sd,
1301 struct v4l2_dv_timings *timings)
1302{
1303 struct adv7604_state *state = to_state(sd);
1304 struct v4l2_bt_timings *bt = &timings->bt;
1305 struct stdi_readback stdi;
1306
1307 if (!timings)
1308 return -EINVAL;
1309
1310 memset(timings, 0, sizeof(struct v4l2_dv_timings));
1311
1312 if (no_signal(sd)) {
Martin Bugge1e0b9152013-12-05 10:34:46 -03001313 state->restart_stdi_once = true;
Hans Verkuil54450f52012-07-18 05:45:16 -03001314 v4l2_dbg(1, debug, sd, "%s: no valid signal\n", __func__);
1315 return -ENOLINK;
1316 }
1317
1318 /* read STDI */
1319 if (read_stdi(sd, &stdi)) {
1320 v4l2_dbg(1, debug, sd, "%s: STDI/SSPD not locked\n", __func__);
1321 return -ENOLINK;
1322 }
1323 bt->interlaced = stdi.interlaced ?
1324 V4L2_DV_INTERLACED : V4L2_DV_PROGRESSIVE;
1325
Mats Randgaard4a31a932013-12-10 09:45:00 -03001326 if (is_digital_input(sd)) {
Martin Buggebb88f322013-08-14 08:52:46 -03001327 uint32_t freq;
1328
Hans Verkuil54450f52012-07-18 05:45:16 -03001329 timings->type = V4L2_DV_BT_656_1120;
1330
Laurent Pinchart51182a92014-01-08 19:30:37 -03001331 bt->width = hdmi_read16(sd, 0x07, 0xfff);
1332 bt->height = hdmi_read16(sd, 0x09, 0xfff);
Martin Buggebb88f322013-08-14 08:52:46 -03001333 freq = (hdmi_read(sd, 0x06) * 1000000) +
Hans Verkuil54450f52012-07-18 05:45:16 -03001334 ((hdmi_read(sd, 0x3b) & 0x30) >> 4) * 250000;
Martin Buggebb88f322013-08-14 08:52:46 -03001335 if (is_hdmi(sd)) {
1336 /* adjust for deep color mode */
1337 unsigned bits_per_channel = ((hdmi_read(sd, 0x0b) & 0x60) >> 4) + 8;
1338
1339 freq = freq * 8 / bits_per_channel;
1340 }
1341 bt->pixelclock = freq;
Laurent Pinchart51182a92014-01-08 19:30:37 -03001342 bt->hfrontporch = hdmi_read16(sd, 0x20, 0x3ff);
1343 bt->hsync = hdmi_read16(sd, 0x22, 0x3ff);
1344 bt->hbackporch = hdmi_read16(sd, 0x24, 0x3ff);
1345 bt->vfrontporch = hdmi_read16(sd, 0x2a, 0x1fff) / 2;
1346 bt->vsync = hdmi_read16(sd, 0x2e, 0x1fff) / 2;
1347 bt->vbackporch = hdmi_read16(sd, 0x32, 0x1fff) / 2;
Hans Verkuil54450f52012-07-18 05:45:16 -03001348 bt->polarities = ((hdmi_read(sd, 0x05) & 0x10) ? V4L2_DV_VSYNC_POS_POL : 0) |
1349 ((hdmi_read(sd, 0x05) & 0x20) ? V4L2_DV_HSYNC_POS_POL : 0);
1350 if (bt->interlaced == V4L2_DV_INTERLACED) {
Laurent Pinchart51182a92014-01-08 19:30:37 -03001351 bt->height += hdmi_read16(sd, 0x0b, 0xfff);
1352 bt->il_vfrontporch = hdmi_read16(sd, 0x2c, 0x1fff) / 2;
1353 bt->il_vsync = hdmi_read16(sd, 0x30, 0x1fff) / 2;
1354 bt->vbackporch = hdmi_read16(sd, 0x34, 0x1fff) / 2;
Hans Verkuil54450f52012-07-18 05:45:16 -03001355 }
1356 adv7604_fill_optional_dv_timings_fields(sd, timings);
1357 } else {
1358 /* find format
Hans Verkuil80939642012-10-16 05:46:21 -03001359 * Since LCVS values are inaccurate [REF_03, p. 275-276],
Hans Verkuil54450f52012-07-18 05:45:16 -03001360 * stdi2dv_timings() is called with lcvs +-1 if the first attempt fails.
1361 */
1362 if (!stdi2dv_timings(sd, &stdi, timings))
1363 goto found;
1364 stdi.lcvs += 1;
1365 v4l2_dbg(1, debug, sd, "%s: lcvs + 1 = %d\n", __func__, stdi.lcvs);
1366 if (!stdi2dv_timings(sd, &stdi, timings))
1367 goto found;
1368 stdi.lcvs -= 2;
1369 v4l2_dbg(1, debug, sd, "%s: lcvs - 1 = %d\n", __func__, stdi.lcvs);
1370 if (stdi2dv_timings(sd, &stdi, timings)) {
Hans Verkuilcf9afb12012-10-16 10:12:55 -03001371 /*
1372 * The STDI block may measure wrong values, especially
1373 * for lcvs and lcf. If the driver can not find any
1374 * valid timing, the STDI block is restarted to measure
1375 * the video timings again. The function will return an
1376 * error, but the restart of STDI will generate a new
1377 * STDI interrupt and the format detection process will
1378 * restart.
1379 */
1380 if (state->restart_stdi_once) {
1381 v4l2_dbg(1, debug, sd, "%s: restart STDI\n", __func__);
1382 /* TODO restart STDI for Sync Channel 2 */
1383 /* enter one-shot mode */
1384 cp_write_and_or(sd, 0x86, 0xf9, 0x00);
1385 /* trigger STDI restart */
1386 cp_write_and_or(sd, 0x86, 0xf9, 0x04);
1387 /* reset to continuous mode */
1388 cp_write_and_or(sd, 0x86, 0xf9, 0x02);
1389 state->restart_stdi_once = false;
1390 return -ENOLINK;
1391 }
Hans Verkuil54450f52012-07-18 05:45:16 -03001392 v4l2_dbg(1, debug, sd, "%s: format not supported\n", __func__);
1393 return -ERANGE;
1394 }
Hans Verkuilcf9afb12012-10-16 10:12:55 -03001395 state->restart_stdi_once = true;
Hans Verkuil54450f52012-07-18 05:45:16 -03001396 }
1397found:
1398
1399 if (no_signal(sd)) {
1400 v4l2_dbg(1, debug, sd, "%s: signal lost during readout\n", __func__);
1401 memset(timings, 0, sizeof(struct v4l2_dv_timings));
1402 return -ENOLINK;
1403 }
1404
Mats Randgaard4a31a932013-12-10 09:45:00 -03001405 if ((is_analog_input(sd) && bt->pixelclock > 170000000) ||
1406 (is_digital_input(sd) && bt->pixelclock > 225000000)) {
Hans Verkuil54450f52012-07-18 05:45:16 -03001407 v4l2_dbg(1, debug, sd, "%s: pixelclock out of range %d\n",
1408 __func__, (u32)bt->pixelclock);
1409 return -ERANGE;
1410 }
1411
1412 if (debug > 1)
Hans Verkuil11d034c2013-08-15 08:05:59 -03001413 v4l2_print_dv_timings(sd->name, "adv7604_query_dv_timings: ",
1414 timings, true);
Hans Verkuil54450f52012-07-18 05:45:16 -03001415
1416 return 0;
1417}
1418
1419static int adv7604_s_dv_timings(struct v4l2_subdev *sd,
1420 struct v4l2_dv_timings *timings)
1421{
1422 struct adv7604_state *state = to_state(sd);
1423 struct v4l2_bt_timings *bt;
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001424 int err;
Hans Verkuil54450f52012-07-18 05:45:16 -03001425
1426 if (!timings)
1427 return -EINVAL;
1428
Mats Randgaardd48eb482013-12-12 10:13:35 -03001429 if (v4l2_match_dv_timings(&state->timings, timings, 0)) {
1430 v4l2_dbg(1, debug, sd, "%s: no change\n", __func__);
1431 return 0;
1432 }
1433
Hans Verkuil54450f52012-07-18 05:45:16 -03001434 bt = &timings->bt;
1435
Mats Randgaard4a31a932013-12-10 09:45:00 -03001436 if ((is_analog_input(sd) && bt->pixelclock > 170000000) ||
1437 (is_digital_input(sd) && bt->pixelclock > 225000000)) {
Hans Verkuil54450f52012-07-18 05:45:16 -03001438 v4l2_dbg(1, debug, sd, "%s: pixelclock out of range %d\n",
1439 __func__, (u32)bt->pixelclock);
1440 return -ERANGE;
1441 }
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001442
Hans Verkuil54450f52012-07-18 05:45:16 -03001443 adv7604_fill_optional_dv_timings_fields(sd, timings);
1444
1445 state->timings = *timings;
1446
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001447 cp_write(sd, 0x91, bt->interlaced ? 0x50 : 0x10);
1448
1449 /* Use prim_mode and vid_std when available */
1450 err = configure_predefined_video_timings(sd, timings);
1451 if (err) {
1452 /* custom settings when the video format
1453 does not have prim_mode/vid_std */
1454 configure_custom_video_timings(sd, bt);
1455 }
Hans Verkuil54450f52012-07-18 05:45:16 -03001456
1457 set_rgb_quantization_range(sd);
1458
Hans Verkuil54450f52012-07-18 05:45:16 -03001459 if (debug > 1)
Hans Verkuil11d034c2013-08-15 08:05:59 -03001460 v4l2_print_dv_timings(sd->name, "adv7604_s_dv_timings: ",
1461 timings, true);
Hans Verkuil54450f52012-07-18 05:45:16 -03001462 return 0;
1463}
1464
1465static int adv7604_g_dv_timings(struct v4l2_subdev *sd,
1466 struct v4l2_dv_timings *timings)
1467{
1468 struct adv7604_state *state = to_state(sd);
1469
1470 *timings = state->timings;
1471 return 0;
1472}
1473
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001474static void enable_input(struct v4l2_subdev *sd)
Hans Verkuil54450f52012-07-18 05:45:16 -03001475{
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001476 struct adv7604_state *state = to_state(sd);
1477
Mats Randgaard4a31a932013-12-10 09:45:00 -03001478 if (is_analog_input(sd)) {
Hans Verkuil54450f52012-07-18 05:45:16 -03001479 io_write(sd, 0x15, 0xb0); /* Disable Tristate of Pins (no audio) */
Mats Randgaard4a31a932013-12-10 09:45:00 -03001480 } else if (is_digital_input(sd)) {
Mats Randgaard4a31a932013-12-10 09:45:00 -03001481 hdmi_write_and_or(sd, 0x00, 0xfc, state->selected_input);
Hans Verkuil54450f52012-07-18 05:45:16 -03001482 hdmi_write(sd, 0x01, 0x00); /* Enable HDMI clock terminators */
1483 io_write(sd, 0x15, 0xa0); /* Disable Tristate of Pins */
Mats Randgaard5474b982013-12-05 10:33:41 -03001484 hdmi_write_and_or(sd, 0x1a, 0xef, 0x00); /* Unmute audio */
Mats Randgaard4a31a932013-12-10 09:45:00 -03001485 } else {
1486 v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
1487 __func__, state->selected_input);
Hans Verkuil54450f52012-07-18 05:45:16 -03001488 }
1489}
1490
1491static void disable_input(struct v4l2_subdev *sd)
1492{
Mats Randgaard5474b982013-12-05 10:33:41 -03001493 hdmi_write_and_or(sd, 0x1a, 0xef, 0x10); /* Mute audio */
1494 msleep(16); /* 512 samples with >= 32 kHz sample rate [REF_03, c. 7.16.10] */
Hans Verkuil54450f52012-07-18 05:45:16 -03001495 io_write(sd, 0x15, 0xbe); /* Tristate all outputs from video core */
Hans Verkuil54450f52012-07-18 05:45:16 -03001496 hdmi_write(sd, 0x01, 0x78); /* Disable HDMI clock terminators */
1497}
1498
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001499static void select_input(struct v4l2_subdev *sd)
Hans Verkuil54450f52012-07-18 05:45:16 -03001500{
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001501 struct adv7604_state *state = to_state(sd);
Hans Verkuil54450f52012-07-18 05:45:16 -03001502
Mats Randgaard4a31a932013-12-10 09:45:00 -03001503 if (is_analog_input(sd)) {
Hans Verkuil54450f52012-07-18 05:45:16 -03001504 /* reset ADI recommended settings for HDMI: */
1505 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 4. */
1506 hdmi_write(sd, 0x0d, 0x04); /* HDMI filter optimization */
1507 hdmi_write(sd, 0x3d, 0x00); /* DDC bus active pull-up control */
1508 hdmi_write(sd, 0x3e, 0x74); /* TMDS PLL optimization */
1509 hdmi_write(sd, 0x4e, 0x3b); /* TMDS PLL optimization */
1510 hdmi_write(sd, 0x57, 0x74); /* TMDS PLL optimization */
1511 hdmi_write(sd, 0x58, 0x63); /* TMDS PLL optimization */
1512 hdmi_write(sd, 0x8d, 0x18); /* equaliser */
1513 hdmi_write(sd, 0x8e, 0x34); /* equaliser */
1514 hdmi_write(sd, 0x93, 0x88); /* equaliser */
1515 hdmi_write(sd, 0x94, 0x2e); /* equaliser */
1516 hdmi_write(sd, 0x96, 0x00); /* enable automatic EQ changing */
1517
1518 afe_write(sd, 0x00, 0x08); /* power up ADC */
1519 afe_write(sd, 0x01, 0x06); /* power up Analog Front End */
1520 afe_write(sd, 0xc8, 0x00); /* phase control */
1521
1522 /* set ADI recommended settings for digitizer */
1523 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 17. */
1524 afe_write(sd, 0x12, 0x7b); /* ADC noise shaping filter controls */
1525 afe_write(sd, 0x0c, 0x1f); /* CP core gain controls */
1526 cp_write(sd, 0x3e, 0x04); /* CP core pre-gain control */
1527 cp_write(sd, 0xc3, 0x39); /* CP coast control. Graphics mode */
1528 cp_write(sd, 0x40, 0x5c); /* CP core pre-gain control. Graphics mode */
Mats Randgaard4a31a932013-12-10 09:45:00 -03001529 } else if (is_digital_input(sd)) {
1530 hdmi_write(sd, 0x00, state->selected_input & 0x03);
Hans Verkuil54450f52012-07-18 05:45:16 -03001531
Hans Verkuil54450f52012-07-18 05:45:16 -03001532 /* set ADI recommended settings for HDMI: */
1533 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 4. */
1534 hdmi_write(sd, 0x0d, 0x84); /* HDMI filter optimization */
1535 hdmi_write(sd, 0x3d, 0x10); /* DDC bus active pull-up control */
1536 hdmi_write(sd, 0x3e, 0x39); /* TMDS PLL optimization */
1537 hdmi_write(sd, 0x4e, 0x3b); /* TMDS PLL optimization */
1538 hdmi_write(sd, 0x57, 0xb6); /* TMDS PLL optimization */
1539 hdmi_write(sd, 0x58, 0x03); /* TMDS PLL optimization */
1540 hdmi_write(sd, 0x8d, 0x18); /* equaliser */
1541 hdmi_write(sd, 0x8e, 0x34); /* equaliser */
1542 hdmi_write(sd, 0x93, 0x8b); /* equaliser */
1543 hdmi_write(sd, 0x94, 0x2d); /* equaliser */
1544 hdmi_write(sd, 0x96, 0x01); /* enable automatic EQ changing */
1545
1546 afe_write(sd, 0x00, 0xff); /* power down ADC */
1547 afe_write(sd, 0x01, 0xfe); /* power down Analog Front End */
1548 afe_write(sd, 0xc8, 0x40); /* phase control */
1549
1550 /* reset ADI recommended settings for digitizer */
1551 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 17. */
1552 afe_write(sd, 0x12, 0xfb); /* ADC noise shaping filter controls */
1553 afe_write(sd, 0x0c, 0x0d); /* CP core gain controls */
1554 cp_write(sd, 0x3e, 0x00); /* CP core pre-gain control */
1555 cp_write(sd, 0xc3, 0x39); /* CP coast control. Graphics mode */
1556 cp_write(sd, 0x40, 0x80); /* CP core pre-gain control. Graphics mode */
Mats Randgaard4a31a932013-12-10 09:45:00 -03001557 } else {
1558 v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
1559 __func__, state->selected_input);
Hans Verkuil54450f52012-07-18 05:45:16 -03001560 }
1561}
1562
1563static int adv7604_s_routing(struct v4l2_subdev *sd,
1564 u32 input, u32 output, u32 config)
1565{
1566 struct adv7604_state *state = to_state(sd);
1567
Mats Randgaardff4f80f2013-12-05 10:24:05 -03001568 v4l2_dbg(2, debug, sd, "%s: input %d, selected input %d",
1569 __func__, input, state->selected_input);
1570
1571 if (input == state->selected_input)
1572 return 0;
Hans Verkuil54450f52012-07-18 05:45:16 -03001573
Mats Randgaard4a31a932013-12-10 09:45:00 -03001574 state->selected_input = input;
Hans Verkuil54450f52012-07-18 05:45:16 -03001575
1576 disable_input(sd);
1577
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001578 select_input(sd);
Hans Verkuil54450f52012-07-18 05:45:16 -03001579
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001580 enable_input(sd);
Hans Verkuil54450f52012-07-18 05:45:16 -03001581
1582 return 0;
1583}
1584
1585static int adv7604_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned int index,
1586 enum v4l2_mbus_pixelcode *code)
1587{
1588 if (index)
1589 return -EINVAL;
1590 /* Good enough for now */
1591 *code = V4L2_MBUS_FMT_FIXED;
1592 return 0;
1593}
1594
1595static int adv7604_g_mbus_fmt(struct v4l2_subdev *sd,
1596 struct v4l2_mbus_framefmt *fmt)
1597{
1598 struct adv7604_state *state = to_state(sd);
1599
1600 fmt->width = state->timings.bt.width;
1601 fmt->height = state->timings.bt.height;
1602 fmt->code = V4L2_MBUS_FMT_FIXED;
1603 fmt->field = V4L2_FIELD_NONE;
1604 if (state->timings.bt.standards & V4L2_DV_BT_STD_CEA861) {
1605 fmt->colorspace = (state->timings.bt.height <= 576) ?
1606 V4L2_COLORSPACE_SMPTE170M : V4L2_COLORSPACE_REC709;
1607 }
1608 return 0;
1609}
1610
1611static int adv7604_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
1612{
Mats Randgaardf24d2292013-12-10 10:15:13 -03001613 const u8 irq_reg_0x43 = io_read(sd, 0x43);
1614 const u8 irq_reg_0x6b = io_read(sd, 0x6b);
1615 const u8 irq_reg_0x70 = io_read(sd, 0x70);
1616 u8 fmt_change_digital;
1617 u8 fmt_change;
1618 u8 tx_5v;
1619
1620 if (irq_reg_0x43)
1621 io_write(sd, 0x44, irq_reg_0x43);
1622 if (irq_reg_0x70)
1623 io_write(sd, 0x71, irq_reg_0x70);
1624 if (irq_reg_0x6b)
1625 io_write(sd, 0x6c, irq_reg_0x6b);
Hans Verkuil54450f52012-07-18 05:45:16 -03001626
Mats Randgaardff4f80f2013-12-05 10:24:05 -03001627 v4l2_dbg(2, debug, sd, "%s: ", __func__);
1628
Hans Verkuil54450f52012-07-18 05:45:16 -03001629 /* format change */
Mats Randgaardf24d2292013-12-10 10:15:13 -03001630 fmt_change = irq_reg_0x43 & 0x98;
1631 fmt_change_digital = is_digital_input(sd) ? (irq_reg_0x6b & 0xc0) : 0;
Mats Randgaard14d03232013-12-05 10:26:11 -03001632
Hans Verkuil54450f52012-07-18 05:45:16 -03001633 if (fmt_change || fmt_change_digital) {
1634 v4l2_dbg(1, debug, sd,
Mats Randgaard25a64ac2013-08-14 07:58:45 -03001635 "%s: fmt_change = 0x%x, fmt_change_digital = 0x%x\n",
Hans Verkuil54450f52012-07-18 05:45:16 -03001636 __func__, fmt_change, fmt_change_digital);
Mats Randgaard25a64ac2013-08-14 07:58:45 -03001637
Mats Randgaard14d03232013-12-05 10:26:11 -03001638 v4l2_subdev_notify(sd, ADV7604_FMT_CHANGE, NULL);
Mats Randgaard25a64ac2013-08-14 07:58:45 -03001639
Hans Verkuil54450f52012-07-18 05:45:16 -03001640 if (handled)
1641 *handled = true;
1642 }
Mats Randgaardf24d2292013-12-10 10:15:13 -03001643 /* HDMI/DVI mode */
1644 if (irq_reg_0x6b & 0x01) {
1645 v4l2_dbg(1, debug, sd, "%s: irq %s mode\n", __func__,
1646 (io_read(sd, 0x6a) & 0x01) ? "HDMI" : "DVI");
1647 set_rgb_quantization_range(sd);
1648 if (handled)
1649 *handled = true;
1650 }
1651
Hans Verkuil54450f52012-07-18 05:45:16 -03001652 /* tx 5v detect */
Mats Randgaard4a31a932013-12-10 09:45:00 -03001653 tx_5v = io_read(sd, 0x70) & 0x1e;
Hans Verkuil54450f52012-07-18 05:45:16 -03001654 if (tx_5v) {
1655 v4l2_dbg(1, debug, sd, "%s: tx_5v: 0x%x\n", __func__, tx_5v);
1656 io_write(sd, 0x71, tx_5v);
1657 adv7604_s_detect_tx_5v_ctrl(sd);
1658 if (handled)
1659 *handled = true;
1660 }
1661 return 0;
1662}
1663
Hans Verkuilb09dfac2014-03-04 08:05:19 -03001664static int adv7604_get_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
Hans Verkuil54450f52012-07-18 05:45:16 -03001665{
1666 struct adv7604_state *state = to_state(sd);
Mats Randgaard4a31a932013-12-10 09:45:00 -03001667 u8 *data = NULL;
Hans Verkuil54450f52012-07-18 05:45:16 -03001668
Mats Randgaard4a31a932013-12-10 09:45:00 -03001669 if (edid->pad > ADV7604_EDID_PORT_D)
Hans Verkuil54450f52012-07-18 05:45:16 -03001670 return -EINVAL;
1671 if (edid->blocks == 0)
1672 return -EINVAL;
Mats Randgaard4a31a932013-12-10 09:45:00 -03001673 if (edid->blocks > 2)
Hans Verkuil54450f52012-07-18 05:45:16 -03001674 return -EINVAL;
Mats Randgaard4a31a932013-12-10 09:45:00 -03001675 if (edid->start_block > 1)
1676 return -EINVAL;
1677 if (edid->start_block == 1)
1678 edid->blocks = 1;
Mats Randgaard4a31a932013-12-10 09:45:00 -03001679
1680 if (edid->blocks > state->edid.blocks)
1681 edid->blocks = state->edid.blocks;
1682
1683 switch (edid->pad) {
1684 case ADV7604_EDID_PORT_A:
1685 case ADV7604_EDID_PORT_B:
1686 case ADV7604_EDID_PORT_C:
1687 case ADV7604_EDID_PORT_D:
1688 if (state->edid.present & (1 << edid->pad))
1689 data = state->edid.edid;
1690 break;
1691 default:
1692 return -EINVAL;
1693 break;
1694 }
1695 if (!data)
1696 return -ENODATA;
1697
1698 memcpy(edid->edid,
1699 data + edid->start_block * 128,
Hans Verkuil54450f52012-07-18 05:45:16 -03001700 edid->blocks * 128);
1701 return 0;
1702}
1703
Mats Randgaarddd08beb2013-12-10 09:57:09 -03001704static int get_edid_spa_location(const u8 *edid)
Mats Randgaard3e86aa82013-12-10 09:55:18 -03001705{
1706 u8 d;
1707
1708 if ((edid[0x7e] != 1) ||
1709 (edid[0x80] != 0x02) ||
1710 (edid[0x81] != 0x03)) {
1711 return -1;
1712 }
1713
1714 /* search Vendor Specific Data Block (tag 3) */
1715 d = edid[0x82] & 0x7f;
1716 if (d > 4) {
1717 int i = 0x84;
1718 int end = 0x80 + d;
1719
1720 do {
1721 u8 tag = edid[i] >> 5;
1722 u8 len = edid[i] & 0x1f;
1723
1724 if ((tag == 3) && (len >= 5))
1725 return i + 4;
1726 i += len + 1;
1727 } while (i < end);
1728 }
1729 return -1;
1730}
1731
Hans Verkuilb09dfac2014-03-04 08:05:19 -03001732static int adv7604_set_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
Hans Verkuil54450f52012-07-18 05:45:16 -03001733{
1734 struct adv7604_state *state = to_state(sd);
Mats Randgaarddd08beb2013-12-10 09:57:09 -03001735 int spa_loc;
Mats Randgaard3e86aa82013-12-10 09:55:18 -03001736 int tmp = 0;
Hans Verkuil54450f52012-07-18 05:45:16 -03001737 int err;
Mats Randgaarddd08beb2013-12-10 09:57:09 -03001738 int i;
Hans Verkuil54450f52012-07-18 05:45:16 -03001739
Mats Randgaard4a31a932013-12-10 09:45:00 -03001740 if (edid->pad > ADV7604_EDID_PORT_D)
Hans Verkuil54450f52012-07-18 05:45:16 -03001741 return -EINVAL;
1742 if (edid->start_block != 0)
1743 return -EINVAL;
1744 if (edid->blocks == 0) {
Mats Randgaard3e86aa82013-12-10 09:55:18 -03001745 /* Disable hotplug and I2C access to EDID RAM from DDC port */
Mats Randgaard4a31a932013-12-10 09:45:00 -03001746 state->edid.present &= ~(1 << edid->pad);
1747 v4l2_subdev_notify(sd, ADV7604_HOTPLUG, (void *)&state->edid.present);
Mats Randgaard3e86aa82013-12-10 09:55:18 -03001748 rep_write_and_or(sd, 0x77, 0xf0, state->edid.present);
1749
Hans Verkuil54450f52012-07-18 05:45:16 -03001750 /* Fall back to a 16:9 aspect ratio */
1751 state->aspect_ratio.numerator = 16;
1752 state->aspect_ratio.denominator = 9;
Mats Randgaard3e86aa82013-12-10 09:55:18 -03001753
1754 if (!state->edid.present)
1755 state->edid.blocks = 0;
1756
1757 v4l2_dbg(2, debug, sd, "%s: clear EDID pad %d, edid.present = 0x%x\n",
1758 __func__, edid->pad, state->edid.present);
Hans Verkuil54450f52012-07-18 05:45:16 -03001759 return 0;
1760 }
Mats Randgaard4a31a932013-12-10 09:45:00 -03001761 if (edid->blocks > 2) {
1762 edid->blocks = 2;
Hans Verkuil54450f52012-07-18 05:45:16 -03001763 return -E2BIG;
Mats Randgaard4a31a932013-12-10 09:45:00 -03001764 }
Mats Randgaard4a31a932013-12-10 09:45:00 -03001765
Mats Randgaarddd08beb2013-12-10 09:57:09 -03001766 v4l2_dbg(2, debug, sd, "%s: write EDID pad %d, edid.present = 0x%x\n",
1767 __func__, edid->pad, state->edid.present);
1768
Mats Randgaard3e86aa82013-12-10 09:55:18 -03001769 /* Disable hotplug and I2C access to EDID RAM from DDC port */
Mats Randgaard4a31a932013-12-10 09:45:00 -03001770 cancel_delayed_work_sync(&state->delayed_work_enable_hotplug);
Mats Randgaard3e86aa82013-12-10 09:55:18 -03001771 v4l2_subdev_notify(sd, ADV7604_HOTPLUG, (void *)&tmp);
1772 rep_write_and_or(sd, 0x77, 0xf0, 0x00);
1773
Mats Randgaarddd08beb2013-12-10 09:57:09 -03001774 spa_loc = get_edid_spa_location(edid->edid);
1775 if (spa_loc < 0)
1776 spa_loc = 0xc0; /* Default value [REF_02, p. 116] */
1777
Mats Randgaard3e86aa82013-12-10 09:55:18 -03001778 switch (edid->pad) {
1779 case ADV7604_EDID_PORT_A:
Mats Randgaarddd08beb2013-12-10 09:57:09 -03001780 state->spa_port_a[0] = edid->edid[spa_loc];
1781 state->spa_port_a[1] = edid->edid[spa_loc + 1];
Mats Randgaard3e86aa82013-12-10 09:55:18 -03001782 break;
1783 case ADV7604_EDID_PORT_B:
Mats Randgaarddd08beb2013-12-10 09:57:09 -03001784 rep_write(sd, 0x70, edid->edid[spa_loc]);
1785 rep_write(sd, 0x71, edid->edid[spa_loc + 1]);
Mats Randgaard3e86aa82013-12-10 09:55:18 -03001786 break;
1787 case ADV7604_EDID_PORT_C:
Mats Randgaarddd08beb2013-12-10 09:57:09 -03001788 rep_write(sd, 0x72, edid->edid[spa_loc]);
1789 rep_write(sd, 0x73, edid->edid[spa_loc + 1]);
Mats Randgaard3e86aa82013-12-10 09:55:18 -03001790 break;
1791 case ADV7604_EDID_PORT_D:
Mats Randgaarddd08beb2013-12-10 09:57:09 -03001792 rep_write(sd, 0x74, edid->edid[spa_loc]);
1793 rep_write(sd, 0x75, edid->edid[spa_loc + 1]);
Mats Randgaard3e86aa82013-12-10 09:55:18 -03001794 break;
Mats Randgaarddd08beb2013-12-10 09:57:09 -03001795 default:
1796 return -EINVAL;
Mats Randgaard3e86aa82013-12-10 09:55:18 -03001797 }
Mats Randgaarddd08beb2013-12-10 09:57:09 -03001798 rep_write(sd, 0x76, spa_loc & 0xff);
1799 rep_write_and_or(sd, 0x77, 0xbf, (spa_loc >> 2) & 0x40);
Mats Randgaard3e86aa82013-12-10 09:55:18 -03001800
Mats Randgaarddd08beb2013-12-10 09:57:09 -03001801 edid->edid[spa_loc] = state->spa_port_a[0];
1802 edid->edid[spa_loc + 1] = state->spa_port_a[1];
Mats Randgaard4a31a932013-12-10 09:45:00 -03001803
1804 memcpy(state->edid.edid, edid->edid, 128 * edid->blocks);
1805 state->edid.blocks = edid->blocks;
Hans Verkuil54450f52012-07-18 05:45:16 -03001806 state->aspect_ratio = v4l2_calc_aspect_ratio(edid->edid[0x15],
1807 edid->edid[0x16]);
Mats Randgaard3e86aa82013-12-10 09:55:18 -03001808 state->edid.present |= 1 << edid->pad;
Mats Randgaard4a31a932013-12-10 09:45:00 -03001809
1810 err = edid_write_block(sd, 128 * edid->blocks, state->edid.edid);
1811 if (err < 0) {
Mats Randgaard3e86aa82013-12-10 09:55:18 -03001812 v4l2_err(sd, "error %d writing edid pad %d\n", err, edid->pad);
Mats Randgaard4a31a932013-12-10 09:45:00 -03001813 return err;
1814 }
1815
Mats Randgaarddd08beb2013-12-10 09:57:09 -03001816 /* adv7604 calculates the checksums and enables I2C access to internal
1817 EDID RAM from DDC port. */
1818 rep_write_and_or(sd, 0x77, 0xf0, state->edid.present);
1819
1820 for (i = 0; i < 1000; i++) {
1821 if (rep_read(sd, 0x7d) & state->edid.present)
1822 break;
1823 mdelay(1);
1824 }
1825 if (i == 1000) {
1826 v4l2_err(sd, "error enabling edid (0x%x)\n", state->edid.present);
1827 return -EIO;
1828 }
1829
1830
Mats Randgaard4a31a932013-12-10 09:45:00 -03001831 /* enable hotplug after 100 ms */
1832 queue_delayed_work(state->work_queues,
1833 &state->delayed_work_enable_hotplug, HZ / 10);
1834 return 0;
Hans Verkuil54450f52012-07-18 05:45:16 -03001835}
1836
1837/*********** avi info frame CEA-861-E **************/
1838
1839static void print_avi_infoframe(struct v4l2_subdev *sd)
1840{
1841 int i;
1842 u8 buf[14];
1843 u8 avi_len;
1844 u8 avi_ver;
1845
Martin Buggebb88f322013-08-14 08:52:46 -03001846 if (!is_hdmi(sd)) {
Hans Verkuil54450f52012-07-18 05:45:16 -03001847 v4l2_info(sd, "receive DVI-D signal (AVI infoframe not supported)\n");
1848 return;
1849 }
1850 if (!(io_read(sd, 0x60) & 0x01)) {
1851 v4l2_info(sd, "AVI infoframe not received\n");
1852 return;
1853 }
1854
1855 if (io_read(sd, 0x83) & 0x01) {
1856 v4l2_info(sd, "AVI infoframe checksum error has occurred earlier\n");
1857 io_write(sd, 0x85, 0x01); /* clear AVI_INF_CKS_ERR_RAW */
1858 if (io_read(sd, 0x83) & 0x01) {
1859 v4l2_info(sd, "AVI infoframe checksum error still present\n");
1860 io_write(sd, 0x85, 0x01); /* clear AVI_INF_CKS_ERR_RAW */
1861 }
1862 }
1863
1864 avi_len = infoframe_read(sd, 0xe2);
1865 avi_ver = infoframe_read(sd, 0xe1);
1866 v4l2_info(sd, "AVI infoframe version %d (%d byte)\n",
1867 avi_ver, avi_len);
1868
1869 if (avi_ver != 0x02)
1870 return;
1871
1872 for (i = 0; i < 14; i++)
1873 buf[i] = infoframe_read(sd, i);
1874
1875 v4l2_info(sd,
1876 "\t%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
1877 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7],
1878 buf[8], buf[9], buf[10], buf[11], buf[12], buf[13]);
1879}
1880
1881static int adv7604_log_status(struct v4l2_subdev *sd)
1882{
1883 struct adv7604_state *state = to_state(sd);
1884 struct v4l2_dv_timings timings;
1885 struct stdi_readback stdi;
1886 u8 reg_io_0x02 = io_read(sd, 0x02);
Laurent Pinchart4a2ccdd2014-01-08 20:26:55 -03001887 u8 edid_enabled;
1888 u8 cable_det;
Hans Verkuil54450f52012-07-18 05:45:16 -03001889
Lars-Peter Clausenf216ccb2013-11-25 16:15:29 -03001890 static const char * const csc_coeff_sel_rb[16] = {
Hans Verkuil54450f52012-07-18 05:45:16 -03001891 "bypassed", "YPbPr601 -> RGB", "reserved", "YPbPr709 -> RGB",
1892 "reserved", "RGB -> YPbPr601", "reserved", "RGB -> YPbPr709",
1893 "reserved", "YPbPr709 -> YPbPr601", "YPbPr601 -> YPbPr709",
1894 "reserved", "reserved", "reserved", "reserved", "manual"
1895 };
Lars-Peter Clausenf216ccb2013-11-25 16:15:29 -03001896 static const char * const input_color_space_txt[16] = {
Hans Verkuil54450f52012-07-18 05:45:16 -03001897 "RGB limited range (16-235)", "RGB full range (0-255)",
1898 "YCbCr Bt.601 (16-235)", "YCbCr Bt.709 (16-235)",
Mats Randgaard98332392013-12-05 10:05:58 -03001899 "xvYCC Bt.601", "xvYCC Bt.709",
Hans Verkuil54450f52012-07-18 05:45:16 -03001900 "YCbCr Bt.601 (0-255)", "YCbCr Bt.709 (0-255)",
1901 "invalid", "invalid", "invalid", "invalid", "invalid",
1902 "invalid", "invalid", "automatic"
1903 };
Lars-Peter Clausenf216ccb2013-11-25 16:15:29 -03001904 static const char * const rgb_quantization_range_txt[] = {
Hans Verkuil54450f52012-07-18 05:45:16 -03001905 "Automatic",
1906 "RGB limited range (16-235)",
1907 "RGB full range (0-255)",
1908 };
Lars-Peter Clausenf216ccb2013-11-25 16:15:29 -03001909 static const char * const deep_color_mode_txt[4] = {
Martin Buggebb88f322013-08-14 08:52:46 -03001910 "8-bits per channel",
1911 "10-bits per channel",
1912 "12-bits per channel",
1913 "16-bits per channel (not supported)"
1914 };
Hans Verkuil54450f52012-07-18 05:45:16 -03001915
1916 v4l2_info(sd, "-----Chip status-----\n");
1917 v4l2_info(sd, "Chip power: %s\n", no_power(sd) ? "off" : "on");
Laurent Pinchart4a2ccdd2014-01-08 20:26:55 -03001918 edid_enabled = rep_read(sd, 0x7d);
Mats Randgaard4a31a932013-12-10 09:45:00 -03001919 v4l2_info(sd, "EDID enabled port A: %s, B: %s, C: %s, D: %s\n",
Laurent Pinchart4a2ccdd2014-01-08 20:26:55 -03001920 ((edid_enabled & 0x01) ? "Yes" : "No"),
1921 ((edid_enabled & 0x02) ? "Yes" : "No"),
1922 ((edid_enabled & 0x04) ? "Yes" : "No"),
1923 ((edid_enabled & 0x08) ? "Yes" : "No"));
Hans Verkuil54450f52012-07-18 05:45:16 -03001924 v4l2_info(sd, "CEC: %s\n", !!(cec_read(sd, 0x2a) & 0x01) ?
1925 "enabled" : "disabled");
1926
1927 v4l2_info(sd, "-----Signal status-----\n");
Laurent Pinchart4a2ccdd2014-01-08 20:26:55 -03001928 cable_det = io_read(sd, 0x6f);
Mats Randgaard4a31a932013-12-10 09:45:00 -03001929 v4l2_info(sd, "Cable detected (+5V power) port A: %s, B: %s, C: %s, D: %s\n",
Laurent Pinchart4a2ccdd2014-01-08 20:26:55 -03001930 ((cable_det & 0x10) ? "Yes" : "No"),
1931 ((cable_det & 0x08) ? "Yes" : "No"),
1932 ((cable_det & 0x04) ? "Yes" : "No"),
1933 ((cable_det & 0x02) ? "Yes" : "No"));
Hans Verkuil54450f52012-07-18 05:45:16 -03001934 v4l2_info(sd, "TMDS signal detected: %s\n",
1935 no_signal_tmds(sd) ? "false" : "true");
1936 v4l2_info(sd, "TMDS signal locked: %s\n",
1937 no_lock_tmds(sd) ? "false" : "true");
1938 v4l2_info(sd, "SSPD locked: %s\n", no_lock_sspd(sd) ? "false" : "true");
1939 v4l2_info(sd, "STDI locked: %s\n", no_lock_stdi(sd) ? "false" : "true");
1940 v4l2_info(sd, "CP locked: %s\n", no_lock_cp(sd) ? "false" : "true");
1941 v4l2_info(sd, "CP free run: %s\n",
1942 (!!(cp_read(sd, 0xff) & 0x10) ? "on" : "off"));
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001943 v4l2_info(sd, "Prim-mode = 0x%x, video std = 0x%x, v_freq = 0x%x\n",
1944 io_read(sd, 0x01) & 0x0f, io_read(sd, 0x00) & 0x3f,
1945 (io_read(sd, 0x01) & 0x70) >> 4);
Hans Verkuil54450f52012-07-18 05:45:16 -03001946
1947 v4l2_info(sd, "-----Video Timings-----\n");
1948 if (read_stdi(sd, &stdi))
1949 v4l2_info(sd, "STDI: not locked\n");
1950 else
1951 v4l2_info(sd, "STDI: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, %s, %chsync, %cvsync\n",
1952 stdi.lcf, stdi.bl, stdi.lcvs,
1953 stdi.interlaced ? "interlaced" : "progressive",
1954 stdi.hs_pol, stdi.vs_pol);
1955 if (adv7604_query_dv_timings(sd, &timings))
1956 v4l2_info(sd, "No video detected\n");
1957 else
Hans Verkuil11d034c2013-08-15 08:05:59 -03001958 v4l2_print_dv_timings(sd->name, "Detected format: ",
1959 &timings, true);
1960 v4l2_print_dv_timings(sd->name, "Configured format: ",
1961 &state->timings, true);
Hans Verkuil54450f52012-07-18 05:45:16 -03001962
Mats Randgaard76eb2d32013-08-14 08:56:57 -03001963 if (no_signal(sd))
1964 return 0;
1965
Hans Verkuil54450f52012-07-18 05:45:16 -03001966 v4l2_info(sd, "-----Color space-----\n");
1967 v4l2_info(sd, "RGB quantization range ctrl: %s\n",
1968 rgb_quantization_range_txt[state->rgb_quantization_range]);
1969 v4l2_info(sd, "Input color space: %s\n",
1970 input_color_space_txt[reg_io_0x02 >> 4]);
1971 v4l2_info(sd, "Output color space: %s %s, saturator %s\n",
1972 (reg_io_0x02 & 0x02) ? "RGB" : "YCbCr",
1973 (reg_io_0x02 & 0x04) ? "(16-235)" : "(0-255)",
1974 ((reg_io_0x02 & 0x04) ^ (reg_io_0x02 & 0x01)) ?
Mats Randgaard76eb2d32013-08-14 08:56:57 -03001975 "enabled" : "disabled");
Hans Verkuil54450f52012-07-18 05:45:16 -03001976 v4l2_info(sd, "Color space conversion: %s\n",
1977 csc_coeff_sel_rb[cp_read(sd, 0xfc) >> 4]);
1978
Mats Randgaard4a31a932013-12-10 09:45:00 -03001979 if (!is_digital_input(sd))
Mats Randgaard76eb2d32013-08-14 08:56:57 -03001980 return 0;
1981
1982 v4l2_info(sd, "-----%s status-----\n", is_hdmi(sd) ? "HDMI" : "DVI-D");
Mats Randgaard4a31a932013-12-10 09:45:00 -03001983 v4l2_info(sd, "Digital video port selected: %c\n",
1984 (hdmi_read(sd, 0x00) & 0x03) + 'A');
1985 v4l2_info(sd, "HDCP encrypted content: %s\n",
1986 (hdmi_read(sd, 0x05) & 0x40) ? "true" : "false");
Mats Randgaard76eb2d32013-08-14 08:56:57 -03001987 v4l2_info(sd, "HDCP keys read: %s%s\n",
1988 (hdmi_read(sd, 0x04) & 0x20) ? "yes" : "no",
1989 (hdmi_read(sd, 0x04) & 0x10) ? "ERROR" : "");
1990 if (!is_hdmi(sd)) {
1991 bool audio_pll_locked = hdmi_read(sd, 0x04) & 0x01;
1992 bool audio_sample_packet_detect = hdmi_read(sd, 0x18) & 0x01;
1993 bool audio_mute = io_read(sd, 0x65) & 0x40;
1994
1995 v4l2_info(sd, "Audio: pll %s, samples %s, %s\n",
1996 audio_pll_locked ? "locked" : "not locked",
1997 audio_sample_packet_detect ? "detected" : "not detected",
1998 audio_mute ? "muted" : "enabled");
1999 if (audio_pll_locked && audio_sample_packet_detect) {
2000 v4l2_info(sd, "Audio format: %s\n",
2001 (hdmi_read(sd, 0x07) & 0x20) ? "multi-channel" : "stereo");
2002 }
2003 v4l2_info(sd, "Audio CTS: %u\n", (hdmi_read(sd, 0x5b) << 12) +
2004 (hdmi_read(sd, 0x5c) << 8) +
2005 (hdmi_read(sd, 0x5d) & 0xf0));
2006 v4l2_info(sd, "Audio N: %u\n", ((hdmi_read(sd, 0x5d) & 0x0f) << 16) +
2007 (hdmi_read(sd, 0x5e) << 8) +
2008 hdmi_read(sd, 0x5f));
2009 v4l2_info(sd, "AV Mute: %s\n", (hdmi_read(sd, 0x04) & 0x40) ? "on" : "off");
2010
2011 v4l2_info(sd, "Deep color mode: %s\n", deep_color_mode_txt[(hdmi_read(sd, 0x0b) & 0x60) >> 5]);
2012
Hans Verkuil54450f52012-07-18 05:45:16 -03002013 print_avi_infoframe(sd);
2014 }
2015
2016 return 0;
2017}
2018
2019/* ----------------------------------------------------------------------- */
2020
2021static const struct v4l2_ctrl_ops adv7604_ctrl_ops = {
2022 .s_ctrl = adv7604_s_ctrl,
2023};
2024
2025static const struct v4l2_subdev_core_ops adv7604_core_ops = {
2026 .log_status = adv7604_log_status,
2027 .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
2028 .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
2029 .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
2030 .g_ctrl = v4l2_subdev_g_ctrl,
2031 .s_ctrl = v4l2_subdev_s_ctrl,
2032 .queryctrl = v4l2_subdev_queryctrl,
2033 .querymenu = v4l2_subdev_querymenu,
Hans Verkuil54450f52012-07-18 05:45:16 -03002034 .interrupt_service_routine = adv7604_isr,
2035#ifdef CONFIG_VIDEO_ADV_DEBUG
2036 .g_register = adv7604_g_register,
2037 .s_register = adv7604_s_register,
2038#endif
2039};
2040
2041static const struct v4l2_subdev_video_ops adv7604_video_ops = {
2042 .s_routing = adv7604_s_routing,
2043 .g_input_status = adv7604_g_input_status,
2044 .s_dv_timings = adv7604_s_dv_timings,
2045 .g_dv_timings = adv7604_g_dv_timings,
2046 .query_dv_timings = adv7604_query_dv_timings,
2047 .enum_dv_timings = adv7604_enum_dv_timings,
2048 .dv_timings_cap = adv7604_dv_timings_cap,
2049 .enum_mbus_fmt = adv7604_enum_mbus_fmt,
2050 .g_mbus_fmt = adv7604_g_mbus_fmt,
2051 .try_mbus_fmt = adv7604_g_mbus_fmt,
2052 .s_mbus_fmt = adv7604_g_mbus_fmt,
2053};
2054
2055static const struct v4l2_subdev_pad_ops adv7604_pad_ops = {
2056 .get_edid = adv7604_get_edid,
2057 .set_edid = adv7604_set_edid,
2058};
2059
2060static const struct v4l2_subdev_ops adv7604_ops = {
2061 .core = &adv7604_core_ops,
2062 .video = &adv7604_video_ops,
2063 .pad = &adv7604_pad_ops,
2064};
2065
2066/* -------------------------- custom ctrls ---------------------------------- */
2067
2068static const struct v4l2_ctrl_config adv7604_ctrl_analog_sampling_phase = {
2069 .ops = &adv7604_ctrl_ops,
2070 .id = V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE,
2071 .name = "Analog Sampling Phase",
2072 .type = V4L2_CTRL_TYPE_INTEGER,
2073 .min = 0,
2074 .max = 0x1f,
2075 .step = 1,
2076 .def = 0,
2077};
2078
2079static const struct v4l2_ctrl_config adv7604_ctrl_free_run_color_manual = {
2080 .ops = &adv7604_ctrl_ops,
2081 .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL,
2082 .name = "Free Running Color, Manual",
2083 .type = V4L2_CTRL_TYPE_BOOLEAN,
2084 .min = false,
2085 .max = true,
2086 .step = 1,
2087 .def = false,
2088};
2089
2090static const struct v4l2_ctrl_config adv7604_ctrl_free_run_color = {
2091 .ops = &adv7604_ctrl_ops,
2092 .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR,
2093 .name = "Free Running Color",
2094 .type = V4L2_CTRL_TYPE_INTEGER,
2095 .min = 0x0,
2096 .max = 0xffffff,
2097 .step = 0x1,
2098 .def = 0x0,
2099};
2100
2101/* ----------------------------------------------------------------------- */
2102
2103static int adv7604_core_init(struct v4l2_subdev *sd)
2104{
2105 struct adv7604_state *state = to_state(sd);
2106 struct adv7604_platform_data *pdata = &state->pdata;
2107
2108 hdmi_write(sd, 0x48,
2109 (pdata->disable_pwrdnb ? 0x80 : 0) |
2110 (pdata->disable_cable_det_rst ? 0x40 : 0));
2111
2112 disable_input(sd);
2113
2114 /* power */
2115 io_write(sd, 0x0c, 0x42); /* Power up part and power down VDP */
2116 io_write(sd, 0x0b, 0x44); /* Power down ESDP block */
2117 cp_write(sd, 0xcf, 0x01); /* Power down macrovision */
2118
2119 /* video format */
2120 io_write_and_or(sd, 0x02, 0xf0,
2121 pdata->alt_gamma << 3 |
2122 pdata->op_656_range << 2 |
2123 pdata->rgb_out << 1 |
2124 pdata->alt_data_sat << 0);
2125 io_write(sd, 0x03, pdata->op_format_sel);
2126 io_write_and_or(sd, 0x04, 0x1f, pdata->op_ch_sel << 5);
2127 io_write_and_or(sd, 0x05, 0xf0, pdata->blank_data << 3 |
2128 pdata->insert_av_codes << 2 |
2129 pdata->replicate_av_codes << 1 |
2130 pdata->invert_cbcr << 0);
2131
Hans Verkuil54450f52012-07-18 05:45:16 -03002132 cp_write(sd, 0x69, 0x30); /* Enable CP CSC */
Martin Bugge98908692013-12-20 05:14:57 -03002133
2134 /* VS, HS polarities */
2135 io_write(sd, 0x06, 0xa0 | pdata->inv_vs_pol << 2 | pdata->inv_hs_pol << 1);
Mikhail Khelikf31b62e2013-12-20 05:12:00 -03002136
2137 /* Adjust drive strength */
2138 io_write(sd, 0x14, 0x40 | pdata->dr_str_data << 4 |
2139 pdata->dr_str_clk << 2 |
2140 pdata->dr_str_sync);
2141
Hans Verkuil54450f52012-07-18 05:45:16 -03002142 cp_write(sd, 0xba, (pdata->hdmi_free_run_mode << 1) | 0x01); /* HDMI free run */
2143 cp_write(sd, 0xf3, 0xdc); /* Low threshold to enter/exit free run mode */
2144 cp_write(sd, 0xf9, 0x23); /* STDI ch. 1 - LCVS change threshold -
Hans Verkuil80939642012-10-16 05:46:21 -03002145 ADI recommended setting [REF_01, c. 2.3.3] */
Hans Verkuil54450f52012-07-18 05:45:16 -03002146 cp_write(sd, 0x45, 0x23); /* STDI ch. 2 - LCVS change threshold -
Hans Verkuil80939642012-10-16 05:46:21 -03002147 ADI recommended setting [REF_01, c. 2.3.3] */
Hans Verkuil54450f52012-07-18 05:45:16 -03002148 cp_write(sd, 0xc9, 0x2d); /* use prim_mode and vid_std as free run resolution
2149 for digital formats */
2150
Mats Randgaard5474b982013-12-05 10:33:41 -03002151 /* HDMI audio */
2152 hdmi_write_and_or(sd, 0x15, 0xfc, 0x03); /* Mute on FIFO over-/underflow [REF_01, c. 1.2.18] */
2153 hdmi_write_and_or(sd, 0x1a, 0xf1, 0x08); /* Wait 1 s before unmute */
2154 hdmi_write_and_or(sd, 0x68, 0xf9, 0x06); /* FIFO reset on over-/underflow [REF_01, c. 1.2.19] */
2155
Hans Verkuil54450f52012-07-18 05:45:16 -03002156 /* TODO from platform data */
2157 afe_write(sd, 0xb5, 0x01); /* Setting MCLK to 256Fs */
2158
2159 afe_write(sd, 0x02, pdata->ain_sel); /* Select analog input muxing mode */
2160 io_write_and_or(sd, 0x30, ~(1 << 4), pdata->output_bus_lsb_to_msb << 4);
2161
Hans Verkuil54450f52012-07-18 05:45:16 -03002162 /* interrupts */
2163 io_write(sd, 0x40, 0xc2); /* Configure INT1 */
2164 io_write(sd, 0x41, 0xd7); /* STDI irq for any change, disable INT2 */
2165 io_write(sd, 0x46, 0x98); /* Enable SSPD, STDI and CP unlocked interrupts */
Mats Randgaardf24d2292013-12-10 10:15:13 -03002166 io_write(sd, 0x6e, 0xc1); /* Enable V_LOCKED, DE_REGEN_LCK, HDMI_MODE interrupts */
Mats Randgaard4a31a932013-12-10 09:45:00 -03002167 io_write(sd, 0x73, 0x1e); /* Enable CABLE_DET_A_ST (+5v) interrupts */
Hans Verkuil54450f52012-07-18 05:45:16 -03002168
2169 return v4l2_ctrl_handler_setup(sd->ctrl_handler);
2170}
2171
2172static void adv7604_unregister_clients(struct adv7604_state *state)
2173{
2174 if (state->i2c_avlink)
2175 i2c_unregister_device(state->i2c_avlink);
2176 if (state->i2c_cec)
2177 i2c_unregister_device(state->i2c_cec);
2178 if (state->i2c_infoframe)
2179 i2c_unregister_device(state->i2c_infoframe);
2180 if (state->i2c_esdp)
2181 i2c_unregister_device(state->i2c_esdp);
2182 if (state->i2c_dpp)
2183 i2c_unregister_device(state->i2c_dpp);
2184 if (state->i2c_afe)
2185 i2c_unregister_device(state->i2c_afe);
2186 if (state->i2c_repeater)
2187 i2c_unregister_device(state->i2c_repeater);
2188 if (state->i2c_edid)
2189 i2c_unregister_device(state->i2c_edid);
2190 if (state->i2c_hdmi)
2191 i2c_unregister_device(state->i2c_hdmi);
2192 if (state->i2c_test)
2193 i2c_unregister_device(state->i2c_test);
2194 if (state->i2c_cp)
2195 i2c_unregister_device(state->i2c_cp);
2196 if (state->i2c_vdp)
2197 i2c_unregister_device(state->i2c_vdp);
2198}
2199
2200static struct i2c_client *adv7604_dummy_client(struct v4l2_subdev *sd,
2201 u8 addr, u8 io_reg)
2202{
2203 struct i2c_client *client = v4l2_get_subdevdata(sd);
2204
2205 if (addr)
2206 io_write(sd, io_reg, addr << 1);
2207 return i2c_new_dummy(client->adapter, io_read(sd, io_reg) >> 1);
2208}
2209
2210static int adv7604_probe(struct i2c_client *client,
2211 const struct i2c_device_id *id)
2212{
Hans Verkuil591b72f2013-12-17 10:05:13 -03002213 static const struct v4l2_dv_timings cea640x480 =
2214 V4L2_DV_BT_CEA_640X480P59_94;
Hans Verkuil54450f52012-07-18 05:45:16 -03002215 struct adv7604_state *state;
2216 struct adv7604_platform_data *pdata = client->dev.platform_data;
2217 struct v4l2_ctrl_handler *hdl;
2218 struct v4l2_subdev *sd;
2219 int err;
2220
2221 /* Check if the adapter supports the needed features */
2222 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
2223 return -EIO;
2224 v4l_dbg(1, debug, client, "detecting adv7604 client on address 0x%x\n",
2225 client->addr << 1);
2226
Laurent Pinchartc02b2112013-05-02 08:29:43 -03002227 state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
Hans Verkuil54450f52012-07-18 05:45:16 -03002228 if (!state) {
2229 v4l_err(client, "Could not allocate adv7604_state memory!\n");
2230 return -ENOMEM;
2231 }
2232
Mats Randgaard25a64ac2013-08-14 07:58:45 -03002233 /* initialize variables */
2234 state->restart_stdi_once = true;
Mats Randgaardff4f80f2013-12-05 10:24:05 -03002235 state->selected_input = ~0;
Mats Randgaard25a64ac2013-08-14 07:58:45 -03002236
Hans Verkuil54450f52012-07-18 05:45:16 -03002237 /* platform data */
2238 if (!pdata) {
2239 v4l_err(client, "No platform data!\n");
Laurent Pinchartc02b2112013-05-02 08:29:43 -03002240 return -ENODEV;
Hans Verkuil54450f52012-07-18 05:45:16 -03002241 }
Hans Verkuil591b72f2013-12-17 10:05:13 -03002242 state->pdata = *pdata;
2243 state->timings = cea640x480;
Hans Verkuil54450f52012-07-18 05:45:16 -03002244
2245 sd = &state->sd;
2246 v4l2_i2c_subdev_init(sd, client, &adv7604_ops);
2247 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
Hans Verkuil54450f52012-07-18 05:45:16 -03002248
2249 /* i2c access to adv7604? */
2250 if (adv_smbus_read_byte_data_check(client, 0xfb, false) != 0x68) {
2251 v4l2_info(sd, "not an adv7604 on address 0x%x\n",
2252 client->addr << 1);
Laurent Pinchartc02b2112013-05-02 08:29:43 -03002253 return -ENODEV;
Hans Verkuil54450f52012-07-18 05:45:16 -03002254 }
2255
2256 /* control handlers */
2257 hdl = &state->hdl;
2258 v4l2_ctrl_handler_init(hdl, 9);
2259
2260 v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
2261 V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
2262 v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
2263 V4L2_CID_CONTRAST, 0, 255, 1, 128);
2264 v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
2265 V4L2_CID_SATURATION, 0, 255, 1, 128);
2266 v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
2267 V4L2_CID_HUE, 0, 128, 1, 0);
2268
2269 /* private controls */
2270 state->detect_tx_5v_ctrl = v4l2_ctrl_new_std(hdl, NULL,
Mats Randgaard4a31a932013-12-10 09:45:00 -03002271 V4L2_CID_DV_RX_POWER_PRESENT, 0, 0x0f, 0, 0);
Hans Verkuil54450f52012-07-18 05:45:16 -03002272 state->rgb_quantization_range_ctrl =
2273 v4l2_ctrl_new_std_menu(hdl, &adv7604_ctrl_ops,
2274 V4L2_CID_DV_RX_RGB_RANGE, V4L2_DV_RGB_RANGE_FULL,
2275 0, V4L2_DV_RGB_RANGE_AUTO);
Hans Verkuil54450f52012-07-18 05:45:16 -03002276
2277 /* custom controls */
2278 state->analog_sampling_phase_ctrl =
2279 v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_analog_sampling_phase, NULL);
Hans Verkuil54450f52012-07-18 05:45:16 -03002280 state->free_run_color_manual_ctrl =
2281 v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_free_run_color_manual, NULL);
Hans Verkuil54450f52012-07-18 05:45:16 -03002282 state->free_run_color_ctrl =
2283 v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_free_run_color, NULL);
Hans Verkuil54450f52012-07-18 05:45:16 -03002284
2285 sd->ctrl_handler = hdl;
2286 if (hdl->error) {
2287 err = hdl->error;
2288 goto err_hdl;
2289 }
Hans Verkuil8c0eadb2013-08-22 06:11:17 -03002290 state->detect_tx_5v_ctrl->is_private = true;
2291 state->rgb_quantization_range_ctrl->is_private = true;
2292 state->analog_sampling_phase_ctrl->is_private = true;
2293 state->free_run_color_manual_ctrl->is_private = true;
2294 state->free_run_color_ctrl->is_private = true;
2295
Hans Verkuil54450f52012-07-18 05:45:16 -03002296 if (adv7604_s_detect_tx_5v_ctrl(sd)) {
2297 err = -ENODEV;
2298 goto err_hdl;
2299 }
2300
2301 state->i2c_avlink = adv7604_dummy_client(sd, pdata->i2c_avlink, 0xf3);
2302 state->i2c_cec = adv7604_dummy_client(sd, pdata->i2c_cec, 0xf4);
2303 state->i2c_infoframe = adv7604_dummy_client(sd, pdata->i2c_infoframe, 0xf5);
2304 state->i2c_esdp = adv7604_dummy_client(sd, pdata->i2c_esdp, 0xf6);
2305 state->i2c_dpp = adv7604_dummy_client(sd, pdata->i2c_dpp, 0xf7);
2306 state->i2c_afe = adv7604_dummy_client(sd, pdata->i2c_afe, 0xf8);
2307 state->i2c_repeater = adv7604_dummy_client(sd, pdata->i2c_repeater, 0xf9);
2308 state->i2c_edid = adv7604_dummy_client(sd, pdata->i2c_edid, 0xfa);
2309 state->i2c_hdmi = adv7604_dummy_client(sd, pdata->i2c_hdmi, 0xfb);
2310 state->i2c_test = adv7604_dummy_client(sd, pdata->i2c_test, 0xfc);
2311 state->i2c_cp = adv7604_dummy_client(sd, pdata->i2c_cp, 0xfd);
2312 state->i2c_vdp = adv7604_dummy_client(sd, pdata->i2c_vdp, 0xfe);
2313 if (!state->i2c_avlink || !state->i2c_cec || !state->i2c_infoframe ||
2314 !state->i2c_esdp || !state->i2c_dpp || !state->i2c_afe ||
2315 !state->i2c_repeater || !state->i2c_edid || !state->i2c_hdmi ||
2316 !state->i2c_test || !state->i2c_cp || !state->i2c_vdp) {
2317 err = -ENOMEM;
2318 v4l2_err(sd, "failed to create all i2c clients\n");
2319 goto err_i2c;
2320 }
Hans Verkuil54450f52012-07-18 05:45:16 -03002321
2322 /* work queues */
2323 state->work_queues = create_singlethread_workqueue(client->name);
2324 if (!state->work_queues) {
2325 v4l2_err(sd, "Could not create work queue\n");
2326 err = -ENOMEM;
2327 goto err_i2c;
2328 }
2329
2330 INIT_DELAYED_WORK(&state->delayed_work_enable_hotplug,
2331 adv7604_delayed_work_enable_hotplug);
2332
2333 state->pad.flags = MEDIA_PAD_FL_SOURCE;
2334 err = media_entity_init(&sd->entity, 1, &state->pad, 0);
2335 if (err)
2336 goto err_work_queues;
2337
2338 err = adv7604_core_init(sd);
2339 if (err)
2340 goto err_entity;
2341 v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
2342 client->addr << 1, client->adapter->name);
Lars-Peter Clausenbedc3932013-11-25 16:18:02 -03002343
2344 err = v4l2_async_register_subdev(sd);
2345 if (err)
2346 goto err_entity;
2347
Hans Verkuil54450f52012-07-18 05:45:16 -03002348 return 0;
2349
2350err_entity:
2351 media_entity_cleanup(&sd->entity);
2352err_work_queues:
2353 cancel_delayed_work(&state->delayed_work_enable_hotplug);
2354 destroy_workqueue(state->work_queues);
2355err_i2c:
2356 adv7604_unregister_clients(state);
2357err_hdl:
2358 v4l2_ctrl_handler_free(hdl);
Hans Verkuil54450f52012-07-18 05:45:16 -03002359 return err;
2360}
2361
2362/* ----------------------------------------------------------------------- */
2363
2364static int adv7604_remove(struct i2c_client *client)
2365{
2366 struct v4l2_subdev *sd = i2c_get_clientdata(client);
2367 struct adv7604_state *state = to_state(sd);
2368
2369 cancel_delayed_work(&state->delayed_work_enable_hotplug);
2370 destroy_workqueue(state->work_queues);
Lars-Peter Clausenbedc3932013-11-25 16:18:02 -03002371 v4l2_async_unregister_subdev(sd);
Hans Verkuil54450f52012-07-18 05:45:16 -03002372 v4l2_device_unregister_subdev(sd);
2373 media_entity_cleanup(&sd->entity);
2374 adv7604_unregister_clients(to_state(sd));
2375 v4l2_ctrl_handler_free(sd->ctrl_handler);
Hans Verkuil54450f52012-07-18 05:45:16 -03002376 return 0;
2377}
2378
2379/* ----------------------------------------------------------------------- */
2380
2381static struct i2c_device_id adv7604_id[] = {
2382 { "adv7604", 0 },
2383 { }
2384};
2385MODULE_DEVICE_TABLE(i2c, adv7604_id);
2386
2387static struct i2c_driver adv7604_driver = {
2388 .driver = {
2389 .owner = THIS_MODULE,
2390 .name = "adv7604",
2391 },
2392 .probe = adv7604_probe,
2393 .remove = adv7604_remove,
2394 .id_table = adv7604_id,
2395};
2396
2397module_i2c_driver(adv7604_driver);