1// SPDX-License-Identifier: GPL-2.0-or-later
2/* Subdriver for the GL860 chip with the MI1320 sensor
3 * Author Olivier LORIN from own logs
4 */
5
6/* Sensor : MI1320 */
7
8#include "gl860.h"
9
10static struct validx tbl_common[] = {
11	{0xba00, 0x00f0}, {0xba00, 0x00f1}, {0xba51, 0x0066}, {0xba02, 0x00f1},
12	{0xba05, 0x0067}, {0xba05, 0x00f1}, {0xbaa0, 0x0065}, {0xba00, 0x00f1},
13	{0xffff, 0xffff},
14	{0xba00, 0x00f0}, {0xba02, 0x00f1}, {0xbafa, 0x0028}, {0xba02, 0x00f1},
15	{0xba00, 0x00f0}, {0xba01, 0x00f1}, {0xbaf0, 0x0006}, {0xba0e, 0x00f1},
16	{0xba70, 0x0006}, {0xba0e, 0x00f1},
17	{0xffff, 0xffff},
18	{0xba74, 0x0006}, {0xba0e, 0x00f1},
19	{0xffff, 0xffff},
20	{0x0061, 0x0000}, {0x0068, 0x000d},
21};
22
23static struct validx tbl_init_at_startup[] = {
24	{0x0000, 0x0000}, {0x0010, 0x0010},
25	{35, 0xffff},
26	{0x0008, 0x00c0}, {0x0001, 0x00c1}, {0x0001, 0x00c2}, {0x0020, 0x0006},
27	{0x006a, 0x000d},
28};
29
30static struct validx tbl_sensor_settings_common[] = {
31	{0x0010, 0x0010}, {0x0003, 0x00c1}, {0x0042, 0x00c2}, {0x0040, 0x0000},
32	{0x006a, 0x0007}, {0x006a, 0x000d}, {0x0063, 0x0006},
33};
34static struct validx tbl_sensor_settings_1280[] = {
35	{0xba00, 0x00f0}, {0xba00, 0x00f1}, {0xba5a, 0x0066}, {0xba02, 0x00f1},
36	{0xba05, 0x0067}, {0xba05, 0x00f1}, {0xba20, 0x0065}, {0xba00, 0x00f1},
37};
38static struct validx tbl_sensor_settings_800[] = {
39	{0xba00, 0x00f0}, {0xba00, 0x00f1}, {0xba5a, 0x0066}, {0xba02, 0x00f1},
40	{0xba05, 0x0067}, {0xba05, 0x00f1}, {0xba20, 0x0065}, {0xba00, 0x00f1},
41};
42static struct validx tbl_sensor_settings_640[] = {
43	{0xba00, 0x00f0}, {0xba00, 0x00f1}, {0xbaa0, 0x0065}, {0xba00, 0x00f1},
44	{0xba51, 0x0066}, {0xba02, 0x00f1}, {0xba05, 0x0067}, {0xba05, 0x00f1},
45	{0xba20, 0x0065}, {0xba00, 0x00f1},
46};
47static struct validx tbl_post_unset_alt[] = {
48	{0xba00, 0x00f0}, {0xba00, 0x00f1}, {0xbaa0, 0x0065}, {0xba00, 0x00f1},
49	{0x0061, 0x0000}, {0x0068, 0x000d},
50};
51
52static u8 *tbl_1280[] = {
53	"\x0d\x80\xf1\x08\x03\x04\xf1\x00" "\x04\x05\xf1\x02\x05\x00\xf1\xf1"
54	"\x06\x00\xf1\x0d\x20\x01\xf1\x00" "\x21\x84\xf1\x00\x0d\x00\xf1\x08"
55	"\xf0\x00\xf1\x01\x34\x00\xf1\x00" "\x9b\x43\xf1\x00\xa6\x05\xf1\x00"
56	"\xa9\x04\xf1\x00\xa1\x05\xf1\x00" "\xa4\x04\xf1\x00\xae\x0a\xf1\x08"
57	,
58	"\xf0\x00\xf1\x02\x3a\x05\xf1\xf1" "\x3c\x05\xf1\xf1\x59\x01\xf1\x47"
59	"\x5a\x01\xf1\x88\x5c\x0a\xf1\x06" "\x5d\x0e\xf1\x0a\x64\x5e\xf1\x1c"
60	"\xd2\x00\xf1\xcf\xcb\x00\xf1\x01"
61	,
62	"\xd3\x02\xd4\x28\xd5\x01\xd0\x02" "\xd1\x18\xd2\xc1"
63};
64
65static u8 *tbl_800[] = {
66	"\x0d\x80\xf1\x08\x03\x03\xf1\xc0" "\x04\x05\xf1\x02\x05\x00\xf1\xf1"
67	"\x06\x00\xf1\x0d\x20\x01\xf1\x00" "\x21\x84\xf1\x00\x0d\x00\xf1\x08"
68	"\xf0\x00\xf1\x01\x34\x00\xf1\x00" "\x9b\x43\xf1\x00\xa6\x05\xf1\x00"
69	"\xa9\x03\xf1\xc0\xa1\x03\xf1\x20" "\xa4\x02\xf1\x5a\xae\x0a\xf1\x08"
70	,
71	"\xf0\x00\xf1\x02\x3a\x05\xf1\xf1" "\x3c\x05\xf1\xf1\x59\x01\xf1\x47"
72	"\x5a\x01\xf1\x88\x5c\x0a\xf1\x06" "\x5d\x0e\xf1\x0a\x64\x5e\xf1\x1c"
73	"\xd2\x00\xf1\xcf\xcb\x00\xf1\x01"
74	,
75	"\xd3\x02\xd4\x18\xd5\x21\xd0\x02" "\xd1\x10\xd2\x59"
76};
77
78static u8 *tbl_640[] = {
79	"\x0d\x80\xf1\x08\x03\x04\xf1\x04" "\x04\x05\xf1\x02\x07\x01\xf1\x7c"
80	"\x08\x00\xf1\x0e\x21\x80\xf1\x00" "\x0d\x00\xf1\x08\xf0\x00\xf1\x01"
81	"\x34\x10\xf1\x10\x3a\x43\xf1\x00" "\xa6\x05\xf1\x02\xa9\x04\xf1\x04"
82	"\xa7\x02\xf1\x81\xaa\x01\xf1\xe2" "\xae\x0c\xf1\x09"
83	,
84	"\xf0\x00\xf1\x02\x39\x03\xf1\xfc" "\x3b\x04\xf1\x04\x57\x01\xf1\xb6"
85	"\x58\x02\xf1\x0d\x5c\x1f\xf1\x19" "\x5d\x24\xf1\x1e\x64\x5e\xf1\x1c"
86	"\xd2\x00\xf1\x00\xcb\x00\xf1\x01"
87	,
88	"\xd3\x02\xd4\x10\xd5\x81\xd0\x02" "\xd1\x08\xd2\xe1"
89};
90
91static s32 tbl_sat[] = {0x25, 0x1d, 0x15, 0x0d, 0x05, 0x4d, 0x55, 0x5d, 0x2d};
92static s32 tbl_bright[] = {0, 8, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70};
93static s32 tbl_backlight[] = {0x0e, 0x06, 0x02};
94
95static s32 tbl_cntr1[] = {
96	0x90, 0x98, 0xa0, 0xa8, 0xb0, 0xb8, 0xc0, 0xc8, 0xd0, 0xe0, 0xf0};
97static s32 tbl_cntr2[] = {
98	0x70, 0x68, 0x60, 0x58, 0x50, 0x48, 0x40, 0x38, 0x30, 0x20, 0x10};
99
100static u8 dat_wbalNL[] =
101	"\xf0\x00\xf1\x01\x05\x00\xf1\x06" "\x3b\x04\xf1\x2a\x47\x10\xf1\x10"
102	"\x9d\x3c\xf1\xae\xaf\x10\xf1\x00" "\xf0\x00\xf1\x02\x2f\x91\xf1\x20"
103	"\x9c\x91\xf1\x20\x37\x03\xf1\x00" "\x9d\xc5\xf1\x0f\xf0\x00\xf1\x00";
104
105static u8 dat_wbalLL[] =
106	"\xf0\x00\xf1\x01\x05\x00\xf1\x0c" "\x3b\x04\xf1\x2a\x47\x40\xf1\x40"
107	"\x9d\x20\xf1\xae\xaf\x10\xf1\x00" "\xf0\x00\xf1\x02\x2f\xd1\xf1\x00"
108	"\x9c\xd1\xf1\x00\x37\x03\xf1\x00" "\x9d\xc5\xf1\x3f\xf0\x00\xf1\x00";
109
110static u8 dat_wbalBL[] =
111	"\xf0\x00\xf1\x01\x05\x00\xf1\x06" "\x47\x10\xf1\x30\x9d\x3c\xf1\xae"
112	"\xaf\x10\xf1\x00\xf0\x00\xf1\x02" "\x2f\x91\xf1\x20\x9c\x91\xf1\x20"
113	"\x37\x03\xf1\x00\x9d\xc5\xf1\x2f" "\xf0\x00\xf1\x00";
114
115static u8 dat_hvflip1[] = {0xf0, 0x00, 0xf1, 0x00};
116
117static u8 dat_common00[] =
118	"\x00\x01\x07\x6a\x06\x63\x0d\x6a" "\xc0\x00\x10\x10\xc1\x03\xc2\x42"
119	"\xd8\x04\x58\x00\x04\x02";
120static u8 dat_common01[] =
121	"\x0d\x00\xf1\x0b\x0d\x00\xf1\x08" "\x35\x00\xf1\x22\x68\x00\xf1\x5d"
122	"\xf0\x00\xf1\x01\x06\x70\xf1\x0e" "\xf0\x00\xf1\x02\xdd\x18\xf1\xe0";
123static u8 dat_common02[] =
124	"\x05\x01\xf1\x84\x06\x00\xf1\x44" "\x07\x00\xf1\xbe\x08\x00\xf1\x1e"
125	"\x20\x01\xf1\x03\x21\x84\xf1\x00" "\x22\x0d\xf1\x0f\x24\x80\xf1\x00"
126	"\x34\x18\xf1\x2d\x35\x00\xf1\x22" "\x43\x83\xf1\x83\x59\x00\xf1\xff";
127static u8 dat_common03[] =
128	"\xf0\x00\xf1\x02\x39\x06\xf1\x8c" "\x3a\x06\xf1\x8c\x3b\x03\xf1\xda"
129	"\x3c\x05\xf1\x30\x57\x01\xf1\x0c" "\x58\x01\xf1\x42\x59\x01\xf1\x0c"
130	"\x5a\x01\xf1\x42\x5c\x13\xf1\x0e" "\x5d\x17\xf1\x12\x64\x1e\xf1\x1c";
131static u8 dat_common04[] =
132	"\xf0\x00\xf1\x02\x24\x5f\xf1\x20" "\x28\xea\xf1\x02\x5f\x41\xf1\x43";
133static u8 dat_common05[] =
134	"\x02\x00\xf1\xee\x03\x29\xf1\x1a" "\x04\x02\xf1\xa4\x09\x00\xf1\x68"
135	"\x0a\x00\xf1\x2a\x0b\x00\xf1\x04" "\x0c\x00\xf1\x93\x0d\x00\xf1\x82"
136	"\x0e\x00\xf1\x40\x0f\x00\xf1\x5f" "\x10\x00\xf1\x4e\x11\x00\xf1\x5b";
137static u8 dat_common06[] =
138	"\x15\x00\xf1\xc9\x16\x00\xf1\x5e" "\x17\x00\xf1\x9d\x18\x00\xf1\x06"
139	"\x19\x00\xf1\x89\x1a\x00\xf1\x12" "\x1b\x00\xf1\xa1\x1c\x00\xf1\xe4"
140	"\x1d\x00\xf1\x7a\x1e\x00\xf1\x64" "\xf6\x00\xf1\x5f";
141static u8 dat_common07[] =
142	"\xf0\x00\xf1\x01\x53\x09\xf1\x03" "\x54\x3d\xf1\x1c\x55\x99\xf1\x72"
143	"\x56\xc1\xf1\xb1\x57\xd8\xf1\xce" "\x58\xe0\xf1\x00\xdc\x0a\xf1\x03"
144	"\xdd\x45\xf1\x20\xde\xae\xf1\x82" "\xdf\xdc\xf1\xc9\xe0\xf6\xf1\xea"
145	"\xe1\xff\xf1\x00";
146static u8 dat_common08[] =
147	"\xf0\x00\xf1\x01\x80\x00\xf1\x06" "\x81\xf6\xf1\x08\x82\xfb\xf1\xf7"
148	"\x83\x00\xf1\xfe\xb6\x07\xf1\x03" "\xb7\x18\xf1\x0c\x84\xfb\xf1\x06"
149	"\x85\xfb\xf1\xf9\x86\x00\xf1\xff" "\xb8\x07\xf1\x04\xb9\x16\xf1\x0a";
150static u8 dat_common09[] =
151	"\x87\xfa\xf1\x05\x88\xfc\xf1\xf9" "\x89\x00\xf1\xff\xba\x06\xf1\x03"
152	"\xbb\x17\xf1\x09\x8a\xe8\xf1\x14" "\x8b\xf7\xf1\xf0\x8c\xfd\xf1\xfa"
153	"\x8d\x00\xf1\x00\xbc\x05\xf1\x01" "\xbd\x0c\xf1\x08\xbe\x00\xf1\x14";
154static u8 dat_common10[] =
155	"\x8e\xea\xf1\x13\x8f\xf7\xf1\xf2" "\x90\xfd\xf1\xfa\x91\x00\xf1\x00"
156	"\xbf\x05\xf1\x01\xc0\x0a\xf1\x08" "\xc1\x00\xf1\x0c\x92\xed\xf1\x0f"
157	"\x93\xf9\xf1\xf4\x94\xfe\xf1\xfb" "\x95\x00\xf1\x00\xc2\x04\xf1\x01"
158	"\xc3\x0a\xf1\x07\xc4\x00\xf1\x10";
159static u8 dat_common11[] =
160	"\xf0\x00\xf1\x01\x05\x00\xf1\x06" "\x25\x00\xf1\x55\x34\x10\xf1\x10"
161	"\x35\xf0\xf1\x10\x3a\x02\xf1\x03" "\x3b\x04\xf1\x2a\x9b\x43\xf1\x00"
162	"\xa4\x03\xf1\xc0\xa7\x02\xf1\x81";
163
164static int  mi1320_init_at_startup(struct gspca_dev *gspca_dev);
165static int  mi1320_configure_alt(struct gspca_dev *gspca_dev);
166static int  mi1320_init_pre_alt(struct gspca_dev *gspca_dev);
167static int  mi1320_init_post_alt(struct gspca_dev *gspca_dev);
168static void mi1320_post_unset_alt(struct gspca_dev *gspca_dev);
169static int  mi1320_sensor_settings(struct gspca_dev *gspca_dev);
170static int  mi1320_camera_settings(struct gspca_dev *gspca_dev);
171/*==========================================================================*/
172
173void mi1320_init_settings(struct gspca_dev *gspca_dev)
174{
175	struct sd *sd = (struct sd *) gspca_dev;
176
177	sd->vcur.backlight  =  0;
178	sd->vcur.brightness =  0;
179	sd->vcur.sharpness  =  6;
180	sd->vcur.contrast   = 10;
181	sd->vcur.gamma      = 20;
182	sd->vcur.hue        =  0;
183	sd->vcur.saturation =  6;
184	sd->vcur.whitebal   =  0;
185	sd->vcur.mirror     = 0;
186	sd->vcur.flip       = 0;
187	sd->vcur.AC50Hz     = 1;
188
189	sd->vmax.backlight  =  2;
190	sd->vmax.brightness =  8;
191	sd->vmax.sharpness  =  7;
192	sd->vmax.contrast   =  0; /* 10 but not working with this driver */
193	sd->vmax.gamma      = 40;
194	sd->vmax.hue        =  5 + 1;
195	sd->vmax.saturation =  8;
196	sd->vmax.whitebal   =  2;
197	sd->vmax.mirror     = 1;
198	sd->vmax.flip       = 1;
199	sd->vmax.AC50Hz     = 1;
200
201	sd->dev_camera_settings = mi1320_camera_settings;
202	sd->dev_init_at_startup = mi1320_init_at_startup;
203	sd->dev_configure_alt   = mi1320_configure_alt;
204	sd->dev_init_pre_alt    = mi1320_init_pre_alt;
205	sd->dev_post_unset_alt  = mi1320_post_unset_alt;
206}
207
208/*==========================================================================*/
209
210static void common(struct gspca_dev *gspca_dev)
211{
212	s32 n; /* reserved for FETCH functions */
213
214	ctrl_out(gspca_dev, 0x40, 3, 0x0000, 0x0200, 22, dat_common00);
215	ctrl_out(gspca_dev, 0x40, 1, 0x0041, 0x0000, 0, NULL);
216	ctrl_out(gspca_dev, 0x40, 3, 0xba00, 0x0200, 32, dat_common01);
217	n = fetch_validx(gspca_dev, tbl_common, ARRAY_SIZE(tbl_common));
218	ctrl_out(gspca_dev, 0x40, 3, 0xba00, 0x0200, 48, dat_common02);
219	ctrl_out(gspca_dev, 0x40, 3, 0xba00, 0x0200, 48, dat_common03);
220	ctrl_out(gspca_dev, 0x40, 3, 0xba00, 0x0200, 16, dat_common04);
221	ctrl_out(gspca_dev, 0x40, 3, 0xba00, 0x0200, 48, dat_common05);
222	ctrl_out(gspca_dev, 0x40, 3, 0xba00, 0x0200, 44, dat_common06);
223	keep_on_fetching_validx(gspca_dev, tbl_common,
224					ARRAY_SIZE(tbl_common), n);
225	ctrl_out(gspca_dev, 0x40, 3, 0xba00, 0x0200, 52, dat_common07);
226	ctrl_out(gspca_dev, 0x40, 3, 0xba00, 0x0200, 48, dat_common08);
227	ctrl_out(gspca_dev, 0x40, 3, 0xba00, 0x0200, 48, dat_common09);
228	ctrl_out(gspca_dev, 0x40, 3, 0xba00, 0x0200, 56, dat_common10);
229	keep_on_fetching_validx(gspca_dev, tbl_common,
230					ARRAY_SIZE(tbl_common), n);
231	ctrl_out(gspca_dev, 0x40, 3, 0xba00, 0x0200, 40, dat_common11);
232	keep_on_fetching_validx(gspca_dev, tbl_common,
233					ARRAY_SIZE(tbl_common), n);
234}
235
236static int mi1320_init_at_startup(struct gspca_dev *gspca_dev)
237{
238	fetch_validx(gspca_dev, tbl_init_at_startup,
239				ARRAY_SIZE(tbl_init_at_startup));
240
241	common(gspca_dev);
242
243/*	ctrl_out(gspca_dev, 0x40, 11, 0x0000, 0x0000, 0, NULL); */
244
245	return 0;
246}
247
248static int mi1320_init_pre_alt(struct gspca_dev *gspca_dev)
249{
250	struct sd *sd = (struct sd *) gspca_dev;
251
252	sd->mirrorMask = 0;
253
254	sd->vold.backlight  = -1;
255	sd->vold.brightness = -1;
256	sd->vold.sharpness  = -1;
257	sd->vold.contrast   = -1;
258	sd->vold.saturation = -1;
259	sd->vold.gamma    = -1;
260	sd->vold.hue      = -1;
261	sd->vold.whitebal = -1;
262	sd->vold.mirror   = -1;
263	sd->vold.flip     = -1;
264	sd->vold.AC50Hz   = -1;
265
266	common(gspca_dev);
267
268	mi1320_sensor_settings(gspca_dev);
269
270	mi1320_init_post_alt(gspca_dev);
271
272	return 0;
273}
274
275static int mi1320_init_post_alt(struct gspca_dev *gspca_dev)
276{
277	mi1320_camera_settings(gspca_dev);
278
279	return 0;
280}
281
282static int mi1320_sensor_settings(struct gspca_dev *gspca_dev)
283{
284	s32 reso = gspca_dev->cam.cam_mode[(s32) gspca_dev->curr_mode].priv;
285
286	ctrl_out(gspca_dev, 0x40, 5, 0x0001, 0x0000, 0, NULL);
287
288	fetch_validx(gspca_dev, tbl_sensor_settings_common,
289				ARRAY_SIZE(tbl_sensor_settings_common));
290
291	switch (reso) {
292	case IMAGE_1280:
293		fetch_validx(gspca_dev, tbl_sensor_settings_1280,
294					ARRAY_SIZE(tbl_sensor_settings_1280));
295		ctrl_out(gspca_dev, 0x40, 3, 0xba00, 0x0200, 64, tbl_1280[0]);
296		ctrl_out(gspca_dev, 0x40, 3, 0xba00, 0x0200, 40, tbl_1280[1]);
297		ctrl_out(gspca_dev, 0x40, 3, 0x0000, 0x0200, 12, tbl_1280[2]);
298		break;
299
300	case IMAGE_800:
301		fetch_validx(gspca_dev, tbl_sensor_settings_800,
302					ARRAY_SIZE(tbl_sensor_settings_800));
303		ctrl_out(gspca_dev, 0x40, 3, 0xba00, 0x0200, 64, tbl_800[0]);
304		ctrl_out(gspca_dev, 0x40, 3, 0xba00, 0x0200, 40, tbl_800[1]);
305		ctrl_out(gspca_dev, 0x40, 3, 0x0000, 0x0200, 12, tbl_800[2]);
306		break;
307
308	default:
309		fetch_validx(gspca_dev, tbl_sensor_settings_640,
310					ARRAY_SIZE(tbl_sensor_settings_640));
311		ctrl_out(gspca_dev, 0x40, 3, 0xba00, 0x0200, 60, tbl_640[0]);
312		ctrl_out(gspca_dev, 0x40, 3, 0xba00, 0x0200, 40, tbl_640[1]);
313		ctrl_out(gspca_dev, 0x40, 3, 0x0000, 0x0200, 12, tbl_640[2]);
314		break;
315	}
316	return 0;
317}
318
319static int mi1320_configure_alt(struct gspca_dev *gspca_dev)
320{
321	s32 reso = gspca_dev->cam.cam_mode[(s32) gspca_dev->curr_mode].priv;
322
323	switch (reso) {
324	case IMAGE_640:
325		gspca_dev->alt = 3 + 1;
326		break;
327
328	case IMAGE_800:
329	case IMAGE_1280:
330		gspca_dev->alt = 1 + 1;
331		break;
332	}
333	return 0;
334}
335
336static int mi1320_camera_settings(struct gspca_dev *gspca_dev)
337{
338	struct sd *sd = (struct sd *) gspca_dev;
339
340	s32 backlight = sd->vcur.backlight;
341	s32 bright = sd->vcur.brightness;
342	s32 sharp  = sd->vcur.sharpness;
343	s32 cntr   = sd->vcur.contrast;
344	s32 gam	   = sd->vcur.gamma;
345	s32 hue    = sd->vcur.hue;
346	s32 sat	   = sd->vcur.saturation;
347	s32 wbal   = sd->vcur.whitebal;
348	s32 mirror = (((sd->vcur.mirror > 0) ^ sd->mirrorMask) > 0);
349	s32 flip   = (((sd->vcur.flip   > 0) ^ sd->mirrorMask) > 0);
350	s32 freq   = (sd->vcur.AC50Hz > 0);
351	s32 i;
352
353	if (freq != sd->vold.AC50Hz) {
354		sd->vold.AC50Hz = freq;
355
356		freq = 2 * (freq == 0);
357		ctrl_out(gspca_dev, 0x40, 1, 0xba00, 0x00f0, 0, NULL);
358		ctrl_out(gspca_dev, 0x40, 1, 0xba02, 0x00f1, 0, NULL);
359		ctrl_out(gspca_dev, 0x40, 1, 0xba00       , 0x005b, 0, NULL);
360		ctrl_out(gspca_dev, 0x40, 1, 0xba01 + freq, 0x00f1, 0, NULL);
361	}
362
363	if (wbal != sd->vold.whitebal) {
364		sd->vold.whitebal = wbal;
365		if (wbal < 0 || wbal > sd->vmax.whitebal)
366			wbal = 0;
367
368		for (i = 0; i < 2; i++) {
369			if (wbal == 0) { /* Normal light */
370				ctrl_out(gspca_dev, 0x40, 1,
371						0x0010, 0x0010, 0, NULL);
372				ctrl_out(gspca_dev, 0x40, 1,
373						0x0003, 0x00c1, 0, NULL);
374				ctrl_out(gspca_dev, 0x40, 1,
375						0x0042, 0x00c2, 0, NULL);
376				ctrl_out(gspca_dev, 0x40, 3,
377						0xba00, 0x0200, 48, dat_wbalNL);
378			}
379
380			if (wbal == 1) { /* Low light */
381				ctrl_out(gspca_dev, 0x40, 1,
382						0x0010, 0x0010, 0, NULL);
383				ctrl_out(gspca_dev, 0x40, 1,
384						0x0004, 0x00c1, 0, NULL);
385				ctrl_out(gspca_dev, 0x40, 1,
386						0x0043, 0x00c2, 0, NULL);
387				ctrl_out(gspca_dev, 0x40, 3,
388						0xba00, 0x0200, 48, dat_wbalLL);
389			}
390
391			if (wbal == 2) { /* Back light */
392				ctrl_out(gspca_dev, 0x40, 1,
393						0x0010, 0x0010, 0, NULL);
394				ctrl_out(gspca_dev, 0x40, 1,
395						0x0003, 0x00c1, 0, NULL);
396				ctrl_out(gspca_dev, 0x40, 1,
397						0x0042, 0x00c2, 0, NULL);
398				ctrl_out(gspca_dev, 0x40, 3,
399						0xba00, 0x0200, 44, dat_wbalBL);
400			}
401		}
402	}
403
404	if (bright != sd->vold.brightness) {
405		sd->vold.brightness = bright;
406		if (bright < 0 || bright > sd->vmax.brightness)
407			bright = 0;
408
409		bright = tbl_bright[bright];
410		ctrl_out(gspca_dev, 0x40, 1, 0xba00, 0x00f0, 0, NULL);
411		ctrl_out(gspca_dev, 0x40, 1, 0xba01, 0x00f1, 0, NULL);
412		ctrl_out(gspca_dev, 0x40, 1, 0xba00 + bright, 0x0034, 0, NULL);
413		ctrl_out(gspca_dev, 0x40, 1, 0xba00 + bright, 0x00f1, 0, NULL);
414	}
415
416	if (sat != sd->vold.saturation) {
417		sd->vold.saturation = sat;
418		if (sat < 0 || sat > sd->vmax.saturation)
419			sat = 0;
420
421		sat = tbl_sat[sat];
422		ctrl_out(gspca_dev, 0x40, 1, 0xba00, 0x00f0, 0, NULL);
423		ctrl_out(gspca_dev, 0x40, 1, 0xba01, 0x00f1, 0, NULL);
424		ctrl_out(gspca_dev, 0x40, 1, 0xba00      , 0x0025, 0, NULL);
425		ctrl_out(gspca_dev, 0x40, 1, 0xba00 + sat, 0x00f1, 0, NULL);
426	}
427
428	if (sharp != sd->vold.sharpness) {
429		sd->vold.sharpness = sharp;
430		if (sharp < 0 || sharp > sd->vmax.sharpness)
431			sharp = 0;
432
433		ctrl_out(gspca_dev, 0x40, 1, 0xba00, 0x00f0, 0, NULL);
434		ctrl_out(gspca_dev, 0x40, 1, 0xba01, 0x00f1, 0, NULL);
435		ctrl_out(gspca_dev, 0x40, 1, 0xba00        , 0x0005, 0, NULL);
436		ctrl_out(gspca_dev, 0x40, 1, 0xba00 + sharp, 0x00f1, 0, NULL);
437	}
438
439	if (hue != sd->vold.hue) {
440		/* 0=normal  1=NB  2="sepia"  3=negative  4=other  5=other2 */
441		if (hue < 0 || hue > sd->vmax.hue)
442			hue = 0;
443		if (hue == sd->vmax.hue)
444			sd->swapRB = 1;
445		else
446			sd->swapRB = 0;
447
448		ctrl_out(gspca_dev, 0x40, 1, 0xba00, 0x00f0, 0, NULL);
449		ctrl_out(gspca_dev, 0x40, 1, 0xba01, 0x00f1, 0, NULL);
450		ctrl_out(gspca_dev, 0x40, 1, 0xba70, 0x00e2, 0, NULL);
451		ctrl_out(gspca_dev, 0x40, 1, 0xba00 + hue * (hue < 6), 0x00f1,
452							0, NULL);
453	}
454
455	if (backlight != sd->vold.backlight) {
456		sd->vold.backlight = backlight;
457		if (backlight < 0 || backlight > sd->vmax.backlight)
458			backlight = 0;
459
460		backlight = tbl_backlight[backlight];
461		for (i = 0; i < 2; i++) {
462			ctrl_out(gspca_dev, 0x40, 1, 0xba00, 0x00f0, 0, NULL);
463			ctrl_out(gspca_dev, 0x40, 1, 0xba01, 0x00f1, 0, NULL);
464			ctrl_out(gspca_dev, 0x40, 1, 0xba74, 0x0006, 0, NULL);
465			ctrl_out(gspca_dev, 0x40, 1, 0xba80 + backlight, 0x00f1,
466								0, NULL);
467		}
468	}
469
470	if (hue != sd->vold.hue) {
471		sd->vold.hue = hue;
472
473		ctrl_out(gspca_dev, 0x40, 1, 0xba00, 0x00f0, 0, NULL);
474		ctrl_out(gspca_dev, 0x40, 1, 0xba01, 0x00f1, 0, NULL);
475		ctrl_out(gspca_dev, 0x40, 1, 0xba70, 0x00e2, 0, NULL);
476		ctrl_out(gspca_dev, 0x40, 1, 0xba00 + hue * (hue < 6), 0x00f1,
477							0, NULL);
478	}
479
480	if (mirror != sd->vold.mirror || flip != sd->vold.flip) {
481		u8 dat_hvflip2[4] = {0x20, 0x01, 0xf1, 0x00};
482		sd->vold.mirror = mirror;
483		sd->vold.flip = flip;
484
485		dat_hvflip2[3] = flip + 2 * mirror;
486		ctrl_out(gspca_dev, 0x40, 3, 0xba00, 0x0200, 4, dat_hvflip1);
487		ctrl_out(gspca_dev, 0x40, 3, 0xba00, 0x0200, 4, dat_hvflip2);
488	}
489
490	if (gam != sd->vold.gamma) {
491		sd->vold.gamma = gam;
492		if (gam < 0 || gam > sd->vmax.gamma)
493			gam = 0;
494
495		gam = 2 * gam;
496		ctrl_out(gspca_dev, 0x40, 1, 0xba00, 0x00f0, 0, NULL);
497		ctrl_out(gspca_dev, 0x40, 1, 0xba01, 0x00f1, 0, NULL);
498		ctrl_out(gspca_dev, 0x40, 1, 0xba04      , 0x003b, 0, NULL);
499		ctrl_out(gspca_dev, 0x40, 1, 0xba02 + gam, 0x00f1, 0, NULL);
500	}
501
502	if (cntr != sd->vold.contrast) {
503		sd->vold.contrast = cntr;
504		if (cntr < 0 || cntr > sd->vmax.contrast)
505			cntr = 0;
506
507		ctrl_out(gspca_dev, 0x40, 1, 0xba00, 0x00f0, 0, NULL);
508		ctrl_out(gspca_dev, 0x40, 1, 0xba01, 0x00f1, 0, NULL);
509		ctrl_out(gspca_dev, 0x40, 1, 0xba00 + tbl_cntr1[cntr], 0x0035,
510							0, NULL);
511		ctrl_out(gspca_dev, 0x40, 1, 0xba00 + tbl_cntr2[cntr], 0x00f1,
512							0, NULL);
513	}
514
515	return 0;
516}
517
518static void mi1320_post_unset_alt(struct gspca_dev *gspca_dev)
519{
520	ctrl_out(gspca_dev, 0x40, 5, 0x0000, 0x0000, 0, NULL);
521
522	fetch_validx(gspca_dev, tbl_post_unset_alt,
523				ARRAY_SIZE(tbl_post_unset_alt));
524}
525