0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 #include <linux/module.h>
0019 #include <linux/types.h>
0020 #include <linux/slab.h>
0021 #include <linux/ioctl.h>
0022 #include <linux/uaccess.h>
0023 #include <linux/i2c.h>
0024 #include <linux/videodev2.h>
0025 #include <media/v4l2-device.h>
0026
0027 MODULE_DESCRIPTION("Brooktree-856A video encoder driver");
0028 MODULE_AUTHOR("Mike Bernson & Dave Perks");
0029 MODULE_LICENSE("GPL");
0030
0031 static int debug;
0032 module_param(debug, int, 0);
0033 MODULE_PARM_DESC(debug, "Debug level (0-1)");
0034
0035
0036
0037
0038 #define BT856_REG_OFFSET 0xDA
0039 #define BT856_NR_REG 6
0040
0041 struct bt856 {
0042 struct v4l2_subdev sd;
0043 unsigned char reg[BT856_NR_REG];
0044
0045 v4l2_std_id norm;
0046 };
0047
0048 static inline struct bt856 *to_bt856(struct v4l2_subdev *sd)
0049 {
0050 return container_of(sd, struct bt856, sd);
0051 }
0052
0053
0054
0055 static inline int bt856_write(struct bt856 *encoder, u8 reg, u8 value)
0056 {
0057 struct i2c_client *client = v4l2_get_subdevdata(&encoder->sd);
0058
0059 encoder->reg[reg - BT856_REG_OFFSET] = value;
0060 return i2c_smbus_write_byte_data(client, reg, value);
0061 }
0062
0063 static inline int bt856_setbit(struct bt856 *encoder, u8 reg, u8 bit, u8 value)
0064 {
0065 return bt856_write(encoder, reg,
0066 (encoder->reg[reg - BT856_REG_OFFSET] & ~(1 << bit)) |
0067 (value ? (1 << bit) : 0));
0068 }
0069
0070 static void bt856_dump(struct bt856 *encoder)
0071 {
0072 int i;
0073
0074 v4l2_info(&encoder->sd, "register dump:\n");
0075 for (i = 0; i < BT856_NR_REG; i += 2)
0076 printk(KERN_CONT " %02x", encoder->reg[i]);
0077 printk(KERN_CONT "\n");
0078 }
0079
0080
0081
0082 static int bt856_init(struct v4l2_subdev *sd, u32 arg)
0083 {
0084 struct bt856 *encoder = to_bt856(sd);
0085
0086
0087 v4l2_dbg(1, debug, sd, "init\n");
0088 bt856_write(encoder, 0xdc, 0x18);
0089 bt856_write(encoder, 0xda, 0);
0090 bt856_write(encoder, 0xde, 0);
0091
0092 bt856_setbit(encoder, 0xdc, 3, 1);
0093
0094 bt856_setbit(encoder, 0xdc, 4, 1);
0095
0096 if (encoder->norm & V4L2_STD_NTSC)
0097 bt856_setbit(encoder, 0xdc, 2, 0);
0098 else
0099 bt856_setbit(encoder, 0xdc, 2, 1);
0100
0101 bt856_setbit(encoder, 0xdc, 1, 1);
0102 bt856_setbit(encoder, 0xde, 4, 0);
0103 bt856_setbit(encoder, 0xde, 3, 1);
0104 if (debug != 0)
0105 bt856_dump(encoder);
0106 return 0;
0107 }
0108
0109 static int bt856_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std)
0110 {
0111 struct bt856 *encoder = to_bt856(sd);
0112
0113 v4l2_dbg(1, debug, sd, "set norm %llx\n", (unsigned long long)std);
0114
0115 if (std & V4L2_STD_NTSC) {
0116 bt856_setbit(encoder, 0xdc, 2, 0);
0117 } else if (std & V4L2_STD_PAL) {
0118 bt856_setbit(encoder, 0xdc, 2, 1);
0119 bt856_setbit(encoder, 0xda, 0, 0);
0120
0121 } else {
0122 return -EINVAL;
0123 }
0124 encoder->norm = std;
0125 if (debug != 0)
0126 bt856_dump(encoder);
0127 return 0;
0128 }
0129
0130 static int bt856_s_routing(struct v4l2_subdev *sd,
0131 u32 input, u32 output, u32 config)
0132 {
0133 struct bt856 *encoder = to_bt856(sd);
0134
0135 v4l2_dbg(1, debug, sd, "set input %d\n", input);
0136
0137
0138
0139
0140 switch (input) {
0141 case 0:
0142 bt856_setbit(encoder, 0xde, 4, 0);
0143 bt856_setbit(encoder, 0xde, 3, 1);
0144 bt856_setbit(encoder, 0xdc, 3, 1);
0145 bt856_setbit(encoder, 0xdc, 6, 0);
0146 break;
0147 case 1:
0148 bt856_setbit(encoder, 0xde, 4, 0);
0149 bt856_setbit(encoder, 0xde, 3, 1);
0150 bt856_setbit(encoder, 0xdc, 3, 1);
0151 bt856_setbit(encoder, 0xdc, 6, 1);
0152 break;
0153 case 2:
0154 bt856_setbit(encoder, 0xdc, 3, 0);
0155 bt856_setbit(encoder, 0xde, 4, 1);
0156 break;
0157 default:
0158 return -EINVAL;
0159 }
0160
0161 if (debug != 0)
0162 bt856_dump(encoder);
0163 return 0;
0164 }
0165
0166
0167
0168 static const struct v4l2_subdev_core_ops bt856_core_ops = {
0169 .init = bt856_init,
0170 };
0171
0172 static const struct v4l2_subdev_video_ops bt856_video_ops = {
0173 .s_std_output = bt856_s_std_output,
0174 .s_routing = bt856_s_routing,
0175 };
0176
0177 static const struct v4l2_subdev_ops bt856_ops = {
0178 .core = &bt856_core_ops,
0179 .video = &bt856_video_ops,
0180 };
0181
0182
0183
0184 static int bt856_probe(struct i2c_client *client,
0185 const struct i2c_device_id *id)
0186 {
0187 struct bt856 *encoder;
0188 struct v4l2_subdev *sd;
0189
0190
0191 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
0192 return -ENODEV;
0193
0194 v4l_info(client, "chip found @ 0x%x (%s)\n",
0195 client->addr << 1, client->adapter->name);
0196
0197 encoder = devm_kzalloc(&client->dev, sizeof(*encoder), GFP_KERNEL);
0198 if (encoder == NULL)
0199 return -ENOMEM;
0200 sd = &encoder->sd;
0201 v4l2_i2c_subdev_init(sd, client, &bt856_ops);
0202 encoder->norm = V4L2_STD_NTSC;
0203
0204 bt856_write(encoder, 0xdc, 0x18);
0205 bt856_write(encoder, 0xda, 0);
0206 bt856_write(encoder, 0xde, 0);
0207
0208 bt856_setbit(encoder, 0xdc, 3, 1);
0209
0210 bt856_setbit(encoder, 0xdc, 4, 1);
0211
0212 if (encoder->norm & V4L2_STD_NTSC)
0213 bt856_setbit(encoder, 0xdc, 2, 0);
0214 else
0215 bt856_setbit(encoder, 0xdc, 2, 1);
0216
0217 bt856_setbit(encoder, 0xdc, 1, 1);
0218 bt856_setbit(encoder, 0xde, 4, 0);
0219 bt856_setbit(encoder, 0xde, 3, 1);
0220
0221 if (debug != 0)
0222 bt856_dump(encoder);
0223 return 0;
0224 }
0225
0226 static int bt856_remove(struct i2c_client *client)
0227 {
0228 struct v4l2_subdev *sd = i2c_get_clientdata(client);
0229
0230 v4l2_device_unregister_subdev(sd);
0231 return 0;
0232 }
0233
0234 static const struct i2c_device_id bt856_id[] = {
0235 { "bt856", 0 },
0236 { }
0237 };
0238 MODULE_DEVICE_TABLE(i2c, bt856_id);
0239
0240 static struct i2c_driver bt856_driver = {
0241 .driver = {
0242 .name = "bt856",
0243 },
0244 .probe = bt856_probe,
0245 .remove = bt856_remove,
0246 .id_table = bt856_id,
0247 };
0248
0249 module_i2c_driver(bt856_driver);