Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * saa7185 - Philips SAA7185B video encoder driver version 0.0.3
0004  *
0005  * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
0006  *
0007  * Slight changes for video timing and attachment output by
0008  * Wolfgang Scherr <scherr@net4you.net>
0009  *
0010  * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
0011  *    - moved over to linux>=2.4.x i2c protocol (1/1/2003)
0012  */
0013 
0014 #include <linux/module.h>
0015 #include <linux/types.h>
0016 #include <linux/slab.h>
0017 #include <linux/ioctl.h>
0018 #include <linux/uaccess.h>
0019 #include <linux/i2c.h>
0020 #include <linux/videodev2.h>
0021 #include <media/v4l2-device.h>
0022 
0023 MODULE_DESCRIPTION("Philips SAA7185 video encoder driver");
0024 MODULE_AUTHOR("Dave Perks");
0025 MODULE_LICENSE("GPL");
0026 
0027 static int debug;
0028 module_param(debug, int, 0);
0029 MODULE_PARM_DESC(debug, "Debug level (0-1)");
0030 
0031 
0032 /* ----------------------------------------------------------------------- */
0033 
0034 struct saa7185 {
0035     struct v4l2_subdev sd;
0036     unsigned char reg[128];
0037 
0038     v4l2_std_id norm;
0039 };
0040 
0041 static inline struct saa7185 *to_saa7185(struct v4l2_subdev *sd)
0042 {
0043     return container_of(sd, struct saa7185, sd);
0044 }
0045 
0046 /* ----------------------------------------------------------------------- */
0047 
0048 static inline int saa7185_read(struct v4l2_subdev *sd)
0049 {
0050     struct i2c_client *client = v4l2_get_subdevdata(sd);
0051 
0052     return i2c_smbus_read_byte(client);
0053 }
0054 
0055 static int saa7185_write(struct v4l2_subdev *sd, u8 reg, u8 value)
0056 {
0057     struct i2c_client *client = v4l2_get_subdevdata(sd);
0058     struct saa7185 *encoder = to_saa7185(sd);
0059 
0060     v4l2_dbg(1, debug, sd, "%02x set to %02x\n", reg, value);
0061     encoder->reg[reg] = value;
0062     return i2c_smbus_write_byte_data(client, reg, value);
0063 }
0064 
0065 static int saa7185_write_block(struct v4l2_subdev *sd,
0066         const u8 *data, unsigned int len)
0067 {
0068     struct i2c_client *client = v4l2_get_subdevdata(sd);
0069     struct saa7185 *encoder = to_saa7185(sd);
0070     int ret = -1;
0071     u8 reg;
0072 
0073     /* the adv7175 has an autoincrement function, use it if
0074      * the adapter understands raw I2C */
0075     if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
0076         /* do raw I2C, not smbus compatible */
0077         u8 block_data[32];
0078         int block_len;
0079 
0080         while (len >= 2) {
0081             block_len = 0;
0082             block_data[block_len++] = reg = data[0];
0083             do {
0084                 block_data[block_len++] =
0085                     encoder->reg[reg++] = data[1];
0086                 len -= 2;
0087                 data += 2;
0088             } while (len >= 2 && data[0] == reg && block_len < 32);
0089             ret = i2c_master_send(client, block_data, block_len);
0090             if (ret < 0)
0091                 break;
0092         }
0093     } else {
0094         /* do some slow I2C emulation kind of thing */
0095         while (len >= 2) {
0096             reg = *data++;
0097             ret = saa7185_write(sd, reg, *data++);
0098             if (ret < 0)
0099                 break;
0100             len -= 2;
0101         }
0102     }
0103 
0104     return ret;
0105 }
0106 
0107 /* ----------------------------------------------------------------------- */
0108 
0109 static const unsigned char init_common[] = {
0110     0x3a, 0x0f,     /* CBENB=0, V656=0, VY2C=1,
0111                  * YUV2C=1, MY2C=1, MUV2C=1 */
0112 
0113     0x42, 0x6b,     /* OVLY0=107 */
0114     0x43, 0x00,     /* OVLU0=0     white */
0115     0x44, 0x00,     /* OVLV0=0   */
0116     0x45, 0x22,     /* OVLY1=34  */
0117     0x46, 0xac,     /* OVLU1=172   yellow */
0118     0x47, 0x0e,     /* OVLV1=14  */
0119     0x48, 0x03,     /* OVLY2=3   */
0120     0x49, 0x1d,     /* OVLU2=29    cyan */
0121     0x4a, 0xac,     /* OVLV2=172 */
0122     0x4b, 0xf0,     /* OVLY3=240 */
0123     0x4c, 0xc8,     /* OVLU3=200   green */
0124     0x4d, 0xb9,     /* OVLV3=185 */
0125     0x4e, 0xd4,     /* OVLY4=212 */
0126     0x4f, 0x38,     /* OVLU4=56    magenta */
0127     0x50, 0x47,     /* OVLV4=71  */
0128     0x51, 0xc1,     /* OVLY5=193 */
0129     0x52, 0xe3,     /* OVLU5=227   red */
0130     0x53, 0x54,     /* OVLV5=84  */
0131     0x54, 0xa3,     /* OVLY6=163 */
0132     0x55, 0x54,     /* OVLU6=84    blue */
0133     0x56, 0xf2,     /* OVLV6=242 */
0134     0x57, 0x90,     /* OVLY7=144 */
0135     0x58, 0x00,     /* OVLU7=0     black */
0136     0x59, 0x00,     /* OVLV7=0   */
0137 
0138     0x5a, 0x00,     /* CHPS=0    */
0139     0x5b, 0x76,     /* GAINU=118 */
0140     0x5c, 0xa5,     /* GAINV=165 */
0141     0x5d, 0x3c,     /* BLCKL=60  */
0142     0x5e, 0x3a,     /* BLNNL=58  */
0143     0x5f, 0x3a,     /* CCRS=0, BLNVB=58 */
0144     0x60, 0x00,     /* NULL      */
0145 
0146     /* 0x61 - 0x66 set according to norm */
0147 
0148     0x67, 0x00,     /* 0 : caption 1st byte odd  field */
0149     0x68, 0x00,     /* 0 : caption 2nd byte odd  field */
0150     0x69, 0x00,     /* 0 : caption 1st byte even field */
0151     0x6a, 0x00,     /* 0 : caption 2nd byte even field */
0152 
0153     0x6b, 0x91,     /* MODIN=2, PCREF=0, SCCLN=17 */
0154     0x6c, 0x20,     /* SRCV1=0, TRCV2=1, ORCV1=0, PRCV1=0,
0155                  * CBLF=0, ORCV2=0, PRCV2=0 */
0156     0x6d, 0x00,     /* SRCM1=0, CCEN=0 */
0157 
0158     0x6e, 0x0e,     /* HTRIG=0x005, approx. centered, at
0159                  * least for PAL */
0160     0x6f, 0x00,     /* HTRIG upper bits */
0161     0x70, 0x20,     /* PHRES=0, SBLN=1, VTRIG=0 */
0162 
0163     /* The following should not be needed */
0164 
0165     0x71, 0x15,     /* BMRQ=0x115 */
0166     0x72, 0x90,     /* EMRQ=0x690 */
0167     0x73, 0x61,     /* EMRQ=0x690, BMRQ=0x115 */
0168     0x74, 0x00,     /* NULL       */
0169     0x75, 0x00,     /* NULL       */
0170     0x76, 0x00,     /* NULL       */
0171     0x77, 0x15,     /* BRCV=0x115 */
0172     0x78, 0x90,     /* ERCV=0x690 */
0173     0x79, 0x61,     /* ERCV=0x690, BRCV=0x115 */
0174 
0175     /* Field length controls */
0176 
0177     0x7a, 0x70,     /* FLC=0 */
0178 
0179     /* The following should not be needed if SBLN = 1 */
0180 
0181     0x7b, 0x16,     /* FAL=22 */
0182     0x7c, 0x35,     /* LAL=244 */
0183     0x7d, 0x20,     /* LAL=244, FAL=22 */
0184 };
0185 
0186 static const unsigned char init_pal[] = {
0187     0x61, 0x1e,     /* FISE=0, PAL=1, SCBW=1, RTCE=1,
0188                  * YGS=1, INPI=0, DOWN=0 */
0189     0x62, 0xc8,     /* DECTYP=1, BSTA=72 */
0190     0x63, 0xcb,     /* FSC0 */
0191     0x64, 0x8a,     /* FSC1 */
0192     0x65, 0x09,     /* FSC2 */
0193     0x66, 0x2a,     /* FSC3 */
0194 };
0195 
0196 static const unsigned char init_ntsc[] = {
0197     0x61, 0x1d,     /* FISE=1, PAL=0, SCBW=1, RTCE=1,
0198                  * YGS=1, INPI=0, DOWN=0 */
0199     0x62, 0xe6,     /* DECTYP=1, BSTA=102 */
0200     0x63, 0x1f,     /* FSC0 */
0201     0x64, 0x7c,     /* FSC1 */
0202     0x65, 0xf0,     /* FSC2 */
0203     0x66, 0x21,     /* FSC3 */
0204 };
0205 
0206 
0207 static int saa7185_init(struct v4l2_subdev *sd, u32 val)
0208 {
0209     struct saa7185 *encoder = to_saa7185(sd);
0210 
0211     saa7185_write_block(sd, init_common, sizeof(init_common));
0212     if (encoder->norm & V4L2_STD_NTSC)
0213         saa7185_write_block(sd, init_ntsc, sizeof(init_ntsc));
0214     else
0215         saa7185_write_block(sd, init_pal, sizeof(init_pal));
0216     return 0;
0217 }
0218 
0219 static int saa7185_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std)
0220 {
0221     struct saa7185 *encoder = to_saa7185(sd);
0222 
0223     if (std & V4L2_STD_NTSC)
0224         saa7185_write_block(sd, init_ntsc, sizeof(init_ntsc));
0225     else if (std & V4L2_STD_PAL)
0226         saa7185_write_block(sd, init_pal, sizeof(init_pal));
0227     else
0228         return -EINVAL;
0229     encoder->norm = std;
0230     return 0;
0231 }
0232 
0233 static int saa7185_s_routing(struct v4l2_subdev *sd,
0234                  u32 input, u32 output, u32 config)
0235 {
0236     struct saa7185 *encoder = to_saa7185(sd);
0237 
0238     /* RJ: input = 0: input is from SA7111
0239      input = 1: input is from ZR36060 */
0240 
0241     switch (input) {
0242     case 0:
0243         /* turn off colorbar */
0244         saa7185_write(sd, 0x3a, 0x0f);
0245         /* Switch RTCE to 1 */
0246         saa7185_write(sd, 0x61, (encoder->reg[0x61] & 0xf7) | 0x08);
0247         saa7185_write(sd, 0x6e, 0x01);
0248         break;
0249 
0250     case 1:
0251         /* turn off colorbar */
0252         saa7185_write(sd, 0x3a, 0x0f);
0253         /* Switch RTCE to 0 */
0254         saa7185_write(sd, 0x61, (encoder->reg[0x61] & 0xf7) | 0x00);
0255         /* SW: a slight sync problem... */
0256         saa7185_write(sd, 0x6e, 0x00);
0257         break;
0258 
0259     case 2:
0260         /* turn on colorbar */
0261         saa7185_write(sd, 0x3a, 0x8f);
0262         /* Switch RTCE to 0 */
0263         saa7185_write(sd, 0x61, (encoder->reg[0x61] & 0xf7) | 0x08);
0264         /* SW: a slight sync problem... */
0265         saa7185_write(sd, 0x6e, 0x01);
0266         break;
0267 
0268     default:
0269         return -EINVAL;
0270     }
0271     return 0;
0272 }
0273 
0274 /* ----------------------------------------------------------------------- */
0275 
0276 static const struct v4l2_subdev_core_ops saa7185_core_ops = {
0277     .init = saa7185_init,
0278 };
0279 
0280 static const struct v4l2_subdev_video_ops saa7185_video_ops = {
0281     .s_std_output = saa7185_s_std_output,
0282     .s_routing = saa7185_s_routing,
0283 };
0284 
0285 static const struct v4l2_subdev_ops saa7185_ops = {
0286     .core = &saa7185_core_ops,
0287     .video = &saa7185_video_ops,
0288 };
0289 
0290 
0291 /* ----------------------------------------------------------------------- */
0292 
0293 static int saa7185_probe(struct i2c_client *client,
0294             const struct i2c_device_id *id)
0295 {
0296     int i;
0297     struct saa7185 *encoder;
0298     struct v4l2_subdev *sd;
0299 
0300     /* Check if the adapter supports the needed features */
0301     if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
0302         return -ENODEV;
0303 
0304     v4l_info(client, "chip found @ 0x%x (%s)\n",
0305             client->addr << 1, client->adapter->name);
0306 
0307     encoder = devm_kzalloc(&client->dev, sizeof(*encoder), GFP_KERNEL);
0308     if (encoder == NULL)
0309         return -ENOMEM;
0310     encoder->norm = V4L2_STD_NTSC;
0311     sd = &encoder->sd;
0312     v4l2_i2c_subdev_init(sd, client, &saa7185_ops);
0313 
0314     i = saa7185_write_block(sd, init_common, sizeof(init_common));
0315     if (i >= 0)
0316         i = saa7185_write_block(sd, init_ntsc, sizeof(init_ntsc));
0317     if (i < 0)
0318         v4l2_dbg(1, debug, sd, "init error %d\n", i);
0319     else
0320         v4l2_dbg(1, debug, sd, "revision 0x%x\n",
0321                 saa7185_read(sd) >> 5);
0322     return 0;
0323 }
0324 
0325 static int saa7185_remove(struct i2c_client *client)
0326 {
0327     struct v4l2_subdev *sd = i2c_get_clientdata(client);
0328     struct saa7185 *encoder = to_saa7185(sd);
0329 
0330     v4l2_device_unregister_subdev(sd);
0331     /* SW: output off is active */
0332     saa7185_write(sd, 0x61, (encoder->reg[0x61]) | 0x40);
0333     return 0;
0334 }
0335 
0336 /* ----------------------------------------------------------------------- */
0337 
0338 static const struct i2c_device_id saa7185_id[] = {
0339     { "saa7185", 0 },
0340     { }
0341 };
0342 MODULE_DEVICE_TABLE(i2c, saa7185_id);
0343 
0344 static struct i2c_driver saa7185_driver = {
0345     .driver = {
0346         .name   = "saa7185",
0347     },
0348     .probe      = saa7185_probe,
0349     .remove     = saa7185_remove,
0350     .id_table   = saa7185_id,
0351 };
0352 
0353 module_i2c_driver(saa7185_driver);