Linux-libre 5.3.12-gnu
[librecmc/linux-libre.git] / drivers / media / i2c / ov7640.c
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 static const u8 initial_registers[] = {
17         0x12, 0x80,
18         0x12, 0x54,
19         0x14, 0x24,
20         0x15, 0x01,
21         0x28, 0x20,
22         0x75, 0x82,
23         0xFF, 0xFF, /* Terminator (reg 0xFF is unused) */
24 };
25
26 static int write_regs(struct i2c_client *client, const u8 *regs)
27 {
28         int i;
29
30         for (i = 0; regs[i] != 0xFF; i += 2)
31                 if (i2c_smbus_write_byte_data(client, regs[i], regs[i + 1]) < 0)
32                         return -1;
33         return 0;
34 }
35
36 /* ----------------------------------------------------------------------- */
37
38 static const struct v4l2_subdev_ops ov7640_ops;
39
40 static int ov7640_probe(struct i2c_client *client,
41                         const struct i2c_device_id *id)
42 {
43         struct i2c_adapter *adapter = client->adapter;
44         struct v4l2_subdev *sd;
45
46         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
47                 return -ENODEV;
48
49         sd = devm_kzalloc(&client->dev, sizeof(*sd), GFP_KERNEL);
50         if (sd == NULL)
51                 return -ENOMEM;
52         v4l2_i2c_subdev_init(sd, client, &ov7640_ops);
53
54         client->flags = I2C_CLIENT_SCCB;
55
56         v4l_info(client, "chip found @ 0x%02x (%s)\n",
57                         client->addr << 1, client->adapter->name);
58
59         if (write_regs(client, initial_registers) < 0) {
60                 v4l_err(client, "error initializing OV7640\n");
61                 return -ENODEV;
62         }
63
64         return 0;
65 }
66
67
68 static int ov7640_remove(struct i2c_client *client)
69 {
70         struct v4l2_subdev *sd = i2c_get_clientdata(client);
71
72         v4l2_device_unregister_subdev(sd);
73
74         return 0;
75 }
76
77 static const struct i2c_device_id ov7640_id[] = {
78         { "ov7640", 0 },
79         { }
80 };
81 MODULE_DEVICE_TABLE(i2c, ov7640_id);
82
83 static struct i2c_driver ov7640_driver = {
84         .driver = {
85                 .name   = "ov7640",
86         },
87         .probe = ov7640_probe,
88         .remove = ov7640_remove,
89         .id_table = ov7640_id,
90 };
91 module_i2c_driver(ov7640_driver);