This commit was generated by cvs2svn to compensate for changes in r517,
[linux-2.6.git] / drivers / usb / media / sn9c102_pas202bcb.c
1 /***************************************************************************
2  * Plug-in for PAS202BCB image sensor connected to the SN9C10x PC Camera   *
3  * Controllers                                                             *
4  *                                                                         *
5  * Copyright (C) 2004 by Carlos Eduardo Medaglia Dyonisio                  *
6  *                       <medaglia@undl.org.br>                            *
7  *                       http://cadu.homelinux.com:8080/                   *
8  *                                                                         *
9  * DAC Magnitude, DAC sign, exposure and green gain controls added by      *
10  * Luca Risolia <luca.risolia@studio.unibo.it>                             *
11  *                                                                         *
12  * This program is free software; you can redistribute it and/or modify    *
13  * it under the terms of the GNU General Public License as published by    *
14  * the Free Software Foundation; either version 2 of the License, or       *
15  * (at your option) any later version.                                     *
16  *                                                                         *
17  * This program is distributed in the hope that it will be useful,         *
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
20  * GNU General Public License for more details.                            *
21  *                                                                         *
22  * You should have received a copy of the GNU General Public License       *
23  * along with this program; if not, write to the Free Software             *
24  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.               *
25  ***************************************************************************/
26
27 #include <linux/delay.h>
28 #include "sn9c102_sensor.h"
29
30
31 static struct sn9c102_sensor pas202bcb;
32
33
34 static int pas202bcb_init(struct sn9c102_device* cam)
35 {
36         int err = 0;
37
38         err += sn9c102_write_reg(cam, 0x00, 0x10);
39         err += sn9c102_write_reg(cam, 0x00, 0x11);
40         err += sn9c102_write_reg(cam, 0x00, 0x14);
41         err += sn9c102_write_reg(cam, 0x20, 0x17);
42         err += sn9c102_write_reg(cam, 0x30, 0x19);
43         err += sn9c102_write_reg(cam, 0x09, 0x18);
44
45         err += sn9c102_i2c_write(cam, 0x02, 0x14);
46         err += sn9c102_i2c_write(cam, 0x03, 0x40);
47         err += sn9c102_i2c_write(cam, 0x0d, 0x2c);
48         err += sn9c102_i2c_write(cam, 0x0e, 0x01);
49         err += sn9c102_i2c_write(cam, 0x0f, 0xa9);
50         err += sn9c102_i2c_write(cam, 0x10, 0x08);
51         err += sn9c102_i2c_write(cam, 0x13, 0x63);
52         err += sn9c102_i2c_write(cam, 0x15, 0x70);
53         err += sn9c102_i2c_write(cam, 0x11, 0x01);
54
55         msleep(400);
56
57         return err;
58 }
59
60
61 static int pas202bcb_get_ctrl(struct sn9c102_device* cam, 
62                               struct v4l2_control* ctrl)
63 {
64         switch (ctrl->id) {
65         case V4L2_CID_EXPOSURE:
66                 {
67                         int r1 = sn9c102_i2c_read(cam, 0x04),
68                             r2 = sn9c102_i2c_read(cam, 0x05);
69                         if (r1 < 0 || r2 < 0)
70                                 return -EIO;
71                         ctrl->value = (r1 << 6) | (r2 & 0x3f);
72                 }
73                 return 0;
74         case V4L2_CID_RED_BALANCE:
75                 if ((ctrl->value = sn9c102_i2c_read(cam, 0x09)) < 0)
76                         return -EIO;
77                 ctrl->value &= 0x0f;
78                 return 0;
79         case V4L2_CID_BLUE_BALANCE:
80                 if ((ctrl->value = sn9c102_i2c_read(cam, 0x07)) < 0)
81                         return -EIO;
82                 ctrl->value &= 0x0f;
83                 return 0;
84         case V4L2_CID_GAIN:
85                 if ((ctrl->value = sn9c102_i2c_read(cam, 0x10)) < 0)
86                         return -EIO;
87                 ctrl->value &= 0x1f;
88                 return 0;
89         case SN9C102_V4L2_CID_GREEN_BALANCE:
90                 if ((ctrl->value = sn9c102_i2c_read(cam, 0x08)) < 0)
91                         return -EIO;
92                 ctrl->value &= 0x0f;
93                 return 0;
94         case SN9C102_V4L2_CID_DAC_MAGNITUDE:
95                 if ((ctrl->value = sn9c102_i2c_read(cam, 0x0c)) < 0)
96                         return -EIO;
97                 return 0;
98         case SN9C102_V4L2_CID_DAC_SIGN:
99                 if ((ctrl->value = sn9c102_i2c_read(cam, 0x0b)) < 0)
100                         return -EIO;
101                 ctrl->value &= 0x01;
102                 return 0;
103         default:
104                 return -EINVAL;
105         }
106 }
107
108
109 static int pas202bcb_set_ctrl(struct sn9c102_device* cam, 
110                               const struct v4l2_control* ctrl)
111 {
112         int err = 0;
113
114         switch (ctrl->id) {
115         case V4L2_CID_EXPOSURE:
116                 err += sn9c102_i2c_write(cam, 0x04, ctrl->value >> 6);
117                 err += sn9c102_i2c_write(cam, 0x05, ctrl->value & 0x3f);
118                 break;
119         case V4L2_CID_RED_BALANCE:
120                 err += sn9c102_i2c_write(cam, 0x09, ctrl->value);
121                 break;
122         case V4L2_CID_BLUE_BALANCE:
123                 err += sn9c102_i2c_write(cam, 0x07, ctrl->value);
124                 break;
125         case V4L2_CID_GAIN:
126                 err += sn9c102_i2c_write(cam, 0x10, ctrl->value);
127                 break;
128         case SN9C102_V4L2_CID_GREEN_BALANCE:
129                 err += sn9c102_i2c_write(cam, 0x08, ctrl->value);
130                 break;
131         case SN9C102_V4L2_CID_DAC_MAGNITUDE:
132                 err += sn9c102_i2c_write(cam, 0x0c, ctrl->value);
133                 break;
134         case SN9C102_V4L2_CID_DAC_SIGN:
135                 {
136                         int r;
137                         err += (r = sn9c102_i2c_read(cam, 0x0b)) < 0 ? r : 0;
138                         err += sn9c102_i2c_write(cam, 0x0b, r | ctrl->value);
139                 }
140                 break;
141         default:
142                 return -EINVAL;
143         }
144         err += sn9c102_i2c_write(cam, 0x11, 0x01);
145
146         return err ? -EIO : 0;
147 }
148
149
150 static int pas202bcb_set_crop(struct sn9c102_device* cam, 
151                               const struct v4l2_rect* rect)
152 {
153         struct sn9c102_sensor* s = &pas202bcb;
154         int err = 0;
155         u8 h_start = (u8)(rect->left - s->cropcap.bounds.left) + 4,
156            v_start = (u8)(rect->top - s->cropcap.bounds.top) + 3;
157
158         err += sn9c102_write_reg(cam, h_start, 0x12);
159         err += sn9c102_write_reg(cam, v_start, 0x13);
160
161         return err;
162 }
163
164
165 static struct sn9c102_sensor pas202bcb = {
166         .name = "PAS202BCB",
167         .maintainer = "Carlos Eduardo Medaglia Dyonisio "
168                       "<medaglia@undl.org.br>",
169         .frequency = SN9C102_I2C_400KHZ | SN9C102_I2C_100KHZ,
170         .interface = SN9C102_I2C_2WIRES,
171         .slave_read_id = 0x40,
172         .slave_write_id = 0x40,
173         .init = &pas202bcb_init,
174         .qctrl = {
175                 {
176                         .id = V4L2_CID_EXPOSURE,
177                         .type = V4L2_CTRL_TYPE_INTEGER,
178                         .name = "exposure",
179                         .minimum = 0x01e5,
180                         .maximum = 0x3fff,
181                         .step = 0x01,
182                         .default_value = 0x01e5,
183                         .flags = 0,
184                 },
185                 {
186                         .id = V4L2_CID_GAIN,
187                         .type = V4L2_CTRL_TYPE_INTEGER,
188                         .name = "global gain",
189                         .minimum = 0x00,
190                         .maximum = 0x1f,
191                         .step = 0x01,
192                         .default_value = 0x0c,
193                         .flags = 0,
194                 },
195                 {
196                         .id = V4L2_CID_RED_BALANCE,
197                         .type = V4L2_CTRL_TYPE_INTEGER,
198                         .name = "red balance",
199                         .minimum = 0x00,
200                         .maximum = 0x0f,
201                         .step = 0x01,
202                         .default_value = 0x01,
203                         .flags = 0,
204                 },
205                 {
206                         .id = V4L2_CID_BLUE_BALANCE,
207                         .type = V4L2_CTRL_TYPE_INTEGER,
208                         .name = "blue balance",
209                         .minimum = 0x00,
210                         .maximum = 0x0f,
211                         .step = 0x01,
212                         .default_value = 0x05,
213                         .flags = 0,
214                 },
215                 {
216                         .id = SN9C102_V4L2_CID_GREEN_BALANCE,
217                         .type = V4L2_CTRL_TYPE_INTEGER,
218                         .name = "green balance",
219                         .minimum = 0x00,
220                         .maximum = 0x0f,
221                         .step = 0x01,
222                         .default_value = 0x00,
223                         .flags = 0,
224                 },
225                 {
226                         .id = SN9C102_V4L2_CID_DAC_MAGNITUDE,
227                         .type = V4L2_CTRL_TYPE_INTEGER,
228                         .name = "DAC magnitude",
229                         .minimum = 0x00,
230                         .maximum = 0xff,
231                         .step = 0x01,
232                         .default_value = 0x04,
233                         .flags = 0,
234                 },
235                 {
236                         .id = SN9C102_V4L2_CID_DAC_SIGN,
237                         .type = V4L2_CTRL_TYPE_BOOLEAN,
238                         .name = "DAC sign",
239                         .minimum = 0x00,
240                         .maximum = 0x01,
241                         .step = 0x01,
242                         .default_value = 0x01,
243                         .flags = 0,
244                 },
245         },
246         .get_ctrl = &pas202bcb_get_ctrl,
247         .set_ctrl = &pas202bcb_set_ctrl,
248         .cropcap = {
249                 .bounds = {
250                         .left = 0,
251                         .top = 0,
252                         .width = 640,
253                         .height = 480,
254                 },
255                 .defrect = {
256                         .left = 0,
257                         .top = 0,
258                         .width = 640,
259                         .height = 480,
260                 },
261         },
262         .set_crop = &pas202bcb_set_crop,
263         .pix_format = {
264                 .width = 640,
265                 .height = 480,
266                 .pixelformat = V4L2_PIX_FMT_SBGGR8,
267                 .priv = 8,
268         }
269 };
270
271
272 int sn9c102_probe_pas202bcb(struct sn9c102_device* cam)
273 {       
274         int r0 = 0, r1 = 0, err = 0;
275         unsigned int pid = 0;
276
277         /*
278          *  Minimal initialization to enable the I2C communication
279          *  NOTE: do NOT change the values!
280          */
281         err += sn9c102_write_reg(cam, 0x01, 0x01); /* sensor power down */
282         err += sn9c102_write_reg(cam, 0x40, 0x01); /* sensor power on */
283         err += sn9c102_write_reg(cam, 0x28, 0x17); /* sensor clock at 24 MHz */
284         if (err)
285                 return -EIO;
286
287         r0 = sn9c102_i2c_try_read(cam, &pas202bcb, 0x00);
288         r1 = sn9c102_i2c_try_read(cam, &pas202bcb, 0x01);
289         
290         if (r0 < 0 || r1 < 0)
291                 return -EIO;
292
293         pid = (r0 << 4) | ((r1 & 0xf0) >> 4);
294         if (pid != 0x017)
295                 return -ENODEV;
296
297         sn9c102_attach_sensor(cam, &pas202bcb);
298
299         return 0;
300 }