Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/shaggy...
[linux-2.6] / drivers / media / video / sn9c102 / sn9c102_pas106b.c
1 /***************************************************************************
2  * Plug-in for PAS106B image sensor connected to the SN9C1xx PC Camera     *
3  * Controllers                                                             *
4  *                                                                         *
5  * Copyright (C) 2004-2007 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         default:
100                 return -EINVAL;
101         }
102 }
103
104
105 static int pas106b_set_ctrl(struct sn9c102_device* cam,
106                             const struct v4l2_control* ctrl)
107 {
108         int err = 0;
109
110         switch (ctrl->id) {
111         case V4L2_CID_EXPOSURE:
112                 err += sn9c102_i2c_write(cam, 0x03, ctrl->value >> 4);
113                 err += sn9c102_i2c_write(cam, 0x04, ctrl->value & 0x0f);
114                 break;
115         case V4L2_CID_RED_BALANCE:
116                 err += sn9c102_i2c_write(cam, 0x0c, ctrl->value);
117                 break;
118         case V4L2_CID_BLUE_BALANCE:
119                 err += sn9c102_i2c_write(cam, 0x09, ctrl->value);
120                 break;
121         case V4L2_CID_GAIN:
122                 err += sn9c102_i2c_write(cam, 0x0e, ctrl->value);
123                 break;
124         case V4L2_CID_CONTRAST:
125                 err += sn9c102_i2c_write(cam, 0x0f, ctrl->value);
126                 break;
127         case SN9C102_V4L2_CID_GREEN_BALANCE:
128                 err += sn9c102_i2c_write(cam, 0x0a, ctrl->value >> 1);
129                 err += sn9c102_i2c_write(cam, 0x0b, ctrl->value >> 1);
130                 break;
131         case SN9C102_V4L2_CID_DAC_MAGNITUDE:
132                 err += sn9c102_i2c_write(cam, 0x08, ctrl->value << 3);
133                 break;
134         default:
135                 return -EINVAL;
136         }
137         err += sn9c102_i2c_write(cam, 0x13, 0x01);
138
139         return err ? -EIO : 0;
140 }
141
142
143 static int pas106b_set_crop(struct sn9c102_device* cam,
144                             const struct v4l2_rect* rect)
145 {
146         struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
147         int err = 0;
148         u8 h_start = (u8)(rect->left - s->cropcap.bounds.left) + 4,
149            v_start = (u8)(rect->top - s->cropcap.bounds.top) + 3;
150
151         err += sn9c102_write_reg(cam, h_start, 0x12);
152         err += sn9c102_write_reg(cam, v_start, 0x13);
153
154         return err;
155 }
156
157
158 static int pas106b_set_pix_format(struct sn9c102_device* cam,
159                                   const struct v4l2_pix_format* pix)
160 {
161         int err = 0;
162
163         if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
164                 err += sn9c102_write_reg(cam, 0x2c, 0x17);
165         else
166                 err += sn9c102_write_reg(cam, 0x20, 0x17);
167
168         return err;
169 }
170
171
172 static struct sn9c102_sensor pas106b = {
173         .name = "PAS106B",
174         .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>",
175         .supported_bridge = BRIDGE_SN9C101 | BRIDGE_SN9C102 | BRIDGE_SN9C103,
176         .sysfs_ops = SN9C102_I2C_READ | SN9C102_I2C_WRITE,
177         .frequency = SN9C102_I2C_400KHZ | SN9C102_I2C_100KHZ,
178         .interface = SN9C102_I2C_2WIRES,
179         .i2c_slave_id = 0x40,
180         .init = &pas106b_init,
181         .qctrl = {
182                 {
183                         .id = V4L2_CID_EXPOSURE,
184                         .type = V4L2_CTRL_TYPE_INTEGER,
185                         .name = "exposure",
186                         .minimum = 0x125,
187                         .maximum = 0xfff,
188                         .step = 0x001,
189                         .default_value = 0x140,
190                         .flags = 0,
191                 },
192                 {
193                         .id = V4L2_CID_GAIN,
194                         .type = V4L2_CTRL_TYPE_INTEGER,
195                         .name = "global gain",
196                         .minimum = 0x00,
197                         .maximum = 0x1f,
198                         .step = 0x01,
199                         .default_value = 0x0d,
200                         .flags = 0,
201                 },
202                 {
203                         .id = V4L2_CID_CONTRAST,
204                         .type = V4L2_CTRL_TYPE_INTEGER,
205                         .name = "contrast",
206                         .minimum = 0x00,
207                         .maximum = 0x07,
208                         .step = 0x01,
209                         .default_value = 0x00, /* 0x00~0x03 have same effect */
210                         .flags = 0,
211                 },
212                 {
213                         .id = V4L2_CID_RED_BALANCE,
214                         .type = V4L2_CTRL_TYPE_INTEGER,
215                         .name = "red balance",
216                         .minimum = 0x00,
217                         .maximum = 0x1f,
218                         .step = 0x01,
219                         .default_value = 0x04,
220                         .flags = 0,
221                 },
222                 {
223                         .id = V4L2_CID_BLUE_BALANCE,
224                         .type = V4L2_CTRL_TYPE_INTEGER,
225                         .name = "blue balance",
226                         .minimum = 0x00,
227                         .maximum = 0x1f,
228                         .step = 0x01,
229                         .default_value = 0x06,
230                         .flags = 0,
231                 },
232                 {
233                         .id = SN9C102_V4L2_CID_GREEN_BALANCE,
234                         .type = V4L2_CTRL_TYPE_INTEGER,
235                         .name = "green balance",
236                         .minimum = 0x00,
237                         .maximum = 0x3e,
238                         .step = 0x02,
239                         .default_value = 0x02,
240                         .flags = 0,
241                 },
242                 {
243                         .id = SN9C102_V4L2_CID_DAC_MAGNITUDE,
244                         .type = V4L2_CTRL_TYPE_INTEGER,
245                         .name = "DAC magnitude",
246                         .minimum = 0x00,
247                         .maximum = 0x1f,
248                         .step = 0x01,
249                         .default_value = 0x01,
250                         .flags = 0,
251                 },
252         },
253         .get_ctrl = &pas106b_get_ctrl,
254         .set_ctrl = &pas106b_set_ctrl,
255         .cropcap = {
256                 .bounds = {
257                         .left = 0,
258                         .top = 0,
259                         .width = 352,
260                         .height = 288,
261                 },
262                 .defrect = {
263                         .left = 0,
264                         .top = 0,
265                         .width = 352,
266                         .height = 288,
267                 },
268         },
269         .set_crop = &pas106b_set_crop,
270         .pix_format = {
271                 .width = 352,
272                 .height = 288,
273                 .pixelformat = V4L2_PIX_FMT_SBGGR8,
274                 .priv = 8, /* we use this field as 'bits per pixel' */
275         },
276         .set_pix_format = &pas106b_set_pix_format
277 };
278
279
280 int sn9c102_probe_pas106b(struct sn9c102_device* cam)
281 {
282         int r0 = 0, r1 = 0, err = 0;
283         unsigned int pid = 0;
284
285         /*
286            Minimal initialization to enable the I2C communication
287            NOTE: do NOT change the values!
288         */
289         err += sn9c102_write_reg(cam, 0x01, 0x01); /* sensor power down */
290         err += sn9c102_write_reg(cam, 0x00, 0x01); /* sensor power on */
291         err += sn9c102_write_reg(cam, 0x28, 0x17); /* sensor clock at 24 MHz */
292         if (err)
293                 return -EIO;
294
295         r0 = sn9c102_i2c_try_read(cam, &pas106b, 0x00);
296         r1 = sn9c102_i2c_try_read(cam, &pas106b, 0x01);
297
298         if (r0 < 0 || r1 < 0)
299                 return -EIO;
300
301         pid = (r0 << 11) | ((r1 & 0xf0) >> 4);
302         if (pid != 0x007)
303                 return -ENODEV;
304
305         sn9c102_attach_sensor(cam, &pas106b);
306
307         return 0;
308 }