blob: 684b91234d5db1618e5c4583fea12d79c98f57f4 [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
Laurent Pinchart539b33b2014-01-26 18:42:37 -030056#define ADV7604_RGB_OUT (1 << 1)
57
58#define ADV7604_OP_FORMAT_SEL_8BIT (0 << 0)
59#define ADV7604_OP_FORMAT_SEL_10BIT (1 << 0)
60#define ADV7604_OP_FORMAT_SEL_12BIT (2 << 0)
61
62#define ADV7604_OP_MODE_SEL_SDR_422 (0 << 5)
63#define ADV7604_OP_MODE_SEL_DDR_422 (1 << 5)
64#define ADV7604_OP_MODE_SEL_SDR_444 (2 << 5)
65#define ADV7604_OP_MODE_SEL_DDR_444 (3 << 5)
66#define ADV7604_OP_MODE_SEL_SDR_422_2X (4 << 5)
67#define ADV7604_OP_MODE_SEL_ADI_CM (5 << 5)
68
69#define ADV7604_OP_CH_SEL_GBR (0 << 5)
70#define ADV7604_OP_CH_SEL_GRB (1 << 5)
71#define ADV7604_OP_CH_SEL_BGR (2 << 5)
72#define ADV7604_OP_CH_SEL_RGB (3 << 5)
73#define ADV7604_OP_CH_SEL_BRG (4 << 5)
74#define ADV7604_OP_CH_SEL_RBG (5 << 5)
75
76#define ADV7604_OP_SWAP_CB_CR (1 << 0)
77
Lars-Peter Clausend42010a2013-11-25 15:45:07 -030078enum adv7604_type {
79 ADV7604,
80 ADV7611,
81};
82
83struct adv7604_reg_seq {
84 unsigned int reg;
85 u8 val;
86};
87
Laurent Pinchart539b33b2014-01-26 18:42:37 -030088struct adv7604_format_info {
89 enum v4l2_mbus_pixelcode code;
90 u8 op_ch_sel;
91 bool rgb_out;
92 bool swap_cb_cr;
93 u8 op_format_sel;
94};
95
Lars-Peter Clausend42010a2013-11-25 15:45:07 -030096struct adv7604_chip_info {
97 enum adv7604_type type;
98
99 bool has_afe;
100 unsigned int max_port;
101 unsigned int num_dv_ports;
102
103 unsigned int edid_enable_reg;
104 unsigned int edid_status_reg;
105 unsigned int lcf_reg;
106
107 unsigned int cable_det_mask;
108 unsigned int tdms_lock_mask;
109 unsigned int fmt_change_digital_mask;
110
Laurent Pinchart539b33b2014-01-26 18:42:37 -0300111 const struct adv7604_format_info *formats;
112 unsigned int nformats;
113
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300114 void (*set_termination)(struct v4l2_subdev *sd, bool enable);
115 void (*setup_irqs)(struct v4l2_subdev *sd);
116 unsigned int (*read_hdmi_pixelclock)(struct v4l2_subdev *sd);
117 unsigned int (*read_cable_det)(struct v4l2_subdev *sd);
118
119 /* 0 = AFE, 1 = HDMI */
120 const struct adv7604_reg_seq *recommended_settings[2];
121 unsigned int num_recommended_settings[2];
122
123 unsigned long page_mask;
124};
125
Hans Verkuil54450f52012-07-18 05:45:16 -0300126/*
127 **********************************************************************
128 *
129 * Arrays with configuration parameters for the ADV7604
130 *
131 **********************************************************************
132 */
Laurent Pinchartc784b1e2014-01-29 10:08:58 -0300133
Hans Verkuil54450f52012-07-18 05:45:16 -0300134struct adv7604_state {
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300135 const struct adv7604_chip_info *info;
Hans Verkuil54450f52012-07-18 05:45:16 -0300136 struct adv7604_platform_data pdata;
Laurent Pinchart539b33b2014-01-26 18:42:37 -0300137
Hans Verkuil54450f52012-07-18 05:45:16 -0300138 struct v4l2_subdev sd;
Laurent Pinchartc784b1e2014-01-29 10:08:58 -0300139 struct media_pad pads[ADV7604_PAD_MAX];
140 unsigned int source_pad;
Laurent Pinchart539b33b2014-01-26 18:42:37 -0300141
Hans Verkuil54450f52012-07-18 05:45:16 -0300142 struct v4l2_ctrl_handler hdl;
Laurent Pinchart539b33b2014-01-26 18:42:37 -0300143
Laurent Pinchartc784b1e2014-01-29 10:08:58 -0300144 enum adv7604_pad selected_input;
Laurent Pinchart539b33b2014-01-26 18:42:37 -0300145
Hans Verkuil54450f52012-07-18 05:45:16 -0300146 struct v4l2_dv_timings timings;
Laurent Pinchart539b33b2014-01-26 18:42:37 -0300147 const struct adv7604_format_info *format;
148
Mats Randgaard4a31a932013-12-10 09:45:00 -0300149 struct {
150 u8 edid[256];
151 u32 present;
152 unsigned blocks;
153 } edid;
Mats Randgaarddd08beb2013-12-10 09:57:09 -0300154 u16 spa_port_a[2];
Hans Verkuil54450f52012-07-18 05:45:16 -0300155 struct v4l2_fract aspect_ratio;
156 u32 rgb_quantization_range;
157 struct workqueue_struct *work_queues;
158 struct delayed_work delayed_work_enable_hotplug;
Hans Verkuilcf9afb12012-10-16 10:12:55 -0300159 bool restart_stdi_once;
Hans Verkuil54450f52012-07-18 05:45:16 -0300160
161 /* i2c clients */
162 struct i2c_client *i2c_avlink;
163 struct i2c_client *i2c_cec;
164 struct i2c_client *i2c_infoframe;
165 struct i2c_client *i2c_esdp;
166 struct i2c_client *i2c_dpp;
167 struct i2c_client *i2c_afe;
168 struct i2c_client *i2c_repeater;
169 struct i2c_client *i2c_edid;
170 struct i2c_client *i2c_hdmi;
171 struct i2c_client *i2c_test;
172 struct i2c_client *i2c_cp;
173 struct i2c_client *i2c_vdp;
174
175 /* controls */
176 struct v4l2_ctrl *detect_tx_5v_ctrl;
177 struct v4l2_ctrl *analog_sampling_phase_ctrl;
178 struct v4l2_ctrl *free_run_color_manual_ctrl;
179 struct v4l2_ctrl *free_run_color_ctrl;
180 struct v4l2_ctrl *rgb_quantization_range_ctrl;
181};
182
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300183static bool adv7604_has_afe(struct adv7604_state *state)
184{
185 return state->info->has_afe;
186}
187
Hans Verkuil54450f52012-07-18 05:45:16 -0300188/* Supported CEA and DMT timings */
189static const struct v4l2_dv_timings adv7604_timings[] = {
190 V4L2_DV_BT_CEA_720X480P59_94,
191 V4L2_DV_BT_CEA_720X576P50,
192 V4L2_DV_BT_CEA_1280X720P24,
193 V4L2_DV_BT_CEA_1280X720P25,
Hans Verkuil54450f52012-07-18 05:45:16 -0300194 V4L2_DV_BT_CEA_1280X720P50,
195 V4L2_DV_BT_CEA_1280X720P60,
196 V4L2_DV_BT_CEA_1920X1080P24,
197 V4L2_DV_BT_CEA_1920X1080P25,
198 V4L2_DV_BT_CEA_1920X1080P30,
199 V4L2_DV_BT_CEA_1920X1080P50,
200 V4L2_DV_BT_CEA_1920X1080P60,
201
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300202 /* sorted by DMT ID */
Hans Verkuil54450f52012-07-18 05:45:16 -0300203 V4L2_DV_BT_DMT_640X350P85,
204 V4L2_DV_BT_DMT_640X400P85,
205 V4L2_DV_BT_DMT_720X400P85,
206 V4L2_DV_BT_DMT_640X480P60,
207 V4L2_DV_BT_DMT_640X480P72,
208 V4L2_DV_BT_DMT_640X480P75,
209 V4L2_DV_BT_DMT_640X480P85,
210 V4L2_DV_BT_DMT_800X600P56,
211 V4L2_DV_BT_DMT_800X600P60,
212 V4L2_DV_BT_DMT_800X600P72,
213 V4L2_DV_BT_DMT_800X600P75,
214 V4L2_DV_BT_DMT_800X600P85,
215 V4L2_DV_BT_DMT_848X480P60,
216 V4L2_DV_BT_DMT_1024X768P60,
217 V4L2_DV_BT_DMT_1024X768P70,
218 V4L2_DV_BT_DMT_1024X768P75,
219 V4L2_DV_BT_DMT_1024X768P85,
220 V4L2_DV_BT_DMT_1152X864P75,
221 V4L2_DV_BT_DMT_1280X768P60_RB,
222 V4L2_DV_BT_DMT_1280X768P60,
223 V4L2_DV_BT_DMT_1280X768P75,
224 V4L2_DV_BT_DMT_1280X768P85,
225 V4L2_DV_BT_DMT_1280X800P60_RB,
226 V4L2_DV_BT_DMT_1280X800P60,
227 V4L2_DV_BT_DMT_1280X800P75,
228 V4L2_DV_BT_DMT_1280X800P85,
229 V4L2_DV_BT_DMT_1280X960P60,
230 V4L2_DV_BT_DMT_1280X960P85,
231 V4L2_DV_BT_DMT_1280X1024P60,
232 V4L2_DV_BT_DMT_1280X1024P75,
233 V4L2_DV_BT_DMT_1280X1024P85,
234 V4L2_DV_BT_DMT_1360X768P60,
235 V4L2_DV_BT_DMT_1400X1050P60_RB,
236 V4L2_DV_BT_DMT_1400X1050P60,
237 V4L2_DV_BT_DMT_1400X1050P75,
238 V4L2_DV_BT_DMT_1400X1050P85,
239 V4L2_DV_BT_DMT_1440X900P60_RB,
240 V4L2_DV_BT_DMT_1440X900P60,
241 V4L2_DV_BT_DMT_1600X1200P60,
242 V4L2_DV_BT_DMT_1680X1050P60_RB,
243 V4L2_DV_BT_DMT_1680X1050P60,
244 V4L2_DV_BT_DMT_1792X1344P60,
245 V4L2_DV_BT_DMT_1856X1392P60,
246 V4L2_DV_BT_DMT_1920X1200P60_RB,
Martin Bugge547ed542013-12-05 10:01:17 -0300247 V4L2_DV_BT_DMT_1366X768P60_RB,
Hans Verkuil54450f52012-07-18 05:45:16 -0300248 V4L2_DV_BT_DMT_1366X768P60,
249 V4L2_DV_BT_DMT_1920X1080P60,
250 { },
251};
252
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300253struct adv7604_video_standards {
254 struct v4l2_dv_timings timings;
255 u8 vid_std;
256 u8 v_freq;
257};
258
259/* sorted by number of lines */
260static const struct adv7604_video_standards adv7604_prim_mode_comp[] = {
261 /* { V4L2_DV_BT_CEA_720X480P59_94, 0x0a, 0x00 }, TODO flickering */
262 { V4L2_DV_BT_CEA_720X576P50, 0x0b, 0x00 },
263 { V4L2_DV_BT_CEA_1280X720P50, 0x19, 0x01 },
264 { V4L2_DV_BT_CEA_1280X720P60, 0x19, 0x00 },
265 { V4L2_DV_BT_CEA_1920X1080P24, 0x1e, 0x04 },
266 { V4L2_DV_BT_CEA_1920X1080P25, 0x1e, 0x03 },
267 { V4L2_DV_BT_CEA_1920X1080P30, 0x1e, 0x02 },
268 { V4L2_DV_BT_CEA_1920X1080P50, 0x1e, 0x01 },
269 { V4L2_DV_BT_CEA_1920X1080P60, 0x1e, 0x00 },
270 /* TODO add 1920x1080P60_RB (CVT timing) */
271 { },
272};
273
274/* sorted by number of lines */
275static const struct adv7604_video_standards adv7604_prim_mode_gr[] = {
276 { V4L2_DV_BT_DMT_640X480P60, 0x08, 0x00 },
277 { V4L2_DV_BT_DMT_640X480P72, 0x09, 0x00 },
278 { V4L2_DV_BT_DMT_640X480P75, 0x0a, 0x00 },
279 { V4L2_DV_BT_DMT_640X480P85, 0x0b, 0x00 },
280 { V4L2_DV_BT_DMT_800X600P56, 0x00, 0x00 },
281 { V4L2_DV_BT_DMT_800X600P60, 0x01, 0x00 },
282 { V4L2_DV_BT_DMT_800X600P72, 0x02, 0x00 },
283 { V4L2_DV_BT_DMT_800X600P75, 0x03, 0x00 },
284 { V4L2_DV_BT_DMT_800X600P85, 0x04, 0x00 },
285 { V4L2_DV_BT_DMT_1024X768P60, 0x0c, 0x00 },
286 { V4L2_DV_BT_DMT_1024X768P70, 0x0d, 0x00 },
287 { V4L2_DV_BT_DMT_1024X768P75, 0x0e, 0x00 },
288 { V4L2_DV_BT_DMT_1024X768P85, 0x0f, 0x00 },
289 { V4L2_DV_BT_DMT_1280X1024P60, 0x05, 0x00 },
290 { V4L2_DV_BT_DMT_1280X1024P75, 0x06, 0x00 },
291 { V4L2_DV_BT_DMT_1360X768P60, 0x12, 0x00 },
292 { V4L2_DV_BT_DMT_1366X768P60, 0x13, 0x00 },
293 { V4L2_DV_BT_DMT_1400X1050P60, 0x14, 0x00 },
294 { V4L2_DV_BT_DMT_1400X1050P75, 0x15, 0x00 },
295 { V4L2_DV_BT_DMT_1600X1200P60, 0x16, 0x00 }, /* TODO not tested */
296 /* TODO add 1600X1200P60_RB (not a DMT timing) */
297 { V4L2_DV_BT_DMT_1680X1050P60, 0x18, 0x00 },
298 { V4L2_DV_BT_DMT_1920X1200P60_RB, 0x19, 0x00 }, /* TODO not tested */
299 { },
300};
301
302/* sorted by number of lines */
303static const struct adv7604_video_standards adv7604_prim_mode_hdmi_comp[] = {
304 { V4L2_DV_BT_CEA_720X480P59_94, 0x0a, 0x00 },
305 { V4L2_DV_BT_CEA_720X576P50, 0x0b, 0x00 },
306 { V4L2_DV_BT_CEA_1280X720P50, 0x13, 0x01 },
307 { V4L2_DV_BT_CEA_1280X720P60, 0x13, 0x00 },
308 { V4L2_DV_BT_CEA_1920X1080P24, 0x1e, 0x04 },
309 { V4L2_DV_BT_CEA_1920X1080P25, 0x1e, 0x03 },
310 { V4L2_DV_BT_CEA_1920X1080P30, 0x1e, 0x02 },
311 { V4L2_DV_BT_CEA_1920X1080P50, 0x1e, 0x01 },
312 { V4L2_DV_BT_CEA_1920X1080P60, 0x1e, 0x00 },
313 { },
314};
315
316/* sorted by number of lines */
317static const struct adv7604_video_standards adv7604_prim_mode_hdmi_gr[] = {
318 { V4L2_DV_BT_DMT_640X480P60, 0x08, 0x00 },
319 { V4L2_DV_BT_DMT_640X480P72, 0x09, 0x00 },
320 { V4L2_DV_BT_DMT_640X480P75, 0x0a, 0x00 },
321 { V4L2_DV_BT_DMT_640X480P85, 0x0b, 0x00 },
322 { V4L2_DV_BT_DMT_800X600P56, 0x00, 0x00 },
323 { V4L2_DV_BT_DMT_800X600P60, 0x01, 0x00 },
324 { V4L2_DV_BT_DMT_800X600P72, 0x02, 0x00 },
325 { V4L2_DV_BT_DMT_800X600P75, 0x03, 0x00 },
326 { V4L2_DV_BT_DMT_800X600P85, 0x04, 0x00 },
327 { V4L2_DV_BT_DMT_1024X768P60, 0x0c, 0x00 },
328 { V4L2_DV_BT_DMT_1024X768P70, 0x0d, 0x00 },
329 { V4L2_DV_BT_DMT_1024X768P75, 0x0e, 0x00 },
330 { V4L2_DV_BT_DMT_1024X768P85, 0x0f, 0x00 },
331 { V4L2_DV_BT_DMT_1280X1024P60, 0x05, 0x00 },
332 { V4L2_DV_BT_DMT_1280X1024P75, 0x06, 0x00 },
333 { },
334};
335
Hans Verkuil54450f52012-07-18 05:45:16 -0300336/* ----------------------------------------------------------------------- */
337
338static inline struct adv7604_state *to_state(struct v4l2_subdev *sd)
339{
340 return container_of(sd, struct adv7604_state, sd);
341}
342
343static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
344{
345 return &container_of(ctrl->handler, struct adv7604_state, hdl)->sd;
346}
347
348static inline unsigned hblanking(const struct v4l2_bt_timings *t)
349{
Hans Verkuileacf8f92013-07-29 08:40:59 -0300350 return V4L2_DV_BT_BLANKING_WIDTH(t);
Hans Verkuil54450f52012-07-18 05:45:16 -0300351}
352
353static inline unsigned htotal(const struct v4l2_bt_timings *t)
354{
Hans Verkuileacf8f92013-07-29 08:40:59 -0300355 return V4L2_DV_BT_FRAME_WIDTH(t);
Hans Verkuil54450f52012-07-18 05:45:16 -0300356}
357
358static inline unsigned vblanking(const struct v4l2_bt_timings *t)
359{
Hans Verkuileacf8f92013-07-29 08:40:59 -0300360 return V4L2_DV_BT_BLANKING_HEIGHT(t);
Hans Verkuil54450f52012-07-18 05:45:16 -0300361}
362
363static inline unsigned vtotal(const struct v4l2_bt_timings *t)
364{
Hans Verkuileacf8f92013-07-29 08:40:59 -0300365 return V4L2_DV_BT_FRAME_HEIGHT(t);
Hans Verkuil54450f52012-07-18 05:45:16 -0300366}
367
368/* ----------------------------------------------------------------------- */
369
370static s32 adv_smbus_read_byte_data_check(struct i2c_client *client,
371 u8 command, bool check)
372{
373 union i2c_smbus_data data;
374
375 if (!i2c_smbus_xfer(client->adapter, client->addr, client->flags,
376 I2C_SMBUS_READ, command,
377 I2C_SMBUS_BYTE_DATA, &data))
378 return data.byte;
379 if (check)
380 v4l_err(client, "error reading %02x, %02x\n",
381 client->addr, command);
382 return -EIO;
383}
384
385static s32 adv_smbus_read_byte_data(struct i2c_client *client, u8 command)
386{
387 return adv_smbus_read_byte_data_check(client, command, true);
388}
389
390static s32 adv_smbus_write_byte_data(struct i2c_client *client,
391 u8 command, u8 value)
392{
393 union i2c_smbus_data data;
394 int err;
395 int i;
396
397 data.byte = value;
398 for (i = 0; i < 3; i++) {
399 err = i2c_smbus_xfer(client->adapter, client->addr,
400 client->flags,
401 I2C_SMBUS_WRITE, command,
402 I2C_SMBUS_BYTE_DATA, &data);
403 if (!err)
404 break;
405 }
406 if (err < 0)
407 v4l_err(client, "error writing %02x, %02x, %02x\n",
408 client->addr, command, value);
409 return err;
410}
411
412static s32 adv_smbus_write_i2c_block_data(struct i2c_client *client,
413 u8 command, unsigned length, const u8 *values)
414{
415 union i2c_smbus_data data;
416
417 if (length > I2C_SMBUS_BLOCK_MAX)
418 length = I2C_SMBUS_BLOCK_MAX;
419 data.block[0] = length;
420 memcpy(data.block + 1, values, length);
421 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
422 I2C_SMBUS_WRITE, command,
423 I2C_SMBUS_I2C_BLOCK_DATA, &data);
424}
425
426/* ----------------------------------------------------------------------- */
427
428static inline int io_read(struct v4l2_subdev *sd, u8 reg)
429{
430 struct i2c_client *client = v4l2_get_subdevdata(sd);
431
432 return adv_smbus_read_byte_data(client, reg);
433}
434
435static inline int io_write(struct v4l2_subdev *sd, u8 reg, u8 val)
436{
437 struct i2c_client *client = v4l2_get_subdevdata(sd);
438
439 return adv_smbus_write_byte_data(client, reg, val);
440}
441
442static inline int io_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
443{
444 return io_write(sd, reg, (io_read(sd, reg) & mask) | val);
445}
446
447static inline int avlink_read(struct v4l2_subdev *sd, u8 reg)
448{
449 struct adv7604_state *state = to_state(sd);
450
451 return adv_smbus_read_byte_data(state->i2c_avlink, reg);
452}
453
454static inline int avlink_write(struct v4l2_subdev *sd, u8 reg, u8 val)
455{
456 struct adv7604_state *state = to_state(sd);
457
458 return adv_smbus_write_byte_data(state->i2c_avlink, reg, val);
459}
460
461static inline int cec_read(struct v4l2_subdev *sd, u8 reg)
462{
463 struct adv7604_state *state = to_state(sd);
464
465 return adv_smbus_read_byte_data(state->i2c_cec, reg);
466}
467
468static inline int cec_write(struct v4l2_subdev *sd, u8 reg, u8 val)
469{
470 struct adv7604_state *state = to_state(sd);
471
472 return adv_smbus_write_byte_data(state->i2c_cec, reg, val);
473}
474
475static inline int cec_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
476{
477 return cec_write(sd, reg, (cec_read(sd, reg) & mask) | val);
478}
479
480static inline int infoframe_read(struct v4l2_subdev *sd, u8 reg)
481{
482 struct adv7604_state *state = to_state(sd);
483
484 return adv_smbus_read_byte_data(state->i2c_infoframe, reg);
485}
486
487static inline int infoframe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
488{
489 struct adv7604_state *state = to_state(sd);
490
491 return adv_smbus_write_byte_data(state->i2c_infoframe, reg, val);
492}
493
494static inline int esdp_read(struct v4l2_subdev *sd, u8 reg)
495{
496 struct adv7604_state *state = to_state(sd);
497
498 return adv_smbus_read_byte_data(state->i2c_esdp, reg);
499}
500
501static inline int esdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
502{
503 struct adv7604_state *state = to_state(sd);
504
505 return adv_smbus_write_byte_data(state->i2c_esdp, reg, val);
506}
507
508static inline int dpp_read(struct v4l2_subdev *sd, u8 reg)
509{
510 struct adv7604_state *state = to_state(sd);
511
512 return adv_smbus_read_byte_data(state->i2c_dpp, reg);
513}
514
515static inline int dpp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
516{
517 struct adv7604_state *state = to_state(sd);
518
519 return adv_smbus_write_byte_data(state->i2c_dpp, reg, val);
520}
521
522static inline int afe_read(struct v4l2_subdev *sd, u8 reg)
523{
524 struct adv7604_state *state = to_state(sd);
525
526 return adv_smbus_read_byte_data(state->i2c_afe, reg);
527}
528
529static inline int afe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
530{
531 struct adv7604_state *state = to_state(sd);
532
533 return adv_smbus_write_byte_data(state->i2c_afe, reg, val);
534}
535
536static inline int rep_read(struct v4l2_subdev *sd, u8 reg)
537{
538 struct adv7604_state *state = to_state(sd);
539
540 return adv_smbus_read_byte_data(state->i2c_repeater, reg);
541}
542
543static inline int rep_write(struct v4l2_subdev *sd, u8 reg, u8 val)
544{
545 struct adv7604_state *state = to_state(sd);
546
547 return adv_smbus_write_byte_data(state->i2c_repeater, reg, val);
548}
549
550static inline int rep_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
551{
552 return rep_write(sd, reg, (rep_read(sd, reg) & mask) | val);
553}
554
555static inline int edid_read(struct v4l2_subdev *sd, u8 reg)
556{
557 struct adv7604_state *state = to_state(sd);
558
559 return adv_smbus_read_byte_data(state->i2c_edid, reg);
560}
561
562static inline int edid_write(struct v4l2_subdev *sd, u8 reg, u8 val)
563{
564 struct adv7604_state *state = to_state(sd);
565
566 return adv_smbus_write_byte_data(state->i2c_edid, reg, val);
567}
568
569static inline int edid_read_block(struct v4l2_subdev *sd, unsigned len, u8 *val)
570{
571 struct adv7604_state *state = to_state(sd);
572 struct i2c_client *client = state->i2c_edid;
573 u8 msgbuf0[1] = { 0 };
574 u8 msgbuf1[256];
Shubhrajyoti D09f29672012-10-25 01:02:36 -0300575 struct i2c_msg msg[2] = {
576 {
577 .addr = client->addr,
578 .len = 1,
579 .buf = msgbuf0
580 },
581 {
582 .addr = client->addr,
583 .flags = I2C_M_RD,
584 .len = len,
585 .buf = msgbuf1
586 },
587 };
Hans Verkuil54450f52012-07-18 05:45:16 -0300588
589 if (i2c_transfer(client->adapter, msg, 2) < 0)
590 return -EIO;
591 memcpy(val, msgbuf1, len);
592 return 0;
593}
594
Mats Randgaarddd08beb2013-12-10 09:57:09 -0300595static inline int edid_write_block(struct v4l2_subdev *sd,
596 unsigned len, const u8 *val)
597{
598 struct adv7604_state *state = to_state(sd);
599 int err = 0;
600 int i;
601
602 v4l2_dbg(2, debug, sd, "%s: write EDID block (%d byte)\n", __func__, len);
603
604 for (i = 0; !err && i < len; i += I2C_SMBUS_BLOCK_MAX)
605 err = adv_smbus_write_i2c_block_data(state->i2c_edid, i,
606 I2C_SMBUS_BLOCK_MAX, val + i);
607 return err;
608}
609
Hans Verkuil54450f52012-07-18 05:45:16 -0300610static void adv7604_delayed_work_enable_hotplug(struct work_struct *work)
611{
612 struct delayed_work *dwork = to_delayed_work(work);
613 struct adv7604_state *state = container_of(dwork, struct adv7604_state,
614 delayed_work_enable_hotplug);
615 struct v4l2_subdev *sd = &state->sd;
616
617 v4l2_dbg(2, debug, sd, "%s: enable hotplug\n", __func__);
618
Mats Randgaard4a31a932013-12-10 09:45:00 -0300619 v4l2_subdev_notify(sd, ADV7604_HOTPLUG, (void *)&state->edid.present);
Hans Verkuil54450f52012-07-18 05:45:16 -0300620}
621
Hans Verkuil54450f52012-07-18 05:45:16 -0300622static inline int hdmi_read(struct v4l2_subdev *sd, u8 reg)
623{
624 struct adv7604_state *state = to_state(sd);
625
626 return adv_smbus_read_byte_data(state->i2c_hdmi, reg);
627}
628
Laurent Pinchart51182a92014-01-08 19:30:37 -0300629static u16 hdmi_read16(struct v4l2_subdev *sd, u8 reg, u16 mask)
630{
631 return ((hdmi_read(sd, reg) << 8) | hdmi_read(sd, reg + 1)) & mask;
632}
633
Hans Verkuil54450f52012-07-18 05:45:16 -0300634static inline int hdmi_write(struct v4l2_subdev *sd, u8 reg, u8 val)
635{
636 struct adv7604_state *state = to_state(sd);
637
638 return adv_smbus_write_byte_data(state->i2c_hdmi, reg, val);
639}
640
Mats Randgaard4a31a932013-12-10 09:45:00 -0300641static inline int hdmi_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
642{
643 return hdmi_write(sd, reg, (hdmi_read(sd, reg) & mask) | val);
644}
645
Hans Verkuil54450f52012-07-18 05:45:16 -0300646static inline int test_read(struct v4l2_subdev *sd, u8 reg)
647{
648 struct adv7604_state *state = to_state(sd);
649
650 return adv_smbus_read_byte_data(state->i2c_test, reg);
651}
652
653static inline int test_write(struct v4l2_subdev *sd, u8 reg, u8 val)
654{
655 struct adv7604_state *state = to_state(sd);
656
657 return adv_smbus_write_byte_data(state->i2c_test, reg, val);
658}
659
660static inline int cp_read(struct v4l2_subdev *sd, u8 reg)
661{
662 struct adv7604_state *state = to_state(sd);
663
664 return adv_smbus_read_byte_data(state->i2c_cp, reg);
665}
666
Laurent Pinchart51182a92014-01-08 19:30:37 -0300667static u16 cp_read16(struct v4l2_subdev *sd, u8 reg, u16 mask)
668{
669 return ((cp_read(sd, reg) << 8) | cp_read(sd, reg + 1)) & mask;
670}
671
Hans Verkuil54450f52012-07-18 05:45:16 -0300672static inline int cp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
673{
674 struct adv7604_state *state = to_state(sd);
675
676 return adv_smbus_write_byte_data(state->i2c_cp, reg, val);
677}
678
679static inline int cp_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
680{
681 return cp_write(sd, reg, (cp_read(sd, reg) & mask) | val);
682}
683
684static inline int vdp_read(struct v4l2_subdev *sd, u8 reg)
685{
686 struct adv7604_state *state = to_state(sd);
687
688 return adv_smbus_read_byte_data(state->i2c_vdp, reg);
689}
690
691static inline int vdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
692{
693 struct adv7604_state *state = to_state(sd);
694
695 return adv_smbus_write_byte_data(state->i2c_vdp, reg, val);
696}
697
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300698enum {
699 ADV7604_PAGE_IO,
700 ADV7604_PAGE_AVLINK,
701 ADV7604_PAGE_CEC,
702 ADV7604_PAGE_INFOFRAME,
703 ADV7604_PAGE_ESDP,
704 ADV7604_PAGE_DPP,
705 ADV7604_PAGE_AFE,
706 ADV7604_PAGE_REP,
707 ADV7604_PAGE_EDID,
708 ADV7604_PAGE_HDMI,
709 ADV7604_PAGE_TEST,
710 ADV7604_PAGE_CP,
711 ADV7604_PAGE_VDP,
712 ADV7604_PAGE_TERM,
713};
714
715#define ADV7604_REG(page, offset) (((page) << 8) | (offset))
716#define ADV7604_REG_SEQ_TERM 0xffff
717
718#ifdef CONFIG_VIDEO_ADV_DEBUG
719static int adv7604_read_reg(struct v4l2_subdev *sd, unsigned int reg)
720{
721 struct adv7604_state *state = to_state(sd);
722 unsigned int page = reg >> 8;
723
724 if (!(BIT(page) & state->info->page_mask))
725 return -EINVAL;
726
727 reg &= 0xff;
728
729 switch (page) {
730 case ADV7604_PAGE_IO:
731 return io_read(sd, reg);
732 case ADV7604_PAGE_AVLINK:
733 return avlink_read(sd, reg);
734 case ADV7604_PAGE_CEC:
735 return cec_read(sd, reg);
736 case ADV7604_PAGE_INFOFRAME:
737 return infoframe_read(sd, reg);
738 case ADV7604_PAGE_ESDP:
739 return esdp_read(sd, reg);
740 case ADV7604_PAGE_DPP:
741 return dpp_read(sd, reg);
742 case ADV7604_PAGE_AFE:
743 return afe_read(sd, reg);
744 case ADV7604_PAGE_REP:
745 return rep_read(sd, reg);
746 case ADV7604_PAGE_EDID:
747 return edid_read(sd, reg);
748 case ADV7604_PAGE_HDMI:
749 return hdmi_read(sd, reg);
750 case ADV7604_PAGE_TEST:
751 return test_read(sd, reg);
752 case ADV7604_PAGE_CP:
753 return cp_read(sd, reg);
754 case ADV7604_PAGE_VDP:
755 return vdp_read(sd, reg);
756 }
757
758 return -EINVAL;
759}
760#endif
761
762static int adv7604_write_reg(struct v4l2_subdev *sd, unsigned int reg, u8 val)
763{
764 struct adv7604_state *state = to_state(sd);
765 unsigned int page = reg >> 8;
766
767 if (!(BIT(page) & state->info->page_mask))
768 return -EINVAL;
769
770 reg &= 0xff;
771
772 switch (page) {
773 case ADV7604_PAGE_IO:
774 return io_write(sd, reg, val);
775 case ADV7604_PAGE_AVLINK:
776 return avlink_write(sd, reg, val);
777 case ADV7604_PAGE_CEC:
778 return cec_write(sd, reg, val);
779 case ADV7604_PAGE_INFOFRAME:
780 return infoframe_write(sd, reg, val);
781 case ADV7604_PAGE_ESDP:
782 return esdp_write(sd, reg, val);
783 case ADV7604_PAGE_DPP:
784 return dpp_write(sd, reg, val);
785 case ADV7604_PAGE_AFE:
786 return afe_write(sd, reg, val);
787 case ADV7604_PAGE_REP:
788 return rep_write(sd, reg, val);
789 case ADV7604_PAGE_EDID:
790 return edid_write(sd, reg, val);
791 case ADV7604_PAGE_HDMI:
792 return hdmi_write(sd, reg, val);
793 case ADV7604_PAGE_TEST:
794 return test_write(sd, reg, val);
795 case ADV7604_PAGE_CP:
796 return cp_write(sd, reg, val);
797 case ADV7604_PAGE_VDP:
798 return vdp_write(sd, reg, val);
799 }
800
801 return -EINVAL;
802}
803
804static void adv7604_write_reg_seq(struct v4l2_subdev *sd,
805 const struct adv7604_reg_seq *reg_seq)
806{
807 unsigned int i;
808
809 for (i = 0; reg_seq[i].reg != ADV7604_REG_SEQ_TERM; i++)
810 adv7604_write_reg(sd, reg_seq[i].reg, reg_seq[i].val);
811}
812
Laurent Pinchart539b33b2014-01-26 18:42:37 -0300813/* -----------------------------------------------------------------------------
814 * Format helpers
815 */
816
817static const struct adv7604_format_info adv7604_formats[] = {
818 { V4L2_MBUS_FMT_RGB888_1X24, ADV7604_OP_CH_SEL_RGB, true, false,
819 ADV7604_OP_MODE_SEL_SDR_444 | ADV7604_OP_FORMAT_SEL_8BIT },
820 { V4L2_MBUS_FMT_YUYV8_2X8, ADV7604_OP_CH_SEL_RGB, false, false,
821 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_8BIT },
822 { V4L2_MBUS_FMT_YVYU8_2X8, ADV7604_OP_CH_SEL_RGB, false, true,
823 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_8BIT },
824 { V4L2_MBUS_FMT_YUYV10_2X10, ADV7604_OP_CH_SEL_RGB, false, false,
825 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_10BIT },
826 { V4L2_MBUS_FMT_YVYU10_2X10, ADV7604_OP_CH_SEL_RGB, false, true,
827 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_10BIT },
828 { V4L2_MBUS_FMT_YUYV12_2X12, ADV7604_OP_CH_SEL_RGB, false, false,
829 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_12BIT },
830 { V4L2_MBUS_FMT_YVYU12_2X12, ADV7604_OP_CH_SEL_RGB, false, true,
831 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_12BIT },
832 { V4L2_MBUS_FMT_UYVY8_1X16, ADV7604_OP_CH_SEL_RBG, false, false,
833 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
834 { V4L2_MBUS_FMT_VYUY8_1X16, ADV7604_OP_CH_SEL_RBG, false, true,
835 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
836 { V4L2_MBUS_FMT_YUYV8_1X16, ADV7604_OP_CH_SEL_RGB, false, false,
837 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
838 { V4L2_MBUS_FMT_YVYU8_1X16, ADV7604_OP_CH_SEL_RGB, false, true,
839 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
840 { V4L2_MBUS_FMT_UYVY10_1X20, ADV7604_OP_CH_SEL_RBG, false, false,
841 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_10BIT },
842 { V4L2_MBUS_FMT_VYUY10_1X20, ADV7604_OP_CH_SEL_RBG, false, true,
843 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_10BIT },
844 { V4L2_MBUS_FMT_YUYV10_1X20, ADV7604_OP_CH_SEL_RGB, false, false,
845 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_10BIT },
846 { V4L2_MBUS_FMT_YVYU10_1X20, ADV7604_OP_CH_SEL_RGB, false, true,
847 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_10BIT },
848 { V4L2_MBUS_FMT_UYVY12_1X24, ADV7604_OP_CH_SEL_RBG, false, false,
849 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
850 { V4L2_MBUS_FMT_VYUY12_1X24, ADV7604_OP_CH_SEL_RBG, false, true,
851 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
852 { V4L2_MBUS_FMT_YUYV12_1X24, ADV7604_OP_CH_SEL_RGB, false, false,
853 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
854 { V4L2_MBUS_FMT_YVYU12_1X24, ADV7604_OP_CH_SEL_RGB, false, true,
855 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
856};
857
858static const struct adv7604_format_info adv7611_formats[] = {
859 { V4L2_MBUS_FMT_RGB888_1X24, ADV7604_OP_CH_SEL_RGB, true, false,
860 ADV7604_OP_MODE_SEL_SDR_444 | ADV7604_OP_FORMAT_SEL_8BIT },
861 { V4L2_MBUS_FMT_YUYV8_2X8, ADV7604_OP_CH_SEL_RGB, false, false,
862 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_8BIT },
863 { V4L2_MBUS_FMT_YVYU8_2X8, ADV7604_OP_CH_SEL_RGB, false, true,
864 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_8BIT },
865 { V4L2_MBUS_FMT_YUYV12_2X12, ADV7604_OP_CH_SEL_RGB, false, false,
866 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_12BIT },
867 { V4L2_MBUS_FMT_YVYU12_2X12, ADV7604_OP_CH_SEL_RGB, false, true,
868 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_12BIT },
869 { V4L2_MBUS_FMT_UYVY8_1X16, ADV7604_OP_CH_SEL_RBG, false, false,
870 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
871 { V4L2_MBUS_FMT_VYUY8_1X16, ADV7604_OP_CH_SEL_RBG, false, true,
872 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
873 { V4L2_MBUS_FMT_YUYV8_1X16, ADV7604_OP_CH_SEL_RGB, false, false,
874 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
875 { V4L2_MBUS_FMT_YVYU8_1X16, ADV7604_OP_CH_SEL_RGB, false, true,
876 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
877 { V4L2_MBUS_FMT_UYVY12_1X24, ADV7604_OP_CH_SEL_RBG, false, false,
878 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
879 { V4L2_MBUS_FMT_VYUY12_1X24, ADV7604_OP_CH_SEL_RBG, false, true,
880 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
881 { V4L2_MBUS_FMT_YUYV12_1X24, ADV7604_OP_CH_SEL_RGB, false, false,
882 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
883 { V4L2_MBUS_FMT_YVYU12_1X24, ADV7604_OP_CH_SEL_RGB, false, true,
884 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
885};
886
887static const struct adv7604_format_info *
888adv7604_format_info(struct adv7604_state *state, enum v4l2_mbus_pixelcode code)
889{
890 unsigned int i;
891
892 for (i = 0; i < state->info->nformats; ++i) {
893 if (state->info->formats[i].code == code)
894 return &state->info->formats[i];
895 }
896
897 return NULL;
898}
899
Hans Verkuil54450f52012-07-18 05:45:16 -0300900/* ----------------------------------------------------------------------- */
901
Mats Randgaard4a31a932013-12-10 09:45:00 -0300902static inline bool is_analog_input(struct v4l2_subdev *sd)
903{
904 struct adv7604_state *state = to_state(sd);
905
Laurent Pinchartc784b1e2014-01-29 10:08:58 -0300906 return state->selected_input == ADV7604_PAD_VGA_RGB ||
907 state->selected_input == ADV7604_PAD_VGA_COMP;
Mats Randgaard4a31a932013-12-10 09:45:00 -0300908}
909
910static inline bool is_digital_input(struct v4l2_subdev *sd)
911{
912 struct adv7604_state *state = to_state(sd);
913
Laurent Pinchartc784b1e2014-01-29 10:08:58 -0300914 return state->selected_input == ADV7604_PAD_HDMI_PORT_A ||
915 state->selected_input == ADV7604_PAD_HDMI_PORT_B ||
916 state->selected_input == ADV7604_PAD_HDMI_PORT_C ||
917 state->selected_input == ADV7604_PAD_HDMI_PORT_D;
Mats Randgaard4a31a932013-12-10 09:45:00 -0300918}
919
920/* ----------------------------------------------------------------------- */
921
Hans Verkuil54450f52012-07-18 05:45:16 -0300922#ifdef CONFIG_VIDEO_ADV_DEBUG
923static void adv7604_inv_register(struct v4l2_subdev *sd)
924{
925 v4l2_info(sd, "0x000-0x0ff: IO Map\n");
926 v4l2_info(sd, "0x100-0x1ff: AVLink Map\n");
927 v4l2_info(sd, "0x200-0x2ff: CEC Map\n");
928 v4l2_info(sd, "0x300-0x3ff: InfoFrame Map\n");
929 v4l2_info(sd, "0x400-0x4ff: ESDP Map\n");
930 v4l2_info(sd, "0x500-0x5ff: DPP Map\n");
931 v4l2_info(sd, "0x600-0x6ff: AFE Map\n");
932 v4l2_info(sd, "0x700-0x7ff: Repeater Map\n");
933 v4l2_info(sd, "0x800-0x8ff: EDID Map\n");
934 v4l2_info(sd, "0x900-0x9ff: HDMI Map\n");
935 v4l2_info(sd, "0xa00-0xaff: Test Map\n");
936 v4l2_info(sd, "0xb00-0xbff: CP Map\n");
937 v4l2_info(sd, "0xc00-0xcff: VDP Map\n");
938}
939
940static int adv7604_g_register(struct v4l2_subdev *sd,
941 struct v4l2_dbg_register *reg)
942{
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300943 int ret;
944
945 ret = adv7604_read_reg(sd, reg->reg);
946 if (ret < 0) {
Hans Verkuil54450f52012-07-18 05:45:16 -0300947 v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
948 adv7604_inv_register(sd);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300949 return ret;
Hans Verkuil54450f52012-07-18 05:45:16 -0300950 }
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300951
952 reg->size = 1;
953 reg->val = ret;
954
Hans Verkuil54450f52012-07-18 05:45:16 -0300955 return 0;
956}
957
958static int adv7604_s_register(struct v4l2_subdev *sd,
Hans Verkuil977ba3b2013-03-24 08:28:46 -0300959 const struct v4l2_dbg_register *reg)
Hans Verkuil54450f52012-07-18 05:45:16 -0300960{
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300961 int ret;
Hans Verkuil15774612013-12-10 10:02:43 -0300962
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300963 ret = adv7604_write_reg(sd, reg->reg, reg->val);
964 if (ret < 0) {
Hans Verkuil54450f52012-07-18 05:45:16 -0300965 v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
966 adv7604_inv_register(sd);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300967 return ret;
Hans Verkuil54450f52012-07-18 05:45:16 -0300968 }
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300969
Hans Verkuil54450f52012-07-18 05:45:16 -0300970 return 0;
971}
972#endif
973
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300974static unsigned int adv7604_read_cable_det(struct v4l2_subdev *sd)
975{
976 u8 value = io_read(sd, 0x6f);
977
978 return ((value & 0x10) >> 4)
979 | ((value & 0x08) >> 2)
980 | ((value & 0x04) << 0)
981 | ((value & 0x02) << 2);
982}
983
984static unsigned int adv7611_read_cable_det(struct v4l2_subdev *sd)
985{
986 u8 value = io_read(sd, 0x6f);
987
988 return value & 1;
989}
990
Hans Verkuil54450f52012-07-18 05:45:16 -0300991static int adv7604_s_detect_tx_5v_ctrl(struct v4l2_subdev *sd)
992{
993 struct adv7604_state *state = to_state(sd);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300994 const struct adv7604_chip_info *info = state->info;
Hans Verkuil54450f52012-07-18 05:45:16 -0300995
Hans Verkuil54450f52012-07-18 05:45:16 -0300996 return v4l2_ctrl_s_ctrl(state->detect_tx_5v_ctrl,
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300997 info->read_cable_det(sd));
Hans Verkuil54450f52012-07-18 05:45:16 -0300998}
999
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001000static int find_and_set_predefined_video_timings(struct v4l2_subdev *sd,
1001 u8 prim_mode,
1002 const struct adv7604_video_standards *predef_vid_timings,
1003 const struct v4l2_dv_timings *timings)
Hans Verkuil54450f52012-07-18 05:45:16 -03001004{
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001005 int i;
1006
1007 for (i = 0; predef_vid_timings[i].timings.bt.width; i++) {
Hans Verkuilef1ed8f2013-08-15 08:28:47 -03001008 if (!v4l2_match_dv_timings(timings, &predef_vid_timings[i].timings,
Mats Randgaard4a31a932013-12-10 09:45:00 -03001009 is_digital_input(sd) ? 250000 : 1000000))
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001010 continue;
1011 io_write(sd, 0x00, predef_vid_timings[i].vid_std); /* video std */
1012 io_write(sd, 0x01, (predef_vid_timings[i].v_freq << 4) +
1013 prim_mode); /* v_freq and prim mode */
1014 return 0;
1015 }
1016
1017 return -1;
1018}
1019
1020static int configure_predefined_video_timings(struct v4l2_subdev *sd,
1021 struct v4l2_dv_timings *timings)
1022{
1023 struct adv7604_state *state = to_state(sd);
1024 int err;
1025
1026 v4l2_dbg(1, debug, sd, "%s", __func__);
1027
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001028 if (adv7604_has_afe(state)) {
1029 /* reset to default values */
1030 io_write(sd, 0x16, 0x43);
1031 io_write(sd, 0x17, 0x5a);
1032 }
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001033 /* disable embedded syncs for auto graphics mode */
1034 cp_write_and_or(sd, 0x81, 0xef, 0x00);
1035 cp_write(sd, 0x8f, 0x00);
1036 cp_write(sd, 0x90, 0x00);
1037 cp_write(sd, 0xa2, 0x00);
1038 cp_write(sd, 0xa3, 0x00);
1039 cp_write(sd, 0xa4, 0x00);
1040 cp_write(sd, 0xa5, 0x00);
1041 cp_write(sd, 0xa6, 0x00);
1042 cp_write(sd, 0xa7, 0x00);
1043 cp_write(sd, 0xab, 0x00);
1044 cp_write(sd, 0xac, 0x00);
1045
Mats Randgaard4a31a932013-12-10 09:45:00 -03001046 if (is_analog_input(sd)) {
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001047 err = find_and_set_predefined_video_timings(sd,
1048 0x01, adv7604_prim_mode_comp, timings);
1049 if (err)
1050 err = find_and_set_predefined_video_timings(sd,
1051 0x02, adv7604_prim_mode_gr, timings);
Mats Randgaard4a31a932013-12-10 09:45:00 -03001052 } else if (is_digital_input(sd)) {
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001053 err = find_and_set_predefined_video_timings(sd,
1054 0x05, adv7604_prim_mode_hdmi_comp, timings);
1055 if (err)
1056 err = find_and_set_predefined_video_timings(sd,
1057 0x06, adv7604_prim_mode_hdmi_gr, timings);
Mats Randgaard4a31a932013-12-10 09:45:00 -03001058 } else {
1059 v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
1060 __func__, state->selected_input);
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001061 err = -1;
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001062 }
1063
1064
1065 return err;
1066}
1067
1068static void configure_custom_video_timings(struct v4l2_subdev *sd,
1069 const struct v4l2_bt_timings *bt)
1070{
1071 struct adv7604_state *state = to_state(sd);
Hans Verkuil54450f52012-07-18 05:45:16 -03001072 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001073 u32 width = htotal(bt);
1074 u32 height = vtotal(bt);
1075 u16 cp_start_sav = bt->hsync + bt->hbackporch - 4;
1076 u16 cp_start_eav = width - bt->hfrontporch;
1077 u16 cp_start_vbi = height - bt->vfrontporch;
1078 u16 cp_end_vbi = bt->vsync + bt->vbackporch;
1079 u16 ch1_fr_ll = (((u32)bt->pixelclock / 100) > 0) ?
1080 ((width * (ADV7604_fsc / 100)) / ((u32)bt->pixelclock / 100)) : 0;
1081 const u8 pll[2] = {
1082 0xc0 | ((width >> 8) & 0x1f),
1083 width & 0xff
1084 };
Hans Verkuil54450f52012-07-18 05:45:16 -03001085
1086 v4l2_dbg(2, debug, sd, "%s\n", __func__);
1087
Mats Randgaard4a31a932013-12-10 09:45:00 -03001088 if (is_analog_input(sd)) {
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001089 /* auto graphics */
1090 io_write(sd, 0x00, 0x07); /* video std */
1091 io_write(sd, 0x01, 0x02); /* prim mode */
1092 /* enable embedded syncs for auto graphics mode */
1093 cp_write_and_or(sd, 0x81, 0xef, 0x10);
Hans Verkuil54450f52012-07-18 05:45:16 -03001094
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001095 /* Should only be set in auto-graphics mode [REF_02, p. 91-92] */
Hans Verkuil54450f52012-07-18 05:45:16 -03001096 /* setup PLL_DIV_MAN_EN and PLL_DIV_RATIO */
1097 /* IO-map reg. 0x16 and 0x17 should be written in sequence */
Mats Randgaard4a31a932013-12-10 09:45:00 -03001098 if (adv_smbus_write_i2c_block_data(client, 0x16, 2, pll))
Hans Verkuil54450f52012-07-18 05:45:16 -03001099 v4l2_err(sd, "writing to reg 0x16 and 0x17 failed\n");
Hans Verkuil54450f52012-07-18 05:45:16 -03001100
1101 /* active video - horizontal timing */
Hans Verkuil54450f52012-07-18 05:45:16 -03001102 cp_write(sd, 0xa2, (cp_start_sav >> 4) & 0xff);
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001103 cp_write(sd, 0xa3, ((cp_start_sav & 0x0f) << 4) |
Mats Randgaard4a31a932013-12-10 09:45:00 -03001104 ((cp_start_eav >> 8) & 0x0f));
Hans Verkuil54450f52012-07-18 05:45:16 -03001105 cp_write(sd, 0xa4, cp_start_eav & 0xff);
1106
1107 /* active video - vertical timing */
Hans Verkuil54450f52012-07-18 05:45:16 -03001108 cp_write(sd, 0xa5, (cp_start_vbi >> 4) & 0xff);
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001109 cp_write(sd, 0xa6, ((cp_start_vbi & 0xf) << 4) |
Mats Randgaard4a31a932013-12-10 09:45:00 -03001110 ((cp_end_vbi >> 8) & 0xf));
Hans Verkuil54450f52012-07-18 05:45:16 -03001111 cp_write(sd, 0xa7, cp_end_vbi & 0xff);
Mats Randgaard4a31a932013-12-10 09:45:00 -03001112 } else if (is_digital_input(sd)) {
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001113 /* set default prim_mode/vid_std for HDMI
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001114 according to [REF_03, c. 4.2] */
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001115 io_write(sd, 0x00, 0x02); /* video std */
1116 io_write(sd, 0x01, 0x06); /* prim mode */
Mats Randgaard4a31a932013-12-10 09:45:00 -03001117 } else {
1118 v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
1119 __func__, state->selected_input);
Hans Verkuil54450f52012-07-18 05:45:16 -03001120 }
Hans Verkuil54450f52012-07-18 05:45:16 -03001121
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001122 cp_write(sd, 0x8f, (ch1_fr_ll >> 8) & 0x7);
1123 cp_write(sd, 0x90, ch1_fr_ll & 0xff);
1124 cp_write(sd, 0xab, (height >> 4) & 0xff);
1125 cp_write(sd, 0xac, (height & 0x0f) << 4);
1126}
Hans Verkuil54450f52012-07-18 05:45:16 -03001127
Mats Randgaard5c6c6342013-12-05 10:39:04 -03001128static void adv7604_set_offset(struct v4l2_subdev *sd, bool auto_offset, u16 offset_a, u16 offset_b, u16 offset_c)
1129{
1130 struct adv7604_state *state = to_state(sd);
1131 u8 offset_buf[4];
1132
1133 if (auto_offset) {
1134 offset_a = 0x3ff;
1135 offset_b = 0x3ff;
1136 offset_c = 0x3ff;
1137 }
1138
1139 v4l2_dbg(2, debug, sd, "%s: %s offset: a = 0x%x, b = 0x%x, c = 0x%x\n",
1140 __func__, auto_offset ? "Auto" : "Manual",
1141 offset_a, offset_b, offset_c);
1142
1143 offset_buf[0] = (cp_read(sd, 0x77) & 0xc0) | ((offset_a & 0x3f0) >> 4);
1144 offset_buf[1] = ((offset_a & 0x00f) << 4) | ((offset_b & 0x3c0) >> 6);
1145 offset_buf[2] = ((offset_b & 0x03f) << 2) | ((offset_c & 0x300) >> 8);
1146 offset_buf[3] = offset_c & 0x0ff;
1147
1148 /* Registers must be written in this order with no i2c access in between */
1149 if (adv_smbus_write_i2c_block_data(state->i2c_cp, 0x77, 4, offset_buf))
1150 v4l2_err(sd, "%s: i2c error writing to CP reg 0x77, 0x78, 0x79, 0x7a\n", __func__);
1151}
1152
1153static void adv7604_set_gain(struct v4l2_subdev *sd, bool auto_gain, u16 gain_a, u16 gain_b, u16 gain_c)
1154{
1155 struct adv7604_state *state = to_state(sd);
1156 u8 gain_buf[4];
1157 u8 gain_man = 1;
1158 u8 agc_mode_man = 1;
1159
1160 if (auto_gain) {
1161 gain_man = 0;
1162 agc_mode_man = 0;
1163 gain_a = 0x100;
1164 gain_b = 0x100;
1165 gain_c = 0x100;
1166 }
1167
1168 v4l2_dbg(2, debug, sd, "%s: %s gain: a = 0x%x, b = 0x%x, c = 0x%x\n",
1169 __func__, auto_gain ? "Auto" : "Manual",
1170 gain_a, gain_b, gain_c);
1171
1172 gain_buf[0] = ((gain_man << 7) | (agc_mode_man << 6) | ((gain_a & 0x3f0) >> 4));
1173 gain_buf[1] = (((gain_a & 0x00f) << 4) | ((gain_b & 0x3c0) >> 6));
1174 gain_buf[2] = (((gain_b & 0x03f) << 2) | ((gain_c & 0x300) >> 8));
1175 gain_buf[3] = ((gain_c & 0x0ff));
1176
1177 /* Registers must be written in this order with no i2c access in between */
1178 if (adv_smbus_write_i2c_block_data(state->i2c_cp, 0x73, 4, gain_buf))
1179 v4l2_err(sd, "%s: i2c error writing to CP reg 0x73, 0x74, 0x75, 0x76\n", __func__);
1180}
1181
Hans Verkuil54450f52012-07-18 05:45:16 -03001182static void set_rgb_quantization_range(struct v4l2_subdev *sd)
1183{
1184 struct adv7604_state *state = to_state(sd);
Mats Randgaard5c6c6342013-12-05 10:39:04 -03001185 bool rgb_output = io_read(sd, 0x02) & 0x02;
1186 bool hdmi_signal = hdmi_read(sd, 0x05) & 0x80;
Hans Verkuil54450f52012-07-18 05:45:16 -03001187
Mats Randgaard5c6c6342013-12-05 10:39:04 -03001188 v4l2_dbg(2, debug, sd, "%s: RGB quantization range: %d, RGB out: %d, HDMI: %d\n",
1189 __func__, state->rgb_quantization_range,
1190 rgb_output, hdmi_signal);
1191
1192 adv7604_set_gain(sd, true, 0x0, 0x0, 0x0);
1193 adv7604_set_offset(sd, true, 0x0, 0x0, 0x0);
Mats Randgaard98332392013-12-05 10:05:58 -03001194
Hans Verkuil54450f52012-07-18 05:45:16 -03001195 switch (state->rgb_quantization_range) {
1196 case V4L2_DV_RGB_RANGE_AUTO:
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03001197 if (state->selected_input == ADV7604_PAD_VGA_RGB) {
Mats Randgaard98332392013-12-05 10:05:58 -03001198 /* Receiving analog RGB signal
1199 * Set RGB full range (0-255) */
1200 io_write_and_or(sd, 0x02, 0x0f, 0x10);
1201 break;
1202 }
Hans Verkuil54450f52012-07-18 05:45:16 -03001203
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03001204 if (state->selected_input == ADV7604_PAD_VGA_COMP) {
Mats Randgaard98332392013-12-05 10:05:58 -03001205 /* Receiving analog YPbPr signal
1206 * Set automode */
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001207 io_write_and_or(sd, 0x02, 0x0f, 0xf0);
Mats Randgaard98332392013-12-05 10:05:58 -03001208 break;
1209 }
1210
Mats Randgaard5c6c6342013-12-05 10:39:04 -03001211 if (hdmi_signal) {
Mats Randgaard98332392013-12-05 10:05:58 -03001212 /* Receiving HDMI signal
1213 * Set automode */
1214 io_write_and_or(sd, 0x02, 0x0f, 0xf0);
1215 break;
1216 }
1217
1218 /* Receiving DVI-D signal
1219 * ADV7604 selects RGB limited range regardless of
1220 * input format (CE/IT) in automatic mode */
1221 if (state->timings.bt.standards & V4L2_DV_BT_STD_CEA861) {
1222 /* RGB limited range (16-235) */
1223 io_write_and_or(sd, 0x02, 0x0f, 0x00);
1224 } else {
1225 /* RGB full range (0-255) */
1226 io_write_and_or(sd, 0x02, 0x0f, 0x10);
Mats Randgaard5c6c6342013-12-05 10:39:04 -03001227
1228 if (is_digital_input(sd) && rgb_output) {
1229 adv7604_set_offset(sd, false, 0x40, 0x40, 0x40);
1230 } else {
1231 adv7604_set_gain(sd, false, 0xe0, 0xe0, 0xe0);
1232 adv7604_set_offset(sd, false, 0x70, 0x70, 0x70);
1233 }
Hans Verkuil54450f52012-07-18 05:45:16 -03001234 }
1235 break;
1236 case V4L2_DV_RGB_RANGE_LIMITED:
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03001237 if (state->selected_input == ADV7604_PAD_VGA_COMP) {
Mats Randgaardd261e842013-12-05 10:17:15 -03001238 /* YCrCb limited range (16-235) */
1239 io_write_and_or(sd, 0x02, 0x0f, 0x20);
Mats Randgaard5c6c6342013-12-05 10:39:04 -03001240 break;
Mats Randgaardd261e842013-12-05 10:17:15 -03001241 }
Mats Randgaard5c6c6342013-12-05 10:39:04 -03001242
1243 /* RGB limited range (16-235) */
1244 io_write_and_or(sd, 0x02, 0x0f, 0x00);
1245
Hans Verkuil54450f52012-07-18 05:45:16 -03001246 break;
1247 case V4L2_DV_RGB_RANGE_FULL:
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03001248 if (state->selected_input == ADV7604_PAD_VGA_COMP) {
Mats Randgaardd261e842013-12-05 10:17:15 -03001249 /* YCrCb full range (0-255) */
1250 io_write_and_or(sd, 0x02, 0x0f, 0x60);
Mats Randgaard5c6c6342013-12-05 10:39:04 -03001251 break;
1252 }
1253
1254 /* RGB full range (0-255) */
1255 io_write_and_or(sd, 0x02, 0x0f, 0x10);
1256
1257 if (is_analog_input(sd) || hdmi_signal)
1258 break;
1259
1260 /* Adjust gain/offset for DVI-D signals only */
1261 if (rgb_output) {
1262 adv7604_set_offset(sd, false, 0x40, 0x40, 0x40);
Mats Randgaardd261e842013-12-05 10:17:15 -03001263 } else {
Mats Randgaard5c6c6342013-12-05 10:39:04 -03001264 adv7604_set_gain(sd, false, 0xe0, 0xe0, 0xe0);
1265 adv7604_set_offset(sd, false, 0x70, 0x70, 0x70);
Mats Randgaardd261e842013-12-05 10:17:15 -03001266 }
Hans Verkuil54450f52012-07-18 05:45:16 -03001267 break;
1268 }
1269}
1270
Hans Verkuil54450f52012-07-18 05:45:16 -03001271static int adv7604_s_ctrl(struct v4l2_ctrl *ctrl)
1272{
1273 struct v4l2_subdev *sd = to_sd(ctrl);
1274 struct adv7604_state *state = to_state(sd);
1275
1276 switch (ctrl->id) {
1277 case V4L2_CID_BRIGHTNESS:
1278 cp_write(sd, 0x3c, ctrl->val);
1279 return 0;
1280 case V4L2_CID_CONTRAST:
1281 cp_write(sd, 0x3a, ctrl->val);
1282 return 0;
1283 case V4L2_CID_SATURATION:
1284 cp_write(sd, 0x3b, ctrl->val);
1285 return 0;
1286 case V4L2_CID_HUE:
1287 cp_write(sd, 0x3d, ctrl->val);
1288 return 0;
1289 case V4L2_CID_DV_RX_RGB_RANGE:
1290 state->rgb_quantization_range = ctrl->val;
1291 set_rgb_quantization_range(sd);
1292 return 0;
1293 case V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE:
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001294 if (!adv7604_has_afe(state))
1295 return -EINVAL;
Hans Verkuil54450f52012-07-18 05:45:16 -03001296 /* Set the analog sampling phase. This is needed to find the
1297 best sampling phase for analog video: an application or
1298 driver has to try a number of phases and analyze the picture
1299 quality before settling on the best performing phase. */
1300 afe_write(sd, 0xc8, ctrl->val);
1301 return 0;
1302 case V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL:
1303 /* Use the default blue color for free running mode,
1304 or supply your own. */
1305 cp_write_and_or(sd, 0xbf, ~0x04, (ctrl->val << 2));
1306 return 0;
1307 case V4L2_CID_ADV_RX_FREE_RUN_COLOR:
1308 cp_write(sd, 0xc0, (ctrl->val & 0xff0000) >> 16);
1309 cp_write(sd, 0xc1, (ctrl->val & 0x00ff00) >> 8);
1310 cp_write(sd, 0xc2, (u8)(ctrl->val & 0x0000ff));
1311 return 0;
1312 }
1313 return -EINVAL;
1314}
1315
Hans Verkuil54450f52012-07-18 05:45:16 -03001316/* ----------------------------------------------------------------------- */
1317
1318static inline bool no_power(struct v4l2_subdev *sd)
1319{
1320 /* Entire chip or CP powered off */
1321 return io_read(sd, 0x0c) & 0x24;
1322}
1323
1324static inline bool no_signal_tmds(struct v4l2_subdev *sd)
1325{
Mats Randgaard4a31a932013-12-10 09:45:00 -03001326 struct adv7604_state *state = to_state(sd);
1327
1328 return !(io_read(sd, 0x6a) & (0x10 >> state->selected_input));
Hans Verkuil54450f52012-07-18 05:45:16 -03001329}
1330
1331static inline bool no_lock_tmds(struct v4l2_subdev *sd)
1332{
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001333 struct adv7604_state *state = to_state(sd);
1334 const struct adv7604_chip_info *info = state->info;
1335
1336 return (io_read(sd, 0x6a) & info->tdms_lock_mask) != info->tdms_lock_mask;
Hans Verkuil54450f52012-07-18 05:45:16 -03001337}
1338
Martin Buggebb88f322013-08-14 08:52:46 -03001339static inline bool is_hdmi(struct v4l2_subdev *sd)
1340{
1341 return hdmi_read(sd, 0x05) & 0x80;
1342}
1343
Hans Verkuil54450f52012-07-18 05:45:16 -03001344static inline bool no_lock_sspd(struct v4l2_subdev *sd)
1345{
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001346 struct adv7604_state *state = to_state(sd);
1347
1348 /*
1349 * Chips without a AFE don't expose registers for the SSPD, so just assume
1350 * that we have a lock.
1351 */
1352 if (adv7604_has_afe(state))
1353 return false;
1354
Hans Verkuil54450f52012-07-18 05:45:16 -03001355 /* TODO channel 2 */
1356 return ((cp_read(sd, 0xb5) & 0xd0) != 0xd0);
1357}
1358
1359static inline bool no_lock_stdi(struct v4l2_subdev *sd)
1360{
1361 /* TODO channel 2 */
1362 return !(cp_read(sd, 0xb1) & 0x80);
1363}
1364
1365static inline bool no_signal(struct v4l2_subdev *sd)
1366{
Hans Verkuil54450f52012-07-18 05:45:16 -03001367 bool ret;
1368
1369 ret = no_power(sd);
1370
1371 ret |= no_lock_stdi(sd);
1372 ret |= no_lock_sspd(sd);
1373
Mats Randgaard4a31a932013-12-10 09:45:00 -03001374 if (is_digital_input(sd)) {
Hans Verkuil54450f52012-07-18 05:45:16 -03001375 ret |= no_lock_tmds(sd);
1376 ret |= no_signal_tmds(sd);
1377 }
1378
1379 return ret;
1380}
1381
1382static inline bool no_lock_cp(struct v4l2_subdev *sd)
1383{
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001384 struct adv7604_state *state = to_state(sd);
1385
1386 if (!adv7604_has_afe(state))
1387 return false;
1388
Hans Verkuil54450f52012-07-18 05:45:16 -03001389 /* CP has detected a non standard number of lines on the incoming
1390 video compared to what it is configured to receive by s_dv_timings */
1391 return io_read(sd, 0x12) & 0x01;
1392}
1393
1394static int adv7604_g_input_status(struct v4l2_subdev *sd, u32 *status)
1395{
Hans Verkuil54450f52012-07-18 05:45:16 -03001396 *status = 0;
1397 *status |= no_power(sd) ? V4L2_IN_ST_NO_POWER : 0;
1398 *status |= no_signal(sd) ? V4L2_IN_ST_NO_SIGNAL : 0;
1399 if (no_lock_cp(sd))
Mats Randgaard4a31a932013-12-10 09:45:00 -03001400 *status |= is_digital_input(sd) ? V4L2_IN_ST_NO_SYNC : V4L2_IN_ST_NO_H_LOCK;
Hans Verkuil54450f52012-07-18 05:45:16 -03001401
1402 v4l2_dbg(1, debug, sd, "%s: status = 0x%x\n", __func__, *status);
1403
1404 return 0;
1405}
1406
1407/* ----------------------------------------------------------------------- */
1408
Hans Verkuil54450f52012-07-18 05:45:16 -03001409struct stdi_readback {
1410 u16 bl, lcf, lcvs;
1411 u8 hs_pol, vs_pol;
1412 bool interlaced;
1413};
1414
1415static int stdi2dv_timings(struct v4l2_subdev *sd,
1416 struct stdi_readback *stdi,
1417 struct v4l2_dv_timings *timings)
1418{
1419 struct adv7604_state *state = to_state(sd);
1420 u32 hfreq = (ADV7604_fsc * 8) / stdi->bl;
1421 u32 pix_clk;
1422 int i;
1423
1424 for (i = 0; adv7604_timings[i].bt.height; i++) {
1425 if (vtotal(&adv7604_timings[i].bt) != stdi->lcf + 1)
1426 continue;
1427 if (adv7604_timings[i].bt.vsync != stdi->lcvs)
1428 continue;
1429
1430 pix_clk = hfreq * htotal(&adv7604_timings[i].bt);
1431
1432 if ((pix_clk < adv7604_timings[i].bt.pixelclock + 1000000) &&
1433 (pix_clk > adv7604_timings[i].bt.pixelclock - 1000000)) {
1434 *timings = adv7604_timings[i];
1435 return 0;
1436 }
1437 }
1438
1439 if (v4l2_detect_cvt(stdi->lcf + 1, hfreq, stdi->lcvs,
1440 (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
1441 (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
1442 timings))
1443 return 0;
1444 if (v4l2_detect_gtf(stdi->lcf + 1, hfreq, stdi->lcvs,
1445 (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
1446 (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
1447 state->aspect_ratio, timings))
1448 return 0;
1449
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001450 v4l2_dbg(2, debug, sd,
1451 "%s: No format candidate found for lcvs = %d, lcf=%d, bl = %d, %chsync, %cvsync\n",
1452 __func__, stdi->lcvs, stdi->lcf, stdi->bl,
1453 stdi->hs_pol, stdi->vs_pol);
Hans Verkuil54450f52012-07-18 05:45:16 -03001454 return -1;
1455}
1456
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001457
Hans Verkuil54450f52012-07-18 05:45:16 -03001458static int read_stdi(struct v4l2_subdev *sd, struct stdi_readback *stdi)
1459{
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001460 struct adv7604_state *state = to_state(sd);
1461 const struct adv7604_chip_info *info = state->info;
Laurent Pinchart4a2ccdd2014-01-08 20:26:55 -03001462 u8 polarity;
1463
Hans Verkuil54450f52012-07-18 05:45:16 -03001464 if (no_lock_stdi(sd) || no_lock_sspd(sd)) {
1465 v4l2_dbg(2, debug, sd, "%s: STDI and/or SSPD not locked\n", __func__);
1466 return -1;
1467 }
1468
1469 /* read STDI */
Laurent Pinchart51182a92014-01-08 19:30:37 -03001470 stdi->bl = cp_read16(sd, 0xb1, 0x3fff);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001471 stdi->lcf = cp_read16(sd, info->lcf_reg, 0x7ff);
Hans Verkuil54450f52012-07-18 05:45:16 -03001472 stdi->lcvs = cp_read(sd, 0xb3) >> 3;
1473 stdi->interlaced = io_read(sd, 0x12) & 0x10;
1474
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001475 if (adv7604_has_afe(state)) {
1476 /* read SSPD */
1477 polarity = cp_read(sd, 0xb5);
1478 if ((polarity & 0x03) == 0x01) {
1479 stdi->hs_pol = polarity & 0x10
1480 ? (polarity & 0x08 ? '+' : '-') : 'x';
1481 stdi->vs_pol = polarity & 0x40
1482 ? (polarity & 0x20 ? '+' : '-') : 'x';
1483 } else {
1484 stdi->hs_pol = 'x';
1485 stdi->vs_pol = 'x';
1486 }
Hans Verkuil54450f52012-07-18 05:45:16 -03001487 } else {
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001488 polarity = hdmi_read(sd, 0x05);
1489 stdi->hs_pol = polarity & 0x20 ? '+' : '-';
1490 stdi->vs_pol = polarity & 0x10 ? '+' : '-';
Hans Verkuil54450f52012-07-18 05:45:16 -03001491 }
1492
1493 if (no_lock_stdi(sd) || no_lock_sspd(sd)) {
1494 v4l2_dbg(2, debug, sd,
1495 "%s: signal lost during readout of STDI/SSPD\n", __func__);
1496 return -1;
1497 }
1498
1499 if (stdi->lcf < 239 || stdi->bl < 8 || stdi->bl == 0x3fff) {
1500 v4l2_dbg(2, debug, sd, "%s: invalid signal\n", __func__);
1501 memset(stdi, 0, sizeof(struct stdi_readback));
1502 return -1;
1503 }
1504
1505 v4l2_dbg(2, debug, sd,
1506 "%s: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, %chsync, %cvsync, %s\n",
1507 __func__, stdi->lcf, stdi->bl, stdi->lcvs,
1508 stdi->hs_pol, stdi->vs_pol,
1509 stdi->interlaced ? "interlaced" : "progressive");
1510
1511 return 0;
1512}
1513
1514static int adv7604_enum_dv_timings(struct v4l2_subdev *sd,
1515 struct v4l2_enum_dv_timings *timings)
1516{
Laurent Pinchartafec5592014-01-29 10:09:41 -03001517 struct adv7604_state *state = to_state(sd);
1518
Hans Verkuil54450f52012-07-18 05:45:16 -03001519 if (timings->index >= ARRAY_SIZE(adv7604_timings) - 1)
1520 return -EINVAL;
Laurent Pinchartafec5592014-01-29 10:09:41 -03001521
1522 if (timings->pad >= state->source_pad)
1523 return -EINVAL;
1524
Hans Verkuil54450f52012-07-18 05:45:16 -03001525 memset(timings->reserved, 0, sizeof(timings->reserved));
1526 timings->timings = adv7604_timings[timings->index];
1527 return 0;
1528}
1529
Laurent Pinchartafec5592014-01-29 10:09:41 -03001530static int __adv7604_dv_timings_cap(struct v4l2_subdev *sd,
1531 struct v4l2_dv_timings_cap *cap,
1532 unsigned int pad)
1533{
1534 cap->type = V4L2_DV_BT_656_1120;
1535 cap->bt.max_width = 1920;
1536 cap->bt.max_height = 1200;
1537 cap->bt.min_pixelclock = 25000000;
1538
1539 switch (pad) {
1540 case ADV7604_PAD_HDMI_PORT_A:
1541 case ADV7604_PAD_HDMI_PORT_B:
1542 case ADV7604_PAD_HDMI_PORT_C:
1543 case ADV7604_PAD_HDMI_PORT_D:
1544 cap->bt.max_pixelclock = 225000000;
1545 break;
1546 case ADV7604_PAD_VGA_RGB:
1547 case ADV7604_PAD_VGA_COMP:
1548 default:
1549 cap->bt.max_pixelclock = 170000000;
1550 break;
1551 }
1552
1553 cap->bt.standards = V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
1554 V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT;
1555 cap->bt.capabilities = V4L2_DV_BT_CAP_PROGRESSIVE |
1556 V4L2_DV_BT_CAP_REDUCED_BLANKING | V4L2_DV_BT_CAP_CUSTOM;
1557 return 0;
1558}
1559
Hans Verkuil54450f52012-07-18 05:45:16 -03001560static int adv7604_dv_timings_cap(struct v4l2_subdev *sd,
1561 struct v4l2_dv_timings_cap *cap)
1562{
Laurent Pinchartafec5592014-01-29 10:09:41 -03001563 struct adv7604_state *state = to_state(sd);
1564
1565 return __adv7604_dv_timings_cap(sd, cap, state->selected_input);
1566}
1567
1568static int adv7604_pad_dv_timings_cap(struct v4l2_subdev *sd,
1569 struct v4l2_dv_timings_cap *cap)
1570{
1571 struct adv7604_state *state = to_state(sd);
1572
1573 if (cap->pad >= state->source_pad)
1574 return -EINVAL;
1575
1576 return __adv7604_dv_timings_cap(sd, cap, cap->pad);
Hans Verkuil54450f52012-07-18 05:45:16 -03001577}
1578
1579/* Fill the optional fields .standards and .flags in struct v4l2_dv_timings
1580 if the format is listed in adv7604_timings[] */
1581static void adv7604_fill_optional_dv_timings_fields(struct v4l2_subdev *sd,
1582 struct v4l2_dv_timings *timings)
1583{
Hans Verkuil54450f52012-07-18 05:45:16 -03001584 int i;
1585
1586 for (i = 0; adv7604_timings[i].bt.width; i++) {
Hans Verkuilef1ed8f2013-08-15 08:28:47 -03001587 if (v4l2_match_dv_timings(timings, &adv7604_timings[i],
Mats Randgaard4a31a932013-12-10 09:45:00 -03001588 is_digital_input(sd) ? 250000 : 1000000)) {
Hans Verkuil54450f52012-07-18 05:45:16 -03001589 *timings = adv7604_timings[i];
1590 break;
1591 }
1592 }
1593}
1594
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001595static unsigned int adv7604_read_hdmi_pixelclock(struct v4l2_subdev *sd)
1596{
1597 unsigned int freq;
1598 int a, b;
1599
1600 a = hdmi_read(sd, 0x06);
1601 b = hdmi_read(sd, 0x3b);
1602 if (a < 0 || b < 0)
1603 return 0;
1604 freq = a * 1000000 + ((b & 0x30) >> 4) * 250000;
1605
1606 if (is_hdmi(sd)) {
1607 /* adjust for deep color mode */
1608 unsigned bits_per_channel = ((hdmi_read(sd, 0x0b) & 0x60) >> 4) + 8;
1609
1610 freq = freq * 8 / bits_per_channel;
1611 }
1612
1613 return freq;
1614}
1615
1616static unsigned int adv7611_read_hdmi_pixelclock(struct v4l2_subdev *sd)
1617{
1618 int a, b;
1619
1620 a = hdmi_read(sd, 0x51);
1621 b = hdmi_read(sd, 0x52);
1622 if (a < 0 || b < 0)
1623 return 0;
1624 return ((a << 1) | (b >> 7)) * 1000000 + (b & 0x7f) * 1000000 / 128;
1625}
1626
Hans Verkuil54450f52012-07-18 05:45:16 -03001627static int adv7604_query_dv_timings(struct v4l2_subdev *sd,
1628 struct v4l2_dv_timings *timings)
1629{
1630 struct adv7604_state *state = to_state(sd);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001631 const struct adv7604_chip_info *info = state->info;
Hans Verkuil54450f52012-07-18 05:45:16 -03001632 struct v4l2_bt_timings *bt = &timings->bt;
1633 struct stdi_readback stdi;
1634
1635 if (!timings)
1636 return -EINVAL;
1637
1638 memset(timings, 0, sizeof(struct v4l2_dv_timings));
1639
1640 if (no_signal(sd)) {
Martin Bugge1e0b9152013-12-05 10:34:46 -03001641 state->restart_stdi_once = true;
Hans Verkuil54450f52012-07-18 05:45:16 -03001642 v4l2_dbg(1, debug, sd, "%s: no valid signal\n", __func__);
1643 return -ENOLINK;
1644 }
1645
1646 /* read STDI */
1647 if (read_stdi(sd, &stdi)) {
1648 v4l2_dbg(1, debug, sd, "%s: STDI/SSPD not locked\n", __func__);
1649 return -ENOLINK;
1650 }
1651 bt->interlaced = stdi.interlaced ?
1652 V4L2_DV_INTERLACED : V4L2_DV_PROGRESSIVE;
1653
Mats Randgaard4a31a932013-12-10 09:45:00 -03001654 if (is_digital_input(sd)) {
Hans Verkuil54450f52012-07-18 05:45:16 -03001655 timings->type = V4L2_DV_BT_656_1120;
1656
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001657 /* FIXME: All masks are incorrect for ADV7611 */
Laurent Pinchart51182a92014-01-08 19:30:37 -03001658 bt->width = hdmi_read16(sd, 0x07, 0xfff);
1659 bt->height = hdmi_read16(sd, 0x09, 0xfff);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001660 bt->pixelclock = info->read_hdmi_pixelclock(sd);
Laurent Pinchart51182a92014-01-08 19:30:37 -03001661 bt->hfrontporch = hdmi_read16(sd, 0x20, 0x3ff);
1662 bt->hsync = hdmi_read16(sd, 0x22, 0x3ff);
1663 bt->hbackporch = hdmi_read16(sd, 0x24, 0x3ff);
1664 bt->vfrontporch = hdmi_read16(sd, 0x2a, 0x1fff) / 2;
1665 bt->vsync = hdmi_read16(sd, 0x2e, 0x1fff) / 2;
1666 bt->vbackporch = hdmi_read16(sd, 0x32, 0x1fff) / 2;
Hans Verkuil54450f52012-07-18 05:45:16 -03001667 bt->polarities = ((hdmi_read(sd, 0x05) & 0x10) ? V4L2_DV_VSYNC_POS_POL : 0) |
1668 ((hdmi_read(sd, 0x05) & 0x20) ? V4L2_DV_HSYNC_POS_POL : 0);
1669 if (bt->interlaced == V4L2_DV_INTERLACED) {
Laurent Pinchart51182a92014-01-08 19:30:37 -03001670 bt->height += hdmi_read16(sd, 0x0b, 0xfff);
1671 bt->il_vfrontporch = hdmi_read16(sd, 0x2c, 0x1fff) / 2;
1672 bt->il_vsync = hdmi_read16(sd, 0x30, 0x1fff) / 2;
1673 bt->vbackporch = hdmi_read16(sd, 0x34, 0x1fff) / 2;
Hans Verkuil54450f52012-07-18 05:45:16 -03001674 }
1675 adv7604_fill_optional_dv_timings_fields(sd, timings);
1676 } else {
1677 /* find format
Hans Verkuil80939642012-10-16 05:46:21 -03001678 * Since LCVS values are inaccurate [REF_03, p. 275-276],
Hans Verkuil54450f52012-07-18 05:45:16 -03001679 * stdi2dv_timings() is called with lcvs +-1 if the first attempt fails.
1680 */
1681 if (!stdi2dv_timings(sd, &stdi, timings))
1682 goto found;
1683 stdi.lcvs += 1;
1684 v4l2_dbg(1, debug, sd, "%s: lcvs + 1 = %d\n", __func__, stdi.lcvs);
1685 if (!stdi2dv_timings(sd, &stdi, timings))
1686 goto found;
1687 stdi.lcvs -= 2;
1688 v4l2_dbg(1, debug, sd, "%s: lcvs - 1 = %d\n", __func__, stdi.lcvs);
1689 if (stdi2dv_timings(sd, &stdi, timings)) {
Hans Verkuilcf9afb12012-10-16 10:12:55 -03001690 /*
1691 * The STDI block may measure wrong values, especially
1692 * for lcvs and lcf. If the driver can not find any
1693 * valid timing, the STDI block is restarted to measure
1694 * the video timings again. The function will return an
1695 * error, but the restart of STDI will generate a new
1696 * STDI interrupt and the format detection process will
1697 * restart.
1698 */
1699 if (state->restart_stdi_once) {
1700 v4l2_dbg(1, debug, sd, "%s: restart STDI\n", __func__);
1701 /* TODO restart STDI for Sync Channel 2 */
1702 /* enter one-shot mode */
1703 cp_write_and_or(sd, 0x86, 0xf9, 0x00);
1704 /* trigger STDI restart */
1705 cp_write_and_or(sd, 0x86, 0xf9, 0x04);
1706 /* reset to continuous mode */
1707 cp_write_and_or(sd, 0x86, 0xf9, 0x02);
1708 state->restart_stdi_once = false;
1709 return -ENOLINK;
1710 }
Hans Verkuil54450f52012-07-18 05:45:16 -03001711 v4l2_dbg(1, debug, sd, "%s: format not supported\n", __func__);
1712 return -ERANGE;
1713 }
Hans Verkuilcf9afb12012-10-16 10:12:55 -03001714 state->restart_stdi_once = true;
Hans Verkuil54450f52012-07-18 05:45:16 -03001715 }
1716found:
1717
1718 if (no_signal(sd)) {
1719 v4l2_dbg(1, debug, sd, "%s: signal lost during readout\n", __func__);
1720 memset(timings, 0, sizeof(struct v4l2_dv_timings));
1721 return -ENOLINK;
1722 }
1723
Mats Randgaard4a31a932013-12-10 09:45:00 -03001724 if ((is_analog_input(sd) && bt->pixelclock > 170000000) ||
1725 (is_digital_input(sd) && bt->pixelclock > 225000000)) {
Hans Verkuil54450f52012-07-18 05:45:16 -03001726 v4l2_dbg(1, debug, sd, "%s: pixelclock out of range %d\n",
1727 __func__, (u32)bt->pixelclock);
1728 return -ERANGE;
1729 }
1730
1731 if (debug > 1)
Hans Verkuil11d034c2013-08-15 08:05:59 -03001732 v4l2_print_dv_timings(sd->name, "adv7604_query_dv_timings: ",
1733 timings, true);
Hans Verkuil54450f52012-07-18 05:45:16 -03001734
1735 return 0;
1736}
1737
1738static int adv7604_s_dv_timings(struct v4l2_subdev *sd,
1739 struct v4l2_dv_timings *timings)
1740{
1741 struct adv7604_state *state = to_state(sd);
1742 struct v4l2_bt_timings *bt;
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001743 int err;
Hans Verkuil54450f52012-07-18 05:45:16 -03001744
1745 if (!timings)
1746 return -EINVAL;
1747
Mats Randgaardd48eb482013-12-12 10:13:35 -03001748 if (v4l2_match_dv_timings(&state->timings, timings, 0)) {
1749 v4l2_dbg(1, debug, sd, "%s: no change\n", __func__);
1750 return 0;
1751 }
1752
Hans Verkuil54450f52012-07-18 05:45:16 -03001753 bt = &timings->bt;
1754
Mats Randgaard4a31a932013-12-10 09:45:00 -03001755 if ((is_analog_input(sd) && bt->pixelclock > 170000000) ||
1756 (is_digital_input(sd) && bt->pixelclock > 225000000)) {
Hans Verkuil54450f52012-07-18 05:45:16 -03001757 v4l2_dbg(1, debug, sd, "%s: pixelclock out of range %d\n",
1758 __func__, (u32)bt->pixelclock);
1759 return -ERANGE;
1760 }
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001761
Hans Verkuil54450f52012-07-18 05:45:16 -03001762 adv7604_fill_optional_dv_timings_fields(sd, timings);
1763
1764 state->timings = *timings;
1765
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001766 cp_write_and_or(sd, 0x91, 0xbf, bt->interlaced ? 0x40 : 0x00);
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001767
1768 /* Use prim_mode and vid_std when available */
1769 err = configure_predefined_video_timings(sd, timings);
1770 if (err) {
1771 /* custom settings when the video format
1772 does not have prim_mode/vid_std */
1773 configure_custom_video_timings(sd, bt);
1774 }
Hans Verkuil54450f52012-07-18 05:45:16 -03001775
1776 set_rgb_quantization_range(sd);
1777
Hans Verkuil54450f52012-07-18 05:45:16 -03001778 if (debug > 1)
Hans Verkuil11d034c2013-08-15 08:05:59 -03001779 v4l2_print_dv_timings(sd->name, "adv7604_s_dv_timings: ",
1780 timings, true);
Hans Verkuil54450f52012-07-18 05:45:16 -03001781 return 0;
1782}
1783
1784static int adv7604_g_dv_timings(struct v4l2_subdev *sd,
1785 struct v4l2_dv_timings *timings)
1786{
1787 struct adv7604_state *state = to_state(sd);
1788
1789 *timings = state->timings;
1790 return 0;
1791}
1792
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001793static void adv7604_set_termination(struct v4l2_subdev *sd, bool enable)
1794{
1795 hdmi_write(sd, 0x01, enable ? 0x00 : 0x78);
1796}
1797
1798static void adv7611_set_termination(struct v4l2_subdev *sd, bool enable)
1799{
1800 hdmi_write(sd, 0x83, enable ? 0xfe : 0xff);
1801}
1802
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001803static void enable_input(struct v4l2_subdev *sd)
Hans Verkuil54450f52012-07-18 05:45:16 -03001804{
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001805 struct adv7604_state *state = to_state(sd);
1806
Mats Randgaard4a31a932013-12-10 09:45:00 -03001807 if (is_analog_input(sd)) {
Hans Verkuil54450f52012-07-18 05:45:16 -03001808 io_write(sd, 0x15, 0xb0); /* Disable Tristate of Pins (no audio) */
Mats Randgaard4a31a932013-12-10 09:45:00 -03001809 } else if (is_digital_input(sd)) {
Mats Randgaard4a31a932013-12-10 09:45:00 -03001810 hdmi_write_and_or(sd, 0x00, 0xfc, state->selected_input);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001811 state->info->set_termination(sd, true);
Hans Verkuil54450f52012-07-18 05:45:16 -03001812 io_write(sd, 0x15, 0xa0); /* Disable Tristate of Pins */
Mats Randgaard5474b982013-12-05 10:33:41 -03001813 hdmi_write_and_or(sd, 0x1a, 0xef, 0x00); /* Unmute audio */
Mats Randgaard4a31a932013-12-10 09:45:00 -03001814 } else {
1815 v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
1816 __func__, state->selected_input);
Hans Verkuil54450f52012-07-18 05:45:16 -03001817 }
1818}
1819
1820static void disable_input(struct v4l2_subdev *sd)
1821{
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001822 struct adv7604_state *state = to_state(sd);
1823
Mats Randgaard5474b982013-12-05 10:33:41 -03001824 hdmi_write_and_or(sd, 0x1a, 0xef, 0x10); /* Mute audio */
1825 msleep(16); /* 512 samples with >= 32 kHz sample rate [REF_03, c. 7.16.10] */
Hans Verkuil54450f52012-07-18 05:45:16 -03001826 io_write(sd, 0x15, 0xbe); /* Tristate all outputs from video core */
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001827 state->info->set_termination(sd, false);
Hans Verkuil54450f52012-07-18 05:45:16 -03001828}
1829
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001830static void select_input(struct v4l2_subdev *sd)
Hans Verkuil54450f52012-07-18 05:45:16 -03001831{
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001832 struct adv7604_state *state = to_state(sd);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001833 const struct adv7604_chip_info *info = state->info;
Hans Verkuil54450f52012-07-18 05:45:16 -03001834
Mats Randgaard4a31a932013-12-10 09:45:00 -03001835 if (is_analog_input(sd)) {
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001836 adv7604_write_reg_seq(sd, info->recommended_settings[0]);
Hans Verkuil54450f52012-07-18 05:45:16 -03001837
1838 afe_write(sd, 0x00, 0x08); /* power up ADC */
1839 afe_write(sd, 0x01, 0x06); /* power up Analog Front End */
1840 afe_write(sd, 0xc8, 0x00); /* phase control */
Mats Randgaard4a31a932013-12-10 09:45:00 -03001841 } else if (is_digital_input(sd)) {
1842 hdmi_write(sd, 0x00, state->selected_input & 0x03);
Hans Verkuil54450f52012-07-18 05:45:16 -03001843
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001844 adv7604_write_reg_seq(sd, info->recommended_settings[1]);
Hans Verkuil54450f52012-07-18 05:45:16 -03001845
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001846 if (adv7604_has_afe(state)) {
1847 afe_write(sd, 0x00, 0xff); /* power down ADC */
1848 afe_write(sd, 0x01, 0xfe); /* power down Analog Front End */
1849 afe_write(sd, 0xc8, 0x40); /* phase control */
1850 }
Hans Verkuil54450f52012-07-18 05:45:16 -03001851
Hans Verkuil54450f52012-07-18 05:45:16 -03001852 cp_write(sd, 0x3e, 0x00); /* CP core pre-gain control */
1853 cp_write(sd, 0xc3, 0x39); /* CP coast control. Graphics mode */
1854 cp_write(sd, 0x40, 0x80); /* CP core pre-gain control. Graphics mode */
Mats Randgaard4a31a932013-12-10 09:45:00 -03001855 } else {
1856 v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
1857 __func__, state->selected_input);
Hans Verkuil54450f52012-07-18 05:45:16 -03001858 }
1859}
1860
1861static int adv7604_s_routing(struct v4l2_subdev *sd,
1862 u32 input, u32 output, u32 config)
1863{
1864 struct adv7604_state *state = to_state(sd);
1865
Mats Randgaardff4f80f2013-12-05 10:24:05 -03001866 v4l2_dbg(2, debug, sd, "%s: input %d, selected input %d",
1867 __func__, input, state->selected_input);
1868
1869 if (input == state->selected_input)
1870 return 0;
Hans Verkuil54450f52012-07-18 05:45:16 -03001871
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001872 if (input > state->info->max_port)
1873 return -EINVAL;
1874
Mats Randgaard4a31a932013-12-10 09:45:00 -03001875 state->selected_input = input;
Hans Verkuil54450f52012-07-18 05:45:16 -03001876
1877 disable_input(sd);
1878
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001879 select_input(sd);
Hans Verkuil54450f52012-07-18 05:45:16 -03001880
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001881 enable_input(sd);
Hans Verkuil54450f52012-07-18 05:45:16 -03001882
1883 return 0;
1884}
1885
Laurent Pinchart539b33b2014-01-26 18:42:37 -03001886static int adv7604_enum_mbus_code(struct v4l2_subdev *sd,
1887 struct v4l2_subdev_fh *fh,
1888 struct v4l2_subdev_mbus_code_enum *code)
Hans Verkuil54450f52012-07-18 05:45:16 -03001889{
1890 struct adv7604_state *state = to_state(sd);
1891
Laurent Pinchart539b33b2014-01-26 18:42:37 -03001892 if (code->index >= state->info->nformats)
1893 return -EINVAL;
1894
1895 code->code = state->info->formats[code->index].code;
1896
1897 return 0;
1898}
1899
1900static void adv7604_fill_format(struct adv7604_state *state,
1901 struct v4l2_mbus_framefmt *format)
1902{
1903 memset(format, 0, sizeof(*format));
1904
1905 format->width = state->timings.bt.width;
1906 format->height = state->timings.bt.height;
1907 format->field = V4L2_FIELD_NONE;
1908
1909 if (state->timings.bt.standards & V4L2_DV_BT_STD_CEA861)
1910 format->colorspace = (state->timings.bt.height <= 576) ?
Hans Verkuil54450f52012-07-18 05:45:16 -03001911 V4L2_COLORSPACE_SMPTE170M : V4L2_COLORSPACE_REC709;
Laurent Pinchart539b33b2014-01-26 18:42:37 -03001912}
1913
1914/*
1915 * Compute the op_ch_sel value required to obtain on the bus the component order
1916 * corresponding to the selected format taking into account bus reordering
1917 * applied by the board at the output of the device.
1918 *
1919 * The following table gives the op_ch_value from the format component order
1920 * (expressed as op_ch_sel value in column) and the bus reordering (expressed as
1921 * adv7604_bus_order value in row).
1922 *
1923 * | GBR(0) GRB(1) BGR(2) RGB(3) BRG(4) RBG(5)
1924 * ----------+-------------------------------------------------
1925 * RGB (NOP) | GBR GRB BGR RGB BRG RBG
1926 * GRB (1-2) | BGR RGB GBR GRB RBG BRG
1927 * RBG (2-3) | GRB GBR BRG RBG BGR RGB
1928 * BGR (1-3) | RBG BRG RGB BGR GRB GBR
1929 * BRG (ROR) | BRG RBG GRB GBR RGB BGR
1930 * GBR (ROL) | RGB BGR RBG BRG GBR GRB
1931 */
1932static unsigned int adv7604_op_ch_sel(struct adv7604_state *state)
1933{
1934#define _SEL(a,b,c,d,e,f) { \
1935 ADV7604_OP_CH_SEL_##a, ADV7604_OP_CH_SEL_##b, ADV7604_OP_CH_SEL_##c, \
1936 ADV7604_OP_CH_SEL_##d, ADV7604_OP_CH_SEL_##e, ADV7604_OP_CH_SEL_##f }
1937#define _BUS(x) [ADV7604_BUS_ORDER_##x]
1938
1939 static const unsigned int op_ch_sel[6][6] = {
1940 _BUS(RGB) /* NOP */ = _SEL(GBR, GRB, BGR, RGB, BRG, RBG),
1941 _BUS(GRB) /* 1-2 */ = _SEL(BGR, RGB, GBR, GRB, RBG, BRG),
1942 _BUS(RBG) /* 2-3 */ = _SEL(GRB, GBR, BRG, RBG, BGR, RGB),
1943 _BUS(BGR) /* 1-3 */ = _SEL(RBG, BRG, RGB, BGR, GRB, GBR),
1944 _BUS(BRG) /* ROR */ = _SEL(BRG, RBG, GRB, GBR, RGB, BGR),
1945 _BUS(GBR) /* ROL */ = _SEL(RGB, BGR, RBG, BRG, GBR, GRB),
1946 };
1947
1948 return op_ch_sel[state->pdata.bus_order][state->format->op_ch_sel >> 5];
1949}
1950
1951static void adv7604_setup_format(struct adv7604_state *state)
1952{
1953 struct v4l2_subdev *sd = &state->sd;
1954
1955 io_write_and_or(sd, 0x02, 0xfd,
1956 state->format->rgb_out ? ADV7604_RGB_OUT : 0);
1957 io_write(sd, 0x03, state->format->op_format_sel |
1958 state->pdata.op_format_mode_sel);
1959 io_write_and_or(sd, 0x04, 0x1f, adv7604_op_ch_sel(state));
1960 io_write_and_or(sd, 0x05, 0xfe,
1961 state->format->swap_cb_cr ? ADV7604_OP_SWAP_CB_CR : 0);
1962}
1963
1964static int adv7604_get_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
1965 struct v4l2_subdev_format *format)
1966{
1967 struct adv7604_state *state = to_state(sd);
1968
1969 if (format->pad != state->source_pad)
1970 return -EINVAL;
1971
1972 adv7604_fill_format(state, &format->format);
1973
1974 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1975 struct v4l2_mbus_framefmt *fmt;
1976
1977 fmt = v4l2_subdev_get_try_format(fh, format->pad);
1978 format->format.code = fmt->code;
1979 } else {
1980 format->format.code = state->format->code;
Hans Verkuil54450f52012-07-18 05:45:16 -03001981 }
Laurent Pinchart539b33b2014-01-26 18:42:37 -03001982
1983 return 0;
1984}
1985
1986static int adv7604_set_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
1987 struct v4l2_subdev_format *format)
1988{
1989 struct adv7604_state *state = to_state(sd);
1990 const struct adv7604_format_info *info;
1991
1992 if (format->pad != state->source_pad)
1993 return -EINVAL;
1994
1995 info = adv7604_format_info(state, format->format.code);
1996 if (info == NULL)
1997 info = adv7604_format_info(state, V4L2_MBUS_FMT_YUYV8_2X8);
1998
1999 adv7604_fill_format(state, &format->format);
2000 format->format.code = info->code;
2001
2002 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
2003 struct v4l2_mbus_framefmt *fmt;
2004
2005 fmt = v4l2_subdev_get_try_format(fh, format->pad);
2006 fmt->code = format->format.code;
2007 } else {
2008 state->format = info;
2009 adv7604_setup_format(state);
2010 }
2011
Hans Verkuil54450f52012-07-18 05:45:16 -03002012 return 0;
2013}
2014
2015static int adv7604_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
2016{
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002017 struct adv7604_state *state = to_state(sd);
2018 const struct adv7604_chip_info *info = state->info;
Mats Randgaardf24d2292013-12-10 10:15:13 -03002019 const u8 irq_reg_0x43 = io_read(sd, 0x43);
2020 const u8 irq_reg_0x6b = io_read(sd, 0x6b);
2021 const u8 irq_reg_0x70 = io_read(sd, 0x70);
2022 u8 fmt_change_digital;
2023 u8 fmt_change;
2024 u8 tx_5v;
2025
2026 if (irq_reg_0x43)
2027 io_write(sd, 0x44, irq_reg_0x43);
2028 if (irq_reg_0x70)
2029 io_write(sd, 0x71, irq_reg_0x70);
2030 if (irq_reg_0x6b)
2031 io_write(sd, 0x6c, irq_reg_0x6b);
Hans Verkuil54450f52012-07-18 05:45:16 -03002032
Mats Randgaardff4f80f2013-12-05 10:24:05 -03002033 v4l2_dbg(2, debug, sd, "%s: ", __func__);
2034
Hans Verkuil54450f52012-07-18 05:45:16 -03002035 /* format change */
Mats Randgaardf24d2292013-12-10 10:15:13 -03002036 fmt_change = irq_reg_0x43 & 0x98;
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002037 fmt_change_digital = is_digital_input(sd)
2038 ? irq_reg_0x6b & info->fmt_change_digital_mask
2039 : 0;
Mats Randgaard14d03232013-12-05 10:26:11 -03002040
Hans Verkuil54450f52012-07-18 05:45:16 -03002041 if (fmt_change || fmt_change_digital) {
2042 v4l2_dbg(1, debug, sd,
Mats Randgaard25a64ac2013-08-14 07:58:45 -03002043 "%s: fmt_change = 0x%x, fmt_change_digital = 0x%x\n",
Hans Verkuil54450f52012-07-18 05:45:16 -03002044 __func__, fmt_change, fmt_change_digital);
Mats Randgaard25a64ac2013-08-14 07:58:45 -03002045
Mats Randgaard14d03232013-12-05 10:26:11 -03002046 v4l2_subdev_notify(sd, ADV7604_FMT_CHANGE, NULL);
Mats Randgaard25a64ac2013-08-14 07:58:45 -03002047
Hans Verkuil54450f52012-07-18 05:45:16 -03002048 if (handled)
2049 *handled = true;
2050 }
Mats Randgaardf24d2292013-12-10 10:15:13 -03002051 /* HDMI/DVI mode */
2052 if (irq_reg_0x6b & 0x01) {
2053 v4l2_dbg(1, debug, sd, "%s: irq %s mode\n", __func__,
2054 (io_read(sd, 0x6a) & 0x01) ? "HDMI" : "DVI");
2055 set_rgb_quantization_range(sd);
2056 if (handled)
2057 *handled = true;
2058 }
2059
Hans Verkuil54450f52012-07-18 05:45:16 -03002060 /* tx 5v detect */
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002061 tx_5v = io_read(sd, 0x70) & info->cable_det_mask;
Hans Verkuil54450f52012-07-18 05:45:16 -03002062 if (tx_5v) {
2063 v4l2_dbg(1, debug, sd, "%s: tx_5v: 0x%x\n", __func__, tx_5v);
2064 io_write(sd, 0x71, tx_5v);
2065 adv7604_s_detect_tx_5v_ctrl(sd);
2066 if (handled)
2067 *handled = true;
2068 }
2069 return 0;
2070}
2071
Hans Verkuilb09dfac2014-03-04 08:05:19 -03002072static int adv7604_get_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
Hans Verkuil54450f52012-07-18 05:45:16 -03002073{
2074 struct adv7604_state *state = to_state(sd);
Mats Randgaard4a31a932013-12-10 09:45:00 -03002075 u8 *data = NULL;
Hans Verkuil54450f52012-07-18 05:45:16 -03002076
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03002077 if (edid->pad > ADV7604_PAD_HDMI_PORT_D)
Hans Verkuil54450f52012-07-18 05:45:16 -03002078 return -EINVAL;
2079 if (edid->blocks == 0)
2080 return -EINVAL;
Mats Randgaard4a31a932013-12-10 09:45:00 -03002081 if (edid->blocks > 2)
Hans Verkuil54450f52012-07-18 05:45:16 -03002082 return -EINVAL;
Mats Randgaard4a31a932013-12-10 09:45:00 -03002083 if (edid->start_block > 1)
2084 return -EINVAL;
2085 if (edid->start_block == 1)
2086 edid->blocks = 1;
Mats Randgaard4a31a932013-12-10 09:45:00 -03002087
2088 if (edid->blocks > state->edid.blocks)
2089 edid->blocks = state->edid.blocks;
2090
2091 switch (edid->pad) {
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03002092 case ADV7604_PAD_HDMI_PORT_A:
2093 case ADV7604_PAD_HDMI_PORT_B:
2094 case ADV7604_PAD_HDMI_PORT_C:
2095 case ADV7604_PAD_HDMI_PORT_D:
Mats Randgaard4a31a932013-12-10 09:45:00 -03002096 if (state->edid.present & (1 << edid->pad))
2097 data = state->edid.edid;
2098 break;
2099 default:
2100 return -EINVAL;
2101 break;
2102 }
2103 if (!data)
2104 return -ENODATA;
2105
2106 memcpy(edid->edid,
2107 data + edid->start_block * 128,
Hans Verkuil54450f52012-07-18 05:45:16 -03002108 edid->blocks * 128);
2109 return 0;
2110}
2111
Mats Randgaarddd08beb2013-12-10 09:57:09 -03002112static int get_edid_spa_location(const u8 *edid)
Mats Randgaard3e86aa82013-12-10 09:55:18 -03002113{
2114 u8 d;
2115
2116 if ((edid[0x7e] != 1) ||
2117 (edid[0x80] != 0x02) ||
2118 (edid[0x81] != 0x03)) {
2119 return -1;
2120 }
2121
2122 /* search Vendor Specific Data Block (tag 3) */
2123 d = edid[0x82] & 0x7f;
2124 if (d > 4) {
2125 int i = 0x84;
2126 int end = 0x80 + d;
2127
2128 do {
2129 u8 tag = edid[i] >> 5;
2130 u8 len = edid[i] & 0x1f;
2131
2132 if ((tag == 3) && (len >= 5))
2133 return i + 4;
2134 i += len + 1;
2135 } while (i < end);
2136 }
2137 return -1;
2138}
2139
Hans Verkuilb09dfac2014-03-04 08:05:19 -03002140static int adv7604_set_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
Hans Verkuil54450f52012-07-18 05:45:16 -03002141{
2142 struct adv7604_state *state = to_state(sd);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002143 const struct adv7604_chip_info *info = state->info;
Mats Randgaarddd08beb2013-12-10 09:57:09 -03002144 int spa_loc;
Mats Randgaard3e86aa82013-12-10 09:55:18 -03002145 int tmp = 0;
Hans Verkuil54450f52012-07-18 05:45:16 -03002146 int err;
Mats Randgaarddd08beb2013-12-10 09:57:09 -03002147 int i;
Hans Verkuil54450f52012-07-18 05:45:16 -03002148
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03002149 if (edid->pad > ADV7604_PAD_HDMI_PORT_D)
Hans Verkuil54450f52012-07-18 05:45:16 -03002150 return -EINVAL;
2151 if (edid->start_block != 0)
2152 return -EINVAL;
2153 if (edid->blocks == 0) {
Mats Randgaard3e86aa82013-12-10 09:55:18 -03002154 /* Disable hotplug and I2C access to EDID RAM from DDC port */
Mats Randgaard4a31a932013-12-10 09:45:00 -03002155 state->edid.present &= ~(1 << edid->pad);
2156 v4l2_subdev_notify(sd, ADV7604_HOTPLUG, (void *)&state->edid.present);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002157 rep_write_and_or(sd, info->edid_enable_reg, 0xf0, state->edid.present);
Mats Randgaard3e86aa82013-12-10 09:55:18 -03002158
Hans Verkuil54450f52012-07-18 05:45:16 -03002159 /* Fall back to a 16:9 aspect ratio */
2160 state->aspect_ratio.numerator = 16;
2161 state->aspect_ratio.denominator = 9;
Mats Randgaard3e86aa82013-12-10 09:55:18 -03002162
2163 if (!state->edid.present)
2164 state->edid.blocks = 0;
2165
2166 v4l2_dbg(2, debug, sd, "%s: clear EDID pad %d, edid.present = 0x%x\n",
2167 __func__, edid->pad, state->edid.present);
Hans Verkuil54450f52012-07-18 05:45:16 -03002168 return 0;
2169 }
Mats Randgaard4a31a932013-12-10 09:45:00 -03002170 if (edid->blocks > 2) {
2171 edid->blocks = 2;
Hans Verkuil54450f52012-07-18 05:45:16 -03002172 return -E2BIG;
Mats Randgaard4a31a932013-12-10 09:45:00 -03002173 }
Mats Randgaard4a31a932013-12-10 09:45:00 -03002174
Mats Randgaarddd08beb2013-12-10 09:57:09 -03002175 v4l2_dbg(2, debug, sd, "%s: write EDID pad %d, edid.present = 0x%x\n",
2176 __func__, edid->pad, state->edid.present);
2177
Mats Randgaard3e86aa82013-12-10 09:55:18 -03002178 /* Disable hotplug and I2C access to EDID RAM from DDC port */
Mats Randgaard4a31a932013-12-10 09:45:00 -03002179 cancel_delayed_work_sync(&state->delayed_work_enable_hotplug);
Mats Randgaard3e86aa82013-12-10 09:55:18 -03002180 v4l2_subdev_notify(sd, ADV7604_HOTPLUG, (void *)&tmp);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002181 rep_write_and_or(sd, info->edid_enable_reg, 0xf0, 0x00);
Mats Randgaard3e86aa82013-12-10 09:55:18 -03002182
Mats Randgaarddd08beb2013-12-10 09:57:09 -03002183 spa_loc = get_edid_spa_location(edid->edid);
2184 if (spa_loc < 0)
2185 spa_loc = 0xc0; /* Default value [REF_02, p. 116] */
2186
Mats Randgaard3e86aa82013-12-10 09:55:18 -03002187 switch (edid->pad) {
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03002188 case ADV7604_PAD_HDMI_PORT_A:
Mats Randgaarddd08beb2013-12-10 09:57:09 -03002189 state->spa_port_a[0] = edid->edid[spa_loc];
2190 state->spa_port_a[1] = edid->edid[spa_loc + 1];
Mats Randgaard3e86aa82013-12-10 09:55:18 -03002191 break;
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03002192 case ADV7604_PAD_HDMI_PORT_B:
Mats Randgaarddd08beb2013-12-10 09:57:09 -03002193 rep_write(sd, 0x70, edid->edid[spa_loc]);
2194 rep_write(sd, 0x71, edid->edid[spa_loc + 1]);
Mats Randgaard3e86aa82013-12-10 09:55:18 -03002195 break;
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03002196 case ADV7604_PAD_HDMI_PORT_C:
Mats Randgaarddd08beb2013-12-10 09:57:09 -03002197 rep_write(sd, 0x72, edid->edid[spa_loc]);
2198 rep_write(sd, 0x73, edid->edid[spa_loc + 1]);
Mats Randgaard3e86aa82013-12-10 09:55:18 -03002199 break;
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03002200 case ADV7604_PAD_HDMI_PORT_D:
Mats Randgaarddd08beb2013-12-10 09:57:09 -03002201 rep_write(sd, 0x74, edid->edid[spa_loc]);
2202 rep_write(sd, 0x75, edid->edid[spa_loc + 1]);
Mats Randgaard3e86aa82013-12-10 09:55:18 -03002203 break;
Mats Randgaarddd08beb2013-12-10 09:57:09 -03002204 default:
2205 return -EINVAL;
Mats Randgaard3e86aa82013-12-10 09:55:18 -03002206 }
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002207
2208 if (info->type == ADV7604) {
2209 rep_write(sd, 0x76, spa_loc & 0xff);
2210 rep_write_and_or(sd, 0x77, 0xbf, (spa_loc & 0x100) >> 2);
2211 } else {
2212 /* FIXME: Where is the SPA location LSB register ? */
2213 rep_write_and_or(sd, 0x71, 0xfe, (spa_loc & 0x100) >> 8);
2214 }
Mats Randgaard3e86aa82013-12-10 09:55:18 -03002215
Mats Randgaarddd08beb2013-12-10 09:57:09 -03002216 edid->edid[spa_loc] = state->spa_port_a[0];
2217 edid->edid[spa_loc + 1] = state->spa_port_a[1];
Mats Randgaard4a31a932013-12-10 09:45:00 -03002218
2219 memcpy(state->edid.edid, edid->edid, 128 * edid->blocks);
2220 state->edid.blocks = edid->blocks;
Hans Verkuil54450f52012-07-18 05:45:16 -03002221 state->aspect_ratio = v4l2_calc_aspect_ratio(edid->edid[0x15],
2222 edid->edid[0x16]);
Mats Randgaard3e86aa82013-12-10 09:55:18 -03002223 state->edid.present |= 1 << edid->pad;
Mats Randgaard4a31a932013-12-10 09:45:00 -03002224
2225 err = edid_write_block(sd, 128 * edid->blocks, state->edid.edid);
2226 if (err < 0) {
Mats Randgaard3e86aa82013-12-10 09:55:18 -03002227 v4l2_err(sd, "error %d writing edid pad %d\n", err, edid->pad);
Mats Randgaard4a31a932013-12-10 09:45:00 -03002228 return err;
2229 }
2230
Mats Randgaarddd08beb2013-12-10 09:57:09 -03002231 /* adv7604 calculates the checksums and enables I2C access to internal
2232 EDID RAM from DDC port. */
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002233 rep_write_and_or(sd, info->edid_enable_reg, 0xf0, state->edid.present);
Mats Randgaarddd08beb2013-12-10 09:57:09 -03002234
2235 for (i = 0; i < 1000; i++) {
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002236 if (rep_read(sd, info->edid_status_reg) & state->edid.present)
Mats Randgaarddd08beb2013-12-10 09:57:09 -03002237 break;
2238 mdelay(1);
2239 }
2240 if (i == 1000) {
2241 v4l2_err(sd, "error enabling edid (0x%x)\n", state->edid.present);
2242 return -EIO;
2243 }
2244
2245
Mats Randgaard4a31a932013-12-10 09:45:00 -03002246 /* enable hotplug after 100 ms */
2247 queue_delayed_work(state->work_queues,
2248 &state->delayed_work_enable_hotplug, HZ / 10);
2249 return 0;
Hans Verkuil54450f52012-07-18 05:45:16 -03002250}
2251
2252/*********** avi info frame CEA-861-E **************/
2253
2254static void print_avi_infoframe(struct v4l2_subdev *sd)
2255{
2256 int i;
2257 u8 buf[14];
2258 u8 avi_len;
2259 u8 avi_ver;
2260
Martin Buggebb88f322013-08-14 08:52:46 -03002261 if (!is_hdmi(sd)) {
Hans Verkuil54450f52012-07-18 05:45:16 -03002262 v4l2_info(sd, "receive DVI-D signal (AVI infoframe not supported)\n");
2263 return;
2264 }
2265 if (!(io_read(sd, 0x60) & 0x01)) {
2266 v4l2_info(sd, "AVI infoframe not received\n");
2267 return;
2268 }
2269
2270 if (io_read(sd, 0x83) & 0x01) {
2271 v4l2_info(sd, "AVI infoframe checksum error has occurred earlier\n");
2272 io_write(sd, 0x85, 0x01); /* clear AVI_INF_CKS_ERR_RAW */
2273 if (io_read(sd, 0x83) & 0x01) {
2274 v4l2_info(sd, "AVI infoframe checksum error still present\n");
2275 io_write(sd, 0x85, 0x01); /* clear AVI_INF_CKS_ERR_RAW */
2276 }
2277 }
2278
2279 avi_len = infoframe_read(sd, 0xe2);
2280 avi_ver = infoframe_read(sd, 0xe1);
2281 v4l2_info(sd, "AVI infoframe version %d (%d byte)\n",
2282 avi_ver, avi_len);
2283
2284 if (avi_ver != 0x02)
2285 return;
2286
2287 for (i = 0; i < 14; i++)
2288 buf[i] = infoframe_read(sd, i);
2289
2290 v4l2_info(sd,
2291 "\t%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
2292 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7],
2293 buf[8], buf[9], buf[10], buf[11], buf[12], buf[13]);
2294}
2295
2296static int adv7604_log_status(struct v4l2_subdev *sd)
2297{
2298 struct adv7604_state *state = to_state(sd);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002299 const struct adv7604_chip_info *info = state->info;
Hans Verkuil54450f52012-07-18 05:45:16 -03002300 struct v4l2_dv_timings timings;
2301 struct stdi_readback stdi;
2302 u8 reg_io_0x02 = io_read(sd, 0x02);
Laurent Pinchart4a2ccdd2014-01-08 20:26:55 -03002303 u8 edid_enabled;
2304 u8 cable_det;
Hans Verkuil54450f52012-07-18 05:45:16 -03002305
Lars-Peter Clausenf216ccb2013-11-25 16:15:29 -03002306 static const char * const csc_coeff_sel_rb[16] = {
Hans Verkuil54450f52012-07-18 05:45:16 -03002307 "bypassed", "YPbPr601 -> RGB", "reserved", "YPbPr709 -> RGB",
2308 "reserved", "RGB -> YPbPr601", "reserved", "RGB -> YPbPr709",
2309 "reserved", "YPbPr709 -> YPbPr601", "YPbPr601 -> YPbPr709",
2310 "reserved", "reserved", "reserved", "reserved", "manual"
2311 };
Lars-Peter Clausenf216ccb2013-11-25 16:15:29 -03002312 static const char * const input_color_space_txt[16] = {
Hans Verkuil54450f52012-07-18 05:45:16 -03002313 "RGB limited range (16-235)", "RGB full range (0-255)",
2314 "YCbCr Bt.601 (16-235)", "YCbCr Bt.709 (16-235)",
Mats Randgaard98332392013-12-05 10:05:58 -03002315 "xvYCC Bt.601", "xvYCC Bt.709",
Hans Verkuil54450f52012-07-18 05:45:16 -03002316 "YCbCr Bt.601 (0-255)", "YCbCr Bt.709 (0-255)",
2317 "invalid", "invalid", "invalid", "invalid", "invalid",
2318 "invalid", "invalid", "automatic"
2319 };
Lars-Peter Clausenf216ccb2013-11-25 16:15:29 -03002320 static const char * const rgb_quantization_range_txt[] = {
Hans Verkuil54450f52012-07-18 05:45:16 -03002321 "Automatic",
2322 "RGB limited range (16-235)",
2323 "RGB full range (0-255)",
2324 };
Lars-Peter Clausenf216ccb2013-11-25 16:15:29 -03002325 static const char * const deep_color_mode_txt[4] = {
Martin Buggebb88f322013-08-14 08:52:46 -03002326 "8-bits per channel",
2327 "10-bits per channel",
2328 "12-bits per channel",
2329 "16-bits per channel (not supported)"
2330 };
Hans Verkuil54450f52012-07-18 05:45:16 -03002331
2332 v4l2_info(sd, "-----Chip status-----\n");
2333 v4l2_info(sd, "Chip power: %s\n", no_power(sd) ? "off" : "on");
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002334 edid_enabled = rep_read(sd, info->edid_status_reg);
Mats Randgaard4a31a932013-12-10 09:45:00 -03002335 v4l2_info(sd, "EDID enabled port A: %s, B: %s, C: %s, D: %s\n",
Laurent Pinchart4a2ccdd2014-01-08 20:26:55 -03002336 ((edid_enabled & 0x01) ? "Yes" : "No"),
2337 ((edid_enabled & 0x02) ? "Yes" : "No"),
2338 ((edid_enabled & 0x04) ? "Yes" : "No"),
2339 ((edid_enabled & 0x08) ? "Yes" : "No"));
Hans Verkuil54450f52012-07-18 05:45:16 -03002340 v4l2_info(sd, "CEC: %s\n", !!(cec_read(sd, 0x2a) & 0x01) ?
2341 "enabled" : "disabled");
2342
2343 v4l2_info(sd, "-----Signal status-----\n");
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002344 cable_det = info->read_cable_det(sd);
Mats Randgaard4a31a932013-12-10 09:45:00 -03002345 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 -03002346 ((cable_det & 0x01) ? "Yes" : "No"),
2347 ((cable_det & 0x02) ? "Yes" : "No"),
Laurent Pinchart4a2ccdd2014-01-08 20:26:55 -03002348 ((cable_det & 0x04) ? "Yes" : "No"),
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002349 ((cable_det & 0x08) ? "Yes" : "No"));
Hans Verkuil54450f52012-07-18 05:45:16 -03002350 v4l2_info(sd, "TMDS signal detected: %s\n",
2351 no_signal_tmds(sd) ? "false" : "true");
2352 v4l2_info(sd, "TMDS signal locked: %s\n",
2353 no_lock_tmds(sd) ? "false" : "true");
2354 v4l2_info(sd, "SSPD locked: %s\n", no_lock_sspd(sd) ? "false" : "true");
2355 v4l2_info(sd, "STDI locked: %s\n", no_lock_stdi(sd) ? "false" : "true");
2356 v4l2_info(sd, "CP locked: %s\n", no_lock_cp(sd) ? "false" : "true");
2357 v4l2_info(sd, "CP free run: %s\n",
2358 (!!(cp_read(sd, 0xff) & 0x10) ? "on" : "off"));
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03002359 v4l2_info(sd, "Prim-mode = 0x%x, video std = 0x%x, v_freq = 0x%x\n",
2360 io_read(sd, 0x01) & 0x0f, io_read(sd, 0x00) & 0x3f,
2361 (io_read(sd, 0x01) & 0x70) >> 4);
Hans Verkuil54450f52012-07-18 05:45:16 -03002362
2363 v4l2_info(sd, "-----Video Timings-----\n");
2364 if (read_stdi(sd, &stdi))
2365 v4l2_info(sd, "STDI: not locked\n");
2366 else
2367 v4l2_info(sd, "STDI: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, %s, %chsync, %cvsync\n",
2368 stdi.lcf, stdi.bl, stdi.lcvs,
2369 stdi.interlaced ? "interlaced" : "progressive",
2370 stdi.hs_pol, stdi.vs_pol);
2371 if (adv7604_query_dv_timings(sd, &timings))
2372 v4l2_info(sd, "No video detected\n");
2373 else
Hans Verkuil11d034c2013-08-15 08:05:59 -03002374 v4l2_print_dv_timings(sd->name, "Detected format: ",
2375 &timings, true);
2376 v4l2_print_dv_timings(sd->name, "Configured format: ",
2377 &state->timings, true);
Hans Verkuil54450f52012-07-18 05:45:16 -03002378
Mats Randgaard76eb2d32013-08-14 08:56:57 -03002379 if (no_signal(sd))
2380 return 0;
2381
Hans Verkuil54450f52012-07-18 05:45:16 -03002382 v4l2_info(sd, "-----Color space-----\n");
2383 v4l2_info(sd, "RGB quantization range ctrl: %s\n",
2384 rgb_quantization_range_txt[state->rgb_quantization_range]);
2385 v4l2_info(sd, "Input color space: %s\n",
2386 input_color_space_txt[reg_io_0x02 >> 4]);
2387 v4l2_info(sd, "Output color space: %s %s, saturator %s\n",
2388 (reg_io_0x02 & 0x02) ? "RGB" : "YCbCr",
2389 (reg_io_0x02 & 0x04) ? "(16-235)" : "(0-255)",
2390 ((reg_io_0x02 & 0x04) ^ (reg_io_0x02 & 0x01)) ?
Mats Randgaard76eb2d32013-08-14 08:56:57 -03002391 "enabled" : "disabled");
Hans Verkuil54450f52012-07-18 05:45:16 -03002392 v4l2_info(sd, "Color space conversion: %s\n",
2393 csc_coeff_sel_rb[cp_read(sd, 0xfc) >> 4]);
2394
Mats Randgaard4a31a932013-12-10 09:45:00 -03002395 if (!is_digital_input(sd))
Mats Randgaard76eb2d32013-08-14 08:56:57 -03002396 return 0;
2397
2398 v4l2_info(sd, "-----%s status-----\n", is_hdmi(sd) ? "HDMI" : "DVI-D");
Mats Randgaard4a31a932013-12-10 09:45:00 -03002399 v4l2_info(sd, "Digital video port selected: %c\n",
2400 (hdmi_read(sd, 0x00) & 0x03) + 'A');
2401 v4l2_info(sd, "HDCP encrypted content: %s\n",
2402 (hdmi_read(sd, 0x05) & 0x40) ? "true" : "false");
Mats Randgaard76eb2d32013-08-14 08:56:57 -03002403 v4l2_info(sd, "HDCP keys read: %s%s\n",
2404 (hdmi_read(sd, 0x04) & 0x20) ? "yes" : "no",
2405 (hdmi_read(sd, 0x04) & 0x10) ? "ERROR" : "");
2406 if (!is_hdmi(sd)) {
2407 bool audio_pll_locked = hdmi_read(sd, 0x04) & 0x01;
2408 bool audio_sample_packet_detect = hdmi_read(sd, 0x18) & 0x01;
2409 bool audio_mute = io_read(sd, 0x65) & 0x40;
2410
2411 v4l2_info(sd, "Audio: pll %s, samples %s, %s\n",
2412 audio_pll_locked ? "locked" : "not locked",
2413 audio_sample_packet_detect ? "detected" : "not detected",
2414 audio_mute ? "muted" : "enabled");
2415 if (audio_pll_locked && audio_sample_packet_detect) {
2416 v4l2_info(sd, "Audio format: %s\n",
2417 (hdmi_read(sd, 0x07) & 0x20) ? "multi-channel" : "stereo");
2418 }
2419 v4l2_info(sd, "Audio CTS: %u\n", (hdmi_read(sd, 0x5b) << 12) +
2420 (hdmi_read(sd, 0x5c) << 8) +
2421 (hdmi_read(sd, 0x5d) & 0xf0));
2422 v4l2_info(sd, "Audio N: %u\n", ((hdmi_read(sd, 0x5d) & 0x0f) << 16) +
2423 (hdmi_read(sd, 0x5e) << 8) +
2424 hdmi_read(sd, 0x5f));
2425 v4l2_info(sd, "AV Mute: %s\n", (hdmi_read(sd, 0x04) & 0x40) ? "on" : "off");
2426
2427 v4l2_info(sd, "Deep color mode: %s\n", deep_color_mode_txt[(hdmi_read(sd, 0x0b) & 0x60) >> 5]);
2428
Hans Verkuil54450f52012-07-18 05:45:16 -03002429 print_avi_infoframe(sd);
2430 }
2431
2432 return 0;
2433}
2434
2435/* ----------------------------------------------------------------------- */
2436
2437static const struct v4l2_ctrl_ops adv7604_ctrl_ops = {
2438 .s_ctrl = adv7604_s_ctrl,
2439};
2440
2441static const struct v4l2_subdev_core_ops adv7604_core_ops = {
2442 .log_status = adv7604_log_status,
Hans Verkuil54450f52012-07-18 05:45:16 -03002443 .interrupt_service_routine = adv7604_isr,
2444#ifdef CONFIG_VIDEO_ADV_DEBUG
2445 .g_register = adv7604_g_register,
2446 .s_register = adv7604_s_register,
2447#endif
2448};
2449
2450static const struct v4l2_subdev_video_ops adv7604_video_ops = {
2451 .s_routing = adv7604_s_routing,
2452 .g_input_status = adv7604_g_input_status,
2453 .s_dv_timings = adv7604_s_dv_timings,
2454 .g_dv_timings = adv7604_g_dv_timings,
2455 .query_dv_timings = adv7604_query_dv_timings,
2456 .enum_dv_timings = adv7604_enum_dv_timings,
2457 .dv_timings_cap = adv7604_dv_timings_cap,
Hans Verkuil54450f52012-07-18 05:45:16 -03002458};
2459
2460static const struct v4l2_subdev_pad_ops adv7604_pad_ops = {
Laurent Pinchart539b33b2014-01-26 18:42:37 -03002461 .enum_mbus_code = adv7604_enum_mbus_code,
2462 .get_fmt = adv7604_get_format,
2463 .set_fmt = adv7604_set_format,
Hans Verkuil54450f52012-07-18 05:45:16 -03002464 .get_edid = adv7604_get_edid,
2465 .set_edid = adv7604_set_edid,
Laurent Pinchartafec5592014-01-29 10:09:41 -03002466 .dv_timings_cap = adv7604_pad_dv_timings_cap,
2467 .enum_dv_timings = adv7604_enum_dv_timings,
Hans Verkuil54450f52012-07-18 05:45:16 -03002468};
2469
2470static const struct v4l2_subdev_ops adv7604_ops = {
2471 .core = &adv7604_core_ops,
2472 .video = &adv7604_video_ops,
2473 .pad = &adv7604_pad_ops,
2474};
2475
2476/* -------------------------- custom ctrls ---------------------------------- */
2477
2478static const struct v4l2_ctrl_config adv7604_ctrl_analog_sampling_phase = {
2479 .ops = &adv7604_ctrl_ops,
2480 .id = V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE,
2481 .name = "Analog Sampling Phase",
2482 .type = V4L2_CTRL_TYPE_INTEGER,
2483 .min = 0,
2484 .max = 0x1f,
2485 .step = 1,
2486 .def = 0,
2487};
2488
2489static const struct v4l2_ctrl_config adv7604_ctrl_free_run_color_manual = {
2490 .ops = &adv7604_ctrl_ops,
2491 .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL,
2492 .name = "Free Running Color, Manual",
2493 .type = V4L2_CTRL_TYPE_BOOLEAN,
2494 .min = false,
2495 .max = true,
2496 .step = 1,
2497 .def = false,
2498};
2499
2500static const struct v4l2_ctrl_config adv7604_ctrl_free_run_color = {
2501 .ops = &adv7604_ctrl_ops,
2502 .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR,
2503 .name = "Free Running Color",
2504 .type = V4L2_CTRL_TYPE_INTEGER,
2505 .min = 0x0,
2506 .max = 0xffffff,
2507 .step = 0x1,
2508 .def = 0x0,
2509};
2510
2511/* ----------------------------------------------------------------------- */
2512
2513static int adv7604_core_init(struct v4l2_subdev *sd)
2514{
2515 struct adv7604_state *state = to_state(sd);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002516 const struct adv7604_chip_info *info = state->info;
Hans Verkuil54450f52012-07-18 05:45:16 -03002517 struct adv7604_platform_data *pdata = &state->pdata;
2518
2519 hdmi_write(sd, 0x48,
2520 (pdata->disable_pwrdnb ? 0x80 : 0) |
2521 (pdata->disable_cable_det_rst ? 0x40 : 0));
2522
2523 disable_input(sd);
2524
2525 /* power */
2526 io_write(sd, 0x0c, 0x42); /* Power up part and power down VDP */
2527 io_write(sd, 0x0b, 0x44); /* Power down ESDP block */
2528 cp_write(sd, 0xcf, 0x01); /* Power down macrovision */
2529
2530 /* video format */
2531 io_write_and_or(sd, 0x02, 0xf0,
2532 pdata->alt_gamma << 3 |
2533 pdata->op_656_range << 2 |
Hans Verkuil54450f52012-07-18 05:45:16 -03002534 pdata->alt_data_sat << 0);
Laurent Pinchart539b33b2014-01-26 18:42:37 -03002535 io_write_and_or(sd, 0x05, 0xf1, pdata->blank_data << 3 |
2536 pdata->insert_av_codes << 2 |
2537 pdata->replicate_av_codes << 1);
2538 adv7604_setup_format(state);
Hans Verkuil54450f52012-07-18 05:45:16 -03002539
Hans Verkuil54450f52012-07-18 05:45:16 -03002540 cp_write(sd, 0x69, 0x30); /* Enable CP CSC */
Martin Bugge98908692013-12-20 05:14:57 -03002541
2542 /* VS, HS polarities */
2543 io_write(sd, 0x06, 0xa0 | pdata->inv_vs_pol << 2 | pdata->inv_hs_pol << 1);
Mikhail Khelikf31b62e2013-12-20 05:12:00 -03002544
2545 /* Adjust drive strength */
2546 io_write(sd, 0x14, 0x40 | pdata->dr_str_data << 4 |
2547 pdata->dr_str_clk << 2 |
2548 pdata->dr_str_sync);
2549
Hans Verkuil54450f52012-07-18 05:45:16 -03002550 cp_write(sd, 0xba, (pdata->hdmi_free_run_mode << 1) | 0x01); /* HDMI free run */
2551 cp_write(sd, 0xf3, 0xdc); /* Low threshold to enter/exit free run mode */
2552 cp_write(sd, 0xf9, 0x23); /* STDI ch. 1 - LCVS change threshold -
Hans Verkuil80939642012-10-16 05:46:21 -03002553 ADI recommended setting [REF_01, c. 2.3.3] */
Hans Verkuil54450f52012-07-18 05:45:16 -03002554 cp_write(sd, 0x45, 0x23); /* STDI ch. 2 - LCVS change threshold -
Hans Verkuil80939642012-10-16 05:46:21 -03002555 ADI recommended setting [REF_01, c. 2.3.3] */
Hans Verkuil54450f52012-07-18 05:45:16 -03002556 cp_write(sd, 0xc9, 0x2d); /* use prim_mode and vid_std as free run resolution
2557 for digital formats */
2558
Mats Randgaard5474b982013-12-05 10:33:41 -03002559 /* HDMI audio */
2560 hdmi_write_and_or(sd, 0x15, 0xfc, 0x03); /* Mute on FIFO over-/underflow [REF_01, c. 1.2.18] */
2561 hdmi_write_and_or(sd, 0x1a, 0xf1, 0x08); /* Wait 1 s before unmute */
2562 hdmi_write_and_or(sd, 0x68, 0xf9, 0x06); /* FIFO reset on over-/underflow [REF_01, c. 1.2.19] */
2563
Hans Verkuil54450f52012-07-18 05:45:16 -03002564 /* TODO from platform data */
2565 afe_write(sd, 0xb5, 0x01); /* Setting MCLK to 256Fs */
2566
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002567 if (adv7604_has_afe(state)) {
2568 afe_write(sd, 0x02, pdata->ain_sel); /* Select analog input muxing mode */
2569 io_write_and_or(sd, 0x30, ~(1 << 4), pdata->output_bus_lsb_to_msb << 4);
2570 }
Hans Verkuil54450f52012-07-18 05:45:16 -03002571
Hans Verkuil54450f52012-07-18 05:45:16 -03002572 /* interrupts */
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002573 io_write(sd, 0x40, 0xc0 | pdata->int1_config); /* Configure INT1 */
Hans Verkuil54450f52012-07-18 05:45:16 -03002574 io_write(sd, 0x46, 0x98); /* Enable SSPD, STDI and CP unlocked interrupts */
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002575 io_write(sd, 0x6e, info->fmt_change_digital_mask); /* Enable V_LOCKED and DE_REGEN_LCK interrupts */
2576 io_write(sd, 0x73, info->cable_det_mask); /* Enable cable detection (+5v) interrupts */
2577 info->setup_irqs(sd);
Hans Verkuil54450f52012-07-18 05:45:16 -03002578
2579 return v4l2_ctrl_handler_setup(sd->ctrl_handler);
2580}
2581
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002582static void adv7604_setup_irqs(struct v4l2_subdev *sd)
2583{
2584 io_write(sd, 0x41, 0xd7); /* STDI irq for any change, disable INT2 */
2585}
2586
2587static void adv7611_setup_irqs(struct v4l2_subdev *sd)
2588{
2589 io_write(sd, 0x41, 0xd0); /* STDI irq for any change, disable INT2 */
2590}
2591
Hans Verkuil54450f52012-07-18 05:45:16 -03002592static void adv7604_unregister_clients(struct adv7604_state *state)
2593{
2594 if (state->i2c_avlink)
2595 i2c_unregister_device(state->i2c_avlink);
2596 if (state->i2c_cec)
2597 i2c_unregister_device(state->i2c_cec);
2598 if (state->i2c_infoframe)
2599 i2c_unregister_device(state->i2c_infoframe);
2600 if (state->i2c_esdp)
2601 i2c_unregister_device(state->i2c_esdp);
2602 if (state->i2c_dpp)
2603 i2c_unregister_device(state->i2c_dpp);
2604 if (state->i2c_afe)
2605 i2c_unregister_device(state->i2c_afe);
2606 if (state->i2c_repeater)
2607 i2c_unregister_device(state->i2c_repeater);
2608 if (state->i2c_edid)
2609 i2c_unregister_device(state->i2c_edid);
2610 if (state->i2c_hdmi)
2611 i2c_unregister_device(state->i2c_hdmi);
2612 if (state->i2c_test)
2613 i2c_unregister_device(state->i2c_test);
2614 if (state->i2c_cp)
2615 i2c_unregister_device(state->i2c_cp);
2616 if (state->i2c_vdp)
2617 i2c_unregister_device(state->i2c_vdp);
2618}
2619
2620static struct i2c_client *adv7604_dummy_client(struct v4l2_subdev *sd,
2621 u8 addr, u8 io_reg)
2622{
2623 struct i2c_client *client = v4l2_get_subdevdata(sd);
2624
2625 if (addr)
2626 io_write(sd, io_reg, addr << 1);
2627 return i2c_new_dummy(client->adapter, io_read(sd, io_reg) >> 1);
2628}
2629
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002630static const struct adv7604_reg_seq adv7604_recommended_settings_afe[] = {
2631 /* reset ADI recommended settings for HDMI: */
2632 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 4. */
2633 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x0d), 0x04 }, /* HDMI filter optimization */
2634 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x0d), 0x04 }, /* HDMI filter optimization */
2635 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x3d), 0x00 }, /* DDC bus active pull-up control */
2636 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x3e), 0x74 }, /* TMDS PLL optimization */
2637 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x4e), 0x3b }, /* TMDS PLL optimization */
2638 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x57), 0x74 }, /* TMDS PLL optimization */
2639 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x58), 0x63 }, /* TMDS PLL optimization */
2640 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8d), 0x18 }, /* equaliser */
2641 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8e), 0x34 }, /* equaliser */
2642 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x93), 0x88 }, /* equaliser */
2643 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x94), 0x2e }, /* equaliser */
2644 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x96), 0x00 }, /* enable automatic EQ changing */
2645
2646 /* set ADI recommended settings for digitizer */
2647 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 17. */
2648 { ADV7604_REG(ADV7604_PAGE_AFE, 0x12), 0x7b }, /* ADC noise shaping filter controls */
2649 { ADV7604_REG(ADV7604_PAGE_AFE, 0x0c), 0x1f }, /* CP core gain controls */
2650 { ADV7604_REG(ADV7604_PAGE_CP, 0x3e), 0x04 }, /* CP core pre-gain control */
2651 { ADV7604_REG(ADV7604_PAGE_CP, 0xc3), 0x39 }, /* CP coast control. Graphics mode */
2652 { ADV7604_REG(ADV7604_PAGE_CP, 0x40), 0x5c }, /* CP core pre-gain control. Graphics mode */
2653
2654 { ADV7604_REG_SEQ_TERM, 0 },
2655};
2656
2657static const struct adv7604_reg_seq adv7604_recommended_settings_hdmi[] = {
2658 /* set ADI recommended settings for HDMI: */
2659 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 4. */
2660 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x0d), 0x84 }, /* HDMI filter optimization */
2661 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x3d), 0x10 }, /* DDC bus active pull-up control */
2662 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x3e), 0x39 }, /* TMDS PLL optimization */
2663 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x4e), 0x3b }, /* TMDS PLL optimization */
2664 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x57), 0xb6 }, /* TMDS PLL optimization */
2665 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x58), 0x03 }, /* TMDS PLL optimization */
2666 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8d), 0x18 }, /* equaliser */
2667 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8e), 0x34 }, /* equaliser */
2668 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x93), 0x8b }, /* equaliser */
2669 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x94), 0x2d }, /* equaliser */
2670 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x96), 0x01 }, /* enable automatic EQ changing */
2671
2672 /* reset ADI recommended settings for digitizer */
2673 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 17. */
2674 { ADV7604_REG(ADV7604_PAGE_AFE, 0x12), 0xfb }, /* ADC noise shaping filter controls */
2675 { ADV7604_REG(ADV7604_PAGE_AFE, 0x0c), 0x0d }, /* CP core gain controls */
2676
2677 { ADV7604_REG_SEQ_TERM, 0 },
2678};
2679
2680static const struct adv7604_reg_seq adv7611_recommended_settings_hdmi[] = {
2681 { ADV7604_REG(ADV7604_PAGE_CP, 0x6c), 0x00 },
2682 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x6f), 0x0c },
2683 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x87), 0x70 },
2684 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x57), 0xda },
2685 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x58), 0x01 },
2686 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x03), 0x98 },
2687 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x4c), 0x44 },
2688 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8d), 0x04 },
2689 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8e), 0x1e },
2690
2691 { ADV7604_REG_SEQ_TERM, 0 },
2692};
2693
2694static const struct adv7604_chip_info adv7604_chip_info[] = {
2695 [ADV7604] = {
2696 .type = ADV7604,
2697 .has_afe = true,
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03002698 .max_port = ADV7604_PAD_VGA_COMP,
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002699 .num_dv_ports = 4,
2700 .edid_enable_reg = 0x77,
2701 .edid_status_reg = 0x7d,
2702 .lcf_reg = 0xb3,
2703 .tdms_lock_mask = 0xe0,
2704 .cable_det_mask = 0x1e,
2705 .fmt_change_digital_mask = 0xc1,
Laurent Pinchart539b33b2014-01-26 18:42:37 -03002706 .formats = adv7604_formats,
2707 .nformats = ARRAY_SIZE(adv7604_formats),
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002708 .set_termination = adv7604_set_termination,
2709 .setup_irqs = adv7604_setup_irqs,
2710 .read_hdmi_pixelclock = adv7604_read_hdmi_pixelclock,
2711 .read_cable_det = adv7604_read_cable_det,
2712 .recommended_settings = {
2713 [0] = adv7604_recommended_settings_afe,
2714 [1] = adv7604_recommended_settings_hdmi,
2715 },
2716 .num_recommended_settings = {
2717 [0] = ARRAY_SIZE(adv7604_recommended_settings_afe),
2718 [1] = ARRAY_SIZE(adv7604_recommended_settings_hdmi),
2719 },
2720 .page_mask = BIT(ADV7604_PAGE_IO) | BIT(ADV7604_PAGE_AVLINK) |
2721 BIT(ADV7604_PAGE_CEC) | BIT(ADV7604_PAGE_INFOFRAME) |
2722 BIT(ADV7604_PAGE_ESDP) | BIT(ADV7604_PAGE_DPP) |
2723 BIT(ADV7604_PAGE_AFE) | BIT(ADV7604_PAGE_REP) |
2724 BIT(ADV7604_PAGE_EDID) | BIT(ADV7604_PAGE_HDMI) |
2725 BIT(ADV7604_PAGE_TEST) | BIT(ADV7604_PAGE_CP) |
2726 BIT(ADV7604_PAGE_VDP),
2727 },
2728 [ADV7611] = {
2729 .type = ADV7611,
2730 .has_afe = false,
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03002731 .max_port = ADV7604_PAD_HDMI_PORT_A,
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002732 .num_dv_ports = 1,
2733 .edid_enable_reg = 0x74,
2734 .edid_status_reg = 0x76,
2735 .lcf_reg = 0xa3,
2736 .tdms_lock_mask = 0x43,
2737 .cable_det_mask = 0x01,
2738 .fmt_change_digital_mask = 0x03,
Laurent Pinchart539b33b2014-01-26 18:42:37 -03002739 .formats = adv7611_formats,
2740 .nformats = ARRAY_SIZE(adv7611_formats),
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002741 .set_termination = adv7611_set_termination,
2742 .setup_irqs = adv7611_setup_irqs,
2743 .read_hdmi_pixelclock = adv7611_read_hdmi_pixelclock,
2744 .read_cable_det = adv7611_read_cable_det,
2745 .recommended_settings = {
2746 [1] = adv7611_recommended_settings_hdmi,
2747 },
2748 .num_recommended_settings = {
2749 [1] = ARRAY_SIZE(adv7611_recommended_settings_hdmi),
2750 },
2751 .page_mask = BIT(ADV7604_PAGE_IO) | BIT(ADV7604_PAGE_CEC) |
2752 BIT(ADV7604_PAGE_INFOFRAME) | BIT(ADV7604_PAGE_AFE) |
2753 BIT(ADV7604_PAGE_REP) | BIT(ADV7604_PAGE_EDID) |
2754 BIT(ADV7604_PAGE_HDMI) | BIT(ADV7604_PAGE_CP),
2755 },
2756};
2757
Hans Verkuil54450f52012-07-18 05:45:16 -03002758static int adv7604_probe(struct i2c_client *client,
2759 const struct i2c_device_id *id)
2760{
Hans Verkuil591b72f2013-12-17 10:05:13 -03002761 static const struct v4l2_dv_timings cea640x480 =
2762 V4L2_DV_BT_CEA_640X480P59_94;
Hans Verkuil54450f52012-07-18 05:45:16 -03002763 struct adv7604_state *state;
2764 struct adv7604_platform_data *pdata = client->dev.platform_data;
2765 struct v4l2_ctrl_handler *hdl;
2766 struct v4l2_subdev *sd;
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03002767 unsigned int i;
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002768 u16 val;
Hans Verkuil54450f52012-07-18 05:45:16 -03002769 int err;
2770
2771 /* Check if the adapter supports the needed features */
2772 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
2773 return -EIO;
2774 v4l_dbg(1, debug, client, "detecting adv7604 client on address 0x%x\n",
2775 client->addr << 1);
2776
Laurent Pinchartc02b2112013-05-02 08:29:43 -03002777 state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
Hans Verkuil54450f52012-07-18 05:45:16 -03002778 if (!state) {
2779 v4l_err(client, "Could not allocate adv7604_state memory!\n");
2780 return -ENOMEM;
2781 }
2782
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002783 state->info = &adv7604_chip_info[id->driver_data];
2784
Mats Randgaard25a64ac2013-08-14 07:58:45 -03002785 /* initialize variables */
2786 state->restart_stdi_once = true;
Mats Randgaardff4f80f2013-12-05 10:24:05 -03002787 state->selected_input = ~0;
Mats Randgaard25a64ac2013-08-14 07:58:45 -03002788
Hans Verkuil54450f52012-07-18 05:45:16 -03002789 /* platform data */
2790 if (!pdata) {
2791 v4l_err(client, "No platform data!\n");
Laurent Pinchartc02b2112013-05-02 08:29:43 -03002792 return -ENODEV;
Hans Verkuil54450f52012-07-18 05:45:16 -03002793 }
Hans Verkuil591b72f2013-12-17 10:05:13 -03002794 state->pdata = *pdata;
2795 state->timings = cea640x480;
Laurent Pinchart539b33b2014-01-26 18:42:37 -03002796 state->format = adv7604_format_info(state, V4L2_MBUS_FMT_YUYV8_2X8);
Hans Verkuil54450f52012-07-18 05:45:16 -03002797
2798 sd = &state->sd;
2799 v4l2_i2c_subdev_init(sd, client, &adv7604_ops);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002800 snprintf(sd->name, sizeof(sd->name), "%s %d-%04x",
2801 id->name, i2c_adapter_id(client->adapter),
2802 client->addr);
Hans Verkuil54450f52012-07-18 05:45:16 -03002803 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
Hans Verkuil54450f52012-07-18 05:45:16 -03002804
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002805 /*
2806 * Verify that the chip is present. On ADV7604 the RD_INFO register only
2807 * identifies the revision, while on ADV7611 it identifies the model as
2808 * well. Use the HDMI slave address on ADV7604 and RD_INFO on ADV7611.
2809 */
2810 if (state->info->type == ADV7604) {
2811 val = adv_smbus_read_byte_data_check(client, 0xfb, false);
2812 if (val != 0x68) {
2813 v4l2_info(sd, "not an adv7604 on address 0x%x\n",
2814 client->addr << 1);
2815 return -ENODEV;
2816 }
2817 } else {
2818 val = (adv_smbus_read_byte_data_check(client, 0xea, false) << 8)
2819 | (adv_smbus_read_byte_data_check(client, 0xeb, false) << 0);
2820 if (val != 0x2051) {
2821 v4l2_info(sd, "not an adv7611 on address 0x%x\n",
2822 client->addr << 1);
2823 return -ENODEV;
2824 }
Hans Verkuil54450f52012-07-18 05:45:16 -03002825 }
2826
2827 /* control handlers */
2828 hdl = &state->hdl;
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002829 v4l2_ctrl_handler_init(hdl, adv7604_has_afe(state) ? 9 : 8);
Hans Verkuil54450f52012-07-18 05:45:16 -03002830
2831 v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
2832 V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
2833 v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
2834 V4L2_CID_CONTRAST, 0, 255, 1, 128);
2835 v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
2836 V4L2_CID_SATURATION, 0, 255, 1, 128);
2837 v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
2838 V4L2_CID_HUE, 0, 128, 1, 0);
2839
2840 /* private controls */
2841 state->detect_tx_5v_ctrl = v4l2_ctrl_new_std(hdl, NULL,
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002842 V4L2_CID_DV_RX_POWER_PRESENT, 0,
2843 (1 << state->info->num_dv_ports) - 1, 0, 0);
Hans Verkuil54450f52012-07-18 05:45:16 -03002844 state->rgb_quantization_range_ctrl =
2845 v4l2_ctrl_new_std_menu(hdl, &adv7604_ctrl_ops,
2846 V4L2_CID_DV_RX_RGB_RANGE, V4L2_DV_RGB_RANGE_FULL,
2847 0, V4L2_DV_RGB_RANGE_AUTO);
Hans Verkuil54450f52012-07-18 05:45:16 -03002848
2849 /* custom controls */
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002850 if (adv7604_has_afe(state))
2851 state->analog_sampling_phase_ctrl =
2852 v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_analog_sampling_phase, NULL);
Hans Verkuil54450f52012-07-18 05:45:16 -03002853 state->free_run_color_manual_ctrl =
2854 v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_free_run_color_manual, NULL);
Hans Verkuil54450f52012-07-18 05:45:16 -03002855 state->free_run_color_ctrl =
2856 v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_free_run_color, NULL);
Hans Verkuil54450f52012-07-18 05:45:16 -03002857
2858 sd->ctrl_handler = hdl;
2859 if (hdl->error) {
2860 err = hdl->error;
2861 goto err_hdl;
2862 }
Hans Verkuil8c0eadb2013-08-22 06:11:17 -03002863 state->detect_tx_5v_ctrl->is_private = true;
2864 state->rgb_quantization_range_ctrl->is_private = true;
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002865 if (adv7604_has_afe(state))
2866 state->analog_sampling_phase_ctrl->is_private = true;
Hans Verkuil8c0eadb2013-08-22 06:11:17 -03002867 state->free_run_color_manual_ctrl->is_private = true;
2868 state->free_run_color_ctrl->is_private = true;
2869
Hans Verkuil54450f52012-07-18 05:45:16 -03002870 if (adv7604_s_detect_tx_5v_ctrl(sd)) {
2871 err = -ENODEV;
2872 goto err_hdl;
2873 }
2874
Hans Verkuil54450f52012-07-18 05:45:16 -03002875 state->i2c_cec = adv7604_dummy_client(sd, pdata->i2c_cec, 0xf4);
2876 state->i2c_infoframe = adv7604_dummy_client(sd, pdata->i2c_infoframe, 0xf5);
Hans Verkuil54450f52012-07-18 05:45:16 -03002877 state->i2c_afe = adv7604_dummy_client(sd, pdata->i2c_afe, 0xf8);
2878 state->i2c_repeater = adv7604_dummy_client(sd, pdata->i2c_repeater, 0xf9);
2879 state->i2c_edid = adv7604_dummy_client(sd, pdata->i2c_edid, 0xfa);
2880 state->i2c_hdmi = adv7604_dummy_client(sd, pdata->i2c_hdmi, 0xfb);
Hans Verkuil54450f52012-07-18 05:45:16 -03002881 state->i2c_cp = adv7604_dummy_client(sd, pdata->i2c_cp, 0xfd);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002882 if (!state->i2c_cec || !state->i2c_infoframe || !state->i2c_afe ||
Hans Verkuil54450f52012-07-18 05:45:16 -03002883 !state->i2c_repeater || !state->i2c_edid || !state->i2c_hdmi ||
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002884 !state->i2c_cp) {
Hans Verkuil54450f52012-07-18 05:45:16 -03002885 err = -ENOMEM;
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002886 v4l2_err(sd, "failed to create digital i2c clients\n");
Hans Verkuil54450f52012-07-18 05:45:16 -03002887 goto err_i2c;
2888 }
Hans Verkuil54450f52012-07-18 05:45:16 -03002889
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002890 if (adv7604_has_afe(state)) {
2891 state->i2c_avlink = adv7604_dummy_client(sd, pdata->i2c_avlink, 0xf3);
2892 state->i2c_esdp = adv7604_dummy_client(sd, pdata->i2c_esdp, 0xf6);
2893 state->i2c_dpp = adv7604_dummy_client(sd, pdata->i2c_dpp, 0xf7);
2894 state->i2c_test = adv7604_dummy_client(sd, pdata->i2c_test, 0xfc);
2895 state->i2c_vdp = adv7604_dummy_client(sd, pdata->i2c_vdp, 0xfe);
2896 if (!state->i2c_avlink || !state->i2c_esdp || !state->i2c_dpp ||
2897 !state->i2c_test || !state->i2c_vdp) {
2898 err = -ENOMEM;
2899 v4l2_err(sd, "failed to create analog i2c clients\n");
2900 goto err_i2c;
2901 }
2902 }
Hans Verkuil54450f52012-07-18 05:45:16 -03002903 /* work queues */
2904 state->work_queues = create_singlethread_workqueue(client->name);
2905 if (!state->work_queues) {
2906 v4l2_err(sd, "Could not create work queue\n");
2907 err = -ENOMEM;
2908 goto err_i2c;
2909 }
2910
2911 INIT_DELAYED_WORK(&state->delayed_work_enable_hotplug,
2912 adv7604_delayed_work_enable_hotplug);
2913
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03002914 state->source_pad = state->info->num_dv_ports
2915 + (state->info->has_afe ? 2 : 0);
2916 for (i = 0; i < state->source_pad; ++i)
2917 state->pads[i].flags = MEDIA_PAD_FL_SINK;
2918 state->pads[state->source_pad].flags = MEDIA_PAD_FL_SOURCE;
2919
2920 err = media_entity_init(&sd->entity, state->source_pad + 1,
2921 state->pads, 0);
Hans Verkuil54450f52012-07-18 05:45:16 -03002922 if (err)
2923 goto err_work_queues;
2924
2925 err = adv7604_core_init(sd);
2926 if (err)
2927 goto err_entity;
2928 v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
2929 client->addr << 1, client->adapter->name);
Lars-Peter Clausenbedc3932013-11-25 16:18:02 -03002930
2931 err = v4l2_async_register_subdev(sd);
2932 if (err)
2933 goto err_entity;
2934
Hans Verkuil54450f52012-07-18 05:45:16 -03002935 return 0;
2936
2937err_entity:
2938 media_entity_cleanup(&sd->entity);
2939err_work_queues:
2940 cancel_delayed_work(&state->delayed_work_enable_hotplug);
2941 destroy_workqueue(state->work_queues);
2942err_i2c:
2943 adv7604_unregister_clients(state);
2944err_hdl:
2945 v4l2_ctrl_handler_free(hdl);
Hans Verkuil54450f52012-07-18 05:45:16 -03002946 return err;
2947}
2948
2949/* ----------------------------------------------------------------------- */
2950
2951static int adv7604_remove(struct i2c_client *client)
2952{
2953 struct v4l2_subdev *sd = i2c_get_clientdata(client);
2954 struct adv7604_state *state = to_state(sd);
2955
2956 cancel_delayed_work(&state->delayed_work_enable_hotplug);
2957 destroy_workqueue(state->work_queues);
Lars-Peter Clausenbedc3932013-11-25 16:18:02 -03002958 v4l2_async_unregister_subdev(sd);
Hans Verkuil54450f52012-07-18 05:45:16 -03002959 v4l2_device_unregister_subdev(sd);
2960 media_entity_cleanup(&sd->entity);
2961 adv7604_unregister_clients(to_state(sd));
2962 v4l2_ctrl_handler_free(sd->ctrl_handler);
Hans Verkuil54450f52012-07-18 05:45:16 -03002963 return 0;
2964}
2965
2966/* ----------------------------------------------------------------------- */
2967
2968static struct i2c_device_id adv7604_id[] = {
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002969 { "adv7604", ADV7604 },
2970 { "adv7611", ADV7611 },
Hans Verkuil54450f52012-07-18 05:45:16 -03002971 { }
2972};
2973MODULE_DEVICE_TABLE(i2c, adv7604_id);
2974
2975static struct i2c_driver adv7604_driver = {
2976 .driver = {
2977 .owner = THIS_MODULE,
2978 .name = "adv7604",
2979 },
2980 .probe = adv7604_probe,
2981 .remove = adv7604_remove,
2982 .id_table = adv7604_id,
2983};
2984
2985module_i2c_driver(adv7604_driver);