2 * Driver for the s5k4aa sensor
4 * Copyright (C) 2008 Erik Andrén
5 * Copyright (C) 2007 Ilyes Gouta. Based on the m5603x Linux Driver Project.
6 * Copyright (C) 2005 m5603x Linux Driver Project <m5602@x3ng.com.br>
8 * Portions of code to USB interface and ALi driver software,
9 * Copyright (c) 2006 Willem Duinker
10 * v4l2 interface modeled after the V4L2 driver
11 * for SN9C10x PC Camera Controllers
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation, version 2.
19 #include "m5602_s5k4aa.h"
21 static int s5k4aa_get_exposure(struct gspca_dev
*gspca_dev
, __s32
*val
);
22 static int s5k4aa_set_exposure(struct gspca_dev
*gspca_dev
, __s32 val
);
23 static int s5k4aa_get_vflip(struct gspca_dev
*gspca_dev
, __s32
*val
);
24 static int s5k4aa_set_vflip(struct gspca_dev
*gspca_dev
, __s32 val
);
25 static int s5k4aa_get_hflip(struct gspca_dev
*gspca_dev
, __s32
*val
);
26 static int s5k4aa_set_hflip(struct gspca_dev
*gspca_dev
, __s32 val
);
27 static int s5k4aa_get_gain(struct gspca_dev
*gspca_dev
, __s32
*val
);
28 static int s5k4aa_set_gain(struct gspca_dev
*gspca_dev
, __s32 val
);
29 static int s5k4aa_get_noise(struct gspca_dev
*gspca_dev
, __s32
*val
);
30 static int s5k4aa_set_noise(struct gspca_dev
*gspca_dev
, __s32 val
);
31 static int s5k4aa_get_brightness(struct gspca_dev
*gspca_dev
, __s32
*val
);
32 static int s5k4aa_set_brightness(struct gspca_dev
*gspca_dev
, __s32 val
);
36 struct dmi_system_id s5k4aa_vflip_dmi_table
[] = {
38 .ident
= "Fujitsu-Siemens Amilo Xa 2528",
40 DMI_MATCH(DMI_SYS_VENDOR
, "FUJITSU SIEMENS"),
41 DMI_MATCH(DMI_PRODUCT_NAME
, "AMILO Xa 2528")
44 .ident
= "Fujitsu-Siemens Amilo Xi 2550",
46 DMI_MATCH(DMI_SYS_VENDOR
, "FUJITSU SIEMENS"),
47 DMI_MATCH(DMI_PRODUCT_NAME
, "AMILO Xi 2550")
52 DMI_MATCH(DMI_SYS_VENDOR
, "Micro-Star International"),
53 DMI_MATCH(DMI_PRODUCT_NAME
, "GX700"),
54 DMI_MATCH(DMI_BIOS_DATE
, "07/26/2007")
57 .ident
= "MSI GX700/GX705/EX700",
59 DMI_MATCH(DMI_SYS_VENDOR
, "Micro-Star International"),
60 DMI_MATCH(DMI_PRODUCT_NAME
, "GX700/GX705/EX700")
65 DMI_MATCH(DMI_SYS_VENDOR
, "Micro-Star International"),
66 DMI_MATCH(DMI_PRODUCT_NAME
, "MS-1717X")
69 .ident
= "Lenovo Y300",
71 DMI_MATCH(DMI_SYS_VENDOR
, "L3000 Y300"),
72 DMI_MATCH(DMI_PRODUCT_NAME
, "Y300")
78 static struct v4l2_pix_format s5k4aa_modes
[] = {
87 .colorspace
= V4L2_COLORSPACE_SRGB
,
98 .colorspace
= V4L2_COLORSPACE_SRGB
,
103 static const struct ctrl s5k4aa_ctrls
[] = {
107 .id
= V4L2_CID_VFLIP
,
108 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
109 .name
= "vertical flip",
115 .set
= s5k4aa_set_vflip
,
116 .get
= s5k4aa_get_vflip
121 .id
= V4L2_CID_HFLIP
,
122 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
123 .name
= "horizontal flip",
129 .set
= s5k4aa_set_hflip
,
130 .get
= s5k4aa_get_hflip
136 .type
= V4L2_CTRL_TYPE_INTEGER
,
141 .default_value
= S5K4AA_DEFAULT_GAIN
,
142 .flags
= V4L2_CTRL_FLAG_SLIDER
144 .set
= s5k4aa_set_gain
,
145 .get
= s5k4aa_get_gain
147 #define EXPOSURE_IDX 3
150 .id
= V4L2_CID_EXPOSURE
,
151 .type
= V4L2_CTRL_TYPE_INTEGER
,
156 .default_value
= 0x100,
157 .flags
= V4L2_CTRL_FLAG_SLIDER
159 .set
= s5k4aa_set_exposure
,
160 .get
= s5k4aa_get_exposure
162 #define NOISE_SUPP_IDX 4
165 .id
= V4L2_CID_PRIVATE_BASE
,
166 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
167 .name
= "Noise suppression (smoothing)",
173 .set
= s5k4aa_set_noise
,
174 .get
= s5k4aa_get_noise
176 #define BRIGHTNESS_IDX 5
179 .id
= V4L2_CID_BRIGHTNESS
,
180 .type
= V4L2_CTRL_TYPE_INTEGER
,
181 .name
= "Brightness",
185 .default_value
= S5K4AA_DEFAULT_BRIGHTNESS
,
187 .set
= s5k4aa_set_brightness
,
188 .get
= s5k4aa_get_brightness
193 static void s5k4aa_dump_registers(struct sd
*sd
);
195 int s5k4aa_probe(struct sd
*sd
)
197 u8 prod_id
[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
198 const u8 expected_prod_id
[6] = {0x00, 0x10, 0x00, 0x4b, 0x33, 0x75};
200 s32
*sensor_settings
;
203 if (force_sensor
== S5K4AA_SENSOR
) {
204 info("Forcing a %s sensor", s5k4aa
.name
);
207 /* If we want to force another sensor, don't try to probe this
212 info("Probing for a s5k4aa sensor");
214 /* Preinit the sensor */
215 for (i
= 0; i
< ARRAY_SIZE(preinit_s5k4aa
) && !err
; i
++) {
216 u8 data
[2] = {0x00, 0x00};
218 switch (preinit_s5k4aa
[i
][0]) {
220 err
= m5602_write_bridge(sd
,
221 preinit_s5k4aa
[i
][1],
222 preinit_s5k4aa
[i
][2]);
226 data
[0] = preinit_s5k4aa
[i
][2];
227 err
= m5602_write_sensor(sd
,
228 preinit_s5k4aa
[i
][1],
233 data
[0] = preinit_s5k4aa
[i
][2];
234 data
[1] = preinit_s5k4aa
[i
][3];
235 err
= m5602_write_sensor(sd
,
236 preinit_s5k4aa
[i
][1],
240 info("Invalid stream command, exiting init");
245 /* Test some registers, but we don't know their exact meaning yet */
246 if (m5602_read_sensor(sd
, 0x00, prod_id
, 2))
248 if (m5602_read_sensor(sd
, 0x02, prod_id
+2, 2))
250 if (m5602_read_sensor(sd
, 0x04, prod_id
+4, 2))
253 if (memcmp(prod_id
, expected_prod_id
, sizeof(prod_id
)))
256 info("Detected a s5k4aa sensor");
259 sensor_settings
= kmalloc(
260 ARRAY_SIZE(s5k4aa_ctrls
) * sizeof(s32
), GFP_KERNEL
);
261 if (!sensor_settings
)
264 sd
->gspca_dev
.cam
.cam_mode
= s5k4aa_modes
;
265 sd
->gspca_dev
.cam
.nmodes
= ARRAY_SIZE(s5k4aa_modes
);
266 sd
->desc
->ctrls
= s5k4aa_ctrls
;
267 sd
->desc
->nctrls
= ARRAY_SIZE(s5k4aa_ctrls
);
269 for (i
= 0; i
< ARRAY_SIZE(s5k4aa_ctrls
); i
++)
270 sensor_settings
[i
] = s5k4aa_ctrls
[i
].qctrl
.default_value
;
271 sd
->sensor_priv
= sensor_settings
;
276 int s5k4aa_start(struct sd
*sd
)
280 struct cam
*cam
= &sd
->gspca_dev
.cam
;
281 s32
*sensor_settings
= sd
->sensor_priv
;
283 switch (cam
->cam_mode
[sd
->gspca_dev
.curr_mode
].width
) {
285 PDEBUG(D_V4L2
, "Configuring camera for SXGA mode");
287 for (i
= 0; i
< ARRAY_SIZE(SXGA_s5k4aa
); i
++) {
288 switch (SXGA_s5k4aa
[i
][0]) {
290 err
= m5602_write_bridge(sd
,
296 data
[0] = SXGA_s5k4aa
[i
][2];
297 err
= m5602_write_sensor(sd
,
303 data
[0] = SXGA_s5k4aa
[i
][2];
304 data
[1] = SXGA_s5k4aa
[i
][3];
305 err
= m5602_write_sensor(sd
,
311 err("Invalid stream command, exiting init");
315 err
= s5k4aa_set_noise(&sd
->gspca_dev
, 0);
321 PDEBUG(D_V4L2
, "Configuring camera for VGA mode");
323 for (i
= 0; i
< ARRAY_SIZE(VGA_s5k4aa
); i
++) {
324 switch (VGA_s5k4aa
[i
][0]) {
326 err
= m5602_write_bridge(sd
,
332 data
[0] = VGA_s5k4aa
[i
][2];
333 err
= m5602_write_sensor(sd
,
339 data
[0] = VGA_s5k4aa
[i
][2];
340 data
[1] = VGA_s5k4aa
[i
][3];
341 err
= m5602_write_sensor(sd
,
347 err("Invalid stream command, exiting init");
351 err
= s5k4aa_set_noise(&sd
->gspca_dev
, 1);
359 err
= s5k4aa_set_exposure(&sd
->gspca_dev
,
360 sensor_settings
[EXPOSURE_IDX
]);
364 err
= s5k4aa_set_gain(&sd
->gspca_dev
, sensor_settings
[GAIN_IDX
]);
368 err
= s5k4aa_set_brightness(&sd
->gspca_dev
,
369 sensor_settings
[BRIGHTNESS_IDX
]);
373 err
= s5k4aa_set_noise(&sd
->gspca_dev
, sensor_settings
[NOISE_SUPP_IDX
]);
377 err
= s5k4aa_set_vflip(&sd
->gspca_dev
, sensor_settings
[VFLIP_IDX
]);
381 return s5k4aa_set_hflip(&sd
->gspca_dev
, sensor_settings
[HFLIP_IDX
]);
384 int s5k4aa_init(struct sd
*sd
)
388 for (i
= 0; i
< ARRAY_SIZE(init_s5k4aa
) && !err
; i
++) {
389 u8 data
[2] = {0x00, 0x00};
391 switch (init_s5k4aa
[i
][0]) {
393 err
= m5602_write_bridge(sd
,
399 data
[0] = init_s5k4aa
[i
][2];
400 err
= m5602_write_sensor(sd
,
401 init_s5k4aa
[i
][1], data
, 1);
405 data
[0] = init_s5k4aa
[i
][2];
406 data
[1] = init_s5k4aa
[i
][3];
407 err
= m5602_write_sensor(sd
,
408 init_s5k4aa
[i
][1], data
, 2);
411 info("Invalid stream command, exiting init");
417 s5k4aa_dump_registers(sd
);
422 static int s5k4aa_get_exposure(struct gspca_dev
*gspca_dev
, __s32
*val
)
424 struct sd
*sd
= (struct sd
*) gspca_dev
;
425 s32
*sensor_settings
= sd
->sensor_priv
;
427 *val
= sensor_settings
[EXPOSURE_IDX
];
428 PDEBUG(D_V4L2
, "Read exposure %d", *val
);
433 static int s5k4aa_set_exposure(struct gspca_dev
*gspca_dev
, __s32 val
)
435 struct sd
*sd
= (struct sd
*) gspca_dev
;
436 s32
*sensor_settings
= sd
->sensor_priv
;
437 u8 data
= S5K4AA_PAGE_MAP_2
;
440 sensor_settings
[EXPOSURE_IDX
] = val
;
441 PDEBUG(D_V4L2
, "Set exposure to %d", val
);
442 err
= m5602_write_sensor(sd
, S5K4AA_PAGE_MAP
, &data
, 1);
445 data
= (val
>> 8) & 0xff;
446 err
= m5602_write_sensor(sd
, S5K4AA_EXPOSURE_HI
, &data
, 1);
450 err
= m5602_write_sensor(sd
, S5K4AA_EXPOSURE_LO
, &data
, 1);
455 static int s5k4aa_get_vflip(struct gspca_dev
*gspca_dev
, __s32
*val
)
457 struct sd
*sd
= (struct sd
*) gspca_dev
;
458 s32
*sensor_settings
= sd
->sensor_priv
;
460 *val
= sensor_settings
[VFLIP_IDX
];
461 PDEBUG(D_V4L2
, "Read vertical flip %d", *val
);
466 static int s5k4aa_set_vflip(struct gspca_dev
*gspca_dev
, __s32 val
)
468 struct sd
*sd
= (struct sd
*) gspca_dev
;
469 s32
*sensor_settings
= sd
->sensor_priv
;
470 u8 data
= S5K4AA_PAGE_MAP_2
;
473 sensor_settings
[VFLIP_IDX
] = val
;
475 PDEBUG(D_V4L2
, "Set vertical flip to %d", val
);
476 err
= m5602_write_sensor(sd
, S5K4AA_PAGE_MAP
, &data
, 1);
480 err
= m5602_read_sensor(sd
, S5K4AA_READ_MODE
, &data
, 1);
484 if (dmi_check_system(s5k4aa_vflip_dmi_table
))
487 data
= ((data
& ~S5K4AA_RM_V_FLIP
) | ((val
& 0x01) << 7));
488 err
= m5602_write_sensor(sd
, S5K4AA_READ_MODE
, &data
, 1);
492 err
= m5602_read_sensor(sd
, S5K4AA_ROWSTART_LO
, &data
, 1);
495 data
= (data
& 0xfe) | !val
;
496 err
= m5602_write_sensor(sd
, S5K4AA_ROWSTART_LO
, &data
, 1);
500 static int s5k4aa_get_hflip(struct gspca_dev
*gspca_dev
, __s32
*val
)
502 struct sd
*sd
= (struct sd
*) gspca_dev
;
503 s32
*sensor_settings
= sd
->sensor_priv
;
505 *val
= sensor_settings
[HFLIP_IDX
];
506 PDEBUG(D_V4L2
, "Read horizontal flip %d", *val
);
511 static int s5k4aa_set_hflip(struct gspca_dev
*gspca_dev
, __s32 val
)
513 struct sd
*sd
= (struct sd
*) gspca_dev
;
514 s32
*sensor_settings
= sd
->sensor_priv
;
515 u8 data
= S5K4AA_PAGE_MAP_2
;
518 sensor_settings
[HFLIP_IDX
] = val
;
520 PDEBUG(D_V4L2
, "Set horizontal flip to %d", val
);
521 err
= m5602_write_sensor(sd
, S5K4AA_PAGE_MAP
, &data
, 1);
525 err
= m5602_read_sensor(sd
, S5K4AA_READ_MODE
, &data
, 1);
529 if (dmi_check_system(s5k4aa_vflip_dmi_table
))
532 data
= ((data
& ~S5K4AA_RM_H_FLIP
) | ((val
& 0x01) << 6));
533 err
= m5602_write_sensor(sd
, S5K4AA_READ_MODE
, &data
, 1);
537 err
= m5602_read_sensor(sd
, S5K4AA_COLSTART_LO
, &data
, 1);
540 data
= (data
& 0xfe) | !val
;
541 err
= m5602_write_sensor(sd
, S5K4AA_COLSTART_LO
, &data
, 1);
545 static int s5k4aa_get_gain(struct gspca_dev
*gspca_dev
, __s32
*val
)
547 struct sd
*sd
= (struct sd
*) gspca_dev
;
548 s32
*sensor_settings
= sd
->sensor_priv
;
550 *val
= sensor_settings
[GAIN_IDX
];
551 PDEBUG(D_V4L2
, "Read gain %d", *val
);
555 static int s5k4aa_set_gain(struct gspca_dev
*gspca_dev
, __s32 val
)
557 struct sd
*sd
= (struct sd
*) gspca_dev
;
558 s32
*sensor_settings
= sd
->sensor_priv
;
559 u8 data
= S5K4AA_PAGE_MAP_2
;
562 sensor_settings
[GAIN_IDX
] = val
;
564 PDEBUG(D_V4L2
, "Set gain to %d", val
);
565 err
= m5602_write_sensor(sd
, S5K4AA_PAGE_MAP
, &data
, 1);
570 err
= m5602_write_sensor(sd
, S5K4AA_GAIN
, &data
, 1);
575 static int s5k4aa_get_brightness(struct gspca_dev
*gspca_dev
, __s32
*val
)
577 struct sd
*sd
= (struct sd
*) gspca_dev
;
578 s32
*sensor_settings
= sd
->sensor_priv
;
580 *val
= sensor_settings
[BRIGHTNESS_IDX
];
581 PDEBUG(D_V4L2
, "Read brightness %d", *val
);
585 static int s5k4aa_set_brightness(struct gspca_dev
*gspca_dev
, __s32 val
)
587 struct sd
*sd
= (struct sd
*) gspca_dev
;
588 s32
*sensor_settings
= sd
->sensor_priv
;
589 u8 data
= S5K4AA_PAGE_MAP_2
;
592 sensor_settings
[BRIGHTNESS_IDX
] = val
;
594 PDEBUG(D_V4L2
, "Set brightness to %d", val
);
595 err
= m5602_write_sensor(sd
, S5K4AA_PAGE_MAP
, &data
, 1);
600 return m5602_write_sensor(sd
, S5K4AA_BRIGHTNESS
, &data
, 1);
603 static int s5k4aa_get_noise(struct gspca_dev
*gspca_dev
, __s32
*val
)
605 struct sd
*sd
= (struct sd
*) gspca_dev
;
606 s32
*sensor_settings
= sd
->sensor_priv
;
608 *val
= sensor_settings
[NOISE_SUPP_IDX
];
609 PDEBUG(D_V4L2
, "Read noise %d", *val
);
613 static int s5k4aa_set_noise(struct gspca_dev
*gspca_dev
, __s32 val
)
615 struct sd
*sd
= (struct sd
*) gspca_dev
;
616 s32
*sensor_settings
= sd
->sensor_priv
;
617 u8 data
= S5K4AA_PAGE_MAP_2
;
620 sensor_settings
[NOISE_SUPP_IDX
] = val
;
622 PDEBUG(D_V4L2
, "Set noise to %d", val
);
623 err
= m5602_write_sensor(sd
, S5K4AA_PAGE_MAP
, &data
, 1);
628 return m5602_write_sensor(sd
, S5K4AA_NOISE_SUPP
, &data
, 1);
631 void s5k4aa_disconnect(struct sd
*sd
)
634 kfree(sd
->sensor_priv
);
637 static void s5k4aa_dump_registers(struct sd
*sd
)
641 m5602_read_sensor(sd
, S5K4AA_PAGE_MAP
, &old_page
, 1);
642 for (page
= 0; page
< 16; page
++) {
643 m5602_write_sensor(sd
, S5K4AA_PAGE_MAP
, &page
, 1);
644 info("Dumping the s5k4aa register state for page 0x%x", page
);
645 for (address
= 0; address
<= 0xff; address
++) {
647 m5602_read_sensor(sd
, address
, &value
, 1);
648 info("register 0x%x contains 0x%x",
652 info("s5k4aa register state dump complete");
654 for (page
= 0; page
< 16; page
++) {
655 m5602_write_sensor(sd
, S5K4AA_PAGE_MAP
, &page
, 1);
656 info("Probing for which registers that are "
657 "read/write for page 0x%x", page
);
658 for (address
= 0; address
<= 0xff; address
++) {
659 u8 old_value
, ctrl_value
, test_value
= 0xff;
661 m5602_read_sensor(sd
, address
, &old_value
, 1);
662 m5602_write_sensor(sd
, address
, &test_value
, 1);
663 m5602_read_sensor(sd
, address
, &ctrl_value
, 1);
665 if (ctrl_value
== test_value
)
666 info("register 0x%x is writeable", address
);
668 info("register 0x%x is read only", address
);
670 /* Restore original value */
671 m5602_write_sensor(sd
, address
, &old_value
, 1);
674 info("Read/write register probing complete");
675 m5602_write_sensor(sd
, S5K4AA_PAGE_MAP
, &old_page
, 1);