19 #include <linux/i2c.h>
21 #include <linux/slab.h>
22 #include <linux/module.h>
31 #define MODULE_NAME "SR030PC30"
37 #define POWER_CTRL_REG 0x0001
38 #define PAGEMODE_REG 0x03
39 #define DEVICE_ID_REG 0x0004
40 #define NOON010PC30_ID 0x86
41 #define SR030PC30_ID 0x8C
42 #define VDO_CTL1_REG 0x0010
43 #define SUBSAMPL_NONE_VGA 0
44 #define SUBSAMPL_QVGA 0x10
45 #define SUBSAMPL_QQVGA 0x20
46 #define VDO_CTL2_REG 0x0011
47 #define SYNC_CTL_REG 0x0012
48 #define WIN_ROWH_REG 0x0020
49 #define WIN_ROWL_REG 0x0021
50 #define WIN_COLH_REG 0x0022
51 #define WIN_COLL_REG 0x0023
52 #define WIN_HEIGHTH_REG 0x0024
53 #define WIN_HEIGHTL_REG 0x0025
54 #define WIN_WIDTHH_REG 0x0026
55 #define WIN_WIDTHL_REG 0x0027
56 #define HBLANKH_REG 0x0040
57 #define HBLANKL_REG 0x0041
58 #define VSYNCH_REG 0x0042
59 #define VSYNCL_REG 0x0043
61 #define ISP_CTL_REG(n) (0x1010 + (n))
62 #define YOFS_REG 0x1040
63 #define DARK_YOFS_REG 0x1041
64 #define AG_ABRTH_REG 0x1050
65 #define SAT_CTL_REG 0x1060
66 #define BSAT_REG 0x1061
67 #define RSAT_REG 0x1062
68 #define AG_SAT_TH_REG 0x1063
70 #define ZLPF_CTRL_REG 0x1110
71 #define ZLPF_CTRL2_REG 0x1112
72 #define ZLPF_AGH_THR_REG 0x1121
73 #define ZLPF_THR_REG 0x1160
74 #define ZLPF_DYN_THR_REG 0x1160
76 #define YCLPF_CTL1_REG 0x1240
77 #define YCLPF_CTL2_REG 0x1241
78 #define YCLPF_THR_REG 0x1250
79 #define BLPF_CTL_REG 0x1270
80 #define BLPF_THR1_REG 0x1274
81 #define BLPF_THR2_REG 0x1275
83 #define LENS_CTRL_REG 0x1410
84 #define LENS_XCEN_REG 0x1420
85 #define LENS_YCEN_REG 0x1421
86 #define LENS_R_COMP_REG 0x1422
87 #define LENS_G_COMP_REG 0x1423
88 #define LENS_B_COMP_REG 0x1424
90 #define CMC_CTL_REG 0x1510
91 #define CMC_OFSGH_REG 0x1514
92 #define CMC_OFSGL_REG 0x1516
93 #define CMC_SIGN_REG 0x1517
95 #define CMC_COEF_REG(n) (0x1530 + (n))
97 #define CMC_OFS_REG(n) (0x1540 + (n))
99 #define GMA_CTL_REG 0x1610
101 #define GMA_COEF_REG(n) (0x1630 + (n))
103 #define AE_CTL1_REG 0x2010
104 #define AE_CTL2_REG 0x2011
105 #define AE_FRM_CTL_REG 0x2020
106 #define AE_FINE_CTL_REG(n) (0x2028 + (n))
107 #define EXP_TIMEH_REG 0x2083
108 #define EXP_TIMEM_REG 0x2084
109 #define EXP_TIMEL_REG 0x2085
110 #define EXP_MMINH_REG 0x2086
111 #define EXP_MMINL_REG 0x2087
112 #define EXP_MMAXH_REG 0x2088
113 #define EXP_MMAXM_REG 0x2089
114 #define EXP_MMAXL_REG 0x208A
116 #define AWB_CTL1_REG 0x2210
117 #define AWB_ENABLE 0x80
118 #define AWB_CTL2_REG 0x2211
119 #define MWB_ENABLE 0x01
121 #define AWB_RGAIN_REG 0x2280
122 #define AWB_GGAIN_REG 0x2281
123 #define AWB_BGAIN_REG 0x2282
124 #define AWB_RMAX_REG 0x2283
125 #define AWB_RMIN_REG 0x2284
126 #define AWB_BMAX_REG 0x2285
127 #define AWB_BMIN_REG 0x2286
129 #define AWB_RMAXB_REG 0x2287
130 #define AWB_RMINB_REG 0x2288
131 #define AWB_BMAXB_REG 0x2289
132 #define AWB_BMINB_REG 0x228A
134 #define MWB_RGAIN_REG 0x22B2
135 #define MWB_BGAIN_REG 0x22B3
137 #define REG_TERM 0xFFFF
140 #define EXPOS_MIN_MS 1
141 #define EXPOS_MAX_MS 125
180 .name =
"Auto White Balance",
188 .name =
"Red Balance",
197 .name =
"Blue Balance",
205 .name =
"Auto Exposure",
264 static const struct i2c_regval sr030pc30_base_regs[] = {
333 if (info->
i2c_reg_page != page && (reg & 0xFF) != 0x03) {
343 struct i2c_client *client = v4l2_get_subdevdata(sd);
346 int ret = set_i2c_page(info, client, reg_addr);
354 struct i2c_client *client = v4l2_get_subdevdata(sd);
357 int ret = set_i2c_page(info, client, reg_addr);
360 client, reg_addr & 0xFF, val);
364 static inline int sr030pc30_bulk_write_reg(
struct v4l2_subdev *sd,
368 int ret = cam_i2c_write(sd, msg->
addr, msg->
val);
377 static int sr030pc30_pwr_ctrl(
struct v4l2_subdev *sd,
378 bool reset,
bool sleep)
381 u8 reg = sleep ? 0xF1 : 0xF0;
397 static inline int sr030pc30_enable_autoexposure(
struct v4l2_subdev *sd,
int on)
401 int ret = cam_i2c_write(sd,
AE_CTL1_REG, on ? 0xDC : 0x0C);
411 unsigned long expos = value * info->
pdata->clk_rate / (8 * 1000);
413 int ret = cam_i2c_write(sd,
EXP_TIMEH_REG, expos >> 16 & 0xFF);
420 ret = sr030pc30_enable_autoexposure(sd, 0);
426 static int sr030pc30_enable_autowhitebalance(
struct v4l2_subdev *sd,
int on)
430 int ret = cam_i2c_write(sd,
AWB_CTL2_REG, on ? 0x2E : 0x2F);
432 ret = cam_i2c_write(sd,
AWB_CTL1_REG, on ? 0xFB : 0x7B);
439 static int sr030pc30_set_flip(
struct v4l2_subdev *sd)
456 static int sr030pc30_set_params(
struct v4l2_subdev *sd)
472 ret = sr030pc30_set_flip(sd);
478 static int sr030pc30_try_frame_size(
struct v4l2_mbus_framefmt *mf)
480 unsigned int min_err = ~0;
494 mf->
width = match->width;
495 mf->height = match->height;
501 static int sr030pc30_queryctrl(
struct v4l2_subdev *sd,
506 for (i = 0; i <
ARRAY_SIZE(sr030pc30_ctrl); i++)
507 if (qc->
id == sr030pc30_ctrl[i].
id) {
508 *qc = sr030pc30_ctrl[
i];
517 static inline int sr030pc30_set_bluebalance(
struct v4l2_subdev *sd,
int value)
521 to_sr030pc30(sd)->blue_balance =
value;
525 static inline int sr030pc30_set_redbalance(
struct v4l2_subdev *sd,
int value)
529 to_sr030pc30(sd)->red_balance =
value;
533 static int sr030pc30_s_ctrl(
struct v4l2_subdev *sd,
538 for (i = 0; i <
ARRAY_SIZE(sr030pc30_ctrl); i++)
539 if (ctrl->
id == sr030pc30_ctrl[i].
id)
550 __func__, ctrl->
id, ctrl->
value);
554 sr030pc30_enable_autowhitebalance(sd, ctrl->
value);
557 ret = sr030pc30_set_bluebalance(sd, ctrl->
value);
560 ret = sr030pc30_set_redbalance(sd, ctrl->
value);
563 sr030pc30_enable_autoexposure(sd,
567 ret = sr030pc30_set_exposure(sd, ctrl->
value);
576 static int sr030pc30_g_ctrl(
struct v4l2_subdev *sd,
608 if (!code || index >=
ARRAY_SIZE(sr030pc30_formats))
616 struct v4l2_mbus_framefmt *mf)
625 ret = sr030pc30_set_params(sd);
631 mf->height = info->
curr_win->height;
633 mf->colorspace = info->
curr_fmt->colorspace;
641 struct v4l2_mbus_framefmt *mf)
645 sr030pc30_try_frame_size(mf);
648 if (mf->code == sr030pc30_formats[i].
code)
651 mf->code = sr030pc30_formats[
i].
code;
653 return &sr030pc30_formats[
i];
657 static int sr030pc30_try_fmt(
struct v4l2_subdev *sd,
658 struct v4l2_mbus_framefmt *mf)
668 struct v4l2_mbus_framefmt *mf)
677 return sr030pc30_set_params(sd);
680 static int sr030pc30_base_config(
struct v4l2_subdev *sd)
684 unsigned long expmin, expmax;
686 ret = sr030pc30_bulk_write_reg(sd, sr030pc30_base_regs);
688 info->
curr_fmt = &sr030pc30_formats[0];
689 info->
curr_win = &sr030pc30_sizes[0];
690 ret = sr030pc30_set_params(sd);
693 ret = sr030pc30_pwr_ctrl(sd,
false,
false);
695 if (!ret && !info->
pdata)
701 v4l2_dbg(1,
debug, sd,
"%s: expmin= %lx, expmax= %lx", __func__,
718 static int sr030pc30_s_power(
struct v4l2_subdev *sd,
int on)
720 struct i2c_client *client = v4l2_get_subdevdata(sd);
726 WARN(1,
"No platform data!\n");
735 sr030pc30_pwr_ctrl(sd,
false,
true);
745 ret = sr030pc30_base_config(sd);
756 .s_power = sr030pc30_s_power,
757 .queryctrl = sr030pc30_queryctrl,
758 .s_ctrl = sr030pc30_s_ctrl,
759 .g_ctrl = sr030pc30_g_ctrl,
763 .g_mbus_fmt = sr030pc30_g_fmt,
764 .s_mbus_fmt = sr030pc30_s_fmt,
765 .try_mbus_fmt = sr030pc30_try_fmt,
766 .enum_mbus_fmt = sr030pc30_enum_fmt,
770 .core = &sr030pc30_core_ops,
771 .video = &sr030pc30_video_ops,
778 static int sr030pc30_detect(
struct i2c_client *client)
781 = client->
dev.platform_data;
797 dev_err(&client->
dev,
"%s: I2C read failed\n", __func__);
805 static int sr030pc30_probe(
struct i2c_client *client,
811 = client->
dev.platform_data;
819 ret = sr030pc30_detect(client);
829 info->
pdata = client->
dev.platform_data;
841 static int sr030pc30_remove(
struct i2c_client *client)
843 struct v4l2_subdev *sd = i2c_get_clientdata(client);
858 static struct i2c_driver sr030pc30_i2c_driver = {
862 .probe = sr030pc30_probe,
863 .remove = sr030pc30_remove,
864 .id_table = sr030pc30_id,