xref: /OK3568_Linux_fs/kernel/drivers/media/usb/gspca/stv06xx/stv06xx_st6422.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Support for the sensor part which is integrated (I think) into the
4*4882a593Smuzhiyun  * st6422 stv06xx alike bridge, as its integrated there are no i2c writes
5*4882a593Smuzhiyun  * but instead direct bridge writes.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Copyright (c) 2009 Hans de Goede <hdegoede@redhat.com>
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * Strongly based on qc-usb-messenger, which is:
10*4882a593Smuzhiyun  * Copyright (c) 2001 Jean-Fredric Clere, Nikolas Zimmermann, Georg Acher
11*4882a593Smuzhiyun  *		      Mark Cave-Ayland, Carlo E Prelz, Dick Streefland
12*4882a593Smuzhiyun  * Copyright (c) 2002, 2003 Tuukka Toivonen
13*4882a593Smuzhiyun  */
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #include "stv06xx_st6422.h"
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun static struct v4l2_pix_format st6422_mode[] = {
20*4882a593Smuzhiyun 	/* Note we actually get 124 lines of data, of which we skip the 4st
21*4882a593Smuzhiyun 	   4 as they are garbage */
22*4882a593Smuzhiyun 	{
23*4882a593Smuzhiyun 		162,
24*4882a593Smuzhiyun 		120,
25*4882a593Smuzhiyun 		V4L2_PIX_FMT_SGRBG8,
26*4882a593Smuzhiyun 		V4L2_FIELD_NONE,
27*4882a593Smuzhiyun 		.sizeimage = 162 * 120,
28*4882a593Smuzhiyun 		.bytesperline = 162,
29*4882a593Smuzhiyun 		.colorspace = V4L2_COLORSPACE_SRGB,
30*4882a593Smuzhiyun 		.priv = 1
31*4882a593Smuzhiyun 	},
32*4882a593Smuzhiyun 	/* Note we actually get 248 lines of data, of which we skip the 4st
33*4882a593Smuzhiyun 	   4 as they are garbage, and we tell the app it only gets the
34*4882a593Smuzhiyun 	   first 240 of the 244 lines it actually gets, so that it ignores
35*4882a593Smuzhiyun 	   the last 4. */
36*4882a593Smuzhiyun 	{
37*4882a593Smuzhiyun 		324,
38*4882a593Smuzhiyun 		240,
39*4882a593Smuzhiyun 		V4L2_PIX_FMT_SGRBG8,
40*4882a593Smuzhiyun 		V4L2_FIELD_NONE,
41*4882a593Smuzhiyun 		.sizeimage = 324 * 244,
42*4882a593Smuzhiyun 		.bytesperline = 324,
43*4882a593Smuzhiyun 		.colorspace = V4L2_COLORSPACE_SRGB,
44*4882a593Smuzhiyun 		.priv = 0
45*4882a593Smuzhiyun 	},
46*4882a593Smuzhiyun };
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun /* V4L2 controls supported by the driver */
49*4882a593Smuzhiyun static int setbrightness(struct sd *sd, s32 val);
50*4882a593Smuzhiyun static int setcontrast(struct sd *sd, s32 val);
51*4882a593Smuzhiyun static int setgain(struct sd *sd, u8 gain);
52*4882a593Smuzhiyun static int setexposure(struct sd *sd, s16 expo);
53*4882a593Smuzhiyun 
st6422_s_ctrl(struct v4l2_ctrl * ctrl)54*4882a593Smuzhiyun static int st6422_s_ctrl(struct v4l2_ctrl *ctrl)
55*4882a593Smuzhiyun {
56*4882a593Smuzhiyun 	struct gspca_dev *gspca_dev =
57*4882a593Smuzhiyun 		container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
58*4882a593Smuzhiyun 	struct sd *sd = (struct sd *)gspca_dev;
59*4882a593Smuzhiyun 	int err = -EINVAL;
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	switch (ctrl->id) {
62*4882a593Smuzhiyun 	case V4L2_CID_BRIGHTNESS:
63*4882a593Smuzhiyun 		err = setbrightness(sd, ctrl->val);
64*4882a593Smuzhiyun 		break;
65*4882a593Smuzhiyun 	case V4L2_CID_CONTRAST:
66*4882a593Smuzhiyun 		err = setcontrast(sd, ctrl->val);
67*4882a593Smuzhiyun 		break;
68*4882a593Smuzhiyun 	case V4L2_CID_GAIN:
69*4882a593Smuzhiyun 		err = setgain(sd, ctrl->val);
70*4882a593Smuzhiyun 		break;
71*4882a593Smuzhiyun 	case V4L2_CID_EXPOSURE:
72*4882a593Smuzhiyun 		err = setexposure(sd, ctrl->val);
73*4882a593Smuzhiyun 		break;
74*4882a593Smuzhiyun 	}
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	/* commit settings */
77*4882a593Smuzhiyun 	if (err >= 0)
78*4882a593Smuzhiyun 		err = stv06xx_write_bridge(sd, 0x143f, 0x01);
79*4882a593Smuzhiyun 	sd->gspca_dev.usb_err = err;
80*4882a593Smuzhiyun 	return err;
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun static const struct v4l2_ctrl_ops st6422_ctrl_ops = {
84*4882a593Smuzhiyun 	.s_ctrl = st6422_s_ctrl,
85*4882a593Smuzhiyun };
86*4882a593Smuzhiyun 
st6422_init_controls(struct sd * sd)87*4882a593Smuzhiyun static int st6422_init_controls(struct sd *sd)
88*4882a593Smuzhiyun {
89*4882a593Smuzhiyun 	struct v4l2_ctrl_handler *hdl = &sd->gspca_dev.ctrl_handler;
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	v4l2_ctrl_handler_init(hdl, 4);
92*4882a593Smuzhiyun 	v4l2_ctrl_new_std(hdl, &st6422_ctrl_ops,
93*4882a593Smuzhiyun 			V4L2_CID_BRIGHTNESS, 0, 31, 1, 3);
94*4882a593Smuzhiyun 	v4l2_ctrl_new_std(hdl, &st6422_ctrl_ops,
95*4882a593Smuzhiyun 			V4L2_CID_CONTRAST, 0, 15, 1, 11);
96*4882a593Smuzhiyun 	v4l2_ctrl_new_std(hdl, &st6422_ctrl_ops,
97*4882a593Smuzhiyun 			V4L2_CID_EXPOSURE, 0, 1023, 1, 256);
98*4882a593Smuzhiyun 	v4l2_ctrl_new_std(hdl, &st6422_ctrl_ops,
99*4882a593Smuzhiyun 			V4L2_CID_GAIN, 0, 255, 1, 64);
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	return hdl->error;
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun 
st6422_probe(struct sd * sd)104*4882a593Smuzhiyun static int st6422_probe(struct sd *sd)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun 	if (sd->bridge != BRIDGE_ST6422)
107*4882a593Smuzhiyun 		return -ENODEV;
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	pr_info("st6422 sensor detected\n");
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	sd->gspca_dev.cam.cam_mode = st6422_mode;
112*4882a593Smuzhiyun 	sd->gspca_dev.cam.nmodes = ARRAY_SIZE(st6422_mode);
113*4882a593Smuzhiyun 	return 0;
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun 
st6422_init(struct sd * sd)116*4882a593Smuzhiyun static int st6422_init(struct sd *sd)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun 	int err = 0, i;
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	static const u16 st6422_bridge_init[][2] = {
121*4882a593Smuzhiyun 		{ STV_ISO_ENABLE, 0x00 }, /* disable capture */
122*4882a593Smuzhiyun 		{ 0x1436, 0x00 },
123*4882a593Smuzhiyun 		{ 0x1432, 0x03 },	/* 0x00-0x1F brightness */
124*4882a593Smuzhiyun 		{ 0x143a, 0xf9 },	/* 0x00-0x0F contrast */
125*4882a593Smuzhiyun 		{ 0x0509, 0x38 },	/* R */
126*4882a593Smuzhiyun 		{ 0x050a, 0x38 },	/* G */
127*4882a593Smuzhiyun 		{ 0x050b, 0x38 },	/* B */
128*4882a593Smuzhiyun 		{ 0x050c, 0x2a },
129*4882a593Smuzhiyun 		{ 0x050d, 0x01 },
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 		{ 0x1431, 0x00 },	/* 0x00-0x07 ??? */
133*4882a593Smuzhiyun 		{ 0x1433, 0x34 },	/* 160x120, 0x00-0x01 night filter */
134*4882a593Smuzhiyun 		{ 0x1438, 0x18 },	/* 640x480 */
135*4882a593Smuzhiyun /* 18 bayes */
136*4882a593Smuzhiyun /* 10 compressed? */
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 		{ 0x1439, 0x00 },
139*4882a593Smuzhiyun /* anti-noise?  0xa2 gives a perfect image */
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 		{ 0x143b, 0x05 },
142*4882a593Smuzhiyun 		{ 0x143c, 0x00 },	/* 0x00-0x01 - ??? */
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun /* shutter time 0x0000-0x03FF */
146*4882a593Smuzhiyun /* low value  give good picures on moving objects (but requires much light) */
147*4882a593Smuzhiyun /* high value gives good picures in darkness (but tends to be overexposed) */
148*4882a593Smuzhiyun 		{ 0x143e, 0x01 },
149*4882a593Smuzhiyun 		{ 0x143d, 0x00 },
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 		{ 0x1442, 0xe2 },
152*4882a593Smuzhiyun /* write: 1x1x xxxx */
153*4882a593Smuzhiyun /* read:  1x1x xxxx */
154*4882a593Smuzhiyun /*        bit 5 == button pressed and hold if 0 */
155*4882a593Smuzhiyun /* write 0xe2,0xea */
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun /* 0x144a */
158*4882a593Smuzhiyun /* 0x00 init */
159*4882a593Smuzhiyun /* bit 7 == button has been pressed, but not handled */
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun /* interrupt */
162*4882a593Smuzhiyun /* if(urb->iso_frame_desc[i].status == 0x80) { */
163*4882a593Smuzhiyun /* if(urb->iso_frame_desc[i].status == 0x88) { */
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 		{ 0x1500, 0xd0 },
166*4882a593Smuzhiyun 		{ 0x1500, 0xd0 },
167*4882a593Smuzhiyun 		{ 0x1500, 0x50 },	/* 0x00 - 0xFF  0x80 == compr ? */
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 		{ 0x1501, 0xaf },
170*4882a593Smuzhiyun /* high val-> light area gets darker */
171*4882a593Smuzhiyun /* low val -> light area gets lighter */
172*4882a593Smuzhiyun 		{ 0x1502, 0xc2 },
173*4882a593Smuzhiyun /* high val-> light area gets darker */
174*4882a593Smuzhiyun /* low val -> light area gets lighter */
175*4882a593Smuzhiyun 		{ 0x1503, 0x45 },
176*4882a593Smuzhiyun /* high val-> light area gets darker */
177*4882a593Smuzhiyun /* low val -> light area gets lighter */
178*4882a593Smuzhiyun 		{ 0x1505, 0x02 },
179*4882a593Smuzhiyun /* 2  : 324x248  80352 bytes */
180*4882a593Smuzhiyun /* 7  : 248x162  40176 bytes */
181*4882a593Smuzhiyun /* c+f: 162*124  20088 bytes */
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 		{ 0x150e, 0x8e },
184*4882a593Smuzhiyun 		{ 0x150f, 0x37 },
185*4882a593Smuzhiyun 		{ 0x15c0, 0x00 },
186*4882a593Smuzhiyun 		{ 0x15c3, 0x08 },	/* 0x04/0x14 ... test pictures ??? */
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 		{ 0x143f, 0x01 },	/* commit settings */
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 	};
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(st6422_bridge_init) && !err; i++) {
194*4882a593Smuzhiyun 		err = stv06xx_write_bridge(sd, st6422_bridge_init[i][0],
195*4882a593Smuzhiyun 					       st6422_bridge_init[i][1]);
196*4882a593Smuzhiyun 	}
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	return err;
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun 
setbrightness(struct sd * sd,s32 val)201*4882a593Smuzhiyun static int setbrightness(struct sd *sd, s32 val)
202*4882a593Smuzhiyun {
203*4882a593Smuzhiyun 	/* val goes from 0 -> 31 */
204*4882a593Smuzhiyun 	return stv06xx_write_bridge(sd, 0x1432, val);
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun 
setcontrast(struct sd * sd,s32 val)207*4882a593Smuzhiyun static int setcontrast(struct sd *sd, s32 val)
208*4882a593Smuzhiyun {
209*4882a593Smuzhiyun 	/* Val goes from 0 -> 15 */
210*4882a593Smuzhiyun 	return stv06xx_write_bridge(sd, 0x143a, val | 0xf0);
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun 
setgain(struct sd * sd,u8 gain)213*4882a593Smuzhiyun static int setgain(struct sd *sd, u8 gain)
214*4882a593Smuzhiyun {
215*4882a593Smuzhiyun 	int err;
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	/* Set red, green, blue, gain */
218*4882a593Smuzhiyun 	err = stv06xx_write_bridge(sd, 0x0509, gain);
219*4882a593Smuzhiyun 	if (err < 0)
220*4882a593Smuzhiyun 		return err;
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	err = stv06xx_write_bridge(sd, 0x050a, gain);
223*4882a593Smuzhiyun 	if (err < 0)
224*4882a593Smuzhiyun 		return err;
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	err = stv06xx_write_bridge(sd, 0x050b, gain);
227*4882a593Smuzhiyun 	if (err < 0)
228*4882a593Smuzhiyun 		return err;
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 	/* 2 mystery writes */
231*4882a593Smuzhiyun 	err = stv06xx_write_bridge(sd, 0x050c, 0x2a);
232*4882a593Smuzhiyun 	if (err < 0)
233*4882a593Smuzhiyun 		return err;
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	return stv06xx_write_bridge(sd, 0x050d, 0x01);
236*4882a593Smuzhiyun }
237*4882a593Smuzhiyun 
setexposure(struct sd * sd,s16 expo)238*4882a593Smuzhiyun static int setexposure(struct sd *sd, s16 expo)
239*4882a593Smuzhiyun {
240*4882a593Smuzhiyun 	int err;
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	err = stv06xx_write_bridge(sd, 0x143d, expo & 0xff);
243*4882a593Smuzhiyun 	if (err < 0)
244*4882a593Smuzhiyun 		return err;
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	return stv06xx_write_bridge(sd, 0x143e, expo >> 8);
247*4882a593Smuzhiyun }
248*4882a593Smuzhiyun 
st6422_start(struct sd * sd)249*4882a593Smuzhiyun static int st6422_start(struct sd *sd)
250*4882a593Smuzhiyun {
251*4882a593Smuzhiyun 	int err;
252*4882a593Smuzhiyun 	struct cam *cam = &sd->gspca_dev.cam;
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	if (cam->cam_mode[sd->gspca_dev.curr_mode].priv)
255*4882a593Smuzhiyun 		err = stv06xx_write_bridge(sd, 0x1505, 0x0f);
256*4882a593Smuzhiyun 	else
257*4882a593Smuzhiyun 		err = stv06xx_write_bridge(sd, 0x1505, 0x02);
258*4882a593Smuzhiyun 	if (err < 0)
259*4882a593Smuzhiyun 		return err;
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	/* commit settings */
262*4882a593Smuzhiyun 	err = stv06xx_write_bridge(sd, 0x143f, 0x01);
263*4882a593Smuzhiyun 	return (err < 0) ? err : 0;
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun 
st6422_stop(struct sd * sd)266*4882a593Smuzhiyun static int st6422_stop(struct sd *sd)
267*4882a593Smuzhiyun {
268*4882a593Smuzhiyun 	struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	gspca_dbg(gspca_dev, D_STREAM, "Halting stream\n");
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 	return 0;
273*4882a593Smuzhiyun }
274