1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
44
45#include "stv06xx_pb0100.h"
46
47struct pb0100_ctrls {
48 struct {
49 struct v4l2_ctrl *autogain;
50 struct v4l2_ctrl *gain;
51 struct v4l2_ctrl *exposure;
52 struct v4l2_ctrl *red;
53 struct v4l2_ctrl *blue;
54 struct v4l2_ctrl *natural;
55 };
56 struct v4l2_ctrl *target;
57};
58
59static struct v4l2_pix_format pb0100_mode[] = {
60
61
62 {
63 320,
64 240,
65 V4L2_PIX_FMT_SGRBG8,
66 V4L2_FIELD_NONE,
67 .sizeimage = 320 * 240,
68 .bytesperline = 320,
69 .colorspace = V4L2_COLORSPACE_SRGB,
70 .priv = PB0100_CROP_TO_VGA
71 },
72 {
73 352,
74 288,
75 V4L2_PIX_FMT_SGRBG8,
76 V4L2_FIELD_NONE,
77 .sizeimage = 352 * 288,
78 .bytesperline = 352,
79 .colorspace = V4L2_COLORSPACE_SRGB,
80 .priv = 0
81 }
82};
83
84static int pb0100_s_ctrl(struct v4l2_ctrl *ctrl)
85{
86 struct gspca_dev *gspca_dev =
87 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
88 struct sd *sd = (struct sd *)gspca_dev;
89 struct pb0100_ctrls *ctrls = sd->sensor_priv;
90 int err = -EINVAL;
91
92 switch (ctrl->id) {
93 case V4L2_CID_AUTOGAIN:
94 err = pb0100_set_autogain(gspca_dev, ctrl->val);
95 if (err)
96 break;
97 if (ctrl->val)
98 break;
99 err = pb0100_set_gain(gspca_dev, ctrls->gain->val);
100 if (err)
101 break;
102 err = pb0100_set_exposure(gspca_dev, ctrls->exposure->val);
103 break;
104 case V4L2_CTRL_CLASS_USER + 0x1001:
105 err = pb0100_set_autogain_target(gspca_dev, ctrl->val);
106 break;
107 }
108 return err;
109}
110
111static const struct v4l2_ctrl_ops pb0100_ctrl_ops = {
112 .s_ctrl = pb0100_s_ctrl,
113};
114
115static int pb0100_init_controls(struct sd *sd)
116{
117 struct v4l2_ctrl_handler *hdl = &sd->gspca_dev.ctrl_handler;
118 struct pb0100_ctrls *ctrls;
119 static const struct v4l2_ctrl_config autogain_target = {
120 .ops = &pb0100_ctrl_ops,
121 .id = V4L2_CTRL_CLASS_USER + 0x1000,
122 .type = V4L2_CTRL_TYPE_INTEGER,
123 .name = "Automatic Gain Target",
124 .max = 255,
125 .step = 1,
126 .def = 128,
127 };
128 static const struct v4l2_ctrl_config natural_light = {
129 .ops = &pb0100_ctrl_ops,
130 .id = V4L2_CTRL_CLASS_USER + 0x1001,
131 .type = V4L2_CTRL_TYPE_BOOLEAN,
132 .name = "Natural Light Source",
133 .max = 1,
134 .step = 1,
135 .def = 1,
136 };
137
138 ctrls = kzalloc(sizeof(*ctrls), GFP_KERNEL);
139 if (!ctrls)
140 return -ENOMEM;
141
142 v4l2_ctrl_handler_init(hdl, 6);
143 ctrls->autogain = v4l2_ctrl_new_std(hdl, &pb0100_ctrl_ops,
144 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
145 ctrls->exposure = v4l2_ctrl_new_std(hdl, &pb0100_ctrl_ops,
146 V4L2_CID_EXPOSURE, 0, 511, 1, 12);
147 ctrls->gain = v4l2_ctrl_new_std(hdl, &pb0100_ctrl_ops,
148 V4L2_CID_GAIN, 0, 255, 1, 128);
149 ctrls->red = v4l2_ctrl_new_std(hdl, &pb0100_ctrl_ops,
150 V4L2_CID_RED_BALANCE, -255, 255, 1, 0);
151 ctrls->blue = v4l2_ctrl_new_std(hdl, &pb0100_ctrl_ops,
152 V4L2_CID_BLUE_BALANCE, -255, 255, 1, 0);
153 ctrls->natural = v4l2_ctrl_new_custom(hdl, &natural_light, NULL);
154 ctrls->target = v4l2_ctrl_new_custom(hdl, &autogain_target, NULL);
155 if (hdl->error) {
156 kfree(ctrls);
157 return hdl->error;
158 }
159 sd->sensor_priv = ctrls;
160 v4l2_ctrl_auto_cluster(5, &ctrls->autogain, 0, false);
161 return 0;
162}
163
164static int pb0100_probe(struct sd *sd)
165{
166 u16 sensor;
167 int err;
168
169 err = stv06xx_read_sensor(sd, PB_IDENT, &sensor);
170
171 if (err < 0)
172 return -ENODEV;
173 if ((sensor >> 8) != 0x64)
174 return -ENODEV;
175
176 pr_info("Photobit pb0100 sensor detected\n");
177
178 sd->gspca_dev.cam.cam_mode = pb0100_mode;
179 sd->gspca_dev.cam.nmodes = ARRAY_SIZE(pb0100_mode);
180
181 return 0;
182}
183
184static int pb0100_start(struct sd *sd)
185{
186 int err, packet_size, max_packet_size;
187 struct usb_host_interface *alt;
188 struct usb_interface *intf;
189 struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
190 struct cam *cam = &sd->gspca_dev.cam;
191 u32 mode = cam->cam_mode[sd->gspca_dev.curr_mode].priv;
192
193 intf = usb_ifnum_to_if(sd->gspca_dev.dev, sd->gspca_dev.iface);
194 alt = usb_altnum_to_altsetting(intf, sd->gspca_dev.alt);
195 if (!alt)
196 return -ENODEV;
197 packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
198
199
200 max_packet_size = sd->sensor->max_packet_size[sd->gspca_dev.curr_mode];
201 if (packet_size < max_packet_size)
202 stv06xx_write_sensor(sd, PB_ROWSPEED, BIT(4)|BIT(3)|BIT(1));
203 else
204 stv06xx_write_sensor(sd, PB_ROWSPEED, BIT(5)|BIT(3)|BIT(1));
205
206
207 if (mode & PB0100_CROP_TO_VGA) {
208 stv06xx_write_sensor(sd, PB_RSTART, 30);
209 stv06xx_write_sensor(sd, PB_CSTART, 20);
210 stv06xx_write_sensor(sd, PB_RWSIZE, 240 - 1);
211 stv06xx_write_sensor(sd, PB_CWSIZE, 320 - 1);
212 } else {
213 stv06xx_write_sensor(sd, PB_RSTART, 8);
214 stv06xx_write_sensor(sd, PB_CSTART, 4);
215 stv06xx_write_sensor(sd, PB_RWSIZE, 288 - 1);
216 stv06xx_write_sensor(sd, PB_CWSIZE, 352 - 1);
217 }
218
219 if (mode & PB0100_SUBSAMPLE) {
220 stv06xx_write_bridge(sd, STV_Y_CTRL, 0x02);
221 stv06xx_write_bridge(sd, STV_X_CTRL, 0x06);
222
223 stv06xx_write_bridge(sd, STV_SCAN_RATE, 0x10);
224 } else {
225 stv06xx_write_bridge(sd, STV_Y_CTRL, 0x01);
226 stv06xx_write_bridge(sd, STV_X_CTRL, 0x0a);
227
228 stv06xx_write_bridge(sd, STV_SCAN_RATE, 0x20);
229 }
230
231 err = stv06xx_write_sensor(sd, PB_CONTROL, BIT(5)|BIT(3)|BIT(1));
232 PDEBUG(D_STREAM, "Started stream, status: %d", err);
233
234 return (err < 0) ? err : 0;
235}
236
237static int pb0100_stop(struct sd *sd)
238{
239 struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
240 int err;
241
242 err = stv06xx_write_sensor(sd, PB_ABORTFRAME, 1);
243
244 if (err < 0)
245 goto out;
246
247
248 err = stv06xx_write_sensor(sd, PB_CONTROL, BIT(5)|BIT(3));
249
250 PDEBUG(D_STREAM, "Halting stream");
251out:
252 return (err < 0) ? err : 0;
253}
254
255
256
257
258
259static int pb0100_init(struct sd *sd)
260{
261 stv06xx_write_bridge(sd, STV_REG00, 1);
262 stv06xx_write_bridge(sd, STV_SCAN_RATE, 0);
263
264
265 stv06xx_write_sensor(sd, PB_RESET, 1);
266 stv06xx_write_sensor(sd, PB_RESET, 0);
267
268
269 stv06xx_write_sensor(sd, PB_CONTROL, BIT(5)|BIT(3));
270
271
272 stv06xx_write_sensor(sd, PB_PREADCTRL, BIT(12)|BIT(10)|BIT(6));
273 stv06xx_write_sensor(sd, PB_ADCGLOBALGAIN, 12);
274
275
276
277
278 stv06xx_write_sensor(sd, PB_R28, 12);
279
280 stv06xx_write_sensor(sd, PB_ADCMAXGAIN, 180);
281
282 stv06xx_write_sensor(sd, PB_ADCMINGAIN, 12);
283
284
285 stv06xx_write_sensor(sd, PB_R54, 3);
286
287
288 stv06xx_write_sensor(sd, PB_R55, 0);
289 stv06xx_write_sensor(sd, PB_UPDATEINT, 1);
290
291 stv06xx_write_sensor(sd, PB_R15, 800);
292
293 stv06xx_write_sensor(sd, PB_R17, 10);
294
295 stv06xx_write_sensor(sd, PB_EXPGAIN, 0);
296
297
298 stv06xx_write_sensor(sd, PB_VOFFSET, 0);
299
300 stv06xx_write_sensor(sd, PB_ADCGAINH, 11);
301
302 stv06xx_write_sensor(sd, PB_ADCGAINL, 0);
303
304
305 stv06xx_write_bridge(sd, STV_REG00, 0x11);
306 stv06xx_write_bridge(sd, STV_REG03, 0x45);
307 stv06xx_write_bridge(sd, STV_REG04, 0x07);
308
309
310 stv06xx_write_sensor(sd, PB_ROWSPEED, BIT(4)|BIT(3)|BIT(1));
311 stv06xx_write_sensor(sd, PB_CFILLIN, 14);
312 stv06xx_write_sensor(sd, PB_VBL, 0);
313 stv06xx_write_sensor(sd, PB_FINTTIME, 0);
314 stv06xx_write_sensor(sd, PB_RINTTIME, 123);
315
316 stv06xx_write_bridge(sd, STV_REG01, 0xc2);
317 stv06xx_write_bridge(sd, STV_REG02, 0xb0);
318 return 0;
319}
320
321static int pb0100_dump(struct sd *sd)
322{
323 return 0;
324}
325
326static int pb0100_set_gain(struct gspca_dev *gspca_dev, __s32 val)
327{
328 int err;
329 struct sd *sd = (struct sd *) gspca_dev;
330 struct pb0100_ctrls *ctrls = sd->sensor_priv;
331
332 err = stv06xx_write_sensor(sd, PB_G1GAIN, val);
333 if (!err)
334 err = stv06xx_write_sensor(sd, PB_G2GAIN, val);
335 PDEBUG(D_CONF, "Set green gain to %d, status: %d", val, err);
336
337 if (!err)
338 err = pb0100_set_red_balance(gspca_dev, ctrls->red->val);
339 if (!err)
340 err = pb0100_set_blue_balance(gspca_dev, ctrls->blue->val);
341
342 return err;
343}
344
345static int pb0100_set_red_balance(struct gspca_dev *gspca_dev, __s32 val)
346{
347 int err;
348 struct sd *sd = (struct sd *) gspca_dev;
349 struct pb0100_ctrls *ctrls = sd->sensor_priv;
350
351 val += ctrls->gain->val;
352 if (val < 0)
353 val = 0;
354 else if (val > 255)
355 val = 255;
356
357 err = stv06xx_write_sensor(sd, PB_RGAIN, val);
358 PDEBUG(D_CONF, "Set red gain to %d, status: %d", val, err);
359
360 return err;
361}
362
363static int pb0100_set_blue_balance(struct gspca_dev *gspca_dev, __s32 val)
364{
365 int err;
366 struct sd *sd = (struct sd *) gspca_dev;
367 struct pb0100_ctrls *ctrls = sd->sensor_priv;
368
369 val += ctrls->gain->val;
370 if (val < 0)
371 val = 0;
372 else if (val > 255)
373 val = 255;
374
375 err = stv06xx_write_sensor(sd, PB_BGAIN, val);
376 PDEBUG(D_CONF, "Set blue gain to %d, status: %d", val, err);
377
378 return err;
379}
380
381static int pb0100_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
382{
383 struct sd *sd = (struct sd *) gspca_dev;
384 int err;
385
386 err = stv06xx_write_sensor(sd, PB_RINTTIME, val);
387 PDEBUG(D_CONF, "Set exposure to %d, status: %d", val, err);
388
389 return err;
390}
391
392static int pb0100_set_autogain(struct gspca_dev *gspca_dev, __s32 val)
393{
394 int err;
395 struct sd *sd = (struct sd *) gspca_dev;
396 struct pb0100_ctrls *ctrls = sd->sensor_priv;
397
398 if (val) {
399 if (ctrls->natural->val)
400 val = BIT(6)|BIT(4)|BIT(0);
401 else
402 val = BIT(4)|BIT(0);
403 } else
404 val = 0;
405
406 err = stv06xx_write_sensor(sd, PB_EXPGAIN, val);
407 PDEBUG(D_CONF, "Set autogain to %d (natural: %d), status: %d",
408 val, ctrls->natural->val, err);
409
410 return err;
411}
412
413static int pb0100_set_autogain_target(struct gspca_dev *gspca_dev, __s32 val)
414{
415 int err, totalpixels, brightpixels, darkpixels;
416 struct sd *sd = (struct sd *) gspca_dev;
417
418
419
420 totalpixels = gspca_dev->pixfmt.width * gspca_dev->pixfmt.height;
421 totalpixels = totalpixels/(8*8) + totalpixels/(64*64);
422
423 brightpixels = (totalpixels * val) >> 8;
424 darkpixels = totalpixels - brightpixels;
425 err = stv06xx_write_sensor(sd, PB_R21, brightpixels);
426 if (!err)
427 err = stv06xx_write_sensor(sd, PB_R22, darkpixels);
428
429 PDEBUG(D_CONF, "Set autogain target to %d, status: %d", val, err);
430
431 return err;
432}
433