]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/media/i2c/adv7175.c
Merge tag 'firewire-update' of git://git.kernel.org/pub/scm/linux/kernel/git/ieee1394...
[mirror_ubuntu-jammy-kernel.git] / drivers / media / i2c / adv7175.c
CommitLineData
c942fddf 1// SPDX-License-Identifier: GPL-2.0-or-later
d56410e0 2/*
1da177e4
LT
3 * adv7175 - adv7175a video encoder driver version 0.0.3
4 *
5 * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
6 * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
7 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
8 * - some corrections for Pinnacle Systems Inc. DC10plus card.
9 *
10 * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
11 * - moved over to linux>=2.4.x i2c protocol (9/9/2002)
1da177e4
LT
12 */
13
14#include <linux/module.h>
18f3fa1e 15#include <linux/types.h>
5a0e3ad6 16#include <linux/slab.h>
bba0d982 17#include <linux/ioctl.h>
7c0f6ba6 18#include <linux/uaccess.h>
bba0d982 19#include <linux/i2c.h>
35631dcc
HV
20#include <linux/videodev2.h>
21#include <media/v4l2-device.h>
1da177e4
LT
22
23MODULE_DESCRIPTION("Analog Devices ADV7175 video encoder driver");
24MODULE_AUTHOR("Dave Perks");
25MODULE_LICENSE("GPL");
26
35631dcc
HV
27#define I2C_ADV7175 0xd4
28#define I2C_ADV7176 0x54
29
35631dcc 30
ff699e6b 31static int debug;
1da177e4
LT
32module_param(debug, int, 0);
33MODULE_PARM_DESC(debug, "Debug level (0-1)");
34
1da177e4
LT
35/* ----------------------------------------------------------------------- */
36
37struct adv7175 {
35631dcc 38 struct v4l2_subdev sd;
107063c6 39 v4l2_std_id norm;
1da177e4 40 int input;
1da177e4
LT
41};
42
35631dcc
HV
43static inline struct adv7175 *to_adv7175(struct v4l2_subdev *sd)
44{
45 return container_of(sd, struct adv7175, sd);
46}
1da177e4 47
1da177e4 48static char *inputs[] = { "pass_through", "play_back", "color_bar" };
1da177e4 49
f5fe58fd
BB
50static u32 adv7175_codes[] = {
51 MEDIA_BUS_FMT_UYVY8_2X8,
52 MEDIA_BUS_FMT_UYVY8_1X16,
f1a84c9b
CG
53};
54
1da177e4
LT
55/* ----------------------------------------------------------------------- */
56
35631dcc 57static inline int adv7175_write(struct v4l2_subdev *sd, u8 reg, u8 value)
1da177e4 58{
35631dcc
HV
59 struct i2c_client *client = v4l2_get_subdevdata(sd);
60
1da177e4
LT
61 return i2c_smbus_write_byte_data(client, reg, value);
62}
63
35631dcc 64static inline int adv7175_read(struct v4l2_subdev *sd, u8 reg)
1da177e4 65{
35631dcc
HV
66 struct i2c_client *client = v4l2_get_subdevdata(sd);
67
1da177e4
LT
68 return i2c_smbus_read_byte_data(client, reg);
69}
70
35631dcc 71static int adv7175_write_block(struct v4l2_subdev *sd,
bba0d982 72 const u8 *data, unsigned int len)
1da177e4 73{
35631dcc 74 struct i2c_client *client = v4l2_get_subdevdata(sd);
1da177e4
LT
75 int ret = -1;
76 u8 reg;
77
78 /* the adv7175 has an autoincrement function, use it if
79 * the adapter understands raw I2C */
80 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
81 /* do raw I2C, not smbus compatible */
1da177e4 82 u8 block_data[32];
9aa45e34 83 int block_len;
1da177e4 84
1da177e4 85 while (len >= 2) {
9aa45e34
JD
86 block_len = 0;
87 block_data[block_len++] = reg = data[0];
1da177e4 88 do {
9aa45e34 89 block_data[block_len++] = data[1];
2467a670 90 reg++;
1da177e4
LT
91 len -= 2;
92 data += 2;
bba0d982
HV
93 } while (len >= 2 && data[0] == reg && block_len < 32);
94 ret = i2c_master_send(client, block_data, block_len);
95 if (ret < 0)
1da177e4
LT
96 break;
97 }
98 } else {
99 /* do some slow I2C emulation kind of thing */
100 while (len >= 2) {
101 reg = *data++;
35631dcc 102 ret = adv7175_write(sd, reg, *data++);
bba0d982 103 if (ret < 0)
1da177e4
LT
104 break;
105 len -= 2;
106 }
107 }
108
109 return ret;
110}
111
35631dcc 112static void set_subcarrier_freq(struct v4l2_subdev *sd, int pass_through)
1da177e4
LT
113{
114 /* for some reason pass_through NTSC needs
115 * a different sub-carrier freq to remain stable. */
bba0d982 116 if (pass_through)
35631dcc 117 adv7175_write(sd, 0x02, 0x00);
1da177e4 118 else
35631dcc 119 adv7175_write(sd, 0x02, 0x55);
1da177e4 120
35631dcc
HV
121 adv7175_write(sd, 0x03, 0x55);
122 adv7175_write(sd, 0x04, 0x55);
123 adv7175_write(sd, 0x05, 0x25);
1da177e4
LT
124}
125
1da177e4 126/* ----------------------------------------------------------------------- */
bba0d982 127/* Output filter: S-Video Composite */
1da177e4 128
bba0d982
HV
129#define MR050 0x11 /* 0x09 */
130#define MR060 0x14 /* 0x0c */
1da177e4 131
bba0d982 132/* ----------------------------------------------------------------------- */
1da177e4
LT
133
134#define TR0MODE 0x46
135#define TR0RST 0x80
136
137#define TR1CAPT 0x80
138#define TR1PLAY 0x00
139
140static const unsigned char init_common[] = {
141
142 0x00, MR050, /* MR0, PAL enabled */
143 0x01, 0x00, /* MR1 */
144 0x02, 0x0c, /* subc. freq. */
145 0x03, 0x8c, /* subc. freq. */
146 0x04, 0x79, /* subc. freq. */
147 0x05, 0x26, /* subc. freq. */
148 0x06, 0x40, /* subc. phase */
149
150 0x07, TR0MODE, /* TR0, 16bit */
151 0x08, 0x21, /* */
152 0x09, 0x00, /* */
153 0x0a, 0x00, /* */
154 0x0b, 0x00, /* */
155 0x0c, TR1CAPT, /* TR1 */
156 0x0d, 0x4f, /* MR2 */
157 0x0e, 0x00, /* */
158 0x0f, 0x00, /* */
159 0x10, 0x00, /* */
160 0x11, 0x00, /* */
161};
162
163static const unsigned char init_pal[] = {
164 0x00, MR050, /* MR0, PAL enabled */
165 0x01, 0x00, /* MR1 */
166 0x02, 0x0c, /* subc. freq. */
167 0x03, 0x8c, /* subc. freq. */
168 0x04, 0x79, /* subc. freq. */
169 0x05, 0x26, /* subc. freq. */
170 0x06, 0x40, /* subc. phase */
171};
172
173static const unsigned char init_ntsc[] = {
174 0x00, MR060, /* MR0, NTSC enabled */
175 0x01, 0x00, /* MR1 */
176 0x02, 0x55, /* subc. freq. */
177 0x03, 0x55, /* subc. freq. */
178 0x04, 0x55, /* subc. freq. */
179 0x05, 0x25, /* subc. freq. */
180 0x06, 0x1a, /* subc. phase */
181};
182
35631dcc
HV
183static int adv7175_init(struct v4l2_subdev *sd, u32 val)
184{
185 /* This is just for testing!!! */
186 adv7175_write_block(sd, init_common, sizeof(init_common));
187 adv7175_write(sd, 0x07, TR0MODE | TR0RST);
188 adv7175_write(sd, 0x07, TR0MODE);
189 return 0;
190}
191
192static int adv7175_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std)
1da177e4 193{
35631dcc
HV
194 struct adv7175 *encoder = to_adv7175(sd);
195
196 if (std & V4L2_STD_NTSC) {
197 adv7175_write_block(sd, init_ntsc, sizeof(init_ntsc));
198 if (encoder->input == 0)
199 adv7175_write(sd, 0x0d, 0x4f); /* Enable genlock */
200 adv7175_write(sd, 0x07, TR0MODE | TR0RST);
201 adv7175_write(sd, 0x07, TR0MODE);
202 } else if (std & V4L2_STD_PAL) {
203 adv7175_write_block(sd, init_pal, sizeof(init_pal));
204 if (encoder->input == 0)
205 adv7175_write(sd, 0x0d, 0x4f); /* Enable genlock */
206 adv7175_write(sd, 0x07, TR0MODE | TR0RST);
207 adv7175_write(sd, 0x07, TR0MODE);
208 } else if (std & V4L2_STD_SECAM) {
209 /* This is an attempt to convert
210 * SECAM->PAL (typically it does not work
211 * due to genlock: when decoder is in SECAM
212 * and encoder in in PAL the subcarrier can
f8a7647d 213 * not be synchronized with horizontal
35631dcc
HV
214 * quency) */
215 adv7175_write_block(sd, init_pal, sizeof(init_pal));
216 if (encoder->input == 0)
217 adv7175_write(sd, 0x0d, 0x49); /* Disable genlock */
218 adv7175_write(sd, 0x07, TR0MODE | TR0RST);
219 adv7175_write(sd, 0x07, TR0MODE);
220 } else {
cc5cef8e
HV
221 v4l2_dbg(1, debug, sd, "illegal norm: %llx\n",
222 (unsigned long long)std);
35631dcc
HV
223 return -EINVAL;
224 }
cc5cef8e 225 v4l2_dbg(1, debug, sd, "switched to %llx\n", (unsigned long long)std);
35631dcc
HV
226 encoder->norm = std;
227 return 0;
228}
229
5325b427
HV
230static int adv7175_s_routing(struct v4l2_subdev *sd,
231 u32 input, u32 output, u32 config)
35631dcc
HV
232{
233 struct adv7175 *encoder = to_adv7175(sd);
234
5325b427
HV
235 /* RJ: input = 0: input is from decoder
236 input = 1: input is from ZR36060
237 input = 2: color bar */
35631dcc 238
5325b427 239 switch (input) {
35631dcc
HV
240 case 0:
241 adv7175_write(sd, 0x01, 0x00);
242
243 if (encoder->norm & V4L2_STD_NTSC)
244 set_subcarrier_freq(sd, 1);
245
246 adv7175_write(sd, 0x0c, TR1CAPT); /* TR1 */
247 if (encoder->norm & V4L2_STD_SECAM)
248 adv7175_write(sd, 0x0d, 0x49); /* Disable genlock */
249 else
250 adv7175_write(sd, 0x0d, 0x4f); /* Enable genlock */
251 adv7175_write(sd, 0x07, TR0MODE | TR0RST);
252 adv7175_write(sd, 0x07, TR0MODE);
253 /*udelay(10);*/
d56410e0 254 break;
1da177e4 255
35631dcc
HV
256 case 1:
257 adv7175_write(sd, 0x01, 0x00);
258
259 if (encoder->norm & V4L2_STD_NTSC)
260 set_subcarrier_freq(sd, 0);
261
262 adv7175_write(sd, 0x0c, TR1PLAY); /* TR1 */
263 adv7175_write(sd, 0x0d, 0x49);
264 adv7175_write(sd, 0x07, TR0MODE | TR0RST);
265 adv7175_write(sd, 0x07, TR0MODE);
266 /* udelay(10); */
1da177e4
LT
267 break;
268
35631dcc
HV
269 case 2:
270 adv7175_write(sd, 0x01, 0x80);
271
272 if (encoder->norm & V4L2_STD_NTSC)
273 set_subcarrier_freq(sd, 0);
274
275 adv7175_write(sd, 0x0d, 0x49);
276 adv7175_write(sd, 0x07, TR0MODE | TR0RST);
277 adv7175_write(sd, 0x07, TR0MODE);
278 /* udelay(10); */
1da177e4
LT
279 break;
280
1da177e4 281 default:
5325b427 282 v4l2_dbg(1, debug, sd, "illegal input: %d\n", input);
1da177e4
LT
283 return -EINVAL;
284 }
5325b427
HV
285 v4l2_dbg(1, debug, sd, "switched to %s\n", inputs[input]);
286 encoder->input = input;
1da177e4
LT
287 return 0;
288}
289
ebcff5fc 290static int adv7175_enum_mbus_code(struct v4l2_subdev *sd,
0d346d2a 291 struct v4l2_subdev_state *sd_state,
ebcff5fc 292 struct v4l2_subdev_mbus_code_enum *code)
f1a84c9b 293{
ebcff5fc 294 if (code->pad || code->index >= ARRAY_SIZE(adv7175_codes))
f1a84c9b
CG
295 return -EINVAL;
296
ebcff5fc 297 code->code = adv7175_codes[code->index];
f1a84c9b
CG
298 return 0;
299}
300
da298c6d 301static int adv7175_get_fmt(struct v4l2_subdev *sd,
0d346d2a 302 struct v4l2_subdev_state *sd_state,
da298c6d 303 struct v4l2_subdev_format *format)
f1a84c9b 304{
da298c6d 305 struct v4l2_mbus_framefmt *mf = &format->format;
f1a84c9b
CG
306 u8 val = adv7175_read(sd, 0x7);
307
da298c6d
HV
308 if (format->pad)
309 return -EINVAL;
310
f1a84c9b 311 if ((val & 0x40) == (1 << 6))
f5fe58fd 312 mf->code = MEDIA_BUS_FMT_UYVY8_1X16;
f1a84c9b 313 else
f5fe58fd 314 mf->code = MEDIA_BUS_FMT_UYVY8_2X8;
f1a84c9b
CG
315
316 mf->colorspace = V4L2_COLORSPACE_SMPTE170M;
317 mf->width = 0;
318 mf->height = 0;
319 mf->field = V4L2_FIELD_ANY;
320
321 return 0;
322}
323
6e80c473 324static int adv7175_set_fmt(struct v4l2_subdev *sd,
0d346d2a 325 struct v4l2_subdev_state *sd_state,
6e80c473 326 struct v4l2_subdev_format *format)
f1a84c9b 327{
6e80c473 328 struct v4l2_mbus_framefmt *mf = &format->format;
f1a84c9b 329 u8 val = adv7175_read(sd, 0x7);
6e80c473
HV
330 int ret = 0;
331
332 if (format->pad)
333 return -EINVAL;
f1a84c9b
CG
334
335 switch (mf->code) {
f5fe58fd 336 case MEDIA_BUS_FMT_UYVY8_2X8:
f1a84c9b
CG
337 val &= ~0x40;
338 break;
339
f5fe58fd 340 case MEDIA_BUS_FMT_UYVY8_1X16:
f1a84c9b
CG
341 val |= 0x40;
342 break;
343
344 default:
345 v4l2_dbg(1, debug, sd,
346 "illegal v4l2_mbus_framefmt code: %d\n", mf->code);
347 return -EINVAL;
348 }
349
6e80c473
HV
350 if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE)
351 ret = adv7175_write(sd, 0x7, val);
f1a84c9b
CG
352
353 return ret;
354}
355
b7eccc46
CG
356static int adv7175_s_power(struct v4l2_subdev *sd, int on)
357{
358 if (on)
359 adv7175_write(sd, 0x01, 0x00);
360 else
361 adv7175_write(sd, 0x01, 0x78);
362
363 return 0;
364}
365
1da177e4
LT
366/* ----------------------------------------------------------------------- */
367
35631dcc 368static const struct v4l2_subdev_core_ops adv7175_core_ops = {
35631dcc 369 .init = adv7175_init,
b7eccc46 370 .s_power = adv7175_s_power,
1da177e4 371};
1da177e4 372
35631dcc
HV
373static const struct v4l2_subdev_video_ops adv7175_video_ops = {
374 .s_std_output = adv7175_s_std_output,
375 .s_routing = adv7175_s_routing,
ebcff5fc
HV
376};
377
378static const struct v4l2_subdev_pad_ops adv7175_pad_ops = {
379 .enum_mbus_code = adv7175_enum_mbus_code,
da298c6d 380 .get_fmt = adv7175_get_fmt,
6e80c473 381 .set_fmt = adv7175_set_fmt,
35631dcc
HV
382};
383
384static const struct v4l2_subdev_ops adv7175_ops = {
385 .core = &adv7175_core_ops,
386 .video = &adv7175_video_ops,
ebcff5fc 387 .pad = &adv7175_pad_ops,
35631dcc
HV
388};
389
390/* ----------------------------------------------------------------------- */
1da177e4 391
bba0d982
HV
392static int adv7175_probe(struct i2c_client *client,
393 const struct i2c_device_id *id)
1da177e4
LT
394{
395 int i;
1da177e4 396 struct adv7175 *encoder;
35631dcc 397 struct v4l2_subdev *sd;
1da177e4
LT
398
399 /* Check if the adapter supports the needed features */
bba0d982
HV
400 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
401 return -ENODEV;
1da177e4 402
bba0d982
HV
403 v4l_info(client, "chip found @ 0x%x (%s)\n",
404 client->addr << 1, client->adapter->name);
1da177e4 405
c02b211d 406 encoder = devm_kzalloc(&client->dev, sizeof(*encoder), GFP_KERNEL);
bba0d982 407 if (encoder == NULL)
1da177e4 408 return -ENOMEM;
35631dcc
HV
409 sd = &encoder->sd;
410 v4l2_i2c_subdev_init(sd, client, &adv7175_ops);
107063c6 411 encoder->norm = V4L2_STD_NTSC;
1da177e4 412 encoder->input = 0;
1da177e4 413
35631dcc 414 i = adv7175_write_block(sd, init_common, sizeof(init_common));
1da177e4 415 if (i >= 0) {
35631dcc
HV
416 i = adv7175_write(sd, 0x07, TR0MODE | TR0RST);
417 i = adv7175_write(sd, 0x07, TR0MODE);
418 i = adv7175_read(sd, 0x12);
419 v4l2_dbg(1, debug, sd, "revision %d\n", i & 1);
1da177e4 420 }
bba0d982 421 if (i < 0)
35631dcc 422 v4l2_dbg(1, debug, sd, "init error 0x%x\n", i);
1da177e4
LT
423 return 0;
424}
425
bba0d982 426static int adv7175_remove(struct i2c_client *client)
1da177e4 427{
35631dcc
HV
428 struct v4l2_subdev *sd = i2c_get_clientdata(client);
429
430 v4l2_device_unregister_subdev(sd);
1da177e4
LT
431 return 0;
432}
433
434/* ----------------------------------------------------------------------- */
435
bba0d982
HV
436static const struct i2c_device_id adv7175_id[] = {
437 { "adv7175", 0 },
438 { "adv7176", 0 },
439 { }
440};
441MODULE_DEVICE_TABLE(i2c, adv7175_id);
1da177e4 442
dc71443f
HV
443static struct i2c_driver adv7175_driver = {
444 .driver = {
dc71443f
HV
445 .name = "adv7175",
446 },
447 .probe = adv7175_probe,
448 .remove = adv7175_remove,
449 .id_table = adv7175_id,
1da177e4 450};
dc71443f 451
c6e8d86f 452module_i2c_driver(adv7175_driver);