include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[linux-2.6-block.git] / drivers / media / video / adv7170.c
CommitLineData
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
HV
36#include <linux/i2c.h>
37#include <linux/i2c-id.h>
7d9ef21c
HV
38#include <linux/videodev2.h>
39#include <media/v4l2-device.h>
40#include <media/v4l2-chip-ident.h>
2d26698e 41#include <media/v4l2-i2c-drv.h>
1da177e4
LT
42
43MODULE_DESCRIPTION("Analog Devices ADV7170 video encoder driver");
44MODULE_AUTHOR("Maxim Yevtyushkin");
45MODULE_LICENSE("GPL");
46
7d9ef21c 47
ff699e6b 48static int debug;
1da177e4
LT
49module_param(debug, int, 0);
50MODULE_PARM_DESC(debug, "Debug level (0-1)");
51
1da177e4
LT
52/* ----------------------------------------------------------------------- */
53
54struct adv7170 {
7d9ef21c 55 struct v4l2_subdev sd;
1da177e4
LT
56 unsigned char reg[128];
57
107063c6 58 v4l2_std_id norm;
1da177e4 59 int input;
1da177e4
LT
60};
61
7d9ef21c
HV
62static inline struct adv7170 *to_adv7170(struct v4l2_subdev *sd)
63{
64 return container_of(sd, struct adv7170, sd);
65}
66
1da177e4 67static char *inputs[] = { "pass_through", "play_back" };
1da177e4
LT
68
69/* ----------------------------------------------------------------------- */
70
7d9ef21c 71static inline int adv7170_write(struct v4l2_subdev *sd, u8 reg, u8 value)
1da177e4 72{
7d9ef21c
HV
73 struct i2c_client *client = v4l2_get_subdevdata(sd);
74 struct adv7170 *encoder = to_adv7170(sd);
1da177e4
LT
75
76 encoder->reg[reg] = value;
77 return i2c_smbus_write_byte_data(client, reg, value);
78}
79
7d9ef21c 80static inline int adv7170_read(struct v4l2_subdev *sd, u8 reg)
1da177e4 81{
7d9ef21c
HV
82 struct i2c_client *client = v4l2_get_subdevdata(sd);
83
1da177e4
LT
84 return i2c_smbus_read_byte_data(client, reg);
85}
86
7d9ef21c 87static int adv7170_write_block(struct v4l2_subdev *sd,
b9a21f84 88 const u8 *data, unsigned int len)
1da177e4 89{
7d9ef21c
HV
90 struct i2c_client *client = v4l2_get_subdevdata(sd);
91 struct adv7170 *encoder = to_adv7170(sd);
1da177e4
LT
92 int ret = -1;
93 u8 reg;
94
95 /* the adv7170 has an autoincrement function, use it if
96 * the adapter understands raw I2C */
97 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
98 /* do raw I2C, not smbus compatible */
1da177e4 99 u8 block_data[32];
9aa45e34 100 int block_len;
1da177e4 101
1da177e4 102 while (len >= 2) {
9aa45e34
JD
103 block_len = 0;
104 block_data[block_len++] = reg = data[0];
1da177e4 105 do {
9aa45e34 106 block_data[block_len++] =
1da177e4
LT
107 encoder->reg[reg++] = data[1];
108 len -= 2;
109 data += 2;
b9a21f84
HV
110 } while (len >= 2 && data[0] == reg && block_len < 32);
111 ret = i2c_master_send(client, block_data, block_len);
112 if (ret < 0)
1da177e4
LT
113 break;
114 }
115 } else {
116 /* do some slow I2C emulation kind of thing */
117 while (len >= 2) {
118 reg = *data++;
7d9ef21c 119 ret = adv7170_write(sd, reg, *data++);
b9a21f84 120 if (ret < 0)
1da177e4
LT
121 break;
122 len -= 2;
123 }
124 }
1da177e4
LT
125 return ret;
126}
127
128/* ----------------------------------------------------------------------- */
1da177e4
LT
129
130#define TR0MODE 0x4c
131#define TR0RST 0x80
132
133#define TR1CAPT 0x00
134#define TR1PLAY 0x00
135
1da177e4 136static const unsigned char init_NTSC[] = {
7d9ef21c
HV
137 0x00, 0x10, /* MR0 */
138 0x01, 0x20, /* MR1 */
139 0x02, 0x0e, /* MR2 RTC control: bits 2 and 1 */
140 0x03, 0x80, /* MR3 */
141 0x04, 0x30, /* MR4 */
142 0x05, 0x00, /* Reserved */
143 0x06, 0x00, /* Reserved */
144 0x07, TR0MODE, /* TM0 */
145 0x08, TR1CAPT, /* TM1 */
146 0x09, 0x16, /* Fsc0 */
147 0x0a, 0x7c, /* Fsc1 */
148 0x0b, 0xf0, /* Fsc2 */
149 0x0c, 0x21, /* Fsc3 */
150 0x0d, 0x00, /* Subcarrier Phase */
151 0x0e, 0x00, /* Closed Capt. Ext 0 */
152 0x0f, 0x00, /* Closed Capt. Ext 1 */
153 0x10, 0x00, /* Closed Capt. 0 */
154 0x11, 0x00, /* Closed Capt. 1 */
155 0x12, 0x00, /* Pedestal Ctl 0 */
156 0x13, 0x00, /* Pedestal Ctl 1 */
157 0x14, 0x00, /* Pedestal Ctl 2 */
158 0x15, 0x00, /* Pedestal Ctl 3 */
159 0x16, 0x00, /* CGMS_WSS_0 */
160 0x17, 0x00, /* CGMS_WSS_1 */
161 0x18, 0x00, /* CGMS_WSS_2 */
162 0x19, 0x00, /* Teletext Ctl */
1da177e4
LT
163};
164
165static const unsigned char init_PAL[] = {
7d9ef21c
HV
166 0x00, 0x71, /* MR0 */
167 0x01, 0x20, /* MR1 */
168 0x02, 0x0e, /* MR2 RTC control: bits 2 and 1 */
169 0x03, 0x80, /* MR3 */
170 0x04, 0x30, /* MR4 */
171 0x05, 0x00, /* Reserved */
172 0x06, 0x00, /* Reserved */
173 0x07, TR0MODE, /* TM0 */
174 0x08, TR1CAPT, /* TM1 */
175 0x09, 0xcb, /* Fsc0 */
176 0x0a, 0x8a, /* Fsc1 */
177 0x0b, 0x09, /* Fsc2 */
178 0x0c, 0x2a, /* Fsc3 */
179 0x0d, 0x00, /* Subcarrier Phase */
180 0x0e, 0x00, /* Closed Capt. Ext 0 */
181 0x0f, 0x00, /* Closed Capt. Ext 1 */
182 0x10, 0x00, /* Closed Capt. 0 */
183 0x11, 0x00, /* Closed Capt. 1 */
184 0x12, 0x00, /* Pedestal Ctl 0 */
185 0x13, 0x00, /* Pedestal Ctl 1 */
186 0x14, 0x00, /* Pedestal Ctl 2 */
187 0x15, 0x00, /* Pedestal Ctl 3 */
188 0x16, 0x00, /* CGMS_WSS_0 */
189 0x17, 0x00, /* CGMS_WSS_1 */
190 0x18, 0x00, /* CGMS_WSS_2 */
191 0x19, 0x00, /* Teletext Ctl */
1da177e4
LT
192};
193
194
7d9ef21c 195static int adv7170_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std)
1da177e4 196{
7d9ef21c
HV
197 struct adv7170 *encoder = to_adv7170(sd);
198
cc5cef8e 199 v4l2_dbg(1, debug, sd, "set norm %llx\n", (unsigned long long)std);
7d9ef21c
HV
200
201 if (std & V4L2_STD_NTSC) {
202 adv7170_write_block(sd, init_NTSC, sizeof(init_NTSC));
203 if (encoder->input == 0)
204 adv7170_write(sd, 0x02, 0x0e); /* Enable genlock */
205 adv7170_write(sd, 0x07, TR0MODE | TR0RST);
206 adv7170_write(sd, 0x07, TR0MODE);
207 } else if (std & V4L2_STD_PAL) {
208 adv7170_write_block(sd, init_PAL, sizeof(init_PAL));
209 if (encoder->input == 0)
210 adv7170_write(sd, 0x02, 0x0e); /* Enable genlock */
211 adv7170_write(sd, 0x07, TR0MODE | TR0RST);
212 adv7170_write(sd, 0x07, TR0MODE);
213 } else {
cc5cef8e
HV
214 v4l2_dbg(1, debug, sd, "illegal norm: %llx\n",
215 (unsigned long long)std);
7d9ef21c 216 return -EINVAL;
b9a21f84 217 }
cc5cef8e 218 v4l2_dbg(1, debug, sd, "switched to %llx\n", (unsigned long long)std);
7d9ef21c
HV
219 encoder->norm = std;
220 return 0;
221}
1da177e4 222
5325b427
HV
223static int adv7170_s_routing(struct v4l2_subdev *sd,
224 u32 input, u32 output, u32 config)
7d9ef21c
HV
225{
226 struct adv7170 *encoder = to_adv7170(sd);
1da177e4 227
5325b427
HV
228 /* RJ: input = 0: input is from decoder
229 input = 1: input is from ZR36060
230 input = 2: color bar */
1da177e4 231
7d9ef21c 232 v4l2_dbg(1, debug, sd, "set input from %s\n",
5325b427 233 input == 0 ? "decoder" : "ZR36060");
1da177e4 234
5325b427 235 switch (input) {
7d9ef21c
HV
236 case 0:
237 adv7170_write(sd, 0x01, 0x20);
238 adv7170_write(sd, 0x08, TR1CAPT); /* TR1 */
239 adv7170_write(sd, 0x02, 0x0e); /* Enable genlock */
240 adv7170_write(sd, 0x07, TR0MODE | TR0RST);
241 adv7170_write(sd, 0x07, TR0MODE);
242 /* udelay(10); */
243 break;
244
245 case 1:
246 adv7170_write(sd, 0x01, 0x00);
247 adv7170_write(sd, 0x08, TR1PLAY); /* TR1 */
248 adv7170_write(sd, 0x02, 0x08);
249 adv7170_write(sd, 0x07, TR0MODE | TR0RST);
250 adv7170_write(sd, 0x07, TR0MODE);
251 /* udelay(10); */
1da177e4
LT
252 break;
253
254 default:
5325b427 255 v4l2_dbg(1, debug, sd, "illegal input: %d\n", input);
1da177e4
LT
256 return -EINVAL;
257 }
5325b427
HV
258 v4l2_dbg(1, debug, sd, "switched to %s\n", inputs[input]);
259 encoder->input = input;
1da177e4
LT
260 return 0;
261}
262
7d9ef21c
HV
263static int adv7170_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
264{
265 struct i2c_client *client = v4l2_get_subdevdata(sd);
266
35631dcc 267 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_ADV7170, 0);
7d9ef21c
HV
268}
269
1da177e4
LT
270/* ----------------------------------------------------------------------- */
271
7d9ef21c
HV
272static const struct v4l2_subdev_core_ops adv7170_core_ops = {
273 .g_chip_ident = adv7170_g_chip_ident,
1da177e4 274};
1da177e4 275
7d9ef21c
HV
276static const struct v4l2_subdev_video_ops adv7170_video_ops = {
277 .s_std_output = adv7170_s_std_output,
278 .s_routing = adv7170_s_routing,
279};
280
281static const struct v4l2_subdev_ops adv7170_ops = {
282 .core = &adv7170_core_ops,
283 .video = &adv7170_video_ops,
284};
285
286/* ----------------------------------------------------------------------- */
d56410e0 287
b9a21f84
HV
288static int adv7170_probe(struct i2c_client *client,
289 const struct i2c_device_id *id)
1da177e4 290{
1da177e4 291 struct adv7170 *encoder;
7d9ef21c 292 struct v4l2_subdev *sd;
b9a21f84 293 int i;
1da177e4
LT
294
295 /* Check if the adapter supports the needed features */
b9a21f84
HV
296 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
297 return -ENODEV;
1da177e4 298
b9a21f84
HV
299 v4l_info(client, "chip found @ 0x%x (%s)\n",
300 client->addr << 1, client->adapter->name);
1da177e4 301
7408187d 302 encoder = kzalloc(sizeof(struct adv7170), GFP_KERNEL);
b9a21f84 303 if (encoder == NULL)
1da177e4 304 return -ENOMEM;
7d9ef21c
HV
305 sd = &encoder->sd;
306 v4l2_i2c_subdev_init(sd, client, &adv7170_ops);
107063c6 307 encoder->norm = V4L2_STD_NTSC;
1da177e4 308 encoder->input = 0;
1da177e4 309
7d9ef21c 310 i = adv7170_write_block(sd, init_NTSC, sizeof(init_NTSC));
1da177e4 311 if (i >= 0) {
7d9ef21c
HV
312 i = adv7170_write(sd, 0x07, TR0MODE | TR0RST);
313 i = adv7170_write(sd, 0x07, TR0MODE);
314 i = adv7170_read(sd, 0x12);
315 v4l2_dbg(1, debug, sd, "revision %d\n", i & 1);
1da177e4 316 }
b9a21f84 317 if (i < 0)
7d9ef21c 318 v4l2_dbg(1, debug, sd, "init error 0x%x\n", i);
1da177e4
LT
319 return 0;
320}
321
b9a21f84 322static int adv7170_remove(struct i2c_client *client)
1da177e4 323{
7d9ef21c
HV
324 struct v4l2_subdev *sd = i2c_get_clientdata(client);
325
326 v4l2_device_unregister_subdev(sd);
327 kfree(to_adv7170(sd));
1da177e4
LT
328 return 0;
329}
330
331/* ----------------------------------------------------------------------- */
332
b9a21f84
HV
333static const struct i2c_device_id adv7170_id[] = {
334 { "adv7170", 0 },
335 { "adv7171", 0 },
336 { }
337};
338MODULE_DEVICE_TABLE(i2c, adv7170_id);
1da177e4 339
b9a21f84
HV
340static struct v4l2_i2c_driver_data v4l2_i2c_data = {
341 .name = "adv7170",
b9a21f84
HV
342 .probe = adv7170_probe,
343 .remove = adv7170_remove,
344 .id_table = adv7170_id,
1da177e4 345};