blob: 43fd1d24cdebe350eb2745a5338d5ed889e62498 [file] [log] [blame]
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * adv7170 - adv7170, adv7171 video encoder driver version 0.0.1
3 *
4 * Copyright (C) 2002 Maxim Yevtyushkin <max@linuxmedialabs.com>
5 *
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03006 * Based on adv7176 driver by:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
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>
Mauro Carvalho Chehab18f3fa12007-07-02 15:39:57 -030032#include <linux/types.h>
Hans Verkuilb9a21f82008-09-07 07:58:20 -030033#include <linux/ioctl.h>
Mauro Carvalho Chehab18f3fa12007-07-02 15:39:57 -030034#include <asm/uaccess.h>
Hans Verkuilb9a21f82008-09-07 07:58:20 -030035#include <linux/i2c.h>
36#include <linux/i2c-id.h>
Hans Verkuil7d9ef212009-02-19 14:47:22 -030037#include <linux/videodev2.h>
38#include <media/v4l2-device.h>
39#include <media/v4l2-chip-ident.h>
Hans Verkuil2d266982009-02-19 17:41:19 -030040#include <media/v4l2-i2c-drv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42MODULE_DESCRIPTION("Analog Devices ADV7170 video encoder driver");
43MODULE_AUTHOR("Maxim Yevtyushkin");
44MODULE_LICENSE("GPL");
45
Hans Verkuil7d9ef212009-02-19 14:47:22 -030046
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030047static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048module_param(debug, int, 0);
49MODULE_PARM_DESC(debug, "Debug level (0-1)");
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/* ----------------------------------------------------------------------- */
52
53struct adv7170 {
Hans Verkuil7d9ef212009-02-19 14:47:22 -030054 struct v4l2_subdev sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 unsigned char reg[128];
56
Hans Verkuil107063c2009-02-18 17:26:06 -030057 v4l2_std_id norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 int input;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
60
Hans Verkuil7d9ef212009-02-19 14:47:22 -030061static inline struct adv7170 *to_adv7170(struct v4l2_subdev *sd)
62{
63 return container_of(sd, struct adv7170, sd);
64}
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static char *inputs[] = { "pass_through", "play_back" };
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/* ----------------------------------------------------------------------- */
69
Hans Verkuil7d9ef212009-02-19 14:47:22 -030070static inline int adv7170_write(struct v4l2_subdev *sd, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Hans Verkuil7d9ef212009-02-19 14:47:22 -030072 struct i2c_client *client = v4l2_get_subdevdata(sd);
73 struct adv7170 *encoder = to_adv7170(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 encoder->reg[reg] = value;
76 return i2c_smbus_write_byte_data(client, reg, value);
77}
78
Hans Verkuil7d9ef212009-02-19 14:47:22 -030079static inline int adv7170_read(struct v4l2_subdev *sd, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Hans Verkuil7d9ef212009-02-19 14:47:22 -030081 struct i2c_client *client = v4l2_get_subdevdata(sd);
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 return i2c_smbus_read_byte_data(client, reg);
84}
85
Hans Verkuil7d9ef212009-02-19 14:47:22 -030086static int adv7170_write_block(struct v4l2_subdev *sd,
Hans Verkuilb9a21f82008-09-07 07:58:20 -030087 const u8 *data, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Hans Verkuil7d9ef212009-02-19 14:47:22 -030089 struct i2c_client *client = v4l2_get_subdevdata(sd);
90 struct adv7170 *encoder = to_adv7170(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 int ret = -1;
92 u8 reg;
93
94 /* the adv7170 has an autoincrement function, use it if
95 * the adapter understands raw I2C */
96 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
97 /* do raw I2C, not smbus compatible */
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 u8 block_data[32];
Jean Delvare9aa45e32006-03-22 03:48:35 -030099 int block_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 while (len >= 2) {
Jean Delvare9aa45e32006-03-22 03:48:35 -0300102 block_len = 0;
103 block_data[block_len++] = reg = data[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 do {
Jean Delvare9aa45e32006-03-22 03:48:35 -0300105 block_data[block_len++] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 encoder->reg[reg++] = data[1];
107 len -= 2;
108 data += 2;
Hans Verkuilb9a21f82008-09-07 07:58:20 -0300109 } while (len >= 2 && data[0] == reg && block_len < 32);
110 ret = i2c_master_send(client, block_data, block_len);
111 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 break;
113 }
114 } else {
115 /* do some slow I2C emulation kind of thing */
116 while (len >= 2) {
117 reg = *data++;
Hans Verkuil7d9ef212009-02-19 14:47:22 -0300118 ret = adv7170_write(sd, reg, *data++);
Hans Verkuilb9a21f82008-09-07 07:58:20 -0300119 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 break;
121 len -= 2;
122 }
123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return ret;
125}
126
127/* ----------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129#define TR0MODE 0x4c
130#define TR0RST 0x80
131
132#define TR1CAPT 0x00
133#define TR1PLAY 0x00
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135static const unsigned char init_NTSC[] = {
Hans Verkuil7d9ef212009-02-19 14:47:22 -0300136 0x00, 0x10, /* MR0 */
137 0x01, 0x20, /* MR1 */
138 0x02, 0x0e, /* MR2 RTC control: bits 2 and 1 */
139 0x03, 0x80, /* MR3 */
140 0x04, 0x30, /* MR4 */
141 0x05, 0x00, /* Reserved */
142 0x06, 0x00, /* Reserved */
143 0x07, TR0MODE, /* TM0 */
144 0x08, TR1CAPT, /* TM1 */
145 0x09, 0x16, /* Fsc0 */
146 0x0a, 0x7c, /* Fsc1 */
147 0x0b, 0xf0, /* Fsc2 */
148 0x0c, 0x21, /* Fsc3 */
149 0x0d, 0x00, /* Subcarrier Phase */
150 0x0e, 0x00, /* Closed Capt. Ext 0 */
151 0x0f, 0x00, /* Closed Capt. Ext 1 */
152 0x10, 0x00, /* Closed Capt. 0 */
153 0x11, 0x00, /* Closed Capt. 1 */
154 0x12, 0x00, /* Pedestal Ctl 0 */
155 0x13, 0x00, /* Pedestal Ctl 1 */
156 0x14, 0x00, /* Pedestal Ctl 2 */
157 0x15, 0x00, /* Pedestal Ctl 3 */
158 0x16, 0x00, /* CGMS_WSS_0 */
159 0x17, 0x00, /* CGMS_WSS_1 */
160 0x18, 0x00, /* CGMS_WSS_2 */
161 0x19, 0x00, /* Teletext Ctl */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162};
163
164static const unsigned char init_PAL[] = {
Hans Verkuil7d9ef212009-02-19 14:47:22 -0300165 0x00, 0x71, /* MR0 */
166 0x01, 0x20, /* MR1 */
167 0x02, 0x0e, /* MR2 RTC control: bits 2 and 1 */
168 0x03, 0x80, /* MR3 */
169 0x04, 0x30, /* MR4 */
170 0x05, 0x00, /* Reserved */
171 0x06, 0x00, /* Reserved */
172 0x07, TR0MODE, /* TM0 */
173 0x08, TR1CAPT, /* TM1 */
174 0x09, 0xcb, /* Fsc0 */
175 0x0a, 0x8a, /* Fsc1 */
176 0x0b, 0x09, /* Fsc2 */
177 0x0c, 0x2a, /* Fsc3 */
178 0x0d, 0x00, /* Subcarrier Phase */
179 0x0e, 0x00, /* Closed Capt. Ext 0 */
180 0x0f, 0x00, /* Closed Capt. Ext 1 */
181 0x10, 0x00, /* Closed Capt. 0 */
182 0x11, 0x00, /* Closed Capt. 1 */
183 0x12, 0x00, /* Pedestal Ctl 0 */
184 0x13, 0x00, /* Pedestal Ctl 1 */
185 0x14, 0x00, /* Pedestal Ctl 2 */
186 0x15, 0x00, /* Pedestal Ctl 3 */
187 0x16, 0x00, /* CGMS_WSS_0 */
188 0x17, 0x00, /* CGMS_WSS_1 */
189 0x18, 0x00, /* CGMS_WSS_2 */
190 0x19, 0x00, /* Teletext Ctl */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191};
192
193
Hans Verkuil7d9ef212009-02-19 14:47:22 -0300194static int adv7170_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Hans Verkuil7d9ef212009-02-19 14:47:22 -0300196 struct adv7170 *encoder = to_adv7170(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Hans Verkuil7d9ef212009-02-19 14:47:22 -0300198 v4l2_dbg(1, debug, sd, "set norm %llx\n", std);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Hans Verkuil7d9ef212009-02-19 14:47:22 -0300200 if (std & V4L2_STD_NTSC) {
201 adv7170_write_block(sd, init_NTSC, sizeof(init_NTSC));
202 if (encoder->input == 0)
203 adv7170_write(sd, 0x02, 0x0e); /* Enable genlock */
204 adv7170_write(sd, 0x07, TR0MODE | TR0RST);
205 adv7170_write(sd, 0x07, TR0MODE);
206 } else if (std & V4L2_STD_PAL) {
207 adv7170_write_block(sd, init_PAL, sizeof(init_PAL));
208 if (encoder->input == 0)
209 adv7170_write(sd, 0x02, 0x0e); /* Enable genlock */
210 adv7170_write(sd, 0x07, TR0MODE | TR0RST);
211 adv7170_write(sd, 0x07, TR0MODE);
212 } else {
213 v4l2_dbg(1, debug, sd, "illegal norm: %llx\n", std);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return -EINVAL;
215 }
Hans Verkuil7d9ef212009-02-19 14:47:22 -0300216 v4l2_dbg(1, debug, sd, "switched to %llx\n", std);
217 encoder->norm = std;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return 0;
219}
220
Hans Verkuil7d9ef212009-02-19 14:47:22 -0300221static int adv7170_s_routing(struct v4l2_subdev *sd, const struct v4l2_routing *route)
222{
223 struct adv7170 *encoder = to_adv7170(sd);
224
225 /* RJ: route->input = 0: input is from decoder
226 route->input = 1: input is from ZR36060
227 route->input = 2: color bar */
228
229 v4l2_dbg(1, debug, sd, "set input from %s\n",
230 route->input == 0 ? "decoder" : "ZR36060");
231
232 switch (route->input) {
233 case 0:
234 adv7170_write(sd, 0x01, 0x20);
235 adv7170_write(sd, 0x08, TR1CAPT); /* TR1 */
236 adv7170_write(sd, 0x02, 0x0e); /* Enable genlock */
237 adv7170_write(sd, 0x07, TR0MODE | TR0RST);
238 adv7170_write(sd, 0x07, TR0MODE);
239 /* udelay(10); */
240 break;
241
242 case 1:
243 adv7170_write(sd, 0x01, 0x00);
244 adv7170_write(sd, 0x08, TR1PLAY); /* TR1 */
245 adv7170_write(sd, 0x02, 0x08);
246 adv7170_write(sd, 0x07, TR0MODE | TR0RST);
247 adv7170_write(sd, 0x07, TR0MODE);
248 /* udelay(10); */
249 break;
250
251 default:
252 v4l2_dbg(1, debug, sd, "illegal input: %d\n", route->input);
253 return -EINVAL;
254 }
255 v4l2_dbg(1, debug, sd, "switched to %s\n", inputs[route->input]);
256 encoder->input = route->input;
257 return 0;
258}
259
260static int adv7170_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
261{
262 struct i2c_client *client = v4l2_get_subdevdata(sd);
263
Hans Verkuil35631dc2009-02-19 14:56:37 -0300264 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_ADV7170, 0);
Hans Verkuil7d9ef212009-02-19 14:47:22 -0300265}
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267/* ----------------------------------------------------------------------- */
268
Hans Verkuil7d9ef212009-02-19 14:47:22 -0300269static const struct v4l2_subdev_core_ops adv7170_core_ops = {
270 .g_chip_ident = adv7170_g_chip_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Hans Verkuil7d9ef212009-02-19 14:47:22 -0300273static const struct v4l2_subdev_video_ops adv7170_video_ops = {
274 .s_std_output = adv7170_s_std_output,
275 .s_routing = adv7170_s_routing,
276};
277
278static const struct v4l2_subdev_ops adv7170_ops = {
279 .core = &adv7170_core_ops,
280 .video = &adv7170_video_ops,
281};
282
283/* ----------------------------------------------------------------------- */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300284
Hans Verkuilb9a21f82008-09-07 07:58:20 -0300285static int adv7170_probe(struct i2c_client *client,
286 const struct i2c_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 struct adv7170 *encoder;
Hans Verkuil7d9ef212009-02-19 14:47:22 -0300289 struct v4l2_subdev *sd;
Hans Verkuilb9a21f82008-09-07 07:58:20 -0300290 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 /* Check if the adapter supports the needed features */
Hans Verkuilb9a21f82008-09-07 07:58:20 -0300293 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
294 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Hans Verkuilb9a21f82008-09-07 07:58:20 -0300296 v4l_info(client, "chip found @ 0x%x (%s)\n",
297 client->addr << 1, client->adapter->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Panagiotis Issaris74081872006-01-11 19:40:56 -0200299 encoder = kzalloc(sizeof(struct adv7170), GFP_KERNEL);
Hans Verkuilb9a21f82008-09-07 07:58:20 -0300300 if (encoder == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return -ENOMEM;
Hans Verkuil7d9ef212009-02-19 14:47:22 -0300302 sd = &encoder->sd;
303 v4l2_i2c_subdev_init(sd, client, &adv7170_ops);
Hans Verkuil107063c2009-02-18 17:26:06 -0300304 encoder->norm = V4L2_STD_NTSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 encoder->input = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Hans Verkuil7d9ef212009-02-19 14:47:22 -0300307 i = adv7170_write_block(sd, init_NTSC, sizeof(init_NTSC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (i >= 0) {
Hans Verkuil7d9ef212009-02-19 14:47:22 -0300309 i = adv7170_write(sd, 0x07, TR0MODE | TR0RST);
310 i = adv7170_write(sd, 0x07, TR0MODE);
311 i = adv7170_read(sd, 0x12);
312 v4l2_dbg(1, debug, sd, "revision %d\n", i & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
Hans Verkuilb9a21f82008-09-07 07:58:20 -0300314 if (i < 0)
Hans Verkuil7d9ef212009-02-19 14:47:22 -0300315 v4l2_dbg(1, debug, sd, "init error 0x%x\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return 0;
317}
318
Hans Verkuilb9a21f82008-09-07 07:58:20 -0300319static int adv7170_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Hans Verkuil7d9ef212009-02-19 14:47:22 -0300321 struct v4l2_subdev *sd = i2c_get_clientdata(client);
322
323 v4l2_device_unregister_subdev(sd);
324 kfree(to_adv7170(sd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return 0;
326}
327
328/* ----------------------------------------------------------------------- */
329
Hans Verkuilb9a21f82008-09-07 07:58:20 -0300330static const struct i2c_device_id adv7170_id[] = {
331 { "adv7170", 0 },
332 { "adv7171", 0 },
333 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334};
Hans Verkuilb9a21f82008-09-07 07:58:20 -0300335MODULE_DEVICE_TABLE(i2c, adv7170_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Hans Verkuilb9a21f82008-09-07 07:58:20 -0300337static struct v4l2_i2c_driver_data v4l2_i2c_data = {
338 .name = "adv7170",
Hans Verkuilb9a21f82008-09-07 07:58:20 -0300339 .probe = adv7170_probe,
340 .remove = adv7170_remove,
341 .id_table = adv7170_id,
342};