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