Staging: add the go7007 video driver
[linux-2.6] / drivers / staging / go7007 / wis-ov7640.c
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  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software Foundation,
15  * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
16  */
17
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/version.h>
21 #include <linux/i2c.h>
22 #include <linux/videodev.h>
23 #include <linux/video_decoder.h>
24
25 #include "wis-i2c.h"
26
27 struct wis_ov7640 {
28         int brightness;
29         int contrast;
30         int saturation;
31         int hue;
32 };
33
34 static u8 initial_registers[] =
35 {
36         0x12, 0x80,
37         0x12, 0x54,
38         0x14, 0x24,
39         0x15, 0x01,
40         0x28, 0x20,
41         0x75, 0x82,
42         0xFF, 0xFF, /* Terminator (reg 0xFF is unused) */
43 };
44
45 static int write_regs(struct i2c_client *client, u8 *regs)
46 {
47         int i;
48
49         for (i = 0; regs[i] != 0xFF; i += 2)
50                 if (i2c_smbus_write_byte_data(client, regs[i], regs[i + 1]) < 0)
51                         return -1;
52         return 0;
53 }
54
55 static struct i2c_driver wis_ov7640_driver;
56
57 static struct i2c_client wis_ov7640_client_templ = {
58         .name           = "OV7640 (WIS)",
59         .driver         = &wis_ov7640_driver,
60 };
61
62 static int wis_ov7640_detect(struct i2c_adapter *adapter, int addr, int kind)
63 {
64         struct i2c_client *client;
65
66         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
67                 return 0;
68
69         client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
70         if (client == NULL)
71                 return -ENOMEM;
72         memcpy(client, &wis_ov7640_client_templ,
73                         sizeof(wis_ov7640_client_templ));
74         client->adapter = adapter;
75         client->addr = addr;
76         client->flags = I2C_CLIENT_SCCB;
77
78         printk(KERN_DEBUG
79                 "wis-ov7640: initializing OV7640 at address %d on %s\n",
80                 addr, adapter->name);
81
82         if (write_regs(client, initial_registers) < 0) {
83                 printk(KERN_ERR "wis-ov7640: error initializing OV7640\n");
84                 kfree(client);
85                 return 0;
86         }
87
88         i2c_attach_client(client);
89         return 0;
90 }
91
92 static int wis_ov7640_detach(struct i2c_client *client)
93 {
94         int r;
95
96         r = i2c_detach_client(client);
97         if (r < 0)
98                 return r;
99
100         kfree(client);
101         return 0;
102 }
103
104 static struct i2c_driver wis_ov7640_driver = {
105         .driver = {
106                 .name   = "WIS OV7640 I2C driver",
107         },
108         .id             = I2C_DRIVERID_WIS_OV7640,
109         .detach_client  = wis_ov7640_detach,
110 };
111
112 static int __init wis_ov7640_init(void)
113 {
114         int r;
115
116         r = i2c_add_driver(&wis_ov7640_driver);
117         if (r < 0)
118                 return r;
119         return wis_i2c_add_driver(wis_ov7640_driver.id, wis_ov7640_detect);
120 }
121
122 static void __exit wis_ov7640_cleanup(void)
123 {
124         wis_i2c_del_driver(wis_ov7640_detect);
125         i2c_del_driver(&wis_ov7640_driver);
126 }
127
128 module_init(wis_ov7640_init);
129 module_exit(wis_ov7640_cleanup);
130
131 MODULE_LICENSE("GPL v2");