xref: /openbmc/linux/drivers/media/i2c/ov7640.c (revision 82e6fdd6)
1 /*
2  * Copyright (C) 2005-2006 Micronas USA Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License (Version 2) as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13 
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/i2c.h>
17 #include <linux/videodev2.h>
18 #include <media/v4l2-device.h>
19 #include <linux/slab.h>
20 
21 MODULE_DESCRIPTION("OmniVision ov7640 sensor driver");
22 MODULE_LICENSE("GPL v2");
23 
24 static const u8 initial_registers[] = {
25 	0x12, 0x80,
26 	0x12, 0x54,
27 	0x14, 0x24,
28 	0x15, 0x01,
29 	0x28, 0x20,
30 	0x75, 0x82,
31 	0xFF, 0xFF, /* Terminator (reg 0xFF is unused) */
32 };
33 
34 static int write_regs(struct i2c_client *client, const u8 *regs)
35 {
36 	int i;
37 
38 	for (i = 0; regs[i] != 0xFF; i += 2)
39 		if (i2c_smbus_write_byte_data(client, regs[i], regs[i + 1]) < 0)
40 			return -1;
41 	return 0;
42 }
43 
44 /* ----------------------------------------------------------------------- */
45 
46 static const struct v4l2_subdev_ops ov7640_ops;
47 
48 static int ov7640_probe(struct i2c_client *client,
49 			const struct i2c_device_id *id)
50 {
51 	struct i2c_adapter *adapter = client->adapter;
52 	struct v4l2_subdev *sd;
53 
54 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
55 		return -ENODEV;
56 
57 	sd = devm_kzalloc(&client->dev, sizeof(*sd), GFP_KERNEL);
58 	if (sd == NULL)
59 		return -ENOMEM;
60 	v4l2_i2c_subdev_init(sd, client, &ov7640_ops);
61 
62 	client->flags = I2C_CLIENT_SCCB;
63 
64 	v4l_info(client, "chip found @ 0x%02x (%s)\n",
65 			client->addr << 1, client->adapter->name);
66 
67 	if (write_regs(client, initial_registers) < 0) {
68 		v4l_err(client, "error initializing OV7640\n");
69 		return -ENODEV;
70 	}
71 
72 	return 0;
73 }
74 
75 
76 static int ov7640_remove(struct i2c_client *client)
77 {
78 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
79 
80 	v4l2_device_unregister_subdev(sd);
81 
82 	return 0;
83 }
84 
85 static const struct i2c_device_id ov7640_id[] = {
86 	{ "ov7640", 0 },
87 	{ }
88 };
89 MODULE_DEVICE_TABLE(i2c, ov7640_id);
90 
91 static struct i2c_driver ov7640_driver = {
92 	.driver = {
93 		.name	= "ov7640",
94 	},
95 	.probe = ov7640_probe,
96 	.remove = ov7640_remove,
97 	.id_table = ov7640_id,
98 };
99 module_i2c_driver(ov7640_driver);
100