1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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>
42
43MODULE_DESCRIPTION("Brooktree-819 video decoder driver");
44MODULE_AUTHOR("Mike Bernson & Dave Perks");
45MODULE_LICENSE("GPL");
46
47static int debug;
48module_param(debug, int, 0);
49MODULE_PARM_DESC(debug, "Debug level (0-1)");
50
51
52
53
54struct bt819 {
55 struct v4l2_subdev sd;
56 unsigned char reg[32];
57
58 v4l2_std_id norm;
59 int ident;
60 int input;
61 int enable;
62 int bright;
63 int contrast;
64 int hue;
65 int sat;
66};
67
68static inline struct bt819 *to_bt819(struct v4l2_subdev *sd)
69{
70 return container_of(sd, struct bt819, sd);
71}
72
73struct timing {
74 int hactive;
75 int hdelay;
76 int vactive;
77 int vdelay;
78 int hscale;
79 int vscale;
80};
81
82
83static struct timing timing_data[] = {
84 {864 - 24, 20, 625 - 2, 1, 0x0504, 0x0000},
85 {858 - 24, 20, 525 - 2, 1, 0x00f8, 0x0000},
86};
87
88
89
90static inline int bt819_write(struct bt819 *decoder, u8 reg, u8 value)
91{
92 struct i2c_client *client = v4l2_get_subdevdata(&decoder->sd);
93
94 decoder->reg[reg] = value;
95 return i2c_smbus_write_byte_data(client, reg, value);
96}
97
98static inline int bt819_setbit(struct bt819 *decoder, u8 reg, u8 bit, u8 value)
99{
100 return bt819_write(decoder, reg,
101 (decoder->reg[reg] & ~(1 << bit)) | (value ? (1 << bit) : 0));
102}
103
104static int bt819_write_block(struct bt819 *decoder, const u8 *data, unsigned int len)
105{
106 struct i2c_client *client = v4l2_get_subdevdata(&decoder->sd);
107 int ret = -1;
108 u8 reg;
109
110
111
112 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
113
114 u8 block_data[32];
115 int block_len;
116
117 while (len >= 2) {
118 block_len = 0;
119 block_data[block_len++] = reg = data[0];
120 do {
121 block_data[block_len++] =
122 decoder->reg[reg++] = data[1];
123 len -= 2;
124 data += 2;
125 } while (len >= 2 && data[0] == reg && block_len < 32);
126 ret = i2c_master_send(client, block_data, block_len);
127 if (ret < 0)
128 break;
129 }
130 } else {
131
132 while (len >= 2) {
133 reg = *data++;
134 ret = bt819_write(decoder, reg, *data++);
135 if (ret < 0)
136 break;
137 len -= 2;
138 }
139 }
140
141 return ret;
142}
143
144static inline int bt819_read(struct bt819 *decoder, u8 reg)
145{
146 struct i2c_client *client = v4l2_get_subdevdata(&decoder->sd);
147
148 return i2c_smbus_read_byte_data(client, reg);
149}
150
151static int bt819_init(struct v4l2_subdev *sd)
152{
153 static unsigned char init[] = {
154
155 0x01, 0x59,
156 0x02, 0x00,
157 0x03, 0x12,
158 0x04, 0x16,
159 0x05, 0xe0,
160 0x06, 0x80,
161 0x07, 0xd0,
162 0x08, 0x00,
163 0x09, 0xf8,
164 0x0a, 0x00,
165 0x0b, 0x30,
166 0x0c, 0xd8,
167 0x0d, 0xfe,
168 0x0e, 0xb4,
169 0x0f, 0x00,
170 0x12, 0x04,
171 0x13, 0x20,
172
173
174
175
176 0x14, 0x00,
177 0x16, 0x07,
178
179
180
181
182 0x18, 0x68,
183 0x19, 0x5d,
184 0x1a, 0x80,
185 };
186
187 struct bt819 *decoder = to_bt819(sd);
188 struct timing *timing = &timing_data[(decoder->norm & V4L2_STD_525_60) ? 1 : 0];
189
190 init[0x03 * 2 - 1] =
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
202 init[0x15 * 2 - 1] = (decoder->norm & V4L2_STD_625_50) ? 115 : 93;
203
204 bt819_write(decoder, 0x1f, 0x00);
205 mdelay(1);
206
207
208 return bt819_write_block(decoder, init, sizeof(init));
209}
210
211
212
213static int bt819_status(struct v4l2_subdev *sd, u32 *pstatus, v4l2_std_id *pstd)
214{
215 struct bt819 *decoder = to_bt819(sd);
216 int status = bt819_read(decoder, 0x00);
217 int res = V4L2_IN_ST_NO_SIGNAL;
218 v4l2_std_id std;
219
220 if ((status & 0x80))
221 res = 0;
222
223 if ((status & 0x10))
224 std = V4L2_STD_PAL;
225 else
226 std = V4L2_STD_NTSC;
227 if (pstd)
228 *pstd = std;
229 if (pstatus)
230 *pstatus = status;
231
232 v4l2_dbg(1, debug, sd, "get status %x\n", status);
233 return 0;
234}
235
236static int bt819_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
237{
238 return bt819_status(sd, NULL, std);
239}
240
241static int bt819_g_input_status(struct v4l2_subdev *sd, u32 *status)
242{
243 return bt819_status(sd, status, NULL);
244}
245
246static int bt819_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
247{
248 struct bt819 *decoder = to_bt819(sd);
249 struct timing *timing = NULL;
250
251 v4l2_dbg(1, debug, sd, "set norm %llx\n", (unsigned long long)std);
252
253 if (sd->v4l2_dev == NULL || sd->v4l2_dev->notify == NULL)
254 v4l2_err(sd, "no notify found!\n");
255
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
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
273 timing = &timing_data[0];
274 } else {
275 v4l2_dbg(1, debug, sd, "unsupported norm %llx\n",
276 (unsigned long long)std);
277 return -EINVAL;
278 }
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);
290 decoder->norm = std;
291 v4l2_subdev_notify(sd, BT819_FIFO_RESET_HIGH, 0);
292 return 0;
293}
294
295static int bt819_s_routing(struct v4l2_subdev *sd,
296 u32 input, u32 output, u32 config)
297{
298 struct bt819 *decoder = to_bt819(sd);
299
300 v4l2_dbg(1, debug, sd, "set input %x\n", input);
301
302 if (input < 0 || input > 7)
303 return -EINVAL;
304
305 if (sd->v4l2_dev == NULL || sd->v4l2_dev->notify == NULL)
306 v4l2_err(sd, "no notify found!\n");
307
308 if (decoder->input != input) {
309 v4l2_subdev_notify(sd, BT819_FIFO_RESET_LOW, 0);
310 decoder->input = input;
311
312 if (decoder->input == 0) {
313 bt819_setbit(decoder, 0x0b, 6, 0);
314 bt819_setbit(decoder, 0x1a, 1, 1);
315 } else {
316 bt819_setbit(decoder, 0x0b, 6, 1);
317 bt819_setbit(decoder, 0x1a, 1, 0);
318 }
319 v4l2_subdev_notify(sd, BT819_FIFO_RESET_HIGH, 0);
320 }
321 return 0;
322}
323
324static int bt819_s_stream(struct v4l2_subdev *sd, int enable)
325{
326 struct bt819 *decoder = to_bt819(sd);
327
328 v4l2_dbg(1, debug, sd, "enable output %x\n", enable);
329
330 if (decoder->enable != enable) {
331 decoder->enable = enable;
332 bt819_setbit(decoder, 0x16, 7, !enable);
333 }
334 return 0;
335}
336
337static int bt819_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
338{
339 switch (qc->id) {
340 case V4L2_CID_BRIGHTNESS:
341 v4l2_ctrl_query_fill(qc, -128, 127, 1, 0);
342 break;
343
344 case V4L2_CID_CONTRAST:
345 v4l2_ctrl_query_fill(qc, 0, 511, 1, 256);
346 break;
347
348 case V4L2_CID_SATURATION:
349 v4l2_ctrl_query_fill(qc, 0, 511, 1, 256);
350 break;
351
352 case V4L2_CID_HUE:
353 v4l2_ctrl_query_fill(qc, -128, 127, 1, 0);
354 break;
355
356 default:
357 return -EINVAL;
358 }
359 return 0;
360}
361
362static int bt819_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
363{
364 struct bt819 *decoder = to_bt819(sd);
365 int temp;
366
367 switch (ctrl->id) {
368 case V4L2_CID_BRIGHTNESS:
369 if (decoder->bright == ctrl->value)
370 break;
371 decoder->bright = ctrl->value;
372 bt819_write(decoder, 0x0a, decoder->bright);
373 break;
374
375 case V4L2_CID_CONTRAST:
376 if (decoder->contrast == ctrl->value)
377 break;
378 decoder->contrast = ctrl->value;
379 bt819_write(decoder, 0x0c, decoder->contrast & 0xff);
380 bt819_setbit(decoder, 0x0b, 2, ((decoder->contrast >> 8) & 0x01));
381 break;
382
383 case V4L2_CID_SATURATION:
384 if (decoder->sat == ctrl->value)
385 break;
386 decoder->sat = ctrl->value;
387 bt819_write(decoder, 0x0d, (decoder->sat >> 7) & 0xff);
388 bt819_setbit(decoder, 0x0b, 1, ((decoder->sat >> 15) & 0x01));
389
390
391
392 temp = (decoder->sat * 180) / 254;
393 bt819_write(decoder, 0x0e, (temp >> 7) & 0xff);
394 bt819_setbit(decoder, 0x0b, 0, (temp >> 15) & 0x01);
395 break;
396
397 case V4L2_CID_HUE:
398 if (decoder->hue == ctrl->value)
399 break;
400 decoder->hue = ctrl->value;
401 bt819_write(decoder, 0x0f, decoder->hue);
402 break;
403
404 default:
405 return -EINVAL;
406 }
407 return 0;
408}
409
410static int bt819_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
411{
412 struct bt819 *decoder = to_bt819(sd);
413
414 switch (ctrl->id) {
415 case V4L2_CID_BRIGHTNESS:
416 ctrl->value = decoder->bright;
417 break;
418 case V4L2_CID_CONTRAST:
419 ctrl->value = decoder->contrast;
420 break;
421 case V4L2_CID_SATURATION:
422 ctrl->value = decoder->sat;
423 break;
424 case V4L2_CID_HUE:
425 ctrl->value = decoder->hue;
426 break;
427 default:
428 return -EINVAL;
429 }
430 return 0;
431}
432
433static int bt819_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
434{
435 struct bt819 *decoder = to_bt819(sd);
436 struct i2c_client *client = v4l2_get_subdevdata(sd);
437
438 return v4l2_chip_ident_i2c_client(client, chip, decoder->ident, 0);
439}
440
441
442
443static const struct v4l2_subdev_core_ops bt819_core_ops = {
444 .g_chip_ident = bt819_g_chip_ident,
445 .g_ctrl = bt819_g_ctrl,
446 .s_ctrl = bt819_s_ctrl,
447 .queryctrl = bt819_queryctrl,
448 .s_std = bt819_s_std,
449};
450
451static const struct v4l2_subdev_video_ops bt819_video_ops = {
452 .s_routing = bt819_s_routing,
453 .s_stream = bt819_s_stream,
454 .querystd = bt819_querystd,
455 .g_input_status = bt819_g_input_status,
456};
457
458static const struct v4l2_subdev_ops bt819_ops = {
459 .core = &bt819_core_ops,
460 .video = &bt819_video_ops,
461};
462
463
464
465static int bt819_probe(struct i2c_client *client,
466 const struct i2c_device_id *id)
467{
468 int i, ver;
469 struct bt819 *decoder;
470 struct v4l2_subdev *sd;
471 const char *name;
472
473
474 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
475 return -ENODEV;
476
477 decoder = kzalloc(sizeof(struct bt819), GFP_KERNEL);
478 if (decoder == NULL)
479 return -ENOMEM;
480 sd = &decoder->sd;
481 v4l2_i2c_subdev_init(sd, client, &bt819_ops);
482
483 ver = bt819_read(decoder, 0x17);
484 switch (ver & 0xf0) {
485 case 0x70:
486 name = "bt819a";
487 decoder->ident = V4L2_IDENT_BT819A;
488 break;
489 case 0x60:
490 name = "bt817a";
491 decoder->ident = V4L2_IDENT_BT817A;
492 break;
493 case 0x20:
494 name = "bt815a";
495 decoder->ident = V4L2_IDENT_BT815A;
496 break;
497 default:
498 v4l2_dbg(1, debug, sd,
499 "unknown chip version 0x%02x\n", ver);
500 return -ENODEV;
501 }
502
503 v4l_info(client, "%s found @ 0x%x (%s)\n", name,
504 client->addr << 1, client->adapter->name);
505
506 decoder->norm = V4L2_STD_NTSC;
507 decoder->input = 0;
508 decoder->enable = 1;
509 decoder->bright = 0;
510 decoder->contrast = 0xd8;
511 decoder->hue = 0;
512 decoder->sat = 0xfe;
513
514 i = bt819_init(sd);
515 if (i < 0)
516 v4l2_dbg(1, debug, sd, "init status %d\n", i);
517 return 0;
518}
519
520static int bt819_remove(struct i2c_client *client)
521{
522 struct v4l2_subdev *sd = i2c_get_clientdata(client);
523
524 v4l2_device_unregister_subdev(sd);
525 kfree(to_bt819(sd));
526 return 0;
527}
528
529
530
531static const struct i2c_device_id bt819_id[] = {
532 { "bt819a", 0 },
533 { "bt817a", 0 },
534 { "bt815a", 0 },
535 { }
536};
537MODULE_DEVICE_TABLE(i2c, bt819_id);
538
539static struct v4l2_i2c_driver_data v4l2_i2c_data = {
540 .name = "bt819",
541 .probe = bt819_probe,
542 .remove = bt819_remove,
543 .id_table = bt819_id,
544};
545