blob: b14dc7d4b7bb470a0ef9dd6bda12c6e3743dd031 [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
Laurent Pinchartc72a53c2014-01-30 19:18:34 -030030#include <linux/delay.h>
Laurent Pincharte9d50e92014-01-30 18:37:08 -030031#include <linux/gpio/consumer.h>
Laurent Pinchartc72a53c2014-01-30 19:18:34 -030032#include <linux/i2c.h>
Hans Verkuil54450f52012-07-18 05:45:16 -030033#include <linux/kernel.h>
34#include <linux/module.h>
35#include <linux/slab.h>
Laurent Pinchartc72a53c2014-01-30 19:18:34 -030036#include <linux/v4l2-dv-timings.h>
Hans Verkuil54450f52012-07-18 05:45:16 -030037#include <linux/videodev2.h>
38#include <linux/workqueue.h>
Laurent Pinchartc72a53c2014-01-30 19:18:34 -030039
Hans Verkuil54450f52012-07-18 05:45:16 -030040#include <media/adv7604.h>
Laurent Pinchartc72a53c2014-01-30 19:18:34 -030041#include <media/v4l2-ctrls.h>
42#include <media/v4l2-device.h>
43#include <media/v4l2-dv-timings.h>
Hans Verkuil54450f52012-07-18 05:45:16 -030044
45static int debug;
46module_param(debug, int, 0644);
47MODULE_PARM_DESC(debug, "debug level (0-2)");
48
49MODULE_DESCRIPTION("Analog Devices ADV7604 video decoder driver");
50MODULE_AUTHOR("Hans Verkuil <hans.verkuil@cisco.com>");
51MODULE_AUTHOR("Mats Randgaard <mats.randgaard@cisco.com>");
52MODULE_LICENSE("GPL");
53
54/* ADV7604 system clock frequency */
55#define ADV7604_fsc (28636360)
56
Laurent Pinchart539b33b2014-01-26 18:42:37 -030057#define ADV7604_RGB_OUT (1 << 1)
58
59#define ADV7604_OP_FORMAT_SEL_8BIT (0 << 0)
60#define ADV7604_OP_FORMAT_SEL_10BIT (1 << 0)
61#define ADV7604_OP_FORMAT_SEL_12BIT (2 << 0)
62
63#define ADV7604_OP_MODE_SEL_SDR_422 (0 << 5)
64#define ADV7604_OP_MODE_SEL_DDR_422 (1 << 5)
65#define ADV7604_OP_MODE_SEL_SDR_444 (2 << 5)
66#define ADV7604_OP_MODE_SEL_DDR_444 (3 << 5)
67#define ADV7604_OP_MODE_SEL_SDR_422_2X (4 << 5)
68#define ADV7604_OP_MODE_SEL_ADI_CM (5 << 5)
69
70#define ADV7604_OP_CH_SEL_GBR (0 << 5)
71#define ADV7604_OP_CH_SEL_GRB (1 << 5)
72#define ADV7604_OP_CH_SEL_BGR (2 << 5)
73#define ADV7604_OP_CH_SEL_RGB (3 << 5)
74#define ADV7604_OP_CH_SEL_BRG (4 << 5)
75#define ADV7604_OP_CH_SEL_RBG (5 << 5)
76
77#define ADV7604_OP_SWAP_CB_CR (1 << 0)
78
Lars-Peter Clausend42010a2013-11-25 15:45:07 -030079enum adv7604_type {
80 ADV7604,
81 ADV7611,
82};
83
84struct adv7604_reg_seq {
85 unsigned int reg;
86 u8 val;
87};
88
Laurent Pinchart539b33b2014-01-26 18:42:37 -030089struct adv7604_format_info {
90 enum v4l2_mbus_pixelcode code;
91 u8 op_ch_sel;
92 bool rgb_out;
93 bool swap_cb_cr;
94 u8 op_format_sel;
95};
96
Lars-Peter Clausend42010a2013-11-25 15:45:07 -030097struct adv7604_chip_info {
98 enum adv7604_type type;
99
100 bool has_afe;
101 unsigned int max_port;
102 unsigned int num_dv_ports;
103
104 unsigned int edid_enable_reg;
105 unsigned int edid_status_reg;
106 unsigned int lcf_reg;
107
108 unsigned int cable_det_mask;
109 unsigned int tdms_lock_mask;
110 unsigned int fmt_change_digital_mask;
111
Laurent Pinchart539b33b2014-01-26 18:42:37 -0300112 const struct adv7604_format_info *formats;
113 unsigned int nformats;
114
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300115 void (*set_termination)(struct v4l2_subdev *sd, bool enable);
116 void (*setup_irqs)(struct v4l2_subdev *sd);
117 unsigned int (*read_hdmi_pixelclock)(struct v4l2_subdev *sd);
118 unsigned int (*read_cable_det)(struct v4l2_subdev *sd);
119
120 /* 0 = AFE, 1 = HDMI */
121 const struct adv7604_reg_seq *recommended_settings[2];
122 unsigned int num_recommended_settings[2];
123
124 unsigned long page_mask;
125};
126
Hans Verkuil54450f52012-07-18 05:45:16 -0300127/*
128 **********************************************************************
129 *
130 * Arrays with configuration parameters for the ADV7604
131 *
132 **********************************************************************
133 */
Laurent Pinchartc784b1e2014-01-29 10:08:58 -0300134
Hans Verkuil54450f52012-07-18 05:45:16 -0300135struct adv7604_state {
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300136 const struct adv7604_chip_info *info;
Hans Verkuil54450f52012-07-18 05:45:16 -0300137 struct adv7604_platform_data pdata;
Laurent Pinchart539b33b2014-01-26 18:42:37 -0300138
Laurent Pincharte9d50e92014-01-30 18:37:08 -0300139 struct gpio_desc *hpd_gpio[4];
140
Hans Verkuil54450f52012-07-18 05:45:16 -0300141 struct v4l2_subdev sd;
Laurent Pinchartc784b1e2014-01-29 10:08:58 -0300142 struct media_pad pads[ADV7604_PAD_MAX];
143 unsigned int source_pad;
Laurent Pinchart539b33b2014-01-26 18:42:37 -0300144
Hans Verkuil54450f52012-07-18 05:45:16 -0300145 struct v4l2_ctrl_handler hdl;
Laurent Pinchart539b33b2014-01-26 18:42:37 -0300146
Laurent Pinchartc784b1e2014-01-29 10:08:58 -0300147 enum adv7604_pad selected_input;
Laurent Pinchart539b33b2014-01-26 18:42:37 -0300148
Hans Verkuil54450f52012-07-18 05:45:16 -0300149 struct v4l2_dv_timings timings;
Laurent Pinchart539b33b2014-01-26 18:42:37 -0300150 const struct adv7604_format_info *format;
151
Mats Randgaard4a31a932013-12-10 09:45:00 -0300152 struct {
153 u8 edid[256];
154 u32 present;
155 unsigned blocks;
156 } edid;
Mats Randgaarddd08beb2013-12-10 09:57:09 -0300157 u16 spa_port_a[2];
Hans Verkuil54450f52012-07-18 05:45:16 -0300158 struct v4l2_fract aspect_ratio;
159 u32 rgb_quantization_range;
160 struct workqueue_struct *work_queues;
161 struct delayed_work delayed_work_enable_hotplug;
Hans Verkuilcf9afb12012-10-16 10:12:55 -0300162 bool restart_stdi_once;
Hans Verkuil54450f52012-07-18 05:45:16 -0300163
164 /* i2c clients */
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300165 struct i2c_client *i2c_clients[ADV7604_PAGE_MAX];
Hans Verkuil54450f52012-07-18 05:45:16 -0300166
167 /* controls */
168 struct v4l2_ctrl *detect_tx_5v_ctrl;
169 struct v4l2_ctrl *analog_sampling_phase_ctrl;
170 struct v4l2_ctrl *free_run_color_manual_ctrl;
171 struct v4l2_ctrl *free_run_color_ctrl;
172 struct v4l2_ctrl *rgb_quantization_range_ctrl;
173};
174
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300175static bool adv7604_has_afe(struct adv7604_state *state)
176{
177 return state->info->has_afe;
178}
179
Hans Verkuil54450f52012-07-18 05:45:16 -0300180/* Supported CEA and DMT timings */
181static const struct v4l2_dv_timings adv7604_timings[] = {
182 V4L2_DV_BT_CEA_720X480P59_94,
183 V4L2_DV_BT_CEA_720X576P50,
184 V4L2_DV_BT_CEA_1280X720P24,
185 V4L2_DV_BT_CEA_1280X720P25,
Hans Verkuil54450f52012-07-18 05:45:16 -0300186 V4L2_DV_BT_CEA_1280X720P50,
187 V4L2_DV_BT_CEA_1280X720P60,
188 V4L2_DV_BT_CEA_1920X1080P24,
189 V4L2_DV_BT_CEA_1920X1080P25,
190 V4L2_DV_BT_CEA_1920X1080P30,
191 V4L2_DV_BT_CEA_1920X1080P50,
192 V4L2_DV_BT_CEA_1920X1080P60,
193
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300194 /* sorted by DMT ID */
Hans Verkuil54450f52012-07-18 05:45:16 -0300195 V4L2_DV_BT_DMT_640X350P85,
196 V4L2_DV_BT_DMT_640X400P85,
197 V4L2_DV_BT_DMT_720X400P85,
198 V4L2_DV_BT_DMT_640X480P60,
199 V4L2_DV_BT_DMT_640X480P72,
200 V4L2_DV_BT_DMT_640X480P75,
201 V4L2_DV_BT_DMT_640X480P85,
202 V4L2_DV_BT_DMT_800X600P56,
203 V4L2_DV_BT_DMT_800X600P60,
204 V4L2_DV_BT_DMT_800X600P72,
205 V4L2_DV_BT_DMT_800X600P75,
206 V4L2_DV_BT_DMT_800X600P85,
207 V4L2_DV_BT_DMT_848X480P60,
208 V4L2_DV_BT_DMT_1024X768P60,
209 V4L2_DV_BT_DMT_1024X768P70,
210 V4L2_DV_BT_DMT_1024X768P75,
211 V4L2_DV_BT_DMT_1024X768P85,
212 V4L2_DV_BT_DMT_1152X864P75,
213 V4L2_DV_BT_DMT_1280X768P60_RB,
214 V4L2_DV_BT_DMT_1280X768P60,
215 V4L2_DV_BT_DMT_1280X768P75,
216 V4L2_DV_BT_DMT_1280X768P85,
217 V4L2_DV_BT_DMT_1280X800P60_RB,
218 V4L2_DV_BT_DMT_1280X800P60,
219 V4L2_DV_BT_DMT_1280X800P75,
220 V4L2_DV_BT_DMT_1280X800P85,
221 V4L2_DV_BT_DMT_1280X960P60,
222 V4L2_DV_BT_DMT_1280X960P85,
223 V4L2_DV_BT_DMT_1280X1024P60,
224 V4L2_DV_BT_DMT_1280X1024P75,
225 V4L2_DV_BT_DMT_1280X1024P85,
226 V4L2_DV_BT_DMT_1360X768P60,
227 V4L2_DV_BT_DMT_1400X1050P60_RB,
228 V4L2_DV_BT_DMT_1400X1050P60,
229 V4L2_DV_BT_DMT_1400X1050P75,
230 V4L2_DV_BT_DMT_1400X1050P85,
231 V4L2_DV_BT_DMT_1440X900P60_RB,
232 V4L2_DV_BT_DMT_1440X900P60,
233 V4L2_DV_BT_DMT_1600X1200P60,
234 V4L2_DV_BT_DMT_1680X1050P60_RB,
235 V4L2_DV_BT_DMT_1680X1050P60,
236 V4L2_DV_BT_DMT_1792X1344P60,
237 V4L2_DV_BT_DMT_1856X1392P60,
238 V4L2_DV_BT_DMT_1920X1200P60_RB,
Martin Bugge547ed542013-12-05 10:01:17 -0300239 V4L2_DV_BT_DMT_1366X768P60_RB,
Hans Verkuil54450f52012-07-18 05:45:16 -0300240 V4L2_DV_BT_DMT_1366X768P60,
241 V4L2_DV_BT_DMT_1920X1080P60,
242 { },
243};
244
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300245struct adv7604_video_standards {
246 struct v4l2_dv_timings timings;
247 u8 vid_std;
248 u8 v_freq;
249};
250
251/* sorted by number of lines */
252static const struct adv7604_video_standards adv7604_prim_mode_comp[] = {
253 /* { V4L2_DV_BT_CEA_720X480P59_94, 0x0a, 0x00 }, TODO flickering */
254 { V4L2_DV_BT_CEA_720X576P50, 0x0b, 0x00 },
255 { V4L2_DV_BT_CEA_1280X720P50, 0x19, 0x01 },
256 { V4L2_DV_BT_CEA_1280X720P60, 0x19, 0x00 },
257 { V4L2_DV_BT_CEA_1920X1080P24, 0x1e, 0x04 },
258 { V4L2_DV_BT_CEA_1920X1080P25, 0x1e, 0x03 },
259 { V4L2_DV_BT_CEA_1920X1080P30, 0x1e, 0x02 },
260 { V4L2_DV_BT_CEA_1920X1080P50, 0x1e, 0x01 },
261 { V4L2_DV_BT_CEA_1920X1080P60, 0x1e, 0x00 },
262 /* TODO add 1920x1080P60_RB (CVT timing) */
263 { },
264};
265
266/* sorted by number of lines */
267static const struct adv7604_video_standards adv7604_prim_mode_gr[] = {
268 { V4L2_DV_BT_DMT_640X480P60, 0x08, 0x00 },
269 { V4L2_DV_BT_DMT_640X480P72, 0x09, 0x00 },
270 { V4L2_DV_BT_DMT_640X480P75, 0x0a, 0x00 },
271 { V4L2_DV_BT_DMT_640X480P85, 0x0b, 0x00 },
272 { V4L2_DV_BT_DMT_800X600P56, 0x00, 0x00 },
273 { V4L2_DV_BT_DMT_800X600P60, 0x01, 0x00 },
274 { V4L2_DV_BT_DMT_800X600P72, 0x02, 0x00 },
275 { V4L2_DV_BT_DMT_800X600P75, 0x03, 0x00 },
276 { V4L2_DV_BT_DMT_800X600P85, 0x04, 0x00 },
277 { V4L2_DV_BT_DMT_1024X768P60, 0x0c, 0x00 },
278 { V4L2_DV_BT_DMT_1024X768P70, 0x0d, 0x00 },
279 { V4L2_DV_BT_DMT_1024X768P75, 0x0e, 0x00 },
280 { V4L2_DV_BT_DMT_1024X768P85, 0x0f, 0x00 },
281 { V4L2_DV_BT_DMT_1280X1024P60, 0x05, 0x00 },
282 { V4L2_DV_BT_DMT_1280X1024P75, 0x06, 0x00 },
283 { V4L2_DV_BT_DMT_1360X768P60, 0x12, 0x00 },
284 { V4L2_DV_BT_DMT_1366X768P60, 0x13, 0x00 },
285 { V4L2_DV_BT_DMT_1400X1050P60, 0x14, 0x00 },
286 { V4L2_DV_BT_DMT_1400X1050P75, 0x15, 0x00 },
287 { V4L2_DV_BT_DMT_1600X1200P60, 0x16, 0x00 }, /* TODO not tested */
288 /* TODO add 1600X1200P60_RB (not a DMT timing) */
289 { V4L2_DV_BT_DMT_1680X1050P60, 0x18, 0x00 },
290 { V4L2_DV_BT_DMT_1920X1200P60_RB, 0x19, 0x00 }, /* TODO not tested */
291 { },
292};
293
294/* sorted by number of lines */
295static const struct adv7604_video_standards adv7604_prim_mode_hdmi_comp[] = {
296 { V4L2_DV_BT_CEA_720X480P59_94, 0x0a, 0x00 },
297 { V4L2_DV_BT_CEA_720X576P50, 0x0b, 0x00 },
298 { V4L2_DV_BT_CEA_1280X720P50, 0x13, 0x01 },
299 { V4L2_DV_BT_CEA_1280X720P60, 0x13, 0x00 },
300 { V4L2_DV_BT_CEA_1920X1080P24, 0x1e, 0x04 },
301 { V4L2_DV_BT_CEA_1920X1080P25, 0x1e, 0x03 },
302 { V4L2_DV_BT_CEA_1920X1080P30, 0x1e, 0x02 },
303 { V4L2_DV_BT_CEA_1920X1080P50, 0x1e, 0x01 },
304 { V4L2_DV_BT_CEA_1920X1080P60, 0x1e, 0x00 },
305 { },
306};
307
308/* sorted by number of lines */
309static const struct adv7604_video_standards adv7604_prim_mode_hdmi_gr[] = {
310 { V4L2_DV_BT_DMT_640X480P60, 0x08, 0x00 },
311 { V4L2_DV_BT_DMT_640X480P72, 0x09, 0x00 },
312 { V4L2_DV_BT_DMT_640X480P75, 0x0a, 0x00 },
313 { V4L2_DV_BT_DMT_640X480P85, 0x0b, 0x00 },
314 { V4L2_DV_BT_DMT_800X600P56, 0x00, 0x00 },
315 { V4L2_DV_BT_DMT_800X600P60, 0x01, 0x00 },
316 { V4L2_DV_BT_DMT_800X600P72, 0x02, 0x00 },
317 { V4L2_DV_BT_DMT_800X600P75, 0x03, 0x00 },
318 { V4L2_DV_BT_DMT_800X600P85, 0x04, 0x00 },
319 { V4L2_DV_BT_DMT_1024X768P60, 0x0c, 0x00 },
320 { V4L2_DV_BT_DMT_1024X768P70, 0x0d, 0x00 },
321 { V4L2_DV_BT_DMT_1024X768P75, 0x0e, 0x00 },
322 { V4L2_DV_BT_DMT_1024X768P85, 0x0f, 0x00 },
323 { V4L2_DV_BT_DMT_1280X1024P60, 0x05, 0x00 },
324 { V4L2_DV_BT_DMT_1280X1024P75, 0x06, 0x00 },
325 { },
326};
327
Hans Verkuil54450f52012-07-18 05:45:16 -0300328/* ----------------------------------------------------------------------- */
329
330static inline struct adv7604_state *to_state(struct v4l2_subdev *sd)
331{
332 return container_of(sd, struct adv7604_state, sd);
333}
334
Hans Verkuil54450f52012-07-18 05:45:16 -0300335static inline unsigned hblanking(const struct v4l2_bt_timings *t)
336{
Hans Verkuileacf8f92013-07-29 08:40:59 -0300337 return V4L2_DV_BT_BLANKING_WIDTH(t);
Hans Verkuil54450f52012-07-18 05:45:16 -0300338}
339
340static inline unsigned htotal(const struct v4l2_bt_timings *t)
341{
Hans Verkuileacf8f92013-07-29 08:40:59 -0300342 return V4L2_DV_BT_FRAME_WIDTH(t);
Hans Verkuil54450f52012-07-18 05:45:16 -0300343}
344
345static inline unsigned vblanking(const struct v4l2_bt_timings *t)
346{
Hans Verkuileacf8f92013-07-29 08:40:59 -0300347 return V4L2_DV_BT_BLANKING_HEIGHT(t);
Hans Verkuil54450f52012-07-18 05:45:16 -0300348}
349
350static inline unsigned vtotal(const struct v4l2_bt_timings *t)
351{
Hans Verkuileacf8f92013-07-29 08:40:59 -0300352 return V4L2_DV_BT_FRAME_HEIGHT(t);
Hans Verkuil54450f52012-07-18 05:45:16 -0300353}
354
355/* ----------------------------------------------------------------------- */
356
357static s32 adv_smbus_read_byte_data_check(struct i2c_client *client,
358 u8 command, bool check)
359{
360 union i2c_smbus_data data;
361
362 if (!i2c_smbus_xfer(client->adapter, client->addr, client->flags,
363 I2C_SMBUS_READ, command,
364 I2C_SMBUS_BYTE_DATA, &data))
365 return data.byte;
366 if (check)
367 v4l_err(client, "error reading %02x, %02x\n",
368 client->addr, command);
369 return -EIO;
370}
371
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300372static s32 adv_smbus_read_byte_data(struct adv7604_state *state,
373 enum adv7604_page page, u8 command)
Hans Verkuil54450f52012-07-18 05:45:16 -0300374{
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300375 return adv_smbus_read_byte_data_check(state->i2c_clients[page],
376 command, true);
Hans Verkuil54450f52012-07-18 05:45:16 -0300377}
378
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300379static s32 adv_smbus_write_byte_data(struct adv7604_state *state,
380 enum adv7604_page page, u8 command,
381 u8 value)
Hans Verkuil54450f52012-07-18 05:45:16 -0300382{
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300383 struct i2c_client *client = state->i2c_clients[page];
Hans Verkuil54450f52012-07-18 05:45:16 -0300384 union i2c_smbus_data data;
385 int err;
386 int i;
387
388 data.byte = value;
389 for (i = 0; i < 3; i++) {
390 err = i2c_smbus_xfer(client->adapter, client->addr,
391 client->flags,
392 I2C_SMBUS_WRITE, command,
393 I2C_SMBUS_BYTE_DATA, &data);
394 if (!err)
395 break;
396 }
397 if (err < 0)
398 v4l_err(client, "error writing %02x, %02x, %02x\n",
399 client->addr, command, value);
400 return err;
401}
402
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300403static s32 adv_smbus_write_i2c_block_data(struct adv7604_state *state,
404 enum adv7604_page page, u8 command,
405 unsigned length, const u8 *values)
Hans Verkuil54450f52012-07-18 05:45:16 -0300406{
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300407 struct i2c_client *client = state->i2c_clients[page];
Hans Verkuil54450f52012-07-18 05:45:16 -0300408 union i2c_smbus_data data;
409
410 if (length > I2C_SMBUS_BLOCK_MAX)
411 length = I2C_SMBUS_BLOCK_MAX;
412 data.block[0] = length;
413 memcpy(data.block + 1, values, length);
414 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
415 I2C_SMBUS_WRITE, command,
416 I2C_SMBUS_I2C_BLOCK_DATA, &data);
417}
418
419/* ----------------------------------------------------------------------- */
420
421static inline int io_read(struct v4l2_subdev *sd, u8 reg)
422{
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300423 struct adv7604_state *state = to_state(sd);
Hans Verkuil54450f52012-07-18 05:45:16 -0300424
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300425 return adv_smbus_read_byte_data(state, ADV7604_PAGE_IO, reg);
Hans Verkuil54450f52012-07-18 05:45:16 -0300426}
427
428static inline int io_write(struct v4l2_subdev *sd, u8 reg, u8 val)
429{
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300430 struct adv7604_state *state = to_state(sd);
Hans Verkuil54450f52012-07-18 05:45:16 -0300431
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300432 return adv_smbus_write_byte_data(state, ADV7604_PAGE_IO, reg, val);
Hans Verkuil54450f52012-07-18 05:45:16 -0300433}
434
Laurent Pinchart22d97e52014-01-30 17:17:42 -0300435static inline int io_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
Hans Verkuil54450f52012-07-18 05:45:16 -0300436{
Laurent Pinchart22d97e52014-01-30 17:17:42 -0300437 return io_write(sd, reg, (io_read(sd, reg) & ~mask) | val);
Hans Verkuil54450f52012-07-18 05:45:16 -0300438}
439
440static inline int avlink_read(struct v4l2_subdev *sd, u8 reg)
441{
442 struct adv7604_state *state = to_state(sd);
443
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300444 return adv_smbus_read_byte_data(state, ADV7604_PAGE_AVLINK, reg);
Hans Verkuil54450f52012-07-18 05:45:16 -0300445}
446
447static inline int avlink_write(struct v4l2_subdev *sd, u8 reg, u8 val)
448{
449 struct adv7604_state *state = to_state(sd);
450
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300451 return adv_smbus_write_byte_data(state, ADV7604_PAGE_AVLINK, reg, val);
Hans Verkuil54450f52012-07-18 05:45:16 -0300452}
453
454static inline int cec_read(struct v4l2_subdev *sd, u8 reg)
455{
456 struct adv7604_state *state = to_state(sd);
457
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300458 return adv_smbus_read_byte_data(state, ADV7604_PAGE_CEC, reg);
Hans Verkuil54450f52012-07-18 05:45:16 -0300459}
460
461static inline int cec_write(struct v4l2_subdev *sd, u8 reg, u8 val)
462{
463 struct adv7604_state *state = to_state(sd);
464
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300465 return adv_smbus_write_byte_data(state, ADV7604_PAGE_CEC, reg, val);
Hans Verkuil54450f52012-07-18 05:45:16 -0300466}
467
Laurent Pinchart22d97e52014-01-30 17:17:42 -0300468static inline int cec_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
Hans Verkuil54450f52012-07-18 05:45:16 -0300469{
Laurent Pinchart22d97e52014-01-30 17:17:42 -0300470 return cec_write(sd, reg, (cec_read(sd, reg) & ~mask) | val);
Hans Verkuil54450f52012-07-18 05:45:16 -0300471}
472
473static inline int infoframe_read(struct v4l2_subdev *sd, u8 reg)
474{
475 struct adv7604_state *state = to_state(sd);
476
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300477 return adv_smbus_read_byte_data(state, ADV7604_PAGE_INFOFRAME, reg);
Hans Verkuil54450f52012-07-18 05:45:16 -0300478}
479
480static inline int infoframe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
481{
482 struct adv7604_state *state = to_state(sd);
483
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300484 return adv_smbus_write_byte_data(state, ADV7604_PAGE_INFOFRAME,
485 reg, val);
Hans Verkuil54450f52012-07-18 05:45:16 -0300486}
487
488static inline int esdp_read(struct v4l2_subdev *sd, u8 reg)
489{
490 struct adv7604_state *state = to_state(sd);
491
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300492 return adv_smbus_read_byte_data(state, ADV7604_PAGE_ESDP, reg);
Hans Verkuil54450f52012-07-18 05:45:16 -0300493}
494
495static inline int esdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
496{
497 struct adv7604_state *state = to_state(sd);
498
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300499 return adv_smbus_write_byte_data(state, ADV7604_PAGE_ESDP, reg, val);
Hans Verkuil54450f52012-07-18 05:45:16 -0300500}
501
502static inline int dpp_read(struct v4l2_subdev *sd, u8 reg)
503{
504 struct adv7604_state *state = to_state(sd);
505
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300506 return adv_smbus_read_byte_data(state, ADV7604_PAGE_DPP, reg);
Hans Verkuil54450f52012-07-18 05:45:16 -0300507}
508
509static inline int dpp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
510{
511 struct adv7604_state *state = to_state(sd);
512
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300513 return adv_smbus_write_byte_data(state, ADV7604_PAGE_DPP, reg, val);
Hans Verkuil54450f52012-07-18 05:45:16 -0300514}
515
516static inline int afe_read(struct v4l2_subdev *sd, u8 reg)
517{
518 struct adv7604_state *state = to_state(sd);
519
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300520 return adv_smbus_read_byte_data(state, ADV7604_PAGE_AFE, reg);
Hans Verkuil54450f52012-07-18 05:45:16 -0300521}
522
523static inline int afe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
524{
525 struct adv7604_state *state = to_state(sd);
526
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300527 return adv_smbus_write_byte_data(state, ADV7604_PAGE_AFE, reg, val);
Hans Verkuil54450f52012-07-18 05:45:16 -0300528}
529
530static inline int rep_read(struct v4l2_subdev *sd, u8 reg)
531{
532 struct adv7604_state *state = to_state(sd);
533
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300534 return adv_smbus_read_byte_data(state, ADV7604_PAGE_REP, reg);
Hans Verkuil54450f52012-07-18 05:45:16 -0300535}
536
537static inline int rep_write(struct v4l2_subdev *sd, u8 reg, u8 val)
538{
539 struct adv7604_state *state = to_state(sd);
540
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300541 return adv_smbus_write_byte_data(state, ADV7604_PAGE_REP, reg, val);
Hans Verkuil54450f52012-07-18 05:45:16 -0300542}
543
Laurent Pinchart22d97e52014-01-30 17:17:42 -0300544static inline int rep_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
Hans Verkuil54450f52012-07-18 05:45:16 -0300545{
Laurent Pinchart22d97e52014-01-30 17:17:42 -0300546 return rep_write(sd, reg, (rep_read(sd, reg) & ~mask) | val);
Hans Verkuil54450f52012-07-18 05:45:16 -0300547}
548
549static inline int edid_read(struct v4l2_subdev *sd, u8 reg)
550{
551 struct adv7604_state *state = to_state(sd);
552
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300553 return adv_smbus_read_byte_data(state, ADV7604_PAGE_EDID, reg);
Hans Verkuil54450f52012-07-18 05:45:16 -0300554}
555
556static inline int edid_write(struct v4l2_subdev *sd, u8 reg, u8 val)
557{
558 struct adv7604_state *state = to_state(sd);
559
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300560 return adv_smbus_write_byte_data(state, ADV7604_PAGE_EDID, reg, val);
Hans Verkuil54450f52012-07-18 05:45:16 -0300561}
562
563static inline int edid_read_block(struct v4l2_subdev *sd, unsigned len, u8 *val)
564{
565 struct adv7604_state *state = to_state(sd);
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300566 struct i2c_client *client = state->i2c_clients[ADV7604_PAGE_EDID];
Hans Verkuil54450f52012-07-18 05:45:16 -0300567 u8 msgbuf0[1] = { 0 };
568 u8 msgbuf1[256];
Shubhrajyoti D09f29672012-10-25 01:02:36 -0300569 struct i2c_msg msg[2] = {
570 {
571 .addr = client->addr,
572 .len = 1,
573 .buf = msgbuf0
574 },
575 {
576 .addr = client->addr,
577 .flags = I2C_M_RD,
578 .len = len,
579 .buf = msgbuf1
580 },
581 };
Hans Verkuil54450f52012-07-18 05:45:16 -0300582
583 if (i2c_transfer(client->adapter, msg, 2) < 0)
584 return -EIO;
585 memcpy(val, msgbuf1, len);
586 return 0;
587}
588
Mats Randgaarddd08beb2013-12-10 09:57:09 -0300589static inline int edid_write_block(struct v4l2_subdev *sd,
590 unsigned len, const u8 *val)
591{
592 struct adv7604_state *state = to_state(sd);
593 int err = 0;
594 int i;
595
596 v4l2_dbg(2, debug, sd, "%s: write EDID block (%d byte)\n", __func__, len);
597
598 for (i = 0; !err && i < len; i += I2C_SMBUS_BLOCK_MAX)
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300599 err = adv_smbus_write_i2c_block_data(state, ADV7604_PAGE_EDID,
600 i, I2C_SMBUS_BLOCK_MAX, val + i);
Mats Randgaarddd08beb2013-12-10 09:57:09 -0300601 return err;
602}
603
Laurent Pincharte9d50e92014-01-30 18:37:08 -0300604static void adv7604_set_hpd(struct adv7604_state *state, unsigned int hpd)
605{
606 unsigned int i;
607
608 for (i = 0; i < state->info->num_dv_ports; ++i) {
609 if (IS_ERR(state->hpd_gpio[i]))
610 continue;
611
612 gpiod_set_value_cansleep(state->hpd_gpio[i], hpd & BIT(i));
613 }
614
615 v4l2_subdev_notify(&state->sd, ADV7604_HOTPLUG, &hpd);
616}
617
Hans Verkuil54450f52012-07-18 05:45:16 -0300618static void adv7604_delayed_work_enable_hotplug(struct work_struct *work)
619{
620 struct delayed_work *dwork = to_delayed_work(work);
621 struct adv7604_state *state = container_of(dwork, struct adv7604_state,
622 delayed_work_enable_hotplug);
623 struct v4l2_subdev *sd = &state->sd;
624
625 v4l2_dbg(2, debug, sd, "%s: enable hotplug\n", __func__);
626
Laurent Pincharte9d50e92014-01-30 18:37:08 -0300627 adv7604_set_hpd(state, state->edid.present);
Hans Verkuil54450f52012-07-18 05:45:16 -0300628}
629
Hans Verkuil54450f52012-07-18 05:45:16 -0300630static inline int hdmi_read(struct v4l2_subdev *sd, u8 reg)
631{
632 struct adv7604_state *state = to_state(sd);
633
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300634 return adv_smbus_read_byte_data(state, ADV7604_PAGE_HDMI, reg);
Hans Verkuil54450f52012-07-18 05:45:16 -0300635}
636
Laurent Pinchart51182a92014-01-08 19:30:37 -0300637static u16 hdmi_read16(struct v4l2_subdev *sd, u8 reg, u16 mask)
638{
639 return ((hdmi_read(sd, reg) << 8) | hdmi_read(sd, reg + 1)) & mask;
640}
641
Hans Verkuil54450f52012-07-18 05:45:16 -0300642static inline int hdmi_write(struct v4l2_subdev *sd, u8 reg, u8 val)
643{
644 struct adv7604_state *state = to_state(sd);
645
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300646 return adv_smbus_write_byte_data(state, ADV7604_PAGE_HDMI, reg, val);
Hans Verkuil54450f52012-07-18 05:45:16 -0300647}
648
Laurent Pinchart22d97e52014-01-30 17:17:42 -0300649static inline int hdmi_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
Mats Randgaard4a31a932013-12-10 09:45:00 -0300650{
Laurent Pinchart22d97e52014-01-30 17:17:42 -0300651 return hdmi_write(sd, reg, (hdmi_read(sd, reg) & ~mask) | val);
Mats Randgaard4a31a932013-12-10 09:45:00 -0300652}
653
Hans Verkuil54450f52012-07-18 05:45:16 -0300654static inline int test_read(struct v4l2_subdev *sd, u8 reg)
655{
656 struct adv7604_state *state = to_state(sd);
657
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300658 return adv_smbus_read_byte_data(state, ADV7604_PAGE_TEST, reg);
Hans Verkuil54450f52012-07-18 05:45:16 -0300659}
660
661static inline int test_write(struct v4l2_subdev *sd, u8 reg, u8 val)
662{
663 struct adv7604_state *state = to_state(sd);
664
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300665 return adv_smbus_write_byte_data(state, ADV7604_PAGE_TEST, reg, val);
Hans Verkuil54450f52012-07-18 05:45:16 -0300666}
667
668static inline int cp_read(struct v4l2_subdev *sd, u8 reg)
669{
670 struct adv7604_state *state = to_state(sd);
671
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300672 return adv_smbus_read_byte_data(state, ADV7604_PAGE_CP, reg);
Hans Verkuil54450f52012-07-18 05:45:16 -0300673}
674
Laurent Pinchart51182a92014-01-08 19:30:37 -0300675static u16 cp_read16(struct v4l2_subdev *sd, u8 reg, u16 mask)
676{
677 return ((cp_read(sd, reg) << 8) | cp_read(sd, reg + 1)) & mask;
678}
679
Hans Verkuil54450f52012-07-18 05:45:16 -0300680static inline int cp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
681{
682 struct adv7604_state *state = to_state(sd);
683
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300684 return adv_smbus_write_byte_data(state, ADV7604_PAGE_CP, reg, val);
Hans Verkuil54450f52012-07-18 05:45:16 -0300685}
686
Laurent Pinchart22d97e52014-01-30 17:17:42 -0300687static inline int cp_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
Hans Verkuil54450f52012-07-18 05:45:16 -0300688{
Laurent Pinchart22d97e52014-01-30 17:17:42 -0300689 return cp_write(sd, reg, (cp_read(sd, reg) & ~mask) | val);
Hans Verkuil54450f52012-07-18 05:45:16 -0300690}
691
692static inline int vdp_read(struct v4l2_subdev *sd, u8 reg)
693{
694 struct adv7604_state *state = to_state(sd);
695
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300696 return adv_smbus_read_byte_data(state, ADV7604_PAGE_VDP, reg);
Hans Verkuil54450f52012-07-18 05:45:16 -0300697}
698
699static inline int vdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
700{
701 struct adv7604_state *state = to_state(sd);
702
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300703 return adv_smbus_write_byte_data(state, ADV7604_PAGE_VDP, reg, val);
Hans Verkuil54450f52012-07-18 05:45:16 -0300704}
705
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300706#define ADV7604_REG(page, offset) (((page) << 8) | (offset))
707#define ADV7604_REG_SEQ_TERM 0xffff
708
709#ifdef CONFIG_VIDEO_ADV_DEBUG
710static int adv7604_read_reg(struct v4l2_subdev *sd, unsigned int reg)
711{
712 struct adv7604_state *state = to_state(sd);
713 unsigned int page = reg >> 8;
714
715 if (!(BIT(page) & state->info->page_mask))
716 return -EINVAL;
717
718 reg &= 0xff;
719
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300720 return adv_smbus_read_byte_data(state, page, reg);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300721}
722#endif
723
724static int adv7604_write_reg(struct v4l2_subdev *sd, unsigned int reg, u8 val)
725{
726 struct adv7604_state *state = to_state(sd);
727 unsigned int page = reg >> 8;
728
729 if (!(BIT(page) & state->info->page_mask))
730 return -EINVAL;
731
732 reg &= 0xff;
733
Laurent Pinchart05cacb12014-01-30 16:32:21 -0300734 return adv_smbus_write_byte_data(state, page, reg, val);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300735}
736
737static void adv7604_write_reg_seq(struct v4l2_subdev *sd,
738 const struct adv7604_reg_seq *reg_seq)
739{
740 unsigned int i;
741
742 for (i = 0; reg_seq[i].reg != ADV7604_REG_SEQ_TERM; i++)
743 adv7604_write_reg(sd, reg_seq[i].reg, reg_seq[i].val);
744}
745
Laurent Pinchart539b33b2014-01-26 18:42:37 -0300746/* -----------------------------------------------------------------------------
747 * Format helpers
748 */
749
750static const struct adv7604_format_info adv7604_formats[] = {
751 { V4L2_MBUS_FMT_RGB888_1X24, ADV7604_OP_CH_SEL_RGB, true, false,
752 ADV7604_OP_MODE_SEL_SDR_444 | ADV7604_OP_FORMAT_SEL_8BIT },
753 { V4L2_MBUS_FMT_YUYV8_2X8, ADV7604_OP_CH_SEL_RGB, false, false,
754 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_8BIT },
755 { V4L2_MBUS_FMT_YVYU8_2X8, ADV7604_OP_CH_SEL_RGB, false, true,
756 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_8BIT },
757 { V4L2_MBUS_FMT_YUYV10_2X10, ADV7604_OP_CH_SEL_RGB, false, false,
758 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_10BIT },
759 { V4L2_MBUS_FMT_YVYU10_2X10, ADV7604_OP_CH_SEL_RGB, false, true,
760 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_10BIT },
761 { V4L2_MBUS_FMT_YUYV12_2X12, ADV7604_OP_CH_SEL_RGB, false, false,
762 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_12BIT },
763 { V4L2_MBUS_FMT_YVYU12_2X12, ADV7604_OP_CH_SEL_RGB, false, true,
764 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_12BIT },
765 { V4L2_MBUS_FMT_UYVY8_1X16, ADV7604_OP_CH_SEL_RBG, false, false,
766 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
767 { V4L2_MBUS_FMT_VYUY8_1X16, ADV7604_OP_CH_SEL_RBG, false, true,
768 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
769 { V4L2_MBUS_FMT_YUYV8_1X16, ADV7604_OP_CH_SEL_RGB, false, false,
770 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
771 { V4L2_MBUS_FMT_YVYU8_1X16, ADV7604_OP_CH_SEL_RGB, false, true,
772 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
773 { V4L2_MBUS_FMT_UYVY10_1X20, ADV7604_OP_CH_SEL_RBG, false, false,
774 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_10BIT },
775 { V4L2_MBUS_FMT_VYUY10_1X20, ADV7604_OP_CH_SEL_RBG, false, true,
776 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_10BIT },
777 { V4L2_MBUS_FMT_YUYV10_1X20, ADV7604_OP_CH_SEL_RGB, false, false,
778 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_10BIT },
779 { V4L2_MBUS_FMT_YVYU10_1X20, ADV7604_OP_CH_SEL_RGB, false, true,
780 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_10BIT },
781 { V4L2_MBUS_FMT_UYVY12_1X24, ADV7604_OP_CH_SEL_RBG, false, false,
782 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
783 { V4L2_MBUS_FMT_VYUY12_1X24, ADV7604_OP_CH_SEL_RBG, false, true,
784 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
785 { V4L2_MBUS_FMT_YUYV12_1X24, ADV7604_OP_CH_SEL_RGB, false, false,
786 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
787 { V4L2_MBUS_FMT_YVYU12_1X24, ADV7604_OP_CH_SEL_RGB, false, true,
788 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
789};
790
791static const struct adv7604_format_info adv7611_formats[] = {
792 { V4L2_MBUS_FMT_RGB888_1X24, ADV7604_OP_CH_SEL_RGB, true, false,
793 ADV7604_OP_MODE_SEL_SDR_444 | ADV7604_OP_FORMAT_SEL_8BIT },
794 { V4L2_MBUS_FMT_YUYV8_2X8, ADV7604_OP_CH_SEL_RGB, false, false,
795 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_8BIT },
796 { V4L2_MBUS_FMT_YVYU8_2X8, ADV7604_OP_CH_SEL_RGB, false, true,
797 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_8BIT },
798 { V4L2_MBUS_FMT_YUYV12_2X12, ADV7604_OP_CH_SEL_RGB, false, false,
799 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_12BIT },
800 { V4L2_MBUS_FMT_YVYU12_2X12, ADV7604_OP_CH_SEL_RGB, false, true,
801 ADV7604_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_12BIT },
802 { V4L2_MBUS_FMT_UYVY8_1X16, ADV7604_OP_CH_SEL_RBG, false, false,
803 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
804 { V4L2_MBUS_FMT_VYUY8_1X16, ADV7604_OP_CH_SEL_RBG, false, true,
805 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
806 { V4L2_MBUS_FMT_YUYV8_1X16, ADV7604_OP_CH_SEL_RGB, false, false,
807 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
808 { V4L2_MBUS_FMT_YVYU8_1X16, ADV7604_OP_CH_SEL_RGB, false, true,
809 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_8BIT },
810 { V4L2_MBUS_FMT_UYVY12_1X24, ADV7604_OP_CH_SEL_RBG, false, false,
811 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
812 { V4L2_MBUS_FMT_VYUY12_1X24, ADV7604_OP_CH_SEL_RBG, false, true,
813 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
814 { V4L2_MBUS_FMT_YUYV12_1X24, ADV7604_OP_CH_SEL_RGB, false, false,
815 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
816 { V4L2_MBUS_FMT_YVYU12_1X24, ADV7604_OP_CH_SEL_RGB, false, true,
817 ADV7604_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_12BIT },
818};
819
820static const struct adv7604_format_info *
821adv7604_format_info(struct adv7604_state *state, enum v4l2_mbus_pixelcode code)
822{
823 unsigned int i;
824
825 for (i = 0; i < state->info->nformats; ++i) {
826 if (state->info->formats[i].code == code)
827 return &state->info->formats[i];
828 }
829
830 return NULL;
831}
832
Hans Verkuil54450f52012-07-18 05:45:16 -0300833/* ----------------------------------------------------------------------- */
834
Mats Randgaard4a31a932013-12-10 09:45:00 -0300835static inline bool is_analog_input(struct v4l2_subdev *sd)
836{
837 struct adv7604_state *state = to_state(sd);
838
Laurent Pinchartc784b1e2014-01-29 10:08:58 -0300839 return state->selected_input == ADV7604_PAD_VGA_RGB ||
840 state->selected_input == ADV7604_PAD_VGA_COMP;
Mats Randgaard4a31a932013-12-10 09:45:00 -0300841}
842
843static inline bool is_digital_input(struct v4l2_subdev *sd)
844{
845 struct adv7604_state *state = to_state(sd);
846
Laurent Pinchartc784b1e2014-01-29 10:08:58 -0300847 return state->selected_input == ADV7604_PAD_HDMI_PORT_A ||
848 state->selected_input == ADV7604_PAD_HDMI_PORT_B ||
849 state->selected_input == ADV7604_PAD_HDMI_PORT_C ||
850 state->selected_input == ADV7604_PAD_HDMI_PORT_D;
Mats Randgaard4a31a932013-12-10 09:45:00 -0300851}
852
853/* ----------------------------------------------------------------------- */
854
Hans Verkuil54450f52012-07-18 05:45:16 -0300855#ifdef CONFIG_VIDEO_ADV_DEBUG
856static void adv7604_inv_register(struct v4l2_subdev *sd)
857{
858 v4l2_info(sd, "0x000-0x0ff: IO Map\n");
859 v4l2_info(sd, "0x100-0x1ff: AVLink Map\n");
860 v4l2_info(sd, "0x200-0x2ff: CEC Map\n");
861 v4l2_info(sd, "0x300-0x3ff: InfoFrame Map\n");
862 v4l2_info(sd, "0x400-0x4ff: ESDP Map\n");
863 v4l2_info(sd, "0x500-0x5ff: DPP Map\n");
864 v4l2_info(sd, "0x600-0x6ff: AFE Map\n");
865 v4l2_info(sd, "0x700-0x7ff: Repeater Map\n");
866 v4l2_info(sd, "0x800-0x8ff: EDID Map\n");
867 v4l2_info(sd, "0x900-0x9ff: HDMI Map\n");
868 v4l2_info(sd, "0xa00-0xaff: Test Map\n");
869 v4l2_info(sd, "0xb00-0xbff: CP Map\n");
870 v4l2_info(sd, "0xc00-0xcff: VDP Map\n");
871}
872
873static int adv7604_g_register(struct v4l2_subdev *sd,
874 struct v4l2_dbg_register *reg)
875{
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300876 int ret;
877
878 ret = adv7604_read_reg(sd, reg->reg);
879 if (ret < 0) {
Hans Verkuil54450f52012-07-18 05:45:16 -0300880 v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
881 adv7604_inv_register(sd);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300882 return ret;
Hans Verkuil54450f52012-07-18 05:45:16 -0300883 }
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300884
885 reg->size = 1;
886 reg->val = ret;
887
Hans Verkuil54450f52012-07-18 05:45:16 -0300888 return 0;
889}
890
891static int adv7604_s_register(struct v4l2_subdev *sd,
Hans Verkuil977ba3b2013-03-24 08:28:46 -0300892 const struct v4l2_dbg_register *reg)
Hans Verkuil54450f52012-07-18 05:45:16 -0300893{
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300894 int ret;
Hans Verkuil15774612013-12-10 10:02:43 -0300895
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300896 ret = adv7604_write_reg(sd, reg->reg, reg->val);
897 if (ret < 0) {
Hans Verkuil54450f52012-07-18 05:45:16 -0300898 v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
899 adv7604_inv_register(sd);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300900 return ret;
Hans Verkuil54450f52012-07-18 05:45:16 -0300901 }
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300902
Hans Verkuil54450f52012-07-18 05:45:16 -0300903 return 0;
904}
905#endif
906
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300907static unsigned int adv7604_read_cable_det(struct v4l2_subdev *sd)
908{
909 u8 value = io_read(sd, 0x6f);
910
911 return ((value & 0x10) >> 4)
912 | ((value & 0x08) >> 2)
913 | ((value & 0x04) << 0)
914 | ((value & 0x02) << 2);
915}
916
917static unsigned int adv7611_read_cable_det(struct v4l2_subdev *sd)
918{
919 u8 value = io_read(sd, 0x6f);
920
921 return value & 1;
922}
923
Hans Verkuil54450f52012-07-18 05:45:16 -0300924static int adv7604_s_detect_tx_5v_ctrl(struct v4l2_subdev *sd)
925{
926 struct adv7604_state *state = to_state(sd);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300927 const struct adv7604_chip_info *info = state->info;
Hans Verkuil54450f52012-07-18 05:45:16 -0300928
Hans Verkuil54450f52012-07-18 05:45:16 -0300929 return v4l2_ctrl_s_ctrl(state->detect_tx_5v_ctrl,
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300930 info->read_cable_det(sd));
Hans Verkuil54450f52012-07-18 05:45:16 -0300931}
932
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300933static int find_and_set_predefined_video_timings(struct v4l2_subdev *sd,
934 u8 prim_mode,
935 const struct adv7604_video_standards *predef_vid_timings,
936 const struct v4l2_dv_timings *timings)
Hans Verkuil54450f52012-07-18 05:45:16 -0300937{
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300938 int i;
939
940 for (i = 0; predef_vid_timings[i].timings.bt.width; i++) {
Hans Verkuilef1ed8f2013-08-15 08:28:47 -0300941 if (!v4l2_match_dv_timings(timings, &predef_vid_timings[i].timings,
Mats Randgaard4a31a932013-12-10 09:45:00 -0300942 is_digital_input(sd) ? 250000 : 1000000))
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300943 continue;
944 io_write(sd, 0x00, predef_vid_timings[i].vid_std); /* video std */
945 io_write(sd, 0x01, (predef_vid_timings[i].v_freq << 4) +
946 prim_mode); /* v_freq and prim mode */
947 return 0;
948 }
949
950 return -1;
951}
952
953static int configure_predefined_video_timings(struct v4l2_subdev *sd,
954 struct v4l2_dv_timings *timings)
955{
956 struct adv7604_state *state = to_state(sd);
957 int err;
958
959 v4l2_dbg(1, debug, sd, "%s", __func__);
960
Lars-Peter Clausend42010a2013-11-25 15:45:07 -0300961 if (adv7604_has_afe(state)) {
962 /* reset to default values */
963 io_write(sd, 0x16, 0x43);
964 io_write(sd, 0x17, 0x5a);
965 }
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300966 /* disable embedded syncs for auto graphics mode */
Laurent Pinchart22d97e52014-01-30 17:17:42 -0300967 cp_write_clr_set(sd, 0x81, 0x10, 0x00);
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300968 cp_write(sd, 0x8f, 0x00);
969 cp_write(sd, 0x90, 0x00);
970 cp_write(sd, 0xa2, 0x00);
971 cp_write(sd, 0xa3, 0x00);
972 cp_write(sd, 0xa4, 0x00);
973 cp_write(sd, 0xa5, 0x00);
974 cp_write(sd, 0xa6, 0x00);
975 cp_write(sd, 0xa7, 0x00);
976 cp_write(sd, 0xab, 0x00);
977 cp_write(sd, 0xac, 0x00);
978
Mats Randgaard4a31a932013-12-10 09:45:00 -0300979 if (is_analog_input(sd)) {
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300980 err = find_and_set_predefined_video_timings(sd,
981 0x01, adv7604_prim_mode_comp, timings);
982 if (err)
983 err = find_and_set_predefined_video_timings(sd,
984 0x02, adv7604_prim_mode_gr, timings);
Mats Randgaard4a31a932013-12-10 09:45:00 -0300985 } else if (is_digital_input(sd)) {
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300986 err = find_and_set_predefined_video_timings(sd,
987 0x05, adv7604_prim_mode_hdmi_comp, timings);
988 if (err)
989 err = find_and_set_predefined_video_timings(sd,
990 0x06, adv7604_prim_mode_hdmi_gr, timings);
Mats Randgaard4a31a932013-12-10 09:45:00 -0300991 } else {
992 v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
993 __func__, state->selected_input);
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300994 err = -1;
Hans Verkuilccbd5bc2012-10-16 10:02:05 -0300995 }
996
997
998 return err;
999}
1000
1001static void configure_custom_video_timings(struct v4l2_subdev *sd,
1002 const struct v4l2_bt_timings *bt)
1003{
1004 struct adv7604_state *state = to_state(sd);
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001005 u32 width = htotal(bt);
1006 u32 height = vtotal(bt);
1007 u16 cp_start_sav = bt->hsync + bt->hbackporch - 4;
1008 u16 cp_start_eav = width - bt->hfrontporch;
1009 u16 cp_start_vbi = height - bt->vfrontporch;
1010 u16 cp_end_vbi = bt->vsync + bt->vbackporch;
1011 u16 ch1_fr_ll = (((u32)bt->pixelclock / 100) > 0) ?
1012 ((width * (ADV7604_fsc / 100)) / ((u32)bt->pixelclock / 100)) : 0;
1013 const u8 pll[2] = {
1014 0xc0 | ((width >> 8) & 0x1f),
1015 width & 0xff
1016 };
Hans Verkuil54450f52012-07-18 05:45:16 -03001017
1018 v4l2_dbg(2, debug, sd, "%s\n", __func__);
1019
Mats Randgaard4a31a932013-12-10 09:45:00 -03001020 if (is_analog_input(sd)) {
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001021 /* auto graphics */
1022 io_write(sd, 0x00, 0x07); /* video std */
1023 io_write(sd, 0x01, 0x02); /* prim mode */
1024 /* enable embedded syncs for auto graphics mode */
Laurent Pinchart22d97e52014-01-30 17:17:42 -03001025 cp_write_clr_set(sd, 0x81, 0x10, 0x10);
Hans Verkuil54450f52012-07-18 05:45:16 -03001026
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001027 /* Should only be set in auto-graphics mode [REF_02, p. 91-92] */
Hans Verkuil54450f52012-07-18 05:45:16 -03001028 /* setup PLL_DIV_MAN_EN and PLL_DIV_RATIO */
1029 /* IO-map reg. 0x16 and 0x17 should be written in sequence */
Laurent Pinchart05cacb12014-01-30 16:32:21 -03001030 if (adv_smbus_write_i2c_block_data(state, ADV7604_PAGE_IO,
1031 0x16, 2, pll))
Hans Verkuil54450f52012-07-18 05:45:16 -03001032 v4l2_err(sd, "writing to reg 0x16 and 0x17 failed\n");
Hans Verkuil54450f52012-07-18 05:45:16 -03001033
1034 /* active video - horizontal timing */
Hans Verkuil54450f52012-07-18 05:45:16 -03001035 cp_write(sd, 0xa2, (cp_start_sav >> 4) & 0xff);
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001036 cp_write(sd, 0xa3, ((cp_start_sav & 0x0f) << 4) |
Mats Randgaard4a31a932013-12-10 09:45:00 -03001037 ((cp_start_eav >> 8) & 0x0f));
Hans Verkuil54450f52012-07-18 05:45:16 -03001038 cp_write(sd, 0xa4, cp_start_eav & 0xff);
1039
1040 /* active video - vertical timing */
Hans Verkuil54450f52012-07-18 05:45:16 -03001041 cp_write(sd, 0xa5, (cp_start_vbi >> 4) & 0xff);
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001042 cp_write(sd, 0xa6, ((cp_start_vbi & 0xf) << 4) |
Mats Randgaard4a31a932013-12-10 09:45:00 -03001043 ((cp_end_vbi >> 8) & 0xf));
Hans Verkuil54450f52012-07-18 05:45:16 -03001044 cp_write(sd, 0xa7, cp_end_vbi & 0xff);
Mats Randgaard4a31a932013-12-10 09:45:00 -03001045 } else if (is_digital_input(sd)) {
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001046 /* set default prim_mode/vid_std for HDMI
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001047 according to [REF_03, c. 4.2] */
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001048 io_write(sd, 0x00, 0x02); /* video std */
1049 io_write(sd, 0x01, 0x06); /* prim mode */
Mats Randgaard4a31a932013-12-10 09:45:00 -03001050 } else {
1051 v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
1052 __func__, state->selected_input);
Hans Verkuil54450f52012-07-18 05:45:16 -03001053 }
Hans Verkuil54450f52012-07-18 05:45:16 -03001054
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001055 cp_write(sd, 0x8f, (ch1_fr_ll >> 8) & 0x7);
1056 cp_write(sd, 0x90, ch1_fr_ll & 0xff);
1057 cp_write(sd, 0xab, (height >> 4) & 0xff);
1058 cp_write(sd, 0xac, (height & 0x0f) << 4);
1059}
Hans Verkuil54450f52012-07-18 05:45:16 -03001060
Mats Randgaard5c6c6342013-12-05 10:39:04 -03001061static void adv7604_set_offset(struct v4l2_subdev *sd, bool auto_offset, u16 offset_a, u16 offset_b, u16 offset_c)
1062{
1063 struct adv7604_state *state = to_state(sd);
1064 u8 offset_buf[4];
1065
1066 if (auto_offset) {
1067 offset_a = 0x3ff;
1068 offset_b = 0x3ff;
1069 offset_c = 0x3ff;
1070 }
1071
1072 v4l2_dbg(2, debug, sd, "%s: %s offset: a = 0x%x, b = 0x%x, c = 0x%x\n",
1073 __func__, auto_offset ? "Auto" : "Manual",
1074 offset_a, offset_b, offset_c);
1075
1076 offset_buf[0] = (cp_read(sd, 0x77) & 0xc0) | ((offset_a & 0x3f0) >> 4);
1077 offset_buf[1] = ((offset_a & 0x00f) << 4) | ((offset_b & 0x3c0) >> 6);
1078 offset_buf[2] = ((offset_b & 0x03f) << 2) | ((offset_c & 0x300) >> 8);
1079 offset_buf[3] = offset_c & 0x0ff;
1080
1081 /* Registers must be written in this order with no i2c access in between */
Laurent Pinchart05cacb12014-01-30 16:32:21 -03001082 if (adv_smbus_write_i2c_block_data(state, ADV7604_PAGE_CP,
1083 0x77, 4, offset_buf))
Mats Randgaard5c6c6342013-12-05 10:39:04 -03001084 v4l2_err(sd, "%s: i2c error writing to CP reg 0x77, 0x78, 0x79, 0x7a\n", __func__);
1085}
1086
1087static void adv7604_set_gain(struct v4l2_subdev *sd, bool auto_gain, u16 gain_a, u16 gain_b, u16 gain_c)
1088{
1089 struct adv7604_state *state = to_state(sd);
1090 u8 gain_buf[4];
1091 u8 gain_man = 1;
1092 u8 agc_mode_man = 1;
1093
1094 if (auto_gain) {
1095 gain_man = 0;
1096 agc_mode_man = 0;
1097 gain_a = 0x100;
1098 gain_b = 0x100;
1099 gain_c = 0x100;
1100 }
1101
1102 v4l2_dbg(2, debug, sd, "%s: %s gain: a = 0x%x, b = 0x%x, c = 0x%x\n",
1103 __func__, auto_gain ? "Auto" : "Manual",
1104 gain_a, gain_b, gain_c);
1105
1106 gain_buf[0] = ((gain_man << 7) | (agc_mode_man << 6) | ((gain_a & 0x3f0) >> 4));
1107 gain_buf[1] = (((gain_a & 0x00f) << 4) | ((gain_b & 0x3c0) >> 6));
1108 gain_buf[2] = (((gain_b & 0x03f) << 2) | ((gain_c & 0x300) >> 8));
1109 gain_buf[3] = ((gain_c & 0x0ff));
1110
1111 /* Registers must be written in this order with no i2c access in between */
Laurent Pinchart05cacb12014-01-30 16:32:21 -03001112 if (adv_smbus_write_i2c_block_data(state, ADV7604_PAGE_CP,
1113 0x73, 4, gain_buf))
Mats Randgaard5c6c6342013-12-05 10:39:04 -03001114 v4l2_err(sd, "%s: i2c error writing to CP reg 0x73, 0x74, 0x75, 0x76\n", __func__);
1115}
1116
Hans Verkuil54450f52012-07-18 05:45:16 -03001117static void set_rgb_quantization_range(struct v4l2_subdev *sd)
1118{
1119 struct adv7604_state *state = to_state(sd);
Mats Randgaard5c6c6342013-12-05 10:39:04 -03001120 bool rgb_output = io_read(sd, 0x02) & 0x02;
1121 bool hdmi_signal = hdmi_read(sd, 0x05) & 0x80;
Hans Verkuil54450f52012-07-18 05:45:16 -03001122
Mats Randgaard5c6c6342013-12-05 10:39:04 -03001123 v4l2_dbg(2, debug, sd, "%s: RGB quantization range: %d, RGB out: %d, HDMI: %d\n",
1124 __func__, state->rgb_quantization_range,
1125 rgb_output, hdmi_signal);
1126
1127 adv7604_set_gain(sd, true, 0x0, 0x0, 0x0);
1128 adv7604_set_offset(sd, true, 0x0, 0x0, 0x0);
Mats Randgaard98332392013-12-05 10:05:58 -03001129
Hans Verkuil54450f52012-07-18 05:45:16 -03001130 switch (state->rgb_quantization_range) {
1131 case V4L2_DV_RGB_RANGE_AUTO:
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03001132 if (state->selected_input == ADV7604_PAD_VGA_RGB) {
Mats Randgaard98332392013-12-05 10:05:58 -03001133 /* Receiving analog RGB signal
1134 * Set RGB full range (0-255) */
Laurent Pinchart22d97e52014-01-30 17:17:42 -03001135 io_write_clr_set(sd, 0x02, 0xf0, 0x10);
Mats Randgaard98332392013-12-05 10:05:58 -03001136 break;
1137 }
Hans Verkuil54450f52012-07-18 05:45:16 -03001138
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03001139 if (state->selected_input == ADV7604_PAD_VGA_COMP) {
Mats Randgaard98332392013-12-05 10:05:58 -03001140 /* Receiving analog YPbPr signal
1141 * Set automode */
Laurent Pinchart22d97e52014-01-30 17:17:42 -03001142 io_write_clr_set(sd, 0x02, 0xf0, 0xf0);
Mats Randgaard98332392013-12-05 10:05:58 -03001143 break;
1144 }
1145
Mats Randgaard5c6c6342013-12-05 10:39:04 -03001146 if (hdmi_signal) {
Mats Randgaard98332392013-12-05 10:05:58 -03001147 /* Receiving HDMI signal
1148 * Set automode */
Laurent Pinchart22d97e52014-01-30 17:17:42 -03001149 io_write_clr_set(sd, 0x02, 0xf0, 0xf0);
Mats Randgaard98332392013-12-05 10:05:58 -03001150 break;
1151 }
1152
1153 /* Receiving DVI-D signal
1154 * ADV7604 selects RGB limited range regardless of
1155 * input format (CE/IT) in automatic mode */
1156 if (state->timings.bt.standards & V4L2_DV_BT_STD_CEA861) {
1157 /* RGB limited range (16-235) */
Laurent Pinchart22d97e52014-01-30 17:17:42 -03001158 io_write_clr_set(sd, 0x02, 0xf0, 0x00);
Mats Randgaard98332392013-12-05 10:05:58 -03001159 } else {
1160 /* RGB full range (0-255) */
Laurent Pinchart22d97e52014-01-30 17:17:42 -03001161 io_write_clr_set(sd, 0x02, 0xf0, 0x10);
Mats Randgaard5c6c6342013-12-05 10:39:04 -03001162
1163 if (is_digital_input(sd) && rgb_output) {
1164 adv7604_set_offset(sd, false, 0x40, 0x40, 0x40);
1165 } else {
1166 adv7604_set_gain(sd, false, 0xe0, 0xe0, 0xe0);
1167 adv7604_set_offset(sd, false, 0x70, 0x70, 0x70);
1168 }
Hans Verkuil54450f52012-07-18 05:45:16 -03001169 }
1170 break;
1171 case V4L2_DV_RGB_RANGE_LIMITED:
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03001172 if (state->selected_input == ADV7604_PAD_VGA_COMP) {
Mats Randgaardd261e842013-12-05 10:17:15 -03001173 /* YCrCb limited range (16-235) */
Laurent Pinchart22d97e52014-01-30 17:17:42 -03001174 io_write_clr_set(sd, 0x02, 0xf0, 0x20);
Mats Randgaard5c6c6342013-12-05 10:39:04 -03001175 break;
Mats Randgaardd261e842013-12-05 10:17:15 -03001176 }
Mats Randgaard5c6c6342013-12-05 10:39:04 -03001177
1178 /* RGB limited range (16-235) */
Laurent Pinchart22d97e52014-01-30 17:17:42 -03001179 io_write_clr_set(sd, 0x02, 0xf0, 0x00);
Mats Randgaard5c6c6342013-12-05 10:39:04 -03001180
Hans Verkuil54450f52012-07-18 05:45:16 -03001181 break;
1182 case V4L2_DV_RGB_RANGE_FULL:
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03001183 if (state->selected_input == ADV7604_PAD_VGA_COMP) {
Mats Randgaardd261e842013-12-05 10:17:15 -03001184 /* YCrCb full range (0-255) */
Laurent Pinchart22d97e52014-01-30 17:17:42 -03001185 io_write_clr_set(sd, 0x02, 0xf0, 0x60);
Mats Randgaard5c6c6342013-12-05 10:39:04 -03001186 break;
1187 }
1188
1189 /* RGB full range (0-255) */
Laurent Pinchart22d97e52014-01-30 17:17:42 -03001190 io_write_clr_set(sd, 0x02, 0xf0, 0x10);
Mats Randgaard5c6c6342013-12-05 10:39:04 -03001191
1192 if (is_analog_input(sd) || hdmi_signal)
1193 break;
1194
1195 /* Adjust gain/offset for DVI-D signals only */
1196 if (rgb_output) {
1197 adv7604_set_offset(sd, false, 0x40, 0x40, 0x40);
Mats Randgaardd261e842013-12-05 10:17:15 -03001198 } else {
Mats Randgaard5c6c6342013-12-05 10:39:04 -03001199 adv7604_set_gain(sd, false, 0xe0, 0xe0, 0xe0);
1200 adv7604_set_offset(sd, false, 0x70, 0x70, 0x70);
Mats Randgaardd261e842013-12-05 10:17:15 -03001201 }
Hans Verkuil54450f52012-07-18 05:45:16 -03001202 break;
1203 }
1204}
1205
Hans Verkuil54450f52012-07-18 05:45:16 -03001206static int adv7604_s_ctrl(struct v4l2_ctrl *ctrl)
1207{
Laurent Pinchartc2698872014-01-30 15:16:03 -03001208 struct v4l2_subdev *sd =
1209 &container_of(ctrl->handler, struct adv7604_state, hdl)->sd;
1210
Hans Verkuil54450f52012-07-18 05:45:16 -03001211 struct adv7604_state *state = to_state(sd);
1212
1213 switch (ctrl->id) {
1214 case V4L2_CID_BRIGHTNESS:
1215 cp_write(sd, 0x3c, ctrl->val);
1216 return 0;
1217 case V4L2_CID_CONTRAST:
1218 cp_write(sd, 0x3a, ctrl->val);
1219 return 0;
1220 case V4L2_CID_SATURATION:
1221 cp_write(sd, 0x3b, ctrl->val);
1222 return 0;
1223 case V4L2_CID_HUE:
1224 cp_write(sd, 0x3d, ctrl->val);
1225 return 0;
1226 case V4L2_CID_DV_RX_RGB_RANGE:
1227 state->rgb_quantization_range = ctrl->val;
1228 set_rgb_quantization_range(sd);
1229 return 0;
1230 case V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE:
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001231 if (!adv7604_has_afe(state))
1232 return -EINVAL;
Hans Verkuil54450f52012-07-18 05:45:16 -03001233 /* Set the analog sampling phase. This is needed to find the
1234 best sampling phase for analog video: an application or
1235 driver has to try a number of phases and analyze the picture
1236 quality before settling on the best performing phase. */
1237 afe_write(sd, 0xc8, ctrl->val);
1238 return 0;
1239 case V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL:
1240 /* Use the default blue color for free running mode,
1241 or supply your own. */
Laurent Pinchart22d97e52014-01-30 17:17:42 -03001242 cp_write_clr_set(sd, 0xbf, 0x04, ctrl->val << 2);
Hans Verkuil54450f52012-07-18 05:45:16 -03001243 return 0;
1244 case V4L2_CID_ADV_RX_FREE_RUN_COLOR:
1245 cp_write(sd, 0xc0, (ctrl->val & 0xff0000) >> 16);
1246 cp_write(sd, 0xc1, (ctrl->val & 0x00ff00) >> 8);
1247 cp_write(sd, 0xc2, (u8)(ctrl->val & 0x0000ff));
1248 return 0;
1249 }
1250 return -EINVAL;
1251}
1252
Hans Verkuil54450f52012-07-18 05:45:16 -03001253/* ----------------------------------------------------------------------- */
1254
1255static inline bool no_power(struct v4l2_subdev *sd)
1256{
1257 /* Entire chip or CP powered off */
1258 return io_read(sd, 0x0c) & 0x24;
1259}
1260
1261static inline bool no_signal_tmds(struct v4l2_subdev *sd)
1262{
Mats Randgaard4a31a932013-12-10 09:45:00 -03001263 struct adv7604_state *state = to_state(sd);
1264
1265 return !(io_read(sd, 0x6a) & (0x10 >> state->selected_input));
Hans Verkuil54450f52012-07-18 05:45:16 -03001266}
1267
1268static inline bool no_lock_tmds(struct v4l2_subdev *sd)
1269{
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001270 struct adv7604_state *state = to_state(sd);
1271 const struct adv7604_chip_info *info = state->info;
1272
1273 return (io_read(sd, 0x6a) & info->tdms_lock_mask) != info->tdms_lock_mask;
Hans Verkuil54450f52012-07-18 05:45:16 -03001274}
1275
Martin Buggebb88f322013-08-14 08:52:46 -03001276static inline bool is_hdmi(struct v4l2_subdev *sd)
1277{
1278 return hdmi_read(sd, 0x05) & 0x80;
1279}
1280
Hans Verkuil54450f52012-07-18 05:45:16 -03001281static inline bool no_lock_sspd(struct v4l2_subdev *sd)
1282{
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001283 struct adv7604_state *state = to_state(sd);
1284
1285 /*
1286 * Chips without a AFE don't expose registers for the SSPD, so just assume
1287 * that we have a lock.
1288 */
1289 if (adv7604_has_afe(state))
1290 return false;
1291
Hans Verkuil54450f52012-07-18 05:45:16 -03001292 /* TODO channel 2 */
1293 return ((cp_read(sd, 0xb5) & 0xd0) != 0xd0);
1294}
1295
1296static inline bool no_lock_stdi(struct v4l2_subdev *sd)
1297{
1298 /* TODO channel 2 */
1299 return !(cp_read(sd, 0xb1) & 0x80);
1300}
1301
1302static inline bool no_signal(struct v4l2_subdev *sd)
1303{
Hans Verkuil54450f52012-07-18 05:45:16 -03001304 bool ret;
1305
1306 ret = no_power(sd);
1307
1308 ret |= no_lock_stdi(sd);
1309 ret |= no_lock_sspd(sd);
1310
Mats Randgaard4a31a932013-12-10 09:45:00 -03001311 if (is_digital_input(sd)) {
Hans Verkuil54450f52012-07-18 05:45:16 -03001312 ret |= no_lock_tmds(sd);
1313 ret |= no_signal_tmds(sd);
1314 }
1315
1316 return ret;
1317}
1318
1319static inline bool no_lock_cp(struct v4l2_subdev *sd)
1320{
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001321 struct adv7604_state *state = to_state(sd);
1322
1323 if (!adv7604_has_afe(state))
1324 return false;
1325
Hans Verkuil54450f52012-07-18 05:45:16 -03001326 /* CP has detected a non standard number of lines on the incoming
1327 video compared to what it is configured to receive by s_dv_timings */
1328 return io_read(sd, 0x12) & 0x01;
1329}
1330
1331static int adv7604_g_input_status(struct v4l2_subdev *sd, u32 *status)
1332{
Hans Verkuil54450f52012-07-18 05:45:16 -03001333 *status = 0;
1334 *status |= no_power(sd) ? V4L2_IN_ST_NO_POWER : 0;
1335 *status |= no_signal(sd) ? V4L2_IN_ST_NO_SIGNAL : 0;
1336 if (no_lock_cp(sd))
Mats Randgaard4a31a932013-12-10 09:45:00 -03001337 *status |= is_digital_input(sd) ? V4L2_IN_ST_NO_SYNC : V4L2_IN_ST_NO_H_LOCK;
Hans Verkuil54450f52012-07-18 05:45:16 -03001338
1339 v4l2_dbg(1, debug, sd, "%s: status = 0x%x\n", __func__, *status);
1340
1341 return 0;
1342}
1343
1344/* ----------------------------------------------------------------------- */
1345
Hans Verkuil54450f52012-07-18 05:45:16 -03001346struct stdi_readback {
1347 u16 bl, lcf, lcvs;
1348 u8 hs_pol, vs_pol;
1349 bool interlaced;
1350};
1351
1352static int stdi2dv_timings(struct v4l2_subdev *sd,
1353 struct stdi_readback *stdi,
1354 struct v4l2_dv_timings *timings)
1355{
1356 struct adv7604_state *state = to_state(sd);
1357 u32 hfreq = (ADV7604_fsc * 8) / stdi->bl;
1358 u32 pix_clk;
1359 int i;
1360
1361 for (i = 0; adv7604_timings[i].bt.height; i++) {
1362 if (vtotal(&adv7604_timings[i].bt) != stdi->lcf + 1)
1363 continue;
1364 if (adv7604_timings[i].bt.vsync != stdi->lcvs)
1365 continue;
1366
1367 pix_clk = hfreq * htotal(&adv7604_timings[i].bt);
1368
1369 if ((pix_clk < adv7604_timings[i].bt.pixelclock + 1000000) &&
1370 (pix_clk > adv7604_timings[i].bt.pixelclock - 1000000)) {
1371 *timings = adv7604_timings[i];
1372 return 0;
1373 }
1374 }
1375
1376 if (v4l2_detect_cvt(stdi->lcf + 1, hfreq, stdi->lcvs,
1377 (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
1378 (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
1379 timings))
1380 return 0;
1381 if (v4l2_detect_gtf(stdi->lcf + 1, hfreq, stdi->lcvs,
1382 (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
1383 (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
1384 state->aspect_ratio, timings))
1385 return 0;
1386
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001387 v4l2_dbg(2, debug, sd,
1388 "%s: No format candidate found for lcvs = %d, lcf=%d, bl = %d, %chsync, %cvsync\n",
1389 __func__, stdi->lcvs, stdi->lcf, stdi->bl,
1390 stdi->hs_pol, stdi->vs_pol);
Hans Verkuil54450f52012-07-18 05:45:16 -03001391 return -1;
1392}
1393
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001394
Hans Verkuil54450f52012-07-18 05:45:16 -03001395static int read_stdi(struct v4l2_subdev *sd, struct stdi_readback *stdi)
1396{
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001397 struct adv7604_state *state = to_state(sd);
1398 const struct adv7604_chip_info *info = state->info;
Laurent Pinchart4a2ccdd2014-01-08 20:26:55 -03001399 u8 polarity;
1400
Hans Verkuil54450f52012-07-18 05:45:16 -03001401 if (no_lock_stdi(sd) || no_lock_sspd(sd)) {
1402 v4l2_dbg(2, debug, sd, "%s: STDI and/or SSPD not locked\n", __func__);
1403 return -1;
1404 }
1405
1406 /* read STDI */
Laurent Pinchart51182a92014-01-08 19:30:37 -03001407 stdi->bl = cp_read16(sd, 0xb1, 0x3fff);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001408 stdi->lcf = cp_read16(sd, info->lcf_reg, 0x7ff);
Hans Verkuil54450f52012-07-18 05:45:16 -03001409 stdi->lcvs = cp_read(sd, 0xb3) >> 3;
1410 stdi->interlaced = io_read(sd, 0x12) & 0x10;
1411
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001412 if (adv7604_has_afe(state)) {
1413 /* read SSPD */
1414 polarity = cp_read(sd, 0xb5);
1415 if ((polarity & 0x03) == 0x01) {
1416 stdi->hs_pol = polarity & 0x10
1417 ? (polarity & 0x08 ? '+' : '-') : 'x';
1418 stdi->vs_pol = polarity & 0x40
1419 ? (polarity & 0x20 ? '+' : '-') : 'x';
1420 } else {
1421 stdi->hs_pol = 'x';
1422 stdi->vs_pol = 'x';
1423 }
Hans Verkuil54450f52012-07-18 05:45:16 -03001424 } else {
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001425 polarity = hdmi_read(sd, 0x05);
1426 stdi->hs_pol = polarity & 0x20 ? '+' : '-';
1427 stdi->vs_pol = polarity & 0x10 ? '+' : '-';
Hans Verkuil54450f52012-07-18 05:45:16 -03001428 }
1429
1430 if (no_lock_stdi(sd) || no_lock_sspd(sd)) {
1431 v4l2_dbg(2, debug, sd,
1432 "%s: signal lost during readout of STDI/SSPD\n", __func__);
1433 return -1;
1434 }
1435
1436 if (stdi->lcf < 239 || stdi->bl < 8 || stdi->bl == 0x3fff) {
1437 v4l2_dbg(2, debug, sd, "%s: invalid signal\n", __func__);
1438 memset(stdi, 0, sizeof(struct stdi_readback));
1439 return -1;
1440 }
1441
1442 v4l2_dbg(2, debug, sd,
1443 "%s: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, %chsync, %cvsync, %s\n",
1444 __func__, stdi->lcf, stdi->bl, stdi->lcvs,
1445 stdi->hs_pol, stdi->vs_pol,
1446 stdi->interlaced ? "interlaced" : "progressive");
1447
1448 return 0;
1449}
1450
1451static int adv7604_enum_dv_timings(struct v4l2_subdev *sd,
1452 struct v4l2_enum_dv_timings *timings)
1453{
Laurent Pinchartafec5592014-01-29 10:09:41 -03001454 struct adv7604_state *state = to_state(sd);
1455
Hans Verkuil54450f52012-07-18 05:45:16 -03001456 if (timings->index >= ARRAY_SIZE(adv7604_timings) - 1)
1457 return -EINVAL;
Laurent Pinchartafec5592014-01-29 10:09:41 -03001458
1459 if (timings->pad >= state->source_pad)
1460 return -EINVAL;
1461
Hans Verkuil54450f52012-07-18 05:45:16 -03001462 memset(timings->reserved, 0, sizeof(timings->reserved));
1463 timings->timings = adv7604_timings[timings->index];
1464 return 0;
1465}
1466
Laurent Pinchart7515e092014-01-31 08:51:18 -03001467static int adv7604_dv_timings_cap(struct v4l2_subdev *sd,
1468 struct v4l2_dv_timings_cap *cap)
Laurent Pinchartafec5592014-01-29 10:09:41 -03001469{
Laurent Pinchart7515e092014-01-31 08:51:18 -03001470 struct adv7604_state *state = to_state(sd);
1471
1472 if (cap->pad >= state->source_pad)
1473 return -EINVAL;
1474
Laurent Pinchartafec5592014-01-29 10:09:41 -03001475 cap->type = V4L2_DV_BT_656_1120;
1476 cap->bt.max_width = 1920;
1477 cap->bt.max_height = 1200;
1478 cap->bt.min_pixelclock = 25000000;
1479
Laurent Pinchart7515e092014-01-31 08:51:18 -03001480 switch (cap->pad) {
Laurent Pinchartafec5592014-01-29 10:09:41 -03001481 case ADV7604_PAD_HDMI_PORT_A:
1482 case ADV7604_PAD_HDMI_PORT_B:
1483 case ADV7604_PAD_HDMI_PORT_C:
1484 case ADV7604_PAD_HDMI_PORT_D:
1485 cap->bt.max_pixelclock = 225000000;
1486 break;
1487 case ADV7604_PAD_VGA_RGB:
1488 case ADV7604_PAD_VGA_COMP:
1489 default:
1490 cap->bt.max_pixelclock = 170000000;
1491 break;
1492 }
1493
1494 cap->bt.standards = V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
1495 V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT;
1496 cap->bt.capabilities = V4L2_DV_BT_CAP_PROGRESSIVE |
1497 V4L2_DV_BT_CAP_REDUCED_BLANKING | V4L2_DV_BT_CAP_CUSTOM;
1498 return 0;
1499}
1500
Hans Verkuil54450f52012-07-18 05:45:16 -03001501/* Fill the optional fields .standards and .flags in struct v4l2_dv_timings
1502 if the format is listed in adv7604_timings[] */
1503static void adv7604_fill_optional_dv_timings_fields(struct v4l2_subdev *sd,
1504 struct v4l2_dv_timings *timings)
1505{
Hans Verkuil54450f52012-07-18 05:45:16 -03001506 int i;
1507
1508 for (i = 0; adv7604_timings[i].bt.width; i++) {
Hans Verkuilef1ed8f2013-08-15 08:28:47 -03001509 if (v4l2_match_dv_timings(timings, &adv7604_timings[i],
Mats Randgaard4a31a932013-12-10 09:45:00 -03001510 is_digital_input(sd) ? 250000 : 1000000)) {
Hans Verkuil54450f52012-07-18 05:45:16 -03001511 *timings = adv7604_timings[i];
1512 break;
1513 }
1514 }
1515}
1516
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001517static unsigned int adv7604_read_hdmi_pixelclock(struct v4l2_subdev *sd)
1518{
1519 unsigned int freq;
1520 int a, b;
1521
1522 a = hdmi_read(sd, 0x06);
1523 b = hdmi_read(sd, 0x3b);
1524 if (a < 0 || b < 0)
1525 return 0;
1526 freq = a * 1000000 + ((b & 0x30) >> 4) * 250000;
1527
1528 if (is_hdmi(sd)) {
1529 /* adjust for deep color mode */
1530 unsigned bits_per_channel = ((hdmi_read(sd, 0x0b) & 0x60) >> 4) + 8;
1531
1532 freq = freq * 8 / bits_per_channel;
1533 }
1534
1535 return freq;
1536}
1537
1538static unsigned int adv7611_read_hdmi_pixelclock(struct v4l2_subdev *sd)
1539{
1540 int a, b;
1541
1542 a = hdmi_read(sd, 0x51);
1543 b = hdmi_read(sd, 0x52);
1544 if (a < 0 || b < 0)
1545 return 0;
1546 return ((a << 1) | (b >> 7)) * 1000000 + (b & 0x7f) * 1000000 / 128;
1547}
1548
Hans Verkuil54450f52012-07-18 05:45:16 -03001549static int adv7604_query_dv_timings(struct v4l2_subdev *sd,
1550 struct v4l2_dv_timings *timings)
1551{
1552 struct adv7604_state *state = to_state(sd);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001553 const struct adv7604_chip_info *info = state->info;
Hans Verkuil54450f52012-07-18 05:45:16 -03001554 struct v4l2_bt_timings *bt = &timings->bt;
1555 struct stdi_readback stdi;
1556
1557 if (!timings)
1558 return -EINVAL;
1559
1560 memset(timings, 0, sizeof(struct v4l2_dv_timings));
1561
1562 if (no_signal(sd)) {
Martin Bugge1e0b9152013-12-05 10:34:46 -03001563 state->restart_stdi_once = true;
Hans Verkuil54450f52012-07-18 05:45:16 -03001564 v4l2_dbg(1, debug, sd, "%s: no valid signal\n", __func__);
1565 return -ENOLINK;
1566 }
1567
1568 /* read STDI */
1569 if (read_stdi(sd, &stdi)) {
1570 v4l2_dbg(1, debug, sd, "%s: STDI/SSPD not locked\n", __func__);
1571 return -ENOLINK;
1572 }
1573 bt->interlaced = stdi.interlaced ?
1574 V4L2_DV_INTERLACED : V4L2_DV_PROGRESSIVE;
1575
Mats Randgaard4a31a932013-12-10 09:45:00 -03001576 if (is_digital_input(sd)) {
Hans Verkuil54450f52012-07-18 05:45:16 -03001577 timings->type = V4L2_DV_BT_656_1120;
1578
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001579 /* FIXME: All masks are incorrect for ADV7611 */
Laurent Pinchart51182a92014-01-08 19:30:37 -03001580 bt->width = hdmi_read16(sd, 0x07, 0xfff);
1581 bt->height = hdmi_read16(sd, 0x09, 0xfff);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001582 bt->pixelclock = info->read_hdmi_pixelclock(sd);
Laurent Pinchart51182a92014-01-08 19:30:37 -03001583 bt->hfrontporch = hdmi_read16(sd, 0x20, 0x3ff);
1584 bt->hsync = hdmi_read16(sd, 0x22, 0x3ff);
1585 bt->hbackporch = hdmi_read16(sd, 0x24, 0x3ff);
1586 bt->vfrontporch = hdmi_read16(sd, 0x2a, 0x1fff) / 2;
1587 bt->vsync = hdmi_read16(sd, 0x2e, 0x1fff) / 2;
1588 bt->vbackporch = hdmi_read16(sd, 0x32, 0x1fff) / 2;
Hans Verkuil54450f52012-07-18 05:45:16 -03001589 bt->polarities = ((hdmi_read(sd, 0x05) & 0x10) ? V4L2_DV_VSYNC_POS_POL : 0) |
1590 ((hdmi_read(sd, 0x05) & 0x20) ? V4L2_DV_HSYNC_POS_POL : 0);
1591 if (bt->interlaced == V4L2_DV_INTERLACED) {
Laurent Pinchart51182a92014-01-08 19:30:37 -03001592 bt->height += hdmi_read16(sd, 0x0b, 0xfff);
1593 bt->il_vfrontporch = hdmi_read16(sd, 0x2c, 0x1fff) / 2;
1594 bt->il_vsync = hdmi_read16(sd, 0x30, 0x1fff) / 2;
1595 bt->vbackporch = hdmi_read16(sd, 0x34, 0x1fff) / 2;
Hans Verkuil54450f52012-07-18 05:45:16 -03001596 }
1597 adv7604_fill_optional_dv_timings_fields(sd, timings);
1598 } else {
1599 /* find format
Hans Verkuil80939642012-10-16 05:46:21 -03001600 * Since LCVS values are inaccurate [REF_03, p. 275-276],
Hans Verkuil54450f52012-07-18 05:45:16 -03001601 * stdi2dv_timings() is called with lcvs +-1 if the first attempt fails.
1602 */
1603 if (!stdi2dv_timings(sd, &stdi, timings))
1604 goto found;
1605 stdi.lcvs += 1;
1606 v4l2_dbg(1, debug, sd, "%s: lcvs + 1 = %d\n", __func__, stdi.lcvs);
1607 if (!stdi2dv_timings(sd, &stdi, timings))
1608 goto found;
1609 stdi.lcvs -= 2;
1610 v4l2_dbg(1, debug, sd, "%s: lcvs - 1 = %d\n", __func__, stdi.lcvs);
1611 if (stdi2dv_timings(sd, &stdi, timings)) {
Hans Verkuilcf9afb12012-10-16 10:12:55 -03001612 /*
1613 * The STDI block may measure wrong values, especially
1614 * for lcvs and lcf. If the driver can not find any
1615 * valid timing, the STDI block is restarted to measure
1616 * the video timings again. The function will return an
1617 * error, but the restart of STDI will generate a new
1618 * STDI interrupt and the format detection process will
1619 * restart.
1620 */
1621 if (state->restart_stdi_once) {
1622 v4l2_dbg(1, debug, sd, "%s: restart STDI\n", __func__);
1623 /* TODO restart STDI for Sync Channel 2 */
1624 /* enter one-shot mode */
Laurent Pinchart22d97e52014-01-30 17:17:42 -03001625 cp_write_clr_set(sd, 0x86, 0x06, 0x00);
Hans Verkuilcf9afb12012-10-16 10:12:55 -03001626 /* trigger STDI restart */
Laurent Pinchart22d97e52014-01-30 17:17:42 -03001627 cp_write_clr_set(sd, 0x86, 0x06, 0x04);
Hans Verkuilcf9afb12012-10-16 10:12:55 -03001628 /* reset to continuous mode */
Laurent Pinchart22d97e52014-01-30 17:17:42 -03001629 cp_write_clr_set(sd, 0x86, 0x06, 0x02);
Hans Verkuilcf9afb12012-10-16 10:12:55 -03001630 state->restart_stdi_once = false;
1631 return -ENOLINK;
1632 }
Hans Verkuil54450f52012-07-18 05:45:16 -03001633 v4l2_dbg(1, debug, sd, "%s: format not supported\n", __func__);
1634 return -ERANGE;
1635 }
Hans Verkuilcf9afb12012-10-16 10:12:55 -03001636 state->restart_stdi_once = true;
Hans Verkuil54450f52012-07-18 05:45:16 -03001637 }
1638found:
1639
1640 if (no_signal(sd)) {
1641 v4l2_dbg(1, debug, sd, "%s: signal lost during readout\n", __func__);
1642 memset(timings, 0, sizeof(struct v4l2_dv_timings));
1643 return -ENOLINK;
1644 }
1645
Mats Randgaard4a31a932013-12-10 09:45:00 -03001646 if ((is_analog_input(sd) && bt->pixelclock > 170000000) ||
1647 (is_digital_input(sd) && bt->pixelclock > 225000000)) {
Hans Verkuil54450f52012-07-18 05:45:16 -03001648 v4l2_dbg(1, debug, sd, "%s: pixelclock out of range %d\n",
1649 __func__, (u32)bt->pixelclock);
1650 return -ERANGE;
1651 }
1652
1653 if (debug > 1)
Hans Verkuil11d034c2013-08-15 08:05:59 -03001654 v4l2_print_dv_timings(sd->name, "adv7604_query_dv_timings: ",
1655 timings, true);
Hans Verkuil54450f52012-07-18 05:45:16 -03001656
1657 return 0;
1658}
1659
1660static int adv7604_s_dv_timings(struct v4l2_subdev *sd,
1661 struct v4l2_dv_timings *timings)
1662{
1663 struct adv7604_state *state = to_state(sd);
1664 struct v4l2_bt_timings *bt;
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001665 int err;
Hans Verkuil54450f52012-07-18 05:45:16 -03001666
1667 if (!timings)
1668 return -EINVAL;
1669
Mats Randgaardd48eb482013-12-12 10:13:35 -03001670 if (v4l2_match_dv_timings(&state->timings, timings, 0)) {
1671 v4l2_dbg(1, debug, sd, "%s: no change\n", __func__);
1672 return 0;
1673 }
1674
Hans Verkuil54450f52012-07-18 05:45:16 -03001675 bt = &timings->bt;
1676
Mats Randgaard4a31a932013-12-10 09:45:00 -03001677 if ((is_analog_input(sd) && bt->pixelclock > 170000000) ||
1678 (is_digital_input(sd) && bt->pixelclock > 225000000)) {
Hans Verkuil54450f52012-07-18 05:45:16 -03001679 v4l2_dbg(1, debug, sd, "%s: pixelclock out of range %d\n",
1680 __func__, (u32)bt->pixelclock);
1681 return -ERANGE;
1682 }
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001683
Hans Verkuil54450f52012-07-18 05:45:16 -03001684 adv7604_fill_optional_dv_timings_fields(sd, timings);
1685
1686 state->timings = *timings;
1687
Laurent Pinchart22d97e52014-01-30 17:17:42 -03001688 cp_write_clr_set(sd, 0x91, 0x40, bt->interlaced ? 0x40 : 0x00);
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03001689
1690 /* Use prim_mode and vid_std when available */
1691 err = configure_predefined_video_timings(sd, timings);
1692 if (err) {
1693 /* custom settings when the video format
1694 does not have prim_mode/vid_std */
1695 configure_custom_video_timings(sd, bt);
1696 }
Hans Verkuil54450f52012-07-18 05:45:16 -03001697
1698 set_rgb_quantization_range(sd);
1699
Hans Verkuil54450f52012-07-18 05:45:16 -03001700 if (debug > 1)
Hans Verkuil11d034c2013-08-15 08:05:59 -03001701 v4l2_print_dv_timings(sd->name, "adv7604_s_dv_timings: ",
1702 timings, true);
Hans Verkuil54450f52012-07-18 05:45:16 -03001703 return 0;
1704}
1705
1706static int adv7604_g_dv_timings(struct v4l2_subdev *sd,
1707 struct v4l2_dv_timings *timings)
1708{
1709 struct adv7604_state *state = to_state(sd);
1710
1711 *timings = state->timings;
1712 return 0;
1713}
1714
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001715static void adv7604_set_termination(struct v4l2_subdev *sd, bool enable)
1716{
1717 hdmi_write(sd, 0x01, enable ? 0x00 : 0x78);
1718}
1719
1720static void adv7611_set_termination(struct v4l2_subdev *sd, bool enable)
1721{
1722 hdmi_write(sd, 0x83, enable ? 0xfe : 0xff);
1723}
1724
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001725static void enable_input(struct v4l2_subdev *sd)
Hans Verkuil54450f52012-07-18 05:45:16 -03001726{
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001727 struct adv7604_state *state = to_state(sd);
1728
Mats Randgaard4a31a932013-12-10 09:45:00 -03001729 if (is_analog_input(sd)) {
Hans Verkuil54450f52012-07-18 05:45:16 -03001730 io_write(sd, 0x15, 0xb0); /* Disable Tristate of Pins (no audio) */
Mats Randgaard4a31a932013-12-10 09:45:00 -03001731 } else if (is_digital_input(sd)) {
Laurent Pinchart22d97e52014-01-30 17:17:42 -03001732 hdmi_write_clr_set(sd, 0x00, 0x03, state->selected_input);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001733 state->info->set_termination(sd, true);
Hans Verkuil54450f52012-07-18 05:45:16 -03001734 io_write(sd, 0x15, 0xa0); /* Disable Tristate of Pins */
Laurent Pinchart22d97e52014-01-30 17:17:42 -03001735 hdmi_write_clr_set(sd, 0x1a, 0x10, 0x00); /* Unmute audio */
Mats Randgaard4a31a932013-12-10 09:45:00 -03001736 } else {
1737 v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
1738 __func__, state->selected_input);
Hans Verkuil54450f52012-07-18 05:45:16 -03001739 }
1740}
1741
1742static void disable_input(struct v4l2_subdev *sd)
1743{
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001744 struct adv7604_state *state = to_state(sd);
1745
Laurent Pinchart22d97e52014-01-30 17:17:42 -03001746 hdmi_write_clr_set(sd, 0x1a, 0x10, 0x10); /* Mute audio */
Mats Randgaard5474b982013-12-05 10:33:41 -03001747 msleep(16); /* 512 samples with >= 32 kHz sample rate [REF_03, c. 7.16.10] */
Hans Verkuil54450f52012-07-18 05:45:16 -03001748 io_write(sd, 0x15, 0xbe); /* Tristate all outputs from video core */
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001749 state->info->set_termination(sd, false);
Hans Verkuil54450f52012-07-18 05:45:16 -03001750}
1751
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001752static void select_input(struct v4l2_subdev *sd)
Hans Verkuil54450f52012-07-18 05:45:16 -03001753{
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001754 struct adv7604_state *state = to_state(sd);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001755 const struct adv7604_chip_info *info = state->info;
Hans Verkuil54450f52012-07-18 05:45:16 -03001756
Mats Randgaard4a31a932013-12-10 09:45:00 -03001757 if (is_analog_input(sd)) {
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001758 adv7604_write_reg_seq(sd, info->recommended_settings[0]);
Hans Verkuil54450f52012-07-18 05:45:16 -03001759
1760 afe_write(sd, 0x00, 0x08); /* power up ADC */
1761 afe_write(sd, 0x01, 0x06); /* power up Analog Front End */
1762 afe_write(sd, 0xc8, 0x00); /* phase control */
Mats Randgaard4a31a932013-12-10 09:45:00 -03001763 } else if (is_digital_input(sd)) {
1764 hdmi_write(sd, 0x00, state->selected_input & 0x03);
Hans Verkuil54450f52012-07-18 05:45:16 -03001765
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001766 adv7604_write_reg_seq(sd, info->recommended_settings[1]);
Hans Verkuil54450f52012-07-18 05:45:16 -03001767
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001768 if (adv7604_has_afe(state)) {
1769 afe_write(sd, 0x00, 0xff); /* power down ADC */
1770 afe_write(sd, 0x01, 0xfe); /* power down Analog Front End */
1771 afe_write(sd, 0xc8, 0x40); /* phase control */
1772 }
Hans Verkuil54450f52012-07-18 05:45:16 -03001773
Hans Verkuil54450f52012-07-18 05:45:16 -03001774 cp_write(sd, 0x3e, 0x00); /* CP core pre-gain control */
1775 cp_write(sd, 0xc3, 0x39); /* CP coast control. Graphics mode */
1776 cp_write(sd, 0x40, 0x80); /* CP core pre-gain control. Graphics mode */
Mats Randgaard4a31a932013-12-10 09:45:00 -03001777 } else {
1778 v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
1779 __func__, state->selected_input);
Hans Verkuil54450f52012-07-18 05:45:16 -03001780 }
1781}
1782
1783static int adv7604_s_routing(struct v4l2_subdev *sd,
1784 u32 input, u32 output, u32 config)
1785{
1786 struct adv7604_state *state = to_state(sd);
1787
Mats Randgaardff4f80f2013-12-05 10:24:05 -03001788 v4l2_dbg(2, debug, sd, "%s: input %d, selected input %d",
1789 __func__, input, state->selected_input);
1790
1791 if (input == state->selected_input)
1792 return 0;
Hans Verkuil54450f52012-07-18 05:45:16 -03001793
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001794 if (input > state->info->max_port)
1795 return -EINVAL;
1796
Mats Randgaard4a31a932013-12-10 09:45:00 -03001797 state->selected_input = input;
Hans Verkuil54450f52012-07-18 05:45:16 -03001798
1799 disable_input(sd);
1800
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001801 select_input(sd);
Hans Verkuil54450f52012-07-18 05:45:16 -03001802
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001803 enable_input(sd);
Hans Verkuil54450f52012-07-18 05:45:16 -03001804
1805 return 0;
1806}
1807
Laurent Pinchart539b33b2014-01-26 18:42:37 -03001808static int adv7604_enum_mbus_code(struct v4l2_subdev *sd,
1809 struct v4l2_subdev_fh *fh,
1810 struct v4l2_subdev_mbus_code_enum *code)
Hans Verkuil54450f52012-07-18 05:45:16 -03001811{
1812 struct adv7604_state *state = to_state(sd);
1813
Laurent Pinchart539b33b2014-01-26 18:42:37 -03001814 if (code->index >= state->info->nformats)
1815 return -EINVAL;
1816
1817 code->code = state->info->formats[code->index].code;
1818
1819 return 0;
1820}
1821
1822static void adv7604_fill_format(struct adv7604_state *state,
1823 struct v4l2_mbus_framefmt *format)
1824{
1825 memset(format, 0, sizeof(*format));
1826
1827 format->width = state->timings.bt.width;
1828 format->height = state->timings.bt.height;
1829 format->field = V4L2_FIELD_NONE;
1830
1831 if (state->timings.bt.standards & V4L2_DV_BT_STD_CEA861)
1832 format->colorspace = (state->timings.bt.height <= 576) ?
Hans Verkuil54450f52012-07-18 05:45:16 -03001833 V4L2_COLORSPACE_SMPTE170M : V4L2_COLORSPACE_REC709;
Laurent Pinchart539b33b2014-01-26 18:42:37 -03001834}
1835
1836/*
1837 * Compute the op_ch_sel value required to obtain on the bus the component order
1838 * corresponding to the selected format taking into account bus reordering
1839 * applied by the board at the output of the device.
1840 *
1841 * The following table gives the op_ch_value from the format component order
1842 * (expressed as op_ch_sel value in column) and the bus reordering (expressed as
1843 * adv7604_bus_order value in row).
1844 *
1845 * | GBR(0) GRB(1) BGR(2) RGB(3) BRG(4) RBG(5)
1846 * ----------+-------------------------------------------------
1847 * RGB (NOP) | GBR GRB BGR RGB BRG RBG
1848 * GRB (1-2) | BGR RGB GBR GRB RBG BRG
1849 * RBG (2-3) | GRB GBR BRG RBG BGR RGB
1850 * BGR (1-3) | RBG BRG RGB BGR GRB GBR
1851 * BRG (ROR) | BRG RBG GRB GBR RGB BGR
1852 * GBR (ROL) | RGB BGR RBG BRG GBR GRB
1853 */
1854static unsigned int adv7604_op_ch_sel(struct adv7604_state *state)
1855{
1856#define _SEL(a,b,c,d,e,f) { \
1857 ADV7604_OP_CH_SEL_##a, ADV7604_OP_CH_SEL_##b, ADV7604_OP_CH_SEL_##c, \
1858 ADV7604_OP_CH_SEL_##d, ADV7604_OP_CH_SEL_##e, ADV7604_OP_CH_SEL_##f }
1859#define _BUS(x) [ADV7604_BUS_ORDER_##x]
1860
1861 static const unsigned int op_ch_sel[6][6] = {
1862 _BUS(RGB) /* NOP */ = _SEL(GBR, GRB, BGR, RGB, BRG, RBG),
1863 _BUS(GRB) /* 1-2 */ = _SEL(BGR, RGB, GBR, GRB, RBG, BRG),
1864 _BUS(RBG) /* 2-3 */ = _SEL(GRB, GBR, BRG, RBG, BGR, RGB),
1865 _BUS(BGR) /* 1-3 */ = _SEL(RBG, BRG, RGB, BGR, GRB, GBR),
1866 _BUS(BRG) /* ROR */ = _SEL(BRG, RBG, GRB, GBR, RGB, BGR),
1867 _BUS(GBR) /* ROL */ = _SEL(RGB, BGR, RBG, BRG, GBR, GRB),
1868 };
1869
1870 return op_ch_sel[state->pdata.bus_order][state->format->op_ch_sel >> 5];
1871}
1872
1873static void adv7604_setup_format(struct adv7604_state *state)
1874{
1875 struct v4l2_subdev *sd = &state->sd;
1876
Laurent Pinchart22d97e52014-01-30 17:17:42 -03001877 io_write_clr_set(sd, 0x02, 0x02,
Laurent Pinchart539b33b2014-01-26 18:42:37 -03001878 state->format->rgb_out ? ADV7604_RGB_OUT : 0);
1879 io_write(sd, 0x03, state->format->op_format_sel |
1880 state->pdata.op_format_mode_sel);
Laurent Pinchart22d97e52014-01-30 17:17:42 -03001881 io_write_clr_set(sd, 0x04, 0xe0, adv7604_op_ch_sel(state));
1882 io_write_clr_set(sd, 0x05, 0x01,
Laurent Pinchart539b33b2014-01-26 18:42:37 -03001883 state->format->swap_cb_cr ? ADV7604_OP_SWAP_CB_CR : 0);
1884}
1885
1886static int adv7604_get_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
1887 struct v4l2_subdev_format *format)
1888{
1889 struct adv7604_state *state = to_state(sd);
1890
1891 if (format->pad != state->source_pad)
1892 return -EINVAL;
1893
1894 adv7604_fill_format(state, &format->format);
1895
1896 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1897 struct v4l2_mbus_framefmt *fmt;
1898
1899 fmt = v4l2_subdev_get_try_format(fh, format->pad);
1900 format->format.code = fmt->code;
1901 } else {
1902 format->format.code = state->format->code;
Hans Verkuil54450f52012-07-18 05:45:16 -03001903 }
Laurent Pinchart539b33b2014-01-26 18:42:37 -03001904
1905 return 0;
1906}
1907
1908static int adv7604_set_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
1909 struct v4l2_subdev_format *format)
1910{
1911 struct adv7604_state *state = to_state(sd);
1912 const struct adv7604_format_info *info;
1913
1914 if (format->pad != state->source_pad)
1915 return -EINVAL;
1916
1917 info = adv7604_format_info(state, format->format.code);
1918 if (info == NULL)
1919 info = adv7604_format_info(state, V4L2_MBUS_FMT_YUYV8_2X8);
1920
1921 adv7604_fill_format(state, &format->format);
1922 format->format.code = info->code;
1923
1924 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1925 struct v4l2_mbus_framefmt *fmt;
1926
1927 fmt = v4l2_subdev_get_try_format(fh, format->pad);
1928 fmt->code = format->format.code;
1929 } else {
1930 state->format = info;
1931 adv7604_setup_format(state);
1932 }
1933
Hans Verkuil54450f52012-07-18 05:45:16 -03001934 return 0;
1935}
1936
1937static int adv7604_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
1938{
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001939 struct adv7604_state *state = to_state(sd);
1940 const struct adv7604_chip_info *info = state->info;
Mats Randgaardf24d2292013-12-10 10:15:13 -03001941 const u8 irq_reg_0x43 = io_read(sd, 0x43);
1942 const u8 irq_reg_0x6b = io_read(sd, 0x6b);
1943 const u8 irq_reg_0x70 = io_read(sd, 0x70);
1944 u8 fmt_change_digital;
1945 u8 fmt_change;
1946 u8 tx_5v;
1947
1948 if (irq_reg_0x43)
1949 io_write(sd, 0x44, irq_reg_0x43);
1950 if (irq_reg_0x70)
1951 io_write(sd, 0x71, irq_reg_0x70);
1952 if (irq_reg_0x6b)
1953 io_write(sd, 0x6c, irq_reg_0x6b);
Hans Verkuil54450f52012-07-18 05:45:16 -03001954
Mats Randgaardff4f80f2013-12-05 10:24:05 -03001955 v4l2_dbg(2, debug, sd, "%s: ", __func__);
1956
Hans Verkuil54450f52012-07-18 05:45:16 -03001957 /* format change */
Mats Randgaardf24d2292013-12-10 10:15:13 -03001958 fmt_change = irq_reg_0x43 & 0x98;
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001959 fmt_change_digital = is_digital_input(sd)
1960 ? irq_reg_0x6b & info->fmt_change_digital_mask
1961 : 0;
Mats Randgaard14d03232013-12-05 10:26:11 -03001962
Hans Verkuil54450f52012-07-18 05:45:16 -03001963 if (fmt_change || fmt_change_digital) {
1964 v4l2_dbg(1, debug, sd,
Mats Randgaard25a64ac2013-08-14 07:58:45 -03001965 "%s: fmt_change = 0x%x, fmt_change_digital = 0x%x\n",
Hans Verkuil54450f52012-07-18 05:45:16 -03001966 __func__, fmt_change, fmt_change_digital);
Mats Randgaard25a64ac2013-08-14 07:58:45 -03001967
Mats Randgaard14d03232013-12-05 10:26:11 -03001968 v4l2_subdev_notify(sd, ADV7604_FMT_CHANGE, NULL);
Mats Randgaard25a64ac2013-08-14 07:58:45 -03001969
Hans Verkuil54450f52012-07-18 05:45:16 -03001970 if (handled)
1971 *handled = true;
1972 }
Mats Randgaardf24d2292013-12-10 10:15:13 -03001973 /* HDMI/DVI mode */
1974 if (irq_reg_0x6b & 0x01) {
1975 v4l2_dbg(1, debug, sd, "%s: irq %s mode\n", __func__,
1976 (io_read(sd, 0x6a) & 0x01) ? "HDMI" : "DVI");
1977 set_rgb_quantization_range(sd);
1978 if (handled)
1979 *handled = true;
1980 }
1981
Hans Verkuil54450f52012-07-18 05:45:16 -03001982 /* tx 5v detect */
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03001983 tx_5v = io_read(sd, 0x70) & info->cable_det_mask;
Hans Verkuil54450f52012-07-18 05:45:16 -03001984 if (tx_5v) {
1985 v4l2_dbg(1, debug, sd, "%s: tx_5v: 0x%x\n", __func__, tx_5v);
1986 io_write(sd, 0x71, tx_5v);
1987 adv7604_s_detect_tx_5v_ctrl(sd);
1988 if (handled)
1989 *handled = true;
1990 }
1991 return 0;
1992}
1993
Hans Verkuilb09dfac2014-03-04 08:05:19 -03001994static int adv7604_get_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
Hans Verkuil54450f52012-07-18 05:45:16 -03001995{
1996 struct adv7604_state *state = to_state(sd);
Mats Randgaard4a31a932013-12-10 09:45:00 -03001997 u8 *data = NULL;
Hans Verkuil54450f52012-07-18 05:45:16 -03001998
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03001999 if (edid->pad > ADV7604_PAD_HDMI_PORT_D)
Hans Verkuil54450f52012-07-18 05:45:16 -03002000 return -EINVAL;
2001 if (edid->blocks == 0)
2002 return -EINVAL;
Mats Randgaard4a31a932013-12-10 09:45:00 -03002003 if (edid->blocks > 2)
Hans Verkuil54450f52012-07-18 05:45:16 -03002004 return -EINVAL;
Mats Randgaard4a31a932013-12-10 09:45:00 -03002005 if (edid->start_block > 1)
2006 return -EINVAL;
2007 if (edid->start_block == 1)
2008 edid->blocks = 1;
Mats Randgaard4a31a932013-12-10 09:45:00 -03002009
2010 if (edid->blocks > state->edid.blocks)
2011 edid->blocks = state->edid.blocks;
2012
2013 switch (edid->pad) {
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03002014 case ADV7604_PAD_HDMI_PORT_A:
2015 case ADV7604_PAD_HDMI_PORT_B:
2016 case ADV7604_PAD_HDMI_PORT_C:
2017 case ADV7604_PAD_HDMI_PORT_D:
Mats Randgaard4a31a932013-12-10 09:45:00 -03002018 if (state->edid.present & (1 << edid->pad))
2019 data = state->edid.edid;
2020 break;
2021 default:
2022 return -EINVAL;
2023 break;
2024 }
2025 if (!data)
2026 return -ENODATA;
2027
2028 memcpy(edid->edid,
2029 data + edid->start_block * 128,
Hans Verkuil54450f52012-07-18 05:45:16 -03002030 edid->blocks * 128);
2031 return 0;
2032}
2033
Mats Randgaarddd08beb2013-12-10 09:57:09 -03002034static int get_edid_spa_location(const u8 *edid)
Mats Randgaard3e86aa82013-12-10 09:55:18 -03002035{
2036 u8 d;
2037
2038 if ((edid[0x7e] != 1) ||
2039 (edid[0x80] != 0x02) ||
2040 (edid[0x81] != 0x03)) {
2041 return -1;
2042 }
2043
2044 /* search Vendor Specific Data Block (tag 3) */
2045 d = edid[0x82] & 0x7f;
2046 if (d > 4) {
2047 int i = 0x84;
2048 int end = 0x80 + d;
2049
2050 do {
2051 u8 tag = edid[i] >> 5;
2052 u8 len = edid[i] & 0x1f;
2053
2054 if ((tag == 3) && (len >= 5))
2055 return i + 4;
2056 i += len + 1;
2057 } while (i < end);
2058 }
2059 return -1;
2060}
2061
Hans Verkuilb09dfac2014-03-04 08:05:19 -03002062static int adv7604_set_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
Hans Verkuil54450f52012-07-18 05:45:16 -03002063{
2064 struct adv7604_state *state = to_state(sd);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002065 const struct adv7604_chip_info *info = state->info;
Mats Randgaarddd08beb2013-12-10 09:57:09 -03002066 int spa_loc;
Hans Verkuil54450f52012-07-18 05:45:16 -03002067 int err;
Mats Randgaarddd08beb2013-12-10 09:57:09 -03002068 int i;
Hans Verkuil54450f52012-07-18 05:45:16 -03002069
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03002070 if (edid->pad > ADV7604_PAD_HDMI_PORT_D)
Hans Verkuil54450f52012-07-18 05:45:16 -03002071 return -EINVAL;
2072 if (edid->start_block != 0)
2073 return -EINVAL;
2074 if (edid->blocks == 0) {
Mats Randgaard3e86aa82013-12-10 09:55:18 -03002075 /* Disable hotplug and I2C access to EDID RAM from DDC port */
Mats Randgaard4a31a932013-12-10 09:45:00 -03002076 state->edid.present &= ~(1 << edid->pad);
Laurent Pincharte9d50e92014-01-30 18:37:08 -03002077 adv7604_set_hpd(state, state->edid.present);
Laurent Pinchart22d97e52014-01-30 17:17:42 -03002078 rep_write_clr_set(sd, info->edid_enable_reg, 0x0f, state->edid.present);
Mats Randgaard3e86aa82013-12-10 09:55:18 -03002079
Hans Verkuil54450f52012-07-18 05:45:16 -03002080 /* Fall back to a 16:9 aspect ratio */
2081 state->aspect_ratio.numerator = 16;
2082 state->aspect_ratio.denominator = 9;
Mats Randgaard3e86aa82013-12-10 09:55:18 -03002083
2084 if (!state->edid.present)
2085 state->edid.blocks = 0;
2086
2087 v4l2_dbg(2, debug, sd, "%s: clear EDID pad %d, edid.present = 0x%x\n",
2088 __func__, edid->pad, state->edid.present);
Hans Verkuil54450f52012-07-18 05:45:16 -03002089 return 0;
2090 }
Mats Randgaard4a31a932013-12-10 09:45:00 -03002091 if (edid->blocks > 2) {
2092 edid->blocks = 2;
Hans Verkuil54450f52012-07-18 05:45:16 -03002093 return -E2BIG;
Mats Randgaard4a31a932013-12-10 09:45:00 -03002094 }
Mats Randgaard4a31a932013-12-10 09:45:00 -03002095
Mats Randgaarddd08beb2013-12-10 09:57:09 -03002096 v4l2_dbg(2, debug, sd, "%s: write EDID pad %d, edid.present = 0x%x\n",
2097 __func__, edid->pad, state->edid.present);
2098
Mats Randgaard3e86aa82013-12-10 09:55:18 -03002099 /* Disable hotplug and I2C access to EDID RAM from DDC port */
Mats Randgaard4a31a932013-12-10 09:45:00 -03002100 cancel_delayed_work_sync(&state->delayed_work_enable_hotplug);
Laurent Pincharte9d50e92014-01-30 18:37:08 -03002101 adv7604_set_hpd(state, 0);
Laurent Pinchart22d97e52014-01-30 17:17:42 -03002102 rep_write_clr_set(sd, info->edid_enable_reg, 0x0f, 0x00);
Mats Randgaard3e86aa82013-12-10 09:55:18 -03002103
Mats Randgaarddd08beb2013-12-10 09:57:09 -03002104 spa_loc = get_edid_spa_location(edid->edid);
2105 if (spa_loc < 0)
2106 spa_loc = 0xc0; /* Default value [REF_02, p. 116] */
2107
Mats Randgaard3e86aa82013-12-10 09:55:18 -03002108 switch (edid->pad) {
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03002109 case ADV7604_PAD_HDMI_PORT_A:
Mats Randgaarddd08beb2013-12-10 09:57:09 -03002110 state->spa_port_a[0] = edid->edid[spa_loc];
2111 state->spa_port_a[1] = edid->edid[spa_loc + 1];
Mats Randgaard3e86aa82013-12-10 09:55:18 -03002112 break;
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03002113 case ADV7604_PAD_HDMI_PORT_B:
Mats Randgaarddd08beb2013-12-10 09:57:09 -03002114 rep_write(sd, 0x70, edid->edid[spa_loc]);
2115 rep_write(sd, 0x71, edid->edid[spa_loc + 1]);
Mats Randgaard3e86aa82013-12-10 09:55:18 -03002116 break;
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03002117 case ADV7604_PAD_HDMI_PORT_C:
Mats Randgaarddd08beb2013-12-10 09:57:09 -03002118 rep_write(sd, 0x72, edid->edid[spa_loc]);
2119 rep_write(sd, 0x73, edid->edid[spa_loc + 1]);
Mats Randgaard3e86aa82013-12-10 09:55:18 -03002120 break;
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03002121 case ADV7604_PAD_HDMI_PORT_D:
Mats Randgaarddd08beb2013-12-10 09:57:09 -03002122 rep_write(sd, 0x74, edid->edid[spa_loc]);
2123 rep_write(sd, 0x75, edid->edid[spa_loc + 1]);
Mats Randgaard3e86aa82013-12-10 09:55:18 -03002124 break;
Mats Randgaarddd08beb2013-12-10 09:57:09 -03002125 default:
2126 return -EINVAL;
Mats Randgaard3e86aa82013-12-10 09:55:18 -03002127 }
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002128
2129 if (info->type == ADV7604) {
2130 rep_write(sd, 0x76, spa_loc & 0xff);
Laurent Pinchart22d97e52014-01-30 17:17:42 -03002131 rep_write_clr_set(sd, 0x77, 0x40, (spa_loc & 0x100) >> 2);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002132 } else {
2133 /* FIXME: Where is the SPA location LSB register ? */
Laurent Pinchart22d97e52014-01-30 17:17:42 -03002134 rep_write_clr_set(sd, 0x71, 0x01, (spa_loc & 0x100) >> 8);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002135 }
Mats Randgaard3e86aa82013-12-10 09:55:18 -03002136
Mats Randgaarddd08beb2013-12-10 09:57:09 -03002137 edid->edid[spa_loc] = state->spa_port_a[0];
2138 edid->edid[spa_loc + 1] = state->spa_port_a[1];
Mats Randgaard4a31a932013-12-10 09:45:00 -03002139
2140 memcpy(state->edid.edid, edid->edid, 128 * edid->blocks);
2141 state->edid.blocks = edid->blocks;
Hans Verkuil54450f52012-07-18 05:45:16 -03002142 state->aspect_ratio = v4l2_calc_aspect_ratio(edid->edid[0x15],
2143 edid->edid[0x16]);
Mats Randgaard3e86aa82013-12-10 09:55:18 -03002144 state->edid.present |= 1 << edid->pad;
Mats Randgaard4a31a932013-12-10 09:45:00 -03002145
2146 err = edid_write_block(sd, 128 * edid->blocks, state->edid.edid);
2147 if (err < 0) {
Mats Randgaard3e86aa82013-12-10 09:55:18 -03002148 v4l2_err(sd, "error %d writing edid pad %d\n", err, edid->pad);
Mats Randgaard4a31a932013-12-10 09:45:00 -03002149 return err;
2150 }
2151
Mats Randgaarddd08beb2013-12-10 09:57:09 -03002152 /* adv7604 calculates the checksums and enables I2C access to internal
2153 EDID RAM from DDC port. */
Laurent Pinchart22d97e52014-01-30 17:17:42 -03002154 rep_write_clr_set(sd, info->edid_enable_reg, 0x0f, state->edid.present);
Mats Randgaarddd08beb2013-12-10 09:57:09 -03002155
2156 for (i = 0; i < 1000; i++) {
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002157 if (rep_read(sd, info->edid_status_reg) & state->edid.present)
Mats Randgaarddd08beb2013-12-10 09:57:09 -03002158 break;
2159 mdelay(1);
2160 }
2161 if (i == 1000) {
2162 v4l2_err(sd, "error enabling edid (0x%x)\n", state->edid.present);
2163 return -EIO;
2164 }
2165
2166
Mats Randgaard4a31a932013-12-10 09:45:00 -03002167 /* enable hotplug after 100 ms */
2168 queue_delayed_work(state->work_queues,
2169 &state->delayed_work_enable_hotplug, HZ / 10);
2170 return 0;
Hans Verkuil54450f52012-07-18 05:45:16 -03002171}
2172
2173/*********** avi info frame CEA-861-E **************/
2174
2175static void print_avi_infoframe(struct v4l2_subdev *sd)
2176{
2177 int i;
2178 u8 buf[14];
2179 u8 avi_len;
2180 u8 avi_ver;
2181
Martin Buggebb88f322013-08-14 08:52:46 -03002182 if (!is_hdmi(sd)) {
Hans Verkuil54450f52012-07-18 05:45:16 -03002183 v4l2_info(sd, "receive DVI-D signal (AVI infoframe not supported)\n");
2184 return;
2185 }
2186 if (!(io_read(sd, 0x60) & 0x01)) {
2187 v4l2_info(sd, "AVI infoframe not received\n");
2188 return;
2189 }
2190
2191 if (io_read(sd, 0x83) & 0x01) {
2192 v4l2_info(sd, "AVI infoframe checksum error has occurred earlier\n");
2193 io_write(sd, 0x85, 0x01); /* clear AVI_INF_CKS_ERR_RAW */
2194 if (io_read(sd, 0x83) & 0x01) {
2195 v4l2_info(sd, "AVI infoframe checksum error still present\n");
2196 io_write(sd, 0x85, 0x01); /* clear AVI_INF_CKS_ERR_RAW */
2197 }
2198 }
2199
2200 avi_len = infoframe_read(sd, 0xe2);
2201 avi_ver = infoframe_read(sd, 0xe1);
2202 v4l2_info(sd, "AVI infoframe version %d (%d byte)\n",
2203 avi_ver, avi_len);
2204
2205 if (avi_ver != 0x02)
2206 return;
2207
2208 for (i = 0; i < 14; i++)
2209 buf[i] = infoframe_read(sd, i);
2210
2211 v4l2_info(sd,
2212 "\t%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
2213 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7],
2214 buf[8], buf[9], buf[10], buf[11], buf[12], buf[13]);
2215}
2216
2217static int adv7604_log_status(struct v4l2_subdev *sd)
2218{
2219 struct adv7604_state *state = to_state(sd);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002220 const struct adv7604_chip_info *info = state->info;
Hans Verkuil54450f52012-07-18 05:45:16 -03002221 struct v4l2_dv_timings timings;
2222 struct stdi_readback stdi;
2223 u8 reg_io_0x02 = io_read(sd, 0x02);
Laurent Pinchart4a2ccdd2014-01-08 20:26:55 -03002224 u8 edid_enabled;
2225 u8 cable_det;
Hans Verkuil54450f52012-07-18 05:45:16 -03002226
Lars-Peter Clausenf216ccb2013-11-25 16:15:29 -03002227 static const char * const csc_coeff_sel_rb[16] = {
Hans Verkuil54450f52012-07-18 05:45:16 -03002228 "bypassed", "YPbPr601 -> RGB", "reserved", "YPbPr709 -> RGB",
2229 "reserved", "RGB -> YPbPr601", "reserved", "RGB -> YPbPr709",
2230 "reserved", "YPbPr709 -> YPbPr601", "YPbPr601 -> YPbPr709",
2231 "reserved", "reserved", "reserved", "reserved", "manual"
2232 };
Lars-Peter Clausenf216ccb2013-11-25 16:15:29 -03002233 static const char * const input_color_space_txt[16] = {
Hans Verkuil54450f52012-07-18 05:45:16 -03002234 "RGB limited range (16-235)", "RGB full range (0-255)",
2235 "YCbCr Bt.601 (16-235)", "YCbCr Bt.709 (16-235)",
Mats Randgaard98332392013-12-05 10:05:58 -03002236 "xvYCC Bt.601", "xvYCC Bt.709",
Hans Verkuil54450f52012-07-18 05:45:16 -03002237 "YCbCr Bt.601 (0-255)", "YCbCr Bt.709 (0-255)",
2238 "invalid", "invalid", "invalid", "invalid", "invalid",
2239 "invalid", "invalid", "automatic"
2240 };
Lars-Peter Clausenf216ccb2013-11-25 16:15:29 -03002241 static const char * const rgb_quantization_range_txt[] = {
Hans Verkuil54450f52012-07-18 05:45:16 -03002242 "Automatic",
2243 "RGB limited range (16-235)",
2244 "RGB full range (0-255)",
2245 };
Lars-Peter Clausenf216ccb2013-11-25 16:15:29 -03002246 static const char * const deep_color_mode_txt[4] = {
Martin Buggebb88f322013-08-14 08:52:46 -03002247 "8-bits per channel",
2248 "10-bits per channel",
2249 "12-bits per channel",
2250 "16-bits per channel (not supported)"
2251 };
Hans Verkuil54450f52012-07-18 05:45:16 -03002252
2253 v4l2_info(sd, "-----Chip status-----\n");
2254 v4l2_info(sd, "Chip power: %s\n", no_power(sd) ? "off" : "on");
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002255 edid_enabled = rep_read(sd, info->edid_status_reg);
Mats Randgaard4a31a932013-12-10 09:45:00 -03002256 v4l2_info(sd, "EDID enabled port A: %s, B: %s, C: %s, D: %s\n",
Laurent Pinchart4a2ccdd2014-01-08 20:26:55 -03002257 ((edid_enabled & 0x01) ? "Yes" : "No"),
2258 ((edid_enabled & 0x02) ? "Yes" : "No"),
2259 ((edid_enabled & 0x04) ? "Yes" : "No"),
2260 ((edid_enabled & 0x08) ? "Yes" : "No"));
Hans Verkuil54450f52012-07-18 05:45:16 -03002261 v4l2_info(sd, "CEC: %s\n", !!(cec_read(sd, 0x2a) & 0x01) ?
2262 "enabled" : "disabled");
2263
2264 v4l2_info(sd, "-----Signal status-----\n");
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002265 cable_det = info->read_cable_det(sd);
Mats Randgaard4a31a932013-12-10 09:45:00 -03002266 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 -03002267 ((cable_det & 0x01) ? "Yes" : "No"),
2268 ((cable_det & 0x02) ? "Yes" : "No"),
Laurent Pinchart4a2ccdd2014-01-08 20:26:55 -03002269 ((cable_det & 0x04) ? "Yes" : "No"),
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002270 ((cable_det & 0x08) ? "Yes" : "No"));
Hans Verkuil54450f52012-07-18 05:45:16 -03002271 v4l2_info(sd, "TMDS signal detected: %s\n",
2272 no_signal_tmds(sd) ? "false" : "true");
2273 v4l2_info(sd, "TMDS signal locked: %s\n",
2274 no_lock_tmds(sd) ? "false" : "true");
2275 v4l2_info(sd, "SSPD locked: %s\n", no_lock_sspd(sd) ? "false" : "true");
2276 v4l2_info(sd, "STDI locked: %s\n", no_lock_stdi(sd) ? "false" : "true");
2277 v4l2_info(sd, "CP locked: %s\n", no_lock_cp(sd) ? "false" : "true");
2278 v4l2_info(sd, "CP free run: %s\n",
2279 (!!(cp_read(sd, 0xff) & 0x10) ? "on" : "off"));
Hans Verkuilccbd5bc2012-10-16 10:02:05 -03002280 v4l2_info(sd, "Prim-mode = 0x%x, video std = 0x%x, v_freq = 0x%x\n",
2281 io_read(sd, 0x01) & 0x0f, io_read(sd, 0x00) & 0x3f,
2282 (io_read(sd, 0x01) & 0x70) >> 4);
Hans Verkuil54450f52012-07-18 05:45:16 -03002283
2284 v4l2_info(sd, "-----Video Timings-----\n");
2285 if (read_stdi(sd, &stdi))
2286 v4l2_info(sd, "STDI: not locked\n");
2287 else
2288 v4l2_info(sd, "STDI: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, %s, %chsync, %cvsync\n",
2289 stdi.lcf, stdi.bl, stdi.lcvs,
2290 stdi.interlaced ? "interlaced" : "progressive",
2291 stdi.hs_pol, stdi.vs_pol);
2292 if (adv7604_query_dv_timings(sd, &timings))
2293 v4l2_info(sd, "No video detected\n");
2294 else
Hans Verkuil11d034c2013-08-15 08:05:59 -03002295 v4l2_print_dv_timings(sd->name, "Detected format: ",
2296 &timings, true);
2297 v4l2_print_dv_timings(sd->name, "Configured format: ",
2298 &state->timings, true);
Hans Verkuil54450f52012-07-18 05:45:16 -03002299
Mats Randgaard76eb2d32013-08-14 08:56:57 -03002300 if (no_signal(sd))
2301 return 0;
2302
Hans Verkuil54450f52012-07-18 05:45:16 -03002303 v4l2_info(sd, "-----Color space-----\n");
2304 v4l2_info(sd, "RGB quantization range ctrl: %s\n",
2305 rgb_quantization_range_txt[state->rgb_quantization_range]);
2306 v4l2_info(sd, "Input color space: %s\n",
2307 input_color_space_txt[reg_io_0x02 >> 4]);
2308 v4l2_info(sd, "Output color space: %s %s, saturator %s\n",
2309 (reg_io_0x02 & 0x02) ? "RGB" : "YCbCr",
2310 (reg_io_0x02 & 0x04) ? "(16-235)" : "(0-255)",
2311 ((reg_io_0x02 & 0x04) ^ (reg_io_0x02 & 0x01)) ?
Mats Randgaard76eb2d32013-08-14 08:56:57 -03002312 "enabled" : "disabled");
Hans Verkuil54450f52012-07-18 05:45:16 -03002313 v4l2_info(sd, "Color space conversion: %s\n",
2314 csc_coeff_sel_rb[cp_read(sd, 0xfc) >> 4]);
2315
Mats Randgaard4a31a932013-12-10 09:45:00 -03002316 if (!is_digital_input(sd))
Mats Randgaard76eb2d32013-08-14 08:56:57 -03002317 return 0;
2318
2319 v4l2_info(sd, "-----%s status-----\n", is_hdmi(sd) ? "HDMI" : "DVI-D");
Mats Randgaard4a31a932013-12-10 09:45:00 -03002320 v4l2_info(sd, "Digital video port selected: %c\n",
2321 (hdmi_read(sd, 0x00) & 0x03) + 'A');
2322 v4l2_info(sd, "HDCP encrypted content: %s\n",
2323 (hdmi_read(sd, 0x05) & 0x40) ? "true" : "false");
Mats Randgaard76eb2d32013-08-14 08:56:57 -03002324 v4l2_info(sd, "HDCP keys read: %s%s\n",
2325 (hdmi_read(sd, 0x04) & 0x20) ? "yes" : "no",
2326 (hdmi_read(sd, 0x04) & 0x10) ? "ERROR" : "");
2327 if (!is_hdmi(sd)) {
2328 bool audio_pll_locked = hdmi_read(sd, 0x04) & 0x01;
2329 bool audio_sample_packet_detect = hdmi_read(sd, 0x18) & 0x01;
2330 bool audio_mute = io_read(sd, 0x65) & 0x40;
2331
2332 v4l2_info(sd, "Audio: pll %s, samples %s, %s\n",
2333 audio_pll_locked ? "locked" : "not locked",
2334 audio_sample_packet_detect ? "detected" : "not detected",
2335 audio_mute ? "muted" : "enabled");
2336 if (audio_pll_locked && audio_sample_packet_detect) {
2337 v4l2_info(sd, "Audio format: %s\n",
2338 (hdmi_read(sd, 0x07) & 0x20) ? "multi-channel" : "stereo");
2339 }
2340 v4l2_info(sd, "Audio CTS: %u\n", (hdmi_read(sd, 0x5b) << 12) +
2341 (hdmi_read(sd, 0x5c) << 8) +
2342 (hdmi_read(sd, 0x5d) & 0xf0));
2343 v4l2_info(sd, "Audio N: %u\n", ((hdmi_read(sd, 0x5d) & 0x0f) << 16) +
2344 (hdmi_read(sd, 0x5e) << 8) +
2345 hdmi_read(sd, 0x5f));
2346 v4l2_info(sd, "AV Mute: %s\n", (hdmi_read(sd, 0x04) & 0x40) ? "on" : "off");
2347
2348 v4l2_info(sd, "Deep color mode: %s\n", deep_color_mode_txt[(hdmi_read(sd, 0x0b) & 0x60) >> 5]);
2349
Hans Verkuil54450f52012-07-18 05:45:16 -03002350 print_avi_infoframe(sd);
2351 }
2352
2353 return 0;
2354}
2355
2356/* ----------------------------------------------------------------------- */
2357
2358static const struct v4l2_ctrl_ops adv7604_ctrl_ops = {
2359 .s_ctrl = adv7604_s_ctrl,
2360};
2361
2362static const struct v4l2_subdev_core_ops adv7604_core_ops = {
2363 .log_status = adv7604_log_status,
Hans Verkuil54450f52012-07-18 05:45:16 -03002364 .interrupt_service_routine = adv7604_isr,
2365#ifdef CONFIG_VIDEO_ADV_DEBUG
2366 .g_register = adv7604_g_register,
2367 .s_register = adv7604_s_register,
2368#endif
2369};
2370
2371static const struct v4l2_subdev_video_ops adv7604_video_ops = {
2372 .s_routing = adv7604_s_routing,
2373 .g_input_status = adv7604_g_input_status,
2374 .s_dv_timings = adv7604_s_dv_timings,
2375 .g_dv_timings = adv7604_g_dv_timings,
2376 .query_dv_timings = adv7604_query_dv_timings,
Hans Verkuil54450f52012-07-18 05:45:16 -03002377};
2378
2379static const struct v4l2_subdev_pad_ops adv7604_pad_ops = {
Laurent Pinchart539b33b2014-01-26 18:42:37 -03002380 .enum_mbus_code = adv7604_enum_mbus_code,
2381 .get_fmt = adv7604_get_format,
2382 .set_fmt = adv7604_set_format,
Hans Verkuil54450f52012-07-18 05:45:16 -03002383 .get_edid = adv7604_get_edid,
2384 .set_edid = adv7604_set_edid,
Laurent Pinchart7515e092014-01-31 08:51:18 -03002385 .dv_timings_cap = adv7604_dv_timings_cap,
Laurent Pinchartafec5592014-01-29 10:09:41 -03002386 .enum_dv_timings = adv7604_enum_dv_timings,
Hans Verkuil54450f52012-07-18 05:45:16 -03002387};
2388
2389static const struct v4l2_subdev_ops adv7604_ops = {
2390 .core = &adv7604_core_ops,
2391 .video = &adv7604_video_ops,
2392 .pad = &adv7604_pad_ops,
2393};
2394
2395/* -------------------------- custom ctrls ---------------------------------- */
2396
2397static const struct v4l2_ctrl_config adv7604_ctrl_analog_sampling_phase = {
2398 .ops = &adv7604_ctrl_ops,
2399 .id = V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE,
2400 .name = "Analog Sampling Phase",
2401 .type = V4L2_CTRL_TYPE_INTEGER,
2402 .min = 0,
2403 .max = 0x1f,
2404 .step = 1,
2405 .def = 0,
2406};
2407
2408static const struct v4l2_ctrl_config adv7604_ctrl_free_run_color_manual = {
2409 .ops = &adv7604_ctrl_ops,
2410 .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL,
2411 .name = "Free Running Color, Manual",
2412 .type = V4L2_CTRL_TYPE_BOOLEAN,
2413 .min = false,
2414 .max = true,
2415 .step = 1,
2416 .def = false,
2417};
2418
2419static const struct v4l2_ctrl_config adv7604_ctrl_free_run_color = {
2420 .ops = &adv7604_ctrl_ops,
2421 .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR,
2422 .name = "Free Running Color",
2423 .type = V4L2_CTRL_TYPE_INTEGER,
2424 .min = 0x0,
2425 .max = 0xffffff,
2426 .step = 0x1,
2427 .def = 0x0,
2428};
2429
2430/* ----------------------------------------------------------------------- */
2431
2432static int adv7604_core_init(struct v4l2_subdev *sd)
2433{
2434 struct adv7604_state *state = to_state(sd);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002435 const struct adv7604_chip_info *info = state->info;
Hans Verkuil54450f52012-07-18 05:45:16 -03002436 struct adv7604_platform_data *pdata = &state->pdata;
2437
2438 hdmi_write(sd, 0x48,
2439 (pdata->disable_pwrdnb ? 0x80 : 0) |
2440 (pdata->disable_cable_det_rst ? 0x40 : 0));
2441
2442 disable_input(sd);
2443
2444 /* power */
2445 io_write(sd, 0x0c, 0x42); /* Power up part and power down VDP */
2446 io_write(sd, 0x0b, 0x44); /* Power down ESDP block */
2447 cp_write(sd, 0xcf, 0x01); /* Power down macrovision */
2448
2449 /* video format */
Laurent Pinchart22d97e52014-01-30 17:17:42 -03002450 io_write_clr_set(sd, 0x02, 0x0f,
Hans Verkuil54450f52012-07-18 05:45:16 -03002451 pdata->alt_gamma << 3 |
2452 pdata->op_656_range << 2 |
Hans Verkuil54450f52012-07-18 05:45:16 -03002453 pdata->alt_data_sat << 0);
Laurent Pinchart22d97e52014-01-30 17:17:42 -03002454 io_write_clr_set(sd, 0x05, 0x0e, pdata->blank_data << 3 |
Laurent Pinchart539b33b2014-01-26 18:42:37 -03002455 pdata->insert_av_codes << 2 |
2456 pdata->replicate_av_codes << 1);
2457 adv7604_setup_format(state);
Hans Verkuil54450f52012-07-18 05:45:16 -03002458
Hans Verkuil54450f52012-07-18 05:45:16 -03002459 cp_write(sd, 0x69, 0x30); /* Enable CP CSC */
Martin Bugge98908692013-12-20 05:14:57 -03002460
2461 /* VS, HS polarities */
2462 io_write(sd, 0x06, 0xa0 | pdata->inv_vs_pol << 2 | pdata->inv_hs_pol << 1);
Mikhail Khelikf31b62e2013-12-20 05:12:00 -03002463
2464 /* Adjust drive strength */
2465 io_write(sd, 0x14, 0x40 | pdata->dr_str_data << 4 |
2466 pdata->dr_str_clk << 2 |
2467 pdata->dr_str_sync);
2468
Hans Verkuil54450f52012-07-18 05:45:16 -03002469 cp_write(sd, 0xba, (pdata->hdmi_free_run_mode << 1) | 0x01); /* HDMI free run */
2470 cp_write(sd, 0xf3, 0xdc); /* Low threshold to enter/exit free run mode */
2471 cp_write(sd, 0xf9, 0x23); /* STDI ch. 1 - LCVS change threshold -
Hans Verkuil80939642012-10-16 05:46:21 -03002472 ADI recommended setting [REF_01, c. 2.3.3] */
Hans Verkuil54450f52012-07-18 05:45:16 -03002473 cp_write(sd, 0x45, 0x23); /* STDI ch. 2 - LCVS change threshold -
Hans Verkuil80939642012-10-16 05:46:21 -03002474 ADI recommended setting [REF_01, c. 2.3.3] */
Hans Verkuil54450f52012-07-18 05:45:16 -03002475 cp_write(sd, 0xc9, 0x2d); /* use prim_mode and vid_std as free run resolution
2476 for digital formats */
2477
Mats Randgaard5474b982013-12-05 10:33:41 -03002478 /* HDMI audio */
Laurent Pinchart22d97e52014-01-30 17:17:42 -03002479 hdmi_write_clr_set(sd, 0x15, 0x03, 0x03); /* Mute on FIFO over-/underflow [REF_01, c. 1.2.18] */
2480 hdmi_write_clr_set(sd, 0x1a, 0x0e, 0x08); /* Wait 1 s before unmute */
2481 hdmi_write_clr_set(sd, 0x68, 0x06, 0x06); /* FIFO reset on over-/underflow [REF_01, c. 1.2.19] */
Mats Randgaard5474b982013-12-05 10:33:41 -03002482
Hans Verkuil54450f52012-07-18 05:45:16 -03002483 /* TODO from platform data */
2484 afe_write(sd, 0xb5, 0x01); /* Setting MCLK to 256Fs */
2485
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002486 if (adv7604_has_afe(state)) {
2487 afe_write(sd, 0x02, pdata->ain_sel); /* Select analog input muxing mode */
Laurent Pinchart22d97e52014-01-30 17:17:42 -03002488 io_write_clr_set(sd, 0x30, 1 << 4, pdata->output_bus_lsb_to_msb << 4);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002489 }
Hans Verkuil54450f52012-07-18 05:45:16 -03002490
Hans Verkuil54450f52012-07-18 05:45:16 -03002491 /* interrupts */
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002492 io_write(sd, 0x40, 0xc0 | pdata->int1_config); /* Configure INT1 */
Hans Verkuil54450f52012-07-18 05:45:16 -03002493 io_write(sd, 0x46, 0x98); /* Enable SSPD, STDI and CP unlocked interrupts */
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002494 io_write(sd, 0x6e, info->fmt_change_digital_mask); /* Enable V_LOCKED and DE_REGEN_LCK interrupts */
2495 io_write(sd, 0x73, info->cable_det_mask); /* Enable cable detection (+5v) interrupts */
2496 info->setup_irqs(sd);
Hans Verkuil54450f52012-07-18 05:45:16 -03002497
2498 return v4l2_ctrl_handler_setup(sd->ctrl_handler);
2499}
2500
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002501static void adv7604_setup_irqs(struct v4l2_subdev *sd)
2502{
2503 io_write(sd, 0x41, 0xd7); /* STDI irq for any change, disable INT2 */
2504}
2505
2506static void adv7611_setup_irqs(struct v4l2_subdev *sd)
2507{
2508 io_write(sd, 0x41, 0xd0); /* STDI irq for any change, disable INT2 */
2509}
2510
Hans Verkuil54450f52012-07-18 05:45:16 -03002511static void adv7604_unregister_clients(struct adv7604_state *state)
2512{
Laurent Pinchart05cacb12014-01-30 16:32:21 -03002513 unsigned int i;
2514
2515 for (i = 1; i < ARRAY_SIZE(state->i2c_clients); ++i) {
2516 if (state->i2c_clients[i])
2517 i2c_unregister_device(state->i2c_clients[i]);
2518 }
Hans Verkuil54450f52012-07-18 05:45:16 -03002519}
2520
2521static struct i2c_client *adv7604_dummy_client(struct v4l2_subdev *sd,
2522 u8 addr, u8 io_reg)
2523{
2524 struct i2c_client *client = v4l2_get_subdevdata(sd);
2525
2526 if (addr)
2527 io_write(sd, io_reg, addr << 1);
2528 return i2c_new_dummy(client->adapter, io_read(sd, io_reg) >> 1);
2529}
2530
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002531static const struct adv7604_reg_seq adv7604_recommended_settings_afe[] = {
2532 /* reset ADI recommended settings for HDMI: */
2533 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 4. */
2534 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x0d), 0x04 }, /* HDMI filter optimization */
2535 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x0d), 0x04 }, /* HDMI filter optimization */
2536 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x3d), 0x00 }, /* DDC bus active pull-up control */
2537 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x3e), 0x74 }, /* TMDS PLL optimization */
2538 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x4e), 0x3b }, /* TMDS PLL optimization */
2539 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x57), 0x74 }, /* TMDS PLL optimization */
2540 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x58), 0x63 }, /* TMDS PLL optimization */
2541 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8d), 0x18 }, /* equaliser */
2542 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8e), 0x34 }, /* equaliser */
2543 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x93), 0x88 }, /* equaliser */
2544 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x94), 0x2e }, /* equaliser */
2545 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x96), 0x00 }, /* enable automatic EQ changing */
2546
2547 /* set ADI recommended settings for digitizer */
2548 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 17. */
2549 { ADV7604_REG(ADV7604_PAGE_AFE, 0x12), 0x7b }, /* ADC noise shaping filter controls */
2550 { ADV7604_REG(ADV7604_PAGE_AFE, 0x0c), 0x1f }, /* CP core gain controls */
2551 { ADV7604_REG(ADV7604_PAGE_CP, 0x3e), 0x04 }, /* CP core pre-gain control */
2552 { ADV7604_REG(ADV7604_PAGE_CP, 0xc3), 0x39 }, /* CP coast control. Graphics mode */
2553 { ADV7604_REG(ADV7604_PAGE_CP, 0x40), 0x5c }, /* CP core pre-gain control. Graphics mode */
2554
2555 { ADV7604_REG_SEQ_TERM, 0 },
2556};
2557
2558static const struct adv7604_reg_seq adv7604_recommended_settings_hdmi[] = {
2559 /* set ADI recommended settings for HDMI: */
2560 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 4. */
2561 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x0d), 0x84 }, /* HDMI filter optimization */
2562 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x3d), 0x10 }, /* DDC bus active pull-up control */
2563 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x3e), 0x39 }, /* TMDS PLL optimization */
2564 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x4e), 0x3b }, /* TMDS PLL optimization */
2565 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x57), 0xb6 }, /* TMDS PLL optimization */
2566 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x58), 0x03 }, /* TMDS PLL optimization */
2567 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8d), 0x18 }, /* equaliser */
2568 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8e), 0x34 }, /* equaliser */
2569 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x93), 0x8b }, /* equaliser */
2570 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x94), 0x2d }, /* equaliser */
2571 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x96), 0x01 }, /* enable automatic EQ changing */
2572
2573 /* reset ADI recommended settings for digitizer */
2574 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 17. */
2575 { ADV7604_REG(ADV7604_PAGE_AFE, 0x12), 0xfb }, /* ADC noise shaping filter controls */
2576 { ADV7604_REG(ADV7604_PAGE_AFE, 0x0c), 0x0d }, /* CP core gain controls */
2577
2578 { ADV7604_REG_SEQ_TERM, 0 },
2579};
2580
2581static const struct adv7604_reg_seq adv7611_recommended_settings_hdmi[] = {
2582 { ADV7604_REG(ADV7604_PAGE_CP, 0x6c), 0x00 },
2583 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x6f), 0x0c },
2584 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x87), 0x70 },
2585 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x57), 0xda },
2586 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x58), 0x01 },
2587 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x03), 0x98 },
2588 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x4c), 0x44 },
2589 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8d), 0x04 },
2590 { ADV7604_REG(ADV7604_PAGE_HDMI, 0x8e), 0x1e },
2591
2592 { ADV7604_REG_SEQ_TERM, 0 },
2593};
2594
2595static const struct adv7604_chip_info adv7604_chip_info[] = {
2596 [ADV7604] = {
2597 .type = ADV7604,
2598 .has_afe = true,
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03002599 .max_port = ADV7604_PAD_VGA_COMP,
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002600 .num_dv_ports = 4,
2601 .edid_enable_reg = 0x77,
2602 .edid_status_reg = 0x7d,
2603 .lcf_reg = 0xb3,
2604 .tdms_lock_mask = 0xe0,
2605 .cable_det_mask = 0x1e,
2606 .fmt_change_digital_mask = 0xc1,
Laurent Pinchart539b33b2014-01-26 18:42:37 -03002607 .formats = adv7604_formats,
2608 .nformats = ARRAY_SIZE(adv7604_formats),
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002609 .set_termination = adv7604_set_termination,
2610 .setup_irqs = adv7604_setup_irqs,
2611 .read_hdmi_pixelclock = adv7604_read_hdmi_pixelclock,
2612 .read_cable_det = adv7604_read_cable_det,
2613 .recommended_settings = {
2614 [0] = adv7604_recommended_settings_afe,
2615 [1] = adv7604_recommended_settings_hdmi,
2616 },
2617 .num_recommended_settings = {
2618 [0] = ARRAY_SIZE(adv7604_recommended_settings_afe),
2619 [1] = ARRAY_SIZE(adv7604_recommended_settings_hdmi),
2620 },
2621 .page_mask = BIT(ADV7604_PAGE_IO) | BIT(ADV7604_PAGE_AVLINK) |
2622 BIT(ADV7604_PAGE_CEC) | BIT(ADV7604_PAGE_INFOFRAME) |
2623 BIT(ADV7604_PAGE_ESDP) | BIT(ADV7604_PAGE_DPP) |
2624 BIT(ADV7604_PAGE_AFE) | BIT(ADV7604_PAGE_REP) |
2625 BIT(ADV7604_PAGE_EDID) | BIT(ADV7604_PAGE_HDMI) |
2626 BIT(ADV7604_PAGE_TEST) | BIT(ADV7604_PAGE_CP) |
2627 BIT(ADV7604_PAGE_VDP),
2628 },
2629 [ADV7611] = {
2630 .type = ADV7611,
2631 .has_afe = false,
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03002632 .max_port = ADV7604_PAD_HDMI_PORT_A,
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002633 .num_dv_ports = 1,
2634 .edid_enable_reg = 0x74,
2635 .edid_status_reg = 0x76,
2636 .lcf_reg = 0xa3,
2637 .tdms_lock_mask = 0x43,
2638 .cable_det_mask = 0x01,
2639 .fmt_change_digital_mask = 0x03,
Laurent Pinchart539b33b2014-01-26 18:42:37 -03002640 .formats = adv7611_formats,
2641 .nformats = ARRAY_SIZE(adv7611_formats),
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002642 .set_termination = adv7611_set_termination,
2643 .setup_irqs = adv7611_setup_irqs,
2644 .read_hdmi_pixelclock = adv7611_read_hdmi_pixelclock,
2645 .read_cable_det = adv7611_read_cable_det,
2646 .recommended_settings = {
2647 [1] = adv7611_recommended_settings_hdmi,
2648 },
2649 .num_recommended_settings = {
2650 [1] = ARRAY_SIZE(adv7611_recommended_settings_hdmi),
2651 },
2652 .page_mask = BIT(ADV7604_PAGE_IO) | BIT(ADV7604_PAGE_CEC) |
2653 BIT(ADV7604_PAGE_INFOFRAME) | BIT(ADV7604_PAGE_AFE) |
2654 BIT(ADV7604_PAGE_REP) | BIT(ADV7604_PAGE_EDID) |
2655 BIT(ADV7604_PAGE_HDMI) | BIT(ADV7604_PAGE_CP),
2656 },
2657};
2658
Hans Verkuil54450f52012-07-18 05:45:16 -03002659static int adv7604_probe(struct i2c_client *client,
2660 const struct i2c_device_id *id)
2661{
Hans Verkuil591b72f2013-12-17 10:05:13 -03002662 static const struct v4l2_dv_timings cea640x480 =
2663 V4L2_DV_BT_CEA_640X480P59_94;
Hans Verkuil54450f52012-07-18 05:45:16 -03002664 struct adv7604_state *state;
2665 struct adv7604_platform_data *pdata = client->dev.platform_data;
2666 struct v4l2_ctrl_handler *hdl;
2667 struct v4l2_subdev *sd;
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03002668 unsigned int i;
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002669 u16 val;
Hans Verkuil54450f52012-07-18 05:45:16 -03002670 int err;
2671
2672 /* Check if the adapter supports the needed features */
2673 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
2674 return -EIO;
2675 v4l_dbg(1, debug, client, "detecting adv7604 client on address 0x%x\n",
2676 client->addr << 1);
2677
Laurent Pinchartc02b2112013-05-02 08:29:43 -03002678 state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
Hans Verkuil54450f52012-07-18 05:45:16 -03002679 if (!state) {
2680 v4l_err(client, "Could not allocate adv7604_state memory!\n");
2681 return -ENOMEM;
2682 }
2683
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002684 state->info = &adv7604_chip_info[id->driver_data];
Laurent Pinchart05cacb12014-01-30 16:32:21 -03002685 state->i2c_clients[ADV7604_PAGE_IO] = client;
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002686
Mats Randgaard25a64ac2013-08-14 07:58:45 -03002687 /* initialize variables */
2688 state->restart_stdi_once = true;
Mats Randgaardff4f80f2013-12-05 10:24:05 -03002689 state->selected_input = ~0;
Mats Randgaard25a64ac2013-08-14 07:58:45 -03002690
Hans Verkuil54450f52012-07-18 05:45:16 -03002691 /* platform data */
2692 if (!pdata) {
2693 v4l_err(client, "No platform data!\n");
Laurent Pinchartc02b2112013-05-02 08:29:43 -03002694 return -ENODEV;
Hans Verkuil54450f52012-07-18 05:45:16 -03002695 }
Hans Verkuil591b72f2013-12-17 10:05:13 -03002696 state->pdata = *pdata;
Laurent Pincharte9d50e92014-01-30 18:37:08 -03002697
2698 /* Request GPIOs. */
2699 for (i = 0; i < state->info->num_dv_ports; ++i) {
2700 state->hpd_gpio[i] =
2701 devm_gpiod_get_index(&client->dev, "hpd", i);
2702 if (IS_ERR(state->hpd_gpio[i]))
2703 continue;
2704
2705 gpiod_set_value_cansleep(state->hpd_gpio[i], 0);
2706
2707 v4l_info(client, "Handling HPD %u GPIO\n", i);
2708 }
2709
Hans Verkuil591b72f2013-12-17 10:05:13 -03002710 state->timings = cea640x480;
Laurent Pinchart539b33b2014-01-26 18:42:37 -03002711 state->format = adv7604_format_info(state, V4L2_MBUS_FMT_YUYV8_2X8);
Hans Verkuil54450f52012-07-18 05:45:16 -03002712
2713 sd = &state->sd;
2714 v4l2_i2c_subdev_init(sd, client, &adv7604_ops);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002715 snprintf(sd->name, sizeof(sd->name), "%s %d-%04x",
2716 id->name, i2c_adapter_id(client->adapter),
2717 client->addr);
Hans Verkuil54450f52012-07-18 05:45:16 -03002718 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
Hans Verkuil54450f52012-07-18 05:45:16 -03002719
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002720 /*
2721 * Verify that the chip is present. On ADV7604 the RD_INFO register only
2722 * identifies the revision, while on ADV7611 it identifies the model as
2723 * well. Use the HDMI slave address on ADV7604 and RD_INFO on ADV7611.
2724 */
2725 if (state->info->type == ADV7604) {
2726 val = adv_smbus_read_byte_data_check(client, 0xfb, false);
2727 if (val != 0x68) {
2728 v4l2_info(sd, "not an adv7604 on address 0x%x\n",
2729 client->addr << 1);
2730 return -ENODEV;
2731 }
2732 } else {
2733 val = (adv_smbus_read_byte_data_check(client, 0xea, false) << 8)
2734 | (adv_smbus_read_byte_data_check(client, 0xeb, false) << 0);
2735 if (val != 0x2051) {
2736 v4l2_info(sd, "not an adv7611 on address 0x%x\n",
2737 client->addr << 1);
2738 return -ENODEV;
2739 }
Hans Verkuil54450f52012-07-18 05:45:16 -03002740 }
2741
2742 /* control handlers */
2743 hdl = &state->hdl;
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002744 v4l2_ctrl_handler_init(hdl, adv7604_has_afe(state) ? 9 : 8);
Hans Verkuil54450f52012-07-18 05:45:16 -03002745
2746 v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
2747 V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
2748 v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
2749 V4L2_CID_CONTRAST, 0, 255, 1, 128);
2750 v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
2751 V4L2_CID_SATURATION, 0, 255, 1, 128);
2752 v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
2753 V4L2_CID_HUE, 0, 128, 1, 0);
2754
2755 /* private controls */
2756 state->detect_tx_5v_ctrl = v4l2_ctrl_new_std(hdl, NULL,
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002757 V4L2_CID_DV_RX_POWER_PRESENT, 0,
2758 (1 << state->info->num_dv_ports) - 1, 0, 0);
Hans Verkuil54450f52012-07-18 05:45:16 -03002759 state->rgb_quantization_range_ctrl =
2760 v4l2_ctrl_new_std_menu(hdl, &adv7604_ctrl_ops,
2761 V4L2_CID_DV_RX_RGB_RANGE, V4L2_DV_RGB_RANGE_FULL,
2762 0, V4L2_DV_RGB_RANGE_AUTO);
Hans Verkuil54450f52012-07-18 05:45:16 -03002763
2764 /* custom controls */
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002765 if (adv7604_has_afe(state))
2766 state->analog_sampling_phase_ctrl =
2767 v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_analog_sampling_phase, NULL);
Hans Verkuil54450f52012-07-18 05:45:16 -03002768 state->free_run_color_manual_ctrl =
2769 v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_free_run_color_manual, NULL);
Hans Verkuil54450f52012-07-18 05:45:16 -03002770 state->free_run_color_ctrl =
2771 v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_free_run_color, NULL);
Hans Verkuil54450f52012-07-18 05:45:16 -03002772
2773 sd->ctrl_handler = hdl;
2774 if (hdl->error) {
2775 err = hdl->error;
2776 goto err_hdl;
2777 }
Hans Verkuil8c0eadb2013-08-22 06:11:17 -03002778 state->detect_tx_5v_ctrl->is_private = true;
2779 state->rgb_quantization_range_ctrl->is_private = true;
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002780 if (adv7604_has_afe(state))
2781 state->analog_sampling_phase_ctrl->is_private = true;
Hans Verkuil8c0eadb2013-08-22 06:11:17 -03002782 state->free_run_color_manual_ctrl->is_private = true;
2783 state->free_run_color_ctrl->is_private = true;
2784
Hans Verkuil54450f52012-07-18 05:45:16 -03002785 if (adv7604_s_detect_tx_5v_ctrl(sd)) {
2786 err = -ENODEV;
2787 goto err_hdl;
2788 }
2789
Laurent Pinchart05cacb12014-01-30 16:32:21 -03002790 for (i = 1; i < ADV7604_PAGE_MAX; ++i) {
2791 if (!(BIT(i) & state->info->page_mask))
2792 continue;
Hans Verkuil54450f52012-07-18 05:45:16 -03002793
Laurent Pinchart05cacb12014-01-30 16:32:21 -03002794 state->i2c_clients[i] =
2795 adv7604_dummy_client(sd, pdata->i2c_addresses[i],
2796 0xf2 + i);
2797 if (state->i2c_clients[i] == NULL) {
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002798 err = -ENOMEM;
Laurent Pinchart05cacb12014-01-30 16:32:21 -03002799 v4l2_err(sd, "failed to create i2c client %u\n", i);
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002800 goto err_i2c;
2801 }
2802 }
Laurent Pinchart05cacb12014-01-30 16:32:21 -03002803
Hans Verkuil54450f52012-07-18 05:45:16 -03002804 /* work queues */
2805 state->work_queues = create_singlethread_workqueue(client->name);
2806 if (!state->work_queues) {
2807 v4l2_err(sd, "Could not create work queue\n");
2808 err = -ENOMEM;
2809 goto err_i2c;
2810 }
2811
2812 INIT_DELAYED_WORK(&state->delayed_work_enable_hotplug,
2813 adv7604_delayed_work_enable_hotplug);
2814
Laurent Pinchartc784b1e2014-01-29 10:08:58 -03002815 state->source_pad = state->info->num_dv_ports
2816 + (state->info->has_afe ? 2 : 0);
2817 for (i = 0; i < state->source_pad; ++i)
2818 state->pads[i].flags = MEDIA_PAD_FL_SINK;
2819 state->pads[state->source_pad].flags = MEDIA_PAD_FL_SOURCE;
2820
2821 err = media_entity_init(&sd->entity, state->source_pad + 1,
2822 state->pads, 0);
Hans Verkuil54450f52012-07-18 05:45:16 -03002823 if (err)
2824 goto err_work_queues;
2825
2826 err = adv7604_core_init(sd);
2827 if (err)
2828 goto err_entity;
2829 v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
2830 client->addr << 1, client->adapter->name);
Lars-Peter Clausenbedc3932013-11-25 16:18:02 -03002831
2832 err = v4l2_async_register_subdev(sd);
2833 if (err)
2834 goto err_entity;
2835
Hans Verkuil54450f52012-07-18 05:45:16 -03002836 return 0;
2837
2838err_entity:
2839 media_entity_cleanup(&sd->entity);
2840err_work_queues:
2841 cancel_delayed_work(&state->delayed_work_enable_hotplug);
2842 destroy_workqueue(state->work_queues);
2843err_i2c:
2844 adv7604_unregister_clients(state);
2845err_hdl:
2846 v4l2_ctrl_handler_free(hdl);
Hans Verkuil54450f52012-07-18 05:45:16 -03002847 return err;
2848}
2849
2850/* ----------------------------------------------------------------------- */
2851
2852static int adv7604_remove(struct i2c_client *client)
2853{
2854 struct v4l2_subdev *sd = i2c_get_clientdata(client);
2855 struct adv7604_state *state = to_state(sd);
2856
2857 cancel_delayed_work(&state->delayed_work_enable_hotplug);
2858 destroy_workqueue(state->work_queues);
Lars-Peter Clausenbedc3932013-11-25 16:18:02 -03002859 v4l2_async_unregister_subdev(sd);
Hans Verkuil54450f52012-07-18 05:45:16 -03002860 v4l2_device_unregister_subdev(sd);
2861 media_entity_cleanup(&sd->entity);
2862 adv7604_unregister_clients(to_state(sd));
2863 v4l2_ctrl_handler_free(sd->ctrl_handler);
Hans Verkuil54450f52012-07-18 05:45:16 -03002864 return 0;
2865}
2866
2867/* ----------------------------------------------------------------------- */
2868
2869static struct i2c_device_id adv7604_id[] = {
Lars-Peter Clausend42010a2013-11-25 15:45:07 -03002870 { "adv7604", ADV7604 },
2871 { "adv7611", ADV7611 },
Hans Verkuil54450f52012-07-18 05:45:16 -03002872 { }
2873};
2874MODULE_DEVICE_TABLE(i2c, adv7604_id);
2875
2876static struct i2c_driver adv7604_driver = {
2877 .driver = {
2878 .owner = THIS_MODULE,
2879 .name = "adv7604",
2880 },
2881 .probe = adv7604_probe,
2882 .remove = adv7604_remove,
2883 .id_table = adv7604_id,
2884};
2885
2886module_i2c_driver(adv7604_driver);