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> | |
35 | #include <media/v4l2-chip-ident.h> | |
1da177e4 LT |
36 | |
37 | MODULE_DESCRIPTION("Analog Devices ADV7175 video encoder driver"); | |
38 | MODULE_AUTHOR("Dave Perks"); | |
39 | MODULE_LICENSE("GPL"); | |
40 | ||
35631dcc HV |
41 | #define I2C_ADV7175 0xd4 |
42 | #define I2C_ADV7176 0x54 | |
43 | ||
35631dcc | 44 | |
ff699e6b | 45 | static int debug; |
1da177e4 LT |
46 | module_param(debug, int, 0); |
47 | MODULE_PARM_DESC(debug, "Debug level (0-1)"); | |
48 | ||
1da177e4 LT |
49 | /* ----------------------------------------------------------------------- */ |
50 | ||
51 | struct adv7175 { | |
35631dcc | 52 | struct v4l2_subdev sd; |
107063c6 | 53 | v4l2_std_id norm; |
1da177e4 | 54 | int input; |
1da177e4 LT |
55 | }; |
56 | ||
35631dcc HV |
57 | static inline struct adv7175 *to_adv7175(struct v4l2_subdev *sd) |
58 | { | |
59 | return container_of(sd, struct adv7175, sd); | |
60 | } | |
1da177e4 | 61 | |
1da177e4 | 62 | static char *inputs[] = { "pass_through", "play_back", "color_bar" }; |
1da177e4 LT |
63 | |
64 | /* ----------------------------------------------------------------------- */ | |
65 | ||
35631dcc | 66 | static inline int adv7175_write(struct v4l2_subdev *sd, u8 reg, u8 value) |
1da177e4 | 67 | { |
35631dcc HV |
68 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
69 | ||
1da177e4 LT |
70 | return i2c_smbus_write_byte_data(client, reg, value); |
71 | } | |
72 | ||
35631dcc | 73 | static inline int adv7175_read(struct v4l2_subdev *sd, u8 reg) |
1da177e4 | 74 | { |
35631dcc HV |
75 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
76 | ||
1da177e4 LT |
77 | return i2c_smbus_read_byte_data(client, reg); |
78 | } | |
79 | ||
35631dcc | 80 | static int adv7175_write_block(struct v4l2_subdev *sd, |
bba0d982 | 81 | const u8 *data, unsigned int len) |
1da177e4 | 82 | { |
35631dcc | 83 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
1da177e4 LT |
84 | int ret = -1; |
85 | u8 reg; | |
86 | ||
87 | /* the adv7175 has an autoincrement function, use it if | |
88 | * the adapter understands raw I2C */ | |
89 | if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { | |
90 | /* do raw I2C, not smbus compatible */ | |
1da177e4 | 91 | u8 block_data[32]; |
9aa45e34 | 92 | int block_len; |
1da177e4 | 93 | |
1da177e4 | 94 | while (len >= 2) { |
9aa45e34 JD |
95 | block_len = 0; |
96 | block_data[block_len++] = reg = data[0]; | |
1da177e4 | 97 | do { |
9aa45e34 | 98 | block_data[block_len++] = data[1]; |
2467a670 | 99 | reg++; |
1da177e4 LT |
100 | len -= 2; |
101 | data += 2; | |
bba0d982 HV |
102 | } while (len >= 2 && data[0] == reg && block_len < 32); |
103 | ret = i2c_master_send(client, block_data, block_len); | |
104 | if (ret < 0) | |
1da177e4 LT |
105 | break; |
106 | } | |
107 | } else { | |
108 | /* do some slow I2C emulation kind of thing */ | |
109 | while (len >= 2) { | |
110 | reg = *data++; | |
35631dcc | 111 | ret = adv7175_write(sd, reg, *data++); |
bba0d982 | 112 | if (ret < 0) |
1da177e4 LT |
113 | break; |
114 | len -= 2; | |
115 | } | |
116 | } | |
117 | ||
118 | return ret; | |
119 | } | |
120 | ||
35631dcc | 121 | static void set_subcarrier_freq(struct v4l2_subdev *sd, int pass_through) |
1da177e4 LT |
122 | { |
123 | /* for some reason pass_through NTSC needs | |
124 | * a different sub-carrier freq to remain stable. */ | |
bba0d982 | 125 | if (pass_through) |
35631dcc | 126 | adv7175_write(sd, 0x02, 0x00); |
1da177e4 | 127 | else |
35631dcc | 128 | adv7175_write(sd, 0x02, 0x55); |
1da177e4 | 129 | |
35631dcc HV |
130 | adv7175_write(sd, 0x03, 0x55); |
131 | adv7175_write(sd, 0x04, 0x55); | |
132 | adv7175_write(sd, 0x05, 0x25); | |
1da177e4 LT |
133 | } |
134 | ||
1da177e4 | 135 | /* ----------------------------------------------------------------------- */ |
bba0d982 | 136 | /* Output filter: S-Video Composite */ |
1da177e4 | 137 | |
bba0d982 HV |
138 | #define MR050 0x11 /* 0x09 */ |
139 | #define MR060 0x14 /* 0x0c */ | |
1da177e4 | 140 | |
bba0d982 | 141 | /* ----------------------------------------------------------------------- */ |
1da177e4 LT |
142 | |
143 | #define TR0MODE 0x46 | |
144 | #define TR0RST 0x80 | |
145 | ||
146 | #define TR1CAPT 0x80 | |
147 | #define TR1PLAY 0x00 | |
148 | ||
149 | static const unsigned char init_common[] = { | |
150 | ||
151 | 0x00, MR050, /* MR0, PAL enabled */ | |
152 | 0x01, 0x00, /* MR1 */ | |
153 | 0x02, 0x0c, /* subc. freq. */ | |
154 | 0x03, 0x8c, /* subc. freq. */ | |
155 | 0x04, 0x79, /* subc. freq. */ | |
156 | 0x05, 0x26, /* subc. freq. */ | |
157 | 0x06, 0x40, /* subc. phase */ | |
158 | ||
159 | 0x07, TR0MODE, /* TR0, 16bit */ | |
160 | 0x08, 0x21, /* */ | |
161 | 0x09, 0x00, /* */ | |
162 | 0x0a, 0x00, /* */ | |
163 | 0x0b, 0x00, /* */ | |
164 | 0x0c, TR1CAPT, /* TR1 */ | |
165 | 0x0d, 0x4f, /* MR2 */ | |
166 | 0x0e, 0x00, /* */ | |
167 | 0x0f, 0x00, /* */ | |
168 | 0x10, 0x00, /* */ | |
169 | 0x11, 0x00, /* */ | |
170 | }; | |
171 | ||
172 | static const unsigned char init_pal[] = { | |
173 | 0x00, MR050, /* MR0, PAL enabled */ | |
174 | 0x01, 0x00, /* MR1 */ | |
175 | 0x02, 0x0c, /* subc. freq. */ | |
176 | 0x03, 0x8c, /* subc. freq. */ | |
177 | 0x04, 0x79, /* subc. freq. */ | |
178 | 0x05, 0x26, /* subc. freq. */ | |
179 | 0x06, 0x40, /* subc. phase */ | |
180 | }; | |
181 | ||
182 | static const unsigned char init_ntsc[] = { | |
183 | 0x00, MR060, /* MR0, NTSC enabled */ | |
184 | 0x01, 0x00, /* MR1 */ | |
185 | 0x02, 0x55, /* subc. freq. */ | |
186 | 0x03, 0x55, /* subc. freq. */ | |
187 | 0x04, 0x55, /* subc. freq. */ | |
188 | 0x05, 0x25, /* subc. freq. */ | |
189 | 0x06, 0x1a, /* subc. phase */ | |
190 | }; | |
191 | ||
35631dcc HV |
192 | static int adv7175_init(struct v4l2_subdev *sd, u32 val) |
193 | { | |
194 | /* This is just for testing!!! */ | |
195 | adv7175_write_block(sd, init_common, sizeof(init_common)); | |
196 | adv7175_write(sd, 0x07, TR0MODE | TR0RST); | |
197 | adv7175_write(sd, 0x07, TR0MODE); | |
198 | return 0; | |
199 | } | |
200 | ||
201 | static int adv7175_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std) | |
1da177e4 | 202 | { |
35631dcc HV |
203 | struct adv7175 *encoder = to_adv7175(sd); |
204 | ||
205 | if (std & V4L2_STD_NTSC) { | |
206 | adv7175_write_block(sd, init_ntsc, sizeof(init_ntsc)); | |
207 | if (encoder->input == 0) | |
208 | adv7175_write(sd, 0x0d, 0x4f); /* Enable genlock */ | |
209 | adv7175_write(sd, 0x07, TR0MODE | TR0RST); | |
210 | adv7175_write(sd, 0x07, TR0MODE); | |
211 | } else if (std & V4L2_STD_PAL) { | |
212 | adv7175_write_block(sd, init_pal, sizeof(init_pal)); | |
213 | if (encoder->input == 0) | |
214 | adv7175_write(sd, 0x0d, 0x4f); /* Enable genlock */ | |
215 | adv7175_write(sd, 0x07, TR0MODE | TR0RST); | |
216 | adv7175_write(sd, 0x07, TR0MODE); | |
217 | } else if (std & V4L2_STD_SECAM) { | |
218 | /* This is an attempt to convert | |
219 | * SECAM->PAL (typically it does not work | |
220 | * due to genlock: when decoder is in SECAM | |
221 | * and encoder in in PAL the subcarrier can | |
222 | * not be syncronized with horizontal | |
223 | * quency) */ | |
224 | adv7175_write_block(sd, init_pal, sizeof(init_pal)); | |
225 | if (encoder->input == 0) | |
226 | adv7175_write(sd, 0x0d, 0x49); /* Disable genlock */ | |
227 | adv7175_write(sd, 0x07, TR0MODE | TR0RST); | |
228 | adv7175_write(sd, 0x07, TR0MODE); | |
229 | } else { | |
cc5cef8e HV |
230 | v4l2_dbg(1, debug, sd, "illegal norm: %llx\n", |
231 | (unsigned long long)std); | |
35631dcc HV |
232 | return -EINVAL; |
233 | } | |
cc5cef8e | 234 | v4l2_dbg(1, debug, sd, "switched to %llx\n", (unsigned long long)std); |
35631dcc HV |
235 | encoder->norm = std; |
236 | return 0; | |
237 | } | |
238 | ||
5325b427 HV |
239 | static int adv7175_s_routing(struct v4l2_subdev *sd, |
240 | u32 input, u32 output, u32 config) | |
35631dcc HV |
241 | { |
242 | struct adv7175 *encoder = to_adv7175(sd); | |
243 | ||
5325b427 HV |
244 | /* RJ: input = 0: input is from decoder |
245 | input = 1: input is from ZR36060 | |
246 | input = 2: color bar */ | |
35631dcc | 247 | |
5325b427 | 248 | switch (input) { |
35631dcc HV |
249 | case 0: |
250 | adv7175_write(sd, 0x01, 0x00); | |
251 | ||
252 | if (encoder->norm & V4L2_STD_NTSC) | |
253 | set_subcarrier_freq(sd, 1); | |
254 | ||
255 | adv7175_write(sd, 0x0c, TR1CAPT); /* TR1 */ | |
256 | if (encoder->norm & V4L2_STD_SECAM) | |
257 | adv7175_write(sd, 0x0d, 0x49); /* Disable genlock */ | |
258 | else | |
259 | adv7175_write(sd, 0x0d, 0x4f); /* Enable genlock */ | |
260 | adv7175_write(sd, 0x07, TR0MODE | TR0RST); | |
261 | adv7175_write(sd, 0x07, TR0MODE); | |
262 | /*udelay(10);*/ | |
d56410e0 | 263 | break; |
1da177e4 | 264 | |
35631dcc HV |
265 | case 1: |
266 | adv7175_write(sd, 0x01, 0x00); | |
267 | ||
268 | if (encoder->norm & V4L2_STD_NTSC) | |
269 | set_subcarrier_freq(sd, 0); | |
270 | ||
271 | adv7175_write(sd, 0x0c, TR1PLAY); /* TR1 */ | |
272 | adv7175_write(sd, 0x0d, 0x49); | |
273 | adv7175_write(sd, 0x07, TR0MODE | TR0RST); | |
274 | adv7175_write(sd, 0x07, TR0MODE); | |
275 | /* udelay(10); */ | |
1da177e4 LT |
276 | break; |
277 | ||
35631dcc HV |
278 | case 2: |
279 | adv7175_write(sd, 0x01, 0x80); | |
280 | ||
281 | if (encoder->norm & V4L2_STD_NTSC) | |
282 | set_subcarrier_freq(sd, 0); | |
283 | ||
284 | adv7175_write(sd, 0x0d, 0x49); | |
285 | adv7175_write(sd, 0x07, TR0MODE | TR0RST); | |
286 | adv7175_write(sd, 0x07, TR0MODE); | |
287 | /* udelay(10); */ | |
1da177e4 LT |
288 | break; |
289 | ||
1da177e4 | 290 | default: |
5325b427 | 291 | v4l2_dbg(1, debug, sd, "illegal input: %d\n", input); |
1da177e4 LT |
292 | return -EINVAL; |
293 | } | |
5325b427 HV |
294 | v4l2_dbg(1, debug, sd, "switched to %s\n", inputs[input]); |
295 | encoder->input = input; | |
1da177e4 LT |
296 | return 0; |
297 | } | |
298 | ||
35631dcc HV |
299 | static int adv7175_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip) |
300 | { | |
301 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
302 | ||
303 | return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_ADV7175, 0); | |
304 | } | |
305 | ||
1da177e4 LT |
306 | /* ----------------------------------------------------------------------- */ |
307 | ||
35631dcc HV |
308 | static const struct v4l2_subdev_core_ops adv7175_core_ops = { |
309 | .g_chip_ident = adv7175_g_chip_ident, | |
310 | .init = adv7175_init, | |
1da177e4 | 311 | }; |
1da177e4 | 312 | |
35631dcc HV |
313 | static const struct v4l2_subdev_video_ops adv7175_video_ops = { |
314 | .s_std_output = adv7175_s_std_output, | |
315 | .s_routing = adv7175_s_routing, | |
316 | }; | |
317 | ||
318 | static const struct v4l2_subdev_ops adv7175_ops = { | |
319 | .core = &adv7175_core_ops, | |
320 | .video = &adv7175_video_ops, | |
321 | }; | |
322 | ||
323 | /* ----------------------------------------------------------------------- */ | |
1da177e4 | 324 | |
bba0d982 HV |
325 | static int adv7175_probe(struct i2c_client *client, |
326 | const struct i2c_device_id *id) | |
1da177e4 LT |
327 | { |
328 | int i; | |
1da177e4 | 329 | struct adv7175 *encoder; |
35631dcc | 330 | struct v4l2_subdev *sd; |
1da177e4 LT |
331 | |
332 | /* Check if the adapter supports the needed features */ | |
bba0d982 HV |
333 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
334 | return -ENODEV; | |
1da177e4 | 335 | |
bba0d982 HV |
336 | v4l_info(client, "chip found @ 0x%x (%s)\n", |
337 | client->addr << 1, client->adapter->name); | |
1da177e4 | 338 | |
7408187d | 339 | encoder = kzalloc(sizeof(struct adv7175), GFP_KERNEL); |
bba0d982 | 340 | if (encoder == NULL) |
1da177e4 | 341 | return -ENOMEM; |
35631dcc HV |
342 | sd = &encoder->sd; |
343 | v4l2_i2c_subdev_init(sd, client, &adv7175_ops); | |
107063c6 | 344 | encoder->norm = V4L2_STD_NTSC; |
1da177e4 | 345 | encoder->input = 0; |
1da177e4 | 346 | |
35631dcc | 347 | i = adv7175_write_block(sd, init_common, sizeof(init_common)); |
1da177e4 | 348 | if (i >= 0) { |
35631dcc HV |
349 | i = adv7175_write(sd, 0x07, TR0MODE | TR0RST); |
350 | i = adv7175_write(sd, 0x07, TR0MODE); | |
351 | i = adv7175_read(sd, 0x12); | |
352 | v4l2_dbg(1, debug, sd, "revision %d\n", i & 1); | |
1da177e4 | 353 | } |
bba0d982 | 354 | if (i < 0) |
35631dcc | 355 | v4l2_dbg(1, debug, sd, "init error 0x%x\n", i); |
1da177e4 LT |
356 | return 0; |
357 | } | |
358 | ||
bba0d982 | 359 | static int adv7175_remove(struct i2c_client *client) |
1da177e4 | 360 | { |
35631dcc HV |
361 | struct v4l2_subdev *sd = i2c_get_clientdata(client); |
362 | ||
363 | v4l2_device_unregister_subdev(sd); | |
364 | kfree(to_adv7175(sd)); | |
1da177e4 LT |
365 | return 0; |
366 | } | |
367 | ||
368 | /* ----------------------------------------------------------------------- */ | |
369 | ||
bba0d982 HV |
370 | static const struct i2c_device_id adv7175_id[] = { |
371 | { "adv7175", 0 }, | |
372 | { "adv7176", 0 }, | |
373 | { } | |
374 | }; | |
375 | MODULE_DEVICE_TABLE(i2c, adv7175_id); | |
1da177e4 | 376 | |
dc71443f HV |
377 | static struct i2c_driver adv7175_driver = { |
378 | .driver = { | |
379 | .owner = THIS_MODULE, | |
380 | .name = "adv7175", | |
381 | }, | |
382 | .probe = adv7175_probe, | |
383 | .remove = adv7175_remove, | |
384 | .id_table = adv7175_id, | |
1da177e4 | 385 | }; |
dc71443f HV |
386 | |
387 | static __init int init_adv7175(void) | |
388 | { | |
389 | return i2c_add_driver(&adv7175_driver); | |
390 | } | |
391 | ||
392 | static __exit void exit_adv7175(void) | |
393 | { | |
394 | i2c_del_driver(&adv7175_driver); | |
395 | } | |
396 | ||
397 | module_init(init_adv7175); | |
398 | module_exit(exit_adv7175); |