treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 157
[sfrench/cifs-2.6.git] / drivers / media / usb / gspca / stv06xx / stv06xx_pb0100.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2001 Jean-Fredric Clere, Nikolas Zimmermann, Georg Acher
4  *                    Mark Cave-Ayland, Carlo E Prelz, Dick Streefland
5  * Copyright (c) 2002, 2003 Tuukka Toivonen
6  * Copyright (c) 2008 Erik AndrĂ©n
7  *
8  * P/N 861037:      Sensor HDCS1000        ASIC STV0600
9  * P/N 861050-0010: Sensor HDCS1000        ASIC STV0600
10  * P/N 861050-0020: Sensor Photobit PB100  ASIC STV0600-1 - QuickCam Express
11  * P/N 861055:      Sensor ST VV6410       ASIC STV0610   - LEGO cam
12  * P/N 861075-0040: Sensor HDCS1000        ASIC
13  * P/N 961179-0700: Sensor ST VV6410       ASIC STV0602   - Dexxa WebCam USB
14  * P/N 861040-0000: Sensor ST VV6410       ASIC STV0610   - QuickCam Web
15  */
16
17 /*
18  * The spec file for the PB-0100 suggests the following for best quality
19  * images after the sensor has been reset :
20  *
21  * PB_ADCGAINL      = R60 = 0x03 (3 dec)      : sets low reference of ADC
22                                                 to produce good black level
23  * PB_PREADCTRL     = R32 = 0x1400 (5120 dec) : Enables global gain changes
24                                                 through R53
25  * PB_ADCMINGAIN    = R52 = 0x10 (16 dec)     : Sets the minimum gain for
26                                                 auto-exposure
27  * PB_ADCGLOBALGAIN = R53 = 0x10 (16 dec)     : Sets the global gain
28  * PB_EXPGAIN       = R14 = 0x11 (17 dec)     : Sets the auto-exposure value
29  * PB_UPDATEINT     = R23 = 0x02 (2 dec)      : Sets the speed on
30                                                 auto-exposure routine
31  * PB_CFILLIN       = R5  = 0x0E (14 dec)     : Sets the frame rate
32  */
33
34 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35
36 #include "stv06xx_pb0100.h"
37
38 struct pb0100_ctrls {
39         struct { /* one big happy control cluster... */
40                 struct v4l2_ctrl *autogain;
41                 struct v4l2_ctrl *gain;
42                 struct v4l2_ctrl *exposure;
43                 struct v4l2_ctrl *red;
44                 struct v4l2_ctrl *blue;
45                 struct v4l2_ctrl *natural;
46         };
47         struct v4l2_ctrl *target;
48 };
49
50 static struct v4l2_pix_format pb0100_mode[] = {
51 /* low res / subsample modes disabled as they are only half res horizontal,
52    halving the vertical resolution does not seem to work */
53         {
54                 320,
55                 240,
56                 V4L2_PIX_FMT_SGRBG8,
57                 V4L2_FIELD_NONE,
58                 .sizeimage = 320 * 240,
59                 .bytesperline = 320,
60                 .colorspace = V4L2_COLORSPACE_SRGB,
61                 .priv = PB0100_CROP_TO_VGA
62         },
63         {
64                 352,
65                 288,
66                 V4L2_PIX_FMT_SGRBG8,
67                 V4L2_FIELD_NONE,
68                 .sizeimage = 352 * 288,
69                 .bytesperline = 352,
70                 .colorspace = V4L2_COLORSPACE_SRGB,
71                 .priv = 0
72         }
73 };
74
75 static int pb0100_s_ctrl(struct v4l2_ctrl *ctrl)
76 {
77         struct gspca_dev *gspca_dev =
78                 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
79         struct sd *sd = (struct sd *)gspca_dev;
80         struct pb0100_ctrls *ctrls = sd->sensor_priv;
81         int err = -EINVAL;
82
83         switch (ctrl->id) {
84         case V4L2_CID_AUTOGAIN:
85                 err = pb0100_set_autogain(gspca_dev, ctrl->val);
86                 if (err)
87                         break;
88                 if (ctrl->val)
89                         break;
90                 err = pb0100_set_gain(gspca_dev, ctrls->gain->val);
91                 if (err)
92                         break;
93                 err = pb0100_set_exposure(gspca_dev, ctrls->exposure->val);
94                 break;
95         case V4L2_CTRL_CLASS_USER + 0x1001:
96                 err = pb0100_set_autogain_target(gspca_dev, ctrl->val);
97                 break;
98         }
99         return err;
100 }
101
102 static const struct v4l2_ctrl_ops pb0100_ctrl_ops = {
103         .s_ctrl = pb0100_s_ctrl,
104 };
105
106 static int pb0100_init_controls(struct sd *sd)
107 {
108         struct v4l2_ctrl_handler *hdl = &sd->gspca_dev.ctrl_handler;
109         struct pb0100_ctrls *ctrls;
110         static const struct v4l2_ctrl_config autogain_target = {
111                 .ops = &pb0100_ctrl_ops,
112                 .id = V4L2_CTRL_CLASS_USER + 0x1000,
113                 .type = V4L2_CTRL_TYPE_INTEGER,
114                 .name = "Automatic Gain Target",
115                 .max = 255,
116                 .step = 1,
117                 .def = 128,
118         };
119         static const struct v4l2_ctrl_config natural_light = {
120                 .ops = &pb0100_ctrl_ops,
121                 .id = V4L2_CTRL_CLASS_USER + 0x1001,
122                 .type = V4L2_CTRL_TYPE_BOOLEAN,
123                 .name = "Natural Light Source",
124                 .max = 1,
125                 .step = 1,
126                 .def = 1,
127         };
128
129         ctrls = kzalloc(sizeof(*ctrls), GFP_KERNEL);
130         if (!ctrls)
131                 return -ENOMEM;
132
133         v4l2_ctrl_handler_init(hdl, 6);
134         ctrls->autogain = v4l2_ctrl_new_std(hdl, &pb0100_ctrl_ops,
135                         V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
136         ctrls->exposure = v4l2_ctrl_new_std(hdl, &pb0100_ctrl_ops,
137                         V4L2_CID_EXPOSURE, 0, 511, 1, 12);
138         ctrls->gain = v4l2_ctrl_new_std(hdl, &pb0100_ctrl_ops,
139                         V4L2_CID_GAIN, 0, 255, 1, 128);
140         ctrls->red = v4l2_ctrl_new_std(hdl, &pb0100_ctrl_ops,
141                         V4L2_CID_RED_BALANCE, -255, 255, 1, 0);
142         ctrls->blue = v4l2_ctrl_new_std(hdl, &pb0100_ctrl_ops,
143                         V4L2_CID_BLUE_BALANCE, -255, 255, 1, 0);
144         ctrls->natural = v4l2_ctrl_new_custom(hdl, &natural_light, NULL);
145         ctrls->target = v4l2_ctrl_new_custom(hdl, &autogain_target, NULL);
146         if (hdl->error) {
147                 kfree(ctrls);
148                 return hdl->error;
149         }
150         sd->sensor_priv = ctrls;
151         v4l2_ctrl_auto_cluster(5, &ctrls->autogain, 0, false);
152         return 0;
153 }
154
155 static int pb0100_probe(struct sd *sd)
156 {
157         u16 sensor;
158         int err;
159
160         err = stv06xx_read_sensor(sd, PB_IDENT, &sensor);
161
162         if (err < 0)
163                 return -ENODEV;
164         if ((sensor >> 8) != 0x64)
165                 return -ENODEV;
166
167         pr_info("Photobit pb0100 sensor detected\n");
168
169         sd->gspca_dev.cam.cam_mode = pb0100_mode;
170         sd->gspca_dev.cam.nmodes = ARRAY_SIZE(pb0100_mode);
171
172         return 0;
173 }
174
175 static int pb0100_start(struct sd *sd)
176 {
177         int err, packet_size, max_packet_size;
178         struct usb_host_interface *alt;
179         struct usb_interface *intf;
180         struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
181         struct cam *cam = &sd->gspca_dev.cam;
182         u32 mode = cam->cam_mode[sd->gspca_dev.curr_mode].priv;
183
184         intf = usb_ifnum_to_if(sd->gspca_dev.dev, sd->gspca_dev.iface);
185         alt = usb_altnum_to_altsetting(intf, sd->gspca_dev.alt);
186         if (!alt)
187                 return -ENODEV;
188         packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
189
190         /* If we don't have enough bandwidth use a lower framerate */
191         max_packet_size = sd->sensor->max_packet_size[sd->gspca_dev.curr_mode];
192         if (packet_size < max_packet_size)
193                 stv06xx_write_sensor(sd, PB_ROWSPEED, BIT(4)|BIT(3)|BIT(1));
194         else
195                 stv06xx_write_sensor(sd, PB_ROWSPEED, BIT(5)|BIT(3)|BIT(1));
196
197         /* Setup sensor window */
198         if (mode & PB0100_CROP_TO_VGA) {
199                 stv06xx_write_sensor(sd, PB_RSTART, 30);
200                 stv06xx_write_sensor(sd, PB_CSTART, 20);
201                 stv06xx_write_sensor(sd, PB_RWSIZE, 240 - 1);
202                 stv06xx_write_sensor(sd, PB_CWSIZE, 320 - 1);
203         } else {
204                 stv06xx_write_sensor(sd, PB_RSTART, 8);
205                 stv06xx_write_sensor(sd, PB_CSTART, 4);
206                 stv06xx_write_sensor(sd, PB_RWSIZE, 288 - 1);
207                 stv06xx_write_sensor(sd, PB_CWSIZE, 352 - 1);
208         }
209
210         if (mode & PB0100_SUBSAMPLE) {
211                 stv06xx_write_bridge(sd, STV_Y_CTRL, 0x02); /* Wrong, FIXME */
212                 stv06xx_write_bridge(sd, STV_X_CTRL, 0x06);
213
214                 stv06xx_write_bridge(sd, STV_SCAN_RATE, 0x10);
215         } else {
216                 stv06xx_write_bridge(sd, STV_Y_CTRL, 0x01);
217                 stv06xx_write_bridge(sd, STV_X_CTRL, 0x0a);
218                 /* larger -> slower */
219                 stv06xx_write_bridge(sd, STV_SCAN_RATE, 0x20);
220         }
221
222         err = stv06xx_write_sensor(sd, PB_CONTROL, BIT(5)|BIT(3)|BIT(1));
223         gspca_dbg(gspca_dev, D_STREAM, "Started stream, status: %d\n", err);
224
225         return (err < 0) ? err : 0;
226 }
227
228 static int pb0100_stop(struct sd *sd)
229 {
230         struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
231         int err;
232
233         err = stv06xx_write_sensor(sd, PB_ABORTFRAME, 1);
234
235         if (err < 0)
236                 goto out;
237
238         /* Set bit 1 to zero */
239         err = stv06xx_write_sensor(sd, PB_CONTROL, BIT(5)|BIT(3));
240
241         gspca_dbg(gspca_dev, D_STREAM, "Halting stream\n");
242 out:
243         return (err < 0) ? err : 0;
244 }
245
246 /* FIXME: Sort the init commands out and put them into tables,
247           this is only for getting the camera to work */
248 /* FIXME: No error handling for now,
249           add this once the init has been converted to proper tables */
250 static int pb0100_init(struct sd *sd)
251 {
252         stv06xx_write_bridge(sd, STV_REG00, 1);
253         stv06xx_write_bridge(sd, STV_SCAN_RATE, 0);
254
255         /* Reset sensor */
256         stv06xx_write_sensor(sd, PB_RESET, 1);
257         stv06xx_write_sensor(sd, PB_RESET, 0);
258
259         /* Disable chip */
260         stv06xx_write_sensor(sd, PB_CONTROL, BIT(5)|BIT(3));
261
262         /* Gain stuff...*/
263         stv06xx_write_sensor(sd, PB_PREADCTRL, BIT(12)|BIT(10)|BIT(6));
264         stv06xx_write_sensor(sd, PB_ADCGLOBALGAIN, 12);
265
266         /* Set up auto-exposure */
267         /* ADC VREF_HI new setting for a transition
268           from the Expose1 to the Expose2 setting */
269         stv06xx_write_sensor(sd, PB_R28, 12);
270         /* gain max for autoexposure */
271         stv06xx_write_sensor(sd, PB_ADCMAXGAIN, 180);
272         /* gain min for autoexposure  */
273         stv06xx_write_sensor(sd, PB_ADCMINGAIN, 12);
274         /* Maximum frame integration time (programmed into R8)
275            allowed for auto-exposure routine */
276         stv06xx_write_sensor(sd, PB_R54, 3);
277         /* Minimum frame integration time (programmed into R8)
278            allowed for auto-exposure routine */
279         stv06xx_write_sensor(sd, PB_R55, 0);
280         stv06xx_write_sensor(sd, PB_UPDATEINT, 1);
281         /* R15  Expose0 (maximum that auto-exposure may use) */
282         stv06xx_write_sensor(sd, PB_R15, 800);
283         /* R17  Expose2 (minimum that auto-exposure may use) */
284         stv06xx_write_sensor(sd, PB_R17, 10);
285
286         stv06xx_write_sensor(sd, PB_EXPGAIN, 0);
287
288         /* 0x14 */
289         stv06xx_write_sensor(sd, PB_VOFFSET, 0);
290         /* 0x0D */
291         stv06xx_write_sensor(sd, PB_ADCGAINH, 11);
292         /* Set black level (important!) */
293         stv06xx_write_sensor(sd, PB_ADCGAINL, 0);
294
295         /* ??? */
296         stv06xx_write_bridge(sd, STV_REG00, 0x11);
297         stv06xx_write_bridge(sd, STV_REG03, 0x45);
298         stv06xx_write_bridge(sd, STV_REG04, 0x07);
299
300         /* Scan/timing for the sensor */
301         stv06xx_write_sensor(sd, PB_ROWSPEED, BIT(4)|BIT(3)|BIT(1));
302         stv06xx_write_sensor(sd, PB_CFILLIN, 14);
303         stv06xx_write_sensor(sd, PB_VBL, 0);
304         stv06xx_write_sensor(sd, PB_FINTTIME, 0);
305         stv06xx_write_sensor(sd, PB_RINTTIME, 123);
306
307         stv06xx_write_bridge(sd, STV_REG01, 0xc2);
308         stv06xx_write_bridge(sd, STV_REG02, 0xb0);
309         return 0;
310 }
311
312 static int pb0100_dump(struct sd *sd)
313 {
314         return 0;
315 }
316
317 static int pb0100_set_gain(struct gspca_dev *gspca_dev, __s32 val)
318 {
319         int err;
320         struct sd *sd = (struct sd *) gspca_dev;
321         struct pb0100_ctrls *ctrls = sd->sensor_priv;
322
323         err = stv06xx_write_sensor(sd, PB_G1GAIN, val);
324         if (!err)
325                 err = stv06xx_write_sensor(sd, PB_G2GAIN, val);
326         gspca_dbg(gspca_dev, D_CONF, "Set green gain to %d, status: %d\n",
327                   val, err);
328
329         if (!err)
330                 err = pb0100_set_red_balance(gspca_dev, ctrls->red->val);
331         if (!err)
332                 err = pb0100_set_blue_balance(gspca_dev, ctrls->blue->val);
333
334         return err;
335 }
336
337 static int pb0100_set_red_balance(struct gspca_dev *gspca_dev, __s32 val)
338 {
339         int err;
340         struct sd *sd = (struct sd *) gspca_dev;
341         struct pb0100_ctrls *ctrls = sd->sensor_priv;
342
343         val += ctrls->gain->val;
344         if (val < 0)
345                 val = 0;
346         else if (val > 255)
347                 val = 255;
348
349         err = stv06xx_write_sensor(sd, PB_RGAIN, val);
350         gspca_dbg(gspca_dev, D_CONF, "Set red gain to %d, status: %d\n",
351                   val, err);
352
353         return err;
354 }
355
356 static int pb0100_set_blue_balance(struct gspca_dev *gspca_dev, __s32 val)
357 {
358         int err;
359         struct sd *sd = (struct sd *) gspca_dev;
360         struct pb0100_ctrls *ctrls = sd->sensor_priv;
361
362         val += ctrls->gain->val;
363         if (val < 0)
364                 val = 0;
365         else if (val > 255)
366                 val = 255;
367
368         err = stv06xx_write_sensor(sd, PB_BGAIN, val);
369         gspca_dbg(gspca_dev, D_CONF, "Set blue gain to %d, status: %d\n",
370                   val, err);
371
372         return err;
373 }
374
375 static int pb0100_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
376 {
377         struct sd *sd = (struct sd *) gspca_dev;
378         int err;
379
380         err = stv06xx_write_sensor(sd, PB_RINTTIME, val);
381         gspca_dbg(gspca_dev, D_CONF, "Set exposure to %d, status: %d\n",
382                   val, err);
383
384         return err;
385 }
386
387 static int pb0100_set_autogain(struct gspca_dev *gspca_dev, __s32 val)
388 {
389         int err;
390         struct sd *sd = (struct sd *) gspca_dev;
391         struct pb0100_ctrls *ctrls = sd->sensor_priv;
392
393         if (val) {
394                 if (ctrls->natural->val)
395                         val = BIT(6)|BIT(4)|BIT(0);
396                 else
397                         val = BIT(4)|BIT(0);
398         } else
399                 val = 0;
400
401         err = stv06xx_write_sensor(sd, PB_EXPGAIN, val);
402         gspca_dbg(gspca_dev, D_CONF, "Set autogain to %d (natural: %d), status: %d\n",
403                   val, ctrls->natural->val, err);
404
405         return err;
406 }
407
408 static int pb0100_set_autogain_target(struct gspca_dev *gspca_dev, __s32 val)
409 {
410         int err, totalpixels, brightpixels, darkpixels;
411         struct sd *sd = (struct sd *) gspca_dev;
412
413         /* Number of pixels counted by the sensor when subsampling the pixels.
414          * Slightly larger than the real value to avoid oscillation */
415         totalpixels = gspca_dev->pixfmt.width * gspca_dev->pixfmt.height;
416         totalpixels = totalpixels/(8*8) + totalpixels/(64*64);
417
418         brightpixels = (totalpixels * val) >> 8;
419         darkpixels   = totalpixels - brightpixels;
420         err = stv06xx_write_sensor(sd, PB_R21, brightpixels);
421         if (!err)
422                 err = stv06xx_write_sensor(sd, PB_R22, darkpixels);
423
424         gspca_dbg(gspca_dev, D_CONF, "Set autogain target to %d, status: %d\n",
425                   val, err);
426
427         return err;
428 }