Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / media / i2c / adv7170.c
blobe4e8fda51ad8a015a0e7ff2922404eb1071aee03
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * adv7170 - adv7170, adv7171 video encoder driver version 0.0.1
5 * Copyright (C) 2002 Maxim Yevtyushkin <max@linuxmedialabs.com>
7 * Based on adv7176 driver by:
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.
14 * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
15 * - moved over to linux>=2.4.x i2c protocol (1/1/2003)
18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/slab.h>
21 #include <linux/ioctl.h>
22 #include <linux/uaccess.h>
23 #include <linux/i2c.h>
24 #include <linux/videodev2.h>
25 #include <media/v4l2-device.h>
27 MODULE_DESCRIPTION("Analog Devices ADV7170 video encoder driver");
28 MODULE_AUTHOR("Maxim Yevtyushkin");
29 MODULE_LICENSE("GPL");
32 static int debug;
33 module_param(debug, int, 0);
34 MODULE_PARM_DESC(debug, "Debug level (0-1)");
36 /* ----------------------------------------------------------------------- */
38 struct adv7170 {
39 struct v4l2_subdev sd;
40 unsigned char reg[128];
42 v4l2_std_id norm;
43 int input;
46 static inline struct adv7170 *to_adv7170(struct v4l2_subdev *sd)
48 return container_of(sd, struct adv7170, sd);
51 static char *inputs[] = { "pass_through", "play_back" };
53 static u32 adv7170_codes[] = {
54 MEDIA_BUS_FMT_UYVY8_2X8,
55 MEDIA_BUS_FMT_UYVY8_1X16,
58 /* ----------------------------------------------------------------------- */
60 static inline int adv7170_write(struct v4l2_subdev *sd, u8 reg, u8 value)
62 struct i2c_client *client = v4l2_get_subdevdata(sd);
63 struct adv7170 *encoder = to_adv7170(sd);
65 encoder->reg[reg] = value;
66 return i2c_smbus_write_byte_data(client, reg, value);
69 static inline int adv7170_read(struct v4l2_subdev *sd, u8 reg)
71 struct i2c_client *client = v4l2_get_subdevdata(sd);
73 return i2c_smbus_read_byte_data(client, reg);
76 static int adv7170_write_block(struct v4l2_subdev *sd,
77 const u8 *data, unsigned int len)
79 struct i2c_client *client = v4l2_get_subdevdata(sd);
80 struct adv7170 *encoder = to_adv7170(sd);
81 int ret = -1;
82 u8 reg;
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 */
88 u8 block_data[32];
89 int block_len;
91 while (len >= 2) {
92 block_len = 0;
93 block_data[block_len++] = reg = data[0];
94 do {
95 block_data[block_len++] =
96 encoder->reg[reg++] = data[1];
97 len -= 2;
98 data += 2;
99 } while (len >= 2 && data[0] == reg && block_len < 32);
100 ret = i2c_master_send(client, block_data, block_len);
101 if (ret < 0)
102 break;
104 } else {
105 /* do some slow I2C emulation kind of thing */
106 while (len >= 2) {
107 reg = *data++;
108 ret = adv7170_write(sd, reg, *data++);
109 if (ret < 0)
110 break;
111 len -= 2;
114 return ret;
117 /* ----------------------------------------------------------------------- */
119 #define TR0MODE 0x4c
120 #define TR0RST 0x80
122 #define TR1CAPT 0x00
123 #define TR1PLAY 0x00
125 static const unsigned char init_NTSC[] = {
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 */
154 static const unsigned char init_PAL[] = {
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 */
184 static int adv7170_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std)
186 struct adv7170 *encoder = to_adv7170(sd);
188 v4l2_dbg(1, debug, sd, "set norm %llx\n", (unsigned long long)std);
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 {
203 v4l2_dbg(1, debug, sd, "illegal norm: %llx\n",
204 (unsigned long long)std);
205 return -EINVAL;
207 v4l2_dbg(1, debug, sd, "switched to %llx\n", (unsigned long long)std);
208 encoder->norm = std;
209 return 0;
212 static int adv7170_s_routing(struct v4l2_subdev *sd,
213 u32 input, u32 output, u32 config)
215 struct adv7170 *encoder = to_adv7170(sd);
217 /* RJ: input = 0: input is from decoder
218 input = 1: input is from ZR36060
219 input = 2: color bar */
221 v4l2_dbg(1, debug, sd, "set input from %s\n",
222 input == 0 ? "decoder" : "ZR36060");
224 switch (input) {
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;
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); */
241 break;
243 default:
244 v4l2_dbg(1, debug, sd, "illegal input: %d\n", input);
245 return -EINVAL;
247 v4l2_dbg(1, debug, sd, "switched to %s\n", inputs[input]);
248 encoder->input = input;
249 return 0;
252 static int adv7170_enum_mbus_code(struct v4l2_subdev *sd,
253 struct v4l2_subdev_pad_config *cfg,
254 struct v4l2_subdev_mbus_code_enum *code)
256 if (code->pad || code->index >= ARRAY_SIZE(adv7170_codes))
257 return -EINVAL;
259 code->code = adv7170_codes[code->index];
260 return 0;
263 static int adv7170_get_fmt(struct v4l2_subdev *sd,
264 struct v4l2_subdev_pad_config *cfg,
265 struct v4l2_subdev_format *format)
267 struct v4l2_mbus_framefmt *mf = &format->format;
268 u8 val = adv7170_read(sd, 0x7);
270 if (format->pad)
271 return -EINVAL;
273 if ((val & 0x40) == (1 << 6))
274 mf->code = MEDIA_BUS_FMT_UYVY8_1X16;
275 else
276 mf->code = MEDIA_BUS_FMT_UYVY8_2X8;
278 mf->colorspace = V4L2_COLORSPACE_SMPTE170M;
279 mf->width = 0;
280 mf->height = 0;
281 mf->field = V4L2_FIELD_ANY;
283 return 0;
286 static int adv7170_set_fmt(struct v4l2_subdev *sd,
287 struct v4l2_subdev_pad_config *cfg,
288 struct v4l2_subdev_format *format)
290 struct v4l2_mbus_framefmt *mf = &format->format;
291 u8 val = adv7170_read(sd, 0x7);
293 if (format->pad)
294 return -EINVAL;
296 switch (mf->code) {
297 case MEDIA_BUS_FMT_UYVY8_2X8:
298 val &= ~0x40;
299 break;
301 case MEDIA_BUS_FMT_UYVY8_1X16:
302 val |= 0x40;
303 break;
305 default:
306 v4l2_dbg(1, debug, sd,
307 "illegal v4l2_mbus_framefmt code: %d\n", mf->code);
308 return -EINVAL;
311 if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE)
312 return adv7170_write(sd, 0x7, val);
314 return 0;
317 /* ----------------------------------------------------------------------- */
319 static const struct v4l2_subdev_video_ops adv7170_video_ops = {
320 .s_std_output = adv7170_s_std_output,
321 .s_routing = adv7170_s_routing,
324 static const struct v4l2_subdev_pad_ops adv7170_pad_ops = {
325 .enum_mbus_code = adv7170_enum_mbus_code,
326 .get_fmt = adv7170_get_fmt,
327 .set_fmt = adv7170_set_fmt,
330 static const struct v4l2_subdev_ops adv7170_ops = {
331 .video = &adv7170_video_ops,
332 .pad = &adv7170_pad_ops,
335 /* ----------------------------------------------------------------------- */
337 static int adv7170_probe(struct i2c_client *client,
338 const struct i2c_device_id *id)
340 struct adv7170 *encoder;
341 struct v4l2_subdev *sd;
342 int i;
344 /* Check if the adapter supports the needed features */
345 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
346 return -ENODEV;
348 v4l_info(client, "chip found @ 0x%x (%s)\n",
349 client->addr << 1, client->adapter->name);
351 encoder = devm_kzalloc(&client->dev, sizeof(*encoder), GFP_KERNEL);
352 if (encoder == NULL)
353 return -ENOMEM;
354 sd = &encoder->sd;
355 v4l2_i2c_subdev_init(sd, client, &adv7170_ops);
356 encoder->norm = V4L2_STD_NTSC;
357 encoder->input = 0;
359 i = adv7170_write_block(sd, init_NTSC, sizeof(init_NTSC));
360 if (i >= 0) {
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);
366 if (i < 0)
367 v4l2_dbg(1, debug, sd, "init error 0x%x\n", i);
368 return 0;
371 static int adv7170_remove(struct i2c_client *client)
373 struct v4l2_subdev *sd = i2c_get_clientdata(client);
375 v4l2_device_unregister_subdev(sd);
376 return 0;
379 /* ----------------------------------------------------------------------- */
381 static const struct i2c_device_id adv7170_id[] = {
382 { "adv7170", 0 },
383 { "adv7171", 0 },
386 MODULE_DEVICE_TABLE(i2c, adv7170_id);
388 static struct i2c_driver adv7170_driver = {
389 .driver = {
390 .name = "adv7170",
392 .probe = adv7170_probe,
393 .remove = adv7170_remove,
394 .id_table = adv7170_id,
397 module_i2c_driver(adv7170_driver);