34 #include <linux/module.h>
35 #include <linux/slab.h>
40 #define FIRMWARE "cpia2/stv0672_vp4.bin"
47 static const char *block_name[] = {
55 static unsigned int debugs_on;
67 int reqwidth,
int reqheight);
69 int reqwidth,
int reqheight);
71 int reqwidth,
int reqheight);
82 static inline unsigned long kvirt_to_pa(
unsigned long adr)
84 unsigned long kva,
ret;
92 static void *rvmalloc(
unsigned long size)
105 adr = (
unsigned long) mem;
107 while ((
long)size > 0) {
115 static void rvfree(
void *mem,
unsigned long size)
124 adr = (
unsigned long) mem;
125 while ((
long)size > 0) {
411 LOG(
"DoCommand received invalid command\n");
425 cam->
params.version.firmware_revision_hi =
427 cam->
params.version.firmware_revision_lo =
435 cam->
params.pnp_id.device_revision =
438 if (cam->
params.pnp_id.vendor == 0x553) {
439 if (cam->
params.pnp_id.product == 0x100) {
441 }
else if (cam->
params.pnp_id.product == 0x140 ||
442 cam->
params.pnp_id.product == 0x151) {
472 cam->
params.flicker_control.cam_register =
517 #define DIR(cmd) ((cmd->direction == TRANSFER_WRITE) ? "Write" : "Read")
518 #define BINDEX(cmd) (cmd->req_mode & 0x03)
533 DBG(
"%s Random: Register block %s\n",
DIR(cmd),
540 if (debugs_on & DEBUG_REG)
541 DBG(
"%s Block: Register block %s\n",
DIR(cmd),
548 if (debugs_on & DEBUG_REG)
549 DBG(
"%s Mask: Register block %s\n",
DIR(cmd),
556 if (debugs_on & DEBUG_REG)
557 DBG(
"%s Repeat: Register block %s\n",
DIR(cmd),
561 LOG(
"%s: invalid request mode\n",__func__);
574 KINFO(
"%s Block: [0x%02X] = 0x%02X\n",
575 DIR(cmd), start + i, buffer[i]);
577 KINFO(
"%s Random: [0x%02X] = 0x%02X\n",
621 retval = configure_sensor(cam,
625 ERR(
"Couldn't configure sensor, error=%d\n", retval);
643 cpia2_set_high_power(cam);
658 retval = apply_vp_patch(cam);
707 set_default_user_mode(cam);
712 set_all_properties(cam);
715 DBG(
"After SetAllProperties(cam), user mode is 0x%0X\n",
716 cam->
params.vp_params.video_mode);
725 tmp_reg = cam->
params.vp_params.system_ctrl;
789 if (cam->
params.compression.inhibit_htables) {
790 tmp_reg = cam->
params.vc_params.vc_control |
793 tmp_reg = cam->
params.vc_params.vc_control &
802 target_kb = (cam->
width * cam->
height * 2 / 16384) *
803 cam->
params.vc_params.quality / 100;
813 for (i = 0; i < 50; i++) {
818 tmp_reg = cam->
params.vc_params.pw_control;
829 DBG(
"After VC RESET, user mode is 0x%0X\n",
830 cam->
params.vp_params.video_mode);
843 for (i = 0; i <= 50; i++) {
848 if(cam->
params.camera_state.system_ctrl &
860 if (cam->
params.vp_params.system_state &
863 }
else if (i == 50) {
865 ERR(
"Camera did not wake up\n");
870 DBG(
"System now in high power state\n");
892 static int cpia2_send_onebyte_command(
struct camera_data *cam,
920 cpia2_send_onebyte_command(cam, &
cmd, 0x0A, fw->
data[0]);
921 cpia2_send_onebyte_command(cam, &
cmd, 0x0B, fw->
data[1]);
924 for (i = 2; i < fw->
size; i += 64) {
932 cpia2_send_onebyte_command(cam, &
cmd, 0x0A, fw->
data[0]);
933 cpia2_send_onebyte_command(cam, &
cmd, 0x0B, fw->
data[1]);
936 cpia2_send_onebyte_command(cam, &
cmd, 0x0D, 1);
947 static int set_default_user_mode(
struct camera_data *cam)
950 unsigned char frame_rate;
954 switch (cam->
params.version.sensor_flags) {
980 LOG(
"%s: Invalid sensor flag value 0x%0X\n",__func__,
981 cam->
params.version.sensor_flags);
985 DBG(
"Sensor flag = 0x%0x, user mode = 0x%0x, frame rate = 0x%X\n",
986 cam->
params.version.sensor_flags, user_mode, frame_rate);
989 if(cam->
params.vp_params.frame_rate > 0 &&
990 frame_rate > cam->
params.vp_params.frame_rate)
991 frame_rate = cam->
params.vp_params.frame_rate;
1023 if (width >= 288 && height >= 216)
1026 if (width >= 256 && height >= 192)
1029 if (width >= 224 && height >= 168)
1032 if (width >= 192 && height >= 144)
1046 static int set_vw_size(
struct camera_data *cam,
int size)
1054 DBG(
"Setting size to VGA\n");
1061 DBG(
"Setting size to CIF\n");
1068 DBG(
"Setting size to QVGA\n");
1075 cam->
params.roi.width = 288;
1076 cam->
params.roi.height = 216;
1083 cam->
params.roi.width = 256;
1084 cam->
params.roi.height = 192;
1089 cam->
params.roi.width = 224;
1090 cam->
params.roi.height = 168;
1095 cam->
params.roi.width = 192;
1096 cam->
params.roi.height = 144;
1099 DBG(
"Setting size to QCIF\n");
1116 static int configure_sensor(
struct camera_data *cam,
1117 int req_width,
int req_height)
1121 switch (cam->
params.version.sensor_flags) {
1126 retval = config_sensor_410(cam, req_width, req_height);
1129 retval = config_sensor_500(cam, req_width, req_height);
1143 static int config_sensor_410(
struct camera_data *cam,
1144 int req_width,
int req_height)
1150 int width = req_width;
1151 int height = req_height;
1163 DBG(
"Config 410: width = %d, height = %d\n", width, height);
1164 DBG(
"Image size returned is %d\n", image_size);
1165 if (image_size >= 0) {
1166 set_vw_size(cam, image_size);
1167 width = cam->
params.roi.width;
1168 height = cam->
params.roi.height;
1170 DBG(
"After set_vw_size(), width = %d, height = %d\n",
1172 if (width <= 176 && height <= 144) {
1173 DBG(
"image type = VIDEOSIZE_QCIF\n");
1176 else if (width <= 320 && height <= 240) {
1177 DBG(
"image type = VIDEOSIZE_QVGA\n");
1181 DBG(
"image type = VIDEOSIZE_CIF\n");
1185 ERR(
"ConfigSensor410 failed\n");
1195 cmd.buffer.registers[i++].value =
1199 cmd.buffer.registers[i++].value =
1207 cmd.buffer.registers[i++].value=
1211 DBG(
"VC_Clocks (0xc4) should be B\n");
1214 cmd.buffer.registers[i++].value=
1220 cmd.buffer.registers[i++].value =
1225 cmd.buffer.registers[i++].value =
1231 DBG(
"VC_Clocks (0xc4) = 0x%0X\n",
cmd.buffer.registers[i-1].value);
1236 cmd.buffer.registers[i++].value =
1239 cmd.buffer.registers[i++].value =
1245 cmd.buffer.registers[i++].value = (
u8) 0;
1247 cmd.buffer.registers[i++].value = (
u8) 1;
1251 cmd.buffer.registers[i++].value = (
u8) 208;
1253 cmd.buffer.registers[i++].value = (
u8) 160;
1257 cmd.buffer.registers[i++].value = (
u8) 0;
1259 cmd.buffer.registers[i++].value = (
u8) 1;
1263 cmd.buffer.registers[i++].value = (
u8) 160;
1265 cmd.buffer.registers[i++].value = (
u8) 64;
1269 cmd.buffer.registers[i++].value = cam->
params.roi.width / 4;
1272 cmd.buffer.registers[i++].value = cam->
params.roi.height / 4;
1277 cmd.buffer.registers[i++].value =
1280 cmd.buffer.registers[i++].value =
1285 cmd.buffer.registers[i++].value =
1288 cmd.buffer.registers[i++].value =
1293 cmd.buffer.registers[i++].value = (
u8) 0;
1296 cmd.buffer.registers[i++].value = (
u8) 0;
1299 cmd.buffer.registers[i++].value = (
u8) 31;
1302 cmd.buffer.registers[i++].value = (
u8) 31;
1305 cmd.buffer.registers[i++].value = (
u8) 0;
1308 cmd.buffer.registers[i++].value = (
u8) 0;
1311 cmd.buffer.registers[i++].value = (
u8) 0x81;
1314 cmd.buffer.registers[i++].value = (
u8) 0x81;
1329 static int config_sensor_500(
struct camera_data *cam,
1330 int req_width,
int req_height)
1336 int width = req_width;
1337 int height = req_height;
1351 if (image_size >= 0) {
1352 set_vw_size(cam, image_size);
1353 width = cam->
params.roi.width;
1354 height = cam->
params.roi.height;
1356 ERR(
"ConfigSensor500 failed\n");
1360 DBG(
"image_size = %d, width = %d, height = %d, type = %d\n",
1361 image_size, width, height, image_type);
1378 cmd.buffer.registers[
i].value =
1381 cmd.buffer.registers[
i].value =
1386 cmd.buffer.registers[
i].value =
1389 cmd.buffer.registers[
i].value =
1395 DBG(
"VC_CLOCKS = 0x%X\n",
cmd.buffer.registers[i-1].value);
1400 cmd.buffer.registers[
i].value =
1403 cmd.buffer.registers[
i].value =
1406 DBG(
"Input width = %d\n",
cmd.buffer.registers[i-1].value);
1411 cmd.buffer.registers[i++].value = (
u8) 2;
1413 cmd.buffer.registers[i++].value = (
u8) 1;
1417 cmd.buffer.registers[i++].value = (
u8) 250;
1419 cmd.buffer.registers[i++].value = (
u8) 125;
1421 cmd.buffer.registers[i++].value = (
u8) 160;
1425 cmd.buffer.registers[i++].value = (
u8) 2;
1427 cmd.buffer.registers[i++].value = (
u8) 1;
1431 cmd.buffer.registers[i++].value = (
u8) 12;
1433 cmd.buffer.registers[i++].value = (
u8) 64;
1435 cmd.buffer.registers[i++].value = (
u8) 6;
1442 cmd.buffer.registers[i++].value = width / 4;
1448 cmd.buffer.registers[i++].value = height / 4;
1453 cmd.buffer.registers[i++].value =
1456 cmd.buffer.registers[i++].value =
1459 cmd.buffer.registers[i++].value =
1462 cmd.buffer.registers[i++].value =
1467 cmd.buffer.registers[i++].value =
1470 cmd.buffer.registers[i++].value =
1473 cmd.buffer.registers[i++].value =
1476 cmd.buffer.registers[i++].value =
1482 cmd.buffer.registers[i++].value = (
u8) 36;
1484 cmd.buffer.registers[i++].value = (
u8) 0;
1488 cmd.buffer.registers[i++].value = (
u8) 32;
1490 cmd.buffer.registers[i++].value = (
u8) 0;
1494 cmd.buffer.registers[i++].value = (
u8) 26;
1496 cmd.buffer.registers[i++].value = (
u8) 31;
1500 cmd.buffer.registers[i++].value = (
u8) 21;
1502 cmd.buffer.registers[i++].value = (
u8) 31;
1505 cmd.buffer.registers[i++].value = (
u8) 0;
1508 cmd.buffer.registers[i++].value = (
u8) 0;
1512 cmd.buffer.registers[i++].value = (
u8) 0x2B;
1514 cmd.buffer.registers[i++].value = (
u8) 0x81;
1518 cmd.buffer.registers[i++].value = (
u8) 0x13;
1520 cmd.buffer.registers[i++].value = (
u8) 0x81;
1536 static int set_all_properties(
struct camera_data *cam)
1544 cam->
params.camera_state.stream_mode);
1550 cam->
params.vp_params.gpio_data);
1556 set_lowlight_boost(cam);
1583 unsigned char cam_reg;
1593 cam_reg = cam->
params.flicker_control.cam_register;
1620 cam_reg = cam->
params.vp_params.exposure_modes;
1640 cam->
params.flicker_control.flicker_mode_req =
mode;
1656 unsigned char cam_reg;
1659 cam_reg = cam->
params.vp_params.user_effects;
1669 cam->
params.vp_params.user_effects = cam_reg;
1681 unsigned char cam_reg;
1684 cam_reg = cam->
params.vp_params.user_effects;
1694 cam->
params.vp_params.user_effects = cam_reg;
1719 cam->
params.vp_params.gpio_direction = 255;
1727 cam->
params.vp_params.gpio_data = setting;
1745 cam->
params.version.sensor_flags ==
1769 cam->
params.vp_params.frame_rate = framerate;
1787 DBG(
"Setting brightness to %d (0x%0x)\n", value, value);
1798 DBG(
"Setting contrast to %d (0x%0x)\n", value, value);
1809 DBG(
"Setting saturation to %d (0x%0x)\n", value, value);
1829 static void set_lowlight_boost(
struct camera_data *cam)
1842 cmd.buffer.block_data[0] = 0;
1843 cmd.buffer.block_data[1] = 0x59;
1844 cmd.buffer.block_data[2] = 0;
1848 if (cam->
params.vp_params.lowlight_boost) {
1849 cmd.buffer.block_data[0] = 0x02;
1851 cmd.buffer.block_data[0] = 0x06;
1890 #ifdef _CPIA2_DEBUG_
2106 static void reset_camera_struct(
struct camera_data *cam)
2111 cam->
params.vp_params.lowlight_boost = 0;
2118 cam->
params.compression.creep_period = 2;
2119 cam->
params.compression.user_squeeze = 20;
2120 cam->
params.compression.inhibit_htables =
false;
2123 cam->
params.vp_params.gpio_direction = 0;
2124 cam->
params.vp_params.gpio_data = 0;
2127 cam->
params.vc_params.quality = 100;
2174 ERR(
"couldn't kmalloc cpia2 struct\n");
2204 cpia2_set_high_power(cam);
2205 cpia2_get_version_info(cam);
2207 ERR(
"Device IO error (asicID has incorrect value of 0x%X\n",
2208 cam->
params.version.asic_id);
2219 reset_camera_struct(cam);
2241 ERR(
"couldn't kmalloc frame buffer structures\n");
2249 ERR(
"couldn't vmalloc frame buffer data area\n");
2300 char __user *
buf,
unsigned long count,
int noblock)
2308 ERR(
"%s: buffer NULL\n",__func__);
2313 ERR(
"%s: Internal error, camera_data NULL!\n",__func__);
2320 cam->
params.camera_state.stream_mode);
2332 !video_is_registered(&cam->
vdev) ||
2337 if (!video_is_registered(&cam->
vdev))
2342 if (frame->
length > count)
2368 cam->
params.camera_state.stream_mode);
2386 const char *adr = (
const char *)vma->
vm_start;
2389 unsigned long start = (
unsigned long) adr;
2394 if (!video_is_registered(&cam->
vdev))
2404 page = kvirt_to_pa(pos);