blob: 154dff03a7d8a97c50ad3bf56c09451b1a0734cb [file] [log] [blame]
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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>
Mauro Carvalho Chehab18f3fa12007-07-02 15:39:57 -030028#include <linux/types.h>
Hans Verkuilbba0d982008-09-07 07:58:46 -030029#include <linux/ioctl.h>
Mauro Carvalho Chehab18f3fa12007-07-02 15:39:57 -030030#include <asm/uaccess.h>
Hans Verkuilbba0d982008-09-07 07:58:46 -030031#include <linux/i2c.h>
32#include <linux/i2c-id.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/videodev.h>
Mauro Carvalho Chehab18f3fa12007-07-02 15:39:57 -030034#include <linux/video_encoder.h>
Hans Verkuilbba0d982008-09-07 07:58:46 -030035#include <media/v4l2-common.h>
36#include <media/v4l2-i2c-drv-legacy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38MODULE_DESCRIPTION("Analog Devices ADV7175 video encoder driver");
39MODULE_AUTHOR("Dave Perks");
40MODULE_LICENSE("GPL");
41
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030042static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043module_param(debug, int, 0);
44MODULE_PARM_DESC(debug, "Debug level (0-1)");
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/* ----------------------------------------------------------------------- */
47
48struct adv7175 {
Hans Verkuil107063c2009-02-18 17:26:06 -030049 v4l2_std_id norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 int input;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 int bright;
52 int contrast;
53 int hue;
54 int sat;
55};
56
57#define I2C_ADV7175 0xd4
58#define I2C_ADV7176 0x54
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static char *inputs[] = { "pass_through", "play_back", "color_bar" };
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62/* ----------------------------------------------------------------------- */
63
Hans Verkuilbba0d982008-09-07 07:58:46 -030064static inline int adv7175_write(struct i2c_client *client, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return i2c_smbus_write_byte_data(client, reg, value);
67}
68
Hans Verkuilbba0d982008-09-07 07:58:46 -030069static inline int adv7175_read(struct i2c_client *client, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
71 return i2c_smbus_read_byte_data(client, reg);
72}
73
Hans Verkuilbba0d982008-09-07 07:58:46 -030074static int adv7175_write_block(struct i2c_client *client,
75 const u8 *data, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 int ret = -1;
78 u8 reg;
79
80 /* the adv7175 has an autoincrement function, use it if
81 * the adapter understands raw I2C */
82 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
83 /* do raw I2C, not smbus compatible */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 u8 block_data[32];
Jean Delvare9aa45e32006-03-22 03:48:35 -030085 int block_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 while (len >= 2) {
Jean Delvare9aa45e32006-03-22 03:48:35 -030088 block_len = 0;
89 block_data[block_len++] = reg = data[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 do {
Jean Delvare9aa45e32006-03-22 03:48:35 -030091 block_data[block_len++] = data[1];
Jean Delvare2467a672006-03-22 03:48:34 -030092 reg++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 len -= 2;
94 data += 2;
Hans Verkuilbba0d982008-09-07 07:58:46 -030095 } while (len >= 2 && data[0] == reg && block_len < 32);
96 ret = i2c_master_send(client, block_data, block_len);
97 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 break;
99 }
100 } else {
101 /* do some slow I2C emulation kind of thing */
102 while (len >= 2) {
103 reg = *data++;
Hans Verkuilbba0d982008-09-07 07:58:46 -0300104 ret = adv7175_write(client, reg, *data++);
105 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 break;
107 len -= 2;
108 }
109 }
110
111 return ret;
112}
113
Hans Verkuilbba0d982008-09-07 07:58:46 -0300114static void set_subcarrier_freq(struct i2c_client *client, int pass_through)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 /* for some reason pass_through NTSC needs
117 * a different sub-carrier freq to remain stable. */
Hans Verkuilbba0d982008-09-07 07:58:46 -0300118 if (pass_through)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 adv7175_write(client, 0x02, 0x00);
120 else
121 adv7175_write(client, 0x02, 0x55);
122
123 adv7175_write(client, 0x03, 0x55);
124 adv7175_write(client, 0x04, 0x55);
125 adv7175_write(client, 0x05, 0x25);
126}
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128/* ----------------------------------------------------------------------- */
Hans Verkuilbba0d982008-09-07 07:58:46 -0300129/* Output filter: S-Video Composite */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Hans Verkuilbba0d982008-09-07 07:58:46 -0300131#define MR050 0x11 /* 0x09 */
132#define MR060 0x14 /* 0x0c */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Hans Verkuilbba0d982008-09-07 07:58:46 -0300134/* ----------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136#define TR0MODE 0x46
137#define TR0RST 0x80
138
139#define TR1CAPT 0x80
140#define TR1PLAY 0x00
141
142static const unsigned char init_common[] = {
143
144 0x00, MR050, /* MR0, PAL enabled */
145 0x01, 0x00, /* MR1 */
146 0x02, 0x0c, /* subc. freq. */
147 0x03, 0x8c, /* subc. freq. */
148 0x04, 0x79, /* subc. freq. */
149 0x05, 0x26, /* subc. freq. */
150 0x06, 0x40, /* subc. phase */
151
152 0x07, TR0MODE, /* TR0, 16bit */
153 0x08, 0x21, /* */
154 0x09, 0x00, /* */
155 0x0a, 0x00, /* */
156 0x0b, 0x00, /* */
157 0x0c, TR1CAPT, /* TR1 */
158 0x0d, 0x4f, /* MR2 */
159 0x0e, 0x00, /* */
160 0x0f, 0x00, /* */
161 0x10, 0x00, /* */
162 0x11, 0x00, /* */
163};
164
165static const unsigned char init_pal[] = {
166 0x00, MR050, /* MR0, PAL enabled */
167 0x01, 0x00, /* MR1 */
168 0x02, 0x0c, /* subc. freq. */
169 0x03, 0x8c, /* subc. freq. */
170 0x04, 0x79, /* subc. freq. */
171 0x05, 0x26, /* subc. freq. */
172 0x06, 0x40, /* subc. phase */
173};
174
175static const unsigned char init_ntsc[] = {
176 0x00, MR060, /* MR0, NTSC enabled */
177 0x01, 0x00, /* MR1 */
178 0x02, 0x55, /* subc. freq. */
179 0x03, 0x55, /* subc. freq. */
180 0x04, 0x55, /* subc. freq. */
181 0x05, 0x25, /* subc. freq. */
182 0x06, 0x1a, /* subc. phase */
183};
184
Hans Verkuilbba0d982008-09-07 07:58:46 -0300185static int adv7175_command(struct i2c_client *client, unsigned cmd, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 struct adv7175 *encoder = i2c_get_clientdata(client);
188
189 switch (cmd) {
Hans Verkuil107063c2009-02-18 17:26:06 -0300190 case VIDIOC_INT_INIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 /* This is just for testing!!! */
192 adv7175_write_block(client, init_common,
193 sizeof(init_common));
194 adv7175_write(client, 0x07, TR0MODE | TR0RST);
195 adv7175_write(client, 0x07, TR0MODE);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300196 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Hans Verkuil107063c2009-02-18 17:26:06 -0300198 case VIDIOC_INT_S_STD_OUTPUT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 {
Hans Verkuil107063c2009-02-18 17:26:06 -0300200 v4l2_std_id iarg = *(v4l2_std_id *) arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Hans Verkuil107063c2009-02-18 17:26:06 -0300202 if (iarg & V4L2_STD_NTSC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 adv7175_write_block(client, init_ntsc,
204 sizeof(init_ntsc));
205 if (encoder->input == 0)
206 adv7175_write(client, 0x0d, 0x4f); // Enable genlock
207 adv7175_write(client, 0x07, TR0MODE | TR0RST);
208 adv7175_write(client, 0x07, TR0MODE);
Hans Verkuil107063c2009-02-18 17:26:06 -0300209 } else if (iarg & V4L2_STD_PAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 adv7175_write_block(client, init_pal,
211 sizeof(init_pal));
212 if (encoder->input == 0)
213 adv7175_write(client, 0x0d, 0x4f); // Enable genlock
214 adv7175_write(client, 0x07, TR0MODE | TR0RST);
215 adv7175_write(client, 0x07, TR0MODE);
Hans Verkuil107063c2009-02-18 17:26:06 -0300216 } else if (iarg & V4L2_STD_SECAM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 /* This is an attempt to convert
218 * SECAM->PAL (typically it does not work
219 * due to genlock: when decoder is in SECAM
220 * and encoder in in PAL the subcarrier can
221 * not be syncronized with horizontal
222 * quency) */
223 adv7175_write_block(client, init_pal,
224 sizeof(init_pal));
225 if (encoder->input == 0)
226 adv7175_write(client, 0x0d, 0x49); // Disable genlock
227 adv7175_write(client, 0x07, TR0MODE | TR0RST);
228 adv7175_write(client, 0x07, TR0MODE);
Hans Verkuil107063c2009-02-18 17:26:06 -0300229 } else {
230 v4l_dbg(1, debug, client, "illegal norm: %llx\n", iarg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
Hans Verkuil107063c2009-02-18 17:26:06 -0300233 v4l_dbg(1, debug, client, "switched to %llx\n", iarg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 encoder->norm = iarg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 break;
Hans Verkuilbba0d982008-09-07 07:58:46 -0300236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Hans Verkuil107063c2009-02-18 17:26:06 -0300238 case VIDIOC_INT_S_VIDEO_ROUTING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 {
Hans Verkuil107063c2009-02-18 17:26:06 -0300240 struct v4l2_routing *route = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 /* RJ: *iarg = 0: input is from SAA7110
243 *iarg = 1: input is from ZR36060
244 *iarg = 2: color bar */
245
Hans Verkuil107063c2009-02-18 17:26:06 -0300246 switch (route->input) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 case 0:
248 adv7175_write(client, 0x01, 0x00);
249
Hans Verkuil107063c2009-02-18 17:26:06 -0300250 if (encoder->norm & V4L2_STD_NTSC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 set_subcarrier_freq(client, 1);
252
253 adv7175_write(client, 0x0c, TR1CAPT); /* TR1 */
Hans Verkuil107063c2009-02-18 17:26:06 -0300254 if (encoder->norm & V4L2_STD_SECAM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 adv7175_write(client, 0x0d, 0x49); // Disable genlock
256 else
257 adv7175_write(client, 0x0d, 0x4f); // Enable genlock
258 adv7175_write(client, 0x07, TR0MODE | TR0RST);
259 adv7175_write(client, 0x07, TR0MODE);
260 //udelay(10);
261 break;
262
263 case 1:
264 adv7175_write(client, 0x01, 0x00);
265
Hans Verkuil107063c2009-02-18 17:26:06 -0300266 if (encoder->norm & V4L2_STD_NTSC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 set_subcarrier_freq(client, 0);
268
269 adv7175_write(client, 0x0c, TR1PLAY); /* TR1 */
270 adv7175_write(client, 0x0d, 0x49);
271 adv7175_write(client, 0x07, TR0MODE | TR0RST);
272 adv7175_write(client, 0x07, TR0MODE);
Hans Verkuilbba0d982008-09-07 07:58:46 -0300273 /* udelay(10); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 break;
275
276 case 2:
277 adv7175_write(client, 0x01, 0x80);
278
Hans Verkuil107063c2009-02-18 17:26:06 -0300279 if (encoder->norm & V4L2_STD_NTSC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 set_subcarrier_freq(client, 0);
281
282 adv7175_write(client, 0x0d, 0x49);
283 adv7175_write(client, 0x07, TR0MODE | TR0RST);
284 adv7175_write(client, 0x07, TR0MODE);
Hans Verkuilbba0d982008-09-07 07:58:46 -0300285 /* udelay(10); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 break;
287
288 default:
Hans Verkuil107063c2009-02-18 17:26:06 -0300289 v4l_dbg(1, debug, client, "illegal input: %d\n", route->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
Hans Verkuil107063c2009-02-18 17:26:06 -0300292 v4l_dbg(1, debug, client, "switched to %s\n", inputs[route->input]);
293 encoder->input = route->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 break;
Hans Verkuilbba0d982008-09-07 07:58:46 -0300295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 default:
298 return -EINVAL;
299 }
300
301 return 0;
302}
303
304/* ----------------------------------------------------------------------- */
305
306/*
307 * Generic i2c probe
308 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
309 */
Hans Verkuilbba0d982008-09-07 07:58:46 -0300310static unsigned short normal_i2c[] = {
311 I2C_ADV7175 >> 1, (I2C_ADV7175 >> 1) + 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 I2C_ADV7176 >> 1, (I2C_ADV7176 >> 1) + 1,
313 I2C_CLIENT_END
314};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Hans Verkuilbba0d982008-09-07 07:58:46 -0300316I2C_CLIENT_INSMOD;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300317
Hans Verkuilbba0d982008-09-07 07:58:46 -0300318static int adv7175_probe(struct i2c_client *client,
319 const struct i2c_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 struct adv7175 *encoder;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 /* Check if the adapter supports the needed features */
Hans Verkuilbba0d982008-09-07 07:58:46 -0300325 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
326 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Hans Verkuilbba0d982008-09-07 07:58:46 -0300328 v4l_info(client, "chip found @ 0x%x (%s)\n",
329 client->addr << 1, client->adapter->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Panagiotis Issaris74081872006-01-11 19:40:56 -0200331 encoder = kzalloc(sizeof(struct adv7175), GFP_KERNEL);
Hans Verkuilbba0d982008-09-07 07:58:46 -0300332 if (encoder == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return -ENOMEM;
Hans Verkuil107063c2009-02-18 17:26:06 -0300334 encoder->norm = V4L2_STD_NTSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 encoder->input = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 i2c_set_clientdata(client, encoder);
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 i = adv7175_write_block(client, init_common, sizeof(init_common));
339 if (i >= 0) {
340 i = adv7175_write(client, 0x07, TR0MODE | TR0RST);
341 i = adv7175_write(client, 0x07, TR0MODE);
342 i = adv7175_read(client, 0x12);
Hans Verkuilbba0d982008-09-07 07:58:46 -0300343 v4l_dbg(1, debug, client, "revision %d\n", i & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
Hans Verkuilbba0d982008-09-07 07:58:46 -0300345 if (i < 0)
346 v4l_dbg(1, debug, client, "init error 0x%x\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return 0;
348}
349
Hans Verkuilbba0d982008-09-07 07:58:46 -0300350static int adv7175_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Hans Verkuilbba0d982008-09-07 07:58:46 -0300352 kfree(i2c_get_clientdata(client));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return 0;
354}
355
356/* ----------------------------------------------------------------------- */
357
Hans Verkuilbba0d982008-09-07 07:58:46 -0300358static const struct i2c_device_id adv7175_id[] = {
359 { "adv7175", 0 },
360 { "adv7176", 0 },
361 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362};
Hans Verkuilbba0d982008-09-07 07:58:46 -0300363MODULE_DEVICE_TABLE(i2c, adv7175_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Hans Verkuilbba0d982008-09-07 07:58:46 -0300365static struct v4l2_i2c_driver_data v4l2_i2c_data = {
366 .name = "adv7175",
367 .driverid = I2C_DRIVERID_ADV7175,
368 .command = adv7175_command,
369 .probe = adv7175_probe,
370 .remove = adv7175_remove,
371 .id_table = adv7175_id,
372};