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