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