V4L/DVB (5474): SN9C1xx driver updates
[linux-2.6.git] / 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 int pas106b_init(struct sn9c102_device* cam)
27 {
28         int err = 0;
29
30         err += sn9c102_write_reg(cam, 0x00, 0x10);
31         err += sn9c102_write_reg(cam, 0x00, 0x11);
32         err += sn9c102_write_reg(cam, 0x00, 0x14);
33         err += sn9c102_write_reg(cam, 0x20, 0x17);
34         err += sn9c102_write_reg(cam, 0x20, 0x19);
35         err += sn9c102_write_reg(cam, 0x09, 0x18);
36
37         err += sn9c102_i2c_write(cam, 0x02, 0x0c);
38         err += sn9c102_i2c_write(cam, 0x05, 0x5a);
39         err += sn9c102_i2c_write(cam, 0x06, 0x88);
40         err += sn9c102_i2c_write(cam, 0x07, 0x80);
41         err += sn9c102_i2c_write(cam, 0x10, 0x06);
42         err += sn9c102_i2c_write(cam, 0x11, 0x06);
43         err += sn9c102_i2c_write(cam, 0x12, 0x00);
44         err += sn9c102_i2c_write(cam, 0x14, 0x02);
45         err += sn9c102_i2c_write(cam, 0x13, 0x01);
46
47         msleep(400);
48
49         return err;
50 }
51
52
53 static int pas106b_get_ctrl(struct sn9c102_device* cam,
54                             struct v4l2_control* ctrl)
55 {
56         switch (ctrl->id) {
57         case V4L2_CID_EXPOSURE:
58                 {
59                         int r1 = sn9c102_i2c_read(cam, 0x03),
60                             r2 = sn9c102_i2c_read(cam, 0x04);
61                         if (r1 < 0 || r2 < 0)
62                                 return -EIO;
63                         ctrl->value = (r1 << 4) | (r2 & 0x0f);
64                 }
65                 return 0;
66         case V4L2_CID_RED_BALANCE:
67                 if ((ctrl->value = sn9c102_i2c_read(cam, 0x0c)) < 0)
68                         return -EIO;
69                 ctrl->value &= 0x1f;
70                 return 0;
71         case V4L2_CID_BLUE_BALANCE:
72                 if ((ctrl->value = sn9c102_i2c_read(cam, 0x09)) < 0)
73                         return -EIO;
74                 ctrl->value &= 0x1f;
75                 return 0;
76         case V4L2_CID_GAIN:
77                 if ((ctrl->value = sn9c102_i2c_read(cam, 0x0e)) < 0)
78                         return -EIO;
79                 ctrl->value &= 0x1f;
80                 return 0;
81         case V4L2_CID_CONTRAST:
82                 if ((ctrl->value = sn9c102_i2c_read(cam, 0x0f)) < 0)
83                         return -EIO;
84                 ctrl->value &= 0x07;
85                 return 0;
86         case SN9C102_V4L2_CID_GREEN_BALANCE:
87                 if ((ctrl->value = sn9c102_i2c_read(cam, 0x0a)) < 0)
88                         return -EIO;
89                 ctrl->value = (ctrl->value & 0x1f) << 1;
90                 return 0;
91         case SN9C102_V4L2_CID_DAC_MAGNITUDE:
92                 if ((ctrl->value = sn9c102_i2c_read(cam, 0x08)) < 0)
93                         return -EIO;
94                 ctrl->value &= 0xf8;
95                 return 0;
96         default:
97                 return -EINVAL;
98         }
99 }
100
101
102 static int pas106b_set_ctrl(struct sn9c102_device* cam,
103                             const struct v4l2_control* ctrl)
104 {
105         int err = 0;
106
107         switch (ctrl->id) {
108         case V4L2_CID_EXPOSURE:
109                 err += sn9c102_i2c_write(cam, 0x03, ctrl->value >> 4);
110                 err += sn9c102_i2c_write(cam, 0x04, ctrl->value & 0x0f);
111                 break;
112         case V4L2_CID_RED_BALANCE:
113                 err += sn9c102_i2c_write(cam, 0x0c, ctrl->value);
114                 break;
115         case V4L2_CID_BLUE_BALANCE:
116                 err += sn9c102_i2c_write(cam, 0x09, ctrl->value);
117                 break;
118         case V4L2_CID_GAIN:
119                 err += sn9c102_i2c_write(cam, 0x0e, ctrl->value);
120                 break;
121         case V4L2_CID_CONTRAST:
122                 err += sn9c102_i2c_write(cam, 0x0f, ctrl->value);
123                 break;
124         case SN9C102_V4L2_CID_GREEN_BALANCE:
125                 err += sn9c102_i2c_write(cam, 0x0a, ctrl->value >> 1);
126                 err += sn9c102_i2c_write(cam, 0x0b, ctrl->value >> 1);
127                 break;
128         case SN9C102_V4L2_CID_DAC_MAGNITUDE:
129                 err += sn9c102_i2c_write(cam, 0x08, ctrl->value << 3);
130                 break;
131         default:
132                 return -EINVAL;
133         }
134         err += sn9c102_i2c_write(cam, 0x13, 0x01);
135
136         return err ? -EIO : 0;
137 }
138
139
140 static int pas106b_set_crop(struct sn9c102_device* cam,
141                             const struct v4l2_rect* rect)
142 {
143         struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
144         int err = 0;
145         u8 h_start = (u8)(rect->left - s->cropcap.bounds.left) + 4,
146            v_start = (u8)(rect->top - s->cropcap.bounds.top) + 3;
147
148         err += sn9c102_write_reg(cam, h_start, 0x12);
149         err += sn9c102_write_reg(cam, v_start, 0x13);
150
151         return err;
152 }
153
154
155 static int pas106b_set_pix_format(struct sn9c102_device* cam,
156                                   const struct v4l2_pix_format* pix)
157 {
158         int err = 0;
159
160         if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
161                 err += sn9c102_write_reg(cam, 0x2c, 0x17);
162         else
163                 err += sn9c102_write_reg(cam, 0x20, 0x17);
164
165         return err;
166 }
167
168
169 static struct sn9c102_sensor pas106b = {
170         .name = "PAS106B",
171         .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>",
172         .supported_bridge = BRIDGE_SN9C101 | BRIDGE_SN9C102,
173         .sysfs_ops = SN9C102_I2C_READ | SN9C102_I2C_WRITE,
174         .frequency = SN9C102_I2C_400KHZ | SN9C102_I2C_100KHZ,
175         .interface = SN9C102_I2C_2WIRES,
176         .i2c_slave_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 = 0x001,
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         .get_ctrl = &pas106b_get_ctrl,
251         .set_ctrl = &pas106b_set_ctrl,
252         .cropcap = {
253                 .bounds = {
254                         .left = 0,
255                         .top = 0,
256                         .width = 352,
257                         .height = 288,
258                 },
259                 .defrect = {
260                         .left = 0,
261                         .top = 0,
262                         .width = 352,
263                         .height = 288,
264                 },
265         },
266         .set_crop = &pas106b_set_crop,
267         .pix_format = {
268                 .width = 352,
269                 .height = 288,
270                 .pixelformat = V4L2_PIX_FMT_SBGGR8,
271                 .priv = 8, /* we use this field as 'bits per pixel' */
272         },
273         .set_pix_format = &pas106b_set_pix_format
274 };
275
276
277 int sn9c102_probe_pas106b(struct sn9c102_device* cam)
278 {
279         int r0 = 0, r1 = 0, err = 0;
280         unsigned int pid = 0;
281
282         /*
283            Minimal initialization to enable the I2C communication
284            NOTE: do NOT change the values!
285         */
286         err += sn9c102_write_reg(cam, 0x01, 0x01); /* sensor power down */
287         err += sn9c102_write_reg(cam, 0x00, 0x01); /* sensor power on */
288         err += sn9c102_write_reg(cam, 0x28, 0x17); /* sensor clock at 24 MHz */
289         if (err)
290                 return -EIO;
291
292         r0 = sn9c102_i2c_try_read(cam, &pas106b, 0x00);
293         r1 = sn9c102_i2c_try_read(cam, &pas106b, 0x01);
294
295         if (r0 < 0 || r1 < 0)
296                 return -EIO;
297
298         pid = (r0 << 11) | ((r1 & 0xf0) >> 4);
299         if (pid != 0x007)
300                 return -ENODEV;
301
302         sn9c102_attach_sensor(cam, &pas106b);
303
304         return 0;
305 }