xref: /openbmc/linux/drivers/media/usb/gspca/pac207.c (revision f220d3eb)
1 /*
2  * Pixart PAC207BCA library
3  *
4  * Copyright (C) 2008 Hans de Goede <hdegoede@redhat.com>
5  * Copyright (C) 2005 Thomas Kaiser thomas@kaiser-linux.li
6  * Copyleft (C) 2005 Michel Xhaard mxhaard@magic.fr
7  *
8  * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  */
21 
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 
24 #define MODULE_NAME "pac207"
25 
26 #include <linux/input.h>
27 #include "gspca.h"
28 /* Include pac common sof detection functions */
29 #include "pac_common.h"
30 
31 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
32 MODULE_DESCRIPTION("Pixart PAC207");
33 MODULE_LICENSE("GPL");
34 
35 #define PAC207_CTRL_TIMEOUT		100  /* ms */
36 
37 #define PAC207_BRIGHTNESS_MIN		0
38 #define PAC207_BRIGHTNESS_MAX		255
39 #define PAC207_BRIGHTNESS_DEFAULT	46
40 #define PAC207_BRIGHTNESS_REG		0x08
41 
42 #define PAC207_EXPOSURE_MIN		3
43 #define PAC207_EXPOSURE_MAX		90 /* 1 sec expo time / 1 fps */
44 #define PAC207_EXPOSURE_DEFAULT		5 /* power on default: 3 */
45 #define PAC207_EXPOSURE_REG		0x02
46 
47 #define PAC207_GAIN_MIN			0
48 #define PAC207_GAIN_MAX			31
49 #define PAC207_GAIN_DEFAULT		7 /* power on default: 9 */
50 #define PAC207_GAIN_REG			0x0e
51 
52 #define PAC207_AUTOGAIN_DEADZONE	30
53 
54 /* global parameters */
55 static int led_invert;
56 module_param(led_invert, int, 0644);
57 MODULE_PARM_DESC(led_invert, "Invert led");
58 
59 /* specific webcam descriptor */
60 struct sd {
61 	struct gspca_dev gspca_dev;		/* !! must be the first item */
62 
63 	struct v4l2_ctrl *brightness;
64 
65 	u8 mode;
66 	u8 sof_read;
67 	u8 header_read;
68 	u8 autogain_ignore_frames;
69 
70 	atomic_t avg_lum;
71 };
72 
73 static const struct v4l2_pix_format sif_mode[] = {
74 	{176, 144, V4L2_PIX_FMT_PAC207, V4L2_FIELD_NONE,
75 		.bytesperline = 176,
76 		.sizeimage = (176 + 2) * 144,
77 			/* uncompressed, add 2 bytes / line for line header */
78 		.colorspace = V4L2_COLORSPACE_SRGB,
79 		.priv = 1},
80 	{352, 288, V4L2_PIX_FMT_PAC207, V4L2_FIELD_NONE,
81 		.bytesperline = 352,
82 			/* compressed, but only when needed (not compressed
83 			   when the framerate is low) */
84 		.sizeimage = (352 + 2) * 288,
85 		.colorspace = V4L2_COLORSPACE_SRGB,
86 		.priv = 0},
87 };
88 
89 static const __u8 pac207_sensor_init[][8] = {
90 	{0x10, 0x12, 0x0d, 0x12, 0x0c, 0x01, 0x29, 0x84},
91 	{0x49, 0x64, 0x64, 0x64, 0x04, 0x10, 0xf0, 0x30},
92 	{0x00, 0x00, 0x00, 0x70, 0xa0, 0xf8, 0x00, 0x00},
93 	{0x32, 0x00, 0x96, 0x00, 0xa2, 0x02, 0xaf, 0x00},
94 };
95 
96 static void pac207_write_regs(struct gspca_dev *gspca_dev, u16 index,
97 	const u8 *buffer, u16 length)
98 {
99 	struct usb_device *udev = gspca_dev->dev;
100 	int err;
101 
102 	if (gspca_dev->usb_err < 0)
103 		return;
104 
105 	memcpy(gspca_dev->usb_buf, buffer, length);
106 
107 	err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x01,
108 			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
109 			0x00, index,
110 			gspca_dev->usb_buf, length, PAC207_CTRL_TIMEOUT);
111 	if (err < 0) {
112 		pr_err("Failed to write registers to index 0x%04X, error %d\n",
113 		       index, err);
114 		gspca_dev->usb_err = err;
115 	}
116 }
117 
118 static void pac207_write_reg(struct gspca_dev *gspca_dev, u16 index, u16 value)
119 {
120 	struct usb_device *udev = gspca_dev->dev;
121 	int err;
122 
123 	if (gspca_dev->usb_err < 0)
124 		return;
125 
126 	err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00,
127 			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
128 			value, index, NULL, 0, PAC207_CTRL_TIMEOUT);
129 	if (err) {
130 		pr_err("Failed to write a register (index 0x%04X, value 0x%02X, error %d)\n",
131 		       index, value, err);
132 		gspca_dev->usb_err = err;
133 	}
134 }
135 
136 static int pac207_read_reg(struct gspca_dev *gspca_dev, u16 index)
137 {
138 	struct usb_device *udev = gspca_dev->dev;
139 	int res;
140 
141 	if (gspca_dev->usb_err < 0)
142 		return 0;
143 
144 	res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00,
145 			USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
146 			0x00, index,
147 			gspca_dev->usb_buf, 1, PAC207_CTRL_TIMEOUT);
148 	if (res < 0) {
149 		pr_err("Failed to read a register (index 0x%04X, error %d)\n",
150 		       index, res);
151 		gspca_dev->usb_err = res;
152 		return 0;
153 	}
154 
155 	return gspca_dev->usb_buf[0];
156 }
157 
158 /* this function is called at probe time */
159 static int sd_config(struct gspca_dev *gspca_dev,
160 			const struct usb_device_id *id)
161 {
162 	struct cam *cam;
163 	u8 idreg[2];
164 
165 	idreg[0] = pac207_read_reg(gspca_dev, 0x0000);
166 	idreg[1] = pac207_read_reg(gspca_dev, 0x0001);
167 	idreg[0] = ((idreg[0] & 0x0f) << 4) | ((idreg[1] & 0xf0) >> 4);
168 	idreg[1] = idreg[1] & 0x0f;
169 	gspca_dbg(gspca_dev, D_PROBE, "Pixart Sensor ID 0x%02X Chips ID 0x%02X\n",
170 		  idreg[0], idreg[1]);
171 
172 	if (idreg[0] != 0x27) {
173 		gspca_dbg(gspca_dev, D_PROBE, "Error invalid sensor ID!\n");
174 		return -ENODEV;
175 	}
176 
177 	gspca_dbg(gspca_dev, D_PROBE,
178 		  "Pixart PAC207BCA Image Processor and Control Chip detected (vid/pid 0x%04X:0x%04X)\n",
179 		  id->idVendor, id->idProduct);
180 
181 	cam = &gspca_dev->cam;
182 	cam->cam_mode = sif_mode;
183 	cam->nmodes = ARRAY_SIZE(sif_mode);
184 
185 	return 0;
186 }
187 
188 /* this function is called at probe and resume time */
189 static int sd_init(struct gspca_dev *gspca_dev)
190 {
191 	u8 mode;
192 
193 	/* mode: Image Format (Bit 0), LED (1), Compr. test mode (2) */
194 	if (led_invert)
195 		mode = 0x02;
196 	else
197 		mode = 0x00;
198 	pac207_write_reg(gspca_dev, 0x41, mode);
199 	pac207_write_reg(gspca_dev, 0x0f, 0x00); /* Power Control */
200 
201 	return gspca_dev->usb_err;
202 }
203 
204 static void setcontrol(struct gspca_dev *gspca_dev, u16 reg, u16 val)
205 {
206 	pac207_write_reg(gspca_dev, reg, val);
207 	pac207_write_reg(gspca_dev, 0x13, 0x01);	/* Bit 0, auto clear */
208 	pac207_write_reg(gspca_dev, 0x1c, 0x01);	/* not documented */
209 }
210 
211 static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
212 {
213 	struct gspca_dev *gspca_dev =
214 		container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
215 	struct sd *sd = (struct sd *)gspca_dev;
216 
217 	gspca_dev->usb_err = 0;
218 
219 	if (ctrl->id == V4L2_CID_AUTOGAIN && ctrl->is_new && ctrl->val) {
220 		/* when switching to autogain set defaults to make sure
221 		   we are on a valid point of the autogain gain /
222 		   exposure knee graph, and give this change time to
223 		   take effect before doing autogain. */
224 		gspca_dev->exposure->val    = PAC207_EXPOSURE_DEFAULT;
225 		gspca_dev->gain->val        = PAC207_GAIN_DEFAULT;
226 		sd->autogain_ignore_frames  = PAC_AUTOGAIN_IGNORE_FRAMES;
227 	}
228 
229 	if (!gspca_dev->streaming)
230 		return 0;
231 
232 	switch (ctrl->id) {
233 	case V4L2_CID_BRIGHTNESS:
234 		setcontrol(gspca_dev, PAC207_BRIGHTNESS_REG, ctrl->val);
235 		break;
236 	case V4L2_CID_AUTOGAIN:
237 		if (gspca_dev->exposure->is_new || (ctrl->is_new && ctrl->val))
238 			setcontrol(gspca_dev, PAC207_EXPOSURE_REG,
239 				   gspca_dev->exposure->val);
240 		if (gspca_dev->gain->is_new || (ctrl->is_new && ctrl->val))
241 			setcontrol(gspca_dev, PAC207_GAIN_REG,
242 				   gspca_dev->gain->val);
243 		break;
244 	default:
245 		return -EINVAL;
246 	}
247 	return gspca_dev->usb_err;
248 }
249 
250 static const struct v4l2_ctrl_ops sd_ctrl_ops = {
251 	.s_ctrl = sd_s_ctrl,
252 };
253 
254 /* this function is called at probe time */
255 static int sd_init_controls(struct gspca_dev *gspca_dev)
256 {
257 	struct sd *sd = (struct sd *) gspca_dev;
258 	struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
259 
260 	gspca_dev->vdev.ctrl_handler = hdl;
261 	v4l2_ctrl_handler_init(hdl, 4);
262 
263 	sd->brightness = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
264 				V4L2_CID_BRIGHTNESS,
265 				PAC207_BRIGHTNESS_MIN, PAC207_BRIGHTNESS_MAX,
266 				1, PAC207_BRIGHTNESS_DEFAULT);
267 	gspca_dev->autogain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
268 				V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
269 	gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
270 				V4L2_CID_EXPOSURE,
271 				PAC207_EXPOSURE_MIN, PAC207_EXPOSURE_MAX,
272 				1, PAC207_EXPOSURE_DEFAULT);
273 	gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
274 				V4L2_CID_GAIN,
275 				PAC207_GAIN_MIN, PAC207_GAIN_MAX,
276 				1, PAC207_GAIN_DEFAULT);
277 	if (hdl->error) {
278 		pr_err("Could not initialize controls\n");
279 		return hdl->error;
280 	}
281 	v4l2_ctrl_auto_cluster(3, &gspca_dev->autogain, 0, false);
282 	return 0;
283 }
284 
285 /* -- start the camera -- */
286 static int sd_start(struct gspca_dev *gspca_dev)
287 {
288 	struct sd *sd = (struct sd *) gspca_dev;
289 	__u8 mode;
290 
291 	pac207_write_reg(gspca_dev, 0x0f, 0x10); /* Power control (Bit 6-0) */
292 	pac207_write_regs(gspca_dev, 0x0002, pac207_sensor_init[0], 8);
293 	pac207_write_regs(gspca_dev, 0x000a, pac207_sensor_init[1], 8);
294 	pac207_write_regs(gspca_dev, 0x0012, pac207_sensor_init[2], 8);
295 	pac207_write_regs(gspca_dev, 0x0042, pac207_sensor_init[3], 8);
296 
297 	/* Compression Balance */
298 	if (gspca_dev->pixfmt.width == 176)
299 		pac207_write_reg(gspca_dev, 0x4a, 0xff);
300 	else
301 		pac207_write_reg(gspca_dev, 0x4a, 0x30);
302 	pac207_write_reg(gspca_dev, 0x4b, 0x00); /* Sram test value */
303 	pac207_write_reg(gspca_dev, 0x08, v4l2_ctrl_g_ctrl(sd->brightness));
304 
305 	/* PGA global gain (Bit 4-0) */
306 	pac207_write_reg(gspca_dev, 0x0e,
307 		v4l2_ctrl_g_ctrl(gspca_dev->gain));
308 	pac207_write_reg(gspca_dev, 0x02,
309 		v4l2_ctrl_g_ctrl(gspca_dev->exposure)); /* PXCK = 12MHz /n */
310 
311 	/* mode: Image Format (Bit 0), LED (1), Compr. test mode (2) */
312 	if (led_invert)
313 		mode = 0x00;
314 	else
315 		mode = 0x02;
316 	if (gspca_dev->pixfmt.width == 176) {	/* 176x144 */
317 		mode |= 0x01;
318 		gspca_dbg(gspca_dev, D_STREAM, "pac207_start mode 176x144\n");
319 	} else {				/* 352x288 */
320 		gspca_dbg(gspca_dev, D_STREAM, "pac207_start mode 352x288\n");
321 	}
322 	pac207_write_reg(gspca_dev, 0x41, mode);
323 
324 	pac207_write_reg(gspca_dev, 0x13, 0x01); /* Bit 0, auto clear */
325 	pac207_write_reg(gspca_dev, 0x1c, 0x01); /* not documented */
326 	msleep(10);
327 	pac207_write_reg(gspca_dev, 0x40, 0x01); /* Start ISO pipe */
328 
329 	sd->sof_read = 0;
330 	sd->autogain_ignore_frames = 0;
331 	atomic_set(&sd->avg_lum, -1);
332 	return gspca_dev->usb_err;
333 }
334 
335 static void sd_stopN(struct gspca_dev *gspca_dev)
336 {
337 	u8 mode;
338 
339 	/* mode: Image Format (Bit 0), LED (1), Compr. test mode (2) */
340 	if (led_invert)
341 		mode = 0x02;
342 	else
343 		mode = 0x00;
344 	pac207_write_reg(gspca_dev, 0x40, 0x00); /* Stop ISO pipe */
345 	pac207_write_reg(gspca_dev, 0x41, mode); /* Turn off LED */
346 	pac207_write_reg(gspca_dev, 0x0f, 0x00); /* Power Control */
347 }
348 
349 
350 static void pac207_do_auto_gain(struct gspca_dev *gspca_dev)
351 {
352 	struct sd *sd = (struct sd *) gspca_dev;
353 	int avg_lum = atomic_read(&sd->avg_lum);
354 
355 	if (avg_lum == -1)
356 		return;
357 
358 	if (sd->autogain_ignore_frames > 0)
359 		sd->autogain_ignore_frames--;
360 	else if (gspca_coarse_grained_expo_autogain(gspca_dev, avg_lum,
361 			90, PAC207_AUTOGAIN_DEADZONE))
362 		sd->autogain_ignore_frames = PAC_AUTOGAIN_IGNORE_FRAMES;
363 }
364 
365 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
366 			u8 *data,
367 			int len)
368 {
369 	struct sd *sd = (struct sd *) gspca_dev;
370 	unsigned char *sof;
371 
372 	sof = pac_find_sof(gspca_dev, &sd->sof_read, data, len);
373 	if (sof) {
374 		int n;
375 
376 		/* finish decoding current frame */
377 		n = sof - data;
378 		if (n > sizeof pac_sof_marker)
379 			n -= sizeof pac_sof_marker;
380 		else
381 			n = 0;
382 		gspca_frame_add(gspca_dev, LAST_PACKET,
383 				data, n);
384 		sd->header_read = 0;
385 		gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
386 		len -= sof - data;
387 		data = sof;
388 	}
389 	if (sd->header_read < 11) {
390 		int needed;
391 
392 		/* get average lumination from frame header (byte 5) */
393 		if (sd->header_read < 5) {
394 			needed = 5 - sd->header_read;
395 			if (len >= needed)
396 				atomic_set(&sd->avg_lum, data[needed - 1]);
397 		}
398 		/* skip the rest of the header */
399 		needed = 11 - sd->header_read;
400 		if (len <= needed) {
401 			sd->header_read += len;
402 			return;
403 		}
404 		data += needed;
405 		len -= needed;
406 		sd->header_read = 11;
407 	}
408 
409 	gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
410 }
411 
412 #if IS_ENABLED(CONFIG_INPUT)
413 static int sd_int_pkt_scan(struct gspca_dev *gspca_dev,
414 			u8 *data,		/* interrupt packet data */
415 			int len)		/* interrupt packet length */
416 {
417 	int ret = -EINVAL;
418 
419 	if (len == 2 && data[0] == 0x5a && data[1] == 0x5a) {
420 		input_report_key(gspca_dev->input_dev, KEY_CAMERA, 1);
421 		input_sync(gspca_dev->input_dev);
422 		input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
423 		input_sync(gspca_dev->input_dev);
424 		ret = 0;
425 	}
426 
427 	return ret;
428 }
429 #endif
430 
431 /* sub-driver description */
432 static const struct sd_desc sd_desc = {
433 	.name = MODULE_NAME,
434 	.config = sd_config,
435 	.init = sd_init,
436 	.init_controls = sd_init_controls,
437 	.start = sd_start,
438 	.stopN = sd_stopN,
439 	.dq_callback = pac207_do_auto_gain,
440 	.pkt_scan = sd_pkt_scan,
441 #if IS_ENABLED(CONFIG_INPUT)
442 	.int_pkt_scan = sd_int_pkt_scan,
443 #endif
444 };
445 
446 /* -- module initialisation -- */
447 static const struct usb_device_id device_table[] = {
448 	{USB_DEVICE(0x041e, 0x4028)},
449 	{USB_DEVICE(0x093a, 0x2460)},
450 	{USB_DEVICE(0x093a, 0x2461)},
451 	{USB_DEVICE(0x093a, 0x2463)},
452 	{USB_DEVICE(0x093a, 0x2464)},
453 	{USB_DEVICE(0x093a, 0x2468)},
454 	{USB_DEVICE(0x093a, 0x2470)},
455 	{USB_DEVICE(0x093a, 0x2471)},
456 	{USB_DEVICE(0x093a, 0x2472)},
457 	{USB_DEVICE(0x093a, 0x2474)},
458 	{USB_DEVICE(0x093a, 0x2476)},
459 	{USB_DEVICE(0x145f, 0x013a)},
460 	{USB_DEVICE(0x2001, 0xf115)},
461 	{}
462 };
463 MODULE_DEVICE_TABLE(usb, device_table);
464 
465 /* -- device connect -- */
466 static int sd_probe(struct usb_interface *intf,
467 			const struct usb_device_id *id)
468 {
469 	return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
470 				THIS_MODULE);
471 }
472 
473 static struct usb_driver sd_driver = {
474 	.name = MODULE_NAME,
475 	.id_table = device_table,
476 	.probe = sd_probe,
477 	.disconnect = gspca_disconnect,
478 #ifdef CONFIG_PM
479 	.suspend = gspca_suspend,
480 	.resume = gspca_resume,
481 	.reset_resume = gspca_resume,
482 #endif
483 };
484 
485 module_usb_driver(sd_driver);
486