2 * adv7175 - adv7175a video encoder driver version 0.0.3
4 * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
5 * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
6 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
7 * - some corrections for Pinnacle Systems Inc. DC10plus card.
9 * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
10 * - moved over to linux>=2.4.x i2c protocol (9/9/2002)
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <linux/module.h>
28 #include <linux/types.h>
29 #include <linux/slab.h>
30 #include <linux/ioctl.h>
31 #include <linux/uaccess.h>
32 #include <linux/i2c.h>
33 #include <linux/videodev2.h>
34 #include <media/v4l2-device.h>
36 MODULE_DESCRIPTION("Analog Devices ADV7175 video encoder driver");
37 MODULE_AUTHOR("Dave Perks");
38 MODULE_LICENSE("GPL");
40 #define I2C_ADV7175 0xd4
41 #define I2C_ADV7176 0x54
45 module_param(debug
, int, 0);
46 MODULE_PARM_DESC(debug
, "Debug level (0-1)");
48 /* ----------------------------------------------------------------------- */
51 struct v4l2_subdev sd
;
56 static inline struct adv7175
*to_adv7175(struct v4l2_subdev
*sd
)
58 return container_of(sd
, struct adv7175
, sd
);
61 static char *inputs
[] = { "pass_through", "play_back", "color_bar" };
63 static u32 adv7175_codes
[] = {
64 MEDIA_BUS_FMT_UYVY8_2X8
,
65 MEDIA_BUS_FMT_UYVY8_1X16
,
68 /* ----------------------------------------------------------------------- */
70 static inline int adv7175_write(struct v4l2_subdev
*sd
, u8 reg
, u8 value
)
72 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
74 return i2c_smbus_write_byte_data(client
, reg
, value
);
77 static inline int adv7175_read(struct v4l2_subdev
*sd
, u8 reg
)
79 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
81 return i2c_smbus_read_byte_data(client
, reg
);
84 static int adv7175_write_block(struct v4l2_subdev
*sd
,
85 const u8
*data
, unsigned int len
)
87 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
91 /* the adv7175 has an autoincrement function, use it if
92 * the adapter understands raw I2C */
93 if (i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
94 /* do raw I2C, not smbus compatible */
100 block_data
[block_len
++] = reg
= data
[0];
102 block_data
[block_len
++] = data
[1];
106 } while (len
>= 2 && data
[0] == reg
&& block_len
< 32);
107 ret
= i2c_master_send(client
, block_data
, block_len
);
112 /* do some slow I2C emulation kind of thing */
115 ret
= adv7175_write(sd
, reg
, *data
++);
125 static void set_subcarrier_freq(struct v4l2_subdev
*sd
, int pass_through
)
127 /* for some reason pass_through NTSC needs
128 * a different sub-carrier freq to remain stable. */
130 adv7175_write(sd
, 0x02, 0x00);
132 adv7175_write(sd
, 0x02, 0x55);
134 adv7175_write(sd
, 0x03, 0x55);
135 adv7175_write(sd
, 0x04, 0x55);
136 adv7175_write(sd
, 0x05, 0x25);
139 /* ----------------------------------------------------------------------- */
140 /* Output filter: S-Video Composite */
142 #define MR050 0x11 /* 0x09 */
143 #define MR060 0x14 /* 0x0c */
145 /* ----------------------------------------------------------------------- */
153 static const unsigned char init_common
[] = {
155 0x00, MR050
, /* MR0, PAL enabled */
156 0x01, 0x00, /* MR1 */
157 0x02, 0x0c, /* subc. freq. */
158 0x03, 0x8c, /* subc. freq. */
159 0x04, 0x79, /* subc. freq. */
160 0x05, 0x26, /* subc. freq. */
161 0x06, 0x40, /* subc. phase */
163 0x07, TR0MODE
, /* TR0, 16bit */
168 0x0c, TR1CAPT
, /* TR1 */
169 0x0d, 0x4f, /* MR2 */
176 static const unsigned char init_pal
[] = {
177 0x00, MR050
, /* MR0, PAL enabled */
178 0x01, 0x00, /* MR1 */
179 0x02, 0x0c, /* subc. freq. */
180 0x03, 0x8c, /* subc. freq. */
181 0x04, 0x79, /* subc. freq. */
182 0x05, 0x26, /* subc. freq. */
183 0x06, 0x40, /* subc. phase */
186 static const unsigned char init_ntsc
[] = {
187 0x00, MR060
, /* MR0, NTSC enabled */
188 0x01, 0x00, /* MR1 */
189 0x02, 0x55, /* subc. freq. */
190 0x03, 0x55, /* subc. freq. */
191 0x04, 0x55, /* subc. freq. */
192 0x05, 0x25, /* subc. freq. */
193 0x06, 0x1a, /* subc. phase */
196 static int adv7175_init(struct v4l2_subdev
*sd
, u32 val
)
198 /* This is just for testing!!! */
199 adv7175_write_block(sd
, init_common
, sizeof(init_common
));
200 adv7175_write(sd
, 0x07, TR0MODE
| TR0RST
);
201 adv7175_write(sd
, 0x07, TR0MODE
);
205 static int adv7175_s_std_output(struct v4l2_subdev
*sd
, v4l2_std_id std
)
207 struct adv7175
*encoder
= to_adv7175(sd
);
209 if (std
& V4L2_STD_NTSC
) {
210 adv7175_write_block(sd
, init_ntsc
, sizeof(init_ntsc
));
211 if (encoder
->input
== 0)
212 adv7175_write(sd
, 0x0d, 0x4f); /* Enable genlock */
213 adv7175_write(sd
, 0x07, TR0MODE
| TR0RST
);
214 adv7175_write(sd
, 0x07, TR0MODE
);
215 } else if (std
& V4L2_STD_PAL
) {
216 adv7175_write_block(sd
, init_pal
, sizeof(init_pal
));
217 if (encoder
->input
== 0)
218 adv7175_write(sd
, 0x0d, 0x4f); /* Enable genlock */
219 adv7175_write(sd
, 0x07, TR0MODE
| TR0RST
);
220 adv7175_write(sd
, 0x07, TR0MODE
);
221 } else if (std
& V4L2_STD_SECAM
) {
222 /* This is an attempt to convert
223 * SECAM->PAL (typically it does not work
224 * due to genlock: when decoder is in SECAM
225 * and encoder in in PAL the subcarrier can
226 * not be syncronized with horizontal
228 adv7175_write_block(sd
, init_pal
, sizeof(init_pal
));
229 if (encoder
->input
== 0)
230 adv7175_write(sd
, 0x0d, 0x49); /* Disable genlock */
231 adv7175_write(sd
, 0x07, TR0MODE
| TR0RST
);
232 adv7175_write(sd
, 0x07, TR0MODE
);
234 v4l2_dbg(1, debug
, sd
, "illegal norm: %llx\n",
235 (unsigned long long)std
);
238 v4l2_dbg(1, debug
, sd
, "switched to %llx\n", (unsigned long long)std
);
243 static int adv7175_s_routing(struct v4l2_subdev
*sd
,
244 u32 input
, u32 output
, u32 config
)
246 struct adv7175
*encoder
= to_adv7175(sd
);
248 /* RJ: input = 0: input is from decoder
249 input = 1: input is from ZR36060
250 input = 2: color bar */
254 adv7175_write(sd
, 0x01, 0x00);
256 if (encoder
->norm
& V4L2_STD_NTSC
)
257 set_subcarrier_freq(sd
, 1);
259 adv7175_write(sd
, 0x0c, TR1CAPT
); /* TR1 */
260 if (encoder
->norm
& V4L2_STD_SECAM
)
261 adv7175_write(sd
, 0x0d, 0x49); /* Disable genlock */
263 adv7175_write(sd
, 0x0d, 0x4f); /* Enable genlock */
264 adv7175_write(sd
, 0x07, TR0MODE
| TR0RST
);
265 adv7175_write(sd
, 0x07, TR0MODE
);
270 adv7175_write(sd
, 0x01, 0x00);
272 if (encoder
->norm
& V4L2_STD_NTSC
)
273 set_subcarrier_freq(sd
, 0);
275 adv7175_write(sd
, 0x0c, TR1PLAY
); /* TR1 */
276 adv7175_write(sd
, 0x0d, 0x49);
277 adv7175_write(sd
, 0x07, TR0MODE
| TR0RST
);
278 adv7175_write(sd
, 0x07, TR0MODE
);
283 adv7175_write(sd
, 0x01, 0x80);
285 if (encoder
->norm
& V4L2_STD_NTSC
)
286 set_subcarrier_freq(sd
, 0);
288 adv7175_write(sd
, 0x0d, 0x49);
289 adv7175_write(sd
, 0x07, TR0MODE
| TR0RST
);
290 adv7175_write(sd
, 0x07, TR0MODE
);
295 v4l2_dbg(1, debug
, sd
, "illegal input: %d\n", input
);
298 v4l2_dbg(1, debug
, sd
, "switched to %s\n", inputs
[input
]);
299 encoder
->input
= input
;
303 static int adv7175_enum_mbus_code(struct v4l2_subdev
*sd
,
304 struct v4l2_subdev_pad_config
*cfg
,
305 struct v4l2_subdev_mbus_code_enum
*code
)
307 if (code
->pad
|| code
->index
>= ARRAY_SIZE(adv7175_codes
))
310 code
->code
= adv7175_codes
[code
->index
];
314 static int adv7175_get_fmt(struct v4l2_subdev
*sd
,
315 struct v4l2_subdev_pad_config
*cfg
,
316 struct v4l2_subdev_format
*format
)
318 struct v4l2_mbus_framefmt
*mf
= &format
->format
;
319 u8 val
= adv7175_read(sd
, 0x7);
324 if ((val
& 0x40) == (1 << 6))
325 mf
->code
= MEDIA_BUS_FMT_UYVY8_1X16
;
327 mf
->code
= MEDIA_BUS_FMT_UYVY8_2X8
;
329 mf
->colorspace
= V4L2_COLORSPACE_SMPTE170M
;
332 mf
->field
= V4L2_FIELD_ANY
;
337 static int adv7175_set_fmt(struct v4l2_subdev
*sd
,
338 struct v4l2_subdev_pad_config
*cfg
,
339 struct v4l2_subdev_format
*format
)
341 struct v4l2_mbus_framefmt
*mf
= &format
->format
;
342 u8 val
= adv7175_read(sd
, 0x7);
349 case MEDIA_BUS_FMT_UYVY8_2X8
:
353 case MEDIA_BUS_FMT_UYVY8_1X16
:
358 v4l2_dbg(1, debug
, sd
,
359 "illegal v4l2_mbus_framefmt code: %d\n", mf
->code
);
363 if (format
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
)
364 ret
= adv7175_write(sd
, 0x7, val
);
369 static int adv7175_s_power(struct v4l2_subdev
*sd
, int on
)
372 adv7175_write(sd
, 0x01, 0x00);
374 adv7175_write(sd
, 0x01, 0x78);
379 /* ----------------------------------------------------------------------- */
381 static const struct v4l2_subdev_core_ops adv7175_core_ops
= {
382 .init
= adv7175_init
,
383 .s_power
= adv7175_s_power
,
386 static const struct v4l2_subdev_video_ops adv7175_video_ops
= {
387 .s_std_output
= adv7175_s_std_output
,
388 .s_routing
= adv7175_s_routing
,
391 static const struct v4l2_subdev_pad_ops adv7175_pad_ops
= {
392 .enum_mbus_code
= adv7175_enum_mbus_code
,
393 .get_fmt
= adv7175_get_fmt
,
394 .set_fmt
= adv7175_set_fmt
,
397 static const struct v4l2_subdev_ops adv7175_ops
= {
398 .core
= &adv7175_core_ops
,
399 .video
= &adv7175_video_ops
,
400 .pad
= &adv7175_pad_ops
,
403 /* ----------------------------------------------------------------------- */
405 static int adv7175_probe(struct i2c_client
*client
,
406 const struct i2c_device_id
*id
)
409 struct adv7175
*encoder
;
410 struct v4l2_subdev
*sd
;
412 /* Check if the adapter supports the needed features */
413 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
416 v4l_info(client
, "chip found @ 0x%x (%s)\n",
417 client
->addr
<< 1, client
->adapter
->name
);
419 encoder
= devm_kzalloc(&client
->dev
, sizeof(*encoder
), GFP_KERNEL
);
423 v4l2_i2c_subdev_init(sd
, client
, &adv7175_ops
);
424 encoder
->norm
= V4L2_STD_NTSC
;
427 i
= adv7175_write_block(sd
, init_common
, sizeof(init_common
));
429 i
= adv7175_write(sd
, 0x07, TR0MODE
| TR0RST
);
430 i
= adv7175_write(sd
, 0x07, TR0MODE
);
431 i
= adv7175_read(sd
, 0x12);
432 v4l2_dbg(1, debug
, sd
, "revision %d\n", i
& 1);
435 v4l2_dbg(1, debug
, sd
, "init error 0x%x\n", i
);
439 static int adv7175_remove(struct i2c_client
*client
)
441 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
443 v4l2_device_unregister_subdev(sd
);
447 /* ----------------------------------------------------------------------- */
449 static const struct i2c_device_id adv7175_id
[] = {
454 MODULE_DEVICE_TABLE(i2c
, adv7175_id
);
456 static struct i2c_driver adv7175_driver
= {
460 .probe
= adv7175_probe
,
461 .remove
= adv7175_remove
,
462 .id_table
= adv7175_id
,
465 module_i2c_driver(adv7175_driver
);