1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2005-2006 Micronas USA Inc.
4  */
5 
6 #include <linux/init.h>
7 #include <linux/module.h>
8 #include <linux/i2c.h>
9 #include <linux/videodev2.h>
10 #include <media/v4l2-device.h>
11 #include <linux/slab.h>
12 
13 MODULE_DESCRIPTION("OmniVision ov7640 sensor driver");
14 MODULE_LICENSE("GPL v2");
15 
16 struct reg_val {
17 	u8 reg;
18 	u8 val;
19 };
20 
21 static const struct reg_val regval_init[] = {
22 	{0x12, 0x80},
23 	{0x12, 0x54},
24 	{0x14, 0x24},
25 	{0x15, 0x01},
26 	{0x28, 0x20},
27 	{0x75, 0x82},
28 };
29 
write_regs(struct i2c_client * client,const struct reg_val * rv,int len)30 static int write_regs(struct i2c_client *client,
31 		const struct reg_val *rv, int len)
32 {
33 	while (--len >= 0) {
34 		if (i2c_smbus_write_byte_data(client, rv->reg, rv->val) < 0)
35 			return -1;
36 		rv++;
37 	}
38 	return 0;
39 }
40 
41 /* ----------------------------------------------------------------------- */
42 
43 static const struct v4l2_subdev_ops ov7640_ops;
44 
ov7640_probe(struct i2c_client * client)45 static int ov7640_probe(struct i2c_client *client)
46 {
47 	struct i2c_adapter *adapter = client->adapter;
48 	struct v4l2_subdev *sd;
49 
50 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
51 		return -ENODEV;
52 
53 	sd = devm_kzalloc(&client->dev, sizeof(*sd), GFP_KERNEL);
54 	if (sd == NULL)
55 		return -ENOMEM;
56 	v4l2_i2c_subdev_init(sd, client, &ov7640_ops);
57 
58 	client->flags = I2C_CLIENT_SCCB;
59 
60 	v4l_info(client, "chip found @ 0x%02x (%s)\n",
61 			client->addr << 1, client->adapter->name);
62 
63 	if (write_regs(client, regval_init, ARRAY_SIZE(regval_init)) < 0) {
64 		v4l_err(client, "error initializing OV7640\n");
65 		return -ENODEV;
66 	}
67 
68 	return 0;
69 }
70 
71 
ov7640_remove(struct i2c_client * client)72 static void ov7640_remove(struct i2c_client *client)
73 {
74 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
75 
76 	v4l2_device_unregister_subdev(sd);
77 }
78 
79 static const struct i2c_device_id ov7640_id[] = {
80 	{ "ov7640", 0 },
81 	{ }
82 };
83 MODULE_DEVICE_TABLE(i2c, ov7640_id);
84 
85 static struct i2c_driver ov7640_driver = {
86 	.driver = {
87 		.name	= "ov7640",
88 	},
89 	.probe_new = ov7640_probe,
90 	.remove = ov7640_remove,
91 	.id_table = ov7640_id,
92 };
93 module_i2c_driver(ov7640_driver);
94