1 /****************************************************************************
3 * Filename: cpia2_core.c
5 * Copyright 2001, STMicrolectronics, Inc.
6 * Contact: steve.miller@st.com
9 * This is a USB driver for CPia2 based video cameras.
10 * The infrastructure of this driver is based on the cpia usb driver by
11 * Jochen Scharrlach and Johannes Erdfeldt.
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 * Stripped of 2.4 stuff ready for main kernel submit by
28 * Alan Cox <alan@redhat.com>
30 ****************************************************************************/
34 #include <linux/slab.h>
36 #include <linux/vmalloc.h>
37 #include <linux/firmware.h>
39 /* #define _CPIA2_DEBUG_ */
43 static const char *block_name[] = {
51 static unsigned int debugs_on; /* default 0 - DEBUG_REG */
54 /******************************************************************************
56 * Forward Declarations
58 *****************************************************************************/
59 static int apply_vp_patch(struct camera_data *cam);
60 static int set_default_user_mode(struct camera_data *cam);
61 static int set_vw_size(struct camera_data *cam, int size);
62 static int configure_sensor(struct camera_data *cam,
63 int reqwidth, int reqheight);
64 static int config_sensor_410(struct camera_data *cam,
65 int reqwidth, int reqheight);
66 static int config_sensor_500(struct camera_data *cam,
67 int reqwidth, int reqheight);
68 static int set_all_properties(struct camera_data *cam);
69 static void get_color_params(struct camera_data *cam);
70 static void wake_system(struct camera_data *cam);
71 static void set_lowlight_boost(struct camera_data *cam);
72 static void reset_camera_struct(struct camera_data *cam);
73 static int cpia2_set_high_power(struct camera_data *cam);
75 /* Here we want the physical address of the memory.
76 * This is used when initializing the contents of the
77 * area and marking the pages as reserved.
79 static inline unsigned long kvirt_to_pa(unsigned long adr)
81 unsigned long kva, ret;
83 kva = (unsigned long) page_address(vmalloc_to_page((void *)adr));
84 kva |= adr & (PAGE_SIZE-1); /* restore the offset */
89 static void *rvmalloc(unsigned long size)
94 /* Round it off to PAGE_SIZE */
95 size = PAGE_ALIGN(size);
97 mem = vmalloc_32(size);
101 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
102 adr = (unsigned long) mem;
104 while ((long)size > 0) {
105 SetPageReserved(vmalloc_to_page((void *)adr));
112 static void rvfree(void *mem, unsigned long size)
119 size = PAGE_ALIGN(size);
121 adr = (unsigned long) mem;
122 while ((long)size > 0) {
123 ClearPageReserved(vmalloc_to_page((void *)adr));
130 /******************************************************************************
134 * Send an arbitrary command to the camera. For commands that read from
135 * the camera, copy the buffers into the proper param structures.
136 *****************************************************************************/
137 int cpia2_do_command(struct camera_data *cam,
138 u32 command, u8 direction, u8 param)
141 struct cpia2_command cmd;
142 unsigned int device = cam->params.pnp_id.device_type;
144 cmd.command = command;
145 cmd.reg_count = 2; /* default */
146 cmd.direction = direction;
149 * Set up the command.
152 case CPIA2_CMD_GET_VERSION:
154 CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_SYSTEM;
155 cmd.start = CPIA2_SYSTEM_DEVICE_HI;
157 case CPIA2_CMD_GET_PNP_ID:
159 CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_SYSTEM;
161 cmd.start = CPIA2_SYSTEM_DESCRIP_VID_HI;
163 case CPIA2_CMD_GET_ASIC_TYPE:
164 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VC;
165 cmd.start = CPIA2_VC_ASIC_ID;
167 case CPIA2_CMD_GET_SENSOR:
168 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
169 cmd.start = CPIA2_VP_SENSOR_FLAGS;
171 case CPIA2_CMD_GET_VP_DEVICE:
172 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
173 cmd.start = CPIA2_VP_DEVICEH;
175 case CPIA2_CMD_SET_VP_BRIGHTNESS:
176 cmd.buffer.block_data[0] = param; /* Then fall through */
177 case CPIA2_CMD_GET_VP_BRIGHTNESS:
178 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
180 if (device == DEVICE_STV_672)
181 cmd.start = CPIA2_VP4_EXPOSURE_TARGET;
183 cmd.start = CPIA2_VP5_EXPOSURE_TARGET;
185 case CPIA2_CMD_SET_CONTRAST:
186 cmd.buffer.block_data[0] = param; /* Then fall through */
187 case CPIA2_CMD_GET_CONTRAST:
188 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
190 cmd.start = CPIA2_VP_YRANGE;
192 case CPIA2_CMD_SET_VP_SATURATION:
193 cmd.buffer.block_data[0] = param; /* Then fall through */
194 case CPIA2_CMD_GET_VP_SATURATION:
195 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
197 if (device == DEVICE_STV_672)
198 cmd.start = CPIA2_VP_SATURATION;
200 cmd.start = CPIA2_VP5_MCUVSATURATION;
202 case CPIA2_CMD_SET_VP_GPIO_DATA:
203 cmd.buffer.block_data[0] = param; /* Then fall through */
204 case CPIA2_CMD_GET_VP_GPIO_DATA:
205 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
207 cmd.start = CPIA2_VP_GPIO_DATA;
209 case CPIA2_CMD_SET_VP_GPIO_DIRECTION:
210 cmd.buffer.block_data[0] = param; /* Then fall through */
211 case CPIA2_CMD_GET_VP_GPIO_DIRECTION:
212 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
214 cmd.start = CPIA2_VP_GPIO_DIRECTION;
216 case CPIA2_CMD_SET_VC_MP_GPIO_DATA:
217 cmd.buffer.block_data[0] = param; /* Then fall through */
218 case CPIA2_CMD_GET_VC_MP_GPIO_DATA:
219 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VC;
221 cmd.start = CPIA2_VC_MP_DATA;
223 case CPIA2_CMD_SET_VC_MP_GPIO_DIRECTION:
224 cmd.buffer.block_data[0] = param; /* Then fall through */
225 case CPIA2_CMD_GET_VC_MP_GPIO_DIRECTION:
226 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VC;
228 cmd.start = CPIA2_VC_MP_DIR;
230 case CPIA2_CMD_ENABLE_PACKET_CTRL:
232 CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_SYSTEM;
233 cmd.start = CPIA2_SYSTEM_INT_PACKET_CTRL;
235 cmd.buffer.block_data[0] = param;
237 case CPIA2_CMD_SET_FLICKER_MODES:
238 cmd.buffer.block_data[0] = param; /* Then fall through */
239 case CPIA2_CMD_GET_FLICKER_MODES:
240 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
242 cmd.start = CPIA2_VP_FLICKER_MODES;
244 case CPIA2_CMD_RESET_FIFO: /* clear fifo and enable stream block */
245 cmd.req_mode = CAMERAACCESS_TYPE_RANDOM | CAMERAACCESS_VC;
248 cmd.buffer.registers[0].index = CPIA2_VC_ST_CTRL;
249 cmd.buffer.registers[0].value = CPIA2_VC_ST_CTRL_SRC_VC |
250 CPIA2_VC_ST_CTRL_DST_USB | CPIA2_VC_ST_CTRL_EOF_DETECT;
251 cmd.buffer.registers[1].index = CPIA2_VC_ST_CTRL;
252 cmd.buffer.registers[1].value = CPIA2_VC_ST_CTRL_SRC_VC |
253 CPIA2_VC_ST_CTRL_DST_USB |
254 CPIA2_VC_ST_CTRL_EOF_DETECT |
255 CPIA2_VC_ST_CTRL_FIFO_ENABLE;
257 case CPIA2_CMD_SET_HI_POWER:
259 CAMERAACCESS_TYPE_RANDOM | CAMERAACCESS_SYSTEM;
261 cmd.buffer.registers[0].index =
262 CPIA2_SYSTEM_SYSTEM_CONTROL;
263 cmd.buffer.registers[1].index =
264 CPIA2_SYSTEM_SYSTEM_CONTROL;
265 cmd.buffer.registers[0].value = CPIA2_SYSTEM_CONTROL_CLEAR_ERR;
266 cmd.buffer.registers[1].value =
267 CPIA2_SYSTEM_CONTROL_HIGH_POWER;
269 case CPIA2_CMD_SET_LOW_POWER:
271 CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_SYSTEM;
273 cmd.start = CPIA2_SYSTEM_SYSTEM_CONTROL;
274 cmd.buffer.block_data[0] = 0;
276 case CPIA2_CMD_CLEAR_V2W_ERR:
278 CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_SYSTEM;
280 cmd.start = CPIA2_SYSTEM_SYSTEM_CONTROL;
281 cmd.buffer.block_data[0] = CPIA2_SYSTEM_CONTROL_CLEAR_ERR;
283 case CPIA2_CMD_SET_USER_MODE: /* Then fall through */
284 cmd.buffer.block_data[0] = param;
285 case CPIA2_CMD_GET_USER_MODE:
286 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
288 if (device == DEVICE_STV_672)
289 cmd.start = CPIA2_VP4_USER_MODE;
291 cmd.start = CPIA2_VP5_USER_MODE;
293 case CPIA2_CMD_FRAMERATE_REQ:
294 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
296 if (device == DEVICE_STV_672)
297 cmd.start = CPIA2_VP4_FRAMERATE_REQUEST;
299 cmd.start = CPIA2_VP5_FRAMERATE_REQUEST;
300 cmd.buffer.block_data[0] = param;
302 case CPIA2_CMD_SET_WAKEUP:
303 cmd.buffer.block_data[0] = param; /* Then fall through */
304 case CPIA2_CMD_GET_WAKEUP:
305 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VC;
307 cmd.start = CPIA2_VC_WAKEUP;
309 case CPIA2_CMD_SET_PW_CONTROL:
310 cmd.buffer.block_data[0] = param; /* Then fall through */
311 case CPIA2_CMD_GET_PW_CONTROL:
312 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VC;
314 cmd.start = CPIA2_VC_PW_CTRL;
316 case CPIA2_CMD_GET_VP_SYSTEM_STATE:
317 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
319 cmd.start = CPIA2_VP_SYSTEMSTATE;
321 case CPIA2_CMD_SET_SYSTEM_CTRL:
322 cmd.buffer.block_data[0] = param; /* Then fall through */
323 case CPIA2_CMD_GET_SYSTEM_CTRL:
325 CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_SYSTEM;
327 cmd.start = CPIA2_SYSTEM_SYSTEM_CONTROL;
329 case CPIA2_CMD_SET_VP_SYSTEM_CTRL:
330 cmd.buffer.block_data[0] = param; /* Then fall through */
331 case CPIA2_CMD_GET_VP_SYSTEM_CTRL:
332 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
334 cmd.start = CPIA2_VP_SYSTEMCTRL;
336 case CPIA2_CMD_SET_VP_EXP_MODES:
337 cmd.buffer.block_data[0] = param; /* Then fall through */
338 case CPIA2_CMD_GET_VP_EXP_MODES:
339 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
341 cmd.start = CPIA2_VP_EXPOSURE_MODES;
343 case CPIA2_CMD_SET_DEVICE_CONFIG:
344 cmd.buffer.block_data[0] = param; /* Then fall through */
345 case CPIA2_CMD_GET_DEVICE_CONFIG:
346 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
348 cmd.start = CPIA2_VP_DEVICE_CONFIG;
350 case CPIA2_CMD_SET_SERIAL_ADDR:
351 cmd.buffer.block_data[0] = param;
353 CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_SYSTEM;
355 cmd.start = CPIA2_SYSTEM_VP_SERIAL_ADDR;
357 case CPIA2_CMD_SET_SENSOR_CR1:
358 cmd.buffer.block_data[0] = param;
359 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
361 cmd.start = CPIA2_SENSOR_CR1;
363 case CPIA2_CMD_SET_VC_CONTROL:
364 cmd.buffer.block_data[0] = param; /* Then fall through */
365 case CPIA2_CMD_GET_VC_CONTROL:
366 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VC;
368 cmd.start = CPIA2_VC_VC_CTRL;
370 case CPIA2_CMD_SET_TARGET_KB:
371 cmd.req_mode = CAMERAACCESS_TYPE_RANDOM | CAMERAACCESS_VC;
373 cmd.buffer.registers[0].index = CPIA2_VC_VC_TARGET_KB;
374 cmd.buffer.registers[0].value = param;
376 case CPIA2_CMD_SET_DEF_JPEG_OPT:
377 cmd.req_mode = CAMERAACCESS_TYPE_RANDOM | CAMERAACCESS_VC;
379 cmd.buffer.registers[0].index = CPIA2_VC_VC_JPEG_OPT;
380 cmd.buffer.registers[0].value =
381 CPIA2_VC_VC_JPEG_OPT_DOUBLE_SQUEEZE;
382 cmd.buffer.registers[1].index = CPIA2_VC_VC_USER_SQUEEZE;
383 cmd.buffer.registers[1].value = 20;
384 cmd.buffer.registers[2].index = CPIA2_VC_VC_CREEP_PERIOD;
385 cmd.buffer.registers[2].value = 2;
386 cmd.buffer.registers[3].index = CPIA2_VC_VC_JPEG_OPT;
387 cmd.buffer.registers[3].value = CPIA2_VC_VC_JPEG_OPT_DEFAULT;
389 case CPIA2_CMD_REHASH_VP4:
390 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
392 cmd.start = CPIA2_VP_REHASH_VALUES;
393 cmd.buffer.block_data[0] = param;
395 case CPIA2_CMD_SET_USER_EFFECTS: /* Note: Be careful with this as
396 this register can also affect
398 cmd.buffer.block_data[0] = param; /* Then fall through */
399 case CPIA2_CMD_GET_USER_EFFECTS:
400 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
402 if (device == DEVICE_STV_672)
403 cmd.start = CPIA2_VP4_USER_EFFECTS;
405 cmd.start = CPIA2_VP5_USER_EFFECTS;
408 LOG("DoCommand received invalid command\n");
412 retval = cpia2_send_command(cam, &cmd);
418 * Now copy any results from a read into the appropriate param struct.
421 case CPIA2_CMD_GET_VERSION:
422 cam->params.version.firmware_revision_hi =
423 cmd.buffer.block_data[0];
424 cam->params.version.firmware_revision_lo =
425 cmd.buffer.block_data[1];
427 case CPIA2_CMD_GET_PNP_ID:
428 cam->params.pnp_id.vendor = (cmd.buffer.block_data[0] << 8) |
429 cmd.buffer.block_data[1];
430 cam->params.pnp_id.product = (cmd.buffer.block_data[2] << 8) |
431 cmd.buffer.block_data[3];
432 cam->params.pnp_id.device_revision =
433 (cmd.buffer.block_data[4] << 8) |
434 cmd.buffer.block_data[5];
435 if (cam->params.pnp_id.vendor == 0x553) {
436 if (cam->params.pnp_id.product == 0x100) {
437 cam->params.pnp_id.device_type = DEVICE_STV_672;
438 } else if (cam->params.pnp_id.product == 0x140 ||
439 cam->params.pnp_id.product == 0x151) {
440 cam->params.pnp_id.device_type = DEVICE_STV_676;
444 case CPIA2_CMD_GET_ASIC_TYPE:
445 cam->params.version.asic_id = cmd.buffer.block_data[0];
446 cam->params.version.asic_rev = cmd.buffer.block_data[1];
448 case CPIA2_CMD_GET_SENSOR:
449 cam->params.version.sensor_flags = cmd.buffer.block_data[0];
450 cam->params.version.sensor_rev = cmd.buffer.block_data[1];
452 case CPIA2_CMD_GET_VP_DEVICE:
453 cam->params.version.vp_device_hi = cmd.buffer.block_data[0];
454 cam->params.version.vp_device_lo = cmd.buffer.block_data[1];
456 case CPIA2_CMD_GET_VP_BRIGHTNESS:
457 cam->params.color_params.brightness = cmd.buffer.block_data[0];
459 case CPIA2_CMD_GET_CONTRAST:
460 cam->params.color_params.contrast = cmd.buffer.block_data[0];
462 case CPIA2_CMD_GET_VP_SATURATION:
463 cam->params.color_params.saturation = cmd.buffer.block_data[0];
465 case CPIA2_CMD_GET_VP_GPIO_DATA:
466 cam->params.vp_params.gpio_data = cmd.buffer.block_data[0];
468 case CPIA2_CMD_GET_VP_GPIO_DIRECTION:
469 cam->params.vp_params.gpio_direction = cmd.buffer.block_data[0];
471 case CPIA2_CMD_GET_VC_MP_GPIO_DIRECTION:
472 cam->params.vc_params.vc_mp_direction =cmd.buffer.block_data[0];
474 case CPIA2_CMD_GET_VC_MP_GPIO_DATA:
475 cam->params.vc_params.vc_mp_data = cmd.buffer.block_data[0];
477 case CPIA2_CMD_GET_FLICKER_MODES:
478 cam->params.flicker_control.cam_register =
479 cmd.buffer.block_data[0];
481 case CPIA2_CMD_GET_WAKEUP:
482 cam->params.vc_params.wakeup = cmd.buffer.block_data[0];
484 case CPIA2_CMD_GET_PW_CONTROL:
485 cam->params.vc_params.pw_control = cmd.buffer.block_data[0];
487 case CPIA2_CMD_GET_SYSTEM_CTRL:
488 cam->params.camera_state.system_ctrl = cmd.buffer.block_data[0];
490 case CPIA2_CMD_GET_VP_SYSTEM_STATE:
491 cam->params.vp_params.system_state = cmd.buffer.block_data[0];
493 case CPIA2_CMD_GET_VP_SYSTEM_CTRL:
494 cam->params.vp_params.system_ctrl = cmd.buffer.block_data[0];
496 case CPIA2_CMD_GET_VP_EXP_MODES:
497 cam->params.vp_params.exposure_modes = cmd.buffer.block_data[0];
499 case CPIA2_CMD_GET_DEVICE_CONFIG:
500 cam->params.vp_params.device_config = cmd.buffer.block_data[0];
502 case CPIA2_CMD_GET_VC_CONTROL:
503 cam->params.vc_params.vc_control = cmd.buffer.block_data[0];
505 case CPIA2_CMD_GET_USER_MODE:
506 cam->params.vp_params.video_mode = cmd.buffer.block_data[0];
508 case CPIA2_CMD_GET_USER_EFFECTS:
509 cam->params.vp_params.user_effects = cmd.buffer.block_data[0];
517 /******************************************************************************
521 *****************************************************************************/
522 int cpia2_send_command(struct camera_data *cam, struct cpia2_command *cmd)
531 if (cmd->direction == TRANSFER_WRITE) {
537 block_index = cmd->req_mode & 0x03;
539 switch (cmd->req_mode & 0x0c) {
540 case CAMERAACCESS_TYPE_RANDOM:
541 count = cmd->reg_count * sizeof(struct cpia2_register);
543 buffer = (u8 *) & cmd->buffer;
544 if (debugs_on & DEBUG_REG)
545 DBG("%s Random: Register block %s\n", dir,
546 block_name[block_index]);
548 case CAMERAACCESS_TYPE_BLOCK:
549 count = cmd->reg_count;
551 buffer = cmd->buffer.block_data;
552 if (debugs_on & DEBUG_REG)
553 DBG("%s Block: Register block %s\n", dir,
554 block_name[block_index]);
556 case CAMERAACCESS_TYPE_MASK:
557 count = cmd->reg_count * sizeof(struct cpia2_reg_mask);
559 buffer = (u8 *) & cmd->buffer;
560 if (debugs_on & DEBUG_REG)
561 DBG("%s Mask: Register block %s\n", dir,
562 block_name[block_index]);
564 case CAMERAACCESS_TYPE_REPEAT: /* For patch blocks only */
565 count = cmd->reg_count;
567 buffer = cmd->buffer.block_data;
568 if (debugs_on & DEBUG_REG)
569 DBG("%s Repeat: Register block %s\n", dir,
570 block_name[block_index]);
573 LOG("%s: invalid request mode\n",__func__);
577 retval = cpia2_usb_transfer_cmd(cam,
580 start, count, cmd->direction);
582 if (debugs_on & DEBUG_REG) {
584 for (i = 0; i < cmd->reg_count; i++) {
585 if((cmd->req_mode & 0x0c) == CAMERAACCESS_TYPE_BLOCK)
586 KINFO("%s Block: [0x%02X] = 0x%02X\n",
587 dir, start + i, buffer[i]);
588 if((cmd->req_mode & 0x0c) == CAMERAACCESS_TYPE_RANDOM)
589 KINFO("%s Random: [0x%02X] = 0x%02X\n",
590 dir, cmd->buffer.registers[i].index,
591 cmd->buffer.registers[i].value);
600 * Functions to implement camera functionality
602 /******************************************************************************
604 * cpia2_get_version_info
606 *****************************************************************************/
607 static void cpia2_get_version_info(struct camera_data *cam)
609 cpia2_do_command(cam, CPIA2_CMD_GET_VERSION, TRANSFER_READ, 0);
610 cpia2_do_command(cam, CPIA2_CMD_GET_PNP_ID, TRANSFER_READ, 0);
611 cpia2_do_command(cam, CPIA2_CMD_GET_ASIC_TYPE, TRANSFER_READ, 0);
612 cpia2_do_command(cam, CPIA2_CMD_GET_SENSOR, TRANSFER_READ, 0);
613 cpia2_do_command(cam, CPIA2_CMD_GET_VP_DEVICE, TRANSFER_READ, 0);
616 /******************************************************************************
620 * Called at least during the open process, sets up initial params.
621 *****************************************************************************/
622 int cpia2_reset_camera(struct camera_data *cam)
627 struct cpia2_command cmd;
632 retval = configure_sensor(cam,
633 cam->params.roi.width,
634 cam->params.roi.height);
636 ERR("Couldn't configure sensor, error=%d\n", retval);
640 /* Clear FIFO and route/enable stream block */
641 cmd.req_mode = CAMERAACCESS_TYPE_RANDOM | CAMERAACCESS_VC;
642 cmd.direction = TRANSFER_WRITE;
644 cmd.buffer.registers[0].index = CPIA2_VC_ST_CTRL;
645 cmd.buffer.registers[0].value = CPIA2_VC_ST_CTRL_SRC_VC |
646 CPIA2_VC_ST_CTRL_DST_USB | CPIA2_VC_ST_CTRL_EOF_DETECT;
647 cmd.buffer.registers[1].index = CPIA2_VC_ST_CTRL;
648 cmd.buffer.registers[1].value = CPIA2_VC_ST_CTRL_SRC_VC |
649 CPIA2_VC_ST_CTRL_DST_USB |
650 CPIA2_VC_ST_CTRL_EOF_DETECT | CPIA2_VC_ST_CTRL_FIFO_ENABLE;
652 cpia2_send_command(cam, &cmd);
654 cpia2_set_high_power(cam);
656 if (cam->params.pnp_id.device_type == DEVICE_STV_672) {
657 /* Enable button notification */
658 cmd.req_mode = CAMERAACCESS_TYPE_RANDOM | CAMERAACCESS_SYSTEM;
659 cmd.buffer.registers[0].index = CPIA2_SYSTEM_INT_PACKET_CTRL;
660 cmd.buffer.registers[0].value =
661 CPIA2_SYSTEM_INT_PACKET_CTRL_ENABLE_SW_XX;
663 cpia2_send_command(cam, &cmd);
666 schedule_timeout_interruptible(msecs_to_jiffies(100));
668 if (cam->params.pnp_id.device_type == DEVICE_STV_672)
669 retval = apply_vp_patch(cam);
671 /* wait for vp to go to sleep */
672 schedule_timeout_interruptible(msecs_to_jiffies(100));
675 * If this is a 676, apply VP5 fixes before we start streaming
677 if (cam->params.pnp_id.device_type == DEVICE_STV_676) {
678 cmd.req_mode = CAMERAACCESS_TYPE_RANDOM | CAMERAACCESS_VP;
680 /* The following writes improve the picture */
681 cmd.buffer.registers[0].index = CPIA2_VP5_MYBLACK_LEVEL;
682 cmd.buffer.registers[0].value = 0; /* reduce from the default
683 * rec 601 pedestal of 16 */
684 cmd.buffer.registers[1].index = CPIA2_VP5_MCYRANGE;
685 cmd.buffer.registers[1].value = 0x92; /* increase from 100% to
686 * (256/256 - 31) to fill
688 cmd.buffer.registers[2].index = CPIA2_VP5_MYCEILING;
689 cmd.buffer.registers[2].value = 0xFF; /* Increase from the
690 * default rec 601 ceiling
692 cmd.buffer.registers[3].index = CPIA2_VP5_MCUVSATURATION;
693 cmd.buffer.registers[3].value = 0xFF; /* Increase from the rec
694 * 601 100% level (128)
696 cmd.buffer.registers[4].index = CPIA2_VP5_ANTIFLKRSETUP;
697 cmd.buffer.registers[4].value = 0x80; /* Inhibit the
700 /* The following 4 writes are a fix to allow QVGA to work at 30 fps */
701 cmd.buffer.registers[5].index = CPIA2_VP_RAM_ADDR_H;
702 cmd.buffer.registers[5].value = 0x01;
703 cmd.buffer.registers[6].index = CPIA2_VP_RAM_ADDR_L;
704 cmd.buffer.registers[6].value = 0xE3;
705 cmd.buffer.registers[7].index = CPIA2_VP_RAM_DATA;
706 cmd.buffer.registers[7].value = 0x02;
707 cmd.buffer.registers[8].index = CPIA2_VP_RAM_DATA;
708 cmd.buffer.registers[8].value = 0xFC;
710 cmd.direction = TRANSFER_WRITE;
713 cpia2_send_command(cam, &cmd);
716 /* Activate all settings and start the data stream */
718 set_default_user_mode(cam);
720 /* Give VP time to wake up */
721 schedule_timeout_interruptible(msecs_to_jiffies(100));
723 set_all_properties(cam);
725 cpia2_do_command(cam, CPIA2_CMD_GET_USER_MODE, TRANSFER_READ, 0);
726 DBG("After SetAllProperties(cam), user mode is 0x%0X\n",
727 cam->params.vp_params.video_mode);
730 * Set audio regulator off. This and the code to set the compresison
731 * state are too complex to form a CPIA2_CMD_, and seem to be somewhat
732 * intertwined. This stuff came straight from the windows driver.
734 /* Turn AutoExposure off in VP and enable the serial bridge to the sensor */
735 cpia2_do_command(cam, CPIA2_CMD_GET_VP_SYSTEM_CTRL, TRANSFER_READ, 0);
736 tmp_reg = cam->params.vp_params.system_ctrl;
737 cmd.buffer.registers[0].value = tmp_reg &
738 (tmp_reg & (CPIA2_VP_SYSTEMCTRL_HK_CONTROL ^ 0xFF));
740 cpia2_do_command(cam, CPIA2_CMD_GET_DEVICE_CONFIG, TRANSFER_READ, 0);
741 cmd.buffer.registers[1].value = cam->params.vp_params.device_config |
742 CPIA2_VP_DEVICE_CONFIG_SERIAL_BRIDGE;
743 cmd.buffer.registers[0].index = CPIA2_VP_SYSTEMCTRL;
744 cmd.buffer.registers[1].index = CPIA2_VP_DEVICE_CONFIG;
745 cmd.req_mode = CAMERAACCESS_TYPE_RANDOM | CAMERAACCESS_VP;
747 cmd.direction = TRANSFER_WRITE;
749 cpia2_send_command(cam, &cmd);
751 /* Set the correct I2C address in the CPiA-2 system register */
752 cpia2_do_command(cam,
753 CPIA2_CMD_SET_SERIAL_ADDR,
755 CPIA2_SYSTEM_VP_SERIAL_ADDR_SENSOR);
757 /* Now have sensor access - set bit to turn the audio regulator off */
758 cpia2_do_command(cam,
759 CPIA2_CMD_SET_SENSOR_CR1,
760 TRANSFER_WRITE, CPIA2_SENSOR_CR1_DOWN_AUDIO_REGULATOR);
762 /* Set the correct I2C address in the CPiA-2 system register */
763 if (cam->params.pnp_id.device_type == DEVICE_STV_672)
764 cpia2_do_command(cam,
765 CPIA2_CMD_SET_SERIAL_ADDR,
767 CPIA2_SYSTEM_VP_SERIAL_ADDR_VP); // 0x88
769 cpia2_do_command(cam,
770 CPIA2_CMD_SET_SERIAL_ADDR,
772 CPIA2_SYSTEM_VP_SERIAL_ADDR_676_VP); // 0x8a
774 /* increase signal drive strength */
775 if (cam->params.pnp_id.device_type == DEVICE_STV_676)
776 cpia2_do_command(cam,
777 CPIA2_CMD_SET_VP_EXP_MODES,
779 CPIA2_VP_EXPOSURE_MODES_COMPILE_EXP);
781 /* Start autoexposure */
782 cpia2_do_command(cam, CPIA2_CMD_GET_DEVICE_CONFIG, TRANSFER_READ, 0);
783 cmd.buffer.registers[0].value = cam->params.vp_params.device_config &
784 (CPIA2_VP_DEVICE_CONFIG_SERIAL_BRIDGE ^ 0xFF);
786 cpia2_do_command(cam, CPIA2_CMD_GET_VP_SYSTEM_CTRL, TRANSFER_READ, 0);
787 cmd.buffer.registers[1].value =
788 cam->params.vp_params.system_ctrl | CPIA2_VP_SYSTEMCTRL_HK_CONTROL;
790 cmd.buffer.registers[0].index = CPIA2_VP_DEVICE_CONFIG;
791 cmd.buffer.registers[1].index = CPIA2_VP_SYSTEMCTRL;
792 cmd.req_mode = CAMERAACCESS_TYPE_RANDOM | CAMERAACCESS_VP;
794 cmd.direction = TRANSFER_WRITE;
796 cpia2_send_command(cam, &cmd);
798 /* Set compression state */
799 cpia2_do_command(cam, CPIA2_CMD_GET_VC_CONTROL, TRANSFER_READ, 0);
800 if (cam->params.compression.inhibit_htables) {
801 tmp_reg = cam->params.vc_params.vc_control |
802 CPIA2_VC_VC_CTRL_INHIBIT_H_TABLES;
804 tmp_reg = cam->params.vc_params.vc_control &
805 ~CPIA2_VC_VC_CTRL_INHIBIT_H_TABLES;
807 cpia2_do_command(cam, CPIA2_CMD_SET_VC_CONTROL, TRANSFER_WRITE,tmp_reg);
809 /* Set target size (kb) on vc */
810 cpia2_do_command(cam, CPIA2_CMD_SET_TARGET_KB,
811 TRANSFER_WRITE, cam->params.vc_params.target_kb);
813 /* Wiggle VC Reset */
815 * First read and wait a bit.
817 for (i = 0; i < 50; i++) {
818 cpia2_do_command(cam, CPIA2_CMD_GET_PW_CONTROL,
822 tmp_reg = cam->params.vc_params.pw_control;
823 tmp_reg &= ~CPIA2_VC_PW_CTRL_VC_RESET_N;
825 cpia2_do_command(cam, CPIA2_CMD_SET_PW_CONTROL, TRANSFER_WRITE,tmp_reg);
827 tmp_reg |= CPIA2_VC_PW_CTRL_VC_RESET_N;
828 cpia2_do_command(cam, CPIA2_CMD_SET_PW_CONTROL, TRANSFER_WRITE,tmp_reg);
830 cpia2_do_command(cam, CPIA2_CMD_SET_DEF_JPEG_OPT, TRANSFER_WRITE, 0);
832 cpia2_do_command(cam, CPIA2_CMD_GET_USER_MODE, TRANSFER_READ, 0);
833 DBG("After VC RESET, user mode is 0x%0X\n",
834 cam->params.vp_params.video_mode);
839 /******************************************************************************
841 * cpia2_set_high_power
843 *****************************************************************************/
844 static int cpia2_set_high_power(struct camera_data *cam)
847 for (i = 0; i <= 50; i++) {
848 /* Read system status */
849 cpia2_do_command(cam,CPIA2_CMD_GET_SYSTEM_CTRL,TRANSFER_READ,0);
851 /* If there is an error, clear it */
852 if(cam->params.camera_state.system_ctrl &
853 CPIA2_SYSTEM_CONTROL_V2W_ERR)
854 cpia2_do_command(cam, CPIA2_CMD_CLEAR_V2W_ERR,
857 /* Try to set high power mode */
858 cpia2_do_command(cam, CPIA2_CMD_SET_SYSTEM_CTRL,
861 /* Try to read something in VP to check if everything is awake */
862 cpia2_do_command(cam, CPIA2_CMD_GET_VP_SYSTEM_STATE,
864 if (cam->params.vp_params.system_state &
865 CPIA2_VP_SYSTEMSTATE_HK_ALIVE) {
867 } else if (i == 50) {
868 cam->params.camera_state.power_mode = LO_POWER_MODE;
869 ERR("Camera did not wake up\n");
874 DBG("System now in high power state\n");
875 cam->params.camera_state.power_mode = HI_POWER_MODE;
879 /******************************************************************************
881 * cpia2_set_low_power
883 *****************************************************************************/
884 int cpia2_set_low_power(struct camera_data *cam)
886 cam->params.camera_state.power_mode = LO_POWER_MODE;
887 cpia2_do_command(cam, CPIA2_CMD_SET_SYSTEM_CTRL, TRANSFER_WRITE, 0);
891 /******************************************************************************
895 *****************************************************************************/
896 static int cpia2_send_onebyte_command(struct camera_data *cam,
897 struct cpia2_command *cmd,
900 cmd->buffer.block_data[0] = datum;
903 return cpia2_send_command(cam, cmd);
906 static int apply_vp_patch(struct camera_data *cam)
908 const struct firmware *fw;
909 const char fw_name[] = "cpia2/stv0672_vp4.bin";
911 struct cpia2_command cmd;
913 ret = request_firmware(&fw, fw_name, &cam->dev->dev);
915 printk(KERN_ERR "cpia2: failed to load VP patch \"%s\"\n",
920 cmd.req_mode = CAMERAACCESS_TYPE_REPEAT | CAMERAACCESS_VP;
921 cmd.direction = TRANSFER_WRITE;
923 /* First send the start address... */
924 cpia2_send_onebyte_command(cam, &cmd, 0x0A, fw->data[0]); /* hi */
925 cpia2_send_onebyte_command(cam, &cmd, 0x0B, fw->data[1]); /* lo */
927 /* ... followed by the data payload */
928 for (i = 2; i < fw->size; i += 64) {
929 cmd.start = 0x0C; /* Data */
930 cmd.reg_count = min_t(int, 64, fw->size - i);
931 memcpy(cmd.buffer.block_data, &fw->data[i], cmd.reg_count);
932 cpia2_send_command(cam, &cmd);
935 /* Next send the start address... */
936 cpia2_send_onebyte_command(cam, &cmd, 0x0A, fw->data[0]); /* hi */
937 cpia2_send_onebyte_command(cam, &cmd, 0x0B, fw->data[1]); /* lo */
939 /* ... followed by the 'goto' command */
940 cpia2_send_onebyte_command(cam, &cmd, 0x0D, 1);
942 release_firmware(fw);
946 /******************************************************************************
948 * set_default_user_mode
950 *****************************************************************************/
951 static int set_default_user_mode(struct camera_data *cam)
953 unsigned char user_mode;
954 unsigned char frame_rate;
955 int width = cam->params.roi.width;
956 int height = cam->params.roi.height;
958 switch (cam->params.version.sensor_flags) {
959 case CPIA2_VP_SENSOR_FLAGS_404:
960 case CPIA2_VP_SENSOR_FLAGS_407:
961 case CPIA2_VP_SENSOR_FLAGS_409:
962 case CPIA2_VP_SENSOR_FLAGS_410:
963 if ((width > STV_IMAGE_QCIF_COLS)
964 || (height > STV_IMAGE_QCIF_ROWS)) {
965 user_mode = CPIA2_VP_USER_MODE_CIF;
967 user_mode = CPIA2_VP_USER_MODE_QCIFDS;
969 frame_rate = CPIA2_VP_FRAMERATE_30;
971 case CPIA2_VP_SENSOR_FLAGS_500:
972 if ((width > STV_IMAGE_CIF_COLS)
973 || (height > STV_IMAGE_CIF_ROWS)) {
974 user_mode = CPIA2_VP_USER_MODE_VGA;
976 user_mode = CPIA2_VP_USER_MODE_QVGADS;
978 if (cam->params.pnp_id.device_type == DEVICE_STV_672)
979 frame_rate = CPIA2_VP_FRAMERATE_15;
981 frame_rate = CPIA2_VP_FRAMERATE_30;
984 LOG("%s: Invalid sensor flag value 0x%0X\n",__func__,
985 cam->params.version.sensor_flags);
989 DBG("Sensor flag = 0x%0x, user mode = 0x%0x, frame rate = 0x%X\n",
990 cam->params.version.sensor_flags, user_mode, frame_rate);
991 cpia2_do_command(cam, CPIA2_CMD_SET_USER_MODE, TRANSFER_WRITE,
993 if(cam->params.vp_params.frame_rate > 0 &&
994 frame_rate > cam->params.vp_params.frame_rate)
995 frame_rate = cam->params.vp_params.frame_rate;
997 cpia2_set_fps(cam, frame_rate);
999 // if (cam->params.pnp_id.device_type == DEVICE_STV_676)
1000 // cpia2_do_command(cam,
1001 // CPIA2_CMD_SET_VP_SYSTEM_CTRL,
1003 // CPIA2_VP_SYSTEMCTRL_HK_CONTROL |
1004 // CPIA2_VP_SYSTEMCTRL_POWER_CONTROL);
1009 /******************************************************************************
1011 * cpia2_match_video_size
1013 * return the best match, where 'best' is as always
1014 * the largest that is not bigger than what is requested.
1015 *****************************************************************************/
1016 int cpia2_match_video_size(int width, int height)
1018 if (width >= STV_IMAGE_VGA_COLS && height >= STV_IMAGE_VGA_ROWS)
1019 return VIDEOSIZE_VGA;
1021 if (width >= STV_IMAGE_CIF_COLS && height >= STV_IMAGE_CIF_ROWS)
1022 return VIDEOSIZE_CIF;
1024 if (width >= STV_IMAGE_QVGA_COLS && height >= STV_IMAGE_QVGA_ROWS)
1025 return VIDEOSIZE_QVGA;
1027 if (width >= 288 && height >= 216)
1028 return VIDEOSIZE_288_216;
1030 if (width >= 256 && height >= 192)
1031 return VIDEOSIZE_256_192;
1033 if (width >= 224 && height >= 168)
1034 return VIDEOSIZE_224_168;
1036 if (width >= 192 && height >= 144)
1037 return VIDEOSIZE_192_144;
1039 if (width >= STV_IMAGE_QCIF_COLS && height >= STV_IMAGE_QCIF_ROWS)
1040 return VIDEOSIZE_QCIF;
1045 /******************************************************************************
1049 *****************************************************************************/
1050 static int set_vw_size(struct camera_data *cam, int size)
1054 cam->params.vp_params.video_size = size;
1058 DBG("Setting size to VGA\n");
1059 cam->params.roi.width = STV_IMAGE_VGA_COLS;
1060 cam->params.roi.height = STV_IMAGE_VGA_ROWS;
1061 cam->vw.width = STV_IMAGE_VGA_COLS;
1062 cam->vw.height = STV_IMAGE_VGA_ROWS;
1065 DBG("Setting size to CIF\n");
1066 cam->params.roi.width = STV_IMAGE_CIF_COLS;
1067 cam->params.roi.height = STV_IMAGE_CIF_ROWS;
1068 cam->vw.width = STV_IMAGE_CIF_COLS;
1069 cam->vw.height = STV_IMAGE_CIF_ROWS;
1071 case VIDEOSIZE_QVGA:
1072 DBG("Setting size to QVGA\n");
1073 cam->params.roi.width = STV_IMAGE_QVGA_COLS;
1074 cam->params.roi.height = STV_IMAGE_QVGA_ROWS;
1075 cam->vw.width = STV_IMAGE_QVGA_COLS;
1076 cam->vw.height = STV_IMAGE_QVGA_ROWS;
1078 case VIDEOSIZE_288_216:
1079 cam->params.roi.width = 288;
1080 cam->params.roi.height = 216;
1081 cam->vw.width = 288;
1082 cam->vw.height = 216;
1084 case VIDEOSIZE_256_192:
1085 cam->vw.width = 256;
1086 cam->vw.height = 192;
1087 cam->params.roi.width = 256;
1088 cam->params.roi.height = 192;
1090 case VIDEOSIZE_224_168:
1091 cam->vw.width = 224;
1092 cam->vw.height = 168;
1093 cam->params.roi.width = 224;
1094 cam->params.roi.height = 168;
1096 case VIDEOSIZE_192_144:
1097 cam->vw.width = 192;
1098 cam->vw.height = 144;
1099 cam->params.roi.width = 192;
1100 cam->params.roi.height = 144;
1102 case VIDEOSIZE_QCIF:
1103 DBG("Setting size to QCIF\n");
1104 cam->params.roi.width = STV_IMAGE_QCIF_COLS;
1105 cam->params.roi.height = STV_IMAGE_QCIF_ROWS;
1106 cam->vw.width = STV_IMAGE_QCIF_COLS;
1107 cam->vw.height = STV_IMAGE_QCIF_ROWS;
1115 /******************************************************************************
1119 *****************************************************************************/
1120 static int configure_sensor(struct camera_data *cam,
1121 int req_width, int req_height)
1125 switch (cam->params.version.sensor_flags) {
1126 case CPIA2_VP_SENSOR_FLAGS_404:
1127 case CPIA2_VP_SENSOR_FLAGS_407:
1128 case CPIA2_VP_SENSOR_FLAGS_409:
1129 case CPIA2_VP_SENSOR_FLAGS_410:
1130 retval = config_sensor_410(cam, req_width, req_height);
1132 case CPIA2_VP_SENSOR_FLAGS_500:
1133 retval = config_sensor_500(cam, req_width, req_height);
1142 /******************************************************************************
1146 *****************************************************************************/
1147 static int config_sensor_410(struct camera_data *cam,
1148 int req_width, int req_height)
1150 struct cpia2_command cmd;
1154 int width = req_width;
1155 int height = req_height;
1158 * Make sure size doesn't exceed CIF.
1160 if (width > STV_IMAGE_CIF_COLS)
1161 width = STV_IMAGE_CIF_COLS;
1162 if (height > STV_IMAGE_CIF_ROWS)
1163 height = STV_IMAGE_CIF_ROWS;
1165 image_size = cpia2_match_video_size(width, height);
1167 DBG("Config 410: width = %d, height = %d\n", width, height);
1168 DBG("Image size returned is %d\n", image_size);
1169 if (image_size >= 0) {
1170 set_vw_size(cam, image_size);
1171 width = cam->params.roi.width;
1172 height = cam->params.roi.height;
1174 DBG("After set_vw_size(), width = %d, height = %d\n",
1176 if (width <= 176 && height <= 144) {
1177 DBG("image type = VIDEOSIZE_QCIF\n");
1178 image_type = VIDEOSIZE_QCIF;
1180 else if (width <= 320 && height <= 240) {
1181 DBG("image type = VIDEOSIZE_QVGA\n");
1182 image_type = VIDEOSIZE_QVGA;
1185 DBG("image type = VIDEOSIZE_CIF\n");
1186 image_type = VIDEOSIZE_CIF;
1189 ERR("ConfigSensor410 failed\n");
1193 cmd.req_mode = CAMERAACCESS_TYPE_RANDOM | CAMERAACCESS_VC;
1194 cmd.direction = TRANSFER_WRITE;
1197 cmd.buffer.registers[i].index = CPIA2_VC_VC_FORMAT;
1198 if (image_type == VIDEOSIZE_CIF) {
1199 cmd.buffer.registers[i++].value =
1200 (u8) (CPIA2_VC_VC_FORMAT_UFIRST |
1201 CPIA2_VC_VC_FORMAT_SHORTLINE);
1203 cmd.buffer.registers[i++].value =
1204 (u8) CPIA2_VC_VC_FORMAT_UFIRST;
1208 cmd.buffer.registers[i].index = CPIA2_VC_VC_CLOCKS;
1209 if (image_type == VIDEOSIZE_QCIF) {
1210 if (cam->params.pnp_id.device_type == DEVICE_STV_672) {
1211 cmd.buffer.registers[i++].value=
1212 (u8)(CPIA2_VC_VC_672_CLOCKS_CIF_DIV_BY_3 |
1213 CPIA2_VC_VC_672_CLOCKS_SCALING |
1214 CPIA2_VC_VC_CLOCKS_LOGDIV2);
1215 DBG("VC_Clocks (0xc4) should be B\n");
1218 cmd.buffer.registers[i++].value=
1219 (u8)(CPIA2_VC_VC_676_CLOCKS_CIF_DIV_BY_3 |
1220 CPIA2_VC_VC_CLOCKS_LOGDIV2);
1223 if (cam->params.pnp_id.device_type == DEVICE_STV_672) {
1224 cmd.buffer.registers[i++].value =
1225 (u8) (CPIA2_VC_VC_672_CLOCKS_CIF_DIV_BY_3 |
1226 CPIA2_VC_VC_CLOCKS_LOGDIV0);
1229 cmd.buffer.registers[i++].value =
1230 (u8) (CPIA2_VC_VC_676_CLOCKS_CIF_DIV_BY_3 |
1231 CPIA2_VC_VC_676_CLOCKS_SCALING |
1232 CPIA2_VC_VC_CLOCKS_LOGDIV0);
1235 DBG("VC_Clocks (0xc4) = 0x%0X\n", cmd.buffer.registers[i-1].value);
1237 /* Input reqWidth from VC */
1238 cmd.buffer.registers[i].index = CPIA2_VC_VC_IHSIZE_LO;
1239 if (image_type == VIDEOSIZE_QCIF)
1240 cmd.buffer.registers[i++].value =
1241 (u8) (STV_IMAGE_QCIF_COLS / 4);
1243 cmd.buffer.registers[i++].value =
1244 (u8) (STV_IMAGE_CIF_COLS / 4);
1247 cmd.buffer.registers[i].index = CPIA2_VC_VC_XLIM_HI;
1248 if (image_type == VIDEOSIZE_QCIF)
1249 cmd.buffer.registers[i++].value = (u8) 0;
1251 cmd.buffer.registers[i++].value = (u8) 1;
1253 cmd.buffer.registers[i].index = CPIA2_VC_VC_XLIM_LO;
1254 if (image_type == VIDEOSIZE_QCIF)
1255 cmd.buffer.registers[i++].value = (u8) 208;
1257 cmd.buffer.registers[i++].value = (u8) 160;
1259 cmd.buffer.registers[i].index = CPIA2_VC_VC_YLIM_HI;
1260 if (image_type == VIDEOSIZE_QCIF)
1261 cmd.buffer.registers[i++].value = (u8) 0;
1263 cmd.buffer.registers[i++].value = (u8) 1;
1265 cmd.buffer.registers[i].index = CPIA2_VC_VC_YLIM_LO;
1266 if (image_type == VIDEOSIZE_QCIF)
1267 cmd.buffer.registers[i++].value = (u8) 160;
1269 cmd.buffer.registers[i++].value = (u8) 64;
1271 /* Output Image Size */
1272 cmd.buffer.registers[i].index = CPIA2_VC_VC_OHSIZE;
1273 cmd.buffer.registers[i++].value = cam->params.roi.width / 4;
1275 cmd.buffer.registers[i].index = CPIA2_VC_VC_OVSIZE;
1276 cmd.buffer.registers[i++].value = cam->params.roi.height / 4;
1279 cmd.buffer.registers[i].index = CPIA2_VC_VC_HCROP;
1280 if (image_type == VIDEOSIZE_QCIF)
1281 cmd.buffer.registers[i++].value =
1282 (u8) (((STV_IMAGE_QCIF_COLS / 4) - (width / 4)) / 2);
1284 cmd.buffer.registers[i++].value =
1285 (u8) (((STV_IMAGE_CIF_COLS / 4) - (width / 4)) / 2);
1287 cmd.buffer.registers[i].index = CPIA2_VC_VC_VCROP;
1288 if (image_type == VIDEOSIZE_QCIF)
1289 cmd.buffer.registers[i++].value =
1290 (u8) (((STV_IMAGE_QCIF_ROWS / 4) - (height / 4)) / 2);
1292 cmd.buffer.registers[i++].value =
1293 (u8) (((STV_IMAGE_CIF_ROWS / 4) - (height / 4)) / 2);
1295 /* Scaling registers (defaults) */
1296 cmd.buffer.registers[i].index = CPIA2_VC_VC_HPHASE;
1297 cmd.buffer.registers[i++].value = (u8) 0;
1299 cmd.buffer.registers[i].index = CPIA2_VC_VC_VPHASE;
1300 cmd.buffer.registers[i++].value = (u8) 0;
1302 cmd.buffer.registers[i].index = CPIA2_VC_VC_HISPAN;
1303 cmd.buffer.registers[i++].value = (u8) 31;
1305 cmd.buffer.registers[i].index = CPIA2_VC_VC_VISPAN;
1306 cmd.buffer.registers[i++].value = (u8) 31;
1308 cmd.buffer.registers[i].index = CPIA2_VC_VC_HICROP;
1309 cmd.buffer.registers[i++].value = (u8) 0;
1311 cmd.buffer.registers[i].index = CPIA2_VC_VC_VICROP;
1312 cmd.buffer.registers[i++].value = (u8) 0;
1314 cmd.buffer.registers[i].index = CPIA2_VC_VC_HFRACT;
1315 cmd.buffer.registers[i++].value = (u8) 0x81; /* = 8/1 = 8 (HIBYTE/LOBYTE) */
1317 cmd.buffer.registers[i].index = CPIA2_VC_VC_VFRACT;
1318 cmd.buffer.registers[i++].value = (u8) 0x81; /* = 8/1 = 8 (HIBYTE/LOBYTE) */
1322 cpia2_send_command(cam, &cmd);
1328 /******************************************************************************
1330 * config_sensor_500(cam)
1332 *****************************************************************************/
1333 static int config_sensor_500(struct camera_data *cam,
1334 int req_width, int req_height)
1336 struct cpia2_command cmd;
1338 int image_size = VIDEOSIZE_CIF;
1339 int image_type = VIDEOSIZE_VGA;
1340 int width = req_width;
1341 int height = req_height;
1342 unsigned int device = cam->params.pnp_id.device_type;
1344 image_size = cpia2_match_video_size(width, height);
1346 if (width > STV_IMAGE_CIF_COLS || height > STV_IMAGE_CIF_ROWS)
1347 image_type = VIDEOSIZE_VGA;
1348 else if (width > STV_IMAGE_QVGA_COLS || height > STV_IMAGE_QVGA_ROWS)
1349 image_type = VIDEOSIZE_CIF;
1350 else if (width > STV_IMAGE_QCIF_COLS || height > STV_IMAGE_QCIF_ROWS)
1351 image_type = VIDEOSIZE_QVGA;
1353 image_type = VIDEOSIZE_QCIF;
1355 if (image_size >= 0) {
1356 set_vw_size(cam, image_size);
1357 width = cam->params.roi.width;
1358 height = cam->params.roi.height;
1360 ERR("ConfigSensor500 failed\n");
1364 DBG("image_size = %d, width = %d, height = %d, type = %d\n",
1365 image_size, width, height, image_type);
1367 cmd.req_mode = CAMERAACCESS_TYPE_RANDOM | CAMERAACCESS_VC;
1368 cmd.direction = TRANSFER_WRITE;
1372 cmd.buffer.registers[i].index = CPIA2_VC_VC_FORMAT;
1373 cmd.buffer.registers[i].value = (u8) CPIA2_VC_VC_FORMAT_UFIRST;
1374 if (image_type == VIDEOSIZE_QCIF)
1375 cmd.buffer.registers[i].value |= (u8) CPIA2_VC_VC_FORMAT_DECIMATING;
1379 cmd.buffer.registers[i].index = CPIA2_VC_VC_CLOCKS;
1380 if (device == DEVICE_STV_672) {
1381 if (image_type == VIDEOSIZE_VGA)
1382 cmd.buffer.registers[i].value =
1383 (u8)CPIA2_VC_VC_CLOCKS_LOGDIV1;
1385 cmd.buffer.registers[i].value =
1386 (u8)(CPIA2_VC_VC_672_CLOCKS_SCALING |
1387 CPIA2_VC_VC_CLOCKS_LOGDIV3);
1389 if (image_type == VIDEOSIZE_VGA)
1390 cmd.buffer.registers[i].value =
1391 (u8)CPIA2_VC_VC_CLOCKS_LOGDIV0;
1393 cmd.buffer.registers[i].value =
1394 (u8)(CPIA2_VC_VC_676_CLOCKS_SCALING |
1395 CPIA2_VC_VC_CLOCKS_LOGDIV2);
1399 DBG("VC_CLOCKS = 0x%X\n", cmd.buffer.registers[i-1].value);
1401 /* Input width from VP */
1402 cmd.buffer.registers[i].index = CPIA2_VC_VC_IHSIZE_LO;
1403 if (image_type == VIDEOSIZE_VGA)
1404 cmd.buffer.registers[i].value =
1405 (u8) (STV_IMAGE_VGA_COLS / 4);
1407 cmd.buffer.registers[i].value =
1408 (u8) (STV_IMAGE_QVGA_COLS / 4);
1410 DBG("Input width = %d\n", cmd.buffer.registers[i-1].value);
1413 cmd.buffer.registers[i].index = CPIA2_VC_VC_XLIM_HI;
1414 if (image_type == VIDEOSIZE_VGA)
1415 cmd.buffer.registers[i++].value = (u8) 2;
1417 cmd.buffer.registers[i++].value = (u8) 1;
1419 cmd.buffer.registers[i].index = CPIA2_VC_VC_XLIM_LO;
1420 if (image_type == VIDEOSIZE_VGA)
1421 cmd.buffer.registers[i++].value = (u8) 250;
1422 else if (image_type == VIDEOSIZE_QVGA)
1423 cmd.buffer.registers[i++].value = (u8) 125;
1425 cmd.buffer.registers[i++].value = (u8) 160;
1427 cmd.buffer.registers[i].index = CPIA2_VC_VC_YLIM_HI;
1428 if (image_type == VIDEOSIZE_VGA)
1429 cmd.buffer.registers[i++].value = (u8) 2;
1431 cmd.buffer.registers[i++].value = (u8) 1;
1433 cmd.buffer.registers[i].index = CPIA2_VC_VC_YLIM_LO;
1434 if (image_type == VIDEOSIZE_VGA)
1435 cmd.buffer.registers[i++].value = (u8) 12;
1436 else if (image_type == VIDEOSIZE_QVGA)
1437 cmd.buffer.registers[i++].value = (u8) 64;
1439 cmd.buffer.registers[i++].value = (u8) 6;
1441 /* Output Image Size */
1442 cmd.buffer.registers[i].index = CPIA2_VC_VC_OHSIZE;
1443 if (image_type == VIDEOSIZE_QCIF)
1444 cmd.buffer.registers[i++].value = STV_IMAGE_CIF_COLS / 4;
1446 cmd.buffer.registers[i++].value = width / 4;
1448 cmd.buffer.registers[i].index = CPIA2_VC_VC_OVSIZE;
1449 if (image_type == VIDEOSIZE_QCIF)
1450 cmd.buffer.registers[i++].value = STV_IMAGE_CIF_ROWS / 4;
1452 cmd.buffer.registers[i++].value = height / 4;
1455 cmd.buffer.registers[i].index = CPIA2_VC_VC_HCROP;
1456 if (image_type == VIDEOSIZE_VGA)
1457 cmd.buffer.registers[i++].value =
1458 (u8) (((STV_IMAGE_VGA_COLS / 4) - (width / 4)) / 2);
1459 else if (image_type == VIDEOSIZE_QVGA)
1460 cmd.buffer.registers[i++].value =
1461 (u8) (((STV_IMAGE_QVGA_COLS / 4) - (width / 4)) / 2);
1462 else if (image_type == VIDEOSIZE_CIF)
1463 cmd.buffer.registers[i++].value =
1464 (u8) (((STV_IMAGE_CIF_COLS / 4) - (width / 4)) / 2);
1465 else /*if (image_type == VIDEOSIZE_QCIF)*/
1466 cmd.buffer.registers[i++].value =
1467 (u8) (((STV_IMAGE_QCIF_COLS / 4) - (width / 4)) / 2);
1469 cmd.buffer.registers[i].index = CPIA2_VC_VC_VCROP;
1470 if (image_type == VIDEOSIZE_VGA)
1471 cmd.buffer.registers[i++].value =
1472 (u8) (((STV_IMAGE_VGA_ROWS / 4) - (height / 4)) / 2);
1473 else if (image_type == VIDEOSIZE_QVGA)
1474 cmd.buffer.registers[i++].value =
1475 (u8) (((STV_IMAGE_QVGA_ROWS / 4) - (height / 4)) / 2);
1476 else if (image_type == VIDEOSIZE_CIF)
1477 cmd.buffer.registers[i++].value =
1478 (u8) (((STV_IMAGE_CIF_ROWS / 4) - (height / 4)) / 2);
1479 else /*if (image_type == VIDEOSIZE_QCIF)*/
1480 cmd.buffer.registers[i++].value =
1481 (u8) (((STV_IMAGE_QCIF_ROWS / 4) - (height / 4)) / 2);
1483 /* Scaling registers (defaults) */
1484 cmd.buffer.registers[i].index = CPIA2_VC_VC_HPHASE;
1485 if (image_type == VIDEOSIZE_CIF || image_type == VIDEOSIZE_QCIF)
1486 cmd.buffer.registers[i++].value = (u8) 36;
1488 cmd.buffer.registers[i++].value = (u8) 0;
1490 cmd.buffer.registers[i].index = CPIA2_VC_VC_VPHASE;
1491 if (image_type == VIDEOSIZE_CIF || image_type == VIDEOSIZE_QCIF)
1492 cmd.buffer.registers[i++].value = (u8) 32;
1494 cmd.buffer.registers[i++].value = (u8) 0;
1496 cmd.buffer.registers[i].index = CPIA2_VC_VC_HISPAN;
1497 if (image_type == VIDEOSIZE_CIF || image_type == VIDEOSIZE_QCIF)
1498 cmd.buffer.registers[i++].value = (u8) 26;
1500 cmd.buffer.registers[i++].value = (u8) 31;
1502 cmd.buffer.registers[i].index = CPIA2_VC_VC_VISPAN;
1503 if (image_type == VIDEOSIZE_CIF || image_type == VIDEOSIZE_QCIF)
1504 cmd.buffer.registers[i++].value = (u8) 21;
1506 cmd.buffer.registers[i++].value = (u8) 31;
1508 cmd.buffer.registers[i].index = CPIA2_VC_VC_HICROP;
1509 cmd.buffer.registers[i++].value = (u8) 0;
1511 cmd.buffer.registers[i].index = CPIA2_VC_VC_VICROP;
1512 cmd.buffer.registers[i++].value = (u8) 0;
1514 cmd.buffer.registers[i].index = CPIA2_VC_VC_HFRACT;
1515 if (image_type == VIDEOSIZE_CIF || image_type == VIDEOSIZE_QCIF)
1516 cmd.buffer.registers[i++].value = (u8) 0x2B; /* 2/11 */
1518 cmd.buffer.registers[i++].value = (u8) 0x81; /* 8/1 */
1520 cmd.buffer.registers[i].index = CPIA2_VC_VC_VFRACT;
1521 if (image_type == VIDEOSIZE_CIF || image_type == VIDEOSIZE_QCIF)
1522 cmd.buffer.registers[i++].value = (u8) 0x13; /* 1/3 */
1524 cmd.buffer.registers[i++].value = (u8) 0x81; /* 8/1 */
1528 cpia2_send_command(cam, &cmd);
1534 /******************************************************************************
1538 * This sets all user changeable properties to the values in cam->params.
1539 *****************************************************************************/
1540 int set_all_properties(struct camera_data *cam)
1543 * Don't set target_kb here, it will be set later.
1544 * framerate and user_mode were already set (set_default_user_mode).
1547 cpia2_set_color_params(cam);
1549 cpia2_usb_change_streaming_alternate(cam,
1550 cam->params.camera_state.stream_mode);
1552 cpia2_do_command(cam, CPIA2_CMD_SET_USER_EFFECTS, TRANSFER_WRITE,
1553 cam->params.vp_params.user_effects);
1555 cpia2_set_flicker_mode(cam,
1556 cam->params.flicker_control.flicker_mode_req);
1558 cpia2_do_command(cam,
1559 CPIA2_CMD_SET_VC_MP_GPIO_DIRECTION,
1560 TRANSFER_WRITE, cam->params.vp_params.gpio_direction);
1561 cpia2_do_command(cam, CPIA2_CMD_SET_VC_MP_GPIO_DATA, TRANSFER_WRITE,
1562 cam->params.vp_params.gpio_data);
1566 set_lowlight_boost(cam);
1571 /******************************************************************************
1573 * cpia2_save_camera_state
1575 *****************************************************************************/
1576 void cpia2_save_camera_state(struct camera_data *cam)
1578 get_color_params(cam);
1579 cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS, TRANSFER_READ, 0);
1580 cpia2_do_command(cam, CPIA2_CMD_GET_VC_MP_GPIO_DIRECTION, TRANSFER_READ,
1582 cpia2_do_command(cam, CPIA2_CMD_GET_VC_MP_GPIO_DATA, TRANSFER_READ, 0);
1583 /* Don't get framerate or target_kb. Trust the values we already have */
1586 /******************************************************************************
1590 *****************************************************************************/
1591 void get_color_params(struct camera_data *cam)
1593 cpia2_do_command(cam, CPIA2_CMD_GET_VP_BRIGHTNESS, TRANSFER_READ, 0);
1594 cpia2_do_command(cam, CPIA2_CMD_GET_VP_SATURATION, TRANSFER_READ, 0);
1595 cpia2_do_command(cam, CPIA2_CMD_GET_CONTRAST, TRANSFER_READ, 0);
1598 /******************************************************************************
1600 * cpia2_set_color_params
1602 *****************************************************************************/
1603 void cpia2_set_color_params(struct camera_data *cam)
1605 DBG("Setting color params\n");
1606 cpia2_set_brightness(cam, cam->params.color_params.brightness);
1607 cpia2_set_contrast(cam, cam->params.color_params.contrast);
1608 cpia2_set_saturation(cam, cam->params.color_params.saturation);
1611 /******************************************************************************
1613 * cpia2_set_flicker_mode
1615 *****************************************************************************/
1616 int cpia2_set_flicker_mode(struct camera_data *cam, int mode)
1618 unsigned char cam_reg;
1621 if(cam->params.pnp_id.device_type != DEVICE_STV_672)
1624 /* Set the appropriate bits in FLICKER_MODES, preserving the rest */
1625 if((err = cpia2_do_command(cam, CPIA2_CMD_GET_FLICKER_MODES,
1628 cam_reg = cam->params.flicker_control.cam_register;
1632 cam_reg |= CPIA2_VP_FLICKER_MODES_NEVER_FLICKER;
1633 cam_reg &= ~CPIA2_VP_FLICKER_MODES_50HZ;
1636 cam_reg &= ~CPIA2_VP_FLICKER_MODES_NEVER_FLICKER;
1637 cam_reg &= ~CPIA2_VP_FLICKER_MODES_50HZ;
1640 cam_reg &= ~CPIA2_VP_FLICKER_MODES_NEVER_FLICKER;
1641 cam_reg |= CPIA2_VP_FLICKER_MODES_50HZ;
1647 if((err = cpia2_do_command(cam, CPIA2_CMD_SET_FLICKER_MODES,
1648 TRANSFER_WRITE, cam_reg)))
1651 /* Set the appropriate bits in EXP_MODES, preserving the rest */
1652 if((err = cpia2_do_command(cam, CPIA2_CMD_GET_VP_EXP_MODES,
1655 cam_reg = cam->params.vp_params.exposure_modes;
1657 if (mode == NEVER_FLICKER) {
1658 cam_reg |= CPIA2_VP_EXPOSURE_MODES_INHIBIT_FLICKER;
1660 cam_reg &= ~CPIA2_VP_EXPOSURE_MODES_INHIBIT_FLICKER;
1663 if((err = cpia2_do_command(cam, CPIA2_CMD_SET_VP_EXP_MODES,
1664 TRANSFER_WRITE, cam_reg)))
1667 if((err = cpia2_do_command(cam, CPIA2_CMD_REHASH_VP4,
1668 TRANSFER_WRITE, 1)))
1673 cam->params.flicker_control.flicker_mode_req = mode;
1676 cam->params.flicker_control.flicker_mode_req = mode;
1677 cam->params.flicker_control.mains_frequency = 60;
1680 cam->params.flicker_control.flicker_mode_req = mode;
1681 cam->params.flicker_control.mains_frequency = 50;
1690 /******************************************************************************
1692 * cpia2_set_property_flip
1694 *****************************************************************************/
1695 void cpia2_set_property_flip(struct camera_data *cam, int prop_val)
1697 unsigned char cam_reg;
1699 cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS, TRANSFER_READ, 0);
1700 cam_reg = cam->params.vp_params.user_effects;
1704 cam_reg |= CPIA2_VP_USER_EFFECTS_FLIP;
1708 cam_reg &= ~CPIA2_VP_USER_EFFECTS_FLIP;
1710 cpia2_do_command(cam, CPIA2_CMD_SET_USER_EFFECTS, TRANSFER_WRITE,
1714 /******************************************************************************
1716 * cpia2_set_property_mirror
1718 *****************************************************************************/
1719 void cpia2_set_property_mirror(struct camera_data *cam, int prop_val)
1721 unsigned char cam_reg;
1723 cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS, TRANSFER_READ, 0);
1724 cam_reg = cam->params.vp_params.user_effects;
1728 cam_reg |= CPIA2_VP_USER_EFFECTS_MIRROR;
1732 cam_reg &= ~CPIA2_VP_USER_EFFECTS_MIRROR;
1734 cpia2_do_command(cam, CPIA2_CMD_SET_USER_EFFECTS, TRANSFER_WRITE,
1738 /******************************************************************************
1742 * The new Target KB is set in cam->params.vc_params.target_kb and
1743 * activates on reset.
1744 *****************************************************************************/
1746 int cpia2_set_target_kb(struct camera_data *cam, unsigned char value)
1748 DBG("Requested target_kb = %d\n", value);
1749 if (value != cam->params.vc_params.target_kb) {
1751 cpia2_usb_stream_pause(cam);
1753 /* reset camera for new target_kb */
1754 cam->params.vc_params.target_kb = value;
1755 cpia2_reset_camera(cam);
1757 cpia2_usb_stream_resume(cam);
1763 /******************************************************************************
1767 *****************************************************************************/
1768 int cpia2_set_gpio(struct camera_data *cam, unsigned char setting)
1772 /* Set the microport direction (register 0x90, should be defined
1773 * already) to 1 (user output), and set the microport data (0x91) to
1774 * the value in the ioctl argument.
1777 ret = cpia2_do_command(cam,
1778 CPIA2_CMD_SET_VC_MP_GPIO_DIRECTION,
1779 CPIA2_VC_MP_DIR_OUTPUT,
1783 cam->params.vp_params.gpio_direction = 255;
1785 ret = cpia2_do_command(cam,
1786 CPIA2_CMD_SET_VC_MP_GPIO_DATA,
1787 CPIA2_VC_MP_DIR_OUTPUT,
1791 cam->params.vp_params.gpio_data = setting;
1796 /******************************************************************************
1800 *****************************************************************************/
1801 int cpia2_set_fps(struct camera_data *cam, int framerate)
1806 case CPIA2_VP_FRAMERATE_30:
1807 case CPIA2_VP_FRAMERATE_25:
1808 if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
1809 cam->params.version.sensor_flags ==
1810 CPIA2_VP_SENSOR_FLAGS_500) {
1814 case CPIA2_VP_FRAMERATE_15:
1815 case CPIA2_VP_FRAMERATE_12_5:
1816 case CPIA2_VP_FRAMERATE_7_5:
1817 case CPIA2_VP_FRAMERATE_6_25:
1823 if (cam->params.pnp_id.device_type == DEVICE_STV_672 &&
1824 framerate == CPIA2_VP_FRAMERATE_15)
1825 framerate = 0; /* Work around bug in VP4 */
1827 retval = cpia2_do_command(cam,
1828 CPIA2_CMD_FRAMERATE_REQ,
1833 cam->params.vp_params.frame_rate = framerate;
1838 /******************************************************************************
1840 * cpia2_set_brightness
1842 *****************************************************************************/
1843 void cpia2_set_brightness(struct camera_data *cam, unsigned char value)
1846 * Don't let the register be set to zero - bug in VP4 - flash of full
1849 if (cam->params.pnp_id.device_type == DEVICE_STV_672 && value == 0)
1851 DBG("Setting brightness to %d (0x%0x)\n", value, value);
1852 cpia2_do_command(cam,CPIA2_CMD_SET_VP_BRIGHTNESS, TRANSFER_WRITE,value);
1855 /******************************************************************************
1857 * cpia2_set_contrast
1859 *****************************************************************************/
1860 void cpia2_set_contrast(struct camera_data *cam, unsigned char value)
1862 DBG("Setting contrast to %d (0x%0x)\n", value, value);
1863 cam->params.color_params.contrast = value;
1864 cpia2_do_command(cam, CPIA2_CMD_SET_CONTRAST, TRANSFER_WRITE, value);
1867 /******************************************************************************
1869 * cpia2_set_saturation
1871 *****************************************************************************/
1872 void cpia2_set_saturation(struct camera_data *cam, unsigned char value)
1874 DBG("Setting saturation to %d (0x%0x)\n", value, value);
1875 cam->params.color_params.saturation = value;
1876 cpia2_do_command(cam,CPIA2_CMD_SET_VP_SATURATION, TRANSFER_WRITE,value);
1879 /******************************************************************************
1883 *****************************************************************************/
1884 void wake_system(struct camera_data *cam)
1886 cpia2_do_command(cam, CPIA2_CMD_SET_WAKEUP, TRANSFER_WRITE, 0);
1889 /******************************************************************************
1891 * set_lowlight_boost
1893 * Valid for STV500 sensor only
1894 *****************************************************************************/
1895 void set_lowlight_boost(struct camera_data *cam)
1897 struct cpia2_command cmd;
1899 if (cam->params.pnp_id.device_type != DEVICE_STV_672 ||
1900 cam->params.version.sensor_flags != CPIA2_VP_SENSOR_FLAGS_500)
1903 cmd.direction = TRANSFER_WRITE;
1904 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
1906 cmd.start = CPIA2_VP_RAM_ADDR_H;
1908 cmd.buffer.block_data[0] = 0; /* High byte of address to write to */
1909 cmd.buffer.block_data[1] = 0x59; /* Low byte of address to write to */
1910 cmd.buffer.block_data[2] = 0; /* High byte of data to write */
1912 cpia2_send_command(cam, &cmd);
1914 if (cam->params.vp_params.lowlight_boost) {
1915 cmd.buffer.block_data[0] = 0x02; /* Low byte data to write */
1917 cmd.buffer.block_data[0] = 0x06;
1919 cmd.start = CPIA2_VP_RAM_DATA;
1921 cpia2_send_command(cam, &cmd);
1923 /* Rehash the VP4 values */
1924 cpia2_do_command(cam, CPIA2_CMD_REHASH_VP4, TRANSFER_WRITE, 1);
1927 /******************************************************************************
1931 * Assumes that new size is already set in param struct.
1932 *****************************************************************************/
1933 void cpia2_set_format(struct camera_data *cam)
1937 cpia2_usb_stream_pause(cam);
1939 /* reset camera to new size */
1940 cpia2_set_low_power(cam);
1941 cpia2_reset_camera(cam);
1944 cpia2_dbg_dump_registers(cam);
1946 cpia2_usb_stream_resume(cam);
1949 /******************************************************************************
1951 * cpia2_dbg_dump_registers
1953 *****************************************************************************/
1954 void cpia2_dbg_dump_registers(struct camera_data *cam)
1956 #ifdef _CPIA2_DEBUG_
1957 struct cpia2_command cmd;
1959 if (!(debugs_on & DEBUG_DUMP_REGS))
1962 cmd.direction = TRANSFER_READ;
1964 /* Start with bank 0 (SYSTEM) */
1965 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_SYSTEM;
1968 cpia2_send_command(cam, &cmd);
1969 printk(KERN_DEBUG "System Device Hi = 0x%X\n",
1970 cmd.buffer.block_data[0]);
1971 printk(KERN_DEBUG "System Device Lo = 0x%X\n",
1972 cmd.buffer.block_data[1]);
1973 printk(KERN_DEBUG "System_system control = 0x%X\n",
1974 cmd.buffer.block_data[2]);
1977 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VC;
1980 cpia2_send_command(cam, &cmd);
1981 printk(KERN_DEBUG "ASIC_ID = 0x%X\n",
1982 cmd.buffer.block_data[0]);
1983 printk(KERN_DEBUG "ASIC_REV = 0x%X\n",
1984 cmd.buffer.block_data[1]);
1985 printk(KERN_DEBUG "PW_CONTRL = 0x%X\n",
1986 cmd.buffer.block_data[2]);
1987 printk(KERN_DEBUG "WAKEUP = 0x%X\n",
1988 cmd.buffer.block_data[3]);
1990 cmd.start = 0xA0; /* ST_CTRL */
1992 cpia2_send_command(cam, &cmd);
1993 printk(KERN_DEBUG "Stream ctrl = 0x%X\n",
1994 cmd.buffer.block_data[0]);
1996 cmd.start = 0xA4; /* Stream status */
1997 cpia2_send_command(cam, &cmd);
1998 printk(KERN_DEBUG "Stream status = 0x%X\n",
1999 cmd.buffer.block_data[0]);
2001 cmd.start = 0xA8; /* USB status */
2003 cpia2_send_command(cam, &cmd);
2004 printk(KERN_DEBUG "USB_CTRL = 0x%X\n",
2005 cmd.buffer.block_data[0]);
2006 printk(KERN_DEBUG "USB_STRM = 0x%X\n",
2007 cmd.buffer.block_data[1]);
2008 printk(KERN_DEBUG "USB_STATUS = 0x%X\n",
2009 cmd.buffer.block_data[2]);
2011 cmd.start = 0xAF; /* USB settings */
2013 cpia2_send_command(cam, &cmd);
2014 printk(KERN_DEBUG "USB settings = 0x%X\n",
2015 cmd.buffer.block_data[0]);
2017 cmd.start = 0xC0; /* VC stuff */
2019 cpia2_send_command(cam, &cmd);
2020 printk(KERN_DEBUG "VC Control = 0x%0X\n",
2021 cmd.buffer.block_data[0]);
2022 printk(KERN_DEBUG "VC Format = 0x%0X\n",
2023 cmd.buffer.block_data[3]);
2024 printk(KERN_DEBUG "VC Clocks = 0x%0X\n",
2025 cmd.buffer.block_data[4]);
2026 printk(KERN_DEBUG "VC IHSize = 0x%0X\n",
2027 cmd.buffer.block_data[5]);
2028 printk(KERN_DEBUG "VC Xlim Hi = 0x%0X\n",
2029 cmd.buffer.block_data[6]);
2030 printk(KERN_DEBUG "VC XLim Lo = 0x%0X\n",
2031 cmd.buffer.block_data[7]);
2032 printk(KERN_DEBUG "VC YLim Hi = 0x%0X\n",
2033 cmd.buffer.block_data[8]);
2034 printk(KERN_DEBUG "VC YLim Lo = 0x%0X\n",
2035 cmd.buffer.block_data[9]);
2036 printk(KERN_DEBUG "VC OHSize = 0x%0X\n",
2037 cmd.buffer.block_data[10]);
2038 printk(KERN_DEBUG "VC OVSize = 0x%0X\n",
2039 cmd.buffer.block_data[11]);
2040 printk(KERN_DEBUG "VC HCrop = 0x%0X\n",
2041 cmd.buffer.block_data[12]);
2042 printk(KERN_DEBUG "VC VCrop = 0x%0X\n",
2043 cmd.buffer.block_data[13]);
2044 printk(KERN_DEBUG "VC HPhase = 0x%0X\n",
2045 cmd.buffer.block_data[14]);
2046 printk(KERN_DEBUG "VC VPhase = 0x%0X\n",
2047 cmd.buffer.block_data[15]);
2048 printk(KERN_DEBUG "VC HIspan = 0x%0X\n",
2049 cmd.buffer.block_data[16]);
2050 printk(KERN_DEBUG "VC VIspan = 0x%0X\n",
2051 cmd.buffer.block_data[17]);
2052 printk(KERN_DEBUG "VC HiCrop = 0x%0X\n",
2053 cmd.buffer.block_data[18]);
2054 printk(KERN_DEBUG "VC ViCrop = 0x%0X\n",
2055 cmd.buffer.block_data[19]);
2056 printk(KERN_DEBUG "VC HiFract = 0x%0X\n",
2057 cmd.buffer.block_data[20]);
2058 printk(KERN_DEBUG "VC ViFract = 0x%0X\n",
2059 cmd.buffer.block_data[21]);
2060 printk(KERN_DEBUG "VC JPeg Opt = 0x%0X\n",
2061 cmd.buffer.block_data[22]);
2062 printk(KERN_DEBUG "VC Creep Per = 0x%0X\n",
2063 cmd.buffer.block_data[23]);
2064 printk(KERN_DEBUG "VC User Sq. = 0x%0X\n",
2065 cmd.buffer.block_data[24]);
2066 printk(KERN_DEBUG "VC Target KB = 0x%0X\n",
2067 cmd.buffer.block_data[25]);
2070 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
2073 cpia2_send_command(cam, &cmd);
2075 printk(KERN_DEBUG "VP Dev Hi = 0x%0X\n",
2076 cmd.buffer.block_data[0]);
2077 printk(KERN_DEBUG "VP Dev Lo = 0x%0X\n",
2078 cmd.buffer.block_data[1]);
2079 printk(KERN_DEBUG "VP Sys State = 0x%0X\n",
2080 cmd.buffer.block_data[2]);
2081 printk(KERN_DEBUG "VP Sys Ctrl = 0x%0X\n",
2082 cmd.buffer.block_data[3]);
2083 printk(KERN_DEBUG "VP Sensor flg = 0x%0X\n",
2084 cmd.buffer.block_data[5]);
2085 printk(KERN_DEBUG "VP Sensor Rev = 0x%0X\n",
2086 cmd.buffer.block_data[6]);
2087 printk(KERN_DEBUG "VP Dev Config = 0x%0X\n",
2088 cmd.buffer.block_data[7]);
2089 printk(KERN_DEBUG "VP GPIO_DIR = 0x%0X\n",
2090 cmd.buffer.block_data[8]);
2091 printk(KERN_DEBUG "VP GPIO_DATA = 0x%0X\n",
2092 cmd.buffer.block_data[9]);
2093 printk(KERN_DEBUG "VP Ram ADDR H = 0x%0X\n",
2094 cmd.buffer.block_data[10]);
2095 printk(KERN_DEBUG "VP Ram ADDR L = 0x%0X\n",
2096 cmd.buffer.block_data[11]);
2097 printk(KERN_DEBUG "VP RAM Data = 0x%0X\n",
2098 cmd.buffer.block_data[12]);
2099 printk(KERN_DEBUG "Do Call = 0x%0X\n",
2100 cmd.buffer.block_data[13]);
2102 if (cam->params.pnp_id.device_type == DEVICE_STV_672) {
2105 cpia2_send_command(cam, &cmd);
2106 printk(KERN_DEBUG "VP Clock Ctrl = 0x%0X\n",
2107 cmd.buffer.block_data[0]);
2108 printk(KERN_DEBUG "VP Patch Rev = 0x%0X\n",
2109 cmd.buffer.block_data[1]);
2110 printk(KERN_DEBUG "VP Vid Mode = 0x%0X\n",
2111 cmd.buffer.block_data[2]);
2112 printk(KERN_DEBUG "VP Framerate = 0x%0X\n",
2113 cmd.buffer.block_data[3]);
2114 printk(KERN_DEBUG "VP UserEffect = 0x%0X\n",
2115 cmd.buffer.block_data[4]);
2116 printk(KERN_DEBUG "VP White Bal = 0x%0X\n",
2117 cmd.buffer.block_data[5]);
2118 printk(KERN_DEBUG "VP WB thresh = 0x%0X\n",
2119 cmd.buffer.block_data[6]);
2120 printk(KERN_DEBUG "VP Exp Modes = 0x%0X\n",
2121 cmd.buffer.block_data[7]);
2122 printk(KERN_DEBUG "VP Exp Target = 0x%0X\n",
2123 cmd.buffer.block_data[8]);
2127 cpia2_send_command(cam, &cmd);
2128 printk(KERN_DEBUG "VP FlickerMds = 0x%0X\n",
2129 cmd.buffer.block_data[0]);
2133 cpia2_send_command(cam, &cmd);
2134 printk(KERN_DEBUG "VP Clock Ctrl = 0x%0X\n",
2135 cmd.buffer.block_data[0]);
2136 printk(KERN_DEBUG "VP Patch Rev = 0x%0X\n",
2137 cmd.buffer.block_data[1]);
2138 printk(KERN_DEBUG "VP Vid Mode = 0x%0X\n",
2139 cmd.buffer.block_data[5]);
2140 printk(KERN_DEBUG "VP Framerate = 0x%0X\n",
2141 cmd.buffer.block_data[6]);
2142 printk(KERN_DEBUG "VP UserEffect = 0x%0X\n",
2143 cmd.buffer.block_data[7]);
2146 cmd.start = CPIA2_VP5_EXPOSURE_TARGET;
2147 cpia2_send_command(cam, &cmd);
2148 printk(KERN_DEBUG "VP5 Exp Target= 0x%0X\n",
2149 cmd.buffer.block_data[0]);
2153 cpia2_send_command(cam, &cmd);
2154 printk(KERN_DEBUG "VP5 MY Black = 0x%0X\n",
2155 cmd.buffer.block_data[0]);
2156 printk(KERN_DEBUG "VP5 MCY Range = 0x%0X\n",
2157 cmd.buffer.block_data[1]);
2158 printk(KERN_DEBUG "VP5 MYCEILING = 0x%0X\n",
2159 cmd.buffer.block_data[2]);
2160 printk(KERN_DEBUG "VP5 MCUV Sat = 0x%0X\n",
2161 cmd.buffer.block_data[3]);
2166 /******************************************************************************
2168 * reset_camera_struct
2170 * Sets all values to the defaults
2171 *****************************************************************************/
2172 void reset_camera_struct(struct camera_data *cam)
2175 * The following parameter values are the defaults from the register map.
2177 cam->params.color_params.brightness = DEFAULT_BRIGHTNESS;
2178 cam->params.color_params.contrast = DEFAULT_CONTRAST;
2179 cam->params.color_params.saturation = DEFAULT_SATURATION;
2180 cam->params.vp_params.lowlight_boost = 0;
2183 cam->params.flicker_control.flicker_mode_req = NEVER_FLICKER;
2184 cam->params.flicker_control.mains_frequency = 60;
2187 cam->params.compression.jpeg_options = CPIA2_VC_VC_JPEG_OPT_DEFAULT;
2188 cam->params.compression.creep_period = 2;
2189 cam->params.compression.user_squeeze = 20;
2190 cam->params.compression.inhibit_htables = false;
2193 cam->params.vp_params.gpio_direction = 0; /* write, the default safe mode */
2194 cam->params.vp_params.gpio_data = 0;
2196 /* Target kb params */
2197 cam->params.vc_params.target_kb = DEFAULT_TARGET_KB;
2200 * Set Sensor FPS as fast as possible.
2202 if(cam->params.pnp_id.device_type == DEVICE_STV_672) {
2203 if(cam->params.version.sensor_flags == CPIA2_VP_SENSOR_FLAGS_500)
2204 cam->params.vp_params.frame_rate = CPIA2_VP_FRAMERATE_15;
2206 cam->params.vp_params.frame_rate = CPIA2_VP_FRAMERATE_30;
2208 cam->params.vp_params.frame_rate = CPIA2_VP_FRAMERATE_30;
2212 * Set default video mode as large as possible :
2213 * for vga sensor set to vga, for cif sensor set to CIF.
2215 if (cam->params.version.sensor_flags == CPIA2_VP_SENSOR_FLAGS_500) {
2216 cam->sensor_type = CPIA2_SENSOR_500;
2217 cam->video_size = VIDEOSIZE_VGA;
2218 cam->params.roi.width = STV_IMAGE_VGA_COLS;
2219 cam->params.roi.height = STV_IMAGE_VGA_ROWS;
2221 cam->sensor_type = CPIA2_SENSOR_410;
2222 cam->video_size = VIDEOSIZE_CIF;
2223 cam->params.roi.width = STV_IMAGE_CIF_COLS;
2224 cam->params.roi.height = STV_IMAGE_CIF_ROWS;
2228 * Fill in the v4l structures. video_cap is filled in inside the VIDIOCCAP
2229 * Ioctl. Here, just do the window and picture stucts.
2231 cam->vp.palette = (u16) VIDEO_PALETTE_RGB24; /* Is this right? */
2232 cam->vp.brightness = (u16) cam->params.color_params.brightness * 256;
2233 cam->vp.colour = (u16) cam->params.color_params.saturation * 256;
2234 cam->vp.contrast = (u16) cam->params.color_params.contrast * 256;
2238 cam->vw.width = cam->params.roi.width;
2239 cam->vw.height = cam->params.roi.height;
2241 cam->vw.clipcount = 0;
2246 /******************************************************************************
2248 * cpia2_init_camera_struct
2250 * Initializes camera struct, does not call reset to fill in defaults.
2251 *****************************************************************************/
2252 struct camera_data *cpia2_init_camera_struct(void)
2254 struct camera_data *cam;
2256 cam = kzalloc(sizeof(*cam), GFP_KERNEL);
2259 ERR("couldn't kmalloc cpia2 struct\n");
2265 mutex_init(&cam->busy_lock);
2266 init_waitqueue_head(&cam->wq_stream);
2271 /******************************************************************************
2275 * Initializes camera.
2276 *****************************************************************************/
2277 int cpia2_init_camera(struct camera_data *cam)
2281 cam->mmapped = false;
2283 /* Get sensor and asic types before reset. */
2284 cpia2_set_high_power(cam);
2285 cpia2_get_version_info(cam);
2286 if (cam->params.version.asic_id != CPIA2_ASIC_672) {
2287 ERR("Device IO error (asicID has incorrect value of 0x%X\n",
2288 cam->params.version.asic_id);
2292 /* Set GPIO direction and data to a safe state. */
2293 cpia2_do_command(cam, CPIA2_CMD_SET_VC_MP_GPIO_DIRECTION,
2295 cpia2_do_command(cam, CPIA2_CMD_SET_VC_MP_GPIO_DATA,
2298 /* resetting struct requires version info for sensor and asic types */
2299 reset_camera_struct(cam);
2301 cpia2_set_low_power(cam);
2308 /******************************************************************************
2310 * cpia2_allocate_buffers
2312 *****************************************************************************/
2313 int cpia2_allocate_buffers(struct camera_data *cam)
2318 u32 size = cam->num_frames*sizeof(struct framebuf);
2319 cam->buffers = kmalloc(size, GFP_KERNEL);
2321 ERR("couldn't kmalloc frame buffer structures\n");
2326 if(!cam->frame_buffer) {
2327 cam->frame_buffer = rvmalloc(cam->frame_size*cam->num_frames);
2328 if (!cam->frame_buffer) {
2329 ERR("couldn't vmalloc frame buffer data area\n");
2330 kfree(cam->buffers);
2331 cam->buffers = NULL;
2336 for(i=0; i<cam->num_frames-1; ++i) {
2337 cam->buffers[i].next = &cam->buffers[i+1];
2338 cam->buffers[i].data = cam->frame_buffer +i*cam->frame_size;
2339 cam->buffers[i].status = FRAME_EMPTY;
2340 cam->buffers[i].length = 0;
2341 cam->buffers[i].max_length = 0;
2342 cam->buffers[i].num = i;
2344 cam->buffers[i].next = cam->buffers;
2345 cam->buffers[i].data = cam->frame_buffer +i*cam->frame_size;
2346 cam->buffers[i].status = FRAME_EMPTY;
2347 cam->buffers[i].length = 0;
2348 cam->buffers[i].max_length = 0;
2349 cam->buffers[i].num = i;
2350 cam->curbuff = cam->buffers;
2351 cam->workbuff = cam->curbuff->next;
2352 DBG("buffers=%p, curbuff=%p, workbuff=%p\n", cam->buffers, cam->curbuff,
2357 /******************************************************************************
2359 * cpia2_free_buffers
2361 *****************************************************************************/
2362 void cpia2_free_buffers(struct camera_data *cam)
2365 kfree(cam->buffers);
2366 cam->buffers = NULL;
2368 if(cam->frame_buffer) {
2369 rvfree(cam->frame_buffer, cam->frame_size*cam->num_frames);
2370 cam->frame_buffer = NULL;
2374 /******************************************************************************
2378 *****************************************************************************/
2379 long cpia2_read(struct camera_data *cam,
2380 char __user *buf, unsigned long count, int noblock)
2382 struct framebuf *frame;
2388 ERR("%s: buffer NULL\n",__func__);
2393 ERR("%s: Internal error, camera_data NULL!\n",__func__);
2397 /* make this _really_ smp and multithread-safe */
2398 if (mutex_lock_interruptible(&cam->busy_lock))
2399 return -ERESTARTSYS;
2401 if (!cam->present) {
2402 LOG("%s: camera removed\n",__func__);
2403 mutex_unlock(&cam->busy_lock);
2407 if(!cam->streaming) {
2408 /* Start streaming */
2409 cpia2_usb_stream_start(cam,
2410 cam->params.camera_state.stream_mode);
2413 /* Copy cam->curbuff in case it changes while we're processing */
2414 frame = cam->curbuff;
2415 if (noblock && frame->status != FRAME_READY) {
2416 mutex_unlock(&cam->busy_lock);
2420 if(frame->status != FRAME_READY) {
2421 mutex_unlock(&cam->busy_lock);
2422 wait_event_interruptible(cam->wq_stream,
2424 (frame = cam->curbuff)->status == FRAME_READY);
2425 if (signal_pending(current))
2426 return -ERESTARTSYS;
2427 /* make this _really_ smp and multithread-safe */
2428 if (mutex_lock_interruptible(&cam->busy_lock)) {
2429 return -ERESTARTSYS;
2432 mutex_unlock(&cam->busy_lock);
2437 /* copy data to user space */
2438 if (frame->length > count) {
2439 mutex_unlock(&cam->busy_lock);
2442 if (copy_to_user(buf, frame->data, frame->length)) {
2443 mutex_unlock(&cam->busy_lock);
2447 count = frame->length;
2449 frame->status = FRAME_EMPTY;
2451 mutex_unlock(&cam->busy_lock);
2455 /******************************************************************************
2459 *****************************************************************************/
2460 unsigned int cpia2_poll(struct camera_data *cam, struct file *filp,
2463 unsigned int status=0;
2466 ERR("%s: Internal error, camera_data not found!\n",__func__);
2470 mutex_lock(&cam->busy_lock);
2473 mutex_unlock(&cam->busy_lock);
2477 if(!cam->streaming) {
2478 /* Start streaming */
2479 cpia2_usb_stream_start(cam,
2480 cam->params.camera_state.stream_mode);
2483 mutex_unlock(&cam->busy_lock);
2484 poll_wait(filp, &cam->wq_stream, wait);
2485 mutex_lock(&cam->busy_lock);
2489 else if(cam->curbuff->status == FRAME_READY)
2490 status = POLLIN | POLLRDNORM;
2492 mutex_unlock(&cam->busy_lock);
2496 /******************************************************************************
2498 * cpia2_remap_buffer
2500 *****************************************************************************/
2501 int cpia2_remap_buffer(struct camera_data *cam, struct vm_area_struct *vma)
2503 const char *adr = (const char *)vma->vm_start;
2504 unsigned long size = vma->vm_end-vma->vm_start;
2505 unsigned long start_offset = vma->vm_pgoff << PAGE_SHIFT;
2506 unsigned long start = (unsigned long) adr;
2507 unsigned long page, pos;
2512 DBG("mmap offset:%ld size:%ld\n", start_offset, size);
2514 /* make this _really_ smp-safe */
2515 if (mutex_lock_interruptible(&cam->busy_lock))
2516 return -ERESTARTSYS;
2518 if (!cam->present) {
2519 mutex_unlock(&cam->busy_lock);
2523 if (size > cam->frame_size*cam->num_frames ||
2524 (start_offset % cam->frame_size) != 0 ||
2525 (start_offset+size > cam->frame_size*cam->num_frames)) {
2526 mutex_unlock(&cam->busy_lock);
2530 pos = ((unsigned long) (cam->frame_buffer)) + start_offset;
2532 page = kvirt_to_pa(pos);
2533 if (remap_pfn_range(vma, start, page >> PAGE_SHIFT, PAGE_SIZE, PAGE_SHARED)) {
2534 mutex_unlock(&cam->busy_lock);
2539 if (size > PAGE_SIZE)
2545 cam->mmapped = true;
2546 mutex_unlock(&cam->busy_lock);