Merge branch 'linux-next' of git://git.infradead.org/ubifs-2.6
[linux-2.6] / drivers / media / video / gspca / stv06xx / stv06xx_vv6410.c
1 /*
2  * Copyright (c) 2001 Jean-Fredric Clere, Nikolas Zimmermann, Georg Acher
3  *                    Mark Cave-Ayland, Carlo E Prelz, Dick Streefland
4  * Copyright (c) 2002, 2003 Tuukka Toivonen
5  * Copyright (c) 2008 Erik AndrĂ©n
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  * P/N 861037:      Sensor HDCS1000        ASIC STV0600
22  * P/N 861050-0010: Sensor HDCS1000        ASIC STV0600
23  * P/N 861050-0020: Sensor Photobit PB100  ASIC STV0600-1 - QuickCam Express
24  * P/N 861055:      Sensor ST VV6410       ASIC STV0610   - LEGO cam
25  * P/N 861075-0040: Sensor HDCS1000        ASIC
26  * P/N 961179-0700: Sensor ST VV6410       ASIC STV0602   - Dexxa WebCam USB
27  * P/N 861040-0000: Sensor ST VV6410       ASIC STV0610   - QuickCam Web
28  */
29
30 #include "stv06xx_vv6410.h"
31
32 static struct v4l2_pix_format vv6410_mode[] = {
33         {
34                 356,
35                 292,
36                 V4L2_PIX_FMT_SGRBG8,
37                 V4L2_FIELD_NONE,
38                 .sizeimage = 356 * 292,
39                 .bytesperline = 356,
40                 .colorspace = V4L2_COLORSPACE_SRGB,
41                 .priv = 0
42         }
43 };
44
45 static const struct ctrl vv6410_ctrl[] = {
46 #define HFLIP_IDX 0
47         {
48                 {
49                         .id             = V4L2_CID_HFLIP,
50                         .type           = V4L2_CTRL_TYPE_BOOLEAN,
51                         .name           = "horizontal flip",
52                         .minimum        = 0,
53                         .maximum        = 1,
54                         .step           = 1,
55                         .default_value  = 0
56                 },
57                 .set = vv6410_set_hflip,
58                 .get = vv6410_get_hflip
59         },
60 #define VFLIP_IDX 1
61         {
62                 {
63                         .id             = V4L2_CID_VFLIP,
64                         .type           = V4L2_CTRL_TYPE_BOOLEAN,
65                         .name           = "vertical flip",
66                         .minimum        = 0,
67                         .maximum        = 1,
68                         .step           = 1,
69                         .default_value  = 0
70                 },
71                 .set = vv6410_set_vflip,
72                 .get = vv6410_get_vflip
73         },
74 #define GAIN_IDX 2
75         {
76                 {
77                         .id             = V4L2_CID_GAIN,
78                         .type           = V4L2_CTRL_TYPE_INTEGER,
79                         .name           = "analog gain",
80                         .minimum        = 0,
81                         .maximum        = 15,
82                         .step           = 1,
83                         .default_value  = 0
84                 },
85                 .set = vv6410_set_analog_gain,
86                 .get = vv6410_get_analog_gain
87         }
88 };
89
90 static int vv6410_probe(struct sd *sd)
91 {
92         u16 data;
93         int err, i;
94         s32 *sensor_settings;
95
96         err = stv06xx_read_sensor(sd, VV6410_DEVICEH, &data);
97         if (err < 0)
98                 return -ENODEV;
99
100         if (data == 0x19) {
101                 info("vv6410 sensor detected");
102
103                 sensor_settings = kmalloc(ARRAY_SIZE(vv6410_ctrl) * sizeof(s32),
104                                           GFP_KERNEL);
105                 if (!sensor_settings)
106                         return -ENOMEM;
107
108                 sd->gspca_dev.cam.cam_mode = vv6410_mode;
109                 sd->gspca_dev.cam.nmodes = ARRAY_SIZE(vv6410_mode);
110                 sd->desc.ctrls = vv6410_ctrl;
111                 sd->desc.nctrls = ARRAY_SIZE(vv6410_ctrl);
112
113                 for (i = 0; i < sd->desc.nctrls; i++)
114                         sensor_settings[i] = vv6410_ctrl[i].qctrl.default_value;
115                 sd->sensor_priv = sensor_settings;
116                 return 0;
117         }
118         return -ENODEV;
119 }
120
121 static int vv6410_init(struct sd *sd)
122 {
123         int err = 0, i;
124
125         for (i = 0; i < ARRAY_SIZE(stv_bridge_init); i++) {
126                 /* if NULL then len contains single value */
127                 if (stv_bridge_init[i].data == NULL) {
128                         err = stv06xx_write_bridge(sd,
129                                 stv_bridge_init[i].start,
130                                 stv_bridge_init[i].len);
131                 } else {
132                         int j;
133                         for (j = 0; j < stv_bridge_init[i].len; j++)
134                                 err = stv06xx_write_bridge(sd,
135                                         stv_bridge_init[i].start + j,
136                                         stv_bridge_init[i].data[j]);
137                 }
138         }
139
140         if (err < 0)
141                 return err;
142
143         err = stv06xx_write_sensor_bytes(sd, (u8 *) vv6410_sensor_init,
144                                          ARRAY_SIZE(vv6410_sensor_init));
145
146         return (err < 0) ? err : 0;
147 }
148
149 static void vv6410_disconnect(struct sd *sd)
150 {
151         sd->sensor = NULL;
152         kfree(sd->sensor_priv);
153 }
154
155 static int vv6410_start(struct sd *sd)
156 {
157         int err;
158         struct cam *cam = &sd->gspca_dev.cam;
159         u32 priv = cam->cam_mode[sd->gspca_dev.curr_mode].priv;
160
161         if (priv & VV6410_CROP_TO_QVGA) {
162                 PDEBUG(D_CONF, "Cropping to QVGA");
163                 stv06xx_write_sensor(sd, VV6410_XENDH, 320 - 1);
164                 stv06xx_write_sensor(sd, VV6410_YENDH, 240 - 1);
165         } else {
166                 stv06xx_write_sensor(sd, VV6410_XENDH, 360 - 1);
167                 stv06xx_write_sensor(sd, VV6410_YENDH, 294 - 1);
168         }
169
170         if (priv & VV6410_SUBSAMPLE) {
171                 PDEBUG(D_CONF, "Enabling subsampling");
172                 stv06xx_write_bridge(sd, STV_Y_CTRL, 0x02);
173                 stv06xx_write_bridge(sd, STV_X_CTRL, 0x06);
174
175                 stv06xx_write_bridge(sd, STV_SCAN_RATE, 0x10);
176         } else {
177                 stv06xx_write_bridge(sd, STV_Y_CTRL, 0x01);
178                 stv06xx_write_bridge(sd, STV_X_CTRL, 0x0a);
179
180                 stv06xx_write_bridge(sd, STV_SCAN_RATE, 0x20);
181         }
182
183         /* Turn on LED */
184         err = stv06xx_write_bridge(sd, STV_LED_CTRL, LED_ON);
185         if (err < 0)
186                 return err;
187
188         err = stv06xx_write_sensor(sd, VV6410_SETUP0, 0);
189         if (err < 0)
190                 return err;
191
192         PDEBUG(D_STREAM, "Starting stream");
193
194         return 0;
195 }
196
197 static int vv6410_stop(struct sd *sd)
198 {
199         int err;
200
201         /* Turn off LED */
202         err = stv06xx_write_bridge(sd, STV_LED_CTRL, LED_OFF);
203         if (err < 0)
204                 return err;
205
206         err = stv06xx_write_sensor(sd, VV6410_SETUP0, VV6410_LOW_POWER_MODE);
207         if (err < 0)
208                 return err;
209
210         PDEBUG(D_STREAM, "Halting stream");
211
212         return (err < 0) ? err : 0;
213 }
214
215 static int vv6410_dump(struct sd *sd)
216 {
217         u8 i;
218         int err = 0;
219
220         info("Dumping all vv6410 sensor registers");
221         for (i = 0; i < 0xff && !err; i++) {
222                 u16 data;
223                 err = stv06xx_read_sensor(sd, i, &data);
224                 info("Register 0x%x contained 0x%x", i, data);
225         }
226         return (err < 0) ? err : 0;
227 }
228
229 static int vv6410_get_hflip(struct gspca_dev *gspca_dev, __s32 *val)
230 {
231         struct sd *sd = (struct sd *) gspca_dev;
232         s32 *sensor_settings = sd->sensor_priv;
233
234         *val = sensor_settings[HFLIP_IDX];
235         PDEBUG(D_V4L2, "Read horizontal flip %d", *val);
236
237         return 0;
238 }
239
240 static int vv6410_set_hflip(struct gspca_dev *gspca_dev, __s32 val)
241 {
242         int err;
243         u16 i2c_data;
244         struct sd *sd = (struct sd *) gspca_dev;
245         s32 *sensor_settings = sd->sensor_priv;
246
247         sensor_settings[HFLIP_IDX] = val;
248         err = stv06xx_read_sensor(sd, VV6410_DATAFORMAT, &i2c_data);
249         if (err < 0)
250                 return err;
251
252         if (val)
253                 i2c_data |= VV6410_HFLIP;
254         else
255                 i2c_data &= ~VV6410_HFLIP;
256
257         PDEBUG(D_V4L2, "Set horizontal flip to %d", val);
258         err = stv06xx_write_sensor(sd, VV6410_DATAFORMAT, i2c_data);
259
260         return (err < 0) ? err : 0;
261 }
262
263 static int vv6410_get_vflip(struct gspca_dev *gspca_dev, __s32 *val)
264 {
265         struct sd *sd = (struct sd *) gspca_dev;
266         s32 *sensor_settings = sd->sensor_priv;
267
268         *val = sensor_settings[VFLIP_IDX];
269         PDEBUG(D_V4L2, "Read vertical flip %d", *val);
270
271         return 0;
272 }
273
274 static int vv6410_set_vflip(struct gspca_dev *gspca_dev, __s32 val)
275 {
276         int err;
277         u16 i2c_data;
278         struct sd *sd = (struct sd *) gspca_dev;
279         s32 *sensor_settings = sd->sensor_priv;
280
281         sensor_settings[VFLIP_IDX] = val;
282         err = stv06xx_read_sensor(sd, VV6410_DATAFORMAT, &i2c_data);
283         if (err < 0)
284                 return err;
285
286         if (val)
287                 i2c_data |= VV6410_VFLIP;
288         else
289                 i2c_data &= ~VV6410_VFLIP;
290
291         PDEBUG(D_V4L2, "Set vertical flip to %d", val);
292         err = stv06xx_write_sensor(sd, VV6410_DATAFORMAT, i2c_data);
293
294         return (err < 0) ? err : 0;
295 }
296
297 static int vv6410_get_analog_gain(struct gspca_dev *gspca_dev, __s32 *val)
298 {
299         struct sd *sd = (struct sd *) gspca_dev;
300         s32 *sensor_settings = sd->sensor_priv;
301
302         *val = sensor_settings[GAIN_IDX];
303
304         PDEBUG(D_V4L2, "Read analog gain %d", *val);
305
306         return 0;
307 }
308
309 static int vv6410_set_analog_gain(struct gspca_dev *gspca_dev, __s32 val)
310 {
311         int err;
312         struct sd *sd = (struct sd *) gspca_dev;
313         s32 *sensor_settings = sd->sensor_priv;
314
315         sensor_settings[GAIN_IDX] = val;
316         PDEBUG(D_V4L2, "Set analog gain to %d", val);
317         err = stv06xx_write_sensor(sd, VV6410_ANALOGGAIN, 0xf0 | (val & 0xf));
318
319         return (err < 0) ? err : 0;
320 }