2 * vs6624.c ST VS6624 CMOS image sensor driver
4 * Copyright (c) 2011 Analog Devices Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <linux/delay.h>
21 #include <linux/errno.h>
22 #include <linux/gpio.h>
23 #include <linux/i2c.h>
24 #include <linux/init.h>
25 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <linux/types.h>
28 #include <linux/videodev2.h>
30 #include <media/v4l2-chip-ident.h>
31 #include <media/v4l2-ctrls.h>
32 #include <media/v4l2-device.h>
33 #include <media/v4l2-mediabus.h>
35 #include "vs6624_regs.h"
38 #define VGA_HEIGHT 480
39 #define QVGA_WIDTH 320
40 #define QVGA_HEIGHT 240
41 #define QQVGA_WIDTH 160
42 #define QQVGA_HEIGHT 120
44 #define CIF_HEIGHT 288
45 #define QCIF_WIDTH 176
46 #define QCIF_HEIGHT 144
47 #define QQCIF_WIDTH 88
48 #define QQCIF_HEIGHT 72
50 #define MAX_FRAME_RATE 30
53 struct v4l2_subdev sd
;
54 struct v4l2_ctrl_handler hdl
;
55 struct v4l2_fract frame_rate
;
56 struct v4l2_mbus_framefmt fmt
;
60 static const struct vs6624_format
{
61 enum v4l2_mbus_pixelcode mbus_code
;
62 enum v4l2_colorspace colorspace
;
63 } vs6624_formats
[] = {
65 .mbus_code
= V4L2_MBUS_FMT_UYVY8_2X8
,
66 .colorspace
= V4L2_COLORSPACE_JPEG
,
69 .mbus_code
= V4L2_MBUS_FMT_YUYV8_2X8
,
70 .colorspace
= V4L2_COLORSPACE_JPEG
,
73 .mbus_code
= V4L2_MBUS_FMT_RGB565_2X8_LE
,
74 .colorspace
= V4L2_COLORSPACE_SRGB
,
78 static struct v4l2_mbus_framefmt vs6624_default_fmt
= {
81 .code
= V4L2_MBUS_FMT_UYVY8_2X8
,
82 .field
= V4L2_FIELD_NONE
,
83 .colorspace
= V4L2_COLORSPACE_JPEG
,
86 static const u16 vs6624_p1
[] = {
412 static const u16 vs6624_p2
[] = {
418 static const u16 vs6624_run_setup
[] = {
419 0x1d18, 0x00, /* Enableconstrainedwhitebalance */
420 VS6624_PEAK_MIN_OUT_G_MSB
, 0x3c, /* Damper PeakGain Output MSB */
421 VS6624_PEAK_MIN_OUT_G_LSB
, 0x66, /* Damper PeakGain Output LSB */
422 VS6624_CM_LOW_THR_MSB
, 0x65, /* Damper Low MSB */
423 VS6624_CM_LOW_THR_LSB
, 0xd1, /* Damper Low LSB */
424 VS6624_CM_HIGH_THR_MSB
, 0x66, /* Damper High MSB */
425 VS6624_CM_HIGH_THR_LSB
, 0x62, /* Damper High LSB */
426 VS6624_CM_MIN_OUT_MSB
, 0x00, /* Damper Min output MSB */
427 VS6624_CM_MIN_OUT_LSB
, 0x00, /* Damper Min output LSB */
428 VS6624_NORA_DISABLE
, 0x00, /* Nora fDisable */
429 VS6624_NORA_USAGE
, 0x04, /* Nora usage */
430 VS6624_NORA_LOW_THR_MSB
, 0x63, /* Damper Low MSB Changed 0x63 to 0x65 */
431 VS6624_NORA_LOW_THR_LSB
, 0xd1, /* Damper Low LSB */
432 VS6624_NORA_HIGH_THR_MSB
, 0x68, /* Damper High MSB */
433 VS6624_NORA_HIGH_THR_LSB
, 0xdd, /* Damper High LSB */
434 VS6624_NORA_MIN_OUT_MSB
, 0x3a, /* Damper Min output MSB */
435 VS6624_NORA_MIN_OUT_LSB
, 0x00, /* Damper Min output LSB */
436 VS6624_F2B_DISABLE
, 0x00, /* Disable */
437 0x1d8a, 0x30, /* MAXWeightHigh */
438 0x1d91, 0x62, /* fpDamperLowThresholdHigh MSB */
439 0x1d92, 0x4a, /* fpDamperLowThresholdHigh LSB */
440 0x1d95, 0x65, /* fpDamperHighThresholdHigh MSB */
441 0x1d96, 0x0e, /* fpDamperHighThresholdHigh LSB */
442 0x1da1, 0x3a, /* fpMinimumDamperOutputLow MSB */
443 0x1da2, 0xb8, /* fpMinimumDamperOutputLow LSB */
444 0x1e08, 0x06, /* MAXWeightLow */
445 0x1e0a, 0x0a, /* MAXWeightHigh */
446 0x1601, 0x3a, /* Red A MSB */
447 0x1602, 0x14, /* Red A LSB */
448 0x1605, 0x3b, /* Blue A MSB */
449 0x1606, 0x85, /* BLue A LSB */
450 0x1609, 0x3b, /* RED B MSB */
451 0x160a, 0x85, /* RED B LSB */
452 0x160d, 0x3a, /* Blue B MSB */
453 0x160e, 0x14, /* Blue B LSB */
454 0x1611, 0x30, /* Max Distance from Locus MSB */
455 0x1612, 0x8f, /* Max Distance from Locus MSB */
456 0x1614, 0x01, /* Enable constrainer */
460 static const u16 vs6624_default
[] = {
461 VS6624_CONTRAST0
, 0x84,
462 VS6624_SATURATION0
, 0x75,
464 VS6624_CONTRAST1
, 0x84,
465 VS6624_SATURATION1
, 0x75,
471 VS6624_EXPO_COMPENSATION
, 0xfe,
472 VS6624_EXPO_METER
, 0x0,
473 VS6624_LIGHT_FREQ
, 0x64,
474 VS6624_PEAK_GAIN
, 0xe,
475 VS6624_PEAK_LOW_THR
, 0x28,
476 VS6624_HMIRROR0
, 0x0,
478 VS6624_ZOOM_HSTEP0_MSB
, 0x0,
479 VS6624_ZOOM_HSTEP0_LSB
, 0x1,
480 VS6624_ZOOM_VSTEP0_MSB
, 0x0,
481 VS6624_ZOOM_VSTEP0_LSB
, 0x1,
482 VS6624_PAN_HSTEP0_MSB
, 0x0,
483 VS6624_PAN_HSTEP0_LSB
, 0xf,
484 VS6624_PAN_VSTEP0_MSB
, 0x0,
485 VS6624_PAN_VSTEP0_LSB
, 0xf,
486 VS6624_SENSOR_MODE
, 0x1,
487 VS6624_SYNC_CODE_SETUP
, 0x21,
488 VS6624_DISABLE_FR_DAMPER
, 0x0,
490 VS6624_FR_NUM_LSB
, 0xf,
491 VS6624_INIT_PIPE_SETUP
, 0x0,
492 VS6624_IMG_FMT0
, 0x0,
493 VS6624_YUV_SETUP
, 0x1,
494 VS6624_IMAGE_SIZE0
, 0x2,
498 static inline struct vs6624
*to_vs6624(struct v4l2_subdev
*sd
)
500 return container_of(sd
, struct vs6624
, sd
);
502 static inline struct v4l2_subdev
*to_sd(struct v4l2_ctrl
*ctrl
)
504 return &container_of(ctrl
->handler
, struct vs6624
, hdl
)->sd
;
507 static int vs6624_read(struct v4l2_subdev
*sd
, u16 index
)
509 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
514 i2c_master_send(client
, buf
, 2);
515 i2c_master_recv(client
, buf
, 1);
520 static int vs6624_write(struct v4l2_subdev
*sd
, u16 index
,
523 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
530 return i2c_master_send(client
, buf
, 3);
533 static int vs6624_writeregs(struct v4l2_subdev
*sd
, const u16
*regs
)
538 while (*regs
!= 0x00) {
542 vs6624_write(sd
, reg
, data
);
547 static int vs6624_s_ctrl(struct v4l2_ctrl
*ctrl
)
549 struct v4l2_subdev
*sd
= to_sd(ctrl
);
552 case V4L2_CID_CONTRAST
:
553 vs6624_write(sd
, VS6624_CONTRAST0
, ctrl
->val
);
555 case V4L2_CID_SATURATION
:
556 vs6624_write(sd
, VS6624_SATURATION0
, ctrl
->val
);
559 vs6624_write(sd
, VS6624_HMIRROR0
, ctrl
->val
);
562 vs6624_write(sd
, VS6624_VFLIP0
, ctrl
->val
);
571 static int vs6624_enum_mbus_fmt(struct v4l2_subdev
*sd
, unsigned index
,
572 enum v4l2_mbus_pixelcode
*code
)
574 if (index
>= ARRAY_SIZE(vs6624_formats
))
577 *code
= vs6624_formats
[index
].mbus_code
;
581 static int vs6624_try_mbus_fmt(struct v4l2_subdev
*sd
,
582 struct v4l2_mbus_framefmt
*fmt
)
586 for (index
= 0; index
< ARRAY_SIZE(vs6624_formats
); index
++)
587 if (vs6624_formats
[index
].mbus_code
== fmt
->code
)
589 if (index
>= ARRAY_SIZE(vs6624_formats
)) {
590 /* default to first format */
592 fmt
->code
= vs6624_formats
[0].mbus_code
;
595 /* sensor mode is VGA */
596 if (fmt
->width
> VGA_WIDTH
)
597 fmt
->width
= VGA_WIDTH
;
598 if (fmt
->height
> VGA_HEIGHT
)
599 fmt
->height
= VGA_HEIGHT
;
600 fmt
->width
= fmt
->width
& (~3);
601 fmt
->height
= fmt
->height
& (~3);
602 fmt
->field
= V4L2_FIELD_NONE
;
603 fmt
->colorspace
= vs6624_formats
[index
].colorspace
;
607 static int vs6624_s_mbus_fmt(struct v4l2_subdev
*sd
,
608 struct v4l2_mbus_framefmt
*fmt
)
610 struct vs6624
*sensor
= to_vs6624(sd
);
613 ret
= vs6624_try_mbus_fmt(sd
, fmt
);
617 /* set image format */
619 case V4L2_MBUS_FMT_UYVY8_2X8
:
620 vs6624_write(sd
, VS6624_IMG_FMT0
, 0x0);
621 vs6624_write(sd
, VS6624_YUV_SETUP
, 0x1);
623 case V4L2_MBUS_FMT_YUYV8_2X8
:
624 vs6624_write(sd
, VS6624_IMG_FMT0
, 0x0);
625 vs6624_write(sd
, VS6624_YUV_SETUP
, 0x3);
627 case V4L2_MBUS_FMT_RGB565_2X8_LE
:
628 vs6624_write(sd
, VS6624_IMG_FMT0
, 0x4);
629 vs6624_write(sd
, VS6624_RGB_SETUP
, 0x0);
636 if ((fmt
->width
== VGA_WIDTH
) && (fmt
->height
== VGA_HEIGHT
))
637 vs6624_write(sd
, VS6624_IMAGE_SIZE0
, 0x2);
638 else if ((fmt
->width
== QVGA_WIDTH
) && (fmt
->height
== QVGA_HEIGHT
))
639 vs6624_write(sd
, VS6624_IMAGE_SIZE0
, 0x4);
640 else if ((fmt
->width
== QQVGA_WIDTH
) && (fmt
->height
== QQVGA_HEIGHT
))
641 vs6624_write(sd
, VS6624_IMAGE_SIZE0
, 0x6);
642 else if ((fmt
->width
== CIF_WIDTH
) && (fmt
->height
== CIF_HEIGHT
))
643 vs6624_write(sd
, VS6624_IMAGE_SIZE0
, 0x3);
644 else if ((fmt
->width
== QCIF_WIDTH
) && (fmt
->height
== QCIF_HEIGHT
))
645 vs6624_write(sd
, VS6624_IMAGE_SIZE0
, 0x5);
646 else if ((fmt
->width
== QQCIF_WIDTH
) && (fmt
->height
== QQCIF_HEIGHT
))
647 vs6624_write(sd
, VS6624_IMAGE_SIZE0
, 0x7);
649 vs6624_write(sd
, VS6624_IMAGE_SIZE0
, 0x8);
650 vs6624_write(sd
, VS6624_MAN_HSIZE0_MSB
, fmt
->width
>> 8);
651 vs6624_write(sd
, VS6624_MAN_HSIZE0_LSB
, fmt
->width
& 0xFF);
652 vs6624_write(sd
, VS6624_MAN_VSIZE0_MSB
, fmt
->height
>> 8);
653 vs6624_write(sd
, VS6624_MAN_VSIZE0_LSB
, fmt
->height
& 0xFF);
654 vs6624_write(sd
, VS6624_CROP_CTRL0
, 0x1);
662 static int vs6624_g_mbus_fmt(struct v4l2_subdev
*sd
,
663 struct v4l2_mbus_framefmt
*fmt
)
665 struct vs6624
*sensor
= to_vs6624(sd
);
671 static int vs6624_g_parm(struct v4l2_subdev
*sd
, struct v4l2_streamparm
*parms
)
673 struct vs6624
*sensor
= to_vs6624(sd
);
674 struct v4l2_captureparm
*cp
= &parms
->parm
.capture
;
676 if (parms
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
679 memset(cp
, 0, sizeof(*cp
));
680 cp
->capability
= V4L2_CAP_TIMEPERFRAME
;
681 cp
->timeperframe
.numerator
= sensor
->frame_rate
.denominator
;
682 cp
->timeperframe
.denominator
= sensor
->frame_rate
.numerator
;
686 static int vs6624_s_parm(struct v4l2_subdev
*sd
, struct v4l2_streamparm
*parms
)
688 struct vs6624
*sensor
= to_vs6624(sd
);
689 struct v4l2_captureparm
*cp
= &parms
->parm
.capture
;
690 struct v4l2_fract
*tpf
= &cp
->timeperframe
;
692 if (parms
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
694 if (cp
->extendedmode
!= 0)
697 if (tpf
->numerator
== 0 || tpf
->denominator
== 0
698 || (tpf
->denominator
> tpf
->numerator
* MAX_FRAME_RATE
)) {
699 /* reset to max frame rate */
701 tpf
->denominator
= MAX_FRAME_RATE
;
703 sensor
->frame_rate
.numerator
= tpf
->denominator
;
704 sensor
->frame_rate
.denominator
= tpf
->numerator
;
705 vs6624_write(sd
, VS6624_DISABLE_FR_DAMPER
, 0x0);
706 vs6624_write(sd
, VS6624_FR_NUM_MSB
,
707 sensor
->frame_rate
.numerator
>> 8);
708 vs6624_write(sd
, VS6624_FR_NUM_LSB
,
709 sensor
->frame_rate
.numerator
& 0xFF);
710 vs6624_write(sd
, VS6624_FR_DEN
,
711 sensor
->frame_rate
.denominator
& 0xFF);
715 static int vs6624_s_stream(struct v4l2_subdev
*sd
, int enable
)
718 vs6624_write(sd
, VS6624_USER_CMD
, 0x2);
720 vs6624_write(sd
, VS6624_USER_CMD
, 0x4);
725 static int vs6624_g_chip_ident(struct v4l2_subdev
*sd
,
726 struct v4l2_dbg_chip_ident
*chip
)
729 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
731 rev
= (vs6624_read(sd
, VS6624_FW_VSN_MAJOR
) << 8)
732 | vs6624_read(sd
, VS6624_FW_VSN_MINOR
);
734 return v4l2_chip_ident_i2c_client(client
, chip
, V4L2_IDENT_VS6624
, rev
);
737 #ifdef CONFIG_VIDEO_ADV_DEBUG
738 static int vs6624_g_register(struct v4l2_subdev
*sd
, struct v4l2_dbg_register
*reg
)
740 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
742 if (!v4l2_chip_match_i2c_client(client
, ®
->match
))
744 if (!capable(CAP_SYS_ADMIN
))
746 reg
->val
= vs6624_read(sd
, reg
->reg
& 0xffff);
751 static int vs6624_s_register(struct v4l2_subdev
*sd
, struct v4l2_dbg_register
*reg
)
753 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
755 if (!v4l2_chip_match_i2c_client(client
, ®
->match
))
757 if (!capable(CAP_SYS_ADMIN
))
759 vs6624_write(sd
, reg
->reg
& 0xffff, reg
->val
& 0xff);
764 static const struct v4l2_ctrl_ops vs6624_ctrl_ops
= {
765 .s_ctrl
= vs6624_s_ctrl
,
768 static const struct v4l2_subdev_core_ops vs6624_core_ops
= {
769 .g_chip_ident
= vs6624_g_chip_ident
,
770 #ifdef CONFIG_VIDEO_ADV_DEBUG
771 .g_register
= vs6624_g_register
,
772 .s_register
= vs6624_s_register
,
776 static const struct v4l2_subdev_video_ops vs6624_video_ops
= {
777 .enum_mbus_fmt
= vs6624_enum_mbus_fmt
,
778 .try_mbus_fmt
= vs6624_try_mbus_fmt
,
779 .s_mbus_fmt
= vs6624_s_mbus_fmt
,
780 .g_mbus_fmt
= vs6624_g_mbus_fmt
,
781 .s_parm
= vs6624_s_parm
,
782 .g_parm
= vs6624_g_parm
,
783 .s_stream
= vs6624_s_stream
,
786 static const struct v4l2_subdev_ops vs6624_ops
= {
787 .core
= &vs6624_core_ops
,
788 .video
= &vs6624_video_ops
,
791 static int __devinit
vs6624_probe(struct i2c_client
*client
,
792 const struct i2c_device_id
*id
)
794 struct vs6624
*sensor
;
795 struct v4l2_subdev
*sd
;
796 struct v4l2_ctrl_handler
*hdl
;
800 /* Check if the adapter supports the needed features */
801 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
))
804 ce
= client
->dev
.platform_data
;
808 ret
= gpio_request(*ce
, "VS6624 Chip Enable");
810 v4l_err(client
, "failed to request GPIO %d\n", *ce
);
813 gpio_direction_output(*ce
, 1);
814 /* wait 100ms before any further i2c writes are performed */
817 sensor
= kzalloc(sizeof(*sensor
), GFP_KERNEL
);
818 if (sensor
== NULL
) {
824 v4l2_i2c_subdev_init(sd
, client
, &vs6624_ops
);
826 vs6624_writeregs(sd
, vs6624_p1
);
827 vs6624_write(sd
, VS6624_MICRO_EN
, 0x2);
828 vs6624_write(sd
, VS6624_DIO_EN
, 0x1);
830 vs6624_writeregs(sd
, vs6624_p2
);
832 vs6624_writeregs(sd
, vs6624_default
);
833 vs6624_write(sd
, VS6624_HSYNC_SETUP
, 0xF);
834 vs6624_writeregs(sd
, vs6624_run_setup
);
837 sensor
->frame_rate
.numerator
= MAX_FRAME_RATE
;
838 sensor
->frame_rate
.denominator
= 1;
839 vs6624_write(sd
, VS6624_DISABLE_FR_DAMPER
, 0x0);
840 vs6624_write(sd
, VS6624_FR_NUM_MSB
,
841 sensor
->frame_rate
.numerator
>> 8);
842 vs6624_write(sd
, VS6624_FR_NUM_LSB
,
843 sensor
->frame_rate
.numerator
& 0xFF);
844 vs6624_write(sd
, VS6624_FR_DEN
,
845 sensor
->frame_rate
.denominator
& 0xFF);
847 sensor
->fmt
= vs6624_default_fmt
;
848 sensor
->ce_pin
= *ce
;
850 v4l_info(client
, "chip found @ 0x%02x (%s)\n",
851 client
->addr
<< 1, client
->adapter
->name
);
854 v4l2_ctrl_handler_init(hdl
, 4);
855 v4l2_ctrl_new_std(hdl
, &vs6624_ctrl_ops
,
856 V4L2_CID_CONTRAST
, 0, 0xFF, 1, 0x87);
857 v4l2_ctrl_new_std(hdl
, &vs6624_ctrl_ops
,
858 V4L2_CID_SATURATION
, 0, 0xFF, 1, 0x78);
859 v4l2_ctrl_new_std(hdl
, &vs6624_ctrl_ops
,
860 V4L2_CID_HFLIP
, 0, 1, 1, 0);
861 v4l2_ctrl_new_std(hdl
, &vs6624_ctrl_ops
,
862 V4L2_CID_VFLIP
, 0, 1, 1, 0);
863 /* hook the control handler into the driver */
864 sd
->ctrl_handler
= hdl
;
866 int err
= hdl
->error
;
868 v4l2_ctrl_handler_free(hdl
);
874 /* initialize the hardware to the default control values */
875 ret
= v4l2_ctrl_handler_setup(hdl
);
877 v4l2_ctrl_handler_free(hdl
);
884 static int __devexit
vs6624_remove(struct i2c_client
*client
)
886 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
887 struct vs6624
*sensor
= to_vs6624(sd
);
889 v4l2_device_unregister_subdev(sd
);
890 v4l2_ctrl_handler_free(sd
->ctrl_handler
);
891 gpio_free(sensor
->ce_pin
);
896 static const struct i2c_device_id vs6624_id
[] = {
901 MODULE_DEVICE_TABLE(i2c
, vs6624_id
);
903 static struct i2c_driver vs6624_driver
= {
905 .owner
= THIS_MODULE
,
908 .probe
= vs6624_probe
,
909 .remove
= __devexit_p(vs6624_remove
),
910 .id_table
= vs6624_id
,
913 static __init
int vs6624_init(void)
915 return i2c_add_driver(&vs6624_driver
);
918 static __exit
void vs6624_exit(void)
920 i2c_del_driver(&vs6624_driver
);
923 module_init(vs6624_init
);
924 module_exit(vs6624_exit
);
926 MODULE_DESCRIPTION("VS6624 sensor driver");
927 MODULE_AUTHOR("Scott Jiang <Scott.Jiang.Linux@gmail.com>");
928 MODULE_LICENSE("GPL v2");