Commit | Line | Data |
---|---|---|
d56410e0 | 1 | /* |
1da177e4 LT |
2 | * adv7175 - adv7175a video encoder driver version 0.0.3 |
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. | |
8 | * | |
9 | * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net> | |
10 | * - moved over to linux>=2.4.x i2c protocol (9/9/2002) | |
11 | * | |
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. | |
16 | * | |
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. | |
21 | * | |
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. | |
25 | */ | |
26 | ||
27 | #include <linux/module.h> | |
18f3fa1e | 28 | #include <linux/types.h> |
5a0e3ad6 | 29 | #include <linux/slab.h> |
bba0d982 | 30 | #include <linux/ioctl.h> |
18f3fa1e | 31 | #include <asm/uaccess.h> |
bba0d982 | 32 | #include <linux/i2c.h> |
35631dcc HV |
33 | #include <linux/videodev2.h> |
34 | #include <media/v4l2-device.h> | |
1da177e4 LT |
35 | |
36 | MODULE_DESCRIPTION("Analog Devices ADV7175 video encoder driver"); | |
37 | MODULE_AUTHOR("Dave Perks"); | |
38 | MODULE_LICENSE("GPL"); | |
39 | ||
35631dcc HV |
40 | #define I2C_ADV7175 0xd4 |
41 | #define I2C_ADV7176 0x54 | |
42 | ||
35631dcc | 43 | |
ff699e6b | 44 | static int debug; |
1da177e4 LT |
45 | module_param(debug, int, 0); |
46 | MODULE_PARM_DESC(debug, "Debug level (0-1)"); | |
47 | ||
1da177e4 LT |
48 | /* ----------------------------------------------------------------------- */ |
49 | ||
50 | struct adv7175 { | |
35631dcc | 51 | struct v4l2_subdev sd; |
107063c6 | 52 | v4l2_std_id norm; |
1da177e4 | 53 | int input; |
1da177e4 LT |
54 | }; |
55 | ||
35631dcc HV |
56 | static inline struct adv7175 *to_adv7175(struct v4l2_subdev *sd) |
57 | { | |
58 | return container_of(sd, struct adv7175, sd); | |
59 | } | |
1da177e4 | 60 | |
1da177e4 | 61 | static char *inputs[] = { "pass_through", "play_back", "color_bar" }; |
1da177e4 | 62 | |
f5fe58fd BB |
63 | static u32 adv7175_codes[] = { |
64 | MEDIA_BUS_FMT_UYVY8_2X8, | |
65 | MEDIA_BUS_FMT_UYVY8_1X16, | |
f1a84c9b CG |
66 | }; |
67 | ||
1da177e4 LT |
68 | /* ----------------------------------------------------------------------- */ |
69 | ||
35631dcc | 70 | static inline int adv7175_write(struct v4l2_subdev *sd, u8 reg, u8 value) |
1da177e4 | 71 | { |
35631dcc HV |
72 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
73 | ||
1da177e4 LT |
74 | return i2c_smbus_write_byte_data(client, reg, value); |
75 | } | |
76 | ||
35631dcc | 77 | static inline int adv7175_read(struct v4l2_subdev *sd, u8 reg) |
1da177e4 | 78 | { |
35631dcc HV |
79 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
80 | ||
1da177e4 LT |
81 | return i2c_smbus_read_byte_data(client, reg); |
82 | } | |
83 | ||
35631dcc | 84 | static int adv7175_write_block(struct v4l2_subdev *sd, |
bba0d982 | 85 | const u8 *data, unsigned int len) |
1da177e4 | 86 | { |
35631dcc | 87 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
1da177e4 LT |
88 | int ret = -1; |
89 | u8 reg; | |
90 | ||
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 */ | |
1da177e4 | 95 | u8 block_data[32]; |
9aa45e34 | 96 | int block_len; |
1da177e4 | 97 | |
1da177e4 | 98 | while (len >= 2) { |
9aa45e34 JD |
99 | block_len = 0; |
100 | block_data[block_len++] = reg = data[0]; | |
1da177e4 | 101 | do { |
9aa45e34 | 102 | block_data[block_len++] = data[1]; |
2467a670 | 103 | reg++; |
1da177e4 LT |
104 | len -= 2; |
105 | data += 2; | |
bba0d982 HV |
106 | } while (len >= 2 && data[0] == reg && block_len < 32); |
107 | ret = i2c_master_send(client, block_data, block_len); | |
108 | if (ret < 0) | |
1da177e4 LT |
109 | break; |
110 | } | |
111 | } else { | |
112 | /* do some slow I2C emulation kind of thing */ | |
113 | while (len >= 2) { | |
114 | reg = *data++; | |
35631dcc | 115 | ret = adv7175_write(sd, reg, *data++); |
bba0d982 | 116 | if (ret < 0) |
1da177e4 LT |
117 | break; |
118 | len -= 2; | |
119 | } | |
120 | } | |
121 | ||
122 | return ret; | |
123 | } | |
124 | ||
35631dcc | 125 | static void set_subcarrier_freq(struct v4l2_subdev *sd, int pass_through) |
1da177e4 LT |
126 | { |
127 | /* for some reason pass_through NTSC needs | |
128 | * a different sub-carrier freq to remain stable. */ | |
bba0d982 | 129 | if (pass_through) |
35631dcc | 130 | adv7175_write(sd, 0x02, 0x00); |
1da177e4 | 131 | else |
35631dcc | 132 | adv7175_write(sd, 0x02, 0x55); |
1da177e4 | 133 | |
35631dcc HV |
134 | adv7175_write(sd, 0x03, 0x55); |
135 | adv7175_write(sd, 0x04, 0x55); | |
136 | adv7175_write(sd, 0x05, 0x25); | |
1da177e4 LT |
137 | } |
138 | ||
1da177e4 | 139 | /* ----------------------------------------------------------------------- */ |
bba0d982 | 140 | /* Output filter: S-Video Composite */ |
1da177e4 | 141 | |
bba0d982 HV |
142 | #define MR050 0x11 /* 0x09 */ |
143 | #define MR060 0x14 /* 0x0c */ | |
1da177e4 | 144 | |
bba0d982 | 145 | /* ----------------------------------------------------------------------- */ |
1da177e4 LT |
146 | |
147 | #define TR0MODE 0x46 | |
148 | #define TR0RST 0x80 | |
149 | ||
150 | #define TR1CAPT 0x80 | |
151 | #define TR1PLAY 0x00 | |
152 | ||
153 | static const unsigned char init_common[] = { | |
154 | ||
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 */ | |
162 | ||
163 | 0x07, TR0MODE, /* TR0, 16bit */ | |
164 | 0x08, 0x21, /* */ | |
165 | 0x09, 0x00, /* */ | |
166 | 0x0a, 0x00, /* */ | |
167 | 0x0b, 0x00, /* */ | |
168 | 0x0c, TR1CAPT, /* TR1 */ | |
169 | 0x0d, 0x4f, /* MR2 */ | |
170 | 0x0e, 0x00, /* */ | |
171 | 0x0f, 0x00, /* */ | |
172 | 0x10, 0x00, /* */ | |
173 | 0x11, 0x00, /* */ | |
174 | }; | |
175 | ||
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 */ | |
184 | }; | |
185 | ||
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 */ | |
194 | }; | |
195 | ||
35631dcc HV |
196 | static int adv7175_init(struct v4l2_subdev *sd, u32 val) |
197 | { | |
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); | |
202 | return 0; | |
203 | } | |
204 | ||
205 | static int adv7175_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std) | |
1da177e4 | 206 | { |
35631dcc HV |
207 | struct adv7175 *encoder = to_adv7175(sd); |
208 | ||
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 | |
227 | * quency) */ | |
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); | |
233 | } else { | |
cc5cef8e HV |
234 | v4l2_dbg(1, debug, sd, "illegal norm: %llx\n", |
235 | (unsigned long long)std); | |
35631dcc HV |
236 | return -EINVAL; |
237 | } | |
cc5cef8e | 238 | v4l2_dbg(1, debug, sd, "switched to %llx\n", (unsigned long long)std); |
35631dcc HV |
239 | encoder->norm = std; |
240 | return 0; | |
241 | } | |
242 | ||
5325b427 HV |
243 | static int adv7175_s_routing(struct v4l2_subdev *sd, |
244 | u32 input, u32 output, u32 config) | |
35631dcc HV |
245 | { |
246 | struct adv7175 *encoder = to_adv7175(sd); | |
247 | ||
5325b427 HV |
248 | /* RJ: input = 0: input is from decoder |
249 | input = 1: input is from ZR36060 | |
250 | input = 2: color bar */ | |
35631dcc | 251 | |
5325b427 | 252 | switch (input) { |
35631dcc HV |
253 | case 0: |
254 | adv7175_write(sd, 0x01, 0x00); | |
255 | ||
256 | if (encoder->norm & V4L2_STD_NTSC) | |
257 | set_subcarrier_freq(sd, 1); | |
258 | ||
259 | adv7175_write(sd, 0x0c, TR1CAPT); /* TR1 */ | |
260 | if (encoder->norm & V4L2_STD_SECAM) | |
261 | adv7175_write(sd, 0x0d, 0x49); /* Disable genlock */ | |
262 | else | |
263 | adv7175_write(sd, 0x0d, 0x4f); /* Enable genlock */ | |
264 | adv7175_write(sd, 0x07, TR0MODE | TR0RST); | |
265 | adv7175_write(sd, 0x07, TR0MODE); | |
266 | /*udelay(10);*/ | |
d56410e0 | 267 | break; |
1da177e4 | 268 | |
35631dcc HV |
269 | case 1: |
270 | adv7175_write(sd, 0x01, 0x00); | |
271 | ||
272 | if (encoder->norm & V4L2_STD_NTSC) | |
273 | set_subcarrier_freq(sd, 0); | |
274 | ||
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); | |
279 | /* udelay(10); */ | |
1da177e4 LT |
280 | break; |
281 | ||
35631dcc HV |
282 | case 2: |
283 | adv7175_write(sd, 0x01, 0x80); | |
284 | ||
285 | if (encoder->norm & V4L2_STD_NTSC) | |
286 | set_subcarrier_freq(sd, 0); | |
287 | ||
288 | adv7175_write(sd, 0x0d, 0x49); | |
289 | adv7175_write(sd, 0x07, TR0MODE | TR0RST); | |
290 | adv7175_write(sd, 0x07, TR0MODE); | |
291 | /* udelay(10); */ | |
1da177e4 LT |
292 | break; |
293 | ||
1da177e4 | 294 | default: |
5325b427 | 295 | v4l2_dbg(1, debug, sd, "illegal input: %d\n", input); |
1da177e4 LT |
296 | return -EINVAL; |
297 | } | |
5325b427 HV |
298 | v4l2_dbg(1, debug, sd, "switched to %s\n", inputs[input]); |
299 | encoder->input = input; | |
1da177e4 LT |
300 | return 0; |
301 | } | |
302 | ||
f1a84c9b | 303 | static int adv7175_enum_fmt(struct v4l2_subdev *sd, unsigned int index, |
f5fe58fd | 304 | u32 *code) |
f1a84c9b CG |
305 | { |
306 | if (index >= ARRAY_SIZE(adv7175_codes)) | |
307 | return -EINVAL; | |
308 | ||
309 | *code = adv7175_codes[index]; | |
310 | return 0; | |
311 | } | |
312 | ||
313 | static int adv7175_g_fmt(struct v4l2_subdev *sd, | |
314 | struct v4l2_mbus_framefmt *mf) | |
315 | { | |
316 | u8 val = adv7175_read(sd, 0x7); | |
317 | ||
318 | if ((val & 0x40) == (1 << 6)) | |
f5fe58fd | 319 | mf->code = MEDIA_BUS_FMT_UYVY8_1X16; |
f1a84c9b | 320 | else |
f5fe58fd | 321 | mf->code = MEDIA_BUS_FMT_UYVY8_2X8; |
f1a84c9b CG |
322 | |
323 | mf->colorspace = V4L2_COLORSPACE_SMPTE170M; | |
324 | mf->width = 0; | |
325 | mf->height = 0; | |
326 | mf->field = V4L2_FIELD_ANY; | |
327 | ||
328 | return 0; | |
329 | } | |
330 | ||
331 | static int adv7175_s_fmt(struct v4l2_subdev *sd, | |
332 | struct v4l2_mbus_framefmt *mf) | |
333 | { | |
334 | u8 val = adv7175_read(sd, 0x7); | |
335 | int ret; | |
336 | ||
337 | switch (mf->code) { | |
f5fe58fd | 338 | case MEDIA_BUS_FMT_UYVY8_2X8: |
f1a84c9b CG |
339 | val &= ~0x40; |
340 | break; | |
341 | ||
f5fe58fd | 342 | case MEDIA_BUS_FMT_UYVY8_1X16: |
f1a84c9b CG |
343 | val |= 0x40; |
344 | break; | |
345 | ||
346 | default: | |
347 | v4l2_dbg(1, debug, sd, | |
348 | "illegal v4l2_mbus_framefmt code: %d\n", mf->code); | |
349 | return -EINVAL; | |
350 | } | |
351 | ||
352 | ret = adv7175_write(sd, 0x7, val); | |
353 | ||
354 | return ret; | |
355 | } | |
356 | ||
b7eccc46 CG |
357 | static int adv7175_s_power(struct v4l2_subdev *sd, int on) |
358 | { | |
359 | if (on) | |
360 | adv7175_write(sd, 0x01, 0x00); | |
361 | else | |
362 | adv7175_write(sd, 0x01, 0x78); | |
363 | ||
364 | return 0; | |
365 | } | |
366 | ||
1da177e4 LT |
367 | /* ----------------------------------------------------------------------- */ |
368 | ||
35631dcc | 369 | static const struct v4l2_subdev_core_ops adv7175_core_ops = { |
35631dcc | 370 | .init = adv7175_init, |
b7eccc46 | 371 | .s_power = adv7175_s_power, |
1da177e4 | 372 | }; |
1da177e4 | 373 | |
35631dcc HV |
374 | static const struct v4l2_subdev_video_ops adv7175_video_ops = { |
375 | .s_std_output = adv7175_s_std_output, | |
376 | .s_routing = adv7175_s_routing, | |
f1a84c9b CG |
377 | .s_mbus_fmt = adv7175_s_fmt, |
378 | .g_mbus_fmt = adv7175_g_fmt, | |
379 | .enum_mbus_fmt = adv7175_enum_fmt, | |
35631dcc HV |
380 | }; |
381 | ||
382 | static const struct v4l2_subdev_ops adv7175_ops = { | |
383 | .core = &adv7175_core_ops, | |
384 | .video = &adv7175_video_ops, | |
385 | }; | |
386 | ||
387 | /* ----------------------------------------------------------------------- */ | |
1da177e4 | 388 | |
bba0d982 HV |
389 | static int adv7175_probe(struct i2c_client *client, |
390 | const struct i2c_device_id *id) | |
1da177e4 LT |
391 | { |
392 | int i; | |
1da177e4 | 393 | struct adv7175 *encoder; |
35631dcc | 394 | struct v4l2_subdev *sd; |
1da177e4 LT |
395 | |
396 | /* Check if the adapter supports the needed features */ | |
bba0d982 HV |
397 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
398 | return -ENODEV; | |
1da177e4 | 399 | |
bba0d982 HV |
400 | v4l_info(client, "chip found @ 0x%x (%s)\n", |
401 | client->addr << 1, client->adapter->name); | |
1da177e4 | 402 | |
c02b211d | 403 | encoder = devm_kzalloc(&client->dev, sizeof(*encoder), GFP_KERNEL); |
bba0d982 | 404 | if (encoder == NULL) |
1da177e4 | 405 | return -ENOMEM; |
35631dcc HV |
406 | sd = &encoder->sd; |
407 | v4l2_i2c_subdev_init(sd, client, &adv7175_ops); | |
107063c6 | 408 | encoder->norm = V4L2_STD_NTSC; |
1da177e4 | 409 | encoder->input = 0; |
1da177e4 | 410 | |
35631dcc | 411 | i = adv7175_write_block(sd, init_common, sizeof(init_common)); |
1da177e4 | 412 | if (i >= 0) { |
35631dcc HV |
413 | i = adv7175_write(sd, 0x07, TR0MODE | TR0RST); |
414 | i = adv7175_write(sd, 0x07, TR0MODE); | |
415 | i = adv7175_read(sd, 0x12); | |
416 | v4l2_dbg(1, debug, sd, "revision %d\n", i & 1); | |
1da177e4 | 417 | } |
bba0d982 | 418 | if (i < 0) |
35631dcc | 419 | v4l2_dbg(1, debug, sd, "init error 0x%x\n", i); |
1da177e4 LT |
420 | return 0; |
421 | } | |
422 | ||
bba0d982 | 423 | static int adv7175_remove(struct i2c_client *client) |
1da177e4 | 424 | { |
35631dcc HV |
425 | struct v4l2_subdev *sd = i2c_get_clientdata(client); |
426 | ||
427 | v4l2_device_unregister_subdev(sd); | |
1da177e4 LT |
428 | return 0; |
429 | } | |
430 | ||
431 | /* ----------------------------------------------------------------------- */ | |
432 | ||
bba0d982 HV |
433 | static const struct i2c_device_id adv7175_id[] = { |
434 | { "adv7175", 0 }, | |
435 | { "adv7176", 0 }, | |
436 | { } | |
437 | }; | |
438 | MODULE_DEVICE_TABLE(i2c, adv7175_id); | |
1da177e4 | 439 | |
dc71443f HV |
440 | static struct i2c_driver adv7175_driver = { |
441 | .driver = { | |
442 | .owner = THIS_MODULE, | |
443 | .name = "adv7175", | |
444 | }, | |
445 | .probe = adv7175_probe, | |
446 | .remove = adv7175_remove, | |
447 | .id_table = adv7175_id, | |
1da177e4 | 448 | }; |
dc71443f | 449 | |
c6e8d86f | 450 | module_i2c_driver(adv7175_driver); |