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.
16 #include <linux/delay.h>
17 #include <linux/errno.h>
18 #include <linux/gpio.h>
19 #include <linux/i2c.h>
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/types.h>
24 #include <linux/videodev2.h>
26 #include <media/v4l2-ctrls.h>
27 #include <media/v4l2-device.h>
28 #include <media/v4l2-mediabus.h>
29 #include <media/v4l2-image-sizes.h>
31 #include "vs6624_regs.h"
33 #define MAX_FRAME_RATE 30
36 struct v4l2_subdev sd
;
37 struct v4l2_ctrl_handler hdl
;
38 struct v4l2_fract frame_rate
;
39 struct v4l2_mbus_framefmt fmt
;
43 static const struct vs6624_format
{
45 enum v4l2_colorspace colorspace
;
46 } vs6624_formats
[] = {
48 .mbus_code
= MEDIA_BUS_FMT_UYVY8_2X8
,
49 .colorspace
= V4L2_COLORSPACE_JPEG
,
52 .mbus_code
= MEDIA_BUS_FMT_YUYV8_2X8
,
53 .colorspace
= V4L2_COLORSPACE_JPEG
,
56 .mbus_code
= MEDIA_BUS_FMT_RGB565_2X8_LE
,
57 .colorspace
= V4L2_COLORSPACE_SRGB
,
61 static const struct v4l2_mbus_framefmt vs6624_default_fmt
= {
64 .code
= MEDIA_BUS_FMT_UYVY8_2X8
,
65 .field
= V4L2_FIELD_NONE
,
66 .colorspace
= V4L2_COLORSPACE_JPEG
,
69 static const u16 vs6624_p1
[] = {
395 static const u16 vs6624_p2
[] = {
401 static const u16 vs6624_run_setup
[] = {
402 0x1d18, 0x00, /* Enableconstrainedwhitebalance */
403 VS6624_PEAK_MIN_OUT_G_MSB
, 0x3c, /* Damper PeakGain Output MSB */
404 VS6624_PEAK_MIN_OUT_G_LSB
, 0x66, /* Damper PeakGain Output LSB */
405 VS6624_CM_LOW_THR_MSB
, 0x65, /* Damper Low MSB */
406 VS6624_CM_LOW_THR_LSB
, 0xd1, /* Damper Low LSB */
407 VS6624_CM_HIGH_THR_MSB
, 0x66, /* Damper High MSB */
408 VS6624_CM_HIGH_THR_LSB
, 0x62, /* Damper High LSB */
409 VS6624_CM_MIN_OUT_MSB
, 0x00, /* Damper Min output MSB */
410 VS6624_CM_MIN_OUT_LSB
, 0x00, /* Damper Min output LSB */
411 VS6624_NORA_DISABLE
, 0x00, /* Nora fDisable */
412 VS6624_NORA_USAGE
, 0x04, /* Nora usage */
413 VS6624_NORA_LOW_THR_MSB
, 0x63, /* Damper Low MSB Changed 0x63 to 0x65 */
414 VS6624_NORA_LOW_THR_LSB
, 0xd1, /* Damper Low LSB */
415 VS6624_NORA_HIGH_THR_MSB
, 0x68, /* Damper High MSB */
416 VS6624_NORA_HIGH_THR_LSB
, 0xdd, /* Damper High LSB */
417 VS6624_NORA_MIN_OUT_MSB
, 0x3a, /* Damper Min output MSB */
418 VS6624_NORA_MIN_OUT_LSB
, 0x00, /* Damper Min output LSB */
419 VS6624_F2B_DISABLE
, 0x00, /* Disable */
420 0x1d8a, 0x30, /* MAXWeightHigh */
421 0x1d91, 0x62, /* fpDamperLowThresholdHigh MSB */
422 0x1d92, 0x4a, /* fpDamperLowThresholdHigh LSB */
423 0x1d95, 0x65, /* fpDamperHighThresholdHigh MSB */
424 0x1d96, 0x0e, /* fpDamperHighThresholdHigh LSB */
425 0x1da1, 0x3a, /* fpMinimumDamperOutputLow MSB */
426 0x1da2, 0xb8, /* fpMinimumDamperOutputLow LSB */
427 0x1e08, 0x06, /* MAXWeightLow */
428 0x1e0a, 0x0a, /* MAXWeightHigh */
429 0x1601, 0x3a, /* Red A MSB */
430 0x1602, 0x14, /* Red A LSB */
431 0x1605, 0x3b, /* Blue A MSB */
432 0x1606, 0x85, /* BLue A LSB */
433 0x1609, 0x3b, /* RED B MSB */
434 0x160a, 0x85, /* RED B LSB */
435 0x160d, 0x3a, /* Blue B MSB */
436 0x160e, 0x14, /* Blue B LSB */
437 0x1611, 0x30, /* Max Distance from Locus MSB */
438 0x1612, 0x8f, /* Max Distance from Locus MSB */
439 0x1614, 0x01, /* Enable constrainer */
443 static const u16 vs6624_default
[] = {
444 VS6624_CONTRAST0
, 0x84,
445 VS6624_SATURATION0
, 0x75,
447 VS6624_CONTRAST1
, 0x84,
448 VS6624_SATURATION1
, 0x75,
454 VS6624_EXPO_COMPENSATION
, 0xfe,
455 VS6624_EXPO_METER
, 0x0,
456 VS6624_LIGHT_FREQ
, 0x64,
457 VS6624_PEAK_GAIN
, 0xe,
458 VS6624_PEAK_LOW_THR
, 0x28,
459 VS6624_HMIRROR0
, 0x0,
461 VS6624_ZOOM_HSTEP0_MSB
, 0x0,
462 VS6624_ZOOM_HSTEP0_LSB
, 0x1,
463 VS6624_ZOOM_VSTEP0_MSB
, 0x0,
464 VS6624_ZOOM_VSTEP0_LSB
, 0x1,
465 VS6624_PAN_HSTEP0_MSB
, 0x0,
466 VS6624_PAN_HSTEP0_LSB
, 0xf,
467 VS6624_PAN_VSTEP0_MSB
, 0x0,
468 VS6624_PAN_VSTEP0_LSB
, 0xf,
469 VS6624_SENSOR_MODE
, 0x1,
470 VS6624_SYNC_CODE_SETUP
, 0x21,
471 VS6624_DISABLE_FR_DAMPER
, 0x0,
473 VS6624_FR_NUM_LSB
, 0xf,
474 VS6624_INIT_PIPE_SETUP
, 0x0,
475 VS6624_IMG_FMT0
, 0x0,
476 VS6624_YUV_SETUP
, 0x1,
477 VS6624_IMAGE_SIZE0
, 0x2,
481 static inline struct vs6624
*to_vs6624(struct v4l2_subdev
*sd
)
483 return container_of(sd
, struct vs6624
, sd
);
485 static inline struct v4l2_subdev
*to_sd(struct v4l2_ctrl
*ctrl
)
487 return &container_of(ctrl
->handler
, struct vs6624
, hdl
)->sd
;
490 #ifdef CONFIG_VIDEO_ADV_DEBUG
491 static int vs6624_read(struct v4l2_subdev
*sd
, u16 index
)
493 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
498 i2c_master_send(client
, buf
, 2);
499 i2c_master_recv(client
, buf
, 1);
505 static int vs6624_write(struct v4l2_subdev
*sd
, u16 index
,
508 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
515 return i2c_master_send(client
, buf
, 3);
518 static int vs6624_writeregs(struct v4l2_subdev
*sd
, const u16
*regs
)
523 while (*regs
!= 0x00) {
527 vs6624_write(sd
, reg
, data
);
532 static int vs6624_s_ctrl(struct v4l2_ctrl
*ctrl
)
534 struct v4l2_subdev
*sd
= to_sd(ctrl
);
537 case V4L2_CID_CONTRAST
:
538 vs6624_write(sd
, VS6624_CONTRAST0
, ctrl
->val
);
540 case V4L2_CID_SATURATION
:
541 vs6624_write(sd
, VS6624_SATURATION0
, ctrl
->val
);
544 vs6624_write(sd
, VS6624_HMIRROR0
, ctrl
->val
);
547 vs6624_write(sd
, VS6624_VFLIP0
, ctrl
->val
);
556 static int vs6624_enum_mbus_code(struct v4l2_subdev
*sd
,
557 struct v4l2_subdev_pad_config
*cfg
,
558 struct v4l2_subdev_mbus_code_enum
*code
)
560 if (code
->pad
|| code
->index
>= ARRAY_SIZE(vs6624_formats
))
563 code
->code
= vs6624_formats
[code
->index
].mbus_code
;
567 static int vs6624_set_fmt(struct v4l2_subdev
*sd
,
568 struct v4l2_subdev_pad_config
*cfg
,
569 struct v4l2_subdev_format
*format
)
571 struct v4l2_mbus_framefmt
*fmt
= &format
->format
;
572 struct vs6624
*sensor
= to_vs6624(sd
);
578 for (index
= 0; index
< ARRAY_SIZE(vs6624_formats
); index
++)
579 if (vs6624_formats
[index
].mbus_code
== fmt
->code
)
581 if (index
>= ARRAY_SIZE(vs6624_formats
)) {
582 /* default to first format */
584 fmt
->code
= vs6624_formats
[0].mbus_code
;
587 /* sensor mode is VGA */
588 if (fmt
->width
> VGA_WIDTH
)
589 fmt
->width
= VGA_WIDTH
;
590 if (fmt
->height
> VGA_HEIGHT
)
591 fmt
->height
= VGA_HEIGHT
;
592 fmt
->width
= fmt
->width
& (~3);
593 fmt
->height
= fmt
->height
& (~3);
594 fmt
->field
= V4L2_FIELD_NONE
;
595 fmt
->colorspace
= vs6624_formats
[index
].colorspace
;
597 if (format
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
602 /* set image format */
604 case MEDIA_BUS_FMT_UYVY8_2X8
:
605 vs6624_write(sd
, VS6624_IMG_FMT0
, 0x0);
606 vs6624_write(sd
, VS6624_YUV_SETUP
, 0x1);
608 case MEDIA_BUS_FMT_YUYV8_2X8
:
609 vs6624_write(sd
, VS6624_IMG_FMT0
, 0x0);
610 vs6624_write(sd
, VS6624_YUV_SETUP
, 0x3);
612 case MEDIA_BUS_FMT_RGB565_2X8_LE
:
613 vs6624_write(sd
, VS6624_IMG_FMT0
, 0x4);
614 vs6624_write(sd
, VS6624_RGB_SETUP
, 0x0);
621 if ((fmt
->width
== VGA_WIDTH
) && (fmt
->height
== VGA_HEIGHT
))
622 vs6624_write(sd
, VS6624_IMAGE_SIZE0
, 0x2);
623 else if ((fmt
->width
== QVGA_WIDTH
) && (fmt
->height
== QVGA_HEIGHT
))
624 vs6624_write(sd
, VS6624_IMAGE_SIZE0
, 0x4);
625 else if ((fmt
->width
== QQVGA_WIDTH
) && (fmt
->height
== QQVGA_HEIGHT
))
626 vs6624_write(sd
, VS6624_IMAGE_SIZE0
, 0x6);
627 else if ((fmt
->width
== CIF_WIDTH
) && (fmt
->height
== CIF_HEIGHT
))
628 vs6624_write(sd
, VS6624_IMAGE_SIZE0
, 0x3);
629 else if ((fmt
->width
== QCIF_WIDTH
) && (fmt
->height
== QCIF_HEIGHT
))
630 vs6624_write(sd
, VS6624_IMAGE_SIZE0
, 0x5);
631 else if ((fmt
->width
== QQCIF_WIDTH
) && (fmt
->height
== QQCIF_HEIGHT
))
632 vs6624_write(sd
, VS6624_IMAGE_SIZE0
, 0x7);
634 vs6624_write(sd
, VS6624_IMAGE_SIZE0
, 0x8);
635 vs6624_write(sd
, VS6624_MAN_HSIZE0_MSB
, fmt
->width
>> 8);
636 vs6624_write(sd
, VS6624_MAN_HSIZE0_LSB
, fmt
->width
& 0xFF);
637 vs6624_write(sd
, VS6624_MAN_VSIZE0_MSB
, fmt
->height
>> 8);
638 vs6624_write(sd
, VS6624_MAN_VSIZE0_LSB
, fmt
->height
& 0xFF);
639 vs6624_write(sd
, VS6624_CROP_CTRL0
, 0x1);
647 static int vs6624_get_fmt(struct v4l2_subdev
*sd
,
648 struct v4l2_subdev_pad_config
*cfg
,
649 struct v4l2_subdev_format
*format
)
651 struct vs6624
*sensor
= to_vs6624(sd
);
656 format
->format
= sensor
->fmt
;
660 static int vs6624_g_parm(struct v4l2_subdev
*sd
, struct v4l2_streamparm
*parms
)
662 struct vs6624
*sensor
= to_vs6624(sd
);
663 struct v4l2_captureparm
*cp
= &parms
->parm
.capture
;
665 if (parms
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
668 memset(cp
, 0, sizeof(*cp
));
669 cp
->capability
= V4L2_CAP_TIMEPERFRAME
;
670 cp
->timeperframe
.numerator
= sensor
->frame_rate
.denominator
;
671 cp
->timeperframe
.denominator
= sensor
->frame_rate
.numerator
;
675 static int vs6624_s_parm(struct v4l2_subdev
*sd
, struct v4l2_streamparm
*parms
)
677 struct vs6624
*sensor
= to_vs6624(sd
);
678 struct v4l2_captureparm
*cp
= &parms
->parm
.capture
;
679 struct v4l2_fract
*tpf
= &cp
->timeperframe
;
681 if (parms
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
683 if (cp
->extendedmode
!= 0)
686 if (tpf
->numerator
== 0 || tpf
->denominator
== 0
687 || (tpf
->denominator
> tpf
->numerator
* MAX_FRAME_RATE
)) {
688 /* reset to max frame rate */
690 tpf
->denominator
= MAX_FRAME_RATE
;
692 sensor
->frame_rate
.numerator
= tpf
->denominator
;
693 sensor
->frame_rate
.denominator
= tpf
->numerator
;
694 vs6624_write(sd
, VS6624_DISABLE_FR_DAMPER
, 0x0);
695 vs6624_write(sd
, VS6624_FR_NUM_MSB
,
696 sensor
->frame_rate
.numerator
>> 8);
697 vs6624_write(sd
, VS6624_FR_NUM_LSB
,
698 sensor
->frame_rate
.numerator
& 0xFF);
699 vs6624_write(sd
, VS6624_FR_DEN
,
700 sensor
->frame_rate
.denominator
& 0xFF);
704 static int vs6624_s_stream(struct v4l2_subdev
*sd
, int enable
)
707 vs6624_write(sd
, VS6624_USER_CMD
, 0x2);
709 vs6624_write(sd
, VS6624_USER_CMD
, 0x4);
714 #ifdef CONFIG_VIDEO_ADV_DEBUG
715 static int vs6624_g_register(struct v4l2_subdev
*sd
, struct v4l2_dbg_register
*reg
)
717 reg
->val
= vs6624_read(sd
, reg
->reg
& 0xffff);
722 static int vs6624_s_register(struct v4l2_subdev
*sd
, const struct v4l2_dbg_register
*reg
)
724 vs6624_write(sd
, reg
->reg
& 0xffff, reg
->val
& 0xff);
729 static const struct v4l2_ctrl_ops vs6624_ctrl_ops
= {
730 .s_ctrl
= vs6624_s_ctrl
,
733 static const struct v4l2_subdev_core_ops vs6624_core_ops
= {
734 #ifdef CONFIG_VIDEO_ADV_DEBUG
735 .g_register
= vs6624_g_register
,
736 .s_register
= vs6624_s_register
,
740 static const struct v4l2_subdev_video_ops vs6624_video_ops
= {
741 .s_parm
= vs6624_s_parm
,
742 .g_parm
= vs6624_g_parm
,
743 .s_stream
= vs6624_s_stream
,
746 static const struct v4l2_subdev_pad_ops vs6624_pad_ops
= {
747 .enum_mbus_code
= vs6624_enum_mbus_code
,
748 .get_fmt
= vs6624_get_fmt
,
749 .set_fmt
= vs6624_set_fmt
,
752 static const struct v4l2_subdev_ops vs6624_ops
= {
753 .core
= &vs6624_core_ops
,
754 .video
= &vs6624_video_ops
,
755 .pad
= &vs6624_pad_ops
,
758 static int vs6624_probe(struct i2c_client
*client
,
759 const struct i2c_device_id
*id
)
761 struct vs6624
*sensor
;
762 struct v4l2_subdev
*sd
;
763 struct v4l2_ctrl_handler
*hdl
;
767 /* Check if the adapter supports the needed features */
768 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
))
771 ce
= client
->dev
.platform_data
;
775 ret
= devm_gpio_request_one(&client
->dev
, *ce
, GPIOF_OUT_INIT_HIGH
,
776 "VS6624 Chip Enable");
778 v4l_err(client
, "failed to request GPIO %d\n", *ce
);
781 /* wait 100ms before any further i2c writes are performed */
784 sensor
= devm_kzalloc(&client
->dev
, sizeof(*sensor
), GFP_KERNEL
);
789 v4l2_i2c_subdev_init(sd
, client
, &vs6624_ops
);
791 vs6624_writeregs(sd
, vs6624_p1
);
792 vs6624_write(sd
, VS6624_MICRO_EN
, 0x2);
793 vs6624_write(sd
, VS6624_DIO_EN
, 0x1);
795 vs6624_writeregs(sd
, vs6624_p2
);
797 vs6624_writeregs(sd
, vs6624_default
);
798 vs6624_write(sd
, VS6624_HSYNC_SETUP
, 0xF);
799 vs6624_writeregs(sd
, vs6624_run_setup
);
802 sensor
->frame_rate
.numerator
= MAX_FRAME_RATE
;
803 sensor
->frame_rate
.denominator
= 1;
804 vs6624_write(sd
, VS6624_DISABLE_FR_DAMPER
, 0x0);
805 vs6624_write(sd
, VS6624_FR_NUM_MSB
,
806 sensor
->frame_rate
.numerator
>> 8);
807 vs6624_write(sd
, VS6624_FR_NUM_LSB
,
808 sensor
->frame_rate
.numerator
& 0xFF);
809 vs6624_write(sd
, VS6624_FR_DEN
,
810 sensor
->frame_rate
.denominator
& 0xFF);
812 sensor
->fmt
= vs6624_default_fmt
;
813 sensor
->ce_pin
= *ce
;
815 v4l_info(client
, "chip found @ 0x%02x (%s)\n",
816 client
->addr
<< 1, client
->adapter
->name
);
819 v4l2_ctrl_handler_init(hdl
, 4);
820 v4l2_ctrl_new_std(hdl
, &vs6624_ctrl_ops
,
821 V4L2_CID_CONTRAST
, 0, 0xFF, 1, 0x87);
822 v4l2_ctrl_new_std(hdl
, &vs6624_ctrl_ops
,
823 V4L2_CID_SATURATION
, 0, 0xFF, 1, 0x78);
824 v4l2_ctrl_new_std(hdl
, &vs6624_ctrl_ops
,
825 V4L2_CID_HFLIP
, 0, 1, 1, 0);
826 v4l2_ctrl_new_std(hdl
, &vs6624_ctrl_ops
,
827 V4L2_CID_VFLIP
, 0, 1, 1, 0);
828 /* hook the control handler into the driver */
829 sd
->ctrl_handler
= hdl
;
831 int err
= hdl
->error
;
833 v4l2_ctrl_handler_free(hdl
);
837 /* initialize the hardware to the default control values */
838 ret
= v4l2_ctrl_handler_setup(hdl
);
840 v4l2_ctrl_handler_free(hdl
);
844 static int vs6624_remove(struct i2c_client
*client
)
846 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
848 v4l2_device_unregister_subdev(sd
);
849 v4l2_ctrl_handler_free(sd
->ctrl_handler
);
853 static const struct i2c_device_id vs6624_id
[] = {
858 MODULE_DEVICE_TABLE(i2c
, vs6624_id
);
860 static struct i2c_driver vs6624_driver
= {
864 .probe
= vs6624_probe
,
865 .remove
= vs6624_remove
,
866 .id_table
= vs6624_id
,
869 module_i2c_driver(vs6624_driver
);
871 MODULE_DESCRIPTION("VS6624 sensor driver");
872 MODULE_AUTHOR("Scott Jiang <Scott.Jiang.Linux@gmail.com>");
873 MODULE_LICENSE("GPL v2");