V4L/DVB (5474): SN9C1xx driver updates
[safe/jmp/linux-2.6] / drivers / media / video / sn9c102 / sn9c102_hv7131d.c
1 /***************************************************************************
2  * Plug-in for HV7131D 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 "sn9c102_sensor.h"
23
24
25 static int hv7131d_init(struct sn9c102_device* cam)
26 {
27         int err = 0;
28
29         err += sn9c102_write_reg(cam, 0x00, 0x10);
30         err += sn9c102_write_reg(cam, 0x00, 0x11);
31         err += sn9c102_write_reg(cam, 0x00, 0x14);
32         err += sn9c102_write_reg(cam, 0x60, 0x17);
33         err += sn9c102_write_reg(cam, 0x0e, 0x18);
34         err += sn9c102_write_reg(cam, 0xf2, 0x19);
35
36         err += sn9c102_i2c_write(cam, 0x01, 0x04);
37         err += sn9c102_i2c_write(cam, 0x02, 0x00);
38         err += sn9c102_i2c_write(cam, 0x28, 0x00);
39
40         return err;
41 }
42
43
44 static int hv7131d_get_ctrl(struct sn9c102_device* cam,
45                             struct v4l2_control* ctrl)
46 {
47         switch (ctrl->id) {
48         case V4L2_CID_EXPOSURE:
49                 {
50                         int r1 = sn9c102_i2c_read(cam, 0x26),
51                             r2 = sn9c102_i2c_read(cam, 0x27);
52                         if (r1 < 0 || r2 < 0)
53                                 return -EIO;
54                         ctrl->value = (r1 << 8) | (r2 & 0xff);
55                 }
56                 return 0;
57         case V4L2_CID_RED_BALANCE:
58                 if ((ctrl->value = sn9c102_i2c_read(cam, 0x31)) < 0)
59                         return -EIO;
60                 ctrl->value = 0x3f - (ctrl->value & 0x3f);
61                 return 0;
62         case V4L2_CID_BLUE_BALANCE:
63                 if ((ctrl->value = sn9c102_i2c_read(cam, 0x33)) < 0)
64                         return -EIO;
65                 ctrl->value = 0x3f - (ctrl->value & 0x3f);
66                 return 0;
67         case SN9C102_V4L2_CID_GREEN_BALANCE:
68                 if ((ctrl->value = sn9c102_i2c_read(cam, 0x32)) < 0)
69                         return -EIO;
70                 ctrl->value = 0x3f - (ctrl->value & 0x3f);
71                 return 0;
72         case SN9C102_V4L2_CID_RESET_LEVEL:
73                 if ((ctrl->value = sn9c102_i2c_read(cam, 0x30)) < 0)
74                         return -EIO;
75                 ctrl->value &= 0x3f;
76                 return 0;
77         case SN9C102_V4L2_CID_PIXEL_BIAS_VOLTAGE:
78                 if ((ctrl->value = sn9c102_i2c_read(cam, 0x34)) < 0)
79                         return -EIO;
80                 ctrl->value &= 0x07;
81                 return 0;
82         default:
83                 return -EINVAL;
84         }
85 }
86
87
88 static int hv7131d_set_ctrl(struct sn9c102_device* cam,
89                             const struct v4l2_control* ctrl)
90 {
91         int err = 0;
92
93         switch (ctrl->id) {
94         case V4L2_CID_EXPOSURE:
95                 err += sn9c102_i2c_write(cam, 0x26, ctrl->value >> 8);
96                 err += sn9c102_i2c_write(cam, 0x27, ctrl->value & 0xff);
97                 break;
98         case V4L2_CID_RED_BALANCE:
99                 err += sn9c102_i2c_write(cam, 0x31, 0x3f - ctrl->value);
100                 break;
101         case V4L2_CID_BLUE_BALANCE:
102                 err += sn9c102_i2c_write(cam, 0x33, 0x3f - ctrl->value);
103                 break;
104         case SN9C102_V4L2_CID_GREEN_BALANCE:
105                 err += sn9c102_i2c_write(cam, 0x32, 0x3f - ctrl->value);
106                 break;
107         case SN9C102_V4L2_CID_RESET_LEVEL:
108                 err += sn9c102_i2c_write(cam, 0x30, ctrl->value);
109                 break;
110         case SN9C102_V4L2_CID_PIXEL_BIAS_VOLTAGE:
111                 err += sn9c102_i2c_write(cam, 0x34, ctrl->value);
112                 break;
113         default:
114                 return -EINVAL;
115         }
116
117         return err ? -EIO : 0;
118 }
119
120
121 static int hv7131d_set_crop(struct sn9c102_device* cam,
122                             const struct v4l2_rect* rect)
123 {
124         struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
125         int err = 0;
126         u8 h_start = (u8)(rect->left - s->cropcap.bounds.left) + 2,
127            v_start = (u8)(rect->top - s->cropcap.bounds.top) + 2;
128
129         err += sn9c102_write_reg(cam, h_start, 0x12);
130         err += sn9c102_write_reg(cam, v_start, 0x13);
131
132         return err;
133 }
134
135
136 static int hv7131d_set_pix_format(struct sn9c102_device* cam,
137                                   const struct v4l2_pix_format* pix)
138 {
139         int err = 0;
140
141         if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
142                 err += sn9c102_write_reg(cam, 0x42, 0x19);
143         else
144                 err += sn9c102_write_reg(cam, 0xf2, 0x19);
145
146         return err;
147 }
148
149
150 static struct sn9c102_sensor hv7131d = {
151         .name = "HV7131D",
152         .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>",
153         .supported_bridge = BRIDGE_SN9C101 | BRIDGE_SN9C102,
154         .sysfs_ops = SN9C102_I2C_READ | SN9C102_I2C_WRITE,
155         .frequency = SN9C102_I2C_100KHZ,
156         .interface = SN9C102_I2C_2WIRES,
157         .i2c_slave_id = 0x11,
158         .init = &hv7131d_init,
159         .qctrl = {
160                 {
161                         .id = V4L2_CID_EXPOSURE,
162                         .type = V4L2_CTRL_TYPE_INTEGER,
163                         .name = "exposure",
164                         .minimum = 0x0250,
165                         .maximum = 0xffff,
166                         .step = 0x0001,
167                         .default_value = 0x0250,
168                         .flags = 0,
169                 },
170                 {
171                         .id = V4L2_CID_RED_BALANCE,
172                         .type = V4L2_CTRL_TYPE_INTEGER,
173                         .name = "red balance",
174                         .minimum = 0x00,
175                         .maximum = 0x3f,
176                         .step = 0x01,
177                         .default_value = 0x00,
178                         .flags = 0,
179                 },
180                 {
181                         .id = V4L2_CID_BLUE_BALANCE,
182                         .type = V4L2_CTRL_TYPE_INTEGER,
183                         .name = "blue balance",
184                         .minimum = 0x00,
185                         .maximum = 0x3f,
186                         .step = 0x01,
187                         .default_value = 0x20,
188                         .flags = 0,
189                 },
190                 {
191                         .id = SN9C102_V4L2_CID_GREEN_BALANCE,
192                         .type = V4L2_CTRL_TYPE_INTEGER,
193                         .name = "green balance",
194                         .minimum = 0x00,
195                         .maximum = 0x3f,
196                         .step = 0x01,
197                         .default_value = 0x1e,
198                         .flags = 0,
199                 },
200                 {
201                         .id = SN9C102_V4L2_CID_RESET_LEVEL,
202                         .type = V4L2_CTRL_TYPE_INTEGER,
203                         .name = "reset level",
204                         .minimum = 0x19,
205                         .maximum = 0x3f,
206                         .step = 0x01,
207                         .default_value = 0x30,
208                         .flags = 0,
209                 },
210                 {
211                         .id = SN9C102_V4L2_CID_PIXEL_BIAS_VOLTAGE,
212                         .type = V4L2_CTRL_TYPE_INTEGER,
213                         .name = "pixel bias voltage",
214                         .minimum = 0x00,
215                         .maximum = 0x07,
216                         .step = 0x01,
217                         .default_value = 0x02,
218                         .flags = 0,
219                 },
220         },
221         .get_ctrl = &hv7131d_get_ctrl,
222         .set_ctrl = &hv7131d_set_ctrl,
223         .cropcap = {
224                 .bounds = {
225                         .left = 0,
226                         .top = 0,
227                         .width = 640,
228                         .height = 480,
229                 },
230                 .defrect = {
231                         .left = 0,
232                         .top = 0,
233                         .width = 640,
234                         .height = 480,
235                 },
236         },
237         .set_crop = &hv7131d_set_crop,
238         .pix_format = {
239                 .width = 640,
240                 .height = 480,
241                 .pixelformat = V4L2_PIX_FMT_SBGGR8,
242                 .priv = 8,
243         },
244         .set_pix_format = &hv7131d_set_pix_format
245 };
246
247
248 int sn9c102_probe_hv7131d(struct sn9c102_device* cam)
249 {
250         int r0 = 0, r1 = 0, err = 0;
251
252         err += sn9c102_write_reg(cam, 0x01, 0x01);
253         err += sn9c102_write_reg(cam, 0x00, 0x01);
254         err += sn9c102_write_reg(cam, 0x28, 0x17);
255         if (err)
256                 return -EIO;
257
258         r0 = sn9c102_i2c_try_read(cam, &hv7131d, 0x00);
259         r1 = sn9c102_i2c_try_read(cam, &hv7131d, 0x01);
260         if (r0 < 0 || r1 < 0)
261                 return -EIO;
262
263         if (r0 != 0x00 || r1 != 0x04)
264                 return -ENODEV;
265
266         sn9c102_attach_sensor(cam, &hv7131d);
267
268         return 0;
269 }