blob: 709e044f007dd4f07cd1194f05c85a020bbe226e [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>
Hans Verkuil35631dc2009-02-19 14:56:37 -030033#include <linux/videodev2.h>
34#include <media/v4l2-device.h>
35#include <media/v4l2-chip-ident.h>
Hans Verkuil2d266982009-02-19 17:41:19 -030036#include <media/v4l2-i2c-drv.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
Hans Verkuil35631dc2009-02-19 14:56:37 -030042#define I2C_ADV7175 0xd4
43#define I2C_ADV7176 0x54
44
Hans Verkuil35631dc2009-02-19 14:56:37 -030045
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030046static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047module_param(debug, int, 0);
48MODULE_PARM_DESC(debug, "Debug level (0-1)");
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050/* ----------------------------------------------------------------------- */
51
52struct adv7175 {
Hans Verkuil35631dc2009-02-19 14:56:37 -030053 struct v4l2_subdev sd;
Hans Verkuil107063c2009-02-18 17:26:06 -030054 v4l2_std_id norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 int input;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056};
57
Hans Verkuil35631dc2009-02-19 14:56:37 -030058static inline struct adv7175 *to_adv7175(struct v4l2_subdev *sd)
59{
60 return container_of(sd, struct adv7175, sd);
61}
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static char *inputs[] = { "pass_through", "play_back", "color_bar" };
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/* ----------------------------------------------------------------------- */
66
Hans Verkuil35631dc2009-02-19 14:56:37 -030067static inline int adv7175_write(struct v4l2_subdev *sd, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Hans Verkuil35631dc2009-02-19 14:56:37 -030069 struct i2c_client *client = v4l2_get_subdevdata(sd);
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 return i2c_smbus_write_byte_data(client, reg, value);
72}
73
Hans Verkuil35631dc2009-02-19 14:56:37 -030074static inline int adv7175_read(struct v4l2_subdev *sd, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Hans Verkuil35631dc2009-02-19 14:56:37 -030076 struct i2c_client *client = v4l2_get_subdevdata(sd);
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return i2c_smbus_read_byte_data(client, reg);
79}
80
Hans Verkuil35631dc2009-02-19 14:56:37 -030081static int adv7175_write_block(struct v4l2_subdev *sd,
Hans Verkuilbba0d982008-09-07 07:58:46 -030082 const u8 *data, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Hans Verkuil35631dc2009-02-19 14:56:37 -030084 struct i2c_client *client = v4l2_get_subdevdata(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 int ret = -1;
86 u8 reg;
87
88 /* the adv7175 has an autoincrement function, use it if
89 * the adapter understands raw I2C */
90 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
91 /* do raw I2C, not smbus compatible */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 u8 block_data[32];
Jean Delvare9aa45e32006-03-22 03:48:35 -030093 int block_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 while (len >= 2) {
Jean Delvare9aa45e32006-03-22 03:48:35 -030096 block_len = 0;
97 block_data[block_len++] = reg = data[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 do {
Jean Delvare9aa45e32006-03-22 03:48:35 -030099 block_data[block_len++] = data[1];
Jean Delvare2467a672006-03-22 03:48:34 -0300100 reg++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 len -= 2;
102 data += 2;
Hans Verkuilbba0d982008-09-07 07:58:46 -0300103 } while (len >= 2 && data[0] == reg && block_len < 32);
104 ret = i2c_master_send(client, block_data, block_len);
105 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 break;
107 }
108 } else {
109 /* do some slow I2C emulation kind of thing */
110 while (len >= 2) {
111 reg = *data++;
Hans Verkuil35631dc2009-02-19 14:56:37 -0300112 ret = adv7175_write(sd, reg, *data++);
Hans Verkuilbba0d982008-09-07 07:58:46 -0300113 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 break;
115 len -= 2;
116 }
117 }
118
119 return ret;
120}
121
Hans Verkuil35631dc2009-02-19 14:56:37 -0300122static void set_subcarrier_freq(struct v4l2_subdev *sd, int pass_through)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 /* for some reason pass_through NTSC needs
125 * a different sub-carrier freq to remain stable. */
Hans Verkuilbba0d982008-09-07 07:58:46 -0300126 if (pass_through)
Hans Verkuil35631dc2009-02-19 14:56:37 -0300127 adv7175_write(sd, 0x02, 0x00);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 else
Hans Verkuil35631dc2009-02-19 14:56:37 -0300129 adv7175_write(sd, 0x02, 0x55);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Hans Verkuil35631dc2009-02-19 14:56:37 -0300131 adv7175_write(sd, 0x03, 0x55);
132 adv7175_write(sd, 0x04, 0x55);
133 adv7175_write(sd, 0x05, 0x25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136/* ----------------------------------------------------------------------- */
Hans Verkuilbba0d982008-09-07 07:58:46 -0300137/* Output filter: S-Video Composite */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Hans Verkuilbba0d982008-09-07 07:58:46 -0300139#define MR050 0x11 /* 0x09 */
140#define MR060 0x14 /* 0x0c */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Hans Verkuilbba0d982008-09-07 07:58:46 -0300142/* ----------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144#define TR0MODE 0x46
145#define TR0RST 0x80
146
147#define TR1CAPT 0x80
148#define TR1PLAY 0x00
149
150static const unsigned char init_common[] = {
151
152 0x00, MR050, /* MR0, PAL enabled */
153 0x01, 0x00, /* MR1 */
154 0x02, 0x0c, /* subc. freq. */
155 0x03, 0x8c, /* subc. freq. */
156 0x04, 0x79, /* subc. freq. */
157 0x05, 0x26, /* subc. freq. */
158 0x06, 0x40, /* subc. phase */
159
160 0x07, TR0MODE, /* TR0, 16bit */
161 0x08, 0x21, /* */
162 0x09, 0x00, /* */
163 0x0a, 0x00, /* */
164 0x0b, 0x00, /* */
165 0x0c, TR1CAPT, /* TR1 */
166 0x0d, 0x4f, /* MR2 */
167 0x0e, 0x00, /* */
168 0x0f, 0x00, /* */
169 0x10, 0x00, /* */
170 0x11, 0x00, /* */
171};
172
173static const unsigned char init_pal[] = {
174 0x00, MR050, /* MR0, PAL enabled */
175 0x01, 0x00, /* MR1 */
176 0x02, 0x0c, /* subc. freq. */
177 0x03, 0x8c, /* subc. freq. */
178 0x04, 0x79, /* subc. freq. */
179 0x05, 0x26, /* subc. freq. */
180 0x06, 0x40, /* subc. phase */
181};
182
183static const unsigned char init_ntsc[] = {
184 0x00, MR060, /* MR0, NTSC enabled */
185 0x01, 0x00, /* MR1 */
186 0x02, 0x55, /* subc. freq. */
187 0x03, 0x55, /* subc. freq. */
188 0x04, 0x55, /* subc. freq. */
189 0x05, 0x25, /* subc. freq. */
190 0x06, 0x1a, /* subc. phase */
191};
192
Hans Verkuil35631dc2009-02-19 14:56:37 -0300193static int adv7175_init(struct v4l2_subdev *sd, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Hans Verkuil35631dc2009-02-19 14:56:37 -0300195 /* This is just for testing!!! */
196 adv7175_write_block(sd, init_common, sizeof(init_common));
197 adv7175_write(sd, 0x07, TR0MODE | TR0RST);
198 adv7175_write(sd, 0x07, TR0MODE);
199 return 0;
200}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Hans Verkuil35631dc2009-02-19 14:56:37 -0300202static int adv7175_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std)
203{
204 struct adv7175 *encoder = to_adv7175(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Hans Verkuil35631dc2009-02-19 14:56:37 -0300206 if (std & V4L2_STD_NTSC) {
207 adv7175_write_block(sd, init_ntsc, sizeof(init_ntsc));
208 if (encoder->input == 0)
209 adv7175_write(sd, 0x0d, 0x4f); /* Enable genlock */
210 adv7175_write(sd, 0x07, TR0MODE | TR0RST);
211 adv7175_write(sd, 0x07, TR0MODE);
212 } else if (std & V4L2_STD_PAL) {
213 adv7175_write_block(sd, init_pal, sizeof(init_pal));
214 if (encoder->input == 0)
215 adv7175_write(sd, 0x0d, 0x4f); /* Enable genlock */
216 adv7175_write(sd, 0x07, TR0MODE | TR0RST);
217 adv7175_write(sd, 0x07, TR0MODE);
218 } else if (std & V4L2_STD_SECAM) {
219 /* This is an attempt to convert
220 * SECAM->PAL (typically it does not work
221 * due to genlock: when decoder is in SECAM
222 * and encoder in in PAL the subcarrier can
223 * not be syncronized with horizontal
224 * quency) */
225 adv7175_write_block(sd, init_pal, sizeof(init_pal));
226 if (encoder->input == 0)
227 adv7175_write(sd, 0x0d, 0x49); /* Disable genlock */
228 adv7175_write(sd, 0x07, TR0MODE | TR0RST);
229 adv7175_write(sd, 0x07, TR0MODE);
230 } else {
231 v4l2_dbg(1, debug, sd, "illegal norm: %llx\n", std);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return -EINVAL;
233 }
Hans Verkuil35631dc2009-02-19 14:56:37 -0300234 v4l2_dbg(1, debug, sd, "switched to %llx\n", std);
235 encoder->norm = std;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return 0;
237}
238
Hans Verkuil35631dc2009-02-19 14:56:37 -0300239static int adv7175_s_routing(struct v4l2_subdev *sd, const struct v4l2_routing *route)
240{
241 struct adv7175 *encoder = to_adv7175(sd);
242
243 /* RJ: route->input = 0: input is from decoder
244 route->input = 1: input is from ZR36060
245 route->input = 2: color bar */
246
247 switch (route->input) {
248 case 0:
249 adv7175_write(sd, 0x01, 0x00);
250
251 if (encoder->norm & V4L2_STD_NTSC)
252 set_subcarrier_freq(sd, 1);
253
254 adv7175_write(sd, 0x0c, TR1CAPT); /* TR1 */
255 if (encoder->norm & V4L2_STD_SECAM)
256 adv7175_write(sd, 0x0d, 0x49); /* Disable genlock */
257 else
258 adv7175_write(sd, 0x0d, 0x4f); /* Enable genlock */
259 adv7175_write(sd, 0x07, TR0MODE | TR0RST);
260 adv7175_write(sd, 0x07, TR0MODE);
261 /*udelay(10);*/
262 break;
263
264 case 1:
265 adv7175_write(sd, 0x01, 0x00);
266
267 if (encoder->norm & V4L2_STD_NTSC)
268 set_subcarrier_freq(sd, 0);
269
270 adv7175_write(sd, 0x0c, TR1PLAY); /* TR1 */
271 adv7175_write(sd, 0x0d, 0x49);
272 adv7175_write(sd, 0x07, TR0MODE | TR0RST);
273 adv7175_write(sd, 0x07, TR0MODE);
274 /* udelay(10); */
275 break;
276
277 case 2:
278 adv7175_write(sd, 0x01, 0x80);
279
280 if (encoder->norm & V4L2_STD_NTSC)
281 set_subcarrier_freq(sd, 0);
282
283 adv7175_write(sd, 0x0d, 0x49);
284 adv7175_write(sd, 0x07, TR0MODE | TR0RST);
285 adv7175_write(sd, 0x07, TR0MODE);
286 /* udelay(10); */
287 break;
288
289 default:
290 v4l2_dbg(1, debug, sd, "illegal input: %d\n", route->input);
291 return -EINVAL;
292 }
293 v4l2_dbg(1, debug, sd, "switched to %s\n", inputs[route->input]);
294 encoder->input = route->input;
295 return 0;
296}
297
298static int adv7175_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
299{
300 struct i2c_client *client = v4l2_get_subdevdata(sd);
301
302 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_ADV7175, 0);
303}
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305/* ----------------------------------------------------------------------- */
306
Hans Verkuil35631dc2009-02-19 14:56:37 -0300307static const struct v4l2_subdev_core_ops adv7175_core_ops = {
308 .g_chip_ident = adv7175_g_chip_ident,
309 .init = adv7175_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Hans Verkuil35631dc2009-02-19 14:56:37 -0300312static const struct v4l2_subdev_video_ops adv7175_video_ops = {
313 .s_std_output = adv7175_s_std_output,
314 .s_routing = adv7175_s_routing,
315};
316
317static const struct v4l2_subdev_ops adv7175_ops = {
318 .core = &adv7175_core_ops,
319 .video = &adv7175_video_ops,
320};
321
322/* ----------------------------------------------------------------------- */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300323
Hans Verkuilbba0d982008-09-07 07:58:46 -0300324static int adv7175_probe(struct i2c_client *client,
325 const struct i2c_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 struct adv7175 *encoder;
Hans Verkuil35631dc2009-02-19 14:56:37 -0300329 struct v4l2_subdev *sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 /* Check if the adapter supports the needed features */
Hans Verkuilbba0d982008-09-07 07:58:46 -0300332 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
333 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Hans Verkuilbba0d982008-09-07 07:58:46 -0300335 v4l_info(client, "chip found @ 0x%x (%s)\n",
336 client->addr << 1, client->adapter->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Panagiotis Issaris74081872006-01-11 19:40:56 -0200338 encoder = kzalloc(sizeof(struct adv7175), GFP_KERNEL);
Hans Verkuilbba0d982008-09-07 07:58:46 -0300339 if (encoder == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return -ENOMEM;
Hans Verkuil35631dc2009-02-19 14:56:37 -0300341 sd = &encoder->sd;
342 v4l2_i2c_subdev_init(sd, client, &adv7175_ops);
Hans Verkuil107063c2009-02-18 17:26:06 -0300343 encoder->norm = V4L2_STD_NTSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 encoder->input = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Hans Verkuil35631dc2009-02-19 14:56:37 -0300346 i = adv7175_write_block(sd, init_common, sizeof(init_common));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (i >= 0) {
Hans Verkuil35631dc2009-02-19 14:56:37 -0300348 i = adv7175_write(sd, 0x07, TR0MODE | TR0RST);
349 i = adv7175_write(sd, 0x07, TR0MODE);
350 i = adv7175_read(sd, 0x12);
351 v4l2_dbg(1, debug, sd, "revision %d\n", i & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
Hans Verkuilbba0d982008-09-07 07:58:46 -0300353 if (i < 0)
Hans Verkuil35631dc2009-02-19 14:56:37 -0300354 v4l2_dbg(1, debug, sd, "init error 0x%x\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return 0;
356}
357
Hans Verkuilbba0d982008-09-07 07:58:46 -0300358static int adv7175_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Hans Verkuil35631dc2009-02-19 14:56:37 -0300360 struct v4l2_subdev *sd = i2c_get_clientdata(client);
361
362 v4l2_device_unregister_subdev(sd);
363 kfree(to_adv7175(sd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return 0;
365}
366
367/* ----------------------------------------------------------------------- */
368
Hans Verkuilbba0d982008-09-07 07:58:46 -0300369static const struct i2c_device_id adv7175_id[] = {
370 { "adv7175", 0 },
371 { "adv7176", 0 },
372 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373};
Hans Verkuilbba0d982008-09-07 07:58:46 -0300374MODULE_DEVICE_TABLE(i2c, adv7175_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Hans Verkuilbba0d982008-09-07 07:58:46 -0300376static struct v4l2_i2c_driver_data v4l2_i2c_data = {
377 .name = "adv7175",
Hans Verkuilbba0d982008-09-07 07:58:46 -0300378 .probe = adv7175_probe,
379 .remove = adv7175_remove,
380 .id_table = adv7175_id,
381};