Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002  /*
0003     saa6752hs - i2c-driver for the saa6752hs by Philips
0004 
0005     Copyright (C) 2004 Andrew de Quincey
0006 
0007     AC-3 support:
0008 
0009     Copyright (C) 2008 Hans Verkuil <hverkuil@xs4all.nl>
0010 
0011   */
0012 
0013 #include <linux/module.h>
0014 #include <linux/kernel.h>
0015 #include <linux/string.h>
0016 #include <linux/timer.h>
0017 #include <linux/delay.h>
0018 #include <linux/errno.h>
0019 #include <linux/slab.h>
0020 #include <linux/poll.h>
0021 #include <linux/i2c.h>
0022 #include <linux/types.h>
0023 #include <linux/videodev2.h>
0024 #include <linux/init.h>
0025 #include <linux/crc32.h>
0026 #include <media/v4l2-device.h>
0027 #include <media/v4l2-ctrls.h>
0028 #include <media/v4l2-common.h>
0029 
0030 #define MPEG_VIDEO_TARGET_BITRATE_MAX  27000
0031 #define MPEG_VIDEO_MAX_BITRATE_MAX     27000
0032 #define MPEG_TOTAL_TARGET_BITRATE_MAX  27000
0033 #define MPEG_PID_MAX ((1 << 14) - 1)
0034 
0035 
0036 MODULE_DESCRIPTION("device driver for saa6752hs MPEG2 encoder");
0037 MODULE_AUTHOR("Andrew de Quincey");
0038 MODULE_LICENSE("GPL");
0039 
0040 enum saa6752hs_videoformat {
0041     SAA6752HS_VF_D1 = 0,    /* standard D1 video format: 720x576 */
0042     SAA6752HS_VF_2_3_D1 = 1,/* 2/3D1 video format: 480x576 */
0043     SAA6752HS_VF_1_2_D1 = 2,/* 1/2D1 video format: 352x576 */
0044     SAA6752HS_VF_SIF = 3,   /* SIF video format: 352x288 */
0045     SAA6752HS_VF_UNKNOWN,
0046 };
0047 
0048 struct saa6752hs_mpeg_params {
0049     /* transport streams */
0050     __u16               ts_pid_pmt;
0051     __u16               ts_pid_audio;
0052     __u16               ts_pid_video;
0053     __u16               ts_pid_pcr;
0054 
0055     /* audio */
0056     enum v4l2_mpeg_audio_encoding    au_encoding;
0057     enum v4l2_mpeg_audio_l2_bitrate  au_l2_bitrate;
0058     enum v4l2_mpeg_audio_ac3_bitrate au_ac3_bitrate;
0059 
0060     /* video */
0061     enum v4l2_mpeg_video_aspect vi_aspect;
0062     enum v4l2_mpeg_video_bitrate_mode vi_bitrate_mode;
0063     __u32               vi_bitrate;
0064     __u32               vi_bitrate_peak;
0065 };
0066 
0067 static const struct v4l2_format v4l2_format_table[] =
0068 {
0069     [SAA6752HS_VF_D1] =
0070         { .fmt = { .pix = { .width = 720, .height = 576 }}},
0071     [SAA6752HS_VF_2_3_D1] =
0072         { .fmt = { .pix = { .width = 480, .height = 576 }}},
0073     [SAA6752HS_VF_1_2_D1] =
0074         { .fmt = { .pix = { .width = 352, .height = 576 }}},
0075     [SAA6752HS_VF_SIF] =
0076         { .fmt = { .pix = { .width = 352, .height = 288 }}},
0077     [SAA6752HS_VF_UNKNOWN] =
0078         { .fmt = { .pix = { .width = 0, .height = 0}}},
0079 };
0080 
0081 struct saa6752hs_state {
0082     struct v4l2_subdev            sd;
0083     struct v4l2_ctrl_handler      hdl;
0084     struct { /* video bitrate mode control cluster */
0085         struct v4l2_ctrl *video_bitrate_mode;
0086         struct v4l2_ctrl *video_bitrate;
0087         struct v4l2_ctrl *video_bitrate_peak;
0088     };
0089     u32               revision;
0090     int               has_ac3;
0091     struct saa6752hs_mpeg_params  params;
0092     enum saa6752hs_videoformat    video_format;
0093     v4l2_std_id                   standard;
0094 };
0095 
0096 enum saa6752hs_command {
0097     SAA6752HS_COMMAND_RESET = 0,
0098     SAA6752HS_COMMAND_STOP = 1,
0099     SAA6752HS_COMMAND_START = 2,
0100     SAA6752HS_COMMAND_PAUSE = 3,
0101     SAA6752HS_COMMAND_RECONFIGURE = 4,
0102     SAA6752HS_COMMAND_SLEEP = 5,
0103     SAA6752HS_COMMAND_RECONFIGURE_FORCE = 6,
0104 
0105     SAA6752HS_COMMAND_MAX
0106 };
0107 
0108 static inline struct saa6752hs_state *to_state(struct v4l2_subdev *sd)
0109 {
0110     return container_of(sd, struct saa6752hs_state, sd);
0111 }
0112 
0113 /* ---------------------------------------------------------------------- */
0114 
0115 static const u8 PAT[] = {
0116     0xc2, /* i2c register */
0117     0x00, /* table number for encoder */
0118 
0119     0x47, /* sync */
0120     0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid(0) */
0121     0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
0122 
0123     0x00, /* PSI pointer to start of table */
0124 
0125     0x00, /* tid(0) */
0126     0xb0, 0x0d, /* section_syntax_indicator(1), section_length(13) */
0127 
0128     0x00, 0x01, /* transport_stream_id(1) */
0129 
0130     0xc1, /* version_number(0), current_next_indicator(1) */
0131 
0132     0x00, 0x00, /* section_number(0), last_section_number(0) */
0133 
0134     0x00, 0x01, /* program_number(1) */
0135 
0136     0xe0, 0x00, /* PMT PID */
0137 
0138     0x00, 0x00, 0x00, 0x00 /* CRC32 */
0139 };
0140 
0141 static const u8 PMT[] = {
0142     0xc2, /* i2c register */
0143     0x01, /* table number for encoder */
0144 
0145     0x47, /* sync */
0146     0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid */
0147     0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
0148 
0149     0x00, /* PSI pointer to start of table */
0150 
0151     0x02, /* tid(2) */
0152     0xb0, 0x17, /* section_syntax_indicator(1), section_length(23) */
0153 
0154     0x00, 0x01, /* program_number(1) */
0155 
0156     0xc1, /* version_number(0), current_next_indicator(1) */
0157 
0158     0x00, 0x00, /* section_number(0), last_section_number(0) */
0159 
0160     0xe0, 0x00, /* PCR_PID */
0161 
0162     0xf0, 0x00, /* program_info_length(0) */
0163 
0164     0x02, 0xe0, 0x00, 0xf0, 0x00, /* video stream type(2), pid */
0165     0x04, 0xe0, 0x00, 0xf0, 0x00, /* audio stream type(4), pid */
0166 
0167     0x00, 0x00, 0x00, 0x00 /* CRC32 */
0168 };
0169 
0170 static const u8 PMT_AC3[] = {
0171     0xc2, /* i2c register */
0172     0x01, /* table number for encoder(1) */
0173     0x47, /* sync */
0174 
0175     0x40, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0) */
0176     0x10, /* PMT PID (0x0010) */
0177     0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
0178 
0179     0x00, /* PSI pointer to start of table */
0180 
0181     0x02, /* TID (2) */
0182     0xb0, 0x1a, /* section_syntax_indicator(1), section_length(26) */
0183 
0184     0x00, 0x01, /* program_number(1) */
0185 
0186     0xc1, /* version_number(0), current_next_indicator(1) */
0187 
0188     0x00, 0x00, /* section_number(0), last_section_number(0) */
0189 
0190     0xe1, 0x04, /* PCR_PID (0x0104) */
0191 
0192     0xf0, 0x00, /* program_info_length(0) */
0193 
0194     0x02, 0xe1, 0x00, 0xf0, 0x00, /* video stream type(2), pid */
0195     0x06, 0xe1, 0x03, 0xf0, 0x03, /* audio stream type(6), pid */
0196     0x6a, /* AC3 */
0197     0x01, /* Descriptor_length(1) */
0198     0x00, /* component_type_flag(0), bsid_flag(0), mainid_flag(0), asvc_flag(0), reserved flags(0) */
0199 
0200     0xED, 0xDE, 0x2D, 0xF3 /* CRC32 BE */
0201 };
0202 
0203 static const struct saa6752hs_mpeg_params param_defaults =
0204 {
0205     .ts_pid_pmt      = 16,
0206     .ts_pid_video    = 260,
0207     .ts_pid_audio    = 256,
0208     .ts_pid_pcr      = 259,
0209 
0210     .vi_aspect       = V4L2_MPEG_VIDEO_ASPECT_4x3,
0211     .vi_bitrate      = 4000,
0212     .vi_bitrate_peak = 6000,
0213     .vi_bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
0214 
0215     .au_encoding     = V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
0216     .au_l2_bitrate   = V4L2_MPEG_AUDIO_L2_BITRATE_256K,
0217     .au_ac3_bitrate  = V4L2_MPEG_AUDIO_AC3_BITRATE_256K,
0218 };
0219 
0220 /* ---------------------------------------------------------------------- */
0221 
0222 static int saa6752hs_chip_command(struct i2c_client *client,
0223                   enum saa6752hs_command command)
0224 {
0225     unsigned char buf[3];
0226     unsigned long timeout;
0227     int status = 0;
0228 
0229     /* execute the command */
0230     switch(command) {
0231     case SAA6752HS_COMMAND_RESET:
0232         buf[0] = 0x00;
0233         break;
0234 
0235     case SAA6752HS_COMMAND_STOP:
0236         buf[0] = 0x03;
0237         break;
0238 
0239     case SAA6752HS_COMMAND_START:
0240         buf[0] = 0x02;
0241         break;
0242 
0243     case SAA6752HS_COMMAND_PAUSE:
0244         buf[0] = 0x04;
0245         break;
0246 
0247     case SAA6752HS_COMMAND_RECONFIGURE:
0248         buf[0] = 0x05;
0249         break;
0250 
0251     case SAA6752HS_COMMAND_SLEEP:
0252         buf[0] = 0x06;
0253         break;
0254 
0255     case SAA6752HS_COMMAND_RECONFIGURE_FORCE:
0256         buf[0] = 0x07;
0257         break;
0258 
0259     default:
0260         return -EINVAL;
0261     }
0262 
0263     /* set it and wait for it to be so */
0264     i2c_master_send(client, buf, 1);
0265     timeout = jiffies + HZ * 3;
0266     for (;;) {
0267         /* get the current status */
0268         buf[0] = 0x10;
0269         i2c_master_send(client, buf, 1);
0270         i2c_master_recv(client, buf, 1);
0271 
0272         if (!(buf[0] & 0x20))
0273             break;
0274         if (time_after(jiffies,timeout)) {
0275             status = -ETIMEDOUT;
0276             break;
0277         }
0278 
0279         msleep(10);
0280     }
0281 
0282     /* delay a bit to let encoder settle */
0283     msleep(50);
0284 
0285     return status;
0286 }
0287 
0288 
0289 static inline void set_reg8(struct i2c_client *client, uint8_t reg, uint8_t val)
0290 {
0291     u8 buf[2];
0292 
0293     buf[0] = reg;
0294     buf[1] = val;
0295     i2c_master_send(client, buf, 2);
0296 }
0297 
0298 static inline void set_reg16(struct i2c_client *client, uint8_t reg, uint16_t val)
0299 {
0300     u8 buf[3];
0301 
0302     buf[0] = reg;
0303     buf[1] = val >> 8;
0304     buf[2] = val & 0xff;
0305     i2c_master_send(client, buf, 3);
0306 }
0307 
0308 static int saa6752hs_set_bitrate(struct i2c_client *client,
0309                  struct saa6752hs_state *h)
0310 {
0311     struct saa6752hs_mpeg_params *params = &h->params;
0312     int tot_bitrate;
0313     int is_384k;
0314 
0315     /* set the bitrate mode */
0316     set_reg8(client, 0x71,
0317         params->vi_bitrate_mode != V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
0318 
0319     /* set the video bitrate */
0320     if (params->vi_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) {
0321         /* set the target bitrate */
0322         set_reg16(client, 0x80, params->vi_bitrate);
0323 
0324         /* set the max bitrate */
0325         set_reg16(client, 0x81, params->vi_bitrate_peak);
0326         tot_bitrate = params->vi_bitrate_peak;
0327     } else {
0328         /* set the target bitrate (no max bitrate for CBR) */
0329         set_reg16(client, 0x81, params->vi_bitrate);
0330         tot_bitrate = params->vi_bitrate;
0331     }
0332 
0333     /* set the audio encoding */
0334     set_reg8(client, 0x93,
0335             params->au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3);
0336 
0337     /* set the audio bitrate */
0338     if (params->au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3)
0339         is_384k = V4L2_MPEG_AUDIO_AC3_BITRATE_384K == params->au_ac3_bitrate;
0340     else
0341         is_384k = V4L2_MPEG_AUDIO_L2_BITRATE_384K == params->au_l2_bitrate;
0342     set_reg8(client, 0x94, is_384k);
0343     tot_bitrate += is_384k ? 384 : 256;
0344 
0345     /* Note: the total max bitrate is determined by adding the video and audio
0346        bitrates together and also adding an extra 768kbit/s to stay on the
0347        safe side. If more control should be required, then an extra MPEG control
0348        should be added. */
0349     tot_bitrate += 768;
0350     if (tot_bitrate > MPEG_TOTAL_TARGET_BITRATE_MAX)
0351         tot_bitrate = MPEG_TOTAL_TARGET_BITRATE_MAX;
0352 
0353     /* set the total bitrate */
0354     set_reg16(client, 0xb1, tot_bitrate);
0355     return 0;
0356 }
0357 
0358 static int saa6752hs_try_ctrl(struct v4l2_ctrl *ctrl)
0359 {
0360     struct saa6752hs_state *h =
0361         container_of(ctrl->handler, struct saa6752hs_state, hdl);
0362 
0363     switch (ctrl->id) {
0364     case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
0365         /* peak bitrate shall be >= normal bitrate */
0366         if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
0367             h->video_bitrate_peak->val < h->video_bitrate->val)
0368             h->video_bitrate_peak->val = h->video_bitrate->val;
0369         break;
0370     }
0371     return 0;
0372 }
0373 
0374 static int saa6752hs_s_ctrl(struct v4l2_ctrl *ctrl)
0375 {
0376     struct saa6752hs_state *h =
0377         container_of(ctrl->handler, struct saa6752hs_state, hdl);
0378     struct saa6752hs_mpeg_params *params = &h->params;
0379 
0380     switch (ctrl->id) {
0381     case V4L2_CID_MPEG_STREAM_TYPE:
0382         break;
0383     case V4L2_CID_MPEG_STREAM_PID_PMT:
0384         params->ts_pid_pmt = ctrl->val;
0385         break;
0386     case V4L2_CID_MPEG_STREAM_PID_AUDIO:
0387         params->ts_pid_audio = ctrl->val;
0388         break;
0389     case V4L2_CID_MPEG_STREAM_PID_VIDEO:
0390         params->ts_pid_video = ctrl->val;
0391         break;
0392     case V4L2_CID_MPEG_STREAM_PID_PCR:
0393         params->ts_pid_pcr = ctrl->val;
0394         break;
0395     case V4L2_CID_MPEG_AUDIO_ENCODING:
0396         params->au_encoding = ctrl->val;
0397         break;
0398     case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
0399         params->au_l2_bitrate = ctrl->val;
0400         break;
0401     case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
0402         params->au_ac3_bitrate = ctrl->val;
0403         break;
0404     case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
0405         break;
0406     case V4L2_CID_MPEG_VIDEO_ENCODING:
0407         break;
0408     case V4L2_CID_MPEG_VIDEO_ASPECT:
0409         params->vi_aspect = ctrl->val;
0410         break;
0411     case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
0412         params->vi_bitrate_mode = ctrl->val;
0413         params->vi_bitrate = h->video_bitrate->val / 1000;
0414         params->vi_bitrate_peak = h->video_bitrate_peak->val / 1000;
0415         v4l2_ctrl_activate(h->video_bitrate_peak,
0416                 ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
0417         break;
0418     default:
0419         return -EINVAL;
0420     }
0421     return 0;
0422 }
0423 
0424 static int saa6752hs_init(struct v4l2_subdev *sd, u32 leading_null_bytes)
0425 {
0426     unsigned char buf[9], buf2[4];
0427     struct saa6752hs_state *h = to_state(sd);
0428     struct i2c_client *client = v4l2_get_subdevdata(sd);
0429     unsigned size;
0430     u32 crc;
0431     unsigned char localPAT[256];
0432     unsigned char localPMT[256];
0433 
0434     /* Set video format - must be done first as it resets other settings */
0435     set_reg8(client, 0x41, h->video_format);
0436 
0437     /* Set number of lines in input signal */
0438     set_reg8(client, 0x40, (h->standard & V4L2_STD_525_60) ? 1 : 0);
0439 
0440     /* set bitrate */
0441     saa6752hs_set_bitrate(client, h);
0442 
0443     /* Set GOP structure {3, 13} */
0444     set_reg16(client, 0x72, 0x030d);
0445 
0446     /* Set minimum Q-scale {4} */
0447     set_reg8(client, 0x82, 0x04);
0448 
0449     /* Set maximum Q-scale {12} */
0450     set_reg8(client, 0x83, 0x0c);
0451 
0452     /* Set Output Protocol */
0453     set_reg8(client, 0xd0, 0x81);
0454 
0455     /* Set video output stream format {TS} */
0456     set_reg8(client, 0xb0, 0x05);
0457 
0458     /* Set leading null byte for TS */
0459     set_reg16(client, 0xf6, leading_null_bytes);
0460 
0461     /* compute PAT */
0462     memcpy(localPAT, PAT, sizeof(PAT));
0463     localPAT[17] = 0xe0 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
0464     localPAT[18] = h->params.ts_pid_pmt & 0xff;
0465     crc = crc32_be(~0, &localPAT[7], sizeof(PAT) - 7 - 4);
0466     localPAT[sizeof(PAT) - 4] = (crc >> 24) & 0xFF;
0467     localPAT[sizeof(PAT) - 3] = (crc >> 16) & 0xFF;
0468     localPAT[sizeof(PAT) - 2] = (crc >> 8) & 0xFF;
0469     localPAT[sizeof(PAT) - 1] = crc & 0xFF;
0470 
0471     /* compute PMT */
0472     if (h->params.au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3) {
0473         size = sizeof(PMT_AC3);
0474         memcpy(localPMT, PMT_AC3, size);
0475     } else {
0476         size = sizeof(PMT);
0477         memcpy(localPMT, PMT, size);
0478     }
0479     localPMT[3] = 0x40 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
0480     localPMT[4] = h->params.ts_pid_pmt & 0xff;
0481     localPMT[15] = 0xE0 | ((h->params.ts_pid_pcr >> 8) & 0x0F);
0482     localPMT[16] = h->params.ts_pid_pcr & 0xFF;
0483     localPMT[20] = 0xE0 | ((h->params.ts_pid_video >> 8) & 0x0F);
0484     localPMT[21] = h->params.ts_pid_video & 0xFF;
0485     localPMT[25] = 0xE0 | ((h->params.ts_pid_audio >> 8) & 0x0F);
0486     localPMT[26] = h->params.ts_pid_audio & 0xFF;
0487     crc = crc32_be(~0, &localPMT[7], size - 7 - 4);
0488     localPMT[size - 4] = (crc >> 24) & 0xFF;
0489     localPMT[size - 3] = (crc >> 16) & 0xFF;
0490     localPMT[size - 2] = (crc >> 8) & 0xFF;
0491     localPMT[size - 1] = crc & 0xFF;
0492 
0493     /* Set Audio PID */
0494     set_reg16(client, 0xc1, h->params.ts_pid_audio);
0495 
0496     /* Set Video PID */
0497     set_reg16(client, 0xc0, h->params.ts_pid_video);
0498 
0499     /* Set PCR PID */
0500     set_reg16(client, 0xc4, h->params.ts_pid_pcr);
0501 
0502     /* Send SI tables */
0503     i2c_master_send(client, localPAT, sizeof(PAT));
0504     i2c_master_send(client, localPMT, size);
0505 
0506     /* mute then unmute audio. This removes buzzing artefacts */
0507     set_reg8(client, 0xa4, 1);
0508     set_reg8(client, 0xa4, 0);
0509 
0510     /* start it going */
0511     saa6752hs_chip_command(client, SAA6752HS_COMMAND_START);
0512 
0513     /* readout current state */
0514     buf[0] = 0xE1;
0515     buf[1] = 0xA7;
0516     buf[2] = 0xFE;
0517     buf[3] = 0x82;
0518     buf[4] = 0xB0;
0519     i2c_master_send(client, buf, 5);
0520     i2c_master_recv(client, buf2, 4);
0521 
0522     /* change aspect ratio */
0523     buf[0] = 0xE0;
0524     buf[1] = 0xA7;
0525     buf[2] = 0xFE;
0526     buf[3] = 0x82;
0527     buf[4] = 0xB0;
0528     buf[5] = buf2[0];
0529     switch (h->params.vi_aspect) {
0530     case V4L2_MPEG_VIDEO_ASPECT_16x9:
0531         buf[6] = buf2[1] | 0x40;
0532         break;
0533     case V4L2_MPEG_VIDEO_ASPECT_4x3:
0534     default:
0535         buf[6] = buf2[1] & 0xBF;
0536         break;
0537     }
0538     buf[7] = buf2[2];
0539     buf[8] = buf2[3];
0540     i2c_master_send(client, buf, 9);
0541 
0542     return 0;
0543 }
0544 
0545 static int saa6752hs_get_fmt(struct v4l2_subdev *sd,
0546         struct v4l2_subdev_state *sd_state,
0547         struct v4l2_subdev_format *format)
0548 {
0549     struct v4l2_mbus_framefmt *f = &format->format;
0550     struct saa6752hs_state *h = to_state(sd);
0551 
0552     if (format->pad)
0553         return -EINVAL;
0554 
0555     if (h->video_format == SAA6752HS_VF_UNKNOWN)
0556         h->video_format = SAA6752HS_VF_D1;
0557     f->width = v4l2_format_table[h->video_format].fmt.pix.width;
0558     f->height = v4l2_format_table[h->video_format].fmt.pix.height;
0559     f->code = MEDIA_BUS_FMT_FIXED;
0560     f->field = V4L2_FIELD_INTERLACED;
0561     f->colorspace = V4L2_COLORSPACE_SMPTE170M;
0562     return 0;
0563 }
0564 
0565 static int saa6752hs_set_fmt(struct v4l2_subdev *sd,
0566         struct v4l2_subdev_state *sd_state,
0567         struct v4l2_subdev_format *format)
0568 {
0569     struct v4l2_mbus_framefmt *f = &format->format;
0570     struct saa6752hs_state *h = to_state(sd);
0571     int dist_352, dist_480, dist_720;
0572 
0573     if (format->pad)
0574         return -EINVAL;
0575 
0576     f->code = MEDIA_BUS_FMT_FIXED;
0577 
0578     dist_352 = abs(f->width - 352);
0579     dist_480 = abs(f->width - 480);
0580     dist_720 = abs(f->width - 720);
0581     if (dist_720 < dist_480) {
0582         f->width = 720;
0583         f->height = 576;
0584     } else if (dist_480 < dist_352) {
0585         f->width = 480;
0586         f->height = 576;
0587     } else {
0588         f->width = 352;
0589         if (abs(f->height - 576) < abs(f->height - 288))
0590             f->height = 576;
0591         else
0592             f->height = 288;
0593     }
0594     f->field = V4L2_FIELD_INTERLACED;
0595     f->colorspace = V4L2_COLORSPACE_SMPTE170M;
0596 
0597     if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
0598         sd_state->pads->try_fmt = *f;
0599         return 0;
0600     }
0601 
0602     /*
0603       FIXME: translate and round width/height into EMPRESS
0604       subsample type:
0605 
0606       type   |   PAL   |  NTSC
0607       ---------------------------
0608       SIF    | 352x288 | 352x240
0609       1/2 D1 | 352x576 | 352x480
0610       2/3 D1 | 480x576 | 480x480
0611       D1     | 720x576 | 720x480
0612     */
0613 
0614     if (f->code != MEDIA_BUS_FMT_FIXED)
0615         return -EINVAL;
0616 
0617     if (f->width == 720)
0618         h->video_format = SAA6752HS_VF_D1;
0619     else if (f->width == 480)
0620         h->video_format = SAA6752HS_VF_2_3_D1;
0621     else if (f->height == 576)
0622         h->video_format = SAA6752HS_VF_1_2_D1;
0623     else
0624         h->video_format = SAA6752HS_VF_SIF;
0625     return 0;
0626 }
0627 
0628 static int saa6752hs_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
0629 {
0630     struct saa6752hs_state *h = to_state(sd);
0631 
0632     h->standard = std;
0633     return 0;
0634 }
0635 
0636 /* ----------------------------------------------------------------------- */
0637 
0638 static const struct v4l2_ctrl_ops saa6752hs_ctrl_ops = {
0639     .try_ctrl = saa6752hs_try_ctrl,
0640     .s_ctrl = saa6752hs_s_ctrl,
0641 };
0642 
0643 static const struct v4l2_subdev_core_ops saa6752hs_core_ops = {
0644     .init = saa6752hs_init,
0645 };
0646 
0647 static const struct v4l2_subdev_video_ops saa6752hs_video_ops = {
0648     .s_std = saa6752hs_s_std,
0649 };
0650 
0651 static const struct v4l2_subdev_pad_ops saa6752hs_pad_ops = {
0652     .get_fmt = saa6752hs_get_fmt,
0653     .set_fmt = saa6752hs_set_fmt,
0654 };
0655 
0656 static const struct v4l2_subdev_ops saa6752hs_ops = {
0657     .core = &saa6752hs_core_ops,
0658     .video = &saa6752hs_video_ops,
0659     .pad = &saa6752hs_pad_ops,
0660 };
0661 
0662 static int saa6752hs_probe(struct i2c_client *client,
0663         const struct i2c_device_id *id)
0664 {
0665     struct saa6752hs_state *h;
0666     struct v4l2_subdev *sd;
0667     struct v4l2_ctrl_handler *hdl;
0668     u8 addr = 0x13;
0669     u8 data[12];
0670 
0671     v4l_info(client, "chip found @ 0x%x (%s)\n",
0672             client->addr << 1, client->adapter->name);
0673 
0674     h = devm_kzalloc(&client->dev, sizeof(*h), GFP_KERNEL);
0675     if (h == NULL)
0676         return -ENOMEM;
0677     sd = &h->sd;
0678     v4l2_i2c_subdev_init(sd, client, &saa6752hs_ops);
0679 
0680     i2c_master_send(client, &addr, 1);
0681     i2c_master_recv(client, data, sizeof(data));
0682     h->revision = (data[8] << 8) | data[9];
0683     h->has_ac3 = 0;
0684     if (h->revision == 0x0206) {
0685         h->has_ac3 = 1;
0686         v4l_info(client, "supports AC-3\n");
0687     }
0688     h->params = param_defaults;
0689 
0690     hdl = &h->hdl;
0691     v4l2_ctrl_handler_init(hdl, 14);
0692     v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
0693         V4L2_CID_MPEG_AUDIO_ENCODING,
0694         h->has_ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3 :
0695             V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
0696         0x0d, V4L2_MPEG_AUDIO_ENCODING_LAYER_2);
0697 
0698     v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
0699         V4L2_CID_MPEG_AUDIO_L2_BITRATE,
0700         V4L2_MPEG_AUDIO_L2_BITRATE_384K,
0701         ~((1 << V4L2_MPEG_AUDIO_L2_BITRATE_256K) |
0702           (1 << V4L2_MPEG_AUDIO_L2_BITRATE_384K)),
0703         V4L2_MPEG_AUDIO_L2_BITRATE_256K);
0704 
0705     if (h->has_ac3)
0706         v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
0707             V4L2_CID_MPEG_AUDIO_AC3_BITRATE,
0708             V4L2_MPEG_AUDIO_AC3_BITRATE_384K,
0709             ~((1 << V4L2_MPEG_AUDIO_AC3_BITRATE_256K) |
0710               (1 << V4L2_MPEG_AUDIO_AC3_BITRATE_384K)),
0711             V4L2_MPEG_AUDIO_AC3_BITRATE_256K);
0712 
0713     v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
0714         V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
0715         V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000,
0716         ~(1 << V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000),
0717         V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000);
0718 
0719     v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
0720         V4L2_CID_MPEG_VIDEO_ENCODING,
0721         V4L2_MPEG_VIDEO_ENCODING_MPEG_2,
0722         ~(1 << V4L2_MPEG_VIDEO_ENCODING_MPEG_2),
0723         V4L2_MPEG_VIDEO_ENCODING_MPEG_2);
0724 
0725     v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
0726         V4L2_CID_MPEG_VIDEO_ASPECT,
0727         V4L2_MPEG_VIDEO_ASPECT_16x9, 0x01,
0728         V4L2_MPEG_VIDEO_ASPECT_4x3);
0729 
0730     h->video_bitrate_peak = v4l2_ctrl_new_std(hdl, &saa6752hs_ctrl_ops,
0731         V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
0732         1000000, 27000000, 1000, 8000000);
0733 
0734     v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
0735         V4L2_CID_MPEG_STREAM_TYPE,
0736         V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
0737         ~(1 << V4L2_MPEG_STREAM_TYPE_MPEG2_TS),
0738         V4L2_MPEG_STREAM_TYPE_MPEG2_TS);
0739 
0740     h->video_bitrate_mode = v4l2_ctrl_new_std_menu(hdl, &saa6752hs_ctrl_ops,
0741         V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
0742         V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 0,
0743         V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
0744     h->video_bitrate = v4l2_ctrl_new_std(hdl, &saa6752hs_ctrl_ops,
0745         V4L2_CID_MPEG_VIDEO_BITRATE, 1000000, 27000000, 1000, 6000000);
0746     v4l2_ctrl_new_std(hdl, &saa6752hs_ctrl_ops,
0747         V4L2_CID_MPEG_STREAM_PID_PMT, 0, (1 << 14) - 1, 1, 16);
0748     v4l2_ctrl_new_std(hdl, &saa6752hs_ctrl_ops,
0749         V4L2_CID_MPEG_STREAM_PID_AUDIO, 0, (1 << 14) - 1, 1, 260);
0750     v4l2_ctrl_new_std(hdl, &saa6752hs_ctrl_ops,
0751         V4L2_CID_MPEG_STREAM_PID_VIDEO, 0, (1 << 14) - 1, 1, 256);
0752     v4l2_ctrl_new_std(hdl, &saa6752hs_ctrl_ops,
0753         V4L2_CID_MPEG_STREAM_PID_PCR, 0, (1 << 14) - 1, 1, 259);
0754     sd->ctrl_handler = hdl;
0755     if (hdl->error) {
0756         int err = hdl->error;
0757 
0758         v4l2_ctrl_handler_free(hdl);
0759         return err;
0760     }
0761     v4l2_ctrl_cluster(3, &h->video_bitrate_mode);
0762     v4l2_ctrl_handler_setup(hdl);
0763     h->standard = 0; /* Assume 625 input lines */
0764     return 0;
0765 }
0766 
0767 static int saa6752hs_remove(struct i2c_client *client)
0768 {
0769     struct v4l2_subdev *sd = i2c_get_clientdata(client);
0770 
0771     v4l2_device_unregister_subdev(sd);
0772     v4l2_ctrl_handler_free(&to_state(sd)->hdl);
0773     return 0;
0774 }
0775 
0776 static const struct i2c_device_id saa6752hs_id[] = {
0777     { "saa6752hs", 0 },
0778     { }
0779 };
0780 MODULE_DEVICE_TABLE(i2c, saa6752hs_id);
0781 
0782 static struct i2c_driver saa6752hs_driver = {
0783     .driver = {
0784         .name   = "saa6752hs",
0785     },
0786     .probe      = saa6752hs_probe,
0787     .remove     = saa6752hs_remove,
0788     .id_table   = saa6752hs_id,
0789 };
0790 
0791 module_i2c_driver(saa6752hs_driver);