Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * adv7393 - ADV7393 Video Encoder Driver
0003  *
0004  * The encoder hardware does not support SECAM.
0005  *
0006  * Copyright (C) 2010-2012 ADVANSEE - http://www.advansee.com/
0007  * Benoît Thébaudeau <benoit.thebaudeau@advansee.com>
0008  *
0009  * Based on ADV7343 driver,
0010  *
0011  * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
0012  *
0013  * This program is free software; you can redistribute it and/or
0014  * modify it under the terms of the GNU General Public License as
0015  * published by the Free Software Foundation version 2.
0016  *
0017  * This program is distributed .as is. WITHOUT ANY WARRANTY of any
0018  * kind, whether express or implied; without even the implied warranty
0019  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0020  * GNU General Public License for more details.
0021  */
0022 
0023 #include <linux/kernel.h>
0024 #include <linux/init.h>
0025 #include <linux/ctype.h>
0026 #include <linux/slab.h>
0027 #include <linux/i2c.h>
0028 #include <linux/device.h>
0029 #include <linux/delay.h>
0030 #include <linux/module.h>
0031 #include <linux/videodev2.h>
0032 #include <linux/uaccess.h>
0033 
0034 #include <media/i2c/adv7393.h>
0035 #include <media/v4l2-device.h>
0036 #include <media/v4l2-ctrls.h>
0037 
0038 #include "adv7393_regs.h"
0039 
0040 MODULE_DESCRIPTION("ADV7393 video encoder driver");
0041 MODULE_LICENSE("GPL");
0042 
0043 static bool debug;
0044 module_param(debug, bool, 0644);
0045 MODULE_PARM_DESC(debug, "Debug level 0-1");
0046 
0047 struct adv7393_state {
0048     struct v4l2_subdev sd;
0049     struct v4l2_ctrl_handler hdl;
0050     u8 reg00;
0051     u8 reg01;
0052     u8 reg02;
0053     u8 reg35;
0054     u8 reg80;
0055     u8 reg82;
0056     u32 output;
0057     v4l2_std_id std;
0058 };
0059 
0060 static inline struct adv7393_state *to_state(struct v4l2_subdev *sd)
0061 {
0062     return container_of(sd, struct adv7393_state, sd);
0063 }
0064 
0065 static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
0066 {
0067     return &container_of(ctrl->handler, struct adv7393_state, hdl)->sd;
0068 }
0069 
0070 static inline int adv7393_write(struct v4l2_subdev *sd, u8 reg, u8 value)
0071 {
0072     struct i2c_client *client = v4l2_get_subdevdata(sd);
0073 
0074     return i2c_smbus_write_byte_data(client, reg, value);
0075 }
0076 
0077 static const u8 adv7393_init_reg_val[] = {
0078     ADV7393_SOFT_RESET, ADV7393_SOFT_RESET_DEFAULT,
0079     ADV7393_POWER_MODE_REG, ADV7393_POWER_MODE_REG_DEFAULT,
0080 
0081     ADV7393_HD_MODE_REG1, ADV7393_HD_MODE_REG1_DEFAULT,
0082     ADV7393_HD_MODE_REG2, ADV7393_HD_MODE_REG2_DEFAULT,
0083     ADV7393_HD_MODE_REG3, ADV7393_HD_MODE_REG3_DEFAULT,
0084     ADV7393_HD_MODE_REG4, ADV7393_HD_MODE_REG4_DEFAULT,
0085     ADV7393_HD_MODE_REG5, ADV7393_HD_MODE_REG5_DEFAULT,
0086     ADV7393_HD_MODE_REG6, ADV7393_HD_MODE_REG6_DEFAULT,
0087     ADV7393_HD_MODE_REG7, ADV7393_HD_MODE_REG7_DEFAULT,
0088 
0089     ADV7393_SD_MODE_REG1, ADV7393_SD_MODE_REG1_DEFAULT,
0090     ADV7393_SD_MODE_REG2, ADV7393_SD_MODE_REG2_DEFAULT,
0091     ADV7393_SD_MODE_REG3, ADV7393_SD_MODE_REG3_DEFAULT,
0092     ADV7393_SD_MODE_REG4, ADV7393_SD_MODE_REG4_DEFAULT,
0093     ADV7393_SD_MODE_REG5, ADV7393_SD_MODE_REG5_DEFAULT,
0094     ADV7393_SD_MODE_REG6, ADV7393_SD_MODE_REG6_DEFAULT,
0095     ADV7393_SD_MODE_REG7, ADV7393_SD_MODE_REG7_DEFAULT,
0096     ADV7393_SD_MODE_REG8, ADV7393_SD_MODE_REG8_DEFAULT,
0097 
0098     ADV7393_SD_TIMING_REG0, ADV7393_SD_TIMING_REG0_DEFAULT,
0099 
0100     ADV7393_SD_HUE_ADJUST, ADV7393_SD_HUE_ADJUST_DEFAULT,
0101     ADV7393_SD_CGMS_WSS0, ADV7393_SD_CGMS_WSS0_DEFAULT,
0102     ADV7393_SD_BRIGHTNESS_WSS, ADV7393_SD_BRIGHTNESS_WSS_DEFAULT,
0103 };
0104 
0105 /*
0106  *              2^32
0107  * FSC(reg) =  FSC (HZ) * --------
0108  *            27000000
0109  */
0110 static const struct adv7393_std_info stdinfo[] = {
0111     {
0112         /* FSC(Hz) = 4,433,618.75 Hz */
0113         SD_STD_NTSC, 705268427, V4L2_STD_NTSC_443,
0114     }, {
0115         /* FSC(Hz) = 3,579,545.45 Hz */
0116         SD_STD_NTSC, 569408542, V4L2_STD_NTSC,
0117     }, {
0118         /* FSC(Hz) = 3,575,611.00 Hz */
0119         SD_STD_PAL_M, 568782678, V4L2_STD_PAL_M,
0120     }, {
0121         /* FSC(Hz) = 3,582,056.00 Hz */
0122         SD_STD_PAL_N, 569807903, V4L2_STD_PAL_Nc,
0123     }, {
0124         /* FSC(Hz) = 4,433,618.75 Hz */
0125         SD_STD_PAL_N, 705268427, V4L2_STD_PAL_N,
0126     }, {
0127         /* FSC(Hz) = 4,433,618.75 Hz */
0128         SD_STD_PAL_M, 705268427, V4L2_STD_PAL_60,
0129     }, {
0130         /* FSC(Hz) = 4,433,618.75 Hz */
0131         SD_STD_PAL_BDGHI, 705268427, V4L2_STD_PAL,
0132     },
0133 };
0134 
0135 static int adv7393_setstd(struct v4l2_subdev *sd, v4l2_std_id std)
0136 {
0137     struct adv7393_state *state = to_state(sd);
0138     const struct adv7393_std_info *std_info;
0139     int num_std;
0140     u8 reg;
0141     u32 val;
0142     int err = 0;
0143     int i;
0144 
0145     num_std = ARRAY_SIZE(stdinfo);
0146 
0147     for (i = 0; i < num_std; i++) {
0148         if (stdinfo[i].stdid & std)
0149             break;
0150     }
0151 
0152     if (i == num_std) {
0153         v4l2_dbg(1, debug, sd,
0154                 "Invalid std or std is not supported: %llx\n",
0155                         (unsigned long long)std);
0156         return -EINVAL;
0157     }
0158 
0159     std_info = &stdinfo[i];
0160 
0161     /* Set the standard */
0162     val = state->reg80 & ~SD_STD_MASK;
0163     val |= std_info->standard_val3;
0164     err = adv7393_write(sd, ADV7393_SD_MODE_REG1, val);
0165     if (err < 0)
0166         goto setstd_exit;
0167 
0168     state->reg80 = val;
0169 
0170     /* Configure the input mode register */
0171     val = state->reg01 & ~INPUT_MODE_MASK;
0172     val |= SD_INPUT_MODE;
0173     err = adv7393_write(sd, ADV7393_MODE_SELECT_REG, val);
0174     if (err < 0)
0175         goto setstd_exit;
0176 
0177     state->reg01 = val;
0178 
0179     /* Program the sub carrier frequency registers */
0180     val = std_info->fsc_val;
0181     for (reg = ADV7393_FSC_REG0; reg <= ADV7393_FSC_REG3; reg++) {
0182         err = adv7393_write(sd, reg, val);
0183         if (err < 0)
0184             goto setstd_exit;
0185         val >>= 8;
0186     }
0187 
0188     val = state->reg82;
0189 
0190     /* Pedestal settings */
0191     if (std & (V4L2_STD_NTSC | V4L2_STD_NTSC_443))
0192         val |= SD_PEDESTAL_EN;
0193     else
0194         val &= SD_PEDESTAL_DI;
0195 
0196     err = adv7393_write(sd, ADV7393_SD_MODE_REG2, val);
0197     if (err < 0)
0198         goto setstd_exit;
0199 
0200     state->reg82 = val;
0201 
0202 setstd_exit:
0203     if (err != 0)
0204         v4l2_err(sd, "Error setting std, write failed\n");
0205 
0206     return err;
0207 }
0208 
0209 static int adv7393_setoutput(struct v4l2_subdev *sd, u32 output_type)
0210 {
0211     struct adv7393_state *state = to_state(sd);
0212     u8 val;
0213     int err = 0;
0214 
0215     if (output_type > ADV7393_SVIDEO_ID) {
0216         v4l2_dbg(1, debug, sd,
0217             "Invalid output type or output type not supported:%d\n",
0218                                 output_type);
0219         return -EINVAL;
0220     }
0221 
0222     /* Enable Appropriate DAC */
0223     val = state->reg00 & 0x03;
0224 
0225     if (output_type == ADV7393_COMPOSITE_ID)
0226         val |= ADV7393_COMPOSITE_POWER_VALUE;
0227     else if (output_type == ADV7393_COMPONENT_ID)
0228         val |= ADV7393_COMPONENT_POWER_VALUE;
0229     else
0230         val |= ADV7393_SVIDEO_POWER_VALUE;
0231 
0232     err = adv7393_write(sd, ADV7393_POWER_MODE_REG, val);
0233     if (err < 0)
0234         goto setoutput_exit;
0235 
0236     state->reg00 = val;
0237 
0238     /* Enable YUV output */
0239     val = state->reg02 | YUV_OUTPUT_SELECT;
0240     err = adv7393_write(sd, ADV7393_MODE_REG0, val);
0241     if (err < 0)
0242         goto setoutput_exit;
0243 
0244     state->reg02 = val;
0245 
0246     /* configure SD DAC Output 1 bit */
0247     val = state->reg82;
0248     if (output_type == ADV7393_COMPONENT_ID)
0249         val &= SD_DAC_OUT1_DI;
0250     else
0251         val |= SD_DAC_OUT1_EN;
0252     err = adv7393_write(sd, ADV7393_SD_MODE_REG2, val);
0253     if (err < 0)
0254         goto setoutput_exit;
0255 
0256     state->reg82 = val;
0257 
0258     /* configure ED/HD Color DAC Swap bit to zero */
0259     val = state->reg35 & HD_DAC_SWAP_DI;
0260     err = adv7393_write(sd, ADV7393_HD_MODE_REG6, val);
0261     if (err < 0)
0262         goto setoutput_exit;
0263 
0264     state->reg35 = val;
0265 
0266 setoutput_exit:
0267     if (err != 0)
0268         v4l2_err(sd, "Error setting output, write failed\n");
0269 
0270     return err;
0271 }
0272 
0273 static int adv7393_log_status(struct v4l2_subdev *sd)
0274 {
0275     struct adv7393_state *state = to_state(sd);
0276 
0277     v4l2_info(sd, "Standard: %llx\n", (unsigned long long)state->std);
0278     v4l2_info(sd, "Output: %s\n", (state->output == 0) ? "Composite" :
0279             ((state->output == 1) ? "Component" : "S-Video"));
0280     return 0;
0281 }
0282 
0283 static int adv7393_s_ctrl(struct v4l2_ctrl *ctrl)
0284 {
0285     struct v4l2_subdev *sd = to_sd(ctrl);
0286 
0287     switch (ctrl->id) {
0288     case V4L2_CID_BRIGHTNESS:
0289         return adv7393_write(sd, ADV7393_SD_BRIGHTNESS_WSS,
0290                     ctrl->val & SD_BRIGHTNESS_VALUE_MASK);
0291 
0292     case V4L2_CID_HUE:
0293         return adv7393_write(sd, ADV7393_SD_HUE_ADJUST,
0294                     ctrl->val - ADV7393_HUE_MIN);
0295 
0296     case V4L2_CID_GAIN:
0297         return adv7393_write(sd, ADV7393_DAC123_OUTPUT_LEVEL,
0298                     ctrl->val);
0299     }
0300     return -EINVAL;
0301 }
0302 
0303 static const struct v4l2_ctrl_ops adv7393_ctrl_ops = {
0304     .s_ctrl = adv7393_s_ctrl,
0305 };
0306 
0307 static const struct v4l2_subdev_core_ops adv7393_core_ops = {
0308     .log_status = adv7393_log_status,
0309 };
0310 
0311 static int adv7393_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std)
0312 {
0313     struct adv7393_state *state = to_state(sd);
0314     int err = 0;
0315 
0316     if (state->std == std)
0317         return 0;
0318 
0319     err = adv7393_setstd(sd, std);
0320     if (!err)
0321         state->std = std;
0322 
0323     return err;
0324 }
0325 
0326 static int adv7393_s_routing(struct v4l2_subdev *sd,
0327         u32 input, u32 output, u32 config)
0328 {
0329     struct adv7393_state *state = to_state(sd);
0330     int err = 0;
0331 
0332     if (state->output == output)
0333         return 0;
0334 
0335     err = adv7393_setoutput(sd, output);
0336     if (!err)
0337         state->output = output;
0338 
0339     return err;
0340 }
0341 
0342 static const struct v4l2_subdev_video_ops adv7393_video_ops = {
0343     .s_std_output   = adv7393_s_std_output,
0344     .s_routing  = adv7393_s_routing,
0345 };
0346 
0347 static const struct v4l2_subdev_ops adv7393_ops = {
0348     .core   = &adv7393_core_ops,
0349     .video  = &adv7393_video_ops,
0350 };
0351 
0352 static int adv7393_initialize(struct v4l2_subdev *sd)
0353 {
0354     struct adv7393_state *state = to_state(sd);
0355     int err = 0;
0356     int i;
0357 
0358     for (i = 0; i < ARRAY_SIZE(adv7393_init_reg_val); i += 2) {
0359 
0360         err = adv7393_write(sd, adv7393_init_reg_val[i],
0361                     adv7393_init_reg_val[i+1]);
0362         if (err) {
0363             v4l2_err(sd, "Error initializing\n");
0364             return err;
0365         }
0366     }
0367 
0368     /* Configure for default video standard */
0369     err = adv7393_setoutput(sd, state->output);
0370     if (err < 0) {
0371         v4l2_err(sd, "Error setting output during init\n");
0372         return -EINVAL;
0373     }
0374 
0375     err = adv7393_setstd(sd, state->std);
0376     if (err < 0) {
0377         v4l2_err(sd, "Error setting std during init\n");
0378         return -EINVAL;
0379     }
0380 
0381     return err;
0382 }
0383 
0384 static int adv7393_probe(struct i2c_client *client,
0385                 const struct i2c_device_id *id)
0386 {
0387     struct adv7393_state *state;
0388     int err;
0389 
0390     if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
0391         return -ENODEV;
0392 
0393     v4l_info(client, "chip found @ 0x%x (%s)\n",
0394             client->addr << 1, client->adapter->name);
0395 
0396     state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
0397     if (state == NULL)
0398         return -ENOMEM;
0399 
0400     state->reg00    = ADV7393_POWER_MODE_REG_DEFAULT;
0401     state->reg01    = 0x00;
0402     state->reg02    = 0x20;
0403     state->reg35    = ADV7393_HD_MODE_REG6_DEFAULT;
0404     state->reg80    = ADV7393_SD_MODE_REG1_DEFAULT;
0405     state->reg82    = ADV7393_SD_MODE_REG2_DEFAULT;
0406 
0407     state->output = ADV7393_COMPOSITE_ID;
0408     state->std = V4L2_STD_NTSC;
0409 
0410     v4l2_i2c_subdev_init(&state->sd, client, &adv7393_ops);
0411 
0412     v4l2_ctrl_handler_init(&state->hdl, 3);
0413     v4l2_ctrl_new_std(&state->hdl, &adv7393_ctrl_ops,
0414             V4L2_CID_BRIGHTNESS, ADV7393_BRIGHTNESS_MIN,
0415                          ADV7393_BRIGHTNESS_MAX, 1,
0416                          ADV7393_BRIGHTNESS_DEF);
0417     v4l2_ctrl_new_std(&state->hdl, &adv7393_ctrl_ops,
0418             V4L2_CID_HUE, ADV7393_HUE_MIN,
0419                       ADV7393_HUE_MAX, 1,
0420                       ADV7393_HUE_DEF);
0421     v4l2_ctrl_new_std(&state->hdl, &adv7393_ctrl_ops,
0422             V4L2_CID_GAIN, ADV7393_GAIN_MIN,
0423                        ADV7393_GAIN_MAX, 1,
0424                        ADV7393_GAIN_DEF);
0425     state->sd.ctrl_handler = &state->hdl;
0426     if (state->hdl.error) {
0427         int err = state->hdl.error;
0428 
0429         v4l2_ctrl_handler_free(&state->hdl);
0430         return err;
0431     }
0432     v4l2_ctrl_handler_setup(&state->hdl);
0433 
0434     err = adv7393_initialize(&state->sd);
0435     if (err)
0436         v4l2_ctrl_handler_free(&state->hdl);
0437     return err;
0438 }
0439 
0440 static int adv7393_remove(struct i2c_client *client)
0441 {
0442     struct v4l2_subdev *sd = i2c_get_clientdata(client);
0443     struct adv7393_state *state = to_state(sd);
0444 
0445     v4l2_device_unregister_subdev(sd);
0446     v4l2_ctrl_handler_free(&state->hdl);
0447 
0448     return 0;
0449 }
0450 
0451 static const struct i2c_device_id adv7393_id[] = {
0452     {"adv7393", 0},
0453     {},
0454 };
0455 MODULE_DEVICE_TABLE(i2c, adv7393_id);
0456 
0457 static struct i2c_driver adv7393_driver = {
0458     .driver = {
0459         .name   = "adv7393",
0460     },
0461     .probe      = adv7393_probe,
0462     .remove     = adv7393_remove,
0463     .id_table   = adv7393_id,
0464 };
0465 module_i2c_driver(adv7393_driver);