54942d607a9306b94a18f2c50205eaeba9c9c76d
[linux-2.6.git] / drivers / usb / media / sn9c102_pas106b.c
1 /***************************************************************************
2  * Plug-in for PAS106B image sensor connected to the SN9C10x 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, 0x05, 0x5a);
42         err += sn9c102_i2c_write(cam, 0x06, 0x88);
43         err += sn9c102_i2c_write(cam, 0x07, 0x80);
44         err += sn9c102_i2c_write(cam, 0x10, 0x06);
45         err += sn9c102_i2c_write(cam, 0x11, 0x06);
46         err += sn9c102_i2c_write(cam, 0x12, 0x00);
47         err += sn9c102_i2c_write(cam, 0x14, 0x02);
48         err += sn9c102_i2c_write(cam, 0x13, 0x01);
49
50         msleep(400);
51
52         return err;
53 }
54
55
56 static int pas106b_get_ctrl(struct sn9c102_device* cam, 
57                             struct v4l2_control* ctrl)
58 {
59         switch (ctrl->id) {
60         case V4L2_CID_EXPOSURE:
61                 {
62                         int r1 = sn9c102_i2c_read(cam, 0x03),
63                             r2 = sn9c102_i2c_read(cam, 0x04);
64                         if (r1 < 0 || r2 < 0)
65                                 return -EIO;
66                         ctrl->value = (r1 << 4) | (r2 & 0x0f);
67                 }
68                 return 0;
69         case V4L2_CID_RED_BALANCE:
70                 if ((ctrl->value = sn9c102_i2c_read(cam, 0x0c)) < 0)
71                         return -EIO;
72                 ctrl->value &= 0x1f;
73                 return 0;
74         case V4L2_CID_BLUE_BALANCE:
75                 if ((ctrl->value = sn9c102_i2c_read(cam, 0x09)) < 0)
76                         return -EIO;
77                 ctrl->value &= 0x1f;
78                 return 0;
79         case V4L2_CID_GAIN:
80                 if ((ctrl->value = sn9c102_i2c_read(cam, 0x0e)) < 0)
81                         return -EIO;
82                 ctrl->value &= 0x1f;
83                 return 0;
84         case V4L2_CID_CONTRAST:
85                 if ((ctrl->value = sn9c102_i2c_read(cam, 0x0f)) < 0)
86                         return -EIO;
87                 ctrl->value &= 0x07;
88                 return 0;
89         case SN9C102_V4L2_CID_GREEN_BALANCE:
90                 if ((ctrl->value = sn9c102_i2c_read(cam, 0x0a)) < 0)
91                         return -EIO;
92                 ctrl->value = (ctrl->value & 0x1f) << 1;
93                 return 0;
94         case SN9C102_V4L2_CID_DAC_MAGNITUDE:
95                 if ((ctrl->value = sn9c102_i2c_read(cam, 0x08)) < 0)
96                         return -EIO;
97                 ctrl->value &= 0xf8;
98                 return 0;
99         case SN9C102_V4L2_CID_DAC_SIGN:
100                 if ((ctrl->value = sn9c102_i2c_read(cam, 0x07)) < 0)
101                         return -EIO;
102                 ctrl->value &= 0x01;
103                 return 0;
104         default:
105                 return -EINVAL;
106         }
107 }
108
109
110 static int pas106b_set_ctrl(struct sn9c102_device* cam, 
111                             const struct v4l2_control* ctrl)
112 {
113         int err = 0;
114
115         switch (ctrl->id) {
116         case V4L2_CID_EXPOSURE:
117                 err += sn9c102_i2c_write(cam, 0x03, ctrl->value >> 4);
118                 err += sn9c102_i2c_write(cam, 0x04, ctrl->value & 0x0f);
119                 break;
120         case V4L2_CID_RED_BALANCE:
121                 err += sn9c102_i2c_write(cam, 0x0c, ctrl->value);
122                 break;
123         case V4L2_CID_BLUE_BALANCE:
124                 err += sn9c102_i2c_write(cam, 0x09, ctrl->value);
125                 break;
126         case V4L2_CID_GAIN:
127                 err += sn9c102_i2c_write(cam, 0x0e, ctrl->value);
128                 break;
129         case V4L2_CID_CONTRAST:
130                 err += sn9c102_i2c_write(cam, 0x0f, ctrl->value);
131                 break;
132         case SN9C102_V4L2_CID_GREEN_BALANCE:
133                 err += sn9c102_i2c_write(cam, 0x0a, ctrl->value >> 1);
134                 err += sn9c102_i2c_write(cam, 0x0b, ctrl->value >> 1);
135                 break;
136         case SN9C102_V4L2_CID_DAC_MAGNITUDE:
137                 err += sn9c102_i2c_write(cam, 0x08, ctrl->value << 3);
138                 break;
139         case SN9C102_V4L2_CID_DAC_SIGN:
140                 {
141                         int r;
142                         err += (r = sn9c102_i2c_read(cam, 0x07)) < 0 ? r : 0;
143                         err += sn9c102_i2c_write(cam, 0x07, r | ctrl->value);
144                 }
145                 break;
146         default:
147                 return -EINVAL;
148         }
149         err += sn9c102_i2c_write(cam, 0x13, 0x01);
150
151         return err ? -EIO : 0;
152 }
153
154
155 static int pas106b_set_crop(struct sn9c102_device* cam, 
156                             const struct v4l2_rect* rect)
157 {
158         struct sn9c102_sensor* s = &pas106b;
159         int err = 0;
160         u8 h_start = (u8)(rect->left - s->cropcap.bounds.left) + 4,
161            v_start = (u8)(rect->top - s->cropcap.bounds.top) + 3;
162
163         err += sn9c102_write_reg(cam, h_start, 0x12);
164         err += sn9c102_write_reg(cam, v_start, 0x13);
165
166         return err;
167 }
168
169
170 static struct sn9c102_sensor pas106b = {
171         .name = "PAS106B",
172         .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>",
173         .frequency = SN9C102_I2C_400KHZ | SN9C102_I2C_100KHZ,
174         .interface = SN9C102_I2C_2WIRES,
175         .slave_read_id = 0x40,
176         .slave_write_id = 0x40,
177         .init = &pas106b_init,
178         .qctrl = {
179                 {
180                         .id = V4L2_CID_EXPOSURE,
181                         .type = V4L2_CTRL_TYPE_INTEGER,
182                         .name = "exposure",
183                         .minimum = 0x125,
184                         .maximum = 0xfff,
185                         .step = 0x01,
186                         .default_value = 0x140,
187                         .flags = 0,
188                 },
189                 {
190                         .id = V4L2_CID_GAIN,
191                         .type = V4L2_CTRL_TYPE_INTEGER,
192                         .name = "global gain",
193                         .minimum = 0x00,
194                         .maximum = 0x1f,
195                         .step = 0x01,
196                         .default_value = 0x0d,
197                         .flags = 0,
198                 },
199                 {
200                         .id = V4L2_CID_CONTRAST,
201                         .type = V4L2_CTRL_TYPE_INTEGER,
202                         .name = "contrast",
203                         .minimum = 0x00,
204                         .maximum = 0x07,
205                         .step = 0x01,
206                         .default_value = 0x00, /* 0x00~0x03 have same effect */
207                         .flags = 0,
208                 },
209                 {
210                         .id = V4L2_CID_RED_BALANCE,
211                         .type = V4L2_CTRL_TYPE_INTEGER,
212                         .name = "red balance",
213                         .minimum = 0x00,
214                         .maximum = 0x1f,
215                         .step = 0x01,
216                         .default_value = 0x04,
217                         .flags = 0,
218                 },
219                 {
220                         .id = V4L2_CID_BLUE_BALANCE,
221                         .type = V4L2_CTRL_TYPE_INTEGER,
222                         .name = "blue balance",
223                         .minimum = 0x00,
224                         .maximum = 0x1f,
225                         .step = 0x01,
226                         .default_value = 0x06,
227                         .flags = 0,
228                 },
229                 {
230                         .id = SN9C102_V4L2_CID_GREEN_BALANCE,
231                         .type = V4L2_CTRL_TYPE_INTEGER,
232                         .name = "green balance",
233                         .minimum = 0x00,
234                         .maximum = 0x3e,
235                         .step = 0x02,
236                         .default_value = 0x02,
237                         .flags = 0,
238                 },
239                 {
240                         .id = SN9C102_V4L2_CID_DAC_MAGNITUDE,
241                         .type = V4L2_CTRL_TYPE_INTEGER,
242                         .name = "DAC magnitude",
243                         .minimum = 0x00,
244                         .maximum = 0x1f,
245                         .step = 0x01,
246                         .default_value = 0x01,
247                         .flags = 0,
248                 },
249                 {
250                         .id = SN9C102_V4L2_CID_DAC_SIGN,
251                         .type = V4L2_CTRL_TYPE_BOOLEAN,
252                         .name = "DAC sign",
253                         .minimum = 0x00,
254                         .maximum = 0x01,
255                         .step = 0x01,
256                         .default_value = 0x00,
257                         .flags = 0,
258                 },
259         },
260         .get_ctrl = &pas106b_get_ctrl,
261         .set_ctrl = &pas106b_set_ctrl,
262         .cropcap = {
263                 .bounds = {
264                         .left = 0,
265                         .top = 0,
266                         .width = 352,
267                         .height = 288,
268                 },
269                 .defrect = {
270                         .left = 0,
271                         .top = 0,
272                         .width = 352,
273                         .height = 288,
274                 },
275         },
276         .set_crop = &pas106b_set_crop,
277         .pix_format = {
278                 .width = 352,
279                 .height = 288,
280                 .pixelformat = V4L2_PIX_FMT_SBGGR8,
281                 .priv = 8, /* we use this field as 'bits per pixel' */
282         }
283 };
284
285
286 int sn9c102_probe_pas106b(struct sn9c102_device* cam)
287 {
288         int r0 = 0, r1 = 0, err = 0;
289         unsigned int pid = 0;
290
291         /*
292            Minimal initialization to enable the I2C communication
293            NOTE: do NOT change the values!
294         */
295         err += sn9c102_write_reg(cam, 0x01, 0x01); /* sensor power down */
296         err += sn9c102_write_reg(cam, 0x00, 0x01); /* sensor power on */
297         err += sn9c102_write_reg(cam, 0x28, 0x17); /* sensor clock at 24 MHz */
298         if (err)
299                 return -EIO;
300
301         r0 = sn9c102_i2c_try_read(cam, &pas106b, 0x00);
302         r1 = sn9c102_i2c_try_read(cam, &pas106b, 0x01);
303
304         if (r0 < 0 || r1 < 0)
305                 return -EIO;
306
307         pid = (r0 << 11) | ((r1 & 0xf0) >> 4);
308         if (pid != 0x007)
309                 return -ENODEV;
310
311         sn9c102_attach_sensor(cam, &pas106b);
312
313         return 0;
314 }