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