blob: ebafe78cb4c04921c8994a5965666594c24df87a [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/videodev.h>
Mauro Carvalho Chehab18f3fa12007-07-02 15:39:57 -030038#include <linux/video_encoder.h>
Hans Verkuilb9a21f82008-09-07 07:58:20 -030039#include <media/v4l2-common.h>
40#include <media/v4l2-i2c-drv-legacy.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
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 adv7170 {
53 unsigned char reg[128];
54
Hans Verkuil107063c2009-02-18 17:26:06 -030055 v4l2_std_id norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 int input;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 int bright;
58 int contrast;
59 int hue;
60 int sat;
61};
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static char *inputs[] = { "pass_through", "play_back" };
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/* ----------------------------------------------------------------------- */
66
Hans Verkuilb9a21f82008-09-07 07:58:20 -030067static inline int adv7170_write(struct i2c_client *client, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 struct adv7170 *encoder = i2c_get_clientdata(client);
70
71 encoder->reg[reg] = value;
72 return i2c_smbus_write_byte_data(client, reg, value);
73}
74
Hans Verkuilb9a21f82008-09-07 07:58:20 -030075static inline int adv7170_read(struct i2c_client *client, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 return i2c_smbus_read_byte_data(client, reg);
78}
79
Hans Verkuilb9a21f82008-09-07 07:58:20 -030080static int adv7170_write_block(struct i2c_client *client,
81 const u8 *data, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83 int ret = -1;
84 u8 reg;
85
86 /* the adv7170 has an autoincrement function, use it if
87 * the adapter understands raw I2C */
88 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
89 /* do raw I2C, not smbus compatible */
90 struct adv7170 *encoder = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 u8 block_data[32];
Jean Delvare9aa45e32006-03-22 03:48:35 -030092 int block_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 while (len >= 2) {
Jean Delvare9aa45e32006-03-22 03:48:35 -030095 block_len = 0;
96 block_data[block_len++] = reg = data[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 do {
Jean Delvare9aa45e32006-03-22 03:48:35 -030098 block_data[block_len++] =
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 encoder->reg[reg++] = data[1];
100 len -= 2;
101 data += 2;
Hans Verkuilb9a21f82008-09-07 07:58:20 -0300102 } while (len >= 2 && data[0] == reg && block_len < 32);
103 ret = i2c_master_send(client, block_data, block_len);
104 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 break;
106 }
107 } else {
108 /* do some slow I2C emulation kind of thing */
109 while (len >= 2) {
110 reg = *data++;
Hans Verkuilb9a21f82008-09-07 07:58:20 -0300111 ret = adv7170_write(client, reg, *data++);
112 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 break;
114 len -= 2;
115 }
116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return ret;
118}
119
120/* ----------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122#define TR0MODE 0x4c
123#define TR0RST 0x80
124
125#define TR1CAPT 0x00
126#define TR1PLAY 0x00
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128static const unsigned char init_NTSC[] = {
129 0x00, 0x10, // MR0
130 0x01, 0x20, // MR1
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300131 0x02, 0x0e, // MR2 RTC control: bits 2 and 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 0x03, 0x80, // MR3
133 0x04, 0x30, // MR4
134 0x05, 0x00, // Reserved
135 0x06, 0x00, // Reserved
136 0x07, TR0MODE, // TM0
137 0x08, TR1CAPT, // TM1
138 0x09, 0x16, // Fsc0
139 0x0a, 0x7c, // Fsc1
140 0x0b, 0xf0, // Fsc2
141 0x0c, 0x21, // Fsc3
142 0x0d, 0x00, // Subcarrier Phase
143 0x0e, 0x00, // Closed Capt. Ext 0
144 0x0f, 0x00, // Closed Capt. Ext 1
145 0x10, 0x00, // Closed Capt. 0
146 0x11, 0x00, // Closed Capt. 1
147 0x12, 0x00, // Pedestal Ctl 0
148 0x13, 0x00, // Pedestal Ctl 1
149 0x14, 0x00, // Pedestal Ctl 2
150 0x15, 0x00, // Pedestal Ctl 3
151 0x16, 0x00, // CGMS_WSS_0
152 0x17, 0x00, // CGMS_WSS_1
153 0x18, 0x00, // CGMS_WSS_2
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300154 0x19, 0x00, // Teletext Ctl
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155};
156
157static const unsigned char init_PAL[] = {
158 0x00, 0x71, // MR0
159 0x01, 0x20, // MR1
160 0x02, 0x0e, // MR2 RTC control: bits 2 and 1
161 0x03, 0x80, // MR3
162 0x04, 0x30, // MR4
163 0x05, 0x00, // Reserved
164 0x06, 0x00, // Reserved
165 0x07, TR0MODE, // TM0
166 0x08, TR1CAPT, // TM1
167 0x09, 0xcb, // Fsc0
168 0x0a, 0x8a, // Fsc1
169 0x0b, 0x09, // Fsc2
170 0x0c, 0x2a, // Fsc3
171 0x0d, 0x00, // Subcarrier Phase
172 0x0e, 0x00, // Closed Capt. Ext 0
173 0x0f, 0x00, // Closed Capt. Ext 1
174 0x10, 0x00, // Closed Capt. 0
175 0x11, 0x00, // Closed Capt. 1
176 0x12, 0x00, // Pedestal Ctl 0
177 0x13, 0x00, // Pedestal Ctl 1
178 0x14, 0x00, // Pedestal Ctl 2
179 0x15, 0x00, // Pedestal Ctl 3
180 0x16, 0x00, // CGMS_WSS_0
181 0x17, 0x00, // CGMS_WSS_1
182 0x18, 0x00, // CGMS_WSS_2
183 0x19, 0x00, // Teletext Ctl
184};
185
186
Hans Verkuilb9a21f82008-09-07 07:58:20 -0300187static int adv7170_command(struct i2c_client *client, unsigned cmd, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 struct adv7170 *encoder = i2c_get_clientdata(client);
190
191 switch (cmd) {
Hans Verkuil107063c2009-02-18 17:26:06 -0300192 case VIDIOC_INT_INIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193#if 0
194 /* This is just for testing!!! */
195 adv7170_write_block(client, init_common,
196 sizeof(init_common));
197 adv7170_write(client, 0x07, TR0MODE | TR0RST);
198 adv7170_write(client, 0x07, TR0MODE);
199#endif
200 break;
201
Hans Verkuil107063c2009-02-18 17:26:06 -0300202 case VIDIOC_INT_S_STD_OUTPUT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 {
Hans Verkuil107063c2009-02-18 17:26:06 -0300204 v4l2_std_id iarg = *(v4l2_std_id *) arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Hans Verkuil107063c2009-02-18 17:26:06 -0300206 v4l_dbg(1, debug, client, "set norm %llx\n", iarg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Hans Verkuil107063c2009-02-18 17:26:06 -0300208 if (iarg & V4L2_STD_NTSC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 adv7170_write_block(client, init_NTSC,
210 sizeof(init_NTSC));
211 if (encoder->input == 0)
212 adv7170_write(client, 0x02, 0x0e); // Enable genlock
213 adv7170_write(client, 0x07, TR0MODE | TR0RST);
214 adv7170_write(client, 0x07, TR0MODE);
Hans Verkuil107063c2009-02-18 17:26:06 -0300215 } else if (iarg & V4L2_STD_PAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 adv7170_write_block(client, init_PAL,
217 sizeof(init_PAL));
218 if (encoder->input == 0)
219 adv7170_write(client, 0x02, 0x0e); // Enable genlock
220 adv7170_write(client, 0x07, TR0MODE | TR0RST);
221 adv7170_write(client, 0x07, TR0MODE);
Hans Verkuil107063c2009-02-18 17:26:06 -0300222 } else {
223 v4l_dbg(1, debug, client, "illegal norm: %llx\n", iarg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
Hans Verkuil107063c2009-02-18 17:26:06 -0300226 v4l_dbg(1, debug, client, "switched to %llx\n", iarg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 encoder->norm = iarg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 break;
Hans Verkuilb9a21f82008-09-07 07:58:20 -0300229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Hans Verkuil107063c2009-02-18 17:26:06 -0300231 case VIDIOC_INT_S_VIDEO_ROUTING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 {
Hans Verkuil107063c2009-02-18 17:26:06 -0300233 struct v4l2_routing *route = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 /* RJ: *iarg = 0: input is from decoder
236 *iarg = 1: input is from ZR36060
237 *iarg = 2: color bar */
238
Hans Verkuilb9a21f82008-09-07 07:58:20 -0300239 v4l_dbg(1, debug, client, "set input from %s\n",
Hans Verkuil107063c2009-02-18 17:26:06 -0300240 route->input == 0 ? "decoder" : "ZR36060");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Hans Verkuil107063c2009-02-18 17:26:06 -0300242 switch (route->input) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 case 0:
244 adv7170_write(client, 0x01, 0x20);
245 adv7170_write(client, 0x08, TR1CAPT); /* TR1 */
246 adv7170_write(client, 0x02, 0x0e); // Enable genlock
247 adv7170_write(client, 0x07, TR0MODE | TR0RST);
248 adv7170_write(client, 0x07, TR0MODE);
Hans Verkuilb9a21f82008-09-07 07:58:20 -0300249 /* udelay(10); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 break;
251
252 case 1:
253 adv7170_write(client, 0x01, 0x00);
254 adv7170_write(client, 0x08, TR1PLAY); /* TR1 */
255 adv7170_write(client, 0x02, 0x08);
256 adv7170_write(client, 0x07, TR0MODE | TR0RST);
257 adv7170_write(client, 0x07, TR0MODE);
Hans Verkuilb9a21f82008-09-07 07:58:20 -0300258 /* udelay(10); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 break;
260
261 default:
Hans Verkuil107063c2009-02-18 17:26:06 -0300262 v4l_dbg(1, debug, client, "illegal input: %d\n", route->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
Hans Verkuil107063c2009-02-18 17:26:06 -0300265 v4l_dbg(1, debug, client, "switched to %s\n", inputs[route->input]);
266 encoder->input = route->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 break;
Hans Verkuilb9a21f82008-09-07 07:58:20 -0300268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 default:
271 return -EINVAL;
272 }
273
274 return 0;
275}
276
277/* ----------------------------------------------------------------------- */
278
Hans Verkuilb9a21f82008-09-07 07:58:20 -0300279static unsigned short normal_i2c[] = {
280 0xd4 >> 1, 0xd6 >> 1, /* adv7170 IDs */
281 0x54 >> 1, 0x56 >> 1, /* adv7171 IDs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 I2C_CLIENT_END
283};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Hans Verkuilb9a21f82008-09-07 07:58:20 -0300285I2C_CLIENT_INSMOD;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300286
Hans Verkuilb9a21f82008-09-07 07:58:20 -0300287static int adv7170_probe(struct i2c_client *client,
288 const struct i2c_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 struct adv7170 *encoder;
Hans Verkuilb9a21f82008-09-07 07:58:20 -0300291 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 /* Check if the adapter supports the needed features */
Hans Verkuilb9a21f82008-09-07 07:58:20 -0300294 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
295 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Hans Verkuilb9a21f82008-09-07 07:58:20 -0300297 v4l_info(client, "chip found @ 0x%x (%s)\n",
298 client->addr << 1, client->adapter->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Panagiotis Issaris74081872006-01-11 19:40:56 -0200300 encoder = kzalloc(sizeof(struct adv7170), GFP_KERNEL);
Hans Verkuilb9a21f82008-09-07 07:58:20 -0300301 if (encoder == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return -ENOMEM;
Hans Verkuil107063c2009-02-18 17:26:06 -0300303 encoder->norm = V4L2_STD_NTSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 encoder->input = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 i2c_set_clientdata(client, encoder);
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 i = adv7170_write_block(client, init_NTSC, sizeof(init_NTSC));
308 if (i >= 0) {
309 i = adv7170_write(client, 0x07, TR0MODE | TR0RST);
310 i = adv7170_write(client, 0x07, TR0MODE);
311 i = adv7170_read(client, 0x12);
Hans Verkuilb9a21f82008-09-07 07:58:20 -0300312 v4l_dbg(1, debug, client, "revision %d\n", i & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
Hans Verkuilb9a21f82008-09-07 07:58:20 -0300314 if (i < 0)
315 v4l_dbg(1, debug, client, "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 Verkuilb9a21f82008-09-07 07:58:20 -0300321 kfree(i2c_get_clientdata(client));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return 0;
323}
324
325/* ----------------------------------------------------------------------- */
326
Hans Verkuilb9a21f82008-09-07 07:58:20 -0300327static const struct i2c_device_id adv7170_id[] = {
328 { "adv7170", 0 },
329 { "adv7171", 0 },
330 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331};
Hans Verkuilb9a21f82008-09-07 07:58:20 -0300332MODULE_DEVICE_TABLE(i2c, adv7170_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Hans Verkuilb9a21f82008-09-07 07:58:20 -0300334static struct v4l2_i2c_driver_data v4l2_i2c_data = {
335 .name = "adv7170",
336 .driverid = I2C_DRIVERID_ADV7170,
337 .command = adv7170_command,
338 .probe = adv7170_probe,
339 .remove = adv7170_remove,
340 .id_table = adv7170_id,
341};