Commit | Line | Data |
---|---|---|
d56410e0 | 1 | /* |
1da177e4 LT |
2 | * adv7170 - adv7170, adv7171 video encoder driver version 0.0.1 |
3 | * | |
4 | * Copyright (C) 2002 Maxim Yevtyushkin <max@linuxmedialabs.com> | |
5 | * | |
d56410e0 | 6 | * Based on adv7176 driver by: |
1da177e4 LT |
7 | * |
8 | * Copyright (C) 1998 Dave Perks <dperks@ibm.net> | |
9 | * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net> | |
10 | * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx> | |
11 | * - some corrections for Pinnacle Systems Inc. DC10plus card. | |
12 | * | |
13 | * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net> | |
14 | * - moved over to linux>=2.4.x i2c protocol (1/1/2003) | |
15 | * | |
16 | * This program is free software; you can redistribute it and/or modify | |
17 | * it under the terms of the GNU General Public License as published by | |
18 | * the Free Software Foundation; either version 2 of the License, or | |
19 | * (at your option) any later version. | |
20 | * | |
21 | * This program is distributed in the hope that it will be useful, | |
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
24 | * GNU General Public License for more details. | |
25 | * | |
26 | * You should have received a copy of the GNU General Public License | |
27 | * along with this program; if not, write to the Free Software | |
28 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
29 | */ | |
30 | ||
31 | #include <linux/module.h> | |
18f3fa1e | 32 | #include <linux/types.h> |
5a0e3ad6 | 33 | #include <linux/slab.h> |
b9a21f84 | 34 | #include <linux/ioctl.h> |
18f3fa1e | 35 | #include <asm/uaccess.h> |
b9a21f84 | 36 | #include <linux/i2c.h> |
7d9ef21c HV |
37 | #include <linux/videodev2.h> |
38 | #include <media/v4l2-device.h> | |
1da177e4 LT |
39 | |
40 | MODULE_DESCRIPTION("Analog Devices ADV7170 video encoder driver"); | |
41 | MODULE_AUTHOR("Maxim Yevtyushkin"); | |
42 | MODULE_LICENSE("GPL"); | |
43 | ||
7d9ef21c | 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 adv7170 { | |
7d9ef21c | 52 | struct v4l2_subdev sd; |
1da177e4 LT |
53 | unsigned char reg[128]; |
54 | ||
107063c6 | 55 | v4l2_std_id norm; |
1da177e4 | 56 | int input; |
1da177e4 LT |
57 | }; |
58 | ||
7d9ef21c HV |
59 | static inline struct adv7170 *to_adv7170(struct v4l2_subdev *sd) |
60 | { | |
61 | return container_of(sd, struct adv7170, sd); | |
62 | } | |
63 | ||
1da177e4 | 64 | static char *inputs[] = { "pass_through", "play_back" }; |
1da177e4 | 65 | |
f5fe58fd BB |
66 | static u32 adv7170_codes[] = { |
67 | MEDIA_BUS_FMT_UYVY8_2X8, | |
68 | MEDIA_BUS_FMT_UYVY8_1X16, | |
0d37d350 CG |
69 | }; |
70 | ||
1da177e4 LT |
71 | /* ----------------------------------------------------------------------- */ |
72 | ||
7d9ef21c | 73 | static inline int adv7170_write(struct v4l2_subdev *sd, u8 reg, u8 value) |
1da177e4 | 74 | { |
7d9ef21c HV |
75 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
76 | struct adv7170 *encoder = to_adv7170(sd); | |
1da177e4 LT |
77 | |
78 | encoder->reg[reg] = value; | |
79 | return i2c_smbus_write_byte_data(client, reg, value); | |
80 | } | |
81 | ||
7d9ef21c | 82 | static inline int adv7170_read(struct v4l2_subdev *sd, u8 reg) |
1da177e4 | 83 | { |
7d9ef21c HV |
84 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
85 | ||
1da177e4 LT |
86 | return i2c_smbus_read_byte_data(client, reg); |
87 | } | |
88 | ||
7d9ef21c | 89 | static int adv7170_write_block(struct v4l2_subdev *sd, |
b9a21f84 | 90 | const u8 *data, unsigned int len) |
1da177e4 | 91 | { |
7d9ef21c HV |
92 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
93 | struct adv7170 *encoder = to_adv7170(sd); | |
1da177e4 LT |
94 | int ret = -1; |
95 | u8 reg; | |
96 | ||
97 | /* the adv7170 has an autoincrement function, use it if | |
98 | * the adapter understands raw I2C */ | |
99 | if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { | |
100 | /* do raw I2C, not smbus compatible */ | |
1da177e4 | 101 | u8 block_data[32]; |
9aa45e34 | 102 | int block_len; |
1da177e4 | 103 | |
1da177e4 | 104 | while (len >= 2) { |
9aa45e34 JD |
105 | block_len = 0; |
106 | block_data[block_len++] = reg = data[0]; | |
1da177e4 | 107 | do { |
9aa45e34 | 108 | block_data[block_len++] = |
1da177e4 LT |
109 | encoder->reg[reg++] = data[1]; |
110 | len -= 2; | |
111 | data += 2; | |
b9a21f84 HV |
112 | } while (len >= 2 && data[0] == reg && block_len < 32); |
113 | ret = i2c_master_send(client, block_data, block_len); | |
114 | if (ret < 0) | |
1da177e4 LT |
115 | break; |
116 | } | |
117 | } else { | |
118 | /* do some slow I2C emulation kind of thing */ | |
119 | while (len >= 2) { | |
120 | reg = *data++; | |
7d9ef21c | 121 | ret = adv7170_write(sd, reg, *data++); |
b9a21f84 | 122 | if (ret < 0) |
1da177e4 LT |
123 | break; |
124 | len -= 2; | |
125 | } | |
126 | } | |
1da177e4 LT |
127 | return ret; |
128 | } | |
129 | ||
130 | /* ----------------------------------------------------------------------- */ | |
1da177e4 LT |
131 | |
132 | #define TR0MODE 0x4c | |
133 | #define TR0RST 0x80 | |
134 | ||
135 | #define TR1CAPT 0x00 | |
136 | #define TR1PLAY 0x00 | |
137 | ||
1da177e4 | 138 | static const unsigned char init_NTSC[] = { |
7d9ef21c HV |
139 | 0x00, 0x10, /* MR0 */ |
140 | 0x01, 0x20, /* MR1 */ | |
141 | 0x02, 0x0e, /* MR2 RTC control: bits 2 and 1 */ | |
142 | 0x03, 0x80, /* MR3 */ | |
143 | 0x04, 0x30, /* MR4 */ | |
144 | 0x05, 0x00, /* Reserved */ | |
145 | 0x06, 0x00, /* Reserved */ | |
146 | 0x07, TR0MODE, /* TM0 */ | |
147 | 0x08, TR1CAPT, /* TM1 */ | |
148 | 0x09, 0x16, /* Fsc0 */ | |
149 | 0x0a, 0x7c, /* Fsc1 */ | |
150 | 0x0b, 0xf0, /* Fsc2 */ | |
151 | 0x0c, 0x21, /* Fsc3 */ | |
152 | 0x0d, 0x00, /* Subcarrier Phase */ | |
153 | 0x0e, 0x00, /* Closed Capt. Ext 0 */ | |
154 | 0x0f, 0x00, /* Closed Capt. Ext 1 */ | |
155 | 0x10, 0x00, /* Closed Capt. 0 */ | |
156 | 0x11, 0x00, /* Closed Capt. 1 */ | |
157 | 0x12, 0x00, /* Pedestal Ctl 0 */ | |
158 | 0x13, 0x00, /* Pedestal Ctl 1 */ | |
159 | 0x14, 0x00, /* Pedestal Ctl 2 */ | |
160 | 0x15, 0x00, /* Pedestal Ctl 3 */ | |
161 | 0x16, 0x00, /* CGMS_WSS_0 */ | |
162 | 0x17, 0x00, /* CGMS_WSS_1 */ | |
163 | 0x18, 0x00, /* CGMS_WSS_2 */ | |
164 | 0x19, 0x00, /* Teletext Ctl */ | |
1da177e4 LT |
165 | }; |
166 | ||
167 | static const unsigned char init_PAL[] = { | |
7d9ef21c HV |
168 | 0x00, 0x71, /* MR0 */ |
169 | 0x01, 0x20, /* MR1 */ | |
170 | 0x02, 0x0e, /* MR2 RTC control: bits 2 and 1 */ | |
171 | 0x03, 0x80, /* MR3 */ | |
172 | 0x04, 0x30, /* MR4 */ | |
173 | 0x05, 0x00, /* Reserved */ | |
174 | 0x06, 0x00, /* Reserved */ | |
175 | 0x07, TR0MODE, /* TM0 */ | |
176 | 0x08, TR1CAPT, /* TM1 */ | |
177 | 0x09, 0xcb, /* Fsc0 */ | |
178 | 0x0a, 0x8a, /* Fsc1 */ | |
179 | 0x0b, 0x09, /* Fsc2 */ | |
180 | 0x0c, 0x2a, /* Fsc3 */ | |
181 | 0x0d, 0x00, /* Subcarrier Phase */ | |
182 | 0x0e, 0x00, /* Closed Capt. Ext 0 */ | |
183 | 0x0f, 0x00, /* Closed Capt. Ext 1 */ | |
184 | 0x10, 0x00, /* Closed Capt. 0 */ | |
185 | 0x11, 0x00, /* Closed Capt. 1 */ | |
186 | 0x12, 0x00, /* Pedestal Ctl 0 */ | |
187 | 0x13, 0x00, /* Pedestal Ctl 1 */ | |
188 | 0x14, 0x00, /* Pedestal Ctl 2 */ | |
189 | 0x15, 0x00, /* Pedestal Ctl 3 */ | |
190 | 0x16, 0x00, /* CGMS_WSS_0 */ | |
191 | 0x17, 0x00, /* CGMS_WSS_1 */ | |
192 | 0x18, 0x00, /* CGMS_WSS_2 */ | |
193 | 0x19, 0x00, /* Teletext Ctl */ | |
1da177e4 LT |
194 | }; |
195 | ||
196 | ||
7d9ef21c | 197 | static int adv7170_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std) |
1da177e4 | 198 | { |
7d9ef21c HV |
199 | struct adv7170 *encoder = to_adv7170(sd); |
200 | ||
cc5cef8e | 201 | v4l2_dbg(1, debug, sd, "set norm %llx\n", (unsigned long long)std); |
7d9ef21c HV |
202 | |
203 | if (std & V4L2_STD_NTSC) { | |
204 | adv7170_write_block(sd, init_NTSC, sizeof(init_NTSC)); | |
205 | if (encoder->input == 0) | |
206 | adv7170_write(sd, 0x02, 0x0e); /* Enable genlock */ | |
207 | adv7170_write(sd, 0x07, TR0MODE | TR0RST); | |
208 | adv7170_write(sd, 0x07, TR0MODE); | |
209 | } else if (std & V4L2_STD_PAL) { | |
210 | adv7170_write_block(sd, init_PAL, sizeof(init_PAL)); | |
211 | if (encoder->input == 0) | |
212 | adv7170_write(sd, 0x02, 0x0e); /* Enable genlock */ | |
213 | adv7170_write(sd, 0x07, TR0MODE | TR0RST); | |
214 | adv7170_write(sd, 0x07, TR0MODE); | |
215 | } else { | |
cc5cef8e HV |
216 | v4l2_dbg(1, debug, sd, "illegal norm: %llx\n", |
217 | (unsigned long long)std); | |
7d9ef21c | 218 | return -EINVAL; |
b9a21f84 | 219 | } |
cc5cef8e | 220 | v4l2_dbg(1, debug, sd, "switched to %llx\n", (unsigned long long)std); |
7d9ef21c HV |
221 | encoder->norm = std; |
222 | return 0; | |
223 | } | |
1da177e4 | 224 | |
5325b427 HV |
225 | static int adv7170_s_routing(struct v4l2_subdev *sd, |
226 | u32 input, u32 output, u32 config) | |
7d9ef21c HV |
227 | { |
228 | struct adv7170 *encoder = to_adv7170(sd); | |
1da177e4 | 229 | |
5325b427 HV |
230 | /* RJ: input = 0: input is from decoder |
231 | input = 1: input is from ZR36060 | |
232 | input = 2: color bar */ | |
1da177e4 | 233 | |
7d9ef21c | 234 | v4l2_dbg(1, debug, sd, "set input from %s\n", |
5325b427 | 235 | input == 0 ? "decoder" : "ZR36060"); |
1da177e4 | 236 | |
5325b427 | 237 | switch (input) { |
7d9ef21c HV |
238 | case 0: |
239 | adv7170_write(sd, 0x01, 0x20); | |
240 | adv7170_write(sd, 0x08, TR1CAPT); /* TR1 */ | |
241 | adv7170_write(sd, 0x02, 0x0e); /* Enable genlock */ | |
242 | adv7170_write(sd, 0x07, TR0MODE | TR0RST); | |
243 | adv7170_write(sd, 0x07, TR0MODE); | |
244 | /* udelay(10); */ | |
245 | break; | |
246 | ||
247 | case 1: | |
248 | adv7170_write(sd, 0x01, 0x00); | |
249 | adv7170_write(sd, 0x08, TR1PLAY); /* TR1 */ | |
250 | adv7170_write(sd, 0x02, 0x08); | |
251 | adv7170_write(sd, 0x07, TR0MODE | TR0RST); | |
252 | adv7170_write(sd, 0x07, TR0MODE); | |
253 | /* udelay(10); */ | |
1da177e4 LT |
254 | break; |
255 | ||
256 | default: | |
5325b427 | 257 | v4l2_dbg(1, debug, sd, "illegal input: %d\n", input); |
1da177e4 LT |
258 | return -EINVAL; |
259 | } | |
5325b427 HV |
260 | v4l2_dbg(1, debug, sd, "switched to %s\n", inputs[input]); |
261 | encoder->input = input; | |
1da177e4 LT |
262 | return 0; |
263 | } | |
264 | ||
ebcff5fc HV |
265 | static int adv7170_enum_mbus_code(struct v4l2_subdev *sd, |
266 | struct v4l2_subdev_pad_config *cfg, | |
267 | struct v4l2_subdev_mbus_code_enum *code) | |
0d37d350 | 268 | { |
ebcff5fc | 269 | if (code->pad || code->index >= ARRAY_SIZE(adv7170_codes)) |
0d37d350 CG |
270 | return -EINVAL; |
271 | ||
ebcff5fc | 272 | code->code = adv7170_codes[code->index]; |
0d37d350 CG |
273 | return 0; |
274 | } | |
275 | ||
da298c6d HV |
276 | static int adv7170_get_fmt(struct v4l2_subdev *sd, |
277 | struct v4l2_subdev_pad_config *cfg, | |
278 | struct v4l2_subdev_format *format) | |
0d37d350 | 279 | { |
da298c6d | 280 | struct v4l2_mbus_framefmt *mf = &format->format; |
0d37d350 CG |
281 | u8 val = adv7170_read(sd, 0x7); |
282 | ||
da298c6d HV |
283 | if (format->pad) |
284 | return -EINVAL; | |
285 | ||
0d37d350 | 286 | if ((val & 0x40) == (1 << 6)) |
f5fe58fd | 287 | mf->code = MEDIA_BUS_FMT_UYVY8_1X16; |
0d37d350 | 288 | else |
f5fe58fd | 289 | mf->code = MEDIA_BUS_FMT_UYVY8_2X8; |
0d37d350 CG |
290 | |
291 | mf->colorspace = V4L2_COLORSPACE_SMPTE170M; | |
292 | mf->width = 0; | |
293 | mf->height = 0; | |
294 | mf->field = V4L2_FIELD_ANY; | |
295 | ||
296 | return 0; | |
297 | } | |
298 | ||
6e80c473 HV |
299 | static int adv7170_set_fmt(struct v4l2_subdev *sd, |
300 | struct v4l2_subdev_pad_config *cfg, | |
301 | struct v4l2_subdev_format *format) | |
0d37d350 | 302 | { |
6e80c473 | 303 | struct v4l2_mbus_framefmt *mf = &format->format; |
0d37d350 | 304 | u8 val = adv7170_read(sd, 0x7); |
6e80c473 HV |
305 | int ret = 0; |
306 | ||
307 | if (format->pad) | |
308 | return -EINVAL; | |
0d37d350 CG |
309 | |
310 | switch (mf->code) { | |
f5fe58fd | 311 | case MEDIA_BUS_FMT_UYVY8_2X8: |
0d37d350 CG |
312 | val &= ~0x40; |
313 | break; | |
314 | ||
f5fe58fd | 315 | case MEDIA_BUS_FMT_UYVY8_1X16: |
0d37d350 CG |
316 | val |= 0x40; |
317 | break; | |
318 | ||
319 | default: | |
320 | v4l2_dbg(1, debug, sd, | |
321 | "illegal v4l2_mbus_framefmt code: %d\n", mf->code); | |
322 | return -EINVAL; | |
323 | } | |
324 | ||
6e80c473 HV |
325 | if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) |
326 | ret = adv7170_write(sd, 0x7, val); | |
0d37d350 CG |
327 | |
328 | return ret; | |
329 | } | |
330 | ||
1da177e4 LT |
331 | /* ----------------------------------------------------------------------- */ |
332 | ||
7d9ef21c HV |
333 | static const struct v4l2_subdev_video_ops adv7170_video_ops = { |
334 | .s_std_output = adv7170_s_std_output, | |
335 | .s_routing = adv7170_s_routing, | |
ebcff5fc HV |
336 | }; |
337 | ||
338 | static const struct v4l2_subdev_pad_ops adv7170_pad_ops = { | |
339 | .enum_mbus_code = adv7170_enum_mbus_code, | |
da298c6d | 340 | .get_fmt = adv7170_get_fmt, |
6e80c473 | 341 | .set_fmt = adv7170_set_fmt, |
7d9ef21c HV |
342 | }; |
343 | ||
344 | static const struct v4l2_subdev_ops adv7170_ops = { | |
7d9ef21c | 345 | .video = &adv7170_video_ops, |
ebcff5fc | 346 | .pad = &adv7170_pad_ops, |
7d9ef21c HV |
347 | }; |
348 | ||
349 | /* ----------------------------------------------------------------------- */ | |
d56410e0 | 350 | |
b9a21f84 HV |
351 | static int adv7170_probe(struct i2c_client *client, |
352 | const struct i2c_device_id *id) | |
1da177e4 | 353 | { |
1da177e4 | 354 | struct adv7170 *encoder; |
7d9ef21c | 355 | struct v4l2_subdev *sd; |
b9a21f84 | 356 | int i; |
1da177e4 LT |
357 | |
358 | /* Check if the adapter supports the needed features */ | |
b9a21f84 HV |
359 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
360 | return -ENODEV; | |
1da177e4 | 361 | |
b9a21f84 HV |
362 | v4l_info(client, "chip found @ 0x%x (%s)\n", |
363 | client->addr << 1, client->adapter->name); | |
1da177e4 | 364 | |
c02b211d | 365 | encoder = devm_kzalloc(&client->dev, sizeof(*encoder), GFP_KERNEL); |
b9a21f84 | 366 | if (encoder == NULL) |
1da177e4 | 367 | return -ENOMEM; |
7d9ef21c HV |
368 | sd = &encoder->sd; |
369 | v4l2_i2c_subdev_init(sd, client, &adv7170_ops); | |
107063c6 | 370 | encoder->norm = V4L2_STD_NTSC; |
1da177e4 | 371 | encoder->input = 0; |
1da177e4 | 372 | |
7d9ef21c | 373 | i = adv7170_write_block(sd, init_NTSC, sizeof(init_NTSC)); |
1da177e4 | 374 | if (i >= 0) { |
7d9ef21c HV |
375 | i = adv7170_write(sd, 0x07, TR0MODE | TR0RST); |
376 | i = adv7170_write(sd, 0x07, TR0MODE); | |
377 | i = adv7170_read(sd, 0x12); | |
378 | v4l2_dbg(1, debug, sd, "revision %d\n", i & 1); | |
1da177e4 | 379 | } |
b9a21f84 | 380 | if (i < 0) |
7d9ef21c | 381 | v4l2_dbg(1, debug, sd, "init error 0x%x\n", i); |
1da177e4 LT |
382 | return 0; |
383 | } | |
384 | ||
b9a21f84 | 385 | static int adv7170_remove(struct i2c_client *client) |
1da177e4 | 386 | { |
7d9ef21c HV |
387 | struct v4l2_subdev *sd = i2c_get_clientdata(client); |
388 | ||
389 | v4l2_device_unregister_subdev(sd); | |
1da177e4 LT |
390 | return 0; |
391 | } | |
392 | ||
393 | /* ----------------------------------------------------------------------- */ | |
394 | ||
b9a21f84 HV |
395 | static const struct i2c_device_id adv7170_id[] = { |
396 | { "adv7170", 0 }, | |
397 | { "adv7171", 0 }, | |
398 | { } | |
399 | }; | |
400 | MODULE_DEVICE_TABLE(i2c, adv7170_id); | |
1da177e4 | 401 | |
c9611802 HV |
402 | static struct i2c_driver adv7170_driver = { |
403 | .driver = { | |
c9611802 HV |
404 | .name = "adv7170", |
405 | }, | |
406 | .probe = adv7170_probe, | |
407 | .remove = adv7170_remove, | |
408 | .id_table = adv7170_id, | |
1da177e4 | 409 | }; |
c9611802 | 410 | |
c6e8d86f | 411 | module_i2c_driver(adv7170_driver); |