]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - drivers/media/i2c/adv7170.c
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 157
[thirdparty/kernel/stable.git] / drivers / media / i2c / adv7170.c
CommitLineData
c942fddf 1// SPDX-License-Identifier: GPL-2.0-or-later
d56410e0 2/*
1da177e4
LT
3 * adv7170 - adv7170, adv7171 video encoder driver version 0.0.1
4 *
5 * Copyright (C) 2002 Maxim Yevtyushkin <max@linuxmedialabs.com>
6 *
d56410e0 7 * Based on adv7176 driver by:
1da177e4
LT
8 *
9 * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
10 * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
11 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
12 * - some corrections for Pinnacle Systems Inc. DC10plus card.
13 *
14 * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
15 * - moved over to linux>=2.4.x i2c protocol (1/1/2003)
1da177e4
LT
16 */
17
18#include <linux/module.h>
18f3fa1e 19#include <linux/types.h>
5a0e3ad6 20#include <linux/slab.h>
b9a21f84 21#include <linux/ioctl.h>
7c0f6ba6 22#include <linux/uaccess.h>
b9a21f84 23#include <linux/i2c.h>
7d9ef21c
HV
24#include <linux/videodev2.h>
25#include <media/v4l2-device.h>
1da177e4
LT
26
27MODULE_DESCRIPTION("Analog Devices ADV7170 video encoder driver");
28MODULE_AUTHOR("Maxim Yevtyushkin");
29MODULE_LICENSE("GPL");
30
7d9ef21c 31
ff699e6b 32static int debug;
1da177e4
LT
33module_param(debug, int, 0);
34MODULE_PARM_DESC(debug, "Debug level (0-1)");
35
1da177e4
LT
36/* ----------------------------------------------------------------------- */
37
38struct adv7170 {
7d9ef21c 39 struct v4l2_subdev sd;
1da177e4
LT
40 unsigned char reg[128];
41
107063c6 42 v4l2_std_id norm;
1da177e4 43 int input;
1da177e4
LT
44};
45
7d9ef21c
HV
46static inline struct adv7170 *to_adv7170(struct v4l2_subdev *sd)
47{
48 return container_of(sd, struct adv7170, sd);
49}
50
1da177e4 51static char *inputs[] = { "pass_through", "play_back" };
1da177e4 52
f5fe58fd
BB
53static u32 adv7170_codes[] = {
54 MEDIA_BUS_FMT_UYVY8_2X8,
55 MEDIA_BUS_FMT_UYVY8_1X16,
0d37d350
CG
56};
57
1da177e4
LT
58/* ----------------------------------------------------------------------- */
59
7d9ef21c 60static inline int adv7170_write(struct v4l2_subdev *sd, u8 reg, u8 value)
1da177e4 61{
7d9ef21c
HV
62 struct i2c_client *client = v4l2_get_subdevdata(sd);
63 struct adv7170 *encoder = to_adv7170(sd);
1da177e4
LT
64
65 encoder->reg[reg] = value;
66 return i2c_smbus_write_byte_data(client, reg, value);
67}
68
7d9ef21c 69static inline int adv7170_read(struct v4l2_subdev *sd, u8 reg)
1da177e4 70{
7d9ef21c
HV
71 struct i2c_client *client = v4l2_get_subdevdata(sd);
72
1da177e4
LT
73 return i2c_smbus_read_byte_data(client, reg);
74}
75
7d9ef21c 76static int adv7170_write_block(struct v4l2_subdev *sd,
b9a21f84 77 const u8 *data, unsigned int len)
1da177e4 78{
7d9ef21c
HV
79 struct i2c_client *client = v4l2_get_subdevdata(sd);
80 struct adv7170 *encoder = to_adv7170(sd);
1da177e4
LT
81 int ret = -1;
82 u8 reg;
83
84 /* the adv7170 has an autoincrement function, use it if
85 * the adapter understands raw I2C */
86 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
87 /* do raw I2C, not smbus compatible */
1da177e4 88 u8 block_data[32];
9aa45e34 89 int block_len;
1da177e4 90
1da177e4 91 while (len >= 2) {
9aa45e34
JD
92 block_len = 0;
93 block_data[block_len++] = reg = data[0];
1da177e4 94 do {
9aa45e34 95 block_data[block_len++] =
1da177e4
LT
96 encoder->reg[reg++] = data[1];
97 len -= 2;
98 data += 2;
b9a21f84
HV
99 } while (len >= 2 && data[0] == reg && block_len < 32);
100 ret = i2c_master_send(client, block_data, block_len);
101 if (ret < 0)
1da177e4
LT
102 break;
103 }
104 } else {
105 /* do some slow I2C emulation kind of thing */
106 while (len >= 2) {
107 reg = *data++;
7d9ef21c 108 ret = adv7170_write(sd, reg, *data++);
b9a21f84 109 if (ret < 0)
1da177e4
LT
110 break;
111 len -= 2;
112 }
113 }
1da177e4
LT
114 return ret;
115}
116
117/* ----------------------------------------------------------------------- */
1da177e4
LT
118
119#define TR0MODE 0x4c
120#define TR0RST 0x80
121
122#define TR1CAPT 0x00
123#define TR1PLAY 0x00
124
1da177e4 125static const unsigned char init_NTSC[] = {
7d9ef21c
HV
126 0x00, 0x10, /* MR0 */
127 0x01, 0x20, /* MR1 */
128 0x02, 0x0e, /* MR2 RTC control: bits 2 and 1 */
129 0x03, 0x80, /* MR3 */
130 0x04, 0x30, /* MR4 */
131 0x05, 0x00, /* Reserved */
132 0x06, 0x00, /* Reserved */
133 0x07, TR0MODE, /* TM0 */
134 0x08, TR1CAPT, /* TM1 */
135 0x09, 0x16, /* Fsc0 */
136 0x0a, 0x7c, /* Fsc1 */
137 0x0b, 0xf0, /* Fsc2 */
138 0x0c, 0x21, /* Fsc3 */
139 0x0d, 0x00, /* Subcarrier Phase */
140 0x0e, 0x00, /* Closed Capt. Ext 0 */
141 0x0f, 0x00, /* Closed Capt. Ext 1 */
142 0x10, 0x00, /* Closed Capt. 0 */
143 0x11, 0x00, /* Closed Capt. 1 */
144 0x12, 0x00, /* Pedestal Ctl 0 */
145 0x13, 0x00, /* Pedestal Ctl 1 */
146 0x14, 0x00, /* Pedestal Ctl 2 */
147 0x15, 0x00, /* Pedestal Ctl 3 */
148 0x16, 0x00, /* CGMS_WSS_0 */
149 0x17, 0x00, /* CGMS_WSS_1 */
150 0x18, 0x00, /* CGMS_WSS_2 */
151 0x19, 0x00, /* Teletext Ctl */
1da177e4
LT
152};
153
154static const unsigned char init_PAL[] = {
7d9ef21c
HV
155 0x00, 0x71, /* MR0 */
156 0x01, 0x20, /* MR1 */
157 0x02, 0x0e, /* MR2 RTC control: bits 2 and 1 */
158 0x03, 0x80, /* MR3 */
159 0x04, 0x30, /* MR4 */
160 0x05, 0x00, /* Reserved */
161 0x06, 0x00, /* Reserved */
162 0x07, TR0MODE, /* TM0 */
163 0x08, TR1CAPT, /* TM1 */
164 0x09, 0xcb, /* Fsc0 */
165 0x0a, 0x8a, /* Fsc1 */
166 0x0b, 0x09, /* Fsc2 */
167 0x0c, 0x2a, /* Fsc3 */
168 0x0d, 0x00, /* Subcarrier Phase */
169 0x0e, 0x00, /* Closed Capt. Ext 0 */
170 0x0f, 0x00, /* Closed Capt. Ext 1 */
171 0x10, 0x00, /* Closed Capt. 0 */
172 0x11, 0x00, /* Closed Capt. 1 */
173 0x12, 0x00, /* Pedestal Ctl 0 */
174 0x13, 0x00, /* Pedestal Ctl 1 */
175 0x14, 0x00, /* Pedestal Ctl 2 */
176 0x15, 0x00, /* Pedestal Ctl 3 */
177 0x16, 0x00, /* CGMS_WSS_0 */
178 0x17, 0x00, /* CGMS_WSS_1 */
179 0x18, 0x00, /* CGMS_WSS_2 */
180 0x19, 0x00, /* Teletext Ctl */
1da177e4
LT
181};
182
183
7d9ef21c 184static int adv7170_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std)
1da177e4 185{
7d9ef21c
HV
186 struct adv7170 *encoder = to_adv7170(sd);
187
cc5cef8e 188 v4l2_dbg(1, debug, sd, "set norm %llx\n", (unsigned long long)std);
7d9ef21c
HV
189
190 if (std & V4L2_STD_NTSC) {
191 adv7170_write_block(sd, init_NTSC, sizeof(init_NTSC));
192 if (encoder->input == 0)
193 adv7170_write(sd, 0x02, 0x0e); /* Enable genlock */
194 adv7170_write(sd, 0x07, TR0MODE | TR0RST);
195 adv7170_write(sd, 0x07, TR0MODE);
196 } else if (std & V4L2_STD_PAL) {
197 adv7170_write_block(sd, init_PAL, sizeof(init_PAL));
198 if (encoder->input == 0)
199 adv7170_write(sd, 0x02, 0x0e); /* Enable genlock */
200 adv7170_write(sd, 0x07, TR0MODE | TR0RST);
201 adv7170_write(sd, 0x07, TR0MODE);
202 } else {
cc5cef8e
HV
203 v4l2_dbg(1, debug, sd, "illegal norm: %llx\n",
204 (unsigned long long)std);
7d9ef21c 205 return -EINVAL;
b9a21f84 206 }
cc5cef8e 207 v4l2_dbg(1, debug, sd, "switched to %llx\n", (unsigned long long)std);
7d9ef21c
HV
208 encoder->norm = std;
209 return 0;
210}
1da177e4 211
5325b427
HV
212static int adv7170_s_routing(struct v4l2_subdev *sd,
213 u32 input, u32 output, u32 config)
7d9ef21c
HV
214{
215 struct adv7170 *encoder = to_adv7170(sd);
1da177e4 216
5325b427
HV
217 /* RJ: input = 0: input is from decoder
218 input = 1: input is from ZR36060
219 input = 2: color bar */
1da177e4 220
7d9ef21c 221 v4l2_dbg(1, debug, sd, "set input from %s\n",
5325b427 222 input == 0 ? "decoder" : "ZR36060");
1da177e4 223
5325b427 224 switch (input) {
7d9ef21c
HV
225 case 0:
226 adv7170_write(sd, 0x01, 0x20);
227 adv7170_write(sd, 0x08, TR1CAPT); /* TR1 */
228 adv7170_write(sd, 0x02, 0x0e); /* Enable genlock */
229 adv7170_write(sd, 0x07, TR0MODE | TR0RST);
230 adv7170_write(sd, 0x07, TR0MODE);
231 /* udelay(10); */
232 break;
233
234 case 1:
235 adv7170_write(sd, 0x01, 0x00);
236 adv7170_write(sd, 0x08, TR1PLAY); /* TR1 */
237 adv7170_write(sd, 0x02, 0x08);
238 adv7170_write(sd, 0x07, TR0MODE | TR0RST);
239 adv7170_write(sd, 0x07, TR0MODE);
240 /* udelay(10); */
1da177e4
LT
241 break;
242
243 default:
5325b427 244 v4l2_dbg(1, debug, sd, "illegal input: %d\n", input);
1da177e4
LT
245 return -EINVAL;
246 }
5325b427
HV
247 v4l2_dbg(1, debug, sd, "switched to %s\n", inputs[input]);
248 encoder->input = input;
1da177e4
LT
249 return 0;
250}
251
ebcff5fc
HV
252static int adv7170_enum_mbus_code(struct v4l2_subdev *sd,
253 struct v4l2_subdev_pad_config *cfg,
254 struct v4l2_subdev_mbus_code_enum *code)
0d37d350 255{
ebcff5fc 256 if (code->pad || code->index >= ARRAY_SIZE(adv7170_codes))
0d37d350
CG
257 return -EINVAL;
258
ebcff5fc 259 code->code = adv7170_codes[code->index];
0d37d350
CG
260 return 0;
261}
262
da298c6d
HV
263static int adv7170_get_fmt(struct v4l2_subdev *sd,
264 struct v4l2_subdev_pad_config *cfg,
265 struct v4l2_subdev_format *format)
0d37d350 266{
da298c6d 267 struct v4l2_mbus_framefmt *mf = &format->format;
0d37d350
CG
268 u8 val = adv7170_read(sd, 0x7);
269
da298c6d
HV
270 if (format->pad)
271 return -EINVAL;
272
0d37d350 273 if ((val & 0x40) == (1 << 6))
f5fe58fd 274 mf->code = MEDIA_BUS_FMT_UYVY8_1X16;
0d37d350 275 else
f5fe58fd 276 mf->code = MEDIA_BUS_FMT_UYVY8_2X8;
0d37d350
CG
277
278 mf->colorspace = V4L2_COLORSPACE_SMPTE170M;
279 mf->width = 0;
280 mf->height = 0;
281 mf->field = V4L2_FIELD_ANY;
282
283 return 0;
284}
285
6e80c473
HV
286static int adv7170_set_fmt(struct v4l2_subdev *sd,
287 struct v4l2_subdev_pad_config *cfg,
288 struct v4l2_subdev_format *format)
0d37d350 289{
6e80c473 290 struct v4l2_mbus_framefmt *mf = &format->format;
0d37d350 291 u8 val = adv7170_read(sd, 0x7);
6e80c473
HV
292
293 if (format->pad)
294 return -EINVAL;
0d37d350
CG
295
296 switch (mf->code) {
f5fe58fd 297 case MEDIA_BUS_FMT_UYVY8_2X8:
0d37d350
CG
298 val &= ~0x40;
299 break;
300
f5fe58fd 301 case MEDIA_BUS_FMT_UYVY8_1X16:
0d37d350
CG
302 val |= 0x40;
303 break;
304
305 default:
306 v4l2_dbg(1, debug, sd,
307 "illegal v4l2_mbus_framefmt code: %d\n", mf->code);
308 return -EINVAL;
309 }
310
6e80c473 311 if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE)
5f98e5f5 312 return adv7170_write(sd, 0x7, val);
0d37d350 313
5f98e5f5 314 return 0;
0d37d350
CG
315}
316
1da177e4
LT
317/* ----------------------------------------------------------------------- */
318
7d9ef21c
HV
319static const struct v4l2_subdev_video_ops adv7170_video_ops = {
320 .s_std_output = adv7170_s_std_output,
321 .s_routing = adv7170_s_routing,
ebcff5fc
HV
322};
323
324static const struct v4l2_subdev_pad_ops adv7170_pad_ops = {
325 .enum_mbus_code = adv7170_enum_mbus_code,
da298c6d 326 .get_fmt = adv7170_get_fmt,
6e80c473 327 .set_fmt = adv7170_set_fmt,
7d9ef21c
HV
328};
329
330static const struct v4l2_subdev_ops adv7170_ops = {
7d9ef21c 331 .video = &adv7170_video_ops,
ebcff5fc 332 .pad = &adv7170_pad_ops,
7d9ef21c
HV
333};
334
335/* ----------------------------------------------------------------------- */
d56410e0 336
b9a21f84
HV
337static int adv7170_probe(struct i2c_client *client,
338 const struct i2c_device_id *id)
1da177e4 339{
1da177e4 340 struct adv7170 *encoder;
7d9ef21c 341 struct v4l2_subdev *sd;
b9a21f84 342 int i;
1da177e4
LT
343
344 /* Check if the adapter supports the needed features */
b9a21f84
HV
345 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
346 return -ENODEV;
1da177e4 347
b9a21f84
HV
348 v4l_info(client, "chip found @ 0x%x (%s)\n",
349 client->addr << 1, client->adapter->name);
1da177e4 350
c02b211d 351 encoder = devm_kzalloc(&client->dev, sizeof(*encoder), GFP_KERNEL);
b9a21f84 352 if (encoder == NULL)
1da177e4 353 return -ENOMEM;
7d9ef21c
HV
354 sd = &encoder->sd;
355 v4l2_i2c_subdev_init(sd, client, &adv7170_ops);
107063c6 356 encoder->norm = V4L2_STD_NTSC;
1da177e4 357 encoder->input = 0;
1da177e4 358
7d9ef21c 359 i = adv7170_write_block(sd, init_NTSC, sizeof(init_NTSC));
1da177e4 360 if (i >= 0) {
7d9ef21c
HV
361 i = adv7170_write(sd, 0x07, TR0MODE | TR0RST);
362 i = adv7170_write(sd, 0x07, TR0MODE);
363 i = adv7170_read(sd, 0x12);
364 v4l2_dbg(1, debug, sd, "revision %d\n", i & 1);
1da177e4 365 }
b9a21f84 366 if (i < 0)
7d9ef21c 367 v4l2_dbg(1, debug, sd, "init error 0x%x\n", i);
1da177e4
LT
368 return 0;
369}
370
b9a21f84 371static int adv7170_remove(struct i2c_client *client)
1da177e4 372{
7d9ef21c
HV
373 struct v4l2_subdev *sd = i2c_get_clientdata(client);
374
375 v4l2_device_unregister_subdev(sd);
1da177e4
LT
376 return 0;
377}
378
379/* ----------------------------------------------------------------------- */
380
b9a21f84
HV
381static const struct i2c_device_id adv7170_id[] = {
382 { "adv7170", 0 },
383 { "adv7171", 0 },
384 { }
385};
386MODULE_DEVICE_TABLE(i2c, adv7170_id);
1da177e4 387
c9611802
HV
388static struct i2c_driver adv7170_driver = {
389 .driver = {
c9611802
HV
390 .name = "adv7170",
391 },
392 .probe = adv7170_probe,
393 .remove = adv7170_remove,
394 .id_table = adv7170_id,
1da177e4 395};
c9611802 396
c6e8d86f 397module_i2c_driver(adv7170_driver);