1// SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 saa6752hs - i2c-driver for the saa6752hs by Philips
4
5 Copyright (C) 2004 Andrew de Quincey
6
7 AC-3 support:
8
9 Copyright (C) 2008 Hans Verkuil <hverkuil@xs4all.nl>
10
11 */
12
13#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/string.h>
16#include <linux/timer.h>
17#include <linux/delay.h>
18#include <linux/errno.h>
19#include <linux/slab.h>
20#include <linux/poll.h>
21#include <linux/i2c.h>
22#include <linux/types.h>
23#include <linux/videodev2.h>
24#include <linux/init.h>
25#include <linux/crc32.h>
26#include <media/v4l2-device.h>
27#include <media/v4l2-ctrls.h>
28#include <media/v4l2-common.h>
29
30#define MPEG_VIDEO_TARGET_BITRATE_MAX 27000
31#define MPEG_VIDEO_MAX_BITRATE_MAX 27000
32#define MPEG_TOTAL_TARGET_BITRATE_MAX 27000
33#define MPEG_PID_MAX ((1 << 14) - 1)
34
35
36MODULE_DESCRIPTION("device driver for saa6752hs MPEG2 encoder");
37MODULE_AUTHOR("Andrew de Quincey");
38MODULE_LICENSE("GPL");
39
40enum saa6752hs_videoformat {
41 SAA6752HS_VF_D1 = 0, /* standard D1 video format: 720x576 */
42 SAA6752HS_VF_2_3_D1 = 1,/* 2/3D1 video format: 480x576 */
43 SAA6752HS_VF_1_2_D1 = 2,/* 1/2D1 video format: 352x576 */
44 SAA6752HS_VF_SIF = 3, /* SIF video format: 352x288 */
45 SAA6752HS_VF_UNKNOWN,
46};
47
48struct saa6752hs_mpeg_params {
49 /* transport streams */
50 __u16 ts_pid_pmt;
51 __u16 ts_pid_audio;
52 __u16 ts_pid_video;
53 __u16 ts_pid_pcr;
54
55 /* audio */
56 enum v4l2_mpeg_audio_encoding au_encoding;
57 enum v4l2_mpeg_audio_l2_bitrate au_l2_bitrate;
58 enum v4l2_mpeg_audio_ac3_bitrate au_ac3_bitrate;
59
60 /* video */
61 enum v4l2_mpeg_video_aspect vi_aspect;
62 enum v4l2_mpeg_video_bitrate_mode vi_bitrate_mode;
63 __u32 vi_bitrate;
64 __u32 vi_bitrate_peak;
65};
66
67static const struct v4l2_format v4l2_format_table[] =
68{
69 [SAA6752HS_VF_D1] =
70 { .fmt = { .pix = { .width = 720, .height = 576 }}},
71 [SAA6752HS_VF_2_3_D1] =
72 { .fmt = { .pix = { .width = 480, .height = 576 }}},
73 [SAA6752HS_VF_1_2_D1] =
74 { .fmt = { .pix = { .width = 352, .height = 576 }}},
75 [SAA6752HS_VF_SIF] =
76 { .fmt = { .pix = { .width = 352, .height = 288 }}},
77 [SAA6752HS_VF_UNKNOWN] =
78 { .fmt = { .pix = { .width = 0, .height = 0}}},
79};
80
81struct saa6752hs_state {
82 struct v4l2_subdev sd;
83 struct v4l2_ctrl_handler hdl;
84 struct { /* video bitrate mode control cluster */
85 struct v4l2_ctrl *video_bitrate_mode;
86 struct v4l2_ctrl *video_bitrate;
87 struct v4l2_ctrl *video_bitrate_peak;
88 };
89 u32 revision;
90 int has_ac3;
91 struct saa6752hs_mpeg_params params;
92 enum saa6752hs_videoformat video_format;
93 v4l2_std_id standard;
94};
95
96enum saa6752hs_command {
97 SAA6752HS_COMMAND_RESET = 0,
98 SAA6752HS_COMMAND_STOP = 1,
99 SAA6752HS_COMMAND_START = 2,
100 SAA6752HS_COMMAND_PAUSE = 3,
101 SAA6752HS_COMMAND_RECONFIGURE = 4,
102 SAA6752HS_COMMAND_SLEEP = 5,
103 SAA6752HS_COMMAND_RECONFIGURE_FORCE = 6,
104
105 SAA6752HS_COMMAND_MAX
106};
107
108static inline struct saa6752hs_state *to_state(struct v4l2_subdev *sd)
109{
110 return container_of(sd, struct saa6752hs_state, sd);
111}
112
113/* ---------------------------------------------------------------------- */
114
115static const u8 PAT[] = {
116 0xc2, /* i2c register */
117 0x00, /* table number for encoder */
118
119 0x47, /* sync */
120 0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid(0) */
121 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
122
123 0x00, /* PSI pointer to start of table */
124
125 0x00, /* tid(0) */
126 0xb0, 0x0d, /* section_syntax_indicator(1), section_length(13) */
127
128 0x00, 0x01, /* transport_stream_id(1) */
129
130 0xc1, /* version_number(0), current_next_indicator(1) */
131
132 0x00, 0x00, /* section_number(0), last_section_number(0) */
133
134 0x00, 0x01, /* program_number(1) */
135
136 0xe0, 0x00, /* PMT PID */
137
138 0x00, 0x00, 0x00, 0x00 /* CRC32 */
139};
140
141static const u8 PMT[] = {
142 0xc2, /* i2c register */
143 0x01, /* table number for encoder */
144
145 0x47, /* sync */
146 0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid */
147 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
148
149 0x00, /* PSI pointer to start of table */
150
151 0x02, /* tid(2) */
152 0xb0, 0x17, /* section_syntax_indicator(1), section_length(23) */
153
154 0x00, 0x01, /* program_number(1) */
155
156 0xc1, /* version_number(0), current_next_indicator(1) */
157
158 0x00, 0x00, /* section_number(0), last_section_number(0) */
159
160 0xe0, 0x00, /* PCR_PID */
161
162 0xf0, 0x00, /* program_info_length(0) */
163
164 0x02, 0xe0, 0x00, 0xf0, 0x00, /* video stream type(2), pid */
165 0x04, 0xe0, 0x00, 0xf0, 0x00, /* audio stream type(4), pid */
166
167 0x00, 0x00, 0x00, 0x00 /* CRC32 */
168};
169
170static const u8 PMT_AC3[] = {
171 0xc2, /* i2c register */
172 0x01, /* table number for encoder(1) */
173 0x47, /* sync */
174
175 0x40, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0) */
176 0x10, /* PMT PID (0x0010) */
177 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
178
179 0x00, /* PSI pointer to start of table */
180
181 0x02, /* TID (2) */
182 0xb0, 0x1a, /* section_syntax_indicator(1), section_length(26) */
183
184 0x00, 0x01, /* program_number(1) */
185
186 0xc1, /* version_number(0), current_next_indicator(1) */
187
188 0x00, 0x00, /* section_number(0), last_section_number(0) */
189
190 0xe1, 0x04, /* PCR_PID (0x0104) */
191
192 0xf0, 0x00, /* program_info_length(0) */
193
194 0x02, 0xe1, 0x00, 0xf0, 0x00, /* video stream type(2), pid */
195 0x06, 0xe1, 0x03, 0xf0, 0x03, /* audio stream type(6), pid */
196 0x6a, /* AC3 */
197 0x01, /* Descriptor_length(1) */
198 0x00, /* component_type_flag(0), bsid_flag(0), mainid_flag(0), asvc_flag(0), reserved flags(0) */
199
200 0xED, 0xDE, 0x2D, 0xF3 /* CRC32 BE */
201};
202
203static const struct saa6752hs_mpeg_params param_defaults =
204{
205 .ts_pid_pmt = 16,
206 .ts_pid_video = 260,
207 .ts_pid_audio = 256,
208 .ts_pid_pcr = 259,
209
210 .vi_aspect = V4L2_MPEG_VIDEO_ASPECT_4x3,
211 .vi_bitrate = 4000,
212 .vi_bitrate_peak = 6000,
213 .vi_bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
214
215 .au_encoding = V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
216 .au_l2_bitrate = V4L2_MPEG_AUDIO_L2_BITRATE_256K,
217 .au_ac3_bitrate = V4L2_MPEG_AUDIO_AC3_BITRATE_256K,
218};
219
220/* ---------------------------------------------------------------------- */
221
222static int saa6752hs_chip_command(struct i2c_client *client,
223 enum saa6752hs_command command)
224{
225 unsigned char buf[3];
226 unsigned long timeout;
227 int status = 0;
228
229 /* execute the command */
230 switch(command) {
231 case SAA6752HS_COMMAND_RESET:
232 buf[0] = 0x00;
233 break;
234
235 case SAA6752HS_COMMAND_STOP:
236 buf[0] = 0x03;
237 break;
238
239 case SAA6752HS_COMMAND_START:
240 buf[0] = 0x02;
241 break;
242
243 case SAA6752HS_COMMAND_PAUSE:
244 buf[0] = 0x04;
245 break;
246
247 case SAA6752HS_COMMAND_RECONFIGURE:
248 buf[0] = 0x05;
249 break;
250
251 case SAA6752HS_COMMAND_SLEEP:
252 buf[0] = 0x06;
253 break;
254
255 case SAA6752HS_COMMAND_RECONFIGURE_FORCE:
256 buf[0] = 0x07;
257 break;
258
259 default:
260 return -EINVAL;
261 }
262
263 /* set it and wait for it to be so */
264 i2c_master_send(client, buf, count: 1);
265 timeout = jiffies + HZ * 3;
266 for (;;) {
267 /* get the current status */
268 buf[0] = 0x10;
269 i2c_master_send(client, buf, count: 1);
270 i2c_master_recv(client, buf, count: 1);
271
272 if (!(buf[0] & 0x20))
273 break;
274 if (time_after(jiffies,timeout)) {
275 status = -ETIMEDOUT;
276 break;
277 }
278
279 msleep(msecs: 10);
280 }
281
282 /* delay a bit to let encoder settle */
283 msleep(msecs: 50);
284
285 return status;
286}
287
288
289static inline void set_reg8(struct i2c_client *client, uint8_t reg, uint8_t val)
290{
291 u8 buf[2];
292
293 buf[0] = reg;
294 buf[1] = val;
295 i2c_master_send(client, buf, count: 2);
296}
297
298static inline void set_reg16(struct i2c_client *client, uint8_t reg, uint16_t val)
299{
300 u8 buf[3];
301
302 buf[0] = reg;
303 buf[1] = val >> 8;
304 buf[2] = val & 0xff;
305 i2c_master_send(client, buf, count: 3);
306}
307
308static int saa6752hs_set_bitrate(struct i2c_client *client,
309 struct saa6752hs_state *h)
310{
311 struct saa6752hs_mpeg_params *params = &h->params;
312 int tot_bitrate;
313 int is_384k;
314
315 /* set the bitrate mode */
316 set_reg8(client, reg: 0x71,
317 val: params->vi_bitrate_mode != V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
318
319 /* set the video bitrate */
320 if (params->vi_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) {
321 /* set the target bitrate */
322 set_reg16(client, reg: 0x80, val: params->vi_bitrate);
323
324 /* set the max bitrate */
325 set_reg16(client, reg: 0x81, val: params->vi_bitrate_peak);
326 tot_bitrate = params->vi_bitrate_peak;
327 } else {
328 /* set the target bitrate (no max bitrate for CBR) */
329 set_reg16(client, reg: 0x81, val: params->vi_bitrate);
330 tot_bitrate = params->vi_bitrate;
331 }
332
333 /* set the audio encoding */
334 set_reg8(client, reg: 0x93,
335 val: params->au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3);
336
337 /* set the audio bitrate */
338 if (params->au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3)
339 is_384k = V4L2_MPEG_AUDIO_AC3_BITRATE_384K == params->au_ac3_bitrate;
340 else
341 is_384k = V4L2_MPEG_AUDIO_L2_BITRATE_384K == params->au_l2_bitrate;
342 set_reg8(client, reg: 0x94, val: is_384k);
343 tot_bitrate += is_384k ? 384 : 256;
344
345 /* Note: the total max bitrate is determined by adding the video and audio
346 bitrates together and also adding an extra 768kbit/s to stay on the
347 safe side. If more control should be required, then an extra MPEG control
348 should be added. */
349 tot_bitrate += 768;
350 if (tot_bitrate > MPEG_TOTAL_TARGET_BITRATE_MAX)
351 tot_bitrate = MPEG_TOTAL_TARGET_BITRATE_MAX;
352
353 /* set the total bitrate */
354 set_reg16(client, reg: 0xb1, val: tot_bitrate);
355 return 0;
356}
357
358static int saa6752hs_try_ctrl(struct v4l2_ctrl *ctrl)
359{
360 struct saa6752hs_state *h =
361 container_of(ctrl->handler, struct saa6752hs_state, hdl);
362
363 switch (ctrl->id) {
364 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
365 /* peak bitrate shall be >= normal bitrate */
366 if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
367 h->video_bitrate_peak->val < h->video_bitrate->val)
368 h->video_bitrate_peak->val = h->video_bitrate->val;
369 break;
370 }
371 return 0;
372}
373
374static int saa6752hs_s_ctrl(struct v4l2_ctrl *ctrl)
375{
376 struct saa6752hs_state *h =
377 container_of(ctrl->handler, struct saa6752hs_state, hdl);
378 struct saa6752hs_mpeg_params *params = &h->params;
379
380 switch (ctrl->id) {
381 case V4L2_CID_MPEG_STREAM_TYPE:
382 break;
383 case V4L2_CID_MPEG_STREAM_PID_PMT:
384 params->ts_pid_pmt = ctrl->val;
385 break;
386 case V4L2_CID_MPEG_STREAM_PID_AUDIO:
387 params->ts_pid_audio = ctrl->val;
388 break;
389 case V4L2_CID_MPEG_STREAM_PID_VIDEO:
390 params->ts_pid_video = ctrl->val;
391 break;
392 case V4L2_CID_MPEG_STREAM_PID_PCR:
393 params->ts_pid_pcr = ctrl->val;
394 break;
395 case V4L2_CID_MPEG_AUDIO_ENCODING:
396 params->au_encoding = ctrl->val;
397 break;
398 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
399 params->au_l2_bitrate = ctrl->val;
400 break;
401 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
402 params->au_ac3_bitrate = ctrl->val;
403 break;
404 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
405 break;
406 case V4L2_CID_MPEG_VIDEO_ENCODING:
407 break;
408 case V4L2_CID_MPEG_VIDEO_ASPECT:
409 params->vi_aspect = ctrl->val;
410 break;
411 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
412 params->vi_bitrate_mode = ctrl->val;
413 params->vi_bitrate = h->video_bitrate->val / 1000;
414 params->vi_bitrate_peak = h->video_bitrate_peak->val / 1000;
415 v4l2_ctrl_activate(ctrl: h->video_bitrate_peak,
416 active: ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
417 break;
418 default:
419 return -EINVAL;
420 }
421 return 0;
422}
423
424static int saa6752hs_init(struct v4l2_subdev *sd, u32 leading_null_bytes)
425{
426 unsigned char buf[9], buf2[4];
427 struct saa6752hs_state *h = to_state(sd);
428 struct i2c_client *client = v4l2_get_subdevdata(sd);
429 unsigned size;
430 u32 crc;
431 unsigned char localPAT[256];
432 unsigned char localPMT[256];
433
434 /* Set video format - must be done first as it resets other settings */
435 set_reg8(client, reg: 0x41, val: h->video_format);
436
437 /* Set number of lines in input signal */
438 set_reg8(client, reg: 0x40, val: (h->standard & V4L2_STD_525_60) ? 1 : 0);
439
440 /* set bitrate */
441 saa6752hs_set_bitrate(client, h);
442
443 /* Set GOP structure {3, 13} */
444 set_reg16(client, reg: 0x72, val: 0x030d);
445
446 /* Set minimum Q-scale {4} */
447 set_reg8(client, reg: 0x82, val: 0x04);
448
449 /* Set maximum Q-scale {12} */
450 set_reg8(client, reg: 0x83, val: 0x0c);
451
452 /* Set Output Protocol */
453 set_reg8(client, reg: 0xd0, val: 0x81);
454
455 /* Set video output stream format {TS} */
456 set_reg8(client, reg: 0xb0, val: 0x05);
457
458 /* Set leading null byte for TS */
459 set_reg16(client, reg: 0xf6, val: leading_null_bytes);
460
461 /* compute PAT */
462 memcpy(localPAT, PAT, sizeof(PAT));
463 localPAT[17] = 0xe0 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
464 localPAT[18] = h->params.ts_pid_pmt & 0xff;
465 crc = crc32_be(crc: ~0, p: &localPAT[7], len: sizeof(PAT) - 7 - 4);
466 localPAT[sizeof(PAT) - 4] = (crc >> 24) & 0xFF;
467 localPAT[sizeof(PAT) - 3] = (crc >> 16) & 0xFF;
468 localPAT[sizeof(PAT) - 2] = (crc >> 8) & 0xFF;
469 localPAT[sizeof(PAT) - 1] = crc & 0xFF;
470
471 /* compute PMT */
472 if (h->params.au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3) {
473 size = sizeof(PMT_AC3);
474 memcpy(localPMT, PMT_AC3, size);
475 } else {
476 size = sizeof(PMT);
477 memcpy(localPMT, PMT, size);
478 }
479 localPMT[3] = 0x40 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
480 localPMT[4] = h->params.ts_pid_pmt & 0xff;
481 localPMT[15] = 0xE0 | ((h->params.ts_pid_pcr >> 8) & 0x0F);
482 localPMT[16] = h->params.ts_pid_pcr & 0xFF;
483 localPMT[20] = 0xE0 | ((h->params.ts_pid_video >> 8) & 0x0F);
484 localPMT[21] = h->params.ts_pid_video & 0xFF;
485 localPMT[25] = 0xE0 | ((h->params.ts_pid_audio >> 8) & 0x0F);
486 localPMT[26] = h->params.ts_pid_audio & 0xFF;
487 crc = crc32_be(crc: ~0, p: &localPMT[7], len: size - 7 - 4);
488 localPMT[size - 4] = (crc >> 24) & 0xFF;
489 localPMT[size - 3] = (crc >> 16) & 0xFF;
490 localPMT[size - 2] = (crc >> 8) & 0xFF;
491 localPMT[size - 1] = crc & 0xFF;
492
493 /* Set Audio PID */
494 set_reg16(client, reg: 0xc1, val: h->params.ts_pid_audio);
495
496 /* Set Video PID */
497 set_reg16(client, reg: 0xc0, val: h->params.ts_pid_video);
498
499 /* Set PCR PID */
500 set_reg16(client, reg: 0xc4, val: h->params.ts_pid_pcr);
501
502 /* Send SI tables */
503 i2c_master_send(client, buf: localPAT, count: sizeof(PAT));
504 i2c_master_send(client, buf: localPMT, count: size);
505
506 /* mute then unmute audio. This removes buzzing artefacts */
507 set_reg8(client, reg: 0xa4, val: 1);
508 set_reg8(client, reg: 0xa4, val: 0);
509
510 /* start it going */
511 saa6752hs_chip_command(client, command: SAA6752HS_COMMAND_START);
512
513 /* readout current state */
514 buf[0] = 0xE1;
515 buf[1] = 0xA7;
516 buf[2] = 0xFE;
517 buf[3] = 0x82;
518 buf[4] = 0xB0;
519 i2c_master_send(client, buf, count: 5);
520 i2c_master_recv(client, buf: buf2, count: 4);
521
522 /* change aspect ratio */
523 buf[0] = 0xE0;
524 buf[1] = 0xA7;
525 buf[2] = 0xFE;
526 buf[3] = 0x82;
527 buf[4] = 0xB0;
528 buf[5] = buf2[0];
529 switch (h->params.vi_aspect) {
530 case V4L2_MPEG_VIDEO_ASPECT_16x9:
531 buf[6] = buf2[1] | 0x40;
532 break;
533 case V4L2_MPEG_VIDEO_ASPECT_4x3:
534 default:
535 buf[6] = buf2[1] & 0xBF;
536 break;
537 }
538 buf[7] = buf2[2];
539 buf[8] = buf2[3];
540 i2c_master_send(client, buf, count: 9);
541
542 return 0;
543}
544
545static int saa6752hs_get_fmt(struct v4l2_subdev *sd,
546 struct v4l2_subdev_state *sd_state,
547 struct v4l2_subdev_format *format)
548{
549 struct v4l2_mbus_framefmt *f = &format->format;
550 struct saa6752hs_state *h = to_state(sd);
551
552 if (format->pad)
553 return -EINVAL;
554
555 if (h->video_format == SAA6752HS_VF_UNKNOWN)
556 h->video_format = SAA6752HS_VF_D1;
557 f->width = v4l2_format_table[h->video_format].fmt.pix.width;
558 f->height = v4l2_format_table[h->video_format].fmt.pix.height;
559 f->code = MEDIA_BUS_FMT_FIXED;
560 f->field = V4L2_FIELD_INTERLACED;
561 f->colorspace = V4L2_COLORSPACE_SMPTE170M;
562 return 0;
563}
564
565static int saa6752hs_set_fmt(struct v4l2_subdev *sd,
566 struct v4l2_subdev_state *sd_state,
567 struct v4l2_subdev_format *format)
568{
569 struct v4l2_mbus_framefmt *f = &format->format;
570 struct saa6752hs_state *h = to_state(sd);
571 int dist_352, dist_480, dist_720;
572
573 if (format->pad)
574 return -EINVAL;
575
576 f->code = MEDIA_BUS_FMT_FIXED;
577
578 dist_352 = abs(f->width - 352);
579 dist_480 = abs(f->width - 480);
580 dist_720 = abs(f->width - 720);
581 if (dist_720 < dist_480) {
582 f->width = 720;
583 f->height = 576;
584 } else if (dist_480 < dist_352) {
585 f->width = 480;
586 f->height = 576;
587 } else {
588 f->width = 352;
589 if (abs(f->height - 576) < abs(f->height - 288))
590 f->height = 576;
591 else
592 f->height = 288;
593 }
594 f->field = V4L2_FIELD_INTERLACED;
595 f->colorspace = V4L2_COLORSPACE_SMPTE170M;
596
597 if (format->which == V4L2_SUBDEV_FORMAT_TRY)
598 return 0;
599
600 /*
601 FIXME: translate and round width/height into EMPRESS
602 subsample type:
603
604 type | PAL | NTSC
605 ---------------------------
606 SIF | 352x288 | 352x240
607 1/2 D1 | 352x576 | 352x480
608 2/3 D1 | 480x576 | 480x480
609 D1 | 720x576 | 720x480
610 */
611
612 if (f->code != MEDIA_BUS_FMT_FIXED)
613 return -EINVAL;
614
615 if (f->width == 720)
616 h->video_format = SAA6752HS_VF_D1;
617 else if (f->width == 480)
618 h->video_format = SAA6752HS_VF_2_3_D1;
619 else if (f->height == 576)
620 h->video_format = SAA6752HS_VF_1_2_D1;
621 else
622 h->video_format = SAA6752HS_VF_SIF;
623 return 0;
624}
625
626static int saa6752hs_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
627{
628 struct saa6752hs_state *h = to_state(sd);
629
630 h->standard = std;
631 return 0;
632}
633
634/* ----------------------------------------------------------------------- */
635
636static const struct v4l2_ctrl_ops saa6752hs_ctrl_ops = {
637 .try_ctrl = saa6752hs_try_ctrl,
638 .s_ctrl = saa6752hs_s_ctrl,
639};
640
641static const struct v4l2_subdev_core_ops saa6752hs_core_ops = {
642 .init = saa6752hs_init,
643};
644
645static const struct v4l2_subdev_video_ops saa6752hs_video_ops = {
646 .s_std = saa6752hs_s_std,
647};
648
649static const struct v4l2_subdev_pad_ops saa6752hs_pad_ops = {
650 .get_fmt = saa6752hs_get_fmt,
651 .set_fmt = saa6752hs_set_fmt,
652};
653
654static const struct v4l2_subdev_ops saa6752hs_ops = {
655 .core = &saa6752hs_core_ops,
656 .video = &saa6752hs_video_ops,
657 .pad = &saa6752hs_pad_ops,
658};
659
660static int saa6752hs_probe(struct i2c_client *client)
661{
662 struct saa6752hs_state *h;
663 struct v4l2_subdev *sd;
664 struct v4l2_ctrl_handler *hdl;
665 u8 addr = 0x13;
666 u8 data[12];
667
668 v4l_info(client, "chip found @ 0x%x (%s)\n",
669 client->addr << 1, client->adapter->name);
670
671 h = devm_kzalloc(dev: &client->dev, size: sizeof(*h), GFP_KERNEL);
672 if (h == NULL)
673 return -ENOMEM;
674 sd = &h->sd;
675 v4l2_i2c_subdev_init(sd, client, ops: &saa6752hs_ops);
676
677 i2c_master_send(client, buf: &addr, count: 1);
678 i2c_master_recv(client, buf: data, count: sizeof(data));
679 h->revision = (data[8] << 8) | data[9];
680 h->has_ac3 = 0;
681 if (h->revision == 0x0206) {
682 h->has_ac3 = 1;
683 v4l_info(client, "supports AC-3\n");
684 }
685 h->params = param_defaults;
686
687 hdl = &h->hdl;
688 v4l2_ctrl_handler_init(hdl, 14);
689 v4l2_ctrl_new_std_menu(hdl, ops: &saa6752hs_ctrl_ops,
690 V4L2_CID_MPEG_AUDIO_ENCODING,
691 max: h->has_ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3 :
692 V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
693 mask: 0x0d, def: V4L2_MPEG_AUDIO_ENCODING_LAYER_2);
694
695 v4l2_ctrl_new_std_menu(hdl, ops: &saa6752hs_ctrl_ops,
696 V4L2_CID_MPEG_AUDIO_L2_BITRATE,
697 max: V4L2_MPEG_AUDIO_L2_BITRATE_384K,
698 mask: ~((1 << V4L2_MPEG_AUDIO_L2_BITRATE_256K) |
699 (1 << V4L2_MPEG_AUDIO_L2_BITRATE_384K)),
700 def: V4L2_MPEG_AUDIO_L2_BITRATE_256K);
701
702 if (h->has_ac3)
703 v4l2_ctrl_new_std_menu(hdl, ops: &saa6752hs_ctrl_ops,
704 V4L2_CID_MPEG_AUDIO_AC3_BITRATE,
705 max: V4L2_MPEG_AUDIO_AC3_BITRATE_384K,
706 mask: ~((1 << V4L2_MPEG_AUDIO_AC3_BITRATE_256K) |
707 (1 << V4L2_MPEG_AUDIO_AC3_BITRATE_384K)),
708 def: V4L2_MPEG_AUDIO_AC3_BITRATE_256K);
709
710 v4l2_ctrl_new_std_menu(hdl, ops: &saa6752hs_ctrl_ops,
711 V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
712 max: V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000,
713 mask: ~(1 << V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000),
714 def: V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000);
715
716 v4l2_ctrl_new_std_menu(hdl, ops: &saa6752hs_ctrl_ops,
717 V4L2_CID_MPEG_VIDEO_ENCODING,
718 max: V4L2_MPEG_VIDEO_ENCODING_MPEG_2,
719 mask: ~(1 << V4L2_MPEG_VIDEO_ENCODING_MPEG_2),
720 def: V4L2_MPEG_VIDEO_ENCODING_MPEG_2);
721
722 v4l2_ctrl_new_std_menu(hdl, ops: &saa6752hs_ctrl_ops,
723 V4L2_CID_MPEG_VIDEO_ASPECT,
724 max: V4L2_MPEG_VIDEO_ASPECT_16x9, mask: 0x01,
725 def: V4L2_MPEG_VIDEO_ASPECT_4x3);
726
727 h->video_bitrate_peak = v4l2_ctrl_new_std(hdl, ops: &saa6752hs_ctrl_ops,
728 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
729 min: 1000000, max: 27000000, step: 1000, def: 8000000);
730
731 v4l2_ctrl_new_std_menu(hdl, ops: &saa6752hs_ctrl_ops,
732 V4L2_CID_MPEG_STREAM_TYPE,
733 max: V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
734 mask: ~(1 << V4L2_MPEG_STREAM_TYPE_MPEG2_TS),
735 def: V4L2_MPEG_STREAM_TYPE_MPEG2_TS);
736
737 h->video_bitrate_mode = v4l2_ctrl_new_std_menu(hdl, ops: &saa6752hs_ctrl_ops,
738 V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
739 max: V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, mask: 0,
740 def: V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
741 h->video_bitrate = v4l2_ctrl_new_std(hdl, ops: &saa6752hs_ctrl_ops,
742 V4L2_CID_MPEG_VIDEO_BITRATE, min: 1000000, max: 27000000, step: 1000, def: 6000000);
743 v4l2_ctrl_new_std(hdl, ops: &saa6752hs_ctrl_ops,
744 V4L2_CID_MPEG_STREAM_PID_PMT, min: 0, max: (1 << 14) - 1, step: 1, def: 16);
745 v4l2_ctrl_new_std(hdl, ops: &saa6752hs_ctrl_ops,
746 V4L2_CID_MPEG_STREAM_PID_AUDIO, min: 0, max: (1 << 14) - 1, step: 1, def: 260);
747 v4l2_ctrl_new_std(hdl, ops: &saa6752hs_ctrl_ops,
748 V4L2_CID_MPEG_STREAM_PID_VIDEO, min: 0, max: (1 << 14) - 1, step: 1, def: 256);
749 v4l2_ctrl_new_std(hdl, ops: &saa6752hs_ctrl_ops,
750 V4L2_CID_MPEG_STREAM_PID_PCR, min: 0, max: (1 << 14) - 1, step: 1, def: 259);
751 sd->ctrl_handler = hdl;
752 if (hdl->error) {
753 int err = hdl->error;
754
755 v4l2_ctrl_handler_free(hdl);
756 return err;
757 }
758 v4l2_ctrl_cluster(ncontrols: 3, controls: &h->video_bitrate_mode);
759 v4l2_ctrl_handler_setup(hdl);
760 h->standard = 0; /* Assume 625 input lines */
761 return 0;
762}
763
764static void saa6752hs_remove(struct i2c_client *client)
765{
766 struct v4l2_subdev *sd = i2c_get_clientdata(client);
767
768 v4l2_device_unregister_subdev(sd);
769 v4l2_ctrl_handler_free(hdl: &to_state(sd)->hdl);
770}
771
772static const struct i2c_device_id saa6752hs_id[] = {
773 { "saa6752hs", 0 },
774 { }
775};
776MODULE_DEVICE_TABLE(i2c, saa6752hs_id);
777
778static struct i2c_driver saa6752hs_driver = {
779 .driver = {
780 .name = "saa6752hs",
781 },
782 .probe = saa6752hs_probe,
783 .remove = saa6752hs_remove,
784 .id_table = saa6752hs_id,
785};
786
787module_i2c_driver(saa6752hs_driver);
788

source code of linux/drivers/media/i2c/saa6752hs.c