0001
0002
0003
0004
0005
0006 #include <linux/init.h>
0007 #include <linux/module.h>
0008 #include <linux/i2c.h>
0009 #include <linux/videodev2.h>
0010 #include <media/v4l2-device.h>
0011 #include <linux/slab.h>
0012
0013 MODULE_DESCRIPTION("OmniVision ov7640 sensor driver");
0014 MODULE_LICENSE("GPL v2");
0015
0016 struct reg_val {
0017 u8 reg;
0018 u8 val;
0019 };
0020
0021 static const struct reg_val regval_init[] = {
0022 {0x12, 0x80},
0023 {0x12, 0x54},
0024 {0x14, 0x24},
0025 {0x15, 0x01},
0026 {0x28, 0x20},
0027 {0x75, 0x82},
0028 };
0029
0030 static int write_regs(struct i2c_client *client,
0031 const struct reg_val *rv, int len)
0032 {
0033 while (--len >= 0) {
0034 if (i2c_smbus_write_byte_data(client, rv->reg, rv->val) < 0)
0035 return -1;
0036 rv++;
0037 }
0038 return 0;
0039 }
0040
0041
0042
0043 static const struct v4l2_subdev_ops ov7640_ops;
0044
0045 static int ov7640_probe(struct i2c_client *client,
0046 const struct i2c_device_id *id)
0047 {
0048 struct i2c_adapter *adapter = client->adapter;
0049 struct v4l2_subdev *sd;
0050
0051 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
0052 return -ENODEV;
0053
0054 sd = devm_kzalloc(&client->dev, sizeof(*sd), GFP_KERNEL);
0055 if (sd == NULL)
0056 return -ENOMEM;
0057 v4l2_i2c_subdev_init(sd, client, &ov7640_ops);
0058
0059 client->flags = I2C_CLIENT_SCCB;
0060
0061 v4l_info(client, "chip found @ 0x%02x (%s)\n",
0062 client->addr << 1, client->adapter->name);
0063
0064 if (write_regs(client, regval_init, ARRAY_SIZE(regval_init)) < 0) {
0065 v4l_err(client, "error initializing OV7640\n");
0066 return -ENODEV;
0067 }
0068
0069 return 0;
0070 }
0071
0072
0073 static int ov7640_remove(struct i2c_client *client)
0074 {
0075 struct v4l2_subdev *sd = i2c_get_clientdata(client);
0076
0077 v4l2_device_unregister_subdev(sd);
0078
0079 return 0;
0080 }
0081
0082 static const struct i2c_device_id ov7640_id[] = {
0083 { "ov7640", 0 },
0084 { }
0085 };
0086 MODULE_DEVICE_TABLE(i2c, ov7640_id);
0087
0088 static struct i2c_driver ov7640_driver = {
0089 .driver = {
0090 .name = "ov7640",
0091 },
0092 .probe = ov7640_probe,
0093 .remove = ov7640_remove,
0094 .id_table = ov7640_id,
0095 };
0096 module_i2c_driver(ov7640_driver);