]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/media/video/adv7175.c
Merge branch 'drm-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/airlied...
[mirror_ubuntu-hirsute-kernel.git] / drivers / media / video / adv7175.c
CommitLineData
d56410e0 1/*
1da177e4
LT
2 * 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>
18f3fa1e 28#include <linux/types.h>
bba0d982 29#include <linux/ioctl.h>
18f3fa1e 30#include <asm/uaccess.h>
bba0d982
HV
31#include <linux/i2c.h>
32#include <linux/i2c-id.h>
35631dcc
HV
33#include <linux/videodev2.h>
34#include <media/v4l2-device.h>
35#include <media/v4l2-chip-ident.h>
2d26698e 36#include <media/v4l2-i2c-drv.h>
1da177e4
LT
37
38MODULE_DESCRIPTION("Analog Devices ADV7175 video encoder driver");
39MODULE_AUTHOR("Dave Perks");
40MODULE_LICENSE("GPL");
41
35631dcc
HV
42#define I2C_ADV7175 0xd4
43#define I2C_ADV7176 0x54
44
35631dcc 45
ff699e6b 46static int debug;
1da177e4
LT
47module_param(debug, int, 0);
48MODULE_PARM_DESC(debug, "Debug level (0-1)");
49
1da177e4
LT
50/* ----------------------------------------------------------------------- */
51
52struct adv7175 {
35631dcc 53 struct v4l2_subdev sd;
107063c6 54 v4l2_std_id norm;
1da177e4 55 int input;
1da177e4
LT
56};
57
35631dcc
HV
58static inline struct adv7175 *to_adv7175(struct v4l2_subdev *sd)
59{
60 return container_of(sd, struct adv7175, sd);
61}
1da177e4 62
1da177e4 63static char *inputs[] = { "pass_through", "play_back", "color_bar" };
1da177e4
LT
64
65/* ----------------------------------------------------------------------- */
66
35631dcc 67static inline int adv7175_write(struct v4l2_subdev *sd, u8 reg, u8 value)
1da177e4 68{
35631dcc
HV
69 struct i2c_client *client = v4l2_get_subdevdata(sd);
70
1da177e4
LT
71 return i2c_smbus_write_byte_data(client, reg, value);
72}
73
35631dcc 74static inline int adv7175_read(struct v4l2_subdev *sd, u8 reg)
1da177e4 75{
35631dcc
HV
76 struct i2c_client *client = v4l2_get_subdevdata(sd);
77
1da177e4
LT
78 return i2c_smbus_read_byte_data(client, reg);
79}
80
35631dcc 81static int adv7175_write_block(struct v4l2_subdev *sd,
bba0d982 82 const u8 *data, unsigned int len)
1da177e4 83{
35631dcc 84 struct i2c_client *client = v4l2_get_subdevdata(sd);
1da177e4
LT
85 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 */
1da177e4 92 u8 block_data[32];
9aa45e34 93 int block_len;
1da177e4 94
1da177e4 95 while (len >= 2) {
9aa45e34
JD
96 block_len = 0;
97 block_data[block_len++] = reg = data[0];
1da177e4 98 do {
9aa45e34 99 block_data[block_len++] = data[1];
2467a670 100 reg++;
1da177e4
LT
101 len -= 2;
102 data += 2;
bba0d982
HV
103 } while (len >= 2 && data[0] == reg && block_len < 32);
104 ret = i2c_master_send(client, block_data, block_len);
105 if (ret < 0)
1da177e4
LT
106 break;
107 }
108 } else {
109 /* do some slow I2C emulation kind of thing */
110 while (len >= 2) {
111 reg = *data++;
35631dcc 112 ret = adv7175_write(sd, reg, *data++);
bba0d982 113 if (ret < 0)
1da177e4
LT
114 break;
115 len -= 2;
116 }
117 }
118
119 return ret;
120}
121
35631dcc 122static void set_subcarrier_freq(struct v4l2_subdev *sd, int pass_through)
1da177e4
LT
123{
124 /* for some reason pass_through NTSC needs
125 * a different sub-carrier freq to remain stable. */
bba0d982 126 if (pass_through)
35631dcc 127 adv7175_write(sd, 0x02, 0x00);
1da177e4 128 else
35631dcc 129 adv7175_write(sd, 0x02, 0x55);
1da177e4 130
35631dcc
HV
131 adv7175_write(sd, 0x03, 0x55);
132 adv7175_write(sd, 0x04, 0x55);
133 adv7175_write(sd, 0x05, 0x25);
1da177e4
LT
134}
135
1da177e4 136/* ----------------------------------------------------------------------- */
bba0d982 137/* Output filter: S-Video Composite */
1da177e4 138
bba0d982
HV
139#define MR050 0x11 /* 0x09 */
140#define MR060 0x14 /* 0x0c */
1da177e4 141
bba0d982 142/* ----------------------------------------------------------------------- */
1da177e4
LT
143
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
35631dcc
HV
193static int adv7175_init(struct v4l2_subdev *sd, u32 val)
194{
195 /* 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}
201
202static int adv7175_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std)
1da177e4 203{
35631dcc
HV
204 struct adv7175 *encoder = to_adv7175(sd);
205
206 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 {
cc5cef8e
HV
231 v4l2_dbg(1, debug, sd, "illegal norm: %llx\n",
232 (unsigned long long)std);
35631dcc
HV
233 return -EINVAL;
234 }
cc5cef8e 235 v4l2_dbg(1, debug, sd, "switched to %llx\n", (unsigned long long)std);
35631dcc
HV
236 encoder->norm = std;
237 return 0;
238}
239
5325b427
HV
240static int adv7175_s_routing(struct v4l2_subdev *sd,
241 u32 input, u32 output, u32 config)
35631dcc
HV
242{
243 struct adv7175 *encoder = to_adv7175(sd);
244
5325b427
HV
245 /* RJ: input = 0: input is from decoder
246 input = 1: input is from ZR36060
247 input = 2: color bar */
35631dcc 248
5325b427 249 switch (input) {
35631dcc
HV
250 case 0:
251 adv7175_write(sd, 0x01, 0x00);
252
253 if (encoder->norm & V4L2_STD_NTSC)
254 set_subcarrier_freq(sd, 1);
255
256 adv7175_write(sd, 0x0c, TR1CAPT); /* TR1 */
257 if (encoder->norm & V4L2_STD_SECAM)
258 adv7175_write(sd, 0x0d, 0x49); /* Disable genlock */
259 else
260 adv7175_write(sd, 0x0d, 0x4f); /* Enable genlock */
261 adv7175_write(sd, 0x07, TR0MODE | TR0RST);
262 adv7175_write(sd, 0x07, TR0MODE);
263 /*udelay(10);*/
d56410e0 264 break;
1da177e4 265
35631dcc
HV
266 case 1:
267 adv7175_write(sd, 0x01, 0x00);
268
269 if (encoder->norm & V4L2_STD_NTSC)
270 set_subcarrier_freq(sd, 0);
271
272 adv7175_write(sd, 0x0c, TR1PLAY); /* TR1 */
273 adv7175_write(sd, 0x0d, 0x49);
274 adv7175_write(sd, 0x07, TR0MODE | TR0RST);
275 adv7175_write(sd, 0x07, TR0MODE);
276 /* udelay(10); */
1da177e4
LT
277 break;
278
35631dcc
HV
279 case 2:
280 adv7175_write(sd, 0x01, 0x80);
281
282 if (encoder->norm & V4L2_STD_NTSC)
283 set_subcarrier_freq(sd, 0);
284
285 adv7175_write(sd, 0x0d, 0x49);
286 adv7175_write(sd, 0x07, TR0MODE | TR0RST);
287 adv7175_write(sd, 0x07, TR0MODE);
288 /* udelay(10); */
1da177e4
LT
289 break;
290
1da177e4 291 default:
5325b427 292 v4l2_dbg(1, debug, sd, "illegal input: %d\n", input);
1da177e4
LT
293 return -EINVAL;
294 }
5325b427
HV
295 v4l2_dbg(1, debug, sd, "switched to %s\n", inputs[input]);
296 encoder->input = input;
1da177e4
LT
297 return 0;
298}
299
35631dcc
HV
300static int adv7175_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
301{
302 struct i2c_client *client = v4l2_get_subdevdata(sd);
303
304 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_ADV7175, 0);
305}
306
1da177e4
LT
307/* ----------------------------------------------------------------------- */
308
35631dcc
HV
309static const struct v4l2_subdev_core_ops adv7175_core_ops = {
310 .g_chip_ident = adv7175_g_chip_ident,
311 .init = adv7175_init,
1da177e4 312};
1da177e4 313
35631dcc
HV
314static const struct v4l2_subdev_video_ops adv7175_video_ops = {
315 .s_std_output = adv7175_s_std_output,
316 .s_routing = adv7175_s_routing,
317};
318
319static const struct v4l2_subdev_ops adv7175_ops = {
320 .core = &adv7175_core_ops,
321 .video = &adv7175_video_ops,
322};
323
324/* ----------------------------------------------------------------------- */
1da177e4 325
bba0d982
HV
326static int adv7175_probe(struct i2c_client *client,
327 const struct i2c_device_id *id)
1da177e4
LT
328{
329 int i;
1da177e4 330 struct adv7175 *encoder;
35631dcc 331 struct v4l2_subdev *sd;
1da177e4
LT
332
333 /* Check if the adapter supports the needed features */
bba0d982
HV
334 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
335 return -ENODEV;
1da177e4 336
bba0d982
HV
337 v4l_info(client, "chip found @ 0x%x (%s)\n",
338 client->addr << 1, client->adapter->name);
1da177e4 339
7408187d 340 encoder = kzalloc(sizeof(struct adv7175), GFP_KERNEL);
bba0d982 341 if (encoder == NULL)
1da177e4 342 return -ENOMEM;
35631dcc
HV
343 sd = &encoder->sd;
344 v4l2_i2c_subdev_init(sd, client, &adv7175_ops);
107063c6 345 encoder->norm = V4L2_STD_NTSC;
1da177e4 346 encoder->input = 0;
1da177e4 347
35631dcc 348 i = adv7175_write_block(sd, init_common, sizeof(init_common));
1da177e4 349 if (i >= 0) {
35631dcc
HV
350 i = adv7175_write(sd, 0x07, TR0MODE | TR0RST);
351 i = adv7175_write(sd, 0x07, TR0MODE);
352 i = adv7175_read(sd, 0x12);
353 v4l2_dbg(1, debug, sd, "revision %d\n", i & 1);
1da177e4 354 }
bba0d982 355 if (i < 0)
35631dcc 356 v4l2_dbg(1, debug, sd, "init error 0x%x\n", i);
1da177e4
LT
357 return 0;
358}
359
bba0d982 360static int adv7175_remove(struct i2c_client *client)
1da177e4 361{
35631dcc
HV
362 struct v4l2_subdev *sd = i2c_get_clientdata(client);
363
364 v4l2_device_unregister_subdev(sd);
365 kfree(to_adv7175(sd));
1da177e4
LT
366 return 0;
367}
368
369/* ----------------------------------------------------------------------- */
370
bba0d982
HV
371static const struct i2c_device_id adv7175_id[] = {
372 { "adv7175", 0 },
373 { "adv7176", 0 },
374 { }
375};
376MODULE_DEVICE_TABLE(i2c, adv7175_id);
1da177e4 377
bba0d982
HV
378static struct v4l2_i2c_driver_data v4l2_i2c_data = {
379 .name = "adv7175",
bba0d982
HV
380 .probe = adv7175_probe,
381 .remove = adv7175_remove,
382 .id_table = adv7175_id,
1da177e4 383};