blob: f5f11e927af3b3bf9be168c1b50daa1bcd37b9cd [file] [log] [blame]
Greg Kroah-Hartman866b8692008-02-15 16:53:09 -08001/*
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>
Ross Cohendf20d692008-09-29 22:36:24 -040022#include <linux/videodev2.h>
Greg Kroah-Hartman866b8692008-02-15 16:53:09 -080023
24#include "wis-i2c.h"
25
26struct wis_ov7640 {
27 int brightness;
28 int contrast;
29 int saturation;
30 int hue;
31};
32
33static u8 initial_registers[] =
34{
35 0x12, 0x80,
36 0x12, 0x54,
37 0x14, 0x24,
38 0x15, 0x01,
39 0x28, 0x20,
40 0x75, 0x82,
41 0xFF, 0xFF, /* Terminator (reg 0xFF is unused) */
42};
43
44static int write_regs(struct i2c_client *client, u8 *regs)
45{
46 int i;
47
48 for (i = 0; regs[i] != 0xFF; i += 2)
49 if (i2c_smbus_write_byte_data(client, regs[i], regs[i + 1]) < 0)
50 return -1;
51 return 0;
52}
53
54static struct i2c_driver wis_ov7640_driver;
55
56static struct i2c_client wis_ov7640_client_templ = {
57 .name = "OV7640 (WIS)",
58 .driver = &wis_ov7640_driver,
59};
60
61static int wis_ov7640_detect(struct i2c_adapter *adapter, int addr, int kind)
62{
63 struct i2c_client *client;
64
65 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
66 return 0;
67
68 client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
69 if (client == NULL)
70 return -ENOMEM;
71 memcpy(client, &wis_ov7640_client_templ,
72 sizeof(wis_ov7640_client_templ));
73 client->adapter = adapter;
74 client->addr = addr;
75 client->flags = I2C_CLIENT_SCCB;
76
77 printk(KERN_DEBUG
78 "wis-ov7640: initializing OV7640 at address %d on %s\n",
79 addr, adapter->name);
80
81 if (write_regs(client, initial_registers) < 0) {
82 printk(KERN_ERR "wis-ov7640: error initializing OV7640\n");
83 kfree(client);
84 return 0;
85 }
86
87 i2c_attach_client(client);
88 return 0;
89}
90
91static int wis_ov7640_detach(struct i2c_client *client)
92{
93 int r;
94
95 r = i2c_detach_client(client);
96 if (r < 0)
97 return r;
98
99 kfree(client);
100 return 0;
101}
102
103static struct i2c_driver wis_ov7640_driver = {
104 .driver = {
105 .name = "WIS OV7640 I2C driver",
106 },
107 .id = I2C_DRIVERID_WIS_OV7640,
108 .detach_client = wis_ov7640_detach,
109};
110
111static int __init wis_ov7640_init(void)
112{
113 int r;
114
115 r = i2c_add_driver(&wis_ov7640_driver);
116 if (r < 0)
117 return r;
118 return wis_i2c_add_driver(wis_ov7640_driver.id, wis_ov7640_detect);
119}
120
121static void __exit wis_ov7640_cleanup(void)
122{
123 wis_i2c_del_driver(wis_ov7640_detect);
124 i2c_del_driver(&wis_ov7640_driver);
125}
126
127module_init(wis_ov7640_init);
128module_exit(wis_ov7640_cleanup);
129
130MODULE_LICENSE("GPL v2");