blob: 79fb34d5ee2d03a93cb66aab8dcb0c7880094554 [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
Lars-Peter Clausend42010a2013-11-25 15:45:07 -030056enum adv7604_type {
57 ADV7604,
58 ADV7611,
59};
60
61struct adv7604_reg_seq {
62 unsigned int reg;
63 u8 val;
64};
65
66struct adv7604_chip_info {
67 enum adv7604_type type;
68
69 bool has_afe;
70 unsigned int max_port;
71 unsigned int num_dv_ports;
72
73 unsigned int edid_enable_reg;
74 unsigned int edid_status_reg;
75 unsigned int lcf_reg;
76
77 unsigned int cable_det_mask;
78 unsigned int tdms_lock_mask;
79 unsigned int fmt_change_digital_mask;
80
81 void (*set_termination)(struct v4l2_subdev *sd, bool enable);
82 void (*setup_irqs)(struct v4l2_subdev *sd);
83 unsigned int (*read_hdmi_pixelclock)(struct v4l2_subdev *sd);
84 unsigned int (*read_cable_det)(struct v4l2_subdev *sd);
85
86 /* 0 = AFE, 1 = HDMI */
87 const struct adv7604_reg_seq *recommended_settings[2];
88 unsigned int num_recommended_settings[2];
89
90 unsigned long page_mask;
91};
92
Hans Verkuil54450f52012-07-18 05:45:16 -030093/*
94 **********************************************************************
95 *
96 * Arrays with configuration parameters for the ADV7604
97 *
98 **********************************************************************
99 */
Laurent Pinchartc784b1e2014-01-29 10:08:58 -0300100
Hans Verkuil54450f52012-07-18 05:45:16 -0300101struct adv7604_state {
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300102 const struct adv7604_chip_info *info;
Hans Verkuil54450f52012-07-18 05:45:16 -0300103 struct adv7604_platform_data pdata;
104 struct v4l2_subdev sd;
Laurent Pinchartc784b1e2014-01-29 10:08:58 -0300105 struct media_pad pads[ADV7604_PAD_MAX];
106 unsigned int source_pad;
Hans Verkuil54450f52012-07-18 05:45:16 -0300107 struct v4l2_ctrl_handler hdl;
Laurent Pinchartc784b1e2014-01-29 10:08:58 -0300108 enum adv7604_pad selected_input;
Hans Verkuil54450f52012-07-18 05:45:16 -0300109 struct v4l2_dv_timings timings;
Mats Randgaard4a31a932013-12-10 09:45:00 -0300110 struct {
111 u8 edid[256];
112 u32 present;
113 unsigned blocks;
114 } edid;
Mats Randgaarddd08beb2013-12-10 09:57:09 -0300115 u16 spa_port_a[2];
Hans Verkuil54450f52012-07-18 05:45:16 -0300116 struct v4l2_fract aspect_ratio;
117 u32 rgb_quantization_range;
118 struct workqueue_struct *work_queues;
119 struct delayed_work delayed_work_enable_hotplug;
Hans Verkuilcf9afb12012-10-16 10:12:55 -0300120 bool restart_stdi_once;
Hans Verkuil54450f52012-07-18 05:45:16 -0300121
122 /* i2c clients */
123 struct i2c_client *i2c_avlink;
124 struct i2c_client *i2c_cec;
125 struct i2c_client *i2c_infoframe;
126 struct i2c_client *i2c_esdp;
127 struct i2c_client *i2c_dpp;
128 struct i2c_client *i2c_afe;
129 struct i2c_client *i2c_repeater;
130 struct i2c_client *i2c_edid;
131 struct i2c_client *i2c_hdmi;
132 struct i2c_client *i2c_test;
133 struct i2c_client *i2c_cp;
134 struct i2c_client *i2c_vdp;
135
136 /* controls */
137 struct v4l2_ctrl *detect_tx_5v_ctrl;
138 struct v4l2_ctrl *analog_sampling_phase_ctrl;
139 struct v4l2_ctrl *free_run_color_manual_ctrl;
140 struct v4l2_ctrl *free_run_color_ctrl;
141 struct v4l2_ctrl *rgb_quantization_range_ctrl;
142};
143
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300144static bool adv7604_has_afe(struct adv7604_state *state)
145{
146 return state->info->has_afe;
147}
148
Hans Verkuil54450f52012-07-18 05:45:16 -0300149/* Supported CEA and DMT timings */
150static const struct v4l2_dv_timings adv7604_timings[] = {
151 V4L2_DV_BT_CEA_720X480P59_94,
152 V4L2_DV_BT_CEA_720X576P50,
153 V4L2_DV_BT_CEA_1280X720P24,
154 V4L2_DV_BT_CEA_1280X720P25,
Hans Verkuil54450f52012-07-18 05:45:16 -0300155 V4L2_DV_BT_CEA_1280X720P50,
156 V4L2_DV_BT_CEA_1280X720P60,
157 V4L2_DV_BT_CEA_1920X1080P24,
158 V4L2_DV_BT_CEA_1920X1080P25,
159 V4L2_DV_BT_CEA_1920X1080P30,
160 V4L2_DV_BT_CEA_1920X1080P50,
161 V4L2_DV_BT_CEA_1920X1080P60,
162
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300163 /* sorted by DMT ID */
Hans Verkuil54450f52012-07-18 05:45:16 -0300164 V4L2_DV_BT_DMT_640X350P85,
165 V4L2_DV_BT_DMT_640X400P85,
166 V4L2_DV_BT_DMT_720X400P85,
167 V4L2_DV_BT_DMT_640X480P60,
168 V4L2_DV_BT_DMT_640X480P72,
169 V4L2_DV_BT_DMT_640X480P75,
170 V4L2_DV_BT_DMT_640X480P85,
171 V4L2_DV_BT_DMT_800X600P56,
172 V4L2_DV_BT_DMT_800X600P60,
173 V4L2_DV_BT_DMT_800X600P72,
174 V4L2_DV_BT_DMT_800X600P75,
175 V4L2_DV_BT_DMT_800X600P85,
176 V4L2_DV_BT_DMT_848X480P60,
177 V4L2_DV_BT_DMT_1024X768P60,
178 V4L2_DV_BT_DMT_1024X768P70,
179 V4L2_DV_BT_DMT_1024X768P75,
180 V4L2_DV_BT_DMT_1024X768P85,
181 V4L2_DV_BT_DMT_1152X864P75,
182 V4L2_DV_BT_DMT_1280X768P60_RB,
183 V4L2_DV_BT_DMT_1280X768P60,
184 V4L2_DV_BT_DMT_1280X768P75,
185 V4L2_DV_BT_DMT_1280X768P85,
186 V4L2_DV_BT_DMT_1280X800P60_RB,
187 V4L2_DV_BT_DMT_1280X800P60,
188 V4L2_DV_BT_DMT_1280X800P75,
189 V4L2_DV_BT_DMT_1280X800P85,
190 V4L2_DV_BT_DMT_1280X960P60,
191 V4L2_DV_BT_DMT_1280X960P85,
192 V4L2_DV_BT_DMT_1280X1024P60,
193 V4L2_DV_BT_DMT_1280X1024P75,
194 V4L2_DV_BT_DMT_1280X1024P85,
195 V4L2_DV_BT_DMT_1360X768P60,
196 V4L2_DV_BT_DMT_1400X1050P60_RB,
197 V4L2_DV_BT_DMT_1400X1050P60,
198 V4L2_DV_BT_DMT_1400X1050P75,
199 V4L2_DV_BT_DMT_1400X1050P85,
200 V4L2_DV_BT_DMT_1440X900P60_RB,
201 V4L2_DV_BT_DMT_1440X900P60,
202 V4L2_DV_BT_DMT_1600X1200P60,
203 V4L2_DV_BT_DMT_1680X1050P60_RB,
204 V4L2_DV_BT_DMT_1680X1050P60,
205 V4L2_DV_BT_DMT_1792X1344P60,
206 V4L2_DV_BT_DMT_1856X1392P60,
207 V4L2_DV_BT_DMT_1920X1200P60_RB,
Martin Bugge547ed542013-12-05 10:01:17 -0300208 V4L2_DV_BT_DMT_1366X768P60_RB,
Hans Verkuil54450f52012-07-18 05:45:16 -0300209 V4L2_DV_BT_DMT_1366X768P60,
210 V4L2_DV_BT_DMT_1920X1080P60,
211 { },
212};
213
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300214struct adv7604_video_standards {
215 struct v4l2_dv_timings timings;
216 u8 vid_std;
217 u8 v_freq;
218};
219
220/* sorted by number of lines */
221static const struct adv7604_video_standards adv7604_prim_mode_comp[] = {
222 /* { V4L2_DV_BT_CEA_720X480P59_94, 0x0a, 0x00 }, TODO flickering */
223 { V4L2_DV_BT_CEA_720X576P50, 0x0b, 0x00 },
224 { V4L2_DV_BT_CEA_1280X720P50, 0x19, 0x01 },
225 { V4L2_DV_BT_CEA_1280X720P60, 0x19, 0x00 },
226 { V4L2_DV_BT_CEA_1920X1080P24, 0x1e, 0x04 },
227 { V4L2_DV_BT_CEA_1920X1080P25, 0x1e, 0x03 },
228 { V4L2_DV_BT_CEA_1920X1080P30, 0x1e, 0x02 },
229 { V4L2_DV_BT_CEA_1920X1080P50, 0x1e, 0x01 },
230 { V4L2_DV_BT_CEA_1920X1080P60, 0x1e, 0x00 },
231 /* TODO add 1920x1080P60_RB (CVT timing) */
232 { },
233};
234
235/* sorted by number of lines */
236static const struct adv7604_video_standards adv7604_prim_mode_gr[] = {
237 { V4L2_DV_BT_DMT_640X480P60, 0x08, 0x00 },
238 { V4L2_DV_BT_DMT_640X480P72, 0x09, 0x00 },
239 { V4L2_DV_BT_DMT_640X480P75, 0x0a, 0x00 },
240 { V4L2_DV_BT_DMT_640X480P85, 0x0b, 0x00 },
241 { V4L2_DV_BT_DMT_800X600P56, 0x00, 0x00 },
242 { V4L2_DV_BT_DMT_800X600P60, 0x01, 0x00 },
243 { V4L2_DV_BT_DMT_800X600P72, 0x02, 0x00 },
244 { V4L2_DV_BT_DMT_800X600P75, 0x03, 0x00 },
245 { V4L2_DV_BT_DMT_800X600P85, 0x04, 0x00 },
246 { V4L2_DV_BT_DMT_1024X768P60, 0x0c, 0x00 },
247 { V4L2_DV_BT_DMT_1024X768P70, 0x0d, 0x00 },
248 { V4L2_DV_BT_DMT_1024X768P75, 0x0e, 0x00 },
249 { V4L2_DV_BT_DMT_1024X768P85, 0x0f, 0x00 },
250 { V4L2_DV_BT_DMT_1280X1024P60, 0x05, 0x00 },
251 { V4L2_DV_BT_DMT_1280X1024P75, 0x06, 0x00 },
252 { V4L2_DV_BT_DMT_1360X768P60, 0x12, 0x00 },
253 { V4L2_DV_BT_DMT_1366X768P60, 0x13, 0x00 },
254 { V4L2_DV_BT_DMT_1400X1050P60, 0x14, 0x00 },
255 { V4L2_DV_BT_DMT_1400X1050P75, 0x15, 0x00 },
256 { V4L2_DV_BT_DMT_1600X1200P60, 0x16, 0x00 }, /* TODO not tested */
257 /* TODO add 1600X1200P60_RB (not a DMT timing) */
258 { V4L2_DV_BT_DMT_1680X1050P60, 0x18, 0x00 },
259 { V4L2_DV_BT_DMT_1920X1200P60_RB, 0x19, 0x00 }, /* TODO not tested */
260 { },
261};
262
263/* sorted by number of lines */
264static const struct adv7604_video_standards adv7604_prim_mode_hdmi_comp[] = {
265 { V4L2_DV_BT_CEA_720X480P59_94, 0x0a, 0x00 },
266 { V4L2_DV_BT_CEA_720X576P50, 0x0b, 0x00 },
267 { V4L2_DV_BT_CEA_1280X720P50, 0x13, 0x01 },
268 { V4L2_DV_BT_CEA_1280X720P60, 0x13, 0x00 },
269 { V4L2_DV_BT_CEA_1920X1080P24, 0x1e, 0x04 },
270 { V4L2_DV_BT_CEA_1920X1080P25, 0x1e, 0x03 },
271 { V4L2_DV_BT_CEA_1920X1080P30, 0x1e, 0x02 },
272 { V4L2_DV_BT_CEA_1920X1080P50, 0x1e, 0x01 },
273 { V4L2_DV_BT_CEA_1920X1080P60, 0x1e, 0x00 },
274 { },
275};
276
277/* sorted by number of lines */
278static const struct adv7604_video_standards adv7604_prim_mode_hdmi_gr[] = {
279 { V4L2_DV_BT_DMT_640X480P60, 0x08, 0x00 },
280 { V4L2_DV_BT_DMT_640X480P72, 0x09, 0x00 },
281 { V4L2_DV_BT_DMT_640X480P75, 0x0a, 0x00 },
282 { V4L2_DV_BT_DMT_640X480P85, 0x0b, 0x00 },
283 { V4L2_DV_BT_DMT_800X600P56, 0x00, 0x00 },
284 { V4L2_DV_BT_DMT_800X600P60, 0x01, 0x00 },
285 { V4L2_DV_BT_DMT_800X600P72, 0x02, 0x00 },
286 { V4L2_DV_BT_DMT_800X600P75, 0x03, 0x00 },
287 { V4L2_DV_BT_DMT_800X600P85, 0x04, 0x00 },
288 { V4L2_DV_BT_DMT_1024X768P60, 0x0c, 0x00 },
289 { V4L2_DV_BT_DMT_1024X768P70, 0x0d, 0x00 },
290 { V4L2_DV_BT_DMT_1024X768P75, 0x0e, 0x00 },
291 { V4L2_DV_BT_DMT_1024X768P85, 0x0f, 0x00 },
292 { V4L2_DV_BT_DMT_1280X1024P60, 0x05, 0x00 },
293 { V4L2_DV_BT_DMT_1280X1024P75, 0x06, 0x00 },
294 { },
295};
296
Hans Verkuil54450f52012-07-18 05:45:16 -0300297/* ----------------------------------------------------------------------- */
298
299static inline struct adv7604_state *to_state(struct v4l2_subdev *sd)
300{
301 return container_of(sd, struct adv7604_state, sd);
302}
303
304static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
305{
306 return &container_of(ctrl->handler, struct adv7604_state, hdl)->sd;
307}
308
309static inline unsigned hblanking(const struct v4l2_bt_timings *t)
310{
Hans Verkuileacf8f92013-07-29 08:40:59 -0300311 return V4L2_DV_BT_BLANKING_WIDTH(t);
Hans Verkuil54450f52012-07-18 05:45:16 -0300312}
313
314static inline unsigned htotal(const struct v4l2_bt_timings *t)
315{
Hans Verkuileacf8f92013-07-29 08:40:59 -0300316 return V4L2_DV_BT_FRAME_WIDTH(t);
Hans Verkuil54450f52012-07-18 05:45:16 -0300317}
318
319static inline unsigned vblanking(const struct v4l2_bt_timings *t)
320{
Hans Verkuileacf8f92013-07-29 08:40:59 -0300321 return V4L2_DV_BT_BLANKING_HEIGHT(t);
Hans Verkuil54450f52012-07-18 05:45:16 -0300322}
323
324static inline unsigned vtotal(const struct v4l2_bt_timings *t)
325{
Hans Verkuileacf8f92013-07-29 08:40:59 -0300326 return V4L2_DV_BT_FRAME_HEIGHT(t);
Hans Verkuil54450f52012-07-18 05:45:16 -0300327}
328
329/* ----------------------------------------------------------------------- */
330
331static s32 adv_smbus_read_byte_data_check(struct i2c_client *client,
332 u8 command, bool check)
333{
334 union i2c_smbus_data data;
335
336 if (!i2c_smbus_xfer(client->adapter, client->addr, client->flags,
337 I2C_SMBUS_READ, command,
338 I2C_SMBUS_BYTE_DATA, &data))
339 return data.byte;
340 if (check)
341 v4l_err(client, "error reading %02x, %02x\n",
342 client->addr, command);
343 return -EIO;
344}
345
346static s32 adv_smbus_read_byte_data(struct i2c_client *client, u8 command)
347{
348 return adv_smbus_read_byte_data_check(client, command, true);
349}
350
351static s32 adv_smbus_write_byte_data(struct i2c_client *client,
352 u8 command, u8 value)
353{
354 union i2c_smbus_data data;
355 int err;
356 int i;
357
358 data.byte = value;
359 for (i = 0; i < 3; i++) {
360 err = i2c_smbus_xfer(client->adapter, client->addr,
361 client->flags,
362 I2C_SMBUS_WRITE, command,
363 I2C_SMBUS_BYTE_DATA, &data);
364 if (!err)
365 break;
366 }
367 if (err < 0)
368 v4l_err(client, "error writing %02x, %02x, %02x\n",
369 client->addr, command, value);
370 return err;
371}
372
373static s32 adv_smbus_write_i2c_block_data(struct i2c_client *client,
374 u8 command, unsigned length, const u8 *values)
375{
376 union i2c_smbus_data data;
377
378 if (length > I2C_SMBUS_BLOCK_MAX)
379 length = I2C_SMBUS_BLOCK_MAX;
380 data.block[0] = length;
381 memcpy(data.block + 1, values, length);
382 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
383 I2C_SMBUS_WRITE, command,
384 I2C_SMBUS_I2C_BLOCK_DATA, &data);
385}
386
387/* ----------------------------------------------------------------------- */
388
389static inline int io_read(struct v4l2_subdev *sd, u8 reg)
390{
391 struct i2c_client *client = v4l2_get_subdevdata(sd);
392
393 return adv_smbus_read_byte_data(client, reg);
394}
395
396static inline int io_write(struct v4l2_subdev *sd, u8 reg, u8 val)
397{
398 struct i2c_client *client = v4l2_get_subdevdata(sd);
399
400 return adv_smbus_write_byte_data(client, reg, val);
401}
402
403static inline int io_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
404{
405 return io_write(sd, reg, (io_read(sd, reg) & mask) | val);
406}
407
408static inline int avlink_read(struct v4l2_subdev *sd, u8 reg)
409{
410 struct adv7604_state *state = to_state(sd);
411
412 return adv_smbus_read_byte_data(state->i2c_avlink, reg);
413}
414
415static inline int avlink_write(struct v4l2_subdev *sd, u8 reg, u8 val)
416{
417 struct adv7604_state *state = to_state(sd);
418
419 return adv_smbus_write_byte_data(state->i2c_avlink, reg, val);
420}
421
422static inline int cec_read(struct v4l2_subdev *sd, u8 reg)
423{
424 struct adv7604_state *state = to_state(sd);
425
426 return adv_smbus_read_byte_data(state->i2c_cec, reg);
427}
428
429static inline int cec_write(struct v4l2_subdev *sd, u8 reg, u8 val)
430{
431 struct adv7604_state *state = to_state(sd);
432
433 return adv_smbus_write_byte_data(state->i2c_cec, reg, val);
434}
435
436static inline int cec_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
437{
438 return cec_write(sd, reg, (cec_read(sd, reg) & mask) | val);
439}
440
441static inline int infoframe_read(struct v4l2_subdev *sd, u8 reg)
442{
443 struct adv7604_state *state = to_state(sd);
444
445 return adv_smbus_read_byte_data(state->i2c_infoframe, reg);
446}
447
448static inline int infoframe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
449{
450 struct adv7604_state *state = to_state(sd);
451
452 return adv_smbus_write_byte_data(state->i2c_infoframe, reg, val);
453}
454
455static inline int esdp_read(struct v4l2_subdev *sd, u8 reg)
456{
457 struct adv7604_state *state = to_state(sd);
458
459 return adv_smbus_read_byte_data(state->i2c_esdp, reg);
460}
461
462static inline int esdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
463{
464 struct adv7604_state *state = to_state(sd);
465
466 return adv_smbus_write_byte_data(state->i2c_esdp, reg, val);
467}
468
469static inline int dpp_read(struct v4l2_subdev *sd, u8 reg)
470{
471 struct adv7604_state *state = to_state(sd);
472
473 return adv_smbus_read_byte_data(state->i2c_dpp, reg);
474}
475
476static inline int dpp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
477{
478 struct adv7604_state *state = to_state(sd);
479
480 return adv_smbus_write_byte_data(state->i2c_dpp, reg, val);
481}
482
483static inline int afe_read(struct v4l2_subdev *sd, u8 reg)
484{
485 struct adv7604_state *state = to_state(sd);
486
487 return adv_smbus_read_byte_data(state->i2c_afe, reg);
488}
489
490static inline int afe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
491{
492 struct adv7604_state *state = to_state(sd);
493
494 return adv_smbus_write_byte_data(state->i2c_afe, reg, val);
495}
496
497static inline int rep_read(struct v4l2_subdev *sd, u8 reg)
498{
499 struct adv7604_state *state = to_state(sd);
500
501 return adv_smbus_read_byte_data(state->i2c_repeater, reg);
502}
503
504static inline int rep_write(struct v4l2_subdev *sd, u8 reg, u8 val)
505{
506 struct adv7604_state *state = to_state(sd);
507
508 return adv_smbus_write_byte_data(state->i2c_repeater, reg, val);
509}
510
511static inline int rep_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
512{
513 return rep_write(sd, reg, (rep_read(sd, reg) & mask) | val);
514}
515
516static inline int edid_read(struct v4l2_subdev *sd, u8 reg)
517{
518 struct adv7604_state *state = to_state(sd);
519
520 return adv_smbus_read_byte_data(state->i2c_edid, reg);
521}
522
523static inline int edid_write(struct v4l2_subdev *sd, u8 reg, u8 val)
524{
525 struct adv7604_state *state = to_state(sd);
526
527 return adv_smbus_write_byte_data(state->i2c_edid, reg, val);
528}
529
530static inline int edid_read_block(struct v4l2_subdev *sd, unsigned len, u8 *val)
531{
532 struct adv7604_state *state = to_state(sd);
533 struct i2c_client *client = state->i2c_edid;
534 u8 msgbuf0[1] = { 0 };
535 u8 msgbuf1[256];
Shubhrajyoti D09f29672012-10-25 01:02:36 -0300536 struct i2c_msg msg[2] = {
537 {
538 .addr = client->addr,
539 .len = 1,
540 .buf = msgbuf0
541 },
542 {
543 .addr = client->addr,
544 .flags = I2C_M_RD,
545 .len = len,
546 .buf = msgbuf1
547 },
548 };
Hans Verkuil54450f52012-07-18 05:45:16 -0300549
550 if (i2c_transfer(client->adapter, msg, 2) < 0)
551 return -EIO;
552 memcpy(val, msgbuf1, len);
553 return 0;
554}
555
Mats Randgaarddd08beb2013-12-10 09:57:09 -0300556static inline int edid_write_block(struct v4l2_subdev *sd,
557 unsigned len, const u8 *val)
558{
559 struct adv7604_state *state = to_state(sd);
560 int err = 0;
561 int i;
562
563 v4l2_dbg(2, debug, sd, "%s: write EDID block (%d byte)\n", __func__, len);
564
565 for (i = 0; !err && i < len; i += I2C_SMBUS_BLOCK_MAX)
566 err = adv_smbus_write_i2c_block_data(state->i2c_edid, i,
567 I2C_SMBUS_BLOCK_MAX, val + i);
568 return err;
569}
570
Hans Verkuil54450f52012-07-18 05:45:16 -0300571static void adv7604_delayed_work_enable_hotplug(struct work_struct *work)
572{
573 struct delayed_work *dwork = to_delayed_work(work);
574 struct adv7604_state *state = container_of(dwork, struct adv7604_state,
575 delayed_work_enable_hotplug);
576 struct v4l2_subdev *sd = &state->sd;
577
578 v4l2_dbg(2, debug, sd, "%s: enable hotplug\n", __func__);
579
Mats Randgaard4a31a932013-12-10 09:45:00 -0300580 v4l2_subdev_notify(sd, ADV7604_HOTPLUG, (void *)&state->edid.present);
Hans Verkuil54450f52012-07-18 05:45:16 -0300581}
582
Hans Verkuil54450f52012-07-18 05:45:16 -0300583static inline int hdmi_read(struct v4l2_subdev *sd, u8 reg)
584{
585 struct adv7604_state *state = to_state(sd);
586
587 return adv_smbus_read_byte_data(state->i2c_hdmi, reg);
588}
589
Laurent Pinchart51182a92014-01-08 19:30:37 -0300590static u16 hdmi_read16(struct v4l2_subdev *sd, u8 reg, u16 mask)
591{
592 return ((hdmi_read(sd, reg) << 8) | hdmi_read(sd, reg + 1)) & mask;
593}
594
Hans Verkuil54450f52012-07-18 05:45:16 -0300595static inline int hdmi_write(struct v4l2_subdev *sd, u8 reg, u8 val)
596{
597 struct adv7604_state *state = to_state(sd);
598
599 return adv_smbus_write_byte_data(state->i2c_hdmi, reg, val);
600}
601
Mats Randgaard4a31a932013-12-10 09:45:00 -0300602static inline int hdmi_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
603{
604 return hdmi_write(sd, reg, (hdmi_read(sd, reg) & mask) | val);
605}
606
Hans Verkuil54450f52012-07-18 05:45:16 -0300607static inline int test_read(struct v4l2_subdev *sd, u8 reg)
608{
609 struct adv7604_state *state = to_state(sd);
610
611 return adv_smbus_read_byte_data(state->i2c_test, reg);
612}
613
614static inline int test_write(struct v4l2_subdev *sd, u8 reg, u8 val)
615{
616 struct adv7604_state *state = to_state(sd);
617
618 return adv_smbus_write_byte_data(state->i2c_test, reg, val);
619}
620
621static inline int cp_read(struct v4l2_subdev *sd, u8 reg)
622{
623 struct adv7604_state *state = to_state(sd);
624
625 return adv_smbus_read_byte_data(state->i2c_cp, reg);
626}
627
Laurent Pinchart51182a92014-01-08 19:30:37 -0300628static u16 cp_read16(struct v4l2_subdev *sd, u8 reg, u16 mask)
629{
630 return ((cp_read(sd, reg) << 8) | cp_read(sd, reg + 1)) & mask;
631}
632
Hans Verkuil54450f52012-07-18 05:45:16 -0300633static inline int cp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
634{
635 struct adv7604_state *state = to_state(sd);
636
637 return adv_smbus_write_byte_data(state->i2c_cp, reg, val);
638}
639
640static inline int cp_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
641{
642 return cp_write(sd, reg, (cp_read(sd, reg) & mask) | val);
643}
644
645static inline int vdp_read(struct v4l2_subdev *sd, u8 reg)
646{
647 struct adv7604_state *state = to_state(sd);
648
649 return adv_smbus_read_byte_data(state->i2c_vdp, reg);
650}
651
652static inline int vdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
653{
654 struct adv7604_state *state = to_state(sd);
655
656 return adv_smbus_write_byte_data(state->i2c_vdp, reg, val);
657}
658
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300659enum {
660 ADV7604_PAGE_IO,
661 ADV7604_PAGE_AVLINK,
662 ADV7604_PAGE_CEC,
663 ADV7604_PAGE_INFOFRAME,
664 ADV7604_PAGE_ESDP,
665 ADV7604_PAGE_DPP,
666 ADV7604_PAGE_AFE,
667 ADV7604_PAGE_REP,
668 ADV7604_PAGE_EDID,
669 ADV7604_PAGE_HDMI,
670 ADV7604_PAGE_TEST,
671 ADV7604_PAGE_CP,
672 ADV7604_PAGE_VDP,
673 ADV7604_PAGE_TERM,
674};
675
676#define ADV7604_REG(page, offset) (((page) << 8) | (offset))
677#define ADV7604_REG_SEQ_TERM 0xffff
678
679#ifdef CONFIG_VIDEO_ADV_DEBUG
680static int adv7604_read_reg(struct v4l2_subdev *sd, unsigned int reg)
681{
682 struct adv7604_state *state = to_state(sd);
683 unsigned int page = reg >> 8;
684
685 if (!(BIT(page) & state->info->page_mask))
686 return -EINVAL;
687
688 reg &= 0xff;
689
690 switch (page) {
691 case ADV7604_PAGE_IO:
692 return io_read(sd, reg);
693 case ADV7604_PAGE_AVLINK:
694 return avlink_read(sd, reg);
695 case ADV7604_PAGE_CEC:
696 return cec_read(sd, reg);
697 case ADV7604_PAGE_INFOFRAME:
698 return infoframe_read(sd, reg);
699 case ADV7604_PAGE_ESDP:
700 return esdp_read(sd, reg);
701 case ADV7604_PAGE_DPP:
702 return dpp_read(sd, reg);
703 case ADV7604_PAGE_AFE:
704 return afe_read(sd, reg);
705 case ADV7604_PAGE_REP:
706 return rep_read(sd, reg);
707 case ADV7604_PAGE_EDID:
708 return edid_read(sd, reg);
709 case ADV7604_PAGE_HDMI:
710 return hdmi_read(sd, reg);
711 case ADV7604_PAGE_TEST:
712 return test_read(sd, reg);
713 case ADV7604_PAGE_CP:
714 return cp_read(sd, reg);
715 case ADV7604_PAGE_VDP:
716 return vdp_read(sd, reg);
717 }
718
719 return -EINVAL;
720}
721#endif
722
723static int adv7604_write_reg(struct v4l2_subdev *sd, unsigned int reg, u8 val)
724{
725 struct adv7604_state *state = to_state(sd);
726 unsigned int page = reg >> 8;
727
728 if (!(BIT(page) & state->info->page_mask))
729 return -EINVAL;
730
731 reg &= 0xff;
732
733 switch (page) {
734 case ADV7604_PAGE_IO:
735 return io_write(sd, reg, val);
736 case ADV7604_PAGE_AVLINK:
737 return avlink_write(sd, reg, val);
738 case ADV7604_PAGE_CEC:
739 return cec_write(sd, reg, val);
740 case ADV7604_PAGE_INFOFRAME:
741 return infoframe_write(sd, reg, val);
742 case ADV7604_PAGE_ESDP:
743 return esdp_write(sd, reg, val);
744 case ADV7604_PAGE_DPP:
745 return dpp_write(sd, reg, val);
746 case ADV7604_PAGE_AFE:
747 return afe_write(sd, reg, val);
748 case ADV7604_PAGE_REP:
749 return rep_write(sd, reg, val);
750 case ADV7604_PAGE_EDID:
751 return edid_write(sd, reg, val);
752 case ADV7604_PAGE_HDMI:
753 return hdmi_write(sd, reg, val);
754 case ADV7604_PAGE_TEST:
755 return test_write(sd, reg, val);
756 case ADV7604_PAGE_CP:
757 return cp_write(sd, reg, val);
758 case ADV7604_PAGE_VDP:
759 return vdp_write(sd, reg, val);
760 }
761
762 return -EINVAL;
763}
764
765static void adv7604_write_reg_seq(struct v4l2_subdev *sd,
766 const struct adv7604_reg_seq *reg_seq)
767{
768 unsigned int i;
769
770 for (i = 0; reg_seq[i].reg != ADV7604_REG_SEQ_TERM; i++)
771 adv7604_write_reg(sd, reg_seq[i].reg, reg_seq[i].val);
772}
773
Hans Verkuil54450f52012-07-18 05:45:16 -0300774/* ----------------------------------------------------------------------- */
775
Mats Randgaard4a31a932013-12-10 09:45:00 -0300776static inline bool is_analog_input(struct v4l2_subdev *sd)
777{
778 struct adv7604_state *state = to_state(sd);
779
Laurent Pinchartc784b1e2014-01-29 10:08:58 -0300780 return state->selected_input == ADV7604_PAD_VGA_RGB ||
781 state->selected_input == ADV7604_PAD_VGA_COMP;
Mats Randgaard4a31a932013-12-10 09:45:00 -0300782}
783
784static inline bool is_digital_input(struct v4l2_subdev *sd)
785{
786 struct adv7604_state *state = to_state(sd);
787
Laurent Pinchartc784b1e2014-01-29 10:08:58 -0300788 return state->selected_input == ADV7604_PAD_HDMI_PORT_A ||
789 state->selected_input == ADV7604_PAD_HDMI_PORT_B ||
790 state->selected_input == ADV7604_PAD_HDMI_PORT_C ||
791 state->selected_input == ADV7604_PAD_HDMI_PORT_D;
Mats Randgaard4a31a932013-12-10 09:45:00 -0300792}
793
794/* ----------------------------------------------------------------------- */
795
Hans Verkuil54450f52012-07-18 05:45:16 -0300796#ifdef CONFIG_VIDEO_ADV_DEBUG
797static void adv7604_inv_register(struct v4l2_subdev *sd)
798{
799 v4l2_info(sd, "0x000-0x0ff: IO Map\n");
800 v4l2_info(sd, "0x100-0x1ff: AVLink Map\n");
801 v4l2_info(sd, "0x200-0x2ff: CEC Map\n");
802 v4l2_info(sd, "0x300-0x3ff: InfoFrame Map\n");
803 v4l2_info(sd, "0x400-0x4ff: ESDP Map\n");
804 v4l2_info(sd, "0x500-0x5ff: DPP Map\n");
805 v4l2_info(sd, "0x600-0x6ff: AFE Map\n");
806 v4l2_info(sd, "0x700-0x7ff: Repeater Map\n");
807 v4l2_info(sd, "0x800-0x8ff: EDID Map\n");
808 v4l2_info(sd, "0x900-0x9ff: HDMI Map\n");
809 v4l2_info(sd, "0xa00-0xaff: Test Map\n");
810 v4l2_info(sd, "0xb00-0xbff: CP Map\n");
811 v4l2_info(sd, "0xc00-0xcff: VDP Map\n");
812}
813
814static int adv7604_g_register(struct v4l2_subdev *sd,
815 struct v4l2_dbg_register *reg)
816{
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300817 int ret;
818
819 ret = adv7604_read_reg(sd, reg->reg);
820 if (ret < 0) {
Hans Verkuil54450f52012-07-18 05:45:16 -0300821 v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
822 adv7604_inv_register(sd);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300823 return ret;
Hans Verkuil54450f52012-07-18 05:45:16 -0300824 }
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300825
826 reg->size = 1;
827 reg->val = ret;
828
Hans Verkuil54450f52012-07-18 05:45:16 -0300829 return 0;
830}
831
832static int adv7604_s_register(struct v4l2_subdev *sd,
Hans Verkuil977ba3b2013-03-24 08:28:46 -0300833 const struct v4l2_dbg_register *reg)
Hans Verkuil54450f52012-07-18 05:45:16 -0300834{
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300835 int ret;
Hans Verkuil15774612013-12-10 10:02:43 -0300836
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300837 ret = adv7604_write_reg(sd, reg->reg, reg->val);
838 if (ret < 0) {
Hans Verkuil54450f52012-07-18 05:45:16 -0300839 v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
840 adv7604_inv_register(sd);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300841 return ret;
Hans Verkuil54450f52012-07-18 05:45:16 -0300842 }
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300843
Hans Verkuil54450f52012-07-18 05:45:16 -0300844 return 0;
845}
846#endif
847
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300848static unsigned int adv7604_read_cable_det(struct v4l2_subdev *sd)
849{
850 u8 value = io_read(sd, 0x6f);
851
852 return ((value & 0x10) >> 4)
853 | ((value & 0x08) >> 2)
854 | ((value & 0x04) << 0)
855 | ((value & 0x02) << 2);
856}
857
858static unsigned int adv7611_read_cable_det(struct v4l2_subdev *sd)
859{
860 u8 value = io_read(sd, 0x6f);
861
862 return value & 1;
863}
864
Hans Verkuil54450f52012-07-18 05:45:16 -0300865static int adv7604_s_detect_tx_5v_ctrl(struct v4l2_subdev *sd)
866{
867 struct adv7604_state *state = to_state(sd);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300868 const struct adv7604_chip_info *info = state->info;
Hans Verkuil54450f52012-07-18 05:45:16 -0300869
Hans Verkuil54450f52012-07-18 05:45:16 -0300870 return v4l2_ctrl_s_ctrl(state->detect_tx_5v_ctrl,
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300871 info->read_cable_det(sd));
Hans Verkuil54450f52012-07-18 05:45:16 -0300872}
873
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300874static int find_and_set_predefined_video_timings(struct v4l2_subdev *sd,
875 u8 prim_mode,
876 const struct adv7604_video_standards *predef_vid_timings,
877 const struct v4l2_dv_timings *timings)
Hans Verkuil54450f52012-07-18 05:45:16 -0300878{
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300879 int i;
880
881 for (i = 0; predef_vid_timings[i].timings.bt.width; i++) {
Hans Verkuilef1ed8f2013-08-15 08:28:47 -0300882 if (!v4l2_match_dv_timings(timings, &predef_vid_timings[i].timings,
Mats Randgaard4a31a932013-12-10 09:45:00 -0300883 is_digital_input(sd) ? 250000 : 1000000))
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300884 continue;
885 io_write(sd, 0x00, predef_vid_timings[i].vid_std); /* video std */
886 io_write(sd, 0x01, (predef_vid_timings[i].v_freq << 4) +
887 prim_mode); /* v_freq and prim mode */
888 return 0;
889 }
890
891 return -1;
892}
893
894static int configure_predefined_video_timings(struct v4l2_subdev *sd,
895 struct v4l2_dv_timings *timings)
896{
897 struct adv7604_state *state = to_state(sd);
898 int err;
899
900 v4l2_dbg(1, debug, sd, "%s", __func__);
901
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300902 if (adv7604_has_afe(state)) {
903 /* reset to default values */
904 io_write(sd, 0x16, 0x43);
905 io_write(sd, 0x17, 0x5a);
906 }
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300907 /* disable embedded syncs for auto graphics mode */
908 cp_write_and_or(sd, 0x81, 0xef, 0x00);
909 cp_write(sd, 0x8f, 0x00);
910 cp_write(sd, 0x90, 0x00);
911 cp_write(sd, 0xa2, 0x00);
912 cp_write(sd, 0xa3, 0x00);
913 cp_write(sd, 0xa4, 0x00);
914 cp_write(sd, 0xa5, 0x00);
915 cp_write(sd, 0xa6, 0x00);
916 cp_write(sd, 0xa7, 0x00);
917 cp_write(sd, 0xab, 0x00);
918 cp_write(sd, 0xac, 0x00);
919
Mats Randgaard4a31a932013-12-10 09:45:00 -0300920 if (is_analog_input(sd)) {
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300921 err = find_and_set_predefined_video_timings(sd,
922 0x01, adv7604_prim_mode_comp, timings);
923 if (err)
924 err = find_and_set_predefined_video_timings(sd,
925 0x02, adv7604_prim_mode_gr, timings);
Mats Randgaard4a31a932013-12-10 09:45:00 -0300926 } else if (is_digital_input(sd)) {
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300927 err = find_and_set_predefined_video_timings(sd,
928 0x05, adv7604_prim_mode_hdmi_comp, timings);
929 if (err)
930 err = find_and_set_predefined_video_timings(sd,
931 0x06, adv7604_prim_mode_hdmi_gr, timings);
Mats Randgaard4a31a932013-12-10 09:45:00 -0300932 } else {
933 v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
934 __func__, state->selected_input);
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300935 err = -1;
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300936 }
937
938
939 return err;
940}
941
942static void configure_custom_video_timings(struct v4l2_subdev *sd,
943 const struct v4l2_bt_timings *bt)
944{
945 struct adv7604_state *state = to_state(sd);
Hans Verkuil54450f52012-07-18 05:45:16 -0300946 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300947 u32 width = htotal(bt);
948 u32 height = vtotal(bt);
949 u16 cp_start_sav = bt->hsync + bt->hbackporch - 4;
950 u16 cp_start_eav = width - bt->hfrontporch;
951 u16 cp_start_vbi = height - bt->vfrontporch;
952 u16 cp_end_vbi = bt->vsync + bt->vbackporch;
953 u16 ch1_fr_ll = (((u32)bt->pixelclock / 100) > 0) ?
954 ((width * (ADV7604_fsc / 100)) / ((u32)bt->pixelclock / 100)) : 0;
955 const u8 pll[2] = {
956 0xc0 | ((width >> 8) & 0x1f),
957 width & 0xff
958 };
Hans Verkuil54450f52012-07-18 05:45:16 -0300959
960 v4l2_dbg(2, debug, sd, "%s\n", __func__);
961
Mats Randgaard4a31a932013-12-10 09:45:00 -0300962 if (is_analog_input(sd)) {
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300963 /* auto graphics */
964 io_write(sd, 0x00, 0x07); /* video std */
965 io_write(sd, 0x01, 0x02); /* prim mode */
966 /* enable embedded syncs for auto graphics mode */
967 cp_write_and_or(sd, 0x81, 0xef, 0x10);
Hans Verkuil54450f52012-07-18 05:45:16 -0300968
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300969 /* Should only be set in auto-graphics mode [REF_02, p. 91-92] */
Hans Verkuil54450f52012-07-18 05:45:16 -0300970 /* setup PLL_DIV_MAN_EN and PLL_DIV_RATIO */
971 /* IO-map reg. 0x16 and 0x17 should be written in sequence */
Mats Randgaard4a31a932013-12-10 09:45:00 -0300972 if (adv_smbus_write_i2c_block_data(client, 0x16, 2, pll))
Hans Verkuil54450f52012-07-18 05:45:16 -0300973 v4l2_err(sd, "writing to reg 0x16 and 0x17 failed\n");
Hans Verkuil54450f52012-07-18 05:45:16 -0300974
975 /* active video - horizontal timing */
Hans Verkuil54450f52012-07-18 05:45:16 -0300976 cp_write(sd, 0xa2, (cp_start_sav >> 4) & 0xff);
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300977 cp_write(sd, 0xa3, ((cp_start_sav & 0x0f) << 4) |
Mats Randgaard4a31a932013-12-10 09:45:00 -0300978 ((cp_start_eav >> 8) & 0x0f));
Hans Verkuil54450f52012-07-18 05:45:16 -0300979 cp_write(sd, 0xa4, cp_start_eav & 0xff);
980
981 /* active video - vertical timing */
Hans Verkuil54450f52012-07-18 05:45:16 -0300982 cp_write(sd, 0xa5, (cp_start_vbi >> 4) & 0xff);
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300983 cp_write(sd, 0xa6, ((cp_start_vbi & 0xf) << 4) |
Mats Randgaard4a31a932013-12-10 09:45:00 -0300984 ((cp_end_vbi >> 8) & 0xf));
Hans Verkuil54450f52012-07-18 05:45:16 -0300985 cp_write(sd, 0xa7, cp_end_vbi & 0xff);
Mats Randgaard4a31a932013-12-10 09:45:00 -0300986 } else if (is_digital_input(sd)) {
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300987 /* set default prim_mode/vid_std for HDMI
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -0300988 according to [REF_03, c. 4.2] */
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300989 io_write(sd, 0x00, 0x02); /* video std */
990 io_write(sd, 0x01, 0x06); /* prim mode */
Mats Randgaard4a31a932013-12-10 09:45:00 -0300991 } else {
992 v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
993 __func__, state->selected_input);
Hans Verkuil54450f52012-07-18 05:45:16 -0300994 }
Hans Verkuil54450f52012-07-18 05:45:16 -0300995
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300996 cp_write(sd, 0x8f, (ch1_fr_ll >> 8) & 0x7);
997 cp_write(sd, 0x90, ch1_fr_ll & 0xff);
998 cp_write(sd, 0xab, (height >> 4) & 0xff);
999 cp_write(sd, 0xac, (height & 0x0f) << 4);
1000}
Hans Verkuil54450f52012-07-18 05:45:16 -03001001
Mats Randgaard5c6c6342013-12-05 10:39:04 -03001002static void adv7604_set_offset(struct v4l2_subdev *sd, bool auto_offset, u16 offset_a, u16 offset_b, u16 offset_c)
1003{
1004 struct adv7604_state *state = to_state(sd);
1005 u8 offset_buf[4];
1006
1007 if (auto_offset) {
1008 offset_a = 0x3ff;
1009 offset_b = 0x3ff;
1010 offset_c = 0x3ff;
1011 }
1012
1013 v4l2_dbg(2, debug, sd, "%s: %s offset: a = 0x%x, b = 0x%x, c = 0x%x\n",
1014 __func__, auto_offset ? "Auto" : "Manual",
1015 offset_a, offset_b, offset_c);
1016
1017 offset_buf[0] = (cp_read(sd, 0x77) & 0xc0) | ((offset_a & 0x3f0) >> 4);
1018 offset_buf[1] = ((offset_a & 0x00f) << 4) | ((offset_b & 0x3c0) >> 6);
1019 offset_buf[2] = ((offset_b & 0x03f) << 2) | ((offset_c & 0x300) >> 8);
1020 offset_buf[3] = offset_c & 0x0ff;
1021
1022 /* Registers must be written in this order with no i2c access in between */
1023 if (adv_smbus_write_i2c_block_data(state->i2c_cp, 0x77, 4, offset_buf))
1024 v4l2_err(sd, "%s: i2c error writing to CP reg 0x77, 0x78, 0x79, 0x7a\n", __func__);
1025}
1026
1027static void adv7604_set_gain(struct v4l2_subdev *sd, bool auto_gain, u16 gain_a, u16 gain_b, u16 gain_c)
1028{
1029 struct adv7604_state *state = to_state(sd);
1030 u8 gain_buf[4];
1031 u8 gain_man = 1;
1032 u8 agc_mode_man = 1;
1033
1034 if (auto_gain) {
1035 gain_man = 0;
1036 agc_mode_man = 0;
1037 gain_a = 0x100;
1038 gain_b = 0x100;
1039 gain_c = 0x100;
1040 }
1041
1042 v4l2_dbg(2, debug, sd, "%s: %s gain: a = 0x%x, b = 0x%x, c = 0x%x\n",
1043 __func__, auto_gain ? "Auto" : "Manual",
1044 gain_a, gain_b, gain_c);
1045
1046 gain_buf[0] = ((gain_man << 7) | (agc_mode_man << 6) | ((gain_a & 0x3f0) >> 4));
1047 gain_buf[1] = (((gain_a & 0x00f) << 4) | ((gain_b & 0x3c0) >> 6));
1048 gain_buf[2] = (((gain_b & 0x03f) << 2) | ((gain_c & 0x300) >> 8));
1049 gain_buf[3] = ((gain_c & 0x0ff));
1050
1051 /* Registers must be written in this order with no i2c access in between */
1052 if (adv_smbus_write_i2c_block_data(state->i2c_cp, 0x73, 4, gain_buf))
1053 v4l2_err(sd, "%s: i2c error writing to CP reg 0x73, 0x74, 0x75, 0x76\n", __func__);
1054}
1055
Hans Verkuil54450f52012-07-18 05:45:16 -03001056static void set_rgb_quantization_range(struct v4l2_subdev *sd)
1057{
1058 struct adv7604_state *state = to_state(sd);
Mats Randgaard5c6c6342013-12-05 10:39:04 -03001059 bool rgb_output = io_read(sd, 0x02) & 0x02;
1060 bool hdmi_signal = hdmi_read(sd, 0x05) & 0x80;
Hans Verkuil54450f52012-07-18 05:45:16 -03001061
Mats Randgaard5c6c6342013-12-05 10:39:04 -03001062 v4l2_dbg(2, debug, sd, "%s: RGB quantization range: %d, RGB out: %d, HDMI: %d\n",
1063 __func__, state->rgb_quantization_range,
1064 rgb_output, hdmi_signal);
1065
1066 adv7604_set_gain(sd, true, 0x0, 0x0, 0x0);
1067 adv7604_set_offset(sd, true, 0x0, 0x0, 0x0);
Mats Randgaard98332392013-12-05 10:05:58 -03001068
Hans Verkuil54450f52012-07-18 05:45:16 -03001069 switch (state->rgb_quantization_range) {
1070 case V4L2_DV_RGB_RANGE_AUTO:
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03001071 if (state->selected_input == ADV7604_PAD_VGA_RGB) {
Mats Randgaard98332392013-12-05 10:05:58 -03001072 /* Receiving analog RGB signal
1073 * Set RGB full range (0-255) */
1074 io_write_and_or(sd, 0x02, 0x0f, 0x10);
1075 break;
1076 }
Hans Verkuil54450f52012-07-18 05:45:16 -03001077
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03001078 if (state->selected_input == ADV7604_PAD_VGA_COMP) {
Mats Randgaard98332392013-12-05 10:05:58 -03001079 /* Receiving analog YPbPr signal
1080 * Set automode */
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001081 io_write_and_or(sd, 0x02, 0x0f, 0xf0);
Mats Randgaard98332392013-12-05 10:05:58 -03001082 break;
1083 }
1084
Mats Randgaard5c6c6342013-12-05 10:39:04 -03001085 if (hdmi_signal) {
Mats Randgaard98332392013-12-05 10:05:58 -03001086 /* Receiving HDMI signal
1087 * Set automode */
1088 io_write_and_or(sd, 0x02, 0x0f, 0xf0);
1089 break;
1090 }
1091
1092 /* Receiving DVI-D signal
1093 * ADV7604 selects RGB limited range regardless of
1094 * input format (CE/IT) in automatic mode */
1095 if (state->timings.bt.standards & V4L2_DV_BT_STD_CEA861) {
1096 /* RGB limited range (16-235) */
1097 io_write_and_or(sd, 0x02, 0x0f, 0x00);
1098 } else {
1099 /* RGB full range (0-255) */
1100 io_write_and_or(sd, 0x02, 0x0f, 0x10);
Mats Randgaard5c6c6342013-12-05 10:39:04 -03001101
1102 if (is_digital_input(sd) && rgb_output) {
1103 adv7604_set_offset(sd, false, 0x40, 0x40, 0x40);
1104 } else {
1105 adv7604_set_gain(sd, false, 0xe0, 0xe0, 0xe0);
1106 adv7604_set_offset(sd, false, 0x70, 0x70, 0x70);
1107 }
Hans Verkuil54450f52012-07-18 05:45:16 -03001108 }
1109 break;
1110 case V4L2_DV_RGB_RANGE_LIMITED:
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03001111 if (state->selected_input == ADV7604_PAD_VGA_COMP) {
Mats Randgaardd261e842013-12-05 10:17:15 -03001112 /* YCrCb limited range (16-235) */
1113 io_write_and_or(sd, 0x02, 0x0f, 0x20);
Mats Randgaard5c6c6342013-12-05 10:39:04 -03001114 break;
Mats Randgaardd261e842013-12-05 10:17:15 -03001115 }
Mats Randgaard5c6c6342013-12-05 10:39:04 -03001116
1117 /* RGB limited range (16-235) */
1118 io_write_and_or(sd, 0x02, 0x0f, 0x00);
1119
Hans Verkuil54450f52012-07-18 05:45:16 -03001120 break;
1121 case V4L2_DV_RGB_RANGE_FULL:
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03001122 if (state->selected_input == ADV7604_PAD_VGA_COMP) {
Mats Randgaardd261e842013-12-05 10:17:15 -03001123 /* YCrCb full range (0-255) */
1124 io_write_and_or(sd, 0x02, 0x0f, 0x60);
Mats Randgaard5c6c6342013-12-05 10:39:04 -03001125 break;
1126 }
1127
1128 /* RGB full range (0-255) */
1129 io_write_and_or(sd, 0x02, 0x0f, 0x10);
1130
1131 if (is_analog_input(sd) || hdmi_signal)
1132 break;
1133
1134 /* Adjust gain/offset for DVI-D signals only */
1135 if (rgb_output) {
1136 adv7604_set_offset(sd, false, 0x40, 0x40, 0x40);
Mats Randgaardd261e842013-12-05 10:17:15 -03001137 } else {
Mats Randgaard5c6c6342013-12-05 10:39:04 -03001138 adv7604_set_gain(sd, false, 0xe0, 0xe0, 0xe0);
1139 adv7604_set_offset(sd, false, 0x70, 0x70, 0x70);
Mats Randgaardd261e842013-12-05 10:17:15 -03001140 }
Hans Verkuil54450f52012-07-18 05:45:16 -03001141 break;
1142 }
1143}
1144
Hans Verkuil54450f52012-07-18 05:45:16 -03001145static int adv7604_s_ctrl(struct v4l2_ctrl *ctrl)
1146{
1147 struct v4l2_subdev *sd = to_sd(ctrl);
1148 struct adv7604_state *state = to_state(sd);
1149
1150 switch (ctrl->id) {
1151 case V4L2_CID_BRIGHTNESS:
1152 cp_write(sd, 0x3c, ctrl->val);
1153 return 0;
1154 case V4L2_CID_CONTRAST:
1155 cp_write(sd, 0x3a, ctrl->val);
1156 return 0;
1157 case V4L2_CID_SATURATION:
1158 cp_write(sd, 0x3b, ctrl->val);
1159 return 0;
1160 case V4L2_CID_HUE:
1161 cp_write(sd, 0x3d, ctrl->val);
1162 return 0;
1163 case V4L2_CID_DV_RX_RGB_RANGE:
1164 state->rgb_quantization_range = ctrl->val;
1165 set_rgb_quantization_range(sd);
1166 return 0;
1167 case V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE:
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001168 if (!adv7604_has_afe(state))
1169 return -EINVAL;
Hans Verkuil54450f52012-07-18 05:45:16 -03001170 /* Set the analog sampling phase. This is needed to find the
1171 best sampling phase for analog video: an application or
1172 driver has to try a number of phases and analyze the picture
1173 quality before settling on the best performing phase. */
1174 afe_write(sd, 0xc8, ctrl->val);
1175 return 0;
1176 case V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL:
1177 /* Use the default blue color for free running mode,
1178 or supply your own. */
1179 cp_write_and_or(sd, 0xbf, ~0x04, (ctrl->val << 2));
1180 return 0;
1181 case V4L2_CID_ADV_RX_FREE_RUN_COLOR:
1182 cp_write(sd, 0xc0, (ctrl->val & 0xff0000) >> 16);
1183 cp_write(sd, 0xc1, (ctrl->val & 0x00ff00) >> 8);
1184 cp_write(sd, 0xc2, (u8)(ctrl->val & 0x0000ff));
1185 return 0;
1186 }
1187 return -EINVAL;
1188}
1189
Hans Verkuil54450f52012-07-18 05:45:16 -03001190/* ----------------------------------------------------------------------- */
1191
1192static inline bool no_power(struct v4l2_subdev *sd)
1193{
1194 /* Entire chip or CP powered off */
1195 return io_read(sd, 0x0c) & 0x24;
1196}
1197
1198static inline bool no_signal_tmds(struct v4l2_subdev *sd)
1199{
Mats Randgaard4a31a932013-12-10 09:45:00 -03001200 struct adv7604_state *state = to_state(sd);
1201
1202 return !(io_read(sd, 0x6a) & (0x10 >> state->selected_input));
Hans Verkuil54450f52012-07-18 05:45:16 -03001203}
1204
1205static inline bool no_lock_tmds(struct v4l2_subdev *sd)
1206{
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001207 struct adv7604_state *state = to_state(sd);
1208 const struct adv7604_chip_info *info = state->info;
1209
1210 return (io_read(sd, 0x6a) & info->tdms_lock_mask) != info->tdms_lock_mask;
Hans Verkuil54450f52012-07-18 05:45:16 -03001211}
1212
Martin Buggebb88f322013-08-14 08:52:46 -03001213static inline bool is_hdmi(struct v4l2_subdev *sd)
1214{
1215 return hdmi_read(sd, 0x05) & 0x80;
1216}
1217
Hans Verkuil54450f52012-07-18 05:45:16 -03001218static inline bool no_lock_sspd(struct v4l2_subdev *sd)
1219{
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001220 struct adv7604_state *state = to_state(sd);
1221
1222 /*
1223 * Chips without a AFE don't expose registers for the SSPD, so just assume
1224 * that we have a lock.
1225 */
1226 if (adv7604_has_afe(state))
1227 return false;
1228
Hans Verkuil54450f52012-07-18 05:45:16 -03001229 /* TODO channel 2 */
1230 return ((cp_read(sd, 0xb5) & 0xd0) != 0xd0);
1231}
1232
1233static inline bool no_lock_stdi(struct v4l2_subdev *sd)
1234{
1235 /* TODO channel 2 */
1236 return !(cp_read(sd, 0xb1) & 0x80);
1237}
1238
1239static inline bool no_signal(struct v4l2_subdev *sd)
1240{
Hans Verkuil54450f52012-07-18 05:45:16 -03001241 bool ret;
1242
1243 ret = no_power(sd);
1244
1245 ret |= no_lock_stdi(sd);
1246 ret |= no_lock_sspd(sd);
1247
Mats Randgaard4a31a932013-12-10 09:45:00 -03001248 if (is_digital_input(sd)) {
Hans Verkuil54450f52012-07-18 05:45:16 -03001249 ret |= no_lock_tmds(sd);
1250 ret |= no_signal_tmds(sd);
1251 }
1252
1253 return ret;
1254}
1255
1256static inline bool no_lock_cp(struct v4l2_subdev *sd)
1257{
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001258 struct adv7604_state *state = to_state(sd);
1259
1260 if (!adv7604_has_afe(state))
1261 return false;
1262
Hans Verkuil54450f52012-07-18 05:45:16 -03001263 /* CP has detected a non standard number of lines on the incoming
1264 video compared to what it is configured to receive by s_dv_timings */
1265 return io_read(sd, 0x12) & 0x01;
1266}
1267
1268static int adv7604_g_input_status(struct v4l2_subdev *sd, u32 *status)
1269{
Hans Verkuil54450f52012-07-18 05:45:16 -03001270 *status = 0;
1271 *status |= no_power(sd) ? V4L2_IN_ST_NO_POWER : 0;
1272 *status |= no_signal(sd) ? V4L2_IN_ST_NO_SIGNAL : 0;
1273 if (no_lock_cp(sd))
Mats Randgaard4a31a932013-12-10 09:45:00 -03001274 *status |= is_digital_input(sd) ? V4L2_IN_ST_NO_SYNC : V4L2_IN_ST_NO_H_LOCK;
Hans Verkuil54450f52012-07-18 05:45:16 -03001275
1276 v4l2_dbg(1, debug, sd, "%s: status = 0x%x\n", __func__, *status);
1277
1278 return 0;
1279}
1280
1281/* ----------------------------------------------------------------------- */
1282
Hans Verkuil54450f52012-07-18 05:45:16 -03001283struct stdi_readback {
1284 u16 bl, lcf, lcvs;
1285 u8 hs_pol, vs_pol;
1286 bool interlaced;
1287};
1288
1289static int stdi2dv_timings(struct v4l2_subdev *sd,
1290 struct stdi_readback *stdi,
1291 struct v4l2_dv_timings *timings)
1292{
1293 struct adv7604_state *state = to_state(sd);
1294 u32 hfreq = (ADV7604_fsc * 8) / stdi->bl;
1295 u32 pix_clk;
1296 int i;
1297
1298 for (i = 0; adv7604_timings[i].bt.height; i++) {
1299 if (vtotal(&adv7604_timings[i].bt) != stdi->lcf + 1)
1300 continue;
1301 if (adv7604_timings[i].bt.vsync != stdi->lcvs)
1302 continue;
1303
1304 pix_clk = hfreq * htotal(&adv7604_timings[i].bt);
1305
1306 if ((pix_clk < adv7604_timings[i].bt.pixelclock + 1000000) &&
1307 (pix_clk > adv7604_timings[i].bt.pixelclock - 1000000)) {
1308 *timings = adv7604_timings[i];
1309 return 0;
1310 }
1311 }
1312
1313 if (v4l2_detect_cvt(stdi->lcf + 1, hfreq, stdi->lcvs,
1314 (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
1315 (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
1316 timings))
1317 return 0;
1318 if (v4l2_detect_gtf(stdi->lcf + 1, hfreq, stdi->lcvs,
1319 (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
1320 (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
1321 state->aspect_ratio, timings))
1322 return 0;
1323
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001324 v4l2_dbg(2, debug, sd,
1325 "%s: No format candidate found for lcvs = %d, lcf=%d, bl = %d, %chsync, %cvsync\n",
1326 __func__, stdi->lcvs, stdi->lcf, stdi->bl,
1327 stdi->hs_pol, stdi->vs_pol);
Hans Verkuil54450f52012-07-18 05:45:16 -03001328 return -1;
1329}
1330
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001331
Hans Verkuil54450f52012-07-18 05:45:16 -03001332static int read_stdi(struct v4l2_subdev *sd, struct stdi_readback *stdi)
1333{
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001334 struct adv7604_state *state = to_state(sd);
1335 const struct adv7604_chip_info *info = state->info;
Laurent Pinchart4a2ccdd2014-01-08 20:26:55 -03001336 u8 polarity;
1337
Hans Verkuil54450f52012-07-18 05:45:16 -03001338 if (no_lock_stdi(sd) || no_lock_sspd(sd)) {
1339 v4l2_dbg(2, debug, sd, "%s: STDI and/or SSPD not locked\n", __func__);
1340 return -1;
1341 }
1342
1343 /* read STDI */
Laurent Pinchart51182a92014-01-08 19:30:37 -03001344 stdi->bl = cp_read16(sd, 0xb1, 0x3fff);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001345 stdi->lcf = cp_read16(sd, info->lcf_reg, 0x7ff);
Hans Verkuil54450f52012-07-18 05:45:16 -03001346 stdi->lcvs = cp_read(sd, 0xb3) >> 3;
1347 stdi->interlaced = io_read(sd, 0x12) & 0x10;
1348
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001349 if (adv7604_has_afe(state)) {
1350 /* read SSPD */
1351 polarity = cp_read(sd, 0xb5);
1352 if ((polarity & 0x03) == 0x01) {
1353 stdi->hs_pol = polarity & 0x10
1354 ? (polarity & 0x08 ? '+' : '-') : 'x';
1355 stdi->vs_pol = polarity & 0x40
1356 ? (polarity & 0x20 ? '+' : '-') : 'x';
1357 } else {
1358 stdi->hs_pol = 'x';
1359 stdi->vs_pol = 'x';
1360 }
Hans Verkuil54450f52012-07-18 05:45:16 -03001361 } else {
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001362 polarity = hdmi_read(sd, 0x05);
1363 stdi->hs_pol = polarity & 0x20 ? '+' : '-';
1364 stdi->vs_pol = polarity & 0x10 ? '+' : '-';
Hans Verkuil54450f52012-07-18 05:45:16 -03001365 }
1366
1367 if (no_lock_stdi(sd) || no_lock_sspd(sd)) {
1368 v4l2_dbg(2, debug, sd,
1369 "%s: signal lost during readout of STDI/SSPD\n", __func__);
1370 return -1;
1371 }
1372
1373 if (stdi->lcf < 239 || stdi->bl < 8 || stdi->bl == 0x3fff) {
1374 v4l2_dbg(2, debug, sd, "%s: invalid signal\n", __func__);
1375 memset(stdi, 0, sizeof(struct stdi_readback));
1376 return -1;
1377 }
1378
1379 v4l2_dbg(2, debug, sd,
1380 "%s: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, %chsync, %cvsync, %s\n",
1381 __func__, stdi->lcf, stdi->bl, stdi->lcvs,
1382 stdi->hs_pol, stdi->vs_pol,
1383 stdi->interlaced ? "interlaced" : "progressive");
1384
1385 return 0;
1386}
1387
1388static int adv7604_enum_dv_timings(struct v4l2_subdev *sd,
1389 struct v4l2_enum_dv_timings *timings)
1390{
1391 if (timings->index >= ARRAY_SIZE(adv7604_timings) - 1)
1392 return -EINVAL;
1393 memset(timings->reserved, 0, sizeof(timings->reserved));
1394 timings->timings = adv7604_timings[timings->index];
1395 return 0;
1396}
1397
1398static int adv7604_dv_timings_cap(struct v4l2_subdev *sd,
1399 struct v4l2_dv_timings_cap *cap)
1400{
Hans Verkuil54450f52012-07-18 05:45:16 -03001401 cap->type = V4L2_DV_BT_656_1120;
1402 cap->bt.max_width = 1920;
1403 cap->bt.max_height = 1200;
Hans Verkuilfe9c2562013-08-19 08:07:26 -03001404 cap->bt.min_pixelclock = 25000000;
Mats Randgaard4a31a932013-12-10 09:45:00 -03001405 if (is_digital_input(sd))
Hans Verkuil54450f52012-07-18 05:45:16 -03001406 cap->bt.max_pixelclock = 225000000;
1407 else
1408 cap->bt.max_pixelclock = 170000000;
1409 cap->bt.standards = V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
1410 V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT;
1411 cap->bt.capabilities = V4L2_DV_BT_CAP_PROGRESSIVE |
1412 V4L2_DV_BT_CAP_REDUCED_BLANKING | V4L2_DV_BT_CAP_CUSTOM;
1413 return 0;
1414}
1415
1416/* Fill the optional fields .standards and .flags in struct v4l2_dv_timings
1417 if the format is listed in adv7604_timings[] */
1418static void adv7604_fill_optional_dv_timings_fields(struct v4l2_subdev *sd,
1419 struct v4l2_dv_timings *timings)
1420{
Hans Verkuil54450f52012-07-18 05:45:16 -03001421 int i;
1422
1423 for (i = 0; adv7604_timings[i].bt.width; i++) {
Hans Verkuilef1ed8f2013-08-15 08:28:47 -03001424 if (v4l2_match_dv_timings(timings, &adv7604_timings[i],
Mats Randgaard4a31a932013-12-10 09:45:00 -03001425 is_digital_input(sd) ? 250000 : 1000000)) {
Hans Verkuil54450f52012-07-18 05:45:16 -03001426 *timings = adv7604_timings[i];
1427 break;
1428 }
1429 }
1430}
1431
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001432static unsigned int adv7604_read_hdmi_pixelclock(struct v4l2_subdev *sd)
1433{
1434 unsigned int freq;
1435 int a, b;
1436
1437 a = hdmi_read(sd, 0x06);
1438 b = hdmi_read(sd, 0x3b);
1439 if (a < 0 || b < 0)
1440 return 0;
1441 freq = a * 1000000 + ((b & 0x30) >> 4) * 250000;
1442
1443 if (is_hdmi(sd)) {
1444 /* adjust for deep color mode */
1445 unsigned bits_per_channel = ((hdmi_read(sd, 0x0b) & 0x60) >> 4) + 8;
1446
1447 freq = freq * 8 / bits_per_channel;
1448 }
1449
1450 return freq;
1451}
1452
1453static unsigned int adv7611_read_hdmi_pixelclock(struct v4l2_subdev *sd)
1454{
1455 int a, b;
1456
1457 a = hdmi_read(sd, 0x51);
1458 b = hdmi_read(sd, 0x52);
1459 if (a < 0 || b < 0)
1460 return 0;
1461 return ((a << 1) | (b >> 7)) * 1000000 + (b & 0x7f) * 1000000 / 128;
1462}
1463
Hans Verkuil54450f52012-07-18 05:45:16 -03001464static int adv7604_query_dv_timings(struct v4l2_subdev *sd,
1465 struct v4l2_dv_timings *timings)
1466{
1467 struct adv7604_state *state = to_state(sd);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001468 const struct adv7604_chip_info *info = state->info;
Hans Verkuil54450f52012-07-18 05:45:16 -03001469 struct v4l2_bt_timings *bt = &timings->bt;
1470 struct stdi_readback stdi;
1471
1472 if (!timings)
1473 return -EINVAL;
1474
1475 memset(timings, 0, sizeof(struct v4l2_dv_timings));
1476
1477 if (no_signal(sd)) {
Martin Bugge1e0b9152013-12-05 10:34:46 -03001478 state->restart_stdi_once = true;
Hans Verkuil54450f52012-07-18 05:45:16 -03001479 v4l2_dbg(1, debug, sd, "%s: no valid signal\n", __func__);
1480 return -ENOLINK;
1481 }
1482
1483 /* read STDI */
1484 if (read_stdi(sd, &stdi)) {
1485 v4l2_dbg(1, debug, sd, "%s: STDI/SSPD not locked\n", __func__);
1486 return -ENOLINK;
1487 }
1488 bt->interlaced = stdi.interlaced ?
1489 V4L2_DV_INTERLACED : V4L2_DV_PROGRESSIVE;
1490
Mats Randgaard4a31a932013-12-10 09:45:00 -03001491 if (is_digital_input(sd)) {
Hans Verkuil54450f52012-07-18 05:45:16 -03001492 timings->type = V4L2_DV_BT_656_1120;
1493
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001494 /* FIXME: All masks are incorrect for ADV7611 */
Laurent Pinchart51182a92014-01-08 19:30:37 -03001495 bt->width = hdmi_read16(sd, 0x07, 0xfff);
1496 bt->height = hdmi_read16(sd, 0x09, 0xfff);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001497 bt->pixelclock = info->read_hdmi_pixelclock(sd);
Laurent Pinchart51182a92014-01-08 19:30:37 -03001498 bt->hfrontporch = hdmi_read16(sd, 0x20, 0x3ff);
1499 bt->hsync = hdmi_read16(sd, 0x22, 0x3ff);
1500 bt->hbackporch = hdmi_read16(sd, 0x24, 0x3ff);
1501 bt->vfrontporch = hdmi_read16(sd, 0x2a, 0x1fff) / 2;
1502 bt->vsync = hdmi_read16(sd, 0x2e, 0x1fff) / 2;
1503 bt->vbackporch = hdmi_read16(sd, 0x32, 0x1fff) / 2;
Hans Verkuil54450f52012-07-18 05:45:16 -03001504 bt->polarities = ((hdmi_read(sd, 0x05) & 0x10) ? V4L2_DV_VSYNC_POS_POL : 0) |
1505 ((hdmi_read(sd, 0x05) & 0x20) ? V4L2_DV_HSYNC_POS_POL : 0);
1506 if (bt->interlaced == V4L2_DV_INTERLACED) {
Laurent Pinchart51182a92014-01-08 19:30:37 -03001507 bt->height += hdmi_read16(sd, 0x0b, 0xfff);
1508 bt->il_vfrontporch = hdmi_read16(sd, 0x2c, 0x1fff) / 2;
1509 bt->il_vsync = hdmi_read16(sd, 0x30, 0x1fff) / 2;
1510 bt->vbackporch = hdmi_read16(sd, 0x34, 0x1fff) / 2;
Hans Verkuil54450f52012-07-18 05:45:16 -03001511 }
1512 adv7604_fill_optional_dv_timings_fields(sd, timings);
1513 } else {
1514 /* find format
Hans Verkuil80939642012-10-16 05:46:21 -03001515 * Since LCVS values are inaccurate [REF_03, p. 275-276],
Hans Verkuil54450f52012-07-18 05:45:16 -03001516 * stdi2dv_timings() is called with lcvs +-1 if the first attempt fails.
1517 */
1518 if (!stdi2dv_timings(sd, &stdi, timings))
1519 goto found;
1520 stdi.lcvs += 1;
1521 v4l2_dbg(1, debug, sd, "%s: lcvs + 1 = %d\n", __func__, stdi.lcvs);
1522 if (!stdi2dv_timings(sd, &stdi, timings))
1523 goto found;
1524 stdi.lcvs -= 2;
1525 v4l2_dbg(1, debug, sd, "%s: lcvs - 1 = %d\n", __func__, stdi.lcvs);
1526 if (stdi2dv_timings(sd, &stdi, timings)) {
Hans Verkuilcf9afb12012-10-16 10:12:55 -03001527 /*
1528 * The STDI block may measure wrong values, especially
1529 * for lcvs and lcf. If the driver can not find any
1530 * valid timing, the STDI block is restarted to measure
1531 * the video timings again. The function will return an
1532 * error, but the restart of STDI will generate a new
1533 * STDI interrupt and the format detection process will
1534 * restart.
1535 */
1536 if (state->restart_stdi_once) {
1537 v4l2_dbg(1, debug, sd, "%s: restart STDI\n", __func__);
1538 /* TODO restart STDI for Sync Channel 2 */
1539 /* enter one-shot mode */
1540 cp_write_and_or(sd, 0x86, 0xf9, 0x00);
1541 /* trigger STDI restart */
1542 cp_write_and_or(sd, 0x86, 0xf9, 0x04);
1543 /* reset to continuous mode */
1544 cp_write_and_or(sd, 0x86, 0xf9, 0x02);
1545 state->restart_stdi_once = false;
1546 return -ENOLINK;
1547 }
Hans Verkuil54450f52012-07-18 05:45:16 -03001548 v4l2_dbg(1, debug, sd, "%s: format not supported\n", __func__);
1549 return -ERANGE;
1550 }
Hans Verkuilcf9afb12012-10-16 10:12:55 -03001551 state->restart_stdi_once = true;
Hans Verkuil54450f52012-07-18 05:45:16 -03001552 }
1553found:
1554
1555 if (no_signal(sd)) {
1556 v4l2_dbg(1, debug, sd, "%s: signal lost during readout\n", __func__);
1557 memset(timings, 0, sizeof(struct v4l2_dv_timings));
1558 return -ENOLINK;
1559 }
1560
Mats Randgaard4a31a932013-12-10 09:45:00 -03001561 if ((is_analog_input(sd) && bt->pixelclock > 170000000) ||
1562 (is_digital_input(sd) && bt->pixelclock > 225000000)) {
Hans Verkuil54450f52012-07-18 05:45:16 -03001563 v4l2_dbg(1, debug, sd, "%s: pixelclock out of range %d\n",
1564 __func__, (u32)bt->pixelclock);
1565 return -ERANGE;
1566 }
1567
1568 if (debug > 1)
Hans Verkuil11d034c2013-08-15 08:05:59 -03001569 v4l2_print_dv_timings(sd->name, "adv7604_query_dv_timings: ",
1570 timings, true);
Hans Verkuil54450f52012-07-18 05:45:16 -03001571
1572 return 0;
1573}
1574
1575static int adv7604_s_dv_timings(struct v4l2_subdev *sd,
1576 struct v4l2_dv_timings *timings)
1577{
1578 struct adv7604_state *state = to_state(sd);
1579 struct v4l2_bt_timings *bt;
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001580 int err;
Hans Verkuil54450f52012-07-18 05:45:16 -03001581
1582 if (!timings)
1583 return -EINVAL;
1584
Mats Randgaardd48eb482013-12-12 10:13:35 -03001585 if (v4l2_match_dv_timings(&state->timings, timings, 0)) {
1586 v4l2_dbg(1, debug, sd, "%s: no change\n", __func__);
1587 return 0;
1588 }
1589
Hans Verkuil54450f52012-07-18 05:45:16 -03001590 bt = &timings->bt;
1591
Mats Randgaard4a31a932013-12-10 09:45:00 -03001592 if ((is_analog_input(sd) && bt->pixelclock > 170000000) ||
1593 (is_digital_input(sd) && bt->pixelclock > 225000000)) {
Hans Verkuil54450f52012-07-18 05:45:16 -03001594 v4l2_dbg(1, debug, sd, "%s: pixelclock out of range %d\n",
1595 __func__, (u32)bt->pixelclock);
1596 return -ERANGE;
1597 }
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001598
Hans Verkuil54450f52012-07-18 05:45:16 -03001599 adv7604_fill_optional_dv_timings_fields(sd, timings);
1600
1601 state->timings = *timings;
1602
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001603 cp_write_and_or(sd, 0x91, 0xbf, bt->interlaced ? 0x40 : 0x00);
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001604
1605 /* Use prim_mode and vid_std when available */
1606 err = configure_predefined_video_timings(sd, timings);
1607 if (err) {
1608 /* custom settings when the video format
1609 does not have prim_mode/vid_std */
1610 configure_custom_video_timings(sd, bt);
1611 }
Hans Verkuil54450f52012-07-18 05:45:16 -03001612
1613 set_rgb_quantization_range(sd);
1614
Hans Verkuil54450f52012-07-18 05:45:16 -03001615 if (debug > 1)
Hans Verkuil11d034c2013-08-15 08:05:59 -03001616 v4l2_print_dv_timings(sd->name, "adv7604_s_dv_timings: ",
1617 timings, true);
Hans Verkuil54450f52012-07-18 05:45:16 -03001618 return 0;
1619}
1620
1621static int adv7604_g_dv_timings(struct v4l2_subdev *sd,
1622 struct v4l2_dv_timings *timings)
1623{
1624 struct adv7604_state *state = to_state(sd);
1625
1626 *timings = state->timings;
1627 return 0;
1628}
1629
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001630static void adv7604_set_termination(struct v4l2_subdev *sd, bool enable)
1631{
1632 hdmi_write(sd, 0x01, enable ? 0x00 : 0x78);
1633}
1634
1635static void adv7611_set_termination(struct v4l2_subdev *sd, bool enable)
1636{
1637 hdmi_write(sd, 0x83, enable ? 0xfe : 0xff);
1638}
1639
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001640static void enable_input(struct v4l2_subdev *sd)
Hans Verkuil54450f52012-07-18 05:45:16 -03001641{
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001642 struct adv7604_state *state = to_state(sd);
1643
Mats Randgaard4a31a932013-12-10 09:45:00 -03001644 if (is_analog_input(sd)) {
Hans Verkuil54450f52012-07-18 05:45:16 -03001645 io_write(sd, 0x15, 0xb0); /* Disable Tristate of Pins (no audio) */
Mats Randgaard4a31a932013-12-10 09:45:00 -03001646 } else if (is_digital_input(sd)) {
Mats Randgaard4a31a932013-12-10 09:45:00 -03001647 hdmi_write_and_or(sd, 0x00, 0xfc, state->selected_input);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001648 state->info->set_termination(sd, true);
Hans Verkuil54450f52012-07-18 05:45:16 -03001649 io_write(sd, 0x15, 0xa0); /* Disable Tristate of Pins */
Mats Randgaard5474b982013-12-05 10:33:41 -03001650 hdmi_write_and_or(sd, 0x1a, 0xef, 0x00); /* Unmute audio */
Mats Randgaard4a31a932013-12-10 09:45:00 -03001651 } else {
1652 v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
1653 __func__, state->selected_input);
Hans Verkuil54450f52012-07-18 05:45:16 -03001654 }
1655}
1656
1657static void disable_input(struct v4l2_subdev *sd)
1658{
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001659 struct adv7604_state *state = to_state(sd);
1660
Mats Randgaard5474b982013-12-05 10:33:41 -03001661 hdmi_write_and_or(sd, 0x1a, 0xef, 0x10); /* Mute audio */
1662 msleep(16); /* 512 samples with >= 32 kHz sample rate [REF_03, c. 7.16.10] */
Hans Verkuil54450f52012-07-18 05:45:16 -03001663 io_write(sd, 0x15, 0xbe); /* Tristate all outputs from video core */
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001664 state->info->set_termination(sd, false);
Hans Verkuil54450f52012-07-18 05:45:16 -03001665}
1666
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001667static void select_input(struct v4l2_subdev *sd)
Hans Verkuil54450f52012-07-18 05:45:16 -03001668{
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001669 struct adv7604_state *state = to_state(sd);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001670 const struct adv7604_chip_info *info = state->info;
Hans Verkuil54450f52012-07-18 05:45:16 -03001671
Mats Randgaard4a31a932013-12-10 09:45:00 -03001672 if (is_analog_input(sd)) {
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001673 adv7604_write_reg_seq(sd, info->recommended_settings[0]);
Hans Verkuil54450f52012-07-18 05:45:16 -03001674
1675 afe_write(sd, 0x00, 0x08); /* power up ADC */
1676 afe_write(sd, 0x01, 0x06); /* power up Analog Front End */
1677 afe_write(sd, 0xc8, 0x00); /* phase control */
Mats Randgaard4a31a932013-12-10 09:45:00 -03001678 } else if (is_digital_input(sd)) {
1679 hdmi_write(sd, 0x00, state->selected_input & 0x03);
Hans Verkuil54450f52012-07-18 05:45:16 -03001680
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001681 adv7604_write_reg_seq(sd, info->recommended_settings[1]);
Hans Verkuil54450f52012-07-18 05:45:16 -03001682
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001683 if (adv7604_has_afe(state)) {
1684 afe_write(sd, 0x00, 0xff); /* power down ADC */
1685 afe_write(sd, 0x01, 0xfe); /* power down Analog Front End */
1686 afe_write(sd, 0xc8, 0x40); /* phase control */
1687 }
Hans Verkuil54450f52012-07-18 05:45:16 -03001688
Hans Verkuil54450f52012-07-18 05:45:16 -03001689 cp_write(sd, 0x3e, 0x00); /* CP core pre-gain control */
1690 cp_write(sd, 0xc3, 0x39); /* CP coast control. Graphics mode */
1691 cp_write(sd, 0x40, 0x80); /* CP core pre-gain control. Graphics mode */
Mats Randgaard4a31a932013-12-10 09:45:00 -03001692 } else {
1693 v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
1694 __func__, state->selected_input);
Hans Verkuil54450f52012-07-18 05:45:16 -03001695 }
1696}
1697
1698static int adv7604_s_routing(struct v4l2_subdev *sd,
1699 u32 input, u32 output, u32 config)
1700{
1701 struct adv7604_state *state = to_state(sd);
1702
Mats Randgaardff4f80f2013-12-05 10:24:05 -03001703 v4l2_dbg(2, debug, sd, "%s: input %d, selected input %d",
1704 __func__, input, state->selected_input);
1705
1706 if (input == state->selected_input)
1707 return 0;
Hans Verkuil54450f52012-07-18 05:45:16 -03001708
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001709 if (input > state->info->max_port)
1710 return -EINVAL;
1711
Mats Randgaard4a31a932013-12-10 09:45:00 -03001712 state->selected_input = input;
Hans Verkuil54450f52012-07-18 05:45:16 -03001713
1714 disable_input(sd);
1715
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001716 select_input(sd);
Hans Verkuil54450f52012-07-18 05:45:16 -03001717
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001718 enable_input(sd);
Hans Verkuil54450f52012-07-18 05:45:16 -03001719
1720 return 0;
1721}
1722
1723static int adv7604_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned int index,
1724 enum v4l2_mbus_pixelcode *code)
1725{
1726 if (index)
1727 return -EINVAL;
1728 /* Good enough for now */
1729 *code = V4L2_MBUS_FMT_FIXED;
1730 return 0;
1731}
1732
1733static int adv7604_g_mbus_fmt(struct v4l2_subdev *sd,
1734 struct v4l2_mbus_framefmt *fmt)
1735{
1736 struct adv7604_state *state = to_state(sd);
1737
1738 fmt->width = state->timings.bt.width;
1739 fmt->height = state->timings.bt.height;
1740 fmt->code = V4L2_MBUS_FMT_FIXED;
1741 fmt->field = V4L2_FIELD_NONE;
1742 if (state->timings.bt.standards & V4L2_DV_BT_STD_CEA861) {
1743 fmt->colorspace = (state->timings.bt.height <= 576) ?
1744 V4L2_COLORSPACE_SMPTE170M : V4L2_COLORSPACE_REC709;
1745 }
1746 return 0;
1747}
1748
1749static int adv7604_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
1750{
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001751 struct adv7604_state *state = to_state(sd);
1752 const struct adv7604_chip_info *info = state->info;
Mats Randgaardf24d2292013-12-10 10:15:13 -03001753 const u8 irq_reg_0x43 = io_read(sd, 0x43);
1754 const u8 irq_reg_0x6b = io_read(sd, 0x6b);
1755 const u8 irq_reg_0x70 = io_read(sd, 0x70);
1756 u8 fmt_change_digital;
1757 u8 fmt_change;
1758 u8 tx_5v;
1759
1760 if (irq_reg_0x43)
1761 io_write(sd, 0x44, irq_reg_0x43);
1762 if (irq_reg_0x70)
1763 io_write(sd, 0x71, irq_reg_0x70);
1764 if (irq_reg_0x6b)
1765 io_write(sd, 0x6c, irq_reg_0x6b);
Hans Verkuil54450f52012-07-18 05:45:16 -03001766
Mats Randgaardff4f80f2013-12-05 10:24:05 -03001767 v4l2_dbg(2, debug, sd, "%s: ", __func__);
1768
Hans Verkuil54450f52012-07-18 05:45:16 -03001769 /* format change */
Mats Randgaardf24d2292013-12-10 10:15:13 -03001770 fmt_change = irq_reg_0x43 & 0x98;
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001771 fmt_change_digital = is_digital_input(sd)
1772 ? irq_reg_0x6b & info->fmt_change_digital_mask
1773 : 0;
Mats Randgaard14d03232013-12-05 10:26:11 -03001774
Hans Verkuil54450f52012-07-18 05:45:16 -03001775 if (fmt_change || fmt_change_digital) {
1776 v4l2_dbg(1, debug, sd,
Mats Randgaard25a64ac2013-08-14 07:58:45 -03001777 "%s: fmt_change = 0x%x, fmt_change_digital = 0x%x\n",
Hans Verkuil54450f52012-07-18 05:45:16 -03001778 __func__, fmt_change, fmt_change_digital);
Mats Randgaard25a64ac2013-08-14 07:58:45 -03001779
Mats Randgaard14d03232013-12-05 10:26:11 -03001780 v4l2_subdev_notify(sd, ADV7604_FMT_CHANGE, NULL);
Mats Randgaard25a64ac2013-08-14 07:58:45 -03001781
Hans Verkuil54450f52012-07-18 05:45:16 -03001782 if (handled)
1783 *handled = true;
1784 }
Mats Randgaardf24d2292013-12-10 10:15:13 -03001785 /* HDMI/DVI mode */
1786 if (irq_reg_0x6b & 0x01) {
1787 v4l2_dbg(1, debug, sd, "%s: irq %s mode\n", __func__,
1788 (io_read(sd, 0x6a) & 0x01) ? "HDMI" : "DVI");
1789 set_rgb_quantization_range(sd);
1790 if (handled)
1791 *handled = true;
1792 }
1793
Hans Verkuil54450f52012-07-18 05:45:16 -03001794 /* tx 5v detect */
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001795 tx_5v = io_read(sd, 0x70) & info->cable_det_mask;
Hans Verkuil54450f52012-07-18 05:45:16 -03001796 if (tx_5v) {
1797 v4l2_dbg(1, debug, sd, "%s: tx_5v: 0x%x\n", __func__, tx_5v);
1798 io_write(sd, 0x71, tx_5v);
1799 adv7604_s_detect_tx_5v_ctrl(sd);
1800 if (handled)
1801 *handled = true;
1802 }
1803 return 0;
1804}
1805
Hans Verkuilb09dfac2014-03-04 08:05:19 -03001806static int adv7604_get_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
Hans Verkuil54450f52012-07-18 05:45:16 -03001807{
1808 struct adv7604_state *state = to_state(sd);
Mats Randgaard4a31a932013-12-10 09:45:00 -03001809 u8 *data = NULL;
Hans Verkuil54450f52012-07-18 05:45:16 -03001810
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03001811 if (edid->pad > ADV7604_PAD_HDMI_PORT_D)
Hans Verkuil54450f52012-07-18 05:45:16 -03001812 return -EINVAL;
1813 if (edid->blocks == 0)
1814 return -EINVAL;
Mats Randgaard4a31a932013-12-10 09:45:00 -03001815 if (edid->blocks > 2)
Hans Verkuil54450f52012-07-18 05:45:16 -03001816 return -EINVAL;
Mats Randgaard4a31a932013-12-10 09:45:00 -03001817 if (edid->start_block > 1)
1818 return -EINVAL;
1819 if (edid->start_block == 1)
1820 edid->blocks = 1;
Mats Randgaard4a31a932013-12-10 09:45:00 -03001821
1822 if (edid->blocks > state->edid.blocks)
1823 edid->blocks = state->edid.blocks;
1824
1825 switch (edid->pad) {
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03001826 case ADV7604_PAD_HDMI_PORT_A:
1827 case ADV7604_PAD_HDMI_PORT_B:
1828 case ADV7604_PAD_HDMI_PORT_C:
1829 case ADV7604_PAD_HDMI_PORT_D:
Mats Randgaard4a31a932013-12-10 09:45:00 -03001830 if (state->edid.present & (1 << edid->pad))
1831 data = state->edid.edid;
1832 break;
1833 default:
1834 return -EINVAL;
1835 break;
1836 }
1837 if (!data)
1838 return -ENODATA;
1839
1840 memcpy(edid->edid,
1841 data + edid->start_block * 128,
Hans Verkuil54450f52012-07-18 05:45:16 -03001842 edid->blocks * 128);
1843 return 0;
1844}
1845
Mats Randgaarddd08beb2013-12-10 09:57:09 -03001846static int get_edid_spa_location(const u8 *edid)
Mats Randgaard3e86aa82013-12-10 09:55:18 -03001847{
1848 u8 d;
1849
1850 if ((edid[0x7e] != 1) ||
1851 (edid[0x80] != 0x02) ||
1852 (edid[0x81] != 0x03)) {
1853 return -1;
1854 }
1855
1856 /* search Vendor Specific Data Block (tag 3) */
1857 d = edid[0x82] & 0x7f;
1858 if (d > 4) {
1859 int i = 0x84;
1860 int end = 0x80 + d;
1861
1862 do {
1863 u8 tag = edid[i] >> 5;
1864 u8 len = edid[i] & 0x1f;
1865
1866 if ((tag == 3) && (len >= 5))
1867 return i + 4;
1868 i += len + 1;
1869 } while (i < end);
1870 }
1871 return -1;
1872}
1873
Hans Verkuilb09dfac2014-03-04 08:05:19 -03001874static int adv7604_set_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
Hans Verkuil54450f52012-07-18 05:45:16 -03001875{
1876 struct adv7604_state *state = to_state(sd);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001877 const struct adv7604_chip_info *info = state->info;
Mats Randgaarddd08beb2013-12-10 09:57:09 -03001878 int spa_loc;
Mats Randgaard3e86aa82013-12-10 09:55:18 -03001879 int tmp = 0;
Hans Verkuil54450f52012-07-18 05:45:16 -03001880 int err;
Mats Randgaarddd08beb2013-12-10 09:57:09 -03001881 int i;
Hans Verkuil54450f52012-07-18 05:45:16 -03001882
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03001883 if (edid->pad > ADV7604_PAD_HDMI_PORT_D)
Hans Verkuil54450f52012-07-18 05:45:16 -03001884 return -EINVAL;
1885 if (edid->start_block != 0)
1886 return -EINVAL;
1887 if (edid->blocks == 0) {
Mats Randgaard3e86aa82013-12-10 09:55:18 -03001888 /* Disable hotplug and I2C access to EDID RAM from DDC port */
Mats Randgaard4a31a932013-12-10 09:45:00 -03001889 state->edid.present &= ~(1 << edid->pad);
1890 v4l2_subdev_notify(sd, ADV7604_HOTPLUG, (void *)&state->edid.present);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001891 rep_write_and_or(sd, info->edid_enable_reg, 0xf0, state->edid.present);
Mats Randgaard3e86aa82013-12-10 09:55:18 -03001892
Hans Verkuil54450f52012-07-18 05:45:16 -03001893 /* Fall back to a 16:9 aspect ratio */
1894 state->aspect_ratio.numerator = 16;
1895 state->aspect_ratio.denominator = 9;
Mats Randgaard3e86aa82013-12-10 09:55:18 -03001896
1897 if (!state->edid.present)
1898 state->edid.blocks = 0;
1899
1900 v4l2_dbg(2, debug, sd, "%s: clear EDID pad %d, edid.present = 0x%x\n",
1901 __func__, edid->pad, state->edid.present);
Hans Verkuil54450f52012-07-18 05:45:16 -03001902 return 0;
1903 }
Mats Randgaard4a31a932013-12-10 09:45:00 -03001904 if (edid->blocks > 2) {
1905 edid->blocks = 2;
Hans Verkuil54450f52012-07-18 05:45:16 -03001906 return -E2BIG;
Mats Randgaard4a31a932013-12-10 09:45:00 -03001907 }
Mats Randgaard4a31a932013-12-10 09:45:00 -03001908
Mats Randgaarddd08beb2013-12-10 09:57:09 -03001909 v4l2_dbg(2, debug, sd, "%s: write EDID pad %d, edid.present = 0x%x\n",
1910 __func__, edid->pad, state->edid.present);
1911
Mats Randgaard3e86aa82013-12-10 09:55:18 -03001912 /* Disable hotplug and I2C access to EDID RAM from DDC port */
Mats Randgaard4a31a932013-12-10 09:45:00 -03001913 cancel_delayed_work_sync(&state->delayed_work_enable_hotplug);
Mats Randgaard3e86aa82013-12-10 09:55:18 -03001914 v4l2_subdev_notify(sd, ADV7604_HOTPLUG, (void *)&tmp);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001915 rep_write_and_or(sd, info->edid_enable_reg, 0xf0, 0x00);
Mats Randgaard3e86aa82013-12-10 09:55:18 -03001916
Mats Randgaarddd08beb2013-12-10 09:57:09 -03001917 spa_loc = get_edid_spa_location(edid->edid);
1918 if (spa_loc < 0)
1919 spa_loc = 0xc0; /* Default value [REF_02, p. 116] */
1920
Mats Randgaard3e86aa82013-12-10 09:55:18 -03001921 switch (edid->pad) {
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03001922 case ADV7604_PAD_HDMI_PORT_A:
Mats Randgaarddd08beb2013-12-10 09:57:09 -03001923 state->spa_port_a[0] = edid->edid[spa_loc];
1924 state->spa_port_a[1] = edid->edid[spa_loc + 1];
Mats Randgaard3e86aa82013-12-10 09:55:18 -03001925 break;
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03001926 case ADV7604_PAD_HDMI_PORT_B:
Mats Randgaarddd08beb2013-12-10 09:57:09 -03001927 rep_write(sd, 0x70, edid->edid[spa_loc]);
1928 rep_write(sd, 0x71, edid->edid[spa_loc + 1]);
Mats Randgaard3e86aa82013-12-10 09:55:18 -03001929 break;
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03001930 case ADV7604_PAD_HDMI_PORT_C:
Mats Randgaarddd08beb2013-12-10 09:57:09 -03001931 rep_write(sd, 0x72, edid->edid[spa_loc]);
1932 rep_write(sd, 0x73, edid->edid[spa_loc + 1]);
Mats Randgaard3e86aa82013-12-10 09:55:18 -03001933 break;
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03001934 case ADV7604_PAD_HDMI_PORT_D:
Mats Randgaarddd08beb2013-12-10 09:57:09 -03001935 rep_write(sd, 0x74, edid->edid[spa_loc]);
1936 rep_write(sd, 0x75, edid->edid[spa_loc + 1]);
Mats Randgaard3e86aa82013-12-10 09:55:18 -03001937 break;
Mats Randgaarddd08beb2013-12-10 09:57:09 -03001938 default:
1939 return -EINVAL;
Mats Randgaard3e86aa82013-12-10 09:55:18 -03001940 }
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001941
1942 if (info->type == ADV7604) {
1943 rep_write(sd, 0x76, spa_loc & 0xff);
1944 rep_write_and_or(sd, 0x77, 0xbf, (spa_loc & 0x100) >> 2);
1945 } else {
1946 /* FIXME: Where is the SPA location LSB register ? */
1947 rep_write_and_or(sd, 0x71, 0xfe, (spa_loc & 0x100) >> 8);
1948 }
Mats Randgaard3e86aa82013-12-10 09:55:18 -03001949
Mats Randgaarddd08beb2013-12-10 09:57:09 -03001950 edid->edid[spa_loc] = state->spa_port_a[0];
1951 edid->edid[spa_loc + 1] = state->spa_port_a[1];
Mats Randgaard4a31a932013-12-10 09:45:00 -03001952
1953 memcpy(state->edid.edid, edid->edid, 128 * edid->blocks);
1954 state->edid.blocks = edid->blocks;
Hans Verkuil54450f52012-07-18 05:45:16 -03001955 state->aspect_ratio = v4l2_calc_aspect_ratio(edid->edid[0x15],
1956 edid->edid[0x16]);
Mats Randgaard3e86aa82013-12-10 09:55:18 -03001957 state->edid.present |= 1 << edid->pad;
Mats Randgaard4a31a932013-12-10 09:45:00 -03001958
1959 err = edid_write_block(sd, 128 * edid->blocks, state->edid.edid);
1960 if (err < 0) {
Mats Randgaard3e86aa82013-12-10 09:55:18 -03001961 v4l2_err(sd, "error %d writing edid pad %d\n", err, edid->pad);
Mats Randgaard4a31a932013-12-10 09:45:00 -03001962 return err;
1963 }
1964
Mats Randgaarddd08beb2013-12-10 09:57:09 -03001965 /* adv7604 calculates the checksums and enables I2C access to internal
1966 EDID RAM from DDC port. */
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001967 rep_write_and_or(sd, info->edid_enable_reg, 0xf0, state->edid.present);
Mats Randgaarddd08beb2013-12-10 09:57:09 -03001968
1969 for (i = 0; i < 1000; i++) {
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001970 if (rep_read(sd, info->edid_status_reg) & state->edid.present)
Mats Randgaarddd08beb2013-12-10 09:57:09 -03001971 break;
1972 mdelay(1);
1973 }
1974 if (i == 1000) {
1975 v4l2_err(sd, "error enabling edid (0x%x)\n", state->edid.present);
1976 return -EIO;
1977 }
1978
1979
Mats Randgaard4a31a932013-12-10 09:45:00 -03001980 /* enable hotplug after 100 ms */
1981 queue_delayed_work(state->work_queues,
1982 &state->delayed_work_enable_hotplug, HZ / 10);
1983 return 0;
Hans Verkuil54450f52012-07-18 05:45:16 -03001984}
1985
1986/*********** avi info frame CEA-861-E **************/
1987
1988static void print_avi_infoframe(struct v4l2_subdev *sd)
1989{
1990 int i;
1991 u8 buf[14];
1992 u8 avi_len;
1993 u8 avi_ver;
1994
Martin Buggebb88f322013-08-14 08:52:46 -03001995 if (!is_hdmi(sd)) {
Hans Verkuil54450f52012-07-18 05:45:16 -03001996 v4l2_info(sd, "receive DVI-D signal (AVI infoframe not supported)\n");
1997 return;
1998 }
1999 if (!(io_read(sd, 0x60) & 0x01)) {
2000 v4l2_info(sd, "AVI infoframe not received\n");
2001 return;
2002 }
2003
2004 if (io_read(sd, 0x83) & 0x01) {
2005 v4l2_info(sd, "AVI infoframe checksum error has occurred earlier\n");
2006 io_write(sd, 0x85, 0x01); /* clear AVI_INF_CKS_ERR_RAW */
2007 if (io_read(sd, 0x83) & 0x01) {
2008 v4l2_info(sd, "AVI infoframe checksum error still present\n");
2009 io_write(sd, 0x85, 0x01); /* clear AVI_INF_CKS_ERR_RAW */
2010 }
2011 }
2012
2013 avi_len = infoframe_read(sd, 0xe2);
2014 avi_ver = infoframe_read(sd, 0xe1);
2015 v4l2_info(sd, "AVI infoframe version %d (%d byte)\n",
2016 avi_ver, avi_len);
2017
2018 if (avi_ver != 0x02)
2019 return;
2020
2021 for (i = 0; i < 14; i++)
2022 buf[i] = infoframe_read(sd, i);
2023
2024 v4l2_info(sd,
2025 "\t%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
2026 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7],
2027 buf[8], buf[9], buf[10], buf[11], buf[12], buf[13]);
2028}
2029
2030static int adv7604_log_status(struct v4l2_subdev *sd)
2031{
2032 struct adv7604_state *state = to_state(sd);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002033 const struct adv7604_chip_info *info = state->info;
Hans Verkuil54450f52012-07-18 05:45:16 -03002034 struct v4l2_dv_timings timings;
2035 struct stdi_readback stdi;
2036 u8 reg_io_0x02 = io_read(sd, 0x02);
Laurent Pinchart4a2ccdd2014-01-08 20:26:55 -03002037 u8 edid_enabled;
2038 u8 cable_det;
Hans Verkuil54450f52012-07-18 05:45:16 -03002039
Lars-Peter Clausenf216ccb2013-11-25 16:15:29 -03002040 static const char * const csc_coeff_sel_rb[16] = {
Hans Verkuil54450f52012-07-18 05:45:16 -03002041 "bypassed", "YPbPr601 -> RGB", "reserved", "YPbPr709 -> RGB",
2042 "reserved", "RGB -> YPbPr601", "reserved", "RGB -> YPbPr709",
2043 "reserved", "YPbPr709 -> YPbPr601", "YPbPr601 -> YPbPr709",
2044 "reserved", "reserved", "reserved", "reserved", "manual"
2045 };
Lars-Peter Clausenf216ccb2013-11-25 16:15:29 -03002046 static const char * const input_color_space_txt[16] = {
Hans Verkuil54450f52012-07-18 05:45:16 -03002047 "RGB limited range (16-235)", "RGB full range (0-255)",
2048 "YCbCr Bt.601 (16-235)", "YCbCr Bt.709 (16-235)",
Mats Randgaard98332392013-12-05 10:05:58 -03002049 "xvYCC Bt.601", "xvYCC Bt.709",
Hans Verkuil54450f52012-07-18 05:45:16 -03002050 "YCbCr Bt.601 (0-255)", "YCbCr Bt.709 (0-255)",
2051 "invalid", "invalid", "invalid", "invalid", "invalid",
2052 "invalid", "invalid", "automatic"
2053 };
Lars-Peter Clausenf216ccb2013-11-25 16:15:29 -03002054 static const char * const rgb_quantization_range_txt[] = {
Hans Verkuil54450f52012-07-18 05:45:16 -03002055 "Automatic",
2056 "RGB limited range (16-235)",
2057 "RGB full range (0-255)",
2058 };
Lars-Peter Clausenf216ccb2013-11-25 16:15:29 -03002059 static const char * const deep_color_mode_txt[4] = {
Martin Buggebb88f322013-08-14 08:52:46 -03002060 "8-bits per channel",
2061 "10-bits per channel",
2062 "12-bits per channel",
2063 "16-bits per channel (not supported)"
2064 };
Hans Verkuil54450f52012-07-18 05:45:16 -03002065
2066 v4l2_info(sd, "-----Chip status-----\n");
2067 v4l2_info(sd, "Chip power: %s\n", no_power(sd) ? "off" : "on");
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002068 edid_enabled = rep_read(sd, info->edid_status_reg);
Mats Randgaard4a31a932013-12-10 09:45:00 -03002069 v4l2_info(sd, "EDID enabled port A: %s, B: %s, C: %s, D: %s\n",
Laurent Pinchart4a2ccdd2014-01-08 20:26:55 -03002070 ((edid_enabled & 0x01) ? "Yes" : "No"),
2071 ((edid_enabled & 0x02) ? "Yes" : "No"),
2072 ((edid_enabled & 0x04) ? "Yes" : "No"),
2073 ((edid_enabled & 0x08) ? "Yes" : "No"));
Hans Verkuil54450f52012-07-18 05:45:16 -03002074 v4l2_info(sd, "CEC: %s\n", !!(cec_read(sd, 0x2a) & 0x01) ?
2075 "enabled" : "disabled");
2076
2077 v4l2_info(sd, "-----Signal status-----\n");
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002078 cable_det = info->read_cable_det(sd);
Mats Randgaard4a31a932013-12-10 09:45:00 -03002079 v4l2_info(sd, "Cable detected (+5V power) port A: %s, B: %s, C: %s, D: %s\n",
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002080 ((cable_det & 0x01) ? "Yes" : "No"),
2081 ((cable_det & 0x02) ? "Yes" : "No"),
Laurent Pinchart4a2ccdd2014-01-08 20:26:55 -03002082 ((cable_det & 0x04) ? "Yes" : "No"),
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002083 ((cable_det & 0x08) ? "Yes" : "No"));
Hans Verkuil54450f52012-07-18 05:45:16 -03002084 v4l2_info(sd, "TMDS signal detected: %s\n",
2085 no_signal_tmds(sd) ? "false" : "true");
2086 v4l2_info(sd, "TMDS signal locked: %s\n",
2087 no_lock_tmds(sd) ? "false" : "true");
2088 v4l2_info(sd, "SSPD locked: %s\n", no_lock_sspd(sd) ? "false" : "true");
2089 v4l2_info(sd, "STDI locked: %s\n", no_lock_stdi(sd) ? "false" : "true");
2090 v4l2_info(sd, "CP locked: %s\n", no_lock_cp(sd) ? "false" : "true");
2091 v4l2_info(sd, "CP free run: %s\n",
2092 (!!(cp_read(sd, 0xff) & 0x10) ? "on" : "off"));
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03002093 v4l2_info(sd, "Prim-mode = 0x%x, video std = 0x%x, v_freq = 0x%x\n",
2094 io_read(sd, 0x01) & 0x0f, io_read(sd, 0x00) & 0x3f,
2095 (io_read(sd, 0x01) & 0x70) >> 4);
Hans Verkuil54450f52012-07-18 05:45:16 -03002096
2097 v4l2_info(sd, "-----Video Timings-----\n");
2098 if (read_stdi(sd, &stdi))
2099 v4l2_info(sd, "STDI: not locked\n");
2100 else
2101 v4l2_info(sd, "STDI: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, %s, %chsync, %cvsync\n",
2102 stdi.lcf, stdi.bl, stdi.lcvs,
2103 stdi.interlaced ? "interlaced" : "progressive",
2104 stdi.hs_pol, stdi.vs_pol);
2105 if (adv7604_query_dv_timings(sd, &timings))
2106 v4l2_info(sd, "No video detected\n");
2107 else
Hans Verkuil11d034c2013-08-15 08:05:59 -03002108 v4l2_print_dv_timings(sd->name, "Detected format: ",
2109 &timings, true);
2110 v4l2_print_dv_timings(sd->name, "Configured format: ",
2111 &state->timings, true);
Hans Verkuil54450f52012-07-18 05:45:16 -03002112
Mats Randgaard76eb2d32013-08-14 08:56:57 -03002113 if (no_signal(sd))
2114 return 0;
2115
Hans Verkuil54450f52012-07-18 05:45:16 -03002116 v4l2_info(sd, "-----Color space-----\n");
2117 v4l2_info(sd, "RGB quantization range ctrl: %s\n",
2118 rgb_quantization_range_txt[state->rgb_quantization_range]);
2119 v4l2_info(sd, "Input color space: %s\n",
2120 input_color_space_txt[reg_io_0x02 >> 4]);
2121 v4l2_info(sd, "Output color space: %s %s, saturator %s\n",
2122 (reg_io_0x02 & 0x02) ? "RGB" : "YCbCr",
2123 (reg_io_0x02 & 0x04) ? "(16-235)" : "(0-255)",
2124 ((reg_io_0x02 & 0x04) ^ (reg_io_0x02 & 0x01)) ?
Mats Randgaard76eb2d32013-08-14 08:56:57 -03002125 "enabled" : "disabled");
Hans Verkuil54450f52012-07-18 05:45:16 -03002126 v4l2_info(sd, "Color space conversion: %s\n",
2127 csc_coeff_sel_rb[cp_read(sd, 0xfc) >> 4]);
2128
Mats Randgaard4a31a932013-12-10 09:45:00 -03002129 if (!is_digital_input(sd))
Mats Randgaard76eb2d32013-08-14 08:56:57 -03002130 return 0;
2131
2132 v4l2_info(sd, "-----%s status-----\n", is_hdmi(sd) ? "HDMI" : "DVI-D");
Mats Randgaard4a31a932013-12-10 09:45:00 -03002133 v4l2_info(sd, "Digital video port selected: %c\n",
2134 (hdmi_read(sd, 0x00) & 0x03) + 'A');
2135 v4l2_info(sd, "HDCP encrypted content: %s\n",
2136 (hdmi_read(sd, 0x05) & 0x40) ? "true" : "false");
Mats Randgaard76eb2d32013-08-14 08:56:57 -03002137 v4l2_info(sd, "HDCP keys read: %s%s\n",
2138 (hdmi_read(sd, 0x04) & 0x20) ? "yes" : "no",
2139 (hdmi_read(sd, 0x04) & 0x10) ? "ERROR" : "");
2140 if (!is_hdmi(sd)) {
2141 bool audio_pll_locked = hdmi_read(sd, 0x04) & 0x01;
2142 bool audio_sample_packet_detect = hdmi_read(sd, 0x18) & 0x01;
2143 bool audio_mute = io_read(sd, 0x65) & 0x40;
2144
2145 v4l2_info(sd, "Audio: pll %s, samples %s, %s\n",
2146 audio_pll_locked ? "locked" : "not locked",
2147 audio_sample_packet_detect ? "detected" : "not detected",
2148 audio_mute ? "muted" : "enabled");
2149 if (audio_pll_locked && audio_sample_packet_detect) {
2150 v4l2_info(sd, "Audio format: %s\n",
2151 (hdmi_read(sd, 0x07) & 0x20) ? "multi-channel" : "stereo");
2152 }
2153 v4l2_info(sd, "Audio CTS: %u\n", (hdmi_read(sd, 0x5b) << 12) +
2154 (hdmi_read(sd, 0x5c) << 8) +
2155 (hdmi_read(sd, 0x5d) & 0xf0));
2156 v4l2_info(sd, "Audio N: %u\n", ((hdmi_read(sd, 0x5d) & 0x0f) << 16) +
2157 (hdmi_read(sd, 0x5e) << 8) +
2158 hdmi_read(sd, 0x5f));
2159 v4l2_info(sd, "AV Mute: %s\n", (hdmi_read(sd, 0x04) & 0x40) ? "on" : "off");
2160
2161 v4l2_info(sd, "Deep color mode: %s\n", deep_color_mode_txt[(hdmi_read(sd, 0x0b) & 0x60) >> 5]);
2162
Hans Verkuil54450f52012-07-18 05:45:16 -03002163 print_avi_infoframe(sd);
2164 }
2165
2166 return 0;
2167}
2168
2169/* ----------------------------------------------------------------------- */
2170
2171static const struct v4l2_ctrl_ops adv7604_ctrl_ops = {
2172 .s_ctrl = adv7604_s_ctrl,
2173};
2174
2175static const struct v4l2_subdev_core_ops adv7604_core_ops = {
2176 .log_status = adv7604_log_status,
Hans Verkuil54450f52012-07-18 05:45:16 -03002177 .interrupt_service_routine = adv7604_isr,
2178#ifdef CONFIG_VIDEO_ADV_DEBUG
2179 .g_register = adv7604_g_register,
2180 .s_register = adv7604_s_register,
2181#endif
2182};
2183
2184static const struct v4l2_subdev_video_ops adv7604_video_ops = {
2185 .s_routing = adv7604_s_routing,
2186 .g_input_status = adv7604_g_input_status,
2187 .s_dv_timings = adv7604_s_dv_timings,
2188 .g_dv_timings = adv7604_g_dv_timings,
2189 .query_dv_timings = adv7604_query_dv_timings,
2190 .enum_dv_timings = adv7604_enum_dv_timings,
2191 .dv_timings_cap = adv7604_dv_timings_cap,
2192 .enum_mbus_fmt = adv7604_enum_mbus_fmt,
2193 .g_mbus_fmt = adv7604_g_mbus_fmt,
2194 .try_mbus_fmt = adv7604_g_mbus_fmt,
2195 .s_mbus_fmt = adv7604_g_mbus_fmt,
2196};
2197
2198static const struct v4l2_subdev_pad_ops adv7604_pad_ops = {
2199 .get_edid = adv7604_get_edid,
2200 .set_edid = adv7604_set_edid,
2201};
2202
2203static const struct v4l2_subdev_ops adv7604_ops = {
2204 .core = &adv7604_core_ops,
2205 .video = &adv7604_video_ops,
2206 .pad = &adv7604_pad_ops,
2207};
2208
2209/* -------------------------- custom ctrls ---------------------------------- */
2210
2211static const struct v4l2_ctrl_config adv7604_ctrl_analog_sampling_phase = {
2212 .ops = &adv7604_ctrl_ops,
2213 .id = V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE,
2214 .name = "Analog Sampling Phase",
2215 .type = V4L2_CTRL_TYPE_INTEGER,
2216 .min = 0,
2217 .max = 0x1f,
2218 .step = 1,
2219 .def = 0,
2220};
2221
2222static const struct v4l2_ctrl_config adv7604_ctrl_free_run_color_manual = {
2223 .ops = &adv7604_ctrl_ops,
2224 .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL,
2225 .name = "Free Running Color, Manual",
2226 .type = V4L2_CTRL_TYPE_BOOLEAN,
2227 .min = false,
2228 .max = true,
2229 .step = 1,
2230 .def = false,
2231};
2232
2233static const struct v4l2_ctrl_config adv7604_ctrl_free_run_color = {
2234 .ops = &adv7604_ctrl_ops,
2235 .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR,
2236 .name = "Free Running Color",
2237 .type = V4L2_CTRL_TYPE_INTEGER,
2238 .min = 0x0,
2239 .max = 0xffffff,
2240 .step = 0x1,
2241 .def = 0x0,
2242};
2243
2244/* ----------------------------------------------------------------------- */
2245
2246static int adv7604_core_init(struct v4l2_subdev *sd)
2247{
2248 struct adv7604_state *state = to_state(sd);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002249 const struct adv7604_chip_info *info = state->info;
Hans Verkuil54450f52012-07-18 05:45:16 -03002250 struct adv7604_platform_data *pdata = &state->pdata;
2251
2252 hdmi_write(sd, 0x48,
2253 (pdata->disable_pwrdnb ? 0x80 : 0) |
2254 (pdata->disable_cable_det_rst ? 0x40 : 0));
2255
2256 disable_input(sd);
2257
2258 /* power */
2259 io_write(sd, 0x0c, 0x42); /* Power up part and power down VDP */
2260 io_write(sd, 0x0b, 0x44); /* Power down ESDP block */
2261 cp_write(sd, 0xcf, 0x01); /* Power down macrovision */
2262
2263 /* video format */
2264 io_write_and_or(sd, 0x02, 0xf0,
2265 pdata->alt_gamma << 3 |
2266 pdata->op_656_range << 2 |
2267 pdata->rgb_out << 1 |
2268 pdata->alt_data_sat << 0);
2269 io_write(sd, 0x03, pdata->op_format_sel);
2270 io_write_and_or(sd, 0x04, 0x1f, pdata->op_ch_sel << 5);
2271 io_write_and_or(sd, 0x05, 0xf0, pdata->blank_data << 3 |
2272 pdata->insert_av_codes << 2 |
2273 pdata->replicate_av_codes << 1 |
2274 pdata->invert_cbcr << 0);
2275
Hans Verkuil54450f52012-07-18 05:45:16 -03002276 cp_write(sd, 0x69, 0x30); /* Enable CP CSC */
Martin Bugge98908692013-12-20 05:14:57 -03002277
2278 /* VS, HS polarities */
2279 io_write(sd, 0x06, 0xa0 | pdata->inv_vs_pol << 2 | pdata->inv_hs_pol << 1);
Mikhail Khelikf31b62e2013-12-20 05:12:00 -03002280
2281 /* Adjust drive strength */
2282 io_write(sd, 0x14, 0x40 | pdata->dr_str_data << 4 |
2283 pdata->dr_str_clk << 2 |
2284 pdata->dr_str_sync);
2285
Hans Verkuil54450f52012-07-18 05:45:16 -03002286 cp_write(sd, 0xba, (pdata->hdmi_free_run_mode << 1) | 0x01); /* HDMI free run */
2287 cp_write(sd, 0xf3, 0xdc); /* Low threshold to enter/exit free run mode */
2288 cp_write(sd, 0xf9, 0x23); /* STDI ch. 1 - LCVS change threshold -
Hans Verkuil80939642012-10-16 05:46:21 -03002289 ADI recommended setting [REF_01, c. 2.3.3] */
Hans Verkuil54450f52012-07-18 05:45:16 -03002290 cp_write(sd, 0x45, 0x23); /* STDI ch. 2 - LCVS change threshold -
Hans Verkuil80939642012-10-16 05:46:21 -03002291 ADI recommended setting [REF_01, c. 2.3.3] */
Hans Verkuil54450f52012-07-18 05:45:16 -03002292 cp_write(sd, 0xc9, 0x2d); /* use prim_mode and vid_std as free run resolution
2293 for digital formats */
2294
Mats Randgaard5474b982013-12-05 10:33:41 -03002295 /* HDMI audio */
2296 hdmi_write_and_or(sd, 0x15, 0xfc, 0x03); /* Mute on FIFO over-/underflow [REF_01, c. 1.2.18] */
2297 hdmi_write_and_or(sd, 0x1a, 0xf1, 0x08); /* Wait 1 s before unmute */
2298 hdmi_write_and_or(sd, 0x68, 0xf9, 0x06); /* FIFO reset on over-/underflow [REF_01, c. 1.2.19] */
2299
Hans Verkuil54450f52012-07-18 05:45:16 -03002300 /* TODO from platform data */
2301 afe_write(sd, 0xb5, 0x01); /* Setting MCLK to 256Fs */
2302
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002303 if (adv7604_has_afe(state)) {
2304 afe_write(sd, 0x02, pdata->ain_sel); /* Select analog input muxing mode */
2305 io_write_and_or(sd, 0x30, ~(1 << 4), pdata->output_bus_lsb_to_msb << 4);
2306 }
Hans Verkuil54450f52012-07-18 05:45:16 -03002307
Hans Verkuil54450f52012-07-18 05:45:16 -03002308 /* interrupts */
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002309 io_write(sd, 0x40, 0xc0 | pdata->int1_config); /* Configure INT1 */
Hans Verkuil54450f52012-07-18 05:45:16 -03002310 io_write(sd, 0x46, 0x98); /* Enable SSPD, STDI and CP unlocked interrupts */
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002311 io_write(sd, 0x6e, info->fmt_change_digital_mask); /* Enable V_LOCKED and DE_REGEN_LCK interrupts */
2312 io_write(sd, 0x73, info->cable_det_mask); /* Enable cable detection (+5v) interrupts */
2313 info->setup_irqs(sd);
Hans Verkuil54450f52012-07-18 05:45:16 -03002314
2315 return v4l2_ctrl_handler_setup(sd->ctrl_handler);
2316}
2317
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002318static void adv7604_setup_irqs(struct v4l2_subdev *sd)
2319{
2320 io_write(sd, 0x41, 0xd7); /* STDI irq for any change, disable INT2 */
2321}
2322
2323static void adv7611_setup_irqs(struct v4l2_subdev *sd)
2324{
2325 io_write(sd, 0x41, 0xd0); /* STDI irq for any change, disable INT2 */
2326}
2327
Hans Verkuil54450f52012-07-18 05:45:16 -03002328static void adv7604_unregister_clients(struct adv7604_state *state)
2329{
2330 if (state->i2c_avlink)
2331 i2c_unregister_device(state->i2c_avlink);
2332 if (state->i2c_cec)
2333 i2c_unregister_device(state->i2c_cec);
2334 if (state->i2c_infoframe)
2335 i2c_unregister_device(state->i2c_infoframe);
2336 if (state->i2c_esdp)
2337 i2c_unregister_device(state->i2c_esdp);
2338 if (state->i2c_dpp)
2339 i2c_unregister_device(state->i2c_dpp);
2340 if (state->i2c_afe)
2341 i2c_unregister_device(state->i2c_afe);
2342 if (state->i2c_repeater)
2343 i2c_unregister_device(state->i2c_repeater);
2344 if (state->i2c_edid)
2345 i2c_unregister_device(state->i2c_edid);
2346 if (state->i2c_hdmi)
2347 i2c_unregister_device(state->i2c_hdmi);
2348 if (state->i2c_test)
2349 i2c_unregister_device(state->i2c_test);
2350 if (state->i2c_cp)
2351 i2c_unregister_device(state->i2c_cp);
2352 if (state->i2c_vdp)
2353 i2c_unregister_device(state->i2c_vdp);
2354}
2355
2356static struct i2c_client *adv7604_dummy_client(struct v4l2_subdev *sd,
2357 u8 addr, u8 io_reg)
2358{
2359 struct i2c_client *client = v4l2_get_subdevdata(sd);
2360
2361 if (addr)
2362 io_write(sd, io_reg, addr << 1);
2363 return i2c_new_dummy(client->adapter, io_read(sd, io_reg) >> 1);
2364}
2365
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002366static const struct adv7604_reg_seq adv7604_recommended_settings_afe[] = {
2367 /* reset ADI recommended settings for HDMI: */
2368 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 4. */
2369 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x0d), 0x04 }, /* HDMI filter optimization */
2370 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x0d), 0x04 }, /* HDMI filter optimization */
2371 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x3d), 0x00 }, /* DDC bus active pull-up control */
2372 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x3e), 0x74 }, /* TMDS PLL optimization */
2373 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x4e), 0x3b }, /* TMDS PLL optimization */
2374 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x57), 0x74 }, /* TMDS PLL optimization */
2375 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x58), 0x63 }, /* TMDS PLL optimization */
2376 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8d), 0x18 }, /* equaliser */
2377 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8e), 0x34 }, /* equaliser */
2378 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x93), 0x88 }, /* equaliser */
2379 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x94), 0x2e }, /* equaliser */
2380 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x96), 0x00 }, /* enable automatic EQ changing */
2381
2382 /* set ADI recommended settings for digitizer */
2383 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 17. */
2384 { ADV7604_REG(ADV7604_PAGE_AFE, 0x12), 0x7b }, /* ADC noise shaping filter controls */
2385 { ADV7604_REG(ADV7604_PAGE_AFE, 0x0c), 0x1f }, /* CP core gain controls */
2386 { ADV7604_REG(ADV7604_PAGE_CP, 0x3e), 0x04 }, /* CP core pre-gain control */
2387 { ADV7604_REG(ADV7604_PAGE_CP, 0xc3), 0x39 }, /* CP coast control. Graphics mode */
2388 { ADV7604_REG(ADV7604_PAGE_CP, 0x40), 0x5c }, /* CP core pre-gain control. Graphics mode */
2389
2390 { ADV7604_REG_SEQ_TERM, 0 },
2391};
2392
2393static const struct adv7604_reg_seq adv7604_recommended_settings_hdmi[] = {
2394 /* set ADI recommended settings for HDMI: */
2395 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 4. */
2396 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x0d), 0x84 }, /* HDMI filter optimization */
2397 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x3d), 0x10 }, /* DDC bus active pull-up control */
2398 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x3e), 0x39 }, /* TMDS PLL optimization */
2399 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x4e), 0x3b }, /* TMDS PLL optimization */
2400 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x57), 0xb6 }, /* TMDS PLL optimization */
2401 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x58), 0x03 }, /* TMDS PLL optimization */
2402 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8d), 0x18 }, /* equaliser */
2403 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8e), 0x34 }, /* equaliser */
2404 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x93), 0x8b }, /* equaliser */
2405 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x94), 0x2d }, /* equaliser */
2406 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x96), 0x01 }, /* enable automatic EQ changing */
2407
2408 /* reset ADI recommended settings for digitizer */
2409 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 17. */
2410 { ADV7604_REG(ADV7604_PAGE_AFE, 0x12), 0xfb }, /* ADC noise shaping filter controls */
2411 { ADV7604_REG(ADV7604_PAGE_AFE, 0x0c), 0x0d }, /* CP core gain controls */
2412
2413 { ADV7604_REG_SEQ_TERM, 0 },
2414};
2415
2416static const struct adv7604_reg_seq adv7611_recommended_settings_hdmi[] = {
2417 { ADV7604_REG(ADV7604_PAGE_CP, 0x6c), 0x00 },
2418 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x6f), 0x0c },
2419 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x87), 0x70 },
2420 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x57), 0xda },
2421 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x58), 0x01 },
2422 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x03), 0x98 },
2423 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x4c), 0x44 },
2424 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8d), 0x04 },
2425 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8e), 0x1e },
2426
2427 { ADV7604_REG_SEQ_TERM, 0 },
2428};
2429
2430static const struct adv7604_chip_info adv7604_chip_info[] = {
2431 [ADV7604] = {
2432 .type = ADV7604,
2433 .has_afe = true,
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03002434 .max_port = ADV7604_PAD_VGA_COMP,
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002435 .num_dv_ports = 4,
2436 .edid_enable_reg = 0x77,
2437 .edid_status_reg = 0x7d,
2438 .lcf_reg = 0xb3,
2439 .tdms_lock_mask = 0xe0,
2440 .cable_det_mask = 0x1e,
2441 .fmt_change_digital_mask = 0xc1,
2442 .set_termination = adv7604_set_termination,
2443 .setup_irqs = adv7604_setup_irqs,
2444 .read_hdmi_pixelclock = adv7604_read_hdmi_pixelclock,
2445 .read_cable_det = adv7604_read_cable_det,
2446 .recommended_settings = {
2447 [0] = adv7604_recommended_settings_afe,
2448 [1] = adv7604_recommended_settings_hdmi,
2449 },
2450 .num_recommended_settings = {
2451 [0] = ARRAY_SIZE(adv7604_recommended_settings_afe),
2452 [1] = ARRAY_SIZE(adv7604_recommended_settings_hdmi),
2453 },
2454 .page_mask = BIT(ADV7604_PAGE_IO) | BIT(ADV7604_PAGE_AVLINK) |
2455 BIT(ADV7604_PAGE_CEC) | BIT(ADV7604_PAGE_INFOFRAME) |
2456 BIT(ADV7604_PAGE_ESDP) | BIT(ADV7604_PAGE_DPP) |
2457 BIT(ADV7604_PAGE_AFE) | BIT(ADV7604_PAGE_REP) |
2458 BIT(ADV7604_PAGE_EDID) | BIT(ADV7604_PAGE_HDMI) |
2459 BIT(ADV7604_PAGE_TEST) | BIT(ADV7604_PAGE_CP) |
2460 BIT(ADV7604_PAGE_VDP),
2461 },
2462 [ADV7611] = {
2463 .type = ADV7611,
2464 .has_afe = false,
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03002465 .max_port = ADV7604_PAD_HDMI_PORT_A,
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002466 .num_dv_ports = 1,
2467 .edid_enable_reg = 0x74,
2468 .edid_status_reg = 0x76,
2469 .lcf_reg = 0xa3,
2470 .tdms_lock_mask = 0x43,
2471 .cable_det_mask = 0x01,
2472 .fmt_change_digital_mask = 0x03,
2473 .set_termination = adv7611_set_termination,
2474 .setup_irqs = adv7611_setup_irqs,
2475 .read_hdmi_pixelclock = adv7611_read_hdmi_pixelclock,
2476 .read_cable_det = adv7611_read_cable_det,
2477 .recommended_settings = {
2478 [1] = adv7611_recommended_settings_hdmi,
2479 },
2480 .num_recommended_settings = {
2481 [1] = ARRAY_SIZE(adv7611_recommended_settings_hdmi),
2482 },
2483 .page_mask = BIT(ADV7604_PAGE_IO) | BIT(ADV7604_PAGE_CEC) |
2484 BIT(ADV7604_PAGE_INFOFRAME) | BIT(ADV7604_PAGE_AFE) |
2485 BIT(ADV7604_PAGE_REP) | BIT(ADV7604_PAGE_EDID) |
2486 BIT(ADV7604_PAGE_HDMI) | BIT(ADV7604_PAGE_CP),
2487 },
2488};
2489
Hans Verkuil54450f52012-07-18 05:45:16 -03002490static int adv7604_probe(struct i2c_client *client,
2491 const struct i2c_device_id *id)
2492{
Hans Verkuil591b72f2013-12-17 10:05:13 -03002493 static const struct v4l2_dv_timings cea640x480 =
2494 V4L2_DV_BT_CEA_640X480P59_94;
Hans Verkuil54450f52012-07-18 05:45:16 -03002495 struct adv7604_state *state;
2496 struct adv7604_platform_data *pdata = client->dev.platform_data;
2497 struct v4l2_ctrl_handler *hdl;
2498 struct v4l2_subdev *sd;
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03002499 unsigned int i;
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002500 u16 val;
Hans Verkuil54450f52012-07-18 05:45:16 -03002501 int err;
2502
2503 /* Check if the adapter supports the needed features */
2504 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
2505 return -EIO;
2506 v4l_dbg(1, debug, client, "detecting adv7604 client on address 0x%x\n",
2507 client->addr << 1);
2508
Laurent Pinchartc02b2112013-05-02 08:29:43 -03002509 state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
Hans Verkuil54450f52012-07-18 05:45:16 -03002510 if (!state) {
2511 v4l_err(client, "Could not allocate adv7604_state memory!\n");
2512 return -ENOMEM;
2513 }
2514
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002515 state->info = &adv7604_chip_info[id->driver_data];
2516
Mats Randgaard25a64ac2013-08-14 07:58:45 -03002517 /* initialize variables */
2518 state->restart_stdi_once = true;
Mats Randgaardff4f80f2013-12-05 10:24:05 -03002519 state->selected_input = ~0;
Mats Randgaard25a64ac2013-08-14 07:58:45 -03002520
Hans Verkuil54450f52012-07-18 05:45:16 -03002521 /* platform data */
2522 if (!pdata) {
2523 v4l_err(client, "No platform data!\n");
Laurent Pinchartc02b2112013-05-02 08:29:43 -03002524 return -ENODEV;
Hans Verkuil54450f52012-07-18 05:45:16 -03002525 }
Hans Verkuil591b72f2013-12-17 10:05:13 -03002526 state->pdata = *pdata;
2527 state->timings = cea640x480;
Hans Verkuil54450f52012-07-18 05:45:16 -03002528
2529 sd = &state->sd;
2530 v4l2_i2c_subdev_init(sd, client, &adv7604_ops);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002531 snprintf(sd->name, sizeof(sd->name), "%s %d-%04x",
2532 id->name, i2c_adapter_id(client->adapter),
2533 client->addr);
Hans Verkuil54450f52012-07-18 05:45:16 -03002534 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
Hans Verkuil54450f52012-07-18 05:45:16 -03002535
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002536 /*
2537 * Verify that the chip is present. On ADV7604 the RD_INFO register only
2538 * identifies the revision, while on ADV7611 it identifies the model as
2539 * well. Use the HDMI slave address on ADV7604 and RD_INFO on ADV7611.
2540 */
2541 if (state->info->type == ADV7604) {
2542 val = adv_smbus_read_byte_data_check(client, 0xfb, false);
2543 if (val != 0x68) {
2544 v4l2_info(sd, "not an adv7604 on address 0x%x\n",
2545 client->addr << 1);
2546 return -ENODEV;
2547 }
2548 } else {
2549 val = (adv_smbus_read_byte_data_check(client, 0xea, false) << 8)
2550 | (adv_smbus_read_byte_data_check(client, 0xeb, false) << 0);
2551 if (val != 0x2051) {
2552 v4l2_info(sd, "not an adv7611 on address 0x%x\n",
2553 client->addr << 1);
2554 return -ENODEV;
2555 }
Hans Verkuil54450f52012-07-18 05:45:16 -03002556 }
2557
2558 /* control handlers */
2559 hdl = &state->hdl;
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002560 v4l2_ctrl_handler_init(hdl, adv7604_has_afe(state) ? 9 : 8);
Hans Verkuil54450f52012-07-18 05:45:16 -03002561
2562 v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
2563 V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
2564 v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
2565 V4L2_CID_CONTRAST, 0, 255, 1, 128);
2566 v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
2567 V4L2_CID_SATURATION, 0, 255, 1, 128);
2568 v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
2569 V4L2_CID_HUE, 0, 128, 1, 0);
2570
2571 /* private controls */
2572 state->detect_tx_5v_ctrl = v4l2_ctrl_new_std(hdl, NULL,
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002573 V4L2_CID_DV_RX_POWER_PRESENT, 0,
2574 (1 << state->info->num_dv_ports) - 1, 0, 0);
Hans Verkuil54450f52012-07-18 05:45:16 -03002575 state->rgb_quantization_range_ctrl =
2576 v4l2_ctrl_new_std_menu(hdl, &adv7604_ctrl_ops,
2577 V4L2_CID_DV_RX_RGB_RANGE, V4L2_DV_RGB_RANGE_FULL,
2578 0, V4L2_DV_RGB_RANGE_AUTO);
Hans Verkuil54450f52012-07-18 05:45:16 -03002579
2580 /* custom controls */
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002581 if (adv7604_has_afe(state))
2582 state->analog_sampling_phase_ctrl =
2583 v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_analog_sampling_phase, NULL);
Hans Verkuil54450f52012-07-18 05:45:16 -03002584 state->free_run_color_manual_ctrl =
2585 v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_free_run_color_manual, NULL);
Hans Verkuil54450f52012-07-18 05:45:16 -03002586 state->free_run_color_ctrl =
2587 v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_free_run_color, NULL);
Hans Verkuil54450f52012-07-18 05:45:16 -03002588
2589 sd->ctrl_handler = hdl;
2590 if (hdl->error) {
2591 err = hdl->error;
2592 goto err_hdl;
2593 }
Hans Verkuil8c0eadb2013-08-22 06:11:17 -03002594 state->detect_tx_5v_ctrl->is_private = true;
2595 state->rgb_quantization_range_ctrl->is_private = true;
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002596 if (adv7604_has_afe(state))
2597 state->analog_sampling_phase_ctrl->is_private = true;
Hans Verkuil8c0eadb2013-08-22 06:11:17 -03002598 state->free_run_color_manual_ctrl->is_private = true;
2599 state->free_run_color_ctrl->is_private = true;
2600
Hans Verkuil54450f52012-07-18 05:45:16 -03002601 if (adv7604_s_detect_tx_5v_ctrl(sd)) {
2602 err = -ENODEV;
2603 goto err_hdl;
2604 }
2605
Hans Verkuil54450f52012-07-18 05:45:16 -03002606 state->i2c_cec = adv7604_dummy_client(sd, pdata->i2c_cec, 0xf4);
2607 state->i2c_infoframe = adv7604_dummy_client(sd, pdata->i2c_infoframe, 0xf5);
Hans Verkuil54450f52012-07-18 05:45:16 -03002608 state->i2c_afe = adv7604_dummy_client(sd, pdata->i2c_afe, 0xf8);
2609 state->i2c_repeater = adv7604_dummy_client(sd, pdata->i2c_repeater, 0xf9);
2610 state->i2c_edid = adv7604_dummy_client(sd, pdata->i2c_edid, 0xfa);
2611 state->i2c_hdmi = adv7604_dummy_client(sd, pdata->i2c_hdmi, 0xfb);
Hans Verkuil54450f52012-07-18 05:45:16 -03002612 state->i2c_cp = adv7604_dummy_client(sd, pdata->i2c_cp, 0xfd);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002613 if (!state->i2c_cec || !state->i2c_infoframe || !state->i2c_afe ||
Hans Verkuil54450f52012-07-18 05:45:16 -03002614 !state->i2c_repeater || !state->i2c_edid || !state->i2c_hdmi ||
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002615 !state->i2c_cp) {
Hans Verkuil54450f52012-07-18 05:45:16 -03002616 err = -ENOMEM;
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002617 v4l2_err(sd, "failed to create digital i2c clients\n");
Hans Verkuil54450f52012-07-18 05:45:16 -03002618 goto err_i2c;
2619 }
Hans Verkuil54450f52012-07-18 05:45:16 -03002620
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002621 if (adv7604_has_afe(state)) {
2622 state->i2c_avlink = adv7604_dummy_client(sd, pdata->i2c_avlink, 0xf3);
2623 state->i2c_esdp = adv7604_dummy_client(sd, pdata->i2c_esdp, 0xf6);
2624 state->i2c_dpp = adv7604_dummy_client(sd, pdata->i2c_dpp, 0xf7);
2625 state->i2c_test = adv7604_dummy_client(sd, pdata->i2c_test, 0xfc);
2626 state->i2c_vdp = adv7604_dummy_client(sd, pdata->i2c_vdp, 0xfe);
2627 if (!state->i2c_avlink || !state->i2c_esdp || !state->i2c_dpp ||
2628 !state->i2c_test || !state->i2c_vdp) {
2629 err = -ENOMEM;
2630 v4l2_err(sd, "failed to create analog i2c clients\n");
2631 goto err_i2c;
2632 }
2633 }
Hans Verkuil54450f52012-07-18 05:45:16 -03002634 /* work queues */
2635 state->work_queues = create_singlethread_workqueue(client->name);
2636 if (!state->work_queues) {
2637 v4l2_err(sd, "Could not create work queue\n");
2638 err = -ENOMEM;
2639 goto err_i2c;
2640 }
2641
2642 INIT_DELAYED_WORK(&state->delayed_work_enable_hotplug,
2643 adv7604_delayed_work_enable_hotplug);
2644
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03002645 state->source_pad = state->info->num_dv_ports
2646 + (state->info->has_afe ? 2 : 0);
2647 for (i = 0; i < state->source_pad; ++i)
2648 state->pads[i].flags = MEDIA_PAD_FL_SINK;
2649 state->pads[state->source_pad].flags = MEDIA_PAD_FL_SOURCE;
2650
2651 err = media_entity_init(&sd->entity, state->source_pad + 1,
2652 state->pads, 0);
Hans Verkuil54450f52012-07-18 05:45:16 -03002653 if (err)
2654 goto err_work_queues;
2655
2656 err = adv7604_core_init(sd);
2657 if (err)
2658 goto err_entity;
2659 v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
2660 client->addr << 1, client->adapter->name);
Lars-Peter Clausenbedc3932013-11-25 16:18:02 -03002661
2662 err = v4l2_async_register_subdev(sd);
2663 if (err)
2664 goto err_entity;
2665
Hans Verkuil54450f52012-07-18 05:45:16 -03002666 return 0;
2667
2668err_entity:
2669 media_entity_cleanup(&sd->entity);
2670err_work_queues:
2671 cancel_delayed_work(&state->delayed_work_enable_hotplug);
2672 destroy_workqueue(state->work_queues);
2673err_i2c:
2674 adv7604_unregister_clients(state);
2675err_hdl:
2676 v4l2_ctrl_handler_free(hdl);
Hans Verkuil54450f52012-07-18 05:45:16 -03002677 return err;
2678}
2679
2680/* ----------------------------------------------------------------------- */
2681
2682static int adv7604_remove(struct i2c_client *client)
2683{
2684 struct v4l2_subdev *sd = i2c_get_clientdata(client);
2685 struct adv7604_state *state = to_state(sd);
2686
2687 cancel_delayed_work(&state->delayed_work_enable_hotplug);
2688 destroy_workqueue(state->work_queues);
Lars-Peter Clausenbedc3932013-11-25 16:18:02 -03002689 v4l2_async_unregister_subdev(sd);
Hans Verkuil54450f52012-07-18 05:45:16 -03002690 v4l2_device_unregister_subdev(sd);
2691 media_entity_cleanup(&sd->entity);
2692 adv7604_unregister_clients(to_state(sd));
2693 v4l2_ctrl_handler_free(sd->ctrl_handler);
Hans Verkuil54450f52012-07-18 05:45:16 -03002694 return 0;
2695}
2696
2697/* ----------------------------------------------------------------------- */
2698
2699static struct i2c_device_id adv7604_id[] = {
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002700 { "adv7604", ADV7604 },
2701 { "adv7611", ADV7611 },
Hans Verkuil54450f52012-07-18 05:45:16 -03002702 { }
2703};
2704MODULE_DEVICE_TABLE(i2c, adv7604_id);
2705
2706static struct i2c_driver adv7604_driver = {
2707 .driver = {
2708 .owner = THIS_MODULE,
2709 .name = "adv7604",
2710 },
2711 .probe = adv7604_probe,
2712 .remove = adv7604_remove,
2713 .id_table = adv7604_id,
2714};
2715
2716module_i2c_driver(adv7604_driver);