2 * bt819 - BT819A VideoStream Decoder (Rockwell Part)
4 * Copyright (C) 1999 Mike Bernson <mike@mlb.org>
5 * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
7 * Modifications for LML33/DC10plus unified driver
8 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
10 * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
11 * - moved over to linux>=2.4.x i2c protocol (9/9/2002)
13 * This code was modify/ported from the saa7111 driver written
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.
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.
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.
31 #include <linux/module.h>
32 #include <linux/types.h>
33 #include <linux/ioctl.h>
34 #include <linux/delay.h>
35 #include <linux/i2c.h>
36 #include <linux/i2c-id.h>
37 #include <linux/videodev2.h>
38 #include <media/v4l2-device.h>
39 #include <media/v4l2-chip-ident.h>
40 #include <media/v4l2-i2c-drv.h>
41 #include <media/bt819.h>
43 MODULE_DESCRIPTION("Brooktree-819 video decoder driver");
44 MODULE_AUTHOR("Mike Bernson & Dave Perks");
45 MODULE_LICENSE("GPL");
48 module_param(debug, int, 0);
49 MODULE_PARM_DESC(debug, "Debug level (0-1)");
52 /* ----------------------------------------------------------------------- */
55 struct v4l2_subdev sd;
56 unsigned char reg[32];
68 static inline struct bt819 *to_bt819(struct v4l2_subdev *sd)
70 return container_of(sd, struct bt819, sd);
82 /* for values, see the bt819 datasheet */
83 static struct timing timing_data[] = {
84 {864 - 24, 20, 625 - 2, 1, 0x0504, 0x0000},
85 {858 - 24, 20, 525 - 2, 1, 0x00f8, 0x0000},
88 /* ----------------------------------------------------------------------- */
90 static inline int bt819_write(struct bt819 *decoder, u8 reg, u8 value)
92 struct i2c_client *client = v4l2_get_subdevdata(&decoder->sd);
94 decoder->reg[reg] = value;
95 return i2c_smbus_write_byte_data(client, reg, value);
98 static inline int bt819_setbit(struct bt819 *decoder, u8 reg, u8 bit, u8 value)
100 return bt819_write(decoder, reg,
101 (decoder->reg[reg] & ~(1 << bit)) | (value ? (1 << bit) : 0));
104 static int bt819_write_block(struct bt819 *decoder, const u8 *data, unsigned int len)
106 struct i2c_client *client = v4l2_get_subdevdata(&decoder->sd);
110 /* the bt819 has an autoincrement function, use it if
111 * the adapter understands raw I2C */
112 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
113 /* do raw I2C, not smbus compatible */
119 block_data[block_len++] = reg = data[0];
121 block_data[block_len++] =
122 decoder->reg[reg++] = data[1];
125 } while (len >= 2 && data[0] == reg && block_len < 32);
126 ret = i2c_master_send(client, block_data, block_len);
131 /* do some slow I2C emulation kind of thing */
134 ret = bt819_write(decoder, reg, *data++);
144 static inline int bt819_read(struct bt819 *decoder, u8 reg)
146 struct i2c_client *client = v4l2_get_subdevdata(&decoder->sd);
148 return i2c_smbus_read_byte_data(client, reg);
151 static int bt819_init(struct v4l2_subdev *sd)
153 static unsigned char init[] = {
154 /*0x1f, 0x00,*/ /* Reset */
155 0x01, 0x59, /* 0x01 input format */
156 0x02, 0x00, /* 0x02 temporal decimation */
157 0x03, 0x12, /* 0x03 Cropping msb */
158 0x04, 0x16, /* 0x04 Vertical Delay, lsb */
159 0x05, 0xe0, /* 0x05 Vertical Active lsb */
160 0x06, 0x80, /* 0x06 Horizontal Delay lsb */
161 0x07, 0xd0, /* 0x07 Horizontal Active lsb */
162 0x08, 0x00, /* 0x08 Horizontal Scaling msb */
163 0x09, 0xf8, /* 0x09 Horizontal Scaling lsb */
164 0x0a, 0x00, /* 0x0a Brightness control */
165 0x0b, 0x30, /* 0x0b Miscellaneous control */
166 0x0c, 0xd8, /* 0x0c Luma Gain lsb */
167 0x0d, 0xfe, /* 0x0d Chroma Gain (U) lsb */
168 0x0e, 0xb4, /* 0x0e Chroma Gain (V) msb */
169 0x0f, 0x00, /* 0x0f Hue control */
170 0x12, 0x04, /* 0x12 Output Format */
171 0x13, 0x20, /* 0x13 Vertial Scaling msb 0x00
172 chroma comb OFF, line drop scaling, interlace scaling
173 BUG? Why does turning the chroma comb on fuck up color?
174 Bug in the bt819 stepping on my board?
176 0x14, 0x00, /* 0x14 Vertial Scaling lsb */
177 0x16, 0x07, /* 0x16 Video Timing Polarity
181 hreset=active high */
182 0x18, 0x68, /* 0x18 AGC Delay */
183 0x19, 0x5d, /* 0x19 Burst Gate Delay */
184 0x1a, 0x80, /* 0x1a ADC Interface */
187 struct bt819 *decoder = to_bt819(sd);
188 struct timing *timing = &timing_data[(decoder->norm & V4L2_STD_525_60) ? 1 : 0];
191 (((timing->vdelay >> 8) & 0x03) << 6) |
192 (((timing->vactive >> 8) & 0x03) << 4) |
193 (((timing->hdelay >> 8) & 0x03) << 2) |
194 ((timing->hactive >> 8) & 0x03);
195 init[0x04 * 2 - 1] = timing->vdelay & 0xff;
196 init[0x05 * 2 - 1] = timing->vactive & 0xff;
197 init[0x06 * 2 - 1] = timing->hdelay & 0xff;
198 init[0x07 * 2 - 1] = timing->hactive & 0xff;
199 init[0x08 * 2 - 1] = timing->hscale >> 8;
200 init[0x09 * 2 - 1] = timing->hscale & 0xff;
201 /* 0x15 in array is address 0x19 */
202 init[0x15 * 2 - 1] = (decoder->norm & V4L2_STD_625_50) ? 115 : 93; /* Chroma burst delay */
204 bt819_write(decoder, 0x1f, 0x00);
208 return bt819_write_block(decoder, init, sizeof(init));
211 /* ----------------------------------------------------------------------- */
213 static int bt819_status(struct v4l2_subdev *sd, u32 *pstatus, v4l2_std_id *pstd)
215 struct bt819 *decoder = to_bt819(sd);
216 int status = bt819_read(decoder, 0x00);
217 int res = V4L2_IN_ST_NO_SIGNAL;
232 v4l2_dbg(1, debug, sd, "get status %x\n", status);
236 static int bt819_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
238 return bt819_status(sd, NULL, std);
241 static int bt819_g_input_status(struct v4l2_subdev *sd, u32 *status)
243 return bt819_status(sd, status, NULL);
246 static int bt819_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
248 struct bt819 *decoder = to_bt819(sd);
249 struct timing *timing = NULL;
251 v4l2_dbg(1, debug, sd, "set norm %llx\n", (unsigned long long)std);
253 if (sd->v4l2_dev == NULL || sd->v4l2_dev->notify == NULL)
254 v4l2_err(sd, "no notify found!\n");
256 if (std & V4L2_STD_NTSC) {
257 v4l2_subdev_notify(sd, BT819_FIFO_RESET_LOW, 0);
258 bt819_setbit(decoder, 0x01, 0, 1);
259 bt819_setbit(decoder, 0x01, 1, 0);
260 bt819_setbit(decoder, 0x01, 5, 0);
261 bt819_write(decoder, 0x18, 0x68);
262 bt819_write(decoder, 0x19, 0x5d);
263 /* bt819_setbit(decoder, 0x1a, 5, 1); */
264 timing = &timing_data[1];
265 } else if (std & V4L2_STD_PAL) {
266 v4l2_subdev_notify(sd, BT819_FIFO_RESET_LOW, 0);
267 bt819_setbit(decoder, 0x01, 0, 1);
268 bt819_setbit(decoder, 0x01, 1, 1);
269 bt819_setbit(decoder, 0x01, 5, 1);
270 bt819_write(decoder, 0x18, 0x7f);
271 bt819_write(decoder, 0x19, 0x72);
272 /* bt819_setbit(decoder, 0x1a, 5, 0); */
273 timing = &timing_data[0];
275 v4l2_dbg(1, debug, sd, "unsupported norm %llx\n",
276 (unsigned long long)std);
279 bt819_write(decoder, 0x03,
280 (((timing->vdelay >> 8) & 0x03) << 6) |
281 (((timing->vactive >> 8) & 0x03) << 4) |
282 (((timing->hdelay >> 8) & 0x03) << 2) |
283 ((timing->hactive >> 8) & 0x03));
284 bt819_write(decoder, 0x04, timing->vdelay & 0xff);
285 bt819_write(decoder, 0x05, timing->vactive & 0xff);
286 bt819_write(decoder, 0x06, timing->hdelay & 0xff);
287 bt819_write(decoder, 0x07, timing->hactive & 0xff);
288 bt819_write(decoder, 0x08, (timing->hscale >> 8) & 0xff);
289 bt819_write(decoder, 0x09, timing->hscale & 0xff);
291 v4l2_subdev_notify(sd, BT819_FIFO_RESET_HIGH, 0);
295 static int bt819_s_routing(struct v4l2_subdev *sd, const struct v4l2_routing *route)
297 struct bt819 *decoder = to_bt819(sd);
299 v4l2_dbg(1, debug, sd, "set input %x\n", route->input);
301 if (route->input < 0 || route->input > 7)
304 if (sd->v4l2_dev == NULL || sd->v4l2_dev->notify == NULL)
305 v4l2_err(sd, "no notify found!\n");
307 if (decoder->input != route->input) {
308 v4l2_subdev_notify(sd, BT819_FIFO_RESET_LOW, 0);
309 decoder->input = route->input;
311 if (decoder->input == 0) {
312 bt819_setbit(decoder, 0x0b, 6, 0);
313 bt819_setbit(decoder, 0x1a, 1, 1);
315 bt819_setbit(decoder, 0x0b, 6, 1);
316 bt819_setbit(decoder, 0x1a, 1, 0);
318 v4l2_subdev_notify(sd, BT819_FIFO_RESET_HIGH, 0);
323 static int bt819_s_stream(struct v4l2_subdev *sd, int enable)
325 struct bt819 *decoder = to_bt819(sd);
327 v4l2_dbg(1, debug, sd, "enable output %x\n", enable);
329 if (decoder->enable != enable) {
330 decoder->enable = enable;
331 bt819_setbit(decoder, 0x16, 7, !enable);
336 static int bt819_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
339 case V4L2_CID_BRIGHTNESS:
340 v4l2_ctrl_query_fill(qc, -128, 127, 1, 0);
343 case V4L2_CID_CONTRAST:
344 v4l2_ctrl_query_fill(qc, 0, 511, 1, 256);
347 case V4L2_CID_SATURATION:
348 v4l2_ctrl_query_fill(qc, 0, 511, 1, 256);
352 v4l2_ctrl_query_fill(qc, -128, 127, 1, 0);
361 static int bt819_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
363 struct bt819 *decoder = to_bt819(sd);
367 case V4L2_CID_BRIGHTNESS:
368 if (decoder->bright == ctrl->value)
370 decoder->bright = ctrl->value;
371 bt819_write(decoder, 0x0a, decoder->bright);
374 case V4L2_CID_CONTRAST:
375 if (decoder->contrast == ctrl->value)
377 decoder->contrast = ctrl->value;
378 bt819_write(decoder, 0x0c, decoder->contrast & 0xff);
379 bt819_setbit(decoder, 0x0b, 2, ((decoder->contrast >> 8) & 0x01));
382 case V4L2_CID_SATURATION:
383 if (decoder->sat == ctrl->value)
385 decoder->sat = ctrl->value;
386 bt819_write(decoder, 0x0d, (decoder->sat >> 7) & 0xff);
387 bt819_setbit(decoder, 0x0b, 1, ((decoder->sat >> 15) & 0x01));
389 /* Ratio between U gain and V gain must stay the same as
390 the ratio between the default U and V gain values. */
391 temp = (decoder->sat * 180) / 254;
392 bt819_write(decoder, 0x0e, (temp >> 7) & 0xff);
393 bt819_setbit(decoder, 0x0b, 0, (temp >> 15) & 0x01);
397 if (decoder->hue == ctrl->value)
399 decoder->hue = ctrl->value;
400 bt819_write(decoder, 0x0f, decoder->hue);
409 static int bt819_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
411 struct bt819 *decoder = to_bt819(sd);
414 case V4L2_CID_BRIGHTNESS:
415 ctrl->value = decoder->bright;
417 case V4L2_CID_CONTRAST:
418 ctrl->value = decoder->contrast;
420 case V4L2_CID_SATURATION:
421 ctrl->value = decoder->sat;
424 ctrl->value = decoder->hue;
432 static int bt819_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
434 struct bt819 *decoder = to_bt819(sd);
435 struct i2c_client *client = v4l2_get_subdevdata(sd);
437 return v4l2_chip_ident_i2c_client(client, chip, decoder->ident, 0);
440 /* ----------------------------------------------------------------------- */
442 static const struct v4l2_subdev_core_ops bt819_core_ops = {
443 .g_chip_ident = bt819_g_chip_ident,
444 .g_ctrl = bt819_g_ctrl,
445 .s_ctrl = bt819_s_ctrl,
446 .queryctrl = bt819_queryctrl,
449 static const struct v4l2_subdev_tuner_ops bt819_tuner_ops = {
450 .s_std = bt819_s_std,
453 static const struct v4l2_subdev_video_ops bt819_video_ops = {
454 .s_routing = bt819_s_routing,
455 .s_stream = bt819_s_stream,
456 .querystd = bt819_querystd,
457 .g_input_status = bt819_g_input_status,
460 static const struct v4l2_subdev_ops bt819_ops = {
461 .core = &bt819_core_ops,
462 .tuner = &bt819_tuner_ops,
463 .video = &bt819_video_ops,
466 /* ----------------------------------------------------------------------- */
468 static int bt819_probe(struct i2c_client *client,
469 const struct i2c_device_id *id)
472 struct bt819 *decoder;
473 struct v4l2_subdev *sd;
476 /* Check if the adapter supports the needed features */
477 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
480 decoder = kzalloc(sizeof(struct bt819), GFP_KERNEL);
484 v4l2_i2c_subdev_init(sd, client, &bt819_ops);
486 ver = bt819_read(decoder, 0x17);
487 switch (ver & 0xf0) {
490 decoder->ident = V4L2_IDENT_BT819A;
494 decoder->ident = V4L2_IDENT_BT817A;
498 decoder->ident = V4L2_IDENT_BT815A;
501 v4l2_dbg(1, debug, sd,
502 "unknown chip version 0x%02x\n", ver);
506 v4l_info(client, "%s found @ 0x%x (%s)\n", name,
507 client->addr << 1, client->adapter->name);
509 decoder->norm = V4L2_STD_NTSC;
513 decoder->contrast = 0xd8; /* 100% of original signal */
515 decoder->sat = 0xfe; /* 100% of original signal */
519 v4l2_dbg(1, debug, sd, "init status %d\n", i);
523 static int bt819_remove(struct i2c_client *client)
525 struct v4l2_subdev *sd = i2c_get_clientdata(client);
527 v4l2_device_unregister_subdev(sd);
532 /* ----------------------------------------------------------------------- */
534 static const struct i2c_device_id bt819_id[] = {
540 MODULE_DEVICE_TABLE(i2c, bt819_id);
542 static struct v4l2_i2c_driver_data v4l2_i2c_data = {
544 .probe = bt819_probe,
545 .remove = bt819_remove,
546 .id_table = bt819_id,