This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / usb / media / sn9c102_pas106b.c
1 /***************************************************************************
2  * Driver for PAS106B image sensor connected to the SN9C10[12] PC Camera   *
3  * Controllers                                                             *
4  *                                                                         *
5  * Copyright (C) 2004 by Luca Risolia <luca.risolia@studio.unibo.it>       *
6  *                                                                         *
7  * This program is free software; you can redistribute it and/or modify    *
8  * it under the terms of the GNU General Public License as published by    *
9  * the Free Software Foundation; either version 2 of the License, or       *
10  * (at your option) any later version.                                     *
11  *                                                                         *
12  * This program is distributed in the hope that it will be useful,         *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
15  * GNU General Public License for more details.                            *
16  *                                                                         *
17  * You should have received a copy of the GNU General Public License       *
18  * along with this program; if not, write to the Free Software             *
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.               *
20  ***************************************************************************/
21
22 #include <linux/delay.h>
23 #include "sn9c102_sensor.h"
24
25
26 static struct sn9c102_sensor pas106b;
27
28
29 static int pas106b_init(struct sn9c102_device* cam)
30 {
31         int err = 0;
32
33         err += sn9c102_write_reg(cam, 0x00, 0x10);
34         err += sn9c102_write_reg(cam, 0x00, 0x11);
35         err += sn9c102_write_reg(cam, 0x00, 0x14);
36         err += sn9c102_write_reg(cam, 0x20, 0x17);
37         err += sn9c102_write_reg(cam, 0x20, 0x19);
38         err += sn9c102_write_reg(cam, 0x09, 0x18);
39
40         err += sn9c102_i2c_write(cam, 0x02, 0x0c);
41         err += sn9c102_i2c_write(cam, 0x03, 0x12);
42         err += sn9c102_i2c_write(cam, 0x04, 0x05);
43         err += sn9c102_i2c_write(cam, 0x05, 0x22);
44         err += sn9c102_i2c_write(cam, 0x06, 0xac);
45         err += sn9c102_i2c_write(cam, 0x07, 0x00);
46         err += sn9c102_i2c_write(cam, 0x08, 0x01);
47         err += sn9c102_i2c_write(cam, 0x0a, 0x00);
48         err += sn9c102_i2c_write(cam, 0x0b, 0x00);
49         err += sn9c102_i2c_write(cam, 0x0d, 0x00);
50         err += sn9c102_i2c_write(cam, 0x10, 0x06);
51         err += sn9c102_i2c_write(cam, 0x11, 0x06);
52         err += sn9c102_i2c_write(cam, 0x12, 0x00);
53         err += sn9c102_i2c_write(cam, 0x14, 0x02);
54         err += sn9c102_i2c_write(cam, 0x13, 0x01);
55
56         msleep(400);
57
58         return err;
59 }
60
61
62 static int pas106b_get_ctrl(struct sn9c102_device* cam, 
63                             struct v4l2_control* ctrl)
64 {
65         switch (ctrl->id) {
66         case V4L2_CID_RED_BALANCE:
67                 return (ctrl->value = sn9c102_i2c_read(cam, 0x0c))<0 ? -EIO:0;
68         case V4L2_CID_BLUE_BALANCE:
69                 return (ctrl->value = sn9c102_i2c_read(cam, 0x09))<0 ? -EIO:0;
70         case V4L2_CID_BRIGHTNESS:
71                 return (ctrl->value = sn9c102_i2c_read(cam, 0x0e))<0 ? -EIO:0;
72         default:
73                 return -EINVAL;
74         }
75 }
76
77
78 static int pas106b_set_ctrl(struct sn9c102_device* cam, 
79                             const struct v4l2_control* ctrl)
80 {
81         int err = 0;
82
83         switch (ctrl->id) {
84         case V4L2_CID_RED_BALANCE:
85                 err += sn9c102_i2c_write(cam, 0x0c, ctrl->value & 0x1f);
86                 break;
87         case V4L2_CID_BLUE_BALANCE:
88                 err += sn9c102_i2c_write(cam, 0x09, ctrl->value & 0x1f);
89                 break;
90         case V4L2_CID_BRIGHTNESS:
91                 err += sn9c102_i2c_write(cam, 0x0e, ctrl->value & 0x1f);
92                 break;
93         default:
94                 return -EINVAL;
95         }
96         err += sn9c102_i2c_write(cam, 0x13, 0x01);
97
98         return err;
99 }
100
101
102 static int pas106b_set_crop(struct sn9c102_device* cam, 
103                             const struct v4l2_rect* rect)
104 {
105         struct sn9c102_sensor* s = &pas106b;
106         int err = 0;
107         u8 h_start = (u8)(rect->left - s->cropcap.bounds.left) + 4,
108            v_start = (u8)(rect->top - s->cropcap.bounds.top) + 3;
109
110         err += sn9c102_write_reg(cam, h_start, 0x12);
111         err += sn9c102_write_reg(cam, v_start, 0x13);
112
113         return err;
114 }
115
116
117 static struct sn9c102_sensor pas106b = {
118         .name = "PAS106B",
119         .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>",
120         .frequency = SN9C102_I2C_400KHZ | SN9C102_I2C_100KHZ,
121         .interface = SN9C102_I2C_2WIRES,
122         .slave_read_id = 0x40,
123         .slave_write_id = 0x40,
124         .init = &pas106b_init,
125         .qctrl = {
126                 {
127                         .id = V4L2_CID_RED_BALANCE,
128                         .type = V4L2_CTRL_TYPE_INTEGER,
129                         .name = "red balance",
130                         .minimum = 0x00,
131                         .maximum = 0x1f,
132                         .step = 0x01,
133                         .default_value = 0x03,
134                         .flags = 0,
135                 },
136                 {
137                         .id = V4L2_CID_BLUE_BALANCE,
138                         .type = V4L2_CTRL_TYPE_INTEGER,
139                         .name = "blue balance",
140                         .minimum = 0x00,
141                         .maximum = 0x1f,
142                         .step = 0x01,
143                         .default_value = 0x02,
144                         .flags = 0,
145                 },
146                 {
147                         .id = V4L2_CID_BRIGHTNESS,
148                         .type = V4L2_CTRL_TYPE_INTEGER,
149                         .name = "brightness",
150                         .minimum = 0x00,
151                         .maximum = 0x1f,
152                         .step = 0x01,
153                         .default_value = 0x06,
154                         .flags = 0,
155                 },
156         },
157         .get_ctrl = &pas106b_get_ctrl,
158         .set_ctrl = &pas106b_set_ctrl,
159         .cropcap = {
160                 .bounds = {
161                         .left = 0,
162                         .top = 0,
163                         .width = 352,
164                         .height = 288,
165                 },
166                 .defrect = {
167                         .left = 0,
168                         .top = 0,
169                         .width = 352,
170                         .height = 288,
171                 },
172         },
173         .set_crop = &pas106b_set_crop,
174         .pix_format = {
175                 .width = 352,
176                 .height = 288,
177                 .pixelformat = V4L2_PIX_FMT_SBGGR8,
178                 .priv = 8, /* we use this field as 'bits per pixel' */
179         }
180 };
181
182
183 int sn9c102_probe_pas106b(struct sn9c102_device* cam)
184 {
185         int r0 = 0, r1 = 0, err = 0;
186         unsigned int pid = 0;
187
188         /* Minimal initialization to enable the I2C communication
189            NOTE: do NOT change the values! */
190         err += sn9c102_write_reg(cam, 0x01, 0x01); /* sensor power down */
191         err += sn9c102_write_reg(cam, 0x00, 0x01); /* sensor power on */
192         err += sn9c102_write_reg(cam, 0x28, 0x17); /* sensor clock at 48 MHz */
193         if (err)
194                 return -EIO;
195
196         r0 = sn9c102_i2c_try_read(cam, &pas106b, 0x00);
197         r1 = sn9c102_i2c_try_read(cam, &pas106b, 0x01);
198
199         if (r0 < 0 || r1 < 0)
200                 return -EIO;
201
202         pid = (r0 << 11) | ((r1 & 0xf0) >> 4);
203         if (pid != 0x007)
204                 return -ENODEV;
205
206         sn9c102_attach_sensor(cam, &pas106b);
207
208         return 0;
209 }